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 737 /* 738 * For a whole disk, defer getting its devid until after labeling it. 739 */ 740 if (S_ISBLK(statbuf.st_mode) && !wholedisk) { 741 /* 742 * Get the devid for the device. 743 */ 744 int fd; 745 ddi_devid_t devid; 746 char *minor = NULL, *devid_str = NULL; 747 748 if ((fd = open(path, O_RDONLY)) < 0) { 749 (void) fprintf(stderr, gettext("cannot open '%s': " 750 "%s\n"), path, strerror(errno)); 751 nvlist_free(vdev); 752 return (NULL); 753 } 754 755 if (devid_get(fd, &devid) == 0) { 756 if (devid_get_minor_name(fd, &minor) == 0 && 757 (devid_str = devid_str_encode(devid, minor)) != 758 NULL) { 759 verify(nvlist_add_string(vdev, 760 ZPOOL_CONFIG_DEVID, devid_str) == 0); 761 } 762 if (devid_str != NULL) 763 devid_str_free(devid_str); 764 if (minor != NULL) 765 devid_str_free(minor); 766 devid_free(devid); 767 } 768 769 (void) close(fd); 770 } 771 772 return (vdev); 773 } 774 775 /* 776 * Go through and verify the replication level of the pool is consistent. 777 * Performs the following checks: 778 * 779 * For the new spec, verifies that devices in mirrors and raidz are the 780 * same size. 781 * 782 * If the current configuration already has inconsistent replication 783 * levels, ignore any other potential problems in the new spec. 784 * 785 * Otherwise, make sure that the current spec (if there is one) and the new 786 * spec have consistent replication levels. 787 */ 788 typedef struct replication_level { 789 char *type; 790 int level; 791 } replication_level_t; 792 793 /* 794 * Given a list of toplevel vdevs, return the current replication level. If 795 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then 796 * an error message will be displayed for each self-inconsistent vdev. 797 */ 798 replication_level_t * 799 get_replication(nvlist_t *nvroot, int fatal) 800 { 801 nvlist_t **top; 802 uint_t t, toplevels; 803 nvlist_t **child; 804 uint_t c, children; 805 nvlist_t *nv; 806 char *type; 807 replication_level_t lastrep, rep, *ret; 808 int dontreport; 809 810 ret = safe_malloc(sizeof (replication_level_t)); 811 812 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 813 &top, &toplevels) == 0); 814 815 lastrep.type = NULL; 816 for (t = 0; t < toplevels; t++) { 817 nv = top[t]; 818 819 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 820 821 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 822 &child, &children) != 0) { 823 /* 824 * This is a 'file' or 'disk' vdev. 825 */ 826 rep.type = type; 827 rep.level = 1; 828 } else { 829 uint64_t vdev_size; 830 831 /* 832 * This is a mirror or RAID-Z vdev. Go through and make 833 * sure the contents are all the same (files vs. disks), 834 * keeping track of the number of elements in the 835 * process. 836 * 837 * We also check that the size of each vdev (if it can 838 * be determined) is the same. 839 */ 840 rep.type = type; 841 rep.level = 0; 842 843 /* 844 * The 'dontreport' variable indicatest that we've 845 * already reported an error for this spec, so don't 846 * bother doing it again. 847 */ 848 type = NULL; 849 dontreport = 0; 850 vdev_size = -1ULL; 851 for (c = 0; c < children; c++) { 852 nvlist_t *cnv = child[c]; 853 char *path; 854 struct stat64 statbuf; 855 uint64_t size = -1ULL; 856 char *childtype; 857 int fd, err; 858 859 rep.level++; 860 861 verify(nvlist_lookup_string(cnv, 862 ZPOOL_CONFIG_TYPE, &childtype) == 0); 863 verify(nvlist_lookup_string(cnv, 864 ZPOOL_CONFIG_PATH, &path) == 0); 865 866 /* 867 * If we have a raidz/mirror that combines disks 868 * with files, report it as an error. 869 */ 870 if (!dontreport && type != NULL && 871 strcmp(type, childtype) != 0) { 872 if (ret != NULL) 873 free(ret); 874 ret = NULL; 875 if (fatal) 876 vdev_error(gettext( 877 "mismatched replication " 878 "level: %s contains both " 879 "files and devices\n"), 880 rep.type); 881 else 882 return (NULL); 883 dontreport = TRUE; 884 } 885 886 /* 887 * According to stat(2), the value of 'st_size' 888 * is undefined for block devices and character 889 * devices. But there is no effective way to 890 * determine the real size in userland. 891 * 892 * Instead, we'll take advantage of an 893 * implementation detail of spec_size(). If the 894 * device is currently open, then we (should) 895 * return a valid size. 896 * 897 * If we still don't get a valid size (indicated 898 * by a size of 0 or MAXOFFSET_T), then ignore 899 * this device altogether. 900 */ 901 if ((fd = open(path, O_RDONLY)) >= 0) { 902 err = fstat64(fd, &statbuf); 903 (void) close(fd); 904 } else { 905 err = stat64(path, &statbuf); 906 } 907 908 if (err != 0 || 909 statbuf.st_size == 0 || 910 statbuf.st_size == MAXOFFSET_T) 911 continue; 912 913 size = statbuf.st_size; 914 915 /* 916 * Also check the size of each device. If they 917 * differ, then report an error. 918 */ 919 if (!dontreport && vdev_size != -1ULL && 920 size != vdev_size) { 921 if (ret != NULL) 922 free(ret); 923 ret = NULL; 924 if (fatal) 925 vdev_error(gettext( 926 "%s contains devices of " 927 "different sizes\n"), 928 rep.type); 929 else 930 return (NULL); 931 dontreport = TRUE; 932 } 933 934 type = childtype; 935 vdev_size = size; 936 } 937 } 938 939 /* 940 * At this point, we have the replication of the last toplevel 941 * vdev in 'rep'. Compare it to 'lastrep' to see if its 942 * different. 943 */ 944 if (lastrep.type != NULL) { 945 if (strcmp(lastrep.type, rep.type) != 0) { 946 if (ret != NULL) 947 free(ret); 948 ret = NULL; 949 if (fatal) 950 vdev_error(gettext( 951 "mismatched replication " 952 "level: both %s and %s vdevs are " 953 "present\n"), 954 lastrep.type, rep.type); 955 else 956 return (NULL); 957 } else if (lastrep.level != rep.level) { 958 if (ret) 959 free(ret); 960 ret = NULL; 961 if (fatal) 962 vdev_error(gettext( 963 "mismatched replication " 964 "level: %d-way %s and %d-way %s " 965 "vdevs are present\n"), 966 lastrep.level, lastrep.type, 967 rep.level, rep.type); 968 else 969 return (NULL); 970 } 971 } 972 lastrep = rep; 973 } 974 975 if (ret != NULL) { 976 ret->type = rep.type; 977 ret->level = rep.level; 978 } 979 980 return (ret); 981 } 982 983 /* 984 * Check the replication level of the vdev spec against the current pool. Calls 985 * get_replication() to make sure the new spec is self-consistent. If the pool 986 * has a consistent replication level, then we ignore any errors. Otherwise, 987 * report any difference between the two. 988 */ 989 int 990 check_replication(nvlist_t *config, nvlist_t *newroot) 991 { 992 replication_level_t *current = NULL, *new; 993 int ret; 994 995 /* 996 * If we have a current pool configuration, check to see if it's 997 * self-consistent. If not, simply return success. 998 */ 999 if (config != NULL) { 1000 nvlist_t *nvroot; 1001 1002 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1003 &nvroot) == 0); 1004 if ((current = get_replication(nvroot, FALSE)) == NULL) 1005 return (0); 1006 } 1007 1008 /* 1009 * Get the replication level of the new vdev spec, reporting any 1010 * inconsistencies found. 1011 */ 1012 if ((new = get_replication(newroot, TRUE)) == NULL) { 1013 free(current); 1014 return (-1); 1015 } 1016 1017 /* 1018 * Check to see if the new vdev spec matches the replication level of 1019 * the current pool. 1020 */ 1021 ret = 0; 1022 if (current != NULL) { 1023 if (strcmp(current->type, new->type) != 0 || 1024 current->level != new->level) { 1025 vdev_error(gettext( 1026 "mismatched replication level: pool uses %d-way %s " 1027 "and new vdev uses %d-way %s\n"), 1028 current->level, current->type, new->level, 1029 new->type); 1030 ret = -1; 1031 } 1032 } 1033 1034 free(new); 1035 if (current != NULL) 1036 free(current); 1037 1038 return (ret); 1039 } 1040 1041 /* 1042 * Label an individual disk. The name provided is the short name, stripped of 1043 * any leading /dev path. 1044 */ 1045 int 1046 label_disk(char *name) 1047 { 1048 char path[MAXPATHLEN]; 1049 struct dk_gpt *vtoc; 1050 int fd; 1051 size_t resv = 16384; 1052 1053 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 1054 BACKUP_SLICE); 1055 1056 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 1057 /* 1058 * This shouldn't happen. We've long since verified that this 1059 * is a valid device. 1060 */ 1061 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"), 1062 path, strerror(errno)); 1063 return (-1); 1064 } 1065 1066 1067 if (efi_alloc_and_init(fd, 9, &vtoc) != 0) { 1068 /* 1069 * The only way this can fail is if we run out of memory, or we 1070 * were unable to read the disk geometry. 1071 */ 1072 if (errno == ENOMEM) 1073 no_memory(); 1074 1075 (void) fprintf(stderr, gettext("cannot label '%s': unable to " 1076 "read disk geometry\n"), name); 1077 (void) close(fd); 1078 return (-1); 1079 } 1080 1081 vtoc->efi_parts[0].p_start = vtoc->efi_first_u_lba; 1082 vtoc->efi_parts[0].p_size = vtoc->efi_last_u_lba + 1 - 1083 vtoc->efi_first_u_lba - resv; 1084 1085 /* 1086 * Why we use V_USR: V_BACKUP confuses users, and is considered 1087 * disposable by some EFI utilities (since EFI doesn't have a backup 1088 * slice). V_UNASSIGNED is supposed to be used only for zero size 1089 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 1090 * etc. were all pretty specific. V_USR is as close to reality as we 1091 * can get, in the absence of V_OTHER. 1092 */ 1093 vtoc->efi_parts[0].p_tag = V_USR; 1094 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 1095 1096 vtoc->efi_parts[8].p_start = vtoc->efi_last_u_lba + 1 - resv; 1097 vtoc->efi_parts[8].p_size = resv; 1098 vtoc->efi_parts[8].p_tag = V_RESERVED; 1099 1100 if (efi_write(fd, vtoc) != 0) { 1101 /* 1102 * Currently, EFI labels are not supported for IDE disks, and it 1103 * is likely that they will not be supported on other drives for 1104 * some time. Print out a helpful error message directing the 1105 * user to manually label the disk and give a specific slice. 1106 */ 1107 (void) fprintf(stderr, gettext("cannot label '%s': failed to " 1108 "write EFI label\n"), name); 1109 (void) fprintf(stderr, gettext("use fdisk(1M) to partition " 1110 "the disk, and provide a specific slice\n")); 1111 (void) close(fd); 1112 return (-1); 1113 } 1114 1115 (void) close(fd); 1116 return (0); 1117 } 1118 1119 /* 1120 * Go through and find any whole disks in the vdev specification, labelling them 1121 * as appropriate. When constructing the vdev spec, we were unable to open this 1122 * device in order to provide a devid. Now that we have labelled the disk and 1123 * know that slice 0 is valid, we can construct the devid now. 1124 * 1125 * If the disk was already labelled with an EFI label, we will have gotten the 1126 * devid already (because we were able to open the whole disk). Otherwise, we 1127 * need to get the devid after we label the disk. 1128 */ 1129 int 1130 make_disks(nvlist_t *nv) 1131 { 1132 nvlist_t **child; 1133 uint_t c, children; 1134 char *type, *path, *diskname; 1135 char buf[MAXPATHLEN]; 1136 struct stat64 statbuf; 1137 int fd; 1138 int ret; 1139 ddi_devid_t devid; 1140 char *minor = NULL, *devid_str = NULL; 1141 1142 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 1143 1144 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1145 &child, &children) != 0) { 1146 1147 if (strcmp(type, VDEV_TYPE_DISK) != 0) 1148 return (0); 1149 1150 /* 1151 * We have a disk device. Get the path to the device 1152 * and see if its a whole disk by appending the backup 1153 * slice and stat()ing the device. 1154 */ 1155 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 1156 1157 if (!is_whole_disk(path, &statbuf)) 1158 return (0); 1159 1160 diskname = strrchr(path, '/'); 1161 assert(diskname != NULL); 1162 diskname++; 1163 if (label_disk(diskname) != 0) 1164 return (-1); 1165 1166 /* 1167 * Fill in the devid, now that we've labeled the disk. 1168 */ 1169 (void) snprintf(buf, sizeof (buf), "%ss0", path); 1170 if ((fd = open(buf, O_RDONLY)) < 0) { 1171 (void) fprintf(stderr, 1172 gettext("cannot open '%s': %s\n"), 1173 buf, strerror(errno)); 1174 return (-1); 1175 } 1176 1177 if (devid_get(fd, &devid) == 0) { 1178 if (devid_get_minor_name(fd, &minor) == 0 && 1179 (devid_str = devid_str_encode(devid, minor)) != 1180 NULL) { 1181 verify(nvlist_add_string(nv, 1182 ZPOOL_CONFIG_DEVID, devid_str) == 0); 1183 } 1184 if (devid_str != NULL) 1185 devid_str_free(devid_str); 1186 if (minor != NULL) 1187 devid_str_free(minor); 1188 devid_free(devid); 1189 } 1190 1191 (void) close(fd); 1192 1193 return (0); 1194 } 1195 1196 for (c = 0; c < children; c++) 1197 if ((ret = make_disks(child[c])) != 0) 1198 return (ret); 1199 1200 return (0); 1201 } 1202 1203 /* 1204 * Go through and find any devices that are in use. We rely on libdiskmgt for 1205 * the majority of this task. 1206 */ 1207 int 1208 check_in_use(nvlist_t *nv, int force) 1209 { 1210 nvlist_t **child; 1211 uint_t c, children; 1212 char *type, *path; 1213 int ret; 1214 1215 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 1216 1217 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1218 &child, &children) != 0) { 1219 1220 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 1221 1222 if (strcmp(type, VDEV_TYPE_DISK) == 0) 1223 ret = check_device(path, force); 1224 1225 if (strcmp(type, VDEV_TYPE_FILE) == 0) 1226 ret = check_file(path, force); 1227 1228 return (ret); 1229 } 1230 1231 for (c = 0; c < children; c++) 1232 if ((ret = check_in_use(child[c], force)) != 0) 1233 return (ret); 1234 1235 return (0); 1236 } 1237 1238 /* 1239 * Construct a syntactically valid vdev specification, 1240 * and ensure that all devices and files exist and can be opened. 1241 * Note: we don't bother freeing anything in the error paths 1242 * because the program is just going to exit anyway. 1243 */ 1244 nvlist_t * 1245 construct_spec(int argc, char **argv) 1246 { 1247 nvlist_t *nvroot, *nv, **top; 1248 int t, toplevels; 1249 1250 top = NULL; 1251 toplevels = 0; 1252 1253 while (argc > 0) { 1254 nv = NULL; 1255 1256 /* 1257 * If it's a mirror or raidz, the subsequent arguments are 1258 * its leaves -- until we encounter the next mirror or raidz. 1259 */ 1260 if (strcmp(argv[0], VDEV_TYPE_MIRROR) == 0 || 1261 strcmp(argv[0], VDEV_TYPE_RAIDZ) == 0) { 1262 1263 char *type = argv[0]; 1264 nvlist_t **child = NULL; 1265 int children = 0; 1266 int c; 1267 1268 for (c = 1; c < argc; c++) { 1269 if (strcmp(argv[c], VDEV_TYPE_MIRROR) == 0 || 1270 strcmp(argv[c], VDEV_TYPE_RAIDZ) == 0) 1271 break; 1272 children++; 1273 child = realloc(child, 1274 children * sizeof (nvlist_t *)); 1275 if (child == NULL) 1276 no_memory(); 1277 if ((nv = make_leaf_vdev(argv[c])) == NULL) 1278 return (NULL); 1279 child[children - 1] = nv; 1280 } 1281 1282 argc -= c; 1283 argv += c; 1284 1285 /* 1286 * Mirrors and RAID-Z devices require at least 1287 * two components. 1288 */ 1289 if (children < 2) { 1290 (void) fprintf(stderr, 1291 gettext("invalid vdev specification: " 1292 "%s requires at least 2 devices\n"), type); 1293 return (NULL); 1294 } 1295 1296 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) == 0); 1297 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 1298 type) == 0); 1299 verify(nvlist_add_nvlist_array(nv, 1300 ZPOOL_CONFIG_CHILDREN, child, children) == 0); 1301 1302 for (c = 0; c < children; c++) 1303 nvlist_free(child[c]); 1304 free(child); 1305 } else { 1306 /* 1307 * We have a device. Pass off to make_leaf_vdev() to 1308 * construct the appropriate nvlist describing the vdev. 1309 */ 1310 if ((nv = make_leaf_vdev(argv[0])) == NULL) 1311 return (NULL); 1312 argc--; 1313 argv++; 1314 } 1315 1316 toplevels++; 1317 top = realloc(top, toplevels * sizeof (nvlist_t *)); 1318 if (top == NULL) 1319 no_memory(); 1320 top[toplevels - 1] = nv; 1321 } 1322 1323 /* 1324 * Finally, create nvroot and add all top-level vdevs to it. 1325 */ 1326 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0); 1327 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 1328 VDEV_TYPE_ROOT) == 0); 1329 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 1330 top, toplevels) == 0); 1331 1332 for (t = 0; t < toplevels; t++) 1333 nvlist_free(top[t]); 1334 free(top); 1335 1336 return (nvroot); 1337 } 1338 1339 /* 1340 * Get and validate the contents of the given vdev specification. This ensures 1341 * that the nvlist returned is well-formed, that all the devices exist, and that 1342 * they are not currently in use by any other known consumer. The 'poolconfig' 1343 * parameter is the current configuration of the pool when adding devices 1344 * existing pool, and is used to perform additional checks, such as changing the 1345 * replication level of the pool. It can be 'NULL' to indicate that this is a 1346 * new pool. The 'force' flag controls whether devices should be forcefully 1347 * added, even if they appear in use. 1348 */ 1349 nvlist_t * 1350 make_root_vdev(nvlist_t *poolconfig, int force, int check_rep, 1351 int argc, char **argv) 1352 { 1353 nvlist_t *newroot; 1354 1355 is_force = force; 1356 1357 /* 1358 * Construct the vdev specification. If this is successful, we know 1359 * that we have a valid specification, and that all devices can be 1360 * opened. 1361 */ 1362 if ((newroot = construct_spec(argc, argv)) == NULL) 1363 return (NULL); 1364 1365 /* 1366 * Validate each device to make sure that its not shared with another 1367 * subsystem. We do this even if 'force' is set, because there are some 1368 * uses (such as a dedicated dump device) that even '-f' cannot 1369 * override. 1370 */ 1371 if (check_in_use(newroot, force) != 0) { 1372 nvlist_free(newroot); 1373 return (NULL); 1374 } 1375 1376 /* 1377 * Check the replication level of the given vdevs and report any errors 1378 * found. We include the existing pool spec, if any, as we need to 1379 * catch changes against the existing replication level. 1380 */ 1381 if (check_rep && check_replication(poolconfig, newroot) != 0) { 1382 nvlist_free(newroot); 1383 return (NULL); 1384 } 1385 1386 /* 1387 * Run through the vdev specification and label any whole disks found. 1388 */ 1389 if (make_disks(newroot) != 0) { 1390 nvlist_free(newroot); 1391 return (NULL); 1392 } 1393 1394 return (newroot); 1395 } 1396