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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 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=(...) 39 * mirror=(...) 40 * 41 * While the underlying implementation supports it, group vdevs cannot contain 42 * other group vdevs. All userland verification of devices is contained within 43 * this file. If successful, the nvlist returned can be passed directly to the 44 * kernel; we've done as much verification as possible in userland. 45 * 46 * The only function exported by this file is 'get_vdev_spec'. The function 47 * performs several passes: 48 * 49 * 1. Construct the vdev specification. Performs syntax validation and 50 * makes sure each device is valid. 51 * 2. Check for devices in use. Using libdiskmgt, makes sure that no 52 * devices are also in use. Some can be overridden using the 'force' 53 * flag, others cannot. 54 * 3. Check for replication errors if the 'force' flag is not specified. 55 * validates that the replication level is consistent across the 56 * entire pool. 57 * 4. Label any whole disks with an EFI label. 58 */ 59 60 #include <assert.h> 61 #include <devid.h> 62 #include <errno.h> 63 #include <fcntl.h> 64 #include <libdiskmgt.h> 65 #include <libintl.h> 66 #include <libnvpair.h> 67 #include <stdio.h> 68 #include <string.h> 69 #include <unistd.h> 70 #include <sys/efi_partition.h> 71 #include <sys/stat.h> 72 #include <sys/vtoc.h> 73 #include <sys/mntent.h> 74 75 #include <libzfs.h> 76 77 #include "zpool_util.h" 78 79 #define DISK_ROOT "/dev/dsk" 80 #define RDISK_ROOT "/dev/rdsk" 81 #define BACKUP_SLICE "s2" 82 83 /* 84 * For any given vdev specification, we can have multiple errors. The 85 * vdev_error() function keeps track of whether we have seen an error yet, and 86 * prints out a header if its the first error we've seen. 87 */ 88 int error_seen; 89 int is_force; 90 91 void 92 vdev_error(const char *fmt, ...) 93 { 94 va_list ap; 95 96 if (!error_seen) { 97 (void) fprintf(stderr, gettext("invalid vdev specification\n")); 98 if (!is_force) 99 (void) fprintf(stderr, gettext("use '-f' to override " 100 "the following errors:\n")); 101 else 102 (void) fprintf(stderr, gettext("the following errors " 103 "must be manually repaired:\n")); 104 error_seen = TRUE; 105 } 106 107 va_start(ap, fmt); 108 (void) vfprintf(stderr, fmt, ap); 109 va_end(ap); 110 } 111 112 void 113 _libdskmgt_error(int err, const char *file, int line) 114 { 115 if (err == 0) 116 no_memory(); 117 118 /* 119 * Some of the libdiskmgt stuff requires root privileges in order to 120 * examine devices. Bail out gracefully in this case. 121 */ 122 if (err == EACCES) { 123 (void) fprintf(stderr, gettext("cannot determine disk " 124 "configuration: permission denied\n")); 125 exit(1); 126 } 127 128 (void) fprintf(stderr, gettext("internal error: disk configuration " 129 "error %d at line %d of file %s\n"), err, line, file); 130 abort(); 131 } 132 133 #define libdskmgt_error(err) (_libdskmgt_error((err), __FILE__, __LINE__)) 134 135 /* 136 * Checks whether a single slice overlaps with any of the slices in the provided 137 * list. Called by check_overlapping(). 138 */ 139 int 140 is_overlapping(dm_descriptor_t slice, dm_descriptor_t media, 141 dm_descriptor_t *slice_list, int *error, char **overlaps_with) 142 { 143 int i = 0; 144 uint32_t in_snum; 145 uint64_t start_block = 0; 146 uint64_t end_block = 0; 147 uint64_t media_size = 0; 148 uint64_t size = 0; 149 nvlist_t *media_attrs; 150 nvlist_t *slice_attrs; 151 152 media_attrs = dm_get_attributes(media, error); 153 if (*error != 0) { 154 return (-1); 155 } 156 157 if (media_attrs == NULL) { 158 return (0); 159 } 160 161 *error = nvlist_lookup_uint64(media_attrs, DM_NACCESSIBLE, &media_size); 162 if (*error != 0) { 163 nvlist_free(media_attrs); 164 return (-1); 165 } 166 167 slice_attrs = dm_get_attributes(slice, error); 168 if (*error != 0) { 169 nvlist_free(media_attrs); 170 return (-1); 171 } 172 /* 173 * Not really possible, but the error above would catch any system 174 * errors. 175 */ 176 if (slice_attrs == NULL) { 177 nvlist_free(media_attrs); 178 return (0); 179 } 180 181 *error = nvlist_lookup_uint64(slice_attrs, DM_START, &start_block); 182 if (*error != 0) { 183 nvlist_free(media_attrs); 184 nvlist_free(slice_attrs); 185 return (-1); 186 } 187 188 *error = nvlist_lookup_uint64(slice_attrs, DM_SIZE, &size); 189 if (*error != 0) { 190 nvlist_free(media_attrs); 191 nvlist_free(slice_attrs); 192 return (-1); 193 } 194 *error = nvlist_lookup_uint32(slice_attrs, DM_INDEX, &in_snum); 195 if (*error != 0) { 196 nvlist_free(media_attrs); 197 nvlist_free(slice_attrs); 198 return (-1); 199 } 200 201 end_block = (start_block + size) - 1; 202 203 for (i = 0; slice_list[i]; i ++) { 204 uint64_t other_start; 205 uint64_t other_end; 206 uint64_t other_size; 207 uint32_t snum; 208 209 nvlist_t *other_attrs = dm_get_attributes(slice_list[i], error); 210 if (*error != 0) { 211 return (-1); 212 } 213 214 if (other_attrs == NULL) 215 continue; 216 217 *error = nvlist_lookup_uint64(other_attrs, DM_START, 218 &other_start); 219 if (*error) { 220 nvlist_free(media_attrs); 221 nvlist_free(slice_attrs); 222 nvlist_free(other_attrs); 223 return (-1); 224 } 225 226 *error = nvlist_lookup_uint64(other_attrs, DM_SIZE, 227 &other_size); 228 229 if (*error) { 230 nvlist_free(media_attrs); 231 nvlist_free(slice_attrs); 232 nvlist_free(other_attrs); 233 return (-1); 234 } 235 236 other_end = (other_size + other_start) - 1; 237 238 *error = nvlist_lookup_uint32(other_attrs, DM_INDEX, 239 &snum); 240 241 if (*error) { 242 nvlist_free(media_attrs); 243 nvlist_free(slice_attrs); 244 nvlist_free(other_attrs); 245 return (-1); 246 } 247 248 /* 249 * Check to see if there are > 2 overlapping regions 250 * on this media in the same region as this slice. 251 * This is done by assuming the following: 252 * Slice 2 is the backup slice if it is the size 253 * of the whole disk 254 * If slice 2 is the overlap and slice 2 is the size of 255 * the whole disk, continue. If another slice is found 256 * that overlaps with our slice, return it. 257 * There is the potential that there is more than one slice 258 * that our slice overlaps with, however, we only return 259 * the first overlapping slice we find. 260 * 261 */ 262 263 if (start_block >= other_start && start_block <= other_end) { 264 if ((snum == 2 && (other_size == media_size)) || 265 snum == in_snum) { 266 continue; 267 } else { 268 char *str = dm_get_name(slice_list[i], error); 269 if (*error != 0) { 270 nvlist_free(media_attrs); 271 nvlist_free(slice_attrs); 272 nvlist_free(other_attrs); 273 return (-1); 274 } 275 *overlaps_with = strdup(str); 276 dm_free_name(str); 277 nvlist_free(media_attrs); 278 nvlist_free(slice_attrs); 279 nvlist_free(other_attrs); 280 return (1); 281 } 282 } else if (other_start >= start_block && 283 other_start <= end_block) { 284 if ((snum == 2 && (other_size == media_size)) || 285 snum == in_snum) { 286 continue; 287 } else { 288 char *str = dm_get_name(slice_list[i], error); 289 if (*error != 0) { 290 nvlist_free(media_attrs); 291 nvlist_free(slice_attrs); 292 nvlist_free(other_attrs); 293 return (-1); 294 } 295 *overlaps_with = strdup(str); 296 dm_free_name(str); 297 nvlist_free(media_attrs); 298 nvlist_free(slice_attrs); 299 nvlist_free(other_attrs); 300 return (1); 301 } 302 } 303 nvlist_free(other_attrs); 304 } 305 nvlist_free(media_attrs); 306 nvlist_free(slice_attrs); 307 return (0); 308 } 309 310 /* 311 * Check to see whether the given slice overlaps with any other slices. Get the 312 * associated slice information and pass on to is_overlapping(). 313 */ 314 int 315 check_overlapping(const char *slicename, dm_descriptor_t slice) 316 { 317 dm_descriptor_t *media; 318 dm_descriptor_t *slices; 319 int error; 320 char *overlaps; 321 int ret = 0; 322 323 /* 324 * Get the list of slices be fetching the associated media, and then all 325 * associated slices. 326 */ 327 media = dm_get_associated_descriptors(slice, DM_MEDIA, &error); 328 if (media == NULL || *media == NULL || error != 0) 329 libdskmgt_error(error); 330 331 slices = dm_get_associated_descriptors(*media, DM_SLICE, &error); 332 if (slices == NULL || *slices == NULL || error != 0) 333 libdskmgt_error(error); 334 335 336 overlaps = NULL; 337 if (is_overlapping(slice, *media, slices, &error, &overlaps)) { 338 vdev_error(gettext("device '%s' overlaps with '%s'\n"), 339 slicename, overlaps); 340 ret = -1; 341 } 342 343 if (overlaps != NULL) 344 free(overlaps); 345 dm_free_descriptors(slices); 346 dm_free_descriptors(media); 347 348 return (ret); 349 } 350 351 /* 352 * Validate the given slice. If 'diskname' is non-NULL, then this is a single 353 * slice on a complete disk. If 'force' is set, then the user specified '-f' 354 * and we only want to report error for completely forbidden uses. 355 */ 356 int 357 check_slice(const char *slicename, dm_descriptor_t slice, int force, 358 int overlap) 359 { 360 nvlist_t *stats; 361 int err; 362 nvpair_t *nvwhat, *nvdesc; 363 char *what, *desc, *name; 364 int found = FALSE; 365 int found_zfs = FALSE; 366 int fd; 367 368 if ((stats = dm_get_stats(slice, DM_SLICE_STAT_USE, &err)) == NULL) 369 libdskmgt_error(err); 370 371 /* 372 * Always check to see if this is used by an active ZFS pool. 373 */ 374 if ((fd = open(slicename, O_RDONLY)) > 0) { 375 if (zpool_in_use(fd, &desc, &name)) { 376 377 if (!force) { 378 vdev_error(gettext("%s is part of %s pool " 379 "'%s'\n"), slicename, desc, name); 380 found = found_zfs = TRUE; 381 } 382 383 free(desc); 384 free(name); 385 } 386 387 (void) close(fd); 388 } 389 390 /* 391 * This slice is in use. Print out a descriptive message describing who 392 * is using it. The 'used_by' nvlist is formatted as: 393 * 394 * (used_by=what, used_name=desc, ...) 395 * 396 * Each 'used_by' must be accompanied by a 'used_name'. 397 */ 398 nvdesc = NULL; 399 for (;;) { 400 nvwhat = nvlist_next_nvpair(stats, nvdesc); 401 nvdesc = nvlist_next_nvpair(stats, nvwhat); 402 403 if (nvwhat == NULL || nvdesc == NULL) 404 break; 405 406 assert(strcmp(nvpair_name(nvwhat), DM_USED_BY) == 0); 407 assert(strcmp(nvpair_name(nvdesc), DM_USED_NAME) == 0); 408 409 verify(nvpair_value_string(nvwhat, &what) == 0); 410 verify(nvpair_value_string(nvdesc, &desc) == 0); 411 412 /* 413 * For currently mounted filesystems, filesystems in 414 * /etc/vfstab, or dedicated dump devices, we can never use 415 * them, even if '-f' is specified. The rest of the errors 416 * indicate that a filesystem was detected on disk, which can be 417 * overridden with '-f'. 418 */ 419 if (strcmp(what, DM_USE_MOUNT) == 0 || 420 strcmp(what, DM_USE_VFSTAB) == 0 || 421 strcmp(what, DM_USE_DUMP) == 0) { 422 found = TRUE; 423 if (strcmp(what, DM_USE_MOUNT) == 0) { 424 vdev_error(gettext("%s is " 425 "currently mounted on %s\n"), 426 slicename, desc); 427 } else if (strcmp(what, DM_USE_VFSTAB) == 0) { 428 vdev_error(gettext("%s is usually " 429 "mounted at %s in /etc/vfstab\n"), 430 slicename, desc); 431 } else if (strcmp(what, DM_USE_DUMP) == 0) { 432 vdev_error(gettext("%s is the " 433 "dedicated dump device\n"), slicename); 434 } 435 } else if (!force) { 436 found = TRUE; 437 if (strcmp(what, DM_USE_SVM) == 0) { 438 vdev_error(gettext("%s is part of " 439 "SVM volume %s\n"), slicename, desc); 440 } else if (strcmp(what, DM_USE_LU) == 0) { 441 vdev_error(gettext("%s is in use " 442 "for live upgrade %s\n"), slicename, desc); 443 } else if (strcmp(what, DM_USE_VXVM) == 0) { 444 vdev_error(gettext("%s is part of " 445 "VxVM volume %s\n"), slicename, desc); 446 } else if (strcmp(what, DM_USE_FS) == 0) { 447 /* 448 * We should have already caught ZFS in-use 449 * filesystems above. If the ZFS version is 450 * different, or there was some other critical 451 * failure, it's possible for fstyp to report it 452 * as in-use, but zpool_open_by_dev() to fail. 453 */ 454 if (strcmp(desc, MNTTYPE_ZFS) != 0) 455 vdev_error(gettext("%s contains a %s " 456 "filesystem\n"), slicename, desc); 457 else if (!found_zfs) 458 vdev_error(gettext("%s is part of an " 459 "outdated or damaged ZFS " 460 "pool\n"), slicename); 461 } else { 462 vdev_error(gettext("is used by %s as %s\n"), 463 slicename, what, desc); 464 } 465 } else { 466 found = FALSE; 467 } 468 } 469 470 /* 471 * Perform any overlap checking if requested to do so. 472 */ 473 if (overlap && !force) 474 found |= (check_overlapping(slicename, slice) != 0); 475 476 return (found ? -1 : 0); 477 } 478 479 /* 480 * Validate a whole disk. Iterate over all slices on the disk and make sure 481 * that none is in use by calling check_slice(). 482 */ 483 /* ARGSUSED */ 484 int 485 check_disk(const char *name, dm_descriptor_t disk, int force) 486 { 487 dm_descriptor_t *drive, *media, *slice; 488 int err = 0; 489 int i; 490 int ret; 491 492 /* 493 * Get the drive associated with this disk. This should never fail, 494 * because we already have an alias handle open for the device. 495 */ 496 if ((drive = dm_get_associated_descriptors(disk, DM_DRIVE, 497 &err)) == NULL || *drive == NULL) 498 libdskmgt_error(err); 499 500 if ((media = dm_get_associated_descriptors(*drive, DM_MEDIA, 501 &err)) == NULL) 502 libdskmgt_error(err); 503 504 dm_free_descriptors(drive); 505 506 /* 507 * It is possible that the user has specified a removable media drive, 508 * and the media is not present. 509 */ 510 if (*media == NULL) { 511 vdev_error(gettext("'%s' has no media in drive\n"), name); 512 dm_free_descriptors(media); 513 return (-1); 514 } 515 516 if ((slice = dm_get_associated_descriptors(*media, DM_SLICE, 517 &err)) == NULL) 518 libdskmgt_error(err); 519 520 dm_free_descriptors(media); 521 522 ret = 0; 523 524 /* 525 * Iterate over all slices and report any errors. We don't care about 526 * overlapping slices because we are using the whole disk. 527 */ 528 for (i = 0; slice[i] != NULL; i++) { 529 if (check_slice(dm_get_name(slice[i], &err), slice[i], 530 force, FALSE) != 0) 531 ret = -1; 532 } 533 534 dm_free_descriptors(slice); 535 return (ret); 536 } 537 538 539 /* 540 * Validate a device. Determines whether the device is a disk, slice, or 541 * partition, and passes it off to an appropriate function. 542 */ 543 int 544 check_device(const char *path, int force) 545 { 546 dm_descriptor_t desc; 547 int err; 548 char *dev, rpath[MAXPATHLEN]; 549 550 /* 551 * For whole disks, libdiskmgt does not include the leading dev path. 552 */ 553 dev = strrchr(path, '/'); 554 assert(dev != NULL); 555 dev++; 556 if ((desc = dm_get_descriptor_by_name(DM_ALIAS, dev, &err)) != NULL) 557 return (check_disk(path, desc, force)); 558 559 /* 560 * If 'err' is not ENODEV, then we've had an unexpected error from 561 * libdiskmgt. The only explanation is that we ran out of memory. 562 */ 563 if (err != ENODEV) 564 libdskmgt_error(err); 565 566 /* 567 * Determine if this is a slice. 568 */ 569 if ((desc = dm_get_descriptor_by_name(DM_SLICE, (char *)path, &err)) 570 != NULL) 571 return (check_slice(path, desc, force, TRUE)); 572 573 if (err != ENODEV) 574 libdskmgt_error(err); 575 576 /* 577 * Check for a partition. libdiskmgt expects path of /dev/rdsk when 578 * dealing with partitions, so convert it. 579 */ 580 (void) snprintf(rpath, sizeof (rpath), "/dev/rdsk/%s", dev); 581 if ((desc = dm_get_descriptor_by_name(DM_PARTITION, rpath, &err)) 582 != NULL) { 583 /* XXZFS perform checking on partitions */ 584 return (0); 585 } 586 587 if (err != ENODEV) 588 libdskmgt_error(err); 589 590 /* 591 * At this point, libdiskmgt failed to find the device as either a whole 592 * disk or a slice. Ignore these errors, as we know that it at least a 593 * block device. The user may have provided us with some unknown device 594 * that libdiskmgt doesn't know about. 595 */ 596 return (0); 597 } 598 599 /* 600 * Check that a file is valid. All we can do in this case is check that it's 601 * not in use by another pool. 602 */ 603 int 604 check_file(const char *file, int force) 605 { 606 char *desc, *name; 607 int fd; 608 int ret = 0; 609 610 if ((fd = open(file, O_RDONLY)) < 0) 611 return (0); 612 613 if (zpool_in_use(fd, &desc, &name)) { 614 if (strcmp(desc, gettext("active")) == 0 || 615 !force) { 616 vdev_error(gettext("%s is part of %s pool '%s'\n"), 617 file, desc, name); 618 ret = -1; 619 } 620 621 free(desc); 622 free(name); 623 } 624 625 (void) close(fd); 626 return (ret); 627 } 628 629 static int 630 is_whole_disk(const char *arg, struct stat64 *statbuf) 631 { 632 char path[MAXPATHLEN]; 633 634 (void) snprintf(path, sizeof (path), "%s%s", arg, BACKUP_SLICE); 635 if (stat64(path, statbuf) == 0) 636 return (TRUE); 637 638 return (FALSE); 639 } 640 641 /* 642 * Create a leaf vdev. Determine if this is a file or a device. If it's a 643 * device, fill in the device id to make a complete nvlist. Valid forms for a 644 * leaf vdev are: 645 * 646 * /dev/dsk/xxx Complete disk path 647 * /xxx Full path to file 648 * xxx Shorthand for /dev/dsk/xxx 649 */ 650 nvlist_t * 651 make_leaf_vdev(const char *arg) 652 { 653 char path[MAXPATHLEN]; 654 struct stat64 statbuf; 655 nvlist_t *vdev = NULL; 656 char *type = NULL; 657 int wholedisk = FALSE; 658 659 /* 660 * Determine what type of vdev this is, and put the full path into 661 * 'path'. We detect whether this is a device of file afterwards by 662 * checking the st_mode of the file. 663 */ 664 if (arg[0] == '/') { 665 /* 666 * Complete device or file path. Exact type is determined by 667 * examining the file descriptor afterwards. 668 */ 669 if (is_whole_disk(arg, &statbuf)) { 670 wholedisk = TRUE; 671 } else if (stat64(arg, &statbuf) != 0) { 672 (void) fprintf(stderr, 673 gettext("cannot open '%s': %s\n"), 674 arg, strerror(errno)); 675 return (NULL); 676 } 677 678 (void) strlcpy(path, arg, sizeof (path)); 679 } else { 680 /* 681 * This may be a short path for a device, or it could be total 682 * gibberish. Check to see if it's a known device in 683 * /dev/dsk/. As part of this check, see if we've been given a 684 * an entire disk (minus the slice number). 685 */ 686 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, 687 arg); 688 if (is_whole_disk(path, &statbuf)) { 689 wholedisk = TRUE; 690 } else if (stat64(path, &statbuf) != 0) { 691 /* 692 * If we got ENOENT, then the user gave us 693 * gibberish, so try to direct them with a 694 * reasonable error message. Otherwise, 695 * regurgitate strerror() since it's the best we 696 * can do. 697 */ 698 if (errno == ENOENT) { 699 (void) fprintf(stderr, 700 gettext("cannot open '%s': no such " 701 "device in %s\n"), arg, DISK_ROOT); 702 (void) fprintf(stderr, 703 gettext("must be a full path or " 704 "shorthand device name\n")); 705 return (NULL); 706 } else { 707 (void) fprintf(stderr, 708 gettext("cannot open '%s': %s\n"), 709 path, strerror(errno)); 710 return (NULL); 711 } 712 } 713 } 714 715 /* 716 * Determine whether this is a device or a file. 717 */ 718 if (S_ISBLK(statbuf.st_mode)) { 719 type = VDEV_TYPE_DISK; 720 } else if (S_ISREG(statbuf.st_mode)) { 721 type = VDEV_TYPE_FILE; 722 } else { 723 (void) fprintf(stderr, gettext("cannot use '%s': must be a " 724 "block device or regular file\n"), path); 725 return (NULL); 726 } 727 728 /* 729 * Finally, we have the complete device or file, and we know that it is 730 * acceptable to use. Construct the nvlist to describe this vdev. All 731 * vdevs have a 'path' element, and devices also have a 'devid' element. 732 */ 733 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0); 734 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0); 735 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0); 736 if (strcmp(type, VDEV_TYPE_DISK) == 0) 737 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK, 738 (uint64_t)wholedisk) == 0); 739 740 /* 741 * For a whole disk, defer getting its devid until after labeling it. 742 */ 743 if (S_ISBLK(statbuf.st_mode) && !wholedisk) { 744 /* 745 * Get the devid for the device. 746 */ 747 int fd; 748 ddi_devid_t devid; 749 char *minor = NULL, *devid_str = NULL; 750 751 if ((fd = open(path, O_RDONLY)) < 0) { 752 (void) fprintf(stderr, gettext("cannot open '%s': " 753 "%s\n"), path, strerror(errno)); 754 nvlist_free(vdev); 755 return (NULL); 756 } 757 758 if (devid_get(fd, &devid) == 0) { 759 if (devid_get_minor_name(fd, &minor) == 0 && 760 (devid_str = devid_str_encode(devid, minor)) != 761 NULL) { 762 verify(nvlist_add_string(vdev, 763 ZPOOL_CONFIG_DEVID, devid_str) == 0); 764 } 765 if (devid_str != NULL) 766 devid_str_free(devid_str); 767 if (minor != NULL) 768 devid_str_free(minor); 769 devid_free(devid); 770 } 771 772 (void) close(fd); 773 } 774 775 return (vdev); 776 } 777 778 /* 779 * Go through and verify the replication level of the pool is consistent. 780 * Performs the following checks: 781 * 782 * For the new spec, verifies that devices in mirrors and raidz are the 783 * same size. 784 * 785 * If the current configuration already has inconsistent replication 786 * levels, ignore any other potential problems in the new spec. 787 * 788 * Otherwise, make sure that the current spec (if there is one) and the new 789 * spec have consistent replication levels. 790 */ 791 typedef struct replication_level { 792 char *type; 793 int level; 794 } replication_level_t; 795 796 /* 797 * Given a list of toplevel vdevs, return the current replication level. If 798 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then 799 * an error message will be displayed for each self-inconsistent vdev. 800 */ 801 replication_level_t * 802 get_replication(nvlist_t *nvroot, int fatal) 803 { 804 nvlist_t **top; 805 uint_t t, toplevels; 806 nvlist_t **child; 807 uint_t c, children; 808 nvlist_t *nv; 809 char *type; 810 replication_level_t lastrep, rep, *ret; 811 int dontreport; 812 813 ret = safe_malloc(sizeof (replication_level_t)); 814 815 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 816 &top, &toplevels) == 0); 817 818 lastrep.type = NULL; 819 for (t = 0; t < toplevels; t++) { 820 nv = top[t]; 821 822 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 823 824 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 825 &child, &children) != 0) { 826 /* 827 * This is a 'file' or 'disk' vdev. 828 */ 829 rep.type = type; 830 rep.level = 1; 831 } else { 832 uint64_t vdev_size; 833 834 /* 835 * This is a mirror or RAID-Z vdev. Go through and make 836 * sure the contents are all the same (files vs. disks), 837 * keeping track of the number of elements in the 838 * process. 839 * 840 * We also check that the size of each vdev (if it can 841 * be determined) is the same. 842 */ 843 rep.type = type; 844 rep.level = 0; 845 846 /* 847 * The 'dontreport' variable indicatest that we've 848 * already reported an error for this spec, so don't 849 * bother doing it again. 850 */ 851 type = NULL; 852 dontreport = 0; 853 vdev_size = -1ULL; 854 for (c = 0; c < children; c++) { 855 nvlist_t *cnv = child[c]; 856 char *path; 857 struct stat64 statbuf; 858 uint64_t size = -1ULL; 859 char *childtype; 860 int fd, err; 861 862 rep.level++; 863 864 verify(nvlist_lookup_string(cnv, 865 ZPOOL_CONFIG_TYPE, &childtype) == 0); 866 verify(nvlist_lookup_string(cnv, 867 ZPOOL_CONFIG_PATH, &path) == 0); 868 869 /* 870 * If we have a raidz/mirror that combines disks 871 * with files, report it as an error. 872 */ 873 if (!dontreport && type != NULL && 874 strcmp(type, childtype) != 0) { 875 if (ret != NULL) 876 free(ret); 877 ret = NULL; 878 if (fatal) 879 vdev_error(gettext( 880 "mismatched replication " 881 "level: %s contains both " 882 "files and devices\n"), 883 rep.type); 884 else 885 return (NULL); 886 dontreport = TRUE; 887 } 888 889 /* 890 * According to stat(2), the value of 'st_size' 891 * is undefined for block devices and character 892 * devices. But there is no effective way to 893 * determine the real size in userland. 894 * 895 * Instead, we'll take advantage of an 896 * implementation detail of spec_size(). If the 897 * device is currently open, then we (should) 898 * return a valid size. 899 * 900 * If we still don't get a valid size (indicated 901 * by a size of 0 or MAXOFFSET_T), then ignore 902 * this device altogether. 903 */ 904 if ((fd = open(path, O_RDONLY)) >= 0) { 905 err = fstat64(fd, &statbuf); 906 (void) close(fd); 907 } else { 908 err = stat64(path, &statbuf); 909 } 910 911 if (err != 0 || 912 statbuf.st_size == 0 || 913 statbuf.st_size == MAXOFFSET_T) 914 continue; 915 916 size = statbuf.st_size; 917 918 /* 919 * Also check the size of each device. If they 920 * differ, then report an error. 921 */ 922 if (!dontreport && vdev_size != -1ULL && 923 size != vdev_size) { 924 if (ret != NULL) 925 free(ret); 926 ret = NULL; 927 if (fatal) 928 vdev_error(gettext( 929 "%s contains devices of " 930 "different sizes\n"), 931 rep.type); 932 else 933 return (NULL); 934 dontreport = TRUE; 935 } 936 937 type = childtype; 938 vdev_size = size; 939 } 940 } 941 942 /* 943 * At this point, we have the replication of the last toplevel 944 * vdev in 'rep'. Compare it to 'lastrep' to see if its 945 * different. 946 */ 947 if (lastrep.type != NULL) { 948 if (strcmp(lastrep.type, rep.type) != 0) { 949 if (ret != NULL) 950 free(ret); 951 ret = NULL; 952 if (fatal) 953 vdev_error(gettext( 954 "mismatched replication " 955 "level: both %s and %s vdevs are " 956 "present\n"), 957 lastrep.type, rep.type); 958 else 959 return (NULL); 960 } else if (lastrep.level != rep.level) { 961 if (ret) 962 free(ret); 963 ret = NULL; 964 if (fatal) 965 vdev_error(gettext( 966 "mismatched replication " 967 "level: %d-way %s and %d-way %s " 968 "vdevs are present\n"), 969 lastrep.level, lastrep.type, 970 rep.level, rep.type); 971 else 972 return (NULL); 973 } 974 } 975 lastrep = rep; 976 } 977 978 if (ret != NULL) { 979 ret->type = rep.type; 980 ret->level = rep.level; 981 } 982 983 return (ret); 984 } 985 986 /* 987 * Check the replication level of the vdev spec against the current pool. Calls 988 * get_replication() to make sure the new spec is self-consistent. If the pool 989 * has a consistent replication level, then we ignore any errors. Otherwise, 990 * report any difference between the two. 991 */ 992 int 993 check_replication(nvlist_t *config, nvlist_t *newroot) 994 { 995 replication_level_t *current = NULL, *new; 996 int ret; 997 998 /* 999 * If we have a current pool configuration, check to see if it's 1000 * self-consistent. If not, simply return success. 1001 */ 1002 if (config != NULL) { 1003 nvlist_t *nvroot; 1004 1005 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1006 &nvroot) == 0); 1007 if ((current = get_replication(nvroot, FALSE)) == NULL) 1008 return (0); 1009 } 1010 1011 /* 1012 * Get the replication level of the new vdev spec, reporting any 1013 * inconsistencies found. 1014 */ 1015 if ((new = get_replication(newroot, TRUE)) == NULL) { 1016 free(current); 1017 return (-1); 1018 } 1019 1020 /* 1021 * Check to see if the new vdev spec matches the replication level of 1022 * the current pool. 1023 */ 1024 ret = 0; 1025 if (current != NULL) { 1026 if (strcmp(current->type, new->type) != 0 || 1027 current->level != new->level) { 1028 vdev_error(gettext( 1029 "mismatched replication level: pool uses %d-way %s " 1030 "and new vdev uses %d-way %s\n"), 1031 current->level, current->type, new->level, 1032 new->type); 1033 ret = -1; 1034 } 1035 } 1036 1037 free(new); 1038 if (current != NULL) 1039 free(current); 1040 1041 return (ret); 1042 } 1043 1044 /* 1045 * Label an individual disk. The name provided is the short name, stripped of 1046 * any leading /dev path. 1047 */ 1048 int 1049 label_disk(char *name) 1050 { 1051 char path[MAXPATHLEN]; 1052 struct dk_gpt *vtoc; 1053 int fd; 1054 size_t resv = 16384; 1055 1056 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 1057 BACKUP_SLICE); 1058 1059 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 1060 /* 1061 * This shouldn't happen. We've long since verified that this 1062 * is a valid device. 1063 */ 1064 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"), 1065 path, strerror(errno)); 1066 return (-1); 1067 } 1068 1069 1070 if (efi_alloc_and_init(fd, 9, &vtoc) != 0) { 1071 /* 1072 * The only way this can fail is if we run out of memory, or we 1073 * were unable to read the disk geometry. 1074 */ 1075 if (errno == ENOMEM) 1076 no_memory(); 1077 1078 (void) fprintf(stderr, gettext("cannot label '%s': unable to " 1079 "read disk geometry\n"), name); 1080 (void) close(fd); 1081 return (-1); 1082 } 1083 1084 vtoc->efi_parts[0].p_start = vtoc->efi_first_u_lba; 1085 vtoc->efi_parts[0].p_size = vtoc->efi_last_u_lba + 1 - 1086 vtoc->efi_first_u_lba - resv; 1087 1088 /* 1089 * Why we use V_USR: V_BACKUP confuses users, and is considered 1090 * disposable by some EFI utilities (since EFI doesn't have a backup 1091 * slice). V_UNASSIGNED is supposed to be used only for zero size 1092 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 1093 * etc. were all pretty specific. V_USR is as close to reality as we 1094 * can get, in the absence of V_OTHER. 1095 */ 1096 vtoc->efi_parts[0].p_tag = V_USR; 1097 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 1098 1099 vtoc->efi_parts[8].p_start = vtoc->efi_last_u_lba + 1 - resv; 1100 vtoc->efi_parts[8].p_size = resv; 1101 vtoc->efi_parts[8].p_tag = V_RESERVED; 1102 1103 if (efi_write(fd, vtoc) != 0) { 1104 /* 1105 * Currently, EFI labels are not supported for IDE disks, and it 1106 * is likely that they will not be supported on other drives for 1107 * some time. Print out a helpful error message directing the 1108 * user to manually label the disk and give a specific slice. 1109 */ 1110 (void) fprintf(stderr, gettext("cannot label '%s': failed to " 1111 "write EFI label\n"), name); 1112 (void) fprintf(stderr, gettext("use fdisk(1M) to partition " 1113 "the disk, and provide a specific slice\n")); 1114 (void) close(fd); 1115 return (-1); 1116 } 1117 1118 (void) close(fd); 1119 return (0); 1120 } 1121 1122 /* 1123 * Go through and find any whole disks in the vdev specification, labelling them 1124 * as appropriate. When constructing the vdev spec, we were unable to open this 1125 * device in order to provide a devid. Now that we have labelled the disk and 1126 * know that slice 0 is valid, we can construct the devid now. 1127 * 1128 * If the disk was already labelled with an EFI label, we will have gotten the 1129 * devid already (because we were able to open the whole disk). Otherwise, we 1130 * need to get the devid after we label the disk. 1131 */ 1132 int 1133 make_disks(nvlist_t *nv) 1134 { 1135 nvlist_t **child; 1136 uint_t c, children; 1137 char *type, *path, *diskname; 1138 char buf[MAXPATHLEN]; 1139 uint64_t wholedisk; 1140 int fd; 1141 int ret; 1142 ddi_devid_t devid; 1143 char *minor = NULL, *devid_str = NULL; 1144 1145 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 1146 1147 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1148 &child, &children) != 0) { 1149 1150 if (strcmp(type, VDEV_TYPE_DISK) != 0) 1151 return (0); 1152 1153 /* 1154 * We have a disk device. Get the path to the device 1155 * and see if its a whole disk by appending the backup 1156 * slice and stat()ing the device. 1157 */ 1158 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 1159 1160 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1161 &wholedisk) != 0 || !wholedisk) 1162 return (0); 1163 1164 diskname = strrchr(path, '/'); 1165 assert(diskname != NULL); 1166 diskname++; 1167 if (label_disk(diskname) != 0) 1168 return (-1); 1169 1170 /* 1171 * Fill in the devid, now that we've labeled the disk. 1172 */ 1173 (void) snprintf(buf, sizeof (buf), "%ss0", path); 1174 if ((fd = open(buf, O_RDONLY)) < 0) { 1175 (void) fprintf(stderr, 1176 gettext("cannot open '%s': %s\n"), 1177 buf, strerror(errno)); 1178 return (-1); 1179 } 1180 1181 if (devid_get(fd, &devid) == 0) { 1182 if (devid_get_minor_name(fd, &minor) == 0 && 1183 (devid_str = devid_str_encode(devid, minor)) != 1184 NULL) { 1185 verify(nvlist_add_string(nv, 1186 ZPOOL_CONFIG_DEVID, devid_str) == 0); 1187 } 1188 if (devid_str != NULL) 1189 devid_str_free(devid_str); 1190 if (minor != NULL) 1191 devid_str_free(minor); 1192 devid_free(devid); 1193 } 1194 1195 /* 1196 * Update the path to refer to the 's0' slice. The presence of 1197 * the 'whole_disk' field indicates to the CLI that we should 1198 * chop off the slice number when displaying the device in 1199 * future output. 1200 */ 1201 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, buf) == 0); 1202 1203 (void) close(fd); 1204 1205 return (0); 1206 } 1207 1208 for (c = 0; c < children; c++) 1209 if ((ret = make_disks(child[c])) != 0) 1210 return (ret); 1211 1212 return (0); 1213 } 1214 1215 /* 1216 * Go through and find any devices that are in use. We rely on libdiskmgt for 1217 * the majority of this task. 1218 */ 1219 int 1220 check_in_use(nvlist_t *nv, int force) 1221 { 1222 nvlist_t **child; 1223 uint_t c, children; 1224 char *type, *path; 1225 int ret; 1226 1227 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 1228 1229 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1230 &child, &children) != 0) { 1231 1232 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 1233 1234 if (strcmp(type, VDEV_TYPE_DISK) == 0) 1235 ret = check_device(path, force); 1236 1237 if (strcmp(type, VDEV_TYPE_FILE) == 0) 1238 ret = check_file(path, force); 1239 1240 return (ret); 1241 } 1242 1243 for (c = 0; c < children; c++) 1244 if ((ret = check_in_use(child[c], force)) != 0) 1245 return (ret); 1246 1247 return (0); 1248 } 1249 1250 /* 1251 * Construct a syntactically valid vdev specification, 1252 * and ensure that all devices and files exist and can be opened. 1253 * Note: we don't bother freeing anything in the error paths 1254 * because the program is just going to exit anyway. 1255 */ 1256 nvlist_t * 1257 construct_spec(int argc, char **argv) 1258 { 1259 nvlist_t *nvroot, *nv, **top; 1260 int t, toplevels; 1261 1262 top = NULL; 1263 toplevels = 0; 1264 1265 while (argc > 0) { 1266 nv = NULL; 1267 1268 /* 1269 * If it's a mirror or raidz, the subsequent arguments are 1270 * its leaves -- until we encounter the next mirror or raidz. 1271 */ 1272 if (strcmp(argv[0], VDEV_TYPE_MIRROR) == 0 || 1273 strcmp(argv[0], VDEV_TYPE_RAIDZ) == 0) { 1274 1275 char *type = argv[0]; 1276 nvlist_t **child = NULL; 1277 int children = 0; 1278 int c; 1279 1280 for (c = 1; c < argc; c++) { 1281 if (strcmp(argv[c], VDEV_TYPE_MIRROR) == 0 || 1282 strcmp(argv[c], VDEV_TYPE_RAIDZ) == 0) 1283 break; 1284 children++; 1285 child = realloc(child, 1286 children * sizeof (nvlist_t *)); 1287 if (child == NULL) 1288 no_memory(); 1289 if ((nv = make_leaf_vdev(argv[c])) == NULL) 1290 return (NULL); 1291 child[children - 1] = nv; 1292 } 1293 1294 argc -= c; 1295 argv += c; 1296 1297 /* 1298 * Mirrors and RAID-Z devices require at least 1299 * two components. 1300 */ 1301 if (children < 2) { 1302 (void) fprintf(stderr, 1303 gettext("invalid vdev specification: " 1304 "%s requires at least 2 devices\n"), type); 1305 return (NULL); 1306 } 1307 1308 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) == 0); 1309 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 1310 type) == 0); 1311 verify(nvlist_add_nvlist_array(nv, 1312 ZPOOL_CONFIG_CHILDREN, child, children) == 0); 1313 1314 for (c = 0; c < children; c++) 1315 nvlist_free(child[c]); 1316 free(child); 1317 } else { 1318 /* 1319 * We have a device. Pass off to make_leaf_vdev() to 1320 * construct the appropriate nvlist describing the vdev. 1321 */ 1322 if ((nv = make_leaf_vdev(argv[0])) == NULL) 1323 return (NULL); 1324 argc--; 1325 argv++; 1326 } 1327 1328 toplevels++; 1329 top = realloc(top, toplevels * sizeof (nvlist_t *)); 1330 if (top == NULL) 1331 no_memory(); 1332 top[toplevels - 1] = nv; 1333 } 1334 1335 /* 1336 * Finally, create nvroot and add all top-level vdevs to it. 1337 */ 1338 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0); 1339 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 1340 VDEV_TYPE_ROOT) == 0); 1341 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1342 top, toplevels) == 0); 1343 1344 for (t = 0; t < toplevels; t++) 1345 nvlist_free(top[t]); 1346 free(top); 1347 1348 return (nvroot); 1349 } 1350 1351 /* 1352 * Get and validate the contents of the given vdev specification. This ensures 1353 * that the nvlist returned is well-formed, that all the devices exist, and that 1354 * they are not currently in use by any other known consumer. The 'poolconfig' 1355 * parameter is the current configuration of the pool when adding devices 1356 * existing pool, and is used to perform additional checks, such as changing the 1357 * replication level of the pool. It can be 'NULL' to indicate that this is a 1358 * new pool. The 'force' flag controls whether devices should be forcefully 1359 * added, even if they appear in use. 1360 */ 1361 nvlist_t * 1362 make_root_vdev(nvlist_t *poolconfig, int force, int check_rep, 1363 int argc, char **argv) 1364 { 1365 nvlist_t *newroot; 1366 1367 is_force = force; 1368 1369 /* 1370 * Construct the vdev specification. If this is successful, we know 1371 * that we have a valid specification, and that all devices can be 1372 * opened. 1373 */ 1374 if ((newroot = construct_spec(argc, argv)) == NULL) 1375 return (NULL); 1376 1377 /* 1378 * Validate each device to make sure that its not shared with another 1379 * subsystem. We do this even if 'force' is set, because there are some 1380 * uses (such as a dedicated dump device) that even '-f' cannot 1381 * override. 1382 */ 1383 if (check_in_use(newroot, force) != 0) { 1384 nvlist_free(newroot); 1385 return (NULL); 1386 } 1387 1388 /* 1389 * Check the replication level of the given vdevs and report any errors 1390 * found. We include the existing pool spec, if any, as we need to 1391 * catch changes against the existing replication level. 1392 */ 1393 if (check_rep && check_replication(poolconfig, newroot) != 0) { 1394 nvlist_free(newroot); 1395 return (NULL); 1396 } 1397 1398 /* 1399 * Run through the vdev specification and label any whole disks found. 1400 */ 1401 if (make_disks(newroot) != 0) { 1402 nvlist_free(newroot); 1403 return (NULL); 1404 } 1405 1406 return (newroot); 1407 } 1408