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. 245cabbc6bSPrashanth Sreenivasa * Copyright (c) 2011, 2017 by Delphix. All rights reserved. 25810e43b2SBill Pijewski * Copyright (c) 2013, Joyent, Inc. All rights reserved. 266401734dSWill Andrews * Copyright 2016 Nexenta Systems, Inc. 2788f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 281702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc. 29fa9e4066Sahrens */ 30fa9e4066Sahrens 31fa9e4066Sahrens #include <ctype.h> 32fa9e4066Sahrens #include <errno.h> 33fa9e4066Sahrens #include <devid.h> 34fa9e4066Sahrens #include <fcntl.h> 35fa9e4066Sahrens #include <libintl.h> 36fa9e4066Sahrens #include <stdio.h> 37fa9e4066Sahrens #include <stdlib.h> 38f3861e1aSahl #include <strings.h> 39fa9e4066Sahrens #include <unistd.h> 404445fffbSMatthew Ahrens #include <libgen.h> 418488aeb5Staylor #include <sys/efi_partition.h> 428488aeb5Staylor #include <sys/vtoc.h> 43fa9e4066Sahrens #include <sys/zfs_ioctl.h> 44573ca77eSGeorge Wilson #include <dlfcn.h> 45fa9e4066Sahrens 46fa9e4066Sahrens #include "zfs_namecheck.h" 47b1b8ab34Slling #include "zfs_prop.h" 48fa9e4066Sahrens #include "libzfs_impl.h" 49468c413aSTim Haley #include "zfs_comutil.h" 50ad135b5dSChristopher Siden #include "zfeature_common.h" 51fa9e4066Sahrens 527855d95bSToomas Soome static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *); 532ba5f978SAlan Somers static boolean_t zpool_vdev_is_interior(const char *name); 54990b4856Slling 55573ca77eSGeorge Wilson #define BACKUP_SLICE "s2" 56573ca77eSGeorge Wilson 57f9af39baSGeorge Wilson typedef struct prop_flags { 58f9af39baSGeorge Wilson int create:1; /* Validate property on creation */ 59f9af39baSGeorge Wilson int import:1; /* Validate property on import */ 60f9af39baSGeorge Wilson } prop_flags_t; 61f9af39baSGeorge Wilson 62990b4856Slling /* 63990b4856Slling * ==================================================================== 64990b4856Slling * zpool property functions 65990b4856Slling * ==================================================================== 66990b4856Slling */ 67990b4856Slling 68990b4856Slling static int 69990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 70990b4856Slling { 71990b4856Slling zfs_cmd_t zc = { 0 }; 72990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 73990b4856Slling 74990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 75990b4856Slling 76990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 77990b4856Slling return (-1); 78990b4856Slling 79990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 80990b4856Slling if (errno == ENOMEM) { 81990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 82990b4856Slling zcmd_free_nvlists(&zc); 83990b4856Slling return (-1); 84990b4856Slling } 85990b4856Slling } else { 86990b4856Slling zcmd_free_nvlists(&zc); 87990b4856Slling return (-1); 88990b4856Slling } 89990b4856Slling } 90990b4856Slling 91990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 92990b4856Slling zcmd_free_nvlists(&zc); 93990b4856Slling return (-1); 94990b4856Slling } 95990b4856Slling 96990b4856Slling zcmd_free_nvlists(&zc); 97990b4856Slling 98990b4856Slling return (0); 99990b4856Slling } 100990b4856Slling 101990b4856Slling static int 102990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 103990b4856Slling { 104990b4856Slling nvlist_t *old_props; 105990b4856Slling 106990b4856Slling old_props = zhp->zpool_props; 107990b4856Slling 108990b4856Slling if (zpool_get_all_props(zhp) != 0) 109990b4856Slling return (-1); 110990b4856Slling 111990b4856Slling nvlist_free(old_props); 112990b4856Slling return (0); 113990b4856Slling } 114990b4856Slling 115990b4856Slling static char * 116990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 117990b4856Slling zprop_source_t *src) 118990b4856Slling { 119990b4856Slling nvlist_t *nv, *nvl; 120990b4856Slling uint64_t ival; 121990b4856Slling char *value; 122990b4856Slling zprop_source_t source; 123990b4856Slling 124990b4856Slling nvl = zhp->zpool_props; 125990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 126990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 127990b4856Slling source = ival; 128990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 129990b4856Slling } else { 130990b4856Slling source = ZPROP_SRC_DEFAULT; 131990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 132990b4856Slling value = "-"; 133990b4856Slling } 134990b4856Slling 135990b4856Slling if (src) 136990b4856Slling *src = source; 137990b4856Slling 138990b4856Slling return (value); 139990b4856Slling } 140990b4856Slling 141990b4856Slling uint64_t 142990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 143990b4856Slling { 144990b4856Slling nvlist_t *nv, *nvl; 145990b4856Slling uint64_t value; 146990b4856Slling zprop_source_t source; 147990b4856Slling 148b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 149b87f3af3Sperrin /* 150b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 151b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 152b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 153b87f3af3Sperrin */ 154b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 155b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 156b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 157b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 158b87f3af3Sperrin == 0)) { 159b87f3af3Sperrin return (value); 160b87f3af3Sperrin } 161990b4856Slling return (zpool_prop_default_numeric(prop)); 162b87f3af3Sperrin } 163990b4856Slling 164990b4856Slling nvl = zhp->zpool_props; 165990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 166990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 167990b4856Slling source = value; 168990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 169990b4856Slling } else { 170990b4856Slling source = ZPROP_SRC_DEFAULT; 171990b4856Slling value = zpool_prop_default_numeric(prop); 172990b4856Slling } 173990b4856Slling 174990b4856Slling if (src) 175990b4856Slling *src = source; 176990b4856Slling 177990b4856Slling return (value); 178990b4856Slling } 179990b4856Slling 180990b4856Slling /* 181990b4856Slling * Map VDEV STATE to printed strings. 182990b4856Slling */ 1836401734dSWill Andrews const char * 184990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 185990b4856Slling { 186990b4856Slling switch (state) { 187990b4856Slling case VDEV_STATE_CLOSED: 188990b4856Slling case VDEV_STATE_OFFLINE: 189990b4856Slling return (gettext("OFFLINE")); 190990b4856Slling case VDEV_STATE_REMOVED: 191990b4856Slling return (gettext("REMOVED")); 192990b4856Slling case VDEV_STATE_CANT_OPEN: 193b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 194990b4856Slling return (gettext("FAULTED")); 1951195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL) 1961195e687SMark J Musante return (gettext("SPLIT")); 197990b4856Slling else 198990b4856Slling return (gettext("UNAVAIL")); 199990b4856Slling case VDEV_STATE_FAULTED: 200990b4856Slling return (gettext("FAULTED")); 201990b4856Slling case VDEV_STATE_DEGRADED: 202990b4856Slling return (gettext("DEGRADED")); 203990b4856Slling case VDEV_STATE_HEALTHY: 204990b4856Slling return (gettext("ONLINE")); 20588f61deeSIgor Kozhukhov 20688f61deeSIgor Kozhukhov default: 20788f61deeSIgor Kozhukhov break; 208990b4856Slling } 209990b4856Slling 210990b4856Slling return (gettext("UNKNOWN")); 211990b4856Slling } 212990b4856Slling 2136401734dSWill Andrews /* 2146401734dSWill Andrews * Map POOL STATE to printed strings. 2156401734dSWill Andrews */ 2166401734dSWill Andrews const char * 2176401734dSWill Andrews zpool_pool_state_to_name(pool_state_t state) 2186401734dSWill Andrews { 2196401734dSWill Andrews switch (state) { 2206401734dSWill Andrews case POOL_STATE_ACTIVE: 2216401734dSWill Andrews return (gettext("ACTIVE")); 2226401734dSWill Andrews case POOL_STATE_EXPORTED: 2236401734dSWill Andrews return (gettext("EXPORTED")); 2246401734dSWill Andrews case POOL_STATE_DESTROYED: 2256401734dSWill Andrews return (gettext("DESTROYED")); 2266401734dSWill Andrews case POOL_STATE_SPARE: 2276401734dSWill Andrews return (gettext("SPARE")); 2286401734dSWill Andrews case POOL_STATE_L2CACHE: 2296401734dSWill Andrews return (gettext("L2CACHE")); 2306401734dSWill Andrews case POOL_STATE_UNINITIALIZED: 2316401734dSWill Andrews return (gettext("UNINITIALIZED")); 2326401734dSWill Andrews case POOL_STATE_UNAVAIL: 2336401734dSWill Andrews return (gettext("UNAVAIL")); 2346401734dSWill Andrews case POOL_STATE_POTENTIALLY_ACTIVE: 2356401734dSWill Andrews return (gettext("POTENTIALLY_ACTIVE")); 2366401734dSWill Andrews } 2376401734dSWill Andrews 2386401734dSWill Andrews return (gettext("UNKNOWN")); 2396401734dSWill Andrews } 2406401734dSWill Andrews 241990b4856Slling /* 242990b4856Slling * Get a zpool property value for 'prop' and return the value in 243990b4856Slling * a pre-allocated buffer. 244990b4856Slling */ 245990b4856Slling int 246990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 247c58b3526SAdam Stevko zprop_source_t *srctype, boolean_t literal) 248990b4856Slling { 249990b4856Slling uint64_t intval; 250990b4856Slling const char *strval; 251990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 252990b4856Slling nvlist_t *nvroot; 253990b4856Slling vdev_stat_t *vs; 254990b4856Slling uint_t vsc; 255990b4856Slling 256990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 257379c004dSEric Schrock switch (prop) { 258379c004dSEric Schrock case ZPOOL_PROP_NAME: 259990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 260379c004dSEric Schrock break; 261379c004dSEric Schrock 262379c004dSEric Schrock case ZPOOL_PROP_HEALTH: 263990b4856Slling (void) strlcpy(buf, "FAULTED", len); 264379c004dSEric Schrock break; 265379c004dSEric Schrock 266379c004dSEric Schrock case ZPOOL_PROP_GUID: 267379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src); 268379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval); 269379c004dSEric Schrock break; 270379c004dSEric Schrock 271379c004dSEric Schrock case ZPOOL_PROP_ALTROOT: 272379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE: 2738704186eSDan McDonald case ZPOOL_PROP_COMMENT: 274379c004dSEric Schrock if (zhp->zpool_props != NULL || 275379c004dSEric Schrock zpool_get_all_props(zhp) == 0) { 276379c004dSEric Schrock (void) strlcpy(buf, 277379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src), 278379c004dSEric Schrock len); 279c58b3526SAdam Stevko break; 280379c004dSEric Schrock } 281379c004dSEric Schrock /* FALLTHROUGH */ 282379c004dSEric Schrock default: 283990b4856Slling (void) strlcpy(buf, "-", len); 284379c004dSEric Schrock break; 285379c004dSEric Schrock } 286379c004dSEric Schrock 287379c004dSEric Schrock if (srctype != NULL) 288379c004dSEric Schrock *srctype = src; 289990b4856Slling return (0); 290990b4856Slling } 291990b4856Slling 292990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 293990b4856Slling prop != ZPOOL_PROP_NAME) 294990b4856Slling return (-1); 295990b4856Slling 296990b4856Slling switch (zpool_prop_get_type(prop)) { 297990b4856Slling case PROP_TYPE_STRING: 298990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 299990b4856Slling len); 300990b4856Slling break; 301990b4856Slling 302990b4856Slling case PROP_TYPE_NUMBER: 303990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 304990b4856Slling 305990b4856Slling switch (prop) { 306990b4856Slling case ZPOOL_PROP_SIZE: 307485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED: 308485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE: 309ad135b5dSChristopher Siden case ZPOOL_PROP_FREEING: 3107fd05ac4SMatthew Ahrens case ZPOOL_PROP_LEAKED: 311c58b3526SAdam Stevko if (literal) { 312c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 313c58b3526SAdam Stevko (u_longlong_t)intval); 314c58b3526SAdam Stevko } else { 315c58b3526SAdam Stevko (void) zfs_nicenum(intval, buf, len); 316c58b3526SAdam Stevko } 317990b4856Slling break; 3187855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE: 3197a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ: 320*86714001SSerapheim Dimitropoulos case ZPOOL_PROP_CHECKPOINT: 3217a09f97bSGeorge Wilson if (intval == 0) { 3227a09f97bSGeorge Wilson (void) strlcpy(buf, "-", len); 3237a09f97bSGeorge Wilson } else if (literal) { 3247a09f97bSGeorge Wilson (void) snprintf(buf, len, "%llu", 3257a09f97bSGeorge Wilson (u_longlong_t)intval); 3267a09f97bSGeorge Wilson } else { 3277a09f97bSGeorge Wilson (void) zfs_nicenum(intval, buf, len); 3287a09f97bSGeorge Wilson } 3297a09f97bSGeorge Wilson break; 330990b4856Slling case ZPOOL_PROP_CAPACITY: 331c58b3526SAdam Stevko if (literal) { 332c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 333c58b3526SAdam Stevko (u_longlong_t)intval); 334c58b3526SAdam Stevko } else { 335c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu%%", 336c58b3526SAdam Stevko (u_longlong_t)intval); 337c58b3526SAdam Stevko } 338990b4856Slling break; 3392e4c9986SGeorge Wilson case ZPOOL_PROP_FRAGMENTATION: 3402e4c9986SGeorge Wilson if (intval == UINT64_MAX) { 3412e4c9986SGeorge Wilson (void) strlcpy(buf, "-", len); 3422e4c9986SGeorge Wilson } else { 3432e4c9986SGeorge Wilson (void) snprintf(buf, len, "%llu%%", 3442e4c9986SGeorge Wilson (u_longlong_t)intval); 3452e4c9986SGeorge Wilson } 3462e4c9986SGeorge Wilson break; 347b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO: 348b24ab676SJeff Bonwick (void) snprintf(buf, len, "%llu.%02llux", 349b24ab676SJeff Bonwick (u_longlong_t)(intval / 100), 350b24ab676SJeff Bonwick (u_longlong_t)(intval % 100)); 351b24ab676SJeff Bonwick break; 352990b4856Slling case ZPOOL_PROP_HEALTH: 353990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 354990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 355990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 3563f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 3573f9d6ad7SLin Ling == 0); 358990b4856Slling 359990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 360990b4856Slling vs->vs_aux), len); 361990b4856Slling break; 362ad135b5dSChristopher Siden case ZPOOL_PROP_VERSION: 363ad135b5dSChristopher Siden if (intval >= SPA_VERSION_FEATURES) { 364ad135b5dSChristopher Siden (void) snprintf(buf, len, "-"); 365ad135b5dSChristopher Siden break; 366ad135b5dSChristopher Siden } 367ad135b5dSChristopher Siden /* FALLTHROUGH */ 368990b4856Slling default: 369990b4856Slling (void) snprintf(buf, len, "%llu", intval); 370990b4856Slling } 371990b4856Slling break; 372990b4856Slling 373990b4856Slling case PROP_TYPE_INDEX: 374990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 375990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 376990b4856Slling != 0) 377990b4856Slling return (-1); 378990b4856Slling (void) strlcpy(buf, strval, len); 379990b4856Slling break; 380990b4856Slling 381990b4856Slling default: 382990b4856Slling abort(); 383990b4856Slling } 384990b4856Slling 385990b4856Slling if (srctype) 386990b4856Slling *srctype = src; 387990b4856Slling 388990b4856Slling return (0); 389990b4856Slling } 390990b4856Slling 391990b4856Slling /* 392990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 393990b4856Slling * Assuming bootfs is a valid dataset name. 394990b4856Slling */ 395990b4856Slling static boolean_t 396990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 397990b4856Slling { 398990b4856Slling int len = strlen(pool); 399990b4856Slling 400fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 401990b4856Slling return (B_FALSE); 402990b4856Slling 403990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 404990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 405990b4856Slling return (B_TRUE); 406990b4856Slling 407990b4856Slling return (B_FALSE); 408990b4856Slling } 409990b4856Slling 4104263d13fSGeorge Wilson boolean_t 4114263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp) 412b5b76fecSGeorge Wilson { 4139adfa60dSMatthew Ahrens char bootfs[ZFS_MAX_DATASET_NAME_LEN]; 414b5b76fecSGeorge Wilson 415b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 416c58b3526SAdam Stevko sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-", 417b5b76fecSGeorge Wilson sizeof (bootfs)) != 0); 418b5b76fecSGeorge Wilson } 419b5b76fecSGeorge Wilson 420b5b76fecSGeorge Wilson 421990b4856Slling /* 422990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 423990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 424990b4856Slling * specified as strings. 425990b4856Slling */ 426990b4856Slling static nvlist_t * 4270a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 428f9af39baSGeorge Wilson nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf) 429990b4856Slling { 430990b4856Slling nvpair_t *elem; 431990b4856Slling nvlist_t *retprops; 432990b4856Slling zpool_prop_t prop; 433990b4856Slling char *strval; 434990b4856Slling uint64_t intval; 4358704186eSDan McDonald char *slash, *check; 4362f8aaab3Seschrock struct stat64 statbuf; 43715e6edf1Sgw zpool_handle_t *zhp; 438990b4856Slling 439990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 440990b4856Slling (void) no_memory(hdl); 441990b4856Slling return (NULL); 442990b4856Slling } 443990b4856Slling 444990b4856Slling elem = NULL; 445990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 446990b4856Slling const char *propname = nvpair_name(elem); 447990b4856Slling 448ad135b5dSChristopher Siden prop = zpool_name_to_prop(propname); 4494ae5f5f0SAlan Somers if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) { 450ad135b5dSChristopher Siden int err; 451ad135b5dSChristopher Siden char *fname = strchr(propname, '@') + 1; 452ad135b5dSChristopher Siden 4532acef22dSMatthew Ahrens err = zfeature_lookup_name(fname, NULL); 454ad135b5dSChristopher Siden if (err != 0) { 455ad135b5dSChristopher Siden ASSERT3U(err, ==, ENOENT); 456ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 457ad135b5dSChristopher Siden "invalid feature '%s'"), fname); 458ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 459ad135b5dSChristopher Siden goto error; 460ad135b5dSChristopher Siden } 461ad135b5dSChristopher Siden 462ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_STRING) { 463ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 464ad135b5dSChristopher Siden "'%s' must be a string"), propname); 465ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 466ad135b5dSChristopher Siden goto error; 467ad135b5dSChristopher Siden } 468ad135b5dSChristopher Siden 469ad135b5dSChristopher Siden (void) nvpair_value_string(elem, &strval); 470ad135b5dSChristopher Siden if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) { 471ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 472ad135b5dSChristopher Siden "property '%s' can only be set to " 473ad135b5dSChristopher Siden "'enabled'"), propname); 474ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 475ad135b5dSChristopher Siden goto error; 476ad135b5dSChristopher Siden } 477ad135b5dSChristopher Siden 478ad135b5dSChristopher Siden if (nvlist_add_uint64(retprops, propname, 0) != 0) { 479ad135b5dSChristopher Siden (void) no_memory(hdl); 480ad135b5dSChristopher Siden goto error; 481ad135b5dSChristopher Siden } 482ad135b5dSChristopher Siden continue; 483ad135b5dSChristopher Siden } 484ad135b5dSChristopher Siden 485990b4856Slling /* 486990b4856Slling * Make sure this property is valid and applies to this type. 487990b4856Slling */ 4884ae5f5f0SAlan Somers if (prop == ZPOOL_PROP_INVAL) { 489990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 490990b4856Slling "invalid property '%s'"), propname); 491990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 492990b4856Slling goto error; 493990b4856Slling } 494990b4856Slling 495990b4856Slling if (zpool_prop_readonly(prop)) { 496990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 497990b4856Slling "is readonly"), propname); 498990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 499990b4856Slling goto error; 500990b4856Slling } 501990b4856Slling 502990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 503990b4856Slling &strval, &intval, errbuf) != 0) 504990b4856Slling goto error; 505990b4856Slling 506990b4856Slling /* 507990b4856Slling * Perform additional checking for specific properties. 508990b4856Slling */ 509990b4856Slling switch (prop) { 510990b4856Slling case ZPOOL_PROP_VERSION: 511ad135b5dSChristopher Siden if (intval < version || 512ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(intval)) { 513990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 514990b4856Slling "property '%s' number %d is invalid."), 515990b4856Slling propname, intval); 516990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 517990b4856Slling goto error; 518990b4856Slling } 519990b4856Slling break; 520990b4856Slling 5217855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE: 5227855d95bSToomas Soome if (!flags.create) { 5237855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5247855d95bSToomas Soome "property '%s' can only be set during pool " 5257855d95bSToomas Soome "creation"), propname); 5267855d95bSToomas Soome (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5277855d95bSToomas Soome goto error; 5287855d95bSToomas Soome } 5297855d95bSToomas Soome break; 5307855d95bSToomas Soome 531990b4856Slling case ZPOOL_PROP_BOOTFS: 532f9af39baSGeorge Wilson if (flags.create || flags.import) { 533990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 534990b4856Slling "property '%s' cannot be set at creation " 535990b4856Slling "or import time"), propname); 536990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 537990b4856Slling goto error; 538990b4856Slling } 539990b4856Slling 540990b4856Slling if (version < SPA_VERSION_BOOTFS) { 541990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 542990b4856Slling "pool must be upgraded to support " 543990b4856Slling "'%s' property"), propname); 544990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 545990b4856Slling goto error; 546990b4856Slling } 547990b4856Slling 548990b4856Slling /* 549990b4856Slling * bootfs property value has to be a dataset name and 550990b4856Slling * the dataset has to be in the same pool as it sets to. 551990b4856Slling */ 552990b4856Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 553990b4856Slling strval)) { 554990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 555990b4856Slling "is an invalid name"), strval); 556990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 557990b4856Slling goto error; 558990b4856Slling } 55915e6edf1Sgw 56015e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 56115e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 56215e6edf1Sgw "could not open pool '%s'"), poolname); 56315e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 56415e6edf1Sgw goto error; 56515e6edf1Sgw } 56615e6edf1Sgw zpool_close(zhp); 567990b4856Slling break; 568990b4856Slling 5692f8aaab3Seschrock case ZPOOL_PROP_ALTROOT: 570f9af39baSGeorge Wilson if (!flags.create && !flags.import) { 571990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 572990b4856Slling "property '%s' can only be set during pool " 573990b4856Slling "creation or import"), propname); 574990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 575990b4856Slling goto error; 576990b4856Slling } 577990b4856Slling 5782f8aaab3Seschrock if (strval[0] != '/') { 579990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5802f8aaab3Seschrock "bad alternate root '%s'"), strval); 5812f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 582990b4856Slling goto error; 583990b4856Slling } 5842f8aaab3Seschrock break; 5852f8aaab3Seschrock 5862f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5872f8aaab3Seschrock if (strval[0] == '\0') 5882f8aaab3Seschrock break; 5892f8aaab3Seschrock 5902f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5912f8aaab3Seschrock break; 592990b4856Slling 593990b4856Slling if (strval[0] != '/') { 594990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5952f8aaab3Seschrock "property '%s' must be empty, an " 5962f8aaab3Seschrock "absolute path, or 'none'"), propname); 597990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 598990b4856Slling goto error; 599990b4856Slling } 600990b4856Slling 6012f8aaab3Seschrock slash = strrchr(strval, '/'); 602990b4856Slling 6032f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 6042f8aaab3Seschrock strcmp(slash, "/..") == 0) { 6052f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6062f8aaab3Seschrock "'%s' is not a valid file"), strval); 6072f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6082f8aaab3Seschrock goto error; 6092f8aaab3Seschrock } 610990b4856Slling 6112f8aaab3Seschrock *slash = '\0'; 6122f8aaab3Seschrock 6132c32020fSeschrock if (strval[0] != '\0' && 6142c32020fSeschrock (stat64(strval, &statbuf) != 0 || 6152c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 6162f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6172f8aaab3Seschrock "'%s' is not a valid directory"), 6182f8aaab3Seschrock strval); 6192f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6202f8aaab3Seschrock goto error; 6212f8aaab3Seschrock } 6222f8aaab3Seschrock 6232f8aaab3Seschrock *slash = '/'; 6242f8aaab3Seschrock break; 625f9af39baSGeorge Wilson 6268704186eSDan McDonald case ZPOOL_PROP_COMMENT: 6278704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 6288704186eSDan McDonald if (!isprint(*check)) { 6298704186eSDan McDonald zfs_error_aux(hdl, 6308704186eSDan McDonald dgettext(TEXT_DOMAIN, 6318704186eSDan McDonald "comment may only have printable " 6328704186eSDan McDonald "characters")); 6338704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, 6348704186eSDan McDonald errbuf); 6358704186eSDan McDonald goto error; 6368704186eSDan McDonald } 6378704186eSDan McDonald } 6388704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) { 6398704186eSDan McDonald zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6408704186eSDan McDonald "comment must not exceed %d characters"), 6418704186eSDan McDonald ZPROP_MAX_COMMENT); 6428704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6438704186eSDan McDonald goto error; 6448704186eSDan McDonald } 6458704186eSDan McDonald break; 646f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 647f9af39baSGeorge Wilson if (!flags.import) { 648f9af39baSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 649f9af39baSGeorge Wilson "property '%s' can only be set at " 650f9af39baSGeorge Wilson "import time"), propname); 651f9af39baSGeorge Wilson (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 652f9af39baSGeorge Wilson goto error; 653f9af39baSGeorge Wilson } 654f9af39baSGeorge Wilson break; 65588f61deeSIgor Kozhukhov 65688f61deeSIgor Kozhukhov default: 65788f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 65888f61deeSIgor Kozhukhov "property '%s'(%d) not defined"), propname, prop); 65988f61deeSIgor Kozhukhov break; 660990b4856Slling } 661990b4856Slling } 662990b4856Slling 663990b4856Slling return (retprops); 664990b4856Slling error: 665990b4856Slling nvlist_free(retprops); 666990b4856Slling return (NULL); 667990b4856Slling } 668990b4856Slling 669990b4856Slling /* 670990b4856Slling * Set zpool property : propname=propval. 671990b4856Slling */ 672990b4856Slling int 673990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 674990b4856Slling { 675990b4856Slling zfs_cmd_t zc = { 0 }; 676990b4856Slling int ret = -1; 677990b4856Slling char errbuf[1024]; 678990b4856Slling nvlist_t *nvl = NULL; 679990b4856Slling nvlist_t *realprops; 680990b4856Slling uint64_t version; 681f9af39baSGeorge Wilson prop_flags_t flags = { 0 }; 682990b4856Slling 683990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 684990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 685990b4856Slling zhp->zpool_name); 686990b4856Slling 687990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 688990b4856Slling return (no_memory(zhp->zpool_hdl)); 689990b4856Slling 690990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 691990b4856Slling nvlist_free(nvl); 692990b4856Slling return (no_memory(zhp->zpool_hdl)); 693990b4856Slling } 694990b4856Slling 695990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 6960a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 697f9af39baSGeorge Wilson zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) { 698990b4856Slling nvlist_free(nvl); 699990b4856Slling return (-1); 700990b4856Slling } 701990b4856Slling 702990b4856Slling nvlist_free(nvl); 703990b4856Slling nvl = realprops; 704990b4856Slling 705990b4856Slling /* 706990b4856Slling * Execute the corresponding ioctl() to set this property. 707990b4856Slling */ 708990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 709990b4856Slling 710990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 711990b4856Slling nvlist_free(nvl); 712990b4856Slling return (-1); 713990b4856Slling } 714990b4856Slling 715990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 716990b4856Slling 717990b4856Slling zcmd_free_nvlists(&zc); 718990b4856Slling nvlist_free(nvl); 719990b4856Slling 720990b4856Slling if (ret) 721990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 722990b4856Slling else 723990b4856Slling (void) zpool_props_refresh(zhp); 724990b4856Slling 725990b4856Slling return (ret); 726990b4856Slling } 727990b4856Slling 728990b4856Slling int 729990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 730990b4856Slling { 731990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 732990b4856Slling zprop_list_t *entry; 733990b4856Slling char buf[ZFS_MAXPROPLEN]; 734ad135b5dSChristopher Siden nvlist_t *features = NULL; 735ad135b5dSChristopher Siden zprop_list_t **last; 736ad135b5dSChristopher Siden boolean_t firstexpand = (NULL == *plp); 737990b4856Slling 738990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 739990b4856Slling return (-1); 740990b4856Slling 741ad135b5dSChristopher Siden last = plp; 742ad135b5dSChristopher Siden while (*last != NULL) 743ad135b5dSChristopher Siden last = &(*last)->pl_next; 744ad135b5dSChristopher Siden 745ad135b5dSChristopher Siden if ((*plp)->pl_all) 746ad135b5dSChristopher Siden features = zpool_get_features(zhp); 747ad135b5dSChristopher Siden 748ad135b5dSChristopher Siden if ((*plp)->pl_all && firstexpand) { 749ad135b5dSChristopher Siden for (int i = 0; i < SPA_FEATURES; i++) { 750ad135b5dSChristopher Siden zprop_list_t *entry = zfs_alloc(hdl, 751ad135b5dSChristopher Siden sizeof (zprop_list_t)); 752ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 753ad135b5dSChristopher Siden entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s", 754ad135b5dSChristopher Siden spa_feature_table[i].fi_uname); 755ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 756ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 757ad135b5dSChristopher Siden 758ad135b5dSChristopher Siden *last = entry; 759ad135b5dSChristopher Siden last = &entry->pl_next; 760ad135b5dSChristopher Siden } 761ad135b5dSChristopher Siden } 762ad135b5dSChristopher Siden 763ad135b5dSChristopher Siden /* add any unsupported features */ 764ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL); 765ad135b5dSChristopher Siden nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) { 766ad135b5dSChristopher Siden char *propname; 767ad135b5dSChristopher Siden boolean_t found; 768ad135b5dSChristopher Siden zprop_list_t *entry; 769ad135b5dSChristopher Siden 770ad135b5dSChristopher Siden if (zfeature_is_supported(nvpair_name(nvp))) 771ad135b5dSChristopher Siden continue; 772ad135b5dSChristopher Siden 773ad135b5dSChristopher Siden propname = zfs_asprintf(hdl, "unsupported@%s", 774ad135b5dSChristopher Siden nvpair_name(nvp)); 775ad135b5dSChristopher Siden 776ad135b5dSChristopher Siden /* 777ad135b5dSChristopher Siden * Before adding the property to the list make sure that no 778ad135b5dSChristopher Siden * other pool already added the same property. 779ad135b5dSChristopher Siden */ 780ad135b5dSChristopher Siden found = B_FALSE; 781ad135b5dSChristopher Siden entry = *plp; 782ad135b5dSChristopher Siden while (entry != NULL) { 783ad135b5dSChristopher Siden if (entry->pl_user_prop != NULL && 784ad135b5dSChristopher Siden strcmp(propname, entry->pl_user_prop) == 0) { 785ad135b5dSChristopher Siden found = B_TRUE; 786ad135b5dSChristopher Siden break; 787ad135b5dSChristopher Siden } 788ad135b5dSChristopher Siden entry = entry->pl_next; 789ad135b5dSChristopher Siden } 790ad135b5dSChristopher Siden if (found) { 791ad135b5dSChristopher Siden free(propname); 792ad135b5dSChristopher Siden continue; 793ad135b5dSChristopher Siden } 794ad135b5dSChristopher Siden 795ad135b5dSChristopher Siden entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 796ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 797ad135b5dSChristopher Siden entry->pl_user_prop = propname; 798ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 799ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 800ad135b5dSChristopher Siden 801ad135b5dSChristopher Siden *last = entry; 802ad135b5dSChristopher Siden last = &entry->pl_next; 803ad135b5dSChristopher Siden } 804ad135b5dSChristopher Siden 805990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 806990b4856Slling 807990b4856Slling if (entry->pl_fixed) 808990b4856Slling continue; 809990b4856Slling 810990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 811990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 812c58b3526SAdam Stevko NULL, B_FALSE) == 0) { 813990b4856Slling if (strlen(buf) > entry->pl_width) 814990b4856Slling entry->pl_width = strlen(buf); 815990b4856Slling } 816990b4856Slling } 817990b4856Slling 818990b4856Slling return (0); 819990b4856Slling } 820990b4856Slling 821ad135b5dSChristopher Siden /* 822ad135b5dSChristopher Siden * Get the state for the given feature on the given ZFS pool. 823ad135b5dSChristopher Siden */ 824ad135b5dSChristopher Siden int 825ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf, 826ad135b5dSChristopher Siden size_t len) 827ad135b5dSChristopher Siden { 828ad135b5dSChristopher Siden uint64_t refcount; 829ad135b5dSChristopher Siden boolean_t found = B_FALSE; 830ad135b5dSChristopher Siden nvlist_t *features = zpool_get_features(zhp); 831ad135b5dSChristopher Siden boolean_t supported; 832ad135b5dSChristopher Siden const char *feature = strchr(propname, '@') + 1; 833ad135b5dSChristopher Siden 834ad135b5dSChristopher Siden supported = zpool_prop_feature(propname); 8357c13517fSSerapheim Dimitropoulos ASSERT(supported || zpool_prop_unsupported(propname)); 836ad135b5dSChristopher Siden 837ad135b5dSChristopher Siden /* 838ad135b5dSChristopher Siden * Convert from feature name to feature guid. This conversion is 839ad135b5dSChristopher Siden * unecessary for unsupported@... properties because they already 840ad135b5dSChristopher Siden * use guids. 841ad135b5dSChristopher Siden */ 842ad135b5dSChristopher Siden if (supported) { 843ad135b5dSChristopher Siden int ret; 8442acef22dSMatthew Ahrens spa_feature_t fid; 845ad135b5dSChristopher Siden 8462acef22dSMatthew Ahrens ret = zfeature_lookup_name(feature, &fid); 847ad135b5dSChristopher Siden if (ret != 0) { 848ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 849ad135b5dSChristopher Siden return (ENOTSUP); 850ad135b5dSChristopher Siden } 8512acef22dSMatthew Ahrens feature = spa_feature_table[fid].fi_guid; 852ad135b5dSChristopher Siden } 853ad135b5dSChristopher Siden 854ad135b5dSChristopher Siden if (nvlist_lookup_uint64(features, feature, &refcount) == 0) 855ad135b5dSChristopher Siden found = B_TRUE; 856ad135b5dSChristopher Siden 857ad135b5dSChristopher Siden if (supported) { 858ad135b5dSChristopher Siden if (!found) { 859ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len); 860ad135b5dSChristopher Siden } else { 861ad135b5dSChristopher Siden if (refcount == 0) 862ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len); 863ad135b5dSChristopher Siden else 864ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len); 865ad135b5dSChristopher Siden } 866ad135b5dSChristopher Siden } else { 867ad135b5dSChristopher Siden if (found) { 868ad135b5dSChristopher Siden if (refcount == 0) { 869ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE); 870ad135b5dSChristopher Siden } else { 871ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY); 872ad135b5dSChristopher Siden } 873ad135b5dSChristopher Siden } else { 874ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 875ad135b5dSChristopher Siden return (ENOTSUP); 876ad135b5dSChristopher Siden } 877ad135b5dSChristopher Siden } 878ad135b5dSChristopher Siden 879ad135b5dSChristopher Siden return (0); 880ad135b5dSChristopher Siden } 881990b4856Slling 882573ca77eSGeorge Wilson /* 883573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage 884573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead. 885573ca77eSGeorge Wilson */ 886573ca77eSGeorge Wilson #define NEW_START_BLOCK 256 887573ca77eSGeorge Wilson 888fa9e4066Sahrens /* 889fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 890fa9e4066Sahrens * 'buf'. 891fa9e4066Sahrens */ 892e7cbe64fSgw boolean_t 89399653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 894fa9e4066Sahrens { 895fa9e4066Sahrens namecheck_err_t why; 896fa9e4066Sahrens char what; 897b468a217Seschrock int ret; 898b468a217Seschrock 899b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 900b468a217Seschrock 901b468a217Seschrock /* 902b468a217Seschrock * The rules for reserved pool names were extended at a later point. 903b468a217Seschrock * But we need to support users with existing pools that may now be 904b468a217Seschrock * invalid. So we only check for this expanded set of names during a 905b468a217Seschrock * create (or import), and only in userland. 906b468a217Seschrock */ 907b468a217Seschrock if (ret == 0 && !isopen && 908b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 909b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 9108654d025Sperrin strncmp(pool, "spare", 5) == 0 || 9118654d025Sperrin strcmp(pool, "log") == 0)) { 912e7cbe64fSgw if (hdl != NULL) 913e7cbe64fSgw zfs_error_aux(hdl, 914e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved")); 91599653d4eSeschrock return (B_FALSE); 916b468a217Seschrock } 917b468a217Seschrock 918fa9e4066Sahrens 919b468a217Seschrock if (ret != 0) { 92099653d4eSeschrock if (hdl != NULL) { 921fa9e4066Sahrens switch (why) { 922b81d61a6Slling case NAME_ERR_TOOLONG: 92399653d4eSeschrock zfs_error_aux(hdl, 924b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 925b81d61a6Slling break; 926b81d61a6Slling 927fa9e4066Sahrens case NAME_ERR_INVALCHAR: 92899653d4eSeschrock zfs_error_aux(hdl, 929fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 930fa9e4066Sahrens "'%c' in pool name"), what); 931fa9e4066Sahrens break; 932fa9e4066Sahrens 933fa9e4066Sahrens case NAME_ERR_NOLETTER: 93499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 93599653d4eSeschrock "name must begin with a letter")); 936fa9e4066Sahrens break; 937fa9e4066Sahrens 938fa9e4066Sahrens case NAME_ERR_RESERVED: 93999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 94099653d4eSeschrock "name is reserved")); 941fa9e4066Sahrens break; 942fa9e4066Sahrens 943fa9e4066Sahrens case NAME_ERR_DISKLIKE: 94499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 94599653d4eSeschrock "pool name is reserved")); 946fa9e4066Sahrens break; 9475ad82045Snd 9485ad82045Snd case NAME_ERR_LEADING_SLASH: 9495ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9505ad82045Snd "leading slash in name")); 9515ad82045Snd break; 9525ad82045Snd 9535ad82045Snd case NAME_ERR_EMPTY_COMPONENT: 9545ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9555ad82045Snd "empty component in name")); 9565ad82045Snd break; 9575ad82045Snd 9585ad82045Snd case NAME_ERR_TRAILING_SLASH: 9595ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9605ad82045Snd "trailing slash in name")); 9615ad82045Snd break; 9625ad82045Snd 963edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS: 9645ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 965edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in " 966edb901aaSMarcel Telka "name")); 9675ad82045Snd break; 9685ad82045Snd 96988f61deeSIgor Kozhukhov default: 97088f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 97188f61deeSIgor Kozhukhov "(%d) not defined"), why); 97288f61deeSIgor Kozhukhov break; 973fa9e4066Sahrens } 974fa9e4066Sahrens } 97599653d4eSeschrock return (B_FALSE); 976fa9e4066Sahrens } 977fa9e4066Sahrens 97899653d4eSeschrock return (B_TRUE); 979fa9e4066Sahrens } 980fa9e4066Sahrens 981fa9e4066Sahrens /* 982fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 983fa9e4066Sahrens * state. 984fa9e4066Sahrens */ 985fa9e4066Sahrens zpool_handle_t * 98699653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 987fa9e4066Sahrens { 988fa9e4066Sahrens zpool_handle_t *zhp; 98994de1d4cSeschrock boolean_t missing; 990fa9e4066Sahrens 991fa9e4066Sahrens /* 992fa9e4066Sahrens * Make sure the pool name is valid. 993fa9e4066Sahrens */ 99499653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 995ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 99699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 99799653d4eSeschrock pool); 998fa9e4066Sahrens return (NULL); 999fa9e4066Sahrens } 1000fa9e4066Sahrens 100199653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 100299653d4eSeschrock return (NULL); 1003fa9e4066Sahrens 100499653d4eSeschrock zhp->zpool_hdl = hdl; 1005fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1006fa9e4066Sahrens 100794de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 100894de1d4cSeschrock zpool_close(zhp); 100994de1d4cSeschrock return (NULL); 101094de1d4cSeschrock } 101194de1d4cSeschrock 101294de1d4cSeschrock if (missing) { 1013990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 1014ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 1015990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 101694de1d4cSeschrock zpool_close(zhp); 101794de1d4cSeschrock return (NULL); 1018fa9e4066Sahrens } 1019fa9e4066Sahrens 1020fa9e4066Sahrens return (zhp); 1021fa9e4066Sahrens } 1022fa9e4066Sahrens 1023fa9e4066Sahrens /* 1024fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 1025fa9e4066Sahrens * the configuration cache may be out of date). 1026fa9e4066Sahrens */ 102794de1d4cSeschrock int 102894de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 1029fa9e4066Sahrens { 1030fa9e4066Sahrens zpool_handle_t *zhp; 103194de1d4cSeschrock boolean_t missing; 1032fa9e4066Sahrens 103394de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 103494de1d4cSeschrock return (-1); 1035fa9e4066Sahrens 103699653d4eSeschrock zhp->zpool_hdl = hdl; 1037fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1038fa9e4066Sahrens 103994de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 104094de1d4cSeschrock zpool_close(zhp); 104194de1d4cSeschrock return (-1); 1042fa9e4066Sahrens } 1043fa9e4066Sahrens 104494de1d4cSeschrock if (missing) { 104594de1d4cSeschrock zpool_close(zhp); 104694de1d4cSeschrock *ret = NULL; 104794de1d4cSeschrock return (0); 104894de1d4cSeschrock } 104994de1d4cSeschrock 105094de1d4cSeschrock *ret = zhp; 105194de1d4cSeschrock return (0); 1052fa9e4066Sahrens } 1053fa9e4066Sahrens 1054fa9e4066Sahrens /* 1055fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 1056fa9e4066Sahrens * state. 1057fa9e4066Sahrens */ 1058fa9e4066Sahrens zpool_handle_t * 105999653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 1060fa9e4066Sahrens { 1061fa9e4066Sahrens zpool_handle_t *zhp; 1062fa9e4066Sahrens 106399653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 1064fa9e4066Sahrens return (NULL); 1065fa9e4066Sahrens 1066fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 1067ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 106899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 1069fa9e4066Sahrens zpool_close(zhp); 1070fa9e4066Sahrens return (NULL); 1071fa9e4066Sahrens } 1072fa9e4066Sahrens 1073fa9e4066Sahrens return (zhp); 1074fa9e4066Sahrens } 1075fa9e4066Sahrens 1076fa9e4066Sahrens /* 1077fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 1078fa9e4066Sahrens */ 1079fa9e4066Sahrens void 1080fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 1081fa9e4066Sahrens { 1082aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_config); 1083aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_old_config); 1084aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_props); 1085fa9e4066Sahrens free(zhp); 1086fa9e4066Sahrens } 1087fa9e4066Sahrens 1088fa9e4066Sahrens /* 1089fa9e4066Sahrens * Return the name of the pool. 1090fa9e4066Sahrens */ 1091fa9e4066Sahrens const char * 1092fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 1093fa9e4066Sahrens { 1094fa9e4066Sahrens return (zhp->zpool_name); 1095fa9e4066Sahrens } 1096fa9e4066Sahrens 1097fa9e4066Sahrens 1098fa9e4066Sahrens /* 1099fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 1100fa9e4066Sahrens */ 1101fa9e4066Sahrens int 1102fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 1103fa9e4066Sahrens { 1104fa9e4066Sahrens return (zhp->zpool_state); 1105fa9e4066Sahrens } 1106fa9e4066Sahrens 1107fa9e4066Sahrens /* 1108fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 1109fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 1110fa9e4066Sahrens * don't have to worry about error semantics. 1111fa9e4066Sahrens */ 1112fa9e4066Sahrens int 111399653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 11140a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 1115fa9e4066Sahrens { 1116fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 11170a48a24eStimh nvlist_t *zc_fsprops = NULL; 11180a48a24eStimh nvlist_t *zc_props = NULL; 111999653d4eSeschrock char msg[1024]; 11200a48a24eStimh int ret = -1; 1121fa9e4066Sahrens 112299653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 112399653d4eSeschrock "cannot create '%s'"), pool); 1124fa9e4066Sahrens 112599653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 112699653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 1127fa9e4066Sahrens 1128351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1129990b4856Slling return (-1); 1130fa9e4066Sahrens 11310a48a24eStimh if (props) { 1132f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE }; 1133f9af39baSGeorge Wilson 11340a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 1135f9af39baSGeorge Wilson SPA_VERSION_1, flags, msg)) == NULL) { 11360a48a24eStimh goto create_failed; 11370a48a24eStimh } 11380a48a24eStimh } 113999653d4eSeschrock 11400a48a24eStimh if (fsprops) { 11410a48a24eStimh uint64_t zoned; 11420a48a24eStimh char *zonestr; 11430a48a24eStimh 11440a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 11450a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 11460a48a24eStimh strcmp(zonestr, "on") == 0); 11470a48a24eStimh 1148e9316f76SJoe Stein if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM, 1149e9316f76SJoe Stein fsprops, zoned, NULL, NULL, msg)) == NULL) { 11500a48a24eStimh goto create_failed; 11510a48a24eStimh } 11520a48a24eStimh if (!zc_props && 11530a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 11540a48a24eStimh goto create_failed; 11550a48a24eStimh } 11560a48a24eStimh if (nvlist_add_nvlist(zc_props, 11570a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 11580a48a24eStimh goto create_failed; 11590a48a24eStimh } 1160351420b3Slling } 1161fa9e4066Sahrens 11620a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 11630a48a24eStimh goto create_failed; 11640a48a24eStimh 1165990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 1166fa9e4066Sahrens 11670a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 1168351420b3Slling 1169e9dbad6fSeschrock zcmd_free_nvlists(&zc); 11700a48a24eStimh nvlist_free(zc_props); 11710a48a24eStimh nvlist_free(zc_fsprops); 1172fa9e4066Sahrens 117399653d4eSeschrock switch (errno) { 1174fa9e4066Sahrens case EBUSY: 1175fa9e4066Sahrens /* 1176fa9e4066Sahrens * This can happen if the user has specified the same 1177fa9e4066Sahrens * device multiple times. We can't reliably detect this 1178fa9e4066Sahrens * until we try to add it and see we already have a 1179fa9e4066Sahrens * label. 1180fa9e4066Sahrens */ 118199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 118299653d4eSeschrock "one or more vdevs refer to the same device")); 118399653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1184fa9e4066Sahrens 1185e9316f76SJoe Stein case ERANGE: 1186e9316f76SJoe Stein /* 1187e9316f76SJoe Stein * This happens if the record size is smaller or larger 1188e9316f76SJoe Stein * than the allowed size range, or not a power of 2. 1189e9316f76SJoe Stein * 1190e9316f76SJoe Stein * NOTE: although zfs_valid_proplist is called earlier, 1191e9316f76SJoe Stein * this case may have slipped through since the 1192e9316f76SJoe Stein * pool does not exist yet and it is therefore 1193e9316f76SJoe Stein * impossible to read properties e.g. max blocksize 1194e9316f76SJoe Stein * from the pool. 1195e9316f76SJoe Stein */ 1196e9316f76SJoe Stein zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1197e9316f76SJoe Stein "record size invalid")); 1198e9316f76SJoe Stein return (zfs_error(hdl, EZFS_BADPROP, msg)); 1199e9316f76SJoe Stein 1200fa9e4066Sahrens case EOVERFLOW: 1201fa9e4066Sahrens /* 120299653d4eSeschrock * This occurs when one of the devices is below 1203fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1204fa9e4066Sahrens * device was the problem device since there's no 1205fa9e4066Sahrens * reliable way to determine device size from userland. 1206fa9e4066Sahrens */ 1207fa9e4066Sahrens { 1208fa9e4066Sahrens char buf[64]; 1209fa9e4066Sahrens 1210fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1211fa9e4066Sahrens 121299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 121399653d4eSeschrock "one or more devices is less than the " 121499653d4eSeschrock "minimum size (%s)"), buf); 1215fa9e4066Sahrens } 121699653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1217fa9e4066Sahrens 1218fa9e4066Sahrens case ENOSPC: 121999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 122099653d4eSeschrock "one or more devices is out of space")); 122199653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1222fa9e4066Sahrens 1223fa94a07fSbrendan case ENOTBLK: 1224fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1225fa94a07fSbrendan "cache device must be a disk or disk slice")); 1226fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 1227fa94a07fSbrendan 1228fa9e4066Sahrens default: 122999653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1230fa9e4066Sahrens } 1231fa9e4066Sahrens } 1232fa9e4066Sahrens 12330a48a24eStimh create_failed: 1234351420b3Slling zcmd_free_nvlists(&zc); 12350a48a24eStimh nvlist_free(zc_props); 12360a48a24eStimh nvlist_free(zc_fsprops); 12370a48a24eStimh return (ret); 1238fa9e4066Sahrens } 1239fa9e4066Sahrens 1240fa9e4066Sahrens /* 1241fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 1242fa9e4066Sahrens * datasets left in the pool. 1243fa9e4066Sahrens */ 1244fa9e4066Sahrens int 12454445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str) 1246fa9e4066Sahrens { 1247fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1248fa9e4066Sahrens zfs_handle_t *zfp = NULL; 124999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 125099653d4eSeschrock char msg[1024]; 1251fa9e4066Sahrens 1252fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 1253cb04b873SMark J Musante (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1254fa9e4066Sahrens return (-1); 1255fa9e4066Sahrens 1256fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 12574445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 1258fa9e4066Sahrens 1259cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 126099653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 126199653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 1262fa9e4066Sahrens 126399653d4eSeschrock if (errno == EROFS) { 126499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 126599653d4eSeschrock "one or more devices is read only")); 126699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 126799653d4eSeschrock } else { 126899653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1269fa9e4066Sahrens } 1270fa9e4066Sahrens 1271fa9e4066Sahrens if (zfp) 1272fa9e4066Sahrens zfs_close(zfp); 1273fa9e4066Sahrens return (-1); 1274fa9e4066Sahrens } 1275fa9e4066Sahrens 1276fa9e4066Sahrens if (zfp) { 1277fa9e4066Sahrens remove_mountpoint(zfp); 1278fa9e4066Sahrens zfs_close(zfp); 1279fa9e4066Sahrens } 1280fa9e4066Sahrens 1281fa9e4066Sahrens return (0); 1282fa9e4066Sahrens } 1283fa9e4066Sahrens 1284*86714001SSerapheim Dimitropoulos /* 1285*86714001SSerapheim Dimitropoulos * Create a checkpoint in the given pool. 1286*86714001SSerapheim Dimitropoulos */ 1287*86714001SSerapheim Dimitropoulos int 1288*86714001SSerapheim Dimitropoulos zpool_checkpoint(zpool_handle_t *zhp) 1289*86714001SSerapheim Dimitropoulos { 1290*86714001SSerapheim Dimitropoulos libzfs_handle_t *hdl = zhp->zpool_hdl; 1291*86714001SSerapheim Dimitropoulos char msg[1024]; 1292*86714001SSerapheim Dimitropoulos int error; 1293*86714001SSerapheim Dimitropoulos 1294*86714001SSerapheim Dimitropoulos error = lzc_pool_checkpoint(zhp->zpool_name); 1295*86714001SSerapheim Dimitropoulos if (error != 0) { 1296*86714001SSerapheim Dimitropoulos (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1297*86714001SSerapheim Dimitropoulos "cannot checkpoint '%s'"), zhp->zpool_name); 1298*86714001SSerapheim Dimitropoulos (void) zpool_standard_error(hdl, error, msg); 1299*86714001SSerapheim Dimitropoulos return (-1); 1300*86714001SSerapheim Dimitropoulos } 1301*86714001SSerapheim Dimitropoulos 1302*86714001SSerapheim Dimitropoulos return (0); 1303*86714001SSerapheim Dimitropoulos } 1304*86714001SSerapheim Dimitropoulos 1305*86714001SSerapheim Dimitropoulos /* 1306*86714001SSerapheim Dimitropoulos * Discard the checkpoint from the given pool. 1307*86714001SSerapheim Dimitropoulos */ 1308*86714001SSerapheim Dimitropoulos int 1309*86714001SSerapheim Dimitropoulos zpool_discard_checkpoint(zpool_handle_t *zhp) 1310*86714001SSerapheim Dimitropoulos { 1311*86714001SSerapheim Dimitropoulos libzfs_handle_t *hdl = zhp->zpool_hdl; 1312*86714001SSerapheim Dimitropoulos char msg[1024]; 1313*86714001SSerapheim Dimitropoulos int error; 1314*86714001SSerapheim Dimitropoulos 1315*86714001SSerapheim Dimitropoulos error = lzc_pool_checkpoint_discard(zhp->zpool_name); 1316*86714001SSerapheim Dimitropoulos if (error != 0) { 1317*86714001SSerapheim Dimitropoulos (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1318*86714001SSerapheim Dimitropoulos "cannot discard checkpoint in '%s'"), zhp->zpool_name); 1319*86714001SSerapheim Dimitropoulos (void) zpool_standard_error(hdl, error, msg); 1320*86714001SSerapheim Dimitropoulos return (-1); 1321*86714001SSerapheim Dimitropoulos } 1322*86714001SSerapheim Dimitropoulos 1323*86714001SSerapheim Dimitropoulos return (0); 1324*86714001SSerapheim Dimitropoulos } 1325*86714001SSerapheim Dimitropoulos 1326fa9e4066Sahrens /* 1327fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1328fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1329fa9e4066Sahrens */ 1330fa9e4066Sahrens int 1331fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1332fa9e4066Sahrens { 1333e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 133499653d4eSeschrock int ret; 133599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 133699653d4eSeschrock char msg[1024]; 1337fa94a07fSbrendan nvlist_t **spares, **l2cache; 1338fa94a07fSbrendan uint_t nspares, nl2cache; 133999653d4eSeschrock 134099653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 134199653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 134299653d4eSeschrock 1343fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1344fa94a07fSbrendan SPA_VERSION_SPARES && 134599653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 134699653d4eSeschrock &spares, &nspares) == 0) { 134799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 134899653d4eSeschrock "upgraded to add hot spares")); 134999653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 135099653d4eSeschrock } 1351fa9e4066Sahrens 1352fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1353fa94a07fSbrendan SPA_VERSION_L2CACHE && 1354fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1355fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1356fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1357fa94a07fSbrendan "upgraded to add cache devices")); 1358fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1359fa94a07fSbrendan } 1360fa94a07fSbrendan 1361990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 136299653d4eSeschrock return (-1); 1363fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1364fa9e4066Sahrens 1365cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1366fa9e4066Sahrens switch (errno) { 1367fa9e4066Sahrens case EBUSY: 1368fa9e4066Sahrens /* 1369fa9e4066Sahrens * This can happen if the user has specified the same 1370fa9e4066Sahrens * device multiple times. We can't reliably detect this 1371fa9e4066Sahrens * until we try to add it and see we already have a 1372fa9e4066Sahrens * label. 1373fa9e4066Sahrens */ 137499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 137599653d4eSeschrock "one or more vdevs refer to the same device")); 137699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1377fa9e4066Sahrens break; 1378fa9e4066Sahrens 13795cabbc6bSPrashanth Sreenivasa case EINVAL: 13805cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13815cabbc6bSPrashanth Sreenivasa "invalid config; a pool with removing/removed " 13825cabbc6bSPrashanth Sreenivasa "vdevs does not support adding raidz vdevs")); 13835cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_BADDEV, msg); 13845cabbc6bSPrashanth Sreenivasa break; 13855cabbc6bSPrashanth Sreenivasa 1386fa9e4066Sahrens case EOVERFLOW: 1387fa9e4066Sahrens /* 1388fa9e4066Sahrens * This occurrs when one of the devices is below 1389fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1390fa9e4066Sahrens * device was the problem device since there's no 1391fa9e4066Sahrens * reliable way to determine device size from userland. 1392fa9e4066Sahrens */ 1393fa9e4066Sahrens { 1394fa9e4066Sahrens char buf[64]; 1395fa9e4066Sahrens 1396fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1397fa9e4066Sahrens 139899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 139999653d4eSeschrock "device is less than the minimum " 140099653d4eSeschrock "size (%s)"), buf); 1401fa9e4066Sahrens } 140299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 140399653d4eSeschrock break; 140499653d4eSeschrock 140599653d4eSeschrock case ENOTSUP: 140699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14078654d025Sperrin "pool must be upgraded to add these vdevs")); 140899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1409fa9e4066Sahrens break; 1410fa9e4066Sahrens 1411b1b8ab34Slling case EDOM: 1412b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14138654d025Sperrin "root pool can not have multiple vdevs" 14148654d025Sperrin " or separate logs")); 1415b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1416b1b8ab34Slling break; 1417b1b8ab34Slling 1418fa94a07fSbrendan case ENOTBLK: 1419fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1420fa94a07fSbrendan "cache device must be a disk or disk slice")); 1421fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1422fa94a07fSbrendan break; 1423fa94a07fSbrendan 1424fa9e4066Sahrens default: 142599653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1426fa9e4066Sahrens } 1427fa9e4066Sahrens 142899653d4eSeschrock ret = -1; 142999653d4eSeschrock } else { 143099653d4eSeschrock ret = 0; 1431fa9e4066Sahrens } 1432fa9e4066Sahrens 1433e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1434fa9e4066Sahrens 143599653d4eSeschrock return (ret); 1436fa9e4066Sahrens } 1437fa9e4066Sahrens 1438fa9e4066Sahrens /* 1439fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1440fa9e4066Sahrens * mounted datasets in the pool. 1441fa9e4066Sahrens */ 14424445fffbSMatthew Ahrens static int 14434445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce, 14444445fffbSMatthew Ahrens const char *log_str) 1445fa9e4066Sahrens { 1446fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 144789a89ebfSlling char msg[1024]; 1448fa9e4066Sahrens 144989a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 145089a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 145189a89ebfSlling 1452fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 145389a89ebfSlling zc.zc_cookie = force; 1454394ab0cbSGeorge Wilson zc.zc_guid = hardforce; 14554445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 145689a89ebfSlling 145789a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 145889a89ebfSlling switch (errno) { 145989a89ebfSlling case EXDEV: 146089a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 146189a89ebfSlling "use '-f' to override the following errors:\n" 146289a89ebfSlling "'%s' has an active shared spare which could be" 146389a89ebfSlling " used by other pools once '%s' is exported."), 146489a89ebfSlling zhp->zpool_name, zhp->zpool_name); 146589a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 146689a89ebfSlling msg)); 146789a89ebfSlling default: 146889a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 146989a89ebfSlling msg)); 147089a89ebfSlling } 147189a89ebfSlling } 1472fa9e4066Sahrens 1473fa9e4066Sahrens return (0); 1474fa9e4066Sahrens } 1475fa9e4066Sahrens 1476394ab0cbSGeorge Wilson int 14774445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str) 1478394ab0cbSGeorge Wilson { 14794445fffbSMatthew Ahrens return (zpool_export_common(zhp, force, B_FALSE, log_str)); 1480394ab0cbSGeorge Wilson } 1481394ab0cbSGeorge Wilson 1482394ab0cbSGeorge Wilson int 14834445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str) 1484394ab0cbSGeorge Wilson { 14854445fffbSMatthew Ahrens return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str)); 1486394ab0cbSGeorge Wilson } 1487394ab0cbSGeorge Wilson 1488468c413aSTim Haley static void 1489468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 14904b964adaSGeorge Wilson nvlist_t *config) 1491468c413aSTim Haley { 14924b964adaSGeorge Wilson nvlist_t *nv = NULL; 1493468c413aSTim Haley uint64_t rewindto; 1494468c413aSTim Haley int64_t loss = -1; 1495468c413aSTim Haley struct tm t; 1496468c413aSTim Haley char timestr[128]; 1497468c413aSTim Haley 14984b964adaSGeorge Wilson if (!hdl->libzfs_printerr || config == NULL) 14994b964adaSGeorge Wilson return; 15004b964adaSGeorge Wilson 1501ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1502ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) { 1503468c413aSTim Haley return; 1504ad135b5dSChristopher Siden } 1505468c413aSTim Haley 15064b964adaSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1507468c413aSTim Haley return; 15084b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1509468c413aSTim Haley 1510468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1511468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1512468c413aSTim Haley if (dryrun) { 1513468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1514468c413aSTim Haley "Would be able to return %s " 1515468c413aSTim Haley "to its state as of %s.\n"), 1516468c413aSTim Haley name, timestr); 1517468c413aSTim Haley } else { 1518468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1519468c413aSTim Haley "Pool %s returned to its state as of %s.\n"), 1520468c413aSTim Haley name, timestr); 1521468c413aSTim Haley } 1522468c413aSTim Haley if (loss > 120) { 1523468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1524468c413aSTim Haley "%s approximately %lld "), 1525468c413aSTim Haley dryrun ? "Would discard" : "Discarded", 1526468c413aSTim Haley (loss + 30) / 60); 1527468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1528468c413aSTim Haley "minutes of transactions.\n")); 1529468c413aSTim Haley } else if (loss > 0) { 1530468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1531468c413aSTim Haley "%s approximately %lld "), 1532468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss); 1533468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1534468c413aSTim Haley "seconds of transactions.\n")); 1535468c413aSTim Haley } 1536468c413aSTim Haley } 1537468c413aSTim Haley } 1538468c413aSTim Haley 1539468c413aSTim Haley void 1540468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1541468c413aSTim Haley nvlist_t *config) 1542468c413aSTim Haley { 15434b964adaSGeorge Wilson nvlist_t *nv = NULL; 1544468c413aSTim Haley int64_t loss = -1; 1545468c413aSTim Haley uint64_t edata = UINT64_MAX; 1546468c413aSTim Haley uint64_t rewindto; 1547468c413aSTim Haley struct tm t; 1548468c413aSTim Haley char timestr[128]; 1549468c413aSTim Haley 1550468c413aSTim Haley if (!hdl->libzfs_printerr) 1551468c413aSTim Haley return; 1552468c413aSTim Haley 1553468c413aSTim Haley if (reason >= 0) 1554468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1555468c413aSTim Haley else 1556468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1557468c413aSTim Haley 1558468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 15594b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1560ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 || 15614b964adaSGeorge Wilson nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1562468c413aSTim Haley goto no_info; 1563468c413aSTim Haley 15644b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 15654b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1566468c413aSTim Haley &edata); 1567468c413aSTim Haley 1568468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1569468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n")); 1570468c413aSTim Haley 1571468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1572468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1573468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1574468c413aSTim Haley "\tReturning the pool to its state as of %s\n" 1575468c413aSTim Haley "\tshould correct the problem. "), 1576468c413aSTim Haley timestr); 1577468c413aSTim Haley } else { 1578468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1579468c413aSTim Haley "\tReverting the pool to an earlier state " 1580468c413aSTim Haley "should correct the problem.\n\t")); 1581468c413aSTim Haley } 1582468c413aSTim Haley 1583468c413aSTim Haley if (loss > 120) { 1584468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1585468c413aSTim Haley "Approximately %lld minutes of data\n" 1586468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1587468c413aSTim Haley } else if (loss > 0) { 1588468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1589468c413aSTim Haley "Approximately %lld seconds of data\n" 1590468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss); 1591468c413aSTim Haley } 1592468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) { 1593468c413aSTim Haley if (edata == 1) { 1594468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1595468c413aSTim Haley "After rewind, at least\n" 1596468c413aSTim Haley "\tone persistent user-data error will remain. ")); 1597468c413aSTim Haley } else { 1598468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1599468c413aSTim Haley "After rewind, several\n" 1600468c413aSTim Haley "\tpersistent user-data errors will remain. ")); 1601468c413aSTim Haley } 1602468c413aSTim Haley } 1603468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1604a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1605a33cae98STim Haley reason >= 0 ? "clear" : "import", name); 1606468c413aSTim Haley 1607468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1608468c413aSTim Haley "A scrub of the pool\n" 1609468c413aSTim Haley "\tis strongly recommended after recovery.\n")); 1610468c413aSTim Haley return; 1611468c413aSTim Haley 1612468c413aSTim Haley no_info: 1613468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1614468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n")); 1615468c413aSTim Haley } 1616468c413aSTim Haley 1617fa9e4066Sahrens /* 1618990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1619990b4856Slling * if possible. 1620990b4856Slling * 1621990b4856Slling * Applications should use zpool_import_props() to import a pool with 1622990b4856Slling * new properties value to be set. 1623fa9e4066Sahrens */ 1624fa9e4066Sahrens int 162599653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1626990b4856Slling char *altroot) 1627990b4856Slling { 1628990b4856Slling nvlist_t *props = NULL; 1629990b4856Slling int ret; 1630990b4856Slling 1631990b4856Slling if (altroot != NULL) { 1632990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1633990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1634990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1635990b4856Slling newname)); 1636990b4856Slling } 1637990b4856Slling 1638990b4856Slling if (nvlist_add_string(props, 1639352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1640352d8027SGeorge Wilson nvlist_add_string(props, 1641352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1642990b4856Slling nvlist_free(props); 1643990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1644990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1645990b4856Slling newname)); 1646990b4856Slling } 1647990b4856Slling } 1648990b4856Slling 16494b964adaSGeorge Wilson ret = zpool_import_props(hdl, config, newname, props, 16504b964adaSGeorge Wilson ZFS_IMPORT_NORMAL); 1651aab83bb8SJosef 'Jeff' Sipek nvlist_free(props); 1652990b4856Slling return (ret); 1653990b4856Slling } 1654990b4856Slling 16554b964adaSGeorge Wilson static void 16564b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 16574b964adaSGeorge Wilson int indent) 16584b964adaSGeorge Wilson { 16594b964adaSGeorge Wilson nvlist_t **child; 16604b964adaSGeorge Wilson uint_t c, children; 16614b964adaSGeorge Wilson char *vname; 16624b964adaSGeorge Wilson uint64_t is_log = 0; 16634b964adaSGeorge Wilson 16644b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 16654b964adaSGeorge Wilson &is_log); 16664b964adaSGeorge Wilson 16674b964adaSGeorge Wilson if (name != NULL) 16684b964adaSGeorge Wilson (void) printf("\t%*s%s%s\n", indent, "", name, 16694b964adaSGeorge Wilson is_log ? " [log]" : ""); 16704b964adaSGeorge Wilson 16714b964adaSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 16724b964adaSGeorge Wilson &child, &children) != 0) 16734b964adaSGeorge Wilson return; 16744b964adaSGeorge Wilson 16754b964adaSGeorge Wilson for (c = 0; c < children; c++) { 16764b964adaSGeorge Wilson vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 16774b964adaSGeorge Wilson print_vdev_tree(hdl, vname, child[c], indent + 2); 16784b964adaSGeorge Wilson free(vname); 16794b964adaSGeorge Wilson } 16804b964adaSGeorge Wilson } 16814b964adaSGeorge Wilson 1682ad135b5dSChristopher Siden void 1683ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config) 1684ad135b5dSChristopher Siden { 1685ad135b5dSChristopher Siden nvlist_t *nvinfo, *unsup_feat; 1686ad135b5dSChristopher Siden 1687ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 1688ad135b5dSChristopher Siden 0); 1689ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT, 1690ad135b5dSChristopher Siden &unsup_feat) == 0); 1691ad135b5dSChristopher Siden 1692ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL; 1693ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(unsup_feat, nvp)) { 1694ad135b5dSChristopher Siden char *desc; 1695ad135b5dSChristopher Siden 1696ad135b5dSChristopher Siden verify(nvpair_type(nvp) == DATA_TYPE_STRING); 1697ad135b5dSChristopher Siden verify(nvpair_value_string(nvp, &desc) == 0); 1698ad135b5dSChristopher Siden 1699ad135b5dSChristopher Siden if (strlen(desc) > 0) 1700ad135b5dSChristopher Siden (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc); 1701ad135b5dSChristopher Siden else 1702ad135b5dSChristopher Siden (void) printf("\t%s\n", nvpair_name(nvp)); 1703ad135b5dSChristopher Siden } 1704ad135b5dSChristopher Siden } 1705ad135b5dSChristopher Siden 1706990b4856Slling /* 1707990b4856Slling * Import the given pool using the known configuration and a list of 1708990b4856Slling * properties to be set. The configuration should have come from 1709990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1710990b4856Slling * is imported with a different name. 1711990b4856Slling */ 1712990b4856Slling int 1713990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 17144b964adaSGeorge Wilson nvlist_t *props, int flags) 1715fa9e4066Sahrens { 1716e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1717468c413aSTim Haley zpool_rewind_policy_t policy; 17184b964adaSGeorge Wilson nvlist_t *nv = NULL; 17194b964adaSGeorge Wilson nvlist_t *nvinfo = NULL; 17204b964adaSGeorge Wilson nvlist_t *missing = NULL; 1721fa9e4066Sahrens char *thename; 1722fa9e4066Sahrens char *origname; 1723fa9e4066Sahrens int ret; 17244b964adaSGeorge Wilson int error = 0; 1725990b4856Slling char errbuf[1024]; 1726fa9e4066Sahrens 1727fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1728fa9e4066Sahrens &origname) == 0); 1729fa9e4066Sahrens 1730990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1731990b4856Slling "cannot import pool '%s'"), origname); 1732990b4856Slling 1733fa9e4066Sahrens if (newname != NULL) { 173499653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1735ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 173699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 173799653d4eSeschrock newname)); 1738fa9e4066Sahrens thename = (char *)newname; 1739fa9e4066Sahrens } else { 1740fa9e4066Sahrens thename = origname; 1741fa9e4066Sahrens } 1742fa9e4066Sahrens 1743078266a5SMarcel Telka if (props != NULL) { 1744990b4856Slling uint64_t version; 1745f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 1746fa9e4066Sahrens 1747990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1748990b4856Slling &version) == 0); 1749fa9e4066Sahrens 17500a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1751078266a5SMarcel Telka props, version, flags, errbuf)) == NULL) 1752990b4856Slling return (-1); 1753078266a5SMarcel Telka if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1754351420b3Slling nvlist_free(props); 1755990b4856Slling return (-1); 1756351420b3Slling } 1757078266a5SMarcel Telka nvlist_free(props); 1758990b4856Slling } 1759990b4856Slling 1760990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1761fa9e4066Sahrens 1762fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1763ea8dc4b6Seschrock &zc.zc_guid) == 0); 1764fa9e4066Sahrens 1765351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1766078266a5SMarcel Telka zcmd_free_nvlists(&zc); 176799653d4eSeschrock return (-1); 1768351420b3Slling } 176957f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1770078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1771468c413aSTim Haley return (-1); 1772468c413aSTim Haley } 1773fa9e4066Sahrens 17744b964adaSGeorge Wilson zc.zc_cookie = flags; 17754b964adaSGeorge Wilson while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 17764b964adaSGeorge Wilson errno == ENOMEM) { 17774b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 17784b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 17794b964adaSGeorge Wilson return (-1); 17804b964adaSGeorge Wilson } 17814b964adaSGeorge Wilson } 17824b964adaSGeorge Wilson if (ret != 0) 17834b964adaSGeorge Wilson error = errno; 17844b964adaSGeorge Wilson 17854b964adaSGeorge Wilson (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1786078266a5SMarcel Telka 1787078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1788078266a5SMarcel Telka 17894b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 17904b964adaSGeorge Wilson 17914b964adaSGeorge Wilson if (error) { 1792fa9e4066Sahrens char desc[1024]; 1793468c413aSTim Haley 1794468c413aSTim Haley /* 1795468c413aSTim Haley * Dry-run failed, but we print out what success 1796468c413aSTim Haley * looks like if we found a best txg 1797468c413aSTim Haley */ 17984b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_TRY_REWIND) { 1799468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 18004b964adaSGeorge Wilson B_TRUE, nv); 18014b964adaSGeorge Wilson nvlist_free(nv); 1802468c413aSTim Haley return (-1); 1803468c413aSTim Haley } 1804468c413aSTim Haley 1805fa9e4066Sahrens if (newname == NULL) 1806fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1807fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1808fa9e4066Sahrens thename); 1809fa9e4066Sahrens else 1810fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1811fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1812fa9e4066Sahrens origname, thename); 1813fa9e4066Sahrens 18144b964adaSGeorge Wilson switch (error) { 1815ea8dc4b6Seschrock case ENOTSUP: 1816ad135b5dSChristopher Siden if (nv != NULL && nvlist_lookup_nvlist(nv, 1817ad135b5dSChristopher Siden ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1818ad135b5dSChristopher Siden nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) { 1819ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, "This " 1820ad135b5dSChristopher Siden "pool uses the following feature(s) not " 1821ad135b5dSChristopher Siden "supported by this system:\n")); 1822ad135b5dSChristopher Siden zpool_print_unsup_feat(nv); 1823ad135b5dSChristopher Siden if (nvlist_exists(nvinfo, 1824ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY)) { 1825ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, 1826ad135b5dSChristopher Siden "All unsupported features are only " 1827ad135b5dSChristopher Siden "required for writing to the pool." 1828ad135b5dSChristopher Siden "\nThe pool can be imported using " 1829ad135b5dSChristopher Siden "'-o readonly=on'.\n")); 1830ad135b5dSChristopher Siden } 1831ad135b5dSChristopher Siden } 1832ea8dc4b6Seschrock /* 1833ea8dc4b6Seschrock * Unsupported version. 1834ea8dc4b6Seschrock */ 183599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1836ea8dc4b6Seschrock break; 1837ea8dc4b6Seschrock 1838b5989ec7Seschrock case EINVAL: 1839b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1840b5989ec7Seschrock break; 1841b5989ec7Seschrock 184254a91118SChris Kirby case EROFS: 184354a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 184454a91118SChris Kirby "one or more devices is read only")); 184554a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc); 184654a91118SChris Kirby break; 184754a91118SChris Kirby 18484b964adaSGeorge Wilson case ENXIO: 18494b964adaSGeorge Wilson if (nv && nvlist_lookup_nvlist(nv, 18504b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 18514b964adaSGeorge Wilson nvlist_lookup_nvlist(nvinfo, 18524b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 18534b964adaSGeorge Wilson (void) printf(dgettext(TEXT_DOMAIN, 18546f793812SPavel Zakharov "The devices below are missing or " 18556f793812SPavel Zakharov "corrupted, use '-m' to import the pool " 18566f793812SPavel Zakharov "anyway:\n")); 18574b964adaSGeorge Wilson print_vdev_tree(hdl, NULL, missing, 2); 18584b964adaSGeorge Wilson (void) printf("\n"); 18594b964adaSGeorge Wilson } 18604b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 18614b964adaSGeorge Wilson break; 18624b964adaSGeorge Wilson 18634b964adaSGeorge Wilson case EEXIST: 18644b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 18654b964adaSGeorge Wilson break; 1866c971037bSPaul Dagnelie case ENAMETOOLONG: 1867c971037bSPaul Dagnelie zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1868c971037bSPaul Dagnelie "new name of at least one dataset is longer than " 1869c971037bSPaul Dagnelie "the maximum allowable length")); 1870c971037bSPaul Dagnelie (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc); 1871c971037bSPaul Dagnelie break; 1872fa9e4066Sahrens default: 18734b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 1874468c413aSTim Haley zpool_explain_recover(hdl, 18754b964adaSGeorge Wilson newname ? origname : thename, -error, nv); 1876468c413aSTim Haley break; 1877fa9e4066Sahrens } 1878fa9e4066Sahrens 18794b964adaSGeorge Wilson nvlist_free(nv); 1880fa9e4066Sahrens ret = -1; 1881fa9e4066Sahrens } else { 1882fa9e4066Sahrens zpool_handle_t *zhp; 1883ecd6cf80Smarks 1884fa9e4066Sahrens /* 1885fa9e4066Sahrens * This should never fail, but play it safe anyway. 1886fa9e4066Sahrens */ 1887681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0) 188894de1d4cSeschrock ret = -1; 1889681d9761SEric Taylor else if (zhp != NULL) 1890fa9e4066Sahrens zpool_close(zhp); 1891468c413aSTim Haley if (policy.zrp_request & 1892468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1893468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 18944b964adaSGeorge Wilson ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 1895468c413aSTim Haley } 18964b964adaSGeorge Wilson nvlist_free(nv); 1897468c413aSTim Haley return (0); 1898fa9e4066Sahrens } 1899fa9e4066Sahrens 1900fa9e4066Sahrens return (ret); 1901fa9e4066Sahrens } 1902fa9e4066Sahrens 1903fa9e4066Sahrens /* 19043f9d6ad7SLin Ling * Scan the pool. 1905fa9e4066Sahrens */ 1906fa9e4066Sahrens int 19071702cce7SAlek Pinchuk zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd) 1908fa9e4066Sahrens { 1909fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1910fa9e4066Sahrens char msg[1024]; 19111702cce7SAlek Pinchuk int err; 191299653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1913fa9e4066Sahrens 1914fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 19153f9d6ad7SLin Ling zc.zc_cookie = func; 19161702cce7SAlek Pinchuk zc.zc_flags = cmd; 1917fa9e4066Sahrens 19181702cce7SAlek Pinchuk if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0) 19191702cce7SAlek Pinchuk return (0); 19201702cce7SAlek Pinchuk 19211702cce7SAlek Pinchuk err = errno; 19221702cce7SAlek Pinchuk 19231702cce7SAlek Pinchuk /* ECANCELED on a scrub means we resumed a paused scrub */ 19241702cce7SAlek Pinchuk if (err == ECANCELED && func == POOL_SCAN_SCRUB && 19251702cce7SAlek Pinchuk cmd == POOL_SCRUB_NORMAL) 19261702cce7SAlek Pinchuk return (0); 19271702cce7SAlek Pinchuk 19281702cce7SAlek Pinchuk if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL) 1929fa9e4066Sahrens return (0); 1930fa9e4066Sahrens 19313f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) { 19321702cce7SAlek Pinchuk if (cmd == POOL_SCRUB_PAUSE) { 19331702cce7SAlek Pinchuk (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 19341702cce7SAlek Pinchuk "cannot pause scrubbing %s"), zc.zc_name); 19351702cce7SAlek Pinchuk } else { 19361702cce7SAlek Pinchuk assert(cmd == POOL_SCRUB_NORMAL); 19371702cce7SAlek Pinchuk (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 19381702cce7SAlek Pinchuk "cannot scrub %s"), zc.zc_name); 19391702cce7SAlek Pinchuk } 19403f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) { 19413f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 19423f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 19433f9d6ad7SLin Ling zc.zc_name); 19443f9d6ad7SLin Ling } else { 19453f9d6ad7SLin Ling assert(!"unexpected result"); 19463f9d6ad7SLin Ling } 1947fa9e4066Sahrens 19481702cce7SAlek Pinchuk if (err == EBUSY) { 19493f9d6ad7SLin Ling nvlist_t *nvroot; 19503f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 19513f9d6ad7SLin Ling uint_t psc; 19523f9d6ad7SLin Ling 19533f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config, 19543f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 19553f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 19563f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 19571702cce7SAlek Pinchuk if (ps && ps->pss_func == POOL_SCAN_SCRUB) { 19581702cce7SAlek Pinchuk if (cmd == POOL_SCRUB_PAUSE) 19591702cce7SAlek Pinchuk return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg)); 19601702cce7SAlek Pinchuk else 19611702cce7SAlek Pinchuk return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 19621702cce7SAlek Pinchuk } else { 19633f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg)); 19641702cce7SAlek Pinchuk } 19651702cce7SAlek Pinchuk } else if (err == ENOENT) { 19663f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 19673f9d6ad7SLin Ling } else { 19681702cce7SAlek Pinchuk return (zpool_standard_error(hdl, err, msg)); 19693f9d6ad7SLin Ling } 1970fa9e4066Sahrens } 1971fa9e4066Sahrens 19723fdda499SJohn Harres /* 19733fdda499SJohn Harres * This provides a very minimal check whether a given string is likely a 19743fdda499SJohn Harres * c#t#d# style string. Users of this are expected to do their own 19753fdda499SJohn Harres * verification of the s# part. 19763fdda499SJohn Harres */ 19773fdda499SJohn Harres #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 19783fdda499SJohn Harres 19793fdda499SJohn Harres /* 19803fdda499SJohn Harres * More elaborate version for ones which may start with "/dev/dsk/" 19813fdda499SJohn Harres * and the like. 19823fdda499SJohn Harres */ 19833fdda499SJohn Harres static int 19849a686fbcSPaul Dagnelie ctd_check_path(char *str) 19859a686fbcSPaul Dagnelie { 19863fdda499SJohn Harres /* 19873fdda499SJohn Harres * If it starts with a slash, check the last component. 19883fdda499SJohn Harres */ 19893fdda499SJohn Harres if (str && str[0] == '/') { 19903fdda499SJohn Harres char *tmp = strrchr(str, '/'); 19913fdda499SJohn Harres 19923fdda499SJohn Harres /* 19933fdda499SJohn Harres * If it ends in "/old", check the second-to-last 19943fdda499SJohn Harres * component of the string instead. 19953fdda499SJohn Harres */ 19963fdda499SJohn Harres if (tmp != str && strcmp(tmp, "/old") == 0) { 19973fdda499SJohn Harres for (tmp--; *tmp != '/'; tmp--) 19983fdda499SJohn Harres ; 19993fdda499SJohn Harres } 20003fdda499SJohn Harres str = tmp + 1; 20013fdda499SJohn Harres } 20023fdda499SJohn Harres return (CTD_CHECK(str)); 20033fdda499SJohn Harres } 20043fdda499SJohn Harres 2005a43d325bSek /* 2006573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the 2007573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device. 2008a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 2009a43d325bSek * spare; but FALSE if its an INUSE spare. 2010a43d325bSek */ 201199653d4eSeschrock static nvlist_t * 2012573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 2013573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log) 2014ea8dc4b6Seschrock { 2015ea8dc4b6Seschrock uint_t c, children; 2016ea8dc4b6Seschrock nvlist_t **child; 201799653d4eSeschrock nvlist_t *ret; 2018ee0eb9f2SEric Schrock uint64_t is_log; 2019573ca77eSGeorge Wilson char *srchkey; 2020573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL); 2021573ca77eSGeorge Wilson 2022573ca77eSGeorge Wilson /* Nothing to look for */ 2023573ca77eSGeorge Wilson if (search == NULL || pair == NULL) 2024573ca77eSGeorge Wilson return (NULL); 2025ea8dc4b6Seschrock 2026573ca77eSGeorge Wilson /* Obtain the key we will use to search */ 2027573ca77eSGeorge Wilson srchkey = nvpair_name(pair); 2028573ca77eSGeorge Wilson 2029573ca77eSGeorge Wilson switch (nvpair_type(pair)) { 2030cb04b873SMark J Musante case DATA_TYPE_UINT64: 2031573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 2032cb04b873SMark J Musante uint64_t srchval, theguid; 2033cb04b873SMark J Musante 2034cb04b873SMark J Musante verify(nvpair_value_uint64(pair, &srchval) == 0); 2035cb04b873SMark J Musante verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2036cb04b873SMark J Musante &theguid) == 0); 2037cb04b873SMark J Musante if (theguid == srchval) 2038cb04b873SMark J Musante return (nv); 2039573ca77eSGeorge Wilson } 2040573ca77eSGeorge Wilson break; 2041573ca77eSGeorge Wilson 2042573ca77eSGeorge Wilson case DATA_TYPE_STRING: { 2043573ca77eSGeorge Wilson char *srchval, *val; 2044573ca77eSGeorge Wilson 2045573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0); 2046573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0) 2047573ca77eSGeorge Wilson break; 2048ea8dc4b6Seschrock 2049ea8dc4b6Seschrock /* 20503fdda499SJohn Harres * Search for the requested value. Special cases: 20513fdda499SJohn Harres * 20527855d95bSToomas Soome * - ZPOOL_CONFIG_PATH for whole disk entries. To support 20537855d95bSToomas Soome * UEFI boot, these end in "s0" or "s0/old" or "s1" or 20547855d95bSToomas Soome * "s1/old". The "s0" or "s1" part is hidden from the user, 20553fdda499SJohn Harres * but included in the string, so this matches around it. 20563fdda499SJohn Harres * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 20573fdda499SJohn Harres * 205888ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares. 2059ea8dc4b6Seschrock */ 20603fdda499SJohn Harres if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 20613fdda499SJohn Harres ctd_check_path(val)) { 2062573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2063573ca77eSGeorge Wilson 2064573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2065573ca77eSGeorge Wilson &wholedisk); 2066573ca77eSGeorge Wilson if (wholedisk) { 20673fdda499SJohn Harres int slen = strlen(srchval); 20683fdda499SJohn Harres int vlen = strlen(val); 20693fdda499SJohn Harres 20703fdda499SJohn Harres if (slen != vlen - 2) 20713fdda499SJohn Harres break; 20723fdda499SJohn Harres 20733fdda499SJohn Harres /* 20743fdda499SJohn Harres * make_leaf_vdev() should only set 20753fdda499SJohn Harres * wholedisk for ZPOOL_CONFIG_PATHs which 20763fdda499SJohn Harres * will include "/dev/dsk/", giving plenty of 20773fdda499SJohn Harres * room for the indices used next. 20783fdda499SJohn Harres */ 20793fdda499SJohn Harres ASSERT(vlen >= 6); 20803fdda499SJohn Harres 20813fdda499SJohn Harres /* 20823fdda499SJohn Harres * strings identical except trailing "s0" 20833fdda499SJohn Harres */ 20847855d95bSToomas Soome if ((strcmp(&val[vlen - 2], "s0") == 0 || 20857855d95bSToomas Soome strcmp(&val[vlen - 2], "s1") == 0) && 20863fdda499SJohn Harres strncmp(srchval, val, slen) == 0) 20873fdda499SJohn Harres return (nv); 20883fdda499SJohn Harres 2089573ca77eSGeorge Wilson /* 20903fdda499SJohn Harres * strings identical except trailing "s0/old" 2091573ca77eSGeorge Wilson */ 20927855d95bSToomas Soome if ((strcmp(&val[vlen - 6], "s0/old") == 0 || 20937855d95bSToomas Soome strcmp(&val[vlen - 6], "s1/old") == 0) && 20943fdda499SJohn Harres strcmp(&srchval[slen - 4], "/old") == 0 && 20953fdda499SJohn Harres strncmp(srchval, val, slen - 4) == 0) 2096573ca77eSGeorge Wilson return (nv); 20973fdda499SJohn Harres 2098573ca77eSGeorge Wilson break; 2099573ca77eSGeorge Wilson } 210088ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 210188ecc943SGeorge Wilson char *type, *idx, *end, *p; 210288ecc943SGeorge Wilson uint64_t id, vdev_id; 210388ecc943SGeorge Wilson 210488ecc943SGeorge Wilson /* 210588ecc943SGeorge Wilson * Determine our vdev type, keeping in mind 210688ecc943SGeorge Wilson * that the srchval is composed of a type and 210788ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4). 210888ecc943SGeorge Wilson */ 210988ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL) 211088ecc943SGeorge Wilson return (NULL); 211188ecc943SGeorge Wilson 211288ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) { 211388ecc943SGeorge Wilson free(type); 211488ecc943SGeorge Wilson break; 211588ecc943SGeorge Wilson } 211688ecc943SGeorge Wilson idx = p + 1; 211788ecc943SGeorge Wilson *p = '\0'; 211888ecc943SGeorge Wilson 211988ecc943SGeorge Wilson /* 212088ecc943SGeorge Wilson * If the types don't match then keep looking. 212188ecc943SGeorge Wilson */ 212288ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) { 212388ecc943SGeorge Wilson free(type); 212488ecc943SGeorge Wilson break; 212588ecc943SGeorge Wilson } 212688ecc943SGeorge Wilson 21272ba5f978SAlan Somers verify(zpool_vdev_is_interior(type)); 212888ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 212988ecc943SGeorge Wilson &id) == 0); 213088ecc943SGeorge Wilson 213188ecc943SGeorge Wilson errno = 0; 213288ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10); 213388ecc943SGeorge Wilson 213488ecc943SGeorge Wilson free(type); 213588ecc943SGeorge Wilson if (errno != 0) 213688ecc943SGeorge Wilson return (NULL); 213788ecc943SGeorge Wilson 213888ecc943SGeorge Wilson /* 213988ecc943SGeorge Wilson * Now verify that we have the correct vdev id. 214088ecc943SGeorge Wilson */ 214188ecc943SGeorge Wilson if (vdev_id == id) 214288ecc943SGeorge Wilson return (nv); 2143ea8dc4b6Seschrock } 2144573ca77eSGeorge Wilson 2145573ca77eSGeorge Wilson /* 2146573ca77eSGeorge Wilson * Common case 2147573ca77eSGeorge Wilson */ 2148573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0) 2149573ca77eSGeorge Wilson return (nv); 2150573ca77eSGeorge Wilson break; 2151573ca77eSGeorge Wilson } 2152573ca77eSGeorge Wilson 2153573ca77eSGeorge Wilson default: 2154573ca77eSGeorge Wilson break; 2155ea8dc4b6Seschrock } 2156ea8dc4b6Seschrock 2157ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2158ea8dc4b6Seschrock &child, &children) != 0) 215999653d4eSeschrock return (NULL); 2160ea8dc4b6Seschrock 2161ee0eb9f2SEric Schrock for (c = 0; c < children; c++) { 2162573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2163ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2164ee0eb9f2SEric Schrock /* 2165ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel 2166ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the 2167ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where 2168ee0eb9f2SEric Schrock * 'log' is non-NULL). 2169ee0eb9f2SEric Schrock */ 2170ee0eb9f2SEric Schrock if (log != NULL && 2171ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c], 2172ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 2173ee0eb9f2SEric Schrock is_log) { 2174ee0eb9f2SEric Schrock *log = B_TRUE; 2175ee0eb9f2SEric Schrock } 2176ea8dc4b6Seschrock return (ret); 2177ee0eb9f2SEric Schrock } 2178ee0eb9f2SEric Schrock } 2179ea8dc4b6Seschrock 218099653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 218199653d4eSeschrock &child, &children) == 0) { 218299653d4eSeschrock for (c = 0; c < children; c++) { 2183573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2184ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2185a43d325bSek *avail_spare = B_TRUE; 218699653d4eSeschrock return (ret); 218799653d4eSeschrock } 218899653d4eSeschrock } 218999653d4eSeschrock } 219099653d4eSeschrock 2191fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2192fa94a07fSbrendan &child, &children) == 0) { 2193fa94a07fSbrendan for (c = 0; c < children; c++) { 2194573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2195ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2196fa94a07fSbrendan *l2cache = B_TRUE; 2197fa94a07fSbrendan return (ret); 2198fa94a07fSbrendan } 2199fa94a07fSbrendan } 2200fa94a07fSbrendan } 2201fa94a07fSbrendan 220299653d4eSeschrock return (NULL); 2203ea8dc4b6Seschrock } 2204ea8dc4b6Seschrock 2205573ca77eSGeorge Wilson /* 2206573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the 2207573ca77eSGeorge Wilson * associated vdev. 2208573ca77eSGeorge Wilson */ 2209573ca77eSGeorge Wilson nvlist_t * 2210573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 2211573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 2212573ca77eSGeorge Wilson { 2213573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret; 2214573ca77eSGeorge Wilson 2215573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2216573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 2217573ca77eSGeorge Wilson 2218573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2219573ca77eSGeorge Wilson &nvroot) == 0); 2220573ca77eSGeorge Wilson 2221573ca77eSGeorge Wilson *avail_spare = B_FALSE; 2222cb04b873SMark J Musante *l2cache = B_FALSE; 2223daeb70e5SMark J Musante if (log != NULL) 2224daeb70e5SMark J Musante *log = B_FALSE; 2225573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2226573ca77eSGeorge Wilson nvlist_free(search); 2227573ca77eSGeorge Wilson 2228573ca77eSGeorge Wilson return (ret); 2229573ca77eSGeorge Wilson } 2230573ca77eSGeorge Wilson 223188ecc943SGeorge Wilson /* 223288ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 223388ecc943SGeorge Wilson */ 22342ba5f978SAlan Somers static boolean_t 223588ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name) 223688ecc943SGeorge Wilson { 223788ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 22382ba5f978SAlan Somers strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 || 22392ba5f978SAlan Somers strncmp(name, 22402ba5f978SAlan Somers VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 || 224188ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 224288ecc943SGeorge Wilson return (B_TRUE); 224388ecc943SGeorge Wilson return (B_FALSE); 224488ecc943SGeorge Wilson } 224588ecc943SGeorge Wilson 224699653d4eSeschrock nvlist_t * 2247fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 2248ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log) 2249ea8dc4b6Seschrock { 2250ea8dc4b6Seschrock char buf[MAXPATHLEN]; 2251ea8dc4b6Seschrock char *end; 2252573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret; 2253ea8dc4b6Seschrock uint64_t guid; 2254ea8dc4b6Seschrock 2255573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2256573ca77eSGeorge Wilson 22570917b783Seschrock guid = strtoull(path, &end, 10); 2258ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 2259573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 226088ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) { 226188ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 2262ea8dc4b6Seschrock } else if (path[0] != '/') { 22636401734dSWill Andrews (void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT, 22646401734dSWill Andrews path); 2265573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 2266ea8dc4b6Seschrock } else { 2267573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 2268ea8dc4b6Seschrock } 2269ea8dc4b6Seschrock 2270ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2271ea8dc4b6Seschrock &nvroot) == 0); 2272ea8dc4b6Seschrock 2273a43d325bSek *avail_spare = B_FALSE; 2274fa94a07fSbrendan *l2cache = B_FALSE; 2275ee0eb9f2SEric Schrock if (log != NULL) 2276ee0eb9f2SEric Schrock *log = B_FALSE; 2277573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2278573ca77eSGeorge Wilson nvlist_free(search); 2279573ca77eSGeorge Wilson 2280573ca77eSGeorge Wilson return (ret); 2281a43d325bSek } 2282a43d325bSek 228319397407SSherry Moore static int 228419397407SSherry Moore vdev_online(nvlist_t *nv) 228519397407SSherry Moore { 228619397407SSherry Moore uint64_t ival; 228719397407SSherry Moore 228819397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 228919397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 229019397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 229119397407SSherry Moore return (0); 229219397407SSherry Moore 229319397407SSherry Moore return (1); 229419397407SSherry Moore } 229519397407SSherry Moore 229619397407SSherry Moore /* 229721ecdf64SLin Ling * Helper function for zpool_get_physpaths(). 229819397407SSherry Moore */ 2299753a6d45SSherry Moore static int 230021ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 2301753a6d45SSherry Moore size_t *bytes_written) 230219397407SSherry Moore { 2303753a6d45SSherry Moore size_t bytes_left, pos, rsz; 2304753a6d45SSherry Moore char *tmppath; 2305753a6d45SSherry Moore const char *format; 2306753a6d45SSherry Moore 2307753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 2308753a6d45SSherry Moore &tmppath) != 0) 2309753a6d45SSherry Moore return (EZFS_NODEVICE); 2310753a6d45SSherry Moore 2311753a6d45SSherry Moore pos = *bytes_written; 2312753a6d45SSherry Moore bytes_left = physpath_size - pos; 2313753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s"; 2314753a6d45SSherry Moore 2315753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 2316753a6d45SSherry Moore *bytes_written += rsz; 2317753a6d45SSherry Moore 2318753a6d45SSherry Moore if (rsz >= bytes_left) { 2319753a6d45SSherry Moore /* if physpath was not copied properly, clear it */ 2320753a6d45SSherry Moore if (bytes_left != 0) { 2321753a6d45SSherry Moore physpath[pos] = 0; 2322753a6d45SSherry Moore } 2323753a6d45SSherry Moore return (EZFS_NOSPC); 2324753a6d45SSherry Moore } 2325753a6d45SSherry Moore return (0); 2326753a6d45SSherry Moore } 2327753a6d45SSherry Moore 232821ecdf64SLin Ling static int 232921ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 233021ecdf64SLin Ling size_t *rsz, boolean_t is_spare) 233121ecdf64SLin Ling { 233221ecdf64SLin Ling char *type; 233321ecdf64SLin Ling int ret; 233421ecdf64SLin Ling 233521ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 233621ecdf64SLin Ling return (EZFS_INVALCONFIG); 233721ecdf64SLin Ling 233821ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) { 233921ecdf64SLin Ling /* 234021ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 234121ecdf64SLin Ling * For a spare vdev, we only want to boot from the active 234221ecdf64SLin Ling * spare device. 234321ecdf64SLin Ling */ 234421ecdf64SLin Ling if (is_spare) { 234521ecdf64SLin Ling uint64_t spare = 0; 234621ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 234721ecdf64SLin Ling &spare); 234821ecdf64SLin Ling if (!spare) 234921ecdf64SLin Ling return (EZFS_INVALCONFIG); 235021ecdf64SLin Ling } 235121ecdf64SLin Ling 235221ecdf64SLin Ling if (vdev_online(nv)) { 235321ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath, 235421ecdf64SLin Ling phypath_size, rsz)) != 0) 235521ecdf64SLin Ling return (ret); 235621ecdf64SLin Ling } 235721ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 2358d5f26ad8SToomas Soome strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 235921ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 || 236021ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 236121ecdf64SLin Ling nvlist_t **child; 236221ecdf64SLin Ling uint_t count; 236321ecdf64SLin Ling int i, ret; 236421ecdf64SLin Ling 236521ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv, 236621ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 236721ecdf64SLin Ling return (EZFS_INVALCONFIG); 236821ecdf64SLin Ling 236921ecdf64SLin Ling for (i = 0; i < count; i++) { 237021ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath, 237121ecdf64SLin Ling phypath_size, rsz, is_spare); 237221ecdf64SLin Ling if (ret == EZFS_NOSPC) 237321ecdf64SLin Ling return (ret); 237421ecdf64SLin Ling } 237521ecdf64SLin Ling } 237621ecdf64SLin Ling 237721ecdf64SLin Ling return (EZFS_POOL_INVALARG); 237821ecdf64SLin Ling } 237921ecdf64SLin Ling 2380753a6d45SSherry Moore /* 2381753a6d45SSherry Moore * Get phys_path for a root pool config. 2382753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2383753a6d45SSherry Moore */ 2384753a6d45SSherry Moore static int 2385753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2386753a6d45SSherry Moore { 2387753a6d45SSherry Moore size_t rsz; 238819397407SSherry Moore nvlist_t *vdev_root; 238919397407SSherry Moore nvlist_t **child; 239019397407SSherry Moore uint_t count; 2391753a6d45SSherry Moore char *type; 2392753a6d45SSherry Moore 2393753a6d45SSherry Moore rsz = 0; 2394753a6d45SSherry Moore 2395753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2396753a6d45SSherry Moore &vdev_root) != 0) 2397753a6d45SSherry Moore return (EZFS_INVALCONFIG); 2398753a6d45SSherry Moore 2399753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2400753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2401753a6d45SSherry Moore &child, &count) != 0) 2402753a6d45SSherry Moore return (EZFS_INVALCONFIG); 240319397407SSherry Moore 240419397407SSherry Moore /* 24051a902ef8SHans Rosenfeld * root pool can only have a single top-level vdev. 240619397407SSherry Moore */ 24071a902ef8SHans Rosenfeld if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1) 2408753a6d45SSherry Moore return (EZFS_POOL_INVALARG); 240919397407SSherry Moore 241021ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 241121ecdf64SLin Ling B_FALSE); 241219397407SSherry Moore 2413753a6d45SSherry Moore /* No online devices */ 2414753a6d45SSherry Moore if (rsz == 0) 2415753a6d45SSherry Moore return (EZFS_NODEVICE); 2416753a6d45SSherry Moore 241719397407SSherry Moore return (0); 241819397407SSherry Moore } 241919397407SSherry Moore 2420753a6d45SSherry Moore /* 2421753a6d45SSherry Moore * Get phys_path for a root pool 2422753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2423753a6d45SSherry Moore */ 2424753a6d45SSherry Moore int 2425753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2426753a6d45SSherry Moore { 2427753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2428753a6d45SSherry Moore phypath_size)); 2429753a6d45SSherry Moore } 2430753a6d45SSherry Moore 2431573ca77eSGeorge Wilson /* 2432573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel 2433573ca77eSGeorge Wilson * the disk to use the new unallocated space. 2434573ca77eSGeorge Wilson */ 2435573ca77eSGeorge Wilson static int 2436573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2437573ca77eSGeorge Wilson { 2438573ca77eSGeorge Wilson char path[MAXPATHLEN]; 2439573ca77eSGeorge Wilson char errbuf[1024]; 2440573ca77eSGeorge Wilson int fd, error; 2441573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int); 2442573ca77eSGeorge Wilson 2443573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2444573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL) 2445573ca77eSGeorge Wilson return (-1); 2446573ca77eSGeorge Wilson 24476401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name); 2448573ca77eSGeorge Wilson 2449573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2450573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2451573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name); 2452573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2453573ca77eSGeorge Wilson } 2454573ca77eSGeorge Wilson 2455573ca77eSGeorge Wilson /* 2456573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device 2457573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply 2458573ca77eSGeorge Wilson * ignore that error and continue on. 2459573ca77eSGeorge Wilson */ 2460573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd); 2461573ca77eSGeorge Wilson (void) close(fd); 2462573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) { 2463573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2464573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name); 2465573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2466573ca77eSGeorge Wilson } 2467573ca77eSGeorge Wilson return (0); 2468573ca77eSGeorge Wilson } 2469573ca77eSGeorge Wilson 2470fa9e4066Sahrens /* 24713d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 24723d7072f8Seschrock * ZFS_ONLINE_* flags. 2473fa9e4066Sahrens */ 2474fa9e4066Sahrens int 24753d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 24763d7072f8Seschrock vdev_state_t *newstate) 2477fa9e4066Sahrens { 2478fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2479fa9e4066Sahrens char msg[1024]; 24809a551dd6SYuri Pankov char *pathname; 248199653d4eSeschrock nvlist_t *tgt; 2482573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog; 248399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2484fa9e4066Sahrens 2485573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) { 2486573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2487573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2488573ca77eSGeorge Wilson } else { 2489573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2490573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2491573ca77eSGeorge Wilson } 2492ea8dc4b6Seschrock 2493fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2494ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2495573ca77eSGeorge Wilson &islog)) == NULL) 249699653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2497fa9e4066Sahrens 249899653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2499fa9e4066Sahrens 2500069f55e2SEric Schrock if (avail_spare) 2501a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2502a43d325bSek 25039a551dd6SYuri Pankov if ((flags & ZFS_ONLINE_EXPAND || 25049a551dd6SYuri Pankov zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) && 25059a551dd6SYuri Pankov nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) { 2506573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2507573ca77eSGeorge Wilson 2508573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2509573ca77eSGeorge Wilson &wholedisk); 2510573ca77eSGeorge Wilson 2511573ca77eSGeorge Wilson /* 2512573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion. 2513573ca77eSGeorge Wilson */ 2514573ca77eSGeorge Wilson if (l2cache) { 2515573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2516573ca77eSGeorge Wilson "cannot expand cache devices")); 2517573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2518573ca77eSGeorge Wilson } 2519573ca77eSGeorge Wilson 2520573ca77eSGeorge Wilson if (wholedisk) { 25216401734dSWill Andrews pathname += strlen(ZFS_DISK_ROOT) + 1; 2522cb04b873SMark J Musante (void) zpool_relabel_disk(hdl, pathname); 2523573ca77eSGeorge Wilson } 2524573ca77eSGeorge Wilson } 2525573ca77eSGeorge Wilson 25263d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 25273d7072f8Seschrock zc.zc_obj = flags; 2528fa9e4066Sahrens 2529cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 25301195e687SMark J Musante if (errno == EINVAL) { 25311195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 25321195e687SMark J Musante "from this pool into a new one. Use '%s' " 25331195e687SMark J Musante "instead"), "zpool detach"); 25341195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 25351195e687SMark J Musante } 25363d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 25371195e687SMark J Musante } 25383d7072f8Seschrock 25393d7072f8Seschrock *newstate = zc.zc_cookie; 25403d7072f8Seschrock return (0); 2541fa9e4066Sahrens } 2542fa9e4066Sahrens 2543fa9e4066Sahrens /* 2544fa9e4066Sahrens * Take the specified vdev offline 2545fa9e4066Sahrens */ 2546fa9e4066Sahrens int 25473d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2548fa9e4066Sahrens { 2549fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2550fa9e4066Sahrens char msg[1024]; 255199653d4eSeschrock nvlist_t *tgt; 2552fa94a07fSbrendan boolean_t avail_spare, l2cache; 255399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2554fa9e4066Sahrens 2555ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2556ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2557ea8dc4b6Seschrock 2558fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2559ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2560ee0eb9f2SEric Schrock NULL)) == NULL) 256199653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 256299653d4eSeschrock 256399653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2564fa9e4066Sahrens 2565069f55e2SEric Schrock if (avail_spare) 2566a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2567a43d325bSek 25683d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 25693d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 25703d7072f8Seschrock 2571cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 25723d7072f8Seschrock return (0); 25733d7072f8Seschrock 25743d7072f8Seschrock switch (errno) { 25753d7072f8Seschrock case EBUSY: 25763d7072f8Seschrock 25773d7072f8Seschrock /* 25783d7072f8Seschrock * There are no other replicas of this device. 25793d7072f8Seschrock */ 25803d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 25813d7072f8Seschrock 2582e6ca193dSGeorge Wilson case EEXIST: 2583e6ca193dSGeorge Wilson /* 2584e6ca193dSGeorge Wilson * The log device has unplayed logs 2585e6ca193dSGeorge Wilson */ 2586e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2587e6ca193dSGeorge Wilson 25883d7072f8Seschrock default: 25893d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 25903d7072f8Seschrock } 25913d7072f8Seschrock } 25923d7072f8Seschrock 25933d7072f8Seschrock /* 25943d7072f8Seschrock * Mark the given vdev faulted. 25953d7072f8Seschrock */ 25963d7072f8Seschrock int 2597069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 25983d7072f8Seschrock { 25993d7072f8Seschrock zfs_cmd_t zc = { 0 }; 26003d7072f8Seschrock char msg[1024]; 26013d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 26023d7072f8Seschrock 26033d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 26043d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2605441d80aaSlling 26063d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 26073d7072f8Seschrock zc.zc_guid = guid; 26083d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 2609069f55e2SEric Schrock zc.zc_obj = aux; 26103d7072f8Seschrock 2611cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2612fa9e4066Sahrens return (0); 2613fa9e4066Sahrens 2614fa9e4066Sahrens switch (errno) { 261599653d4eSeschrock case EBUSY: 2616fa9e4066Sahrens 2617fa9e4066Sahrens /* 2618fa9e4066Sahrens * There are no other replicas of this device. 2619fa9e4066Sahrens */ 262099653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2621fa9e4066Sahrens 262299653d4eSeschrock default: 262399653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2624fa9e4066Sahrens } 26253d7072f8Seschrock 26263d7072f8Seschrock } 26273d7072f8Seschrock 26283d7072f8Seschrock /* 26293d7072f8Seschrock * Mark the given vdev degraded. 26303d7072f8Seschrock */ 26313d7072f8Seschrock int 2632069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 26333d7072f8Seschrock { 26343d7072f8Seschrock zfs_cmd_t zc = { 0 }; 26353d7072f8Seschrock char msg[1024]; 26363d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 26373d7072f8Seschrock 26383d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 26393d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 26403d7072f8Seschrock 26413d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 26423d7072f8Seschrock zc.zc_guid = guid; 26433d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 2644069f55e2SEric Schrock zc.zc_obj = aux; 26453d7072f8Seschrock 2646cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 26473d7072f8Seschrock return (0); 26483d7072f8Seschrock 26493d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 265099653d4eSeschrock } 265199653d4eSeschrock 265299653d4eSeschrock /* 265399653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 265499653d4eSeschrock * a hot spare. 265599653d4eSeschrock */ 265699653d4eSeschrock static boolean_t 265799653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 265899653d4eSeschrock { 265999653d4eSeschrock nvlist_t **child; 266099653d4eSeschrock uint_t c, children; 266199653d4eSeschrock char *type; 266299653d4eSeschrock 266399653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 266499653d4eSeschrock &children) == 0) { 266599653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 266699653d4eSeschrock &type) == 0); 266799653d4eSeschrock 266899653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 266999653d4eSeschrock children == 2 && child[which] == tgt) 267099653d4eSeschrock return (B_TRUE); 267199653d4eSeschrock 267299653d4eSeschrock for (c = 0; c < children; c++) 267399653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 267499653d4eSeschrock return (B_TRUE); 267599653d4eSeschrock } 267699653d4eSeschrock 267799653d4eSeschrock return (B_FALSE); 2678fa9e4066Sahrens } 2679fa9e4066Sahrens 2680fa9e4066Sahrens /* 2681fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 26828654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2683fa9e4066Sahrens */ 2684fa9e4066Sahrens int 2685fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2686fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2687fa9e4066Sahrens { 2688fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2689fa9e4066Sahrens char msg[1024]; 2690fa9e4066Sahrens int ret; 269199653d4eSeschrock nvlist_t *tgt; 2692ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog; 2693ee0eb9f2SEric Schrock uint64_t val; 2694cb04b873SMark J Musante char *newname; 269599653d4eSeschrock nvlist_t **child; 269699653d4eSeschrock uint_t children; 269799653d4eSeschrock nvlist_t *config_root; 269899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 26994263d13fSGeorge Wilson boolean_t rootpool = zpool_is_bootable(zhp); 2700fa9e4066Sahrens 2701ea8dc4b6Seschrock if (replacing) 2702ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2703ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 2704ea8dc4b6Seschrock else 2705ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2706ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 2707ea8dc4b6Seschrock 2708fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2709ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 27105cabbc6bSPrashanth Sreenivasa &islog)) == NULL) 271199653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 271299653d4eSeschrock 2713a43d325bSek if (avail_spare) 271499653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 271599653d4eSeschrock 2716fa94a07fSbrendan if (l2cache) 2717fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2718fa94a07fSbrendan 271999653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2720fa9e4066Sahrens zc.zc_cookie = replacing; 2721fa9e4066Sahrens 272299653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 272399653d4eSeschrock &child, &children) != 0 || children != 1) { 272499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 272599653d4eSeschrock "new device must be a single disk")); 272699653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 272799653d4eSeschrock } 272899653d4eSeschrock 272999653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 273099653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 273199653d4eSeschrock 273288ecc943SGeorge Wilson if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 27330430f8daSeschrock return (-1); 27340430f8daSeschrock 273599653d4eSeschrock /* 273699653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 273799653d4eSeschrock * replace it with another hot spare. 273899653d4eSeschrock */ 273999653d4eSeschrock if (replacing && 274099653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2741ee0eb9f2SEric Schrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2742ee0eb9f2SEric Schrock NULL) == NULL || !avail_spare) && 2743ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) { 274499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 274599653d4eSeschrock "can only be replaced by another hot spare")); 27460430f8daSeschrock free(newname); 274799653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 274899653d4eSeschrock } 274999653d4eSeschrock 27500430f8daSeschrock free(newname); 27510430f8daSeschrock 2752990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 275399653d4eSeschrock return (-1); 2754fa9e4066Sahrens 2755cb04b873SMark J Musante ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2756fa9e4066Sahrens 2757e9dbad6fSeschrock zcmd_free_nvlists(&zc); 2758fa9e4066Sahrens 2759b5b76fecSGeorge Wilson if (ret == 0) { 2760b5b76fecSGeorge Wilson if (rootpool) { 276121ecdf64SLin Ling /* 276221ecdf64SLin Ling * XXX need a better way to prevent user from 276321ecdf64SLin Ling * booting up a half-baked vdev. 276421ecdf64SLin Ling */ 276521ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 276621ecdf64SLin Ling "sure to wait until resilver is done " 276721ecdf64SLin Ling "before rebooting.\n")); 2768b5b76fecSGeorge Wilson } 2769fa9e4066Sahrens return (0); 2770b5b76fecSGeorge Wilson } 2771fa9e4066Sahrens 2772fa9e4066Sahrens switch (errno) { 2773ea8dc4b6Seschrock case ENOTSUP: 2774fa9e4066Sahrens /* 2775fa9e4066Sahrens * Can't attach to or replace this type of vdev. 2776fa9e4066Sahrens */ 27778654d025Sperrin if (replacing) { 2778cb04b873SMark J Musante uint64_t version = zpool_get_prop_int(zhp, 2779cb04b873SMark J Musante ZPOOL_PROP_VERSION, NULL); 2780cb04b873SMark J Musante 2781ee0eb9f2SEric Schrock if (islog) 27828654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27838654d025Sperrin "cannot replace a log with a spare")); 2784cb04b873SMark J Musante else if (version >= SPA_VERSION_MULTI_REPLACE) 2785cb04b873SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2786cb04b873SMark J Musante "already in replacing/spare config; wait " 2787cb04b873SMark J Musante "for completion or use 'zpool detach'")); 27888654d025Sperrin else 27898654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27908654d025Sperrin "cannot replace a replacing device")); 27918654d025Sperrin } else { 279299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 279399653d4eSeschrock "can only attach to mirrors and top-level " 279499653d4eSeschrock "disks")); 27958654d025Sperrin } 279699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2797fa9e4066Sahrens break; 2798fa9e4066Sahrens 2799ea8dc4b6Seschrock case EINVAL: 2800fa9e4066Sahrens /* 2801fa9e4066Sahrens * The new device must be a single disk. 2802fa9e4066Sahrens */ 280399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 280499653d4eSeschrock "new device must be a single disk")); 280599653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2806fa9e4066Sahrens break; 2807fa9e4066Sahrens 2808ea8dc4b6Seschrock case EBUSY: 28095cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, " 28105cabbc6bSPrashanth Sreenivasa "or pool has removing/removed vdevs"), 281199653d4eSeschrock new_disk); 281299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2813fa9e4066Sahrens break; 2814fa9e4066Sahrens 2815ea8dc4b6Seschrock case EOVERFLOW: 2816fa9e4066Sahrens /* 2817fa9e4066Sahrens * The new device is too small. 2818fa9e4066Sahrens */ 281999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 282099653d4eSeschrock "device is too small")); 282199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2822fa9e4066Sahrens break; 2823fa9e4066Sahrens 2824ea8dc4b6Seschrock case EDOM: 2825fa9e4066Sahrens /* 2826fa9e4066Sahrens * The new device has a different alignment requirement. 2827fa9e4066Sahrens */ 282899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 282999653d4eSeschrock "devices have different sector alignment")); 283099653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2831fa9e4066Sahrens break; 2832fa9e4066Sahrens 2833ea8dc4b6Seschrock case ENAMETOOLONG: 2834fa9e4066Sahrens /* 2835fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 2836fa9e4066Sahrens */ 283799653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2838fa9e4066Sahrens break; 2839fa9e4066Sahrens 2840ea8dc4b6Seschrock default: 284199653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2842fa9e4066Sahrens } 2843fa9e4066Sahrens 284499653d4eSeschrock return (-1); 2845fa9e4066Sahrens } 2846fa9e4066Sahrens 2847fa9e4066Sahrens /* 2848fa9e4066Sahrens * Detach the specified device. 2849fa9e4066Sahrens */ 2850fa9e4066Sahrens int 2851fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2852fa9e4066Sahrens { 2853fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2854fa9e4066Sahrens char msg[1024]; 285599653d4eSeschrock nvlist_t *tgt; 2856fa94a07fSbrendan boolean_t avail_spare, l2cache; 285799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2858fa9e4066Sahrens 2859ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2860ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2861ea8dc4b6Seschrock 2862fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2863ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 28645cabbc6bSPrashanth Sreenivasa NULL)) == NULL) 286599653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2866fa9e4066Sahrens 2867a43d325bSek if (avail_spare) 286899653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 286999653d4eSeschrock 2870fa94a07fSbrendan if (l2cache) 2871fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2872fa94a07fSbrendan 287399653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 287499653d4eSeschrock 2875ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2876fa9e4066Sahrens return (0); 2877fa9e4066Sahrens 2878fa9e4066Sahrens switch (errno) { 2879fa9e4066Sahrens 2880ea8dc4b6Seschrock case ENOTSUP: 2881fa9e4066Sahrens /* 2882fa9e4066Sahrens * Can't detach from this type of vdev. 2883fa9e4066Sahrens */ 288499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 288599653d4eSeschrock "applicable to mirror and replacing vdevs")); 2886cb04b873SMark J Musante (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2887fa9e4066Sahrens break; 2888fa9e4066Sahrens 2889ea8dc4b6Seschrock case EBUSY: 2890fa9e4066Sahrens /* 2891fa9e4066Sahrens * There are no other replicas of this device. 2892fa9e4066Sahrens */ 289399653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2894fa9e4066Sahrens break; 2895fa9e4066Sahrens 2896ea8dc4b6Seschrock default: 289799653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2898ea8dc4b6Seschrock } 2899ea8dc4b6Seschrock 290099653d4eSeschrock return (-1); 290199653d4eSeschrock } 290299653d4eSeschrock 29031195e687SMark J Musante /* 29041195e687SMark J Musante * Find a mirror vdev in the source nvlist. 29051195e687SMark J Musante * 29061195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors 29071195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the 29081195e687SMark J Musante * user specified on the command line. We loop over the mchild array to 29091195e687SMark J Musante * see if any entry in the schild array matches. 29101195e687SMark J Musante * 29111195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return 29121195e687SMark J Musante * the index of that entry. Otherwise we return -1. 29131195e687SMark J Musante */ 29141195e687SMark J Musante static int 29151195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 29161195e687SMark J Musante nvlist_t **schild, uint_t schildren) 29171195e687SMark J Musante { 29181195e687SMark J Musante uint_t mc; 29191195e687SMark J Musante 29201195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) { 29211195e687SMark J Musante uint_t sc; 29221195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 29231195e687SMark J Musante mchild[mc], B_FALSE); 29241195e687SMark J Musante 29251195e687SMark J Musante for (sc = 0; sc < schildren; sc++) { 29261195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 29271195e687SMark J Musante schild[sc], B_FALSE); 29281195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0); 29291195e687SMark J Musante 29301195e687SMark J Musante free(spath); 29311195e687SMark J Musante if (result) { 29321195e687SMark J Musante free(mpath); 29331195e687SMark J Musante return (mc); 29341195e687SMark J Musante } 29351195e687SMark J Musante } 29361195e687SMark J Musante 29371195e687SMark J Musante free(mpath); 29381195e687SMark J Musante } 29391195e687SMark J Musante 29401195e687SMark J Musante return (-1); 29411195e687SMark J Musante } 29421195e687SMark J Musante 29431195e687SMark J Musante /* 29441195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist 29451195e687SMark J Musante * is generated and it is the responsibility of the caller to free it. 29461195e687SMark J Musante */ 29471195e687SMark J Musante int 29481195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 29491195e687SMark J Musante nvlist_t *props, splitflags_t flags) 29501195e687SMark J Musante { 29511195e687SMark J Musante zfs_cmd_t zc = { 0 }; 29521195e687SMark J Musante char msg[1024]; 29531195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 29541195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL; 29551195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 29561195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl; 29571195e687SMark J Musante uint64_t vers; 29581195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE; 29591195e687SMark J Musante int retval = 0; 29601195e687SMark J Musante 29611195e687SMark J Musante (void) snprintf(msg, sizeof (msg), 29621195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 29631195e687SMark J Musante 29641195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname)) 29651195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 29661195e687SMark J Musante 29671195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) { 29681195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to " 29691195e687SMark J Musante "retrieve pool configuration\n")); 29701195e687SMark J Musante return (-1); 29711195e687SMark J Musante } 29721195e687SMark J Musante 29731195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 29741195e687SMark J Musante == 0); 29751195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 29761195e687SMark J Musante 29771195e687SMark J Musante if (props) { 2978f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 29791195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 2980f9af39baSGeorge Wilson props, vers, flags, msg)) == NULL) 29811195e687SMark J Musante return (-1); 29821195e687SMark J Musante } 29831195e687SMark J Musante 29841195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 29851195e687SMark J Musante &children) != 0) { 29861195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29871195e687SMark J Musante "Source pool is missing vdev tree")); 2988aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 29891195e687SMark J Musante return (-1); 29901195e687SMark J Musante } 29911195e687SMark J Musante 29921195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 29931195e687SMark J Musante vcount = 0; 29941195e687SMark J Musante 29951195e687SMark J Musante if (*newroot == NULL || 29961195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 29971195e687SMark J Musante &newchild, &newchildren) != 0) 29981195e687SMark J Musante newchildren = 0; 29991195e687SMark J Musante 30001195e687SMark J Musante for (c = 0; c < children; c++) { 30011195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE; 30021195e687SMark J Musante char *type; 30031195e687SMark J Musante nvlist_t **mchild, *vdev; 30041195e687SMark J Musante uint_t mchildren; 30051195e687SMark J Musante int entry; 30061195e687SMark J Musante 30071195e687SMark J Musante /* 30081195e687SMark J Musante * Unlike cache & spares, slogs are stored in the 30091195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 30101195e687SMark J Musante */ 30111195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 30121195e687SMark J Musante &is_log); 30131195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 30141195e687SMark J Musante &is_hole); 30151195e687SMark J Musante if (is_log || is_hole) { 30161195e687SMark J Musante /* 30171195e687SMark J Musante * Create a hole vdev and put it in the config. 30181195e687SMark J Musante */ 30191195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 30201195e687SMark J Musante goto out; 30211195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 30221195e687SMark J Musante VDEV_TYPE_HOLE) != 0) 30231195e687SMark J Musante goto out; 30241195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 30251195e687SMark J Musante 1) != 0) 30261195e687SMark J Musante goto out; 30271195e687SMark J Musante if (lastlog == 0) 30281195e687SMark J Musante lastlog = vcount; 30291195e687SMark J Musante varray[vcount++] = vdev; 30301195e687SMark J Musante continue; 30311195e687SMark J Musante } 30321195e687SMark J Musante lastlog = 0; 30331195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 30341195e687SMark J Musante == 0); 30351195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 30361195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30371195e687SMark J Musante "Source pool must be composed only of mirrors\n")); 30381195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 30391195e687SMark J Musante goto out; 30401195e687SMark J Musante } 30411195e687SMark J Musante 30421195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c], 30431195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 30441195e687SMark J Musante 30451195e687SMark J Musante /* find or add an entry for this top-level vdev */ 30461195e687SMark J Musante if (newchildren > 0 && 30471195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren, 30481195e687SMark J Musante newchild, newchildren)) >= 0) { 30491195e687SMark J Musante /* We found a disk that the user specified. */ 30501195e687SMark J Musante vdev = mchild[entry]; 30511195e687SMark J Musante ++found; 30521195e687SMark J Musante } else { 30531195e687SMark J Musante /* User didn't specify a disk for this vdev. */ 30541195e687SMark J Musante vdev = mchild[mchildren - 1]; 30551195e687SMark J Musante } 30561195e687SMark J Musante 30571195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 30581195e687SMark J Musante goto out; 30591195e687SMark J Musante } 30601195e687SMark J Musante 30611195e687SMark J Musante /* did we find every disk the user specified? */ 30621195e687SMark J Musante if (found != newchildren) { 30631195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 30641195e687SMark J Musante "include at most one disk from each mirror")); 30651195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 30661195e687SMark J Musante goto out; 30671195e687SMark J Musante } 30681195e687SMark J Musante 30691195e687SMark J Musante /* Prepare the nvlist for populating. */ 30701195e687SMark J Musante if (*newroot == NULL) { 30711195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 30721195e687SMark J Musante goto out; 30731195e687SMark J Musante freelist = B_TRUE; 30741195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 30751195e687SMark J Musante VDEV_TYPE_ROOT) != 0) 30761195e687SMark J Musante goto out; 30771195e687SMark J Musante } else { 30781195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 30791195e687SMark J Musante } 30801195e687SMark J Musante 30811195e687SMark J Musante /* Add all the children we found */ 30821195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 30831195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0) 30841195e687SMark J Musante goto out; 30851195e687SMark J Musante 30861195e687SMark J Musante /* 30871195e687SMark J Musante * If we're just doing a dry run, exit now with success. 30881195e687SMark J Musante */ 30891195e687SMark J Musante if (flags.dryrun) { 30901195e687SMark J Musante memory_err = B_FALSE; 30911195e687SMark J Musante freelist = B_FALSE; 30921195e687SMark J Musante goto out; 30931195e687SMark J Musante } 30941195e687SMark J Musante 30951195e687SMark J Musante /* now build up the config list & call the ioctl */ 30961195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 30971195e687SMark J Musante goto out; 30981195e687SMark J Musante 30991195e687SMark J Musante if (nvlist_add_nvlist(newconfig, 31001195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 31011195e687SMark J Musante nvlist_add_string(newconfig, 31021195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 31031195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 31041195e687SMark J Musante goto out; 31051195e687SMark J Musante 31061195e687SMark J Musante /* 31071195e687SMark J Musante * The new pool is automatically part of the namespace unless we 31081195e687SMark J Musante * explicitly export it. 31091195e687SMark J Musante */ 31101195e687SMark J Musante if (!flags.import) 31111195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 31121195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 31131195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 31141195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 31151195e687SMark J Musante goto out; 31161195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 31171195e687SMark J Musante goto out; 31181195e687SMark J Musante 31191195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 31201195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg); 31211195e687SMark J Musante goto out; 31221195e687SMark J Musante } 31231195e687SMark J Musante 31241195e687SMark J Musante freelist = B_FALSE; 31251195e687SMark J Musante memory_err = B_FALSE; 31261195e687SMark J Musante 31271195e687SMark J Musante out: 31281195e687SMark J Musante if (varray != NULL) { 31291195e687SMark J Musante int v; 31301195e687SMark J Musante 31311195e687SMark J Musante for (v = 0; v < vcount; v++) 31321195e687SMark J Musante nvlist_free(varray[v]); 31331195e687SMark J Musante free(varray); 31341195e687SMark J Musante } 31351195e687SMark J Musante zcmd_free_nvlists(&zc); 3136aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 3137aab83bb8SJosef 'Jeff' Sipek nvlist_free(newconfig); 31381195e687SMark J Musante if (freelist) { 31391195e687SMark J Musante nvlist_free(*newroot); 31401195e687SMark J Musante *newroot = NULL; 31411195e687SMark J Musante } 31421195e687SMark J Musante 31431195e687SMark J Musante if (retval != 0) 31441195e687SMark J Musante return (retval); 31451195e687SMark J Musante 31461195e687SMark J Musante if (memory_err) 31471195e687SMark J Musante return (no_memory(hdl)); 31481195e687SMark J Musante 31491195e687SMark J Musante return (0); 31501195e687SMark J Musante } 31511195e687SMark J Musante 315299653d4eSeschrock /* 31535cabbc6bSPrashanth Sreenivasa * Remove the given device. 315499653d4eSeschrock */ 315599653d4eSeschrock int 315699653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 315799653d4eSeschrock { 315899653d4eSeschrock zfs_cmd_t zc = { 0 }; 315999653d4eSeschrock char msg[1024]; 316099653d4eSeschrock nvlist_t *tgt; 316188ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog; 316299653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 316388ecc943SGeorge Wilson uint64_t version; 316499653d4eSeschrock 316599653d4eSeschrock (void) snprintf(msg, sizeof (msg), 316699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 316799653d4eSeschrock 316899653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3169ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 31705cabbc6bSPrashanth Sreenivasa &islog)) == NULL) 317199653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 317299653d4eSeschrock 317388ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 317488ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) { 317588ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31765cabbc6bSPrashanth Sreenivasa "pool must be upgraded to support log removal")); 317788ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg)); 317888ecc943SGeorge Wilson } 317988ecc943SGeorge Wilson 31805cabbc6bSPrashanth Sreenivasa if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) { 31815cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31825cabbc6bSPrashanth Sreenivasa "root pool can not have removed devices, " 31835cabbc6bSPrashanth Sreenivasa "because GRUB does not understand them")); 31845cabbc6bSPrashanth Sreenivasa return (zfs_error(hdl, EINVAL, msg)); 31855cabbc6bSPrashanth Sreenivasa } 31865cabbc6bSPrashanth Sreenivasa 31875cabbc6bSPrashanth Sreenivasa zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 31885cabbc6bSPrashanth Sreenivasa 31895cabbc6bSPrashanth Sreenivasa if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 31905cabbc6bSPrashanth Sreenivasa return (0); 31915cabbc6bSPrashanth Sreenivasa 31925cabbc6bSPrashanth Sreenivasa switch (errno) { 31935cabbc6bSPrashanth Sreenivasa 31945cabbc6bSPrashanth Sreenivasa case EINVAL: 31955cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31965cabbc6bSPrashanth Sreenivasa "invalid config; all top-level vdevs must " 31975cabbc6bSPrashanth Sreenivasa "have the same sector size and not be raidz.")); 31985cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 31995cabbc6bSPrashanth Sreenivasa break; 32005cabbc6bSPrashanth Sreenivasa 32015cabbc6bSPrashanth Sreenivasa case EBUSY: 32025cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32035cabbc6bSPrashanth Sreenivasa "Pool busy; removal may already be in progress")); 32045cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_BUSY, msg); 32055cabbc6bSPrashanth Sreenivasa break; 32065cabbc6bSPrashanth Sreenivasa 32075cabbc6bSPrashanth Sreenivasa default: 32085cabbc6bSPrashanth Sreenivasa (void) zpool_standard_error(hdl, errno, msg); 32095cabbc6bSPrashanth Sreenivasa } 32105cabbc6bSPrashanth Sreenivasa return (-1); 32115cabbc6bSPrashanth Sreenivasa } 32125cabbc6bSPrashanth Sreenivasa 32135cabbc6bSPrashanth Sreenivasa int 32145cabbc6bSPrashanth Sreenivasa zpool_vdev_remove_cancel(zpool_handle_t *zhp) 32155cabbc6bSPrashanth Sreenivasa { 32165cabbc6bSPrashanth Sreenivasa zfs_cmd_t zc = { 0 }; 32175cabbc6bSPrashanth Sreenivasa char msg[1024]; 32185cabbc6bSPrashanth Sreenivasa libzfs_handle_t *hdl = zhp->zpool_hdl; 32195cabbc6bSPrashanth Sreenivasa 32205cabbc6bSPrashanth Sreenivasa (void) snprintf(msg, sizeof (msg), 32215cabbc6bSPrashanth Sreenivasa dgettext(TEXT_DOMAIN, "cannot cancel removal")); 32225cabbc6bSPrashanth Sreenivasa 32235cabbc6bSPrashanth Sreenivasa (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 32245cabbc6bSPrashanth Sreenivasa zc.zc_cookie = 1; 322599653d4eSeschrock 3226ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 322799653d4eSeschrock return (0); 322899653d4eSeschrock 322999653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3230ea8dc4b6Seschrock } 3231ea8dc4b6Seschrock 32325cabbc6bSPrashanth Sreenivasa int 32335cabbc6bSPrashanth Sreenivasa zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path, 32345cabbc6bSPrashanth Sreenivasa uint64_t *sizep) 32355cabbc6bSPrashanth Sreenivasa { 32365cabbc6bSPrashanth Sreenivasa char msg[1024]; 32375cabbc6bSPrashanth Sreenivasa nvlist_t *tgt; 32385cabbc6bSPrashanth Sreenivasa boolean_t avail_spare, l2cache, islog; 32395cabbc6bSPrashanth Sreenivasa libzfs_handle_t *hdl = zhp->zpool_hdl; 32405cabbc6bSPrashanth Sreenivasa 32415cabbc6bSPrashanth Sreenivasa (void) snprintf(msg, sizeof (msg), 32425cabbc6bSPrashanth Sreenivasa dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"), 32435cabbc6bSPrashanth Sreenivasa path); 32445cabbc6bSPrashanth Sreenivasa 32455cabbc6bSPrashanth Sreenivasa if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 32465cabbc6bSPrashanth Sreenivasa &islog)) == NULL) 32475cabbc6bSPrashanth Sreenivasa return (zfs_error(hdl, EZFS_NODEVICE, msg)); 32485cabbc6bSPrashanth Sreenivasa 32495cabbc6bSPrashanth Sreenivasa if (avail_spare || l2cache || islog) { 32505cabbc6bSPrashanth Sreenivasa *sizep = 0; 32515cabbc6bSPrashanth Sreenivasa return (0); 32525cabbc6bSPrashanth Sreenivasa } 32535cabbc6bSPrashanth Sreenivasa 32545cabbc6bSPrashanth Sreenivasa if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) { 32555cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32565cabbc6bSPrashanth Sreenivasa "indirect size not available")); 32575cabbc6bSPrashanth Sreenivasa return (zfs_error(hdl, EINVAL, msg)); 32585cabbc6bSPrashanth Sreenivasa } 32595cabbc6bSPrashanth Sreenivasa return (0); 32605cabbc6bSPrashanth Sreenivasa } 32615cabbc6bSPrashanth Sreenivasa 3262ea8dc4b6Seschrock /* 3263ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 3264ea8dc4b6Seschrock */ 3265ea8dc4b6Seschrock int 3266468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 3267ea8dc4b6Seschrock { 3268ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3269ea8dc4b6Seschrock char msg[1024]; 327099653d4eSeschrock nvlist_t *tgt; 3271468c413aSTim Haley zpool_rewind_policy_t policy; 3272fa94a07fSbrendan boolean_t avail_spare, l2cache; 327399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3274468c413aSTim Haley nvlist_t *nvi = NULL; 32754b964adaSGeorge Wilson int error; 3276ea8dc4b6Seschrock 3277ea8dc4b6Seschrock if (path) 3278ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3279ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3280e9dbad6fSeschrock path); 3281ea8dc4b6Seschrock else 3282ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3283ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3284ea8dc4b6Seschrock zhp->zpool_name); 3285ea8dc4b6Seschrock 3286ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 328799653d4eSeschrock if (path) { 3288fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 32895cabbc6bSPrashanth Sreenivasa &l2cache, NULL)) == NULL) 329099653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3291ea8dc4b6Seschrock 3292fa94a07fSbrendan /* 3293fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 3294fa94a07fSbrendan * error clearing for l2cache devices. 3295fa94a07fSbrendan */ 3296a43d325bSek if (avail_spare) 329799653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3298ea8dc4b6Seschrock 329999653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 330099653d4eSeschrock &zc.zc_guid) == 0); 3301fa9e4066Sahrens } 3302fa9e4066Sahrens 3303468c413aSTim Haley zpool_get_rewind_policy(rewindnvl, &policy); 3304468c413aSTim Haley zc.zc_cookie = policy.zrp_request; 3305468c413aSTim Haley 330657f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 3307468c413aSTim Haley return (-1); 3308468c413aSTim Haley 3309cb04b873SMark J Musante if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 3310468c413aSTim Haley return (-1); 3311468c413aSTim Haley 33124b964adaSGeorge Wilson while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 33134b964adaSGeorge Wilson errno == ENOMEM) { 33144b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 33154b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 33164b964adaSGeorge Wilson return (-1); 33174b964adaSGeorge Wilson } 33184b964adaSGeorge Wilson } 33194b964adaSGeorge Wilson 33204b964adaSGeorge Wilson if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 3321468c413aSTim Haley errno != EPERM && errno != EACCES)) { 3322468c413aSTim Haley if (policy.zrp_request & 3323468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 3324468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 3325468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name, 3326468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 3327468c413aSTim Haley nvi); 3328468c413aSTim Haley nvlist_free(nvi); 3329468c413aSTim Haley } 3330468c413aSTim Haley zcmd_free_nvlists(&zc); 333199653d4eSeschrock return (0); 3332468c413aSTim Haley } 333399653d4eSeschrock 3334468c413aSTim Haley zcmd_free_nvlists(&zc); 333599653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3336fa9e4066Sahrens } 3337fa9e4066Sahrens 33383d7072f8Seschrock /* 33393d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 33403d7072f8Seschrock */ 33413d7072f8Seschrock int 33423d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 33433d7072f8Seschrock { 33443d7072f8Seschrock zfs_cmd_t zc = { 0 }; 33453d7072f8Seschrock char msg[1024]; 33463d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 33473d7072f8Seschrock 33483d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 33493d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 33503d7072f8Seschrock guid); 33513d7072f8Seschrock 33523d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 33533d7072f8Seschrock zc.zc_guid = guid; 335414f8ce41SVictor Latushkin zc.zc_cookie = ZPOOL_NO_REWIND; 33553d7072f8Seschrock 33563d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 33573d7072f8Seschrock return (0); 33583d7072f8Seschrock 33593d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 33603d7072f8Seschrock } 33613d7072f8Seschrock 3362e9103aaeSGarrett D'Amore /* 3363e9103aaeSGarrett D'Amore * Change the GUID for a pool. 3364e9103aaeSGarrett D'Amore */ 3365e9103aaeSGarrett D'Amore int 3366e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp) 3367e9103aaeSGarrett D'Amore { 3368e9103aaeSGarrett D'Amore char msg[1024]; 3369e9103aaeSGarrett D'Amore libzfs_handle_t *hdl = zhp->zpool_hdl; 3370e9103aaeSGarrett D'Amore zfs_cmd_t zc = { 0 }; 3371e9103aaeSGarrett D'Amore 3372e9103aaeSGarrett D'Amore (void) snprintf(msg, sizeof (msg), 3373e9103aaeSGarrett D'Amore dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 3374e9103aaeSGarrett D'Amore 3375e9103aaeSGarrett D'Amore (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3376e9103aaeSGarrett D'Amore if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 3377e9103aaeSGarrett D'Amore return (0); 3378e9103aaeSGarrett D'Amore 3379e9103aaeSGarrett D'Amore return (zpool_standard_error(hdl, errno, msg)); 3380e9103aaeSGarrett D'Amore } 3381e9103aaeSGarrett D'Amore 33824263d13fSGeorge Wilson /* 33834263d13fSGeorge Wilson * Reopen the pool. 33844263d13fSGeorge Wilson */ 33854263d13fSGeorge Wilson int 33864263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp) 33874263d13fSGeorge Wilson { 33884263d13fSGeorge Wilson zfs_cmd_t zc = { 0 }; 33894263d13fSGeorge Wilson char msg[1024]; 33904263d13fSGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl; 33914263d13fSGeorge Wilson 33924263d13fSGeorge Wilson (void) snprintf(msg, sizeof (msg), 33934263d13fSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), 33944263d13fSGeorge Wilson zhp->zpool_name); 33954263d13fSGeorge Wilson 33964263d13fSGeorge Wilson (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 33974263d13fSGeorge Wilson if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0) 33984263d13fSGeorge Wilson return (0); 33994263d13fSGeorge Wilson return (zpool_standard_error(hdl, errno, msg)); 34004263d13fSGeorge Wilson } 34014263d13fSGeorge Wilson 3402c67d9675Seschrock /* 3403c67d9675Seschrock * Convert from a devid string to a path. 3404c67d9675Seschrock */ 3405c67d9675Seschrock static char * 3406c67d9675Seschrock devid_to_path(char *devid_str) 3407c67d9675Seschrock { 3408c67d9675Seschrock ddi_devid_t devid; 3409c67d9675Seschrock char *minor; 3410c67d9675Seschrock char *path; 3411c67d9675Seschrock devid_nmlist_t *list = NULL; 3412c67d9675Seschrock int ret; 3413c67d9675Seschrock 3414c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 3415c67d9675Seschrock return (NULL); 3416c67d9675Seschrock 3417c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 3418c67d9675Seschrock 3419c67d9675Seschrock devid_str_free(minor); 3420c67d9675Seschrock devid_free(devid); 3421c67d9675Seschrock 3422c67d9675Seschrock if (ret != 0) 3423c67d9675Seschrock return (NULL); 3424c67d9675Seschrock 3425078266a5SMarcel Telka /* 3426078266a5SMarcel Telka * In a case the strdup() fails, we will just return NULL below. 3427078266a5SMarcel Telka */ 3428078266a5SMarcel Telka path = strdup(list[0].devname); 342999653d4eSeschrock 3430c67d9675Seschrock devid_free_nmlist(list); 3431c67d9675Seschrock 3432c67d9675Seschrock return (path); 3433c67d9675Seschrock } 3434c67d9675Seschrock 3435c67d9675Seschrock /* 3436c67d9675Seschrock * Convert from a path to a devid string. 3437c67d9675Seschrock */ 3438c67d9675Seschrock static char * 3439c67d9675Seschrock path_to_devid(const char *path) 3440c67d9675Seschrock { 3441c67d9675Seschrock int fd; 3442c67d9675Seschrock ddi_devid_t devid; 3443c67d9675Seschrock char *minor, *ret; 3444c67d9675Seschrock 3445c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 3446c67d9675Seschrock return (NULL); 3447c67d9675Seschrock 3448c67d9675Seschrock minor = NULL; 3449c67d9675Seschrock ret = NULL; 3450c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 3451c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 3452c67d9675Seschrock ret = devid_str_encode(devid, minor); 3453c67d9675Seschrock if (minor != NULL) 3454c67d9675Seschrock devid_str_free(minor); 3455c67d9675Seschrock devid_free(devid); 3456c67d9675Seschrock } 3457c67d9675Seschrock (void) close(fd); 3458c67d9675Seschrock 3459c67d9675Seschrock return (ret); 3460c67d9675Seschrock } 3461c67d9675Seschrock 3462c67d9675Seschrock /* 3463c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 3464c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 3465c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 3466c67d9675Seschrock */ 3467c67d9675Seschrock static void 3468c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3469c67d9675Seschrock { 3470c67d9675Seschrock zfs_cmd_t zc = { 0 }; 3471c67d9675Seschrock 3472c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3473e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3474c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3475ea8dc4b6Seschrock &zc.zc_guid) == 0); 3476c67d9675Seschrock 347799653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3478c67d9675Seschrock } 3479c67d9675Seschrock 3480c67d9675Seschrock /* 3481c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 3482c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3483c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 3484c67d9675Seschrock * trailing 's0' slice name. 3485c67d9675Seschrock * 3486c67d9675Seschrock * This routine is also responsible for identifying when disks have been 3487c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 3488c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 3489c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 3490c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 3491c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 3492c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3493c67d9675Seschrock * of these checks. 3494c67d9675Seschrock */ 3495c67d9675Seschrock char * 349688ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 349788ecc943SGeorge Wilson boolean_t verbose) 3498c67d9675Seschrock { 3499c67d9675Seschrock char *path, *devid; 3500ea8dc4b6Seschrock uint64_t value; 3501ea8dc4b6Seschrock char buf[64]; 35023d7072f8Seschrock vdev_stat_t *vs; 35033d7072f8Seschrock uint_t vsc; 3504c67d9675Seschrock 3505ea8dc4b6Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3506ea8dc4b6Seschrock &value) == 0) { 3507ea8dc4b6Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3508ea8dc4b6Seschrock &value) == 0); 35095ad82045Snd (void) snprintf(buf, sizeof (buf), "%llu", 35105ad82045Snd (u_longlong_t)value); 3511ea8dc4b6Seschrock path = buf; 3512ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3513c67d9675Seschrock 35143d7072f8Seschrock /* 35153d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 35163d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 35173d7072f8Seschrock * open a misbehaving device, which can have undesirable 35183d7072f8Seschrock * effects. 35193d7072f8Seschrock */ 35203f9d6ad7SLin Ling if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 35213d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 35223d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 35233d7072f8Seschrock zhp != NULL && 3524c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3525c67d9675Seschrock /* 3526c67d9675Seschrock * Determine if the current path is correct. 3527c67d9675Seschrock */ 3528c67d9675Seschrock char *newdevid = path_to_devid(path); 3529c67d9675Seschrock 3530c67d9675Seschrock if (newdevid == NULL || 3531c67d9675Seschrock strcmp(devid, newdevid) != 0) { 3532c67d9675Seschrock char *newpath; 3533c67d9675Seschrock 3534c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 3535c67d9675Seschrock /* 3536c67d9675Seschrock * Update the path appropriately. 3537c67d9675Seschrock */ 3538c67d9675Seschrock set_path(zhp, nv, newpath); 353999653d4eSeschrock if (nvlist_add_string(nv, 354099653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 354199653d4eSeschrock verify(nvlist_lookup_string(nv, 354299653d4eSeschrock ZPOOL_CONFIG_PATH, 354399653d4eSeschrock &path) == 0); 3544c67d9675Seschrock free(newpath); 3545c67d9675Seschrock } 3546c67d9675Seschrock } 3547c67d9675Seschrock 354899653d4eSeschrock if (newdevid) 354999653d4eSeschrock devid_str_free(newdevid); 3550c67d9675Seschrock } 3551c67d9675Seschrock 35526401734dSWill Andrews if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) 35536401734dSWill Andrews path += strlen(ZFS_DISK_ROOTD); 3554c67d9675Seschrock 3555c67d9675Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3556ea8dc4b6Seschrock &value) == 0 && value) { 35573fdda499SJohn Harres int pathlen = strlen(path); 355899653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 35593fdda499SJohn Harres 35603fdda499SJohn Harres /* 35617855d95bSToomas Soome * If it starts with c#, and ends with "s0" or "s1", 35627855d95bSToomas Soome * chop the slice off, or if it ends with "s0/old" or 35637855d95bSToomas Soome * "s1/old", remove the slice from the middle. 35643fdda499SJohn Harres */ 35653fdda499SJohn Harres if (CTD_CHECK(tmp)) { 35667855d95bSToomas Soome if (strcmp(&tmp[pathlen - 2], "s0") == 0 || 35677855d95bSToomas Soome strcmp(&tmp[pathlen - 2], "s1") == 0) { 35683fdda499SJohn Harres tmp[pathlen - 2] = '\0'; 35693fdda499SJohn Harres } else if (pathlen > 6 && 35707855d95bSToomas Soome (strcmp(&tmp[pathlen - 6], "s0/old") == 0 || 35717855d95bSToomas Soome strcmp(&tmp[pathlen - 6], "s1/old") == 0)) { 35723fdda499SJohn Harres (void) strcpy(&tmp[pathlen - 6], 35733fdda499SJohn Harres "/old"); 35743fdda499SJohn Harres } 35753fdda499SJohn Harres } 3576c67d9675Seschrock return (tmp); 3577c67d9675Seschrock } 3578c67d9675Seschrock } else { 3579c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 358099653d4eSeschrock 358199653d4eSeschrock /* 358299653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 358399653d4eSeschrock */ 358499653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 358599653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 358699653d4eSeschrock &value) == 0); 358799653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 35885ad82045Snd (u_longlong_t)value); 358999653d4eSeschrock path = buf; 359099653d4eSeschrock } 359188ecc943SGeorge Wilson 359288ecc943SGeorge Wilson /* 359388ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id> 359488ecc943SGeorge Wilson * naming convention. 359588ecc943SGeorge Wilson */ 359688ecc943SGeorge Wilson if (verbose) { 359788ecc943SGeorge Wilson uint64_t id; 359888ecc943SGeorge Wilson 359988ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 360088ecc943SGeorge Wilson &id) == 0); 360188ecc943SGeorge Wilson (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 360288ecc943SGeorge Wilson (u_longlong_t)id); 360388ecc943SGeorge Wilson path = buf; 360488ecc943SGeorge Wilson } 3605c67d9675Seschrock } 3606c67d9675Seschrock 360799653d4eSeschrock return (zfs_strdup(hdl, path)); 3608c67d9675Seschrock } 3609ea8dc4b6Seschrock 3610ea8dc4b6Seschrock static int 3611a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b) 3612ea8dc4b6Seschrock { 36137802d7bfSMatthew Ahrens return (memcmp(a, b, sizeof (zbookmark_phys_t))); 3614ea8dc4b6Seschrock } 3615ea8dc4b6Seschrock 3616ea8dc4b6Seschrock /* 3617ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 3618ea8dc4b6Seschrock * caller. 3619ea8dc4b6Seschrock */ 3620ea8dc4b6Seschrock int 362155434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3622ea8dc4b6Seschrock { 3623ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3624ea8dc4b6Seschrock uint64_t count; 36257802d7bfSMatthew Ahrens zbookmark_phys_t *zb = NULL; 362655434c77Sek int i; 3627ea8dc4b6Seschrock 3628ea8dc4b6Seschrock /* 3629ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 3630ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 3631ea8dc4b6Seschrock * entire list. 3632ea8dc4b6Seschrock */ 3633ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3634ea8dc4b6Seschrock &count) == 0); 363575519f38Sek if (count == 0) 363675519f38Sek return (0); 3637e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 36387802d7bfSMatthew Ahrens count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL) 363999653d4eSeschrock return (-1); 3640e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 3641ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3642ea8dc4b6Seschrock for (;;) { 364399653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 364499653d4eSeschrock &zc) != 0) { 3645e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 3646ea8dc4b6Seschrock if (errno == ENOMEM) { 36477802d7bfSMatthew Ahrens void *dst; 36487802d7bfSMatthew Ahrens 3649bf561db0Svb count = zc.zc_nvlist_dst_size; 36507802d7bfSMatthew Ahrens dst = zfs_alloc(zhp->zpool_hdl, count * 36517802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 36527802d7bfSMatthew Ahrens if (dst == NULL) 365399653d4eSeschrock return (-1); 36547802d7bfSMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)dst; 3655ea8dc4b6Seschrock } else { 3656ea8dc4b6Seschrock return (-1); 3657ea8dc4b6Seschrock } 3658ea8dc4b6Seschrock } else { 3659ea8dc4b6Seschrock break; 3660ea8dc4b6Seschrock } 3661ea8dc4b6Seschrock } 3662ea8dc4b6Seschrock 3663ea8dc4b6Seschrock /* 3664ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 3665ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3666e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3667ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 3668ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 3669ea8dc4b6Seschrock */ 36707802d7bfSMatthew Ahrens zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) + 3671e9dbad6fSeschrock zc.zc_nvlist_dst_size; 3672e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 3673ea8dc4b6Seschrock 3674a2cdcdd2SPaul Dagnelie qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare); 3675ea8dc4b6Seschrock 367655434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3677ea8dc4b6Seschrock 3678ea8dc4b6Seschrock /* 367955434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3680ea8dc4b6Seschrock */ 3681ea8dc4b6Seschrock for (i = 0; i < count; i++) { 3682ea8dc4b6Seschrock nvlist_t *nv; 3683ea8dc4b6Seschrock 3684c0a81264Sek /* ignoring zb_blkid and zb_level for now */ 3685c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3686c0a81264Sek zb[i-1].zb_object == zb[i].zb_object) 3687ea8dc4b6Seschrock continue; 3688ea8dc4b6Seschrock 368955434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 369055434c77Sek goto nomem; 369155434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 369255434c77Sek zb[i].zb_objset) != 0) { 369355434c77Sek nvlist_free(nv); 369499653d4eSeschrock goto nomem; 3695ea8dc4b6Seschrock } 369655434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 369755434c77Sek zb[i].zb_object) != 0) { 369855434c77Sek nvlist_free(nv); 369955434c77Sek goto nomem; 370055434c77Sek } 370155434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 370255434c77Sek nvlist_free(nv); 370355434c77Sek goto nomem; 370455434c77Sek } 370555434c77Sek nvlist_free(nv); 3706ea8dc4b6Seschrock } 3707ea8dc4b6Seschrock 37083ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 3709ea8dc4b6Seschrock return (0); 371099653d4eSeschrock 371199653d4eSeschrock nomem: 3712e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 371399653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 3714ea8dc4b6Seschrock } 3715eaca9bbdSeschrock 3716eaca9bbdSeschrock /* 3717eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 3718eaca9bbdSeschrock */ 3719eaca9bbdSeschrock int 3720990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3721eaca9bbdSeschrock { 3722eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 372399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3724eaca9bbdSeschrock 3725eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3726990b4856Slling zc.zc_cookie = new_version; 3727990b4856Slling 3728ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3729ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 373099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 373199653d4eSeschrock zhp->zpool_name)); 3732eaca9bbdSeschrock return (0); 3733eaca9bbdSeschrock } 373406eeb2adSek 373506eeb2adSek void 37364445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len) 373706eeb2adSek { 37384445fffbSMatthew Ahrens (void) strlcpy(string, basename(argv[0]), len); 37394445fffbSMatthew Ahrens for (int i = 1; i < argc; i++) { 37404445fffbSMatthew Ahrens (void) strlcat(string, " ", len); 37414445fffbSMatthew Ahrens (void) strlcat(string, argv[i], len); 37422a6b87f0Sek } 37432a6b87f0Sek } 37442a6b87f0Sek 37452a6b87f0Sek int 37464445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message) 37472a6b87f0Sek { 37484445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 }; 37494445fffbSMatthew Ahrens nvlist_t *args; 37504445fffbSMatthew Ahrens int err; 37514445fffbSMatthew Ahrens 37524445fffbSMatthew Ahrens args = fnvlist_alloc(); 37534445fffbSMatthew Ahrens fnvlist_add_string(args, "message", message); 37544445fffbSMatthew Ahrens err = zcmd_write_src_nvlist(hdl, &zc, args); 37554445fffbSMatthew Ahrens if (err == 0) 37564445fffbSMatthew Ahrens err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc); 37574445fffbSMatthew Ahrens nvlist_free(args); 37584445fffbSMatthew Ahrens zcmd_free_nvlists(&zc); 37594445fffbSMatthew Ahrens return (err); 376006eeb2adSek } 376106eeb2adSek 376206eeb2adSek /* 376306eeb2adSek * Perform ioctl to get some command history of a pool. 376406eeb2adSek * 376506eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 376606eeb2adSek * logical offset of the history buffer to start reading from. 376706eeb2adSek * 376806eeb2adSek * Upon return, 'off' is the next logical offset to read from and 376906eeb2adSek * 'len' is the actual amount of bytes read into 'buf'. 377006eeb2adSek */ 377106eeb2adSek static int 377206eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 377306eeb2adSek { 377406eeb2adSek zfs_cmd_t zc = { 0 }; 377506eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl; 377606eeb2adSek 377706eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 377806eeb2adSek 377906eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf; 378006eeb2adSek zc.zc_history_len = *len; 378106eeb2adSek zc.zc_history_offset = *off; 378206eeb2adSek 378306eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 378406eeb2adSek switch (errno) { 378506eeb2adSek case EPERM: 3786ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 3787ece3d9b3Slling dgettext(TEXT_DOMAIN, 378806eeb2adSek "cannot show history for pool '%s'"), 378906eeb2adSek zhp->zpool_name)); 379006eeb2adSek case ENOENT: 3791ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 379206eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool " 379306eeb2adSek "'%s'"), zhp->zpool_name)); 3794d7306b64Sek case ENOTSUP: 3795d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3796d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool " 3797d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name)); 379806eeb2adSek default: 3799ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 380006eeb2adSek dgettext(TEXT_DOMAIN, 380106eeb2adSek "cannot get history for '%s'"), zhp->zpool_name)); 380206eeb2adSek } 380306eeb2adSek } 380406eeb2adSek 380506eeb2adSek *len = zc.zc_history_len; 380606eeb2adSek *off = zc.zc_history_offset; 380706eeb2adSek 380806eeb2adSek return (0); 380906eeb2adSek } 381006eeb2adSek 381106eeb2adSek /* 381206eeb2adSek * Process the buffer of nvlists, unpacking and storing each nvlist record 381306eeb2adSek * into 'records'. 'leftover' is set to the number of bytes that weren't 381406eeb2adSek * processed as there wasn't a complete record. 381506eeb2adSek */ 38168f18d1faSGeorge Wilson int 381706eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 381806eeb2adSek nvlist_t ***records, uint_t *numrecords) 381906eeb2adSek { 382006eeb2adSek uint64_t reclen; 382106eeb2adSek nvlist_t *nv; 382206eeb2adSek int i; 382306eeb2adSek 382406eeb2adSek while (bytes_read > sizeof (reclen)) { 382506eeb2adSek 382606eeb2adSek /* get length of packed record (stored as little endian) */ 382706eeb2adSek for (i = 0, reclen = 0; i < sizeof (reclen); i++) 382806eeb2adSek reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 382906eeb2adSek 383006eeb2adSek if (bytes_read < sizeof (reclen) + reclen) 383106eeb2adSek break; 383206eeb2adSek 383306eeb2adSek /* unpack record */ 383406eeb2adSek if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 383506eeb2adSek return (ENOMEM); 383606eeb2adSek bytes_read -= sizeof (reclen) + reclen; 383706eeb2adSek buf += sizeof (reclen) + reclen; 383806eeb2adSek 383906eeb2adSek /* add record to nvlist array */ 384006eeb2adSek (*numrecords)++; 384106eeb2adSek if (ISP2(*numrecords + 1)) { 384206eeb2adSek *records = realloc(*records, 384306eeb2adSek *numrecords * 2 * sizeof (nvlist_t *)); 384406eeb2adSek } 384506eeb2adSek (*records)[*numrecords - 1] = nv; 384606eeb2adSek } 384706eeb2adSek 384806eeb2adSek *leftover = bytes_read; 384906eeb2adSek return (0); 385006eeb2adSek } 385106eeb2adSek 385206eeb2adSek /* 385306eeb2adSek * Retrieve the command history of a pool. 385406eeb2adSek */ 385506eeb2adSek int 385606eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 385706eeb2adSek { 38583339867aSMatthew Ahrens char *buf; 38593339867aSMatthew Ahrens int buflen = 128 * 1024; 386006eeb2adSek uint64_t off = 0; 386106eeb2adSek nvlist_t **records = NULL; 386206eeb2adSek uint_t numrecords = 0; 386306eeb2adSek int err, i; 386406eeb2adSek 38653339867aSMatthew Ahrens buf = malloc(buflen); 38663339867aSMatthew Ahrens if (buf == NULL) 38673339867aSMatthew Ahrens return (ENOMEM); 386806eeb2adSek do { 38693339867aSMatthew Ahrens uint64_t bytes_read = buflen; 387006eeb2adSek uint64_t leftover; 387106eeb2adSek 387206eeb2adSek if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 387306eeb2adSek break; 387406eeb2adSek 387506eeb2adSek /* if nothing else was read in, we're at EOF, just return */ 387606eeb2adSek if (!bytes_read) 387706eeb2adSek break; 387806eeb2adSek 387906eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read, 388006eeb2adSek &leftover, &records, &numrecords)) != 0) 388106eeb2adSek break; 388206eeb2adSek off -= leftover; 38833339867aSMatthew Ahrens if (leftover == bytes_read) { 38843339867aSMatthew Ahrens /* 38853339867aSMatthew Ahrens * no progress made, because buffer is not big enough 38863339867aSMatthew Ahrens * to hold this record; resize and retry. 38873339867aSMatthew Ahrens */ 38883339867aSMatthew Ahrens buflen *= 2; 38893339867aSMatthew Ahrens free(buf); 38903339867aSMatthew Ahrens buf = malloc(buflen); 38913339867aSMatthew Ahrens if (buf == NULL) 38923339867aSMatthew Ahrens return (ENOMEM); 38933339867aSMatthew Ahrens } 389406eeb2adSek 389506eeb2adSek /* CONSTCOND */ 389606eeb2adSek } while (1); 389706eeb2adSek 38983339867aSMatthew Ahrens free(buf); 38993339867aSMatthew Ahrens 390006eeb2adSek if (!err) { 390106eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 390206eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 390306eeb2adSek records, numrecords) == 0); 390406eeb2adSek } 390506eeb2adSek for (i = 0; i < numrecords; i++) 390606eeb2adSek nvlist_free(records[i]); 390706eeb2adSek free(records); 390806eeb2adSek 390906eeb2adSek return (err); 391006eeb2adSek } 391155434c77Sek 391255434c77Sek void 391355434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 391455434c77Sek char *pathname, size_t len) 391555434c77Sek { 391655434c77Sek zfs_cmd_t zc = { 0 }; 391755434c77Sek boolean_t mounted = B_FALSE; 391855434c77Sek char *mntpnt = NULL; 39199adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 392055434c77Sek 392155434c77Sek if (dsobj == 0) { 392255434c77Sek /* special case for the MOS */ 392355434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 392455434c77Sek return; 392555434c77Sek } 392655434c77Sek 392755434c77Sek /* get the dataset's name */ 392855434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 392955434c77Sek zc.zc_obj = dsobj; 393055434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, 393155434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 393255434c77Sek /* just write out a path of two object numbers */ 393355434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 393455434c77Sek dsobj, obj); 393555434c77Sek return; 393655434c77Sek } 393755434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 393855434c77Sek 393955434c77Sek /* find out if the dataset is mounted */ 394055434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 394155434c77Sek 394255434c77Sek /* get the corrupted object's path */ 394355434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 394455434c77Sek zc.zc_obj = obj; 394555434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 394655434c77Sek &zc) == 0) { 394755434c77Sek if (mounted) { 394855434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt, 394955434c77Sek zc.zc_value); 395055434c77Sek } else { 395155434c77Sek (void) snprintf(pathname, len, "%s:%s", 395255434c77Sek dsname, zc.zc_value); 395355434c77Sek } 395455434c77Sek } else { 395555434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 395655434c77Sek } 395755434c77Sek free(mntpnt); 395855434c77Sek } 3959b1b8ab34Slling 396015e6edf1Sgw /* 396115e6edf1Sgw * Read the EFI label from the config, if a label does not exist then 396215e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL 396315e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI 39647855d95bSToomas Soome * partition. If the caller has passed a non-NULL boolean argument, then 39657855d95bSToomas Soome * we set it to indicate if the disk does have efi system partition. 396615e6edf1Sgw */ 396715e6edf1Sgw static int 39687855d95bSToomas Soome read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system) 396915e6edf1Sgw { 397015e6edf1Sgw char *path; 397115e6edf1Sgw int fd; 397215e6edf1Sgw char diskname[MAXPATHLEN]; 39737855d95bSToomas Soome boolean_t boot = B_FALSE; 397415e6edf1Sgw int err = -1; 39757855d95bSToomas Soome int slice; 397615e6edf1Sgw 397715e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 397815e6edf1Sgw return (err); 397915e6edf1Sgw 39806401734dSWill Andrews (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT, 398115e6edf1Sgw strrchr(path, '/')); 398215e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 398315e6edf1Sgw struct dk_gpt *vtoc; 398415e6edf1Sgw 398515e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 39867855d95bSToomas Soome for (slice = 0; slice < vtoc->efi_nparts; slice++) { 39877855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_SYSTEM) 39887855d95bSToomas Soome boot = B_TRUE; 39897855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_USR) 39907855d95bSToomas Soome break; 39917855d95bSToomas Soome } 39927855d95bSToomas Soome if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR) 39937855d95bSToomas Soome *sb = vtoc->efi_parts[slice].p_start; 39947855d95bSToomas Soome if (system != NULL) 39957855d95bSToomas Soome *system = boot; 399615e6edf1Sgw efi_free(vtoc); 399715e6edf1Sgw } 399815e6edf1Sgw (void) close(fd); 399915e6edf1Sgw } 400015e6edf1Sgw return (err); 400115e6edf1Sgw } 400215e6edf1Sgw 40038488aeb5Staylor /* 40048488aeb5Staylor * determine where a partition starts on a disk in the current 40058488aeb5Staylor * configuration 40068488aeb5Staylor */ 40078488aeb5Staylor static diskaddr_t 40088488aeb5Staylor find_start_block(nvlist_t *config) 40098488aeb5Staylor { 40108488aeb5Staylor nvlist_t **child; 40118488aeb5Staylor uint_t c, children; 40128488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 40138488aeb5Staylor uint64_t wholedisk; 40148488aeb5Staylor 40158488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 40168488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 40178488aeb5Staylor if (nvlist_lookup_uint64(config, 40188488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 40198488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 40208488aeb5Staylor return (MAXOFFSET_T); 40218488aeb5Staylor } 40227855d95bSToomas Soome if (read_efi_label(config, &sb, NULL) < 0) 402315e6edf1Sgw sb = MAXOFFSET_T; 40248488aeb5Staylor return (sb); 40258488aeb5Staylor } 40268488aeb5Staylor 40278488aeb5Staylor for (c = 0; c < children; c++) { 40288488aeb5Staylor sb = find_start_block(child[c]); 40298488aeb5Staylor if (sb != MAXOFFSET_T) { 40308488aeb5Staylor return (sb); 40318488aeb5Staylor } 40328488aeb5Staylor } 40338488aeb5Staylor return (MAXOFFSET_T); 40348488aeb5Staylor } 40358488aeb5Staylor 40368488aeb5Staylor /* 40378488aeb5Staylor * Label an individual disk. The name provided is the short name, 40388488aeb5Staylor * stripped of any leading /dev path. 40398488aeb5Staylor */ 40408488aeb5Staylor int 40417855d95bSToomas Soome zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name, 40427855d95bSToomas Soome zpool_boot_label_t boot_type, uint64_t boot_size, int *slice) 40438488aeb5Staylor { 40448488aeb5Staylor char path[MAXPATHLEN]; 40458488aeb5Staylor struct dk_gpt *vtoc; 40468488aeb5Staylor int fd; 40478488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 40488488aeb5Staylor uint64_t slice_size; 40498488aeb5Staylor diskaddr_t start_block; 40508488aeb5Staylor char errbuf[1024]; 40518488aeb5Staylor 4052c6ef114fSmmusante /* prepare an error message just in case */ 4053c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 4054c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 4055c6ef114fSmmusante 40568488aeb5Staylor if (zhp) { 40578488aeb5Staylor nvlist_t *nvroot; 40588488aeb5Staylor 40598488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 40608488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 40618488aeb5Staylor 40628488aeb5Staylor if (zhp->zpool_start_block == 0) 40638488aeb5Staylor start_block = find_start_block(nvroot); 40648488aeb5Staylor else 40658488aeb5Staylor start_block = zhp->zpool_start_block; 40668488aeb5Staylor zhp->zpool_start_block = start_block; 40678488aeb5Staylor } else { 40688488aeb5Staylor /* new pool */ 40698488aeb5Staylor start_block = NEW_START_BLOCK; 40708488aeb5Staylor } 40718488aeb5Staylor 40726401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name, 40738488aeb5Staylor BACKUP_SLICE); 40748488aeb5Staylor 40758488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 40768488aeb5Staylor /* 40778488aeb5Staylor * This shouldn't happen. We've long since verified that this 40788488aeb5Staylor * is a valid device. 40798488aeb5Staylor */ 4080c6ef114fSmmusante zfs_error_aux(hdl, 4081c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 40828488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 40838488aeb5Staylor } 40848488aeb5Staylor 40858488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 40868488aeb5Staylor /* 40878488aeb5Staylor * The only way this can fail is if we run out of memory, or we 40888488aeb5Staylor * were unable to read the disk's capacity 40898488aeb5Staylor */ 40908488aeb5Staylor if (errno == ENOMEM) 40918488aeb5Staylor (void) no_memory(hdl); 40928488aeb5Staylor 40938488aeb5Staylor (void) close(fd); 4094c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4095c6ef114fSmmusante "unable to read disk capacity"), name); 40968488aeb5Staylor 40978488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 40988488aeb5Staylor } 40998488aeb5Staylor 41008488aeb5Staylor /* 41018488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 41028488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 41038488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 41048488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 41058488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 41068488aeb5Staylor * can get, in the absence of V_OTHER. 41078488aeb5Staylor */ 41087855d95bSToomas Soome /* first fix the partition start block */ 41097855d95bSToomas Soome if (start_block == MAXOFFSET_T) 41107855d95bSToomas Soome start_block = NEW_START_BLOCK; 41118488aeb5Staylor 41127855d95bSToomas Soome /* 41137855d95bSToomas Soome * EFI System partition is using slice 0. 41147855d95bSToomas Soome * ZFS is on slice 1 and slice 8 is reserved. 41157855d95bSToomas Soome * We assume the GPT partition table without system 41167855d95bSToomas Soome * partition has zfs p_start == NEW_START_BLOCK. 41177855d95bSToomas Soome * If start_block != NEW_START_BLOCK, it means we have 41187855d95bSToomas Soome * system partition. Correct solution would be to query/cache vtoc 41197855d95bSToomas Soome * from existing vdev member. 41207855d95bSToomas Soome */ 41217855d95bSToomas Soome if (boot_type == ZPOOL_CREATE_BOOT_LABEL) { 41227855d95bSToomas Soome if (boot_size % vtoc->efi_lbasize != 0) { 41237855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41247855d95bSToomas Soome "boot partition size must be a multiple of %d"), 41257855d95bSToomas Soome vtoc->efi_lbasize); 41267855d95bSToomas Soome (void) close(fd); 41277855d95bSToomas Soome efi_free(vtoc); 41287855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 41297855d95bSToomas Soome } 41307855d95bSToomas Soome /* 41317855d95bSToomas Soome * System partition size checks. 41327855d95bSToomas Soome * Note the 1MB is quite arbitrary value, since we 41337855d95bSToomas Soome * are creating dedicated pool, it should be enough 41347855d95bSToomas Soome * to hold fat + efi bootloader. May need to be 41357855d95bSToomas Soome * adjusted if the bootloader size will grow. 41367855d95bSToomas Soome */ 41377855d95bSToomas Soome if (boot_size < 1024 * 1024) { 41387855d95bSToomas Soome char buf[64]; 41397855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf)); 41407855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41417855d95bSToomas Soome "Specified size %s for EFI System partition is too " 41427855d95bSToomas Soome "small, the minimum size is 1MB."), buf); 41437855d95bSToomas Soome (void) close(fd); 41447855d95bSToomas Soome efi_free(vtoc); 41457855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 41467855d95bSToomas Soome } 41477855d95bSToomas Soome /* 33MB is tested with mkfs -F pcfs */ 41487855d95bSToomas Soome if (hdl->libzfs_printerr && 41497855d95bSToomas Soome ((vtoc->efi_lbasize == 512 && 41507855d95bSToomas Soome boot_size < 33 * 1024 * 1024) || 41517855d95bSToomas Soome (vtoc->efi_lbasize == 4096 && 41527855d95bSToomas Soome boot_size < 256 * 1024 * 1024))) { 41537855d95bSToomas Soome char buf[64]; 41547855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf)); 41557855d95bSToomas Soome (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 41567855d95bSToomas Soome "Warning: EFI System partition size %s is " 41577855d95bSToomas Soome "not allowing to create FAT32 file\nsystem, which " 41587855d95bSToomas Soome "may result in unbootable system.\n"), buf); 41597855d95bSToomas Soome } 41607855d95bSToomas Soome /* Adjust zfs partition start by size of system partition. */ 41617855d95bSToomas Soome start_block += boot_size / vtoc->efi_lbasize; 41627855d95bSToomas Soome } 41637855d95bSToomas Soome 41647855d95bSToomas Soome if (start_block == NEW_START_BLOCK) { 41657855d95bSToomas Soome /* 41667855d95bSToomas Soome * Use default layout. 41677855d95bSToomas Soome * ZFS is on slice 0 and slice 8 is reserved. 41687855d95bSToomas Soome */ 41697855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1; 41707855d95bSToomas Soome slice_size -= EFI_MIN_RESV_SIZE; 41717855d95bSToomas Soome slice_size -= start_block; 41727855d95bSToomas Soome if (slice != NULL) 41737855d95bSToomas Soome *slice = 0; 41747855d95bSToomas Soome 41757855d95bSToomas Soome vtoc->efi_parts[0].p_start = start_block; 41767855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size; 41777855d95bSToomas Soome 41787855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_USR; 41797855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 41807855d95bSToomas Soome 41817855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block; 41827855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv; 41837855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED; 41847855d95bSToomas Soome } else { 41857855d95bSToomas Soome slice_size = start_block - NEW_START_BLOCK; 41867855d95bSToomas Soome vtoc->efi_parts[0].p_start = NEW_START_BLOCK; 41877855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size; 41887855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_SYSTEM; 41897855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "loader"); 41907855d95bSToomas Soome if (slice != NULL) 41917855d95bSToomas Soome *slice = 1; 41927855d95bSToomas Soome /* prepare slice 1 */ 41937855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1 - slice_size; 41947855d95bSToomas Soome slice_size -= resv; 41957855d95bSToomas Soome slice_size -= NEW_START_BLOCK; 41967855d95bSToomas Soome vtoc->efi_parts[1].p_start = start_block; 41977855d95bSToomas Soome vtoc->efi_parts[1].p_size = slice_size; 41987855d95bSToomas Soome vtoc->efi_parts[1].p_tag = V_USR; 41997855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[1].p_name, "zfs"); 42007855d95bSToomas Soome 42017855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block; 42027855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv; 42037855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED; 42047855d95bSToomas Soome } 42058488aeb5Staylor 42068488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 42078488aeb5Staylor /* 42088488aeb5Staylor * Some block drivers (like pcata) may not support EFI 42098488aeb5Staylor * GPT labels. Print out a helpful error message dir- 42108488aeb5Staylor * ecting the user to manually label the disk and give 42118488aeb5Staylor * a specific slice. 42128488aeb5Staylor */ 42138488aeb5Staylor (void) close(fd); 42148488aeb5Staylor efi_free(vtoc); 42158488aeb5Staylor 42168488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4217c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 42188488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 42198488aeb5Staylor } 42208488aeb5Staylor 42218488aeb5Staylor (void) close(fd); 42228488aeb5Staylor efi_free(vtoc); 42238488aeb5Staylor return (0); 42248488aeb5Staylor } 4225e7cbe64fSgw 4226e7cbe64fSgw static boolean_t 4227e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 4228e7cbe64fSgw { 4229e7cbe64fSgw char *type; 4230e7cbe64fSgw nvlist_t **child; 4231e7cbe64fSgw uint_t children, c; 4232e7cbe64fSgw 4233e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 4234810e43b2SBill Pijewski if (strcmp(type, VDEV_TYPE_FILE) == 0 || 423588ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 || 4236e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) { 4237e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4238e7cbe64fSgw "vdev type '%s' is not supported"), type); 4239e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 4240e7cbe64fSgw return (B_FALSE); 4241e7cbe64fSgw } 4242e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 4243e7cbe64fSgw &child, &children) == 0) { 4244e7cbe64fSgw for (c = 0; c < children; c++) { 4245e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 4246e7cbe64fSgw return (B_FALSE); 4247e7cbe64fSgw } 4248e7cbe64fSgw } 4249e7cbe64fSgw return (B_TRUE); 4250e7cbe64fSgw } 4251e7cbe64fSgw 4252e7cbe64fSgw /* 4253810e43b2SBill Pijewski * Check if this zvol is allowable for use as a dump device; zero if 4254810e43b2SBill Pijewski * it is, > 0 if it isn't, < 0 if it isn't a zvol. 4255810e43b2SBill Pijewski * 4256810e43b2SBill Pijewski * Allowable storage configurations include mirrors, all raidz variants, and 4257810e43b2SBill Pijewski * pools with log, cache, and spare devices. Pools which are backed by files or 4258810e43b2SBill Pijewski * have missing/hole vdevs are not suitable. 4259e7cbe64fSgw */ 4260e7cbe64fSgw int 4261e7cbe64fSgw zvol_check_dump_config(char *arg) 4262e7cbe64fSgw { 4263e7cbe64fSgw zpool_handle_t *zhp = NULL; 4264e7cbe64fSgw nvlist_t *config, *nvroot; 4265e7cbe64fSgw char *p, *volname; 4266e7cbe64fSgw nvlist_t **top; 4267e7cbe64fSgw uint_t toplevels; 4268e7cbe64fSgw libzfs_handle_t *hdl; 4269e7cbe64fSgw char errbuf[1024]; 42709adfa60dSMatthew Ahrens char poolname[ZFS_MAX_DATASET_NAME_LEN]; 4271e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR); 4272e7cbe64fSgw int ret = 1; 4273e7cbe64fSgw 4274e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 4275e7cbe64fSgw return (-1); 4276e7cbe64fSgw } 4277e7cbe64fSgw 4278e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4279e7cbe64fSgw "dump is not supported on device '%s'"), arg); 4280e7cbe64fSgw 4281e7cbe64fSgw if ((hdl = libzfs_init()) == NULL) 4282e7cbe64fSgw return (1); 4283e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE); 4284e7cbe64fSgw 4285e7cbe64fSgw volname = arg + pathlen; 4286e7cbe64fSgw 4287e7cbe64fSgw /* check the configuration of the pool */ 4288e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) { 4289e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4290e7cbe64fSgw "malformed dataset name")); 4291e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4292e7cbe64fSgw return (1); 42939adfa60dSMatthew Ahrens } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) { 4294e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4295e7cbe64fSgw "dataset name is too long")); 4296e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 4297e7cbe64fSgw return (1); 4298e7cbe64fSgw } else { 4299e7cbe64fSgw (void) strncpy(poolname, volname, p - volname); 4300e7cbe64fSgw poolname[p - volname] = '\0'; 4301e7cbe64fSgw } 4302e7cbe64fSgw 4303e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) { 4304e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4305e7cbe64fSgw "could not open pool '%s'"), poolname); 4306e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4307e7cbe64fSgw goto out; 4308e7cbe64fSgw } 4309e7cbe64fSgw config = zpool_get_config(zhp, NULL); 4310e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4311e7cbe64fSgw &nvroot) != 0) { 4312e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4313e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname); 4314e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 4315e7cbe64fSgw goto out; 4316e7cbe64fSgw } 4317e7cbe64fSgw 4318e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 4319e7cbe64fSgw &top, &toplevels) == 0); 4320e7cbe64fSgw 4321e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 4322e7cbe64fSgw goto out; 4323e7cbe64fSgw } 4324e7cbe64fSgw ret = 0; 4325e7cbe64fSgw 4326e7cbe64fSgw out: 4327e7cbe64fSgw if (zhp) 4328e7cbe64fSgw zpool_close(zhp); 4329e7cbe64fSgw libzfs_fini(hdl); 4330e7cbe64fSgw return (ret); 4331e7cbe64fSgw } 4332