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