1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 212082Seschrock 22789Sahrens /* 2312296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <ctype.h> 27789Sahrens #include <errno.h> 28789Sahrens #include <devid.h> 29789Sahrens #include <fcntl.h> 30789Sahrens #include <libintl.h> 31789Sahrens #include <stdio.h> 32789Sahrens #include <stdlib.h> 333126Sahl #include <strings.h> 34789Sahrens #include <unistd.h> 354276Staylor #include <sys/efi_partition.h> 364276Staylor #include <sys/vtoc.h> 37789Sahrens #include <sys/zfs_ioctl.h> 389816SGeorge.Wilson@Sun.COM #include <dlfcn.h> 39789Sahrens 40789Sahrens #include "zfs_namecheck.h" 413912Slling #include "zfs_prop.h" 42789Sahrens #include "libzfs_impl.h" 4310921STim.Haley@Sun.COM #include "zfs_comutil.h" 44789Sahrens 457042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 465094Slling 479816SGeorge.Wilson@Sun.COM #define DISK_ROOT "/dev/dsk" 489816SGeorge.Wilson@Sun.COM #define RDISK_ROOT "/dev/rdsk" 499816SGeorge.Wilson@Sun.COM #define BACKUP_SLICE "s2" 509816SGeorge.Wilson@Sun.COM 515094Slling /* 525094Slling * ==================================================================== 535094Slling * zpool property functions 545094Slling * ==================================================================== 555094Slling */ 565094Slling 575094Slling static int 585094Slling zpool_get_all_props(zpool_handle_t *zhp) 595094Slling { 605094Slling zfs_cmd_t zc = { 0 }; 615094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 625094Slling 635094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 645094Slling 655094Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 665094Slling return (-1); 675094Slling 685094Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 695094Slling if (errno == ENOMEM) { 705094Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 715094Slling zcmd_free_nvlists(&zc); 725094Slling return (-1); 735094Slling } 745094Slling } else { 755094Slling zcmd_free_nvlists(&zc); 765094Slling return (-1); 775094Slling } 785094Slling } 795094Slling 805094Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 815094Slling zcmd_free_nvlists(&zc); 825094Slling return (-1); 835094Slling } 845094Slling 855094Slling zcmd_free_nvlists(&zc); 865094Slling 875094Slling return (0); 885094Slling } 895094Slling 905094Slling static int 915094Slling zpool_props_refresh(zpool_handle_t *zhp) 925094Slling { 935094Slling nvlist_t *old_props; 945094Slling 955094Slling old_props = zhp->zpool_props; 965094Slling 975094Slling if (zpool_get_all_props(zhp) != 0) 985094Slling return (-1); 995094Slling 1005094Slling nvlist_free(old_props); 1015094Slling return (0); 1025094Slling } 1035094Slling 1045094Slling static char * 1055094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 1065094Slling zprop_source_t *src) 1075094Slling { 1085094Slling nvlist_t *nv, *nvl; 1095094Slling uint64_t ival; 1105094Slling char *value; 1115094Slling zprop_source_t source; 1125094Slling 1135094Slling nvl = zhp->zpool_props; 1145094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1155094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 1165094Slling source = ival; 1175094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1185094Slling } else { 1195094Slling source = ZPROP_SRC_DEFAULT; 1205094Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 1215094Slling value = "-"; 1225094Slling } 1235094Slling 1245094Slling if (src) 1255094Slling *src = source; 1265094Slling 1275094Slling return (value); 1285094Slling } 1295094Slling 1305094Slling uint64_t 1315094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 1325094Slling { 1335094Slling nvlist_t *nv, *nvl; 1345094Slling uint64_t value; 1355094Slling zprop_source_t source; 1365094Slling 1377294Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 1387294Sperrin /* 1397294Sperrin * zpool_get_all_props() has most likely failed because 1407294Sperrin * the pool is faulted, but if all we need is the top level 1417294Sperrin * vdev's guid then get it from the zhp config nvlist. 1427294Sperrin */ 1437294Sperrin if ((prop == ZPOOL_PROP_GUID) && 1447294Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 1457294Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 1467294Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 1477294Sperrin == 0)) { 1487294Sperrin return (value); 1497294Sperrin } 1505094Slling return (zpool_prop_default_numeric(prop)); 1517294Sperrin } 1525094Slling 1535094Slling nvl = zhp->zpool_props; 1545094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1555094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 1565094Slling source = value; 1575094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1585094Slling } else { 1595094Slling source = ZPROP_SRC_DEFAULT; 1605094Slling value = zpool_prop_default_numeric(prop); 1615094Slling } 1625094Slling 1635094Slling if (src) 1645094Slling *src = source; 1655094Slling 1665094Slling return (value); 1675094Slling } 1685094Slling 1695094Slling /* 1705094Slling * Map VDEV STATE to printed strings. 1715094Slling */ 1725094Slling char * 1735094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 1745094Slling { 1755094Slling switch (state) { 1765094Slling case VDEV_STATE_CLOSED: 1775094Slling case VDEV_STATE_OFFLINE: 1785094Slling return (gettext("OFFLINE")); 1795094Slling case VDEV_STATE_REMOVED: 1805094Slling return (gettext("REMOVED")); 1815094Slling case VDEV_STATE_CANT_OPEN: 1827294Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 1835094Slling return (gettext("FAULTED")); 18411422SMark.Musante@Sun.COM else if (aux == VDEV_AUX_SPLIT_POOL) 18511422SMark.Musante@Sun.COM return (gettext("SPLIT")); 1865094Slling else 1875094Slling return (gettext("UNAVAIL")); 1885094Slling case VDEV_STATE_FAULTED: 1895094Slling return (gettext("FAULTED")); 1905094Slling case VDEV_STATE_DEGRADED: 1915094Slling return (gettext("DEGRADED")); 1925094Slling case VDEV_STATE_HEALTHY: 1935094Slling return (gettext("ONLINE")); 1945094Slling } 1955094Slling 1965094Slling return (gettext("UNKNOWN")); 1975094Slling } 1985094Slling 1995094Slling /* 2005094Slling * Get a zpool property value for 'prop' and return the value in 2015094Slling * a pre-allocated buffer. 2025094Slling */ 2035094Slling int 2045094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 2055094Slling zprop_source_t *srctype) 2065094Slling { 2075094Slling uint64_t intval; 2085094Slling const char *strval; 2095094Slling zprop_source_t src = ZPROP_SRC_NONE; 2105094Slling nvlist_t *nvroot; 2115094Slling vdev_stat_t *vs; 2125094Slling uint_t vsc; 2135094Slling 2145094Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 2158525SEric.Schrock@Sun.COM switch (prop) { 2168525SEric.Schrock@Sun.COM case ZPOOL_PROP_NAME: 2175094Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 2188525SEric.Schrock@Sun.COM break; 2198525SEric.Schrock@Sun.COM 2208525SEric.Schrock@Sun.COM case ZPOOL_PROP_HEALTH: 2215094Slling (void) strlcpy(buf, "FAULTED", len); 2228525SEric.Schrock@Sun.COM break; 2238525SEric.Schrock@Sun.COM 2248525SEric.Schrock@Sun.COM case ZPOOL_PROP_GUID: 2258525SEric.Schrock@Sun.COM intval = zpool_get_prop_int(zhp, prop, &src); 2268525SEric.Schrock@Sun.COM (void) snprintf(buf, len, "%llu", intval); 2278525SEric.Schrock@Sun.COM break; 2288525SEric.Schrock@Sun.COM 2298525SEric.Schrock@Sun.COM case ZPOOL_PROP_ALTROOT: 2308525SEric.Schrock@Sun.COM case ZPOOL_PROP_CACHEFILE: 2318525SEric.Schrock@Sun.COM if (zhp->zpool_props != NULL || 2328525SEric.Schrock@Sun.COM zpool_get_all_props(zhp) == 0) { 2338525SEric.Schrock@Sun.COM (void) strlcpy(buf, 2348525SEric.Schrock@Sun.COM zpool_get_prop_string(zhp, prop, &src), 2358525SEric.Schrock@Sun.COM len); 2368525SEric.Schrock@Sun.COM if (srctype != NULL) 2378525SEric.Schrock@Sun.COM *srctype = src; 2388525SEric.Schrock@Sun.COM return (0); 2398525SEric.Schrock@Sun.COM } 2408525SEric.Schrock@Sun.COM /* FALLTHROUGH */ 2418525SEric.Schrock@Sun.COM default: 2425094Slling (void) strlcpy(buf, "-", len); 2438525SEric.Schrock@Sun.COM break; 2448525SEric.Schrock@Sun.COM } 2458525SEric.Schrock@Sun.COM 2468525SEric.Schrock@Sun.COM if (srctype != NULL) 2478525SEric.Schrock@Sun.COM *srctype = src; 2485094Slling return (0); 2495094Slling } 2505094Slling 2515094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 2525094Slling prop != ZPOOL_PROP_NAME) 2535094Slling return (-1); 2545094Slling 2555094Slling switch (zpool_prop_get_type(prop)) { 2565094Slling case PROP_TYPE_STRING: 2575094Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 2585094Slling len); 2595094Slling break; 2605094Slling 2615094Slling case PROP_TYPE_NUMBER: 2625094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2635094Slling 2645094Slling switch (prop) { 2655094Slling case ZPOOL_PROP_SIZE: 26610956SGeorge.Wilson@Sun.COM case ZPOOL_PROP_ALLOCATED: 26710956SGeorge.Wilson@Sun.COM case ZPOOL_PROP_FREE: 2685094Slling (void) zfs_nicenum(intval, buf, len); 2695094Slling break; 2705094Slling 2715094Slling case ZPOOL_PROP_CAPACITY: 2725094Slling (void) snprintf(buf, len, "%llu%%", 2735094Slling (u_longlong_t)intval); 2745094Slling break; 2755094Slling 27610922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPRATIO: 27710922SJeff.Bonwick@Sun.COM (void) snprintf(buf, len, "%llu.%02llux", 27810922SJeff.Bonwick@Sun.COM (u_longlong_t)(intval / 100), 27910922SJeff.Bonwick@Sun.COM (u_longlong_t)(intval % 100)); 28010922SJeff.Bonwick@Sun.COM break; 28110922SJeff.Bonwick@Sun.COM 2825094Slling case ZPOOL_PROP_HEALTH: 2835094Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2845094Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2855094Slling verify(nvlist_lookup_uint64_array(nvroot, 28612296SLin.Ling@Sun.COM ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 28712296SLin.Ling@Sun.COM == 0); 2885094Slling 2895094Slling (void) strlcpy(buf, zpool_state_to_name(intval, 2905094Slling vs->vs_aux), len); 2915094Slling break; 2925094Slling default: 2935094Slling (void) snprintf(buf, len, "%llu", intval); 2945094Slling } 2955094Slling break; 2965094Slling 2975094Slling case PROP_TYPE_INDEX: 2985094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2995094Slling if (zpool_prop_index_to_string(prop, intval, &strval) 3005094Slling != 0) 3015094Slling return (-1); 3025094Slling (void) strlcpy(buf, strval, len); 3035094Slling break; 3045094Slling 3055094Slling default: 3065094Slling abort(); 3075094Slling } 3085094Slling 3095094Slling if (srctype) 3105094Slling *srctype = src; 3115094Slling 3125094Slling return (0); 3135094Slling } 3145094Slling 3155094Slling /* 3165094Slling * Check if the bootfs name has the same pool name as it is set to. 3175094Slling * Assuming bootfs is a valid dataset name. 3185094Slling */ 3195094Slling static boolean_t 3205094Slling bootfs_name_valid(const char *pool, char *bootfs) 3215094Slling { 3225094Slling int len = strlen(pool); 3235094Slling 3247300SEric.Taylor@Sun.COM if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 3255094Slling return (B_FALSE); 3265094Slling 3275094Slling if (strncmp(pool, bootfs, len) == 0 && 3285094Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 3295094Slling return (B_TRUE); 3305094Slling 3315094Slling return (B_FALSE); 3325094Slling } 3335094Slling 3345094Slling /* 3357042Sgw25295 * Inspect the configuration to determine if any of the devices contain 3367042Sgw25295 * an EFI label. 3377042Sgw25295 */ 3387042Sgw25295 static boolean_t 3397042Sgw25295 pool_uses_efi(nvlist_t *config) 3407042Sgw25295 { 3417042Sgw25295 nvlist_t **child; 3427042Sgw25295 uint_t c, children; 3437042Sgw25295 3447042Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3457042Sgw25295 &child, &children) != 0) 3467042Sgw25295 return (read_efi_label(config, NULL) >= 0); 3477042Sgw25295 3487042Sgw25295 for (c = 0; c < children; c++) { 3497042Sgw25295 if (pool_uses_efi(child[c])) 3507042Sgw25295 return (B_TRUE); 3517042Sgw25295 } 3527042Sgw25295 return (B_FALSE); 3537042Sgw25295 } 3547042Sgw25295 3557965SGeorge.Wilson@Sun.COM static boolean_t 3567965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp) 3577965SGeorge.Wilson@Sun.COM { 3587965SGeorge.Wilson@Sun.COM char bootfs[ZPOOL_MAXNAMELEN]; 3597965SGeorge.Wilson@Sun.COM 3607965SGeorge.Wilson@Sun.COM return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 3617965SGeorge.Wilson@Sun.COM sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 3627965SGeorge.Wilson@Sun.COM sizeof (bootfs)) != 0); 3637965SGeorge.Wilson@Sun.COM } 3647965SGeorge.Wilson@Sun.COM 3657965SGeorge.Wilson@Sun.COM 3667042Sgw25295 /* 3675094Slling * Given an nvlist of zpool properties to be set, validate that they are 3685094Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 3695094Slling * specified as strings. 3705094Slling */ 3715094Slling static nvlist_t * 3727184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 3735094Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 3745094Slling { 3755094Slling nvpair_t *elem; 3765094Slling nvlist_t *retprops; 3775094Slling zpool_prop_t prop; 3785094Slling char *strval; 3795094Slling uint64_t intval; 3805363Seschrock char *slash; 3815363Seschrock struct stat64 statbuf; 3827042Sgw25295 zpool_handle_t *zhp; 3837042Sgw25295 nvlist_t *nvroot; 3845094Slling 3855094Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 3865094Slling (void) no_memory(hdl); 3875094Slling return (NULL); 3885094Slling } 3895094Slling 3905094Slling elem = NULL; 3915094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3925094Slling const char *propname = nvpair_name(elem); 3935094Slling 3945094Slling /* 3955094Slling * Make sure this property is valid and applies to this type. 3965094Slling */ 3975094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 3985094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3995094Slling "invalid property '%s'"), propname); 4005094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4015094Slling goto error; 4025094Slling } 4035094Slling 4045094Slling if (zpool_prop_readonly(prop)) { 4055094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4065094Slling "is readonly"), propname); 4075094Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 4085094Slling goto error; 4095094Slling } 4105094Slling 4115094Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 4125094Slling &strval, &intval, errbuf) != 0) 4135094Slling goto error; 4145094Slling 4155094Slling /* 4165094Slling * Perform additional checking for specific properties. 4175094Slling */ 4185094Slling switch (prop) { 4195094Slling case ZPOOL_PROP_VERSION: 4205094Slling if (intval < version || intval > SPA_VERSION) { 4215094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4225094Slling "property '%s' number %d is invalid."), 4235094Slling propname, intval); 4245094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4255094Slling goto error; 4265094Slling } 4275094Slling break; 4285094Slling 4295094Slling case ZPOOL_PROP_BOOTFS: 4305094Slling if (create_or_import) { 4315094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4325094Slling "property '%s' cannot be set at creation " 4335094Slling "or import time"), propname); 4345094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4355094Slling goto error; 4365094Slling } 4375094Slling 4385094Slling if (version < SPA_VERSION_BOOTFS) { 4395094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4405094Slling "pool must be upgraded to support " 4415094Slling "'%s' property"), propname); 4425094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4435094Slling goto error; 4445094Slling } 4455094Slling 4465094Slling /* 4475094Slling * bootfs property value has to be a dataset name and 4485094Slling * the dataset has to be in the same pool as it sets to. 4495094Slling */ 4505094Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 4515094Slling strval)) { 4525094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4535094Slling "is an invalid name"), strval); 4545094Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4555094Slling goto error; 4565094Slling } 4577042Sgw25295 4587042Sgw25295 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 4597042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4607042Sgw25295 "could not open pool '%s'"), poolname); 4617042Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4627042Sgw25295 goto error; 4637042Sgw25295 } 4647042Sgw25295 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 4657042Sgw25295 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4667042Sgw25295 4677042Sgw25295 /* 4687042Sgw25295 * bootfs property cannot be set on a disk which has 4697042Sgw25295 * been EFI labeled. 4707042Sgw25295 */ 4717042Sgw25295 if (pool_uses_efi(nvroot)) { 4727042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4737042Sgw25295 "property '%s' not supported on " 4747042Sgw25295 "EFI labeled devices"), propname); 4757042Sgw25295 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 4767042Sgw25295 zpool_close(zhp); 4777042Sgw25295 goto error; 4787042Sgw25295 } 4797042Sgw25295 zpool_close(zhp); 4805094Slling break; 4815094Slling 4825094Slling case ZPOOL_PROP_ALTROOT: 4835094Slling if (!create_or_import) { 4845094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4855094Slling "property '%s' can only be set during pool " 4865094Slling "creation or import"), propname); 4875094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4885094Slling goto error; 4895094Slling } 4905094Slling 4915094Slling if (strval[0] != '/') { 4925094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4935094Slling "bad alternate root '%s'"), strval); 4945094Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4955094Slling goto error; 4965094Slling } 4975094Slling break; 4985363Seschrock 4995363Seschrock case ZPOOL_PROP_CACHEFILE: 5005363Seschrock if (strval[0] == '\0') 5015363Seschrock break; 5025363Seschrock 5035363Seschrock if (strcmp(strval, "none") == 0) 5045363Seschrock break; 5055363Seschrock 5065363Seschrock if (strval[0] != '/') { 5075363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5085363Seschrock "property '%s' must be empty, an " 5095363Seschrock "absolute path, or 'none'"), propname); 5105363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5115363Seschrock goto error; 5125363Seschrock } 5135363Seschrock 5145363Seschrock slash = strrchr(strval, '/'); 5155363Seschrock 5165363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5175363Seschrock strcmp(slash, "/..") == 0) { 5185363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5195363Seschrock "'%s' is not a valid file"), strval); 5205363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5215363Seschrock goto error; 5225363Seschrock } 5235363Seschrock 5245363Seschrock *slash = '\0'; 5255363Seschrock 5265621Seschrock if (strval[0] != '\0' && 5275621Seschrock (stat64(strval, &statbuf) != 0 || 5285621Seschrock !S_ISDIR(statbuf.st_mode))) { 5295363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5305363Seschrock "'%s' is not a valid directory"), 5315363Seschrock strval); 5325363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5335363Seschrock goto error; 5345363Seschrock } 5355363Seschrock 5365363Seschrock *slash = '/'; 5375363Seschrock break; 5385094Slling } 5395094Slling } 5405094Slling 5415094Slling return (retprops); 5425094Slling error: 5435094Slling nvlist_free(retprops); 5445094Slling return (NULL); 5455094Slling } 5465094Slling 5475094Slling /* 5485094Slling * Set zpool property : propname=propval. 5495094Slling */ 5505094Slling int 5515094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 5525094Slling { 5535094Slling zfs_cmd_t zc = { 0 }; 5545094Slling int ret = -1; 5555094Slling char errbuf[1024]; 5565094Slling nvlist_t *nvl = NULL; 5575094Slling nvlist_t *realprops; 5585094Slling uint64_t version; 5595094Slling 5605094Slling (void) snprintf(errbuf, sizeof (errbuf), 5615094Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 5625094Slling zhp->zpool_name); 5635094Slling 5645094Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5655094Slling return (no_memory(zhp->zpool_hdl)); 5665094Slling 5675094Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 5685094Slling nvlist_free(nvl); 5695094Slling return (no_memory(zhp->zpool_hdl)); 5705094Slling } 5715094Slling 5725094Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5737184Stimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 5745094Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 5755094Slling nvlist_free(nvl); 5765094Slling return (-1); 5775094Slling } 5785094Slling 5795094Slling nvlist_free(nvl); 5805094Slling nvl = realprops; 5815094Slling 5825094Slling /* 5835094Slling * Execute the corresponding ioctl() to set this property. 5845094Slling */ 5855094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 5865094Slling 5875094Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 5885094Slling nvlist_free(nvl); 5895094Slling return (-1); 5905094Slling } 5915094Slling 5925094Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 5935094Slling 5945094Slling zcmd_free_nvlists(&zc); 5955094Slling nvlist_free(nvl); 5965094Slling 5975094Slling if (ret) 5985094Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 5995094Slling else 6005094Slling (void) zpool_props_refresh(zhp); 6015094Slling 6025094Slling return (ret); 6035094Slling } 6045094Slling 6055094Slling int 6065094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 6075094Slling { 6085094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 6095094Slling zprop_list_t *entry; 6105094Slling char buf[ZFS_MAXPROPLEN]; 6115094Slling 6125094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 6135094Slling return (-1); 6145094Slling 6155094Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 6165094Slling 6175094Slling if (entry->pl_fixed) 6185094Slling continue; 6195094Slling 6205094Slling if (entry->pl_prop != ZPROP_INVAL && 6215094Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 6225094Slling NULL) == 0) { 6235094Slling if (strlen(buf) > entry->pl_width) 6245094Slling entry->pl_width = strlen(buf); 6255094Slling } 6265094Slling } 6275094Slling 6285094Slling return (0); 6295094Slling } 6305094Slling 6315094Slling 632789Sahrens /* 6339816SGeorge.Wilson@Sun.COM * Don't start the slice at the default block of 34; many storage 6349816SGeorge.Wilson@Sun.COM * devices will use a stripe width of 128k, so start there instead. 6359816SGeorge.Wilson@Sun.COM */ 6369816SGeorge.Wilson@Sun.COM #define NEW_START_BLOCK 256 6379816SGeorge.Wilson@Sun.COM 6389816SGeorge.Wilson@Sun.COM /* 639789Sahrens * Validate the given pool name, optionally putting an extended error message in 640789Sahrens * 'buf'. 641789Sahrens */ 6426423Sgw25295 boolean_t 6432082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 644789Sahrens { 645789Sahrens namecheck_err_t why; 646789Sahrens char what; 6471773Seschrock int ret; 648789Sahrens 6491773Seschrock ret = pool_namecheck(pool, &why, &what); 6501773Seschrock 6511773Seschrock /* 6521773Seschrock * The rules for reserved pool names were extended at a later point. 6531773Seschrock * But we need to support users with existing pools that may now be 6541773Seschrock * invalid. So we only check for this expanded set of names during a 6551773Seschrock * create (or import), and only in userland. 6561773Seschrock */ 6571773Seschrock if (ret == 0 && !isopen && 6581773Seschrock (strncmp(pool, "mirror", 6) == 0 || 6591773Seschrock strncmp(pool, "raidz", 5) == 0 || 6604527Sperrin strncmp(pool, "spare", 5) == 0 || 6614527Sperrin strcmp(pool, "log") == 0)) { 6626423Sgw25295 if (hdl != NULL) 6636423Sgw25295 zfs_error_aux(hdl, 6646423Sgw25295 dgettext(TEXT_DOMAIN, "name is reserved")); 6652082Seschrock return (B_FALSE); 6661773Seschrock } 6671773Seschrock 6681773Seschrock 6691773Seschrock if (ret != 0) { 6702082Seschrock if (hdl != NULL) { 671789Sahrens switch (why) { 6721003Slling case NAME_ERR_TOOLONG: 6732082Seschrock zfs_error_aux(hdl, 6741003Slling dgettext(TEXT_DOMAIN, "name is too long")); 6751003Slling break; 6761003Slling 677789Sahrens case NAME_ERR_INVALCHAR: 6782082Seschrock zfs_error_aux(hdl, 679789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 680789Sahrens "'%c' in pool name"), what); 681789Sahrens break; 682789Sahrens 683789Sahrens case NAME_ERR_NOLETTER: 6842082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6852082Seschrock "name must begin with a letter")); 686789Sahrens break; 687789Sahrens 688789Sahrens case NAME_ERR_RESERVED: 6892082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6902082Seschrock "name is reserved")); 691789Sahrens break; 692789Sahrens 693789Sahrens case NAME_ERR_DISKLIKE: 6942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6952082Seschrock "pool name is reserved")); 696789Sahrens break; 6972856Snd150628 6982856Snd150628 case NAME_ERR_LEADING_SLASH: 6992856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7002856Snd150628 "leading slash in name")); 7012856Snd150628 break; 7022856Snd150628 7032856Snd150628 case NAME_ERR_EMPTY_COMPONENT: 7042856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7052856Snd150628 "empty component in name")); 7062856Snd150628 break; 7072856Snd150628 7082856Snd150628 case NAME_ERR_TRAILING_SLASH: 7092856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7102856Snd150628 "trailing slash in name")); 7112856Snd150628 break; 7122856Snd150628 7132856Snd150628 case NAME_ERR_MULTIPLE_AT: 7142856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7152856Snd150628 "multiple '@' delimiters in name")); 7162856Snd150628 break; 7172856Snd150628 718789Sahrens } 719789Sahrens } 7202082Seschrock return (B_FALSE); 721789Sahrens } 722789Sahrens 7232082Seschrock return (B_TRUE); 724789Sahrens } 725789Sahrens 726789Sahrens /* 727789Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 728789Sahrens * state. 729789Sahrens */ 730789Sahrens zpool_handle_t * 7312082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 732789Sahrens { 733789Sahrens zpool_handle_t *zhp; 7342142Seschrock boolean_t missing; 735789Sahrens 736789Sahrens /* 737789Sahrens * Make sure the pool name is valid. 738789Sahrens */ 7392082Seschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 7403237Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 7412082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 7422082Seschrock pool); 743789Sahrens return (NULL); 744789Sahrens } 745789Sahrens 7462082Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7472082Seschrock return (NULL); 748789Sahrens 7492082Seschrock zhp->zpool_hdl = hdl; 750789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 751789Sahrens 7522142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7532142Seschrock zpool_close(zhp); 7542142Seschrock return (NULL); 7552142Seschrock } 7562142Seschrock 7572142Seschrock if (missing) { 7585094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 7593237Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 7605094Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 7612142Seschrock zpool_close(zhp); 7622142Seschrock return (NULL); 763789Sahrens } 764789Sahrens 765789Sahrens return (zhp); 766789Sahrens } 767789Sahrens 768789Sahrens /* 769789Sahrens * Like the above, but silent on error. Used when iterating over pools (because 770789Sahrens * the configuration cache may be out of date). 771789Sahrens */ 7722142Seschrock int 7732142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 774789Sahrens { 775789Sahrens zpool_handle_t *zhp; 7762142Seschrock boolean_t missing; 777789Sahrens 7782142Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7792142Seschrock return (-1); 780789Sahrens 7812082Seschrock zhp->zpool_hdl = hdl; 782789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 783789Sahrens 7842142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7852142Seschrock zpool_close(zhp); 7862142Seschrock return (-1); 787789Sahrens } 788789Sahrens 7892142Seschrock if (missing) { 7902142Seschrock zpool_close(zhp); 7912142Seschrock *ret = NULL; 7922142Seschrock return (0); 7932142Seschrock } 7942142Seschrock 7952142Seschrock *ret = zhp; 7962142Seschrock return (0); 797789Sahrens } 798789Sahrens 799789Sahrens /* 800789Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 801789Sahrens * state. 802789Sahrens */ 803789Sahrens zpool_handle_t * 8042082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 805789Sahrens { 806789Sahrens zpool_handle_t *zhp; 807789Sahrens 8082082Seschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 809789Sahrens return (NULL); 810789Sahrens 811789Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 8123237Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 8132082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 814789Sahrens zpool_close(zhp); 815789Sahrens return (NULL); 816789Sahrens } 817789Sahrens 818789Sahrens return (zhp); 819789Sahrens } 820789Sahrens 821789Sahrens /* 822789Sahrens * Close the handle. Simply frees the memory associated with the handle. 823789Sahrens */ 824789Sahrens void 825789Sahrens zpool_close(zpool_handle_t *zhp) 826789Sahrens { 827789Sahrens if (zhp->zpool_config) 828789Sahrens nvlist_free(zhp->zpool_config); 829952Seschrock if (zhp->zpool_old_config) 830952Seschrock nvlist_free(zhp->zpool_old_config); 8313912Slling if (zhp->zpool_props) 8323912Slling nvlist_free(zhp->zpool_props); 833789Sahrens free(zhp); 834789Sahrens } 835789Sahrens 836789Sahrens /* 837789Sahrens * Return the name of the pool. 838789Sahrens */ 839789Sahrens const char * 840789Sahrens zpool_get_name(zpool_handle_t *zhp) 841789Sahrens { 842789Sahrens return (zhp->zpool_name); 843789Sahrens } 844789Sahrens 845789Sahrens 846789Sahrens /* 847789Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 848789Sahrens */ 849789Sahrens int 850789Sahrens zpool_get_state(zpool_handle_t *zhp) 851789Sahrens { 852789Sahrens return (zhp->zpool_state); 853789Sahrens } 854789Sahrens 855789Sahrens /* 856789Sahrens * Create the named pool, using the provided vdev list. It is assumed 857789Sahrens * that the consumer has already validated the contents of the nvlist, so we 858789Sahrens * don't have to worry about error semantics. 859789Sahrens */ 860789Sahrens int 8612082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8627184Stimh nvlist_t *props, nvlist_t *fsprops) 863789Sahrens { 864789Sahrens zfs_cmd_t zc = { 0 }; 8657184Stimh nvlist_t *zc_fsprops = NULL; 8667184Stimh nvlist_t *zc_props = NULL; 8672082Seschrock char msg[1024]; 8685094Slling char *altroot; 8697184Stimh int ret = -1; 8702082Seschrock 8712082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 8722082Seschrock "cannot create '%s'"), pool); 873789Sahrens 8742082Seschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 8752082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 8762082Seschrock 8775320Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 8785320Slling return (-1); 8795320Slling 8807184Stimh if (props) { 8817184Stimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8827184Stimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8837184Stimh goto create_failed; 8847184Stimh } 8855320Slling } 886789Sahrens 8877184Stimh if (fsprops) { 8887184Stimh uint64_t zoned; 8897184Stimh char *zonestr; 8907184Stimh 8917184Stimh zoned = ((nvlist_lookup_string(fsprops, 8927184Stimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8937184Stimh strcmp(zonestr, "on") == 0); 8947184Stimh 8957184Stimh if ((zc_fsprops = zfs_valid_proplist(hdl, 8967184Stimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 8977184Stimh goto create_failed; 8987184Stimh } 8997184Stimh if (!zc_props && 9007184Stimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 9017184Stimh goto create_failed; 9027184Stimh } 9037184Stimh if (nvlist_add_nvlist(zc_props, 9047184Stimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 9057184Stimh goto create_failed; 9067184Stimh } 9077184Stimh } 9087184Stimh 9097184Stimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 9107184Stimh goto create_failed; 9117184Stimh 912789Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 913789Sahrens 9147184Stimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 9155320Slling 9162676Seschrock zcmd_free_nvlists(&zc); 9177184Stimh nvlist_free(zc_props); 9187184Stimh nvlist_free(zc_fsprops); 9192082Seschrock 920789Sahrens switch (errno) { 921789Sahrens case EBUSY: 922789Sahrens /* 923789Sahrens * This can happen if the user has specified the same 924789Sahrens * device multiple times. We can't reliably detect this 925789Sahrens * until we try to add it and see we already have a 926789Sahrens * label. 927789Sahrens */ 9282082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9292082Seschrock "one or more vdevs refer to the same device")); 9302082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 931789Sahrens 932789Sahrens case EOVERFLOW: 933789Sahrens /* 9342082Seschrock * This occurs when one of the devices is below 935789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 936789Sahrens * device was the problem device since there's no 937789Sahrens * reliable way to determine device size from userland. 938789Sahrens */ 939789Sahrens { 940789Sahrens char buf[64]; 941789Sahrens 942789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 943789Sahrens 9442082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9452082Seschrock "one or more devices is less than the " 9462082Seschrock "minimum size (%s)"), buf); 947789Sahrens } 9482082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 949789Sahrens 950789Sahrens case ENOSPC: 9512082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9522082Seschrock "one or more devices is out of space")); 9532082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 954789Sahrens 9555450Sbrendan case ENOTBLK: 9565450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9575450Sbrendan "cache device must be a disk or disk slice")); 9585450Sbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 9595450Sbrendan 960789Sahrens default: 9612082Seschrock return (zpool_standard_error(hdl, errno, msg)); 962789Sahrens } 963789Sahrens } 964789Sahrens 965789Sahrens /* 966789Sahrens * If this is an alternate root pool, then we automatically set the 9672676Seschrock * mountpoint of the root dataset to be '/'. 968789Sahrens */ 9695094Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 9705094Slling &altroot) == 0) { 971789Sahrens zfs_handle_t *zhp; 972789Sahrens 9735094Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 9742676Seschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 9752676Seschrock "/") == 0); 976789Sahrens 977789Sahrens zfs_close(zhp); 978789Sahrens } 979789Sahrens 9807184Stimh create_failed: 9815320Slling zcmd_free_nvlists(&zc); 9827184Stimh nvlist_free(zc_props); 9837184Stimh nvlist_free(zc_fsprops); 9847184Stimh return (ret); 985789Sahrens } 986789Sahrens 987789Sahrens /* 988789Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 989789Sahrens * datasets left in the pool. 990789Sahrens */ 991789Sahrens int 992789Sahrens zpool_destroy(zpool_handle_t *zhp) 993789Sahrens { 994789Sahrens zfs_cmd_t zc = { 0 }; 995789Sahrens zfs_handle_t *zfp = NULL; 9962082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9972082Seschrock char msg[1024]; 998789Sahrens 999789Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 10002082Seschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 10012082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 1002789Sahrens return (-1); 1003789Sahrens 1004789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1005789Sahrens 10064543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 10072082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10082082Seschrock "cannot destroy '%s'"), zhp->zpool_name); 1009789Sahrens 10102082Seschrock if (errno == EROFS) { 10112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10122082Seschrock "one or more devices is read only")); 10132082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 10142082Seschrock } else { 10152082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1016789Sahrens } 1017789Sahrens 1018789Sahrens if (zfp) 1019789Sahrens zfs_close(zfp); 1020789Sahrens return (-1); 1021789Sahrens } 1022789Sahrens 1023789Sahrens if (zfp) { 1024789Sahrens remove_mountpoint(zfp); 1025789Sahrens zfs_close(zfp); 1026789Sahrens } 1027789Sahrens 1028789Sahrens return (0); 1029789Sahrens } 1030789Sahrens 1031789Sahrens /* 1032789Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1033789Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1034789Sahrens */ 1035789Sahrens int 1036789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1037789Sahrens { 10382676Seschrock zfs_cmd_t zc = { 0 }; 10392082Seschrock int ret; 10402082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10412082Seschrock char msg[1024]; 10425450Sbrendan nvlist_t **spares, **l2cache; 10435450Sbrendan uint_t nspares, nl2cache; 10442082Seschrock 10452082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10462082Seschrock "cannot add to '%s'"), zhp->zpool_name); 10472082Seschrock 10485450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10495450Sbrendan SPA_VERSION_SPARES && 10502082Seschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10512082Seschrock &spares, &nspares) == 0) { 10522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10532082Seschrock "upgraded to add hot spares")); 10542082Seschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10552082Seschrock } 1056789Sahrens 10577965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 10587965SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 10597965SGeorge.Wilson@Sun.COM uint64_t s; 10607965SGeorge.Wilson@Sun.COM 10617965SGeorge.Wilson@Sun.COM for (s = 0; s < nspares; s++) { 10627965SGeorge.Wilson@Sun.COM char *path; 10637965SGeorge.Wilson@Sun.COM 10647965SGeorge.Wilson@Sun.COM if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 10657965SGeorge.Wilson@Sun.COM &path) == 0 && pool_uses_efi(spares[s])) { 10667965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10677965SGeorge.Wilson@Sun.COM "device '%s' contains an EFI label and " 10687965SGeorge.Wilson@Sun.COM "cannot be used on root pools."), 106910594SGeorge.Wilson@Sun.COM zpool_vdev_name(hdl, NULL, spares[s], 107010594SGeorge.Wilson@Sun.COM B_FALSE)); 10717965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 10727965SGeorge.Wilson@Sun.COM } 10737965SGeorge.Wilson@Sun.COM } 10747965SGeorge.Wilson@Sun.COM } 10757965SGeorge.Wilson@Sun.COM 10765450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10775450Sbrendan SPA_VERSION_L2CACHE && 10785450Sbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 10795450Sbrendan &l2cache, &nl2cache) == 0) { 10805450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10815450Sbrendan "upgraded to add cache devices")); 10825450Sbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10835450Sbrendan } 10845450Sbrendan 10855094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 10862082Seschrock return (-1); 1087789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1088789Sahrens 10894543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1090789Sahrens switch (errno) { 1091789Sahrens case EBUSY: 1092789Sahrens /* 1093789Sahrens * This can happen if the user has specified the same 1094789Sahrens * device multiple times. We can't reliably detect this 1095789Sahrens * until we try to add it and see we already have a 1096789Sahrens * label. 1097789Sahrens */ 10982082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10992082Seschrock "one or more vdevs refer to the same device")); 11002082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1101789Sahrens break; 1102789Sahrens 1103789Sahrens case EOVERFLOW: 1104789Sahrens /* 1105789Sahrens * This occurrs when one of the devices is below 1106789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1107789Sahrens * device was the problem device since there's no 1108789Sahrens * reliable way to determine device size from userland. 1109789Sahrens */ 1110789Sahrens { 1111789Sahrens char buf[64]; 1112789Sahrens 1113789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1114789Sahrens 11152082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11162082Seschrock "device is less than the minimum " 11172082Seschrock "size (%s)"), buf); 1118789Sahrens } 11192082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 11202082Seschrock break; 11212082Seschrock 11222082Seschrock case ENOTSUP: 11232082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11244527Sperrin "pool must be upgraded to add these vdevs")); 11252082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1126789Sahrens break; 1127789Sahrens 11283912Slling case EDOM: 11293912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11304527Sperrin "root pool can not have multiple vdevs" 11314527Sperrin " or separate logs")); 11323912Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 11333912Slling break; 11343912Slling 11355450Sbrendan case ENOTBLK: 11365450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11375450Sbrendan "cache device must be a disk or disk slice")); 11385450Sbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 11395450Sbrendan break; 11405450Sbrendan 1141789Sahrens default: 11422082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1143789Sahrens } 1144789Sahrens 11452082Seschrock ret = -1; 11462082Seschrock } else { 11472082Seschrock ret = 0; 1148789Sahrens } 1149789Sahrens 11502676Seschrock zcmd_free_nvlists(&zc); 1151789Sahrens 11522082Seschrock return (ret); 1153789Sahrens } 1154789Sahrens 1155789Sahrens /* 1156789Sahrens * Exports the pool from the system. The caller must ensure that there are no 1157789Sahrens * mounted datasets in the pool. 1158789Sahrens */ 1159789Sahrens int 11608211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1161789Sahrens { 1162789Sahrens zfs_cmd_t zc = { 0 }; 11637214Slling char msg[1024]; 1164789Sahrens 11657214Slling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 11667214Slling "cannot export '%s'"), zhp->zpool_name); 11677214Slling 1168789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 11697214Slling zc.zc_cookie = force; 11708211SGeorge.Wilson@Sun.COM zc.zc_guid = hardforce; 11717214Slling 11727214Slling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 11737214Slling switch (errno) { 11747214Slling case EXDEV: 11757214Slling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 11767214Slling "use '-f' to override the following errors:\n" 11777214Slling "'%s' has an active shared spare which could be" 11787214Slling " used by other pools once '%s' is exported."), 11797214Slling zhp->zpool_name, zhp->zpool_name); 11807214Slling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 11817214Slling msg)); 11827214Slling default: 11837214Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 11847214Slling msg)); 11857214Slling } 11867214Slling } 11877214Slling 1188789Sahrens return (0); 1189789Sahrens } 1190789Sahrens 11918211SGeorge.Wilson@Sun.COM int 11928211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force) 11938211SGeorge.Wilson@Sun.COM { 11948211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, force, B_FALSE)); 11958211SGeorge.Wilson@Sun.COM } 11968211SGeorge.Wilson@Sun.COM 11978211SGeorge.Wilson@Sun.COM int 11988211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp) 11998211SGeorge.Wilson@Sun.COM { 12008211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 12018211SGeorge.Wilson@Sun.COM } 12028211SGeorge.Wilson@Sun.COM 120310921STim.Haley@Sun.COM static void 120410921STim.Haley@Sun.COM zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 1205*12949SGeorge.Wilson@Sun.COM nvlist_t *config) 120610921STim.Haley@Sun.COM { 1207*12949SGeorge.Wilson@Sun.COM nvlist_t *nv = NULL; 120810921STim.Haley@Sun.COM uint64_t rewindto; 120910921STim.Haley@Sun.COM int64_t loss = -1; 121010921STim.Haley@Sun.COM struct tm t; 121110921STim.Haley@Sun.COM char timestr[128]; 121210921STim.Haley@Sun.COM 1213*12949SGeorge.Wilson@Sun.COM if (!hdl->libzfs_printerr || config == NULL) 1214*12949SGeorge.Wilson@Sun.COM return; 1215*12949SGeorge.Wilson@Sun.COM 1216*12949SGeorge.Wilson@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0) 121710921STim.Haley@Sun.COM return; 121810921STim.Haley@Sun.COM 1219*12949SGeorge.Wilson@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 122010921STim.Haley@Sun.COM return; 1221*12949SGeorge.Wilson@Sun.COM (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 122210921STim.Haley@Sun.COM 122310921STim.Haley@Sun.COM if (localtime_r((time_t *)&rewindto, &t) != NULL && 122410921STim.Haley@Sun.COM strftime(timestr, 128, 0, &t) != 0) { 122510921STim.Haley@Sun.COM if (dryrun) { 122610921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 122710921STim.Haley@Sun.COM "Would be able to return %s " 122810921STim.Haley@Sun.COM "to its state as of %s.\n"), 122910921STim.Haley@Sun.COM name, timestr); 123010921STim.Haley@Sun.COM } else { 123110921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 123210921STim.Haley@Sun.COM "Pool %s returned to its state as of %s.\n"), 123310921STim.Haley@Sun.COM name, timestr); 123410921STim.Haley@Sun.COM } 123510921STim.Haley@Sun.COM if (loss > 120) { 123610921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 123710921STim.Haley@Sun.COM "%s approximately %lld "), 123810921STim.Haley@Sun.COM dryrun ? "Would discard" : "Discarded", 123910921STim.Haley@Sun.COM (loss + 30) / 60); 124010921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 124110921STim.Haley@Sun.COM "minutes of transactions.\n")); 124210921STim.Haley@Sun.COM } else if (loss > 0) { 124310921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 124410921STim.Haley@Sun.COM "%s approximately %lld "), 124510921STim.Haley@Sun.COM dryrun ? "Would discard" : "Discarded", loss); 124610921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 124710921STim.Haley@Sun.COM "seconds of transactions.\n")); 124810921STim.Haley@Sun.COM } 124910921STim.Haley@Sun.COM } 125010921STim.Haley@Sun.COM } 125110921STim.Haley@Sun.COM 125210921STim.Haley@Sun.COM void 125310921STim.Haley@Sun.COM zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 125410921STim.Haley@Sun.COM nvlist_t *config) 125510921STim.Haley@Sun.COM { 1256*12949SGeorge.Wilson@Sun.COM nvlist_t *nv = NULL; 125710921STim.Haley@Sun.COM int64_t loss = -1; 125810921STim.Haley@Sun.COM uint64_t edata = UINT64_MAX; 125910921STim.Haley@Sun.COM uint64_t rewindto; 126010921STim.Haley@Sun.COM struct tm t; 126110921STim.Haley@Sun.COM char timestr[128]; 126210921STim.Haley@Sun.COM 126310921STim.Haley@Sun.COM if (!hdl->libzfs_printerr) 126410921STim.Haley@Sun.COM return; 126510921STim.Haley@Sun.COM 126610921STim.Haley@Sun.COM if (reason >= 0) 126710921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 126810921STim.Haley@Sun.COM else 126910921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, "\t")); 127010921STim.Haley@Sun.COM 127110921STim.Haley@Sun.COM /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 1272*12949SGeorge.Wilson@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1273*12949SGeorge.Wilson@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 127410921STim.Haley@Sun.COM goto no_info; 127510921STim.Haley@Sun.COM 1276*12949SGeorge.Wilson@Sun.COM (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1277*12949SGeorge.Wilson@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 127810921STim.Haley@Sun.COM &edata); 127910921STim.Haley@Sun.COM 128010921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 128110921STim.Haley@Sun.COM "Recovery is possible, but will result in some data loss.\n")); 128210921STim.Haley@Sun.COM 128310921STim.Haley@Sun.COM if (localtime_r((time_t *)&rewindto, &t) != NULL && 128410921STim.Haley@Sun.COM strftime(timestr, 128, 0, &t) != 0) { 128510921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 128610921STim.Haley@Sun.COM "\tReturning the pool to its state as of %s\n" 128710921STim.Haley@Sun.COM "\tshould correct the problem. "), 128810921STim.Haley@Sun.COM timestr); 128910921STim.Haley@Sun.COM } else { 129010921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 129110921STim.Haley@Sun.COM "\tReverting the pool to an earlier state " 129210921STim.Haley@Sun.COM "should correct the problem.\n\t")); 129310921STim.Haley@Sun.COM } 129410921STim.Haley@Sun.COM 129510921STim.Haley@Sun.COM if (loss > 120) { 129610921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 129710921STim.Haley@Sun.COM "Approximately %lld minutes of data\n" 129810921STim.Haley@Sun.COM "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 129910921STim.Haley@Sun.COM } else if (loss > 0) { 130010921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 130110921STim.Haley@Sun.COM "Approximately %lld seconds of data\n" 130210921STim.Haley@Sun.COM "\tmust be discarded, irreversibly. "), loss); 130310921STim.Haley@Sun.COM } 130410921STim.Haley@Sun.COM if (edata != 0 && edata != UINT64_MAX) { 130510921STim.Haley@Sun.COM if (edata == 1) { 130610921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 130710921STim.Haley@Sun.COM "After rewind, at least\n" 130810921STim.Haley@Sun.COM "\tone persistent user-data error will remain. ")); 130910921STim.Haley@Sun.COM } else { 131010921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 131110921STim.Haley@Sun.COM "After rewind, several\n" 131210921STim.Haley@Sun.COM "\tpersistent user-data errors will remain. ")); 131310921STim.Haley@Sun.COM } 131410921STim.Haley@Sun.COM } 131510921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 131611026STim.Haley@Sun.COM "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 131711026STim.Haley@Sun.COM reason >= 0 ? "clear" : "import", name); 131810921STim.Haley@Sun.COM 131910921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 132010921STim.Haley@Sun.COM "A scrub of the pool\n" 132110921STim.Haley@Sun.COM "\tis strongly recommended after recovery.\n")); 132210921STim.Haley@Sun.COM return; 132310921STim.Haley@Sun.COM 132410921STim.Haley@Sun.COM no_info: 132510921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 132610921STim.Haley@Sun.COM "Destroy and re-create the pool from\n\ta backup source.\n")); 132710921STim.Haley@Sun.COM } 132810921STim.Haley@Sun.COM 1329789Sahrens /* 13305094Slling * zpool_import() is a contracted interface. Should be kept the same 13315094Slling * if possible. 13325094Slling * 13335094Slling * Applications should use zpool_import_props() to import a pool with 13345094Slling * new properties value to be set. 1335789Sahrens */ 1336789Sahrens int 13372082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 13385094Slling char *altroot) 13395094Slling { 13405094Slling nvlist_t *props = NULL; 13415094Slling int ret; 13425094Slling 13435094Slling if (altroot != NULL) { 13445094Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 13455094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 13465094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 13475094Slling newname)); 13485094Slling } 13495094Slling 13505094Slling if (nvlist_add_string(props, 13518084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 13528084SGeorge.Wilson@Sun.COM nvlist_add_string(props, 13538084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 13545094Slling nvlist_free(props); 13555094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 13565094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 13575094Slling newname)); 13585094Slling } 13595094Slling } 13605094Slling 1361*12949SGeorge.Wilson@Sun.COM ret = zpool_import_props(hdl, config, newname, props, 1362*12949SGeorge.Wilson@Sun.COM ZFS_IMPORT_NORMAL); 13635094Slling if (props) 13645094Slling nvlist_free(props); 13655094Slling return (ret); 13665094Slling } 13675094Slling 1368*12949SGeorge.Wilson@Sun.COM static void 1369*12949SGeorge.Wilson@Sun.COM print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 1370*12949SGeorge.Wilson@Sun.COM int indent) 1371*12949SGeorge.Wilson@Sun.COM { 1372*12949SGeorge.Wilson@Sun.COM nvlist_t **child; 1373*12949SGeorge.Wilson@Sun.COM uint_t c, children; 1374*12949SGeorge.Wilson@Sun.COM char *vname; 1375*12949SGeorge.Wilson@Sun.COM uint64_t is_log = 0; 1376*12949SGeorge.Wilson@Sun.COM 1377*12949SGeorge.Wilson@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 1378*12949SGeorge.Wilson@Sun.COM &is_log); 1379*12949SGeorge.Wilson@Sun.COM 1380*12949SGeorge.Wilson@Sun.COM if (name != NULL) 1381*12949SGeorge.Wilson@Sun.COM (void) printf("\t%*s%s%s\n", indent, "", name, 1382*12949SGeorge.Wilson@Sun.COM is_log ? " [log]" : ""); 1383*12949SGeorge.Wilson@Sun.COM 1384*12949SGeorge.Wilson@Sun.COM if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1385*12949SGeorge.Wilson@Sun.COM &child, &children) != 0) 1386*12949SGeorge.Wilson@Sun.COM return; 1387*12949SGeorge.Wilson@Sun.COM 1388*12949SGeorge.Wilson@Sun.COM for (c = 0; c < children; c++) { 1389*12949SGeorge.Wilson@Sun.COM vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 1390*12949SGeorge.Wilson@Sun.COM print_vdev_tree(hdl, vname, child[c], indent + 2); 1391*12949SGeorge.Wilson@Sun.COM free(vname); 1392*12949SGeorge.Wilson@Sun.COM } 1393*12949SGeorge.Wilson@Sun.COM } 1394*12949SGeorge.Wilson@Sun.COM 13955094Slling /* 13965094Slling * Import the given pool using the known configuration and a list of 13975094Slling * properties to be set. The configuration should have come from 13985094Slling * zpool_find_import(). The 'newname' parameters control whether the pool 13995094Slling * is imported with a different name. 14005094Slling */ 14015094Slling int 14025094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1403*12949SGeorge.Wilson@Sun.COM nvlist_t *props, int flags) 1404789Sahrens { 14052676Seschrock zfs_cmd_t zc = { 0 }; 140610921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 1407*12949SGeorge.Wilson@Sun.COM nvlist_t *nv = NULL; 1408*12949SGeorge.Wilson@Sun.COM nvlist_t *nvinfo = NULL; 1409*12949SGeorge.Wilson@Sun.COM nvlist_t *missing = NULL; 1410789Sahrens char *thename; 1411789Sahrens char *origname; 1412789Sahrens int ret; 1413*12949SGeorge.Wilson@Sun.COM int error = 0; 14145094Slling char errbuf[1024]; 1415789Sahrens 1416789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1417789Sahrens &origname) == 0); 1418789Sahrens 14195094Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 14205094Slling "cannot import pool '%s'"), origname); 14215094Slling 1422789Sahrens if (newname != NULL) { 14232082Seschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 14243237Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 14252082Seschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 14262082Seschrock newname)); 1427789Sahrens thename = (char *)newname; 1428789Sahrens } else { 1429789Sahrens thename = origname; 1430789Sahrens } 1431789Sahrens 14325094Slling if (props) { 14335094Slling uint64_t version; 14345094Slling 14355094Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 14365094Slling &version) == 0); 14375094Slling 14387184Stimh if ((props = zpool_valid_proplist(hdl, origname, 14395320Slling props, version, B_TRUE, errbuf)) == NULL) { 14405094Slling return (-1); 14415320Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 14425320Slling nvlist_free(props); 14435094Slling return (-1); 14445320Slling } 14455094Slling } 1446789Sahrens 1447789Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1448789Sahrens 1449789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 14501544Seschrock &zc.zc_guid) == 0); 1451789Sahrens 14525320Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 14535320Slling nvlist_free(props); 14542082Seschrock return (-1); 14555320Slling } 1456*12949SGeorge.Wilson@Sun.COM if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 145710921STim.Haley@Sun.COM nvlist_free(props); 145810921STim.Haley@Sun.COM return (-1); 145910921STim.Haley@Sun.COM } 1460789Sahrens 1461*12949SGeorge.Wilson@Sun.COM zc.zc_cookie = flags; 1462*12949SGeorge.Wilson@Sun.COM while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 1463*12949SGeorge.Wilson@Sun.COM errno == ENOMEM) { 1464*12949SGeorge.Wilson@Sun.COM if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 1465*12949SGeorge.Wilson@Sun.COM zcmd_free_nvlists(&zc); 1466*12949SGeorge.Wilson@Sun.COM return (-1); 1467*12949SGeorge.Wilson@Sun.COM } 1468*12949SGeorge.Wilson@Sun.COM } 1469*12949SGeorge.Wilson@Sun.COM if (ret != 0) 1470*12949SGeorge.Wilson@Sun.COM error = errno; 1471*12949SGeorge.Wilson@Sun.COM 1472*12949SGeorge.Wilson@Sun.COM (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1473*12949SGeorge.Wilson@Sun.COM zpool_get_rewind_policy(config, &policy); 1474*12949SGeorge.Wilson@Sun.COM 1475*12949SGeorge.Wilson@Sun.COM if (error) { 1476789Sahrens char desc[1024]; 147710921STim.Haley@Sun.COM 147810921STim.Haley@Sun.COM /* 147910921STim.Haley@Sun.COM * Dry-run failed, but we print out what success 148010921STim.Haley@Sun.COM * looks like if we found a best txg 148110921STim.Haley@Sun.COM */ 1482*12949SGeorge.Wilson@Sun.COM if (policy.zrp_request & ZPOOL_TRY_REWIND) { 148310921STim.Haley@Sun.COM zpool_rewind_exclaim(hdl, newname ? origname : thename, 1484*12949SGeorge.Wilson@Sun.COM B_TRUE, nv); 1485*12949SGeorge.Wilson@Sun.COM nvlist_free(nv); 148610921STim.Haley@Sun.COM return (-1); 148710921STim.Haley@Sun.COM } 148810921STim.Haley@Sun.COM 1489789Sahrens if (newname == NULL) 1490789Sahrens (void) snprintf(desc, sizeof (desc), 1491789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1492789Sahrens thename); 1493789Sahrens else 1494789Sahrens (void) snprintf(desc, sizeof (desc), 1495789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1496789Sahrens origname, thename); 1497789Sahrens 1498*12949SGeorge.Wilson@Sun.COM switch (error) { 14991544Seschrock case ENOTSUP: 15001544Seschrock /* 15011544Seschrock * Unsupported version. 15021544Seschrock */ 15032082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 15041544Seschrock break; 15051544Seschrock 15062174Seschrock case EINVAL: 15072174Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 15082174Seschrock break; 15092174Seschrock 151011814SChris.Kirby@sun.com case EROFS: 151111814SChris.Kirby@sun.com zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 151211814SChris.Kirby@sun.com "one or more devices is read only")); 151311814SChris.Kirby@sun.com (void) zfs_error(hdl, EZFS_BADDEV, desc); 151411814SChris.Kirby@sun.com break; 151511814SChris.Kirby@sun.com 1516*12949SGeorge.Wilson@Sun.COM case ENXIO: 1517*12949SGeorge.Wilson@Sun.COM if (nv && nvlist_lookup_nvlist(nv, 1518*12949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1519*12949SGeorge.Wilson@Sun.COM nvlist_lookup_nvlist(nvinfo, 1520*12949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 1521*12949SGeorge.Wilson@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 1522*12949SGeorge.Wilson@Sun.COM "The devices below are missing, use " 1523*12949SGeorge.Wilson@Sun.COM "'-m' to import the pool anyway:\n")); 1524*12949SGeorge.Wilson@Sun.COM print_vdev_tree(hdl, NULL, missing, 2); 1525*12949SGeorge.Wilson@Sun.COM (void) printf("\n"); 1526*12949SGeorge.Wilson@Sun.COM } 1527*12949SGeorge.Wilson@Sun.COM (void) zpool_standard_error(hdl, error, desc); 1528*12949SGeorge.Wilson@Sun.COM break; 1529*12949SGeorge.Wilson@Sun.COM 1530*12949SGeorge.Wilson@Sun.COM case EEXIST: 1531*12949SGeorge.Wilson@Sun.COM (void) zpool_standard_error(hdl, error, desc); 1532*12949SGeorge.Wilson@Sun.COM break; 1533*12949SGeorge.Wilson@Sun.COM 1534789Sahrens default: 1535*12949SGeorge.Wilson@Sun.COM (void) zpool_standard_error(hdl, error, desc); 153610921STim.Haley@Sun.COM zpool_explain_recover(hdl, 1537*12949SGeorge.Wilson@Sun.COM newname ? origname : thename, -error, nv); 153810921STim.Haley@Sun.COM break; 1539789Sahrens } 1540789Sahrens 1541*12949SGeorge.Wilson@Sun.COM nvlist_free(nv); 1542789Sahrens ret = -1; 1543789Sahrens } else { 1544789Sahrens zpool_handle_t *zhp; 15454543Smarks 1546789Sahrens /* 1547789Sahrens * This should never fail, but play it safe anyway. 1548789Sahrens */ 154910588SEric.Taylor@Sun.COM if (zpool_open_silent(hdl, thename, &zhp) != 0) 15502142Seschrock ret = -1; 155110588SEric.Taylor@Sun.COM else if (zhp != NULL) 1552789Sahrens zpool_close(zhp); 155310921STim.Haley@Sun.COM if (policy.zrp_request & 155410921STim.Haley@Sun.COM (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 155510921STim.Haley@Sun.COM zpool_rewind_exclaim(hdl, newname ? origname : thename, 1556*12949SGeorge.Wilson@Sun.COM ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 155710921STim.Haley@Sun.COM } 1558*12949SGeorge.Wilson@Sun.COM nvlist_free(nv); 155910921STim.Haley@Sun.COM return (0); 1560789Sahrens } 1561789Sahrens 15622676Seschrock zcmd_free_nvlists(&zc); 15635320Slling nvlist_free(props); 15645320Slling 1565789Sahrens return (ret); 1566789Sahrens } 1567789Sahrens 1568789Sahrens /* 156912296SLin.Ling@Sun.COM * Scan the pool. 1570789Sahrens */ 1571789Sahrens int 157212296SLin.Ling@Sun.COM zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1573789Sahrens { 1574789Sahrens zfs_cmd_t zc = { 0 }; 1575789Sahrens char msg[1024]; 15762082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1577789Sahrens 1578789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 157912296SLin.Ling@Sun.COM zc.zc_cookie = func; 158012296SLin.Ling@Sun.COM 158112296SLin.Ling@Sun.COM if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 158212296SLin.Ling@Sun.COM (errno == ENOENT && func != POOL_SCAN_NONE)) 1583789Sahrens return (0); 1584789Sahrens 158512296SLin.Ling@Sun.COM if (func == POOL_SCAN_SCRUB) { 158612296SLin.Ling@Sun.COM (void) snprintf(msg, sizeof (msg), 158712296SLin.Ling@Sun.COM dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 158812296SLin.Ling@Sun.COM } else if (func == POOL_SCAN_NONE) { 158912296SLin.Ling@Sun.COM (void) snprintf(msg, sizeof (msg), 159012296SLin.Ling@Sun.COM dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 159112296SLin.Ling@Sun.COM zc.zc_name); 159212296SLin.Ling@Sun.COM } else { 159312296SLin.Ling@Sun.COM assert(!"unexpected result"); 159412296SLin.Ling@Sun.COM } 159512296SLin.Ling@Sun.COM 159612296SLin.Ling@Sun.COM if (errno == EBUSY) { 159712296SLin.Ling@Sun.COM nvlist_t *nvroot; 159812296SLin.Ling@Sun.COM pool_scan_stat_t *ps = NULL; 159912296SLin.Ling@Sun.COM uint_t psc; 160012296SLin.Ling@Sun.COM 160112296SLin.Ling@Sun.COM verify(nvlist_lookup_nvlist(zhp->zpool_config, 160212296SLin.Ling@Sun.COM ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 160312296SLin.Ling@Sun.COM (void) nvlist_lookup_uint64_array(nvroot, 160412296SLin.Ling@Sun.COM ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 160512296SLin.Ling@Sun.COM if (ps && ps->pss_func == POOL_SCAN_SCRUB) 160612296SLin.Ling@Sun.COM return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 160712296SLin.Ling@Sun.COM else 160812296SLin.Ling@Sun.COM return (zfs_error(hdl, EZFS_RESILVERING, msg)); 160912296SLin.Ling@Sun.COM } else if (errno == ENOENT) { 161012296SLin.Ling@Sun.COM return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 161112296SLin.Ling@Sun.COM } else { 16122082Seschrock return (zpool_standard_error(hdl, errno, msg)); 161312296SLin.Ling@Sun.COM } 1614789Sahrens } 1615789Sahrens 16162468Sek110237 /* 161712383SJohn.Harres@Sun.COM * This provides a very minimal check whether a given string is likely a 161812383SJohn.Harres@Sun.COM * c#t#d# style string. Users of this are expected to do their own 161912383SJohn.Harres@Sun.COM * verification of the s# part. 162012383SJohn.Harres@Sun.COM */ 162112383SJohn.Harres@Sun.COM #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 162212383SJohn.Harres@Sun.COM 162312383SJohn.Harres@Sun.COM /* 162412383SJohn.Harres@Sun.COM * More elaborate version for ones which may start with "/dev/dsk/" 162512383SJohn.Harres@Sun.COM * and the like. 162612383SJohn.Harres@Sun.COM */ 162712383SJohn.Harres@Sun.COM static int 162812383SJohn.Harres@Sun.COM ctd_check_path(char *str) { 162912383SJohn.Harres@Sun.COM /* 163012383SJohn.Harres@Sun.COM * If it starts with a slash, check the last component. 163112383SJohn.Harres@Sun.COM */ 163212383SJohn.Harres@Sun.COM if (str && str[0] == '/') { 163312383SJohn.Harres@Sun.COM char *tmp = strrchr(str, '/'); 163412383SJohn.Harres@Sun.COM 163512383SJohn.Harres@Sun.COM /* 163612383SJohn.Harres@Sun.COM * If it ends in "/old", check the second-to-last 163712383SJohn.Harres@Sun.COM * component of the string instead. 163812383SJohn.Harres@Sun.COM */ 163912383SJohn.Harres@Sun.COM if (tmp != str && strcmp(tmp, "/old") == 0) { 164012383SJohn.Harres@Sun.COM for (tmp--; *tmp != '/'; tmp--) 164112383SJohn.Harres@Sun.COM ; 164212383SJohn.Harres@Sun.COM } 164312383SJohn.Harres@Sun.COM str = tmp + 1; 164412383SJohn.Harres@Sun.COM } 164512383SJohn.Harres@Sun.COM return (CTD_CHECK(str)); 164612383SJohn.Harres@Sun.COM } 164712383SJohn.Harres@Sun.COM 164812383SJohn.Harres@Sun.COM /* 16499816SGeorge.Wilson@Sun.COM * Find a vdev that matches the search criteria specified. We use the 16509816SGeorge.Wilson@Sun.COM * the nvpair name to determine how we should look for the device. 16512468Sek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 16522468Sek110237 * spare; but FALSE if its an INUSE spare. 16532468Sek110237 */ 16542082Seschrock static nvlist_t * 16559816SGeorge.Wilson@Sun.COM vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 16569816SGeorge.Wilson@Sun.COM boolean_t *l2cache, boolean_t *log) 16571544Seschrock { 16581544Seschrock uint_t c, children; 16591544Seschrock nvlist_t **child; 16602082Seschrock nvlist_t *ret; 16617326SEric.Schrock@Sun.COM uint64_t is_log; 16629816SGeorge.Wilson@Sun.COM char *srchkey; 16639816SGeorge.Wilson@Sun.COM nvpair_t *pair = nvlist_next_nvpair(search, NULL); 16649816SGeorge.Wilson@Sun.COM 16659816SGeorge.Wilson@Sun.COM /* Nothing to look for */ 16669816SGeorge.Wilson@Sun.COM if (search == NULL || pair == NULL) 16679816SGeorge.Wilson@Sun.COM return (NULL); 16689816SGeorge.Wilson@Sun.COM 16699816SGeorge.Wilson@Sun.COM /* Obtain the key we will use to search */ 16709816SGeorge.Wilson@Sun.COM srchkey = nvpair_name(pair); 16719816SGeorge.Wilson@Sun.COM 16729816SGeorge.Wilson@Sun.COM switch (nvpair_type(pair)) { 16739816SGeorge.Wilson@Sun.COM case DATA_TYPE_UINT64: { 16749816SGeorge.Wilson@Sun.COM uint64_t srchval, theguid, present; 16759816SGeorge.Wilson@Sun.COM 16769816SGeorge.Wilson@Sun.COM verify(nvpair_value_uint64(pair, &srchval) == 0); 16779816SGeorge.Wilson@Sun.COM if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 16789816SGeorge.Wilson@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 16799816SGeorge.Wilson@Sun.COM &present) == 0) { 16809816SGeorge.Wilson@Sun.COM /* 16819816SGeorge.Wilson@Sun.COM * If the device has never been present since 16829816SGeorge.Wilson@Sun.COM * import, the only reliable way to match the 16839816SGeorge.Wilson@Sun.COM * vdev is by GUID. 16849816SGeorge.Wilson@Sun.COM */ 16859816SGeorge.Wilson@Sun.COM verify(nvlist_lookup_uint64(nv, 16869816SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_GUID, &theguid) == 0); 16879816SGeorge.Wilson@Sun.COM if (theguid == srchval) 16889816SGeorge.Wilson@Sun.COM return (nv); 16899816SGeorge.Wilson@Sun.COM } 16909816SGeorge.Wilson@Sun.COM } 16919816SGeorge.Wilson@Sun.COM break; 16929816SGeorge.Wilson@Sun.COM } 16939816SGeorge.Wilson@Sun.COM 16949816SGeorge.Wilson@Sun.COM case DATA_TYPE_STRING: { 16959816SGeorge.Wilson@Sun.COM char *srchval, *val; 16969816SGeorge.Wilson@Sun.COM 16979816SGeorge.Wilson@Sun.COM verify(nvpair_value_string(pair, &srchval) == 0); 16989816SGeorge.Wilson@Sun.COM if (nvlist_lookup_string(nv, srchkey, &val) != 0) 16999816SGeorge.Wilson@Sun.COM break; 17009816SGeorge.Wilson@Sun.COM 17011544Seschrock /* 170212383SJohn.Harres@Sun.COM * Search for the requested value. Special cases: 170312383SJohn.Harres@Sun.COM * 170412383SJohn.Harres@Sun.COM * - ZPOOL_CONFIG_PATH for whole disk entries. These end in 170512383SJohn.Harres@Sun.COM * "s0" or "s0/old". The "s0" part is hidden from the user, 170612383SJohn.Harres@Sun.COM * but included in the string, so this matches around it. 170712383SJohn.Harres@Sun.COM * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 170812383SJohn.Harres@Sun.COM * 170910594SGeorge.Wilson@Sun.COM * Otherwise, all other searches are simple string compares. 17101544Seschrock */ 171112383SJohn.Harres@Sun.COM if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 171212383SJohn.Harres@Sun.COM ctd_check_path(val)) { 17139816SGeorge.Wilson@Sun.COM uint64_t wholedisk = 0; 17149816SGeorge.Wilson@Sun.COM 17159816SGeorge.Wilson@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 17169816SGeorge.Wilson@Sun.COM &wholedisk); 17179816SGeorge.Wilson@Sun.COM if (wholedisk) { 171812383SJohn.Harres@Sun.COM int slen = strlen(srchval); 171912383SJohn.Harres@Sun.COM int vlen = strlen(val); 172012383SJohn.Harres@Sun.COM 172112383SJohn.Harres@Sun.COM if (slen != vlen - 2) 172212383SJohn.Harres@Sun.COM break; 172312383SJohn.Harres@Sun.COM 17249816SGeorge.Wilson@Sun.COM /* 172512383SJohn.Harres@Sun.COM * make_leaf_vdev() should only set 172612383SJohn.Harres@Sun.COM * wholedisk for ZPOOL_CONFIG_PATHs which 172712383SJohn.Harres@Sun.COM * will include "/dev/dsk/", giving plenty of 172812383SJohn.Harres@Sun.COM * room for the indices used next. 17299816SGeorge.Wilson@Sun.COM */ 173012383SJohn.Harres@Sun.COM ASSERT(vlen >= 6); 173112383SJohn.Harres@Sun.COM 173212383SJohn.Harres@Sun.COM /* 173312383SJohn.Harres@Sun.COM * strings identical except trailing "s0" 173412383SJohn.Harres@Sun.COM */ 173512383SJohn.Harres@Sun.COM if (strcmp(&val[vlen - 2], "s0") == 0 && 173612383SJohn.Harres@Sun.COM strncmp(srchval, val, slen) == 0) 17379816SGeorge.Wilson@Sun.COM return (nv); 173812383SJohn.Harres@Sun.COM 173912383SJohn.Harres@Sun.COM /* 174012383SJohn.Harres@Sun.COM * strings identical except trailing "s0/old" 174112383SJohn.Harres@Sun.COM */ 174212383SJohn.Harres@Sun.COM if (strcmp(&val[vlen - 6], "s0/old") == 0 && 174312383SJohn.Harres@Sun.COM strcmp(&srchval[slen - 4], "/old") == 0 && 174412383SJohn.Harres@Sun.COM strncmp(srchval, val, slen - 4) == 0) 174512383SJohn.Harres@Sun.COM return (nv); 174612383SJohn.Harres@Sun.COM 17479816SGeorge.Wilson@Sun.COM break; 17489816SGeorge.Wilson@Sun.COM } 174910594SGeorge.Wilson@Sun.COM } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 175010594SGeorge.Wilson@Sun.COM char *type, *idx, *end, *p; 175110594SGeorge.Wilson@Sun.COM uint64_t id, vdev_id; 175210594SGeorge.Wilson@Sun.COM 175310594SGeorge.Wilson@Sun.COM /* 175410594SGeorge.Wilson@Sun.COM * Determine our vdev type, keeping in mind 175510594SGeorge.Wilson@Sun.COM * that the srchval is composed of a type and 175610594SGeorge.Wilson@Sun.COM * vdev id pair (i.e. mirror-4). 175710594SGeorge.Wilson@Sun.COM */ 175810594SGeorge.Wilson@Sun.COM if ((type = strdup(srchval)) == NULL) 175910594SGeorge.Wilson@Sun.COM return (NULL); 176010594SGeorge.Wilson@Sun.COM 176110594SGeorge.Wilson@Sun.COM if ((p = strrchr(type, '-')) == NULL) { 176210594SGeorge.Wilson@Sun.COM free(type); 176310594SGeorge.Wilson@Sun.COM break; 176410594SGeorge.Wilson@Sun.COM } 176510594SGeorge.Wilson@Sun.COM idx = p + 1; 176610594SGeorge.Wilson@Sun.COM *p = '\0'; 176710594SGeorge.Wilson@Sun.COM 176810594SGeorge.Wilson@Sun.COM /* 176910594SGeorge.Wilson@Sun.COM * If the types don't match then keep looking. 177010594SGeorge.Wilson@Sun.COM */ 177110594SGeorge.Wilson@Sun.COM if (strncmp(val, type, strlen(val)) != 0) { 177210594SGeorge.Wilson@Sun.COM free(type); 177310594SGeorge.Wilson@Sun.COM break; 177410594SGeorge.Wilson@Sun.COM } 177510594SGeorge.Wilson@Sun.COM 177610594SGeorge.Wilson@Sun.COM verify(strncmp(type, VDEV_TYPE_RAIDZ, 177710594SGeorge.Wilson@Sun.COM strlen(VDEV_TYPE_RAIDZ)) == 0 || 177810594SGeorge.Wilson@Sun.COM strncmp(type, VDEV_TYPE_MIRROR, 177910594SGeorge.Wilson@Sun.COM strlen(VDEV_TYPE_MIRROR)) == 0); 178010594SGeorge.Wilson@Sun.COM verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 178110594SGeorge.Wilson@Sun.COM &id) == 0); 178210594SGeorge.Wilson@Sun.COM 178310594SGeorge.Wilson@Sun.COM errno = 0; 178410594SGeorge.Wilson@Sun.COM vdev_id = strtoull(idx, &end, 10); 178510594SGeorge.Wilson@Sun.COM 178610594SGeorge.Wilson@Sun.COM free(type); 178710594SGeorge.Wilson@Sun.COM if (errno != 0) 178810594SGeorge.Wilson@Sun.COM return (NULL); 178910594SGeorge.Wilson@Sun.COM 179010594SGeorge.Wilson@Sun.COM /* 179110594SGeorge.Wilson@Sun.COM * Now verify that we have the correct vdev id. 179210594SGeorge.Wilson@Sun.COM */ 179310594SGeorge.Wilson@Sun.COM if (vdev_id == id) 179410594SGeorge.Wilson@Sun.COM return (nv); 17959816SGeorge.Wilson@Sun.COM } 17969816SGeorge.Wilson@Sun.COM 17979816SGeorge.Wilson@Sun.COM /* 17989816SGeorge.Wilson@Sun.COM * Common case 17999816SGeorge.Wilson@Sun.COM */ 18009816SGeorge.Wilson@Sun.COM if (strcmp(srchval, val) == 0) 18012082Seschrock return (nv); 18029816SGeorge.Wilson@Sun.COM break; 18039816SGeorge.Wilson@Sun.COM } 18049816SGeorge.Wilson@Sun.COM 18059816SGeorge.Wilson@Sun.COM default: 18069816SGeorge.Wilson@Sun.COM break; 18071544Seschrock } 18081544Seschrock 18091544Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 18101544Seschrock &child, &children) != 0) 18112082Seschrock return (NULL); 18121544Seschrock 18137326SEric.Schrock@Sun.COM for (c = 0; c < children; c++) { 18149816SGeorge.Wilson@Sun.COM if ((ret = vdev_to_nvlist_iter(child[c], search, 18157326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 18167326SEric.Schrock@Sun.COM /* 18177326SEric.Schrock@Sun.COM * The 'is_log' value is only set for the toplevel 18187326SEric.Schrock@Sun.COM * vdev, not the leaf vdevs. So we always lookup the 18197326SEric.Schrock@Sun.COM * log device from the root of the vdev tree (where 18207326SEric.Schrock@Sun.COM * 'log' is non-NULL). 18217326SEric.Schrock@Sun.COM */ 18227326SEric.Schrock@Sun.COM if (log != NULL && 18237326SEric.Schrock@Sun.COM nvlist_lookup_uint64(child[c], 18247326SEric.Schrock@Sun.COM ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 18257326SEric.Schrock@Sun.COM is_log) { 18267326SEric.Schrock@Sun.COM *log = B_TRUE; 18277326SEric.Schrock@Sun.COM } 18281544Seschrock return (ret); 18297326SEric.Schrock@Sun.COM } 18307326SEric.Schrock@Sun.COM } 18311544Seschrock 18322082Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 18332082Seschrock &child, &children) == 0) { 18342082Seschrock for (c = 0; c < children; c++) { 18359816SGeorge.Wilson@Sun.COM if ((ret = vdev_to_nvlist_iter(child[c], search, 18367326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 18372468Sek110237 *avail_spare = B_TRUE; 18382082Seschrock return (ret); 18392082Seschrock } 18402082Seschrock } 18412082Seschrock } 18422082Seschrock 18435450Sbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 18445450Sbrendan &child, &children) == 0) { 18455450Sbrendan for (c = 0; c < children; c++) { 18469816SGeorge.Wilson@Sun.COM if ((ret = vdev_to_nvlist_iter(child[c], search, 18477326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 18485450Sbrendan *l2cache = B_TRUE; 18495450Sbrendan return (ret); 18505450Sbrendan } 18515450Sbrendan } 18525450Sbrendan } 18535450Sbrendan 18542082Seschrock return (NULL); 18551544Seschrock } 18561544Seschrock 18579816SGeorge.Wilson@Sun.COM /* 18589816SGeorge.Wilson@Sun.COM * Given a physical path (minus the "/devices" prefix), find the 18599816SGeorge.Wilson@Sun.COM * associated vdev. 18609816SGeorge.Wilson@Sun.COM */ 18619816SGeorge.Wilson@Sun.COM nvlist_t * 18629816SGeorge.Wilson@Sun.COM zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 18639816SGeorge.Wilson@Sun.COM boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 18649816SGeorge.Wilson@Sun.COM { 18659816SGeorge.Wilson@Sun.COM nvlist_t *search, *nvroot, *ret; 18669816SGeorge.Wilson@Sun.COM 18679816SGeorge.Wilson@Sun.COM verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 18689816SGeorge.Wilson@Sun.COM verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 18699816SGeorge.Wilson@Sun.COM 18709816SGeorge.Wilson@Sun.COM verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 18719816SGeorge.Wilson@Sun.COM &nvroot) == 0); 18729816SGeorge.Wilson@Sun.COM 18739816SGeorge.Wilson@Sun.COM *avail_spare = B_FALSE; 18749816SGeorge.Wilson@Sun.COM ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 18759816SGeorge.Wilson@Sun.COM nvlist_free(search); 18769816SGeorge.Wilson@Sun.COM 18779816SGeorge.Wilson@Sun.COM return (ret); 18789816SGeorge.Wilson@Sun.COM } 18799816SGeorge.Wilson@Sun.COM 188010594SGeorge.Wilson@Sun.COM /* 188110594SGeorge.Wilson@Sun.COM * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 188210594SGeorge.Wilson@Sun.COM */ 188310594SGeorge.Wilson@Sun.COM boolean_t 188410594SGeorge.Wilson@Sun.COM zpool_vdev_is_interior(const char *name) 188510594SGeorge.Wilson@Sun.COM { 188610594SGeorge.Wilson@Sun.COM if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 188710594SGeorge.Wilson@Sun.COM strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 188810594SGeorge.Wilson@Sun.COM return (B_TRUE); 188910594SGeorge.Wilson@Sun.COM return (B_FALSE); 189010594SGeorge.Wilson@Sun.COM } 189110594SGeorge.Wilson@Sun.COM 18922082Seschrock nvlist_t * 18935450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 18947326SEric.Schrock@Sun.COM boolean_t *l2cache, boolean_t *log) 18951544Seschrock { 18961544Seschrock char buf[MAXPATHLEN]; 18971544Seschrock char *end; 18989816SGeorge.Wilson@Sun.COM nvlist_t *nvroot, *search, *ret; 18991544Seschrock uint64_t guid; 19001544Seschrock 19019816SGeorge.Wilson@Sun.COM verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 19029816SGeorge.Wilson@Sun.COM 19031613Seschrock guid = strtoull(path, &end, 10); 19041544Seschrock if (guid != 0 && *end == '\0') { 19059816SGeorge.Wilson@Sun.COM verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 190610594SGeorge.Wilson@Sun.COM } else if (zpool_vdev_is_interior(path)) { 190710594SGeorge.Wilson@Sun.COM verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 19081544Seschrock } else if (path[0] != '/') { 19091544Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 19109816SGeorge.Wilson@Sun.COM verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 19111544Seschrock } else { 19129816SGeorge.Wilson@Sun.COM verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 19131544Seschrock } 19141544Seschrock 19151544Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 19161544Seschrock &nvroot) == 0); 19171544Seschrock 19182468Sek110237 *avail_spare = B_FALSE; 19195450Sbrendan *l2cache = B_FALSE; 19207326SEric.Schrock@Sun.COM if (log != NULL) 19217326SEric.Schrock@Sun.COM *log = B_FALSE; 19229816SGeorge.Wilson@Sun.COM ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 19239816SGeorge.Wilson@Sun.COM nvlist_free(search); 19249816SGeorge.Wilson@Sun.COM 19259816SGeorge.Wilson@Sun.COM return (ret); 19262468Sek110237 } 19272468Sek110237 19287656SSherry.Moore@Sun.COM static int 19297656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv) 19307656SSherry.Moore@Sun.COM { 19317656SSherry.Moore@Sun.COM uint64_t ival; 19327656SSherry.Moore@Sun.COM 19337656SSherry.Moore@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 19347656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 19357656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 19367656SSherry.Moore@Sun.COM return (0); 19377656SSherry.Moore@Sun.COM 19387656SSherry.Moore@Sun.COM return (1); 19397656SSherry.Moore@Sun.COM } 19407656SSherry.Moore@Sun.COM 19417656SSherry.Moore@Sun.COM /* 19429790SLin.Ling@Sun.COM * Helper function for zpool_get_physpaths(). 19437656SSherry.Moore@Sun.COM */ 19449160SSherry.Moore@Sun.COM static int 19459790SLin.Ling@Sun.COM vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 19469160SSherry.Moore@Sun.COM size_t *bytes_written) 19477656SSherry.Moore@Sun.COM { 19489160SSherry.Moore@Sun.COM size_t bytes_left, pos, rsz; 19499160SSherry.Moore@Sun.COM char *tmppath; 19509160SSherry.Moore@Sun.COM const char *format; 19519160SSherry.Moore@Sun.COM 19529160SSherry.Moore@Sun.COM if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 19539160SSherry.Moore@Sun.COM &tmppath) != 0) 19549160SSherry.Moore@Sun.COM return (EZFS_NODEVICE); 19559160SSherry.Moore@Sun.COM 19569160SSherry.Moore@Sun.COM pos = *bytes_written; 19579160SSherry.Moore@Sun.COM bytes_left = physpath_size - pos; 19589160SSherry.Moore@Sun.COM format = (pos == 0) ? "%s" : " %s"; 19599160SSherry.Moore@Sun.COM 19609160SSherry.Moore@Sun.COM rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 19619160SSherry.Moore@Sun.COM *bytes_written += rsz; 19629160SSherry.Moore@Sun.COM 19639160SSherry.Moore@Sun.COM if (rsz >= bytes_left) { 19649160SSherry.Moore@Sun.COM /* if physpath was not copied properly, clear it */ 19659160SSherry.Moore@Sun.COM if (bytes_left != 0) { 19669160SSherry.Moore@Sun.COM physpath[pos] = 0; 19679160SSherry.Moore@Sun.COM } 19689160SSherry.Moore@Sun.COM return (EZFS_NOSPC); 19699160SSherry.Moore@Sun.COM } 19709160SSherry.Moore@Sun.COM return (0); 19719160SSherry.Moore@Sun.COM } 19729160SSherry.Moore@Sun.COM 19739790SLin.Ling@Sun.COM static int 19749790SLin.Ling@Sun.COM vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 19759790SLin.Ling@Sun.COM size_t *rsz, boolean_t is_spare) 19769790SLin.Ling@Sun.COM { 19779790SLin.Ling@Sun.COM char *type; 19789790SLin.Ling@Sun.COM int ret; 19799790SLin.Ling@Sun.COM 19809790SLin.Ling@Sun.COM if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 19819790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 19829790SLin.Ling@Sun.COM 19839790SLin.Ling@Sun.COM if (strcmp(type, VDEV_TYPE_DISK) == 0) { 19849790SLin.Ling@Sun.COM /* 19859790SLin.Ling@Sun.COM * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 19869790SLin.Ling@Sun.COM * For a spare vdev, we only want to boot from the active 19879790SLin.Ling@Sun.COM * spare device. 19889790SLin.Ling@Sun.COM */ 19899790SLin.Ling@Sun.COM if (is_spare) { 19909790SLin.Ling@Sun.COM uint64_t spare = 0; 19919790SLin.Ling@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 19929790SLin.Ling@Sun.COM &spare); 19939790SLin.Ling@Sun.COM if (!spare) 19949790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 19959790SLin.Ling@Sun.COM } 19969790SLin.Ling@Sun.COM 19979790SLin.Ling@Sun.COM if (vdev_online(nv)) { 19989790SLin.Ling@Sun.COM if ((ret = vdev_get_one_physpath(nv, physpath, 19999790SLin.Ling@Sun.COM phypath_size, rsz)) != 0) 20009790SLin.Ling@Sun.COM return (ret); 20019790SLin.Ling@Sun.COM } 20029790SLin.Ling@Sun.COM } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 20039790SLin.Ling@Sun.COM strcmp(type, VDEV_TYPE_REPLACING) == 0 || 20049790SLin.Ling@Sun.COM (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 20059790SLin.Ling@Sun.COM nvlist_t **child; 20069790SLin.Ling@Sun.COM uint_t count; 20079790SLin.Ling@Sun.COM int i, ret; 20089790SLin.Ling@Sun.COM 20099790SLin.Ling@Sun.COM if (nvlist_lookup_nvlist_array(nv, 20109790SLin.Ling@Sun.COM ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 20119790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 20129790SLin.Ling@Sun.COM 20139790SLin.Ling@Sun.COM for (i = 0; i < count; i++) { 20149790SLin.Ling@Sun.COM ret = vdev_get_physpaths(child[i], physpath, 20159790SLin.Ling@Sun.COM phypath_size, rsz, is_spare); 20169790SLin.Ling@Sun.COM if (ret == EZFS_NOSPC) 20179790SLin.Ling@Sun.COM return (ret); 20189790SLin.Ling@Sun.COM } 20199790SLin.Ling@Sun.COM } 20209790SLin.Ling@Sun.COM 20219790SLin.Ling@Sun.COM return (EZFS_POOL_INVALARG); 20229790SLin.Ling@Sun.COM } 20239790SLin.Ling@Sun.COM 20249160SSherry.Moore@Sun.COM /* 20259160SSherry.Moore@Sun.COM * Get phys_path for a root pool config. 20269160SSherry.Moore@Sun.COM * Return 0 on success; non-zero on failure. 20279160SSherry.Moore@Sun.COM */ 20289160SSherry.Moore@Sun.COM static int 20299160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 20309160SSherry.Moore@Sun.COM { 20319160SSherry.Moore@Sun.COM size_t rsz; 20327656SSherry.Moore@Sun.COM nvlist_t *vdev_root; 20337656SSherry.Moore@Sun.COM nvlist_t **child; 20347656SSherry.Moore@Sun.COM uint_t count; 20359160SSherry.Moore@Sun.COM char *type; 20369160SSherry.Moore@Sun.COM 20379160SSherry.Moore@Sun.COM rsz = 0; 20389160SSherry.Moore@Sun.COM 20399160SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 20409160SSherry.Moore@Sun.COM &vdev_root) != 0) 20419160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 20429160SSherry.Moore@Sun.COM 20439160SSherry.Moore@Sun.COM if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 20449160SSherry.Moore@Sun.COM nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 20459160SSherry.Moore@Sun.COM &child, &count) != 0) 20469160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 20477656SSherry.Moore@Sun.COM 20487656SSherry.Moore@Sun.COM /* 20499160SSherry.Moore@Sun.COM * root pool can not have EFI labeled disks and can only have 20509160SSherry.Moore@Sun.COM * a single top-level vdev. 20517656SSherry.Moore@Sun.COM */ 20529160SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 20539160SSherry.Moore@Sun.COM pool_uses_efi(vdev_root)) 20549160SSherry.Moore@Sun.COM return (EZFS_POOL_INVALARG); 20559160SSherry.Moore@Sun.COM 20569790SLin.Ling@Sun.COM (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 20579790SLin.Ling@Sun.COM B_FALSE); 20587656SSherry.Moore@Sun.COM 20599160SSherry.Moore@Sun.COM /* No online devices */ 20609160SSherry.Moore@Sun.COM if (rsz == 0) 20619160SSherry.Moore@Sun.COM return (EZFS_NODEVICE); 20629160SSherry.Moore@Sun.COM 20637656SSherry.Moore@Sun.COM return (0); 20647656SSherry.Moore@Sun.COM } 20657656SSherry.Moore@Sun.COM 20662468Sek110237 /* 20679160SSherry.Moore@Sun.COM * Get phys_path for a root pool 20689160SSherry.Moore@Sun.COM * Return 0 on success; non-zero on failure. 20699160SSherry.Moore@Sun.COM */ 20709160SSherry.Moore@Sun.COM int 20719160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 20729160SSherry.Moore@Sun.COM { 20739160SSherry.Moore@Sun.COM return (zpool_get_config_physpath(zhp->zpool_config, physpath, 20749160SSherry.Moore@Sun.COM phypath_size)); 20759160SSherry.Moore@Sun.COM } 20769160SSherry.Moore@Sun.COM 20779160SSherry.Moore@Sun.COM /* 20789816SGeorge.Wilson@Sun.COM * If the device has being dynamically expanded then we need to relabel 20799816SGeorge.Wilson@Sun.COM * the disk to use the new unallocated space. 20809816SGeorge.Wilson@Sun.COM */ 20819816SGeorge.Wilson@Sun.COM static int 20829816SGeorge.Wilson@Sun.COM zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 20839816SGeorge.Wilson@Sun.COM { 20849816SGeorge.Wilson@Sun.COM char path[MAXPATHLEN]; 20859816SGeorge.Wilson@Sun.COM char errbuf[1024]; 20869816SGeorge.Wilson@Sun.COM int fd, error; 20879816SGeorge.Wilson@Sun.COM int (*_efi_use_whole_disk)(int); 20889816SGeorge.Wilson@Sun.COM 20899816SGeorge.Wilson@Sun.COM if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 20909816SGeorge.Wilson@Sun.COM "efi_use_whole_disk")) == NULL) 20919816SGeorge.Wilson@Sun.COM return (-1); 20929816SGeorge.Wilson@Sun.COM 20939816SGeorge.Wilson@Sun.COM (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name); 20949816SGeorge.Wilson@Sun.COM 20959816SGeorge.Wilson@Sun.COM if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 20969816SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 20979816SGeorge.Wilson@Sun.COM "relabel '%s': unable to open device"), name); 20989816SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 20999816SGeorge.Wilson@Sun.COM } 21009816SGeorge.Wilson@Sun.COM 21019816SGeorge.Wilson@Sun.COM /* 21029816SGeorge.Wilson@Sun.COM * It's possible that we might encounter an error if the device 21039816SGeorge.Wilson@Sun.COM * does not have any unallocated space left. If so, we simply 21049816SGeorge.Wilson@Sun.COM * ignore that error and continue on. 21059816SGeorge.Wilson@Sun.COM */ 21069816SGeorge.Wilson@Sun.COM error = _efi_use_whole_disk(fd); 21079816SGeorge.Wilson@Sun.COM (void) close(fd); 21089816SGeorge.Wilson@Sun.COM if (error && error != VT_ENOSPC) { 21099816SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 21109816SGeorge.Wilson@Sun.COM "relabel '%s': unable to read disk capacity"), name); 21119816SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 21129816SGeorge.Wilson@Sun.COM } 21139816SGeorge.Wilson@Sun.COM return (0); 21149816SGeorge.Wilson@Sun.COM } 21159816SGeorge.Wilson@Sun.COM 21169816SGeorge.Wilson@Sun.COM /* 21174451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 21184451Seschrock * ZFS_ONLINE_* flags. 2119789Sahrens */ 2120789Sahrens int 21214451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 21224451Seschrock vdev_state_t *newstate) 2123789Sahrens { 2124789Sahrens zfs_cmd_t zc = { 0 }; 2125789Sahrens char msg[1024]; 21262082Seschrock nvlist_t *tgt; 21279816SGeorge.Wilson@Sun.COM boolean_t avail_spare, l2cache, islog; 21282082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2129789Sahrens 21309816SGeorge.Wilson@Sun.COM if (flags & ZFS_ONLINE_EXPAND) { 21319816SGeorge.Wilson@Sun.COM (void) snprintf(msg, sizeof (msg), 21329816SGeorge.Wilson@Sun.COM dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 21339816SGeorge.Wilson@Sun.COM } else { 21349816SGeorge.Wilson@Sun.COM (void) snprintf(msg, sizeof (msg), 21359816SGeorge.Wilson@Sun.COM dgettext(TEXT_DOMAIN, "cannot online %s"), path); 21369816SGeorge.Wilson@Sun.COM } 2137789Sahrens 21381544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21397326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 21409816SGeorge.Wilson@Sun.COM &islog)) == NULL) 21412082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2142789Sahrens 21432468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 21442468Sek110237 214510817SEric.Schrock@Sun.COM if (avail_spare) 21462082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 21472082Seschrock 21489816SGeorge.Wilson@Sun.COM if (flags & ZFS_ONLINE_EXPAND || 21499816SGeorge.Wilson@Sun.COM zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 21509816SGeorge.Wilson@Sun.COM char *pathname = NULL; 21519816SGeorge.Wilson@Sun.COM uint64_t wholedisk = 0; 21529816SGeorge.Wilson@Sun.COM 21539816SGeorge.Wilson@Sun.COM (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 21549816SGeorge.Wilson@Sun.COM &wholedisk); 21559816SGeorge.Wilson@Sun.COM verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 21569816SGeorge.Wilson@Sun.COM &pathname) == 0); 21579816SGeorge.Wilson@Sun.COM 21589816SGeorge.Wilson@Sun.COM /* 21599816SGeorge.Wilson@Sun.COM * XXX - L2ARC 1.0 devices can't support expansion. 21609816SGeorge.Wilson@Sun.COM */ 21619816SGeorge.Wilson@Sun.COM if (l2cache) { 21629816SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21639816SGeorge.Wilson@Sun.COM "cannot expand cache devices")); 21649816SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 21659816SGeorge.Wilson@Sun.COM } 21669816SGeorge.Wilson@Sun.COM 21679816SGeorge.Wilson@Sun.COM if (wholedisk) { 21689816SGeorge.Wilson@Sun.COM pathname += strlen(DISK_ROOT) + 1; 21699816SGeorge.Wilson@Sun.COM (void) zpool_relabel_disk(zhp->zpool_hdl, pathname); 21709816SGeorge.Wilson@Sun.COM } 21719816SGeorge.Wilson@Sun.COM } 21729816SGeorge.Wilson@Sun.COM 21734451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 21744451Seschrock zc.zc_obj = flags; 21754451Seschrock 217611422SMark.Musante@Sun.COM if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 217711422SMark.Musante@Sun.COM if (errno == EINVAL) { 217811422SMark.Musante@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 217911422SMark.Musante@Sun.COM "from this pool into a new one. Use '%s' " 218011422SMark.Musante@Sun.COM "instead"), "zpool detach"); 218111422SMark.Musante@Sun.COM return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 218211422SMark.Musante@Sun.COM } 21834451Seschrock return (zpool_standard_error(hdl, errno, msg)); 218411422SMark.Musante@Sun.COM } 21854451Seschrock 21864451Seschrock *newstate = zc.zc_cookie; 21874451Seschrock return (0); 2188789Sahrens } 2189789Sahrens 2190789Sahrens /* 2191789Sahrens * Take the specified vdev offline 2192789Sahrens */ 2193789Sahrens int 21944451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2195789Sahrens { 2196789Sahrens zfs_cmd_t zc = { 0 }; 2197789Sahrens char msg[1024]; 21982082Seschrock nvlist_t *tgt; 21995450Sbrendan boolean_t avail_spare, l2cache; 22002082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2201789Sahrens 22021544Seschrock (void) snprintf(msg, sizeof (msg), 22031544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 22041544Seschrock 2205789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 22067326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 22077326SEric.Schrock@Sun.COM NULL)) == NULL) 22082082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 22092082Seschrock 22102468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 22112468Sek110237 221210817SEric.Schrock@Sun.COM if (avail_spare) 22132082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 22142082Seschrock 22154451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 22164451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 22171485Slling 22184543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2219789Sahrens return (0); 2220789Sahrens 2221789Sahrens switch (errno) { 22222082Seschrock case EBUSY: 2223789Sahrens 2224789Sahrens /* 2225789Sahrens * There are no other replicas of this device. 2226789Sahrens */ 22272082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 22282082Seschrock 22299701SGeorge.Wilson@Sun.COM case EEXIST: 22309701SGeorge.Wilson@Sun.COM /* 22319701SGeorge.Wilson@Sun.COM * The log device has unplayed logs 22329701SGeorge.Wilson@Sun.COM */ 22339701SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 22349701SGeorge.Wilson@Sun.COM 22352082Seschrock default: 22362082Seschrock return (zpool_standard_error(hdl, errno, msg)); 22372082Seschrock } 22382082Seschrock } 2239789Sahrens 22402082Seschrock /* 22414451Seschrock * Mark the given vdev faulted. 22424451Seschrock */ 22434451Seschrock int 224410817SEric.Schrock@Sun.COM zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 22454451Seschrock { 22464451Seschrock zfs_cmd_t zc = { 0 }; 22474451Seschrock char msg[1024]; 22484451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 22494451Seschrock 22504451Seschrock (void) snprintf(msg, sizeof (msg), 22514451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 22524451Seschrock 22534451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 22544451Seschrock zc.zc_guid = guid; 22554451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 225610817SEric.Schrock@Sun.COM zc.zc_obj = aux; 22574451Seschrock 22584451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 22594451Seschrock return (0); 22604451Seschrock 22614451Seschrock switch (errno) { 22624451Seschrock case EBUSY: 22634451Seschrock 22644451Seschrock /* 22654451Seschrock * There are no other replicas of this device. 22664451Seschrock */ 22674451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 22684451Seschrock 22694451Seschrock default: 22704451Seschrock return (zpool_standard_error(hdl, errno, msg)); 22714451Seschrock } 22724451Seschrock 22734451Seschrock } 22744451Seschrock 22754451Seschrock /* 22764451Seschrock * Mark the given vdev degraded. 22774451Seschrock */ 22784451Seschrock int 227910817SEric.Schrock@Sun.COM zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 22804451Seschrock { 22814451Seschrock zfs_cmd_t zc = { 0 }; 22824451Seschrock char msg[1024]; 22834451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 22844451Seschrock 22854451Seschrock (void) snprintf(msg, sizeof (msg), 22864451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 22874451Seschrock 22884451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 22894451Seschrock zc.zc_guid = guid; 22904451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 229110817SEric.Schrock@Sun.COM zc.zc_obj = aux; 22924451Seschrock 22934451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 22944451Seschrock return (0); 22954451Seschrock 22964451Seschrock return (zpool_standard_error(hdl, errno, msg)); 22974451Seschrock } 22984451Seschrock 22994451Seschrock /* 23002082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 23012082Seschrock * a hot spare. 23022082Seschrock */ 23032082Seschrock static boolean_t 23042082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 23052082Seschrock { 23062082Seschrock nvlist_t **child; 23072082Seschrock uint_t c, children; 23082082Seschrock char *type; 23092082Seschrock 23102082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 23112082Seschrock &children) == 0) { 23122082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 23132082Seschrock &type) == 0); 23142082Seschrock 23152082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 23162082Seschrock children == 2 && child[which] == tgt) 23172082Seschrock return (B_TRUE); 23182082Seschrock 23192082Seschrock for (c = 0; c < children; c++) 23202082Seschrock if (is_replacing_spare(child[c], tgt, which)) 23212082Seschrock return (B_TRUE); 2322789Sahrens } 23232082Seschrock 23242082Seschrock return (B_FALSE); 2325789Sahrens } 2326789Sahrens 2327789Sahrens /* 2328789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 23294527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2330789Sahrens */ 2331789Sahrens int 2332789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2333789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2334789Sahrens { 2335789Sahrens zfs_cmd_t zc = { 0 }; 2336789Sahrens char msg[1024]; 2337789Sahrens int ret; 23382082Seschrock nvlist_t *tgt; 23397326SEric.Schrock@Sun.COM boolean_t avail_spare, l2cache, islog; 23407326SEric.Schrock@Sun.COM uint64_t val; 23417041Seschrock char *path, *newname; 23422082Seschrock nvlist_t **child; 23432082Seschrock uint_t children; 23442082Seschrock nvlist_t *config_root; 23452082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 23467965SGeorge.Wilson@Sun.COM boolean_t rootpool = pool_is_bootable(zhp); 2347789Sahrens 23481544Seschrock if (replacing) 23491544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 23501544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 23511544Seschrock else 23521544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 23531544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 23541544Seschrock 23557965SGeorge.Wilson@Sun.COM /* 23567965SGeorge.Wilson@Sun.COM * If this is a root pool, make sure that we're not attaching an 23577965SGeorge.Wilson@Sun.COM * EFI labeled device. 23587965SGeorge.Wilson@Sun.COM */ 23597965SGeorge.Wilson@Sun.COM if (rootpool && pool_uses_efi(nvroot)) { 23607965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23617965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root pools.")); 23627965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 23637965SGeorge.Wilson@Sun.COM } 23647965SGeorge.Wilson@Sun.COM 2365789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 23667326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 23677326SEric.Schrock@Sun.COM &islog)) == 0) 23682082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 23692082Seschrock 23702468Sek110237 if (avail_spare) 23712082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 23722082Seschrock 23735450Sbrendan if (l2cache) 23745450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 23755450Sbrendan 23762082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 23772082Seschrock zc.zc_cookie = replacing; 23782082Seschrock 23792082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 23802082Seschrock &child, &children) != 0 || children != 1) { 23812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23822082Seschrock "new device must be a single disk")); 23832082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 23841544Seschrock } 23852082Seschrock 23862082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 23872082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 23882082Seschrock 238910594SGeorge.Wilson@Sun.COM if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 23907041Seschrock return (-1); 23917041Seschrock 23922082Seschrock /* 23932082Seschrock * If the target is a hot spare that has been swapped in, we can only 23942082Seschrock * replace it with another hot spare. 23952082Seschrock */ 23962082Seschrock if (replacing && 23972082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 23987326SEric.Schrock@Sun.COM (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 23997326SEric.Schrock@Sun.COM NULL) == NULL || !avail_spare) && 24007326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 1)) { 24012082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24022082Seschrock "can only be replaced by another hot spare")); 24037041Seschrock free(newname); 24042082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 24052082Seschrock } 24062082Seschrock 24072082Seschrock /* 24082082Seschrock * If we are attempting to replace a spare, it canot be applied to an 24092082Seschrock * already spared device. 24102082Seschrock */ 24112082Seschrock if (replacing && 24122082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 24137326SEric.Schrock@Sun.COM zpool_find_vdev(zhp, newname, &avail_spare, 24147326SEric.Schrock@Sun.COM &l2cache, NULL) != NULL && avail_spare && 24157326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 0)) { 24162082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24172082Seschrock "device has already been replaced with a spare")); 24187041Seschrock free(newname); 24192082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 24202082Seschrock } 2421789Sahrens 24227041Seschrock free(newname); 24237041Seschrock 24245094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 24252082Seschrock return (-1); 2426789Sahrens 24274543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2428789Sahrens 24292676Seschrock zcmd_free_nvlists(&zc); 2430789Sahrens 24317965SGeorge.Wilson@Sun.COM if (ret == 0) { 24327965SGeorge.Wilson@Sun.COM if (rootpool) { 24337965SGeorge.Wilson@Sun.COM /* 24349790SLin.Ling@Sun.COM * XXX need a better way to prevent user from 24359790SLin.Ling@Sun.COM * booting up a half-baked vdev. 24369790SLin.Ling@Sun.COM */ 24379790SLin.Ling@Sun.COM (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 24389790SLin.Ling@Sun.COM "sure to wait until resilver is done " 24399790SLin.Ling@Sun.COM "before rebooting.\n")); 24407965SGeorge.Wilson@Sun.COM } 2441789Sahrens return (0); 24427965SGeorge.Wilson@Sun.COM } 2443789Sahrens 2444789Sahrens switch (errno) { 24451544Seschrock case ENOTSUP: 2446789Sahrens /* 2447789Sahrens * Can't attach to or replace this type of vdev. 2448789Sahrens */ 24494527Sperrin if (replacing) { 24507326SEric.Schrock@Sun.COM if (islog) 24514527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24524527Sperrin "cannot replace a log with a spare")); 24534527Sperrin else 24544527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24554527Sperrin "cannot replace a replacing device")); 24564527Sperrin } else { 24572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24582082Seschrock "can only attach to mirrors and top-level " 24592082Seschrock "disks")); 24604527Sperrin } 24612082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2462789Sahrens break; 2463789Sahrens 24641544Seschrock case EINVAL: 2465789Sahrens /* 2466789Sahrens * The new device must be a single disk. 2467789Sahrens */ 24682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24692082Seschrock "new device must be a single disk")); 24702082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2471789Sahrens break; 2472789Sahrens 24731544Seschrock case EBUSY: 24742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 24752082Seschrock new_disk); 24762082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2477789Sahrens break; 2478789Sahrens 24791544Seschrock case EOVERFLOW: 2480789Sahrens /* 2481789Sahrens * The new device is too small. 2482789Sahrens */ 24832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24842082Seschrock "device is too small")); 24852082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2486789Sahrens break; 2487789Sahrens 24881544Seschrock case EDOM: 2489789Sahrens /* 2490789Sahrens * The new device has a different alignment requirement. 2491789Sahrens */ 24922082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24932082Seschrock "devices have different sector alignment")); 24942082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2495789Sahrens break; 2496789Sahrens 24971544Seschrock case ENAMETOOLONG: 2498789Sahrens /* 2499789Sahrens * The resulting top-level vdev spec won't fit in the label. 2500789Sahrens */ 25012082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2502789Sahrens break; 2503789Sahrens 25041544Seschrock default: 25052082Seschrock (void) zpool_standard_error(hdl, errno, msg); 2506789Sahrens } 2507789Sahrens 25082082Seschrock return (-1); 2509789Sahrens } 2510789Sahrens 2511789Sahrens /* 2512789Sahrens * Detach the specified device. 2513789Sahrens */ 2514789Sahrens int 2515789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2516789Sahrens { 2517789Sahrens zfs_cmd_t zc = { 0 }; 2518789Sahrens char msg[1024]; 25192082Seschrock nvlist_t *tgt; 25205450Sbrendan boolean_t avail_spare, l2cache; 25212082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2522789Sahrens 25231544Seschrock (void) snprintf(msg, sizeof (msg), 25241544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 25251544Seschrock 2526789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25277326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 25287326SEric.Schrock@Sun.COM NULL)) == 0) 25292082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2530789Sahrens 25312468Sek110237 if (avail_spare) 25322082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 25332082Seschrock 25345450Sbrendan if (l2cache) 25355450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 25365450Sbrendan 25372082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 25382082Seschrock 25394543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2540789Sahrens return (0); 2541789Sahrens 2542789Sahrens switch (errno) { 2543789Sahrens 25441544Seschrock case ENOTSUP: 2545789Sahrens /* 2546789Sahrens * Can't detach from this type of vdev. 2547789Sahrens */ 25482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 25492082Seschrock "applicable to mirror and replacing vdevs")); 25502082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 2551789Sahrens break; 2552789Sahrens 25531544Seschrock case EBUSY: 2554789Sahrens /* 2555789Sahrens * There are no other replicas of this device. 2556789Sahrens */ 25572082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2558789Sahrens break; 2559789Sahrens 25601544Seschrock default: 25612082Seschrock (void) zpool_standard_error(hdl, errno, msg); 25621544Seschrock } 25631544Seschrock 25642082Seschrock return (-1); 25652082Seschrock } 25662082Seschrock 25672082Seschrock /* 256811422SMark.Musante@Sun.COM * Find a mirror vdev in the source nvlist. 256911422SMark.Musante@Sun.COM * 257011422SMark.Musante@Sun.COM * The mchild array contains a list of disks in one of the top-level mirrors 257111422SMark.Musante@Sun.COM * of the source pool. The schild array contains a list of disks that the 257211422SMark.Musante@Sun.COM * user specified on the command line. We loop over the mchild array to 257311422SMark.Musante@Sun.COM * see if any entry in the schild array matches. 257411422SMark.Musante@Sun.COM * 257511422SMark.Musante@Sun.COM * If a disk in the mchild array is found in the schild array, we return 257611422SMark.Musante@Sun.COM * the index of that entry. Otherwise we return -1. 257711422SMark.Musante@Sun.COM */ 257811422SMark.Musante@Sun.COM static int 257911422SMark.Musante@Sun.COM find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 258011422SMark.Musante@Sun.COM nvlist_t **schild, uint_t schildren) 258111422SMark.Musante@Sun.COM { 258211422SMark.Musante@Sun.COM uint_t mc; 258311422SMark.Musante@Sun.COM 258411422SMark.Musante@Sun.COM for (mc = 0; mc < mchildren; mc++) { 258511422SMark.Musante@Sun.COM uint_t sc; 258611422SMark.Musante@Sun.COM char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 258711422SMark.Musante@Sun.COM mchild[mc], B_FALSE); 258811422SMark.Musante@Sun.COM 258911422SMark.Musante@Sun.COM for (sc = 0; sc < schildren; sc++) { 259011422SMark.Musante@Sun.COM char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 259111422SMark.Musante@Sun.COM schild[sc], B_FALSE); 259211422SMark.Musante@Sun.COM boolean_t result = (strcmp(mpath, spath) == 0); 259311422SMark.Musante@Sun.COM 259411422SMark.Musante@Sun.COM free(spath); 259511422SMark.Musante@Sun.COM if (result) { 259611422SMark.Musante@Sun.COM free(mpath); 259711422SMark.Musante@Sun.COM return (mc); 259811422SMark.Musante@Sun.COM } 259911422SMark.Musante@Sun.COM } 260011422SMark.Musante@Sun.COM 260111422SMark.Musante@Sun.COM free(mpath); 260211422SMark.Musante@Sun.COM } 260311422SMark.Musante@Sun.COM 260411422SMark.Musante@Sun.COM return (-1); 260511422SMark.Musante@Sun.COM } 260611422SMark.Musante@Sun.COM 260711422SMark.Musante@Sun.COM /* 260811422SMark.Musante@Sun.COM * Split a mirror pool. If newroot points to null, then a new nvlist 260911422SMark.Musante@Sun.COM * is generated and it is the responsibility of the caller to free it. 261011422SMark.Musante@Sun.COM */ 261111422SMark.Musante@Sun.COM int 261211422SMark.Musante@Sun.COM zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 261311422SMark.Musante@Sun.COM nvlist_t *props, splitflags_t flags) 261411422SMark.Musante@Sun.COM { 261511422SMark.Musante@Sun.COM zfs_cmd_t zc = { 0 }; 261611422SMark.Musante@Sun.COM char msg[1024]; 261711422SMark.Musante@Sun.COM nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 261811422SMark.Musante@Sun.COM nvlist_t **varray = NULL, *zc_props = NULL; 261911422SMark.Musante@Sun.COM uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 262011422SMark.Musante@Sun.COM libzfs_handle_t *hdl = zhp->zpool_hdl; 262111422SMark.Musante@Sun.COM uint64_t vers; 262211422SMark.Musante@Sun.COM boolean_t freelist = B_FALSE, memory_err = B_TRUE; 262311422SMark.Musante@Sun.COM int retval = 0; 262411422SMark.Musante@Sun.COM 262511422SMark.Musante@Sun.COM (void) snprintf(msg, sizeof (msg), 262611422SMark.Musante@Sun.COM dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 262711422SMark.Musante@Sun.COM 262811422SMark.Musante@Sun.COM if (!zpool_name_valid(hdl, B_FALSE, newname)) 262911422SMark.Musante@Sun.COM return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 263011422SMark.Musante@Sun.COM 263111422SMark.Musante@Sun.COM if ((config = zpool_get_config(zhp, NULL)) == NULL) { 263211422SMark.Musante@Sun.COM (void) fprintf(stderr, gettext("Internal error: unable to " 263311422SMark.Musante@Sun.COM "retrieve pool configuration\n")); 263411422SMark.Musante@Sun.COM return (-1); 263511422SMark.Musante@Sun.COM } 263611422SMark.Musante@Sun.COM 263711422SMark.Musante@Sun.COM verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 263811422SMark.Musante@Sun.COM == 0); 263911422SMark.Musante@Sun.COM verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 264011422SMark.Musante@Sun.COM 264111422SMark.Musante@Sun.COM if (props) { 264211422SMark.Musante@Sun.COM if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 264311422SMark.Musante@Sun.COM props, vers, B_TRUE, msg)) == NULL) 264411422SMark.Musante@Sun.COM return (-1); 264511422SMark.Musante@Sun.COM } 264611422SMark.Musante@Sun.COM 264711422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 264811422SMark.Musante@Sun.COM &children) != 0) { 264911422SMark.Musante@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 265011422SMark.Musante@Sun.COM "Source pool is missing vdev tree")); 265111422SMark.Musante@Sun.COM if (zc_props) 265211422SMark.Musante@Sun.COM nvlist_free(zc_props); 265311422SMark.Musante@Sun.COM return (-1); 265411422SMark.Musante@Sun.COM } 265511422SMark.Musante@Sun.COM 265611422SMark.Musante@Sun.COM varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 265711422SMark.Musante@Sun.COM vcount = 0; 265811422SMark.Musante@Sun.COM 265911422SMark.Musante@Sun.COM if (*newroot == NULL || 266011422SMark.Musante@Sun.COM nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 266111422SMark.Musante@Sun.COM &newchild, &newchildren) != 0) 266211422SMark.Musante@Sun.COM newchildren = 0; 266311422SMark.Musante@Sun.COM 266411422SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 266511422SMark.Musante@Sun.COM uint64_t is_log = B_FALSE, is_hole = B_FALSE; 266611422SMark.Musante@Sun.COM char *type; 266711422SMark.Musante@Sun.COM nvlist_t **mchild, *vdev; 266811422SMark.Musante@Sun.COM uint_t mchildren; 266911422SMark.Musante@Sun.COM int entry; 267011422SMark.Musante@Sun.COM 267111422SMark.Musante@Sun.COM /* 267211422SMark.Musante@Sun.COM * Unlike cache & spares, slogs are stored in the 267311422SMark.Musante@Sun.COM * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 267411422SMark.Musante@Sun.COM */ 267511422SMark.Musante@Sun.COM (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 267611422SMark.Musante@Sun.COM &is_log); 267711422SMark.Musante@Sun.COM (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 267811422SMark.Musante@Sun.COM &is_hole); 267911422SMark.Musante@Sun.COM if (is_log || is_hole) { 268011422SMark.Musante@Sun.COM /* 268111422SMark.Musante@Sun.COM * Create a hole vdev and put it in the config. 268211422SMark.Musante@Sun.COM */ 268311422SMark.Musante@Sun.COM if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 268411422SMark.Musante@Sun.COM goto out; 268511422SMark.Musante@Sun.COM if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 268611422SMark.Musante@Sun.COM VDEV_TYPE_HOLE) != 0) 268711422SMark.Musante@Sun.COM goto out; 268811422SMark.Musante@Sun.COM if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 268911422SMark.Musante@Sun.COM 1) != 0) 269011422SMark.Musante@Sun.COM goto out; 269111422SMark.Musante@Sun.COM if (lastlog == 0) 269211422SMark.Musante@Sun.COM lastlog = vcount; 269311422SMark.Musante@Sun.COM varray[vcount++] = vdev; 269411422SMark.Musante@Sun.COM continue; 269511422SMark.Musante@Sun.COM } 269611422SMark.Musante@Sun.COM lastlog = 0; 269711422SMark.Musante@Sun.COM verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 269811422SMark.Musante@Sun.COM == 0); 269911422SMark.Musante@Sun.COM if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 270011422SMark.Musante@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 270111422SMark.Musante@Sun.COM "Source pool must be composed only of mirrors\n")); 270211422SMark.Musante@Sun.COM retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 270311422SMark.Musante@Sun.COM goto out; 270411422SMark.Musante@Sun.COM } 270511422SMark.Musante@Sun.COM 270611422SMark.Musante@Sun.COM verify(nvlist_lookup_nvlist_array(child[c], 270711422SMark.Musante@Sun.COM ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 270811422SMark.Musante@Sun.COM 270911422SMark.Musante@Sun.COM /* find or add an entry for this top-level vdev */ 271011422SMark.Musante@Sun.COM if (newchildren > 0 && 271111422SMark.Musante@Sun.COM (entry = find_vdev_entry(zhp, mchild, mchildren, 271211422SMark.Musante@Sun.COM newchild, newchildren)) >= 0) { 271311422SMark.Musante@Sun.COM /* We found a disk that the user specified. */ 271411422SMark.Musante@Sun.COM vdev = mchild[entry]; 271511422SMark.Musante@Sun.COM ++found; 271611422SMark.Musante@Sun.COM } else { 271711422SMark.Musante@Sun.COM /* User didn't specify a disk for this vdev. */ 271811422SMark.Musante@Sun.COM vdev = mchild[mchildren - 1]; 271911422SMark.Musante@Sun.COM } 272011422SMark.Musante@Sun.COM 272111422SMark.Musante@Sun.COM if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 272211422SMark.Musante@Sun.COM goto out; 272311422SMark.Musante@Sun.COM } 272411422SMark.Musante@Sun.COM 272511422SMark.Musante@Sun.COM /* did we find every disk the user specified? */ 272611422SMark.Musante@Sun.COM if (found != newchildren) { 272711422SMark.Musante@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 272811422SMark.Musante@Sun.COM "include at most one disk from each mirror")); 272911422SMark.Musante@Sun.COM retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 273011422SMark.Musante@Sun.COM goto out; 273111422SMark.Musante@Sun.COM } 273211422SMark.Musante@Sun.COM 273311422SMark.Musante@Sun.COM /* Prepare the nvlist for populating. */ 273411422SMark.Musante@Sun.COM if (*newroot == NULL) { 273511422SMark.Musante@Sun.COM if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 273611422SMark.Musante@Sun.COM goto out; 273711422SMark.Musante@Sun.COM freelist = B_TRUE; 273811422SMark.Musante@Sun.COM if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 273911422SMark.Musante@Sun.COM VDEV_TYPE_ROOT) != 0) 274011422SMark.Musante@Sun.COM goto out; 274111422SMark.Musante@Sun.COM } else { 274211422SMark.Musante@Sun.COM verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 274311422SMark.Musante@Sun.COM } 274411422SMark.Musante@Sun.COM 274511422SMark.Musante@Sun.COM /* Add all the children we found */ 274611422SMark.Musante@Sun.COM if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 274711422SMark.Musante@Sun.COM lastlog == 0 ? vcount : lastlog) != 0) 274811422SMark.Musante@Sun.COM goto out; 274911422SMark.Musante@Sun.COM 275011422SMark.Musante@Sun.COM /* 275111422SMark.Musante@Sun.COM * If we're just doing a dry run, exit now with success. 275211422SMark.Musante@Sun.COM */ 275311422SMark.Musante@Sun.COM if (flags.dryrun) { 275411422SMark.Musante@Sun.COM memory_err = B_FALSE; 275511422SMark.Musante@Sun.COM freelist = B_FALSE; 275611422SMark.Musante@Sun.COM goto out; 275711422SMark.Musante@Sun.COM } 275811422SMark.Musante@Sun.COM 275911422SMark.Musante@Sun.COM /* now build up the config list & call the ioctl */ 276011422SMark.Musante@Sun.COM if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 276111422SMark.Musante@Sun.COM goto out; 276211422SMark.Musante@Sun.COM 276311422SMark.Musante@Sun.COM if (nvlist_add_nvlist(newconfig, 276411422SMark.Musante@Sun.COM ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 276511422SMark.Musante@Sun.COM nvlist_add_string(newconfig, 276611422SMark.Musante@Sun.COM ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 276711422SMark.Musante@Sun.COM nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 276811422SMark.Musante@Sun.COM goto out; 276911422SMark.Musante@Sun.COM 277011422SMark.Musante@Sun.COM /* 277111422SMark.Musante@Sun.COM * The new pool is automatically part of the namespace unless we 277211422SMark.Musante@Sun.COM * explicitly export it. 277311422SMark.Musante@Sun.COM */ 277411422SMark.Musante@Sun.COM if (!flags.import) 277511422SMark.Musante@Sun.COM zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 277611422SMark.Musante@Sun.COM (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 277711422SMark.Musante@Sun.COM (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 277811422SMark.Musante@Sun.COM if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 277911422SMark.Musante@Sun.COM goto out; 278011422SMark.Musante@Sun.COM if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 278111422SMark.Musante@Sun.COM goto out; 278211422SMark.Musante@Sun.COM 278311422SMark.Musante@Sun.COM if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 278411422SMark.Musante@Sun.COM retval = zpool_standard_error(hdl, errno, msg); 278511422SMark.Musante@Sun.COM goto out; 278611422SMark.Musante@Sun.COM } 278711422SMark.Musante@Sun.COM 278811422SMark.Musante@Sun.COM freelist = B_FALSE; 278911422SMark.Musante@Sun.COM memory_err = B_FALSE; 279011422SMark.Musante@Sun.COM 279111422SMark.Musante@Sun.COM out: 279211422SMark.Musante@Sun.COM if (varray != NULL) { 279311422SMark.Musante@Sun.COM int v; 279411422SMark.Musante@Sun.COM 279511422SMark.Musante@Sun.COM for (v = 0; v < vcount; v++) 279611422SMark.Musante@Sun.COM nvlist_free(varray[v]); 279711422SMark.Musante@Sun.COM free(varray); 279811422SMark.Musante@Sun.COM } 279911422SMark.Musante@Sun.COM zcmd_free_nvlists(&zc); 280011422SMark.Musante@Sun.COM if (zc_props) 280111422SMark.Musante@Sun.COM nvlist_free(zc_props); 280211422SMark.Musante@Sun.COM if (newconfig) 280311422SMark.Musante@Sun.COM nvlist_free(newconfig); 280411422SMark.Musante@Sun.COM if (freelist) { 280511422SMark.Musante@Sun.COM nvlist_free(*newroot); 280611422SMark.Musante@Sun.COM *newroot = NULL; 280711422SMark.Musante@Sun.COM } 280811422SMark.Musante@Sun.COM 280911422SMark.Musante@Sun.COM if (retval != 0) 281011422SMark.Musante@Sun.COM return (retval); 281111422SMark.Musante@Sun.COM 281211422SMark.Musante@Sun.COM if (memory_err) 281311422SMark.Musante@Sun.COM return (no_memory(hdl)); 281411422SMark.Musante@Sun.COM 281511422SMark.Musante@Sun.COM return (0); 281611422SMark.Musante@Sun.COM } 281711422SMark.Musante@Sun.COM 281811422SMark.Musante@Sun.COM /* 28195450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 28205450Sbrendan * and level 2 cache devices. 28212082Seschrock */ 28222082Seschrock int 28232082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 28242082Seschrock { 28252082Seschrock zfs_cmd_t zc = { 0 }; 28262082Seschrock char msg[1024]; 28272082Seschrock nvlist_t *tgt; 282810594SGeorge.Wilson@Sun.COM boolean_t avail_spare, l2cache, islog; 28292082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 283010594SGeorge.Wilson@Sun.COM uint64_t version; 28312082Seschrock 28322082Seschrock (void) snprintf(msg, sizeof (msg), 28332082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 28342082Seschrock 28352082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 28367326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 283710594SGeorge.Wilson@Sun.COM &islog)) == 0) 28382082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 283910594SGeorge.Wilson@Sun.COM /* 284010594SGeorge.Wilson@Sun.COM * XXX - this should just go away. 284110594SGeorge.Wilson@Sun.COM */ 284210594SGeorge.Wilson@Sun.COM if (!avail_spare && !l2cache && !islog) { 28432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 284410594SGeorge.Wilson@Sun.COM "only inactive hot spares, cache, top-level, " 284510594SGeorge.Wilson@Sun.COM "or log devices can be removed")); 28462082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 28472082Seschrock } 28482082Seschrock 284910594SGeorge.Wilson@Sun.COM version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 285010594SGeorge.Wilson@Sun.COM if (islog && version < SPA_VERSION_HOLES) { 285110594SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 285210594SGeorge.Wilson@Sun.COM "pool must be upgrade to support log removal")); 285310594SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_BADVERSION, msg)); 285410594SGeorge.Wilson@Sun.COM } 285510594SGeorge.Wilson@Sun.COM 28562082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 28572082Seschrock 28584543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 28592082Seschrock return (0); 28602082Seschrock 28612082Seschrock return (zpool_standard_error(hdl, errno, msg)); 28621544Seschrock } 28631544Seschrock 28641544Seschrock /* 28651544Seschrock * Clear the errors for the pool, or the particular device if specified. 28661544Seschrock */ 28671544Seschrock int 286810921STim.Haley@Sun.COM zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 28691544Seschrock { 28701544Seschrock zfs_cmd_t zc = { 0 }; 28711544Seschrock char msg[1024]; 28722082Seschrock nvlist_t *tgt; 287310921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 28745450Sbrendan boolean_t avail_spare, l2cache; 28752082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 287610921STim.Haley@Sun.COM nvlist_t *nvi = NULL; 2877*12949SGeorge.Wilson@Sun.COM int error; 28781544Seschrock 28791544Seschrock if (path) 28801544Seschrock (void) snprintf(msg, sizeof (msg), 28811544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 28822676Seschrock path); 28831544Seschrock else 28841544Seschrock (void) snprintf(msg, sizeof (msg), 28851544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 28861544Seschrock zhp->zpool_name); 28871544Seschrock 28881544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 28892082Seschrock if (path) { 28905450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 28917326SEric.Schrock@Sun.COM &l2cache, NULL)) == 0) 28922082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 28932082Seschrock 28945450Sbrendan /* 28955450Sbrendan * Don't allow error clearing for hot spares. Do allow 28965450Sbrendan * error clearing for l2cache devices. 28975450Sbrendan */ 28982468Sek110237 if (avail_spare) 28992082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 29002082Seschrock 29012082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 29022082Seschrock &zc.zc_guid) == 0); 29031544Seschrock } 29041544Seschrock 290510921STim.Haley@Sun.COM zpool_get_rewind_policy(rewindnvl, &policy); 290610921STim.Haley@Sun.COM zc.zc_cookie = policy.zrp_request; 290710921STim.Haley@Sun.COM 2908*12949SGeorge.Wilson@Sun.COM if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 290910921STim.Haley@Sun.COM return (-1); 291010921STim.Haley@Sun.COM 291110921STim.Haley@Sun.COM if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, rewindnvl) != 0) 291210921STim.Haley@Sun.COM return (-1); 291310921STim.Haley@Sun.COM 2914*12949SGeorge.Wilson@Sun.COM while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 2915*12949SGeorge.Wilson@Sun.COM errno == ENOMEM) { 2916*12949SGeorge.Wilson@Sun.COM if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2917*12949SGeorge.Wilson@Sun.COM zcmd_free_nvlists(&zc); 2918*12949SGeorge.Wilson@Sun.COM return (-1); 2919*12949SGeorge.Wilson@Sun.COM } 2920*12949SGeorge.Wilson@Sun.COM } 2921*12949SGeorge.Wilson@Sun.COM 2922*12949SGeorge.Wilson@Sun.COM if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 292310921STim.Haley@Sun.COM errno != EPERM && errno != EACCES)) { 292410921STim.Haley@Sun.COM if (policy.zrp_request & 292510921STim.Haley@Sun.COM (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 292610921STim.Haley@Sun.COM (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 292710921STim.Haley@Sun.COM zpool_rewind_exclaim(hdl, zc.zc_name, 292810921STim.Haley@Sun.COM ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 292910921STim.Haley@Sun.COM nvi); 293010921STim.Haley@Sun.COM nvlist_free(nvi); 293110921STim.Haley@Sun.COM } 293210921STim.Haley@Sun.COM zcmd_free_nvlists(&zc); 29331544Seschrock return (0); 293410921STim.Haley@Sun.COM } 293510921STim.Haley@Sun.COM 293610921STim.Haley@Sun.COM zcmd_free_nvlists(&zc); 29372082Seschrock return (zpool_standard_error(hdl, errno, msg)); 2938789Sahrens } 2939789Sahrens 29403126Sahl /* 29414451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 29424451Seschrock */ 29434451Seschrock int 29444451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 29454451Seschrock { 29464451Seschrock zfs_cmd_t zc = { 0 }; 29474451Seschrock char msg[1024]; 29484451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 29494451Seschrock 29504451Seschrock (void) snprintf(msg, sizeof (msg), 29514451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 29524451Seschrock guid); 29534451Seschrock 29544451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 29554451Seschrock zc.zc_guid = guid; 295612441SVictor.Latushkin@Sun.COM zc.zc_cookie = ZPOOL_NO_REWIND; 29574451Seschrock 29584451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 29594451Seschrock return (0); 29604451Seschrock 29614451Seschrock return (zpool_standard_error(hdl, errno, msg)); 29624451Seschrock } 29634451Seschrock 29644451Seschrock /* 29651354Seschrock * Convert from a devid string to a path. 29661354Seschrock */ 29671354Seschrock static char * 29681354Seschrock devid_to_path(char *devid_str) 29691354Seschrock { 29701354Seschrock ddi_devid_t devid; 29711354Seschrock char *minor; 29721354Seschrock char *path; 29731354Seschrock devid_nmlist_t *list = NULL; 29741354Seschrock int ret; 29751354Seschrock 29761354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 29771354Seschrock return (NULL); 29781354Seschrock 29791354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 29801354Seschrock 29811354Seschrock devid_str_free(minor); 29821354Seschrock devid_free(devid); 29831354Seschrock 29841354Seschrock if (ret != 0) 29851354Seschrock return (NULL); 29861354Seschrock 29872082Seschrock if ((path = strdup(list[0].devname)) == NULL) 29882082Seschrock return (NULL); 29892082Seschrock 29901354Seschrock devid_free_nmlist(list); 29911354Seschrock 29921354Seschrock return (path); 29931354Seschrock } 29941354Seschrock 29951354Seschrock /* 29961354Seschrock * Convert from a path to a devid string. 29971354Seschrock */ 29981354Seschrock static char * 29991354Seschrock path_to_devid(const char *path) 30001354Seschrock { 30011354Seschrock int fd; 30021354Seschrock ddi_devid_t devid; 30031354Seschrock char *minor, *ret; 30041354Seschrock 30051354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 30061354Seschrock return (NULL); 30071354Seschrock 30081354Seschrock minor = NULL; 30091354Seschrock ret = NULL; 30101354Seschrock if (devid_get(fd, &devid) == 0) { 30111354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 30121354Seschrock ret = devid_str_encode(devid, minor); 30131354Seschrock if (minor != NULL) 30141354Seschrock devid_str_free(minor); 30151354Seschrock devid_free(devid); 30161354Seschrock } 30171354Seschrock (void) close(fd); 30181354Seschrock 30191354Seschrock return (ret); 30201354Seschrock } 30211354Seschrock 30221354Seschrock /* 30231354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 30241354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 30251354Seschrock * type 'zpool status', and we'll display the correct information anyway. 30261354Seschrock */ 30271354Seschrock static void 30281354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 30291354Seschrock { 30301354Seschrock zfs_cmd_t zc = { 0 }; 30311354Seschrock 30321354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 30332676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 30341354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 30351544Seschrock &zc.zc_guid) == 0); 30361354Seschrock 30372082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 30381354Seschrock } 30391354Seschrock 30401354Seschrock /* 30411354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 30421354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 30431354Seschrock * We also check if this is a whole disk, in which case we strip off the 30441354Seschrock * trailing 's0' slice name. 30451354Seschrock * 30461354Seschrock * This routine is also responsible for identifying when disks have been 30471354Seschrock * reconfigured in a new location. The kernel will have opened the device by 30481354Seschrock * devid, but the path will still refer to the old location. To catch this, we 30491354Seschrock * first do a path -> devid translation (which is fast for the common case). If 30501354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 30511354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 30521354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 30531354Seschrock * of these checks. 30541354Seschrock */ 30551354Seschrock char * 305610594SGeorge.Wilson@Sun.COM zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 305710594SGeorge.Wilson@Sun.COM boolean_t verbose) 30581354Seschrock { 30591354Seschrock char *path, *devid; 30601544Seschrock uint64_t value; 30611544Seschrock char buf[64]; 30624451Seschrock vdev_stat_t *vs; 30634451Seschrock uint_t vsc; 30641354Seschrock 30651544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 30661544Seschrock &value) == 0) { 30671544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 30681544Seschrock &value) == 0); 30692856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 30702856Snd150628 (u_longlong_t)value); 30711544Seschrock path = buf; 30721544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 30731354Seschrock 30744451Seschrock /* 30754451Seschrock * If the device is dead (faulted, offline, etc) then don't 30764451Seschrock * bother opening it. Otherwise we may be forcing the user to 30774451Seschrock * open a misbehaving device, which can have undesirable 30784451Seschrock * effects. 30794451Seschrock */ 308012296SLin.Ling@Sun.COM if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 30814451Seschrock (uint64_t **)&vs, &vsc) != 0 || 30824451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 30834451Seschrock zhp != NULL && 30841354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 30851354Seschrock /* 30861354Seschrock * Determine if the current path is correct. 30871354Seschrock */ 30881354Seschrock char *newdevid = path_to_devid(path); 30891354Seschrock 30901354Seschrock if (newdevid == NULL || 30911354Seschrock strcmp(devid, newdevid) != 0) { 30921354Seschrock char *newpath; 30931354Seschrock 30941354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 30951354Seschrock /* 30961354Seschrock * Update the path appropriately. 30971354Seschrock */ 30981354Seschrock set_path(zhp, nv, newpath); 30992082Seschrock if (nvlist_add_string(nv, 31002082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 31012082Seschrock verify(nvlist_lookup_string(nv, 31022082Seschrock ZPOOL_CONFIG_PATH, 31032082Seschrock &path) == 0); 31041354Seschrock free(newpath); 31051354Seschrock } 31061354Seschrock } 31071354Seschrock 31082082Seschrock if (newdevid) 31092082Seschrock devid_str_free(newdevid); 31101354Seschrock } 31111354Seschrock 31121354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 31131354Seschrock path += 9; 31141354Seschrock 31151354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 31161544Seschrock &value) == 0 && value) { 311712383SJohn.Harres@Sun.COM int pathlen = strlen(path); 31182082Seschrock char *tmp = zfs_strdup(hdl, path); 311912383SJohn.Harres@Sun.COM 312012383SJohn.Harres@Sun.COM /* 312112383SJohn.Harres@Sun.COM * If it starts with c#, and ends with "s0", chop 312212383SJohn.Harres@Sun.COM * the "s0" off, or if it ends with "s0/old", remove 312312383SJohn.Harres@Sun.COM * the "s0" from the middle. 312412383SJohn.Harres@Sun.COM */ 312512383SJohn.Harres@Sun.COM if (CTD_CHECK(tmp)) { 312612383SJohn.Harres@Sun.COM if (strcmp(&tmp[pathlen - 2], "s0") == 0) { 312712383SJohn.Harres@Sun.COM tmp[pathlen - 2] = '\0'; 312812383SJohn.Harres@Sun.COM } else if (pathlen > 6 && 312912383SJohn.Harres@Sun.COM strcmp(&tmp[pathlen - 6], "s0/old") == 0) { 313012383SJohn.Harres@Sun.COM (void) strcpy(&tmp[pathlen - 6], 313112383SJohn.Harres@Sun.COM "/old"); 313212383SJohn.Harres@Sun.COM } 313312383SJohn.Harres@Sun.COM } 31341354Seschrock return (tmp); 31351354Seschrock } 31361354Seschrock } else { 31371354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 31382082Seschrock 31392082Seschrock /* 31402082Seschrock * If it's a raidz device, we need to stick in the parity level. 31412082Seschrock */ 31422082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 31432082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 31442082Seschrock &value) == 0); 31452082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 31462856Snd150628 (u_longlong_t)value); 31472082Seschrock path = buf; 31482082Seschrock } 314910594SGeorge.Wilson@Sun.COM 315010594SGeorge.Wilson@Sun.COM /* 315110594SGeorge.Wilson@Sun.COM * We identify each top-level vdev by using a <type-id> 315210594SGeorge.Wilson@Sun.COM * naming convention. 315310594SGeorge.Wilson@Sun.COM */ 315410594SGeorge.Wilson@Sun.COM if (verbose) { 315510594SGeorge.Wilson@Sun.COM uint64_t id; 315610594SGeorge.Wilson@Sun.COM 315710594SGeorge.Wilson@Sun.COM verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 315810594SGeorge.Wilson@Sun.COM &id) == 0); 315910594SGeorge.Wilson@Sun.COM (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 316010594SGeorge.Wilson@Sun.COM (u_longlong_t)id); 316110594SGeorge.Wilson@Sun.COM path = buf; 316210594SGeorge.Wilson@Sun.COM } 31631354Seschrock } 31641354Seschrock 31652082Seschrock return (zfs_strdup(hdl, path)); 31661354Seschrock } 31671544Seschrock 31681544Seschrock static int 31691544Seschrock zbookmark_compare(const void *a, const void *b) 31701544Seschrock { 31711544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 31721544Seschrock } 31731544Seschrock 31741544Seschrock /* 31751544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 31761544Seschrock * caller. 31771544Seschrock */ 31781544Seschrock int 31793444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 31801544Seschrock { 31811544Seschrock zfs_cmd_t zc = { 0 }; 31821544Seschrock uint64_t count; 31832676Seschrock zbookmark_t *zb = NULL; 31843444Sek110237 int i; 31851544Seschrock 31861544Seschrock /* 31871544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 31881544Seschrock * has increased, allocate more space and continue until we get the 31891544Seschrock * entire list. 31901544Seschrock */ 31911544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 31921544Seschrock &count) == 0); 31934820Sek110237 if (count == 0) 31944820Sek110237 return (0); 31952676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 31962856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 31972082Seschrock return (-1); 31982676Seschrock zc.zc_nvlist_dst_size = count; 31991544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 32001544Seschrock for (;;) { 32012082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 32022082Seschrock &zc) != 0) { 32032676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 32041544Seschrock if (errno == ENOMEM) { 32053823Svb160487 count = zc.zc_nvlist_dst_size; 32062676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 32073823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 32083823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 32092082Seschrock return (-1); 32101544Seschrock } else { 32111544Seschrock return (-1); 32121544Seschrock } 32131544Seschrock } else { 32141544Seschrock break; 32151544Seschrock } 32161544Seschrock } 32171544Seschrock 32181544Seschrock /* 32191544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 32201544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 32212676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 32221544Seschrock * _not_ copied as part of the process. So we point the start of our 32231544Seschrock * array appropriate and decrement the total number of elements. 32241544Seschrock */ 32252676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 32262676Seschrock zc.zc_nvlist_dst_size; 32272676Seschrock count -= zc.zc_nvlist_dst_size; 32281544Seschrock 32291544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 32301544Seschrock 32313444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 32321544Seschrock 32331544Seschrock /* 32343444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 32351544Seschrock */ 32361544Seschrock for (i = 0; i < count; i++) { 32371544Seschrock nvlist_t *nv; 32381544Seschrock 32393700Sek110237 /* ignoring zb_blkid and zb_level for now */ 32403700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 32413700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 32421544Seschrock continue; 32431544Seschrock 32443444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 32453444Sek110237 goto nomem; 32463444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 32473444Sek110237 zb[i].zb_objset) != 0) { 32483444Sek110237 nvlist_free(nv); 32492082Seschrock goto nomem; 32503444Sek110237 } 32513444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 32523444Sek110237 zb[i].zb_object) != 0) { 32533444Sek110237 nvlist_free(nv); 32543444Sek110237 goto nomem; 32551544Seschrock } 32563444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 32573444Sek110237 nvlist_free(nv); 32583444Sek110237 goto nomem; 32593444Sek110237 } 32603444Sek110237 nvlist_free(nv); 32611544Seschrock } 32621544Seschrock 32633265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 32641544Seschrock return (0); 32652082Seschrock 32662082Seschrock nomem: 32672676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 32682082Seschrock return (no_memory(zhp->zpool_hdl)); 32691544Seschrock } 32701760Seschrock 32711760Seschrock /* 32721760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 32731760Seschrock */ 32741760Seschrock int 32755094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 32761760Seschrock { 32771760Seschrock zfs_cmd_t zc = { 0 }; 32782082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 32791760Seschrock 32801760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 32815094Slling zc.zc_cookie = new_version; 32825094Slling 32834543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 32843237Slling return (zpool_standard_error_fmt(hdl, errno, 32852082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 32862082Seschrock zhp->zpool_name)); 32871760Seschrock return (0); 32881760Seschrock } 32892926Sek110237 32904988Sek110237 void 32914988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 32924988Sek110237 char *history_str) 32934988Sek110237 { 32944988Sek110237 int i; 32954988Sek110237 32964988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 32974988Sek110237 for (i = 1; i < argc; i++) { 32984988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 32994988Sek110237 HIS_MAX_RECORD_LEN) 33004988Sek110237 break; 33014988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 33024988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 33034988Sek110237 } 33044988Sek110237 } 33054988Sek110237 33062926Sek110237 /* 33074988Sek110237 * Stage command history for logging. 33082926Sek110237 */ 33094988Sek110237 int 33104988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 33112926Sek110237 { 33124988Sek110237 if (history_str == NULL) 33134988Sek110237 return (EINVAL); 33144988Sek110237 33154988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 33164988Sek110237 return (EINVAL); 33172926Sek110237 33184715Sek110237 if (hdl->libzfs_log_str != NULL) 33194543Smarks free(hdl->libzfs_log_str); 33202926Sek110237 33214988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 33224988Sek110237 return (no_memory(hdl)); 33234543Smarks 33244988Sek110237 return (0); 33252926Sek110237 } 33262926Sek110237 33272926Sek110237 /* 33282926Sek110237 * Perform ioctl to get some command history of a pool. 33292926Sek110237 * 33302926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 33312926Sek110237 * logical offset of the history buffer to start reading from. 33322926Sek110237 * 33332926Sek110237 * Upon return, 'off' is the next logical offset to read from and 33342926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 33352926Sek110237 */ 33362926Sek110237 static int 33372926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 33382926Sek110237 { 33392926Sek110237 zfs_cmd_t zc = { 0 }; 33402926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 33412926Sek110237 33422926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 33432926Sek110237 33442926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 33452926Sek110237 zc.zc_history_len = *len; 33462926Sek110237 zc.zc_history_offset = *off; 33472926Sek110237 33482926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 33492926Sek110237 switch (errno) { 33502926Sek110237 case EPERM: 33513237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 33523237Slling dgettext(TEXT_DOMAIN, 33532926Sek110237 "cannot show history for pool '%s'"), 33542926Sek110237 zhp->zpool_name)); 33552926Sek110237 case ENOENT: 33563237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 33572926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 33582926Sek110237 "'%s'"), zhp->zpool_name)); 33593863Sek110237 case ENOTSUP: 33603863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 33613863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 33623863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 33632926Sek110237 default: 33643237Slling return (zpool_standard_error_fmt(hdl, errno, 33652926Sek110237 dgettext(TEXT_DOMAIN, 33662926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 33672926Sek110237 } 33682926Sek110237 } 33692926Sek110237 33702926Sek110237 *len = zc.zc_history_len; 33712926Sek110237 *off = zc.zc_history_offset; 33722926Sek110237 33732926Sek110237 return (0); 33742926Sek110237 } 33752926Sek110237 33762926Sek110237 /* 33772926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 33782926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 33792926Sek110237 * processed as there wasn't a complete record. 33802926Sek110237 */ 338110685SGeorge.Wilson@Sun.COM int 33822926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 33832926Sek110237 nvlist_t ***records, uint_t *numrecords) 33842926Sek110237 { 33852926Sek110237 uint64_t reclen; 33862926Sek110237 nvlist_t *nv; 33872926Sek110237 int i; 33882926Sek110237 33892926Sek110237 while (bytes_read > sizeof (reclen)) { 33902926Sek110237 33912926Sek110237 /* get length of packed record (stored as little endian) */ 33922926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 33932926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 33942926Sek110237 33952926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 33962926Sek110237 break; 33972926Sek110237 33982926Sek110237 /* unpack record */ 33992926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 34002926Sek110237 return (ENOMEM); 34012926Sek110237 bytes_read -= sizeof (reclen) + reclen; 34022926Sek110237 buf += sizeof (reclen) + reclen; 34032926Sek110237 34042926Sek110237 /* add record to nvlist array */ 34052926Sek110237 (*numrecords)++; 34062926Sek110237 if (ISP2(*numrecords + 1)) { 34072926Sek110237 *records = realloc(*records, 34082926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 34092926Sek110237 } 34102926Sek110237 (*records)[*numrecords - 1] = nv; 34112926Sek110237 } 34122926Sek110237 34132926Sek110237 *leftover = bytes_read; 34142926Sek110237 return (0); 34152926Sek110237 } 34162926Sek110237 34172926Sek110237 #define HIS_BUF_LEN (128*1024) 34182926Sek110237 34192926Sek110237 /* 34202926Sek110237 * Retrieve the command history of a pool. 34212926Sek110237 */ 34222926Sek110237 int 34232926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 34242926Sek110237 { 34252926Sek110237 char buf[HIS_BUF_LEN]; 34262926Sek110237 uint64_t off = 0; 34272926Sek110237 nvlist_t **records = NULL; 34282926Sek110237 uint_t numrecords = 0; 34292926Sek110237 int err, i; 34302926Sek110237 34312926Sek110237 do { 34322926Sek110237 uint64_t bytes_read = sizeof (buf); 34332926Sek110237 uint64_t leftover; 34342926Sek110237 34352926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 34362926Sek110237 break; 34372926Sek110237 34382926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 34392926Sek110237 if (!bytes_read) 34402926Sek110237 break; 34412926Sek110237 34422926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 34432926Sek110237 &leftover, &records, &numrecords)) != 0) 34442926Sek110237 break; 34452926Sek110237 off -= leftover; 34462926Sek110237 34472926Sek110237 /* CONSTCOND */ 34482926Sek110237 } while (1); 34492926Sek110237 34502926Sek110237 if (!err) { 34512926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 34522926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 34532926Sek110237 records, numrecords) == 0); 34542926Sek110237 } 34552926Sek110237 for (i = 0; i < numrecords; i++) 34562926Sek110237 nvlist_free(records[i]); 34572926Sek110237 free(records); 34582926Sek110237 34592926Sek110237 return (err); 34602926Sek110237 } 34613444Sek110237 34623444Sek110237 void 34633444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 34643444Sek110237 char *pathname, size_t len) 34653444Sek110237 { 34663444Sek110237 zfs_cmd_t zc = { 0 }; 34673444Sek110237 boolean_t mounted = B_FALSE; 34683444Sek110237 char *mntpnt = NULL; 34693444Sek110237 char dsname[MAXNAMELEN]; 34703444Sek110237 34713444Sek110237 if (dsobj == 0) { 34723444Sek110237 /* special case for the MOS */ 34733444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 34743444Sek110237 return; 34753444Sek110237 } 34763444Sek110237 34773444Sek110237 /* get the dataset's name */ 34783444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 34793444Sek110237 zc.zc_obj = dsobj; 34803444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 34813444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 34823444Sek110237 /* just write out a path of two object numbers */ 34833444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 34843444Sek110237 dsobj, obj); 34853444Sek110237 return; 34863444Sek110237 } 34873444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 34883444Sek110237 34893444Sek110237 /* find out if the dataset is mounted */ 34903444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 34913444Sek110237 34923444Sek110237 /* get the corrupted object's path */ 34933444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 34943444Sek110237 zc.zc_obj = obj; 34953444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 34963444Sek110237 &zc) == 0) { 34973444Sek110237 if (mounted) { 34983444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 34993444Sek110237 zc.zc_value); 35003444Sek110237 } else { 35013444Sek110237 (void) snprintf(pathname, len, "%s:%s", 35023444Sek110237 dsname, zc.zc_value); 35033444Sek110237 } 35043444Sek110237 } else { 35053444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 35063444Sek110237 } 35073444Sek110237 free(mntpnt); 35083444Sek110237 } 35093912Slling 35104276Staylor /* 35117042Sgw25295 * Read the EFI label from the config, if a label does not exist then 35127042Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 35137042Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 35147042Sgw25295 * partition. 35157042Sgw25295 */ 35167042Sgw25295 static int 35177042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 35187042Sgw25295 { 35197042Sgw25295 char *path; 35207042Sgw25295 int fd; 35217042Sgw25295 char diskname[MAXPATHLEN]; 35227042Sgw25295 int err = -1; 35237042Sgw25295 35247042Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 35257042Sgw25295 return (err); 35267042Sgw25295 35277042Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 35287042Sgw25295 strrchr(path, '/')); 35297042Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 35307042Sgw25295 struct dk_gpt *vtoc; 35317042Sgw25295 35327042Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 35337042Sgw25295 if (sb != NULL) 35347042Sgw25295 *sb = vtoc->efi_parts[0].p_start; 35357042Sgw25295 efi_free(vtoc); 35367042Sgw25295 } 35377042Sgw25295 (void) close(fd); 35387042Sgw25295 } 35397042Sgw25295 return (err); 35407042Sgw25295 } 35417042Sgw25295 35427042Sgw25295 /* 35434276Staylor * determine where a partition starts on a disk in the current 35444276Staylor * configuration 35454276Staylor */ 35464276Staylor static diskaddr_t 35474276Staylor find_start_block(nvlist_t *config) 35484276Staylor { 35494276Staylor nvlist_t **child; 35504276Staylor uint_t c, children; 35514276Staylor diskaddr_t sb = MAXOFFSET_T; 35524276Staylor uint64_t wholedisk; 35534276Staylor 35544276Staylor if (nvlist_lookup_nvlist_array(config, 35554276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 35564276Staylor if (nvlist_lookup_uint64(config, 35574276Staylor ZPOOL_CONFIG_WHOLE_DISK, 35584276Staylor &wholedisk) != 0 || !wholedisk) { 35594276Staylor return (MAXOFFSET_T); 35604276Staylor } 35617042Sgw25295 if (read_efi_label(config, &sb) < 0) 35627042Sgw25295 sb = MAXOFFSET_T; 35634276Staylor return (sb); 35644276Staylor } 35654276Staylor 35664276Staylor for (c = 0; c < children; c++) { 35674276Staylor sb = find_start_block(child[c]); 35684276Staylor if (sb != MAXOFFSET_T) { 35694276Staylor return (sb); 35704276Staylor } 35714276Staylor } 35724276Staylor return (MAXOFFSET_T); 35734276Staylor } 35744276Staylor 35754276Staylor /* 35764276Staylor * Label an individual disk. The name provided is the short name, 35774276Staylor * stripped of any leading /dev path. 35784276Staylor */ 35794276Staylor int 35804276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 35814276Staylor { 35824276Staylor char path[MAXPATHLEN]; 35834276Staylor struct dk_gpt *vtoc; 35844276Staylor int fd; 35854276Staylor size_t resv = EFI_MIN_RESV_SIZE; 35864276Staylor uint64_t slice_size; 35874276Staylor diskaddr_t start_block; 35884276Staylor char errbuf[1024]; 35894276Staylor 35906289Smmusante /* prepare an error message just in case */ 35916289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 35926289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 35936289Smmusante 35944276Staylor if (zhp) { 35954276Staylor nvlist_t *nvroot; 35964276Staylor 35977965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp)) { 35987965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35997965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root " 36007965SGeorge.Wilson@Sun.COM "pools.")); 36017965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 36027965SGeorge.Wilson@Sun.COM } 36037965SGeorge.Wilson@Sun.COM 36044276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 36054276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 36064276Staylor 36074276Staylor if (zhp->zpool_start_block == 0) 36084276Staylor start_block = find_start_block(nvroot); 36094276Staylor else 36104276Staylor start_block = zhp->zpool_start_block; 36114276Staylor zhp->zpool_start_block = start_block; 36124276Staylor } else { 36134276Staylor /* new pool */ 36144276Staylor start_block = NEW_START_BLOCK; 36154276Staylor } 36164276Staylor 36174276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 36184276Staylor BACKUP_SLICE); 36194276Staylor 36204276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 36214276Staylor /* 36224276Staylor * This shouldn't happen. We've long since verified that this 36234276Staylor * is a valid device. 36244276Staylor */ 36256289Smmusante zfs_error_aux(hdl, 36266289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 36274276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 36284276Staylor } 36294276Staylor 36304276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 36314276Staylor /* 36324276Staylor * The only way this can fail is if we run out of memory, or we 36334276Staylor * were unable to read the disk's capacity 36344276Staylor */ 36354276Staylor if (errno == ENOMEM) 36364276Staylor (void) no_memory(hdl); 36374276Staylor 36384276Staylor (void) close(fd); 36396289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36406289Smmusante "unable to read disk capacity"), name); 36414276Staylor 36424276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 36434276Staylor } 36444276Staylor 36454276Staylor slice_size = vtoc->efi_last_u_lba + 1; 36464276Staylor slice_size -= EFI_MIN_RESV_SIZE; 36474276Staylor if (start_block == MAXOFFSET_T) 36484276Staylor start_block = NEW_START_BLOCK; 36494276Staylor slice_size -= start_block; 36504276Staylor 36514276Staylor vtoc->efi_parts[0].p_start = start_block; 36524276Staylor vtoc->efi_parts[0].p_size = slice_size; 36534276Staylor 36544276Staylor /* 36554276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 36564276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 36574276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 36584276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 36594276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 36604276Staylor * can get, in the absence of V_OTHER. 36614276Staylor */ 36624276Staylor vtoc->efi_parts[0].p_tag = V_USR; 36634276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 36644276Staylor 36654276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 36664276Staylor vtoc->efi_parts[8].p_size = resv; 36674276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 36684276Staylor 36694276Staylor if (efi_write(fd, vtoc) != 0) { 36704276Staylor /* 36714276Staylor * Some block drivers (like pcata) may not support EFI 36724276Staylor * GPT labels. Print out a helpful error message dir- 36734276Staylor * ecting the user to manually label the disk and give 36744276Staylor * a specific slice. 36754276Staylor */ 36764276Staylor (void) close(fd); 36774276Staylor efi_free(vtoc); 36784276Staylor 36794276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36806289Smmusante "try using fdisk(1M) and then provide a specific slice")); 36814276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 36824276Staylor } 36834276Staylor 36844276Staylor (void) close(fd); 36854276Staylor efi_free(vtoc); 36864276Staylor return (0); 36874276Staylor } 36886423Sgw25295 36896423Sgw25295 static boolean_t 36906423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 36916423Sgw25295 { 36926423Sgw25295 char *type; 36936423Sgw25295 nvlist_t **child; 36946423Sgw25295 uint_t children, c; 36956423Sgw25295 36966423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 36976423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 36986423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 36996423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 370010594SGeorge.Wilson@Sun.COM strcmp(type, VDEV_TYPE_HOLE) == 0 || 37016423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 37026423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37036423Sgw25295 "vdev type '%s' is not supported"), type); 37046423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 37056423Sgw25295 return (B_FALSE); 37066423Sgw25295 } 37076423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 37086423Sgw25295 &child, &children) == 0) { 37096423Sgw25295 for (c = 0; c < children; c++) { 37106423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 37116423Sgw25295 return (B_FALSE); 37126423Sgw25295 } 37136423Sgw25295 } 37146423Sgw25295 return (B_TRUE); 37156423Sgw25295 } 37166423Sgw25295 37176423Sgw25295 /* 37186423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 37196423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 37206423Sgw25295 */ 37216423Sgw25295 int 37226423Sgw25295 zvol_check_dump_config(char *arg) 37236423Sgw25295 { 37246423Sgw25295 zpool_handle_t *zhp = NULL; 37256423Sgw25295 nvlist_t *config, *nvroot; 37266423Sgw25295 char *p, *volname; 37276423Sgw25295 nvlist_t **top; 37286423Sgw25295 uint_t toplevels; 37296423Sgw25295 libzfs_handle_t *hdl; 37306423Sgw25295 char errbuf[1024]; 37316423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 37326423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 37336423Sgw25295 int ret = 1; 37346423Sgw25295 37356423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 37366423Sgw25295 return (-1); 37376423Sgw25295 } 37386423Sgw25295 37396423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37406423Sgw25295 "dump is not supported on device '%s'"), arg); 37416423Sgw25295 37426423Sgw25295 if ((hdl = libzfs_init()) == NULL) 37436423Sgw25295 return (1); 37446423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 37456423Sgw25295 37466423Sgw25295 volname = arg + pathlen; 37476423Sgw25295 37486423Sgw25295 /* check the configuration of the pool */ 37496423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 37506423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37516423Sgw25295 "malformed dataset name")); 37526423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 37536423Sgw25295 return (1); 37546423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 37556423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37566423Sgw25295 "dataset name is too long")); 37576423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 37586423Sgw25295 return (1); 37596423Sgw25295 } else { 37606423Sgw25295 (void) strncpy(poolname, volname, p - volname); 37616423Sgw25295 poolname[p - volname] = '\0'; 37626423Sgw25295 } 37636423Sgw25295 37646423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 37656423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37666423Sgw25295 "could not open pool '%s'"), poolname); 37676423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 37686423Sgw25295 goto out; 37696423Sgw25295 } 37706423Sgw25295 config = zpool_get_config(zhp, NULL); 37716423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 37726423Sgw25295 &nvroot) != 0) { 37736423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37746423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 37756423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 37766423Sgw25295 goto out; 37776423Sgw25295 } 37786423Sgw25295 37796423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 37806423Sgw25295 &top, &toplevels) == 0); 37816423Sgw25295 if (toplevels != 1) { 37826423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37836423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 37846423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 37856423Sgw25295 goto out; 37866423Sgw25295 } 37876423Sgw25295 37886423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 37896423Sgw25295 goto out; 37906423Sgw25295 } 37916423Sgw25295 ret = 0; 37926423Sgw25295 37936423Sgw25295 out: 37946423Sgw25295 if (zhp) 37956423Sgw25295 zpool_close(zhp); 37966423Sgw25295 libzfs_fini(hdl); 37976423Sgw25295 return (ret); 37986423Sgw25295 } 3799