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 /* 23078266a5SMarcel Telka * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 243f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 259a686fbcSPaul Dagnelie * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 26810e43b2SBill Pijewski * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29fa9e4066Sahrens #include <ctype.h> 30fa9e4066Sahrens #include <errno.h> 31fa9e4066Sahrens #include <devid.h> 32fa9e4066Sahrens #include <fcntl.h> 33fa9e4066Sahrens #include <libintl.h> 34fa9e4066Sahrens #include <stdio.h> 35fa9e4066Sahrens #include <stdlib.h> 36f3861e1aSahl #include <strings.h> 37fa9e4066Sahrens #include <unistd.h> 384445fffbSMatthew Ahrens #include <libgen.h> 398488aeb5Staylor #include <sys/efi_partition.h> 408488aeb5Staylor #include <sys/vtoc.h> 41fa9e4066Sahrens #include <sys/zfs_ioctl.h> 42573ca77eSGeorge Wilson #include <dlfcn.h> 43fa9e4066Sahrens 44fa9e4066Sahrens #include "zfs_namecheck.h" 45b1b8ab34Slling #include "zfs_prop.h" 46fa9e4066Sahrens #include "libzfs_impl.h" 47468c413aSTim Haley #include "zfs_comutil.h" 48ad135b5dSChristopher Siden #include "zfeature_common.h" 49fa9e4066Sahrens 5015e6edf1Sgw25295 static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 51990b4856Slling 52573ca77eSGeorge Wilson #define DISK_ROOT "/dev/dsk" 53573ca77eSGeorge Wilson #define RDISK_ROOT "/dev/rdsk" 54573ca77eSGeorge Wilson #define BACKUP_SLICE "s2" 55573ca77eSGeorge Wilson 56f9af39baSGeorge Wilson typedef struct prop_flags { 57f9af39baSGeorge Wilson int create:1; /* Validate property on creation */ 58f9af39baSGeorge Wilson int import:1; /* Validate property on import */ 59f9af39baSGeorge Wilson } prop_flags_t; 60f9af39baSGeorge Wilson 61990b4856Slling /* 62990b4856Slling * ==================================================================== 63990b4856Slling * zpool property functions 64990b4856Slling * ==================================================================== 65990b4856Slling */ 66990b4856Slling 67990b4856Slling static int 68990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 69990b4856Slling { 70990b4856Slling zfs_cmd_t zc = { 0 }; 71990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 72990b4856Slling 73990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 74990b4856Slling 75990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 76990b4856Slling return (-1); 77990b4856Slling 78990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 79990b4856Slling if (errno == ENOMEM) { 80990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 81990b4856Slling zcmd_free_nvlists(&zc); 82990b4856Slling return (-1); 83990b4856Slling } 84990b4856Slling } else { 85990b4856Slling zcmd_free_nvlists(&zc); 86990b4856Slling return (-1); 87990b4856Slling } 88990b4856Slling } 89990b4856Slling 90990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 91990b4856Slling zcmd_free_nvlists(&zc); 92990b4856Slling return (-1); 93990b4856Slling } 94990b4856Slling 95990b4856Slling zcmd_free_nvlists(&zc); 96990b4856Slling 97990b4856Slling return (0); 98990b4856Slling } 99990b4856Slling 100990b4856Slling static int 101990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 102990b4856Slling { 103990b4856Slling nvlist_t *old_props; 104990b4856Slling 105990b4856Slling old_props = zhp->zpool_props; 106990b4856Slling 107990b4856Slling if (zpool_get_all_props(zhp) != 0) 108990b4856Slling return (-1); 109990b4856Slling 110990b4856Slling nvlist_free(old_props); 111990b4856Slling return (0); 112990b4856Slling } 113990b4856Slling 114990b4856Slling static char * 115990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 116990b4856Slling zprop_source_t *src) 117990b4856Slling { 118990b4856Slling nvlist_t *nv, *nvl; 119990b4856Slling uint64_t ival; 120990b4856Slling char *value; 121990b4856Slling zprop_source_t source; 122990b4856Slling 123990b4856Slling nvl = zhp->zpool_props; 124990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 125990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 126990b4856Slling source = ival; 127990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 128990b4856Slling } else { 129990b4856Slling source = ZPROP_SRC_DEFAULT; 130990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 131990b4856Slling value = "-"; 132990b4856Slling } 133990b4856Slling 134990b4856Slling if (src) 135990b4856Slling *src = source; 136990b4856Slling 137990b4856Slling return (value); 138990b4856Slling } 139990b4856Slling 140990b4856Slling uint64_t 141990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 142990b4856Slling { 143990b4856Slling nvlist_t *nv, *nvl; 144990b4856Slling uint64_t value; 145990b4856Slling zprop_source_t source; 146990b4856Slling 147b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 148b87f3af3Sperrin /* 149b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 150b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 151b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 152b87f3af3Sperrin */ 153b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 154b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 155b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 156b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 157b87f3af3Sperrin == 0)) { 158b87f3af3Sperrin return (value); 159b87f3af3Sperrin } 160990b4856Slling return (zpool_prop_default_numeric(prop)); 161b87f3af3Sperrin } 162990b4856Slling 163990b4856Slling nvl = zhp->zpool_props; 164990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 165990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 166990b4856Slling source = value; 167990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 168990b4856Slling } else { 169990b4856Slling source = ZPROP_SRC_DEFAULT; 170990b4856Slling value = zpool_prop_default_numeric(prop); 171990b4856Slling } 172990b4856Slling 173990b4856Slling if (src) 174990b4856Slling *src = source; 175990b4856Slling 176990b4856Slling return (value); 177990b4856Slling } 178990b4856Slling 179990b4856Slling /* 180990b4856Slling * Map VDEV STATE to printed strings. 181990b4856Slling */ 182990b4856Slling char * 183990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 184990b4856Slling { 185990b4856Slling switch (state) { 186990b4856Slling case VDEV_STATE_CLOSED: 187990b4856Slling case VDEV_STATE_OFFLINE: 188990b4856Slling return (gettext("OFFLINE")); 189990b4856Slling case VDEV_STATE_REMOVED: 190990b4856Slling return (gettext("REMOVED")); 191990b4856Slling case VDEV_STATE_CANT_OPEN: 192b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 193990b4856Slling return (gettext("FAULTED")); 1941195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL) 1951195e687SMark J Musante return (gettext("SPLIT")); 196990b4856Slling else 197990b4856Slling return (gettext("UNAVAIL")); 198990b4856Slling case VDEV_STATE_FAULTED: 199990b4856Slling return (gettext("FAULTED")); 200990b4856Slling case VDEV_STATE_DEGRADED: 201990b4856Slling return (gettext("DEGRADED")); 202990b4856Slling case VDEV_STATE_HEALTHY: 203990b4856Slling return (gettext("ONLINE")); 204990b4856Slling } 205990b4856Slling 206990b4856Slling return (gettext("UNKNOWN")); 207990b4856Slling } 208990b4856Slling 209990b4856Slling /* 210990b4856Slling * Get a zpool property value for 'prop' and return the value in 211990b4856Slling * a pre-allocated buffer. 212990b4856Slling */ 213990b4856Slling int 214990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 215c58b3526SAdam Stevko zprop_source_t *srctype, boolean_t literal) 216990b4856Slling { 217990b4856Slling uint64_t intval; 218990b4856Slling const char *strval; 219990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 220990b4856Slling nvlist_t *nvroot; 221990b4856Slling vdev_stat_t *vs; 222990b4856Slling uint_t vsc; 223990b4856Slling 224990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 225379c004dSEric Schrock switch (prop) { 226379c004dSEric Schrock case ZPOOL_PROP_NAME: 227990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 228379c004dSEric Schrock break; 229379c004dSEric Schrock 230379c004dSEric Schrock case ZPOOL_PROP_HEALTH: 231990b4856Slling (void) strlcpy(buf, "FAULTED", len); 232379c004dSEric Schrock break; 233379c004dSEric Schrock 234379c004dSEric Schrock case ZPOOL_PROP_GUID: 235379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src); 236379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval); 237379c004dSEric Schrock break; 238379c004dSEric Schrock 239379c004dSEric Schrock case ZPOOL_PROP_ALTROOT: 240379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE: 2418704186eSDan McDonald case ZPOOL_PROP_COMMENT: 242379c004dSEric Schrock if (zhp->zpool_props != NULL || 243379c004dSEric Schrock zpool_get_all_props(zhp) == 0) { 244379c004dSEric Schrock (void) strlcpy(buf, 245379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src), 246379c004dSEric Schrock len); 247c58b3526SAdam Stevko break; 248379c004dSEric Schrock } 249379c004dSEric Schrock /* FALLTHROUGH */ 250379c004dSEric Schrock default: 251990b4856Slling (void) strlcpy(buf, "-", len); 252379c004dSEric Schrock break; 253379c004dSEric Schrock } 254379c004dSEric Schrock 255379c004dSEric Schrock if (srctype != NULL) 256379c004dSEric Schrock *srctype = src; 257990b4856Slling return (0); 258990b4856Slling } 259990b4856Slling 260990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 261990b4856Slling prop != ZPOOL_PROP_NAME) 262990b4856Slling return (-1); 263990b4856Slling 264990b4856Slling switch (zpool_prop_get_type(prop)) { 265990b4856Slling case PROP_TYPE_STRING: 266990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 267990b4856Slling len); 268990b4856Slling break; 269990b4856Slling 270990b4856Slling case PROP_TYPE_NUMBER: 271990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 272990b4856Slling 273990b4856Slling switch (prop) { 274990b4856Slling case ZPOOL_PROP_SIZE: 275485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED: 276485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE: 277ad135b5dSChristopher Siden case ZPOOL_PROP_FREEING: 2787fd05ac4SMatthew Ahrens case ZPOOL_PROP_LEAKED: 279c58b3526SAdam Stevko if (literal) { 280c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 281c58b3526SAdam Stevko (u_longlong_t)intval); 282c58b3526SAdam Stevko } else { 283990b4856Slling (void) zfs_nicenum(intval, buf, len); 284c58b3526SAdam Stevko } 285990b4856Slling break; 2867a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ: 2877a09f97bSGeorge Wilson if (intval == 0) { 2887a09f97bSGeorge Wilson (void) strlcpy(buf, "-", len); 2897a09f97bSGeorge Wilson } else if (literal) { 2907a09f97bSGeorge Wilson (void) snprintf(buf, len, "%llu", 2917a09f97bSGeorge Wilson (u_longlong_t)intval); 2927a09f97bSGeorge Wilson } else { 2937a09f97bSGeorge Wilson (void) zfs_nicenum(intval, buf, len); 2947a09f97bSGeorge Wilson } 2957a09f97bSGeorge Wilson break; 296990b4856Slling case ZPOOL_PROP_CAPACITY: 297c58b3526SAdam Stevko if (literal) { 298c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 299c58b3526SAdam Stevko (u_longlong_t)intval); 300c58b3526SAdam Stevko } else { 301990b4856Slling (void) snprintf(buf, len, "%llu%%", 302990b4856Slling (u_longlong_t)intval); 303c58b3526SAdam Stevko } 304990b4856Slling break; 3052e4c9986SGeorge Wilson case ZPOOL_PROP_FRAGMENTATION: 3062e4c9986SGeorge Wilson if (intval == UINT64_MAX) { 3072e4c9986SGeorge Wilson (void) strlcpy(buf, "-", len); 3082e4c9986SGeorge Wilson } else { 3092e4c9986SGeorge Wilson (void) snprintf(buf, len, "%llu%%", 3102e4c9986SGeorge Wilson (u_longlong_t)intval); 3112e4c9986SGeorge Wilson } 3122e4c9986SGeorge Wilson break; 313b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO: 314b24ab676SJeff Bonwick (void) snprintf(buf, len, "%llu.%02llux", 315b24ab676SJeff Bonwick (u_longlong_t)(intval / 100), 316b24ab676SJeff Bonwick (u_longlong_t)(intval % 100)); 317b24ab676SJeff Bonwick break; 318990b4856Slling case ZPOOL_PROP_HEALTH: 319990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 320990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 321990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 3223f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 3233f9d6ad7SLin Ling == 0); 324990b4856Slling 325990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 326990b4856Slling vs->vs_aux), len); 327990b4856Slling break; 328ad135b5dSChristopher Siden case ZPOOL_PROP_VERSION: 329ad135b5dSChristopher Siden if (intval >= SPA_VERSION_FEATURES) { 330ad135b5dSChristopher Siden (void) snprintf(buf, len, "-"); 331ad135b5dSChristopher Siden break; 332ad135b5dSChristopher Siden } 333ad135b5dSChristopher Siden /* FALLTHROUGH */ 334990b4856Slling default: 335990b4856Slling (void) snprintf(buf, len, "%llu", intval); 336990b4856Slling } 337990b4856Slling break; 338990b4856Slling 339990b4856Slling case PROP_TYPE_INDEX: 340990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 341990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 342990b4856Slling != 0) 343990b4856Slling return (-1); 344990b4856Slling (void) strlcpy(buf, strval, len); 345990b4856Slling break; 346990b4856Slling 347990b4856Slling default: 348990b4856Slling abort(); 349990b4856Slling } 350990b4856Slling 351990b4856Slling if (srctype) 352990b4856Slling *srctype = src; 353990b4856Slling 354990b4856Slling return (0); 355990b4856Slling } 356990b4856Slling 357990b4856Slling /* 358990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 359990b4856Slling * Assuming bootfs is a valid dataset name. 360990b4856Slling */ 361990b4856Slling static boolean_t 362990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 363990b4856Slling { 364990b4856Slling int len = strlen(pool); 365990b4856Slling 366fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 367990b4856Slling return (B_FALSE); 368990b4856Slling 369990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 370990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 371990b4856Slling return (B_TRUE); 372990b4856Slling 373990b4856Slling return (B_FALSE); 374990b4856Slling } 375990b4856Slling 3764263d13fSGeorge Wilson boolean_t 3774263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp) 378b5b76fecSGeorge Wilson { 37940a5c998SMatthew Ahrens char bootfs[ZFS_MAX_DATASET_NAME_LEN]; 380b5b76fecSGeorge Wilson 381b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 382c58b3526SAdam Stevko sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-", 383b5b76fecSGeorge Wilson sizeof (bootfs)) != 0); 384b5b76fecSGeorge Wilson } 385b5b76fecSGeorge Wilson 386b5b76fecSGeorge Wilson 38715e6edf1Sgw25295 /* 388990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 389990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 390990b4856Slling * specified as strings. 391990b4856Slling */ 392990b4856Slling static nvlist_t * 3930a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 394f9af39baSGeorge Wilson nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf) 395990b4856Slling { 396990b4856Slling nvpair_t *elem; 397990b4856Slling nvlist_t *retprops; 398990b4856Slling zpool_prop_t prop; 399990b4856Slling char *strval; 400990b4856Slling uint64_t intval; 4018704186eSDan McDonald char *slash, *check; 4022f8aaab3Seschrock struct stat64 statbuf; 40315e6edf1Sgw25295 zpool_handle_t *zhp; 404990b4856Slling 405990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 406990b4856Slling (void) no_memory(hdl); 407990b4856Slling return (NULL); 408990b4856Slling } 409990b4856Slling 410990b4856Slling elem = NULL; 411990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 412990b4856Slling const char *propname = nvpair_name(elem); 413990b4856Slling 414ad135b5dSChristopher Siden prop = zpool_name_to_prop(propname); 415ad135b5dSChristopher Siden if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) { 416ad135b5dSChristopher Siden int err; 417ad135b5dSChristopher Siden char *fname = strchr(propname, '@') + 1; 418ad135b5dSChristopher Siden 4192acef22dSMatthew Ahrens err = zfeature_lookup_name(fname, NULL); 420ad135b5dSChristopher Siden if (err != 0) { 421ad135b5dSChristopher Siden ASSERT3U(err, ==, ENOENT); 422ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 423ad135b5dSChristopher Siden "invalid feature '%s'"), fname); 424ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 425ad135b5dSChristopher Siden goto error; 426ad135b5dSChristopher Siden } 427ad135b5dSChristopher Siden 428ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_STRING) { 429ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 430ad135b5dSChristopher Siden "'%s' must be a string"), propname); 431ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 432ad135b5dSChristopher Siden goto error; 433ad135b5dSChristopher Siden } 434ad135b5dSChristopher Siden 435ad135b5dSChristopher Siden (void) nvpair_value_string(elem, &strval); 436ad135b5dSChristopher Siden if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) { 437ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 438ad135b5dSChristopher Siden "property '%s' can only be set to " 439ad135b5dSChristopher Siden "'enabled'"), propname); 440ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 441ad135b5dSChristopher Siden goto error; 442ad135b5dSChristopher Siden } 443ad135b5dSChristopher Siden 444ad135b5dSChristopher Siden if (nvlist_add_uint64(retprops, propname, 0) != 0) { 445ad135b5dSChristopher Siden (void) no_memory(hdl); 446ad135b5dSChristopher Siden goto error; 447ad135b5dSChristopher Siden } 448ad135b5dSChristopher Siden continue; 449ad135b5dSChristopher Siden } 450ad135b5dSChristopher Siden 451990b4856Slling /* 452990b4856Slling * Make sure this property is valid and applies to this type. 453990b4856Slling */ 454ad135b5dSChristopher Siden if (prop == ZPROP_INVAL) { 455990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 456990b4856Slling "invalid property '%s'"), propname); 457990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 458990b4856Slling goto error; 459990b4856Slling } 460990b4856Slling 461990b4856Slling if (zpool_prop_readonly(prop)) { 462990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 463990b4856Slling "is readonly"), propname); 464990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 465990b4856Slling goto error; 466990b4856Slling } 467990b4856Slling 468990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 469990b4856Slling &strval, &intval, errbuf) != 0) 470990b4856Slling goto error; 471990b4856Slling 472990b4856Slling /* 473990b4856Slling * Perform additional checking for specific properties. 474990b4856Slling */ 475990b4856Slling switch (prop) { 476990b4856Slling case ZPOOL_PROP_VERSION: 477ad135b5dSChristopher Siden if (intval < version || 478ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(intval)) { 479990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 480990b4856Slling "property '%s' number %d is invalid."), 481990b4856Slling propname, intval); 482990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 483990b4856Slling goto error; 484990b4856Slling } 485990b4856Slling break; 486990b4856Slling 487990b4856Slling case ZPOOL_PROP_BOOTFS: 488f9af39baSGeorge Wilson if (flags.create || flags.import) { 489990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 490990b4856Slling "property '%s' cannot be set at creation " 491990b4856Slling "or import time"), propname); 492990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 493990b4856Slling goto error; 494990b4856Slling } 495990b4856Slling 496990b4856Slling if (version < SPA_VERSION_BOOTFS) { 497990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 498990b4856Slling "pool must be upgraded to support " 499990b4856Slling "'%s' property"), propname); 500990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 501990b4856Slling goto error; 502990b4856Slling } 503990b4856Slling 504990b4856Slling /* 505990b4856Slling * bootfs property value has to be a dataset name and 506990b4856Slling * the dataset has to be in the same pool as it sets to. 507990b4856Slling */ 508990b4856Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 509990b4856Slling strval)) { 510990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 511990b4856Slling "is an invalid name"), strval); 512990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 513990b4856Slling goto error; 514990b4856Slling } 51515e6edf1Sgw25295 51615e6edf1Sgw25295 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 51715e6edf1Sgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 51815e6edf1Sgw25295 "could not open pool '%s'"), poolname); 51915e6edf1Sgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 52015e6edf1Sgw25295 goto error; 52115e6edf1Sgw25295 } 52215e6edf1Sgw25295 zpool_close(zhp); 523990b4856Slling break; 524990b4856Slling 525990b4856Slling case ZPOOL_PROP_ALTROOT: 526f9af39baSGeorge Wilson if (!flags.create && !flags.import) { 527990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 528990b4856Slling "property '%s' can only be set during pool " 529990b4856Slling "creation or import"), propname); 530990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 531990b4856Slling goto error; 532990b4856Slling } 533990b4856Slling 534990b4856Slling if (strval[0] != '/') { 535990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 536990b4856Slling "bad alternate root '%s'"), strval); 537990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 538990b4856Slling goto error; 539990b4856Slling } 540990b4856Slling break; 541990b4856Slling 5422f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5432f8aaab3Seschrock if (strval[0] == '\0') 5442f8aaab3Seschrock break; 5452f8aaab3Seschrock 5462f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5472f8aaab3Seschrock break; 5482f8aaab3Seschrock 5492f8aaab3Seschrock if (strval[0] != '/') { 550990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5512f8aaab3Seschrock "property '%s' must be empty, an " 5522f8aaab3Seschrock "absolute path, or 'none'"), propname); 5532f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 554990b4856Slling goto error; 5552f8aaab3Seschrock } 556990b4856Slling 5572f8aaab3Seschrock slash = strrchr(strval, '/'); 5582f8aaab3Seschrock 5592f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5602f8aaab3Seschrock strcmp(slash, "/..") == 0) { 5612f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5622f8aaab3Seschrock "'%s' is not a valid file"), strval); 5632f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 564990b4856Slling goto error; 565990b4856Slling } 5662f8aaab3Seschrock 5672f8aaab3Seschrock *slash = '\0'; 5682f8aaab3Seschrock 5692c32020fSeschrock if (strval[0] != '\0' && 5702c32020fSeschrock (stat64(strval, &statbuf) != 0 || 5712c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 5722f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5732f8aaab3Seschrock "'%s' is not a valid directory"), 5742f8aaab3Seschrock strval); 5752f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5762f8aaab3Seschrock goto error; 5772f8aaab3Seschrock } 5782f8aaab3Seschrock 5792f8aaab3Seschrock *slash = '/'; 5802f8aaab3Seschrock break; 581f9af39baSGeorge Wilson 5828704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5838704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5848704186eSDan McDonald if (!isprint(*check)) { 5858704186eSDan McDonald zfs_error_aux(hdl, 5868704186eSDan McDonald dgettext(TEXT_DOMAIN, 5878704186eSDan McDonald "comment may only have printable " 5888704186eSDan McDonald "characters")); 5898704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, 5908704186eSDan McDonald errbuf); 5918704186eSDan McDonald goto error; 5928704186eSDan McDonald } 5938704186eSDan McDonald } 5948704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) { 5958704186eSDan McDonald zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5968704186eSDan McDonald "comment must not exceed %d characters"), 5978704186eSDan McDonald ZPROP_MAX_COMMENT); 5988704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5998704186eSDan McDonald goto error; 6008704186eSDan McDonald } 6018704186eSDan McDonald break; 602f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 603f9af39baSGeorge Wilson if (!flags.import) { 604f9af39baSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 605f9af39baSGeorge Wilson "property '%s' can only be set at " 606f9af39baSGeorge Wilson "import time"), propname); 607f9af39baSGeorge Wilson (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 608f9af39baSGeorge Wilson goto error; 609f9af39baSGeorge Wilson } 610f9af39baSGeorge Wilson break; 6112f8aaab3Seschrock } 612990b4856Slling } 613990b4856Slling 614990b4856Slling return (retprops); 615990b4856Slling error: 616990b4856Slling nvlist_free(retprops); 617990b4856Slling return (NULL); 618990b4856Slling } 619990b4856Slling 620990b4856Slling /* 621990b4856Slling * Set zpool property : propname=propval. 622990b4856Slling */ 623990b4856Slling int 624990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 625990b4856Slling { 626990b4856Slling zfs_cmd_t zc = { 0 }; 627990b4856Slling int ret = -1; 628990b4856Slling char errbuf[1024]; 629990b4856Slling nvlist_t *nvl = NULL; 630990b4856Slling nvlist_t *realprops; 631990b4856Slling uint64_t version; 632f9af39baSGeorge Wilson prop_flags_t flags = { 0 }; 633990b4856Slling 634990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 635990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 636990b4856Slling zhp->zpool_name); 637990b4856Slling 638990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 639990b4856Slling return (no_memory(zhp->zpool_hdl)); 640990b4856Slling 641990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 642990b4856Slling nvlist_free(nvl); 643990b4856Slling return (no_memory(zhp->zpool_hdl)); 644990b4856Slling } 645990b4856Slling 646990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 6470a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 648f9af39baSGeorge Wilson zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) { 649990b4856Slling nvlist_free(nvl); 650990b4856Slling return (-1); 651990b4856Slling } 652990b4856Slling 653990b4856Slling nvlist_free(nvl); 654990b4856Slling nvl = realprops; 655990b4856Slling 656990b4856Slling /* 657990b4856Slling * Execute the corresponding ioctl() to set this property. 658990b4856Slling */ 659990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 660990b4856Slling 661990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 662990b4856Slling nvlist_free(nvl); 663990b4856Slling return (-1); 664990b4856Slling } 665990b4856Slling 666990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 667990b4856Slling 668990b4856Slling zcmd_free_nvlists(&zc); 669990b4856Slling nvlist_free(nvl); 670990b4856Slling 671990b4856Slling if (ret) 672990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 673990b4856Slling else 674990b4856Slling (void) zpool_props_refresh(zhp); 675990b4856Slling 676990b4856Slling return (ret); 677990b4856Slling } 678990b4856Slling 679990b4856Slling int 680990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 681990b4856Slling { 682990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 683990b4856Slling zprop_list_t *entry; 684990b4856Slling char buf[ZFS_MAXPROPLEN]; 685ad135b5dSChristopher Siden nvlist_t *features = NULL; 686ad135b5dSChristopher Siden zprop_list_t **last; 687ad135b5dSChristopher Siden boolean_t firstexpand = (NULL == *plp); 688990b4856Slling 689990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 690990b4856Slling return (-1); 691990b4856Slling 692ad135b5dSChristopher Siden last = plp; 693ad135b5dSChristopher Siden while (*last != NULL) 694ad135b5dSChristopher Siden last = &(*last)->pl_next; 695ad135b5dSChristopher Siden 696ad135b5dSChristopher Siden if ((*plp)->pl_all) 697ad135b5dSChristopher Siden features = zpool_get_features(zhp); 698ad135b5dSChristopher Siden 699ad135b5dSChristopher Siden if ((*plp)->pl_all && firstexpand) { 700ad135b5dSChristopher Siden for (int i = 0; i < SPA_FEATURES; i++) { 701ad135b5dSChristopher Siden zprop_list_t *entry = zfs_alloc(hdl, 702ad135b5dSChristopher Siden sizeof (zprop_list_t)); 703ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 704ad135b5dSChristopher Siden entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s", 705ad135b5dSChristopher Siden spa_feature_table[i].fi_uname); 706ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 707ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 708ad135b5dSChristopher Siden 709ad135b5dSChristopher Siden *last = entry; 710ad135b5dSChristopher Siden last = &entry->pl_next; 711ad135b5dSChristopher Siden } 712ad135b5dSChristopher Siden } 713ad135b5dSChristopher Siden 714ad135b5dSChristopher Siden /* add any unsupported features */ 715ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL); 716ad135b5dSChristopher Siden nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) { 717ad135b5dSChristopher Siden char *propname; 718ad135b5dSChristopher Siden boolean_t found; 719ad135b5dSChristopher Siden zprop_list_t *entry; 720ad135b5dSChristopher Siden 721ad135b5dSChristopher Siden if (zfeature_is_supported(nvpair_name(nvp))) 722ad135b5dSChristopher Siden continue; 723ad135b5dSChristopher Siden 724ad135b5dSChristopher Siden propname = zfs_asprintf(hdl, "unsupported@%s", 725ad135b5dSChristopher Siden nvpair_name(nvp)); 726ad135b5dSChristopher Siden 727ad135b5dSChristopher Siden /* 728ad135b5dSChristopher Siden * Before adding the property to the list make sure that no 729ad135b5dSChristopher Siden * other pool already added the same property. 730ad135b5dSChristopher Siden */ 731ad135b5dSChristopher Siden found = B_FALSE; 732ad135b5dSChristopher Siden entry = *plp; 733ad135b5dSChristopher Siden while (entry != NULL) { 734ad135b5dSChristopher Siden if (entry->pl_user_prop != NULL && 735ad135b5dSChristopher Siden strcmp(propname, entry->pl_user_prop) == 0) { 736ad135b5dSChristopher Siden found = B_TRUE; 737ad135b5dSChristopher Siden break; 738ad135b5dSChristopher Siden } 739ad135b5dSChristopher Siden entry = entry->pl_next; 740ad135b5dSChristopher Siden } 741ad135b5dSChristopher Siden if (found) { 742ad135b5dSChristopher Siden free(propname); 743ad135b5dSChristopher Siden continue; 744ad135b5dSChristopher Siden } 745ad135b5dSChristopher Siden 746ad135b5dSChristopher Siden entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 747ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 748ad135b5dSChristopher Siden entry->pl_user_prop = propname; 749ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 750ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 751ad135b5dSChristopher Siden 752ad135b5dSChristopher Siden *last = entry; 753ad135b5dSChristopher Siden last = &entry->pl_next; 754ad135b5dSChristopher Siden } 755ad135b5dSChristopher Siden 756990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 757990b4856Slling 758990b4856Slling if (entry->pl_fixed) 759990b4856Slling continue; 760990b4856Slling 761990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 762990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 763c58b3526SAdam Stevko NULL, B_FALSE) == 0) { 764990b4856Slling if (strlen(buf) > entry->pl_width) 765990b4856Slling entry->pl_width = strlen(buf); 766990b4856Slling } 767990b4856Slling } 768990b4856Slling 769990b4856Slling return (0); 770990b4856Slling } 771990b4856Slling 772ad135b5dSChristopher Siden /* 773ad135b5dSChristopher Siden * Get the state for the given feature on the given ZFS pool. 774ad135b5dSChristopher Siden */ 775ad135b5dSChristopher Siden int 776ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf, 777ad135b5dSChristopher Siden size_t len) 778ad135b5dSChristopher Siden { 779ad135b5dSChristopher Siden uint64_t refcount; 780ad135b5dSChristopher Siden boolean_t found = B_FALSE; 781ad135b5dSChristopher Siden nvlist_t *features = zpool_get_features(zhp); 782ad135b5dSChristopher Siden boolean_t supported; 783ad135b5dSChristopher Siden const char *feature = strchr(propname, '@') + 1; 784ad135b5dSChristopher Siden 785ad135b5dSChristopher Siden supported = zpool_prop_feature(propname); 786ad135b5dSChristopher Siden ASSERT(supported || zfs_prop_unsupported(propname)); 787ad135b5dSChristopher Siden 788ad135b5dSChristopher Siden /* 789ad135b5dSChristopher Siden * Convert from feature name to feature guid. This conversion is 790ad135b5dSChristopher Siden * unecessary for unsupported@... properties because they already 791ad135b5dSChristopher Siden * use guids. 792ad135b5dSChristopher Siden */ 793ad135b5dSChristopher Siden if (supported) { 794ad135b5dSChristopher Siden int ret; 7952acef22dSMatthew Ahrens spa_feature_t fid; 796ad135b5dSChristopher Siden 7972acef22dSMatthew Ahrens ret = zfeature_lookup_name(feature, &fid); 798ad135b5dSChristopher Siden if (ret != 0) { 799ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 800ad135b5dSChristopher Siden return (ENOTSUP); 801ad135b5dSChristopher Siden } 8022acef22dSMatthew Ahrens feature = spa_feature_table[fid].fi_guid; 803ad135b5dSChristopher Siden } 804ad135b5dSChristopher Siden 805ad135b5dSChristopher Siden if (nvlist_lookup_uint64(features, feature, &refcount) == 0) 806ad135b5dSChristopher Siden found = B_TRUE; 807ad135b5dSChristopher Siden 808ad135b5dSChristopher Siden if (supported) { 809ad135b5dSChristopher Siden if (!found) { 810ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len); 811ad135b5dSChristopher Siden } else { 812ad135b5dSChristopher Siden if (refcount == 0) 813ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len); 814ad135b5dSChristopher Siden else 815ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len); 816ad135b5dSChristopher Siden } 817ad135b5dSChristopher Siden } else { 818ad135b5dSChristopher Siden if (found) { 819ad135b5dSChristopher Siden if (refcount == 0) { 820ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE); 821ad135b5dSChristopher Siden } else { 822ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY); 823ad135b5dSChristopher Siden } 824ad135b5dSChristopher Siden } else { 825ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 826ad135b5dSChristopher Siden return (ENOTSUP); 827ad135b5dSChristopher Siden } 828ad135b5dSChristopher Siden } 829ad135b5dSChristopher Siden 830ad135b5dSChristopher Siden return (0); 831ad135b5dSChristopher Siden } 832990b4856Slling 833fa9e4066Sahrens /* 834573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage 835573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead. 836573ca77eSGeorge Wilson */ 837573ca77eSGeorge Wilson #define NEW_START_BLOCK 256 838573ca77eSGeorge Wilson 839573ca77eSGeorge Wilson /* 840fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 841fa9e4066Sahrens * 'buf'. 842fa9e4066Sahrens */ 843e7cbe64fSgw25295 boolean_t 84499653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 845fa9e4066Sahrens { 846fa9e4066Sahrens namecheck_err_t why; 847fa9e4066Sahrens char what; 848b468a217Seschrock int ret; 849fa9e4066Sahrens 850b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 851b468a217Seschrock 852b468a217Seschrock /* 853b468a217Seschrock * The rules for reserved pool names were extended at a later point. 854b468a217Seschrock * But we need to support users with existing pools that may now be 855b468a217Seschrock * invalid. So we only check for this expanded set of names during a 856b468a217Seschrock * create (or import), and only in userland. 857b468a217Seschrock */ 858b468a217Seschrock if (ret == 0 && !isopen && 859b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 860b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 8618654d025Sperrin strncmp(pool, "spare", 5) == 0 || 8628654d025Sperrin strcmp(pool, "log") == 0)) { 863e7cbe64fSgw25295 if (hdl != NULL) 86499653d4eSeschrock zfs_error_aux(hdl, 86599653d4eSeschrock dgettext(TEXT_DOMAIN, "name is reserved")); 86699653d4eSeschrock return (B_FALSE); 867b468a217Seschrock } 868b468a217Seschrock 869b468a217Seschrock 870b468a217Seschrock if (ret != 0) { 87199653d4eSeschrock if (hdl != NULL) { 872fa9e4066Sahrens switch (why) { 873b81d61a6Slling case NAME_ERR_TOOLONG: 87499653d4eSeschrock zfs_error_aux(hdl, 875b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 876b81d61a6Slling break; 877b81d61a6Slling 878fa9e4066Sahrens case NAME_ERR_INVALCHAR: 87999653d4eSeschrock zfs_error_aux(hdl, 880fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 881fa9e4066Sahrens "'%c' in pool name"), what); 882fa9e4066Sahrens break; 883fa9e4066Sahrens 884fa9e4066Sahrens case NAME_ERR_NOLETTER: 88599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 88699653d4eSeschrock "name must begin with a letter")); 887fa9e4066Sahrens break; 888fa9e4066Sahrens 889fa9e4066Sahrens case NAME_ERR_RESERVED: 89099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 89199653d4eSeschrock "name is reserved")); 892fa9e4066Sahrens break; 893fa9e4066Sahrens 894fa9e4066Sahrens case NAME_ERR_DISKLIKE: 89599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 89699653d4eSeschrock "pool name is reserved")); 897fa9e4066Sahrens break; 8985ad82045Snd150628 8995ad82045Snd150628 case NAME_ERR_LEADING_SLASH: 9005ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9015ad82045Snd150628 "leading slash in name")); 9025ad82045Snd150628 break; 9035ad82045Snd150628 9045ad82045Snd150628 case NAME_ERR_EMPTY_COMPONENT: 9055ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9065ad82045Snd150628 "empty component in name")); 9075ad82045Snd150628 break; 9085ad82045Snd150628 9095ad82045Snd150628 case NAME_ERR_TRAILING_SLASH: 9105ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9115ad82045Snd150628 "trailing slash in name")); 9125ad82045Snd150628 break; 9135ad82045Snd150628 914fbc66171SMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS: 9155ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 916fbc66171SMarcel Telka "multiple '@' and/or '#' delimiters in " 917fbc66171SMarcel Telka "name")); 9185ad82045Snd150628 break; 9195ad82045Snd150628 920fa9e4066Sahrens } 921fa9e4066Sahrens } 92299653d4eSeschrock return (B_FALSE); 923fa9e4066Sahrens } 924fa9e4066Sahrens 92599653d4eSeschrock return (B_TRUE); 926fa9e4066Sahrens } 927fa9e4066Sahrens 928fa9e4066Sahrens /* 929fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 930fa9e4066Sahrens * state. 931fa9e4066Sahrens */ 932fa9e4066Sahrens zpool_handle_t * 93399653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 934fa9e4066Sahrens { 935fa9e4066Sahrens zpool_handle_t *zhp; 93694de1d4cSeschrock boolean_t missing; 937fa9e4066Sahrens 938fa9e4066Sahrens /* 939fa9e4066Sahrens * Make sure the pool name is valid. 940fa9e4066Sahrens */ 94199653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 942ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 94399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 94499653d4eSeschrock pool); 945fa9e4066Sahrens return (NULL); 946fa9e4066Sahrens } 947fa9e4066Sahrens 94899653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 94999653d4eSeschrock return (NULL); 950fa9e4066Sahrens 95199653d4eSeschrock zhp->zpool_hdl = hdl; 952fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 953fa9e4066Sahrens 95494de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 95594de1d4cSeschrock zpool_close(zhp); 95694de1d4cSeschrock return (NULL); 95794de1d4cSeschrock } 95894de1d4cSeschrock 95994de1d4cSeschrock if (missing) { 960990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 961ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 962990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 96394de1d4cSeschrock zpool_close(zhp); 964fa9e4066Sahrens return (NULL); 965fa9e4066Sahrens } 966fa9e4066Sahrens 967fa9e4066Sahrens return (zhp); 968fa9e4066Sahrens } 969fa9e4066Sahrens 970fa9e4066Sahrens /* 971fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 972fa9e4066Sahrens * the configuration cache may be out of date). 973fa9e4066Sahrens */ 97494de1d4cSeschrock int 97594de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 976fa9e4066Sahrens { 977fa9e4066Sahrens zpool_handle_t *zhp; 97894de1d4cSeschrock boolean_t missing; 979fa9e4066Sahrens 98094de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 98194de1d4cSeschrock return (-1); 982fa9e4066Sahrens 98399653d4eSeschrock zhp->zpool_hdl = hdl; 984fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 985fa9e4066Sahrens 98694de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 98794de1d4cSeschrock zpool_close(zhp); 98894de1d4cSeschrock return (-1); 989fa9e4066Sahrens } 990fa9e4066Sahrens 99194de1d4cSeschrock if (missing) { 99294de1d4cSeschrock zpool_close(zhp); 99394de1d4cSeschrock *ret = NULL; 99494de1d4cSeschrock return (0); 99594de1d4cSeschrock } 99694de1d4cSeschrock 99794de1d4cSeschrock *ret = zhp; 99894de1d4cSeschrock return (0); 999fa9e4066Sahrens } 1000fa9e4066Sahrens 1001fa9e4066Sahrens /* 1002fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 1003fa9e4066Sahrens * state. 1004fa9e4066Sahrens */ 1005fa9e4066Sahrens zpool_handle_t * 100699653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 1007fa9e4066Sahrens { 1008fa9e4066Sahrens zpool_handle_t *zhp; 1009fa9e4066Sahrens 101099653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 1011fa9e4066Sahrens return (NULL); 1012fa9e4066Sahrens 1013fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 1014ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 101599653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 1016fa9e4066Sahrens zpool_close(zhp); 1017fa9e4066Sahrens return (NULL); 1018fa9e4066Sahrens } 1019fa9e4066Sahrens 1020fa9e4066Sahrens return (zhp); 1021fa9e4066Sahrens } 1022fa9e4066Sahrens 1023fa9e4066Sahrens /* 1024fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 1025fa9e4066Sahrens */ 1026fa9e4066Sahrens void 1027fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 1028fa9e4066Sahrens { 1029fa9e4066Sahrens nvlist_free(zhp->zpool_config); 1030088e9d47Seschrock nvlist_free(zhp->zpool_old_config); 1031b1b8ab34Slling nvlist_free(zhp->zpool_props); 1032fa9e4066Sahrens free(zhp); 1033fa9e4066Sahrens } 1034fa9e4066Sahrens 1035fa9e4066Sahrens /* 1036fa9e4066Sahrens * Return the name of the pool. 1037fa9e4066Sahrens */ 1038fa9e4066Sahrens const char * 1039fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 1040fa9e4066Sahrens { 1041fa9e4066Sahrens return (zhp->zpool_name); 1042fa9e4066Sahrens } 1043fa9e4066Sahrens 1044fa9e4066Sahrens 1045fa9e4066Sahrens /* 1046fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 1047fa9e4066Sahrens */ 1048fa9e4066Sahrens int 1049fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 1050fa9e4066Sahrens { 1051fa9e4066Sahrens return (zhp->zpool_state); 1052fa9e4066Sahrens } 1053fa9e4066Sahrens 1054fa9e4066Sahrens /* 1055fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 1056fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 1057fa9e4066Sahrens * don't have to worry about error semantics. 1058fa9e4066Sahrens */ 1059fa9e4066Sahrens int 106099653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 10610a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 1062fa9e4066Sahrens { 1063fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 10640a48a24eStimh nvlist_t *zc_fsprops = NULL; 10650a48a24eStimh nvlist_t *zc_props = NULL; 106699653d4eSeschrock char msg[1024]; 10670a48a24eStimh int ret = -1; 1068fa9e4066Sahrens 106999653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 107099653d4eSeschrock "cannot create '%s'"), pool); 107199653d4eSeschrock 107299653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 107399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 107499653d4eSeschrock 1075351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1076351420b3Slling return (-1); 1077351420b3Slling 10780a48a24eStimh if (props) { 1079f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE }; 1080f9af39baSGeorge Wilson 10810a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 1082f9af39baSGeorge Wilson SPA_VERSION_1, flags, msg)) == NULL) { 10830a48a24eStimh goto create_failed; 1084351420b3Slling } 10850a48a24eStimh } 10860a48a24eStimh 10870a48a24eStimh if (fsprops) { 10880a48a24eStimh uint64_t zoned; 10890a48a24eStimh char *zonestr; 10900a48a24eStimh 10910a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 10920a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 10930a48a24eStimh strcmp(zonestr, "on") == 0); 10940a48a24eStimh 1095e9316f76SJoe Stein if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM, 1096e9316f76SJoe Stein fsprops, zoned, NULL, NULL, msg)) == NULL) { 10970a48a24eStimh goto create_failed; 10980a48a24eStimh } 10990a48a24eStimh if (!zc_props && 11000a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 11010a48a24eStimh goto create_failed; 11020a48a24eStimh } 11030a48a24eStimh if (nvlist_add_nvlist(zc_props, 11040a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 11050a48a24eStimh goto create_failed; 11060a48a24eStimh } 11070a48a24eStimh } 11080a48a24eStimh 11090a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 11100a48a24eStimh goto create_failed; 111199653d4eSeschrock 1112fa9e4066Sahrens (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 1113fa9e4066Sahrens 11140a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 1115351420b3Slling 1116e9dbad6fSeschrock zcmd_free_nvlists(&zc); 11170a48a24eStimh nvlist_free(zc_props); 11180a48a24eStimh nvlist_free(zc_fsprops); 111999653d4eSeschrock 1120fa9e4066Sahrens switch (errno) { 1121fa9e4066Sahrens case EBUSY: 1122fa9e4066Sahrens /* 1123fa9e4066Sahrens * This can happen if the user has specified the same 1124fa9e4066Sahrens * device multiple times. We can't reliably detect this 1125fa9e4066Sahrens * until we try to add it and see we already have a 1126fa9e4066Sahrens * label. 1127fa9e4066Sahrens */ 112899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 112999653d4eSeschrock "one or more vdevs refer to the same device")); 113099653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1131fa9e4066Sahrens 1132e9316f76SJoe Stein case ERANGE: 1133e9316f76SJoe Stein /* 1134e9316f76SJoe Stein * This happens if the record size is smaller or larger 1135e9316f76SJoe Stein * than the allowed size range, or not a power of 2. 1136e9316f76SJoe Stein * 1137e9316f76SJoe Stein * NOTE: although zfs_valid_proplist is called earlier, 1138e9316f76SJoe Stein * this case may have slipped through since the 1139e9316f76SJoe Stein * pool does not exist yet and it is therefore 1140e9316f76SJoe Stein * impossible to read properties e.g. max blocksize 1141e9316f76SJoe Stein * from the pool. 1142e9316f76SJoe Stein */ 1143e9316f76SJoe Stein zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1144e9316f76SJoe Stein "record size invalid")); 1145e9316f76SJoe Stein return (zfs_error(hdl, EZFS_BADPROP, msg)); 1146e9316f76SJoe Stein 1147fa9e4066Sahrens case EOVERFLOW: 1148fa9e4066Sahrens /* 114999653d4eSeschrock * This occurs when one of the devices is below 1150fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1151fa9e4066Sahrens * device was the problem device since there's no 1152fa9e4066Sahrens * reliable way to determine device size from userland. 1153fa9e4066Sahrens */ 1154fa9e4066Sahrens { 1155fa9e4066Sahrens char buf[64]; 1156fa9e4066Sahrens 1157fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1158fa9e4066Sahrens 115999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 116099653d4eSeschrock "one or more devices is less than the " 116199653d4eSeschrock "minimum size (%s)"), buf); 1162fa9e4066Sahrens } 116399653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1164fa9e4066Sahrens 1165fa9e4066Sahrens case ENOSPC: 116699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 116799653d4eSeschrock "one or more devices is out of space")); 116899653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1169fa9e4066Sahrens 1170fa94a07fSbrendan case ENOTBLK: 1171fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1172fa94a07fSbrendan "cache device must be a disk or disk slice")); 1173fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 1174fa94a07fSbrendan 1175fa9e4066Sahrens default: 117699653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1177fa9e4066Sahrens } 1178fa9e4066Sahrens } 1179fa9e4066Sahrens 11800a48a24eStimh create_failed: 1181351420b3Slling zcmd_free_nvlists(&zc); 11820a48a24eStimh nvlist_free(zc_props); 11830a48a24eStimh nvlist_free(zc_fsprops); 11840a48a24eStimh return (ret); 1185fa9e4066Sahrens } 1186fa9e4066Sahrens 1187fa9e4066Sahrens /* 1188fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 1189fa9e4066Sahrens * datasets left in the pool. 1190fa9e4066Sahrens */ 1191fa9e4066Sahrens int 11924445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str) 1193fa9e4066Sahrens { 1194fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1195fa9e4066Sahrens zfs_handle_t *zfp = NULL; 119699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 119799653d4eSeschrock char msg[1024]; 1198fa9e4066Sahrens 1199fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 1200cb04b873SMark J Musante (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1201fa9e4066Sahrens return (-1); 1202fa9e4066Sahrens 1203fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 12044445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 1205fa9e4066Sahrens 1206cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 120799653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 120899653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 1209fa9e4066Sahrens 121099653d4eSeschrock if (errno == EROFS) { 121199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 121299653d4eSeschrock "one or more devices is read only")); 121399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 121499653d4eSeschrock } else { 121599653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1216fa9e4066Sahrens } 1217fa9e4066Sahrens 1218fa9e4066Sahrens if (zfp) 1219fa9e4066Sahrens zfs_close(zfp); 1220fa9e4066Sahrens return (-1); 1221fa9e4066Sahrens } 1222fa9e4066Sahrens 1223fa9e4066Sahrens if (zfp) { 1224fa9e4066Sahrens remove_mountpoint(zfp); 1225fa9e4066Sahrens zfs_close(zfp); 1226fa9e4066Sahrens } 1227fa9e4066Sahrens 1228fa9e4066Sahrens return (0); 1229fa9e4066Sahrens } 1230fa9e4066Sahrens 1231fa9e4066Sahrens /* 1232fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1233fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1234fa9e4066Sahrens */ 1235fa9e4066Sahrens int 1236fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1237fa9e4066Sahrens { 1238e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 123999653d4eSeschrock int ret; 124099653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 124199653d4eSeschrock char msg[1024]; 1242fa94a07fSbrendan nvlist_t **spares, **l2cache; 1243fa94a07fSbrendan uint_t nspares, nl2cache; 124499653d4eSeschrock 124599653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 124699653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 124799653d4eSeschrock 1248fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1249fa94a07fSbrendan SPA_VERSION_SPARES && 125099653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 125199653d4eSeschrock &spares, &nspares) == 0) { 125299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 125399653d4eSeschrock "upgraded to add hot spares")); 125499653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 125599653d4eSeschrock } 1256fa9e4066Sahrens 1257fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1258fa94a07fSbrendan SPA_VERSION_L2CACHE && 1259fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1260fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1261fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1262fa94a07fSbrendan "upgraded to add cache devices")); 1263fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1264fa94a07fSbrendan } 1265fa94a07fSbrendan 1266990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 126799653d4eSeschrock return (-1); 1268fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1269fa9e4066Sahrens 1270cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1271fa9e4066Sahrens switch (errno) { 1272fa9e4066Sahrens case EBUSY: 1273fa9e4066Sahrens /* 1274fa9e4066Sahrens * This can happen if the user has specified the same 1275fa9e4066Sahrens * device multiple times. We can't reliably detect this 1276fa9e4066Sahrens * until we try to add it and see we already have a 1277fa9e4066Sahrens * label. 1278fa9e4066Sahrens */ 127999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 128099653d4eSeschrock "one or more vdevs refer to the same device")); 128199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1282fa9e4066Sahrens break; 1283fa9e4066Sahrens 1284fa9e4066Sahrens case EOVERFLOW: 1285fa9e4066Sahrens /* 1286fa9e4066Sahrens * This occurrs when one of the devices is below 1287fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1288fa9e4066Sahrens * device was the problem device since there's no 1289fa9e4066Sahrens * reliable way to determine device size from userland. 1290fa9e4066Sahrens */ 1291fa9e4066Sahrens { 1292fa9e4066Sahrens char buf[64]; 1293fa9e4066Sahrens 1294fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1295fa9e4066Sahrens 129699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 129799653d4eSeschrock "device is less than the minimum " 129899653d4eSeschrock "size (%s)"), buf); 1299fa9e4066Sahrens } 130099653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 130199653d4eSeschrock break; 130299653d4eSeschrock 130399653d4eSeschrock case ENOTSUP: 130499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13058654d025Sperrin "pool must be upgraded to add these vdevs")); 130699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1307fa9e4066Sahrens break; 1308fa9e4066Sahrens 1309b1b8ab34Slling case EDOM: 1310b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13118654d025Sperrin "root pool can not have multiple vdevs" 13128654d025Sperrin " or separate logs")); 1313b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1314b1b8ab34Slling break; 1315b1b8ab34Slling 1316fa94a07fSbrendan case ENOTBLK: 1317fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1318fa94a07fSbrendan "cache device must be a disk or disk slice")); 1319fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1320fa94a07fSbrendan break; 1321fa94a07fSbrendan 1322fa9e4066Sahrens default: 132399653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1324fa9e4066Sahrens } 1325fa9e4066Sahrens 132699653d4eSeschrock ret = -1; 132799653d4eSeschrock } else { 132899653d4eSeschrock ret = 0; 1329fa9e4066Sahrens } 1330fa9e4066Sahrens 1331e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1332fa9e4066Sahrens 133399653d4eSeschrock return (ret); 1334fa9e4066Sahrens } 1335fa9e4066Sahrens 1336fa9e4066Sahrens /* 1337fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1338fa9e4066Sahrens * mounted datasets in the pool. 1339fa9e4066Sahrens */ 13404445fffbSMatthew Ahrens static int 13414445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce, 13424445fffbSMatthew Ahrens const char *log_str) 1343fa9e4066Sahrens { 1344fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 134589a89ebfSlling char msg[1024]; 1346fa9e4066Sahrens 134789a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 134889a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 1349fa9e4066Sahrens 135089a89ebfSlling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 135189a89ebfSlling zc.zc_cookie = force; 1352394ab0cbSGeorge Wilson zc.zc_guid = hardforce; 13534445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 135489a89ebfSlling 135589a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 135689a89ebfSlling switch (errno) { 135789a89ebfSlling case EXDEV: 135889a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 135989a89ebfSlling "use '-f' to override the following errors:\n" 136089a89ebfSlling "'%s' has an active shared spare which could be" 136189a89ebfSlling " used by other pools once '%s' is exported."), 136289a89ebfSlling zhp->zpool_name, zhp->zpool_name); 136389a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 136489a89ebfSlling msg)); 136589a89ebfSlling default: 1366ece3d9b3Slling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 136789a89ebfSlling msg)); 136889a89ebfSlling } 136989a89ebfSlling } 137089a89ebfSlling 1371fa9e4066Sahrens return (0); 1372fa9e4066Sahrens } 1373fa9e4066Sahrens 1374394ab0cbSGeorge Wilson int 13754445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str) 1376394ab0cbSGeorge Wilson { 13774445fffbSMatthew Ahrens return (zpool_export_common(zhp, force, B_FALSE, log_str)); 1378394ab0cbSGeorge Wilson } 1379394ab0cbSGeorge Wilson 1380394ab0cbSGeorge Wilson int 13814445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str) 1382394ab0cbSGeorge Wilson { 13834445fffbSMatthew Ahrens return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str)); 1384394ab0cbSGeorge Wilson } 1385394ab0cbSGeorge Wilson 1386468c413aSTim Haley static void 1387468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 13884b964adaSGeorge Wilson nvlist_t *config) 1389468c413aSTim Haley { 13904b964adaSGeorge Wilson nvlist_t *nv = NULL; 1391468c413aSTim Haley uint64_t rewindto; 1392468c413aSTim Haley int64_t loss = -1; 1393468c413aSTim Haley struct tm t; 1394468c413aSTim Haley char timestr[128]; 1395468c413aSTim Haley 13964b964adaSGeorge Wilson if (!hdl->libzfs_printerr || config == NULL) 1397468c413aSTim Haley return; 1398468c413aSTim Haley 1399ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1400ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) { 1401468c413aSTim Haley return; 1402ad135b5dSChristopher Siden } 14034b964adaSGeorge Wilson 14044b964adaSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 14054b964adaSGeorge Wilson return; 14064b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1407468c413aSTim Haley 1408468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1409468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1410468c413aSTim Haley if (dryrun) { 1411468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1412468c413aSTim Haley "Would be able to return %s " 1413468c413aSTim Haley "to its state as of %s.\n"), 1414468c413aSTim Haley name, timestr); 1415468c413aSTim Haley } else { 1416468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1417468c413aSTim Haley "Pool %s returned to its state as of %s.\n"), 1418468c413aSTim Haley name, timestr); 1419468c413aSTim Haley } 1420468c413aSTim Haley if (loss > 120) { 1421468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1422468c413aSTim Haley "%s approximately %lld "), 1423468c413aSTim Haley dryrun ? "Would discard" : "Discarded", 1424468c413aSTim Haley (loss + 30) / 60); 1425468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1426468c413aSTim Haley "minutes of transactions.\n")); 1427468c413aSTim Haley } else if (loss > 0) { 1428468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1429468c413aSTim Haley "%s approximately %lld "), 1430468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss); 1431468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1432468c413aSTim Haley "seconds of transactions.\n")); 1433468c413aSTim Haley } 1434468c413aSTim Haley } 1435468c413aSTim Haley } 1436468c413aSTim Haley 1437468c413aSTim Haley void 1438468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1439468c413aSTim Haley nvlist_t *config) 1440468c413aSTim Haley { 14414b964adaSGeorge Wilson nvlist_t *nv = NULL; 1442468c413aSTim Haley int64_t loss = -1; 1443468c413aSTim Haley uint64_t edata = UINT64_MAX; 1444468c413aSTim Haley uint64_t rewindto; 1445468c413aSTim Haley struct tm t; 1446468c413aSTim Haley char timestr[128]; 1447468c413aSTim Haley 1448468c413aSTim Haley if (!hdl->libzfs_printerr) 1449468c413aSTim Haley return; 1450468c413aSTim Haley 1451468c413aSTim Haley if (reason >= 0) 1452468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1453468c413aSTim Haley else 1454468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1455468c413aSTim Haley 1456468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 14574b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1458ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 || 14594b964adaSGeorge Wilson nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1460468c413aSTim Haley goto no_info; 1461468c413aSTim Haley 14624b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 14634b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1464468c413aSTim Haley &edata); 1465468c413aSTim Haley 1466468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1467468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n")); 1468468c413aSTim Haley 1469468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1470468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1471468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1472468c413aSTim Haley "\tReturning the pool to its state as of %s\n" 1473468c413aSTim Haley "\tshould correct the problem. "), 1474468c413aSTim Haley timestr); 1475468c413aSTim Haley } else { 1476468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1477468c413aSTim Haley "\tReverting the pool to an earlier state " 1478468c413aSTim Haley "should correct the problem.\n\t")); 1479468c413aSTim Haley } 1480468c413aSTim Haley 1481468c413aSTim Haley if (loss > 120) { 1482468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1483468c413aSTim Haley "Approximately %lld minutes of data\n" 1484468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1485468c413aSTim Haley } else if (loss > 0) { 1486468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1487468c413aSTim Haley "Approximately %lld seconds of data\n" 1488468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss); 1489468c413aSTim Haley } 1490468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) { 1491468c413aSTim Haley if (edata == 1) { 1492468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1493468c413aSTim Haley "After rewind, at least\n" 1494468c413aSTim Haley "\tone persistent user-data error will remain. ")); 1495468c413aSTim Haley } else { 1496468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1497468c413aSTim Haley "After rewind, several\n" 1498468c413aSTim Haley "\tpersistent user-data errors will remain. ")); 1499468c413aSTim Haley } 1500468c413aSTim Haley } 1501468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1502a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1503a33cae98STim Haley reason >= 0 ? "clear" : "import", name); 1504468c413aSTim Haley 1505468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1506468c413aSTim Haley "A scrub of the pool\n" 1507468c413aSTim Haley "\tis strongly recommended after recovery.\n")); 1508468c413aSTim Haley return; 1509468c413aSTim Haley 1510468c413aSTim Haley no_info: 1511468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1512468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n")); 1513468c413aSTim Haley } 1514468c413aSTim Haley 1515fa9e4066Sahrens /* 1516990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1517990b4856Slling * if possible. 1518990b4856Slling * 1519990b4856Slling * Applications should use zpool_import_props() to import a pool with 1520990b4856Slling * new properties value to be set. 1521fa9e4066Sahrens */ 1522fa9e4066Sahrens int 152399653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1524990b4856Slling char *altroot) 1525990b4856Slling { 1526990b4856Slling nvlist_t *props = NULL; 1527990b4856Slling int ret; 1528990b4856Slling 1529990b4856Slling if (altroot != NULL) { 1530990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1531990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1532990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1533990b4856Slling newname)); 1534990b4856Slling } 1535990b4856Slling 1536990b4856Slling if (nvlist_add_string(props, 1537352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1538352d8027SGeorge Wilson nvlist_add_string(props, 1539352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1540990b4856Slling nvlist_free(props); 1541990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1542990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1543990b4856Slling newname)); 1544990b4856Slling } 1545990b4856Slling } 1546990b4856Slling 15474b964adaSGeorge Wilson ret = zpool_import_props(hdl, config, newname, props, 15484b964adaSGeorge Wilson ZFS_IMPORT_NORMAL); 1549990b4856Slling nvlist_free(props); 1550990b4856Slling return (ret); 1551990b4856Slling } 1552990b4856Slling 15534b964adaSGeorge Wilson static void 15544b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 15554b964adaSGeorge Wilson int indent) 15564b964adaSGeorge Wilson { 15574b964adaSGeorge Wilson nvlist_t **child; 15584b964adaSGeorge Wilson uint_t c, children; 15594b964adaSGeorge Wilson char *vname; 15604b964adaSGeorge Wilson uint64_t is_log = 0; 15614b964adaSGeorge Wilson 15624b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 15634b964adaSGeorge Wilson &is_log); 15644b964adaSGeorge Wilson 15654b964adaSGeorge Wilson if (name != NULL) 15664b964adaSGeorge Wilson (void) printf("\t%*s%s%s\n", indent, "", name, 15674b964adaSGeorge Wilson is_log ? " [log]" : ""); 15684b964adaSGeorge Wilson 15694b964adaSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 15704b964adaSGeorge Wilson &child, &children) != 0) 15714b964adaSGeorge Wilson return; 15724b964adaSGeorge Wilson 15734b964adaSGeorge Wilson for (c = 0; c < children; c++) { 15744b964adaSGeorge Wilson vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 15754b964adaSGeorge Wilson print_vdev_tree(hdl, vname, child[c], indent + 2); 15764b964adaSGeorge Wilson free(vname); 15774b964adaSGeorge Wilson } 15784b964adaSGeorge Wilson } 15794b964adaSGeorge Wilson 1580ad135b5dSChristopher Siden void 1581ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config) 1582ad135b5dSChristopher Siden { 1583ad135b5dSChristopher Siden nvlist_t *nvinfo, *unsup_feat; 1584ad135b5dSChristopher Siden 1585ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 1586ad135b5dSChristopher Siden 0); 1587ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT, 1588ad135b5dSChristopher Siden &unsup_feat) == 0); 1589ad135b5dSChristopher Siden 1590ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL; 1591ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(unsup_feat, nvp)) { 1592ad135b5dSChristopher Siden char *desc; 1593ad135b5dSChristopher Siden 1594ad135b5dSChristopher Siden verify(nvpair_type(nvp) == DATA_TYPE_STRING); 1595ad135b5dSChristopher Siden verify(nvpair_value_string(nvp, &desc) == 0); 1596ad135b5dSChristopher Siden 1597ad135b5dSChristopher Siden if (strlen(desc) > 0) 1598ad135b5dSChristopher Siden (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc); 1599ad135b5dSChristopher Siden else 1600ad135b5dSChristopher Siden (void) printf("\t%s\n", nvpair_name(nvp)); 1601ad135b5dSChristopher Siden } 1602ad135b5dSChristopher Siden } 1603ad135b5dSChristopher Siden 1604990b4856Slling /* 1605990b4856Slling * Import the given pool using the known configuration and a list of 1606990b4856Slling * properties to be set. The configuration should have come from 1607990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1608990b4856Slling * is imported with a different name. 1609990b4856Slling */ 1610990b4856Slling int 1611990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 16124b964adaSGeorge Wilson nvlist_t *props, int flags) 1613fa9e4066Sahrens { 1614e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1615468c413aSTim Haley zpool_rewind_policy_t policy; 16164b964adaSGeorge Wilson nvlist_t *nv = NULL; 16174b964adaSGeorge Wilson nvlist_t *nvinfo = NULL; 16184b964adaSGeorge Wilson nvlist_t *missing = NULL; 1619fa9e4066Sahrens char *thename; 1620fa9e4066Sahrens char *origname; 1621fa9e4066Sahrens int ret; 16224b964adaSGeorge Wilson int error = 0; 1623990b4856Slling char errbuf[1024]; 1624fa9e4066Sahrens 1625fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1626fa9e4066Sahrens &origname) == 0); 1627fa9e4066Sahrens 1628990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1629990b4856Slling "cannot import pool '%s'"), origname); 1630990b4856Slling 1631fa9e4066Sahrens if (newname != NULL) { 163299653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1633ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 163499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 163599653d4eSeschrock newname)); 1636fa9e4066Sahrens thename = (char *)newname; 1637fa9e4066Sahrens } else { 1638fa9e4066Sahrens thename = origname; 1639fa9e4066Sahrens } 1640fa9e4066Sahrens 1641078266a5SMarcel Telka if (props != NULL) { 1642990b4856Slling uint64_t version; 1643f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 1644990b4856Slling 1645990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1646990b4856Slling &version) == 0); 1647990b4856Slling 16480a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1649078266a5SMarcel Telka props, version, flags, errbuf)) == NULL) 1650990b4856Slling return (-1); 1651078266a5SMarcel Telka if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1652351420b3Slling nvlist_free(props); 1653990b4856Slling return (-1); 1654990b4856Slling } 1655078266a5SMarcel Telka nvlist_free(props); 1656351420b3Slling } 1657fa9e4066Sahrens 1658fa9e4066Sahrens (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1659fa9e4066Sahrens 1660fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1661ea8dc4b6Seschrock &zc.zc_guid) == 0); 1662fa9e4066Sahrens 1663351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1664078266a5SMarcel Telka zcmd_free_nvlists(&zc); 166599653d4eSeschrock return (-1); 1666351420b3Slling } 166757f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1668078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1669468c413aSTim Haley return (-1); 1670468c413aSTim Haley } 1671fa9e4066Sahrens 16724b964adaSGeorge Wilson zc.zc_cookie = flags; 16734b964adaSGeorge Wilson while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 16744b964adaSGeorge Wilson errno == ENOMEM) { 16754b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 16764b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 16774b964adaSGeorge Wilson return (-1); 16784b964adaSGeorge Wilson } 16794b964adaSGeorge Wilson } 16804b964adaSGeorge Wilson if (ret != 0) 16814b964adaSGeorge Wilson error = errno; 16824b964adaSGeorge Wilson 16834b964adaSGeorge Wilson (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1684078266a5SMarcel Telka 1685078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1686078266a5SMarcel Telka 16874b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 16884b964adaSGeorge Wilson 16894b964adaSGeorge Wilson if (error) { 1690fa9e4066Sahrens char desc[1024]; 1691468c413aSTim Haley 1692468c413aSTim Haley /* 1693468c413aSTim Haley * Dry-run failed, but we print out what success 1694468c413aSTim Haley * looks like if we found a best txg 1695468c413aSTim Haley */ 16964b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_TRY_REWIND) { 1697468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 16984b964adaSGeorge Wilson B_TRUE, nv); 16994b964adaSGeorge Wilson nvlist_free(nv); 1700468c413aSTim Haley return (-1); 1701468c413aSTim Haley } 1702468c413aSTim Haley 1703fa9e4066Sahrens if (newname == NULL) 1704fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1705fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1706fa9e4066Sahrens thename); 1707fa9e4066Sahrens else 1708fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1709fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1710fa9e4066Sahrens origname, thename); 1711fa9e4066Sahrens 17124b964adaSGeorge Wilson switch (error) { 1713ea8dc4b6Seschrock case ENOTSUP: 1714ad135b5dSChristopher Siden if (nv != NULL && nvlist_lookup_nvlist(nv, 1715ad135b5dSChristopher Siden ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1716ad135b5dSChristopher Siden nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) { 1717ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, "This " 1718ad135b5dSChristopher Siden "pool uses the following feature(s) not " 1719ad135b5dSChristopher Siden "supported by this system:\n")); 1720ad135b5dSChristopher Siden zpool_print_unsup_feat(nv); 1721ad135b5dSChristopher Siden if (nvlist_exists(nvinfo, 1722ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY)) { 1723ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, 1724ad135b5dSChristopher Siden "All unsupported features are only " 1725ad135b5dSChristopher Siden "required for writing to the pool." 1726ad135b5dSChristopher Siden "\nThe pool can be imported using " 1727ad135b5dSChristopher Siden "'-o readonly=on'.\n")); 1728ad135b5dSChristopher Siden } 1729ad135b5dSChristopher Siden } 1730ea8dc4b6Seschrock /* 1731ea8dc4b6Seschrock * Unsupported version. 1732ea8dc4b6Seschrock */ 173399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1734ea8dc4b6Seschrock break; 1735ea8dc4b6Seschrock 1736b5989ec7Seschrock case EINVAL: 1737b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1738b5989ec7Seschrock break; 1739b5989ec7Seschrock 174054a91118SChris Kirby case EROFS: 174154a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 174254a91118SChris Kirby "one or more devices is read only")); 174354a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc); 174454a91118SChris Kirby break; 174554a91118SChris Kirby 17464b964adaSGeorge Wilson case ENXIO: 17474b964adaSGeorge Wilson if (nv && nvlist_lookup_nvlist(nv, 17484b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 17494b964adaSGeorge Wilson nvlist_lookup_nvlist(nvinfo, 17504b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 17514b964adaSGeorge Wilson (void) printf(dgettext(TEXT_DOMAIN, 17524b964adaSGeorge Wilson "The devices below are missing, use " 17534b964adaSGeorge Wilson "'-m' to import the pool anyway:\n")); 17544b964adaSGeorge Wilson print_vdev_tree(hdl, NULL, missing, 2); 17554b964adaSGeorge Wilson (void) printf("\n"); 17564b964adaSGeorge Wilson } 17574b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 17584b964adaSGeorge Wilson break; 17594b964adaSGeorge Wilson 17604b964adaSGeorge Wilson case EEXIST: 17614b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 17624b964adaSGeorge Wilson break; 17634b964adaSGeorge Wilson 1764fa9e4066Sahrens default: 17654b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 1766468c413aSTim Haley zpool_explain_recover(hdl, 17674b964adaSGeorge Wilson newname ? origname : thename, -error, nv); 1768468c413aSTim Haley break; 1769fa9e4066Sahrens } 1770fa9e4066Sahrens 17714b964adaSGeorge Wilson nvlist_free(nv); 1772fa9e4066Sahrens ret = -1; 1773fa9e4066Sahrens } else { 1774fa9e4066Sahrens zpool_handle_t *zhp; 1775ecd6cf80Smarks 1776fa9e4066Sahrens /* 1777fa9e4066Sahrens * This should never fail, but play it safe anyway. 1778fa9e4066Sahrens */ 1779681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0) 178094de1d4cSeschrock ret = -1; 1781681d9761SEric Taylor else if (zhp != NULL) 1782fa9e4066Sahrens zpool_close(zhp); 1783468c413aSTim Haley if (policy.zrp_request & 1784468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1785468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 17864b964adaSGeorge Wilson ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 1787468c413aSTim Haley } 17884b964adaSGeorge Wilson nvlist_free(nv); 1789468c413aSTim Haley return (0); 1790fa9e4066Sahrens } 1791ecd6cf80Smarks 1792fa9e4066Sahrens return (ret); 1793fa9e4066Sahrens } 1794fa9e4066Sahrens 1795fa9e4066Sahrens /* 17963f9d6ad7SLin Ling * Scan the pool. 1797fa9e4066Sahrens */ 1798fa9e4066Sahrens int 17993f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1800fa9e4066Sahrens { 1801fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1802fa9e4066Sahrens char msg[1024]; 180399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1804fa9e4066Sahrens 1805fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18063f9d6ad7SLin Ling zc.zc_cookie = func; 1807fa9e4066Sahrens 1808cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 18093f9d6ad7SLin Ling (errno == ENOENT && func != POOL_SCAN_NONE)) 1810fa9e4066Sahrens return (0); 1811fa9e4066Sahrens 18123f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) { 1813fa9e4066Sahrens (void) snprintf(msg, sizeof (msg), 1814fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 18153f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) { 18163f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 18173f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 18183f9d6ad7SLin Ling zc.zc_name); 18193f9d6ad7SLin Ling } else { 18203f9d6ad7SLin Ling assert(!"unexpected result"); 18213f9d6ad7SLin Ling } 1822fa9e4066Sahrens 18233f9d6ad7SLin Ling if (errno == EBUSY) { 18243f9d6ad7SLin Ling nvlist_t *nvroot; 18253f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 18263f9d6ad7SLin Ling uint_t psc; 18273f9d6ad7SLin Ling 18283f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config, 18293f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 18303f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 18313f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 18323f9d6ad7SLin Ling if (ps && ps->pss_func == POOL_SCAN_SCRUB) 18333f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 183499653d4eSeschrock else 18353f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg)); 18363f9d6ad7SLin Ling } else if (errno == ENOENT) { 18373f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 18383f9d6ad7SLin Ling } else { 183999653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1840fa9e4066Sahrens } 18413f9d6ad7SLin Ling } 1842fa9e4066Sahrens 1843a43d325bSek110237 /* 18443fdda499SJohn Harres * This provides a very minimal check whether a given string is likely a 18453fdda499SJohn Harres * c#t#d# style string. Users of this are expected to do their own 18463fdda499SJohn Harres * verification of the s# part. 18473fdda499SJohn Harres */ 18483fdda499SJohn Harres #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 18493fdda499SJohn Harres 18503fdda499SJohn Harres /* 18513fdda499SJohn Harres * More elaborate version for ones which may start with "/dev/dsk/" 18523fdda499SJohn Harres * and the like. 18533fdda499SJohn Harres */ 18543fdda499SJohn Harres static int 18559a686fbcSPaul Dagnelie ctd_check_path(char *str) 18569a686fbcSPaul Dagnelie { 18573fdda499SJohn Harres /* 18583fdda499SJohn Harres * If it starts with a slash, check the last component. 18593fdda499SJohn Harres */ 18603fdda499SJohn Harres if (str && str[0] == '/') { 18613fdda499SJohn Harres char *tmp = strrchr(str, '/'); 18623fdda499SJohn Harres 18633fdda499SJohn Harres /* 18643fdda499SJohn Harres * If it ends in "/old", check the second-to-last 18653fdda499SJohn Harres * component of the string instead. 18663fdda499SJohn Harres */ 18673fdda499SJohn Harres if (tmp != str && strcmp(tmp, "/old") == 0) { 18683fdda499SJohn Harres for (tmp--; *tmp != '/'; tmp--) 18693fdda499SJohn Harres ; 18703fdda499SJohn Harres } 18713fdda499SJohn Harres str = tmp + 1; 18723fdda499SJohn Harres } 18733fdda499SJohn Harres return (CTD_CHECK(str)); 18743fdda499SJohn Harres } 18753fdda499SJohn Harres 18763fdda499SJohn Harres /* 1877573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the 1878573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device. 1879a43d325bSek110237 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1880a43d325bSek110237 * spare; but FALSE if its an INUSE spare. 1881a43d325bSek110237 */ 188299653d4eSeschrock static nvlist_t * 1883573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 1884573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log) 1885ea8dc4b6Seschrock { 1886ea8dc4b6Seschrock uint_t c, children; 1887ea8dc4b6Seschrock nvlist_t **child; 188899653d4eSeschrock nvlist_t *ret; 1889ee0eb9f2SEric Schrock uint64_t is_log; 1890573ca77eSGeorge Wilson char *srchkey; 1891573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL); 1892ea8dc4b6Seschrock 1893573ca77eSGeorge Wilson /* Nothing to look for */ 1894573ca77eSGeorge Wilson if (search == NULL || pair == NULL) 1895573ca77eSGeorge Wilson return (NULL); 1896ea8dc4b6Seschrock 1897573ca77eSGeorge Wilson /* Obtain the key we will use to search */ 1898573ca77eSGeorge Wilson srchkey = nvpair_name(pair); 1899573ca77eSGeorge Wilson 1900573ca77eSGeorge Wilson switch (nvpair_type(pair)) { 1901cb04b873SMark J Musante case DATA_TYPE_UINT64: 1902cb04b873SMark J Musante if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 1903cb04b873SMark J Musante uint64_t srchval, theguid; 1904573ca77eSGeorge Wilson 1905573ca77eSGeorge Wilson verify(nvpair_value_uint64(pair, &srchval) == 0); 1906cb04b873SMark J Musante verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1907cb04b873SMark J Musante &theguid) == 0); 1908573ca77eSGeorge Wilson if (theguid == srchval) 190999653d4eSeschrock return (nv); 1910573ca77eSGeorge Wilson } 1911573ca77eSGeorge Wilson break; 1912573ca77eSGeorge Wilson 1913573ca77eSGeorge Wilson case DATA_TYPE_STRING: { 1914573ca77eSGeorge Wilson char *srchval, *val; 1915573ca77eSGeorge Wilson 1916573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0); 1917573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0) 1918573ca77eSGeorge Wilson break; 1919573ca77eSGeorge Wilson 1920573ca77eSGeorge Wilson /* 19213fdda499SJohn Harres * Search for the requested value. Special cases: 19223fdda499SJohn Harres * 19233fdda499SJohn Harres * - ZPOOL_CONFIG_PATH for whole disk entries. These end in 19243fdda499SJohn Harres * "s0" or "s0/old". The "s0" part is hidden from the user, 19253fdda499SJohn Harres * but included in the string, so this matches around it. 19263fdda499SJohn Harres * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 19273fdda499SJohn Harres * 192888ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares. 1929573ca77eSGeorge Wilson */ 19303fdda499SJohn Harres if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 19313fdda499SJohn Harres ctd_check_path(val)) { 1932573ca77eSGeorge Wilson uint64_t wholedisk = 0; 1933573ca77eSGeorge Wilson 1934ea8dc4b6Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1935ea8dc4b6Seschrock &wholedisk); 1936ea8dc4b6Seschrock if (wholedisk) { 19373fdda499SJohn Harres int slen = strlen(srchval); 19383fdda499SJohn Harres int vlen = strlen(val); 19393fdda499SJohn Harres 19403fdda499SJohn Harres if (slen != vlen - 2) 19413fdda499SJohn Harres break; 19423fdda499SJohn Harres 1943ea8dc4b6Seschrock /* 19443fdda499SJohn Harres * make_leaf_vdev() should only set 19453fdda499SJohn Harres * wholedisk for ZPOOL_CONFIG_PATHs which 19463fdda499SJohn Harres * will include "/dev/dsk/", giving plenty of 19473fdda499SJohn Harres * room for the indices used next. 1948ea8dc4b6Seschrock */ 19493fdda499SJohn Harres ASSERT(vlen >= 6); 19503fdda499SJohn Harres 19513fdda499SJohn Harres /* 19523fdda499SJohn Harres * strings identical except trailing "s0" 19533fdda499SJohn Harres */ 19543fdda499SJohn Harres if (strcmp(&val[vlen - 2], "s0") == 0 && 19553fdda499SJohn Harres strncmp(srchval, val, slen) == 0) 195699653d4eSeschrock return (nv); 19573fdda499SJohn Harres 19583fdda499SJohn Harres /* 19593fdda499SJohn Harres * strings identical except trailing "s0/old" 19603fdda499SJohn Harres */ 19613fdda499SJohn Harres if (strcmp(&val[vlen - 6], "s0/old") == 0 && 19623fdda499SJohn Harres strcmp(&srchval[slen - 4], "/old") == 0 && 19633fdda499SJohn Harres strncmp(srchval, val, slen - 4) == 0) 19643fdda499SJohn Harres return (nv); 19653fdda499SJohn Harres 1966573ca77eSGeorge Wilson break; 1967ea8dc4b6Seschrock } 196888ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 196988ecc943SGeorge Wilson char *type, *idx, *end, *p; 197088ecc943SGeorge Wilson uint64_t id, vdev_id; 197188ecc943SGeorge Wilson 197288ecc943SGeorge Wilson /* 197388ecc943SGeorge Wilson * Determine our vdev type, keeping in mind 197488ecc943SGeorge Wilson * that the srchval is composed of a type and 197588ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4). 197688ecc943SGeorge Wilson */ 197788ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL) 197888ecc943SGeorge Wilson return (NULL); 197988ecc943SGeorge Wilson 198088ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) { 198188ecc943SGeorge Wilson free(type); 198288ecc943SGeorge Wilson break; 198388ecc943SGeorge Wilson } 198488ecc943SGeorge Wilson idx = p + 1; 198588ecc943SGeorge Wilson *p = '\0'; 198688ecc943SGeorge Wilson 198788ecc943SGeorge Wilson /* 198888ecc943SGeorge Wilson * If the types don't match then keep looking. 198988ecc943SGeorge Wilson */ 199088ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) { 199188ecc943SGeorge Wilson free(type); 199288ecc943SGeorge Wilson break; 199388ecc943SGeorge Wilson } 199488ecc943SGeorge Wilson 199588ecc943SGeorge Wilson verify(strncmp(type, VDEV_TYPE_RAIDZ, 199688ecc943SGeorge Wilson strlen(VDEV_TYPE_RAIDZ)) == 0 || 199788ecc943SGeorge Wilson strncmp(type, VDEV_TYPE_MIRROR, 199888ecc943SGeorge Wilson strlen(VDEV_TYPE_MIRROR)) == 0); 199988ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 200088ecc943SGeorge Wilson &id) == 0); 200188ecc943SGeorge Wilson 200288ecc943SGeorge Wilson errno = 0; 200388ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10); 200488ecc943SGeorge Wilson 200588ecc943SGeorge Wilson free(type); 200688ecc943SGeorge Wilson if (errno != 0) 200788ecc943SGeorge Wilson return (NULL); 200888ecc943SGeorge Wilson 200988ecc943SGeorge Wilson /* 201088ecc943SGeorge Wilson * Now verify that we have the correct vdev id. 201188ecc943SGeorge Wilson */ 201288ecc943SGeorge Wilson if (vdev_id == id) 201388ecc943SGeorge Wilson return (nv); 2014ea8dc4b6Seschrock } 2015ea8dc4b6Seschrock 2016573ca77eSGeorge Wilson /* 2017573ca77eSGeorge Wilson * Common case 2018573ca77eSGeorge Wilson */ 2019573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0) 2020573ca77eSGeorge Wilson return (nv); 2021573ca77eSGeorge Wilson break; 2022573ca77eSGeorge Wilson } 2023573ca77eSGeorge Wilson 2024573ca77eSGeorge Wilson default: 2025573ca77eSGeorge Wilson break; 2026573ca77eSGeorge Wilson } 2027573ca77eSGeorge Wilson 2028ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2029ea8dc4b6Seschrock &child, &children) != 0) 203099653d4eSeschrock return (NULL); 2031ea8dc4b6Seschrock 2032ee0eb9f2SEric Schrock for (c = 0; c < children; c++) { 2033573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2034ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2035ee0eb9f2SEric Schrock /* 2036ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel 2037ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the 2038ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where 2039ee0eb9f2SEric Schrock * 'log' is non-NULL). 2040ee0eb9f2SEric Schrock */ 2041ee0eb9f2SEric Schrock if (log != NULL && 2042ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c], 2043ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 2044ee0eb9f2SEric Schrock is_log) { 2045ee0eb9f2SEric Schrock *log = B_TRUE; 2046ee0eb9f2SEric Schrock } 2047ea8dc4b6Seschrock return (ret); 2048ee0eb9f2SEric Schrock } 2049ee0eb9f2SEric Schrock } 2050ea8dc4b6Seschrock 205199653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 205299653d4eSeschrock &child, &children) == 0) { 205399653d4eSeschrock for (c = 0; c < children; c++) { 2054573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2055ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2056a43d325bSek110237 *avail_spare = B_TRUE; 205799653d4eSeschrock return (ret); 205899653d4eSeschrock } 205999653d4eSeschrock } 2060ea8dc4b6Seschrock } 2061ea8dc4b6Seschrock 2062fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2063fa94a07fSbrendan &child, &children) == 0) { 2064fa94a07fSbrendan for (c = 0; c < children; c++) { 2065573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2066ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2067fa94a07fSbrendan *l2cache = B_TRUE; 2068fa94a07fSbrendan return (ret); 2069fa94a07fSbrendan } 2070fa94a07fSbrendan } 2071fa94a07fSbrendan } 2072fa94a07fSbrendan 207399653d4eSeschrock return (NULL); 207499653d4eSeschrock } 207599653d4eSeschrock 2076573ca77eSGeorge Wilson /* 2077573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the 2078573ca77eSGeorge Wilson * associated vdev. 2079573ca77eSGeorge Wilson */ 2080573ca77eSGeorge Wilson nvlist_t * 2081573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 2082573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 2083573ca77eSGeorge Wilson { 2084573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret; 2085573ca77eSGeorge Wilson 2086573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2087573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 2088573ca77eSGeorge Wilson 2089573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2090573ca77eSGeorge Wilson &nvroot) == 0); 2091573ca77eSGeorge Wilson 2092573ca77eSGeorge Wilson *avail_spare = B_FALSE; 2093cb04b873SMark J Musante *l2cache = B_FALSE; 2094daeb70e5SMark J Musante if (log != NULL) 2095cb04b873SMark J Musante *log = B_FALSE; 2096573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2097573ca77eSGeorge Wilson nvlist_free(search); 2098573ca77eSGeorge Wilson 2099573ca77eSGeorge Wilson return (ret); 2100573ca77eSGeorge Wilson } 2101573ca77eSGeorge Wilson 210288ecc943SGeorge Wilson /* 210388ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 210488ecc943SGeorge Wilson */ 210588ecc943SGeorge Wilson boolean_t 210688ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name) 210788ecc943SGeorge Wilson { 210888ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 210988ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 211088ecc943SGeorge Wilson return (B_TRUE); 211188ecc943SGeorge Wilson return (B_FALSE); 211288ecc943SGeorge Wilson } 211388ecc943SGeorge Wilson 211499653d4eSeschrock nvlist_t * 2115fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 2116ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log) 2117ea8dc4b6Seschrock { 2118ea8dc4b6Seschrock char buf[MAXPATHLEN]; 2119ea8dc4b6Seschrock char *end; 2120573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret; 2121ea8dc4b6Seschrock uint64_t guid; 2122ea8dc4b6Seschrock 2123573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2124573ca77eSGeorge Wilson 21250917b783Seschrock guid = strtoull(path, &end, 10); 2126ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 2127573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 212888ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) { 212988ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 2130ea8dc4b6Seschrock } else if (path[0] != '/') { 2131ea8dc4b6Seschrock (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); 2132573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 2133ea8dc4b6Seschrock } else { 2134573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 2135ea8dc4b6Seschrock } 2136ea8dc4b6Seschrock 2137ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2138ea8dc4b6Seschrock &nvroot) == 0); 2139ea8dc4b6Seschrock 2140a43d325bSek110237 *avail_spare = B_FALSE; 2141fa94a07fSbrendan *l2cache = B_FALSE; 2142ee0eb9f2SEric Schrock if (log != NULL) 2143ee0eb9f2SEric Schrock *log = B_FALSE; 2144573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2145573ca77eSGeorge Wilson nvlist_free(search); 2146573ca77eSGeorge Wilson 2147573ca77eSGeorge Wilson return (ret); 2148a43d325bSek110237 } 2149a43d325bSek110237 215019397407SSherry Moore static int 215119397407SSherry Moore vdev_online(nvlist_t *nv) 215219397407SSherry Moore { 215319397407SSherry Moore uint64_t ival; 215419397407SSherry Moore 215519397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 215619397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 215719397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 215819397407SSherry Moore return (0); 215919397407SSherry Moore 216019397407SSherry Moore return (1); 216119397407SSherry Moore } 216219397407SSherry Moore 216319397407SSherry Moore /* 216421ecdf64SLin Ling * Helper function for zpool_get_physpaths(). 216519397407SSherry Moore */ 2166753a6d45SSherry Moore static int 216721ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 2168753a6d45SSherry Moore size_t *bytes_written) 216919397407SSherry Moore { 2170753a6d45SSherry Moore size_t bytes_left, pos, rsz; 2171753a6d45SSherry Moore char *tmppath; 2172753a6d45SSherry Moore const char *format; 2173753a6d45SSherry Moore 2174753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 2175753a6d45SSherry Moore &tmppath) != 0) 2176753a6d45SSherry Moore return (EZFS_NODEVICE); 2177753a6d45SSherry Moore 2178753a6d45SSherry Moore pos = *bytes_written; 2179753a6d45SSherry Moore bytes_left = physpath_size - pos; 2180753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s"; 2181753a6d45SSherry Moore 2182753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 2183753a6d45SSherry Moore *bytes_written += rsz; 2184753a6d45SSherry Moore 2185753a6d45SSherry Moore if (rsz >= bytes_left) { 2186753a6d45SSherry Moore /* if physpath was not copied properly, clear it */ 2187753a6d45SSherry Moore if (bytes_left != 0) { 2188753a6d45SSherry Moore physpath[pos] = 0; 2189753a6d45SSherry Moore } 2190753a6d45SSherry Moore return (EZFS_NOSPC); 2191753a6d45SSherry Moore } 2192753a6d45SSherry Moore return (0); 2193753a6d45SSherry Moore } 219419397407SSherry Moore 219521ecdf64SLin Ling static int 219621ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 219721ecdf64SLin Ling size_t *rsz, boolean_t is_spare) 219821ecdf64SLin Ling { 219921ecdf64SLin Ling char *type; 220021ecdf64SLin Ling int ret; 220121ecdf64SLin Ling 220221ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 220321ecdf64SLin Ling return (EZFS_INVALCONFIG); 220421ecdf64SLin Ling 220521ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) { 220621ecdf64SLin Ling /* 220721ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 220821ecdf64SLin Ling * For a spare vdev, we only want to boot from the active 220921ecdf64SLin Ling * spare device. 221021ecdf64SLin Ling */ 221121ecdf64SLin Ling if (is_spare) { 221221ecdf64SLin Ling uint64_t spare = 0; 221321ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 221421ecdf64SLin Ling &spare); 221521ecdf64SLin Ling if (!spare) 221621ecdf64SLin Ling return (EZFS_INVALCONFIG); 221721ecdf64SLin Ling } 221821ecdf64SLin Ling 221921ecdf64SLin Ling if (vdev_online(nv)) { 222021ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath, 222121ecdf64SLin Ling phypath_size, rsz)) != 0) 222221ecdf64SLin Ling return (ret); 222321ecdf64SLin Ling } 222421ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 2225*b4dd733cSToomas Soome strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 222621ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 || 222721ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 222821ecdf64SLin Ling nvlist_t **child; 222921ecdf64SLin Ling uint_t count; 223021ecdf64SLin Ling int i, ret; 223121ecdf64SLin Ling 223221ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv, 223321ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 223421ecdf64SLin Ling return (EZFS_INVALCONFIG); 223521ecdf64SLin Ling 223621ecdf64SLin Ling for (i = 0; i < count; i++) { 223721ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath, 223821ecdf64SLin Ling phypath_size, rsz, is_spare); 223921ecdf64SLin Ling if (ret == EZFS_NOSPC) 224021ecdf64SLin Ling return (ret); 224121ecdf64SLin Ling } 224221ecdf64SLin Ling } 224321ecdf64SLin Ling 224421ecdf64SLin Ling return (EZFS_POOL_INVALARG); 224521ecdf64SLin Ling } 224621ecdf64SLin Ling 224719397407SSherry Moore /* 2248753a6d45SSherry Moore * Get phys_path for a root pool config. 2249753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 225019397407SSherry Moore */ 2251753a6d45SSherry Moore static int 2252753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2253753a6d45SSherry Moore { 2254753a6d45SSherry Moore size_t rsz; 2255753a6d45SSherry Moore nvlist_t *vdev_root; 2256753a6d45SSherry Moore nvlist_t **child; 2257753a6d45SSherry Moore uint_t count; 225819397407SSherry Moore char *type; 225919397407SSherry Moore 2260753a6d45SSherry Moore rsz = 0; 2261753a6d45SSherry Moore 2262753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2263753a6d45SSherry Moore &vdev_root) != 0) 2264753a6d45SSherry Moore return (EZFS_INVALCONFIG); 2265753a6d45SSherry Moore 2266753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2267753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2268753a6d45SSherry Moore &child, &count) != 0) 2269753a6d45SSherry Moore return (EZFS_INVALCONFIG); 2270753a6d45SSherry Moore 2271753a6d45SSherry Moore /* 22721a902ef8SHans Rosenfeld * root pool can only have a single top-level vdev. 2273753a6d45SSherry Moore */ 22741a902ef8SHans Rosenfeld if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1) 2275753a6d45SSherry Moore return (EZFS_POOL_INVALARG); 2276753a6d45SSherry Moore 227721ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 227821ecdf64SLin Ling B_FALSE); 2279753a6d45SSherry Moore 2280753a6d45SSherry Moore /* No online devices */ 2281753a6d45SSherry Moore if (rsz == 0) 2282753a6d45SSherry Moore return (EZFS_NODEVICE); 228319397407SSherry Moore 228419397407SSherry Moore return (0); 228519397407SSherry Moore } 228619397407SSherry Moore 2287a43d325bSek110237 /* 2288753a6d45SSherry Moore * Get phys_path for a root pool 2289753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2290753a6d45SSherry Moore */ 2291753a6d45SSherry Moore int 2292753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2293753a6d45SSherry Moore { 2294753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2295753a6d45SSherry Moore phypath_size)); 2296753a6d45SSherry Moore } 2297753a6d45SSherry Moore 2298753a6d45SSherry Moore /* 2299573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel 2300573ca77eSGeorge Wilson * the disk to use the new unallocated space. 2301573ca77eSGeorge Wilson */ 2302573ca77eSGeorge Wilson static int 2303573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2304573ca77eSGeorge Wilson { 2305573ca77eSGeorge Wilson char path[MAXPATHLEN]; 2306573ca77eSGeorge Wilson char errbuf[1024]; 2307573ca77eSGeorge Wilson int fd, error; 2308573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int); 2309573ca77eSGeorge Wilson 2310573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2311573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL) 2312573ca77eSGeorge Wilson return (-1); 2313573ca77eSGeorge Wilson 2314573ca77eSGeorge Wilson (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name); 2315573ca77eSGeorge Wilson 2316573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2317573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2318573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name); 2319573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2320573ca77eSGeorge Wilson } 2321573ca77eSGeorge Wilson 2322573ca77eSGeorge Wilson /* 2323573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device 2324573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply 2325573ca77eSGeorge Wilson * ignore that error and continue on. 2326573ca77eSGeorge Wilson */ 2327573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd); 2328573ca77eSGeorge Wilson (void) close(fd); 2329573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) { 2330573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2331573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name); 2332573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2333573ca77eSGeorge Wilson } 2334573ca77eSGeorge Wilson return (0); 2335573ca77eSGeorge Wilson } 2336573ca77eSGeorge Wilson 2337573ca77eSGeorge Wilson /* 23383d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 23393d7072f8Seschrock * ZFS_ONLINE_* flags. 2340fa9e4066Sahrens */ 2341fa9e4066Sahrens int 23423d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 23433d7072f8Seschrock vdev_state_t *newstate) 2344fa9e4066Sahrens { 2345fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2346fa9e4066Sahrens char msg[1024]; 234799653d4eSeschrock nvlist_t *tgt; 2348573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog; 234999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2350fa9e4066Sahrens 2351573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) { 2352573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2353573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2354573ca77eSGeorge Wilson } else { 2355fa9e4066Sahrens (void) snprintf(msg, sizeof (msg), 2356ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2357573ca77eSGeorge Wilson } 2358ea8dc4b6Seschrock 2359ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2360ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2361573ca77eSGeorge Wilson &islog)) == NULL) 236299653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2363ea8dc4b6Seschrock 236499653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 236599653d4eSeschrock 2366069f55e2SEric Schrock if (avail_spare) 2367a43d325bSek110237 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2368a43d325bSek110237 2369573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND || 2370573ca77eSGeorge Wilson zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 2371573ca77eSGeorge Wilson char *pathname = NULL; 2372573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2373573ca77eSGeorge Wilson 2374573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2375573ca77eSGeorge Wilson &wholedisk); 2376573ca77eSGeorge Wilson verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 2377573ca77eSGeorge Wilson &pathname) == 0); 2378573ca77eSGeorge Wilson 2379573ca77eSGeorge Wilson /* 2380573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion. 2381573ca77eSGeorge Wilson */ 2382573ca77eSGeorge Wilson if (l2cache) { 2383573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2384573ca77eSGeorge Wilson "cannot expand cache devices")); 2385573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2386573ca77eSGeorge Wilson } 2387573ca77eSGeorge Wilson 2388573ca77eSGeorge Wilson if (wholedisk) { 2389573ca77eSGeorge Wilson pathname += strlen(DISK_ROOT) + 1; 2390cb04b873SMark J Musante (void) zpool_relabel_disk(hdl, pathname); 2391573ca77eSGeorge Wilson } 2392573ca77eSGeorge Wilson } 2393573ca77eSGeorge Wilson 23943d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 23953d7072f8Seschrock zc.zc_obj = flags; 2396fa9e4066Sahrens 2397cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 23981195e687SMark J Musante if (errno == EINVAL) { 23991195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 24001195e687SMark J Musante "from this pool into a new one. Use '%s' " 24011195e687SMark J Musante "instead"), "zpool detach"); 24021195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 24031195e687SMark J Musante } 240499653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 24051195e687SMark J Musante } 24063d7072f8Seschrock 24073d7072f8Seschrock *newstate = zc.zc_cookie; 24083d7072f8Seschrock return (0); 2409fa9e4066Sahrens } 2410fa9e4066Sahrens 2411fa9e4066Sahrens /* 2412fa9e4066Sahrens * Take the specified vdev offline 2413fa9e4066Sahrens */ 2414fa9e4066Sahrens int 24153d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2416fa9e4066Sahrens { 2417fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2418fa9e4066Sahrens char msg[1024]; 241999653d4eSeschrock nvlist_t *tgt; 2420fa94a07fSbrendan boolean_t avail_spare, l2cache; 242199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2422fa9e4066Sahrens 2423ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2424ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2425ea8dc4b6Seschrock 2426fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2427ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2428ee0eb9f2SEric Schrock NULL)) == NULL) 242999653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 243099653d4eSeschrock 243199653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2432fa9e4066Sahrens 2433069f55e2SEric Schrock if (avail_spare) 2434a43d325bSek110237 return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2435a43d325bSek110237 24363d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 24373d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 2438441d80aaSlling 2439cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2440fa9e4066Sahrens return (0); 2441fa9e4066Sahrens 2442fa9e4066Sahrens switch (errno) { 2443fa9e4066Sahrens case EBUSY: 244499653d4eSeschrock 2445fa9e4066Sahrens /* 2446fa9e4066Sahrens * There are no other replicas of this device. 2447fa9e4066Sahrens */ 244899653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2449fa9e4066Sahrens 2450e6ca193dSGeorge Wilson case EEXIST: 2451e6ca193dSGeorge Wilson /* 2452e6ca193dSGeorge Wilson * The log device has unplayed logs 2453e6ca193dSGeorge Wilson */ 2454e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2455e6ca193dSGeorge Wilson 2456fa9e4066Sahrens default: 245799653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2458fa9e4066Sahrens } 245999653d4eSeschrock } 246099653d4eSeschrock 246199653d4eSeschrock /* 24623d7072f8Seschrock * Mark the given vdev faulted. 24633d7072f8Seschrock */ 24643d7072f8Seschrock int 2465069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 24663d7072f8Seschrock { 24673d7072f8Seschrock zfs_cmd_t zc = { 0 }; 24683d7072f8Seschrock char msg[1024]; 24693d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 24703d7072f8Seschrock 24713d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 24723d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 24733d7072f8Seschrock 24743d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 24753d7072f8Seschrock zc.zc_guid = guid; 24763d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 2477069f55e2SEric Schrock zc.zc_obj = aux; 24783d7072f8Seschrock 2479cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 24803d7072f8Seschrock return (0); 24813d7072f8Seschrock 24823d7072f8Seschrock switch (errno) { 24833d7072f8Seschrock case EBUSY: 24843d7072f8Seschrock 24853d7072f8Seschrock /* 24863d7072f8Seschrock * There are no other replicas of this device. 24873d7072f8Seschrock */ 24883d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 24893d7072f8Seschrock 24903d7072f8Seschrock default: 24913d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 24923d7072f8Seschrock } 24933d7072f8Seschrock 24943d7072f8Seschrock } 24953d7072f8Seschrock 24963d7072f8Seschrock /* 24973d7072f8Seschrock * Mark the given vdev degraded. 24983d7072f8Seschrock */ 24993d7072f8Seschrock int 2500069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 25013d7072f8Seschrock { 25023d7072f8Seschrock zfs_cmd_t zc = { 0 }; 25033d7072f8Seschrock char msg[1024]; 25043d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25053d7072f8Seschrock 25063d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 25073d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 25083d7072f8Seschrock 25093d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25103d7072f8Seschrock zc.zc_guid = guid; 25113d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 2512069f55e2SEric Schrock zc.zc_obj = aux; 25133d7072f8Seschrock 2514cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 25153d7072f8Seschrock return (0); 25163d7072f8Seschrock 25173d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 25183d7072f8Seschrock } 25193d7072f8Seschrock 25203d7072f8Seschrock /* 252199653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 252299653d4eSeschrock * a hot spare. 252399653d4eSeschrock */ 252499653d4eSeschrock static boolean_t 252599653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 252699653d4eSeschrock { 252799653d4eSeschrock nvlist_t **child; 252899653d4eSeschrock uint_t c, children; 252999653d4eSeschrock char *type; 253099653d4eSeschrock 253199653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 253299653d4eSeschrock &children) == 0) { 253399653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 253499653d4eSeschrock &type) == 0); 253599653d4eSeschrock 253699653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 253799653d4eSeschrock children == 2 && child[which] == tgt) 253899653d4eSeschrock return (B_TRUE); 253999653d4eSeschrock 254099653d4eSeschrock for (c = 0; c < children; c++) 254199653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 254299653d4eSeschrock return (B_TRUE); 254399653d4eSeschrock } 254499653d4eSeschrock 254599653d4eSeschrock return (B_FALSE); 2546fa9e4066Sahrens } 2547fa9e4066Sahrens 2548fa9e4066Sahrens /* 2549fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 25508654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2551fa9e4066Sahrens */ 2552fa9e4066Sahrens int 2553fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2554fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2555fa9e4066Sahrens { 2556fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2557fa9e4066Sahrens char msg[1024]; 2558fa9e4066Sahrens int ret; 255999653d4eSeschrock nvlist_t *tgt; 2560ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog; 2561ee0eb9f2SEric Schrock uint64_t val; 2562cb04b873SMark J Musante char *newname; 256399653d4eSeschrock nvlist_t **child; 256499653d4eSeschrock uint_t children; 256599653d4eSeschrock nvlist_t *config_root; 256699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25674263d13fSGeorge Wilson boolean_t rootpool = zpool_is_bootable(zhp); 2568fa9e4066Sahrens 2569ea8dc4b6Seschrock if (replacing) 2570ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2571ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 2572ea8dc4b6Seschrock else 2573ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2574ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 2575ea8dc4b6Seschrock 2576fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2577ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 2578ee0eb9f2SEric Schrock &islog)) == 0) 257999653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 258099653d4eSeschrock 2581a43d325bSek110237 if (avail_spare) 258299653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 258399653d4eSeschrock 2584fa94a07fSbrendan if (l2cache) 2585fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2586fa94a07fSbrendan 258799653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2588fa9e4066Sahrens zc.zc_cookie = replacing; 2589fa9e4066Sahrens 259099653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 259199653d4eSeschrock &child, &children) != 0 || children != 1) { 259299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 259399653d4eSeschrock "new device must be a single disk")); 259499653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 259599653d4eSeschrock } 259699653d4eSeschrock 259799653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 259899653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 259999653d4eSeschrock 260088ecc943SGeorge Wilson if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 26010430f8daSeschrock return (-1); 26020430f8daSeschrock 260399653d4eSeschrock /* 260499653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 260599653d4eSeschrock * replace it with another hot spare. 260699653d4eSeschrock */ 260799653d4eSeschrock if (replacing && 260899653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2609ee0eb9f2SEric Schrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2610ee0eb9f2SEric Schrock NULL) == NULL || !avail_spare) && 2611ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) { 261299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 261399653d4eSeschrock "can only be replaced by another hot spare")); 26140430f8daSeschrock free(newname); 261599653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 261699653d4eSeschrock } 261799653d4eSeschrock 26180430f8daSeschrock free(newname); 26190430f8daSeschrock 2620990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 262199653d4eSeschrock return (-1); 2622fa9e4066Sahrens 2623cb04b873SMark J Musante ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2624fa9e4066Sahrens 2625e9dbad6fSeschrock zcmd_free_nvlists(&zc); 2626fa9e4066Sahrens 2627b5b76fecSGeorge Wilson if (ret == 0) { 2628b5b76fecSGeorge Wilson if (rootpool) { 2629b5b76fecSGeorge Wilson /* 263021ecdf64SLin Ling * XXX need a better way to prevent user from 263121ecdf64SLin Ling * booting up a half-baked vdev. 263221ecdf64SLin Ling */ 263321ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 263421ecdf64SLin Ling "sure to wait until resilver is done " 263521ecdf64SLin Ling "before rebooting.\n")); 2636b5b76fecSGeorge Wilson } 2637fa9e4066Sahrens return (0); 2638b5b76fecSGeorge Wilson } 2639fa9e4066Sahrens 2640fa9e4066Sahrens switch (errno) { 2641fa9e4066Sahrens case ENOTSUP: 2642fa9e4066Sahrens /* 2643fa9e4066Sahrens * Can't attach to or replace this type of vdev. 2644fa9e4066Sahrens */ 26458654d025Sperrin if (replacing) { 2646cb04b873SMark J Musante uint64_t version = zpool_get_prop_int(zhp, 2647cb04b873SMark J Musante ZPOOL_PROP_VERSION, NULL); 2648cb04b873SMark J Musante 2649ee0eb9f2SEric Schrock if (islog) 26508654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26518654d025Sperrin "cannot replace a log with a spare")); 2652cb04b873SMark J Musante else if (version >= SPA_VERSION_MULTI_REPLACE) 2653cb04b873SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2654cb04b873SMark J Musante "already in replacing/spare config; wait " 2655cb04b873SMark J Musante "for completion or use 'zpool detach'")); 26568654d025Sperrin else 265799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 265899653d4eSeschrock "cannot replace a replacing device")); 26598654d025Sperrin } else { 266099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 266199653d4eSeschrock "can only attach to mirrors and top-level " 266299653d4eSeschrock "disks")); 26638654d025Sperrin } 266499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2665fa9e4066Sahrens break; 2666fa9e4066Sahrens 2667fa9e4066Sahrens case EINVAL: 2668fa9e4066Sahrens /* 2669fa9e4066Sahrens * The new device must be a single disk. 2670fa9e4066Sahrens */ 267199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 267299653d4eSeschrock "new device must be a single disk")); 267399653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2674fa9e4066Sahrens break; 2675fa9e4066Sahrens 2676fa9e4066Sahrens case EBUSY: 267799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 267899653d4eSeschrock new_disk); 267999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2680fa9e4066Sahrens break; 2681fa9e4066Sahrens 2682fa9e4066Sahrens case EOVERFLOW: 2683fa9e4066Sahrens /* 2684fa9e4066Sahrens * The new device is too small. 2685fa9e4066Sahrens */ 268699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 268799653d4eSeschrock "device is too small")); 268899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2689fa9e4066Sahrens break; 2690fa9e4066Sahrens 2691fa9e4066Sahrens case EDOM: 2692fa9e4066Sahrens /* 2693fa9e4066Sahrens * The new device has a different alignment requirement. 2694fa9e4066Sahrens */ 269599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 269699653d4eSeschrock "devices have different sector alignment")); 269799653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2698fa9e4066Sahrens break; 2699fa9e4066Sahrens 2700fa9e4066Sahrens case ENAMETOOLONG: 2701fa9e4066Sahrens /* 2702fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 2703fa9e4066Sahrens */ 270499653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2705fa9e4066Sahrens break; 2706fa9e4066Sahrens 2707fa9e4066Sahrens default: 270899653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2709fa9e4066Sahrens } 2710fa9e4066Sahrens 271199653d4eSeschrock return (-1); 2712fa9e4066Sahrens } 2713fa9e4066Sahrens 2714fa9e4066Sahrens /* 2715fa9e4066Sahrens * Detach the specified device. 2716fa9e4066Sahrens */ 2717fa9e4066Sahrens int 2718fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2719fa9e4066Sahrens { 2720fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2721fa9e4066Sahrens char msg[1024]; 272299653d4eSeschrock nvlist_t *tgt; 2723fa94a07fSbrendan boolean_t avail_spare, l2cache; 272499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2725fa9e4066Sahrens 2726fa9e4066Sahrens (void) snprintf(msg, sizeof (msg), 2727ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2728ea8dc4b6Seschrock 2729ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2730ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2731ee0eb9f2SEric Schrock NULL)) == 0) 273299653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2733ea8dc4b6Seschrock 2734a43d325bSek110237 if (avail_spare) 273599653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 273699653d4eSeschrock 2737fa94a07fSbrendan if (l2cache) 2738fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2739fa94a07fSbrendan 274099653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 274199653d4eSeschrock 2742ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2743ea8dc4b6Seschrock return (0); 2744fa9e4066Sahrens 2745fa9e4066Sahrens switch (errno) { 2746fa9e4066Sahrens 2747fa9e4066Sahrens case ENOTSUP: 2748fa9e4066Sahrens /* 2749fa9e4066Sahrens * Can't detach from this type of vdev. 2750fa9e4066Sahrens */ 275199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 275299653d4eSeschrock "applicable to mirror and replacing vdevs")); 2753cb04b873SMark J Musante (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2754fa9e4066Sahrens break; 2755fa9e4066Sahrens 2756fa9e4066Sahrens case EBUSY: 2757fa9e4066Sahrens /* 2758fa9e4066Sahrens * There are no other replicas of this device. 2759fa9e4066Sahrens */ 276099653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2761fa9e4066Sahrens break; 2762fa9e4066Sahrens 2763fa9e4066Sahrens default: 276499653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2765fa9e4066Sahrens } 2766fa9e4066Sahrens 276799653d4eSeschrock return (-1); 276899653d4eSeschrock } 276999653d4eSeschrock 277099653d4eSeschrock /* 27711195e687SMark J Musante * Find a mirror vdev in the source nvlist. 27721195e687SMark J Musante * 27731195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors 27741195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the 27751195e687SMark J Musante * user specified on the command line. We loop over the mchild array to 27761195e687SMark J Musante * see if any entry in the schild array matches. 27771195e687SMark J Musante * 27781195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return 27791195e687SMark J Musante * the index of that entry. Otherwise we return -1. 27801195e687SMark J Musante */ 27811195e687SMark J Musante static int 27821195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 27831195e687SMark J Musante nvlist_t **schild, uint_t schildren) 27841195e687SMark J Musante { 27851195e687SMark J Musante uint_t mc; 27861195e687SMark J Musante 27871195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) { 27881195e687SMark J Musante uint_t sc; 27891195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 27901195e687SMark J Musante mchild[mc], B_FALSE); 27911195e687SMark J Musante 27921195e687SMark J Musante for (sc = 0; sc < schildren; sc++) { 27931195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 27941195e687SMark J Musante schild[sc], B_FALSE); 27951195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0); 27961195e687SMark J Musante 27971195e687SMark J Musante free(spath); 27981195e687SMark J Musante if (result) { 27991195e687SMark J Musante free(mpath); 28001195e687SMark J Musante return (mc); 28011195e687SMark J Musante } 28021195e687SMark J Musante } 28031195e687SMark J Musante 28041195e687SMark J Musante free(mpath); 28051195e687SMark J Musante } 28061195e687SMark J Musante 28071195e687SMark J Musante return (-1); 28081195e687SMark J Musante } 28091195e687SMark J Musante 28101195e687SMark J Musante /* 28111195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist 28121195e687SMark J Musante * is generated and it is the responsibility of the caller to free it. 28131195e687SMark J Musante */ 28141195e687SMark J Musante int 28151195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 28161195e687SMark J Musante nvlist_t *props, splitflags_t flags) 28171195e687SMark J Musante { 28181195e687SMark J Musante zfs_cmd_t zc = { 0 }; 28191195e687SMark J Musante char msg[1024]; 28201195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 28211195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL; 28221195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 28231195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl; 28241195e687SMark J Musante uint64_t vers; 28251195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE; 28261195e687SMark J Musante int retval = 0; 28271195e687SMark J Musante 28281195e687SMark J Musante (void) snprintf(msg, sizeof (msg), 28291195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 28301195e687SMark J Musante 28311195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname)) 28321195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 28331195e687SMark J Musante 28341195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) { 28351195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to " 28361195e687SMark J Musante "retrieve pool configuration\n")); 28371195e687SMark J Musante return (-1); 28381195e687SMark J Musante } 28391195e687SMark J Musante 28401195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 28411195e687SMark J Musante == 0); 28421195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 28431195e687SMark J Musante 28441195e687SMark J Musante if (props) { 2845f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 28461195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 2847f9af39baSGeorge Wilson props, vers, flags, msg)) == NULL) 28481195e687SMark J Musante return (-1); 28491195e687SMark J Musante } 28501195e687SMark J Musante 28511195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 28521195e687SMark J Musante &children) != 0) { 28531195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28541195e687SMark J Musante "Source pool is missing vdev tree")); 28551195e687SMark J Musante nvlist_free(zc_props); 28561195e687SMark J Musante return (-1); 28571195e687SMark J Musante } 28581195e687SMark J Musante 28591195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 28601195e687SMark J Musante vcount = 0; 28611195e687SMark J Musante 28621195e687SMark J Musante if (*newroot == NULL || 28631195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 28641195e687SMark J Musante &newchild, &newchildren) != 0) 28651195e687SMark J Musante newchildren = 0; 28661195e687SMark J Musante 28671195e687SMark J Musante for (c = 0; c < children; c++) { 28681195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE; 28691195e687SMark J Musante char *type; 28701195e687SMark J Musante nvlist_t **mchild, *vdev; 28711195e687SMark J Musante uint_t mchildren; 28721195e687SMark J Musante int entry; 28731195e687SMark J Musante 28741195e687SMark J Musante /* 28751195e687SMark J Musante * Unlike cache & spares, slogs are stored in the 28761195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 28771195e687SMark J Musante */ 28781195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 28791195e687SMark J Musante &is_log); 28801195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 28811195e687SMark J Musante &is_hole); 28821195e687SMark J Musante if (is_log || is_hole) { 28831195e687SMark J Musante /* 28841195e687SMark J Musante * Create a hole vdev and put it in the config. 28851195e687SMark J Musante */ 28861195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 28871195e687SMark J Musante goto out; 28881195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 28891195e687SMark J Musante VDEV_TYPE_HOLE) != 0) 28901195e687SMark J Musante goto out; 28911195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 28921195e687SMark J Musante 1) != 0) 28931195e687SMark J Musante goto out; 28941195e687SMark J Musante if (lastlog == 0) 28951195e687SMark J Musante lastlog = vcount; 28961195e687SMark J Musante varray[vcount++] = vdev; 28971195e687SMark J Musante continue; 28981195e687SMark J Musante } 28991195e687SMark J Musante lastlog = 0; 29001195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 29011195e687SMark J Musante == 0); 29021195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 29031195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29041195e687SMark J Musante "Source pool must be composed only of mirrors\n")); 29051195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 29061195e687SMark J Musante goto out; 29071195e687SMark J Musante } 29081195e687SMark J Musante 29091195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c], 29101195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 29111195e687SMark J Musante 29121195e687SMark J Musante /* find or add an entry for this top-level vdev */ 29131195e687SMark J Musante if (newchildren > 0 && 29141195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren, 29151195e687SMark J Musante newchild, newchildren)) >= 0) { 29161195e687SMark J Musante /* We found a disk that the user specified. */ 29171195e687SMark J Musante vdev = mchild[entry]; 29181195e687SMark J Musante ++found; 29191195e687SMark J Musante } else { 29201195e687SMark J Musante /* User didn't specify a disk for this vdev. */ 29211195e687SMark J Musante vdev = mchild[mchildren - 1]; 29221195e687SMark J Musante } 29231195e687SMark J Musante 29241195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 29251195e687SMark J Musante goto out; 29261195e687SMark J Musante } 29271195e687SMark J Musante 29281195e687SMark J Musante /* did we find every disk the user specified? */ 29291195e687SMark J Musante if (found != newchildren) { 29301195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 29311195e687SMark J Musante "include at most one disk from each mirror")); 29321195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 29331195e687SMark J Musante goto out; 29341195e687SMark J Musante } 29351195e687SMark J Musante 29361195e687SMark J Musante /* Prepare the nvlist for populating. */ 29371195e687SMark J Musante if (*newroot == NULL) { 29381195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 29391195e687SMark J Musante goto out; 29401195e687SMark J Musante freelist = B_TRUE; 29411195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 29421195e687SMark J Musante VDEV_TYPE_ROOT) != 0) 29431195e687SMark J Musante goto out; 29441195e687SMark J Musante } else { 29451195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 29461195e687SMark J Musante } 29471195e687SMark J Musante 29481195e687SMark J Musante /* Add all the children we found */ 29491195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 29501195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0) 29511195e687SMark J Musante goto out; 29521195e687SMark J Musante 29531195e687SMark J Musante /* 29541195e687SMark J Musante * If we're just doing a dry run, exit now with success. 29551195e687SMark J Musante */ 29561195e687SMark J Musante if (flags.dryrun) { 29571195e687SMark J Musante memory_err = B_FALSE; 29581195e687SMark J Musante freelist = B_FALSE; 29591195e687SMark J Musante goto out; 29601195e687SMark J Musante } 29611195e687SMark J Musante 29621195e687SMark J Musante /* now build up the config list & call the ioctl */ 29631195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 29641195e687SMark J Musante goto out; 29651195e687SMark J Musante 29661195e687SMark J Musante if (nvlist_add_nvlist(newconfig, 29671195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 29681195e687SMark J Musante nvlist_add_string(newconfig, 29691195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 29701195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 29711195e687SMark J Musante goto out; 29721195e687SMark J Musante 29731195e687SMark J Musante /* 29741195e687SMark J Musante * The new pool is automatically part of the namespace unless we 29751195e687SMark J Musante * explicitly export it. 29761195e687SMark J Musante */ 29771195e687SMark J Musante if (!flags.import) 29781195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 29791195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 29801195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 29811195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 29821195e687SMark J Musante goto out; 29831195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 29841195e687SMark J Musante goto out; 29851195e687SMark J Musante 29861195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 29871195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg); 29881195e687SMark J Musante goto out; 29891195e687SMark J Musante } 29901195e687SMark J Musante 29911195e687SMark J Musante freelist = B_FALSE; 29921195e687SMark J Musante memory_err = B_FALSE; 29931195e687SMark J Musante 29941195e687SMark J Musante out: 29951195e687SMark J Musante if (varray != NULL) { 29961195e687SMark J Musante int v; 29971195e687SMark J Musante 29981195e687SMark J Musante for (v = 0; v < vcount; v++) 29991195e687SMark J Musante nvlist_free(varray[v]); 30001195e687SMark J Musante free(varray); 30011195e687SMark J Musante } 30021195e687SMark J Musante zcmd_free_nvlists(&zc); 30031195e687SMark J Musante nvlist_free(zc_props); 30041195e687SMark J Musante nvlist_free(newconfig); 30051195e687SMark J Musante if (freelist) { 30061195e687SMark J Musante nvlist_free(*newroot); 30071195e687SMark J Musante *newroot = NULL; 30081195e687SMark J Musante } 30091195e687SMark J Musante 30101195e687SMark J Musante if (retval != 0) 30111195e687SMark J Musante return (retval); 30121195e687SMark J Musante 30131195e687SMark J Musante if (memory_err) 30141195e687SMark J Musante return (no_memory(hdl)); 30151195e687SMark J Musante 30161195e687SMark J Musante return (0); 30171195e687SMark J Musante } 30181195e687SMark J Musante 30191195e687SMark J Musante /* 3020fa94a07fSbrendan * Remove the given device. Currently, this is supported only for hot spares 3021fa94a07fSbrendan * and level 2 cache devices. 302299653d4eSeschrock */ 302399653d4eSeschrock int 302499653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 302599653d4eSeschrock { 302699653d4eSeschrock zfs_cmd_t zc = { 0 }; 302799653d4eSeschrock char msg[1024]; 302899653d4eSeschrock nvlist_t *tgt; 302988ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog; 303099653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 303188ecc943SGeorge Wilson uint64_t version; 303299653d4eSeschrock 303399653d4eSeschrock (void) snprintf(msg, sizeof (msg), 303499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 303599653d4eSeschrock 303699653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3037ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 303888ecc943SGeorge Wilson &islog)) == 0) 303999653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 304088ecc943SGeorge Wilson /* 304188ecc943SGeorge Wilson * XXX - this should just go away. 304288ecc943SGeorge Wilson */ 304388ecc943SGeorge Wilson if (!avail_spare && !l2cache && !islog) { 304499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 304588ecc943SGeorge Wilson "only inactive hot spares, cache, top-level, " 304688ecc943SGeorge Wilson "or log devices can be removed")); 304799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 304899653d4eSeschrock } 304999653d4eSeschrock 305088ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 305188ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) { 305288ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 305388ecc943SGeorge Wilson "pool must be upgrade to support log removal")); 305488ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg)); 305588ecc943SGeorge Wilson } 305688ecc943SGeorge Wilson 305799653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 305899653d4eSeschrock 3059ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 306099653d4eSeschrock return (0); 306199653d4eSeschrock 306299653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3063fa9e4066Sahrens } 3064fa9e4066Sahrens 3065ea8dc4b6Seschrock /* 3066ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 3067ea8dc4b6Seschrock */ 3068ea8dc4b6Seschrock int 3069468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 3070ea8dc4b6Seschrock { 3071ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3072ea8dc4b6Seschrock char msg[1024]; 307399653d4eSeschrock nvlist_t *tgt; 3074468c413aSTim Haley zpool_rewind_policy_t policy; 3075fa94a07fSbrendan boolean_t avail_spare, l2cache; 307699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3077468c413aSTim Haley nvlist_t *nvi = NULL; 30784b964adaSGeorge Wilson int error; 3079ea8dc4b6Seschrock 3080ea8dc4b6Seschrock if (path) 3081ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3082ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3083e9dbad6fSeschrock path); 3084ea8dc4b6Seschrock else 3085ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3086ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3087ea8dc4b6Seschrock zhp->zpool_name); 3088ea8dc4b6Seschrock 3089ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 309099653d4eSeschrock if (path) { 3091fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 3092ee0eb9f2SEric Schrock &l2cache, NULL)) == 0) 309399653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 309499653d4eSeschrock 3095fa94a07fSbrendan /* 3096fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 3097fa94a07fSbrendan * error clearing for l2cache devices. 3098fa94a07fSbrendan */ 3099a43d325bSek110237 if (avail_spare) 310099653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 310199653d4eSeschrock 310299653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 310399653d4eSeschrock &zc.zc_guid) == 0); 3104ea8dc4b6Seschrock } 3105ea8dc4b6Seschrock 3106468c413aSTim Haley zpool_get_rewind_policy(rewindnvl, &policy); 3107468c413aSTim Haley zc.zc_cookie = policy.zrp_request; 3108ea8dc4b6Seschrock 310957f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 3110468c413aSTim Haley return (-1); 3111468c413aSTim Haley 3112cb04b873SMark J Musante if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 3113468c413aSTim Haley return (-1); 3114468c413aSTim Haley 31154b964adaSGeorge Wilson while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 31164b964adaSGeorge Wilson errno == ENOMEM) { 31174b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 31184b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 31194b964adaSGeorge Wilson return (-1); 31204b964adaSGeorge Wilson } 31214b964adaSGeorge Wilson } 31224b964adaSGeorge Wilson 31234b964adaSGeorge Wilson if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 3124468c413aSTim Haley errno != EPERM && errno != EACCES)) { 3125468c413aSTim Haley if (policy.zrp_request & 3126468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 3127468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 3128468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name, 3129468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 3130468c413aSTim Haley nvi); 3131468c413aSTim Haley nvlist_free(nvi); 3132468c413aSTim Haley } 3133468c413aSTim Haley zcmd_free_nvlists(&zc); 3134468c413aSTim Haley return (0); 3135468c413aSTim Haley } 3136468c413aSTim Haley 3137468c413aSTim Haley zcmd_free_nvlists(&zc); 313899653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3139ea8dc4b6Seschrock } 3140ea8dc4b6Seschrock 3141fa9e4066Sahrens /* 31423d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 31433d7072f8Seschrock */ 31443d7072f8Seschrock int 31453d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 31463d7072f8Seschrock { 31473d7072f8Seschrock zfs_cmd_t zc = { 0 }; 31483d7072f8Seschrock char msg[1024]; 31493d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 31503d7072f8Seschrock 31513d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 31523d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 31533d7072f8Seschrock guid); 31543d7072f8Seschrock 31553d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 31563d7072f8Seschrock zc.zc_guid = guid; 315714f8ce41SVictor Latushkin zc.zc_cookie = ZPOOL_NO_REWIND; 31583d7072f8Seschrock 31593d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 31603d7072f8Seschrock return (0); 31613d7072f8Seschrock 31623d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 31633d7072f8Seschrock } 31643d7072f8Seschrock 31653d7072f8Seschrock /* 3166e9103aaeSGarrett D'Amore * Change the GUID for a pool. 3167e9103aaeSGarrett D'Amore */ 3168e9103aaeSGarrett D'Amore int 3169e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp) 3170e9103aaeSGarrett D'Amore { 3171e9103aaeSGarrett D'Amore char msg[1024]; 3172e9103aaeSGarrett D'Amore libzfs_handle_t *hdl = zhp->zpool_hdl; 3173e9103aaeSGarrett D'Amore zfs_cmd_t zc = { 0 }; 3174e9103aaeSGarrett D'Amore 3175e9103aaeSGarrett D'Amore (void) snprintf(msg, sizeof (msg), 3176e9103aaeSGarrett D'Amore dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 3177e9103aaeSGarrett D'Amore 3178e9103aaeSGarrett D'Amore (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3179e9103aaeSGarrett D'Amore if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 3180e9103aaeSGarrett D'Amore return (0); 3181e9103aaeSGarrett D'Amore 3182e9103aaeSGarrett D'Amore return (zpool_standard_error(hdl, errno, msg)); 3183e9103aaeSGarrett D'Amore } 3184e9103aaeSGarrett D'Amore 3185e9103aaeSGarrett D'Amore /* 31864263d13fSGeorge Wilson * Reopen the pool. 31874263d13fSGeorge Wilson */ 31884263d13fSGeorge Wilson int 31894263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp) 31904263d13fSGeorge Wilson { 31914263d13fSGeorge Wilson zfs_cmd_t zc = { 0 }; 31924263d13fSGeorge Wilson char msg[1024]; 31934263d13fSGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl; 31944263d13fSGeorge Wilson 31954263d13fSGeorge Wilson (void) snprintf(msg, sizeof (msg), 31964263d13fSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), 31974263d13fSGeorge Wilson zhp->zpool_name); 31984263d13fSGeorge Wilson 31994263d13fSGeorge Wilson (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 32004263d13fSGeorge Wilson if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0) 32014263d13fSGeorge Wilson return (0); 32024263d13fSGeorge Wilson return (zpool_standard_error(hdl, errno, msg)); 32034263d13fSGeorge Wilson } 32044263d13fSGeorge Wilson 32054263d13fSGeorge Wilson /* 3206c67d9675Seschrock * Convert from a devid string to a path. 3207c67d9675Seschrock */ 3208c67d9675Seschrock static char * 3209c67d9675Seschrock devid_to_path(char *devid_str) 3210c67d9675Seschrock { 3211c67d9675Seschrock ddi_devid_t devid; 3212c67d9675Seschrock char *minor; 3213c67d9675Seschrock char *path; 3214c67d9675Seschrock devid_nmlist_t *list = NULL; 3215c67d9675Seschrock int ret; 3216c67d9675Seschrock 3217c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 3218c67d9675Seschrock return (NULL); 3219c67d9675Seschrock 3220c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 3221c67d9675Seschrock 3222c67d9675Seschrock devid_str_free(minor); 3223c67d9675Seschrock devid_free(devid); 3224c67d9675Seschrock 3225c67d9675Seschrock if (ret != 0) 3226c67d9675Seschrock return (NULL); 3227c67d9675Seschrock 3228078266a5SMarcel Telka /* 3229078266a5SMarcel Telka * In a case the strdup() fails, we will just return NULL below. 3230078266a5SMarcel Telka */ 3231078266a5SMarcel Telka path = strdup(list[0].devname); 323299653d4eSeschrock 3233c67d9675Seschrock devid_free_nmlist(list); 3234c67d9675Seschrock 3235c67d9675Seschrock return (path); 3236c67d9675Seschrock } 3237c67d9675Seschrock 3238c67d9675Seschrock /* 3239c67d9675Seschrock * Convert from a path to a devid string. 3240c67d9675Seschrock */ 3241c67d9675Seschrock static char * 3242c67d9675Seschrock path_to_devid(const char *path) 3243c67d9675Seschrock { 3244c67d9675Seschrock int fd; 3245c67d9675Seschrock ddi_devid_t devid; 3246c67d9675Seschrock char *minor, *ret; 3247c67d9675Seschrock 3248c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 3249c67d9675Seschrock return (NULL); 3250c67d9675Seschrock 3251c67d9675Seschrock minor = NULL; 3252c67d9675Seschrock ret = NULL; 3253c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 3254c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 3255c67d9675Seschrock ret = devid_str_encode(devid, minor); 3256c67d9675Seschrock if (minor != NULL) 3257c67d9675Seschrock devid_str_free(minor); 3258c67d9675Seschrock devid_free(devid); 3259c67d9675Seschrock } 3260c67d9675Seschrock (void) close(fd); 3261c67d9675Seschrock 3262c67d9675Seschrock return (ret); 3263c67d9675Seschrock } 3264c67d9675Seschrock 3265c67d9675Seschrock /* 3266c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 3267c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 3268c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 3269c67d9675Seschrock */ 3270c67d9675Seschrock static void 3271c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3272c67d9675Seschrock { 3273c67d9675Seschrock zfs_cmd_t zc = { 0 }; 3274c67d9675Seschrock 3275c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3276e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3277c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3278ea8dc4b6Seschrock &zc.zc_guid) == 0); 3279c67d9675Seschrock 328099653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3281c67d9675Seschrock } 3282c67d9675Seschrock 3283c67d9675Seschrock /* 3284c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 3285c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3286c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 3287c67d9675Seschrock * trailing 's0' slice name. 3288c67d9675Seschrock * 3289c67d9675Seschrock * This routine is also responsible for identifying when disks have been 3290c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 3291c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 3292c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 3293c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 3294c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 3295c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3296c67d9675Seschrock * of these checks. 3297c67d9675Seschrock */ 3298c67d9675Seschrock char * 329988ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 330088ecc943SGeorge Wilson boolean_t verbose) 3301c67d9675Seschrock { 3302c67d9675Seschrock char *path, *devid; 3303ea8dc4b6Seschrock uint64_t value; 3304ea8dc4b6Seschrock char buf[64]; 33053d7072f8Seschrock vdev_stat_t *vs; 33063d7072f8Seschrock uint_t vsc; 3307c67d9675Seschrock 3308ea8dc4b6Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3309ea8dc4b6Seschrock &value) == 0) { 3310ea8dc4b6Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3311ea8dc4b6Seschrock &value) == 0); 33125ad82045Snd150628 (void) snprintf(buf, sizeof (buf), "%llu", 33135ad82045Snd150628 (u_longlong_t)value); 3314ea8dc4b6Seschrock path = buf; 3315ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3316c67d9675Seschrock 33173d7072f8Seschrock /* 33183d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 33193d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 33203d7072f8Seschrock * open a misbehaving device, which can have undesirable 33213d7072f8Seschrock * effects. 33223d7072f8Seschrock */ 33233f9d6ad7SLin Ling if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 33243d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 33253d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 33263d7072f8Seschrock zhp != NULL && 3327c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3328c67d9675Seschrock /* 3329c67d9675Seschrock * Determine if the current path is correct. 3330c67d9675Seschrock */ 3331c67d9675Seschrock char *newdevid = path_to_devid(path); 3332c67d9675Seschrock 3333c67d9675Seschrock if (newdevid == NULL || 3334c67d9675Seschrock strcmp(devid, newdevid) != 0) { 3335c67d9675Seschrock char *newpath; 3336c67d9675Seschrock 3337c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 3338c67d9675Seschrock /* 3339c67d9675Seschrock * Update the path appropriately. 3340c67d9675Seschrock */ 3341c67d9675Seschrock set_path(zhp, nv, newpath); 334299653d4eSeschrock if (nvlist_add_string(nv, 334399653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 3344c67d9675Seschrock verify(nvlist_lookup_string(nv, 334599653d4eSeschrock ZPOOL_CONFIG_PATH, 334699653d4eSeschrock &path) == 0); 334799653d4eSeschrock free(newpath); 334899653d4eSeschrock } 3349c67d9675Seschrock } 3350c67d9675Seschrock 3351c67d9675Seschrock if (newdevid) 3352c67d9675Seschrock devid_str_free(newdevid); 3353c67d9675Seschrock } 3354c67d9675Seschrock 3355c67d9675Seschrock if (strncmp(path, "/dev/dsk/", 9) == 0) 3356c67d9675Seschrock path += 9; 3357c67d9675Seschrock 3358c67d9675Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3359ea8dc4b6Seschrock &value) == 0 && value) { 33603fdda499SJohn Harres int pathlen = strlen(path); 336199653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 33623fdda499SJohn Harres 33633fdda499SJohn Harres /* 33643fdda499SJohn Harres * If it starts with c#, and ends with "s0", chop 33653fdda499SJohn Harres * the "s0" off, or if it ends with "s0/old", remove 33663fdda499SJohn Harres * the "s0" from the middle. 33673fdda499SJohn Harres */ 33683fdda499SJohn Harres if (CTD_CHECK(tmp)) { 33693fdda499SJohn Harres if (strcmp(&tmp[pathlen - 2], "s0") == 0) { 33703fdda499SJohn Harres tmp[pathlen - 2] = '\0'; 33713fdda499SJohn Harres } else if (pathlen > 6 && 33723fdda499SJohn Harres strcmp(&tmp[pathlen - 6], "s0/old") == 0) { 33733fdda499SJohn Harres (void) strcpy(&tmp[pathlen - 6], 33743fdda499SJohn Harres "/old"); 33753fdda499SJohn Harres } 33763fdda499SJohn Harres } 3377c67d9675Seschrock return (tmp); 3378c67d9675Seschrock } 3379c67d9675Seschrock } else { 3380c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 338199653d4eSeschrock 338299653d4eSeschrock /* 338399653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 338499653d4eSeschrock */ 338599653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 338699653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 338799653d4eSeschrock &value) == 0); 338899653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 33895ad82045Snd150628 (u_longlong_t)value); 339099653d4eSeschrock path = buf; 339199653d4eSeschrock } 339288ecc943SGeorge Wilson 339388ecc943SGeorge Wilson /* 339488ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id> 339588ecc943SGeorge Wilson * naming convention. 339688ecc943SGeorge Wilson */ 339788ecc943SGeorge Wilson if (verbose) { 339888ecc943SGeorge Wilson uint64_t id; 339988ecc943SGeorge Wilson 340088ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 340188ecc943SGeorge Wilson &id) == 0); 340288ecc943SGeorge Wilson (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 340388ecc943SGeorge Wilson (u_longlong_t)id); 340488ecc943SGeorge Wilson path = buf; 340588ecc943SGeorge Wilson } 3406c67d9675Seschrock } 3407c67d9675Seschrock 340899653d4eSeschrock return (zfs_strdup(hdl, path)); 3409c67d9675Seschrock } 3410ea8dc4b6Seschrock 3411ea8dc4b6Seschrock static int 3412a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b) 3413ea8dc4b6Seschrock { 34147802d7bfSMatthew Ahrens return (memcmp(a, b, sizeof (zbookmark_phys_t))); 3415ea8dc4b6Seschrock } 3416ea8dc4b6Seschrock 3417ea8dc4b6Seschrock /* 3418ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 3419ea8dc4b6Seschrock * caller. 3420ea8dc4b6Seschrock */ 3421ea8dc4b6Seschrock int 342255434c77Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3423ea8dc4b6Seschrock { 3424ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3425ea8dc4b6Seschrock uint64_t count; 34267802d7bfSMatthew Ahrens zbookmark_phys_t *zb = NULL; 342755434c77Sek110237 int i; 3428ea8dc4b6Seschrock 3429ea8dc4b6Seschrock /* 3430ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 3431ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 3432ea8dc4b6Seschrock * entire list. 3433ea8dc4b6Seschrock */ 3434ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3435ea8dc4b6Seschrock &count) == 0); 343675519f38Sek110237 if (count == 0) 343775519f38Sek110237 return (0); 3438e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 34397802d7bfSMatthew Ahrens count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL) 344099653d4eSeschrock return (-1); 3441e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 3442ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3443ea8dc4b6Seschrock for (;;) { 344499653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 344599653d4eSeschrock &zc) != 0) { 3446e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 344799653d4eSeschrock if (errno == ENOMEM) { 34487802d7bfSMatthew Ahrens void *dst; 34497802d7bfSMatthew Ahrens 3450bf561db0Svb160487 count = zc.zc_nvlist_dst_size; 34517802d7bfSMatthew Ahrens dst = zfs_alloc(zhp->zpool_hdl, count * 34527802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 34537802d7bfSMatthew Ahrens if (dst == NULL) 345499653d4eSeschrock return (-1); 34557802d7bfSMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)dst; 3456ea8dc4b6Seschrock } else { 3457ea8dc4b6Seschrock return (-1); 3458ea8dc4b6Seschrock } 3459ea8dc4b6Seschrock } else { 3460ea8dc4b6Seschrock break; 3461ea8dc4b6Seschrock } 3462ea8dc4b6Seschrock } 3463ea8dc4b6Seschrock 3464ea8dc4b6Seschrock /* 3465ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 3466ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3467e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3468ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 3469ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 3470ea8dc4b6Seschrock */ 34717802d7bfSMatthew Ahrens zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) + 3472e9dbad6fSeschrock zc.zc_nvlist_dst_size; 3473e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 3474ea8dc4b6Seschrock 3475a2cdcdd2SPaul Dagnelie qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare); 3476ea8dc4b6Seschrock 347755434c77Sek110237 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 347855434c77Sek110237 3479ea8dc4b6Seschrock /* 348055434c77Sek110237 * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3481ea8dc4b6Seschrock */ 3482ea8dc4b6Seschrock for (i = 0; i < count; i++) { 3483ea8dc4b6Seschrock nvlist_t *nv; 3484ea8dc4b6Seschrock 3485c0a81264Sek110237 /* ignoring zb_blkid and zb_level for now */ 3486c0a81264Sek110237 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3487c0a81264Sek110237 zb[i-1].zb_object == zb[i].zb_object) 3488ea8dc4b6Seschrock continue; 3489ea8dc4b6Seschrock 349055434c77Sek110237 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 3491e9dbad6fSeschrock goto nomem; 349255434c77Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 349355434c77Sek110237 zb[i].zb_objset) != 0) { 349455434c77Sek110237 nvlist_free(nv); 3495e9dbad6fSeschrock goto nomem; 3496e9dbad6fSeschrock } 349755434c77Sek110237 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 349855434c77Sek110237 zb[i].zb_object) != 0) { 349955434c77Sek110237 nvlist_free(nv); 350099653d4eSeschrock goto nomem; 3501ea8dc4b6Seschrock } 350255434c77Sek110237 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 350355434c77Sek110237 nvlist_free(nv); 3504e9dbad6fSeschrock goto nomem; 3505e9dbad6fSeschrock } 350655434c77Sek110237 nvlist_free(nv); 3507e9dbad6fSeschrock } 3508e9dbad6fSeschrock 35093ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 3510ea8dc4b6Seschrock return (0); 351199653d4eSeschrock 351299653d4eSeschrock nomem: 3513e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 351499653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 3515ea8dc4b6Seschrock } 3516eaca9bbdSeschrock 3517eaca9bbdSeschrock /* 3518eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 3519eaca9bbdSeschrock */ 3520eaca9bbdSeschrock int 3521990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3522eaca9bbdSeschrock { 3523eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 352499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3525eaca9bbdSeschrock 3526eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3527990b4856Slling zc.zc_cookie = new_version; 3528990b4856Slling 3529ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3530ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 353199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 353299653d4eSeschrock zhp->zpool_name)); 3533eaca9bbdSeschrock return (0); 3534eaca9bbdSeschrock } 353506eeb2adSek110237 353606eeb2adSek110237 void 35374445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len) 353806eeb2adSek110237 { 35394445fffbSMatthew Ahrens (void) strlcpy(string, basename(argv[0]), len); 35404445fffbSMatthew Ahrens for (int i = 1; i < argc; i++) { 35414445fffbSMatthew Ahrens (void) strlcat(string, " ", len); 35424445fffbSMatthew Ahrens (void) strlcat(string, argv[i], len); 35432a6b87f0Sek110237 } 35442a6b87f0Sek110237 } 35452a6b87f0Sek110237 35462a6b87f0Sek110237 int 35474445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message) 35482a6b87f0Sek110237 { 35494445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 }; 35504445fffbSMatthew Ahrens nvlist_t *args; 35514445fffbSMatthew Ahrens int err; 35522a6b87f0Sek110237 35534445fffbSMatthew Ahrens args = fnvlist_alloc(); 35544445fffbSMatthew Ahrens fnvlist_add_string(args, "message", message); 35554445fffbSMatthew Ahrens err = zcmd_write_src_nvlist(hdl, &zc, args); 35564445fffbSMatthew Ahrens if (err == 0) 35574445fffbSMatthew Ahrens err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc); 35584445fffbSMatthew Ahrens nvlist_free(args); 35594445fffbSMatthew Ahrens zcmd_free_nvlists(&zc); 35604445fffbSMatthew Ahrens return (err); 356106eeb2adSek110237 } 356206eeb2adSek110237 356306eeb2adSek110237 /* 356406eeb2adSek110237 * Perform ioctl to get some command history of a pool. 356506eeb2adSek110237 * 356606eeb2adSek110237 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 356706eeb2adSek110237 * logical offset of the history buffer to start reading from. 356806eeb2adSek110237 * 356906eeb2adSek110237 * Upon return, 'off' is the next logical offset to read from and 357006eeb2adSek110237 * 'len' is the actual amount of bytes read into 'buf'. 357106eeb2adSek110237 */ 357206eeb2adSek110237 static int 357306eeb2adSek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 357406eeb2adSek110237 { 357506eeb2adSek110237 zfs_cmd_t zc = { 0 }; 357606eeb2adSek110237 libzfs_handle_t *hdl = zhp->zpool_hdl; 357706eeb2adSek110237 357806eeb2adSek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 357906eeb2adSek110237 358006eeb2adSek110237 zc.zc_history = (uint64_t)(uintptr_t)buf; 358106eeb2adSek110237 zc.zc_history_len = *len; 358206eeb2adSek110237 zc.zc_history_offset = *off; 358306eeb2adSek110237 358406eeb2adSek110237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 358506eeb2adSek110237 switch (errno) { 358606eeb2adSek110237 case EPERM: 3587ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 3588ece3d9b3Slling dgettext(TEXT_DOMAIN, 358906eeb2adSek110237 "cannot show history for pool '%s'"), 359006eeb2adSek110237 zhp->zpool_name)); 359106eeb2adSek110237 case ENOENT: 3592ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 359306eeb2adSek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 359406eeb2adSek110237 "'%s'"), zhp->zpool_name)); 3595d7306b64Sek110237 case ENOTSUP: 3596d7306b64Sek110237 return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3597d7306b64Sek110237 dgettext(TEXT_DOMAIN, "cannot get history for pool " 3598d7306b64Sek110237 "'%s', pool must be upgraded"), zhp->zpool_name)); 359906eeb2adSek110237 default: 3600ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 360106eeb2adSek110237 dgettext(TEXT_DOMAIN, 360206eeb2adSek110237 "cannot get history for '%s'"), zhp->zpool_name)); 360306eeb2adSek110237 } 360406eeb2adSek110237 } 360506eeb2adSek110237 360606eeb2adSek110237 *len = zc.zc_history_len; 360706eeb2adSek110237 *off = zc.zc_history_offset; 360806eeb2adSek110237 360906eeb2adSek110237 return (0); 361006eeb2adSek110237 } 361106eeb2adSek110237 361206eeb2adSek110237 /* 361306eeb2adSek110237 * Process the buffer of nvlists, unpacking and storing each nvlist record 361406eeb2adSek110237 * into 'records'. 'leftover' is set to the number of bytes that weren't 361506eeb2adSek110237 * processed as there wasn't a complete record. 361606eeb2adSek110237 */ 36178f18d1faSGeorge Wilson int 361806eeb2adSek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 361906eeb2adSek110237 nvlist_t ***records, uint_t *numrecords) 362006eeb2adSek110237 { 362106eeb2adSek110237 uint64_t reclen; 362206eeb2adSek110237 nvlist_t *nv; 362306eeb2adSek110237 int i; 362406eeb2adSek110237 362506eeb2adSek110237 while (bytes_read > sizeof (reclen)) { 362606eeb2adSek110237 362706eeb2adSek110237 /* get length of packed record (stored as little endian) */ 362806eeb2adSek110237 for (i = 0, reclen = 0; i < sizeof (reclen); i++) 362906eeb2adSek110237 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 363006eeb2adSek110237 363106eeb2adSek110237 if (bytes_read < sizeof (reclen) + reclen) 363206eeb2adSek110237 break; 363306eeb2adSek110237 363406eeb2adSek110237 /* unpack record */ 363506eeb2adSek110237 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 363606eeb2adSek110237 return (ENOMEM); 363706eeb2adSek110237 bytes_read -= sizeof (reclen) + reclen; 363806eeb2adSek110237 buf += sizeof (reclen) + reclen; 363906eeb2adSek110237 364006eeb2adSek110237 /* add record to nvlist array */ 364106eeb2adSek110237 (*numrecords)++; 364206eeb2adSek110237 if (ISP2(*numrecords + 1)) { 364306eeb2adSek110237 *records = realloc(*records, 364406eeb2adSek110237 *numrecords * 2 * sizeof (nvlist_t *)); 364506eeb2adSek110237 } 364606eeb2adSek110237 (*records)[*numrecords - 1] = nv; 364706eeb2adSek110237 } 364806eeb2adSek110237 364906eeb2adSek110237 *leftover = bytes_read; 365006eeb2adSek110237 return (0); 365106eeb2adSek110237 } 365206eeb2adSek110237 365306eeb2adSek110237 /* 365406eeb2adSek110237 * Retrieve the command history of a pool. 365506eeb2adSek110237 */ 365606eeb2adSek110237 int 365706eeb2adSek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 365806eeb2adSek110237 { 36593339867aSMatthew Ahrens char *buf; 36603339867aSMatthew Ahrens int buflen = 128 * 1024; 366106eeb2adSek110237 uint64_t off = 0; 366206eeb2adSek110237 nvlist_t **records = NULL; 366306eeb2adSek110237 uint_t numrecords = 0; 366406eeb2adSek110237 int err, i; 366506eeb2adSek110237 36663339867aSMatthew Ahrens buf = malloc(buflen); 36673339867aSMatthew Ahrens if (buf == NULL) 36683339867aSMatthew Ahrens return (ENOMEM); 366906eeb2adSek110237 do { 36703339867aSMatthew Ahrens uint64_t bytes_read = buflen; 367106eeb2adSek110237 uint64_t leftover; 367206eeb2adSek110237 367306eeb2adSek110237 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 367406eeb2adSek110237 break; 367506eeb2adSek110237 367606eeb2adSek110237 /* if nothing else was read in, we're at EOF, just return */ 367706eeb2adSek110237 if (!bytes_read) 367806eeb2adSek110237 break; 367906eeb2adSek110237 368006eeb2adSek110237 if ((err = zpool_history_unpack(buf, bytes_read, 368106eeb2adSek110237 &leftover, &records, &numrecords)) != 0) 368206eeb2adSek110237 break; 368306eeb2adSek110237 off -= leftover; 36843339867aSMatthew Ahrens if (leftover == bytes_read) { 36853339867aSMatthew Ahrens /* 36863339867aSMatthew Ahrens * no progress made, because buffer is not big enough 36873339867aSMatthew Ahrens * to hold this record; resize and retry. 36883339867aSMatthew Ahrens */ 36893339867aSMatthew Ahrens buflen *= 2; 36903339867aSMatthew Ahrens free(buf); 36913339867aSMatthew Ahrens buf = malloc(buflen); 36923339867aSMatthew Ahrens if (buf == NULL) 36933339867aSMatthew Ahrens return (ENOMEM); 36943339867aSMatthew Ahrens } 369506eeb2adSek110237 369606eeb2adSek110237 /* CONSTCOND */ 369706eeb2adSek110237 } while (1); 369806eeb2adSek110237 36993339867aSMatthew Ahrens free(buf); 37003339867aSMatthew Ahrens 370106eeb2adSek110237 if (!err) { 370206eeb2adSek110237 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 370306eeb2adSek110237 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 370406eeb2adSek110237 records, numrecords) == 0); 370506eeb2adSek110237 } 370606eeb2adSek110237 for (i = 0; i < numrecords; i++) 370706eeb2adSek110237 nvlist_free(records[i]); 370806eeb2adSek110237 free(records); 370906eeb2adSek110237 371006eeb2adSek110237 return (err); 371106eeb2adSek110237 } 371255434c77Sek110237 371355434c77Sek110237 void 371455434c77Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 371555434c77Sek110237 char *pathname, size_t len) 371655434c77Sek110237 { 371755434c77Sek110237 zfs_cmd_t zc = { 0 }; 371855434c77Sek110237 boolean_t mounted = B_FALSE; 371955434c77Sek110237 char *mntpnt = NULL; 372040a5c998SMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 372155434c77Sek110237 372255434c77Sek110237 if (dsobj == 0) { 372355434c77Sek110237 /* special case for the MOS */ 372455434c77Sek110237 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 372555434c77Sek110237 return; 372655434c77Sek110237 } 372755434c77Sek110237 372855434c77Sek110237 /* get the dataset's name */ 372955434c77Sek110237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 373055434c77Sek110237 zc.zc_obj = dsobj; 373155434c77Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, 373255434c77Sek110237 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 373355434c77Sek110237 /* just write out a path of two object numbers */ 373455434c77Sek110237 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 373555434c77Sek110237 dsobj, obj); 373655434c77Sek110237 return; 373755434c77Sek110237 } 373855434c77Sek110237 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 373955434c77Sek110237 374055434c77Sek110237 /* find out if the dataset is mounted */ 374155434c77Sek110237 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 374255434c77Sek110237 374355434c77Sek110237 /* get the corrupted object's path */ 374455434c77Sek110237 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 374555434c77Sek110237 zc.zc_obj = obj; 374655434c77Sek110237 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 374755434c77Sek110237 &zc) == 0) { 374855434c77Sek110237 if (mounted) { 374955434c77Sek110237 (void) snprintf(pathname, len, "%s%s", mntpnt, 375055434c77Sek110237 zc.zc_value); 375155434c77Sek110237 } else { 375255434c77Sek110237 (void) snprintf(pathname, len, "%s:%s", 375355434c77Sek110237 dsname, zc.zc_value); 375455434c77Sek110237 } 375555434c77Sek110237 } else { 375655434c77Sek110237 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 375755434c77Sek110237 } 375855434c77Sek110237 free(mntpnt); 375955434c77Sek110237 } 3760b1b8ab34Slling 37618488aeb5Staylor /* 376215e6edf1Sgw25295 * Read the EFI label from the config, if a label does not exist then 376315e6edf1Sgw25295 * pass back the error to the caller. If the caller has passed a non-NULL 376415e6edf1Sgw25295 * diskaddr argument then we set it to the starting address of the EFI 376515e6edf1Sgw25295 * partition. 376615e6edf1Sgw25295 */ 376715e6edf1Sgw25295 static int 376815e6edf1Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb) 376915e6edf1Sgw25295 { 377015e6edf1Sgw25295 char *path; 377115e6edf1Sgw25295 int fd; 377215e6edf1Sgw25295 char diskname[MAXPATHLEN]; 377315e6edf1Sgw25295 int err = -1; 377415e6edf1Sgw25295 377515e6edf1Sgw25295 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 377615e6edf1Sgw25295 return (err); 377715e6edf1Sgw25295 377815e6edf1Sgw25295 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, 377915e6edf1Sgw25295 strrchr(path, '/')); 378015e6edf1Sgw25295 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 378115e6edf1Sgw25295 struct dk_gpt *vtoc; 378215e6edf1Sgw25295 378315e6edf1Sgw25295 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 378415e6edf1Sgw25295 if (sb != NULL) 378515e6edf1Sgw25295 *sb = vtoc->efi_parts[0].p_start; 378615e6edf1Sgw25295 efi_free(vtoc); 378715e6edf1Sgw25295 } 378815e6edf1Sgw25295 (void) close(fd); 378915e6edf1Sgw25295 } 379015e6edf1Sgw25295 return (err); 379115e6edf1Sgw25295 } 379215e6edf1Sgw25295 379315e6edf1Sgw25295 /* 37948488aeb5Staylor * determine where a partition starts on a disk in the current 37958488aeb5Staylor * configuration 37968488aeb5Staylor */ 37978488aeb5Staylor static diskaddr_t 37988488aeb5Staylor find_start_block(nvlist_t *config) 37998488aeb5Staylor { 38008488aeb5Staylor nvlist_t **child; 38018488aeb5Staylor uint_t c, children; 38028488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 38038488aeb5Staylor uint64_t wholedisk; 38048488aeb5Staylor 38058488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 38068488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 38078488aeb5Staylor if (nvlist_lookup_uint64(config, 38088488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 38098488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 38108488aeb5Staylor return (MAXOFFSET_T); 38118488aeb5Staylor } 381215e6edf1Sgw25295 if (read_efi_label(config, &sb) < 0) 381315e6edf1Sgw25295 sb = MAXOFFSET_T; 38148488aeb5Staylor return (sb); 38158488aeb5Staylor } 38168488aeb5Staylor 38178488aeb5Staylor for (c = 0; c < children; c++) { 38188488aeb5Staylor sb = find_start_block(child[c]); 38198488aeb5Staylor if (sb != MAXOFFSET_T) { 38208488aeb5Staylor return (sb); 38218488aeb5Staylor } 38228488aeb5Staylor } 38238488aeb5Staylor return (MAXOFFSET_T); 38248488aeb5Staylor } 38258488aeb5Staylor 38268488aeb5Staylor /* 38278488aeb5Staylor * Label an individual disk. The name provided is the short name, 38288488aeb5Staylor * stripped of any leading /dev path. 38298488aeb5Staylor */ 38308488aeb5Staylor int 38318488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name) 38328488aeb5Staylor { 38338488aeb5Staylor char path[MAXPATHLEN]; 38348488aeb5Staylor struct dk_gpt *vtoc; 38358488aeb5Staylor int fd; 38368488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 38378488aeb5Staylor uint64_t slice_size; 38388488aeb5Staylor diskaddr_t start_block; 38398488aeb5Staylor char errbuf[1024]; 38408488aeb5Staylor 3841c6ef114fSmmusante /* prepare an error message just in case */ 3842c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 3843c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 3844c6ef114fSmmusante 38458488aeb5Staylor if (zhp) { 38468488aeb5Staylor nvlist_t *nvroot; 38478488aeb5Staylor 38488488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 38498488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 38508488aeb5Staylor 38518488aeb5Staylor if (zhp->zpool_start_block == 0) 38528488aeb5Staylor start_block = find_start_block(nvroot); 38538488aeb5Staylor else 38548488aeb5Staylor start_block = zhp->zpool_start_block; 38558488aeb5Staylor zhp->zpool_start_block = start_block; 38568488aeb5Staylor } else { 38578488aeb5Staylor /* new pool */ 38588488aeb5Staylor start_block = NEW_START_BLOCK; 38598488aeb5Staylor } 38608488aeb5Staylor 38618488aeb5Staylor (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, 38628488aeb5Staylor BACKUP_SLICE); 38638488aeb5Staylor 38648488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 38658488aeb5Staylor /* 38668488aeb5Staylor * This shouldn't happen. We've long since verified that this 38678488aeb5Staylor * is a valid device. 38688488aeb5Staylor */ 3869c6ef114fSmmusante zfs_error_aux(hdl, 3870c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 38718488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 38728488aeb5Staylor } 38738488aeb5Staylor 38748488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 38758488aeb5Staylor /* 38768488aeb5Staylor * The only way this can fail is if we run out of memory, or we 38778488aeb5Staylor * were unable to read the disk's capacity 38788488aeb5Staylor */ 38798488aeb5Staylor if (errno == ENOMEM) 38808488aeb5Staylor (void) no_memory(hdl); 38818488aeb5Staylor 38828488aeb5Staylor (void) close(fd); 3883c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3884c6ef114fSmmusante "unable to read disk capacity"), name); 38858488aeb5Staylor 38868488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 38878488aeb5Staylor } 38888488aeb5Staylor 38898488aeb5Staylor slice_size = vtoc->efi_last_u_lba + 1; 38908488aeb5Staylor slice_size -= EFI_MIN_RESV_SIZE; 38918488aeb5Staylor if (start_block == MAXOFFSET_T) 38928488aeb5Staylor start_block = NEW_START_BLOCK; 38938488aeb5Staylor slice_size -= start_block; 38948488aeb5Staylor 38958488aeb5Staylor vtoc->efi_parts[0].p_start = start_block; 38968488aeb5Staylor vtoc->efi_parts[0].p_size = slice_size; 38978488aeb5Staylor 38988488aeb5Staylor /* 38998488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 39008488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 39018488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 39028488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 39038488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 39048488aeb5Staylor * can get, in the absence of V_OTHER. 39058488aeb5Staylor */ 39068488aeb5Staylor vtoc->efi_parts[0].p_tag = V_USR; 39078488aeb5Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 39088488aeb5Staylor 39098488aeb5Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 39108488aeb5Staylor vtoc->efi_parts[8].p_size = resv; 39118488aeb5Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 39128488aeb5Staylor 39138488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 39148488aeb5Staylor /* 39158488aeb5Staylor * Some block drivers (like pcata) may not support EFI 39168488aeb5Staylor * GPT labels. Print out a helpful error message dir- 39178488aeb5Staylor * ecting the user to manually label the disk and give 39188488aeb5Staylor * a specific slice. 39198488aeb5Staylor */ 39208488aeb5Staylor (void) close(fd); 39218488aeb5Staylor efi_free(vtoc); 39228488aeb5Staylor 39238488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3924c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 39258488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 39268488aeb5Staylor } 39278488aeb5Staylor 39288488aeb5Staylor (void) close(fd); 39298488aeb5Staylor efi_free(vtoc); 39308488aeb5Staylor return (0); 39318488aeb5Staylor } 3932e7cbe64fSgw25295 3933e7cbe64fSgw25295 static boolean_t 3934e7cbe64fSgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 3935e7cbe64fSgw25295 { 3936e7cbe64fSgw25295 char *type; 3937e7cbe64fSgw25295 nvlist_t **child; 3938e7cbe64fSgw25295 uint_t children, c; 3939e7cbe64fSgw25295 3940e7cbe64fSgw25295 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 3941810e43b2SBill Pijewski if (strcmp(type, VDEV_TYPE_FILE) == 0 || 394288ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 || 3943e7cbe64fSgw25295 strcmp(type, VDEV_TYPE_MISSING) == 0) { 3944e7cbe64fSgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3945e7cbe64fSgw25295 "vdev type '%s' is not supported"), type); 3946e7cbe64fSgw25295 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 3947e7cbe64fSgw25295 return (B_FALSE); 3948e7cbe64fSgw25295 } 3949e7cbe64fSgw25295 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3950e7cbe64fSgw25295 &child, &children) == 0) { 3951e7cbe64fSgw25295 for (c = 0; c < children; c++) { 3952e7cbe64fSgw25295 if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 3953e7cbe64fSgw25295 return (B_FALSE); 3954e7cbe64fSgw25295 } 3955e7cbe64fSgw25295 } 3956e7cbe64fSgw25295 return (B_TRUE); 3957e7cbe64fSgw25295 } 3958e7cbe64fSgw25295 3959e7cbe64fSgw25295 /* 3960810e43b2SBill Pijewski * Check if this zvol is allowable for use as a dump device; zero if 3961810e43b2SBill Pijewski * it is, > 0 if it isn't, < 0 if it isn't a zvol. 3962810e43b2SBill Pijewski * 3963810e43b2SBill Pijewski * Allowable storage configurations include mirrors, all raidz variants, and 3964810e43b2SBill Pijewski * pools with log, cache, and spare devices. Pools which are backed by files or 3965810e43b2SBill Pijewski * have missing/hole vdevs are not suitable. 3966e7cbe64fSgw25295 */ 3967e7cbe64fSgw25295 int 3968e7cbe64fSgw25295 zvol_check_dump_config(char *arg) 3969e7cbe64fSgw25295 { 3970e7cbe64fSgw25295 zpool_handle_t *zhp = NULL; 3971e7cbe64fSgw25295 nvlist_t *config, *nvroot; 3972e7cbe64fSgw25295 char *p, *volname; 3973e7cbe64fSgw25295 nvlist_t **top; 3974e7cbe64fSgw25295 uint_t toplevels; 3975e7cbe64fSgw25295 libzfs_handle_t *hdl; 3976e7cbe64fSgw25295 char errbuf[1024]; 397740a5c998SMatthew Ahrens char poolname[ZFS_MAX_DATASET_NAME_LEN]; 3978e7cbe64fSgw25295 int pathlen = strlen(ZVOL_FULL_DEV_DIR); 3979e7cbe64fSgw25295 int ret = 1; 3980e7cbe64fSgw25295 3981e7cbe64fSgw25295 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 3982e7cbe64fSgw25295 return (-1); 3983e7cbe64fSgw25295 } 3984e7cbe64fSgw25295 3985e7cbe64fSgw25295 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3986e7cbe64fSgw25295 "dump is not supported on device '%s'"), arg); 3987e7cbe64fSgw25295 3988e7cbe64fSgw25295 if ((hdl = libzfs_init()) == NULL) 3989e7cbe64fSgw25295 return (1); 3990e7cbe64fSgw25295 libzfs_print_on_error(hdl, B_TRUE); 3991e7cbe64fSgw25295 3992e7cbe64fSgw25295 volname = arg + pathlen; 3993e7cbe64fSgw25295 3994e7cbe64fSgw25295 /* check the configuration of the pool */ 3995e7cbe64fSgw25295 if ((p = strchr(volname, '/')) == NULL) { 3996e7cbe64fSgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3997e7cbe64fSgw25295 "malformed dataset name")); 3998e7cbe64fSgw25295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 3999e7cbe64fSgw25295 return (1); 400040a5c998SMatthew Ahrens } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) { 4001e7cbe64fSgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4002e7cbe64fSgw25295 "dataset name is too long")); 4003e7cbe64fSgw25295 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 4004e7cbe64fSgw25295 return (1); 4005e7cbe64fSgw25295 } else { 4006e7cbe64fSgw25295 (void) strncpy(poolname, volname, p - volname); 4007e7cbe64fSgw25295 poolname[p - volname] = '\0'; 4008e7cbe64fSgw25295 } 4009e7cbe64fSgw25295 4010e7cbe64fSgw25295 if ((zhp = zpool_open(hdl, poolname)) == NULL) { 4011e7cbe64fSgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4012e7cbe64fSgw25295 "could not open pool '%s'"), poolname); 4013e7cbe64fSgw25295 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4014e7cbe64fSgw25295 goto out; 4015e7cbe64fSgw25295 } 4016e7cbe64fSgw25295 config = zpool_get_config(zhp, NULL); 4017e7cbe64fSgw25295 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4018e7cbe64fSgw25295 &nvroot) != 0) { 4019e7cbe64fSgw25295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4020e7cbe64fSgw25295 "could not obtain vdev configuration for '%s'"), poolname); 4021e7cbe64fSgw25295 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 4022e7cbe64fSgw25295 goto out; 4023e7cbe64fSgw25295 } 4024e7cbe64fSgw25295 4025e7cbe64fSgw25295 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 4026e7cbe64fSgw25295 &top, &toplevels) == 0); 4027e7cbe64fSgw25295 4028e7cbe64fSgw25295 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 4029e7cbe64fSgw25295 goto out; 4030e7cbe64fSgw25295 } 4031e7cbe64fSgw25295 ret = 0; 4032e7cbe64fSgw25295 4033e7cbe64fSgw25295 out: 4034e7cbe64fSgw25295 if (zhp) 4035e7cbe64fSgw25295 zpool_close(zhp); 4036e7cbe64fSgw25295 libzfs_fini(hdl); 4037e7cbe64fSgw25295 return (ret); 4038e7cbe64fSgw25295 } 4039