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 /* 23*12296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <ctype.h> 27789Sahrens #include <errno.h> 28789Sahrens #include <devid.h> 29789Sahrens #include <fcntl.h> 30789Sahrens #include <libintl.h> 31789Sahrens #include <stdio.h> 32789Sahrens #include <stdlib.h> 333126Sahl #include <strings.h> 34789Sahrens #include <unistd.h> 354276Staylor #include <sys/efi_partition.h> 364276Staylor #include <sys/vtoc.h> 37789Sahrens #include <sys/zfs_ioctl.h> 389816SGeorge.Wilson@Sun.COM #include <dlfcn.h> 39789Sahrens 40789Sahrens #include "zfs_namecheck.h" 413912Slling #include "zfs_prop.h" 42789Sahrens #include "libzfs_impl.h" 4310921STim.Haley@Sun.COM #include "zfs_comutil.h" 44789Sahrens 457042Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 465094Slling 477965SGeorge.Wilson@Sun.COM #if defined(__i386) || defined(__amd64) 487965SGeorge.Wilson@Sun.COM #define BOOTCMD "installgrub(1M)" 497965SGeorge.Wilson@Sun.COM #else 507965SGeorge.Wilson@Sun.COM #define BOOTCMD "installboot(1M)" 517965SGeorge.Wilson@Sun.COM #endif 527965SGeorge.Wilson@Sun.COM 539816SGeorge.Wilson@Sun.COM #define DISK_ROOT "/dev/dsk" 549816SGeorge.Wilson@Sun.COM #define RDISK_ROOT "/dev/rdsk" 559816SGeorge.Wilson@Sun.COM #define BACKUP_SLICE "s2" 569816SGeorge.Wilson@Sun.COM 575094Slling /* 585094Slling * ==================================================================== 595094Slling * zpool property functions 605094Slling * ==================================================================== 615094Slling */ 625094Slling 635094Slling static int 645094Slling zpool_get_all_props(zpool_handle_t *zhp) 655094Slling { 665094Slling zfs_cmd_t zc = { 0 }; 675094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 685094Slling 695094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 705094Slling 715094Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 725094Slling return (-1); 735094Slling 745094Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 755094Slling if (errno == ENOMEM) { 765094Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 775094Slling zcmd_free_nvlists(&zc); 785094Slling return (-1); 795094Slling } 805094Slling } else { 815094Slling zcmd_free_nvlists(&zc); 825094Slling return (-1); 835094Slling } 845094Slling } 855094Slling 865094Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 875094Slling zcmd_free_nvlists(&zc); 885094Slling return (-1); 895094Slling } 905094Slling 915094Slling zcmd_free_nvlists(&zc); 925094Slling 935094Slling return (0); 945094Slling } 955094Slling 965094Slling static int 975094Slling zpool_props_refresh(zpool_handle_t *zhp) 985094Slling { 995094Slling nvlist_t *old_props; 1005094Slling 1015094Slling old_props = zhp->zpool_props; 1025094Slling 1035094Slling if (zpool_get_all_props(zhp) != 0) 1045094Slling return (-1); 1055094Slling 1065094Slling nvlist_free(old_props); 1075094Slling return (0); 1085094Slling } 1095094Slling 1105094Slling static char * 1115094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 1125094Slling zprop_source_t *src) 1135094Slling { 1145094Slling nvlist_t *nv, *nvl; 1155094Slling uint64_t ival; 1165094Slling char *value; 1175094Slling zprop_source_t source; 1185094Slling 1195094Slling nvl = zhp->zpool_props; 1205094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1215094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 1225094Slling source = ival; 1235094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1245094Slling } else { 1255094Slling source = ZPROP_SRC_DEFAULT; 1265094Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 1275094Slling value = "-"; 1285094Slling } 1295094Slling 1305094Slling if (src) 1315094Slling *src = source; 1325094Slling 1335094Slling return (value); 1345094Slling } 1355094Slling 1365094Slling uint64_t 1375094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 1385094Slling { 1395094Slling nvlist_t *nv, *nvl; 1405094Slling uint64_t value; 1415094Slling zprop_source_t source; 1425094Slling 1437294Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 1447294Sperrin /* 1457294Sperrin * zpool_get_all_props() has most likely failed because 1467294Sperrin * the pool is faulted, but if all we need is the top level 1477294Sperrin * vdev's guid then get it from the zhp config nvlist. 1487294Sperrin */ 1497294Sperrin if ((prop == ZPOOL_PROP_GUID) && 1507294Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 1517294Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 1527294Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 1537294Sperrin == 0)) { 1547294Sperrin return (value); 1557294Sperrin } 1565094Slling return (zpool_prop_default_numeric(prop)); 1577294Sperrin } 1585094Slling 1595094Slling nvl = zhp->zpool_props; 1605094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1615094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 1625094Slling source = value; 1635094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1645094Slling } else { 1655094Slling source = ZPROP_SRC_DEFAULT; 1665094Slling value = zpool_prop_default_numeric(prop); 1675094Slling } 1685094Slling 1695094Slling if (src) 1705094Slling *src = source; 1715094Slling 1725094Slling return (value); 1735094Slling } 1745094Slling 1755094Slling /* 1765094Slling * Map VDEV STATE to printed strings. 1775094Slling */ 1785094Slling char * 1795094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 1805094Slling { 1815094Slling switch (state) { 1825094Slling case VDEV_STATE_CLOSED: 1835094Slling case VDEV_STATE_OFFLINE: 1845094Slling return (gettext("OFFLINE")); 1855094Slling case VDEV_STATE_REMOVED: 1865094Slling return (gettext("REMOVED")); 1875094Slling case VDEV_STATE_CANT_OPEN: 1887294Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 1895094Slling return (gettext("FAULTED")); 19011422SMark.Musante@Sun.COM else if (aux == VDEV_AUX_SPLIT_POOL) 19111422SMark.Musante@Sun.COM return (gettext("SPLIT")); 1925094Slling else 1935094Slling return (gettext("UNAVAIL")); 1945094Slling case VDEV_STATE_FAULTED: 1955094Slling return (gettext("FAULTED")); 1965094Slling case VDEV_STATE_DEGRADED: 1975094Slling return (gettext("DEGRADED")); 1985094Slling case VDEV_STATE_HEALTHY: 1995094Slling return (gettext("ONLINE")); 2005094Slling } 2015094Slling 2025094Slling return (gettext("UNKNOWN")); 2035094Slling } 2045094Slling 2055094Slling /* 2065094Slling * Get a zpool property value for 'prop' and return the value in 2075094Slling * a pre-allocated buffer. 2085094Slling */ 2095094Slling int 2105094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 2115094Slling zprop_source_t *srctype) 2125094Slling { 2135094Slling uint64_t intval; 2145094Slling const char *strval; 2155094Slling zprop_source_t src = ZPROP_SRC_NONE; 2165094Slling nvlist_t *nvroot; 2175094Slling vdev_stat_t *vs; 2185094Slling uint_t vsc; 2195094Slling 2205094Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 2218525SEric.Schrock@Sun.COM switch (prop) { 2228525SEric.Schrock@Sun.COM case ZPOOL_PROP_NAME: 2235094Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 2248525SEric.Schrock@Sun.COM break; 2258525SEric.Schrock@Sun.COM 2268525SEric.Schrock@Sun.COM case ZPOOL_PROP_HEALTH: 2275094Slling (void) strlcpy(buf, "FAULTED", len); 2288525SEric.Schrock@Sun.COM break; 2298525SEric.Schrock@Sun.COM 2308525SEric.Schrock@Sun.COM case ZPOOL_PROP_GUID: 2318525SEric.Schrock@Sun.COM intval = zpool_get_prop_int(zhp, prop, &src); 2328525SEric.Schrock@Sun.COM (void) snprintf(buf, len, "%llu", intval); 2338525SEric.Schrock@Sun.COM break; 2348525SEric.Schrock@Sun.COM 2358525SEric.Schrock@Sun.COM case ZPOOL_PROP_ALTROOT: 2368525SEric.Schrock@Sun.COM case ZPOOL_PROP_CACHEFILE: 2378525SEric.Schrock@Sun.COM if (zhp->zpool_props != NULL || 2388525SEric.Schrock@Sun.COM zpool_get_all_props(zhp) == 0) { 2398525SEric.Schrock@Sun.COM (void) strlcpy(buf, 2408525SEric.Schrock@Sun.COM zpool_get_prop_string(zhp, prop, &src), 2418525SEric.Schrock@Sun.COM len); 2428525SEric.Schrock@Sun.COM if (srctype != NULL) 2438525SEric.Schrock@Sun.COM *srctype = src; 2448525SEric.Schrock@Sun.COM return (0); 2458525SEric.Schrock@Sun.COM } 2468525SEric.Schrock@Sun.COM /* FALLTHROUGH */ 2478525SEric.Schrock@Sun.COM default: 2485094Slling (void) strlcpy(buf, "-", len); 2498525SEric.Schrock@Sun.COM break; 2508525SEric.Schrock@Sun.COM } 2518525SEric.Schrock@Sun.COM 2528525SEric.Schrock@Sun.COM if (srctype != NULL) 2538525SEric.Schrock@Sun.COM *srctype = src; 2545094Slling return (0); 2555094Slling } 2565094Slling 2575094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 2585094Slling prop != ZPOOL_PROP_NAME) 2595094Slling return (-1); 2605094Slling 2615094Slling switch (zpool_prop_get_type(prop)) { 2625094Slling case PROP_TYPE_STRING: 2635094Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 2645094Slling len); 2655094Slling break; 2665094Slling 2675094Slling case PROP_TYPE_NUMBER: 2685094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2695094Slling 2705094Slling switch (prop) { 2715094Slling case ZPOOL_PROP_SIZE: 27210956SGeorge.Wilson@Sun.COM case ZPOOL_PROP_ALLOCATED: 27310956SGeorge.Wilson@Sun.COM case ZPOOL_PROP_FREE: 2745094Slling (void) zfs_nicenum(intval, buf, len); 2755094Slling break; 2765094Slling 2775094Slling case ZPOOL_PROP_CAPACITY: 2785094Slling (void) snprintf(buf, len, "%llu%%", 2795094Slling (u_longlong_t)intval); 2805094Slling break; 2815094Slling 28210922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPRATIO: 28310922SJeff.Bonwick@Sun.COM (void) snprintf(buf, len, "%llu.%02llux", 28410922SJeff.Bonwick@Sun.COM (u_longlong_t)(intval / 100), 28510922SJeff.Bonwick@Sun.COM (u_longlong_t)(intval % 100)); 28610922SJeff.Bonwick@Sun.COM break; 28710922SJeff.Bonwick@Sun.COM 2885094Slling case ZPOOL_PROP_HEALTH: 2895094Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2905094Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2915094Slling verify(nvlist_lookup_uint64_array(nvroot, 292*12296SLin.Ling@Sun.COM ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 293*12296SLin.Ling@Sun.COM == 0); 2945094Slling 2955094Slling (void) strlcpy(buf, zpool_state_to_name(intval, 2965094Slling vs->vs_aux), len); 2975094Slling break; 2985094Slling default: 2995094Slling (void) snprintf(buf, len, "%llu", intval); 3005094Slling } 3015094Slling break; 3025094Slling 3035094Slling case PROP_TYPE_INDEX: 3045094Slling intval = zpool_get_prop_int(zhp, prop, &src); 3055094Slling if (zpool_prop_index_to_string(prop, intval, &strval) 3065094Slling != 0) 3075094Slling return (-1); 3085094Slling (void) strlcpy(buf, strval, len); 3095094Slling break; 3105094Slling 3115094Slling default: 3125094Slling abort(); 3135094Slling } 3145094Slling 3155094Slling if (srctype) 3165094Slling *srctype = src; 3175094Slling 3185094Slling return (0); 3195094Slling } 3205094Slling 3215094Slling /* 3225094Slling * Check if the bootfs name has the same pool name as it is set to. 3235094Slling * Assuming bootfs is a valid dataset name. 3245094Slling */ 3255094Slling static boolean_t 3265094Slling bootfs_name_valid(const char *pool, char *bootfs) 3275094Slling { 3285094Slling int len = strlen(pool); 3295094Slling 3307300SEric.Taylor@Sun.COM if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 3315094Slling return (B_FALSE); 3325094Slling 3335094Slling if (strncmp(pool, bootfs, len) == 0 && 3345094Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 3355094Slling return (B_TRUE); 3365094Slling 3375094Slling return (B_FALSE); 3385094Slling } 3395094Slling 3405094Slling /* 3417042Sgw25295 * Inspect the configuration to determine if any of the devices contain 3427042Sgw25295 * an EFI label. 3437042Sgw25295 */ 3447042Sgw25295 static boolean_t 3457042Sgw25295 pool_uses_efi(nvlist_t *config) 3467042Sgw25295 { 3477042Sgw25295 nvlist_t **child; 3487042Sgw25295 uint_t c, children; 3497042Sgw25295 3507042Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3517042Sgw25295 &child, &children) != 0) 3527042Sgw25295 return (read_efi_label(config, NULL) >= 0); 3537042Sgw25295 3547042Sgw25295 for (c = 0; c < children; c++) { 3557042Sgw25295 if (pool_uses_efi(child[c])) 3567042Sgw25295 return (B_TRUE); 3577042Sgw25295 } 3587042Sgw25295 return (B_FALSE); 3597042Sgw25295 } 3607042Sgw25295 3617965SGeorge.Wilson@Sun.COM static boolean_t 3627965SGeorge.Wilson@Sun.COM pool_is_bootable(zpool_handle_t *zhp) 3637965SGeorge.Wilson@Sun.COM { 3647965SGeorge.Wilson@Sun.COM char bootfs[ZPOOL_MAXNAMELEN]; 3657965SGeorge.Wilson@Sun.COM 3667965SGeorge.Wilson@Sun.COM return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 3677965SGeorge.Wilson@Sun.COM sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 3687965SGeorge.Wilson@Sun.COM sizeof (bootfs)) != 0); 3697965SGeorge.Wilson@Sun.COM } 3707965SGeorge.Wilson@Sun.COM 3717965SGeorge.Wilson@Sun.COM 3727042Sgw25295 /* 3735094Slling * Given an nvlist of zpool properties to be set, validate that they are 3745094Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 3755094Slling * specified as strings. 3765094Slling */ 3775094Slling static nvlist_t * 3787184Stimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 3795094Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 3805094Slling { 3815094Slling nvpair_t *elem; 3825094Slling nvlist_t *retprops; 3835094Slling zpool_prop_t prop; 3845094Slling char *strval; 3855094Slling uint64_t intval; 3865363Seschrock char *slash; 3875363Seschrock struct stat64 statbuf; 3887042Sgw25295 zpool_handle_t *zhp; 3897042Sgw25295 nvlist_t *nvroot; 3905094Slling 3915094Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 3925094Slling (void) no_memory(hdl); 3935094Slling return (NULL); 3945094Slling } 3955094Slling 3965094Slling elem = NULL; 3975094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3985094Slling const char *propname = nvpair_name(elem); 3995094Slling 4005094Slling /* 4015094Slling * Make sure this property is valid and applies to this type. 4025094Slling */ 4035094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 4045094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4055094Slling "invalid property '%s'"), propname); 4065094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4075094Slling goto error; 4085094Slling } 4095094Slling 4105094Slling if (zpool_prop_readonly(prop)) { 4115094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4125094Slling "is readonly"), propname); 4135094Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 4145094Slling goto error; 4155094Slling } 4165094Slling 4175094Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 4185094Slling &strval, &intval, errbuf) != 0) 4195094Slling goto error; 4205094Slling 4215094Slling /* 4225094Slling * Perform additional checking for specific properties. 4235094Slling */ 4245094Slling switch (prop) { 4255094Slling case ZPOOL_PROP_VERSION: 4265094Slling if (intval < version || intval > SPA_VERSION) { 4275094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4285094Slling "property '%s' number %d is invalid."), 4295094Slling propname, intval); 4305094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4315094Slling goto error; 4325094Slling } 4335094Slling break; 4345094Slling 4355094Slling case ZPOOL_PROP_BOOTFS: 4365094Slling if (create_or_import) { 4375094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4385094Slling "property '%s' cannot be set at creation " 4395094Slling "or import time"), propname); 4405094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4415094Slling goto error; 4425094Slling } 4435094Slling 4445094Slling if (version < SPA_VERSION_BOOTFS) { 4455094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4465094Slling "pool must be upgraded to support " 4475094Slling "'%s' property"), propname); 4485094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 4495094Slling goto error; 4505094Slling } 4515094Slling 4525094Slling /* 4535094Slling * bootfs property value has to be a dataset name and 4545094Slling * the dataset has to be in the same pool as it sets to. 4555094Slling */ 4565094Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 4575094Slling strval)) { 4585094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 4595094Slling "is an invalid name"), strval); 4605094Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4615094Slling goto error; 4625094Slling } 4637042Sgw25295 4647042Sgw25295 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 4657042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4667042Sgw25295 "could not open pool '%s'"), poolname); 4677042Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4687042Sgw25295 goto error; 4697042Sgw25295 } 4707042Sgw25295 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 4717042Sgw25295 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4727042Sgw25295 4737042Sgw25295 /* 4747042Sgw25295 * bootfs property cannot be set on a disk which has 4757042Sgw25295 * been EFI labeled. 4767042Sgw25295 */ 4777042Sgw25295 if (pool_uses_efi(nvroot)) { 4787042Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4797042Sgw25295 "property '%s' not supported on " 4807042Sgw25295 "EFI labeled devices"), propname); 4817042Sgw25295 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 4827042Sgw25295 zpool_close(zhp); 4837042Sgw25295 goto error; 4847042Sgw25295 } 4857042Sgw25295 zpool_close(zhp); 4865094Slling break; 4875094Slling 4885094Slling case ZPOOL_PROP_ALTROOT: 4895094Slling if (!create_or_import) { 4905094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4915094Slling "property '%s' can only be set during pool " 4925094Slling "creation or import"), propname); 4935094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 4945094Slling goto error; 4955094Slling } 4965094Slling 4975094Slling if (strval[0] != '/') { 4985094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4995094Slling "bad alternate root '%s'"), strval); 5005094Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5015094Slling goto error; 5025094Slling } 5035094Slling break; 5045363Seschrock 5055363Seschrock case ZPOOL_PROP_CACHEFILE: 5065363Seschrock if (strval[0] == '\0') 5075363Seschrock break; 5085363Seschrock 5095363Seschrock if (strcmp(strval, "none") == 0) 5105363Seschrock break; 5115363Seschrock 5125363Seschrock if (strval[0] != '/') { 5135363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5145363Seschrock "property '%s' must be empty, an " 5155363Seschrock "absolute path, or 'none'"), propname); 5165363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5175363Seschrock goto error; 5185363Seschrock } 5195363Seschrock 5205363Seschrock slash = strrchr(strval, '/'); 5215363Seschrock 5225363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5235363Seschrock strcmp(slash, "/..") == 0) { 5245363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5255363Seschrock "'%s' is not a valid file"), strval); 5265363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5275363Seschrock goto error; 5285363Seschrock } 5295363Seschrock 5305363Seschrock *slash = '\0'; 5315363Seschrock 5325621Seschrock if (strval[0] != '\0' && 5335621Seschrock (stat64(strval, &statbuf) != 0 || 5345621Seschrock !S_ISDIR(statbuf.st_mode))) { 5355363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5365363Seschrock "'%s' is not a valid directory"), 5375363Seschrock strval); 5385363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5395363Seschrock goto error; 5405363Seschrock } 5415363Seschrock 5425363Seschrock *slash = '/'; 5435363Seschrock break; 5445094Slling } 5455094Slling } 5465094Slling 5475094Slling return (retprops); 5485094Slling error: 5495094Slling nvlist_free(retprops); 5505094Slling return (NULL); 5515094Slling } 5525094Slling 5535094Slling /* 5545094Slling * Set zpool property : propname=propval. 5555094Slling */ 5565094Slling int 5575094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 5585094Slling { 5595094Slling zfs_cmd_t zc = { 0 }; 5605094Slling int ret = -1; 5615094Slling char errbuf[1024]; 5625094Slling nvlist_t *nvl = NULL; 5635094Slling nvlist_t *realprops; 5645094Slling uint64_t version; 5655094Slling 5665094Slling (void) snprintf(errbuf, sizeof (errbuf), 5675094Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 5685094Slling zhp->zpool_name); 5695094Slling 5705094Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5715094Slling return (no_memory(zhp->zpool_hdl)); 5725094Slling 5735094Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 5745094Slling nvlist_free(nvl); 5755094Slling return (no_memory(zhp->zpool_hdl)); 5765094Slling } 5775094Slling 5785094Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5797184Stimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 5805094Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 5815094Slling nvlist_free(nvl); 5825094Slling return (-1); 5835094Slling } 5845094Slling 5855094Slling nvlist_free(nvl); 5865094Slling nvl = realprops; 5875094Slling 5885094Slling /* 5895094Slling * Execute the corresponding ioctl() to set this property. 5905094Slling */ 5915094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 5925094Slling 5935094Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 5945094Slling nvlist_free(nvl); 5955094Slling return (-1); 5965094Slling } 5975094Slling 5985094Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 5995094Slling 6005094Slling zcmd_free_nvlists(&zc); 6015094Slling nvlist_free(nvl); 6025094Slling 6035094Slling if (ret) 6045094Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 6055094Slling else 6065094Slling (void) zpool_props_refresh(zhp); 6075094Slling 6085094Slling return (ret); 6095094Slling } 6105094Slling 6115094Slling int 6125094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 6135094Slling { 6145094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 6155094Slling zprop_list_t *entry; 6165094Slling char buf[ZFS_MAXPROPLEN]; 6175094Slling 6185094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 6195094Slling return (-1); 6205094Slling 6215094Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 6225094Slling 6235094Slling if (entry->pl_fixed) 6245094Slling continue; 6255094Slling 6265094Slling if (entry->pl_prop != ZPROP_INVAL && 6275094Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 6285094Slling NULL) == 0) { 6295094Slling if (strlen(buf) > entry->pl_width) 6305094Slling entry->pl_width = strlen(buf); 6315094Slling } 6325094Slling } 6335094Slling 6345094Slling return (0); 6355094Slling } 6365094Slling 6375094Slling 638789Sahrens /* 6399816SGeorge.Wilson@Sun.COM * Don't start the slice at the default block of 34; many storage 6409816SGeorge.Wilson@Sun.COM * devices will use a stripe width of 128k, so start there instead. 6419816SGeorge.Wilson@Sun.COM */ 6429816SGeorge.Wilson@Sun.COM #define NEW_START_BLOCK 256 6439816SGeorge.Wilson@Sun.COM 6449816SGeorge.Wilson@Sun.COM /* 645789Sahrens * Validate the given pool name, optionally putting an extended error message in 646789Sahrens * 'buf'. 647789Sahrens */ 6486423Sgw25295 boolean_t 6492082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 650789Sahrens { 651789Sahrens namecheck_err_t why; 652789Sahrens char what; 6531773Seschrock int ret; 654789Sahrens 6551773Seschrock ret = pool_namecheck(pool, &why, &what); 6561773Seschrock 6571773Seschrock /* 6581773Seschrock * The rules for reserved pool names were extended at a later point. 6591773Seschrock * But we need to support users with existing pools that may now be 6601773Seschrock * invalid. So we only check for this expanded set of names during a 6611773Seschrock * create (or import), and only in userland. 6621773Seschrock */ 6631773Seschrock if (ret == 0 && !isopen && 6641773Seschrock (strncmp(pool, "mirror", 6) == 0 || 6651773Seschrock strncmp(pool, "raidz", 5) == 0 || 6664527Sperrin strncmp(pool, "spare", 5) == 0 || 6674527Sperrin strcmp(pool, "log") == 0)) { 6686423Sgw25295 if (hdl != NULL) 6696423Sgw25295 zfs_error_aux(hdl, 6706423Sgw25295 dgettext(TEXT_DOMAIN, "name is reserved")); 6712082Seschrock return (B_FALSE); 6721773Seschrock } 6731773Seschrock 6741773Seschrock 6751773Seschrock if (ret != 0) { 6762082Seschrock if (hdl != NULL) { 677789Sahrens switch (why) { 6781003Slling case NAME_ERR_TOOLONG: 6792082Seschrock zfs_error_aux(hdl, 6801003Slling dgettext(TEXT_DOMAIN, "name is too long")); 6811003Slling break; 6821003Slling 683789Sahrens case NAME_ERR_INVALCHAR: 6842082Seschrock zfs_error_aux(hdl, 685789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 686789Sahrens "'%c' in pool name"), what); 687789Sahrens break; 688789Sahrens 689789Sahrens case NAME_ERR_NOLETTER: 6902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6912082Seschrock "name must begin with a letter")); 692789Sahrens break; 693789Sahrens 694789Sahrens case NAME_ERR_RESERVED: 6952082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6962082Seschrock "name is reserved")); 697789Sahrens break; 698789Sahrens 699789Sahrens case NAME_ERR_DISKLIKE: 7002082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7012082Seschrock "pool name is reserved")); 702789Sahrens break; 7032856Snd150628 7042856Snd150628 case NAME_ERR_LEADING_SLASH: 7052856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7062856Snd150628 "leading slash in name")); 7072856Snd150628 break; 7082856Snd150628 7092856Snd150628 case NAME_ERR_EMPTY_COMPONENT: 7102856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7112856Snd150628 "empty component in name")); 7122856Snd150628 break; 7132856Snd150628 7142856Snd150628 case NAME_ERR_TRAILING_SLASH: 7152856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7162856Snd150628 "trailing slash in name")); 7172856Snd150628 break; 7182856Snd150628 7192856Snd150628 case NAME_ERR_MULTIPLE_AT: 7202856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7212856Snd150628 "multiple '@' delimiters in name")); 7222856Snd150628 break; 7232856Snd150628 724789Sahrens } 725789Sahrens } 7262082Seschrock return (B_FALSE); 727789Sahrens } 728789Sahrens 7292082Seschrock return (B_TRUE); 730789Sahrens } 731789Sahrens 732789Sahrens /* 733789Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 734789Sahrens * state. 735789Sahrens */ 736789Sahrens zpool_handle_t * 7372082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 738789Sahrens { 739789Sahrens zpool_handle_t *zhp; 7402142Seschrock boolean_t missing; 741789Sahrens 742789Sahrens /* 743789Sahrens * Make sure the pool name is valid. 744789Sahrens */ 7452082Seschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 7463237Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 7472082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 7482082Seschrock pool); 749789Sahrens return (NULL); 750789Sahrens } 751789Sahrens 7522082Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7532082Seschrock return (NULL); 754789Sahrens 7552082Seschrock zhp->zpool_hdl = hdl; 756789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 757789Sahrens 7582142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7592142Seschrock zpool_close(zhp); 7602142Seschrock return (NULL); 7612142Seschrock } 7622142Seschrock 7632142Seschrock if (missing) { 7645094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 7653237Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 7665094Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 7672142Seschrock zpool_close(zhp); 7682142Seschrock return (NULL); 769789Sahrens } 770789Sahrens 771789Sahrens return (zhp); 772789Sahrens } 773789Sahrens 774789Sahrens /* 775789Sahrens * Like the above, but silent on error. Used when iterating over pools (because 776789Sahrens * the configuration cache may be out of date). 777789Sahrens */ 7782142Seschrock int 7792142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 780789Sahrens { 781789Sahrens zpool_handle_t *zhp; 7822142Seschrock boolean_t missing; 783789Sahrens 7842142Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 7852142Seschrock return (-1); 786789Sahrens 7872082Seschrock zhp->zpool_hdl = hdl; 788789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 789789Sahrens 7902142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 7912142Seschrock zpool_close(zhp); 7922142Seschrock return (-1); 793789Sahrens } 794789Sahrens 7952142Seschrock if (missing) { 7962142Seschrock zpool_close(zhp); 7972142Seschrock *ret = NULL; 7982142Seschrock return (0); 7992142Seschrock } 8002142Seschrock 8012142Seschrock *ret = zhp; 8022142Seschrock return (0); 803789Sahrens } 804789Sahrens 805789Sahrens /* 806789Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 807789Sahrens * state. 808789Sahrens */ 809789Sahrens zpool_handle_t * 8102082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 811789Sahrens { 812789Sahrens zpool_handle_t *zhp; 813789Sahrens 8142082Seschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 815789Sahrens return (NULL); 816789Sahrens 817789Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 8183237Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 8192082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 820789Sahrens zpool_close(zhp); 821789Sahrens return (NULL); 822789Sahrens } 823789Sahrens 824789Sahrens return (zhp); 825789Sahrens } 826789Sahrens 827789Sahrens /* 828789Sahrens * Close the handle. Simply frees the memory associated with the handle. 829789Sahrens */ 830789Sahrens void 831789Sahrens zpool_close(zpool_handle_t *zhp) 832789Sahrens { 833789Sahrens if (zhp->zpool_config) 834789Sahrens nvlist_free(zhp->zpool_config); 835952Seschrock if (zhp->zpool_old_config) 836952Seschrock nvlist_free(zhp->zpool_old_config); 8373912Slling if (zhp->zpool_props) 8383912Slling nvlist_free(zhp->zpool_props); 839789Sahrens free(zhp); 840789Sahrens } 841789Sahrens 842789Sahrens /* 843789Sahrens * Return the name of the pool. 844789Sahrens */ 845789Sahrens const char * 846789Sahrens zpool_get_name(zpool_handle_t *zhp) 847789Sahrens { 848789Sahrens return (zhp->zpool_name); 849789Sahrens } 850789Sahrens 851789Sahrens 852789Sahrens /* 853789Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 854789Sahrens */ 855789Sahrens int 856789Sahrens zpool_get_state(zpool_handle_t *zhp) 857789Sahrens { 858789Sahrens return (zhp->zpool_state); 859789Sahrens } 860789Sahrens 861789Sahrens /* 862789Sahrens * Create the named pool, using the provided vdev list. It is assumed 863789Sahrens * that the consumer has already validated the contents of the nvlist, so we 864789Sahrens * don't have to worry about error semantics. 865789Sahrens */ 866789Sahrens int 8672082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8687184Stimh nvlist_t *props, nvlist_t *fsprops) 869789Sahrens { 870789Sahrens zfs_cmd_t zc = { 0 }; 8717184Stimh nvlist_t *zc_fsprops = NULL; 8727184Stimh nvlist_t *zc_props = NULL; 8732082Seschrock char msg[1024]; 8745094Slling char *altroot; 8757184Stimh int ret = -1; 8762082Seschrock 8772082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 8782082Seschrock "cannot create '%s'"), pool); 879789Sahrens 8802082Seschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 8812082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 8822082Seschrock 8835320Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 8845320Slling return (-1); 8855320Slling 8867184Stimh if (props) { 8877184Stimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8887184Stimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8897184Stimh goto create_failed; 8907184Stimh } 8915320Slling } 892789Sahrens 8937184Stimh if (fsprops) { 8947184Stimh uint64_t zoned; 8957184Stimh char *zonestr; 8967184Stimh 8977184Stimh zoned = ((nvlist_lookup_string(fsprops, 8987184Stimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8997184Stimh strcmp(zonestr, "on") == 0); 9007184Stimh 9017184Stimh if ((zc_fsprops = zfs_valid_proplist(hdl, 9027184Stimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 9037184Stimh goto create_failed; 9047184Stimh } 9057184Stimh if (!zc_props && 9067184Stimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 9077184Stimh goto create_failed; 9087184Stimh } 9097184Stimh if (nvlist_add_nvlist(zc_props, 9107184Stimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 9117184Stimh goto create_failed; 9127184Stimh } 9137184Stimh } 9147184Stimh 9157184Stimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 9167184Stimh goto create_failed; 9177184Stimh 918789Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 919789Sahrens 9207184Stimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 9215320Slling 9222676Seschrock zcmd_free_nvlists(&zc); 9237184Stimh nvlist_free(zc_props); 9247184Stimh nvlist_free(zc_fsprops); 9252082Seschrock 926789Sahrens switch (errno) { 927789Sahrens case EBUSY: 928789Sahrens /* 929789Sahrens * This can happen if the user has specified the same 930789Sahrens * device multiple times. We can't reliably detect this 931789Sahrens * until we try to add it and see we already have a 932789Sahrens * label. 933789Sahrens */ 9342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9352082Seschrock "one or more vdevs refer to the same device")); 9362082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 937789Sahrens 938789Sahrens case EOVERFLOW: 939789Sahrens /* 9402082Seschrock * This occurs when one of the devices is below 941789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 942789Sahrens * device was the problem device since there's no 943789Sahrens * reliable way to determine device size from userland. 944789Sahrens */ 945789Sahrens { 946789Sahrens char buf[64]; 947789Sahrens 948789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 949789Sahrens 9502082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9512082Seschrock "one or more devices is less than the " 9522082Seschrock "minimum size (%s)"), buf); 953789Sahrens } 9542082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 955789Sahrens 956789Sahrens case ENOSPC: 9572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9582082Seschrock "one or more devices is out of space")); 9592082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 960789Sahrens 9615450Sbrendan case ENOTBLK: 9625450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9635450Sbrendan "cache device must be a disk or disk slice")); 9645450Sbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 9655450Sbrendan 966789Sahrens default: 9672082Seschrock return (zpool_standard_error(hdl, errno, msg)); 968789Sahrens } 969789Sahrens } 970789Sahrens 971789Sahrens /* 972789Sahrens * If this is an alternate root pool, then we automatically set the 9732676Seschrock * mountpoint of the root dataset to be '/'. 974789Sahrens */ 9755094Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 9765094Slling &altroot) == 0) { 977789Sahrens zfs_handle_t *zhp; 978789Sahrens 9795094Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 9802676Seschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 9812676Seschrock "/") == 0); 982789Sahrens 983789Sahrens zfs_close(zhp); 984789Sahrens } 985789Sahrens 9867184Stimh create_failed: 9875320Slling zcmd_free_nvlists(&zc); 9887184Stimh nvlist_free(zc_props); 9897184Stimh nvlist_free(zc_fsprops); 9907184Stimh return (ret); 991789Sahrens } 992789Sahrens 993789Sahrens /* 994789Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 995789Sahrens * datasets left in the pool. 996789Sahrens */ 997789Sahrens int 998789Sahrens zpool_destroy(zpool_handle_t *zhp) 999789Sahrens { 1000789Sahrens zfs_cmd_t zc = { 0 }; 1001789Sahrens zfs_handle_t *zfp = NULL; 10022082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10032082Seschrock char msg[1024]; 1004789Sahrens 1005789Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 10062082Seschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 10072082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 1008789Sahrens return (-1); 1009789Sahrens 1010789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1011789Sahrens 10124543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 10132082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10142082Seschrock "cannot destroy '%s'"), zhp->zpool_name); 1015789Sahrens 10162082Seschrock if (errno == EROFS) { 10172082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10182082Seschrock "one or more devices is read only")); 10192082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 10202082Seschrock } else { 10212082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1022789Sahrens } 1023789Sahrens 1024789Sahrens if (zfp) 1025789Sahrens zfs_close(zfp); 1026789Sahrens return (-1); 1027789Sahrens } 1028789Sahrens 1029789Sahrens if (zfp) { 1030789Sahrens remove_mountpoint(zfp); 1031789Sahrens zfs_close(zfp); 1032789Sahrens } 1033789Sahrens 1034789Sahrens return (0); 1035789Sahrens } 1036789Sahrens 1037789Sahrens /* 1038789Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1039789Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1040789Sahrens */ 1041789Sahrens int 1042789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1043789Sahrens { 10442676Seschrock zfs_cmd_t zc = { 0 }; 10452082Seschrock int ret; 10462082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 10472082Seschrock char msg[1024]; 10485450Sbrendan nvlist_t **spares, **l2cache; 10495450Sbrendan uint_t nspares, nl2cache; 10502082Seschrock 10512082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 10522082Seschrock "cannot add to '%s'"), zhp->zpool_name); 10532082Seschrock 10545450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10555450Sbrendan SPA_VERSION_SPARES && 10562082Seschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 10572082Seschrock &spares, &nspares) == 0) { 10582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10592082Seschrock "upgraded to add hot spares")); 10602082Seschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10612082Seschrock } 1062789Sahrens 10637965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 10647965SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 10657965SGeorge.Wilson@Sun.COM uint64_t s; 10667965SGeorge.Wilson@Sun.COM 10677965SGeorge.Wilson@Sun.COM for (s = 0; s < nspares; s++) { 10687965SGeorge.Wilson@Sun.COM char *path; 10697965SGeorge.Wilson@Sun.COM 10707965SGeorge.Wilson@Sun.COM if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 10717965SGeorge.Wilson@Sun.COM &path) == 0 && pool_uses_efi(spares[s])) { 10727965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10737965SGeorge.Wilson@Sun.COM "device '%s' contains an EFI label and " 10747965SGeorge.Wilson@Sun.COM "cannot be used on root pools."), 107510594SGeorge.Wilson@Sun.COM zpool_vdev_name(hdl, NULL, spares[s], 107610594SGeorge.Wilson@Sun.COM B_FALSE)); 10777965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 10787965SGeorge.Wilson@Sun.COM } 10797965SGeorge.Wilson@Sun.COM } 10807965SGeorge.Wilson@Sun.COM } 10817965SGeorge.Wilson@Sun.COM 10825450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 10835450Sbrendan SPA_VERSION_L2CACHE && 10845450Sbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 10855450Sbrendan &l2cache, &nl2cache) == 0) { 10865450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 10875450Sbrendan "upgraded to add cache devices")); 10885450Sbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 10895450Sbrendan } 10905450Sbrendan 10915094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 10922082Seschrock return (-1); 1093789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1094789Sahrens 10954543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1096789Sahrens switch (errno) { 1097789Sahrens case EBUSY: 1098789Sahrens /* 1099789Sahrens * This can happen if the user has specified the same 1100789Sahrens * device multiple times. We can't reliably detect this 1101789Sahrens * until we try to add it and see we already have a 1102789Sahrens * label. 1103789Sahrens */ 11042082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11052082Seschrock "one or more vdevs refer to the same device")); 11062082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1107789Sahrens break; 1108789Sahrens 1109789Sahrens case EOVERFLOW: 1110789Sahrens /* 1111789Sahrens * This occurrs when one of the devices is below 1112789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1113789Sahrens * device was the problem device since there's no 1114789Sahrens * reliable way to determine device size from userland. 1115789Sahrens */ 1116789Sahrens { 1117789Sahrens char buf[64]; 1118789Sahrens 1119789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1120789Sahrens 11212082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11222082Seschrock "device is less than the minimum " 11232082Seschrock "size (%s)"), buf); 1124789Sahrens } 11252082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 11262082Seschrock break; 11272082Seschrock 11282082Seschrock case ENOTSUP: 11292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11304527Sperrin "pool must be upgraded to add these vdevs")); 11312082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1132789Sahrens break; 1133789Sahrens 11343912Slling case EDOM: 11353912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11364527Sperrin "root pool can not have multiple vdevs" 11374527Sperrin " or separate logs")); 11383912Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 11393912Slling break; 11403912Slling 11415450Sbrendan case ENOTBLK: 11425450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11435450Sbrendan "cache device must be a disk or disk slice")); 11445450Sbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 11455450Sbrendan break; 11465450Sbrendan 1147789Sahrens default: 11482082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1149789Sahrens } 1150789Sahrens 11512082Seschrock ret = -1; 11522082Seschrock } else { 11532082Seschrock ret = 0; 1154789Sahrens } 1155789Sahrens 11562676Seschrock zcmd_free_nvlists(&zc); 1157789Sahrens 11582082Seschrock return (ret); 1159789Sahrens } 1160789Sahrens 1161789Sahrens /* 1162789Sahrens * Exports the pool from the system. The caller must ensure that there are no 1163789Sahrens * mounted datasets in the pool. 1164789Sahrens */ 1165789Sahrens int 11668211SGeorge.Wilson@Sun.COM zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1167789Sahrens { 1168789Sahrens zfs_cmd_t zc = { 0 }; 11697214Slling char msg[1024]; 1170789Sahrens 11717214Slling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 11727214Slling "cannot export '%s'"), zhp->zpool_name); 11737214Slling 1174789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 11757214Slling zc.zc_cookie = force; 11768211SGeorge.Wilson@Sun.COM zc.zc_guid = hardforce; 11777214Slling 11787214Slling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 11797214Slling switch (errno) { 11807214Slling case EXDEV: 11817214Slling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 11827214Slling "use '-f' to override the following errors:\n" 11837214Slling "'%s' has an active shared spare which could be" 11847214Slling " used by other pools once '%s' is exported."), 11857214Slling zhp->zpool_name, zhp->zpool_name); 11867214Slling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 11877214Slling msg)); 11887214Slling default: 11897214Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 11907214Slling msg)); 11917214Slling } 11927214Slling } 11937214Slling 1194789Sahrens return (0); 1195789Sahrens } 1196789Sahrens 11978211SGeorge.Wilson@Sun.COM int 11988211SGeorge.Wilson@Sun.COM zpool_export(zpool_handle_t *zhp, boolean_t force) 11998211SGeorge.Wilson@Sun.COM { 12008211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, force, B_FALSE)); 12018211SGeorge.Wilson@Sun.COM } 12028211SGeorge.Wilson@Sun.COM 12038211SGeorge.Wilson@Sun.COM int 12048211SGeorge.Wilson@Sun.COM zpool_export_force(zpool_handle_t *zhp) 12058211SGeorge.Wilson@Sun.COM { 12068211SGeorge.Wilson@Sun.COM return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 12078211SGeorge.Wilson@Sun.COM } 12088211SGeorge.Wilson@Sun.COM 120910921STim.Haley@Sun.COM static void 121010921STim.Haley@Sun.COM zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 121110921STim.Haley@Sun.COM nvlist_t *rbi) 121210921STim.Haley@Sun.COM { 121310921STim.Haley@Sun.COM uint64_t rewindto; 121410921STim.Haley@Sun.COM int64_t loss = -1; 121510921STim.Haley@Sun.COM struct tm t; 121610921STim.Haley@Sun.COM char timestr[128]; 121710921STim.Haley@Sun.COM 121810921STim.Haley@Sun.COM if (!hdl->libzfs_printerr || rbi == NULL) 121910921STim.Haley@Sun.COM return; 122010921STim.Haley@Sun.COM 122110921STim.Haley@Sun.COM if (nvlist_lookup_uint64(rbi, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 122210921STim.Haley@Sun.COM return; 122310921STim.Haley@Sun.COM (void) nvlist_lookup_int64(rbi, ZPOOL_CONFIG_REWIND_TIME, &loss); 122410921STim.Haley@Sun.COM 122510921STim.Haley@Sun.COM if (localtime_r((time_t *)&rewindto, &t) != NULL && 122610921STim.Haley@Sun.COM strftime(timestr, 128, 0, &t) != 0) { 122710921STim.Haley@Sun.COM if (dryrun) { 122810921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 122910921STim.Haley@Sun.COM "Would be able to return %s " 123010921STim.Haley@Sun.COM "to its state as of %s.\n"), 123110921STim.Haley@Sun.COM name, timestr); 123210921STim.Haley@Sun.COM } else { 123310921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 123410921STim.Haley@Sun.COM "Pool %s returned to its state as of %s.\n"), 123510921STim.Haley@Sun.COM name, timestr); 123610921STim.Haley@Sun.COM } 123710921STim.Haley@Sun.COM if (loss > 120) { 123810921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 123910921STim.Haley@Sun.COM "%s approximately %lld "), 124010921STim.Haley@Sun.COM dryrun ? "Would discard" : "Discarded", 124110921STim.Haley@Sun.COM (loss + 30) / 60); 124210921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 124310921STim.Haley@Sun.COM "minutes of transactions.\n")); 124410921STim.Haley@Sun.COM } else if (loss > 0) { 124510921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 124610921STim.Haley@Sun.COM "%s approximately %lld "), 124710921STim.Haley@Sun.COM dryrun ? "Would discard" : "Discarded", loss); 124810921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 124910921STim.Haley@Sun.COM "seconds of transactions.\n")); 125010921STim.Haley@Sun.COM } 125110921STim.Haley@Sun.COM } 125210921STim.Haley@Sun.COM } 125310921STim.Haley@Sun.COM 125410921STim.Haley@Sun.COM void 125510921STim.Haley@Sun.COM zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 125610921STim.Haley@Sun.COM nvlist_t *config) 125710921STim.Haley@Sun.COM { 125810921STim.Haley@Sun.COM int64_t loss = -1; 125910921STim.Haley@Sun.COM uint64_t edata = UINT64_MAX; 126010921STim.Haley@Sun.COM uint64_t rewindto; 126110921STim.Haley@Sun.COM struct tm t; 126210921STim.Haley@Sun.COM char timestr[128]; 126310921STim.Haley@Sun.COM 126410921STim.Haley@Sun.COM if (!hdl->libzfs_printerr) 126510921STim.Haley@Sun.COM return; 126610921STim.Haley@Sun.COM 126710921STim.Haley@Sun.COM if (reason >= 0) 126810921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 126910921STim.Haley@Sun.COM else 127010921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, "\t")); 127110921STim.Haley@Sun.COM 127210921STim.Haley@Sun.COM /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 127310921STim.Haley@Sun.COM if (nvlist_lookup_uint64(config, 127410921STim.Haley@Sun.COM ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 127510921STim.Haley@Sun.COM goto no_info; 127610921STim.Haley@Sun.COM 127710921STim.Haley@Sun.COM (void) nvlist_lookup_int64(config, ZPOOL_CONFIG_REWIND_TIME, &loss); 127810921STim.Haley@Sun.COM (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 127910921STim.Haley@Sun.COM &edata); 128010921STim.Haley@Sun.COM 128110921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 128210921STim.Haley@Sun.COM "Recovery is possible, but will result in some data loss.\n")); 128310921STim.Haley@Sun.COM 128410921STim.Haley@Sun.COM if (localtime_r((time_t *)&rewindto, &t) != NULL && 128510921STim.Haley@Sun.COM strftime(timestr, 128, 0, &t) != 0) { 128610921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 128710921STim.Haley@Sun.COM "\tReturning the pool to its state as of %s\n" 128810921STim.Haley@Sun.COM "\tshould correct the problem. "), 128910921STim.Haley@Sun.COM timestr); 129010921STim.Haley@Sun.COM } else { 129110921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 129210921STim.Haley@Sun.COM "\tReverting the pool to an earlier state " 129310921STim.Haley@Sun.COM "should correct the problem.\n\t")); 129410921STim.Haley@Sun.COM } 129510921STim.Haley@Sun.COM 129610921STim.Haley@Sun.COM if (loss > 120) { 129710921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 129810921STim.Haley@Sun.COM "Approximately %lld minutes of data\n" 129910921STim.Haley@Sun.COM "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 130010921STim.Haley@Sun.COM } else if (loss > 0) { 130110921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 130210921STim.Haley@Sun.COM "Approximately %lld seconds of data\n" 130310921STim.Haley@Sun.COM "\tmust be discarded, irreversibly. "), loss); 130410921STim.Haley@Sun.COM } 130510921STim.Haley@Sun.COM if (edata != 0 && edata != UINT64_MAX) { 130610921STim.Haley@Sun.COM if (edata == 1) { 130710921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 130810921STim.Haley@Sun.COM "After rewind, at least\n" 130910921STim.Haley@Sun.COM "\tone persistent user-data error will remain. ")); 131010921STim.Haley@Sun.COM } else { 131110921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 131210921STim.Haley@Sun.COM "After rewind, several\n" 131310921STim.Haley@Sun.COM "\tpersistent user-data errors will remain. ")); 131410921STim.Haley@Sun.COM } 131510921STim.Haley@Sun.COM } 131610921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 131711026STim.Haley@Sun.COM "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 131811026STim.Haley@Sun.COM reason >= 0 ? "clear" : "import", name); 131910921STim.Haley@Sun.COM 132010921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 132110921STim.Haley@Sun.COM "A scrub of the pool\n" 132210921STim.Haley@Sun.COM "\tis strongly recommended after recovery.\n")); 132310921STim.Haley@Sun.COM return; 132410921STim.Haley@Sun.COM 132510921STim.Haley@Sun.COM no_info: 132610921STim.Haley@Sun.COM (void) printf(dgettext(TEXT_DOMAIN, 132710921STim.Haley@Sun.COM "Destroy and re-create the pool from\n\ta backup source.\n")); 132810921STim.Haley@Sun.COM } 132910921STim.Haley@Sun.COM 1330789Sahrens /* 13315094Slling * zpool_import() is a contracted interface. Should be kept the same 13325094Slling * if possible. 13335094Slling * 13345094Slling * Applications should use zpool_import_props() to import a pool with 13355094Slling * new properties value to be set. 1336789Sahrens */ 1337789Sahrens int 13382082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 13395094Slling char *altroot) 13405094Slling { 13415094Slling nvlist_t *props = NULL; 13425094Slling int ret; 13435094Slling 13445094Slling if (altroot != NULL) { 13455094Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 13465094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 13475094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 13485094Slling newname)); 13495094Slling } 13505094Slling 13515094Slling if (nvlist_add_string(props, 13528084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 13538084SGeorge.Wilson@Sun.COM nvlist_add_string(props, 13548084SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 13555094Slling nvlist_free(props); 13565094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 13575094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 13585094Slling newname)); 13595094Slling } 13605094Slling } 13615094Slling 13626643Seschrock ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 13635094Slling if (props) 13645094Slling nvlist_free(props); 13655094Slling return (ret); 13665094Slling } 13675094Slling 13685094Slling /* 13695094Slling * Import the given pool using the known configuration and a list of 13705094Slling * properties to be set. The configuration should have come from 13715094Slling * zpool_find_import(). The 'newname' parameters control whether the pool 13725094Slling * is imported with a different name. 13735094Slling */ 13745094Slling int 13755094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 13766643Seschrock nvlist_t *props, boolean_t importfaulted) 1377789Sahrens { 13782676Seschrock zfs_cmd_t zc = { 0 }; 137910921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 138010921STim.Haley@Sun.COM nvlist_t *nvi = NULL; 1381789Sahrens char *thename; 1382789Sahrens char *origname; 138310921STim.Haley@Sun.COM uint64_t returned_size; 1384789Sahrens int ret; 13855094Slling char errbuf[1024]; 1386789Sahrens 1387789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1388789Sahrens &origname) == 0); 1389789Sahrens 13905094Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 13915094Slling "cannot import pool '%s'"), origname); 13925094Slling 1393789Sahrens if (newname != NULL) { 13942082Seschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 13953237Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 13962082Seschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 13972082Seschrock newname)); 1398789Sahrens thename = (char *)newname; 1399789Sahrens } else { 1400789Sahrens thename = origname; 1401789Sahrens } 1402789Sahrens 14035094Slling if (props) { 14045094Slling uint64_t version; 14055094Slling 14065094Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 14075094Slling &version) == 0); 14085094Slling 14097184Stimh if ((props = zpool_valid_proplist(hdl, origname, 14105320Slling props, version, B_TRUE, errbuf)) == NULL) { 14115094Slling return (-1); 14125320Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 14135320Slling nvlist_free(props); 14145094Slling return (-1); 14155320Slling } 14165094Slling } 1417789Sahrens 1418789Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1419789Sahrens 1420789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 14211544Seschrock &zc.zc_guid) == 0); 1422789Sahrens 14235320Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 14245320Slling nvlist_free(props); 14252082Seschrock return (-1); 14265320Slling } 142710921STim.Haley@Sun.COM returned_size = zc.zc_nvlist_conf_size + 512; 142810921STim.Haley@Sun.COM if (zcmd_alloc_dst_nvlist(hdl, &zc, returned_size) != 0) { 142910921STim.Haley@Sun.COM nvlist_free(props); 143010921STim.Haley@Sun.COM return (-1); 143110921STim.Haley@Sun.COM } 1432789Sahrens 14336643Seschrock zc.zc_cookie = (uint64_t)importfaulted; 1434789Sahrens ret = 0; 14354543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1436789Sahrens char desc[1024]; 143710921STim.Haley@Sun.COM 143810921STim.Haley@Sun.COM (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 143910921STim.Haley@Sun.COM zpool_get_rewind_policy(config, &policy); 144010921STim.Haley@Sun.COM /* 144110921STim.Haley@Sun.COM * Dry-run failed, but we print out what success 144210921STim.Haley@Sun.COM * looks like if we found a best txg 144310921STim.Haley@Sun.COM */ 144410921STim.Haley@Sun.COM if ((policy.zrp_request & ZPOOL_TRY_REWIND) && nvi) { 144510921STim.Haley@Sun.COM zpool_rewind_exclaim(hdl, newname ? origname : thename, 144610921STim.Haley@Sun.COM B_TRUE, nvi); 144710921STim.Haley@Sun.COM nvlist_free(nvi); 144810921STim.Haley@Sun.COM return (-1); 144910921STim.Haley@Sun.COM } 145010921STim.Haley@Sun.COM 1451789Sahrens if (newname == NULL) 1452789Sahrens (void) snprintf(desc, sizeof (desc), 1453789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1454789Sahrens thename); 1455789Sahrens else 1456789Sahrens (void) snprintf(desc, sizeof (desc), 1457789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1458789Sahrens origname, thename); 1459789Sahrens 1460789Sahrens switch (errno) { 14611544Seschrock case ENOTSUP: 14621544Seschrock /* 14631544Seschrock * Unsupported version. 14641544Seschrock */ 14652082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 14661544Seschrock break; 14671544Seschrock 14682174Seschrock case EINVAL: 14692174Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 14702174Seschrock break; 14712174Seschrock 147211814SChris.Kirby@sun.com case EROFS: 147311814SChris.Kirby@sun.com zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 147411814SChris.Kirby@sun.com "one or more devices is read only")); 147511814SChris.Kirby@sun.com (void) zfs_error(hdl, EZFS_BADDEV, desc); 147611814SChris.Kirby@sun.com break; 147711814SChris.Kirby@sun.com 1478789Sahrens default: 147910921STim.Haley@Sun.COM (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 14802082Seschrock (void) zpool_standard_error(hdl, errno, desc); 148110921STim.Haley@Sun.COM zpool_explain_recover(hdl, 148210921STim.Haley@Sun.COM newname ? origname : thename, -errno, nvi); 148310921STim.Haley@Sun.COM nvlist_free(nvi); 148410921STim.Haley@Sun.COM break; 1485789Sahrens } 1486789Sahrens 1487789Sahrens ret = -1; 1488789Sahrens } else { 1489789Sahrens zpool_handle_t *zhp; 14904543Smarks 1491789Sahrens /* 1492789Sahrens * This should never fail, but play it safe anyway. 1493789Sahrens */ 149410588SEric.Taylor@Sun.COM if (zpool_open_silent(hdl, thename, &zhp) != 0) 14952142Seschrock ret = -1; 149610588SEric.Taylor@Sun.COM else if (zhp != NULL) 1497789Sahrens zpool_close(zhp); 149810921STim.Haley@Sun.COM (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 149910921STim.Haley@Sun.COM zpool_get_rewind_policy(config, &policy); 150010921STim.Haley@Sun.COM if (policy.zrp_request & 150110921STim.Haley@Sun.COM (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 150210921STim.Haley@Sun.COM zpool_rewind_exclaim(hdl, newname ? origname : thename, 150310921STim.Haley@Sun.COM ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 150410921STim.Haley@Sun.COM nvi); 150510921STim.Haley@Sun.COM } 150610921STim.Haley@Sun.COM nvlist_free(nvi); 150710921STim.Haley@Sun.COM return (0); 1508789Sahrens } 1509789Sahrens 15102676Seschrock zcmd_free_nvlists(&zc); 15115320Slling nvlist_free(props); 15125320Slling 1513789Sahrens return (ret); 1514789Sahrens } 1515789Sahrens 1516789Sahrens /* 1517*12296SLin.Ling@Sun.COM * Scan the pool. 1518789Sahrens */ 1519789Sahrens int 1520*12296SLin.Ling@Sun.COM zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1521789Sahrens { 1522789Sahrens zfs_cmd_t zc = { 0 }; 1523789Sahrens char msg[1024]; 15242082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1525789Sahrens 1526789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1527*12296SLin.Ling@Sun.COM zc.zc_cookie = func; 1528*12296SLin.Ling@Sun.COM 1529*12296SLin.Ling@Sun.COM if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 1530*12296SLin.Ling@Sun.COM (errno == ENOENT && func != POOL_SCAN_NONE)) 1531789Sahrens return (0); 1532789Sahrens 1533*12296SLin.Ling@Sun.COM if (func == POOL_SCAN_SCRUB) { 1534*12296SLin.Ling@Sun.COM (void) snprintf(msg, sizeof (msg), 1535*12296SLin.Ling@Sun.COM dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1536*12296SLin.Ling@Sun.COM } else if (func == POOL_SCAN_NONE) { 1537*12296SLin.Ling@Sun.COM (void) snprintf(msg, sizeof (msg), 1538*12296SLin.Ling@Sun.COM dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 1539*12296SLin.Ling@Sun.COM zc.zc_name); 1540*12296SLin.Ling@Sun.COM } else { 1541*12296SLin.Ling@Sun.COM assert(!"unexpected result"); 1542*12296SLin.Ling@Sun.COM } 1543*12296SLin.Ling@Sun.COM 1544*12296SLin.Ling@Sun.COM if (errno == EBUSY) { 1545*12296SLin.Ling@Sun.COM nvlist_t *nvroot; 1546*12296SLin.Ling@Sun.COM pool_scan_stat_t *ps = NULL; 1547*12296SLin.Ling@Sun.COM uint_t psc; 1548*12296SLin.Ling@Sun.COM 1549*12296SLin.Ling@Sun.COM verify(nvlist_lookup_nvlist(zhp->zpool_config, 1550*12296SLin.Ling@Sun.COM ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1551*12296SLin.Ling@Sun.COM (void) nvlist_lookup_uint64_array(nvroot, 1552*12296SLin.Ling@Sun.COM ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 1553*12296SLin.Ling@Sun.COM if (ps && ps->pss_func == POOL_SCAN_SCRUB) 1554*12296SLin.Ling@Sun.COM return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 1555*12296SLin.Ling@Sun.COM else 1556*12296SLin.Ling@Sun.COM return (zfs_error(hdl, EZFS_RESILVERING, msg)); 1557*12296SLin.Ling@Sun.COM } else if (errno == ENOENT) { 1558*12296SLin.Ling@Sun.COM return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 1559*12296SLin.Ling@Sun.COM } else { 15602082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1561*12296SLin.Ling@Sun.COM } 1562789Sahrens } 1563789Sahrens 15642468Sek110237 /* 15659816SGeorge.Wilson@Sun.COM * Find a vdev that matches the search criteria specified. We use the 15669816SGeorge.Wilson@Sun.COM * the nvpair name to determine how we should look for the device. 15672468Sek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 15682468Sek110237 * spare; but FALSE if its an INUSE spare. 15692468Sek110237 */ 15702082Seschrock static nvlist_t * 15719816SGeorge.Wilson@Sun.COM vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 15729816SGeorge.Wilson@Sun.COM boolean_t *l2cache, boolean_t *log) 15731544Seschrock { 15741544Seschrock uint_t c, children; 15751544Seschrock nvlist_t **child; 15762082Seschrock nvlist_t *ret; 15777326SEric.Schrock@Sun.COM uint64_t is_log; 15789816SGeorge.Wilson@Sun.COM char *srchkey; 15799816SGeorge.Wilson@Sun.COM nvpair_t *pair = nvlist_next_nvpair(search, NULL); 15809816SGeorge.Wilson@Sun.COM 15819816SGeorge.Wilson@Sun.COM /* Nothing to look for */ 15829816SGeorge.Wilson@Sun.COM if (search == NULL || pair == NULL) 15839816SGeorge.Wilson@Sun.COM return (NULL); 15849816SGeorge.Wilson@Sun.COM 15859816SGeorge.Wilson@Sun.COM /* Obtain the key we will use to search */ 15869816SGeorge.Wilson@Sun.COM srchkey = nvpair_name(pair); 15879816SGeorge.Wilson@Sun.COM 15889816SGeorge.Wilson@Sun.COM switch (nvpair_type(pair)) { 15899816SGeorge.Wilson@Sun.COM case DATA_TYPE_UINT64: { 15909816SGeorge.Wilson@Sun.COM uint64_t srchval, theguid, present; 15919816SGeorge.Wilson@Sun.COM 15929816SGeorge.Wilson@Sun.COM verify(nvpair_value_uint64(pair, &srchval) == 0); 15939816SGeorge.Wilson@Sun.COM if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 15949816SGeorge.Wilson@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 15959816SGeorge.Wilson@Sun.COM &present) == 0) { 15969816SGeorge.Wilson@Sun.COM /* 15979816SGeorge.Wilson@Sun.COM * If the device has never been present since 15989816SGeorge.Wilson@Sun.COM * import, the only reliable way to match the 15999816SGeorge.Wilson@Sun.COM * vdev is by GUID. 16009816SGeorge.Wilson@Sun.COM */ 16019816SGeorge.Wilson@Sun.COM verify(nvlist_lookup_uint64(nv, 16029816SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_GUID, &theguid) == 0); 16039816SGeorge.Wilson@Sun.COM if (theguid == srchval) 16049816SGeorge.Wilson@Sun.COM return (nv); 16059816SGeorge.Wilson@Sun.COM } 16069816SGeorge.Wilson@Sun.COM } 16079816SGeorge.Wilson@Sun.COM break; 16089816SGeorge.Wilson@Sun.COM } 16099816SGeorge.Wilson@Sun.COM 16109816SGeorge.Wilson@Sun.COM case DATA_TYPE_STRING: { 16119816SGeorge.Wilson@Sun.COM char *srchval, *val; 16129816SGeorge.Wilson@Sun.COM 16139816SGeorge.Wilson@Sun.COM verify(nvpair_value_string(pair, &srchval) == 0); 16149816SGeorge.Wilson@Sun.COM if (nvlist_lookup_string(nv, srchkey, &val) != 0) 16159816SGeorge.Wilson@Sun.COM break; 16169816SGeorge.Wilson@Sun.COM 16171544Seschrock /* 16189816SGeorge.Wilson@Sun.COM * Search for the requested value. We special case the search 161910594SGeorge.Wilson@Sun.COM * for ZPOOL_CONFIG_PATH when it's a wholedisk and when 162010594SGeorge.Wilson@Sun.COM * Looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 162110594SGeorge.Wilson@Sun.COM * Otherwise, all other searches are simple string compares. 16221544Seschrock */ 16239816SGeorge.Wilson@Sun.COM if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && val) { 16249816SGeorge.Wilson@Sun.COM uint64_t wholedisk = 0; 16259816SGeorge.Wilson@Sun.COM 16269816SGeorge.Wilson@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 16279816SGeorge.Wilson@Sun.COM &wholedisk); 16289816SGeorge.Wilson@Sun.COM if (wholedisk) { 16299816SGeorge.Wilson@Sun.COM /* 16309816SGeorge.Wilson@Sun.COM * For whole disks, the internal path has 's0', 16319816SGeorge.Wilson@Sun.COM * but the path passed in by the user doesn't. 16329816SGeorge.Wilson@Sun.COM */ 16339816SGeorge.Wilson@Sun.COM if (strlen(srchval) == strlen(val) - 2 && 16349816SGeorge.Wilson@Sun.COM strncmp(srchval, val, strlen(srchval)) == 0) 16359816SGeorge.Wilson@Sun.COM return (nv); 16369816SGeorge.Wilson@Sun.COM break; 16379816SGeorge.Wilson@Sun.COM } 163810594SGeorge.Wilson@Sun.COM } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 163910594SGeorge.Wilson@Sun.COM char *type, *idx, *end, *p; 164010594SGeorge.Wilson@Sun.COM uint64_t id, vdev_id; 164110594SGeorge.Wilson@Sun.COM 164210594SGeorge.Wilson@Sun.COM /* 164310594SGeorge.Wilson@Sun.COM * Determine our vdev type, keeping in mind 164410594SGeorge.Wilson@Sun.COM * that the srchval is composed of a type and 164510594SGeorge.Wilson@Sun.COM * vdev id pair (i.e. mirror-4). 164610594SGeorge.Wilson@Sun.COM */ 164710594SGeorge.Wilson@Sun.COM if ((type = strdup(srchval)) == NULL) 164810594SGeorge.Wilson@Sun.COM return (NULL); 164910594SGeorge.Wilson@Sun.COM 165010594SGeorge.Wilson@Sun.COM if ((p = strrchr(type, '-')) == NULL) { 165110594SGeorge.Wilson@Sun.COM free(type); 165210594SGeorge.Wilson@Sun.COM break; 165310594SGeorge.Wilson@Sun.COM } 165410594SGeorge.Wilson@Sun.COM idx = p + 1; 165510594SGeorge.Wilson@Sun.COM *p = '\0'; 165610594SGeorge.Wilson@Sun.COM 165710594SGeorge.Wilson@Sun.COM /* 165810594SGeorge.Wilson@Sun.COM * If the types don't match then keep looking. 165910594SGeorge.Wilson@Sun.COM */ 166010594SGeorge.Wilson@Sun.COM if (strncmp(val, type, strlen(val)) != 0) { 166110594SGeorge.Wilson@Sun.COM free(type); 166210594SGeorge.Wilson@Sun.COM break; 166310594SGeorge.Wilson@Sun.COM } 166410594SGeorge.Wilson@Sun.COM 166510594SGeorge.Wilson@Sun.COM verify(strncmp(type, VDEV_TYPE_RAIDZ, 166610594SGeorge.Wilson@Sun.COM strlen(VDEV_TYPE_RAIDZ)) == 0 || 166710594SGeorge.Wilson@Sun.COM strncmp(type, VDEV_TYPE_MIRROR, 166810594SGeorge.Wilson@Sun.COM strlen(VDEV_TYPE_MIRROR)) == 0); 166910594SGeorge.Wilson@Sun.COM verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 167010594SGeorge.Wilson@Sun.COM &id) == 0); 167110594SGeorge.Wilson@Sun.COM 167210594SGeorge.Wilson@Sun.COM errno = 0; 167310594SGeorge.Wilson@Sun.COM vdev_id = strtoull(idx, &end, 10); 167410594SGeorge.Wilson@Sun.COM 167510594SGeorge.Wilson@Sun.COM free(type); 167610594SGeorge.Wilson@Sun.COM if (errno != 0) 167710594SGeorge.Wilson@Sun.COM return (NULL); 167810594SGeorge.Wilson@Sun.COM 167910594SGeorge.Wilson@Sun.COM /* 168010594SGeorge.Wilson@Sun.COM * Now verify that we have the correct vdev id. 168110594SGeorge.Wilson@Sun.COM */ 168210594SGeorge.Wilson@Sun.COM if (vdev_id == id) 168310594SGeorge.Wilson@Sun.COM return (nv); 16849816SGeorge.Wilson@Sun.COM } 16859816SGeorge.Wilson@Sun.COM 16869816SGeorge.Wilson@Sun.COM /* 16879816SGeorge.Wilson@Sun.COM * Common case 16889816SGeorge.Wilson@Sun.COM */ 16899816SGeorge.Wilson@Sun.COM if (strcmp(srchval, val) == 0) 16902082Seschrock return (nv); 16919816SGeorge.Wilson@Sun.COM break; 16929816SGeorge.Wilson@Sun.COM } 16939816SGeorge.Wilson@Sun.COM 16949816SGeorge.Wilson@Sun.COM default: 16959816SGeorge.Wilson@Sun.COM break; 16961544Seschrock } 16971544Seschrock 16981544Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 16991544Seschrock &child, &children) != 0) 17002082Seschrock return (NULL); 17011544Seschrock 17027326SEric.Schrock@Sun.COM for (c = 0; c < children; c++) { 17039816SGeorge.Wilson@Sun.COM if ((ret = vdev_to_nvlist_iter(child[c], search, 17047326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 17057326SEric.Schrock@Sun.COM /* 17067326SEric.Schrock@Sun.COM * The 'is_log' value is only set for the toplevel 17077326SEric.Schrock@Sun.COM * vdev, not the leaf vdevs. So we always lookup the 17087326SEric.Schrock@Sun.COM * log device from the root of the vdev tree (where 17097326SEric.Schrock@Sun.COM * 'log' is non-NULL). 17107326SEric.Schrock@Sun.COM */ 17117326SEric.Schrock@Sun.COM if (log != NULL && 17127326SEric.Schrock@Sun.COM nvlist_lookup_uint64(child[c], 17137326SEric.Schrock@Sun.COM ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 17147326SEric.Schrock@Sun.COM is_log) { 17157326SEric.Schrock@Sun.COM *log = B_TRUE; 17167326SEric.Schrock@Sun.COM } 17171544Seschrock return (ret); 17187326SEric.Schrock@Sun.COM } 17197326SEric.Schrock@Sun.COM } 17201544Seschrock 17212082Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 17222082Seschrock &child, &children) == 0) { 17232082Seschrock for (c = 0; c < children; c++) { 17249816SGeorge.Wilson@Sun.COM if ((ret = vdev_to_nvlist_iter(child[c], search, 17257326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 17262468Sek110237 *avail_spare = B_TRUE; 17272082Seschrock return (ret); 17282082Seschrock } 17292082Seschrock } 17302082Seschrock } 17312082Seschrock 17325450Sbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 17335450Sbrendan &child, &children) == 0) { 17345450Sbrendan for (c = 0; c < children; c++) { 17359816SGeorge.Wilson@Sun.COM if ((ret = vdev_to_nvlist_iter(child[c], search, 17367326SEric.Schrock@Sun.COM avail_spare, l2cache, NULL)) != NULL) { 17375450Sbrendan *l2cache = B_TRUE; 17385450Sbrendan return (ret); 17395450Sbrendan } 17405450Sbrendan } 17415450Sbrendan } 17425450Sbrendan 17432082Seschrock return (NULL); 17441544Seschrock } 17451544Seschrock 17469816SGeorge.Wilson@Sun.COM /* 17479816SGeorge.Wilson@Sun.COM * Given a physical path (minus the "/devices" prefix), find the 17489816SGeorge.Wilson@Sun.COM * associated vdev. 17499816SGeorge.Wilson@Sun.COM */ 17509816SGeorge.Wilson@Sun.COM nvlist_t * 17519816SGeorge.Wilson@Sun.COM zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 17529816SGeorge.Wilson@Sun.COM boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 17539816SGeorge.Wilson@Sun.COM { 17549816SGeorge.Wilson@Sun.COM nvlist_t *search, *nvroot, *ret; 17559816SGeorge.Wilson@Sun.COM 17569816SGeorge.Wilson@Sun.COM verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 17579816SGeorge.Wilson@Sun.COM verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 17589816SGeorge.Wilson@Sun.COM 17599816SGeorge.Wilson@Sun.COM verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 17609816SGeorge.Wilson@Sun.COM &nvroot) == 0); 17619816SGeorge.Wilson@Sun.COM 17629816SGeorge.Wilson@Sun.COM *avail_spare = B_FALSE; 17639816SGeorge.Wilson@Sun.COM ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 17649816SGeorge.Wilson@Sun.COM nvlist_free(search); 17659816SGeorge.Wilson@Sun.COM 17669816SGeorge.Wilson@Sun.COM return (ret); 17679816SGeorge.Wilson@Sun.COM } 17689816SGeorge.Wilson@Sun.COM 176910594SGeorge.Wilson@Sun.COM /* 177010594SGeorge.Wilson@Sun.COM * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 177110594SGeorge.Wilson@Sun.COM */ 177210594SGeorge.Wilson@Sun.COM boolean_t 177310594SGeorge.Wilson@Sun.COM zpool_vdev_is_interior(const char *name) 177410594SGeorge.Wilson@Sun.COM { 177510594SGeorge.Wilson@Sun.COM if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 177610594SGeorge.Wilson@Sun.COM strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 177710594SGeorge.Wilson@Sun.COM return (B_TRUE); 177810594SGeorge.Wilson@Sun.COM return (B_FALSE); 177910594SGeorge.Wilson@Sun.COM } 178010594SGeorge.Wilson@Sun.COM 17812082Seschrock nvlist_t * 17825450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 17837326SEric.Schrock@Sun.COM boolean_t *l2cache, boolean_t *log) 17841544Seschrock { 17851544Seschrock char buf[MAXPATHLEN]; 17861544Seschrock char *end; 17879816SGeorge.Wilson@Sun.COM nvlist_t *nvroot, *search, *ret; 17881544Seschrock uint64_t guid; 17891544Seschrock 17909816SGeorge.Wilson@Sun.COM verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 17919816SGeorge.Wilson@Sun.COM 17921613Seschrock guid = strtoull(path, &end, 10); 17931544Seschrock if (guid != 0 && *end == '\0') { 17949816SGeorge.Wilson@Sun.COM verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 179510594SGeorge.Wilson@Sun.COM } else if (zpool_vdev_is_interior(path)) { 179610594SGeorge.Wilson@Sun.COM verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 17971544Seschrock } else if (path[0] != '/') { 17981544Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 17999816SGeorge.Wilson@Sun.COM verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 18001544Seschrock } else { 18019816SGeorge.Wilson@Sun.COM verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 18021544Seschrock } 18031544Seschrock 18041544Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 18051544Seschrock &nvroot) == 0); 18061544Seschrock 18072468Sek110237 *avail_spare = B_FALSE; 18085450Sbrendan *l2cache = B_FALSE; 18097326SEric.Schrock@Sun.COM if (log != NULL) 18107326SEric.Schrock@Sun.COM *log = B_FALSE; 18119816SGeorge.Wilson@Sun.COM ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 18129816SGeorge.Wilson@Sun.COM nvlist_free(search); 18139816SGeorge.Wilson@Sun.COM 18149816SGeorge.Wilson@Sun.COM return (ret); 18152468Sek110237 } 18162468Sek110237 18177656SSherry.Moore@Sun.COM static int 18187656SSherry.Moore@Sun.COM vdev_online(nvlist_t *nv) 18197656SSherry.Moore@Sun.COM { 18207656SSherry.Moore@Sun.COM uint64_t ival; 18217656SSherry.Moore@Sun.COM 18227656SSherry.Moore@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 18237656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 18247656SSherry.Moore@Sun.COM nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 18257656SSherry.Moore@Sun.COM return (0); 18267656SSherry.Moore@Sun.COM 18277656SSherry.Moore@Sun.COM return (1); 18287656SSherry.Moore@Sun.COM } 18297656SSherry.Moore@Sun.COM 18307656SSherry.Moore@Sun.COM /* 18319790SLin.Ling@Sun.COM * Helper function for zpool_get_physpaths(). 18327656SSherry.Moore@Sun.COM */ 18339160SSherry.Moore@Sun.COM static int 18349790SLin.Ling@Sun.COM vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 18359160SSherry.Moore@Sun.COM size_t *bytes_written) 18367656SSherry.Moore@Sun.COM { 18379160SSherry.Moore@Sun.COM size_t bytes_left, pos, rsz; 18389160SSherry.Moore@Sun.COM char *tmppath; 18399160SSherry.Moore@Sun.COM const char *format; 18409160SSherry.Moore@Sun.COM 18419160SSherry.Moore@Sun.COM if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 18429160SSherry.Moore@Sun.COM &tmppath) != 0) 18439160SSherry.Moore@Sun.COM return (EZFS_NODEVICE); 18449160SSherry.Moore@Sun.COM 18459160SSherry.Moore@Sun.COM pos = *bytes_written; 18469160SSherry.Moore@Sun.COM bytes_left = physpath_size - pos; 18479160SSherry.Moore@Sun.COM format = (pos == 0) ? "%s" : " %s"; 18489160SSherry.Moore@Sun.COM 18499160SSherry.Moore@Sun.COM rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 18509160SSherry.Moore@Sun.COM *bytes_written += rsz; 18519160SSherry.Moore@Sun.COM 18529160SSherry.Moore@Sun.COM if (rsz >= bytes_left) { 18539160SSherry.Moore@Sun.COM /* if physpath was not copied properly, clear it */ 18549160SSherry.Moore@Sun.COM if (bytes_left != 0) { 18559160SSherry.Moore@Sun.COM physpath[pos] = 0; 18569160SSherry.Moore@Sun.COM } 18579160SSherry.Moore@Sun.COM return (EZFS_NOSPC); 18589160SSherry.Moore@Sun.COM } 18599160SSherry.Moore@Sun.COM return (0); 18609160SSherry.Moore@Sun.COM } 18619160SSherry.Moore@Sun.COM 18629790SLin.Ling@Sun.COM static int 18639790SLin.Ling@Sun.COM vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 18649790SLin.Ling@Sun.COM size_t *rsz, boolean_t is_spare) 18659790SLin.Ling@Sun.COM { 18669790SLin.Ling@Sun.COM char *type; 18679790SLin.Ling@Sun.COM int ret; 18689790SLin.Ling@Sun.COM 18699790SLin.Ling@Sun.COM if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 18709790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 18719790SLin.Ling@Sun.COM 18729790SLin.Ling@Sun.COM if (strcmp(type, VDEV_TYPE_DISK) == 0) { 18739790SLin.Ling@Sun.COM /* 18749790SLin.Ling@Sun.COM * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 18759790SLin.Ling@Sun.COM * For a spare vdev, we only want to boot from the active 18769790SLin.Ling@Sun.COM * spare device. 18779790SLin.Ling@Sun.COM */ 18789790SLin.Ling@Sun.COM if (is_spare) { 18799790SLin.Ling@Sun.COM uint64_t spare = 0; 18809790SLin.Ling@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 18819790SLin.Ling@Sun.COM &spare); 18829790SLin.Ling@Sun.COM if (!spare) 18839790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 18849790SLin.Ling@Sun.COM } 18859790SLin.Ling@Sun.COM 18869790SLin.Ling@Sun.COM if (vdev_online(nv)) { 18879790SLin.Ling@Sun.COM if ((ret = vdev_get_one_physpath(nv, physpath, 18889790SLin.Ling@Sun.COM phypath_size, rsz)) != 0) 18899790SLin.Ling@Sun.COM return (ret); 18909790SLin.Ling@Sun.COM } 18919790SLin.Ling@Sun.COM } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 18929790SLin.Ling@Sun.COM strcmp(type, VDEV_TYPE_REPLACING) == 0 || 18939790SLin.Ling@Sun.COM (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 18949790SLin.Ling@Sun.COM nvlist_t **child; 18959790SLin.Ling@Sun.COM uint_t count; 18969790SLin.Ling@Sun.COM int i, ret; 18979790SLin.Ling@Sun.COM 18989790SLin.Ling@Sun.COM if (nvlist_lookup_nvlist_array(nv, 18999790SLin.Ling@Sun.COM ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 19009790SLin.Ling@Sun.COM return (EZFS_INVALCONFIG); 19019790SLin.Ling@Sun.COM 19029790SLin.Ling@Sun.COM for (i = 0; i < count; i++) { 19039790SLin.Ling@Sun.COM ret = vdev_get_physpaths(child[i], physpath, 19049790SLin.Ling@Sun.COM phypath_size, rsz, is_spare); 19059790SLin.Ling@Sun.COM if (ret == EZFS_NOSPC) 19069790SLin.Ling@Sun.COM return (ret); 19079790SLin.Ling@Sun.COM } 19089790SLin.Ling@Sun.COM } 19099790SLin.Ling@Sun.COM 19109790SLin.Ling@Sun.COM return (EZFS_POOL_INVALARG); 19119790SLin.Ling@Sun.COM } 19129790SLin.Ling@Sun.COM 19139160SSherry.Moore@Sun.COM /* 19149160SSherry.Moore@Sun.COM * Get phys_path for a root pool config. 19159160SSherry.Moore@Sun.COM * Return 0 on success; non-zero on failure. 19169160SSherry.Moore@Sun.COM */ 19179160SSherry.Moore@Sun.COM static int 19189160SSherry.Moore@Sun.COM zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 19199160SSherry.Moore@Sun.COM { 19209160SSherry.Moore@Sun.COM size_t rsz; 19217656SSherry.Moore@Sun.COM nvlist_t *vdev_root; 19227656SSherry.Moore@Sun.COM nvlist_t **child; 19237656SSherry.Moore@Sun.COM uint_t count; 19249160SSherry.Moore@Sun.COM char *type; 19259160SSherry.Moore@Sun.COM 19269160SSherry.Moore@Sun.COM rsz = 0; 19279160SSherry.Moore@Sun.COM 19289160SSherry.Moore@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 19299160SSherry.Moore@Sun.COM &vdev_root) != 0) 19309160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 19319160SSherry.Moore@Sun.COM 19329160SSherry.Moore@Sun.COM if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 19339160SSherry.Moore@Sun.COM nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 19349160SSherry.Moore@Sun.COM &child, &count) != 0) 19359160SSherry.Moore@Sun.COM return (EZFS_INVALCONFIG); 19367656SSherry.Moore@Sun.COM 19377656SSherry.Moore@Sun.COM /* 19389160SSherry.Moore@Sun.COM * root pool can not have EFI labeled disks and can only have 19399160SSherry.Moore@Sun.COM * a single top-level vdev. 19407656SSherry.Moore@Sun.COM */ 19419160SSherry.Moore@Sun.COM if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 19429160SSherry.Moore@Sun.COM pool_uses_efi(vdev_root)) 19439160SSherry.Moore@Sun.COM return (EZFS_POOL_INVALARG); 19449160SSherry.Moore@Sun.COM 19459790SLin.Ling@Sun.COM (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 19469790SLin.Ling@Sun.COM B_FALSE); 19477656SSherry.Moore@Sun.COM 19489160SSherry.Moore@Sun.COM /* No online devices */ 19499160SSherry.Moore@Sun.COM if (rsz == 0) 19509160SSherry.Moore@Sun.COM return (EZFS_NODEVICE); 19519160SSherry.Moore@Sun.COM 19527656SSherry.Moore@Sun.COM return (0); 19537656SSherry.Moore@Sun.COM } 19547656SSherry.Moore@Sun.COM 19552468Sek110237 /* 19569160SSherry.Moore@Sun.COM * Get phys_path for a root pool 19579160SSherry.Moore@Sun.COM * Return 0 on success; non-zero on failure. 19589160SSherry.Moore@Sun.COM */ 19599160SSherry.Moore@Sun.COM int 19609160SSherry.Moore@Sun.COM zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 19619160SSherry.Moore@Sun.COM { 19629160SSherry.Moore@Sun.COM return (zpool_get_config_physpath(zhp->zpool_config, physpath, 19639160SSherry.Moore@Sun.COM phypath_size)); 19649160SSherry.Moore@Sun.COM } 19659160SSherry.Moore@Sun.COM 19669160SSherry.Moore@Sun.COM /* 19679816SGeorge.Wilson@Sun.COM * If the device has being dynamically expanded then we need to relabel 19689816SGeorge.Wilson@Sun.COM * the disk to use the new unallocated space. 19699816SGeorge.Wilson@Sun.COM */ 19709816SGeorge.Wilson@Sun.COM static int 19719816SGeorge.Wilson@Sun.COM zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 19729816SGeorge.Wilson@Sun.COM { 19739816SGeorge.Wilson@Sun.COM char path[MAXPATHLEN]; 19749816SGeorge.Wilson@Sun.COM char errbuf[1024]; 19759816SGeorge.Wilson@Sun.COM int fd, error; 19769816SGeorge.Wilson@Sun.COM int (*_efi_use_whole_disk)(int); 19779816SGeorge.Wilson@Sun.COM 19789816SGeorge.Wilson@Sun.COM if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 19799816SGeorge.Wilson@Sun.COM "efi_use_whole_disk")) == NULL) 19809816SGeorge.Wilson@Sun.COM return (-1); 19819816SGeorge.Wilson@Sun.COM 19829816SGeorge.Wilson@Sun.COM (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name); 19839816SGeorge.Wilson@Sun.COM 19849816SGeorge.Wilson@Sun.COM if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 19859816SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 19869816SGeorge.Wilson@Sun.COM "relabel '%s': unable to open device"), name); 19879816SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 19889816SGeorge.Wilson@Sun.COM } 19899816SGeorge.Wilson@Sun.COM 19909816SGeorge.Wilson@Sun.COM /* 19919816SGeorge.Wilson@Sun.COM * It's possible that we might encounter an error if the device 19929816SGeorge.Wilson@Sun.COM * does not have any unallocated space left. If so, we simply 19939816SGeorge.Wilson@Sun.COM * ignore that error and continue on. 19949816SGeorge.Wilson@Sun.COM */ 19959816SGeorge.Wilson@Sun.COM error = _efi_use_whole_disk(fd); 19969816SGeorge.Wilson@Sun.COM (void) close(fd); 19979816SGeorge.Wilson@Sun.COM if (error && error != VT_ENOSPC) { 19989816SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 19999816SGeorge.Wilson@Sun.COM "relabel '%s': unable to read disk capacity"), name); 20009816SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 20019816SGeorge.Wilson@Sun.COM } 20029816SGeorge.Wilson@Sun.COM return (0); 20039816SGeorge.Wilson@Sun.COM } 20049816SGeorge.Wilson@Sun.COM 20059816SGeorge.Wilson@Sun.COM /* 20064451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 20074451Seschrock * ZFS_ONLINE_* flags. 2008789Sahrens */ 2009789Sahrens int 20104451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 20114451Seschrock vdev_state_t *newstate) 2012789Sahrens { 2013789Sahrens zfs_cmd_t zc = { 0 }; 2014789Sahrens char msg[1024]; 20152082Seschrock nvlist_t *tgt; 20169816SGeorge.Wilson@Sun.COM boolean_t avail_spare, l2cache, islog; 20172082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2018789Sahrens 20199816SGeorge.Wilson@Sun.COM if (flags & ZFS_ONLINE_EXPAND) { 20209816SGeorge.Wilson@Sun.COM (void) snprintf(msg, sizeof (msg), 20219816SGeorge.Wilson@Sun.COM dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 20229816SGeorge.Wilson@Sun.COM } else { 20239816SGeorge.Wilson@Sun.COM (void) snprintf(msg, sizeof (msg), 20249816SGeorge.Wilson@Sun.COM dgettext(TEXT_DOMAIN, "cannot online %s"), path); 20259816SGeorge.Wilson@Sun.COM } 2026789Sahrens 20271544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20287326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 20299816SGeorge.Wilson@Sun.COM &islog)) == NULL) 20302082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2031789Sahrens 20322468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 20332468Sek110237 203410817SEric.Schrock@Sun.COM if (avail_spare) 20352082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 20362082Seschrock 20379816SGeorge.Wilson@Sun.COM if (flags & ZFS_ONLINE_EXPAND || 20389816SGeorge.Wilson@Sun.COM zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 20399816SGeorge.Wilson@Sun.COM char *pathname = NULL; 20409816SGeorge.Wilson@Sun.COM uint64_t wholedisk = 0; 20419816SGeorge.Wilson@Sun.COM 20429816SGeorge.Wilson@Sun.COM (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 20439816SGeorge.Wilson@Sun.COM &wholedisk); 20449816SGeorge.Wilson@Sun.COM verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 20459816SGeorge.Wilson@Sun.COM &pathname) == 0); 20469816SGeorge.Wilson@Sun.COM 20479816SGeorge.Wilson@Sun.COM /* 20489816SGeorge.Wilson@Sun.COM * XXX - L2ARC 1.0 devices can't support expansion. 20499816SGeorge.Wilson@Sun.COM */ 20509816SGeorge.Wilson@Sun.COM if (l2cache) { 20519816SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20529816SGeorge.Wilson@Sun.COM "cannot expand cache devices")); 20539816SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 20549816SGeorge.Wilson@Sun.COM } 20559816SGeorge.Wilson@Sun.COM 20569816SGeorge.Wilson@Sun.COM if (wholedisk) { 20579816SGeorge.Wilson@Sun.COM pathname += strlen(DISK_ROOT) + 1; 20589816SGeorge.Wilson@Sun.COM (void) zpool_relabel_disk(zhp->zpool_hdl, pathname); 20599816SGeorge.Wilson@Sun.COM } 20609816SGeorge.Wilson@Sun.COM } 20619816SGeorge.Wilson@Sun.COM 20624451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 20634451Seschrock zc.zc_obj = flags; 20644451Seschrock 206511422SMark.Musante@Sun.COM if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 206611422SMark.Musante@Sun.COM if (errno == EINVAL) { 206711422SMark.Musante@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 206811422SMark.Musante@Sun.COM "from this pool into a new one. Use '%s' " 206911422SMark.Musante@Sun.COM "instead"), "zpool detach"); 207011422SMark.Musante@Sun.COM return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 207111422SMark.Musante@Sun.COM } 20724451Seschrock return (zpool_standard_error(hdl, errno, msg)); 207311422SMark.Musante@Sun.COM } 20744451Seschrock 20754451Seschrock *newstate = zc.zc_cookie; 20764451Seschrock return (0); 2077789Sahrens } 2078789Sahrens 2079789Sahrens /* 2080789Sahrens * Take the specified vdev offline 2081789Sahrens */ 2082789Sahrens int 20834451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2084789Sahrens { 2085789Sahrens zfs_cmd_t zc = { 0 }; 2086789Sahrens char msg[1024]; 20872082Seschrock nvlist_t *tgt; 20885450Sbrendan boolean_t avail_spare, l2cache; 20892082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2090789Sahrens 20911544Seschrock (void) snprintf(msg, sizeof (msg), 20921544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 20931544Seschrock 2094789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20957326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 20967326SEric.Schrock@Sun.COM NULL)) == NULL) 20972082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 20982082Seschrock 20992468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 21002468Sek110237 210110817SEric.Schrock@Sun.COM if (avail_spare) 21022082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 21032082Seschrock 21044451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 21054451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 21061485Slling 21074543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2108789Sahrens return (0); 2109789Sahrens 2110789Sahrens switch (errno) { 21112082Seschrock case EBUSY: 2112789Sahrens 2113789Sahrens /* 2114789Sahrens * There are no other replicas of this device. 2115789Sahrens */ 21162082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 21172082Seschrock 21189701SGeorge.Wilson@Sun.COM case EEXIST: 21199701SGeorge.Wilson@Sun.COM /* 21209701SGeorge.Wilson@Sun.COM * The log device has unplayed logs 21219701SGeorge.Wilson@Sun.COM */ 21229701SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 21239701SGeorge.Wilson@Sun.COM 21242082Seschrock default: 21252082Seschrock return (zpool_standard_error(hdl, errno, msg)); 21262082Seschrock } 21272082Seschrock } 2128789Sahrens 21292082Seschrock /* 21304451Seschrock * Mark the given vdev faulted. 21314451Seschrock */ 21324451Seschrock int 213310817SEric.Schrock@Sun.COM zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 21344451Seschrock { 21354451Seschrock zfs_cmd_t zc = { 0 }; 21364451Seschrock char msg[1024]; 21374451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21384451Seschrock 21394451Seschrock (void) snprintf(msg, sizeof (msg), 21404451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 21414451Seschrock 21424451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21434451Seschrock zc.zc_guid = guid; 21444451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 214510817SEric.Schrock@Sun.COM zc.zc_obj = aux; 21464451Seschrock 21474451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 21484451Seschrock return (0); 21494451Seschrock 21504451Seschrock switch (errno) { 21514451Seschrock case EBUSY: 21524451Seschrock 21534451Seschrock /* 21544451Seschrock * There are no other replicas of this device. 21554451Seschrock */ 21564451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 21574451Seschrock 21584451Seschrock default: 21594451Seschrock return (zpool_standard_error(hdl, errno, msg)); 21604451Seschrock } 21614451Seschrock 21624451Seschrock } 21634451Seschrock 21644451Seschrock /* 21654451Seschrock * Mark the given vdev degraded. 21664451Seschrock */ 21674451Seschrock int 216810817SEric.Schrock@Sun.COM zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 21694451Seschrock { 21704451Seschrock zfs_cmd_t zc = { 0 }; 21714451Seschrock char msg[1024]; 21724451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21734451Seschrock 21744451Seschrock (void) snprintf(msg, sizeof (msg), 21754451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 21764451Seschrock 21774451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21784451Seschrock zc.zc_guid = guid; 21794451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 218010817SEric.Schrock@Sun.COM zc.zc_obj = aux; 21814451Seschrock 21824451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 21834451Seschrock return (0); 21844451Seschrock 21854451Seschrock return (zpool_standard_error(hdl, errno, msg)); 21864451Seschrock } 21874451Seschrock 21884451Seschrock /* 21892082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 21902082Seschrock * a hot spare. 21912082Seschrock */ 21922082Seschrock static boolean_t 21932082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 21942082Seschrock { 21952082Seschrock nvlist_t **child; 21962082Seschrock uint_t c, children; 21972082Seschrock char *type; 21982082Seschrock 21992082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 22002082Seschrock &children) == 0) { 22012082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 22022082Seschrock &type) == 0); 22032082Seschrock 22042082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 22052082Seschrock children == 2 && child[which] == tgt) 22062082Seschrock return (B_TRUE); 22072082Seschrock 22082082Seschrock for (c = 0; c < children; c++) 22092082Seschrock if (is_replacing_spare(child[c], tgt, which)) 22102082Seschrock return (B_TRUE); 2211789Sahrens } 22122082Seschrock 22132082Seschrock return (B_FALSE); 2214789Sahrens } 2215789Sahrens 2216789Sahrens /* 2217789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 22184527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2219789Sahrens */ 2220789Sahrens int 2221789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2222789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2223789Sahrens { 2224789Sahrens zfs_cmd_t zc = { 0 }; 2225789Sahrens char msg[1024]; 2226789Sahrens int ret; 22272082Seschrock nvlist_t *tgt; 22287326SEric.Schrock@Sun.COM boolean_t avail_spare, l2cache, islog; 22297326SEric.Schrock@Sun.COM uint64_t val; 22307041Seschrock char *path, *newname; 22312082Seschrock nvlist_t **child; 22322082Seschrock uint_t children; 22332082Seschrock nvlist_t *config_root; 22342082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 22357965SGeorge.Wilson@Sun.COM boolean_t rootpool = pool_is_bootable(zhp); 2236789Sahrens 22371544Seschrock if (replacing) 22381544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 22391544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 22401544Seschrock else 22411544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 22421544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 22431544Seschrock 22447965SGeorge.Wilson@Sun.COM /* 22457965SGeorge.Wilson@Sun.COM * If this is a root pool, make sure that we're not attaching an 22467965SGeorge.Wilson@Sun.COM * EFI labeled device. 22477965SGeorge.Wilson@Sun.COM */ 22487965SGeorge.Wilson@Sun.COM if (rootpool && pool_uses_efi(nvroot)) { 22497965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 22507965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root pools.")); 22517965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 22527965SGeorge.Wilson@Sun.COM } 22537965SGeorge.Wilson@Sun.COM 2254789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 22557326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 22567326SEric.Schrock@Sun.COM &islog)) == 0) 22572082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 22582082Seschrock 22592468Sek110237 if (avail_spare) 22602082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 22612082Seschrock 22625450Sbrendan if (l2cache) 22635450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 22645450Sbrendan 22652082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 22662082Seschrock zc.zc_cookie = replacing; 22672082Seschrock 22682082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 22692082Seschrock &child, &children) != 0 || children != 1) { 22702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 22712082Seschrock "new device must be a single disk")); 22722082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 22731544Seschrock } 22742082Seschrock 22752082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 22762082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 22772082Seschrock 227810594SGeorge.Wilson@Sun.COM if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 22797041Seschrock return (-1); 22807041Seschrock 22812082Seschrock /* 22822082Seschrock * If the target is a hot spare that has been swapped in, we can only 22832082Seschrock * replace it with another hot spare. 22842082Seschrock */ 22852082Seschrock if (replacing && 22862082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 22877326SEric.Schrock@Sun.COM (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 22887326SEric.Schrock@Sun.COM NULL) == NULL || !avail_spare) && 22897326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 1)) { 22902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 22912082Seschrock "can only be replaced by another hot spare")); 22927041Seschrock free(newname); 22932082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 22942082Seschrock } 22952082Seschrock 22962082Seschrock /* 22972082Seschrock * If we are attempting to replace a spare, it canot be applied to an 22982082Seschrock * already spared device. 22992082Seschrock */ 23002082Seschrock if (replacing && 23012082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 23027326SEric.Schrock@Sun.COM zpool_find_vdev(zhp, newname, &avail_spare, 23037326SEric.Schrock@Sun.COM &l2cache, NULL) != NULL && avail_spare && 23047326SEric.Schrock@Sun.COM is_replacing_spare(config_root, tgt, 0)) { 23052082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23062082Seschrock "device has already been replaced with a spare")); 23077041Seschrock free(newname); 23082082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 23092082Seschrock } 2310789Sahrens 23117041Seschrock free(newname); 23127041Seschrock 23135094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 23142082Seschrock return (-1); 2315789Sahrens 23164543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2317789Sahrens 23182676Seschrock zcmd_free_nvlists(&zc); 2319789Sahrens 23207965SGeorge.Wilson@Sun.COM if (ret == 0) { 23217965SGeorge.Wilson@Sun.COM if (rootpool) { 23227965SGeorge.Wilson@Sun.COM /* 23237965SGeorge.Wilson@Sun.COM * XXX - This should be removed once we can 23247965SGeorge.Wilson@Sun.COM * automatically install the bootblocks on the 23257965SGeorge.Wilson@Sun.COM * newly attached disk. 23267965SGeorge.Wilson@Sun.COM */ 23277965SGeorge.Wilson@Sun.COM (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please " 23287965SGeorge.Wilson@Sun.COM "be sure to invoke %s to make '%s' bootable.\n"), 23297965SGeorge.Wilson@Sun.COM BOOTCMD, new_disk); 23309790SLin.Ling@Sun.COM 23319790SLin.Ling@Sun.COM /* 23329790SLin.Ling@Sun.COM * XXX need a better way to prevent user from 23339790SLin.Ling@Sun.COM * booting up a half-baked vdev. 23349790SLin.Ling@Sun.COM */ 23359790SLin.Ling@Sun.COM (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 23369790SLin.Ling@Sun.COM "sure to wait until resilver is done " 23379790SLin.Ling@Sun.COM "before rebooting.\n")); 23387965SGeorge.Wilson@Sun.COM } 2339789Sahrens return (0); 23407965SGeorge.Wilson@Sun.COM } 2341789Sahrens 2342789Sahrens switch (errno) { 23431544Seschrock case ENOTSUP: 2344789Sahrens /* 2345789Sahrens * Can't attach to or replace this type of vdev. 2346789Sahrens */ 23474527Sperrin if (replacing) { 23487326SEric.Schrock@Sun.COM if (islog) 23494527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23504527Sperrin "cannot replace a log with a spare")); 23514527Sperrin else 23524527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23534527Sperrin "cannot replace a replacing device")); 23544527Sperrin } else { 23552082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23562082Seschrock "can only attach to mirrors and top-level " 23572082Seschrock "disks")); 23584527Sperrin } 23592082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2360789Sahrens break; 2361789Sahrens 23621544Seschrock case EINVAL: 2363789Sahrens /* 2364789Sahrens * The new device must be a single disk. 2365789Sahrens */ 23662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23672082Seschrock "new device must be a single disk")); 23682082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2369789Sahrens break; 2370789Sahrens 23711544Seschrock case EBUSY: 23722082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 23732082Seschrock new_disk); 23742082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2375789Sahrens break; 2376789Sahrens 23771544Seschrock case EOVERFLOW: 2378789Sahrens /* 2379789Sahrens * The new device is too small. 2380789Sahrens */ 23812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23822082Seschrock "device is too small")); 23832082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2384789Sahrens break; 2385789Sahrens 23861544Seschrock case EDOM: 2387789Sahrens /* 2388789Sahrens * The new device has a different alignment requirement. 2389789Sahrens */ 23902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23912082Seschrock "devices have different sector alignment")); 23922082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2393789Sahrens break; 2394789Sahrens 23951544Seschrock case ENAMETOOLONG: 2396789Sahrens /* 2397789Sahrens * The resulting top-level vdev spec won't fit in the label. 2398789Sahrens */ 23992082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2400789Sahrens break; 2401789Sahrens 24021544Seschrock default: 24032082Seschrock (void) zpool_standard_error(hdl, errno, msg); 2404789Sahrens } 2405789Sahrens 24062082Seschrock return (-1); 2407789Sahrens } 2408789Sahrens 2409789Sahrens /* 2410789Sahrens * Detach the specified device. 2411789Sahrens */ 2412789Sahrens int 2413789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2414789Sahrens { 2415789Sahrens zfs_cmd_t zc = { 0 }; 2416789Sahrens char msg[1024]; 24172082Seschrock nvlist_t *tgt; 24185450Sbrendan boolean_t avail_spare, l2cache; 24192082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2420789Sahrens 24211544Seschrock (void) snprintf(msg, sizeof (msg), 24221544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 24231544Seschrock 2424789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 24257326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 24267326SEric.Schrock@Sun.COM NULL)) == 0) 24272082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2428789Sahrens 24292468Sek110237 if (avail_spare) 24302082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 24312082Seschrock 24325450Sbrendan if (l2cache) 24335450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 24345450Sbrendan 24352082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 24362082Seschrock 24374543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2438789Sahrens return (0); 2439789Sahrens 2440789Sahrens switch (errno) { 2441789Sahrens 24421544Seschrock case ENOTSUP: 2443789Sahrens /* 2444789Sahrens * Can't detach from this type of vdev. 2445789Sahrens */ 24462082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 24472082Seschrock "applicable to mirror and replacing vdevs")); 24482082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 2449789Sahrens break; 2450789Sahrens 24511544Seschrock case EBUSY: 2452789Sahrens /* 2453789Sahrens * There are no other replicas of this device. 2454789Sahrens */ 24552082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2456789Sahrens break; 2457789Sahrens 24581544Seschrock default: 24592082Seschrock (void) zpool_standard_error(hdl, errno, msg); 24601544Seschrock } 24611544Seschrock 24622082Seschrock return (-1); 24632082Seschrock } 24642082Seschrock 24652082Seschrock /* 246611422SMark.Musante@Sun.COM * Find a mirror vdev in the source nvlist. 246711422SMark.Musante@Sun.COM * 246811422SMark.Musante@Sun.COM * The mchild array contains a list of disks in one of the top-level mirrors 246911422SMark.Musante@Sun.COM * of the source pool. The schild array contains a list of disks that the 247011422SMark.Musante@Sun.COM * user specified on the command line. We loop over the mchild array to 247111422SMark.Musante@Sun.COM * see if any entry in the schild array matches. 247211422SMark.Musante@Sun.COM * 247311422SMark.Musante@Sun.COM * If a disk in the mchild array is found in the schild array, we return 247411422SMark.Musante@Sun.COM * the index of that entry. Otherwise we return -1. 247511422SMark.Musante@Sun.COM */ 247611422SMark.Musante@Sun.COM static int 247711422SMark.Musante@Sun.COM find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 247811422SMark.Musante@Sun.COM nvlist_t **schild, uint_t schildren) 247911422SMark.Musante@Sun.COM { 248011422SMark.Musante@Sun.COM uint_t mc; 248111422SMark.Musante@Sun.COM 248211422SMark.Musante@Sun.COM for (mc = 0; mc < mchildren; mc++) { 248311422SMark.Musante@Sun.COM uint_t sc; 248411422SMark.Musante@Sun.COM char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 248511422SMark.Musante@Sun.COM mchild[mc], B_FALSE); 248611422SMark.Musante@Sun.COM 248711422SMark.Musante@Sun.COM for (sc = 0; sc < schildren; sc++) { 248811422SMark.Musante@Sun.COM char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 248911422SMark.Musante@Sun.COM schild[sc], B_FALSE); 249011422SMark.Musante@Sun.COM boolean_t result = (strcmp(mpath, spath) == 0); 249111422SMark.Musante@Sun.COM 249211422SMark.Musante@Sun.COM free(spath); 249311422SMark.Musante@Sun.COM if (result) { 249411422SMark.Musante@Sun.COM free(mpath); 249511422SMark.Musante@Sun.COM return (mc); 249611422SMark.Musante@Sun.COM } 249711422SMark.Musante@Sun.COM } 249811422SMark.Musante@Sun.COM 249911422SMark.Musante@Sun.COM free(mpath); 250011422SMark.Musante@Sun.COM } 250111422SMark.Musante@Sun.COM 250211422SMark.Musante@Sun.COM return (-1); 250311422SMark.Musante@Sun.COM } 250411422SMark.Musante@Sun.COM 250511422SMark.Musante@Sun.COM /* 250611422SMark.Musante@Sun.COM * Split a mirror pool. If newroot points to null, then a new nvlist 250711422SMark.Musante@Sun.COM * is generated and it is the responsibility of the caller to free it. 250811422SMark.Musante@Sun.COM */ 250911422SMark.Musante@Sun.COM int 251011422SMark.Musante@Sun.COM zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 251111422SMark.Musante@Sun.COM nvlist_t *props, splitflags_t flags) 251211422SMark.Musante@Sun.COM { 251311422SMark.Musante@Sun.COM zfs_cmd_t zc = { 0 }; 251411422SMark.Musante@Sun.COM char msg[1024]; 251511422SMark.Musante@Sun.COM nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 251611422SMark.Musante@Sun.COM nvlist_t **varray = NULL, *zc_props = NULL; 251711422SMark.Musante@Sun.COM uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 251811422SMark.Musante@Sun.COM libzfs_handle_t *hdl = zhp->zpool_hdl; 251911422SMark.Musante@Sun.COM uint64_t vers; 252011422SMark.Musante@Sun.COM boolean_t freelist = B_FALSE, memory_err = B_TRUE; 252111422SMark.Musante@Sun.COM int retval = 0; 252211422SMark.Musante@Sun.COM 252311422SMark.Musante@Sun.COM (void) snprintf(msg, sizeof (msg), 252411422SMark.Musante@Sun.COM dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 252511422SMark.Musante@Sun.COM 252611422SMark.Musante@Sun.COM if (!zpool_name_valid(hdl, B_FALSE, newname)) 252711422SMark.Musante@Sun.COM return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 252811422SMark.Musante@Sun.COM 252911422SMark.Musante@Sun.COM if ((config = zpool_get_config(zhp, NULL)) == NULL) { 253011422SMark.Musante@Sun.COM (void) fprintf(stderr, gettext("Internal error: unable to " 253111422SMark.Musante@Sun.COM "retrieve pool configuration\n")); 253211422SMark.Musante@Sun.COM return (-1); 253311422SMark.Musante@Sun.COM } 253411422SMark.Musante@Sun.COM 253511422SMark.Musante@Sun.COM verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 253611422SMark.Musante@Sun.COM == 0); 253711422SMark.Musante@Sun.COM verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 253811422SMark.Musante@Sun.COM 253911422SMark.Musante@Sun.COM if (props) { 254011422SMark.Musante@Sun.COM if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 254111422SMark.Musante@Sun.COM props, vers, B_TRUE, msg)) == NULL) 254211422SMark.Musante@Sun.COM return (-1); 254311422SMark.Musante@Sun.COM } 254411422SMark.Musante@Sun.COM 254511422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 254611422SMark.Musante@Sun.COM &children) != 0) { 254711422SMark.Musante@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 254811422SMark.Musante@Sun.COM "Source pool is missing vdev tree")); 254911422SMark.Musante@Sun.COM if (zc_props) 255011422SMark.Musante@Sun.COM nvlist_free(zc_props); 255111422SMark.Musante@Sun.COM return (-1); 255211422SMark.Musante@Sun.COM } 255311422SMark.Musante@Sun.COM 255411422SMark.Musante@Sun.COM varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 255511422SMark.Musante@Sun.COM vcount = 0; 255611422SMark.Musante@Sun.COM 255711422SMark.Musante@Sun.COM if (*newroot == NULL || 255811422SMark.Musante@Sun.COM nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 255911422SMark.Musante@Sun.COM &newchild, &newchildren) != 0) 256011422SMark.Musante@Sun.COM newchildren = 0; 256111422SMark.Musante@Sun.COM 256211422SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 256311422SMark.Musante@Sun.COM uint64_t is_log = B_FALSE, is_hole = B_FALSE; 256411422SMark.Musante@Sun.COM char *type; 256511422SMark.Musante@Sun.COM nvlist_t **mchild, *vdev; 256611422SMark.Musante@Sun.COM uint_t mchildren; 256711422SMark.Musante@Sun.COM int entry; 256811422SMark.Musante@Sun.COM 256911422SMark.Musante@Sun.COM /* 257011422SMark.Musante@Sun.COM * Unlike cache & spares, slogs are stored in the 257111422SMark.Musante@Sun.COM * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 257211422SMark.Musante@Sun.COM */ 257311422SMark.Musante@Sun.COM (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 257411422SMark.Musante@Sun.COM &is_log); 257511422SMark.Musante@Sun.COM (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 257611422SMark.Musante@Sun.COM &is_hole); 257711422SMark.Musante@Sun.COM if (is_log || is_hole) { 257811422SMark.Musante@Sun.COM /* 257911422SMark.Musante@Sun.COM * Create a hole vdev and put it in the config. 258011422SMark.Musante@Sun.COM */ 258111422SMark.Musante@Sun.COM if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 258211422SMark.Musante@Sun.COM goto out; 258311422SMark.Musante@Sun.COM if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 258411422SMark.Musante@Sun.COM VDEV_TYPE_HOLE) != 0) 258511422SMark.Musante@Sun.COM goto out; 258611422SMark.Musante@Sun.COM if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 258711422SMark.Musante@Sun.COM 1) != 0) 258811422SMark.Musante@Sun.COM goto out; 258911422SMark.Musante@Sun.COM if (lastlog == 0) 259011422SMark.Musante@Sun.COM lastlog = vcount; 259111422SMark.Musante@Sun.COM varray[vcount++] = vdev; 259211422SMark.Musante@Sun.COM continue; 259311422SMark.Musante@Sun.COM } 259411422SMark.Musante@Sun.COM lastlog = 0; 259511422SMark.Musante@Sun.COM verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 259611422SMark.Musante@Sun.COM == 0); 259711422SMark.Musante@Sun.COM if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 259811422SMark.Musante@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 259911422SMark.Musante@Sun.COM "Source pool must be composed only of mirrors\n")); 260011422SMark.Musante@Sun.COM retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 260111422SMark.Musante@Sun.COM goto out; 260211422SMark.Musante@Sun.COM } 260311422SMark.Musante@Sun.COM 260411422SMark.Musante@Sun.COM verify(nvlist_lookup_nvlist_array(child[c], 260511422SMark.Musante@Sun.COM ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 260611422SMark.Musante@Sun.COM 260711422SMark.Musante@Sun.COM /* find or add an entry for this top-level vdev */ 260811422SMark.Musante@Sun.COM if (newchildren > 0 && 260911422SMark.Musante@Sun.COM (entry = find_vdev_entry(zhp, mchild, mchildren, 261011422SMark.Musante@Sun.COM newchild, newchildren)) >= 0) { 261111422SMark.Musante@Sun.COM /* We found a disk that the user specified. */ 261211422SMark.Musante@Sun.COM vdev = mchild[entry]; 261311422SMark.Musante@Sun.COM ++found; 261411422SMark.Musante@Sun.COM } else { 261511422SMark.Musante@Sun.COM /* User didn't specify a disk for this vdev. */ 261611422SMark.Musante@Sun.COM vdev = mchild[mchildren - 1]; 261711422SMark.Musante@Sun.COM } 261811422SMark.Musante@Sun.COM 261911422SMark.Musante@Sun.COM if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 262011422SMark.Musante@Sun.COM goto out; 262111422SMark.Musante@Sun.COM } 262211422SMark.Musante@Sun.COM 262311422SMark.Musante@Sun.COM /* did we find every disk the user specified? */ 262411422SMark.Musante@Sun.COM if (found != newchildren) { 262511422SMark.Musante@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 262611422SMark.Musante@Sun.COM "include at most one disk from each mirror")); 262711422SMark.Musante@Sun.COM retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 262811422SMark.Musante@Sun.COM goto out; 262911422SMark.Musante@Sun.COM } 263011422SMark.Musante@Sun.COM 263111422SMark.Musante@Sun.COM /* Prepare the nvlist for populating. */ 263211422SMark.Musante@Sun.COM if (*newroot == NULL) { 263311422SMark.Musante@Sun.COM if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 263411422SMark.Musante@Sun.COM goto out; 263511422SMark.Musante@Sun.COM freelist = B_TRUE; 263611422SMark.Musante@Sun.COM if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 263711422SMark.Musante@Sun.COM VDEV_TYPE_ROOT) != 0) 263811422SMark.Musante@Sun.COM goto out; 263911422SMark.Musante@Sun.COM } else { 264011422SMark.Musante@Sun.COM verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 264111422SMark.Musante@Sun.COM } 264211422SMark.Musante@Sun.COM 264311422SMark.Musante@Sun.COM /* Add all the children we found */ 264411422SMark.Musante@Sun.COM if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 264511422SMark.Musante@Sun.COM lastlog == 0 ? vcount : lastlog) != 0) 264611422SMark.Musante@Sun.COM goto out; 264711422SMark.Musante@Sun.COM 264811422SMark.Musante@Sun.COM /* 264911422SMark.Musante@Sun.COM * If we're just doing a dry run, exit now with success. 265011422SMark.Musante@Sun.COM */ 265111422SMark.Musante@Sun.COM if (flags.dryrun) { 265211422SMark.Musante@Sun.COM memory_err = B_FALSE; 265311422SMark.Musante@Sun.COM freelist = B_FALSE; 265411422SMark.Musante@Sun.COM goto out; 265511422SMark.Musante@Sun.COM } 265611422SMark.Musante@Sun.COM 265711422SMark.Musante@Sun.COM /* now build up the config list & call the ioctl */ 265811422SMark.Musante@Sun.COM if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 265911422SMark.Musante@Sun.COM goto out; 266011422SMark.Musante@Sun.COM 266111422SMark.Musante@Sun.COM if (nvlist_add_nvlist(newconfig, 266211422SMark.Musante@Sun.COM ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 266311422SMark.Musante@Sun.COM nvlist_add_string(newconfig, 266411422SMark.Musante@Sun.COM ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 266511422SMark.Musante@Sun.COM nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 266611422SMark.Musante@Sun.COM goto out; 266711422SMark.Musante@Sun.COM 266811422SMark.Musante@Sun.COM /* 266911422SMark.Musante@Sun.COM * The new pool is automatically part of the namespace unless we 267011422SMark.Musante@Sun.COM * explicitly export it. 267111422SMark.Musante@Sun.COM */ 267211422SMark.Musante@Sun.COM if (!flags.import) 267311422SMark.Musante@Sun.COM zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 267411422SMark.Musante@Sun.COM (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 267511422SMark.Musante@Sun.COM (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 267611422SMark.Musante@Sun.COM if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 267711422SMark.Musante@Sun.COM goto out; 267811422SMark.Musante@Sun.COM if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 267911422SMark.Musante@Sun.COM goto out; 268011422SMark.Musante@Sun.COM 268111422SMark.Musante@Sun.COM if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 268211422SMark.Musante@Sun.COM retval = zpool_standard_error(hdl, errno, msg); 268311422SMark.Musante@Sun.COM goto out; 268411422SMark.Musante@Sun.COM } 268511422SMark.Musante@Sun.COM 268611422SMark.Musante@Sun.COM freelist = B_FALSE; 268711422SMark.Musante@Sun.COM memory_err = B_FALSE; 268811422SMark.Musante@Sun.COM 268911422SMark.Musante@Sun.COM out: 269011422SMark.Musante@Sun.COM if (varray != NULL) { 269111422SMark.Musante@Sun.COM int v; 269211422SMark.Musante@Sun.COM 269311422SMark.Musante@Sun.COM for (v = 0; v < vcount; v++) 269411422SMark.Musante@Sun.COM nvlist_free(varray[v]); 269511422SMark.Musante@Sun.COM free(varray); 269611422SMark.Musante@Sun.COM } 269711422SMark.Musante@Sun.COM zcmd_free_nvlists(&zc); 269811422SMark.Musante@Sun.COM if (zc_props) 269911422SMark.Musante@Sun.COM nvlist_free(zc_props); 270011422SMark.Musante@Sun.COM if (newconfig) 270111422SMark.Musante@Sun.COM nvlist_free(newconfig); 270211422SMark.Musante@Sun.COM if (freelist) { 270311422SMark.Musante@Sun.COM nvlist_free(*newroot); 270411422SMark.Musante@Sun.COM *newroot = NULL; 270511422SMark.Musante@Sun.COM } 270611422SMark.Musante@Sun.COM 270711422SMark.Musante@Sun.COM if (retval != 0) 270811422SMark.Musante@Sun.COM return (retval); 270911422SMark.Musante@Sun.COM 271011422SMark.Musante@Sun.COM if (memory_err) 271111422SMark.Musante@Sun.COM return (no_memory(hdl)); 271211422SMark.Musante@Sun.COM 271311422SMark.Musante@Sun.COM return (0); 271411422SMark.Musante@Sun.COM } 271511422SMark.Musante@Sun.COM 271611422SMark.Musante@Sun.COM /* 27175450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 27185450Sbrendan * and level 2 cache devices. 27192082Seschrock */ 27202082Seschrock int 27212082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 27222082Seschrock { 27232082Seschrock zfs_cmd_t zc = { 0 }; 27242082Seschrock char msg[1024]; 27252082Seschrock nvlist_t *tgt; 272610594SGeorge.Wilson@Sun.COM boolean_t avail_spare, l2cache, islog; 27272082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 272810594SGeorge.Wilson@Sun.COM uint64_t version; 27292082Seschrock 27302082Seschrock (void) snprintf(msg, sizeof (msg), 27312082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 27322082Seschrock 27332082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 27347326SEric.Schrock@Sun.COM if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 273510594SGeorge.Wilson@Sun.COM &islog)) == 0) 27362082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 273710594SGeorge.Wilson@Sun.COM /* 273810594SGeorge.Wilson@Sun.COM * XXX - this should just go away. 273910594SGeorge.Wilson@Sun.COM */ 274010594SGeorge.Wilson@Sun.COM if (!avail_spare && !l2cache && !islog) { 27412082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 274210594SGeorge.Wilson@Sun.COM "only inactive hot spares, cache, top-level, " 274310594SGeorge.Wilson@Sun.COM "or log devices can be removed")); 27442082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 27452082Seschrock } 27462082Seschrock 274710594SGeorge.Wilson@Sun.COM version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 274810594SGeorge.Wilson@Sun.COM if (islog && version < SPA_VERSION_HOLES) { 274910594SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 275010594SGeorge.Wilson@Sun.COM "pool must be upgrade to support log removal")); 275110594SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_BADVERSION, msg)); 275210594SGeorge.Wilson@Sun.COM } 275310594SGeorge.Wilson@Sun.COM 27542082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 27552082Seschrock 27564543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 27572082Seschrock return (0); 27582082Seschrock 27592082Seschrock return (zpool_standard_error(hdl, errno, msg)); 27601544Seschrock } 27611544Seschrock 27621544Seschrock /* 27631544Seschrock * Clear the errors for the pool, or the particular device if specified. 27641544Seschrock */ 27651544Seschrock int 276610921STim.Haley@Sun.COM zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 27671544Seschrock { 27681544Seschrock zfs_cmd_t zc = { 0 }; 27691544Seschrock char msg[1024]; 27702082Seschrock nvlist_t *tgt; 277110921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 27725450Sbrendan boolean_t avail_spare, l2cache; 27732082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 277410921STim.Haley@Sun.COM nvlist_t *nvi = NULL; 27751544Seschrock 27761544Seschrock if (path) 27771544Seschrock (void) snprintf(msg, sizeof (msg), 27781544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 27792676Seschrock path); 27801544Seschrock else 27811544Seschrock (void) snprintf(msg, sizeof (msg), 27821544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 27831544Seschrock zhp->zpool_name); 27841544Seschrock 27851544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 27862082Seschrock if (path) { 27875450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 27887326SEric.Schrock@Sun.COM &l2cache, NULL)) == 0) 27892082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 27902082Seschrock 27915450Sbrendan /* 27925450Sbrendan * Don't allow error clearing for hot spares. Do allow 27935450Sbrendan * error clearing for l2cache devices. 27945450Sbrendan */ 27952468Sek110237 if (avail_spare) 27962082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 27972082Seschrock 27982082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 27992082Seschrock &zc.zc_guid) == 0); 28001544Seschrock } 28011544Seschrock 280210921STim.Haley@Sun.COM zpool_get_rewind_policy(rewindnvl, &policy); 280310921STim.Haley@Sun.COM zc.zc_cookie = policy.zrp_request; 280410921STim.Haley@Sun.COM 280510921STim.Haley@Sun.COM if (zcmd_alloc_dst_nvlist(hdl, &zc, 8192) != 0) 280610921STim.Haley@Sun.COM return (-1); 280710921STim.Haley@Sun.COM 280810921STim.Haley@Sun.COM if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, rewindnvl) != 0) 280910921STim.Haley@Sun.COM return (-1); 281010921STim.Haley@Sun.COM 281110921STim.Haley@Sun.COM if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0 || 281210921STim.Haley@Sun.COM ((policy.zrp_request & ZPOOL_TRY_REWIND) && 281310921STim.Haley@Sun.COM errno != EPERM && errno != EACCES)) { 281410921STim.Haley@Sun.COM if (policy.zrp_request & 281510921STim.Haley@Sun.COM (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 281610921STim.Haley@Sun.COM (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 281710921STim.Haley@Sun.COM zpool_rewind_exclaim(hdl, zc.zc_name, 281810921STim.Haley@Sun.COM ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 281910921STim.Haley@Sun.COM nvi); 282010921STim.Haley@Sun.COM nvlist_free(nvi); 282110921STim.Haley@Sun.COM } 282210921STim.Haley@Sun.COM zcmd_free_nvlists(&zc); 28231544Seschrock return (0); 282410921STim.Haley@Sun.COM } 282510921STim.Haley@Sun.COM 282610921STim.Haley@Sun.COM zcmd_free_nvlists(&zc); 28272082Seschrock return (zpool_standard_error(hdl, errno, msg)); 2828789Sahrens } 2829789Sahrens 28303126Sahl /* 28314451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 28324451Seschrock */ 28334451Seschrock int 28344451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 28354451Seschrock { 28364451Seschrock zfs_cmd_t zc = { 0 }; 28374451Seschrock char msg[1024]; 28384451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 28394451Seschrock 28404451Seschrock (void) snprintf(msg, sizeof (msg), 28414451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 28424451Seschrock guid); 28434451Seschrock 28444451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 28454451Seschrock zc.zc_guid = guid; 28464451Seschrock 28474451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 28484451Seschrock return (0); 28494451Seschrock 28504451Seschrock return (zpool_standard_error(hdl, errno, msg)); 28514451Seschrock } 28524451Seschrock 28534451Seschrock /* 28541354Seschrock * Convert from a devid string to a path. 28551354Seschrock */ 28561354Seschrock static char * 28571354Seschrock devid_to_path(char *devid_str) 28581354Seschrock { 28591354Seschrock ddi_devid_t devid; 28601354Seschrock char *minor; 28611354Seschrock char *path; 28621354Seschrock devid_nmlist_t *list = NULL; 28631354Seschrock int ret; 28641354Seschrock 28651354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 28661354Seschrock return (NULL); 28671354Seschrock 28681354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 28691354Seschrock 28701354Seschrock devid_str_free(minor); 28711354Seschrock devid_free(devid); 28721354Seschrock 28731354Seschrock if (ret != 0) 28741354Seschrock return (NULL); 28751354Seschrock 28762082Seschrock if ((path = strdup(list[0].devname)) == NULL) 28772082Seschrock return (NULL); 28782082Seschrock 28791354Seschrock devid_free_nmlist(list); 28801354Seschrock 28811354Seschrock return (path); 28821354Seschrock } 28831354Seschrock 28841354Seschrock /* 28851354Seschrock * Convert from a path to a devid string. 28861354Seschrock */ 28871354Seschrock static char * 28881354Seschrock path_to_devid(const char *path) 28891354Seschrock { 28901354Seschrock int fd; 28911354Seschrock ddi_devid_t devid; 28921354Seschrock char *minor, *ret; 28931354Seschrock 28941354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 28951354Seschrock return (NULL); 28961354Seschrock 28971354Seschrock minor = NULL; 28981354Seschrock ret = NULL; 28991354Seschrock if (devid_get(fd, &devid) == 0) { 29001354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 29011354Seschrock ret = devid_str_encode(devid, minor); 29021354Seschrock if (minor != NULL) 29031354Seschrock devid_str_free(minor); 29041354Seschrock devid_free(devid); 29051354Seschrock } 29061354Seschrock (void) close(fd); 29071354Seschrock 29081354Seschrock return (ret); 29091354Seschrock } 29101354Seschrock 29111354Seschrock /* 29121354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 29131354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 29141354Seschrock * type 'zpool status', and we'll display the correct information anyway. 29151354Seschrock */ 29161354Seschrock static void 29171354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 29181354Seschrock { 29191354Seschrock zfs_cmd_t zc = { 0 }; 29201354Seschrock 29211354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 29222676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 29231354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 29241544Seschrock &zc.zc_guid) == 0); 29251354Seschrock 29262082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 29271354Seschrock } 29281354Seschrock 29291354Seschrock /* 29301354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 29311354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 29321354Seschrock * We also check if this is a whole disk, in which case we strip off the 29331354Seschrock * trailing 's0' slice name. 29341354Seschrock * 29351354Seschrock * This routine is also responsible for identifying when disks have been 29361354Seschrock * reconfigured in a new location. The kernel will have opened the device by 29371354Seschrock * devid, but the path will still refer to the old location. To catch this, we 29381354Seschrock * first do a path -> devid translation (which is fast for the common case). If 29391354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 29401354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 29411354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 29421354Seschrock * of these checks. 29431354Seschrock */ 29441354Seschrock char * 294510594SGeorge.Wilson@Sun.COM zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 294610594SGeorge.Wilson@Sun.COM boolean_t verbose) 29471354Seschrock { 29481354Seschrock char *path, *devid; 29491544Seschrock uint64_t value; 29501544Seschrock char buf[64]; 29514451Seschrock vdev_stat_t *vs; 29524451Seschrock uint_t vsc; 29531354Seschrock 29541544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 29551544Seschrock &value) == 0) { 29561544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 29571544Seschrock &value) == 0); 29582856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 29592856Snd150628 (u_longlong_t)value); 29601544Seschrock path = buf; 29611544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 29621354Seschrock 29634451Seschrock /* 29644451Seschrock * If the device is dead (faulted, offline, etc) then don't 29654451Seschrock * bother opening it. Otherwise we may be forcing the user to 29664451Seschrock * open a misbehaving device, which can have undesirable 29674451Seschrock * effects. 29684451Seschrock */ 2969*12296SLin.Ling@Sun.COM if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 29704451Seschrock (uint64_t **)&vs, &vsc) != 0 || 29714451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 29724451Seschrock zhp != NULL && 29731354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 29741354Seschrock /* 29751354Seschrock * Determine if the current path is correct. 29761354Seschrock */ 29771354Seschrock char *newdevid = path_to_devid(path); 29781354Seschrock 29791354Seschrock if (newdevid == NULL || 29801354Seschrock strcmp(devid, newdevid) != 0) { 29811354Seschrock char *newpath; 29821354Seschrock 29831354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 29841354Seschrock /* 29851354Seschrock * Update the path appropriately. 29861354Seschrock */ 29871354Seschrock set_path(zhp, nv, newpath); 29882082Seschrock if (nvlist_add_string(nv, 29892082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 29902082Seschrock verify(nvlist_lookup_string(nv, 29912082Seschrock ZPOOL_CONFIG_PATH, 29922082Seschrock &path) == 0); 29931354Seschrock free(newpath); 29941354Seschrock } 29951354Seschrock } 29961354Seschrock 29972082Seschrock if (newdevid) 29982082Seschrock devid_str_free(newdevid); 29991354Seschrock } 30001354Seschrock 30011354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 30021354Seschrock path += 9; 30031354Seschrock 30041354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 30051544Seschrock &value) == 0 && value) { 30062082Seschrock char *tmp = zfs_strdup(hdl, path); 30072082Seschrock if (tmp == NULL) 30082082Seschrock return (NULL); 30091354Seschrock tmp[strlen(path) - 2] = '\0'; 30101354Seschrock return (tmp); 30111354Seschrock } 30121354Seschrock } else { 30131354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 30142082Seschrock 30152082Seschrock /* 30162082Seschrock * If it's a raidz device, we need to stick in the parity level. 30172082Seschrock */ 30182082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 30192082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 30202082Seschrock &value) == 0); 30212082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 30222856Snd150628 (u_longlong_t)value); 30232082Seschrock path = buf; 30242082Seschrock } 302510594SGeorge.Wilson@Sun.COM 302610594SGeorge.Wilson@Sun.COM /* 302710594SGeorge.Wilson@Sun.COM * We identify each top-level vdev by using a <type-id> 302810594SGeorge.Wilson@Sun.COM * naming convention. 302910594SGeorge.Wilson@Sun.COM */ 303010594SGeorge.Wilson@Sun.COM if (verbose) { 303110594SGeorge.Wilson@Sun.COM uint64_t id; 303210594SGeorge.Wilson@Sun.COM 303310594SGeorge.Wilson@Sun.COM verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 303410594SGeorge.Wilson@Sun.COM &id) == 0); 303510594SGeorge.Wilson@Sun.COM (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 303610594SGeorge.Wilson@Sun.COM (u_longlong_t)id); 303710594SGeorge.Wilson@Sun.COM path = buf; 303810594SGeorge.Wilson@Sun.COM } 30391354Seschrock } 30401354Seschrock 30412082Seschrock return (zfs_strdup(hdl, path)); 30421354Seschrock } 30431544Seschrock 30441544Seschrock static int 30451544Seschrock zbookmark_compare(const void *a, const void *b) 30461544Seschrock { 30471544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 30481544Seschrock } 30491544Seschrock 30501544Seschrock /* 30511544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 30521544Seschrock * caller. 30531544Seschrock */ 30541544Seschrock int 30553444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 30561544Seschrock { 30571544Seschrock zfs_cmd_t zc = { 0 }; 30581544Seschrock uint64_t count; 30592676Seschrock zbookmark_t *zb = NULL; 30603444Sek110237 int i; 30611544Seschrock 30621544Seschrock /* 30631544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 30641544Seschrock * has increased, allocate more space and continue until we get the 30651544Seschrock * entire list. 30661544Seschrock */ 30671544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 30681544Seschrock &count) == 0); 30694820Sek110237 if (count == 0) 30704820Sek110237 return (0); 30712676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 30722856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 30732082Seschrock return (-1); 30742676Seschrock zc.zc_nvlist_dst_size = count; 30751544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 30761544Seschrock for (;;) { 30772082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 30782082Seschrock &zc) != 0) { 30792676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 30801544Seschrock if (errno == ENOMEM) { 30813823Svb160487 count = zc.zc_nvlist_dst_size; 30822676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 30833823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 30843823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 30852082Seschrock return (-1); 30861544Seschrock } else { 30871544Seschrock return (-1); 30881544Seschrock } 30891544Seschrock } else { 30901544Seschrock break; 30911544Seschrock } 30921544Seschrock } 30931544Seschrock 30941544Seschrock /* 30951544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 30961544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 30972676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 30981544Seschrock * _not_ copied as part of the process. So we point the start of our 30991544Seschrock * array appropriate and decrement the total number of elements. 31001544Seschrock */ 31012676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 31022676Seschrock zc.zc_nvlist_dst_size; 31032676Seschrock count -= zc.zc_nvlist_dst_size; 31041544Seschrock 31051544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 31061544Seschrock 31073444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 31081544Seschrock 31091544Seschrock /* 31103444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 31111544Seschrock */ 31121544Seschrock for (i = 0; i < count; i++) { 31131544Seschrock nvlist_t *nv; 31141544Seschrock 31153700Sek110237 /* ignoring zb_blkid and zb_level for now */ 31163700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 31173700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 31181544Seschrock continue; 31191544Seschrock 31203444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 31213444Sek110237 goto nomem; 31223444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 31233444Sek110237 zb[i].zb_objset) != 0) { 31243444Sek110237 nvlist_free(nv); 31252082Seschrock goto nomem; 31263444Sek110237 } 31273444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 31283444Sek110237 zb[i].zb_object) != 0) { 31293444Sek110237 nvlist_free(nv); 31303444Sek110237 goto nomem; 31311544Seschrock } 31323444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 31333444Sek110237 nvlist_free(nv); 31343444Sek110237 goto nomem; 31353444Sek110237 } 31363444Sek110237 nvlist_free(nv); 31371544Seschrock } 31381544Seschrock 31393265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 31401544Seschrock return (0); 31412082Seschrock 31422082Seschrock nomem: 31432676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 31442082Seschrock return (no_memory(zhp->zpool_hdl)); 31451544Seschrock } 31461760Seschrock 31471760Seschrock /* 31481760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 31491760Seschrock */ 31501760Seschrock int 31515094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 31521760Seschrock { 31531760Seschrock zfs_cmd_t zc = { 0 }; 31542082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 31551760Seschrock 31561760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 31575094Slling zc.zc_cookie = new_version; 31585094Slling 31594543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 31603237Slling return (zpool_standard_error_fmt(hdl, errno, 31612082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 31622082Seschrock zhp->zpool_name)); 31631760Seschrock return (0); 31641760Seschrock } 31652926Sek110237 31664988Sek110237 void 31674988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 31684988Sek110237 char *history_str) 31694988Sek110237 { 31704988Sek110237 int i; 31714988Sek110237 31724988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 31734988Sek110237 for (i = 1; i < argc; i++) { 31744988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 31754988Sek110237 HIS_MAX_RECORD_LEN) 31764988Sek110237 break; 31774988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 31784988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 31794988Sek110237 } 31804988Sek110237 } 31814988Sek110237 31822926Sek110237 /* 31834988Sek110237 * Stage command history for logging. 31842926Sek110237 */ 31854988Sek110237 int 31864988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 31872926Sek110237 { 31884988Sek110237 if (history_str == NULL) 31894988Sek110237 return (EINVAL); 31904988Sek110237 31914988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 31924988Sek110237 return (EINVAL); 31932926Sek110237 31944715Sek110237 if (hdl->libzfs_log_str != NULL) 31954543Smarks free(hdl->libzfs_log_str); 31962926Sek110237 31974988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 31984988Sek110237 return (no_memory(hdl)); 31994543Smarks 32004988Sek110237 return (0); 32012926Sek110237 } 32022926Sek110237 32032926Sek110237 /* 32042926Sek110237 * Perform ioctl to get some command history of a pool. 32052926Sek110237 * 32062926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 32072926Sek110237 * logical offset of the history buffer to start reading from. 32082926Sek110237 * 32092926Sek110237 * Upon return, 'off' is the next logical offset to read from and 32102926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 32112926Sek110237 */ 32122926Sek110237 static int 32132926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 32142926Sek110237 { 32152926Sek110237 zfs_cmd_t zc = { 0 }; 32162926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 32172926Sek110237 32182926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 32192926Sek110237 32202926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 32212926Sek110237 zc.zc_history_len = *len; 32222926Sek110237 zc.zc_history_offset = *off; 32232926Sek110237 32242926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 32252926Sek110237 switch (errno) { 32262926Sek110237 case EPERM: 32273237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 32283237Slling dgettext(TEXT_DOMAIN, 32292926Sek110237 "cannot show history for pool '%s'"), 32302926Sek110237 zhp->zpool_name)); 32312926Sek110237 case ENOENT: 32323237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 32332926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 32342926Sek110237 "'%s'"), zhp->zpool_name)); 32353863Sek110237 case ENOTSUP: 32363863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 32373863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 32383863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 32392926Sek110237 default: 32403237Slling return (zpool_standard_error_fmt(hdl, errno, 32412926Sek110237 dgettext(TEXT_DOMAIN, 32422926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 32432926Sek110237 } 32442926Sek110237 } 32452926Sek110237 32462926Sek110237 *len = zc.zc_history_len; 32472926Sek110237 *off = zc.zc_history_offset; 32482926Sek110237 32492926Sek110237 return (0); 32502926Sek110237 } 32512926Sek110237 32522926Sek110237 /* 32532926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 32542926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 32552926Sek110237 * processed as there wasn't a complete record. 32562926Sek110237 */ 325710685SGeorge.Wilson@Sun.COM int 32582926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 32592926Sek110237 nvlist_t ***records, uint_t *numrecords) 32602926Sek110237 { 32612926Sek110237 uint64_t reclen; 32622926Sek110237 nvlist_t *nv; 32632926Sek110237 int i; 32642926Sek110237 32652926Sek110237 while (bytes_read > sizeof (reclen)) { 32662926Sek110237 32672926Sek110237 /* get length of packed record (stored as little endian) */ 32682926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 32692926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 32702926Sek110237 32712926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 32722926Sek110237 break; 32732926Sek110237 32742926Sek110237 /* unpack record */ 32752926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 32762926Sek110237 return (ENOMEM); 32772926Sek110237 bytes_read -= sizeof (reclen) + reclen; 32782926Sek110237 buf += sizeof (reclen) + reclen; 32792926Sek110237 32802926Sek110237 /* add record to nvlist array */ 32812926Sek110237 (*numrecords)++; 32822926Sek110237 if (ISP2(*numrecords + 1)) { 32832926Sek110237 *records = realloc(*records, 32842926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 32852926Sek110237 } 32862926Sek110237 (*records)[*numrecords - 1] = nv; 32872926Sek110237 } 32882926Sek110237 32892926Sek110237 *leftover = bytes_read; 32902926Sek110237 return (0); 32912926Sek110237 } 32922926Sek110237 32932926Sek110237 #define HIS_BUF_LEN (128*1024) 32942926Sek110237 32952926Sek110237 /* 32962926Sek110237 * Retrieve the command history of a pool. 32972926Sek110237 */ 32982926Sek110237 int 32992926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 33002926Sek110237 { 33012926Sek110237 char buf[HIS_BUF_LEN]; 33022926Sek110237 uint64_t off = 0; 33032926Sek110237 nvlist_t **records = NULL; 33042926Sek110237 uint_t numrecords = 0; 33052926Sek110237 int err, i; 33062926Sek110237 33072926Sek110237 do { 33082926Sek110237 uint64_t bytes_read = sizeof (buf); 33092926Sek110237 uint64_t leftover; 33102926Sek110237 33112926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 33122926Sek110237 break; 33132926Sek110237 33142926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 33152926Sek110237 if (!bytes_read) 33162926Sek110237 break; 33172926Sek110237 33182926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 33192926Sek110237 &leftover, &records, &numrecords)) != 0) 33202926Sek110237 break; 33212926Sek110237 off -= leftover; 33222926Sek110237 33232926Sek110237 /* CONSTCOND */ 33242926Sek110237 } while (1); 33252926Sek110237 33262926Sek110237 if (!err) { 33272926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 33282926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 33292926Sek110237 records, numrecords) == 0); 33302926Sek110237 } 33312926Sek110237 for (i = 0; i < numrecords; i++) 33322926Sek110237 nvlist_free(records[i]); 33332926Sek110237 free(records); 33342926Sek110237 33352926Sek110237 return (err); 33362926Sek110237 } 33373444Sek110237 33383444Sek110237 void 33393444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 33403444Sek110237 char *pathname, size_t len) 33413444Sek110237 { 33423444Sek110237 zfs_cmd_t zc = { 0 }; 33433444Sek110237 boolean_t mounted = B_FALSE; 33443444Sek110237 char *mntpnt = NULL; 33453444Sek110237 char dsname[MAXNAMELEN]; 33463444Sek110237 33473444Sek110237 if (dsobj == 0) { 33483444Sek110237 /* special case for the MOS */ 33493444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 33503444Sek110237 return; 33513444Sek110237 } 33523444Sek110237 33533444Sek110237 /* get the dataset's name */ 33543444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 33553444Sek110237 zc.zc_obj = dsobj; 33563444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 33573444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 33583444Sek110237 /* just write out a path of two object numbers */ 33593444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 33603444Sek110237 dsobj, obj); 33613444Sek110237 return; 33623444Sek110237 } 33633444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 33643444Sek110237 33653444Sek110237 /* find out if the dataset is mounted */ 33663444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 33673444Sek110237 33683444Sek110237 /* get the corrupted object's path */ 33693444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 33703444Sek110237 zc.zc_obj = obj; 33713444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 33723444Sek110237 &zc) == 0) { 33733444Sek110237 if (mounted) { 33743444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 33753444Sek110237 zc.zc_value); 33763444Sek110237 } else { 33773444Sek110237 (void) snprintf(pathname, len, "%s:%s", 33783444Sek110237 dsname, zc.zc_value); 33793444Sek110237 } 33803444Sek110237 } else { 33813444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 33823444Sek110237 } 33833444Sek110237 free(mntpnt); 33843444Sek110237 } 33853912Slling 33864276Staylor /* 33877042Sgw25295 * Read the EFI label from the config, if a label does not exist then 33887042Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 33897042Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 33907042Sgw25295 * partition. 33917042Sgw25295 */ 33927042Sgw25295 static int 33937042Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 33947042Sgw25295 { 33957042Sgw25295 char *path; 33967042Sgw25295 int fd; 33977042Sgw25295 char diskname[MAXPATHLEN]; 33987042Sgw25295 int err = -1; 33997042Sgw25295 34007042Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 34017042Sgw25295 return (err); 34027042Sgw25295 34037042Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 34047042Sgw25295 strrchr(path, '/')); 34057042Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 34067042Sgw25295 struct dk_gpt *vtoc; 34077042Sgw25295 34087042Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 34097042Sgw25295 if (sb != NULL) 34107042Sgw25295 *sb = vtoc->efi_parts[0].p_start; 34117042Sgw25295 efi_free(vtoc); 34127042Sgw25295 } 34137042Sgw25295 (void) close(fd); 34147042Sgw25295 } 34157042Sgw25295 return (err); 34167042Sgw25295 } 34177042Sgw25295 34187042Sgw25295 /* 34194276Staylor * determine where a partition starts on a disk in the current 34204276Staylor * configuration 34214276Staylor */ 34224276Staylor static diskaddr_t 34234276Staylor find_start_block(nvlist_t *config) 34244276Staylor { 34254276Staylor nvlist_t **child; 34264276Staylor uint_t c, children; 34274276Staylor diskaddr_t sb = MAXOFFSET_T; 34284276Staylor uint64_t wholedisk; 34294276Staylor 34304276Staylor if (nvlist_lookup_nvlist_array(config, 34314276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 34324276Staylor if (nvlist_lookup_uint64(config, 34334276Staylor ZPOOL_CONFIG_WHOLE_DISK, 34344276Staylor &wholedisk) != 0 || !wholedisk) { 34354276Staylor return (MAXOFFSET_T); 34364276Staylor } 34377042Sgw25295 if (read_efi_label(config, &sb) < 0) 34387042Sgw25295 sb = MAXOFFSET_T; 34394276Staylor return (sb); 34404276Staylor } 34414276Staylor 34424276Staylor for (c = 0; c < children; c++) { 34434276Staylor sb = find_start_block(child[c]); 34444276Staylor if (sb != MAXOFFSET_T) { 34454276Staylor return (sb); 34464276Staylor } 34474276Staylor } 34484276Staylor return (MAXOFFSET_T); 34494276Staylor } 34504276Staylor 34514276Staylor /* 34524276Staylor * Label an individual disk. The name provided is the short name, 34534276Staylor * stripped of any leading /dev path. 34544276Staylor */ 34554276Staylor int 34564276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 34574276Staylor { 34584276Staylor char path[MAXPATHLEN]; 34594276Staylor struct dk_gpt *vtoc; 34604276Staylor int fd; 34614276Staylor size_t resv = EFI_MIN_RESV_SIZE; 34624276Staylor uint64_t slice_size; 34634276Staylor diskaddr_t start_block; 34644276Staylor char errbuf[1024]; 34654276Staylor 34666289Smmusante /* prepare an error message just in case */ 34676289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 34686289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 34696289Smmusante 34704276Staylor if (zhp) { 34714276Staylor nvlist_t *nvroot; 34724276Staylor 34737965SGeorge.Wilson@Sun.COM if (pool_is_bootable(zhp)) { 34747965SGeorge.Wilson@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34757965SGeorge.Wilson@Sun.COM "EFI labeled devices are not supported on root " 34767965SGeorge.Wilson@Sun.COM "pools.")); 34777965SGeorge.Wilson@Sun.COM return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 34787965SGeorge.Wilson@Sun.COM } 34797965SGeorge.Wilson@Sun.COM 34804276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 34814276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 34824276Staylor 34834276Staylor if (zhp->zpool_start_block == 0) 34844276Staylor start_block = find_start_block(nvroot); 34854276Staylor else 34864276Staylor start_block = zhp->zpool_start_block; 34874276Staylor zhp->zpool_start_block = start_block; 34884276Staylor } else { 34894276Staylor /* new pool */ 34904276Staylor start_block = NEW_START_BLOCK; 34914276Staylor } 34924276Staylor 34934276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 34944276Staylor BACKUP_SLICE); 34954276Staylor 34964276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 34974276Staylor /* 34984276Staylor * This shouldn't happen. We've long since verified that this 34994276Staylor * is a valid device. 35004276Staylor */ 35016289Smmusante zfs_error_aux(hdl, 35026289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 35034276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 35044276Staylor } 35054276Staylor 35064276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 35074276Staylor /* 35084276Staylor * The only way this can fail is if we run out of memory, or we 35094276Staylor * were unable to read the disk's capacity 35104276Staylor */ 35114276Staylor if (errno == ENOMEM) 35124276Staylor (void) no_memory(hdl); 35134276Staylor 35144276Staylor (void) close(fd); 35156289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35166289Smmusante "unable to read disk capacity"), name); 35174276Staylor 35184276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 35194276Staylor } 35204276Staylor 35214276Staylor slice_size = vtoc->efi_last_u_lba + 1; 35224276Staylor slice_size -= EFI_MIN_RESV_SIZE; 35234276Staylor if (start_block == MAXOFFSET_T) 35244276Staylor start_block = NEW_START_BLOCK; 35254276Staylor slice_size -= start_block; 35264276Staylor 35274276Staylor vtoc->efi_parts[0].p_start = start_block; 35284276Staylor vtoc->efi_parts[0].p_size = slice_size; 35294276Staylor 35304276Staylor /* 35314276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 35324276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 35334276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 35344276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 35354276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 35364276Staylor * can get, in the absence of V_OTHER. 35374276Staylor */ 35384276Staylor vtoc->efi_parts[0].p_tag = V_USR; 35394276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 35404276Staylor 35414276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 35424276Staylor vtoc->efi_parts[8].p_size = resv; 35434276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 35444276Staylor 35454276Staylor if (efi_write(fd, vtoc) != 0) { 35464276Staylor /* 35474276Staylor * Some block drivers (like pcata) may not support EFI 35484276Staylor * GPT labels. Print out a helpful error message dir- 35494276Staylor * ecting the user to manually label the disk and give 35504276Staylor * a specific slice. 35514276Staylor */ 35524276Staylor (void) close(fd); 35534276Staylor efi_free(vtoc); 35544276Staylor 35554276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35566289Smmusante "try using fdisk(1M) and then provide a specific slice")); 35574276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 35584276Staylor } 35594276Staylor 35604276Staylor (void) close(fd); 35614276Staylor efi_free(vtoc); 35624276Staylor return (0); 35634276Staylor } 35646423Sgw25295 35656423Sgw25295 static boolean_t 35666423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 35676423Sgw25295 { 35686423Sgw25295 char *type; 35696423Sgw25295 nvlist_t **child; 35706423Sgw25295 uint_t children, c; 35716423Sgw25295 35726423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 35736423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 35746423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 35756423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 357610594SGeorge.Wilson@Sun.COM strcmp(type, VDEV_TYPE_HOLE) == 0 || 35776423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 35786423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35796423Sgw25295 "vdev type '%s' is not supported"), type); 35806423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 35816423Sgw25295 return (B_FALSE); 35826423Sgw25295 } 35836423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 35846423Sgw25295 &child, &children) == 0) { 35856423Sgw25295 for (c = 0; c < children; c++) { 35866423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 35876423Sgw25295 return (B_FALSE); 35886423Sgw25295 } 35896423Sgw25295 } 35906423Sgw25295 return (B_TRUE); 35916423Sgw25295 } 35926423Sgw25295 35936423Sgw25295 /* 35946423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 35956423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 35966423Sgw25295 */ 35976423Sgw25295 int 35986423Sgw25295 zvol_check_dump_config(char *arg) 35996423Sgw25295 { 36006423Sgw25295 zpool_handle_t *zhp = NULL; 36016423Sgw25295 nvlist_t *config, *nvroot; 36026423Sgw25295 char *p, *volname; 36036423Sgw25295 nvlist_t **top; 36046423Sgw25295 uint_t toplevels; 36056423Sgw25295 libzfs_handle_t *hdl; 36066423Sgw25295 char errbuf[1024]; 36076423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 36086423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 36096423Sgw25295 int ret = 1; 36106423Sgw25295 36116423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 36126423Sgw25295 return (-1); 36136423Sgw25295 } 36146423Sgw25295 36156423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36166423Sgw25295 "dump is not supported on device '%s'"), arg); 36176423Sgw25295 36186423Sgw25295 if ((hdl = libzfs_init()) == NULL) 36196423Sgw25295 return (1); 36206423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 36216423Sgw25295 36226423Sgw25295 volname = arg + pathlen; 36236423Sgw25295 36246423Sgw25295 /* check the configuration of the pool */ 36256423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 36266423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36276423Sgw25295 "malformed dataset name")); 36286423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 36296423Sgw25295 return (1); 36306423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 36316423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36326423Sgw25295 "dataset name is too long")); 36336423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 36346423Sgw25295 return (1); 36356423Sgw25295 } else { 36366423Sgw25295 (void) strncpy(poolname, volname, p - volname); 36376423Sgw25295 poolname[p - volname] = '\0'; 36386423Sgw25295 } 36396423Sgw25295 36406423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 36416423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36426423Sgw25295 "could not open pool '%s'"), poolname); 36436423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 36446423Sgw25295 goto out; 36456423Sgw25295 } 36466423Sgw25295 config = zpool_get_config(zhp, NULL); 36476423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 36486423Sgw25295 &nvroot) != 0) { 36496423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36506423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 36516423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 36526423Sgw25295 goto out; 36536423Sgw25295 } 36546423Sgw25295 36556423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 36566423Sgw25295 &top, &toplevels) == 0); 36576423Sgw25295 if (toplevels != 1) { 36586423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36596423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 36606423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 36616423Sgw25295 goto out; 36626423Sgw25295 } 36636423Sgw25295 36646423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 36656423Sgw25295 goto out; 36666423Sgw25295 } 36676423Sgw25295 ret = 0; 36686423Sgw25295 36696423Sgw25295 out: 36706423Sgw25295 if (zhp) 36716423Sgw25295 zpool_close(zhp); 36726423Sgw25295 libzfs_fini(hdl); 36736423Sgw25295 return (ret); 36746423Sgw25295 } 3675