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 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 293126Sahl #include <alloca.h> 30789Sahrens #include <assert.h> 31789Sahrens #include <ctype.h> 32789Sahrens #include <errno.h> 33789Sahrens #include <devid.h> 343126Sahl #include <dirent.h> 35789Sahrens #include <fcntl.h> 36789Sahrens #include <libintl.h> 37789Sahrens #include <stdio.h> 38789Sahrens #include <stdlib.h> 393126Sahl #include <strings.h> 40789Sahrens #include <unistd.h> 417184Stimh #include <zone.h> 424276Staylor #include <sys/efi_partition.h> 434276Staylor #include <sys/vtoc.h> 44789Sahrens #include <sys/zfs_ioctl.h> 451544Seschrock #include <sys/zio.h> 462926Sek110237 #include <strings.h> 47789Sahrens 48789Sahrens #include "zfs_namecheck.h" 493912Slling #include "zfs_prop.h" 50789Sahrens #include "libzfs_impl.h" 51789Sahrens 527042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 535094Slling 545094Slling /* 555094Slling * ==================================================================== 565094Slling * zpool property functions 575094Slling * ==================================================================== 585094Slling */ 595094Slling 605094Slling static int 615094Slling zpool_get_all_props(zpool_handle_t *zhp) 625094Slling { 635094Slling zfs_cmd_t zc = { 0 }; 645094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 655094Slling 665094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 675094Slling 685094Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 695094Slling return (-1); 705094Slling 715094Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 725094Slling if (errno == ENOMEM) { 735094Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 745094Slling zcmd_free_nvlists(&zc); 755094Slling return (-1); 765094Slling } 775094Slling } else { 785094Slling zcmd_free_nvlists(&zc); 795094Slling return (-1); 805094Slling } 815094Slling } 825094Slling 835094Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 845094Slling zcmd_free_nvlists(&zc); 855094Slling return (-1); 865094Slling } 875094Slling 885094Slling zcmd_free_nvlists(&zc); 895094Slling 905094Slling return (0); 915094Slling } 925094Slling 935094Slling static int 945094Slling zpool_props_refresh(zpool_handle_t *zhp) 955094Slling { 965094Slling nvlist_t *old_props; 975094Slling 985094Slling old_props = zhp->zpool_props; 995094Slling 1005094Slling if (zpool_get_all_props(zhp) != 0) 1015094Slling return (-1); 1025094Slling 1035094Slling nvlist_free(old_props); 1045094Slling return (0); 1055094Slling } 1065094Slling 1075094Slling static char * 1085094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 1095094Slling zprop_source_t *src) 1105094Slling { 1115094Slling nvlist_t *nv, *nvl; 1125094Slling uint64_t ival; 1135094Slling char *value; 1145094Slling zprop_source_t source; 1155094Slling 1165094Slling nvl = zhp->zpool_props; 1175094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1185094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 1195094Slling source = ival; 1205094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1215094Slling } else { 1225094Slling source = ZPROP_SRC_DEFAULT; 1235094Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 1245094Slling value = "-"; 1255094Slling } 1265094Slling 1275094Slling if (src) 1285094Slling *src = source; 1295094Slling 1305094Slling return (value); 1315094Slling } 1325094Slling 1335094Slling uint64_t 1345094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 1355094Slling { 1365094Slling nvlist_t *nv, *nvl; 1375094Slling uint64_t value; 1385094Slling zprop_source_t source; 1395094Slling 1405094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) 1415094Slling return (zpool_prop_default_numeric(prop)); 1425094Slling 1435094Slling nvl = zhp->zpool_props; 1445094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1455094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 1465094Slling source = value; 1475094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1485094Slling } else { 1495094Slling source = ZPROP_SRC_DEFAULT; 1505094Slling value = zpool_prop_default_numeric(prop); 1515094Slling } 1525094Slling 1535094Slling if (src) 1545094Slling *src = source; 1555094Slling 1565094Slling return (value); 1575094Slling } 1585094Slling 1595094Slling /* 1605094Slling * Map VDEV STATE to printed strings. 1615094Slling */ 1625094Slling char * 1635094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 1645094Slling { 1655094Slling switch (state) { 1665094Slling case VDEV_STATE_CLOSED: 1675094Slling case VDEV_STATE_OFFLINE: 1685094Slling return (gettext("OFFLINE")); 1695094Slling case VDEV_STATE_REMOVED: 1705094Slling return (gettext("REMOVED")); 1715094Slling case VDEV_STATE_CANT_OPEN: 1725094Slling if (aux == VDEV_AUX_CORRUPT_DATA) 1735094Slling return (gettext("FAULTED")); 1745094Slling else 1755094Slling return (gettext("UNAVAIL")); 1765094Slling case VDEV_STATE_FAULTED: 1775094Slling return (gettext("FAULTED")); 1785094Slling case VDEV_STATE_DEGRADED: 1795094Slling return (gettext("DEGRADED")); 1805094Slling case VDEV_STATE_HEALTHY: 1815094Slling return (gettext("ONLINE")); 1825094Slling } 1835094Slling 1845094Slling return (gettext("UNKNOWN")); 1855094Slling } 1865094Slling 1875094Slling /* 1885094Slling * Get a zpool property value for 'prop' and return the value in 1895094Slling * a pre-allocated buffer. 1905094Slling */ 1915094Slling int 1925094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 1935094Slling zprop_source_t *srctype) 1945094Slling { 1955094Slling uint64_t intval; 1965094Slling const char *strval; 1975094Slling zprop_source_t src = ZPROP_SRC_NONE; 1985094Slling nvlist_t *nvroot; 1995094Slling vdev_stat_t *vs; 2005094Slling uint_t vsc; 2015094Slling 2025094Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 2035094Slling if (prop == ZPOOL_PROP_NAME) 2045094Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 2055094Slling else if (prop == ZPOOL_PROP_HEALTH) 2065094Slling (void) strlcpy(buf, "FAULTED", len); 2075094Slling else 2085094Slling (void) strlcpy(buf, "-", len); 2095094Slling return (0); 2105094Slling } 2115094Slling 2125094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 2135094Slling prop != ZPOOL_PROP_NAME) 2145094Slling return (-1); 2155094Slling 2165094Slling switch (zpool_prop_get_type(prop)) { 2175094Slling case PROP_TYPE_STRING: 2185094Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 2195094Slling len); 2205094Slling break; 2215094Slling 2225094Slling case PROP_TYPE_NUMBER: 2235094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2245094Slling 2255094Slling switch (prop) { 2265094Slling case ZPOOL_PROP_SIZE: 2275094Slling case ZPOOL_PROP_USED: 2285094Slling case ZPOOL_PROP_AVAILABLE: 2295094Slling (void) zfs_nicenum(intval, buf, len); 2305094Slling break; 2315094Slling 2325094Slling case ZPOOL_PROP_CAPACITY: 2335094Slling (void) snprintf(buf, len, "%llu%%", 2345094Slling (u_longlong_t)intval); 2355094Slling break; 2365094Slling 2375094Slling case ZPOOL_PROP_HEALTH: 2385094Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2395094Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2405094Slling verify(nvlist_lookup_uint64_array(nvroot, 2415094Slling ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 2425094Slling 2435094Slling (void) strlcpy(buf, zpool_state_to_name(intval, 2445094Slling vs->vs_aux), len); 2455094Slling break; 2465094Slling default: 2475094Slling (void) snprintf(buf, len, "%llu", intval); 2485094Slling } 2495094Slling break; 2505094Slling 2515094Slling case PROP_TYPE_INDEX: 2525094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2535094Slling if (zpool_prop_index_to_string(prop, intval, &strval) 2545094Slling != 0) 2555094Slling return (-1); 2565094Slling (void) strlcpy(buf, strval, len); 2575094Slling break; 2585094Slling 2595094Slling default: 2605094Slling abort(); 2615094Slling } 2625094Slling 2635094Slling if (srctype) 2645094Slling *srctype = src; 2655094Slling 2665094Slling return (0); 2675094Slling } 2685094Slling 2695094Slling /* 2705094Slling * Check if the bootfs name has the same pool name as it is set to. 2715094Slling * Assuming bootfs is a valid dataset name. 2725094Slling */ 2735094Slling static boolean_t 2745094Slling bootfs_name_valid(const char *pool, char *bootfs) 2755094Slling { 2765094Slling int len = strlen(pool); 2775094Slling 2785094Slling if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM)) 2795094Slling return (B_FALSE); 2805094Slling 2815094Slling if (strncmp(pool, bootfs, len) == 0 && 2825094Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 2835094Slling return (B_TRUE); 2845094Slling 2855094Slling return (B_FALSE); 2865094Slling } 2875094Slling 2885094Slling /* 2897042Sgw25295 * Inspect the configuration to determine if any of the devices contain 2907042Sgw25295 * an EFI label. 2917042Sgw25295 */ 2927042Sgw25295 static boolean_t 2937042Sgw25295 pool_uses_efi(nvlist_t *config) 2947042Sgw25295 { 2957042Sgw25295 nvlist_t **child; 2967042Sgw25295 uint_t c, children; 2977042Sgw25295 2987042Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 2997042Sgw25295 &child, &children) != 0) 3007042Sgw25295 return (read_efi_label(config, NULL) >= 0); 3017042Sgw25295 3027042Sgw25295 for (c = 0; c < children; c++) { 3037042Sgw25295 if (pool_uses_efi(child[c])) 3047042Sgw25295 return (B_TRUE); 3057042Sgw25295 } 3067042Sgw25295 return (B_FALSE); 3077042Sgw25295 } 3087042Sgw25295 3097042Sgw25295 /* 3105094Slling * Given an nvlist of zpool properties to be set, validate that they are 3115094Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 3125094Slling * specified as strings. 3135094Slling */ 3145094Slling static nvlist_t * 3157184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 3165094Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 3175094Slling { 3185094Slling nvpair_t *elem; 3195094Slling nvlist_t *retprops; 3205094Slling zpool_prop_t prop; 3215094Slling char *strval; 3225094Slling uint64_t intval; 3235363Seschrock char *slash; 3245363Seschrock struct stat64 statbuf; 3257042Sgw25295 zpool_handle_t *zhp; 3267042Sgw25295 nvlist_t *nvroot; 3275094Slling 3285094Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 3295094Slling (void) no_memory(hdl); 3305094Slling return (NULL); 3315094Slling } 3325094Slling 3335094Slling elem = NULL; 3345094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3355094Slling const char *propname = nvpair_name(elem); 3365094Slling 3375094Slling /* 3385094Slling * Make sure this property is valid and applies to this type. 3395094Slling */ 3405094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 3415094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3425094Slling "invalid property '%s'"), propname); 3435094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3445094Slling goto error; 3455094Slling } 3465094Slling 3475094Slling if (zpool_prop_readonly(prop)) { 3485094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 3495094Slling "is readonly"), propname); 3505094Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 3515094Slling goto error; 3525094Slling } 3535094Slling 3545094Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 3555094Slling &strval, &intval, errbuf) != 0) 3565094Slling goto error; 3575094Slling 3585094Slling /* 3595094Slling * Perform additional checking for specific properties. 3605094Slling */ 3615094Slling switch (prop) { 3625094Slling case ZPOOL_PROP_VERSION: 3635094Slling if (intval < version || intval > SPA_VERSION) { 3645094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3655094Slling "property '%s' number %d is invalid."), 3665094Slling propname, intval); 3675094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 3685094Slling goto error; 3695094Slling } 3705094Slling break; 3715094Slling 3725094Slling case ZPOOL_PROP_BOOTFS: 3735094Slling if (create_or_import) { 3745094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3755094Slling "property '%s' cannot be set at creation " 3765094Slling "or import time"), propname); 3775094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3785094Slling goto error; 3795094Slling } 3805094Slling 3815094Slling if (version < SPA_VERSION_BOOTFS) { 3825094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3835094Slling "pool must be upgraded to support " 3845094Slling "'%s' property"), propname); 3855094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 3865094Slling goto error; 3875094Slling } 3885094Slling 3895094Slling /* 3905094Slling * bootfs property value has to be a dataset name and 3915094Slling * the dataset has to be in the same pool as it sets to. 3925094Slling */ 3935094Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 3945094Slling strval)) { 3955094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 3965094Slling "is an invalid name"), strval); 3975094Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 3985094Slling goto error; 3995094Slling } 4007042Sgw25295 4017042Sgw25295 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 4027042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4037042Sgw25295 "could not open pool '%s'"), poolname); 4047042Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4057042Sgw25295 goto error; 4067042Sgw25295 } 4077042Sgw25295 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 4087042Sgw25295 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4097042Sgw25295 4107042Sgw25295 /* 4117042Sgw25295 * bootfs property cannot be set on a disk which has 4127042Sgw25295 * been EFI labeled. 4137042Sgw25295 */ 4147042Sgw25295 if (pool_uses_efi(nvroot)) { 4157042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4167042Sgw25295 "property '%s' not supported on " 4177042Sgw25295 "EFI labeled devices"), propname); 4187042Sgw25295 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 4197042Sgw25295 zpool_close(zhp); 4207042Sgw25295 goto error; 4217042Sgw25295 } 4227042Sgw25295 zpool_close(zhp); 4235094Slling break; 4245094Slling 4255094Slling case ZPOOL_PROP_ALTROOT: 4265094Slling if (!create_or_import) { 4275094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4285094Slling "property '%s' can only be set during pool " 4295094Slling "creation or import"), propname); 4305094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4315094Slling goto error; 4325094Slling } 4335094Slling 4345094Slling if (strval[0] != '/') { 4355094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4365094Slling "bad alternate root '%s'"), strval); 4375094Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4385094Slling goto error; 4395094Slling } 4405094Slling break; 4415363Seschrock 4425363Seschrock case ZPOOL_PROP_CACHEFILE: 4435363Seschrock if (strval[0] == '\0') 4445363Seschrock break; 4455363Seschrock 4465363Seschrock if (strcmp(strval, "none") == 0) 4475363Seschrock break; 4485363Seschrock 4495363Seschrock if (strval[0] != '/') { 4505363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4515363Seschrock "property '%s' must be empty, an " 4525363Seschrock "absolute path, or 'none'"), propname); 4535363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4545363Seschrock goto error; 4555363Seschrock } 4565363Seschrock 4575363Seschrock slash = strrchr(strval, '/'); 4585363Seschrock 4595363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4605363Seschrock strcmp(slash, "/..") == 0) { 4615363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4625363Seschrock "'%s' is not a valid file"), strval); 4635363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4645363Seschrock goto error; 4655363Seschrock } 4665363Seschrock 4675363Seschrock *slash = '\0'; 4685363Seschrock 4695621Seschrock if (strval[0] != '\0' && 4705621Seschrock (stat64(strval, &statbuf) != 0 || 4715621Seschrock !S_ISDIR(statbuf.st_mode))) { 4725363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4735363Seschrock "'%s' is not a valid directory"), 4745363Seschrock strval); 4755363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4765363Seschrock goto error; 4775363Seschrock } 4785363Seschrock 4795363Seschrock *slash = '/'; 4805363Seschrock break; 4815094Slling } 4825094Slling } 4835094Slling 4845094Slling return (retprops); 4855094Slling error: 4865094Slling nvlist_free(retprops); 4875094Slling return (NULL); 4885094Slling } 4895094Slling 4905094Slling /* 4915094Slling * Set zpool property : propname=propval. 4925094Slling */ 4935094Slling int 4945094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 4955094Slling { 4965094Slling zfs_cmd_t zc = { 0 }; 4975094Slling int ret = -1; 4985094Slling char errbuf[1024]; 4995094Slling nvlist_t *nvl = NULL; 5005094Slling nvlist_t *realprops; 5015094Slling uint64_t version; 5025094Slling 5035094Slling (void) snprintf(errbuf, sizeof (errbuf), 5045094Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 5055094Slling zhp->zpool_name); 5065094Slling 5075094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) 5085094Slling return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf)); 5095094Slling 5105094Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5115094Slling return (no_memory(zhp->zpool_hdl)); 5125094Slling 5135094Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 5145094Slling nvlist_free(nvl); 5155094Slling return (no_memory(zhp->zpool_hdl)); 5165094Slling } 5175094Slling 5185094Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5197184Stimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 5205094Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 5215094Slling nvlist_free(nvl); 5225094Slling return (-1); 5235094Slling } 5245094Slling 5255094Slling nvlist_free(nvl); 5265094Slling nvl = realprops; 5275094Slling 5285094Slling /* 5295094Slling * Execute the corresponding ioctl() to set this property. 5305094Slling */ 5315094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 5325094Slling 5335094Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 5345094Slling nvlist_free(nvl); 5355094Slling return (-1); 5365094Slling } 5375094Slling 5385094Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 5395094Slling 5405094Slling zcmd_free_nvlists(&zc); 5415094Slling nvlist_free(nvl); 5425094Slling 5435094Slling if (ret) 5445094Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 5455094Slling else 5465094Slling (void) zpool_props_refresh(zhp); 5475094Slling 5485094Slling return (ret); 5495094Slling } 5505094Slling 5515094Slling int 5525094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 5535094Slling { 5545094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 5555094Slling zprop_list_t *entry; 5565094Slling char buf[ZFS_MAXPROPLEN]; 5575094Slling 5585094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 5595094Slling return (-1); 5605094Slling 5615094Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 5625094Slling 5635094Slling if (entry->pl_fixed) 5645094Slling continue; 5655094Slling 5665094Slling if (entry->pl_prop != ZPROP_INVAL && 5675094Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 5685094Slling NULL) == 0) { 5695094Slling if (strlen(buf) > entry->pl_width) 5705094Slling entry->pl_width = strlen(buf); 5715094Slling } 5725094Slling } 5735094Slling 5745094Slling return (0); 5755094Slling } 5765094Slling 5775094Slling 578789Sahrens /* 579789Sahrens * Validate the given pool name, optionally putting an extended error message in 580789Sahrens * 'buf'. 581789Sahrens */ 5826423Sgw25295 boolean_t 5832082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 584789Sahrens { 585789Sahrens namecheck_err_t why; 586789Sahrens char what; 5871773Seschrock int ret; 588789Sahrens 5891773Seschrock ret = pool_namecheck(pool, &why, &what); 5901773Seschrock 5911773Seschrock /* 5921773Seschrock * The rules for reserved pool names were extended at a later point. 5931773Seschrock * But we need to support users with existing pools that may now be 5941773Seschrock * invalid. So we only check for this expanded set of names during a 5951773Seschrock * create (or import), and only in userland. 5961773Seschrock */ 5971773Seschrock if (ret == 0 && !isopen && 5981773Seschrock (strncmp(pool, "mirror", 6) == 0 || 5991773Seschrock strncmp(pool, "raidz", 5) == 0 || 6004527Sperrin strncmp(pool, "spare", 5) == 0 || 6014527Sperrin strcmp(pool, "log") == 0)) { 6026423Sgw25295 if (hdl != NULL) 6036423Sgw25295 zfs_error_aux(hdl, 6046423Sgw25295 dgettext(TEXT_DOMAIN, "name is reserved")); 6052082Seschrock return (B_FALSE); 6061773Seschrock } 6071773Seschrock 6081773Seschrock 6091773Seschrock if (ret != 0) { 6102082Seschrock if (hdl != NULL) { 611789Sahrens switch (why) { 6121003Slling case NAME_ERR_TOOLONG: 6132082Seschrock zfs_error_aux(hdl, 6141003Slling dgettext(TEXT_DOMAIN, "name is too long")); 6151003Slling break; 6161003Slling 617789Sahrens case NAME_ERR_INVALCHAR: 6182082Seschrock zfs_error_aux(hdl, 619789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 620789Sahrens "'%c' in pool name"), what); 621789Sahrens break; 622789Sahrens 623789Sahrens case NAME_ERR_NOLETTER: 6242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6252082Seschrock "name must begin with a letter")); 626789Sahrens break; 627789Sahrens 628789Sahrens case NAME_ERR_RESERVED: 6292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6302082Seschrock "name is reserved")); 631789Sahrens break; 632789Sahrens 633789Sahrens case NAME_ERR_DISKLIKE: 6342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6352082Seschrock "pool name is reserved")); 636789Sahrens break; 6372856Snd150628 6382856Snd150628 case NAME_ERR_LEADING_SLASH: 6392856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6402856Snd150628 "leading slash in name")); 6412856Snd150628 break; 6422856Snd150628 6432856Snd150628 case NAME_ERR_EMPTY_COMPONENT: 6442856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6452856Snd150628 "empty component in name")); 6462856Snd150628 break; 6472856Snd150628 6482856Snd150628 case NAME_ERR_TRAILING_SLASH: 6492856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6502856Snd150628 "trailing slash in name")); 6512856Snd150628 break; 6522856Snd150628 6532856Snd150628 case NAME_ERR_MULTIPLE_AT: 6542856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6552856Snd150628 "multiple '@' delimiters in name")); 6562856Snd150628 break; 6572856Snd150628 658789Sahrens } 659789Sahrens } 6602082Seschrock return (B_FALSE); 661789Sahrens } 662789Sahrens 6632082Seschrock return (B_TRUE); 664789Sahrens } 665789Sahrens 666789Sahrens /* 667789Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 668789Sahrens * state. 669789Sahrens */ 670789Sahrens zpool_handle_t * 6712082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 672789Sahrens { 673789Sahrens zpool_handle_t *zhp; 6742142Seschrock boolean_t missing; 675789Sahrens 676789Sahrens /* 677789Sahrens * Make sure the pool name is valid. 678789Sahrens */ 6792082Seschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 6803237Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 6812082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 6822082Seschrock pool); 683789Sahrens return (NULL); 684789Sahrens } 685789Sahrens 6862082Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 6872082Seschrock return (NULL); 688789Sahrens 6892082Seschrock zhp->zpool_hdl = hdl; 690789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 691789Sahrens 6922142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 6932142Seschrock zpool_close(zhp); 6942142Seschrock return (NULL); 6952142Seschrock } 6962142Seschrock 6972142Seschrock if (missing) { 6985094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 6993237Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 7005094Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 7012142Seschrock zpool_close(zhp); 7022142Seschrock return (NULL); 703789Sahrens } 704789Sahrens 705789Sahrens return (zhp); 706789Sahrens } 707789Sahrens 708789Sahrens /* 709789Sahrens * Like the above, but silent on error. Used when iterating over pools (because 710789Sahrens * the configuration cache may be out of date). 711789Sahrens */ 7122142Seschrock int 7132142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 714789Sahrens { 715789Sahrens zpool_handle_t *zhp; 7162142Seschrock boolean_t missing; 717789Sahrens 7182142Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7192142Seschrock return (-1); 720789Sahrens 7212082Seschrock zhp->zpool_hdl = hdl; 722789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 723789Sahrens 7242142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7252142Seschrock zpool_close(zhp); 7262142Seschrock return (-1); 727789Sahrens } 728789Sahrens 7292142Seschrock if (missing) { 7302142Seschrock zpool_close(zhp); 7312142Seschrock *ret = NULL; 7322142Seschrock return (0); 7332142Seschrock } 7342142Seschrock 7352142Seschrock *ret = zhp; 7362142Seschrock return (0); 737789Sahrens } 738789Sahrens 739789Sahrens /* 740789Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 741789Sahrens * state. 742789Sahrens */ 743789Sahrens zpool_handle_t * 7442082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 745789Sahrens { 746789Sahrens zpool_handle_t *zhp; 747789Sahrens 7482082Seschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 749789Sahrens return (NULL); 750789Sahrens 751789Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 7523237Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 7532082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 754789Sahrens zpool_close(zhp); 755789Sahrens return (NULL); 756789Sahrens } 757789Sahrens 758789Sahrens return (zhp); 759789Sahrens } 760789Sahrens 761789Sahrens /* 762789Sahrens * Close the handle. Simply frees the memory associated with the handle. 763789Sahrens */ 764789Sahrens void 765789Sahrens zpool_close(zpool_handle_t *zhp) 766789Sahrens { 767789Sahrens if (zhp->zpool_config) 768789Sahrens nvlist_free(zhp->zpool_config); 769952Seschrock if (zhp->zpool_old_config) 770952Seschrock nvlist_free(zhp->zpool_old_config); 7713912Slling if (zhp->zpool_props) 7723912Slling nvlist_free(zhp->zpool_props); 773789Sahrens free(zhp); 774789Sahrens } 775789Sahrens 776789Sahrens /* 777789Sahrens * Return the name of the pool. 778789Sahrens */ 779789Sahrens const char * 780789Sahrens zpool_get_name(zpool_handle_t *zhp) 781789Sahrens { 782789Sahrens return (zhp->zpool_name); 783789Sahrens } 784789Sahrens 785789Sahrens 786789Sahrens /* 787789Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 788789Sahrens */ 789789Sahrens int 790789Sahrens zpool_get_state(zpool_handle_t *zhp) 791789Sahrens { 792789Sahrens return (zhp->zpool_state); 793789Sahrens } 794789Sahrens 795789Sahrens /* 796789Sahrens * Create the named pool, using the provided vdev list. It is assumed 797789Sahrens * that the consumer has already validated the contents of the nvlist, so we 798789Sahrens * don't have to worry about error semantics. 799789Sahrens */ 800789Sahrens int 8012082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8027184Stimh nvlist_t *props, nvlist_t *fsprops) 803789Sahrens { 804789Sahrens zfs_cmd_t zc = { 0 }; 8057184Stimh nvlist_t *zc_fsprops = NULL; 8067184Stimh nvlist_t *zc_props = NULL; 8072082Seschrock char msg[1024]; 8085094Slling char *altroot; 8097184Stimh int ret = -1; 8102082Seschrock 8112082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 8122082Seschrock "cannot create '%s'"), pool); 813789Sahrens 8142082Seschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 8152082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 8162082Seschrock 8175320Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 8185320Slling return (-1); 8195320Slling 8207184Stimh if (props) { 8217184Stimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8227184Stimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8237184Stimh goto create_failed; 8247184Stimh } 8255320Slling } 826789Sahrens 8277184Stimh if (fsprops) { 8287184Stimh uint64_t zoned; 8297184Stimh char *zonestr; 8307184Stimh 8317184Stimh zoned = ((nvlist_lookup_string(fsprops, 8327184Stimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8337184Stimh strcmp(zonestr, "on") == 0); 8347184Stimh 8357184Stimh if ((zc_fsprops = zfs_valid_proplist(hdl, 8367184Stimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 8377184Stimh goto create_failed; 8387184Stimh } 8397184Stimh if (!zc_props && 8407184Stimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 8417184Stimh goto create_failed; 8427184Stimh } 8437184Stimh if (nvlist_add_nvlist(zc_props, 8447184Stimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 8457184Stimh goto create_failed; 8467184Stimh } 8477184Stimh } 8487184Stimh 8497184Stimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 8507184Stimh goto create_failed; 8517184Stimh 852789Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 853789Sahrens 8547184Stimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 8555320Slling 8562676Seschrock zcmd_free_nvlists(&zc); 8577184Stimh nvlist_free(zc_props); 8587184Stimh nvlist_free(zc_fsprops); 8592082Seschrock 860789Sahrens switch (errno) { 861789Sahrens case EBUSY: 862789Sahrens /* 863789Sahrens * This can happen if the user has specified the same 864789Sahrens * device multiple times. We can't reliably detect this 865789Sahrens * until we try to add it and see we already have a 866789Sahrens * label. 867789Sahrens */ 8682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8692082Seschrock "one or more vdevs refer to the same device")); 8702082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 871789Sahrens 872789Sahrens case EOVERFLOW: 873789Sahrens /* 8742082Seschrock * This occurs when one of the devices is below 875789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 876789Sahrens * device was the problem device since there's no 877789Sahrens * reliable way to determine device size from userland. 878789Sahrens */ 879789Sahrens { 880789Sahrens char buf[64]; 881789Sahrens 882789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 883789Sahrens 8842082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8852082Seschrock "one or more devices is less than the " 8862082Seschrock "minimum size (%s)"), buf); 887789Sahrens } 8882082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 889789Sahrens 890789Sahrens case ENOSPC: 8912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8922082Seschrock "one or more devices is out of space")); 8932082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 894789Sahrens 8955450Sbrendan case ENOTBLK: 8965450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8975450Sbrendan "cache device must be a disk or disk slice")); 8985450Sbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 8995450Sbrendan 900789Sahrens default: 9012082Seschrock return (zpool_standard_error(hdl, errno, msg)); 902789Sahrens } 903789Sahrens } 904789Sahrens 905789Sahrens /* 906789Sahrens * If this is an alternate root pool, then we automatically set the 9072676Seschrock * mountpoint of the root dataset to be '/'. 908789Sahrens */ 9095094Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 9105094Slling &altroot) == 0) { 911789Sahrens zfs_handle_t *zhp; 912789Sahrens 9135094Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 9142676Seschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 9152676Seschrock "/") == 0); 916789Sahrens 917789Sahrens zfs_close(zhp); 918789Sahrens } 919789Sahrens 9207184Stimh create_failed: 9215320Slling zcmd_free_nvlists(&zc); 9227184Stimh nvlist_free(zc_props); 9237184Stimh nvlist_free(zc_fsprops); 9247184Stimh return (ret); 925789Sahrens } 926789Sahrens 927789Sahrens /* 928789Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 929789Sahrens * datasets left in the pool. 930789Sahrens */ 931789Sahrens int 932789Sahrens zpool_destroy(zpool_handle_t *zhp) 933789Sahrens { 934789Sahrens zfs_cmd_t zc = { 0 }; 935789Sahrens zfs_handle_t *zfp = NULL; 9362082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9372082Seschrock char msg[1024]; 938789Sahrens 939789Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 9402082Seschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 9412082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 942789Sahrens return (-1); 943789Sahrens 9442856Snd150628 if (zpool_remove_zvol_links(zhp) != 0) 945789Sahrens return (-1); 946789Sahrens 947789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 948789Sahrens 9494543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 9502082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 9512082Seschrock "cannot destroy '%s'"), zhp->zpool_name); 952789Sahrens 9532082Seschrock if (errno == EROFS) { 9542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9552082Seschrock "one or more devices is read only")); 9562082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 9572082Seschrock } else { 9582082Seschrock (void) zpool_standard_error(hdl, errno, msg); 959789Sahrens } 960789Sahrens 961789Sahrens if (zfp) 962789Sahrens zfs_close(zfp); 963789Sahrens return (-1); 964789Sahrens } 965789Sahrens 966789Sahrens if (zfp) { 967789Sahrens remove_mountpoint(zfp); 968789Sahrens zfs_close(zfp); 969789Sahrens } 970789Sahrens 971789Sahrens return (0); 972789Sahrens } 973789Sahrens 974789Sahrens /* 975789Sahrens * Add the given vdevs to the pool. The caller must have already performed the 976789Sahrens * necessary verification to ensure that the vdev specification is well-formed. 977789Sahrens */ 978789Sahrens int 979789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 980789Sahrens { 9812676Seschrock zfs_cmd_t zc = { 0 }; 9822082Seschrock int ret; 9832082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9842082Seschrock char msg[1024]; 9855450Sbrendan nvlist_t **spares, **l2cache; 9865450Sbrendan uint_t nspares, nl2cache; 9872082Seschrock 9882082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 9892082Seschrock "cannot add to '%s'"), zhp->zpool_name); 9902082Seschrock 9915450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 9925450Sbrendan SPA_VERSION_SPARES && 9932082Seschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 9942082Seschrock &spares, &nspares) == 0) { 9952082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 9962082Seschrock "upgraded to add hot spares")); 9972082Seschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 9982082Seschrock } 999789Sahrens 10005450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10015450Sbrendan SPA_VERSION_L2CACHE && 10025450Sbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 10035450Sbrendan &l2cache, &nl2cache) == 0) { 10045450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10055450Sbrendan "upgraded to add cache devices")); 10065450Sbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10075450Sbrendan } 10085450Sbrendan 10095094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 10102082Seschrock return (-1); 1011789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1012789Sahrens 10134543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1014789Sahrens switch (errno) { 1015789Sahrens case EBUSY: 1016789Sahrens /* 1017789Sahrens * This can happen if the user has specified the same 1018789Sahrens * device multiple times. We can't reliably detect this 1019789Sahrens * until we try to add it and see we already have a 1020789Sahrens * label. 1021789Sahrens */ 10222082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10232082Seschrock "one or more vdevs refer to the same device")); 10242082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1025789Sahrens break; 1026789Sahrens 1027789Sahrens case EOVERFLOW: 1028789Sahrens /* 1029789Sahrens * This occurrs when one of the devices is below 1030789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1031789Sahrens * device was the problem device since there's no 1032789Sahrens * reliable way to determine device size from userland. 1033789Sahrens */ 1034789Sahrens { 1035789Sahrens char buf[64]; 1036789Sahrens 1037789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1038789Sahrens 10392082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10402082Seschrock "device is less than the minimum " 10412082Seschrock "size (%s)"), buf); 1042789Sahrens } 10432082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 10442082Seschrock break; 10452082Seschrock 10462082Seschrock case ENOTSUP: 10472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10484527Sperrin "pool must be upgraded to add these vdevs")); 10492082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1050789Sahrens break; 1051789Sahrens 10523912Slling case EDOM: 10533912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10544527Sperrin "root pool can not have multiple vdevs" 10554527Sperrin " or separate logs")); 10563912Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 10573912Slling break; 10583912Slling 10595450Sbrendan case ENOTBLK: 10605450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10615450Sbrendan "cache device must be a disk or disk slice")); 10625450Sbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 10635450Sbrendan break; 10645450Sbrendan 1065789Sahrens default: 10662082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1067789Sahrens } 1068789Sahrens 10692082Seschrock ret = -1; 10702082Seschrock } else { 10712082Seschrock ret = 0; 1072789Sahrens } 1073789Sahrens 10742676Seschrock zcmd_free_nvlists(&zc); 1075789Sahrens 10762082Seschrock return (ret); 1077789Sahrens } 1078789Sahrens 1079789Sahrens /* 1080789Sahrens * Exports the pool from the system. The caller must ensure that there are no 1081789Sahrens * mounted datasets in the pool. 1082789Sahrens */ 1083789Sahrens int 1084*7214Slling zpool_export(zpool_handle_t *zhp, boolean_t force) 1085789Sahrens { 1086789Sahrens zfs_cmd_t zc = { 0 }; 1087*7214Slling char msg[1024]; 1088789Sahrens 1089789Sahrens if (zpool_remove_zvol_links(zhp) != 0) 1090789Sahrens return (-1); 1091789Sahrens 1092*7214Slling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1093*7214Slling "cannot export '%s'"), zhp->zpool_name); 1094*7214Slling 1095789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1096*7214Slling zc.zc_cookie = force; 1097*7214Slling 1098*7214Slling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 1099*7214Slling switch (errno) { 1100*7214Slling case EXDEV: 1101*7214Slling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 1102*7214Slling "use '-f' to override the following errors:\n" 1103*7214Slling "'%s' has an active shared spare which could be" 1104*7214Slling " used by other pools once '%s' is exported."), 1105*7214Slling zhp->zpool_name, zhp->zpool_name); 1106*7214Slling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 1107*7214Slling msg)); 1108*7214Slling default: 1109*7214Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 1110*7214Slling msg)); 1111*7214Slling } 1112*7214Slling } 1113*7214Slling 1114789Sahrens return (0); 1115789Sahrens } 1116789Sahrens 1117789Sahrens /* 11185094Slling * zpool_import() is a contracted interface. Should be kept the same 11195094Slling * if possible. 11205094Slling * 11215094Slling * Applications should use zpool_import_props() to import a pool with 11225094Slling * new properties value to be set. 1123789Sahrens */ 1124789Sahrens int 11252082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 11265094Slling char *altroot) 11275094Slling { 11285094Slling nvlist_t *props = NULL; 11295094Slling int ret; 11305094Slling 11315094Slling if (altroot != NULL) { 11325094Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 11335094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 11345094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 11355094Slling newname)); 11365094Slling } 11375094Slling 11385094Slling if (nvlist_add_string(props, 11395094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) { 11405094Slling nvlist_free(props); 11415094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 11425094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 11435094Slling newname)); 11445094Slling } 11455094Slling } 11465094Slling 11476643Seschrock ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 11485094Slling if (props) 11495094Slling nvlist_free(props); 11505094Slling return (ret); 11515094Slling } 11525094Slling 11535094Slling /* 11545094Slling * Import the given pool using the known configuration and a list of 11555094Slling * properties to be set. The configuration should have come from 11565094Slling * zpool_find_import(). The 'newname' parameters control whether the pool 11575094Slling * is imported with a different name. 11585094Slling */ 11595094Slling int 11605094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 11616643Seschrock nvlist_t *props, boolean_t importfaulted) 1162789Sahrens { 11632676Seschrock zfs_cmd_t zc = { 0 }; 1164789Sahrens char *thename; 1165789Sahrens char *origname; 1166789Sahrens int ret; 11675094Slling char errbuf[1024]; 1168789Sahrens 1169789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1170789Sahrens &origname) == 0); 1171789Sahrens 11725094Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 11735094Slling "cannot import pool '%s'"), origname); 11745094Slling 1175789Sahrens if (newname != NULL) { 11762082Seschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 11773237Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 11782082Seschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 11792082Seschrock newname)); 1180789Sahrens thename = (char *)newname; 1181789Sahrens } else { 1182789Sahrens thename = origname; 1183789Sahrens } 1184789Sahrens 11855094Slling if (props) { 11865094Slling uint64_t version; 11875094Slling 11885094Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 11895094Slling &version) == 0); 11905094Slling 11917184Stimh if ((props = zpool_valid_proplist(hdl, origname, 11925320Slling props, version, B_TRUE, errbuf)) == NULL) { 11935094Slling return (-1); 11945320Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 11955320Slling nvlist_free(props); 11965094Slling return (-1); 11975320Slling } 11985094Slling } 1199789Sahrens 1200789Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1201789Sahrens 1202789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 12031544Seschrock &zc.zc_guid) == 0); 1204789Sahrens 12055320Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 12065320Slling nvlist_free(props); 12072082Seschrock return (-1); 12085320Slling } 1209789Sahrens 12106643Seschrock zc.zc_cookie = (uint64_t)importfaulted; 1211789Sahrens ret = 0; 12124543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1213789Sahrens char desc[1024]; 1214789Sahrens if (newname == NULL) 1215789Sahrens (void) snprintf(desc, sizeof (desc), 1216789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1217789Sahrens thename); 1218789Sahrens else 1219789Sahrens (void) snprintf(desc, sizeof (desc), 1220789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1221789Sahrens origname, thename); 1222789Sahrens 1223789Sahrens switch (errno) { 12241544Seschrock case ENOTSUP: 12251544Seschrock /* 12261544Seschrock * Unsupported version. 12271544Seschrock */ 12282082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 12291544Seschrock break; 12301544Seschrock 12312174Seschrock case EINVAL: 12322174Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 12332174Seschrock break; 12342174Seschrock 1235789Sahrens default: 12362082Seschrock (void) zpool_standard_error(hdl, errno, desc); 1237789Sahrens } 1238789Sahrens 1239789Sahrens ret = -1; 1240789Sahrens } else { 1241789Sahrens zpool_handle_t *zhp; 12424543Smarks 1243789Sahrens /* 1244789Sahrens * This should never fail, but play it safe anyway. 1245789Sahrens */ 12462142Seschrock if (zpool_open_silent(hdl, thename, &zhp) != 0) { 12472142Seschrock ret = -1; 12482142Seschrock } else if (zhp != NULL) { 1249789Sahrens ret = zpool_create_zvol_links(zhp); 1250789Sahrens zpool_close(zhp); 1251789Sahrens } 12524543Smarks 1253789Sahrens } 1254789Sahrens 12552676Seschrock zcmd_free_nvlists(&zc); 12565320Slling nvlist_free(props); 12575320Slling 1258789Sahrens return (ret); 1259789Sahrens } 1260789Sahrens 1261789Sahrens /* 1262789Sahrens * Scrub the pool. 1263789Sahrens */ 1264789Sahrens int 1265789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) 1266789Sahrens { 1267789Sahrens zfs_cmd_t zc = { 0 }; 1268789Sahrens char msg[1024]; 12692082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1270789Sahrens 1271789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1272789Sahrens zc.zc_cookie = type; 1273789Sahrens 12744543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0) 1275789Sahrens return (0); 1276789Sahrens 1277789Sahrens (void) snprintf(msg, sizeof (msg), 1278789Sahrens dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1279789Sahrens 12802082Seschrock if (errno == EBUSY) 12812082Seschrock return (zfs_error(hdl, EZFS_RESILVERING, msg)); 12822082Seschrock else 12832082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1284789Sahrens } 1285789Sahrens 12862468Sek110237 /* 12872468Sek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 12882468Sek110237 * spare; but FALSE if its an INUSE spare. 12892468Sek110237 */ 12902082Seschrock static nvlist_t * 12912082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, 12925450Sbrendan boolean_t *avail_spare, boolean_t *l2cache) 12931544Seschrock { 12941544Seschrock uint_t c, children; 12951544Seschrock nvlist_t **child; 12962082Seschrock uint64_t theguid, present; 12971544Seschrock char *path; 12981544Seschrock uint64_t wholedisk = 0; 12992082Seschrock nvlist_t *ret; 13001544Seschrock 13012082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); 13021544Seschrock 13031544Seschrock if (search == NULL && 13041544Seschrock nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { 13051544Seschrock /* 13061544Seschrock * If the device has never been present since import, the only 13071544Seschrock * reliable way to match the vdev is by GUID. 13081544Seschrock */ 13092082Seschrock if (theguid == guid) 13102082Seschrock return (nv); 13111544Seschrock } else if (search != NULL && 13121544Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 13131544Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 13141544Seschrock &wholedisk); 13151544Seschrock if (wholedisk) { 13161544Seschrock /* 13171544Seschrock * For whole disks, the internal path has 's0', but the 13181544Seschrock * path passed in by the user doesn't. 13191544Seschrock */ 13201544Seschrock if (strlen(search) == strlen(path) - 2 && 13211544Seschrock strncmp(search, path, strlen(search)) == 0) 13222082Seschrock return (nv); 13231544Seschrock } else if (strcmp(search, path) == 0) { 13242082Seschrock return (nv); 13251544Seschrock } 13261544Seschrock } 13271544Seschrock 13281544Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13291544Seschrock &child, &children) != 0) 13302082Seschrock return (NULL); 13311544Seschrock 13321544Seschrock for (c = 0; c < children; c++) 13332082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 13345450Sbrendan avail_spare, l2cache)) != NULL) 13351544Seschrock return (ret); 13361544Seschrock 13372082Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 13382082Seschrock &child, &children) == 0) { 13392082Seschrock for (c = 0; c < children; c++) { 13402082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 13415450Sbrendan avail_spare, l2cache)) != NULL) { 13422468Sek110237 *avail_spare = B_TRUE; 13432082Seschrock return (ret); 13442082Seschrock } 13452082Seschrock } 13462082Seschrock } 13472082Seschrock 13485450Sbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 13495450Sbrendan &child, &children) == 0) { 13505450Sbrendan for (c = 0; c < children; c++) { 13515450Sbrendan if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 13525450Sbrendan avail_spare, l2cache)) != NULL) { 13535450Sbrendan *l2cache = B_TRUE; 13545450Sbrendan return (ret); 13555450Sbrendan } 13565450Sbrendan } 13575450Sbrendan } 13585450Sbrendan 13592082Seschrock return (NULL); 13601544Seschrock } 13611544Seschrock 13622082Seschrock nvlist_t * 13635450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 13645450Sbrendan boolean_t *l2cache) 13651544Seschrock { 13661544Seschrock char buf[MAXPATHLEN]; 13671544Seschrock const char *search; 13681544Seschrock char *end; 13691544Seschrock nvlist_t *nvroot; 13701544Seschrock uint64_t guid; 13711544Seschrock 13721613Seschrock guid = strtoull(path, &end, 10); 13731544Seschrock if (guid != 0 && *end == '\0') { 13741544Seschrock search = NULL; 13751544Seschrock } else if (path[0] != '/') { 13761544Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 13771544Seschrock search = buf; 13781544Seschrock } else { 13791544Seschrock search = path; 13801544Seschrock } 13811544Seschrock 13821544Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 13831544Seschrock &nvroot) == 0); 13841544Seschrock 13852468Sek110237 *avail_spare = B_FALSE; 13865450Sbrendan *l2cache = B_FALSE; 13875450Sbrendan return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare, 13885450Sbrendan l2cache)); 13892468Sek110237 } 13902468Sek110237 13912468Sek110237 /* 13925450Sbrendan * Returns TRUE if the given guid corresponds to the given type. 13935450Sbrendan * This is used to check for hot spares (INUSE or not), and level 2 cache 13945450Sbrendan * devices. 13952468Sek110237 */ 13962468Sek110237 static boolean_t 13975450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 13982468Sek110237 { 13995450Sbrendan uint64_t target_guid; 14002468Sek110237 nvlist_t *nvroot; 14015450Sbrendan nvlist_t **list; 14025450Sbrendan uint_t count; 14032468Sek110237 int i; 14042468Sek110237 14052468Sek110237 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 14062468Sek110237 &nvroot) == 0); 14075450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 14085450Sbrendan for (i = 0; i < count; i++) { 14095450Sbrendan verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 14105450Sbrendan &target_guid) == 0); 14115450Sbrendan if (guid == target_guid) 14122468Sek110237 return (B_TRUE); 14132468Sek110237 } 14142468Sek110237 } 14152468Sek110237 14162468Sek110237 return (B_FALSE); 14171544Seschrock } 14181544Seschrock 1419789Sahrens /* 14204451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 14214451Seschrock * ZFS_ONLINE_* flags. 1422789Sahrens */ 1423789Sahrens int 14244451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 14254451Seschrock vdev_state_t *newstate) 1426789Sahrens { 1427789Sahrens zfs_cmd_t zc = { 0 }; 1428789Sahrens char msg[1024]; 14292082Seschrock nvlist_t *tgt; 14305450Sbrendan boolean_t avail_spare, l2cache; 14312082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1432789Sahrens 14331544Seschrock (void) snprintf(msg, sizeof (msg), 14341544Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1435789Sahrens 14361544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 14375450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL) 14382082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1439789Sahrens 14402468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 14412468Sek110237 14425450Sbrendan if (avail_spare || 14435450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 14442082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 14452082Seschrock 14464451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 14474451Seschrock zc.zc_obj = flags; 14484451Seschrock 14494543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 14504451Seschrock return (zpool_standard_error(hdl, errno, msg)); 14514451Seschrock 14524451Seschrock *newstate = zc.zc_cookie; 14534451Seschrock return (0); 1454789Sahrens } 1455789Sahrens 1456789Sahrens /* 1457789Sahrens * Take the specified vdev offline 1458789Sahrens */ 1459789Sahrens int 14604451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1461789Sahrens { 1462789Sahrens zfs_cmd_t zc = { 0 }; 1463789Sahrens char msg[1024]; 14642082Seschrock nvlist_t *tgt; 14655450Sbrendan boolean_t avail_spare, l2cache; 14662082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1467789Sahrens 14681544Seschrock (void) snprintf(msg, sizeof (msg), 14691544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 14701544Seschrock 1471789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 14725450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL) 14732082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 14742082Seschrock 14752468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 14762468Sek110237 14775450Sbrendan if (avail_spare || 14785450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 14792082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 14802082Seschrock 14814451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 14824451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 14831485Slling 14844543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1485789Sahrens return (0); 1486789Sahrens 1487789Sahrens switch (errno) { 14882082Seschrock case EBUSY: 1489789Sahrens 1490789Sahrens /* 1491789Sahrens * There are no other replicas of this device. 1492789Sahrens */ 14932082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 14942082Seschrock 14952082Seschrock default: 14962082Seschrock return (zpool_standard_error(hdl, errno, msg)); 14972082Seschrock } 14982082Seschrock } 1499789Sahrens 15002082Seschrock /* 15014451Seschrock * Mark the given vdev faulted. 15024451Seschrock */ 15034451Seschrock int 15044451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 15054451Seschrock { 15064451Seschrock zfs_cmd_t zc = { 0 }; 15074451Seschrock char msg[1024]; 15084451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 15094451Seschrock 15104451Seschrock (void) snprintf(msg, sizeof (msg), 15114451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 15124451Seschrock 15134451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 15144451Seschrock zc.zc_guid = guid; 15154451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 15164451Seschrock 15174451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 15184451Seschrock return (0); 15194451Seschrock 15204451Seschrock switch (errno) { 15214451Seschrock case EBUSY: 15224451Seschrock 15234451Seschrock /* 15244451Seschrock * There are no other replicas of this device. 15254451Seschrock */ 15264451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 15274451Seschrock 15284451Seschrock default: 15294451Seschrock return (zpool_standard_error(hdl, errno, msg)); 15304451Seschrock } 15314451Seschrock 15324451Seschrock } 15334451Seschrock 15344451Seschrock /* 15354451Seschrock * Mark the given vdev degraded. 15364451Seschrock */ 15374451Seschrock int 15384451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 15394451Seschrock { 15404451Seschrock zfs_cmd_t zc = { 0 }; 15414451Seschrock char msg[1024]; 15424451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 15434451Seschrock 15444451Seschrock (void) snprintf(msg, sizeof (msg), 15454451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 15464451Seschrock 15474451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 15484451Seschrock zc.zc_guid = guid; 15494451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 15504451Seschrock 15514451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 15524451Seschrock return (0); 15534451Seschrock 15544451Seschrock return (zpool_standard_error(hdl, errno, msg)); 15554451Seschrock } 15564451Seschrock 15574451Seschrock /* 15582082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 15592082Seschrock * a hot spare. 15602082Seschrock */ 15612082Seschrock static boolean_t 15622082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 15632082Seschrock { 15642082Seschrock nvlist_t **child; 15652082Seschrock uint_t c, children; 15662082Seschrock char *type; 15672082Seschrock 15682082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 15692082Seschrock &children) == 0) { 15702082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 15712082Seschrock &type) == 0); 15722082Seschrock 15732082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 15742082Seschrock children == 2 && child[which] == tgt) 15752082Seschrock return (B_TRUE); 15762082Seschrock 15772082Seschrock for (c = 0; c < children; c++) 15782082Seschrock if (is_replacing_spare(child[c], tgt, which)) 15792082Seschrock return (B_TRUE); 1580789Sahrens } 15812082Seschrock 15822082Seschrock return (B_FALSE); 1583789Sahrens } 1584789Sahrens 1585789Sahrens /* 1586789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 15874527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 1588789Sahrens */ 1589789Sahrens int 1590789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 1591789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1592789Sahrens { 1593789Sahrens zfs_cmd_t zc = { 0 }; 1594789Sahrens char msg[1024]; 1595789Sahrens int ret; 15962082Seschrock nvlist_t *tgt; 15975450Sbrendan boolean_t avail_spare, l2cache; 15984527Sperrin uint64_t val, is_log; 15997041Seschrock char *path, *newname; 16002082Seschrock nvlist_t **child; 16012082Seschrock uint_t children; 16022082Seschrock nvlist_t *config_root; 16032082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1604789Sahrens 16051544Seschrock if (replacing) 16061544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 16071544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 16081544Seschrock else 16091544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 16101544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 16111544Seschrock 1612789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16135450Sbrendan if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0) 16142082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 16152082Seschrock 16162468Sek110237 if (avail_spare) 16172082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16182082Seschrock 16195450Sbrendan if (l2cache) 16205450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 16215450Sbrendan 16222082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 16232082Seschrock zc.zc_cookie = replacing; 16242082Seschrock 16252082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 16262082Seschrock &child, &children) != 0 || children != 1) { 16272082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16282082Seschrock "new device must be a single disk")); 16292082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 16301544Seschrock } 16312082Seschrock 16322082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 16332082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 16342082Seschrock 16357041Seschrock if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL) 16367041Seschrock return (-1); 16377041Seschrock 16382082Seschrock /* 16392082Seschrock * If the target is a hot spare that has been swapped in, we can only 16402082Seschrock * replace it with another hot spare. 16412082Seschrock */ 16422082Seschrock if (replacing && 16432082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 16447041Seschrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) == NULL || 16452468Sek110237 !avail_spare) && is_replacing_spare(config_root, tgt, 1)) { 16462082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16472082Seschrock "can only be replaced by another hot spare")); 16487041Seschrock free(newname); 16492082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 16502082Seschrock } 16512082Seschrock 16522082Seschrock /* 16532082Seschrock * If we are attempting to replace a spare, it canot be applied to an 16542082Seschrock * already spared device. 16552082Seschrock */ 16562082Seschrock if (replacing && 16572082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 16587041Seschrock zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) != NULL && 16595450Sbrendan avail_spare && is_replacing_spare(config_root, tgt, 0)) { 16602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16612082Seschrock "device has already been replaced with a spare")); 16627041Seschrock free(newname); 16632082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 16642082Seschrock } 1665789Sahrens 16667041Seschrock free(newname); 16677041Seschrock 16685094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 16692082Seschrock return (-1); 1670789Sahrens 16714543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1672789Sahrens 16732676Seschrock zcmd_free_nvlists(&zc); 1674789Sahrens 1675789Sahrens if (ret == 0) 1676789Sahrens return (0); 1677789Sahrens 1678789Sahrens switch (errno) { 16791544Seschrock case ENOTSUP: 1680789Sahrens /* 1681789Sahrens * Can't attach to or replace this type of vdev. 1682789Sahrens */ 16834527Sperrin if (replacing) { 16844527Sperrin is_log = B_FALSE; 16854527Sperrin (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG, 16864527Sperrin &is_log); 16874527Sperrin if (is_log) 16884527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16894527Sperrin "cannot replace a log with a spare")); 16904527Sperrin else 16914527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16924527Sperrin "cannot replace a replacing device")); 16934527Sperrin } else { 16942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16952082Seschrock "can only attach to mirrors and top-level " 16962082Seschrock "disks")); 16974527Sperrin } 16982082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1699789Sahrens break; 1700789Sahrens 17011544Seschrock case EINVAL: 1702789Sahrens /* 1703789Sahrens * The new device must be a single disk. 1704789Sahrens */ 17052082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17062082Seschrock "new device must be a single disk")); 17072082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1708789Sahrens break; 1709789Sahrens 17101544Seschrock case EBUSY: 17112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 17122082Seschrock new_disk); 17132082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1714789Sahrens break; 1715789Sahrens 17161544Seschrock case EOVERFLOW: 1717789Sahrens /* 1718789Sahrens * The new device is too small. 1719789Sahrens */ 17202082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17212082Seschrock "device is too small")); 17222082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1723789Sahrens break; 1724789Sahrens 17251544Seschrock case EDOM: 1726789Sahrens /* 1727789Sahrens * The new device has a different alignment requirement. 1728789Sahrens */ 17292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17302082Seschrock "devices have different sector alignment")); 17312082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1732789Sahrens break; 1733789Sahrens 17341544Seschrock case ENAMETOOLONG: 1735789Sahrens /* 1736789Sahrens * The resulting top-level vdev spec won't fit in the label. 1737789Sahrens */ 17382082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 1739789Sahrens break; 1740789Sahrens 17411544Seschrock default: 17422082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1743789Sahrens } 1744789Sahrens 17452082Seschrock return (-1); 1746789Sahrens } 1747789Sahrens 1748789Sahrens /* 1749789Sahrens * Detach the specified device. 1750789Sahrens */ 1751789Sahrens int 1752789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 1753789Sahrens { 1754789Sahrens zfs_cmd_t zc = { 0 }; 1755789Sahrens char msg[1024]; 17562082Seschrock nvlist_t *tgt; 17575450Sbrendan boolean_t avail_spare, l2cache; 17582082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1759789Sahrens 17601544Seschrock (void) snprintf(msg, sizeof (msg), 17611544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 17621544Seschrock 1763789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17645450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0) 17652082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1766789Sahrens 17672468Sek110237 if (avail_spare) 17682082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 17692082Seschrock 17705450Sbrendan if (l2cache) 17715450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 17725450Sbrendan 17732082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 17742082Seschrock 17754543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 1776789Sahrens return (0); 1777789Sahrens 1778789Sahrens switch (errno) { 1779789Sahrens 17801544Seschrock case ENOTSUP: 1781789Sahrens /* 1782789Sahrens * Can't detach from this type of vdev. 1783789Sahrens */ 17842082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 17852082Seschrock "applicable to mirror and replacing vdevs")); 17862082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 1787789Sahrens break; 1788789Sahrens 17891544Seschrock case EBUSY: 1790789Sahrens /* 1791789Sahrens * There are no other replicas of this device. 1792789Sahrens */ 17932082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 1794789Sahrens break; 1795789Sahrens 17961544Seschrock default: 17972082Seschrock (void) zpool_standard_error(hdl, errno, msg); 17981544Seschrock } 17991544Seschrock 18002082Seschrock return (-1); 18012082Seschrock } 18022082Seschrock 18032082Seschrock /* 18045450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 18055450Sbrendan * and level 2 cache devices. 18062082Seschrock */ 18072082Seschrock int 18082082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 18092082Seschrock { 18102082Seschrock zfs_cmd_t zc = { 0 }; 18112082Seschrock char msg[1024]; 18122082Seschrock nvlist_t *tgt; 18135450Sbrendan boolean_t avail_spare, l2cache; 18142082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 18152082Seschrock 18162082Seschrock (void) snprintf(msg, sizeof (msg), 18172082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 18182082Seschrock 18192082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18205450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0) 18212082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 18222082Seschrock 18235450Sbrendan if (!avail_spare && !l2cache) { 18242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18255450Sbrendan "only inactive hot spares or cache devices " 18265450Sbrendan "can be removed")); 18272082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 18282082Seschrock } 18292082Seschrock 18302082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 18312082Seschrock 18324543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 18332082Seschrock return (0); 18342082Seschrock 18352082Seschrock return (zpool_standard_error(hdl, errno, msg)); 18361544Seschrock } 18371544Seschrock 18381544Seschrock /* 18391544Seschrock * Clear the errors for the pool, or the particular device if specified. 18401544Seschrock */ 18411544Seschrock int 18421544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path) 18431544Seschrock { 18441544Seschrock zfs_cmd_t zc = { 0 }; 18451544Seschrock char msg[1024]; 18462082Seschrock nvlist_t *tgt; 18475450Sbrendan boolean_t avail_spare, l2cache; 18482082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 18491544Seschrock 18501544Seschrock if (path) 18511544Seschrock (void) snprintf(msg, sizeof (msg), 18521544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 18532676Seschrock path); 18541544Seschrock else 18551544Seschrock (void) snprintf(msg, sizeof (msg), 18561544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 18571544Seschrock zhp->zpool_name); 18581544Seschrock 18591544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18602082Seschrock if (path) { 18615450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 18625450Sbrendan &l2cache)) == 0) 18632082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 18642082Seschrock 18655450Sbrendan /* 18665450Sbrendan * Don't allow error clearing for hot spares. Do allow 18675450Sbrendan * error clearing for l2cache devices. 18685450Sbrendan */ 18692468Sek110237 if (avail_spare) 18702082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 18712082Seschrock 18722082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 18732082Seschrock &zc.zc_guid) == 0); 18741544Seschrock } 18751544Seschrock 18764543Smarks if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 18771544Seschrock return (0); 18781544Seschrock 18792082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1880789Sahrens } 1881789Sahrens 18823126Sahl /* 18834451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 18844451Seschrock */ 18854451Seschrock int 18864451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 18874451Seschrock { 18884451Seschrock zfs_cmd_t zc = { 0 }; 18894451Seschrock char msg[1024]; 18904451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 18914451Seschrock 18924451Seschrock (void) snprintf(msg, sizeof (msg), 18934451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 18944451Seschrock guid); 18954451Seschrock 18964451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18974451Seschrock zc.zc_guid = guid; 18984451Seschrock 18994451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 19004451Seschrock return (0); 19014451Seschrock 19024451Seschrock return (zpool_standard_error(hdl, errno, msg)); 19034451Seschrock } 19044451Seschrock 19054451Seschrock /* 19063126Sahl * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 19073126Sahl * hierarchy. 19083126Sahl */ 19093126Sahl int 19103126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 19113126Sahl void *data) 1912789Sahrens { 19133126Sahl libzfs_handle_t *hdl = zhp->zpool_hdl; 19143126Sahl char (*paths)[MAXPATHLEN]; 19153126Sahl size_t size = 4; 19163126Sahl int curr, fd, base, ret = 0; 19173126Sahl DIR *dirp; 19183126Sahl struct dirent *dp; 19193126Sahl struct stat st; 19203126Sahl 19213126Sahl if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 19223126Sahl return (errno == ENOENT ? 0 : -1); 19233126Sahl 19243126Sahl if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 19253126Sahl int err = errno; 19263126Sahl (void) close(base); 19273126Sahl return (err == ENOENT ? 0 : -1); 19283126Sahl } 1929789Sahrens 1930789Sahrens /* 19313126Sahl * Oddly this wasn't a directory -- ignore that failure since we 19323126Sahl * know there are no links lower in the (non-existant) hierarchy. 1933789Sahrens */ 19343126Sahl if (!S_ISDIR(st.st_mode)) { 19353126Sahl (void) close(base); 19363126Sahl return (0); 19373126Sahl } 19383126Sahl 19393126Sahl if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 19403126Sahl (void) close(base); 19413126Sahl return (-1); 1942789Sahrens } 1943789Sahrens 19443126Sahl (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 19453126Sahl curr = 0; 19463126Sahl 19473126Sahl while (curr >= 0) { 19483126Sahl if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 19493126Sahl goto err; 19503126Sahl 19513126Sahl if (S_ISDIR(st.st_mode)) { 19523126Sahl if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 19533126Sahl goto err; 19543126Sahl 19553126Sahl if ((dirp = fdopendir(fd)) == NULL) { 19563126Sahl (void) close(fd); 19573126Sahl goto err; 19583126Sahl } 19593126Sahl 19603126Sahl while ((dp = readdir(dirp)) != NULL) { 19613126Sahl if (dp->d_name[0] == '.') 19623126Sahl continue; 19633126Sahl 19643126Sahl if (curr + 1 == size) { 19653126Sahl paths = zfs_realloc(hdl, paths, 19663126Sahl size * sizeof (paths[0]), 19673126Sahl size * 2 * sizeof (paths[0])); 19683126Sahl if (paths == NULL) { 19693126Sahl (void) closedir(dirp); 19703126Sahl (void) close(fd); 19713126Sahl goto err; 19723126Sahl } 19733126Sahl 19743126Sahl size *= 2; 19753126Sahl } 19763126Sahl 19773126Sahl (void) strlcpy(paths[curr + 1], paths[curr], 19783126Sahl sizeof (paths[curr + 1])); 19793126Sahl (void) strlcat(paths[curr], "/", 19803126Sahl sizeof (paths[curr])); 19813126Sahl (void) strlcat(paths[curr], dp->d_name, 19823126Sahl sizeof (paths[curr])); 19833126Sahl curr++; 19843126Sahl } 19853126Sahl 19863126Sahl (void) closedir(dirp); 19873126Sahl 19883126Sahl } else { 19893126Sahl if ((ret = cb(paths[curr], data)) != 0) 19903126Sahl break; 19913126Sahl } 19923126Sahl 19933126Sahl curr--; 19943126Sahl } 19953126Sahl 19963126Sahl free(paths); 19973126Sahl (void) close(base); 19983126Sahl 19993126Sahl return (ret); 20003126Sahl 20013126Sahl err: 20023126Sahl free(paths); 20033126Sahl (void) close(base); 20043126Sahl return (-1); 20053126Sahl } 20063126Sahl 20073126Sahl typedef struct zvol_cb { 20083126Sahl zpool_handle_t *zcb_pool; 20093126Sahl boolean_t zcb_create; 20103126Sahl } zvol_cb_t; 20113126Sahl 20123126Sahl /*ARGSUSED*/ 20133126Sahl static int 20143126Sahl do_zvol_create(zfs_handle_t *zhp, void *data) 20153126Sahl { 20164657Sahrens int ret = 0; 20173126Sahl 20184657Sahrens if (ZFS_IS_VOLUME(zhp)) { 20193126Sahl (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 20204657Sahrens ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 20214657Sahrens } 20223126Sahl 20234657Sahrens if (ret == 0) 20244657Sahrens ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 2025789Sahrens 2026789Sahrens zfs_close(zhp); 20273126Sahl 2028789Sahrens return (ret); 2029789Sahrens } 2030789Sahrens 2031789Sahrens /* 2032789Sahrens * Iterate over all zvols in the pool and make any necessary minor nodes. 2033789Sahrens */ 2034789Sahrens int 2035789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp) 2036789Sahrens { 2037789Sahrens zfs_handle_t *zfp; 2038789Sahrens int ret; 2039789Sahrens 2040789Sahrens /* 2041789Sahrens * If the pool is unavailable, just return success. 2042789Sahrens */ 20432082Seschrock if ((zfp = make_dataset_handle(zhp->zpool_hdl, 20442082Seschrock zhp->zpool_name)) == NULL) 2045789Sahrens return (0); 2046789Sahrens 20474657Sahrens ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 2048789Sahrens 2049789Sahrens zfs_close(zfp); 2050789Sahrens return (ret); 2051789Sahrens } 2052789Sahrens 20533126Sahl static int 20543126Sahl do_zvol_remove(const char *dataset, void *data) 20553126Sahl { 20563126Sahl zpool_handle_t *zhp = data; 20573126Sahl 20583126Sahl return (zvol_remove_link(zhp->zpool_hdl, dataset)); 20593126Sahl } 20603126Sahl 2061789Sahrens /* 20623126Sahl * Iterate over all zvols in the pool and remove any minor nodes. We iterate 20633126Sahl * by examining the /dev links so that a corrupted pool doesn't impede this 20643126Sahl * operation. 2065789Sahrens */ 2066789Sahrens int 2067789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp) 2068789Sahrens { 20693126Sahl return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 2070789Sahrens } 20711354Seschrock 20721354Seschrock /* 20731354Seschrock * Convert from a devid string to a path. 20741354Seschrock */ 20751354Seschrock static char * 20761354Seschrock devid_to_path(char *devid_str) 20771354Seschrock { 20781354Seschrock ddi_devid_t devid; 20791354Seschrock char *minor; 20801354Seschrock char *path; 20811354Seschrock devid_nmlist_t *list = NULL; 20821354Seschrock int ret; 20831354Seschrock 20841354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 20851354Seschrock return (NULL); 20861354Seschrock 20871354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 20881354Seschrock 20891354Seschrock devid_str_free(minor); 20901354Seschrock devid_free(devid); 20911354Seschrock 20921354Seschrock if (ret != 0) 20931354Seschrock return (NULL); 20941354Seschrock 20952082Seschrock if ((path = strdup(list[0].devname)) == NULL) 20962082Seschrock return (NULL); 20972082Seschrock 20981354Seschrock devid_free_nmlist(list); 20991354Seschrock 21001354Seschrock return (path); 21011354Seschrock } 21021354Seschrock 21031354Seschrock /* 21041354Seschrock * Convert from a path to a devid string. 21051354Seschrock */ 21061354Seschrock static char * 21071354Seschrock path_to_devid(const char *path) 21081354Seschrock { 21091354Seschrock int fd; 21101354Seschrock ddi_devid_t devid; 21111354Seschrock char *minor, *ret; 21121354Seschrock 21131354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 21141354Seschrock return (NULL); 21151354Seschrock 21161354Seschrock minor = NULL; 21171354Seschrock ret = NULL; 21181354Seschrock if (devid_get(fd, &devid) == 0) { 21191354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 21201354Seschrock ret = devid_str_encode(devid, minor); 21211354Seschrock if (minor != NULL) 21221354Seschrock devid_str_free(minor); 21231354Seschrock devid_free(devid); 21241354Seschrock } 21251354Seschrock (void) close(fd); 21261354Seschrock 21271354Seschrock return (ret); 21281354Seschrock } 21291354Seschrock 21301354Seschrock /* 21311354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 21321354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 21331354Seschrock * type 'zpool status', and we'll display the correct information anyway. 21341354Seschrock */ 21351354Seschrock static void 21361354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 21371354Seschrock { 21381354Seschrock zfs_cmd_t zc = { 0 }; 21391354Seschrock 21401354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21412676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 21421354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 21431544Seschrock &zc.zc_guid) == 0); 21441354Seschrock 21452082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 21461354Seschrock } 21471354Seschrock 21481354Seschrock /* 21491354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 21501354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 21511354Seschrock * We also check if this is a whole disk, in which case we strip off the 21521354Seschrock * trailing 's0' slice name. 21531354Seschrock * 21541354Seschrock * This routine is also responsible for identifying when disks have been 21551354Seschrock * reconfigured in a new location. The kernel will have opened the device by 21561354Seschrock * devid, but the path will still refer to the old location. To catch this, we 21571354Seschrock * first do a path -> devid translation (which is fast for the common case). If 21581354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 21591354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 21601354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 21611354Seschrock * of these checks. 21621354Seschrock */ 21631354Seschrock char * 21642082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 21651354Seschrock { 21661354Seschrock char *path, *devid; 21671544Seschrock uint64_t value; 21681544Seschrock char buf[64]; 21694451Seschrock vdev_stat_t *vs; 21704451Seschrock uint_t vsc; 21711354Seschrock 21721544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 21731544Seschrock &value) == 0) { 21741544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 21751544Seschrock &value) == 0); 21762856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 21772856Snd150628 (u_longlong_t)value); 21781544Seschrock path = buf; 21791544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 21801354Seschrock 21814451Seschrock /* 21824451Seschrock * If the device is dead (faulted, offline, etc) then don't 21834451Seschrock * bother opening it. Otherwise we may be forcing the user to 21844451Seschrock * open a misbehaving device, which can have undesirable 21854451Seschrock * effects. 21864451Seschrock */ 21874451Seschrock if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 21884451Seschrock (uint64_t **)&vs, &vsc) != 0 || 21894451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 21904451Seschrock zhp != NULL && 21911354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 21921354Seschrock /* 21931354Seschrock * Determine if the current path is correct. 21941354Seschrock */ 21951354Seschrock char *newdevid = path_to_devid(path); 21961354Seschrock 21971354Seschrock if (newdevid == NULL || 21981354Seschrock strcmp(devid, newdevid) != 0) { 21991354Seschrock char *newpath; 22001354Seschrock 22011354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 22021354Seschrock /* 22031354Seschrock * Update the path appropriately. 22041354Seschrock */ 22051354Seschrock set_path(zhp, nv, newpath); 22062082Seschrock if (nvlist_add_string(nv, 22072082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 22082082Seschrock verify(nvlist_lookup_string(nv, 22092082Seschrock ZPOOL_CONFIG_PATH, 22102082Seschrock &path) == 0); 22111354Seschrock free(newpath); 22121354Seschrock } 22131354Seschrock } 22141354Seschrock 22152082Seschrock if (newdevid) 22162082Seschrock devid_str_free(newdevid); 22171354Seschrock } 22181354Seschrock 22191354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 22201354Seschrock path += 9; 22211354Seschrock 22221354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 22231544Seschrock &value) == 0 && value) { 22242082Seschrock char *tmp = zfs_strdup(hdl, path); 22252082Seschrock if (tmp == NULL) 22262082Seschrock return (NULL); 22271354Seschrock tmp[strlen(path) - 2] = '\0'; 22281354Seschrock return (tmp); 22291354Seschrock } 22301354Seschrock } else { 22311354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 22322082Seschrock 22332082Seschrock /* 22342082Seschrock * If it's a raidz device, we need to stick in the parity level. 22352082Seschrock */ 22362082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 22372082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 22382082Seschrock &value) == 0); 22392082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 22402856Snd150628 (u_longlong_t)value); 22412082Seschrock path = buf; 22422082Seschrock } 22431354Seschrock } 22441354Seschrock 22452082Seschrock return (zfs_strdup(hdl, path)); 22461354Seschrock } 22471544Seschrock 22481544Seschrock static int 22491544Seschrock zbookmark_compare(const void *a, const void *b) 22501544Seschrock { 22511544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 22521544Seschrock } 22531544Seschrock 22541544Seschrock /* 22551544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 22561544Seschrock * caller. 22571544Seschrock */ 22581544Seschrock int 22593444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 22601544Seschrock { 22611544Seschrock zfs_cmd_t zc = { 0 }; 22621544Seschrock uint64_t count; 22632676Seschrock zbookmark_t *zb = NULL; 22643444Sek110237 int i; 22651544Seschrock 22661544Seschrock /* 22671544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 22681544Seschrock * has increased, allocate more space and continue until we get the 22691544Seschrock * entire list. 22701544Seschrock */ 22711544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 22721544Seschrock &count) == 0); 22734820Sek110237 if (count == 0) 22744820Sek110237 return (0); 22752676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 22762856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 22772082Seschrock return (-1); 22782676Seschrock zc.zc_nvlist_dst_size = count; 22791544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 22801544Seschrock for (;;) { 22812082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 22822082Seschrock &zc) != 0) { 22832676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 22841544Seschrock if (errno == ENOMEM) { 22853823Svb160487 count = zc.zc_nvlist_dst_size; 22862676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 22873823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 22883823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 22892082Seschrock return (-1); 22901544Seschrock } else { 22911544Seschrock return (-1); 22921544Seschrock } 22931544Seschrock } else { 22941544Seschrock break; 22951544Seschrock } 22961544Seschrock } 22971544Seschrock 22981544Seschrock /* 22991544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 23001544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 23012676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 23021544Seschrock * _not_ copied as part of the process. So we point the start of our 23031544Seschrock * array appropriate and decrement the total number of elements. 23041544Seschrock */ 23052676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 23062676Seschrock zc.zc_nvlist_dst_size; 23072676Seschrock count -= zc.zc_nvlist_dst_size; 23081544Seschrock 23091544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 23101544Seschrock 23113444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 23121544Seschrock 23131544Seschrock /* 23143444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 23151544Seschrock */ 23161544Seschrock for (i = 0; i < count; i++) { 23171544Seschrock nvlist_t *nv; 23181544Seschrock 23193700Sek110237 /* ignoring zb_blkid and zb_level for now */ 23203700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 23213700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 23221544Seschrock continue; 23231544Seschrock 23243444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 23253444Sek110237 goto nomem; 23263444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 23273444Sek110237 zb[i].zb_objset) != 0) { 23283444Sek110237 nvlist_free(nv); 23292082Seschrock goto nomem; 23303444Sek110237 } 23313444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 23323444Sek110237 zb[i].zb_object) != 0) { 23333444Sek110237 nvlist_free(nv); 23343444Sek110237 goto nomem; 23351544Seschrock } 23363444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 23373444Sek110237 nvlist_free(nv); 23383444Sek110237 goto nomem; 23393444Sek110237 } 23403444Sek110237 nvlist_free(nv); 23411544Seschrock } 23421544Seschrock 23433265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 23441544Seschrock return (0); 23452082Seschrock 23462082Seschrock nomem: 23472676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 23482082Seschrock return (no_memory(zhp->zpool_hdl)); 23491544Seschrock } 23501760Seschrock 23511760Seschrock /* 23521760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 23531760Seschrock */ 23541760Seschrock int 23555094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 23561760Seschrock { 23571760Seschrock zfs_cmd_t zc = { 0 }; 23582082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 23591760Seschrock 23601760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 23615094Slling zc.zc_cookie = new_version; 23625094Slling 23634543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 23643237Slling return (zpool_standard_error_fmt(hdl, errno, 23652082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 23662082Seschrock zhp->zpool_name)); 23671760Seschrock return (0); 23681760Seschrock } 23692926Sek110237 23704988Sek110237 void 23714988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 23724988Sek110237 char *history_str) 23734988Sek110237 { 23744988Sek110237 int i; 23754988Sek110237 23764988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 23774988Sek110237 for (i = 1; i < argc; i++) { 23784988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 23794988Sek110237 HIS_MAX_RECORD_LEN) 23804988Sek110237 break; 23814988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 23824988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 23834988Sek110237 } 23844988Sek110237 } 23854988Sek110237 23862926Sek110237 /* 23874988Sek110237 * Stage command history for logging. 23882926Sek110237 */ 23894988Sek110237 int 23904988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 23912926Sek110237 { 23924988Sek110237 if (history_str == NULL) 23934988Sek110237 return (EINVAL); 23944988Sek110237 23954988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 23964988Sek110237 return (EINVAL); 23972926Sek110237 23984715Sek110237 if (hdl->libzfs_log_str != NULL) 23994543Smarks free(hdl->libzfs_log_str); 24002926Sek110237 24014988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 24024988Sek110237 return (no_memory(hdl)); 24034543Smarks 24044988Sek110237 return (0); 24052926Sek110237 } 24062926Sek110237 24072926Sek110237 /* 24082926Sek110237 * Perform ioctl to get some command history of a pool. 24092926Sek110237 * 24102926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 24112926Sek110237 * logical offset of the history buffer to start reading from. 24122926Sek110237 * 24132926Sek110237 * Upon return, 'off' is the next logical offset to read from and 24142926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 24152926Sek110237 */ 24162926Sek110237 static int 24172926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 24182926Sek110237 { 24192926Sek110237 zfs_cmd_t zc = { 0 }; 24202926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 24212926Sek110237 24222926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 24232926Sek110237 24242926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 24252926Sek110237 zc.zc_history_len = *len; 24262926Sek110237 zc.zc_history_offset = *off; 24272926Sek110237 24282926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 24292926Sek110237 switch (errno) { 24302926Sek110237 case EPERM: 24313237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 24323237Slling dgettext(TEXT_DOMAIN, 24332926Sek110237 "cannot show history for pool '%s'"), 24342926Sek110237 zhp->zpool_name)); 24352926Sek110237 case ENOENT: 24363237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 24372926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 24382926Sek110237 "'%s'"), zhp->zpool_name)); 24393863Sek110237 case ENOTSUP: 24403863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 24413863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 24423863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 24432926Sek110237 default: 24443237Slling return (zpool_standard_error_fmt(hdl, errno, 24452926Sek110237 dgettext(TEXT_DOMAIN, 24462926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 24472926Sek110237 } 24482926Sek110237 } 24492926Sek110237 24502926Sek110237 *len = zc.zc_history_len; 24512926Sek110237 *off = zc.zc_history_offset; 24522926Sek110237 24532926Sek110237 return (0); 24542926Sek110237 } 24552926Sek110237 24562926Sek110237 /* 24572926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 24582926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 24592926Sek110237 * processed as there wasn't a complete record. 24602926Sek110237 */ 24612926Sek110237 static int 24622926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 24632926Sek110237 nvlist_t ***records, uint_t *numrecords) 24642926Sek110237 { 24652926Sek110237 uint64_t reclen; 24662926Sek110237 nvlist_t *nv; 24672926Sek110237 int i; 24682926Sek110237 24692926Sek110237 while (bytes_read > sizeof (reclen)) { 24702926Sek110237 24712926Sek110237 /* get length of packed record (stored as little endian) */ 24722926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 24732926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 24742926Sek110237 24752926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 24762926Sek110237 break; 24772926Sek110237 24782926Sek110237 /* unpack record */ 24792926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 24802926Sek110237 return (ENOMEM); 24812926Sek110237 bytes_read -= sizeof (reclen) + reclen; 24822926Sek110237 buf += sizeof (reclen) + reclen; 24832926Sek110237 24842926Sek110237 /* add record to nvlist array */ 24852926Sek110237 (*numrecords)++; 24862926Sek110237 if (ISP2(*numrecords + 1)) { 24872926Sek110237 *records = realloc(*records, 24882926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 24892926Sek110237 } 24902926Sek110237 (*records)[*numrecords - 1] = nv; 24912926Sek110237 } 24922926Sek110237 24932926Sek110237 *leftover = bytes_read; 24942926Sek110237 return (0); 24952926Sek110237 } 24962926Sek110237 24972926Sek110237 #define HIS_BUF_LEN (128*1024) 24982926Sek110237 24992926Sek110237 /* 25002926Sek110237 * Retrieve the command history of a pool. 25012926Sek110237 */ 25022926Sek110237 int 25032926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 25042926Sek110237 { 25052926Sek110237 char buf[HIS_BUF_LEN]; 25062926Sek110237 uint64_t off = 0; 25072926Sek110237 nvlist_t **records = NULL; 25082926Sek110237 uint_t numrecords = 0; 25092926Sek110237 int err, i; 25102926Sek110237 25112926Sek110237 do { 25122926Sek110237 uint64_t bytes_read = sizeof (buf); 25132926Sek110237 uint64_t leftover; 25142926Sek110237 25152926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 25162926Sek110237 break; 25172926Sek110237 25182926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 25192926Sek110237 if (!bytes_read) 25202926Sek110237 break; 25212926Sek110237 25222926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 25232926Sek110237 &leftover, &records, &numrecords)) != 0) 25242926Sek110237 break; 25252926Sek110237 off -= leftover; 25262926Sek110237 25272926Sek110237 /* CONSTCOND */ 25282926Sek110237 } while (1); 25292926Sek110237 25302926Sek110237 if (!err) { 25312926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 25322926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 25332926Sek110237 records, numrecords) == 0); 25342926Sek110237 } 25352926Sek110237 for (i = 0; i < numrecords; i++) 25362926Sek110237 nvlist_free(records[i]); 25372926Sek110237 free(records); 25382926Sek110237 25392926Sek110237 return (err); 25402926Sek110237 } 25413444Sek110237 25423444Sek110237 void 25433444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 25443444Sek110237 char *pathname, size_t len) 25453444Sek110237 { 25463444Sek110237 zfs_cmd_t zc = { 0 }; 25473444Sek110237 boolean_t mounted = B_FALSE; 25483444Sek110237 char *mntpnt = NULL; 25493444Sek110237 char dsname[MAXNAMELEN]; 25503444Sek110237 25513444Sek110237 if (dsobj == 0) { 25523444Sek110237 /* special case for the MOS */ 25533444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 25543444Sek110237 return; 25553444Sek110237 } 25563444Sek110237 25573444Sek110237 /* get the dataset's name */ 25583444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25593444Sek110237 zc.zc_obj = dsobj; 25603444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 25613444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 25623444Sek110237 /* just write out a path of two object numbers */ 25633444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 25643444Sek110237 dsobj, obj); 25653444Sek110237 return; 25663444Sek110237 } 25673444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 25683444Sek110237 25693444Sek110237 /* find out if the dataset is mounted */ 25703444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 25713444Sek110237 25723444Sek110237 /* get the corrupted object's path */ 25733444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 25743444Sek110237 zc.zc_obj = obj; 25753444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 25763444Sek110237 &zc) == 0) { 25773444Sek110237 if (mounted) { 25783444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 25793444Sek110237 zc.zc_value); 25803444Sek110237 } else { 25813444Sek110237 (void) snprintf(pathname, len, "%s:%s", 25823444Sek110237 dsname, zc.zc_value); 25833444Sek110237 } 25843444Sek110237 } else { 25853444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 25863444Sek110237 } 25873444Sek110237 free(mntpnt); 25883444Sek110237 } 25893912Slling 25904276Staylor #define RDISK_ROOT "/dev/rdsk" 25914276Staylor #define BACKUP_SLICE "s2" 25924276Staylor /* 25934276Staylor * Don't start the slice at the default block of 34; many storage 25944276Staylor * devices will use a stripe width of 128k, so start there instead. 25954276Staylor */ 25964276Staylor #define NEW_START_BLOCK 256 25974276Staylor 25984276Staylor /* 25997042Sgw25295 * Read the EFI label from the config, if a label does not exist then 26007042Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 26017042Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 26027042Sgw25295 * partition. 26037042Sgw25295 */ 26047042Sgw25295 static int 26057042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 26067042Sgw25295 { 26077042Sgw25295 char *path; 26087042Sgw25295 int fd; 26097042Sgw25295 char diskname[MAXPATHLEN]; 26107042Sgw25295 int err = -1; 26117042Sgw25295 26127042Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 26137042Sgw25295 return (err); 26147042Sgw25295 26157042Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 26167042Sgw25295 strrchr(path, '/')); 26177042Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 26187042Sgw25295 struct dk_gpt *vtoc; 26197042Sgw25295 26207042Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 26217042Sgw25295 if (sb != NULL) 26227042Sgw25295 *sb = vtoc->efi_parts[0].p_start; 26237042Sgw25295 efi_free(vtoc); 26247042Sgw25295 } 26257042Sgw25295 (void) close(fd); 26267042Sgw25295 } 26277042Sgw25295 return (err); 26287042Sgw25295 } 26297042Sgw25295 26307042Sgw25295 /* 26314276Staylor * determine where a partition starts on a disk in the current 26324276Staylor * configuration 26334276Staylor */ 26344276Staylor static diskaddr_t 26354276Staylor find_start_block(nvlist_t *config) 26364276Staylor { 26374276Staylor nvlist_t **child; 26384276Staylor uint_t c, children; 26394276Staylor diskaddr_t sb = MAXOFFSET_T; 26404276Staylor uint64_t wholedisk; 26414276Staylor 26424276Staylor if (nvlist_lookup_nvlist_array(config, 26434276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 26444276Staylor if (nvlist_lookup_uint64(config, 26454276Staylor ZPOOL_CONFIG_WHOLE_DISK, 26464276Staylor &wholedisk) != 0 || !wholedisk) { 26474276Staylor return (MAXOFFSET_T); 26484276Staylor } 26497042Sgw25295 if (read_efi_label(config, &sb) < 0) 26507042Sgw25295 sb = MAXOFFSET_T; 26514276Staylor return (sb); 26524276Staylor } 26534276Staylor 26544276Staylor for (c = 0; c < children; c++) { 26554276Staylor sb = find_start_block(child[c]); 26564276Staylor if (sb != MAXOFFSET_T) { 26574276Staylor return (sb); 26584276Staylor } 26594276Staylor } 26604276Staylor return (MAXOFFSET_T); 26614276Staylor } 26624276Staylor 26634276Staylor /* 26644276Staylor * Label an individual disk. The name provided is the short name, 26654276Staylor * stripped of any leading /dev path. 26664276Staylor */ 26674276Staylor int 26684276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 26694276Staylor { 26704276Staylor char path[MAXPATHLEN]; 26714276Staylor struct dk_gpt *vtoc; 26724276Staylor int fd; 26734276Staylor size_t resv = EFI_MIN_RESV_SIZE; 26744276Staylor uint64_t slice_size; 26754276Staylor diskaddr_t start_block; 26764276Staylor char errbuf[1024]; 26774276Staylor 26786289Smmusante /* prepare an error message just in case */ 26796289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 26806289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 26816289Smmusante 26824276Staylor if (zhp) { 26834276Staylor nvlist_t *nvroot; 26844276Staylor 26854276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 26864276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 26874276Staylor 26884276Staylor if (zhp->zpool_start_block == 0) 26894276Staylor start_block = find_start_block(nvroot); 26904276Staylor else 26914276Staylor start_block = zhp->zpool_start_block; 26924276Staylor zhp->zpool_start_block = start_block; 26934276Staylor } else { 26944276Staylor /* new pool */ 26954276Staylor start_block = NEW_START_BLOCK; 26964276Staylor } 26974276Staylor 26984276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 26994276Staylor BACKUP_SLICE); 27004276Staylor 27014276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 27024276Staylor /* 27034276Staylor * This shouldn't happen. We've long since verified that this 27044276Staylor * is a valid device. 27054276Staylor */ 27066289Smmusante zfs_error_aux(hdl, 27076289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 27084276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 27094276Staylor } 27104276Staylor 27114276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 27124276Staylor /* 27134276Staylor * The only way this can fail is if we run out of memory, or we 27144276Staylor * were unable to read the disk's capacity 27154276Staylor */ 27164276Staylor if (errno == ENOMEM) 27174276Staylor (void) no_memory(hdl); 27184276Staylor 27194276Staylor (void) close(fd); 27206289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27216289Smmusante "unable to read disk capacity"), name); 27224276Staylor 27234276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 27244276Staylor } 27254276Staylor 27264276Staylor slice_size = vtoc->efi_last_u_lba + 1; 27274276Staylor slice_size -= EFI_MIN_RESV_SIZE; 27284276Staylor if (start_block == MAXOFFSET_T) 27294276Staylor start_block = NEW_START_BLOCK; 27304276Staylor slice_size -= start_block; 27314276Staylor 27324276Staylor vtoc->efi_parts[0].p_start = start_block; 27334276Staylor vtoc->efi_parts[0].p_size = slice_size; 27344276Staylor 27354276Staylor /* 27364276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 27374276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 27384276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 27394276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 27404276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 27414276Staylor * can get, in the absence of V_OTHER. 27424276Staylor */ 27434276Staylor vtoc->efi_parts[0].p_tag = V_USR; 27444276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 27454276Staylor 27464276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 27474276Staylor vtoc->efi_parts[8].p_size = resv; 27484276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 27494276Staylor 27504276Staylor if (efi_write(fd, vtoc) != 0) { 27514276Staylor /* 27524276Staylor * Some block drivers (like pcata) may not support EFI 27534276Staylor * GPT labels. Print out a helpful error message dir- 27544276Staylor * ecting the user to manually label the disk and give 27554276Staylor * a specific slice. 27564276Staylor */ 27574276Staylor (void) close(fd); 27584276Staylor efi_free(vtoc); 27594276Staylor 27604276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27616289Smmusante "try using fdisk(1M) and then provide a specific slice")); 27624276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 27634276Staylor } 27644276Staylor 27654276Staylor (void) close(fd); 27664276Staylor efi_free(vtoc); 27674276Staylor return (0); 27684276Staylor } 27696423Sgw25295 27706423Sgw25295 static boolean_t 27716423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 27726423Sgw25295 { 27736423Sgw25295 char *type; 27746423Sgw25295 nvlist_t **child; 27756423Sgw25295 uint_t children, c; 27766423Sgw25295 27776423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 27786423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 27796423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 27806423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 27816423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 27826423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27836423Sgw25295 "vdev type '%s' is not supported"), type); 27846423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 27856423Sgw25295 return (B_FALSE); 27866423Sgw25295 } 27876423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 27886423Sgw25295 &child, &children) == 0) { 27896423Sgw25295 for (c = 0; c < children; c++) { 27906423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 27916423Sgw25295 return (B_FALSE); 27926423Sgw25295 } 27936423Sgw25295 } 27946423Sgw25295 return (B_TRUE); 27956423Sgw25295 } 27966423Sgw25295 27976423Sgw25295 /* 27986423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 27996423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 28006423Sgw25295 */ 28016423Sgw25295 int 28026423Sgw25295 zvol_check_dump_config(char *arg) 28036423Sgw25295 { 28046423Sgw25295 zpool_handle_t *zhp = NULL; 28056423Sgw25295 nvlist_t *config, *nvroot; 28066423Sgw25295 char *p, *volname; 28076423Sgw25295 nvlist_t **top; 28086423Sgw25295 uint_t toplevels; 28096423Sgw25295 libzfs_handle_t *hdl; 28106423Sgw25295 char errbuf[1024]; 28116423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 28126423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 28136423Sgw25295 int ret = 1; 28146423Sgw25295 28156423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 28166423Sgw25295 return (-1); 28176423Sgw25295 } 28186423Sgw25295 28196423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28206423Sgw25295 "dump is not supported on device '%s'"), arg); 28216423Sgw25295 28226423Sgw25295 if ((hdl = libzfs_init()) == NULL) 28236423Sgw25295 return (1); 28246423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 28256423Sgw25295 28266423Sgw25295 volname = arg + pathlen; 28276423Sgw25295 28286423Sgw25295 /* check the configuration of the pool */ 28296423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 28306423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28316423Sgw25295 "malformed dataset name")); 28326423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 28336423Sgw25295 return (1); 28346423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 28356423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28366423Sgw25295 "dataset name is too long")); 28376423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 28386423Sgw25295 return (1); 28396423Sgw25295 } else { 28406423Sgw25295 (void) strncpy(poolname, volname, p - volname); 28416423Sgw25295 poolname[p - volname] = '\0'; 28426423Sgw25295 } 28436423Sgw25295 28446423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 28456423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28466423Sgw25295 "could not open pool '%s'"), poolname); 28476423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 28486423Sgw25295 goto out; 28496423Sgw25295 } 28506423Sgw25295 config = zpool_get_config(zhp, NULL); 28516423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 28526423Sgw25295 &nvroot) != 0) { 28536423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28546423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 28556423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 28566423Sgw25295 goto out; 28576423Sgw25295 } 28586423Sgw25295 28596423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 28606423Sgw25295 &top, &toplevels) == 0); 28616423Sgw25295 if (toplevels != 1) { 28626423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28636423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 28646423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 28656423Sgw25295 goto out; 28666423Sgw25295 } 28676423Sgw25295 28686423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 28696423Sgw25295 goto out; 28706423Sgw25295 } 28716423Sgw25295 ret = 0; 28726423Sgw25295 28736423Sgw25295 out: 28746423Sgw25295 if (zhp) 28756423Sgw25295 zpool_close(zhp); 28766423Sgw25295 libzfs_fini(hdl); 28776423Sgw25295 return (ret); 28786423Sgw25295 } 2879