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