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