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 /* 236289Smmusante * Copyright 2008 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 525094Slling /* 535094Slling * ==================================================================== 545094Slling * zpool property functions 555094Slling * ==================================================================== 565094Slling */ 575094Slling 585094Slling static int 595094Slling zpool_get_all_props(zpool_handle_t *zhp) 605094Slling { 615094Slling zfs_cmd_t zc = { 0 }; 625094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 635094Slling 645094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 655094Slling 665094Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 675094Slling return (-1); 685094Slling 695094Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 705094Slling if (errno == ENOMEM) { 715094Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 725094Slling zcmd_free_nvlists(&zc); 735094Slling return (-1); 745094Slling } 755094Slling } else { 765094Slling zcmd_free_nvlists(&zc); 775094Slling return (-1); 785094Slling } 795094Slling } 805094Slling 815094Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 825094Slling zcmd_free_nvlists(&zc); 835094Slling return (-1); 845094Slling } 855094Slling 865094Slling zcmd_free_nvlists(&zc); 875094Slling 885094Slling return (0); 895094Slling } 905094Slling 915094Slling static int 925094Slling zpool_props_refresh(zpool_handle_t *zhp) 935094Slling { 945094Slling nvlist_t *old_props; 955094Slling 965094Slling old_props = zhp->zpool_props; 975094Slling 985094Slling if (zpool_get_all_props(zhp) != 0) 995094Slling return (-1); 1005094Slling 1015094Slling nvlist_free(old_props); 1025094Slling return (0); 1035094Slling } 1045094Slling 1055094Slling static char * 1065094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 1075094Slling zprop_source_t *src) 1085094Slling { 1095094Slling nvlist_t *nv, *nvl; 1105094Slling uint64_t ival; 1115094Slling char *value; 1125094Slling zprop_source_t source; 1135094Slling 1145094Slling nvl = zhp->zpool_props; 1155094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1165094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 1175094Slling source = ival; 1185094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1195094Slling } else { 1205094Slling source = ZPROP_SRC_DEFAULT; 1215094Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 1225094Slling value = "-"; 1235094Slling } 1245094Slling 1255094Slling if (src) 1265094Slling *src = source; 1275094Slling 1285094Slling return (value); 1295094Slling } 1305094Slling 1315094Slling uint64_t 1325094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 1335094Slling { 1345094Slling nvlist_t *nv, *nvl; 1355094Slling uint64_t value; 1365094Slling zprop_source_t source; 1375094Slling 1387294Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 1397294Sperrin /* 1407294Sperrin * zpool_get_all_props() has most likely failed because 1417294Sperrin * the pool is faulted, but if all we need is the top level 1427294Sperrin * vdev's guid then get it from the zhp config nvlist. 1437294Sperrin */ 1447294Sperrin if ((prop == ZPOOL_PROP_GUID) && 1457294Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 1467294Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 1477294Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 1487294Sperrin == 0)) { 1497294Sperrin return (value); 1507294Sperrin } 1515094Slling return (zpool_prop_default_numeric(prop)); 1527294Sperrin } 1535094Slling 1545094Slling nvl = zhp->zpool_props; 1555094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1565094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 1575094Slling source = value; 1585094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1595094Slling } else { 1605094Slling source = ZPROP_SRC_DEFAULT; 1615094Slling value = zpool_prop_default_numeric(prop); 1625094Slling } 1635094Slling 1645094Slling if (src) 1655094Slling *src = source; 1665094Slling 1675094Slling return (value); 1685094Slling } 1695094Slling 1705094Slling /* 1715094Slling * Map VDEV STATE to printed strings. 1725094Slling */ 1735094Slling char * 1745094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 1755094Slling { 1765094Slling switch (state) { 1775094Slling case VDEV_STATE_CLOSED: 1785094Slling case VDEV_STATE_OFFLINE: 1795094Slling return (gettext("OFFLINE")); 1805094Slling case VDEV_STATE_REMOVED: 1815094Slling return (gettext("REMOVED")); 1825094Slling case VDEV_STATE_CANT_OPEN: 1837294Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 1845094Slling return (gettext("FAULTED")); 1855094Slling else 1865094Slling return (gettext("UNAVAIL")); 1875094Slling case VDEV_STATE_FAULTED: 1885094Slling return (gettext("FAULTED")); 1895094Slling case VDEV_STATE_DEGRADED: 1905094Slling return (gettext("DEGRADED")); 1915094Slling case VDEV_STATE_HEALTHY: 1925094Slling return (gettext("ONLINE")); 1935094Slling } 1945094Slling 1955094Slling return (gettext("UNKNOWN")); 1965094Slling } 1975094Slling 1985094Slling /* 1995094Slling * Get a zpool property value for 'prop' and return the value in 2005094Slling * a pre-allocated buffer. 2015094Slling */ 2025094Slling int 2035094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 2045094Slling zprop_source_t *srctype) 2055094Slling { 2065094Slling uint64_t intval; 2075094Slling const char *strval; 2085094Slling zprop_source_t src = ZPROP_SRC_NONE; 2095094Slling nvlist_t *nvroot; 2105094Slling vdev_stat_t *vs; 2115094Slling uint_t vsc; 2125094Slling 2135094Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 2145094Slling if (prop == ZPOOL_PROP_NAME) 2155094Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 2165094Slling else if (prop == ZPOOL_PROP_HEALTH) 2175094Slling (void) strlcpy(buf, "FAULTED", len); 2185094Slling else 2195094Slling (void) strlcpy(buf, "-", len); 2205094Slling return (0); 2215094Slling } 2225094Slling 2235094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 2245094Slling prop != ZPOOL_PROP_NAME) 2255094Slling return (-1); 2265094Slling 2275094Slling switch (zpool_prop_get_type(prop)) { 2285094Slling case PROP_TYPE_STRING: 2295094Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 2305094Slling len); 2315094Slling break; 2325094Slling 2335094Slling case PROP_TYPE_NUMBER: 2345094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2355094Slling 2365094Slling switch (prop) { 2375094Slling case ZPOOL_PROP_SIZE: 2385094Slling case ZPOOL_PROP_USED: 2395094Slling case ZPOOL_PROP_AVAILABLE: 2405094Slling (void) zfs_nicenum(intval, buf, len); 2415094Slling break; 2425094Slling 2435094Slling case ZPOOL_PROP_CAPACITY: 2445094Slling (void) snprintf(buf, len, "%llu%%", 2455094Slling (u_longlong_t)intval); 2465094Slling break; 2475094Slling 2485094Slling case ZPOOL_PROP_HEALTH: 2495094Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2505094Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2515094Slling verify(nvlist_lookup_uint64_array(nvroot, 2525094Slling ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 2535094Slling 2545094Slling (void) strlcpy(buf, zpool_state_to_name(intval, 2555094Slling vs->vs_aux), len); 2565094Slling break; 2575094Slling default: 2585094Slling (void) snprintf(buf, len, "%llu", intval); 2595094Slling } 2605094Slling break; 2615094Slling 2625094Slling case PROP_TYPE_INDEX: 2635094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2645094Slling if (zpool_prop_index_to_string(prop, intval, &strval) 2655094Slling != 0) 2665094Slling return (-1); 2675094Slling (void) strlcpy(buf, strval, len); 2685094Slling break; 2695094Slling 2705094Slling default: 2715094Slling abort(); 2725094Slling } 2735094Slling 2745094Slling if (srctype) 2755094Slling *srctype = src; 2765094Slling 2775094Slling return (0); 2785094Slling } 2795094Slling 2805094Slling /* 2815094Slling * Check if the bootfs name has the same pool name as it is set to. 2825094Slling * Assuming bootfs is a valid dataset name. 2835094Slling */ 2845094Slling static boolean_t 2855094Slling bootfs_name_valid(const char *pool, char *bootfs) 2865094Slling { 2875094Slling int len = strlen(pool); 2885094Slling 2897300SEric.Taylor@Sun.COM if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 2905094Slling return (B_FALSE); 2915094Slling 2925094Slling if (strncmp(pool, bootfs, len) == 0 && 2935094Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 2945094Slling return (B_TRUE); 2955094Slling 2965094Slling return (B_FALSE); 2975094Slling } 2985094Slling 2995094Slling /* 3007042Sgw25295 * Inspect the configuration to determine if any of the devices contain 3017042Sgw25295 * an EFI label. 3027042Sgw25295 */ 3037042Sgw25295 static boolean_t 3047042Sgw25295 pool_uses_efi(nvlist_t *config) 3057042Sgw25295 { 3067042Sgw25295 nvlist_t **child; 3077042Sgw25295 uint_t c, children; 3087042Sgw25295 3097042Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3107042Sgw25295 &child, &children) != 0) 3117042Sgw25295 return (read_efi_label(config, NULL) >= 0); 3127042Sgw25295 3137042Sgw25295 for (c = 0; c < children; c++) { 3147042Sgw25295 if (pool_uses_efi(child[c])) 3157042Sgw25295 return (B_TRUE); 3167042Sgw25295 } 3177042Sgw25295 return (B_FALSE); 3187042Sgw25295 } 3197042Sgw25295 3207042Sgw25295 /* 3215094Slling * Given an nvlist of zpool properties to be set, validate that they are 3225094Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 3235094Slling * specified as strings. 3245094Slling */ 3255094Slling static nvlist_t * 3267184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 3275094Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 3285094Slling { 3295094Slling nvpair_t *elem; 3305094Slling nvlist_t *retprops; 3315094Slling zpool_prop_t prop; 3325094Slling char *strval; 3335094Slling uint64_t intval; 3345363Seschrock char *slash; 3355363Seschrock struct stat64 statbuf; 3367042Sgw25295 zpool_handle_t *zhp; 3377042Sgw25295 nvlist_t *nvroot; 3385094Slling 3395094Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 3405094Slling (void) no_memory(hdl); 3415094Slling return (NULL); 3425094Slling } 3435094Slling 3445094Slling elem = NULL; 3455094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3465094Slling const char *propname = nvpair_name(elem); 3475094Slling 3485094Slling /* 3495094Slling * Make sure this property is valid and applies to this type. 3505094Slling */ 3515094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 3525094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3535094Slling "invalid property '%s'"), propname); 3545094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3555094Slling goto error; 3565094Slling } 3575094Slling 3585094Slling if (zpool_prop_readonly(prop)) { 3595094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 3605094Slling "is readonly"), propname); 3615094Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 3625094Slling goto error; 3635094Slling } 3645094Slling 3655094Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 3665094Slling &strval, &intval, errbuf) != 0) 3675094Slling goto error; 3685094Slling 3695094Slling /* 3705094Slling * Perform additional checking for specific properties. 3715094Slling */ 3725094Slling switch (prop) { 3735094Slling case ZPOOL_PROP_VERSION: 3745094Slling if (intval < version || intval > SPA_VERSION) { 3755094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3765094Slling "property '%s' number %d is invalid."), 3775094Slling propname, intval); 3785094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 3795094Slling goto error; 3805094Slling } 3815094Slling break; 3825094Slling 3835094Slling case ZPOOL_PROP_BOOTFS: 3845094Slling if (create_or_import) { 3855094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3865094Slling "property '%s' cannot be set at creation " 3875094Slling "or import time"), propname); 3885094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3895094Slling goto error; 3905094Slling } 3915094Slling 3925094Slling if (version < SPA_VERSION_BOOTFS) { 3935094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3945094Slling "pool must be upgraded to support " 3955094Slling "'%s' property"), propname); 3965094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 3975094Slling goto error; 3985094Slling } 3995094Slling 4005094Slling /* 4015094Slling * bootfs property value has to be a dataset name and 4025094Slling * the dataset has to be in the same pool as it sets to. 4035094Slling */ 4045094Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 4055094Slling strval)) { 4065094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4075094Slling "is an invalid name"), strval); 4085094Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4095094Slling goto error; 4105094Slling } 4117042Sgw25295 4127042Sgw25295 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 4137042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4147042Sgw25295 "could not open pool '%s'"), poolname); 4157042Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4167042Sgw25295 goto error; 4177042Sgw25295 } 4187042Sgw25295 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 4197042Sgw25295 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4207042Sgw25295 4217042Sgw25295 /* 4227042Sgw25295 * bootfs property cannot be set on a disk which has 4237042Sgw25295 * been EFI labeled. 4247042Sgw25295 */ 4257042Sgw25295 if (pool_uses_efi(nvroot)) { 4267042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4277042Sgw25295 "property '%s' not supported on " 4287042Sgw25295 "EFI labeled devices"), propname); 4297042Sgw25295 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 4307042Sgw25295 zpool_close(zhp); 4317042Sgw25295 goto error; 4327042Sgw25295 } 4337042Sgw25295 zpool_close(zhp); 4345094Slling break; 4355094Slling 4365094Slling case ZPOOL_PROP_ALTROOT: 4375094Slling if (!create_or_import) { 4385094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4395094Slling "property '%s' can only be set during pool " 4405094Slling "creation or import"), propname); 4415094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4425094Slling goto error; 4435094Slling } 4445094Slling 4455094Slling if (strval[0] != '/') { 4465094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4475094Slling "bad alternate root '%s'"), strval); 4485094Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4495094Slling goto error; 4505094Slling } 4515094Slling break; 4525363Seschrock 4535363Seschrock case ZPOOL_PROP_CACHEFILE: 4545363Seschrock if (strval[0] == '\0') 4555363Seschrock break; 4565363Seschrock 4575363Seschrock if (strcmp(strval, "none") == 0) 4585363Seschrock break; 4595363Seschrock 4605363Seschrock if (strval[0] != '/') { 4615363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4625363Seschrock "property '%s' must be empty, an " 4635363Seschrock "absolute path, or 'none'"), propname); 4645363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4655363Seschrock goto error; 4665363Seschrock } 4675363Seschrock 4685363Seschrock slash = strrchr(strval, '/'); 4695363Seschrock 4705363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4715363Seschrock strcmp(slash, "/..") == 0) { 4725363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4735363Seschrock "'%s' is not a valid file"), strval); 4745363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4755363Seschrock goto error; 4765363Seschrock } 4775363Seschrock 4785363Seschrock *slash = '\0'; 4795363Seschrock 4805621Seschrock if (strval[0] != '\0' && 4815621Seschrock (stat64(strval, &statbuf) != 0 || 4825621Seschrock !S_ISDIR(statbuf.st_mode))) { 4835363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4845363Seschrock "'%s' is not a valid directory"), 4855363Seschrock strval); 4865363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4875363Seschrock goto error; 4885363Seschrock } 4895363Seschrock 4905363Seschrock *slash = '/'; 4915363Seschrock break; 4925094Slling } 4935094Slling } 4945094Slling 4955094Slling return (retprops); 4965094Slling error: 4975094Slling nvlist_free(retprops); 4985094Slling return (NULL); 4995094Slling } 5005094Slling 5015094Slling /* 5025094Slling * Set zpool property : propname=propval. 5035094Slling */ 5045094Slling int 5055094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 5065094Slling { 5075094Slling zfs_cmd_t zc = { 0 }; 5085094Slling int ret = -1; 5095094Slling char errbuf[1024]; 5105094Slling nvlist_t *nvl = NULL; 5115094Slling nvlist_t *realprops; 5125094Slling uint64_t version; 5135094Slling 5145094Slling (void) snprintf(errbuf, sizeof (errbuf), 5155094Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 5165094Slling zhp->zpool_name); 5175094Slling 5185094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) 5195094Slling return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf)); 5205094Slling 5215094Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5225094Slling return (no_memory(zhp->zpool_hdl)); 5235094Slling 5245094Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 5255094Slling nvlist_free(nvl); 5265094Slling return (no_memory(zhp->zpool_hdl)); 5275094Slling } 5285094Slling 5295094Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5307184Stimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 5315094Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 5325094Slling nvlist_free(nvl); 5335094Slling return (-1); 5345094Slling } 5355094Slling 5365094Slling nvlist_free(nvl); 5375094Slling nvl = realprops; 5385094Slling 5395094Slling /* 5405094Slling * Execute the corresponding ioctl() to set this property. 5415094Slling */ 5425094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 5435094Slling 5445094Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 5455094Slling nvlist_free(nvl); 5465094Slling return (-1); 5475094Slling } 5485094Slling 5495094Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 5505094Slling 5515094Slling zcmd_free_nvlists(&zc); 5525094Slling nvlist_free(nvl); 5535094Slling 5545094Slling if (ret) 5555094Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 5565094Slling else 5575094Slling (void) zpool_props_refresh(zhp); 5585094Slling 5595094Slling return (ret); 5605094Slling } 5615094Slling 5625094Slling int 5635094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 5645094Slling { 5655094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 5665094Slling zprop_list_t *entry; 5675094Slling char buf[ZFS_MAXPROPLEN]; 5685094Slling 5695094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 5705094Slling return (-1); 5715094Slling 5725094Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 5735094Slling 5745094Slling if (entry->pl_fixed) 5755094Slling continue; 5765094Slling 5775094Slling if (entry->pl_prop != ZPROP_INVAL && 5785094Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 5795094Slling NULL) == 0) { 5805094Slling if (strlen(buf) > entry->pl_width) 5815094Slling entry->pl_width = strlen(buf); 5825094Slling } 5835094Slling } 5845094Slling 5855094Slling return (0); 5865094Slling } 5875094Slling 5885094Slling 589789Sahrens /* 590789Sahrens * Validate the given pool name, optionally putting an extended error message in 591789Sahrens * 'buf'. 592789Sahrens */ 5936423Sgw25295 boolean_t 5942082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 595789Sahrens { 596789Sahrens namecheck_err_t why; 597789Sahrens char what; 5981773Seschrock int ret; 599789Sahrens 6001773Seschrock ret = pool_namecheck(pool, &why, &what); 6011773Seschrock 6021773Seschrock /* 6031773Seschrock * The rules for reserved pool names were extended at a later point. 6041773Seschrock * But we need to support users with existing pools that may now be 6051773Seschrock * invalid. So we only check for this expanded set of names during a 6061773Seschrock * create (or import), and only in userland. 6071773Seschrock */ 6081773Seschrock if (ret == 0 && !isopen && 6091773Seschrock (strncmp(pool, "mirror", 6) == 0 || 6101773Seschrock strncmp(pool, "raidz", 5) == 0 || 6114527Sperrin strncmp(pool, "spare", 5) == 0 || 6124527Sperrin strcmp(pool, "log") == 0)) { 6136423Sgw25295 if (hdl != NULL) 6146423Sgw25295 zfs_error_aux(hdl, 6156423Sgw25295 dgettext(TEXT_DOMAIN, "name is reserved")); 6162082Seschrock return (B_FALSE); 6171773Seschrock } 6181773Seschrock 6191773Seschrock 6201773Seschrock if (ret != 0) { 6212082Seschrock if (hdl != NULL) { 622789Sahrens switch (why) { 6231003Slling case NAME_ERR_TOOLONG: 6242082Seschrock zfs_error_aux(hdl, 6251003Slling dgettext(TEXT_DOMAIN, "name is too long")); 6261003Slling break; 6271003Slling 628789Sahrens case NAME_ERR_INVALCHAR: 6292082Seschrock zfs_error_aux(hdl, 630789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 631789Sahrens "'%c' in pool name"), what); 632789Sahrens break; 633789Sahrens 634789Sahrens case NAME_ERR_NOLETTER: 6352082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6362082Seschrock "name must begin with a letter")); 637789Sahrens break; 638789Sahrens 639789Sahrens case NAME_ERR_RESERVED: 6402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6412082Seschrock "name is reserved")); 642789Sahrens break; 643789Sahrens 644789Sahrens case NAME_ERR_DISKLIKE: 6452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6462082Seschrock "pool name is reserved")); 647789Sahrens break; 6482856Snd150628 6492856Snd150628 case NAME_ERR_LEADING_SLASH: 6502856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6512856Snd150628 "leading slash in name")); 6522856Snd150628 break; 6532856Snd150628 6542856Snd150628 case NAME_ERR_EMPTY_COMPONENT: 6552856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6562856Snd150628 "empty component in name")); 6572856Snd150628 break; 6582856Snd150628 6592856Snd150628 case NAME_ERR_TRAILING_SLASH: 6602856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6612856Snd150628 "trailing slash in name")); 6622856Snd150628 break; 6632856Snd150628 6642856Snd150628 case NAME_ERR_MULTIPLE_AT: 6652856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6662856Snd150628 "multiple '@' delimiters in name")); 6672856Snd150628 break; 6682856Snd150628 669789Sahrens } 670789Sahrens } 6712082Seschrock return (B_FALSE); 672789Sahrens } 673789Sahrens 6742082Seschrock return (B_TRUE); 675789Sahrens } 676789Sahrens 677789Sahrens /* 678789Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 679789Sahrens * state. 680789Sahrens */ 681789Sahrens zpool_handle_t * 6822082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 683789Sahrens { 684789Sahrens zpool_handle_t *zhp; 6852142Seschrock boolean_t missing; 686789Sahrens 687789Sahrens /* 688789Sahrens * Make sure the pool name is valid. 689789Sahrens */ 6902082Seschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 6913237Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 6922082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 6932082Seschrock pool); 694789Sahrens return (NULL); 695789Sahrens } 696789Sahrens 6972082Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 6982082Seschrock return (NULL); 699789Sahrens 7002082Seschrock zhp->zpool_hdl = hdl; 701789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 702789Sahrens 7032142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7042142Seschrock zpool_close(zhp); 7052142Seschrock return (NULL); 7062142Seschrock } 7072142Seschrock 7082142Seschrock if (missing) { 7095094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 7103237Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 7115094Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 7122142Seschrock zpool_close(zhp); 7132142Seschrock return (NULL); 714789Sahrens } 715789Sahrens 716789Sahrens return (zhp); 717789Sahrens } 718789Sahrens 719789Sahrens /* 720789Sahrens * Like the above, but silent on error. Used when iterating over pools (because 721789Sahrens * the configuration cache may be out of date). 722789Sahrens */ 7232142Seschrock int 7242142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 725789Sahrens { 726789Sahrens zpool_handle_t *zhp; 7272142Seschrock boolean_t missing; 728789Sahrens 7292142Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7302142Seschrock return (-1); 731789Sahrens 7322082Seschrock zhp->zpool_hdl = hdl; 733789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 734789Sahrens 7352142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7362142Seschrock zpool_close(zhp); 7372142Seschrock return (-1); 738789Sahrens } 739789Sahrens 7402142Seschrock if (missing) { 7412142Seschrock zpool_close(zhp); 7422142Seschrock *ret = NULL; 7432142Seschrock return (0); 7442142Seschrock } 7452142Seschrock 7462142Seschrock *ret = zhp; 7472142Seschrock return (0); 748789Sahrens } 749789Sahrens 750789Sahrens /* 751789Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 752789Sahrens * state. 753789Sahrens */ 754789Sahrens zpool_handle_t * 7552082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 756789Sahrens { 757789Sahrens zpool_handle_t *zhp; 758789Sahrens 7592082Seschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 760789Sahrens return (NULL); 761789Sahrens 762789Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 7633237Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 7642082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 765789Sahrens zpool_close(zhp); 766789Sahrens return (NULL); 767789Sahrens } 768789Sahrens 769789Sahrens return (zhp); 770789Sahrens } 771789Sahrens 772789Sahrens /* 773789Sahrens * Close the handle. Simply frees the memory associated with the handle. 774789Sahrens */ 775789Sahrens void 776789Sahrens zpool_close(zpool_handle_t *zhp) 777789Sahrens { 778789Sahrens if (zhp->zpool_config) 779789Sahrens nvlist_free(zhp->zpool_config); 780952Seschrock if (zhp->zpool_old_config) 781952Seschrock nvlist_free(zhp->zpool_old_config); 7823912Slling if (zhp->zpool_props) 7833912Slling nvlist_free(zhp->zpool_props); 784789Sahrens free(zhp); 785789Sahrens } 786789Sahrens 787789Sahrens /* 788789Sahrens * Return the name of the pool. 789789Sahrens */ 790789Sahrens const char * 791789Sahrens zpool_get_name(zpool_handle_t *zhp) 792789Sahrens { 793789Sahrens return (zhp->zpool_name); 794789Sahrens } 795789Sahrens 796789Sahrens 797789Sahrens /* 798789Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 799789Sahrens */ 800789Sahrens int 801789Sahrens zpool_get_state(zpool_handle_t *zhp) 802789Sahrens { 803789Sahrens return (zhp->zpool_state); 804789Sahrens } 805789Sahrens 806789Sahrens /* 807789Sahrens * Create the named pool, using the provided vdev list. It is assumed 808789Sahrens * that the consumer has already validated the contents of the nvlist, so we 809789Sahrens * don't have to worry about error semantics. 810789Sahrens */ 811789Sahrens int 8122082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8137184Stimh nvlist_t *props, nvlist_t *fsprops) 814789Sahrens { 815789Sahrens zfs_cmd_t zc = { 0 }; 8167184Stimh nvlist_t *zc_fsprops = NULL; 8177184Stimh nvlist_t *zc_props = NULL; 8182082Seschrock char msg[1024]; 8195094Slling char *altroot; 8207184Stimh int ret = -1; 8212082Seschrock 8222082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 8232082Seschrock "cannot create '%s'"), pool); 824789Sahrens 8252082Seschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 8262082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 8272082Seschrock 8285320Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 8295320Slling return (-1); 8305320Slling 8317184Stimh if (props) { 8327184Stimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8337184Stimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8347184Stimh goto create_failed; 8357184Stimh } 8365320Slling } 837789Sahrens 8387184Stimh if (fsprops) { 8397184Stimh uint64_t zoned; 8407184Stimh char *zonestr; 8417184Stimh 8427184Stimh zoned = ((nvlist_lookup_string(fsprops, 8437184Stimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8447184Stimh strcmp(zonestr, "on") == 0); 8457184Stimh 8467184Stimh if ((zc_fsprops = zfs_valid_proplist(hdl, 8477184Stimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 8487184Stimh goto create_failed; 8497184Stimh } 8507184Stimh if (!zc_props && 8517184Stimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 8527184Stimh goto create_failed; 8537184Stimh } 8547184Stimh if (nvlist_add_nvlist(zc_props, 8557184Stimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 8567184Stimh goto create_failed; 8577184Stimh } 8587184Stimh } 8597184Stimh 8607184Stimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 8617184Stimh goto create_failed; 8627184Stimh 863789Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 864789Sahrens 8657184Stimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 8665320Slling 8672676Seschrock zcmd_free_nvlists(&zc); 8687184Stimh nvlist_free(zc_props); 8697184Stimh nvlist_free(zc_fsprops); 8702082Seschrock 871789Sahrens switch (errno) { 872789Sahrens case EBUSY: 873789Sahrens /* 874789Sahrens * This can happen if the user has specified the same 875789Sahrens * device multiple times. We can't reliably detect this 876789Sahrens * until we try to add it and see we already have a 877789Sahrens * label. 878789Sahrens */ 8792082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8802082Seschrock "one or more vdevs refer to the same device")); 8812082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 882789Sahrens 883789Sahrens case EOVERFLOW: 884789Sahrens /* 8852082Seschrock * This occurs when one of the devices is below 886789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 887789Sahrens * device was the problem device since there's no 888789Sahrens * reliable way to determine device size from userland. 889789Sahrens */ 890789Sahrens { 891789Sahrens char buf[64]; 892789Sahrens 893789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 894789Sahrens 8952082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8962082Seschrock "one or more devices is less than the " 8972082Seschrock "minimum size (%s)"), buf); 898789Sahrens } 8992082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 900789Sahrens 901789Sahrens case ENOSPC: 9022082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9032082Seschrock "one or more devices is out of space")); 9042082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 905789Sahrens 9065450Sbrendan case ENOTBLK: 9075450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9085450Sbrendan "cache device must be a disk or disk slice")); 9095450Sbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 9105450Sbrendan 911789Sahrens default: 9122082Seschrock return (zpool_standard_error(hdl, errno, msg)); 913789Sahrens } 914789Sahrens } 915789Sahrens 916789Sahrens /* 917789Sahrens * If this is an alternate root pool, then we automatically set the 9182676Seschrock * mountpoint of the root dataset to be '/'. 919789Sahrens */ 9205094Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 9215094Slling &altroot) == 0) { 922789Sahrens zfs_handle_t *zhp; 923789Sahrens 9245094Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 9252676Seschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 9262676Seschrock "/") == 0); 927789Sahrens 928789Sahrens zfs_close(zhp); 929789Sahrens } 930789Sahrens 9317184Stimh create_failed: 9325320Slling zcmd_free_nvlists(&zc); 9337184Stimh nvlist_free(zc_props); 9347184Stimh nvlist_free(zc_fsprops); 9357184Stimh return (ret); 936789Sahrens } 937789Sahrens 938789Sahrens /* 939789Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 940789Sahrens * datasets left in the pool. 941789Sahrens */ 942789Sahrens int 943789Sahrens zpool_destroy(zpool_handle_t *zhp) 944789Sahrens { 945789Sahrens zfs_cmd_t zc = { 0 }; 946789Sahrens zfs_handle_t *zfp = NULL; 9472082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9482082Seschrock char msg[1024]; 949789Sahrens 950789Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 9512082Seschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 9522082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 953789Sahrens return (-1); 954789Sahrens 9552856Snd150628 if (zpool_remove_zvol_links(zhp) != 0) 956789Sahrens return (-1); 957789Sahrens 958789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 959789Sahrens 9604543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 9612082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 9622082Seschrock "cannot destroy '%s'"), zhp->zpool_name); 963789Sahrens 9642082Seschrock if (errno == EROFS) { 9652082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9662082Seschrock "one or more devices is read only")); 9672082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 9682082Seschrock } else { 9692082Seschrock (void) zpool_standard_error(hdl, errno, msg); 970789Sahrens } 971789Sahrens 972789Sahrens if (zfp) 973789Sahrens zfs_close(zfp); 974789Sahrens return (-1); 975789Sahrens } 976789Sahrens 977789Sahrens if (zfp) { 978789Sahrens remove_mountpoint(zfp); 979789Sahrens zfs_close(zfp); 980789Sahrens } 981789Sahrens 982789Sahrens return (0); 983789Sahrens } 984789Sahrens 985789Sahrens /* 986789Sahrens * Add the given vdevs to the pool. The caller must have already performed the 987789Sahrens * necessary verification to ensure that the vdev specification is well-formed. 988789Sahrens */ 989789Sahrens int 990789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 991789Sahrens { 9922676Seschrock zfs_cmd_t zc = { 0 }; 9932082Seschrock int ret; 9942082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9952082Seschrock char msg[1024]; 9965450Sbrendan nvlist_t **spares, **l2cache; 9975450Sbrendan uint_t nspares, nl2cache; 9982082Seschrock 9992082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10002082Seschrock "cannot add to '%s'"), zhp->zpool_name); 10012082Seschrock 10025450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10035450Sbrendan SPA_VERSION_SPARES && 10042082Seschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10052082Seschrock &spares, &nspares) == 0) { 10062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10072082Seschrock "upgraded to add hot spares")); 10082082Seschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10092082Seschrock } 1010789Sahrens 10115450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10125450Sbrendan SPA_VERSION_L2CACHE && 10135450Sbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 10145450Sbrendan &l2cache, &nl2cache) == 0) { 10155450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10165450Sbrendan "upgraded to add cache devices")); 10175450Sbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10185450Sbrendan } 10195450Sbrendan 10205094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 10212082Seschrock return (-1); 1022789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1023789Sahrens 10244543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1025789Sahrens switch (errno) { 1026789Sahrens case EBUSY: 1027789Sahrens /* 1028789Sahrens * This can happen if the user has specified the same 1029789Sahrens * device multiple times. We can't reliably detect this 1030789Sahrens * until we try to add it and see we already have a 1031789Sahrens * label. 1032789Sahrens */ 10332082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10342082Seschrock "one or more vdevs refer to the same device")); 10352082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1036789Sahrens break; 1037789Sahrens 1038789Sahrens case EOVERFLOW: 1039789Sahrens /* 1040789Sahrens * This occurrs when one of the devices is below 1041789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1042789Sahrens * device was the problem device since there's no 1043789Sahrens * reliable way to determine device size from userland. 1044789Sahrens */ 1045789Sahrens { 1046789Sahrens char buf[64]; 1047789Sahrens 1048789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1049789Sahrens 10502082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10512082Seschrock "device is less than the minimum " 10522082Seschrock "size (%s)"), buf); 1053789Sahrens } 10542082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 10552082Seschrock break; 10562082Seschrock 10572082Seschrock case ENOTSUP: 10582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10594527Sperrin "pool must be upgraded to add these vdevs")); 10602082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1061789Sahrens break; 1062789Sahrens 10633912Slling case EDOM: 10643912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10654527Sperrin "root pool can not have multiple vdevs" 10664527Sperrin " or separate logs")); 10673912Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 10683912Slling break; 10693912Slling 10705450Sbrendan case ENOTBLK: 10715450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10725450Sbrendan "cache device must be a disk or disk slice")); 10735450Sbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 10745450Sbrendan break; 10755450Sbrendan 1076789Sahrens default: 10772082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1078789Sahrens } 1079789Sahrens 10802082Seschrock ret = -1; 10812082Seschrock } else { 10822082Seschrock ret = 0; 1083789Sahrens } 1084789Sahrens 10852676Seschrock zcmd_free_nvlists(&zc); 1086789Sahrens 10872082Seschrock return (ret); 1088789Sahrens } 1089789Sahrens 1090789Sahrens /* 1091789Sahrens * Exports the pool from the system. The caller must ensure that there are no 1092789Sahrens * mounted datasets in the pool. 1093789Sahrens */ 1094789Sahrens int 10957214Slling zpool_export(zpool_handle_t *zhp, boolean_t force) 1096789Sahrens { 1097789Sahrens zfs_cmd_t zc = { 0 }; 10987214Slling char msg[1024]; 1099789Sahrens 1100789Sahrens if (zpool_remove_zvol_links(zhp) != 0) 1101789Sahrens return (-1); 1102789Sahrens 11037214Slling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 11047214Slling "cannot export '%s'"), zhp->zpool_name); 11057214Slling 1106789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 11077214Slling zc.zc_cookie = force; 11087214Slling 11097214Slling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 11107214Slling switch (errno) { 11117214Slling case EXDEV: 11127214Slling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 11137214Slling "use '-f' to override the following errors:\n" 11147214Slling "'%s' has an active shared spare which could be" 11157214Slling " used by other pools once '%s' is exported."), 11167214Slling zhp->zpool_name, zhp->zpool_name); 11177214Slling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 11187214Slling msg)); 11197214Slling default: 11207214Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 11217214Slling msg)); 11227214Slling } 11237214Slling } 11247214Slling 1125789Sahrens return (0); 1126789Sahrens } 1127789Sahrens 1128789Sahrens /* 11295094Slling * zpool_import() is a contracted interface. Should be kept the same 11305094Slling * if possible. 11315094Slling * 11325094Slling * Applications should use zpool_import_props() to import a pool with 11335094Slling * new properties value to be set. 1134789Sahrens */ 1135789Sahrens int 11362082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 11375094Slling char *altroot) 11385094Slling { 11395094Slling nvlist_t *props = NULL; 11405094Slling int ret; 11415094Slling 11425094Slling if (altroot != NULL) { 11435094Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 11445094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 11455094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 11465094Slling newname)); 11475094Slling } 11485094Slling 11495094Slling if (nvlist_add_string(props, 11505094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) { 11515094Slling nvlist_free(props); 11525094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 11535094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 11545094Slling newname)); 11555094Slling } 11565094Slling } 11575094Slling 11586643Seschrock ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 11595094Slling if (props) 11605094Slling nvlist_free(props); 11615094Slling return (ret); 11625094Slling } 11635094Slling 11645094Slling /* 11655094Slling * Import the given pool using the known configuration and a list of 11665094Slling * properties to be set. The configuration should have come from 11675094Slling * zpool_find_import(). The 'newname' parameters control whether the pool 11685094Slling * is imported with a different name. 11695094Slling */ 11705094Slling int 11715094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 11726643Seschrock nvlist_t *props, boolean_t importfaulted) 1173789Sahrens { 11742676Seschrock zfs_cmd_t zc = { 0 }; 1175789Sahrens char *thename; 1176789Sahrens char *origname; 1177789Sahrens int ret; 11785094Slling char errbuf[1024]; 1179789Sahrens 1180789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1181789Sahrens &origname) == 0); 1182789Sahrens 11835094Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 11845094Slling "cannot import pool '%s'"), origname); 11855094Slling 1186789Sahrens if (newname != NULL) { 11872082Seschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 11883237Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 11892082Seschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 11902082Seschrock newname)); 1191789Sahrens thename = (char *)newname; 1192789Sahrens } else { 1193789Sahrens thename = origname; 1194789Sahrens } 1195789Sahrens 11965094Slling if (props) { 11975094Slling uint64_t version; 11985094Slling 11995094Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 12005094Slling &version) == 0); 12015094Slling 12027184Stimh if ((props = zpool_valid_proplist(hdl, origname, 12035320Slling props, version, B_TRUE, errbuf)) == NULL) { 12045094Slling return (-1); 12055320Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 12065320Slling nvlist_free(props); 12075094Slling return (-1); 12085320Slling } 12095094Slling } 1210789Sahrens 1211789Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1212789Sahrens 1213789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 12141544Seschrock &zc.zc_guid) == 0); 1215789Sahrens 12165320Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 12175320Slling nvlist_free(props); 12182082Seschrock return (-1); 12195320Slling } 1220789Sahrens 12216643Seschrock zc.zc_cookie = (uint64_t)importfaulted; 1222789Sahrens ret = 0; 12234543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1224789Sahrens char desc[1024]; 1225789Sahrens if (newname == NULL) 1226789Sahrens (void) snprintf(desc, sizeof (desc), 1227789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1228789Sahrens thename); 1229789Sahrens else 1230789Sahrens (void) snprintf(desc, sizeof (desc), 1231789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1232789Sahrens origname, thename); 1233789Sahrens 1234789Sahrens switch (errno) { 12351544Seschrock case ENOTSUP: 12361544Seschrock /* 12371544Seschrock * Unsupported version. 12381544Seschrock */ 12392082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 12401544Seschrock break; 12411544Seschrock 12422174Seschrock case EINVAL: 12432174Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 12442174Seschrock break; 12452174Seschrock 1246789Sahrens default: 12472082Seschrock (void) zpool_standard_error(hdl, errno, desc); 1248789Sahrens } 1249789Sahrens 1250789Sahrens ret = -1; 1251789Sahrens } else { 1252789Sahrens zpool_handle_t *zhp; 12534543Smarks 1254789Sahrens /* 1255789Sahrens * This should never fail, but play it safe anyway. 1256789Sahrens */ 12572142Seschrock if (zpool_open_silent(hdl, thename, &zhp) != 0) { 12582142Seschrock ret = -1; 12592142Seschrock } else if (zhp != NULL) { 1260789Sahrens ret = zpool_create_zvol_links(zhp); 1261789Sahrens zpool_close(zhp); 1262789Sahrens } 12634543Smarks 1264789Sahrens } 1265789Sahrens 12662676Seschrock zcmd_free_nvlists(&zc); 12675320Slling nvlist_free(props); 12685320Slling 1269789Sahrens return (ret); 1270789Sahrens } 1271789Sahrens 1272789Sahrens /* 1273789Sahrens * Scrub the pool. 1274789Sahrens */ 1275789Sahrens int 1276789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) 1277789Sahrens { 1278789Sahrens zfs_cmd_t zc = { 0 }; 1279789Sahrens char msg[1024]; 12802082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1281789Sahrens 1282789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1283789Sahrens zc.zc_cookie = type; 1284789Sahrens 12854543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0) 1286789Sahrens return (0); 1287789Sahrens 1288789Sahrens (void) snprintf(msg, sizeof (msg), 1289789Sahrens dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1290789Sahrens 12912082Seschrock if (errno == EBUSY) 12922082Seschrock return (zfs_error(hdl, EZFS_RESILVERING, msg)); 12932082Seschrock else 12942082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1295789Sahrens } 1296789Sahrens 12972468Sek110237 /* 12982468Sek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 12992468Sek110237 * spare; but FALSE if its an INUSE spare. 13002468Sek110237 */ 13012082Seschrock static nvlist_t * 13022082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, 13037326SEric.Schrock@Sun.COM boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 13041544Seschrock { 13051544Seschrock uint_t c, children; 13061544Seschrock nvlist_t **child; 13072082Seschrock uint64_t theguid, present; 13081544Seschrock char *path; 13091544Seschrock uint64_t wholedisk = 0; 13102082Seschrock nvlist_t *ret; 13117326SEric.Schrock@Sun.COM uint64_t is_log; 13121544Seschrock 13132082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); 13141544Seschrock 13151544Seschrock if (search == NULL && 13161544Seschrock nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { 13171544Seschrock /* 13181544Seschrock * If the device has never been present since import, the only 13191544Seschrock * reliable way to match the vdev is by GUID. 13201544Seschrock */ 13212082Seschrock if (theguid == guid) 13222082Seschrock return (nv); 13231544Seschrock } else if (search != NULL && 13241544Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 13251544Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 13261544Seschrock &wholedisk); 13271544Seschrock if (wholedisk) { 13281544Seschrock /* 13291544Seschrock * For whole disks, the internal path has 's0', but the 13301544Seschrock * path passed in by the user doesn't. 13311544Seschrock */ 13321544Seschrock if (strlen(search) == strlen(path) - 2 && 13331544Seschrock strncmp(search, path, strlen(search)) == 0) 13342082Seschrock return (nv); 13351544Seschrock } else if (strcmp(search, path) == 0) { 13362082Seschrock return (nv); 13371544Seschrock } 13381544Seschrock } 13391544Seschrock 13401544Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13411544Seschrock &child, &children) != 0) 13422082Seschrock return (NULL); 13431544Seschrock 13447326SEric.Schrock@Sun.COM for (c = 0; c < children; c++) { 13452082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 13467326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 13477326SEric.Schrock@Sun.COM /* 13487326SEric.Schrock@Sun.COM * The 'is_log' value is only set for the toplevel 13497326SEric.Schrock@Sun.COM * vdev, not the leaf vdevs. So we always lookup the 13507326SEric.Schrock@Sun.COM * log device from the root of the vdev tree (where 13517326SEric.Schrock@Sun.COM * 'log' is non-NULL). 13527326SEric.Schrock@Sun.COM */ 13537326SEric.Schrock@Sun.COM if (log != NULL && 13547326SEric.Schrock@Sun.COM nvlist_lookup_uint64(child[c], 13557326SEric.Schrock@Sun.COM ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 13567326SEric.Schrock@Sun.COM is_log) { 13577326SEric.Schrock@Sun.COM *log = B_TRUE; 13587326SEric.Schrock@Sun.COM } 13591544Seschrock return (ret); 13607326SEric.Schrock@Sun.COM } 13617326SEric.Schrock@Sun.COM } 13621544Seschrock 13632082Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 13642082Seschrock &child, &children) == 0) { 13652082Seschrock for (c = 0; c < children; c++) { 13662082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 13677326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 13682468Sek110237 *avail_spare = B_TRUE; 13692082Seschrock return (ret); 13702082Seschrock } 13712082Seschrock } 13722082Seschrock } 13732082Seschrock 13745450Sbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 13755450Sbrendan &child, &children) == 0) { 13765450Sbrendan for (c = 0; c < children; c++) { 13775450Sbrendan if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 13787326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 13795450Sbrendan *l2cache = B_TRUE; 13805450Sbrendan return (ret); 13815450Sbrendan } 13825450Sbrendan } 13835450Sbrendan } 13845450Sbrendan 13852082Seschrock return (NULL); 13861544Seschrock } 13871544Seschrock 13882082Seschrock nvlist_t * 13895450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 13907326SEric.Schrock@Sun.COM boolean_t *l2cache, boolean_t *log) 13911544Seschrock { 13921544Seschrock char buf[MAXPATHLEN]; 13931544Seschrock const char *search; 13941544Seschrock char *end; 13951544Seschrock nvlist_t *nvroot; 13961544Seschrock uint64_t guid; 13971544Seschrock 13981613Seschrock guid = strtoull(path, &end, 10); 13991544Seschrock if (guid != 0 && *end == '\0') { 14001544Seschrock search = NULL; 14011544Seschrock } else if (path[0] != '/') { 14021544Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 14031544Seschrock search = buf; 14041544Seschrock } else { 14051544Seschrock search = path; 14061544Seschrock } 14071544Seschrock 14081544Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 14091544Seschrock &nvroot) == 0); 14101544Seschrock 14112468Sek110237 *avail_spare = B_FALSE; 14125450Sbrendan *l2cache = B_FALSE; 14137326SEric.Schrock@Sun.COM if (log != NULL) 14147326SEric.Schrock@Sun.COM *log = B_FALSE; 14155450Sbrendan return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare, 14167326SEric.Schrock@Sun.COM l2cache, log)); 14172468Sek110237 } 14182468Sek110237 1419*7656SSherry.Moore@Sun.COM static int 1420*7656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv) 1421*7656SSherry.Moore@Sun.COM { 1422*7656SSherry.Moore@Sun.COM uint64_t ival; 1423*7656SSherry.Moore@Sun.COM 1424*7656SSherry.Moore@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 1425*7656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 1426*7656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 1427*7656SSherry.Moore@Sun.COM return (0); 1428*7656SSherry.Moore@Sun.COM 1429*7656SSherry.Moore@Sun.COM return (1); 1430*7656SSherry.Moore@Sun.COM } 1431*7656SSherry.Moore@Sun.COM 1432*7656SSherry.Moore@Sun.COM /* 1433*7656SSherry.Moore@Sun.COM * Get phys_path for a root pool 1434*7656SSherry.Moore@Sun.COM * Return 0 on success; non-zeron on failure. 1435*7656SSherry.Moore@Sun.COM */ 1436*7656SSherry.Moore@Sun.COM int 1437*7656SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath) 1438*7656SSherry.Moore@Sun.COM { 1439*7656SSherry.Moore@Sun.COM char bootfs[ZPOOL_MAXNAMELEN]; 1440*7656SSherry.Moore@Sun.COM nvlist_t *vdev_root; 1441*7656SSherry.Moore@Sun.COM nvlist_t **child; 1442*7656SSherry.Moore@Sun.COM uint_t count; 1443*7656SSherry.Moore@Sun.COM int i; 1444*7656SSherry.Moore@Sun.COM 1445*7656SSherry.Moore@Sun.COM /* 1446*7656SSherry.Moore@Sun.COM * Make sure this is a root pool, as phys_path doesn't mean 1447*7656SSherry.Moore@Sun.COM * anything to a non-root pool. 1448*7656SSherry.Moore@Sun.COM */ 1449*7656SSherry.Moore@Sun.COM if (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 1450*7656SSherry.Moore@Sun.COM sizeof (bootfs), NULL) != 0) 1451*7656SSherry.Moore@Sun.COM return (-1); 1452*7656SSherry.Moore@Sun.COM 1453*7656SSherry.Moore@Sun.COM verify(nvlist_lookup_nvlist(zhp->zpool_config, 1454*7656SSherry.Moore@Sun.COM ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0); 1455*7656SSherry.Moore@Sun.COM 1456*7656SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 1457*7656SSherry.Moore@Sun.COM &child, &count) != 0) 1458*7656SSherry.Moore@Sun.COM return (-2); 1459*7656SSherry.Moore@Sun.COM 1460*7656SSherry.Moore@Sun.COM for (i = 0; i < count; i++) { 1461*7656SSherry.Moore@Sun.COM nvlist_t **child2; 1462*7656SSherry.Moore@Sun.COM uint_t count2; 1463*7656SSherry.Moore@Sun.COM char *type; 1464*7656SSherry.Moore@Sun.COM char *tmppath; 1465*7656SSherry.Moore@Sun.COM int j; 1466*7656SSherry.Moore@Sun.COM 1467*7656SSherry.Moore@Sun.COM if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type) 1468*7656SSherry.Moore@Sun.COM != 0) 1469*7656SSherry.Moore@Sun.COM return (-3); 1470*7656SSherry.Moore@Sun.COM 1471*7656SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_DISK) == 0) { 1472*7656SSherry.Moore@Sun.COM if (!vdev_online(child[i])) 1473*7656SSherry.Moore@Sun.COM return (-8); 1474*7656SSherry.Moore@Sun.COM verify(nvlist_lookup_string(child[i], 1475*7656SSherry.Moore@Sun.COM ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0); 1476*7656SSherry.Moore@Sun.COM (void) strncpy(physpath, tmppath, strlen(tmppath)); 1477*7656SSherry.Moore@Sun.COM } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) { 1478*7656SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist_array(child[i], 1479*7656SSherry.Moore@Sun.COM ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0) 1480*7656SSherry.Moore@Sun.COM return (-4); 1481*7656SSherry.Moore@Sun.COM 1482*7656SSherry.Moore@Sun.COM for (j = 0; j < count2; j++) { 1483*7656SSherry.Moore@Sun.COM if (!vdev_online(child2[j])) 1484*7656SSherry.Moore@Sun.COM return (-8); 1485*7656SSherry.Moore@Sun.COM if (nvlist_lookup_string(child2[j], 1486*7656SSherry.Moore@Sun.COM ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0) 1487*7656SSherry.Moore@Sun.COM return (-5); 1488*7656SSherry.Moore@Sun.COM 1489*7656SSherry.Moore@Sun.COM if ((strlen(physpath) + strlen(tmppath)) > 1490*7656SSherry.Moore@Sun.COM MAXNAMELEN) 1491*7656SSherry.Moore@Sun.COM return (-6); 1492*7656SSherry.Moore@Sun.COM 1493*7656SSherry.Moore@Sun.COM if (strlen(physpath) == 0) { 1494*7656SSherry.Moore@Sun.COM (void) strncpy(physpath, tmppath, 1495*7656SSherry.Moore@Sun.COM strlen(tmppath)); 1496*7656SSherry.Moore@Sun.COM } else { 1497*7656SSherry.Moore@Sun.COM (void) strcat(physpath, " "); 1498*7656SSherry.Moore@Sun.COM (void) strcat(physpath, tmppath); 1499*7656SSherry.Moore@Sun.COM } 1500*7656SSherry.Moore@Sun.COM } 1501*7656SSherry.Moore@Sun.COM } else { 1502*7656SSherry.Moore@Sun.COM return (-7); 1503*7656SSherry.Moore@Sun.COM } 1504*7656SSherry.Moore@Sun.COM } 1505*7656SSherry.Moore@Sun.COM 1506*7656SSherry.Moore@Sun.COM return (0); 1507*7656SSherry.Moore@Sun.COM } 1508*7656SSherry.Moore@Sun.COM 15092468Sek110237 /* 15105450Sbrendan * Returns TRUE if the given guid corresponds to the given type. 15115450Sbrendan * This is used to check for hot spares (INUSE or not), and level 2 cache 15125450Sbrendan * devices. 15132468Sek110237 */ 15142468Sek110237 static boolean_t 15155450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 15162468Sek110237 { 15175450Sbrendan uint64_t target_guid; 15182468Sek110237 nvlist_t *nvroot; 15195450Sbrendan nvlist_t **list; 15205450Sbrendan uint_t count; 15212468Sek110237 int i; 15222468Sek110237 15232468Sek110237 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 15242468Sek110237 &nvroot) == 0); 15255450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 15265450Sbrendan for (i = 0; i < count; i++) { 15275450Sbrendan verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 15285450Sbrendan &target_guid) == 0); 15295450Sbrendan if (guid == target_guid) 15302468Sek110237 return (B_TRUE); 15312468Sek110237 } 15322468Sek110237 } 15332468Sek110237 15342468Sek110237 return (B_FALSE); 15351544Seschrock } 15361544Seschrock 1537789Sahrens /* 15384451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 15394451Seschrock * ZFS_ONLINE_* flags. 1540789Sahrens */ 1541789Sahrens int 15424451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 15434451Seschrock vdev_state_t *newstate) 1544789Sahrens { 1545789Sahrens zfs_cmd_t zc = { 0 }; 1546789Sahrens char msg[1024]; 15472082Seschrock nvlist_t *tgt; 15485450Sbrendan boolean_t avail_spare, l2cache; 15492082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1550789Sahrens 15511544Seschrock (void) snprintf(msg, sizeof (msg), 15521544Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1553789Sahrens 15541544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 15557326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 15567326SEric.Schrock@Sun.COM NULL)) == NULL) 15572082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1558789Sahrens 15592468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 15602468Sek110237 15615450Sbrendan if (avail_spare || 15625450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 15632082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 15642082Seschrock 15654451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 15664451Seschrock zc.zc_obj = flags; 15674451Seschrock 15684543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 15694451Seschrock return (zpool_standard_error(hdl, errno, msg)); 15704451Seschrock 15714451Seschrock *newstate = zc.zc_cookie; 15724451Seschrock return (0); 1573789Sahrens } 1574789Sahrens 1575789Sahrens /* 1576789Sahrens * Take the specified vdev offline 1577789Sahrens */ 1578789Sahrens int 15794451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1580789Sahrens { 1581789Sahrens zfs_cmd_t zc = { 0 }; 1582789Sahrens char msg[1024]; 15832082Seschrock nvlist_t *tgt; 15845450Sbrendan boolean_t avail_spare, l2cache; 15852082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1586789Sahrens 15871544Seschrock (void) snprintf(msg, sizeof (msg), 15881544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 15891544Seschrock 1590789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 15917326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 15927326SEric.Schrock@Sun.COM NULL)) == NULL) 15932082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 15942082Seschrock 15952468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 15962468Sek110237 15975450Sbrendan if (avail_spare || 15985450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 15992082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16002082Seschrock 16014451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 16024451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 16031485Slling 16044543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1605789Sahrens return (0); 1606789Sahrens 1607789Sahrens switch (errno) { 16082082Seschrock case EBUSY: 1609789Sahrens 1610789Sahrens /* 1611789Sahrens * There are no other replicas of this device. 1612789Sahrens */ 16132082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 16142082Seschrock 16152082Seschrock default: 16162082Seschrock return (zpool_standard_error(hdl, errno, msg)); 16172082Seschrock } 16182082Seschrock } 1619789Sahrens 16202082Seschrock /* 16214451Seschrock * Mark the given vdev faulted. 16224451Seschrock */ 16234451Seschrock int 16244451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 16254451Seschrock { 16264451Seschrock zfs_cmd_t zc = { 0 }; 16274451Seschrock char msg[1024]; 16284451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 16294451Seschrock 16304451Seschrock (void) snprintf(msg, sizeof (msg), 16314451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 16324451Seschrock 16334451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16344451Seschrock zc.zc_guid = guid; 16354451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 16364451Seschrock 16374451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 16384451Seschrock return (0); 16394451Seschrock 16404451Seschrock switch (errno) { 16414451Seschrock case EBUSY: 16424451Seschrock 16434451Seschrock /* 16444451Seschrock * There are no other replicas of this device. 16454451Seschrock */ 16464451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 16474451Seschrock 16484451Seschrock default: 16494451Seschrock return (zpool_standard_error(hdl, errno, msg)); 16504451Seschrock } 16514451Seschrock 16524451Seschrock } 16534451Seschrock 16544451Seschrock /* 16554451Seschrock * Mark the given vdev degraded. 16564451Seschrock */ 16574451Seschrock int 16584451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 16594451Seschrock { 16604451Seschrock zfs_cmd_t zc = { 0 }; 16614451Seschrock char msg[1024]; 16624451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 16634451Seschrock 16644451Seschrock (void) snprintf(msg, sizeof (msg), 16654451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 16664451Seschrock 16674451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16684451Seschrock zc.zc_guid = guid; 16694451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 16704451Seschrock 16714451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 16724451Seschrock return (0); 16734451Seschrock 16744451Seschrock return (zpool_standard_error(hdl, errno, msg)); 16754451Seschrock } 16764451Seschrock 16774451Seschrock /* 16782082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 16792082Seschrock * a hot spare. 16802082Seschrock */ 16812082Seschrock static boolean_t 16822082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 16832082Seschrock { 16842082Seschrock nvlist_t **child; 16852082Seschrock uint_t c, children; 16862082Seschrock char *type; 16872082Seschrock 16882082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 16892082Seschrock &children) == 0) { 16902082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 16912082Seschrock &type) == 0); 16922082Seschrock 16932082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 16942082Seschrock children == 2 && child[which] == tgt) 16952082Seschrock return (B_TRUE); 16962082Seschrock 16972082Seschrock for (c = 0; c < children; c++) 16982082Seschrock if (is_replacing_spare(child[c], tgt, which)) 16992082Seschrock return (B_TRUE); 1700789Sahrens } 17012082Seschrock 17022082Seschrock return (B_FALSE); 1703789Sahrens } 1704789Sahrens 1705789Sahrens /* 1706789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 17074527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 1708789Sahrens */ 1709789Sahrens int 1710789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 1711789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1712789Sahrens { 1713789Sahrens zfs_cmd_t zc = { 0 }; 1714789Sahrens char msg[1024]; 1715789Sahrens int ret; 17162082Seschrock nvlist_t *tgt; 17177326SEric.Schrock@Sun.COM boolean_t avail_spare, l2cache, islog; 17187326SEric.Schrock@Sun.COM uint64_t val; 17197041Seschrock char *path, *newname; 17202082Seschrock nvlist_t **child; 17212082Seschrock uint_t children; 17222082Seschrock nvlist_t *config_root; 17232082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1724789Sahrens 17251544Seschrock if (replacing) 17261544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 17271544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 17281544Seschrock else 17291544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 17301544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 17311544Seschrock 1732789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17337326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 17347326SEric.Schrock@Sun.COM &islog)) == 0) 17352082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 17362082Seschrock 17372468Sek110237 if (avail_spare) 17382082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 17392082Seschrock 17405450Sbrendan if (l2cache) 17415450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 17425450Sbrendan 17432082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 17442082Seschrock zc.zc_cookie = replacing; 17452082Seschrock 17462082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 17472082Seschrock &child, &children) != 0 || children != 1) { 17482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17492082Seschrock "new device must be a single disk")); 17502082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 17511544Seschrock } 17522082Seschrock 17532082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 17542082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 17552082Seschrock 17567041Seschrock if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL) 17577041Seschrock return (-1); 17587041Seschrock 17592082Seschrock /* 17602082Seschrock * If the target is a hot spare that has been swapped in, we can only 17612082Seschrock * replace it with another hot spare. 17622082Seschrock */ 17632082Seschrock if (replacing && 17642082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 17657326SEric.Schrock@Sun.COM (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 17667326SEric.Schrock@Sun.COM NULL) == NULL || !avail_spare) && 17677326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 1)) { 17682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17692082Seschrock "can only be replaced by another hot spare")); 17707041Seschrock free(newname); 17712082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 17722082Seschrock } 17732082Seschrock 17742082Seschrock /* 17752082Seschrock * If we are attempting to replace a spare, it canot be applied to an 17762082Seschrock * already spared device. 17772082Seschrock */ 17782082Seschrock if (replacing && 17792082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 17807326SEric.Schrock@Sun.COM zpool_find_vdev(zhp, newname, &avail_spare, 17817326SEric.Schrock@Sun.COM &l2cache, NULL) != NULL && avail_spare && 17827326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 0)) { 17832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17842082Seschrock "device has already been replaced with a spare")); 17857041Seschrock free(newname); 17862082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 17872082Seschrock } 1788789Sahrens 17897041Seschrock free(newname); 17907041Seschrock 17915094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 17922082Seschrock return (-1); 1793789Sahrens 17944543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1795789Sahrens 17962676Seschrock zcmd_free_nvlists(&zc); 1797789Sahrens 1798789Sahrens if (ret == 0) 1799789Sahrens return (0); 1800789Sahrens 1801789Sahrens switch (errno) { 18021544Seschrock case ENOTSUP: 1803789Sahrens /* 1804789Sahrens * Can't attach to or replace this type of vdev. 1805789Sahrens */ 18064527Sperrin if (replacing) { 18077326SEric.Schrock@Sun.COM if (islog) 18084527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18094527Sperrin "cannot replace a log with a spare")); 18104527Sperrin else 18114527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18124527Sperrin "cannot replace a replacing device")); 18134527Sperrin } else { 18142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18152082Seschrock "can only attach to mirrors and top-level " 18162082Seschrock "disks")); 18174527Sperrin } 18182082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1819789Sahrens break; 1820789Sahrens 18211544Seschrock case EINVAL: 1822789Sahrens /* 1823789Sahrens * The new device must be a single disk. 1824789Sahrens */ 18252082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18262082Seschrock "new device must be a single disk")); 18272082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1828789Sahrens break; 1829789Sahrens 18301544Seschrock case EBUSY: 18312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 18322082Seschrock new_disk); 18332082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1834789Sahrens break; 1835789Sahrens 18361544Seschrock case EOVERFLOW: 1837789Sahrens /* 1838789Sahrens * The new device is too small. 1839789Sahrens */ 18402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18412082Seschrock "device is too small")); 18422082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1843789Sahrens break; 1844789Sahrens 18451544Seschrock case EDOM: 1846789Sahrens /* 1847789Sahrens * The new device has a different alignment requirement. 1848789Sahrens */ 18492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18502082Seschrock "devices have different sector alignment")); 18512082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1852789Sahrens break; 1853789Sahrens 18541544Seschrock case ENAMETOOLONG: 1855789Sahrens /* 1856789Sahrens * The resulting top-level vdev spec won't fit in the label. 1857789Sahrens */ 18582082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 1859789Sahrens break; 1860789Sahrens 18611544Seschrock default: 18622082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1863789Sahrens } 1864789Sahrens 18652082Seschrock return (-1); 1866789Sahrens } 1867789Sahrens 1868789Sahrens /* 1869789Sahrens * Detach the specified device. 1870789Sahrens */ 1871789Sahrens int 1872789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 1873789Sahrens { 1874789Sahrens zfs_cmd_t zc = { 0 }; 1875789Sahrens char msg[1024]; 18762082Seschrock nvlist_t *tgt; 18775450Sbrendan boolean_t avail_spare, l2cache; 18782082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1879789Sahrens 18801544Seschrock (void) snprintf(msg, sizeof (msg), 18811544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 18821544Seschrock 1883789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18847326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 18857326SEric.Schrock@Sun.COM NULL)) == 0) 18862082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1887789Sahrens 18882468Sek110237 if (avail_spare) 18892082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 18902082Seschrock 18915450Sbrendan if (l2cache) 18925450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 18935450Sbrendan 18942082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 18952082Seschrock 18964543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 1897789Sahrens return (0); 1898789Sahrens 1899789Sahrens switch (errno) { 1900789Sahrens 19011544Seschrock case ENOTSUP: 1902789Sahrens /* 1903789Sahrens * Can't detach from this type of vdev. 1904789Sahrens */ 19052082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 19062082Seschrock "applicable to mirror and replacing vdevs")); 19072082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 1908789Sahrens break; 1909789Sahrens 19101544Seschrock case EBUSY: 1911789Sahrens /* 1912789Sahrens * There are no other replicas of this device. 1913789Sahrens */ 19142082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 1915789Sahrens break; 1916789Sahrens 19171544Seschrock default: 19182082Seschrock (void) zpool_standard_error(hdl, errno, msg); 19191544Seschrock } 19201544Seschrock 19212082Seschrock return (-1); 19222082Seschrock } 19232082Seschrock 19242082Seschrock /* 19255450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 19265450Sbrendan * and level 2 cache devices. 19272082Seschrock */ 19282082Seschrock int 19292082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 19302082Seschrock { 19312082Seschrock zfs_cmd_t zc = { 0 }; 19322082Seschrock char msg[1024]; 19332082Seschrock nvlist_t *tgt; 19345450Sbrendan boolean_t avail_spare, l2cache; 19352082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 19362082Seschrock 19372082Seschrock (void) snprintf(msg, sizeof (msg), 19382082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 19392082Seschrock 19402082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 19417326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 19427326SEric.Schrock@Sun.COM NULL)) == 0) 19432082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 19442082Seschrock 19455450Sbrendan if (!avail_spare && !l2cache) { 19462082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19475450Sbrendan "only inactive hot spares or cache devices " 19485450Sbrendan "can be removed")); 19492082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 19502082Seschrock } 19512082Seschrock 19522082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 19532082Seschrock 19544543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 19552082Seschrock return (0); 19562082Seschrock 19572082Seschrock return (zpool_standard_error(hdl, errno, msg)); 19581544Seschrock } 19591544Seschrock 19601544Seschrock /* 19611544Seschrock * Clear the errors for the pool, or the particular device if specified. 19621544Seschrock */ 19631544Seschrock int 19641544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path) 19651544Seschrock { 19661544Seschrock zfs_cmd_t zc = { 0 }; 19671544Seschrock char msg[1024]; 19682082Seschrock nvlist_t *tgt; 19695450Sbrendan boolean_t avail_spare, l2cache; 19702082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 19711544Seschrock 19721544Seschrock if (path) 19731544Seschrock (void) snprintf(msg, sizeof (msg), 19741544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 19752676Seschrock path); 19761544Seschrock else 19771544Seschrock (void) snprintf(msg, sizeof (msg), 19781544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 19791544Seschrock zhp->zpool_name); 19801544Seschrock 19811544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 19822082Seschrock if (path) { 19835450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 19847326SEric.Schrock@Sun.COM &l2cache, NULL)) == 0) 19852082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 19862082Seschrock 19875450Sbrendan /* 19885450Sbrendan * Don't allow error clearing for hot spares. Do allow 19895450Sbrendan * error clearing for l2cache devices. 19905450Sbrendan */ 19912468Sek110237 if (avail_spare) 19922082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 19932082Seschrock 19942082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 19952082Seschrock &zc.zc_guid) == 0); 19961544Seschrock } 19971544Seschrock 19984543Smarks if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 19991544Seschrock return (0); 20001544Seschrock 20012082Seschrock return (zpool_standard_error(hdl, errno, msg)); 2002789Sahrens } 2003789Sahrens 20043126Sahl /* 20054451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 20064451Seschrock */ 20074451Seschrock int 20084451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 20094451Seschrock { 20104451Seschrock zfs_cmd_t zc = { 0 }; 20114451Seschrock char msg[1024]; 20124451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 20134451Seschrock 20144451Seschrock (void) snprintf(msg, sizeof (msg), 20154451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 20164451Seschrock guid); 20174451Seschrock 20184451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20194451Seschrock zc.zc_guid = guid; 20204451Seschrock 20214451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 20224451Seschrock return (0); 20234451Seschrock 20244451Seschrock return (zpool_standard_error(hdl, errno, msg)); 20254451Seschrock } 20264451Seschrock 20274451Seschrock /* 20283126Sahl * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 20293126Sahl * hierarchy. 20303126Sahl */ 20313126Sahl int 20323126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 20333126Sahl void *data) 2034789Sahrens { 20353126Sahl libzfs_handle_t *hdl = zhp->zpool_hdl; 20363126Sahl char (*paths)[MAXPATHLEN]; 20373126Sahl size_t size = 4; 20383126Sahl int curr, fd, base, ret = 0; 20393126Sahl DIR *dirp; 20403126Sahl struct dirent *dp; 20413126Sahl struct stat st; 20423126Sahl 20433126Sahl if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 20443126Sahl return (errno == ENOENT ? 0 : -1); 20453126Sahl 20463126Sahl if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 20473126Sahl int err = errno; 20483126Sahl (void) close(base); 20493126Sahl return (err == ENOENT ? 0 : -1); 20503126Sahl } 2051789Sahrens 2052789Sahrens /* 20533126Sahl * Oddly this wasn't a directory -- ignore that failure since we 20543126Sahl * know there are no links lower in the (non-existant) hierarchy. 2055789Sahrens */ 20563126Sahl if (!S_ISDIR(st.st_mode)) { 20573126Sahl (void) close(base); 20583126Sahl return (0); 20593126Sahl } 20603126Sahl 20613126Sahl if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 20623126Sahl (void) close(base); 20633126Sahl return (-1); 2064789Sahrens } 2065789Sahrens 20663126Sahl (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 20673126Sahl curr = 0; 20683126Sahl 20693126Sahl while (curr >= 0) { 20703126Sahl if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 20713126Sahl goto err; 20723126Sahl 20733126Sahl if (S_ISDIR(st.st_mode)) { 20743126Sahl if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 20753126Sahl goto err; 20763126Sahl 20773126Sahl if ((dirp = fdopendir(fd)) == NULL) { 20783126Sahl (void) close(fd); 20793126Sahl goto err; 20803126Sahl } 20813126Sahl 20823126Sahl while ((dp = readdir(dirp)) != NULL) { 20833126Sahl if (dp->d_name[0] == '.') 20843126Sahl continue; 20853126Sahl 20863126Sahl if (curr + 1 == size) { 20873126Sahl paths = zfs_realloc(hdl, paths, 20883126Sahl size * sizeof (paths[0]), 20893126Sahl size * 2 * sizeof (paths[0])); 20903126Sahl if (paths == NULL) { 20913126Sahl (void) closedir(dirp); 20923126Sahl (void) close(fd); 20933126Sahl goto err; 20943126Sahl } 20953126Sahl 20963126Sahl size *= 2; 20973126Sahl } 20983126Sahl 20993126Sahl (void) strlcpy(paths[curr + 1], paths[curr], 21003126Sahl sizeof (paths[curr + 1])); 21013126Sahl (void) strlcat(paths[curr], "/", 21023126Sahl sizeof (paths[curr])); 21033126Sahl (void) strlcat(paths[curr], dp->d_name, 21043126Sahl sizeof (paths[curr])); 21053126Sahl curr++; 21063126Sahl } 21073126Sahl 21083126Sahl (void) closedir(dirp); 21093126Sahl 21103126Sahl } else { 21113126Sahl if ((ret = cb(paths[curr], data)) != 0) 21123126Sahl break; 21133126Sahl } 21143126Sahl 21153126Sahl curr--; 21163126Sahl } 21173126Sahl 21183126Sahl free(paths); 21193126Sahl (void) close(base); 21203126Sahl 21213126Sahl return (ret); 21223126Sahl 21233126Sahl err: 21243126Sahl free(paths); 21253126Sahl (void) close(base); 21263126Sahl return (-1); 21273126Sahl } 21283126Sahl 21293126Sahl typedef struct zvol_cb { 21303126Sahl zpool_handle_t *zcb_pool; 21313126Sahl boolean_t zcb_create; 21323126Sahl } zvol_cb_t; 21333126Sahl 21343126Sahl /*ARGSUSED*/ 21353126Sahl static int 21363126Sahl do_zvol_create(zfs_handle_t *zhp, void *data) 21373126Sahl { 21384657Sahrens int ret = 0; 21393126Sahl 21404657Sahrens if (ZFS_IS_VOLUME(zhp)) { 21413126Sahl (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 21424657Sahrens ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 21434657Sahrens } 21443126Sahl 21454657Sahrens if (ret == 0) 21464657Sahrens ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 2147789Sahrens 2148789Sahrens zfs_close(zhp); 21493126Sahl 2150789Sahrens return (ret); 2151789Sahrens } 2152789Sahrens 2153789Sahrens /* 2154789Sahrens * Iterate over all zvols in the pool and make any necessary minor nodes. 2155789Sahrens */ 2156789Sahrens int 2157789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp) 2158789Sahrens { 2159789Sahrens zfs_handle_t *zfp; 2160789Sahrens int ret; 2161789Sahrens 2162789Sahrens /* 2163789Sahrens * If the pool is unavailable, just return success. 2164789Sahrens */ 21652082Seschrock if ((zfp = make_dataset_handle(zhp->zpool_hdl, 21662082Seschrock zhp->zpool_name)) == NULL) 2167789Sahrens return (0); 2168789Sahrens 21694657Sahrens ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 2170789Sahrens 2171789Sahrens zfs_close(zfp); 2172789Sahrens return (ret); 2173789Sahrens } 2174789Sahrens 21753126Sahl static int 21763126Sahl do_zvol_remove(const char *dataset, void *data) 21773126Sahl { 21783126Sahl zpool_handle_t *zhp = data; 21793126Sahl 21803126Sahl return (zvol_remove_link(zhp->zpool_hdl, dataset)); 21813126Sahl } 21823126Sahl 2183789Sahrens /* 21843126Sahl * Iterate over all zvols in the pool and remove any minor nodes. We iterate 21853126Sahl * by examining the /dev links so that a corrupted pool doesn't impede this 21863126Sahl * operation. 2187789Sahrens */ 2188789Sahrens int 2189789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp) 2190789Sahrens { 21913126Sahl return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 2192789Sahrens } 21931354Seschrock 21941354Seschrock /* 21951354Seschrock * Convert from a devid string to a path. 21961354Seschrock */ 21971354Seschrock static char * 21981354Seschrock devid_to_path(char *devid_str) 21991354Seschrock { 22001354Seschrock ddi_devid_t devid; 22011354Seschrock char *minor; 22021354Seschrock char *path; 22031354Seschrock devid_nmlist_t *list = NULL; 22041354Seschrock int ret; 22051354Seschrock 22061354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 22071354Seschrock return (NULL); 22081354Seschrock 22091354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 22101354Seschrock 22111354Seschrock devid_str_free(minor); 22121354Seschrock devid_free(devid); 22131354Seschrock 22141354Seschrock if (ret != 0) 22151354Seschrock return (NULL); 22161354Seschrock 22172082Seschrock if ((path = strdup(list[0].devname)) == NULL) 22182082Seschrock return (NULL); 22192082Seschrock 22201354Seschrock devid_free_nmlist(list); 22211354Seschrock 22221354Seschrock return (path); 22231354Seschrock } 22241354Seschrock 22251354Seschrock /* 22261354Seschrock * Convert from a path to a devid string. 22271354Seschrock */ 22281354Seschrock static char * 22291354Seschrock path_to_devid(const char *path) 22301354Seschrock { 22311354Seschrock int fd; 22321354Seschrock ddi_devid_t devid; 22331354Seschrock char *minor, *ret; 22341354Seschrock 22351354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 22361354Seschrock return (NULL); 22371354Seschrock 22381354Seschrock minor = NULL; 22391354Seschrock ret = NULL; 22401354Seschrock if (devid_get(fd, &devid) == 0) { 22411354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 22421354Seschrock ret = devid_str_encode(devid, minor); 22431354Seschrock if (minor != NULL) 22441354Seschrock devid_str_free(minor); 22451354Seschrock devid_free(devid); 22461354Seschrock } 22471354Seschrock (void) close(fd); 22481354Seschrock 22491354Seschrock return (ret); 22501354Seschrock } 22511354Seschrock 22521354Seschrock /* 22531354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 22541354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 22551354Seschrock * type 'zpool status', and we'll display the correct information anyway. 22561354Seschrock */ 22571354Seschrock static void 22581354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 22591354Seschrock { 22601354Seschrock zfs_cmd_t zc = { 0 }; 22611354Seschrock 22621354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 22632676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 22641354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 22651544Seschrock &zc.zc_guid) == 0); 22661354Seschrock 22672082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 22681354Seschrock } 22691354Seschrock 22701354Seschrock /* 22711354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 22721354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 22731354Seschrock * We also check if this is a whole disk, in which case we strip off the 22741354Seschrock * trailing 's0' slice name. 22751354Seschrock * 22761354Seschrock * This routine is also responsible for identifying when disks have been 22771354Seschrock * reconfigured in a new location. The kernel will have opened the device by 22781354Seschrock * devid, but the path will still refer to the old location. To catch this, we 22791354Seschrock * first do a path -> devid translation (which is fast for the common case). If 22801354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 22811354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 22821354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 22831354Seschrock * of these checks. 22841354Seschrock */ 22851354Seschrock char * 22862082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 22871354Seschrock { 22881354Seschrock char *path, *devid; 22891544Seschrock uint64_t value; 22901544Seschrock char buf[64]; 22914451Seschrock vdev_stat_t *vs; 22924451Seschrock uint_t vsc; 22931354Seschrock 22941544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 22951544Seschrock &value) == 0) { 22961544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 22971544Seschrock &value) == 0); 22982856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 22992856Snd150628 (u_longlong_t)value); 23001544Seschrock path = buf; 23011544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 23021354Seschrock 23034451Seschrock /* 23044451Seschrock * If the device is dead (faulted, offline, etc) then don't 23054451Seschrock * bother opening it. Otherwise we may be forcing the user to 23064451Seschrock * open a misbehaving device, which can have undesirable 23074451Seschrock * effects. 23084451Seschrock */ 23094451Seschrock if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 23104451Seschrock (uint64_t **)&vs, &vsc) != 0 || 23114451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 23124451Seschrock zhp != NULL && 23131354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 23141354Seschrock /* 23151354Seschrock * Determine if the current path is correct. 23161354Seschrock */ 23171354Seschrock char *newdevid = path_to_devid(path); 23181354Seschrock 23191354Seschrock if (newdevid == NULL || 23201354Seschrock strcmp(devid, newdevid) != 0) { 23211354Seschrock char *newpath; 23221354Seschrock 23231354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 23241354Seschrock /* 23251354Seschrock * Update the path appropriately. 23261354Seschrock */ 23271354Seschrock set_path(zhp, nv, newpath); 23282082Seschrock if (nvlist_add_string(nv, 23292082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 23302082Seschrock verify(nvlist_lookup_string(nv, 23312082Seschrock ZPOOL_CONFIG_PATH, 23322082Seschrock &path) == 0); 23331354Seschrock free(newpath); 23341354Seschrock } 23351354Seschrock } 23361354Seschrock 23372082Seschrock if (newdevid) 23382082Seschrock devid_str_free(newdevid); 23391354Seschrock } 23401354Seschrock 23411354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 23421354Seschrock path += 9; 23431354Seschrock 23441354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 23451544Seschrock &value) == 0 && value) { 23462082Seschrock char *tmp = zfs_strdup(hdl, path); 23472082Seschrock if (tmp == NULL) 23482082Seschrock return (NULL); 23491354Seschrock tmp[strlen(path) - 2] = '\0'; 23501354Seschrock return (tmp); 23511354Seschrock } 23521354Seschrock } else { 23531354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 23542082Seschrock 23552082Seschrock /* 23562082Seschrock * If it's a raidz device, we need to stick in the parity level. 23572082Seschrock */ 23582082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 23592082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 23602082Seschrock &value) == 0); 23612082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 23622856Snd150628 (u_longlong_t)value); 23632082Seschrock path = buf; 23642082Seschrock } 23651354Seschrock } 23661354Seschrock 23672082Seschrock return (zfs_strdup(hdl, path)); 23681354Seschrock } 23691544Seschrock 23701544Seschrock static int 23711544Seschrock zbookmark_compare(const void *a, const void *b) 23721544Seschrock { 23731544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 23741544Seschrock } 23751544Seschrock 23761544Seschrock /* 23771544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 23781544Seschrock * caller. 23791544Seschrock */ 23801544Seschrock int 23813444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 23821544Seschrock { 23831544Seschrock zfs_cmd_t zc = { 0 }; 23841544Seschrock uint64_t count; 23852676Seschrock zbookmark_t *zb = NULL; 23863444Sek110237 int i; 23871544Seschrock 23881544Seschrock /* 23891544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 23901544Seschrock * has increased, allocate more space and continue until we get the 23911544Seschrock * entire list. 23921544Seschrock */ 23931544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 23941544Seschrock &count) == 0); 23954820Sek110237 if (count == 0) 23964820Sek110237 return (0); 23972676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 23982856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 23992082Seschrock return (-1); 24002676Seschrock zc.zc_nvlist_dst_size = count; 24011544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 24021544Seschrock for (;;) { 24032082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 24042082Seschrock &zc) != 0) { 24052676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 24061544Seschrock if (errno == ENOMEM) { 24073823Svb160487 count = zc.zc_nvlist_dst_size; 24082676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 24093823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 24103823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 24112082Seschrock return (-1); 24121544Seschrock } else { 24131544Seschrock return (-1); 24141544Seschrock } 24151544Seschrock } else { 24161544Seschrock break; 24171544Seschrock } 24181544Seschrock } 24191544Seschrock 24201544Seschrock /* 24211544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 24221544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 24232676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 24241544Seschrock * _not_ copied as part of the process. So we point the start of our 24251544Seschrock * array appropriate and decrement the total number of elements. 24261544Seschrock */ 24272676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 24282676Seschrock zc.zc_nvlist_dst_size; 24292676Seschrock count -= zc.zc_nvlist_dst_size; 24301544Seschrock 24311544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 24321544Seschrock 24333444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 24341544Seschrock 24351544Seschrock /* 24363444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 24371544Seschrock */ 24381544Seschrock for (i = 0; i < count; i++) { 24391544Seschrock nvlist_t *nv; 24401544Seschrock 24413700Sek110237 /* ignoring zb_blkid and zb_level for now */ 24423700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 24433700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 24441544Seschrock continue; 24451544Seschrock 24463444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 24473444Sek110237 goto nomem; 24483444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 24493444Sek110237 zb[i].zb_objset) != 0) { 24503444Sek110237 nvlist_free(nv); 24512082Seschrock goto nomem; 24523444Sek110237 } 24533444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 24543444Sek110237 zb[i].zb_object) != 0) { 24553444Sek110237 nvlist_free(nv); 24563444Sek110237 goto nomem; 24571544Seschrock } 24583444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 24593444Sek110237 nvlist_free(nv); 24603444Sek110237 goto nomem; 24613444Sek110237 } 24623444Sek110237 nvlist_free(nv); 24631544Seschrock } 24641544Seschrock 24653265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 24661544Seschrock return (0); 24672082Seschrock 24682082Seschrock nomem: 24692676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 24702082Seschrock return (no_memory(zhp->zpool_hdl)); 24711544Seschrock } 24721760Seschrock 24731760Seschrock /* 24741760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 24751760Seschrock */ 24761760Seschrock int 24775094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 24781760Seschrock { 24791760Seschrock zfs_cmd_t zc = { 0 }; 24802082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 24811760Seschrock 24821760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 24835094Slling zc.zc_cookie = new_version; 24845094Slling 24854543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 24863237Slling return (zpool_standard_error_fmt(hdl, errno, 24872082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 24882082Seschrock zhp->zpool_name)); 24891760Seschrock return (0); 24901760Seschrock } 24912926Sek110237 24924988Sek110237 void 24934988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 24944988Sek110237 char *history_str) 24954988Sek110237 { 24964988Sek110237 int i; 24974988Sek110237 24984988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 24994988Sek110237 for (i = 1; i < argc; i++) { 25004988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 25014988Sek110237 HIS_MAX_RECORD_LEN) 25024988Sek110237 break; 25034988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 25044988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 25054988Sek110237 } 25064988Sek110237 } 25074988Sek110237 25082926Sek110237 /* 25094988Sek110237 * Stage command history for logging. 25102926Sek110237 */ 25114988Sek110237 int 25124988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 25132926Sek110237 { 25144988Sek110237 if (history_str == NULL) 25154988Sek110237 return (EINVAL); 25164988Sek110237 25174988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 25184988Sek110237 return (EINVAL); 25192926Sek110237 25204715Sek110237 if (hdl->libzfs_log_str != NULL) 25214543Smarks free(hdl->libzfs_log_str); 25222926Sek110237 25234988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 25244988Sek110237 return (no_memory(hdl)); 25254543Smarks 25264988Sek110237 return (0); 25272926Sek110237 } 25282926Sek110237 25292926Sek110237 /* 25302926Sek110237 * Perform ioctl to get some command history of a pool. 25312926Sek110237 * 25322926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 25332926Sek110237 * logical offset of the history buffer to start reading from. 25342926Sek110237 * 25352926Sek110237 * Upon return, 'off' is the next logical offset to read from and 25362926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 25372926Sek110237 */ 25382926Sek110237 static int 25392926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 25402926Sek110237 { 25412926Sek110237 zfs_cmd_t zc = { 0 }; 25422926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 25432926Sek110237 25442926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25452926Sek110237 25462926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 25472926Sek110237 zc.zc_history_len = *len; 25482926Sek110237 zc.zc_history_offset = *off; 25492926Sek110237 25502926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 25512926Sek110237 switch (errno) { 25522926Sek110237 case EPERM: 25533237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 25543237Slling dgettext(TEXT_DOMAIN, 25552926Sek110237 "cannot show history for pool '%s'"), 25562926Sek110237 zhp->zpool_name)); 25572926Sek110237 case ENOENT: 25583237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 25592926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 25602926Sek110237 "'%s'"), zhp->zpool_name)); 25613863Sek110237 case ENOTSUP: 25623863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 25633863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 25643863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 25652926Sek110237 default: 25663237Slling return (zpool_standard_error_fmt(hdl, errno, 25672926Sek110237 dgettext(TEXT_DOMAIN, 25682926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 25692926Sek110237 } 25702926Sek110237 } 25712926Sek110237 25722926Sek110237 *len = zc.zc_history_len; 25732926Sek110237 *off = zc.zc_history_offset; 25742926Sek110237 25752926Sek110237 return (0); 25762926Sek110237 } 25772926Sek110237 25782926Sek110237 /* 25792926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 25802926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 25812926Sek110237 * processed as there wasn't a complete record. 25822926Sek110237 */ 25832926Sek110237 static int 25842926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 25852926Sek110237 nvlist_t ***records, uint_t *numrecords) 25862926Sek110237 { 25872926Sek110237 uint64_t reclen; 25882926Sek110237 nvlist_t *nv; 25892926Sek110237 int i; 25902926Sek110237 25912926Sek110237 while (bytes_read > sizeof (reclen)) { 25922926Sek110237 25932926Sek110237 /* get length of packed record (stored as little endian) */ 25942926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 25952926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 25962926Sek110237 25972926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 25982926Sek110237 break; 25992926Sek110237 26002926Sek110237 /* unpack record */ 26012926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 26022926Sek110237 return (ENOMEM); 26032926Sek110237 bytes_read -= sizeof (reclen) + reclen; 26042926Sek110237 buf += sizeof (reclen) + reclen; 26052926Sek110237 26062926Sek110237 /* add record to nvlist array */ 26072926Sek110237 (*numrecords)++; 26082926Sek110237 if (ISP2(*numrecords + 1)) { 26092926Sek110237 *records = realloc(*records, 26102926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 26112926Sek110237 } 26122926Sek110237 (*records)[*numrecords - 1] = nv; 26132926Sek110237 } 26142926Sek110237 26152926Sek110237 *leftover = bytes_read; 26162926Sek110237 return (0); 26172926Sek110237 } 26182926Sek110237 26192926Sek110237 #define HIS_BUF_LEN (128*1024) 26202926Sek110237 26212926Sek110237 /* 26222926Sek110237 * Retrieve the command history of a pool. 26232926Sek110237 */ 26242926Sek110237 int 26252926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 26262926Sek110237 { 26272926Sek110237 char buf[HIS_BUF_LEN]; 26282926Sek110237 uint64_t off = 0; 26292926Sek110237 nvlist_t **records = NULL; 26302926Sek110237 uint_t numrecords = 0; 26312926Sek110237 int err, i; 26322926Sek110237 26332926Sek110237 do { 26342926Sek110237 uint64_t bytes_read = sizeof (buf); 26352926Sek110237 uint64_t leftover; 26362926Sek110237 26372926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 26382926Sek110237 break; 26392926Sek110237 26402926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 26412926Sek110237 if (!bytes_read) 26422926Sek110237 break; 26432926Sek110237 26442926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 26452926Sek110237 &leftover, &records, &numrecords)) != 0) 26462926Sek110237 break; 26472926Sek110237 off -= leftover; 26482926Sek110237 26492926Sek110237 /* CONSTCOND */ 26502926Sek110237 } while (1); 26512926Sek110237 26522926Sek110237 if (!err) { 26532926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 26542926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 26552926Sek110237 records, numrecords) == 0); 26562926Sek110237 } 26572926Sek110237 for (i = 0; i < numrecords; i++) 26582926Sek110237 nvlist_free(records[i]); 26592926Sek110237 free(records); 26602926Sek110237 26612926Sek110237 return (err); 26622926Sek110237 } 26633444Sek110237 26643444Sek110237 void 26653444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 26663444Sek110237 char *pathname, size_t len) 26673444Sek110237 { 26683444Sek110237 zfs_cmd_t zc = { 0 }; 26693444Sek110237 boolean_t mounted = B_FALSE; 26703444Sek110237 char *mntpnt = NULL; 26713444Sek110237 char dsname[MAXNAMELEN]; 26723444Sek110237 26733444Sek110237 if (dsobj == 0) { 26743444Sek110237 /* special case for the MOS */ 26753444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 26763444Sek110237 return; 26773444Sek110237 } 26783444Sek110237 26793444Sek110237 /* get the dataset's name */ 26803444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 26813444Sek110237 zc.zc_obj = dsobj; 26823444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 26833444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 26843444Sek110237 /* just write out a path of two object numbers */ 26853444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 26863444Sek110237 dsobj, obj); 26873444Sek110237 return; 26883444Sek110237 } 26893444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 26903444Sek110237 26913444Sek110237 /* find out if the dataset is mounted */ 26923444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 26933444Sek110237 26943444Sek110237 /* get the corrupted object's path */ 26953444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 26963444Sek110237 zc.zc_obj = obj; 26973444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 26983444Sek110237 &zc) == 0) { 26993444Sek110237 if (mounted) { 27003444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 27013444Sek110237 zc.zc_value); 27023444Sek110237 } else { 27033444Sek110237 (void) snprintf(pathname, len, "%s:%s", 27043444Sek110237 dsname, zc.zc_value); 27053444Sek110237 } 27063444Sek110237 } else { 27073444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 27083444Sek110237 } 27093444Sek110237 free(mntpnt); 27103444Sek110237 } 27113912Slling 27124276Staylor #define RDISK_ROOT "/dev/rdsk" 27134276Staylor #define BACKUP_SLICE "s2" 27144276Staylor /* 27154276Staylor * Don't start the slice at the default block of 34; many storage 27164276Staylor * devices will use a stripe width of 128k, so start there instead. 27174276Staylor */ 27184276Staylor #define NEW_START_BLOCK 256 27194276Staylor 27204276Staylor /* 27217042Sgw25295 * Read the EFI label from the config, if a label does not exist then 27227042Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 27237042Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 27247042Sgw25295 * partition. 27257042Sgw25295 */ 27267042Sgw25295 static int 27277042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 27287042Sgw25295 { 27297042Sgw25295 char *path; 27307042Sgw25295 int fd; 27317042Sgw25295 char diskname[MAXPATHLEN]; 27327042Sgw25295 int err = -1; 27337042Sgw25295 27347042Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 27357042Sgw25295 return (err); 27367042Sgw25295 27377042Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 27387042Sgw25295 strrchr(path, '/')); 27397042Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 27407042Sgw25295 struct dk_gpt *vtoc; 27417042Sgw25295 27427042Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 27437042Sgw25295 if (sb != NULL) 27447042Sgw25295 *sb = vtoc->efi_parts[0].p_start; 27457042Sgw25295 efi_free(vtoc); 27467042Sgw25295 } 27477042Sgw25295 (void) close(fd); 27487042Sgw25295 } 27497042Sgw25295 return (err); 27507042Sgw25295 } 27517042Sgw25295 27527042Sgw25295 /* 27534276Staylor * determine where a partition starts on a disk in the current 27544276Staylor * configuration 27554276Staylor */ 27564276Staylor static diskaddr_t 27574276Staylor find_start_block(nvlist_t *config) 27584276Staylor { 27594276Staylor nvlist_t **child; 27604276Staylor uint_t c, children; 27614276Staylor diskaddr_t sb = MAXOFFSET_T; 27624276Staylor uint64_t wholedisk; 27634276Staylor 27644276Staylor if (nvlist_lookup_nvlist_array(config, 27654276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 27664276Staylor if (nvlist_lookup_uint64(config, 27674276Staylor ZPOOL_CONFIG_WHOLE_DISK, 27684276Staylor &wholedisk) != 0 || !wholedisk) { 27694276Staylor return (MAXOFFSET_T); 27704276Staylor } 27717042Sgw25295 if (read_efi_label(config, &sb) < 0) 27727042Sgw25295 sb = MAXOFFSET_T; 27734276Staylor return (sb); 27744276Staylor } 27754276Staylor 27764276Staylor for (c = 0; c < children; c++) { 27774276Staylor sb = find_start_block(child[c]); 27784276Staylor if (sb != MAXOFFSET_T) { 27794276Staylor return (sb); 27804276Staylor } 27814276Staylor } 27824276Staylor return (MAXOFFSET_T); 27834276Staylor } 27844276Staylor 27854276Staylor /* 27864276Staylor * Label an individual disk. The name provided is the short name, 27874276Staylor * stripped of any leading /dev path. 27884276Staylor */ 27894276Staylor int 27904276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 27914276Staylor { 27924276Staylor char path[MAXPATHLEN]; 27934276Staylor struct dk_gpt *vtoc; 27944276Staylor int fd; 27954276Staylor size_t resv = EFI_MIN_RESV_SIZE; 27964276Staylor uint64_t slice_size; 27974276Staylor diskaddr_t start_block; 27984276Staylor char errbuf[1024]; 27994276Staylor 28006289Smmusante /* prepare an error message just in case */ 28016289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 28026289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 28036289Smmusante 28044276Staylor if (zhp) { 28054276Staylor nvlist_t *nvroot; 28064276Staylor 28074276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 28084276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 28094276Staylor 28104276Staylor if (zhp->zpool_start_block == 0) 28114276Staylor start_block = find_start_block(nvroot); 28124276Staylor else 28134276Staylor start_block = zhp->zpool_start_block; 28144276Staylor zhp->zpool_start_block = start_block; 28154276Staylor } else { 28164276Staylor /* new pool */ 28174276Staylor start_block = NEW_START_BLOCK; 28184276Staylor } 28194276Staylor 28204276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 28214276Staylor BACKUP_SLICE); 28224276Staylor 28234276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 28244276Staylor /* 28254276Staylor * This shouldn't happen. We've long since verified that this 28264276Staylor * is a valid device. 28274276Staylor */ 28286289Smmusante zfs_error_aux(hdl, 28296289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 28304276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 28314276Staylor } 28324276Staylor 28334276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 28344276Staylor /* 28354276Staylor * The only way this can fail is if we run out of memory, or we 28364276Staylor * were unable to read the disk's capacity 28374276Staylor */ 28384276Staylor if (errno == ENOMEM) 28394276Staylor (void) no_memory(hdl); 28404276Staylor 28414276Staylor (void) close(fd); 28426289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28436289Smmusante "unable to read disk capacity"), name); 28444276Staylor 28454276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 28464276Staylor } 28474276Staylor 28484276Staylor slice_size = vtoc->efi_last_u_lba + 1; 28494276Staylor slice_size -= EFI_MIN_RESV_SIZE; 28504276Staylor if (start_block == MAXOFFSET_T) 28514276Staylor start_block = NEW_START_BLOCK; 28524276Staylor slice_size -= start_block; 28534276Staylor 28544276Staylor vtoc->efi_parts[0].p_start = start_block; 28554276Staylor vtoc->efi_parts[0].p_size = slice_size; 28564276Staylor 28574276Staylor /* 28584276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 28594276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 28604276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 28614276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 28624276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 28634276Staylor * can get, in the absence of V_OTHER. 28644276Staylor */ 28654276Staylor vtoc->efi_parts[0].p_tag = V_USR; 28664276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 28674276Staylor 28684276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 28694276Staylor vtoc->efi_parts[8].p_size = resv; 28704276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 28714276Staylor 28724276Staylor if (efi_write(fd, vtoc) != 0) { 28734276Staylor /* 28744276Staylor * Some block drivers (like pcata) may not support EFI 28754276Staylor * GPT labels. Print out a helpful error message dir- 28764276Staylor * ecting the user to manually label the disk and give 28774276Staylor * a specific slice. 28784276Staylor */ 28794276Staylor (void) close(fd); 28804276Staylor efi_free(vtoc); 28814276Staylor 28824276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28836289Smmusante "try using fdisk(1M) and then provide a specific slice")); 28844276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 28854276Staylor } 28864276Staylor 28874276Staylor (void) close(fd); 28884276Staylor efi_free(vtoc); 28894276Staylor return (0); 28904276Staylor } 28916423Sgw25295 28926423Sgw25295 static boolean_t 28936423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 28946423Sgw25295 { 28956423Sgw25295 char *type; 28966423Sgw25295 nvlist_t **child; 28976423Sgw25295 uint_t children, c; 28986423Sgw25295 28996423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 29006423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 29016423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 29026423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 29036423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 29046423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29056423Sgw25295 "vdev type '%s' is not supported"), type); 29066423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 29076423Sgw25295 return (B_FALSE); 29086423Sgw25295 } 29096423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 29106423Sgw25295 &child, &children) == 0) { 29116423Sgw25295 for (c = 0; c < children; c++) { 29126423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 29136423Sgw25295 return (B_FALSE); 29146423Sgw25295 } 29156423Sgw25295 } 29166423Sgw25295 return (B_TRUE); 29176423Sgw25295 } 29186423Sgw25295 29196423Sgw25295 /* 29206423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 29216423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 29226423Sgw25295 */ 29236423Sgw25295 int 29246423Sgw25295 zvol_check_dump_config(char *arg) 29256423Sgw25295 { 29266423Sgw25295 zpool_handle_t *zhp = NULL; 29276423Sgw25295 nvlist_t *config, *nvroot; 29286423Sgw25295 char *p, *volname; 29296423Sgw25295 nvlist_t **top; 29306423Sgw25295 uint_t toplevels; 29316423Sgw25295 libzfs_handle_t *hdl; 29326423Sgw25295 char errbuf[1024]; 29336423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 29346423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 29356423Sgw25295 int ret = 1; 29366423Sgw25295 29376423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 29386423Sgw25295 return (-1); 29396423Sgw25295 } 29406423Sgw25295 29416423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29426423Sgw25295 "dump is not supported on device '%s'"), arg); 29436423Sgw25295 29446423Sgw25295 if ((hdl = libzfs_init()) == NULL) 29456423Sgw25295 return (1); 29466423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 29476423Sgw25295 29486423Sgw25295 volname = arg + pathlen; 29496423Sgw25295 29506423Sgw25295 /* check the configuration of the pool */ 29516423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 29526423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29536423Sgw25295 "malformed dataset name")); 29546423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 29556423Sgw25295 return (1); 29566423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 29576423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29586423Sgw25295 "dataset name is too long")); 29596423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 29606423Sgw25295 return (1); 29616423Sgw25295 } else { 29626423Sgw25295 (void) strncpy(poolname, volname, p - volname); 29636423Sgw25295 poolname[p - volname] = '\0'; 29646423Sgw25295 } 29656423Sgw25295 29666423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 29676423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29686423Sgw25295 "could not open pool '%s'"), poolname); 29696423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 29706423Sgw25295 goto out; 29716423Sgw25295 } 29726423Sgw25295 config = zpool_get_config(zhp, NULL); 29736423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 29746423Sgw25295 &nvroot) != 0) { 29756423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29766423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 29776423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 29786423Sgw25295 goto out; 29796423Sgw25295 } 29806423Sgw25295 29816423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 29826423Sgw25295 &top, &toplevels) == 0); 29836423Sgw25295 if (toplevels != 1) { 29846423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29856423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 29866423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 29876423Sgw25295 goto out; 29886423Sgw25295 } 29896423Sgw25295 29906423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 29916423Sgw25295 goto out; 29926423Sgw25295 } 29936423Sgw25295 ret = 0; 29946423Sgw25295 29956423Sgw25295 out: 29966423Sgw25295 if (zhp) 29976423Sgw25295 zpool_close(zhp); 29986423Sgw25295 libzfs_fini(hdl); 29996423Sgw25295 return (ret); 30006423Sgw25295 } 3001