Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / cddl / osnet / dist / cmd / zpool / zpool_vdev.c
blob092744cf0a3a5bfa0c46074092d78d45a31a4a8d
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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Functions to convert between a list of vdevs and an nvlist representing the
29 * configuration. Each entry in the list can be one of:
31 * Device vdevs
32 * disk=(path=..., devid=...)
33 * file=(path=...)
35 * Group vdevs
36 * raidz[1|2]=(...)
37 * mirror=(...)
39 * Hot spares
41 * While the underlying implementation supports it, group vdevs cannot contain
42 * other group vdevs. All userland verification of devices is contained within
43 * this file. If successful, the nvlist returned can be passed directly to the
44 * kernel; we've done as much verification as possible in userland.
46 * Hot spares are a special case, and passed down as an array of disk vdevs, at
47 * the same level as the root of the vdev tree.
49 * The only function exported by this file is 'make_root_vdev'. The
50 * function performs several passes:
52 * 1. Construct the vdev specification. Performs syntax validation and
53 * makes sure each device is valid.
54 * 2. Check for devices in use. Using libdiskmgt, makes sure that no
55 * devices are also in use. Some can be overridden using the 'force'
56 * flag, others cannot.
57 * 3. Check for replication errors if the 'force' flag is not specified.
58 * validates that the replication level is consistent across the
59 * entire pool.
60 * 4. Call libzfs to label any whole disks with an EFI label.
63 #include <assert.h>
64 #include <devid.h>
65 #include <errno.h>
66 #include <fcntl.h>
67 #include <libintl.h>
68 #include <libnvpair.h>
69 #include <stdio.h>
70 #include <string.h>
71 #include <unistd.h>
72 #include <sys/efi_partition.h>
73 #include <sys/stat.h>
74 #include <sys/vtoc.h>
75 #include <sys/mntent.h>
77 #include "zpool_util.h"
79 #define DISK_ROOT "/dev/dsk"
80 #define RDISK_ROOT "/dev/rdsk"
81 #define BACKUP_SLICE "s2"
84 * For any given vdev specification, we can have multiple errors. The
85 * vdev_error() function keeps track of whether we have seen an error yet, and
86 * prints out a header if its the first error we've seen.
88 boolean_t error_seen;
89 boolean_t is_force;
91 /*PRINTFLIKE1*/
92 static void
93 vdev_error(const char *fmt, ...)
95 va_list ap;
97 if (!error_seen) {
98 (void) fprintf(stderr, gettext("invalid vdev specification\n"));
99 if (!is_force)
100 (void) fprintf(stderr, gettext("use '-f' to override "
101 "the following errors:\n"));
102 else
103 (void) fprintf(stderr, gettext("the following errors "
104 "must be manually repaired:\n"));
105 error_seen = B_TRUE;
108 va_start(ap, fmt);
109 (void) vfprintf(stderr, fmt, ap);
110 va_end(ap);
113 static void
114 libdiskmgt_error(int error)
117 * ENXIO/ENODEV is a valid error message if the device doesn't live in
118 * /dev/dsk. Don't bother printing an error message in this case.
120 if (error == ENXIO || error == ENODEV)
121 return;
123 (void) fprintf(stderr, gettext("warning: device in use checking "
124 "failed: %s\n"), strerror(error));
128 * Check that a file is valid. All we can do in this case is check that it's
129 * not in use by another pool, and not in use by swap.
131 static int
132 check_file(const char *file, boolean_t force, boolean_t isspare)
134 char *name;
135 int fd;
136 int ret = 0;
137 int err;
138 pool_state_t state;
139 boolean_t inuse;
141 #ifndef __NetBSD__
142 if (dm_inuse_swap(file, &err)) {
143 if (err)
144 libdiskmgt_error(err);
145 else
146 vdev_error(gettext("%s is currently used by swap. "
147 "Please see swap(1M).\n"), file);
148 return (-1);
150 #endif
152 if ((fd = open(file, O_RDONLY)) < 0)
153 return (0);
155 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
156 const char *desc;
158 switch (state) {
159 case POOL_STATE_ACTIVE:
160 desc = gettext("active");
161 break;
163 case POOL_STATE_EXPORTED:
164 desc = gettext("exported");
165 break;
167 case POOL_STATE_POTENTIALLY_ACTIVE:
168 desc = gettext("potentially active");
169 break;
171 default:
172 desc = gettext("unknown");
173 break;
177 * Allow hot spares to be shared between pools.
179 if (state == POOL_STATE_SPARE && isspare)
180 return (0);
182 if (state == POOL_STATE_ACTIVE ||
183 state == POOL_STATE_SPARE || !force) {
184 switch (state) {
185 case POOL_STATE_SPARE:
186 vdev_error(gettext("%s is reserved as a hot "
187 "spare for pool %s\n"), file, name);
188 break;
189 default:
190 vdev_error(gettext("%s is part of %s pool "
191 "'%s'\n"), file, desc, name);
192 break;
194 ret = -1;
197 free(name);
200 (void) close(fd);
201 return (ret);
206 * By "whole disk" we mean an entire physical disk (something we can
207 * label, toggle the write cache on, etc.) as opposed to the full
208 * capacity of a pseudo-device such as lofi or did. We act as if we
209 * are labeling the disk, which should be a pretty good test of whether
210 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
211 * it isn't.
213 static boolean_t
214 is_whole_disk(const char *arg)
216 struct dk_gpt *label;
217 int fd;
218 char path[MAXPATHLEN];
220 (void) snprintf(path, sizeof (path), "%s%s%s",
221 RDISK_ROOT, strrchr(arg, '/'), BACKUP_SLICE);
222 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0)
223 return (B_FALSE);
224 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
225 (void) close(fd);
226 return (B_FALSE);
228 efi_free(label);
229 (void) close(fd);
230 return (B_TRUE);
234 * Create a leaf vdev. Determine if this is a file or a device. If it's a
235 * device, fill in the device id to make a complete nvlist. Valid forms for a
236 * leaf vdev are:
238 * /dev/dsk/xxx Complete disk path
239 * /xxx Full path to file
240 * xxx Shorthand for /dev/dsk/xxx
242 static nvlist_t *
243 make_leaf_vdev(const char *arg, uint64_t is_log)
245 char path[MAXPATHLEN];
246 struct stat64 statbuf;
247 nvlist_t *vdev = NULL;
248 char *type = NULL;
249 boolean_t wholedisk = B_FALSE;
252 * Determine what type of vdev this is, and put the full path into
253 * 'path'. We detect whether this is a device of file afterwards by
254 * checking the st_mode of the file.
256 if (arg[0] == '/') {
258 * Complete device or file path. Exact type is determined by
259 * examining the file descriptor afterwards.
261 wholedisk = is_whole_disk(arg);
262 if (!wholedisk && (stat64(arg, &statbuf) != 0)) {
263 (void) fprintf(stderr,
264 gettext("cannot open '%s': %s\n"),
265 arg, strerror(errno));
266 return (NULL);
269 (void) strlcpy(path, arg, sizeof (path));
270 } else {
272 * This may be a short path for a device, or it could be total
273 * gibberish. Check to see if it's a known device in
274 * /dev/dsk/. As part of this check, see if we've been given a
275 * an entire disk (minus the slice number).
277 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT,
278 arg);
279 wholedisk = is_whole_disk(path);
280 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
282 * If we got ENOENT, then the user gave us
283 * gibberish, so try to direct them with a
284 * reasonable error message. Otherwise,
285 * regurgitate strerror() since it's the best we
286 * can do.
288 if (errno == ENOENT) {
289 (void) fprintf(stderr,
290 gettext("cannot open '%s': no such "
291 "device in %s\n"), arg, DISK_ROOT);
292 (void) fprintf(stderr,
293 gettext("must be a full path or "
294 "shorthand device name\n"));
295 return (NULL);
296 } else {
297 (void) fprintf(stderr,
298 gettext("cannot open '%s': %s\n"),
299 path, strerror(errno));
300 return (NULL);
306 * Determine whether this is a device or a file.
308 if (wholedisk || S_ISBLK(statbuf.st_mode)) {
309 type = VDEV_TYPE_DISK;
310 } else if (S_ISREG(statbuf.st_mode)) {
311 type = VDEV_TYPE_FILE;
312 } else {
313 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
314 "block device or regular file\n"), path);
315 return (NULL);
319 * Finally, we have the complete device or file, and we know that it is
320 * acceptable to use. Construct the nvlist to describe this vdev. All
321 * vdevs have a 'path' element, and devices also have a 'devid' element.
323 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
324 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
325 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
326 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
327 if (strcmp(type, VDEV_TYPE_DISK) == 0)
328 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
329 (uint64_t)wholedisk) == 0);
332 * For a whole disk, defer getting its devid until after labeling it.
334 if (S_ISBLK(statbuf.st_mode) && !wholedisk) {
336 * Get the devid for the device.
338 int fd;
339 ddi_devid_t devid;
340 char *minor = NULL, *devid_str = NULL;
342 if ((fd = open(path, O_RDONLY)) < 0) {
343 (void) fprintf(stderr, gettext("cannot open '%s': "
344 "%s\n"), path, strerror(errno));
345 nvlist_free(vdev);
346 return (NULL);
349 if (devid_get(fd, &devid) == 0) {
350 if (devid_get_minor_name(fd, &minor) == 0 &&
351 (devid_str = devid_str_encode(devid, minor)) !=
352 NULL) {
353 verify(nvlist_add_string(vdev,
354 ZPOOL_CONFIG_DEVID, devid_str) == 0);
356 if (devid_str != NULL)
357 devid_str_free(devid_str);
358 if (minor != NULL)
359 devid_str_free(minor);
360 devid_free(devid);
363 (void) close(fd);
366 return (vdev);
370 * Go through and verify the replication level of the pool is consistent.
371 * Performs the following checks:
373 * For the new spec, verifies that devices in mirrors and raidz are the
374 * same size.
376 * If the current configuration already has inconsistent replication
377 * levels, ignore any other potential problems in the new spec.
379 * Otherwise, make sure that the current spec (if there is one) and the new
380 * spec have consistent replication levels.
382 typedef struct replication_level {
383 char *zprl_type;
384 uint64_t zprl_children;
385 uint64_t zprl_parity;
386 } replication_level_t;
388 #define ZPOOL_FUZZ (16 * 1024 * 1024)
391 * Given a list of toplevel vdevs, return the current replication level. If
392 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
393 * an error message will be displayed for each self-inconsistent vdev.
395 static replication_level_t *
396 get_replication(nvlist_t *nvroot, boolean_t fatal)
398 nvlist_t **top;
399 uint_t t, toplevels;
400 nvlist_t **child;
401 uint_t c, children;
402 nvlist_t *nv;
403 char *type;
404 replication_level_t lastrep, rep, *ret;
405 boolean_t dontreport;
407 ret = safe_malloc(sizeof (replication_level_t));
409 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
410 &top, &toplevels) == 0);
412 lastrep.zprl_type = NULL;
413 for (t = 0; t < toplevels; t++) {
414 uint64_t is_log = B_FALSE;
416 nv = top[t];
419 * For separate logs we ignore the top level vdev replication
420 * constraints.
422 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
423 if (is_log)
424 continue;
426 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
427 &type) == 0);
428 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
429 &child, &children) != 0) {
431 * This is a 'file' or 'disk' vdev.
433 rep.zprl_type = type;
434 rep.zprl_children = 1;
435 rep.zprl_parity = 0;
436 } else {
437 uint64_t vdev_size;
440 * This is a mirror or RAID-Z vdev. Go through and make
441 * sure the contents are all the same (files vs. disks),
442 * keeping track of the number of elements in the
443 * process.
445 * We also check that the size of each vdev (if it can
446 * be determined) is the same.
448 rep.zprl_type = type;
449 rep.zprl_children = 0;
451 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
452 verify(nvlist_lookup_uint64(nv,
453 ZPOOL_CONFIG_NPARITY,
454 &rep.zprl_parity) == 0);
455 assert(rep.zprl_parity != 0);
456 } else {
457 rep.zprl_parity = 0;
461 * The 'dontreport' variable indicates that we've
462 * already reported an error for this spec, so don't
463 * bother doing it again.
465 type = NULL;
466 dontreport = 0;
467 vdev_size = -1ULL;
468 for (c = 0; c < children; c++) {
469 nvlist_t *cnv = child[c];
470 char *path;
471 struct stat64 statbuf;
472 uint64_t size = -1ULL;
473 char *childtype;
474 int fd, err;
476 rep.zprl_children++;
478 verify(nvlist_lookup_string(cnv,
479 ZPOOL_CONFIG_TYPE, &childtype) == 0);
482 * If this is a replacing or spare vdev, then
483 * get the real first child of the vdev.
485 if (strcmp(childtype,
486 VDEV_TYPE_REPLACING) == 0 ||
487 strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
488 nvlist_t **rchild;
489 uint_t rchildren;
491 verify(nvlist_lookup_nvlist_array(cnv,
492 ZPOOL_CONFIG_CHILDREN, &rchild,
493 &rchildren) == 0);
494 assert(rchildren == 2);
495 cnv = rchild[0];
497 verify(nvlist_lookup_string(cnv,
498 ZPOOL_CONFIG_TYPE,
499 &childtype) == 0);
502 verify(nvlist_lookup_string(cnv,
503 ZPOOL_CONFIG_PATH, &path) == 0);
506 * If we have a raidz/mirror that combines disks
507 * with files, report it as an error.
509 if (!dontreport && type != NULL &&
510 strcmp(type, childtype) != 0) {
511 if (ret != NULL)
512 free(ret);
513 ret = NULL;
514 if (fatal)
515 vdev_error(gettext(
516 "mismatched replication "
517 "level: %s contains both "
518 "files and devices\n"),
519 rep.zprl_type);
520 else
521 return (NULL);
522 dontreport = B_TRUE;
526 * According to stat(2), the value of 'st_size'
527 * is undefined for block devices and character
528 * devices. But there is no effective way to
529 * determine the real size in userland.
531 * Instead, we'll take advantage of an
532 * implementation detail of spec_size(). If the
533 * device is currently open, then we (should)
534 * return a valid size.
536 * If we still don't get a valid size (indicated
537 * by a size of 0 or MAXOFFSET_T), then ignore
538 * this device altogether.
540 if ((fd = open(path, O_RDONLY)) >= 0) {
541 err = fstat64(fd, &statbuf);
542 (void) close(fd);
543 } else {
544 err = stat64(path, &statbuf);
547 if (err != 0 ||
548 statbuf.st_size == 0 ||
549 statbuf.st_size == MAXOFFSET_T)
550 continue;
552 size = statbuf.st_size;
555 * Also make sure that devices and
556 * slices have a consistent size. If
557 * they differ by a significant amount
558 * (~16MB) then report an error.
560 if (!dontreport &&
561 (vdev_size != -1ULL &&
562 (labs(size - vdev_size) >
563 ZPOOL_FUZZ))) {
564 if (ret != NULL)
565 free(ret);
566 ret = NULL;
567 if (fatal)
568 vdev_error(gettext(
569 "%s contains devices of "
570 "different sizes\n"),
571 rep.zprl_type);
572 else
573 return (NULL);
574 dontreport = B_TRUE;
577 type = childtype;
578 vdev_size = size;
583 * At this point, we have the replication of the last toplevel
584 * vdev in 'rep'. Compare it to 'lastrep' to see if its
585 * different.
587 if (lastrep.zprl_type != NULL) {
588 if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
589 if (ret != NULL)
590 free(ret);
591 ret = NULL;
592 if (fatal)
593 vdev_error(gettext(
594 "mismatched replication level: "
595 "both %s and %s vdevs are "
596 "present\n"),
597 lastrep.zprl_type, rep.zprl_type);
598 else
599 return (NULL);
600 } else if (lastrep.zprl_parity != rep.zprl_parity) {
601 if (ret)
602 free(ret);
603 ret = NULL;
604 if (fatal)
605 vdev_error(gettext(
606 "mismatched replication level: "
607 "both %llu and %llu device parity "
608 "%s vdevs are present\n"),
609 lastrep.zprl_parity,
610 rep.zprl_parity,
611 rep.zprl_type);
612 else
613 return (NULL);
614 } else if (lastrep.zprl_children != rep.zprl_children) {
615 if (ret)
616 free(ret);
617 ret = NULL;
618 if (fatal)
619 vdev_error(gettext(
620 "mismatched replication level: "
621 "both %llu-way and %llu-way %s "
622 "vdevs are present\n"),
623 lastrep.zprl_children,
624 rep.zprl_children,
625 rep.zprl_type);
626 else
627 return (NULL);
630 lastrep = rep;
633 if (ret != NULL)
634 *ret = rep;
636 return (ret);
640 * Check the replication level of the vdev spec against the current pool. Calls
641 * get_replication() to make sure the new spec is self-consistent. If the pool
642 * has a consistent replication level, then we ignore any errors. Otherwise,
643 * report any difference between the two.
645 static int
646 check_replication(nvlist_t *config, nvlist_t *newroot)
648 nvlist_t **child;
649 uint_t children;
650 replication_level_t *current = NULL, *new;
651 int ret;
654 * If we have a current pool configuration, check to see if it's
655 * self-consistent. If not, simply return success.
657 if (config != NULL) {
658 nvlist_t *nvroot;
660 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
661 &nvroot) == 0);
662 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
663 return (0);
666 * for spares there may be no children, and therefore no
667 * replication level to check
669 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
670 &child, &children) != 0) || (children == 0)) {
671 free(current);
672 return (0);
676 * If all we have is logs then there's no replication level to check.
678 if (num_logs(newroot) == children) {
679 free(current);
680 return (0);
684 * Get the replication level of the new vdev spec, reporting any
685 * inconsistencies found.
687 if ((new = get_replication(newroot, B_TRUE)) == NULL) {
688 free(current);
689 return (-1);
693 * Check to see if the new vdev spec matches the replication level of
694 * the current pool.
696 ret = 0;
697 if (current != NULL) {
698 if (strcmp(current->zprl_type, new->zprl_type) != 0) {
699 vdev_error(gettext(
700 "mismatched replication level: pool uses %s "
701 "and new vdev is %s\n"),
702 current->zprl_type, new->zprl_type);
703 ret = -1;
704 } else if (current->zprl_parity != new->zprl_parity) {
705 vdev_error(gettext(
706 "mismatched replication level: pool uses %llu "
707 "device parity and new vdev uses %llu\n"),
708 current->zprl_parity, new->zprl_parity);
709 ret = -1;
710 } else if (current->zprl_children != new->zprl_children) {
711 vdev_error(gettext(
712 "mismatched replication level: pool uses %llu-way "
713 "%s and new vdev uses %llu-way %s\n"),
714 current->zprl_children, current->zprl_type,
715 new->zprl_children, new->zprl_type);
716 ret = -1;
720 free(new);
721 if (current != NULL)
722 free(current);
724 return (ret);
728 * Go through and find any whole disks in the vdev specification, labelling them
729 * as appropriate. When constructing the vdev spec, we were unable to open this
730 * device in order to provide a devid. Now that we have labelled the disk and
731 * know that slice 0 is valid, we can construct the devid now.
733 * If the disk was already labeled with an EFI label, we will have gotten the
734 * devid already (because we were able to open the whole disk). Otherwise, we
735 * need to get the devid after we label the disk.
737 static int
738 make_disks(zpool_handle_t *zhp, nvlist_t *nv)
740 nvlist_t **child;
741 uint_t c, children;
742 char *type, *path, *diskname;
743 char buf[MAXPATHLEN];
744 uint64_t wholedisk;
745 int fd;
746 int ret;
747 ddi_devid_t devid;
748 char *minor = NULL, *devid_str = NULL;
750 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
752 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
753 &child, &children) != 0) {
755 if (strcmp(type, VDEV_TYPE_DISK) != 0)
756 return (0);
759 * We have a disk device. Get the path to the device
760 * and see if it's a whole disk by appending the backup
761 * slice and stat()ing the device.
763 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
764 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
765 &wholedisk) != 0 || !wholedisk)
766 return (0);
768 diskname = strrchr(path, '/');
769 assert(diskname != NULL);
770 diskname++;
771 if (zpool_label_disk(g_zfs, zhp, diskname) == -1)
772 return (-1);
775 * Fill in the devid, now that we've labeled the disk.
777 (void) snprintf(buf, sizeof (buf), "%ss0", path);
778 if ((fd = open(buf, O_RDONLY)) < 0) {
779 (void) fprintf(stderr,
780 gettext("cannot open '%s': %s\n"),
781 buf, strerror(errno));
782 return (-1);
785 if (devid_get(fd, &devid) == 0) {
786 if (devid_get_minor_name(fd, &minor) == 0 &&
787 (devid_str = devid_str_encode(devid, minor)) !=
788 NULL) {
789 verify(nvlist_add_string(nv,
790 ZPOOL_CONFIG_DEVID, devid_str) == 0);
792 if (devid_str != NULL)
793 devid_str_free(devid_str);
794 if (minor != NULL)
795 devid_str_free(minor);
796 devid_free(devid);
800 * Update the path to refer to the 's0' slice. The presence of
801 * the 'whole_disk' field indicates to the CLI that we should
802 * chop off the slice number when displaying the device in
803 * future output.
805 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0);
807 (void) close(fd);
809 return (0);
812 for (c = 0; c < children; c++)
813 if ((ret = make_disks(zhp, child[c])) != 0)
814 return (ret);
816 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
817 &child, &children) == 0)
818 for (c = 0; c < children; c++)
819 if ((ret = make_disks(zhp, child[c])) != 0)
820 return (ret);
822 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
823 &child, &children) == 0)
824 for (c = 0; c < children; c++)
825 if ((ret = make_disks(zhp, child[c])) != 0)
826 return (ret);
828 return (0);
832 * Determine if the given path is a hot spare within the given configuration.
834 static boolean_t
835 is_spare(nvlist_t *config, const char *path)
837 int fd;
838 pool_state_t state;
839 char *name = NULL;
840 nvlist_t *label;
841 uint64_t guid, spareguid;
842 nvlist_t *nvroot;
843 nvlist_t **spares;
844 uint_t i, nspares;
845 boolean_t inuse;
847 if ((fd = open(path, O_RDONLY)) < 0)
848 return (B_FALSE);
850 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
851 !inuse ||
852 state != POOL_STATE_SPARE ||
853 zpool_read_label(fd, &label) != 0) {
854 free(name);
855 (void) close(fd);
856 return (B_FALSE);
858 free(name);
860 (void) close(fd);
861 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
862 nvlist_free(label);
864 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
865 &nvroot) == 0);
866 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
867 &spares, &nspares) == 0) {
868 for (i = 0; i < nspares; i++) {
869 verify(nvlist_lookup_uint64(spares[i],
870 ZPOOL_CONFIG_GUID, &spareguid) == 0);
871 if (spareguid == guid)
872 return (B_TRUE);
876 return (B_FALSE);
880 * Go through and find any devices that are in use. We rely on libdiskmgt for
881 * the majority of this task.
883 static int
884 check_in_use(nvlist_t *config, nvlist_t *nv, int force, int isreplacing,
885 int isspare)
887 nvlist_t **child;
888 uint_t c, children;
889 char *type, *path;
890 int ret;
891 char buf[MAXPATHLEN];
892 uint64_t wholedisk;
894 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
896 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
897 &child, &children) != 0) {
899 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
902 * As a generic check, we look to see if this is a replace of a
903 * hot spare within the same pool. If so, we allow it
904 * regardless of what libdiskmgt or zpool_in_use() says.
906 if (isreplacing) {
907 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
908 &wholedisk) == 0 && wholedisk)
909 (void) snprintf(buf, sizeof (buf), "%ss0",
910 path);
911 else
912 (void) strlcpy(buf, path, sizeof (buf));
913 if (is_spare(config, buf))
914 return (0);
917 if (strcmp(type, VDEV_TYPE_DISK) == 0 ||
918 strcmp(type, VDEV_TYPE_FILE) == 0)
919 ret = check_file(path, force, isspare);
921 return (ret);
924 for (c = 0; c < children; c++)
925 if ((ret = check_in_use(config, child[c], force,
926 isreplacing, B_FALSE)) != 0)
927 return (ret);
929 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
930 &child, &children) == 0)
931 for (c = 0; c < children; c++)
932 if ((ret = check_in_use(config, child[c], force,
933 isreplacing, B_TRUE)) != 0)
934 return (ret);
936 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
937 &child, &children) == 0)
938 for (c = 0; c < children; c++)
939 if ((ret = check_in_use(config, child[c], force,
940 isreplacing, B_FALSE)) != 0)
941 return (ret);
943 return (0);
946 static const char *
947 is_grouping(const char *type, int *mindev)
949 if (strcmp(type, "raidz") == 0 || strcmp(type, "raidz1") == 0) {
950 if (mindev != NULL)
951 *mindev = 2;
952 return (VDEV_TYPE_RAIDZ);
955 if (strcmp(type, "raidz2") == 0) {
956 if (mindev != NULL)
957 *mindev = 3;
958 return (VDEV_TYPE_RAIDZ);
961 if (strcmp(type, "mirror") == 0) {
962 if (mindev != NULL)
963 *mindev = 2;
964 return (VDEV_TYPE_MIRROR);
967 if (strcmp(type, "spare") == 0) {
968 if (mindev != NULL)
969 *mindev = 1;
970 return (VDEV_TYPE_SPARE);
973 if (strcmp(type, "log") == 0) {
974 if (mindev != NULL)
975 *mindev = 1;
976 return (VDEV_TYPE_LOG);
979 if (strcmp(type, "cache") == 0) {
980 if (mindev != NULL)
981 *mindev = 1;
982 return (VDEV_TYPE_L2CACHE);
985 return (NULL);
989 * Construct a syntactically valid vdev specification,
990 * and ensure that all devices and files exist and can be opened.
991 * Note: we don't bother freeing anything in the error paths
992 * because the program is just going to exit anyway.
994 nvlist_t *
995 construct_spec(int argc, char **argv)
997 nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
998 int t, toplevels, mindev, nspares, nlogs, nl2cache;
999 const char *type;
1000 uint64_t is_log;
1001 boolean_t seen_logs;
1003 top = NULL;
1004 toplevels = 0;
1005 spares = NULL;
1006 l2cache = NULL;
1007 nspares = 0;
1008 nlogs = 0;
1009 nl2cache = 0;
1010 is_log = B_FALSE;
1011 seen_logs = B_FALSE;
1013 while (argc > 0) {
1014 nv = NULL;
1017 * If it's a mirror or raidz, the subsequent arguments are
1018 * its leaves -- until we encounter the next mirror or raidz.
1020 if ((type = is_grouping(argv[0], &mindev)) != NULL) {
1021 nvlist_t **child = NULL;
1022 int c, children = 0;
1024 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1025 if (spares != NULL) {
1026 (void) fprintf(stderr,
1027 gettext("invalid vdev "
1028 "specification: 'spare' can be "
1029 "specified only once\n"));
1030 return (NULL);
1032 is_log = B_FALSE;
1035 if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1036 if (seen_logs) {
1037 (void) fprintf(stderr,
1038 gettext("invalid vdev "
1039 "specification: 'log' can be "
1040 "specified only once\n"));
1041 return (NULL);
1043 seen_logs = B_TRUE;
1044 is_log = B_TRUE;
1045 argc--;
1046 argv++;
1048 * A log is not a real grouping device.
1049 * We just set is_log and continue.
1051 continue;
1054 if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1055 if (l2cache != NULL) {
1056 (void) fprintf(stderr,
1057 gettext("invalid vdev "
1058 "specification: 'cache' can be "
1059 "specified only once\n"));
1060 return (NULL);
1062 is_log = B_FALSE;
1065 if (is_log) {
1066 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1067 (void) fprintf(stderr,
1068 gettext("invalid vdev "
1069 "specification: unsupported 'log' "
1070 "device: %s\n"), type);
1071 return (NULL);
1073 nlogs++;
1076 for (c = 1; c < argc; c++) {
1077 if (is_grouping(argv[c], NULL) != NULL)
1078 break;
1079 children++;
1080 child = realloc(child,
1081 children * sizeof (nvlist_t *));
1082 if (child == NULL)
1083 zpool_no_memory();
1084 if ((nv = make_leaf_vdev(argv[c], B_FALSE))
1085 == NULL)
1086 return (NULL);
1087 child[children - 1] = nv;
1090 if (children < mindev) {
1091 (void) fprintf(stderr, gettext("invalid vdev "
1092 "specification: %s requires at least %d "
1093 "devices\n"), argv[0], mindev);
1094 return (NULL);
1097 argc -= c;
1098 argv += c;
1100 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1101 spares = child;
1102 nspares = children;
1103 continue;
1104 } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1105 l2cache = child;
1106 nl2cache = children;
1107 continue;
1108 } else {
1109 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1110 0) == 0);
1111 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1112 type) == 0);
1113 verify(nvlist_add_uint64(nv,
1114 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1115 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1116 verify(nvlist_add_uint64(nv,
1117 ZPOOL_CONFIG_NPARITY,
1118 mindev - 1) == 0);
1120 verify(nvlist_add_nvlist_array(nv,
1121 ZPOOL_CONFIG_CHILDREN, child,
1122 children) == 0);
1124 for (c = 0; c < children; c++)
1125 nvlist_free(child[c]);
1126 free(child);
1128 } else {
1130 * We have a device. Pass off to make_leaf_vdev() to
1131 * construct the appropriate nvlist describing the vdev.
1133 if ((nv = make_leaf_vdev(argv[0], is_log)) == NULL)
1134 return (NULL);
1135 if (is_log)
1136 nlogs++;
1137 argc--;
1138 argv++;
1141 toplevels++;
1142 top = realloc(top, toplevels * sizeof (nvlist_t *));
1143 if (top == NULL)
1144 zpool_no_memory();
1145 top[toplevels - 1] = nv;
1148 if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1149 (void) fprintf(stderr, gettext("invalid vdev "
1150 "specification: at least one toplevel vdev must be "
1151 "specified\n"));
1152 return (NULL);
1155 if (seen_logs && nlogs == 0) {
1156 (void) fprintf(stderr, gettext("invalid vdev specification: "
1157 "log requires at least 1 device\n"));
1158 return (NULL);
1162 * Finally, create nvroot and add all top-level vdevs to it.
1164 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1165 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1166 VDEV_TYPE_ROOT) == 0);
1167 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1168 top, toplevels) == 0);
1169 if (nspares != 0)
1170 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1171 spares, nspares) == 0);
1172 if (nl2cache != 0)
1173 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1174 l2cache, nl2cache) == 0);
1176 for (t = 0; t < toplevels; t++)
1177 nvlist_free(top[t]);
1178 for (t = 0; t < nspares; t++)
1179 nvlist_free(spares[t]);
1180 for (t = 0; t < nl2cache; t++)
1181 nvlist_free(l2cache[t]);
1182 if (spares)
1183 free(spares);
1184 if (l2cache)
1185 free(l2cache);
1186 free(top);
1188 return (nvroot);
1193 * Get and validate the contents of the given vdev specification. This ensures
1194 * that the nvlist returned is well-formed, that all the devices exist, and that
1195 * they are not currently in use by any other known consumer. The 'poolconfig'
1196 * parameter is the current configuration of the pool when adding devices
1197 * existing pool, and is used to perform additional checks, such as changing the
1198 * replication level of the pool. It can be 'NULL' to indicate that this is a
1199 * new pool. The 'force' flag controls whether devices should be forcefully
1200 * added, even if they appear in use.
1202 nvlist_t *
1203 make_root_vdev(zpool_handle_t *zhp, int force, int check_rep,
1204 boolean_t isreplacing, boolean_t dryrun, int argc, char **argv)
1206 nvlist_t *newroot;
1207 nvlist_t *poolconfig = NULL;
1208 is_force = force;
1211 * Construct the vdev specification. If this is successful, we know
1212 * that we have a valid specification, and that all devices can be
1213 * opened.
1215 if ((newroot = construct_spec(argc, argv)) == NULL)
1216 return (NULL);
1218 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
1219 return (NULL);
1222 * Validate each device to make sure that its not shared with another
1223 * subsystem. We do this even if 'force' is set, because there are some
1224 * uses (such as a dedicated dump device) that even '-f' cannot
1225 * override.
1227 if (check_in_use(poolconfig, newroot, force, isreplacing,
1228 B_FALSE) != 0) {
1229 nvlist_free(newroot);
1230 return (NULL);
1234 * Check the replication level of the given vdevs and report any errors
1235 * found. We include the existing pool spec, if any, as we need to
1236 * catch changes against the existing replication level.
1238 if (check_rep && check_replication(poolconfig, newroot) != 0) {
1239 nvlist_free(newroot);
1240 return (NULL);
1244 * Run through the vdev specification and label any whole disks found.
1246 if (!dryrun && make_disks(zhp, newroot) != 0) {
1247 nvlist_free(newroot);
1248 return (NULL);
1251 return (newroot);