1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 23c6ef114fSmmusante * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24fa9e4066Sahrens * Use is subject to license terms. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28fa9e4066Sahrens 29f3861e1aSahl #include <alloca.h> 30fa9e4066Sahrens #include <assert.h> 31fa9e4066Sahrens #include <ctype.h> 32fa9e4066Sahrens #include <errno.h> 33fa9e4066Sahrens #include <devid.h> 34f3861e1aSahl #include <dirent.h> 35fa9e4066Sahrens #include <fcntl.h> 36fa9e4066Sahrens #include <libintl.h> 37fa9e4066Sahrens #include <stdio.h> 38fa9e4066Sahrens #include <stdlib.h> 39f3861e1aSahl #include <strings.h> 40fa9e4066Sahrens #include <unistd.h> 410a48a24eStimh #include <zone.h> 428488aeb5Staylor #include <sys/efi_partition.h> 438488aeb5Staylor #include <sys/vtoc.h> 44fa9e4066Sahrens #include <sys/zfs_ioctl.h> 45ea8dc4b6Seschrock #include <sys/zio.h> 4606eeb2adSek #include <strings.h> 47fa9e4066Sahrens 48fa9e4066Sahrens #include "zfs_namecheck.h" 49b1b8ab34Slling #include "zfs_prop.h" 50fa9e4066Sahrens #include "libzfs_impl.h" 51fa9e4066Sahrens 5215e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 53990b4856Slling 54990b4856Slling /* 55990b4856Slling * ==================================================================== 56990b4856Slling * zpool property functions 57990b4856Slling * ==================================================================== 58990b4856Slling */ 59990b4856Slling 60990b4856Slling static int 61990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 62990b4856Slling { 63990b4856Slling zfs_cmd_t zc = { 0 }; 64990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 65990b4856Slling 66990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 67990b4856Slling 68990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 69990b4856Slling return (-1); 70990b4856Slling 71990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 72990b4856Slling if (errno == ENOMEM) { 73990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 74990b4856Slling zcmd_free_nvlists(&zc); 75990b4856Slling return (-1); 76990b4856Slling } 77990b4856Slling } else { 78990b4856Slling zcmd_free_nvlists(&zc); 79990b4856Slling return (-1); 80990b4856Slling } 81990b4856Slling } 82990b4856Slling 83990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 84990b4856Slling zcmd_free_nvlists(&zc); 85990b4856Slling return (-1); 86990b4856Slling } 87990b4856Slling 88990b4856Slling zcmd_free_nvlists(&zc); 89990b4856Slling 90990b4856Slling return (0); 91990b4856Slling } 92990b4856Slling 93990b4856Slling static int 94990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 95990b4856Slling { 96990b4856Slling nvlist_t *old_props; 97990b4856Slling 98990b4856Slling old_props = zhp->zpool_props; 99990b4856Slling 100990b4856Slling if (zpool_get_all_props(zhp) != 0) 101990b4856Slling return (-1); 102990b4856Slling 103990b4856Slling nvlist_free(old_props); 104990b4856Slling return (0); 105990b4856Slling } 106990b4856Slling 107990b4856Slling static char * 108990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 109990b4856Slling zprop_source_t *src) 110990b4856Slling { 111990b4856Slling nvlist_t *nv, *nvl; 112990b4856Slling uint64_t ival; 113990b4856Slling char *value; 114990b4856Slling zprop_source_t source; 115990b4856Slling 116990b4856Slling nvl = zhp->zpool_props; 117990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 118990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 119990b4856Slling source = ival; 120990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 121990b4856Slling } else { 122990b4856Slling source = ZPROP_SRC_DEFAULT; 123990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 124990b4856Slling value = "-"; 125990b4856Slling } 126990b4856Slling 127990b4856Slling if (src) 128990b4856Slling *src = source; 129990b4856Slling 130990b4856Slling return (value); 131990b4856Slling } 132990b4856Slling 133990b4856Slling uint64_t 134990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 135990b4856Slling { 136990b4856Slling nvlist_t *nv, *nvl; 137990b4856Slling uint64_t value; 138990b4856Slling zprop_source_t source; 139990b4856Slling 140*b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 141*b87f3af3Sperrin /* 142*b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 143*b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 144*b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 145*b87f3af3Sperrin */ 146*b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 147*b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 148*b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 149*b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 150*b87f3af3Sperrin == 0)) { 151*b87f3af3Sperrin return (value); 152*b87f3af3Sperrin } 153990b4856Slling return (zpool_prop_default_numeric(prop)); 154*b87f3af3Sperrin } 155990b4856Slling 156990b4856Slling nvl = zhp->zpool_props; 157990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 158990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 159990b4856Slling source = value; 160990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 161990b4856Slling } else { 162990b4856Slling source = ZPROP_SRC_DEFAULT; 163990b4856Slling value = zpool_prop_default_numeric(prop); 164990b4856Slling } 165990b4856Slling 166990b4856Slling if (src) 167990b4856Slling *src = source; 168990b4856Slling 169990b4856Slling return (value); 170990b4856Slling } 171990b4856Slling 172990b4856Slling /* 173990b4856Slling * Map VDEV STATE to printed strings. 174990b4856Slling */ 175990b4856Slling char * 176990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 177990b4856Slling { 178990b4856Slling switch (state) { 179990b4856Slling case VDEV_STATE_CLOSED: 180990b4856Slling case VDEV_STATE_OFFLINE: 181990b4856Slling return (gettext("OFFLINE")); 182990b4856Slling case VDEV_STATE_REMOVED: 183990b4856Slling return (gettext("REMOVED")); 184990b4856Slling case VDEV_STATE_CANT_OPEN: 185*b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 186990b4856Slling return (gettext("FAULTED")); 187990b4856Slling else 188990b4856Slling return (gettext("UNAVAIL")); 189990b4856Slling case VDEV_STATE_FAULTED: 190990b4856Slling return (gettext("FAULTED")); 191990b4856Slling case VDEV_STATE_DEGRADED: 192990b4856Slling return (gettext("DEGRADED")); 193990b4856Slling case VDEV_STATE_HEALTHY: 194990b4856Slling return (gettext("ONLINE")); 195990b4856Slling } 196990b4856Slling 197990b4856Slling return (gettext("UNKNOWN")); 198990b4856Slling } 199990b4856Slling 200990b4856Slling /* 201990b4856Slling * Get a zpool property value for 'prop' and return the value in 202990b4856Slling * a pre-allocated buffer. 203990b4856Slling */ 204990b4856Slling int 205990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 206990b4856Slling zprop_source_t *srctype) 207990b4856Slling { 208990b4856Slling uint64_t intval; 209990b4856Slling const char *strval; 210990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 211990b4856Slling nvlist_t *nvroot; 212990b4856Slling vdev_stat_t *vs; 213990b4856Slling uint_t vsc; 214990b4856Slling 215990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 216990b4856Slling if (prop == ZPOOL_PROP_NAME) 217990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 218990b4856Slling else if (prop == ZPOOL_PROP_HEALTH) 219990b4856Slling (void) strlcpy(buf, "FAULTED", len); 220990b4856Slling else 221990b4856Slling (void) strlcpy(buf, "-", len); 222990b4856Slling return (0); 223990b4856Slling } 224990b4856Slling 225990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 226990b4856Slling prop != ZPOOL_PROP_NAME) 227990b4856Slling return (-1); 228990b4856Slling 229990b4856Slling switch (zpool_prop_get_type(prop)) { 230990b4856Slling case PROP_TYPE_STRING: 231990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 232990b4856Slling len); 233990b4856Slling break; 234990b4856Slling 235990b4856Slling case PROP_TYPE_NUMBER: 236990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 237990b4856Slling 238990b4856Slling switch (prop) { 239990b4856Slling case ZPOOL_PROP_SIZE: 240990b4856Slling case ZPOOL_PROP_USED: 241990b4856Slling case ZPOOL_PROP_AVAILABLE: 242990b4856Slling (void) zfs_nicenum(intval, buf, len); 243990b4856Slling break; 244990b4856Slling 245990b4856Slling case ZPOOL_PROP_CAPACITY: 246990b4856Slling (void) snprintf(buf, len, "%llu%%", 247990b4856Slling (u_longlong_t)intval); 248990b4856Slling break; 249990b4856Slling 250990b4856Slling case ZPOOL_PROP_HEALTH: 251990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 252990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 253990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 254990b4856Slling ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 255990b4856Slling 256990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 257990b4856Slling vs->vs_aux), len); 258990b4856Slling break; 259990b4856Slling default: 260990b4856Slling (void) snprintf(buf, len, "%llu", intval); 261990b4856Slling } 262990b4856Slling break; 263990b4856Slling 264990b4856Slling case PROP_TYPE_INDEX: 265990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 266990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 267990b4856Slling != 0) 268990b4856Slling return (-1); 269990b4856Slling (void) strlcpy(buf, strval, len); 270990b4856Slling break; 271990b4856Slling 272990b4856Slling default: 273990b4856Slling abort(); 274990b4856Slling } 275990b4856Slling 276990b4856Slling if (srctype) 277990b4856Slling *srctype = src; 278990b4856Slling 279990b4856Slling return (0); 280990b4856Slling } 281990b4856Slling 282990b4856Slling /* 283990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 284990b4856Slling * Assuming bootfs is a valid dataset name. 285990b4856Slling */ 286990b4856Slling static boolean_t 287990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 288990b4856Slling { 289990b4856Slling int len = strlen(pool); 290990b4856Slling 291990b4856Slling if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM)) 292990b4856Slling return (B_FALSE); 293990b4856Slling 294990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 295990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 296990b4856Slling return (B_TRUE); 297990b4856Slling 298990b4856Slling return (B_FALSE); 299990b4856Slling } 300990b4856Slling 30115e6edf1Sgw /* 30215e6edf1Sgw * Inspect the configuration to determine if any of the devices contain 30315e6edf1Sgw * an EFI label. 30415e6edf1Sgw */ 30515e6edf1Sgw static boolean_t 30615e6edf1Sgw pool_uses_efi(nvlist_t *config) 30715e6edf1Sgw { 30815e6edf1Sgw nvlist_t **child; 30915e6edf1Sgw uint_t c, children; 31015e6edf1Sgw 31115e6edf1Sgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 31215e6edf1Sgw &child, &children) != 0) 31315e6edf1Sgw return (read_efi_label(config, NULL) >= 0); 31415e6edf1Sgw 31515e6edf1Sgw for (c = 0; c < children; c++) { 31615e6edf1Sgw if (pool_uses_efi(child[c])) 31715e6edf1Sgw return (B_TRUE); 31815e6edf1Sgw } 31915e6edf1Sgw return (B_FALSE); 32015e6edf1Sgw } 32115e6edf1Sgw 322990b4856Slling /* 323990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 324990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 325990b4856Slling * specified as strings. 326990b4856Slling */ 327990b4856Slling static nvlist_t * 3280a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 329990b4856Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 330990b4856Slling { 331990b4856Slling nvpair_t *elem; 332990b4856Slling nvlist_t *retprops; 333990b4856Slling zpool_prop_t prop; 334990b4856Slling char *strval; 335990b4856Slling uint64_t intval; 3362f8aaab3Seschrock char *slash; 3372f8aaab3Seschrock struct stat64 statbuf; 33815e6edf1Sgw zpool_handle_t *zhp; 33915e6edf1Sgw nvlist_t *nvroot; 340990b4856Slling 341990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 342990b4856Slling (void) no_memory(hdl); 343990b4856Slling return (NULL); 344990b4856Slling } 345990b4856Slling 346990b4856Slling elem = NULL; 347990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 348990b4856Slling const char *propname = nvpair_name(elem); 349990b4856Slling 350990b4856Slling /* 351990b4856Slling * Make sure this property is valid and applies to this type. 352990b4856Slling */ 353990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 354990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 355990b4856Slling "invalid property '%s'"), propname); 356990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 357990b4856Slling goto error; 358990b4856Slling } 359990b4856Slling 360990b4856Slling if (zpool_prop_readonly(prop)) { 361990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 362990b4856Slling "is readonly"), propname); 363990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 364990b4856Slling goto error; 365990b4856Slling } 366990b4856Slling 367990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 368990b4856Slling &strval, &intval, errbuf) != 0) 369990b4856Slling goto error; 370990b4856Slling 371990b4856Slling /* 372990b4856Slling * Perform additional checking for specific properties. 373990b4856Slling */ 374990b4856Slling switch (prop) { 375990b4856Slling case ZPOOL_PROP_VERSION: 376990b4856Slling if (intval < version || intval > SPA_VERSION) { 377990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 378990b4856Slling "property '%s' number %d is invalid."), 379990b4856Slling propname, intval); 380990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 381990b4856Slling goto error; 382990b4856Slling } 383990b4856Slling break; 384990b4856Slling 385990b4856Slling case ZPOOL_PROP_BOOTFS: 386990b4856Slling if (create_or_import) { 387990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 388990b4856Slling "property '%s' cannot be set at creation " 389990b4856Slling "or import time"), propname); 390990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 391990b4856Slling goto error; 392990b4856Slling } 393990b4856Slling 394990b4856Slling if (version < SPA_VERSION_BOOTFS) { 395990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 396990b4856Slling "pool must be upgraded to support " 397990b4856Slling "'%s' property"), propname); 398990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 399990b4856Slling goto error; 400990b4856Slling } 401990b4856Slling 402990b4856Slling /* 403990b4856Slling * bootfs property value has to be a dataset name and 404990b4856Slling * the dataset has to be in the same pool as it sets to. 405990b4856Slling */ 406990b4856Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 407990b4856Slling strval)) { 408990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 409990b4856Slling "is an invalid name"), strval); 410990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 411990b4856Slling goto error; 412990b4856Slling } 41315e6edf1Sgw 41415e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 41515e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41615e6edf1Sgw "could not open pool '%s'"), poolname); 41715e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 41815e6edf1Sgw goto error; 41915e6edf1Sgw } 42015e6edf1Sgw verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 42115e6edf1Sgw ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 42215e6edf1Sgw 42315e6edf1Sgw /* 42415e6edf1Sgw * bootfs property cannot be set on a disk which has 42515e6edf1Sgw * been EFI labeled. 42615e6edf1Sgw */ 42715e6edf1Sgw if (pool_uses_efi(nvroot)) { 42815e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42915e6edf1Sgw "property '%s' not supported on " 43015e6edf1Sgw "EFI labeled devices"), propname); 43115e6edf1Sgw (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 43215e6edf1Sgw zpool_close(zhp); 43315e6edf1Sgw goto error; 43415e6edf1Sgw } 43515e6edf1Sgw zpool_close(zhp); 436990b4856Slling break; 437990b4856Slling 4382f8aaab3Seschrock case ZPOOL_PROP_ALTROOT: 439990b4856Slling if (!create_or_import) { 440990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 441990b4856Slling "property '%s' can only be set during pool " 442990b4856Slling "creation or import"), propname); 443990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 444990b4856Slling goto error; 445990b4856Slling } 446990b4856Slling 4472f8aaab3Seschrock if (strval[0] != '/') { 448990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4492f8aaab3Seschrock "bad alternate root '%s'"), strval); 4502f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 451990b4856Slling goto error; 452990b4856Slling } 4532f8aaab3Seschrock break; 4542f8aaab3Seschrock 4552f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 4562f8aaab3Seschrock if (strval[0] == '\0') 4572f8aaab3Seschrock break; 4582f8aaab3Seschrock 4592f8aaab3Seschrock if (strcmp(strval, "none") == 0) 4602f8aaab3Seschrock break; 461990b4856Slling 462990b4856Slling if (strval[0] != '/') { 463990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4642f8aaab3Seschrock "property '%s' must be empty, an " 4652f8aaab3Seschrock "absolute path, or 'none'"), propname); 466990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 467990b4856Slling goto error; 468990b4856Slling } 469990b4856Slling 4702f8aaab3Seschrock slash = strrchr(strval, '/'); 471990b4856Slling 4722f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4732f8aaab3Seschrock strcmp(slash, "/..") == 0) { 4742f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4752f8aaab3Seschrock "'%s' is not a valid file"), strval); 4762f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4772f8aaab3Seschrock goto error; 4782f8aaab3Seschrock } 479990b4856Slling 4802f8aaab3Seschrock *slash = '\0'; 4812f8aaab3Seschrock 4822c32020fSeschrock if (strval[0] != '\0' && 4832c32020fSeschrock (stat64(strval, &statbuf) != 0 || 4842c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 4852f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4862f8aaab3Seschrock "'%s' is not a valid directory"), 4872f8aaab3Seschrock strval); 4882f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 4892f8aaab3Seschrock goto error; 4902f8aaab3Seschrock } 4912f8aaab3Seschrock 4922f8aaab3Seschrock *slash = '/'; 4932f8aaab3Seschrock break; 494990b4856Slling } 495990b4856Slling } 496990b4856Slling 497990b4856Slling return (retprops); 498990b4856Slling error: 499990b4856Slling nvlist_free(retprops); 500990b4856Slling return (NULL); 501990b4856Slling } 502990b4856Slling 503990b4856Slling /* 504990b4856Slling * Set zpool property : propname=propval. 505990b4856Slling */ 506990b4856Slling int 507990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 508990b4856Slling { 509990b4856Slling zfs_cmd_t zc = { 0 }; 510990b4856Slling int ret = -1; 511990b4856Slling char errbuf[1024]; 512990b4856Slling nvlist_t *nvl = NULL; 513990b4856Slling nvlist_t *realprops; 514990b4856Slling uint64_t version; 515990b4856Slling 516990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 517990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 518990b4856Slling zhp->zpool_name); 519990b4856Slling 520990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) 521990b4856Slling return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf)); 522990b4856Slling 523990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 524990b4856Slling return (no_memory(zhp->zpool_hdl)); 525990b4856Slling 526990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 527990b4856Slling nvlist_free(nvl); 528990b4856Slling return (no_memory(zhp->zpool_hdl)); 529990b4856Slling } 530990b4856Slling 531990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5320a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 533990b4856Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 534990b4856Slling nvlist_free(nvl); 535990b4856Slling return (-1); 536990b4856Slling } 537990b4856Slling 538990b4856Slling nvlist_free(nvl); 539990b4856Slling nvl = realprops; 540990b4856Slling 541990b4856Slling /* 542990b4856Slling * Execute the corresponding ioctl() to set this property. 543990b4856Slling */ 544990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 545990b4856Slling 546990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 547990b4856Slling nvlist_free(nvl); 548990b4856Slling return (-1); 549990b4856Slling } 550990b4856Slling 551990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 552990b4856Slling 553990b4856Slling zcmd_free_nvlists(&zc); 554990b4856Slling nvlist_free(nvl); 555990b4856Slling 556990b4856Slling if (ret) 557990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 558990b4856Slling else 559990b4856Slling (void) zpool_props_refresh(zhp); 560990b4856Slling 561990b4856Slling return (ret); 562990b4856Slling } 563990b4856Slling 564990b4856Slling int 565990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 566990b4856Slling { 567990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 568990b4856Slling zprop_list_t *entry; 569990b4856Slling char buf[ZFS_MAXPROPLEN]; 570990b4856Slling 571990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 572990b4856Slling return (-1); 573990b4856Slling 574990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 575990b4856Slling 576990b4856Slling if (entry->pl_fixed) 577990b4856Slling continue; 578990b4856Slling 579990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 580990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 581990b4856Slling NULL) == 0) { 582990b4856Slling if (strlen(buf) > entry->pl_width) 583990b4856Slling entry->pl_width = strlen(buf); 584990b4856Slling } 585990b4856Slling } 586990b4856Slling 587990b4856Slling return (0); 588990b4856Slling } 589990b4856Slling 590990b4856Slling 591fa9e4066Sahrens /* 592fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 593fa9e4066Sahrens * 'buf'. 594fa9e4066Sahrens */ 595e7cbe64fSgw boolean_t 59699653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 597fa9e4066Sahrens { 598fa9e4066Sahrens namecheck_err_t why; 599fa9e4066Sahrens char what; 600b468a217Seschrock int ret; 601b468a217Seschrock 602b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 603b468a217Seschrock 604b468a217Seschrock /* 605b468a217Seschrock * The rules for reserved pool names were extended at a later point. 606b468a217Seschrock * But we need to support users with existing pools that may now be 607b468a217Seschrock * invalid. So we only check for this expanded set of names during a 608b468a217Seschrock * create (or import), and only in userland. 609b468a217Seschrock */ 610b468a217Seschrock if (ret == 0 && !isopen && 611b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 612b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 6138654d025Sperrin strncmp(pool, "spare", 5) == 0 || 6148654d025Sperrin strcmp(pool, "log") == 0)) { 615e7cbe64fSgw if (hdl != NULL) 616e7cbe64fSgw zfs_error_aux(hdl, 617e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved")); 61899653d4eSeschrock return (B_FALSE); 619b468a217Seschrock } 620b468a217Seschrock 621fa9e4066Sahrens 622b468a217Seschrock if (ret != 0) { 62399653d4eSeschrock if (hdl != NULL) { 624fa9e4066Sahrens switch (why) { 625b81d61a6Slling case NAME_ERR_TOOLONG: 62699653d4eSeschrock zfs_error_aux(hdl, 627b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 628b81d61a6Slling break; 629b81d61a6Slling 630fa9e4066Sahrens case NAME_ERR_INVALCHAR: 63199653d4eSeschrock zfs_error_aux(hdl, 632fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 633fa9e4066Sahrens "'%c' in pool name"), what); 634fa9e4066Sahrens break; 635fa9e4066Sahrens 636fa9e4066Sahrens case NAME_ERR_NOLETTER: 63799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 63899653d4eSeschrock "name must begin with a letter")); 639fa9e4066Sahrens break; 640fa9e4066Sahrens 641fa9e4066Sahrens case NAME_ERR_RESERVED: 64299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 64399653d4eSeschrock "name is reserved")); 644fa9e4066Sahrens break; 645fa9e4066Sahrens 646fa9e4066Sahrens case NAME_ERR_DISKLIKE: 64799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 64899653d4eSeschrock "pool name is reserved")); 649fa9e4066Sahrens break; 6505ad82045Snd 6515ad82045Snd case NAME_ERR_LEADING_SLASH: 6525ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6535ad82045Snd "leading slash in name")); 6545ad82045Snd break; 6555ad82045Snd 6565ad82045Snd case NAME_ERR_EMPTY_COMPONENT: 6575ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6585ad82045Snd "empty component in name")); 6595ad82045Snd break; 6605ad82045Snd 6615ad82045Snd case NAME_ERR_TRAILING_SLASH: 6625ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6635ad82045Snd "trailing slash in name")); 6645ad82045Snd break; 6655ad82045Snd 6665ad82045Snd case NAME_ERR_MULTIPLE_AT: 6675ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6685ad82045Snd "multiple '@' delimiters in name")); 6695ad82045Snd break; 6705ad82045Snd 671fa9e4066Sahrens } 672fa9e4066Sahrens } 67399653d4eSeschrock return (B_FALSE); 674fa9e4066Sahrens } 675fa9e4066Sahrens 67699653d4eSeschrock return (B_TRUE); 677fa9e4066Sahrens } 678fa9e4066Sahrens 679fa9e4066Sahrens /* 680fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 681fa9e4066Sahrens * state. 682fa9e4066Sahrens */ 683fa9e4066Sahrens zpool_handle_t * 68499653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 685fa9e4066Sahrens { 686fa9e4066Sahrens zpool_handle_t *zhp; 68794de1d4cSeschrock boolean_t missing; 688fa9e4066Sahrens 689fa9e4066Sahrens /* 690fa9e4066Sahrens * Make sure the pool name is valid. 691fa9e4066Sahrens */ 69299653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 693ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 69499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 69599653d4eSeschrock pool); 696fa9e4066Sahrens return (NULL); 697fa9e4066Sahrens } 698fa9e4066Sahrens 69999653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 70099653d4eSeschrock return (NULL); 701fa9e4066Sahrens 70299653d4eSeschrock zhp->zpool_hdl = hdl; 703fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 704fa9e4066Sahrens 70594de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 70694de1d4cSeschrock zpool_close(zhp); 70794de1d4cSeschrock return (NULL); 70894de1d4cSeschrock } 70994de1d4cSeschrock 71094de1d4cSeschrock if (missing) { 711990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 712ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 713990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 71494de1d4cSeschrock zpool_close(zhp); 71594de1d4cSeschrock return (NULL); 716fa9e4066Sahrens } 717fa9e4066Sahrens 718fa9e4066Sahrens return (zhp); 719fa9e4066Sahrens } 720fa9e4066Sahrens 721fa9e4066Sahrens /* 722fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 723fa9e4066Sahrens * the configuration cache may be out of date). 724fa9e4066Sahrens */ 72594de1d4cSeschrock int 72694de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 727fa9e4066Sahrens { 728fa9e4066Sahrens zpool_handle_t *zhp; 72994de1d4cSeschrock boolean_t missing; 730fa9e4066Sahrens 73194de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 73294de1d4cSeschrock return (-1); 733fa9e4066Sahrens 73499653d4eSeschrock zhp->zpool_hdl = hdl; 735fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 736fa9e4066Sahrens 73794de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 73894de1d4cSeschrock zpool_close(zhp); 73994de1d4cSeschrock return (-1); 740fa9e4066Sahrens } 741fa9e4066Sahrens 74294de1d4cSeschrock if (missing) { 74394de1d4cSeschrock zpool_close(zhp); 74494de1d4cSeschrock *ret = NULL; 74594de1d4cSeschrock return (0); 74694de1d4cSeschrock } 74794de1d4cSeschrock 74894de1d4cSeschrock *ret = zhp; 74994de1d4cSeschrock return (0); 750fa9e4066Sahrens } 751fa9e4066Sahrens 752fa9e4066Sahrens /* 753fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 754fa9e4066Sahrens * state. 755fa9e4066Sahrens */ 756fa9e4066Sahrens zpool_handle_t * 75799653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 758fa9e4066Sahrens { 759fa9e4066Sahrens zpool_handle_t *zhp; 760fa9e4066Sahrens 76199653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 762fa9e4066Sahrens return (NULL); 763fa9e4066Sahrens 764fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 765ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 76699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 767fa9e4066Sahrens zpool_close(zhp); 768fa9e4066Sahrens return (NULL); 769fa9e4066Sahrens } 770fa9e4066Sahrens 771fa9e4066Sahrens return (zhp); 772fa9e4066Sahrens } 773fa9e4066Sahrens 774fa9e4066Sahrens /* 775fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 776fa9e4066Sahrens */ 777fa9e4066Sahrens void 778fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 779fa9e4066Sahrens { 780fa9e4066Sahrens if (zhp->zpool_config) 781fa9e4066Sahrens nvlist_free(zhp->zpool_config); 782088e9d47Seschrock if (zhp->zpool_old_config) 783088e9d47Seschrock nvlist_free(zhp->zpool_old_config); 784b1b8ab34Slling if (zhp->zpool_props) 785b1b8ab34Slling nvlist_free(zhp->zpool_props); 786fa9e4066Sahrens free(zhp); 787fa9e4066Sahrens } 788fa9e4066Sahrens 789fa9e4066Sahrens /* 790fa9e4066Sahrens * Return the name of the pool. 791fa9e4066Sahrens */ 792fa9e4066Sahrens const char * 793fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 794fa9e4066Sahrens { 795fa9e4066Sahrens return (zhp->zpool_name); 796fa9e4066Sahrens } 797fa9e4066Sahrens 798fa9e4066Sahrens 799fa9e4066Sahrens /* 800fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 801fa9e4066Sahrens */ 802fa9e4066Sahrens int 803fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 804fa9e4066Sahrens { 805fa9e4066Sahrens return (zhp->zpool_state); 806fa9e4066Sahrens } 807fa9e4066Sahrens 808fa9e4066Sahrens /* 809fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 810fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 811fa9e4066Sahrens * don't have to worry about error semantics. 812fa9e4066Sahrens */ 813fa9e4066Sahrens int 81499653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8150a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 816fa9e4066Sahrens { 817fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 8180a48a24eStimh nvlist_t *zc_fsprops = NULL; 8190a48a24eStimh nvlist_t *zc_props = NULL; 82099653d4eSeschrock char msg[1024]; 821990b4856Slling char *altroot; 8220a48a24eStimh int ret = -1; 823fa9e4066Sahrens 82499653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 82599653d4eSeschrock "cannot create '%s'"), pool); 826fa9e4066Sahrens 82799653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 82899653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 829fa9e4066Sahrens 830351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 831990b4856Slling return (-1); 832fa9e4066Sahrens 8330a48a24eStimh if (props) { 8340a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8350a48a24eStimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8360a48a24eStimh goto create_failed; 8370a48a24eStimh } 8380a48a24eStimh } 83999653d4eSeschrock 8400a48a24eStimh if (fsprops) { 8410a48a24eStimh uint64_t zoned; 8420a48a24eStimh char *zonestr; 8430a48a24eStimh 8440a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 8450a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8460a48a24eStimh strcmp(zonestr, "on") == 0); 8470a48a24eStimh 8480a48a24eStimh if ((zc_fsprops = zfs_valid_proplist(hdl, 8490a48a24eStimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 8500a48a24eStimh goto create_failed; 8510a48a24eStimh } 8520a48a24eStimh if (!zc_props && 8530a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 8540a48a24eStimh goto create_failed; 8550a48a24eStimh } 8560a48a24eStimh if (nvlist_add_nvlist(zc_props, 8570a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 8580a48a24eStimh goto create_failed; 8590a48a24eStimh } 860351420b3Slling } 861fa9e4066Sahrens 8620a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 8630a48a24eStimh goto create_failed; 8640a48a24eStimh 865990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 866fa9e4066Sahrens 8670a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 868351420b3Slling 869e9dbad6fSeschrock zcmd_free_nvlists(&zc); 8700a48a24eStimh nvlist_free(zc_props); 8710a48a24eStimh nvlist_free(zc_fsprops); 872fa9e4066Sahrens 87399653d4eSeschrock switch (errno) { 874fa9e4066Sahrens case EBUSY: 875fa9e4066Sahrens /* 876fa9e4066Sahrens * This can happen if the user has specified the same 877fa9e4066Sahrens * device multiple times. We can't reliably detect this 878fa9e4066Sahrens * until we try to add it and see we already have a 879fa9e4066Sahrens * label. 880fa9e4066Sahrens */ 88199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 88299653d4eSeschrock "one or more vdevs refer to the same device")); 88399653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 884fa9e4066Sahrens 885fa9e4066Sahrens case EOVERFLOW: 886fa9e4066Sahrens /* 88799653d4eSeschrock * This occurs when one of the devices is below 888fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 889fa9e4066Sahrens * device was the problem device since there's no 890fa9e4066Sahrens * reliable way to determine device size from userland. 891fa9e4066Sahrens */ 892fa9e4066Sahrens { 893fa9e4066Sahrens char buf[64]; 894fa9e4066Sahrens 895fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 896fa9e4066Sahrens 89799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 89899653d4eSeschrock "one or more devices is less than the " 89999653d4eSeschrock "minimum size (%s)"), buf); 900fa9e4066Sahrens } 90199653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 902fa9e4066Sahrens 903fa9e4066Sahrens case ENOSPC: 90499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 90599653d4eSeschrock "one or more devices is out of space")); 90699653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 907fa9e4066Sahrens 908fa94a07fSbrendan case ENOTBLK: 909fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 910fa94a07fSbrendan "cache device must be a disk or disk slice")); 911fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 912fa94a07fSbrendan 913fa9e4066Sahrens default: 91499653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 915fa9e4066Sahrens } 916fa9e4066Sahrens } 917fa9e4066Sahrens 918fa9e4066Sahrens /* 919fa9e4066Sahrens * If this is an alternate root pool, then we automatically set the 920e9dbad6fSeschrock * mountpoint of the root dataset to be '/'. 921fa9e4066Sahrens */ 922990b4856Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 923990b4856Slling &altroot) == 0) { 924fa9e4066Sahrens zfs_handle_t *zhp; 925fa9e4066Sahrens 926990b4856Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 927e9dbad6fSeschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 928e9dbad6fSeschrock "/") == 0); 929fa9e4066Sahrens 930fa9e4066Sahrens zfs_close(zhp); 931fa9e4066Sahrens } 932fa9e4066Sahrens 9330a48a24eStimh create_failed: 934351420b3Slling zcmd_free_nvlists(&zc); 9350a48a24eStimh nvlist_free(zc_props); 9360a48a24eStimh nvlist_free(zc_fsprops); 9370a48a24eStimh return (ret); 938fa9e4066Sahrens } 939fa9e4066Sahrens 940fa9e4066Sahrens /* 941fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 942fa9e4066Sahrens * datasets left in the pool. 943fa9e4066Sahrens */ 944fa9e4066Sahrens int 945fa9e4066Sahrens zpool_destroy(zpool_handle_t *zhp) 946fa9e4066Sahrens { 947fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 948fa9e4066Sahrens zfs_handle_t *zfp = NULL; 94999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 95099653d4eSeschrock char msg[1024]; 951fa9e4066Sahrens 952fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 95399653d4eSeschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 95499653d4eSeschrock ZFS_TYPE_FILESYSTEM)) == NULL) 955fa9e4066Sahrens return (-1); 956fa9e4066Sahrens 9575ad82045Snd if (zpool_remove_zvol_links(zhp) != 0) 958fa9e4066Sahrens return (-1); 959fa9e4066Sahrens 960fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 961fa9e4066Sahrens 962ecd6cf80Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 96399653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 96499653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 965fa9e4066Sahrens 96699653d4eSeschrock if (errno == EROFS) { 96799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 96899653d4eSeschrock "one or more devices is read only")); 96999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 97099653d4eSeschrock } else { 97199653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 972fa9e4066Sahrens } 973fa9e4066Sahrens 974fa9e4066Sahrens if (zfp) 975fa9e4066Sahrens zfs_close(zfp); 976fa9e4066Sahrens return (-1); 977fa9e4066Sahrens } 978fa9e4066Sahrens 979fa9e4066Sahrens if (zfp) { 980fa9e4066Sahrens remove_mountpoint(zfp); 981fa9e4066Sahrens zfs_close(zfp); 982fa9e4066Sahrens } 983fa9e4066Sahrens 984fa9e4066Sahrens return (0); 985fa9e4066Sahrens } 986fa9e4066Sahrens 987fa9e4066Sahrens /* 988fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 989fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 990fa9e4066Sahrens */ 991fa9e4066Sahrens int 992fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 993fa9e4066Sahrens { 994e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 99599653d4eSeschrock int ret; 99699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 99799653d4eSeschrock char msg[1024]; 998fa94a07fSbrendan nvlist_t **spares, **l2cache; 999fa94a07fSbrendan uint_t nspares, nl2cache; 100099653d4eSeschrock 100199653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 100299653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 100399653d4eSeschrock 1004fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1005fa94a07fSbrendan SPA_VERSION_SPARES && 100699653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 100799653d4eSeschrock &spares, &nspares) == 0) { 100899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 100999653d4eSeschrock "upgraded to add hot spares")); 101099653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 101199653d4eSeschrock } 1012fa9e4066Sahrens 1013fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1014fa94a07fSbrendan SPA_VERSION_L2CACHE && 1015fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1016fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1017fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1018fa94a07fSbrendan "upgraded to add cache devices")); 1019fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1020fa94a07fSbrendan } 1021fa94a07fSbrendan 1022990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 102399653d4eSeschrock return (-1); 1024fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1025fa9e4066Sahrens 1026ecd6cf80Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1027fa9e4066Sahrens switch (errno) { 1028fa9e4066Sahrens case EBUSY: 1029fa9e4066Sahrens /* 1030fa9e4066Sahrens * This can happen if the user has specified the same 1031fa9e4066Sahrens * device multiple times. We can't reliably detect this 1032fa9e4066Sahrens * until we try to add it and see we already have a 1033fa9e4066Sahrens * label. 1034fa9e4066Sahrens */ 103599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 103699653d4eSeschrock "one or more vdevs refer to the same device")); 103799653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1038fa9e4066Sahrens break; 1039fa9e4066Sahrens 1040fa9e4066Sahrens case EOVERFLOW: 1041fa9e4066Sahrens /* 1042fa9e4066Sahrens * This occurrs when one of the devices is below 1043fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1044fa9e4066Sahrens * device was the problem device since there's no 1045fa9e4066Sahrens * reliable way to determine device size from userland. 1046fa9e4066Sahrens */ 1047fa9e4066Sahrens { 1048fa9e4066Sahrens char buf[64]; 1049fa9e4066Sahrens 1050fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1051fa9e4066Sahrens 105299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 105399653d4eSeschrock "device is less than the minimum " 105499653d4eSeschrock "size (%s)"), buf); 1055fa9e4066Sahrens } 105699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 105799653d4eSeschrock break; 105899653d4eSeschrock 105999653d4eSeschrock case ENOTSUP: 106099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10618654d025Sperrin "pool must be upgraded to add these vdevs")); 106299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1063fa9e4066Sahrens break; 1064fa9e4066Sahrens 1065b1b8ab34Slling case EDOM: 1066b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10678654d025Sperrin "root pool can not have multiple vdevs" 10688654d025Sperrin " or separate logs")); 1069b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1070b1b8ab34Slling break; 1071b1b8ab34Slling 1072fa94a07fSbrendan case ENOTBLK: 1073fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1074fa94a07fSbrendan "cache device must be a disk or disk slice")); 1075fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1076fa94a07fSbrendan break; 1077fa94a07fSbrendan 1078fa9e4066Sahrens default: 107999653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1080fa9e4066Sahrens } 1081fa9e4066Sahrens 108299653d4eSeschrock ret = -1; 108399653d4eSeschrock } else { 108499653d4eSeschrock ret = 0; 1085fa9e4066Sahrens } 1086fa9e4066Sahrens 1087e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1088fa9e4066Sahrens 108999653d4eSeschrock return (ret); 1090fa9e4066Sahrens } 1091fa9e4066Sahrens 1092fa9e4066Sahrens /* 1093fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1094fa9e4066Sahrens * mounted datasets in the pool. 1095fa9e4066Sahrens */ 1096fa9e4066Sahrens int 109789a89ebfSlling zpool_export(zpool_handle_t *zhp, boolean_t force) 1098fa9e4066Sahrens { 1099fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 110089a89ebfSlling char msg[1024]; 1101fa9e4066Sahrens 1102fa9e4066Sahrens if (zpool_remove_zvol_links(zhp) != 0) 1103fa9e4066Sahrens return (-1); 1104fa9e4066Sahrens 110589a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 110689a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 110789a89ebfSlling 1108fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 110989a89ebfSlling zc.zc_cookie = force; 111089a89ebfSlling 111189a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 111289a89ebfSlling switch (errno) { 111389a89ebfSlling case EXDEV: 111489a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 111589a89ebfSlling "use '-f' to override the following errors:\n" 111689a89ebfSlling "'%s' has an active shared spare which could be" 111789a89ebfSlling " used by other pools once '%s' is exported."), 111889a89ebfSlling zhp->zpool_name, zhp->zpool_name); 111989a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 112089a89ebfSlling msg)); 112189a89ebfSlling default: 112289a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 112389a89ebfSlling msg)); 112489a89ebfSlling } 112589a89ebfSlling } 1126fa9e4066Sahrens 1127fa9e4066Sahrens return (0); 1128fa9e4066Sahrens } 1129fa9e4066Sahrens 1130fa9e4066Sahrens /* 1131990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1132990b4856Slling * if possible. 1133990b4856Slling * 1134990b4856Slling * Applications should use zpool_import_props() to import a pool with 1135990b4856Slling * new properties value to be set. 1136fa9e4066Sahrens */ 1137fa9e4066Sahrens int 113899653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1139990b4856Slling char *altroot) 1140990b4856Slling { 1141990b4856Slling nvlist_t *props = NULL; 1142990b4856Slling int ret; 1143990b4856Slling 1144990b4856Slling if (altroot != NULL) { 1145990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1146990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1147990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1148990b4856Slling newname)); 1149990b4856Slling } 1150990b4856Slling 1151990b4856Slling if (nvlist_add_string(props, 1152990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) { 1153990b4856Slling nvlist_free(props); 1154990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1155990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1156990b4856Slling newname)); 1157990b4856Slling } 1158990b4856Slling } 1159990b4856Slling 1160c5904d13Seschrock ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 1161990b4856Slling if (props) 1162990b4856Slling nvlist_free(props); 1163990b4856Slling return (ret); 1164990b4856Slling } 1165990b4856Slling 1166990b4856Slling /* 1167990b4856Slling * Import the given pool using the known configuration and a list of 1168990b4856Slling * properties to be set. The configuration should have come from 1169990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1170990b4856Slling * is imported with a different name. 1171990b4856Slling */ 1172990b4856Slling int 1173990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1174c5904d13Seschrock nvlist_t *props, boolean_t importfaulted) 1175fa9e4066Sahrens { 1176e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1177fa9e4066Sahrens char *thename; 1178fa9e4066Sahrens char *origname; 1179fa9e4066Sahrens int ret; 1180990b4856Slling char errbuf[1024]; 1181fa9e4066Sahrens 1182fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1183fa9e4066Sahrens &origname) == 0); 1184fa9e4066Sahrens 1185990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1186990b4856Slling "cannot import pool '%s'"), origname); 1187990b4856Slling 1188fa9e4066Sahrens if (newname != NULL) { 118999653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1190ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 119199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 119299653d4eSeschrock newname)); 1193fa9e4066Sahrens thename = (char *)newname; 1194fa9e4066Sahrens } else { 1195fa9e4066Sahrens thename = origname; 1196fa9e4066Sahrens } 1197fa9e4066Sahrens 1198990b4856Slling if (props) { 1199990b4856Slling uint64_t version; 1200fa9e4066Sahrens 1201990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1202990b4856Slling &version) == 0); 1203fa9e4066Sahrens 12040a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1205351420b3Slling props, version, B_TRUE, errbuf)) == NULL) { 1206990b4856Slling return (-1); 1207351420b3Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1208351420b3Slling nvlist_free(props); 1209990b4856Slling return (-1); 1210351420b3Slling } 1211990b4856Slling } 1212990b4856Slling 1213990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1214fa9e4066Sahrens 1215fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1216ea8dc4b6Seschrock &zc.zc_guid) == 0); 1217fa9e4066Sahrens 1218351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1219351420b3Slling nvlist_free(props); 122099653d4eSeschrock return (-1); 1221351420b3Slling } 1222fa9e4066Sahrens 1223c5904d13Seschrock zc.zc_cookie = (uint64_t)importfaulted; 1224fa9e4066Sahrens ret = 0; 1225ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1226fa9e4066Sahrens char desc[1024]; 1227fa9e4066Sahrens if (newname == NULL) 1228fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1229fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1230fa9e4066Sahrens thename); 1231fa9e4066Sahrens else 1232fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1233fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1234fa9e4066Sahrens origname, thename); 1235fa9e4066Sahrens 1236fa9e4066Sahrens switch (errno) { 1237ea8dc4b6Seschrock case ENOTSUP: 1238ea8dc4b6Seschrock /* 1239ea8dc4b6Seschrock * Unsupported version. 1240ea8dc4b6Seschrock */ 124199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1242ea8dc4b6Seschrock break; 1243ea8dc4b6Seschrock 1244b5989ec7Seschrock case EINVAL: 1245b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1246b5989ec7Seschrock break; 1247b5989ec7Seschrock 1248fa9e4066Sahrens default: 124999653d4eSeschrock (void) zpool_standard_error(hdl, errno, desc); 1250fa9e4066Sahrens } 1251fa9e4066Sahrens 1252fa9e4066Sahrens ret = -1; 1253fa9e4066Sahrens } else { 1254fa9e4066Sahrens zpool_handle_t *zhp; 1255ecd6cf80Smarks 1256fa9e4066Sahrens /* 1257fa9e4066Sahrens * This should never fail, but play it safe anyway. 1258fa9e4066Sahrens */ 125994de1d4cSeschrock if (zpool_open_silent(hdl, thename, &zhp) != 0) { 126094de1d4cSeschrock ret = -1; 126194de1d4cSeschrock } else if (zhp != NULL) { 1262fa9e4066Sahrens ret = zpool_create_zvol_links(zhp); 1263fa9e4066Sahrens zpool_close(zhp); 1264fa9e4066Sahrens } 1265ecd6cf80Smarks 1266fa9e4066Sahrens } 1267fa9e4066Sahrens 1268e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1269351420b3Slling nvlist_free(props); 1270351420b3Slling 1271fa9e4066Sahrens return (ret); 1272fa9e4066Sahrens } 1273fa9e4066Sahrens 1274fa9e4066Sahrens /* 1275fa9e4066Sahrens * Scrub the pool. 1276fa9e4066Sahrens */ 1277fa9e4066Sahrens int 1278fa9e4066Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) 1279fa9e4066Sahrens { 1280fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1281fa9e4066Sahrens char msg[1024]; 128299653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1283fa9e4066Sahrens 1284fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1285fa9e4066Sahrens zc.zc_cookie = type; 1286fa9e4066Sahrens 1287ecd6cf80Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0) 1288fa9e4066Sahrens return (0); 1289fa9e4066Sahrens 1290fa9e4066Sahrens (void) snprintf(msg, sizeof (msg), 1291fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1292fa9e4066Sahrens 129399653d4eSeschrock if (errno == EBUSY) 129499653d4eSeschrock return (zfs_error(hdl, EZFS_RESILVERING, msg)); 129599653d4eSeschrock else 129699653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1297fa9e4066Sahrens } 1298fa9e4066Sahrens 1299a43d325bSek /* 1300a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1301a43d325bSek * spare; but FALSE if its an INUSE spare. 1302a43d325bSek */ 130399653d4eSeschrock static nvlist_t * 130499653d4eSeschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, 1305fa94a07fSbrendan boolean_t *avail_spare, boolean_t *l2cache) 1306ea8dc4b6Seschrock { 1307ea8dc4b6Seschrock uint_t c, children; 1308ea8dc4b6Seschrock nvlist_t **child; 130999653d4eSeschrock uint64_t theguid, present; 1310ea8dc4b6Seschrock char *path; 1311ea8dc4b6Seschrock uint64_t wholedisk = 0; 131299653d4eSeschrock nvlist_t *ret; 1313ea8dc4b6Seschrock 131499653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); 1315ea8dc4b6Seschrock 1316ea8dc4b6Seschrock if (search == NULL && 1317ea8dc4b6Seschrock nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { 1318ea8dc4b6Seschrock /* 1319ea8dc4b6Seschrock * If the device has never been present since import, the only 1320ea8dc4b6Seschrock * reliable way to match the vdev is by GUID. 1321ea8dc4b6Seschrock */ 132299653d4eSeschrock if (theguid == guid) 132399653d4eSeschrock return (nv); 1324ea8dc4b6Seschrock } else if (search != NULL && 1325ea8dc4b6Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 1326ea8dc4b6Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1327ea8dc4b6Seschrock &wholedisk); 1328ea8dc4b6Seschrock if (wholedisk) { 1329ea8dc4b6Seschrock /* 1330ea8dc4b6Seschrock * For whole disks, the internal path has 's0', but the 1331ea8dc4b6Seschrock * path passed in by the user doesn't. 1332ea8dc4b6Seschrock */ 1333ea8dc4b6Seschrock if (strlen(search) == strlen(path) - 2 && 1334ea8dc4b6Seschrock strncmp(search, path, strlen(search)) == 0) 133599653d4eSeschrock return (nv); 1336ea8dc4b6Seschrock } else if (strcmp(search, path) == 0) { 133799653d4eSeschrock return (nv); 1338ea8dc4b6Seschrock } 1339ea8dc4b6Seschrock } 1340ea8dc4b6Seschrock 1341ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1342ea8dc4b6Seschrock &child, &children) != 0) 134399653d4eSeschrock return (NULL); 1344ea8dc4b6Seschrock 1345ea8dc4b6Seschrock for (c = 0; c < children; c++) 134699653d4eSeschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 1347fa94a07fSbrendan avail_spare, l2cache)) != NULL) 1348ea8dc4b6Seschrock return (ret); 1349ea8dc4b6Seschrock 135099653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 135199653d4eSeschrock &child, &children) == 0) { 135299653d4eSeschrock for (c = 0; c < children; c++) { 135399653d4eSeschrock if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 1354fa94a07fSbrendan avail_spare, l2cache)) != NULL) { 1355a43d325bSek *avail_spare = B_TRUE; 135699653d4eSeschrock return (ret); 135799653d4eSeschrock } 135899653d4eSeschrock } 135999653d4eSeschrock } 136099653d4eSeschrock 1361fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1362fa94a07fSbrendan &child, &children) == 0) { 1363fa94a07fSbrendan for (c = 0; c < children; c++) { 1364fa94a07fSbrendan if ((ret = vdev_to_nvlist_iter(child[c], search, guid, 1365fa94a07fSbrendan avail_spare, l2cache)) != NULL) { 1366fa94a07fSbrendan *l2cache = B_TRUE; 1367fa94a07fSbrendan return (ret); 1368fa94a07fSbrendan } 1369fa94a07fSbrendan } 1370fa94a07fSbrendan } 1371fa94a07fSbrendan 137299653d4eSeschrock return (NULL); 1373ea8dc4b6Seschrock } 1374ea8dc4b6Seschrock 137599653d4eSeschrock nvlist_t * 1376fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 1377fa94a07fSbrendan boolean_t *l2cache) 1378ea8dc4b6Seschrock { 1379ea8dc4b6Seschrock char buf[MAXPATHLEN]; 1380ea8dc4b6Seschrock const char *search; 1381ea8dc4b6Seschrock char *end; 1382ea8dc4b6Seschrock nvlist_t *nvroot; 1383ea8dc4b6Seschrock uint64_t guid; 1384ea8dc4b6Seschrock 13850917b783Seschrock guid = strtoull(path, &end, 10); 1386ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 1387ea8dc4b6Seschrock search = NULL; 1388ea8dc4b6Seschrock } else if (path[0] != '/') { 1389ea8dc4b6Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 1390ea8dc4b6Seschrock search = buf; 1391ea8dc4b6Seschrock } else { 1392ea8dc4b6Seschrock search = path; 1393ea8dc4b6Seschrock } 1394ea8dc4b6Seschrock 1395ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1396ea8dc4b6Seschrock &nvroot) == 0); 1397ea8dc4b6Seschrock 1398a43d325bSek *avail_spare = B_FALSE; 1399fa94a07fSbrendan *l2cache = B_FALSE; 1400fa94a07fSbrendan return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare, 1401fa94a07fSbrendan l2cache)); 1402a43d325bSek } 1403a43d325bSek 1404a43d325bSek /* 1405fa94a07fSbrendan * Returns TRUE if the given guid corresponds to the given type. 1406fa94a07fSbrendan * This is used to check for hot spares (INUSE or not), and level 2 cache 1407fa94a07fSbrendan * devices. 1408a43d325bSek */ 1409a43d325bSek static boolean_t 1410fa94a07fSbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type) 1411a43d325bSek { 1412fa94a07fSbrendan uint64_t target_guid; 1413a43d325bSek nvlist_t *nvroot; 1414fa94a07fSbrendan nvlist_t **list; 1415fa94a07fSbrendan uint_t count; 1416a43d325bSek int i; 1417a43d325bSek 1418a43d325bSek verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1419a43d325bSek &nvroot) == 0); 1420fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) { 1421fa94a07fSbrendan for (i = 0; i < count; i++) { 1422fa94a07fSbrendan verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID, 1423fa94a07fSbrendan &target_guid) == 0); 1424fa94a07fSbrendan if (guid == target_guid) 1425a43d325bSek return (B_TRUE); 1426a43d325bSek } 1427a43d325bSek } 1428a43d325bSek 1429a43d325bSek return (B_FALSE); 1430ea8dc4b6Seschrock } 1431ea8dc4b6Seschrock 1432fa9e4066Sahrens /* 14333d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 14343d7072f8Seschrock * ZFS_ONLINE_* flags. 1435fa9e4066Sahrens */ 1436fa9e4066Sahrens int 14373d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 14383d7072f8Seschrock vdev_state_t *newstate) 1439fa9e4066Sahrens { 1440fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1441fa9e4066Sahrens char msg[1024]; 144299653d4eSeschrock nvlist_t *tgt; 1443fa94a07fSbrendan boolean_t avail_spare, l2cache; 144499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1445fa9e4066Sahrens 1446ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 1447ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 1448ea8dc4b6Seschrock 1449fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1450fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL) 145199653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1452fa9e4066Sahrens 145399653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 1454fa9e4066Sahrens 1455fa94a07fSbrendan if (avail_spare || 1456fa94a07fSbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 1457a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 1458a43d325bSek 14593d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 14603d7072f8Seschrock zc.zc_obj = flags; 1461fa9e4066Sahrens 1462ecd6cf80Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) 14633d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 14643d7072f8Seschrock 14653d7072f8Seschrock *newstate = zc.zc_cookie; 14663d7072f8Seschrock return (0); 1467fa9e4066Sahrens } 1468fa9e4066Sahrens 1469fa9e4066Sahrens /* 1470fa9e4066Sahrens * Take the specified vdev offline 1471fa9e4066Sahrens */ 1472fa9e4066Sahrens int 14733d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 1474fa9e4066Sahrens { 1475fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1476fa9e4066Sahrens char msg[1024]; 147799653d4eSeschrock nvlist_t *tgt; 1478fa94a07fSbrendan boolean_t avail_spare, l2cache; 147999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1480fa9e4066Sahrens 1481ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 1482ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 1483ea8dc4b6Seschrock 1484fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1485fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL) 148699653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 148799653d4eSeschrock 148899653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 1489fa9e4066Sahrens 1490fa94a07fSbrendan if (avail_spare || 1491fa94a07fSbrendan is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE) 1492a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 1493a43d325bSek 14943d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 14953d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 14963d7072f8Seschrock 1497ecd6cf80Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 14983d7072f8Seschrock return (0); 14993d7072f8Seschrock 15003d7072f8Seschrock switch (errno) { 15013d7072f8Seschrock case EBUSY: 15023d7072f8Seschrock 15033d7072f8Seschrock /* 15043d7072f8Seschrock * There are no other replicas of this device. 15053d7072f8Seschrock */ 15063d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 15073d7072f8Seschrock 15083d7072f8Seschrock default: 15093d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 15103d7072f8Seschrock } 15113d7072f8Seschrock } 15123d7072f8Seschrock 15133d7072f8Seschrock /* 15143d7072f8Seschrock * Mark the given vdev faulted. 15153d7072f8Seschrock */ 15163d7072f8Seschrock int 15173d7072f8Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid) 15183d7072f8Seschrock { 15193d7072f8Seschrock zfs_cmd_t zc = { 0 }; 15203d7072f8Seschrock char msg[1024]; 15213d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 15223d7072f8Seschrock 15233d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 15243d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 1525441d80aaSlling 15263d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 15273d7072f8Seschrock zc.zc_guid = guid; 15283d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 15293d7072f8Seschrock 15303d7072f8Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 1531fa9e4066Sahrens return (0); 1532fa9e4066Sahrens 1533fa9e4066Sahrens switch (errno) { 153499653d4eSeschrock case EBUSY: 1535fa9e4066Sahrens 1536fa9e4066Sahrens /* 1537fa9e4066Sahrens * There are no other replicas of this device. 1538fa9e4066Sahrens */ 153999653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 1540fa9e4066Sahrens 154199653d4eSeschrock default: 154299653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1543fa9e4066Sahrens } 15443d7072f8Seschrock 15453d7072f8Seschrock } 15463d7072f8Seschrock 15473d7072f8Seschrock /* 15483d7072f8Seschrock * Mark the given vdev degraded. 15493d7072f8Seschrock */ 15503d7072f8Seschrock int 15513d7072f8Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid) 15523d7072f8Seschrock { 15533d7072f8Seschrock zfs_cmd_t zc = { 0 }; 15543d7072f8Seschrock char msg[1024]; 15553d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 15563d7072f8Seschrock 15573d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 15583d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 15593d7072f8Seschrock 15603d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 15613d7072f8Seschrock zc.zc_guid = guid; 15623d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 15633d7072f8Seschrock 15643d7072f8Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 15653d7072f8Seschrock return (0); 15663d7072f8Seschrock 15673d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 156899653d4eSeschrock } 156999653d4eSeschrock 157099653d4eSeschrock /* 157199653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 157299653d4eSeschrock * a hot spare. 157399653d4eSeschrock */ 157499653d4eSeschrock static boolean_t 157599653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 157699653d4eSeschrock { 157799653d4eSeschrock nvlist_t **child; 157899653d4eSeschrock uint_t c, children; 157999653d4eSeschrock char *type; 158099653d4eSeschrock 158199653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 158299653d4eSeschrock &children) == 0) { 158399653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 158499653d4eSeschrock &type) == 0); 158599653d4eSeschrock 158699653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 158799653d4eSeschrock children == 2 && child[which] == tgt) 158899653d4eSeschrock return (B_TRUE); 158999653d4eSeschrock 159099653d4eSeschrock for (c = 0; c < children; c++) 159199653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 159299653d4eSeschrock return (B_TRUE); 159399653d4eSeschrock } 159499653d4eSeschrock 159599653d4eSeschrock return (B_FALSE); 1596fa9e4066Sahrens } 1597fa9e4066Sahrens 1598fa9e4066Sahrens /* 1599fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 16008654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 1601fa9e4066Sahrens */ 1602fa9e4066Sahrens int 1603fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 1604fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 1605fa9e4066Sahrens { 1606fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1607fa9e4066Sahrens char msg[1024]; 1608fa9e4066Sahrens int ret; 160999653d4eSeschrock nvlist_t *tgt; 1610fa94a07fSbrendan boolean_t avail_spare, l2cache; 16118654d025Sperrin uint64_t val, is_log; 16120430f8daSeschrock char *path, *newname; 161399653d4eSeschrock nvlist_t **child; 161499653d4eSeschrock uint_t children; 161599653d4eSeschrock nvlist_t *config_root; 161699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1617fa9e4066Sahrens 1618ea8dc4b6Seschrock if (replacing) 1619ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1620ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 1621ea8dc4b6Seschrock else 1622ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1623ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 1624ea8dc4b6Seschrock 1625fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1626fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0) 162799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 162899653d4eSeschrock 1629a43d325bSek if (avail_spare) 163099653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 163199653d4eSeschrock 1632fa94a07fSbrendan if (l2cache) 1633fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 1634fa94a07fSbrendan 163599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 1636fa9e4066Sahrens zc.zc_cookie = replacing; 1637fa9e4066Sahrens 163899653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 163999653d4eSeschrock &child, &children) != 0 || children != 1) { 164099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 164199653d4eSeschrock "new device must be a single disk")); 164299653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 164399653d4eSeschrock } 164499653d4eSeschrock 164599653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 164699653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 164799653d4eSeschrock 16480430f8daSeschrock if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL) 16490430f8daSeschrock return (-1); 16500430f8daSeschrock 165199653d4eSeschrock /* 165299653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 165399653d4eSeschrock * replace it with another hot spare. 165499653d4eSeschrock */ 165599653d4eSeschrock if (replacing && 165699653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 16570430f8daSeschrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) == NULL || 1658a43d325bSek !avail_spare) && is_replacing_spare(config_root, tgt, 1)) { 165999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 166099653d4eSeschrock "can only be replaced by another hot spare")); 16610430f8daSeschrock free(newname); 166299653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 166399653d4eSeschrock } 166499653d4eSeschrock 166599653d4eSeschrock /* 166699653d4eSeschrock * If we are attempting to replace a spare, it canot be applied to an 166799653d4eSeschrock * already spared device. 166899653d4eSeschrock */ 166999653d4eSeschrock if (replacing && 167099653d4eSeschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 16710430f8daSeschrock zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) != NULL && 1672fa94a07fSbrendan avail_spare && is_replacing_spare(config_root, tgt, 0)) { 167399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 167499653d4eSeschrock "device has already been replaced with a spare")); 16750430f8daSeschrock free(newname); 167699653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 167799653d4eSeschrock } 167899653d4eSeschrock 16790430f8daSeschrock free(newname); 16800430f8daSeschrock 1681990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 168299653d4eSeschrock return (-1); 1683fa9e4066Sahrens 1684ecd6cf80Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 1685fa9e4066Sahrens 1686e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1687fa9e4066Sahrens 1688fa9e4066Sahrens if (ret == 0) 1689fa9e4066Sahrens return (0); 1690fa9e4066Sahrens 1691fa9e4066Sahrens switch (errno) { 1692ea8dc4b6Seschrock case ENOTSUP: 1693fa9e4066Sahrens /* 1694fa9e4066Sahrens * Can't attach to or replace this type of vdev. 1695fa9e4066Sahrens */ 16968654d025Sperrin if (replacing) { 16978654d025Sperrin is_log = B_FALSE; 16988654d025Sperrin (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG, 16998654d025Sperrin &is_log); 17008654d025Sperrin if (is_log) 17018654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17028654d025Sperrin "cannot replace a log with a spare")); 17038654d025Sperrin else 17048654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17058654d025Sperrin "cannot replace a replacing device")); 17068654d025Sperrin } else { 170799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 170899653d4eSeschrock "can only attach to mirrors and top-level " 170999653d4eSeschrock "disks")); 17108654d025Sperrin } 171199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 1712fa9e4066Sahrens break; 1713fa9e4066Sahrens 1714ea8dc4b6Seschrock case EINVAL: 1715fa9e4066Sahrens /* 1716fa9e4066Sahrens * The new device must be a single disk. 1717fa9e4066Sahrens */ 171899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 171999653d4eSeschrock "new device must be a single disk")); 172099653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 1721fa9e4066Sahrens break; 1722fa9e4066Sahrens 1723ea8dc4b6Seschrock case EBUSY: 172499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 172599653d4eSeschrock new_disk); 172699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1727fa9e4066Sahrens break; 1728fa9e4066Sahrens 1729ea8dc4b6Seschrock case EOVERFLOW: 1730fa9e4066Sahrens /* 1731fa9e4066Sahrens * The new device is too small. 1732fa9e4066Sahrens */ 173399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 173499653d4eSeschrock "device is too small")); 173599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1736fa9e4066Sahrens break; 1737fa9e4066Sahrens 1738ea8dc4b6Seschrock case EDOM: 1739fa9e4066Sahrens /* 1740fa9e4066Sahrens * The new device has a different alignment requirement. 1741fa9e4066Sahrens */ 174299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 174399653d4eSeschrock "devices have different sector alignment")); 174499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1745fa9e4066Sahrens break; 1746fa9e4066Sahrens 1747ea8dc4b6Seschrock case ENAMETOOLONG: 1748fa9e4066Sahrens /* 1749fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 1750fa9e4066Sahrens */ 175199653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 1752fa9e4066Sahrens break; 1753fa9e4066Sahrens 1754ea8dc4b6Seschrock default: 175599653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1756fa9e4066Sahrens } 1757fa9e4066Sahrens 175899653d4eSeschrock return (-1); 1759fa9e4066Sahrens } 1760fa9e4066Sahrens 1761fa9e4066Sahrens /* 1762fa9e4066Sahrens * Detach the specified device. 1763fa9e4066Sahrens */ 1764fa9e4066Sahrens int 1765fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 1766fa9e4066Sahrens { 1767fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1768fa9e4066Sahrens char msg[1024]; 176999653d4eSeschrock nvlist_t *tgt; 1770fa94a07fSbrendan boolean_t avail_spare, l2cache; 177199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1772fa9e4066Sahrens 1773ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 1774ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 1775ea8dc4b6Seschrock 1776fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1777fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0) 177899653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1779fa9e4066Sahrens 1780a43d325bSek if (avail_spare) 178199653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 178299653d4eSeschrock 1783fa94a07fSbrendan if (l2cache) 1784fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 1785fa94a07fSbrendan 178699653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 178799653d4eSeschrock 1788ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 1789fa9e4066Sahrens return (0); 1790fa9e4066Sahrens 1791fa9e4066Sahrens switch (errno) { 1792fa9e4066Sahrens 1793ea8dc4b6Seschrock case ENOTSUP: 1794fa9e4066Sahrens /* 1795fa9e4066Sahrens * Can't detach from this type of vdev. 1796fa9e4066Sahrens */ 179799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 179899653d4eSeschrock "applicable to mirror and replacing vdevs")); 179999653d4eSeschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 1800fa9e4066Sahrens break; 1801fa9e4066Sahrens 1802ea8dc4b6Seschrock case EBUSY: 1803fa9e4066Sahrens /* 1804fa9e4066Sahrens * There are no other replicas of this device. 1805fa9e4066Sahrens */ 180699653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 1807fa9e4066Sahrens break; 1808fa9e4066Sahrens 1809ea8dc4b6Seschrock default: 181099653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1811ea8dc4b6Seschrock } 1812ea8dc4b6Seschrock 181399653d4eSeschrock return (-1); 181499653d4eSeschrock } 181599653d4eSeschrock 181699653d4eSeschrock /* 1817fa94a07fSbrendan * Remove the given device. Currently, this is supported only for hot spares 1818fa94a07fSbrendan * and level 2 cache devices. 181999653d4eSeschrock */ 182099653d4eSeschrock int 182199653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 182299653d4eSeschrock { 182399653d4eSeschrock zfs_cmd_t zc = { 0 }; 182499653d4eSeschrock char msg[1024]; 182599653d4eSeschrock nvlist_t *tgt; 1826fa94a07fSbrendan boolean_t avail_spare, l2cache; 182799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 182899653d4eSeschrock 182999653d4eSeschrock (void) snprintf(msg, sizeof (msg), 183099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 183199653d4eSeschrock 183299653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1833fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0) 183499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 183599653d4eSeschrock 1836fa94a07fSbrendan if (!avail_spare && !l2cache) { 183799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1838fa94a07fSbrendan "only inactive hot spares or cache devices " 1839fa94a07fSbrendan "can be removed")); 184099653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 184199653d4eSeschrock } 184299653d4eSeschrock 184399653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 184499653d4eSeschrock 1845ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 184699653d4eSeschrock return (0); 184799653d4eSeschrock 184899653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1849ea8dc4b6Seschrock } 1850ea8dc4b6Seschrock 1851ea8dc4b6Seschrock /* 1852ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 1853ea8dc4b6Seschrock */ 1854ea8dc4b6Seschrock int 1855ea8dc4b6Seschrock zpool_clear(zpool_handle_t *zhp, const char *path) 1856ea8dc4b6Seschrock { 1857ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 1858ea8dc4b6Seschrock char msg[1024]; 185999653d4eSeschrock nvlist_t *tgt; 1860fa94a07fSbrendan boolean_t avail_spare, l2cache; 186199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1862ea8dc4b6Seschrock 1863ea8dc4b6Seschrock if (path) 1864ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 1865ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 1866e9dbad6fSeschrock path); 1867ea8dc4b6Seschrock else 1868ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 1869ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 1870ea8dc4b6Seschrock zhp->zpool_name); 1871ea8dc4b6Seschrock 1872ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 187399653d4eSeschrock if (path) { 1874fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 1875fa94a07fSbrendan &l2cache)) == 0) 187699653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 1877ea8dc4b6Seschrock 1878fa94a07fSbrendan /* 1879fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 1880fa94a07fSbrendan * error clearing for l2cache devices. 1881fa94a07fSbrendan */ 1882a43d325bSek if (avail_spare) 188399653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 1884ea8dc4b6Seschrock 188599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 188699653d4eSeschrock &zc.zc_guid) == 0); 1887fa9e4066Sahrens } 1888fa9e4066Sahrens 1889ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 189099653d4eSeschrock return (0); 189199653d4eSeschrock 189299653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1893fa9e4066Sahrens } 1894fa9e4066Sahrens 18953d7072f8Seschrock /* 18963d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 18973d7072f8Seschrock */ 18983d7072f8Seschrock int 18993d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 19003d7072f8Seschrock { 19013d7072f8Seschrock zfs_cmd_t zc = { 0 }; 19023d7072f8Seschrock char msg[1024]; 19033d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 19043d7072f8Seschrock 19053d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 19063d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 19073d7072f8Seschrock guid); 19083d7072f8Seschrock 19093d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 19103d7072f8Seschrock zc.zc_guid = guid; 19113d7072f8Seschrock 19123d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 19133d7072f8Seschrock return (0); 19143d7072f8Seschrock 19153d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 19163d7072f8Seschrock } 19173d7072f8Seschrock 1918f3861e1aSahl /* 1919f3861e1aSahl * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool> 1920f3861e1aSahl * hierarchy. 1921f3861e1aSahl */ 1922f3861e1aSahl int 1923f3861e1aSahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *), 1924f3861e1aSahl void *data) 1925fa9e4066Sahrens { 1926f3861e1aSahl libzfs_handle_t *hdl = zhp->zpool_hdl; 1927f3861e1aSahl char (*paths)[MAXPATHLEN]; 1928f3861e1aSahl size_t size = 4; 1929f3861e1aSahl int curr, fd, base, ret = 0; 1930f3861e1aSahl DIR *dirp; 1931f3861e1aSahl struct dirent *dp; 1932f3861e1aSahl struct stat st; 1933f3861e1aSahl 1934f3861e1aSahl if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0) 1935f3861e1aSahl return (errno == ENOENT ? 0 : -1); 1936f3861e1aSahl 1937f3861e1aSahl if (fstatat(base, zhp->zpool_name, &st, 0) != 0) { 1938f3861e1aSahl int err = errno; 1939f3861e1aSahl (void) close(base); 1940f3861e1aSahl return (err == ENOENT ? 0 : -1); 1941f3861e1aSahl } 1942fa9e4066Sahrens 1943fa9e4066Sahrens /* 1944f3861e1aSahl * Oddly this wasn't a directory -- ignore that failure since we 1945f3861e1aSahl * know there are no links lower in the (non-existant) hierarchy. 1946fa9e4066Sahrens */ 1947f3861e1aSahl if (!S_ISDIR(st.st_mode)) { 1948f3861e1aSahl (void) close(base); 1949f3861e1aSahl return (0); 1950fa9e4066Sahrens } 1951fa9e4066Sahrens 1952f3861e1aSahl if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) { 1953f3861e1aSahl (void) close(base); 1954f3861e1aSahl return (-1); 1955f3861e1aSahl } 1956f3861e1aSahl 1957f3861e1aSahl (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0])); 1958f3861e1aSahl curr = 0; 1959f3861e1aSahl 1960f3861e1aSahl while (curr >= 0) { 1961f3861e1aSahl if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0) 1962f3861e1aSahl goto err; 1963f3861e1aSahl 1964f3861e1aSahl if (S_ISDIR(st.st_mode)) { 1965f3861e1aSahl if ((fd = openat(base, paths[curr], O_RDONLY)) < 0) 1966f3861e1aSahl goto err; 1967f3861e1aSahl 1968f3861e1aSahl if ((dirp = fdopendir(fd)) == NULL) { 1969f3861e1aSahl (void) close(fd); 1970f3861e1aSahl goto err; 1971f3861e1aSahl } 1972f3861e1aSahl 1973f3861e1aSahl while ((dp = readdir(dirp)) != NULL) { 1974f3861e1aSahl if (dp->d_name[0] == '.') 1975f3861e1aSahl continue; 1976f3861e1aSahl 1977f3861e1aSahl if (curr + 1 == size) { 1978f3861e1aSahl paths = zfs_realloc(hdl, paths, 1979f3861e1aSahl size * sizeof (paths[0]), 1980f3861e1aSahl size * 2 * sizeof (paths[0])); 1981f3861e1aSahl if (paths == NULL) { 1982f3861e1aSahl (void) closedir(dirp); 1983f3861e1aSahl (void) close(fd); 1984f3861e1aSahl goto err; 1985f3861e1aSahl } 1986f3861e1aSahl 1987f3861e1aSahl size *= 2; 1988f3861e1aSahl } 1989f3861e1aSahl 1990f3861e1aSahl (void) strlcpy(paths[curr + 1], paths[curr], 1991f3861e1aSahl sizeof (paths[curr + 1])); 1992f3861e1aSahl (void) strlcat(paths[curr], "/", 1993f3861e1aSahl sizeof (paths[curr])); 1994f3861e1aSahl (void) strlcat(paths[curr], dp->d_name, 1995f3861e1aSahl sizeof (paths[curr])); 1996f3861e1aSahl curr++; 1997f3861e1aSahl } 1998f3861e1aSahl 1999f3861e1aSahl (void) closedir(dirp); 2000f3861e1aSahl 2001f3861e1aSahl } else { 2002f3861e1aSahl if ((ret = cb(paths[curr], data)) != 0) 2003f3861e1aSahl break; 2004f3861e1aSahl } 2005f3861e1aSahl 2006f3861e1aSahl curr--; 2007f3861e1aSahl } 2008f3861e1aSahl 2009f3861e1aSahl free(paths); 2010f3861e1aSahl (void) close(base); 2011f3861e1aSahl 2012f3861e1aSahl return (ret); 2013f3861e1aSahl 2014f3861e1aSahl err: 2015f3861e1aSahl free(paths); 2016f3861e1aSahl (void) close(base); 2017f3861e1aSahl return (-1); 2018f3861e1aSahl } 2019f3861e1aSahl 2020f3861e1aSahl typedef struct zvol_cb { 2021f3861e1aSahl zpool_handle_t *zcb_pool; 2022f3861e1aSahl boolean_t zcb_create; 2023f3861e1aSahl } zvol_cb_t; 2024f3861e1aSahl 2025f3861e1aSahl /*ARGSUSED*/ 2026f3861e1aSahl static int 2027f3861e1aSahl do_zvol_create(zfs_handle_t *zhp, void *data) 2028f3861e1aSahl { 20293aefe2c7Sahrens int ret = 0; 2030f3861e1aSahl 20313aefe2c7Sahrens if (ZFS_IS_VOLUME(zhp)) { 2032f3861e1aSahl (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 20333aefe2c7Sahrens ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL); 20343aefe2c7Sahrens } 2035f3861e1aSahl 20363aefe2c7Sahrens if (ret == 0) 20373aefe2c7Sahrens ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL); 2038fa9e4066Sahrens 2039fa9e4066Sahrens zfs_close(zhp); 2040f3861e1aSahl 2041fa9e4066Sahrens return (ret); 2042fa9e4066Sahrens } 2043fa9e4066Sahrens 2044fa9e4066Sahrens /* 2045fa9e4066Sahrens * Iterate over all zvols in the pool and make any necessary minor nodes. 2046fa9e4066Sahrens */ 2047fa9e4066Sahrens int 2048fa9e4066Sahrens zpool_create_zvol_links(zpool_handle_t *zhp) 2049fa9e4066Sahrens { 2050fa9e4066Sahrens zfs_handle_t *zfp; 2051fa9e4066Sahrens int ret; 2052fa9e4066Sahrens 2053fa9e4066Sahrens /* 2054fa9e4066Sahrens * If the pool is unavailable, just return success. 2055fa9e4066Sahrens */ 205699653d4eSeschrock if ((zfp = make_dataset_handle(zhp->zpool_hdl, 205799653d4eSeschrock zhp->zpool_name)) == NULL) 2058fa9e4066Sahrens return (0); 2059fa9e4066Sahrens 20603aefe2c7Sahrens ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL); 2061fa9e4066Sahrens 2062fa9e4066Sahrens zfs_close(zfp); 2063fa9e4066Sahrens return (ret); 2064fa9e4066Sahrens } 2065fa9e4066Sahrens 2066f3861e1aSahl static int 2067f3861e1aSahl do_zvol_remove(const char *dataset, void *data) 2068f3861e1aSahl { 2069f3861e1aSahl zpool_handle_t *zhp = data; 2070f3861e1aSahl 2071f3861e1aSahl return (zvol_remove_link(zhp->zpool_hdl, dataset)); 2072f3861e1aSahl } 2073f3861e1aSahl 2074fa9e4066Sahrens /* 2075f3861e1aSahl * Iterate over all zvols in the pool and remove any minor nodes. We iterate 2076f3861e1aSahl * by examining the /dev links so that a corrupted pool doesn't impede this 2077f3861e1aSahl * operation. 2078fa9e4066Sahrens */ 2079fa9e4066Sahrens int 2080fa9e4066Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp) 2081fa9e4066Sahrens { 2082f3861e1aSahl return (zpool_iter_zvol(zhp, do_zvol_remove, zhp)); 2083fa9e4066Sahrens } 2084c67d9675Seschrock 2085c67d9675Seschrock /* 2086c67d9675Seschrock * Convert from a devid string to a path. 2087c67d9675Seschrock */ 2088c67d9675Seschrock static char * 2089c67d9675Seschrock devid_to_path(char *devid_str) 2090c67d9675Seschrock { 2091c67d9675Seschrock ddi_devid_t devid; 2092c67d9675Seschrock char *minor; 2093c67d9675Seschrock char *path; 2094c67d9675Seschrock devid_nmlist_t *list = NULL; 2095c67d9675Seschrock int ret; 2096c67d9675Seschrock 2097c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 2098c67d9675Seschrock return (NULL); 2099c67d9675Seschrock 2100c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 2101c67d9675Seschrock 2102c67d9675Seschrock devid_str_free(minor); 2103c67d9675Seschrock devid_free(devid); 2104c67d9675Seschrock 2105c67d9675Seschrock if (ret != 0) 2106c67d9675Seschrock return (NULL); 2107c67d9675Seschrock 210899653d4eSeschrock if ((path = strdup(list[0].devname)) == NULL) 210999653d4eSeschrock return (NULL); 211099653d4eSeschrock 2111c67d9675Seschrock devid_free_nmlist(list); 2112c67d9675Seschrock 2113c67d9675Seschrock return (path); 2114c67d9675Seschrock } 2115c67d9675Seschrock 2116c67d9675Seschrock /* 2117c67d9675Seschrock * Convert from a path to a devid string. 2118c67d9675Seschrock */ 2119c67d9675Seschrock static char * 2120c67d9675Seschrock path_to_devid(const char *path) 2121c67d9675Seschrock { 2122c67d9675Seschrock int fd; 2123c67d9675Seschrock ddi_devid_t devid; 2124c67d9675Seschrock char *minor, *ret; 2125c67d9675Seschrock 2126c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 2127c67d9675Seschrock return (NULL); 2128c67d9675Seschrock 2129c67d9675Seschrock minor = NULL; 2130c67d9675Seschrock ret = NULL; 2131c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 2132c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 2133c67d9675Seschrock ret = devid_str_encode(devid, minor); 2134c67d9675Seschrock if (minor != NULL) 2135c67d9675Seschrock devid_str_free(minor); 2136c67d9675Seschrock devid_free(devid); 2137c67d9675Seschrock } 2138c67d9675Seschrock (void) close(fd); 2139c67d9675Seschrock 2140c67d9675Seschrock return (ret); 2141c67d9675Seschrock } 2142c67d9675Seschrock 2143c67d9675Seschrock /* 2144c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 2145c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 2146c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 2147c67d9675Seschrock */ 2148c67d9675Seschrock static void 2149c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 2150c67d9675Seschrock { 2151c67d9675Seschrock zfs_cmd_t zc = { 0 }; 2152c67d9675Seschrock 2153c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2154e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 2155c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2156ea8dc4b6Seschrock &zc.zc_guid) == 0); 2157c67d9675Seschrock 215899653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 2159c67d9675Seschrock } 2160c67d9675Seschrock 2161c67d9675Seschrock /* 2162c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 2163c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 2164c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 2165c67d9675Seschrock * trailing 's0' slice name. 2166c67d9675Seschrock * 2167c67d9675Seschrock * This routine is also responsible for identifying when disks have been 2168c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 2169c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 2170c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 2171c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 2172c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 2173c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 2174c67d9675Seschrock * of these checks. 2175c67d9675Seschrock */ 2176c67d9675Seschrock char * 217799653d4eSeschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) 2178c67d9675Seschrock { 2179c67d9675Seschrock char *path, *devid; 2180ea8dc4b6Seschrock uint64_t value; 2181ea8dc4b6Seschrock char buf[64]; 21823d7072f8Seschrock vdev_stat_t *vs; 21833d7072f8Seschrock uint_t vsc; 2184c67d9675Seschrock 2185ea8dc4b6Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 2186ea8dc4b6Seschrock &value) == 0) { 2187ea8dc4b6Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2188ea8dc4b6Seschrock &value) == 0); 21895ad82045Snd (void) snprintf(buf, sizeof (buf), "%llu", 21905ad82045Snd (u_longlong_t)value); 2191ea8dc4b6Seschrock path = buf; 2192ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 2193c67d9675Seschrock 21943d7072f8Seschrock /* 21953d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 21963d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 21973d7072f8Seschrock * open a misbehaving device, which can have undesirable 21983d7072f8Seschrock * effects. 21993d7072f8Seschrock */ 22003d7072f8Seschrock if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 22013d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 22023d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 22033d7072f8Seschrock zhp != NULL && 2204c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 2205c67d9675Seschrock /* 2206c67d9675Seschrock * Determine if the current path is correct. 2207c67d9675Seschrock */ 2208c67d9675Seschrock char *newdevid = path_to_devid(path); 2209c67d9675Seschrock 2210c67d9675Seschrock if (newdevid == NULL || 2211c67d9675Seschrock strcmp(devid, newdevid) != 0) { 2212c67d9675Seschrock char *newpath; 2213c67d9675Seschrock 2214c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 2215c67d9675Seschrock /* 2216c67d9675Seschrock * Update the path appropriately. 2217c67d9675Seschrock */ 2218c67d9675Seschrock set_path(zhp, nv, newpath); 221999653d4eSeschrock if (nvlist_add_string(nv, 222099653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 222199653d4eSeschrock verify(nvlist_lookup_string(nv, 222299653d4eSeschrock ZPOOL_CONFIG_PATH, 222399653d4eSeschrock &path) == 0); 2224c67d9675Seschrock free(newpath); 2225c67d9675Seschrock } 2226c67d9675Seschrock } 2227c67d9675Seschrock 222899653d4eSeschrock if (newdevid) 222999653d4eSeschrock devid_str_free(newdevid); 2230c67d9675Seschrock } 2231c67d9675Seschrock 2232c67d9675Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 2233c67d9675Seschrock path += 9; 2234c67d9675Seschrock 2235c67d9675Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2236ea8dc4b6Seschrock &value) == 0 && value) { 223799653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 223899653d4eSeschrock if (tmp == NULL) 223999653d4eSeschrock return (NULL); 2240c67d9675Seschrock tmp[strlen(path) - 2] = '\0'; 2241c67d9675Seschrock return (tmp); 2242c67d9675Seschrock } 2243c67d9675Seschrock } else { 2244c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 224599653d4eSeschrock 224699653d4eSeschrock /* 224799653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 224899653d4eSeschrock */ 224999653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 225099653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 225199653d4eSeschrock &value) == 0); 225299653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 22535ad82045Snd (u_longlong_t)value); 225499653d4eSeschrock path = buf; 225599653d4eSeschrock } 2256c67d9675Seschrock } 2257c67d9675Seschrock 225899653d4eSeschrock return (zfs_strdup(hdl, path)); 2259c67d9675Seschrock } 2260ea8dc4b6Seschrock 2261ea8dc4b6Seschrock static int 2262ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b) 2263ea8dc4b6Seschrock { 2264ea8dc4b6Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 2265ea8dc4b6Seschrock } 2266ea8dc4b6Seschrock 2267ea8dc4b6Seschrock /* 2268ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 2269ea8dc4b6Seschrock * caller. 2270ea8dc4b6Seschrock */ 2271ea8dc4b6Seschrock int 227255434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 2273ea8dc4b6Seschrock { 2274ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 2275ea8dc4b6Seschrock uint64_t count; 2276e9dbad6fSeschrock zbookmark_t *zb = NULL; 227755434c77Sek int i; 2278ea8dc4b6Seschrock 2279ea8dc4b6Seschrock /* 2280ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 2281ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 2282ea8dc4b6Seschrock * entire list. 2283ea8dc4b6Seschrock */ 2284ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 2285ea8dc4b6Seschrock &count) == 0); 228675519f38Sek if (count == 0) 228775519f38Sek return (0); 2288e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 22895ad82045Snd count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 229099653d4eSeschrock return (-1); 2291e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 2292ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 2293ea8dc4b6Seschrock for (;;) { 229499653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 229599653d4eSeschrock &zc) != 0) { 2296e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 2297ea8dc4b6Seschrock if (errno == ENOMEM) { 2298bf561db0Svb count = zc.zc_nvlist_dst_size; 2299e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t) 2300bf561db0Svb zfs_alloc(zhp->zpool_hdl, count * 2301bf561db0Svb sizeof (zbookmark_t))) == (uintptr_t)NULL) 230299653d4eSeschrock return (-1); 2303ea8dc4b6Seschrock } else { 2304ea8dc4b6Seschrock return (-1); 2305ea8dc4b6Seschrock } 2306ea8dc4b6Seschrock } else { 2307ea8dc4b6Seschrock break; 2308ea8dc4b6Seschrock } 2309ea8dc4b6Seschrock } 2310ea8dc4b6Seschrock 2311ea8dc4b6Seschrock /* 2312ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 2313ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 2314e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 2315ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 2316ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 2317ea8dc4b6Seschrock */ 2318e9dbad6fSeschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 2319e9dbad6fSeschrock zc.zc_nvlist_dst_size; 2320e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 2321ea8dc4b6Seschrock 2322ea8dc4b6Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 2323ea8dc4b6Seschrock 232455434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 2325ea8dc4b6Seschrock 2326ea8dc4b6Seschrock /* 232755434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers. 2328ea8dc4b6Seschrock */ 2329ea8dc4b6Seschrock for (i = 0; i < count; i++) { 2330ea8dc4b6Seschrock nvlist_t *nv; 2331ea8dc4b6Seschrock 2332c0a81264Sek /* ignoring zb_blkid and zb_level for now */ 2333c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 2334c0a81264Sek zb[i-1].zb_object == zb[i].zb_object) 2335ea8dc4b6Seschrock continue; 2336ea8dc4b6Seschrock 233755434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 233855434c77Sek goto nomem; 233955434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 234055434c77Sek zb[i].zb_objset) != 0) { 234155434c77Sek nvlist_free(nv); 234299653d4eSeschrock goto nomem; 2343ea8dc4b6Seschrock } 234455434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 234555434c77Sek zb[i].zb_object) != 0) { 234655434c77Sek nvlist_free(nv); 234755434c77Sek goto nomem; 234855434c77Sek } 234955434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 235055434c77Sek nvlist_free(nv); 235155434c77Sek goto nomem; 235255434c77Sek } 235355434c77Sek nvlist_free(nv); 2354ea8dc4b6Seschrock } 2355ea8dc4b6Seschrock 23563ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 2357ea8dc4b6Seschrock return (0); 235899653d4eSeschrock 235999653d4eSeschrock nomem: 2360e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 236199653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 2362ea8dc4b6Seschrock } 2363eaca9bbdSeschrock 2364eaca9bbdSeschrock /* 2365eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 2366eaca9bbdSeschrock */ 2367eaca9bbdSeschrock int 2368990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 2369eaca9bbdSeschrock { 2370eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 237199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2372eaca9bbdSeschrock 2373eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 2374990b4856Slling zc.zc_cookie = new_version; 2375990b4856Slling 2376ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 2377ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 237899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 237999653d4eSeschrock zhp->zpool_name)); 2380eaca9bbdSeschrock return (0); 2381eaca9bbdSeschrock } 238206eeb2adSek 238306eeb2adSek void 23842a6b87f0Sek zpool_set_history_str(const char *subcommand, int argc, char **argv, 23852a6b87f0Sek char *history_str) 238606eeb2adSek { 238706eeb2adSek int i; 238806eeb2adSek 23892a6b87f0Sek (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 23902a6b87f0Sek for (i = 1; i < argc; i++) { 23912a6b87f0Sek if (strlen(history_str) + 1 + strlen(argv[i]) > 23922a6b87f0Sek HIS_MAX_RECORD_LEN) 23932a6b87f0Sek break; 23942a6b87f0Sek (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 23952a6b87f0Sek (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 23962a6b87f0Sek } 23972a6b87f0Sek } 23982a6b87f0Sek 23992a6b87f0Sek /* 24002a6b87f0Sek * Stage command history for logging. 24012a6b87f0Sek */ 24022a6b87f0Sek int 24032a6b87f0Sek zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 24042a6b87f0Sek { 24052a6b87f0Sek if (history_str == NULL) 24062a6b87f0Sek return (EINVAL); 24072a6b87f0Sek 24082a6b87f0Sek if (strlen(history_str) > HIS_MAX_RECORD_LEN) 24092a6b87f0Sek return (EINVAL); 24102a6b87f0Sek 2411228975ccSek if (hdl->libzfs_log_str != NULL) 2412ecd6cf80Smarks free(hdl->libzfs_log_str); 241306eeb2adSek 24142a6b87f0Sek if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 24152a6b87f0Sek return (no_memory(hdl)); 241606eeb2adSek 24172a6b87f0Sek return (0); 241806eeb2adSek } 241906eeb2adSek 242006eeb2adSek /* 242106eeb2adSek * Perform ioctl to get some command history of a pool. 242206eeb2adSek * 242306eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 242406eeb2adSek * logical offset of the history buffer to start reading from. 242506eeb2adSek * 242606eeb2adSek * Upon return, 'off' is the next logical offset to read from and 242706eeb2adSek * 'len' is the actual amount of bytes read into 'buf'. 242806eeb2adSek */ 242906eeb2adSek static int 243006eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 243106eeb2adSek { 243206eeb2adSek zfs_cmd_t zc = { 0 }; 243306eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl; 243406eeb2adSek 243506eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 243606eeb2adSek 243706eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf; 243806eeb2adSek zc.zc_history_len = *len; 243906eeb2adSek zc.zc_history_offset = *off; 244006eeb2adSek 244106eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 244206eeb2adSek switch (errno) { 244306eeb2adSek case EPERM: 2444ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 2445ece3d9b3Slling dgettext(TEXT_DOMAIN, 244606eeb2adSek "cannot show history for pool '%s'"), 244706eeb2adSek zhp->zpool_name)); 244806eeb2adSek case ENOENT: 2449ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 245006eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool " 245106eeb2adSek "'%s'"), zhp->zpool_name)); 2452d7306b64Sek case ENOTSUP: 2453d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION, 2454d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool " 2455d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name)); 245606eeb2adSek default: 2457ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 245806eeb2adSek dgettext(TEXT_DOMAIN, 245906eeb2adSek "cannot get history for '%s'"), zhp->zpool_name)); 246006eeb2adSek } 246106eeb2adSek } 246206eeb2adSek 246306eeb2adSek *len = zc.zc_history_len; 246406eeb2adSek *off = zc.zc_history_offset; 246506eeb2adSek 246606eeb2adSek return (0); 246706eeb2adSek } 246806eeb2adSek 246906eeb2adSek /* 247006eeb2adSek * Process the buffer of nvlists, unpacking and storing each nvlist record 247106eeb2adSek * into 'records'. 'leftover' is set to the number of bytes that weren't 247206eeb2adSek * processed as there wasn't a complete record. 247306eeb2adSek */ 247406eeb2adSek static int 247506eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 247606eeb2adSek nvlist_t ***records, uint_t *numrecords) 247706eeb2adSek { 247806eeb2adSek uint64_t reclen; 247906eeb2adSek nvlist_t *nv; 248006eeb2adSek int i; 248106eeb2adSek 248206eeb2adSek while (bytes_read > sizeof (reclen)) { 248306eeb2adSek 248406eeb2adSek /* get length of packed record (stored as little endian) */ 248506eeb2adSek for (i = 0, reclen = 0; i < sizeof (reclen); i++) 248606eeb2adSek reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 248706eeb2adSek 248806eeb2adSek if (bytes_read < sizeof (reclen) + reclen) 248906eeb2adSek break; 249006eeb2adSek 249106eeb2adSek /* unpack record */ 249206eeb2adSek if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 249306eeb2adSek return (ENOMEM); 249406eeb2adSek bytes_read -= sizeof (reclen) + reclen; 249506eeb2adSek buf += sizeof (reclen) + reclen; 249606eeb2adSek 249706eeb2adSek /* add record to nvlist array */ 249806eeb2adSek (*numrecords)++; 249906eeb2adSek if (ISP2(*numrecords + 1)) { 250006eeb2adSek *records = realloc(*records, 250106eeb2adSek *numrecords * 2 * sizeof (nvlist_t *)); 250206eeb2adSek } 250306eeb2adSek (*records)[*numrecords - 1] = nv; 250406eeb2adSek } 250506eeb2adSek 250606eeb2adSek *leftover = bytes_read; 250706eeb2adSek return (0); 250806eeb2adSek } 250906eeb2adSek 251006eeb2adSek #define HIS_BUF_LEN (128*1024) 251106eeb2adSek 251206eeb2adSek /* 251306eeb2adSek * Retrieve the command history of a pool. 251406eeb2adSek */ 251506eeb2adSek int 251606eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 251706eeb2adSek { 251806eeb2adSek char buf[HIS_BUF_LEN]; 251906eeb2adSek uint64_t off = 0; 252006eeb2adSek nvlist_t **records = NULL; 252106eeb2adSek uint_t numrecords = 0; 252206eeb2adSek int err, i; 252306eeb2adSek 252406eeb2adSek do { 252506eeb2adSek uint64_t bytes_read = sizeof (buf); 252606eeb2adSek uint64_t leftover; 252706eeb2adSek 252806eeb2adSek if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 252906eeb2adSek break; 253006eeb2adSek 253106eeb2adSek /* if nothing else was read in, we're at EOF, just return */ 253206eeb2adSek if (!bytes_read) 253306eeb2adSek break; 253406eeb2adSek 253506eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read, 253606eeb2adSek &leftover, &records, &numrecords)) != 0) 253706eeb2adSek break; 253806eeb2adSek off -= leftover; 253906eeb2adSek 254006eeb2adSek /* CONSTCOND */ 254106eeb2adSek } while (1); 254206eeb2adSek 254306eeb2adSek if (!err) { 254406eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 254506eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 254606eeb2adSek records, numrecords) == 0); 254706eeb2adSek } 254806eeb2adSek for (i = 0; i < numrecords; i++) 254906eeb2adSek nvlist_free(records[i]); 255006eeb2adSek free(records); 255106eeb2adSek 255206eeb2adSek return (err); 255306eeb2adSek } 255455434c77Sek 255555434c77Sek void 255655434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 255755434c77Sek char *pathname, size_t len) 255855434c77Sek { 255955434c77Sek zfs_cmd_t zc = { 0 }; 256055434c77Sek boolean_t mounted = B_FALSE; 256155434c77Sek char *mntpnt = NULL; 256255434c77Sek char dsname[MAXNAMELEN]; 256355434c77Sek 256455434c77Sek if (dsobj == 0) { 256555434c77Sek /* special case for the MOS */ 256655434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 256755434c77Sek return; 256855434c77Sek } 256955434c77Sek 257055434c77Sek /* get the dataset's name */ 257155434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 257255434c77Sek zc.zc_obj = dsobj; 257355434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, 257455434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 257555434c77Sek /* just write out a path of two object numbers */ 257655434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 257755434c77Sek dsobj, obj); 257855434c77Sek return; 257955434c77Sek } 258055434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 258155434c77Sek 258255434c77Sek /* find out if the dataset is mounted */ 258355434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 258455434c77Sek 258555434c77Sek /* get the corrupted object's path */ 258655434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 258755434c77Sek zc.zc_obj = obj; 258855434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 258955434c77Sek &zc) == 0) { 259055434c77Sek if (mounted) { 259155434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt, 259255434c77Sek zc.zc_value); 259355434c77Sek } else { 259455434c77Sek (void) snprintf(pathname, len, "%s:%s", 259555434c77Sek dsname, zc.zc_value); 259655434c77Sek } 259755434c77Sek } else { 259855434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 259955434c77Sek } 260055434c77Sek free(mntpnt); 260155434c77Sek } 2602b1b8ab34Slling 26038488aeb5Staylor #define RDISK_ROOT "/dev/rdsk" 26048488aeb5Staylor #define BACKUP_SLICE "s2" 26058488aeb5Staylor /* 26068488aeb5Staylor * Don't start the slice at the default block of 34; many storage 26078488aeb5Staylor * devices will use a stripe width of 128k, so start there instead. 26088488aeb5Staylor */ 26098488aeb5Staylor #define NEW_START_BLOCK 256 26108488aeb5Staylor 261115e6edf1Sgw /* 261215e6edf1Sgw * Read the EFI label from the config, if a label does not exist then 261315e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL 261415e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI 261515e6edf1Sgw * partition. 261615e6edf1Sgw */ 261715e6edf1Sgw static int 261815e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb) 261915e6edf1Sgw { 262015e6edf1Sgw char *path; 262115e6edf1Sgw int fd; 262215e6edf1Sgw char diskname[MAXPATHLEN]; 262315e6edf1Sgw int err = -1; 262415e6edf1Sgw 262515e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 262615e6edf1Sgw return (err); 262715e6edf1Sgw 262815e6edf1Sgw (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 262915e6edf1Sgw strrchr(path, '/')); 263015e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 263115e6edf1Sgw struct dk_gpt *vtoc; 263215e6edf1Sgw 263315e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 263415e6edf1Sgw if (sb != NULL) 263515e6edf1Sgw *sb = vtoc->efi_parts[0].p_start; 263615e6edf1Sgw efi_free(vtoc); 263715e6edf1Sgw } 263815e6edf1Sgw (void) close(fd); 263915e6edf1Sgw } 264015e6edf1Sgw return (err); 264115e6edf1Sgw } 264215e6edf1Sgw 26438488aeb5Staylor /* 26448488aeb5Staylor * determine where a partition starts on a disk in the current 26458488aeb5Staylor * configuration 26468488aeb5Staylor */ 26478488aeb5Staylor static diskaddr_t 26488488aeb5Staylor find_start_block(nvlist_t *config) 26498488aeb5Staylor { 26508488aeb5Staylor nvlist_t **child; 26518488aeb5Staylor uint_t c, children; 26528488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 26538488aeb5Staylor uint64_t wholedisk; 26548488aeb5Staylor 26558488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 26568488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 26578488aeb5Staylor if (nvlist_lookup_uint64(config, 26588488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 26598488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 26608488aeb5Staylor return (MAXOFFSET_T); 26618488aeb5Staylor } 266215e6edf1Sgw if (read_efi_label(config, &sb) < 0) 266315e6edf1Sgw sb = MAXOFFSET_T; 26648488aeb5Staylor return (sb); 26658488aeb5Staylor } 26668488aeb5Staylor 26678488aeb5Staylor for (c = 0; c < children; c++) { 26688488aeb5Staylor sb = find_start_block(child[c]); 26698488aeb5Staylor if (sb != MAXOFFSET_T) { 26708488aeb5Staylor return (sb); 26718488aeb5Staylor } 26728488aeb5Staylor } 26738488aeb5Staylor return (MAXOFFSET_T); 26748488aeb5Staylor } 26758488aeb5Staylor 26768488aeb5Staylor /* 26778488aeb5Staylor * Label an individual disk. The name provided is the short name, 26788488aeb5Staylor * stripped of any leading /dev path. 26798488aeb5Staylor */ 26808488aeb5Staylor int 26818488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 26828488aeb5Staylor { 26838488aeb5Staylor char path[MAXPATHLEN]; 26848488aeb5Staylor struct dk_gpt *vtoc; 26858488aeb5Staylor int fd; 26868488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 26878488aeb5Staylor uint64_t slice_size; 26888488aeb5Staylor diskaddr_t start_block; 26898488aeb5Staylor char errbuf[1024]; 26908488aeb5Staylor 2691c6ef114fSmmusante /* prepare an error message just in case */ 2692c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 2693c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 2694c6ef114fSmmusante 26958488aeb5Staylor if (zhp) { 26968488aeb5Staylor nvlist_t *nvroot; 26978488aeb5Staylor 26988488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 26998488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 27008488aeb5Staylor 27018488aeb5Staylor if (zhp->zpool_start_block == 0) 27028488aeb5Staylor start_block = find_start_block(nvroot); 27038488aeb5Staylor else 27048488aeb5Staylor start_block = zhp->zpool_start_block; 27058488aeb5Staylor zhp->zpool_start_block = start_block; 27068488aeb5Staylor } else { 27078488aeb5Staylor /* new pool */ 27088488aeb5Staylor start_block = NEW_START_BLOCK; 27098488aeb5Staylor } 27108488aeb5Staylor 27118488aeb5Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 27128488aeb5Staylor BACKUP_SLICE); 27138488aeb5Staylor 27148488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 27158488aeb5Staylor /* 27168488aeb5Staylor * This shouldn't happen. We've long since verified that this 27178488aeb5Staylor * is a valid device. 27188488aeb5Staylor */ 2719c6ef114fSmmusante zfs_error_aux(hdl, 2720c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 27218488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 27228488aeb5Staylor } 27238488aeb5Staylor 27248488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 27258488aeb5Staylor /* 27268488aeb5Staylor * The only way this can fail is if we run out of memory, or we 27278488aeb5Staylor * were unable to read the disk's capacity 27288488aeb5Staylor */ 27298488aeb5Staylor if (errno == ENOMEM) 27308488aeb5Staylor (void) no_memory(hdl); 27318488aeb5Staylor 27328488aeb5Staylor (void) close(fd); 2733c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2734c6ef114fSmmusante "unable to read disk capacity"), name); 27358488aeb5Staylor 27368488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 27378488aeb5Staylor } 27388488aeb5Staylor 27398488aeb5Staylor slice_size = vtoc->efi_last_u_lba + 1; 27408488aeb5Staylor slice_size -= EFI_MIN_RESV_SIZE; 27418488aeb5Staylor if (start_block == MAXOFFSET_T) 27428488aeb5Staylor start_block = NEW_START_BLOCK; 27438488aeb5Staylor slice_size -= start_block; 27448488aeb5Staylor 27458488aeb5Staylor vtoc->efi_parts[0].p_start = start_block; 27468488aeb5Staylor vtoc->efi_parts[0].p_size = slice_size; 27478488aeb5Staylor 27488488aeb5Staylor /* 27498488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 27508488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 27518488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 27528488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 27538488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 27548488aeb5Staylor * can get, in the absence of V_OTHER. 27558488aeb5Staylor */ 27568488aeb5Staylor vtoc->efi_parts[0].p_tag = V_USR; 27578488aeb5Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 27588488aeb5Staylor 27598488aeb5Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 27608488aeb5Staylor vtoc->efi_parts[8].p_size = resv; 27618488aeb5Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 27628488aeb5Staylor 27638488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 27648488aeb5Staylor /* 27658488aeb5Staylor * Some block drivers (like pcata) may not support EFI 27668488aeb5Staylor * GPT labels. Print out a helpful error message dir- 27678488aeb5Staylor * ecting the user to manually label the disk and give 27688488aeb5Staylor * a specific slice. 27698488aeb5Staylor */ 27708488aeb5Staylor (void) close(fd); 27718488aeb5Staylor efi_free(vtoc); 27728488aeb5Staylor 27738488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2774c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 27758488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 27768488aeb5Staylor } 27778488aeb5Staylor 27788488aeb5Staylor (void) close(fd); 27798488aeb5Staylor efi_free(vtoc); 27808488aeb5Staylor return (0); 27818488aeb5Staylor } 2782e7cbe64fSgw 2783e7cbe64fSgw static boolean_t 2784e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 2785e7cbe64fSgw { 2786e7cbe64fSgw char *type; 2787e7cbe64fSgw nvlist_t **child; 2788e7cbe64fSgw uint_t children, c; 2789e7cbe64fSgw 2790e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 2791e7cbe64fSgw if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 2792e7cbe64fSgw strcmp(type, VDEV_TYPE_FILE) == 0 || 2793e7cbe64fSgw strcmp(type, VDEV_TYPE_LOG) == 0 || 2794e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) { 2795e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2796e7cbe64fSgw "vdev type '%s' is not supported"), type); 2797e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 2798e7cbe64fSgw return (B_FALSE); 2799e7cbe64fSgw } 2800e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 2801e7cbe64fSgw &child, &children) == 0) { 2802e7cbe64fSgw for (c = 0; c < children; c++) { 2803e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 2804e7cbe64fSgw return (B_FALSE); 2805e7cbe64fSgw } 2806e7cbe64fSgw } 2807e7cbe64fSgw return (B_TRUE); 2808e7cbe64fSgw } 2809e7cbe64fSgw 2810e7cbe64fSgw /* 2811e7cbe64fSgw * check if this zvol is allowable for use as a dump device; zero if 2812e7cbe64fSgw * it is, > 0 if it isn't, < 0 if it isn't a zvol 2813e7cbe64fSgw */ 2814e7cbe64fSgw int 2815e7cbe64fSgw zvol_check_dump_config(char *arg) 2816e7cbe64fSgw { 2817e7cbe64fSgw zpool_handle_t *zhp = NULL; 2818e7cbe64fSgw nvlist_t *config, *nvroot; 2819e7cbe64fSgw char *p, *volname; 2820e7cbe64fSgw nvlist_t **top; 2821e7cbe64fSgw uint_t toplevels; 2822e7cbe64fSgw libzfs_handle_t *hdl; 2823e7cbe64fSgw char errbuf[1024]; 2824e7cbe64fSgw char poolname[ZPOOL_MAXNAMELEN]; 2825e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR); 2826e7cbe64fSgw int ret = 1; 2827e7cbe64fSgw 2828e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 2829e7cbe64fSgw return (-1); 2830e7cbe64fSgw } 2831e7cbe64fSgw 2832e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2833e7cbe64fSgw "dump is not supported on device '%s'"), arg); 2834e7cbe64fSgw 2835e7cbe64fSgw if ((hdl = libzfs_init()) == NULL) 2836e7cbe64fSgw return (1); 2837e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE); 2838e7cbe64fSgw 2839e7cbe64fSgw volname = arg + pathlen; 2840e7cbe64fSgw 2841e7cbe64fSgw /* check the configuration of the pool */ 2842e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) { 2843e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2844e7cbe64fSgw "malformed dataset name")); 2845e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 2846e7cbe64fSgw return (1); 2847e7cbe64fSgw } else if (p - volname >= ZFS_MAXNAMELEN) { 2848e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2849e7cbe64fSgw "dataset name is too long")); 2850e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 2851e7cbe64fSgw return (1); 2852e7cbe64fSgw } else { 2853e7cbe64fSgw (void) strncpy(poolname, volname, p - volname); 2854e7cbe64fSgw poolname[p - volname] = '\0'; 2855e7cbe64fSgw } 2856e7cbe64fSgw 2857e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) { 2858e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2859e7cbe64fSgw "could not open pool '%s'"), poolname); 2860e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 2861e7cbe64fSgw goto out; 2862e7cbe64fSgw } 2863e7cbe64fSgw config = zpool_get_config(zhp, NULL); 2864e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2865e7cbe64fSgw &nvroot) != 0) { 2866e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2867e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname); 2868e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 2869e7cbe64fSgw goto out; 2870e7cbe64fSgw } 2871e7cbe64fSgw 2872e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2873e7cbe64fSgw &top, &toplevels) == 0); 2874e7cbe64fSgw if (toplevels != 1) { 2875e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2876e7cbe64fSgw "'%s' has multiple top level vdevs"), poolname); 2877e7cbe64fSgw (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 2878e7cbe64fSgw goto out; 2879e7cbe64fSgw } 2880e7cbe64fSgw 2881e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 2882e7cbe64fSgw goto out; 2883e7cbe64fSgw } 2884e7cbe64fSgw ret = 0; 2885e7cbe64fSgw 2886e7cbe64fSgw out: 2887e7cbe64fSgw if (zhp) 2888e7cbe64fSgw zpool_close(zhp); 2889e7cbe64fSgw libzfs_fini(hdl); 2890e7cbe64fSgw return (ret); 2891e7cbe64fSgw } 2892