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