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 /* 238525SEric.Schrock@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 273126Sahl #include <alloca.h> 28789Sahrens #include <assert.h> 29789Sahrens #include <ctype.h> 30789Sahrens #include <errno.h> 31789Sahrens #include <devid.h> 323126Sahl #include <dirent.h> 33789Sahrens #include <fcntl.h> 34789Sahrens #include <libintl.h> 35789Sahrens #include <stdio.h> 36789Sahrens #include <stdlib.h> 373126Sahl #include <strings.h> 38789Sahrens #include <unistd.h> 397184Stimh #include <zone.h> 404276Staylor #include <sys/efi_partition.h> 414276Staylor #include <sys/vtoc.h> 42789Sahrens #include <sys/zfs_ioctl.h> 431544Seschrock #include <sys/zio.h> 442926Sek110237 #include <strings.h> 45789Sahrens 46789Sahrens #include "zfs_namecheck.h" 473912Slling #include "zfs_prop.h" 48789Sahrens #include "libzfs_impl.h" 49789Sahrens 507042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 515094Slling 527965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64) 537965SGeorge.Wilson@Sun.COM #define BOOTCMD "installgrub(1M)" 547965SGeorge.Wilson@Sun.COM #else 557965SGeorge.Wilson@Sun.COM #define BOOTCMD "installboot(1M)" 567965SGeorge.Wilson@Sun.COM #endif 577965SGeorge.Wilson@Sun.COM 585094Slling /* 595094Slling * ==================================================================== 605094Slling * zpool property functions 615094Slling * ==================================================================== 625094Slling */ 635094Slling 645094Slling static int 655094Slling zpool_get_all_props(zpool_handle_t *zhp) 665094Slling { 675094Slling zfs_cmd_t zc = { 0 }; 685094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 695094Slling 705094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 715094Slling 725094Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 735094Slling return (-1); 745094Slling 755094Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 765094Slling if (errno == ENOMEM) { 775094Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 785094Slling zcmd_free_nvlists(&zc); 795094Slling return (-1); 805094Slling } 815094Slling } else { 825094Slling zcmd_free_nvlists(&zc); 835094Slling return (-1); 845094Slling } 855094Slling } 865094Slling 875094Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 885094Slling zcmd_free_nvlists(&zc); 895094Slling return (-1); 905094Slling } 915094Slling 925094Slling zcmd_free_nvlists(&zc); 935094Slling 945094Slling return (0); 955094Slling } 965094Slling 975094Slling static int 985094Slling zpool_props_refresh(zpool_handle_t *zhp) 995094Slling { 1005094Slling nvlist_t *old_props; 1015094Slling 1025094Slling old_props = zhp->zpool_props; 1035094Slling 1045094Slling if (zpool_get_all_props(zhp) != 0) 1055094Slling return (-1); 1065094Slling 1075094Slling nvlist_free(old_props); 1085094Slling return (0); 1095094Slling } 1105094Slling 1115094Slling static char * 1125094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 1135094Slling zprop_source_t *src) 1145094Slling { 1155094Slling nvlist_t *nv, *nvl; 1165094Slling uint64_t ival; 1175094Slling char *value; 1185094Slling zprop_source_t source; 1195094Slling 1205094Slling nvl = zhp->zpool_props; 1215094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1225094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 1235094Slling source = ival; 1245094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1255094Slling } else { 1265094Slling source = ZPROP_SRC_DEFAULT; 1275094Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 1285094Slling value = "-"; 1295094Slling } 1305094Slling 1315094Slling if (src) 1325094Slling *src = source; 1335094Slling 1345094Slling return (value); 1355094Slling } 1365094Slling 1375094Slling uint64_t 1385094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 1395094Slling { 1405094Slling nvlist_t *nv, *nvl; 1415094Slling uint64_t value; 1425094Slling zprop_source_t source; 1435094Slling 1447294Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 1457294Sperrin /* 1467294Sperrin * zpool_get_all_props() has most likely failed because 1477294Sperrin * the pool is faulted, but if all we need is the top level 1487294Sperrin * vdev's guid then get it from the zhp config nvlist. 1497294Sperrin */ 1507294Sperrin if ((prop == ZPOOL_PROP_GUID) && 1517294Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 1527294Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 1537294Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 1547294Sperrin == 0)) { 1557294Sperrin return (value); 1567294Sperrin } 1575094Slling return (zpool_prop_default_numeric(prop)); 1587294Sperrin } 1595094Slling 1605094Slling nvl = zhp->zpool_props; 1615094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1625094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 1635094Slling source = value; 1645094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1655094Slling } else { 1665094Slling source = ZPROP_SRC_DEFAULT; 1675094Slling value = zpool_prop_default_numeric(prop); 1685094Slling } 1695094Slling 1705094Slling if (src) 1715094Slling *src = source; 1725094Slling 1735094Slling return (value); 1745094Slling } 1755094Slling 1765094Slling /* 1775094Slling * Map VDEV STATE to printed strings. 1785094Slling */ 1795094Slling char * 1805094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 1815094Slling { 1825094Slling switch (state) { 1835094Slling case VDEV_STATE_CLOSED: 1845094Slling case VDEV_STATE_OFFLINE: 1855094Slling return (gettext("OFFLINE")); 1865094Slling case VDEV_STATE_REMOVED: 1875094Slling return (gettext("REMOVED")); 1885094Slling case VDEV_STATE_CANT_OPEN: 1897294Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 1905094Slling return (gettext("FAULTED")); 1915094Slling else 1925094Slling return (gettext("UNAVAIL")); 1935094Slling case VDEV_STATE_FAULTED: 1945094Slling return (gettext("FAULTED")); 1955094Slling case VDEV_STATE_DEGRADED: 1965094Slling return (gettext("DEGRADED")); 1975094Slling case VDEV_STATE_HEALTHY: 1985094Slling return (gettext("ONLINE")); 1995094Slling } 2005094Slling 2015094Slling return (gettext("UNKNOWN")); 2025094Slling } 2035094Slling 2045094Slling /* 2055094Slling * Get a zpool property value for 'prop' and return the value in 2065094Slling * a pre-allocated buffer. 2075094Slling */ 2085094Slling int 2095094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 2105094Slling zprop_source_t *srctype) 2115094Slling { 2125094Slling uint64_t intval; 2135094Slling const char *strval; 2145094Slling zprop_source_t src = ZPROP_SRC_NONE; 2155094Slling nvlist_t *nvroot; 2165094Slling vdev_stat_t *vs; 2175094Slling uint_t vsc; 2185094Slling 2195094Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 2208525SEric.Schrock@Sun.COM switch (prop) { 2218525SEric.Schrock@Sun.COM case ZPOOL_PROP_NAME: 2225094Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 2238525SEric.Schrock@Sun.COM break; 2248525SEric.Schrock@Sun.COM 2258525SEric.Schrock@Sun.COM case ZPOOL_PROP_HEALTH: 2265094Slling (void) strlcpy(buf, "FAULTED", len); 2278525SEric.Schrock@Sun.COM break; 2288525SEric.Schrock@Sun.COM 2298525SEric.Schrock@Sun.COM case ZPOOL_PROP_GUID: 2308525SEric.Schrock@Sun.COM intval = zpool_get_prop_int(zhp, prop, &src); 2318525SEric.Schrock@Sun.COM (void) snprintf(buf, len, "%llu", intval); 2328525SEric.Schrock@Sun.COM break; 2338525SEric.Schrock@Sun.COM 2348525SEric.Schrock@Sun.COM case ZPOOL_PROP_ALTROOT: 2358525SEric.Schrock@Sun.COM case ZPOOL_PROP_CACHEFILE: 2368525SEric.Schrock@Sun.COM if (zhp->zpool_props != NULL || 2378525SEric.Schrock@Sun.COM zpool_get_all_props(zhp) == 0) { 2388525SEric.Schrock@Sun.COM (void) strlcpy(buf, 2398525SEric.Schrock@Sun.COM zpool_get_prop_string(zhp, prop, &src), 2408525SEric.Schrock@Sun.COM len); 2418525SEric.Schrock@Sun.COM if (srctype != NULL) 2428525SEric.Schrock@Sun.COM *srctype = src; 2438525SEric.Schrock@Sun.COM return (0); 2448525SEric.Schrock@Sun.COM } 2458525SEric.Schrock@Sun.COM /* FALLTHROUGH */ 2468525SEric.Schrock@Sun.COM default: 2475094Slling (void) strlcpy(buf, "-", len); 2488525SEric.Schrock@Sun.COM break; 2498525SEric.Schrock@Sun.COM } 2508525SEric.Schrock@Sun.COM 2518525SEric.Schrock@Sun.COM if (srctype != NULL) 2528525SEric.Schrock@Sun.COM *srctype = src; 2535094Slling return (0); 2545094Slling } 2555094Slling 2565094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 2575094Slling prop != ZPOOL_PROP_NAME) 2585094Slling return (-1); 2595094Slling 2605094Slling switch (zpool_prop_get_type(prop)) { 2615094Slling case PROP_TYPE_STRING: 2625094Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 2635094Slling len); 2645094Slling break; 2655094Slling 2665094Slling case PROP_TYPE_NUMBER: 2675094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2685094Slling 2695094Slling switch (prop) { 2705094Slling case ZPOOL_PROP_SIZE: 2715094Slling case ZPOOL_PROP_USED: 2725094Slling case ZPOOL_PROP_AVAILABLE: 2735094Slling (void) zfs_nicenum(intval, buf, len); 2745094Slling break; 2755094Slling 2765094Slling case ZPOOL_PROP_CAPACITY: 2775094Slling (void) snprintf(buf, len, "%llu%%", 2785094Slling (u_longlong_t)intval); 2795094Slling break; 2805094Slling 2815094Slling case ZPOOL_PROP_HEALTH: 2825094Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2835094Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2845094Slling verify(nvlist_lookup_uint64_array(nvroot, 2855094Slling ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 2865094Slling 2875094Slling (void) strlcpy(buf, zpool_state_to_name(intval, 2885094Slling vs->vs_aux), len); 2895094Slling break; 2905094Slling default: 2915094Slling (void) snprintf(buf, len, "%llu", intval); 2925094Slling } 2935094Slling break; 2945094Slling 2955094Slling case PROP_TYPE_INDEX: 2965094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2975094Slling if (zpool_prop_index_to_string(prop, intval, &strval) 2985094Slling != 0) 2995094Slling return (-1); 3005094Slling (void) strlcpy(buf, strval, len); 3015094Slling break; 3025094Slling 3035094Slling default: 3045094Slling abort(); 3055094Slling } 3065094Slling 3075094Slling if (srctype) 3085094Slling *srctype = src; 3095094Slling 3105094Slling return (0); 3115094Slling } 3125094Slling 3135094Slling /* 3145094Slling * Check if the bootfs name has the same pool name as it is set to. 3155094Slling * Assuming bootfs is a valid dataset name. 3165094Slling */ 3175094Slling static boolean_t 3185094Slling bootfs_name_valid(const char *pool, char *bootfs) 3195094Slling { 3205094Slling int len = strlen(pool); 3215094Slling 3227300SEric.Taylor@Sun.COM if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 3235094Slling return (B_FALSE); 3245094Slling 3255094Slling if (strncmp(pool, bootfs, len) == 0 && 3265094Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 3275094Slling return (B_TRUE); 3285094Slling 3295094Slling return (B_FALSE); 3305094Slling } 3315094Slling 3325094Slling /* 3337042Sgw25295 * Inspect the configuration to determine if any of the devices contain 3347042Sgw25295 * an EFI label. 3357042Sgw25295 */ 3367042Sgw25295 static boolean_t 3377042Sgw25295 pool_uses_efi(nvlist_t *config) 3387042Sgw25295 { 3397042Sgw25295 nvlist_t **child; 3407042Sgw25295 uint_t c, children; 3417042Sgw25295 3427042Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3437042Sgw25295 &child, &children) != 0) 3447042Sgw25295 return (read_efi_label(config, NULL) >= 0); 3457042Sgw25295 3467042Sgw25295 for (c = 0; c < children; c++) { 3477042Sgw25295 if (pool_uses_efi(child[c])) 3487042Sgw25295 return (B_TRUE); 3497042Sgw25295 } 3507042Sgw25295 return (B_FALSE); 3517042Sgw25295 } 3527042Sgw25295 3537965SGeorge.Wilson@Sun.COM static boolean_t 3547965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp) 3557965SGeorge.Wilson@Sun.COM { 3567965SGeorge.Wilson@Sun.COM char bootfs[ZPOOL_MAXNAMELEN]; 3577965SGeorge.Wilson@Sun.COM 3587965SGeorge.Wilson@Sun.COM return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 3597965SGeorge.Wilson@Sun.COM sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 3607965SGeorge.Wilson@Sun.COM sizeof (bootfs)) != 0); 3617965SGeorge.Wilson@Sun.COM } 3627965SGeorge.Wilson@Sun.COM 3637965SGeorge.Wilson@Sun.COM 3647042Sgw25295 /* 3655094Slling * Given an nvlist of zpool properties to be set, validate that they are 3665094Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 3675094Slling * specified as strings. 3685094Slling */ 3695094Slling static nvlist_t * 3707184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 3715094Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 3725094Slling { 3735094Slling nvpair_t *elem; 3745094Slling nvlist_t *retprops; 3755094Slling zpool_prop_t prop; 3765094Slling char *strval; 3775094Slling uint64_t intval; 3785363Seschrock char *slash; 3795363Seschrock struct stat64 statbuf; 3807042Sgw25295 zpool_handle_t *zhp; 3817042Sgw25295 nvlist_t *nvroot; 3825094Slling 3835094Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 3845094Slling (void) no_memory(hdl); 3855094Slling return (NULL); 3865094Slling } 3875094Slling 3885094Slling elem = NULL; 3895094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3905094Slling const char *propname = nvpair_name(elem); 3915094Slling 3925094Slling /* 3935094Slling * Make sure this property is valid and applies to this type. 3945094Slling */ 3955094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 3965094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3975094Slling "invalid property '%s'"), propname); 3985094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3995094Slling goto error; 4005094Slling } 4015094Slling 4025094Slling if (zpool_prop_readonly(prop)) { 4035094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4045094Slling "is readonly"), propname); 4055094Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 4065094Slling goto error; 4075094Slling } 4085094Slling 4095094Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 4105094Slling &strval, &intval, errbuf) != 0) 4115094Slling goto error; 4125094Slling 4135094Slling /* 4145094Slling * Perform additional checking for specific properties. 4155094Slling */ 4165094Slling switch (prop) { 4175094Slling case ZPOOL_PROP_VERSION: 4185094Slling if (intval < version || intval > SPA_VERSION) { 4195094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4205094Slling "property '%s' number %d is invalid."), 4215094Slling propname, intval); 4225094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4235094Slling goto error; 4245094Slling } 4255094Slling break; 4265094Slling 4275094Slling case ZPOOL_PROP_BOOTFS: 4285094Slling if (create_or_import) { 4295094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4305094Slling "property '%s' cannot be set at creation " 4315094Slling "or import time"), propname); 4325094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4335094Slling goto error; 4345094Slling } 4355094Slling 4365094Slling if (version < SPA_VERSION_BOOTFS) { 4375094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4385094Slling "pool must be upgraded to support " 4395094Slling "'%s' property"), propname); 4405094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4415094Slling goto error; 4425094Slling } 4435094Slling 4445094Slling /* 4455094Slling * bootfs property value has to be a dataset name and 4465094Slling * the dataset has to be in the same pool as it sets to. 4475094Slling */ 4485094Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 4495094Slling strval)) { 4505094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4515094Slling "is an invalid name"), strval); 4525094Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4535094Slling goto error; 4545094Slling } 4557042Sgw25295 4567042Sgw25295 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 4577042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4587042Sgw25295 "could not open pool '%s'"), poolname); 4597042Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4607042Sgw25295 goto error; 4617042Sgw25295 } 4627042Sgw25295 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 4637042Sgw25295 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4647042Sgw25295 4657042Sgw25295 /* 4667042Sgw25295 * bootfs property cannot be set on a disk which has 4677042Sgw25295 * been EFI labeled. 4687042Sgw25295 */ 4697042Sgw25295 if (pool_uses_efi(nvroot)) { 4707042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4717042Sgw25295 "property '%s' not supported on " 4727042Sgw25295 "EFI labeled devices"), propname); 4737042Sgw25295 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 4747042Sgw25295 zpool_close(zhp); 4757042Sgw25295 goto error; 4767042Sgw25295 } 4777042Sgw25295 zpool_close(zhp); 4785094Slling break; 4795094Slling 4805094Slling case ZPOOL_PROP_ALTROOT: 4815094Slling if (!create_or_import) { 4825094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4835094Slling "property '%s' can only be set during pool " 4845094Slling "creation or import"), propname); 4855094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4865094Slling goto error; 4875094Slling } 4885094Slling 4895094Slling if (strval[0] != '/') { 4905094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4915094Slling "bad alternate root '%s'"), strval); 4925094Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4935094Slling goto error; 4945094Slling } 4955094Slling break; 4965363Seschrock 4975363Seschrock case ZPOOL_PROP_CACHEFILE: 4985363Seschrock if (strval[0] == '\0') 4995363Seschrock break; 5005363Seschrock 5015363Seschrock if (strcmp(strval, "none") == 0) 5025363Seschrock break; 5035363Seschrock 5045363Seschrock if (strval[0] != '/') { 5055363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5065363Seschrock "property '%s' must be empty, an " 5075363Seschrock "absolute path, or 'none'"), propname); 5085363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5095363Seschrock goto error; 5105363Seschrock } 5115363Seschrock 5125363Seschrock slash = strrchr(strval, '/'); 5135363Seschrock 5145363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5155363Seschrock strcmp(slash, "/..") == 0) { 5165363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5175363Seschrock "'%s' is not a valid file"), strval); 5185363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5195363Seschrock goto error; 5205363Seschrock } 5215363Seschrock 5225363Seschrock *slash = '\0'; 5235363Seschrock 5245621Seschrock if (strval[0] != '\0' && 5255621Seschrock (stat64(strval, &statbuf) != 0 || 5265621Seschrock !S_ISDIR(statbuf.st_mode))) { 5275363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5285363Seschrock "'%s' is not a valid directory"), 5295363Seschrock strval); 5305363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5315363Seschrock goto error; 5325363Seschrock } 5335363Seschrock 5345363Seschrock *slash = '/'; 5355363Seschrock break; 5365094Slling } 5375094Slling } 5385094Slling 5395094Slling return (retprops); 5405094Slling error: 5415094Slling nvlist_free(retprops); 5425094Slling return (NULL); 5435094Slling } 5445094Slling 5455094Slling /* 5465094Slling * Set zpool property : propname=propval. 5475094Slling */ 5485094Slling int 5495094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 5505094Slling { 5515094Slling zfs_cmd_t zc = { 0 }; 5525094Slling int ret = -1; 5535094Slling char errbuf[1024]; 5545094Slling nvlist_t *nvl = NULL; 5555094Slling nvlist_t *realprops; 5565094Slling uint64_t version; 5575094Slling 5585094Slling (void) snprintf(errbuf, sizeof (errbuf), 5595094Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 5605094Slling zhp->zpool_name); 5615094Slling 5625094Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5635094Slling return (no_memory(zhp->zpool_hdl)); 5645094Slling 5655094Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 5665094Slling nvlist_free(nvl); 5675094Slling return (no_memory(zhp->zpool_hdl)); 5685094Slling } 5695094Slling 5705094Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5717184Stimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 5725094Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 5735094Slling nvlist_free(nvl); 5745094Slling return (-1); 5755094Slling } 5765094Slling 5775094Slling nvlist_free(nvl); 5785094Slling nvl = realprops; 5795094Slling 5805094Slling /* 5815094Slling * Execute the corresponding ioctl() to set this property. 5825094Slling */ 5835094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 5845094Slling 5855094Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 5865094Slling nvlist_free(nvl); 5875094Slling return (-1); 5885094Slling } 5895094Slling 5905094Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 5915094Slling 5925094Slling zcmd_free_nvlists(&zc); 5935094Slling nvlist_free(nvl); 5945094Slling 5955094Slling if (ret) 5965094Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 5975094Slling else 5985094Slling (void) zpool_props_refresh(zhp); 5995094Slling 6005094Slling return (ret); 6015094Slling } 6025094Slling 6035094Slling int 6045094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 6055094Slling { 6065094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 6075094Slling zprop_list_t *entry; 6085094Slling char buf[ZFS_MAXPROPLEN]; 6095094Slling 6105094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 6115094Slling return (-1); 6125094Slling 6135094Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 6145094Slling 6155094Slling if (entry->pl_fixed) 6165094Slling continue; 6175094Slling 6185094Slling if (entry->pl_prop != ZPROP_INVAL && 6195094Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 6205094Slling NULL) == 0) { 6215094Slling if (strlen(buf) > entry->pl_width) 6225094Slling entry->pl_width = strlen(buf); 6235094Slling } 6245094Slling } 6255094Slling 6265094Slling return (0); 6275094Slling } 6285094Slling 6295094Slling 630789Sahrens /* 631789Sahrens * Validate the given pool name, optionally putting an extended error message in 632789Sahrens * 'buf'. 633789Sahrens */ 6346423Sgw25295 boolean_t 6352082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 636789Sahrens { 637789Sahrens namecheck_err_t why; 638789Sahrens char what; 6391773Seschrock int ret; 640789Sahrens 6411773Seschrock ret = pool_namecheck(pool, &why, &what); 6421773Seschrock 6431773Seschrock /* 6441773Seschrock * The rules for reserved pool names were extended at a later point. 6451773Seschrock * But we need to support users with existing pools that may now be 6461773Seschrock * invalid. So we only check for this expanded set of names during a 6471773Seschrock * create (or import), and only in userland. 6481773Seschrock */ 6491773Seschrock if (ret == 0 && !isopen && 6501773Seschrock (strncmp(pool, "mirror", 6) == 0 || 6511773Seschrock strncmp(pool, "raidz", 5) == 0 || 6524527Sperrin strncmp(pool, "spare", 5) == 0 || 6534527Sperrin strcmp(pool, "log") == 0)) { 6546423Sgw25295 if (hdl != NULL) 6556423Sgw25295 zfs_error_aux(hdl, 6566423Sgw25295 dgettext(TEXT_DOMAIN, "name is reserved")); 6572082Seschrock return (B_FALSE); 6581773Seschrock } 6591773Seschrock 6601773Seschrock 6611773Seschrock if (ret != 0) { 6622082Seschrock if (hdl != NULL) { 663789Sahrens switch (why) { 6641003Slling case NAME_ERR_TOOLONG: 6652082Seschrock zfs_error_aux(hdl, 6661003Slling dgettext(TEXT_DOMAIN, "name is too long")); 6671003Slling break; 6681003Slling 669789Sahrens case NAME_ERR_INVALCHAR: 6702082Seschrock zfs_error_aux(hdl, 671789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 672789Sahrens "'%c' in pool name"), what); 673789Sahrens break; 674789Sahrens 675789Sahrens case NAME_ERR_NOLETTER: 6762082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6772082Seschrock "name must begin with a letter")); 678789Sahrens break; 679789Sahrens 680789Sahrens case NAME_ERR_RESERVED: 6812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6822082Seschrock "name is reserved")); 683789Sahrens break; 684789Sahrens 685789Sahrens case NAME_ERR_DISKLIKE: 6862082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6872082Seschrock "pool name is reserved")); 688789Sahrens break; 6892856Snd150628 6902856Snd150628 case NAME_ERR_LEADING_SLASH: 6912856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6922856Snd150628 "leading slash in name")); 6932856Snd150628 break; 6942856Snd150628 6952856Snd150628 case NAME_ERR_EMPTY_COMPONENT: 6962856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6972856Snd150628 "empty component in name")); 6982856Snd150628 break; 6992856Snd150628 7002856Snd150628 case NAME_ERR_TRAILING_SLASH: 7012856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7022856Snd150628 "trailing slash in name")); 7032856Snd150628 break; 7042856Snd150628 7052856Snd150628 case NAME_ERR_MULTIPLE_AT: 7062856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7072856Snd150628 "multiple '@' delimiters in name")); 7082856Snd150628 break; 7092856Snd150628 710789Sahrens } 711789Sahrens } 7122082Seschrock return (B_FALSE); 713789Sahrens } 714789Sahrens 7152082Seschrock return (B_TRUE); 716789Sahrens } 717789Sahrens 718789Sahrens /* 719789Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 720789Sahrens * state. 721789Sahrens */ 722789Sahrens zpool_handle_t * 7232082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 724789Sahrens { 725789Sahrens zpool_handle_t *zhp; 7262142Seschrock boolean_t missing; 727789Sahrens 728789Sahrens /* 729789Sahrens * Make sure the pool name is valid. 730789Sahrens */ 7312082Seschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 7323237Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 7332082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 7342082Seschrock pool); 735789Sahrens return (NULL); 736789Sahrens } 737789Sahrens 7382082Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7392082Seschrock return (NULL); 740789Sahrens 7412082Seschrock zhp->zpool_hdl = hdl; 742789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 743789Sahrens 7442142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7452142Seschrock zpool_close(zhp); 7462142Seschrock return (NULL); 7472142Seschrock } 7482142Seschrock 7492142Seschrock if (missing) { 7505094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 7513237Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 7525094Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 7532142Seschrock zpool_close(zhp); 7542142Seschrock return (NULL); 755789Sahrens } 756789Sahrens 757789Sahrens return (zhp); 758789Sahrens } 759789Sahrens 760789Sahrens /* 761789Sahrens * Like the above, but silent on error. Used when iterating over pools (because 762789Sahrens * the configuration cache may be out of date). 763789Sahrens */ 7642142Seschrock int 7652142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 766789Sahrens { 767789Sahrens zpool_handle_t *zhp; 7682142Seschrock boolean_t missing; 769789Sahrens 7702142Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7712142Seschrock return (-1); 772789Sahrens 7732082Seschrock zhp->zpool_hdl = hdl; 774789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 775789Sahrens 7762142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7772142Seschrock zpool_close(zhp); 7782142Seschrock return (-1); 779789Sahrens } 780789Sahrens 7812142Seschrock if (missing) { 7822142Seschrock zpool_close(zhp); 7832142Seschrock *ret = NULL; 7842142Seschrock return (0); 7852142Seschrock } 7862142Seschrock 7872142Seschrock *ret = zhp; 7882142Seschrock return (0); 789789Sahrens } 790789Sahrens 791789Sahrens /* 792789Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 793789Sahrens * state. 794789Sahrens */ 795789Sahrens zpool_handle_t * 7962082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 797789Sahrens { 798789Sahrens zpool_handle_t *zhp; 799789Sahrens 8002082Seschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 801789Sahrens return (NULL); 802789Sahrens 803789Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 8043237Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 8052082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 806789Sahrens zpool_close(zhp); 807789Sahrens return (NULL); 808789Sahrens } 809789Sahrens 810789Sahrens return (zhp); 811789Sahrens } 812789Sahrens 813789Sahrens /* 814789Sahrens * Close the handle. Simply frees the memory associated with the handle. 815789Sahrens */ 816789Sahrens void 817789Sahrens zpool_close(zpool_handle_t *zhp) 818789Sahrens { 819789Sahrens if (zhp->zpool_config) 820789Sahrens nvlist_free(zhp->zpool_config); 821952Seschrock if (zhp->zpool_old_config) 822952Seschrock nvlist_free(zhp->zpool_old_config); 8233912Slling if (zhp->zpool_props) 8243912Slling nvlist_free(zhp->zpool_props); 825789Sahrens free(zhp); 826789Sahrens } 827789Sahrens 828789Sahrens /* 829789Sahrens * Return the name of the pool. 830789Sahrens */ 831789Sahrens const char * 832789Sahrens zpool_get_name(zpool_handle_t *zhp) 833789Sahrens { 834789Sahrens return (zhp->zpool_name); 835789Sahrens } 836789Sahrens 837789Sahrens 838789Sahrens /* 839789Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 840789Sahrens */ 841789Sahrens int 842789Sahrens zpool_get_state(zpool_handle_t *zhp) 843789Sahrens { 844789Sahrens return (zhp->zpool_state); 845789Sahrens } 846789Sahrens 847789Sahrens /* 848789Sahrens * Create the named pool, using the provided vdev list. It is assumed 849789Sahrens * that the consumer has already validated the contents of the nvlist, so we 850789Sahrens * don't have to worry about error semantics. 851789Sahrens */ 852789Sahrens int 8532082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8547184Stimh nvlist_t *props, nvlist_t *fsprops) 855789Sahrens { 856789Sahrens zfs_cmd_t zc = { 0 }; 8577184Stimh nvlist_t *zc_fsprops = NULL; 8587184Stimh nvlist_t *zc_props = NULL; 8592082Seschrock char msg[1024]; 8605094Slling char *altroot; 8617184Stimh int ret = -1; 8622082Seschrock 8632082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 8642082Seschrock "cannot create '%s'"), pool); 865789Sahrens 8662082Seschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 8672082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 8682082Seschrock 8695320Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 8705320Slling return (-1); 8715320Slling 8727184Stimh if (props) { 8737184Stimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8747184Stimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8757184Stimh goto create_failed; 8767184Stimh } 8775320Slling } 878789Sahrens 8797184Stimh if (fsprops) { 8807184Stimh uint64_t zoned; 8817184Stimh char *zonestr; 8827184Stimh 8837184Stimh zoned = ((nvlist_lookup_string(fsprops, 8847184Stimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8857184Stimh strcmp(zonestr, "on") == 0); 8867184Stimh 8877184Stimh if ((zc_fsprops = zfs_valid_proplist(hdl, 8887184Stimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 8897184Stimh goto create_failed; 8907184Stimh } 8917184Stimh if (!zc_props && 8927184Stimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 8937184Stimh goto create_failed; 8947184Stimh } 8957184Stimh if (nvlist_add_nvlist(zc_props, 8967184Stimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 8977184Stimh goto create_failed; 8987184Stimh } 8997184Stimh } 9007184Stimh 9017184Stimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 9027184Stimh goto create_failed; 9037184Stimh 904789Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 905789Sahrens 9067184Stimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 9075320Slling 9082676Seschrock zcmd_free_nvlists(&zc); 9097184Stimh nvlist_free(zc_props); 9107184Stimh nvlist_free(zc_fsprops); 9112082Seschrock 912789Sahrens switch (errno) { 913789Sahrens case EBUSY: 914789Sahrens /* 915789Sahrens * This can happen if the user has specified the same 916789Sahrens * device multiple times. We can't reliably detect this 917789Sahrens * until we try to add it and see we already have a 918789Sahrens * label. 919789Sahrens */ 9202082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9212082Seschrock "one or more vdevs refer to the same device")); 9222082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 923789Sahrens 924789Sahrens case EOVERFLOW: 925789Sahrens /* 9262082Seschrock * This occurs when one of the devices is below 927789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 928789Sahrens * device was the problem device since there's no 929789Sahrens * reliable way to determine device size from userland. 930789Sahrens */ 931789Sahrens { 932789Sahrens char buf[64]; 933789Sahrens 934789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 935789Sahrens 9362082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9372082Seschrock "one or more devices is less than the " 9382082Seschrock "minimum size (%s)"), buf); 939789Sahrens } 9402082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 941789Sahrens 942789Sahrens case ENOSPC: 9432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9442082Seschrock "one or more devices is out of space")); 9452082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 946789Sahrens 9475450Sbrendan case ENOTBLK: 9485450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9495450Sbrendan "cache device must be a disk or disk slice")); 9505450Sbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 9515450Sbrendan 952789Sahrens default: 9532082Seschrock return (zpool_standard_error(hdl, errno, msg)); 954789Sahrens } 955789Sahrens } 956789Sahrens 957789Sahrens /* 958789Sahrens * If this is an alternate root pool, then we automatically set the 9592676Seschrock * mountpoint of the root dataset to be '/'. 960789Sahrens */ 9615094Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 9625094Slling &altroot) == 0) { 963789Sahrens zfs_handle_t *zhp; 964789Sahrens 9655094Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 9662676Seschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 9672676Seschrock "/") == 0); 968789Sahrens 969789Sahrens zfs_close(zhp); 970789Sahrens } 971789Sahrens 9727184Stimh create_failed: 9735320Slling zcmd_free_nvlists(&zc); 9747184Stimh nvlist_free(zc_props); 9757184Stimh nvlist_free(zc_fsprops); 9767184Stimh return (ret); 977789Sahrens } 978789Sahrens 979789Sahrens /* 980789Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 981789Sahrens * datasets left in the pool. 982789Sahrens */ 983789Sahrens int 984789Sahrens zpool_destroy(zpool_handle_t *zhp) 985789Sahrens { 986789Sahrens zfs_cmd_t zc = { 0 }; 987789Sahrens zfs_handle_t *zfp = NULL; 9882082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9892082Seschrock char msg[1024]; 990789Sahrens 991789Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 9922082Seschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 9932082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 994789Sahrens return (-1); 995789Sahrens 9962856Snd150628 if (zpool_remove_zvol_links(zhp) != 0) 997789Sahrens return (-1); 998789Sahrens 999789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1000789Sahrens 10014543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 10022082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10032082Seschrock "cannot destroy '%s'"), zhp->zpool_name); 1004789Sahrens 10052082Seschrock if (errno == EROFS) { 10062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10072082Seschrock "one or more devices is read only")); 10082082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 10092082Seschrock } else { 10102082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1011789Sahrens } 1012789Sahrens 1013789Sahrens if (zfp) 1014789Sahrens zfs_close(zfp); 1015789Sahrens return (-1); 1016789Sahrens } 1017789Sahrens 1018789Sahrens if (zfp) { 1019789Sahrens remove_mountpoint(zfp); 1020789Sahrens zfs_close(zfp); 1021789Sahrens } 1022789Sahrens 1023789Sahrens return (0); 1024789Sahrens } 1025789Sahrens 1026789Sahrens /* 1027789Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1028789Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1029789Sahrens */ 1030789Sahrens int 1031789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1032789Sahrens { 10332676Seschrock zfs_cmd_t zc = { 0 }; 10342082Seschrock int ret; 10352082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10362082Seschrock char msg[1024]; 10375450Sbrendan nvlist_t **spares, **l2cache; 10385450Sbrendan uint_t nspares, nl2cache; 10392082Seschrock 10402082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10412082Seschrock "cannot add to '%s'"), zhp->zpool_name); 10422082Seschrock 10435450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10445450Sbrendan SPA_VERSION_SPARES && 10452082Seschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10462082Seschrock &spares, &nspares) == 0) { 10472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10482082Seschrock "upgraded to add hot spares")); 10492082Seschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10502082Seschrock } 1051789Sahrens 10527965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 10537965SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 10547965SGeorge.Wilson@Sun.COM uint64_t s; 10557965SGeorge.Wilson@Sun.COM 10567965SGeorge.Wilson@Sun.COM for (s = 0; s < nspares; s++) { 10577965SGeorge.Wilson@Sun.COM char *path; 10587965SGeorge.Wilson@Sun.COM 10597965SGeorge.Wilson@Sun.COM if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 10607965SGeorge.Wilson@Sun.COM &path) == 0 && pool_uses_efi(spares[s])) { 10617965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10627965SGeorge.Wilson@Sun.COM "device '%s' contains an EFI label and " 10637965SGeorge.Wilson@Sun.COM "cannot be used on root pools."), 10647965SGeorge.Wilson@Sun.COM zpool_vdev_name(hdl, NULL, spares[s])); 10657965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 10667965SGeorge.Wilson@Sun.COM } 10677965SGeorge.Wilson@Sun.COM } 10687965SGeorge.Wilson@Sun.COM } 10697965SGeorge.Wilson@Sun.COM 10705450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10715450Sbrendan SPA_VERSION_L2CACHE && 10725450Sbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 10735450Sbrendan &l2cache, &nl2cache) == 0) { 10745450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10755450Sbrendan "upgraded to add cache devices")); 10765450Sbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10775450Sbrendan } 10785450Sbrendan 10795094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 10802082Seschrock return (-1); 1081789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1082789Sahrens 10834543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1084789Sahrens switch (errno) { 1085789Sahrens case EBUSY: 1086789Sahrens /* 1087789Sahrens * This can happen if the user has specified the same 1088789Sahrens * device multiple times. We can't reliably detect this 1089789Sahrens * until we try to add it and see we already have a 1090789Sahrens * label. 1091789Sahrens */ 10922082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10932082Seschrock "one or more vdevs refer to the same device")); 10942082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1095789Sahrens break; 1096789Sahrens 1097789Sahrens case EOVERFLOW: 1098789Sahrens /* 1099789Sahrens * This occurrs when one of the devices is below 1100789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1101789Sahrens * device was the problem device since there's no 1102789Sahrens * reliable way to determine device size from userland. 1103789Sahrens */ 1104789Sahrens { 1105789Sahrens char buf[64]; 1106789Sahrens 1107789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1108789Sahrens 11092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11102082Seschrock "device is less than the minimum " 11112082Seschrock "size (%s)"), buf); 1112789Sahrens } 11132082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 11142082Seschrock break; 11152082Seschrock 11162082Seschrock case ENOTSUP: 11172082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11184527Sperrin "pool must be upgraded to add these vdevs")); 11192082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1120789Sahrens break; 1121789Sahrens 11223912Slling case EDOM: 11233912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11244527Sperrin "root pool can not have multiple vdevs" 11254527Sperrin " or separate logs")); 11263912Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 11273912Slling break; 11283912Slling 11295450Sbrendan case ENOTBLK: 11305450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11315450Sbrendan "cache device must be a disk or disk slice")); 11325450Sbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 11335450Sbrendan break; 11345450Sbrendan 1135789Sahrens default: 11362082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1137789Sahrens } 1138789Sahrens 11392082Seschrock ret = -1; 11402082Seschrock } else { 11412082Seschrock ret = 0; 1142789Sahrens } 1143789Sahrens 11442676Seschrock zcmd_free_nvlists(&zc); 1145789Sahrens 11462082Seschrock return (ret); 1147789Sahrens } 1148789Sahrens 1149789Sahrens /* 1150789Sahrens * Exports the pool from the system. The caller must ensure that there are no 1151789Sahrens * mounted datasets in the pool. 1152789Sahrens */ 1153789Sahrens int 11548211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1155789Sahrens { 1156789Sahrens zfs_cmd_t zc = { 0 }; 11577214Slling char msg[1024]; 1158789Sahrens 1159789Sahrens if (zpool_remove_zvol_links(zhp) != 0) 1160789Sahrens return (-1); 1161789Sahrens 11627214Slling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 11637214Slling "cannot export '%s'"), zhp->zpool_name); 11647214Slling 1165789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 11667214Slling zc.zc_cookie = force; 11678211SGeorge.Wilson@Sun.COM zc.zc_guid = hardforce; 11687214Slling 11697214Slling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 11707214Slling switch (errno) { 11717214Slling case EXDEV: 11727214Slling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 11737214Slling "use '-f' to override the following errors:\n" 11747214Slling "'%s' has an active shared spare which could be" 11757214Slling " used by other pools once '%s' is exported."), 11767214Slling zhp->zpool_name, zhp->zpool_name); 11777214Slling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 11787214Slling msg)); 11797214Slling default: 11807214Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 11817214Slling msg)); 11827214Slling } 11837214Slling } 11847214Slling 1185789Sahrens return (0); 1186789Sahrens } 1187789Sahrens 11888211SGeorge.Wilson@Sun.COM int 11898211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force) 11908211SGeorge.Wilson@Sun.COM { 11918211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, force, B_FALSE)); 11928211SGeorge.Wilson@Sun.COM } 11938211SGeorge.Wilson@Sun.COM 11948211SGeorge.Wilson@Sun.COM int 11958211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp) 11968211SGeorge.Wilson@Sun.COM { 11978211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 11988211SGeorge.Wilson@Sun.COM } 11998211SGeorge.Wilson@Sun.COM 1200789Sahrens /* 12015094Slling * zpool_import() is a contracted interface. Should be kept the same 12025094Slling * if possible. 12035094Slling * 12045094Slling * Applications should use zpool_import_props() to import a pool with 12055094Slling * new properties value to be set. 1206789Sahrens */ 1207789Sahrens int 12082082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 12095094Slling char *altroot) 12105094Slling { 12115094Slling nvlist_t *props = NULL; 12125094Slling int ret; 12135094Slling 12145094Slling if (altroot != NULL) { 12155094Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 12165094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 12175094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 12185094Slling newname)); 12195094Slling } 12205094Slling 12215094Slling if (nvlist_add_string(props, 12228084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 12238084SGeorge.Wilson@Sun.COM nvlist_add_string(props, 12248084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 12255094Slling nvlist_free(props); 12265094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 12275094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 12285094Slling newname)); 12295094Slling } 12305094Slling } 12315094Slling 12326643Seschrock ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 12335094Slling if (props) 12345094Slling nvlist_free(props); 12355094Slling return (ret); 12365094Slling } 12375094Slling 12385094Slling /* 12395094Slling * Import the given pool using the known configuration and a list of 12405094Slling * properties to be set. The configuration should have come from 12415094Slling * zpool_find_import(). The 'newname' parameters control whether the pool 12425094Slling * is imported with a different name. 12435094Slling */ 12445094Slling int 12455094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 12466643Seschrock nvlist_t *props, boolean_t importfaulted) 1247789Sahrens { 12482676Seschrock zfs_cmd_t zc = { 0 }; 1249789Sahrens char *thename; 1250789Sahrens char *origname; 1251789Sahrens int ret; 12525094Slling char errbuf[1024]; 1253789Sahrens 1254789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1255789Sahrens &origname) == 0); 1256789Sahrens 12575094Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 12585094Slling "cannot import pool '%s'"), origname); 12595094Slling 1260789Sahrens if (newname != NULL) { 12612082Seschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 12623237Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 12632082Seschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 12642082Seschrock newname)); 1265789Sahrens thename = (char *)newname; 1266789Sahrens } else { 1267789Sahrens thename = origname; 1268789Sahrens } 1269789Sahrens 12705094Slling if (props) { 12715094Slling uint64_t version; 12725094Slling 12735094Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 12745094Slling &version) == 0); 12755094Slling 12767184Stimh if ((props = zpool_valid_proplist(hdl, origname, 12775320Slling props, version, B_TRUE, errbuf)) == NULL) { 12785094Slling return (-1); 12795320Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 12805320Slling nvlist_free(props); 12815094Slling return (-1); 12825320Slling } 12835094Slling } 1284789Sahrens 1285789Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1286789Sahrens 1287789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 12881544Seschrock &zc.zc_guid) == 0); 1289789Sahrens 12905320Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 12915320Slling nvlist_free(props); 12922082Seschrock return (-1); 12935320Slling } 1294789Sahrens 12956643Seschrock zc.zc_cookie = (uint64_t)importfaulted; 1296789Sahrens ret = 0; 12974543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1298789Sahrens char desc[1024]; 1299789Sahrens if (newname == NULL) 1300789Sahrens (void) snprintf(desc, sizeof (desc), 1301789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1302789Sahrens thename); 1303789Sahrens else 1304789Sahrens (void) snprintf(desc, sizeof (desc), 1305789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1306789Sahrens origname, thename); 1307789Sahrens 1308789Sahrens switch (errno) { 13091544Seschrock case ENOTSUP: 13101544Seschrock /* 13111544Seschrock * Unsupported version. 13121544Seschrock */ 13132082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 13141544Seschrock break; 13151544Seschrock 13162174Seschrock case EINVAL: 13172174Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 13182174Seschrock break; 13192174Seschrock 1320789Sahrens default: 13212082Seschrock (void) zpool_standard_error(hdl, errno, desc); 1322789Sahrens } 1323789Sahrens 1324789Sahrens ret = -1; 1325789Sahrens } else { 1326789Sahrens zpool_handle_t *zhp; 13274543Smarks 1328789Sahrens /* 1329789Sahrens * This should never fail, but play it safe anyway. 1330789Sahrens */ 13312142Seschrock if (zpool_open_silent(hdl, thename, &zhp) != 0) { 13322142Seschrock ret = -1; 13332142Seschrock } else if (zhp != NULL) { 1334789Sahrens ret = zpool_create_zvol_links(zhp); 1335789Sahrens zpool_close(zhp); 1336789Sahrens } 13374543Smarks 1338789Sahrens } 1339789Sahrens 13402676Seschrock zcmd_free_nvlists(&zc); 13415320Slling nvlist_free(props); 13425320Slling 1343789Sahrens return (ret); 1344789Sahrens } 1345789Sahrens 1346789Sahrens /* 1347789Sahrens * Scrub the pool. 1348789Sahrens */ 1349789Sahrens int 1350789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) 1351789Sahrens { 1352789Sahrens zfs_cmd_t zc = { 0 }; 1353789Sahrens char msg[1024]; 13542082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1355789Sahrens 1356789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1357789Sahrens zc.zc_cookie = type; 1358789Sahrens 13594543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0) 1360789Sahrens return (0); 1361789Sahrens 1362789Sahrens (void) snprintf(msg, sizeof (msg), 1363789Sahrens dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1364789Sahrens 13652082Seschrock if (errno == EBUSY) 13662082Seschrock return (zfs_error(hdl, EZFS_RESILVERING, msg)); 13672082Seschrock else 13682082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1369789Sahrens } 1370789Sahrens 13712468Sek110237 /* 13722468Sek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 13732468Sek110237 * spare; but FALSE if its an INUSE spare. 13742468Sek110237 */ 13752082Seschrock static nvlist_t * 13762082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, 13777326SEric.Schrock@Sun.COM boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 13781544Seschrock { 13791544Seschrock uint_t c, children; 13801544Seschrock nvlist_t **child; 13812082Seschrock uint64_t theguid, present; 13821544Seschrock char *path; 13831544Seschrock uint64_t wholedisk = 0; 13842082Seschrock nvlist_t *ret; 13857326SEric.Schrock@Sun.COM uint64_t is_log; 13861544Seschrock 13872082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); 13881544Seschrock 13891544Seschrock if (search == NULL && 13901544Seschrock nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { 13911544Seschrock /* 13921544Seschrock * If the device has never been present since import, the only 13931544Seschrock * reliable way to match the vdev is by GUID. 13941544Seschrock */ 13952082Seschrock if (theguid == guid) 13962082Seschrock return (nv); 13971544Seschrock } else if (search != NULL && 13981544Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 13991544Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 14001544Seschrock &wholedisk); 14011544Seschrock if (wholedisk) { 14021544Seschrock /* 14031544Seschrock * For whole disks, the internal path has 's0', but the 14041544Seschrock * path passed in by the user doesn't. 14051544Seschrock */ 14061544Seschrock if (strlen(search) == strlen(path) - 2 && 14071544Seschrock strncmp(search, path, strlen(search)) == 0) 14082082Seschrock return (nv); 14091544Seschrock } else if (strcmp(search, path) == 0) { 14102082Seschrock return (nv); 14111544Seschrock } 14121544Seschrock } 14131544Seschrock 14141544Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 14151544Seschrock &child, &children) != 0) 14162082Seschrock return (NULL); 14171544Seschrock 14187326SEric.Schrock@Sun.COM for (c = 0; c < children; c++) { 14192082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 14207326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 14217326SEric.Schrock@Sun.COM /* 14227326SEric.Schrock@Sun.COM * The 'is_log' value is only set for the toplevel 14237326SEric.Schrock@Sun.COM * vdev, not the leaf vdevs. So we always lookup the 14247326SEric.Schrock@Sun.COM * log device from the root of the vdev tree (where 14257326SEric.Schrock@Sun.COM * 'log' is non-NULL). 14267326SEric.Schrock@Sun.COM */ 14277326SEric.Schrock@Sun.COM if (log != NULL && 14287326SEric.Schrock@Sun.COM nvlist_lookup_uint64(child[c], 14297326SEric.Schrock@Sun.COM ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 14307326SEric.Schrock@Sun.COM is_log) { 14317326SEric.Schrock@Sun.COM *log = B_TRUE; 14327326SEric.Schrock@Sun.COM } 14331544Seschrock return (ret); 14347326SEric.Schrock@Sun.COM } 14357326SEric.Schrock@Sun.COM } 14361544Seschrock 14372082Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 14382082Seschrock &child, &children) == 0) { 14392082Seschrock for (c = 0; c < children; c++) { 14402082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 14417326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 14422468Sek110237 *avail_spare = B_TRUE; 14432082Seschrock return (ret); 14442082Seschrock } 14452082Seschrock } 14462082Seschrock } 14472082Seschrock 14485450Sbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 14495450Sbrendan &child, &children) == 0) { 14505450Sbrendan for (c = 0; c < children; c++) { 14515450Sbrendan if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 14527326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 14535450Sbrendan *l2cache = B_TRUE; 14545450Sbrendan return (ret); 14555450Sbrendan } 14565450Sbrendan } 14575450Sbrendan } 14585450Sbrendan 14592082Seschrock return (NULL); 14601544Seschrock } 14611544Seschrock 14622082Seschrock nvlist_t * 14635450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 14647326SEric.Schrock@Sun.COM boolean_t *l2cache, boolean_t *log) 14651544Seschrock { 14661544Seschrock char buf[MAXPATHLEN]; 14671544Seschrock const char *search; 14681544Seschrock char *end; 14691544Seschrock nvlist_t *nvroot; 14701544Seschrock uint64_t guid; 14711544Seschrock 14721613Seschrock guid = strtoull(path, &end, 10); 14731544Seschrock if (guid != 0 && *end == '\0') { 14741544Seschrock search = NULL; 14751544Seschrock } else if (path[0] != '/') { 14761544Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 14771544Seschrock search = buf; 14781544Seschrock } else { 14791544Seschrock search = path; 14801544Seschrock } 14811544Seschrock 14821544Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 14831544Seschrock &nvroot) == 0); 14841544Seschrock 14852468Sek110237 *avail_spare = B_FALSE; 14865450Sbrendan *l2cache = B_FALSE; 14877326SEric.Schrock@Sun.COM if (log != NULL) 14887326SEric.Schrock@Sun.COM *log = B_FALSE; 14895450Sbrendan return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare, 14907326SEric.Schrock@Sun.COM l2cache, log)); 14912468Sek110237 } 14922468Sek110237 14937656SSherry.Moore@Sun.COM static int 14947656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv) 14957656SSherry.Moore@Sun.COM { 14967656SSherry.Moore@Sun.COM uint64_t ival; 14977656SSherry.Moore@Sun.COM 14987656SSherry.Moore@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 14997656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 15007656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 15017656SSherry.Moore@Sun.COM return (0); 15027656SSherry.Moore@Sun.COM 15037656SSherry.Moore@Sun.COM return (1); 15047656SSherry.Moore@Sun.COM } 15057656SSherry.Moore@Sun.COM 15067656SSherry.Moore@Sun.COM /* 15079160SSherry.Moore@Sun.COM * Helper function for zpool_get_config_physpath(). 15087656SSherry.Moore@Sun.COM */ 15099160SSherry.Moore@Sun.COM static int 15109160SSherry.Moore@Sun.COM vdev_get_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 15119160SSherry.Moore@Sun.COM size_t *bytes_written) 15127656SSherry.Moore@Sun.COM { 15139160SSherry.Moore@Sun.COM size_t bytes_left, pos, rsz; 15149160SSherry.Moore@Sun.COM char *tmppath; 15159160SSherry.Moore@Sun.COM const char *format; 15169160SSherry.Moore@Sun.COM 15179160SSherry.Moore@Sun.COM if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 15189160SSherry.Moore@Sun.COM &tmppath) != 0) 15199160SSherry.Moore@Sun.COM return (EZFS_NODEVICE); 15209160SSherry.Moore@Sun.COM 15219160SSherry.Moore@Sun.COM pos = *bytes_written; 15229160SSherry.Moore@Sun.COM bytes_left = physpath_size - pos; 15239160SSherry.Moore@Sun.COM format = (pos == 0) ? "%s" : " %s"; 15249160SSherry.Moore@Sun.COM 15259160SSherry.Moore@Sun.COM rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 15269160SSherry.Moore@Sun.COM *bytes_written += rsz; 15279160SSherry.Moore@Sun.COM 15289160SSherry.Moore@Sun.COM if (rsz >= bytes_left) { 15299160SSherry.Moore@Sun.COM /* if physpath was not copied properly, clear it */ 15309160SSherry.Moore@Sun.COM if (bytes_left != 0) { 15319160SSherry.Moore@Sun.COM physpath[pos] = 0; 15329160SSherry.Moore@Sun.COM } 15339160SSherry.Moore@Sun.COM return (EZFS_NOSPC); 15349160SSherry.Moore@Sun.COM } 15359160SSherry.Moore@Sun.COM return (0); 15369160SSherry.Moore@Sun.COM } 15379160SSherry.Moore@Sun.COM 15389160SSherry.Moore@Sun.COM /* 15399160SSherry.Moore@Sun.COM * Get phys_path for a root pool config. 15409160SSherry.Moore@Sun.COM * Return 0 on success; non-zero on failure. 15419160SSherry.Moore@Sun.COM */ 15429160SSherry.Moore@Sun.COM static int 15439160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 15449160SSherry.Moore@Sun.COM { 15459160SSherry.Moore@Sun.COM size_t rsz; 15467656SSherry.Moore@Sun.COM nvlist_t *vdev_root; 15477656SSherry.Moore@Sun.COM nvlist_t **child; 15489160SSherry.Moore@Sun.COM nvlist_t **child2; 15497656SSherry.Moore@Sun.COM uint_t count; 15509160SSherry.Moore@Sun.COM char *type; 15519160SSherry.Moore@Sun.COM int j, ret; 15529160SSherry.Moore@Sun.COM 15539160SSherry.Moore@Sun.COM rsz = 0; 15549160SSherry.Moore@Sun.COM 15559160SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 15569160SSherry.Moore@Sun.COM &vdev_root) != 0) 15579160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 15589160SSherry.Moore@Sun.COM 15599160SSherry.Moore@Sun.COM if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 15609160SSherry.Moore@Sun.COM nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 15619160SSherry.Moore@Sun.COM &child, &count) != 0) 15629160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 15637656SSherry.Moore@Sun.COM 15647656SSherry.Moore@Sun.COM /* 15659160SSherry.Moore@Sun.COM * root pool can not have EFI labeled disks and can only have 15669160SSherry.Moore@Sun.COM * a single top-level vdev. 15677656SSherry.Moore@Sun.COM */ 15689160SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 15699160SSherry.Moore@Sun.COM pool_uses_efi(vdev_root)) 15709160SSherry.Moore@Sun.COM return (EZFS_POOL_INVALARG); 15719160SSherry.Moore@Sun.COM 15729160SSherry.Moore@Sun.COM if (nvlist_lookup_string(child[0], ZPOOL_CONFIG_TYPE, &type) != 0) 15739160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 15749160SSherry.Moore@Sun.COM 15759160SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_DISK) == 0) { 15769160SSherry.Moore@Sun.COM if (vdev_online(child[0])) { 15779160SSherry.Moore@Sun.COM if ((ret = vdev_get_physpath(child[0], physpath, 15789160SSherry.Moore@Sun.COM phypath_size, &rsz)) != 0) 15799160SSherry.Moore@Sun.COM return (ret); 15809160SSherry.Moore@Sun.COM } 15819160SSherry.Moore@Sun.COM } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) { 15829160SSherry.Moore@Sun.COM 15839160SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist_array(child[0], 15849160SSherry.Moore@Sun.COM ZPOOL_CONFIG_CHILDREN, &child2, &count) != 0) 15859160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 15869160SSherry.Moore@Sun.COM 15879160SSherry.Moore@Sun.COM for (j = 0; j < count; j++) { 15889160SSherry.Moore@Sun.COM if (nvlist_lookup_string(child2[j], ZPOOL_CONFIG_TYPE, 15899160SSherry.Moore@Sun.COM &type) != 0) 15909160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 15919160SSherry.Moore@Sun.COM 15929160SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_DISK) != 0) 15939160SSherry.Moore@Sun.COM return (EZFS_POOL_INVALARG); 15949160SSherry.Moore@Sun.COM 15959160SSherry.Moore@Sun.COM if (vdev_online(child2[j])) { 15969160SSherry.Moore@Sun.COM ret = vdev_get_physpath(child2[j], 15979160SSherry.Moore@Sun.COM physpath, phypath_size, &rsz); 15989160SSherry.Moore@Sun.COM 15999160SSherry.Moore@Sun.COM if (ret == EZFS_NOSPC) 16009160SSherry.Moore@Sun.COM return (ret); 16017656SSherry.Moore@Sun.COM } 16027656SSherry.Moore@Sun.COM } 16039160SSherry.Moore@Sun.COM } else { 16049160SSherry.Moore@Sun.COM return (EZFS_POOL_INVALARG); 16057656SSherry.Moore@Sun.COM } 16067656SSherry.Moore@Sun.COM 16079160SSherry.Moore@Sun.COM /* No online devices */ 16089160SSherry.Moore@Sun.COM if (rsz == 0) 16099160SSherry.Moore@Sun.COM return (EZFS_NODEVICE); 16109160SSherry.Moore@Sun.COM 16117656SSherry.Moore@Sun.COM return (0); 16127656SSherry.Moore@Sun.COM } 16137656SSherry.Moore@Sun.COM 16142468Sek110237 /* 16159160SSherry.Moore@Sun.COM * Get phys_path for a root pool 16169160SSherry.Moore@Sun.COM * Return 0 on success; non-zero on failure. 16179160SSherry.Moore@Sun.COM */ 16189160SSherry.Moore@Sun.COM int 16199160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 16209160SSherry.Moore@Sun.COM { 16219160SSherry.Moore@Sun.COM return (zpool_get_config_physpath(zhp->zpool_config, physpath, 16229160SSherry.Moore@Sun.COM phypath_size)); 16239160SSherry.Moore@Sun.COM } 16249160SSherry.Moore@Sun.COM 16259160SSherry.Moore@Sun.COM /* 16265450Sbrendan * Returns TRUE if the given guid corresponds to the given type. 16275450Sbrendan * This is used to check for hot spares (INUSE or not), and level 2 cache 16285450Sbrendan * devices. 16292468Sek110237 */ 16302468Sek110237 static boolean_t 16315450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 16322468Sek110237 { 16335450Sbrendan uint64_t target_guid; 16342468Sek110237 nvlist_t *nvroot; 16355450Sbrendan nvlist_t **list; 16365450Sbrendan uint_t count; 16372468Sek110237 int i; 16382468Sek110237 16392468Sek110237 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 16402468Sek110237 &nvroot) == 0); 16415450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 16425450Sbrendan for (i = 0; i < count; i++) { 16435450Sbrendan verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 16445450Sbrendan &target_guid) == 0); 16455450Sbrendan if (guid == target_guid) 16462468Sek110237 return (B_TRUE); 16472468Sek110237 } 16482468Sek110237 } 16492468Sek110237 16502468Sek110237 return (B_FALSE); 16511544Seschrock } 16521544Seschrock 1653789Sahrens /* 16544451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 16554451Seschrock * ZFS_ONLINE_* flags. 1656789Sahrens */ 1657789Sahrens int 16584451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 16594451Seschrock vdev_state_t *newstate) 1660789Sahrens { 1661789Sahrens zfs_cmd_t zc = { 0 }; 1662789Sahrens char msg[1024]; 16632082Seschrock nvlist_t *tgt; 16645450Sbrendan boolean_t avail_spare, l2cache; 16652082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1666789Sahrens 16671544Seschrock (void) snprintf(msg, sizeof (msg), 16681544Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1669789Sahrens 16701544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16717326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 16727326SEric.Schrock@Sun.COM NULL)) == NULL) 16732082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1674789Sahrens 16752468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 16762468Sek110237 16775450Sbrendan if (avail_spare || 16785450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 16792082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16802082Seschrock 16814451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 16824451Seschrock zc.zc_obj = flags; 16834451Seschrock 16844543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 16854451Seschrock return (zpool_standard_error(hdl, errno, msg)); 16864451Seschrock 16874451Seschrock *newstate = zc.zc_cookie; 16884451Seschrock return (0); 1689789Sahrens } 1690789Sahrens 1691789Sahrens /* 1692789Sahrens * Take the specified vdev offline 1693789Sahrens */ 1694789Sahrens int 16954451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1696789Sahrens { 1697789Sahrens zfs_cmd_t zc = { 0 }; 1698789Sahrens char msg[1024]; 16992082Seschrock nvlist_t *tgt; 17005450Sbrendan boolean_t avail_spare, l2cache; 17012082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1702789Sahrens 17031544Seschrock (void) snprintf(msg, sizeof (msg), 17041544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 17051544Seschrock 1706789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17077326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 17087326SEric.Schrock@Sun.COM NULL)) == NULL) 17092082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 17102082Seschrock 17112468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 17122468Sek110237 17135450Sbrendan if (avail_spare || 17145450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 17152082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 17162082Seschrock 17174451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 17184451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 17191485Slling 17204543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1721789Sahrens return (0); 1722789Sahrens 1723789Sahrens switch (errno) { 17242082Seschrock case EBUSY: 1725789Sahrens 1726789Sahrens /* 1727789Sahrens * There are no other replicas of this device. 1728789Sahrens */ 17292082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 17302082Seschrock 1731*9701SGeorge.Wilson@Sun.COM case EEXIST: 1732*9701SGeorge.Wilson@Sun.COM /* 1733*9701SGeorge.Wilson@Sun.COM * The log device has unplayed logs 1734*9701SGeorge.Wilson@Sun.COM */ 1735*9701SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 1736*9701SGeorge.Wilson@Sun.COM 17372082Seschrock default: 17382082Seschrock return (zpool_standard_error(hdl, errno, msg)); 17392082Seschrock } 17402082Seschrock } 1741789Sahrens 17422082Seschrock /* 17434451Seschrock * Mark the given vdev faulted. 17444451Seschrock */ 17454451Seschrock int 17464451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 17474451Seschrock { 17484451Seschrock zfs_cmd_t zc = { 0 }; 17494451Seschrock char msg[1024]; 17504451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17514451Seschrock 17524451Seschrock (void) snprintf(msg, sizeof (msg), 17534451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 17544451Seschrock 17554451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17564451Seschrock zc.zc_guid = guid; 17574451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 17584451Seschrock 17594451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 17604451Seschrock return (0); 17614451Seschrock 17624451Seschrock switch (errno) { 17634451Seschrock case EBUSY: 17644451Seschrock 17654451Seschrock /* 17664451Seschrock * There are no other replicas of this device. 17674451Seschrock */ 17684451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 17694451Seschrock 17704451Seschrock default: 17714451Seschrock return (zpool_standard_error(hdl, errno, msg)); 17724451Seschrock } 17734451Seschrock 17744451Seschrock } 17754451Seschrock 17764451Seschrock /* 17774451Seschrock * Mark the given vdev degraded. 17784451Seschrock */ 17794451Seschrock int 17804451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 17814451Seschrock { 17824451Seschrock zfs_cmd_t zc = { 0 }; 17834451Seschrock char msg[1024]; 17844451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17854451Seschrock 17864451Seschrock (void) snprintf(msg, sizeof (msg), 17874451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 17884451Seschrock 17894451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17904451Seschrock zc.zc_guid = guid; 17914451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 17924451Seschrock 17934451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 17944451Seschrock return (0); 17954451Seschrock 17964451Seschrock return (zpool_standard_error(hdl, errno, msg)); 17974451Seschrock } 17984451Seschrock 17994451Seschrock /* 18002082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 18012082Seschrock * a hot spare. 18022082Seschrock */ 18032082Seschrock static boolean_t 18042082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 18052082Seschrock { 18062082Seschrock nvlist_t **child; 18072082Seschrock uint_t c, children; 18082082Seschrock char *type; 18092082Seschrock 18102082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 18112082Seschrock &children) == 0) { 18122082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 18132082Seschrock &type) == 0); 18142082Seschrock 18152082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 18162082Seschrock children == 2 && child[which] == tgt) 18172082Seschrock return (B_TRUE); 18182082Seschrock 18192082Seschrock for (c = 0; c < children; c++) 18202082Seschrock if (is_replacing_spare(child[c], tgt, which)) 18212082Seschrock return (B_TRUE); 1822789Sahrens } 18232082Seschrock 18242082Seschrock return (B_FALSE); 1825789Sahrens } 1826789Sahrens 1827789Sahrens /* 1828789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 18294527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 1830789Sahrens */ 1831789Sahrens int 1832789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 1833789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1834789Sahrens { 1835789Sahrens zfs_cmd_t zc = { 0 }; 1836789Sahrens char msg[1024]; 1837789Sahrens int ret; 18382082Seschrock nvlist_t *tgt; 18397326SEric.Schrock@Sun.COM boolean_t avail_spare, l2cache, islog; 18407326SEric.Schrock@Sun.COM uint64_t val; 18417041Seschrock char *path, *newname; 18422082Seschrock nvlist_t **child; 18432082Seschrock uint_t children; 18442082Seschrock nvlist_t *config_root; 18452082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 18467965SGeorge.Wilson@Sun.COM boolean_t rootpool = pool_is_bootable(zhp); 1847789Sahrens 18481544Seschrock if (replacing) 18491544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 18501544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 18511544Seschrock else 18521544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 18531544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 18541544Seschrock 18557965SGeorge.Wilson@Sun.COM /* 18567965SGeorge.Wilson@Sun.COM * If this is a root pool, make sure that we're not attaching an 18577965SGeorge.Wilson@Sun.COM * EFI labeled device. 18587965SGeorge.Wilson@Sun.COM */ 18597965SGeorge.Wilson@Sun.COM if (rootpool && pool_uses_efi(nvroot)) { 18607965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18617965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root pools.")); 18627965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 18637965SGeorge.Wilson@Sun.COM } 18647965SGeorge.Wilson@Sun.COM 1865789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18667326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 18677326SEric.Schrock@Sun.COM &islog)) == 0) 18682082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 18692082Seschrock 18702468Sek110237 if (avail_spare) 18712082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 18722082Seschrock 18735450Sbrendan if (l2cache) 18745450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 18755450Sbrendan 18762082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 18772082Seschrock zc.zc_cookie = replacing; 18782082Seschrock 18792082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 18802082Seschrock &child, &children) != 0 || children != 1) { 18812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18822082Seschrock "new device must be a single disk")); 18832082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 18841544Seschrock } 18852082Seschrock 18862082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 18872082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 18882082Seschrock 18897041Seschrock if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL) 18907041Seschrock return (-1); 18917041Seschrock 18922082Seschrock /* 18932082Seschrock * If the target is a hot spare that has been swapped in, we can only 18942082Seschrock * replace it with another hot spare. 18952082Seschrock */ 18962082Seschrock if (replacing && 18972082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 18987326SEric.Schrock@Sun.COM (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 18997326SEric.Schrock@Sun.COM NULL) == NULL || !avail_spare) && 19007326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 1)) { 19012082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19022082Seschrock "can only be replaced by another hot spare")); 19037041Seschrock free(newname); 19042082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 19052082Seschrock } 19062082Seschrock 19072082Seschrock /* 19082082Seschrock * If we are attempting to replace a spare, it canot be applied to an 19092082Seschrock * already spared device. 19102082Seschrock */ 19112082Seschrock if (replacing && 19122082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 19137326SEric.Schrock@Sun.COM zpool_find_vdev(zhp, newname, &avail_spare, 19147326SEric.Schrock@Sun.COM &l2cache, NULL) != NULL && avail_spare && 19157326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 0)) { 19162082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19172082Seschrock "device has already been replaced with a spare")); 19187041Seschrock free(newname); 19192082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 19202082Seschrock } 1921789Sahrens 19227041Seschrock free(newname); 19237041Seschrock 19245094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 19252082Seschrock return (-1); 1926789Sahrens 19274543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1928789Sahrens 19292676Seschrock zcmd_free_nvlists(&zc); 1930789Sahrens 19317965SGeorge.Wilson@Sun.COM if (ret == 0) { 19327965SGeorge.Wilson@Sun.COM if (rootpool) { 19337965SGeorge.Wilson@Sun.COM /* 19347965SGeorge.Wilson@Sun.COM * XXX - This should be removed once we can 19357965SGeorge.Wilson@Sun.COM * automatically install the bootblocks on the 19367965SGeorge.Wilson@Sun.COM * newly attached disk. 19377965SGeorge.Wilson@Sun.COM */ 19387965SGeorge.Wilson@Sun.COM (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please " 19397965SGeorge.Wilson@Sun.COM "be sure to invoke %s to make '%s' bootable.\n"), 19407965SGeorge.Wilson@Sun.COM BOOTCMD, new_disk); 19417965SGeorge.Wilson@Sun.COM } 1942789Sahrens return (0); 19437965SGeorge.Wilson@Sun.COM } 1944789Sahrens 1945789Sahrens switch (errno) { 19461544Seschrock case ENOTSUP: 1947789Sahrens /* 1948789Sahrens * Can't attach to or replace this type of vdev. 1949789Sahrens */ 19504527Sperrin if (replacing) { 19517326SEric.Schrock@Sun.COM if (islog) 19524527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19534527Sperrin "cannot replace a log with a spare")); 19544527Sperrin else 19554527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19564527Sperrin "cannot replace a replacing device")); 19574527Sperrin } else { 19582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19592082Seschrock "can only attach to mirrors and top-level " 19602082Seschrock "disks")); 19614527Sperrin } 19622082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1963789Sahrens break; 1964789Sahrens 19651544Seschrock case EINVAL: 1966789Sahrens /* 1967789Sahrens * The new device must be a single disk. 1968789Sahrens */ 19692082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19702082Seschrock "new device must be a single disk")); 19712082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1972789Sahrens break; 1973789Sahrens 19741544Seschrock case EBUSY: 19752082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 19762082Seschrock new_disk); 19772082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1978789Sahrens break; 1979789Sahrens 19801544Seschrock case EOVERFLOW: 1981789Sahrens /* 1982789Sahrens * The new device is too small. 1983789Sahrens */ 19842082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19852082Seschrock "device is too small")); 19862082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1987789Sahrens break; 1988789Sahrens 19891544Seschrock case EDOM: 1990789Sahrens /* 1991789Sahrens * The new device has a different alignment requirement. 1992789Sahrens */ 19932082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19942082Seschrock "devices have different sector alignment")); 19952082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1996789Sahrens break; 1997789Sahrens 19981544Seschrock case ENAMETOOLONG: 1999789Sahrens /* 2000789Sahrens * The resulting top-level vdev spec won't fit in the label. 2001789Sahrens */ 20022082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2003789Sahrens break; 2004789Sahrens 20051544Seschrock default: 20062082Seschrock (void) zpool_standard_error(hdl, errno, msg); 2007789Sahrens } 2008789Sahrens 20092082Seschrock return (-1); 2010789Sahrens } 2011789Sahrens 2012789Sahrens /* 2013789Sahrens * Detach the specified device. 2014789Sahrens */ 2015789Sahrens int 2016789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2017789Sahrens { 2018789Sahrens zfs_cmd_t zc = { 0 }; 2019789Sahrens char msg[1024]; 20202082Seschrock nvlist_t *tgt; 20215450Sbrendan boolean_t avail_spare, l2cache; 20222082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2023789Sahrens 20241544Seschrock (void) snprintf(msg, sizeof (msg), 20251544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 20261544Seschrock 2027789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20287326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 20297326SEric.Schrock@Sun.COM NULL)) == 0) 20302082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2031789Sahrens 20322468Sek110237 if (avail_spare) 20332082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 20342082Seschrock 20355450Sbrendan if (l2cache) 20365450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 20375450Sbrendan 20382082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 20392082Seschrock 20404543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2041789Sahrens return (0); 2042789Sahrens 2043789Sahrens switch (errno) { 2044789Sahrens 20451544Seschrock case ENOTSUP: 2046789Sahrens /* 2047789Sahrens * Can't detach from this type of vdev. 2048789Sahrens */ 20492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 20502082Seschrock "applicable to mirror and replacing vdevs")); 20512082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 2052789Sahrens break; 2053789Sahrens 20541544Seschrock case EBUSY: 2055789Sahrens /* 2056789Sahrens * There are no other replicas of this device. 2057789Sahrens */ 20582082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2059789Sahrens break; 2060789Sahrens 20611544Seschrock default: 20622082Seschrock (void) zpool_standard_error(hdl, errno, msg); 20631544Seschrock } 20641544Seschrock 20652082Seschrock return (-1); 20662082Seschrock } 20672082Seschrock 20682082Seschrock /* 20695450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 20705450Sbrendan * and level 2 cache devices. 20712082Seschrock */ 20722082Seschrock int 20732082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 20742082Seschrock { 20752082Seschrock zfs_cmd_t zc = { 0 }; 20762082Seschrock char msg[1024]; 20772082Seschrock nvlist_t *tgt; 20785450Sbrendan boolean_t avail_spare, l2cache; 20792082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 20802082Seschrock 20812082Seschrock (void) snprintf(msg, sizeof (msg), 20822082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 20832082Seschrock 20842082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20857326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 20867326SEric.Schrock@Sun.COM NULL)) == 0) 20872082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20882082Seschrock 20895450Sbrendan if (!avail_spare && !l2cache) { 20902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20915450Sbrendan "only inactive hot spares or cache devices " 20925450Sbrendan "can be removed")); 20932082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20942082Seschrock } 20952082Seschrock 20962082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 20972082Seschrock 20984543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 20992082Seschrock return (0); 21002082Seschrock 21012082Seschrock return (zpool_standard_error(hdl, errno, msg)); 21021544Seschrock } 21031544Seschrock 21041544Seschrock /* 21051544Seschrock * Clear the errors for the pool, or the particular device if specified. 21061544Seschrock */ 21071544Seschrock int 21081544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path) 21091544Seschrock { 21101544Seschrock zfs_cmd_t zc = { 0 }; 21111544Seschrock char msg[1024]; 21122082Seschrock nvlist_t *tgt; 21135450Sbrendan boolean_t avail_spare, l2cache; 21142082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21151544Seschrock 21161544Seschrock if (path) 21171544Seschrock (void) snprintf(msg, sizeof (msg), 21181544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 21192676Seschrock path); 21201544Seschrock else 21211544Seschrock (void) snprintf(msg, sizeof (msg), 21221544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 21231544Seschrock zhp->zpool_name); 21241544Seschrock 21251544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21262082Seschrock if (path) { 21275450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 21287326SEric.Schrock@Sun.COM &l2cache, NULL)) == 0) 21292082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 21302082Seschrock 21315450Sbrendan /* 21325450Sbrendan * Don't allow error clearing for hot spares. Do allow 21335450Sbrendan * error clearing for l2cache devices. 21345450Sbrendan */ 21352468Sek110237 if (avail_spare) 21362082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 21372082Seschrock 21382082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 21392082Seschrock &zc.zc_guid) == 0); 21401544Seschrock } 21411544Seschrock 21424543Smarks if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 21431544Seschrock return (0); 21441544Seschrock 21452082Seschrock return (zpool_standard_error(hdl, errno, msg)); 2146789Sahrens } 2147789Sahrens 21483126Sahl /* 21494451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 21504451Seschrock */ 21514451Seschrock int 21524451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 21534451Seschrock { 21544451Seschrock zfs_cmd_t zc = { 0 }; 21554451Seschrock char msg[1024]; 21564451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21574451Seschrock 21584451Seschrock (void) snprintf(msg, sizeof (msg), 21594451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 21604451Seschrock guid); 21614451Seschrock 21624451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21634451Seschrock zc.zc_guid = guid; 21644451Seschrock 21654451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 21664451Seschrock return (0); 21674451Seschrock 21684451Seschrock return (zpool_standard_error(hdl, errno, msg)); 21694451Seschrock } 21704451Seschrock 21714451Seschrock /* 21723126Sahl * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 21733126Sahl * hierarchy. 21743126Sahl */ 21753126Sahl int 21763126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 21773126Sahl void *data) 2178789Sahrens { 21793126Sahl libzfs_handle_t *hdl = zhp->zpool_hdl; 21803126Sahl char (*paths)[MAXPATHLEN]; 21813126Sahl size_t size = 4; 21823126Sahl int curr, fd, base, ret = 0; 21833126Sahl DIR *dirp; 21843126Sahl struct dirent *dp; 21853126Sahl struct stat st; 21863126Sahl 21873126Sahl if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 21883126Sahl return (errno == ENOENT ? 0 : -1); 21893126Sahl 21903126Sahl if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 21913126Sahl int err = errno; 21923126Sahl (void) close(base); 21933126Sahl return (err == ENOENT ? 0 : -1); 21943126Sahl } 2195789Sahrens 2196789Sahrens /* 21973126Sahl * Oddly this wasn't a directory -- ignore that failure since we 21983126Sahl * know there are no links lower in the (non-existant) hierarchy. 2199789Sahrens */ 22003126Sahl if (!S_ISDIR(st.st_mode)) { 22013126Sahl (void) close(base); 22023126Sahl return (0); 22033126Sahl } 22043126Sahl 22053126Sahl if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 22063126Sahl (void) close(base); 22073126Sahl return (-1); 2208789Sahrens } 2209789Sahrens 22103126Sahl (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 22113126Sahl curr = 0; 22123126Sahl 22133126Sahl while (curr >= 0) { 22143126Sahl if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 22153126Sahl goto err; 22163126Sahl 22173126Sahl if (S_ISDIR(st.st_mode)) { 22183126Sahl if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 22193126Sahl goto err; 22203126Sahl 22213126Sahl if ((dirp = fdopendir(fd)) == NULL) { 22223126Sahl (void) close(fd); 22233126Sahl goto err; 22243126Sahl } 22253126Sahl 22263126Sahl while ((dp = readdir(dirp)) != NULL) { 22273126Sahl if (dp->d_name[0] == '.') 22283126Sahl continue; 22293126Sahl 22303126Sahl if (curr + 1 == size) { 22313126Sahl paths = zfs_realloc(hdl, paths, 22323126Sahl size * sizeof (paths[0]), 22333126Sahl size * 2 * sizeof (paths[0])); 22343126Sahl if (paths == NULL) { 22353126Sahl (void) closedir(dirp); 22363126Sahl (void) close(fd); 22373126Sahl goto err; 22383126Sahl } 22393126Sahl 22403126Sahl size *= 2; 22413126Sahl } 22423126Sahl 22433126Sahl (void) strlcpy(paths[curr + 1], paths[curr], 22443126Sahl sizeof (paths[curr + 1])); 22453126Sahl (void) strlcat(paths[curr], "/", 22463126Sahl sizeof (paths[curr])); 22473126Sahl (void) strlcat(paths[curr], dp->d_name, 22483126Sahl sizeof (paths[curr])); 22493126Sahl curr++; 22503126Sahl } 22513126Sahl 22523126Sahl (void) closedir(dirp); 22533126Sahl 22543126Sahl } else { 22553126Sahl if ((ret = cb(paths[curr], data)) != 0) 22563126Sahl break; 22573126Sahl } 22583126Sahl 22593126Sahl curr--; 22603126Sahl } 22613126Sahl 22623126Sahl free(paths); 22633126Sahl (void) close(base); 22643126Sahl 22653126Sahl return (ret); 22663126Sahl 22673126Sahl err: 22683126Sahl free(paths); 22693126Sahl (void) close(base); 22703126Sahl return (-1); 22713126Sahl } 22723126Sahl 22733126Sahl typedef struct zvol_cb { 22743126Sahl zpool_handle_t *zcb_pool; 22753126Sahl boolean_t zcb_create; 22763126Sahl } zvol_cb_t; 22773126Sahl 22783126Sahl /*ARGSUSED*/ 22793126Sahl static int 22803126Sahl do_zvol_create(zfs_handle_t *zhp, void *data) 22813126Sahl { 22824657Sahrens int ret = 0; 22833126Sahl 22844657Sahrens if (ZFS_IS_VOLUME(zhp)) { 22853126Sahl (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 22864657Sahrens ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 22874657Sahrens } 22883126Sahl 22894657Sahrens if (ret == 0) 22904657Sahrens ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 2291789Sahrens 2292789Sahrens zfs_close(zhp); 22933126Sahl 2294789Sahrens return (ret); 2295789Sahrens } 2296789Sahrens 2297789Sahrens /* 2298789Sahrens * Iterate over all zvols in the pool and make any necessary minor nodes. 2299789Sahrens */ 2300789Sahrens int 2301789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp) 2302789Sahrens { 2303789Sahrens zfs_handle_t *zfp; 2304789Sahrens int ret; 2305789Sahrens 2306789Sahrens /* 2307789Sahrens * If the pool is unavailable, just return success. 2308789Sahrens */ 23092082Seschrock if ((zfp = make_dataset_handle(zhp->zpool_hdl, 23102082Seschrock zhp->zpool_name)) == NULL) 2311789Sahrens return (0); 2312789Sahrens 23134657Sahrens ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 2314789Sahrens 2315789Sahrens zfs_close(zfp); 2316789Sahrens return (ret); 2317789Sahrens } 2318789Sahrens 23193126Sahl static int 23203126Sahl do_zvol_remove(const char *dataset, void *data) 23213126Sahl { 23223126Sahl zpool_handle_t *zhp = data; 23233126Sahl 23243126Sahl return (zvol_remove_link(zhp->zpool_hdl, dataset)); 23253126Sahl } 23263126Sahl 2327789Sahrens /* 23283126Sahl * Iterate over all zvols in the pool and remove any minor nodes. We iterate 23293126Sahl * by examining the /dev links so that a corrupted pool doesn't impede this 23303126Sahl * operation. 2331789Sahrens */ 2332789Sahrens int 2333789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp) 2334789Sahrens { 23353126Sahl return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 2336789Sahrens } 23371354Seschrock 23381354Seschrock /* 23391354Seschrock * Convert from a devid string to a path. 23401354Seschrock */ 23411354Seschrock static char * 23421354Seschrock devid_to_path(char *devid_str) 23431354Seschrock { 23441354Seschrock ddi_devid_t devid; 23451354Seschrock char *minor; 23461354Seschrock char *path; 23471354Seschrock devid_nmlist_t *list = NULL; 23481354Seschrock int ret; 23491354Seschrock 23501354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 23511354Seschrock return (NULL); 23521354Seschrock 23531354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 23541354Seschrock 23551354Seschrock devid_str_free(minor); 23561354Seschrock devid_free(devid); 23571354Seschrock 23581354Seschrock if (ret != 0) 23591354Seschrock return (NULL); 23601354Seschrock 23612082Seschrock if ((path = strdup(list[0].devname)) == NULL) 23622082Seschrock return (NULL); 23632082Seschrock 23641354Seschrock devid_free_nmlist(list); 23651354Seschrock 23661354Seschrock return (path); 23671354Seschrock } 23681354Seschrock 23691354Seschrock /* 23701354Seschrock * Convert from a path to a devid string. 23711354Seschrock */ 23721354Seschrock static char * 23731354Seschrock path_to_devid(const char *path) 23741354Seschrock { 23751354Seschrock int fd; 23761354Seschrock ddi_devid_t devid; 23771354Seschrock char *minor, *ret; 23781354Seschrock 23791354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 23801354Seschrock return (NULL); 23811354Seschrock 23821354Seschrock minor = NULL; 23831354Seschrock ret = NULL; 23841354Seschrock if (devid_get(fd, &devid) == 0) { 23851354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 23861354Seschrock ret = devid_str_encode(devid, minor); 23871354Seschrock if (minor != NULL) 23881354Seschrock devid_str_free(minor); 23891354Seschrock devid_free(devid); 23901354Seschrock } 23911354Seschrock (void) close(fd); 23921354Seschrock 23931354Seschrock return (ret); 23941354Seschrock } 23951354Seschrock 23961354Seschrock /* 23971354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 23981354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 23991354Seschrock * type 'zpool status', and we'll display the correct information anyway. 24001354Seschrock */ 24011354Seschrock static void 24021354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 24031354Seschrock { 24041354Seschrock zfs_cmd_t zc = { 0 }; 24051354Seschrock 24061354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 24072676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 24081354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 24091544Seschrock &zc.zc_guid) == 0); 24101354Seschrock 24112082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 24121354Seschrock } 24131354Seschrock 24141354Seschrock /* 24151354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 24161354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 24171354Seschrock * We also check if this is a whole disk, in which case we strip off the 24181354Seschrock * trailing 's0' slice name. 24191354Seschrock * 24201354Seschrock * This routine is also responsible for identifying when disks have been 24211354Seschrock * reconfigured in a new location. The kernel will have opened the device by 24221354Seschrock * devid, but the path will still refer to the old location. To catch this, we 24231354Seschrock * first do a path -> devid translation (which is fast for the common case). If 24241354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 24251354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 24261354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 24271354Seschrock * of these checks. 24281354Seschrock */ 24291354Seschrock char * 24302082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 24311354Seschrock { 24321354Seschrock char *path, *devid; 24331544Seschrock uint64_t value; 24341544Seschrock char buf[64]; 24354451Seschrock vdev_stat_t *vs; 24364451Seschrock uint_t vsc; 24371354Seschrock 24381544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 24391544Seschrock &value) == 0) { 24401544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 24411544Seschrock &value) == 0); 24422856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 24432856Snd150628 (u_longlong_t)value); 24441544Seschrock path = buf; 24451544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 24461354Seschrock 24474451Seschrock /* 24484451Seschrock * If the device is dead (faulted, offline, etc) then don't 24494451Seschrock * bother opening it. Otherwise we may be forcing the user to 24504451Seschrock * open a misbehaving device, which can have undesirable 24514451Seschrock * effects. 24524451Seschrock */ 24534451Seschrock if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 24544451Seschrock (uint64_t **)&vs, &vsc) != 0 || 24554451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 24564451Seschrock zhp != NULL && 24571354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 24581354Seschrock /* 24591354Seschrock * Determine if the current path is correct. 24601354Seschrock */ 24611354Seschrock char *newdevid = path_to_devid(path); 24621354Seschrock 24631354Seschrock if (newdevid == NULL || 24641354Seschrock strcmp(devid, newdevid) != 0) { 24651354Seschrock char *newpath; 24661354Seschrock 24671354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 24681354Seschrock /* 24691354Seschrock * Update the path appropriately. 24701354Seschrock */ 24711354Seschrock set_path(zhp, nv, newpath); 24722082Seschrock if (nvlist_add_string(nv, 24732082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 24742082Seschrock verify(nvlist_lookup_string(nv, 24752082Seschrock ZPOOL_CONFIG_PATH, 24762082Seschrock &path) == 0); 24771354Seschrock free(newpath); 24781354Seschrock } 24791354Seschrock } 24801354Seschrock 24812082Seschrock if (newdevid) 24822082Seschrock devid_str_free(newdevid); 24831354Seschrock } 24841354Seschrock 24851354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 24861354Seschrock path += 9; 24871354Seschrock 24881354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 24891544Seschrock &value) == 0 && value) { 24902082Seschrock char *tmp = zfs_strdup(hdl, path); 24912082Seschrock if (tmp == NULL) 24922082Seschrock return (NULL); 24931354Seschrock tmp[strlen(path) - 2] = '\0'; 24941354Seschrock return (tmp); 24951354Seschrock } 24961354Seschrock } else { 24971354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 24982082Seschrock 24992082Seschrock /* 25002082Seschrock * If it's a raidz device, we need to stick in the parity level. 25012082Seschrock */ 25022082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 25032082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 25042082Seschrock &value) == 0); 25052082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 25062856Snd150628 (u_longlong_t)value); 25072082Seschrock path = buf; 25082082Seschrock } 25091354Seschrock } 25101354Seschrock 25112082Seschrock return (zfs_strdup(hdl, path)); 25121354Seschrock } 25131544Seschrock 25141544Seschrock static int 25151544Seschrock zbookmark_compare(const void *a, const void *b) 25161544Seschrock { 25171544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 25181544Seschrock } 25191544Seschrock 25201544Seschrock /* 25211544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 25221544Seschrock * caller. 25231544Seschrock */ 25241544Seschrock int 25253444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 25261544Seschrock { 25271544Seschrock zfs_cmd_t zc = { 0 }; 25281544Seschrock uint64_t count; 25292676Seschrock zbookmark_t *zb = NULL; 25303444Sek110237 int i; 25311544Seschrock 25321544Seschrock /* 25331544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 25341544Seschrock * has increased, allocate more space and continue until we get the 25351544Seschrock * entire list. 25361544Seschrock */ 25371544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 25381544Seschrock &count) == 0); 25394820Sek110237 if (count == 0) 25404820Sek110237 return (0); 25412676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 25422856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 25432082Seschrock return (-1); 25442676Seschrock zc.zc_nvlist_dst_size = count; 25451544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 25461544Seschrock for (;;) { 25472082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 25482082Seschrock &zc) != 0) { 25492676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 25501544Seschrock if (errno == ENOMEM) { 25513823Svb160487 count = zc.zc_nvlist_dst_size; 25522676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 25533823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 25543823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 25552082Seschrock return (-1); 25561544Seschrock } else { 25571544Seschrock return (-1); 25581544Seschrock } 25591544Seschrock } else { 25601544Seschrock break; 25611544Seschrock } 25621544Seschrock } 25631544Seschrock 25641544Seschrock /* 25651544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 25661544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 25672676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 25681544Seschrock * _not_ copied as part of the process. So we point the start of our 25691544Seschrock * array appropriate and decrement the total number of elements. 25701544Seschrock */ 25712676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 25722676Seschrock zc.zc_nvlist_dst_size; 25732676Seschrock count -= zc.zc_nvlist_dst_size; 25741544Seschrock 25751544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 25761544Seschrock 25773444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 25781544Seschrock 25791544Seschrock /* 25803444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 25811544Seschrock */ 25821544Seschrock for (i = 0; i < count; i++) { 25831544Seschrock nvlist_t *nv; 25841544Seschrock 25853700Sek110237 /* ignoring zb_blkid and zb_level for now */ 25863700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 25873700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 25881544Seschrock continue; 25891544Seschrock 25903444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 25913444Sek110237 goto nomem; 25923444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 25933444Sek110237 zb[i].zb_objset) != 0) { 25943444Sek110237 nvlist_free(nv); 25952082Seschrock goto nomem; 25963444Sek110237 } 25973444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 25983444Sek110237 zb[i].zb_object) != 0) { 25993444Sek110237 nvlist_free(nv); 26003444Sek110237 goto nomem; 26011544Seschrock } 26023444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 26033444Sek110237 nvlist_free(nv); 26043444Sek110237 goto nomem; 26053444Sek110237 } 26063444Sek110237 nvlist_free(nv); 26071544Seschrock } 26081544Seschrock 26093265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 26101544Seschrock return (0); 26112082Seschrock 26122082Seschrock nomem: 26132676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 26142082Seschrock return (no_memory(zhp->zpool_hdl)); 26151544Seschrock } 26161760Seschrock 26171760Seschrock /* 26181760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 26191760Seschrock */ 26201760Seschrock int 26215094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 26221760Seschrock { 26231760Seschrock zfs_cmd_t zc = { 0 }; 26242082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 26251760Seschrock 26261760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 26275094Slling zc.zc_cookie = new_version; 26285094Slling 26294543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 26303237Slling return (zpool_standard_error_fmt(hdl, errno, 26312082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 26322082Seschrock zhp->zpool_name)); 26331760Seschrock return (0); 26341760Seschrock } 26352926Sek110237 26364988Sek110237 void 26374988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 26384988Sek110237 char *history_str) 26394988Sek110237 { 26404988Sek110237 int i; 26414988Sek110237 26424988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 26434988Sek110237 for (i = 1; i < argc; i++) { 26444988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 26454988Sek110237 HIS_MAX_RECORD_LEN) 26464988Sek110237 break; 26474988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 26484988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 26494988Sek110237 } 26504988Sek110237 } 26514988Sek110237 26522926Sek110237 /* 26534988Sek110237 * Stage command history for logging. 26542926Sek110237 */ 26554988Sek110237 int 26564988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 26572926Sek110237 { 26584988Sek110237 if (history_str == NULL) 26594988Sek110237 return (EINVAL); 26604988Sek110237 26614988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 26624988Sek110237 return (EINVAL); 26632926Sek110237 26644715Sek110237 if (hdl->libzfs_log_str != NULL) 26654543Smarks free(hdl->libzfs_log_str); 26662926Sek110237 26674988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 26684988Sek110237 return (no_memory(hdl)); 26694543Smarks 26704988Sek110237 return (0); 26712926Sek110237 } 26722926Sek110237 26732926Sek110237 /* 26742926Sek110237 * Perform ioctl to get some command history of a pool. 26752926Sek110237 * 26762926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 26772926Sek110237 * logical offset of the history buffer to start reading from. 26782926Sek110237 * 26792926Sek110237 * Upon return, 'off' is the next logical offset to read from and 26802926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 26812926Sek110237 */ 26822926Sek110237 static int 26832926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 26842926Sek110237 { 26852926Sek110237 zfs_cmd_t zc = { 0 }; 26862926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 26872926Sek110237 26882926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 26892926Sek110237 26902926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 26912926Sek110237 zc.zc_history_len = *len; 26922926Sek110237 zc.zc_history_offset = *off; 26932926Sek110237 26942926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 26952926Sek110237 switch (errno) { 26962926Sek110237 case EPERM: 26973237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 26983237Slling dgettext(TEXT_DOMAIN, 26992926Sek110237 "cannot show history for pool '%s'"), 27002926Sek110237 zhp->zpool_name)); 27012926Sek110237 case ENOENT: 27023237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 27032926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 27042926Sek110237 "'%s'"), zhp->zpool_name)); 27053863Sek110237 case ENOTSUP: 27063863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 27073863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 27083863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 27092926Sek110237 default: 27103237Slling return (zpool_standard_error_fmt(hdl, errno, 27112926Sek110237 dgettext(TEXT_DOMAIN, 27122926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 27132926Sek110237 } 27142926Sek110237 } 27152926Sek110237 27162926Sek110237 *len = zc.zc_history_len; 27172926Sek110237 *off = zc.zc_history_offset; 27182926Sek110237 27192926Sek110237 return (0); 27202926Sek110237 } 27212926Sek110237 27222926Sek110237 /* 27232926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 27242926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 27252926Sek110237 * processed as there wasn't a complete record. 27262926Sek110237 */ 27272926Sek110237 static int 27282926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 27292926Sek110237 nvlist_t ***records, uint_t *numrecords) 27302926Sek110237 { 27312926Sek110237 uint64_t reclen; 27322926Sek110237 nvlist_t *nv; 27332926Sek110237 int i; 27342926Sek110237 27352926Sek110237 while (bytes_read > sizeof (reclen)) { 27362926Sek110237 27372926Sek110237 /* get length of packed record (stored as little endian) */ 27382926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 27392926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 27402926Sek110237 27412926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 27422926Sek110237 break; 27432926Sek110237 27442926Sek110237 /* unpack record */ 27452926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 27462926Sek110237 return (ENOMEM); 27472926Sek110237 bytes_read -= sizeof (reclen) + reclen; 27482926Sek110237 buf += sizeof (reclen) + reclen; 27492926Sek110237 27502926Sek110237 /* add record to nvlist array */ 27512926Sek110237 (*numrecords)++; 27522926Sek110237 if (ISP2(*numrecords + 1)) { 27532926Sek110237 *records = realloc(*records, 27542926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 27552926Sek110237 } 27562926Sek110237 (*records)[*numrecords - 1] = nv; 27572926Sek110237 } 27582926Sek110237 27592926Sek110237 *leftover = bytes_read; 27602926Sek110237 return (0); 27612926Sek110237 } 27622926Sek110237 27632926Sek110237 #define HIS_BUF_LEN (128*1024) 27642926Sek110237 27652926Sek110237 /* 27662926Sek110237 * Retrieve the command history of a pool. 27672926Sek110237 */ 27682926Sek110237 int 27692926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 27702926Sek110237 { 27712926Sek110237 char buf[HIS_BUF_LEN]; 27722926Sek110237 uint64_t off = 0; 27732926Sek110237 nvlist_t **records = NULL; 27742926Sek110237 uint_t numrecords = 0; 27752926Sek110237 int err, i; 27762926Sek110237 27772926Sek110237 do { 27782926Sek110237 uint64_t bytes_read = sizeof (buf); 27792926Sek110237 uint64_t leftover; 27802926Sek110237 27812926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 27822926Sek110237 break; 27832926Sek110237 27842926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 27852926Sek110237 if (!bytes_read) 27862926Sek110237 break; 27872926Sek110237 27882926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 27892926Sek110237 &leftover, &records, &numrecords)) != 0) 27902926Sek110237 break; 27912926Sek110237 off -= leftover; 27922926Sek110237 27932926Sek110237 /* CONSTCOND */ 27942926Sek110237 } while (1); 27952926Sek110237 27962926Sek110237 if (!err) { 27972926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 27982926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 27992926Sek110237 records, numrecords) == 0); 28002926Sek110237 } 28012926Sek110237 for (i = 0; i < numrecords; i++) 28022926Sek110237 nvlist_free(records[i]); 28032926Sek110237 free(records); 28042926Sek110237 28052926Sek110237 return (err); 28062926Sek110237 } 28073444Sek110237 28083444Sek110237 void 28093444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 28103444Sek110237 char *pathname, size_t len) 28113444Sek110237 { 28123444Sek110237 zfs_cmd_t zc = { 0 }; 28133444Sek110237 boolean_t mounted = B_FALSE; 28143444Sek110237 char *mntpnt = NULL; 28153444Sek110237 char dsname[MAXNAMELEN]; 28163444Sek110237 28173444Sek110237 if (dsobj == 0) { 28183444Sek110237 /* special case for the MOS */ 28193444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 28203444Sek110237 return; 28213444Sek110237 } 28223444Sek110237 28233444Sek110237 /* get the dataset's name */ 28243444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 28253444Sek110237 zc.zc_obj = dsobj; 28263444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 28273444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 28283444Sek110237 /* just write out a path of two object numbers */ 28293444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 28303444Sek110237 dsobj, obj); 28313444Sek110237 return; 28323444Sek110237 } 28333444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 28343444Sek110237 28353444Sek110237 /* find out if the dataset is mounted */ 28363444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 28373444Sek110237 28383444Sek110237 /* get the corrupted object's path */ 28393444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 28403444Sek110237 zc.zc_obj = obj; 28413444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 28423444Sek110237 &zc) == 0) { 28433444Sek110237 if (mounted) { 28443444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 28453444Sek110237 zc.zc_value); 28463444Sek110237 } else { 28473444Sek110237 (void) snprintf(pathname, len, "%s:%s", 28483444Sek110237 dsname, zc.zc_value); 28493444Sek110237 } 28503444Sek110237 } else { 28513444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 28523444Sek110237 } 28533444Sek110237 free(mntpnt); 28543444Sek110237 } 28553912Slling 28564276Staylor #define RDISK_ROOT "/dev/rdsk" 28574276Staylor #define BACKUP_SLICE "s2" 28584276Staylor /* 28594276Staylor * Don't start the slice at the default block of 34; many storage 28604276Staylor * devices will use a stripe width of 128k, so start there instead. 28614276Staylor */ 28624276Staylor #define NEW_START_BLOCK 256 28634276Staylor 28644276Staylor /* 28657042Sgw25295 * Read the EFI label from the config, if a label does not exist then 28667042Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 28677042Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 28687042Sgw25295 * partition. 28697042Sgw25295 */ 28707042Sgw25295 static int 28717042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 28727042Sgw25295 { 28737042Sgw25295 char *path; 28747042Sgw25295 int fd; 28757042Sgw25295 char diskname[MAXPATHLEN]; 28767042Sgw25295 int err = -1; 28777042Sgw25295 28787042Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 28797042Sgw25295 return (err); 28807042Sgw25295 28817042Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 28827042Sgw25295 strrchr(path, '/')); 28837042Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 28847042Sgw25295 struct dk_gpt *vtoc; 28857042Sgw25295 28867042Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 28877042Sgw25295 if (sb != NULL) 28887042Sgw25295 *sb = vtoc->efi_parts[0].p_start; 28897042Sgw25295 efi_free(vtoc); 28907042Sgw25295 } 28917042Sgw25295 (void) close(fd); 28927042Sgw25295 } 28937042Sgw25295 return (err); 28947042Sgw25295 } 28957042Sgw25295 28967042Sgw25295 /* 28974276Staylor * determine where a partition starts on a disk in the current 28984276Staylor * configuration 28994276Staylor */ 29004276Staylor static diskaddr_t 29014276Staylor find_start_block(nvlist_t *config) 29024276Staylor { 29034276Staylor nvlist_t **child; 29044276Staylor uint_t c, children; 29054276Staylor diskaddr_t sb = MAXOFFSET_T; 29064276Staylor uint64_t wholedisk; 29074276Staylor 29084276Staylor if (nvlist_lookup_nvlist_array(config, 29094276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 29104276Staylor if (nvlist_lookup_uint64(config, 29114276Staylor ZPOOL_CONFIG_WHOLE_DISK, 29124276Staylor &wholedisk) != 0 || !wholedisk) { 29134276Staylor return (MAXOFFSET_T); 29144276Staylor } 29157042Sgw25295 if (read_efi_label(config, &sb) < 0) 29167042Sgw25295 sb = MAXOFFSET_T; 29174276Staylor return (sb); 29184276Staylor } 29194276Staylor 29204276Staylor for (c = 0; c < children; c++) { 29214276Staylor sb = find_start_block(child[c]); 29224276Staylor if (sb != MAXOFFSET_T) { 29234276Staylor return (sb); 29244276Staylor } 29254276Staylor } 29264276Staylor return (MAXOFFSET_T); 29274276Staylor } 29284276Staylor 29294276Staylor /* 29304276Staylor * Label an individual disk. The name provided is the short name, 29314276Staylor * stripped of any leading /dev path. 29324276Staylor */ 29334276Staylor int 29344276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 29354276Staylor { 29364276Staylor char path[MAXPATHLEN]; 29374276Staylor struct dk_gpt *vtoc; 29384276Staylor int fd; 29394276Staylor size_t resv = EFI_MIN_RESV_SIZE; 29404276Staylor uint64_t slice_size; 29414276Staylor diskaddr_t start_block; 29424276Staylor char errbuf[1024]; 29434276Staylor 29446289Smmusante /* prepare an error message just in case */ 29456289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 29466289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 29476289Smmusante 29484276Staylor if (zhp) { 29494276Staylor nvlist_t *nvroot; 29504276Staylor 29517965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp)) { 29527965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29537965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root " 29547965SGeorge.Wilson@Sun.COM "pools.")); 29557965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 29567965SGeorge.Wilson@Sun.COM } 29577965SGeorge.Wilson@Sun.COM 29584276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 29594276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 29604276Staylor 29614276Staylor if (zhp->zpool_start_block == 0) 29624276Staylor start_block = find_start_block(nvroot); 29634276Staylor else 29644276Staylor start_block = zhp->zpool_start_block; 29654276Staylor zhp->zpool_start_block = start_block; 29664276Staylor } else { 29674276Staylor /* new pool */ 29684276Staylor start_block = NEW_START_BLOCK; 29694276Staylor } 29704276Staylor 29714276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 29724276Staylor BACKUP_SLICE); 29734276Staylor 29744276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 29754276Staylor /* 29764276Staylor * This shouldn't happen. We've long since verified that this 29774276Staylor * is a valid device. 29784276Staylor */ 29796289Smmusante zfs_error_aux(hdl, 29806289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 29814276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 29824276Staylor } 29834276Staylor 29844276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 29854276Staylor /* 29864276Staylor * The only way this can fail is if we run out of memory, or we 29874276Staylor * were unable to read the disk's capacity 29884276Staylor */ 29894276Staylor if (errno == ENOMEM) 29904276Staylor (void) no_memory(hdl); 29914276Staylor 29924276Staylor (void) close(fd); 29936289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29946289Smmusante "unable to read disk capacity"), name); 29954276Staylor 29964276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 29974276Staylor } 29984276Staylor 29994276Staylor slice_size = vtoc->efi_last_u_lba + 1; 30004276Staylor slice_size -= EFI_MIN_RESV_SIZE; 30014276Staylor if (start_block == MAXOFFSET_T) 30024276Staylor start_block = NEW_START_BLOCK; 30034276Staylor slice_size -= start_block; 30044276Staylor 30054276Staylor vtoc->efi_parts[0].p_start = start_block; 30064276Staylor vtoc->efi_parts[0].p_size = slice_size; 30074276Staylor 30084276Staylor /* 30094276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 30104276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 30114276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 30124276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 30134276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 30144276Staylor * can get, in the absence of V_OTHER. 30154276Staylor */ 30164276Staylor vtoc->efi_parts[0].p_tag = V_USR; 30174276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 30184276Staylor 30194276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 30204276Staylor vtoc->efi_parts[8].p_size = resv; 30214276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 30224276Staylor 30234276Staylor if (efi_write(fd, vtoc) != 0) { 30244276Staylor /* 30254276Staylor * Some block drivers (like pcata) may not support EFI 30264276Staylor * GPT labels. Print out a helpful error message dir- 30274276Staylor * ecting the user to manually label the disk and give 30284276Staylor * a specific slice. 30294276Staylor */ 30304276Staylor (void) close(fd); 30314276Staylor efi_free(vtoc); 30324276Staylor 30334276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30346289Smmusante "try using fdisk(1M) and then provide a specific slice")); 30354276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 30364276Staylor } 30374276Staylor 30384276Staylor (void) close(fd); 30394276Staylor efi_free(vtoc); 30404276Staylor return (0); 30414276Staylor } 30426423Sgw25295 30436423Sgw25295 static boolean_t 30446423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 30456423Sgw25295 { 30466423Sgw25295 char *type; 30476423Sgw25295 nvlist_t **child; 30486423Sgw25295 uint_t children, c; 30496423Sgw25295 30506423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 30516423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 30526423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 30536423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 30546423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 30556423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30566423Sgw25295 "vdev type '%s' is not supported"), type); 30576423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 30586423Sgw25295 return (B_FALSE); 30596423Sgw25295 } 30606423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 30616423Sgw25295 &child, &children) == 0) { 30626423Sgw25295 for (c = 0; c < children; c++) { 30636423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 30646423Sgw25295 return (B_FALSE); 30656423Sgw25295 } 30666423Sgw25295 } 30676423Sgw25295 return (B_TRUE); 30686423Sgw25295 } 30696423Sgw25295 30706423Sgw25295 /* 30716423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 30726423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 30736423Sgw25295 */ 30746423Sgw25295 int 30756423Sgw25295 zvol_check_dump_config(char *arg) 30766423Sgw25295 { 30776423Sgw25295 zpool_handle_t *zhp = NULL; 30786423Sgw25295 nvlist_t *config, *nvroot; 30796423Sgw25295 char *p, *volname; 30806423Sgw25295 nvlist_t **top; 30816423Sgw25295 uint_t toplevels; 30826423Sgw25295 libzfs_handle_t *hdl; 30836423Sgw25295 char errbuf[1024]; 30846423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 30856423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 30866423Sgw25295 int ret = 1; 30876423Sgw25295 30886423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 30896423Sgw25295 return (-1); 30906423Sgw25295 } 30916423Sgw25295 30926423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30936423Sgw25295 "dump is not supported on device '%s'"), arg); 30946423Sgw25295 30956423Sgw25295 if ((hdl = libzfs_init()) == NULL) 30966423Sgw25295 return (1); 30976423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 30986423Sgw25295 30996423Sgw25295 volname = arg + pathlen; 31006423Sgw25295 31016423Sgw25295 /* check the configuration of the pool */ 31026423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 31036423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31046423Sgw25295 "malformed dataset name")); 31056423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 31066423Sgw25295 return (1); 31076423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 31086423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31096423Sgw25295 "dataset name is too long")); 31106423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 31116423Sgw25295 return (1); 31126423Sgw25295 } else { 31136423Sgw25295 (void) strncpy(poolname, volname, p - volname); 31146423Sgw25295 poolname[p - volname] = '\0'; 31156423Sgw25295 } 31166423Sgw25295 31176423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 31186423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31196423Sgw25295 "could not open pool '%s'"), poolname); 31206423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 31216423Sgw25295 goto out; 31226423Sgw25295 } 31236423Sgw25295 config = zpool_get_config(zhp, NULL); 31246423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 31256423Sgw25295 &nvroot) != 0) { 31266423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31276423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 31286423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 31296423Sgw25295 goto out; 31306423Sgw25295 } 31316423Sgw25295 31326423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 31336423Sgw25295 &top, &toplevels) == 0); 31346423Sgw25295 if (toplevels != 1) { 31356423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31366423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 31376423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 31386423Sgw25295 goto out; 31396423Sgw25295 } 31406423Sgw25295 31416423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 31426423Sgw25295 goto out; 31436423Sgw25295 } 31446423Sgw25295 ret = 0; 31456423Sgw25295 31466423Sgw25295 out: 31476423Sgw25295 if (zhp) 31486423Sgw25295 zpool_close(zhp); 31496423Sgw25295 libzfs_fini(hdl); 31506423Sgw25295 return (ret); 31516423Sgw25295 } 3152