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