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