Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / zpool / zpool_vdev.c
blob3872028d718ea6605bca0d1c00c3c767888386e4
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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 <ikozhukhov@gmail.com>.
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:
32 * Device vdevs
33 * disk=(path=..., devid=...)
34 * file=(path=...)
36 * Group vdevs
37 * raidz[1|2]=(...)
38 * mirror=(...)
40 * Hot spares
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.
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.
50 * The only function exported by this file is 'make_root_vdev'. The
51 * function performs several passes:
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.
64 #include <assert.h>
65 #include <devid.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <libdiskmgt.h>
69 #include <libintl.h>
70 #include <libnvpair.h>
71 #include <limits.h>
72 #include <stdio.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <sys/efi_partition.h>
76 #include <sys/stat.h>
77 #include <sys/vtoc.h>
78 #include <sys/mntent.h>
80 #include "zpool_util.h"
82 #define BACKUP_SLICE "s2"
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.
89 boolean_t error_seen;
90 boolean_t is_force;
92 /*PRINTFLIKE1*/
93 static void
94 vdev_error(const char *fmt, ...)
96 va_list ap;
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;
109 va_start(ap, fmt);
110 (void) vfprintf(stderr, fmt, ap);
111 va_end(ap);
114 static void
115 libdiskmgt_error(int error)
118 * ENXIO/ENODEV is a valid error message if the device doesn't live in
119 * /dev/dsk. Don't bother printing an error message in this case.
121 if (error == ENXIO || error == ENODEV)
122 return;
124 (void) fprintf(stderr, gettext("warning: device in use checking "
125 "failed: %s\n"), strerror(error));
129 * Validate a device, passing the bulk of the work off to libdiskmgt.
131 static int
132 check_slice(const char *path, int force, boolean_t wholedisk, boolean_t isspare)
134 char *msg;
135 int error = 0;
136 dm_who_type_t who;
138 if (force)
139 who = DM_WHO_ZPOOL_FORCE;
140 else if (isspare)
141 who = DM_WHO_ZPOOL_SPARE;
142 else
143 who = DM_WHO_ZPOOL;
145 if (dm_inuse((char *)path, &msg, who, &error) || error) {
146 if (error != 0) {
147 libdiskmgt_error(error);
148 return (0);
149 } else {
150 vdev_error("%s", msg);
151 free(msg);
152 return (-1);
157 * If we're given a whole disk, ignore overlapping slices since we're
158 * about to label it anyway.
160 error = 0;
161 if (!wholedisk && !force &&
162 (dm_isoverlapping((char *)path, &msg, &error) || error)) {
163 if (error == 0) {
164 /* dm_isoverlapping returned -1 */
165 vdev_error(gettext("%s overlaps with %s\n"), path, msg);
166 free(msg);
167 return (-1);
168 } else if (error != ENODEV) {
169 /* libdiskmgt's devcache only handles physical drives */
170 libdiskmgt_error(error);
171 return (0);
175 return (0);
180 * Validate a whole disk. Iterate over all slices on the disk and make sure
181 * that none is in use by calling check_slice().
183 static int
184 check_disk(const char *name, dm_descriptor_t disk, int force, int isspare)
186 dm_descriptor_t *drive, *media, *slice;
187 int err = 0;
188 int i;
189 int ret;
192 * Get the drive associated with this disk. This should never fail,
193 * because we already have an alias handle open for the device.
195 if ((drive = dm_get_associated_descriptors(disk, DM_DRIVE,
196 &err)) == NULL || *drive == 0) {
197 if (err)
198 libdiskmgt_error(err);
199 return (0);
202 if ((media = dm_get_associated_descriptors(*drive, DM_MEDIA,
203 &err)) == NULL) {
204 dm_free_descriptors(drive);
205 if (err)
206 libdiskmgt_error(err);
207 return (0);
210 dm_free_descriptors(drive);
213 * It is possible that the user has specified a removable media drive,
214 * and the media is not present.
216 if (*media == 0) {
217 dm_free_descriptors(media);
218 vdev_error(gettext("'%s' has no media in drive\n"), name);
219 return (-1);
222 if ((slice = dm_get_associated_descriptors(*media, DM_SLICE,
223 &err)) == NULL) {
224 dm_free_descriptors(media);
225 if (err)
226 libdiskmgt_error(err);
227 return (0);
230 dm_free_descriptors(media);
232 ret = 0;
235 * Iterate over all slices and report any errors. We don't care about
236 * overlapping slices because we are using the whole disk.
238 for (i = 0; slice[i] != (uintptr_t)NULL; i++) {
239 char *name = dm_get_name(slice[i], &err);
241 if (check_slice(name, force, B_TRUE, isspare) != 0)
242 ret = -1;
244 dm_free_name(name);
247 dm_free_descriptors(slice);
248 return (ret);
252 * Validate a device.
254 static int
255 check_device(const char *path, boolean_t force, boolean_t isspare)
257 dm_descriptor_t desc;
258 int err;
259 char *dev;
262 * For whole disks, libdiskmgt does not include the leading dev path.
264 dev = strrchr(path, '/');
265 assert(dev != NULL);
266 dev++;
267 if ((desc = dm_get_descriptor_by_name(DM_ALIAS, dev, &err))
268 != (uintptr_t)NULL) {
269 err = check_disk(path, desc, force, isspare);
270 dm_free_descriptor(desc);
271 return (err);
274 return (check_slice(path, force, B_FALSE, isspare));
278 * Check that a file is valid. All we can do in this case is check that it's
279 * not in use by another pool, and not in use by swap.
281 static int
282 check_file(const char *file, boolean_t force, boolean_t isspare)
284 char *name;
285 int fd;
286 int ret = 0;
287 int err;
288 pool_state_t state;
289 boolean_t inuse;
291 if (dm_inuse_swap(file, &err)) {
292 if (err)
293 libdiskmgt_error(err);
294 else
295 vdev_error(gettext("%s is currently used by swap. "
296 "Please see swap(8).\n"), file);
297 return (-1);
300 if ((fd = open(file, O_RDONLY)) < 0)
301 return (0);
303 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
304 const char *desc;
306 switch (state) {
307 case POOL_STATE_ACTIVE:
308 desc = gettext("active");
309 break;
311 case POOL_STATE_EXPORTED:
312 desc = gettext("exported");
313 break;
315 case POOL_STATE_POTENTIALLY_ACTIVE:
316 desc = gettext("potentially active");
317 break;
319 default:
320 desc = gettext("unknown");
321 break;
325 * Allow hot spares to be shared between pools.
327 if (state == POOL_STATE_SPARE && isspare)
328 return (0);
330 if (state == POOL_STATE_ACTIVE ||
331 state == POOL_STATE_SPARE || !force) {
332 switch (state) {
333 case POOL_STATE_SPARE:
334 vdev_error(gettext("%s is reserved as a hot "
335 "spare for pool %s\n"), file, name);
336 break;
337 default:
338 vdev_error(gettext("%s is part of %s pool "
339 "'%s'\n"), file, desc, name);
340 break;
342 ret = -1;
345 free(name);
348 (void) close(fd);
349 return (ret);
354 * By "whole disk" we mean an entire physical disk (something we can
355 * label, toggle the write cache on, etc.) as opposed to the full
356 * capacity of a pseudo-device such as lofi or did. We act as if we
357 * are labeling the disk, which should be a pretty good test of whether
358 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
359 * it isn't.
361 static boolean_t
362 is_whole_disk(const char *arg)
364 struct dk_gpt *label;
365 int fd;
366 char path[MAXPATHLEN];
368 (void) snprintf(path, sizeof (path), "%s%s%s",
369 ZFS_RDISK_ROOT, strrchr(arg, '/'), BACKUP_SLICE);
370 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0)
371 return (B_FALSE);
372 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
373 (void) close(fd);
374 return (B_FALSE);
376 efi_free(label);
377 (void) close(fd);
378 return (B_TRUE);
382 * Create a leaf vdev. Determine if this is a file or a device. If it's a
383 * device, fill in the device id to make a complete nvlist. Valid forms for a
384 * leaf vdev are:
386 * /dev/dsk/xxx Complete disk path
387 * /xxx Full path to file
388 * xxx Shorthand for /dev/dsk/xxx
390 static nvlist_t *
391 make_leaf_vdev(const char *arg, uint64_t is_log)
393 char path[MAXPATHLEN];
394 struct stat64 statbuf;
395 nvlist_t *vdev = NULL;
396 char *type = NULL;
397 boolean_t wholedisk = B_FALSE;
400 * Determine what type of vdev this is, and put the full path into
401 * 'path'. We detect whether this is a device of file afterwards by
402 * checking the st_mode of the file.
404 if (arg[0] == '/') {
406 * Complete device or file path. Exact type is determined by
407 * examining the file descriptor afterwards.
409 wholedisk = is_whole_disk(arg);
410 if (!wholedisk && (stat64(arg, &statbuf) != 0)) {
411 (void) fprintf(stderr,
412 gettext("cannot open '%s': %s\n"),
413 arg, strerror(errno));
414 return (NULL);
417 (void) strlcpy(path, arg, sizeof (path));
418 } else {
420 * This may be a short path for a device, or it could be total
421 * gibberish. Check to see if it's a known device in
422 * /dev/dsk/. As part of this check, see if we've been given a
423 * an entire disk (minus the slice number).
425 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_DISK_ROOT,
426 arg);
427 wholedisk = is_whole_disk(path);
428 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
430 * If we got ENOENT, then the user gave us
431 * gibberish, so try to direct them with a
432 * reasonable error message. Otherwise,
433 * regurgitate strerror() since it's the best we
434 * can do.
436 if (errno == ENOENT) {
437 (void) fprintf(stderr,
438 gettext("cannot open '%s': no such "
439 "device in %s\n"), arg, ZFS_DISK_ROOT);
440 (void) fprintf(stderr,
441 gettext("must be a full path or "
442 "shorthand device name\n"));
443 return (NULL);
444 } else {
445 (void) fprintf(stderr,
446 gettext("cannot open '%s': %s\n"),
447 path, strerror(errno));
448 return (NULL);
454 * Determine whether this is a device or a file.
456 if (wholedisk || S_ISBLK(statbuf.st_mode)) {
457 type = VDEV_TYPE_DISK;
458 } else if (S_ISREG(statbuf.st_mode)) {
459 type = VDEV_TYPE_FILE;
460 } else {
461 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
462 "block device or regular file\n"), path);
463 return (NULL);
467 * Finally, we have the complete device or file, and we know that it is
468 * acceptable to use. Construct the nvlist to describe this vdev. All
469 * vdevs have a 'path' element, and devices also have a 'devid' element.
471 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
472 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
473 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
474 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
475 if (strcmp(type, VDEV_TYPE_DISK) == 0)
476 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
477 (uint64_t)wholedisk) == 0);
480 * For a whole disk, defer getting its devid until after labeling it.
482 if (S_ISBLK(statbuf.st_mode) && !wholedisk) {
484 * Get the devid for the device.
486 int fd;
487 ddi_devid_t devid;
488 char *minor = NULL, *devid_str = NULL;
490 if ((fd = open(path, O_RDONLY)) < 0) {
491 (void) fprintf(stderr, gettext("cannot open '%s': "
492 "%s\n"), path, strerror(errno));
493 nvlist_free(vdev);
494 return (NULL);
497 if (devid_get(fd, &devid) == 0) {
498 if (devid_get_minor_name(fd, &minor) == 0 &&
499 (devid_str = devid_str_encode(devid, minor)) !=
500 NULL) {
501 verify(nvlist_add_string(vdev,
502 ZPOOL_CONFIG_DEVID, devid_str) == 0);
504 if (devid_str != NULL)
505 devid_str_free(devid_str);
506 if (minor != NULL)
507 devid_str_free(minor);
508 devid_free(devid);
511 (void) close(fd);
514 return (vdev);
518 * Go through and verify the replication level of the pool is consistent.
519 * Performs the following checks:
521 * For the new spec, verifies that devices in mirrors and raidz are the
522 * same size.
524 * If the current configuration already has inconsistent replication
525 * levels, ignore any other potential problems in the new spec.
527 * Otherwise, make sure that the current spec (if there is one) and the new
528 * spec have consistent replication levels.
530 typedef struct replication_level {
531 char *zprl_type;
532 uint64_t zprl_children;
533 uint64_t zprl_parity;
534 } replication_level_t;
536 #define ZPOOL_FUZZ (16 * 1024 * 1024)
539 * Given a list of toplevel vdevs, return the current replication level. If
540 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
541 * an error message will be displayed for each self-inconsistent vdev.
543 static replication_level_t *
544 get_replication(nvlist_t *nvroot, boolean_t fatal)
546 nvlist_t **top;
547 uint_t t, toplevels;
548 nvlist_t **child;
549 uint_t c, children;
550 nvlist_t *nv;
551 char *type;
552 replication_level_t lastrep = {0};
553 replication_level_t rep;
554 replication_level_t *ret;
555 boolean_t dontreport;
557 ret = safe_malloc(sizeof (replication_level_t));
559 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
560 &top, &toplevels) == 0);
562 for (t = 0; t < toplevels; t++) {
563 uint64_t is_log = B_FALSE;
565 nv = top[t];
568 * For separate logs we ignore the top level vdev replication
569 * constraints.
571 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
572 if (is_log)
573 continue;
575 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
576 &type) == 0);
577 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
578 &child, &children) != 0) {
580 * This is a 'file' or 'disk' vdev.
582 rep.zprl_type = type;
583 rep.zprl_children = 1;
584 rep.zprl_parity = 0;
585 } else {
586 uint64_t vdev_size;
589 * This is a mirror or RAID-Z vdev. Go through and make
590 * sure the contents are all the same (files vs. disks),
591 * keeping track of the number of elements in the
592 * process.
594 * We also check that the size of each vdev (if it can
595 * be determined) is the same.
597 rep.zprl_type = type;
598 rep.zprl_children = 0;
600 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
601 verify(nvlist_lookup_uint64(nv,
602 ZPOOL_CONFIG_NPARITY,
603 &rep.zprl_parity) == 0);
604 assert(rep.zprl_parity != 0);
605 } else {
606 rep.zprl_parity = 0;
610 * The 'dontreport' variable indicates that we've
611 * already reported an error for this spec, so don't
612 * bother doing it again.
614 type = NULL;
615 dontreport = 0;
616 vdev_size = -1ULL;
617 for (c = 0; c < children; c++) {
618 nvlist_t *cnv = child[c];
619 char *path;
620 struct stat64 statbuf;
621 uint64_t size = -1ULL;
622 char *childtype;
623 int fd, err;
625 rep.zprl_children++;
627 verify(nvlist_lookup_string(cnv,
628 ZPOOL_CONFIG_TYPE, &childtype) == 0);
631 * If this is a replacing or spare vdev, then
632 * get the real first child of the vdev: do this
633 * in a loop because replacing and spare vdevs
634 * can be nested.
636 while (strcmp(childtype,
637 VDEV_TYPE_REPLACING) == 0 ||
638 strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
639 nvlist_t **rchild;
640 uint_t rchildren;
642 verify(nvlist_lookup_nvlist_array(cnv,
643 ZPOOL_CONFIG_CHILDREN, &rchild,
644 &rchildren) == 0);
645 assert(rchildren == 2);
646 cnv = rchild[0];
648 verify(nvlist_lookup_string(cnv,
649 ZPOOL_CONFIG_TYPE,
650 &childtype) == 0);
653 verify(nvlist_lookup_string(cnv,
654 ZPOOL_CONFIG_PATH, &path) == 0);
657 * If we have a raidz/mirror that combines disks
658 * with files, report it as an error.
660 if (!dontreport && type != NULL &&
661 strcmp(type, childtype) != 0) {
662 free(ret);
663 ret = NULL;
664 if (fatal)
665 vdev_error(gettext(
666 "mismatched replication "
667 "level: %s contains both "
668 "files and devices\n"),
669 rep.zprl_type);
670 else
671 return (NULL);
672 dontreport = B_TRUE;
676 * According to stat(2), the value of 'st_size'
677 * is undefined for block devices and character
678 * devices. But there is no effective way to
679 * determine the real size in userland.
681 * Instead, we'll take advantage of an
682 * implementation detail of spec_size(). If the
683 * device is currently open, then we (should)
684 * return a valid size.
686 * If we still don't get a valid size (indicated
687 * by a size of 0 or MAXOFFSET_T), then ignore
688 * this device altogether.
690 if ((fd = open(path, O_RDONLY)) >= 0) {
691 err = fstat64(fd, &statbuf);
692 (void) close(fd);
693 } else {
694 err = stat64(path, &statbuf);
697 if (err != 0 ||
698 statbuf.st_size == 0 ||
699 statbuf.st_size == MAXOFFSET_T)
700 continue;
702 size = statbuf.st_size;
705 * Also make sure that devices and
706 * slices have a consistent size. If
707 * they differ by a significant amount
708 * (~16MB) then report an error.
710 if (!dontreport &&
711 (vdev_size != -1ULL &&
712 (labs(size - vdev_size) >
713 ZPOOL_FUZZ))) {
714 free(ret);
715 ret = NULL;
716 if (fatal)
717 vdev_error(gettext(
718 "%s contains devices of "
719 "different sizes\n"),
720 rep.zprl_type);
721 else
722 return (NULL);
723 dontreport = B_TRUE;
726 type = childtype;
727 vdev_size = size;
732 * At this point, we have the replication of the last toplevel
733 * vdev in 'rep'. Compare it to 'lastrep' to see if its
734 * different.
736 if (lastrep.zprl_type != NULL) {
737 if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
738 free(ret);
739 ret = NULL;
740 if (fatal)
741 vdev_error(gettext(
742 "mismatched replication level: "
743 "both %s and %s vdevs are "
744 "present\n"),
745 lastrep.zprl_type, rep.zprl_type);
746 else
747 return (NULL);
748 } else if (lastrep.zprl_parity != rep.zprl_parity) {
749 free(ret);
750 ret = NULL;
751 if (fatal)
752 vdev_error(gettext(
753 "mismatched replication level: "
754 "both %llu and %llu device parity "
755 "%s vdevs are present\n"),
756 lastrep.zprl_parity,
757 rep.zprl_parity,
758 rep.zprl_type);
759 else
760 return (NULL);
761 } else if (lastrep.zprl_children != rep.zprl_children) {
762 free(ret);
763 ret = NULL;
764 if (fatal)
765 vdev_error(gettext(
766 "mismatched replication level: "
767 "both %llu-way and %llu-way %s "
768 "vdevs are present\n"),
769 lastrep.zprl_children,
770 rep.zprl_children,
771 rep.zprl_type);
772 else
773 return (NULL);
776 lastrep = rep;
779 if (ret != NULL)
780 *ret = rep;
782 return (ret);
786 * Check the replication level of the vdev spec against the current pool. Calls
787 * get_replication() to make sure the new spec is self-consistent. If the pool
788 * has a consistent replication level, then we ignore any errors. Otherwise,
789 * report any difference between the two.
791 static int
792 check_replication(nvlist_t *config, nvlist_t *newroot)
794 nvlist_t **child;
795 uint_t children;
796 replication_level_t *current = NULL, *new;
797 int ret;
800 * If we have a current pool configuration, check to see if it's
801 * self-consistent. If not, simply return success.
803 if (config != NULL) {
804 nvlist_t *nvroot;
806 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
807 &nvroot) == 0);
808 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
809 return (0);
812 * for spares there may be no children, and therefore no
813 * replication level to check
815 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
816 &child, &children) != 0) || (children == 0)) {
817 free(current);
818 return (0);
822 * If all we have is logs then there's no replication level to check.
824 if (num_logs(newroot) == children) {
825 free(current);
826 return (0);
830 * Get the replication level of the new vdev spec, reporting any
831 * inconsistencies found.
833 if ((new = get_replication(newroot, B_TRUE)) == NULL) {
834 free(current);
835 return (-1);
839 * Check to see if the new vdev spec matches the replication level of
840 * the current pool.
842 ret = 0;
843 if (current != NULL) {
844 if (strcmp(current->zprl_type, new->zprl_type) != 0) {
845 vdev_error(gettext(
846 "mismatched replication level: pool uses %s "
847 "and new vdev is %s\n"),
848 current->zprl_type, new->zprl_type);
849 ret = -1;
850 } else if (current->zprl_parity != new->zprl_parity) {
851 vdev_error(gettext(
852 "mismatched replication level: pool uses %llu "
853 "device parity and new vdev uses %llu\n"),
854 current->zprl_parity, new->zprl_parity);
855 ret = -1;
856 } else if (current->zprl_children != new->zprl_children) {
857 vdev_error(gettext(
858 "mismatched replication level: pool uses %llu-way "
859 "%s and new vdev uses %llu-way %s\n"),
860 current->zprl_children, current->zprl_type,
861 new->zprl_children, new->zprl_type);
862 ret = -1;
866 free(new);
867 free(current);
869 return (ret);
873 * Go through and find any whole disks in the vdev specification, labelling them
874 * as appropriate. When constructing the vdev spec, we were unable to open this
875 * device in order to provide a devid. Now that we have labelled the disk and
876 * know the pool slice is valid, we can construct the devid now.
878 * If the disk was already labeled with an EFI label, we will have gotten the
879 * devid already (because we were able to open the whole disk). Otherwise, we
880 * need to get the devid after we label the disk.
882 static int
883 make_disks(zpool_handle_t *zhp, nvlist_t *nv, zpool_boot_label_t boot_type,
884 uint64_t boot_size)
886 nvlist_t **child;
887 uint_t c, children;
888 char *type, *path, *diskname;
889 char buf[MAXPATHLEN];
890 uint64_t wholedisk;
891 int fd;
892 int ret;
893 int slice;
894 ddi_devid_t devid;
895 char *minor = NULL, *devid_str = NULL;
897 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
899 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
900 &child, &children) != 0) {
902 if (strcmp(type, VDEV_TYPE_DISK) != 0)
903 return (0);
906 * We have a disk device. Get the path to the device
907 * and see if it's a whole disk by appending the backup
908 * slice and stat()ing the device.
910 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
912 diskname = strrchr(path, '/');
913 assert(diskname != NULL);
914 diskname++;
916 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
917 &wholedisk) != 0 || !wholedisk) {
919 * This is not whole disk, return error if
920 * boot partition creation was requested
922 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
923 (void) fprintf(stderr,
924 gettext("creating boot partition is only "
925 "supported on whole disk vdevs: %s\n"),
926 diskname);
927 return (-1);
929 return (0);
932 ret = zpool_label_disk(g_zfs, zhp, diskname, boot_type,
933 boot_size, &slice);
934 if (ret == -1)
935 return (ret);
938 * Fill in the devid, now that we've labeled the disk.
940 (void) snprintf(buf, sizeof (buf), "%ss%d", path, slice);
941 if ((fd = open(buf, O_RDONLY)) < 0) {
942 (void) fprintf(stderr,
943 gettext("cannot open '%s': %s\n"),
944 buf, strerror(errno));
945 return (-1);
948 if (devid_get(fd, &devid) == 0) {
949 if (devid_get_minor_name(fd, &minor) == 0 &&
950 (devid_str = devid_str_encode(devid, minor)) !=
951 NULL) {
952 verify(nvlist_add_string(nv,
953 ZPOOL_CONFIG_DEVID, devid_str) == 0);
955 if (devid_str != NULL)
956 devid_str_free(devid_str);
957 if (minor != NULL)
958 devid_str_free(minor);
959 devid_free(devid);
963 * Update the path to refer to the pool slice. The presence of
964 * the 'whole_disk' field indicates to the CLI that we should
965 * chop off the slice number when displaying the device in
966 * future output.
968 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0);
970 (void) close(fd);
972 return (0);
975 /* illumos kernel does not support booting from multi-vdev pools. */
976 if ((boot_type == ZPOOL_CREATE_BOOT_LABEL)) {
977 if ((strcmp(type, VDEV_TYPE_ROOT) == 0) && children > 1) {
978 (void) fprintf(stderr, gettext("boot pool "
979 "can not have more than one vdev\n"));
980 return (-1);
984 for (c = 0; c < children; c++) {
985 ret = make_disks(zhp, child[c], boot_type, boot_size);
986 if (ret != 0)
987 return (ret);
990 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
991 &child, &children) == 0)
992 for (c = 0; c < children; c++) {
993 ret = make_disks(zhp, child[c], boot_type, boot_size);
994 if (ret != 0)
995 return (ret);
998 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
999 &child, &children) == 0)
1000 for (c = 0; c < children; c++) {
1001 ret = make_disks(zhp, child[c], boot_type, boot_size);
1002 if (ret != 0)
1003 return (ret);
1006 return (0);
1010 * Determine if the given path is a hot spare within the given configuration.
1012 static boolean_t
1013 is_spare(nvlist_t *config, const char *path)
1015 int fd;
1016 pool_state_t state;
1017 char *name = NULL;
1018 nvlist_t *label;
1019 uint64_t guid, spareguid;
1020 nvlist_t *nvroot;
1021 nvlist_t **spares;
1022 uint_t i, nspares;
1023 boolean_t inuse;
1025 if ((fd = open(path, O_RDONLY)) < 0)
1026 return (B_FALSE);
1028 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
1029 !inuse ||
1030 state != POOL_STATE_SPARE ||
1031 zpool_read_label(fd, &label) != 0) {
1032 free(name);
1033 (void) close(fd);
1034 return (B_FALSE);
1036 free(name);
1037 (void) close(fd);
1039 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
1040 nvlist_free(label);
1042 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1043 &nvroot) == 0);
1044 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1045 &spares, &nspares) == 0) {
1046 for (i = 0; i < nspares; i++) {
1047 verify(nvlist_lookup_uint64(spares[i],
1048 ZPOOL_CONFIG_GUID, &spareguid) == 0);
1049 if (spareguid == guid)
1050 return (B_TRUE);
1054 return (B_FALSE);
1058 * Go through and find any devices that are in use. We rely on libdiskmgt for
1059 * the majority of this task.
1061 static boolean_t
1062 is_device_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1063 boolean_t replacing, boolean_t isspare)
1065 nvlist_t **child;
1066 uint_t c, children;
1067 char *type, *path;
1068 int ret = 0;
1069 char buf[MAXPATHLEN];
1070 uint64_t wholedisk;
1071 boolean_t anyinuse = B_FALSE;
1073 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1075 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1076 &child, &children) != 0) {
1078 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1081 * As a generic check, we look to see if this is a replace of a
1082 * hot spare within the same pool. If so, we allow it
1083 * regardless of what libdiskmgt or zpool_in_use() says.
1085 if (replacing) {
1086 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1087 &wholedisk) == 0 && wholedisk)
1088 (void) snprintf(buf, sizeof (buf), "%ss0",
1089 path);
1090 else
1091 (void) strlcpy(buf, path, sizeof (buf));
1093 if (is_spare(config, buf))
1094 return (B_FALSE);
1097 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1098 ret = check_device(path, force, isspare);
1099 else if (strcmp(type, VDEV_TYPE_FILE) == 0)
1100 ret = check_file(path, force, isspare);
1102 return (ret != 0);
1105 for (c = 0; c < children; c++)
1106 if (is_device_in_use(config, child[c], force, replacing,
1107 B_FALSE))
1108 anyinuse = B_TRUE;
1110 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1111 &child, &children) == 0)
1112 for (c = 0; c < children; c++)
1113 if (is_device_in_use(config, child[c], force, replacing,
1114 B_TRUE))
1115 anyinuse = B_TRUE;
1117 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1118 &child, &children) == 0)
1119 for (c = 0; c < children; c++)
1120 if (is_device_in_use(config, child[c], force, replacing,
1121 B_FALSE))
1122 anyinuse = B_TRUE;
1124 return (anyinuse);
1127 static const char *
1128 is_grouping(const char *type, int *mindev, int *maxdev)
1130 if (strncmp(type, "raidz", 5) == 0) {
1131 const char *p = type + 5;
1132 char *end;
1133 long nparity;
1135 if (*p == '\0') {
1136 nparity = 1;
1137 } else if (*p == '0') {
1138 return (NULL); /* no zero prefixes allowed */
1139 } else {
1140 errno = 0;
1141 nparity = strtol(p, &end, 10);
1142 if (errno != 0 || nparity < 1 || nparity >= 255 ||
1143 *end != '\0')
1144 return (NULL);
1147 if (mindev != NULL)
1148 *mindev = nparity + 1;
1149 if (maxdev != NULL)
1150 *maxdev = 255;
1151 return (VDEV_TYPE_RAIDZ);
1154 if (maxdev != NULL)
1155 *maxdev = INT_MAX;
1157 if (strcmp(type, "mirror") == 0) {
1158 if (mindev != NULL)
1159 *mindev = 2;
1160 return (VDEV_TYPE_MIRROR);
1163 if (strcmp(type, "spare") == 0) {
1164 if (mindev != NULL)
1165 *mindev = 1;
1166 return (VDEV_TYPE_SPARE);
1169 if (strcmp(type, "log") == 0) {
1170 if (mindev != NULL)
1171 *mindev = 1;
1172 return (VDEV_TYPE_LOG);
1175 if (strcmp(type, "cache") == 0) {
1176 if (mindev != NULL)
1177 *mindev = 1;
1178 return (VDEV_TYPE_L2CACHE);
1181 return (NULL);
1185 * Construct a syntactically valid vdev specification,
1186 * and ensure that all devices and files exist and can be opened.
1187 * Note: we don't bother freeing anything in the error paths
1188 * because the program is just going to exit anyway.
1190 nvlist_t *
1191 construct_spec(int argc, char **argv)
1193 nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1194 int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
1195 const char *type;
1196 uint64_t is_log;
1197 boolean_t seen_logs;
1199 top = NULL;
1200 toplevels = 0;
1201 spares = NULL;
1202 l2cache = NULL;
1203 nspares = 0;
1204 nlogs = 0;
1205 nl2cache = 0;
1206 is_log = B_FALSE;
1207 seen_logs = B_FALSE;
1209 while (argc > 0) {
1210 nv = NULL;
1213 * If it's a mirror or raidz, the subsequent arguments are
1214 * its leaves -- until we encounter the next mirror or raidz.
1216 if ((type = is_grouping(argv[0], &mindev, &maxdev)) != NULL) {
1217 nvlist_t **child = NULL;
1218 int c, children = 0;
1220 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1221 if (spares != NULL) {
1222 (void) fprintf(stderr,
1223 gettext("invalid vdev "
1224 "specification: 'spare' can be "
1225 "specified only once\n"));
1226 return (NULL);
1228 is_log = B_FALSE;
1231 if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1232 if (seen_logs) {
1233 (void) fprintf(stderr,
1234 gettext("invalid vdev "
1235 "specification: 'log' can be "
1236 "specified only once\n"));
1237 return (NULL);
1239 seen_logs = B_TRUE;
1240 is_log = B_TRUE;
1241 argc--;
1242 argv++;
1244 * A log is not a real grouping device.
1245 * We just set is_log and continue.
1247 continue;
1250 if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1251 if (l2cache != NULL) {
1252 (void) fprintf(stderr,
1253 gettext("invalid vdev "
1254 "specification: 'cache' can be "
1255 "specified only once\n"));
1256 return (NULL);
1258 is_log = B_FALSE;
1261 if (is_log) {
1262 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1263 (void) fprintf(stderr,
1264 gettext("invalid vdev "
1265 "specification: unsupported 'log' "
1266 "device: %s\n"), type);
1267 return (NULL);
1269 nlogs++;
1272 for (c = 1; c < argc; c++) {
1273 if (is_grouping(argv[c], NULL, NULL) != NULL)
1274 break;
1275 children++;
1276 child = reallocarray(child, children,
1277 sizeof (nvlist_t *));
1278 if (child == NULL)
1279 zpool_no_memory();
1280 if ((nv = make_leaf_vdev(argv[c], B_FALSE))
1281 == NULL)
1282 return (NULL);
1283 child[children - 1] = nv;
1286 if (children < mindev) {
1287 (void) fprintf(stderr, gettext("invalid vdev "
1288 "specification: %s requires at least %d "
1289 "devices\n"), argv[0], mindev);
1290 return (NULL);
1293 if (children > maxdev) {
1294 (void) fprintf(stderr, gettext("invalid vdev "
1295 "specification: %s supports no more than "
1296 "%d devices\n"), argv[0], maxdev);
1297 return (NULL);
1300 argc -= c;
1301 argv += c;
1303 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1304 spares = child;
1305 nspares = children;
1306 continue;
1307 } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1308 l2cache = child;
1309 nl2cache = children;
1310 continue;
1311 } else {
1312 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1313 0) == 0);
1314 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1315 type) == 0);
1316 verify(nvlist_add_uint64(nv,
1317 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1318 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1319 verify(nvlist_add_uint64(nv,
1320 ZPOOL_CONFIG_NPARITY,
1321 mindev - 1) == 0);
1323 verify(nvlist_add_nvlist_array(nv,
1324 ZPOOL_CONFIG_CHILDREN, child,
1325 children) == 0);
1327 for (c = 0; c < children; c++)
1328 nvlist_free(child[c]);
1329 free(child);
1331 } else {
1333 * We have a device. Pass off to make_leaf_vdev() to
1334 * construct the appropriate nvlist describing the vdev.
1336 if ((nv = make_leaf_vdev(argv[0], is_log)) == NULL)
1337 return (NULL);
1338 if (is_log)
1339 nlogs++;
1340 argc--;
1341 argv++;
1344 toplevels++;
1345 top = reallocarray(top, toplevels, sizeof (nvlist_t *));
1346 if (top == NULL)
1347 zpool_no_memory();
1348 top[toplevels - 1] = nv;
1351 if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1352 (void) fprintf(stderr, gettext("invalid vdev "
1353 "specification: at least one toplevel vdev must be "
1354 "specified\n"));
1355 return (NULL);
1358 if (seen_logs && nlogs == 0) {
1359 (void) fprintf(stderr, gettext("invalid vdev specification: "
1360 "log requires at least 1 device\n"));
1361 return (NULL);
1365 * Finally, create nvroot and add all top-level vdevs to it.
1367 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1368 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1369 VDEV_TYPE_ROOT) == 0);
1370 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1371 top, toplevels) == 0);
1372 if (nspares != 0)
1373 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1374 spares, nspares) == 0);
1375 if (nl2cache != 0)
1376 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1377 l2cache, nl2cache) == 0);
1379 for (t = 0; t < toplevels; t++)
1380 nvlist_free(top[t]);
1381 for (t = 0; t < nspares; t++)
1382 nvlist_free(spares[t]);
1383 for (t = 0; t < nl2cache; t++)
1384 nvlist_free(l2cache[t]);
1385 free(spares);
1386 free(l2cache);
1387 free(top);
1389 return (nvroot);
1392 nvlist_t *
1393 split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1394 splitflags_t flags, int argc, char **argv)
1396 nvlist_t *newroot = NULL, **child;
1397 uint_t c, children;
1398 zpool_boot_label_t boot_type;
1400 if (argc > 0) {
1401 if ((newroot = construct_spec(argc, argv)) == NULL) {
1402 (void) fprintf(stderr, gettext("Unable to build a "
1403 "pool from the specified devices\n"));
1404 return (NULL);
1407 if (zpool_is_bootable(zhp))
1408 boot_type = ZPOOL_COPY_BOOT_LABEL;
1409 else
1410 boot_type = ZPOOL_NO_BOOT_LABEL;
1412 if (!flags.dryrun &&
1413 make_disks(zhp, newroot, boot_type, 0) != 0) {
1414 nvlist_free(newroot);
1415 return (NULL);
1418 /* avoid any tricks in the spec */
1419 verify(nvlist_lookup_nvlist_array(newroot,
1420 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1421 for (c = 0; c < children; c++) {
1422 char *path;
1423 const char *type;
1424 int min, max;
1426 verify(nvlist_lookup_string(child[c],
1427 ZPOOL_CONFIG_PATH, &path) == 0);
1428 if ((type = is_grouping(path, &min, &max)) != NULL) {
1429 (void) fprintf(stderr, gettext("Cannot use "
1430 "'%s' as a device for splitting\n"), type);
1431 nvlist_free(newroot);
1432 return (NULL);
1437 if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1438 nvlist_free(newroot);
1439 return (NULL);
1442 return (newroot);
1446 * Get and validate the contents of the given vdev specification. This ensures
1447 * that the nvlist returned is well-formed, that all the devices exist, and that
1448 * they are not currently in use by any other known consumer. The 'poolconfig'
1449 * parameter is the current configuration of the pool when adding devices
1450 * existing pool, and is used to perform additional checks, such as changing the
1451 * replication level of the pool. It can be 'NULL' to indicate that this is a
1452 * new pool. The 'force' flag controls whether devices should be forcefully
1453 * added, even if they appear in use.
1455 nvlist_t *
1456 make_root_vdev(zpool_handle_t *zhp, int force, int check_rep,
1457 boolean_t replacing, boolean_t dryrun, zpool_boot_label_t boot_type,
1458 uint64_t boot_size, int argc, char **argv)
1460 nvlist_t *newroot;
1461 nvlist_t *poolconfig = NULL;
1462 is_force = force;
1465 * Construct the vdev specification. If this is successful, we know
1466 * that we have a valid specification, and that all devices can be
1467 * opened.
1469 if ((newroot = construct_spec(argc, argv)) == NULL)
1470 return (NULL);
1472 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
1473 return (NULL);
1476 * Validate each device to make sure that its not shared with another
1477 * subsystem. We do this even if 'force' is set, because there are some
1478 * uses (such as a dedicated dump device) that even '-f' cannot
1479 * override.
1481 if (is_device_in_use(poolconfig, newroot, force, replacing, B_FALSE)) {
1482 nvlist_free(newroot);
1483 return (NULL);
1487 * Check the replication level of the given vdevs and report any errors
1488 * found. We include the existing pool spec, if any, as we need to
1489 * catch changes against the existing replication level.
1491 if (check_rep && check_replication(poolconfig, newroot) != 0) {
1492 nvlist_free(newroot);
1493 return (NULL);
1497 * Run through the vdev specification and label any whole disks found.
1499 if (!dryrun && make_disks(zhp, newroot, boot_type, boot_size) != 0) {
1500 nvlist_free(newroot);
1501 return (NULL);
1504 return (newroot);