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