1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 249a686fbcSPaul Dagnelie * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 25810e43b2SBill Pijewski * Copyright (c) 2013, Joyent, Inc. All rights reserved. 26*6401734dSWill Andrews * Copyright 2016 Nexenta Systems, Inc. 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 5015e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb); 51990b4856Slling 52573ca77eSGeorge Wilson #define BACKUP_SLICE "s2" 53573ca77eSGeorge Wilson 54f9af39baSGeorge Wilson typedef struct prop_flags { 55f9af39baSGeorge Wilson int create:1; /* Validate property on creation */ 56f9af39baSGeorge Wilson int import:1; /* Validate property on import */ 57f9af39baSGeorge Wilson } prop_flags_t; 58f9af39baSGeorge Wilson 59990b4856Slling /* 60990b4856Slling * ==================================================================== 61990b4856Slling * zpool property functions 62990b4856Slling * ==================================================================== 63990b4856Slling */ 64990b4856Slling 65990b4856Slling static int 66990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 67990b4856Slling { 68990b4856Slling zfs_cmd_t zc = { 0 }; 69990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 70990b4856Slling 71990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 72990b4856Slling 73990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 74990b4856Slling return (-1); 75990b4856Slling 76990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 77990b4856Slling if (errno == ENOMEM) { 78990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 79990b4856Slling zcmd_free_nvlists(&zc); 80990b4856Slling return (-1); 81990b4856Slling } 82990b4856Slling } else { 83990b4856Slling zcmd_free_nvlists(&zc); 84990b4856Slling return (-1); 85990b4856Slling } 86990b4856Slling } 87990b4856Slling 88990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 89990b4856Slling zcmd_free_nvlists(&zc); 90990b4856Slling return (-1); 91990b4856Slling } 92990b4856Slling 93990b4856Slling zcmd_free_nvlists(&zc); 94990b4856Slling 95990b4856Slling return (0); 96990b4856Slling } 97990b4856Slling 98990b4856Slling static int 99990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 100990b4856Slling { 101990b4856Slling nvlist_t *old_props; 102990b4856Slling 103990b4856Slling old_props = zhp->zpool_props; 104990b4856Slling 105990b4856Slling if (zpool_get_all_props(zhp) != 0) 106990b4856Slling return (-1); 107990b4856Slling 108990b4856Slling nvlist_free(old_props); 109990b4856Slling return (0); 110990b4856Slling } 111990b4856Slling 112990b4856Slling static char * 113990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 114990b4856Slling zprop_source_t *src) 115990b4856Slling { 116990b4856Slling nvlist_t *nv, *nvl; 117990b4856Slling uint64_t ival; 118990b4856Slling char *value; 119990b4856Slling zprop_source_t source; 120990b4856Slling 121990b4856Slling nvl = zhp->zpool_props; 122990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 123990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 124990b4856Slling source = ival; 125990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 126990b4856Slling } else { 127990b4856Slling source = ZPROP_SRC_DEFAULT; 128990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 129990b4856Slling value = "-"; 130990b4856Slling } 131990b4856Slling 132990b4856Slling if (src) 133990b4856Slling *src = source; 134990b4856Slling 135990b4856Slling return (value); 136990b4856Slling } 137990b4856Slling 138990b4856Slling uint64_t 139990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 140990b4856Slling { 141990b4856Slling nvlist_t *nv, *nvl; 142990b4856Slling uint64_t value; 143990b4856Slling zprop_source_t source; 144990b4856Slling 145b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 146b87f3af3Sperrin /* 147b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 148b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 149b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 150b87f3af3Sperrin */ 151b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 152b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 153b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 154b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 155b87f3af3Sperrin == 0)) { 156b87f3af3Sperrin return (value); 157b87f3af3Sperrin } 158990b4856Slling return (zpool_prop_default_numeric(prop)); 159b87f3af3Sperrin } 160990b4856Slling 161990b4856Slling nvl = zhp->zpool_props; 162990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 163990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 164990b4856Slling source = value; 165990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 166990b4856Slling } else { 167990b4856Slling source = ZPROP_SRC_DEFAULT; 168990b4856Slling value = zpool_prop_default_numeric(prop); 169990b4856Slling } 170990b4856Slling 171990b4856Slling if (src) 172990b4856Slling *src = source; 173990b4856Slling 174990b4856Slling return (value); 175990b4856Slling } 176990b4856Slling 177990b4856Slling /* 178990b4856Slling * Map VDEV STATE to printed strings. 179990b4856Slling */ 180*6401734dSWill Andrews const char * 181990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 182990b4856Slling { 183990b4856Slling switch (state) { 184990b4856Slling case VDEV_STATE_CLOSED: 185990b4856Slling case VDEV_STATE_OFFLINE: 186990b4856Slling return (gettext("OFFLINE")); 187990b4856Slling case VDEV_STATE_REMOVED: 188990b4856Slling return (gettext("REMOVED")); 189990b4856Slling case VDEV_STATE_CANT_OPEN: 190b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 191990b4856Slling return (gettext("FAULTED")); 1921195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL) 1931195e687SMark J Musante return (gettext("SPLIT")); 194990b4856Slling else 195990b4856Slling return (gettext("UNAVAIL")); 196990b4856Slling case VDEV_STATE_FAULTED: 197990b4856Slling return (gettext("FAULTED")); 198990b4856Slling case VDEV_STATE_DEGRADED: 199990b4856Slling return (gettext("DEGRADED")); 200990b4856Slling case VDEV_STATE_HEALTHY: 201990b4856Slling return (gettext("ONLINE")); 202990b4856Slling } 203990b4856Slling 204990b4856Slling return (gettext("UNKNOWN")); 205990b4856Slling } 206990b4856Slling 207*6401734dSWill Andrews /* 208*6401734dSWill Andrews * Map POOL STATE to printed strings. 209*6401734dSWill Andrews */ 210*6401734dSWill Andrews const char * 211*6401734dSWill Andrews zpool_pool_state_to_name(pool_state_t state) 212*6401734dSWill Andrews { 213*6401734dSWill Andrews switch (state) { 214*6401734dSWill Andrews case POOL_STATE_ACTIVE: 215*6401734dSWill Andrews return (gettext("ACTIVE")); 216*6401734dSWill Andrews case POOL_STATE_EXPORTED: 217*6401734dSWill Andrews return (gettext("EXPORTED")); 218*6401734dSWill Andrews case POOL_STATE_DESTROYED: 219*6401734dSWill Andrews return (gettext("DESTROYED")); 220*6401734dSWill Andrews case POOL_STATE_SPARE: 221*6401734dSWill Andrews return (gettext("SPARE")); 222*6401734dSWill Andrews case POOL_STATE_L2CACHE: 223*6401734dSWill Andrews return (gettext("L2CACHE")); 224*6401734dSWill Andrews case POOL_STATE_UNINITIALIZED: 225*6401734dSWill Andrews return (gettext("UNINITIALIZED")); 226*6401734dSWill Andrews case POOL_STATE_UNAVAIL: 227*6401734dSWill Andrews return (gettext("UNAVAIL")); 228*6401734dSWill Andrews case POOL_STATE_POTENTIALLY_ACTIVE: 229*6401734dSWill Andrews return (gettext("POTENTIALLY_ACTIVE")); 230*6401734dSWill Andrews } 231*6401734dSWill Andrews 232*6401734dSWill Andrews return (gettext("UNKNOWN")); 233*6401734dSWill Andrews } 234*6401734dSWill Andrews 235990b4856Slling /* 236990b4856Slling * Get a zpool property value for 'prop' and return the value in 237990b4856Slling * a pre-allocated buffer. 238990b4856Slling */ 239990b4856Slling int 240990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 241c58b3526SAdam Stevko zprop_source_t *srctype, boolean_t literal) 242990b4856Slling { 243990b4856Slling uint64_t intval; 244990b4856Slling const char *strval; 245990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 246990b4856Slling nvlist_t *nvroot; 247990b4856Slling vdev_stat_t *vs; 248990b4856Slling uint_t vsc; 249990b4856Slling 250990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 251379c004dSEric Schrock switch (prop) { 252379c004dSEric Schrock case ZPOOL_PROP_NAME: 253990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 254379c004dSEric Schrock break; 255379c004dSEric Schrock 256379c004dSEric Schrock case ZPOOL_PROP_HEALTH: 257990b4856Slling (void) strlcpy(buf, "FAULTED", len); 258379c004dSEric Schrock break; 259379c004dSEric Schrock 260379c004dSEric Schrock case ZPOOL_PROP_GUID: 261379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src); 262379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval); 263379c004dSEric Schrock break; 264379c004dSEric Schrock 265379c004dSEric Schrock case ZPOOL_PROP_ALTROOT: 266379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE: 2678704186eSDan McDonald case ZPOOL_PROP_COMMENT: 268379c004dSEric Schrock if (zhp->zpool_props != NULL || 269379c004dSEric Schrock zpool_get_all_props(zhp) == 0) { 270379c004dSEric Schrock (void) strlcpy(buf, 271379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src), 272379c004dSEric Schrock len); 273c58b3526SAdam Stevko break; 274379c004dSEric Schrock } 275379c004dSEric Schrock /* FALLTHROUGH */ 276379c004dSEric Schrock default: 277990b4856Slling (void) strlcpy(buf, "-", len); 278379c004dSEric Schrock break; 279379c004dSEric Schrock } 280379c004dSEric Schrock 281379c004dSEric Schrock if (srctype != NULL) 282379c004dSEric Schrock *srctype = src; 283990b4856Slling return (0); 284990b4856Slling } 285990b4856Slling 286990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 287990b4856Slling prop != ZPOOL_PROP_NAME) 288990b4856Slling return (-1); 289990b4856Slling 290990b4856Slling switch (zpool_prop_get_type(prop)) { 291990b4856Slling case PROP_TYPE_STRING: 292990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 293990b4856Slling len); 294990b4856Slling break; 295990b4856Slling 296990b4856Slling case PROP_TYPE_NUMBER: 297990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 298990b4856Slling 299990b4856Slling switch (prop) { 300990b4856Slling case ZPOOL_PROP_SIZE: 301485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED: 302485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE: 303ad135b5dSChristopher Siden case ZPOOL_PROP_FREEING: 3047fd05ac4SMatthew Ahrens case ZPOOL_PROP_LEAKED: 305c58b3526SAdam Stevko if (literal) { 306c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 307c58b3526SAdam Stevko (u_longlong_t)intval); 308c58b3526SAdam Stevko } else { 309c58b3526SAdam Stevko (void) zfs_nicenum(intval, buf, len); 310c58b3526SAdam Stevko } 311990b4856Slling break; 3127a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ: 3137a09f97bSGeorge Wilson if (intval == 0) { 3147a09f97bSGeorge Wilson (void) strlcpy(buf, "-", len); 3157a09f97bSGeorge Wilson } else if (literal) { 3167a09f97bSGeorge Wilson (void) snprintf(buf, len, "%llu", 3177a09f97bSGeorge Wilson (u_longlong_t)intval); 3187a09f97bSGeorge Wilson } else { 3197a09f97bSGeorge Wilson (void) zfs_nicenum(intval, buf, len); 3207a09f97bSGeorge Wilson } 3217a09f97bSGeorge Wilson break; 322990b4856Slling case ZPOOL_PROP_CAPACITY: 323c58b3526SAdam Stevko if (literal) { 324c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 325c58b3526SAdam Stevko (u_longlong_t)intval); 326c58b3526SAdam Stevko } else { 327c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu%%", 328c58b3526SAdam Stevko (u_longlong_t)intval); 329c58b3526SAdam Stevko } 330990b4856Slling break; 3312e4c9986SGeorge Wilson case ZPOOL_PROP_FRAGMENTATION: 3322e4c9986SGeorge Wilson if (intval == UINT64_MAX) { 3332e4c9986SGeorge Wilson (void) strlcpy(buf, "-", len); 3342e4c9986SGeorge Wilson } else { 3352e4c9986SGeorge Wilson (void) snprintf(buf, len, "%llu%%", 3362e4c9986SGeorge Wilson (u_longlong_t)intval); 3372e4c9986SGeorge Wilson } 3382e4c9986SGeorge Wilson break; 339b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO: 340b24ab676SJeff Bonwick (void) snprintf(buf, len, "%llu.%02llux", 341b24ab676SJeff Bonwick (u_longlong_t)(intval / 100), 342b24ab676SJeff Bonwick (u_longlong_t)(intval % 100)); 343b24ab676SJeff Bonwick break; 344990b4856Slling case ZPOOL_PROP_HEALTH: 345990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 346990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 347990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 3483f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 3493f9d6ad7SLin Ling == 0); 350990b4856Slling 351990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 352990b4856Slling vs->vs_aux), len); 353990b4856Slling break; 354ad135b5dSChristopher Siden case ZPOOL_PROP_VERSION: 355ad135b5dSChristopher Siden if (intval >= SPA_VERSION_FEATURES) { 356ad135b5dSChristopher Siden (void) snprintf(buf, len, "-"); 357ad135b5dSChristopher Siden break; 358ad135b5dSChristopher Siden } 359ad135b5dSChristopher Siden /* FALLTHROUGH */ 360990b4856Slling default: 361990b4856Slling (void) snprintf(buf, len, "%llu", intval); 362990b4856Slling } 363990b4856Slling break; 364990b4856Slling 365990b4856Slling case PROP_TYPE_INDEX: 366990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 367990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 368990b4856Slling != 0) 369990b4856Slling return (-1); 370990b4856Slling (void) strlcpy(buf, strval, len); 371990b4856Slling break; 372990b4856Slling 373990b4856Slling default: 374990b4856Slling abort(); 375990b4856Slling } 376990b4856Slling 377990b4856Slling if (srctype) 378990b4856Slling *srctype = src; 379990b4856Slling 380990b4856Slling return (0); 381990b4856Slling } 382990b4856Slling 383990b4856Slling /* 384990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 385990b4856Slling * Assuming bootfs is a valid dataset name. 386990b4856Slling */ 387990b4856Slling static boolean_t 388990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 389990b4856Slling { 390990b4856Slling int len = strlen(pool); 391990b4856Slling 392fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 393990b4856Slling return (B_FALSE); 394990b4856Slling 395990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 396990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 397990b4856Slling return (B_TRUE); 398990b4856Slling 399990b4856Slling return (B_FALSE); 400990b4856Slling } 401990b4856Slling 4024263d13fSGeorge Wilson boolean_t 4034263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp) 404b5b76fecSGeorge Wilson { 405b5b76fecSGeorge Wilson char bootfs[ZPOOL_MAXNAMELEN]; 406b5b76fecSGeorge Wilson 407b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 408c58b3526SAdam Stevko sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-", 409b5b76fecSGeorge Wilson sizeof (bootfs)) != 0); 410b5b76fecSGeorge Wilson } 411b5b76fecSGeorge Wilson 412b5b76fecSGeorge Wilson 413990b4856Slling /* 414990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 415990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 416990b4856Slling * specified as strings. 417990b4856Slling */ 418990b4856Slling static nvlist_t * 4190a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 420f9af39baSGeorge Wilson nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf) 421990b4856Slling { 422990b4856Slling nvpair_t *elem; 423990b4856Slling nvlist_t *retprops; 424990b4856Slling zpool_prop_t prop; 425990b4856Slling char *strval; 426990b4856Slling uint64_t intval; 4278704186eSDan McDonald char *slash, *check; 4282f8aaab3Seschrock struct stat64 statbuf; 42915e6edf1Sgw zpool_handle_t *zhp; 430990b4856Slling 431990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 432990b4856Slling (void) no_memory(hdl); 433990b4856Slling return (NULL); 434990b4856Slling } 435990b4856Slling 436990b4856Slling elem = NULL; 437990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 438990b4856Slling const char *propname = nvpair_name(elem); 439990b4856Slling 440ad135b5dSChristopher Siden prop = zpool_name_to_prop(propname); 441ad135b5dSChristopher Siden if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) { 442ad135b5dSChristopher Siden int err; 443ad135b5dSChristopher Siden char *fname = strchr(propname, '@') + 1; 444ad135b5dSChristopher Siden 4452acef22dSMatthew Ahrens err = zfeature_lookup_name(fname, NULL); 446ad135b5dSChristopher Siden if (err != 0) { 447ad135b5dSChristopher Siden ASSERT3U(err, ==, ENOENT); 448ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 449ad135b5dSChristopher Siden "invalid feature '%s'"), fname); 450ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 451ad135b5dSChristopher Siden goto error; 452ad135b5dSChristopher Siden } 453ad135b5dSChristopher Siden 454ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_STRING) { 455ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 456ad135b5dSChristopher Siden "'%s' must be a string"), propname); 457ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 458ad135b5dSChristopher Siden goto error; 459ad135b5dSChristopher Siden } 460ad135b5dSChristopher Siden 461ad135b5dSChristopher Siden (void) nvpair_value_string(elem, &strval); 462ad135b5dSChristopher Siden if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) { 463ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 464ad135b5dSChristopher Siden "property '%s' can only be set to " 465ad135b5dSChristopher Siden "'enabled'"), propname); 466ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 467ad135b5dSChristopher Siden goto error; 468ad135b5dSChristopher Siden } 469ad135b5dSChristopher Siden 470ad135b5dSChristopher Siden if (nvlist_add_uint64(retprops, propname, 0) != 0) { 471ad135b5dSChristopher Siden (void) no_memory(hdl); 472ad135b5dSChristopher Siden goto error; 473ad135b5dSChristopher Siden } 474ad135b5dSChristopher Siden continue; 475ad135b5dSChristopher Siden } 476ad135b5dSChristopher Siden 477990b4856Slling /* 478990b4856Slling * Make sure this property is valid and applies to this type. 479990b4856Slling */ 480ad135b5dSChristopher Siden if (prop == ZPROP_INVAL) { 481990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 482990b4856Slling "invalid property '%s'"), propname); 483990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 484990b4856Slling goto error; 485990b4856Slling } 486990b4856Slling 487990b4856Slling if (zpool_prop_readonly(prop)) { 488990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 489990b4856Slling "is readonly"), propname); 490990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 491990b4856Slling goto error; 492990b4856Slling } 493990b4856Slling 494990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 495990b4856Slling &strval, &intval, errbuf) != 0) 496990b4856Slling goto error; 497990b4856Slling 498990b4856Slling /* 499990b4856Slling * Perform additional checking for specific properties. 500990b4856Slling */ 501990b4856Slling switch (prop) { 502990b4856Slling case ZPOOL_PROP_VERSION: 503ad135b5dSChristopher Siden if (intval < version || 504ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(intval)) { 505990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 506990b4856Slling "property '%s' number %d is invalid."), 507990b4856Slling propname, intval); 508990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 509990b4856Slling goto error; 510990b4856Slling } 511990b4856Slling break; 512990b4856Slling 513990b4856Slling case ZPOOL_PROP_BOOTFS: 514f9af39baSGeorge Wilson if (flags.create || flags.import) { 515990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 516990b4856Slling "property '%s' cannot be set at creation " 517990b4856Slling "or import time"), propname); 518990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 519990b4856Slling goto error; 520990b4856Slling } 521990b4856Slling 522990b4856Slling if (version < SPA_VERSION_BOOTFS) { 523990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 524990b4856Slling "pool must be upgraded to support " 525990b4856Slling "'%s' property"), propname); 526990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 527990b4856Slling goto error; 528990b4856Slling } 529990b4856Slling 530990b4856Slling /* 531990b4856Slling * bootfs property value has to be a dataset name and 532990b4856Slling * the dataset has to be in the same pool as it sets to. 533990b4856Slling */ 534990b4856Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 535990b4856Slling strval)) { 536990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 537990b4856Slling "is an invalid name"), strval); 538990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 539990b4856Slling goto error; 540990b4856Slling } 54115e6edf1Sgw 54215e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 54315e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 54415e6edf1Sgw "could not open pool '%s'"), poolname); 54515e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 54615e6edf1Sgw goto error; 54715e6edf1Sgw } 54815e6edf1Sgw zpool_close(zhp); 549990b4856Slling break; 550990b4856Slling 5512f8aaab3Seschrock case ZPOOL_PROP_ALTROOT: 552f9af39baSGeorge Wilson if (!flags.create && !flags.import) { 553990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 554990b4856Slling "property '%s' can only be set during pool " 555990b4856Slling "creation or import"), propname); 556990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 557990b4856Slling goto error; 558990b4856Slling } 559990b4856Slling 5602f8aaab3Seschrock if (strval[0] != '/') { 561990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5622f8aaab3Seschrock "bad alternate root '%s'"), strval); 5632f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 564990b4856Slling goto error; 565990b4856Slling } 5662f8aaab3Seschrock break; 5672f8aaab3Seschrock 5682f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5692f8aaab3Seschrock if (strval[0] == '\0') 5702f8aaab3Seschrock break; 5712f8aaab3Seschrock 5722f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5732f8aaab3Seschrock break; 574990b4856Slling 575990b4856Slling if (strval[0] != '/') { 576990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5772f8aaab3Seschrock "property '%s' must be empty, an " 5782f8aaab3Seschrock "absolute path, or 'none'"), propname); 579990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 580990b4856Slling goto error; 581990b4856Slling } 582990b4856Slling 5832f8aaab3Seschrock slash = strrchr(strval, '/'); 584990b4856Slling 5852f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5862f8aaab3Seschrock strcmp(slash, "/..") == 0) { 5872f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5882f8aaab3Seschrock "'%s' is not a valid file"), strval); 5892f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 5902f8aaab3Seschrock goto error; 5912f8aaab3Seschrock } 592990b4856Slling 5932f8aaab3Seschrock *slash = '\0'; 5942f8aaab3Seschrock 5952c32020fSeschrock if (strval[0] != '\0' && 5962c32020fSeschrock (stat64(strval, &statbuf) != 0 || 5972c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 5982f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5992f8aaab3Seschrock "'%s' is not a valid directory"), 6002f8aaab3Seschrock strval); 6012f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6022f8aaab3Seschrock goto error; 6032f8aaab3Seschrock } 6042f8aaab3Seschrock 6052f8aaab3Seschrock *slash = '/'; 6062f8aaab3Seschrock break; 607f9af39baSGeorge Wilson 6088704186eSDan McDonald case ZPOOL_PROP_COMMENT: 6098704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 6108704186eSDan McDonald if (!isprint(*check)) { 6118704186eSDan McDonald zfs_error_aux(hdl, 6128704186eSDan McDonald dgettext(TEXT_DOMAIN, 6138704186eSDan McDonald "comment may only have printable " 6148704186eSDan McDonald "characters")); 6158704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, 6168704186eSDan McDonald errbuf); 6178704186eSDan McDonald goto error; 6188704186eSDan McDonald } 6198704186eSDan McDonald } 6208704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) { 6218704186eSDan McDonald zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6228704186eSDan McDonald "comment must not exceed %d characters"), 6238704186eSDan McDonald ZPROP_MAX_COMMENT); 6248704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6258704186eSDan McDonald goto error; 6268704186eSDan McDonald } 6278704186eSDan McDonald break; 628f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 629f9af39baSGeorge Wilson if (!flags.import) { 630f9af39baSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 631f9af39baSGeorge Wilson "property '%s' can only be set at " 632f9af39baSGeorge Wilson "import time"), propname); 633f9af39baSGeorge Wilson (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 634f9af39baSGeorge Wilson goto error; 635f9af39baSGeorge Wilson } 636f9af39baSGeorge Wilson break; 637990b4856Slling } 638990b4856Slling } 639990b4856Slling 640990b4856Slling return (retprops); 641990b4856Slling error: 642990b4856Slling nvlist_free(retprops); 643990b4856Slling return (NULL); 644990b4856Slling } 645990b4856Slling 646990b4856Slling /* 647990b4856Slling * Set zpool property : propname=propval. 648990b4856Slling */ 649990b4856Slling int 650990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 651990b4856Slling { 652990b4856Slling zfs_cmd_t zc = { 0 }; 653990b4856Slling int ret = -1; 654990b4856Slling char errbuf[1024]; 655990b4856Slling nvlist_t *nvl = NULL; 656990b4856Slling nvlist_t *realprops; 657990b4856Slling uint64_t version; 658f9af39baSGeorge Wilson prop_flags_t flags = { 0 }; 659990b4856Slling 660990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 661990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 662990b4856Slling zhp->zpool_name); 663990b4856Slling 664990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 665990b4856Slling return (no_memory(zhp->zpool_hdl)); 666990b4856Slling 667990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 668990b4856Slling nvlist_free(nvl); 669990b4856Slling return (no_memory(zhp->zpool_hdl)); 670990b4856Slling } 671990b4856Slling 672990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 6730a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 674f9af39baSGeorge Wilson zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) { 675990b4856Slling nvlist_free(nvl); 676990b4856Slling return (-1); 677990b4856Slling } 678990b4856Slling 679990b4856Slling nvlist_free(nvl); 680990b4856Slling nvl = realprops; 681990b4856Slling 682990b4856Slling /* 683990b4856Slling * Execute the corresponding ioctl() to set this property. 684990b4856Slling */ 685990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 686990b4856Slling 687990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 688990b4856Slling nvlist_free(nvl); 689990b4856Slling return (-1); 690990b4856Slling } 691990b4856Slling 692990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 693990b4856Slling 694990b4856Slling zcmd_free_nvlists(&zc); 695990b4856Slling nvlist_free(nvl); 696990b4856Slling 697990b4856Slling if (ret) 698990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 699990b4856Slling else 700990b4856Slling (void) zpool_props_refresh(zhp); 701990b4856Slling 702990b4856Slling return (ret); 703990b4856Slling } 704990b4856Slling 705990b4856Slling int 706990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 707990b4856Slling { 708990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 709990b4856Slling zprop_list_t *entry; 710990b4856Slling char buf[ZFS_MAXPROPLEN]; 711ad135b5dSChristopher Siden nvlist_t *features = NULL; 712ad135b5dSChristopher Siden zprop_list_t **last; 713ad135b5dSChristopher Siden boolean_t firstexpand = (NULL == *plp); 714990b4856Slling 715990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 716990b4856Slling return (-1); 717990b4856Slling 718ad135b5dSChristopher Siden last = plp; 719ad135b5dSChristopher Siden while (*last != NULL) 720ad135b5dSChristopher Siden last = &(*last)->pl_next; 721ad135b5dSChristopher Siden 722ad135b5dSChristopher Siden if ((*plp)->pl_all) 723ad135b5dSChristopher Siden features = zpool_get_features(zhp); 724ad135b5dSChristopher Siden 725ad135b5dSChristopher Siden if ((*plp)->pl_all && firstexpand) { 726ad135b5dSChristopher Siden for (int i = 0; i < SPA_FEATURES; i++) { 727ad135b5dSChristopher Siden zprop_list_t *entry = zfs_alloc(hdl, 728ad135b5dSChristopher Siden sizeof (zprop_list_t)); 729ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 730ad135b5dSChristopher Siden entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s", 731ad135b5dSChristopher Siden spa_feature_table[i].fi_uname); 732ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 733ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 734ad135b5dSChristopher Siden 735ad135b5dSChristopher Siden *last = entry; 736ad135b5dSChristopher Siden last = &entry->pl_next; 737ad135b5dSChristopher Siden } 738ad135b5dSChristopher Siden } 739ad135b5dSChristopher Siden 740ad135b5dSChristopher Siden /* add any unsupported features */ 741ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL); 742ad135b5dSChristopher Siden nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) { 743ad135b5dSChristopher Siden char *propname; 744ad135b5dSChristopher Siden boolean_t found; 745ad135b5dSChristopher Siden zprop_list_t *entry; 746ad135b5dSChristopher Siden 747ad135b5dSChristopher Siden if (zfeature_is_supported(nvpair_name(nvp))) 748ad135b5dSChristopher Siden continue; 749ad135b5dSChristopher Siden 750ad135b5dSChristopher Siden propname = zfs_asprintf(hdl, "unsupported@%s", 751ad135b5dSChristopher Siden nvpair_name(nvp)); 752ad135b5dSChristopher Siden 753ad135b5dSChristopher Siden /* 754ad135b5dSChristopher Siden * Before adding the property to the list make sure that no 755ad135b5dSChristopher Siden * other pool already added the same property. 756ad135b5dSChristopher Siden */ 757ad135b5dSChristopher Siden found = B_FALSE; 758ad135b5dSChristopher Siden entry = *plp; 759ad135b5dSChristopher Siden while (entry != NULL) { 760ad135b5dSChristopher Siden if (entry->pl_user_prop != NULL && 761ad135b5dSChristopher Siden strcmp(propname, entry->pl_user_prop) == 0) { 762ad135b5dSChristopher Siden found = B_TRUE; 763ad135b5dSChristopher Siden break; 764ad135b5dSChristopher Siden } 765ad135b5dSChristopher Siden entry = entry->pl_next; 766ad135b5dSChristopher Siden } 767ad135b5dSChristopher Siden if (found) { 768ad135b5dSChristopher Siden free(propname); 769ad135b5dSChristopher Siden continue; 770ad135b5dSChristopher Siden } 771ad135b5dSChristopher Siden 772ad135b5dSChristopher Siden entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 773ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 774ad135b5dSChristopher Siden entry->pl_user_prop = propname; 775ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 776ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 777ad135b5dSChristopher Siden 778ad135b5dSChristopher Siden *last = entry; 779ad135b5dSChristopher Siden last = &entry->pl_next; 780ad135b5dSChristopher Siden } 781ad135b5dSChristopher Siden 782990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 783990b4856Slling 784990b4856Slling if (entry->pl_fixed) 785990b4856Slling continue; 786990b4856Slling 787990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 788990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 789c58b3526SAdam Stevko NULL, B_FALSE) == 0) { 790990b4856Slling if (strlen(buf) > entry->pl_width) 791990b4856Slling entry->pl_width = strlen(buf); 792990b4856Slling } 793990b4856Slling } 794990b4856Slling 795990b4856Slling return (0); 796990b4856Slling } 797990b4856Slling 798ad135b5dSChristopher Siden /* 799ad135b5dSChristopher Siden * Get the state for the given feature on the given ZFS pool. 800ad135b5dSChristopher Siden */ 801ad135b5dSChristopher Siden int 802ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf, 803ad135b5dSChristopher Siden size_t len) 804ad135b5dSChristopher Siden { 805ad135b5dSChristopher Siden uint64_t refcount; 806ad135b5dSChristopher Siden boolean_t found = B_FALSE; 807ad135b5dSChristopher Siden nvlist_t *features = zpool_get_features(zhp); 808ad135b5dSChristopher Siden boolean_t supported; 809ad135b5dSChristopher Siden const char *feature = strchr(propname, '@') + 1; 810ad135b5dSChristopher Siden 811ad135b5dSChristopher Siden supported = zpool_prop_feature(propname); 812ad135b5dSChristopher Siden ASSERT(supported || zfs_prop_unsupported(propname)); 813ad135b5dSChristopher Siden 814ad135b5dSChristopher Siden /* 815ad135b5dSChristopher Siden * Convert from feature name to feature guid. This conversion is 816ad135b5dSChristopher Siden * unecessary for unsupported@... properties because they already 817ad135b5dSChristopher Siden * use guids. 818ad135b5dSChristopher Siden */ 819ad135b5dSChristopher Siden if (supported) { 820ad135b5dSChristopher Siden int ret; 8212acef22dSMatthew Ahrens spa_feature_t fid; 822ad135b5dSChristopher Siden 8232acef22dSMatthew Ahrens ret = zfeature_lookup_name(feature, &fid); 824ad135b5dSChristopher Siden if (ret != 0) { 825ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 826ad135b5dSChristopher Siden return (ENOTSUP); 827ad135b5dSChristopher Siden } 8282acef22dSMatthew Ahrens feature = spa_feature_table[fid].fi_guid; 829ad135b5dSChristopher Siden } 830ad135b5dSChristopher Siden 831ad135b5dSChristopher Siden if (nvlist_lookup_uint64(features, feature, &refcount) == 0) 832ad135b5dSChristopher Siden found = B_TRUE; 833ad135b5dSChristopher Siden 834ad135b5dSChristopher Siden if (supported) { 835ad135b5dSChristopher Siden if (!found) { 836ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len); 837ad135b5dSChristopher Siden } else { 838ad135b5dSChristopher Siden if (refcount == 0) 839ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len); 840ad135b5dSChristopher Siden else 841ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len); 842ad135b5dSChristopher Siden } 843ad135b5dSChristopher Siden } else { 844ad135b5dSChristopher Siden if (found) { 845ad135b5dSChristopher Siden if (refcount == 0) { 846ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE); 847ad135b5dSChristopher Siden } else { 848ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY); 849ad135b5dSChristopher Siden } 850ad135b5dSChristopher Siden } else { 851ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 852ad135b5dSChristopher Siden return (ENOTSUP); 853ad135b5dSChristopher Siden } 854ad135b5dSChristopher Siden } 855ad135b5dSChristopher Siden 856ad135b5dSChristopher Siden return (0); 857ad135b5dSChristopher Siden } 858990b4856Slling 859573ca77eSGeorge Wilson /* 860573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage 861573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead. 862573ca77eSGeorge Wilson */ 863573ca77eSGeorge Wilson #define NEW_START_BLOCK 256 864573ca77eSGeorge Wilson 865fa9e4066Sahrens /* 866fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 867fa9e4066Sahrens * 'buf'. 868fa9e4066Sahrens */ 869e7cbe64fSgw boolean_t 87099653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 871fa9e4066Sahrens { 872fa9e4066Sahrens namecheck_err_t why; 873fa9e4066Sahrens char what; 874b468a217Seschrock int ret; 875b468a217Seschrock 876b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 877b468a217Seschrock 878b468a217Seschrock /* 879b468a217Seschrock * The rules for reserved pool names were extended at a later point. 880b468a217Seschrock * But we need to support users with existing pools that may now be 881b468a217Seschrock * invalid. So we only check for this expanded set of names during a 882b468a217Seschrock * create (or import), and only in userland. 883b468a217Seschrock */ 884b468a217Seschrock if (ret == 0 && !isopen && 885b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 886b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 8878654d025Sperrin strncmp(pool, "spare", 5) == 0 || 8888654d025Sperrin strcmp(pool, "log") == 0)) { 889e7cbe64fSgw if (hdl != NULL) 890e7cbe64fSgw zfs_error_aux(hdl, 891e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved")); 89299653d4eSeschrock return (B_FALSE); 893b468a217Seschrock } 894b468a217Seschrock 895fa9e4066Sahrens 896b468a217Seschrock if (ret != 0) { 89799653d4eSeschrock if (hdl != NULL) { 898fa9e4066Sahrens switch (why) { 899b81d61a6Slling case NAME_ERR_TOOLONG: 90099653d4eSeschrock zfs_error_aux(hdl, 901b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 902b81d61a6Slling break; 903b81d61a6Slling 904fa9e4066Sahrens case NAME_ERR_INVALCHAR: 90599653d4eSeschrock zfs_error_aux(hdl, 906fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 907fa9e4066Sahrens "'%c' in pool name"), what); 908fa9e4066Sahrens break; 909fa9e4066Sahrens 910fa9e4066Sahrens case NAME_ERR_NOLETTER: 91199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 91299653d4eSeschrock "name must begin with a letter")); 913fa9e4066Sahrens break; 914fa9e4066Sahrens 915fa9e4066Sahrens case NAME_ERR_RESERVED: 91699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 91799653d4eSeschrock "name is reserved")); 918fa9e4066Sahrens break; 919fa9e4066Sahrens 920fa9e4066Sahrens case NAME_ERR_DISKLIKE: 92199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 92299653d4eSeschrock "pool name is reserved")); 923fa9e4066Sahrens break; 9245ad82045Snd 9255ad82045Snd case NAME_ERR_LEADING_SLASH: 9265ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9275ad82045Snd "leading slash in name")); 9285ad82045Snd break; 9295ad82045Snd 9305ad82045Snd case NAME_ERR_EMPTY_COMPONENT: 9315ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9325ad82045Snd "empty component in name")); 9335ad82045Snd break; 9345ad82045Snd 9355ad82045Snd case NAME_ERR_TRAILING_SLASH: 9365ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9375ad82045Snd "trailing slash in name")); 9385ad82045Snd break; 9395ad82045Snd 9405ad82045Snd case NAME_ERR_MULTIPLE_AT: 9415ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9425ad82045Snd "multiple '@' delimiters in name")); 9435ad82045Snd break; 9445ad82045Snd 945fa9e4066Sahrens } 946fa9e4066Sahrens } 94799653d4eSeschrock return (B_FALSE); 948fa9e4066Sahrens } 949fa9e4066Sahrens 95099653d4eSeschrock return (B_TRUE); 951fa9e4066Sahrens } 952fa9e4066Sahrens 953fa9e4066Sahrens /* 954fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 955fa9e4066Sahrens * state. 956fa9e4066Sahrens */ 957fa9e4066Sahrens zpool_handle_t * 95899653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 959fa9e4066Sahrens { 960fa9e4066Sahrens zpool_handle_t *zhp; 96194de1d4cSeschrock boolean_t missing; 962fa9e4066Sahrens 963fa9e4066Sahrens /* 964fa9e4066Sahrens * Make sure the pool name is valid. 965fa9e4066Sahrens */ 96699653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 967ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 96899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 96999653d4eSeschrock pool); 970fa9e4066Sahrens return (NULL); 971fa9e4066Sahrens } 972fa9e4066Sahrens 97399653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 97499653d4eSeschrock return (NULL); 975fa9e4066Sahrens 97699653d4eSeschrock zhp->zpool_hdl = hdl; 977fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 978fa9e4066Sahrens 97994de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 98094de1d4cSeschrock zpool_close(zhp); 98194de1d4cSeschrock return (NULL); 98294de1d4cSeschrock } 98394de1d4cSeschrock 98494de1d4cSeschrock if (missing) { 985990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 986ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 987990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 98894de1d4cSeschrock zpool_close(zhp); 98994de1d4cSeschrock return (NULL); 990fa9e4066Sahrens } 991fa9e4066Sahrens 992fa9e4066Sahrens return (zhp); 993fa9e4066Sahrens } 994fa9e4066Sahrens 995fa9e4066Sahrens /* 996fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 997fa9e4066Sahrens * the configuration cache may be out of date). 998fa9e4066Sahrens */ 99994de1d4cSeschrock int 100094de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 1001fa9e4066Sahrens { 1002fa9e4066Sahrens zpool_handle_t *zhp; 100394de1d4cSeschrock boolean_t missing; 1004fa9e4066Sahrens 100594de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 100694de1d4cSeschrock return (-1); 1007fa9e4066Sahrens 100899653d4eSeschrock zhp->zpool_hdl = hdl; 1009fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1010fa9e4066Sahrens 101194de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 101294de1d4cSeschrock zpool_close(zhp); 101394de1d4cSeschrock return (-1); 1014fa9e4066Sahrens } 1015fa9e4066Sahrens 101694de1d4cSeschrock if (missing) { 101794de1d4cSeschrock zpool_close(zhp); 101894de1d4cSeschrock *ret = NULL; 101994de1d4cSeschrock return (0); 102094de1d4cSeschrock } 102194de1d4cSeschrock 102294de1d4cSeschrock *ret = zhp; 102394de1d4cSeschrock return (0); 1024fa9e4066Sahrens } 1025fa9e4066Sahrens 1026fa9e4066Sahrens /* 1027fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 1028fa9e4066Sahrens * state. 1029fa9e4066Sahrens */ 1030fa9e4066Sahrens zpool_handle_t * 103199653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 1032fa9e4066Sahrens { 1033fa9e4066Sahrens zpool_handle_t *zhp; 1034fa9e4066Sahrens 103599653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 1036fa9e4066Sahrens return (NULL); 1037fa9e4066Sahrens 1038fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 1039ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 104099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 1041fa9e4066Sahrens zpool_close(zhp); 1042fa9e4066Sahrens return (NULL); 1043fa9e4066Sahrens } 1044fa9e4066Sahrens 1045fa9e4066Sahrens return (zhp); 1046fa9e4066Sahrens } 1047fa9e4066Sahrens 1048fa9e4066Sahrens /* 1049fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 1050fa9e4066Sahrens */ 1051fa9e4066Sahrens void 1052fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 1053fa9e4066Sahrens { 1054aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_config); 1055aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_old_config); 1056aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_props); 1057fa9e4066Sahrens free(zhp); 1058fa9e4066Sahrens } 1059fa9e4066Sahrens 1060fa9e4066Sahrens /* 1061fa9e4066Sahrens * Return the name of the pool. 1062fa9e4066Sahrens */ 1063fa9e4066Sahrens const char * 1064fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 1065fa9e4066Sahrens { 1066fa9e4066Sahrens return (zhp->zpool_name); 1067fa9e4066Sahrens } 1068fa9e4066Sahrens 1069fa9e4066Sahrens 1070fa9e4066Sahrens /* 1071fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 1072fa9e4066Sahrens */ 1073fa9e4066Sahrens int 1074fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 1075fa9e4066Sahrens { 1076fa9e4066Sahrens return (zhp->zpool_state); 1077fa9e4066Sahrens } 1078fa9e4066Sahrens 1079fa9e4066Sahrens /* 1080fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 1081fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 1082fa9e4066Sahrens * don't have to worry about error semantics. 1083fa9e4066Sahrens */ 1084fa9e4066Sahrens int 108599653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 10860a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 1087fa9e4066Sahrens { 1088fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 10890a48a24eStimh nvlist_t *zc_fsprops = NULL; 10900a48a24eStimh nvlist_t *zc_props = NULL; 109199653d4eSeschrock char msg[1024]; 10920a48a24eStimh int ret = -1; 1093fa9e4066Sahrens 109499653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 109599653d4eSeschrock "cannot create '%s'"), pool); 1096fa9e4066Sahrens 109799653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 109899653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 1099fa9e4066Sahrens 1100351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1101990b4856Slling return (-1); 1102fa9e4066Sahrens 11030a48a24eStimh if (props) { 1104f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE }; 1105f9af39baSGeorge Wilson 11060a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 1107f9af39baSGeorge Wilson SPA_VERSION_1, flags, msg)) == NULL) { 11080a48a24eStimh goto create_failed; 11090a48a24eStimh } 11100a48a24eStimh } 111199653d4eSeschrock 11120a48a24eStimh if (fsprops) { 11130a48a24eStimh uint64_t zoned; 11140a48a24eStimh char *zonestr; 11150a48a24eStimh 11160a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 11170a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 11180a48a24eStimh strcmp(zonestr, "on") == 0); 11190a48a24eStimh 1120e9316f76SJoe Stein if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM, 1121e9316f76SJoe Stein fsprops, zoned, NULL, NULL, msg)) == NULL) { 11220a48a24eStimh goto create_failed; 11230a48a24eStimh } 11240a48a24eStimh if (!zc_props && 11250a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 11260a48a24eStimh goto create_failed; 11270a48a24eStimh } 11280a48a24eStimh if (nvlist_add_nvlist(zc_props, 11290a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 11300a48a24eStimh goto create_failed; 11310a48a24eStimh } 1132351420b3Slling } 1133fa9e4066Sahrens 11340a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 11350a48a24eStimh goto create_failed; 11360a48a24eStimh 1137990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 1138fa9e4066Sahrens 11390a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 1140351420b3Slling 1141e9dbad6fSeschrock zcmd_free_nvlists(&zc); 11420a48a24eStimh nvlist_free(zc_props); 11430a48a24eStimh nvlist_free(zc_fsprops); 1144fa9e4066Sahrens 114599653d4eSeschrock switch (errno) { 1146fa9e4066Sahrens case EBUSY: 1147fa9e4066Sahrens /* 1148fa9e4066Sahrens * This can happen if the user has specified the same 1149fa9e4066Sahrens * device multiple times. We can't reliably detect this 1150fa9e4066Sahrens * until we try to add it and see we already have a 1151fa9e4066Sahrens * label. 1152fa9e4066Sahrens */ 115399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 115499653d4eSeschrock "one or more vdevs refer to the same device")); 115599653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1156fa9e4066Sahrens 1157e9316f76SJoe Stein case ERANGE: 1158e9316f76SJoe Stein /* 1159e9316f76SJoe Stein * This happens if the record size is smaller or larger 1160e9316f76SJoe Stein * than the allowed size range, or not a power of 2. 1161e9316f76SJoe Stein * 1162e9316f76SJoe Stein * NOTE: although zfs_valid_proplist is called earlier, 1163e9316f76SJoe Stein * this case may have slipped through since the 1164e9316f76SJoe Stein * pool does not exist yet and it is therefore 1165e9316f76SJoe Stein * impossible to read properties e.g. max blocksize 1166e9316f76SJoe Stein * from the pool. 1167e9316f76SJoe Stein */ 1168e9316f76SJoe Stein zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1169e9316f76SJoe Stein "record size invalid")); 1170e9316f76SJoe Stein return (zfs_error(hdl, EZFS_BADPROP, msg)); 1171e9316f76SJoe Stein 1172fa9e4066Sahrens case EOVERFLOW: 1173fa9e4066Sahrens /* 117499653d4eSeschrock * This occurs when one of the devices is below 1175fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1176fa9e4066Sahrens * device was the problem device since there's no 1177fa9e4066Sahrens * reliable way to determine device size from userland. 1178fa9e4066Sahrens */ 1179fa9e4066Sahrens { 1180fa9e4066Sahrens char buf[64]; 1181fa9e4066Sahrens 1182fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1183fa9e4066Sahrens 118499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 118599653d4eSeschrock "one or more devices is less than the " 118699653d4eSeschrock "minimum size (%s)"), buf); 1187fa9e4066Sahrens } 118899653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1189fa9e4066Sahrens 1190fa9e4066Sahrens case ENOSPC: 119199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 119299653d4eSeschrock "one or more devices is out of space")); 119399653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1194fa9e4066Sahrens 1195fa94a07fSbrendan case ENOTBLK: 1196fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1197fa94a07fSbrendan "cache device must be a disk or disk slice")); 1198fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 1199fa94a07fSbrendan 1200fa9e4066Sahrens default: 120199653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1202fa9e4066Sahrens } 1203fa9e4066Sahrens } 1204fa9e4066Sahrens 12050a48a24eStimh create_failed: 1206351420b3Slling zcmd_free_nvlists(&zc); 12070a48a24eStimh nvlist_free(zc_props); 12080a48a24eStimh nvlist_free(zc_fsprops); 12090a48a24eStimh return (ret); 1210fa9e4066Sahrens } 1211fa9e4066Sahrens 1212fa9e4066Sahrens /* 1213fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 1214fa9e4066Sahrens * datasets left in the pool. 1215fa9e4066Sahrens */ 1216fa9e4066Sahrens int 12174445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str) 1218fa9e4066Sahrens { 1219fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1220fa9e4066Sahrens zfs_handle_t *zfp = NULL; 122199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 122299653d4eSeschrock char msg[1024]; 1223fa9e4066Sahrens 1224fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 1225cb04b873SMark J Musante (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1226fa9e4066Sahrens return (-1); 1227fa9e4066Sahrens 1228fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 12294445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 1230fa9e4066Sahrens 1231cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 123299653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 123399653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 1234fa9e4066Sahrens 123599653d4eSeschrock if (errno == EROFS) { 123699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 123799653d4eSeschrock "one or more devices is read only")); 123899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 123999653d4eSeschrock } else { 124099653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1241fa9e4066Sahrens } 1242fa9e4066Sahrens 1243fa9e4066Sahrens if (zfp) 1244fa9e4066Sahrens zfs_close(zfp); 1245fa9e4066Sahrens return (-1); 1246fa9e4066Sahrens } 1247fa9e4066Sahrens 1248fa9e4066Sahrens if (zfp) { 1249fa9e4066Sahrens remove_mountpoint(zfp); 1250fa9e4066Sahrens zfs_close(zfp); 1251fa9e4066Sahrens } 1252fa9e4066Sahrens 1253fa9e4066Sahrens return (0); 1254fa9e4066Sahrens } 1255fa9e4066Sahrens 1256fa9e4066Sahrens /* 1257fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1258fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1259fa9e4066Sahrens */ 1260fa9e4066Sahrens int 1261fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1262fa9e4066Sahrens { 1263e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 126499653d4eSeschrock int ret; 126599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 126699653d4eSeschrock char msg[1024]; 1267fa94a07fSbrendan nvlist_t **spares, **l2cache; 1268fa94a07fSbrendan uint_t nspares, nl2cache; 126999653d4eSeschrock 127099653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 127199653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 127299653d4eSeschrock 1273fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1274fa94a07fSbrendan SPA_VERSION_SPARES && 127599653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 127699653d4eSeschrock &spares, &nspares) == 0) { 127799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 127899653d4eSeschrock "upgraded to add hot spares")); 127999653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 128099653d4eSeschrock } 1281fa9e4066Sahrens 1282fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1283fa94a07fSbrendan SPA_VERSION_L2CACHE && 1284fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1285fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1286fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1287fa94a07fSbrendan "upgraded to add cache devices")); 1288fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1289fa94a07fSbrendan } 1290fa94a07fSbrendan 1291990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 129299653d4eSeschrock return (-1); 1293fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1294fa9e4066Sahrens 1295cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1296fa9e4066Sahrens switch (errno) { 1297fa9e4066Sahrens case EBUSY: 1298fa9e4066Sahrens /* 1299fa9e4066Sahrens * This can happen if the user has specified the same 1300fa9e4066Sahrens * device multiple times. We can't reliably detect this 1301fa9e4066Sahrens * until we try to add it and see we already have a 1302fa9e4066Sahrens * label. 1303fa9e4066Sahrens */ 130499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 130599653d4eSeschrock "one or more vdevs refer to the same device")); 130699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1307fa9e4066Sahrens break; 1308fa9e4066Sahrens 1309fa9e4066Sahrens case EOVERFLOW: 1310fa9e4066Sahrens /* 1311fa9e4066Sahrens * This occurrs when one of the devices is below 1312fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1313fa9e4066Sahrens * device was the problem device since there's no 1314fa9e4066Sahrens * reliable way to determine device size from userland. 1315fa9e4066Sahrens */ 1316fa9e4066Sahrens { 1317fa9e4066Sahrens char buf[64]; 1318fa9e4066Sahrens 1319fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1320fa9e4066Sahrens 132199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 132299653d4eSeschrock "device is less than the minimum " 132399653d4eSeschrock "size (%s)"), buf); 1324fa9e4066Sahrens } 132599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 132699653d4eSeschrock break; 132799653d4eSeschrock 132899653d4eSeschrock case ENOTSUP: 132999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13308654d025Sperrin "pool must be upgraded to add these vdevs")); 133199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1332fa9e4066Sahrens break; 1333fa9e4066Sahrens 1334b1b8ab34Slling case EDOM: 1335b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13368654d025Sperrin "root pool can not have multiple vdevs" 13378654d025Sperrin " or separate logs")); 1338b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1339b1b8ab34Slling break; 1340b1b8ab34Slling 1341fa94a07fSbrendan case ENOTBLK: 1342fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1343fa94a07fSbrendan "cache device must be a disk or disk slice")); 1344fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1345fa94a07fSbrendan break; 1346fa94a07fSbrendan 1347fa9e4066Sahrens default: 134899653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1349fa9e4066Sahrens } 1350fa9e4066Sahrens 135199653d4eSeschrock ret = -1; 135299653d4eSeschrock } else { 135399653d4eSeschrock ret = 0; 1354fa9e4066Sahrens } 1355fa9e4066Sahrens 1356e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1357fa9e4066Sahrens 135899653d4eSeschrock return (ret); 1359fa9e4066Sahrens } 1360fa9e4066Sahrens 1361fa9e4066Sahrens /* 1362fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1363fa9e4066Sahrens * mounted datasets in the pool. 1364fa9e4066Sahrens */ 13654445fffbSMatthew Ahrens static int 13664445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce, 13674445fffbSMatthew Ahrens const char *log_str) 1368fa9e4066Sahrens { 1369fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 137089a89ebfSlling char msg[1024]; 1371fa9e4066Sahrens 137289a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 137389a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 137489a89ebfSlling 1375fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 137689a89ebfSlling zc.zc_cookie = force; 1377394ab0cbSGeorge Wilson zc.zc_guid = hardforce; 13784445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 137989a89ebfSlling 138089a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 138189a89ebfSlling switch (errno) { 138289a89ebfSlling case EXDEV: 138389a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 138489a89ebfSlling "use '-f' to override the following errors:\n" 138589a89ebfSlling "'%s' has an active shared spare which could be" 138689a89ebfSlling " used by other pools once '%s' is exported."), 138789a89ebfSlling zhp->zpool_name, zhp->zpool_name); 138889a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 138989a89ebfSlling msg)); 139089a89ebfSlling default: 139189a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 139289a89ebfSlling msg)); 139389a89ebfSlling } 139489a89ebfSlling } 1395fa9e4066Sahrens 1396fa9e4066Sahrens return (0); 1397fa9e4066Sahrens } 1398fa9e4066Sahrens 1399394ab0cbSGeorge Wilson int 14004445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str) 1401394ab0cbSGeorge Wilson { 14024445fffbSMatthew Ahrens return (zpool_export_common(zhp, force, B_FALSE, log_str)); 1403394ab0cbSGeorge Wilson } 1404394ab0cbSGeorge Wilson 1405394ab0cbSGeorge Wilson int 14064445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str) 1407394ab0cbSGeorge Wilson { 14084445fffbSMatthew Ahrens return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str)); 1409394ab0cbSGeorge Wilson } 1410394ab0cbSGeorge Wilson 1411468c413aSTim Haley static void 1412468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 14134b964adaSGeorge Wilson nvlist_t *config) 1414468c413aSTim Haley { 14154b964adaSGeorge Wilson nvlist_t *nv = NULL; 1416468c413aSTim Haley uint64_t rewindto; 1417468c413aSTim Haley int64_t loss = -1; 1418468c413aSTim Haley struct tm t; 1419468c413aSTim Haley char timestr[128]; 1420468c413aSTim Haley 14214b964adaSGeorge Wilson if (!hdl->libzfs_printerr || config == NULL) 14224b964adaSGeorge Wilson return; 14234b964adaSGeorge Wilson 1424ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1425ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) { 1426468c413aSTim Haley return; 1427ad135b5dSChristopher Siden } 1428468c413aSTim Haley 14294b964adaSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1430468c413aSTim Haley return; 14314b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1432468c413aSTim Haley 1433468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1434468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1435468c413aSTim Haley if (dryrun) { 1436468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1437468c413aSTim Haley "Would be able to return %s " 1438468c413aSTim Haley "to its state as of %s.\n"), 1439468c413aSTim Haley name, timestr); 1440468c413aSTim Haley } else { 1441468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1442468c413aSTim Haley "Pool %s returned to its state as of %s.\n"), 1443468c413aSTim Haley name, timestr); 1444468c413aSTim Haley } 1445468c413aSTim Haley if (loss > 120) { 1446468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1447468c413aSTim Haley "%s approximately %lld "), 1448468c413aSTim Haley dryrun ? "Would discard" : "Discarded", 1449468c413aSTim Haley (loss + 30) / 60); 1450468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1451468c413aSTim Haley "minutes of transactions.\n")); 1452468c413aSTim Haley } else if (loss > 0) { 1453468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1454468c413aSTim Haley "%s approximately %lld "), 1455468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss); 1456468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1457468c413aSTim Haley "seconds of transactions.\n")); 1458468c413aSTim Haley } 1459468c413aSTim Haley } 1460468c413aSTim Haley } 1461468c413aSTim Haley 1462468c413aSTim Haley void 1463468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1464468c413aSTim Haley nvlist_t *config) 1465468c413aSTim Haley { 14664b964adaSGeorge Wilson nvlist_t *nv = NULL; 1467468c413aSTim Haley int64_t loss = -1; 1468468c413aSTim Haley uint64_t edata = UINT64_MAX; 1469468c413aSTim Haley uint64_t rewindto; 1470468c413aSTim Haley struct tm t; 1471468c413aSTim Haley char timestr[128]; 1472468c413aSTim Haley 1473468c413aSTim Haley if (!hdl->libzfs_printerr) 1474468c413aSTim Haley return; 1475468c413aSTim Haley 1476468c413aSTim Haley if (reason >= 0) 1477468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1478468c413aSTim Haley else 1479468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1480468c413aSTim Haley 1481468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 14824b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1483ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 || 14844b964adaSGeorge Wilson nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1485468c413aSTim Haley goto no_info; 1486468c413aSTim Haley 14874b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 14884b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1489468c413aSTim Haley &edata); 1490468c413aSTim Haley 1491468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1492468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n")); 1493468c413aSTim Haley 1494468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1495468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1496468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1497468c413aSTim Haley "\tReturning the pool to its state as of %s\n" 1498468c413aSTim Haley "\tshould correct the problem. "), 1499468c413aSTim Haley timestr); 1500468c413aSTim Haley } else { 1501468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1502468c413aSTim Haley "\tReverting the pool to an earlier state " 1503468c413aSTim Haley "should correct the problem.\n\t")); 1504468c413aSTim Haley } 1505468c413aSTim Haley 1506468c413aSTim Haley if (loss > 120) { 1507468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1508468c413aSTim Haley "Approximately %lld minutes of data\n" 1509468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1510468c413aSTim Haley } else if (loss > 0) { 1511468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1512468c413aSTim Haley "Approximately %lld seconds of data\n" 1513468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss); 1514468c413aSTim Haley } 1515468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) { 1516468c413aSTim Haley if (edata == 1) { 1517468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1518468c413aSTim Haley "After rewind, at least\n" 1519468c413aSTim Haley "\tone persistent user-data error will remain. ")); 1520468c413aSTim Haley } else { 1521468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1522468c413aSTim Haley "After rewind, several\n" 1523468c413aSTim Haley "\tpersistent user-data errors will remain. ")); 1524468c413aSTim Haley } 1525468c413aSTim Haley } 1526468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1527a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1528a33cae98STim Haley reason >= 0 ? "clear" : "import", name); 1529468c413aSTim Haley 1530468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1531468c413aSTim Haley "A scrub of the pool\n" 1532468c413aSTim Haley "\tis strongly recommended after recovery.\n")); 1533468c413aSTim Haley return; 1534468c413aSTim Haley 1535468c413aSTim Haley no_info: 1536468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1537468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n")); 1538468c413aSTim Haley } 1539468c413aSTim Haley 1540fa9e4066Sahrens /* 1541990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1542990b4856Slling * if possible. 1543990b4856Slling * 1544990b4856Slling * Applications should use zpool_import_props() to import a pool with 1545990b4856Slling * new properties value to be set. 1546fa9e4066Sahrens */ 1547fa9e4066Sahrens int 154899653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1549990b4856Slling char *altroot) 1550990b4856Slling { 1551990b4856Slling nvlist_t *props = NULL; 1552990b4856Slling int ret; 1553990b4856Slling 1554990b4856Slling if (altroot != NULL) { 1555990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1556990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1557990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1558990b4856Slling newname)); 1559990b4856Slling } 1560990b4856Slling 1561990b4856Slling if (nvlist_add_string(props, 1562352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1563352d8027SGeorge Wilson nvlist_add_string(props, 1564352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1565990b4856Slling nvlist_free(props); 1566990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1567990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1568990b4856Slling newname)); 1569990b4856Slling } 1570990b4856Slling } 1571990b4856Slling 15724b964adaSGeorge Wilson ret = zpool_import_props(hdl, config, newname, props, 15734b964adaSGeorge Wilson ZFS_IMPORT_NORMAL); 1574aab83bb8SJosef 'Jeff' Sipek nvlist_free(props); 1575990b4856Slling return (ret); 1576990b4856Slling } 1577990b4856Slling 15784b964adaSGeorge Wilson static void 15794b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 15804b964adaSGeorge Wilson int indent) 15814b964adaSGeorge Wilson { 15824b964adaSGeorge Wilson nvlist_t **child; 15834b964adaSGeorge Wilson uint_t c, children; 15844b964adaSGeorge Wilson char *vname; 15854b964adaSGeorge Wilson uint64_t is_log = 0; 15864b964adaSGeorge Wilson 15874b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 15884b964adaSGeorge Wilson &is_log); 15894b964adaSGeorge Wilson 15904b964adaSGeorge Wilson if (name != NULL) 15914b964adaSGeorge Wilson (void) printf("\t%*s%s%s\n", indent, "", name, 15924b964adaSGeorge Wilson is_log ? " [log]" : ""); 15934b964adaSGeorge Wilson 15944b964adaSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 15954b964adaSGeorge Wilson &child, &children) != 0) 15964b964adaSGeorge Wilson return; 15974b964adaSGeorge Wilson 15984b964adaSGeorge Wilson for (c = 0; c < children; c++) { 15994b964adaSGeorge Wilson vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 16004b964adaSGeorge Wilson print_vdev_tree(hdl, vname, child[c], indent + 2); 16014b964adaSGeorge Wilson free(vname); 16024b964adaSGeorge Wilson } 16034b964adaSGeorge Wilson } 16044b964adaSGeorge Wilson 1605ad135b5dSChristopher Siden void 1606ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config) 1607ad135b5dSChristopher Siden { 1608ad135b5dSChristopher Siden nvlist_t *nvinfo, *unsup_feat; 1609ad135b5dSChristopher Siden 1610ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 1611ad135b5dSChristopher Siden 0); 1612ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT, 1613ad135b5dSChristopher Siden &unsup_feat) == 0); 1614ad135b5dSChristopher Siden 1615ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL; 1616ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(unsup_feat, nvp)) { 1617ad135b5dSChristopher Siden char *desc; 1618ad135b5dSChristopher Siden 1619ad135b5dSChristopher Siden verify(nvpair_type(nvp) == DATA_TYPE_STRING); 1620ad135b5dSChristopher Siden verify(nvpair_value_string(nvp, &desc) == 0); 1621ad135b5dSChristopher Siden 1622ad135b5dSChristopher Siden if (strlen(desc) > 0) 1623ad135b5dSChristopher Siden (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc); 1624ad135b5dSChristopher Siden else 1625ad135b5dSChristopher Siden (void) printf("\t%s\n", nvpair_name(nvp)); 1626ad135b5dSChristopher Siden } 1627ad135b5dSChristopher Siden } 1628ad135b5dSChristopher Siden 1629990b4856Slling /* 1630990b4856Slling * Import the given pool using the known configuration and a list of 1631990b4856Slling * properties to be set. The configuration should have come from 1632990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1633990b4856Slling * is imported with a different name. 1634990b4856Slling */ 1635990b4856Slling int 1636990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 16374b964adaSGeorge Wilson nvlist_t *props, int flags) 1638fa9e4066Sahrens { 1639e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1640468c413aSTim Haley zpool_rewind_policy_t policy; 16414b964adaSGeorge Wilson nvlist_t *nv = NULL; 16424b964adaSGeorge Wilson nvlist_t *nvinfo = NULL; 16434b964adaSGeorge Wilson nvlist_t *missing = NULL; 1644fa9e4066Sahrens char *thename; 1645fa9e4066Sahrens char *origname; 1646fa9e4066Sahrens int ret; 16474b964adaSGeorge Wilson int error = 0; 1648990b4856Slling char errbuf[1024]; 1649fa9e4066Sahrens 1650fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1651fa9e4066Sahrens &origname) == 0); 1652fa9e4066Sahrens 1653990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1654990b4856Slling "cannot import pool '%s'"), origname); 1655990b4856Slling 1656fa9e4066Sahrens if (newname != NULL) { 165799653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1658ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 165999653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 166099653d4eSeschrock newname)); 1661fa9e4066Sahrens thename = (char *)newname; 1662fa9e4066Sahrens } else { 1663fa9e4066Sahrens thename = origname; 1664fa9e4066Sahrens } 1665fa9e4066Sahrens 1666078266a5SMarcel Telka if (props != NULL) { 1667990b4856Slling uint64_t version; 1668f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 1669fa9e4066Sahrens 1670990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1671990b4856Slling &version) == 0); 1672fa9e4066Sahrens 16730a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1674078266a5SMarcel Telka props, version, flags, errbuf)) == NULL) 1675990b4856Slling return (-1); 1676078266a5SMarcel Telka if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1677351420b3Slling nvlist_free(props); 1678990b4856Slling return (-1); 1679351420b3Slling } 1680078266a5SMarcel Telka nvlist_free(props); 1681990b4856Slling } 1682990b4856Slling 1683990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1684fa9e4066Sahrens 1685fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1686ea8dc4b6Seschrock &zc.zc_guid) == 0); 1687fa9e4066Sahrens 1688351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1689078266a5SMarcel Telka zcmd_free_nvlists(&zc); 169099653d4eSeschrock return (-1); 1691351420b3Slling } 169257f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1693078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1694468c413aSTim Haley return (-1); 1695468c413aSTim Haley } 1696fa9e4066Sahrens 16974b964adaSGeorge Wilson zc.zc_cookie = flags; 16984b964adaSGeorge Wilson while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 16994b964adaSGeorge Wilson errno == ENOMEM) { 17004b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 17014b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 17024b964adaSGeorge Wilson return (-1); 17034b964adaSGeorge Wilson } 17044b964adaSGeorge Wilson } 17054b964adaSGeorge Wilson if (ret != 0) 17064b964adaSGeorge Wilson error = errno; 17074b964adaSGeorge Wilson 17084b964adaSGeorge Wilson (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1709078266a5SMarcel Telka 1710078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1711078266a5SMarcel Telka 17124b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 17134b964adaSGeorge Wilson 17144b964adaSGeorge Wilson if (error) { 1715fa9e4066Sahrens char desc[1024]; 1716468c413aSTim Haley 1717468c413aSTim Haley /* 1718468c413aSTim Haley * Dry-run failed, but we print out what success 1719468c413aSTim Haley * looks like if we found a best txg 1720468c413aSTim Haley */ 17214b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_TRY_REWIND) { 1722468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 17234b964adaSGeorge Wilson B_TRUE, nv); 17244b964adaSGeorge Wilson nvlist_free(nv); 1725468c413aSTim Haley return (-1); 1726468c413aSTim Haley } 1727468c413aSTim Haley 1728fa9e4066Sahrens if (newname == NULL) 1729fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1730fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1731fa9e4066Sahrens thename); 1732fa9e4066Sahrens else 1733fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1734fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1735fa9e4066Sahrens origname, thename); 1736fa9e4066Sahrens 17374b964adaSGeorge Wilson switch (error) { 1738ea8dc4b6Seschrock case ENOTSUP: 1739ad135b5dSChristopher Siden if (nv != NULL && nvlist_lookup_nvlist(nv, 1740ad135b5dSChristopher Siden ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1741ad135b5dSChristopher Siden nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) { 1742ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, "This " 1743ad135b5dSChristopher Siden "pool uses the following feature(s) not " 1744ad135b5dSChristopher Siden "supported by this system:\n")); 1745ad135b5dSChristopher Siden zpool_print_unsup_feat(nv); 1746ad135b5dSChristopher Siden if (nvlist_exists(nvinfo, 1747ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY)) { 1748ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, 1749ad135b5dSChristopher Siden "All unsupported features are only " 1750ad135b5dSChristopher Siden "required for writing to the pool." 1751ad135b5dSChristopher Siden "\nThe pool can be imported using " 1752ad135b5dSChristopher Siden "'-o readonly=on'.\n")); 1753ad135b5dSChristopher Siden } 1754ad135b5dSChristopher Siden } 1755ea8dc4b6Seschrock /* 1756ea8dc4b6Seschrock * Unsupported version. 1757ea8dc4b6Seschrock */ 175899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1759ea8dc4b6Seschrock break; 1760ea8dc4b6Seschrock 1761b5989ec7Seschrock case EINVAL: 1762b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1763b5989ec7Seschrock break; 1764b5989ec7Seschrock 176554a91118SChris Kirby case EROFS: 176654a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 176754a91118SChris Kirby "one or more devices is read only")); 176854a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc); 176954a91118SChris Kirby break; 177054a91118SChris Kirby 17714b964adaSGeorge Wilson case ENXIO: 17724b964adaSGeorge Wilson if (nv && nvlist_lookup_nvlist(nv, 17734b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 17744b964adaSGeorge Wilson nvlist_lookup_nvlist(nvinfo, 17754b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 17764b964adaSGeorge Wilson (void) printf(dgettext(TEXT_DOMAIN, 17774b964adaSGeorge Wilson "The devices below are missing, use " 17784b964adaSGeorge Wilson "'-m' to import the pool anyway:\n")); 17794b964adaSGeorge Wilson print_vdev_tree(hdl, NULL, missing, 2); 17804b964adaSGeorge Wilson (void) printf("\n"); 17814b964adaSGeorge Wilson } 17824b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 17834b964adaSGeorge Wilson break; 17844b964adaSGeorge Wilson 17854b964adaSGeorge Wilson case EEXIST: 17864b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 17874b964adaSGeorge Wilson break; 17884b964adaSGeorge Wilson 1789fa9e4066Sahrens default: 17904b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 1791468c413aSTim Haley zpool_explain_recover(hdl, 17924b964adaSGeorge Wilson newname ? origname : thename, -error, nv); 1793468c413aSTim Haley break; 1794fa9e4066Sahrens } 1795fa9e4066Sahrens 17964b964adaSGeorge Wilson nvlist_free(nv); 1797fa9e4066Sahrens ret = -1; 1798fa9e4066Sahrens } else { 1799fa9e4066Sahrens zpool_handle_t *zhp; 1800ecd6cf80Smarks 1801fa9e4066Sahrens /* 1802fa9e4066Sahrens * This should never fail, but play it safe anyway. 1803fa9e4066Sahrens */ 1804681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0) 180594de1d4cSeschrock ret = -1; 1806681d9761SEric Taylor else if (zhp != NULL) 1807fa9e4066Sahrens zpool_close(zhp); 1808468c413aSTim Haley if (policy.zrp_request & 1809468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1810468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 18114b964adaSGeorge Wilson ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 1812468c413aSTim Haley } 18134b964adaSGeorge Wilson nvlist_free(nv); 1814468c413aSTim Haley return (0); 1815fa9e4066Sahrens } 1816fa9e4066Sahrens 1817fa9e4066Sahrens return (ret); 1818fa9e4066Sahrens } 1819fa9e4066Sahrens 1820fa9e4066Sahrens /* 18213f9d6ad7SLin Ling * Scan the pool. 1822fa9e4066Sahrens */ 1823fa9e4066Sahrens int 18243f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1825fa9e4066Sahrens { 1826fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1827fa9e4066Sahrens char msg[1024]; 182899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1829fa9e4066Sahrens 1830fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18313f9d6ad7SLin Ling zc.zc_cookie = func; 1832fa9e4066Sahrens 1833cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 18343f9d6ad7SLin Ling (errno == ENOENT && func != POOL_SCAN_NONE)) 1835fa9e4066Sahrens return (0); 1836fa9e4066Sahrens 18373f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) { 18383f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 18393f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 18403f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) { 18413f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 18423f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 18433f9d6ad7SLin Ling zc.zc_name); 18443f9d6ad7SLin Ling } else { 18453f9d6ad7SLin Ling assert(!"unexpected result"); 18463f9d6ad7SLin Ling } 1847fa9e4066Sahrens 18483f9d6ad7SLin Ling if (errno == EBUSY) { 18493f9d6ad7SLin Ling nvlist_t *nvroot; 18503f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 18513f9d6ad7SLin Ling uint_t psc; 18523f9d6ad7SLin Ling 18533f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config, 18543f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 18553f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 18563f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 18573f9d6ad7SLin Ling if (ps && ps->pss_func == POOL_SCAN_SCRUB) 18583f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 18593f9d6ad7SLin Ling else 18603f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg)); 18613f9d6ad7SLin Ling } else if (errno == ENOENT) { 18623f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 18633f9d6ad7SLin Ling } else { 186499653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 18653f9d6ad7SLin Ling } 1866fa9e4066Sahrens } 1867fa9e4066Sahrens 18683fdda499SJohn Harres /* 18693fdda499SJohn Harres * This provides a very minimal check whether a given string is likely a 18703fdda499SJohn Harres * c#t#d# style string. Users of this are expected to do their own 18713fdda499SJohn Harres * verification of the s# part. 18723fdda499SJohn Harres */ 18733fdda499SJohn Harres #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 18743fdda499SJohn Harres 18753fdda499SJohn Harres /* 18763fdda499SJohn Harres * More elaborate version for ones which may start with "/dev/dsk/" 18773fdda499SJohn Harres * and the like. 18783fdda499SJohn Harres */ 18793fdda499SJohn Harres static int 18809a686fbcSPaul Dagnelie ctd_check_path(char *str) 18819a686fbcSPaul Dagnelie { 18823fdda499SJohn Harres /* 18833fdda499SJohn Harres * If it starts with a slash, check the last component. 18843fdda499SJohn Harres */ 18853fdda499SJohn Harres if (str && str[0] == '/') { 18863fdda499SJohn Harres char *tmp = strrchr(str, '/'); 18873fdda499SJohn Harres 18883fdda499SJohn Harres /* 18893fdda499SJohn Harres * If it ends in "/old", check the second-to-last 18903fdda499SJohn Harres * component of the string instead. 18913fdda499SJohn Harres */ 18923fdda499SJohn Harres if (tmp != str && strcmp(tmp, "/old") == 0) { 18933fdda499SJohn Harres for (tmp--; *tmp != '/'; tmp--) 18943fdda499SJohn Harres ; 18953fdda499SJohn Harres } 18963fdda499SJohn Harres str = tmp + 1; 18973fdda499SJohn Harres } 18983fdda499SJohn Harres return (CTD_CHECK(str)); 18993fdda499SJohn Harres } 19003fdda499SJohn Harres 1901a43d325bSek /* 1902573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the 1903573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device. 1904a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1905a43d325bSek * spare; but FALSE if its an INUSE spare. 1906a43d325bSek */ 190799653d4eSeschrock static nvlist_t * 1908573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 1909573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log) 1910ea8dc4b6Seschrock { 1911ea8dc4b6Seschrock uint_t c, children; 1912ea8dc4b6Seschrock nvlist_t **child; 191399653d4eSeschrock nvlist_t *ret; 1914ee0eb9f2SEric Schrock uint64_t is_log; 1915573ca77eSGeorge Wilson char *srchkey; 1916573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL); 1917573ca77eSGeorge Wilson 1918573ca77eSGeorge Wilson /* Nothing to look for */ 1919573ca77eSGeorge Wilson if (search == NULL || pair == NULL) 1920573ca77eSGeorge Wilson return (NULL); 1921ea8dc4b6Seschrock 1922573ca77eSGeorge Wilson /* Obtain the key we will use to search */ 1923573ca77eSGeorge Wilson srchkey = nvpair_name(pair); 1924573ca77eSGeorge Wilson 1925573ca77eSGeorge Wilson switch (nvpair_type(pair)) { 1926cb04b873SMark J Musante case DATA_TYPE_UINT64: 1927573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 1928cb04b873SMark J Musante uint64_t srchval, theguid; 1929cb04b873SMark J Musante 1930cb04b873SMark J Musante verify(nvpair_value_uint64(pair, &srchval) == 0); 1931cb04b873SMark J Musante verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1932cb04b873SMark J Musante &theguid) == 0); 1933cb04b873SMark J Musante if (theguid == srchval) 1934cb04b873SMark J Musante return (nv); 1935573ca77eSGeorge Wilson } 1936573ca77eSGeorge Wilson break; 1937573ca77eSGeorge Wilson 1938573ca77eSGeorge Wilson case DATA_TYPE_STRING: { 1939573ca77eSGeorge Wilson char *srchval, *val; 1940573ca77eSGeorge Wilson 1941573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0); 1942573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0) 1943573ca77eSGeorge Wilson break; 1944ea8dc4b6Seschrock 1945ea8dc4b6Seschrock /* 19463fdda499SJohn Harres * Search for the requested value. Special cases: 19473fdda499SJohn Harres * 19483fdda499SJohn Harres * - ZPOOL_CONFIG_PATH for whole disk entries. These end in 19493fdda499SJohn Harres * "s0" or "s0/old". The "s0" part is hidden from the user, 19503fdda499SJohn Harres * but included in the string, so this matches around it. 19513fdda499SJohn Harres * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 19523fdda499SJohn Harres * 195388ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares. 1954ea8dc4b6Seschrock */ 19553fdda499SJohn Harres if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 19563fdda499SJohn Harres ctd_check_path(val)) { 1957573ca77eSGeorge Wilson uint64_t wholedisk = 0; 1958573ca77eSGeorge Wilson 1959573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1960573ca77eSGeorge Wilson &wholedisk); 1961573ca77eSGeorge Wilson if (wholedisk) { 19623fdda499SJohn Harres int slen = strlen(srchval); 19633fdda499SJohn Harres int vlen = strlen(val); 19643fdda499SJohn Harres 19653fdda499SJohn Harres if (slen != vlen - 2) 19663fdda499SJohn Harres break; 19673fdda499SJohn Harres 19683fdda499SJohn Harres /* 19693fdda499SJohn Harres * make_leaf_vdev() should only set 19703fdda499SJohn Harres * wholedisk for ZPOOL_CONFIG_PATHs which 19713fdda499SJohn Harres * will include "/dev/dsk/", giving plenty of 19723fdda499SJohn Harres * room for the indices used next. 19733fdda499SJohn Harres */ 19743fdda499SJohn Harres ASSERT(vlen >= 6); 19753fdda499SJohn Harres 19763fdda499SJohn Harres /* 19773fdda499SJohn Harres * strings identical except trailing "s0" 19783fdda499SJohn Harres */ 19793fdda499SJohn Harres if (strcmp(&val[vlen - 2], "s0") == 0 && 19803fdda499SJohn Harres strncmp(srchval, val, slen) == 0) 19813fdda499SJohn Harres return (nv); 19823fdda499SJohn Harres 1983573ca77eSGeorge Wilson /* 19843fdda499SJohn Harres * strings identical except trailing "s0/old" 1985573ca77eSGeorge Wilson */ 19863fdda499SJohn Harres if (strcmp(&val[vlen - 6], "s0/old") == 0 && 19873fdda499SJohn Harres strcmp(&srchval[slen - 4], "/old") == 0 && 19883fdda499SJohn Harres strncmp(srchval, val, slen - 4) == 0) 1989573ca77eSGeorge Wilson return (nv); 19903fdda499SJohn Harres 1991573ca77eSGeorge Wilson break; 1992573ca77eSGeorge Wilson } 199388ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 199488ecc943SGeorge Wilson char *type, *idx, *end, *p; 199588ecc943SGeorge Wilson uint64_t id, vdev_id; 199688ecc943SGeorge Wilson 199788ecc943SGeorge Wilson /* 199888ecc943SGeorge Wilson * Determine our vdev type, keeping in mind 199988ecc943SGeorge Wilson * that the srchval is composed of a type and 200088ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4). 200188ecc943SGeorge Wilson */ 200288ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL) 200388ecc943SGeorge Wilson return (NULL); 200488ecc943SGeorge Wilson 200588ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) { 200688ecc943SGeorge Wilson free(type); 200788ecc943SGeorge Wilson break; 200888ecc943SGeorge Wilson } 200988ecc943SGeorge Wilson idx = p + 1; 201088ecc943SGeorge Wilson *p = '\0'; 201188ecc943SGeorge Wilson 201288ecc943SGeorge Wilson /* 201388ecc943SGeorge Wilson * If the types don't match then keep looking. 201488ecc943SGeorge Wilson */ 201588ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) { 201688ecc943SGeorge Wilson free(type); 201788ecc943SGeorge Wilson break; 201888ecc943SGeorge Wilson } 201988ecc943SGeorge Wilson 202088ecc943SGeorge Wilson verify(strncmp(type, VDEV_TYPE_RAIDZ, 202188ecc943SGeorge Wilson strlen(VDEV_TYPE_RAIDZ)) == 0 || 202288ecc943SGeorge Wilson strncmp(type, VDEV_TYPE_MIRROR, 202388ecc943SGeorge Wilson strlen(VDEV_TYPE_MIRROR)) == 0); 202488ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 202588ecc943SGeorge Wilson &id) == 0); 202688ecc943SGeorge Wilson 202788ecc943SGeorge Wilson errno = 0; 202888ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10); 202988ecc943SGeorge Wilson 203088ecc943SGeorge Wilson free(type); 203188ecc943SGeorge Wilson if (errno != 0) 203288ecc943SGeorge Wilson return (NULL); 203388ecc943SGeorge Wilson 203488ecc943SGeorge Wilson /* 203588ecc943SGeorge Wilson * Now verify that we have the correct vdev id. 203688ecc943SGeorge Wilson */ 203788ecc943SGeorge Wilson if (vdev_id == id) 203888ecc943SGeorge Wilson return (nv); 2039ea8dc4b6Seschrock } 2040573ca77eSGeorge Wilson 2041573ca77eSGeorge Wilson /* 2042573ca77eSGeorge Wilson * Common case 2043573ca77eSGeorge Wilson */ 2044573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0) 2045573ca77eSGeorge Wilson return (nv); 2046573ca77eSGeorge Wilson break; 2047573ca77eSGeorge Wilson } 2048573ca77eSGeorge Wilson 2049573ca77eSGeorge Wilson default: 2050573ca77eSGeorge Wilson break; 2051ea8dc4b6Seschrock } 2052ea8dc4b6Seschrock 2053ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2054ea8dc4b6Seschrock &child, &children) != 0) 205599653d4eSeschrock return (NULL); 2056ea8dc4b6Seschrock 2057ee0eb9f2SEric Schrock for (c = 0; c < children; c++) { 2058573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2059ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2060ee0eb9f2SEric Schrock /* 2061ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel 2062ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the 2063ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where 2064ee0eb9f2SEric Schrock * 'log' is non-NULL). 2065ee0eb9f2SEric Schrock */ 2066ee0eb9f2SEric Schrock if (log != NULL && 2067ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c], 2068ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 2069ee0eb9f2SEric Schrock is_log) { 2070ee0eb9f2SEric Schrock *log = B_TRUE; 2071ee0eb9f2SEric Schrock } 2072ea8dc4b6Seschrock return (ret); 2073ee0eb9f2SEric Schrock } 2074ee0eb9f2SEric Schrock } 2075ea8dc4b6Seschrock 207699653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 207799653d4eSeschrock &child, &children) == 0) { 207899653d4eSeschrock for (c = 0; c < children; c++) { 2079573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2080ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2081a43d325bSek *avail_spare = B_TRUE; 208299653d4eSeschrock return (ret); 208399653d4eSeschrock } 208499653d4eSeschrock } 208599653d4eSeschrock } 208699653d4eSeschrock 2087fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2088fa94a07fSbrendan &child, &children) == 0) { 2089fa94a07fSbrendan for (c = 0; c < children; c++) { 2090573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2091ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2092fa94a07fSbrendan *l2cache = B_TRUE; 2093fa94a07fSbrendan return (ret); 2094fa94a07fSbrendan } 2095fa94a07fSbrendan } 2096fa94a07fSbrendan } 2097fa94a07fSbrendan 209899653d4eSeschrock return (NULL); 2099ea8dc4b6Seschrock } 2100ea8dc4b6Seschrock 2101573ca77eSGeorge Wilson /* 2102573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the 2103573ca77eSGeorge Wilson * associated vdev. 2104573ca77eSGeorge Wilson */ 2105573ca77eSGeorge Wilson nvlist_t * 2106573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 2107573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 2108573ca77eSGeorge Wilson { 2109573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret; 2110573ca77eSGeorge Wilson 2111573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2112573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 2113573ca77eSGeorge Wilson 2114573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2115573ca77eSGeorge Wilson &nvroot) == 0); 2116573ca77eSGeorge Wilson 2117573ca77eSGeorge Wilson *avail_spare = B_FALSE; 2118cb04b873SMark J Musante *l2cache = B_FALSE; 2119daeb70e5SMark J Musante if (log != NULL) 2120daeb70e5SMark J Musante *log = B_FALSE; 2121573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2122573ca77eSGeorge Wilson nvlist_free(search); 2123573ca77eSGeorge Wilson 2124573ca77eSGeorge Wilson return (ret); 2125573ca77eSGeorge Wilson } 2126573ca77eSGeorge Wilson 212788ecc943SGeorge Wilson /* 212888ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 212988ecc943SGeorge Wilson */ 213088ecc943SGeorge Wilson boolean_t 213188ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name) 213288ecc943SGeorge Wilson { 213388ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 213488ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 213588ecc943SGeorge Wilson return (B_TRUE); 213688ecc943SGeorge Wilson return (B_FALSE); 213788ecc943SGeorge Wilson } 213888ecc943SGeorge Wilson 213999653d4eSeschrock nvlist_t * 2140fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 2141ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log) 2142ea8dc4b6Seschrock { 2143ea8dc4b6Seschrock char buf[MAXPATHLEN]; 2144ea8dc4b6Seschrock char *end; 2145573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret; 2146ea8dc4b6Seschrock uint64_t guid; 2147ea8dc4b6Seschrock 2148573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2149573ca77eSGeorge Wilson 21500917b783Seschrock guid = strtoull(path, &end, 10); 2151ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 2152573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 215388ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) { 215488ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 2155ea8dc4b6Seschrock } else if (path[0] != '/') { 2156*6401734dSWill Andrews (void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT, 2157*6401734dSWill Andrews path); 2158573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 2159ea8dc4b6Seschrock } else { 2160573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 2161ea8dc4b6Seschrock } 2162ea8dc4b6Seschrock 2163ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2164ea8dc4b6Seschrock &nvroot) == 0); 2165ea8dc4b6Seschrock 2166a43d325bSek *avail_spare = B_FALSE; 2167fa94a07fSbrendan *l2cache = B_FALSE; 2168ee0eb9f2SEric Schrock if (log != NULL) 2169ee0eb9f2SEric Schrock *log = B_FALSE; 2170573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2171573ca77eSGeorge Wilson nvlist_free(search); 2172573ca77eSGeorge Wilson 2173573ca77eSGeorge Wilson return (ret); 2174a43d325bSek } 2175a43d325bSek 217619397407SSherry Moore static int 217719397407SSherry Moore vdev_online(nvlist_t *nv) 217819397407SSherry Moore { 217919397407SSherry Moore uint64_t ival; 218019397407SSherry Moore 218119397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 218219397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 218319397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 218419397407SSherry Moore return (0); 218519397407SSherry Moore 218619397407SSherry Moore return (1); 218719397407SSherry Moore } 218819397407SSherry Moore 218919397407SSherry Moore /* 219021ecdf64SLin Ling * Helper function for zpool_get_physpaths(). 219119397407SSherry Moore */ 2192753a6d45SSherry Moore static int 219321ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 2194753a6d45SSherry Moore size_t *bytes_written) 219519397407SSherry Moore { 2196753a6d45SSherry Moore size_t bytes_left, pos, rsz; 2197753a6d45SSherry Moore char *tmppath; 2198753a6d45SSherry Moore const char *format; 2199753a6d45SSherry Moore 2200753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 2201753a6d45SSherry Moore &tmppath) != 0) 2202753a6d45SSherry Moore return (EZFS_NODEVICE); 2203753a6d45SSherry Moore 2204753a6d45SSherry Moore pos = *bytes_written; 2205753a6d45SSherry Moore bytes_left = physpath_size - pos; 2206753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s"; 2207753a6d45SSherry Moore 2208753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 2209753a6d45SSherry Moore *bytes_written += rsz; 2210753a6d45SSherry Moore 2211753a6d45SSherry Moore if (rsz >= bytes_left) { 2212753a6d45SSherry Moore /* if physpath was not copied properly, clear it */ 2213753a6d45SSherry Moore if (bytes_left != 0) { 2214753a6d45SSherry Moore physpath[pos] = 0; 2215753a6d45SSherry Moore } 2216753a6d45SSherry Moore return (EZFS_NOSPC); 2217753a6d45SSherry Moore } 2218753a6d45SSherry Moore return (0); 2219753a6d45SSherry Moore } 2220753a6d45SSherry Moore 222121ecdf64SLin Ling static int 222221ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 222321ecdf64SLin Ling size_t *rsz, boolean_t is_spare) 222421ecdf64SLin Ling { 222521ecdf64SLin Ling char *type; 222621ecdf64SLin Ling int ret; 222721ecdf64SLin Ling 222821ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 222921ecdf64SLin Ling return (EZFS_INVALCONFIG); 223021ecdf64SLin Ling 223121ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) { 223221ecdf64SLin Ling /* 223321ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 223421ecdf64SLin Ling * For a spare vdev, we only want to boot from the active 223521ecdf64SLin Ling * spare device. 223621ecdf64SLin Ling */ 223721ecdf64SLin Ling if (is_spare) { 223821ecdf64SLin Ling uint64_t spare = 0; 223921ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 224021ecdf64SLin Ling &spare); 224121ecdf64SLin Ling if (!spare) 224221ecdf64SLin Ling return (EZFS_INVALCONFIG); 224321ecdf64SLin Ling } 224421ecdf64SLin Ling 224521ecdf64SLin Ling if (vdev_online(nv)) { 224621ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath, 224721ecdf64SLin Ling phypath_size, rsz)) != 0) 224821ecdf64SLin Ling return (ret); 224921ecdf64SLin Ling } 225021ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 225121ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 || 225221ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 225321ecdf64SLin Ling nvlist_t **child; 225421ecdf64SLin Ling uint_t count; 225521ecdf64SLin Ling int i, ret; 225621ecdf64SLin Ling 225721ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv, 225821ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 225921ecdf64SLin Ling return (EZFS_INVALCONFIG); 226021ecdf64SLin Ling 226121ecdf64SLin Ling for (i = 0; i < count; i++) { 226221ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath, 226321ecdf64SLin Ling phypath_size, rsz, is_spare); 226421ecdf64SLin Ling if (ret == EZFS_NOSPC) 226521ecdf64SLin Ling return (ret); 226621ecdf64SLin Ling } 226721ecdf64SLin Ling } 226821ecdf64SLin Ling 226921ecdf64SLin Ling return (EZFS_POOL_INVALARG); 227021ecdf64SLin Ling } 227121ecdf64SLin Ling 2272753a6d45SSherry Moore /* 2273753a6d45SSherry Moore * Get phys_path for a root pool config. 2274753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2275753a6d45SSherry Moore */ 2276753a6d45SSherry Moore static int 2277753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2278753a6d45SSherry Moore { 2279753a6d45SSherry Moore size_t rsz; 228019397407SSherry Moore nvlist_t *vdev_root; 228119397407SSherry Moore nvlist_t **child; 228219397407SSherry Moore uint_t count; 2283753a6d45SSherry Moore char *type; 2284753a6d45SSherry Moore 2285753a6d45SSherry Moore rsz = 0; 2286753a6d45SSherry Moore 2287753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2288753a6d45SSherry Moore &vdev_root) != 0) 2289753a6d45SSherry Moore return (EZFS_INVALCONFIG); 2290753a6d45SSherry Moore 2291753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2292753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2293753a6d45SSherry Moore &child, &count) != 0) 2294753a6d45SSherry Moore return (EZFS_INVALCONFIG); 229519397407SSherry Moore 229619397407SSherry Moore /* 22971a902ef8SHans Rosenfeld * root pool can only have a single top-level vdev. 229819397407SSherry Moore */ 22991a902ef8SHans Rosenfeld if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1) 2300753a6d45SSherry Moore return (EZFS_POOL_INVALARG); 230119397407SSherry Moore 230221ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 230321ecdf64SLin Ling B_FALSE); 230419397407SSherry Moore 2305753a6d45SSherry Moore /* No online devices */ 2306753a6d45SSherry Moore if (rsz == 0) 2307753a6d45SSherry Moore return (EZFS_NODEVICE); 2308753a6d45SSherry Moore 230919397407SSherry Moore return (0); 231019397407SSherry Moore } 231119397407SSherry Moore 2312753a6d45SSherry Moore /* 2313753a6d45SSherry Moore * Get phys_path for a root pool 2314753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2315753a6d45SSherry Moore */ 2316753a6d45SSherry Moore int 2317753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2318753a6d45SSherry Moore { 2319753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2320753a6d45SSherry Moore phypath_size)); 2321753a6d45SSherry Moore } 2322753a6d45SSherry Moore 2323573ca77eSGeorge Wilson /* 2324573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel 2325573ca77eSGeorge Wilson * the disk to use the new unallocated space. 2326573ca77eSGeorge Wilson */ 2327573ca77eSGeorge Wilson static int 2328573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2329573ca77eSGeorge Wilson { 2330573ca77eSGeorge Wilson char path[MAXPATHLEN]; 2331573ca77eSGeorge Wilson char errbuf[1024]; 2332573ca77eSGeorge Wilson int fd, error; 2333573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int); 2334573ca77eSGeorge Wilson 2335573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2336573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL) 2337573ca77eSGeorge Wilson return (-1); 2338573ca77eSGeorge Wilson 2339*6401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name); 2340573ca77eSGeorge Wilson 2341573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2342573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2343573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name); 2344573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2345573ca77eSGeorge Wilson } 2346573ca77eSGeorge Wilson 2347573ca77eSGeorge Wilson /* 2348573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device 2349573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply 2350573ca77eSGeorge Wilson * ignore that error and continue on. 2351573ca77eSGeorge Wilson */ 2352573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd); 2353573ca77eSGeorge Wilson (void) close(fd); 2354573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) { 2355573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2356573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name); 2357573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2358573ca77eSGeorge Wilson } 2359573ca77eSGeorge Wilson return (0); 2360573ca77eSGeorge Wilson } 2361573ca77eSGeorge Wilson 2362fa9e4066Sahrens /* 23633d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 23643d7072f8Seschrock * ZFS_ONLINE_* flags. 2365fa9e4066Sahrens */ 2366fa9e4066Sahrens int 23673d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 23683d7072f8Seschrock vdev_state_t *newstate) 2369fa9e4066Sahrens { 2370fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2371fa9e4066Sahrens char msg[1024]; 237299653d4eSeschrock nvlist_t *tgt; 2373573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog; 237499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2375fa9e4066Sahrens 2376573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) { 2377573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2378573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2379573ca77eSGeorge Wilson } else { 2380573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2381573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2382573ca77eSGeorge Wilson } 2383ea8dc4b6Seschrock 2384fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2385ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2386573ca77eSGeorge Wilson &islog)) == NULL) 238799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2388fa9e4066Sahrens 238999653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2390fa9e4066Sahrens 2391069f55e2SEric Schrock if (avail_spare) 2392a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2393a43d325bSek 2394573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND || 2395573ca77eSGeorge Wilson zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 2396573ca77eSGeorge Wilson char *pathname = NULL; 2397573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2398573ca77eSGeorge Wilson 2399573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2400573ca77eSGeorge Wilson &wholedisk); 2401573ca77eSGeorge Wilson verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 2402573ca77eSGeorge Wilson &pathname) == 0); 2403573ca77eSGeorge Wilson 2404573ca77eSGeorge Wilson /* 2405573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion. 2406573ca77eSGeorge Wilson */ 2407573ca77eSGeorge Wilson if (l2cache) { 2408573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2409573ca77eSGeorge Wilson "cannot expand cache devices")); 2410573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2411573ca77eSGeorge Wilson } 2412573ca77eSGeorge Wilson 2413573ca77eSGeorge Wilson if (wholedisk) { 2414*6401734dSWill Andrews pathname += strlen(ZFS_DISK_ROOT) + 1; 2415cb04b873SMark J Musante (void) zpool_relabel_disk(hdl, pathname); 2416573ca77eSGeorge Wilson } 2417573ca77eSGeorge Wilson } 2418573ca77eSGeorge Wilson 24193d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 24203d7072f8Seschrock zc.zc_obj = flags; 2421fa9e4066Sahrens 2422cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 24231195e687SMark J Musante if (errno == EINVAL) { 24241195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 24251195e687SMark J Musante "from this pool into a new one. Use '%s' " 24261195e687SMark J Musante "instead"), "zpool detach"); 24271195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 24281195e687SMark J Musante } 24293d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 24301195e687SMark J Musante } 24313d7072f8Seschrock 24323d7072f8Seschrock *newstate = zc.zc_cookie; 24333d7072f8Seschrock return (0); 2434fa9e4066Sahrens } 2435fa9e4066Sahrens 2436fa9e4066Sahrens /* 2437fa9e4066Sahrens * Take the specified vdev offline 2438fa9e4066Sahrens */ 2439fa9e4066Sahrens int 24403d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2441fa9e4066Sahrens { 2442fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2443fa9e4066Sahrens char msg[1024]; 244499653d4eSeschrock nvlist_t *tgt; 2445fa94a07fSbrendan boolean_t avail_spare, l2cache; 244699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2447fa9e4066Sahrens 2448ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2449ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2450ea8dc4b6Seschrock 2451fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2452ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2453ee0eb9f2SEric Schrock NULL)) == NULL) 245499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 245599653d4eSeschrock 245699653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2457fa9e4066Sahrens 2458069f55e2SEric Schrock if (avail_spare) 2459a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2460a43d325bSek 24613d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 24623d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 24633d7072f8Seschrock 2464cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 24653d7072f8Seschrock return (0); 24663d7072f8Seschrock 24673d7072f8Seschrock switch (errno) { 24683d7072f8Seschrock case EBUSY: 24693d7072f8Seschrock 24703d7072f8Seschrock /* 24713d7072f8Seschrock * There are no other replicas of this device. 24723d7072f8Seschrock */ 24733d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 24743d7072f8Seschrock 2475e6ca193dSGeorge Wilson case EEXIST: 2476e6ca193dSGeorge Wilson /* 2477e6ca193dSGeorge Wilson * The log device has unplayed logs 2478e6ca193dSGeorge Wilson */ 2479e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2480e6ca193dSGeorge Wilson 24813d7072f8Seschrock default: 24823d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 24833d7072f8Seschrock } 24843d7072f8Seschrock } 24853d7072f8Seschrock 24863d7072f8Seschrock /* 24873d7072f8Seschrock * Mark the given vdev faulted. 24883d7072f8Seschrock */ 24893d7072f8Seschrock int 2490069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 24913d7072f8Seschrock { 24923d7072f8Seschrock zfs_cmd_t zc = { 0 }; 24933d7072f8Seschrock char msg[1024]; 24943d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 24953d7072f8Seschrock 24963d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 24973d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2498441d80aaSlling 24993d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25003d7072f8Seschrock zc.zc_guid = guid; 25013d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 2502069f55e2SEric Schrock zc.zc_obj = aux; 25033d7072f8Seschrock 2504cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2505fa9e4066Sahrens return (0); 2506fa9e4066Sahrens 2507fa9e4066Sahrens switch (errno) { 250899653d4eSeschrock case EBUSY: 2509fa9e4066Sahrens 2510fa9e4066Sahrens /* 2511fa9e4066Sahrens * There are no other replicas of this device. 2512fa9e4066Sahrens */ 251399653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2514fa9e4066Sahrens 251599653d4eSeschrock default: 251699653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2517fa9e4066Sahrens } 25183d7072f8Seschrock 25193d7072f8Seschrock } 25203d7072f8Seschrock 25213d7072f8Seschrock /* 25223d7072f8Seschrock * Mark the given vdev degraded. 25233d7072f8Seschrock */ 25243d7072f8Seschrock int 2525069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 25263d7072f8Seschrock { 25273d7072f8Seschrock zfs_cmd_t zc = { 0 }; 25283d7072f8Seschrock char msg[1024]; 25293d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25303d7072f8Seschrock 25313d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 25323d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 25333d7072f8Seschrock 25343d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25353d7072f8Seschrock zc.zc_guid = guid; 25363d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 2537069f55e2SEric Schrock zc.zc_obj = aux; 25383d7072f8Seschrock 2539cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 25403d7072f8Seschrock return (0); 25413d7072f8Seschrock 25423d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 254399653d4eSeschrock } 254499653d4eSeschrock 254599653d4eSeschrock /* 254699653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 254799653d4eSeschrock * a hot spare. 254899653d4eSeschrock */ 254999653d4eSeschrock static boolean_t 255099653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 255199653d4eSeschrock { 255299653d4eSeschrock nvlist_t **child; 255399653d4eSeschrock uint_t c, children; 255499653d4eSeschrock char *type; 255599653d4eSeschrock 255699653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 255799653d4eSeschrock &children) == 0) { 255899653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 255999653d4eSeschrock &type) == 0); 256099653d4eSeschrock 256199653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 256299653d4eSeschrock children == 2 && child[which] == tgt) 256399653d4eSeschrock return (B_TRUE); 256499653d4eSeschrock 256599653d4eSeschrock for (c = 0; c < children; c++) 256699653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 256799653d4eSeschrock return (B_TRUE); 256899653d4eSeschrock } 256999653d4eSeschrock 257099653d4eSeschrock return (B_FALSE); 2571fa9e4066Sahrens } 2572fa9e4066Sahrens 2573fa9e4066Sahrens /* 2574fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 25758654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2576fa9e4066Sahrens */ 2577fa9e4066Sahrens int 2578fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2579fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2580fa9e4066Sahrens { 2581fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2582fa9e4066Sahrens char msg[1024]; 2583fa9e4066Sahrens int ret; 258499653d4eSeschrock nvlist_t *tgt; 2585ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog; 2586ee0eb9f2SEric Schrock uint64_t val; 2587cb04b873SMark J Musante char *newname; 258899653d4eSeschrock nvlist_t **child; 258999653d4eSeschrock uint_t children; 259099653d4eSeschrock nvlist_t *config_root; 259199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25924263d13fSGeorge Wilson boolean_t rootpool = zpool_is_bootable(zhp); 2593fa9e4066Sahrens 2594ea8dc4b6Seschrock if (replacing) 2595ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2596ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 2597ea8dc4b6Seschrock else 2598ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2599ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 2600ea8dc4b6Seschrock 2601fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2602ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 2603ee0eb9f2SEric Schrock &islog)) == 0) 260499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 260599653d4eSeschrock 2606a43d325bSek if (avail_spare) 260799653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 260899653d4eSeschrock 2609fa94a07fSbrendan if (l2cache) 2610fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2611fa94a07fSbrendan 261299653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2613fa9e4066Sahrens zc.zc_cookie = replacing; 2614fa9e4066Sahrens 261599653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 261699653d4eSeschrock &child, &children) != 0 || children != 1) { 261799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 261899653d4eSeschrock "new device must be a single disk")); 261999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 262099653d4eSeschrock } 262199653d4eSeschrock 262299653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 262399653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 262499653d4eSeschrock 262588ecc943SGeorge Wilson if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 26260430f8daSeschrock return (-1); 26270430f8daSeschrock 262899653d4eSeschrock /* 262999653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 263099653d4eSeschrock * replace it with another hot spare. 263199653d4eSeschrock */ 263299653d4eSeschrock if (replacing && 263399653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2634ee0eb9f2SEric Schrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2635ee0eb9f2SEric Schrock NULL) == NULL || !avail_spare) && 2636ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) { 263799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 263899653d4eSeschrock "can only be replaced by another hot spare")); 26390430f8daSeschrock free(newname); 264099653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 264199653d4eSeschrock } 264299653d4eSeschrock 26430430f8daSeschrock free(newname); 26440430f8daSeschrock 2645990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 264699653d4eSeschrock return (-1); 2647fa9e4066Sahrens 2648cb04b873SMark J Musante ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2649fa9e4066Sahrens 2650e9dbad6fSeschrock zcmd_free_nvlists(&zc); 2651fa9e4066Sahrens 2652b5b76fecSGeorge Wilson if (ret == 0) { 2653b5b76fecSGeorge Wilson if (rootpool) { 265421ecdf64SLin Ling /* 265521ecdf64SLin Ling * XXX need a better way to prevent user from 265621ecdf64SLin Ling * booting up a half-baked vdev. 265721ecdf64SLin Ling */ 265821ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 265921ecdf64SLin Ling "sure to wait until resilver is done " 266021ecdf64SLin Ling "before rebooting.\n")); 2661b5b76fecSGeorge Wilson } 2662fa9e4066Sahrens return (0); 2663b5b76fecSGeorge Wilson } 2664fa9e4066Sahrens 2665fa9e4066Sahrens switch (errno) { 2666ea8dc4b6Seschrock case ENOTSUP: 2667fa9e4066Sahrens /* 2668fa9e4066Sahrens * Can't attach to or replace this type of vdev. 2669fa9e4066Sahrens */ 26708654d025Sperrin if (replacing) { 2671cb04b873SMark J Musante uint64_t version = zpool_get_prop_int(zhp, 2672cb04b873SMark J Musante ZPOOL_PROP_VERSION, NULL); 2673cb04b873SMark J Musante 2674ee0eb9f2SEric Schrock if (islog) 26758654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26768654d025Sperrin "cannot replace a log with a spare")); 2677cb04b873SMark J Musante else if (version >= SPA_VERSION_MULTI_REPLACE) 2678cb04b873SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2679cb04b873SMark J Musante "already in replacing/spare config; wait " 2680cb04b873SMark J Musante "for completion or use 'zpool detach'")); 26818654d025Sperrin else 26828654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26838654d025Sperrin "cannot replace a replacing device")); 26848654d025Sperrin } else { 268599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 268699653d4eSeschrock "can only attach to mirrors and top-level " 268799653d4eSeschrock "disks")); 26888654d025Sperrin } 268999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2690fa9e4066Sahrens break; 2691fa9e4066Sahrens 2692ea8dc4b6Seschrock case EINVAL: 2693fa9e4066Sahrens /* 2694fa9e4066Sahrens * The new device must be a single disk. 2695fa9e4066Sahrens */ 269699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 269799653d4eSeschrock "new device must be a single disk")); 269899653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2699fa9e4066Sahrens break; 2700fa9e4066Sahrens 2701ea8dc4b6Seschrock case EBUSY: 270299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 270399653d4eSeschrock new_disk); 270499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2705fa9e4066Sahrens break; 2706fa9e4066Sahrens 2707ea8dc4b6Seschrock case EOVERFLOW: 2708fa9e4066Sahrens /* 2709fa9e4066Sahrens * The new device is too small. 2710fa9e4066Sahrens */ 271199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 271299653d4eSeschrock "device is too small")); 271399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2714fa9e4066Sahrens break; 2715fa9e4066Sahrens 2716ea8dc4b6Seschrock case EDOM: 2717fa9e4066Sahrens /* 2718fa9e4066Sahrens * The new device has a different alignment requirement. 2719fa9e4066Sahrens */ 272099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 272199653d4eSeschrock "devices have different sector alignment")); 272299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2723fa9e4066Sahrens break; 2724fa9e4066Sahrens 2725ea8dc4b6Seschrock case ENAMETOOLONG: 2726fa9e4066Sahrens /* 2727fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 2728fa9e4066Sahrens */ 272999653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2730fa9e4066Sahrens break; 2731fa9e4066Sahrens 2732ea8dc4b6Seschrock default: 273399653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2734fa9e4066Sahrens } 2735fa9e4066Sahrens 273699653d4eSeschrock return (-1); 2737fa9e4066Sahrens } 2738fa9e4066Sahrens 2739fa9e4066Sahrens /* 2740fa9e4066Sahrens * Detach the specified device. 2741fa9e4066Sahrens */ 2742fa9e4066Sahrens int 2743fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2744fa9e4066Sahrens { 2745fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2746fa9e4066Sahrens char msg[1024]; 274799653d4eSeschrock nvlist_t *tgt; 2748fa94a07fSbrendan boolean_t avail_spare, l2cache; 274999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2750fa9e4066Sahrens 2751ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2752ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2753ea8dc4b6Seschrock 2754fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2755ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2756ee0eb9f2SEric Schrock NULL)) == 0) 275799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2758fa9e4066Sahrens 2759a43d325bSek if (avail_spare) 276099653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 276199653d4eSeschrock 2762fa94a07fSbrendan if (l2cache) 2763fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2764fa94a07fSbrendan 276599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 276699653d4eSeschrock 2767ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2768fa9e4066Sahrens return (0); 2769fa9e4066Sahrens 2770fa9e4066Sahrens switch (errno) { 2771fa9e4066Sahrens 2772ea8dc4b6Seschrock case ENOTSUP: 2773fa9e4066Sahrens /* 2774fa9e4066Sahrens * Can't detach from this type of vdev. 2775fa9e4066Sahrens */ 277699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 277799653d4eSeschrock "applicable to mirror and replacing vdevs")); 2778cb04b873SMark J Musante (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2779fa9e4066Sahrens break; 2780fa9e4066Sahrens 2781ea8dc4b6Seschrock case EBUSY: 2782fa9e4066Sahrens /* 2783fa9e4066Sahrens * There are no other replicas of this device. 2784fa9e4066Sahrens */ 278599653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2786fa9e4066Sahrens break; 2787fa9e4066Sahrens 2788ea8dc4b6Seschrock default: 278999653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2790ea8dc4b6Seschrock } 2791ea8dc4b6Seschrock 279299653d4eSeschrock return (-1); 279399653d4eSeschrock } 279499653d4eSeschrock 27951195e687SMark J Musante /* 27961195e687SMark J Musante * Find a mirror vdev in the source nvlist. 27971195e687SMark J Musante * 27981195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors 27991195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the 28001195e687SMark J Musante * user specified on the command line. We loop over the mchild array to 28011195e687SMark J Musante * see if any entry in the schild array matches. 28021195e687SMark J Musante * 28031195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return 28041195e687SMark J Musante * the index of that entry. Otherwise we return -1. 28051195e687SMark J Musante */ 28061195e687SMark J Musante static int 28071195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 28081195e687SMark J Musante nvlist_t **schild, uint_t schildren) 28091195e687SMark J Musante { 28101195e687SMark J Musante uint_t mc; 28111195e687SMark J Musante 28121195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) { 28131195e687SMark J Musante uint_t sc; 28141195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 28151195e687SMark J Musante mchild[mc], B_FALSE); 28161195e687SMark J Musante 28171195e687SMark J Musante for (sc = 0; sc < schildren; sc++) { 28181195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 28191195e687SMark J Musante schild[sc], B_FALSE); 28201195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0); 28211195e687SMark J Musante 28221195e687SMark J Musante free(spath); 28231195e687SMark J Musante if (result) { 28241195e687SMark J Musante free(mpath); 28251195e687SMark J Musante return (mc); 28261195e687SMark J Musante } 28271195e687SMark J Musante } 28281195e687SMark J Musante 28291195e687SMark J Musante free(mpath); 28301195e687SMark J Musante } 28311195e687SMark J Musante 28321195e687SMark J Musante return (-1); 28331195e687SMark J Musante } 28341195e687SMark J Musante 28351195e687SMark J Musante /* 28361195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist 28371195e687SMark J Musante * is generated and it is the responsibility of the caller to free it. 28381195e687SMark J Musante */ 28391195e687SMark J Musante int 28401195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 28411195e687SMark J Musante nvlist_t *props, splitflags_t flags) 28421195e687SMark J Musante { 28431195e687SMark J Musante zfs_cmd_t zc = { 0 }; 28441195e687SMark J Musante char msg[1024]; 28451195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 28461195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL; 28471195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 28481195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl; 28491195e687SMark J Musante uint64_t vers; 28501195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE; 28511195e687SMark J Musante int retval = 0; 28521195e687SMark J Musante 28531195e687SMark J Musante (void) snprintf(msg, sizeof (msg), 28541195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 28551195e687SMark J Musante 28561195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname)) 28571195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 28581195e687SMark J Musante 28591195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) { 28601195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to " 28611195e687SMark J Musante "retrieve pool configuration\n")); 28621195e687SMark J Musante return (-1); 28631195e687SMark J Musante } 28641195e687SMark J Musante 28651195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 28661195e687SMark J Musante == 0); 28671195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 28681195e687SMark J Musante 28691195e687SMark J Musante if (props) { 2870f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 28711195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 2872f9af39baSGeorge Wilson props, vers, flags, msg)) == NULL) 28731195e687SMark J Musante return (-1); 28741195e687SMark J Musante } 28751195e687SMark J Musante 28761195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 28771195e687SMark J Musante &children) != 0) { 28781195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28791195e687SMark J Musante "Source pool is missing vdev tree")); 2880aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 28811195e687SMark J Musante return (-1); 28821195e687SMark J Musante } 28831195e687SMark J Musante 28841195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 28851195e687SMark J Musante vcount = 0; 28861195e687SMark J Musante 28871195e687SMark J Musante if (*newroot == NULL || 28881195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 28891195e687SMark J Musante &newchild, &newchildren) != 0) 28901195e687SMark J Musante newchildren = 0; 28911195e687SMark J Musante 28921195e687SMark J Musante for (c = 0; c < children; c++) { 28931195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE; 28941195e687SMark J Musante char *type; 28951195e687SMark J Musante nvlist_t **mchild, *vdev; 28961195e687SMark J Musante uint_t mchildren; 28971195e687SMark J Musante int entry; 28981195e687SMark J Musante 28991195e687SMark J Musante /* 29001195e687SMark J Musante * Unlike cache & spares, slogs are stored in the 29011195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 29021195e687SMark J Musante */ 29031195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 29041195e687SMark J Musante &is_log); 29051195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 29061195e687SMark J Musante &is_hole); 29071195e687SMark J Musante if (is_log || is_hole) { 29081195e687SMark J Musante /* 29091195e687SMark J Musante * Create a hole vdev and put it in the config. 29101195e687SMark J Musante */ 29111195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 29121195e687SMark J Musante goto out; 29131195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 29141195e687SMark J Musante VDEV_TYPE_HOLE) != 0) 29151195e687SMark J Musante goto out; 29161195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 29171195e687SMark J Musante 1) != 0) 29181195e687SMark J Musante goto out; 29191195e687SMark J Musante if (lastlog == 0) 29201195e687SMark J Musante lastlog = vcount; 29211195e687SMark J Musante varray[vcount++] = vdev; 29221195e687SMark J Musante continue; 29231195e687SMark J Musante } 29241195e687SMark J Musante lastlog = 0; 29251195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 29261195e687SMark J Musante == 0); 29271195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 29281195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29291195e687SMark J Musante "Source pool must be composed only of mirrors\n")); 29301195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 29311195e687SMark J Musante goto out; 29321195e687SMark J Musante } 29331195e687SMark J Musante 29341195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c], 29351195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 29361195e687SMark J Musante 29371195e687SMark J Musante /* find or add an entry for this top-level vdev */ 29381195e687SMark J Musante if (newchildren > 0 && 29391195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren, 29401195e687SMark J Musante newchild, newchildren)) >= 0) { 29411195e687SMark J Musante /* We found a disk that the user specified. */ 29421195e687SMark J Musante vdev = mchild[entry]; 29431195e687SMark J Musante ++found; 29441195e687SMark J Musante } else { 29451195e687SMark J Musante /* User didn't specify a disk for this vdev. */ 29461195e687SMark J Musante vdev = mchild[mchildren - 1]; 29471195e687SMark J Musante } 29481195e687SMark J Musante 29491195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 29501195e687SMark J Musante goto out; 29511195e687SMark J Musante } 29521195e687SMark J Musante 29531195e687SMark J Musante /* did we find every disk the user specified? */ 29541195e687SMark J Musante if (found != newchildren) { 29551195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 29561195e687SMark J Musante "include at most one disk from each mirror")); 29571195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 29581195e687SMark J Musante goto out; 29591195e687SMark J Musante } 29601195e687SMark J Musante 29611195e687SMark J Musante /* Prepare the nvlist for populating. */ 29621195e687SMark J Musante if (*newroot == NULL) { 29631195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 29641195e687SMark J Musante goto out; 29651195e687SMark J Musante freelist = B_TRUE; 29661195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 29671195e687SMark J Musante VDEV_TYPE_ROOT) != 0) 29681195e687SMark J Musante goto out; 29691195e687SMark J Musante } else { 29701195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 29711195e687SMark J Musante } 29721195e687SMark J Musante 29731195e687SMark J Musante /* Add all the children we found */ 29741195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 29751195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0) 29761195e687SMark J Musante goto out; 29771195e687SMark J Musante 29781195e687SMark J Musante /* 29791195e687SMark J Musante * If we're just doing a dry run, exit now with success. 29801195e687SMark J Musante */ 29811195e687SMark J Musante if (flags.dryrun) { 29821195e687SMark J Musante memory_err = B_FALSE; 29831195e687SMark J Musante freelist = B_FALSE; 29841195e687SMark J Musante goto out; 29851195e687SMark J Musante } 29861195e687SMark J Musante 29871195e687SMark J Musante /* now build up the config list & call the ioctl */ 29881195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 29891195e687SMark J Musante goto out; 29901195e687SMark J Musante 29911195e687SMark J Musante if (nvlist_add_nvlist(newconfig, 29921195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 29931195e687SMark J Musante nvlist_add_string(newconfig, 29941195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 29951195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 29961195e687SMark J Musante goto out; 29971195e687SMark J Musante 29981195e687SMark J Musante /* 29991195e687SMark J Musante * The new pool is automatically part of the namespace unless we 30001195e687SMark J Musante * explicitly export it. 30011195e687SMark J Musante */ 30021195e687SMark J Musante if (!flags.import) 30031195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 30041195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 30051195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 30061195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 30071195e687SMark J Musante goto out; 30081195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 30091195e687SMark J Musante goto out; 30101195e687SMark J Musante 30111195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 30121195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg); 30131195e687SMark J Musante goto out; 30141195e687SMark J Musante } 30151195e687SMark J Musante 30161195e687SMark J Musante freelist = B_FALSE; 30171195e687SMark J Musante memory_err = B_FALSE; 30181195e687SMark J Musante 30191195e687SMark J Musante out: 30201195e687SMark J Musante if (varray != NULL) { 30211195e687SMark J Musante int v; 30221195e687SMark J Musante 30231195e687SMark J Musante for (v = 0; v < vcount; v++) 30241195e687SMark J Musante nvlist_free(varray[v]); 30251195e687SMark J Musante free(varray); 30261195e687SMark J Musante } 30271195e687SMark J Musante zcmd_free_nvlists(&zc); 3028aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 3029aab83bb8SJosef 'Jeff' Sipek nvlist_free(newconfig); 30301195e687SMark J Musante if (freelist) { 30311195e687SMark J Musante nvlist_free(*newroot); 30321195e687SMark J Musante *newroot = NULL; 30331195e687SMark J Musante } 30341195e687SMark J Musante 30351195e687SMark J Musante if (retval != 0) 30361195e687SMark J Musante return (retval); 30371195e687SMark J Musante 30381195e687SMark J Musante if (memory_err) 30391195e687SMark J Musante return (no_memory(hdl)); 30401195e687SMark J Musante 30411195e687SMark J Musante return (0); 30421195e687SMark J Musante } 30431195e687SMark J Musante 304499653d4eSeschrock /* 3045fa94a07fSbrendan * Remove the given device. Currently, this is supported only for hot spares 3046fa94a07fSbrendan * and level 2 cache devices. 304799653d4eSeschrock */ 304899653d4eSeschrock int 304999653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 305099653d4eSeschrock { 305199653d4eSeschrock zfs_cmd_t zc = { 0 }; 305299653d4eSeschrock char msg[1024]; 305399653d4eSeschrock nvlist_t *tgt; 305488ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog; 305599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 305688ecc943SGeorge Wilson uint64_t version; 305799653d4eSeschrock 305899653d4eSeschrock (void) snprintf(msg, sizeof (msg), 305999653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 306099653d4eSeschrock 306199653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3062ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 306388ecc943SGeorge Wilson &islog)) == 0) 306499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 306588ecc943SGeorge Wilson /* 306688ecc943SGeorge Wilson * XXX - this should just go away. 306788ecc943SGeorge Wilson */ 306888ecc943SGeorge Wilson if (!avail_spare && !l2cache && !islog) { 306999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 307088ecc943SGeorge Wilson "only inactive hot spares, cache, top-level, " 307188ecc943SGeorge Wilson "or log devices can be removed")); 307299653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 307399653d4eSeschrock } 307499653d4eSeschrock 307588ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 307688ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) { 307788ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 307888ecc943SGeorge Wilson "pool must be upgrade to support log removal")); 307988ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg)); 308088ecc943SGeorge Wilson } 308188ecc943SGeorge Wilson 308299653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 308399653d4eSeschrock 3084ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 308599653d4eSeschrock return (0); 308699653d4eSeschrock 308799653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3088ea8dc4b6Seschrock } 3089ea8dc4b6Seschrock 3090ea8dc4b6Seschrock /* 3091ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 3092ea8dc4b6Seschrock */ 3093ea8dc4b6Seschrock int 3094468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 3095ea8dc4b6Seschrock { 3096ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3097ea8dc4b6Seschrock char msg[1024]; 309899653d4eSeschrock nvlist_t *tgt; 3099468c413aSTim Haley zpool_rewind_policy_t policy; 3100fa94a07fSbrendan boolean_t avail_spare, l2cache; 310199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3102468c413aSTim Haley nvlist_t *nvi = NULL; 31034b964adaSGeorge Wilson int error; 3104ea8dc4b6Seschrock 3105ea8dc4b6Seschrock if (path) 3106ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3107ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3108e9dbad6fSeschrock path); 3109ea8dc4b6Seschrock else 3110ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3111ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3112ea8dc4b6Seschrock zhp->zpool_name); 3113ea8dc4b6Seschrock 3114ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 311599653d4eSeschrock if (path) { 3116fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 3117ee0eb9f2SEric Schrock &l2cache, NULL)) == 0) 311899653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3119ea8dc4b6Seschrock 3120fa94a07fSbrendan /* 3121fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 3122fa94a07fSbrendan * error clearing for l2cache devices. 3123fa94a07fSbrendan */ 3124a43d325bSek if (avail_spare) 312599653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3126ea8dc4b6Seschrock 312799653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 312899653d4eSeschrock &zc.zc_guid) == 0); 3129fa9e4066Sahrens } 3130fa9e4066Sahrens 3131468c413aSTim Haley zpool_get_rewind_policy(rewindnvl, &policy); 3132468c413aSTim Haley zc.zc_cookie = policy.zrp_request; 3133468c413aSTim Haley 313457f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 3135468c413aSTim Haley return (-1); 3136468c413aSTim Haley 3137cb04b873SMark J Musante if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 3138468c413aSTim Haley return (-1); 3139468c413aSTim Haley 31404b964adaSGeorge Wilson while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 31414b964adaSGeorge Wilson errno == ENOMEM) { 31424b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 31434b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 31444b964adaSGeorge Wilson return (-1); 31454b964adaSGeorge Wilson } 31464b964adaSGeorge Wilson } 31474b964adaSGeorge Wilson 31484b964adaSGeorge Wilson if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 3149468c413aSTim Haley errno != EPERM && errno != EACCES)) { 3150468c413aSTim Haley if (policy.zrp_request & 3151468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 3152468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 3153468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name, 3154468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 3155468c413aSTim Haley nvi); 3156468c413aSTim Haley nvlist_free(nvi); 3157468c413aSTim Haley } 3158468c413aSTim Haley zcmd_free_nvlists(&zc); 315999653d4eSeschrock return (0); 3160468c413aSTim Haley } 316199653d4eSeschrock 3162468c413aSTim Haley zcmd_free_nvlists(&zc); 316399653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3164fa9e4066Sahrens } 3165fa9e4066Sahrens 31663d7072f8Seschrock /* 31673d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 31683d7072f8Seschrock */ 31693d7072f8Seschrock int 31703d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 31713d7072f8Seschrock { 31723d7072f8Seschrock zfs_cmd_t zc = { 0 }; 31733d7072f8Seschrock char msg[1024]; 31743d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 31753d7072f8Seschrock 31763d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 31773d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 31783d7072f8Seschrock guid); 31793d7072f8Seschrock 31803d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 31813d7072f8Seschrock zc.zc_guid = guid; 318214f8ce41SVictor Latushkin zc.zc_cookie = ZPOOL_NO_REWIND; 31833d7072f8Seschrock 31843d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 31853d7072f8Seschrock return (0); 31863d7072f8Seschrock 31873d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 31883d7072f8Seschrock } 31893d7072f8Seschrock 3190e9103aaeSGarrett D'Amore /* 3191e9103aaeSGarrett D'Amore * Change the GUID for a pool. 3192e9103aaeSGarrett D'Amore */ 3193e9103aaeSGarrett D'Amore int 3194e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp) 3195e9103aaeSGarrett D'Amore { 3196e9103aaeSGarrett D'Amore char msg[1024]; 3197e9103aaeSGarrett D'Amore libzfs_handle_t *hdl = zhp->zpool_hdl; 3198e9103aaeSGarrett D'Amore zfs_cmd_t zc = { 0 }; 3199e9103aaeSGarrett D'Amore 3200e9103aaeSGarrett D'Amore (void) snprintf(msg, sizeof (msg), 3201e9103aaeSGarrett D'Amore dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 3202e9103aaeSGarrett D'Amore 3203e9103aaeSGarrett D'Amore (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3204e9103aaeSGarrett D'Amore if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 3205e9103aaeSGarrett D'Amore return (0); 3206e9103aaeSGarrett D'Amore 3207e9103aaeSGarrett D'Amore return (zpool_standard_error(hdl, errno, msg)); 3208e9103aaeSGarrett D'Amore } 3209e9103aaeSGarrett D'Amore 32104263d13fSGeorge Wilson /* 32114263d13fSGeorge Wilson * Reopen the pool. 32124263d13fSGeorge Wilson */ 32134263d13fSGeorge Wilson int 32144263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp) 32154263d13fSGeorge Wilson { 32164263d13fSGeorge Wilson zfs_cmd_t zc = { 0 }; 32174263d13fSGeorge Wilson char msg[1024]; 32184263d13fSGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl; 32194263d13fSGeorge Wilson 32204263d13fSGeorge Wilson (void) snprintf(msg, sizeof (msg), 32214263d13fSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), 32224263d13fSGeorge Wilson zhp->zpool_name); 32234263d13fSGeorge Wilson 32244263d13fSGeorge Wilson (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 32254263d13fSGeorge Wilson if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0) 32264263d13fSGeorge Wilson return (0); 32274263d13fSGeorge Wilson return (zpool_standard_error(hdl, errno, msg)); 32284263d13fSGeorge Wilson } 32294263d13fSGeorge Wilson 3230c67d9675Seschrock /* 3231c67d9675Seschrock * Convert from a devid string to a path. 3232c67d9675Seschrock */ 3233c67d9675Seschrock static char * 3234c67d9675Seschrock devid_to_path(char *devid_str) 3235c67d9675Seschrock { 3236c67d9675Seschrock ddi_devid_t devid; 3237c67d9675Seschrock char *minor; 3238c67d9675Seschrock char *path; 3239c67d9675Seschrock devid_nmlist_t *list = NULL; 3240c67d9675Seschrock int ret; 3241c67d9675Seschrock 3242c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 3243c67d9675Seschrock return (NULL); 3244c67d9675Seschrock 3245c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 3246c67d9675Seschrock 3247c67d9675Seschrock devid_str_free(minor); 3248c67d9675Seschrock devid_free(devid); 3249c67d9675Seschrock 3250c67d9675Seschrock if (ret != 0) 3251c67d9675Seschrock return (NULL); 3252c67d9675Seschrock 3253078266a5SMarcel Telka /* 3254078266a5SMarcel Telka * In a case the strdup() fails, we will just return NULL below. 3255078266a5SMarcel Telka */ 3256078266a5SMarcel Telka path = strdup(list[0].devname); 325799653d4eSeschrock 3258c67d9675Seschrock devid_free_nmlist(list); 3259c67d9675Seschrock 3260c67d9675Seschrock return (path); 3261c67d9675Seschrock } 3262c67d9675Seschrock 3263c67d9675Seschrock /* 3264c67d9675Seschrock * Convert from a path to a devid string. 3265c67d9675Seschrock */ 3266c67d9675Seschrock static char * 3267c67d9675Seschrock path_to_devid(const char *path) 3268c67d9675Seschrock { 3269c67d9675Seschrock int fd; 3270c67d9675Seschrock ddi_devid_t devid; 3271c67d9675Seschrock char *minor, *ret; 3272c67d9675Seschrock 3273c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 3274c67d9675Seschrock return (NULL); 3275c67d9675Seschrock 3276c67d9675Seschrock minor = NULL; 3277c67d9675Seschrock ret = NULL; 3278c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 3279c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 3280c67d9675Seschrock ret = devid_str_encode(devid, minor); 3281c67d9675Seschrock if (minor != NULL) 3282c67d9675Seschrock devid_str_free(minor); 3283c67d9675Seschrock devid_free(devid); 3284c67d9675Seschrock } 3285c67d9675Seschrock (void) close(fd); 3286c67d9675Seschrock 3287c67d9675Seschrock return (ret); 3288c67d9675Seschrock } 3289c67d9675Seschrock 3290c67d9675Seschrock /* 3291c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 3292c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 3293c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 3294c67d9675Seschrock */ 3295c67d9675Seschrock static void 3296c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3297c67d9675Seschrock { 3298c67d9675Seschrock zfs_cmd_t zc = { 0 }; 3299c67d9675Seschrock 3300c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3301e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3302c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3303ea8dc4b6Seschrock &zc.zc_guid) == 0); 3304c67d9675Seschrock 330599653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3306c67d9675Seschrock } 3307c67d9675Seschrock 3308c67d9675Seschrock /* 3309c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 3310c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3311c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 3312c67d9675Seschrock * trailing 's0' slice name. 3313c67d9675Seschrock * 3314c67d9675Seschrock * This routine is also responsible for identifying when disks have been 3315c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 3316c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 3317c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 3318c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 3319c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 3320c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3321c67d9675Seschrock * of these checks. 3322c67d9675Seschrock */ 3323c67d9675Seschrock char * 332488ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 332588ecc943SGeorge Wilson boolean_t verbose) 3326c67d9675Seschrock { 3327c67d9675Seschrock char *path, *devid; 3328ea8dc4b6Seschrock uint64_t value; 3329ea8dc4b6Seschrock char buf[64]; 33303d7072f8Seschrock vdev_stat_t *vs; 33313d7072f8Seschrock uint_t vsc; 3332c67d9675Seschrock 3333ea8dc4b6Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3334ea8dc4b6Seschrock &value) == 0) { 3335ea8dc4b6Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3336ea8dc4b6Seschrock &value) == 0); 33375ad82045Snd (void) snprintf(buf, sizeof (buf), "%llu", 33385ad82045Snd (u_longlong_t)value); 3339ea8dc4b6Seschrock path = buf; 3340ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3341c67d9675Seschrock 33423d7072f8Seschrock /* 33433d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 33443d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 33453d7072f8Seschrock * open a misbehaving device, which can have undesirable 33463d7072f8Seschrock * effects. 33473d7072f8Seschrock */ 33483f9d6ad7SLin Ling if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 33493d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 33503d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 33513d7072f8Seschrock zhp != NULL && 3352c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3353c67d9675Seschrock /* 3354c67d9675Seschrock * Determine if the current path is correct. 3355c67d9675Seschrock */ 3356c67d9675Seschrock char *newdevid = path_to_devid(path); 3357c67d9675Seschrock 3358c67d9675Seschrock if (newdevid == NULL || 3359c67d9675Seschrock strcmp(devid, newdevid) != 0) { 3360c67d9675Seschrock char *newpath; 3361c67d9675Seschrock 3362c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 3363c67d9675Seschrock /* 3364c67d9675Seschrock * Update the path appropriately. 3365c67d9675Seschrock */ 3366c67d9675Seschrock set_path(zhp, nv, newpath); 336799653d4eSeschrock if (nvlist_add_string(nv, 336899653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 336999653d4eSeschrock verify(nvlist_lookup_string(nv, 337099653d4eSeschrock ZPOOL_CONFIG_PATH, 337199653d4eSeschrock &path) == 0); 3372c67d9675Seschrock free(newpath); 3373c67d9675Seschrock } 3374c67d9675Seschrock } 3375c67d9675Seschrock 337699653d4eSeschrock if (newdevid) 337799653d4eSeschrock devid_str_free(newdevid); 3378c67d9675Seschrock } 3379c67d9675Seschrock 3380*6401734dSWill Andrews if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) 3381*6401734dSWill Andrews path += strlen(ZFS_DISK_ROOTD); 3382c67d9675Seschrock 3383c67d9675Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3384ea8dc4b6Seschrock &value) == 0 && value) { 33853fdda499SJohn Harres int pathlen = strlen(path); 338699653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 33873fdda499SJohn Harres 33883fdda499SJohn Harres /* 33893fdda499SJohn Harres * If it starts with c#, and ends with "s0", chop 33903fdda499SJohn Harres * the "s0" off, or if it ends with "s0/old", remove 33913fdda499SJohn Harres * the "s0" from the middle. 33923fdda499SJohn Harres */ 33933fdda499SJohn Harres if (CTD_CHECK(tmp)) { 33943fdda499SJohn Harres if (strcmp(&tmp[pathlen - 2], "s0") == 0) { 33953fdda499SJohn Harres tmp[pathlen - 2] = '\0'; 33963fdda499SJohn Harres } else if (pathlen > 6 && 33973fdda499SJohn Harres strcmp(&tmp[pathlen - 6], "s0/old") == 0) { 33983fdda499SJohn Harres (void) strcpy(&tmp[pathlen - 6], 33993fdda499SJohn Harres "/old"); 34003fdda499SJohn Harres } 34013fdda499SJohn Harres } 3402c67d9675Seschrock return (tmp); 3403c67d9675Seschrock } 3404c67d9675Seschrock } else { 3405c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 340699653d4eSeschrock 340799653d4eSeschrock /* 340899653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 340999653d4eSeschrock */ 341099653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 341199653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 341299653d4eSeschrock &value) == 0); 341399653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 34145ad82045Snd (u_longlong_t)value); 341599653d4eSeschrock path = buf; 341699653d4eSeschrock } 341788ecc943SGeorge Wilson 341888ecc943SGeorge Wilson /* 341988ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id> 342088ecc943SGeorge Wilson * naming convention. 342188ecc943SGeorge Wilson */ 342288ecc943SGeorge Wilson if (verbose) { 342388ecc943SGeorge Wilson uint64_t id; 342488ecc943SGeorge Wilson 342588ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 342688ecc943SGeorge Wilson &id) == 0); 342788ecc943SGeorge Wilson (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 342888ecc943SGeorge Wilson (u_longlong_t)id); 342988ecc943SGeorge Wilson path = buf; 343088ecc943SGeorge Wilson } 3431c67d9675Seschrock } 3432c67d9675Seschrock 343399653d4eSeschrock return (zfs_strdup(hdl, path)); 3434c67d9675Seschrock } 3435ea8dc4b6Seschrock 3436ea8dc4b6Seschrock static int 3437a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b) 3438ea8dc4b6Seschrock { 34397802d7bfSMatthew Ahrens return (memcmp(a, b, sizeof (zbookmark_phys_t))); 3440ea8dc4b6Seschrock } 3441ea8dc4b6Seschrock 3442ea8dc4b6Seschrock /* 3443ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 3444ea8dc4b6Seschrock * caller. 3445ea8dc4b6Seschrock */ 3446ea8dc4b6Seschrock int 344755434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3448ea8dc4b6Seschrock { 3449ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3450ea8dc4b6Seschrock uint64_t count; 34517802d7bfSMatthew Ahrens zbookmark_phys_t *zb = NULL; 345255434c77Sek int i; 3453ea8dc4b6Seschrock 3454ea8dc4b6Seschrock /* 3455ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 3456ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 3457ea8dc4b6Seschrock * entire list. 3458ea8dc4b6Seschrock */ 3459ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3460ea8dc4b6Seschrock &count) == 0); 346175519f38Sek if (count == 0) 346275519f38Sek return (0); 3463e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 34647802d7bfSMatthew Ahrens count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL) 346599653d4eSeschrock return (-1); 3466e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 3467ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3468ea8dc4b6Seschrock for (;;) { 346999653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 347099653d4eSeschrock &zc) != 0) { 3471e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 3472ea8dc4b6Seschrock if (errno == ENOMEM) { 34737802d7bfSMatthew Ahrens void *dst; 34747802d7bfSMatthew Ahrens 3475bf561db0Svb count = zc.zc_nvlist_dst_size; 34767802d7bfSMatthew Ahrens dst = zfs_alloc(zhp->zpool_hdl, count * 34777802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 34787802d7bfSMatthew Ahrens if (dst == NULL) 347999653d4eSeschrock return (-1); 34807802d7bfSMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)dst; 3481ea8dc4b6Seschrock } else { 3482ea8dc4b6Seschrock return (-1); 3483ea8dc4b6Seschrock } 3484ea8dc4b6Seschrock } else { 3485ea8dc4b6Seschrock break; 3486ea8dc4b6Seschrock } 3487ea8dc4b6Seschrock } 3488ea8dc4b6Seschrock 3489ea8dc4b6Seschrock /* 3490ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 3491ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3492e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3493ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 3494ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 3495ea8dc4b6Seschrock */ 34967802d7bfSMatthew Ahrens zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) + 3497e9dbad6fSeschrock zc.zc_nvlist_dst_size; 3498e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 3499ea8dc4b6Seschrock 3500a2cdcdd2SPaul Dagnelie qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare); 3501ea8dc4b6Seschrock 350255434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3503ea8dc4b6Seschrock 3504ea8dc4b6Seschrock /* 350555434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3506ea8dc4b6Seschrock */ 3507ea8dc4b6Seschrock for (i = 0; i < count; i++) { 3508ea8dc4b6Seschrock nvlist_t *nv; 3509ea8dc4b6Seschrock 3510c0a81264Sek /* ignoring zb_blkid and zb_level for now */ 3511c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3512c0a81264Sek zb[i-1].zb_object == zb[i].zb_object) 3513ea8dc4b6Seschrock continue; 3514ea8dc4b6Seschrock 351555434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 351655434c77Sek goto nomem; 351755434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 351855434c77Sek zb[i].zb_objset) != 0) { 351955434c77Sek nvlist_free(nv); 352099653d4eSeschrock goto nomem; 3521ea8dc4b6Seschrock } 352255434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 352355434c77Sek zb[i].zb_object) != 0) { 352455434c77Sek nvlist_free(nv); 352555434c77Sek goto nomem; 352655434c77Sek } 352755434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 352855434c77Sek nvlist_free(nv); 352955434c77Sek goto nomem; 353055434c77Sek } 353155434c77Sek nvlist_free(nv); 3532ea8dc4b6Seschrock } 3533ea8dc4b6Seschrock 35343ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 3535ea8dc4b6Seschrock return (0); 353699653d4eSeschrock 353799653d4eSeschrock nomem: 3538e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 353999653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 3540ea8dc4b6Seschrock } 3541eaca9bbdSeschrock 3542eaca9bbdSeschrock /* 3543eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 3544eaca9bbdSeschrock */ 3545eaca9bbdSeschrock int 3546990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3547eaca9bbdSeschrock { 3548eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 354999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3550eaca9bbdSeschrock 3551eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3552990b4856Slling zc.zc_cookie = new_version; 3553990b4856Slling 3554ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3555ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 355699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 355799653d4eSeschrock zhp->zpool_name)); 3558eaca9bbdSeschrock return (0); 3559eaca9bbdSeschrock } 356006eeb2adSek 356106eeb2adSek void 35624445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len) 356306eeb2adSek { 35644445fffbSMatthew Ahrens (void) strlcpy(string, basename(argv[0]), len); 35654445fffbSMatthew Ahrens for (int i = 1; i < argc; i++) { 35664445fffbSMatthew Ahrens (void) strlcat(string, " ", len); 35674445fffbSMatthew Ahrens (void) strlcat(string, argv[i], len); 35682a6b87f0Sek } 35692a6b87f0Sek } 35702a6b87f0Sek 35712a6b87f0Sek int 35724445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message) 35732a6b87f0Sek { 35744445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 }; 35754445fffbSMatthew Ahrens nvlist_t *args; 35764445fffbSMatthew Ahrens int err; 35774445fffbSMatthew Ahrens 35784445fffbSMatthew Ahrens args = fnvlist_alloc(); 35794445fffbSMatthew Ahrens fnvlist_add_string(args, "message", message); 35804445fffbSMatthew Ahrens err = zcmd_write_src_nvlist(hdl, &zc, args); 35814445fffbSMatthew Ahrens if (err == 0) 35824445fffbSMatthew Ahrens err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc); 35834445fffbSMatthew Ahrens nvlist_free(args); 35844445fffbSMatthew Ahrens zcmd_free_nvlists(&zc); 35854445fffbSMatthew Ahrens return (err); 358606eeb2adSek } 358706eeb2adSek 358806eeb2adSek /* 358906eeb2adSek * Perform ioctl to get some command history of a pool. 359006eeb2adSek * 359106eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 359206eeb2adSek * logical offset of the history buffer to start reading from. 359306eeb2adSek * 359406eeb2adSek * Upon return, 'off' is the next logical offset to read from and 359506eeb2adSek * 'len' is the actual amount of bytes read into 'buf'. 359606eeb2adSek */ 359706eeb2adSek static int 359806eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 359906eeb2adSek { 360006eeb2adSek zfs_cmd_t zc = { 0 }; 360106eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl; 360206eeb2adSek 360306eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 360406eeb2adSek 360506eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf; 360606eeb2adSek zc.zc_history_len = *len; 360706eeb2adSek zc.zc_history_offset = *off; 360806eeb2adSek 360906eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 361006eeb2adSek switch (errno) { 361106eeb2adSek case EPERM: 3612ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 3613ece3d9b3Slling dgettext(TEXT_DOMAIN, 361406eeb2adSek "cannot show history for pool '%s'"), 361506eeb2adSek zhp->zpool_name)); 361606eeb2adSek case ENOENT: 3617ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 361806eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool " 361906eeb2adSek "'%s'"), zhp->zpool_name)); 3620d7306b64Sek case ENOTSUP: 3621d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3622d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool " 3623d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name)); 362406eeb2adSek default: 3625ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 362606eeb2adSek dgettext(TEXT_DOMAIN, 362706eeb2adSek "cannot get history for '%s'"), zhp->zpool_name)); 362806eeb2adSek } 362906eeb2adSek } 363006eeb2adSek 363106eeb2adSek *len = zc.zc_history_len; 363206eeb2adSek *off = zc.zc_history_offset; 363306eeb2adSek 363406eeb2adSek return (0); 363506eeb2adSek } 363606eeb2adSek 363706eeb2adSek /* 363806eeb2adSek * Process the buffer of nvlists, unpacking and storing each nvlist record 363906eeb2adSek * into 'records'. 'leftover' is set to the number of bytes that weren't 364006eeb2adSek * processed as there wasn't a complete record. 364106eeb2adSek */ 36428f18d1faSGeorge Wilson int 364306eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 364406eeb2adSek nvlist_t ***records, uint_t *numrecords) 364506eeb2adSek { 364606eeb2adSek uint64_t reclen; 364706eeb2adSek nvlist_t *nv; 364806eeb2adSek int i; 364906eeb2adSek 365006eeb2adSek while (bytes_read > sizeof (reclen)) { 365106eeb2adSek 365206eeb2adSek /* get length of packed record (stored as little endian) */ 365306eeb2adSek for (i = 0, reclen = 0; i < sizeof (reclen); i++) 365406eeb2adSek reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 365506eeb2adSek 365606eeb2adSek if (bytes_read < sizeof (reclen) + reclen) 365706eeb2adSek break; 365806eeb2adSek 365906eeb2adSek /* unpack record */ 366006eeb2adSek if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 366106eeb2adSek return (ENOMEM); 366206eeb2adSek bytes_read -= sizeof (reclen) + reclen; 366306eeb2adSek buf += sizeof (reclen) + reclen; 366406eeb2adSek 366506eeb2adSek /* add record to nvlist array */ 366606eeb2adSek (*numrecords)++; 366706eeb2adSek if (ISP2(*numrecords + 1)) { 366806eeb2adSek *records = realloc(*records, 366906eeb2adSek *numrecords * 2 * sizeof (nvlist_t *)); 367006eeb2adSek } 367106eeb2adSek (*records)[*numrecords - 1] = nv; 367206eeb2adSek } 367306eeb2adSek 367406eeb2adSek *leftover = bytes_read; 367506eeb2adSek return (0); 367606eeb2adSek } 367706eeb2adSek 367806eeb2adSek /* 367906eeb2adSek * Retrieve the command history of a pool. 368006eeb2adSek */ 368106eeb2adSek int 368206eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 368306eeb2adSek { 36843339867aSMatthew Ahrens char *buf; 36853339867aSMatthew Ahrens int buflen = 128 * 1024; 368606eeb2adSek uint64_t off = 0; 368706eeb2adSek nvlist_t **records = NULL; 368806eeb2adSek uint_t numrecords = 0; 368906eeb2adSek int err, i; 369006eeb2adSek 36913339867aSMatthew Ahrens buf = malloc(buflen); 36923339867aSMatthew Ahrens if (buf == NULL) 36933339867aSMatthew Ahrens return (ENOMEM); 369406eeb2adSek do { 36953339867aSMatthew Ahrens uint64_t bytes_read = buflen; 369606eeb2adSek uint64_t leftover; 369706eeb2adSek 369806eeb2adSek if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 369906eeb2adSek break; 370006eeb2adSek 370106eeb2adSek /* if nothing else was read in, we're at EOF, just return */ 370206eeb2adSek if (!bytes_read) 370306eeb2adSek break; 370406eeb2adSek 370506eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read, 370606eeb2adSek &leftover, &records, &numrecords)) != 0) 370706eeb2adSek break; 370806eeb2adSek off -= leftover; 37093339867aSMatthew Ahrens if (leftover == bytes_read) { 37103339867aSMatthew Ahrens /* 37113339867aSMatthew Ahrens * no progress made, because buffer is not big enough 37123339867aSMatthew Ahrens * to hold this record; resize and retry. 37133339867aSMatthew Ahrens */ 37143339867aSMatthew Ahrens buflen *= 2; 37153339867aSMatthew Ahrens free(buf); 37163339867aSMatthew Ahrens buf = malloc(buflen); 37173339867aSMatthew Ahrens if (buf == NULL) 37183339867aSMatthew Ahrens return (ENOMEM); 37193339867aSMatthew Ahrens } 372006eeb2adSek 372106eeb2adSek /* CONSTCOND */ 372206eeb2adSek } while (1); 372306eeb2adSek 37243339867aSMatthew Ahrens free(buf); 37253339867aSMatthew Ahrens 372606eeb2adSek if (!err) { 372706eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 372806eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 372906eeb2adSek records, numrecords) == 0); 373006eeb2adSek } 373106eeb2adSek for (i = 0; i < numrecords; i++) 373206eeb2adSek nvlist_free(records[i]); 373306eeb2adSek free(records); 373406eeb2adSek 373506eeb2adSek return (err); 373606eeb2adSek } 373755434c77Sek 373855434c77Sek void 373955434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 374055434c77Sek char *pathname, size_t len) 374155434c77Sek { 374255434c77Sek zfs_cmd_t zc = { 0 }; 374355434c77Sek boolean_t mounted = B_FALSE; 374455434c77Sek char *mntpnt = NULL; 374555434c77Sek char dsname[MAXNAMELEN]; 374655434c77Sek 374755434c77Sek if (dsobj == 0) { 374855434c77Sek /* special case for the MOS */ 374955434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 375055434c77Sek return; 375155434c77Sek } 375255434c77Sek 375355434c77Sek /* get the dataset's name */ 375455434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 375555434c77Sek zc.zc_obj = dsobj; 375655434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, 375755434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 375855434c77Sek /* just write out a path of two object numbers */ 375955434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 376055434c77Sek dsobj, obj); 376155434c77Sek return; 376255434c77Sek } 376355434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 376455434c77Sek 376555434c77Sek /* find out if the dataset is mounted */ 376655434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 376755434c77Sek 376855434c77Sek /* get the corrupted object's path */ 376955434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 377055434c77Sek zc.zc_obj = obj; 377155434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 377255434c77Sek &zc) == 0) { 377355434c77Sek if (mounted) { 377455434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt, 377555434c77Sek zc.zc_value); 377655434c77Sek } else { 377755434c77Sek (void) snprintf(pathname, len, "%s:%s", 377855434c77Sek dsname, zc.zc_value); 377955434c77Sek } 378055434c77Sek } else { 378155434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 378255434c77Sek } 378355434c77Sek free(mntpnt); 378455434c77Sek } 3785b1b8ab34Slling 378615e6edf1Sgw /* 378715e6edf1Sgw * Read the EFI label from the config, if a label does not exist then 378815e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL 378915e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI 379015e6edf1Sgw * partition. 379115e6edf1Sgw */ 379215e6edf1Sgw static int 379315e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb) 379415e6edf1Sgw { 379515e6edf1Sgw char *path; 379615e6edf1Sgw int fd; 379715e6edf1Sgw char diskname[MAXPATHLEN]; 379815e6edf1Sgw int err = -1; 379915e6edf1Sgw 380015e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 380115e6edf1Sgw return (err); 380215e6edf1Sgw 3803*6401734dSWill Andrews (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT, 380415e6edf1Sgw strrchr(path, '/')); 380515e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 380615e6edf1Sgw struct dk_gpt *vtoc; 380715e6edf1Sgw 380815e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 380915e6edf1Sgw if (sb != NULL) 381015e6edf1Sgw *sb = vtoc->efi_parts[0].p_start; 381115e6edf1Sgw efi_free(vtoc); 381215e6edf1Sgw } 381315e6edf1Sgw (void) close(fd); 381415e6edf1Sgw } 381515e6edf1Sgw return (err); 381615e6edf1Sgw } 381715e6edf1Sgw 38188488aeb5Staylor /* 38198488aeb5Staylor * determine where a partition starts on a disk in the current 38208488aeb5Staylor * configuration 38218488aeb5Staylor */ 38228488aeb5Staylor static diskaddr_t 38238488aeb5Staylor find_start_block(nvlist_t *config) 38248488aeb5Staylor { 38258488aeb5Staylor nvlist_t **child; 38268488aeb5Staylor uint_t c, children; 38278488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 38288488aeb5Staylor uint64_t wholedisk; 38298488aeb5Staylor 38308488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 38318488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 38328488aeb5Staylor if (nvlist_lookup_uint64(config, 38338488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 38348488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 38358488aeb5Staylor return (MAXOFFSET_T); 38368488aeb5Staylor } 383715e6edf1Sgw if (read_efi_label(config, &sb) < 0) 383815e6edf1Sgw sb = MAXOFFSET_T; 38398488aeb5Staylor return (sb); 38408488aeb5Staylor } 38418488aeb5Staylor 38428488aeb5Staylor for (c = 0; c < children; c++) { 38438488aeb5Staylor sb = find_start_block(child[c]); 38448488aeb5Staylor if (sb != MAXOFFSET_T) { 38458488aeb5Staylor return (sb); 38468488aeb5Staylor } 38478488aeb5Staylor } 38488488aeb5Staylor return (MAXOFFSET_T); 38498488aeb5Staylor } 38508488aeb5Staylor 38518488aeb5Staylor /* 38528488aeb5Staylor * Label an individual disk. The name provided is the short name, 38538488aeb5Staylor * stripped of any leading /dev path. 38548488aeb5Staylor */ 38558488aeb5Staylor int 3856*6401734dSWill Andrews zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name) 38578488aeb5Staylor { 38588488aeb5Staylor char path[MAXPATHLEN]; 38598488aeb5Staylor struct dk_gpt *vtoc; 38608488aeb5Staylor int fd; 38618488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 38628488aeb5Staylor uint64_t slice_size; 38638488aeb5Staylor diskaddr_t start_block; 38648488aeb5Staylor char errbuf[1024]; 38658488aeb5Staylor 3866c6ef114fSmmusante /* prepare an error message just in case */ 3867c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 3868c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 3869c6ef114fSmmusante 38708488aeb5Staylor if (zhp) { 38718488aeb5Staylor nvlist_t *nvroot; 38728488aeb5Staylor 38738488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 38748488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 38758488aeb5Staylor 38768488aeb5Staylor if (zhp->zpool_start_block == 0) 38778488aeb5Staylor start_block = find_start_block(nvroot); 38788488aeb5Staylor else 38798488aeb5Staylor start_block = zhp->zpool_start_block; 38808488aeb5Staylor zhp->zpool_start_block = start_block; 38818488aeb5Staylor } else { 38828488aeb5Staylor /* new pool */ 38838488aeb5Staylor start_block = NEW_START_BLOCK; 38848488aeb5Staylor } 38858488aeb5Staylor 3886*6401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name, 38878488aeb5Staylor BACKUP_SLICE); 38888488aeb5Staylor 38898488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 38908488aeb5Staylor /* 38918488aeb5Staylor * This shouldn't happen. We've long since verified that this 38928488aeb5Staylor * is a valid device. 38938488aeb5Staylor */ 3894c6ef114fSmmusante zfs_error_aux(hdl, 3895c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 38968488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 38978488aeb5Staylor } 38988488aeb5Staylor 38998488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 39008488aeb5Staylor /* 39018488aeb5Staylor * The only way this can fail is if we run out of memory, or we 39028488aeb5Staylor * were unable to read the disk's capacity 39038488aeb5Staylor */ 39048488aeb5Staylor if (errno == ENOMEM) 39058488aeb5Staylor (void) no_memory(hdl); 39068488aeb5Staylor 39078488aeb5Staylor (void) close(fd); 3908c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3909c6ef114fSmmusante "unable to read disk capacity"), name); 39108488aeb5Staylor 39118488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 39128488aeb5Staylor } 39138488aeb5Staylor 39148488aeb5Staylor slice_size = vtoc->efi_last_u_lba + 1; 39158488aeb5Staylor slice_size -= EFI_MIN_RESV_SIZE; 39168488aeb5Staylor if (start_block == MAXOFFSET_T) 39178488aeb5Staylor start_block = NEW_START_BLOCK; 39188488aeb5Staylor slice_size -= start_block; 39198488aeb5Staylor 39208488aeb5Staylor vtoc->efi_parts[0].p_start = start_block; 39218488aeb5Staylor vtoc->efi_parts[0].p_size = slice_size; 39228488aeb5Staylor 39238488aeb5Staylor /* 39248488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 39258488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 39268488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 39278488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 39288488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 39298488aeb5Staylor * can get, in the absence of V_OTHER. 39308488aeb5Staylor */ 39318488aeb5Staylor vtoc->efi_parts[0].p_tag = V_USR; 39328488aeb5Staylor (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 39338488aeb5Staylor 39348488aeb5Staylor vtoc->efi_parts[8].p_start = slice_size + start_block; 39358488aeb5Staylor vtoc->efi_parts[8].p_size = resv; 39368488aeb5Staylor vtoc->efi_parts[8].p_tag = V_RESERVED; 39378488aeb5Staylor 39388488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 39398488aeb5Staylor /* 39408488aeb5Staylor * Some block drivers (like pcata) may not support EFI 39418488aeb5Staylor * GPT labels. Print out a helpful error message dir- 39428488aeb5Staylor * ecting the user to manually label the disk and give 39438488aeb5Staylor * a specific slice. 39448488aeb5Staylor */ 39458488aeb5Staylor (void) close(fd); 39468488aeb5Staylor efi_free(vtoc); 39478488aeb5Staylor 39488488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3949c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 39508488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 39518488aeb5Staylor } 39528488aeb5Staylor 39538488aeb5Staylor (void) close(fd); 39548488aeb5Staylor efi_free(vtoc); 39558488aeb5Staylor return (0); 39568488aeb5Staylor } 3957e7cbe64fSgw 3958e7cbe64fSgw static boolean_t 3959e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 3960e7cbe64fSgw { 3961e7cbe64fSgw char *type; 3962e7cbe64fSgw nvlist_t **child; 3963e7cbe64fSgw uint_t children, c; 3964e7cbe64fSgw 3965e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 3966810e43b2SBill Pijewski if (strcmp(type, VDEV_TYPE_FILE) == 0 || 396788ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 || 3968e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) { 3969e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3970e7cbe64fSgw "vdev type '%s' is not supported"), type); 3971e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 3972e7cbe64fSgw return (B_FALSE); 3973e7cbe64fSgw } 3974e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 3975e7cbe64fSgw &child, &children) == 0) { 3976e7cbe64fSgw for (c = 0; c < children; c++) { 3977e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 3978e7cbe64fSgw return (B_FALSE); 3979e7cbe64fSgw } 3980e7cbe64fSgw } 3981e7cbe64fSgw return (B_TRUE); 3982e7cbe64fSgw } 3983e7cbe64fSgw 3984e7cbe64fSgw /* 3985810e43b2SBill Pijewski * Check if this zvol is allowable for use as a dump device; zero if 3986810e43b2SBill Pijewski * it is, > 0 if it isn't, < 0 if it isn't a zvol. 3987810e43b2SBill Pijewski * 3988810e43b2SBill Pijewski * Allowable storage configurations include mirrors, all raidz variants, and 3989810e43b2SBill Pijewski * pools with log, cache, and spare devices. Pools which are backed by files or 3990810e43b2SBill Pijewski * have missing/hole vdevs are not suitable. 3991e7cbe64fSgw */ 3992e7cbe64fSgw int 3993e7cbe64fSgw zvol_check_dump_config(char *arg) 3994e7cbe64fSgw { 3995e7cbe64fSgw zpool_handle_t *zhp = NULL; 3996e7cbe64fSgw nvlist_t *config, *nvroot; 3997e7cbe64fSgw char *p, *volname; 3998e7cbe64fSgw nvlist_t **top; 3999e7cbe64fSgw uint_t toplevels; 4000e7cbe64fSgw libzfs_handle_t *hdl; 4001e7cbe64fSgw char errbuf[1024]; 4002e7cbe64fSgw char poolname[ZPOOL_MAXNAMELEN]; 4003e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR); 4004e7cbe64fSgw int ret = 1; 4005e7cbe64fSgw 4006e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 4007e7cbe64fSgw return (-1); 4008e7cbe64fSgw } 4009e7cbe64fSgw 4010e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4011e7cbe64fSgw "dump is not supported on device '%s'"), arg); 4012e7cbe64fSgw 4013e7cbe64fSgw if ((hdl = libzfs_init()) == NULL) 4014e7cbe64fSgw return (1); 4015e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE); 4016e7cbe64fSgw 4017e7cbe64fSgw volname = arg + pathlen; 4018e7cbe64fSgw 4019e7cbe64fSgw /* check the configuration of the pool */ 4020e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) { 4021e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4022e7cbe64fSgw "malformed dataset name")); 4023e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4024e7cbe64fSgw return (1); 4025e7cbe64fSgw } else if (p - volname >= ZFS_MAXNAMELEN) { 4026e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4027e7cbe64fSgw "dataset name is too long")); 4028e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 4029e7cbe64fSgw return (1); 4030e7cbe64fSgw } else { 4031e7cbe64fSgw (void) strncpy(poolname, volname, p - volname); 4032e7cbe64fSgw poolname[p - volname] = '\0'; 4033e7cbe64fSgw } 4034e7cbe64fSgw 4035e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) { 4036e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4037e7cbe64fSgw "could not open pool '%s'"), poolname); 4038e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4039e7cbe64fSgw goto out; 4040e7cbe64fSgw } 4041e7cbe64fSgw config = zpool_get_config(zhp, NULL); 4042e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4043e7cbe64fSgw &nvroot) != 0) { 4044e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4045e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname); 4046e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 4047e7cbe64fSgw goto out; 4048e7cbe64fSgw } 4049e7cbe64fSgw 4050e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 4051e7cbe64fSgw &top, &toplevels) == 0); 4052e7cbe64fSgw 4053e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 4054e7cbe64fSgw goto out; 4055e7cbe64fSgw } 4056e7cbe64fSgw ret = 0; 4057e7cbe64fSgw 4058e7cbe64fSgw out: 4059e7cbe64fSgw if (zhp) 4060e7cbe64fSgw zpool_close(zhp); 4061e7cbe64fSgw libzfs_fini(hdl); 4062e7cbe64fSgw return (ret); 4063e7cbe64fSgw } 4064