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