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 https://opensource.org/licenses/CDDL-1.0.
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 void
90 vdev_error(const char *fmt, ...)
91 {
92 	va_list ap;
93 
94 	if (!error_seen) {
95 		(void) fprintf(stderr, gettext("invalid vdev specification\n"));
96 		if (!is_force)
97 			(void) fprintf(stderr, gettext("use '-f' to override "
98 			    "the following errors:\n"));
99 		else
100 			(void) fprintf(stderr, gettext("the following errors "
101 			    "must be manually repaired:\n"));
102 		error_seen = B_TRUE;
103 	}
104 
105 	va_start(ap, fmt);
106 	(void) vfprintf(stderr, fmt, ap);
107 	va_end(ap);
108 }
109 
110 /*
111  * Check that a file is valid.  All we can do in this case is check that it's
112  * not in use by another pool, and not in use by swap.
113  */
114 int
115 check_file_generic(const char *file, boolean_t force, boolean_t isspare)
116 {
117 	char  *name;
118 	int fd;
119 	int ret = 0;
120 	pool_state_t state;
121 	boolean_t inuse;
122 
123 	if ((fd = open(file, O_RDONLY)) < 0)
124 		return (0);
125 
126 	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
127 		const char *desc;
128 
129 		switch (state) {
130 		case POOL_STATE_ACTIVE:
131 			desc = gettext("active");
132 			break;
133 
134 		case POOL_STATE_EXPORTED:
135 			desc = gettext("exported");
136 			break;
137 
138 		case POOL_STATE_POTENTIALLY_ACTIVE:
139 			desc = gettext("potentially active");
140 			break;
141 
142 		default:
143 			desc = gettext("unknown");
144 			break;
145 		}
146 
147 		/*
148 		 * Allow hot spares to be shared between pools.
149 		 */
150 		if (state == POOL_STATE_SPARE && isspare) {
151 			free(name);
152 			(void) close(fd);
153 			return (0);
154 		}
155 
156 		if (state == POOL_STATE_ACTIVE ||
157 		    state == POOL_STATE_SPARE || !force) {
158 			switch (state) {
159 			case POOL_STATE_SPARE:
160 				vdev_error(gettext("%s is reserved as a hot "
161 				    "spare for pool %s\n"), file, name);
162 				break;
163 			default:
164 				vdev_error(gettext("%s is part of %s pool "
165 				    "'%s'\n"), file, desc, name);
166 				break;
167 			}
168 			ret = -1;
169 		}
170 
171 		free(name);
172 	}
173 
174 	(void) close(fd);
175 	return (ret);
176 }
177 
178 /*
179  * This may be a shorthand device path or it could be total gibberish.
180  * Check to see if it is a known device available in zfs_vdev_paths.
181  * As part of this check, see if we've been given an entire disk
182  * (minus the slice number).
183  */
184 static int
185 is_shorthand_path(const char *arg, char *path, size_t path_size,
186     struct stat64 *statbuf, boolean_t *wholedisk)
187 {
188 	int error;
189 
190 	error = zfs_resolve_shortname(arg, path, path_size);
191 	if (error == 0) {
192 		*wholedisk = zfs_dev_is_whole_disk(path);
193 		if (*wholedisk || (stat64(path, statbuf) == 0))
194 			return (0);
195 	}
196 
197 	strlcpy(path, arg, path_size);
198 	memset(statbuf, 0, sizeof (*statbuf));
199 	*wholedisk = B_FALSE;
200 
201 	return (error);
202 }
203 
204 /*
205  * Determine if the given path is a hot spare within the given configuration.
206  * If no configuration is given we rely solely on the label.
207  */
208 static boolean_t
209 is_spare(nvlist_t *config, const char *path)
210 {
211 	int fd;
212 	pool_state_t state;
213 	char *name = NULL;
214 	nvlist_t *label;
215 	uint64_t guid, spareguid;
216 	nvlist_t *nvroot;
217 	nvlist_t **spares;
218 	uint_t i, nspares;
219 	boolean_t inuse;
220 
221 	if (zpool_is_draid_spare(path))
222 		return (B_TRUE);
223 
224 	if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
225 		return (B_FALSE);
226 
227 	if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
228 	    !inuse ||
229 	    state != POOL_STATE_SPARE ||
230 	    zpool_read_label(fd, &label, NULL) != 0) {
231 		free(name);
232 		(void) close(fd);
233 		return (B_FALSE);
234 	}
235 	free(name);
236 	(void) close(fd);
237 
238 	if (config == NULL) {
239 		nvlist_free(label);
240 		return (B_TRUE);
241 	}
242 
243 	verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
244 	nvlist_free(label);
245 
246 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
247 	    &nvroot) == 0);
248 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
249 	    &spares, &nspares) == 0) {
250 		for (i = 0; i < nspares; i++) {
251 			verify(nvlist_lookup_uint64(spares[i],
252 			    ZPOOL_CONFIG_GUID, &spareguid) == 0);
253 			if (spareguid == guid)
254 				return (B_TRUE);
255 		}
256 	}
257 
258 	return (B_FALSE);
259 }
260 
261 /*
262  * Create a leaf vdev.  Determine if this is a file or a device.  If it's a
263  * device, fill in the device id to make a complete nvlist.  Valid forms for a
264  * leaf vdev are:
265  *
266  *	/dev/xxx	Complete disk path
267  *	/xxx		Full path to file
268  *	xxx		Shorthand for <zfs_vdev_paths>/xxx
269  *	draid*		Virtual dRAID spare
270  */
271 static nvlist_t *
272 make_leaf_vdev(nvlist_t *props, const char *arg, boolean_t is_primary)
273 {
274 	char path[MAXPATHLEN];
275 	struct stat64 statbuf;
276 	nvlist_t *vdev = NULL;
277 	const 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 if (zpool_is_draid_spare(arg)) {
313 		if (!is_primary) {
314 			(void) fprintf(stderr,
315 			    gettext("cannot open '%s': dRAID spares can only "
316 			    "be used to replace primary vdevs\n"), arg);
317 			return (NULL);
318 		}
319 
320 		wholedisk = B_TRUE;
321 		strlcpy(path, arg, sizeof (path));
322 		type = VDEV_TYPE_DRAID_SPARE;
323 	} else {
324 		err = is_shorthand_path(arg, path, sizeof (path),
325 		    &statbuf, &wholedisk);
326 		if (err != 0) {
327 			/*
328 			 * If we got ENOENT, then the user gave us
329 			 * gibberish, so try to direct them with a
330 			 * reasonable error message.  Otherwise,
331 			 * regurgitate strerror() since it's the best we
332 			 * can do.
333 			 */
334 			if (err == ENOENT) {
335 				(void) fprintf(stderr,
336 				    gettext("cannot open '%s': no such "
337 				    "device in %s\n"), arg, DISK_ROOT);
338 				(void) fprintf(stderr,
339 				    gettext("must be a full path or "
340 				    "shorthand device name\n"));
341 				return (NULL);
342 			} else {
343 				(void) fprintf(stderr,
344 				    gettext("cannot open '%s': %s\n"),
345 				    path, strerror(errno));
346 				return (NULL);
347 			}
348 		}
349 	}
350 
351 	if (type == NULL) {
352 		/*
353 		 * Determine whether this is a device or a file.
354 		 */
355 		if (wholedisk || S_ISBLK(statbuf.st_mode)) {
356 			type = VDEV_TYPE_DISK;
357 		} else if (S_ISREG(statbuf.st_mode)) {
358 			type = VDEV_TYPE_FILE;
359 		} else {
360 			fprintf(stderr, gettext("cannot use '%s': must "
361 			    "be a block device or regular file\n"), path);
362 			return (NULL);
363 		}
364 	}
365 
366 	/*
367 	 * Finally, we have the complete device or file, and we know that it is
368 	 * acceptable to use.  Construct the nvlist to describe this vdev.  All
369 	 * vdevs have a 'path' element, and devices also have a 'devid' element.
370 	 */
371 	verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
372 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
373 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
374 
375 	if (strcmp(type, VDEV_TYPE_DISK) == 0)
376 		verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
377 		    (uint64_t)wholedisk) == 0);
378 
379 	/*
380 	 * Override defaults if custom properties are provided.
381 	 */
382 	if (props != NULL) {
383 		const char *value = NULL;
384 
385 		if (nvlist_lookup_string(props,
386 		    zpool_prop_to_name(ZPOOL_PROP_ASHIFT), &value) == 0) {
387 			if (zfs_nicestrtonum(NULL, value, &ashift) != 0) {
388 				(void) fprintf(stderr,
389 				    gettext("ashift must be a number.\n"));
390 				return (NULL);
391 			}
392 			if (ashift != 0 &&
393 			    (ashift < ASHIFT_MIN || ashift > ASHIFT_MAX)) {
394 				(void) fprintf(stderr,
395 				    gettext("invalid 'ashift=%" PRIu64 "' "
396 				    "property: only values between %" PRId32 " "
397 				    "and %" PRId32 " are allowed.\n"),
398 				    ashift, ASHIFT_MIN, ASHIFT_MAX);
399 				return (NULL);
400 			}
401 		}
402 	}
403 
404 	/*
405 	 * If the device is known to incorrectly report its physical sector
406 	 * size explicitly provide the known correct value.
407 	 */
408 	if (ashift == 0) {
409 		int sector_size;
410 
411 		if (check_sector_size_database(path, &sector_size) == B_TRUE)
412 			ashift = highbit64(sector_size) - 1;
413 	}
414 
415 	if (ashift > 0)
416 		(void) nvlist_add_uint64(vdev, ZPOOL_CONFIG_ASHIFT, ashift);
417 
418 	return (vdev);
419 }
420 
421 /*
422  * Go through and verify the replication level of the pool is consistent.
423  * Performs the following checks:
424  *
425  * 	For the new spec, verifies that devices in mirrors and raidz are the
426  * 	same size.
427  *
428  * 	If the current configuration already has inconsistent replication
429  * 	levels, ignore any other potential problems in the new spec.
430  *
431  * 	Otherwise, make sure that the current spec (if there is one) and the new
432  * 	spec have consistent replication levels.
433  *
434  *	If there is no current spec (create), make sure new spec has at least
435  *	one general purpose vdev.
436  */
437 typedef struct replication_level {
438 	const char *zprl_type;
439 	uint64_t zprl_children;
440 	uint64_t zprl_parity;
441 } replication_level_t;
442 
443 #define	ZPOOL_FUZZ	(16 * 1024 * 1024)
444 
445 /*
446  * N.B. For the purposes of comparing replication levels dRAID can be
447  * considered functionally equivalent to raidz.
448  */
449 static boolean_t
450 is_raidz_mirror(replication_level_t *a, replication_level_t *b,
451     replication_level_t **raidz, replication_level_t **mirror)
452 {
453 	if ((strcmp(a->zprl_type, "raidz") == 0 ||
454 	    strcmp(a->zprl_type, "draid") == 0) &&
455 	    strcmp(b->zprl_type, "mirror") == 0) {
456 		*raidz = a;
457 		*mirror = b;
458 		return (B_TRUE);
459 	}
460 	return (B_FALSE);
461 }
462 
463 /*
464  * Comparison for determining if dRAID and raidz where passed in either order.
465  */
466 static boolean_t
467 is_raidz_draid(replication_level_t *a, replication_level_t *b)
468 {
469 	if ((strcmp(a->zprl_type, "raidz") == 0 ||
470 	    strcmp(a->zprl_type, "draid") == 0) &&
471 	    (strcmp(b->zprl_type, "raidz") == 0 ||
472 	    strcmp(b->zprl_type, "draid") == 0)) {
473 		return (B_TRUE);
474 	}
475 
476 	return (B_FALSE);
477 }
478 
479 /*
480  * Given a list of toplevel vdevs, return the current replication level.  If
481  * the config is inconsistent, then NULL is returned.  If 'fatal' is set, then
482  * an error message will be displayed for each self-inconsistent vdev.
483  */
484 static replication_level_t *
485 get_replication(nvlist_t *nvroot, boolean_t fatal)
486 {
487 	nvlist_t **top;
488 	uint_t t, toplevels;
489 	nvlist_t **child;
490 	uint_t c, children;
491 	nvlist_t *nv;
492 	const char *type;
493 	replication_level_t lastrep = {0};
494 	replication_level_t rep;
495 	replication_level_t *ret;
496 	replication_level_t *raidz, *mirror;
497 	boolean_t dontreport;
498 
499 	ret = safe_malloc(sizeof (replication_level_t));
500 
501 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
502 	    &top, &toplevels) == 0);
503 
504 	for (t = 0; t < toplevels; t++) {
505 		uint64_t is_log = B_FALSE;
506 
507 		nv = top[t];
508 
509 		/*
510 		 * For separate logs we ignore the top level vdev replication
511 		 * constraints.
512 		 */
513 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
514 		if (is_log)
515 			continue;
516 
517 		/*
518 		 * Ignore holes introduced by removing aux devices, along
519 		 * with indirect vdevs introduced by previously removed
520 		 * vdevs.
521 		 */
522 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
523 		if (strcmp(type, VDEV_TYPE_HOLE) == 0 ||
524 		    strcmp(type, VDEV_TYPE_INDIRECT) == 0)
525 			continue;
526 
527 		if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
528 		    &child, &children) != 0) {
529 			/*
530 			 * This is a 'file' or 'disk' vdev.
531 			 */
532 			rep.zprl_type = type;
533 			rep.zprl_children = 1;
534 			rep.zprl_parity = 0;
535 		} else {
536 			int64_t vdev_size;
537 
538 			/*
539 			 * This is a mirror or RAID-Z vdev.  Go through and make
540 			 * sure the contents are all the same (files vs. disks),
541 			 * keeping track of the number of elements in the
542 			 * process.
543 			 *
544 			 * We also check that the size of each vdev (if it can
545 			 * be determined) is the same.
546 			 */
547 			rep.zprl_type = type;
548 			rep.zprl_children = 0;
549 
550 			if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
551 			    strcmp(type, VDEV_TYPE_DRAID) == 0) {
552 				verify(nvlist_lookup_uint64(nv,
553 				    ZPOOL_CONFIG_NPARITY,
554 				    &rep.zprl_parity) == 0);
555 				assert(rep.zprl_parity != 0);
556 			} else {
557 				rep.zprl_parity = 0;
558 			}
559 
560 			/*
561 			 * The 'dontreport' variable indicates that we've
562 			 * already reported an error for this spec, so don't
563 			 * bother doing it again.
564 			 */
565 			type = NULL;
566 			dontreport = 0;
567 			vdev_size = -1LL;
568 			for (c = 0; c < children; c++) {
569 				nvlist_t *cnv = child[c];
570 				const char *path;
571 				struct stat64 statbuf;
572 				int64_t size = -1LL;
573 				const char *childtype;
574 				int fd, err;
575 
576 				rep.zprl_children++;
577 
578 				verify(nvlist_lookup_string(cnv,
579 				    ZPOOL_CONFIG_TYPE, &childtype) == 0);
580 
581 				/*
582 				 * If this is a replacing or spare vdev, then
583 				 * get the real first child of the vdev: do this
584 				 * in a loop because replacing and spare vdevs
585 				 * can be nested.
586 				 */
587 				while (strcmp(childtype,
588 				    VDEV_TYPE_REPLACING) == 0 ||
589 				    strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
590 					nvlist_t **rchild;
591 					uint_t rchildren;
592 
593 					verify(nvlist_lookup_nvlist_array(cnv,
594 					    ZPOOL_CONFIG_CHILDREN, &rchild,
595 					    &rchildren) == 0);
596 					assert(rchildren == 2);
597 					cnv = rchild[0];
598 
599 					verify(nvlist_lookup_string(cnv,
600 					    ZPOOL_CONFIG_TYPE,
601 					    &childtype) == 0);
602 				}
603 
604 				verify(nvlist_lookup_string(cnv,
605 				    ZPOOL_CONFIG_PATH, &path) == 0);
606 
607 				/*
608 				 * If we have a raidz/mirror that combines disks
609 				 * with files, report it as an error.
610 				 */
611 				if (!dontreport && type != NULL &&
612 				    strcmp(type, childtype) != 0) {
613 					if (ret != NULL)
614 						free(ret);
615 					ret = NULL;
616 					if (fatal)
617 						vdev_error(gettext(
618 						    "mismatched replication "
619 						    "level: %s contains both "
620 						    "files and devices\n"),
621 						    rep.zprl_type);
622 					else
623 						return (NULL);
624 					dontreport = B_TRUE;
625 				}
626 
627 				/*
628 				 * According to stat(2), the value of 'st_size'
629 				 * is undefined for block devices and character
630 				 * devices.  But there is no effective way to
631 				 * determine the real size in userland.
632 				 *
633 				 * Instead, we'll take advantage of an
634 				 * implementation detail of spec_size().  If the
635 				 * device is currently open, then we (should)
636 				 * return a valid size.
637 				 *
638 				 * If we still don't get a valid size (indicated
639 				 * by a size of 0 or MAXOFFSET_T), then ignore
640 				 * this device altogether.
641 				 */
642 				if ((fd = open(path, O_RDONLY)) >= 0) {
643 					err = fstat64_blk(fd, &statbuf);
644 					(void) close(fd);
645 				} else {
646 					err = stat64(path, &statbuf);
647 				}
648 
649 				if (err != 0 ||
650 				    statbuf.st_size == 0 ||
651 				    statbuf.st_size == MAXOFFSET_T)
652 					continue;
653 
654 				size = statbuf.st_size;
655 
656 				/*
657 				 * Also make sure that devices and
658 				 * slices have a consistent size.  If
659 				 * they differ by a significant amount
660 				 * (~16MB) then report an error.
661 				 */
662 				if (!dontreport &&
663 				    (vdev_size != -1LL &&
664 				    (llabs(size - vdev_size) >
665 				    ZPOOL_FUZZ))) {
666 					if (ret != NULL)
667 						free(ret);
668 					ret = NULL;
669 					if (fatal)
670 						vdev_error(gettext(
671 						    "%s contains devices of "
672 						    "different sizes\n"),
673 						    rep.zprl_type);
674 					else
675 						return (NULL);
676 					dontreport = B_TRUE;
677 				}
678 
679 				type = childtype;
680 				vdev_size = size;
681 			}
682 		}
683 
684 		/*
685 		 * At this point, we have the replication of the last toplevel
686 		 * vdev in 'rep'.  Compare it to 'lastrep' to see if it is
687 		 * different.
688 		 */
689 		if (lastrep.zprl_type != NULL) {
690 			if (is_raidz_mirror(&lastrep, &rep, &raidz, &mirror) ||
691 			    is_raidz_mirror(&rep, &lastrep, &raidz, &mirror)) {
692 				/*
693 				 * Accepted raidz and mirror when they can
694 				 * handle the same number of disk failures.
695 				 */
696 				if (raidz->zprl_parity !=
697 				    mirror->zprl_children - 1) {
698 					if (ret != NULL)
699 						free(ret);
700 					ret = NULL;
701 					if (fatal)
702 						vdev_error(gettext(
703 						    "mismatched replication "
704 						    "level: "
705 						    "%s and %s vdevs with "
706 						    "different redundancy, "
707 						    "%llu vs. %llu (%llu-way) "
708 						    "are present\n"),
709 						    raidz->zprl_type,
710 						    mirror->zprl_type,
711 						    (u_longlong_t)
712 						    raidz->zprl_parity,
713 						    (u_longlong_t)
714 						    mirror->zprl_children - 1,
715 						    (u_longlong_t)
716 						    mirror->zprl_children);
717 					else
718 						return (NULL);
719 				}
720 			} else if (is_raidz_draid(&lastrep, &rep)) {
721 				/*
722 				 * Accepted raidz and draid when they can
723 				 * handle the same number of disk failures.
724 				 */
725 				if (lastrep.zprl_parity != rep.zprl_parity) {
726 					if (ret != NULL)
727 						free(ret);
728 					ret = NULL;
729 					if (fatal)
730 						vdev_error(gettext(
731 						    "mismatched replication "
732 						    "level: %s and %s vdevs "
733 						    "with different "
734 						    "redundancy, %llu vs. "
735 						    "%llu are present\n"),
736 						    lastrep.zprl_type,
737 						    rep.zprl_type,
738 						    (u_longlong_t)
739 						    lastrep.zprl_parity,
740 						    (u_longlong_t)
741 						    rep.zprl_parity);
742 					else
743 						return (NULL);
744 				}
745 			} else if (strcmp(lastrep.zprl_type, rep.zprl_type) !=
746 			    0) {
747 				if (ret != NULL)
748 					free(ret);
749 				ret = NULL;
750 				if (fatal)
751 					vdev_error(gettext(
752 					    "mismatched replication level: "
753 					    "both %s and %s vdevs are "
754 					    "present\n"),
755 					    lastrep.zprl_type, rep.zprl_type);
756 				else
757 					return (NULL);
758 			} else if (lastrep.zprl_parity != rep.zprl_parity) {
759 				if (ret)
760 					free(ret);
761 				ret = NULL;
762 				if (fatal)
763 					vdev_error(gettext(
764 					    "mismatched replication level: "
765 					    "both %llu and %llu device parity "
766 					    "%s vdevs are present\n"),
767 					    (u_longlong_t)
768 					    lastrep.zprl_parity,
769 					    (u_longlong_t)rep.zprl_parity,
770 					    rep.zprl_type);
771 				else
772 					return (NULL);
773 			} else if (lastrep.zprl_children != rep.zprl_children) {
774 				if (ret)
775 					free(ret);
776 				ret = NULL;
777 				if (fatal)
778 					vdev_error(gettext(
779 					    "mismatched replication level: "
780 					    "both %llu-way and %llu-way %s "
781 					    "vdevs are present\n"),
782 					    (u_longlong_t)
783 					    lastrep.zprl_children,
784 					    (u_longlong_t)
785 					    rep.zprl_children,
786 					    rep.zprl_type);
787 				else
788 					return (NULL);
789 			}
790 		}
791 		lastrep = rep;
792 	}
793 
794 	if (ret != NULL)
795 		*ret = rep;
796 
797 	return (ret);
798 }
799 
800 /*
801  * Check the replication level of the vdev spec against the current pool.  Calls
802  * get_replication() to make sure the new spec is self-consistent.  If the pool
803  * has a consistent replication level, then we ignore any errors.  Otherwise,
804  * report any difference between the two.
805  */
806 static int
807 check_replication(nvlist_t *config, nvlist_t *newroot)
808 {
809 	nvlist_t **child;
810 	uint_t	children;
811 	replication_level_t *current = NULL, *new;
812 	replication_level_t *raidz, *mirror;
813 	int ret;
814 
815 	/*
816 	 * If we have a current pool configuration, check to see if it's
817 	 * self-consistent.  If not, simply return success.
818 	 */
819 	if (config != NULL) {
820 		nvlist_t *nvroot;
821 
822 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
823 		    &nvroot) == 0);
824 		if ((current = get_replication(nvroot, B_FALSE)) == NULL)
825 			return (0);
826 	}
827 	/*
828 	 * for spares there may be no children, and therefore no
829 	 * replication level to check
830 	 */
831 	if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
832 	    &child, &children) != 0) || (children == 0)) {
833 		free(current);
834 		return (0);
835 	}
836 
837 	/*
838 	 * If all we have is logs then there's no replication level to check.
839 	 */
840 	if (num_logs(newroot) == children) {
841 		free(current);
842 		return (0);
843 	}
844 
845 	/*
846 	 * Get the replication level of the new vdev spec, reporting any
847 	 * inconsistencies found.
848 	 */
849 	if ((new = get_replication(newroot, B_TRUE)) == NULL) {
850 		free(current);
851 		return (-1);
852 	}
853 
854 	/*
855 	 * Check to see if the new vdev spec matches the replication level of
856 	 * the current pool.
857 	 */
858 	ret = 0;
859 	if (current != NULL) {
860 		if (is_raidz_mirror(current, new, &raidz, &mirror) ||
861 		    is_raidz_mirror(new, current, &raidz, &mirror)) {
862 			if (raidz->zprl_parity != mirror->zprl_children - 1) {
863 				vdev_error(gettext(
864 				    "mismatched replication level: pool and "
865 				    "new vdev with different redundancy, %s "
866 				    "and %s vdevs, %llu vs. %llu (%llu-way)\n"),
867 				    raidz->zprl_type,
868 				    mirror->zprl_type,
869 				    (u_longlong_t)raidz->zprl_parity,
870 				    (u_longlong_t)mirror->zprl_children - 1,
871 				    (u_longlong_t)mirror->zprl_children);
872 				ret = -1;
873 			}
874 		} else if (strcmp(current->zprl_type, new->zprl_type) != 0) {
875 			vdev_error(gettext(
876 			    "mismatched replication level: pool uses %s "
877 			    "and new vdev is %s\n"),
878 			    current->zprl_type, new->zprl_type);
879 			ret = -1;
880 		} else if (current->zprl_parity != new->zprl_parity) {
881 			vdev_error(gettext(
882 			    "mismatched replication level: pool uses %llu "
883 			    "device parity and new vdev uses %llu\n"),
884 			    (u_longlong_t)current->zprl_parity,
885 			    (u_longlong_t)new->zprl_parity);
886 			ret = -1;
887 		} else if (current->zprl_children != new->zprl_children) {
888 			vdev_error(gettext(
889 			    "mismatched replication level: pool uses %llu-way "
890 			    "%s and new vdev uses %llu-way %s\n"),
891 			    (u_longlong_t)current->zprl_children,
892 			    current->zprl_type,
893 			    (u_longlong_t)new->zprl_children,
894 			    new->zprl_type);
895 			ret = -1;
896 		}
897 	}
898 
899 	free(new);
900 	if (current != NULL)
901 		free(current);
902 
903 	return (ret);
904 }
905 
906 static int
907 zero_label(const char *path)
908 {
909 	const int size = 4096;
910 	char buf[size];
911 	int err, fd;
912 
913 	if ((fd = open(path, O_WRONLY|O_EXCL)) < 0) {
914 		(void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
915 		    path, strerror(errno));
916 		return (-1);
917 	}
918 
919 	memset(buf, 0, size);
920 	err = write(fd, buf, size);
921 	(void) fdatasync(fd);
922 	(void) close(fd);
923 
924 	if (err == -1) {
925 		(void) fprintf(stderr, gettext("cannot zero first %d bytes "
926 		    "of '%s': %s\n"), size, path, strerror(errno));
927 		return (-1);
928 	}
929 
930 	if (err != size) {
931 		(void) fprintf(stderr, gettext("could only zero %d/%d bytes "
932 		    "of '%s'\n"), err, size, path);
933 		return (-1);
934 	}
935 
936 	return (0);
937 }
938 
939 /*
940  * Go through and find any whole disks in the vdev specification, labelling them
941  * as appropriate.  When constructing the vdev spec, we were unable to open this
942  * device in order to provide a devid.  Now that we have labelled the disk and
943  * know that slice 0 is valid, we can construct the devid now.
944  *
945  * If the disk was already labeled with an EFI label, we will have gotten the
946  * devid already (because we were able to open the whole disk).  Otherwise, we
947  * need to get the devid after we label the disk.
948  */
949 static int
950 make_disks(zpool_handle_t *zhp, nvlist_t *nv)
951 {
952 	nvlist_t **child;
953 	uint_t c, children;
954 	const char *type, *path;
955 	char devpath[MAXPATHLEN];
956 	char udevpath[MAXPATHLEN];
957 	uint64_t wholedisk;
958 	struct stat64 statbuf;
959 	int is_exclusive = 0;
960 	int fd;
961 	int ret;
962 
963 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
964 
965 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
966 	    &child, &children) != 0) {
967 
968 		if (strcmp(type, VDEV_TYPE_DISK) != 0)
969 			return (0);
970 
971 		/*
972 		 * We have a disk device.  If this is a whole disk write
973 		 * out the efi partition table, otherwise write zero's to
974 		 * the first 4k of the partition.  This is to ensure that
975 		 * libblkid will not misidentify the partition due to a
976 		 * magic value left by the previous filesystem.
977 		 */
978 		verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
979 		verify(!nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
980 		    &wholedisk));
981 
982 		if (!wholedisk) {
983 			/*
984 			 * Update device id string for mpath nodes (Linux only)
985 			 */
986 			if (is_mpath_whole_disk(path))
987 				update_vdev_config_dev_strs(nv);
988 
989 			if (!is_spare(NULL, path))
990 				(void) zero_label(path);
991 			return (0);
992 		}
993 
994 		if (realpath(path, devpath) == NULL) {
995 			ret = errno;
996 			(void) fprintf(stderr,
997 			    gettext("cannot resolve path '%s'\n"), path);
998 			return (ret);
999 		}
1000 
1001 		/*
1002 		 * Remove any previously existing symlink from a udev path to
1003 		 * the device before labeling the disk.  This ensures that
1004 		 * only newly created links are used.  Otherwise there is a
1005 		 * window between when udev deletes and recreates the link
1006 		 * during which access attempts will fail with ENOENT.
1007 		 */
1008 		strlcpy(udevpath, path, MAXPATHLEN);
1009 		(void) zfs_append_partition(udevpath, MAXPATHLEN);
1010 
1011 		fd = open(devpath, O_RDWR|O_EXCL);
1012 		if (fd == -1) {
1013 			if (errno == EBUSY)
1014 				is_exclusive = 1;
1015 #ifdef __FreeBSD__
1016 			if (errno == EPERM)
1017 				is_exclusive = 1;
1018 #endif
1019 		} else {
1020 			(void) close(fd);
1021 		}
1022 
1023 		/*
1024 		 * If the partition exists, contains a valid spare label,
1025 		 * and is opened exclusively there is no need to partition
1026 		 * it.  Hot spares have already been partitioned and are
1027 		 * held open exclusively by the kernel as a safety measure.
1028 		 *
1029 		 * If the provided path is for a /dev/disk/ device its
1030 		 * symbolic link will be removed, partition table created,
1031 		 * and then block until udev creates the new link.
1032 		 */
1033 		if (!is_exclusive && !is_spare(NULL, udevpath)) {
1034 			char *devnode = strrchr(devpath, '/') + 1;
1035 
1036 			ret = strncmp(udevpath, UDISK_ROOT, strlen(UDISK_ROOT));
1037 			if (ret == 0) {
1038 				ret = lstat64(udevpath, &statbuf);
1039 				if (ret == 0 && S_ISLNK(statbuf.st_mode))
1040 					(void) unlink(udevpath);
1041 			}
1042 
1043 			/*
1044 			 * When labeling a pool the raw device node name
1045 			 * is provided as it appears under /dev/.
1046 			 */
1047 			if (zpool_label_disk(g_zfs, zhp, devnode) == -1)
1048 				return (-1);
1049 
1050 			/*
1051 			 * Wait for udev to signal the device is available
1052 			 * by the provided path.
1053 			 */
1054 			ret = zpool_label_disk_wait(udevpath, DISK_LABEL_WAIT);
1055 			if (ret) {
1056 				(void) fprintf(stderr,
1057 				    gettext("missing link: %s was "
1058 				    "partitioned but %s is missing\n"),
1059 				    devnode, udevpath);
1060 				return (ret);
1061 			}
1062 
1063 			ret = zero_label(udevpath);
1064 			if (ret)
1065 				return (ret);
1066 		}
1067 
1068 		/*
1069 		 * Update the path to refer to the partition.  The presence of
1070 		 * the 'whole_disk' field indicates to the CLI that we should
1071 		 * chop off the partition number when displaying the device in
1072 		 * future output.
1073 		 */
1074 		verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0);
1075 
1076 		/*
1077 		 * Update device id strings for whole disks (Linux only)
1078 		 */
1079 		update_vdev_config_dev_strs(nv);
1080 
1081 		return (0);
1082 	}
1083 
1084 	for (c = 0; c < children; c++)
1085 		if ((ret = make_disks(zhp, child[c])) != 0)
1086 			return (ret);
1087 
1088 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1089 	    &child, &children) == 0)
1090 		for (c = 0; c < children; c++)
1091 			if ((ret = make_disks(zhp, child[c])) != 0)
1092 				return (ret);
1093 
1094 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1095 	    &child, &children) == 0)
1096 		for (c = 0; c < children; c++)
1097 			if ((ret = make_disks(zhp, child[c])) != 0)
1098 				return (ret);
1099 
1100 	return (0);
1101 }
1102 
1103 /*
1104  * Go through and find any devices that are in use.  We rely on libdiskmgt for
1105  * the majority of this task.
1106  */
1107 static boolean_t
1108 is_device_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1109     boolean_t replacing, boolean_t isspare)
1110 {
1111 	nvlist_t **child;
1112 	uint_t c, children;
1113 	const char *type, *path;
1114 	int ret = 0;
1115 	char buf[MAXPATHLEN];
1116 	uint64_t wholedisk = B_FALSE;
1117 	boolean_t anyinuse = B_FALSE;
1118 
1119 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1120 
1121 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1122 	    &child, &children) != 0) {
1123 
1124 		verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
1125 		if (strcmp(type, VDEV_TYPE_DISK) == 0)
1126 			verify(!nvlist_lookup_uint64(nv,
1127 			    ZPOOL_CONFIG_WHOLE_DISK, &wholedisk));
1128 
1129 		/*
1130 		 * As a generic check, we look to see if this is a replace of a
1131 		 * hot spare within the same pool.  If so, we allow it
1132 		 * regardless of what libblkid or zpool_in_use() says.
1133 		 */
1134 		if (replacing) {
1135 			(void) strlcpy(buf, path, sizeof (buf));
1136 			if (wholedisk) {
1137 				ret = zfs_append_partition(buf,  sizeof (buf));
1138 				if (ret == -1)
1139 					return (-1);
1140 			}
1141 
1142 			if (is_spare(config, buf))
1143 				return (B_FALSE);
1144 		}
1145 
1146 		if (strcmp(type, VDEV_TYPE_DISK) == 0)
1147 			ret = check_device(path, force, isspare, wholedisk);
1148 
1149 		else if (strcmp(type, VDEV_TYPE_FILE) == 0)
1150 			ret = check_file(path, force, isspare);
1151 
1152 		return (ret != 0);
1153 	}
1154 
1155 	for (c = 0; c < children; c++)
1156 		if (is_device_in_use(config, child[c], force, replacing,
1157 		    B_FALSE))
1158 			anyinuse = B_TRUE;
1159 
1160 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1161 	    &child, &children) == 0)
1162 		for (c = 0; c < children; c++)
1163 			if (is_device_in_use(config, child[c], force, replacing,
1164 			    B_TRUE))
1165 				anyinuse = B_TRUE;
1166 
1167 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1168 	    &child, &children) == 0)
1169 		for (c = 0; c < children; c++)
1170 			if (is_device_in_use(config, child[c], force, replacing,
1171 			    B_FALSE))
1172 				anyinuse = B_TRUE;
1173 
1174 	return (anyinuse);
1175 }
1176 
1177 /*
1178  * Returns the parity level extracted from a raidz or draid type.
1179  * If the parity cannot be determined zero is returned.
1180  */
1181 static int
1182 get_parity(const char *type)
1183 {
1184 	long parity = 0;
1185 	const char *p;
1186 
1187 	if (strncmp(type, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0) {
1188 		p = type + strlen(VDEV_TYPE_RAIDZ);
1189 
1190 		if (*p == '\0') {
1191 			/* when unspecified default to single parity */
1192 			return (1);
1193 		} else if (*p == '0') {
1194 			/* no zero prefixes allowed */
1195 			return (0);
1196 		} else {
1197 			/* 0-3, no suffixes allowed */
1198 			char *end;
1199 			errno = 0;
1200 			parity = strtol(p, &end, 10);
1201 			if (errno != 0 || *end != '\0' ||
1202 			    parity < 1 || parity > VDEV_RAIDZ_MAXPARITY) {
1203 				return (0);
1204 			}
1205 		}
1206 	} else if (strncmp(type, VDEV_TYPE_DRAID,
1207 	    strlen(VDEV_TYPE_DRAID)) == 0) {
1208 		p = type + strlen(VDEV_TYPE_DRAID);
1209 
1210 		if (*p == '\0' || *p == ':') {
1211 			/* when unspecified default to single parity */
1212 			return (1);
1213 		} else if (*p == '0') {
1214 			/* no zero prefixes allowed */
1215 			return (0);
1216 		} else {
1217 			/* 0-3, allowed suffixes: '\0' or ':' */
1218 			char *end;
1219 			errno = 0;
1220 			parity = strtol(p, &end, 10);
1221 			if (errno != 0 ||
1222 			    parity < 1 || parity > VDEV_DRAID_MAXPARITY ||
1223 			    (*end != '\0' && *end != ':')) {
1224 				return (0);
1225 			}
1226 		}
1227 	}
1228 
1229 	return ((int)parity);
1230 }
1231 
1232 /*
1233  * Assign the minimum and maximum number of devices allowed for
1234  * the specified type.  On error NULL is returned, otherwise the
1235  * type prefix is returned (raidz, mirror, etc).
1236  */
1237 static const char *
1238 is_grouping(const char *type, int *mindev, int *maxdev)
1239 {
1240 	int nparity;
1241 
1242 	if (strncmp(type, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1243 	    strncmp(type, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0) {
1244 		nparity = get_parity(type);
1245 		if (nparity == 0)
1246 			return (NULL);
1247 		if (mindev != NULL)
1248 			*mindev = nparity + 1;
1249 		if (maxdev != NULL)
1250 			*maxdev = 255;
1251 
1252 		if (strncmp(type, VDEV_TYPE_RAIDZ,
1253 		    strlen(VDEV_TYPE_RAIDZ)) == 0) {
1254 			return (VDEV_TYPE_RAIDZ);
1255 		} else {
1256 			return (VDEV_TYPE_DRAID);
1257 		}
1258 	}
1259 
1260 	if (maxdev != NULL)
1261 		*maxdev = INT_MAX;
1262 
1263 	if (strcmp(type, "mirror") == 0) {
1264 		if (mindev != NULL)
1265 			*mindev = 2;
1266 		return (VDEV_TYPE_MIRROR);
1267 	}
1268 
1269 	if (strcmp(type, "spare") == 0) {
1270 		if (mindev != NULL)
1271 			*mindev = 1;
1272 		return (VDEV_TYPE_SPARE);
1273 	}
1274 
1275 	if (strcmp(type, "log") == 0) {
1276 		if (mindev != NULL)
1277 			*mindev = 1;
1278 		return (VDEV_TYPE_LOG);
1279 	}
1280 
1281 	if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0 ||
1282 	    strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1283 		if (mindev != NULL)
1284 			*mindev = 1;
1285 		return (type);
1286 	}
1287 
1288 	if (strcmp(type, "cache") == 0) {
1289 		if (mindev != NULL)
1290 			*mindev = 1;
1291 		return (VDEV_TYPE_L2CACHE);
1292 	}
1293 
1294 	return (NULL);
1295 }
1296 
1297 /*
1298  * Extract the configuration parameters encoded in the dRAID type and
1299  * use them to generate a dRAID configuration.  The expected format is:
1300  *
1301  * draid[<parity>][:<data><d|D>][:<children><c|C>][:<spares><s|S>]
1302  *
1303  * The intent is to be able to generate a good configuration when no
1304  * additional information is provided.  The only mandatory component
1305  * of the 'type' is the 'draid' prefix.  If a value is not provided
1306  * then reasonable defaults are used.  The optional components may
1307  * appear in any order but the d/s/c suffix is required.
1308  *
1309  * Valid inputs:
1310  * - data:     number of data devices per group (1-255)
1311  * - parity:   number of parity blocks per group (1-3)
1312  * - spares:   number of distributed spare (0-100)
1313  * - children: total number of devices (1-255)
1314  *
1315  * Examples:
1316  * - zpool create tank draid <devices...>
1317  * - zpool create tank draid2:8d:51c:2s <devices...>
1318  */
1319 static int
1320 draid_config_by_type(nvlist_t *nv, const char *type, uint64_t children)
1321 {
1322 	uint64_t nparity = 1;
1323 	uint64_t nspares = 0;
1324 	uint64_t ndata = UINT64_MAX;
1325 	uint64_t ngroups = 1;
1326 	long value;
1327 
1328 	if (strncmp(type, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) != 0)
1329 		return (EINVAL);
1330 
1331 	nparity = (uint64_t)get_parity(type);
1332 	if (nparity == 0 || nparity > VDEV_DRAID_MAXPARITY) {
1333 		fprintf(stderr,
1334 		    gettext("invalid dRAID parity level %llu; must be "
1335 		    "between 1 and %d\n"), (u_longlong_t)nparity,
1336 		    VDEV_DRAID_MAXPARITY);
1337 		return (EINVAL);
1338 	}
1339 
1340 	char *p = (char *)type;
1341 	while ((p = strchr(p, ':')) != NULL) {
1342 		char *end;
1343 
1344 		p = p + 1;
1345 		errno = 0;
1346 
1347 		if (!isdigit(p[0])) {
1348 			(void) fprintf(stderr, gettext("invalid dRAID "
1349 			    "syntax; expected [:<number><c|d|s>] not '%s'\n"),
1350 			    type);
1351 			return (EINVAL);
1352 		}
1353 
1354 		/* Expected non-zero value with c/d/s suffix */
1355 		value = strtol(p, &end, 10);
1356 		char suffix = tolower(*end);
1357 		if (errno != 0 ||
1358 		    (suffix != 'c' && suffix != 'd' && suffix != 's')) {
1359 			(void) fprintf(stderr, gettext("invalid dRAID "
1360 			    "syntax; expected [:<number><c|d|s>] not '%s'\n"),
1361 			    type);
1362 			return (EINVAL);
1363 		}
1364 
1365 		if (suffix == 'c') {
1366 			if ((uint64_t)value != children) {
1367 				fprintf(stderr,
1368 				    gettext("invalid number of dRAID children; "
1369 				    "%llu required but %llu provided\n"),
1370 				    (u_longlong_t)value,
1371 				    (u_longlong_t)children);
1372 				return (EINVAL);
1373 			}
1374 		} else if (suffix == 'd') {
1375 			ndata = (uint64_t)value;
1376 		} else if (suffix == 's') {
1377 			nspares = (uint64_t)value;
1378 		} else {
1379 			verify(0); /* Unreachable */
1380 		}
1381 	}
1382 
1383 	/*
1384 	 * When a specific number of data disks is not provided limit a
1385 	 * redundancy group to 8 data disks.  This value was selected to
1386 	 * provide a reasonable tradeoff between capacity and performance.
1387 	 */
1388 	if (ndata == UINT64_MAX) {
1389 		if (children > nspares + nparity) {
1390 			ndata = MIN(children - nspares - nparity, 8);
1391 		} else {
1392 			fprintf(stderr, gettext("request number of "
1393 			    "distributed spares %llu and parity level %llu\n"
1394 			    "leaves no disks available for data\n"),
1395 			    (u_longlong_t)nspares, (u_longlong_t)nparity);
1396 			return (EINVAL);
1397 		}
1398 	}
1399 
1400 	/* Verify the maximum allowed group size is never exceeded. */
1401 	if (ndata == 0 || (ndata + nparity > children - nspares)) {
1402 		fprintf(stderr, gettext("requested number of dRAID data "
1403 		    "disks per group %llu is too high,\nat most %llu disks "
1404 		    "are available for data\n"), (u_longlong_t)ndata,
1405 		    (u_longlong_t)(children - nspares - nparity));
1406 		return (EINVAL);
1407 	}
1408 
1409 	/*
1410 	 * Verify the requested number of spares can be satisfied.
1411 	 * An arbitrary limit of 100 distributed spares is applied.
1412 	 */
1413 	if (nspares > 100 || nspares > (children - (ndata + nparity))) {
1414 		fprintf(stderr,
1415 		    gettext("invalid number of dRAID spares %llu; additional "
1416 		    "disks would be required\n"), (u_longlong_t)nspares);
1417 		return (EINVAL);
1418 	}
1419 
1420 	/* Verify the requested number children is sufficient. */
1421 	if (children < (ndata + nparity + nspares)) {
1422 		fprintf(stderr, gettext("%llu disks were provided, but at "
1423 		    "least %llu disks are required for this config\n"),
1424 		    (u_longlong_t)children,
1425 		    (u_longlong_t)(ndata + nparity + nspares));
1426 	}
1427 
1428 	if (children > VDEV_DRAID_MAX_CHILDREN) {
1429 		fprintf(stderr, gettext("%llu disks were provided, but "
1430 		    "dRAID only supports up to %u disks"),
1431 		    (u_longlong_t)children, VDEV_DRAID_MAX_CHILDREN);
1432 	}
1433 
1434 	/*
1435 	 * Calculate the minimum number of groups required to fill a slice.
1436 	 * This is the LCM of the stripe width (ndata + nparity) and the
1437 	 * number of data drives (children - nspares).
1438 	 */
1439 	while (ngroups * (ndata + nparity) % (children - nspares) != 0)
1440 		ngroups++;
1441 
1442 	/* Store the basic dRAID configuration. */
1443 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, nparity);
1444 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA, ndata);
1445 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NSPARES, nspares);
1446 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NGROUPS, ngroups);
1447 
1448 	return (0);
1449 }
1450 
1451 /*
1452  * Construct a syntactically valid vdev specification,
1453  * and ensure that all devices and files exist and can be opened.
1454  * Note: we don't bother freeing anything in the error paths
1455  * because the program is just going to exit anyway.
1456  */
1457 static nvlist_t *
1458 construct_spec(nvlist_t *props, int argc, char **argv)
1459 {
1460 	nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1461 	int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
1462 	const char *type, *fulltype;
1463 	boolean_t is_log, is_special, is_dedup, is_spare;
1464 	boolean_t seen_logs;
1465 
1466 	top = NULL;
1467 	toplevels = 0;
1468 	spares = NULL;
1469 	l2cache = NULL;
1470 	nspares = 0;
1471 	nlogs = 0;
1472 	nl2cache = 0;
1473 	is_log = is_special = is_dedup = is_spare = B_FALSE;
1474 	seen_logs = B_FALSE;
1475 	nvroot = NULL;
1476 
1477 	while (argc > 0) {
1478 		fulltype = argv[0];
1479 		nv = NULL;
1480 
1481 		/*
1482 		 * If it's a mirror, raidz, or draid the subsequent arguments
1483 		 * are its leaves -- until we encounter the next mirror,
1484 		 * raidz or draid.
1485 		 */
1486 		if ((type = is_grouping(fulltype, &mindev, &maxdev)) != NULL) {
1487 			nvlist_t **child = NULL;
1488 			int c, children = 0;
1489 
1490 			if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1491 				if (spares != NULL) {
1492 					(void) fprintf(stderr,
1493 					    gettext("invalid vdev "
1494 					    "specification: 'spare' can be "
1495 					    "specified only once\n"));
1496 					goto spec_out;
1497 				}
1498 				is_spare = B_TRUE;
1499 				is_log = is_special = is_dedup = B_FALSE;
1500 			}
1501 
1502 			if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1503 				if (seen_logs) {
1504 					(void) fprintf(stderr,
1505 					    gettext("invalid vdev "
1506 					    "specification: 'log' can be "
1507 					    "specified only once\n"));
1508 					goto spec_out;
1509 				}
1510 				seen_logs = B_TRUE;
1511 				is_log = B_TRUE;
1512 				is_special = is_dedup = is_spare = B_FALSE;
1513 				argc--;
1514 				argv++;
1515 				/*
1516 				 * A log is not a real grouping device.
1517 				 * We just set is_log and continue.
1518 				 */
1519 				continue;
1520 			}
1521 
1522 			if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1523 				is_special = B_TRUE;
1524 				is_log = is_dedup = is_spare = B_FALSE;
1525 				argc--;
1526 				argv++;
1527 				continue;
1528 			}
1529 
1530 			if (strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1531 				is_dedup = B_TRUE;
1532 				is_log = is_special = is_spare = B_FALSE;
1533 				argc--;
1534 				argv++;
1535 				continue;
1536 			}
1537 
1538 			if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1539 				if (l2cache != NULL) {
1540 					(void) fprintf(stderr,
1541 					    gettext("invalid vdev "
1542 					    "specification: 'cache' can be "
1543 					    "specified only once\n"));
1544 					goto spec_out;
1545 				}
1546 				is_log = is_special = B_FALSE;
1547 				is_dedup = is_spare = B_FALSE;
1548 			}
1549 
1550 			if (is_log || is_special || is_dedup) {
1551 				if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1552 					(void) fprintf(stderr,
1553 					    gettext("invalid vdev "
1554 					    "specification: unsupported '%s' "
1555 					    "device: %s\n"), is_log ? "log" :
1556 					    "special", type);
1557 					goto spec_out;
1558 				}
1559 				nlogs++;
1560 			}
1561 
1562 			for (c = 1; c < argc; c++) {
1563 				if (is_grouping(argv[c], NULL, NULL) != NULL)
1564 					break;
1565 
1566 				children++;
1567 				child = realloc(child,
1568 				    children * sizeof (nvlist_t *));
1569 				if (child == NULL)
1570 					zpool_no_memory();
1571 				if ((nv = make_leaf_vdev(props, argv[c],
1572 				    !(is_log || is_special || is_dedup ||
1573 				    is_spare))) == NULL) {
1574 					for (c = 0; c < children - 1; c++)
1575 						nvlist_free(child[c]);
1576 					free(child);
1577 					goto spec_out;
1578 				}
1579 
1580 				child[children - 1] = nv;
1581 			}
1582 
1583 			if (children < mindev) {
1584 				(void) fprintf(stderr, gettext("invalid vdev "
1585 				    "specification: %s requires at least %d "
1586 				    "devices\n"), argv[0], mindev);
1587 				for (c = 0; c < children; c++)
1588 					nvlist_free(child[c]);
1589 				free(child);
1590 				goto spec_out;
1591 			}
1592 
1593 			if (children > maxdev) {
1594 				(void) fprintf(stderr, gettext("invalid vdev "
1595 				    "specification: %s supports no more than "
1596 				    "%d devices\n"), argv[0], maxdev);
1597 				for (c = 0; c < children; c++)
1598 					nvlist_free(child[c]);
1599 				free(child);
1600 				goto spec_out;
1601 			}
1602 
1603 			argc -= c;
1604 			argv += c;
1605 
1606 			if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1607 				spares = child;
1608 				nspares = children;
1609 				continue;
1610 			} else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1611 				l2cache = child;
1612 				nl2cache = children;
1613 				continue;
1614 			} else {
1615 				/* create a top-level vdev with children */
1616 				verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1617 				    0) == 0);
1618 				verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1619 				    type) == 0);
1620 				verify(nvlist_add_uint64(nv,
1621 				    ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1622 				if (is_log) {
1623 					verify(nvlist_add_string(nv,
1624 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1625 					    VDEV_ALLOC_BIAS_LOG) == 0);
1626 				}
1627 				if (is_special) {
1628 					verify(nvlist_add_string(nv,
1629 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1630 					    VDEV_ALLOC_BIAS_SPECIAL) == 0);
1631 				}
1632 				if (is_dedup) {
1633 					verify(nvlist_add_string(nv,
1634 					    ZPOOL_CONFIG_ALLOCATION_BIAS,
1635 					    VDEV_ALLOC_BIAS_DEDUP) == 0);
1636 				}
1637 				if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1638 					verify(nvlist_add_uint64(nv,
1639 					    ZPOOL_CONFIG_NPARITY,
1640 					    mindev - 1) == 0);
1641 				}
1642 				if (strcmp(type, VDEV_TYPE_DRAID) == 0) {
1643 					if (draid_config_by_type(nv,
1644 					    fulltype, children) != 0) {
1645 						for (c = 0; c < children; c++)
1646 							nvlist_free(child[c]);
1647 						free(child);
1648 						goto spec_out;
1649 					}
1650 				}
1651 				verify(nvlist_add_nvlist_array(nv,
1652 				    ZPOOL_CONFIG_CHILDREN,
1653 				    (const nvlist_t **)child, children) == 0);
1654 
1655 				for (c = 0; c < children; c++)
1656 					nvlist_free(child[c]);
1657 				free(child);
1658 			}
1659 		} else {
1660 			/*
1661 			 * We have a device.  Pass off to make_leaf_vdev() to
1662 			 * construct the appropriate nvlist describing the vdev.
1663 			 */
1664 			if ((nv = make_leaf_vdev(props, argv[0], !(is_log ||
1665 			    is_special || is_dedup || is_spare))) == NULL)
1666 				goto spec_out;
1667 
1668 			verify(nvlist_add_uint64(nv,
1669 			    ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1670 			if (is_log) {
1671 				verify(nvlist_add_string(nv,
1672 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
1673 				    VDEV_ALLOC_BIAS_LOG) == 0);
1674 				nlogs++;
1675 			}
1676 
1677 			if (is_special) {
1678 				verify(nvlist_add_string(nv,
1679 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
1680 				    VDEV_ALLOC_BIAS_SPECIAL) == 0);
1681 			}
1682 			if (is_dedup) {
1683 				verify(nvlist_add_string(nv,
1684 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
1685 				    VDEV_ALLOC_BIAS_DEDUP) == 0);
1686 			}
1687 			argc--;
1688 			argv++;
1689 		}
1690 
1691 		toplevels++;
1692 		top = realloc(top, toplevels * sizeof (nvlist_t *));
1693 		if (top == NULL)
1694 			zpool_no_memory();
1695 		top[toplevels - 1] = nv;
1696 	}
1697 
1698 	if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1699 		(void) fprintf(stderr, gettext("invalid vdev "
1700 		    "specification: at least one toplevel vdev must be "
1701 		    "specified\n"));
1702 		goto spec_out;
1703 	}
1704 
1705 	if (seen_logs && nlogs == 0) {
1706 		(void) fprintf(stderr, gettext("invalid vdev specification: "
1707 		    "log requires at least 1 device\n"));
1708 		goto spec_out;
1709 	}
1710 
1711 	/*
1712 	 * Finally, create nvroot and add all top-level vdevs to it.
1713 	 */
1714 	verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1715 	verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1716 	    VDEV_TYPE_ROOT) == 0);
1717 	verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1718 	    (const nvlist_t **)top, toplevels) == 0);
1719 	if (nspares != 0)
1720 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1721 		    (const nvlist_t **)spares, nspares) == 0);
1722 	if (nl2cache != 0)
1723 		verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1724 		    (const nvlist_t **)l2cache, nl2cache) == 0);
1725 
1726 spec_out:
1727 	for (t = 0; t < toplevels; t++)
1728 		nvlist_free(top[t]);
1729 	for (t = 0; t < nspares; t++)
1730 		nvlist_free(spares[t]);
1731 	for (t = 0; t < nl2cache; t++)
1732 		nvlist_free(l2cache[t]);
1733 
1734 	free(spares);
1735 	free(l2cache);
1736 	free(top);
1737 
1738 	return (nvroot);
1739 }
1740 
1741 nvlist_t *
1742 split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1743     splitflags_t flags, int argc, char **argv)
1744 {
1745 	nvlist_t *newroot = NULL, **child;
1746 	uint_t c, children;
1747 
1748 	if (argc > 0) {
1749 		if ((newroot = construct_spec(props, argc, argv)) == NULL) {
1750 			(void) fprintf(stderr, gettext("Unable to build a "
1751 			    "pool from the specified devices\n"));
1752 			return (NULL);
1753 		}
1754 
1755 		if (!flags.dryrun && make_disks(zhp, newroot) != 0) {
1756 			nvlist_free(newroot);
1757 			return (NULL);
1758 		}
1759 
1760 		/* avoid any tricks in the spec */
1761 		verify(nvlist_lookup_nvlist_array(newroot,
1762 		    ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1763 		for (c = 0; c < children; c++) {
1764 			const char *path;
1765 			const char *type;
1766 			int min, max;
1767 
1768 			verify(nvlist_lookup_string(child[c],
1769 			    ZPOOL_CONFIG_PATH, &path) == 0);
1770 			if ((type = is_grouping(path, &min, &max)) != NULL) {
1771 				(void) fprintf(stderr, gettext("Cannot use "
1772 				    "'%s' as a device for splitting\n"), type);
1773 				nvlist_free(newroot);
1774 				return (NULL);
1775 			}
1776 		}
1777 	}
1778 
1779 	if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1780 		nvlist_free(newroot);
1781 		return (NULL);
1782 	}
1783 
1784 	return (newroot);
1785 }
1786 
1787 static int
1788 num_normal_vdevs(nvlist_t *nvroot)
1789 {
1790 	nvlist_t **top;
1791 	uint_t t, toplevels, normal = 0;
1792 
1793 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1794 	    &top, &toplevels) == 0);
1795 
1796 	for (t = 0; t < toplevels; t++) {
1797 		uint64_t log = B_FALSE;
1798 
1799 		(void) nvlist_lookup_uint64(top[t], ZPOOL_CONFIG_IS_LOG, &log);
1800 		if (log)
1801 			continue;
1802 		if (nvlist_exists(top[t], ZPOOL_CONFIG_ALLOCATION_BIAS))
1803 			continue;
1804 
1805 		normal++;
1806 	}
1807 
1808 	return (normal);
1809 }
1810 
1811 /*
1812  * Get and validate the contents of the given vdev specification.  This ensures
1813  * that the nvlist returned is well-formed, that all the devices exist, and that
1814  * they are not currently in use by any other known consumer.  The 'poolconfig'
1815  * parameter is the current configuration of the pool when adding devices
1816  * existing pool, and is used to perform additional checks, such as changing the
1817  * replication level of the pool.  It can be 'NULL' to indicate that this is a
1818  * new pool.  The 'force' flag controls whether devices should be forcefully
1819  * added, even if they appear in use.
1820  */
1821 nvlist_t *
1822 make_root_vdev(zpool_handle_t *zhp, nvlist_t *props, int force, int check_rep,
1823     boolean_t replacing, boolean_t dryrun, int argc, char **argv)
1824 {
1825 	nvlist_t *newroot;
1826 	nvlist_t *poolconfig = NULL;
1827 	is_force = force;
1828 
1829 	/*
1830 	 * Construct the vdev specification.  If this is successful, we know
1831 	 * that we have a valid specification, and that all devices can be
1832 	 * opened.
1833 	 */
1834 	if ((newroot = construct_spec(props, argc, argv)) == NULL)
1835 		return (NULL);
1836 
1837 	if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL)) {
1838 		nvlist_free(newroot);
1839 		return (NULL);
1840 	}
1841 
1842 	/*
1843 	 * Validate each device to make sure that it's not shared with another
1844 	 * subsystem.  We do this even if 'force' is set, because there are some
1845 	 * uses (such as a dedicated dump device) that even '-f' cannot
1846 	 * override.
1847 	 */
1848 	if (is_device_in_use(poolconfig, newroot, force, replacing, B_FALSE)) {
1849 		nvlist_free(newroot);
1850 		return (NULL);
1851 	}
1852 
1853 	/*
1854 	 * Check the replication level of the given vdevs and report any errors
1855 	 * found.  We include the existing pool spec, if any, as we need to
1856 	 * catch changes against the existing replication level.
1857 	 */
1858 	if (check_rep && check_replication(poolconfig, newroot) != 0) {
1859 		nvlist_free(newroot);
1860 		return (NULL);
1861 	}
1862 
1863 	/*
1864 	 * On pool create the new vdev spec must have one normal vdev.
1865 	 */
1866 	if (poolconfig == NULL && num_normal_vdevs(newroot) == 0) {
1867 		vdev_error(gettext("at least one general top-level vdev must "
1868 		    "be specified\n"));
1869 		nvlist_free(newroot);
1870 		return (NULL);
1871 	}
1872 
1873 	/*
1874 	 * Run through the vdev specification and label any whole disks found.
1875 	 */
1876 	if (!dryrun && make_disks(zhp, newroot) != 0) {
1877 		nvlist_free(newroot);
1878 		return (NULL);
1879 	}
1880 
1881 	return (newroot);
1882 }
1883