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