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