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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 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 <libdiskmgt.h> 70 #include <libintl.h> 71 #include <libnvpair.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> 79 80 #include "zpool_util.h" 81 82 #define DISK_ROOT "/dev/dsk" 83 #define RDISK_ROOT "/dev/rdsk" 84 #define BACKUP_SLICE "s2" 85 86 /* 87 * For any given vdev specification, we can have multiple errors. The 88 * vdev_error() function keeps track of whether we have seen an error yet, and 89 * prints out a header if its the first error we've seen. 90 */ 91 boolean_t error_seen; 92 boolean_t is_force; 93 94 /*PRINTFLIKE1*/ 95 static void 96 vdev_error(const char *fmt, ...) 97 { 98 va_list ap; 99 100 if (!error_seen) { 101 (void) fprintf(stderr, gettext("invalid vdev specification\n")); 102 if (!is_force) 103 (void) fprintf(stderr, gettext("use '-f' to override " 104 "the following errors:\n")); 105 else 106 (void) fprintf(stderr, gettext("the following errors " 107 "must be manually repaired:\n")); 108 error_seen = B_TRUE; 109 } 110 111 va_start(ap, fmt); 112 (void) vfprintf(stderr, fmt, ap); 113 va_end(ap); 114 } 115 116 static void 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 134 check_slice(const char *path, int force, boolean_t wholedisk, boolean_t isspare) 135 { 136 char *msg; 137 int error = 0; 138 139 if (dm_inuse((char *)path, &msg, isspare ? DM_WHO_ZPOOL_SPARE : 140 (force ? DM_WHO_ZPOOL_FORCE : DM_WHO_ZPOOL), &error) || error) { 141 if (error != 0) { 142 libdiskmgt_error(error); 143 return (0); 144 } else { 145 vdev_error("%s", msg); 146 free(msg); 147 return (-1); 148 } 149 } 150 151 /* 152 * If we're given a whole disk, ignore overlapping slices since we're 153 * about to label it anyway. 154 */ 155 error = 0; 156 if (!wholedisk && !force && 157 (dm_isoverlapping((char *)path, &msg, &error) || error)) { 158 if (error == 0) { 159 /* dm_isoverlapping returned -1 */ 160 vdev_error(gettext("%s overlaps with %s\n"), path, msg); 161 free(msg); 162 return (-1); 163 } else if (error != ENODEV) { 164 /* libdiskmgt's devcache only handles physical drives */ 165 libdiskmgt_error(error); 166 return (0); 167 } 168 } 169 170 return (0); 171 } 172 173 174 /* 175 * Validate a whole disk. Iterate over all slices on the disk and make sure 176 * that none is in use by calling check_slice(). 177 */ 178 static int 179 check_disk(const char *name, dm_descriptor_t disk, int force, int isspare) 180 { 181 dm_descriptor_t *drive, *media, *slice; 182 int err = 0; 183 int i; 184 int ret; 185 186 /* 187 * Get the drive associated with this disk. This should never fail, 188 * because we already have an alias handle open for the device. 189 */ 190 if ((drive = dm_get_associated_descriptors(disk, DM_DRIVE, 191 &err)) == NULL || *drive == NULL) { 192 if (err) 193 libdiskmgt_error(err); 194 return (0); 195 } 196 197 if ((media = dm_get_associated_descriptors(*drive, DM_MEDIA, 198 &err)) == NULL) { 199 dm_free_descriptors(drive); 200 if (err) 201 libdiskmgt_error(err); 202 return (0); 203 } 204 205 dm_free_descriptors(drive); 206 207 /* 208 * It is possible that the user has specified a removable media drive, 209 * and the media is not present. 210 */ 211 if (*media == NULL) { 212 dm_free_descriptors(media); 213 vdev_error(gettext("'%s' has no media in drive\n"), name); 214 return (-1); 215 } 216 217 if ((slice = dm_get_associated_descriptors(*media, DM_SLICE, 218 &err)) == NULL) { 219 dm_free_descriptors(media); 220 if (err) 221 libdiskmgt_error(err); 222 return (0); 223 } 224 225 dm_free_descriptors(media); 226 227 ret = 0; 228 229 /* 230 * Iterate over all slices and report any errors. We don't care about 231 * overlapping slices because we are using the whole disk. 232 */ 233 for (i = 0; slice[i] != NULL; i++) { 234 char *name = dm_get_name(slice[i], &err); 235 236 if (check_slice(name, force, B_TRUE, isspare) != 0) 237 ret = -1; 238 239 dm_free_name(name); 240 } 241 242 dm_free_descriptors(slice); 243 return (ret); 244 } 245 246 /* 247 * Validate a device. 248 */ 249 static int 250 check_device(const char *path, boolean_t force, boolean_t isspare) 251 { 252 dm_descriptor_t desc; 253 int err; 254 char *dev; 255 256 /* 257 * For whole disks, libdiskmgt does not include the leading dev path. 258 */ 259 dev = strrchr(path, '/'); 260 assert(dev != NULL); 261 dev++; 262 if ((desc = dm_get_descriptor_by_name(DM_ALIAS, dev, &err)) != NULL) { 263 err = check_disk(path, desc, force, isspare); 264 dm_free_descriptor(desc); 265 return (err); 266 } 267 268 return (check_slice(path, force, B_FALSE, isspare)); 269 } 270 271 /* 272 * Check that a file is valid. All we can do in this case is check that it's 273 * not in use by another pool, and not in use by swap. 274 */ 275 static int 276 check_file(const char *file, boolean_t force, boolean_t isspare) 277 { 278 char *name; 279 int fd; 280 int ret = 0; 281 int err; 282 pool_state_t state; 283 boolean_t inuse; 284 285 if (dm_inuse_swap(file, &err)) { 286 if (err) 287 libdiskmgt_error(err); 288 else 289 vdev_error(gettext("%s is currently used by swap. " 290 "Please see swap(1M).\n"), file); 291 return (-1); 292 } 293 294 if ((fd = open(file, O_RDONLY)) < 0) 295 return (0); 296 297 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) { 298 const char *desc; 299 300 switch (state) { 301 case POOL_STATE_ACTIVE: 302 desc = gettext("active"); 303 break; 304 305 case POOL_STATE_EXPORTED: 306 desc = gettext("exported"); 307 break; 308 309 case POOL_STATE_POTENTIALLY_ACTIVE: 310 desc = gettext("potentially active"); 311 break; 312 313 default: 314 desc = gettext("unknown"); 315 break; 316 } 317 318 /* 319 * Allow hot spares to be shared between pools. 320 */ 321 if (state == POOL_STATE_SPARE && isspare) 322 return (0); 323 324 if (state == POOL_STATE_ACTIVE || 325 state == POOL_STATE_SPARE || !force) { 326 switch (state) { 327 case POOL_STATE_SPARE: 328 vdev_error(gettext("%s is reserved as a hot " 329 "spare for pool %s\n"), file, name); 330 break; 331 default: 332 vdev_error(gettext("%s is part of %s pool " 333 "'%s'\n"), file, desc, name); 334 break; 335 } 336 ret = -1; 337 } 338 339 free(name); 340 } 341 342 (void) close(fd); 343 return (ret); 344 } 345 346 347 /* 348 * By "whole disk" we mean an entire physical disk (something we can 349 * label, toggle the write cache on, etc.) as opposed to the full 350 * capacity of a pseudo-device such as lofi or did. We act as if we 351 * are labeling the disk, which should be a pretty good test of whether 352 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if 353 * it isn't. 354 */ 355 static boolean_t 356 is_whole_disk(const char *arg) 357 { 358 struct dk_gpt *label; 359 int fd; 360 char path[MAXPATHLEN]; 361 362 (void) snprintf(path, sizeof (path), "%s%s%s", 363 RDISK_ROOT, strrchr(arg, '/'), BACKUP_SLICE); 364 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) 365 return (B_FALSE); 366 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) { 367 (void) close(fd); 368 return (B_FALSE); 369 } 370 efi_free(label); 371 (void) close(fd); 372 return (B_TRUE); 373 } 374 375 /* 376 * Create a leaf vdev. Determine if this is a file or a device. If it's a 377 * device, fill in the device id to make a complete nvlist. Valid forms for a 378 * leaf vdev are: 379 * 380 * /dev/dsk/xxx Complete disk path 381 * /xxx Full path to file 382 * xxx Shorthand for /dev/dsk/xxx 383 */ 384 static nvlist_t * 385 make_leaf_vdev(const char *arg) 386 { 387 char path[MAXPATHLEN]; 388 struct stat64 statbuf; 389 nvlist_t *vdev = NULL; 390 char *type = NULL; 391 boolean_t wholedisk = B_FALSE; 392 393 /* 394 * Determine what type of vdev this is, and put the full path into 395 * 'path'. We detect whether this is a device of file afterwards by 396 * checking the st_mode of the file. 397 */ 398 if (arg[0] == '/') { 399 /* 400 * Complete device or file path. Exact type is determined by 401 * examining the file descriptor afterwards. 402 */ 403 wholedisk = is_whole_disk(arg); 404 if (!wholedisk && (stat64(arg, &statbuf) != 0)) { 405 (void) fprintf(stderr, 406 gettext("cannot open '%s': %s\n"), 407 arg, strerror(errno)); 408 return (NULL); 409 } 410 411 (void) strlcpy(path, arg, sizeof (path)); 412 } else { 413 /* 414 * This may be a short path for a device, or it could be total 415 * gibberish. Check to see if it's a known device in 416 * /dev/dsk/. As part of this check, see if we've been given a 417 * an entire disk (minus the slice number). 418 */ 419 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, 420 arg); 421 wholedisk = is_whole_disk(path); 422 if (!wholedisk && (stat64(path, &statbuf) != 0)) { 423 /* 424 * If we got ENOENT, then the user gave us 425 * gibberish, so try to direct them with a 426 * reasonable error message. Otherwise, 427 * regurgitate strerror() since it's the best we 428 * can do. 429 */ 430 if (errno == ENOENT) { 431 (void) fprintf(stderr, 432 gettext("cannot open '%s': no such " 433 "device in %s\n"), arg, DISK_ROOT); 434 (void) fprintf(stderr, 435 gettext("must be a full path or " 436 "shorthand device name\n")); 437 return (NULL); 438 } else { 439 (void) fprintf(stderr, 440 gettext("cannot open '%s': %s\n"), 441 path, strerror(errno)); 442 return (NULL); 443 } 444 } 445 } 446 447 /* 448 * Determine whether this is a device or a file. 449 */ 450 if (wholedisk || S_ISBLK(statbuf.st_mode)) { 451 type = VDEV_TYPE_DISK; 452 } else if (S_ISREG(statbuf.st_mode)) { 453 type = VDEV_TYPE_FILE; 454 } else { 455 (void) fprintf(stderr, gettext("cannot use '%s': must be a " 456 "block device or regular file\n"), path); 457 return (NULL); 458 } 459 460 /* 461 * Finally, we have the complete device or file, and we know that it is 462 * acceptable to use. Construct the nvlist to describe this vdev. All 463 * vdevs have a 'path' element, and devices also have a 'devid' element. 464 */ 465 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0); 466 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0); 467 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0); 468 if (strcmp(type, VDEV_TYPE_DISK) == 0) 469 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK, 470 (uint64_t)wholedisk) == 0); 471 472 /* 473 * For a whole disk, defer getting its devid until after labeling it. 474 */ 475 if (S_ISBLK(statbuf.st_mode) && !wholedisk) { 476 /* 477 * Get the devid for the device. 478 */ 479 int fd; 480 ddi_devid_t devid; 481 char *minor = NULL, *devid_str = NULL; 482 483 if ((fd = open(path, O_RDONLY)) < 0) { 484 (void) fprintf(stderr, gettext("cannot open '%s': " 485 "%s\n"), path, strerror(errno)); 486 nvlist_free(vdev); 487 return (NULL); 488 } 489 490 if (devid_get(fd, &devid) == 0) { 491 if (devid_get_minor_name(fd, &minor) == 0 && 492 (devid_str = devid_str_encode(devid, minor)) != 493 NULL) { 494 verify(nvlist_add_string(vdev, 495 ZPOOL_CONFIG_DEVID, devid_str) == 0); 496 } 497 if (devid_str != NULL) 498 devid_str_free(devid_str); 499 if (minor != NULL) 500 devid_str_free(minor); 501 devid_free(devid); 502 } 503 504 (void) close(fd); 505 } 506 507 return (vdev); 508 } 509 510 /* 511 * Go through and verify the replication level of the pool is consistent. 512 * Performs the following checks: 513 * 514 * For the new spec, verifies that devices in mirrors and raidz are the 515 * same size. 516 * 517 * If the current configuration already has inconsistent replication 518 * levels, ignore any other potential problems in the new spec. 519 * 520 * Otherwise, make sure that the current spec (if there is one) and the new 521 * spec have consistent replication levels. 522 */ 523 typedef struct replication_level { 524 char *zprl_type; 525 uint64_t zprl_children; 526 uint64_t zprl_parity; 527 } replication_level_t; 528 529 #define ZPOOL_FUZZ (16 * 1024 * 1024) 530 531 /* 532 * Given a list of toplevel vdevs, return the current replication level. If 533 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then 534 * an error message will be displayed for each self-inconsistent vdev. 535 */ 536 static replication_level_t * 537 get_replication(nvlist_t *nvroot, boolean_t fatal) 538 { 539 nvlist_t **top; 540 uint_t t, toplevels; 541 nvlist_t **child; 542 uint_t c, children; 543 nvlist_t *nv; 544 char *type; 545 replication_level_t lastrep, rep, *ret; 546 boolean_t dontreport; 547 548 ret = safe_malloc(sizeof (replication_level_t)); 549 550 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 551 &top, &toplevels) == 0); 552 553 lastrep.zprl_type = NULL; 554 for (t = 0; t < toplevels; t++) { 555 nv = top[t]; 556 557 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 558 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 559 &child, &children) != 0) { 560 /* 561 * This is a 'file' or 'disk' vdev. 562 */ 563 rep.zprl_type = type; 564 rep.zprl_children = 1; 565 rep.zprl_parity = 0; 566 } else { 567 uint64_t vdev_size; 568 569 /* 570 * This is a mirror or RAID-Z vdev. Go through and make 571 * sure the contents are all the same (files vs. disks), 572 * keeping track of the number of elements in the 573 * process. 574 * 575 * We also check that the size of each vdev (if it can 576 * be determined) is the same. 577 */ 578 rep.zprl_type = type; 579 rep.zprl_children = 0; 580 581 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) { 582 verify(nvlist_lookup_uint64(nv, 583 ZPOOL_CONFIG_NPARITY, 584 &rep.zprl_parity) == 0); 585 assert(rep.zprl_parity != 0); 586 } else { 587 rep.zprl_parity = 0; 588 } 589 590 /* 591 * The 'dontreport' variable indicatest that we've 592 * already reported an error for this spec, so don't 593 * bother doing it again. 594 */ 595 type = NULL; 596 dontreport = 0; 597 vdev_size = -1ULL; 598 for (c = 0; c < children; c++) { 599 nvlist_t *cnv = child[c]; 600 char *path; 601 struct stat64 statbuf; 602 uint64_t size = -1ULL; 603 char *childtype; 604 int fd, err; 605 606 rep.zprl_children++; 607 608 verify(nvlist_lookup_string(cnv, 609 ZPOOL_CONFIG_TYPE, &childtype) == 0); 610 611 /* 612 * If this is a a replacing or spare vdev, then 613 * get the real first child of the vdev. 614 */ 615 if (strcmp(childtype, 616 VDEV_TYPE_REPLACING) == 0 || 617 strcmp(childtype, VDEV_TYPE_SPARE) == 0) { 618 nvlist_t **rchild; 619 uint_t rchildren; 620 621 verify(nvlist_lookup_nvlist_array(cnv, 622 ZPOOL_CONFIG_CHILDREN, &rchild, 623 &rchildren) == 0); 624 assert(rchildren == 2); 625 cnv = rchild[0]; 626 627 verify(nvlist_lookup_string(cnv, 628 ZPOOL_CONFIG_TYPE, 629 &childtype) == 0); 630 } 631 632 verify(nvlist_lookup_string(cnv, 633 ZPOOL_CONFIG_PATH, &path) == 0); 634 635 /* 636 * If we have a raidz/mirror that combines disks 637 * with files, report it as an error. 638 */ 639 if (!dontreport && type != NULL && 640 strcmp(type, childtype) != 0) { 641 if (ret != NULL) 642 free(ret); 643 ret = NULL; 644 if (fatal) 645 vdev_error(gettext( 646 "mismatched replication " 647 "level: %s contains both " 648 "files and devices\n"), 649 rep.zprl_type); 650 else 651 return (NULL); 652 dontreport = B_TRUE; 653 } 654 655 /* 656 * According to stat(2), the value of 'st_size' 657 * is undefined for block devices and character 658 * devices. But there is no effective way to 659 * determine the real size in userland. 660 * 661 * Instead, we'll take advantage of an 662 * implementation detail of spec_size(). If the 663 * device is currently open, then we (should) 664 * return a valid size. 665 * 666 * If we still don't get a valid size (indicated 667 * by a size of 0 or MAXOFFSET_T), then ignore 668 * this device altogether. 669 */ 670 if ((fd = open(path, O_RDONLY)) >= 0) { 671 err = fstat64(fd, &statbuf); 672 (void) close(fd); 673 } else { 674 err = stat64(path, &statbuf); 675 } 676 677 if (err != 0 || 678 statbuf.st_size == 0 || 679 statbuf.st_size == MAXOFFSET_T) 680 continue; 681 682 size = statbuf.st_size; 683 684 /* 685 * Also make sure that devices and 686 * slices have a consistent size. If 687 * they differ by a significant amount 688 * (~16MB) then report an error. 689 */ 690 if (!dontreport && 691 (vdev_size != -1ULL && 692 (labs(size - vdev_size) > 693 ZPOOL_FUZZ))) { 694 if (ret != NULL) 695 free(ret); 696 ret = NULL; 697 if (fatal) 698 vdev_error(gettext( 699 "%s contains devices of " 700 "different sizes\n"), 701 rep.zprl_type); 702 else 703 return (NULL); 704 dontreport = B_TRUE; 705 } 706 707 type = childtype; 708 vdev_size = size; 709 } 710 } 711 712 /* 713 * At this point, we have the replication of the last toplevel 714 * vdev in 'rep'. Compare it to 'lastrep' to see if its 715 * different. 716 */ 717 if (lastrep.zprl_type != NULL) { 718 if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) { 719 if (ret != NULL) 720 free(ret); 721 ret = NULL; 722 if (fatal) 723 vdev_error(gettext( 724 "mismatched replication level: " 725 "both %s and %s vdevs are " 726 "present\n"), 727 lastrep.zprl_type, rep.zprl_type); 728 else 729 return (NULL); 730 } else if (lastrep.zprl_parity != rep.zprl_parity) { 731 if (ret) 732 free(ret); 733 ret = NULL; 734 if (fatal) 735 vdev_error(gettext( 736 "mismatched replication level: " 737 "both %llu and %llu device parity " 738 "%s vdevs are present\n"), 739 lastrep.zprl_parity, 740 rep.zprl_parity, 741 rep.zprl_type); 742 else 743 return (NULL); 744 } else if (lastrep.zprl_children != rep.zprl_children) { 745 if (ret) 746 free(ret); 747 ret = NULL; 748 if (fatal) 749 vdev_error(gettext( 750 "mismatched replication level: " 751 "both %llu-way and %llu-way %s " 752 "vdevs are present\n"), 753 lastrep.zprl_children, 754 rep.zprl_children, 755 rep.zprl_type); 756 else 757 return (NULL); 758 } 759 } 760 lastrep = rep; 761 } 762 763 if (ret != NULL) 764 *ret = rep; 765 766 return (ret); 767 } 768 769 /* 770 * Check the replication level of the vdev spec against the current pool. Calls 771 * get_replication() to make sure the new spec is self-consistent. If the pool 772 * has a consistent replication level, then we ignore any errors. Otherwise, 773 * report any difference between the two. 774 */ 775 static int 776 check_replication(nvlist_t *config, nvlist_t *newroot) 777 { 778 nvlist_t **child; 779 uint_t children; 780 replication_level_t *current = NULL, *new; 781 int ret; 782 783 /* 784 * If we have a current pool configuration, check to see if it's 785 * self-consistent. If not, simply return success. 786 */ 787 if (config != NULL) { 788 nvlist_t *nvroot; 789 790 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 791 &nvroot) == 0); 792 if ((current = get_replication(nvroot, B_FALSE)) == NULL) 793 return (0); 794 } 795 /* 796 * for spares there may be no children, and therefore no 797 * replication level to check 798 */ 799 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN, 800 &child, &children) != 0) || (children == 0)) { 801 free(current); 802 return (0); 803 } 804 805 /* 806 * Get the replication level of the new vdev spec, reporting any 807 * inconsistencies found. 808 */ 809 if ((new = get_replication(newroot, B_TRUE)) == NULL) { 810 free(current); 811 return (-1); 812 } 813 814 /* 815 * Check to see if the new vdev spec matches the replication level of 816 * the current pool. 817 */ 818 ret = 0; 819 if (current != NULL) { 820 if (strcmp(current->zprl_type, new->zprl_type) != 0) { 821 vdev_error(gettext( 822 "mismatched replication level: pool uses %s " 823 "and new vdev is %s\n"), 824 current->zprl_type, new->zprl_type); 825 ret = -1; 826 } else if (current->zprl_parity != new->zprl_parity) { 827 vdev_error(gettext( 828 "mismatched replication level: pool uses %llu " 829 "device parity and new vdev uses %llu\n"), 830 current->zprl_parity, new->zprl_parity); 831 ret = -1; 832 } else if (current->zprl_children != new->zprl_children) { 833 vdev_error(gettext( 834 "mismatched replication level: pool uses %llu-way " 835 "%s and new vdev uses %llu-way %s\n"), 836 current->zprl_children, current->zprl_type, 837 new->zprl_children, new->zprl_type); 838 ret = -1; 839 } 840 } 841 842 free(new); 843 if (current != NULL) 844 free(current); 845 846 return (ret); 847 } 848 849 /* 850 * Go through and find any whole disks in the vdev specification, labelling them 851 * as appropriate. When constructing the vdev spec, we were unable to open this 852 * device in order to provide a devid. Now that we have labelled the disk and 853 * know that slice 0 is valid, we can construct the devid now. 854 * 855 * If the disk was already labeled with an EFI label, we will have gotten the 856 * devid already (because we were able to open the whole disk). Otherwise, we 857 * need to get the devid after we label the disk. 858 */ 859 static int 860 make_disks(zpool_handle_t *zhp, nvlist_t *nv) 861 { 862 nvlist_t **child; 863 uint_t c, children; 864 char *type, *path, *diskname; 865 char buf[MAXPATHLEN]; 866 uint64_t wholedisk; 867 int fd; 868 int ret; 869 ddi_devid_t devid; 870 char *minor = NULL, *devid_str = NULL; 871 872 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 873 874 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 875 &child, &children) != 0) { 876 877 if (strcmp(type, VDEV_TYPE_DISK) != 0) 878 return (0); 879 880 /* 881 * We have a disk device. Get the path to the device 882 * and see if it's a whole disk by appending the backup 883 * slice and stat()ing the device. 884 */ 885 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 886 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 887 &wholedisk) != 0 || !wholedisk) 888 return (0); 889 890 diskname = strrchr(path, '/'); 891 assert(diskname != NULL); 892 diskname++; 893 if (zpool_label_disk(g_zfs, zhp, diskname) == -1) 894 return (-1); 895 896 /* 897 * Fill in the devid, now that we've labeled the disk. 898 */ 899 (void) snprintf(buf, sizeof (buf), "%ss0", path); 900 if ((fd = open(buf, O_RDONLY)) < 0) { 901 (void) fprintf(stderr, 902 gettext("cannot open '%s': %s\n"), 903 buf, strerror(errno)); 904 return (-1); 905 } 906 907 if (devid_get(fd, &devid) == 0) { 908 if (devid_get_minor_name(fd, &minor) == 0 && 909 (devid_str = devid_str_encode(devid, minor)) != 910 NULL) { 911 verify(nvlist_add_string(nv, 912 ZPOOL_CONFIG_DEVID, devid_str) == 0); 913 } 914 if (devid_str != NULL) 915 devid_str_free(devid_str); 916 if (minor != NULL) 917 devid_str_free(minor); 918 devid_free(devid); 919 } 920 921 /* 922 * Update the path to refer to the 's0' slice. The presence of 923 * the 'whole_disk' field indicates to the CLI that we should 924 * chop off the slice number when displaying the device in 925 * future output. 926 */ 927 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0); 928 929 (void) close(fd); 930 931 return (0); 932 } 933 934 for (c = 0; c < children; c++) 935 if ((ret = make_disks(zhp, child[c])) != 0) 936 return (ret); 937 938 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 939 &child, &children) == 0) 940 for (c = 0; c < children; c++) 941 if ((ret = make_disks(zhp, child[c])) != 0) 942 return (ret); 943 944 return (0); 945 } 946 947 /* 948 * Determine if the given path is a hot spare within the given configuration. 949 */ 950 static boolean_t 951 is_spare(nvlist_t *config, const char *path) 952 { 953 int fd; 954 pool_state_t state; 955 char *name = NULL; 956 nvlist_t *label; 957 uint64_t guid, spareguid; 958 nvlist_t *nvroot; 959 nvlist_t **spares; 960 uint_t i, nspares; 961 boolean_t inuse; 962 963 if ((fd = open(path, O_RDONLY)) < 0) 964 return (B_FALSE); 965 966 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 || 967 !inuse || 968 state != POOL_STATE_SPARE || 969 zpool_read_label(fd, &label) != 0) { 970 free(name); 971 (void) close(fd); 972 return (B_FALSE); 973 } 974 free(name); 975 976 (void) close(fd); 977 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0); 978 nvlist_free(label); 979 980 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 981 &nvroot) == 0); 982 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 983 &spares, &nspares) == 0) { 984 for (i = 0; i < nspares; i++) { 985 verify(nvlist_lookup_uint64(spares[i], 986 ZPOOL_CONFIG_GUID, &spareguid) == 0); 987 if (spareguid == guid) 988 return (B_TRUE); 989 } 990 } 991 992 return (B_FALSE); 993 } 994 995 /* 996 * Go through and find any devices that are in use. We rely on libdiskmgt for 997 * the majority of this task. 998 */ 999 static int 1000 check_in_use(nvlist_t *config, nvlist_t *nv, int force, int isreplacing, 1001 int isspare) 1002 { 1003 nvlist_t **child; 1004 uint_t c, children; 1005 char *type, *path; 1006 int ret; 1007 char buf[MAXPATHLEN]; 1008 uint64_t wholedisk; 1009 1010 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 1011 1012 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1013 &child, &children) != 0) { 1014 1015 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 1016 1017 /* 1018 * As a generic check, we look to see if this is a replace of a 1019 * hot spare within the same pool. If so, we allow it 1020 * regardless of what libdiskmgt or zpool_in_use() says. 1021 */ 1022 if (isreplacing) { 1023 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1024 &wholedisk) == 0 && wholedisk) 1025 (void) snprintf(buf, sizeof (buf), "%ss0", 1026 path); 1027 else 1028 (void) strlcpy(buf, path, sizeof (buf)); 1029 if (is_spare(config, buf)) 1030 return (0); 1031 } 1032 1033 if (strcmp(type, VDEV_TYPE_DISK) == 0) 1034 ret = check_device(path, force, isspare); 1035 1036 if (strcmp(type, VDEV_TYPE_FILE) == 0) 1037 ret = check_file(path, force, isspare); 1038 1039 return (ret); 1040 } 1041 1042 for (c = 0; c < children; c++) 1043 if ((ret = check_in_use(config, child[c], force, 1044 isreplacing, B_FALSE)) != 0) 1045 return (ret); 1046 1047 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1048 &child, &children) == 0) 1049 for (c = 0; c < children; c++) 1050 if ((ret = check_in_use(config, child[c], force, 1051 isreplacing, B_TRUE)) != 0) 1052 return (ret); 1053 1054 return (0); 1055 } 1056 1057 static const char * 1058 is_grouping(const char *type, int *mindev) 1059 { 1060 if (strcmp(type, "raidz") == 0 || strcmp(type, "raidz1") == 0) { 1061 if (mindev != NULL) 1062 *mindev = 2; 1063 return (VDEV_TYPE_RAIDZ); 1064 } 1065 1066 if (strcmp(type, "raidz2") == 0) { 1067 if (mindev != NULL) 1068 *mindev = 3; 1069 return (VDEV_TYPE_RAIDZ); 1070 } 1071 1072 if (strcmp(type, "mirror") == 0) { 1073 if (mindev != NULL) 1074 *mindev = 2; 1075 return (VDEV_TYPE_MIRROR); 1076 } 1077 1078 if (strcmp(type, "spare") == 0) { 1079 if (mindev != NULL) 1080 *mindev = 1; 1081 return (VDEV_TYPE_SPARE); 1082 } 1083 1084 return (NULL); 1085 } 1086 1087 /* 1088 * Construct a syntactically valid vdev specification, 1089 * and ensure that all devices and files exist and can be opened. 1090 * Note: we don't bother freeing anything in the error paths 1091 * because the program is just going to exit anyway. 1092 */ 1093 nvlist_t * 1094 construct_spec(int argc, char **argv) 1095 { 1096 nvlist_t *nvroot, *nv, **top, **spares; 1097 int t, toplevels, mindev, nspares; 1098 const char *type; 1099 1100 top = NULL; 1101 toplevels = 0; 1102 spares = NULL; 1103 nspares = 0; 1104 1105 while (argc > 0) { 1106 nv = NULL; 1107 1108 /* 1109 * If it's a mirror or raidz, the subsequent arguments are 1110 * its leaves -- until we encounter the next mirror or raidz. 1111 */ 1112 if ((type = is_grouping(argv[0], &mindev)) != NULL) { 1113 nvlist_t **child = NULL; 1114 int c, children = 0; 1115 1116 if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 1117 spares != NULL) { 1118 (void) fprintf(stderr, gettext("invalid vdev " 1119 "specification: 'spare' can be " 1120 "specified only once\n")); 1121 return (NULL); 1122 } 1123 1124 for (c = 1; c < argc; c++) { 1125 if (is_grouping(argv[c], NULL) != NULL) 1126 break; 1127 children++; 1128 child = realloc(child, 1129 children * sizeof (nvlist_t *)); 1130 if (child == NULL) 1131 zpool_no_memory(); 1132 if ((nv = make_leaf_vdev(argv[c])) == NULL) 1133 return (NULL); 1134 child[children - 1] = nv; 1135 } 1136 1137 if (children < mindev) { 1138 (void) fprintf(stderr, gettext("invalid vdev " 1139 "specification: %s requires at least %d " 1140 "devices\n"), argv[0], mindev); 1141 return (NULL); 1142 } 1143 1144 argc -= c; 1145 argv += c; 1146 1147 if (strcmp(type, VDEV_TYPE_SPARE) == 0) { 1148 spares = child; 1149 nspares = children; 1150 continue; 1151 } else { 1152 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME, 1153 0) == 0); 1154 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 1155 type) == 0); 1156 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) { 1157 verify(nvlist_add_uint64(nv, 1158 ZPOOL_CONFIG_NPARITY, 1159 mindev - 1) == 0); 1160 } 1161 verify(nvlist_add_nvlist_array(nv, 1162 ZPOOL_CONFIG_CHILDREN, child, 1163 children) == 0); 1164 1165 for (c = 0; c < children; c++) 1166 nvlist_free(child[c]); 1167 free(child); 1168 } 1169 } else { 1170 /* 1171 * We have a device. Pass off to make_leaf_vdev() to 1172 * construct the appropriate nvlist describing the vdev. 1173 */ 1174 if ((nv = make_leaf_vdev(argv[0])) == NULL) 1175 return (NULL); 1176 argc--; 1177 argv++; 1178 } 1179 1180 toplevels++; 1181 top = realloc(top, toplevels * sizeof (nvlist_t *)); 1182 if (top == NULL) 1183 zpool_no_memory(); 1184 top[toplevels - 1] = nv; 1185 } 1186 1187 if (toplevels == 0 && nspares == 0) { 1188 (void) fprintf(stderr, gettext("invalid vdev " 1189 "specification: at least one toplevel vdev must be " 1190 "specified\n")); 1191 return (NULL); 1192 } 1193 1194 /* 1195 * Finally, create nvroot and add all top-level vdevs to it. 1196 */ 1197 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0); 1198 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 1199 VDEV_TYPE_ROOT) == 0); 1200 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1201 top, toplevels) == 0); 1202 if (nspares != 0) 1203 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1204 spares, nspares) == 0); 1205 1206 for (t = 0; t < toplevels; t++) 1207 nvlist_free(top[t]); 1208 for (t = 0; t < nspares; t++) 1209 nvlist_free(spares[t]); 1210 if (spares) 1211 free(spares); 1212 free(top); 1213 1214 return (nvroot); 1215 } 1216 1217 1218 /* 1219 * Get and validate the contents of the given vdev specification. This ensures 1220 * that the nvlist returned is well-formed, that all the devices exist, and that 1221 * they are not currently in use by any other known consumer. The 'poolconfig' 1222 * parameter is the current configuration of the pool when adding devices 1223 * existing pool, and is used to perform additional checks, such as changing the 1224 * replication level of the pool. It can be 'NULL' to indicate that this is a 1225 * new pool. The 'force' flag controls whether devices should be forcefully 1226 * added, even if they appear in use. 1227 */ 1228 nvlist_t * 1229 make_root_vdev(zpool_handle_t *zhp, int force, int check_rep, 1230 boolean_t isreplacing, int argc, char **argv) 1231 { 1232 nvlist_t *newroot; 1233 nvlist_t *poolconfig = NULL; 1234 is_force = force; 1235 1236 /* 1237 * Construct the vdev specification. If this is successful, we know 1238 * that we have a valid specification, and that all devices can be 1239 * opened. 1240 */ 1241 if ((newroot = construct_spec(argc, argv)) == NULL) 1242 return (NULL); 1243 1244 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL)) 1245 return (NULL); 1246 1247 /* 1248 * Validate each device to make sure that its not shared with another 1249 * subsystem. We do this even if 'force' is set, because there are some 1250 * uses (such as a dedicated dump device) that even '-f' cannot 1251 * override. 1252 */ 1253 if (check_in_use(poolconfig, newroot, force, isreplacing, 1254 B_FALSE) != 0) { 1255 nvlist_free(newroot); 1256 return (NULL); 1257 } 1258 1259 /* 1260 * Check the replication level of the given vdevs and report any errors 1261 * found. We include the existing pool spec, if any, as we need to 1262 * catch changes against the existing replication level. 1263 */ 1264 if (check_rep && check_replication(poolconfig, newroot) != 0) { 1265 nvlist_free(newroot); 1266 return (NULL); 1267 } 1268 1269 /* 1270 * Run through the vdev specification and label any whole disks found. 1271 */ 1272 if (make_disks(zhp, newroot) != 0) { 1273 nvlist_free(newroot); 1274 return (NULL); 1275 } 1276 1277 return (newroot); 1278 } 1279