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