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 /* 1507*9790SLin.Ling@Sun.COM * Helper function for zpool_get_physpaths(). 15087656SSherry.Moore@Sun.COM */ 15099160SSherry.Moore@Sun.COM static int 1510*9790SLin.Ling@Sun.COM vdev_get_one_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 1538*9790SLin.Ling@Sun.COM static int 1539*9790SLin.Ling@Sun.COM vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 1540*9790SLin.Ling@Sun.COM size_t *rsz, boolean_t is_spare) 1541*9790SLin.Ling@Sun.COM { 1542*9790SLin.Ling@Sun.COM char *type; 1543*9790SLin.Ling@Sun.COM int ret; 1544*9790SLin.Ling@Sun.COM 1545*9790SLin.Ling@Sun.COM if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 1546*9790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 1547*9790SLin.Ling@Sun.COM 1548*9790SLin.Ling@Sun.COM if (strcmp(type, VDEV_TYPE_DISK) == 0) { 1549*9790SLin.Ling@Sun.COM /* 1550*9790SLin.Ling@Sun.COM * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 1551*9790SLin.Ling@Sun.COM * For a spare vdev, we only want to boot from the active 1552*9790SLin.Ling@Sun.COM * spare device. 1553*9790SLin.Ling@Sun.COM */ 1554*9790SLin.Ling@Sun.COM if (is_spare) { 1555*9790SLin.Ling@Sun.COM uint64_t spare = 0; 1556*9790SLin.Ling@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1557*9790SLin.Ling@Sun.COM &spare); 1558*9790SLin.Ling@Sun.COM if (!spare) 1559*9790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 1560*9790SLin.Ling@Sun.COM } 1561*9790SLin.Ling@Sun.COM 1562*9790SLin.Ling@Sun.COM if (vdev_online(nv)) { 1563*9790SLin.Ling@Sun.COM if ((ret = vdev_get_one_physpath(nv, physpath, 1564*9790SLin.Ling@Sun.COM phypath_size, rsz)) != 0) 1565*9790SLin.Ling@Sun.COM return (ret); 1566*9790SLin.Ling@Sun.COM } 1567*9790SLin.Ling@Sun.COM } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 1568*9790SLin.Ling@Sun.COM strcmp(type, VDEV_TYPE_REPLACING) == 0 || 1569*9790SLin.Ling@Sun.COM (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 1570*9790SLin.Ling@Sun.COM nvlist_t **child; 1571*9790SLin.Ling@Sun.COM uint_t count; 1572*9790SLin.Ling@Sun.COM int i, ret; 1573*9790SLin.Ling@Sun.COM 1574*9790SLin.Ling@Sun.COM if (nvlist_lookup_nvlist_array(nv, 1575*9790SLin.Ling@Sun.COM ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 1576*9790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 1577*9790SLin.Ling@Sun.COM 1578*9790SLin.Ling@Sun.COM for (i = 0; i < count; i++) { 1579*9790SLin.Ling@Sun.COM ret = vdev_get_physpaths(child[i], physpath, 1580*9790SLin.Ling@Sun.COM phypath_size, rsz, is_spare); 1581*9790SLin.Ling@Sun.COM if (ret == EZFS_NOSPC) 1582*9790SLin.Ling@Sun.COM return (ret); 1583*9790SLin.Ling@Sun.COM } 1584*9790SLin.Ling@Sun.COM } 1585*9790SLin.Ling@Sun.COM 1586*9790SLin.Ling@Sun.COM return (EZFS_POOL_INVALARG); 1587*9790SLin.Ling@Sun.COM } 1588*9790SLin.Ling@Sun.COM 15899160SSherry.Moore@Sun.COM /* 15909160SSherry.Moore@Sun.COM * Get phys_path for a root pool config. 15919160SSherry.Moore@Sun.COM * Return 0 on success; non-zero on failure. 15929160SSherry.Moore@Sun.COM */ 15939160SSherry.Moore@Sun.COM static int 15949160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 15959160SSherry.Moore@Sun.COM { 15969160SSherry.Moore@Sun.COM size_t rsz; 15977656SSherry.Moore@Sun.COM nvlist_t *vdev_root; 15987656SSherry.Moore@Sun.COM nvlist_t **child; 15997656SSherry.Moore@Sun.COM uint_t count; 16009160SSherry.Moore@Sun.COM char *type; 16019160SSherry.Moore@Sun.COM 16029160SSherry.Moore@Sun.COM rsz = 0; 16039160SSherry.Moore@Sun.COM 16049160SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 16059160SSherry.Moore@Sun.COM &vdev_root) != 0) 16069160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 16079160SSherry.Moore@Sun.COM 16089160SSherry.Moore@Sun.COM if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 16099160SSherry.Moore@Sun.COM nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 16109160SSherry.Moore@Sun.COM &child, &count) != 0) 16119160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 16127656SSherry.Moore@Sun.COM 16137656SSherry.Moore@Sun.COM /* 16149160SSherry.Moore@Sun.COM * root pool can not have EFI labeled disks and can only have 16159160SSherry.Moore@Sun.COM * a single top-level vdev. 16167656SSherry.Moore@Sun.COM */ 16179160SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 16189160SSherry.Moore@Sun.COM pool_uses_efi(vdev_root)) 16199160SSherry.Moore@Sun.COM return (EZFS_POOL_INVALARG); 16209160SSherry.Moore@Sun.COM 1621*9790SLin.Ling@Sun.COM (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 1622*9790SLin.Ling@Sun.COM B_FALSE); 16237656SSherry.Moore@Sun.COM 16249160SSherry.Moore@Sun.COM /* No online devices */ 16259160SSherry.Moore@Sun.COM if (rsz == 0) 16269160SSherry.Moore@Sun.COM return (EZFS_NODEVICE); 16279160SSherry.Moore@Sun.COM 16287656SSherry.Moore@Sun.COM return (0); 16297656SSherry.Moore@Sun.COM } 16307656SSherry.Moore@Sun.COM 16312468Sek110237 /* 16329160SSherry.Moore@Sun.COM * Get phys_path for a root pool 16339160SSherry.Moore@Sun.COM * Return 0 on success; non-zero on failure. 16349160SSherry.Moore@Sun.COM */ 16359160SSherry.Moore@Sun.COM int 16369160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 16379160SSherry.Moore@Sun.COM { 16389160SSherry.Moore@Sun.COM return (zpool_get_config_physpath(zhp->zpool_config, physpath, 16399160SSherry.Moore@Sun.COM phypath_size)); 16409160SSherry.Moore@Sun.COM } 16419160SSherry.Moore@Sun.COM 16429160SSherry.Moore@Sun.COM /* 16435450Sbrendan * Returns TRUE if the given guid corresponds to the given type. 16445450Sbrendan * This is used to check for hot spares (INUSE or not), and level 2 cache 16455450Sbrendan * devices. 16462468Sek110237 */ 16472468Sek110237 static boolean_t 16485450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 16492468Sek110237 { 16505450Sbrendan uint64_t target_guid; 16512468Sek110237 nvlist_t *nvroot; 16525450Sbrendan nvlist_t **list; 16535450Sbrendan uint_t count; 16542468Sek110237 int i; 16552468Sek110237 16562468Sek110237 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 16572468Sek110237 &nvroot) == 0); 16585450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 16595450Sbrendan for (i = 0; i < count; i++) { 16605450Sbrendan verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 16615450Sbrendan &target_guid) == 0); 16625450Sbrendan if (guid == target_guid) 16632468Sek110237 return (B_TRUE); 16642468Sek110237 } 16652468Sek110237 } 16662468Sek110237 16672468Sek110237 return (B_FALSE); 16681544Seschrock } 16691544Seschrock 1670789Sahrens /* 16714451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 16724451Seschrock * ZFS_ONLINE_* flags. 1673789Sahrens */ 1674789Sahrens int 16754451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 16764451Seschrock vdev_state_t *newstate) 1677789Sahrens { 1678789Sahrens zfs_cmd_t zc = { 0 }; 1679789Sahrens char msg[1024]; 16802082Seschrock nvlist_t *tgt; 16815450Sbrendan boolean_t avail_spare, l2cache; 16822082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1683789Sahrens 16841544Seschrock (void) snprintf(msg, sizeof (msg), 16851544Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1686789Sahrens 16871544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16887326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 16897326SEric.Schrock@Sun.COM NULL)) == NULL) 16902082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1691789Sahrens 16922468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 16932468Sek110237 16945450Sbrendan if (avail_spare || 16955450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 16962082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16972082Seschrock 16984451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 16994451Seschrock zc.zc_obj = flags; 17004451Seschrock 17014543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 17024451Seschrock return (zpool_standard_error(hdl, errno, msg)); 17034451Seschrock 17044451Seschrock *newstate = zc.zc_cookie; 17054451Seschrock return (0); 1706789Sahrens } 1707789Sahrens 1708789Sahrens /* 1709789Sahrens * Take the specified vdev offline 1710789Sahrens */ 1711789Sahrens int 17124451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1713789Sahrens { 1714789Sahrens zfs_cmd_t zc = { 0 }; 1715789Sahrens char msg[1024]; 17162082Seschrock nvlist_t *tgt; 17175450Sbrendan boolean_t avail_spare, l2cache; 17182082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1719789Sahrens 17201544Seschrock (void) snprintf(msg, sizeof (msg), 17211544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 17221544Seschrock 1723789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17247326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 17257326SEric.Schrock@Sun.COM NULL)) == NULL) 17262082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 17272082Seschrock 17282468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 17292468Sek110237 17305450Sbrendan if (avail_spare || 17315450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 17322082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 17332082Seschrock 17344451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 17354451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 17361485Slling 17374543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1738789Sahrens return (0); 1739789Sahrens 1740789Sahrens switch (errno) { 17412082Seschrock case EBUSY: 1742789Sahrens 1743789Sahrens /* 1744789Sahrens * There are no other replicas of this device. 1745789Sahrens */ 17462082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 17472082Seschrock 17489701SGeorge.Wilson@Sun.COM case EEXIST: 17499701SGeorge.Wilson@Sun.COM /* 17509701SGeorge.Wilson@Sun.COM * The log device has unplayed logs 17519701SGeorge.Wilson@Sun.COM */ 17529701SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 17539701SGeorge.Wilson@Sun.COM 17542082Seschrock default: 17552082Seschrock return (zpool_standard_error(hdl, errno, msg)); 17562082Seschrock } 17572082Seschrock } 1758789Sahrens 17592082Seschrock /* 17604451Seschrock * Mark the given vdev faulted. 17614451Seschrock */ 17624451Seschrock int 17634451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 17644451Seschrock { 17654451Seschrock zfs_cmd_t zc = { 0 }; 17664451Seschrock char msg[1024]; 17674451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17684451Seschrock 17694451Seschrock (void) snprintf(msg, sizeof (msg), 17704451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 17714451Seschrock 17724451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17734451Seschrock zc.zc_guid = guid; 17744451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 17754451Seschrock 17764451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 17774451Seschrock return (0); 17784451Seschrock 17794451Seschrock switch (errno) { 17804451Seschrock case EBUSY: 17814451Seschrock 17824451Seschrock /* 17834451Seschrock * There are no other replicas of this device. 17844451Seschrock */ 17854451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 17864451Seschrock 17874451Seschrock default: 17884451Seschrock return (zpool_standard_error(hdl, errno, msg)); 17894451Seschrock } 17904451Seschrock 17914451Seschrock } 17924451Seschrock 17934451Seschrock /* 17944451Seschrock * Mark the given vdev degraded. 17954451Seschrock */ 17964451Seschrock int 17974451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 17984451Seschrock { 17994451Seschrock zfs_cmd_t zc = { 0 }; 18004451Seschrock char msg[1024]; 18014451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 18024451Seschrock 18034451Seschrock (void) snprintf(msg, sizeof (msg), 18044451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 18054451Seschrock 18064451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18074451Seschrock zc.zc_guid = guid; 18084451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 18094451Seschrock 18104451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 18114451Seschrock return (0); 18124451Seschrock 18134451Seschrock return (zpool_standard_error(hdl, errno, msg)); 18144451Seschrock } 18154451Seschrock 18164451Seschrock /* 18172082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 18182082Seschrock * a hot spare. 18192082Seschrock */ 18202082Seschrock static boolean_t 18212082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 18222082Seschrock { 18232082Seschrock nvlist_t **child; 18242082Seschrock uint_t c, children; 18252082Seschrock char *type; 18262082Seschrock 18272082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 18282082Seschrock &children) == 0) { 18292082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 18302082Seschrock &type) == 0); 18312082Seschrock 18322082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 18332082Seschrock children == 2 && child[which] == tgt) 18342082Seschrock return (B_TRUE); 18352082Seschrock 18362082Seschrock for (c = 0; c < children; c++) 18372082Seschrock if (is_replacing_spare(child[c], tgt, which)) 18382082Seschrock return (B_TRUE); 1839789Sahrens } 18402082Seschrock 18412082Seschrock return (B_FALSE); 1842789Sahrens } 1843789Sahrens 1844789Sahrens /* 1845789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 18464527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 1847789Sahrens */ 1848789Sahrens int 1849789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 1850789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1851789Sahrens { 1852789Sahrens zfs_cmd_t zc = { 0 }; 1853789Sahrens char msg[1024]; 1854789Sahrens int ret; 18552082Seschrock nvlist_t *tgt; 18567326SEric.Schrock@Sun.COM boolean_t avail_spare, l2cache, islog; 18577326SEric.Schrock@Sun.COM uint64_t val; 18587041Seschrock char *path, *newname; 18592082Seschrock nvlist_t **child; 18602082Seschrock uint_t children; 18612082Seschrock nvlist_t *config_root; 18622082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 18637965SGeorge.Wilson@Sun.COM boolean_t rootpool = pool_is_bootable(zhp); 1864789Sahrens 18651544Seschrock if (replacing) 18661544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 18671544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 18681544Seschrock else 18691544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 18701544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 18711544Seschrock 18727965SGeorge.Wilson@Sun.COM /* 18737965SGeorge.Wilson@Sun.COM * If this is a root pool, make sure that we're not attaching an 18747965SGeorge.Wilson@Sun.COM * EFI labeled device. 18757965SGeorge.Wilson@Sun.COM */ 18767965SGeorge.Wilson@Sun.COM if (rootpool && pool_uses_efi(nvroot)) { 18777965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18787965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root pools.")); 18797965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 18807965SGeorge.Wilson@Sun.COM } 18817965SGeorge.Wilson@Sun.COM 1882789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18837326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 18847326SEric.Schrock@Sun.COM &islog)) == 0) 18852082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 18862082Seschrock 18872468Sek110237 if (avail_spare) 18882082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 18892082Seschrock 18905450Sbrendan if (l2cache) 18915450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 18925450Sbrendan 18932082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 18942082Seschrock zc.zc_cookie = replacing; 18952082Seschrock 18962082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 18972082Seschrock &child, &children) != 0 || children != 1) { 18982082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18992082Seschrock "new device must be a single disk")); 19002082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 19011544Seschrock } 19022082Seschrock 19032082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 19042082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 19052082Seschrock 19067041Seschrock if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL) 19077041Seschrock return (-1); 19087041Seschrock 19092082Seschrock /* 19102082Seschrock * If the target is a hot spare that has been swapped in, we can only 19112082Seschrock * replace it with another hot spare. 19122082Seschrock */ 19132082Seschrock if (replacing && 19142082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 19157326SEric.Schrock@Sun.COM (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 19167326SEric.Schrock@Sun.COM NULL) == NULL || !avail_spare) && 19177326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 1)) { 19182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19192082Seschrock "can only be replaced by another hot spare")); 19207041Seschrock free(newname); 19212082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 19222082Seschrock } 19232082Seschrock 19242082Seschrock /* 19252082Seschrock * If we are attempting to replace a spare, it canot be applied to an 19262082Seschrock * already spared device. 19272082Seschrock */ 19282082Seschrock if (replacing && 19292082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 19307326SEric.Schrock@Sun.COM zpool_find_vdev(zhp, newname, &avail_spare, 19317326SEric.Schrock@Sun.COM &l2cache, NULL) != NULL && avail_spare && 19327326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 0)) { 19332082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19342082Seschrock "device has already been replaced with a spare")); 19357041Seschrock free(newname); 19362082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 19372082Seschrock } 1938789Sahrens 19397041Seschrock free(newname); 19407041Seschrock 19415094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 19422082Seschrock return (-1); 1943789Sahrens 19444543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1945789Sahrens 19462676Seschrock zcmd_free_nvlists(&zc); 1947789Sahrens 19487965SGeorge.Wilson@Sun.COM if (ret == 0) { 19497965SGeorge.Wilson@Sun.COM if (rootpool) { 19507965SGeorge.Wilson@Sun.COM /* 19517965SGeorge.Wilson@Sun.COM * XXX - This should be removed once we can 19527965SGeorge.Wilson@Sun.COM * automatically install the bootblocks on the 19537965SGeorge.Wilson@Sun.COM * newly attached disk. 19547965SGeorge.Wilson@Sun.COM */ 19557965SGeorge.Wilson@Sun.COM (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please " 19567965SGeorge.Wilson@Sun.COM "be sure to invoke %s to make '%s' bootable.\n"), 19577965SGeorge.Wilson@Sun.COM BOOTCMD, new_disk); 1958*9790SLin.Ling@Sun.COM 1959*9790SLin.Ling@Sun.COM /* 1960*9790SLin.Ling@Sun.COM * XXX need a better way to prevent user from 1961*9790SLin.Ling@Sun.COM * booting up a half-baked vdev. 1962*9790SLin.Ling@Sun.COM */ 1963*9790SLin.Ling@Sun.COM (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 1964*9790SLin.Ling@Sun.COM "sure to wait until resilver is done " 1965*9790SLin.Ling@Sun.COM "before rebooting.\n")); 19667965SGeorge.Wilson@Sun.COM } 1967789Sahrens return (0); 19687965SGeorge.Wilson@Sun.COM } 1969789Sahrens 1970789Sahrens switch (errno) { 19711544Seschrock case ENOTSUP: 1972789Sahrens /* 1973789Sahrens * Can't attach to or replace this type of vdev. 1974789Sahrens */ 19754527Sperrin if (replacing) { 19767326SEric.Schrock@Sun.COM if (islog) 19774527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19784527Sperrin "cannot replace a log with a spare")); 19794527Sperrin else 19804527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19814527Sperrin "cannot replace a replacing device")); 19824527Sperrin } else { 19832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19842082Seschrock "can only attach to mirrors and top-level " 19852082Seschrock "disks")); 19864527Sperrin } 19872082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1988789Sahrens break; 1989789Sahrens 19901544Seschrock case EINVAL: 1991789Sahrens /* 1992789Sahrens * The new device must be a single disk. 1993789Sahrens */ 19942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19952082Seschrock "new device must be a single disk")); 19962082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1997789Sahrens break; 1998789Sahrens 19991544Seschrock case EBUSY: 20002082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 20012082Seschrock new_disk); 20022082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2003789Sahrens break; 2004789Sahrens 20051544Seschrock case EOVERFLOW: 2006789Sahrens /* 2007789Sahrens * The new device is too small. 2008789Sahrens */ 20092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20102082Seschrock "device is too small")); 20112082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2012789Sahrens break; 2013789Sahrens 20141544Seschrock case EDOM: 2015789Sahrens /* 2016789Sahrens * The new device has a different alignment requirement. 2017789Sahrens */ 20182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20192082Seschrock "devices have different sector alignment")); 20202082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2021789Sahrens break; 2022789Sahrens 20231544Seschrock case ENAMETOOLONG: 2024789Sahrens /* 2025789Sahrens * The resulting top-level vdev spec won't fit in the label. 2026789Sahrens */ 20272082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2028789Sahrens break; 2029789Sahrens 20301544Seschrock default: 20312082Seschrock (void) zpool_standard_error(hdl, errno, msg); 2032789Sahrens } 2033789Sahrens 20342082Seschrock return (-1); 2035789Sahrens } 2036789Sahrens 2037789Sahrens /* 2038789Sahrens * Detach the specified device. 2039789Sahrens */ 2040789Sahrens int 2041789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2042789Sahrens { 2043789Sahrens zfs_cmd_t zc = { 0 }; 2044789Sahrens char msg[1024]; 20452082Seschrock nvlist_t *tgt; 20465450Sbrendan boolean_t avail_spare, l2cache; 20472082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2048789Sahrens 20491544Seschrock (void) snprintf(msg, sizeof (msg), 20501544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 20511544Seschrock 2052789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20537326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 20547326SEric.Schrock@Sun.COM NULL)) == 0) 20552082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2056789Sahrens 20572468Sek110237 if (avail_spare) 20582082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 20592082Seschrock 20605450Sbrendan if (l2cache) 20615450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 20625450Sbrendan 20632082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 20642082Seschrock 20654543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2066789Sahrens return (0); 2067789Sahrens 2068789Sahrens switch (errno) { 2069789Sahrens 20701544Seschrock case ENOTSUP: 2071789Sahrens /* 2072789Sahrens * Can't detach from this type of vdev. 2073789Sahrens */ 20742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 20752082Seschrock "applicable to mirror and replacing vdevs")); 20762082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 2077789Sahrens break; 2078789Sahrens 20791544Seschrock case EBUSY: 2080789Sahrens /* 2081789Sahrens * There are no other replicas of this device. 2082789Sahrens */ 20832082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2084789Sahrens break; 2085789Sahrens 20861544Seschrock default: 20872082Seschrock (void) zpool_standard_error(hdl, errno, msg); 20881544Seschrock } 20891544Seschrock 20902082Seschrock return (-1); 20912082Seschrock } 20922082Seschrock 20932082Seschrock /* 20945450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 20955450Sbrendan * and level 2 cache devices. 20962082Seschrock */ 20972082Seschrock int 20982082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 20992082Seschrock { 21002082Seschrock zfs_cmd_t zc = { 0 }; 21012082Seschrock char msg[1024]; 21022082Seschrock nvlist_t *tgt; 21035450Sbrendan boolean_t avail_spare, l2cache; 21042082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21052082Seschrock 21062082Seschrock (void) snprintf(msg, sizeof (msg), 21072082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 21082082Seschrock 21092082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21107326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 21117326SEric.Schrock@Sun.COM NULL)) == 0) 21122082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 21132082Seschrock 21145450Sbrendan if (!avail_spare && !l2cache) { 21152082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21165450Sbrendan "only inactive hot spares or cache devices " 21175450Sbrendan "can be removed")); 21182082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 21192082Seschrock } 21202082Seschrock 21212082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 21222082Seschrock 21234543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 21242082Seschrock return (0); 21252082Seschrock 21262082Seschrock return (zpool_standard_error(hdl, errno, msg)); 21271544Seschrock } 21281544Seschrock 21291544Seschrock /* 21301544Seschrock * Clear the errors for the pool, or the particular device if specified. 21311544Seschrock */ 21321544Seschrock int 21331544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path) 21341544Seschrock { 21351544Seschrock zfs_cmd_t zc = { 0 }; 21361544Seschrock char msg[1024]; 21372082Seschrock nvlist_t *tgt; 21385450Sbrendan boolean_t avail_spare, l2cache; 21392082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21401544Seschrock 21411544Seschrock if (path) 21421544Seschrock (void) snprintf(msg, sizeof (msg), 21431544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 21442676Seschrock path); 21451544Seschrock else 21461544Seschrock (void) snprintf(msg, sizeof (msg), 21471544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 21481544Seschrock zhp->zpool_name); 21491544Seschrock 21501544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21512082Seschrock if (path) { 21525450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 21537326SEric.Schrock@Sun.COM &l2cache, NULL)) == 0) 21542082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 21552082Seschrock 21565450Sbrendan /* 21575450Sbrendan * Don't allow error clearing for hot spares. Do allow 21585450Sbrendan * error clearing for l2cache devices. 21595450Sbrendan */ 21602468Sek110237 if (avail_spare) 21612082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 21622082Seschrock 21632082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 21642082Seschrock &zc.zc_guid) == 0); 21651544Seschrock } 21661544Seschrock 21674543Smarks if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 21681544Seschrock return (0); 21691544Seschrock 21702082Seschrock return (zpool_standard_error(hdl, errno, msg)); 2171789Sahrens } 2172789Sahrens 21733126Sahl /* 21744451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 21754451Seschrock */ 21764451Seschrock int 21774451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 21784451Seschrock { 21794451Seschrock zfs_cmd_t zc = { 0 }; 21804451Seschrock char msg[1024]; 21814451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21824451Seschrock 21834451Seschrock (void) snprintf(msg, sizeof (msg), 21844451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 21854451Seschrock guid); 21864451Seschrock 21874451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21884451Seschrock zc.zc_guid = guid; 21894451Seschrock 21904451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 21914451Seschrock return (0); 21924451Seschrock 21934451Seschrock return (zpool_standard_error(hdl, errno, msg)); 21944451Seschrock } 21954451Seschrock 21964451Seschrock /* 21973126Sahl * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 21983126Sahl * hierarchy. 21993126Sahl */ 22003126Sahl int 22013126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 22023126Sahl void *data) 2203789Sahrens { 22043126Sahl libzfs_handle_t *hdl = zhp->zpool_hdl; 22053126Sahl char (*paths)[MAXPATHLEN]; 22063126Sahl size_t size = 4; 22073126Sahl int curr, fd, base, ret = 0; 22083126Sahl DIR *dirp; 22093126Sahl struct dirent *dp; 22103126Sahl struct stat st; 22113126Sahl 22123126Sahl if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 22133126Sahl return (errno == ENOENT ? 0 : -1); 22143126Sahl 22153126Sahl if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 22163126Sahl int err = errno; 22173126Sahl (void) close(base); 22183126Sahl return (err == ENOENT ? 0 : -1); 22193126Sahl } 2220789Sahrens 2221789Sahrens /* 22223126Sahl * Oddly this wasn't a directory -- ignore that failure since we 22233126Sahl * know there are no links lower in the (non-existant) hierarchy. 2224789Sahrens */ 22253126Sahl if (!S_ISDIR(st.st_mode)) { 22263126Sahl (void) close(base); 22273126Sahl return (0); 22283126Sahl } 22293126Sahl 22303126Sahl if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 22313126Sahl (void) close(base); 22323126Sahl return (-1); 2233789Sahrens } 2234789Sahrens 22353126Sahl (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 22363126Sahl curr = 0; 22373126Sahl 22383126Sahl while (curr >= 0) { 22393126Sahl if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 22403126Sahl goto err; 22413126Sahl 22423126Sahl if (S_ISDIR(st.st_mode)) { 22433126Sahl if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 22443126Sahl goto err; 22453126Sahl 22463126Sahl if ((dirp = fdopendir(fd)) == NULL) { 22473126Sahl (void) close(fd); 22483126Sahl goto err; 22493126Sahl } 22503126Sahl 22513126Sahl while ((dp = readdir(dirp)) != NULL) { 22523126Sahl if (dp->d_name[0] == '.') 22533126Sahl continue; 22543126Sahl 22553126Sahl if (curr + 1 == size) { 22563126Sahl paths = zfs_realloc(hdl, paths, 22573126Sahl size * sizeof (paths[0]), 22583126Sahl size * 2 * sizeof (paths[0])); 22593126Sahl if (paths == NULL) { 22603126Sahl (void) closedir(dirp); 22613126Sahl (void) close(fd); 22623126Sahl goto err; 22633126Sahl } 22643126Sahl 22653126Sahl size *= 2; 22663126Sahl } 22673126Sahl 22683126Sahl (void) strlcpy(paths[curr + 1], paths[curr], 22693126Sahl sizeof (paths[curr + 1])); 22703126Sahl (void) strlcat(paths[curr], "/", 22713126Sahl sizeof (paths[curr])); 22723126Sahl (void) strlcat(paths[curr], dp->d_name, 22733126Sahl sizeof (paths[curr])); 22743126Sahl curr++; 22753126Sahl } 22763126Sahl 22773126Sahl (void) closedir(dirp); 22783126Sahl 22793126Sahl } else { 22803126Sahl if ((ret = cb(paths[curr], data)) != 0) 22813126Sahl break; 22823126Sahl } 22833126Sahl 22843126Sahl curr--; 22853126Sahl } 22863126Sahl 22873126Sahl free(paths); 22883126Sahl (void) close(base); 22893126Sahl 22903126Sahl return (ret); 22913126Sahl 22923126Sahl err: 22933126Sahl free(paths); 22943126Sahl (void) close(base); 22953126Sahl return (-1); 22963126Sahl } 22973126Sahl 22983126Sahl typedef struct zvol_cb { 22993126Sahl zpool_handle_t *zcb_pool; 23003126Sahl boolean_t zcb_create; 23013126Sahl } zvol_cb_t; 23023126Sahl 23033126Sahl /*ARGSUSED*/ 23043126Sahl static int 23053126Sahl do_zvol_create(zfs_handle_t *zhp, void *data) 23063126Sahl { 23074657Sahrens int ret = 0; 23083126Sahl 23094657Sahrens if (ZFS_IS_VOLUME(zhp)) { 23103126Sahl (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 23114657Sahrens ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 23124657Sahrens } 23133126Sahl 23144657Sahrens if (ret == 0) 23154657Sahrens ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 2316789Sahrens 2317789Sahrens zfs_close(zhp); 23183126Sahl 2319789Sahrens return (ret); 2320789Sahrens } 2321789Sahrens 2322789Sahrens /* 2323789Sahrens * Iterate over all zvols in the pool and make any necessary minor nodes. 2324789Sahrens */ 2325789Sahrens int 2326789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp) 2327789Sahrens { 2328789Sahrens zfs_handle_t *zfp; 2329789Sahrens int ret; 2330789Sahrens 2331789Sahrens /* 2332789Sahrens * If the pool is unavailable, just return success. 2333789Sahrens */ 23342082Seschrock if ((zfp = make_dataset_handle(zhp->zpool_hdl, 23352082Seschrock zhp->zpool_name)) == NULL) 2336789Sahrens return (0); 2337789Sahrens 23384657Sahrens ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 2339789Sahrens 2340789Sahrens zfs_close(zfp); 2341789Sahrens return (ret); 2342789Sahrens } 2343789Sahrens 23443126Sahl static int 23453126Sahl do_zvol_remove(const char *dataset, void *data) 23463126Sahl { 23473126Sahl zpool_handle_t *zhp = data; 23483126Sahl 23493126Sahl return (zvol_remove_link(zhp->zpool_hdl, dataset)); 23503126Sahl } 23513126Sahl 2352789Sahrens /* 23533126Sahl * Iterate over all zvols in the pool and remove any minor nodes. We iterate 23543126Sahl * by examining the /dev links so that a corrupted pool doesn't impede this 23553126Sahl * operation. 2356789Sahrens */ 2357789Sahrens int 2358789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp) 2359789Sahrens { 23603126Sahl return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 2361789Sahrens } 23621354Seschrock 23631354Seschrock /* 23641354Seschrock * Convert from a devid string to a path. 23651354Seschrock */ 23661354Seschrock static char * 23671354Seschrock devid_to_path(char *devid_str) 23681354Seschrock { 23691354Seschrock ddi_devid_t devid; 23701354Seschrock char *minor; 23711354Seschrock char *path; 23721354Seschrock devid_nmlist_t *list = NULL; 23731354Seschrock int ret; 23741354Seschrock 23751354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 23761354Seschrock return (NULL); 23771354Seschrock 23781354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 23791354Seschrock 23801354Seschrock devid_str_free(minor); 23811354Seschrock devid_free(devid); 23821354Seschrock 23831354Seschrock if (ret != 0) 23841354Seschrock return (NULL); 23851354Seschrock 23862082Seschrock if ((path = strdup(list[0].devname)) == NULL) 23872082Seschrock return (NULL); 23882082Seschrock 23891354Seschrock devid_free_nmlist(list); 23901354Seschrock 23911354Seschrock return (path); 23921354Seschrock } 23931354Seschrock 23941354Seschrock /* 23951354Seschrock * Convert from a path to a devid string. 23961354Seschrock */ 23971354Seschrock static char * 23981354Seschrock path_to_devid(const char *path) 23991354Seschrock { 24001354Seschrock int fd; 24011354Seschrock ddi_devid_t devid; 24021354Seschrock char *minor, *ret; 24031354Seschrock 24041354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 24051354Seschrock return (NULL); 24061354Seschrock 24071354Seschrock minor = NULL; 24081354Seschrock ret = NULL; 24091354Seschrock if (devid_get(fd, &devid) == 0) { 24101354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 24111354Seschrock ret = devid_str_encode(devid, minor); 24121354Seschrock if (minor != NULL) 24131354Seschrock devid_str_free(minor); 24141354Seschrock devid_free(devid); 24151354Seschrock } 24161354Seschrock (void) close(fd); 24171354Seschrock 24181354Seschrock return (ret); 24191354Seschrock } 24201354Seschrock 24211354Seschrock /* 24221354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 24231354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 24241354Seschrock * type 'zpool status', and we'll display the correct information anyway. 24251354Seschrock */ 24261354Seschrock static void 24271354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 24281354Seschrock { 24291354Seschrock zfs_cmd_t zc = { 0 }; 24301354Seschrock 24311354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 24322676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 24331354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 24341544Seschrock &zc.zc_guid) == 0); 24351354Seschrock 24362082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 24371354Seschrock } 24381354Seschrock 24391354Seschrock /* 24401354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 24411354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 24421354Seschrock * We also check if this is a whole disk, in which case we strip off the 24431354Seschrock * trailing 's0' slice name. 24441354Seschrock * 24451354Seschrock * This routine is also responsible for identifying when disks have been 24461354Seschrock * reconfigured in a new location. The kernel will have opened the device by 24471354Seschrock * devid, but the path will still refer to the old location. To catch this, we 24481354Seschrock * first do a path -> devid translation (which is fast for the common case). If 24491354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 24501354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 24511354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 24521354Seschrock * of these checks. 24531354Seschrock */ 24541354Seschrock char * 24552082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 24561354Seschrock { 24571354Seschrock char *path, *devid; 24581544Seschrock uint64_t value; 24591544Seschrock char buf[64]; 24604451Seschrock vdev_stat_t *vs; 24614451Seschrock uint_t vsc; 24621354Seschrock 24631544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 24641544Seschrock &value) == 0) { 24651544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 24661544Seschrock &value) == 0); 24672856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 24682856Snd150628 (u_longlong_t)value); 24691544Seschrock path = buf; 24701544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 24711354Seschrock 24724451Seschrock /* 24734451Seschrock * If the device is dead (faulted, offline, etc) then don't 24744451Seschrock * bother opening it. Otherwise we may be forcing the user to 24754451Seschrock * open a misbehaving device, which can have undesirable 24764451Seschrock * effects. 24774451Seschrock */ 24784451Seschrock if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 24794451Seschrock (uint64_t **)&vs, &vsc) != 0 || 24804451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 24814451Seschrock zhp != NULL && 24821354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 24831354Seschrock /* 24841354Seschrock * Determine if the current path is correct. 24851354Seschrock */ 24861354Seschrock char *newdevid = path_to_devid(path); 24871354Seschrock 24881354Seschrock if (newdevid == NULL || 24891354Seschrock strcmp(devid, newdevid) != 0) { 24901354Seschrock char *newpath; 24911354Seschrock 24921354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 24931354Seschrock /* 24941354Seschrock * Update the path appropriately. 24951354Seschrock */ 24961354Seschrock set_path(zhp, nv, newpath); 24972082Seschrock if (nvlist_add_string(nv, 24982082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 24992082Seschrock verify(nvlist_lookup_string(nv, 25002082Seschrock ZPOOL_CONFIG_PATH, 25012082Seschrock &path) == 0); 25021354Seschrock free(newpath); 25031354Seschrock } 25041354Seschrock } 25051354Seschrock 25062082Seschrock if (newdevid) 25072082Seschrock devid_str_free(newdevid); 25081354Seschrock } 25091354Seschrock 25101354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 25111354Seschrock path += 9; 25121354Seschrock 25131354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 25141544Seschrock &value) == 0 && value) { 25152082Seschrock char *tmp = zfs_strdup(hdl, path); 25162082Seschrock if (tmp == NULL) 25172082Seschrock return (NULL); 25181354Seschrock tmp[strlen(path) - 2] = '\0'; 25191354Seschrock return (tmp); 25201354Seschrock } 25211354Seschrock } else { 25221354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 25232082Seschrock 25242082Seschrock /* 25252082Seschrock * If it's a raidz device, we need to stick in the parity level. 25262082Seschrock */ 25272082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 25282082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 25292082Seschrock &value) == 0); 25302082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 25312856Snd150628 (u_longlong_t)value); 25322082Seschrock path = buf; 25332082Seschrock } 25341354Seschrock } 25351354Seschrock 25362082Seschrock return (zfs_strdup(hdl, path)); 25371354Seschrock } 25381544Seschrock 25391544Seschrock static int 25401544Seschrock zbookmark_compare(const void *a, const void *b) 25411544Seschrock { 25421544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 25431544Seschrock } 25441544Seschrock 25451544Seschrock /* 25461544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 25471544Seschrock * caller. 25481544Seschrock */ 25491544Seschrock int 25503444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 25511544Seschrock { 25521544Seschrock zfs_cmd_t zc = { 0 }; 25531544Seschrock uint64_t count; 25542676Seschrock zbookmark_t *zb = NULL; 25553444Sek110237 int i; 25561544Seschrock 25571544Seschrock /* 25581544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 25591544Seschrock * has increased, allocate more space and continue until we get the 25601544Seschrock * entire list. 25611544Seschrock */ 25621544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 25631544Seschrock &count) == 0); 25644820Sek110237 if (count == 0) 25654820Sek110237 return (0); 25662676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 25672856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 25682082Seschrock return (-1); 25692676Seschrock zc.zc_nvlist_dst_size = count; 25701544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 25711544Seschrock for (;;) { 25722082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 25732082Seschrock &zc) != 0) { 25742676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 25751544Seschrock if (errno == ENOMEM) { 25763823Svb160487 count = zc.zc_nvlist_dst_size; 25772676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 25783823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 25793823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 25802082Seschrock return (-1); 25811544Seschrock } else { 25821544Seschrock return (-1); 25831544Seschrock } 25841544Seschrock } else { 25851544Seschrock break; 25861544Seschrock } 25871544Seschrock } 25881544Seschrock 25891544Seschrock /* 25901544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 25911544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 25922676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 25931544Seschrock * _not_ copied as part of the process. So we point the start of our 25941544Seschrock * array appropriate and decrement the total number of elements. 25951544Seschrock */ 25962676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 25972676Seschrock zc.zc_nvlist_dst_size; 25982676Seschrock count -= zc.zc_nvlist_dst_size; 25991544Seschrock 26001544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 26011544Seschrock 26023444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 26031544Seschrock 26041544Seschrock /* 26053444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 26061544Seschrock */ 26071544Seschrock for (i = 0; i < count; i++) { 26081544Seschrock nvlist_t *nv; 26091544Seschrock 26103700Sek110237 /* ignoring zb_blkid and zb_level for now */ 26113700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 26123700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 26131544Seschrock continue; 26141544Seschrock 26153444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 26163444Sek110237 goto nomem; 26173444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 26183444Sek110237 zb[i].zb_objset) != 0) { 26193444Sek110237 nvlist_free(nv); 26202082Seschrock goto nomem; 26213444Sek110237 } 26223444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 26233444Sek110237 zb[i].zb_object) != 0) { 26243444Sek110237 nvlist_free(nv); 26253444Sek110237 goto nomem; 26261544Seschrock } 26273444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 26283444Sek110237 nvlist_free(nv); 26293444Sek110237 goto nomem; 26303444Sek110237 } 26313444Sek110237 nvlist_free(nv); 26321544Seschrock } 26331544Seschrock 26343265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 26351544Seschrock return (0); 26362082Seschrock 26372082Seschrock nomem: 26382676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 26392082Seschrock return (no_memory(zhp->zpool_hdl)); 26401544Seschrock } 26411760Seschrock 26421760Seschrock /* 26431760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 26441760Seschrock */ 26451760Seschrock int 26465094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 26471760Seschrock { 26481760Seschrock zfs_cmd_t zc = { 0 }; 26492082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 26501760Seschrock 26511760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 26525094Slling zc.zc_cookie = new_version; 26535094Slling 26544543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 26553237Slling return (zpool_standard_error_fmt(hdl, errno, 26562082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 26572082Seschrock zhp->zpool_name)); 26581760Seschrock return (0); 26591760Seschrock } 26602926Sek110237 26614988Sek110237 void 26624988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 26634988Sek110237 char *history_str) 26644988Sek110237 { 26654988Sek110237 int i; 26664988Sek110237 26674988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 26684988Sek110237 for (i = 1; i < argc; i++) { 26694988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 26704988Sek110237 HIS_MAX_RECORD_LEN) 26714988Sek110237 break; 26724988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 26734988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 26744988Sek110237 } 26754988Sek110237 } 26764988Sek110237 26772926Sek110237 /* 26784988Sek110237 * Stage command history for logging. 26792926Sek110237 */ 26804988Sek110237 int 26814988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 26822926Sek110237 { 26834988Sek110237 if (history_str == NULL) 26844988Sek110237 return (EINVAL); 26854988Sek110237 26864988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 26874988Sek110237 return (EINVAL); 26882926Sek110237 26894715Sek110237 if (hdl->libzfs_log_str != NULL) 26904543Smarks free(hdl->libzfs_log_str); 26912926Sek110237 26924988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 26934988Sek110237 return (no_memory(hdl)); 26944543Smarks 26954988Sek110237 return (0); 26962926Sek110237 } 26972926Sek110237 26982926Sek110237 /* 26992926Sek110237 * Perform ioctl to get some command history of a pool. 27002926Sek110237 * 27012926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 27022926Sek110237 * logical offset of the history buffer to start reading from. 27032926Sek110237 * 27042926Sek110237 * Upon return, 'off' is the next logical offset to read from and 27052926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 27062926Sek110237 */ 27072926Sek110237 static int 27082926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 27092926Sek110237 { 27102926Sek110237 zfs_cmd_t zc = { 0 }; 27112926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 27122926Sek110237 27132926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 27142926Sek110237 27152926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 27162926Sek110237 zc.zc_history_len = *len; 27172926Sek110237 zc.zc_history_offset = *off; 27182926Sek110237 27192926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 27202926Sek110237 switch (errno) { 27212926Sek110237 case EPERM: 27223237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 27233237Slling dgettext(TEXT_DOMAIN, 27242926Sek110237 "cannot show history for pool '%s'"), 27252926Sek110237 zhp->zpool_name)); 27262926Sek110237 case ENOENT: 27273237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 27282926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 27292926Sek110237 "'%s'"), zhp->zpool_name)); 27303863Sek110237 case ENOTSUP: 27313863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 27323863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 27333863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 27342926Sek110237 default: 27353237Slling return (zpool_standard_error_fmt(hdl, errno, 27362926Sek110237 dgettext(TEXT_DOMAIN, 27372926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 27382926Sek110237 } 27392926Sek110237 } 27402926Sek110237 27412926Sek110237 *len = zc.zc_history_len; 27422926Sek110237 *off = zc.zc_history_offset; 27432926Sek110237 27442926Sek110237 return (0); 27452926Sek110237 } 27462926Sek110237 27472926Sek110237 /* 27482926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 27492926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 27502926Sek110237 * processed as there wasn't a complete record. 27512926Sek110237 */ 27522926Sek110237 static int 27532926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 27542926Sek110237 nvlist_t ***records, uint_t *numrecords) 27552926Sek110237 { 27562926Sek110237 uint64_t reclen; 27572926Sek110237 nvlist_t *nv; 27582926Sek110237 int i; 27592926Sek110237 27602926Sek110237 while (bytes_read > sizeof (reclen)) { 27612926Sek110237 27622926Sek110237 /* get length of packed record (stored as little endian) */ 27632926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 27642926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 27652926Sek110237 27662926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 27672926Sek110237 break; 27682926Sek110237 27692926Sek110237 /* unpack record */ 27702926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 27712926Sek110237 return (ENOMEM); 27722926Sek110237 bytes_read -= sizeof (reclen) + reclen; 27732926Sek110237 buf += sizeof (reclen) + reclen; 27742926Sek110237 27752926Sek110237 /* add record to nvlist array */ 27762926Sek110237 (*numrecords)++; 27772926Sek110237 if (ISP2(*numrecords + 1)) { 27782926Sek110237 *records = realloc(*records, 27792926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 27802926Sek110237 } 27812926Sek110237 (*records)[*numrecords - 1] = nv; 27822926Sek110237 } 27832926Sek110237 27842926Sek110237 *leftover = bytes_read; 27852926Sek110237 return (0); 27862926Sek110237 } 27872926Sek110237 27882926Sek110237 #define HIS_BUF_LEN (128*1024) 27892926Sek110237 27902926Sek110237 /* 27912926Sek110237 * Retrieve the command history of a pool. 27922926Sek110237 */ 27932926Sek110237 int 27942926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 27952926Sek110237 { 27962926Sek110237 char buf[HIS_BUF_LEN]; 27972926Sek110237 uint64_t off = 0; 27982926Sek110237 nvlist_t **records = NULL; 27992926Sek110237 uint_t numrecords = 0; 28002926Sek110237 int err, i; 28012926Sek110237 28022926Sek110237 do { 28032926Sek110237 uint64_t bytes_read = sizeof (buf); 28042926Sek110237 uint64_t leftover; 28052926Sek110237 28062926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 28072926Sek110237 break; 28082926Sek110237 28092926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 28102926Sek110237 if (!bytes_read) 28112926Sek110237 break; 28122926Sek110237 28132926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 28142926Sek110237 &leftover, &records, &numrecords)) != 0) 28152926Sek110237 break; 28162926Sek110237 off -= leftover; 28172926Sek110237 28182926Sek110237 /* CONSTCOND */ 28192926Sek110237 } while (1); 28202926Sek110237 28212926Sek110237 if (!err) { 28222926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 28232926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 28242926Sek110237 records, numrecords) == 0); 28252926Sek110237 } 28262926Sek110237 for (i = 0; i < numrecords; i++) 28272926Sek110237 nvlist_free(records[i]); 28282926Sek110237 free(records); 28292926Sek110237 28302926Sek110237 return (err); 28312926Sek110237 } 28323444Sek110237 28333444Sek110237 void 28343444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 28353444Sek110237 char *pathname, size_t len) 28363444Sek110237 { 28373444Sek110237 zfs_cmd_t zc = { 0 }; 28383444Sek110237 boolean_t mounted = B_FALSE; 28393444Sek110237 char *mntpnt = NULL; 28403444Sek110237 char dsname[MAXNAMELEN]; 28413444Sek110237 28423444Sek110237 if (dsobj == 0) { 28433444Sek110237 /* special case for the MOS */ 28443444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 28453444Sek110237 return; 28463444Sek110237 } 28473444Sek110237 28483444Sek110237 /* get the dataset's name */ 28493444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 28503444Sek110237 zc.zc_obj = dsobj; 28513444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 28523444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 28533444Sek110237 /* just write out a path of two object numbers */ 28543444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 28553444Sek110237 dsobj, obj); 28563444Sek110237 return; 28573444Sek110237 } 28583444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 28593444Sek110237 28603444Sek110237 /* find out if the dataset is mounted */ 28613444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 28623444Sek110237 28633444Sek110237 /* get the corrupted object's path */ 28643444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 28653444Sek110237 zc.zc_obj = obj; 28663444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 28673444Sek110237 &zc) == 0) { 28683444Sek110237 if (mounted) { 28693444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 28703444Sek110237 zc.zc_value); 28713444Sek110237 } else { 28723444Sek110237 (void) snprintf(pathname, len, "%s:%s", 28733444Sek110237 dsname, zc.zc_value); 28743444Sek110237 } 28753444Sek110237 } else { 28763444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 28773444Sek110237 } 28783444Sek110237 free(mntpnt); 28793444Sek110237 } 28803912Slling 28814276Staylor #define RDISK_ROOT "/dev/rdsk" 28824276Staylor #define BACKUP_SLICE "s2" 28834276Staylor /* 28844276Staylor * Don't start the slice at the default block of 34; many storage 28854276Staylor * devices will use a stripe width of 128k, so start there instead. 28864276Staylor */ 28874276Staylor #define NEW_START_BLOCK 256 28884276Staylor 28894276Staylor /* 28907042Sgw25295 * Read the EFI label from the config, if a label does not exist then 28917042Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 28927042Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 28937042Sgw25295 * partition. 28947042Sgw25295 */ 28957042Sgw25295 static int 28967042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 28977042Sgw25295 { 28987042Sgw25295 char *path; 28997042Sgw25295 int fd; 29007042Sgw25295 char diskname[MAXPATHLEN]; 29017042Sgw25295 int err = -1; 29027042Sgw25295 29037042Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 29047042Sgw25295 return (err); 29057042Sgw25295 29067042Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 29077042Sgw25295 strrchr(path, '/')); 29087042Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 29097042Sgw25295 struct dk_gpt *vtoc; 29107042Sgw25295 29117042Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 29127042Sgw25295 if (sb != NULL) 29137042Sgw25295 *sb = vtoc->efi_parts[0].p_start; 29147042Sgw25295 efi_free(vtoc); 29157042Sgw25295 } 29167042Sgw25295 (void) close(fd); 29177042Sgw25295 } 29187042Sgw25295 return (err); 29197042Sgw25295 } 29207042Sgw25295 29217042Sgw25295 /* 29224276Staylor * determine where a partition starts on a disk in the current 29234276Staylor * configuration 29244276Staylor */ 29254276Staylor static diskaddr_t 29264276Staylor find_start_block(nvlist_t *config) 29274276Staylor { 29284276Staylor nvlist_t **child; 29294276Staylor uint_t c, children; 29304276Staylor diskaddr_t sb = MAXOFFSET_T; 29314276Staylor uint64_t wholedisk; 29324276Staylor 29334276Staylor if (nvlist_lookup_nvlist_array(config, 29344276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 29354276Staylor if (nvlist_lookup_uint64(config, 29364276Staylor ZPOOL_CONFIG_WHOLE_DISK, 29374276Staylor &wholedisk) != 0 || !wholedisk) { 29384276Staylor return (MAXOFFSET_T); 29394276Staylor } 29407042Sgw25295 if (read_efi_label(config, &sb) < 0) 29417042Sgw25295 sb = MAXOFFSET_T; 29424276Staylor return (sb); 29434276Staylor } 29444276Staylor 29454276Staylor for (c = 0; c < children; c++) { 29464276Staylor sb = find_start_block(child[c]); 29474276Staylor if (sb != MAXOFFSET_T) { 29484276Staylor return (sb); 29494276Staylor } 29504276Staylor } 29514276Staylor return (MAXOFFSET_T); 29524276Staylor } 29534276Staylor 29544276Staylor /* 29554276Staylor * Label an individual disk. The name provided is the short name, 29564276Staylor * stripped of any leading /dev path. 29574276Staylor */ 29584276Staylor int 29594276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 29604276Staylor { 29614276Staylor char path[MAXPATHLEN]; 29624276Staylor struct dk_gpt *vtoc; 29634276Staylor int fd; 29644276Staylor size_t resv = EFI_MIN_RESV_SIZE; 29654276Staylor uint64_t slice_size; 29664276Staylor diskaddr_t start_block; 29674276Staylor char errbuf[1024]; 29684276Staylor 29696289Smmusante /* prepare an error message just in case */ 29706289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 29716289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 29726289Smmusante 29734276Staylor if (zhp) { 29744276Staylor nvlist_t *nvroot; 29754276Staylor 29767965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp)) { 29777965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29787965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root " 29797965SGeorge.Wilson@Sun.COM "pools.")); 29807965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 29817965SGeorge.Wilson@Sun.COM } 29827965SGeorge.Wilson@Sun.COM 29834276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 29844276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 29854276Staylor 29864276Staylor if (zhp->zpool_start_block == 0) 29874276Staylor start_block = find_start_block(nvroot); 29884276Staylor else 29894276Staylor start_block = zhp->zpool_start_block; 29904276Staylor zhp->zpool_start_block = start_block; 29914276Staylor } else { 29924276Staylor /* new pool */ 29934276Staylor start_block = NEW_START_BLOCK; 29944276Staylor } 29954276Staylor 29964276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 29974276Staylor BACKUP_SLICE); 29984276Staylor 29994276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 30004276Staylor /* 30014276Staylor * This shouldn't happen. We've long since verified that this 30024276Staylor * is a valid device. 30034276Staylor */ 30046289Smmusante zfs_error_aux(hdl, 30056289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 30064276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 30074276Staylor } 30084276Staylor 30094276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 30104276Staylor /* 30114276Staylor * The only way this can fail is if we run out of memory, or we 30124276Staylor * were unable to read the disk's capacity 30134276Staylor */ 30144276Staylor if (errno == ENOMEM) 30154276Staylor (void) no_memory(hdl); 30164276Staylor 30174276Staylor (void) close(fd); 30186289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30196289Smmusante "unable to read disk capacity"), name); 30204276Staylor 30214276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 30224276Staylor } 30234276Staylor 30244276Staylor slice_size = vtoc->efi_last_u_lba + 1; 30254276Staylor slice_size -= EFI_MIN_RESV_SIZE; 30264276Staylor if (start_block == MAXOFFSET_T) 30274276Staylor start_block = NEW_START_BLOCK; 30284276Staylor slice_size -= start_block; 30294276Staylor 30304276Staylor vtoc->efi_parts[0].p_start = start_block; 30314276Staylor vtoc->efi_parts[0].p_size = slice_size; 30324276Staylor 30334276Staylor /* 30344276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 30354276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 30364276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 30374276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 30384276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 30394276Staylor * can get, in the absence of V_OTHER. 30404276Staylor */ 30414276Staylor vtoc->efi_parts[0].p_tag = V_USR; 30424276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 30434276Staylor 30444276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 30454276Staylor vtoc->efi_parts[8].p_size = resv; 30464276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 30474276Staylor 30484276Staylor if (efi_write(fd, vtoc) != 0) { 30494276Staylor /* 30504276Staylor * Some block drivers (like pcata) may not support EFI 30514276Staylor * GPT labels. Print out a helpful error message dir- 30524276Staylor * ecting the user to manually label the disk and give 30534276Staylor * a specific slice. 30544276Staylor */ 30554276Staylor (void) close(fd); 30564276Staylor efi_free(vtoc); 30574276Staylor 30584276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30596289Smmusante "try using fdisk(1M) and then provide a specific slice")); 30604276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 30614276Staylor } 30624276Staylor 30634276Staylor (void) close(fd); 30644276Staylor efi_free(vtoc); 30654276Staylor return (0); 30664276Staylor } 30676423Sgw25295 30686423Sgw25295 static boolean_t 30696423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 30706423Sgw25295 { 30716423Sgw25295 char *type; 30726423Sgw25295 nvlist_t **child; 30736423Sgw25295 uint_t children, c; 30746423Sgw25295 30756423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 30766423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 30776423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 30786423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 30796423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 30806423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30816423Sgw25295 "vdev type '%s' is not supported"), type); 30826423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 30836423Sgw25295 return (B_FALSE); 30846423Sgw25295 } 30856423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 30866423Sgw25295 &child, &children) == 0) { 30876423Sgw25295 for (c = 0; c < children; c++) { 30886423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 30896423Sgw25295 return (B_FALSE); 30906423Sgw25295 } 30916423Sgw25295 } 30926423Sgw25295 return (B_TRUE); 30936423Sgw25295 } 30946423Sgw25295 30956423Sgw25295 /* 30966423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 30976423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 30986423Sgw25295 */ 30996423Sgw25295 int 31006423Sgw25295 zvol_check_dump_config(char *arg) 31016423Sgw25295 { 31026423Sgw25295 zpool_handle_t *zhp = NULL; 31036423Sgw25295 nvlist_t *config, *nvroot; 31046423Sgw25295 char *p, *volname; 31056423Sgw25295 nvlist_t **top; 31066423Sgw25295 uint_t toplevels; 31076423Sgw25295 libzfs_handle_t *hdl; 31086423Sgw25295 char errbuf[1024]; 31096423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 31106423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 31116423Sgw25295 int ret = 1; 31126423Sgw25295 31136423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 31146423Sgw25295 return (-1); 31156423Sgw25295 } 31166423Sgw25295 31176423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31186423Sgw25295 "dump is not supported on device '%s'"), arg); 31196423Sgw25295 31206423Sgw25295 if ((hdl = libzfs_init()) == NULL) 31216423Sgw25295 return (1); 31226423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 31236423Sgw25295 31246423Sgw25295 volname = arg + pathlen; 31256423Sgw25295 31266423Sgw25295 /* check the configuration of the pool */ 31276423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 31286423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31296423Sgw25295 "malformed dataset name")); 31306423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 31316423Sgw25295 return (1); 31326423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 31336423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31346423Sgw25295 "dataset name is too long")); 31356423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 31366423Sgw25295 return (1); 31376423Sgw25295 } else { 31386423Sgw25295 (void) strncpy(poolname, volname, p - volname); 31396423Sgw25295 poolname[p - volname] = '\0'; 31406423Sgw25295 } 31416423Sgw25295 31426423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 31436423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31446423Sgw25295 "could not open pool '%s'"), poolname); 31456423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 31466423Sgw25295 goto out; 31476423Sgw25295 } 31486423Sgw25295 config = zpool_get_config(zhp, NULL); 31496423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 31506423Sgw25295 &nvroot) != 0) { 31516423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31526423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 31536423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 31546423Sgw25295 goto out; 31556423Sgw25295 } 31566423Sgw25295 31576423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 31586423Sgw25295 &top, &toplevels) == 0); 31596423Sgw25295 if (toplevels != 1) { 31606423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31616423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 31626423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 31636423Sgw25295 goto out; 31646423Sgw25295 } 31656423Sgw25295 31666423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 31676423Sgw25295 goto out; 31686423Sgw25295 } 31696423Sgw25295 ret = 0; 31706423Sgw25295 31716423Sgw25295 out: 31726423Sgw25295 if (zhp) 31736423Sgw25295 zpool_close(zhp); 31746423Sgw25295 libzfs_fini(hdl); 31756423Sgw25295 return (ret); 31766423Sgw25295 } 3177