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 /* 23*3f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #include <ctype.h> 27fa9e4066Sahrens #include <errno.h> 28fa9e4066Sahrens #include <devid.h> 29fa9e4066Sahrens #include <fcntl.h> 30fa9e4066Sahrens #include <libintl.h> 31fa9e4066Sahrens #include <stdio.h> 32fa9e4066Sahrens #include <stdlib.h> 33f3861e1aSahl #include <strings.h> 34fa9e4066Sahrens #include <unistd.h> 358488aeb5Staylor #include <sys/efi_partition.h> 368488aeb5Staylor #include <sys/vtoc.h> 37fa9e4066Sahrens #include <sys/zfs_ioctl.h> 38573ca77eSGeorge Wilson #include <dlfcn.h> 39fa9e4066Sahrens 40fa9e4066Sahrens #include "zfs_namecheck.h" 41b1b8ab34Slling #include "zfs_prop.h" 42fa9e4066Sahrens #include "libzfs_impl.h" 43468c413aSTim Haley #include "zfs_comutil.h" 44fa9e4066Sahrens 4515e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 46990b4856Slling 47b5b76fecSGeorge Wilson #if defined(__i386) || defined(__amd64) 48b5b76fecSGeorge Wilson #define BOOTCMD "installgrub(1M)" 49b5b76fecSGeorge Wilson #else 50b5b76fecSGeorge Wilson #define BOOTCMD "installboot(1M)" 51b5b76fecSGeorge Wilson #endif 52b5b76fecSGeorge Wilson 53573ca77eSGeorge Wilson #define DISK_ROOT "/dev/dsk" 54573ca77eSGeorge Wilson #define RDISK_ROOT "/dev/rdsk" 55573ca77eSGeorge Wilson #define BACKUP_SLICE "s2" 56573ca77eSGeorge Wilson 57990b4856Slling /* 58990b4856Slling * ==================================================================== 59990b4856Slling * zpool property functions 60990b4856Slling * ==================================================================== 61990b4856Slling */ 62990b4856Slling 63990b4856Slling static int 64990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 65990b4856Slling { 66990b4856Slling zfs_cmd_t zc = { 0 }; 67990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 68990b4856Slling 69990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 70990b4856Slling 71990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 72990b4856Slling return (-1); 73990b4856Slling 74990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 75990b4856Slling if (errno == ENOMEM) { 76990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 77990b4856Slling zcmd_free_nvlists(&zc); 78990b4856Slling return (-1); 79990b4856Slling } 80990b4856Slling } else { 81990b4856Slling zcmd_free_nvlists(&zc); 82990b4856Slling return (-1); 83990b4856Slling } 84990b4856Slling } 85990b4856Slling 86990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 87990b4856Slling zcmd_free_nvlists(&zc); 88990b4856Slling return (-1); 89990b4856Slling } 90990b4856Slling 91990b4856Slling zcmd_free_nvlists(&zc); 92990b4856Slling 93990b4856Slling return (0); 94990b4856Slling } 95990b4856Slling 96990b4856Slling static int 97990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 98990b4856Slling { 99990b4856Slling nvlist_t *old_props; 100990b4856Slling 101990b4856Slling old_props = zhp->zpool_props; 102990b4856Slling 103990b4856Slling if (zpool_get_all_props(zhp) != 0) 104990b4856Slling return (-1); 105990b4856Slling 106990b4856Slling nvlist_free(old_props); 107990b4856Slling return (0); 108990b4856Slling } 109990b4856Slling 110990b4856Slling static char * 111990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 112990b4856Slling zprop_source_t *src) 113990b4856Slling { 114990b4856Slling nvlist_t *nv, *nvl; 115990b4856Slling uint64_t ival; 116990b4856Slling char *value; 117990b4856Slling zprop_source_t source; 118990b4856Slling 119990b4856Slling nvl = zhp->zpool_props; 120990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 121990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 122990b4856Slling source = ival; 123990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 124990b4856Slling } else { 125990b4856Slling source = ZPROP_SRC_DEFAULT; 126990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 127990b4856Slling value = "-"; 128990b4856Slling } 129990b4856Slling 130990b4856Slling if (src) 131990b4856Slling *src = source; 132990b4856Slling 133990b4856Slling return (value); 134990b4856Slling } 135990b4856Slling 136990b4856Slling uint64_t 137990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 138990b4856Slling { 139990b4856Slling nvlist_t *nv, *nvl; 140990b4856Slling uint64_t value; 141990b4856Slling zprop_source_t source; 142990b4856Slling 143b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 144b87f3af3Sperrin /* 145b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 146b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 147b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 148b87f3af3Sperrin */ 149b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 150b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 151b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 152b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 153b87f3af3Sperrin == 0)) { 154b87f3af3Sperrin return (value); 155b87f3af3Sperrin } 156990b4856Slling return (zpool_prop_default_numeric(prop)); 157b87f3af3Sperrin } 158990b4856Slling 159990b4856Slling nvl = zhp->zpool_props; 160990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 161990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 162990b4856Slling source = value; 163990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 164990b4856Slling } else { 165990b4856Slling source = ZPROP_SRC_DEFAULT; 166990b4856Slling value = zpool_prop_default_numeric(prop); 167990b4856Slling } 168990b4856Slling 169990b4856Slling if (src) 170990b4856Slling *src = source; 171990b4856Slling 172990b4856Slling return (value); 173990b4856Slling } 174990b4856Slling 175990b4856Slling /* 176990b4856Slling * Map VDEV STATE to printed strings. 177990b4856Slling */ 178990b4856Slling char * 179990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 180990b4856Slling { 181990b4856Slling switch (state) { 182990b4856Slling case VDEV_STATE_CLOSED: 183990b4856Slling case VDEV_STATE_OFFLINE: 184990b4856Slling return (gettext("OFFLINE")); 185990b4856Slling case VDEV_STATE_REMOVED: 186990b4856Slling return (gettext("REMOVED")); 187990b4856Slling case VDEV_STATE_CANT_OPEN: 188b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 189990b4856Slling return (gettext("FAULTED")); 1901195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL) 1911195e687SMark J Musante return (gettext("SPLIT")); 192990b4856Slling else 193990b4856Slling return (gettext("UNAVAIL")); 194990b4856Slling case VDEV_STATE_FAULTED: 195990b4856Slling return (gettext("FAULTED")); 196990b4856Slling case VDEV_STATE_DEGRADED: 197990b4856Slling return (gettext("DEGRADED")); 198990b4856Slling case VDEV_STATE_HEALTHY: 199990b4856Slling return (gettext("ONLINE")); 200990b4856Slling } 201990b4856Slling 202990b4856Slling return (gettext("UNKNOWN")); 203990b4856Slling } 204990b4856Slling 205990b4856Slling /* 206990b4856Slling * Get a zpool property value for 'prop' and return the value in 207990b4856Slling * a pre-allocated buffer. 208990b4856Slling */ 209990b4856Slling int 210990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 211990b4856Slling zprop_source_t *srctype) 212990b4856Slling { 213990b4856Slling uint64_t intval; 214990b4856Slling const char *strval; 215990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 216990b4856Slling nvlist_t *nvroot; 217990b4856Slling vdev_stat_t *vs; 218990b4856Slling uint_t vsc; 219990b4856Slling 220990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 221379c004dSEric Schrock switch (prop) { 222379c004dSEric Schrock case ZPOOL_PROP_NAME: 223990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 224379c004dSEric Schrock break; 225379c004dSEric Schrock 226379c004dSEric Schrock case ZPOOL_PROP_HEALTH: 227990b4856Slling (void) strlcpy(buf, "FAULTED", len); 228379c004dSEric Schrock break; 229379c004dSEric Schrock 230379c004dSEric Schrock case ZPOOL_PROP_GUID: 231379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src); 232379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval); 233379c004dSEric Schrock break; 234379c004dSEric Schrock 235379c004dSEric Schrock case ZPOOL_PROP_ALTROOT: 236379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE: 237379c004dSEric Schrock if (zhp->zpool_props != NULL || 238379c004dSEric Schrock zpool_get_all_props(zhp) == 0) { 239379c004dSEric Schrock (void) strlcpy(buf, 240379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src), 241379c004dSEric Schrock len); 242379c004dSEric Schrock if (srctype != NULL) 243379c004dSEric Schrock *srctype = src; 244379c004dSEric Schrock return (0); 245379c004dSEric Schrock } 246379c004dSEric Schrock /* FALLTHROUGH */ 247379c004dSEric Schrock default: 248990b4856Slling (void) strlcpy(buf, "-", len); 249379c004dSEric Schrock break; 250379c004dSEric Schrock } 251379c004dSEric Schrock 252379c004dSEric Schrock if (srctype != NULL) 253379c004dSEric Schrock *srctype = src; 254990b4856Slling return (0); 255990b4856Slling } 256990b4856Slling 257990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 258990b4856Slling prop != ZPOOL_PROP_NAME) 259990b4856Slling return (-1); 260990b4856Slling 261990b4856Slling switch (zpool_prop_get_type(prop)) { 262990b4856Slling case PROP_TYPE_STRING: 263990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 264990b4856Slling len); 265990b4856Slling break; 266990b4856Slling 267990b4856Slling case PROP_TYPE_NUMBER: 268990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 269990b4856Slling 270990b4856Slling switch (prop) { 271990b4856Slling case ZPOOL_PROP_SIZE: 272485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED: 273485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE: 274990b4856Slling (void) zfs_nicenum(intval, buf, len); 275990b4856Slling break; 276990b4856Slling 277990b4856Slling case ZPOOL_PROP_CAPACITY: 278990b4856Slling (void) snprintf(buf, len, "%llu%%", 279990b4856Slling (u_longlong_t)intval); 280990b4856Slling break; 281990b4856Slling 282b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO: 283b24ab676SJeff Bonwick (void) snprintf(buf, len, "%llu.%02llux", 284b24ab676SJeff Bonwick (u_longlong_t)(intval / 100), 285b24ab676SJeff Bonwick (u_longlong_t)(intval % 100)); 286b24ab676SJeff Bonwick break; 287b24ab676SJeff Bonwick 288990b4856Slling case ZPOOL_PROP_HEALTH: 289990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 290990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 291990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 292*3f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 293*3f9d6ad7SLin Ling == 0); 294990b4856Slling 295990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 296990b4856Slling vs->vs_aux), len); 297990b4856Slling break; 298990b4856Slling default: 299990b4856Slling (void) snprintf(buf, len, "%llu", intval); 300990b4856Slling } 301990b4856Slling break; 302990b4856Slling 303990b4856Slling case PROP_TYPE_INDEX: 304990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 305990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 306990b4856Slling != 0) 307990b4856Slling return (-1); 308990b4856Slling (void) strlcpy(buf, strval, len); 309990b4856Slling break; 310990b4856Slling 311990b4856Slling default: 312990b4856Slling abort(); 313990b4856Slling } 314990b4856Slling 315990b4856Slling if (srctype) 316990b4856Slling *srctype = src; 317990b4856Slling 318990b4856Slling return (0); 319990b4856Slling } 320990b4856Slling 321990b4856Slling /* 322990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 323990b4856Slling * Assuming bootfs is a valid dataset name. 324990b4856Slling */ 325990b4856Slling static boolean_t 326990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 327990b4856Slling { 328990b4856Slling int len = strlen(pool); 329990b4856Slling 330fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 331990b4856Slling return (B_FALSE); 332990b4856Slling 333990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 334990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 335990b4856Slling return (B_TRUE); 336990b4856Slling 337990b4856Slling return (B_FALSE); 338990b4856Slling } 339990b4856Slling 34015e6edf1Sgw /* 34115e6edf1Sgw * Inspect the configuration to determine if any of the devices contain 34215e6edf1Sgw * an EFI label. 34315e6edf1Sgw */ 34415e6edf1Sgw static boolean_t 34515e6edf1Sgw pool_uses_efi(nvlist_t *config) 34615e6edf1Sgw { 34715e6edf1Sgw nvlist_t **child; 34815e6edf1Sgw uint_t c, children; 34915e6edf1Sgw 35015e6edf1Sgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 35115e6edf1Sgw &child, &children) != 0) 35215e6edf1Sgw return (read_efi_label(config, NULL) >= 0); 35315e6edf1Sgw 35415e6edf1Sgw for (c = 0; c < children; c++) { 35515e6edf1Sgw if (pool_uses_efi(child[c])) 35615e6edf1Sgw return (B_TRUE); 35715e6edf1Sgw } 35815e6edf1Sgw return (B_FALSE); 35915e6edf1Sgw } 36015e6edf1Sgw 361b5b76fecSGeorge Wilson static boolean_t 362b5b76fecSGeorge Wilson pool_is_bootable(zpool_handle_t *zhp) 363b5b76fecSGeorge Wilson { 364b5b76fecSGeorge Wilson char bootfs[ZPOOL_MAXNAMELEN]; 365b5b76fecSGeorge Wilson 366b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 367b5b76fecSGeorge Wilson sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 368b5b76fecSGeorge Wilson sizeof (bootfs)) != 0); 369b5b76fecSGeorge Wilson } 370b5b76fecSGeorge Wilson 371b5b76fecSGeorge Wilson 372990b4856Slling /* 373990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 374990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 375990b4856Slling * specified as strings. 376990b4856Slling */ 377990b4856Slling static nvlist_t * 3780a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 379990b4856Slling nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf) 380990b4856Slling { 381990b4856Slling nvpair_t *elem; 382990b4856Slling nvlist_t *retprops; 383990b4856Slling zpool_prop_t prop; 384990b4856Slling char *strval; 385990b4856Slling uint64_t intval; 3862f8aaab3Seschrock char *slash; 3872f8aaab3Seschrock struct stat64 statbuf; 38815e6edf1Sgw zpool_handle_t *zhp; 38915e6edf1Sgw nvlist_t *nvroot; 390990b4856Slling 391990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 392990b4856Slling (void) no_memory(hdl); 393990b4856Slling return (NULL); 394990b4856Slling } 395990b4856Slling 396990b4856Slling elem = NULL; 397990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 398990b4856Slling const char *propname = nvpair_name(elem); 399990b4856Slling 400990b4856Slling /* 401990b4856Slling * Make sure this property is valid and applies to this type. 402990b4856Slling */ 403990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 404990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 405990b4856Slling "invalid property '%s'"), propname); 406990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 407990b4856Slling goto error; 408990b4856Slling } 409990b4856Slling 410990b4856Slling if (zpool_prop_readonly(prop)) { 411990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 412990b4856Slling "is readonly"), propname); 413990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 414990b4856Slling goto error; 415990b4856Slling } 416990b4856Slling 417990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 418990b4856Slling &strval, &intval, errbuf) != 0) 419990b4856Slling goto error; 420990b4856Slling 421990b4856Slling /* 422990b4856Slling * Perform additional checking for specific properties. 423990b4856Slling */ 424990b4856Slling switch (prop) { 425990b4856Slling case ZPOOL_PROP_VERSION: 426990b4856Slling if (intval < version || intval > SPA_VERSION) { 427990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 428990b4856Slling "property '%s' number %d is invalid."), 429990b4856Slling propname, intval); 430990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 431990b4856Slling goto error; 432990b4856Slling } 433990b4856Slling break; 434990b4856Slling 435990b4856Slling case ZPOOL_PROP_BOOTFS: 436990b4856Slling if (create_or_import) { 437990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 438990b4856Slling "property '%s' cannot be set at creation " 439990b4856Slling "or import time"), propname); 440990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 441990b4856Slling goto error; 442990b4856Slling } 443990b4856Slling 444990b4856Slling if (version < SPA_VERSION_BOOTFS) { 445990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 446990b4856Slling "pool must be upgraded to support " 447990b4856Slling "'%s' property"), propname); 448990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 449990b4856Slling goto error; 450990b4856Slling } 451990b4856Slling 452990b4856Slling /* 453990b4856Slling * bootfs property value has to be a dataset name and 454990b4856Slling * the dataset has to be in the same pool as it sets to. 455990b4856Slling */ 456990b4856Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 457990b4856Slling strval)) { 458990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 459990b4856Slling "is an invalid name"), strval); 460990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 461990b4856Slling goto error; 462990b4856Slling } 46315e6edf1Sgw 46415e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 46515e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 46615e6edf1Sgw "could not open pool '%s'"), poolname); 46715e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 46815e6edf1Sgw goto error; 46915e6edf1Sgw } 47015e6edf1Sgw verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 47115e6edf1Sgw ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 47215e6edf1Sgw 47315e6edf1Sgw /* 47415e6edf1Sgw * bootfs property cannot be set on a disk which has 47515e6edf1Sgw * been EFI labeled. 47615e6edf1Sgw */ 47715e6edf1Sgw if (pool_uses_efi(nvroot)) { 47815e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 47915e6edf1Sgw "property '%s' not supported on " 48015e6edf1Sgw "EFI labeled devices"), propname); 48115e6edf1Sgw (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 48215e6edf1Sgw zpool_close(zhp); 48315e6edf1Sgw goto error; 48415e6edf1Sgw } 48515e6edf1Sgw zpool_close(zhp); 486990b4856Slling break; 487990b4856Slling 4882f8aaab3Seschrock case ZPOOL_PROP_ALTROOT: 489990b4856Slling if (!create_or_import) { 490990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 491990b4856Slling "property '%s' can only be set during pool " 492990b4856Slling "creation or import"), propname); 493990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 494990b4856Slling goto error; 495990b4856Slling } 496990b4856Slling 4972f8aaab3Seschrock if (strval[0] != '/') { 498990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4992f8aaab3Seschrock "bad alternate root '%s'"), strval); 5002f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 501990b4856Slling goto error; 502990b4856Slling } 5032f8aaab3Seschrock break; 5042f8aaab3Seschrock 5052f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5062f8aaab3Seschrock if (strval[0] == '\0') 5072f8aaab3Seschrock break; 5082f8aaab3Seschrock 5092f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5102f8aaab3Seschrock break; 511990b4856Slling 512990b4856Slling if (strval[0] != '/') { 513990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5142f8aaab3Seschrock "property '%s' must be empty, an " 5152f8aaab3Seschrock "absolute path, or 'none'"), propname); 516990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 517990b4856Slling goto error; 518990b4856Slling } 519990b4856Slling 5202f8aaab3Seschrock slash = strrchr(strval, '/'); 521990b4856Slling 5222f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5232f8aaab3Seschrock strcmp(slash, "/..") == 0) { 5242f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5252f8aaab3Seschrock "'%s' is not a valid file"), strval); 5262f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5272f8aaab3Seschrock goto error; 5282f8aaab3Seschrock } 529990b4856Slling 5302f8aaab3Seschrock *slash = '\0'; 5312f8aaab3Seschrock 5322c32020fSeschrock if (strval[0] != '\0' && 5332c32020fSeschrock (stat64(strval, &statbuf) != 0 || 5342c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 5352f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5362f8aaab3Seschrock "'%s' is not a valid directory"), 5372f8aaab3Seschrock strval); 5382f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5392f8aaab3Seschrock goto error; 5402f8aaab3Seschrock } 5412f8aaab3Seschrock 5422f8aaab3Seschrock *slash = '/'; 5432f8aaab3Seschrock break; 544990b4856Slling } 545990b4856Slling } 546990b4856Slling 547990b4856Slling return (retprops); 548990b4856Slling error: 549990b4856Slling nvlist_free(retprops); 550990b4856Slling return (NULL); 551990b4856Slling } 552990b4856Slling 553990b4856Slling /* 554990b4856Slling * Set zpool property : propname=propval. 555990b4856Slling */ 556990b4856Slling int 557990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 558990b4856Slling { 559990b4856Slling zfs_cmd_t zc = { 0 }; 560990b4856Slling int ret = -1; 561990b4856Slling char errbuf[1024]; 562990b4856Slling nvlist_t *nvl = NULL; 563990b4856Slling nvlist_t *realprops; 564990b4856Slling uint64_t version; 565990b4856Slling 566990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 567990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 568990b4856Slling zhp->zpool_name); 569990b4856Slling 570990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 571990b4856Slling return (no_memory(zhp->zpool_hdl)); 572990b4856Slling 573990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 574990b4856Slling nvlist_free(nvl); 575990b4856Slling return (no_memory(zhp->zpool_hdl)); 576990b4856Slling } 577990b4856Slling 578990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 5790a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 580990b4856Slling zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) { 581990b4856Slling nvlist_free(nvl); 582990b4856Slling return (-1); 583990b4856Slling } 584990b4856Slling 585990b4856Slling nvlist_free(nvl); 586990b4856Slling nvl = realprops; 587990b4856Slling 588990b4856Slling /* 589990b4856Slling * Execute the corresponding ioctl() to set this property. 590990b4856Slling */ 591990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 592990b4856Slling 593990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 594990b4856Slling nvlist_free(nvl); 595990b4856Slling return (-1); 596990b4856Slling } 597990b4856Slling 598990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 599990b4856Slling 600990b4856Slling zcmd_free_nvlists(&zc); 601990b4856Slling nvlist_free(nvl); 602990b4856Slling 603990b4856Slling if (ret) 604990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 605990b4856Slling else 606990b4856Slling (void) zpool_props_refresh(zhp); 607990b4856Slling 608990b4856Slling return (ret); 609990b4856Slling } 610990b4856Slling 611990b4856Slling int 612990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 613990b4856Slling { 614990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 615990b4856Slling zprop_list_t *entry; 616990b4856Slling char buf[ZFS_MAXPROPLEN]; 617990b4856Slling 618990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 619990b4856Slling return (-1); 620990b4856Slling 621990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 622990b4856Slling 623990b4856Slling if (entry->pl_fixed) 624990b4856Slling continue; 625990b4856Slling 626990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 627990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 628990b4856Slling NULL) == 0) { 629990b4856Slling if (strlen(buf) > entry->pl_width) 630990b4856Slling entry->pl_width = strlen(buf); 631990b4856Slling } 632990b4856Slling } 633990b4856Slling 634990b4856Slling return (0); 635990b4856Slling } 636990b4856Slling 637990b4856Slling 638573ca77eSGeorge Wilson /* 639573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage 640573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead. 641573ca77eSGeorge Wilson */ 642573ca77eSGeorge Wilson #define NEW_START_BLOCK 256 643573ca77eSGeorge Wilson 644fa9e4066Sahrens /* 645fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 646fa9e4066Sahrens * 'buf'. 647fa9e4066Sahrens */ 648e7cbe64fSgw boolean_t 64999653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 650fa9e4066Sahrens { 651fa9e4066Sahrens namecheck_err_t why; 652fa9e4066Sahrens char what; 653b468a217Seschrock int ret; 654b468a217Seschrock 655b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 656b468a217Seschrock 657b468a217Seschrock /* 658b468a217Seschrock * The rules for reserved pool names were extended at a later point. 659b468a217Seschrock * But we need to support users with existing pools that may now be 660b468a217Seschrock * invalid. So we only check for this expanded set of names during a 661b468a217Seschrock * create (or import), and only in userland. 662b468a217Seschrock */ 663b468a217Seschrock if (ret == 0 && !isopen && 664b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 665b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 6668654d025Sperrin strncmp(pool, "spare", 5) == 0 || 6678654d025Sperrin strcmp(pool, "log") == 0)) { 668e7cbe64fSgw if (hdl != NULL) 669e7cbe64fSgw zfs_error_aux(hdl, 670e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved")); 67199653d4eSeschrock return (B_FALSE); 672b468a217Seschrock } 673b468a217Seschrock 674fa9e4066Sahrens 675b468a217Seschrock if (ret != 0) { 67699653d4eSeschrock if (hdl != NULL) { 677fa9e4066Sahrens switch (why) { 678b81d61a6Slling case NAME_ERR_TOOLONG: 67999653d4eSeschrock zfs_error_aux(hdl, 680b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 681b81d61a6Slling break; 682b81d61a6Slling 683fa9e4066Sahrens case NAME_ERR_INVALCHAR: 68499653d4eSeschrock zfs_error_aux(hdl, 685fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 686fa9e4066Sahrens "'%c' in pool name"), what); 687fa9e4066Sahrens break; 688fa9e4066Sahrens 689fa9e4066Sahrens case NAME_ERR_NOLETTER: 69099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 69199653d4eSeschrock "name must begin with a letter")); 692fa9e4066Sahrens break; 693fa9e4066Sahrens 694fa9e4066Sahrens case NAME_ERR_RESERVED: 69599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 69699653d4eSeschrock "name is reserved")); 697fa9e4066Sahrens break; 698fa9e4066Sahrens 699fa9e4066Sahrens case NAME_ERR_DISKLIKE: 70099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 70199653d4eSeschrock "pool name is reserved")); 702fa9e4066Sahrens break; 7035ad82045Snd 7045ad82045Snd case NAME_ERR_LEADING_SLASH: 7055ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7065ad82045Snd "leading slash in name")); 7075ad82045Snd break; 7085ad82045Snd 7095ad82045Snd case NAME_ERR_EMPTY_COMPONENT: 7105ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7115ad82045Snd "empty component in name")); 7125ad82045Snd break; 7135ad82045Snd 7145ad82045Snd case NAME_ERR_TRAILING_SLASH: 7155ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7165ad82045Snd "trailing slash in name")); 7175ad82045Snd break; 7185ad82045Snd 7195ad82045Snd case NAME_ERR_MULTIPLE_AT: 7205ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7215ad82045Snd "multiple '@' delimiters in name")); 7225ad82045Snd break; 7235ad82045Snd 724fa9e4066Sahrens } 725fa9e4066Sahrens } 72699653d4eSeschrock return (B_FALSE); 727fa9e4066Sahrens } 728fa9e4066Sahrens 72999653d4eSeschrock return (B_TRUE); 730fa9e4066Sahrens } 731fa9e4066Sahrens 732fa9e4066Sahrens /* 733fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 734fa9e4066Sahrens * state. 735fa9e4066Sahrens */ 736fa9e4066Sahrens zpool_handle_t * 73799653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 738fa9e4066Sahrens { 739fa9e4066Sahrens zpool_handle_t *zhp; 74094de1d4cSeschrock boolean_t missing; 741fa9e4066Sahrens 742fa9e4066Sahrens /* 743fa9e4066Sahrens * Make sure the pool name is valid. 744fa9e4066Sahrens */ 74599653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 746ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 74799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 74899653d4eSeschrock pool); 749fa9e4066Sahrens return (NULL); 750fa9e4066Sahrens } 751fa9e4066Sahrens 75299653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 75399653d4eSeschrock return (NULL); 754fa9e4066Sahrens 75599653d4eSeschrock zhp->zpool_hdl = hdl; 756fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 757fa9e4066Sahrens 75894de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 75994de1d4cSeschrock zpool_close(zhp); 76094de1d4cSeschrock return (NULL); 76194de1d4cSeschrock } 76294de1d4cSeschrock 76394de1d4cSeschrock if (missing) { 764990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 765ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 766990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 76794de1d4cSeschrock zpool_close(zhp); 76894de1d4cSeschrock return (NULL); 769fa9e4066Sahrens } 770fa9e4066Sahrens 771fa9e4066Sahrens return (zhp); 772fa9e4066Sahrens } 773fa9e4066Sahrens 774fa9e4066Sahrens /* 775fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 776fa9e4066Sahrens * the configuration cache may be out of date). 777fa9e4066Sahrens */ 77894de1d4cSeschrock int 77994de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 780fa9e4066Sahrens { 781fa9e4066Sahrens zpool_handle_t *zhp; 78294de1d4cSeschrock boolean_t missing; 783fa9e4066Sahrens 78494de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 78594de1d4cSeschrock return (-1); 786fa9e4066Sahrens 78799653d4eSeschrock zhp->zpool_hdl = hdl; 788fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 789fa9e4066Sahrens 79094de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 79194de1d4cSeschrock zpool_close(zhp); 79294de1d4cSeschrock return (-1); 793fa9e4066Sahrens } 794fa9e4066Sahrens 79594de1d4cSeschrock if (missing) { 79694de1d4cSeschrock zpool_close(zhp); 79794de1d4cSeschrock *ret = NULL; 79894de1d4cSeschrock return (0); 79994de1d4cSeschrock } 80094de1d4cSeschrock 80194de1d4cSeschrock *ret = zhp; 80294de1d4cSeschrock return (0); 803fa9e4066Sahrens } 804fa9e4066Sahrens 805fa9e4066Sahrens /* 806fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 807fa9e4066Sahrens * state. 808fa9e4066Sahrens */ 809fa9e4066Sahrens zpool_handle_t * 81099653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 811fa9e4066Sahrens { 812fa9e4066Sahrens zpool_handle_t *zhp; 813fa9e4066Sahrens 81499653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 815fa9e4066Sahrens return (NULL); 816fa9e4066Sahrens 817fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 818ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 81999653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 820fa9e4066Sahrens zpool_close(zhp); 821fa9e4066Sahrens return (NULL); 822fa9e4066Sahrens } 823fa9e4066Sahrens 824fa9e4066Sahrens return (zhp); 825fa9e4066Sahrens } 826fa9e4066Sahrens 827fa9e4066Sahrens /* 828fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 829fa9e4066Sahrens */ 830fa9e4066Sahrens void 831fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 832fa9e4066Sahrens { 833fa9e4066Sahrens if (zhp->zpool_config) 834fa9e4066Sahrens nvlist_free(zhp->zpool_config); 835088e9d47Seschrock if (zhp->zpool_old_config) 836088e9d47Seschrock nvlist_free(zhp->zpool_old_config); 837b1b8ab34Slling if (zhp->zpool_props) 838b1b8ab34Slling nvlist_free(zhp->zpool_props); 839fa9e4066Sahrens free(zhp); 840fa9e4066Sahrens } 841fa9e4066Sahrens 842fa9e4066Sahrens /* 843fa9e4066Sahrens * Return the name of the pool. 844fa9e4066Sahrens */ 845fa9e4066Sahrens const char * 846fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 847fa9e4066Sahrens { 848fa9e4066Sahrens return (zhp->zpool_name); 849fa9e4066Sahrens } 850fa9e4066Sahrens 851fa9e4066Sahrens 852fa9e4066Sahrens /* 853fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 854fa9e4066Sahrens */ 855fa9e4066Sahrens int 856fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 857fa9e4066Sahrens { 858fa9e4066Sahrens return (zhp->zpool_state); 859fa9e4066Sahrens } 860fa9e4066Sahrens 861fa9e4066Sahrens /* 862fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 863fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 864fa9e4066Sahrens * don't have to worry about error semantics. 865fa9e4066Sahrens */ 866fa9e4066Sahrens int 86799653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 8680a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 869fa9e4066Sahrens { 870fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 8710a48a24eStimh nvlist_t *zc_fsprops = NULL; 8720a48a24eStimh nvlist_t *zc_props = NULL; 87399653d4eSeschrock char msg[1024]; 874990b4856Slling char *altroot; 8750a48a24eStimh int ret = -1; 876fa9e4066Sahrens 87799653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 87899653d4eSeschrock "cannot create '%s'"), pool); 879fa9e4066Sahrens 88099653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 88199653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 882fa9e4066Sahrens 883351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 884990b4856Slling return (-1); 885fa9e4066Sahrens 8860a48a24eStimh if (props) { 8870a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 8880a48a24eStimh SPA_VERSION_1, B_TRUE, msg)) == NULL) { 8890a48a24eStimh goto create_failed; 8900a48a24eStimh } 8910a48a24eStimh } 89299653d4eSeschrock 8930a48a24eStimh if (fsprops) { 8940a48a24eStimh uint64_t zoned; 8950a48a24eStimh char *zonestr; 8960a48a24eStimh 8970a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 8980a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 8990a48a24eStimh strcmp(zonestr, "on") == 0); 9000a48a24eStimh 9010a48a24eStimh if ((zc_fsprops = zfs_valid_proplist(hdl, 9020a48a24eStimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 9030a48a24eStimh goto create_failed; 9040a48a24eStimh } 9050a48a24eStimh if (!zc_props && 9060a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 9070a48a24eStimh goto create_failed; 9080a48a24eStimh } 9090a48a24eStimh if (nvlist_add_nvlist(zc_props, 9100a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 9110a48a24eStimh goto create_failed; 9120a48a24eStimh } 913351420b3Slling } 914fa9e4066Sahrens 9150a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 9160a48a24eStimh goto create_failed; 9170a48a24eStimh 918990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 919fa9e4066Sahrens 9200a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 921351420b3Slling 922e9dbad6fSeschrock zcmd_free_nvlists(&zc); 9230a48a24eStimh nvlist_free(zc_props); 9240a48a24eStimh nvlist_free(zc_fsprops); 925fa9e4066Sahrens 92699653d4eSeschrock switch (errno) { 927fa9e4066Sahrens case EBUSY: 928fa9e4066Sahrens /* 929fa9e4066Sahrens * This can happen if the user has specified the same 930fa9e4066Sahrens * device multiple times. We can't reliably detect this 931fa9e4066Sahrens * until we try to add it and see we already have a 932fa9e4066Sahrens * label. 933fa9e4066Sahrens */ 93499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 93599653d4eSeschrock "one or more vdevs refer to the same device")); 93699653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 937fa9e4066Sahrens 938fa9e4066Sahrens case EOVERFLOW: 939fa9e4066Sahrens /* 94099653d4eSeschrock * This occurs when one of the devices is below 941fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 942fa9e4066Sahrens * device was the problem device since there's no 943fa9e4066Sahrens * reliable way to determine device size from userland. 944fa9e4066Sahrens */ 945fa9e4066Sahrens { 946fa9e4066Sahrens char buf[64]; 947fa9e4066Sahrens 948fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 949fa9e4066Sahrens 95099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 95199653d4eSeschrock "one or more devices is less than the " 95299653d4eSeschrock "minimum size (%s)"), buf); 953fa9e4066Sahrens } 95499653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 955fa9e4066Sahrens 956fa9e4066Sahrens case ENOSPC: 95799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 95899653d4eSeschrock "one or more devices is out of space")); 95999653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 960fa9e4066Sahrens 961fa94a07fSbrendan case ENOTBLK: 962fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 963fa94a07fSbrendan "cache device must be a disk or disk slice")); 964fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 965fa94a07fSbrendan 966fa9e4066Sahrens default: 96799653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 968fa9e4066Sahrens } 969fa9e4066Sahrens } 970fa9e4066Sahrens 971fa9e4066Sahrens /* 972fa9e4066Sahrens * If this is an alternate root pool, then we automatically set the 973e9dbad6fSeschrock * mountpoint of the root dataset to be '/'. 974fa9e4066Sahrens */ 975990b4856Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 976990b4856Slling &altroot) == 0) { 977fa9e4066Sahrens zfs_handle_t *zhp; 978fa9e4066Sahrens 979990b4856Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 980e9dbad6fSeschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 981e9dbad6fSeschrock "/") == 0); 982fa9e4066Sahrens 983fa9e4066Sahrens zfs_close(zhp); 984fa9e4066Sahrens } 985fa9e4066Sahrens 9860a48a24eStimh create_failed: 987351420b3Slling zcmd_free_nvlists(&zc); 9880a48a24eStimh nvlist_free(zc_props); 9890a48a24eStimh nvlist_free(zc_fsprops); 9900a48a24eStimh return (ret); 991fa9e4066Sahrens } 992fa9e4066Sahrens 993fa9e4066Sahrens /* 994fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 995fa9e4066Sahrens * datasets left in the pool. 996fa9e4066Sahrens */ 997fa9e4066Sahrens int 998fa9e4066Sahrens zpool_destroy(zpool_handle_t *zhp) 999fa9e4066Sahrens { 1000fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1001fa9e4066Sahrens zfs_handle_t *zfp = NULL; 100299653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 100399653d4eSeschrock char msg[1024]; 1004fa9e4066Sahrens 1005fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 100699653d4eSeschrock (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 100799653d4eSeschrock ZFS_TYPE_FILESYSTEM)) == NULL) 1008fa9e4066Sahrens return (-1); 1009fa9e4066Sahrens 1010fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1011fa9e4066Sahrens 1012ecd6cf80Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 101399653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 101499653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 1015fa9e4066Sahrens 101699653d4eSeschrock if (errno == EROFS) { 101799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 101899653d4eSeschrock "one or more devices is read only")); 101999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 102099653d4eSeschrock } else { 102199653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1022fa9e4066Sahrens } 1023fa9e4066Sahrens 1024fa9e4066Sahrens if (zfp) 1025fa9e4066Sahrens zfs_close(zfp); 1026fa9e4066Sahrens return (-1); 1027fa9e4066Sahrens } 1028fa9e4066Sahrens 1029fa9e4066Sahrens if (zfp) { 1030fa9e4066Sahrens remove_mountpoint(zfp); 1031fa9e4066Sahrens zfs_close(zfp); 1032fa9e4066Sahrens } 1033fa9e4066Sahrens 1034fa9e4066Sahrens return (0); 1035fa9e4066Sahrens } 1036fa9e4066Sahrens 1037fa9e4066Sahrens /* 1038fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1039fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1040fa9e4066Sahrens */ 1041fa9e4066Sahrens int 1042fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1043fa9e4066Sahrens { 1044e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 104599653d4eSeschrock int ret; 104699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 104799653d4eSeschrock char msg[1024]; 1048fa94a07fSbrendan nvlist_t **spares, **l2cache; 1049fa94a07fSbrendan uint_t nspares, nl2cache; 105099653d4eSeschrock 105199653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 105299653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 105399653d4eSeschrock 1054fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1055fa94a07fSbrendan SPA_VERSION_SPARES && 105699653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 105799653d4eSeschrock &spares, &nspares) == 0) { 105899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 105999653d4eSeschrock "upgraded to add hot spares")); 106099653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 106199653d4eSeschrock } 1062fa9e4066Sahrens 1063b5b76fecSGeorge Wilson if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 1064b5b76fecSGeorge Wilson ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 1065b5b76fecSGeorge Wilson uint64_t s; 1066b5b76fecSGeorge Wilson 1067b5b76fecSGeorge Wilson for (s = 0; s < nspares; s++) { 1068b5b76fecSGeorge Wilson char *path; 1069b5b76fecSGeorge Wilson 1070b5b76fecSGeorge Wilson if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 1071b5b76fecSGeorge Wilson &path) == 0 && pool_uses_efi(spares[s])) { 1072b5b76fecSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1073b5b76fecSGeorge Wilson "device '%s' contains an EFI label and " 1074b5b76fecSGeorge Wilson "cannot be used on root pools."), 107588ecc943SGeorge Wilson zpool_vdev_name(hdl, NULL, spares[s], 107688ecc943SGeorge Wilson B_FALSE)); 1077b5b76fecSGeorge Wilson return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 1078b5b76fecSGeorge Wilson } 1079b5b76fecSGeorge Wilson } 1080b5b76fecSGeorge Wilson } 1081b5b76fecSGeorge Wilson 1082fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1083fa94a07fSbrendan SPA_VERSION_L2CACHE && 1084fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1085fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1086fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1087fa94a07fSbrendan "upgraded to add cache devices")); 1088fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1089fa94a07fSbrendan } 1090fa94a07fSbrendan 1091990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 109299653d4eSeschrock return (-1); 1093fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1094fa9e4066Sahrens 1095ecd6cf80Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1096fa9e4066Sahrens switch (errno) { 1097fa9e4066Sahrens case EBUSY: 1098fa9e4066Sahrens /* 1099fa9e4066Sahrens * This can happen if the user has specified the same 1100fa9e4066Sahrens * device multiple times. We can't reliably detect this 1101fa9e4066Sahrens * until we try to add it and see we already have a 1102fa9e4066Sahrens * label. 1103fa9e4066Sahrens */ 110499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 110599653d4eSeschrock "one or more vdevs refer to the same device")); 110699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1107fa9e4066Sahrens break; 1108fa9e4066Sahrens 1109fa9e4066Sahrens case EOVERFLOW: 1110fa9e4066Sahrens /* 1111fa9e4066Sahrens * This occurrs when one of the devices is below 1112fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1113fa9e4066Sahrens * device was the problem device since there's no 1114fa9e4066Sahrens * reliable way to determine device size from userland. 1115fa9e4066Sahrens */ 1116fa9e4066Sahrens { 1117fa9e4066Sahrens char buf[64]; 1118fa9e4066Sahrens 1119fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1120fa9e4066Sahrens 112199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 112299653d4eSeschrock "device is less than the minimum " 112399653d4eSeschrock "size (%s)"), buf); 1124fa9e4066Sahrens } 112599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 112699653d4eSeschrock break; 112799653d4eSeschrock 112899653d4eSeschrock case ENOTSUP: 112999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11308654d025Sperrin "pool must be upgraded to add these vdevs")); 113199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1132fa9e4066Sahrens break; 1133fa9e4066Sahrens 1134b1b8ab34Slling case EDOM: 1135b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11368654d025Sperrin "root pool can not have multiple vdevs" 11378654d025Sperrin " or separate logs")); 1138b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1139b1b8ab34Slling break; 1140b1b8ab34Slling 1141fa94a07fSbrendan case ENOTBLK: 1142fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1143fa94a07fSbrendan "cache device must be a disk or disk slice")); 1144fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1145fa94a07fSbrendan break; 1146fa94a07fSbrendan 1147fa9e4066Sahrens default: 114899653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1149fa9e4066Sahrens } 1150fa9e4066Sahrens 115199653d4eSeschrock ret = -1; 115299653d4eSeschrock } else { 115399653d4eSeschrock ret = 0; 1154fa9e4066Sahrens } 1155fa9e4066Sahrens 1156e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1157fa9e4066Sahrens 115899653d4eSeschrock return (ret); 1159fa9e4066Sahrens } 1160fa9e4066Sahrens 1161fa9e4066Sahrens /* 1162fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1163fa9e4066Sahrens * mounted datasets in the pool. 1164fa9e4066Sahrens */ 1165fa9e4066Sahrens int 1166394ab0cbSGeorge Wilson zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1167fa9e4066Sahrens { 1168fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 116989a89ebfSlling char msg[1024]; 1170fa9e4066Sahrens 117189a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 117289a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 117389a89ebfSlling 1174fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 117589a89ebfSlling zc.zc_cookie = force; 1176394ab0cbSGeorge Wilson zc.zc_guid = hardforce; 117789a89ebfSlling 117889a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 117989a89ebfSlling switch (errno) { 118089a89ebfSlling case EXDEV: 118189a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 118289a89ebfSlling "use '-f' to override the following errors:\n" 118389a89ebfSlling "'%s' has an active shared spare which could be" 118489a89ebfSlling " used by other pools once '%s' is exported."), 118589a89ebfSlling zhp->zpool_name, zhp->zpool_name); 118689a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 118789a89ebfSlling msg)); 118889a89ebfSlling default: 118989a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 119089a89ebfSlling msg)); 119189a89ebfSlling } 119289a89ebfSlling } 1193fa9e4066Sahrens 1194fa9e4066Sahrens return (0); 1195fa9e4066Sahrens } 1196fa9e4066Sahrens 1197394ab0cbSGeorge Wilson int 1198394ab0cbSGeorge Wilson zpool_export(zpool_handle_t *zhp, boolean_t force) 1199394ab0cbSGeorge Wilson { 1200394ab0cbSGeorge Wilson return (zpool_export_common(zhp, force, B_FALSE)); 1201394ab0cbSGeorge Wilson } 1202394ab0cbSGeorge Wilson 1203394ab0cbSGeorge Wilson int 1204394ab0cbSGeorge Wilson zpool_export_force(zpool_handle_t *zhp) 1205394ab0cbSGeorge Wilson { 1206394ab0cbSGeorge Wilson return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 1207394ab0cbSGeorge Wilson } 1208394ab0cbSGeorge Wilson 1209468c413aSTim Haley static void 1210468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 1211468c413aSTim Haley nvlist_t *rbi) 1212468c413aSTim Haley { 1213468c413aSTim Haley uint64_t rewindto; 1214468c413aSTim Haley int64_t loss = -1; 1215468c413aSTim Haley struct tm t; 1216468c413aSTim Haley char timestr[128]; 1217468c413aSTim Haley 1218468c413aSTim Haley if (!hdl->libzfs_printerr || rbi == NULL) 1219468c413aSTim Haley return; 1220468c413aSTim Haley 1221468c413aSTim Haley if (nvlist_lookup_uint64(rbi, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1222468c413aSTim Haley return; 1223468c413aSTim Haley (void) nvlist_lookup_int64(rbi, ZPOOL_CONFIG_REWIND_TIME, &loss); 1224468c413aSTim Haley 1225468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1226468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1227468c413aSTim Haley if (dryrun) { 1228468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1229468c413aSTim Haley "Would be able to return %s " 1230468c413aSTim Haley "to its state as of %s.\n"), 1231468c413aSTim Haley name, timestr); 1232468c413aSTim Haley } else { 1233468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1234468c413aSTim Haley "Pool %s returned to its state as of %s.\n"), 1235468c413aSTim Haley name, timestr); 1236468c413aSTim Haley } 1237468c413aSTim Haley if (loss > 120) { 1238468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1239468c413aSTim Haley "%s approximately %lld "), 1240468c413aSTim Haley dryrun ? "Would discard" : "Discarded", 1241468c413aSTim Haley (loss + 30) / 60); 1242468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1243468c413aSTim Haley "minutes of transactions.\n")); 1244468c413aSTim Haley } else if (loss > 0) { 1245468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1246468c413aSTim Haley "%s approximately %lld "), 1247468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss); 1248468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1249468c413aSTim Haley "seconds of transactions.\n")); 1250468c413aSTim Haley } 1251468c413aSTim Haley } 1252468c413aSTim Haley } 1253468c413aSTim Haley 1254468c413aSTim Haley void 1255468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1256468c413aSTim Haley nvlist_t *config) 1257468c413aSTim Haley { 1258468c413aSTim Haley int64_t loss = -1; 1259468c413aSTim Haley uint64_t edata = UINT64_MAX; 1260468c413aSTim Haley uint64_t rewindto; 1261468c413aSTim Haley struct tm t; 1262468c413aSTim Haley char timestr[128]; 1263468c413aSTim Haley 1264468c413aSTim Haley if (!hdl->libzfs_printerr) 1265468c413aSTim Haley return; 1266468c413aSTim Haley 1267468c413aSTim Haley if (reason >= 0) 1268468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1269468c413aSTim Haley else 1270468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1271468c413aSTim Haley 1272468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 1273468c413aSTim Haley if (nvlist_lookup_uint64(config, 1274468c413aSTim Haley ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1275468c413aSTim Haley goto no_info; 1276468c413aSTim Haley 1277468c413aSTim Haley (void) nvlist_lookup_int64(config, ZPOOL_CONFIG_REWIND_TIME, &loss); 1278468c413aSTim Haley (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1279468c413aSTim Haley &edata); 1280468c413aSTim Haley 1281468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1282468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n")); 1283468c413aSTim Haley 1284468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1285468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1286468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1287468c413aSTim Haley "\tReturning the pool to its state as of %s\n" 1288468c413aSTim Haley "\tshould correct the problem. "), 1289468c413aSTim Haley timestr); 1290468c413aSTim Haley } else { 1291468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1292468c413aSTim Haley "\tReverting the pool to an earlier state " 1293468c413aSTim Haley "should correct the problem.\n\t")); 1294468c413aSTim Haley } 1295468c413aSTim Haley 1296468c413aSTim Haley if (loss > 120) { 1297468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1298468c413aSTim Haley "Approximately %lld minutes of data\n" 1299468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1300468c413aSTim Haley } else if (loss > 0) { 1301468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1302468c413aSTim Haley "Approximately %lld seconds of data\n" 1303468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss); 1304468c413aSTim Haley } 1305468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) { 1306468c413aSTim Haley if (edata == 1) { 1307468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1308468c413aSTim Haley "After rewind, at least\n" 1309468c413aSTim Haley "\tone persistent user-data error will remain. ")); 1310468c413aSTim Haley } else { 1311468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1312468c413aSTim Haley "After rewind, several\n" 1313468c413aSTim Haley "\tpersistent user-data errors will remain. ")); 1314468c413aSTim Haley } 1315468c413aSTim Haley } 1316468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1317a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1318a33cae98STim Haley reason >= 0 ? "clear" : "import", name); 1319468c413aSTim Haley 1320468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1321468c413aSTim Haley "A scrub of the pool\n" 1322468c413aSTim Haley "\tis strongly recommended after recovery.\n")); 1323468c413aSTim Haley return; 1324468c413aSTim Haley 1325468c413aSTim Haley no_info: 1326468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1327468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n")); 1328468c413aSTim Haley } 1329468c413aSTim Haley 1330fa9e4066Sahrens /* 1331990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1332990b4856Slling * if possible. 1333990b4856Slling * 1334990b4856Slling * Applications should use zpool_import_props() to import a pool with 1335990b4856Slling * new properties value to be set. 1336fa9e4066Sahrens */ 1337fa9e4066Sahrens int 133899653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1339990b4856Slling char *altroot) 1340990b4856Slling { 1341990b4856Slling nvlist_t *props = NULL; 1342990b4856Slling int ret; 1343990b4856Slling 1344990b4856Slling if (altroot != NULL) { 1345990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1346990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1347990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1348990b4856Slling newname)); 1349990b4856Slling } 1350990b4856Slling 1351990b4856Slling if (nvlist_add_string(props, 1352352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1353352d8027SGeorge Wilson nvlist_add_string(props, 1354352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1355990b4856Slling nvlist_free(props); 1356990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1357990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1358990b4856Slling newname)); 1359990b4856Slling } 1360990b4856Slling } 1361990b4856Slling 1362c5904d13Seschrock ret = zpool_import_props(hdl, config, newname, props, B_FALSE); 1363990b4856Slling if (props) 1364990b4856Slling nvlist_free(props); 1365990b4856Slling return (ret); 1366990b4856Slling } 1367990b4856Slling 1368990b4856Slling /* 1369990b4856Slling * Import the given pool using the known configuration and a list of 1370990b4856Slling * properties to be set. The configuration should have come from 1371990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1372990b4856Slling * is imported with a different name. 1373990b4856Slling */ 1374990b4856Slling int 1375990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1376c5904d13Seschrock nvlist_t *props, boolean_t importfaulted) 1377fa9e4066Sahrens { 1378e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1379468c413aSTim Haley zpool_rewind_policy_t policy; 1380468c413aSTim Haley nvlist_t *nvi = NULL; 1381fa9e4066Sahrens char *thename; 1382fa9e4066Sahrens char *origname; 1383468c413aSTim Haley uint64_t returned_size; 1384fa9e4066Sahrens int ret; 1385990b4856Slling char errbuf[1024]; 1386fa9e4066Sahrens 1387fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1388fa9e4066Sahrens &origname) == 0); 1389fa9e4066Sahrens 1390990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1391990b4856Slling "cannot import pool '%s'"), origname); 1392990b4856Slling 1393fa9e4066Sahrens if (newname != NULL) { 139499653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1395ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 139699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 139799653d4eSeschrock newname)); 1398fa9e4066Sahrens thename = (char *)newname; 1399fa9e4066Sahrens } else { 1400fa9e4066Sahrens thename = origname; 1401fa9e4066Sahrens } 1402fa9e4066Sahrens 1403990b4856Slling if (props) { 1404990b4856Slling uint64_t version; 1405fa9e4066Sahrens 1406990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1407990b4856Slling &version) == 0); 1408fa9e4066Sahrens 14090a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1410351420b3Slling props, version, B_TRUE, errbuf)) == NULL) { 1411990b4856Slling return (-1); 1412351420b3Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1413351420b3Slling nvlist_free(props); 1414990b4856Slling return (-1); 1415351420b3Slling } 1416990b4856Slling } 1417990b4856Slling 1418990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1419fa9e4066Sahrens 1420fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1421ea8dc4b6Seschrock &zc.zc_guid) == 0); 1422fa9e4066Sahrens 1423351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1424351420b3Slling nvlist_free(props); 142599653d4eSeschrock return (-1); 1426351420b3Slling } 1427468c413aSTim Haley returned_size = zc.zc_nvlist_conf_size + 512; 1428468c413aSTim Haley if (zcmd_alloc_dst_nvlist(hdl, &zc, returned_size) != 0) { 1429468c413aSTim Haley nvlist_free(props); 1430468c413aSTim Haley return (-1); 1431468c413aSTim Haley } 1432fa9e4066Sahrens 1433c5904d13Seschrock zc.zc_cookie = (uint64_t)importfaulted; 1434fa9e4066Sahrens ret = 0; 1435ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) { 1436fa9e4066Sahrens char desc[1024]; 1437468c413aSTim Haley 1438468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 1439468c413aSTim Haley zpool_get_rewind_policy(config, &policy); 1440468c413aSTim Haley /* 1441468c413aSTim Haley * Dry-run failed, but we print out what success 1442468c413aSTim Haley * looks like if we found a best txg 1443468c413aSTim Haley */ 1444468c413aSTim Haley if ((policy.zrp_request & ZPOOL_TRY_REWIND) && nvi) { 1445468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 1446468c413aSTim Haley B_TRUE, nvi); 1447468c413aSTim Haley nvlist_free(nvi); 1448468c413aSTim Haley return (-1); 1449468c413aSTim Haley } 1450468c413aSTim Haley 1451fa9e4066Sahrens if (newname == NULL) 1452fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1453fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1454fa9e4066Sahrens thename); 1455fa9e4066Sahrens else 1456fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1457fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1458fa9e4066Sahrens origname, thename); 1459fa9e4066Sahrens 1460fa9e4066Sahrens switch (errno) { 1461ea8dc4b6Seschrock case ENOTSUP: 1462ea8dc4b6Seschrock /* 1463ea8dc4b6Seschrock * Unsupported version. 1464ea8dc4b6Seschrock */ 146599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1466ea8dc4b6Seschrock break; 1467ea8dc4b6Seschrock 1468b5989ec7Seschrock case EINVAL: 1469b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1470b5989ec7Seschrock break; 1471b5989ec7Seschrock 147254a91118SChris Kirby case EROFS: 147354a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 147454a91118SChris Kirby "one or more devices is read only")); 147554a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc); 147654a91118SChris Kirby break; 147754a91118SChris Kirby 1478fa9e4066Sahrens default: 1479468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 148099653d4eSeschrock (void) zpool_standard_error(hdl, errno, desc); 1481468c413aSTim Haley zpool_explain_recover(hdl, 1482468c413aSTim Haley newname ? origname : thename, -errno, nvi); 1483468c413aSTim Haley nvlist_free(nvi); 1484468c413aSTim Haley break; 1485fa9e4066Sahrens } 1486fa9e4066Sahrens 1487fa9e4066Sahrens ret = -1; 1488fa9e4066Sahrens } else { 1489fa9e4066Sahrens zpool_handle_t *zhp; 1490ecd6cf80Smarks 1491fa9e4066Sahrens /* 1492fa9e4066Sahrens * This should never fail, but play it safe anyway. 1493fa9e4066Sahrens */ 1494681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0) 149594de1d4cSeschrock ret = -1; 1496681d9761SEric Taylor else if (zhp != NULL) 1497fa9e4066Sahrens zpool_close(zhp); 1498468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 1499468c413aSTim Haley zpool_get_rewind_policy(config, &policy); 1500468c413aSTim Haley if (policy.zrp_request & 1501468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1502468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 1503468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 1504468c413aSTim Haley nvi); 1505468c413aSTim Haley } 1506468c413aSTim Haley nvlist_free(nvi); 1507468c413aSTim Haley return (0); 1508fa9e4066Sahrens } 1509fa9e4066Sahrens 1510e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1511351420b3Slling nvlist_free(props); 1512351420b3Slling 1513fa9e4066Sahrens return (ret); 1514fa9e4066Sahrens } 1515fa9e4066Sahrens 1516fa9e4066Sahrens /* 1517*3f9d6ad7SLin Ling * Scan the pool. 1518fa9e4066Sahrens */ 1519fa9e4066Sahrens int 1520*3f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1521fa9e4066Sahrens { 1522fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1523fa9e4066Sahrens char msg[1024]; 152499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1525fa9e4066Sahrens 1526fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1527*3f9d6ad7SLin Ling zc.zc_cookie = func; 1528fa9e4066Sahrens 1529*3f9d6ad7SLin Ling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 1530*3f9d6ad7SLin Ling (errno == ENOENT && func != POOL_SCAN_NONE)) 1531fa9e4066Sahrens return (0); 1532fa9e4066Sahrens 1533*3f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) { 1534*3f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 1535*3f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 1536*3f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) { 1537*3f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 1538*3f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 1539*3f9d6ad7SLin Ling zc.zc_name); 1540*3f9d6ad7SLin Ling } else { 1541*3f9d6ad7SLin Ling assert(!"unexpected result"); 1542*3f9d6ad7SLin Ling } 1543fa9e4066Sahrens 1544*3f9d6ad7SLin Ling if (errno == EBUSY) { 1545*3f9d6ad7SLin Ling nvlist_t *nvroot; 1546*3f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 1547*3f9d6ad7SLin Ling uint_t psc; 1548*3f9d6ad7SLin Ling 1549*3f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config, 1550*3f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 1551*3f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 1552*3f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 1553*3f9d6ad7SLin Ling if (ps && ps->pss_func == POOL_SCAN_SCRUB) 1554*3f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 1555*3f9d6ad7SLin Ling else 1556*3f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg)); 1557*3f9d6ad7SLin Ling } else if (errno == ENOENT) { 1558*3f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 1559*3f9d6ad7SLin Ling } else { 156099653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1561*3f9d6ad7SLin Ling } 1562fa9e4066Sahrens } 1563fa9e4066Sahrens 1564a43d325bSek /* 1565573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the 1566573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device. 1567a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1568a43d325bSek * spare; but FALSE if its an INUSE spare. 1569a43d325bSek */ 157099653d4eSeschrock static nvlist_t * 1571573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 1572573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log) 1573ea8dc4b6Seschrock { 1574ea8dc4b6Seschrock uint_t c, children; 1575ea8dc4b6Seschrock nvlist_t **child; 157699653d4eSeschrock nvlist_t *ret; 1577ee0eb9f2SEric Schrock uint64_t is_log; 1578573ca77eSGeorge Wilson char *srchkey; 1579573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL); 1580573ca77eSGeorge Wilson 1581573ca77eSGeorge Wilson /* Nothing to look for */ 1582573ca77eSGeorge Wilson if (search == NULL || pair == NULL) 1583573ca77eSGeorge Wilson return (NULL); 1584ea8dc4b6Seschrock 1585573ca77eSGeorge Wilson /* Obtain the key we will use to search */ 1586573ca77eSGeorge Wilson srchkey = nvpair_name(pair); 1587573ca77eSGeorge Wilson 1588573ca77eSGeorge Wilson switch (nvpair_type(pair)) { 1589573ca77eSGeorge Wilson case DATA_TYPE_UINT64: { 1590573ca77eSGeorge Wilson uint64_t srchval, theguid, present; 1591573ca77eSGeorge Wilson 1592573ca77eSGeorge Wilson verify(nvpair_value_uint64(pair, &srchval) == 0); 1593573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 1594573ca77eSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1595573ca77eSGeorge Wilson &present) == 0) { 1596573ca77eSGeorge Wilson /* 1597573ca77eSGeorge Wilson * If the device has never been present since 1598573ca77eSGeorge Wilson * import, the only reliable way to match the 1599573ca77eSGeorge Wilson * vdev is by GUID. 1600573ca77eSGeorge Wilson */ 1601573ca77eSGeorge Wilson verify(nvlist_lookup_uint64(nv, 1602573ca77eSGeorge Wilson ZPOOL_CONFIG_GUID, &theguid) == 0); 1603573ca77eSGeorge Wilson if (theguid == srchval) 1604573ca77eSGeorge Wilson return (nv); 1605573ca77eSGeorge Wilson } 1606573ca77eSGeorge Wilson } 1607573ca77eSGeorge Wilson break; 1608573ca77eSGeorge Wilson } 1609573ca77eSGeorge Wilson 1610573ca77eSGeorge Wilson case DATA_TYPE_STRING: { 1611573ca77eSGeorge Wilson char *srchval, *val; 1612573ca77eSGeorge Wilson 1613573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0); 1614573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0) 1615573ca77eSGeorge Wilson break; 1616ea8dc4b6Seschrock 1617ea8dc4b6Seschrock /* 1618573ca77eSGeorge Wilson * Search for the requested value. We special case the search 161988ecc943SGeorge Wilson * for ZPOOL_CONFIG_PATH when it's a wholedisk and when 162088ecc943SGeorge Wilson * Looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 162188ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares. 1622ea8dc4b6Seschrock */ 1623573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && val) { 1624573ca77eSGeorge Wilson uint64_t wholedisk = 0; 1625573ca77eSGeorge Wilson 1626573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1627573ca77eSGeorge Wilson &wholedisk); 1628573ca77eSGeorge Wilson if (wholedisk) { 1629573ca77eSGeorge Wilson /* 1630573ca77eSGeorge Wilson * For whole disks, the internal path has 's0', 1631573ca77eSGeorge Wilson * but the path passed in by the user doesn't. 1632573ca77eSGeorge Wilson */ 1633573ca77eSGeorge Wilson if (strlen(srchval) == strlen(val) - 2 && 1634573ca77eSGeorge Wilson strncmp(srchval, val, strlen(srchval)) == 0) 1635573ca77eSGeorge Wilson return (nv); 1636573ca77eSGeorge Wilson break; 1637573ca77eSGeorge Wilson } 163888ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 163988ecc943SGeorge Wilson char *type, *idx, *end, *p; 164088ecc943SGeorge Wilson uint64_t id, vdev_id; 164188ecc943SGeorge Wilson 164288ecc943SGeorge Wilson /* 164388ecc943SGeorge Wilson * Determine our vdev type, keeping in mind 164488ecc943SGeorge Wilson * that the srchval is composed of a type and 164588ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4). 164688ecc943SGeorge Wilson */ 164788ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL) 164888ecc943SGeorge Wilson return (NULL); 164988ecc943SGeorge Wilson 165088ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) { 165188ecc943SGeorge Wilson free(type); 165288ecc943SGeorge Wilson break; 165388ecc943SGeorge Wilson } 165488ecc943SGeorge Wilson idx = p + 1; 165588ecc943SGeorge Wilson *p = '\0'; 165688ecc943SGeorge Wilson 165788ecc943SGeorge Wilson /* 165888ecc943SGeorge Wilson * If the types don't match then keep looking. 165988ecc943SGeorge Wilson */ 166088ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) { 166188ecc943SGeorge Wilson free(type); 166288ecc943SGeorge Wilson break; 166388ecc943SGeorge Wilson } 166488ecc943SGeorge Wilson 166588ecc943SGeorge Wilson verify(strncmp(type, VDEV_TYPE_RAIDZ, 166688ecc943SGeorge Wilson strlen(VDEV_TYPE_RAIDZ)) == 0 || 166788ecc943SGeorge Wilson strncmp(type, VDEV_TYPE_MIRROR, 166888ecc943SGeorge Wilson strlen(VDEV_TYPE_MIRROR)) == 0); 166988ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 167088ecc943SGeorge Wilson &id) == 0); 167188ecc943SGeorge Wilson 167288ecc943SGeorge Wilson errno = 0; 167388ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10); 167488ecc943SGeorge Wilson 167588ecc943SGeorge Wilson free(type); 167688ecc943SGeorge Wilson if (errno != 0) 167788ecc943SGeorge Wilson return (NULL); 167888ecc943SGeorge Wilson 167988ecc943SGeorge Wilson /* 168088ecc943SGeorge Wilson * Now verify that we have the correct vdev id. 168188ecc943SGeorge Wilson */ 168288ecc943SGeorge Wilson if (vdev_id == id) 168388ecc943SGeorge Wilson return (nv); 1684ea8dc4b6Seschrock } 1685573ca77eSGeorge Wilson 1686573ca77eSGeorge Wilson /* 1687573ca77eSGeorge Wilson * Common case 1688573ca77eSGeorge Wilson */ 1689573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0) 1690573ca77eSGeorge Wilson return (nv); 1691573ca77eSGeorge Wilson break; 1692573ca77eSGeorge Wilson } 1693573ca77eSGeorge Wilson 1694573ca77eSGeorge Wilson default: 1695573ca77eSGeorge Wilson break; 1696ea8dc4b6Seschrock } 1697ea8dc4b6Seschrock 1698ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1699ea8dc4b6Seschrock &child, &children) != 0) 170099653d4eSeschrock return (NULL); 1701ea8dc4b6Seschrock 1702ee0eb9f2SEric Schrock for (c = 0; c < children; c++) { 1703573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 1704ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 1705ee0eb9f2SEric Schrock /* 1706ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel 1707ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the 1708ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where 1709ee0eb9f2SEric Schrock * 'log' is non-NULL). 1710ee0eb9f2SEric Schrock */ 1711ee0eb9f2SEric Schrock if (log != NULL && 1712ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c], 1713ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 1714ee0eb9f2SEric Schrock is_log) { 1715ee0eb9f2SEric Schrock *log = B_TRUE; 1716ee0eb9f2SEric Schrock } 1717ea8dc4b6Seschrock return (ret); 1718ee0eb9f2SEric Schrock } 1719ee0eb9f2SEric Schrock } 1720ea8dc4b6Seschrock 172199653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 172299653d4eSeschrock &child, &children) == 0) { 172399653d4eSeschrock for (c = 0; c < children; c++) { 1724573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 1725ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 1726a43d325bSek *avail_spare = B_TRUE; 172799653d4eSeschrock return (ret); 172899653d4eSeschrock } 172999653d4eSeschrock } 173099653d4eSeschrock } 173199653d4eSeschrock 1732fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1733fa94a07fSbrendan &child, &children) == 0) { 1734fa94a07fSbrendan for (c = 0; c < children; c++) { 1735573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 1736ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 1737fa94a07fSbrendan *l2cache = B_TRUE; 1738fa94a07fSbrendan return (ret); 1739fa94a07fSbrendan } 1740fa94a07fSbrendan } 1741fa94a07fSbrendan } 1742fa94a07fSbrendan 174399653d4eSeschrock return (NULL); 1744ea8dc4b6Seschrock } 1745ea8dc4b6Seschrock 1746573ca77eSGeorge Wilson /* 1747573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the 1748573ca77eSGeorge Wilson * associated vdev. 1749573ca77eSGeorge Wilson */ 1750573ca77eSGeorge Wilson nvlist_t * 1751573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 1752573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 1753573ca77eSGeorge Wilson { 1754573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret; 1755573ca77eSGeorge Wilson 1756573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1757573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 1758573ca77eSGeorge Wilson 1759573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1760573ca77eSGeorge Wilson &nvroot) == 0); 1761573ca77eSGeorge Wilson 1762573ca77eSGeorge Wilson *avail_spare = B_FALSE; 1763573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 1764573ca77eSGeorge Wilson nvlist_free(search); 1765573ca77eSGeorge Wilson 1766573ca77eSGeorge Wilson return (ret); 1767573ca77eSGeorge Wilson } 1768573ca77eSGeorge Wilson 176988ecc943SGeorge Wilson /* 177088ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 177188ecc943SGeorge Wilson */ 177288ecc943SGeorge Wilson boolean_t 177388ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name) 177488ecc943SGeorge Wilson { 177588ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 177688ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 177788ecc943SGeorge Wilson return (B_TRUE); 177888ecc943SGeorge Wilson return (B_FALSE); 177988ecc943SGeorge Wilson } 178088ecc943SGeorge Wilson 178199653d4eSeschrock nvlist_t * 1782fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 1783ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log) 1784ea8dc4b6Seschrock { 1785ea8dc4b6Seschrock char buf[MAXPATHLEN]; 1786ea8dc4b6Seschrock char *end; 1787573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret; 1788ea8dc4b6Seschrock uint64_t guid; 1789ea8dc4b6Seschrock 1790573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1791573ca77eSGeorge Wilson 17920917b783Seschrock guid = strtoull(path, &end, 10); 1793ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 1794573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 179588ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) { 179688ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 1797ea8dc4b6Seschrock } else if (path[0] != '/') { 1798ea8dc4b6Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 1799573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 1800ea8dc4b6Seschrock } else { 1801573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 1802ea8dc4b6Seschrock } 1803ea8dc4b6Seschrock 1804ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1805ea8dc4b6Seschrock &nvroot) == 0); 1806ea8dc4b6Seschrock 1807a43d325bSek *avail_spare = B_FALSE; 1808fa94a07fSbrendan *l2cache = B_FALSE; 1809ee0eb9f2SEric Schrock if (log != NULL) 1810ee0eb9f2SEric Schrock *log = B_FALSE; 1811573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 1812573ca77eSGeorge Wilson nvlist_free(search); 1813573ca77eSGeorge Wilson 1814573ca77eSGeorge Wilson return (ret); 1815a43d325bSek } 1816a43d325bSek 181719397407SSherry Moore static int 181819397407SSherry Moore vdev_online(nvlist_t *nv) 181919397407SSherry Moore { 182019397407SSherry Moore uint64_t ival; 182119397407SSherry Moore 182219397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 182319397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 182419397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 182519397407SSherry Moore return (0); 182619397407SSherry Moore 182719397407SSherry Moore return (1); 182819397407SSherry Moore } 182919397407SSherry Moore 183019397407SSherry Moore /* 183121ecdf64SLin Ling * Helper function for zpool_get_physpaths(). 183219397407SSherry Moore */ 1833753a6d45SSherry Moore static int 183421ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 1835753a6d45SSherry Moore size_t *bytes_written) 183619397407SSherry Moore { 1837753a6d45SSherry Moore size_t bytes_left, pos, rsz; 1838753a6d45SSherry Moore char *tmppath; 1839753a6d45SSherry Moore const char *format; 1840753a6d45SSherry Moore 1841753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 1842753a6d45SSherry Moore &tmppath) != 0) 1843753a6d45SSherry Moore return (EZFS_NODEVICE); 1844753a6d45SSherry Moore 1845753a6d45SSherry Moore pos = *bytes_written; 1846753a6d45SSherry Moore bytes_left = physpath_size - pos; 1847753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s"; 1848753a6d45SSherry Moore 1849753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 1850753a6d45SSherry Moore *bytes_written += rsz; 1851753a6d45SSherry Moore 1852753a6d45SSherry Moore if (rsz >= bytes_left) { 1853753a6d45SSherry Moore /* if physpath was not copied properly, clear it */ 1854753a6d45SSherry Moore if (bytes_left != 0) { 1855753a6d45SSherry Moore physpath[pos] = 0; 1856753a6d45SSherry Moore } 1857753a6d45SSherry Moore return (EZFS_NOSPC); 1858753a6d45SSherry Moore } 1859753a6d45SSherry Moore return (0); 1860753a6d45SSherry Moore } 1861753a6d45SSherry Moore 186221ecdf64SLin Ling static int 186321ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 186421ecdf64SLin Ling size_t *rsz, boolean_t is_spare) 186521ecdf64SLin Ling { 186621ecdf64SLin Ling char *type; 186721ecdf64SLin Ling int ret; 186821ecdf64SLin Ling 186921ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 187021ecdf64SLin Ling return (EZFS_INVALCONFIG); 187121ecdf64SLin Ling 187221ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) { 187321ecdf64SLin Ling /* 187421ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 187521ecdf64SLin Ling * For a spare vdev, we only want to boot from the active 187621ecdf64SLin Ling * spare device. 187721ecdf64SLin Ling */ 187821ecdf64SLin Ling if (is_spare) { 187921ecdf64SLin Ling uint64_t spare = 0; 188021ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 188121ecdf64SLin Ling &spare); 188221ecdf64SLin Ling if (!spare) 188321ecdf64SLin Ling return (EZFS_INVALCONFIG); 188421ecdf64SLin Ling } 188521ecdf64SLin Ling 188621ecdf64SLin Ling if (vdev_online(nv)) { 188721ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath, 188821ecdf64SLin Ling phypath_size, rsz)) != 0) 188921ecdf64SLin Ling return (ret); 189021ecdf64SLin Ling } 189121ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 189221ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 || 189321ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 189421ecdf64SLin Ling nvlist_t **child; 189521ecdf64SLin Ling uint_t count; 189621ecdf64SLin Ling int i, ret; 189721ecdf64SLin Ling 189821ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv, 189921ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 190021ecdf64SLin Ling return (EZFS_INVALCONFIG); 190121ecdf64SLin Ling 190221ecdf64SLin Ling for (i = 0; i < count; i++) { 190321ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath, 190421ecdf64SLin Ling phypath_size, rsz, is_spare); 190521ecdf64SLin Ling if (ret == EZFS_NOSPC) 190621ecdf64SLin Ling return (ret); 190721ecdf64SLin Ling } 190821ecdf64SLin Ling } 190921ecdf64SLin Ling 191021ecdf64SLin Ling return (EZFS_POOL_INVALARG); 191121ecdf64SLin Ling } 191221ecdf64SLin Ling 1913753a6d45SSherry Moore /* 1914753a6d45SSherry Moore * Get phys_path for a root pool config. 1915753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 1916753a6d45SSherry Moore */ 1917753a6d45SSherry Moore static int 1918753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 1919753a6d45SSherry Moore { 1920753a6d45SSherry Moore size_t rsz; 192119397407SSherry Moore nvlist_t *vdev_root; 192219397407SSherry Moore nvlist_t **child; 192319397407SSherry Moore uint_t count; 1924753a6d45SSherry Moore char *type; 1925753a6d45SSherry Moore 1926753a6d45SSherry Moore rsz = 0; 1927753a6d45SSherry Moore 1928753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1929753a6d45SSherry Moore &vdev_root) != 0) 1930753a6d45SSherry Moore return (EZFS_INVALCONFIG); 1931753a6d45SSherry Moore 1932753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 1933753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 1934753a6d45SSherry Moore &child, &count) != 0) 1935753a6d45SSherry Moore return (EZFS_INVALCONFIG); 193619397407SSherry Moore 193719397407SSherry Moore /* 1938753a6d45SSherry Moore * root pool can not have EFI labeled disks and can only have 1939753a6d45SSherry Moore * a single top-level vdev. 194019397407SSherry Moore */ 1941753a6d45SSherry Moore if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 1942753a6d45SSherry Moore pool_uses_efi(vdev_root)) 1943753a6d45SSherry Moore return (EZFS_POOL_INVALARG); 194419397407SSherry Moore 194521ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 194621ecdf64SLin Ling B_FALSE); 194719397407SSherry Moore 1948753a6d45SSherry Moore /* No online devices */ 1949753a6d45SSherry Moore if (rsz == 0) 1950753a6d45SSherry Moore return (EZFS_NODEVICE); 1951753a6d45SSherry Moore 195219397407SSherry Moore return (0); 195319397407SSherry Moore } 195419397407SSherry Moore 1955753a6d45SSherry Moore /* 1956753a6d45SSherry Moore * Get phys_path for a root pool 1957753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 1958753a6d45SSherry Moore */ 1959753a6d45SSherry Moore int 1960753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 1961753a6d45SSherry Moore { 1962753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath, 1963753a6d45SSherry Moore phypath_size)); 1964753a6d45SSherry Moore } 1965753a6d45SSherry Moore 1966573ca77eSGeorge Wilson /* 1967573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel 1968573ca77eSGeorge Wilson * the disk to use the new unallocated space. 1969573ca77eSGeorge Wilson */ 1970573ca77eSGeorge Wilson static int 1971573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 1972573ca77eSGeorge Wilson { 1973573ca77eSGeorge Wilson char path[MAXPATHLEN]; 1974573ca77eSGeorge Wilson char errbuf[1024]; 1975573ca77eSGeorge Wilson int fd, error; 1976573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int); 1977573ca77eSGeorge Wilson 1978573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 1979573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL) 1980573ca77eSGeorge Wilson return (-1); 1981573ca77eSGeorge Wilson 1982573ca77eSGeorge Wilson (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name); 1983573ca77eSGeorge Wilson 1984573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 1985573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 1986573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name); 1987573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 1988573ca77eSGeorge Wilson } 1989573ca77eSGeorge Wilson 1990573ca77eSGeorge Wilson /* 1991573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device 1992573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply 1993573ca77eSGeorge Wilson * ignore that error and continue on. 1994573ca77eSGeorge Wilson */ 1995573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd); 1996573ca77eSGeorge Wilson (void) close(fd); 1997573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) { 1998573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 1999573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name); 2000573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2001573ca77eSGeorge Wilson } 2002573ca77eSGeorge Wilson return (0); 2003573ca77eSGeorge Wilson } 2004573ca77eSGeorge Wilson 2005fa9e4066Sahrens /* 20063d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 20073d7072f8Seschrock * ZFS_ONLINE_* flags. 2008fa9e4066Sahrens */ 2009fa9e4066Sahrens int 20103d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 20113d7072f8Seschrock vdev_state_t *newstate) 2012fa9e4066Sahrens { 2013fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2014fa9e4066Sahrens char msg[1024]; 201599653d4eSeschrock nvlist_t *tgt; 2016573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog; 201799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2018fa9e4066Sahrens 2019573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) { 2020573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2021573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2022573ca77eSGeorge Wilson } else { 2023573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2024573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2025573ca77eSGeorge Wilson } 2026ea8dc4b6Seschrock 2027fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2028ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2029573ca77eSGeorge Wilson &islog)) == NULL) 203099653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2031fa9e4066Sahrens 203299653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2033fa9e4066Sahrens 2034069f55e2SEric Schrock if (avail_spare) 2035a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2036a43d325bSek 2037573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND || 2038573ca77eSGeorge Wilson zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 2039573ca77eSGeorge Wilson char *pathname = NULL; 2040573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2041573ca77eSGeorge Wilson 2042573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2043573ca77eSGeorge Wilson &wholedisk); 2044573ca77eSGeorge Wilson verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 2045573ca77eSGeorge Wilson &pathname) == 0); 2046573ca77eSGeorge Wilson 2047573ca77eSGeorge Wilson /* 2048573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion. 2049573ca77eSGeorge Wilson */ 2050573ca77eSGeorge Wilson if (l2cache) { 2051573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2052573ca77eSGeorge Wilson "cannot expand cache devices")); 2053573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2054573ca77eSGeorge Wilson } 2055573ca77eSGeorge Wilson 2056573ca77eSGeorge Wilson if (wholedisk) { 2057573ca77eSGeorge Wilson pathname += strlen(DISK_ROOT) + 1; 2058573ca77eSGeorge Wilson (void) zpool_relabel_disk(zhp->zpool_hdl, pathname); 2059573ca77eSGeorge Wilson } 2060573ca77eSGeorge Wilson } 2061573ca77eSGeorge Wilson 20623d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 20633d7072f8Seschrock zc.zc_obj = flags; 2064fa9e4066Sahrens 20651195e687SMark J Musante if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 20661195e687SMark J Musante if (errno == EINVAL) { 20671195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 20681195e687SMark J Musante "from this pool into a new one. Use '%s' " 20691195e687SMark J Musante "instead"), "zpool detach"); 20701195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 20711195e687SMark J Musante } 20723d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 20731195e687SMark J Musante } 20743d7072f8Seschrock 20753d7072f8Seschrock *newstate = zc.zc_cookie; 20763d7072f8Seschrock return (0); 2077fa9e4066Sahrens } 2078fa9e4066Sahrens 2079fa9e4066Sahrens /* 2080fa9e4066Sahrens * Take the specified vdev offline 2081fa9e4066Sahrens */ 2082fa9e4066Sahrens int 20833d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2084fa9e4066Sahrens { 2085fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2086fa9e4066Sahrens char msg[1024]; 208799653d4eSeschrock nvlist_t *tgt; 2088fa94a07fSbrendan boolean_t avail_spare, l2cache; 208999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2090fa9e4066Sahrens 2091ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2092ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2093ea8dc4b6Seschrock 2094fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2095ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2096ee0eb9f2SEric Schrock NULL)) == NULL) 209799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 209899653d4eSeschrock 209999653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2100fa9e4066Sahrens 2101069f55e2SEric Schrock if (avail_spare) 2102a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2103a43d325bSek 21043d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 21053d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 21063d7072f8Seschrock 2107ecd6cf80Smarks if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 21083d7072f8Seschrock return (0); 21093d7072f8Seschrock 21103d7072f8Seschrock switch (errno) { 21113d7072f8Seschrock case EBUSY: 21123d7072f8Seschrock 21133d7072f8Seschrock /* 21143d7072f8Seschrock * There are no other replicas of this device. 21153d7072f8Seschrock */ 21163d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 21173d7072f8Seschrock 2118e6ca193dSGeorge Wilson case EEXIST: 2119e6ca193dSGeorge Wilson /* 2120e6ca193dSGeorge Wilson * The log device has unplayed logs 2121e6ca193dSGeorge Wilson */ 2122e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2123e6ca193dSGeorge Wilson 21243d7072f8Seschrock default: 21253d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 21263d7072f8Seschrock } 21273d7072f8Seschrock } 21283d7072f8Seschrock 21293d7072f8Seschrock /* 21303d7072f8Seschrock * Mark the given vdev faulted. 21313d7072f8Seschrock */ 21323d7072f8Seschrock int 2133069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 21343d7072f8Seschrock { 21353d7072f8Seschrock zfs_cmd_t zc = { 0 }; 21363d7072f8Seschrock char msg[1024]; 21373d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21383d7072f8Seschrock 21393d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 21403d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2141441d80aaSlling 21423d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21433d7072f8Seschrock zc.zc_guid = guid; 21443d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 2145069f55e2SEric Schrock zc.zc_obj = aux; 21463d7072f8Seschrock 21473d7072f8Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2148fa9e4066Sahrens return (0); 2149fa9e4066Sahrens 2150fa9e4066Sahrens switch (errno) { 215199653d4eSeschrock case EBUSY: 2152fa9e4066Sahrens 2153fa9e4066Sahrens /* 2154fa9e4066Sahrens * There are no other replicas of this device. 2155fa9e4066Sahrens */ 215699653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2157fa9e4066Sahrens 215899653d4eSeschrock default: 215999653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2160fa9e4066Sahrens } 21613d7072f8Seschrock 21623d7072f8Seschrock } 21633d7072f8Seschrock 21643d7072f8Seschrock /* 21653d7072f8Seschrock * Mark the given vdev degraded. 21663d7072f8Seschrock */ 21673d7072f8Seschrock int 2168069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 21693d7072f8Seschrock { 21703d7072f8Seschrock zfs_cmd_t zc = { 0 }; 21713d7072f8Seschrock char msg[1024]; 21723d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 21733d7072f8Seschrock 21743d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 21753d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 21763d7072f8Seschrock 21773d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 21783d7072f8Seschrock zc.zc_guid = guid; 21793d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 2180069f55e2SEric Schrock zc.zc_obj = aux; 21813d7072f8Seschrock 21823d7072f8Seschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 21833d7072f8Seschrock return (0); 21843d7072f8Seschrock 21853d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 218699653d4eSeschrock } 218799653d4eSeschrock 218899653d4eSeschrock /* 218999653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 219099653d4eSeschrock * a hot spare. 219199653d4eSeschrock */ 219299653d4eSeschrock static boolean_t 219399653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 219499653d4eSeschrock { 219599653d4eSeschrock nvlist_t **child; 219699653d4eSeschrock uint_t c, children; 219799653d4eSeschrock char *type; 219899653d4eSeschrock 219999653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 220099653d4eSeschrock &children) == 0) { 220199653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 220299653d4eSeschrock &type) == 0); 220399653d4eSeschrock 220499653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 220599653d4eSeschrock children == 2 && child[which] == tgt) 220699653d4eSeschrock return (B_TRUE); 220799653d4eSeschrock 220899653d4eSeschrock for (c = 0; c < children; c++) 220999653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 221099653d4eSeschrock return (B_TRUE); 221199653d4eSeschrock } 221299653d4eSeschrock 221399653d4eSeschrock return (B_FALSE); 2214fa9e4066Sahrens } 2215fa9e4066Sahrens 2216fa9e4066Sahrens /* 2217fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 22188654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2219fa9e4066Sahrens */ 2220fa9e4066Sahrens int 2221fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2222fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2223fa9e4066Sahrens { 2224fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2225fa9e4066Sahrens char msg[1024]; 2226fa9e4066Sahrens int ret; 222799653d4eSeschrock nvlist_t *tgt; 2228ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog; 2229ee0eb9f2SEric Schrock uint64_t val; 22300430f8daSeschrock char *path, *newname; 223199653d4eSeschrock nvlist_t **child; 223299653d4eSeschrock uint_t children; 223399653d4eSeschrock nvlist_t *config_root; 223499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2235b5b76fecSGeorge Wilson boolean_t rootpool = pool_is_bootable(zhp); 2236fa9e4066Sahrens 2237ea8dc4b6Seschrock if (replacing) 2238ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2239ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 2240ea8dc4b6Seschrock else 2241ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2242ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 2243ea8dc4b6Seschrock 2244b5b76fecSGeorge Wilson /* 2245b5b76fecSGeorge Wilson * If this is a root pool, make sure that we're not attaching an 2246b5b76fecSGeorge Wilson * EFI labeled device. 2247b5b76fecSGeorge Wilson */ 2248b5b76fecSGeorge Wilson if (rootpool && pool_uses_efi(nvroot)) { 2249b5b76fecSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2250b5b76fecSGeorge Wilson "EFI labeled devices are not supported on root pools.")); 2251b5b76fecSGeorge Wilson return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 2252b5b76fecSGeorge Wilson } 2253b5b76fecSGeorge Wilson 2254fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2255ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 2256ee0eb9f2SEric Schrock &islog)) == 0) 225799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 225899653d4eSeschrock 2259a43d325bSek if (avail_spare) 226099653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 226199653d4eSeschrock 2262fa94a07fSbrendan if (l2cache) 2263fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2264fa94a07fSbrendan 226599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2266fa9e4066Sahrens zc.zc_cookie = replacing; 2267fa9e4066Sahrens 226899653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 226999653d4eSeschrock &child, &children) != 0 || children != 1) { 227099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 227199653d4eSeschrock "new device must be a single disk")); 227299653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 227399653d4eSeschrock } 227499653d4eSeschrock 227599653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 227699653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 227799653d4eSeschrock 227888ecc943SGeorge Wilson if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 22790430f8daSeschrock return (-1); 22800430f8daSeschrock 228199653d4eSeschrock /* 228299653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 228399653d4eSeschrock * replace it with another hot spare. 228499653d4eSeschrock */ 228599653d4eSeschrock if (replacing && 228699653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2287ee0eb9f2SEric Schrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2288ee0eb9f2SEric Schrock NULL) == NULL || !avail_spare) && 2289ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) { 229099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 229199653d4eSeschrock "can only be replaced by another hot spare")); 22920430f8daSeschrock free(newname); 229399653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 229499653d4eSeschrock } 229599653d4eSeschrock 229699653d4eSeschrock /* 229799653d4eSeschrock * If we are attempting to replace a spare, it canot be applied to an 229899653d4eSeschrock * already spared device. 229999653d4eSeschrock */ 230099653d4eSeschrock if (replacing && 230199653d4eSeschrock nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && 2302ee0eb9f2SEric Schrock zpool_find_vdev(zhp, newname, &avail_spare, 2303ee0eb9f2SEric Schrock &l2cache, NULL) != NULL && avail_spare && 2304ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 0)) { 230599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 230699653d4eSeschrock "device has already been replaced with a spare")); 23070430f8daSeschrock free(newname); 230899653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 230999653d4eSeschrock } 231099653d4eSeschrock 23110430f8daSeschrock free(newname); 23120430f8daSeschrock 2313990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 231499653d4eSeschrock return (-1); 2315fa9e4066Sahrens 2316ecd6cf80Smarks ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2317fa9e4066Sahrens 2318e9dbad6fSeschrock zcmd_free_nvlists(&zc); 2319fa9e4066Sahrens 2320b5b76fecSGeorge Wilson if (ret == 0) { 2321b5b76fecSGeorge Wilson if (rootpool) { 2322b5b76fecSGeorge Wilson /* 2323b5b76fecSGeorge Wilson * XXX - This should be removed once we can 2324b5b76fecSGeorge Wilson * automatically install the bootblocks on the 2325b5b76fecSGeorge Wilson * newly attached disk. 2326b5b76fecSGeorge Wilson */ 2327b5b76fecSGeorge Wilson (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please " 2328b5b76fecSGeorge Wilson "be sure to invoke %s to make '%s' bootable.\n"), 2329b5b76fecSGeorge Wilson BOOTCMD, new_disk); 233021ecdf64SLin Ling 233121ecdf64SLin Ling /* 233221ecdf64SLin Ling * XXX need a better way to prevent user from 233321ecdf64SLin Ling * booting up a half-baked vdev. 233421ecdf64SLin Ling */ 233521ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 233621ecdf64SLin Ling "sure to wait until resilver is done " 233721ecdf64SLin Ling "before rebooting.\n")); 2338b5b76fecSGeorge Wilson } 2339fa9e4066Sahrens return (0); 2340b5b76fecSGeorge Wilson } 2341fa9e4066Sahrens 2342fa9e4066Sahrens switch (errno) { 2343ea8dc4b6Seschrock case ENOTSUP: 2344fa9e4066Sahrens /* 2345fa9e4066Sahrens * Can't attach to or replace this type of vdev. 2346fa9e4066Sahrens */ 23478654d025Sperrin if (replacing) { 2348ee0eb9f2SEric Schrock if (islog) 23498654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23508654d025Sperrin "cannot replace a log with a spare")); 23518654d025Sperrin else 23528654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23538654d025Sperrin "cannot replace a replacing device")); 23548654d025Sperrin } else { 235599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 235699653d4eSeschrock "can only attach to mirrors and top-level " 235799653d4eSeschrock "disks")); 23588654d025Sperrin } 235999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2360fa9e4066Sahrens break; 2361fa9e4066Sahrens 2362ea8dc4b6Seschrock case EINVAL: 2363fa9e4066Sahrens /* 2364fa9e4066Sahrens * The new device must be a single disk. 2365fa9e4066Sahrens */ 236699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 236799653d4eSeschrock "new device must be a single disk")); 236899653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2369fa9e4066Sahrens break; 2370fa9e4066Sahrens 2371ea8dc4b6Seschrock case EBUSY: 237299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 237399653d4eSeschrock new_disk); 237499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2375fa9e4066Sahrens break; 2376fa9e4066Sahrens 2377ea8dc4b6Seschrock case EOVERFLOW: 2378fa9e4066Sahrens /* 2379fa9e4066Sahrens * The new device is too small. 2380fa9e4066Sahrens */ 238199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 238299653d4eSeschrock "device is too small")); 238399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2384fa9e4066Sahrens break; 2385fa9e4066Sahrens 2386ea8dc4b6Seschrock case EDOM: 2387fa9e4066Sahrens /* 2388fa9e4066Sahrens * The new device has a different alignment requirement. 2389fa9e4066Sahrens */ 239099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 239199653d4eSeschrock "devices have different sector alignment")); 239299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2393fa9e4066Sahrens break; 2394fa9e4066Sahrens 2395ea8dc4b6Seschrock case ENAMETOOLONG: 2396fa9e4066Sahrens /* 2397fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 2398fa9e4066Sahrens */ 239999653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2400fa9e4066Sahrens break; 2401fa9e4066Sahrens 2402ea8dc4b6Seschrock default: 240399653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2404fa9e4066Sahrens } 2405fa9e4066Sahrens 240699653d4eSeschrock return (-1); 2407fa9e4066Sahrens } 2408fa9e4066Sahrens 2409fa9e4066Sahrens /* 2410fa9e4066Sahrens * Detach the specified device. 2411fa9e4066Sahrens */ 2412fa9e4066Sahrens int 2413fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2414fa9e4066Sahrens { 2415fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2416fa9e4066Sahrens char msg[1024]; 241799653d4eSeschrock nvlist_t *tgt; 2418fa94a07fSbrendan boolean_t avail_spare, l2cache; 241999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2420fa9e4066Sahrens 2421ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2422ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2423ea8dc4b6Seschrock 2424fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2425ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2426ee0eb9f2SEric Schrock NULL)) == 0) 242799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2428fa9e4066Sahrens 2429a43d325bSek if (avail_spare) 243099653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 243199653d4eSeschrock 2432fa94a07fSbrendan if (l2cache) 2433fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2434fa94a07fSbrendan 243599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 243699653d4eSeschrock 2437ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2438fa9e4066Sahrens return (0); 2439fa9e4066Sahrens 2440fa9e4066Sahrens switch (errno) { 2441fa9e4066Sahrens 2442ea8dc4b6Seschrock case ENOTSUP: 2443fa9e4066Sahrens /* 2444fa9e4066Sahrens * Can't detach from this type of vdev. 2445fa9e4066Sahrens */ 244699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 244799653d4eSeschrock "applicable to mirror and replacing vdevs")); 244899653d4eSeschrock (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); 2449fa9e4066Sahrens break; 2450fa9e4066Sahrens 2451ea8dc4b6Seschrock case EBUSY: 2452fa9e4066Sahrens /* 2453fa9e4066Sahrens * There are no other replicas of this device. 2454fa9e4066Sahrens */ 245599653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2456fa9e4066Sahrens break; 2457fa9e4066Sahrens 2458ea8dc4b6Seschrock default: 245999653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2460ea8dc4b6Seschrock } 2461ea8dc4b6Seschrock 246299653d4eSeschrock return (-1); 246399653d4eSeschrock } 246499653d4eSeschrock 24651195e687SMark J Musante /* 24661195e687SMark J Musante * Find a mirror vdev in the source nvlist. 24671195e687SMark J Musante * 24681195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors 24691195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the 24701195e687SMark J Musante * user specified on the command line. We loop over the mchild array to 24711195e687SMark J Musante * see if any entry in the schild array matches. 24721195e687SMark J Musante * 24731195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return 24741195e687SMark J Musante * the index of that entry. Otherwise we return -1. 24751195e687SMark J Musante */ 24761195e687SMark J Musante static int 24771195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 24781195e687SMark J Musante nvlist_t **schild, uint_t schildren) 24791195e687SMark J Musante { 24801195e687SMark J Musante uint_t mc; 24811195e687SMark J Musante 24821195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) { 24831195e687SMark J Musante uint_t sc; 24841195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 24851195e687SMark J Musante mchild[mc], B_FALSE); 24861195e687SMark J Musante 24871195e687SMark J Musante for (sc = 0; sc < schildren; sc++) { 24881195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 24891195e687SMark J Musante schild[sc], B_FALSE); 24901195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0); 24911195e687SMark J Musante 24921195e687SMark J Musante free(spath); 24931195e687SMark J Musante if (result) { 24941195e687SMark J Musante free(mpath); 24951195e687SMark J Musante return (mc); 24961195e687SMark J Musante } 24971195e687SMark J Musante } 24981195e687SMark J Musante 24991195e687SMark J Musante free(mpath); 25001195e687SMark J Musante } 25011195e687SMark J Musante 25021195e687SMark J Musante return (-1); 25031195e687SMark J Musante } 25041195e687SMark J Musante 25051195e687SMark J Musante /* 25061195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist 25071195e687SMark J Musante * is generated and it is the responsibility of the caller to free it. 25081195e687SMark J Musante */ 25091195e687SMark J Musante int 25101195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 25111195e687SMark J Musante nvlist_t *props, splitflags_t flags) 25121195e687SMark J Musante { 25131195e687SMark J Musante zfs_cmd_t zc = { 0 }; 25141195e687SMark J Musante char msg[1024]; 25151195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 25161195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL; 25171195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 25181195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl; 25191195e687SMark J Musante uint64_t vers; 25201195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE; 25211195e687SMark J Musante int retval = 0; 25221195e687SMark J Musante 25231195e687SMark J Musante (void) snprintf(msg, sizeof (msg), 25241195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 25251195e687SMark J Musante 25261195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname)) 25271195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 25281195e687SMark J Musante 25291195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) { 25301195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to " 25311195e687SMark J Musante "retrieve pool configuration\n")); 25321195e687SMark J Musante return (-1); 25331195e687SMark J Musante } 25341195e687SMark J Musante 25351195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 25361195e687SMark J Musante == 0); 25371195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 25381195e687SMark J Musante 25391195e687SMark J Musante if (props) { 25401195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 25411195e687SMark J Musante props, vers, B_TRUE, msg)) == NULL) 25421195e687SMark J Musante return (-1); 25431195e687SMark J Musante } 25441195e687SMark J Musante 25451195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 25461195e687SMark J Musante &children) != 0) { 25471195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25481195e687SMark J Musante "Source pool is missing vdev tree")); 25491195e687SMark J Musante if (zc_props) 25501195e687SMark J Musante nvlist_free(zc_props); 25511195e687SMark J Musante return (-1); 25521195e687SMark J Musante } 25531195e687SMark J Musante 25541195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 25551195e687SMark J Musante vcount = 0; 25561195e687SMark J Musante 25571195e687SMark J Musante if (*newroot == NULL || 25581195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 25591195e687SMark J Musante &newchild, &newchildren) != 0) 25601195e687SMark J Musante newchildren = 0; 25611195e687SMark J Musante 25621195e687SMark J Musante for (c = 0; c < children; c++) { 25631195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE; 25641195e687SMark J Musante char *type; 25651195e687SMark J Musante nvlist_t **mchild, *vdev; 25661195e687SMark J Musante uint_t mchildren; 25671195e687SMark J Musante int entry; 25681195e687SMark J Musante 25691195e687SMark J Musante /* 25701195e687SMark J Musante * Unlike cache & spares, slogs are stored in the 25711195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 25721195e687SMark J Musante */ 25731195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 25741195e687SMark J Musante &is_log); 25751195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 25761195e687SMark J Musante &is_hole); 25771195e687SMark J Musante if (is_log || is_hole) { 25781195e687SMark J Musante /* 25791195e687SMark J Musante * Create a hole vdev and put it in the config. 25801195e687SMark J Musante */ 25811195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 25821195e687SMark J Musante goto out; 25831195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 25841195e687SMark J Musante VDEV_TYPE_HOLE) != 0) 25851195e687SMark J Musante goto out; 25861195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 25871195e687SMark J Musante 1) != 0) 25881195e687SMark J Musante goto out; 25891195e687SMark J Musante if (lastlog == 0) 25901195e687SMark J Musante lastlog = vcount; 25911195e687SMark J Musante varray[vcount++] = vdev; 25921195e687SMark J Musante continue; 25931195e687SMark J Musante } 25941195e687SMark J Musante lastlog = 0; 25951195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 25961195e687SMark J Musante == 0); 25971195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 25981195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25991195e687SMark J Musante "Source pool must be composed only of mirrors\n")); 26001195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 26011195e687SMark J Musante goto out; 26021195e687SMark J Musante } 26031195e687SMark J Musante 26041195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c], 26051195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 26061195e687SMark J Musante 26071195e687SMark J Musante /* find or add an entry for this top-level vdev */ 26081195e687SMark J Musante if (newchildren > 0 && 26091195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren, 26101195e687SMark J Musante newchild, newchildren)) >= 0) { 26111195e687SMark J Musante /* We found a disk that the user specified. */ 26121195e687SMark J Musante vdev = mchild[entry]; 26131195e687SMark J Musante ++found; 26141195e687SMark J Musante } else { 26151195e687SMark J Musante /* User didn't specify a disk for this vdev. */ 26161195e687SMark J Musante vdev = mchild[mchildren - 1]; 26171195e687SMark J Musante } 26181195e687SMark J Musante 26191195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 26201195e687SMark J Musante goto out; 26211195e687SMark J Musante } 26221195e687SMark J Musante 26231195e687SMark J Musante /* did we find every disk the user specified? */ 26241195e687SMark J Musante if (found != newchildren) { 26251195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 26261195e687SMark J Musante "include at most one disk from each mirror")); 26271195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 26281195e687SMark J Musante goto out; 26291195e687SMark J Musante } 26301195e687SMark J Musante 26311195e687SMark J Musante /* Prepare the nvlist for populating. */ 26321195e687SMark J Musante if (*newroot == NULL) { 26331195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 26341195e687SMark J Musante goto out; 26351195e687SMark J Musante freelist = B_TRUE; 26361195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 26371195e687SMark J Musante VDEV_TYPE_ROOT) != 0) 26381195e687SMark J Musante goto out; 26391195e687SMark J Musante } else { 26401195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 26411195e687SMark J Musante } 26421195e687SMark J Musante 26431195e687SMark J Musante /* Add all the children we found */ 26441195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 26451195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0) 26461195e687SMark J Musante goto out; 26471195e687SMark J Musante 26481195e687SMark J Musante /* 26491195e687SMark J Musante * If we're just doing a dry run, exit now with success. 26501195e687SMark J Musante */ 26511195e687SMark J Musante if (flags.dryrun) { 26521195e687SMark J Musante memory_err = B_FALSE; 26531195e687SMark J Musante freelist = B_FALSE; 26541195e687SMark J Musante goto out; 26551195e687SMark J Musante } 26561195e687SMark J Musante 26571195e687SMark J Musante /* now build up the config list & call the ioctl */ 26581195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 26591195e687SMark J Musante goto out; 26601195e687SMark J Musante 26611195e687SMark J Musante if (nvlist_add_nvlist(newconfig, 26621195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 26631195e687SMark J Musante nvlist_add_string(newconfig, 26641195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 26651195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 26661195e687SMark J Musante goto out; 26671195e687SMark J Musante 26681195e687SMark J Musante /* 26691195e687SMark J Musante * The new pool is automatically part of the namespace unless we 26701195e687SMark J Musante * explicitly export it. 26711195e687SMark J Musante */ 26721195e687SMark J Musante if (!flags.import) 26731195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 26741195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 26751195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 26761195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 26771195e687SMark J Musante goto out; 26781195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 26791195e687SMark J Musante goto out; 26801195e687SMark J Musante 26811195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 26821195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg); 26831195e687SMark J Musante goto out; 26841195e687SMark J Musante } 26851195e687SMark J Musante 26861195e687SMark J Musante freelist = B_FALSE; 26871195e687SMark J Musante memory_err = B_FALSE; 26881195e687SMark J Musante 26891195e687SMark J Musante out: 26901195e687SMark J Musante if (varray != NULL) { 26911195e687SMark J Musante int v; 26921195e687SMark J Musante 26931195e687SMark J Musante for (v = 0; v < vcount; v++) 26941195e687SMark J Musante nvlist_free(varray[v]); 26951195e687SMark J Musante free(varray); 26961195e687SMark J Musante } 26971195e687SMark J Musante zcmd_free_nvlists(&zc); 26981195e687SMark J Musante if (zc_props) 26991195e687SMark J Musante nvlist_free(zc_props); 27001195e687SMark J Musante if (newconfig) 27011195e687SMark J Musante nvlist_free(newconfig); 27021195e687SMark J Musante if (freelist) { 27031195e687SMark J Musante nvlist_free(*newroot); 27041195e687SMark J Musante *newroot = NULL; 27051195e687SMark J Musante } 27061195e687SMark J Musante 27071195e687SMark J Musante if (retval != 0) 27081195e687SMark J Musante return (retval); 27091195e687SMark J Musante 27101195e687SMark J Musante if (memory_err) 27111195e687SMark J Musante return (no_memory(hdl)); 27121195e687SMark J Musante 27131195e687SMark J Musante return (0); 27141195e687SMark J Musante } 27151195e687SMark J Musante 271699653d4eSeschrock /* 2717fa94a07fSbrendan * Remove the given device. Currently, this is supported only for hot spares 2718fa94a07fSbrendan * and level 2 cache devices. 271999653d4eSeschrock */ 272099653d4eSeschrock int 272199653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 272299653d4eSeschrock { 272399653d4eSeschrock zfs_cmd_t zc = { 0 }; 272499653d4eSeschrock char msg[1024]; 272599653d4eSeschrock nvlist_t *tgt; 272688ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog; 272799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 272888ecc943SGeorge Wilson uint64_t version; 272999653d4eSeschrock 273099653d4eSeschrock (void) snprintf(msg, sizeof (msg), 273199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 273299653d4eSeschrock 273399653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2734ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 273588ecc943SGeorge Wilson &islog)) == 0) 273699653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 273788ecc943SGeorge Wilson /* 273888ecc943SGeorge Wilson * XXX - this should just go away. 273988ecc943SGeorge Wilson */ 274088ecc943SGeorge Wilson if (!avail_spare && !l2cache && !islog) { 274199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 274288ecc943SGeorge Wilson "only inactive hot spares, cache, top-level, " 274388ecc943SGeorge Wilson "or log devices can be removed")); 274499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 274599653d4eSeschrock } 274699653d4eSeschrock 274788ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 274888ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) { 274988ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 275088ecc943SGeorge Wilson "pool must be upgrade to support log removal")); 275188ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg)); 275288ecc943SGeorge Wilson } 275388ecc943SGeorge Wilson 275499653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 275599653d4eSeschrock 2756ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 275799653d4eSeschrock return (0); 275899653d4eSeschrock 275999653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2760ea8dc4b6Seschrock } 2761ea8dc4b6Seschrock 2762ea8dc4b6Seschrock /* 2763ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 2764ea8dc4b6Seschrock */ 2765ea8dc4b6Seschrock int 2766468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 2767ea8dc4b6Seschrock { 2768ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 2769ea8dc4b6Seschrock char msg[1024]; 277099653d4eSeschrock nvlist_t *tgt; 2771468c413aSTim Haley zpool_rewind_policy_t policy; 2772fa94a07fSbrendan boolean_t avail_spare, l2cache; 277399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2774468c413aSTim Haley nvlist_t *nvi = NULL; 2775ea8dc4b6Seschrock 2776ea8dc4b6Seschrock if (path) 2777ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2778ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2779e9dbad6fSeschrock path); 2780ea8dc4b6Seschrock else 2781ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2782ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2783ea8dc4b6Seschrock zhp->zpool_name); 2784ea8dc4b6Seschrock 2785ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 278699653d4eSeschrock if (path) { 2787fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 2788ee0eb9f2SEric Schrock &l2cache, NULL)) == 0) 278999653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2790ea8dc4b6Seschrock 2791fa94a07fSbrendan /* 2792fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 2793fa94a07fSbrendan * error clearing for l2cache devices. 2794fa94a07fSbrendan */ 2795a43d325bSek if (avail_spare) 279699653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2797ea8dc4b6Seschrock 279899653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 279999653d4eSeschrock &zc.zc_guid) == 0); 2800fa9e4066Sahrens } 2801fa9e4066Sahrens 2802468c413aSTim Haley zpool_get_rewind_policy(rewindnvl, &policy); 2803468c413aSTim Haley zc.zc_cookie = policy.zrp_request; 2804468c413aSTim Haley 2805468c413aSTim Haley if (zcmd_alloc_dst_nvlist(hdl, &zc, 8192) != 0) 2806468c413aSTim Haley return (-1); 2807468c413aSTim Haley 2808468c413aSTim Haley if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, rewindnvl) != 0) 2809468c413aSTim Haley return (-1); 2810468c413aSTim Haley 2811468c413aSTim Haley if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0 || 2812468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) && 2813468c413aSTim Haley errno != EPERM && errno != EACCES)) { 2814468c413aSTim Haley if (policy.zrp_request & 2815468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 2816468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 2817468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name, 2818468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 2819468c413aSTim Haley nvi); 2820468c413aSTim Haley nvlist_free(nvi); 2821468c413aSTim Haley } 2822468c413aSTim Haley zcmd_free_nvlists(&zc); 282399653d4eSeschrock return (0); 2824468c413aSTim Haley } 282599653d4eSeschrock 2826468c413aSTim Haley zcmd_free_nvlists(&zc); 282799653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2828fa9e4066Sahrens } 2829fa9e4066Sahrens 28303d7072f8Seschrock /* 28313d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 28323d7072f8Seschrock */ 28333d7072f8Seschrock int 28343d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 28353d7072f8Seschrock { 28363d7072f8Seschrock zfs_cmd_t zc = { 0 }; 28373d7072f8Seschrock char msg[1024]; 28383d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 28393d7072f8Seschrock 28403d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 28413d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 28423d7072f8Seschrock guid); 28433d7072f8Seschrock 28443d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 28453d7072f8Seschrock zc.zc_guid = guid; 28463d7072f8Seschrock 28473d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 28483d7072f8Seschrock return (0); 28493d7072f8Seschrock 28503d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 28513d7072f8Seschrock } 28523d7072f8Seschrock 2853c67d9675Seschrock /* 2854c67d9675Seschrock * Convert from a devid string to a path. 2855c67d9675Seschrock */ 2856c67d9675Seschrock static char * 2857c67d9675Seschrock devid_to_path(char *devid_str) 2858c67d9675Seschrock { 2859c67d9675Seschrock ddi_devid_t devid; 2860c67d9675Seschrock char *minor; 2861c67d9675Seschrock char *path; 2862c67d9675Seschrock devid_nmlist_t *list = NULL; 2863c67d9675Seschrock int ret; 2864c67d9675Seschrock 2865c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 2866c67d9675Seschrock return (NULL); 2867c67d9675Seschrock 2868c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 2869c67d9675Seschrock 2870c67d9675Seschrock devid_str_free(minor); 2871c67d9675Seschrock devid_free(devid); 2872c67d9675Seschrock 2873c67d9675Seschrock if (ret != 0) 2874c67d9675Seschrock return (NULL); 2875c67d9675Seschrock 287699653d4eSeschrock if ((path = strdup(list[0].devname)) == NULL) 287799653d4eSeschrock return (NULL); 287899653d4eSeschrock 2879c67d9675Seschrock devid_free_nmlist(list); 2880c67d9675Seschrock 2881c67d9675Seschrock return (path); 2882c67d9675Seschrock } 2883c67d9675Seschrock 2884c67d9675Seschrock /* 2885c67d9675Seschrock * Convert from a path to a devid string. 2886c67d9675Seschrock */ 2887c67d9675Seschrock static char * 2888c67d9675Seschrock path_to_devid(const char *path) 2889c67d9675Seschrock { 2890c67d9675Seschrock int fd; 2891c67d9675Seschrock ddi_devid_t devid; 2892c67d9675Seschrock char *minor, *ret; 2893c67d9675Seschrock 2894c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 2895c67d9675Seschrock return (NULL); 2896c67d9675Seschrock 2897c67d9675Seschrock minor = NULL; 2898c67d9675Seschrock ret = NULL; 2899c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 2900c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 2901c67d9675Seschrock ret = devid_str_encode(devid, minor); 2902c67d9675Seschrock if (minor != NULL) 2903c67d9675Seschrock devid_str_free(minor); 2904c67d9675Seschrock devid_free(devid); 2905c67d9675Seschrock } 2906c67d9675Seschrock (void) close(fd); 2907c67d9675Seschrock 2908c67d9675Seschrock return (ret); 2909c67d9675Seschrock } 2910c67d9675Seschrock 2911c67d9675Seschrock /* 2912c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 2913c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 2914c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 2915c67d9675Seschrock */ 2916c67d9675Seschrock static void 2917c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 2918c67d9675Seschrock { 2919c67d9675Seschrock zfs_cmd_t zc = { 0 }; 2920c67d9675Seschrock 2921c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2922e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 2923c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2924ea8dc4b6Seschrock &zc.zc_guid) == 0); 2925c67d9675Seschrock 292699653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 2927c67d9675Seschrock } 2928c67d9675Seschrock 2929c67d9675Seschrock /* 2930c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 2931c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 2932c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 2933c67d9675Seschrock * trailing 's0' slice name. 2934c67d9675Seschrock * 2935c67d9675Seschrock * This routine is also responsible for identifying when disks have been 2936c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 2937c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 2938c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 2939c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 2940c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 2941c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 2942c67d9675Seschrock * of these checks. 2943c67d9675Seschrock */ 2944c67d9675Seschrock char * 294588ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 294688ecc943SGeorge Wilson boolean_t verbose) 2947c67d9675Seschrock { 2948c67d9675Seschrock char *path, *devid; 2949ea8dc4b6Seschrock uint64_t value; 2950ea8dc4b6Seschrock char buf[64]; 29513d7072f8Seschrock vdev_stat_t *vs; 29523d7072f8Seschrock uint_t vsc; 2953c67d9675Seschrock 2954ea8dc4b6Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 2955ea8dc4b6Seschrock &value) == 0) { 2956ea8dc4b6Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2957ea8dc4b6Seschrock &value) == 0); 29585ad82045Snd (void) snprintf(buf, sizeof (buf), "%llu", 29595ad82045Snd (u_longlong_t)value); 2960ea8dc4b6Seschrock path = buf; 2961ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 2962c67d9675Seschrock 29633d7072f8Seschrock /* 29643d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 29653d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 29663d7072f8Seschrock * open a misbehaving device, which can have undesirable 29673d7072f8Seschrock * effects. 29683d7072f8Seschrock */ 2969*3f9d6ad7SLin Ling if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 29703d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 29713d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 29723d7072f8Seschrock zhp != NULL && 2973c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 2974c67d9675Seschrock /* 2975c67d9675Seschrock * Determine if the current path is correct. 2976c67d9675Seschrock */ 2977c67d9675Seschrock char *newdevid = path_to_devid(path); 2978c67d9675Seschrock 2979c67d9675Seschrock if (newdevid == NULL || 2980c67d9675Seschrock strcmp(devid, newdevid) != 0) { 2981c67d9675Seschrock char *newpath; 2982c67d9675Seschrock 2983c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 2984c67d9675Seschrock /* 2985c67d9675Seschrock * Update the path appropriately. 2986c67d9675Seschrock */ 2987c67d9675Seschrock set_path(zhp, nv, newpath); 298899653d4eSeschrock if (nvlist_add_string(nv, 298999653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 299099653d4eSeschrock verify(nvlist_lookup_string(nv, 299199653d4eSeschrock ZPOOL_CONFIG_PATH, 299299653d4eSeschrock &path) == 0); 2993c67d9675Seschrock free(newpath); 2994c67d9675Seschrock } 2995c67d9675Seschrock } 2996c67d9675Seschrock 299799653d4eSeschrock if (newdevid) 299899653d4eSeschrock devid_str_free(newdevid); 2999c67d9675Seschrock } 3000c67d9675Seschrock 3001c67d9675Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 3002c67d9675Seschrock path += 9; 3003c67d9675Seschrock 3004c67d9675Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3005ea8dc4b6Seschrock &value) == 0 && value) { 300699653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 300799653d4eSeschrock if (tmp == NULL) 300899653d4eSeschrock return (NULL); 3009c67d9675Seschrock tmp[strlen(path) - 2] = '\0'; 3010c67d9675Seschrock return (tmp); 3011c67d9675Seschrock } 3012c67d9675Seschrock } else { 3013c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 301499653d4eSeschrock 301599653d4eSeschrock /* 301699653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 301799653d4eSeschrock */ 301899653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 301999653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 302099653d4eSeschrock &value) == 0); 302199653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 30225ad82045Snd (u_longlong_t)value); 302399653d4eSeschrock path = buf; 302499653d4eSeschrock } 302588ecc943SGeorge Wilson 302688ecc943SGeorge Wilson /* 302788ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id> 302888ecc943SGeorge Wilson * naming convention. 302988ecc943SGeorge Wilson */ 303088ecc943SGeorge Wilson if (verbose) { 303188ecc943SGeorge Wilson uint64_t id; 303288ecc943SGeorge Wilson 303388ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 303488ecc943SGeorge Wilson &id) == 0); 303588ecc943SGeorge Wilson (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 303688ecc943SGeorge Wilson (u_longlong_t)id); 303788ecc943SGeorge Wilson path = buf; 303888ecc943SGeorge Wilson } 3039c67d9675Seschrock } 3040c67d9675Seschrock 304199653d4eSeschrock return (zfs_strdup(hdl, path)); 3042c67d9675Seschrock } 3043ea8dc4b6Seschrock 3044ea8dc4b6Seschrock static int 3045ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b) 3046ea8dc4b6Seschrock { 3047ea8dc4b6Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 3048ea8dc4b6Seschrock } 3049ea8dc4b6Seschrock 3050ea8dc4b6Seschrock /* 3051ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 3052ea8dc4b6Seschrock * caller. 3053ea8dc4b6Seschrock */ 3054ea8dc4b6Seschrock int 305555434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3056ea8dc4b6Seschrock { 3057ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3058ea8dc4b6Seschrock uint64_t count; 3059e9dbad6fSeschrock zbookmark_t *zb = NULL; 306055434c77Sek int i; 3061ea8dc4b6Seschrock 3062ea8dc4b6Seschrock /* 3063ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 3064ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 3065ea8dc4b6Seschrock * entire list. 3066ea8dc4b6Seschrock */ 3067ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3068ea8dc4b6Seschrock &count) == 0); 306975519f38Sek if (count == 0) 307075519f38Sek return (0); 3071e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 30725ad82045Snd count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 307399653d4eSeschrock return (-1); 3074e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 3075ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3076ea8dc4b6Seschrock for (;;) { 307799653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 307899653d4eSeschrock &zc) != 0) { 3079e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 3080ea8dc4b6Seschrock if (errno == ENOMEM) { 3081bf561db0Svb count = zc.zc_nvlist_dst_size; 3082e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t) 3083bf561db0Svb zfs_alloc(zhp->zpool_hdl, count * 3084bf561db0Svb sizeof (zbookmark_t))) == (uintptr_t)NULL) 308599653d4eSeschrock return (-1); 3086ea8dc4b6Seschrock } else { 3087ea8dc4b6Seschrock return (-1); 3088ea8dc4b6Seschrock } 3089ea8dc4b6Seschrock } else { 3090ea8dc4b6Seschrock break; 3091ea8dc4b6Seschrock } 3092ea8dc4b6Seschrock } 3093ea8dc4b6Seschrock 3094ea8dc4b6Seschrock /* 3095ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 3096ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3097e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3098ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 3099ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 3100ea8dc4b6Seschrock */ 3101e9dbad6fSeschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 3102e9dbad6fSeschrock zc.zc_nvlist_dst_size; 3103e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 3104ea8dc4b6Seschrock 3105ea8dc4b6Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 3106ea8dc4b6Seschrock 310755434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3108ea8dc4b6Seschrock 3109ea8dc4b6Seschrock /* 311055434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3111ea8dc4b6Seschrock */ 3112ea8dc4b6Seschrock for (i = 0; i < count; i++) { 3113ea8dc4b6Seschrock nvlist_t *nv; 3114ea8dc4b6Seschrock 3115c0a81264Sek /* ignoring zb_blkid and zb_level for now */ 3116c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3117c0a81264Sek zb[i-1].zb_object == zb[i].zb_object) 3118ea8dc4b6Seschrock continue; 3119ea8dc4b6Seschrock 312055434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 312155434c77Sek goto nomem; 312255434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 312355434c77Sek zb[i].zb_objset) != 0) { 312455434c77Sek nvlist_free(nv); 312599653d4eSeschrock goto nomem; 3126ea8dc4b6Seschrock } 312755434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 312855434c77Sek zb[i].zb_object) != 0) { 312955434c77Sek nvlist_free(nv); 313055434c77Sek goto nomem; 313155434c77Sek } 313255434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 313355434c77Sek nvlist_free(nv); 313455434c77Sek goto nomem; 313555434c77Sek } 313655434c77Sek nvlist_free(nv); 3137ea8dc4b6Seschrock } 3138ea8dc4b6Seschrock 31393ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 3140ea8dc4b6Seschrock return (0); 314199653d4eSeschrock 314299653d4eSeschrock nomem: 3143e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 314499653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 3145ea8dc4b6Seschrock } 3146eaca9bbdSeschrock 3147eaca9bbdSeschrock /* 3148eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 3149eaca9bbdSeschrock */ 3150eaca9bbdSeschrock int 3151990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3152eaca9bbdSeschrock { 3153eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 315499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3155eaca9bbdSeschrock 3156eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3157990b4856Slling zc.zc_cookie = new_version; 3158990b4856Slling 3159ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3160ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 316199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 316299653d4eSeschrock zhp->zpool_name)); 3163eaca9bbdSeschrock return (0); 3164eaca9bbdSeschrock } 316506eeb2adSek 316606eeb2adSek void 31672a6b87f0Sek zpool_set_history_str(const char *subcommand, int argc, char **argv, 31682a6b87f0Sek char *history_str) 316906eeb2adSek { 317006eeb2adSek int i; 317106eeb2adSek 31722a6b87f0Sek (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 31732a6b87f0Sek for (i = 1; i < argc; i++) { 31742a6b87f0Sek if (strlen(history_str) + 1 + strlen(argv[i]) > 31752a6b87f0Sek HIS_MAX_RECORD_LEN) 31762a6b87f0Sek break; 31772a6b87f0Sek (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 31782a6b87f0Sek (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 31792a6b87f0Sek } 31802a6b87f0Sek } 31812a6b87f0Sek 31822a6b87f0Sek /* 31832a6b87f0Sek * Stage command history for logging. 31842a6b87f0Sek */ 31852a6b87f0Sek int 31862a6b87f0Sek zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 31872a6b87f0Sek { 31882a6b87f0Sek if (history_str == NULL) 31892a6b87f0Sek return (EINVAL); 31902a6b87f0Sek 31912a6b87f0Sek if (strlen(history_str) > HIS_MAX_RECORD_LEN) 31922a6b87f0Sek return (EINVAL); 31932a6b87f0Sek 3194228975ccSek if (hdl->libzfs_log_str != NULL) 3195ecd6cf80Smarks free(hdl->libzfs_log_str); 319606eeb2adSek 31972a6b87f0Sek if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 31982a6b87f0Sek return (no_memory(hdl)); 319906eeb2adSek 32002a6b87f0Sek return (0); 320106eeb2adSek } 320206eeb2adSek 320306eeb2adSek /* 320406eeb2adSek * Perform ioctl to get some command history of a pool. 320506eeb2adSek * 320606eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 320706eeb2adSek * logical offset of the history buffer to start reading from. 320806eeb2adSek * 320906eeb2adSek * Upon return, 'off' is the next logical offset to read from and 321006eeb2adSek * 'len' is the actual amount of bytes read into 'buf'. 321106eeb2adSek */ 321206eeb2adSek static int 321306eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 321406eeb2adSek { 321506eeb2adSek zfs_cmd_t zc = { 0 }; 321606eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl; 321706eeb2adSek 321806eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 321906eeb2adSek 322006eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf; 322106eeb2adSek zc.zc_history_len = *len; 322206eeb2adSek zc.zc_history_offset = *off; 322306eeb2adSek 322406eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 322506eeb2adSek switch (errno) { 322606eeb2adSek case EPERM: 3227ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 3228ece3d9b3Slling dgettext(TEXT_DOMAIN, 322906eeb2adSek "cannot show history for pool '%s'"), 323006eeb2adSek zhp->zpool_name)); 323106eeb2adSek case ENOENT: 3232ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 323306eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool " 323406eeb2adSek "'%s'"), zhp->zpool_name)); 3235d7306b64Sek case ENOTSUP: 3236d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3237d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool " 3238d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name)); 323906eeb2adSek default: 3240ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 324106eeb2adSek dgettext(TEXT_DOMAIN, 324206eeb2adSek "cannot get history for '%s'"), zhp->zpool_name)); 324306eeb2adSek } 324406eeb2adSek } 324506eeb2adSek 324606eeb2adSek *len = zc.zc_history_len; 324706eeb2adSek *off = zc.zc_history_offset; 324806eeb2adSek 324906eeb2adSek return (0); 325006eeb2adSek } 325106eeb2adSek 325206eeb2adSek /* 325306eeb2adSek * Process the buffer of nvlists, unpacking and storing each nvlist record 325406eeb2adSek * into 'records'. 'leftover' is set to the number of bytes that weren't 325506eeb2adSek * processed as there wasn't a complete record. 325606eeb2adSek */ 32578f18d1faSGeorge Wilson int 325806eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 325906eeb2adSek nvlist_t ***records, uint_t *numrecords) 326006eeb2adSek { 326106eeb2adSek uint64_t reclen; 326206eeb2adSek nvlist_t *nv; 326306eeb2adSek int i; 326406eeb2adSek 326506eeb2adSek while (bytes_read > sizeof (reclen)) { 326606eeb2adSek 326706eeb2adSek /* get length of packed record (stored as little endian) */ 326806eeb2adSek for (i = 0, reclen = 0; i < sizeof (reclen); i++) 326906eeb2adSek reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 327006eeb2adSek 327106eeb2adSek if (bytes_read < sizeof (reclen) + reclen) 327206eeb2adSek break; 327306eeb2adSek 327406eeb2adSek /* unpack record */ 327506eeb2adSek if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 327606eeb2adSek return (ENOMEM); 327706eeb2adSek bytes_read -= sizeof (reclen) + reclen; 327806eeb2adSek buf += sizeof (reclen) + reclen; 327906eeb2adSek 328006eeb2adSek /* add record to nvlist array */ 328106eeb2adSek (*numrecords)++; 328206eeb2adSek if (ISP2(*numrecords + 1)) { 328306eeb2adSek *records = realloc(*records, 328406eeb2adSek *numrecords * 2 * sizeof (nvlist_t *)); 328506eeb2adSek } 328606eeb2adSek (*records)[*numrecords - 1] = nv; 328706eeb2adSek } 328806eeb2adSek 328906eeb2adSek *leftover = bytes_read; 329006eeb2adSek return (0); 329106eeb2adSek } 329206eeb2adSek 329306eeb2adSek #define HIS_BUF_LEN (128*1024) 329406eeb2adSek 329506eeb2adSek /* 329606eeb2adSek * Retrieve the command history of a pool. 329706eeb2adSek */ 329806eeb2adSek int 329906eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 330006eeb2adSek { 330106eeb2adSek char buf[HIS_BUF_LEN]; 330206eeb2adSek uint64_t off = 0; 330306eeb2adSek nvlist_t **records = NULL; 330406eeb2adSek uint_t numrecords = 0; 330506eeb2adSek int err, i; 330606eeb2adSek 330706eeb2adSek do { 330806eeb2adSek uint64_t bytes_read = sizeof (buf); 330906eeb2adSek uint64_t leftover; 331006eeb2adSek 331106eeb2adSek if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 331206eeb2adSek break; 331306eeb2adSek 331406eeb2adSek /* if nothing else was read in, we're at EOF, just return */ 331506eeb2adSek if (!bytes_read) 331606eeb2adSek break; 331706eeb2adSek 331806eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read, 331906eeb2adSek &leftover, &records, &numrecords)) != 0) 332006eeb2adSek break; 332106eeb2adSek off -= leftover; 332206eeb2adSek 332306eeb2adSek /* CONSTCOND */ 332406eeb2adSek } while (1); 332506eeb2adSek 332606eeb2adSek if (!err) { 332706eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 332806eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 332906eeb2adSek records, numrecords) == 0); 333006eeb2adSek } 333106eeb2adSek for (i = 0; i < numrecords; i++) 333206eeb2adSek nvlist_free(records[i]); 333306eeb2adSek free(records); 333406eeb2adSek 333506eeb2adSek return (err); 333606eeb2adSek } 333755434c77Sek 333855434c77Sek void 333955434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 334055434c77Sek char *pathname, size_t len) 334155434c77Sek { 334255434c77Sek zfs_cmd_t zc = { 0 }; 334355434c77Sek boolean_t mounted = B_FALSE; 334455434c77Sek char *mntpnt = NULL; 334555434c77Sek char dsname[MAXNAMELEN]; 334655434c77Sek 334755434c77Sek if (dsobj == 0) { 334855434c77Sek /* special case for the MOS */ 334955434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 335055434c77Sek return; 335155434c77Sek } 335255434c77Sek 335355434c77Sek /* get the dataset's name */ 335455434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 335555434c77Sek zc.zc_obj = dsobj; 335655434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, 335755434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 335855434c77Sek /* just write out a path of two object numbers */ 335955434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 336055434c77Sek dsobj, obj); 336155434c77Sek return; 336255434c77Sek } 336355434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 336455434c77Sek 336555434c77Sek /* find out if the dataset is mounted */ 336655434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 336755434c77Sek 336855434c77Sek /* get the corrupted object's path */ 336955434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 337055434c77Sek zc.zc_obj = obj; 337155434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 337255434c77Sek &zc) == 0) { 337355434c77Sek if (mounted) { 337455434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt, 337555434c77Sek zc.zc_value); 337655434c77Sek } else { 337755434c77Sek (void) snprintf(pathname, len, "%s:%s", 337855434c77Sek dsname, zc.zc_value); 337955434c77Sek } 338055434c77Sek } else { 338155434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 338255434c77Sek } 338355434c77Sek free(mntpnt); 338455434c77Sek } 3385b1b8ab34Slling 338615e6edf1Sgw /* 338715e6edf1Sgw * Read the EFI label from the config, if a label does not exist then 338815e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL 338915e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI 339015e6edf1Sgw * partition. 339115e6edf1Sgw */ 339215e6edf1Sgw static int 339315e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb) 339415e6edf1Sgw { 339515e6edf1Sgw char *path; 339615e6edf1Sgw int fd; 339715e6edf1Sgw char diskname[MAXPATHLEN]; 339815e6edf1Sgw int err = -1; 339915e6edf1Sgw 340015e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 340115e6edf1Sgw return (err); 340215e6edf1Sgw 340315e6edf1Sgw (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 340415e6edf1Sgw strrchr(path, '/')); 340515e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 340615e6edf1Sgw struct dk_gpt *vtoc; 340715e6edf1Sgw 340815e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 340915e6edf1Sgw if (sb != NULL) 341015e6edf1Sgw *sb = vtoc->efi_parts[0].p_start; 341115e6edf1Sgw efi_free(vtoc); 341215e6edf1Sgw } 341315e6edf1Sgw (void) close(fd); 341415e6edf1Sgw } 341515e6edf1Sgw return (err); 341615e6edf1Sgw } 341715e6edf1Sgw 34188488aeb5Staylor /* 34198488aeb5Staylor * determine where a partition starts on a disk in the current 34208488aeb5Staylor * configuration 34218488aeb5Staylor */ 34228488aeb5Staylor static diskaddr_t 34238488aeb5Staylor find_start_block(nvlist_t *config) 34248488aeb5Staylor { 34258488aeb5Staylor nvlist_t **child; 34268488aeb5Staylor uint_t c, children; 34278488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 34288488aeb5Staylor uint64_t wholedisk; 34298488aeb5Staylor 34308488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 34318488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 34328488aeb5Staylor if (nvlist_lookup_uint64(config, 34338488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 34348488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 34358488aeb5Staylor return (MAXOFFSET_T); 34368488aeb5Staylor } 343715e6edf1Sgw if (read_efi_label(config, &sb) < 0) 343815e6edf1Sgw sb = MAXOFFSET_T; 34398488aeb5Staylor return (sb); 34408488aeb5Staylor } 34418488aeb5Staylor 34428488aeb5Staylor for (c = 0; c < children; c++) { 34438488aeb5Staylor sb = find_start_block(child[c]); 34448488aeb5Staylor if (sb != MAXOFFSET_T) { 34458488aeb5Staylor return (sb); 34468488aeb5Staylor } 34478488aeb5Staylor } 34488488aeb5Staylor return (MAXOFFSET_T); 34498488aeb5Staylor } 34508488aeb5Staylor 34518488aeb5Staylor /* 34528488aeb5Staylor * Label an individual disk. The name provided is the short name, 34538488aeb5Staylor * stripped of any leading /dev path. 34548488aeb5Staylor */ 34558488aeb5Staylor int 34568488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 34578488aeb5Staylor { 34588488aeb5Staylor char path[MAXPATHLEN]; 34598488aeb5Staylor struct dk_gpt *vtoc; 34608488aeb5Staylor int fd; 34618488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 34628488aeb5Staylor uint64_t slice_size; 34638488aeb5Staylor diskaddr_t start_block; 34648488aeb5Staylor char errbuf[1024]; 34658488aeb5Staylor 3466c6ef114fSmmusante /* prepare an error message just in case */ 3467c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 3468c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 3469c6ef114fSmmusante 34708488aeb5Staylor if (zhp) { 34718488aeb5Staylor nvlist_t *nvroot; 34728488aeb5Staylor 3473b5b76fecSGeorge Wilson if (pool_is_bootable(zhp)) { 3474b5b76fecSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3475b5b76fecSGeorge Wilson "EFI labeled devices are not supported on root " 3476b5b76fecSGeorge Wilson "pools.")); 3477b5b76fecSGeorge Wilson return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 3478b5b76fecSGeorge Wilson } 3479b5b76fecSGeorge Wilson 34808488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 34818488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 34828488aeb5Staylor 34838488aeb5Staylor if (zhp->zpool_start_block == 0) 34848488aeb5Staylor start_block = find_start_block(nvroot); 34858488aeb5Staylor else 34868488aeb5Staylor start_block = zhp->zpool_start_block; 34878488aeb5Staylor zhp->zpool_start_block = start_block; 34888488aeb5Staylor } else { 34898488aeb5Staylor /* new pool */ 34908488aeb5Staylor start_block = NEW_START_BLOCK; 34918488aeb5Staylor } 34928488aeb5Staylor 34938488aeb5Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 34948488aeb5Staylor BACKUP_SLICE); 34958488aeb5Staylor 34968488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 34978488aeb5Staylor /* 34988488aeb5Staylor * This shouldn't happen. We've long since verified that this 34998488aeb5Staylor * is a valid device. 35008488aeb5Staylor */ 3501c6ef114fSmmusante zfs_error_aux(hdl, 3502c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 35038488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 35048488aeb5Staylor } 35058488aeb5Staylor 35068488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 35078488aeb5Staylor /* 35088488aeb5Staylor * The only way this can fail is if we run out of memory, or we 35098488aeb5Staylor * were unable to read the disk's capacity 35108488aeb5Staylor */ 35118488aeb5Staylor if (errno == ENOMEM) 35128488aeb5Staylor (void) no_memory(hdl); 35138488aeb5Staylor 35148488aeb5Staylor (void) close(fd); 3515c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3516c6ef114fSmmusante "unable to read disk capacity"), name); 35178488aeb5Staylor 35188488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 35198488aeb5Staylor } 35208488aeb5Staylor 35218488aeb5Staylor slice_size = vtoc->efi_last_u_lba + 1; 35228488aeb5Staylor slice_size -= EFI_MIN_RESV_SIZE; 35238488aeb5Staylor if (start_block == MAXOFFSET_T) 35248488aeb5Staylor start_block = NEW_START_BLOCK; 35258488aeb5Staylor slice_size -= start_block; 35268488aeb5Staylor 35278488aeb5Staylor vtoc->efi_parts[0].p_start = start_block; 35288488aeb5Staylor vtoc->efi_parts[0].p_size = slice_size; 35298488aeb5Staylor 35308488aeb5Staylor /* 35318488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 35328488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 35338488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 35348488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 35358488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 35368488aeb5Staylor * can get, in the absence of V_OTHER. 35378488aeb5Staylor */ 35388488aeb5Staylor vtoc->efi_parts[0].p_tag = V_USR; 35398488aeb5Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 35408488aeb5Staylor 35418488aeb5Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 35428488aeb5Staylor vtoc->efi_parts[8].p_size = resv; 35438488aeb5Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 35448488aeb5Staylor 35458488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 35468488aeb5Staylor /* 35478488aeb5Staylor * Some block drivers (like pcata) may not support EFI 35488488aeb5Staylor * GPT labels. Print out a helpful error message dir- 35498488aeb5Staylor * ecting the user to manually label the disk and give 35508488aeb5Staylor * a specific slice. 35518488aeb5Staylor */ 35528488aeb5Staylor (void) close(fd); 35538488aeb5Staylor efi_free(vtoc); 35548488aeb5Staylor 35558488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3556c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 35578488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 35588488aeb5Staylor } 35598488aeb5Staylor 35608488aeb5Staylor (void) close(fd); 35618488aeb5Staylor efi_free(vtoc); 35628488aeb5Staylor return (0); 35638488aeb5Staylor } 3564e7cbe64fSgw 3565e7cbe64fSgw static boolean_t 3566e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 3567e7cbe64fSgw { 3568e7cbe64fSgw char *type; 3569e7cbe64fSgw nvlist_t **child; 3570e7cbe64fSgw uint_t children, c; 3571e7cbe64fSgw 3572e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 3573e7cbe64fSgw if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 3574e7cbe64fSgw strcmp(type, VDEV_TYPE_FILE) == 0 || 3575e7cbe64fSgw strcmp(type, VDEV_TYPE_LOG) == 0 || 357688ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 || 3577e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) { 3578e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3579e7cbe64fSgw "vdev type '%s' is not supported"), type); 3580e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 3581e7cbe64fSgw return (B_FALSE); 3582e7cbe64fSgw } 3583e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3584e7cbe64fSgw &child, &children) == 0) { 3585e7cbe64fSgw for (c = 0; c < children; c++) { 3586e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 3587e7cbe64fSgw return (B_FALSE); 3588e7cbe64fSgw } 3589e7cbe64fSgw } 3590e7cbe64fSgw return (B_TRUE); 3591e7cbe64fSgw } 3592e7cbe64fSgw 3593e7cbe64fSgw /* 3594e7cbe64fSgw * check if this zvol is allowable for use as a dump device; zero if 3595e7cbe64fSgw * it is, > 0 if it isn't, < 0 if it isn't a zvol 3596e7cbe64fSgw */ 3597e7cbe64fSgw int 3598e7cbe64fSgw zvol_check_dump_config(char *arg) 3599e7cbe64fSgw { 3600e7cbe64fSgw zpool_handle_t *zhp = NULL; 3601e7cbe64fSgw nvlist_t *config, *nvroot; 3602e7cbe64fSgw char *p, *volname; 3603e7cbe64fSgw nvlist_t **top; 3604e7cbe64fSgw uint_t toplevels; 3605e7cbe64fSgw libzfs_handle_t *hdl; 3606e7cbe64fSgw char errbuf[1024]; 3607e7cbe64fSgw char poolname[ZPOOL_MAXNAMELEN]; 3608e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR); 3609e7cbe64fSgw int ret = 1; 3610e7cbe64fSgw 3611e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 3612e7cbe64fSgw return (-1); 3613e7cbe64fSgw } 3614e7cbe64fSgw 3615e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3616e7cbe64fSgw "dump is not supported on device '%s'"), arg); 3617e7cbe64fSgw 3618e7cbe64fSgw if ((hdl = libzfs_init()) == NULL) 3619e7cbe64fSgw return (1); 3620e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE); 3621e7cbe64fSgw 3622e7cbe64fSgw volname = arg + pathlen; 3623e7cbe64fSgw 3624e7cbe64fSgw /* check the configuration of the pool */ 3625e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) { 3626e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3627e7cbe64fSgw "malformed dataset name")); 3628e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 3629e7cbe64fSgw return (1); 3630e7cbe64fSgw } else if (p - volname >= ZFS_MAXNAMELEN) { 3631e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3632e7cbe64fSgw "dataset name is too long")); 3633e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 3634e7cbe64fSgw return (1); 3635e7cbe64fSgw } else { 3636e7cbe64fSgw (void) strncpy(poolname, volname, p - volname); 3637e7cbe64fSgw poolname[p - volname] = '\0'; 3638e7cbe64fSgw } 3639e7cbe64fSgw 3640e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) { 3641e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3642e7cbe64fSgw "could not open pool '%s'"), poolname); 3643e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 3644e7cbe64fSgw goto out; 3645e7cbe64fSgw } 3646e7cbe64fSgw config = zpool_get_config(zhp, NULL); 3647e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 3648e7cbe64fSgw &nvroot) != 0) { 3649e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3650e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname); 3651e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 3652e7cbe64fSgw goto out; 3653e7cbe64fSgw } 3654e7cbe64fSgw 3655e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3656e7cbe64fSgw &top, &toplevels) == 0); 3657e7cbe64fSgw if (toplevels != 1) { 3658e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3659e7cbe64fSgw "'%s' has multiple top level vdevs"), poolname); 3660e7cbe64fSgw (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 3661e7cbe64fSgw goto out; 3662e7cbe64fSgw } 3663e7cbe64fSgw 3664e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 3665e7cbe64fSgw goto out; 3666e7cbe64fSgw } 3667e7cbe64fSgw ret = 0; 3668e7cbe64fSgw 3669e7cbe64fSgw out: 3670e7cbe64fSgw if (zhp) 3671e7cbe64fSgw zpool_close(zhp); 3672e7cbe64fSgw libzfs_fini(hdl); 3673e7cbe64fSgw return (ret); 3674e7cbe64fSgw } 3675