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