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 /* 23*8525SEric.Schrock@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 273126Sahl #include <alloca.h> 28789Sahrens #include <assert.h> 29789Sahrens #include <ctype.h> 30789Sahrens #include <errno.h> 31789Sahrens #include <devid.h> 323126Sahl #include <dirent.h> 33789Sahrens #include <fcntl.h> 34789Sahrens #include <libintl.h> 35789Sahrens #include <stdio.h> 36789Sahrens #include <stdlib.h> 373126Sahl #include <strings.h> 38789Sahrens #include <unistd.h> 397184Stimh #include <zone.h> 404276Staylor #include <sys/efi_partition.h> 414276Staylor #include <sys/vtoc.h> 42789Sahrens #include <sys/zfs_ioctl.h> 431544Seschrock #include <sys/zio.h> 442926Sek110237 #include <strings.h> 45789Sahrens 46789Sahrens #include "zfs_namecheck.h" 473912Slling #include "zfs_prop.h" 48789Sahrens #include "libzfs_impl.h" 49789Sahrens 507042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 515094Slling 527965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64) 537965SGeorge.Wilson@Sun.COM #define BOOTCMD "installgrub(1M)" 547965SGeorge.Wilson@Sun.COM #else 557965SGeorge.Wilson@Sun.COM #define BOOTCMD "installboot(1M)" 567965SGeorge.Wilson@Sun.COM #endif 577965SGeorge.Wilson@Sun.COM 585094Slling /* 595094Slling * ==================================================================== 605094Slling * zpool property functions 615094Slling * ==================================================================== 625094Slling */ 635094Slling 645094Slling static int 655094Slling zpool_get_all_props(zpool_handle_t *zhp) 665094Slling { 675094Slling zfs_cmd_t zc = { 0 }; 685094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 695094Slling 705094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 715094Slling 725094Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 735094Slling return (-1); 745094Slling 755094Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 765094Slling if (errno == ENOMEM) { 775094Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 785094Slling zcmd_free_nvlists(&zc); 795094Slling return (-1); 805094Slling } 815094Slling } else { 825094Slling zcmd_free_nvlists(&zc); 835094Slling return (-1); 845094Slling } 855094Slling } 865094Slling 875094Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 885094Slling zcmd_free_nvlists(&zc); 895094Slling return (-1); 905094Slling } 915094Slling 925094Slling zcmd_free_nvlists(&zc); 935094Slling 945094Slling return (0); 955094Slling } 965094Slling 975094Slling static int 985094Slling zpool_props_refresh(zpool_handle_t *zhp) 995094Slling { 1005094Slling nvlist_t *old_props; 1015094Slling 1025094Slling old_props = zhp->zpool_props; 1035094Slling 1045094Slling if (zpool_get_all_props(zhp) != 0) 1055094Slling return (-1); 1065094Slling 1075094Slling nvlist_free(old_props); 1085094Slling return (0); 1095094Slling } 1105094Slling 1115094Slling static char * 1125094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 1135094Slling zprop_source_t *src) 1145094Slling { 1155094Slling nvlist_t *nv, *nvl; 1165094Slling uint64_t ival; 1175094Slling char *value; 1185094Slling zprop_source_t source; 1195094Slling 1205094Slling nvl = zhp->zpool_props; 1215094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1225094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 1235094Slling source = ival; 1245094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1255094Slling } else { 1265094Slling source = ZPROP_SRC_DEFAULT; 1275094Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 1285094Slling value = "-"; 1295094Slling } 1305094Slling 1315094Slling if (src) 1325094Slling *src = source; 1335094Slling 1345094Slling return (value); 1355094Slling } 1365094Slling 1375094Slling uint64_t 1385094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 1395094Slling { 1405094Slling nvlist_t *nv, *nvl; 1415094Slling uint64_t value; 1425094Slling zprop_source_t source; 1435094Slling 1447294Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 1457294Sperrin /* 1467294Sperrin * zpool_get_all_props() has most likely failed because 1477294Sperrin * the pool is faulted, but if all we need is the top level 1487294Sperrin * vdev's guid then get it from the zhp config nvlist. 1497294Sperrin */ 1507294Sperrin if ((prop == ZPOOL_PROP_GUID) && 1517294Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 1527294Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 1537294Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 1547294Sperrin == 0)) { 1557294Sperrin return (value); 1567294Sperrin } 1575094Slling return (zpool_prop_default_numeric(prop)); 1587294Sperrin } 1595094Slling 1605094Slling nvl = zhp->zpool_props; 1615094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1625094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 1635094Slling source = value; 1645094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1655094Slling } else { 1665094Slling source = ZPROP_SRC_DEFAULT; 1675094Slling value = zpool_prop_default_numeric(prop); 1685094Slling } 1695094Slling 1705094Slling if (src) 1715094Slling *src = source; 1725094Slling 1735094Slling return (value); 1745094Slling } 1755094Slling 1765094Slling /* 1775094Slling * Map VDEV STATE to printed strings. 1785094Slling */ 1795094Slling char * 1805094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 1815094Slling { 1825094Slling switch (state) { 1835094Slling case VDEV_STATE_CLOSED: 1845094Slling case VDEV_STATE_OFFLINE: 1855094Slling return (gettext("OFFLINE")); 1865094Slling case VDEV_STATE_REMOVED: 1875094Slling return (gettext("REMOVED")); 1885094Slling case VDEV_STATE_CANT_OPEN: 1897294Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 1905094Slling return (gettext("FAULTED")); 1915094Slling else 1925094Slling return (gettext("UNAVAIL")); 1935094Slling case VDEV_STATE_FAULTED: 1945094Slling return (gettext("FAULTED")); 1955094Slling case VDEV_STATE_DEGRADED: 1965094Slling return (gettext("DEGRADED")); 1975094Slling case VDEV_STATE_HEALTHY: 1985094Slling return (gettext("ONLINE")); 1995094Slling } 2005094Slling 2015094Slling return (gettext("UNKNOWN")); 2025094Slling } 2035094Slling 2045094Slling /* 2055094Slling * Get a zpool property value for 'prop' and return the value in 2065094Slling * a pre-allocated buffer. 2075094Slling */ 2085094Slling int 2095094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 2105094Slling zprop_source_t *srctype) 2115094Slling { 2125094Slling uint64_t intval; 2135094Slling const char *strval; 2145094Slling zprop_source_t src = ZPROP_SRC_NONE; 2155094Slling nvlist_t *nvroot; 2165094Slling vdev_stat_t *vs; 2175094Slling uint_t vsc; 2185094Slling 2195094Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 220*8525SEric.Schrock@Sun.COM switch (prop) { 221*8525SEric.Schrock@Sun.COM case ZPOOL_PROP_NAME: 2225094Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 223*8525SEric.Schrock@Sun.COM break; 224*8525SEric.Schrock@Sun.COM 225*8525SEric.Schrock@Sun.COM case ZPOOL_PROP_HEALTH: 2265094Slling (void) strlcpy(buf, "FAULTED", len); 227*8525SEric.Schrock@Sun.COM break; 228*8525SEric.Schrock@Sun.COM 229*8525SEric.Schrock@Sun.COM case ZPOOL_PROP_GUID: 230*8525SEric.Schrock@Sun.COM intval = zpool_get_prop_int(zhp, prop, &src); 231*8525SEric.Schrock@Sun.COM (void) snprintf(buf, len, "%llu", intval); 232*8525SEric.Schrock@Sun.COM break; 233*8525SEric.Schrock@Sun.COM 234*8525SEric.Schrock@Sun.COM case ZPOOL_PROP_ALTROOT: 235*8525SEric.Schrock@Sun.COM case ZPOOL_PROP_CACHEFILE: 236*8525SEric.Schrock@Sun.COM if (zhp->zpool_props != NULL || 237*8525SEric.Schrock@Sun.COM zpool_get_all_props(zhp) == 0) { 238*8525SEric.Schrock@Sun.COM (void) strlcpy(buf, 239*8525SEric.Schrock@Sun.COM zpool_get_prop_string(zhp, prop, &src), 240*8525SEric.Schrock@Sun.COM len); 241*8525SEric.Schrock@Sun.COM if (srctype != NULL) 242*8525SEric.Schrock@Sun.COM *srctype = src; 243*8525SEric.Schrock@Sun.COM return (0); 244*8525SEric.Schrock@Sun.COM } 245*8525SEric.Schrock@Sun.COM /* FALLTHROUGH */ 246*8525SEric.Schrock@Sun.COM default: 2475094Slling (void) strlcpy(buf, "-", len); 248*8525SEric.Schrock@Sun.COM break; 249*8525SEric.Schrock@Sun.COM } 250*8525SEric.Schrock@Sun.COM 251*8525SEric.Schrock@Sun.COM if (srctype != NULL) 252*8525SEric.Schrock@Sun.COM *srctype = src; 2535094Slling return (0); 2545094Slling } 2555094Slling 2565094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 2575094Slling prop != ZPOOL_PROP_NAME) 2585094Slling return (-1); 2595094Slling 2605094Slling switch (zpool_prop_get_type(prop)) { 2615094Slling case PROP_TYPE_STRING: 2625094Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 2635094Slling len); 2645094Slling break; 2655094Slling 2665094Slling case PROP_TYPE_NUMBER: 2675094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2685094Slling 2695094Slling switch (prop) { 2705094Slling case ZPOOL_PROP_SIZE: 2715094Slling case ZPOOL_PROP_USED: 2725094Slling case ZPOOL_PROP_AVAILABLE: 2735094Slling (void) zfs_nicenum(intval, buf, len); 2745094Slling break; 2755094Slling 2765094Slling case ZPOOL_PROP_CAPACITY: 2775094Slling (void) snprintf(buf, len, "%llu%%", 2785094Slling (u_longlong_t)intval); 2795094Slling break; 2805094Slling 2815094Slling case ZPOOL_PROP_HEALTH: 2825094Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2835094Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2845094Slling verify(nvlist_lookup_uint64_array(nvroot, 2855094Slling ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 2865094Slling 2875094Slling (void) strlcpy(buf, zpool_state_to_name(intval, 2885094Slling vs->vs_aux), len); 2895094Slling break; 2905094Slling default: 2915094Slling (void) snprintf(buf, len, "%llu", intval); 2925094Slling } 2935094Slling break; 2945094Slling 2955094Slling case PROP_TYPE_INDEX: 2965094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2975094Slling if (zpool_prop_index_to_string(prop, intval, &strval) 2985094Slling != 0) 2995094Slling return (-1); 3005094Slling (void) strlcpy(buf, strval, len); 3015094Slling break; 3025094Slling 3035094Slling default: 3045094Slling abort(); 3055094Slling } 3065094Slling 3075094Slling if (srctype) 3085094Slling *srctype = src; 3095094Slling 3105094Slling return (0); 3115094Slling } 3125094Slling 3135094Slling /* 3145094Slling * Check if the bootfs name has the same pool name as it is set to. 3155094Slling * Assuming bootfs is a valid dataset name. 3165094Slling */ 3175094Slling static boolean_t 3185094Slling bootfs_name_valid(const char *pool, char *bootfs) 3195094Slling { 3205094Slling int len = strlen(pool); 3215094Slling 3227300SEric.Taylor@Sun.COM if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 3235094Slling return (B_FALSE); 3245094Slling 3255094Slling if (strncmp(pool, bootfs, len) == 0 && 3265094Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 3275094Slling return (B_TRUE); 3285094Slling 3295094Slling return (B_FALSE); 3305094Slling } 3315094Slling 3325094Slling /* 3337042Sgw25295 * Inspect the configuration to determine if any of the devices contain 3347042Sgw25295 * an EFI label. 3357042Sgw25295 */ 3367042Sgw25295 static boolean_t 3377042Sgw25295 pool_uses_efi(nvlist_t *config) 3387042Sgw25295 { 3397042Sgw25295 nvlist_t **child; 3407042Sgw25295 uint_t c, children; 3417042Sgw25295 3427042Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3437042Sgw25295 &child, &children) != 0) 3447042Sgw25295 return (read_efi_label(config, NULL) >= 0); 3457042Sgw25295 3467042Sgw25295 for (c = 0; c < children; c++) { 3477042Sgw25295 if (pool_uses_efi(child[c])) 3487042Sgw25295 return (B_TRUE); 3497042Sgw25295 } 3507042Sgw25295 return (B_FALSE); 3517042Sgw25295 } 3527042Sgw25295 3537965SGeorge.Wilson@Sun.COM static boolean_t 3547965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp) 3557965SGeorge.Wilson@Sun.COM { 3567965SGeorge.Wilson@Sun.COM char bootfs[ZPOOL_MAXNAMELEN]; 3577965SGeorge.Wilson@Sun.COM 3587965SGeorge.Wilson@Sun.COM return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 3597965SGeorge.Wilson@Sun.COM sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 3607965SGeorge.Wilson@Sun.COM sizeof (bootfs)) != 0); 3617965SGeorge.Wilson@Sun.COM } 3627965SGeorge.Wilson@Sun.COM 3637965SGeorge.Wilson@Sun.COM 3647042Sgw25295 /* 3655094Slling * Given an nvlist of zpool properties to be set, validate that they are 3665094Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 3675094Slling * specified as strings. 3685094Slling */ 3695094Slling static nvlist_t * 3707184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 3715094Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 3725094Slling { 3735094Slling nvpair_t *elem; 3745094Slling nvlist_t *retprops; 3755094Slling zpool_prop_t prop; 3765094Slling char *strval; 3775094Slling uint64_t intval; 3785363Seschrock char *slash; 3795363Seschrock struct stat64 statbuf; 3807042Sgw25295 zpool_handle_t *zhp; 3817042Sgw25295 nvlist_t *nvroot; 3825094Slling 3835094Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 3845094Slling (void) no_memory(hdl); 3855094Slling return (NULL); 3865094Slling } 3875094Slling 3885094Slling elem = NULL; 3895094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3905094Slling const char *propname = nvpair_name(elem); 3915094Slling 3925094Slling /* 3935094Slling * Make sure this property is valid and applies to this type. 3945094Slling */ 3955094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 3965094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3975094Slling "invalid property '%s'"), propname); 3985094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3995094Slling goto error; 4005094Slling } 4015094Slling 4025094Slling if (zpool_prop_readonly(prop)) { 4035094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4045094Slling "is readonly"), propname); 4055094Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 4065094Slling goto error; 4075094Slling } 4085094Slling 4095094Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 4105094Slling &strval, &intval, errbuf) != 0) 4115094Slling goto error; 4125094Slling 4135094Slling /* 4145094Slling * Perform additional checking for specific properties. 4155094Slling */ 4165094Slling switch (prop) { 4175094Slling case ZPOOL_PROP_VERSION: 4185094Slling if (intval < version || intval > SPA_VERSION) { 4195094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4205094Slling "property '%s' number %d is invalid."), 4215094Slling propname, intval); 4225094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4235094Slling goto error; 4245094Slling } 4255094Slling break; 4265094Slling 4275094Slling case ZPOOL_PROP_BOOTFS: 4285094Slling if (create_or_import) { 4295094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4305094Slling "property '%s' cannot be set at creation " 4315094Slling "or import time"), propname); 4325094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4335094Slling goto error; 4345094Slling } 4355094Slling 4365094Slling if (version < SPA_VERSION_BOOTFS) { 4375094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4385094Slling "pool must be upgraded to support " 4395094Slling "'%s' property"), propname); 4405094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4415094Slling goto error; 4425094Slling } 4435094Slling 4445094Slling /* 4455094Slling * bootfs property value has to be a dataset name and 4465094Slling * the dataset has to be in the same pool as it sets to. 4475094Slling */ 4485094Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 4495094Slling strval)) { 4505094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4515094Slling "is an invalid name"), strval); 4525094Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4535094Slling goto error; 4545094Slling } 4557042Sgw25295 4567042Sgw25295 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 4577042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4587042Sgw25295 "could not open pool '%s'"), poolname); 4597042Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4607042Sgw25295 goto error; 4617042Sgw25295 } 4627042Sgw25295 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 4637042Sgw25295 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4647042Sgw25295 4657042Sgw25295 /* 4667042Sgw25295 * bootfs property cannot be set on a disk which has 4677042Sgw25295 * been EFI labeled. 4687042Sgw25295 */ 4697042Sgw25295 if (pool_uses_efi(nvroot)) { 4707042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4717042Sgw25295 "property '%s' not supported on " 4727042Sgw25295 "EFI labeled devices"), propname); 4737042Sgw25295 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 4747042Sgw25295 zpool_close(zhp); 4757042Sgw25295 goto error; 4767042Sgw25295 } 4777042Sgw25295 zpool_close(zhp); 4785094Slling break; 4795094Slling 4805094Slling case ZPOOL_PROP_ALTROOT: 4815094Slling if (!create_or_import) { 4825094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4835094Slling "property '%s' can only be set during pool " 4845094Slling "creation or import"), propname); 4855094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4865094Slling goto error; 4875094Slling } 4885094Slling 4895094Slling if (strval[0] != '/') { 4905094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4915094Slling "bad alternate root '%s'"), strval); 4925094Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4935094Slling goto error; 4945094Slling } 4955094Slling break; 4965363Seschrock 4975363Seschrock case ZPOOL_PROP_CACHEFILE: 4985363Seschrock if (strval[0] == '\0') 4995363Seschrock break; 5005363Seschrock 5015363Seschrock if (strcmp(strval, "none") == 0) 5025363Seschrock break; 5035363Seschrock 5045363Seschrock if (strval[0] != '/') { 5055363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5065363Seschrock "property '%s' must be empty, an " 5075363Seschrock "absolute path, or 'none'"), propname); 5085363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5095363Seschrock goto error; 5105363Seschrock } 5115363Seschrock 5125363Seschrock slash = strrchr(strval, '/'); 5135363Seschrock 5145363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5155363Seschrock strcmp(slash, "/..") == 0) { 5165363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5175363Seschrock "'%s' is not a valid file"), strval); 5185363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5195363Seschrock goto error; 5205363Seschrock } 5215363Seschrock 5225363Seschrock *slash = '\0'; 5235363Seschrock 5245621Seschrock if (strval[0] != '\0' && 5255621Seschrock (stat64(strval, &statbuf) != 0 || 5265621Seschrock !S_ISDIR(statbuf.st_mode))) { 5275363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5285363Seschrock "'%s' is not a valid directory"), 5295363Seschrock strval); 5305363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5315363Seschrock goto error; 5325363Seschrock } 5335363Seschrock 5345363Seschrock *slash = '/'; 5355363Seschrock break; 5365094Slling } 5375094Slling } 5385094Slling 5395094Slling return (retprops); 5405094Slling error: 5415094Slling nvlist_free(retprops); 5425094Slling return (NULL); 5435094Slling } 5445094Slling 5455094Slling /* 5465094Slling * Set zpool property : propname=propval. 5475094Slling */ 5485094Slling int 5495094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 5505094Slling { 5515094Slling zfs_cmd_t zc = { 0 }; 5525094Slling int ret = -1; 5535094Slling char errbuf[1024]; 5545094Slling nvlist_t *nvl = NULL; 5555094Slling nvlist_t *realprops; 5565094Slling uint64_t version; 5575094Slling 5585094Slling (void) snprintf(errbuf, sizeof (errbuf), 5595094Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 5605094Slling zhp->zpool_name); 5615094Slling 5625094Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5635094Slling return (no_memory(zhp->zpool_hdl)); 5645094Slling 5655094Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 5665094Slling nvlist_free(nvl); 5675094Slling return (no_memory(zhp->zpool_hdl)); 5685094Slling } 5695094Slling 5705094Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5717184Stimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 5725094Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 5735094Slling nvlist_free(nvl); 5745094Slling return (-1); 5755094Slling } 5765094Slling 5775094Slling nvlist_free(nvl); 5785094Slling nvl = realprops; 5795094Slling 5805094Slling /* 5815094Slling * Execute the corresponding ioctl() to set this property. 5825094Slling */ 5835094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 5845094Slling 5855094Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 5865094Slling nvlist_free(nvl); 5875094Slling return (-1); 5885094Slling } 5895094Slling 5905094Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 5915094Slling 5925094Slling zcmd_free_nvlists(&zc); 5935094Slling nvlist_free(nvl); 5945094Slling 5955094Slling if (ret) 5965094Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 5975094Slling else 5985094Slling (void) zpool_props_refresh(zhp); 5995094Slling 6005094Slling return (ret); 6015094Slling } 6025094Slling 6035094Slling int 6045094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 6055094Slling { 6065094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 6075094Slling zprop_list_t *entry; 6085094Slling char buf[ZFS_MAXPROPLEN]; 6095094Slling 6105094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 6115094Slling return (-1); 6125094Slling 6135094Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 6145094Slling 6155094Slling if (entry->pl_fixed) 6165094Slling continue; 6175094Slling 6185094Slling if (entry->pl_prop != ZPROP_INVAL && 6195094Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 6205094Slling NULL) == 0) { 6215094Slling if (strlen(buf) > entry->pl_width) 6225094Slling entry->pl_width = strlen(buf); 6235094Slling } 6245094Slling } 6255094Slling 6265094Slling return (0); 6275094Slling } 6285094Slling 6295094Slling 630789Sahrens /* 631789Sahrens * Validate the given pool name, optionally putting an extended error message in 632789Sahrens * 'buf'. 633789Sahrens */ 6346423Sgw25295 boolean_t 6352082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 636789Sahrens { 637789Sahrens namecheck_err_t why; 638789Sahrens char what; 6391773Seschrock int ret; 640789Sahrens 6411773Seschrock ret = pool_namecheck(pool, &why, &what); 6421773Seschrock 6431773Seschrock /* 6441773Seschrock * The rules for reserved pool names were extended at a later point. 6451773Seschrock * But we need to support users with existing pools that may now be 6461773Seschrock * invalid. So we only check for this expanded set of names during a 6471773Seschrock * create (or import), and only in userland. 6481773Seschrock */ 6491773Seschrock if (ret == 0 && !isopen && 6501773Seschrock (strncmp(pool, "mirror", 6) == 0 || 6511773Seschrock strncmp(pool, "raidz", 5) == 0 || 6524527Sperrin strncmp(pool, "spare", 5) == 0 || 6534527Sperrin strcmp(pool, "log") == 0)) { 6546423Sgw25295 if (hdl != NULL) 6556423Sgw25295 zfs_error_aux(hdl, 6566423Sgw25295 dgettext(TEXT_DOMAIN, "name is reserved")); 6572082Seschrock return (B_FALSE); 6581773Seschrock } 6591773Seschrock 6601773Seschrock 6611773Seschrock if (ret != 0) { 6622082Seschrock if (hdl != NULL) { 663789Sahrens switch (why) { 6641003Slling case NAME_ERR_TOOLONG: 6652082Seschrock zfs_error_aux(hdl, 6661003Slling dgettext(TEXT_DOMAIN, "name is too long")); 6671003Slling break; 6681003Slling 669789Sahrens case NAME_ERR_INVALCHAR: 6702082Seschrock zfs_error_aux(hdl, 671789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 672789Sahrens "'%c' in pool name"), what); 673789Sahrens break; 674789Sahrens 675789Sahrens case NAME_ERR_NOLETTER: 6762082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6772082Seschrock "name must begin with a letter")); 678789Sahrens break; 679789Sahrens 680789Sahrens case NAME_ERR_RESERVED: 6812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6822082Seschrock "name is reserved")); 683789Sahrens break; 684789Sahrens 685789Sahrens case NAME_ERR_DISKLIKE: 6862082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6872082Seschrock "pool name is reserved")); 688789Sahrens break; 6892856Snd150628 6902856Snd150628 case NAME_ERR_LEADING_SLASH: 6912856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6922856Snd150628 "leading slash in name")); 6932856Snd150628 break; 6942856Snd150628 6952856Snd150628 case NAME_ERR_EMPTY_COMPONENT: 6962856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6972856Snd150628 "empty component in name")); 6982856Snd150628 break; 6992856Snd150628 7002856Snd150628 case NAME_ERR_TRAILING_SLASH: 7012856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7022856Snd150628 "trailing slash in name")); 7032856Snd150628 break; 7042856Snd150628 7052856Snd150628 case NAME_ERR_MULTIPLE_AT: 7062856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7072856Snd150628 "multiple '@' delimiters in name")); 7082856Snd150628 break; 7092856Snd150628 710789Sahrens } 711789Sahrens } 7122082Seschrock return (B_FALSE); 713789Sahrens } 714789Sahrens 7152082Seschrock return (B_TRUE); 716789Sahrens } 717789Sahrens 718789Sahrens /* 719789Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 720789Sahrens * state. 721789Sahrens */ 722789Sahrens zpool_handle_t * 7232082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 724789Sahrens { 725789Sahrens zpool_handle_t *zhp; 7262142Seschrock boolean_t missing; 727789Sahrens 728789Sahrens /* 729789Sahrens * Make sure the pool name is valid. 730789Sahrens */ 7312082Seschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 7323237Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 7332082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 7342082Seschrock pool); 735789Sahrens return (NULL); 736789Sahrens } 737789Sahrens 7382082Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7392082Seschrock return (NULL); 740789Sahrens 7412082Seschrock zhp->zpool_hdl = hdl; 742789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 743789Sahrens 7442142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7452142Seschrock zpool_close(zhp); 7462142Seschrock return (NULL); 7472142Seschrock } 7482142Seschrock 7492142Seschrock if (missing) { 7505094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 7513237Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 7525094Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 7532142Seschrock zpool_close(zhp); 7542142Seschrock return (NULL); 755789Sahrens } 756789Sahrens 757789Sahrens return (zhp); 758789Sahrens } 759789Sahrens 760789Sahrens /* 761789Sahrens * Like the above, but silent on error. Used when iterating over pools (because 762789Sahrens * the configuration cache may be out of date). 763789Sahrens */ 7642142Seschrock int 7652142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 766789Sahrens { 767789Sahrens zpool_handle_t *zhp; 7682142Seschrock boolean_t missing; 769789Sahrens 7702142Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7712142Seschrock return (-1); 772789Sahrens 7732082Seschrock zhp->zpool_hdl = hdl; 774789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 775789Sahrens 7762142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7772142Seschrock zpool_close(zhp); 7782142Seschrock return (-1); 779789Sahrens } 780789Sahrens 7812142Seschrock if (missing) { 7822142Seschrock zpool_close(zhp); 7832142Seschrock *ret = NULL; 7842142Seschrock return (0); 7852142Seschrock } 7862142Seschrock 7872142Seschrock *ret = zhp; 7882142Seschrock return (0); 789789Sahrens } 790789Sahrens 791789Sahrens /* 792789Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 793789Sahrens * state. 794789Sahrens */ 795789Sahrens zpool_handle_t * 7962082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 797789Sahrens { 798789Sahrens zpool_handle_t *zhp; 799789Sahrens 8002082Seschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 801789Sahrens return (NULL); 802789Sahrens 803789Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 8043237Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 8052082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 806789Sahrens zpool_close(zhp); 807789Sahrens return (NULL); 808789Sahrens } 809789Sahrens 810789Sahrens return (zhp); 811789Sahrens } 812789Sahrens 813789Sahrens /* 814789Sahrens * Close the handle. Simply frees the memory associated with the handle. 815789Sahrens */ 816789Sahrens void 817789Sahrens zpool_close(zpool_handle_t *zhp) 818789Sahrens { 819789Sahrens if (zhp->zpool_config) 820789Sahrens nvlist_free(zhp->zpool_config); 821952Seschrock if (zhp->zpool_old_config) 822952Seschrock nvlist_free(zhp->zpool_old_config); 8233912Slling if (zhp->zpool_props) 8243912Slling nvlist_free(zhp->zpool_props); 825789Sahrens free(zhp); 826789Sahrens } 827789Sahrens 828789Sahrens /* 829789Sahrens * Return the name of the pool. 830789Sahrens */ 831789Sahrens const char * 832789Sahrens zpool_get_name(zpool_handle_t *zhp) 833789Sahrens { 834789Sahrens return (zhp->zpool_name); 835789Sahrens } 836789Sahrens 837789Sahrens 838789Sahrens /* 839789Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 840789Sahrens */ 841789Sahrens int 842789Sahrens zpool_get_state(zpool_handle_t *zhp) 843789Sahrens { 844789Sahrens return (zhp->zpool_state); 845789Sahrens } 846789Sahrens 847789Sahrens /* 848789Sahrens * Create the named pool, using the provided vdev list. It is assumed 849789Sahrens * that the consumer has already validated the contents of the nvlist, so we 850789Sahrens * don't have to worry about error semantics. 851789Sahrens */ 852789Sahrens int 8532082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8547184Stimh nvlist_t *props, nvlist_t *fsprops) 855789Sahrens { 856789Sahrens zfs_cmd_t zc = { 0 }; 8577184Stimh nvlist_t *zc_fsprops = NULL; 8587184Stimh nvlist_t *zc_props = NULL; 8592082Seschrock char msg[1024]; 8605094Slling char *altroot; 8617184Stimh int ret = -1; 8622082Seschrock 8632082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 8642082Seschrock "cannot create '%s'"), pool); 865789Sahrens 8662082Seschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 8672082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 8682082Seschrock 8695320Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 8705320Slling return (-1); 8715320Slling 8727184Stimh if (props) { 8737184Stimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8747184Stimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8757184Stimh goto create_failed; 8767184Stimh } 8775320Slling } 878789Sahrens 8797184Stimh if (fsprops) { 8807184Stimh uint64_t zoned; 8817184Stimh char *zonestr; 8827184Stimh 8837184Stimh zoned = ((nvlist_lookup_string(fsprops, 8847184Stimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8857184Stimh strcmp(zonestr, "on") == 0); 8867184Stimh 8877184Stimh if ((zc_fsprops = zfs_valid_proplist(hdl, 8887184Stimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 8897184Stimh goto create_failed; 8907184Stimh } 8917184Stimh if (!zc_props && 8927184Stimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 8937184Stimh goto create_failed; 8947184Stimh } 8957184Stimh if (nvlist_add_nvlist(zc_props, 8967184Stimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 8977184Stimh goto create_failed; 8987184Stimh } 8997184Stimh } 9007184Stimh 9017184Stimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 9027184Stimh goto create_failed; 9037184Stimh 904789Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 905789Sahrens 9067184Stimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 9075320Slling 9082676Seschrock zcmd_free_nvlists(&zc); 9097184Stimh nvlist_free(zc_props); 9107184Stimh nvlist_free(zc_fsprops); 9112082Seschrock 912789Sahrens switch (errno) { 913789Sahrens case EBUSY: 914789Sahrens /* 915789Sahrens * This can happen if the user has specified the same 916789Sahrens * device multiple times. We can't reliably detect this 917789Sahrens * until we try to add it and see we already have a 918789Sahrens * label. 919789Sahrens */ 9202082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9212082Seschrock "one or more vdevs refer to the same device")); 9222082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 923789Sahrens 924789Sahrens case EOVERFLOW: 925789Sahrens /* 9262082Seschrock * This occurs when one of the devices is below 927789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 928789Sahrens * device was the problem device since there's no 929789Sahrens * reliable way to determine device size from userland. 930789Sahrens */ 931789Sahrens { 932789Sahrens char buf[64]; 933789Sahrens 934789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 935789Sahrens 9362082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9372082Seschrock "one or more devices is less than the " 9382082Seschrock "minimum size (%s)"), buf); 939789Sahrens } 9402082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 941789Sahrens 942789Sahrens case ENOSPC: 9432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9442082Seschrock "one or more devices is out of space")); 9452082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 946789Sahrens 9475450Sbrendan case ENOTBLK: 9485450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9495450Sbrendan "cache device must be a disk or disk slice")); 9505450Sbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 9515450Sbrendan 952789Sahrens default: 9532082Seschrock return (zpool_standard_error(hdl, errno, msg)); 954789Sahrens } 955789Sahrens } 956789Sahrens 957789Sahrens /* 958789Sahrens * If this is an alternate root pool, then we automatically set the 9592676Seschrock * mountpoint of the root dataset to be '/'. 960789Sahrens */ 9615094Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 9625094Slling &altroot) == 0) { 963789Sahrens zfs_handle_t *zhp; 964789Sahrens 9655094Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 9662676Seschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 9672676Seschrock "/") == 0); 968789Sahrens 969789Sahrens zfs_close(zhp); 970789Sahrens } 971789Sahrens 9727184Stimh create_failed: 9735320Slling zcmd_free_nvlists(&zc); 9747184Stimh nvlist_free(zc_props); 9757184Stimh nvlist_free(zc_fsprops); 9767184Stimh return (ret); 977789Sahrens } 978789Sahrens 979789Sahrens /* 980789Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 981789Sahrens * datasets left in the pool. 982789Sahrens */ 983789Sahrens int 984789Sahrens zpool_destroy(zpool_handle_t *zhp) 985789Sahrens { 986789Sahrens zfs_cmd_t zc = { 0 }; 987789Sahrens zfs_handle_t *zfp = NULL; 9882082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9892082Seschrock char msg[1024]; 990789Sahrens 991789Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 9922082Seschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 9932082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 994789Sahrens return (-1); 995789Sahrens 9962856Snd150628 if (zpool_remove_zvol_links(zhp) != 0) 997789Sahrens return (-1); 998789Sahrens 999789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1000789Sahrens 10014543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 10022082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10032082Seschrock "cannot destroy '%s'"), zhp->zpool_name); 1004789Sahrens 10052082Seschrock if (errno == EROFS) { 10062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10072082Seschrock "one or more devices is read only")); 10082082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 10092082Seschrock } else { 10102082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1011789Sahrens } 1012789Sahrens 1013789Sahrens if (zfp) 1014789Sahrens zfs_close(zfp); 1015789Sahrens return (-1); 1016789Sahrens } 1017789Sahrens 1018789Sahrens if (zfp) { 1019789Sahrens remove_mountpoint(zfp); 1020789Sahrens zfs_close(zfp); 1021789Sahrens } 1022789Sahrens 1023789Sahrens return (0); 1024789Sahrens } 1025789Sahrens 1026789Sahrens /* 1027789Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1028789Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1029789Sahrens */ 1030789Sahrens int 1031789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1032789Sahrens { 10332676Seschrock zfs_cmd_t zc = { 0 }; 10342082Seschrock int ret; 10352082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10362082Seschrock char msg[1024]; 10375450Sbrendan nvlist_t **spares, **l2cache; 10385450Sbrendan uint_t nspares, nl2cache; 10392082Seschrock 10402082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10412082Seschrock "cannot add to '%s'"), zhp->zpool_name); 10422082Seschrock 10435450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10445450Sbrendan SPA_VERSION_SPARES && 10452082Seschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10462082Seschrock &spares, &nspares) == 0) { 10472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10482082Seschrock "upgraded to add hot spares")); 10492082Seschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10502082Seschrock } 1051789Sahrens 10527965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 10537965SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 10547965SGeorge.Wilson@Sun.COM uint64_t s; 10557965SGeorge.Wilson@Sun.COM 10567965SGeorge.Wilson@Sun.COM for (s = 0; s < nspares; s++) { 10577965SGeorge.Wilson@Sun.COM char *path; 10587965SGeorge.Wilson@Sun.COM 10597965SGeorge.Wilson@Sun.COM if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 10607965SGeorge.Wilson@Sun.COM &path) == 0 && pool_uses_efi(spares[s])) { 10617965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10627965SGeorge.Wilson@Sun.COM "device '%s' contains an EFI label and " 10637965SGeorge.Wilson@Sun.COM "cannot be used on root pools."), 10647965SGeorge.Wilson@Sun.COM zpool_vdev_name(hdl, NULL, spares[s])); 10657965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 10667965SGeorge.Wilson@Sun.COM } 10677965SGeorge.Wilson@Sun.COM } 10687965SGeorge.Wilson@Sun.COM } 10697965SGeorge.Wilson@Sun.COM 10705450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10715450Sbrendan SPA_VERSION_L2CACHE && 10725450Sbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 10735450Sbrendan &l2cache, &nl2cache) == 0) { 10745450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10755450Sbrendan "upgraded to add cache devices")); 10765450Sbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10775450Sbrendan } 10785450Sbrendan 10795094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 10802082Seschrock return (-1); 1081789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1082789Sahrens 10834543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1084789Sahrens switch (errno) { 1085789Sahrens case EBUSY: 1086789Sahrens /* 1087789Sahrens * This can happen if the user has specified the same 1088789Sahrens * device multiple times. We can't reliably detect this 1089789Sahrens * until we try to add it and see we already have a 1090789Sahrens * label. 1091789Sahrens */ 10922082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10932082Seschrock "one or more vdevs refer to the same device")); 10942082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1095789Sahrens break; 1096789Sahrens 1097789Sahrens case EOVERFLOW: 1098789Sahrens /* 1099789Sahrens * This occurrs when one of the devices is below 1100789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1101789Sahrens * device was the problem device since there's no 1102789Sahrens * reliable way to determine device size from userland. 1103789Sahrens */ 1104789Sahrens { 1105789Sahrens char buf[64]; 1106789Sahrens 1107789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1108789Sahrens 11092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11102082Seschrock "device is less than the minimum " 11112082Seschrock "size (%s)"), buf); 1112789Sahrens } 11132082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 11142082Seschrock break; 11152082Seschrock 11162082Seschrock case ENOTSUP: 11172082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11184527Sperrin "pool must be upgraded to add these vdevs")); 11192082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1120789Sahrens break; 1121789Sahrens 11223912Slling case EDOM: 11233912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11244527Sperrin "root pool can not have multiple vdevs" 11254527Sperrin " or separate logs")); 11263912Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 11273912Slling break; 11283912Slling 11295450Sbrendan case ENOTBLK: 11305450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11315450Sbrendan "cache device must be a disk or disk slice")); 11325450Sbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 11335450Sbrendan break; 11345450Sbrendan 1135789Sahrens default: 11362082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1137789Sahrens } 1138789Sahrens 11392082Seschrock ret = -1; 11402082Seschrock } else { 11412082Seschrock ret = 0; 1142789Sahrens } 1143789Sahrens 11442676Seschrock zcmd_free_nvlists(&zc); 1145789Sahrens 11462082Seschrock return (ret); 1147789Sahrens } 1148789Sahrens 1149789Sahrens /* 1150789Sahrens * Exports the pool from the system. The caller must ensure that there are no 1151789Sahrens * mounted datasets in the pool. 1152789Sahrens */ 1153789Sahrens int 11548211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1155789Sahrens { 1156789Sahrens zfs_cmd_t zc = { 0 }; 11577214Slling char msg[1024]; 1158789Sahrens 1159789Sahrens if (zpool_remove_zvol_links(zhp) != 0) 1160789Sahrens return (-1); 1161789Sahrens 11627214Slling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 11637214Slling "cannot export '%s'"), zhp->zpool_name); 11647214Slling 1165789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 11667214Slling zc.zc_cookie = force; 11678211SGeorge.Wilson@Sun.COM zc.zc_guid = hardforce; 11687214Slling 11697214Slling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 11707214Slling switch (errno) { 11717214Slling case EXDEV: 11727214Slling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 11737214Slling "use '-f' to override the following errors:\n" 11747214Slling "'%s' has an active shared spare which could be" 11757214Slling " used by other pools once '%s' is exported."), 11767214Slling zhp->zpool_name, zhp->zpool_name); 11777214Slling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 11787214Slling msg)); 11797214Slling default: 11807214Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 11817214Slling msg)); 11827214Slling } 11837214Slling } 11847214Slling 1185789Sahrens return (0); 1186789Sahrens } 1187789Sahrens 11888211SGeorge.Wilson@Sun.COM int 11898211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force) 11908211SGeorge.Wilson@Sun.COM { 11918211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, force, B_FALSE)); 11928211SGeorge.Wilson@Sun.COM } 11938211SGeorge.Wilson@Sun.COM 11948211SGeorge.Wilson@Sun.COM int 11958211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp) 11968211SGeorge.Wilson@Sun.COM { 11978211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 11988211SGeorge.Wilson@Sun.COM } 11998211SGeorge.Wilson@Sun.COM 1200789Sahrens /* 12015094Slling * zpool_import() is a contracted interface. Should be kept the same 12025094Slling * if possible. 12035094Slling * 12045094Slling * Applications should use zpool_import_props() to import a pool with 12055094Slling * new properties value to be set. 1206789Sahrens */ 1207789Sahrens int 12082082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 12095094Slling char *altroot) 12105094Slling { 12115094Slling nvlist_t *props = NULL; 12125094Slling int ret; 12135094Slling 12145094Slling if (altroot != NULL) { 12155094Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 12165094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 12175094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 12185094Slling newname)); 12195094Slling } 12205094Slling 12215094Slling if (nvlist_add_string(props, 12228084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 12238084SGeorge.Wilson@Sun.COM nvlist_add_string(props, 12248084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 12255094Slling nvlist_free(props); 12265094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 12275094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 12285094Slling newname)); 12295094Slling } 12305094Slling } 12315094Slling 12326643Seschrock ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 12335094Slling if (props) 12345094Slling nvlist_free(props); 12355094Slling return (ret); 12365094Slling } 12375094Slling 12385094Slling /* 12395094Slling * Import the given pool using the known configuration and a list of 12405094Slling * properties to be set. The configuration should have come from 12415094Slling * zpool_find_import(). The 'newname' parameters control whether the pool 12425094Slling * is imported with a different name. 12435094Slling */ 12445094Slling int 12455094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 12466643Seschrock nvlist_t *props, boolean_t importfaulted) 1247789Sahrens { 12482676Seschrock zfs_cmd_t zc = { 0 }; 1249789Sahrens char *thename; 1250789Sahrens char *origname; 1251789Sahrens int ret; 12525094Slling char errbuf[1024]; 1253789Sahrens 1254789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1255789Sahrens &origname) == 0); 1256789Sahrens 12575094Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 12585094Slling "cannot import pool '%s'"), origname); 12595094Slling 1260789Sahrens if (newname != NULL) { 12612082Seschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 12623237Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 12632082Seschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 12642082Seschrock newname)); 1265789Sahrens thename = (char *)newname; 1266789Sahrens } else { 1267789Sahrens thename = origname; 1268789Sahrens } 1269789Sahrens 12705094Slling if (props) { 12715094Slling uint64_t version; 12725094Slling 12735094Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 12745094Slling &version) == 0); 12755094Slling 12767184Stimh if ((props = zpool_valid_proplist(hdl, origname, 12775320Slling props, version, B_TRUE, errbuf)) == NULL) { 12785094Slling return (-1); 12795320Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 12805320Slling nvlist_free(props); 12815094Slling return (-1); 12825320Slling } 12835094Slling } 1284789Sahrens 1285789Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1286789Sahrens 1287789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 12881544Seschrock &zc.zc_guid) == 0); 1289789Sahrens 12905320Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 12915320Slling nvlist_free(props); 12922082Seschrock return (-1); 12935320Slling } 1294789Sahrens 12956643Seschrock zc.zc_cookie = (uint64_t)importfaulted; 1296789Sahrens ret = 0; 12974543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1298789Sahrens char desc[1024]; 1299789Sahrens if (newname == NULL) 1300789Sahrens (void) snprintf(desc, sizeof (desc), 1301789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1302789Sahrens thename); 1303789Sahrens else 1304789Sahrens (void) snprintf(desc, sizeof (desc), 1305789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1306789Sahrens origname, thename); 1307789Sahrens 1308789Sahrens switch (errno) { 13091544Seschrock case ENOTSUP: 13101544Seschrock /* 13111544Seschrock * Unsupported version. 13121544Seschrock */ 13132082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 13141544Seschrock break; 13151544Seschrock 13162174Seschrock case EINVAL: 13172174Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 13182174Seschrock break; 13192174Seschrock 1320789Sahrens default: 13212082Seschrock (void) zpool_standard_error(hdl, errno, desc); 1322789Sahrens } 1323789Sahrens 1324789Sahrens ret = -1; 1325789Sahrens } else { 1326789Sahrens zpool_handle_t *zhp; 13274543Smarks 1328789Sahrens /* 1329789Sahrens * This should never fail, but play it safe anyway. 1330789Sahrens */ 13312142Seschrock if (zpool_open_silent(hdl, thename, &zhp) != 0) { 13322142Seschrock ret = -1; 13332142Seschrock } else if (zhp != NULL) { 1334789Sahrens ret = zpool_create_zvol_links(zhp); 1335789Sahrens zpool_close(zhp); 1336789Sahrens } 13374543Smarks 1338789Sahrens } 1339789Sahrens 13402676Seschrock zcmd_free_nvlists(&zc); 13415320Slling nvlist_free(props); 13425320Slling 1343789Sahrens return (ret); 1344789Sahrens } 1345789Sahrens 1346789Sahrens /* 1347789Sahrens * Scrub the pool. 1348789Sahrens */ 1349789Sahrens int 1350789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) 1351789Sahrens { 1352789Sahrens zfs_cmd_t zc = { 0 }; 1353789Sahrens char msg[1024]; 13542082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1355789Sahrens 1356789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1357789Sahrens zc.zc_cookie = type; 1358789Sahrens 13594543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0) 1360789Sahrens return (0); 1361789Sahrens 1362789Sahrens (void) snprintf(msg, sizeof (msg), 1363789Sahrens dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1364789Sahrens 13652082Seschrock if (errno == EBUSY) 13662082Seschrock return (zfs_error(hdl, EZFS_RESILVERING, msg)); 13672082Seschrock else 13682082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1369789Sahrens } 1370789Sahrens 13712468Sek110237 /* 13722468Sek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 13732468Sek110237 * spare; but FALSE if its an INUSE spare. 13742468Sek110237 */ 13752082Seschrock static nvlist_t * 13762082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, 13777326SEric.Schrock@Sun.COM boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 13781544Seschrock { 13791544Seschrock uint_t c, children; 13801544Seschrock nvlist_t **child; 13812082Seschrock uint64_t theguid, present; 13821544Seschrock char *path; 13831544Seschrock uint64_t wholedisk = 0; 13842082Seschrock nvlist_t *ret; 13857326SEric.Schrock@Sun.COM uint64_t is_log; 13861544Seschrock 13872082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); 13881544Seschrock 13891544Seschrock if (search == NULL && 13901544Seschrock nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { 13911544Seschrock /* 13921544Seschrock * If the device has never been present since import, the only 13931544Seschrock * reliable way to match the vdev is by GUID. 13941544Seschrock */ 13952082Seschrock if (theguid == guid) 13962082Seschrock return (nv); 13971544Seschrock } else if (search != NULL && 13981544Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 13991544Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 14001544Seschrock &wholedisk); 14011544Seschrock if (wholedisk) { 14021544Seschrock /* 14031544Seschrock * For whole disks, the internal path has 's0', but the 14041544Seschrock * path passed in by the user doesn't. 14051544Seschrock */ 14061544Seschrock if (strlen(search) == strlen(path) - 2 && 14071544Seschrock strncmp(search, path, strlen(search)) == 0) 14082082Seschrock return (nv); 14091544Seschrock } else if (strcmp(search, path) == 0) { 14102082Seschrock return (nv); 14111544Seschrock } 14121544Seschrock } 14131544Seschrock 14141544Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 14151544Seschrock &child, &children) != 0) 14162082Seschrock return (NULL); 14171544Seschrock 14187326SEric.Schrock@Sun.COM for (c = 0; c < children; c++) { 14192082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 14207326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 14217326SEric.Schrock@Sun.COM /* 14227326SEric.Schrock@Sun.COM * The 'is_log' value is only set for the toplevel 14237326SEric.Schrock@Sun.COM * vdev, not the leaf vdevs. So we always lookup the 14247326SEric.Schrock@Sun.COM * log device from the root of the vdev tree (where 14257326SEric.Schrock@Sun.COM * 'log' is non-NULL). 14267326SEric.Schrock@Sun.COM */ 14277326SEric.Schrock@Sun.COM if (log != NULL && 14287326SEric.Schrock@Sun.COM nvlist_lookup_uint64(child[c], 14297326SEric.Schrock@Sun.COM ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 14307326SEric.Schrock@Sun.COM is_log) { 14317326SEric.Schrock@Sun.COM *log = B_TRUE; 14327326SEric.Schrock@Sun.COM } 14331544Seschrock return (ret); 14347326SEric.Schrock@Sun.COM } 14357326SEric.Schrock@Sun.COM } 14361544Seschrock 14372082Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 14382082Seschrock &child, &children) == 0) { 14392082Seschrock for (c = 0; c < children; c++) { 14402082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 14417326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 14422468Sek110237 *avail_spare = B_TRUE; 14432082Seschrock return (ret); 14442082Seschrock } 14452082Seschrock } 14462082Seschrock } 14472082Seschrock 14485450Sbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 14495450Sbrendan &child, &children) == 0) { 14505450Sbrendan for (c = 0; c < children; c++) { 14515450Sbrendan if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 14527326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 14535450Sbrendan *l2cache = B_TRUE; 14545450Sbrendan return (ret); 14555450Sbrendan } 14565450Sbrendan } 14575450Sbrendan } 14585450Sbrendan 14592082Seschrock return (NULL); 14601544Seschrock } 14611544Seschrock 14622082Seschrock nvlist_t * 14635450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 14647326SEric.Schrock@Sun.COM boolean_t *l2cache, boolean_t *log) 14651544Seschrock { 14661544Seschrock char buf[MAXPATHLEN]; 14671544Seschrock const char *search; 14681544Seschrock char *end; 14691544Seschrock nvlist_t *nvroot; 14701544Seschrock uint64_t guid; 14711544Seschrock 14721613Seschrock guid = strtoull(path, &end, 10); 14731544Seschrock if (guid != 0 && *end == '\0') { 14741544Seschrock search = NULL; 14751544Seschrock } else if (path[0] != '/') { 14761544Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 14771544Seschrock search = buf; 14781544Seschrock } else { 14791544Seschrock search = path; 14801544Seschrock } 14811544Seschrock 14821544Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 14831544Seschrock &nvroot) == 0); 14841544Seschrock 14852468Sek110237 *avail_spare = B_FALSE; 14865450Sbrendan *l2cache = B_FALSE; 14877326SEric.Schrock@Sun.COM if (log != NULL) 14887326SEric.Schrock@Sun.COM *log = B_FALSE; 14895450Sbrendan return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare, 14907326SEric.Schrock@Sun.COM l2cache, log)); 14912468Sek110237 } 14922468Sek110237 14937656SSherry.Moore@Sun.COM static int 14947656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv) 14957656SSherry.Moore@Sun.COM { 14967656SSherry.Moore@Sun.COM uint64_t ival; 14977656SSherry.Moore@Sun.COM 14987656SSherry.Moore@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 14997656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 15007656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 15017656SSherry.Moore@Sun.COM return (0); 15027656SSherry.Moore@Sun.COM 15037656SSherry.Moore@Sun.COM return (1); 15047656SSherry.Moore@Sun.COM } 15057656SSherry.Moore@Sun.COM 15067656SSherry.Moore@Sun.COM /* 15077656SSherry.Moore@Sun.COM * Get phys_path for a root pool 15087656SSherry.Moore@Sun.COM * Return 0 on success; non-zeron on failure. 15097656SSherry.Moore@Sun.COM */ 15107656SSherry.Moore@Sun.COM int 15117656SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath) 15127656SSherry.Moore@Sun.COM { 15137656SSherry.Moore@Sun.COM nvlist_t *vdev_root; 15147656SSherry.Moore@Sun.COM nvlist_t **child; 15157656SSherry.Moore@Sun.COM uint_t count; 15167656SSherry.Moore@Sun.COM int i; 15177656SSherry.Moore@Sun.COM 15187656SSherry.Moore@Sun.COM /* 15197656SSherry.Moore@Sun.COM * Make sure this is a root pool, as phys_path doesn't mean 15207656SSherry.Moore@Sun.COM * anything to a non-root pool. 15217656SSherry.Moore@Sun.COM */ 15227965SGeorge.Wilson@Sun.COM if (!pool_is_bootable(zhp)) 15237656SSherry.Moore@Sun.COM return (-1); 15247656SSherry.Moore@Sun.COM 15257656SSherry.Moore@Sun.COM verify(nvlist_lookup_nvlist(zhp->zpool_config, 15267656SSherry.Moore@Sun.COM ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0); 15277656SSherry.Moore@Sun.COM 15287656SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 15297656SSherry.Moore@Sun.COM &child, &count) != 0) 15307656SSherry.Moore@Sun.COM return (-2); 15317656SSherry.Moore@Sun.COM 15327656SSherry.Moore@Sun.COM for (i = 0; i < count; i++) { 15337656SSherry.Moore@Sun.COM nvlist_t **child2; 15347656SSherry.Moore@Sun.COM uint_t count2; 15357656SSherry.Moore@Sun.COM char *type; 15367656SSherry.Moore@Sun.COM char *tmppath; 15377656SSherry.Moore@Sun.COM int j; 15387656SSherry.Moore@Sun.COM 15397656SSherry.Moore@Sun.COM if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type) 15407656SSherry.Moore@Sun.COM != 0) 15417656SSherry.Moore@Sun.COM return (-3); 15427656SSherry.Moore@Sun.COM 15437656SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_DISK) == 0) { 15447656SSherry.Moore@Sun.COM if (!vdev_online(child[i])) 15457656SSherry.Moore@Sun.COM return (-8); 15467656SSherry.Moore@Sun.COM verify(nvlist_lookup_string(child[i], 15477656SSherry.Moore@Sun.COM ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0); 15487656SSherry.Moore@Sun.COM (void) strncpy(physpath, tmppath, strlen(tmppath)); 15497656SSherry.Moore@Sun.COM } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) { 15507656SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist_array(child[i], 15517656SSherry.Moore@Sun.COM ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0) 15527656SSherry.Moore@Sun.COM return (-4); 15537656SSherry.Moore@Sun.COM 15547656SSherry.Moore@Sun.COM for (j = 0; j < count2; j++) { 15557656SSherry.Moore@Sun.COM if (!vdev_online(child2[j])) 15567656SSherry.Moore@Sun.COM return (-8); 15577656SSherry.Moore@Sun.COM if (nvlist_lookup_string(child2[j], 15587656SSherry.Moore@Sun.COM ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0) 15597656SSherry.Moore@Sun.COM return (-5); 15607656SSherry.Moore@Sun.COM 15617656SSherry.Moore@Sun.COM if ((strlen(physpath) + strlen(tmppath)) > 15627656SSherry.Moore@Sun.COM MAXNAMELEN) 15637656SSherry.Moore@Sun.COM return (-6); 15647656SSherry.Moore@Sun.COM 15657656SSherry.Moore@Sun.COM if (strlen(physpath) == 0) { 15667656SSherry.Moore@Sun.COM (void) strncpy(physpath, tmppath, 15677656SSherry.Moore@Sun.COM strlen(tmppath)); 15687656SSherry.Moore@Sun.COM } else { 15697656SSherry.Moore@Sun.COM (void) strcat(physpath, " "); 15707656SSherry.Moore@Sun.COM (void) strcat(physpath, tmppath); 15717656SSherry.Moore@Sun.COM } 15727656SSherry.Moore@Sun.COM } 15737656SSherry.Moore@Sun.COM } else { 15747656SSherry.Moore@Sun.COM return (-7); 15757656SSherry.Moore@Sun.COM } 15767656SSherry.Moore@Sun.COM } 15777656SSherry.Moore@Sun.COM 15787656SSherry.Moore@Sun.COM return (0); 15797656SSherry.Moore@Sun.COM } 15807656SSherry.Moore@Sun.COM 15812468Sek110237 /* 15825450Sbrendan * Returns TRUE if the given guid corresponds to the given type. 15835450Sbrendan * This is used to check for hot spares (INUSE or not), and level 2 cache 15845450Sbrendan * devices. 15852468Sek110237 */ 15862468Sek110237 static boolean_t 15875450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 15882468Sek110237 { 15895450Sbrendan uint64_t target_guid; 15902468Sek110237 nvlist_t *nvroot; 15915450Sbrendan nvlist_t **list; 15925450Sbrendan uint_t count; 15932468Sek110237 int i; 15942468Sek110237 15952468Sek110237 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 15962468Sek110237 &nvroot) == 0); 15975450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 15985450Sbrendan for (i = 0; i < count; i++) { 15995450Sbrendan verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 16005450Sbrendan &target_guid) == 0); 16015450Sbrendan if (guid == target_guid) 16022468Sek110237 return (B_TRUE); 16032468Sek110237 } 16042468Sek110237 } 16052468Sek110237 16062468Sek110237 return (B_FALSE); 16071544Seschrock } 16081544Seschrock 1609789Sahrens /* 16104451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 16114451Seschrock * ZFS_ONLINE_* flags. 1612789Sahrens */ 1613789Sahrens int 16144451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 16154451Seschrock vdev_state_t *newstate) 1616789Sahrens { 1617789Sahrens zfs_cmd_t zc = { 0 }; 1618789Sahrens char msg[1024]; 16192082Seschrock nvlist_t *tgt; 16205450Sbrendan boolean_t avail_spare, l2cache; 16212082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1622789Sahrens 16231544Seschrock (void) snprintf(msg, sizeof (msg), 16241544Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1625789Sahrens 16261544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16277326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 16287326SEric.Schrock@Sun.COM NULL)) == NULL) 16292082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1630789Sahrens 16312468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 16322468Sek110237 16335450Sbrendan if (avail_spare || 16345450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 16352082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16362082Seschrock 16374451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 16384451Seschrock zc.zc_obj = flags; 16394451Seschrock 16404543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 16414451Seschrock return (zpool_standard_error(hdl, errno, msg)); 16424451Seschrock 16434451Seschrock *newstate = zc.zc_cookie; 16444451Seschrock return (0); 1645789Sahrens } 1646789Sahrens 1647789Sahrens /* 1648789Sahrens * Take the specified vdev offline 1649789Sahrens */ 1650789Sahrens int 16514451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1652789Sahrens { 1653789Sahrens zfs_cmd_t zc = { 0 }; 1654789Sahrens char msg[1024]; 16552082Seschrock nvlist_t *tgt; 16565450Sbrendan boolean_t avail_spare, l2cache; 16572082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1658789Sahrens 16591544Seschrock (void) snprintf(msg, sizeof (msg), 16601544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 16611544Seschrock 1662789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16637326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 16647326SEric.Schrock@Sun.COM NULL)) == NULL) 16652082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 16662082Seschrock 16672468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 16682468Sek110237 16695450Sbrendan if (avail_spare || 16705450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 16712082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16722082Seschrock 16734451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 16744451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 16751485Slling 16764543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1677789Sahrens return (0); 1678789Sahrens 1679789Sahrens switch (errno) { 16802082Seschrock case EBUSY: 1681789Sahrens 1682789Sahrens /* 1683789Sahrens * There are no other replicas of this device. 1684789Sahrens */ 16852082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 16862082Seschrock 16872082Seschrock default: 16882082Seschrock return (zpool_standard_error(hdl, errno, msg)); 16892082Seschrock } 16902082Seschrock } 1691789Sahrens 16922082Seschrock /* 16934451Seschrock * Mark the given vdev faulted. 16944451Seschrock */ 16954451Seschrock int 16964451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 16974451Seschrock { 16984451Seschrock zfs_cmd_t zc = { 0 }; 16994451Seschrock char msg[1024]; 17004451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17014451Seschrock 17024451Seschrock (void) snprintf(msg, sizeof (msg), 17034451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 17044451Seschrock 17054451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17064451Seschrock zc.zc_guid = guid; 17074451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 17084451Seschrock 17094451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 17104451Seschrock return (0); 17114451Seschrock 17124451Seschrock switch (errno) { 17134451Seschrock case EBUSY: 17144451Seschrock 17154451Seschrock /* 17164451Seschrock * There are no other replicas of this device. 17174451Seschrock */ 17184451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 17194451Seschrock 17204451Seschrock default: 17214451Seschrock return (zpool_standard_error(hdl, errno, msg)); 17224451Seschrock } 17234451Seschrock 17244451Seschrock } 17254451Seschrock 17264451Seschrock /* 17274451Seschrock * Mark the given vdev degraded. 17284451Seschrock */ 17294451Seschrock int 17304451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 17314451Seschrock { 17324451Seschrock zfs_cmd_t zc = { 0 }; 17334451Seschrock char msg[1024]; 17344451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17354451Seschrock 17364451Seschrock (void) snprintf(msg, sizeof (msg), 17374451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 17384451Seschrock 17394451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17404451Seschrock zc.zc_guid = guid; 17414451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 17424451Seschrock 17434451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 17444451Seschrock return (0); 17454451Seschrock 17464451Seschrock return (zpool_standard_error(hdl, errno, msg)); 17474451Seschrock } 17484451Seschrock 17494451Seschrock /* 17502082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 17512082Seschrock * a hot spare. 17522082Seschrock */ 17532082Seschrock static boolean_t 17542082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 17552082Seschrock { 17562082Seschrock nvlist_t **child; 17572082Seschrock uint_t c, children; 17582082Seschrock char *type; 17592082Seschrock 17602082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 17612082Seschrock &children) == 0) { 17622082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 17632082Seschrock &type) == 0); 17642082Seschrock 17652082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 17662082Seschrock children == 2 && child[which] == tgt) 17672082Seschrock return (B_TRUE); 17682082Seschrock 17692082Seschrock for (c = 0; c < children; c++) 17702082Seschrock if (is_replacing_spare(child[c], tgt, which)) 17712082Seschrock return (B_TRUE); 1772789Sahrens } 17732082Seschrock 17742082Seschrock return (B_FALSE); 1775789Sahrens } 1776789Sahrens 1777789Sahrens /* 1778789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 17794527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 1780789Sahrens */ 1781789Sahrens int 1782789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 1783789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1784789Sahrens { 1785789Sahrens zfs_cmd_t zc = { 0 }; 1786789Sahrens char msg[1024]; 1787789Sahrens int ret; 17882082Seschrock nvlist_t *tgt; 17897326SEric.Schrock@Sun.COM boolean_t avail_spare, l2cache, islog; 17907326SEric.Schrock@Sun.COM uint64_t val; 17917041Seschrock char *path, *newname; 17922082Seschrock nvlist_t **child; 17932082Seschrock uint_t children; 17942082Seschrock nvlist_t *config_root; 17952082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17967965SGeorge.Wilson@Sun.COM boolean_t rootpool = pool_is_bootable(zhp); 1797789Sahrens 17981544Seschrock if (replacing) 17991544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 18001544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 18011544Seschrock else 18021544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 18031544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 18041544Seschrock 18057965SGeorge.Wilson@Sun.COM /* 18067965SGeorge.Wilson@Sun.COM * If this is a root pool, make sure that we're not attaching an 18077965SGeorge.Wilson@Sun.COM * EFI labeled device. 18087965SGeorge.Wilson@Sun.COM */ 18097965SGeorge.Wilson@Sun.COM if (rootpool && pool_uses_efi(nvroot)) { 18107965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18117965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root pools.")); 18127965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 18137965SGeorge.Wilson@Sun.COM } 18147965SGeorge.Wilson@Sun.COM 1815789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18167326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 18177326SEric.Schrock@Sun.COM &islog)) == 0) 18182082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 18192082Seschrock 18202468Sek110237 if (avail_spare) 18212082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 18222082Seschrock 18235450Sbrendan if (l2cache) 18245450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 18255450Sbrendan 18262082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 18272082Seschrock zc.zc_cookie = replacing; 18282082Seschrock 18292082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 18302082Seschrock &child, &children) != 0 || children != 1) { 18312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18322082Seschrock "new device must be a single disk")); 18332082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 18341544Seschrock } 18352082Seschrock 18362082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 18372082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 18382082Seschrock 18397041Seschrock if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL) 18407041Seschrock return (-1); 18417041Seschrock 18422082Seschrock /* 18432082Seschrock * If the target is a hot spare that has been swapped in, we can only 18442082Seschrock * replace it with another hot spare. 18452082Seschrock */ 18462082Seschrock if (replacing && 18472082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 18487326SEric.Schrock@Sun.COM (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 18497326SEric.Schrock@Sun.COM NULL) == NULL || !avail_spare) && 18507326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 1)) { 18512082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18522082Seschrock "can only be replaced by another hot spare")); 18537041Seschrock free(newname); 18542082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 18552082Seschrock } 18562082Seschrock 18572082Seschrock /* 18582082Seschrock * If we are attempting to replace a spare, it canot be applied to an 18592082Seschrock * already spared device. 18602082Seschrock */ 18612082Seschrock if (replacing && 18622082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 18637326SEric.Schrock@Sun.COM zpool_find_vdev(zhp, newname, &avail_spare, 18647326SEric.Schrock@Sun.COM &l2cache, NULL) != NULL && avail_spare && 18657326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 0)) { 18662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18672082Seschrock "device has already been replaced with a spare")); 18687041Seschrock free(newname); 18692082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 18702082Seschrock } 1871789Sahrens 18727041Seschrock free(newname); 18737041Seschrock 18745094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 18752082Seschrock return (-1); 1876789Sahrens 18774543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1878789Sahrens 18792676Seschrock zcmd_free_nvlists(&zc); 1880789Sahrens 18817965SGeorge.Wilson@Sun.COM if (ret == 0) { 18827965SGeorge.Wilson@Sun.COM if (rootpool) { 18837965SGeorge.Wilson@Sun.COM /* 18847965SGeorge.Wilson@Sun.COM * XXX - This should be removed once we can 18857965SGeorge.Wilson@Sun.COM * automatically install the bootblocks on the 18867965SGeorge.Wilson@Sun.COM * newly attached disk. 18877965SGeorge.Wilson@Sun.COM */ 18887965SGeorge.Wilson@Sun.COM (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please " 18897965SGeorge.Wilson@Sun.COM "be sure to invoke %s to make '%s' bootable.\n"), 18907965SGeorge.Wilson@Sun.COM BOOTCMD, new_disk); 18917965SGeorge.Wilson@Sun.COM } 1892789Sahrens return (0); 18937965SGeorge.Wilson@Sun.COM } 1894789Sahrens 1895789Sahrens switch (errno) { 18961544Seschrock case ENOTSUP: 1897789Sahrens /* 1898789Sahrens * Can't attach to or replace this type of vdev. 1899789Sahrens */ 19004527Sperrin if (replacing) { 19017326SEric.Schrock@Sun.COM if (islog) 19024527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19034527Sperrin "cannot replace a log with a spare")); 19044527Sperrin else 19054527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19064527Sperrin "cannot replace a replacing device")); 19074527Sperrin } else { 19082082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19092082Seschrock "can only attach to mirrors and top-level " 19102082Seschrock "disks")); 19114527Sperrin } 19122082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1913789Sahrens break; 1914789Sahrens 19151544Seschrock case EINVAL: 1916789Sahrens /* 1917789Sahrens * The new device must be a single disk. 1918789Sahrens */ 19192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19202082Seschrock "new device must be a single disk")); 19212082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1922789Sahrens break; 1923789Sahrens 19241544Seschrock case EBUSY: 19252082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 19262082Seschrock new_disk); 19272082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1928789Sahrens break; 1929789Sahrens 19301544Seschrock case EOVERFLOW: 1931789Sahrens /* 1932789Sahrens * The new device is too small. 1933789Sahrens */ 19342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19352082Seschrock "device is too small")); 19362082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1937789Sahrens break; 1938789Sahrens 19391544Seschrock case EDOM: 1940789Sahrens /* 1941789Sahrens * The new device has a different alignment requirement. 1942789Sahrens */ 19432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19442082Seschrock "devices have different sector alignment")); 19452082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1946789Sahrens break; 1947789Sahrens 19481544Seschrock case ENAMETOOLONG: 1949789Sahrens /* 1950789Sahrens * The resulting top-level vdev spec won't fit in the label. 1951789Sahrens */ 19522082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 1953789Sahrens break; 1954789Sahrens 19551544Seschrock default: 19562082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1957789Sahrens } 1958789Sahrens 19592082Seschrock return (-1); 1960789Sahrens } 1961789Sahrens 1962789Sahrens /* 1963789Sahrens * Detach the specified device. 1964789Sahrens */ 1965789Sahrens int 1966789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 1967789Sahrens { 1968789Sahrens zfs_cmd_t zc = { 0 }; 1969789Sahrens char msg[1024]; 19702082Seschrock nvlist_t *tgt; 19715450Sbrendan boolean_t avail_spare, l2cache; 19722082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1973789Sahrens 19741544Seschrock (void) snprintf(msg, sizeof (msg), 19751544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 19761544Seschrock 1977789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 19787326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 19797326SEric.Schrock@Sun.COM NULL)) == 0) 19802082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1981789Sahrens 19822468Sek110237 if (avail_spare) 19832082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 19842082Seschrock 19855450Sbrendan if (l2cache) 19865450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 19875450Sbrendan 19882082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 19892082Seschrock 19904543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 1991789Sahrens return (0); 1992789Sahrens 1993789Sahrens switch (errno) { 1994789Sahrens 19951544Seschrock case ENOTSUP: 1996789Sahrens /* 1997789Sahrens * Can't detach from this type of vdev. 1998789Sahrens */ 19992082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 20002082Seschrock "applicable to mirror and replacing vdevs")); 20012082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 2002789Sahrens break; 2003789Sahrens 20041544Seschrock case EBUSY: 2005789Sahrens /* 2006789Sahrens * There are no other replicas of this device. 2007789Sahrens */ 20082082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2009789Sahrens break; 2010789Sahrens 20111544Seschrock default: 20122082Seschrock (void) zpool_standard_error(hdl, errno, msg); 20131544Seschrock } 20141544Seschrock 20152082Seschrock return (-1); 20162082Seschrock } 20172082Seschrock 20182082Seschrock /* 20195450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 20205450Sbrendan * and level 2 cache devices. 20212082Seschrock */ 20222082Seschrock int 20232082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 20242082Seschrock { 20252082Seschrock zfs_cmd_t zc = { 0 }; 20262082Seschrock char msg[1024]; 20272082Seschrock nvlist_t *tgt; 20285450Sbrendan boolean_t avail_spare, l2cache; 20292082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 20302082Seschrock 20312082Seschrock (void) snprintf(msg, sizeof (msg), 20322082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 20332082Seschrock 20342082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20357326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 20367326SEric.Schrock@Sun.COM NULL)) == 0) 20372082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20382082Seschrock 20395450Sbrendan if (!avail_spare && !l2cache) { 20402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20415450Sbrendan "only inactive hot spares or cache devices " 20425450Sbrendan "can be removed")); 20432082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20442082Seschrock } 20452082Seschrock 20462082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 20472082Seschrock 20484543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 20492082Seschrock return (0); 20502082Seschrock 20512082Seschrock return (zpool_standard_error(hdl, errno, msg)); 20521544Seschrock } 20531544Seschrock 20541544Seschrock /* 20551544Seschrock * Clear the errors for the pool, or the particular device if specified. 20561544Seschrock */ 20571544Seschrock int 20581544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path) 20591544Seschrock { 20601544Seschrock zfs_cmd_t zc = { 0 }; 20611544Seschrock char msg[1024]; 20622082Seschrock nvlist_t *tgt; 20635450Sbrendan boolean_t avail_spare, l2cache; 20642082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 20651544Seschrock 20661544Seschrock if (path) 20671544Seschrock (void) snprintf(msg, sizeof (msg), 20681544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 20692676Seschrock path); 20701544Seschrock else 20711544Seschrock (void) snprintf(msg, sizeof (msg), 20721544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 20731544Seschrock zhp->zpool_name); 20741544Seschrock 20751544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20762082Seschrock if (path) { 20775450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 20787326SEric.Schrock@Sun.COM &l2cache, NULL)) == 0) 20792082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20802082Seschrock 20815450Sbrendan /* 20825450Sbrendan * Don't allow error clearing for hot spares. Do allow 20835450Sbrendan * error clearing for l2cache devices. 20845450Sbrendan */ 20852468Sek110237 if (avail_spare) 20862082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 20872082Seschrock 20882082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 20892082Seschrock &zc.zc_guid) == 0); 20901544Seschrock } 20911544Seschrock 20924543Smarks if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 20931544Seschrock return (0); 20941544Seschrock 20952082Seschrock return (zpool_standard_error(hdl, errno, msg)); 2096789Sahrens } 2097789Sahrens 20983126Sahl /* 20994451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 21004451Seschrock */ 21014451Seschrock int 21024451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 21034451Seschrock { 21044451Seschrock zfs_cmd_t zc = { 0 }; 21054451Seschrock char msg[1024]; 21064451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21074451Seschrock 21084451Seschrock (void) snprintf(msg, sizeof (msg), 21094451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 21104451Seschrock guid); 21114451Seschrock 21124451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21134451Seschrock zc.zc_guid = guid; 21144451Seschrock 21154451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 21164451Seschrock return (0); 21174451Seschrock 21184451Seschrock return (zpool_standard_error(hdl, errno, msg)); 21194451Seschrock } 21204451Seschrock 21214451Seschrock /* 21223126Sahl * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 21233126Sahl * hierarchy. 21243126Sahl */ 21253126Sahl int 21263126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 21273126Sahl void *data) 2128789Sahrens { 21293126Sahl libzfs_handle_t *hdl = zhp->zpool_hdl; 21303126Sahl char (*paths)[MAXPATHLEN]; 21313126Sahl size_t size = 4; 21323126Sahl int curr, fd, base, ret = 0; 21333126Sahl DIR *dirp; 21343126Sahl struct dirent *dp; 21353126Sahl struct stat st; 21363126Sahl 21373126Sahl if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 21383126Sahl return (errno == ENOENT ? 0 : -1); 21393126Sahl 21403126Sahl if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 21413126Sahl int err = errno; 21423126Sahl (void) close(base); 21433126Sahl return (err == ENOENT ? 0 : -1); 21443126Sahl } 2145789Sahrens 2146789Sahrens /* 21473126Sahl * Oddly this wasn't a directory -- ignore that failure since we 21483126Sahl * know there are no links lower in the (non-existant) hierarchy. 2149789Sahrens */ 21503126Sahl if (!S_ISDIR(st.st_mode)) { 21513126Sahl (void) close(base); 21523126Sahl return (0); 21533126Sahl } 21543126Sahl 21553126Sahl if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 21563126Sahl (void) close(base); 21573126Sahl return (-1); 2158789Sahrens } 2159789Sahrens 21603126Sahl (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 21613126Sahl curr = 0; 21623126Sahl 21633126Sahl while (curr >= 0) { 21643126Sahl if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 21653126Sahl goto err; 21663126Sahl 21673126Sahl if (S_ISDIR(st.st_mode)) { 21683126Sahl if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 21693126Sahl goto err; 21703126Sahl 21713126Sahl if ((dirp = fdopendir(fd)) == NULL) { 21723126Sahl (void) close(fd); 21733126Sahl goto err; 21743126Sahl } 21753126Sahl 21763126Sahl while ((dp = readdir(dirp)) != NULL) { 21773126Sahl if (dp->d_name[0] == '.') 21783126Sahl continue; 21793126Sahl 21803126Sahl if (curr + 1 == size) { 21813126Sahl paths = zfs_realloc(hdl, paths, 21823126Sahl size * sizeof (paths[0]), 21833126Sahl size * 2 * sizeof (paths[0])); 21843126Sahl if (paths == NULL) { 21853126Sahl (void) closedir(dirp); 21863126Sahl (void) close(fd); 21873126Sahl goto err; 21883126Sahl } 21893126Sahl 21903126Sahl size *= 2; 21913126Sahl } 21923126Sahl 21933126Sahl (void) strlcpy(paths[curr + 1], paths[curr], 21943126Sahl sizeof (paths[curr + 1])); 21953126Sahl (void) strlcat(paths[curr], "/", 21963126Sahl sizeof (paths[curr])); 21973126Sahl (void) strlcat(paths[curr], dp->d_name, 21983126Sahl sizeof (paths[curr])); 21993126Sahl curr++; 22003126Sahl } 22013126Sahl 22023126Sahl (void) closedir(dirp); 22033126Sahl 22043126Sahl } else { 22053126Sahl if ((ret = cb(paths[curr], data)) != 0) 22063126Sahl break; 22073126Sahl } 22083126Sahl 22093126Sahl curr--; 22103126Sahl } 22113126Sahl 22123126Sahl free(paths); 22133126Sahl (void) close(base); 22143126Sahl 22153126Sahl return (ret); 22163126Sahl 22173126Sahl err: 22183126Sahl free(paths); 22193126Sahl (void) close(base); 22203126Sahl return (-1); 22213126Sahl } 22223126Sahl 22233126Sahl typedef struct zvol_cb { 22243126Sahl zpool_handle_t *zcb_pool; 22253126Sahl boolean_t zcb_create; 22263126Sahl } zvol_cb_t; 22273126Sahl 22283126Sahl /*ARGSUSED*/ 22293126Sahl static int 22303126Sahl do_zvol_create(zfs_handle_t *zhp, void *data) 22313126Sahl { 22324657Sahrens int ret = 0; 22333126Sahl 22344657Sahrens if (ZFS_IS_VOLUME(zhp)) { 22353126Sahl (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 22364657Sahrens ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 22374657Sahrens } 22383126Sahl 22394657Sahrens if (ret == 0) 22404657Sahrens ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 2241789Sahrens 2242789Sahrens zfs_close(zhp); 22433126Sahl 2244789Sahrens return (ret); 2245789Sahrens } 2246789Sahrens 2247789Sahrens /* 2248789Sahrens * Iterate over all zvols in the pool and make any necessary minor nodes. 2249789Sahrens */ 2250789Sahrens int 2251789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp) 2252789Sahrens { 2253789Sahrens zfs_handle_t *zfp; 2254789Sahrens int ret; 2255789Sahrens 2256789Sahrens /* 2257789Sahrens * If the pool is unavailable, just return success. 2258789Sahrens */ 22592082Seschrock if ((zfp = make_dataset_handle(zhp->zpool_hdl, 22602082Seschrock zhp->zpool_name)) == NULL) 2261789Sahrens return (0); 2262789Sahrens 22634657Sahrens ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 2264789Sahrens 2265789Sahrens zfs_close(zfp); 2266789Sahrens return (ret); 2267789Sahrens } 2268789Sahrens 22693126Sahl static int 22703126Sahl do_zvol_remove(const char *dataset, void *data) 22713126Sahl { 22723126Sahl zpool_handle_t *zhp = data; 22733126Sahl 22743126Sahl return (zvol_remove_link(zhp->zpool_hdl, dataset)); 22753126Sahl } 22763126Sahl 2277789Sahrens /* 22783126Sahl * Iterate over all zvols in the pool and remove any minor nodes. We iterate 22793126Sahl * by examining the /dev links so that a corrupted pool doesn't impede this 22803126Sahl * operation. 2281789Sahrens */ 2282789Sahrens int 2283789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp) 2284789Sahrens { 22853126Sahl return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 2286789Sahrens } 22871354Seschrock 22881354Seschrock /* 22891354Seschrock * Convert from a devid string to a path. 22901354Seschrock */ 22911354Seschrock static char * 22921354Seschrock devid_to_path(char *devid_str) 22931354Seschrock { 22941354Seschrock ddi_devid_t devid; 22951354Seschrock char *minor; 22961354Seschrock char *path; 22971354Seschrock devid_nmlist_t *list = NULL; 22981354Seschrock int ret; 22991354Seschrock 23001354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 23011354Seschrock return (NULL); 23021354Seschrock 23031354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 23041354Seschrock 23051354Seschrock devid_str_free(minor); 23061354Seschrock devid_free(devid); 23071354Seschrock 23081354Seschrock if (ret != 0) 23091354Seschrock return (NULL); 23101354Seschrock 23112082Seschrock if ((path = strdup(list[0].devname)) == NULL) 23122082Seschrock return (NULL); 23132082Seschrock 23141354Seschrock devid_free_nmlist(list); 23151354Seschrock 23161354Seschrock return (path); 23171354Seschrock } 23181354Seschrock 23191354Seschrock /* 23201354Seschrock * Convert from a path to a devid string. 23211354Seschrock */ 23221354Seschrock static char * 23231354Seschrock path_to_devid(const char *path) 23241354Seschrock { 23251354Seschrock int fd; 23261354Seschrock ddi_devid_t devid; 23271354Seschrock char *minor, *ret; 23281354Seschrock 23291354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 23301354Seschrock return (NULL); 23311354Seschrock 23321354Seschrock minor = NULL; 23331354Seschrock ret = NULL; 23341354Seschrock if (devid_get(fd, &devid) == 0) { 23351354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 23361354Seschrock ret = devid_str_encode(devid, minor); 23371354Seschrock if (minor != NULL) 23381354Seschrock devid_str_free(minor); 23391354Seschrock devid_free(devid); 23401354Seschrock } 23411354Seschrock (void) close(fd); 23421354Seschrock 23431354Seschrock return (ret); 23441354Seschrock } 23451354Seschrock 23461354Seschrock /* 23471354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 23481354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 23491354Seschrock * type 'zpool status', and we'll display the correct information anyway. 23501354Seschrock */ 23511354Seschrock static void 23521354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 23531354Seschrock { 23541354Seschrock zfs_cmd_t zc = { 0 }; 23551354Seschrock 23561354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 23572676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 23581354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 23591544Seschrock &zc.zc_guid) == 0); 23601354Seschrock 23612082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 23621354Seschrock } 23631354Seschrock 23641354Seschrock /* 23651354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 23661354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 23671354Seschrock * We also check if this is a whole disk, in which case we strip off the 23681354Seschrock * trailing 's0' slice name. 23691354Seschrock * 23701354Seschrock * This routine is also responsible for identifying when disks have been 23711354Seschrock * reconfigured in a new location. The kernel will have opened the device by 23721354Seschrock * devid, but the path will still refer to the old location. To catch this, we 23731354Seschrock * first do a path -> devid translation (which is fast for the common case). If 23741354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 23751354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 23761354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 23771354Seschrock * of these checks. 23781354Seschrock */ 23791354Seschrock char * 23802082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 23811354Seschrock { 23821354Seschrock char *path, *devid; 23831544Seschrock uint64_t value; 23841544Seschrock char buf[64]; 23854451Seschrock vdev_stat_t *vs; 23864451Seschrock uint_t vsc; 23871354Seschrock 23881544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 23891544Seschrock &value) == 0) { 23901544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 23911544Seschrock &value) == 0); 23922856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 23932856Snd150628 (u_longlong_t)value); 23941544Seschrock path = buf; 23951544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 23961354Seschrock 23974451Seschrock /* 23984451Seschrock * If the device is dead (faulted, offline, etc) then don't 23994451Seschrock * bother opening it. Otherwise we may be forcing the user to 24004451Seschrock * open a misbehaving device, which can have undesirable 24014451Seschrock * effects. 24024451Seschrock */ 24034451Seschrock if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 24044451Seschrock (uint64_t **)&vs, &vsc) != 0 || 24054451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 24064451Seschrock zhp != NULL && 24071354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 24081354Seschrock /* 24091354Seschrock * Determine if the current path is correct. 24101354Seschrock */ 24111354Seschrock char *newdevid = path_to_devid(path); 24121354Seschrock 24131354Seschrock if (newdevid == NULL || 24141354Seschrock strcmp(devid, newdevid) != 0) { 24151354Seschrock char *newpath; 24161354Seschrock 24171354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 24181354Seschrock /* 24191354Seschrock * Update the path appropriately. 24201354Seschrock */ 24211354Seschrock set_path(zhp, nv, newpath); 24222082Seschrock if (nvlist_add_string(nv, 24232082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 24242082Seschrock verify(nvlist_lookup_string(nv, 24252082Seschrock ZPOOL_CONFIG_PATH, 24262082Seschrock &path) == 0); 24271354Seschrock free(newpath); 24281354Seschrock } 24291354Seschrock } 24301354Seschrock 24312082Seschrock if (newdevid) 24322082Seschrock devid_str_free(newdevid); 24331354Seschrock } 24341354Seschrock 24351354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 24361354Seschrock path += 9; 24371354Seschrock 24381354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 24391544Seschrock &value) == 0 && value) { 24402082Seschrock char *tmp = zfs_strdup(hdl, path); 24412082Seschrock if (tmp == NULL) 24422082Seschrock return (NULL); 24431354Seschrock tmp[strlen(path) - 2] = '\0'; 24441354Seschrock return (tmp); 24451354Seschrock } 24461354Seschrock } else { 24471354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 24482082Seschrock 24492082Seschrock /* 24502082Seschrock * If it's a raidz device, we need to stick in the parity level. 24512082Seschrock */ 24522082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 24532082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 24542082Seschrock &value) == 0); 24552082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 24562856Snd150628 (u_longlong_t)value); 24572082Seschrock path = buf; 24582082Seschrock } 24591354Seschrock } 24601354Seschrock 24612082Seschrock return (zfs_strdup(hdl, path)); 24621354Seschrock } 24631544Seschrock 24641544Seschrock static int 24651544Seschrock zbookmark_compare(const void *a, const void *b) 24661544Seschrock { 24671544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 24681544Seschrock } 24691544Seschrock 24701544Seschrock /* 24711544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 24721544Seschrock * caller. 24731544Seschrock */ 24741544Seschrock int 24753444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 24761544Seschrock { 24771544Seschrock zfs_cmd_t zc = { 0 }; 24781544Seschrock uint64_t count; 24792676Seschrock zbookmark_t *zb = NULL; 24803444Sek110237 int i; 24811544Seschrock 24821544Seschrock /* 24831544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 24841544Seschrock * has increased, allocate more space and continue until we get the 24851544Seschrock * entire list. 24861544Seschrock */ 24871544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 24881544Seschrock &count) == 0); 24894820Sek110237 if (count == 0) 24904820Sek110237 return (0); 24912676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 24922856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 24932082Seschrock return (-1); 24942676Seschrock zc.zc_nvlist_dst_size = count; 24951544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 24961544Seschrock for (;;) { 24972082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 24982082Seschrock &zc) != 0) { 24992676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 25001544Seschrock if (errno == ENOMEM) { 25013823Svb160487 count = zc.zc_nvlist_dst_size; 25022676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 25033823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 25043823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 25052082Seschrock return (-1); 25061544Seschrock } else { 25071544Seschrock return (-1); 25081544Seschrock } 25091544Seschrock } else { 25101544Seschrock break; 25111544Seschrock } 25121544Seschrock } 25131544Seschrock 25141544Seschrock /* 25151544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 25161544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 25172676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 25181544Seschrock * _not_ copied as part of the process. So we point the start of our 25191544Seschrock * array appropriate and decrement the total number of elements. 25201544Seschrock */ 25212676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 25222676Seschrock zc.zc_nvlist_dst_size; 25232676Seschrock count -= zc.zc_nvlist_dst_size; 25241544Seschrock 25251544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 25261544Seschrock 25273444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 25281544Seschrock 25291544Seschrock /* 25303444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 25311544Seschrock */ 25321544Seschrock for (i = 0; i < count; i++) { 25331544Seschrock nvlist_t *nv; 25341544Seschrock 25353700Sek110237 /* ignoring zb_blkid and zb_level for now */ 25363700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 25373700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 25381544Seschrock continue; 25391544Seschrock 25403444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 25413444Sek110237 goto nomem; 25423444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 25433444Sek110237 zb[i].zb_objset) != 0) { 25443444Sek110237 nvlist_free(nv); 25452082Seschrock goto nomem; 25463444Sek110237 } 25473444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 25483444Sek110237 zb[i].zb_object) != 0) { 25493444Sek110237 nvlist_free(nv); 25503444Sek110237 goto nomem; 25511544Seschrock } 25523444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 25533444Sek110237 nvlist_free(nv); 25543444Sek110237 goto nomem; 25553444Sek110237 } 25563444Sek110237 nvlist_free(nv); 25571544Seschrock } 25581544Seschrock 25593265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 25601544Seschrock return (0); 25612082Seschrock 25622082Seschrock nomem: 25632676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 25642082Seschrock return (no_memory(zhp->zpool_hdl)); 25651544Seschrock } 25661760Seschrock 25671760Seschrock /* 25681760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 25691760Seschrock */ 25701760Seschrock int 25715094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 25721760Seschrock { 25731760Seschrock zfs_cmd_t zc = { 0 }; 25742082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25751760Seschrock 25761760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 25775094Slling zc.zc_cookie = new_version; 25785094Slling 25794543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 25803237Slling return (zpool_standard_error_fmt(hdl, errno, 25812082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 25822082Seschrock zhp->zpool_name)); 25831760Seschrock return (0); 25841760Seschrock } 25852926Sek110237 25864988Sek110237 void 25874988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 25884988Sek110237 char *history_str) 25894988Sek110237 { 25904988Sek110237 int i; 25914988Sek110237 25924988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 25934988Sek110237 for (i = 1; i < argc; i++) { 25944988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 25954988Sek110237 HIS_MAX_RECORD_LEN) 25964988Sek110237 break; 25974988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 25984988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 25994988Sek110237 } 26004988Sek110237 } 26014988Sek110237 26022926Sek110237 /* 26034988Sek110237 * Stage command history for logging. 26042926Sek110237 */ 26054988Sek110237 int 26064988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 26072926Sek110237 { 26084988Sek110237 if (history_str == NULL) 26094988Sek110237 return (EINVAL); 26104988Sek110237 26114988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 26124988Sek110237 return (EINVAL); 26132926Sek110237 26144715Sek110237 if (hdl->libzfs_log_str != NULL) 26154543Smarks free(hdl->libzfs_log_str); 26162926Sek110237 26174988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 26184988Sek110237 return (no_memory(hdl)); 26194543Smarks 26204988Sek110237 return (0); 26212926Sek110237 } 26222926Sek110237 26232926Sek110237 /* 26242926Sek110237 * Perform ioctl to get some command history of a pool. 26252926Sek110237 * 26262926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 26272926Sek110237 * logical offset of the history buffer to start reading from. 26282926Sek110237 * 26292926Sek110237 * Upon return, 'off' is the next logical offset to read from and 26302926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 26312926Sek110237 */ 26322926Sek110237 static int 26332926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 26342926Sek110237 { 26352926Sek110237 zfs_cmd_t zc = { 0 }; 26362926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 26372926Sek110237 26382926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 26392926Sek110237 26402926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 26412926Sek110237 zc.zc_history_len = *len; 26422926Sek110237 zc.zc_history_offset = *off; 26432926Sek110237 26442926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 26452926Sek110237 switch (errno) { 26462926Sek110237 case EPERM: 26473237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 26483237Slling dgettext(TEXT_DOMAIN, 26492926Sek110237 "cannot show history for pool '%s'"), 26502926Sek110237 zhp->zpool_name)); 26512926Sek110237 case ENOENT: 26523237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 26532926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 26542926Sek110237 "'%s'"), zhp->zpool_name)); 26553863Sek110237 case ENOTSUP: 26563863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 26573863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 26583863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 26592926Sek110237 default: 26603237Slling return (zpool_standard_error_fmt(hdl, errno, 26612926Sek110237 dgettext(TEXT_DOMAIN, 26622926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 26632926Sek110237 } 26642926Sek110237 } 26652926Sek110237 26662926Sek110237 *len = zc.zc_history_len; 26672926Sek110237 *off = zc.zc_history_offset; 26682926Sek110237 26692926Sek110237 return (0); 26702926Sek110237 } 26712926Sek110237 26722926Sek110237 /* 26732926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 26742926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 26752926Sek110237 * processed as there wasn't a complete record. 26762926Sek110237 */ 26772926Sek110237 static int 26782926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 26792926Sek110237 nvlist_t ***records, uint_t *numrecords) 26802926Sek110237 { 26812926Sek110237 uint64_t reclen; 26822926Sek110237 nvlist_t *nv; 26832926Sek110237 int i; 26842926Sek110237 26852926Sek110237 while (bytes_read > sizeof (reclen)) { 26862926Sek110237 26872926Sek110237 /* get length of packed record (stored as little endian) */ 26882926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 26892926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 26902926Sek110237 26912926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 26922926Sek110237 break; 26932926Sek110237 26942926Sek110237 /* unpack record */ 26952926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 26962926Sek110237 return (ENOMEM); 26972926Sek110237 bytes_read -= sizeof (reclen) + reclen; 26982926Sek110237 buf += sizeof (reclen) + reclen; 26992926Sek110237 27002926Sek110237 /* add record to nvlist array */ 27012926Sek110237 (*numrecords)++; 27022926Sek110237 if (ISP2(*numrecords + 1)) { 27032926Sek110237 *records = realloc(*records, 27042926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 27052926Sek110237 } 27062926Sek110237 (*records)[*numrecords - 1] = nv; 27072926Sek110237 } 27082926Sek110237 27092926Sek110237 *leftover = bytes_read; 27102926Sek110237 return (0); 27112926Sek110237 } 27122926Sek110237 27132926Sek110237 #define HIS_BUF_LEN (128*1024) 27142926Sek110237 27152926Sek110237 /* 27162926Sek110237 * Retrieve the command history of a pool. 27172926Sek110237 */ 27182926Sek110237 int 27192926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 27202926Sek110237 { 27212926Sek110237 char buf[HIS_BUF_LEN]; 27222926Sek110237 uint64_t off = 0; 27232926Sek110237 nvlist_t **records = NULL; 27242926Sek110237 uint_t numrecords = 0; 27252926Sek110237 int err, i; 27262926Sek110237 27272926Sek110237 do { 27282926Sek110237 uint64_t bytes_read = sizeof (buf); 27292926Sek110237 uint64_t leftover; 27302926Sek110237 27312926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 27322926Sek110237 break; 27332926Sek110237 27342926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 27352926Sek110237 if (!bytes_read) 27362926Sek110237 break; 27372926Sek110237 27382926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 27392926Sek110237 &leftover, &records, &numrecords)) != 0) 27402926Sek110237 break; 27412926Sek110237 off -= leftover; 27422926Sek110237 27432926Sek110237 /* CONSTCOND */ 27442926Sek110237 } while (1); 27452926Sek110237 27462926Sek110237 if (!err) { 27472926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 27482926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 27492926Sek110237 records, numrecords) == 0); 27502926Sek110237 } 27512926Sek110237 for (i = 0; i < numrecords; i++) 27522926Sek110237 nvlist_free(records[i]); 27532926Sek110237 free(records); 27542926Sek110237 27552926Sek110237 return (err); 27562926Sek110237 } 27573444Sek110237 27583444Sek110237 void 27593444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 27603444Sek110237 char *pathname, size_t len) 27613444Sek110237 { 27623444Sek110237 zfs_cmd_t zc = { 0 }; 27633444Sek110237 boolean_t mounted = B_FALSE; 27643444Sek110237 char *mntpnt = NULL; 27653444Sek110237 char dsname[MAXNAMELEN]; 27663444Sek110237 27673444Sek110237 if (dsobj == 0) { 27683444Sek110237 /* special case for the MOS */ 27693444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 27703444Sek110237 return; 27713444Sek110237 } 27723444Sek110237 27733444Sek110237 /* get the dataset's name */ 27743444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 27753444Sek110237 zc.zc_obj = dsobj; 27763444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 27773444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 27783444Sek110237 /* just write out a path of two object numbers */ 27793444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 27803444Sek110237 dsobj, obj); 27813444Sek110237 return; 27823444Sek110237 } 27833444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 27843444Sek110237 27853444Sek110237 /* find out if the dataset is mounted */ 27863444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 27873444Sek110237 27883444Sek110237 /* get the corrupted object's path */ 27893444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 27903444Sek110237 zc.zc_obj = obj; 27913444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 27923444Sek110237 &zc) == 0) { 27933444Sek110237 if (mounted) { 27943444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 27953444Sek110237 zc.zc_value); 27963444Sek110237 } else { 27973444Sek110237 (void) snprintf(pathname, len, "%s:%s", 27983444Sek110237 dsname, zc.zc_value); 27993444Sek110237 } 28003444Sek110237 } else { 28013444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 28023444Sek110237 } 28033444Sek110237 free(mntpnt); 28043444Sek110237 } 28053912Slling 28064276Staylor #define RDISK_ROOT "/dev/rdsk" 28074276Staylor #define BACKUP_SLICE "s2" 28084276Staylor /* 28094276Staylor * Don't start the slice at the default block of 34; many storage 28104276Staylor * devices will use a stripe width of 128k, so start there instead. 28114276Staylor */ 28124276Staylor #define NEW_START_BLOCK 256 28134276Staylor 28144276Staylor /* 28157042Sgw25295 * Read the EFI label from the config, if a label does not exist then 28167042Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 28177042Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 28187042Sgw25295 * partition. 28197042Sgw25295 */ 28207042Sgw25295 static int 28217042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 28227042Sgw25295 { 28237042Sgw25295 char *path; 28247042Sgw25295 int fd; 28257042Sgw25295 char diskname[MAXPATHLEN]; 28267042Sgw25295 int err = -1; 28277042Sgw25295 28287042Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 28297042Sgw25295 return (err); 28307042Sgw25295 28317042Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 28327042Sgw25295 strrchr(path, '/')); 28337042Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 28347042Sgw25295 struct dk_gpt *vtoc; 28357042Sgw25295 28367042Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 28377042Sgw25295 if (sb != NULL) 28387042Sgw25295 *sb = vtoc->efi_parts[0].p_start; 28397042Sgw25295 efi_free(vtoc); 28407042Sgw25295 } 28417042Sgw25295 (void) close(fd); 28427042Sgw25295 } 28437042Sgw25295 return (err); 28447042Sgw25295 } 28457042Sgw25295 28467042Sgw25295 /* 28474276Staylor * determine where a partition starts on a disk in the current 28484276Staylor * configuration 28494276Staylor */ 28504276Staylor static diskaddr_t 28514276Staylor find_start_block(nvlist_t *config) 28524276Staylor { 28534276Staylor nvlist_t **child; 28544276Staylor uint_t c, children; 28554276Staylor diskaddr_t sb = MAXOFFSET_T; 28564276Staylor uint64_t wholedisk; 28574276Staylor 28584276Staylor if (nvlist_lookup_nvlist_array(config, 28594276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 28604276Staylor if (nvlist_lookup_uint64(config, 28614276Staylor ZPOOL_CONFIG_WHOLE_DISK, 28624276Staylor &wholedisk) != 0 || !wholedisk) { 28634276Staylor return (MAXOFFSET_T); 28644276Staylor } 28657042Sgw25295 if (read_efi_label(config, &sb) < 0) 28667042Sgw25295 sb = MAXOFFSET_T; 28674276Staylor return (sb); 28684276Staylor } 28694276Staylor 28704276Staylor for (c = 0; c < children; c++) { 28714276Staylor sb = find_start_block(child[c]); 28724276Staylor if (sb != MAXOFFSET_T) { 28734276Staylor return (sb); 28744276Staylor } 28754276Staylor } 28764276Staylor return (MAXOFFSET_T); 28774276Staylor } 28784276Staylor 28794276Staylor /* 28804276Staylor * Label an individual disk. The name provided is the short name, 28814276Staylor * stripped of any leading /dev path. 28824276Staylor */ 28834276Staylor int 28844276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 28854276Staylor { 28864276Staylor char path[MAXPATHLEN]; 28874276Staylor struct dk_gpt *vtoc; 28884276Staylor int fd; 28894276Staylor size_t resv = EFI_MIN_RESV_SIZE; 28904276Staylor uint64_t slice_size; 28914276Staylor diskaddr_t start_block; 28924276Staylor char errbuf[1024]; 28934276Staylor 28946289Smmusante /* prepare an error message just in case */ 28956289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 28966289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 28976289Smmusante 28984276Staylor if (zhp) { 28994276Staylor nvlist_t *nvroot; 29004276Staylor 29017965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp)) { 29027965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29037965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root " 29047965SGeorge.Wilson@Sun.COM "pools.")); 29057965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 29067965SGeorge.Wilson@Sun.COM } 29077965SGeorge.Wilson@Sun.COM 29084276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 29094276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 29104276Staylor 29114276Staylor if (zhp->zpool_start_block == 0) 29124276Staylor start_block = find_start_block(nvroot); 29134276Staylor else 29144276Staylor start_block = zhp->zpool_start_block; 29154276Staylor zhp->zpool_start_block = start_block; 29164276Staylor } else { 29174276Staylor /* new pool */ 29184276Staylor start_block = NEW_START_BLOCK; 29194276Staylor } 29204276Staylor 29214276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 29224276Staylor BACKUP_SLICE); 29234276Staylor 29244276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 29254276Staylor /* 29264276Staylor * This shouldn't happen. We've long since verified that this 29274276Staylor * is a valid device. 29284276Staylor */ 29296289Smmusante zfs_error_aux(hdl, 29306289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 29314276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 29324276Staylor } 29334276Staylor 29344276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 29354276Staylor /* 29364276Staylor * The only way this can fail is if we run out of memory, or we 29374276Staylor * were unable to read the disk's capacity 29384276Staylor */ 29394276Staylor if (errno == ENOMEM) 29404276Staylor (void) no_memory(hdl); 29414276Staylor 29424276Staylor (void) close(fd); 29436289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29446289Smmusante "unable to read disk capacity"), name); 29454276Staylor 29464276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 29474276Staylor } 29484276Staylor 29494276Staylor slice_size = vtoc->efi_last_u_lba + 1; 29504276Staylor slice_size -= EFI_MIN_RESV_SIZE; 29514276Staylor if (start_block == MAXOFFSET_T) 29524276Staylor start_block = NEW_START_BLOCK; 29534276Staylor slice_size -= start_block; 29544276Staylor 29554276Staylor vtoc->efi_parts[0].p_start = start_block; 29564276Staylor vtoc->efi_parts[0].p_size = slice_size; 29574276Staylor 29584276Staylor /* 29594276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 29604276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 29614276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 29624276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 29634276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 29644276Staylor * can get, in the absence of V_OTHER. 29654276Staylor */ 29664276Staylor vtoc->efi_parts[0].p_tag = V_USR; 29674276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 29684276Staylor 29694276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 29704276Staylor vtoc->efi_parts[8].p_size = resv; 29714276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 29724276Staylor 29734276Staylor if (efi_write(fd, vtoc) != 0) { 29744276Staylor /* 29754276Staylor * Some block drivers (like pcata) may not support EFI 29764276Staylor * GPT labels. Print out a helpful error message dir- 29774276Staylor * ecting the user to manually label the disk and give 29784276Staylor * a specific slice. 29794276Staylor */ 29804276Staylor (void) close(fd); 29814276Staylor efi_free(vtoc); 29824276Staylor 29834276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29846289Smmusante "try using fdisk(1M) and then provide a specific slice")); 29854276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 29864276Staylor } 29874276Staylor 29884276Staylor (void) close(fd); 29894276Staylor efi_free(vtoc); 29904276Staylor return (0); 29914276Staylor } 29926423Sgw25295 29936423Sgw25295 static boolean_t 29946423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 29956423Sgw25295 { 29966423Sgw25295 char *type; 29976423Sgw25295 nvlist_t **child; 29986423Sgw25295 uint_t children, c; 29996423Sgw25295 30006423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 30016423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 30026423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 30036423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 30046423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 30056423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30066423Sgw25295 "vdev type '%s' is not supported"), type); 30076423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 30086423Sgw25295 return (B_FALSE); 30096423Sgw25295 } 30106423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 30116423Sgw25295 &child, &children) == 0) { 30126423Sgw25295 for (c = 0; c < children; c++) { 30136423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 30146423Sgw25295 return (B_FALSE); 30156423Sgw25295 } 30166423Sgw25295 } 30176423Sgw25295 return (B_TRUE); 30186423Sgw25295 } 30196423Sgw25295 30206423Sgw25295 /* 30216423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 30226423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 30236423Sgw25295 */ 30246423Sgw25295 int 30256423Sgw25295 zvol_check_dump_config(char *arg) 30266423Sgw25295 { 30276423Sgw25295 zpool_handle_t *zhp = NULL; 30286423Sgw25295 nvlist_t *config, *nvroot; 30296423Sgw25295 char *p, *volname; 30306423Sgw25295 nvlist_t **top; 30316423Sgw25295 uint_t toplevels; 30326423Sgw25295 libzfs_handle_t *hdl; 30336423Sgw25295 char errbuf[1024]; 30346423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 30356423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 30366423Sgw25295 int ret = 1; 30376423Sgw25295 30386423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 30396423Sgw25295 return (-1); 30406423Sgw25295 } 30416423Sgw25295 30426423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30436423Sgw25295 "dump is not supported on device '%s'"), arg); 30446423Sgw25295 30456423Sgw25295 if ((hdl = libzfs_init()) == NULL) 30466423Sgw25295 return (1); 30476423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 30486423Sgw25295 30496423Sgw25295 volname = arg + pathlen; 30506423Sgw25295 30516423Sgw25295 /* check the configuration of the pool */ 30526423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 30536423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30546423Sgw25295 "malformed dataset name")); 30556423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 30566423Sgw25295 return (1); 30576423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 30586423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30596423Sgw25295 "dataset name is too long")); 30606423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 30616423Sgw25295 return (1); 30626423Sgw25295 } else { 30636423Sgw25295 (void) strncpy(poolname, volname, p - volname); 30646423Sgw25295 poolname[p - volname] = '\0'; 30656423Sgw25295 } 30666423Sgw25295 30676423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 30686423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30696423Sgw25295 "could not open pool '%s'"), poolname); 30706423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 30716423Sgw25295 goto out; 30726423Sgw25295 } 30736423Sgw25295 config = zpool_get_config(zhp, NULL); 30746423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 30756423Sgw25295 &nvroot) != 0) { 30766423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30776423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 30786423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 30796423Sgw25295 goto out; 30806423Sgw25295 } 30816423Sgw25295 30826423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 30836423Sgw25295 &top, &toplevels) == 0); 30846423Sgw25295 if (toplevels != 1) { 30856423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30866423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 30876423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 30886423Sgw25295 goto out; 30896423Sgw25295 } 30906423Sgw25295 30916423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 30926423Sgw25295 goto out; 30936423Sgw25295 } 30946423Sgw25295 ret = 0; 30956423Sgw25295 30966423Sgw25295 out: 30976423Sgw25295 if (zhp) 30986423Sgw25295 zpool_close(zhp); 30996423Sgw25295 libzfs_fini(hdl); 31006423Sgw25295 return (ret); 31016423Sgw25295 } 3102