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