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 /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24e9103aaeSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25*4263d13fSGeorge Wilson * Copyright (c) 2012 by Delphix. All rights reserved. 26fa9e4066Sahrens */ 27fa9e4066Sahrens 28fa9e4066Sahrens #include <ctype.h> 29fa9e4066Sahrens #include <errno.h> 30fa9e4066Sahrens #include <devid.h> 31fa9e4066Sahrens #include <fcntl.h> 32fa9e4066Sahrens #include <libintl.h> 33fa9e4066Sahrens #include <stdio.h> 34fa9e4066Sahrens #include <stdlib.h> 35f3861e1aSahl #include <strings.h> 36fa9e4066Sahrens #include <unistd.h> 378488aeb5Staylor #include <sys/efi_partition.h> 388488aeb5Staylor #include <sys/vtoc.h> 39fa9e4066Sahrens #include <sys/zfs_ioctl.h> 40573ca77eSGeorge Wilson #include <dlfcn.h> 41fa9e4066Sahrens 42fa9e4066Sahrens #include "zfs_namecheck.h" 43b1b8ab34Slling #include "zfs_prop.h" 44fa9e4066Sahrens #include "libzfs_impl.h" 45468c413aSTim Haley #include "zfs_comutil.h" 46fa9e4066Sahrens 4715e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 48990b4856Slling 49573ca77eSGeorge Wilson #define DISK_ROOT "/dev/dsk" 50573ca77eSGeorge Wilson #define RDISK_ROOT "/dev/rdsk" 51573ca77eSGeorge Wilson #define BACKUP_SLICE "s2" 52573ca77eSGeorge Wilson 53f9af39baSGeorge Wilson typedef struct prop_flags { 54f9af39baSGeorge Wilson int create:1; /* Validate property on creation */ 55f9af39baSGeorge Wilson int import:1; /* Validate property on import */ 56f9af39baSGeorge Wilson } prop_flags_t; 57f9af39baSGeorge Wilson 58990b4856Slling /* 59990b4856Slling * ==================================================================== 60990b4856Slling * zpool property functions 61990b4856Slling * ==================================================================== 62990b4856Slling */ 63990b4856Slling 64990b4856Slling static int 65990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 66990b4856Slling { 67990b4856Slling zfs_cmd_t zc = { 0 }; 68990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 69990b4856Slling 70990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 71990b4856Slling 72990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 73990b4856Slling return (-1); 74990b4856Slling 75990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 76990b4856Slling if (errno == ENOMEM) { 77990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 78990b4856Slling zcmd_free_nvlists(&zc); 79990b4856Slling return (-1); 80990b4856Slling } 81990b4856Slling } else { 82990b4856Slling zcmd_free_nvlists(&zc); 83990b4856Slling return (-1); 84990b4856Slling } 85990b4856Slling } 86990b4856Slling 87990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 88990b4856Slling zcmd_free_nvlists(&zc); 89990b4856Slling return (-1); 90990b4856Slling } 91990b4856Slling 92990b4856Slling zcmd_free_nvlists(&zc); 93990b4856Slling 94990b4856Slling return (0); 95990b4856Slling } 96990b4856Slling 97990b4856Slling static int 98990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 99990b4856Slling { 100990b4856Slling nvlist_t *old_props; 101990b4856Slling 102990b4856Slling old_props = zhp->zpool_props; 103990b4856Slling 104990b4856Slling if (zpool_get_all_props(zhp) != 0) 105990b4856Slling return (-1); 106990b4856Slling 107990b4856Slling nvlist_free(old_props); 108990b4856Slling return (0); 109990b4856Slling } 110990b4856Slling 111990b4856Slling static char * 112990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 113990b4856Slling zprop_source_t *src) 114990b4856Slling { 115990b4856Slling nvlist_t *nv, *nvl; 116990b4856Slling uint64_t ival; 117990b4856Slling char *value; 118990b4856Slling zprop_source_t source; 119990b4856Slling 120990b4856Slling nvl = zhp->zpool_props; 121990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 122990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 123990b4856Slling source = ival; 124990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 125990b4856Slling } else { 126990b4856Slling source = ZPROP_SRC_DEFAULT; 127990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 128990b4856Slling value = "-"; 129990b4856Slling } 130990b4856Slling 131990b4856Slling if (src) 132990b4856Slling *src = source; 133990b4856Slling 134990b4856Slling return (value); 135990b4856Slling } 136990b4856Slling 137990b4856Slling uint64_t 138990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 139990b4856Slling { 140990b4856Slling nvlist_t *nv, *nvl; 141990b4856Slling uint64_t value; 142990b4856Slling zprop_source_t source; 143990b4856Slling 144b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 145b87f3af3Sperrin /* 146b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 147b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 148b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 149b87f3af3Sperrin */ 150b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 151b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 152b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 153b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 154b87f3af3Sperrin == 0)) { 155b87f3af3Sperrin return (value); 156b87f3af3Sperrin } 157990b4856Slling return (zpool_prop_default_numeric(prop)); 158b87f3af3Sperrin } 159990b4856Slling 160990b4856Slling nvl = zhp->zpool_props; 161990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 162990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 163990b4856Slling source = value; 164990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 165990b4856Slling } else { 166990b4856Slling source = ZPROP_SRC_DEFAULT; 167990b4856Slling value = zpool_prop_default_numeric(prop); 168990b4856Slling } 169990b4856Slling 170990b4856Slling if (src) 171990b4856Slling *src = source; 172990b4856Slling 173990b4856Slling return (value); 174990b4856Slling } 175990b4856Slling 176990b4856Slling /* 177990b4856Slling * Map VDEV STATE to printed strings. 178990b4856Slling */ 179990b4856Slling char * 180990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 181990b4856Slling { 182990b4856Slling switch (state) { 183990b4856Slling case VDEV_STATE_CLOSED: 184990b4856Slling case VDEV_STATE_OFFLINE: 185990b4856Slling return (gettext("OFFLINE")); 186990b4856Slling case VDEV_STATE_REMOVED: 187990b4856Slling return (gettext("REMOVED")); 188990b4856Slling case VDEV_STATE_CANT_OPEN: 189b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 190990b4856Slling return (gettext("FAULTED")); 1911195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL) 1921195e687SMark J Musante return (gettext("SPLIT")); 193990b4856Slling else 194990b4856Slling return (gettext("UNAVAIL")); 195990b4856Slling case VDEV_STATE_FAULTED: 196990b4856Slling return (gettext("FAULTED")); 197990b4856Slling case VDEV_STATE_DEGRADED: 198990b4856Slling return (gettext("DEGRADED")); 199990b4856Slling case VDEV_STATE_HEALTHY: 200990b4856Slling return (gettext("ONLINE")); 201990b4856Slling } 202990b4856Slling 203990b4856Slling return (gettext("UNKNOWN")); 204990b4856Slling } 205990b4856Slling 206990b4856Slling /* 207990b4856Slling * Get a zpool property value for 'prop' and return the value in 208990b4856Slling * a pre-allocated buffer. 209990b4856Slling */ 210990b4856Slling int 211990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 212990b4856Slling zprop_source_t *srctype) 213990b4856Slling { 214990b4856Slling uint64_t intval; 215990b4856Slling const char *strval; 216990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 217990b4856Slling nvlist_t *nvroot; 218990b4856Slling vdev_stat_t *vs; 219990b4856Slling uint_t vsc; 220990b4856Slling 221990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 222379c004dSEric Schrock switch (prop) { 223379c004dSEric Schrock case ZPOOL_PROP_NAME: 224990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 225379c004dSEric Schrock break; 226379c004dSEric Schrock 227379c004dSEric Schrock case ZPOOL_PROP_HEALTH: 228990b4856Slling (void) strlcpy(buf, "FAULTED", len); 229379c004dSEric Schrock break; 230379c004dSEric Schrock 231379c004dSEric Schrock case ZPOOL_PROP_GUID: 232379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src); 233379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval); 234379c004dSEric Schrock break; 235379c004dSEric Schrock 236379c004dSEric Schrock case ZPOOL_PROP_ALTROOT: 237379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE: 2388704186eSDan McDonald case ZPOOL_PROP_COMMENT: 239379c004dSEric Schrock if (zhp->zpool_props != NULL || 240379c004dSEric Schrock zpool_get_all_props(zhp) == 0) { 241379c004dSEric Schrock (void) strlcpy(buf, 242379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src), 243379c004dSEric Schrock len); 244379c004dSEric Schrock if (srctype != NULL) 245379c004dSEric Schrock *srctype = src; 246379c004dSEric Schrock return (0); 247379c004dSEric Schrock } 248379c004dSEric Schrock /* FALLTHROUGH */ 249379c004dSEric Schrock default: 250990b4856Slling (void) strlcpy(buf, "-", len); 251379c004dSEric Schrock break; 252379c004dSEric Schrock } 253379c004dSEric Schrock 254379c004dSEric Schrock if (srctype != NULL) 255379c004dSEric Schrock *srctype = src; 256990b4856Slling return (0); 257990b4856Slling } 258990b4856Slling 259990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 260990b4856Slling prop != ZPOOL_PROP_NAME) 261990b4856Slling return (-1); 262990b4856Slling 263990b4856Slling switch (zpool_prop_get_type(prop)) { 264990b4856Slling case PROP_TYPE_STRING: 265990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 266990b4856Slling len); 267990b4856Slling break; 268990b4856Slling 269990b4856Slling case PROP_TYPE_NUMBER: 270990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 271990b4856Slling 272990b4856Slling switch (prop) { 273990b4856Slling case ZPOOL_PROP_SIZE: 274485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED: 275485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE: 276*4263d13fSGeorge Wilson case ZPOOL_PROP_EXPANDSZ: 277990b4856Slling (void) zfs_nicenum(intval, buf, len); 278990b4856Slling break; 279990b4856Slling 280990b4856Slling case ZPOOL_PROP_CAPACITY: 281990b4856Slling (void) snprintf(buf, len, "%llu%%", 282990b4856Slling (u_longlong_t)intval); 283990b4856Slling break; 284990b4856Slling 285b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO: 286b24ab676SJeff Bonwick (void) snprintf(buf, len, "%llu.%02llux", 287b24ab676SJeff Bonwick (u_longlong_t)(intval / 100), 288b24ab676SJeff Bonwick (u_longlong_t)(intval % 100)); 289b24ab676SJeff Bonwick break; 290b24ab676SJeff Bonwick 291990b4856Slling case ZPOOL_PROP_HEALTH: 292990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 293990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 294990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 2953f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 2963f9d6ad7SLin Ling == 0); 297990b4856Slling 298990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 299990b4856Slling vs->vs_aux), len); 300990b4856Slling break; 301990b4856Slling default: 302990b4856Slling (void) snprintf(buf, len, "%llu", intval); 303990b4856Slling } 304990b4856Slling break; 305990b4856Slling 306990b4856Slling case PROP_TYPE_INDEX: 307990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 308990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 309990b4856Slling != 0) 310990b4856Slling return (-1); 311990b4856Slling (void) strlcpy(buf, strval, len); 312990b4856Slling break; 313990b4856Slling 314990b4856Slling default: 315990b4856Slling abort(); 316990b4856Slling } 317990b4856Slling 318990b4856Slling if (srctype) 319990b4856Slling *srctype = src; 320990b4856Slling 321990b4856Slling return (0); 322990b4856Slling } 323990b4856Slling 324990b4856Slling /* 325990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 326990b4856Slling * Assuming bootfs is a valid dataset name. 327990b4856Slling */ 328990b4856Slling static boolean_t 329990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 330990b4856Slling { 331990b4856Slling int len = strlen(pool); 332990b4856Slling 333fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 334990b4856Slling return (B_FALSE); 335990b4856Slling 336990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 337990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 338990b4856Slling return (B_TRUE); 339990b4856Slling 340990b4856Slling return (B_FALSE); 341990b4856Slling } 342990b4856Slling 34315e6edf1Sgw /* 34415e6edf1Sgw * Inspect the configuration to determine if any of the devices contain 34515e6edf1Sgw * an EFI label. 34615e6edf1Sgw */ 34715e6edf1Sgw static boolean_t 34815e6edf1Sgw pool_uses_efi(nvlist_t *config) 34915e6edf1Sgw { 35015e6edf1Sgw nvlist_t **child; 35115e6edf1Sgw uint_t c, children; 35215e6edf1Sgw 35315e6edf1Sgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 35415e6edf1Sgw &child, &children) != 0) 35515e6edf1Sgw return (read_efi_label(config, NULL) >= 0); 35615e6edf1Sgw 35715e6edf1Sgw for (c = 0; c < children; c++) { 35815e6edf1Sgw if (pool_uses_efi(child[c])) 35915e6edf1Sgw return (B_TRUE); 36015e6edf1Sgw } 36115e6edf1Sgw return (B_FALSE); 36215e6edf1Sgw } 36315e6edf1Sgw 364*4263d13fSGeorge Wilson boolean_t 365*4263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp) 366b5b76fecSGeorge Wilson { 367b5b76fecSGeorge Wilson char bootfs[ZPOOL_MAXNAMELEN]; 368b5b76fecSGeorge Wilson 369b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 370b5b76fecSGeorge Wilson sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-", 371b5b76fecSGeorge Wilson sizeof (bootfs)) != 0); 372b5b76fecSGeorge Wilson } 373b5b76fecSGeorge Wilson 374b5b76fecSGeorge Wilson 375990b4856Slling /* 376990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 377990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 378990b4856Slling * specified as strings. 379990b4856Slling */ 380990b4856Slling static nvlist_t * 3810a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 382f9af39baSGeorge Wilson nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf) 383990b4856Slling { 384990b4856Slling nvpair_t *elem; 385990b4856Slling nvlist_t *retprops; 386990b4856Slling zpool_prop_t prop; 387990b4856Slling char *strval; 388990b4856Slling uint64_t intval; 3898704186eSDan McDonald char *slash, *check; 3902f8aaab3Seschrock struct stat64 statbuf; 39115e6edf1Sgw zpool_handle_t *zhp; 39215e6edf1Sgw nvlist_t *nvroot; 393990b4856Slling 394990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 395990b4856Slling (void) no_memory(hdl); 396990b4856Slling return (NULL); 397990b4856Slling } 398990b4856Slling 399990b4856Slling elem = NULL; 400990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 401990b4856Slling const char *propname = nvpair_name(elem); 402990b4856Slling 403990b4856Slling /* 404990b4856Slling * Make sure this property is valid and applies to this type. 405990b4856Slling */ 406990b4856Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) { 407990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 408990b4856Slling "invalid property '%s'"), propname); 409990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 410990b4856Slling goto error; 411990b4856Slling } 412990b4856Slling 413990b4856Slling if (zpool_prop_readonly(prop)) { 414990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 415990b4856Slling "is readonly"), propname); 416990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 417990b4856Slling goto error; 418990b4856Slling } 419990b4856Slling 420990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 421990b4856Slling &strval, &intval, errbuf) != 0) 422990b4856Slling goto error; 423990b4856Slling 424990b4856Slling /* 425990b4856Slling * Perform additional checking for specific properties. 426990b4856Slling */ 427990b4856Slling switch (prop) { 428990b4856Slling case ZPOOL_PROP_VERSION: 429990b4856Slling if (intval < version || intval > SPA_VERSION) { 430990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 431990b4856Slling "property '%s' number %d is invalid."), 432990b4856Slling propname, intval); 433990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 434990b4856Slling goto error; 435990b4856Slling } 436990b4856Slling break; 437990b4856Slling 438990b4856Slling case ZPOOL_PROP_BOOTFS: 439f9af39baSGeorge Wilson if (flags.create || flags.import) { 440990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 441990b4856Slling "property '%s' cannot be set at creation " 442990b4856Slling "or import time"), propname); 443990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 444990b4856Slling goto error; 445990b4856Slling } 446990b4856Slling 447990b4856Slling if (version < SPA_VERSION_BOOTFS) { 448990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 449990b4856Slling "pool must be upgraded to support " 450990b4856Slling "'%s' property"), propname); 451990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 452990b4856Slling goto error; 453990b4856Slling } 454990b4856Slling 455990b4856Slling /* 456990b4856Slling * bootfs property value has to be a dataset name and 457990b4856Slling * the dataset has to be in the same pool as it sets to. 458990b4856Slling */ 459990b4856Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 460990b4856Slling strval)) { 461990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 462990b4856Slling "is an invalid name"), strval); 463990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 464990b4856Slling goto error; 465990b4856Slling } 46615e6edf1Sgw 46715e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 46815e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 46915e6edf1Sgw "could not open pool '%s'"), poolname); 47015e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 47115e6edf1Sgw goto error; 47215e6edf1Sgw } 47315e6edf1Sgw verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 47415e6edf1Sgw ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 47515e6edf1Sgw 47615e6edf1Sgw /* 47715e6edf1Sgw * bootfs property cannot be set on a disk which has 47815e6edf1Sgw * been EFI labeled. 47915e6edf1Sgw */ 48015e6edf1Sgw if (pool_uses_efi(nvroot)) { 48115e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 48215e6edf1Sgw "property '%s' not supported on " 48315e6edf1Sgw "EFI labeled devices"), propname); 48415e6edf1Sgw (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf); 48515e6edf1Sgw zpool_close(zhp); 48615e6edf1Sgw goto error; 48715e6edf1Sgw } 48815e6edf1Sgw zpool_close(zhp); 489990b4856Slling break; 490990b4856Slling 4912f8aaab3Seschrock case ZPOOL_PROP_ALTROOT: 492f9af39baSGeorge Wilson if (!flags.create && !flags.import) { 493990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 494990b4856Slling "property '%s' can only be set during pool " 495990b4856Slling "creation or import"), propname); 496990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 497990b4856Slling goto error; 498990b4856Slling } 499990b4856Slling 5002f8aaab3Seschrock if (strval[0] != '/') { 501990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5022f8aaab3Seschrock "bad alternate root '%s'"), strval); 5032f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 504990b4856Slling goto error; 505990b4856Slling } 5062f8aaab3Seschrock break; 5072f8aaab3Seschrock 5082f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5092f8aaab3Seschrock if (strval[0] == '\0') 5102f8aaab3Seschrock break; 5112f8aaab3Seschrock 5122f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5132f8aaab3Seschrock break; 514990b4856Slling 515990b4856Slling if (strval[0] != '/') { 516990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5172f8aaab3Seschrock "property '%s' must be empty, an " 5182f8aaab3Seschrock "absolute path, or 'none'"), propname); 519990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 520990b4856Slling goto error; 521990b4856Slling } 522990b4856Slling 5232f8aaab3Seschrock slash = strrchr(strval, '/'); 524990b4856Slling 5252f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5262f8aaab3Seschrock strcmp(slash, "/..") == 0) { 5272f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5282f8aaab3Seschrock "'%s' is not a valid file"), strval); 5292f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5302f8aaab3Seschrock goto error; 5312f8aaab3Seschrock } 532990b4856Slling 5332f8aaab3Seschrock *slash = '\0'; 5342f8aaab3Seschrock 5352c32020fSeschrock if (strval[0] != '\0' && 5362c32020fSeschrock (stat64(strval, &statbuf) != 0 || 5372c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 5382f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5392f8aaab3Seschrock "'%s' is not a valid directory"), 5402f8aaab3Seschrock strval); 5412f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5422f8aaab3Seschrock goto error; 5432f8aaab3Seschrock } 5442f8aaab3Seschrock 5452f8aaab3Seschrock *slash = '/'; 5462f8aaab3Seschrock break; 547f9af39baSGeorge Wilson 5488704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5498704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5508704186eSDan McDonald if (!isprint(*check)) { 5518704186eSDan McDonald zfs_error_aux(hdl, 5528704186eSDan McDonald dgettext(TEXT_DOMAIN, 5538704186eSDan McDonald "comment may only have printable " 5548704186eSDan McDonald "characters")); 5558704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, 5568704186eSDan McDonald errbuf); 5578704186eSDan McDonald goto error; 5588704186eSDan McDonald } 5598704186eSDan McDonald } 5608704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) { 5618704186eSDan McDonald zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5628704186eSDan McDonald "comment must not exceed %d characters"), 5638704186eSDan McDonald ZPROP_MAX_COMMENT); 5648704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5658704186eSDan McDonald goto error; 5668704186eSDan McDonald } 5678704186eSDan McDonald break; 568f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 569f9af39baSGeorge Wilson if (!flags.import) { 570f9af39baSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 571f9af39baSGeorge Wilson "property '%s' can only be set at " 572f9af39baSGeorge Wilson "import time"), propname); 573f9af39baSGeorge Wilson (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 574f9af39baSGeorge Wilson goto error; 575f9af39baSGeorge Wilson } 576f9af39baSGeorge Wilson break; 577990b4856Slling } 578990b4856Slling } 579990b4856Slling 580990b4856Slling return (retprops); 581990b4856Slling error: 582990b4856Slling nvlist_free(retprops); 583990b4856Slling return (NULL); 584990b4856Slling } 585990b4856Slling 586990b4856Slling /* 587990b4856Slling * Set zpool property : propname=propval. 588990b4856Slling */ 589990b4856Slling int 590990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 591990b4856Slling { 592990b4856Slling zfs_cmd_t zc = { 0 }; 593990b4856Slling int ret = -1; 594990b4856Slling char errbuf[1024]; 595990b4856Slling nvlist_t *nvl = NULL; 596990b4856Slling nvlist_t *realprops; 597990b4856Slling uint64_t version; 598f9af39baSGeorge Wilson prop_flags_t flags = { 0 }; 599990b4856Slling 600990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 601990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 602990b4856Slling zhp->zpool_name); 603990b4856Slling 604990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 605990b4856Slling return (no_memory(zhp->zpool_hdl)); 606990b4856Slling 607990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 608990b4856Slling nvlist_free(nvl); 609990b4856Slling return (no_memory(zhp->zpool_hdl)); 610990b4856Slling } 611990b4856Slling 612990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 6130a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 614f9af39baSGeorge Wilson zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) { 615990b4856Slling nvlist_free(nvl); 616990b4856Slling return (-1); 617990b4856Slling } 618990b4856Slling 619990b4856Slling nvlist_free(nvl); 620990b4856Slling nvl = realprops; 621990b4856Slling 622990b4856Slling /* 623990b4856Slling * Execute the corresponding ioctl() to set this property. 624990b4856Slling */ 625990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 626990b4856Slling 627990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 628990b4856Slling nvlist_free(nvl); 629990b4856Slling return (-1); 630990b4856Slling } 631990b4856Slling 632990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 633990b4856Slling 634990b4856Slling zcmd_free_nvlists(&zc); 635990b4856Slling nvlist_free(nvl); 636990b4856Slling 637990b4856Slling if (ret) 638990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 639990b4856Slling else 640990b4856Slling (void) zpool_props_refresh(zhp); 641990b4856Slling 642990b4856Slling return (ret); 643990b4856Slling } 644990b4856Slling 645990b4856Slling int 646990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 647990b4856Slling { 648990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 649990b4856Slling zprop_list_t *entry; 650990b4856Slling char buf[ZFS_MAXPROPLEN]; 651990b4856Slling 652990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 653990b4856Slling return (-1); 654990b4856Slling 655990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 656990b4856Slling 657990b4856Slling if (entry->pl_fixed) 658990b4856Slling continue; 659990b4856Slling 660990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 661990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 662990b4856Slling NULL) == 0) { 663990b4856Slling if (strlen(buf) > entry->pl_width) 664990b4856Slling entry->pl_width = strlen(buf); 665990b4856Slling } 666990b4856Slling } 667990b4856Slling 668990b4856Slling return (0); 669990b4856Slling } 670990b4856Slling 671990b4856Slling 672573ca77eSGeorge Wilson /* 673573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage 674573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead. 675573ca77eSGeorge Wilson */ 676573ca77eSGeorge Wilson #define NEW_START_BLOCK 256 677573ca77eSGeorge Wilson 678fa9e4066Sahrens /* 679fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 680fa9e4066Sahrens * 'buf'. 681fa9e4066Sahrens */ 682e7cbe64fSgw boolean_t 68399653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 684fa9e4066Sahrens { 685fa9e4066Sahrens namecheck_err_t why; 686fa9e4066Sahrens char what; 687b468a217Seschrock int ret; 688b468a217Seschrock 689b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 690b468a217Seschrock 691b468a217Seschrock /* 692b468a217Seschrock * The rules for reserved pool names were extended at a later point. 693b468a217Seschrock * But we need to support users with existing pools that may now be 694b468a217Seschrock * invalid. So we only check for this expanded set of names during a 695b468a217Seschrock * create (or import), and only in userland. 696b468a217Seschrock */ 697b468a217Seschrock if (ret == 0 && !isopen && 698b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 699b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 7008654d025Sperrin strncmp(pool, "spare", 5) == 0 || 7018654d025Sperrin strcmp(pool, "log") == 0)) { 702e7cbe64fSgw if (hdl != NULL) 703e7cbe64fSgw zfs_error_aux(hdl, 704e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved")); 70599653d4eSeschrock return (B_FALSE); 706b468a217Seschrock } 707b468a217Seschrock 708fa9e4066Sahrens 709b468a217Seschrock if (ret != 0) { 71099653d4eSeschrock if (hdl != NULL) { 711fa9e4066Sahrens switch (why) { 712b81d61a6Slling case NAME_ERR_TOOLONG: 71399653d4eSeschrock zfs_error_aux(hdl, 714b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 715b81d61a6Slling break; 716b81d61a6Slling 717fa9e4066Sahrens case NAME_ERR_INVALCHAR: 71899653d4eSeschrock zfs_error_aux(hdl, 719fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 720fa9e4066Sahrens "'%c' in pool name"), what); 721fa9e4066Sahrens break; 722fa9e4066Sahrens 723fa9e4066Sahrens case NAME_ERR_NOLETTER: 72499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 72599653d4eSeschrock "name must begin with a letter")); 726fa9e4066Sahrens break; 727fa9e4066Sahrens 728fa9e4066Sahrens case NAME_ERR_RESERVED: 72999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 73099653d4eSeschrock "name is reserved")); 731fa9e4066Sahrens break; 732fa9e4066Sahrens 733fa9e4066Sahrens case NAME_ERR_DISKLIKE: 73499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 73599653d4eSeschrock "pool name is reserved")); 736fa9e4066Sahrens break; 7375ad82045Snd 7385ad82045Snd case NAME_ERR_LEADING_SLASH: 7395ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7405ad82045Snd "leading slash in name")); 7415ad82045Snd break; 7425ad82045Snd 7435ad82045Snd case NAME_ERR_EMPTY_COMPONENT: 7445ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7455ad82045Snd "empty component in name")); 7465ad82045Snd break; 7475ad82045Snd 7485ad82045Snd case NAME_ERR_TRAILING_SLASH: 7495ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7505ad82045Snd "trailing slash in name")); 7515ad82045Snd break; 7525ad82045Snd 7535ad82045Snd case NAME_ERR_MULTIPLE_AT: 7545ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7555ad82045Snd "multiple '@' delimiters in name")); 7565ad82045Snd break; 7575ad82045Snd 758fa9e4066Sahrens } 759fa9e4066Sahrens } 76099653d4eSeschrock return (B_FALSE); 761fa9e4066Sahrens } 762fa9e4066Sahrens 76399653d4eSeschrock return (B_TRUE); 764fa9e4066Sahrens } 765fa9e4066Sahrens 766fa9e4066Sahrens /* 767fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 768fa9e4066Sahrens * state. 769fa9e4066Sahrens */ 770fa9e4066Sahrens zpool_handle_t * 77199653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 772fa9e4066Sahrens { 773fa9e4066Sahrens zpool_handle_t *zhp; 77494de1d4cSeschrock boolean_t missing; 775fa9e4066Sahrens 776fa9e4066Sahrens /* 777fa9e4066Sahrens * Make sure the pool name is valid. 778fa9e4066Sahrens */ 77999653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 780ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 78199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 78299653d4eSeschrock pool); 783fa9e4066Sahrens return (NULL); 784fa9e4066Sahrens } 785fa9e4066Sahrens 78699653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 78799653d4eSeschrock return (NULL); 788fa9e4066Sahrens 78999653d4eSeschrock zhp->zpool_hdl = hdl; 790fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 791fa9e4066Sahrens 79294de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 79394de1d4cSeschrock zpool_close(zhp); 79494de1d4cSeschrock return (NULL); 79594de1d4cSeschrock } 79694de1d4cSeschrock 79794de1d4cSeschrock if (missing) { 798990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 799ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 800990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 80194de1d4cSeschrock zpool_close(zhp); 80294de1d4cSeschrock return (NULL); 803fa9e4066Sahrens } 804fa9e4066Sahrens 805fa9e4066Sahrens return (zhp); 806fa9e4066Sahrens } 807fa9e4066Sahrens 808fa9e4066Sahrens /* 809fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 810fa9e4066Sahrens * the configuration cache may be out of date). 811fa9e4066Sahrens */ 81294de1d4cSeschrock int 81394de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 814fa9e4066Sahrens { 815fa9e4066Sahrens zpool_handle_t *zhp; 81694de1d4cSeschrock boolean_t missing; 817fa9e4066Sahrens 81894de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 81994de1d4cSeschrock return (-1); 820fa9e4066Sahrens 82199653d4eSeschrock zhp->zpool_hdl = hdl; 822fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 823fa9e4066Sahrens 82494de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 82594de1d4cSeschrock zpool_close(zhp); 82694de1d4cSeschrock return (-1); 827fa9e4066Sahrens } 828fa9e4066Sahrens 82994de1d4cSeschrock if (missing) { 83094de1d4cSeschrock zpool_close(zhp); 83194de1d4cSeschrock *ret = NULL; 83294de1d4cSeschrock return (0); 83394de1d4cSeschrock } 83494de1d4cSeschrock 83594de1d4cSeschrock *ret = zhp; 83694de1d4cSeschrock return (0); 837fa9e4066Sahrens } 838fa9e4066Sahrens 839fa9e4066Sahrens /* 840fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 841fa9e4066Sahrens * state. 842fa9e4066Sahrens */ 843fa9e4066Sahrens zpool_handle_t * 84499653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 845fa9e4066Sahrens { 846fa9e4066Sahrens zpool_handle_t *zhp; 847fa9e4066Sahrens 84899653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 849fa9e4066Sahrens return (NULL); 850fa9e4066Sahrens 851fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 852ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 85399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 854fa9e4066Sahrens zpool_close(zhp); 855fa9e4066Sahrens return (NULL); 856fa9e4066Sahrens } 857fa9e4066Sahrens 858fa9e4066Sahrens return (zhp); 859fa9e4066Sahrens } 860fa9e4066Sahrens 861fa9e4066Sahrens /* 862fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 863fa9e4066Sahrens */ 864fa9e4066Sahrens void 865fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 866fa9e4066Sahrens { 867fa9e4066Sahrens if (zhp->zpool_config) 868fa9e4066Sahrens nvlist_free(zhp->zpool_config); 869088e9d47Seschrock if (zhp->zpool_old_config) 870088e9d47Seschrock nvlist_free(zhp->zpool_old_config); 871b1b8ab34Slling if (zhp->zpool_props) 872b1b8ab34Slling nvlist_free(zhp->zpool_props); 873fa9e4066Sahrens free(zhp); 874fa9e4066Sahrens } 875fa9e4066Sahrens 876fa9e4066Sahrens /* 877fa9e4066Sahrens * Return the name of the pool. 878fa9e4066Sahrens */ 879fa9e4066Sahrens const char * 880fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 881fa9e4066Sahrens { 882fa9e4066Sahrens return (zhp->zpool_name); 883fa9e4066Sahrens } 884fa9e4066Sahrens 885fa9e4066Sahrens 886fa9e4066Sahrens /* 887fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 888fa9e4066Sahrens */ 889fa9e4066Sahrens int 890fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 891fa9e4066Sahrens { 892fa9e4066Sahrens return (zhp->zpool_state); 893fa9e4066Sahrens } 894fa9e4066Sahrens 895fa9e4066Sahrens /* 896fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 897fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 898fa9e4066Sahrens * don't have to worry about error semantics. 899fa9e4066Sahrens */ 900fa9e4066Sahrens int 90199653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 9020a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 903fa9e4066Sahrens { 904fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 9050a48a24eStimh nvlist_t *zc_fsprops = NULL; 9060a48a24eStimh nvlist_t *zc_props = NULL; 90799653d4eSeschrock char msg[1024]; 908990b4856Slling char *altroot; 9090a48a24eStimh int ret = -1; 910fa9e4066Sahrens 91199653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 91299653d4eSeschrock "cannot create '%s'"), pool); 913fa9e4066Sahrens 91499653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 91599653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 916fa9e4066Sahrens 917351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 918990b4856Slling return (-1); 919fa9e4066Sahrens 9200a48a24eStimh if (props) { 921f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE }; 922f9af39baSGeorge Wilson 9230a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 924f9af39baSGeorge Wilson SPA_VERSION_1, flags, msg)) == NULL) { 9250a48a24eStimh goto create_failed; 9260a48a24eStimh } 9270a48a24eStimh } 92899653d4eSeschrock 9290a48a24eStimh if (fsprops) { 9300a48a24eStimh uint64_t zoned; 9310a48a24eStimh char *zonestr; 9320a48a24eStimh 9330a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 9340a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 9350a48a24eStimh strcmp(zonestr, "on") == 0); 9360a48a24eStimh 9370a48a24eStimh if ((zc_fsprops = zfs_valid_proplist(hdl, 9380a48a24eStimh ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) { 9390a48a24eStimh goto create_failed; 9400a48a24eStimh } 9410a48a24eStimh if (!zc_props && 9420a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 9430a48a24eStimh goto create_failed; 9440a48a24eStimh } 9450a48a24eStimh if (nvlist_add_nvlist(zc_props, 9460a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 9470a48a24eStimh goto create_failed; 9480a48a24eStimh } 949351420b3Slling } 950fa9e4066Sahrens 9510a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 9520a48a24eStimh goto create_failed; 9530a48a24eStimh 954990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 955fa9e4066Sahrens 9560a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 957351420b3Slling 958e9dbad6fSeschrock zcmd_free_nvlists(&zc); 9590a48a24eStimh nvlist_free(zc_props); 9600a48a24eStimh nvlist_free(zc_fsprops); 961fa9e4066Sahrens 96299653d4eSeschrock switch (errno) { 963fa9e4066Sahrens case EBUSY: 964fa9e4066Sahrens /* 965fa9e4066Sahrens * This can happen if the user has specified the same 966fa9e4066Sahrens * device multiple times. We can't reliably detect this 967fa9e4066Sahrens * until we try to add it and see we already have a 968fa9e4066Sahrens * label. 969fa9e4066Sahrens */ 97099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 97199653d4eSeschrock "one or more vdevs refer to the same device")); 97299653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 973fa9e4066Sahrens 974fa9e4066Sahrens case EOVERFLOW: 975fa9e4066Sahrens /* 97699653d4eSeschrock * This occurs when one of the devices is below 977fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 978fa9e4066Sahrens * device was the problem device since there's no 979fa9e4066Sahrens * reliable way to determine device size from userland. 980fa9e4066Sahrens */ 981fa9e4066Sahrens { 982fa9e4066Sahrens char buf[64]; 983fa9e4066Sahrens 984fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 985fa9e4066Sahrens 98699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 98799653d4eSeschrock "one or more devices is less than the " 98899653d4eSeschrock "minimum size (%s)"), buf); 989fa9e4066Sahrens } 99099653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 991fa9e4066Sahrens 992fa9e4066Sahrens case ENOSPC: 99399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 99499653d4eSeschrock "one or more devices is out of space")); 99599653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 996fa9e4066Sahrens 997fa94a07fSbrendan case ENOTBLK: 998fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 999fa94a07fSbrendan "cache device must be a disk or disk slice")); 1000fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 1001fa94a07fSbrendan 1002fa9e4066Sahrens default: 100399653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1004fa9e4066Sahrens } 1005fa9e4066Sahrens } 1006fa9e4066Sahrens 1007fa9e4066Sahrens /* 1008fa9e4066Sahrens * If this is an alternate root pool, then we automatically set the 1009e9dbad6fSeschrock * mountpoint of the root dataset to be '/'. 1010fa9e4066Sahrens */ 1011990b4856Slling if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT), 1012990b4856Slling &altroot) == 0) { 1013fa9e4066Sahrens zfs_handle_t *zhp; 1014fa9e4066Sahrens 1015990b4856Slling verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL); 1016e9dbad6fSeschrock verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1017e9dbad6fSeschrock "/") == 0); 1018fa9e4066Sahrens 1019fa9e4066Sahrens zfs_close(zhp); 1020fa9e4066Sahrens } 1021fa9e4066Sahrens 10220a48a24eStimh create_failed: 1023351420b3Slling zcmd_free_nvlists(&zc); 10240a48a24eStimh nvlist_free(zc_props); 10250a48a24eStimh nvlist_free(zc_fsprops); 10260a48a24eStimh return (ret); 1027fa9e4066Sahrens } 1028fa9e4066Sahrens 1029fa9e4066Sahrens /* 1030fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 1031fa9e4066Sahrens * datasets left in the pool. 1032fa9e4066Sahrens */ 1033fa9e4066Sahrens int 1034fa9e4066Sahrens zpool_destroy(zpool_handle_t *zhp) 1035fa9e4066Sahrens { 1036fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1037fa9e4066Sahrens zfs_handle_t *zfp = NULL; 103899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 103999653d4eSeschrock char msg[1024]; 1040fa9e4066Sahrens 1041fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 1042cb04b873SMark J Musante (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1043fa9e4066Sahrens return (-1); 1044fa9e4066Sahrens 1045fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1046fa9e4066Sahrens 1047cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 104899653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 104999653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 1050fa9e4066Sahrens 105199653d4eSeschrock if (errno == EROFS) { 105299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 105399653d4eSeschrock "one or more devices is read only")); 105499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 105599653d4eSeschrock } else { 105699653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1057fa9e4066Sahrens } 1058fa9e4066Sahrens 1059fa9e4066Sahrens if (zfp) 1060fa9e4066Sahrens zfs_close(zfp); 1061fa9e4066Sahrens return (-1); 1062fa9e4066Sahrens } 1063fa9e4066Sahrens 1064fa9e4066Sahrens if (zfp) { 1065fa9e4066Sahrens remove_mountpoint(zfp); 1066fa9e4066Sahrens zfs_close(zfp); 1067fa9e4066Sahrens } 1068fa9e4066Sahrens 1069fa9e4066Sahrens return (0); 1070fa9e4066Sahrens } 1071fa9e4066Sahrens 1072fa9e4066Sahrens /* 1073fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1074fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1075fa9e4066Sahrens */ 1076fa9e4066Sahrens int 1077fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1078fa9e4066Sahrens { 1079e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 108099653d4eSeschrock int ret; 108199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 108299653d4eSeschrock char msg[1024]; 1083fa94a07fSbrendan nvlist_t **spares, **l2cache; 1084fa94a07fSbrendan uint_t nspares, nl2cache; 108599653d4eSeschrock 108699653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 108799653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 108899653d4eSeschrock 1089fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1090fa94a07fSbrendan SPA_VERSION_SPARES && 109199653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 109299653d4eSeschrock &spares, &nspares) == 0) { 109399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 109499653d4eSeschrock "upgraded to add hot spares")); 109599653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 109699653d4eSeschrock } 1097fa9e4066Sahrens 1098*4263d13fSGeorge Wilson if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot, 1099b5b76fecSGeorge Wilson ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 1100b5b76fecSGeorge Wilson uint64_t s; 1101b5b76fecSGeorge Wilson 1102b5b76fecSGeorge Wilson for (s = 0; s < nspares; s++) { 1103b5b76fecSGeorge Wilson char *path; 1104b5b76fecSGeorge Wilson 1105b5b76fecSGeorge Wilson if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH, 1106b5b76fecSGeorge Wilson &path) == 0 && pool_uses_efi(spares[s])) { 1107b5b76fecSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1108b5b76fecSGeorge Wilson "device '%s' contains an EFI label and " 1109b5b76fecSGeorge Wilson "cannot be used on root pools."), 111088ecc943SGeorge Wilson zpool_vdev_name(hdl, NULL, spares[s], 111188ecc943SGeorge Wilson B_FALSE)); 1112b5b76fecSGeorge Wilson return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 1113b5b76fecSGeorge Wilson } 1114b5b76fecSGeorge Wilson } 1115b5b76fecSGeorge Wilson } 1116b5b76fecSGeorge Wilson 1117fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1118fa94a07fSbrendan SPA_VERSION_L2CACHE && 1119fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1120fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1121fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1122fa94a07fSbrendan "upgraded to add cache devices")); 1123fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1124fa94a07fSbrendan } 1125fa94a07fSbrendan 1126990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 112799653d4eSeschrock return (-1); 1128fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1129fa9e4066Sahrens 1130cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1131fa9e4066Sahrens switch (errno) { 1132fa9e4066Sahrens case EBUSY: 1133fa9e4066Sahrens /* 1134fa9e4066Sahrens * This can happen if the user has specified the same 1135fa9e4066Sahrens * device multiple times. We can't reliably detect this 1136fa9e4066Sahrens * until we try to add it and see we already have a 1137fa9e4066Sahrens * label. 1138fa9e4066Sahrens */ 113999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 114099653d4eSeschrock "one or more vdevs refer to the same device")); 114199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1142fa9e4066Sahrens break; 1143fa9e4066Sahrens 1144fa9e4066Sahrens case EOVERFLOW: 1145fa9e4066Sahrens /* 1146fa9e4066Sahrens * This occurrs when one of the devices is below 1147fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1148fa9e4066Sahrens * device was the problem device since there's no 1149fa9e4066Sahrens * reliable way to determine device size from userland. 1150fa9e4066Sahrens */ 1151fa9e4066Sahrens { 1152fa9e4066Sahrens char buf[64]; 1153fa9e4066Sahrens 1154fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1155fa9e4066Sahrens 115699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 115799653d4eSeschrock "device is less than the minimum " 115899653d4eSeschrock "size (%s)"), buf); 1159fa9e4066Sahrens } 116099653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 116199653d4eSeschrock break; 116299653d4eSeschrock 116399653d4eSeschrock case ENOTSUP: 116499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11658654d025Sperrin "pool must be upgraded to add these vdevs")); 116699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1167fa9e4066Sahrens break; 1168fa9e4066Sahrens 1169b1b8ab34Slling case EDOM: 1170b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11718654d025Sperrin "root pool can not have multiple vdevs" 11728654d025Sperrin " or separate logs")); 1173b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1174b1b8ab34Slling break; 1175b1b8ab34Slling 1176fa94a07fSbrendan case ENOTBLK: 1177fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1178fa94a07fSbrendan "cache device must be a disk or disk slice")); 1179fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1180fa94a07fSbrendan break; 1181fa94a07fSbrendan 1182fa9e4066Sahrens default: 118399653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1184fa9e4066Sahrens } 1185fa9e4066Sahrens 118699653d4eSeschrock ret = -1; 118799653d4eSeschrock } else { 118899653d4eSeschrock ret = 0; 1189fa9e4066Sahrens } 1190fa9e4066Sahrens 1191e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1192fa9e4066Sahrens 119399653d4eSeschrock return (ret); 1194fa9e4066Sahrens } 1195fa9e4066Sahrens 1196fa9e4066Sahrens /* 1197fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1198fa9e4066Sahrens * mounted datasets in the pool. 1199fa9e4066Sahrens */ 1200fa9e4066Sahrens int 1201394ab0cbSGeorge Wilson zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce) 1202fa9e4066Sahrens { 1203fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 120489a89ebfSlling char msg[1024]; 1205fa9e4066Sahrens 120689a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 120789a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 120889a89ebfSlling 1209fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 121089a89ebfSlling zc.zc_cookie = force; 1211394ab0cbSGeorge Wilson zc.zc_guid = hardforce; 121289a89ebfSlling 121389a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 121489a89ebfSlling switch (errno) { 121589a89ebfSlling case EXDEV: 121689a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 121789a89ebfSlling "use '-f' to override the following errors:\n" 121889a89ebfSlling "'%s' has an active shared spare which could be" 121989a89ebfSlling " used by other pools once '%s' is exported."), 122089a89ebfSlling zhp->zpool_name, zhp->zpool_name); 122189a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 122289a89ebfSlling msg)); 122389a89ebfSlling default: 122489a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 122589a89ebfSlling msg)); 122689a89ebfSlling } 122789a89ebfSlling } 1228fa9e4066Sahrens 1229fa9e4066Sahrens return (0); 1230fa9e4066Sahrens } 1231fa9e4066Sahrens 1232394ab0cbSGeorge Wilson int 1233394ab0cbSGeorge Wilson zpool_export(zpool_handle_t *zhp, boolean_t force) 1234394ab0cbSGeorge Wilson { 1235394ab0cbSGeorge Wilson return (zpool_export_common(zhp, force, B_FALSE)); 1236394ab0cbSGeorge Wilson } 1237394ab0cbSGeorge Wilson 1238394ab0cbSGeorge Wilson int 1239394ab0cbSGeorge Wilson zpool_export_force(zpool_handle_t *zhp) 1240394ab0cbSGeorge Wilson { 1241394ab0cbSGeorge Wilson return (zpool_export_common(zhp, B_TRUE, B_TRUE)); 1242394ab0cbSGeorge Wilson } 1243394ab0cbSGeorge Wilson 1244468c413aSTim Haley static void 1245468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 12464b964adaSGeorge Wilson nvlist_t *config) 1247468c413aSTim Haley { 12484b964adaSGeorge Wilson nvlist_t *nv = NULL; 1249468c413aSTim Haley uint64_t rewindto; 1250468c413aSTim Haley int64_t loss = -1; 1251468c413aSTim Haley struct tm t; 1252468c413aSTim Haley char timestr[128]; 1253468c413aSTim Haley 12544b964adaSGeorge Wilson if (!hdl->libzfs_printerr || config == NULL) 12554b964adaSGeorge Wilson return; 12564b964adaSGeorge Wilson 12574b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0) 1258468c413aSTim Haley return; 1259468c413aSTim Haley 12604b964adaSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1261468c413aSTim Haley return; 12624b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1263468c413aSTim Haley 1264468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1265468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1266468c413aSTim Haley if (dryrun) { 1267468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1268468c413aSTim Haley "Would be able to return %s " 1269468c413aSTim Haley "to its state as of %s.\n"), 1270468c413aSTim Haley name, timestr); 1271468c413aSTim Haley } else { 1272468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1273468c413aSTim Haley "Pool %s returned to its state as of %s.\n"), 1274468c413aSTim Haley name, timestr); 1275468c413aSTim Haley } 1276468c413aSTim Haley if (loss > 120) { 1277468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1278468c413aSTim Haley "%s approximately %lld "), 1279468c413aSTim Haley dryrun ? "Would discard" : "Discarded", 1280468c413aSTim Haley (loss + 30) / 60); 1281468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1282468c413aSTim Haley "minutes of transactions.\n")); 1283468c413aSTim Haley } else if (loss > 0) { 1284468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1285468c413aSTim Haley "%s approximately %lld "), 1286468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss); 1287468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1288468c413aSTim Haley "seconds of transactions.\n")); 1289468c413aSTim Haley } 1290468c413aSTim Haley } 1291468c413aSTim Haley } 1292468c413aSTim Haley 1293468c413aSTim Haley void 1294468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1295468c413aSTim Haley nvlist_t *config) 1296468c413aSTim Haley { 12974b964adaSGeorge Wilson nvlist_t *nv = NULL; 1298468c413aSTim Haley int64_t loss = -1; 1299468c413aSTim Haley uint64_t edata = UINT64_MAX; 1300468c413aSTim Haley uint64_t rewindto; 1301468c413aSTim Haley struct tm t; 1302468c413aSTim Haley char timestr[128]; 1303468c413aSTim Haley 1304468c413aSTim Haley if (!hdl->libzfs_printerr) 1305468c413aSTim Haley return; 1306468c413aSTim Haley 1307468c413aSTim Haley if (reason >= 0) 1308468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1309468c413aSTim Haley else 1310468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1311468c413aSTim Haley 1312468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 13134b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 13144b964adaSGeorge Wilson nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1315468c413aSTim Haley goto no_info; 1316468c413aSTim Haley 13174b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 13184b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1319468c413aSTim Haley &edata); 1320468c413aSTim Haley 1321468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1322468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n")); 1323468c413aSTim Haley 1324468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1325468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1326468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1327468c413aSTim Haley "\tReturning the pool to its state as of %s\n" 1328468c413aSTim Haley "\tshould correct the problem. "), 1329468c413aSTim Haley timestr); 1330468c413aSTim Haley } else { 1331468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1332468c413aSTim Haley "\tReverting the pool to an earlier state " 1333468c413aSTim Haley "should correct the problem.\n\t")); 1334468c413aSTim Haley } 1335468c413aSTim Haley 1336468c413aSTim Haley if (loss > 120) { 1337468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1338468c413aSTim Haley "Approximately %lld minutes of data\n" 1339468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1340468c413aSTim Haley } else if (loss > 0) { 1341468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1342468c413aSTim Haley "Approximately %lld seconds of data\n" 1343468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss); 1344468c413aSTim Haley } 1345468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) { 1346468c413aSTim Haley if (edata == 1) { 1347468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1348468c413aSTim Haley "After rewind, at least\n" 1349468c413aSTim Haley "\tone persistent user-data error will remain. ")); 1350468c413aSTim Haley } else { 1351468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1352468c413aSTim Haley "After rewind, several\n" 1353468c413aSTim Haley "\tpersistent user-data errors will remain. ")); 1354468c413aSTim Haley } 1355468c413aSTim Haley } 1356468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1357a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1358a33cae98STim Haley reason >= 0 ? "clear" : "import", name); 1359468c413aSTim Haley 1360468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1361468c413aSTim Haley "A scrub of the pool\n" 1362468c413aSTim Haley "\tis strongly recommended after recovery.\n")); 1363468c413aSTim Haley return; 1364468c413aSTim Haley 1365468c413aSTim Haley no_info: 1366468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1367468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n")); 1368468c413aSTim Haley } 1369468c413aSTim Haley 1370fa9e4066Sahrens /* 1371990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1372990b4856Slling * if possible. 1373990b4856Slling * 1374990b4856Slling * Applications should use zpool_import_props() to import a pool with 1375990b4856Slling * new properties value to be set. 1376fa9e4066Sahrens */ 1377fa9e4066Sahrens int 137899653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1379990b4856Slling char *altroot) 1380990b4856Slling { 1381990b4856Slling nvlist_t *props = NULL; 1382990b4856Slling int ret; 1383990b4856Slling 1384990b4856Slling if (altroot != NULL) { 1385990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1386990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1387990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1388990b4856Slling newname)); 1389990b4856Slling } 1390990b4856Slling 1391990b4856Slling if (nvlist_add_string(props, 1392352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1393352d8027SGeorge Wilson nvlist_add_string(props, 1394352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1395990b4856Slling nvlist_free(props); 1396990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1397990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1398990b4856Slling newname)); 1399990b4856Slling } 1400990b4856Slling } 1401990b4856Slling 14024b964adaSGeorge Wilson ret = zpool_import_props(hdl, config, newname, props, 14034b964adaSGeorge Wilson ZFS_IMPORT_NORMAL); 1404990b4856Slling if (props) 1405990b4856Slling nvlist_free(props); 1406990b4856Slling return (ret); 1407990b4856Slling } 1408990b4856Slling 14094b964adaSGeorge Wilson static void 14104b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 14114b964adaSGeorge Wilson int indent) 14124b964adaSGeorge Wilson { 14134b964adaSGeorge Wilson nvlist_t **child; 14144b964adaSGeorge Wilson uint_t c, children; 14154b964adaSGeorge Wilson char *vname; 14164b964adaSGeorge Wilson uint64_t is_log = 0; 14174b964adaSGeorge Wilson 14184b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 14194b964adaSGeorge Wilson &is_log); 14204b964adaSGeorge Wilson 14214b964adaSGeorge Wilson if (name != NULL) 14224b964adaSGeorge Wilson (void) printf("\t%*s%s%s\n", indent, "", name, 14234b964adaSGeorge Wilson is_log ? " [log]" : ""); 14244b964adaSGeorge Wilson 14254b964adaSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 14264b964adaSGeorge Wilson &child, &children) != 0) 14274b964adaSGeorge Wilson return; 14284b964adaSGeorge Wilson 14294b964adaSGeorge Wilson for (c = 0; c < children; c++) { 14304b964adaSGeorge Wilson vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 14314b964adaSGeorge Wilson print_vdev_tree(hdl, vname, child[c], indent + 2); 14324b964adaSGeorge Wilson free(vname); 14334b964adaSGeorge Wilson } 14344b964adaSGeorge Wilson } 14354b964adaSGeorge Wilson 1436990b4856Slling /* 1437990b4856Slling * Import the given pool using the known configuration and a list of 1438990b4856Slling * properties to be set. The configuration should have come from 1439990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1440990b4856Slling * is imported with a different name. 1441990b4856Slling */ 1442990b4856Slling int 1443990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 14444b964adaSGeorge Wilson nvlist_t *props, int flags) 1445fa9e4066Sahrens { 1446e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1447468c413aSTim Haley zpool_rewind_policy_t policy; 14484b964adaSGeorge Wilson nvlist_t *nv = NULL; 14494b964adaSGeorge Wilson nvlist_t *nvinfo = NULL; 14504b964adaSGeorge Wilson nvlist_t *missing = NULL; 1451fa9e4066Sahrens char *thename; 1452fa9e4066Sahrens char *origname; 1453fa9e4066Sahrens int ret; 14544b964adaSGeorge Wilson int error = 0; 1455990b4856Slling char errbuf[1024]; 1456fa9e4066Sahrens 1457fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1458fa9e4066Sahrens &origname) == 0); 1459fa9e4066Sahrens 1460990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1461990b4856Slling "cannot import pool '%s'"), origname); 1462990b4856Slling 1463fa9e4066Sahrens if (newname != NULL) { 146499653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1465ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 146699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 146799653d4eSeschrock newname)); 1468fa9e4066Sahrens thename = (char *)newname; 1469fa9e4066Sahrens } else { 1470fa9e4066Sahrens thename = origname; 1471fa9e4066Sahrens } 1472fa9e4066Sahrens 1473990b4856Slling if (props) { 1474990b4856Slling uint64_t version; 1475f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 1476fa9e4066Sahrens 1477990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1478990b4856Slling &version) == 0); 1479fa9e4066Sahrens 14800a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1481f9af39baSGeorge Wilson props, version, flags, errbuf)) == NULL) { 1482990b4856Slling return (-1); 1483351420b3Slling } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1484351420b3Slling nvlist_free(props); 1485990b4856Slling return (-1); 1486351420b3Slling } 1487990b4856Slling } 1488990b4856Slling 1489990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1490fa9e4066Sahrens 1491fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1492ea8dc4b6Seschrock &zc.zc_guid) == 0); 1493fa9e4066Sahrens 1494351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1495351420b3Slling nvlist_free(props); 149699653d4eSeschrock return (-1); 1497351420b3Slling } 149857f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1499468c413aSTim Haley nvlist_free(props); 1500468c413aSTim Haley return (-1); 1501468c413aSTim Haley } 1502fa9e4066Sahrens 15034b964adaSGeorge Wilson zc.zc_cookie = flags; 15044b964adaSGeorge Wilson while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 15054b964adaSGeorge Wilson errno == ENOMEM) { 15064b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 15074b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 15084b964adaSGeorge Wilson return (-1); 15094b964adaSGeorge Wilson } 15104b964adaSGeorge Wilson } 15114b964adaSGeorge Wilson if (ret != 0) 15124b964adaSGeorge Wilson error = errno; 15134b964adaSGeorge Wilson 15144b964adaSGeorge Wilson (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 15154b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 15164b964adaSGeorge Wilson 15174b964adaSGeorge Wilson if (error) { 1518fa9e4066Sahrens char desc[1024]; 1519468c413aSTim Haley 1520468c413aSTim Haley /* 1521468c413aSTim Haley * Dry-run failed, but we print out what success 1522468c413aSTim Haley * looks like if we found a best txg 1523468c413aSTim Haley */ 15244b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_TRY_REWIND) { 1525468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 15264b964adaSGeorge Wilson B_TRUE, nv); 15274b964adaSGeorge Wilson nvlist_free(nv); 1528468c413aSTim Haley return (-1); 1529468c413aSTim Haley } 1530468c413aSTim Haley 1531fa9e4066Sahrens if (newname == NULL) 1532fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1533fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1534fa9e4066Sahrens thename); 1535fa9e4066Sahrens else 1536fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1537fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1538fa9e4066Sahrens origname, thename); 1539fa9e4066Sahrens 15404b964adaSGeorge Wilson switch (error) { 1541ea8dc4b6Seschrock case ENOTSUP: 1542ea8dc4b6Seschrock /* 1543ea8dc4b6Seschrock * Unsupported version. 1544ea8dc4b6Seschrock */ 154599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1546ea8dc4b6Seschrock break; 1547ea8dc4b6Seschrock 1548b5989ec7Seschrock case EINVAL: 1549b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1550b5989ec7Seschrock break; 1551b5989ec7Seschrock 155254a91118SChris Kirby case EROFS: 155354a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 155454a91118SChris Kirby "one or more devices is read only")); 155554a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc); 155654a91118SChris Kirby break; 155754a91118SChris Kirby 15584b964adaSGeorge Wilson case ENXIO: 15594b964adaSGeorge Wilson if (nv && nvlist_lookup_nvlist(nv, 15604b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 15614b964adaSGeorge Wilson nvlist_lookup_nvlist(nvinfo, 15624b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 15634b964adaSGeorge Wilson (void) printf(dgettext(TEXT_DOMAIN, 15644b964adaSGeorge Wilson "The devices below are missing, use " 15654b964adaSGeorge Wilson "'-m' to import the pool anyway:\n")); 15664b964adaSGeorge Wilson print_vdev_tree(hdl, NULL, missing, 2); 15674b964adaSGeorge Wilson (void) printf("\n"); 15684b964adaSGeorge Wilson } 15694b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 15704b964adaSGeorge Wilson break; 15714b964adaSGeorge Wilson 15724b964adaSGeorge Wilson case EEXIST: 15734b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 15744b964adaSGeorge Wilson break; 15754b964adaSGeorge Wilson 1576fa9e4066Sahrens default: 15774b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 1578468c413aSTim Haley zpool_explain_recover(hdl, 15794b964adaSGeorge Wilson newname ? origname : thename, -error, nv); 1580468c413aSTim Haley break; 1581fa9e4066Sahrens } 1582fa9e4066Sahrens 15834b964adaSGeorge Wilson nvlist_free(nv); 1584fa9e4066Sahrens ret = -1; 1585fa9e4066Sahrens } else { 1586fa9e4066Sahrens zpool_handle_t *zhp; 1587ecd6cf80Smarks 1588fa9e4066Sahrens /* 1589fa9e4066Sahrens * This should never fail, but play it safe anyway. 1590fa9e4066Sahrens */ 1591681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0) 159294de1d4cSeschrock ret = -1; 1593681d9761SEric Taylor else if (zhp != NULL) 1594fa9e4066Sahrens zpool_close(zhp); 1595468c413aSTim Haley if (policy.zrp_request & 1596468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1597468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 15984b964adaSGeorge Wilson ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 1599468c413aSTim Haley } 16004b964adaSGeorge Wilson nvlist_free(nv); 1601468c413aSTim Haley return (0); 1602fa9e4066Sahrens } 1603fa9e4066Sahrens 1604e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1605351420b3Slling nvlist_free(props); 1606351420b3Slling 1607fa9e4066Sahrens return (ret); 1608fa9e4066Sahrens } 1609fa9e4066Sahrens 1610fa9e4066Sahrens /* 16113f9d6ad7SLin Ling * Scan the pool. 1612fa9e4066Sahrens */ 1613fa9e4066Sahrens int 16143f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1615fa9e4066Sahrens { 1616fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1617fa9e4066Sahrens char msg[1024]; 161899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1619fa9e4066Sahrens 1620fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 16213f9d6ad7SLin Ling zc.zc_cookie = func; 1622fa9e4066Sahrens 1623cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 16243f9d6ad7SLin Ling (errno == ENOENT && func != POOL_SCAN_NONE)) 1625fa9e4066Sahrens return (0); 1626fa9e4066Sahrens 16273f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) { 16283f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 16293f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 16303f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) { 16313f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 16323f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 16333f9d6ad7SLin Ling zc.zc_name); 16343f9d6ad7SLin Ling } else { 16353f9d6ad7SLin Ling assert(!"unexpected result"); 16363f9d6ad7SLin Ling } 1637fa9e4066Sahrens 16383f9d6ad7SLin Ling if (errno == EBUSY) { 16393f9d6ad7SLin Ling nvlist_t *nvroot; 16403f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 16413f9d6ad7SLin Ling uint_t psc; 16423f9d6ad7SLin Ling 16433f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config, 16443f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 16453f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 16463f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 16473f9d6ad7SLin Ling if (ps && ps->pss_func == POOL_SCAN_SCRUB) 16483f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 16493f9d6ad7SLin Ling else 16503f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg)); 16513f9d6ad7SLin Ling } else if (errno == ENOENT) { 16523f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 16533f9d6ad7SLin Ling } else { 165499653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 16553f9d6ad7SLin Ling } 1656fa9e4066Sahrens } 1657fa9e4066Sahrens 16583fdda499SJohn Harres /* 16593fdda499SJohn Harres * This provides a very minimal check whether a given string is likely a 16603fdda499SJohn Harres * c#t#d# style string. Users of this are expected to do their own 16613fdda499SJohn Harres * verification of the s# part. 16623fdda499SJohn Harres */ 16633fdda499SJohn Harres #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 16643fdda499SJohn Harres 16653fdda499SJohn Harres /* 16663fdda499SJohn Harres * More elaborate version for ones which may start with "/dev/dsk/" 16673fdda499SJohn Harres * and the like. 16683fdda499SJohn Harres */ 16693fdda499SJohn Harres static int 16703fdda499SJohn Harres ctd_check_path(char *str) { 16713fdda499SJohn Harres /* 16723fdda499SJohn Harres * If it starts with a slash, check the last component. 16733fdda499SJohn Harres */ 16743fdda499SJohn Harres if (str && str[0] == '/') { 16753fdda499SJohn Harres char *tmp = strrchr(str, '/'); 16763fdda499SJohn Harres 16773fdda499SJohn Harres /* 16783fdda499SJohn Harres * If it ends in "/old", check the second-to-last 16793fdda499SJohn Harres * component of the string instead. 16803fdda499SJohn Harres */ 16813fdda499SJohn Harres if (tmp != str && strcmp(tmp, "/old") == 0) { 16823fdda499SJohn Harres for (tmp--; *tmp != '/'; tmp--) 16833fdda499SJohn Harres ; 16843fdda499SJohn Harres } 16853fdda499SJohn Harres str = tmp + 1; 16863fdda499SJohn Harres } 16873fdda499SJohn Harres return (CTD_CHECK(str)); 16883fdda499SJohn Harres } 16893fdda499SJohn Harres 1690a43d325bSek /* 1691573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the 1692573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device. 1693a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1694a43d325bSek * spare; but FALSE if its an INUSE spare. 1695a43d325bSek */ 169699653d4eSeschrock static nvlist_t * 1697573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 1698573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log) 1699ea8dc4b6Seschrock { 1700ea8dc4b6Seschrock uint_t c, children; 1701ea8dc4b6Seschrock nvlist_t **child; 170299653d4eSeschrock nvlist_t *ret; 1703ee0eb9f2SEric Schrock uint64_t is_log; 1704573ca77eSGeorge Wilson char *srchkey; 1705573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL); 1706573ca77eSGeorge Wilson 1707573ca77eSGeorge Wilson /* Nothing to look for */ 1708573ca77eSGeorge Wilson if (search == NULL || pair == NULL) 1709573ca77eSGeorge Wilson return (NULL); 1710ea8dc4b6Seschrock 1711573ca77eSGeorge Wilson /* Obtain the key we will use to search */ 1712573ca77eSGeorge Wilson srchkey = nvpair_name(pair); 1713573ca77eSGeorge Wilson 1714573ca77eSGeorge Wilson switch (nvpair_type(pair)) { 1715cb04b873SMark J Musante case DATA_TYPE_UINT64: 1716573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 1717cb04b873SMark J Musante uint64_t srchval, theguid; 1718cb04b873SMark J Musante 1719cb04b873SMark J Musante verify(nvpair_value_uint64(pair, &srchval) == 0); 1720cb04b873SMark J Musante verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1721cb04b873SMark J Musante &theguid) == 0); 1722cb04b873SMark J Musante if (theguid == srchval) 1723cb04b873SMark J Musante return (nv); 1724573ca77eSGeorge Wilson } 1725573ca77eSGeorge Wilson break; 1726573ca77eSGeorge Wilson 1727573ca77eSGeorge Wilson case DATA_TYPE_STRING: { 1728573ca77eSGeorge Wilson char *srchval, *val; 1729573ca77eSGeorge Wilson 1730573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0); 1731573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0) 1732573ca77eSGeorge Wilson break; 1733ea8dc4b6Seschrock 1734ea8dc4b6Seschrock /* 17353fdda499SJohn Harres * Search for the requested value. Special cases: 17363fdda499SJohn Harres * 17373fdda499SJohn Harres * - ZPOOL_CONFIG_PATH for whole disk entries. These end in 17383fdda499SJohn Harres * "s0" or "s0/old". The "s0" part is hidden from the user, 17393fdda499SJohn Harres * but included in the string, so this matches around it. 17403fdda499SJohn Harres * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 17413fdda499SJohn Harres * 174288ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares. 1743ea8dc4b6Seschrock */ 17443fdda499SJohn Harres if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 17453fdda499SJohn Harres ctd_check_path(val)) { 1746573ca77eSGeorge Wilson uint64_t wholedisk = 0; 1747573ca77eSGeorge Wilson 1748573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1749573ca77eSGeorge Wilson &wholedisk); 1750573ca77eSGeorge Wilson if (wholedisk) { 17513fdda499SJohn Harres int slen = strlen(srchval); 17523fdda499SJohn Harres int vlen = strlen(val); 17533fdda499SJohn Harres 17543fdda499SJohn Harres if (slen != vlen - 2) 17553fdda499SJohn Harres break; 17563fdda499SJohn Harres 17573fdda499SJohn Harres /* 17583fdda499SJohn Harres * make_leaf_vdev() should only set 17593fdda499SJohn Harres * wholedisk for ZPOOL_CONFIG_PATHs which 17603fdda499SJohn Harres * will include "/dev/dsk/", giving plenty of 17613fdda499SJohn Harres * room for the indices used next. 17623fdda499SJohn Harres */ 17633fdda499SJohn Harres ASSERT(vlen >= 6); 17643fdda499SJohn Harres 17653fdda499SJohn Harres /* 17663fdda499SJohn Harres * strings identical except trailing "s0" 17673fdda499SJohn Harres */ 17683fdda499SJohn Harres if (strcmp(&val[vlen - 2], "s0") == 0 && 17693fdda499SJohn Harres strncmp(srchval, val, slen) == 0) 17703fdda499SJohn Harres return (nv); 17713fdda499SJohn Harres 1772573ca77eSGeorge Wilson /* 17733fdda499SJohn Harres * strings identical except trailing "s0/old" 1774573ca77eSGeorge Wilson */ 17753fdda499SJohn Harres if (strcmp(&val[vlen - 6], "s0/old") == 0 && 17763fdda499SJohn Harres strcmp(&srchval[slen - 4], "/old") == 0 && 17773fdda499SJohn Harres strncmp(srchval, val, slen - 4) == 0) 1778573ca77eSGeorge Wilson return (nv); 17793fdda499SJohn Harres 1780573ca77eSGeorge Wilson break; 1781573ca77eSGeorge Wilson } 178288ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 178388ecc943SGeorge Wilson char *type, *idx, *end, *p; 178488ecc943SGeorge Wilson uint64_t id, vdev_id; 178588ecc943SGeorge Wilson 178688ecc943SGeorge Wilson /* 178788ecc943SGeorge Wilson * Determine our vdev type, keeping in mind 178888ecc943SGeorge Wilson * that the srchval is composed of a type and 178988ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4). 179088ecc943SGeorge Wilson */ 179188ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL) 179288ecc943SGeorge Wilson return (NULL); 179388ecc943SGeorge Wilson 179488ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) { 179588ecc943SGeorge Wilson free(type); 179688ecc943SGeorge Wilson break; 179788ecc943SGeorge Wilson } 179888ecc943SGeorge Wilson idx = p + 1; 179988ecc943SGeorge Wilson *p = '\0'; 180088ecc943SGeorge Wilson 180188ecc943SGeorge Wilson /* 180288ecc943SGeorge Wilson * If the types don't match then keep looking. 180388ecc943SGeorge Wilson */ 180488ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) { 180588ecc943SGeorge Wilson free(type); 180688ecc943SGeorge Wilson break; 180788ecc943SGeorge Wilson } 180888ecc943SGeorge Wilson 180988ecc943SGeorge Wilson verify(strncmp(type, VDEV_TYPE_RAIDZ, 181088ecc943SGeorge Wilson strlen(VDEV_TYPE_RAIDZ)) == 0 || 181188ecc943SGeorge Wilson strncmp(type, VDEV_TYPE_MIRROR, 181288ecc943SGeorge Wilson strlen(VDEV_TYPE_MIRROR)) == 0); 181388ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 181488ecc943SGeorge Wilson &id) == 0); 181588ecc943SGeorge Wilson 181688ecc943SGeorge Wilson errno = 0; 181788ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10); 181888ecc943SGeorge Wilson 181988ecc943SGeorge Wilson free(type); 182088ecc943SGeorge Wilson if (errno != 0) 182188ecc943SGeorge Wilson return (NULL); 182288ecc943SGeorge Wilson 182388ecc943SGeorge Wilson /* 182488ecc943SGeorge Wilson * Now verify that we have the correct vdev id. 182588ecc943SGeorge Wilson */ 182688ecc943SGeorge Wilson if (vdev_id == id) 182788ecc943SGeorge Wilson return (nv); 1828ea8dc4b6Seschrock } 1829573ca77eSGeorge Wilson 1830573ca77eSGeorge Wilson /* 1831573ca77eSGeorge Wilson * Common case 1832573ca77eSGeorge Wilson */ 1833573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0) 1834573ca77eSGeorge Wilson return (nv); 1835573ca77eSGeorge Wilson break; 1836573ca77eSGeorge Wilson } 1837573ca77eSGeorge Wilson 1838573ca77eSGeorge Wilson default: 1839573ca77eSGeorge Wilson break; 1840ea8dc4b6Seschrock } 1841ea8dc4b6Seschrock 1842ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1843ea8dc4b6Seschrock &child, &children) != 0) 184499653d4eSeschrock return (NULL); 1845ea8dc4b6Seschrock 1846ee0eb9f2SEric Schrock for (c = 0; c < children; c++) { 1847573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 1848ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 1849ee0eb9f2SEric Schrock /* 1850ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel 1851ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the 1852ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where 1853ee0eb9f2SEric Schrock * 'log' is non-NULL). 1854ee0eb9f2SEric Schrock */ 1855ee0eb9f2SEric Schrock if (log != NULL && 1856ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c], 1857ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 1858ee0eb9f2SEric Schrock is_log) { 1859ee0eb9f2SEric Schrock *log = B_TRUE; 1860ee0eb9f2SEric Schrock } 1861ea8dc4b6Seschrock return (ret); 1862ee0eb9f2SEric Schrock } 1863ee0eb9f2SEric Schrock } 1864ea8dc4b6Seschrock 186599653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 186699653d4eSeschrock &child, &children) == 0) { 186799653d4eSeschrock for (c = 0; c < children; c++) { 1868573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 1869ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 1870a43d325bSek *avail_spare = B_TRUE; 187199653d4eSeschrock return (ret); 187299653d4eSeschrock } 187399653d4eSeschrock } 187499653d4eSeschrock } 187599653d4eSeschrock 1876fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1877fa94a07fSbrendan &child, &children) == 0) { 1878fa94a07fSbrendan for (c = 0; c < children; c++) { 1879573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 1880ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 1881fa94a07fSbrendan *l2cache = B_TRUE; 1882fa94a07fSbrendan return (ret); 1883fa94a07fSbrendan } 1884fa94a07fSbrendan } 1885fa94a07fSbrendan } 1886fa94a07fSbrendan 188799653d4eSeschrock return (NULL); 1888ea8dc4b6Seschrock } 1889ea8dc4b6Seschrock 1890573ca77eSGeorge Wilson /* 1891573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the 1892573ca77eSGeorge Wilson * associated vdev. 1893573ca77eSGeorge Wilson */ 1894573ca77eSGeorge Wilson nvlist_t * 1895573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 1896573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 1897573ca77eSGeorge Wilson { 1898573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret; 1899573ca77eSGeorge Wilson 1900573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1901573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 1902573ca77eSGeorge Wilson 1903573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1904573ca77eSGeorge Wilson &nvroot) == 0); 1905573ca77eSGeorge Wilson 1906573ca77eSGeorge Wilson *avail_spare = B_FALSE; 1907cb04b873SMark J Musante *l2cache = B_FALSE; 1908daeb70e5SMark J Musante if (log != NULL) 1909daeb70e5SMark J Musante *log = B_FALSE; 1910573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 1911573ca77eSGeorge Wilson nvlist_free(search); 1912573ca77eSGeorge Wilson 1913573ca77eSGeorge Wilson return (ret); 1914573ca77eSGeorge Wilson } 1915573ca77eSGeorge Wilson 191688ecc943SGeorge Wilson /* 191788ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 191888ecc943SGeorge Wilson */ 191988ecc943SGeorge Wilson boolean_t 192088ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name) 192188ecc943SGeorge Wilson { 192288ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 192388ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 192488ecc943SGeorge Wilson return (B_TRUE); 192588ecc943SGeorge Wilson return (B_FALSE); 192688ecc943SGeorge Wilson } 192788ecc943SGeorge Wilson 192899653d4eSeschrock nvlist_t * 1929fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 1930ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log) 1931ea8dc4b6Seschrock { 1932ea8dc4b6Seschrock char buf[MAXPATHLEN]; 1933ea8dc4b6Seschrock char *end; 1934573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret; 1935ea8dc4b6Seschrock uint64_t guid; 1936ea8dc4b6Seschrock 1937573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1938573ca77eSGeorge Wilson 19390917b783Seschrock guid = strtoull(path, &end, 10); 1940ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 1941573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 194288ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) { 194388ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 1944ea8dc4b6Seschrock } else if (path[0] != '/') { 1945ea8dc4b6Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 1946573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 1947ea8dc4b6Seschrock } else { 1948573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 1949ea8dc4b6Seschrock } 1950ea8dc4b6Seschrock 1951ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 1952ea8dc4b6Seschrock &nvroot) == 0); 1953ea8dc4b6Seschrock 1954a43d325bSek *avail_spare = B_FALSE; 1955fa94a07fSbrendan *l2cache = B_FALSE; 1956ee0eb9f2SEric Schrock if (log != NULL) 1957ee0eb9f2SEric Schrock *log = B_FALSE; 1958573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 1959573ca77eSGeorge Wilson nvlist_free(search); 1960573ca77eSGeorge Wilson 1961573ca77eSGeorge Wilson return (ret); 1962a43d325bSek } 1963a43d325bSek 196419397407SSherry Moore static int 196519397407SSherry Moore vdev_online(nvlist_t *nv) 196619397407SSherry Moore { 196719397407SSherry Moore uint64_t ival; 196819397407SSherry Moore 196919397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 197019397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 197119397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 197219397407SSherry Moore return (0); 197319397407SSherry Moore 197419397407SSherry Moore return (1); 197519397407SSherry Moore } 197619397407SSherry Moore 197719397407SSherry Moore /* 197821ecdf64SLin Ling * Helper function for zpool_get_physpaths(). 197919397407SSherry Moore */ 1980753a6d45SSherry Moore static int 198121ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 1982753a6d45SSherry Moore size_t *bytes_written) 198319397407SSherry Moore { 1984753a6d45SSherry Moore size_t bytes_left, pos, rsz; 1985753a6d45SSherry Moore char *tmppath; 1986753a6d45SSherry Moore const char *format; 1987753a6d45SSherry Moore 1988753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 1989753a6d45SSherry Moore &tmppath) != 0) 1990753a6d45SSherry Moore return (EZFS_NODEVICE); 1991753a6d45SSherry Moore 1992753a6d45SSherry Moore pos = *bytes_written; 1993753a6d45SSherry Moore bytes_left = physpath_size - pos; 1994753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s"; 1995753a6d45SSherry Moore 1996753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 1997753a6d45SSherry Moore *bytes_written += rsz; 1998753a6d45SSherry Moore 1999753a6d45SSherry Moore if (rsz >= bytes_left) { 2000753a6d45SSherry Moore /* if physpath was not copied properly, clear it */ 2001753a6d45SSherry Moore if (bytes_left != 0) { 2002753a6d45SSherry Moore physpath[pos] = 0; 2003753a6d45SSherry Moore } 2004753a6d45SSherry Moore return (EZFS_NOSPC); 2005753a6d45SSherry Moore } 2006753a6d45SSherry Moore return (0); 2007753a6d45SSherry Moore } 2008753a6d45SSherry Moore 200921ecdf64SLin Ling static int 201021ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 201121ecdf64SLin Ling size_t *rsz, boolean_t is_spare) 201221ecdf64SLin Ling { 201321ecdf64SLin Ling char *type; 201421ecdf64SLin Ling int ret; 201521ecdf64SLin Ling 201621ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 201721ecdf64SLin Ling return (EZFS_INVALCONFIG); 201821ecdf64SLin Ling 201921ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) { 202021ecdf64SLin Ling /* 202121ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 202221ecdf64SLin Ling * For a spare vdev, we only want to boot from the active 202321ecdf64SLin Ling * spare device. 202421ecdf64SLin Ling */ 202521ecdf64SLin Ling if (is_spare) { 202621ecdf64SLin Ling uint64_t spare = 0; 202721ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 202821ecdf64SLin Ling &spare); 202921ecdf64SLin Ling if (!spare) 203021ecdf64SLin Ling return (EZFS_INVALCONFIG); 203121ecdf64SLin Ling } 203221ecdf64SLin Ling 203321ecdf64SLin Ling if (vdev_online(nv)) { 203421ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath, 203521ecdf64SLin Ling phypath_size, rsz)) != 0) 203621ecdf64SLin Ling return (ret); 203721ecdf64SLin Ling } 203821ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 203921ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 || 204021ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 204121ecdf64SLin Ling nvlist_t **child; 204221ecdf64SLin Ling uint_t count; 204321ecdf64SLin Ling int i, ret; 204421ecdf64SLin Ling 204521ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv, 204621ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 204721ecdf64SLin Ling return (EZFS_INVALCONFIG); 204821ecdf64SLin Ling 204921ecdf64SLin Ling for (i = 0; i < count; i++) { 205021ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath, 205121ecdf64SLin Ling phypath_size, rsz, is_spare); 205221ecdf64SLin Ling if (ret == EZFS_NOSPC) 205321ecdf64SLin Ling return (ret); 205421ecdf64SLin Ling } 205521ecdf64SLin Ling } 205621ecdf64SLin Ling 205721ecdf64SLin Ling return (EZFS_POOL_INVALARG); 205821ecdf64SLin Ling } 205921ecdf64SLin Ling 2060753a6d45SSherry Moore /* 2061753a6d45SSherry Moore * Get phys_path for a root pool config. 2062753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2063753a6d45SSherry Moore */ 2064753a6d45SSherry Moore static int 2065753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2066753a6d45SSherry Moore { 2067753a6d45SSherry Moore size_t rsz; 206819397407SSherry Moore nvlist_t *vdev_root; 206919397407SSherry Moore nvlist_t **child; 207019397407SSherry Moore uint_t count; 2071753a6d45SSherry Moore char *type; 2072753a6d45SSherry Moore 2073753a6d45SSherry Moore rsz = 0; 2074753a6d45SSherry Moore 2075753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2076753a6d45SSherry Moore &vdev_root) != 0) 2077753a6d45SSherry Moore return (EZFS_INVALCONFIG); 2078753a6d45SSherry Moore 2079753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2080753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2081753a6d45SSherry Moore &child, &count) != 0) 2082753a6d45SSherry Moore return (EZFS_INVALCONFIG); 208319397407SSherry Moore 208419397407SSherry Moore /* 2085753a6d45SSherry Moore * root pool can not have EFI labeled disks and can only have 2086753a6d45SSherry Moore * a single top-level vdev. 208719397407SSherry Moore */ 2088753a6d45SSherry Moore if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 || 2089753a6d45SSherry Moore pool_uses_efi(vdev_root)) 2090753a6d45SSherry Moore return (EZFS_POOL_INVALARG); 209119397407SSherry Moore 209221ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 209321ecdf64SLin Ling B_FALSE); 209419397407SSherry Moore 2095753a6d45SSherry Moore /* No online devices */ 2096753a6d45SSherry Moore if (rsz == 0) 2097753a6d45SSherry Moore return (EZFS_NODEVICE); 2098753a6d45SSherry Moore 209919397407SSherry Moore return (0); 210019397407SSherry Moore } 210119397407SSherry Moore 2102753a6d45SSherry Moore /* 2103753a6d45SSherry Moore * Get phys_path for a root pool 2104753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2105753a6d45SSherry Moore */ 2106753a6d45SSherry Moore int 2107753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2108753a6d45SSherry Moore { 2109753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2110753a6d45SSherry Moore phypath_size)); 2111753a6d45SSherry Moore } 2112753a6d45SSherry Moore 2113573ca77eSGeorge Wilson /* 2114573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel 2115573ca77eSGeorge Wilson * the disk to use the new unallocated space. 2116573ca77eSGeorge Wilson */ 2117573ca77eSGeorge Wilson static int 2118573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2119573ca77eSGeorge Wilson { 2120573ca77eSGeorge Wilson char path[MAXPATHLEN]; 2121573ca77eSGeorge Wilson char errbuf[1024]; 2122573ca77eSGeorge Wilson int fd, error; 2123573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int); 2124573ca77eSGeorge Wilson 2125573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2126573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL) 2127573ca77eSGeorge Wilson return (-1); 2128573ca77eSGeorge Wilson 2129573ca77eSGeorge Wilson (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name); 2130573ca77eSGeorge Wilson 2131573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2132573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2133573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name); 2134573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2135573ca77eSGeorge Wilson } 2136573ca77eSGeorge Wilson 2137573ca77eSGeorge Wilson /* 2138573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device 2139573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply 2140573ca77eSGeorge Wilson * ignore that error and continue on. 2141573ca77eSGeorge Wilson */ 2142573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd); 2143573ca77eSGeorge Wilson (void) close(fd); 2144573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) { 2145573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2146573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name); 2147573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2148573ca77eSGeorge Wilson } 2149573ca77eSGeorge Wilson return (0); 2150573ca77eSGeorge Wilson } 2151573ca77eSGeorge Wilson 2152fa9e4066Sahrens /* 21533d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 21543d7072f8Seschrock * ZFS_ONLINE_* flags. 2155fa9e4066Sahrens */ 2156fa9e4066Sahrens int 21573d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 21583d7072f8Seschrock vdev_state_t *newstate) 2159fa9e4066Sahrens { 2160fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2161fa9e4066Sahrens char msg[1024]; 216299653d4eSeschrock nvlist_t *tgt; 2163573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog; 216499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2165fa9e4066Sahrens 2166573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) { 2167573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2168573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2169573ca77eSGeorge Wilson } else { 2170573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2171573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2172573ca77eSGeorge Wilson } 2173ea8dc4b6Seschrock 2174fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2175ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2176573ca77eSGeorge Wilson &islog)) == NULL) 217799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2178fa9e4066Sahrens 217999653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2180fa9e4066Sahrens 2181069f55e2SEric Schrock if (avail_spare) 2182a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2183a43d325bSek 2184573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND || 2185573ca77eSGeorge Wilson zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 2186573ca77eSGeorge Wilson char *pathname = NULL; 2187573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2188573ca77eSGeorge Wilson 2189573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2190573ca77eSGeorge Wilson &wholedisk); 2191573ca77eSGeorge Wilson verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 2192573ca77eSGeorge Wilson &pathname) == 0); 2193573ca77eSGeorge Wilson 2194573ca77eSGeorge Wilson /* 2195573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion. 2196573ca77eSGeorge Wilson */ 2197573ca77eSGeorge Wilson if (l2cache) { 2198573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2199573ca77eSGeorge Wilson "cannot expand cache devices")); 2200573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2201573ca77eSGeorge Wilson } 2202573ca77eSGeorge Wilson 2203573ca77eSGeorge Wilson if (wholedisk) { 2204573ca77eSGeorge Wilson pathname += strlen(DISK_ROOT) + 1; 2205cb04b873SMark J Musante (void) zpool_relabel_disk(hdl, pathname); 2206573ca77eSGeorge Wilson } 2207573ca77eSGeorge Wilson } 2208573ca77eSGeorge Wilson 22093d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 22103d7072f8Seschrock zc.zc_obj = flags; 2211fa9e4066Sahrens 2212cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 22131195e687SMark J Musante if (errno == EINVAL) { 22141195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 22151195e687SMark J Musante "from this pool into a new one. Use '%s' " 22161195e687SMark J Musante "instead"), "zpool detach"); 22171195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 22181195e687SMark J Musante } 22193d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 22201195e687SMark J Musante } 22213d7072f8Seschrock 22223d7072f8Seschrock *newstate = zc.zc_cookie; 22233d7072f8Seschrock return (0); 2224fa9e4066Sahrens } 2225fa9e4066Sahrens 2226fa9e4066Sahrens /* 2227fa9e4066Sahrens * Take the specified vdev offline 2228fa9e4066Sahrens */ 2229fa9e4066Sahrens int 22303d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2231fa9e4066Sahrens { 2232fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2233fa9e4066Sahrens char msg[1024]; 223499653d4eSeschrock nvlist_t *tgt; 2235fa94a07fSbrendan boolean_t avail_spare, l2cache; 223699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2237fa9e4066Sahrens 2238ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2239ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2240ea8dc4b6Seschrock 2241fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2242ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2243ee0eb9f2SEric Schrock NULL)) == NULL) 224499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 224599653d4eSeschrock 224699653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2247fa9e4066Sahrens 2248069f55e2SEric Schrock if (avail_spare) 2249a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2250a43d325bSek 22513d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 22523d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 22533d7072f8Seschrock 2254cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 22553d7072f8Seschrock return (0); 22563d7072f8Seschrock 22573d7072f8Seschrock switch (errno) { 22583d7072f8Seschrock case EBUSY: 22593d7072f8Seschrock 22603d7072f8Seschrock /* 22613d7072f8Seschrock * There are no other replicas of this device. 22623d7072f8Seschrock */ 22633d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 22643d7072f8Seschrock 2265e6ca193dSGeorge Wilson case EEXIST: 2266e6ca193dSGeorge Wilson /* 2267e6ca193dSGeorge Wilson * The log device has unplayed logs 2268e6ca193dSGeorge Wilson */ 2269e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2270e6ca193dSGeorge Wilson 22713d7072f8Seschrock default: 22723d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 22733d7072f8Seschrock } 22743d7072f8Seschrock } 22753d7072f8Seschrock 22763d7072f8Seschrock /* 22773d7072f8Seschrock * Mark the given vdev faulted. 22783d7072f8Seschrock */ 22793d7072f8Seschrock int 2280069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 22813d7072f8Seschrock { 22823d7072f8Seschrock zfs_cmd_t zc = { 0 }; 22833d7072f8Seschrock char msg[1024]; 22843d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 22853d7072f8Seschrock 22863d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 22873d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2288441d80aaSlling 22893d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 22903d7072f8Seschrock zc.zc_guid = guid; 22913d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 2292069f55e2SEric Schrock zc.zc_obj = aux; 22933d7072f8Seschrock 2294cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2295fa9e4066Sahrens return (0); 2296fa9e4066Sahrens 2297fa9e4066Sahrens switch (errno) { 229899653d4eSeschrock case EBUSY: 2299fa9e4066Sahrens 2300fa9e4066Sahrens /* 2301fa9e4066Sahrens * There are no other replicas of this device. 2302fa9e4066Sahrens */ 230399653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2304fa9e4066Sahrens 230599653d4eSeschrock default: 230699653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2307fa9e4066Sahrens } 23083d7072f8Seschrock 23093d7072f8Seschrock } 23103d7072f8Seschrock 23113d7072f8Seschrock /* 23123d7072f8Seschrock * Mark the given vdev degraded. 23133d7072f8Seschrock */ 23143d7072f8Seschrock int 2315069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 23163d7072f8Seschrock { 23173d7072f8Seschrock zfs_cmd_t zc = { 0 }; 23183d7072f8Seschrock char msg[1024]; 23193d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 23203d7072f8Seschrock 23213d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 23223d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 23233d7072f8Seschrock 23243d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 23253d7072f8Seschrock zc.zc_guid = guid; 23263d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 2327069f55e2SEric Schrock zc.zc_obj = aux; 23283d7072f8Seschrock 2329cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 23303d7072f8Seschrock return (0); 23313d7072f8Seschrock 23323d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 233399653d4eSeschrock } 233499653d4eSeschrock 233599653d4eSeschrock /* 233699653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 233799653d4eSeschrock * a hot spare. 233899653d4eSeschrock */ 233999653d4eSeschrock static boolean_t 234099653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 234199653d4eSeschrock { 234299653d4eSeschrock nvlist_t **child; 234399653d4eSeschrock uint_t c, children; 234499653d4eSeschrock char *type; 234599653d4eSeschrock 234699653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 234799653d4eSeschrock &children) == 0) { 234899653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 234999653d4eSeschrock &type) == 0); 235099653d4eSeschrock 235199653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 235299653d4eSeschrock children == 2 && child[which] == tgt) 235399653d4eSeschrock return (B_TRUE); 235499653d4eSeschrock 235599653d4eSeschrock for (c = 0; c < children; c++) 235699653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 235799653d4eSeschrock return (B_TRUE); 235899653d4eSeschrock } 235999653d4eSeschrock 236099653d4eSeschrock return (B_FALSE); 2361fa9e4066Sahrens } 2362fa9e4066Sahrens 2363fa9e4066Sahrens /* 2364fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 23658654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2366fa9e4066Sahrens */ 2367fa9e4066Sahrens int 2368fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2369fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2370fa9e4066Sahrens { 2371fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2372fa9e4066Sahrens char msg[1024]; 2373fa9e4066Sahrens int ret; 237499653d4eSeschrock nvlist_t *tgt; 2375ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog; 2376ee0eb9f2SEric Schrock uint64_t val; 2377cb04b873SMark J Musante char *newname; 237899653d4eSeschrock nvlist_t **child; 237999653d4eSeschrock uint_t children; 238099653d4eSeschrock nvlist_t *config_root; 238199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2382*4263d13fSGeorge Wilson boolean_t rootpool = zpool_is_bootable(zhp); 2383fa9e4066Sahrens 2384ea8dc4b6Seschrock if (replacing) 2385ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2386ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 2387ea8dc4b6Seschrock else 2388ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2389ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 2390ea8dc4b6Seschrock 2391b5b76fecSGeorge Wilson /* 2392b5b76fecSGeorge Wilson * If this is a root pool, make sure that we're not attaching an 2393b5b76fecSGeorge Wilson * EFI labeled device. 2394b5b76fecSGeorge Wilson */ 2395b5b76fecSGeorge Wilson if (rootpool && pool_uses_efi(nvroot)) { 2396b5b76fecSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2397b5b76fecSGeorge Wilson "EFI labeled devices are not supported on root pools.")); 2398b5b76fecSGeorge Wilson return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 2399b5b76fecSGeorge Wilson } 2400b5b76fecSGeorge Wilson 2401fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2402ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 2403ee0eb9f2SEric Schrock &islog)) == 0) 240499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 240599653d4eSeschrock 2406a43d325bSek if (avail_spare) 240799653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 240899653d4eSeschrock 2409fa94a07fSbrendan if (l2cache) 2410fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2411fa94a07fSbrendan 241299653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2413fa9e4066Sahrens zc.zc_cookie = replacing; 2414fa9e4066Sahrens 241599653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 241699653d4eSeschrock &child, &children) != 0 || children != 1) { 241799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 241899653d4eSeschrock "new device must be a single disk")); 241999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 242099653d4eSeschrock } 242199653d4eSeschrock 242299653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 242399653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 242499653d4eSeschrock 242588ecc943SGeorge Wilson if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 24260430f8daSeschrock return (-1); 24270430f8daSeschrock 242899653d4eSeschrock /* 242999653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 243099653d4eSeschrock * replace it with another hot spare. 243199653d4eSeschrock */ 243299653d4eSeschrock if (replacing && 243399653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2434ee0eb9f2SEric Schrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2435ee0eb9f2SEric Schrock NULL) == NULL || !avail_spare) && 2436ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) { 243799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 243899653d4eSeschrock "can only be replaced by another hot spare")); 24390430f8daSeschrock free(newname); 244099653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 244199653d4eSeschrock } 244299653d4eSeschrock 24430430f8daSeschrock free(newname); 24440430f8daSeschrock 2445990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 244699653d4eSeschrock return (-1); 2447fa9e4066Sahrens 2448cb04b873SMark J Musante ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2449fa9e4066Sahrens 2450e9dbad6fSeschrock zcmd_free_nvlists(&zc); 2451fa9e4066Sahrens 2452b5b76fecSGeorge Wilson if (ret == 0) { 2453b5b76fecSGeorge Wilson if (rootpool) { 245421ecdf64SLin Ling /* 245521ecdf64SLin Ling * XXX need a better way to prevent user from 245621ecdf64SLin Ling * booting up a half-baked vdev. 245721ecdf64SLin Ling */ 245821ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 245921ecdf64SLin Ling "sure to wait until resilver is done " 246021ecdf64SLin Ling "before rebooting.\n")); 2461b5b76fecSGeorge Wilson } 2462fa9e4066Sahrens return (0); 2463b5b76fecSGeorge Wilson } 2464fa9e4066Sahrens 2465fa9e4066Sahrens switch (errno) { 2466ea8dc4b6Seschrock case ENOTSUP: 2467fa9e4066Sahrens /* 2468fa9e4066Sahrens * Can't attach to or replace this type of vdev. 2469fa9e4066Sahrens */ 24708654d025Sperrin if (replacing) { 2471cb04b873SMark J Musante uint64_t version = zpool_get_prop_int(zhp, 2472cb04b873SMark J Musante ZPOOL_PROP_VERSION, NULL); 2473cb04b873SMark J Musante 2474ee0eb9f2SEric Schrock if (islog) 24758654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24768654d025Sperrin "cannot replace a log with a spare")); 2477cb04b873SMark J Musante else if (version >= SPA_VERSION_MULTI_REPLACE) 2478cb04b873SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2479cb04b873SMark J Musante "already in replacing/spare config; wait " 2480cb04b873SMark J Musante "for completion or use 'zpool detach'")); 24818654d025Sperrin else 24828654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24838654d025Sperrin "cannot replace a replacing device")); 24848654d025Sperrin } else { 248599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 248699653d4eSeschrock "can only attach to mirrors and top-level " 248799653d4eSeschrock "disks")); 24888654d025Sperrin } 248999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2490fa9e4066Sahrens break; 2491fa9e4066Sahrens 2492ea8dc4b6Seschrock case EINVAL: 2493fa9e4066Sahrens /* 2494fa9e4066Sahrens * The new device must be a single disk. 2495fa9e4066Sahrens */ 249699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 249799653d4eSeschrock "new device must be a single disk")); 249899653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2499fa9e4066Sahrens break; 2500fa9e4066Sahrens 2501ea8dc4b6Seschrock case EBUSY: 250299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 250399653d4eSeschrock new_disk); 250499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2505fa9e4066Sahrens break; 2506fa9e4066Sahrens 2507ea8dc4b6Seschrock case EOVERFLOW: 2508fa9e4066Sahrens /* 2509fa9e4066Sahrens * The new device is too small. 2510fa9e4066Sahrens */ 251199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 251299653d4eSeschrock "device is too small")); 251399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2514fa9e4066Sahrens break; 2515fa9e4066Sahrens 2516ea8dc4b6Seschrock case EDOM: 2517fa9e4066Sahrens /* 2518fa9e4066Sahrens * The new device has a different alignment requirement. 2519fa9e4066Sahrens */ 252099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 252199653d4eSeschrock "devices have different sector alignment")); 252299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2523fa9e4066Sahrens break; 2524fa9e4066Sahrens 2525ea8dc4b6Seschrock case ENAMETOOLONG: 2526fa9e4066Sahrens /* 2527fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 2528fa9e4066Sahrens */ 252999653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2530fa9e4066Sahrens break; 2531fa9e4066Sahrens 2532ea8dc4b6Seschrock default: 253399653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2534fa9e4066Sahrens } 2535fa9e4066Sahrens 253699653d4eSeschrock return (-1); 2537fa9e4066Sahrens } 2538fa9e4066Sahrens 2539fa9e4066Sahrens /* 2540fa9e4066Sahrens * Detach the specified device. 2541fa9e4066Sahrens */ 2542fa9e4066Sahrens int 2543fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2544fa9e4066Sahrens { 2545fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2546fa9e4066Sahrens char msg[1024]; 254799653d4eSeschrock nvlist_t *tgt; 2548fa94a07fSbrendan boolean_t avail_spare, l2cache; 254999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2550fa9e4066Sahrens 2551ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2552ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2553ea8dc4b6Seschrock 2554fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2555ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2556ee0eb9f2SEric Schrock NULL)) == 0) 255799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2558fa9e4066Sahrens 2559a43d325bSek if (avail_spare) 256099653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 256199653d4eSeschrock 2562fa94a07fSbrendan if (l2cache) 2563fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2564fa94a07fSbrendan 256599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 256699653d4eSeschrock 2567ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2568fa9e4066Sahrens return (0); 2569fa9e4066Sahrens 2570fa9e4066Sahrens switch (errno) { 2571fa9e4066Sahrens 2572ea8dc4b6Seschrock case ENOTSUP: 2573fa9e4066Sahrens /* 2574fa9e4066Sahrens * Can't detach from this type of vdev. 2575fa9e4066Sahrens */ 257699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 257799653d4eSeschrock "applicable to mirror and replacing vdevs")); 2578cb04b873SMark J Musante (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2579fa9e4066Sahrens break; 2580fa9e4066Sahrens 2581ea8dc4b6Seschrock case EBUSY: 2582fa9e4066Sahrens /* 2583fa9e4066Sahrens * There are no other replicas of this device. 2584fa9e4066Sahrens */ 258599653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2586fa9e4066Sahrens break; 2587fa9e4066Sahrens 2588ea8dc4b6Seschrock default: 258999653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2590ea8dc4b6Seschrock } 2591ea8dc4b6Seschrock 259299653d4eSeschrock return (-1); 259399653d4eSeschrock } 259499653d4eSeschrock 25951195e687SMark J Musante /* 25961195e687SMark J Musante * Find a mirror vdev in the source nvlist. 25971195e687SMark J Musante * 25981195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors 25991195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the 26001195e687SMark J Musante * user specified on the command line. We loop over the mchild array to 26011195e687SMark J Musante * see if any entry in the schild array matches. 26021195e687SMark J Musante * 26031195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return 26041195e687SMark J Musante * the index of that entry. Otherwise we return -1. 26051195e687SMark J Musante */ 26061195e687SMark J Musante static int 26071195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 26081195e687SMark J Musante nvlist_t **schild, uint_t schildren) 26091195e687SMark J Musante { 26101195e687SMark J Musante uint_t mc; 26111195e687SMark J Musante 26121195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) { 26131195e687SMark J Musante uint_t sc; 26141195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 26151195e687SMark J Musante mchild[mc], B_FALSE); 26161195e687SMark J Musante 26171195e687SMark J Musante for (sc = 0; sc < schildren; sc++) { 26181195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 26191195e687SMark J Musante schild[sc], B_FALSE); 26201195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0); 26211195e687SMark J Musante 26221195e687SMark J Musante free(spath); 26231195e687SMark J Musante if (result) { 26241195e687SMark J Musante free(mpath); 26251195e687SMark J Musante return (mc); 26261195e687SMark J Musante } 26271195e687SMark J Musante } 26281195e687SMark J Musante 26291195e687SMark J Musante free(mpath); 26301195e687SMark J Musante } 26311195e687SMark J Musante 26321195e687SMark J Musante return (-1); 26331195e687SMark J Musante } 26341195e687SMark J Musante 26351195e687SMark J Musante /* 26361195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist 26371195e687SMark J Musante * is generated and it is the responsibility of the caller to free it. 26381195e687SMark J Musante */ 26391195e687SMark J Musante int 26401195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 26411195e687SMark J Musante nvlist_t *props, splitflags_t flags) 26421195e687SMark J Musante { 26431195e687SMark J Musante zfs_cmd_t zc = { 0 }; 26441195e687SMark J Musante char msg[1024]; 26451195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 26461195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL; 26471195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 26481195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl; 26491195e687SMark J Musante uint64_t vers; 26501195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE; 26511195e687SMark J Musante int retval = 0; 26521195e687SMark J Musante 26531195e687SMark J Musante (void) snprintf(msg, sizeof (msg), 26541195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 26551195e687SMark J Musante 26561195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname)) 26571195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 26581195e687SMark J Musante 26591195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) { 26601195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to " 26611195e687SMark J Musante "retrieve pool configuration\n")); 26621195e687SMark J Musante return (-1); 26631195e687SMark J Musante } 26641195e687SMark J Musante 26651195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 26661195e687SMark J Musante == 0); 26671195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 26681195e687SMark J Musante 26691195e687SMark J Musante if (props) { 2670f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 26711195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 2672f9af39baSGeorge Wilson props, vers, flags, msg)) == NULL) 26731195e687SMark J Musante return (-1); 26741195e687SMark J Musante } 26751195e687SMark J Musante 26761195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 26771195e687SMark J Musante &children) != 0) { 26781195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26791195e687SMark J Musante "Source pool is missing vdev tree")); 26801195e687SMark J Musante if (zc_props) 26811195e687SMark J Musante nvlist_free(zc_props); 26821195e687SMark J Musante return (-1); 26831195e687SMark J Musante } 26841195e687SMark J Musante 26851195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 26861195e687SMark J Musante vcount = 0; 26871195e687SMark J Musante 26881195e687SMark J Musante if (*newroot == NULL || 26891195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 26901195e687SMark J Musante &newchild, &newchildren) != 0) 26911195e687SMark J Musante newchildren = 0; 26921195e687SMark J Musante 26931195e687SMark J Musante for (c = 0; c < children; c++) { 26941195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE; 26951195e687SMark J Musante char *type; 26961195e687SMark J Musante nvlist_t **mchild, *vdev; 26971195e687SMark J Musante uint_t mchildren; 26981195e687SMark J Musante int entry; 26991195e687SMark J Musante 27001195e687SMark J Musante /* 27011195e687SMark J Musante * Unlike cache & spares, slogs are stored in the 27021195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 27031195e687SMark J Musante */ 27041195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 27051195e687SMark J Musante &is_log); 27061195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 27071195e687SMark J Musante &is_hole); 27081195e687SMark J Musante if (is_log || is_hole) { 27091195e687SMark J Musante /* 27101195e687SMark J Musante * Create a hole vdev and put it in the config. 27111195e687SMark J Musante */ 27121195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 27131195e687SMark J Musante goto out; 27141195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 27151195e687SMark J Musante VDEV_TYPE_HOLE) != 0) 27161195e687SMark J Musante goto out; 27171195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 27181195e687SMark J Musante 1) != 0) 27191195e687SMark J Musante goto out; 27201195e687SMark J Musante if (lastlog == 0) 27211195e687SMark J Musante lastlog = vcount; 27221195e687SMark J Musante varray[vcount++] = vdev; 27231195e687SMark J Musante continue; 27241195e687SMark J Musante } 27251195e687SMark J Musante lastlog = 0; 27261195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 27271195e687SMark J Musante == 0); 27281195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 27291195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27301195e687SMark J Musante "Source pool must be composed only of mirrors\n")); 27311195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 27321195e687SMark J Musante goto out; 27331195e687SMark J Musante } 27341195e687SMark J Musante 27351195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c], 27361195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 27371195e687SMark J Musante 27381195e687SMark J Musante /* find or add an entry for this top-level vdev */ 27391195e687SMark J Musante if (newchildren > 0 && 27401195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren, 27411195e687SMark J Musante newchild, newchildren)) >= 0) { 27421195e687SMark J Musante /* We found a disk that the user specified. */ 27431195e687SMark J Musante vdev = mchild[entry]; 27441195e687SMark J Musante ++found; 27451195e687SMark J Musante } else { 27461195e687SMark J Musante /* User didn't specify a disk for this vdev. */ 27471195e687SMark J Musante vdev = mchild[mchildren - 1]; 27481195e687SMark J Musante } 27491195e687SMark J Musante 27501195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 27511195e687SMark J Musante goto out; 27521195e687SMark J Musante } 27531195e687SMark J Musante 27541195e687SMark J Musante /* did we find every disk the user specified? */ 27551195e687SMark J Musante if (found != newchildren) { 27561195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 27571195e687SMark J Musante "include at most one disk from each mirror")); 27581195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 27591195e687SMark J Musante goto out; 27601195e687SMark J Musante } 27611195e687SMark J Musante 27621195e687SMark J Musante /* Prepare the nvlist for populating. */ 27631195e687SMark J Musante if (*newroot == NULL) { 27641195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 27651195e687SMark J Musante goto out; 27661195e687SMark J Musante freelist = B_TRUE; 27671195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 27681195e687SMark J Musante VDEV_TYPE_ROOT) != 0) 27691195e687SMark J Musante goto out; 27701195e687SMark J Musante } else { 27711195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 27721195e687SMark J Musante } 27731195e687SMark J Musante 27741195e687SMark J Musante /* Add all the children we found */ 27751195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 27761195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0) 27771195e687SMark J Musante goto out; 27781195e687SMark J Musante 27791195e687SMark J Musante /* 27801195e687SMark J Musante * If we're just doing a dry run, exit now with success. 27811195e687SMark J Musante */ 27821195e687SMark J Musante if (flags.dryrun) { 27831195e687SMark J Musante memory_err = B_FALSE; 27841195e687SMark J Musante freelist = B_FALSE; 27851195e687SMark J Musante goto out; 27861195e687SMark J Musante } 27871195e687SMark J Musante 27881195e687SMark J Musante /* now build up the config list & call the ioctl */ 27891195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 27901195e687SMark J Musante goto out; 27911195e687SMark J Musante 27921195e687SMark J Musante if (nvlist_add_nvlist(newconfig, 27931195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 27941195e687SMark J Musante nvlist_add_string(newconfig, 27951195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 27961195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 27971195e687SMark J Musante goto out; 27981195e687SMark J Musante 27991195e687SMark J Musante /* 28001195e687SMark J Musante * The new pool is automatically part of the namespace unless we 28011195e687SMark J Musante * explicitly export it. 28021195e687SMark J Musante */ 28031195e687SMark J Musante if (!flags.import) 28041195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 28051195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 28061195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 28071195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 28081195e687SMark J Musante goto out; 28091195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 28101195e687SMark J Musante goto out; 28111195e687SMark J Musante 28121195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 28131195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg); 28141195e687SMark J Musante goto out; 28151195e687SMark J Musante } 28161195e687SMark J Musante 28171195e687SMark J Musante freelist = B_FALSE; 28181195e687SMark J Musante memory_err = B_FALSE; 28191195e687SMark J Musante 28201195e687SMark J Musante out: 28211195e687SMark J Musante if (varray != NULL) { 28221195e687SMark J Musante int v; 28231195e687SMark J Musante 28241195e687SMark J Musante for (v = 0; v < vcount; v++) 28251195e687SMark J Musante nvlist_free(varray[v]); 28261195e687SMark J Musante free(varray); 28271195e687SMark J Musante } 28281195e687SMark J Musante zcmd_free_nvlists(&zc); 28291195e687SMark J Musante if (zc_props) 28301195e687SMark J Musante nvlist_free(zc_props); 28311195e687SMark J Musante if (newconfig) 28321195e687SMark J Musante nvlist_free(newconfig); 28331195e687SMark J Musante if (freelist) { 28341195e687SMark J Musante nvlist_free(*newroot); 28351195e687SMark J Musante *newroot = NULL; 28361195e687SMark J Musante } 28371195e687SMark J Musante 28381195e687SMark J Musante if (retval != 0) 28391195e687SMark J Musante return (retval); 28401195e687SMark J Musante 28411195e687SMark J Musante if (memory_err) 28421195e687SMark J Musante return (no_memory(hdl)); 28431195e687SMark J Musante 28441195e687SMark J Musante return (0); 28451195e687SMark J Musante } 28461195e687SMark J Musante 284799653d4eSeschrock /* 2848fa94a07fSbrendan * Remove the given device. Currently, this is supported only for hot spares 2849fa94a07fSbrendan * and level 2 cache devices. 285099653d4eSeschrock */ 285199653d4eSeschrock int 285299653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 285399653d4eSeschrock { 285499653d4eSeschrock zfs_cmd_t zc = { 0 }; 285599653d4eSeschrock char msg[1024]; 285699653d4eSeschrock nvlist_t *tgt; 285788ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog; 285899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 285988ecc943SGeorge Wilson uint64_t version; 286099653d4eSeschrock 286199653d4eSeschrock (void) snprintf(msg, sizeof (msg), 286299653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 286399653d4eSeschrock 286499653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2865ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 286688ecc943SGeorge Wilson &islog)) == 0) 286799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 286888ecc943SGeorge Wilson /* 286988ecc943SGeorge Wilson * XXX - this should just go away. 287088ecc943SGeorge Wilson */ 287188ecc943SGeorge Wilson if (!avail_spare && !l2cache && !islog) { 287299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 287388ecc943SGeorge Wilson "only inactive hot spares, cache, top-level, " 287488ecc943SGeorge Wilson "or log devices can be removed")); 287599653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 287699653d4eSeschrock } 287799653d4eSeschrock 287888ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 287988ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) { 288088ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 288188ecc943SGeorge Wilson "pool must be upgrade to support log removal")); 288288ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg)); 288388ecc943SGeorge Wilson } 288488ecc943SGeorge Wilson 288599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 288699653d4eSeschrock 2887ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 288899653d4eSeschrock return (0); 288999653d4eSeschrock 289099653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2891ea8dc4b6Seschrock } 2892ea8dc4b6Seschrock 2893ea8dc4b6Seschrock /* 2894ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 2895ea8dc4b6Seschrock */ 2896ea8dc4b6Seschrock int 2897468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 2898ea8dc4b6Seschrock { 2899ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 2900ea8dc4b6Seschrock char msg[1024]; 290199653d4eSeschrock nvlist_t *tgt; 2902468c413aSTim Haley zpool_rewind_policy_t policy; 2903fa94a07fSbrendan boolean_t avail_spare, l2cache; 290499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2905468c413aSTim Haley nvlist_t *nvi = NULL; 29064b964adaSGeorge Wilson int error; 2907ea8dc4b6Seschrock 2908ea8dc4b6Seschrock if (path) 2909ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2910ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2911e9dbad6fSeschrock path); 2912ea8dc4b6Seschrock else 2913ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2914ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 2915ea8dc4b6Seschrock zhp->zpool_name); 2916ea8dc4b6Seschrock 2917ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 291899653d4eSeschrock if (path) { 2919fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 2920ee0eb9f2SEric Schrock &l2cache, NULL)) == 0) 292199653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2922ea8dc4b6Seschrock 2923fa94a07fSbrendan /* 2924fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 2925fa94a07fSbrendan * error clearing for l2cache devices. 2926fa94a07fSbrendan */ 2927a43d325bSek if (avail_spare) 292899653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2929ea8dc4b6Seschrock 293099653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 293199653d4eSeschrock &zc.zc_guid) == 0); 2932fa9e4066Sahrens } 2933fa9e4066Sahrens 2934468c413aSTim Haley zpool_get_rewind_policy(rewindnvl, &policy); 2935468c413aSTim Haley zc.zc_cookie = policy.zrp_request; 2936468c413aSTim Haley 293757f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 2938468c413aSTim Haley return (-1); 2939468c413aSTim Haley 2940cb04b873SMark J Musante if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 2941468c413aSTim Haley return (-1); 2942468c413aSTim Haley 29434b964adaSGeorge Wilson while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 29444b964adaSGeorge Wilson errno == ENOMEM) { 29454b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 29464b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 29474b964adaSGeorge Wilson return (-1); 29484b964adaSGeorge Wilson } 29494b964adaSGeorge Wilson } 29504b964adaSGeorge Wilson 29514b964adaSGeorge Wilson if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 2952468c413aSTim Haley errno != EPERM && errno != EACCES)) { 2953468c413aSTim Haley if (policy.zrp_request & 2954468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 2955468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 2956468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name, 2957468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 2958468c413aSTim Haley nvi); 2959468c413aSTim Haley nvlist_free(nvi); 2960468c413aSTim Haley } 2961468c413aSTim Haley zcmd_free_nvlists(&zc); 296299653d4eSeschrock return (0); 2963468c413aSTim Haley } 296499653d4eSeschrock 2965468c413aSTim Haley zcmd_free_nvlists(&zc); 296699653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2967fa9e4066Sahrens } 2968fa9e4066Sahrens 29693d7072f8Seschrock /* 29703d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 29713d7072f8Seschrock */ 29723d7072f8Seschrock int 29733d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 29743d7072f8Seschrock { 29753d7072f8Seschrock zfs_cmd_t zc = { 0 }; 29763d7072f8Seschrock char msg[1024]; 29773d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 29783d7072f8Seschrock 29793d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 29803d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 29813d7072f8Seschrock guid); 29823d7072f8Seschrock 29833d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 29843d7072f8Seschrock zc.zc_guid = guid; 298514f8ce41SVictor Latushkin zc.zc_cookie = ZPOOL_NO_REWIND; 29863d7072f8Seschrock 29873d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 29883d7072f8Seschrock return (0); 29893d7072f8Seschrock 29903d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 29913d7072f8Seschrock } 29923d7072f8Seschrock 2993e9103aaeSGarrett D'Amore /* 2994e9103aaeSGarrett D'Amore * Change the GUID for a pool. 2995e9103aaeSGarrett D'Amore */ 2996e9103aaeSGarrett D'Amore int 2997e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp) 2998e9103aaeSGarrett D'Amore { 2999e9103aaeSGarrett D'Amore char msg[1024]; 3000e9103aaeSGarrett D'Amore libzfs_handle_t *hdl = zhp->zpool_hdl; 3001e9103aaeSGarrett D'Amore zfs_cmd_t zc = { 0 }; 3002e9103aaeSGarrett D'Amore 3003e9103aaeSGarrett D'Amore (void) snprintf(msg, sizeof (msg), 3004e9103aaeSGarrett D'Amore dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 3005e9103aaeSGarrett D'Amore 3006e9103aaeSGarrett D'Amore (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3007e9103aaeSGarrett D'Amore if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 3008e9103aaeSGarrett D'Amore return (0); 3009e9103aaeSGarrett D'Amore 3010e9103aaeSGarrett D'Amore return (zpool_standard_error(hdl, errno, msg)); 3011e9103aaeSGarrett D'Amore } 3012e9103aaeSGarrett D'Amore 3013*4263d13fSGeorge Wilson /* 3014*4263d13fSGeorge Wilson * Reopen the pool. 3015*4263d13fSGeorge Wilson */ 3016*4263d13fSGeorge Wilson int 3017*4263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp) 3018*4263d13fSGeorge Wilson { 3019*4263d13fSGeorge Wilson zfs_cmd_t zc = { 0 }; 3020*4263d13fSGeorge Wilson char msg[1024]; 3021*4263d13fSGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl; 3022*4263d13fSGeorge Wilson 3023*4263d13fSGeorge Wilson (void) snprintf(msg, sizeof (msg), 3024*4263d13fSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), 3025*4263d13fSGeorge Wilson zhp->zpool_name); 3026*4263d13fSGeorge Wilson 3027*4263d13fSGeorge Wilson (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3028*4263d13fSGeorge Wilson if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0) 3029*4263d13fSGeorge Wilson return (0); 3030*4263d13fSGeorge Wilson return (zpool_standard_error(hdl, errno, msg)); 3031*4263d13fSGeorge Wilson } 3032*4263d13fSGeorge Wilson 3033c67d9675Seschrock /* 3034c67d9675Seschrock * Convert from a devid string to a path. 3035c67d9675Seschrock */ 3036c67d9675Seschrock static char * 3037c67d9675Seschrock devid_to_path(char *devid_str) 3038c67d9675Seschrock { 3039c67d9675Seschrock ddi_devid_t devid; 3040c67d9675Seschrock char *minor; 3041c67d9675Seschrock char *path; 3042c67d9675Seschrock devid_nmlist_t *list = NULL; 3043c67d9675Seschrock int ret; 3044c67d9675Seschrock 3045c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 3046c67d9675Seschrock return (NULL); 3047c67d9675Seschrock 3048c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 3049c67d9675Seschrock 3050c67d9675Seschrock devid_str_free(minor); 3051c67d9675Seschrock devid_free(devid); 3052c67d9675Seschrock 3053c67d9675Seschrock if (ret != 0) 3054c67d9675Seschrock return (NULL); 3055c67d9675Seschrock 305699653d4eSeschrock if ((path = strdup(list[0].devname)) == NULL) 305799653d4eSeschrock return (NULL); 305899653d4eSeschrock 3059c67d9675Seschrock devid_free_nmlist(list); 3060c67d9675Seschrock 3061c67d9675Seschrock return (path); 3062c67d9675Seschrock } 3063c67d9675Seschrock 3064c67d9675Seschrock /* 3065c67d9675Seschrock * Convert from a path to a devid string. 3066c67d9675Seschrock */ 3067c67d9675Seschrock static char * 3068c67d9675Seschrock path_to_devid(const char *path) 3069c67d9675Seschrock { 3070c67d9675Seschrock int fd; 3071c67d9675Seschrock ddi_devid_t devid; 3072c67d9675Seschrock char *minor, *ret; 3073c67d9675Seschrock 3074c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 3075c67d9675Seschrock return (NULL); 3076c67d9675Seschrock 3077c67d9675Seschrock minor = NULL; 3078c67d9675Seschrock ret = NULL; 3079c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 3080c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 3081c67d9675Seschrock ret = devid_str_encode(devid, minor); 3082c67d9675Seschrock if (minor != NULL) 3083c67d9675Seschrock devid_str_free(minor); 3084c67d9675Seschrock devid_free(devid); 3085c67d9675Seschrock } 3086c67d9675Seschrock (void) close(fd); 3087c67d9675Seschrock 3088c67d9675Seschrock return (ret); 3089c67d9675Seschrock } 3090c67d9675Seschrock 3091c67d9675Seschrock /* 3092c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 3093c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 3094c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 3095c67d9675Seschrock */ 3096c67d9675Seschrock static void 3097c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3098c67d9675Seschrock { 3099c67d9675Seschrock zfs_cmd_t zc = { 0 }; 3100c67d9675Seschrock 3101c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3102e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3103c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3104ea8dc4b6Seschrock &zc.zc_guid) == 0); 3105c67d9675Seschrock 310699653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3107c67d9675Seschrock } 3108c67d9675Seschrock 3109c67d9675Seschrock /* 3110c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 3111c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3112c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 3113c67d9675Seschrock * trailing 's0' slice name. 3114c67d9675Seschrock * 3115c67d9675Seschrock * This routine is also responsible for identifying when disks have been 3116c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 3117c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 3118c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 3119c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 3120c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 3121c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3122c67d9675Seschrock * of these checks. 3123c67d9675Seschrock */ 3124c67d9675Seschrock char * 312588ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 312688ecc943SGeorge Wilson boolean_t verbose) 3127c67d9675Seschrock { 3128c67d9675Seschrock char *path, *devid; 3129ea8dc4b6Seschrock uint64_t value; 3130ea8dc4b6Seschrock char buf[64]; 31313d7072f8Seschrock vdev_stat_t *vs; 31323d7072f8Seschrock uint_t vsc; 3133c67d9675Seschrock 3134ea8dc4b6Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3135ea8dc4b6Seschrock &value) == 0) { 3136ea8dc4b6Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3137ea8dc4b6Seschrock &value) == 0); 31385ad82045Snd (void) snprintf(buf, sizeof (buf), "%llu", 31395ad82045Snd (u_longlong_t)value); 3140ea8dc4b6Seschrock path = buf; 3141ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3142c67d9675Seschrock 31433d7072f8Seschrock /* 31443d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 31453d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 31463d7072f8Seschrock * open a misbehaving device, which can have undesirable 31473d7072f8Seschrock * effects. 31483d7072f8Seschrock */ 31493f9d6ad7SLin Ling if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 31503d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 31513d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 31523d7072f8Seschrock zhp != NULL && 3153c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3154c67d9675Seschrock /* 3155c67d9675Seschrock * Determine if the current path is correct. 3156c67d9675Seschrock */ 3157c67d9675Seschrock char *newdevid = path_to_devid(path); 3158c67d9675Seschrock 3159c67d9675Seschrock if (newdevid == NULL || 3160c67d9675Seschrock strcmp(devid, newdevid) != 0) { 3161c67d9675Seschrock char *newpath; 3162c67d9675Seschrock 3163c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 3164c67d9675Seschrock /* 3165c67d9675Seschrock * Update the path appropriately. 3166c67d9675Seschrock */ 3167c67d9675Seschrock set_path(zhp, nv, newpath); 316899653d4eSeschrock if (nvlist_add_string(nv, 316999653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 317099653d4eSeschrock verify(nvlist_lookup_string(nv, 317199653d4eSeschrock ZPOOL_CONFIG_PATH, 317299653d4eSeschrock &path) == 0); 3173c67d9675Seschrock free(newpath); 3174c67d9675Seschrock } 3175c67d9675Seschrock } 3176c67d9675Seschrock 317799653d4eSeschrock if (newdevid) 317899653d4eSeschrock devid_str_free(newdevid); 3179c67d9675Seschrock } 3180c67d9675Seschrock 3181c67d9675Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 3182c67d9675Seschrock path += 9; 3183c67d9675Seschrock 3184c67d9675Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3185ea8dc4b6Seschrock &value) == 0 && value) { 31863fdda499SJohn Harres int pathlen = strlen(path); 318799653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 31883fdda499SJohn Harres 31893fdda499SJohn Harres /* 31903fdda499SJohn Harres * If it starts with c#, and ends with "s0", chop 31913fdda499SJohn Harres * the "s0" off, or if it ends with "s0/old", remove 31923fdda499SJohn Harres * the "s0" from the middle. 31933fdda499SJohn Harres */ 31943fdda499SJohn Harres if (CTD_CHECK(tmp)) { 31953fdda499SJohn Harres if (strcmp(&tmp[pathlen - 2], "s0") == 0) { 31963fdda499SJohn Harres tmp[pathlen - 2] = '\0'; 31973fdda499SJohn Harres } else if (pathlen > 6 && 31983fdda499SJohn Harres strcmp(&tmp[pathlen - 6], "s0/old") == 0) { 31993fdda499SJohn Harres (void) strcpy(&tmp[pathlen - 6], 32003fdda499SJohn Harres "/old"); 32013fdda499SJohn Harres } 32023fdda499SJohn Harres } 3203c67d9675Seschrock return (tmp); 3204c67d9675Seschrock } 3205c67d9675Seschrock } else { 3206c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 320799653d4eSeschrock 320899653d4eSeschrock /* 320999653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 321099653d4eSeschrock */ 321199653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 321299653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 321399653d4eSeschrock &value) == 0); 321499653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 32155ad82045Snd (u_longlong_t)value); 321699653d4eSeschrock path = buf; 321799653d4eSeschrock } 321888ecc943SGeorge Wilson 321988ecc943SGeorge Wilson /* 322088ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id> 322188ecc943SGeorge Wilson * naming convention. 322288ecc943SGeorge Wilson */ 322388ecc943SGeorge Wilson if (verbose) { 322488ecc943SGeorge Wilson uint64_t id; 322588ecc943SGeorge Wilson 322688ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 322788ecc943SGeorge Wilson &id) == 0); 322888ecc943SGeorge Wilson (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 322988ecc943SGeorge Wilson (u_longlong_t)id); 323088ecc943SGeorge Wilson path = buf; 323188ecc943SGeorge Wilson } 3232c67d9675Seschrock } 3233c67d9675Seschrock 323499653d4eSeschrock return (zfs_strdup(hdl, path)); 3235c67d9675Seschrock } 3236ea8dc4b6Seschrock 3237ea8dc4b6Seschrock static int 3238ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b) 3239ea8dc4b6Seschrock { 3240ea8dc4b6Seschrock return (memcmp(a, b, sizeof (zbookmark_t))); 3241ea8dc4b6Seschrock } 3242ea8dc4b6Seschrock 3243ea8dc4b6Seschrock /* 3244ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 3245ea8dc4b6Seschrock * caller. 3246ea8dc4b6Seschrock */ 3247ea8dc4b6Seschrock int 324855434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3249ea8dc4b6Seschrock { 3250ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3251ea8dc4b6Seschrock uint64_t count; 3252e9dbad6fSeschrock zbookmark_t *zb = NULL; 325355434c77Sek int i; 3254ea8dc4b6Seschrock 3255ea8dc4b6Seschrock /* 3256ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 3257ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 3258ea8dc4b6Seschrock * entire list. 3259ea8dc4b6Seschrock */ 3260ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3261ea8dc4b6Seschrock &count) == 0); 326275519f38Sek if (count == 0) 326375519f38Sek return (0); 3264e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 32655ad82045Snd count * sizeof (zbookmark_t))) == (uintptr_t)NULL) 326699653d4eSeschrock return (-1); 3267e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 3268ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3269ea8dc4b6Seschrock for (;;) { 327099653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 327199653d4eSeschrock &zc) != 0) { 3272e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 3273ea8dc4b6Seschrock if (errno == ENOMEM) { 3274bf561db0Svb count = zc.zc_nvlist_dst_size; 3275e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t) 3276bf561db0Svb zfs_alloc(zhp->zpool_hdl, count * 3277bf561db0Svb sizeof (zbookmark_t))) == (uintptr_t)NULL) 327899653d4eSeschrock return (-1); 3279ea8dc4b6Seschrock } else { 3280ea8dc4b6Seschrock return (-1); 3281ea8dc4b6Seschrock } 3282ea8dc4b6Seschrock } else { 3283ea8dc4b6Seschrock break; 3284ea8dc4b6Seschrock } 3285ea8dc4b6Seschrock } 3286ea8dc4b6Seschrock 3287ea8dc4b6Seschrock /* 3288ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 3289ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3290e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3291ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 3292ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 3293ea8dc4b6Seschrock */ 3294e9dbad6fSeschrock zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + 3295e9dbad6fSeschrock zc.zc_nvlist_dst_size; 3296e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 3297ea8dc4b6Seschrock 3298ea8dc4b6Seschrock qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); 3299ea8dc4b6Seschrock 330055434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3301ea8dc4b6Seschrock 3302ea8dc4b6Seschrock /* 330355434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3304ea8dc4b6Seschrock */ 3305ea8dc4b6Seschrock for (i = 0; i < count; i++) { 3306ea8dc4b6Seschrock nvlist_t *nv; 3307ea8dc4b6Seschrock 3308c0a81264Sek /* ignoring zb_blkid and zb_level for now */ 3309c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3310c0a81264Sek zb[i-1].zb_object == zb[i].zb_object) 3311ea8dc4b6Seschrock continue; 3312ea8dc4b6Seschrock 331355434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 331455434c77Sek goto nomem; 331555434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 331655434c77Sek zb[i].zb_objset) != 0) { 331755434c77Sek nvlist_free(nv); 331899653d4eSeschrock goto nomem; 3319ea8dc4b6Seschrock } 332055434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 332155434c77Sek zb[i].zb_object) != 0) { 332255434c77Sek nvlist_free(nv); 332355434c77Sek goto nomem; 332455434c77Sek } 332555434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 332655434c77Sek nvlist_free(nv); 332755434c77Sek goto nomem; 332855434c77Sek } 332955434c77Sek nvlist_free(nv); 3330ea8dc4b6Seschrock } 3331ea8dc4b6Seschrock 33323ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 3333ea8dc4b6Seschrock return (0); 333499653d4eSeschrock 333599653d4eSeschrock nomem: 3336e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 333799653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 3338ea8dc4b6Seschrock } 3339eaca9bbdSeschrock 3340eaca9bbdSeschrock /* 3341eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 3342eaca9bbdSeschrock */ 3343eaca9bbdSeschrock int 3344990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3345eaca9bbdSeschrock { 3346eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 334799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3348eaca9bbdSeschrock 3349eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3350990b4856Slling zc.zc_cookie = new_version; 3351990b4856Slling 3352ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3353ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 335499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 335599653d4eSeschrock zhp->zpool_name)); 3356eaca9bbdSeschrock return (0); 3357eaca9bbdSeschrock } 335806eeb2adSek 335906eeb2adSek void 33602a6b87f0Sek zpool_set_history_str(const char *subcommand, int argc, char **argv, 33612a6b87f0Sek char *history_str) 336206eeb2adSek { 336306eeb2adSek int i; 336406eeb2adSek 33652a6b87f0Sek (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN); 33662a6b87f0Sek for (i = 1; i < argc; i++) { 33672a6b87f0Sek if (strlen(history_str) + 1 + strlen(argv[i]) > 33682a6b87f0Sek HIS_MAX_RECORD_LEN) 33692a6b87f0Sek break; 33702a6b87f0Sek (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN); 33712a6b87f0Sek (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN); 33722a6b87f0Sek } 33732a6b87f0Sek } 33742a6b87f0Sek 33752a6b87f0Sek /* 33762a6b87f0Sek * Stage command history for logging. 33772a6b87f0Sek */ 33782a6b87f0Sek int 33792a6b87f0Sek zpool_stage_history(libzfs_handle_t *hdl, const char *history_str) 33802a6b87f0Sek { 33812a6b87f0Sek if (history_str == NULL) 33822a6b87f0Sek return (EINVAL); 33832a6b87f0Sek 33842a6b87f0Sek if (strlen(history_str) > HIS_MAX_RECORD_LEN) 33852a6b87f0Sek return (EINVAL); 33862a6b87f0Sek 3387228975ccSek if (hdl->libzfs_log_str != NULL) 3388ecd6cf80Smarks free(hdl->libzfs_log_str); 338906eeb2adSek 33902a6b87f0Sek if ((hdl->libzfs_log_str = strdup(history_str)) == NULL) 33912a6b87f0Sek return (no_memory(hdl)); 339206eeb2adSek 33932a6b87f0Sek return (0); 339406eeb2adSek } 339506eeb2adSek 339606eeb2adSek /* 339706eeb2adSek * Perform ioctl to get some command history of a pool. 339806eeb2adSek * 339906eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 340006eeb2adSek * logical offset of the history buffer to start reading from. 340106eeb2adSek * 340206eeb2adSek * Upon return, 'off' is the next logical offset to read from and 340306eeb2adSek * 'len' is the actual amount of bytes read into 'buf'. 340406eeb2adSek */ 340506eeb2adSek static int 340606eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 340706eeb2adSek { 340806eeb2adSek zfs_cmd_t zc = { 0 }; 340906eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl; 341006eeb2adSek 341106eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 341206eeb2adSek 341306eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf; 341406eeb2adSek zc.zc_history_len = *len; 341506eeb2adSek zc.zc_history_offset = *off; 341606eeb2adSek 341706eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 341806eeb2adSek switch (errno) { 341906eeb2adSek case EPERM: 3420ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 3421ece3d9b3Slling dgettext(TEXT_DOMAIN, 342206eeb2adSek "cannot show history for pool '%s'"), 342306eeb2adSek zhp->zpool_name)); 342406eeb2adSek case ENOENT: 3425ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 342606eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool " 342706eeb2adSek "'%s'"), zhp->zpool_name)); 3428d7306b64Sek case ENOTSUP: 3429d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3430d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool " 3431d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name)); 343206eeb2adSek default: 3433ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 343406eeb2adSek dgettext(TEXT_DOMAIN, 343506eeb2adSek "cannot get history for '%s'"), zhp->zpool_name)); 343606eeb2adSek } 343706eeb2adSek } 343806eeb2adSek 343906eeb2adSek *len = zc.zc_history_len; 344006eeb2adSek *off = zc.zc_history_offset; 344106eeb2adSek 344206eeb2adSek return (0); 344306eeb2adSek } 344406eeb2adSek 344506eeb2adSek /* 344606eeb2adSek * Process the buffer of nvlists, unpacking and storing each nvlist record 344706eeb2adSek * into 'records'. 'leftover' is set to the number of bytes that weren't 344806eeb2adSek * processed as there wasn't a complete record. 344906eeb2adSek */ 34508f18d1faSGeorge Wilson int 345106eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 345206eeb2adSek nvlist_t ***records, uint_t *numrecords) 345306eeb2adSek { 345406eeb2adSek uint64_t reclen; 345506eeb2adSek nvlist_t *nv; 345606eeb2adSek int i; 345706eeb2adSek 345806eeb2adSek while (bytes_read > sizeof (reclen)) { 345906eeb2adSek 346006eeb2adSek /* get length of packed record (stored as little endian) */ 346106eeb2adSek for (i = 0, reclen = 0; i < sizeof (reclen); i++) 346206eeb2adSek reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 346306eeb2adSek 346406eeb2adSek if (bytes_read < sizeof (reclen) + reclen) 346506eeb2adSek break; 346606eeb2adSek 346706eeb2adSek /* unpack record */ 346806eeb2adSek if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 346906eeb2adSek return (ENOMEM); 347006eeb2adSek bytes_read -= sizeof (reclen) + reclen; 347106eeb2adSek buf += sizeof (reclen) + reclen; 347206eeb2adSek 347306eeb2adSek /* add record to nvlist array */ 347406eeb2adSek (*numrecords)++; 347506eeb2adSek if (ISP2(*numrecords + 1)) { 347606eeb2adSek *records = realloc(*records, 347706eeb2adSek *numrecords * 2 * sizeof (nvlist_t *)); 347806eeb2adSek } 347906eeb2adSek (*records)[*numrecords - 1] = nv; 348006eeb2adSek } 348106eeb2adSek 348206eeb2adSek *leftover = bytes_read; 348306eeb2adSek return (0); 348406eeb2adSek } 348506eeb2adSek 348606eeb2adSek #define HIS_BUF_LEN (128*1024) 348706eeb2adSek 348806eeb2adSek /* 348906eeb2adSek * Retrieve the command history of a pool. 349006eeb2adSek */ 349106eeb2adSek int 349206eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 349306eeb2adSek { 349406eeb2adSek char buf[HIS_BUF_LEN]; 349506eeb2adSek uint64_t off = 0; 349606eeb2adSek nvlist_t **records = NULL; 349706eeb2adSek uint_t numrecords = 0; 349806eeb2adSek int err, i; 349906eeb2adSek 350006eeb2adSek do { 350106eeb2adSek uint64_t bytes_read = sizeof (buf); 350206eeb2adSek uint64_t leftover; 350306eeb2adSek 350406eeb2adSek if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 350506eeb2adSek break; 350606eeb2adSek 350706eeb2adSek /* if nothing else was read in, we're at EOF, just return */ 350806eeb2adSek if (!bytes_read) 350906eeb2adSek break; 351006eeb2adSek 351106eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read, 351206eeb2adSek &leftover, &records, &numrecords)) != 0) 351306eeb2adSek break; 351406eeb2adSek off -= leftover; 351506eeb2adSek 351606eeb2adSek /* CONSTCOND */ 351706eeb2adSek } while (1); 351806eeb2adSek 351906eeb2adSek if (!err) { 352006eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 352106eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 352206eeb2adSek records, numrecords) == 0); 352306eeb2adSek } 352406eeb2adSek for (i = 0; i < numrecords; i++) 352506eeb2adSek nvlist_free(records[i]); 352606eeb2adSek free(records); 352706eeb2adSek 352806eeb2adSek return (err); 352906eeb2adSek } 353055434c77Sek 353155434c77Sek void 353255434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 353355434c77Sek char *pathname, size_t len) 353455434c77Sek { 353555434c77Sek zfs_cmd_t zc = { 0 }; 353655434c77Sek boolean_t mounted = B_FALSE; 353755434c77Sek char *mntpnt = NULL; 353855434c77Sek char dsname[MAXNAMELEN]; 353955434c77Sek 354055434c77Sek if (dsobj == 0) { 354155434c77Sek /* special case for the MOS */ 354255434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 354355434c77Sek return; 354455434c77Sek } 354555434c77Sek 354655434c77Sek /* get the dataset's name */ 354755434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 354855434c77Sek zc.zc_obj = dsobj; 354955434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, 355055434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 355155434c77Sek /* just write out a path of two object numbers */ 355255434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 355355434c77Sek dsobj, obj); 355455434c77Sek return; 355555434c77Sek } 355655434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 355755434c77Sek 355855434c77Sek /* find out if the dataset is mounted */ 355955434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 356055434c77Sek 356155434c77Sek /* get the corrupted object's path */ 356255434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 356355434c77Sek zc.zc_obj = obj; 356455434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 356555434c77Sek &zc) == 0) { 356655434c77Sek if (mounted) { 356755434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt, 356855434c77Sek zc.zc_value); 356955434c77Sek } else { 357055434c77Sek (void) snprintf(pathname, len, "%s:%s", 357155434c77Sek dsname, zc.zc_value); 357255434c77Sek } 357355434c77Sek } else { 357455434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 357555434c77Sek } 357655434c77Sek free(mntpnt); 357755434c77Sek } 3578b1b8ab34Slling 357915e6edf1Sgw /* 358015e6edf1Sgw * Read the EFI label from the config, if a label does not exist then 358115e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL 358215e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI 358315e6edf1Sgw * partition. 358415e6edf1Sgw */ 358515e6edf1Sgw static int 358615e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb) 358715e6edf1Sgw { 358815e6edf1Sgw char *path; 358915e6edf1Sgw int fd; 359015e6edf1Sgw char diskname[MAXPATHLEN]; 359115e6edf1Sgw int err = -1; 359215e6edf1Sgw 359315e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 359415e6edf1Sgw return (err); 359515e6edf1Sgw 359615e6edf1Sgw (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 359715e6edf1Sgw strrchr(path, '/')); 359815e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 359915e6edf1Sgw struct dk_gpt *vtoc; 360015e6edf1Sgw 360115e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 360215e6edf1Sgw if (sb != NULL) 360315e6edf1Sgw *sb = vtoc->efi_parts[0].p_start; 360415e6edf1Sgw efi_free(vtoc); 360515e6edf1Sgw } 360615e6edf1Sgw (void) close(fd); 360715e6edf1Sgw } 360815e6edf1Sgw return (err); 360915e6edf1Sgw } 361015e6edf1Sgw 36118488aeb5Staylor /* 36128488aeb5Staylor * determine where a partition starts on a disk in the current 36138488aeb5Staylor * configuration 36148488aeb5Staylor */ 36158488aeb5Staylor static diskaddr_t 36168488aeb5Staylor find_start_block(nvlist_t *config) 36178488aeb5Staylor { 36188488aeb5Staylor nvlist_t **child; 36198488aeb5Staylor uint_t c, children; 36208488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 36218488aeb5Staylor uint64_t wholedisk; 36228488aeb5Staylor 36238488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 36248488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 36258488aeb5Staylor if (nvlist_lookup_uint64(config, 36268488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 36278488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 36288488aeb5Staylor return (MAXOFFSET_T); 36298488aeb5Staylor } 363015e6edf1Sgw if (read_efi_label(config, &sb) < 0) 363115e6edf1Sgw sb = MAXOFFSET_T; 36328488aeb5Staylor return (sb); 36338488aeb5Staylor } 36348488aeb5Staylor 36358488aeb5Staylor for (c = 0; c < children; c++) { 36368488aeb5Staylor sb = find_start_block(child[c]); 36378488aeb5Staylor if (sb != MAXOFFSET_T) { 36388488aeb5Staylor return (sb); 36398488aeb5Staylor } 36408488aeb5Staylor } 36418488aeb5Staylor return (MAXOFFSET_T); 36428488aeb5Staylor } 36438488aeb5Staylor 36448488aeb5Staylor /* 36458488aeb5Staylor * Label an individual disk. The name provided is the short name, 36468488aeb5Staylor * stripped of any leading /dev path. 36478488aeb5Staylor */ 36488488aeb5Staylor int 36498488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 36508488aeb5Staylor { 36518488aeb5Staylor char path[MAXPATHLEN]; 36528488aeb5Staylor struct dk_gpt *vtoc; 36538488aeb5Staylor int fd; 36548488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 36558488aeb5Staylor uint64_t slice_size; 36568488aeb5Staylor diskaddr_t start_block; 36578488aeb5Staylor char errbuf[1024]; 36588488aeb5Staylor 3659c6ef114fSmmusante /* prepare an error message just in case */ 3660c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 3661c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 3662c6ef114fSmmusante 36638488aeb5Staylor if (zhp) { 36648488aeb5Staylor nvlist_t *nvroot; 36658488aeb5Staylor 3666*4263d13fSGeorge Wilson if (zpool_is_bootable(zhp)) { 3667b5b76fecSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3668b5b76fecSGeorge Wilson "EFI labeled devices are not supported on root " 3669b5b76fecSGeorge Wilson "pools.")); 3670b5b76fecSGeorge Wilson return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf)); 3671b5b76fecSGeorge Wilson } 3672b5b76fecSGeorge Wilson 36738488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 36748488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 36758488aeb5Staylor 36768488aeb5Staylor if (zhp->zpool_start_block == 0) 36778488aeb5Staylor start_block = find_start_block(nvroot); 36788488aeb5Staylor else 36798488aeb5Staylor start_block = zhp->zpool_start_block; 36808488aeb5Staylor zhp->zpool_start_block = start_block; 36818488aeb5Staylor } else { 36828488aeb5Staylor /* new pool */ 36838488aeb5Staylor start_block = NEW_START_BLOCK; 36848488aeb5Staylor } 36858488aeb5Staylor 36868488aeb5Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 36878488aeb5Staylor BACKUP_SLICE); 36888488aeb5Staylor 36898488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 36908488aeb5Staylor /* 36918488aeb5Staylor * This shouldn't happen. We've long since verified that this 36928488aeb5Staylor * is a valid device. 36938488aeb5Staylor */ 3694c6ef114fSmmusante zfs_error_aux(hdl, 3695c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 36968488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 36978488aeb5Staylor } 36988488aeb5Staylor 36998488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 37008488aeb5Staylor /* 37018488aeb5Staylor * The only way this can fail is if we run out of memory, or we 37028488aeb5Staylor * were unable to read the disk's capacity 37038488aeb5Staylor */ 37048488aeb5Staylor if (errno == ENOMEM) 37058488aeb5Staylor (void) no_memory(hdl); 37068488aeb5Staylor 37078488aeb5Staylor (void) close(fd); 3708c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3709c6ef114fSmmusante "unable to read disk capacity"), name); 37108488aeb5Staylor 37118488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 37128488aeb5Staylor } 37138488aeb5Staylor 37148488aeb5Staylor slice_size = vtoc->efi_last_u_lba + 1; 37158488aeb5Staylor slice_size -= EFI_MIN_RESV_SIZE; 37168488aeb5Staylor if (start_block == MAXOFFSET_T) 37178488aeb5Staylor start_block = NEW_START_BLOCK; 37188488aeb5Staylor slice_size -= start_block; 37198488aeb5Staylor 37208488aeb5Staylor vtoc->efi_parts[0].p_start = start_block; 37218488aeb5Staylor vtoc->efi_parts[0].p_size = slice_size; 37228488aeb5Staylor 37238488aeb5Staylor /* 37248488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 37258488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 37268488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 37278488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 37288488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 37298488aeb5Staylor * can get, in the absence of V_OTHER. 37308488aeb5Staylor */ 37318488aeb5Staylor vtoc->efi_parts[0].p_tag = V_USR; 37328488aeb5Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 37338488aeb5Staylor 37348488aeb5Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 37358488aeb5Staylor vtoc->efi_parts[8].p_size = resv; 37368488aeb5Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 37378488aeb5Staylor 37388488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 37398488aeb5Staylor /* 37408488aeb5Staylor * Some block drivers (like pcata) may not support EFI 37418488aeb5Staylor * GPT labels. Print out a helpful error message dir- 37428488aeb5Staylor * ecting the user to manually label the disk and give 37438488aeb5Staylor * a specific slice. 37448488aeb5Staylor */ 37458488aeb5Staylor (void) close(fd); 37468488aeb5Staylor efi_free(vtoc); 37478488aeb5Staylor 37488488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3749c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 37508488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 37518488aeb5Staylor } 37528488aeb5Staylor 37538488aeb5Staylor (void) close(fd); 37548488aeb5Staylor efi_free(vtoc); 37558488aeb5Staylor return (0); 37568488aeb5Staylor } 3757e7cbe64fSgw 3758e7cbe64fSgw static boolean_t 3759e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 3760e7cbe64fSgw { 3761e7cbe64fSgw char *type; 3762e7cbe64fSgw nvlist_t **child; 3763e7cbe64fSgw uint_t children, c; 3764e7cbe64fSgw 3765e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 3766e7cbe64fSgw if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 3767e7cbe64fSgw strcmp(type, VDEV_TYPE_FILE) == 0 || 3768e7cbe64fSgw strcmp(type, VDEV_TYPE_LOG) == 0 || 376988ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 || 3770e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) { 3771e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3772e7cbe64fSgw "vdev type '%s' is not supported"), type); 3773e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 3774e7cbe64fSgw return (B_FALSE); 3775e7cbe64fSgw } 3776e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3777e7cbe64fSgw &child, &children) == 0) { 3778e7cbe64fSgw for (c = 0; c < children; c++) { 3779e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 3780e7cbe64fSgw return (B_FALSE); 3781e7cbe64fSgw } 3782e7cbe64fSgw } 3783e7cbe64fSgw return (B_TRUE); 3784e7cbe64fSgw } 3785e7cbe64fSgw 3786e7cbe64fSgw /* 3787e7cbe64fSgw * check if this zvol is allowable for use as a dump device; zero if 3788e7cbe64fSgw * it is, > 0 if it isn't, < 0 if it isn't a zvol 3789e7cbe64fSgw */ 3790e7cbe64fSgw int 3791e7cbe64fSgw zvol_check_dump_config(char *arg) 3792e7cbe64fSgw { 3793e7cbe64fSgw zpool_handle_t *zhp = NULL; 3794e7cbe64fSgw nvlist_t *config, *nvroot; 3795e7cbe64fSgw char *p, *volname; 3796e7cbe64fSgw nvlist_t **top; 3797e7cbe64fSgw uint_t toplevels; 3798e7cbe64fSgw libzfs_handle_t *hdl; 3799e7cbe64fSgw char errbuf[1024]; 3800e7cbe64fSgw char poolname[ZPOOL_MAXNAMELEN]; 3801e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR); 3802e7cbe64fSgw int ret = 1; 3803e7cbe64fSgw 3804e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 3805e7cbe64fSgw return (-1); 3806e7cbe64fSgw } 3807e7cbe64fSgw 3808e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3809e7cbe64fSgw "dump is not supported on device '%s'"), arg); 3810e7cbe64fSgw 3811e7cbe64fSgw if ((hdl = libzfs_init()) == NULL) 3812e7cbe64fSgw return (1); 3813e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE); 3814e7cbe64fSgw 3815e7cbe64fSgw volname = arg + pathlen; 3816e7cbe64fSgw 3817e7cbe64fSgw /* check the configuration of the pool */ 3818e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) { 3819e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3820e7cbe64fSgw "malformed dataset name")); 3821e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 3822e7cbe64fSgw return (1); 3823e7cbe64fSgw } else if (p - volname >= ZFS_MAXNAMELEN) { 3824e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3825e7cbe64fSgw "dataset name is too long")); 3826e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 3827e7cbe64fSgw return (1); 3828e7cbe64fSgw } else { 3829e7cbe64fSgw (void) strncpy(poolname, volname, p - volname); 3830e7cbe64fSgw poolname[p - volname] = '\0'; 3831e7cbe64fSgw } 3832e7cbe64fSgw 3833e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) { 3834e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3835e7cbe64fSgw "could not open pool '%s'"), poolname); 3836e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 3837e7cbe64fSgw goto out; 3838e7cbe64fSgw } 3839e7cbe64fSgw config = zpool_get_config(zhp, NULL); 3840e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 3841e7cbe64fSgw &nvroot) != 0) { 3842e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3843e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname); 3844e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 3845e7cbe64fSgw goto out; 3846e7cbe64fSgw } 3847e7cbe64fSgw 3848e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3849e7cbe64fSgw &top, &toplevels) == 0); 3850e7cbe64fSgw if (toplevels != 1) { 3851e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3852e7cbe64fSgw "'%s' has multiple top level vdevs"), poolname); 3853e7cbe64fSgw (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf); 3854e7cbe64fSgw goto out; 3855e7cbe64fSgw } 3856e7cbe64fSgw 3857e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 3858e7cbe64fSgw goto out; 3859e7cbe64fSgw } 3860e7cbe64fSgw ret = 0; 3861e7cbe64fSgw 3862e7cbe64fSgw out: 3863e7cbe64fSgw if (zhp) 3864e7cbe64fSgw zpool_close(zhp); 3865e7cbe64fSgw libzfs_fini(hdl); 3866e7cbe64fSgw return (ret); 3867e7cbe64fSgw } 3868