1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 212082Seschrock 22789Sahrens /* 236289Smmusante * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 273126Sahl #include <alloca.h> 28789Sahrens #include <assert.h> 29789Sahrens #include <ctype.h> 30789Sahrens #include <errno.h> 31789Sahrens #include <devid.h> 323126Sahl #include <dirent.h> 33789Sahrens #include <fcntl.h> 34789Sahrens #include <libintl.h> 35789Sahrens #include <stdio.h> 36789Sahrens #include <stdlib.h> 373126Sahl #include <strings.h> 38789Sahrens #include <unistd.h> 397184Stimh #include <zone.h> 404276Staylor #include <sys/efi_partition.h> 414276Staylor #include <sys/vtoc.h> 42789Sahrens #include <sys/zfs_ioctl.h> 431544Seschrock #include <sys/zio.h> 442926Sek110237 #include <strings.h> 45789Sahrens 46789Sahrens #include "zfs_namecheck.h" 473912Slling #include "zfs_prop.h" 48789Sahrens #include "libzfs_impl.h" 49789Sahrens 507042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 515094Slling 527965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64) 537965SGeorge.Wilson@Sun.COM #define BOOTCMD "installgrub(1M)" 547965SGeorge.Wilson@Sun.COM #else 557965SGeorge.Wilson@Sun.COM #define BOOTCMD "installboot(1M)" 567965SGeorge.Wilson@Sun.COM #endif 577965SGeorge.Wilson@Sun.COM 585094Slling /* 595094Slling * ==================================================================== 605094Slling * zpool property functions 615094Slling * ==================================================================== 625094Slling */ 635094Slling 645094Slling static int 655094Slling zpool_get_all_props(zpool_handle_t *zhp) 665094Slling { 675094Slling zfs_cmd_t zc = { 0 }; 685094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 695094Slling 705094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 715094Slling 725094Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 735094Slling return (-1); 745094Slling 755094Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 765094Slling if (errno == ENOMEM) { 775094Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 785094Slling zcmd_free_nvlists(&zc); 795094Slling return (-1); 805094Slling } 815094Slling } else { 825094Slling zcmd_free_nvlists(&zc); 835094Slling return (-1); 845094Slling } 855094Slling } 865094Slling 875094Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 885094Slling zcmd_free_nvlists(&zc); 895094Slling return (-1); 905094Slling } 915094Slling 925094Slling zcmd_free_nvlists(&zc); 935094Slling 945094Slling return (0); 955094Slling } 965094Slling 975094Slling static int 985094Slling zpool_props_refresh(zpool_handle_t *zhp) 995094Slling { 1005094Slling nvlist_t *old_props; 1015094Slling 1025094Slling old_props = zhp->zpool_props; 1035094Slling 1045094Slling if (zpool_get_all_props(zhp) != 0) 1055094Slling return (-1); 1065094Slling 1075094Slling nvlist_free(old_props); 1085094Slling return (0); 1095094Slling } 1105094Slling 1115094Slling static char * 1125094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 1135094Slling zprop_source_t *src) 1145094Slling { 1155094Slling nvlist_t *nv, *nvl; 1165094Slling uint64_t ival; 1175094Slling char *value; 1185094Slling zprop_source_t source; 1195094Slling 1205094Slling nvl = zhp->zpool_props; 1215094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1225094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 1235094Slling source = ival; 1245094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1255094Slling } else { 1265094Slling source = ZPROP_SRC_DEFAULT; 1275094Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 1285094Slling value = "-"; 1295094Slling } 1305094Slling 1315094Slling if (src) 1325094Slling *src = source; 1335094Slling 1345094Slling return (value); 1355094Slling } 1365094Slling 1375094Slling uint64_t 1385094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 1395094Slling { 1405094Slling nvlist_t *nv, *nvl; 1415094Slling uint64_t value; 1425094Slling zprop_source_t source; 1435094Slling 1447294Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 1457294Sperrin /* 1467294Sperrin * zpool_get_all_props() has most likely failed because 1477294Sperrin * the pool is faulted, but if all we need is the top level 1487294Sperrin * vdev's guid then get it from the zhp config nvlist. 1497294Sperrin */ 1507294Sperrin if ((prop == ZPOOL_PROP_GUID) && 1517294Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 1527294Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 1537294Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 1547294Sperrin == 0)) { 1557294Sperrin return (value); 1567294Sperrin } 1575094Slling return (zpool_prop_default_numeric(prop)); 1587294Sperrin } 1595094Slling 1605094Slling nvl = zhp->zpool_props; 1615094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1625094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 1635094Slling source = value; 1645094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1655094Slling } else { 1665094Slling source = ZPROP_SRC_DEFAULT; 1675094Slling value = zpool_prop_default_numeric(prop); 1685094Slling } 1695094Slling 1705094Slling if (src) 1715094Slling *src = source; 1725094Slling 1735094Slling return (value); 1745094Slling } 1755094Slling 1765094Slling /* 1775094Slling * Map VDEV STATE to printed strings. 1785094Slling */ 1795094Slling char * 1805094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 1815094Slling { 1825094Slling switch (state) { 1835094Slling case VDEV_STATE_CLOSED: 1845094Slling case VDEV_STATE_OFFLINE: 1855094Slling return (gettext("OFFLINE")); 1865094Slling case VDEV_STATE_REMOVED: 1875094Slling return (gettext("REMOVED")); 1885094Slling case VDEV_STATE_CANT_OPEN: 1897294Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 1905094Slling return (gettext("FAULTED")); 1915094Slling else 1925094Slling return (gettext("UNAVAIL")); 1935094Slling case VDEV_STATE_FAULTED: 1945094Slling return (gettext("FAULTED")); 1955094Slling case VDEV_STATE_DEGRADED: 1965094Slling return (gettext("DEGRADED")); 1975094Slling case VDEV_STATE_HEALTHY: 1985094Slling return (gettext("ONLINE")); 1995094Slling } 2005094Slling 2015094Slling return (gettext("UNKNOWN")); 2025094Slling } 2035094Slling 2045094Slling /* 2055094Slling * Get a zpool property value for 'prop' and return the value in 2065094Slling * a pre-allocated buffer. 2075094Slling */ 2085094Slling int 2095094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 2105094Slling zprop_source_t *srctype) 2115094Slling { 2125094Slling uint64_t intval; 2135094Slling const char *strval; 2145094Slling zprop_source_t src = ZPROP_SRC_NONE; 2155094Slling nvlist_t *nvroot; 2165094Slling vdev_stat_t *vs; 2175094Slling uint_t vsc; 2185094Slling 2195094Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 2205094Slling if (prop == ZPOOL_PROP_NAME) 2215094Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 2225094Slling else if (prop == ZPOOL_PROP_HEALTH) 2235094Slling (void) strlcpy(buf, "FAULTED", len); 2245094Slling else 2255094Slling (void) strlcpy(buf, "-", len); 2265094Slling return (0); 2275094Slling } 2285094Slling 2295094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 2305094Slling prop != ZPOOL_PROP_NAME) 2315094Slling return (-1); 2325094Slling 2335094Slling switch (zpool_prop_get_type(prop)) { 2345094Slling case PROP_TYPE_STRING: 2355094Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 2365094Slling len); 2375094Slling break; 2385094Slling 2395094Slling case PROP_TYPE_NUMBER: 2405094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2415094Slling 2425094Slling switch (prop) { 2435094Slling case ZPOOL_PROP_SIZE: 2445094Slling case ZPOOL_PROP_USED: 2455094Slling case ZPOOL_PROP_AVAILABLE: 2465094Slling (void) zfs_nicenum(intval, buf, len); 2475094Slling break; 2485094Slling 2495094Slling case ZPOOL_PROP_CAPACITY: 2505094Slling (void) snprintf(buf, len, "%llu%%", 2515094Slling (u_longlong_t)intval); 2525094Slling break; 2535094Slling 2545094Slling case ZPOOL_PROP_HEALTH: 2555094Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2565094Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2575094Slling verify(nvlist_lookup_uint64_array(nvroot, 2585094Slling ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 2595094Slling 2605094Slling (void) strlcpy(buf, zpool_state_to_name(intval, 2615094Slling vs->vs_aux), len); 2625094Slling break; 2635094Slling default: 2645094Slling (void) snprintf(buf, len, "%llu", intval); 2655094Slling } 2665094Slling break; 2675094Slling 2685094Slling case PROP_TYPE_INDEX: 2695094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2705094Slling if (zpool_prop_index_to_string(prop, intval, &strval) 2715094Slling != 0) 2725094Slling return (-1); 2735094Slling (void) strlcpy(buf, strval, len); 2745094Slling break; 2755094Slling 2765094Slling default: 2775094Slling abort(); 2785094Slling } 2795094Slling 2805094Slling if (srctype) 2815094Slling *srctype = src; 2825094Slling 2835094Slling return (0); 2845094Slling } 2855094Slling 2865094Slling /* 2875094Slling * Check if the bootfs name has the same pool name as it is set to. 2885094Slling * Assuming bootfs is a valid dataset name. 2895094Slling */ 2905094Slling static boolean_t 2915094Slling bootfs_name_valid(const char *pool, char *bootfs) 2925094Slling { 2935094Slling int len = strlen(pool); 2945094Slling 2957300SEric.Taylor@Sun.COM if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 2965094Slling return (B_FALSE); 2975094Slling 2985094Slling if (strncmp(pool, bootfs, len) == 0 && 2995094Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 3005094Slling return (B_TRUE); 3015094Slling 3025094Slling return (B_FALSE); 3035094Slling } 3045094Slling 3055094Slling /* 3067042Sgw25295 * Inspect the configuration to determine if any of the devices contain 3077042Sgw25295 * an EFI label. 3087042Sgw25295 */ 3097042Sgw25295 static boolean_t 3107042Sgw25295 pool_uses_efi(nvlist_t *config) 3117042Sgw25295 { 3127042Sgw25295 nvlist_t **child; 3137042Sgw25295 uint_t c, children; 3147042Sgw25295 3157042Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3167042Sgw25295 &child, &children) != 0) 3177042Sgw25295 return (read_efi_label(config, NULL) >= 0); 3187042Sgw25295 3197042Sgw25295 for (c = 0; c < children; c++) { 3207042Sgw25295 if (pool_uses_efi(child[c])) 3217042Sgw25295 return (B_TRUE); 3227042Sgw25295 } 3237042Sgw25295 return (B_FALSE); 3247042Sgw25295 } 3257042Sgw25295 3267965SGeorge.Wilson@Sun.COM static boolean_t 3277965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp) 3287965SGeorge.Wilson@Sun.COM { 3297965SGeorge.Wilson@Sun.COM char bootfs[ZPOOL_MAXNAMELEN]; 3307965SGeorge.Wilson@Sun.COM 3317965SGeorge.Wilson@Sun.COM return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 3327965SGeorge.Wilson@Sun.COM sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 3337965SGeorge.Wilson@Sun.COM sizeof (bootfs)) != 0); 3347965SGeorge.Wilson@Sun.COM } 3357965SGeorge.Wilson@Sun.COM 3367965SGeorge.Wilson@Sun.COM 3377042Sgw25295 /* 3385094Slling * Given an nvlist of zpool properties to be set, validate that they are 3395094Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 3405094Slling * specified as strings. 3415094Slling */ 3425094Slling static nvlist_t * 3437184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 3445094Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 3455094Slling { 3465094Slling nvpair_t *elem; 3475094Slling nvlist_t *retprops; 3485094Slling zpool_prop_t prop; 3495094Slling char *strval; 3505094Slling uint64_t intval; 3515363Seschrock char *slash; 3525363Seschrock struct stat64 statbuf; 3537042Sgw25295 zpool_handle_t *zhp; 3547042Sgw25295 nvlist_t *nvroot; 3555094Slling 3565094Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 3575094Slling (void) no_memory(hdl); 3585094Slling return (NULL); 3595094Slling } 3605094Slling 3615094Slling elem = NULL; 3625094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3635094Slling const char *propname = nvpair_name(elem); 3645094Slling 3655094Slling /* 3665094Slling * Make sure this property is valid and applies to this type. 3675094Slling */ 3685094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 3695094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3705094Slling "invalid property '%s'"), propname); 3715094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3725094Slling goto error; 3735094Slling } 3745094Slling 3755094Slling if (zpool_prop_readonly(prop)) { 3765094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 3775094Slling "is readonly"), propname); 3785094Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 3795094Slling goto error; 3805094Slling } 3815094Slling 3825094Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 3835094Slling &strval, &intval, errbuf) != 0) 3845094Slling goto error; 3855094Slling 3865094Slling /* 3875094Slling * Perform additional checking for specific properties. 3885094Slling */ 3895094Slling switch (prop) { 3905094Slling case ZPOOL_PROP_VERSION: 3915094Slling if (intval < version || intval > SPA_VERSION) { 3925094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3935094Slling "property '%s' number %d is invalid."), 3945094Slling propname, intval); 3955094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 3965094Slling goto error; 3975094Slling } 3985094Slling break; 3995094Slling 4005094Slling case ZPOOL_PROP_BOOTFS: 4015094Slling if (create_or_import) { 4025094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4035094Slling "property '%s' cannot be set at creation " 4045094Slling "or import time"), propname); 4055094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4065094Slling goto error; 4075094Slling } 4085094Slling 4095094Slling if (version < SPA_VERSION_BOOTFS) { 4105094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4115094Slling "pool must be upgraded to support " 4125094Slling "'%s' property"), propname); 4135094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4145094Slling goto error; 4155094Slling } 4165094Slling 4175094Slling /* 4185094Slling * bootfs property value has to be a dataset name and 4195094Slling * the dataset has to be in the same pool as it sets to. 4205094Slling */ 4215094Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 4225094Slling strval)) { 4235094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4245094Slling "is an invalid name"), strval); 4255094Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4265094Slling goto error; 4275094Slling } 4287042Sgw25295 4297042Sgw25295 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 4307042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4317042Sgw25295 "could not open pool '%s'"), poolname); 4327042Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4337042Sgw25295 goto error; 4347042Sgw25295 } 4357042Sgw25295 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 4367042Sgw25295 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4377042Sgw25295 4387042Sgw25295 /* 4397042Sgw25295 * bootfs property cannot be set on a disk which has 4407042Sgw25295 * been EFI labeled. 4417042Sgw25295 */ 4427042Sgw25295 if (pool_uses_efi(nvroot)) { 4437042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4447042Sgw25295 "property '%s' not supported on " 4457042Sgw25295 "EFI labeled devices"), propname); 4467042Sgw25295 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 4477042Sgw25295 zpool_close(zhp); 4487042Sgw25295 goto error; 4497042Sgw25295 } 4507042Sgw25295 zpool_close(zhp); 4515094Slling break; 4525094Slling 4535094Slling case ZPOOL_PROP_ALTROOT: 4545094Slling if (!create_or_import) { 4555094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4565094Slling "property '%s' can only be set during pool " 4575094Slling "creation or import"), propname); 4585094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4595094Slling goto error; 4605094Slling } 4615094Slling 4625094Slling if (strval[0] != '/') { 4635094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4645094Slling "bad alternate root '%s'"), strval); 4655094Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4665094Slling goto error; 4675094Slling } 4685094Slling break; 4695363Seschrock 4705363Seschrock case ZPOOL_PROP_CACHEFILE: 4715363Seschrock if (strval[0] == '\0') 4725363Seschrock break; 4735363Seschrock 4745363Seschrock if (strcmp(strval, "none") == 0) 4755363Seschrock break; 4765363Seschrock 4775363Seschrock if (strval[0] != '/') { 4785363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4795363Seschrock "property '%s' must be empty, an " 4805363Seschrock "absolute path, or 'none'"), propname); 4815363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4825363Seschrock goto error; 4835363Seschrock } 4845363Seschrock 4855363Seschrock slash = strrchr(strval, '/'); 4865363Seschrock 4875363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4885363Seschrock strcmp(slash, "/..") == 0) { 4895363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4905363Seschrock "'%s' is not a valid file"), strval); 4915363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4925363Seschrock goto error; 4935363Seschrock } 4945363Seschrock 4955363Seschrock *slash = '\0'; 4965363Seschrock 4975621Seschrock if (strval[0] != '\0' && 4985621Seschrock (stat64(strval, &statbuf) != 0 || 4995621Seschrock !S_ISDIR(statbuf.st_mode))) { 5005363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5015363Seschrock "'%s' is not a valid directory"), 5025363Seschrock strval); 5035363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5045363Seschrock goto error; 5055363Seschrock } 5065363Seschrock 5075363Seschrock *slash = '/'; 5085363Seschrock break; 5095094Slling } 5105094Slling } 5115094Slling 5125094Slling return (retprops); 5135094Slling error: 5145094Slling nvlist_free(retprops); 5155094Slling return (NULL); 5165094Slling } 5175094Slling 5185094Slling /* 5195094Slling * Set zpool property : propname=propval. 5205094Slling */ 5215094Slling int 5225094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 5235094Slling { 5245094Slling zfs_cmd_t zc = { 0 }; 5255094Slling int ret = -1; 5265094Slling char errbuf[1024]; 5275094Slling nvlist_t *nvl = NULL; 5285094Slling nvlist_t *realprops; 5295094Slling uint64_t version; 5305094Slling 5315094Slling (void) snprintf(errbuf, sizeof (errbuf), 5325094Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 5335094Slling zhp->zpool_name); 5345094Slling 5355094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) 5365094Slling return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf)); 5375094Slling 5385094Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5395094Slling return (no_memory(zhp->zpool_hdl)); 5405094Slling 5415094Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 5425094Slling nvlist_free(nvl); 5435094Slling return (no_memory(zhp->zpool_hdl)); 5445094Slling } 5455094Slling 5465094Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5477184Stimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 5485094Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 5495094Slling nvlist_free(nvl); 5505094Slling return (-1); 5515094Slling } 5525094Slling 5535094Slling nvlist_free(nvl); 5545094Slling nvl = realprops; 5555094Slling 5565094Slling /* 5575094Slling * Execute the corresponding ioctl() to set this property. 5585094Slling */ 5595094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 5605094Slling 5615094Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 5625094Slling nvlist_free(nvl); 5635094Slling return (-1); 5645094Slling } 5655094Slling 5665094Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 5675094Slling 5685094Slling zcmd_free_nvlists(&zc); 5695094Slling nvlist_free(nvl); 5705094Slling 5715094Slling if (ret) 5725094Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 5735094Slling else 5745094Slling (void) zpool_props_refresh(zhp); 5755094Slling 5765094Slling return (ret); 5775094Slling } 5785094Slling 5795094Slling int 5805094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 5815094Slling { 5825094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 5835094Slling zprop_list_t *entry; 5845094Slling char buf[ZFS_MAXPROPLEN]; 5855094Slling 5865094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 5875094Slling return (-1); 5885094Slling 5895094Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 5905094Slling 5915094Slling if (entry->pl_fixed) 5925094Slling continue; 5935094Slling 5945094Slling if (entry->pl_prop != ZPROP_INVAL && 5955094Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 5965094Slling NULL) == 0) { 5975094Slling if (strlen(buf) > entry->pl_width) 5985094Slling entry->pl_width = strlen(buf); 5995094Slling } 6005094Slling } 6015094Slling 6025094Slling return (0); 6035094Slling } 6045094Slling 6055094Slling 606789Sahrens /* 607789Sahrens * Validate the given pool name, optionally putting an extended error message in 608789Sahrens * 'buf'. 609789Sahrens */ 6106423Sgw25295 boolean_t 6112082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 612789Sahrens { 613789Sahrens namecheck_err_t why; 614789Sahrens char what; 6151773Seschrock int ret; 616789Sahrens 6171773Seschrock ret = pool_namecheck(pool, &why, &what); 6181773Seschrock 6191773Seschrock /* 6201773Seschrock * The rules for reserved pool names were extended at a later point. 6211773Seschrock * But we need to support users with existing pools that may now be 6221773Seschrock * invalid. So we only check for this expanded set of names during a 6231773Seschrock * create (or import), and only in userland. 6241773Seschrock */ 6251773Seschrock if (ret == 0 && !isopen && 6261773Seschrock (strncmp(pool, "mirror", 6) == 0 || 6271773Seschrock strncmp(pool, "raidz", 5) == 0 || 6284527Sperrin strncmp(pool, "spare", 5) == 0 || 6294527Sperrin strcmp(pool, "log") == 0)) { 6306423Sgw25295 if (hdl != NULL) 6316423Sgw25295 zfs_error_aux(hdl, 6326423Sgw25295 dgettext(TEXT_DOMAIN, "name is reserved")); 6332082Seschrock return (B_FALSE); 6341773Seschrock } 6351773Seschrock 6361773Seschrock 6371773Seschrock if (ret != 0) { 6382082Seschrock if (hdl != NULL) { 639789Sahrens switch (why) { 6401003Slling case NAME_ERR_TOOLONG: 6412082Seschrock zfs_error_aux(hdl, 6421003Slling dgettext(TEXT_DOMAIN, "name is too long")); 6431003Slling break; 6441003Slling 645789Sahrens case NAME_ERR_INVALCHAR: 6462082Seschrock zfs_error_aux(hdl, 647789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 648789Sahrens "'%c' in pool name"), what); 649789Sahrens break; 650789Sahrens 651789Sahrens case NAME_ERR_NOLETTER: 6522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6532082Seschrock "name must begin with a letter")); 654789Sahrens break; 655789Sahrens 656789Sahrens case NAME_ERR_RESERVED: 6572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6582082Seschrock "name is reserved")); 659789Sahrens break; 660789Sahrens 661789Sahrens case NAME_ERR_DISKLIKE: 6622082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6632082Seschrock "pool name is reserved")); 664789Sahrens break; 6652856Snd150628 6662856Snd150628 case NAME_ERR_LEADING_SLASH: 6672856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6682856Snd150628 "leading slash in name")); 6692856Snd150628 break; 6702856Snd150628 6712856Snd150628 case NAME_ERR_EMPTY_COMPONENT: 6722856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6732856Snd150628 "empty component in name")); 6742856Snd150628 break; 6752856Snd150628 6762856Snd150628 case NAME_ERR_TRAILING_SLASH: 6772856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6782856Snd150628 "trailing slash in name")); 6792856Snd150628 break; 6802856Snd150628 6812856Snd150628 case NAME_ERR_MULTIPLE_AT: 6822856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6832856Snd150628 "multiple '@' delimiters in name")); 6842856Snd150628 break; 6852856Snd150628 686789Sahrens } 687789Sahrens } 6882082Seschrock return (B_FALSE); 689789Sahrens } 690789Sahrens 6912082Seschrock return (B_TRUE); 692789Sahrens } 693789Sahrens 694789Sahrens /* 695789Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 696789Sahrens * state. 697789Sahrens */ 698789Sahrens zpool_handle_t * 6992082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 700789Sahrens { 701789Sahrens zpool_handle_t *zhp; 7022142Seschrock boolean_t missing; 703789Sahrens 704789Sahrens /* 705789Sahrens * Make sure the pool name is valid. 706789Sahrens */ 7072082Seschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 7083237Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 7092082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 7102082Seschrock pool); 711789Sahrens return (NULL); 712789Sahrens } 713789Sahrens 7142082Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7152082Seschrock return (NULL); 716789Sahrens 7172082Seschrock zhp->zpool_hdl = hdl; 718789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 719789Sahrens 7202142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7212142Seschrock zpool_close(zhp); 7222142Seschrock return (NULL); 7232142Seschrock } 7242142Seschrock 7252142Seschrock if (missing) { 7265094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 7273237Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 7285094Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 7292142Seschrock zpool_close(zhp); 7302142Seschrock return (NULL); 731789Sahrens } 732789Sahrens 733789Sahrens return (zhp); 734789Sahrens } 735789Sahrens 736789Sahrens /* 737789Sahrens * Like the above, but silent on error. Used when iterating over pools (because 738789Sahrens * the configuration cache may be out of date). 739789Sahrens */ 7402142Seschrock int 7412142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 742789Sahrens { 743789Sahrens zpool_handle_t *zhp; 7442142Seschrock boolean_t missing; 745789Sahrens 7462142Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7472142Seschrock return (-1); 748789Sahrens 7492082Seschrock zhp->zpool_hdl = hdl; 750789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 751789Sahrens 7522142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7532142Seschrock zpool_close(zhp); 7542142Seschrock return (-1); 755789Sahrens } 756789Sahrens 7572142Seschrock if (missing) { 7582142Seschrock zpool_close(zhp); 7592142Seschrock *ret = NULL; 7602142Seschrock return (0); 7612142Seschrock } 7622142Seschrock 7632142Seschrock *ret = zhp; 7642142Seschrock return (0); 765789Sahrens } 766789Sahrens 767789Sahrens /* 768789Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 769789Sahrens * state. 770789Sahrens */ 771789Sahrens zpool_handle_t * 7722082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 773789Sahrens { 774789Sahrens zpool_handle_t *zhp; 775789Sahrens 7762082Seschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 777789Sahrens return (NULL); 778789Sahrens 779789Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 7803237Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 7812082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 782789Sahrens zpool_close(zhp); 783789Sahrens return (NULL); 784789Sahrens } 785789Sahrens 786789Sahrens return (zhp); 787789Sahrens } 788789Sahrens 789789Sahrens /* 790789Sahrens * Close the handle. Simply frees the memory associated with the handle. 791789Sahrens */ 792789Sahrens void 793789Sahrens zpool_close(zpool_handle_t *zhp) 794789Sahrens { 795789Sahrens if (zhp->zpool_config) 796789Sahrens nvlist_free(zhp->zpool_config); 797952Seschrock if (zhp->zpool_old_config) 798952Seschrock nvlist_free(zhp->zpool_old_config); 7993912Slling if (zhp->zpool_props) 8003912Slling nvlist_free(zhp->zpool_props); 801789Sahrens free(zhp); 802789Sahrens } 803789Sahrens 804789Sahrens /* 805789Sahrens * Return the name of the pool. 806789Sahrens */ 807789Sahrens const char * 808789Sahrens zpool_get_name(zpool_handle_t *zhp) 809789Sahrens { 810789Sahrens return (zhp->zpool_name); 811789Sahrens } 812789Sahrens 813789Sahrens 814789Sahrens /* 815789Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 816789Sahrens */ 817789Sahrens int 818789Sahrens zpool_get_state(zpool_handle_t *zhp) 819789Sahrens { 820789Sahrens return (zhp->zpool_state); 821789Sahrens } 822789Sahrens 823789Sahrens /* 824789Sahrens * Create the named pool, using the provided vdev list. It is assumed 825789Sahrens * that the consumer has already validated the contents of the nvlist, so we 826789Sahrens * don't have to worry about error semantics. 827789Sahrens */ 828789Sahrens int 8292082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8307184Stimh nvlist_t *props, nvlist_t *fsprops) 831789Sahrens { 832789Sahrens zfs_cmd_t zc = { 0 }; 8337184Stimh nvlist_t *zc_fsprops = NULL; 8347184Stimh nvlist_t *zc_props = NULL; 8352082Seschrock char msg[1024]; 8365094Slling char *altroot; 8377184Stimh int ret = -1; 8382082Seschrock 8392082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 8402082Seschrock "cannot create '%s'"), pool); 841789Sahrens 8422082Seschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 8432082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 8442082Seschrock 8455320Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 8465320Slling return (-1); 8475320Slling 8487184Stimh if (props) { 8497184Stimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8507184Stimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8517184Stimh goto create_failed; 8527184Stimh } 8535320Slling } 854789Sahrens 8557184Stimh if (fsprops) { 8567184Stimh uint64_t zoned; 8577184Stimh char *zonestr; 8587184Stimh 8597184Stimh zoned = ((nvlist_lookup_string(fsprops, 8607184Stimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8617184Stimh strcmp(zonestr, "on") == 0); 8627184Stimh 8637184Stimh if ((zc_fsprops = zfs_valid_proplist(hdl, 8647184Stimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 8657184Stimh goto create_failed; 8667184Stimh } 8677184Stimh if (!zc_props && 8687184Stimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 8697184Stimh goto create_failed; 8707184Stimh } 8717184Stimh if (nvlist_add_nvlist(zc_props, 8727184Stimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 8737184Stimh goto create_failed; 8747184Stimh } 8757184Stimh } 8767184Stimh 8777184Stimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 8787184Stimh goto create_failed; 8797184Stimh 880789Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 881789Sahrens 8827184Stimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 8835320Slling 8842676Seschrock zcmd_free_nvlists(&zc); 8857184Stimh nvlist_free(zc_props); 8867184Stimh nvlist_free(zc_fsprops); 8872082Seschrock 888789Sahrens switch (errno) { 889789Sahrens case EBUSY: 890789Sahrens /* 891789Sahrens * This can happen if the user has specified the same 892789Sahrens * device multiple times. We can't reliably detect this 893789Sahrens * until we try to add it and see we already have a 894789Sahrens * label. 895789Sahrens */ 8962082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8972082Seschrock "one or more vdevs refer to the same device")); 8982082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 899789Sahrens 900789Sahrens case EOVERFLOW: 901789Sahrens /* 9022082Seschrock * This occurs when one of the devices is below 903789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 904789Sahrens * device was the problem device since there's no 905789Sahrens * reliable way to determine device size from userland. 906789Sahrens */ 907789Sahrens { 908789Sahrens char buf[64]; 909789Sahrens 910789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 911789Sahrens 9122082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9132082Seschrock "one or more devices is less than the " 9142082Seschrock "minimum size (%s)"), buf); 915789Sahrens } 9162082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 917789Sahrens 918789Sahrens case ENOSPC: 9192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9202082Seschrock "one or more devices is out of space")); 9212082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 922789Sahrens 9235450Sbrendan case ENOTBLK: 9245450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9255450Sbrendan "cache device must be a disk or disk slice")); 9265450Sbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 9275450Sbrendan 928789Sahrens default: 9292082Seschrock return (zpool_standard_error(hdl, errno, msg)); 930789Sahrens } 931789Sahrens } 932789Sahrens 933789Sahrens /* 934789Sahrens * If this is an alternate root pool, then we automatically set the 9352676Seschrock * mountpoint of the root dataset to be '/'. 936789Sahrens */ 9375094Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 9385094Slling &altroot) == 0) { 939789Sahrens zfs_handle_t *zhp; 940789Sahrens 9415094Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 9422676Seschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 9432676Seschrock "/") == 0); 944789Sahrens 945789Sahrens zfs_close(zhp); 946789Sahrens } 947789Sahrens 9487184Stimh create_failed: 9495320Slling zcmd_free_nvlists(&zc); 9507184Stimh nvlist_free(zc_props); 9517184Stimh nvlist_free(zc_fsprops); 9527184Stimh return (ret); 953789Sahrens } 954789Sahrens 955789Sahrens /* 956789Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 957789Sahrens * datasets left in the pool. 958789Sahrens */ 959789Sahrens int 960789Sahrens zpool_destroy(zpool_handle_t *zhp) 961789Sahrens { 962789Sahrens zfs_cmd_t zc = { 0 }; 963789Sahrens zfs_handle_t *zfp = NULL; 9642082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9652082Seschrock char msg[1024]; 966789Sahrens 967789Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 9682082Seschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 9692082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 970789Sahrens return (-1); 971789Sahrens 9722856Snd150628 if (zpool_remove_zvol_links(zhp) != 0) 973789Sahrens return (-1); 974789Sahrens 975789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 976789Sahrens 9774543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 9782082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 9792082Seschrock "cannot destroy '%s'"), zhp->zpool_name); 980789Sahrens 9812082Seschrock if (errno == EROFS) { 9822082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9832082Seschrock "one or more devices is read only")); 9842082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 9852082Seschrock } else { 9862082Seschrock (void) zpool_standard_error(hdl, errno, msg); 987789Sahrens } 988789Sahrens 989789Sahrens if (zfp) 990789Sahrens zfs_close(zfp); 991789Sahrens return (-1); 992789Sahrens } 993789Sahrens 994789Sahrens if (zfp) { 995789Sahrens remove_mountpoint(zfp); 996789Sahrens zfs_close(zfp); 997789Sahrens } 998789Sahrens 999789Sahrens return (0); 1000789Sahrens } 1001789Sahrens 1002789Sahrens /* 1003789Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1004789Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1005789Sahrens */ 1006789Sahrens int 1007789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1008789Sahrens { 10092676Seschrock zfs_cmd_t zc = { 0 }; 10102082Seschrock int ret; 10112082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10122082Seschrock char msg[1024]; 10135450Sbrendan nvlist_t **spares, **l2cache; 10145450Sbrendan uint_t nspares, nl2cache; 10152082Seschrock 10162082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10172082Seschrock "cannot add to '%s'"), zhp->zpool_name); 10182082Seschrock 10195450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10205450Sbrendan SPA_VERSION_SPARES && 10212082Seschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10222082Seschrock &spares, &nspares) == 0) { 10232082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10242082Seschrock "upgraded to add hot spares")); 10252082Seschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10262082Seschrock } 1027789Sahrens 10287965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 10297965SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 10307965SGeorge.Wilson@Sun.COM uint64_t s; 10317965SGeorge.Wilson@Sun.COM 10327965SGeorge.Wilson@Sun.COM for (s = 0; s < nspares; s++) { 10337965SGeorge.Wilson@Sun.COM char *path; 10347965SGeorge.Wilson@Sun.COM 10357965SGeorge.Wilson@Sun.COM if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 10367965SGeorge.Wilson@Sun.COM &path) == 0 && pool_uses_efi(spares[s])) { 10377965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10387965SGeorge.Wilson@Sun.COM "device '%s' contains an EFI label and " 10397965SGeorge.Wilson@Sun.COM "cannot be used on root pools."), 10407965SGeorge.Wilson@Sun.COM zpool_vdev_name(hdl, NULL, spares[s])); 10417965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 10427965SGeorge.Wilson@Sun.COM } 10437965SGeorge.Wilson@Sun.COM } 10447965SGeorge.Wilson@Sun.COM } 10457965SGeorge.Wilson@Sun.COM 10465450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10475450Sbrendan SPA_VERSION_L2CACHE && 10485450Sbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 10495450Sbrendan &l2cache, &nl2cache) == 0) { 10505450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10515450Sbrendan "upgraded to add cache devices")); 10525450Sbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10535450Sbrendan } 10545450Sbrendan 10555094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 10562082Seschrock return (-1); 1057789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1058789Sahrens 10594543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1060789Sahrens switch (errno) { 1061789Sahrens case EBUSY: 1062789Sahrens /* 1063789Sahrens * This can happen if the user has specified the same 1064789Sahrens * device multiple times. We can't reliably detect this 1065789Sahrens * until we try to add it and see we already have a 1066789Sahrens * label. 1067789Sahrens */ 10682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10692082Seschrock "one or more vdevs refer to the same device")); 10702082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1071789Sahrens break; 1072789Sahrens 1073789Sahrens case EOVERFLOW: 1074789Sahrens /* 1075789Sahrens * This occurrs when one of the devices is below 1076789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1077789Sahrens * device was the problem device since there's no 1078789Sahrens * reliable way to determine device size from userland. 1079789Sahrens */ 1080789Sahrens { 1081789Sahrens char buf[64]; 1082789Sahrens 1083789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1084789Sahrens 10852082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10862082Seschrock "device is less than the minimum " 10872082Seschrock "size (%s)"), buf); 1088789Sahrens } 10892082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 10902082Seschrock break; 10912082Seschrock 10922082Seschrock case ENOTSUP: 10932082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10944527Sperrin "pool must be upgraded to add these vdevs")); 10952082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1096789Sahrens break; 1097789Sahrens 10983912Slling case EDOM: 10993912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11004527Sperrin "root pool can not have multiple vdevs" 11014527Sperrin " or separate logs")); 11023912Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 11033912Slling break; 11043912Slling 11055450Sbrendan case ENOTBLK: 11065450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11075450Sbrendan "cache device must be a disk or disk slice")); 11085450Sbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 11095450Sbrendan break; 11105450Sbrendan 1111789Sahrens default: 11122082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1113789Sahrens } 1114789Sahrens 11152082Seschrock ret = -1; 11162082Seschrock } else { 11172082Seschrock ret = 0; 1118789Sahrens } 1119789Sahrens 11202676Seschrock zcmd_free_nvlists(&zc); 1121789Sahrens 11222082Seschrock return (ret); 1123789Sahrens } 1124789Sahrens 1125789Sahrens /* 1126789Sahrens * Exports the pool from the system. The caller must ensure that there are no 1127789Sahrens * mounted datasets in the pool. 1128789Sahrens */ 1129789Sahrens int 1130*8211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1131789Sahrens { 1132789Sahrens zfs_cmd_t zc = { 0 }; 11337214Slling char msg[1024]; 1134789Sahrens 1135789Sahrens if (zpool_remove_zvol_links(zhp) != 0) 1136789Sahrens return (-1); 1137789Sahrens 11387214Slling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 11397214Slling "cannot export '%s'"), zhp->zpool_name); 11407214Slling 1141789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 11427214Slling zc.zc_cookie = force; 1143*8211SGeorge.Wilson@Sun.COM zc.zc_guid = hardforce; 11447214Slling 11457214Slling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 11467214Slling switch (errno) { 11477214Slling case EXDEV: 11487214Slling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 11497214Slling "use '-f' to override the following errors:\n" 11507214Slling "'%s' has an active shared spare which could be" 11517214Slling " used by other pools once '%s' is exported."), 11527214Slling zhp->zpool_name, zhp->zpool_name); 11537214Slling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 11547214Slling msg)); 11557214Slling default: 11567214Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 11577214Slling msg)); 11587214Slling } 11597214Slling } 11607214Slling 1161789Sahrens return (0); 1162789Sahrens } 1163789Sahrens 1164*8211SGeorge.Wilson@Sun.COM int 1165*8211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force) 1166*8211SGeorge.Wilson@Sun.COM { 1167*8211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, force, B_FALSE)); 1168*8211SGeorge.Wilson@Sun.COM } 1169*8211SGeorge.Wilson@Sun.COM 1170*8211SGeorge.Wilson@Sun.COM int 1171*8211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp) 1172*8211SGeorge.Wilson@Sun.COM { 1173*8211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 1174*8211SGeorge.Wilson@Sun.COM } 1175*8211SGeorge.Wilson@Sun.COM 1176789Sahrens /* 11775094Slling * zpool_import() is a contracted interface. Should be kept the same 11785094Slling * if possible. 11795094Slling * 11805094Slling * Applications should use zpool_import_props() to import a pool with 11815094Slling * new properties value to be set. 1182789Sahrens */ 1183789Sahrens int 11842082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 11855094Slling char *altroot) 11865094Slling { 11875094Slling nvlist_t *props = NULL; 11885094Slling int ret; 11895094Slling 11905094Slling if (altroot != NULL) { 11915094Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 11925094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 11935094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 11945094Slling newname)); 11955094Slling } 11965094Slling 11975094Slling if (nvlist_add_string(props, 11988084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 11998084SGeorge.Wilson@Sun.COM nvlist_add_string(props, 12008084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 12015094Slling nvlist_free(props); 12025094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 12035094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 12045094Slling newname)); 12055094Slling } 12065094Slling } 12075094Slling 12086643Seschrock ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 12095094Slling if (props) 12105094Slling nvlist_free(props); 12115094Slling return (ret); 12125094Slling } 12135094Slling 12145094Slling /* 12155094Slling * Import the given pool using the known configuration and a list of 12165094Slling * properties to be set. The configuration should have come from 12175094Slling * zpool_find_import(). The 'newname' parameters control whether the pool 12185094Slling * is imported with a different name. 12195094Slling */ 12205094Slling int 12215094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 12226643Seschrock nvlist_t *props, boolean_t importfaulted) 1223789Sahrens { 12242676Seschrock zfs_cmd_t zc = { 0 }; 1225789Sahrens char *thename; 1226789Sahrens char *origname; 1227789Sahrens int ret; 12285094Slling char errbuf[1024]; 1229789Sahrens 1230789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1231789Sahrens &origname) == 0); 1232789Sahrens 12335094Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 12345094Slling "cannot import pool '%s'"), origname); 12355094Slling 1236789Sahrens if (newname != NULL) { 12372082Seschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 12383237Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 12392082Seschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 12402082Seschrock newname)); 1241789Sahrens thename = (char *)newname; 1242789Sahrens } else { 1243789Sahrens thename = origname; 1244789Sahrens } 1245789Sahrens 12465094Slling if (props) { 12475094Slling uint64_t version; 12485094Slling 12495094Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 12505094Slling &version) == 0); 12515094Slling 12527184Stimh if ((props = zpool_valid_proplist(hdl, origname, 12535320Slling props, version, B_TRUE, errbuf)) == NULL) { 12545094Slling return (-1); 12555320Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 12565320Slling nvlist_free(props); 12575094Slling return (-1); 12585320Slling } 12595094Slling } 1260789Sahrens 1261789Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1262789Sahrens 1263789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 12641544Seschrock &zc.zc_guid) == 0); 1265789Sahrens 12665320Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 12675320Slling nvlist_free(props); 12682082Seschrock return (-1); 12695320Slling } 1270789Sahrens 12716643Seschrock zc.zc_cookie = (uint64_t)importfaulted; 1272789Sahrens ret = 0; 12734543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1274789Sahrens char desc[1024]; 1275789Sahrens if (newname == NULL) 1276789Sahrens (void) snprintf(desc, sizeof (desc), 1277789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1278789Sahrens thename); 1279789Sahrens else 1280789Sahrens (void) snprintf(desc, sizeof (desc), 1281789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1282789Sahrens origname, thename); 1283789Sahrens 1284789Sahrens switch (errno) { 12851544Seschrock case ENOTSUP: 12861544Seschrock /* 12871544Seschrock * Unsupported version. 12881544Seschrock */ 12892082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 12901544Seschrock break; 12911544Seschrock 12922174Seschrock case EINVAL: 12932174Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 12942174Seschrock break; 12952174Seschrock 1296789Sahrens default: 12972082Seschrock (void) zpool_standard_error(hdl, errno, desc); 1298789Sahrens } 1299789Sahrens 1300789Sahrens ret = -1; 1301789Sahrens } else { 1302789Sahrens zpool_handle_t *zhp; 13034543Smarks 1304789Sahrens /* 1305789Sahrens * This should never fail, but play it safe anyway. 1306789Sahrens */ 13072142Seschrock if (zpool_open_silent(hdl, thename, &zhp) != 0) { 13082142Seschrock ret = -1; 13092142Seschrock } else if (zhp != NULL) { 1310789Sahrens ret = zpool_create_zvol_links(zhp); 1311789Sahrens zpool_close(zhp); 1312789Sahrens } 13134543Smarks 1314789Sahrens } 1315789Sahrens 13162676Seschrock zcmd_free_nvlists(&zc); 13175320Slling nvlist_free(props); 13185320Slling 1319789Sahrens return (ret); 1320789Sahrens } 1321789Sahrens 1322789Sahrens /* 1323789Sahrens * Scrub the pool. 1324789Sahrens */ 1325789Sahrens int 1326789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) 1327789Sahrens { 1328789Sahrens zfs_cmd_t zc = { 0 }; 1329789Sahrens char msg[1024]; 13302082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1331789Sahrens 1332789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1333789Sahrens zc.zc_cookie = type; 1334789Sahrens 13354543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0) 1336789Sahrens return (0); 1337789Sahrens 1338789Sahrens (void) snprintf(msg, sizeof (msg), 1339789Sahrens dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1340789Sahrens 13412082Seschrock if (errno == EBUSY) 13422082Seschrock return (zfs_error(hdl, EZFS_RESILVERING, msg)); 13432082Seschrock else 13442082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1345789Sahrens } 1346789Sahrens 13472468Sek110237 /* 13482468Sek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 13492468Sek110237 * spare; but FALSE if its an INUSE spare. 13502468Sek110237 */ 13512082Seschrock static nvlist_t * 13522082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, 13537326SEric.Schrock@Sun.COM boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 13541544Seschrock { 13551544Seschrock uint_t c, children; 13561544Seschrock nvlist_t **child; 13572082Seschrock uint64_t theguid, present; 13581544Seschrock char *path; 13591544Seschrock uint64_t wholedisk = 0; 13602082Seschrock nvlist_t *ret; 13617326SEric.Schrock@Sun.COM uint64_t is_log; 13621544Seschrock 13632082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); 13641544Seschrock 13651544Seschrock if (search == NULL && 13661544Seschrock nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { 13671544Seschrock /* 13681544Seschrock * If the device has never been present since import, the only 13691544Seschrock * reliable way to match the vdev is by GUID. 13701544Seschrock */ 13712082Seschrock if (theguid == guid) 13722082Seschrock return (nv); 13731544Seschrock } else if (search != NULL && 13741544Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 13751544Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 13761544Seschrock &wholedisk); 13771544Seschrock if (wholedisk) { 13781544Seschrock /* 13791544Seschrock * For whole disks, the internal path has 's0', but the 13801544Seschrock * path passed in by the user doesn't. 13811544Seschrock */ 13821544Seschrock if (strlen(search) == strlen(path) - 2 && 13831544Seschrock strncmp(search, path, strlen(search)) == 0) 13842082Seschrock return (nv); 13851544Seschrock } else if (strcmp(search, path) == 0) { 13862082Seschrock return (nv); 13871544Seschrock } 13881544Seschrock } 13891544Seschrock 13901544Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 13911544Seschrock &child, &children) != 0) 13922082Seschrock return (NULL); 13931544Seschrock 13947326SEric.Schrock@Sun.COM for (c = 0; c < children; c++) { 13952082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 13967326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 13977326SEric.Schrock@Sun.COM /* 13987326SEric.Schrock@Sun.COM * The 'is_log' value is only set for the toplevel 13997326SEric.Schrock@Sun.COM * vdev, not the leaf vdevs. So we always lookup the 14007326SEric.Schrock@Sun.COM * log device from the root of the vdev tree (where 14017326SEric.Schrock@Sun.COM * 'log' is non-NULL). 14027326SEric.Schrock@Sun.COM */ 14037326SEric.Schrock@Sun.COM if (log != NULL && 14047326SEric.Schrock@Sun.COM nvlist_lookup_uint64(child[c], 14057326SEric.Schrock@Sun.COM ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 14067326SEric.Schrock@Sun.COM is_log) { 14077326SEric.Schrock@Sun.COM *log = B_TRUE; 14087326SEric.Schrock@Sun.COM } 14091544Seschrock return (ret); 14107326SEric.Schrock@Sun.COM } 14117326SEric.Schrock@Sun.COM } 14121544Seschrock 14132082Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 14142082Seschrock &child, &children) == 0) { 14152082Seschrock for (c = 0; c < children; c++) { 14162082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 14177326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 14182468Sek110237 *avail_spare = B_TRUE; 14192082Seschrock return (ret); 14202082Seschrock } 14212082Seschrock } 14222082Seschrock } 14232082Seschrock 14245450Sbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 14255450Sbrendan &child, &children) == 0) { 14265450Sbrendan for (c = 0; c < children; c++) { 14275450Sbrendan if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 14287326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 14295450Sbrendan *l2cache = B_TRUE; 14305450Sbrendan return (ret); 14315450Sbrendan } 14325450Sbrendan } 14335450Sbrendan } 14345450Sbrendan 14352082Seschrock return (NULL); 14361544Seschrock } 14371544Seschrock 14382082Seschrock nvlist_t * 14395450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 14407326SEric.Schrock@Sun.COM boolean_t *l2cache, boolean_t *log) 14411544Seschrock { 14421544Seschrock char buf[MAXPATHLEN]; 14431544Seschrock const char *search; 14441544Seschrock char *end; 14451544Seschrock nvlist_t *nvroot; 14461544Seschrock uint64_t guid; 14471544Seschrock 14481613Seschrock guid = strtoull(path, &end, 10); 14491544Seschrock if (guid != 0 && *end == '\0') { 14501544Seschrock search = NULL; 14511544Seschrock } else if (path[0] != '/') { 14521544Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 14531544Seschrock search = buf; 14541544Seschrock } else { 14551544Seschrock search = path; 14561544Seschrock } 14571544Seschrock 14581544Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 14591544Seschrock &nvroot) == 0); 14601544Seschrock 14612468Sek110237 *avail_spare = B_FALSE; 14625450Sbrendan *l2cache = B_FALSE; 14637326SEric.Schrock@Sun.COM if (log != NULL) 14647326SEric.Schrock@Sun.COM *log = B_FALSE; 14655450Sbrendan return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare, 14667326SEric.Schrock@Sun.COM l2cache, log)); 14672468Sek110237 } 14682468Sek110237 14697656SSherry.Moore@Sun.COM static int 14707656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv) 14717656SSherry.Moore@Sun.COM { 14727656SSherry.Moore@Sun.COM uint64_t ival; 14737656SSherry.Moore@Sun.COM 14747656SSherry.Moore@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 14757656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 14767656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 14777656SSherry.Moore@Sun.COM return (0); 14787656SSherry.Moore@Sun.COM 14797656SSherry.Moore@Sun.COM return (1); 14807656SSherry.Moore@Sun.COM } 14817656SSherry.Moore@Sun.COM 14827656SSherry.Moore@Sun.COM /* 14837656SSherry.Moore@Sun.COM * Get phys_path for a root pool 14847656SSherry.Moore@Sun.COM * Return 0 on success; non-zeron on failure. 14857656SSherry.Moore@Sun.COM */ 14867656SSherry.Moore@Sun.COM int 14877656SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath) 14887656SSherry.Moore@Sun.COM { 14897656SSherry.Moore@Sun.COM nvlist_t *vdev_root; 14907656SSherry.Moore@Sun.COM nvlist_t **child; 14917656SSherry.Moore@Sun.COM uint_t count; 14927656SSherry.Moore@Sun.COM int i; 14937656SSherry.Moore@Sun.COM 14947656SSherry.Moore@Sun.COM /* 14957656SSherry.Moore@Sun.COM * Make sure this is a root pool, as phys_path doesn't mean 14967656SSherry.Moore@Sun.COM * anything to a non-root pool. 14977656SSherry.Moore@Sun.COM */ 14987965SGeorge.Wilson@Sun.COM if (!pool_is_bootable(zhp)) 14997656SSherry.Moore@Sun.COM return (-1); 15007656SSherry.Moore@Sun.COM 15017656SSherry.Moore@Sun.COM verify(nvlist_lookup_nvlist(zhp->zpool_config, 15027656SSherry.Moore@Sun.COM ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0); 15037656SSherry.Moore@Sun.COM 15047656SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 15057656SSherry.Moore@Sun.COM &child, &count) != 0) 15067656SSherry.Moore@Sun.COM return (-2); 15077656SSherry.Moore@Sun.COM 15087656SSherry.Moore@Sun.COM for (i = 0; i < count; i++) { 15097656SSherry.Moore@Sun.COM nvlist_t **child2; 15107656SSherry.Moore@Sun.COM uint_t count2; 15117656SSherry.Moore@Sun.COM char *type; 15127656SSherry.Moore@Sun.COM char *tmppath; 15137656SSherry.Moore@Sun.COM int j; 15147656SSherry.Moore@Sun.COM 15157656SSherry.Moore@Sun.COM if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type) 15167656SSherry.Moore@Sun.COM != 0) 15177656SSherry.Moore@Sun.COM return (-3); 15187656SSherry.Moore@Sun.COM 15197656SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_DISK) == 0) { 15207656SSherry.Moore@Sun.COM if (!vdev_online(child[i])) 15217656SSherry.Moore@Sun.COM return (-8); 15227656SSherry.Moore@Sun.COM verify(nvlist_lookup_string(child[i], 15237656SSherry.Moore@Sun.COM ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0); 15247656SSherry.Moore@Sun.COM (void) strncpy(physpath, tmppath, strlen(tmppath)); 15257656SSherry.Moore@Sun.COM } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) { 15267656SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist_array(child[i], 15277656SSherry.Moore@Sun.COM ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0) 15287656SSherry.Moore@Sun.COM return (-4); 15297656SSherry.Moore@Sun.COM 15307656SSherry.Moore@Sun.COM for (j = 0; j < count2; j++) { 15317656SSherry.Moore@Sun.COM if (!vdev_online(child2[j])) 15327656SSherry.Moore@Sun.COM return (-8); 15337656SSherry.Moore@Sun.COM if (nvlist_lookup_string(child2[j], 15347656SSherry.Moore@Sun.COM ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0) 15357656SSherry.Moore@Sun.COM return (-5); 15367656SSherry.Moore@Sun.COM 15377656SSherry.Moore@Sun.COM if ((strlen(physpath) + strlen(tmppath)) > 15387656SSherry.Moore@Sun.COM MAXNAMELEN) 15397656SSherry.Moore@Sun.COM return (-6); 15407656SSherry.Moore@Sun.COM 15417656SSherry.Moore@Sun.COM if (strlen(physpath) == 0) { 15427656SSherry.Moore@Sun.COM (void) strncpy(physpath, tmppath, 15437656SSherry.Moore@Sun.COM strlen(tmppath)); 15447656SSherry.Moore@Sun.COM } else { 15457656SSherry.Moore@Sun.COM (void) strcat(physpath, " "); 15467656SSherry.Moore@Sun.COM (void) strcat(physpath, tmppath); 15477656SSherry.Moore@Sun.COM } 15487656SSherry.Moore@Sun.COM } 15497656SSherry.Moore@Sun.COM } else { 15507656SSherry.Moore@Sun.COM return (-7); 15517656SSherry.Moore@Sun.COM } 15527656SSherry.Moore@Sun.COM } 15537656SSherry.Moore@Sun.COM 15547656SSherry.Moore@Sun.COM return (0); 15557656SSherry.Moore@Sun.COM } 15567656SSherry.Moore@Sun.COM 15572468Sek110237 /* 15585450Sbrendan * Returns TRUE if the given guid corresponds to the given type. 15595450Sbrendan * This is used to check for hot spares (INUSE or not), and level 2 cache 15605450Sbrendan * devices. 15612468Sek110237 */ 15622468Sek110237 static boolean_t 15635450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 15642468Sek110237 { 15655450Sbrendan uint64_t target_guid; 15662468Sek110237 nvlist_t *nvroot; 15675450Sbrendan nvlist_t **list; 15685450Sbrendan uint_t count; 15692468Sek110237 int i; 15702468Sek110237 15712468Sek110237 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 15722468Sek110237 &nvroot) == 0); 15735450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 15745450Sbrendan for (i = 0; i < count; i++) { 15755450Sbrendan verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 15765450Sbrendan &target_guid) == 0); 15775450Sbrendan if (guid == target_guid) 15782468Sek110237 return (B_TRUE); 15792468Sek110237 } 15802468Sek110237 } 15812468Sek110237 15822468Sek110237 return (B_FALSE); 15831544Seschrock } 15841544Seschrock 1585789Sahrens /* 15864451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 15874451Seschrock * ZFS_ONLINE_* flags. 1588789Sahrens */ 1589789Sahrens int 15904451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 15914451Seschrock vdev_state_t *newstate) 1592789Sahrens { 1593789Sahrens zfs_cmd_t zc = { 0 }; 1594789Sahrens char msg[1024]; 15952082Seschrock nvlist_t *tgt; 15965450Sbrendan boolean_t avail_spare, l2cache; 15972082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1598789Sahrens 15991544Seschrock (void) snprintf(msg, sizeof (msg), 16001544Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1601789Sahrens 16021544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16037326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 16047326SEric.Schrock@Sun.COM NULL)) == NULL) 16052082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1606789Sahrens 16072468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 16082468Sek110237 16095450Sbrendan if (avail_spare || 16105450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 16112082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16122082Seschrock 16134451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 16144451Seschrock zc.zc_obj = flags; 16154451Seschrock 16164543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 16174451Seschrock return (zpool_standard_error(hdl, errno, msg)); 16184451Seschrock 16194451Seschrock *newstate = zc.zc_cookie; 16204451Seschrock return (0); 1621789Sahrens } 1622789Sahrens 1623789Sahrens /* 1624789Sahrens * Take the specified vdev offline 1625789Sahrens */ 1626789Sahrens int 16274451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1628789Sahrens { 1629789Sahrens zfs_cmd_t zc = { 0 }; 1630789Sahrens char msg[1024]; 16312082Seschrock nvlist_t *tgt; 16325450Sbrendan boolean_t avail_spare, l2cache; 16332082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1634789Sahrens 16351544Seschrock (void) snprintf(msg, sizeof (msg), 16361544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 16371544Seschrock 1638789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16397326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 16407326SEric.Schrock@Sun.COM NULL)) == NULL) 16412082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 16422082Seschrock 16432468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 16442468Sek110237 16455450Sbrendan if (avail_spare || 16465450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 16472082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16482082Seschrock 16494451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 16504451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 16511485Slling 16524543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1653789Sahrens return (0); 1654789Sahrens 1655789Sahrens switch (errno) { 16562082Seschrock case EBUSY: 1657789Sahrens 1658789Sahrens /* 1659789Sahrens * There are no other replicas of this device. 1660789Sahrens */ 16612082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 16622082Seschrock 16632082Seschrock default: 16642082Seschrock return (zpool_standard_error(hdl, errno, msg)); 16652082Seschrock } 16662082Seschrock } 1667789Sahrens 16682082Seschrock /* 16694451Seschrock * Mark the given vdev faulted. 16704451Seschrock */ 16714451Seschrock int 16724451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 16734451Seschrock { 16744451Seschrock zfs_cmd_t zc = { 0 }; 16754451Seschrock char msg[1024]; 16764451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 16774451Seschrock 16784451Seschrock (void) snprintf(msg, sizeof (msg), 16794451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 16804451Seschrock 16814451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16824451Seschrock zc.zc_guid = guid; 16834451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 16844451Seschrock 16854451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 16864451Seschrock return (0); 16874451Seschrock 16884451Seschrock switch (errno) { 16894451Seschrock case EBUSY: 16904451Seschrock 16914451Seschrock /* 16924451Seschrock * There are no other replicas of this device. 16934451Seschrock */ 16944451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 16954451Seschrock 16964451Seschrock default: 16974451Seschrock return (zpool_standard_error(hdl, errno, msg)); 16984451Seschrock } 16994451Seschrock 17004451Seschrock } 17014451Seschrock 17024451Seschrock /* 17034451Seschrock * Mark the given vdev degraded. 17044451Seschrock */ 17054451Seschrock int 17064451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 17074451Seschrock { 17084451Seschrock zfs_cmd_t zc = { 0 }; 17094451Seschrock char msg[1024]; 17104451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17114451Seschrock 17124451Seschrock (void) snprintf(msg, sizeof (msg), 17134451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 17144451Seschrock 17154451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17164451Seschrock zc.zc_guid = guid; 17174451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 17184451Seschrock 17194451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 17204451Seschrock return (0); 17214451Seschrock 17224451Seschrock return (zpool_standard_error(hdl, errno, msg)); 17234451Seschrock } 17244451Seschrock 17254451Seschrock /* 17262082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 17272082Seschrock * a hot spare. 17282082Seschrock */ 17292082Seschrock static boolean_t 17302082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 17312082Seschrock { 17322082Seschrock nvlist_t **child; 17332082Seschrock uint_t c, children; 17342082Seschrock char *type; 17352082Seschrock 17362082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 17372082Seschrock &children) == 0) { 17382082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 17392082Seschrock &type) == 0); 17402082Seschrock 17412082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 17422082Seschrock children == 2 && child[which] == tgt) 17432082Seschrock return (B_TRUE); 17442082Seschrock 17452082Seschrock for (c = 0; c < children; c++) 17462082Seschrock if (is_replacing_spare(child[c], tgt, which)) 17472082Seschrock return (B_TRUE); 1748789Sahrens } 17492082Seschrock 17502082Seschrock return (B_FALSE); 1751789Sahrens } 1752789Sahrens 1753789Sahrens /* 1754789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 17554527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 1756789Sahrens */ 1757789Sahrens int 1758789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 1759789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1760789Sahrens { 1761789Sahrens zfs_cmd_t zc = { 0 }; 1762789Sahrens char msg[1024]; 1763789Sahrens int ret; 17642082Seschrock nvlist_t *tgt; 17657326SEric.Schrock@Sun.COM boolean_t avail_spare, l2cache, islog; 17667326SEric.Schrock@Sun.COM uint64_t val; 17677041Seschrock char *path, *newname; 17682082Seschrock nvlist_t **child; 17692082Seschrock uint_t children; 17702082Seschrock nvlist_t *config_root; 17712082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17727965SGeorge.Wilson@Sun.COM boolean_t rootpool = pool_is_bootable(zhp); 1773789Sahrens 17741544Seschrock if (replacing) 17751544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 17761544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 17771544Seschrock else 17781544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 17791544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 17801544Seschrock 17817965SGeorge.Wilson@Sun.COM /* 17827965SGeorge.Wilson@Sun.COM * If this is a root pool, make sure that we're not attaching an 17837965SGeorge.Wilson@Sun.COM * EFI labeled device. 17847965SGeorge.Wilson@Sun.COM */ 17857965SGeorge.Wilson@Sun.COM if (rootpool && pool_uses_efi(nvroot)) { 17867965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17877965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root pools.")); 17887965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 17897965SGeorge.Wilson@Sun.COM } 17907965SGeorge.Wilson@Sun.COM 1791789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17927326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 17937326SEric.Schrock@Sun.COM &islog)) == 0) 17942082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 17952082Seschrock 17962468Sek110237 if (avail_spare) 17972082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 17982082Seschrock 17995450Sbrendan if (l2cache) 18005450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 18015450Sbrendan 18022082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 18032082Seschrock zc.zc_cookie = replacing; 18042082Seschrock 18052082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 18062082Seschrock &child, &children) != 0 || children != 1) { 18072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18082082Seschrock "new device must be a single disk")); 18092082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 18101544Seschrock } 18112082Seschrock 18122082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 18132082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 18142082Seschrock 18157041Seschrock if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL) 18167041Seschrock return (-1); 18177041Seschrock 18182082Seschrock /* 18192082Seschrock * If the target is a hot spare that has been swapped in, we can only 18202082Seschrock * replace it with another hot spare. 18212082Seschrock */ 18222082Seschrock if (replacing && 18232082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 18247326SEric.Schrock@Sun.COM (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 18257326SEric.Schrock@Sun.COM NULL) == NULL || !avail_spare) && 18267326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 1)) { 18272082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18282082Seschrock "can only be replaced by another hot spare")); 18297041Seschrock free(newname); 18302082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 18312082Seschrock } 18322082Seschrock 18332082Seschrock /* 18342082Seschrock * If we are attempting to replace a spare, it canot be applied to an 18352082Seschrock * already spared device. 18362082Seschrock */ 18372082Seschrock if (replacing && 18382082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 18397326SEric.Schrock@Sun.COM zpool_find_vdev(zhp, newname, &avail_spare, 18407326SEric.Schrock@Sun.COM &l2cache, NULL) != NULL && avail_spare && 18417326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 0)) { 18422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18432082Seschrock "device has already been replaced with a spare")); 18447041Seschrock free(newname); 18452082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 18462082Seschrock } 1847789Sahrens 18487041Seschrock free(newname); 18497041Seschrock 18505094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 18512082Seschrock return (-1); 1852789Sahrens 18534543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1854789Sahrens 18552676Seschrock zcmd_free_nvlists(&zc); 1856789Sahrens 18577965SGeorge.Wilson@Sun.COM if (ret == 0) { 18587965SGeorge.Wilson@Sun.COM if (rootpool) { 18597965SGeorge.Wilson@Sun.COM /* 18607965SGeorge.Wilson@Sun.COM * XXX - This should be removed once we can 18617965SGeorge.Wilson@Sun.COM * automatically install the bootblocks on the 18627965SGeorge.Wilson@Sun.COM * newly attached disk. 18637965SGeorge.Wilson@Sun.COM */ 18647965SGeorge.Wilson@Sun.COM (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please " 18657965SGeorge.Wilson@Sun.COM "be sure to invoke %s to make '%s' bootable.\n"), 18667965SGeorge.Wilson@Sun.COM BOOTCMD, new_disk); 18677965SGeorge.Wilson@Sun.COM } 1868789Sahrens return (0); 18697965SGeorge.Wilson@Sun.COM } 1870789Sahrens 1871789Sahrens switch (errno) { 18721544Seschrock case ENOTSUP: 1873789Sahrens /* 1874789Sahrens * Can't attach to or replace this type of vdev. 1875789Sahrens */ 18764527Sperrin if (replacing) { 18777326SEric.Schrock@Sun.COM if (islog) 18784527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18794527Sperrin "cannot replace a log with a spare")); 18804527Sperrin else 18814527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18824527Sperrin "cannot replace a replacing device")); 18834527Sperrin } else { 18842082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18852082Seschrock "can only attach to mirrors and top-level " 18862082Seschrock "disks")); 18874527Sperrin } 18882082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1889789Sahrens break; 1890789Sahrens 18911544Seschrock case EINVAL: 1892789Sahrens /* 1893789Sahrens * The new device must be a single disk. 1894789Sahrens */ 18952082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18962082Seschrock "new device must be a single disk")); 18972082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1898789Sahrens break; 1899789Sahrens 19001544Seschrock case EBUSY: 19012082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 19022082Seschrock new_disk); 19032082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1904789Sahrens break; 1905789Sahrens 19061544Seschrock case EOVERFLOW: 1907789Sahrens /* 1908789Sahrens * The new device is too small. 1909789Sahrens */ 19102082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19112082Seschrock "device is too small")); 19122082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1913789Sahrens break; 1914789Sahrens 19151544Seschrock case EDOM: 1916789Sahrens /* 1917789Sahrens * The new device has a different alignment requirement. 1918789Sahrens */ 19192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19202082Seschrock "devices have different sector alignment")); 19212082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1922789Sahrens break; 1923789Sahrens 19241544Seschrock case ENAMETOOLONG: 1925789Sahrens /* 1926789Sahrens * The resulting top-level vdev spec won't fit in the label. 1927789Sahrens */ 19282082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 1929789Sahrens break; 1930789Sahrens 19311544Seschrock default: 19322082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1933789Sahrens } 1934789Sahrens 19352082Seschrock return (-1); 1936789Sahrens } 1937789Sahrens 1938789Sahrens /* 1939789Sahrens * Detach the specified device. 1940789Sahrens */ 1941789Sahrens int 1942789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 1943789Sahrens { 1944789Sahrens zfs_cmd_t zc = { 0 }; 1945789Sahrens char msg[1024]; 19462082Seschrock nvlist_t *tgt; 19475450Sbrendan boolean_t avail_spare, l2cache; 19482082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1949789Sahrens 19501544Seschrock (void) snprintf(msg, sizeof (msg), 19511544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 19521544Seschrock 1953789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 19547326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 19557326SEric.Schrock@Sun.COM NULL)) == 0) 19562082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1957789Sahrens 19582468Sek110237 if (avail_spare) 19592082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 19602082Seschrock 19615450Sbrendan if (l2cache) 19625450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 19635450Sbrendan 19642082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 19652082Seschrock 19664543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 1967789Sahrens return (0); 1968789Sahrens 1969789Sahrens switch (errno) { 1970789Sahrens 19711544Seschrock case ENOTSUP: 1972789Sahrens /* 1973789Sahrens * Can't detach from this type of vdev. 1974789Sahrens */ 19752082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 19762082Seschrock "applicable to mirror and replacing vdevs")); 19772082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 1978789Sahrens break; 1979789Sahrens 19801544Seschrock case EBUSY: 1981789Sahrens /* 1982789Sahrens * There are no other replicas of this device. 1983789Sahrens */ 19842082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 1985789Sahrens break; 1986789Sahrens 19871544Seschrock default: 19882082Seschrock (void) zpool_standard_error(hdl, errno, msg); 19891544Seschrock } 19901544Seschrock 19912082Seschrock return (-1); 19922082Seschrock } 19932082Seschrock 19942082Seschrock /* 19955450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 19965450Sbrendan * and level 2 cache devices. 19972082Seschrock */ 19982082Seschrock int 19992082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 20002082Seschrock { 20012082Seschrock zfs_cmd_t zc = { 0 }; 20022082Seschrock char msg[1024]; 20032082Seschrock nvlist_t *tgt; 20045450Sbrendan boolean_t avail_spare, l2cache; 20052082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 20062082Seschrock 20072082Seschrock (void) snprintf(msg, sizeof (msg), 20082082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 20092082Seschrock 20102082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20117326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 20127326SEric.Schrock@Sun.COM NULL)) == 0) 20132082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20142082Seschrock 20155450Sbrendan if (!avail_spare && !l2cache) { 20162082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20175450Sbrendan "only inactive hot spares or cache devices " 20185450Sbrendan "can be removed")); 20192082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20202082Seschrock } 20212082Seschrock 20222082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 20232082Seschrock 20244543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 20252082Seschrock return (0); 20262082Seschrock 20272082Seschrock return (zpool_standard_error(hdl, errno, msg)); 20281544Seschrock } 20291544Seschrock 20301544Seschrock /* 20311544Seschrock * Clear the errors for the pool, or the particular device if specified. 20321544Seschrock */ 20331544Seschrock int 20341544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path) 20351544Seschrock { 20361544Seschrock zfs_cmd_t zc = { 0 }; 20371544Seschrock char msg[1024]; 20382082Seschrock nvlist_t *tgt; 20395450Sbrendan boolean_t avail_spare, l2cache; 20402082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 20411544Seschrock 20421544Seschrock if (path) 20431544Seschrock (void) snprintf(msg, sizeof (msg), 20441544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 20452676Seschrock path); 20461544Seschrock else 20471544Seschrock (void) snprintf(msg, sizeof (msg), 20481544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 20491544Seschrock zhp->zpool_name); 20501544Seschrock 20511544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20522082Seschrock if (path) { 20535450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 20547326SEric.Schrock@Sun.COM &l2cache, NULL)) == 0) 20552082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20562082Seschrock 20575450Sbrendan /* 20585450Sbrendan * Don't allow error clearing for hot spares. Do allow 20595450Sbrendan * error clearing for l2cache devices. 20605450Sbrendan */ 20612468Sek110237 if (avail_spare) 20622082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 20632082Seschrock 20642082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 20652082Seschrock &zc.zc_guid) == 0); 20661544Seschrock } 20671544Seschrock 20684543Smarks if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 20691544Seschrock return (0); 20701544Seschrock 20712082Seschrock return (zpool_standard_error(hdl, errno, msg)); 2072789Sahrens } 2073789Sahrens 20743126Sahl /* 20754451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 20764451Seschrock */ 20774451Seschrock int 20784451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 20794451Seschrock { 20804451Seschrock zfs_cmd_t zc = { 0 }; 20814451Seschrock char msg[1024]; 20824451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 20834451Seschrock 20844451Seschrock (void) snprintf(msg, sizeof (msg), 20854451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 20864451Seschrock guid); 20874451Seschrock 20884451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20894451Seschrock zc.zc_guid = guid; 20904451Seschrock 20914451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 20924451Seschrock return (0); 20934451Seschrock 20944451Seschrock return (zpool_standard_error(hdl, errno, msg)); 20954451Seschrock } 20964451Seschrock 20974451Seschrock /* 20983126Sahl * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 20993126Sahl * hierarchy. 21003126Sahl */ 21013126Sahl int 21023126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 21033126Sahl void *data) 2104789Sahrens { 21053126Sahl libzfs_handle_t *hdl = zhp->zpool_hdl; 21063126Sahl char (*paths)[MAXPATHLEN]; 21073126Sahl size_t size = 4; 21083126Sahl int curr, fd, base, ret = 0; 21093126Sahl DIR *dirp; 21103126Sahl struct dirent *dp; 21113126Sahl struct stat st; 21123126Sahl 21133126Sahl if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 21143126Sahl return (errno == ENOENT ? 0 : -1); 21153126Sahl 21163126Sahl if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 21173126Sahl int err = errno; 21183126Sahl (void) close(base); 21193126Sahl return (err == ENOENT ? 0 : -1); 21203126Sahl } 2121789Sahrens 2122789Sahrens /* 21233126Sahl * Oddly this wasn't a directory -- ignore that failure since we 21243126Sahl * know there are no links lower in the (non-existant) hierarchy. 2125789Sahrens */ 21263126Sahl if (!S_ISDIR(st.st_mode)) { 21273126Sahl (void) close(base); 21283126Sahl return (0); 21293126Sahl } 21303126Sahl 21313126Sahl if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 21323126Sahl (void) close(base); 21333126Sahl return (-1); 2134789Sahrens } 2135789Sahrens 21363126Sahl (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 21373126Sahl curr = 0; 21383126Sahl 21393126Sahl while (curr >= 0) { 21403126Sahl if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 21413126Sahl goto err; 21423126Sahl 21433126Sahl if (S_ISDIR(st.st_mode)) { 21443126Sahl if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 21453126Sahl goto err; 21463126Sahl 21473126Sahl if ((dirp = fdopendir(fd)) == NULL) { 21483126Sahl (void) close(fd); 21493126Sahl goto err; 21503126Sahl } 21513126Sahl 21523126Sahl while ((dp = readdir(dirp)) != NULL) { 21533126Sahl if (dp->d_name[0] == '.') 21543126Sahl continue; 21553126Sahl 21563126Sahl if (curr + 1 == size) { 21573126Sahl paths = zfs_realloc(hdl, paths, 21583126Sahl size * sizeof (paths[0]), 21593126Sahl size * 2 * sizeof (paths[0])); 21603126Sahl if (paths == NULL) { 21613126Sahl (void) closedir(dirp); 21623126Sahl (void) close(fd); 21633126Sahl goto err; 21643126Sahl } 21653126Sahl 21663126Sahl size *= 2; 21673126Sahl } 21683126Sahl 21693126Sahl (void) strlcpy(paths[curr + 1], paths[curr], 21703126Sahl sizeof (paths[curr + 1])); 21713126Sahl (void) strlcat(paths[curr], "/", 21723126Sahl sizeof (paths[curr])); 21733126Sahl (void) strlcat(paths[curr], dp->d_name, 21743126Sahl sizeof (paths[curr])); 21753126Sahl curr++; 21763126Sahl } 21773126Sahl 21783126Sahl (void) closedir(dirp); 21793126Sahl 21803126Sahl } else { 21813126Sahl if ((ret = cb(paths[curr], data)) != 0) 21823126Sahl break; 21833126Sahl } 21843126Sahl 21853126Sahl curr--; 21863126Sahl } 21873126Sahl 21883126Sahl free(paths); 21893126Sahl (void) close(base); 21903126Sahl 21913126Sahl return (ret); 21923126Sahl 21933126Sahl err: 21943126Sahl free(paths); 21953126Sahl (void) close(base); 21963126Sahl return (-1); 21973126Sahl } 21983126Sahl 21993126Sahl typedef struct zvol_cb { 22003126Sahl zpool_handle_t *zcb_pool; 22013126Sahl boolean_t zcb_create; 22023126Sahl } zvol_cb_t; 22033126Sahl 22043126Sahl /*ARGSUSED*/ 22053126Sahl static int 22063126Sahl do_zvol_create(zfs_handle_t *zhp, void *data) 22073126Sahl { 22084657Sahrens int ret = 0; 22093126Sahl 22104657Sahrens if (ZFS_IS_VOLUME(zhp)) { 22113126Sahl (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 22124657Sahrens ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 22134657Sahrens } 22143126Sahl 22154657Sahrens if (ret == 0) 22164657Sahrens ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 2217789Sahrens 2218789Sahrens zfs_close(zhp); 22193126Sahl 2220789Sahrens return (ret); 2221789Sahrens } 2222789Sahrens 2223789Sahrens /* 2224789Sahrens * Iterate over all zvols in the pool and make any necessary minor nodes. 2225789Sahrens */ 2226789Sahrens int 2227789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp) 2228789Sahrens { 2229789Sahrens zfs_handle_t *zfp; 2230789Sahrens int ret; 2231789Sahrens 2232789Sahrens /* 2233789Sahrens * If the pool is unavailable, just return success. 2234789Sahrens */ 22352082Seschrock if ((zfp = make_dataset_handle(zhp->zpool_hdl, 22362082Seschrock zhp->zpool_name)) == NULL) 2237789Sahrens return (0); 2238789Sahrens 22394657Sahrens ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 2240789Sahrens 2241789Sahrens zfs_close(zfp); 2242789Sahrens return (ret); 2243789Sahrens } 2244789Sahrens 22453126Sahl static int 22463126Sahl do_zvol_remove(const char *dataset, void *data) 22473126Sahl { 22483126Sahl zpool_handle_t *zhp = data; 22493126Sahl 22503126Sahl return (zvol_remove_link(zhp->zpool_hdl, dataset)); 22513126Sahl } 22523126Sahl 2253789Sahrens /* 22543126Sahl * Iterate over all zvols in the pool and remove any minor nodes. We iterate 22553126Sahl * by examining the /dev links so that a corrupted pool doesn't impede this 22563126Sahl * operation. 2257789Sahrens */ 2258789Sahrens int 2259789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp) 2260789Sahrens { 22613126Sahl return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 2262789Sahrens } 22631354Seschrock 22641354Seschrock /* 22651354Seschrock * Convert from a devid string to a path. 22661354Seschrock */ 22671354Seschrock static char * 22681354Seschrock devid_to_path(char *devid_str) 22691354Seschrock { 22701354Seschrock ddi_devid_t devid; 22711354Seschrock char *minor; 22721354Seschrock char *path; 22731354Seschrock devid_nmlist_t *list = NULL; 22741354Seschrock int ret; 22751354Seschrock 22761354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 22771354Seschrock return (NULL); 22781354Seschrock 22791354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 22801354Seschrock 22811354Seschrock devid_str_free(minor); 22821354Seschrock devid_free(devid); 22831354Seschrock 22841354Seschrock if (ret != 0) 22851354Seschrock return (NULL); 22861354Seschrock 22872082Seschrock if ((path = strdup(list[0].devname)) == NULL) 22882082Seschrock return (NULL); 22892082Seschrock 22901354Seschrock devid_free_nmlist(list); 22911354Seschrock 22921354Seschrock return (path); 22931354Seschrock } 22941354Seschrock 22951354Seschrock /* 22961354Seschrock * Convert from a path to a devid string. 22971354Seschrock */ 22981354Seschrock static char * 22991354Seschrock path_to_devid(const char *path) 23001354Seschrock { 23011354Seschrock int fd; 23021354Seschrock ddi_devid_t devid; 23031354Seschrock char *minor, *ret; 23041354Seschrock 23051354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 23061354Seschrock return (NULL); 23071354Seschrock 23081354Seschrock minor = NULL; 23091354Seschrock ret = NULL; 23101354Seschrock if (devid_get(fd, &devid) == 0) { 23111354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 23121354Seschrock ret = devid_str_encode(devid, minor); 23131354Seschrock if (minor != NULL) 23141354Seschrock devid_str_free(minor); 23151354Seschrock devid_free(devid); 23161354Seschrock } 23171354Seschrock (void) close(fd); 23181354Seschrock 23191354Seschrock return (ret); 23201354Seschrock } 23211354Seschrock 23221354Seschrock /* 23231354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 23241354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 23251354Seschrock * type 'zpool status', and we'll display the correct information anyway. 23261354Seschrock */ 23271354Seschrock static void 23281354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 23291354Seschrock { 23301354Seschrock zfs_cmd_t zc = { 0 }; 23311354Seschrock 23321354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 23332676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 23341354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 23351544Seschrock &zc.zc_guid) == 0); 23361354Seschrock 23372082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 23381354Seschrock } 23391354Seschrock 23401354Seschrock /* 23411354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 23421354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 23431354Seschrock * We also check if this is a whole disk, in which case we strip off the 23441354Seschrock * trailing 's0' slice name. 23451354Seschrock * 23461354Seschrock * This routine is also responsible for identifying when disks have been 23471354Seschrock * reconfigured in a new location. The kernel will have opened the device by 23481354Seschrock * devid, but the path will still refer to the old location. To catch this, we 23491354Seschrock * first do a path -> devid translation (which is fast for the common case). If 23501354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 23511354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 23521354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 23531354Seschrock * of these checks. 23541354Seschrock */ 23551354Seschrock char * 23562082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 23571354Seschrock { 23581354Seschrock char *path, *devid; 23591544Seschrock uint64_t value; 23601544Seschrock char buf[64]; 23614451Seschrock vdev_stat_t *vs; 23624451Seschrock uint_t vsc; 23631354Seschrock 23641544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 23651544Seschrock &value) == 0) { 23661544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 23671544Seschrock &value) == 0); 23682856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 23692856Snd150628 (u_longlong_t)value); 23701544Seschrock path = buf; 23711544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 23721354Seschrock 23734451Seschrock /* 23744451Seschrock * If the device is dead (faulted, offline, etc) then don't 23754451Seschrock * bother opening it. Otherwise we may be forcing the user to 23764451Seschrock * open a misbehaving device, which can have undesirable 23774451Seschrock * effects. 23784451Seschrock */ 23794451Seschrock if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 23804451Seschrock (uint64_t **)&vs, &vsc) != 0 || 23814451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 23824451Seschrock zhp != NULL && 23831354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 23841354Seschrock /* 23851354Seschrock * Determine if the current path is correct. 23861354Seschrock */ 23871354Seschrock char *newdevid = path_to_devid(path); 23881354Seschrock 23891354Seschrock if (newdevid == NULL || 23901354Seschrock strcmp(devid, newdevid) != 0) { 23911354Seschrock char *newpath; 23921354Seschrock 23931354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 23941354Seschrock /* 23951354Seschrock * Update the path appropriately. 23961354Seschrock */ 23971354Seschrock set_path(zhp, nv, newpath); 23982082Seschrock if (nvlist_add_string(nv, 23992082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 24002082Seschrock verify(nvlist_lookup_string(nv, 24012082Seschrock ZPOOL_CONFIG_PATH, 24022082Seschrock &path) == 0); 24031354Seschrock free(newpath); 24041354Seschrock } 24051354Seschrock } 24061354Seschrock 24072082Seschrock if (newdevid) 24082082Seschrock devid_str_free(newdevid); 24091354Seschrock } 24101354Seschrock 24111354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 24121354Seschrock path += 9; 24131354Seschrock 24141354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 24151544Seschrock &value) == 0 && value) { 24162082Seschrock char *tmp = zfs_strdup(hdl, path); 24172082Seschrock if (tmp == NULL) 24182082Seschrock return (NULL); 24191354Seschrock tmp[strlen(path) - 2] = '\0'; 24201354Seschrock return (tmp); 24211354Seschrock } 24221354Seschrock } else { 24231354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 24242082Seschrock 24252082Seschrock /* 24262082Seschrock * If it's a raidz device, we need to stick in the parity level. 24272082Seschrock */ 24282082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 24292082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 24302082Seschrock &value) == 0); 24312082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 24322856Snd150628 (u_longlong_t)value); 24332082Seschrock path = buf; 24342082Seschrock } 24351354Seschrock } 24361354Seschrock 24372082Seschrock return (zfs_strdup(hdl, path)); 24381354Seschrock } 24391544Seschrock 24401544Seschrock static int 24411544Seschrock zbookmark_compare(const void *a, const void *b) 24421544Seschrock { 24431544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 24441544Seschrock } 24451544Seschrock 24461544Seschrock /* 24471544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 24481544Seschrock * caller. 24491544Seschrock */ 24501544Seschrock int 24513444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 24521544Seschrock { 24531544Seschrock zfs_cmd_t zc = { 0 }; 24541544Seschrock uint64_t count; 24552676Seschrock zbookmark_t *zb = NULL; 24563444Sek110237 int i; 24571544Seschrock 24581544Seschrock /* 24591544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 24601544Seschrock * has increased, allocate more space and continue until we get the 24611544Seschrock * entire list. 24621544Seschrock */ 24631544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 24641544Seschrock &count) == 0); 24654820Sek110237 if (count == 0) 24664820Sek110237 return (0); 24672676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 24682856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 24692082Seschrock return (-1); 24702676Seschrock zc.zc_nvlist_dst_size = count; 24711544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 24721544Seschrock for (;;) { 24732082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 24742082Seschrock &zc) != 0) { 24752676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 24761544Seschrock if (errno == ENOMEM) { 24773823Svb160487 count = zc.zc_nvlist_dst_size; 24782676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 24793823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 24803823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 24812082Seschrock return (-1); 24821544Seschrock } else { 24831544Seschrock return (-1); 24841544Seschrock } 24851544Seschrock } else { 24861544Seschrock break; 24871544Seschrock } 24881544Seschrock } 24891544Seschrock 24901544Seschrock /* 24911544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 24921544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 24932676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 24941544Seschrock * _not_ copied as part of the process. So we point the start of our 24951544Seschrock * array appropriate and decrement the total number of elements. 24961544Seschrock */ 24972676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 24982676Seschrock zc.zc_nvlist_dst_size; 24992676Seschrock count -= zc.zc_nvlist_dst_size; 25001544Seschrock 25011544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 25021544Seschrock 25033444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 25041544Seschrock 25051544Seschrock /* 25063444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 25071544Seschrock */ 25081544Seschrock for (i = 0; i < count; i++) { 25091544Seschrock nvlist_t *nv; 25101544Seschrock 25113700Sek110237 /* ignoring zb_blkid and zb_level for now */ 25123700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 25133700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 25141544Seschrock continue; 25151544Seschrock 25163444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 25173444Sek110237 goto nomem; 25183444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 25193444Sek110237 zb[i].zb_objset) != 0) { 25203444Sek110237 nvlist_free(nv); 25212082Seschrock goto nomem; 25223444Sek110237 } 25233444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 25243444Sek110237 zb[i].zb_object) != 0) { 25253444Sek110237 nvlist_free(nv); 25263444Sek110237 goto nomem; 25271544Seschrock } 25283444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 25293444Sek110237 nvlist_free(nv); 25303444Sek110237 goto nomem; 25313444Sek110237 } 25323444Sek110237 nvlist_free(nv); 25331544Seschrock } 25341544Seschrock 25353265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 25361544Seschrock return (0); 25372082Seschrock 25382082Seschrock nomem: 25392676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 25402082Seschrock return (no_memory(zhp->zpool_hdl)); 25411544Seschrock } 25421760Seschrock 25431760Seschrock /* 25441760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 25451760Seschrock */ 25461760Seschrock int 25475094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 25481760Seschrock { 25491760Seschrock zfs_cmd_t zc = { 0 }; 25502082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25511760Seschrock 25521760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 25535094Slling zc.zc_cookie = new_version; 25545094Slling 25554543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 25563237Slling return (zpool_standard_error_fmt(hdl, errno, 25572082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 25582082Seschrock zhp->zpool_name)); 25591760Seschrock return (0); 25601760Seschrock } 25612926Sek110237 25624988Sek110237 void 25634988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 25644988Sek110237 char *history_str) 25654988Sek110237 { 25664988Sek110237 int i; 25674988Sek110237 25684988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 25694988Sek110237 for (i = 1; i < argc; i++) { 25704988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 25714988Sek110237 HIS_MAX_RECORD_LEN) 25724988Sek110237 break; 25734988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 25744988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 25754988Sek110237 } 25764988Sek110237 } 25774988Sek110237 25782926Sek110237 /* 25794988Sek110237 * Stage command history for logging. 25802926Sek110237 */ 25814988Sek110237 int 25824988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 25832926Sek110237 { 25844988Sek110237 if (history_str == NULL) 25854988Sek110237 return (EINVAL); 25864988Sek110237 25874988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 25884988Sek110237 return (EINVAL); 25892926Sek110237 25904715Sek110237 if (hdl->libzfs_log_str != NULL) 25914543Smarks free(hdl->libzfs_log_str); 25922926Sek110237 25934988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 25944988Sek110237 return (no_memory(hdl)); 25954543Smarks 25964988Sek110237 return (0); 25972926Sek110237 } 25982926Sek110237 25992926Sek110237 /* 26002926Sek110237 * Perform ioctl to get some command history of a pool. 26012926Sek110237 * 26022926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 26032926Sek110237 * logical offset of the history buffer to start reading from. 26042926Sek110237 * 26052926Sek110237 * Upon return, 'off' is the next logical offset to read from and 26062926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 26072926Sek110237 */ 26082926Sek110237 static int 26092926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 26102926Sek110237 { 26112926Sek110237 zfs_cmd_t zc = { 0 }; 26122926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 26132926Sek110237 26142926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 26152926Sek110237 26162926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 26172926Sek110237 zc.zc_history_len = *len; 26182926Sek110237 zc.zc_history_offset = *off; 26192926Sek110237 26202926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 26212926Sek110237 switch (errno) { 26222926Sek110237 case EPERM: 26233237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 26243237Slling dgettext(TEXT_DOMAIN, 26252926Sek110237 "cannot show history for pool '%s'"), 26262926Sek110237 zhp->zpool_name)); 26272926Sek110237 case ENOENT: 26283237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 26292926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 26302926Sek110237 "'%s'"), zhp->zpool_name)); 26313863Sek110237 case ENOTSUP: 26323863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 26333863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 26343863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 26352926Sek110237 default: 26363237Slling return (zpool_standard_error_fmt(hdl, errno, 26372926Sek110237 dgettext(TEXT_DOMAIN, 26382926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 26392926Sek110237 } 26402926Sek110237 } 26412926Sek110237 26422926Sek110237 *len = zc.zc_history_len; 26432926Sek110237 *off = zc.zc_history_offset; 26442926Sek110237 26452926Sek110237 return (0); 26462926Sek110237 } 26472926Sek110237 26482926Sek110237 /* 26492926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 26502926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 26512926Sek110237 * processed as there wasn't a complete record. 26522926Sek110237 */ 26532926Sek110237 static int 26542926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 26552926Sek110237 nvlist_t ***records, uint_t *numrecords) 26562926Sek110237 { 26572926Sek110237 uint64_t reclen; 26582926Sek110237 nvlist_t *nv; 26592926Sek110237 int i; 26602926Sek110237 26612926Sek110237 while (bytes_read > sizeof (reclen)) { 26622926Sek110237 26632926Sek110237 /* get length of packed record (stored as little endian) */ 26642926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 26652926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 26662926Sek110237 26672926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 26682926Sek110237 break; 26692926Sek110237 26702926Sek110237 /* unpack record */ 26712926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 26722926Sek110237 return (ENOMEM); 26732926Sek110237 bytes_read -= sizeof (reclen) + reclen; 26742926Sek110237 buf += sizeof (reclen) + reclen; 26752926Sek110237 26762926Sek110237 /* add record to nvlist array */ 26772926Sek110237 (*numrecords)++; 26782926Sek110237 if (ISP2(*numrecords + 1)) { 26792926Sek110237 *records = realloc(*records, 26802926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 26812926Sek110237 } 26822926Sek110237 (*records)[*numrecords - 1] = nv; 26832926Sek110237 } 26842926Sek110237 26852926Sek110237 *leftover = bytes_read; 26862926Sek110237 return (0); 26872926Sek110237 } 26882926Sek110237 26892926Sek110237 #define HIS_BUF_LEN (128*1024) 26902926Sek110237 26912926Sek110237 /* 26922926Sek110237 * Retrieve the command history of a pool. 26932926Sek110237 */ 26942926Sek110237 int 26952926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 26962926Sek110237 { 26972926Sek110237 char buf[HIS_BUF_LEN]; 26982926Sek110237 uint64_t off = 0; 26992926Sek110237 nvlist_t **records = NULL; 27002926Sek110237 uint_t numrecords = 0; 27012926Sek110237 int err, i; 27022926Sek110237 27032926Sek110237 do { 27042926Sek110237 uint64_t bytes_read = sizeof (buf); 27052926Sek110237 uint64_t leftover; 27062926Sek110237 27072926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 27082926Sek110237 break; 27092926Sek110237 27102926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 27112926Sek110237 if (!bytes_read) 27122926Sek110237 break; 27132926Sek110237 27142926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 27152926Sek110237 &leftover, &records, &numrecords)) != 0) 27162926Sek110237 break; 27172926Sek110237 off -= leftover; 27182926Sek110237 27192926Sek110237 /* CONSTCOND */ 27202926Sek110237 } while (1); 27212926Sek110237 27222926Sek110237 if (!err) { 27232926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 27242926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 27252926Sek110237 records, numrecords) == 0); 27262926Sek110237 } 27272926Sek110237 for (i = 0; i < numrecords; i++) 27282926Sek110237 nvlist_free(records[i]); 27292926Sek110237 free(records); 27302926Sek110237 27312926Sek110237 return (err); 27322926Sek110237 } 27333444Sek110237 27343444Sek110237 void 27353444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 27363444Sek110237 char *pathname, size_t len) 27373444Sek110237 { 27383444Sek110237 zfs_cmd_t zc = { 0 }; 27393444Sek110237 boolean_t mounted = B_FALSE; 27403444Sek110237 char *mntpnt = NULL; 27413444Sek110237 char dsname[MAXNAMELEN]; 27423444Sek110237 27433444Sek110237 if (dsobj == 0) { 27443444Sek110237 /* special case for the MOS */ 27453444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 27463444Sek110237 return; 27473444Sek110237 } 27483444Sek110237 27493444Sek110237 /* get the dataset's name */ 27503444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 27513444Sek110237 zc.zc_obj = dsobj; 27523444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 27533444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 27543444Sek110237 /* just write out a path of two object numbers */ 27553444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 27563444Sek110237 dsobj, obj); 27573444Sek110237 return; 27583444Sek110237 } 27593444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 27603444Sek110237 27613444Sek110237 /* find out if the dataset is mounted */ 27623444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 27633444Sek110237 27643444Sek110237 /* get the corrupted object's path */ 27653444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 27663444Sek110237 zc.zc_obj = obj; 27673444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 27683444Sek110237 &zc) == 0) { 27693444Sek110237 if (mounted) { 27703444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 27713444Sek110237 zc.zc_value); 27723444Sek110237 } else { 27733444Sek110237 (void) snprintf(pathname, len, "%s:%s", 27743444Sek110237 dsname, zc.zc_value); 27753444Sek110237 } 27763444Sek110237 } else { 27773444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 27783444Sek110237 } 27793444Sek110237 free(mntpnt); 27803444Sek110237 } 27813912Slling 27824276Staylor #define RDISK_ROOT "/dev/rdsk" 27834276Staylor #define BACKUP_SLICE "s2" 27844276Staylor /* 27854276Staylor * Don't start the slice at the default block of 34; many storage 27864276Staylor * devices will use a stripe width of 128k, so start there instead. 27874276Staylor */ 27884276Staylor #define NEW_START_BLOCK 256 27894276Staylor 27904276Staylor /* 27917042Sgw25295 * Read the EFI label from the config, if a label does not exist then 27927042Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 27937042Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 27947042Sgw25295 * partition. 27957042Sgw25295 */ 27967042Sgw25295 static int 27977042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 27987042Sgw25295 { 27997042Sgw25295 char *path; 28007042Sgw25295 int fd; 28017042Sgw25295 char diskname[MAXPATHLEN]; 28027042Sgw25295 int err = -1; 28037042Sgw25295 28047042Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 28057042Sgw25295 return (err); 28067042Sgw25295 28077042Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 28087042Sgw25295 strrchr(path, '/')); 28097042Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 28107042Sgw25295 struct dk_gpt *vtoc; 28117042Sgw25295 28127042Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 28137042Sgw25295 if (sb != NULL) 28147042Sgw25295 *sb = vtoc->efi_parts[0].p_start; 28157042Sgw25295 efi_free(vtoc); 28167042Sgw25295 } 28177042Sgw25295 (void) close(fd); 28187042Sgw25295 } 28197042Sgw25295 return (err); 28207042Sgw25295 } 28217042Sgw25295 28227042Sgw25295 /* 28234276Staylor * determine where a partition starts on a disk in the current 28244276Staylor * configuration 28254276Staylor */ 28264276Staylor static diskaddr_t 28274276Staylor find_start_block(nvlist_t *config) 28284276Staylor { 28294276Staylor nvlist_t **child; 28304276Staylor uint_t c, children; 28314276Staylor diskaddr_t sb = MAXOFFSET_T; 28324276Staylor uint64_t wholedisk; 28334276Staylor 28344276Staylor if (nvlist_lookup_nvlist_array(config, 28354276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 28364276Staylor if (nvlist_lookup_uint64(config, 28374276Staylor ZPOOL_CONFIG_WHOLE_DISK, 28384276Staylor &wholedisk) != 0 || !wholedisk) { 28394276Staylor return (MAXOFFSET_T); 28404276Staylor } 28417042Sgw25295 if (read_efi_label(config, &sb) < 0) 28427042Sgw25295 sb = MAXOFFSET_T; 28434276Staylor return (sb); 28444276Staylor } 28454276Staylor 28464276Staylor for (c = 0; c < children; c++) { 28474276Staylor sb = find_start_block(child[c]); 28484276Staylor if (sb != MAXOFFSET_T) { 28494276Staylor return (sb); 28504276Staylor } 28514276Staylor } 28524276Staylor return (MAXOFFSET_T); 28534276Staylor } 28544276Staylor 28554276Staylor /* 28564276Staylor * Label an individual disk. The name provided is the short name, 28574276Staylor * stripped of any leading /dev path. 28584276Staylor */ 28594276Staylor int 28604276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 28614276Staylor { 28624276Staylor char path[MAXPATHLEN]; 28634276Staylor struct dk_gpt *vtoc; 28644276Staylor int fd; 28654276Staylor size_t resv = EFI_MIN_RESV_SIZE; 28664276Staylor uint64_t slice_size; 28674276Staylor diskaddr_t start_block; 28684276Staylor char errbuf[1024]; 28694276Staylor 28706289Smmusante /* prepare an error message just in case */ 28716289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 28726289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 28736289Smmusante 28744276Staylor if (zhp) { 28754276Staylor nvlist_t *nvroot; 28764276Staylor 28777965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp)) { 28787965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28797965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root " 28807965SGeorge.Wilson@Sun.COM "pools.")); 28817965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 28827965SGeorge.Wilson@Sun.COM } 28837965SGeorge.Wilson@Sun.COM 28844276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 28854276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 28864276Staylor 28874276Staylor if (zhp->zpool_start_block == 0) 28884276Staylor start_block = find_start_block(nvroot); 28894276Staylor else 28904276Staylor start_block = zhp->zpool_start_block; 28914276Staylor zhp->zpool_start_block = start_block; 28924276Staylor } else { 28934276Staylor /* new pool */ 28944276Staylor start_block = NEW_START_BLOCK; 28954276Staylor } 28964276Staylor 28974276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 28984276Staylor BACKUP_SLICE); 28994276Staylor 29004276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 29014276Staylor /* 29024276Staylor * This shouldn't happen. We've long since verified that this 29034276Staylor * is a valid device. 29044276Staylor */ 29056289Smmusante zfs_error_aux(hdl, 29066289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 29074276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 29084276Staylor } 29094276Staylor 29104276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 29114276Staylor /* 29124276Staylor * The only way this can fail is if we run out of memory, or we 29134276Staylor * were unable to read the disk's capacity 29144276Staylor */ 29154276Staylor if (errno == ENOMEM) 29164276Staylor (void) no_memory(hdl); 29174276Staylor 29184276Staylor (void) close(fd); 29196289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29206289Smmusante "unable to read disk capacity"), name); 29214276Staylor 29224276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 29234276Staylor } 29244276Staylor 29254276Staylor slice_size = vtoc->efi_last_u_lba + 1; 29264276Staylor slice_size -= EFI_MIN_RESV_SIZE; 29274276Staylor if (start_block == MAXOFFSET_T) 29284276Staylor start_block = NEW_START_BLOCK; 29294276Staylor slice_size -= start_block; 29304276Staylor 29314276Staylor vtoc->efi_parts[0].p_start = start_block; 29324276Staylor vtoc->efi_parts[0].p_size = slice_size; 29334276Staylor 29344276Staylor /* 29354276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 29364276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 29374276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 29384276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 29394276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 29404276Staylor * can get, in the absence of V_OTHER. 29414276Staylor */ 29424276Staylor vtoc->efi_parts[0].p_tag = V_USR; 29434276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 29444276Staylor 29454276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 29464276Staylor vtoc->efi_parts[8].p_size = resv; 29474276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 29484276Staylor 29494276Staylor if (efi_write(fd, vtoc) != 0) { 29504276Staylor /* 29514276Staylor * Some block drivers (like pcata) may not support EFI 29524276Staylor * GPT labels. Print out a helpful error message dir- 29534276Staylor * ecting the user to manually label the disk and give 29544276Staylor * a specific slice. 29554276Staylor */ 29564276Staylor (void) close(fd); 29574276Staylor efi_free(vtoc); 29584276Staylor 29594276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29606289Smmusante "try using fdisk(1M) and then provide a specific slice")); 29614276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 29624276Staylor } 29634276Staylor 29644276Staylor (void) close(fd); 29654276Staylor efi_free(vtoc); 29664276Staylor return (0); 29674276Staylor } 29686423Sgw25295 29696423Sgw25295 static boolean_t 29706423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 29716423Sgw25295 { 29726423Sgw25295 char *type; 29736423Sgw25295 nvlist_t **child; 29746423Sgw25295 uint_t children, c; 29756423Sgw25295 29766423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 29776423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 29786423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 29796423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 29806423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 29816423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29826423Sgw25295 "vdev type '%s' is not supported"), type); 29836423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 29846423Sgw25295 return (B_FALSE); 29856423Sgw25295 } 29866423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 29876423Sgw25295 &child, &children) == 0) { 29886423Sgw25295 for (c = 0; c < children; c++) { 29896423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 29906423Sgw25295 return (B_FALSE); 29916423Sgw25295 } 29926423Sgw25295 } 29936423Sgw25295 return (B_TRUE); 29946423Sgw25295 } 29956423Sgw25295 29966423Sgw25295 /* 29976423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 29986423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 29996423Sgw25295 */ 30006423Sgw25295 int 30016423Sgw25295 zvol_check_dump_config(char *arg) 30026423Sgw25295 { 30036423Sgw25295 zpool_handle_t *zhp = NULL; 30046423Sgw25295 nvlist_t *config, *nvroot; 30056423Sgw25295 char *p, *volname; 30066423Sgw25295 nvlist_t **top; 30076423Sgw25295 uint_t toplevels; 30086423Sgw25295 libzfs_handle_t *hdl; 30096423Sgw25295 char errbuf[1024]; 30106423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 30116423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 30126423Sgw25295 int ret = 1; 30136423Sgw25295 30146423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 30156423Sgw25295 return (-1); 30166423Sgw25295 } 30176423Sgw25295 30186423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30196423Sgw25295 "dump is not supported on device '%s'"), arg); 30206423Sgw25295 30216423Sgw25295 if ((hdl = libzfs_init()) == NULL) 30226423Sgw25295 return (1); 30236423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 30246423Sgw25295 30256423Sgw25295 volname = arg + pathlen; 30266423Sgw25295 30276423Sgw25295 /* check the configuration of the pool */ 30286423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 30296423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30306423Sgw25295 "malformed dataset name")); 30316423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 30326423Sgw25295 return (1); 30336423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 30346423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30356423Sgw25295 "dataset name is too long")); 30366423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 30376423Sgw25295 return (1); 30386423Sgw25295 } else { 30396423Sgw25295 (void) strncpy(poolname, volname, p - volname); 30406423Sgw25295 poolname[p - volname] = '\0'; 30416423Sgw25295 } 30426423Sgw25295 30436423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 30446423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30456423Sgw25295 "could not open pool '%s'"), poolname); 30466423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 30476423Sgw25295 goto out; 30486423Sgw25295 } 30496423Sgw25295 config = zpool_get_config(zhp, NULL); 30506423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 30516423Sgw25295 &nvroot) != 0) { 30526423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30536423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 30546423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 30556423Sgw25295 goto out; 30566423Sgw25295 } 30576423Sgw25295 30586423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 30596423Sgw25295 &top, &toplevels) == 0); 30606423Sgw25295 if (toplevels != 1) { 30616423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30626423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 30636423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 30646423Sgw25295 goto out; 30656423Sgw25295 } 30666423Sgw25295 30676423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 30686423Sgw25295 goto out; 30696423Sgw25295 } 30706423Sgw25295 ret = 0; 30716423Sgw25295 30726423Sgw25295 out: 30736423Sgw25295 if (zhp) 30746423Sgw25295 zpool_close(zhp); 30756423Sgw25295 libzfs_fini(hdl); 30766423Sgw25295 return (ret); 30776423Sgw25295 } 3078