1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 212082Seschrock 22789Sahrens /* 236289Smmusante * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 293126Sahl #include <alloca.h> 30789Sahrens #include <assert.h> 31789Sahrens #include <ctype.h> 32789Sahrens #include <errno.h> 33789Sahrens #include <devid.h> 343126Sahl #include <dirent.h> 35789Sahrens #include <fcntl.h> 36789Sahrens #include <libintl.h> 37789Sahrens #include <stdio.h> 38789Sahrens #include <stdlib.h> 393126Sahl #include <strings.h> 40789Sahrens #include <unistd.h> 414276Staylor #include <sys/efi_partition.h> 424276Staylor #include <sys/vtoc.h> 43789Sahrens #include <sys/zfs_ioctl.h> 441544Seschrock #include <sys/zio.h> 452926Sek110237 #include <strings.h> 46789Sahrens 47789Sahrens #include "zfs_namecheck.h" 483912Slling #include "zfs_prop.h" 49789Sahrens #include "libzfs_impl.h" 50789Sahrens 515094Slling 525094Slling /* 535094Slling * ==================================================================== 545094Slling * zpool property functions 555094Slling * ==================================================================== 565094Slling */ 575094Slling 585094Slling static int 595094Slling zpool_get_all_props(zpool_handle_t *zhp) 605094Slling { 615094Slling zfs_cmd_t zc = { 0 }; 625094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 635094Slling 645094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 655094Slling 665094Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 675094Slling return (-1); 685094Slling 695094Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 705094Slling if (errno == ENOMEM) { 715094Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 725094Slling zcmd_free_nvlists(&zc); 735094Slling return (-1); 745094Slling } 755094Slling } else { 765094Slling zcmd_free_nvlists(&zc); 775094Slling return (-1); 785094Slling } 795094Slling } 805094Slling 815094Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 825094Slling zcmd_free_nvlists(&zc); 835094Slling return (-1); 845094Slling } 855094Slling 865094Slling zcmd_free_nvlists(&zc); 875094Slling 885094Slling return (0); 895094Slling } 905094Slling 915094Slling static int 925094Slling zpool_props_refresh(zpool_handle_t *zhp) 935094Slling { 945094Slling nvlist_t *old_props; 955094Slling 965094Slling old_props = zhp->zpool_props; 975094Slling 985094Slling if (zpool_get_all_props(zhp) != 0) 995094Slling return (-1); 1005094Slling 1015094Slling nvlist_free(old_props); 1025094Slling return (0); 1035094Slling } 1045094Slling 1055094Slling static char * 1065094Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 1075094Slling zprop_source_t *src) 1085094Slling { 1095094Slling nvlist_t *nv, *nvl; 1105094Slling uint64_t ival; 1115094Slling char *value; 1125094Slling zprop_source_t source; 1135094Slling 1145094Slling nvl = zhp->zpool_props; 1155094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1165094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 1175094Slling source = ival; 1185094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1195094Slling } else { 1205094Slling source = ZPROP_SRC_DEFAULT; 1215094Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 1225094Slling value = "-"; 1235094Slling } 1245094Slling 1255094Slling if (src) 1265094Slling *src = source; 1275094Slling 1285094Slling return (value); 1295094Slling } 1305094Slling 1315094Slling uint64_t 1325094Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 1335094Slling { 1345094Slling nvlist_t *nv, *nvl; 1355094Slling uint64_t value; 1365094Slling zprop_source_t source; 1375094Slling 1385094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) 1395094Slling return (zpool_prop_default_numeric(prop)); 1405094Slling 1415094Slling nvl = zhp->zpool_props; 1425094Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 1435094Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 1445094Slling source = value; 1455094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1465094Slling } else { 1475094Slling source = ZPROP_SRC_DEFAULT; 1485094Slling value = zpool_prop_default_numeric(prop); 1495094Slling } 1505094Slling 1515094Slling if (src) 1525094Slling *src = source; 1535094Slling 1545094Slling return (value); 1555094Slling } 1565094Slling 1575094Slling /* 1585094Slling * Map VDEV STATE to printed strings. 1595094Slling */ 1605094Slling char * 1615094Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 1625094Slling { 1635094Slling switch (state) { 1645094Slling case VDEV_STATE_CLOSED: 1655094Slling case VDEV_STATE_OFFLINE: 1665094Slling return (gettext("OFFLINE")); 1675094Slling case VDEV_STATE_REMOVED: 1685094Slling return (gettext("REMOVED")); 1695094Slling case VDEV_STATE_CANT_OPEN: 1705094Slling if (aux == VDEV_AUX_CORRUPT_DATA) 1715094Slling return (gettext("FAULTED")); 1725094Slling else 1735094Slling return (gettext("UNAVAIL")); 1745094Slling case VDEV_STATE_FAULTED: 1755094Slling return (gettext("FAULTED")); 1765094Slling case VDEV_STATE_DEGRADED: 1775094Slling return (gettext("DEGRADED")); 1785094Slling case VDEV_STATE_HEALTHY: 1795094Slling return (gettext("ONLINE")); 1805094Slling } 1815094Slling 1825094Slling return (gettext("UNKNOWN")); 1835094Slling } 1845094Slling 1855094Slling /* 1865094Slling * Get a zpool property value for 'prop' and return the value in 1875094Slling * a pre-allocated buffer. 1885094Slling */ 1895094Slling int 1905094Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 1915094Slling zprop_source_t *srctype) 1925094Slling { 1935094Slling uint64_t intval; 1945094Slling const char *strval; 1955094Slling zprop_source_t src = ZPROP_SRC_NONE; 1965094Slling nvlist_t *nvroot; 1975094Slling vdev_stat_t *vs; 1985094Slling uint_t vsc; 1995094Slling 2005094Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 2015094Slling if (prop == ZPOOL_PROP_NAME) 2025094Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 2035094Slling else if (prop == ZPOOL_PROP_HEALTH) 2045094Slling (void) strlcpy(buf, "FAULTED", len); 2055094Slling else 2065094Slling (void) strlcpy(buf, "-", len); 2075094Slling return (0); 2085094Slling } 2095094Slling 2105094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 2115094Slling prop != ZPOOL_PROP_NAME) 2125094Slling return (-1); 2135094Slling 2145094Slling switch (zpool_prop_get_type(prop)) { 2155094Slling case PROP_TYPE_STRING: 2165094Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 2175094Slling len); 2185094Slling break; 2195094Slling 2205094Slling case PROP_TYPE_NUMBER: 2215094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2225094Slling 2235094Slling switch (prop) { 2245094Slling case ZPOOL_PROP_SIZE: 2255094Slling case ZPOOL_PROP_USED: 2265094Slling case ZPOOL_PROP_AVAILABLE: 2275094Slling (void) zfs_nicenum(intval, buf, len); 2285094Slling break; 2295094Slling 2305094Slling case ZPOOL_PROP_CAPACITY: 2315094Slling (void) snprintf(buf, len, "%llu%%", 2325094Slling (u_longlong_t)intval); 2335094Slling break; 2345094Slling 2355094Slling case ZPOOL_PROP_HEALTH: 2365094Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 2375094Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2385094Slling verify(nvlist_lookup_uint64_array(nvroot, 2395094Slling ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 2405094Slling 2415094Slling (void) strlcpy(buf, zpool_state_to_name(intval, 2425094Slling vs->vs_aux), len); 2435094Slling break; 2445094Slling default: 2455094Slling (void) snprintf(buf, len, "%llu", intval); 2465094Slling } 2475094Slling break; 2485094Slling 2495094Slling case PROP_TYPE_INDEX: 2505094Slling intval = zpool_get_prop_int(zhp, prop, &src); 2515094Slling if (zpool_prop_index_to_string(prop, intval, &strval) 2525094Slling != 0) 2535094Slling return (-1); 2545094Slling (void) strlcpy(buf, strval, len); 2555094Slling break; 2565094Slling 2575094Slling default: 2585094Slling abort(); 2595094Slling } 2605094Slling 2615094Slling if (srctype) 2625094Slling *srctype = src; 2635094Slling 2645094Slling return (0); 2655094Slling } 2665094Slling 2675094Slling /* 2685094Slling * Check if the bootfs name has the same pool name as it is set to. 2695094Slling * Assuming bootfs is a valid dataset name. 2705094Slling */ 2715094Slling static boolean_t 2725094Slling bootfs_name_valid(const char *pool, char *bootfs) 2735094Slling { 2745094Slling int len = strlen(pool); 2755094Slling 2765094Slling if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM)) 2775094Slling return (B_FALSE); 2785094Slling 2795094Slling if (strncmp(pool, bootfs, len) == 0 && 2805094Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 2815094Slling return (B_TRUE); 2825094Slling 2835094Slling return (B_FALSE); 2845094Slling } 2855094Slling 2865094Slling /* 2875094Slling * Given an nvlist of zpool properties to be set, validate that they are 2885094Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 2895094Slling * specified as strings. 2905094Slling */ 2915094Slling static nvlist_t * 2925094Slling zpool_validate_properties(libzfs_handle_t *hdl, const char *poolname, 2935094Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 2945094Slling { 2955094Slling nvpair_t *elem; 2965094Slling nvlist_t *retprops; 2975094Slling zpool_prop_t prop; 2985094Slling char *strval; 2995094Slling uint64_t intval; 3005363Seschrock char *slash; 3015363Seschrock struct stat64 statbuf; 3025094Slling 3035094Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 3045094Slling (void) no_memory(hdl); 3055094Slling return (NULL); 3065094Slling } 3075094Slling 3085094Slling elem = NULL; 3095094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3105094Slling const char *propname = nvpair_name(elem); 3115094Slling 3125094Slling /* 3135094Slling * Make sure this property is valid and applies to this type. 3145094Slling */ 3155094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 3165094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3175094Slling "invalid property '%s'"), propname); 3185094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3195094Slling goto error; 3205094Slling } 3215094Slling 3225094Slling if (zpool_prop_readonly(prop)) { 3235094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 3245094Slling "is readonly"), propname); 3255094Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 3265094Slling goto error; 3275094Slling } 3285094Slling 3295094Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 3305094Slling &strval, &intval, errbuf) != 0) 3315094Slling goto error; 3325094Slling 3335094Slling /* 3345094Slling * Perform additional checking for specific properties. 3355094Slling */ 3365094Slling switch (prop) { 3375094Slling case ZPOOL_PROP_VERSION: 3385094Slling if (intval < version || intval > SPA_VERSION) { 3395094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3405094Slling "property '%s' number %d is invalid."), 3415094Slling propname, intval); 3425094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 3435094Slling goto error; 3445094Slling } 3455094Slling break; 3465094Slling 3475094Slling case ZPOOL_PROP_BOOTFS: 3485094Slling if (create_or_import) { 3495094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3505094Slling "property '%s' cannot be set at creation " 3515094Slling "or import time"), propname); 3525094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3535094Slling goto error; 3545094Slling } 3555094Slling 3565094Slling if (version < SPA_VERSION_BOOTFS) { 3575094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3585094Slling "pool must be upgraded to support " 3595094Slling "'%s' property"), propname); 3605094Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 3615094Slling goto error; 3625094Slling } 3635094Slling 3645094Slling /* 3655094Slling * bootfs property value has to be a dataset name and 3665094Slling * the dataset has to be in the same pool as it sets to. 3675094Slling */ 3685094Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 3695094Slling strval)) { 3705094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 3715094Slling "is an invalid name"), strval); 3725094Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 3735094Slling goto error; 3745094Slling } 3755094Slling break; 3765094Slling 3775094Slling case ZPOOL_PROP_ALTROOT: 3785094Slling if (!create_or_import) { 3795094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3805094Slling "property '%s' can only be set during pool " 3815094Slling "creation or import"), propname); 3825094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 3835094Slling goto error; 3845094Slling } 3855094Slling 3865094Slling if (strval[0] != '/') { 3875094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3885094Slling "bad alternate root '%s'"), strval); 3895094Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 3905094Slling goto error; 3915094Slling } 3925094Slling break; 3935363Seschrock 3945363Seschrock case ZPOOL_PROP_CACHEFILE: 3955363Seschrock if (strval[0] == '\0') 3965363Seschrock break; 3975363Seschrock 3985363Seschrock if (strcmp(strval, "none") == 0) 3995363Seschrock break; 4005363Seschrock 4015363Seschrock if (strval[0] != '/') { 4025363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4035363Seschrock "property '%s' must be empty, an " 4045363Seschrock "absolute path, or 'none'"), propname); 4055363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4065363Seschrock goto error; 4075363Seschrock } 4085363Seschrock 4095363Seschrock slash = strrchr(strval, '/'); 4105363Seschrock 4115363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4125363Seschrock strcmp(slash, "/..") == 0) { 4135363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4145363Seschrock "'%s' is not a valid file"), strval); 4155363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4165363Seschrock goto error; 4175363Seschrock } 4185363Seschrock 4195363Seschrock *slash = '\0'; 4205363Seschrock 4215621Seschrock if (strval[0] != '\0' && 4225621Seschrock (stat64(strval, &statbuf) != 0 || 4235621Seschrock !S_ISDIR(statbuf.st_mode))) { 4245363Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4255363Seschrock "'%s' is not a valid directory"), 4265363Seschrock strval); 4275363Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4285363Seschrock goto error; 4295363Seschrock } 4305363Seschrock 4315363Seschrock *slash = '/'; 4325363Seschrock break; 4335094Slling } 4345094Slling } 4355094Slling 4365094Slling return (retprops); 4375094Slling error: 4385094Slling nvlist_free(retprops); 4395094Slling return (NULL); 4405094Slling } 4415094Slling 4425094Slling /* 4435094Slling * Set zpool property : propname=propval. 4445094Slling */ 4455094Slling int 4465094Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 4475094Slling { 4485094Slling zfs_cmd_t zc = { 0 }; 4495094Slling int ret = -1; 4505094Slling char errbuf[1024]; 4515094Slling nvlist_t *nvl = NULL; 4525094Slling nvlist_t *realprops; 4535094Slling uint64_t version; 4545094Slling 4555094Slling (void) snprintf(errbuf, sizeof (errbuf), 4565094Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 4575094Slling zhp->zpool_name); 4585094Slling 4595094Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) 4605094Slling return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf)); 4615094Slling 4625094Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 4635094Slling return (no_memory(zhp->zpool_hdl)); 4645094Slling 4655094Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 4665094Slling nvlist_free(nvl); 4675094Slling return (no_memory(zhp->zpool_hdl)); 4685094Slling } 4695094Slling 4705094Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 4715094Slling if ((realprops = zpool_validate_properties(zhp->zpool_hdl, 4725094Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 4735094Slling nvlist_free(nvl); 4745094Slling return (-1); 4755094Slling } 4765094Slling 4775094Slling nvlist_free(nvl); 4785094Slling nvl = realprops; 4795094Slling 4805094Slling /* 4815094Slling * Execute the corresponding ioctl() to set this property. 4825094Slling */ 4835094Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4845094Slling 4855094Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 4865094Slling nvlist_free(nvl); 4875094Slling return (-1); 4885094Slling } 4895094Slling 4905094Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 4915094Slling 4925094Slling zcmd_free_nvlists(&zc); 4935094Slling nvlist_free(nvl); 4945094Slling 4955094Slling if (ret) 4965094Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 4975094Slling else 4985094Slling (void) zpool_props_refresh(zhp); 4995094Slling 5005094Slling return (ret); 5015094Slling } 5025094Slling 5035094Slling int 5045094Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 5055094Slling { 5065094Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 5075094Slling zprop_list_t *entry; 5085094Slling char buf[ZFS_MAXPROPLEN]; 5095094Slling 5105094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 5115094Slling return (-1); 5125094Slling 5135094Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 5145094Slling 5155094Slling if (entry->pl_fixed) 5165094Slling continue; 5175094Slling 5185094Slling if (entry->pl_prop != ZPROP_INVAL && 5195094Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 5205094Slling NULL) == 0) { 5215094Slling if (strlen(buf) > entry->pl_width) 5225094Slling entry->pl_width = strlen(buf); 5235094Slling } 5245094Slling } 5255094Slling 5265094Slling return (0); 5275094Slling } 5285094Slling 5295094Slling 530789Sahrens /* 531789Sahrens * Validate the given pool name, optionally putting an extended error message in 532789Sahrens * 'buf'. 533789Sahrens */ 534*6423Sgw25295 boolean_t 5352082Seschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 536789Sahrens { 537789Sahrens namecheck_err_t why; 538789Sahrens char what; 5391773Seschrock int ret; 540789Sahrens 5411773Seschrock ret = pool_namecheck(pool, &why, &what); 5421773Seschrock 5431773Seschrock /* 5441773Seschrock * The rules for reserved pool names were extended at a later point. 5451773Seschrock * But we need to support users with existing pools that may now be 5461773Seschrock * invalid. So we only check for this expanded set of names during a 5471773Seschrock * create (or import), and only in userland. 5481773Seschrock */ 5491773Seschrock if (ret == 0 && !isopen && 5501773Seschrock (strncmp(pool, "mirror", 6) == 0 || 5511773Seschrock strncmp(pool, "raidz", 5) == 0 || 5524527Sperrin strncmp(pool, "spare", 5) == 0 || 5534527Sperrin strcmp(pool, "log") == 0)) { 554*6423Sgw25295 if (hdl != NULL) 555*6423Sgw25295 zfs_error_aux(hdl, 556*6423Sgw25295 dgettext(TEXT_DOMAIN, "name is reserved")); 5572082Seschrock return (B_FALSE); 5581773Seschrock } 5591773Seschrock 5601773Seschrock 5611773Seschrock if (ret != 0) { 5622082Seschrock if (hdl != NULL) { 563789Sahrens switch (why) { 5641003Slling case NAME_ERR_TOOLONG: 5652082Seschrock zfs_error_aux(hdl, 5661003Slling dgettext(TEXT_DOMAIN, "name is too long")); 5671003Slling break; 5681003Slling 569789Sahrens case NAME_ERR_INVALCHAR: 5702082Seschrock zfs_error_aux(hdl, 571789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 572789Sahrens "'%c' in pool name"), what); 573789Sahrens break; 574789Sahrens 575789Sahrens case NAME_ERR_NOLETTER: 5762082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5772082Seschrock "name must begin with a letter")); 578789Sahrens break; 579789Sahrens 580789Sahrens case NAME_ERR_RESERVED: 5812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5822082Seschrock "name is reserved")); 583789Sahrens break; 584789Sahrens 585789Sahrens case NAME_ERR_DISKLIKE: 5862082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5872082Seschrock "pool name is reserved")); 588789Sahrens break; 5892856Snd150628 5902856Snd150628 case NAME_ERR_LEADING_SLASH: 5912856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5922856Snd150628 "leading slash in name")); 5932856Snd150628 break; 5942856Snd150628 5952856Snd150628 case NAME_ERR_EMPTY_COMPONENT: 5962856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5972856Snd150628 "empty component in name")); 5982856Snd150628 break; 5992856Snd150628 6002856Snd150628 case NAME_ERR_TRAILING_SLASH: 6012856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6022856Snd150628 "trailing slash in name")); 6032856Snd150628 break; 6042856Snd150628 6052856Snd150628 case NAME_ERR_MULTIPLE_AT: 6062856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6072856Snd150628 "multiple '@' delimiters in name")); 6082856Snd150628 break; 6092856Snd150628 610789Sahrens } 611789Sahrens } 6122082Seschrock return (B_FALSE); 613789Sahrens } 614789Sahrens 6152082Seschrock return (B_TRUE); 616789Sahrens } 617789Sahrens 618789Sahrens /* 619789Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 620789Sahrens * state. 621789Sahrens */ 622789Sahrens zpool_handle_t * 6232082Seschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 624789Sahrens { 625789Sahrens zpool_handle_t *zhp; 6262142Seschrock boolean_t missing; 627789Sahrens 628789Sahrens /* 629789Sahrens * Make sure the pool name is valid. 630789Sahrens */ 6312082Seschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 6323237Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 6332082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 6342082Seschrock pool); 635789Sahrens return (NULL); 636789Sahrens } 637789Sahrens 6382082Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 6392082Seschrock return (NULL); 640789Sahrens 6412082Seschrock zhp->zpool_hdl = hdl; 642789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 643789Sahrens 6442142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 6452142Seschrock zpool_close(zhp); 6462142Seschrock return (NULL); 6472142Seschrock } 6482142Seschrock 6492142Seschrock if (missing) { 6505094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 6513237Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 6525094Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 6532142Seschrock zpool_close(zhp); 6542142Seschrock return (NULL); 655789Sahrens } 656789Sahrens 657789Sahrens return (zhp); 658789Sahrens } 659789Sahrens 660789Sahrens /* 661789Sahrens * Like the above, but silent on error. Used when iterating over pools (because 662789Sahrens * the configuration cache may be out of date). 663789Sahrens */ 6642142Seschrock int 6652142Seschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 666789Sahrens { 667789Sahrens zpool_handle_t *zhp; 6682142Seschrock boolean_t missing; 669789Sahrens 6702142Seschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 6712142Seschrock return (-1); 672789Sahrens 6732082Seschrock zhp->zpool_hdl = hdl; 674789Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 675789Sahrens 6762142Seschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 6772142Seschrock zpool_close(zhp); 6782142Seschrock return (-1); 679789Sahrens } 680789Sahrens 6812142Seschrock if (missing) { 6822142Seschrock zpool_close(zhp); 6832142Seschrock *ret = NULL; 6842142Seschrock return (0); 6852142Seschrock } 6862142Seschrock 6872142Seschrock *ret = zhp; 6882142Seschrock return (0); 689789Sahrens } 690789Sahrens 691789Sahrens /* 692789Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 693789Sahrens * state. 694789Sahrens */ 695789Sahrens zpool_handle_t * 6962082Seschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 697789Sahrens { 698789Sahrens zpool_handle_t *zhp; 699789Sahrens 7002082Seschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 701789Sahrens return (NULL); 702789Sahrens 703789Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 7043237Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 7052082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 706789Sahrens zpool_close(zhp); 707789Sahrens return (NULL); 708789Sahrens } 709789Sahrens 710789Sahrens return (zhp); 711789Sahrens } 712789Sahrens 713789Sahrens /* 714789Sahrens * Close the handle. Simply frees the memory associated with the handle. 715789Sahrens */ 716789Sahrens void 717789Sahrens zpool_close(zpool_handle_t *zhp) 718789Sahrens { 719789Sahrens if (zhp->zpool_config) 720789Sahrens nvlist_free(zhp->zpool_config); 721952Seschrock if (zhp->zpool_old_config) 722952Seschrock nvlist_free(zhp->zpool_old_config); 7233912Slling if (zhp->zpool_props) 7243912Slling nvlist_free(zhp->zpool_props); 725789Sahrens free(zhp); 726789Sahrens } 727789Sahrens 728789Sahrens /* 729789Sahrens * Return the name of the pool. 730789Sahrens */ 731789Sahrens const char * 732789Sahrens zpool_get_name(zpool_handle_t *zhp) 733789Sahrens { 734789Sahrens return (zhp->zpool_name); 735789Sahrens } 736789Sahrens 737789Sahrens 738789Sahrens /* 739789Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 740789Sahrens */ 741789Sahrens int 742789Sahrens zpool_get_state(zpool_handle_t *zhp) 743789Sahrens { 744789Sahrens return (zhp->zpool_state); 745789Sahrens } 746789Sahrens 747789Sahrens /* 748789Sahrens * Create the named pool, using the provided vdev list. It is assumed 749789Sahrens * that the consumer has already validated the contents of the nvlist, so we 750789Sahrens * don't have to worry about error semantics. 751789Sahrens */ 752789Sahrens int 7532082Seschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 7545094Slling nvlist_t *props) 755789Sahrens { 756789Sahrens zfs_cmd_t zc = { 0 }; 7572082Seschrock char msg[1024]; 7585094Slling char *altroot; 7592082Seschrock 7602082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 7612082Seschrock "cannot create '%s'"), pool); 762789Sahrens 7632082Seschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 7642082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 7652082Seschrock 7665320Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 7675320Slling return (-1); 7685320Slling 7695094Slling if (props && (props = zpool_validate_properties(hdl, pool, props, 7705094Slling SPA_VERSION_1, B_TRUE, msg)) == NULL) 7715094Slling return (-1); 7725094Slling 7735320Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 7745320Slling nvlist_free(props); 7755094Slling return (-1); 7765320Slling } 777789Sahrens 778789Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 779789Sahrens 7804543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc) != 0) { 7815320Slling 7822676Seschrock zcmd_free_nvlists(&zc); 7835320Slling nvlist_free(props); 7842082Seschrock 785789Sahrens switch (errno) { 786789Sahrens case EBUSY: 787789Sahrens /* 788789Sahrens * This can happen if the user has specified the same 789789Sahrens * device multiple times. We can't reliably detect this 790789Sahrens * until we try to add it and see we already have a 791789Sahrens * label. 792789Sahrens */ 7932082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7942082Seschrock "one or more vdevs refer to the same device")); 7952082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 796789Sahrens 797789Sahrens case EOVERFLOW: 798789Sahrens /* 7992082Seschrock * This occurs when one of the devices is below 800789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 801789Sahrens * device was the problem device since there's no 802789Sahrens * reliable way to determine device size from userland. 803789Sahrens */ 804789Sahrens { 805789Sahrens char buf[64]; 806789Sahrens 807789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 808789Sahrens 8092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8102082Seschrock "one or more devices is less than the " 8112082Seschrock "minimum size (%s)"), buf); 812789Sahrens } 8132082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 814789Sahrens 815789Sahrens case ENOSPC: 8162082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8172082Seschrock "one or more devices is out of space")); 8182082Seschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 819789Sahrens 8205450Sbrendan case ENOTBLK: 8215450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8225450Sbrendan "cache device must be a disk or disk slice")); 8235450Sbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 8245450Sbrendan 825789Sahrens default: 8262082Seschrock return (zpool_standard_error(hdl, errno, msg)); 827789Sahrens } 828789Sahrens } 829789Sahrens 830789Sahrens /* 831789Sahrens * If this is an alternate root pool, then we automatically set the 8322676Seschrock * mountpoint of the root dataset to be '/'. 833789Sahrens */ 8345094Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 8355094Slling &altroot) == 0) { 836789Sahrens zfs_handle_t *zhp; 837789Sahrens 8385094Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 8392676Seschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 8402676Seschrock "/") == 0); 841789Sahrens 842789Sahrens zfs_close(zhp); 843789Sahrens } 844789Sahrens 8455320Slling zcmd_free_nvlists(&zc); 8465320Slling nvlist_free(props); 847789Sahrens return (0); 848789Sahrens } 849789Sahrens 850789Sahrens /* 851789Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 852789Sahrens * datasets left in the pool. 853789Sahrens */ 854789Sahrens int 855789Sahrens zpool_destroy(zpool_handle_t *zhp) 856789Sahrens { 857789Sahrens zfs_cmd_t zc = { 0 }; 858789Sahrens zfs_handle_t *zfp = NULL; 8592082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 8602082Seschrock char msg[1024]; 861789Sahrens 862789Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 8632082Seschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 8642082Seschrock ZFS_TYPE_FILESYSTEM)) == NULL) 865789Sahrens return (-1); 866789Sahrens 8672856Snd150628 if (zpool_remove_zvol_links(zhp) != 0) 868789Sahrens return (-1); 869789Sahrens 870789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 871789Sahrens 8724543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 8732082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 8742082Seschrock "cannot destroy '%s'"), zhp->zpool_name); 875789Sahrens 8762082Seschrock if (errno == EROFS) { 8772082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8782082Seschrock "one or more devices is read only")); 8792082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 8802082Seschrock } else { 8812082Seschrock (void) zpool_standard_error(hdl, errno, msg); 882789Sahrens } 883789Sahrens 884789Sahrens if (zfp) 885789Sahrens zfs_close(zfp); 886789Sahrens return (-1); 887789Sahrens } 888789Sahrens 889789Sahrens if (zfp) { 890789Sahrens remove_mountpoint(zfp); 891789Sahrens zfs_close(zfp); 892789Sahrens } 893789Sahrens 894789Sahrens return (0); 895789Sahrens } 896789Sahrens 897789Sahrens /* 898789Sahrens * Add the given vdevs to the pool. The caller must have already performed the 899789Sahrens * necessary verification to ensure that the vdev specification is well-formed. 900789Sahrens */ 901789Sahrens int 902789Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 903789Sahrens { 9042676Seschrock zfs_cmd_t zc = { 0 }; 9052082Seschrock int ret; 9062082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 9072082Seschrock char msg[1024]; 9085450Sbrendan nvlist_t **spares, **l2cache; 9095450Sbrendan uint_t nspares, nl2cache; 9102082Seschrock 9112082Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 9122082Seschrock "cannot add to '%s'"), zhp->zpool_name); 9132082Seschrock 9145450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 9155450Sbrendan SPA_VERSION_SPARES && 9162082Seschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 9172082Seschrock &spares, &nspares) == 0) { 9182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 9192082Seschrock "upgraded to add hot spares")); 9202082Seschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 9212082Seschrock } 922789Sahrens 9235450Sbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 9245450Sbrendan SPA_VERSION_L2CACHE && 9255450Sbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 9265450Sbrendan &l2cache, &nl2cache) == 0) { 9275450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 9285450Sbrendan "upgraded to add cache devices")); 9295450Sbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 9305450Sbrendan } 9315450Sbrendan 9325094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 9332082Seschrock return (-1); 934789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 935789Sahrens 9364543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 937789Sahrens switch (errno) { 938789Sahrens case EBUSY: 939789Sahrens /* 940789Sahrens * This can happen if the user has specified the same 941789Sahrens * device multiple times. We can't reliably detect this 942789Sahrens * until we try to add it and see we already have a 943789Sahrens * label. 944789Sahrens */ 9452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9462082Seschrock "one or more vdevs refer to the same device")); 9472082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 948789Sahrens break; 949789Sahrens 950789Sahrens case EOVERFLOW: 951789Sahrens /* 952789Sahrens * This occurrs when one of the devices is below 953789Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 954789Sahrens * device was the problem device since there's no 955789Sahrens * reliable way to determine device size from userland. 956789Sahrens */ 957789Sahrens { 958789Sahrens char buf[64]; 959789Sahrens 960789Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 961789Sahrens 9622082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9632082Seschrock "device is less than the minimum " 9642082Seschrock "size (%s)"), buf); 965789Sahrens } 9662082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 9672082Seschrock break; 9682082Seschrock 9692082Seschrock case ENOTSUP: 9702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9714527Sperrin "pool must be upgraded to add these vdevs")); 9722082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 973789Sahrens break; 974789Sahrens 9753912Slling case EDOM: 9763912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9774527Sperrin "root pool can not have multiple vdevs" 9784527Sperrin " or separate logs")); 9793912Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 9803912Slling break; 9813912Slling 9825450Sbrendan case ENOTBLK: 9835450Sbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9845450Sbrendan "cache device must be a disk or disk slice")); 9855450Sbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 9865450Sbrendan break; 9875450Sbrendan 988789Sahrens default: 9892082Seschrock (void) zpool_standard_error(hdl, errno, msg); 990789Sahrens } 991789Sahrens 9922082Seschrock ret = -1; 9932082Seschrock } else { 9942082Seschrock ret = 0; 995789Sahrens } 996789Sahrens 9972676Seschrock zcmd_free_nvlists(&zc); 998789Sahrens 9992082Seschrock return (ret); 1000789Sahrens } 1001789Sahrens 1002789Sahrens /* 1003789Sahrens * Exports the pool from the system. The caller must ensure that there are no 1004789Sahrens * mounted datasets in the pool. 1005789Sahrens */ 1006789Sahrens int 1007789Sahrens zpool_export(zpool_handle_t *zhp) 1008789Sahrens { 1009789Sahrens zfs_cmd_t zc = { 0 }; 1010789Sahrens 1011789Sahrens if (zpool_remove_zvol_links(zhp) != 0) 1012789Sahrens return (-1); 1013789Sahrens 1014789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1015789Sahrens 10164543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) 10173237Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 10182082Seschrock dgettext(TEXT_DOMAIN, "cannot export '%s'"), 10192082Seschrock zhp->zpool_name)); 1020789Sahrens return (0); 1021789Sahrens } 1022789Sahrens 1023789Sahrens /* 10245094Slling * zpool_import() is a contracted interface. Should be kept the same 10255094Slling * if possible. 10265094Slling * 10275094Slling * Applications should use zpool_import_props() to import a pool with 10285094Slling * new properties value to be set. 1029789Sahrens */ 1030789Sahrens int 10312082Seschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 10325094Slling char *altroot) 10335094Slling { 10345094Slling nvlist_t *props = NULL; 10355094Slling int ret; 10365094Slling 10375094Slling if (altroot != NULL) { 10385094Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 10395094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 10405094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 10415094Slling newname)); 10425094Slling } 10435094Slling 10445094Slling if (nvlist_add_string(props, 10455094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) { 10465094Slling nvlist_free(props); 10475094Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 10485094Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 10495094Slling newname)); 10505094Slling } 10515094Slling } 10525094Slling 10535094Slling ret = zpool_import_props(hdl, config, newname, props); 10545094Slling if (props) 10555094Slling nvlist_free(props); 10565094Slling return (ret); 10575094Slling } 10585094Slling 10595094Slling /* 10605094Slling * Import the given pool using the known configuration and a list of 10615094Slling * properties to be set. The configuration should have come from 10625094Slling * zpool_find_import(). The 'newname' parameters control whether the pool 10635094Slling * is imported with a different name. 10645094Slling */ 10655094Slling int 10665094Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 10675094Slling nvlist_t *props) 1068789Sahrens { 10692676Seschrock zfs_cmd_t zc = { 0 }; 1070789Sahrens char *thename; 1071789Sahrens char *origname; 1072789Sahrens int ret; 10735094Slling char errbuf[1024]; 1074789Sahrens 1075789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1076789Sahrens &origname) == 0); 1077789Sahrens 10785094Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 10795094Slling "cannot import pool '%s'"), origname); 10805094Slling 1081789Sahrens if (newname != NULL) { 10822082Seschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 10833237Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 10842082Seschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 10852082Seschrock newname)); 1086789Sahrens thename = (char *)newname; 1087789Sahrens } else { 1088789Sahrens thename = origname; 1089789Sahrens } 1090789Sahrens 10915094Slling if (props) { 10925094Slling uint64_t version; 10935094Slling 10945094Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 10955094Slling &version) == 0); 10965094Slling 10975094Slling if ((props = zpool_validate_properties(hdl, origname, 10985320Slling props, version, B_TRUE, errbuf)) == NULL) { 10995094Slling return (-1); 11005320Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 11015320Slling nvlist_free(props); 11025094Slling return (-1); 11035320Slling } 11045094Slling } 1105789Sahrens 1106789Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1107789Sahrens 1108789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 11091544Seschrock &zc.zc_guid) == 0); 1110789Sahrens 11115320Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 11125320Slling nvlist_free(props); 11132082Seschrock return (-1); 11145320Slling } 1115789Sahrens 1116789Sahrens ret = 0; 11174543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1118789Sahrens char desc[1024]; 1119789Sahrens if (newname == NULL) 1120789Sahrens (void) snprintf(desc, sizeof (desc), 1121789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1122789Sahrens thename); 1123789Sahrens else 1124789Sahrens (void) snprintf(desc, sizeof (desc), 1125789Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1126789Sahrens origname, thename); 1127789Sahrens 1128789Sahrens switch (errno) { 11291544Seschrock case ENOTSUP: 11301544Seschrock /* 11311544Seschrock * Unsupported version. 11321544Seschrock */ 11332082Seschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 11341544Seschrock break; 11351544Seschrock 11362174Seschrock case EINVAL: 11372174Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 11382174Seschrock break; 11392174Seschrock 1140789Sahrens default: 11412082Seschrock (void) zpool_standard_error(hdl, errno, desc); 1142789Sahrens } 1143789Sahrens 1144789Sahrens ret = -1; 1145789Sahrens } else { 1146789Sahrens zpool_handle_t *zhp; 11474543Smarks 1148789Sahrens /* 1149789Sahrens * This should never fail, but play it safe anyway. 1150789Sahrens */ 11512142Seschrock if (zpool_open_silent(hdl, thename, &zhp) != 0) { 11522142Seschrock ret = -1; 11532142Seschrock } else if (zhp != NULL) { 1154789Sahrens ret = zpool_create_zvol_links(zhp); 1155789Sahrens zpool_close(zhp); 1156789Sahrens } 11574543Smarks 1158789Sahrens } 1159789Sahrens 11602676Seschrock zcmd_free_nvlists(&zc); 11615320Slling nvlist_free(props); 11625320Slling 1163789Sahrens return (ret); 1164789Sahrens } 1165789Sahrens 1166789Sahrens /* 1167789Sahrens * Scrub the pool. 1168789Sahrens */ 1169789Sahrens int 1170789Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) 1171789Sahrens { 1172789Sahrens zfs_cmd_t zc = { 0 }; 1173789Sahrens char msg[1024]; 11742082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1175789Sahrens 1176789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1177789Sahrens zc.zc_cookie = type; 1178789Sahrens 11794543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0) 1180789Sahrens return (0); 1181789Sahrens 1182789Sahrens (void) snprintf(msg, sizeof (msg), 1183789Sahrens dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1184789Sahrens 11852082Seschrock if (errno == EBUSY) 11862082Seschrock return (zfs_error(hdl, EZFS_RESILVERING, msg)); 11872082Seschrock else 11882082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1189789Sahrens } 1190789Sahrens 11912468Sek110237 /* 11922468Sek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 11932468Sek110237 * spare; but FALSE if its an INUSE spare. 11942468Sek110237 */ 11952082Seschrock static nvlist_t * 11962082Seschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, 11975450Sbrendan boolean_t *avail_spare, boolean_t *l2cache) 11981544Seschrock { 11991544Seschrock uint_t c, children; 12001544Seschrock nvlist_t **child; 12012082Seschrock uint64_t theguid, present; 12021544Seschrock char *path; 12031544Seschrock uint64_t wholedisk = 0; 12042082Seschrock nvlist_t *ret; 12051544Seschrock 12062082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); 12071544Seschrock 12081544Seschrock if (search == NULL && 12091544Seschrock nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { 12101544Seschrock /* 12111544Seschrock * If the device has never been present since import, the only 12121544Seschrock * reliable way to match the vdev is by GUID. 12131544Seschrock */ 12142082Seschrock if (theguid == guid) 12152082Seschrock return (nv); 12161544Seschrock } else if (search != NULL && 12171544Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 12181544Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 12191544Seschrock &wholedisk); 12201544Seschrock if (wholedisk) { 12211544Seschrock /* 12221544Seschrock * For whole disks, the internal path has 's0', but the 12231544Seschrock * path passed in by the user doesn't. 12241544Seschrock */ 12251544Seschrock if (strlen(search) == strlen(path) - 2 && 12261544Seschrock strncmp(search, path, strlen(search)) == 0) 12272082Seschrock return (nv); 12281544Seschrock } else if (strcmp(search, path) == 0) { 12292082Seschrock return (nv); 12301544Seschrock } 12311544Seschrock } 12321544Seschrock 12331544Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 12341544Seschrock &child, &children) != 0) 12352082Seschrock return (NULL); 12361544Seschrock 12371544Seschrock for (c = 0; c < children; c++) 12382082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 12395450Sbrendan avail_spare, l2cache)) != NULL) 12401544Seschrock return (ret); 12411544Seschrock 12422082Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 12432082Seschrock &child, &children) == 0) { 12442082Seschrock for (c = 0; c < children; c++) { 12452082Seschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 12465450Sbrendan avail_spare, l2cache)) != NULL) { 12472468Sek110237 *avail_spare = B_TRUE; 12482082Seschrock return (ret); 12492082Seschrock } 12502082Seschrock } 12512082Seschrock } 12522082Seschrock 12535450Sbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 12545450Sbrendan &child, &children) == 0) { 12555450Sbrendan for (c = 0; c < children; c++) { 12565450Sbrendan if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 12575450Sbrendan avail_spare, l2cache)) != NULL) { 12585450Sbrendan *l2cache = B_TRUE; 12595450Sbrendan return (ret); 12605450Sbrendan } 12615450Sbrendan } 12625450Sbrendan } 12635450Sbrendan 12642082Seschrock return (NULL); 12651544Seschrock } 12661544Seschrock 12672082Seschrock nvlist_t * 12685450Sbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 12695450Sbrendan boolean_t *l2cache) 12701544Seschrock { 12711544Seschrock char buf[MAXPATHLEN]; 12721544Seschrock const char *search; 12731544Seschrock char *end; 12741544Seschrock nvlist_t *nvroot; 12751544Seschrock uint64_t guid; 12761544Seschrock 12771613Seschrock guid = strtoull(path, &end, 10); 12781544Seschrock if (guid != 0 && *end == '\0') { 12791544Seschrock search = NULL; 12801544Seschrock } else if (path[0] != '/') { 12811544Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 12821544Seschrock search = buf; 12831544Seschrock } else { 12841544Seschrock search = path; 12851544Seschrock } 12861544Seschrock 12871544Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 12881544Seschrock &nvroot) == 0); 12891544Seschrock 12902468Sek110237 *avail_spare = B_FALSE; 12915450Sbrendan *l2cache = B_FALSE; 12925450Sbrendan return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare, 12935450Sbrendan l2cache)); 12942468Sek110237 } 12952468Sek110237 12962468Sek110237 /* 12975450Sbrendan * Returns TRUE if the given guid corresponds to the given type. 12985450Sbrendan * This is used to check for hot spares (INUSE or not), and level 2 cache 12995450Sbrendan * devices. 13002468Sek110237 */ 13012468Sek110237 static boolean_t 13025450Sbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 13032468Sek110237 { 13045450Sbrendan uint64_t target_guid; 13052468Sek110237 nvlist_t *nvroot; 13065450Sbrendan nvlist_t **list; 13075450Sbrendan uint_t count; 13082468Sek110237 int i; 13092468Sek110237 13102468Sek110237 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 13112468Sek110237 &nvroot) == 0); 13125450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 13135450Sbrendan for (i = 0; i < count; i++) { 13145450Sbrendan verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 13155450Sbrendan &target_guid) == 0); 13165450Sbrendan if (guid == target_guid) 13172468Sek110237 return (B_TRUE); 13182468Sek110237 } 13192468Sek110237 } 13202468Sek110237 13212468Sek110237 return (B_FALSE); 13221544Seschrock } 13231544Seschrock 1324789Sahrens /* 13254451Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 13264451Seschrock * ZFS_ONLINE_* flags. 1327789Sahrens */ 1328789Sahrens int 13294451Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 13304451Seschrock vdev_state_t *newstate) 1331789Sahrens { 1332789Sahrens zfs_cmd_t zc = { 0 }; 1333789Sahrens char msg[1024]; 13342082Seschrock nvlist_t *tgt; 13355450Sbrendan boolean_t avail_spare, l2cache; 13362082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1337789Sahrens 13381544Seschrock (void) snprintf(msg, sizeof (msg), 13391544Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1340789Sahrens 13411544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 13425450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL) 13432082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1344789Sahrens 13452468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 13462468Sek110237 13475450Sbrendan if (avail_spare || 13485450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 13492082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 13502082Seschrock 13515450Sbrendan if (l2cache || 13525450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_L2CACHE) == B_TRUE) 13535450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 13545450Sbrendan 13554451Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 13564451Seschrock zc.zc_obj = flags; 13574451Seschrock 1358789Sahrens 13594543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 13604451Seschrock return (zpool_standard_error(hdl, errno, msg)); 13614451Seschrock 13624451Seschrock *newstate = zc.zc_cookie; 13634451Seschrock return (0); 1364789Sahrens } 1365789Sahrens 1366789Sahrens /* 1367789Sahrens * Take the specified vdev offline 1368789Sahrens */ 1369789Sahrens int 13704451Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1371789Sahrens { 1372789Sahrens zfs_cmd_t zc = { 0 }; 1373789Sahrens char msg[1024]; 13742082Seschrock nvlist_t *tgt; 13755450Sbrendan boolean_t avail_spare, l2cache; 13762082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1377789Sahrens 13781544Seschrock (void) snprintf(msg, sizeof (msg), 13791544Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 13801544Seschrock 1381789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 13825450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL) 13832082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 13842082Seschrock 13852468Sek110237 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 13862468Sek110237 13875450Sbrendan if (avail_spare || 13885450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 13892082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 13902082Seschrock 13915450Sbrendan if (l2cache || 13925450Sbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_L2CACHE) == B_TRUE) 13935450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 13945450Sbrendan 13954451Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 13964451Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 13971485Slling 13984543Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1399789Sahrens return (0); 1400789Sahrens 1401789Sahrens switch (errno) { 14022082Seschrock case EBUSY: 1403789Sahrens 1404789Sahrens /* 1405789Sahrens * There are no other replicas of this device. 1406789Sahrens */ 14072082Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 14082082Seschrock 14092082Seschrock default: 14102082Seschrock return (zpool_standard_error(hdl, errno, msg)); 14112082Seschrock } 14122082Seschrock } 1413789Sahrens 14142082Seschrock /* 14154451Seschrock * Mark the given vdev faulted. 14164451Seschrock */ 14174451Seschrock int 14184451Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 14194451Seschrock { 14204451Seschrock zfs_cmd_t zc = { 0 }; 14214451Seschrock char msg[1024]; 14224451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 14234451Seschrock 14244451Seschrock (void) snprintf(msg, sizeof (msg), 14254451Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 14264451Seschrock 14274451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 14284451Seschrock zc.zc_guid = guid; 14294451Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 14304451Seschrock 14314451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 14324451Seschrock return (0); 14334451Seschrock 14344451Seschrock switch (errno) { 14354451Seschrock case EBUSY: 14364451Seschrock 14374451Seschrock /* 14384451Seschrock * There are no other replicas of this device. 14394451Seschrock */ 14404451Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 14414451Seschrock 14424451Seschrock default: 14434451Seschrock return (zpool_standard_error(hdl, errno, msg)); 14444451Seschrock } 14454451Seschrock 14464451Seschrock } 14474451Seschrock 14484451Seschrock /* 14494451Seschrock * Mark the given vdev degraded. 14504451Seschrock */ 14514451Seschrock int 14524451Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 14534451Seschrock { 14544451Seschrock zfs_cmd_t zc = { 0 }; 14554451Seschrock char msg[1024]; 14564451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 14574451Seschrock 14584451Seschrock (void) snprintf(msg, sizeof (msg), 14594451Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 14604451Seschrock 14614451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 14624451Seschrock zc.zc_guid = guid; 14634451Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 14644451Seschrock 14654451Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 14664451Seschrock return (0); 14674451Seschrock 14684451Seschrock return (zpool_standard_error(hdl, errno, msg)); 14694451Seschrock } 14704451Seschrock 14714451Seschrock /* 14722082Seschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 14732082Seschrock * a hot spare. 14742082Seschrock */ 14752082Seschrock static boolean_t 14762082Seschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 14772082Seschrock { 14782082Seschrock nvlist_t **child; 14792082Seschrock uint_t c, children; 14802082Seschrock char *type; 14812082Seschrock 14822082Seschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 14832082Seschrock &children) == 0) { 14842082Seschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 14852082Seschrock &type) == 0); 14862082Seschrock 14872082Seschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 14882082Seschrock children == 2 && child[which] == tgt) 14892082Seschrock return (B_TRUE); 14902082Seschrock 14912082Seschrock for (c = 0; c < children; c++) 14922082Seschrock if (is_replacing_spare(child[c], tgt, which)) 14932082Seschrock return (B_TRUE); 1494789Sahrens } 14952082Seschrock 14962082Seschrock return (B_FALSE); 1497789Sahrens } 1498789Sahrens 1499789Sahrens /* 1500789Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 15014527Sperrin * If 'replacing' is specified, the new disk will replace the old one. 1502789Sahrens */ 1503789Sahrens int 1504789Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 1505789Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1506789Sahrens { 1507789Sahrens zfs_cmd_t zc = { 0 }; 1508789Sahrens char msg[1024]; 1509789Sahrens int ret; 15102082Seschrock nvlist_t *tgt; 15115450Sbrendan boolean_t avail_spare, l2cache; 15124527Sperrin uint64_t val, is_log; 15132082Seschrock char *path; 15142082Seschrock nvlist_t **child; 15152082Seschrock uint_t children; 15162082Seschrock nvlist_t *config_root; 15172082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1518789Sahrens 15191544Seschrock if (replacing) 15201544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 15211544Seschrock "cannot replace %s with %s"), old_disk, new_disk); 15221544Seschrock else 15231544Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 15241544Seschrock "cannot attach %s to %s"), new_disk, old_disk); 15251544Seschrock 1526789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 15275450Sbrendan if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0) 15282082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 15292082Seschrock 15302468Sek110237 if (avail_spare) 15312082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 15322082Seschrock 15335450Sbrendan if (l2cache) 15345450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 15355450Sbrendan 15362082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 15372082Seschrock zc.zc_cookie = replacing; 15382082Seschrock 15392082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 15402082Seschrock &child, &children) != 0 || children != 1) { 15412082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15422082Seschrock "new device must be a single disk")); 15432082Seschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 15441544Seschrock } 15452082Seschrock 15462082Seschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 15472082Seschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 15482082Seschrock 15492082Seschrock /* 15502082Seschrock * If the target is a hot spare that has been swapped in, we can only 15512082Seschrock * replace it with another hot spare. 15522082Seschrock */ 15532082Seschrock if (replacing && 15542082Seschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 15552082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 15565450Sbrendan (zpool_find_vdev(zhp, path, &avail_spare, &l2cache) == NULL || 15572468Sek110237 !avail_spare) && is_replacing_spare(config_root, tgt, 1)) { 15582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15592082Seschrock "can only be replaced by another hot spare")); 15602082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 15612082Seschrock } 15622082Seschrock 15632082Seschrock /* 15642082Seschrock * If we are attempting to replace a spare, it canot be applied to an 15652082Seschrock * already spared device. 15662082Seschrock */ 15672082Seschrock if (replacing && 15682082Seschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 15695450Sbrendan zpool_find_vdev(zhp, path, &avail_spare, &l2cache) != NULL && 15705450Sbrendan avail_spare && is_replacing_spare(config_root, tgt, 0)) { 15712082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15722082Seschrock "device has already been replaced with a spare")); 15732082Seschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 15742082Seschrock } 1575789Sahrens 15765094Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 15772082Seschrock return (-1); 1578789Sahrens 15794543Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1580789Sahrens 15812676Seschrock zcmd_free_nvlists(&zc); 1582789Sahrens 1583789Sahrens if (ret == 0) 1584789Sahrens return (0); 1585789Sahrens 1586789Sahrens switch (errno) { 15871544Seschrock case ENOTSUP: 1588789Sahrens /* 1589789Sahrens * Can't attach to or replace this type of vdev. 1590789Sahrens */ 15914527Sperrin if (replacing) { 15924527Sperrin is_log = B_FALSE; 15934527Sperrin (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG, 15944527Sperrin &is_log); 15954527Sperrin if (is_log) 15964527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15974527Sperrin "cannot replace a log with a spare")); 15984527Sperrin else 15994527Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16004527Sperrin "cannot replace a replacing device")); 16014527Sperrin } else { 16022082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16032082Seschrock "can only attach to mirrors and top-level " 16042082Seschrock "disks")); 16054527Sperrin } 16062082Seschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1607789Sahrens break; 1608789Sahrens 16091544Seschrock case EINVAL: 1610789Sahrens /* 1611789Sahrens * The new device must be a single disk. 1612789Sahrens */ 16132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16142082Seschrock "new device must be a single disk")); 16152082Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1616789Sahrens break; 1617789Sahrens 16181544Seschrock case EBUSY: 16192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 16202082Seschrock new_disk); 16212082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1622789Sahrens break; 1623789Sahrens 16241544Seschrock case EOVERFLOW: 1625789Sahrens /* 1626789Sahrens * The new device is too small. 1627789Sahrens */ 16282082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16292082Seschrock "device is too small")); 16302082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1631789Sahrens break; 1632789Sahrens 16331544Seschrock case EDOM: 1634789Sahrens /* 1635789Sahrens * The new device has a different alignment requirement. 1636789Sahrens */ 16372082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16382082Seschrock "devices have different sector alignment")); 16392082Seschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1640789Sahrens break; 1641789Sahrens 16421544Seschrock case ENAMETOOLONG: 1643789Sahrens /* 1644789Sahrens * The resulting top-level vdev spec won't fit in the label. 1645789Sahrens */ 16462082Seschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 1647789Sahrens break; 1648789Sahrens 16491544Seschrock default: 16502082Seschrock (void) zpool_standard_error(hdl, errno, msg); 1651789Sahrens } 1652789Sahrens 16532082Seschrock return (-1); 1654789Sahrens } 1655789Sahrens 1656789Sahrens /* 1657789Sahrens * Detach the specified device. 1658789Sahrens */ 1659789Sahrens int 1660789Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 1661789Sahrens { 1662789Sahrens zfs_cmd_t zc = { 0 }; 1663789Sahrens char msg[1024]; 16642082Seschrock nvlist_t *tgt; 16655450Sbrendan boolean_t avail_spare, l2cache; 16662082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1667789Sahrens 16681544Seschrock (void) snprintf(msg, sizeof (msg), 16691544Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 16701544Seschrock 1671789Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16725450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0) 16732082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1674789Sahrens 16752468Sek110237 if (avail_spare) 16762082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 16772082Seschrock 16785450Sbrendan if (l2cache) 16795450Sbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 16805450Sbrendan 16812082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 16822082Seschrock 16834543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 1684789Sahrens return (0); 1685789Sahrens 1686789Sahrens switch (errno) { 1687789Sahrens 16881544Seschrock case ENOTSUP: 1689789Sahrens /* 1690789Sahrens * Can't detach from this type of vdev. 1691789Sahrens */ 16922082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 16932082Seschrock "applicable to mirror and replacing vdevs")); 16942082Seschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 1695789Sahrens break; 1696789Sahrens 16971544Seschrock case EBUSY: 1698789Sahrens /* 1699789Sahrens * There are no other replicas of this device. 1700789Sahrens */ 17012082Seschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 1702789Sahrens break; 1703789Sahrens 17041544Seschrock default: 17052082Seschrock (void) zpool_standard_error(hdl, errno, msg); 17061544Seschrock } 17071544Seschrock 17082082Seschrock return (-1); 17092082Seschrock } 17102082Seschrock 17112082Seschrock /* 17125450Sbrendan * Remove the given device. Currently, this is supported only for hot spares 17135450Sbrendan * and level 2 cache devices. 17142082Seschrock */ 17152082Seschrock int 17162082Seschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 17172082Seschrock { 17182082Seschrock zfs_cmd_t zc = { 0 }; 17192082Seschrock char msg[1024]; 17202082Seschrock nvlist_t *tgt; 17215450Sbrendan boolean_t avail_spare, l2cache; 17222082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17232082Seschrock 17242082Seschrock (void) snprintf(msg, sizeof (msg), 17252082Seschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 17262082Seschrock 17272082Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17285450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0) 17292082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 17302082Seschrock 17315450Sbrendan if (!avail_spare && !l2cache) { 17322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17335450Sbrendan "only inactive hot spares or cache devices " 17345450Sbrendan "can be removed")); 17352082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 17362082Seschrock } 17372082Seschrock 17382082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 17392082Seschrock 17404543Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 17412082Seschrock return (0); 17422082Seschrock 17432082Seschrock return (zpool_standard_error(hdl, errno, msg)); 17441544Seschrock } 17451544Seschrock 17461544Seschrock /* 17471544Seschrock * Clear the errors for the pool, or the particular device if specified. 17481544Seschrock */ 17491544Seschrock int 17501544Seschrock zpool_clear(zpool_handle_t *zhp, const char *path) 17511544Seschrock { 17521544Seschrock zfs_cmd_t zc = { 0 }; 17531544Seschrock char msg[1024]; 17542082Seschrock nvlist_t *tgt; 17555450Sbrendan boolean_t avail_spare, l2cache; 17562082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17571544Seschrock 17581544Seschrock if (path) 17591544Seschrock (void) snprintf(msg, sizeof (msg), 17601544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 17612676Seschrock path); 17621544Seschrock else 17631544Seschrock (void) snprintf(msg, sizeof (msg), 17641544Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 17651544Seschrock zhp->zpool_name); 17661544Seschrock 17671544Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 17682082Seschrock if (path) { 17695450Sbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 17705450Sbrendan &l2cache)) == 0) 17712082Seschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 17722082Seschrock 17735450Sbrendan /* 17745450Sbrendan * Don't allow error clearing for hot spares. Do allow 17755450Sbrendan * error clearing for l2cache devices. 17765450Sbrendan */ 17772468Sek110237 if (avail_spare) 17782082Seschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 17792082Seschrock 17802082Seschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 17812082Seschrock &zc.zc_guid) == 0); 17821544Seschrock } 17831544Seschrock 17844543Smarks if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 17851544Seschrock return (0); 17861544Seschrock 17872082Seschrock return (zpool_standard_error(hdl, errno, msg)); 1788789Sahrens } 1789789Sahrens 17903126Sahl /* 17914451Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 17924451Seschrock */ 17934451Seschrock int 17944451Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 17954451Seschrock { 17964451Seschrock zfs_cmd_t zc = { 0 }; 17974451Seschrock char msg[1024]; 17984451Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 17994451Seschrock 18004451Seschrock (void) snprintf(msg, sizeof (msg), 18014451Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 18024451Seschrock guid); 18034451Seschrock 18044451Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18054451Seschrock zc.zc_guid = guid; 18064451Seschrock 18074451Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 18084451Seschrock return (0); 18094451Seschrock 18104451Seschrock return (zpool_standard_error(hdl, errno, msg)); 18114451Seschrock } 18124451Seschrock 18134451Seschrock /* 18143126Sahl * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 18153126Sahl * hierarchy. 18163126Sahl */ 18173126Sahl int 18183126Sahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 18193126Sahl void *data) 1820789Sahrens { 18213126Sahl libzfs_handle_t *hdl = zhp->zpool_hdl; 18223126Sahl char (*paths)[MAXPATHLEN]; 18233126Sahl size_t size = 4; 18243126Sahl int curr, fd, base, ret = 0; 18253126Sahl DIR *dirp; 18263126Sahl struct dirent *dp; 18273126Sahl struct stat st; 18283126Sahl 18293126Sahl if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 18303126Sahl return (errno == ENOENT ? 0 : -1); 18313126Sahl 18323126Sahl if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 18333126Sahl int err = errno; 18343126Sahl (void) close(base); 18353126Sahl return (err == ENOENT ? 0 : -1); 18363126Sahl } 1837789Sahrens 1838789Sahrens /* 18393126Sahl * Oddly this wasn't a directory -- ignore that failure since we 18403126Sahl * know there are no links lower in the (non-existant) hierarchy. 1841789Sahrens */ 18423126Sahl if (!S_ISDIR(st.st_mode)) { 18433126Sahl (void) close(base); 18443126Sahl return (0); 18453126Sahl } 18463126Sahl 18473126Sahl if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 18483126Sahl (void) close(base); 18493126Sahl return (-1); 1850789Sahrens } 1851789Sahrens 18523126Sahl (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 18533126Sahl curr = 0; 18543126Sahl 18553126Sahl while (curr >= 0) { 18563126Sahl if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 18573126Sahl goto err; 18583126Sahl 18593126Sahl if (S_ISDIR(st.st_mode)) { 18603126Sahl if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 18613126Sahl goto err; 18623126Sahl 18633126Sahl if ((dirp = fdopendir(fd)) == NULL) { 18643126Sahl (void) close(fd); 18653126Sahl goto err; 18663126Sahl } 18673126Sahl 18683126Sahl while ((dp = readdir(dirp)) != NULL) { 18693126Sahl if (dp->d_name[0] == '.') 18703126Sahl continue; 18713126Sahl 18723126Sahl if (curr + 1 == size) { 18733126Sahl paths = zfs_realloc(hdl, paths, 18743126Sahl size * sizeof (paths[0]), 18753126Sahl size * 2 * sizeof (paths[0])); 18763126Sahl if (paths == NULL) { 18773126Sahl (void) closedir(dirp); 18783126Sahl (void) close(fd); 18793126Sahl goto err; 18803126Sahl } 18813126Sahl 18823126Sahl size *= 2; 18833126Sahl } 18843126Sahl 18853126Sahl (void) strlcpy(paths[curr + 1], paths[curr], 18863126Sahl sizeof (paths[curr + 1])); 18873126Sahl (void) strlcat(paths[curr], "/", 18883126Sahl sizeof (paths[curr])); 18893126Sahl (void) strlcat(paths[curr], dp->d_name, 18903126Sahl sizeof (paths[curr])); 18913126Sahl curr++; 18923126Sahl } 18933126Sahl 18943126Sahl (void) closedir(dirp); 18953126Sahl 18963126Sahl } else { 18973126Sahl if ((ret = cb(paths[curr], data)) != 0) 18983126Sahl break; 18993126Sahl } 19003126Sahl 19013126Sahl curr--; 19023126Sahl } 19033126Sahl 19043126Sahl free(paths); 19053126Sahl (void) close(base); 19063126Sahl 19073126Sahl return (ret); 19083126Sahl 19093126Sahl err: 19103126Sahl free(paths); 19113126Sahl (void) close(base); 19123126Sahl return (-1); 19133126Sahl } 19143126Sahl 19153126Sahl typedef struct zvol_cb { 19163126Sahl zpool_handle_t *zcb_pool; 19173126Sahl boolean_t zcb_create; 19183126Sahl } zvol_cb_t; 19193126Sahl 19203126Sahl /*ARGSUSED*/ 19213126Sahl static int 19223126Sahl do_zvol_create(zfs_handle_t *zhp, void *data) 19233126Sahl { 19244657Sahrens int ret = 0; 19253126Sahl 19264657Sahrens if (ZFS_IS_VOLUME(zhp)) { 19273126Sahl (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 19284657Sahrens ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 19294657Sahrens } 19303126Sahl 19314657Sahrens if (ret == 0) 19324657Sahrens ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 1933789Sahrens 1934789Sahrens zfs_close(zhp); 19353126Sahl 1936789Sahrens return (ret); 1937789Sahrens } 1938789Sahrens 1939789Sahrens /* 1940789Sahrens * Iterate over all zvols in the pool and make any necessary minor nodes. 1941789Sahrens */ 1942789Sahrens int 1943789Sahrens zpool_create_zvol_links(zpool_handle_t *zhp) 1944789Sahrens { 1945789Sahrens zfs_handle_t *zfp; 1946789Sahrens int ret; 1947789Sahrens 1948789Sahrens /* 1949789Sahrens * If the pool is unavailable, just return success. 1950789Sahrens */ 19512082Seschrock if ((zfp = make_dataset_handle(zhp->zpool_hdl, 19522082Seschrock zhp->zpool_name)) == NULL) 1953789Sahrens return (0); 1954789Sahrens 19554657Sahrens ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 1956789Sahrens 1957789Sahrens zfs_close(zfp); 1958789Sahrens return (ret); 1959789Sahrens } 1960789Sahrens 19613126Sahl static int 19623126Sahl do_zvol_remove(const char *dataset, void *data) 19633126Sahl { 19643126Sahl zpool_handle_t *zhp = data; 19653126Sahl 19663126Sahl return (zvol_remove_link(zhp->zpool_hdl, dataset)); 19673126Sahl } 19683126Sahl 1969789Sahrens /* 19703126Sahl * Iterate over all zvols in the pool and remove any minor nodes. We iterate 19713126Sahl * by examining the /dev links so that a corrupted pool doesn't impede this 19723126Sahl * operation. 1973789Sahrens */ 1974789Sahrens int 1975789Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp) 1976789Sahrens { 19773126Sahl return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 1978789Sahrens } 19791354Seschrock 19801354Seschrock /* 19811354Seschrock * Convert from a devid string to a path. 19821354Seschrock */ 19831354Seschrock static char * 19841354Seschrock devid_to_path(char *devid_str) 19851354Seschrock { 19861354Seschrock ddi_devid_t devid; 19871354Seschrock char *minor; 19881354Seschrock char *path; 19891354Seschrock devid_nmlist_t *list = NULL; 19901354Seschrock int ret; 19911354Seschrock 19921354Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 19931354Seschrock return (NULL); 19941354Seschrock 19951354Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 19961354Seschrock 19971354Seschrock devid_str_free(minor); 19981354Seschrock devid_free(devid); 19991354Seschrock 20001354Seschrock if (ret != 0) 20011354Seschrock return (NULL); 20021354Seschrock 20032082Seschrock if ((path = strdup(list[0].devname)) == NULL) 20042082Seschrock return (NULL); 20052082Seschrock 20061354Seschrock devid_free_nmlist(list); 20071354Seschrock 20081354Seschrock return (path); 20091354Seschrock } 20101354Seschrock 20111354Seschrock /* 20121354Seschrock * Convert from a path to a devid string. 20131354Seschrock */ 20141354Seschrock static char * 20151354Seschrock path_to_devid(const char *path) 20161354Seschrock { 20171354Seschrock int fd; 20181354Seschrock ddi_devid_t devid; 20191354Seschrock char *minor, *ret; 20201354Seschrock 20211354Seschrock if ((fd = open(path, O_RDONLY)) < 0) 20221354Seschrock return (NULL); 20231354Seschrock 20241354Seschrock minor = NULL; 20251354Seschrock ret = NULL; 20261354Seschrock if (devid_get(fd, &devid) == 0) { 20271354Seschrock if (devid_get_minor_name(fd, &minor) == 0) 20281354Seschrock ret = devid_str_encode(devid, minor); 20291354Seschrock if (minor != NULL) 20301354Seschrock devid_str_free(minor); 20311354Seschrock devid_free(devid); 20321354Seschrock } 20331354Seschrock (void) close(fd); 20341354Seschrock 20351354Seschrock return (ret); 20361354Seschrock } 20371354Seschrock 20381354Seschrock /* 20391354Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 20401354Seschrock * ignore any failure here, since a common case is for an unprivileged user to 20411354Seschrock * type 'zpool status', and we'll display the correct information anyway. 20421354Seschrock */ 20431354Seschrock static void 20441354Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 20451354Seschrock { 20461354Seschrock zfs_cmd_t zc = { 0 }; 20471354Seschrock 20481354Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20492676Seschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 20501354Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 20511544Seschrock &zc.zc_guid) == 0); 20521354Seschrock 20532082Seschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 20541354Seschrock } 20551354Seschrock 20561354Seschrock /* 20571354Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 20581354Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 20591354Seschrock * We also check if this is a whole disk, in which case we strip off the 20601354Seschrock * trailing 's0' slice name. 20611354Seschrock * 20621354Seschrock * This routine is also responsible for identifying when disks have been 20631354Seschrock * reconfigured in a new location. The kernel will have opened the device by 20641354Seschrock * devid, but the path will still refer to the old location. To catch this, we 20651354Seschrock * first do a path -> devid translation (which is fast for the common case). If 20661354Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 20671354Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 20681354Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 20691354Seschrock * of these checks. 20701354Seschrock */ 20711354Seschrock char * 20722082Seschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 20731354Seschrock { 20741354Seschrock char *path, *devid; 20751544Seschrock uint64_t value; 20761544Seschrock char buf[64]; 20774451Seschrock vdev_stat_t *vs; 20784451Seschrock uint_t vsc; 20791354Seschrock 20801544Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 20811544Seschrock &value) == 0) { 20821544Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 20831544Seschrock &value) == 0); 20842856Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 20852856Snd150628 (u_longlong_t)value); 20861544Seschrock path = buf; 20871544Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 20881354Seschrock 20894451Seschrock /* 20904451Seschrock * If the device is dead (faulted, offline, etc) then don't 20914451Seschrock * bother opening it. Otherwise we may be forcing the user to 20924451Seschrock * open a misbehaving device, which can have undesirable 20934451Seschrock * effects. 20944451Seschrock */ 20954451Seschrock if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 20964451Seschrock (uint64_t **)&vs, &vsc) != 0 || 20974451Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 20984451Seschrock zhp != NULL && 20991354Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 21001354Seschrock /* 21011354Seschrock * Determine if the current path is correct. 21021354Seschrock */ 21031354Seschrock char *newdevid = path_to_devid(path); 21041354Seschrock 21051354Seschrock if (newdevid == NULL || 21061354Seschrock strcmp(devid, newdevid) != 0) { 21071354Seschrock char *newpath; 21081354Seschrock 21091354Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 21101354Seschrock /* 21111354Seschrock * Update the path appropriately. 21121354Seschrock */ 21131354Seschrock set_path(zhp, nv, newpath); 21142082Seschrock if (nvlist_add_string(nv, 21152082Seschrock ZPOOL_CONFIG_PATH, newpath) == 0) 21162082Seschrock verify(nvlist_lookup_string(nv, 21172082Seschrock ZPOOL_CONFIG_PATH, 21182082Seschrock &path) == 0); 21191354Seschrock free(newpath); 21201354Seschrock } 21211354Seschrock } 21221354Seschrock 21232082Seschrock if (newdevid) 21242082Seschrock devid_str_free(newdevid); 21251354Seschrock } 21261354Seschrock 21271354Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 21281354Seschrock path += 9; 21291354Seschrock 21301354Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 21311544Seschrock &value) == 0 && value) { 21322082Seschrock char *tmp = zfs_strdup(hdl, path); 21332082Seschrock if (tmp == NULL) 21342082Seschrock return (NULL); 21351354Seschrock tmp[strlen(path) - 2] = '\0'; 21361354Seschrock return (tmp); 21371354Seschrock } 21381354Seschrock } else { 21391354Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 21402082Seschrock 21412082Seschrock /* 21422082Seschrock * If it's a raidz device, we need to stick in the parity level. 21432082Seschrock */ 21442082Seschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 21452082Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 21462082Seschrock &value) == 0); 21472082Seschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 21482856Snd150628 (u_longlong_t)value); 21492082Seschrock path = buf; 21502082Seschrock } 21511354Seschrock } 21521354Seschrock 21532082Seschrock return (zfs_strdup(hdl, path)); 21541354Seschrock } 21551544Seschrock 21561544Seschrock static int 21571544Seschrock zbookmark_compare(const void *a, const void *b) 21581544Seschrock { 21591544Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 21601544Seschrock } 21611544Seschrock 21621544Seschrock /* 21631544Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 21641544Seschrock * caller. 21651544Seschrock */ 21661544Seschrock int 21673444Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 21681544Seschrock { 21691544Seschrock zfs_cmd_t zc = { 0 }; 21701544Seschrock uint64_t count; 21712676Seschrock zbookmark_t *zb = NULL; 21723444Sek110237 int i; 21731544Seschrock 21741544Seschrock /* 21751544Seschrock * Retrieve the raw error list from the kernel. If the number of errors 21761544Seschrock * has increased, allocate more space and continue until we get the 21771544Seschrock * entire list. 21781544Seschrock */ 21791544Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 21801544Seschrock &count) == 0); 21814820Sek110237 if (count == 0) 21824820Sek110237 return (0); 21832676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 21842856Snd150628 count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 21852082Seschrock return (-1); 21862676Seschrock zc.zc_nvlist_dst_size = count; 21871544Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 21881544Seschrock for (;;) { 21892082Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 21902082Seschrock &zc) != 0) { 21912676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 21921544Seschrock if (errno == ENOMEM) { 21933823Svb160487 count = zc.zc_nvlist_dst_size; 21942676Seschrock if ((zc.zc_nvlist_dst = (uintptr_t) 21953823Svb160487 zfs_alloc(zhp->zpool_hdl, count * 21963823Svb160487 sizeof (zbookmark_t))) == (uintptr_t)NULL) 21972082Seschrock return (-1); 21981544Seschrock } else { 21991544Seschrock return (-1); 22001544Seschrock } 22011544Seschrock } else { 22021544Seschrock break; 22031544Seschrock } 22041544Seschrock } 22051544Seschrock 22061544Seschrock /* 22071544Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 22081544Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 22092676Seschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 22101544Seschrock * _not_ copied as part of the process. So we point the start of our 22111544Seschrock * array appropriate and decrement the total number of elements. 22121544Seschrock */ 22132676Seschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 22142676Seschrock zc.zc_nvlist_dst_size; 22152676Seschrock count -= zc.zc_nvlist_dst_size; 22161544Seschrock 22171544Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 22181544Seschrock 22193444Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 22201544Seschrock 22211544Seschrock /* 22223444Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 22231544Seschrock */ 22241544Seschrock for (i = 0; i < count; i++) { 22251544Seschrock nvlist_t *nv; 22261544Seschrock 22273700Sek110237 /* ignoring zb_blkid and zb_level for now */ 22283700Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 22293700Sek110237 zb[i-1].zb_object == zb[i].zb_object) 22301544Seschrock continue; 22311544Seschrock 22323444Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 22333444Sek110237 goto nomem; 22343444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 22353444Sek110237 zb[i].zb_objset) != 0) { 22363444Sek110237 nvlist_free(nv); 22372082Seschrock goto nomem; 22383444Sek110237 } 22393444Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 22403444Sek110237 zb[i].zb_object) != 0) { 22413444Sek110237 nvlist_free(nv); 22423444Sek110237 goto nomem; 22431544Seschrock } 22443444Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 22453444Sek110237 nvlist_free(nv); 22463444Sek110237 goto nomem; 22473444Sek110237 } 22483444Sek110237 nvlist_free(nv); 22491544Seschrock } 22501544Seschrock 22513265Sahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 22521544Seschrock return (0); 22532082Seschrock 22542082Seschrock nomem: 22552676Seschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 22562082Seschrock return (no_memory(zhp->zpool_hdl)); 22571544Seschrock } 22581760Seschrock 22591760Seschrock /* 22601760Seschrock * Upgrade a ZFS pool to the latest on-disk version. 22611760Seschrock */ 22621760Seschrock int 22635094Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 22641760Seschrock { 22651760Seschrock zfs_cmd_t zc = { 0 }; 22662082Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 22671760Seschrock 22681760Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 22695094Slling zc.zc_cookie = new_version; 22705094Slling 22714543Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 22723237Slling return (zpool_standard_error_fmt(hdl, errno, 22732082Seschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 22742082Seschrock zhp->zpool_name)); 22751760Seschrock return (0); 22761760Seschrock } 22772926Sek110237 22784988Sek110237 void 22794988Sek110237 zpool_set_history_str(const char *subcommand, int argc, char **argv, 22804988Sek110237 char *history_str) 22814988Sek110237 { 22824988Sek110237 int i; 22834988Sek110237 22844988Sek110237 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 22854988Sek110237 for (i = 1; i < argc; i++) { 22864988Sek110237 if (strlen(history_str) + 1 + strlen(argv[i]) > 22874988Sek110237 HIS_MAX_RECORD_LEN) 22884988Sek110237 break; 22894988Sek110237 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 22904988Sek110237 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 22914988Sek110237 } 22924988Sek110237 } 22934988Sek110237 22942926Sek110237 /* 22954988Sek110237 * Stage command history for logging. 22962926Sek110237 */ 22974988Sek110237 int 22984988Sek110237 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 22992926Sek110237 { 23004988Sek110237 if (history_str == NULL) 23014988Sek110237 return (EINVAL); 23024988Sek110237 23034988Sek110237 if (strlen(history_str) > HIS_MAX_RECORD_LEN) 23044988Sek110237 return (EINVAL); 23052926Sek110237 23064715Sek110237 if (hdl->libzfs_log_str != NULL) 23074543Smarks free(hdl->libzfs_log_str); 23082926Sek110237 23094988Sek110237 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 23104988Sek110237 return (no_memory(hdl)); 23114543Smarks 23124988Sek110237 return (0); 23132926Sek110237 } 23142926Sek110237 23152926Sek110237 /* 23162926Sek110237 * Perform ioctl to get some command history of a pool. 23172926Sek110237 * 23182926Sek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 23192926Sek110237 * logical offset of the history buffer to start reading from. 23202926Sek110237 * 23212926Sek110237 * Upon return, 'off' is the next logical offset to read from and 23222926Sek110237 * 'len' is the actual amount of bytes read into 'buf'. 23232926Sek110237 */ 23242926Sek110237 static int 23252926Sek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 23262926Sek110237 { 23272926Sek110237 zfs_cmd_t zc = { 0 }; 23282926Sek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 23292926Sek110237 23302926Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 23312926Sek110237 23322926Sek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 23332926Sek110237 zc.zc_history_len = *len; 23342926Sek110237 zc.zc_history_offset = *off; 23352926Sek110237 23362926Sek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 23372926Sek110237 switch (errno) { 23382926Sek110237 case EPERM: 23393237Slling return (zfs_error_fmt(hdl, EZFS_PERM, 23403237Slling dgettext(TEXT_DOMAIN, 23412926Sek110237 "cannot show history for pool '%s'"), 23422926Sek110237 zhp->zpool_name)); 23432926Sek110237 case ENOENT: 23443237Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 23452926Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 23462926Sek110237 "'%s'"), zhp->zpool_name)); 23473863Sek110237 case ENOTSUP: 23483863Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 23493863Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 23503863Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 23512926Sek110237 default: 23523237Slling return (zpool_standard_error_fmt(hdl, errno, 23532926Sek110237 dgettext(TEXT_DOMAIN, 23542926Sek110237 "cannot get history for '%s'"), zhp->zpool_name)); 23552926Sek110237 } 23562926Sek110237 } 23572926Sek110237 23582926Sek110237 *len = zc.zc_history_len; 23592926Sek110237 *off = zc.zc_history_offset; 23602926Sek110237 23612926Sek110237 return (0); 23622926Sek110237 } 23632926Sek110237 23642926Sek110237 /* 23652926Sek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 23662926Sek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 23672926Sek110237 * processed as there wasn't a complete record. 23682926Sek110237 */ 23692926Sek110237 static int 23702926Sek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 23712926Sek110237 nvlist_t ***records, uint_t *numrecords) 23722926Sek110237 { 23732926Sek110237 uint64_t reclen; 23742926Sek110237 nvlist_t *nv; 23752926Sek110237 int i; 23762926Sek110237 23772926Sek110237 while (bytes_read > sizeof (reclen)) { 23782926Sek110237 23792926Sek110237 /* get length of packed record (stored as little endian) */ 23802926Sek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 23812926Sek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 23822926Sek110237 23832926Sek110237 if (bytes_read < sizeof (reclen) + reclen) 23842926Sek110237 break; 23852926Sek110237 23862926Sek110237 /* unpack record */ 23872926Sek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 23882926Sek110237 return (ENOMEM); 23892926Sek110237 bytes_read -= sizeof (reclen) + reclen; 23902926Sek110237 buf += sizeof (reclen) + reclen; 23912926Sek110237 23922926Sek110237 /* add record to nvlist array */ 23932926Sek110237 (*numrecords)++; 23942926Sek110237 if (ISP2(*numrecords + 1)) { 23952926Sek110237 *records = realloc(*records, 23962926Sek110237 *numrecords * 2 * sizeof (nvlist_t *)); 23972926Sek110237 } 23982926Sek110237 (*records)[*numrecords - 1] = nv; 23992926Sek110237 } 24002926Sek110237 24012926Sek110237 *leftover = bytes_read; 24022926Sek110237 return (0); 24032926Sek110237 } 24042926Sek110237 24052926Sek110237 #define HIS_BUF_LEN (128*1024) 24062926Sek110237 24072926Sek110237 /* 24082926Sek110237 * Retrieve the command history of a pool. 24092926Sek110237 */ 24102926Sek110237 int 24112926Sek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 24122926Sek110237 { 24132926Sek110237 char buf[HIS_BUF_LEN]; 24142926Sek110237 uint64_t off = 0; 24152926Sek110237 nvlist_t **records = NULL; 24162926Sek110237 uint_t numrecords = 0; 24172926Sek110237 int err, i; 24182926Sek110237 24192926Sek110237 do { 24202926Sek110237 uint64_t bytes_read = sizeof (buf); 24212926Sek110237 uint64_t leftover; 24222926Sek110237 24232926Sek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 24242926Sek110237 break; 24252926Sek110237 24262926Sek110237 /* if nothing else was read in, we're at EOF, just return */ 24272926Sek110237 if (!bytes_read) 24282926Sek110237 break; 24292926Sek110237 24302926Sek110237 if ((err = zpool_history_unpack(buf, bytes_read, 24312926Sek110237 &leftover, &records, &numrecords)) != 0) 24322926Sek110237 break; 24332926Sek110237 off -= leftover; 24342926Sek110237 24352926Sek110237 /* CONSTCOND */ 24362926Sek110237 } while (1); 24372926Sek110237 24382926Sek110237 if (!err) { 24392926Sek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 24402926Sek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 24412926Sek110237 records, numrecords) == 0); 24422926Sek110237 } 24432926Sek110237 for (i = 0; i < numrecords; i++) 24442926Sek110237 nvlist_free(records[i]); 24452926Sek110237 free(records); 24462926Sek110237 24472926Sek110237 return (err); 24482926Sek110237 } 24493444Sek110237 24503444Sek110237 void 24513444Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 24523444Sek110237 char *pathname, size_t len) 24533444Sek110237 { 24543444Sek110237 zfs_cmd_t zc = { 0 }; 24553444Sek110237 boolean_t mounted = B_FALSE; 24563444Sek110237 char *mntpnt = NULL; 24573444Sek110237 char dsname[MAXNAMELEN]; 24583444Sek110237 24593444Sek110237 if (dsobj == 0) { 24603444Sek110237 /* special case for the MOS */ 24613444Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 24623444Sek110237 return; 24633444Sek110237 } 24643444Sek110237 24653444Sek110237 /* get the dataset's name */ 24663444Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 24673444Sek110237 zc.zc_obj = dsobj; 24683444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 24693444Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 24703444Sek110237 /* just write out a path of two object numbers */ 24713444Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 24723444Sek110237 dsobj, obj); 24733444Sek110237 return; 24743444Sek110237 } 24753444Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 24763444Sek110237 24773444Sek110237 /* find out if the dataset is mounted */ 24783444Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 24793444Sek110237 24803444Sek110237 /* get the corrupted object's path */ 24813444Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 24823444Sek110237 zc.zc_obj = obj; 24833444Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 24843444Sek110237 &zc) == 0) { 24853444Sek110237 if (mounted) { 24863444Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 24873444Sek110237 zc.zc_value); 24883444Sek110237 } else { 24893444Sek110237 (void) snprintf(pathname, len, "%s:%s", 24903444Sek110237 dsname, zc.zc_value); 24913444Sek110237 } 24923444Sek110237 } else { 24933444Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 24943444Sek110237 } 24953444Sek110237 free(mntpnt); 24963444Sek110237 } 24973912Slling 24984276Staylor #define RDISK_ROOT "/dev/rdsk" 24994276Staylor #define BACKUP_SLICE "s2" 25004276Staylor /* 25014276Staylor * Don't start the slice at the default block of 34; many storage 25024276Staylor * devices will use a stripe width of 128k, so start there instead. 25034276Staylor */ 25044276Staylor #define NEW_START_BLOCK 256 25054276Staylor 25064276Staylor /* 25074276Staylor * determine where a partition starts on a disk in the current 25084276Staylor * configuration 25094276Staylor */ 25104276Staylor static diskaddr_t 25114276Staylor find_start_block(nvlist_t *config) 25124276Staylor { 25134276Staylor nvlist_t **child; 25144276Staylor uint_t c, children; 25154276Staylor char *path; 25164276Staylor diskaddr_t sb = MAXOFFSET_T; 25174276Staylor int fd; 25184276Staylor char diskname[MAXPATHLEN]; 25194276Staylor uint64_t wholedisk; 25204276Staylor 25214276Staylor if (nvlist_lookup_nvlist_array(config, 25224276Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 25234276Staylor if (nvlist_lookup_uint64(config, 25244276Staylor ZPOOL_CONFIG_WHOLE_DISK, 25254276Staylor &wholedisk) != 0 || !wholedisk) { 25264276Staylor return (MAXOFFSET_T); 25274276Staylor } 25284276Staylor if (nvlist_lookup_string(config, 25294276Staylor ZPOOL_CONFIG_PATH, &path) != 0) { 25304276Staylor return (MAXOFFSET_T); 25314276Staylor } 25324276Staylor 25334276Staylor (void) snprintf(diskname, sizeof (diskname), "%s%s", 25344276Staylor RDISK_ROOT, strrchr(path, '/')); 25354276Staylor if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 25364276Staylor struct dk_gpt *vtoc; 25374276Staylor if (efi_alloc_and_read(fd, &vtoc) >= 0) { 25384276Staylor sb = vtoc->efi_parts[0].p_start; 25394276Staylor efi_free(vtoc); 25404276Staylor } 25414276Staylor (void) close(fd); 25424276Staylor } 25434276Staylor return (sb); 25444276Staylor } 25454276Staylor 25464276Staylor for (c = 0; c < children; c++) { 25474276Staylor sb = find_start_block(child[c]); 25484276Staylor if (sb != MAXOFFSET_T) { 25494276Staylor return (sb); 25504276Staylor } 25514276Staylor } 25524276Staylor return (MAXOFFSET_T); 25534276Staylor } 25544276Staylor 25554276Staylor /* 25564276Staylor * Label an individual disk. The name provided is the short name, 25574276Staylor * stripped of any leading /dev path. 25584276Staylor */ 25594276Staylor int 25604276Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 25614276Staylor { 25624276Staylor char path[MAXPATHLEN]; 25634276Staylor struct dk_gpt *vtoc; 25644276Staylor int fd; 25654276Staylor size_t resv = EFI_MIN_RESV_SIZE; 25664276Staylor uint64_t slice_size; 25674276Staylor diskaddr_t start_block; 25684276Staylor char errbuf[1024]; 25694276Staylor 25706289Smmusante /* prepare an error message just in case */ 25716289Smmusante (void) snprintf(errbuf, sizeof (errbuf), 25726289Smmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 25736289Smmusante 25744276Staylor if (zhp) { 25754276Staylor nvlist_t *nvroot; 25764276Staylor 25774276Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 25784276Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 25794276Staylor 25804276Staylor if (zhp->zpool_start_block == 0) 25814276Staylor start_block = find_start_block(nvroot); 25824276Staylor else 25834276Staylor start_block = zhp->zpool_start_block; 25844276Staylor zhp->zpool_start_block = start_block; 25854276Staylor } else { 25864276Staylor /* new pool */ 25874276Staylor start_block = NEW_START_BLOCK; 25884276Staylor } 25894276Staylor 25904276Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 25914276Staylor BACKUP_SLICE); 25924276Staylor 25934276Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 25944276Staylor /* 25954276Staylor * This shouldn't happen. We've long since verified that this 25964276Staylor * is a valid device. 25974276Staylor */ 25986289Smmusante zfs_error_aux(hdl, 25996289Smmusante dgettext(TEXT_DOMAIN, "unable to open device")); 26004276Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 26014276Staylor } 26024276Staylor 26034276Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 26044276Staylor /* 26054276Staylor * The only way this can fail is if we run out of memory, or we 26064276Staylor * were unable to read the disk's capacity 26074276Staylor */ 26084276Staylor if (errno == ENOMEM) 26094276Staylor (void) no_memory(hdl); 26104276Staylor 26114276Staylor (void) close(fd); 26126289Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26136289Smmusante "unable to read disk capacity"), name); 26144276Staylor 26154276Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 26164276Staylor } 26174276Staylor 26184276Staylor slice_size = vtoc->efi_last_u_lba + 1; 26194276Staylor slice_size -= EFI_MIN_RESV_SIZE; 26204276Staylor if (start_block == MAXOFFSET_T) 26214276Staylor start_block = NEW_START_BLOCK; 26224276Staylor slice_size -= start_block; 26234276Staylor 26244276Staylor vtoc->efi_parts[0].p_start = start_block; 26254276Staylor vtoc->efi_parts[0].p_size = slice_size; 26264276Staylor 26274276Staylor /* 26284276Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 26294276Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 26304276Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 26314276Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 26324276Staylor * etc. were all pretty specific. V_USR is as close to reality as we 26334276Staylor * can get, in the absence of V_OTHER. 26344276Staylor */ 26354276Staylor vtoc->efi_parts[0].p_tag = V_USR; 26364276Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 26374276Staylor 26384276Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 26394276Staylor vtoc->efi_parts[8].p_size = resv; 26404276Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 26414276Staylor 26424276Staylor if (efi_write(fd, vtoc) != 0) { 26434276Staylor /* 26444276Staylor * Some block drivers (like pcata) may not support EFI 26454276Staylor * GPT labels. Print out a helpful error message dir- 26464276Staylor * ecting the user to manually label the disk and give 26474276Staylor * a specific slice. 26484276Staylor */ 26494276Staylor (void) close(fd); 26504276Staylor efi_free(vtoc); 26514276Staylor 26524276Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26536289Smmusante "try using fdisk(1M) and then provide a specific slice")); 26544276Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 26554276Staylor } 26564276Staylor 26574276Staylor (void) close(fd); 26584276Staylor efi_free(vtoc); 26594276Staylor return (0); 26604276Staylor } 2661*6423Sgw25295 2662*6423Sgw25295 static boolean_t 2663*6423Sgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 2664*6423Sgw25295 { 2665*6423Sgw25295 char *type; 2666*6423Sgw25295 nvlist_t **child; 2667*6423Sgw25295 uint_t children, c; 2668*6423Sgw25295 2669*6423Sgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 2670*6423Sgw25295 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 2671*6423Sgw25295 strcmp(type, VDEV_TYPE_FILE) == 0 || 2672*6423Sgw25295 strcmp(type, VDEV_TYPE_LOG) == 0 || 2673*6423Sgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 2674*6423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2675*6423Sgw25295 "vdev type '%s' is not supported"), type); 2676*6423Sgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 2677*6423Sgw25295 return (B_FALSE); 2678*6423Sgw25295 } 2679*6423Sgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 2680*6423Sgw25295 &child, &children) == 0) { 2681*6423Sgw25295 for (c = 0; c < children; c++) { 2682*6423Sgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 2683*6423Sgw25295 return (B_FALSE); 2684*6423Sgw25295 } 2685*6423Sgw25295 } 2686*6423Sgw25295 return (B_TRUE); 2687*6423Sgw25295 } 2688*6423Sgw25295 2689*6423Sgw25295 /* 2690*6423Sgw25295 * check if this zvol is allowable for use as a dump device; zero if 2691*6423Sgw25295 * it is, > 0 if it isn't, < 0 if it isn't a zvol 2692*6423Sgw25295 */ 2693*6423Sgw25295 int 2694*6423Sgw25295 zvol_check_dump_config(char *arg) 2695*6423Sgw25295 { 2696*6423Sgw25295 zpool_handle_t *zhp = NULL; 2697*6423Sgw25295 nvlist_t *config, *nvroot; 2698*6423Sgw25295 char *p, *volname; 2699*6423Sgw25295 nvlist_t **top; 2700*6423Sgw25295 uint_t toplevels; 2701*6423Sgw25295 libzfs_handle_t *hdl; 2702*6423Sgw25295 char errbuf[1024]; 2703*6423Sgw25295 char poolname[ZPOOL_MAXNAMELEN]; 2704*6423Sgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 2705*6423Sgw25295 int ret = 1; 2706*6423Sgw25295 2707*6423Sgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 2708*6423Sgw25295 return (-1); 2709*6423Sgw25295 } 2710*6423Sgw25295 2711*6423Sgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2712*6423Sgw25295 "dump is not supported on device '%s'"), arg); 2713*6423Sgw25295 2714*6423Sgw25295 if ((hdl = libzfs_init()) == NULL) 2715*6423Sgw25295 return (1); 2716*6423Sgw25295 libzfs_print_on_error(hdl, B_TRUE); 2717*6423Sgw25295 2718*6423Sgw25295 volname = arg + pathlen; 2719*6423Sgw25295 2720*6423Sgw25295 /* check the configuration of the pool */ 2721*6423Sgw25295 if ((p = strchr(volname, '/')) == NULL) { 2722*6423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2723*6423Sgw25295 "malformed dataset name")); 2724*6423Sgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 2725*6423Sgw25295 return (1); 2726*6423Sgw25295 } else if (p - volname >= ZFS_MAXNAMELEN) { 2727*6423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2728*6423Sgw25295 "dataset name is too long")); 2729*6423Sgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 2730*6423Sgw25295 return (1); 2731*6423Sgw25295 } else { 2732*6423Sgw25295 (void) strncpy(poolname, volname, p - volname); 2733*6423Sgw25295 poolname[p - volname] = '\0'; 2734*6423Sgw25295 } 2735*6423Sgw25295 2736*6423Sgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 2737*6423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2738*6423Sgw25295 "could not open pool '%s'"), poolname); 2739*6423Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 2740*6423Sgw25295 goto out; 2741*6423Sgw25295 } 2742*6423Sgw25295 config = zpool_get_config(zhp, NULL); 2743*6423Sgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2744*6423Sgw25295 &nvroot) != 0) { 2745*6423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2746*6423Sgw25295 "could not obtain vdev configuration for '%s'"), poolname); 2747*6423Sgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 2748*6423Sgw25295 goto out; 2749*6423Sgw25295 } 2750*6423Sgw25295 2751*6423Sgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2752*6423Sgw25295 &top, &toplevels) == 0); 2753*6423Sgw25295 if (toplevels != 1) { 2754*6423Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2755*6423Sgw25295 "'%s' has multiple top level vdevs"), poolname); 2756*6423Sgw25295 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 2757*6423Sgw25295 goto out; 2758*6423Sgw25295 } 2759*6423Sgw25295 2760*6423Sgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 2761*6423Sgw25295 goto out; 2762*6423Sgw25295 } 2763*6423Sgw25295 ret = 0; 2764*6423Sgw25295 2765*6423Sgw25295 out: 2766*6423Sgw25295 if (zhp) 2767*6423Sgw25295 zpool_close(zhp); 2768*6423Sgw25295 libzfs_fini(hdl); 2769*6423Sgw25295 return (ret); 2770*6423Sgw25295 } 2771