1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 249a686fbcSPaul Dagnelie * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 25810e43b2SBill Pijewski * Copyright (c) 2013, Joyent, Inc. All rights reserved. 266401734dSWill Andrews * Copyright 2016 Nexenta Systems, Inc. 2788f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 28fa9e4066Sahrens */ 29fa9e4066Sahrens 30fa9e4066Sahrens #include <ctype.h> 31fa9e4066Sahrens #include <errno.h> 32fa9e4066Sahrens #include <devid.h> 33fa9e4066Sahrens #include <fcntl.h> 34fa9e4066Sahrens #include <libintl.h> 35fa9e4066Sahrens #include <stdio.h> 36fa9e4066Sahrens #include <stdlib.h> 37f3861e1aSahl #include <strings.h> 38fa9e4066Sahrens #include <unistd.h> 394445fffbSMatthew Ahrens #include <libgen.h> 408488aeb5Staylor #include <sys/efi_partition.h> 418488aeb5Staylor #include <sys/vtoc.h> 42fa9e4066Sahrens #include <sys/zfs_ioctl.h> 43573ca77eSGeorge Wilson #include <dlfcn.h> 44fa9e4066Sahrens 45fa9e4066Sahrens #include "zfs_namecheck.h" 46b1b8ab34Slling #include "zfs_prop.h" 47fa9e4066Sahrens #include "libzfs_impl.h" 48468c413aSTim Haley #include "zfs_comutil.h" 49ad135b5dSChristopher Siden #include "zfeature_common.h" 50fa9e4066Sahrens 51*7855d95bSToomas Soome static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *); 52990b4856Slling 53573ca77eSGeorge Wilson #define BACKUP_SLICE "s2" 54573ca77eSGeorge Wilson 55f9af39baSGeorge Wilson typedef struct prop_flags { 56f9af39baSGeorge Wilson int create:1; /* Validate property on creation */ 57f9af39baSGeorge Wilson int import:1; /* Validate property on import */ 58f9af39baSGeorge Wilson } prop_flags_t; 59f9af39baSGeorge Wilson 60990b4856Slling /* 61990b4856Slling * ==================================================================== 62990b4856Slling * zpool property functions 63990b4856Slling * ==================================================================== 64990b4856Slling */ 65990b4856Slling 66990b4856Slling static int 67990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 68990b4856Slling { 69990b4856Slling zfs_cmd_t zc = { 0 }; 70990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 71990b4856Slling 72990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 73990b4856Slling 74990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 75990b4856Slling return (-1); 76990b4856Slling 77990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 78990b4856Slling if (errno == ENOMEM) { 79990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 80990b4856Slling zcmd_free_nvlists(&zc); 81990b4856Slling return (-1); 82990b4856Slling } 83990b4856Slling } else { 84990b4856Slling zcmd_free_nvlists(&zc); 85990b4856Slling return (-1); 86990b4856Slling } 87990b4856Slling } 88990b4856Slling 89990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 90990b4856Slling zcmd_free_nvlists(&zc); 91990b4856Slling return (-1); 92990b4856Slling } 93990b4856Slling 94990b4856Slling zcmd_free_nvlists(&zc); 95990b4856Slling 96990b4856Slling return (0); 97990b4856Slling } 98990b4856Slling 99990b4856Slling static int 100990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 101990b4856Slling { 102990b4856Slling nvlist_t *old_props; 103990b4856Slling 104990b4856Slling old_props = zhp->zpool_props; 105990b4856Slling 106990b4856Slling if (zpool_get_all_props(zhp) != 0) 107990b4856Slling return (-1); 108990b4856Slling 109990b4856Slling nvlist_free(old_props); 110990b4856Slling return (0); 111990b4856Slling } 112990b4856Slling 113990b4856Slling static char * 114990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 115990b4856Slling zprop_source_t *src) 116990b4856Slling { 117990b4856Slling nvlist_t *nv, *nvl; 118990b4856Slling uint64_t ival; 119990b4856Slling char *value; 120990b4856Slling zprop_source_t source; 121990b4856Slling 122990b4856Slling nvl = zhp->zpool_props; 123990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 124990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 125990b4856Slling source = ival; 126990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 127990b4856Slling } else { 128990b4856Slling source = ZPROP_SRC_DEFAULT; 129990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 130990b4856Slling value = "-"; 131990b4856Slling } 132990b4856Slling 133990b4856Slling if (src) 134990b4856Slling *src = source; 135990b4856Slling 136990b4856Slling return (value); 137990b4856Slling } 138990b4856Slling 139990b4856Slling uint64_t 140990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 141990b4856Slling { 142990b4856Slling nvlist_t *nv, *nvl; 143990b4856Slling uint64_t value; 144990b4856Slling zprop_source_t source; 145990b4856Slling 146b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 147b87f3af3Sperrin /* 148b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 149b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 150b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 151b87f3af3Sperrin */ 152b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 153b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 154b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 155b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 156b87f3af3Sperrin == 0)) { 157b87f3af3Sperrin return (value); 158b87f3af3Sperrin } 159990b4856Slling return (zpool_prop_default_numeric(prop)); 160b87f3af3Sperrin } 161990b4856Slling 162990b4856Slling nvl = zhp->zpool_props; 163990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 164990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 165990b4856Slling source = value; 166990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 167990b4856Slling } else { 168990b4856Slling source = ZPROP_SRC_DEFAULT; 169990b4856Slling value = zpool_prop_default_numeric(prop); 170990b4856Slling } 171990b4856Slling 172990b4856Slling if (src) 173990b4856Slling *src = source; 174990b4856Slling 175990b4856Slling return (value); 176990b4856Slling } 177990b4856Slling 178990b4856Slling /* 179990b4856Slling * Map VDEV STATE to printed strings. 180990b4856Slling */ 1816401734dSWill Andrews const char * 182990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 183990b4856Slling { 184990b4856Slling switch (state) { 185990b4856Slling case VDEV_STATE_CLOSED: 186990b4856Slling case VDEV_STATE_OFFLINE: 187990b4856Slling return (gettext("OFFLINE")); 188990b4856Slling case VDEV_STATE_REMOVED: 189990b4856Slling return (gettext("REMOVED")); 190990b4856Slling case VDEV_STATE_CANT_OPEN: 191b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 192990b4856Slling return (gettext("FAULTED")); 1931195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL) 1941195e687SMark J Musante return (gettext("SPLIT")); 195990b4856Slling else 196990b4856Slling return (gettext("UNAVAIL")); 197990b4856Slling case VDEV_STATE_FAULTED: 198990b4856Slling return (gettext("FAULTED")); 199990b4856Slling case VDEV_STATE_DEGRADED: 200990b4856Slling return (gettext("DEGRADED")); 201990b4856Slling case VDEV_STATE_HEALTHY: 202990b4856Slling return (gettext("ONLINE")); 20388f61deeSIgor Kozhukhov 20488f61deeSIgor Kozhukhov default: 20588f61deeSIgor Kozhukhov break; 206990b4856Slling } 207990b4856Slling 208990b4856Slling return (gettext("UNKNOWN")); 209990b4856Slling } 210990b4856Slling 2116401734dSWill Andrews /* 2126401734dSWill Andrews * Map POOL STATE to printed strings. 2136401734dSWill Andrews */ 2146401734dSWill Andrews const char * 2156401734dSWill Andrews zpool_pool_state_to_name(pool_state_t state) 2166401734dSWill Andrews { 2176401734dSWill Andrews switch (state) { 2186401734dSWill Andrews case POOL_STATE_ACTIVE: 2196401734dSWill Andrews return (gettext("ACTIVE")); 2206401734dSWill Andrews case POOL_STATE_EXPORTED: 2216401734dSWill Andrews return (gettext("EXPORTED")); 2226401734dSWill Andrews case POOL_STATE_DESTROYED: 2236401734dSWill Andrews return (gettext("DESTROYED")); 2246401734dSWill Andrews case POOL_STATE_SPARE: 2256401734dSWill Andrews return (gettext("SPARE")); 2266401734dSWill Andrews case POOL_STATE_L2CACHE: 2276401734dSWill Andrews return (gettext("L2CACHE")); 2286401734dSWill Andrews case POOL_STATE_UNINITIALIZED: 2296401734dSWill Andrews return (gettext("UNINITIALIZED")); 2306401734dSWill Andrews case POOL_STATE_UNAVAIL: 2316401734dSWill Andrews return (gettext("UNAVAIL")); 2326401734dSWill Andrews case POOL_STATE_POTENTIALLY_ACTIVE: 2336401734dSWill Andrews return (gettext("POTENTIALLY_ACTIVE")); 2346401734dSWill Andrews } 2356401734dSWill Andrews 2366401734dSWill Andrews return (gettext("UNKNOWN")); 2376401734dSWill Andrews } 2386401734dSWill Andrews 239990b4856Slling /* 240990b4856Slling * Get a zpool property value for 'prop' and return the value in 241990b4856Slling * a pre-allocated buffer. 242990b4856Slling */ 243990b4856Slling int 244990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 245c58b3526SAdam Stevko zprop_source_t *srctype, boolean_t literal) 246990b4856Slling { 247990b4856Slling uint64_t intval; 248990b4856Slling const char *strval; 249990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 250990b4856Slling nvlist_t *nvroot; 251990b4856Slling vdev_stat_t *vs; 252990b4856Slling uint_t vsc; 253990b4856Slling 254990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 255379c004dSEric Schrock switch (prop) { 256379c004dSEric Schrock case ZPOOL_PROP_NAME: 257990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 258379c004dSEric Schrock break; 259379c004dSEric Schrock 260379c004dSEric Schrock case ZPOOL_PROP_HEALTH: 261990b4856Slling (void) strlcpy(buf, "FAULTED", len); 262379c004dSEric Schrock break; 263379c004dSEric Schrock 264379c004dSEric Schrock case ZPOOL_PROP_GUID: 265379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src); 266379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval); 267379c004dSEric Schrock break; 268379c004dSEric Schrock 269379c004dSEric Schrock case ZPOOL_PROP_ALTROOT: 270379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE: 2718704186eSDan McDonald case ZPOOL_PROP_COMMENT: 272379c004dSEric Schrock if (zhp->zpool_props != NULL || 273379c004dSEric Schrock zpool_get_all_props(zhp) == 0) { 274379c004dSEric Schrock (void) strlcpy(buf, 275379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src), 276379c004dSEric Schrock len); 277c58b3526SAdam Stevko break; 278379c004dSEric Schrock } 279379c004dSEric Schrock /* FALLTHROUGH */ 280379c004dSEric Schrock default: 281990b4856Slling (void) strlcpy(buf, "-", len); 282379c004dSEric Schrock break; 283379c004dSEric Schrock } 284379c004dSEric Schrock 285379c004dSEric Schrock if (srctype != NULL) 286379c004dSEric Schrock *srctype = src; 287990b4856Slling return (0); 288990b4856Slling } 289990b4856Slling 290990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 291990b4856Slling prop != ZPOOL_PROP_NAME) 292990b4856Slling return (-1); 293990b4856Slling 294990b4856Slling switch (zpool_prop_get_type(prop)) { 295990b4856Slling case PROP_TYPE_STRING: 296990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 297990b4856Slling len); 298990b4856Slling break; 299990b4856Slling 300990b4856Slling case PROP_TYPE_NUMBER: 301990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 302990b4856Slling 303990b4856Slling switch (prop) { 304990b4856Slling case ZPOOL_PROP_SIZE: 305485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED: 306485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE: 307ad135b5dSChristopher Siden case ZPOOL_PROP_FREEING: 3087fd05ac4SMatthew Ahrens case ZPOOL_PROP_LEAKED: 309c58b3526SAdam Stevko if (literal) { 310c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 311c58b3526SAdam Stevko (u_longlong_t)intval); 312c58b3526SAdam Stevko } else { 313c58b3526SAdam Stevko (void) zfs_nicenum(intval, buf, len); 314c58b3526SAdam Stevko } 315990b4856Slling break; 316*7855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE: 3177a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ: 3187a09f97bSGeorge Wilson if (intval == 0) { 3197a09f97bSGeorge Wilson (void) strlcpy(buf, "-", len); 3207a09f97bSGeorge Wilson } else if (literal) { 3217a09f97bSGeorge Wilson (void) snprintf(buf, len, "%llu", 3227a09f97bSGeorge Wilson (u_longlong_t)intval); 3237a09f97bSGeorge Wilson } else { 3247a09f97bSGeorge Wilson (void) zfs_nicenum(intval, buf, len); 3257a09f97bSGeorge Wilson } 3267a09f97bSGeorge Wilson break; 327990b4856Slling case ZPOOL_PROP_CAPACITY: 328c58b3526SAdam Stevko if (literal) { 329c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 330c58b3526SAdam Stevko (u_longlong_t)intval); 331c58b3526SAdam Stevko } else { 332c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu%%", 333c58b3526SAdam Stevko (u_longlong_t)intval); 334c58b3526SAdam Stevko } 335990b4856Slling break; 3362e4c9986SGeorge Wilson case ZPOOL_PROP_FRAGMENTATION: 3372e4c9986SGeorge Wilson if (intval == UINT64_MAX) { 3382e4c9986SGeorge Wilson (void) strlcpy(buf, "-", len); 3392e4c9986SGeorge Wilson } else { 3402e4c9986SGeorge Wilson (void) snprintf(buf, len, "%llu%%", 3412e4c9986SGeorge Wilson (u_longlong_t)intval); 3422e4c9986SGeorge Wilson } 3432e4c9986SGeorge Wilson break; 344b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO: 345b24ab676SJeff Bonwick (void) snprintf(buf, len, "%llu.%02llux", 346b24ab676SJeff Bonwick (u_longlong_t)(intval / 100), 347b24ab676SJeff Bonwick (u_longlong_t)(intval % 100)); 348b24ab676SJeff Bonwick break; 349990b4856Slling case ZPOOL_PROP_HEALTH: 350990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 351990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 352990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 3533f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 3543f9d6ad7SLin Ling == 0); 355990b4856Slling 356990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 357990b4856Slling vs->vs_aux), len); 358990b4856Slling break; 359ad135b5dSChristopher Siden case ZPOOL_PROP_VERSION: 360ad135b5dSChristopher Siden if (intval >= SPA_VERSION_FEATURES) { 361ad135b5dSChristopher Siden (void) snprintf(buf, len, "-"); 362ad135b5dSChristopher Siden break; 363ad135b5dSChristopher Siden } 364ad135b5dSChristopher Siden /* FALLTHROUGH */ 365990b4856Slling default: 366990b4856Slling (void) snprintf(buf, len, "%llu", intval); 367990b4856Slling } 368990b4856Slling break; 369990b4856Slling 370990b4856Slling case PROP_TYPE_INDEX: 371990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 372990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 373990b4856Slling != 0) 374990b4856Slling return (-1); 375990b4856Slling (void) strlcpy(buf, strval, len); 376990b4856Slling break; 377990b4856Slling 378990b4856Slling default: 379990b4856Slling abort(); 380990b4856Slling } 381990b4856Slling 382990b4856Slling if (srctype) 383990b4856Slling *srctype = src; 384990b4856Slling 385990b4856Slling return (0); 386990b4856Slling } 387990b4856Slling 388990b4856Slling /* 389990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 390990b4856Slling * Assuming bootfs is a valid dataset name. 391990b4856Slling */ 392990b4856Slling static boolean_t 393990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 394990b4856Slling { 395990b4856Slling int len = strlen(pool); 396990b4856Slling 397fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 398990b4856Slling return (B_FALSE); 399990b4856Slling 400990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 401990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 402990b4856Slling return (B_TRUE); 403990b4856Slling 404990b4856Slling return (B_FALSE); 405990b4856Slling } 406990b4856Slling 4074263d13fSGeorge Wilson boolean_t 4084263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp) 409b5b76fecSGeorge Wilson { 4109adfa60dSMatthew Ahrens char bootfs[ZFS_MAX_DATASET_NAME_LEN]; 411b5b76fecSGeorge Wilson 412b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 413c58b3526SAdam Stevko sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-", 414b5b76fecSGeorge Wilson sizeof (bootfs)) != 0); 415b5b76fecSGeorge Wilson } 416b5b76fecSGeorge Wilson 417b5b76fecSGeorge Wilson 418990b4856Slling /* 419990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 420990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 421990b4856Slling * specified as strings. 422990b4856Slling */ 423990b4856Slling static nvlist_t * 4240a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 425f9af39baSGeorge Wilson nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf) 426990b4856Slling { 427990b4856Slling nvpair_t *elem; 428990b4856Slling nvlist_t *retprops; 429990b4856Slling zpool_prop_t prop; 430990b4856Slling char *strval; 431990b4856Slling uint64_t intval; 4328704186eSDan McDonald char *slash, *check; 4332f8aaab3Seschrock struct stat64 statbuf; 43415e6edf1Sgw zpool_handle_t *zhp; 435990b4856Slling 436990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 437990b4856Slling (void) no_memory(hdl); 438990b4856Slling return (NULL); 439990b4856Slling } 440990b4856Slling 441990b4856Slling elem = NULL; 442990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 443990b4856Slling const char *propname = nvpair_name(elem); 444990b4856Slling 445ad135b5dSChristopher Siden prop = zpool_name_to_prop(propname); 446ad135b5dSChristopher Siden if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) { 447ad135b5dSChristopher Siden int err; 448ad135b5dSChristopher Siden char *fname = strchr(propname, '@') + 1; 449ad135b5dSChristopher Siden 4502acef22dSMatthew Ahrens err = zfeature_lookup_name(fname, NULL); 451ad135b5dSChristopher Siden if (err != 0) { 452ad135b5dSChristopher Siden ASSERT3U(err, ==, ENOENT); 453ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 454ad135b5dSChristopher Siden "invalid feature '%s'"), fname); 455ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 456ad135b5dSChristopher Siden goto error; 457ad135b5dSChristopher Siden } 458ad135b5dSChristopher Siden 459ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_STRING) { 460ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 461ad135b5dSChristopher Siden "'%s' must be a string"), propname); 462ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 463ad135b5dSChristopher Siden goto error; 464ad135b5dSChristopher Siden } 465ad135b5dSChristopher Siden 466ad135b5dSChristopher Siden (void) nvpair_value_string(elem, &strval); 467ad135b5dSChristopher Siden if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) { 468ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 469ad135b5dSChristopher Siden "property '%s' can only be set to " 470ad135b5dSChristopher Siden "'enabled'"), propname); 471ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 472ad135b5dSChristopher Siden goto error; 473ad135b5dSChristopher Siden } 474ad135b5dSChristopher Siden 475ad135b5dSChristopher Siden if (nvlist_add_uint64(retprops, propname, 0) != 0) { 476ad135b5dSChristopher Siden (void) no_memory(hdl); 477ad135b5dSChristopher Siden goto error; 478ad135b5dSChristopher Siden } 479ad135b5dSChristopher Siden continue; 480ad135b5dSChristopher Siden } 481ad135b5dSChristopher Siden 482990b4856Slling /* 483990b4856Slling * Make sure this property is valid and applies to this type. 484990b4856Slling */ 485ad135b5dSChristopher Siden if (prop == ZPROP_INVAL) { 486990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 487990b4856Slling "invalid property '%s'"), propname); 488990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 489990b4856Slling goto error; 490990b4856Slling } 491990b4856Slling 492990b4856Slling if (zpool_prop_readonly(prop)) { 493990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 494990b4856Slling "is readonly"), propname); 495990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 496990b4856Slling goto error; 497990b4856Slling } 498990b4856Slling 499990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 500990b4856Slling &strval, &intval, errbuf) != 0) 501990b4856Slling goto error; 502990b4856Slling 503990b4856Slling /* 504990b4856Slling * Perform additional checking for specific properties. 505990b4856Slling */ 506990b4856Slling switch (prop) { 507990b4856Slling case ZPOOL_PROP_VERSION: 508ad135b5dSChristopher Siden if (intval < version || 509ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(intval)) { 510990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 511990b4856Slling "property '%s' number %d is invalid."), 512990b4856Slling propname, intval); 513990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 514990b4856Slling goto error; 515990b4856Slling } 516990b4856Slling break; 517990b4856Slling 518*7855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE: 519*7855d95bSToomas Soome if (!flags.create) { 520*7855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 521*7855d95bSToomas Soome "property '%s' can only be set during pool " 522*7855d95bSToomas Soome "creation"), propname); 523*7855d95bSToomas Soome (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 524*7855d95bSToomas Soome goto error; 525*7855d95bSToomas Soome } 526*7855d95bSToomas Soome break; 527*7855d95bSToomas Soome 528990b4856Slling case ZPOOL_PROP_BOOTFS: 529f9af39baSGeorge Wilson if (flags.create || flags.import) { 530990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 531990b4856Slling "property '%s' cannot be set at creation " 532990b4856Slling "or import time"), propname); 533990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 534990b4856Slling goto error; 535990b4856Slling } 536990b4856Slling 537990b4856Slling if (version < SPA_VERSION_BOOTFS) { 538990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 539990b4856Slling "pool must be upgraded to support " 540990b4856Slling "'%s' property"), propname); 541990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 542990b4856Slling goto error; 543990b4856Slling } 544990b4856Slling 545990b4856Slling /* 546990b4856Slling * bootfs property value has to be a dataset name and 547990b4856Slling * the dataset has to be in the same pool as it sets to. 548990b4856Slling */ 549990b4856Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 550990b4856Slling strval)) { 551990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 552990b4856Slling "is an invalid name"), strval); 553990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 554990b4856Slling goto error; 555990b4856Slling } 55615e6edf1Sgw 55715e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 55815e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 55915e6edf1Sgw "could not open pool '%s'"), poolname); 56015e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 56115e6edf1Sgw goto error; 56215e6edf1Sgw } 56315e6edf1Sgw zpool_close(zhp); 564990b4856Slling break; 565990b4856Slling 5662f8aaab3Seschrock case ZPOOL_PROP_ALTROOT: 567f9af39baSGeorge Wilson if (!flags.create && !flags.import) { 568990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 569990b4856Slling "property '%s' can only be set during pool " 570990b4856Slling "creation or import"), propname); 571990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 572990b4856Slling goto error; 573990b4856Slling } 574990b4856Slling 5752f8aaab3Seschrock if (strval[0] != '/') { 576990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5772f8aaab3Seschrock "bad alternate root '%s'"), strval); 5782f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 579990b4856Slling goto error; 580990b4856Slling } 5812f8aaab3Seschrock break; 5822f8aaab3Seschrock 5832f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5842f8aaab3Seschrock if (strval[0] == '\0') 5852f8aaab3Seschrock break; 5862f8aaab3Seschrock 5872f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5882f8aaab3Seschrock break; 589990b4856Slling 590990b4856Slling if (strval[0] != '/') { 591990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5922f8aaab3Seschrock "property '%s' must be empty, an " 5932f8aaab3Seschrock "absolute path, or 'none'"), propname); 594990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 595990b4856Slling goto error; 596990b4856Slling } 597990b4856Slling 5982f8aaab3Seschrock slash = strrchr(strval, '/'); 599990b4856Slling 6002f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 6012f8aaab3Seschrock strcmp(slash, "/..") == 0) { 6022f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6032f8aaab3Seschrock "'%s' is not a valid file"), strval); 6042f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6052f8aaab3Seschrock goto error; 6062f8aaab3Seschrock } 607990b4856Slling 6082f8aaab3Seschrock *slash = '\0'; 6092f8aaab3Seschrock 6102c32020fSeschrock if (strval[0] != '\0' && 6112c32020fSeschrock (stat64(strval, &statbuf) != 0 || 6122c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 6132f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6142f8aaab3Seschrock "'%s' is not a valid directory"), 6152f8aaab3Seschrock strval); 6162f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6172f8aaab3Seschrock goto error; 6182f8aaab3Seschrock } 6192f8aaab3Seschrock 6202f8aaab3Seschrock *slash = '/'; 6212f8aaab3Seschrock break; 622f9af39baSGeorge Wilson 6238704186eSDan McDonald case ZPOOL_PROP_COMMENT: 6248704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 6258704186eSDan McDonald if (!isprint(*check)) { 6268704186eSDan McDonald zfs_error_aux(hdl, 6278704186eSDan McDonald dgettext(TEXT_DOMAIN, 6288704186eSDan McDonald "comment may only have printable " 6298704186eSDan McDonald "characters")); 6308704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, 6318704186eSDan McDonald errbuf); 6328704186eSDan McDonald goto error; 6338704186eSDan McDonald } 6348704186eSDan McDonald } 6358704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) { 6368704186eSDan McDonald zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6378704186eSDan McDonald "comment must not exceed %d characters"), 6388704186eSDan McDonald ZPROP_MAX_COMMENT); 6398704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6408704186eSDan McDonald goto error; 6418704186eSDan McDonald } 6428704186eSDan McDonald break; 643f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 644f9af39baSGeorge Wilson if (!flags.import) { 645f9af39baSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 646f9af39baSGeorge Wilson "property '%s' can only be set at " 647f9af39baSGeorge Wilson "import time"), propname); 648f9af39baSGeorge Wilson (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 649f9af39baSGeorge Wilson goto error; 650f9af39baSGeorge Wilson } 651f9af39baSGeorge Wilson break; 65288f61deeSIgor Kozhukhov 65388f61deeSIgor Kozhukhov default: 65488f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 65588f61deeSIgor Kozhukhov "property '%s'(%d) not defined"), propname, prop); 65688f61deeSIgor Kozhukhov break; 657990b4856Slling } 658990b4856Slling } 659990b4856Slling 660990b4856Slling return (retprops); 661990b4856Slling error: 662990b4856Slling nvlist_free(retprops); 663990b4856Slling return (NULL); 664990b4856Slling } 665990b4856Slling 666990b4856Slling /* 667990b4856Slling * Set zpool property : propname=propval. 668990b4856Slling */ 669990b4856Slling int 670990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 671990b4856Slling { 672990b4856Slling zfs_cmd_t zc = { 0 }; 673990b4856Slling int ret = -1; 674990b4856Slling char errbuf[1024]; 675990b4856Slling nvlist_t *nvl = NULL; 676990b4856Slling nvlist_t *realprops; 677990b4856Slling uint64_t version; 678f9af39baSGeorge Wilson prop_flags_t flags = { 0 }; 679990b4856Slling 680990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 681990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 682990b4856Slling zhp->zpool_name); 683990b4856Slling 684990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 685990b4856Slling return (no_memory(zhp->zpool_hdl)); 686990b4856Slling 687990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 688990b4856Slling nvlist_free(nvl); 689990b4856Slling return (no_memory(zhp->zpool_hdl)); 690990b4856Slling } 691990b4856Slling 692990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 6930a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 694f9af39baSGeorge Wilson zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) { 695990b4856Slling nvlist_free(nvl); 696990b4856Slling return (-1); 697990b4856Slling } 698990b4856Slling 699990b4856Slling nvlist_free(nvl); 700990b4856Slling nvl = realprops; 701990b4856Slling 702990b4856Slling /* 703990b4856Slling * Execute the corresponding ioctl() to set this property. 704990b4856Slling */ 705990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 706990b4856Slling 707990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 708990b4856Slling nvlist_free(nvl); 709990b4856Slling return (-1); 710990b4856Slling } 711990b4856Slling 712990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 713990b4856Slling 714990b4856Slling zcmd_free_nvlists(&zc); 715990b4856Slling nvlist_free(nvl); 716990b4856Slling 717990b4856Slling if (ret) 718990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 719990b4856Slling else 720990b4856Slling (void) zpool_props_refresh(zhp); 721990b4856Slling 722990b4856Slling return (ret); 723990b4856Slling } 724990b4856Slling 725990b4856Slling int 726990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 727990b4856Slling { 728990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 729990b4856Slling zprop_list_t *entry; 730990b4856Slling char buf[ZFS_MAXPROPLEN]; 731ad135b5dSChristopher Siden nvlist_t *features = NULL; 732ad135b5dSChristopher Siden zprop_list_t **last; 733ad135b5dSChristopher Siden boolean_t firstexpand = (NULL == *plp); 734990b4856Slling 735990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 736990b4856Slling return (-1); 737990b4856Slling 738ad135b5dSChristopher Siden last = plp; 739ad135b5dSChristopher Siden while (*last != NULL) 740ad135b5dSChristopher Siden last = &(*last)->pl_next; 741ad135b5dSChristopher Siden 742ad135b5dSChristopher Siden if ((*plp)->pl_all) 743ad135b5dSChristopher Siden features = zpool_get_features(zhp); 744ad135b5dSChristopher Siden 745ad135b5dSChristopher Siden if ((*plp)->pl_all && firstexpand) { 746ad135b5dSChristopher Siden for (int i = 0; i < SPA_FEATURES; i++) { 747ad135b5dSChristopher Siden zprop_list_t *entry = zfs_alloc(hdl, 748ad135b5dSChristopher Siden sizeof (zprop_list_t)); 749ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 750ad135b5dSChristopher Siden entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s", 751ad135b5dSChristopher Siden spa_feature_table[i].fi_uname); 752ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 753ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 754ad135b5dSChristopher Siden 755ad135b5dSChristopher Siden *last = entry; 756ad135b5dSChristopher Siden last = &entry->pl_next; 757ad135b5dSChristopher Siden } 758ad135b5dSChristopher Siden } 759ad135b5dSChristopher Siden 760ad135b5dSChristopher Siden /* add any unsupported features */ 761ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL); 762ad135b5dSChristopher Siden nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) { 763ad135b5dSChristopher Siden char *propname; 764ad135b5dSChristopher Siden boolean_t found; 765ad135b5dSChristopher Siden zprop_list_t *entry; 766ad135b5dSChristopher Siden 767ad135b5dSChristopher Siden if (zfeature_is_supported(nvpair_name(nvp))) 768ad135b5dSChristopher Siden continue; 769ad135b5dSChristopher Siden 770ad135b5dSChristopher Siden propname = zfs_asprintf(hdl, "unsupported@%s", 771ad135b5dSChristopher Siden nvpair_name(nvp)); 772ad135b5dSChristopher Siden 773ad135b5dSChristopher Siden /* 774ad135b5dSChristopher Siden * Before adding the property to the list make sure that no 775ad135b5dSChristopher Siden * other pool already added the same property. 776ad135b5dSChristopher Siden */ 777ad135b5dSChristopher Siden found = B_FALSE; 778ad135b5dSChristopher Siden entry = *plp; 779ad135b5dSChristopher Siden while (entry != NULL) { 780ad135b5dSChristopher Siden if (entry->pl_user_prop != NULL && 781ad135b5dSChristopher Siden strcmp(propname, entry->pl_user_prop) == 0) { 782ad135b5dSChristopher Siden found = B_TRUE; 783ad135b5dSChristopher Siden break; 784ad135b5dSChristopher Siden } 785ad135b5dSChristopher Siden entry = entry->pl_next; 786ad135b5dSChristopher Siden } 787ad135b5dSChristopher Siden if (found) { 788ad135b5dSChristopher Siden free(propname); 789ad135b5dSChristopher Siden continue; 790ad135b5dSChristopher Siden } 791ad135b5dSChristopher Siden 792ad135b5dSChristopher Siden entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 793ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 794ad135b5dSChristopher Siden entry->pl_user_prop = propname; 795ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 796ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 797ad135b5dSChristopher Siden 798ad135b5dSChristopher Siden *last = entry; 799ad135b5dSChristopher Siden last = &entry->pl_next; 800ad135b5dSChristopher Siden } 801ad135b5dSChristopher Siden 802990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 803990b4856Slling 804990b4856Slling if (entry->pl_fixed) 805990b4856Slling continue; 806990b4856Slling 807990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 808990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 809c58b3526SAdam Stevko NULL, B_FALSE) == 0) { 810990b4856Slling if (strlen(buf) > entry->pl_width) 811990b4856Slling entry->pl_width = strlen(buf); 812990b4856Slling } 813990b4856Slling } 814990b4856Slling 815990b4856Slling return (0); 816990b4856Slling } 817990b4856Slling 818ad135b5dSChristopher Siden /* 819ad135b5dSChristopher Siden * Get the state for the given feature on the given ZFS pool. 820ad135b5dSChristopher Siden */ 821ad135b5dSChristopher Siden int 822ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf, 823ad135b5dSChristopher Siden size_t len) 824ad135b5dSChristopher Siden { 825ad135b5dSChristopher Siden uint64_t refcount; 826ad135b5dSChristopher Siden boolean_t found = B_FALSE; 827ad135b5dSChristopher Siden nvlist_t *features = zpool_get_features(zhp); 828ad135b5dSChristopher Siden boolean_t supported; 829ad135b5dSChristopher Siden const char *feature = strchr(propname, '@') + 1; 830ad135b5dSChristopher Siden 831ad135b5dSChristopher Siden supported = zpool_prop_feature(propname); 8327c13517fSSerapheim Dimitropoulos ASSERT(supported || zpool_prop_unsupported(propname)); 833ad135b5dSChristopher Siden 834ad135b5dSChristopher Siden /* 835ad135b5dSChristopher Siden * Convert from feature name to feature guid. This conversion is 836ad135b5dSChristopher Siden * unecessary for unsupported@... properties because they already 837ad135b5dSChristopher Siden * use guids. 838ad135b5dSChristopher Siden */ 839ad135b5dSChristopher Siden if (supported) { 840ad135b5dSChristopher Siden int ret; 8412acef22dSMatthew Ahrens spa_feature_t fid; 842ad135b5dSChristopher Siden 8432acef22dSMatthew Ahrens ret = zfeature_lookup_name(feature, &fid); 844ad135b5dSChristopher Siden if (ret != 0) { 845ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 846ad135b5dSChristopher Siden return (ENOTSUP); 847ad135b5dSChristopher Siden } 8482acef22dSMatthew Ahrens feature = spa_feature_table[fid].fi_guid; 849ad135b5dSChristopher Siden } 850ad135b5dSChristopher Siden 851ad135b5dSChristopher Siden if (nvlist_lookup_uint64(features, feature, &refcount) == 0) 852ad135b5dSChristopher Siden found = B_TRUE; 853ad135b5dSChristopher Siden 854ad135b5dSChristopher Siden if (supported) { 855ad135b5dSChristopher Siden if (!found) { 856ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len); 857ad135b5dSChristopher Siden } else { 858ad135b5dSChristopher Siden if (refcount == 0) 859ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len); 860ad135b5dSChristopher Siden else 861ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len); 862ad135b5dSChristopher Siden } 863ad135b5dSChristopher Siden } else { 864ad135b5dSChristopher Siden if (found) { 865ad135b5dSChristopher Siden if (refcount == 0) { 866ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE); 867ad135b5dSChristopher Siden } else { 868ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY); 869ad135b5dSChristopher Siden } 870ad135b5dSChristopher Siden } else { 871ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 872ad135b5dSChristopher Siden return (ENOTSUP); 873ad135b5dSChristopher Siden } 874ad135b5dSChristopher Siden } 875ad135b5dSChristopher Siden 876ad135b5dSChristopher Siden return (0); 877ad135b5dSChristopher Siden } 878990b4856Slling 879573ca77eSGeorge Wilson /* 880573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage 881573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead. 882573ca77eSGeorge Wilson */ 883573ca77eSGeorge Wilson #define NEW_START_BLOCK 256 884573ca77eSGeorge Wilson 885fa9e4066Sahrens /* 886fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 887fa9e4066Sahrens * 'buf'. 888fa9e4066Sahrens */ 889e7cbe64fSgw boolean_t 89099653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 891fa9e4066Sahrens { 892fa9e4066Sahrens namecheck_err_t why; 893fa9e4066Sahrens char what; 894b468a217Seschrock int ret; 895b468a217Seschrock 896b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 897b468a217Seschrock 898b468a217Seschrock /* 899b468a217Seschrock * The rules for reserved pool names were extended at a later point. 900b468a217Seschrock * But we need to support users with existing pools that may now be 901b468a217Seschrock * invalid. So we only check for this expanded set of names during a 902b468a217Seschrock * create (or import), and only in userland. 903b468a217Seschrock */ 904b468a217Seschrock if (ret == 0 && !isopen && 905b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 906b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 9078654d025Sperrin strncmp(pool, "spare", 5) == 0 || 9088654d025Sperrin strcmp(pool, "log") == 0)) { 909e7cbe64fSgw if (hdl != NULL) 910e7cbe64fSgw zfs_error_aux(hdl, 911e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved")); 91299653d4eSeschrock return (B_FALSE); 913b468a217Seschrock } 914b468a217Seschrock 915fa9e4066Sahrens 916b468a217Seschrock if (ret != 0) { 91799653d4eSeschrock if (hdl != NULL) { 918fa9e4066Sahrens switch (why) { 919b81d61a6Slling case NAME_ERR_TOOLONG: 92099653d4eSeschrock zfs_error_aux(hdl, 921b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 922b81d61a6Slling break; 923b81d61a6Slling 924fa9e4066Sahrens case NAME_ERR_INVALCHAR: 92599653d4eSeschrock zfs_error_aux(hdl, 926fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 927fa9e4066Sahrens "'%c' in pool name"), what); 928fa9e4066Sahrens break; 929fa9e4066Sahrens 930fa9e4066Sahrens case NAME_ERR_NOLETTER: 93199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 93299653d4eSeschrock "name must begin with a letter")); 933fa9e4066Sahrens break; 934fa9e4066Sahrens 935fa9e4066Sahrens case NAME_ERR_RESERVED: 93699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 93799653d4eSeschrock "name is reserved")); 938fa9e4066Sahrens break; 939fa9e4066Sahrens 940fa9e4066Sahrens case NAME_ERR_DISKLIKE: 94199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 94299653d4eSeschrock "pool name is reserved")); 943fa9e4066Sahrens break; 9445ad82045Snd 9455ad82045Snd case NAME_ERR_LEADING_SLASH: 9465ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9475ad82045Snd "leading slash in name")); 9485ad82045Snd break; 9495ad82045Snd 9505ad82045Snd case NAME_ERR_EMPTY_COMPONENT: 9515ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9525ad82045Snd "empty component in name")); 9535ad82045Snd break; 9545ad82045Snd 9555ad82045Snd case NAME_ERR_TRAILING_SLASH: 9565ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9575ad82045Snd "trailing slash in name")); 9585ad82045Snd break; 9595ad82045Snd 960edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS: 9615ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 962edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in " 963edb901aaSMarcel Telka "name")); 9645ad82045Snd break; 9655ad82045Snd 96688f61deeSIgor Kozhukhov default: 96788f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 96888f61deeSIgor Kozhukhov "(%d) not defined"), why); 96988f61deeSIgor Kozhukhov break; 970fa9e4066Sahrens } 971fa9e4066Sahrens } 97299653d4eSeschrock return (B_FALSE); 973fa9e4066Sahrens } 974fa9e4066Sahrens 97599653d4eSeschrock return (B_TRUE); 976fa9e4066Sahrens } 977fa9e4066Sahrens 978fa9e4066Sahrens /* 979fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 980fa9e4066Sahrens * state. 981fa9e4066Sahrens */ 982fa9e4066Sahrens zpool_handle_t * 98399653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 984fa9e4066Sahrens { 985fa9e4066Sahrens zpool_handle_t *zhp; 98694de1d4cSeschrock boolean_t missing; 987fa9e4066Sahrens 988fa9e4066Sahrens /* 989fa9e4066Sahrens * Make sure the pool name is valid. 990fa9e4066Sahrens */ 99199653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 992ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 99399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 99499653d4eSeschrock pool); 995fa9e4066Sahrens return (NULL); 996fa9e4066Sahrens } 997fa9e4066Sahrens 99899653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 99999653d4eSeschrock return (NULL); 1000fa9e4066Sahrens 100199653d4eSeschrock zhp->zpool_hdl = hdl; 1002fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1003fa9e4066Sahrens 100494de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 100594de1d4cSeschrock zpool_close(zhp); 100694de1d4cSeschrock return (NULL); 100794de1d4cSeschrock } 100894de1d4cSeschrock 100994de1d4cSeschrock if (missing) { 1010990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 1011ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 1012990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 101394de1d4cSeschrock zpool_close(zhp); 101494de1d4cSeschrock return (NULL); 1015fa9e4066Sahrens } 1016fa9e4066Sahrens 1017fa9e4066Sahrens return (zhp); 1018fa9e4066Sahrens } 1019fa9e4066Sahrens 1020fa9e4066Sahrens /* 1021fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 1022fa9e4066Sahrens * the configuration cache may be out of date). 1023fa9e4066Sahrens */ 102494de1d4cSeschrock int 102594de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 1026fa9e4066Sahrens { 1027fa9e4066Sahrens zpool_handle_t *zhp; 102894de1d4cSeschrock boolean_t missing; 1029fa9e4066Sahrens 103094de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 103194de1d4cSeschrock return (-1); 1032fa9e4066Sahrens 103399653d4eSeschrock zhp->zpool_hdl = hdl; 1034fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1035fa9e4066Sahrens 103694de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 103794de1d4cSeschrock zpool_close(zhp); 103894de1d4cSeschrock return (-1); 1039fa9e4066Sahrens } 1040fa9e4066Sahrens 104194de1d4cSeschrock if (missing) { 104294de1d4cSeschrock zpool_close(zhp); 104394de1d4cSeschrock *ret = NULL; 104494de1d4cSeschrock return (0); 104594de1d4cSeschrock } 104694de1d4cSeschrock 104794de1d4cSeschrock *ret = zhp; 104894de1d4cSeschrock return (0); 1049fa9e4066Sahrens } 1050fa9e4066Sahrens 1051fa9e4066Sahrens /* 1052fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 1053fa9e4066Sahrens * state. 1054fa9e4066Sahrens */ 1055fa9e4066Sahrens zpool_handle_t * 105699653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 1057fa9e4066Sahrens { 1058fa9e4066Sahrens zpool_handle_t *zhp; 1059fa9e4066Sahrens 106099653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 1061fa9e4066Sahrens return (NULL); 1062fa9e4066Sahrens 1063fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 1064ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 106599653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 1066fa9e4066Sahrens zpool_close(zhp); 1067fa9e4066Sahrens return (NULL); 1068fa9e4066Sahrens } 1069fa9e4066Sahrens 1070fa9e4066Sahrens return (zhp); 1071fa9e4066Sahrens } 1072fa9e4066Sahrens 1073fa9e4066Sahrens /* 1074fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 1075fa9e4066Sahrens */ 1076fa9e4066Sahrens void 1077fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 1078fa9e4066Sahrens { 1079aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_config); 1080aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_old_config); 1081aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_props); 1082fa9e4066Sahrens free(zhp); 1083fa9e4066Sahrens } 1084fa9e4066Sahrens 1085fa9e4066Sahrens /* 1086fa9e4066Sahrens * Return the name of the pool. 1087fa9e4066Sahrens */ 1088fa9e4066Sahrens const char * 1089fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 1090fa9e4066Sahrens { 1091fa9e4066Sahrens return (zhp->zpool_name); 1092fa9e4066Sahrens } 1093fa9e4066Sahrens 1094fa9e4066Sahrens 1095fa9e4066Sahrens /* 1096fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 1097fa9e4066Sahrens */ 1098fa9e4066Sahrens int 1099fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 1100fa9e4066Sahrens { 1101fa9e4066Sahrens return (zhp->zpool_state); 1102fa9e4066Sahrens } 1103fa9e4066Sahrens 1104fa9e4066Sahrens /* 1105fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 1106fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 1107fa9e4066Sahrens * don't have to worry about error semantics. 1108fa9e4066Sahrens */ 1109fa9e4066Sahrens int 111099653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 11110a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 1112fa9e4066Sahrens { 1113fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 11140a48a24eStimh nvlist_t *zc_fsprops = NULL; 11150a48a24eStimh nvlist_t *zc_props = NULL; 111699653d4eSeschrock char msg[1024]; 11170a48a24eStimh int ret = -1; 1118fa9e4066Sahrens 111999653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 112099653d4eSeschrock "cannot create '%s'"), pool); 1121fa9e4066Sahrens 112299653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 112399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 1124fa9e4066Sahrens 1125351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1126990b4856Slling return (-1); 1127fa9e4066Sahrens 11280a48a24eStimh if (props) { 1129f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE }; 1130f9af39baSGeorge Wilson 11310a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 1132f9af39baSGeorge Wilson SPA_VERSION_1, flags, msg)) == NULL) { 11330a48a24eStimh goto create_failed; 11340a48a24eStimh } 11350a48a24eStimh } 113699653d4eSeschrock 11370a48a24eStimh if (fsprops) { 11380a48a24eStimh uint64_t zoned; 11390a48a24eStimh char *zonestr; 11400a48a24eStimh 11410a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 11420a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 11430a48a24eStimh strcmp(zonestr, "on") == 0); 11440a48a24eStimh 1145e9316f76SJoe Stein if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM, 1146e9316f76SJoe Stein fsprops, zoned, NULL, NULL, msg)) == NULL) { 11470a48a24eStimh goto create_failed; 11480a48a24eStimh } 11490a48a24eStimh if (!zc_props && 11500a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 11510a48a24eStimh goto create_failed; 11520a48a24eStimh } 11530a48a24eStimh if (nvlist_add_nvlist(zc_props, 11540a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 11550a48a24eStimh goto create_failed; 11560a48a24eStimh } 1157351420b3Slling } 1158fa9e4066Sahrens 11590a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 11600a48a24eStimh goto create_failed; 11610a48a24eStimh 1162990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 1163fa9e4066Sahrens 11640a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 1165351420b3Slling 1166e9dbad6fSeschrock zcmd_free_nvlists(&zc); 11670a48a24eStimh nvlist_free(zc_props); 11680a48a24eStimh nvlist_free(zc_fsprops); 1169fa9e4066Sahrens 117099653d4eSeschrock switch (errno) { 1171fa9e4066Sahrens case EBUSY: 1172fa9e4066Sahrens /* 1173fa9e4066Sahrens * This can happen if the user has specified the same 1174fa9e4066Sahrens * device multiple times. We can't reliably detect this 1175fa9e4066Sahrens * until we try to add it and see we already have a 1176fa9e4066Sahrens * label. 1177fa9e4066Sahrens */ 117899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 117999653d4eSeschrock "one or more vdevs refer to the same device")); 118099653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1181fa9e4066Sahrens 1182e9316f76SJoe Stein case ERANGE: 1183e9316f76SJoe Stein /* 1184e9316f76SJoe Stein * This happens if the record size is smaller or larger 1185e9316f76SJoe Stein * than the allowed size range, or not a power of 2. 1186e9316f76SJoe Stein * 1187e9316f76SJoe Stein * NOTE: although zfs_valid_proplist is called earlier, 1188e9316f76SJoe Stein * this case may have slipped through since the 1189e9316f76SJoe Stein * pool does not exist yet and it is therefore 1190e9316f76SJoe Stein * impossible to read properties e.g. max blocksize 1191e9316f76SJoe Stein * from the pool. 1192e9316f76SJoe Stein */ 1193e9316f76SJoe Stein zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1194e9316f76SJoe Stein "record size invalid")); 1195e9316f76SJoe Stein return (zfs_error(hdl, EZFS_BADPROP, msg)); 1196e9316f76SJoe Stein 1197fa9e4066Sahrens case EOVERFLOW: 1198fa9e4066Sahrens /* 119999653d4eSeschrock * This occurs when one of the devices is below 1200fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1201fa9e4066Sahrens * device was the problem device since there's no 1202fa9e4066Sahrens * reliable way to determine device size from userland. 1203fa9e4066Sahrens */ 1204fa9e4066Sahrens { 1205fa9e4066Sahrens char buf[64]; 1206fa9e4066Sahrens 1207fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1208fa9e4066Sahrens 120999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 121099653d4eSeschrock "one or more devices is less than the " 121199653d4eSeschrock "minimum size (%s)"), buf); 1212fa9e4066Sahrens } 121399653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1214fa9e4066Sahrens 1215fa9e4066Sahrens case ENOSPC: 121699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 121799653d4eSeschrock "one or more devices is out of space")); 121899653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1219fa9e4066Sahrens 1220fa94a07fSbrendan case ENOTBLK: 1221fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1222fa94a07fSbrendan "cache device must be a disk or disk slice")); 1223fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 1224fa94a07fSbrendan 1225fa9e4066Sahrens default: 122699653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1227fa9e4066Sahrens } 1228fa9e4066Sahrens } 1229fa9e4066Sahrens 12300a48a24eStimh create_failed: 1231351420b3Slling zcmd_free_nvlists(&zc); 12320a48a24eStimh nvlist_free(zc_props); 12330a48a24eStimh nvlist_free(zc_fsprops); 12340a48a24eStimh return (ret); 1235fa9e4066Sahrens } 1236fa9e4066Sahrens 1237fa9e4066Sahrens /* 1238fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 1239fa9e4066Sahrens * datasets left in the pool. 1240fa9e4066Sahrens */ 1241fa9e4066Sahrens int 12424445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str) 1243fa9e4066Sahrens { 1244fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1245fa9e4066Sahrens zfs_handle_t *zfp = NULL; 124699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 124799653d4eSeschrock char msg[1024]; 1248fa9e4066Sahrens 1249fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 1250cb04b873SMark J Musante (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1251fa9e4066Sahrens return (-1); 1252fa9e4066Sahrens 1253fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 12544445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 1255fa9e4066Sahrens 1256cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 125799653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 125899653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 1259fa9e4066Sahrens 126099653d4eSeschrock if (errno == EROFS) { 126199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 126299653d4eSeschrock "one or more devices is read only")); 126399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 126499653d4eSeschrock } else { 126599653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1266fa9e4066Sahrens } 1267fa9e4066Sahrens 1268fa9e4066Sahrens if (zfp) 1269fa9e4066Sahrens zfs_close(zfp); 1270fa9e4066Sahrens return (-1); 1271fa9e4066Sahrens } 1272fa9e4066Sahrens 1273fa9e4066Sahrens if (zfp) { 1274fa9e4066Sahrens remove_mountpoint(zfp); 1275fa9e4066Sahrens zfs_close(zfp); 1276fa9e4066Sahrens } 1277fa9e4066Sahrens 1278fa9e4066Sahrens return (0); 1279fa9e4066Sahrens } 1280fa9e4066Sahrens 1281fa9e4066Sahrens /* 1282fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1283fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1284fa9e4066Sahrens */ 1285fa9e4066Sahrens int 1286fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1287fa9e4066Sahrens { 1288e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 128999653d4eSeschrock int ret; 129099653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 129199653d4eSeschrock char msg[1024]; 1292fa94a07fSbrendan nvlist_t **spares, **l2cache; 1293fa94a07fSbrendan uint_t nspares, nl2cache; 129499653d4eSeschrock 129599653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 129699653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 129799653d4eSeschrock 1298fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1299fa94a07fSbrendan SPA_VERSION_SPARES && 130099653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 130199653d4eSeschrock &spares, &nspares) == 0) { 130299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 130399653d4eSeschrock "upgraded to add hot spares")); 130499653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 130599653d4eSeschrock } 1306fa9e4066Sahrens 1307fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1308fa94a07fSbrendan SPA_VERSION_L2CACHE && 1309fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1310fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1311fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1312fa94a07fSbrendan "upgraded to add cache devices")); 1313fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1314fa94a07fSbrendan } 1315fa94a07fSbrendan 1316990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 131799653d4eSeschrock return (-1); 1318fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1319fa9e4066Sahrens 1320cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1321fa9e4066Sahrens switch (errno) { 1322fa9e4066Sahrens case EBUSY: 1323fa9e4066Sahrens /* 1324fa9e4066Sahrens * This can happen if the user has specified the same 1325fa9e4066Sahrens * device multiple times. We can't reliably detect this 1326fa9e4066Sahrens * until we try to add it and see we already have a 1327fa9e4066Sahrens * label. 1328fa9e4066Sahrens */ 132999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 133099653d4eSeschrock "one or more vdevs refer to the same device")); 133199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1332fa9e4066Sahrens break; 1333fa9e4066Sahrens 1334fa9e4066Sahrens case EOVERFLOW: 1335fa9e4066Sahrens /* 1336fa9e4066Sahrens * This occurrs when one of the devices is below 1337fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1338fa9e4066Sahrens * device was the problem device since there's no 1339fa9e4066Sahrens * reliable way to determine device size from userland. 1340fa9e4066Sahrens */ 1341fa9e4066Sahrens { 1342fa9e4066Sahrens char buf[64]; 1343fa9e4066Sahrens 1344fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1345fa9e4066Sahrens 134699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 134799653d4eSeschrock "device is less than the minimum " 134899653d4eSeschrock "size (%s)"), buf); 1349fa9e4066Sahrens } 135099653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 135199653d4eSeschrock break; 135299653d4eSeschrock 135399653d4eSeschrock case ENOTSUP: 135499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13558654d025Sperrin "pool must be upgraded to add these vdevs")); 135699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1357fa9e4066Sahrens break; 1358fa9e4066Sahrens 1359b1b8ab34Slling case EDOM: 1360b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13618654d025Sperrin "root pool can not have multiple vdevs" 13628654d025Sperrin " or separate logs")); 1363b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1364b1b8ab34Slling break; 1365b1b8ab34Slling 1366fa94a07fSbrendan case ENOTBLK: 1367fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1368fa94a07fSbrendan "cache device must be a disk or disk slice")); 1369fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1370fa94a07fSbrendan break; 1371fa94a07fSbrendan 1372fa9e4066Sahrens default: 137399653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1374fa9e4066Sahrens } 1375fa9e4066Sahrens 137699653d4eSeschrock ret = -1; 137799653d4eSeschrock } else { 137899653d4eSeschrock ret = 0; 1379fa9e4066Sahrens } 1380fa9e4066Sahrens 1381e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1382fa9e4066Sahrens 138399653d4eSeschrock return (ret); 1384fa9e4066Sahrens } 1385fa9e4066Sahrens 1386fa9e4066Sahrens /* 1387fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1388fa9e4066Sahrens * mounted datasets in the pool. 1389fa9e4066Sahrens */ 13904445fffbSMatthew Ahrens static int 13914445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce, 13924445fffbSMatthew Ahrens const char *log_str) 1393fa9e4066Sahrens { 1394fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 139589a89ebfSlling char msg[1024]; 1396fa9e4066Sahrens 139789a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 139889a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 139989a89ebfSlling 1400fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 140189a89ebfSlling zc.zc_cookie = force; 1402394ab0cbSGeorge Wilson zc.zc_guid = hardforce; 14034445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 140489a89ebfSlling 140589a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 140689a89ebfSlling switch (errno) { 140789a89ebfSlling case EXDEV: 140889a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 140989a89ebfSlling "use '-f' to override the following errors:\n" 141089a89ebfSlling "'%s' has an active shared spare which could be" 141189a89ebfSlling " used by other pools once '%s' is exported."), 141289a89ebfSlling zhp->zpool_name, zhp->zpool_name); 141389a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 141489a89ebfSlling msg)); 141589a89ebfSlling default: 141689a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 141789a89ebfSlling msg)); 141889a89ebfSlling } 141989a89ebfSlling } 1420fa9e4066Sahrens 1421fa9e4066Sahrens return (0); 1422fa9e4066Sahrens } 1423fa9e4066Sahrens 1424394ab0cbSGeorge Wilson int 14254445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str) 1426394ab0cbSGeorge Wilson { 14274445fffbSMatthew Ahrens return (zpool_export_common(zhp, force, B_FALSE, log_str)); 1428394ab0cbSGeorge Wilson } 1429394ab0cbSGeorge Wilson 1430394ab0cbSGeorge Wilson int 14314445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str) 1432394ab0cbSGeorge Wilson { 14334445fffbSMatthew Ahrens return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str)); 1434394ab0cbSGeorge Wilson } 1435394ab0cbSGeorge Wilson 1436468c413aSTim Haley static void 1437468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 14384b964adaSGeorge Wilson nvlist_t *config) 1439468c413aSTim Haley { 14404b964adaSGeorge Wilson nvlist_t *nv = NULL; 1441468c413aSTim Haley uint64_t rewindto; 1442468c413aSTim Haley int64_t loss = -1; 1443468c413aSTim Haley struct tm t; 1444468c413aSTim Haley char timestr[128]; 1445468c413aSTim Haley 14464b964adaSGeorge Wilson if (!hdl->libzfs_printerr || config == NULL) 14474b964adaSGeorge Wilson return; 14484b964adaSGeorge Wilson 1449ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1450ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) { 1451468c413aSTim Haley return; 1452ad135b5dSChristopher Siden } 1453468c413aSTim Haley 14544b964adaSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1455468c413aSTim Haley return; 14564b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1457468c413aSTim Haley 1458468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1459468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1460468c413aSTim Haley if (dryrun) { 1461468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1462468c413aSTim Haley "Would be able to return %s " 1463468c413aSTim Haley "to its state as of %s.\n"), 1464468c413aSTim Haley name, timestr); 1465468c413aSTim Haley } else { 1466468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1467468c413aSTim Haley "Pool %s returned to its state as of %s.\n"), 1468468c413aSTim Haley name, timestr); 1469468c413aSTim Haley } 1470468c413aSTim Haley if (loss > 120) { 1471468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1472468c413aSTim Haley "%s approximately %lld "), 1473468c413aSTim Haley dryrun ? "Would discard" : "Discarded", 1474468c413aSTim Haley (loss + 30) / 60); 1475468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1476468c413aSTim Haley "minutes of transactions.\n")); 1477468c413aSTim Haley } else if (loss > 0) { 1478468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1479468c413aSTim Haley "%s approximately %lld "), 1480468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss); 1481468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1482468c413aSTim Haley "seconds of transactions.\n")); 1483468c413aSTim Haley } 1484468c413aSTim Haley } 1485468c413aSTim Haley } 1486468c413aSTim Haley 1487468c413aSTim Haley void 1488468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1489468c413aSTim Haley nvlist_t *config) 1490468c413aSTim Haley { 14914b964adaSGeorge Wilson nvlist_t *nv = NULL; 1492468c413aSTim Haley int64_t loss = -1; 1493468c413aSTim Haley uint64_t edata = UINT64_MAX; 1494468c413aSTim Haley uint64_t rewindto; 1495468c413aSTim Haley struct tm t; 1496468c413aSTim Haley char timestr[128]; 1497468c413aSTim Haley 1498468c413aSTim Haley if (!hdl->libzfs_printerr) 1499468c413aSTim Haley return; 1500468c413aSTim Haley 1501468c413aSTim Haley if (reason >= 0) 1502468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1503468c413aSTim Haley else 1504468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1505468c413aSTim Haley 1506468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 15074b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1508ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 || 15094b964adaSGeorge Wilson nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1510468c413aSTim Haley goto no_info; 1511468c413aSTim Haley 15124b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 15134b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1514468c413aSTim Haley &edata); 1515468c413aSTim Haley 1516468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1517468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n")); 1518468c413aSTim Haley 1519468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1520468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1521468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1522468c413aSTim Haley "\tReturning the pool to its state as of %s\n" 1523468c413aSTim Haley "\tshould correct the problem. "), 1524468c413aSTim Haley timestr); 1525468c413aSTim Haley } else { 1526468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1527468c413aSTim Haley "\tReverting the pool to an earlier state " 1528468c413aSTim Haley "should correct the problem.\n\t")); 1529468c413aSTim Haley } 1530468c413aSTim Haley 1531468c413aSTim Haley if (loss > 120) { 1532468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1533468c413aSTim Haley "Approximately %lld minutes of data\n" 1534468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1535468c413aSTim Haley } else if (loss > 0) { 1536468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1537468c413aSTim Haley "Approximately %lld seconds of data\n" 1538468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss); 1539468c413aSTim Haley } 1540468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) { 1541468c413aSTim Haley if (edata == 1) { 1542468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1543468c413aSTim Haley "After rewind, at least\n" 1544468c413aSTim Haley "\tone persistent user-data error will remain. ")); 1545468c413aSTim Haley } else { 1546468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1547468c413aSTim Haley "After rewind, several\n" 1548468c413aSTim Haley "\tpersistent user-data errors will remain. ")); 1549468c413aSTim Haley } 1550468c413aSTim Haley } 1551468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1552a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1553a33cae98STim Haley reason >= 0 ? "clear" : "import", name); 1554468c413aSTim Haley 1555468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1556468c413aSTim Haley "A scrub of the pool\n" 1557468c413aSTim Haley "\tis strongly recommended after recovery.\n")); 1558468c413aSTim Haley return; 1559468c413aSTim Haley 1560468c413aSTim Haley no_info: 1561468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1562468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n")); 1563468c413aSTim Haley } 1564468c413aSTim Haley 1565fa9e4066Sahrens /* 1566990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1567990b4856Slling * if possible. 1568990b4856Slling * 1569990b4856Slling * Applications should use zpool_import_props() to import a pool with 1570990b4856Slling * new properties value to be set. 1571fa9e4066Sahrens */ 1572fa9e4066Sahrens int 157399653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1574990b4856Slling char *altroot) 1575990b4856Slling { 1576990b4856Slling nvlist_t *props = NULL; 1577990b4856Slling int ret; 1578990b4856Slling 1579990b4856Slling if (altroot != NULL) { 1580990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1581990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1582990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1583990b4856Slling newname)); 1584990b4856Slling } 1585990b4856Slling 1586990b4856Slling if (nvlist_add_string(props, 1587352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1588352d8027SGeorge Wilson nvlist_add_string(props, 1589352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1590990b4856Slling nvlist_free(props); 1591990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1592990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1593990b4856Slling newname)); 1594990b4856Slling } 1595990b4856Slling } 1596990b4856Slling 15974b964adaSGeorge Wilson ret = zpool_import_props(hdl, config, newname, props, 15984b964adaSGeorge Wilson ZFS_IMPORT_NORMAL); 1599aab83bb8SJosef 'Jeff' Sipek nvlist_free(props); 1600990b4856Slling return (ret); 1601990b4856Slling } 1602990b4856Slling 16034b964adaSGeorge Wilson static void 16044b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 16054b964adaSGeorge Wilson int indent) 16064b964adaSGeorge Wilson { 16074b964adaSGeorge Wilson nvlist_t **child; 16084b964adaSGeorge Wilson uint_t c, children; 16094b964adaSGeorge Wilson char *vname; 16104b964adaSGeorge Wilson uint64_t is_log = 0; 16114b964adaSGeorge Wilson 16124b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 16134b964adaSGeorge Wilson &is_log); 16144b964adaSGeorge Wilson 16154b964adaSGeorge Wilson if (name != NULL) 16164b964adaSGeorge Wilson (void) printf("\t%*s%s%s\n", indent, "", name, 16174b964adaSGeorge Wilson is_log ? " [log]" : ""); 16184b964adaSGeorge Wilson 16194b964adaSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 16204b964adaSGeorge Wilson &child, &children) != 0) 16214b964adaSGeorge Wilson return; 16224b964adaSGeorge Wilson 16234b964adaSGeorge Wilson for (c = 0; c < children; c++) { 16244b964adaSGeorge Wilson vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 16254b964adaSGeorge Wilson print_vdev_tree(hdl, vname, child[c], indent + 2); 16264b964adaSGeorge Wilson free(vname); 16274b964adaSGeorge Wilson } 16284b964adaSGeorge Wilson } 16294b964adaSGeorge Wilson 1630ad135b5dSChristopher Siden void 1631ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config) 1632ad135b5dSChristopher Siden { 1633ad135b5dSChristopher Siden nvlist_t *nvinfo, *unsup_feat; 1634ad135b5dSChristopher Siden 1635ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 1636ad135b5dSChristopher Siden 0); 1637ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT, 1638ad135b5dSChristopher Siden &unsup_feat) == 0); 1639ad135b5dSChristopher Siden 1640ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL; 1641ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(unsup_feat, nvp)) { 1642ad135b5dSChristopher Siden char *desc; 1643ad135b5dSChristopher Siden 1644ad135b5dSChristopher Siden verify(nvpair_type(nvp) == DATA_TYPE_STRING); 1645ad135b5dSChristopher Siden verify(nvpair_value_string(nvp, &desc) == 0); 1646ad135b5dSChristopher Siden 1647ad135b5dSChristopher Siden if (strlen(desc) > 0) 1648ad135b5dSChristopher Siden (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc); 1649ad135b5dSChristopher Siden else 1650ad135b5dSChristopher Siden (void) printf("\t%s\n", nvpair_name(nvp)); 1651ad135b5dSChristopher Siden } 1652ad135b5dSChristopher Siden } 1653ad135b5dSChristopher Siden 1654990b4856Slling /* 1655990b4856Slling * Import the given pool using the known configuration and a list of 1656990b4856Slling * properties to be set. The configuration should have come from 1657990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1658990b4856Slling * is imported with a different name. 1659990b4856Slling */ 1660990b4856Slling int 1661990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 16624b964adaSGeorge Wilson nvlist_t *props, int flags) 1663fa9e4066Sahrens { 1664e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1665468c413aSTim Haley zpool_rewind_policy_t policy; 16664b964adaSGeorge Wilson nvlist_t *nv = NULL; 16674b964adaSGeorge Wilson nvlist_t *nvinfo = NULL; 16684b964adaSGeorge Wilson nvlist_t *missing = NULL; 1669fa9e4066Sahrens char *thename; 1670fa9e4066Sahrens char *origname; 1671fa9e4066Sahrens int ret; 16724b964adaSGeorge Wilson int error = 0; 1673990b4856Slling char errbuf[1024]; 1674fa9e4066Sahrens 1675fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1676fa9e4066Sahrens &origname) == 0); 1677fa9e4066Sahrens 1678990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1679990b4856Slling "cannot import pool '%s'"), origname); 1680990b4856Slling 1681fa9e4066Sahrens if (newname != NULL) { 168299653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1683ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 168499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 168599653d4eSeschrock newname)); 1686fa9e4066Sahrens thename = (char *)newname; 1687fa9e4066Sahrens } else { 1688fa9e4066Sahrens thename = origname; 1689fa9e4066Sahrens } 1690fa9e4066Sahrens 1691078266a5SMarcel Telka if (props != NULL) { 1692990b4856Slling uint64_t version; 1693f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 1694fa9e4066Sahrens 1695990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1696990b4856Slling &version) == 0); 1697fa9e4066Sahrens 16980a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1699078266a5SMarcel Telka props, version, flags, errbuf)) == NULL) 1700990b4856Slling return (-1); 1701078266a5SMarcel Telka if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1702351420b3Slling nvlist_free(props); 1703990b4856Slling return (-1); 1704351420b3Slling } 1705078266a5SMarcel Telka nvlist_free(props); 1706990b4856Slling } 1707990b4856Slling 1708990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1709fa9e4066Sahrens 1710fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1711ea8dc4b6Seschrock &zc.zc_guid) == 0); 1712fa9e4066Sahrens 1713351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1714078266a5SMarcel Telka zcmd_free_nvlists(&zc); 171599653d4eSeschrock return (-1); 1716351420b3Slling } 171757f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1718078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1719468c413aSTim Haley return (-1); 1720468c413aSTim Haley } 1721fa9e4066Sahrens 17224b964adaSGeorge Wilson zc.zc_cookie = flags; 17234b964adaSGeorge Wilson while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 17244b964adaSGeorge Wilson errno == ENOMEM) { 17254b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 17264b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 17274b964adaSGeorge Wilson return (-1); 17284b964adaSGeorge Wilson } 17294b964adaSGeorge Wilson } 17304b964adaSGeorge Wilson if (ret != 0) 17314b964adaSGeorge Wilson error = errno; 17324b964adaSGeorge Wilson 17334b964adaSGeorge Wilson (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1734078266a5SMarcel Telka 1735078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1736078266a5SMarcel Telka 17374b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 17384b964adaSGeorge Wilson 17394b964adaSGeorge Wilson if (error) { 1740fa9e4066Sahrens char desc[1024]; 1741468c413aSTim Haley 1742468c413aSTim Haley /* 1743468c413aSTim Haley * Dry-run failed, but we print out what success 1744468c413aSTim Haley * looks like if we found a best txg 1745468c413aSTim Haley */ 17464b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_TRY_REWIND) { 1747468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 17484b964adaSGeorge Wilson B_TRUE, nv); 17494b964adaSGeorge Wilson nvlist_free(nv); 1750468c413aSTim Haley return (-1); 1751468c413aSTim Haley } 1752468c413aSTim Haley 1753fa9e4066Sahrens if (newname == NULL) 1754fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1755fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1756fa9e4066Sahrens thename); 1757fa9e4066Sahrens else 1758fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1759fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1760fa9e4066Sahrens origname, thename); 1761fa9e4066Sahrens 17624b964adaSGeorge Wilson switch (error) { 1763ea8dc4b6Seschrock case ENOTSUP: 1764ad135b5dSChristopher Siden if (nv != NULL && nvlist_lookup_nvlist(nv, 1765ad135b5dSChristopher Siden ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1766ad135b5dSChristopher Siden nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) { 1767ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, "This " 1768ad135b5dSChristopher Siden "pool uses the following feature(s) not " 1769ad135b5dSChristopher Siden "supported by this system:\n")); 1770ad135b5dSChristopher Siden zpool_print_unsup_feat(nv); 1771ad135b5dSChristopher Siden if (nvlist_exists(nvinfo, 1772ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY)) { 1773ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, 1774ad135b5dSChristopher Siden "All unsupported features are only " 1775ad135b5dSChristopher Siden "required for writing to the pool." 1776ad135b5dSChristopher Siden "\nThe pool can be imported using " 1777ad135b5dSChristopher Siden "'-o readonly=on'.\n")); 1778ad135b5dSChristopher Siden } 1779ad135b5dSChristopher Siden } 1780ea8dc4b6Seschrock /* 1781ea8dc4b6Seschrock * Unsupported version. 1782ea8dc4b6Seschrock */ 178399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1784ea8dc4b6Seschrock break; 1785ea8dc4b6Seschrock 1786b5989ec7Seschrock case EINVAL: 1787b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1788b5989ec7Seschrock break; 1789b5989ec7Seschrock 179054a91118SChris Kirby case EROFS: 179154a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 179254a91118SChris Kirby "one or more devices is read only")); 179354a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc); 179454a91118SChris Kirby break; 179554a91118SChris Kirby 17964b964adaSGeorge Wilson case ENXIO: 17974b964adaSGeorge Wilson if (nv && nvlist_lookup_nvlist(nv, 17984b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 17994b964adaSGeorge Wilson nvlist_lookup_nvlist(nvinfo, 18004b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 18014b964adaSGeorge Wilson (void) printf(dgettext(TEXT_DOMAIN, 18024b964adaSGeorge Wilson "The devices below are missing, use " 18034b964adaSGeorge Wilson "'-m' to import the pool anyway:\n")); 18044b964adaSGeorge Wilson print_vdev_tree(hdl, NULL, missing, 2); 18054b964adaSGeorge Wilson (void) printf("\n"); 18064b964adaSGeorge Wilson } 18074b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 18084b964adaSGeorge Wilson break; 18094b964adaSGeorge Wilson 18104b964adaSGeorge Wilson case EEXIST: 18114b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 18124b964adaSGeorge Wilson break; 1813c971037bSPaul Dagnelie case ENAMETOOLONG: 1814c971037bSPaul Dagnelie zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1815c971037bSPaul Dagnelie "new name of at least one dataset is longer than " 1816c971037bSPaul Dagnelie "the maximum allowable length")); 1817c971037bSPaul Dagnelie (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc); 1818c971037bSPaul Dagnelie break; 1819fa9e4066Sahrens default: 18204b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 1821468c413aSTim Haley zpool_explain_recover(hdl, 18224b964adaSGeorge Wilson newname ? origname : thename, -error, nv); 1823468c413aSTim Haley break; 1824fa9e4066Sahrens } 1825fa9e4066Sahrens 18264b964adaSGeorge Wilson nvlist_free(nv); 1827fa9e4066Sahrens ret = -1; 1828fa9e4066Sahrens } else { 1829fa9e4066Sahrens zpool_handle_t *zhp; 1830ecd6cf80Smarks 1831fa9e4066Sahrens /* 1832fa9e4066Sahrens * This should never fail, but play it safe anyway. 1833fa9e4066Sahrens */ 1834681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0) 183594de1d4cSeschrock ret = -1; 1836681d9761SEric Taylor else if (zhp != NULL) 1837fa9e4066Sahrens zpool_close(zhp); 1838468c413aSTim Haley if (policy.zrp_request & 1839468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1840468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 18414b964adaSGeorge Wilson ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 1842468c413aSTim Haley } 18434b964adaSGeorge Wilson nvlist_free(nv); 1844468c413aSTim Haley return (0); 1845fa9e4066Sahrens } 1846fa9e4066Sahrens 1847fa9e4066Sahrens return (ret); 1848fa9e4066Sahrens } 1849fa9e4066Sahrens 1850fa9e4066Sahrens /* 18513f9d6ad7SLin Ling * Scan the pool. 1852fa9e4066Sahrens */ 1853fa9e4066Sahrens int 18543f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func) 1855fa9e4066Sahrens { 1856fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1857fa9e4066Sahrens char msg[1024]; 185899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1859fa9e4066Sahrens 1860fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18613f9d6ad7SLin Ling zc.zc_cookie = func; 1862fa9e4066Sahrens 1863cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 || 18643f9d6ad7SLin Ling (errno == ENOENT && func != POOL_SCAN_NONE)) 1865fa9e4066Sahrens return (0); 1866fa9e4066Sahrens 18673f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) { 18683f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 18693f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); 18703f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) { 18713f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 18723f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 18733f9d6ad7SLin Ling zc.zc_name); 18743f9d6ad7SLin Ling } else { 18753f9d6ad7SLin Ling assert(!"unexpected result"); 18763f9d6ad7SLin Ling } 1877fa9e4066Sahrens 18783f9d6ad7SLin Ling if (errno == EBUSY) { 18793f9d6ad7SLin Ling nvlist_t *nvroot; 18803f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 18813f9d6ad7SLin Ling uint_t psc; 18823f9d6ad7SLin Ling 18833f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config, 18843f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 18853f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 18863f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 18873f9d6ad7SLin Ling if (ps && ps->pss_func == POOL_SCAN_SCRUB) 18883f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 18893f9d6ad7SLin Ling else 18903f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg)); 18913f9d6ad7SLin Ling } else if (errno == ENOENT) { 18923f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 18933f9d6ad7SLin Ling } else { 189499653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 18953f9d6ad7SLin Ling } 1896fa9e4066Sahrens } 1897fa9e4066Sahrens 18983fdda499SJohn Harres /* 18993fdda499SJohn Harres * This provides a very minimal check whether a given string is likely a 19003fdda499SJohn Harres * c#t#d# style string. Users of this are expected to do their own 19013fdda499SJohn Harres * verification of the s# part. 19023fdda499SJohn Harres */ 19033fdda499SJohn Harres #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 19043fdda499SJohn Harres 19053fdda499SJohn Harres /* 19063fdda499SJohn Harres * More elaborate version for ones which may start with "/dev/dsk/" 19073fdda499SJohn Harres * and the like. 19083fdda499SJohn Harres */ 19093fdda499SJohn Harres static int 19109a686fbcSPaul Dagnelie ctd_check_path(char *str) 19119a686fbcSPaul Dagnelie { 19123fdda499SJohn Harres /* 19133fdda499SJohn Harres * If it starts with a slash, check the last component. 19143fdda499SJohn Harres */ 19153fdda499SJohn Harres if (str && str[0] == '/') { 19163fdda499SJohn Harres char *tmp = strrchr(str, '/'); 19173fdda499SJohn Harres 19183fdda499SJohn Harres /* 19193fdda499SJohn Harres * If it ends in "/old", check the second-to-last 19203fdda499SJohn Harres * component of the string instead. 19213fdda499SJohn Harres */ 19223fdda499SJohn Harres if (tmp != str && strcmp(tmp, "/old") == 0) { 19233fdda499SJohn Harres for (tmp--; *tmp != '/'; tmp--) 19243fdda499SJohn Harres ; 19253fdda499SJohn Harres } 19263fdda499SJohn Harres str = tmp + 1; 19273fdda499SJohn Harres } 19283fdda499SJohn Harres return (CTD_CHECK(str)); 19293fdda499SJohn Harres } 19303fdda499SJohn Harres 1931a43d325bSek /* 1932573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the 1933573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device. 1934a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1935a43d325bSek * spare; but FALSE if its an INUSE spare. 1936a43d325bSek */ 193799653d4eSeschrock static nvlist_t * 1938573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 1939573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log) 1940ea8dc4b6Seschrock { 1941ea8dc4b6Seschrock uint_t c, children; 1942ea8dc4b6Seschrock nvlist_t **child; 194399653d4eSeschrock nvlist_t *ret; 1944ee0eb9f2SEric Schrock uint64_t is_log; 1945573ca77eSGeorge Wilson char *srchkey; 1946573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL); 1947573ca77eSGeorge Wilson 1948573ca77eSGeorge Wilson /* Nothing to look for */ 1949573ca77eSGeorge Wilson if (search == NULL || pair == NULL) 1950573ca77eSGeorge Wilson return (NULL); 1951ea8dc4b6Seschrock 1952573ca77eSGeorge Wilson /* Obtain the key we will use to search */ 1953573ca77eSGeorge Wilson srchkey = nvpair_name(pair); 1954573ca77eSGeorge Wilson 1955573ca77eSGeorge Wilson switch (nvpair_type(pair)) { 1956cb04b873SMark J Musante case DATA_TYPE_UINT64: 1957573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 1958cb04b873SMark J Musante uint64_t srchval, theguid; 1959cb04b873SMark J Musante 1960cb04b873SMark J Musante verify(nvpair_value_uint64(pair, &srchval) == 0); 1961cb04b873SMark J Musante verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1962cb04b873SMark J Musante &theguid) == 0); 1963cb04b873SMark J Musante if (theguid == srchval) 1964cb04b873SMark J Musante return (nv); 1965573ca77eSGeorge Wilson } 1966573ca77eSGeorge Wilson break; 1967573ca77eSGeorge Wilson 1968573ca77eSGeorge Wilson case DATA_TYPE_STRING: { 1969573ca77eSGeorge Wilson char *srchval, *val; 1970573ca77eSGeorge Wilson 1971573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0); 1972573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0) 1973573ca77eSGeorge Wilson break; 1974ea8dc4b6Seschrock 1975ea8dc4b6Seschrock /* 19763fdda499SJohn Harres * Search for the requested value. Special cases: 19773fdda499SJohn Harres * 1978*7855d95bSToomas Soome * - ZPOOL_CONFIG_PATH for whole disk entries. To support 1979*7855d95bSToomas Soome * UEFI boot, these end in "s0" or "s0/old" or "s1" or 1980*7855d95bSToomas Soome * "s1/old". The "s0" or "s1" part is hidden from the user, 19813fdda499SJohn Harres * but included in the string, so this matches around it. 19823fdda499SJohn Harres * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 19833fdda499SJohn Harres * 198488ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares. 1985ea8dc4b6Seschrock */ 19863fdda499SJohn Harres if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 19873fdda499SJohn Harres ctd_check_path(val)) { 1988573ca77eSGeorge Wilson uint64_t wholedisk = 0; 1989573ca77eSGeorge Wilson 1990573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 1991573ca77eSGeorge Wilson &wholedisk); 1992573ca77eSGeorge Wilson if (wholedisk) { 19933fdda499SJohn Harres int slen = strlen(srchval); 19943fdda499SJohn Harres int vlen = strlen(val); 19953fdda499SJohn Harres 19963fdda499SJohn Harres if (slen != vlen - 2) 19973fdda499SJohn Harres break; 19983fdda499SJohn Harres 19993fdda499SJohn Harres /* 20003fdda499SJohn Harres * make_leaf_vdev() should only set 20013fdda499SJohn Harres * wholedisk for ZPOOL_CONFIG_PATHs which 20023fdda499SJohn Harres * will include "/dev/dsk/", giving plenty of 20033fdda499SJohn Harres * room for the indices used next. 20043fdda499SJohn Harres */ 20053fdda499SJohn Harres ASSERT(vlen >= 6); 20063fdda499SJohn Harres 20073fdda499SJohn Harres /* 20083fdda499SJohn Harres * strings identical except trailing "s0" 20093fdda499SJohn Harres */ 2010*7855d95bSToomas Soome if ((strcmp(&val[vlen - 2], "s0") == 0 || 2011*7855d95bSToomas Soome strcmp(&val[vlen - 2], "s1") == 0) && 20123fdda499SJohn Harres strncmp(srchval, val, slen) == 0) 20133fdda499SJohn Harres return (nv); 20143fdda499SJohn Harres 2015573ca77eSGeorge Wilson /* 20163fdda499SJohn Harres * strings identical except trailing "s0/old" 2017573ca77eSGeorge Wilson */ 2018*7855d95bSToomas Soome if ((strcmp(&val[vlen - 6], "s0/old") == 0 || 2019*7855d95bSToomas Soome strcmp(&val[vlen - 6], "s1/old") == 0) && 20203fdda499SJohn Harres strcmp(&srchval[slen - 4], "/old") == 0 && 20213fdda499SJohn Harres strncmp(srchval, val, slen - 4) == 0) 2022573ca77eSGeorge Wilson return (nv); 20233fdda499SJohn Harres 2024573ca77eSGeorge Wilson break; 2025573ca77eSGeorge Wilson } 202688ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 202788ecc943SGeorge Wilson char *type, *idx, *end, *p; 202888ecc943SGeorge Wilson uint64_t id, vdev_id; 202988ecc943SGeorge Wilson 203088ecc943SGeorge Wilson /* 203188ecc943SGeorge Wilson * Determine our vdev type, keeping in mind 203288ecc943SGeorge Wilson * that the srchval is composed of a type and 203388ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4). 203488ecc943SGeorge Wilson */ 203588ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL) 203688ecc943SGeorge Wilson return (NULL); 203788ecc943SGeorge Wilson 203888ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) { 203988ecc943SGeorge Wilson free(type); 204088ecc943SGeorge Wilson break; 204188ecc943SGeorge Wilson } 204288ecc943SGeorge Wilson idx = p + 1; 204388ecc943SGeorge Wilson *p = '\0'; 204488ecc943SGeorge Wilson 204588ecc943SGeorge Wilson /* 204688ecc943SGeorge Wilson * If the types don't match then keep looking. 204788ecc943SGeorge Wilson */ 204888ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) { 204988ecc943SGeorge Wilson free(type); 205088ecc943SGeorge Wilson break; 205188ecc943SGeorge Wilson } 205288ecc943SGeorge Wilson 205388ecc943SGeorge Wilson verify(strncmp(type, VDEV_TYPE_RAIDZ, 205488ecc943SGeorge Wilson strlen(VDEV_TYPE_RAIDZ)) == 0 || 205588ecc943SGeorge Wilson strncmp(type, VDEV_TYPE_MIRROR, 205688ecc943SGeorge Wilson strlen(VDEV_TYPE_MIRROR)) == 0); 205788ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 205888ecc943SGeorge Wilson &id) == 0); 205988ecc943SGeorge Wilson 206088ecc943SGeorge Wilson errno = 0; 206188ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10); 206288ecc943SGeorge Wilson 206388ecc943SGeorge Wilson free(type); 206488ecc943SGeorge Wilson if (errno != 0) 206588ecc943SGeorge Wilson return (NULL); 206688ecc943SGeorge Wilson 206788ecc943SGeorge Wilson /* 206888ecc943SGeorge Wilson * Now verify that we have the correct vdev id. 206988ecc943SGeorge Wilson */ 207088ecc943SGeorge Wilson if (vdev_id == id) 207188ecc943SGeorge Wilson return (nv); 2072ea8dc4b6Seschrock } 2073573ca77eSGeorge Wilson 2074573ca77eSGeorge Wilson /* 2075573ca77eSGeorge Wilson * Common case 2076573ca77eSGeorge Wilson */ 2077573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0) 2078573ca77eSGeorge Wilson return (nv); 2079573ca77eSGeorge Wilson break; 2080573ca77eSGeorge Wilson } 2081573ca77eSGeorge Wilson 2082573ca77eSGeorge Wilson default: 2083573ca77eSGeorge Wilson break; 2084ea8dc4b6Seschrock } 2085ea8dc4b6Seschrock 2086ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2087ea8dc4b6Seschrock &child, &children) != 0) 208899653d4eSeschrock return (NULL); 2089ea8dc4b6Seschrock 2090ee0eb9f2SEric Schrock for (c = 0; c < children; c++) { 2091573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2092ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2093ee0eb9f2SEric Schrock /* 2094ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel 2095ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the 2096ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where 2097ee0eb9f2SEric Schrock * 'log' is non-NULL). 2098ee0eb9f2SEric Schrock */ 2099ee0eb9f2SEric Schrock if (log != NULL && 2100ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c], 2101ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 2102ee0eb9f2SEric Schrock is_log) { 2103ee0eb9f2SEric Schrock *log = B_TRUE; 2104ee0eb9f2SEric Schrock } 2105ea8dc4b6Seschrock return (ret); 2106ee0eb9f2SEric Schrock } 2107ee0eb9f2SEric Schrock } 2108ea8dc4b6Seschrock 210999653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 211099653d4eSeschrock &child, &children) == 0) { 211199653d4eSeschrock for (c = 0; c < children; c++) { 2112573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2113ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2114a43d325bSek *avail_spare = B_TRUE; 211599653d4eSeschrock return (ret); 211699653d4eSeschrock } 211799653d4eSeschrock } 211899653d4eSeschrock } 211999653d4eSeschrock 2120fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2121fa94a07fSbrendan &child, &children) == 0) { 2122fa94a07fSbrendan for (c = 0; c < children; c++) { 2123573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2124ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2125fa94a07fSbrendan *l2cache = B_TRUE; 2126fa94a07fSbrendan return (ret); 2127fa94a07fSbrendan } 2128fa94a07fSbrendan } 2129fa94a07fSbrendan } 2130fa94a07fSbrendan 213199653d4eSeschrock return (NULL); 2132ea8dc4b6Seschrock } 2133ea8dc4b6Seschrock 2134573ca77eSGeorge Wilson /* 2135573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the 2136573ca77eSGeorge Wilson * associated vdev. 2137573ca77eSGeorge Wilson */ 2138573ca77eSGeorge Wilson nvlist_t * 2139573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 2140573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 2141573ca77eSGeorge Wilson { 2142573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret; 2143573ca77eSGeorge Wilson 2144573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2145573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 2146573ca77eSGeorge Wilson 2147573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2148573ca77eSGeorge Wilson &nvroot) == 0); 2149573ca77eSGeorge Wilson 2150573ca77eSGeorge Wilson *avail_spare = B_FALSE; 2151cb04b873SMark J Musante *l2cache = B_FALSE; 2152daeb70e5SMark J Musante if (log != NULL) 2153daeb70e5SMark J Musante *log = B_FALSE; 2154573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2155573ca77eSGeorge Wilson nvlist_free(search); 2156573ca77eSGeorge Wilson 2157573ca77eSGeorge Wilson return (ret); 2158573ca77eSGeorge Wilson } 2159573ca77eSGeorge Wilson 216088ecc943SGeorge Wilson /* 216188ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 216288ecc943SGeorge Wilson */ 216388ecc943SGeorge Wilson boolean_t 216488ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name) 216588ecc943SGeorge Wilson { 216688ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 216788ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 216888ecc943SGeorge Wilson return (B_TRUE); 216988ecc943SGeorge Wilson return (B_FALSE); 217088ecc943SGeorge Wilson } 217188ecc943SGeorge Wilson 217299653d4eSeschrock nvlist_t * 2173fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 2174ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log) 2175ea8dc4b6Seschrock { 2176ea8dc4b6Seschrock char buf[MAXPATHLEN]; 2177ea8dc4b6Seschrock char *end; 2178573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret; 2179ea8dc4b6Seschrock uint64_t guid; 2180ea8dc4b6Seschrock 2181573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2182573ca77eSGeorge Wilson 21830917b783Seschrock guid = strtoull(path, &end, 10); 2184ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 2185573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 218688ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) { 218788ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 2188ea8dc4b6Seschrock } else if (path[0] != '/') { 21896401734dSWill Andrews (void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT, 21906401734dSWill Andrews path); 2191573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 2192ea8dc4b6Seschrock } else { 2193573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 2194ea8dc4b6Seschrock } 2195ea8dc4b6Seschrock 2196ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2197ea8dc4b6Seschrock &nvroot) == 0); 2198ea8dc4b6Seschrock 2199a43d325bSek *avail_spare = B_FALSE; 2200fa94a07fSbrendan *l2cache = B_FALSE; 2201ee0eb9f2SEric Schrock if (log != NULL) 2202ee0eb9f2SEric Schrock *log = B_FALSE; 2203573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2204573ca77eSGeorge Wilson nvlist_free(search); 2205573ca77eSGeorge Wilson 2206573ca77eSGeorge Wilson return (ret); 2207a43d325bSek } 2208a43d325bSek 220919397407SSherry Moore static int 221019397407SSherry Moore vdev_online(nvlist_t *nv) 221119397407SSherry Moore { 221219397407SSherry Moore uint64_t ival; 221319397407SSherry Moore 221419397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 221519397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 221619397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 221719397407SSherry Moore return (0); 221819397407SSherry Moore 221919397407SSherry Moore return (1); 222019397407SSherry Moore } 222119397407SSherry Moore 222219397407SSherry Moore /* 222321ecdf64SLin Ling * Helper function for zpool_get_physpaths(). 222419397407SSherry Moore */ 2225753a6d45SSherry Moore static int 222621ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 2227753a6d45SSherry Moore size_t *bytes_written) 222819397407SSherry Moore { 2229753a6d45SSherry Moore size_t bytes_left, pos, rsz; 2230753a6d45SSherry Moore char *tmppath; 2231753a6d45SSherry Moore const char *format; 2232753a6d45SSherry Moore 2233753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 2234753a6d45SSherry Moore &tmppath) != 0) 2235753a6d45SSherry Moore return (EZFS_NODEVICE); 2236753a6d45SSherry Moore 2237753a6d45SSherry Moore pos = *bytes_written; 2238753a6d45SSherry Moore bytes_left = physpath_size - pos; 2239753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s"; 2240753a6d45SSherry Moore 2241753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 2242753a6d45SSherry Moore *bytes_written += rsz; 2243753a6d45SSherry Moore 2244753a6d45SSherry Moore if (rsz >= bytes_left) { 2245753a6d45SSherry Moore /* if physpath was not copied properly, clear it */ 2246753a6d45SSherry Moore if (bytes_left != 0) { 2247753a6d45SSherry Moore physpath[pos] = 0; 2248753a6d45SSherry Moore } 2249753a6d45SSherry Moore return (EZFS_NOSPC); 2250753a6d45SSherry Moore } 2251753a6d45SSherry Moore return (0); 2252753a6d45SSherry Moore } 2253753a6d45SSherry Moore 225421ecdf64SLin Ling static int 225521ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 225621ecdf64SLin Ling size_t *rsz, boolean_t is_spare) 225721ecdf64SLin Ling { 225821ecdf64SLin Ling char *type; 225921ecdf64SLin Ling int ret; 226021ecdf64SLin Ling 226121ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 226221ecdf64SLin Ling return (EZFS_INVALCONFIG); 226321ecdf64SLin Ling 226421ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) { 226521ecdf64SLin Ling /* 226621ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 226721ecdf64SLin Ling * For a spare vdev, we only want to boot from the active 226821ecdf64SLin Ling * spare device. 226921ecdf64SLin Ling */ 227021ecdf64SLin Ling if (is_spare) { 227121ecdf64SLin Ling uint64_t spare = 0; 227221ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 227321ecdf64SLin Ling &spare); 227421ecdf64SLin Ling if (!spare) 227521ecdf64SLin Ling return (EZFS_INVALCONFIG); 227621ecdf64SLin Ling } 227721ecdf64SLin Ling 227821ecdf64SLin Ling if (vdev_online(nv)) { 227921ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath, 228021ecdf64SLin Ling phypath_size, rsz)) != 0) 228121ecdf64SLin Ling return (ret); 228221ecdf64SLin Ling } 228321ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 2284d5f26ad8SToomas Soome strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 228521ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 || 228621ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 228721ecdf64SLin Ling nvlist_t **child; 228821ecdf64SLin Ling uint_t count; 228921ecdf64SLin Ling int i, ret; 229021ecdf64SLin Ling 229121ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv, 229221ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 229321ecdf64SLin Ling return (EZFS_INVALCONFIG); 229421ecdf64SLin Ling 229521ecdf64SLin Ling for (i = 0; i < count; i++) { 229621ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath, 229721ecdf64SLin Ling phypath_size, rsz, is_spare); 229821ecdf64SLin Ling if (ret == EZFS_NOSPC) 229921ecdf64SLin Ling return (ret); 230021ecdf64SLin Ling } 230121ecdf64SLin Ling } 230221ecdf64SLin Ling 230321ecdf64SLin Ling return (EZFS_POOL_INVALARG); 230421ecdf64SLin Ling } 230521ecdf64SLin Ling 2306753a6d45SSherry Moore /* 2307753a6d45SSherry Moore * Get phys_path for a root pool config. 2308753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2309753a6d45SSherry Moore */ 2310753a6d45SSherry Moore static int 2311753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2312753a6d45SSherry Moore { 2313753a6d45SSherry Moore size_t rsz; 231419397407SSherry Moore nvlist_t *vdev_root; 231519397407SSherry Moore nvlist_t **child; 231619397407SSherry Moore uint_t count; 2317753a6d45SSherry Moore char *type; 2318753a6d45SSherry Moore 2319753a6d45SSherry Moore rsz = 0; 2320753a6d45SSherry Moore 2321753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2322753a6d45SSherry Moore &vdev_root) != 0) 2323753a6d45SSherry Moore return (EZFS_INVALCONFIG); 2324753a6d45SSherry Moore 2325753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2326753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2327753a6d45SSherry Moore &child, &count) != 0) 2328753a6d45SSherry Moore return (EZFS_INVALCONFIG); 232919397407SSherry Moore 233019397407SSherry Moore /* 23311a902ef8SHans Rosenfeld * root pool can only have a single top-level vdev. 233219397407SSherry Moore */ 23331a902ef8SHans Rosenfeld if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1) 2334753a6d45SSherry Moore return (EZFS_POOL_INVALARG); 233519397407SSherry Moore 233621ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 233721ecdf64SLin Ling B_FALSE); 233819397407SSherry Moore 2339753a6d45SSherry Moore /* No online devices */ 2340753a6d45SSherry Moore if (rsz == 0) 2341753a6d45SSherry Moore return (EZFS_NODEVICE); 2342753a6d45SSherry Moore 234319397407SSherry Moore return (0); 234419397407SSherry Moore } 234519397407SSherry Moore 2346753a6d45SSherry Moore /* 2347753a6d45SSherry Moore * Get phys_path for a root pool 2348753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2349753a6d45SSherry Moore */ 2350753a6d45SSherry Moore int 2351753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2352753a6d45SSherry Moore { 2353753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2354753a6d45SSherry Moore phypath_size)); 2355753a6d45SSherry Moore } 2356753a6d45SSherry Moore 2357573ca77eSGeorge Wilson /* 2358573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel 2359573ca77eSGeorge Wilson * the disk to use the new unallocated space. 2360573ca77eSGeorge Wilson */ 2361573ca77eSGeorge Wilson static int 2362573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2363573ca77eSGeorge Wilson { 2364573ca77eSGeorge Wilson char path[MAXPATHLEN]; 2365573ca77eSGeorge Wilson char errbuf[1024]; 2366573ca77eSGeorge Wilson int fd, error; 2367573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int); 2368573ca77eSGeorge Wilson 2369573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2370573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL) 2371573ca77eSGeorge Wilson return (-1); 2372573ca77eSGeorge Wilson 23736401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name); 2374573ca77eSGeorge Wilson 2375573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2376573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2377573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name); 2378573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2379573ca77eSGeorge Wilson } 2380573ca77eSGeorge Wilson 2381573ca77eSGeorge Wilson /* 2382573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device 2383573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply 2384573ca77eSGeorge Wilson * ignore that error and continue on. 2385573ca77eSGeorge Wilson */ 2386573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd); 2387573ca77eSGeorge Wilson (void) close(fd); 2388573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) { 2389573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2390573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name); 2391573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2392573ca77eSGeorge Wilson } 2393573ca77eSGeorge Wilson return (0); 2394573ca77eSGeorge Wilson } 2395573ca77eSGeorge Wilson 2396fa9e4066Sahrens /* 23973d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 23983d7072f8Seschrock * ZFS_ONLINE_* flags. 2399fa9e4066Sahrens */ 2400fa9e4066Sahrens int 24013d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 24023d7072f8Seschrock vdev_state_t *newstate) 2403fa9e4066Sahrens { 2404fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2405fa9e4066Sahrens char msg[1024]; 240699653d4eSeschrock nvlist_t *tgt; 2407573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog; 240899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2409fa9e4066Sahrens 2410573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) { 2411573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2412573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2413573ca77eSGeorge Wilson } else { 2414573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2415573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2416573ca77eSGeorge Wilson } 2417ea8dc4b6Seschrock 2418fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2419ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2420573ca77eSGeorge Wilson &islog)) == NULL) 242199653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2422fa9e4066Sahrens 242399653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2424fa9e4066Sahrens 2425069f55e2SEric Schrock if (avail_spare) 2426a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2427a43d325bSek 2428573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND || 2429573ca77eSGeorge Wilson zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 2430573ca77eSGeorge Wilson char *pathname = NULL; 2431573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2432573ca77eSGeorge Wilson 2433573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2434573ca77eSGeorge Wilson &wholedisk); 2435573ca77eSGeorge Wilson verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 2436573ca77eSGeorge Wilson &pathname) == 0); 2437573ca77eSGeorge Wilson 2438573ca77eSGeorge Wilson /* 2439573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion. 2440573ca77eSGeorge Wilson */ 2441573ca77eSGeorge Wilson if (l2cache) { 2442573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2443573ca77eSGeorge Wilson "cannot expand cache devices")); 2444573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2445573ca77eSGeorge Wilson } 2446573ca77eSGeorge Wilson 2447573ca77eSGeorge Wilson if (wholedisk) { 24486401734dSWill Andrews pathname += strlen(ZFS_DISK_ROOT) + 1; 2449cb04b873SMark J Musante (void) zpool_relabel_disk(hdl, pathname); 2450573ca77eSGeorge Wilson } 2451573ca77eSGeorge Wilson } 2452573ca77eSGeorge Wilson 24533d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 24543d7072f8Seschrock zc.zc_obj = flags; 2455fa9e4066Sahrens 2456cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 24571195e687SMark J Musante if (errno == EINVAL) { 24581195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 24591195e687SMark J Musante "from this pool into a new one. Use '%s' " 24601195e687SMark J Musante "instead"), "zpool detach"); 24611195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 24621195e687SMark J Musante } 24633d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 24641195e687SMark J Musante } 24653d7072f8Seschrock 24663d7072f8Seschrock *newstate = zc.zc_cookie; 24673d7072f8Seschrock return (0); 2468fa9e4066Sahrens } 2469fa9e4066Sahrens 2470fa9e4066Sahrens /* 2471fa9e4066Sahrens * Take the specified vdev offline 2472fa9e4066Sahrens */ 2473fa9e4066Sahrens int 24743d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2475fa9e4066Sahrens { 2476fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2477fa9e4066Sahrens char msg[1024]; 247899653d4eSeschrock nvlist_t *tgt; 2479fa94a07fSbrendan boolean_t avail_spare, l2cache; 248099653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2481fa9e4066Sahrens 2482ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2483ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2484ea8dc4b6Seschrock 2485fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2486ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2487ee0eb9f2SEric Schrock NULL)) == NULL) 248899653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 248999653d4eSeschrock 249099653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2491fa9e4066Sahrens 2492069f55e2SEric Schrock if (avail_spare) 2493a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2494a43d325bSek 24953d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 24963d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 24973d7072f8Seschrock 2498cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 24993d7072f8Seschrock return (0); 25003d7072f8Seschrock 25013d7072f8Seschrock switch (errno) { 25023d7072f8Seschrock case EBUSY: 25033d7072f8Seschrock 25043d7072f8Seschrock /* 25053d7072f8Seschrock * There are no other replicas of this device. 25063d7072f8Seschrock */ 25073d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 25083d7072f8Seschrock 2509e6ca193dSGeorge Wilson case EEXIST: 2510e6ca193dSGeorge Wilson /* 2511e6ca193dSGeorge Wilson * The log device has unplayed logs 2512e6ca193dSGeorge Wilson */ 2513e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2514e6ca193dSGeorge Wilson 25153d7072f8Seschrock default: 25163d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 25173d7072f8Seschrock } 25183d7072f8Seschrock } 25193d7072f8Seschrock 25203d7072f8Seschrock /* 25213d7072f8Seschrock * Mark the given vdev faulted. 25223d7072f8Seschrock */ 25233d7072f8Seschrock int 2524069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 25253d7072f8Seschrock { 25263d7072f8Seschrock zfs_cmd_t zc = { 0 }; 25273d7072f8Seschrock char msg[1024]; 25283d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25293d7072f8Seschrock 25303d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 25313d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2532441d80aaSlling 25333d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25343d7072f8Seschrock zc.zc_guid = guid; 25353d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 2536069f55e2SEric Schrock zc.zc_obj = aux; 25373d7072f8Seschrock 2538cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2539fa9e4066Sahrens return (0); 2540fa9e4066Sahrens 2541fa9e4066Sahrens switch (errno) { 254299653d4eSeschrock case EBUSY: 2543fa9e4066Sahrens 2544fa9e4066Sahrens /* 2545fa9e4066Sahrens * There are no other replicas of this device. 2546fa9e4066Sahrens */ 254799653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2548fa9e4066Sahrens 254999653d4eSeschrock default: 255099653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2551fa9e4066Sahrens } 25523d7072f8Seschrock 25533d7072f8Seschrock } 25543d7072f8Seschrock 25553d7072f8Seschrock /* 25563d7072f8Seschrock * Mark the given vdev degraded. 25573d7072f8Seschrock */ 25583d7072f8Seschrock int 2559069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 25603d7072f8Seschrock { 25613d7072f8Seschrock zfs_cmd_t zc = { 0 }; 25623d7072f8Seschrock char msg[1024]; 25633d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25643d7072f8Seschrock 25653d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 25663d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 25673d7072f8Seschrock 25683d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25693d7072f8Seschrock zc.zc_guid = guid; 25703d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 2571069f55e2SEric Schrock zc.zc_obj = aux; 25723d7072f8Seschrock 2573cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 25743d7072f8Seschrock return (0); 25753d7072f8Seschrock 25763d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 257799653d4eSeschrock } 257899653d4eSeschrock 257999653d4eSeschrock /* 258099653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 258199653d4eSeschrock * a hot spare. 258299653d4eSeschrock */ 258399653d4eSeschrock static boolean_t 258499653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 258599653d4eSeschrock { 258699653d4eSeschrock nvlist_t **child; 258799653d4eSeschrock uint_t c, children; 258899653d4eSeschrock char *type; 258999653d4eSeschrock 259099653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 259199653d4eSeschrock &children) == 0) { 259299653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 259399653d4eSeschrock &type) == 0); 259499653d4eSeschrock 259599653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 259699653d4eSeschrock children == 2 && child[which] == tgt) 259799653d4eSeschrock return (B_TRUE); 259899653d4eSeschrock 259999653d4eSeschrock for (c = 0; c < children; c++) 260099653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 260199653d4eSeschrock return (B_TRUE); 260299653d4eSeschrock } 260399653d4eSeschrock 260499653d4eSeschrock return (B_FALSE); 2605fa9e4066Sahrens } 2606fa9e4066Sahrens 2607fa9e4066Sahrens /* 2608fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 26098654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2610fa9e4066Sahrens */ 2611fa9e4066Sahrens int 2612fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2613fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2614fa9e4066Sahrens { 2615fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2616fa9e4066Sahrens char msg[1024]; 2617fa9e4066Sahrens int ret; 261899653d4eSeschrock nvlist_t *tgt; 2619ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog; 2620ee0eb9f2SEric Schrock uint64_t val; 2621cb04b873SMark J Musante char *newname; 262299653d4eSeschrock nvlist_t **child; 262399653d4eSeschrock uint_t children; 262499653d4eSeschrock nvlist_t *config_root; 262599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 26264263d13fSGeorge Wilson boolean_t rootpool = zpool_is_bootable(zhp); 2627fa9e4066Sahrens 2628ea8dc4b6Seschrock if (replacing) 2629ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2630ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 2631ea8dc4b6Seschrock else 2632ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2633ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 2634ea8dc4b6Seschrock 2635fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2636ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 2637ee0eb9f2SEric Schrock &islog)) == 0) 263899653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 263999653d4eSeschrock 2640a43d325bSek if (avail_spare) 264199653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 264299653d4eSeschrock 2643fa94a07fSbrendan if (l2cache) 2644fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2645fa94a07fSbrendan 264699653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2647fa9e4066Sahrens zc.zc_cookie = replacing; 2648fa9e4066Sahrens 264999653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 265099653d4eSeschrock &child, &children) != 0 || children != 1) { 265199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 265299653d4eSeschrock "new device must be a single disk")); 265399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 265499653d4eSeschrock } 265599653d4eSeschrock 265699653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 265799653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 265899653d4eSeschrock 265988ecc943SGeorge Wilson if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 26600430f8daSeschrock return (-1); 26610430f8daSeschrock 266299653d4eSeschrock /* 266399653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 266499653d4eSeschrock * replace it with another hot spare. 266599653d4eSeschrock */ 266699653d4eSeschrock if (replacing && 266799653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2668ee0eb9f2SEric Schrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2669ee0eb9f2SEric Schrock NULL) == NULL || !avail_spare) && 2670ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) { 267199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 267299653d4eSeschrock "can only be replaced by another hot spare")); 26730430f8daSeschrock free(newname); 267499653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 267599653d4eSeschrock } 267699653d4eSeschrock 26770430f8daSeschrock free(newname); 26780430f8daSeschrock 2679990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 268099653d4eSeschrock return (-1); 2681fa9e4066Sahrens 2682cb04b873SMark J Musante ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2683fa9e4066Sahrens 2684e9dbad6fSeschrock zcmd_free_nvlists(&zc); 2685fa9e4066Sahrens 2686b5b76fecSGeorge Wilson if (ret == 0) { 2687b5b76fecSGeorge Wilson if (rootpool) { 268821ecdf64SLin Ling /* 268921ecdf64SLin Ling * XXX need a better way to prevent user from 269021ecdf64SLin Ling * booting up a half-baked vdev. 269121ecdf64SLin Ling */ 269221ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 269321ecdf64SLin Ling "sure to wait until resilver is done " 269421ecdf64SLin Ling "before rebooting.\n")); 2695b5b76fecSGeorge Wilson } 2696fa9e4066Sahrens return (0); 2697b5b76fecSGeorge Wilson } 2698fa9e4066Sahrens 2699fa9e4066Sahrens switch (errno) { 2700ea8dc4b6Seschrock case ENOTSUP: 2701fa9e4066Sahrens /* 2702fa9e4066Sahrens * Can't attach to or replace this type of vdev. 2703fa9e4066Sahrens */ 27048654d025Sperrin if (replacing) { 2705cb04b873SMark J Musante uint64_t version = zpool_get_prop_int(zhp, 2706cb04b873SMark J Musante ZPOOL_PROP_VERSION, NULL); 2707cb04b873SMark J Musante 2708ee0eb9f2SEric Schrock if (islog) 27098654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27108654d025Sperrin "cannot replace a log with a spare")); 2711cb04b873SMark J Musante else if (version >= SPA_VERSION_MULTI_REPLACE) 2712cb04b873SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2713cb04b873SMark J Musante "already in replacing/spare config; wait " 2714cb04b873SMark J Musante "for completion or use 'zpool detach'")); 27158654d025Sperrin else 27168654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27178654d025Sperrin "cannot replace a replacing device")); 27188654d025Sperrin } else { 271999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 272099653d4eSeschrock "can only attach to mirrors and top-level " 272199653d4eSeschrock "disks")); 27228654d025Sperrin } 272399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2724fa9e4066Sahrens break; 2725fa9e4066Sahrens 2726ea8dc4b6Seschrock case EINVAL: 2727fa9e4066Sahrens /* 2728fa9e4066Sahrens * The new device must be a single disk. 2729fa9e4066Sahrens */ 273099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 273199653d4eSeschrock "new device must be a single disk")); 273299653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2733fa9e4066Sahrens break; 2734fa9e4066Sahrens 2735ea8dc4b6Seschrock case EBUSY: 273699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 273799653d4eSeschrock new_disk); 273899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2739fa9e4066Sahrens break; 2740fa9e4066Sahrens 2741ea8dc4b6Seschrock case EOVERFLOW: 2742fa9e4066Sahrens /* 2743fa9e4066Sahrens * The new device is too small. 2744fa9e4066Sahrens */ 274599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 274699653d4eSeschrock "device is too small")); 274799653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2748fa9e4066Sahrens break; 2749fa9e4066Sahrens 2750ea8dc4b6Seschrock case EDOM: 2751fa9e4066Sahrens /* 2752fa9e4066Sahrens * The new device has a different alignment requirement. 2753fa9e4066Sahrens */ 275499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 275599653d4eSeschrock "devices have different sector alignment")); 275699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2757fa9e4066Sahrens break; 2758fa9e4066Sahrens 2759ea8dc4b6Seschrock case ENAMETOOLONG: 2760fa9e4066Sahrens /* 2761fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 2762fa9e4066Sahrens */ 276399653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2764fa9e4066Sahrens break; 2765fa9e4066Sahrens 2766ea8dc4b6Seschrock default: 276799653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2768fa9e4066Sahrens } 2769fa9e4066Sahrens 277099653d4eSeschrock return (-1); 2771fa9e4066Sahrens } 2772fa9e4066Sahrens 2773fa9e4066Sahrens /* 2774fa9e4066Sahrens * Detach the specified device. 2775fa9e4066Sahrens */ 2776fa9e4066Sahrens int 2777fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2778fa9e4066Sahrens { 2779fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2780fa9e4066Sahrens char msg[1024]; 278199653d4eSeschrock nvlist_t *tgt; 2782fa94a07fSbrendan boolean_t avail_spare, l2cache; 278399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2784fa9e4066Sahrens 2785ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2786ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2787ea8dc4b6Seschrock 2788fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2789ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2790ee0eb9f2SEric Schrock NULL)) == 0) 279199653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2792fa9e4066Sahrens 2793a43d325bSek if (avail_spare) 279499653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 279599653d4eSeschrock 2796fa94a07fSbrendan if (l2cache) 2797fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2798fa94a07fSbrendan 279999653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 280099653d4eSeschrock 2801ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2802fa9e4066Sahrens return (0); 2803fa9e4066Sahrens 2804fa9e4066Sahrens switch (errno) { 2805fa9e4066Sahrens 2806ea8dc4b6Seschrock case ENOTSUP: 2807fa9e4066Sahrens /* 2808fa9e4066Sahrens * Can't detach from this type of vdev. 2809fa9e4066Sahrens */ 281099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 281199653d4eSeschrock "applicable to mirror and replacing vdevs")); 2812cb04b873SMark J Musante (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2813fa9e4066Sahrens break; 2814fa9e4066Sahrens 2815ea8dc4b6Seschrock case EBUSY: 2816fa9e4066Sahrens /* 2817fa9e4066Sahrens * There are no other replicas of this device. 2818fa9e4066Sahrens */ 281999653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2820fa9e4066Sahrens break; 2821fa9e4066Sahrens 2822ea8dc4b6Seschrock default: 282399653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2824ea8dc4b6Seschrock } 2825ea8dc4b6Seschrock 282699653d4eSeschrock return (-1); 282799653d4eSeschrock } 282899653d4eSeschrock 28291195e687SMark J Musante /* 28301195e687SMark J Musante * Find a mirror vdev in the source nvlist. 28311195e687SMark J Musante * 28321195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors 28331195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the 28341195e687SMark J Musante * user specified on the command line. We loop over the mchild array to 28351195e687SMark J Musante * see if any entry in the schild array matches. 28361195e687SMark J Musante * 28371195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return 28381195e687SMark J Musante * the index of that entry. Otherwise we return -1. 28391195e687SMark J Musante */ 28401195e687SMark J Musante static int 28411195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 28421195e687SMark J Musante nvlist_t **schild, uint_t schildren) 28431195e687SMark J Musante { 28441195e687SMark J Musante uint_t mc; 28451195e687SMark J Musante 28461195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) { 28471195e687SMark J Musante uint_t sc; 28481195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 28491195e687SMark J Musante mchild[mc], B_FALSE); 28501195e687SMark J Musante 28511195e687SMark J Musante for (sc = 0; sc < schildren; sc++) { 28521195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 28531195e687SMark J Musante schild[sc], B_FALSE); 28541195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0); 28551195e687SMark J Musante 28561195e687SMark J Musante free(spath); 28571195e687SMark J Musante if (result) { 28581195e687SMark J Musante free(mpath); 28591195e687SMark J Musante return (mc); 28601195e687SMark J Musante } 28611195e687SMark J Musante } 28621195e687SMark J Musante 28631195e687SMark J Musante free(mpath); 28641195e687SMark J Musante } 28651195e687SMark J Musante 28661195e687SMark J Musante return (-1); 28671195e687SMark J Musante } 28681195e687SMark J Musante 28691195e687SMark J Musante /* 28701195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist 28711195e687SMark J Musante * is generated and it is the responsibility of the caller to free it. 28721195e687SMark J Musante */ 28731195e687SMark J Musante int 28741195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 28751195e687SMark J Musante nvlist_t *props, splitflags_t flags) 28761195e687SMark J Musante { 28771195e687SMark J Musante zfs_cmd_t zc = { 0 }; 28781195e687SMark J Musante char msg[1024]; 28791195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 28801195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL; 28811195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 28821195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl; 28831195e687SMark J Musante uint64_t vers; 28841195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE; 28851195e687SMark J Musante int retval = 0; 28861195e687SMark J Musante 28871195e687SMark J Musante (void) snprintf(msg, sizeof (msg), 28881195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 28891195e687SMark J Musante 28901195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname)) 28911195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 28921195e687SMark J Musante 28931195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) { 28941195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to " 28951195e687SMark J Musante "retrieve pool configuration\n")); 28961195e687SMark J Musante return (-1); 28971195e687SMark J Musante } 28981195e687SMark J Musante 28991195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 29001195e687SMark J Musante == 0); 29011195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 29021195e687SMark J Musante 29031195e687SMark J Musante if (props) { 2904f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 29051195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 2906f9af39baSGeorge Wilson props, vers, flags, msg)) == NULL) 29071195e687SMark J Musante return (-1); 29081195e687SMark J Musante } 29091195e687SMark J Musante 29101195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 29111195e687SMark J Musante &children) != 0) { 29121195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29131195e687SMark J Musante "Source pool is missing vdev tree")); 2914aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 29151195e687SMark J Musante return (-1); 29161195e687SMark J Musante } 29171195e687SMark J Musante 29181195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 29191195e687SMark J Musante vcount = 0; 29201195e687SMark J Musante 29211195e687SMark J Musante if (*newroot == NULL || 29221195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 29231195e687SMark J Musante &newchild, &newchildren) != 0) 29241195e687SMark J Musante newchildren = 0; 29251195e687SMark J Musante 29261195e687SMark J Musante for (c = 0; c < children; c++) { 29271195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE; 29281195e687SMark J Musante char *type; 29291195e687SMark J Musante nvlist_t **mchild, *vdev; 29301195e687SMark J Musante uint_t mchildren; 29311195e687SMark J Musante int entry; 29321195e687SMark J Musante 29331195e687SMark J Musante /* 29341195e687SMark J Musante * Unlike cache & spares, slogs are stored in the 29351195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 29361195e687SMark J Musante */ 29371195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 29381195e687SMark J Musante &is_log); 29391195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 29401195e687SMark J Musante &is_hole); 29411195e687SMark J Musante if (is_log || is_hole) { 29421195e687SMark J Musante /* 29431195e687SMark J Musante * Create a hole vdev and put it in the config. 29441195e687SMark J Musante */ 29451195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 29461195e687SMark J Musante goto out; 29471195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 29481195e687SMark J Musante VDEV_TYPE_HOLE) != 0) 29491195e687SMark J Musante goto out; 29501195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 29511195e687SMark J Musante 1) != 0) 29521195e687SMark J Musante goto out; 29531195e687SMark J Musante if (lastlog == 0) 29541195e687SMark J Musante lastlog = vcount; 29551195e687SMark J Musante varray[vcount++] = vdev; 29561195e687SMark J Musante continue; 29571195e687SMark J Musante } 29581195e687SMark J Musante lastlog = 0; 29591195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 29601195e687SMark J Musante == 0); 29611195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 29621195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29631195e687SMark J Musante "Source pool must be composed only of mirrors\n")); 29641195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 29651195e687SMark J Musante goto out; 29661195e687SMark J Musante } 29671195e687SMark J Musante 29681195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c], 29691195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 29701195e687SMark J Musante 29711195e687SMark J Musante /* find or add an entry for this top-level vdev */ 29721195e687SMark J Musante if (newchildren > 0 && 29731195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren, 29741195e687SMark J Musante newchild, newchildren)) >= 0) { 29751195e687SMark J Musante /* We found a disk that the user specified. */ 29761195e687SMark J Musante vdev = mchild[entry]; 29771195e687SMark J Musante ++found; 29781195e687SMark J Musante } else { 29791195e687SMark J Musante /* User didn't specify a disk for this vdev. */ 29801195e687SMark J Musante vdev = mchild[mchildren - 1]; 29811195e687SMark J Musante } 29821195e687SMark J Musante 29831195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 29841195e687SMark J Musante goto out; 29851195e687SMark J Musante } 29861195e687SMark J Musante 29871195e687SMark J Musante /* did we find every disk the user specified? */ 29881195e687SMark J Musante if (found != newchildren) { 29891195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 29901195e687SMark J Musante "include at most one disk from each mirror")); 29911195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 29921195e687SMark J Musante goto out; 29931195e687SMark J Musante } 29941195e687SMark J Musante 29951195e687SMark J Musante /* Prepare the nvlist for populating. */ 29961195e687SMark J Musante if (*newroot == NULL) { 29971195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 29981195e687SMark J Musante goto out; 29991195e687SMark J Musante freelist = B_TRUE; 30001195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 30011195e687SMark J Musante VDEV_TYPE_ROOT) != 0) 30021195e687SMark J Musante goto out; 30031195e687SMark J Musante } else { 30041195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 30051195e687SMark J Musante } 30061195e687SMark J Musante 30071195e687SMark J Musante /* Add all the children we found */ 30081195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 30091195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0) 30101195e687SMark J Musante goto out; 30111195e687SMark J Musante 30121195e687SMark J Musante /* 30131195e687SMark J Musante * If we're just doing a dry run, exit now with success. 30141195e687SMark J Musante */ 30151195e687SMark J Musante if (flags.dryrun) { 30161195e687SMark J Musante memory_err = B_FALSE; 30171195e687SMark J Musante freelist = B_FALSE; 30181195e687SMark J Musante goto out; 30191195e687SMark J Musante } 30201195e687SMark J Musante 30211195e687SMark J Musante /* now build up the config list & call the ioctl */ 30221195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 30231195e687SMark J Musante goto out; 30241195e687SMark J Musante 30251195e687SMark J Musante if (nvlist_add_nvlist(newconfig, 30261195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 30271195e687SMark J Musante nvlist_add_string(newconfig, 30281195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 30291195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 30301195e687SMark J Musante goto out; 30311195e687SMark J Musante 30321195e687SMark J Musante /* 30331195e687SMark J Musante * The new pool is automatically part of the namespace unless we 30341195e687SMark J Musante * explicitly export it. 30351195e687SMark J Musante */ 30361195e687SMark J Musante if (!flags.import) 30371195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 30381195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 30391195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 30401195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 30411195e687SMark J Musante goto out; 30421195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 30431195e687SMark J Musante goto out; 30441195e687SMark J Musante 30451195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 30461195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg); 30471195e687SMark J Musante goto out; 30481195e687SMark J Musante } 30491195e687SMark J Musante 30501195e687SMark J Musante freelist = B_FALSE; 30511195e687SMark J Musante memory_err = B_FALSE; 30521195e687SMark J Musante 30531195e687SMark J Musante out: 30541195e687SMark J Musante if (varray != NULL) { 30551195e687SMark J Musante int v; 30561195e687SMark J Musante 30571195e687SMark J Musante for (v = 0; v < vcount; v++) 30581195e687SMark J Musante nvlist_free(varray[v]); 30591195e687SMark J Musante free(varray); 30601195e687SMark J Musante } 30611195e687SMark J Musante zcmd_free_nvlists(&zc); 3062aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 3063aab83bb8SJosef 'Jeff' Sipek nvlist_free(newconfig); 30641195e687SMark J Musante if (freelist) { 30651195e687SMark J Musante nvlist_free(*newroot); 30661195e687SMark J Musante *newroot = NULL; 30671195e687SMark J Musante } 30681195e687SMark J Musante 30691195e687SMark J Musante if (retval != 0) 30701195e687SMark J Musante return (retval); 30711195e687SMark J Musante 30721195e687SMark J Musante if (memory_err) 30731195e687SMark J Musante return (no_memory(hdl)); 30741195e687SMark J Musante 30751195e687SMark J Musante return (0); 30761195e687SMark J Musante } 30771195e687SMark J Musante 307899653d4eSeschrock /* 3079fa94a07fSbrendan * Remove the given device. Currently, this is supported only for hot spares 3080fa94a07fSbrendan * and level 2 cache devices. 308199653d4eSeschrock */ 308299653d4eSeschrock int 308399653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 308499653d4eSeschrock { 308599653d4eSeschrock zfs_cmd_t zc = { 0 }; 308699653d4eSeschrock char msg[1024]; 308799653d4eSeschrock nvlist_t *tgt; 308888ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog; 308999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 309088ecc943SGeorge Wilson uint64_t version; 309199653d4eSeschrock 309299653d4eSeschrock (void) snprintf(msg, sizeof (msg), 309399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 309499653d4eSeschrock 309599653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3096ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 309788ecc943SGeorge Wilson &islog)) == 0) 309899653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 309988ecc943SGeorge Wilson /* 310088ecc943SGeorge Wilson * XXX - this should just go away. 310188ecc943SGeorge Wilson */ 310288ecc943SGeorge Wilson if (!avail_spare && !l2cache && !islog) { 310399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 310488ecc943SGeorge Wilson "only inactive hot spares, cache, top-level, " 310588ecc943SGeorge Wilson "or log devices can be removed")); 310699653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 310799653d4eSeschrock } 310899653d4eSeschrock 310988ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 311088ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) { 311188ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 311288ecc943SGeorge Wilson "pool must be upgrade to support log removal")); 311388ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg)); 311488ecc943SGeorge Wilson } 311588ecc943SGeorge Wilson 311699653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 311799653d4eSeschrock 3118ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 311999653d4eSeschrock return (0); 312099653d4eSeschrock 312199653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3122ea8dc4b6Seschrock } 3123ea8dc4b6Seschrock 3124ea8dc4b6Seschrock /* 3125ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 3126ea8dc4b6Seschrock */ 3127ea8dc4b6Seschrock int 3128468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 3129ea8dc4b6Seschrock { 3130ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3131ea8dc4b6Seschrock char msg[1024]; 313299653d4eSeschrock nvlist_t *tgt; 3133468c413aSTim Haley zpool_rewind_policy_t policy; 3134fa94a07fSbrendan boolean_t avail_spare, l2cache; 313599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3136468c413aSTim Haley nvlist_t *nvi = NULL; 31374b964adaSGeorge Wilson int error; 3138ea8dc4b6Seschrock 3139ea8dc4b6Seschrock if (path) 3140ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3141ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3142e9dbad6fSeschrock path); 3143ea8dc4b6Seschrock else 3144ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3145ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3146ea8dc4b6Seschrock zhp->zpool_name); 3147ea8dc4b6Seschrock 3148ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 314999653d4eSeschrock if (path) { 3150fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 3151ee0eb9f2SEric Schrock &l2cache, NULL)) == 0) 315299653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3153ea8dc4b6Seschrock 3154fa94a07fSbrendan /* 3155fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 3156fa94a07fSbrendan * error clearing for l2cache devices. 3157fa94a07fSbrendan */ 3158a43d325bSek if (avail_spare) 315999653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3160ea8dc4b6Seschrock 316199653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 316299653d4eSeschrock &zc.zc_guid) == 0); 3163fa9e4066Sahrens } 3164fa9e4066Sahrens 3165468c413aSTim Haley zpool_get_rewind_policy(rewindnvl, &policy); 3166468c413aSTim Haley zc.zc_cookie = policy.zrp_request; 3167468c413aSTim Haley 316857f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 3169468c413aSTim Haley return (-1); 3170468c413aSTim Haley 3171cb04b873SMark J Musante if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 3172468c413aSTim Haley return (-1); 3173468c413aSTim Haley 31744b964adaSGeorge Wilson while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 31754b964adaSGeorge Wilson errno == ENOMEM) { 31764b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 31774b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 31784b964adaSGeorge Wilson return (-1); 31794b964adaSGeorge Wilson } 31804b964adaSGeorge Wilson } 31814b964adaSGeorge Wilson 31824b964adaSGeorge Wilson if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 3183468c413aSTim Haley errno != EPERM && errno != EACCES)) { 3184468c413aSTim Haley if (policy.zrp_request & 3185468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 3186468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 3187468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name, 3188468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 3189468c413aSTim Haley nvi); 3190468c413aSTim Haley nvlist_free(nvi); 3191468c413aSTim Haley } 3192468c413aSTim Haley zcmd_free_nvlists(&zc); 319399653d4eSeschrock return (0); 3194468c413aSTim Haley } 319599653d4eSeschrock 3196468c413aSTim Haley zcmd_free_nvlists(&zc); 319799653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3198fa9e4066Sahrens } 3199fa9e4066Sahrens 32003d7072f8Seschrock /* 32013d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 32023d7072f8Seschrock */ 32033d7072f8Seschrock int 32043d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 32053d7072f8Seschrock { 32063d7072f8Seschrock zfs_cmd_t zc = { 0 }; 32073d7072f8Seschrock char msg[1024]; 32083d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 32093d7072f8Seschrock 32103d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 32113d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 32123d7072f8Seschrock guid); 32133d7072f8Seschrock 32143d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 32153d7072f8Seschrock zc.zc_guid = guid; 321614f8ce41SVictor Latushkin zc.zc_cookie = ZPOOL_NO_REWIND; 32173d7072f8Seschrock 32183d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 32193d7072f8Seschrock return (0); 32203d7072f8Seschrock 32213d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 32223d7072f8Seschrock } 32233d7072f8Seschrock 3224e9103aaeSGarrett D'Amore /* 3225e9103aaeSGarrett D'Amore * Change the GUID for a pool. 3226e9103aaeSGarrett D'Amore */ 3227e9103aaeSGarrett D'Amore int 3228e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp) 3229e9103aaeSGarrett D'Amore { 3230e9103aaeSGarrett D'Amore char msg[1024]; 3231e9103aaeSGarrett D'Amore libzfs_handle_t *hdl = zhp->zpool_hdl; 3232e9103aaeSGarrett D'Amore zfs_cmd_t zc = { 0 }; 3233e9103aaeSGarrett D'Amore 3234e9103aaeSGarrett D'Amore (void) snprintf(msg, sizeof (msg), 3235e9103aaeSGarrett D'Amore dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 3236e9103aaeSGarrett D'Amore 3237e9103aaeSGarrett D'Amore (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3238e9103aaeSGarrett D'Amore if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 3239e9103aaeSGarrett D'Amore return (0); 3240e9103aaeSGarrett D'Amore 3241e9103aaeSGarrett D'Amore return (zpool_standard_error(hdl, errno, msg)); 3242e9103aaeSGarrett D'Amore } 3243e9103aaeSGarrett D'Amore 32444263d13fSGeorge Wilson /* 32454263d13fSGeorge Wilson * Reopen the pool. 32464263d13fSGeorge Wilson */ 32474263d13fSGeorge Wilson int 32484263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp) 32494263d13fSGeorge Wilson { 32504263d13fSGeorge Wilson zfs_cmd_t zc = { 0 }; 32514263d13fSGeorge Wilson char msg[1024]; 32524263d13fSGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl; 32534263d13fSGeorge Wilson 32544263d13fSGeorge Wilson (void) snprintf(msg, sizeof (msg), 32554263d13fSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), 32564263d13fSGeorge Wilson zhp->zpool_name); 32574263d13fSGeorge Wilson 32584263d13fSGeorge Wilson (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 32594263d13fSGeorge Wilson if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0) 32604263d13fSGeorge Wilson return (0); 32614263d13fSGeorge Wilson return (zpool_standard_error(hdl, errno, msg)); 32624263d13fSGeorge Wilson } 32634263d13fSGeorge Wilson 3264c67d9675Seschrock /* 3265c67d9675Seschrock * Convert from a devid string to a path. 3266c67d9675Seschrock */ 3267c67d9675Seschrock static char * 3268c67d9675Seschrock devid_to_path(char *devid_str) 3269c67d9675Seschrock { 3270c67d9675Seschrock ddi_devid_t devid; 3271c67d9675Seschrock char *minor; 3272c67d9675Seschrock char *path; 3273c67d9675Seschrock devid_nmlist_t *list = NULL; 3274c67d9675Seschrock int ret; 3275c67d9675Seschrock 3276c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 3277c67d9675Seschrock return (NULL); 3278c67d9675Seschrock 3279c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 3280c67d9675Seschrock 3281c67d9675Seschrock devid_str_free(minor); 3282c67d9675Seschrock devid_free(devid); 3283c67d9675Seschrock 3284c67d9675Seschrock if (ret != 0) 3285c67d9675Seschrock return (NULL); 3286c67d9675Seschrock 3287078266a5SMarcel Telka /* 3288078266a5SMarcel Telka * In a case the strdup() fails, we will just return NULL below. 3289078266a5SMarcel Telka */ 3290078266a5SMarcel Telka path = strdup(list[0].devname); 329199653d4eSeschrock 3292c67d9675Seschrock devid_free_nmlist(list); 3293c67d9675Seschrock 3294c67d9675Seschrock return (path); 3295c67d9675Seschrock } 3296c67d9675Seschrock 3297c67d9675Seschrock /* 3298c67d9675Seschrock * Convert from a path to a devid string. 3299c67d9675Seschrock */ 3300c67d9675Seschrock static char * 3301c67d9675Seschrock path_to_devid(const char *path) 3302c67d9675Seschrock { 3303c67d9675Seschrock int fd; 3304c67d9675Seschrock ddi_devid_t devid; 3305c67d9675Seschrock char *minor, *ret; 3306c67d9675Seschrock 3307c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 3308c67d9675Seschrock return (NULL); 3309c67d9675Seschrock 3310c67d9675Seschrock minor = NULL; 3311c67d9675Seschrock ret = NULL; 3312c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 3313c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 3314c67d9675Seschrock ret = devid_str_encode(devid, minor); 3315c67d9675Seschrock if (minor != NULL) 3316c67d9675Seschrock devid_str_free(minor); 3317c67d9675Seschrock devid_free(devid); 3318c67d9675Seschrock } 3319c67d9675Seschrock (void) close(fd); 3320c67d9675Seschrock 3321c67d9675Seschrock return (ret); 3322c67d9675Seschrock } 3323c67d9675Seschrock 3324c67d9675Seschrock /* 3325c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 3326c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 3327c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 3328c67d9675Seschrock */ 3329c67d9675Seschrock static void 3330c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3331c67d9675Seschrock { 3332c67d9675Seschrock zfs_cmd_t zc = { 0 }; 3333c67d9675Seschrock 3334c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3335e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3336c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3337ea8dc4b6Seschrock &zc.zc_guid) == 0); 3338c67d9675Seschrock 333999653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3340c67d9675Seschrock } 3341c67d9675Seschrock 3342c67d9675Seschrock /* 3343c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 3344c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3345c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 3346c67d9675Seschrock * trailing 's0' slice name. 3347c67d9675Seschrock * 3348c67d9675Seschrock * This routine is also responsible for identifying when disks have been 3349c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 3350c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 3351c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 3352c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 3353c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 3354c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3355c67d9675Seschrock * of these checks. 3356c67d9675Seschrock */ 3357c67d9675Seschrock char * 335888ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 335988ecc943SGeorge Wilson boolean_t verbose) 3360c67d9675Seschrock { 3361c67d9675Seschrock char *path, *devid; 3362ea8dc4b6Seschrock uint64_t value; 3363ea8dc4b6Seschrock char buf[64]; 33643d7072f8Seschrock vdev_stat_t *vs; 33653d7072f8Seschrock uint_t vsc; 3366c67d9675Seschrock 3367ea8dc4b6Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3368ea8dc4b6Seschrock &value) == 0) { 3369ea8dc4b6Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3370ea8dc4b6Seschrock &value) == 0); 33715ad82045Snd (void) snprintf(buf, sizeof (buf), "%llu", 33725ad82045Snd (u_longlong_t)value); 3373ea8dc4b6Seschrock path = buf; 3374ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3375c67d9675Seschrock 33763d7072f8Seschrock /* 33773d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 33783d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 33793d7072f8Seschrock * open a misbehaving device, which can have undesirable 33803d7072f8Seschrock * effects. 33813d7072f8Seschrock */ 33823f9d6ad7SLin Ling if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 33833d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 33843d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 33853d7072f8Seschrock zhp != NULL && 3386c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3387c67d9675Seschrock /* 3388c67d9675Seschrock * Determine if the current path is correct. 3389c67d9675Seschrock */ 3390c67d9675Seschrock char *newdevid = path_to_devid(path); 3391c67d9675Seschrock 3392c67d9675Seschrock if (newdevid == NULL || 3393c67d9675Seschrock strcmp(devid, newdevid) != 0) { 3394c67d9675Seschrock char *newpath; 3395c67d9675Seschrock 3396c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 3397c67d9675Seschrock /* 3398c67d9675Seschrock * Update the path appropriately. 3399c67d9675Seschrock */ 3400c67d9675Seschrock set_path(zhp, nv, newpath); 340199653d4eSeschrock if (nvlist_add_string(nv, 340299653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 340399653d4eSeschrock verify(nvlist_lookup_string(nv, 340499653d4eSeschrock ZPOOL_CONFIG_PATH, 340599653d4eSeschrock &path) == 0); 3406c67d9675Seschrock free(newpath); 3407c67d9675Seschrock } 3408c67d9675Seschrock } 3409c67d9675Seschrock 341099653d4eSeschrock if (newdevid) 341199653d4eSeschrock devid_str_free(newdevid); 3412c67d9675Seschrock } 3413c67d9675Seschrock 34146401734dSWill Andrews if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) 34156401734dSWill Andrews path += strlen(ZFS_DISK_ROOTD); 3416c67d9675Seschrock 3417c67d9675Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3418ea8dc4b6Seschrock &value) == 0 && value) { 34193fdda499SJohn Harres int pathlen = strlen(path); 342099653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 34213fdda499SJohn Harres 34223fdda499SJohn Harres /* 3423*7855d95bSToomas Soome * If it starts with c#, and ends with "s0" or "s1", 3424*7855d95bSToomas Soome * chop the slice off, or if it ends with "s0/old" or 3425*7855d95bSToomas Soome * "s1/old", remove the slice from the middle. 34263fdda499SJohn Harres */ 34273fdda499SJohn Harres if (CTD_CHECK(tmp)) { 3428*7855d95bSToomas Soome if (strcmp(&tmp[pathlen - 2], "s0") == 0 || 3429*7855d95bSToomas Soome strcmp(&tmp[pathlen - 2], "s1") == 0) { 34303fdda499SJohn Harres tmp[pathlen - 2] = '\0'; 34313fdda499SJohn Harres } else if (pathlen > 6 && 3432*7855d95bSToomas Soome (strcmp(&tmp[pathlen - 6], "s0/old") == 0 || 3433*7855d95bSToomas Soome strcmp(&tmp[pathlen - 6], "s1/old") == 0)) { 34343fdda499SJohn Harres (void) strcpy(&tmp[pathlen - 6], 34353fdda499SJohn Harres "/old"); 34363fdda499SJohn Harres } 34373fdda499SJohn Harres } 3438c67d9675Seschrock return (tmp); 3439c67d9675Seschrock } 3440c67d9675Seschrock } else { 3441c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 344299653d4eSeschrock 344399653d4eSeschrock /* 344499653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 344599653d4eSeschrock */ 344699653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 344799653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 344899653d4eSeschrock &value) == 0); 344999653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 34505ad82045Snd (u_longlong_t)value); 345199653d4eSeschrock path = buf; 345299653d4eSeschrock } 345388ecc943SGeorge Wilson 345488ecc943SGeorge Wilson /* 345588ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id> 345688ecc943SGeorge Wilson * naming convention. 345788ecc943SGeorge Wilson */ 345888ecc943SGeorge Wilson if (verbose) { 345988ecc943SGeorge Wilson uint64_t id; 346088ecc943SGeorge Wilson 346188ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 346288ecc943SGeorge Wilson &id) == 0); 346388ecc943SGeorge Wilson (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 346488ecc943SGeorge Wilson (u_longlong_t)id); 346588ecc943SGeorge Wilson path = buf; 346688ecc943SGeorge Wilson } 3467c67d9675Seschrock } 3468c67d9675Seschrock 346999653d4eSeschrock return (zfs_strdup(hdl, path)); 3470c67d9675Seschrock } 3471ea8dc4b6Seschrock 3472ea8dc4b6Seschrock static int 3473a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b) 3474ea8dc4b6Seschrock { 34757802d7bfSMatthew Ahrens return (memcmp(a, b, sizeof (zbookmark_phys_t))); 3476ea8dc4b6Seschrock } 3477ea8dc4b6Seschrock 3478ea8dc4b6Seschrock /* 3479ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 3480ea8dc4b6Seschrock * caller. 3481ea8dc4b6Seschrock */ 3482ea8dc4b6Seschrock int 348355434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3484ea8dc4b6Seschrock { 3485ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3486ea8dc4b6Seschrock uint64_t count; 34877802d7bfSMatthew Ahrens zbookmark_phys_t *zb = NULL; 348855434c77Sek int i; 3489ea8dc4b6Seschrock 3490ea8dc4b6Seschrock /* 3491ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 3492ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 3493ea8dc4b6Seschrock * entire list. 3494ea8dc4b6Seschrock */ 3495ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3496ea8dc4b6Seschrock &count) == 0); 349775519f38Sek if (count == 0) 349875519f38Sek return (0); 3499e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 35007802d7bfSMatthew Ahrens count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL) 350199653d4eSeschrock return (-1); 3502e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 3503ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3504ea8dc4b6Seschrock for (;;) { 350599653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 350699653d4eSeschrock &zc) != 0) { 3507e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 3508ea8dc4b6Seschrock if (errno == ENOMEM) { 35097802d7bfSMatthew Ahrens void *dst; 35107802d7bfSMatthew Ahrens 3511bf561db0Svb count = zc.zc_nvlist_dst_size; 35127802d7bfSMatthew Ahrens dst = zfs_alloc(zhp->zpool_hdl, count * 35137802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 35147802d7bfSMatthew Ahrens if (dst == NULL) 351599653d4eSeschrock return (-1); 35167802d7bfSMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)dst; 3517ea8dc4b6Seschrock } else { 3518ea8dc4b6Seschrock return (-1); 3519ea8dc4b6Seschrock } 3520ea8dc4b6Seschrock } else { 3521ea8dc4b6Seschrock break; 3522ea8dc4b6Seschrock } 3523ea8dc4b6Seschrock } 3524ea8dc4b6Seschrock 3525ea8dc4b6Seschrock /* 3526ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 3527ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3528e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3529ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 3530ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 3531ea8dc4b6Seschrock */ 35327802d7bfSMatthew Ahrens zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) + 3533e9dbad6fSeschrock zc.zc_nvlist_dst_size; 3534e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 3535ea8dc4b6Seschrock 3536a2cdcdd2SPaul Dagnelie qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare); 3537ea8dc4b6Seschrock 353855434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3539ea8dc4b6Seschrock 3540ea8dc4b6Seschrock /* 354155434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3542ea8dc4b6Seschrock */ 3543ea8dc4b6Seschrock for (i = 0; i < count; i++) { 3544ea8dc4b6Seschrock nvlist_t *nv; 3545ea8dc4b6Seschrock 3546c0a81264Sek /* ignoring zb_blkid and zb_level for now */ 3547c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3548c0a81264Sek zb[i-1].zb_object == zb[i].zb_object) 3549ea8dc4b6Seschrock continue; 3550ea8dc4b6Seschrock 355155434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 355255434c77Sek goto nomem; 355355434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 355455434c77Sek zb[i].zb_objset) != 0) { 355555434c77Sek nvlist_free(nv); 355699653d4eSeschrock goto nomem; 3557ea8dc4b6Seschrock } 355855434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 355955434c77Sek zb[i].zb_object) != 0) { 356055434c77Sek nvlist_free(nv); 356155434c77Sek goto nomem; 356255434c77Sek } 356355434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 356455434c77Sek nvlist_free(nv); 356555434c77Sek goto nomem; 356655434c77Sek } 356755434c77Sek nvlist_free(nv); 3568ea8dc4b6Seschrock } 3569ea8dc4b6Seschrock 35703ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 3571ea8dc4b6Seschrock return (0); 357299653d4eSeschrock 357399653d4eSeschrock nomem: 3574e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 357599653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 3576ea8dc4b6Seschrock } 3577eaca9bbdSeschrock 3578eaca9bbdSeschrock /* 3579eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 3580eaca9bbdSeschrock */ 3581eaca9bbdSeschrock int 3582990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3583eaca9bbdSeschrock { 3584eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 358599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3586eaca9bbdSeschrock 3587eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3588990b4856Slling zc.zc_cookie = new_version; 3589990b4856Slling 3590ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3591ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 359299653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 359399653d4eSeschrock zhp->zpool_name)); 3594eaca9bbdSeschrock return (0); 3595eaca9bbdSeschrock } 359606eeb2adSek 359706eeb2adSek void 35984445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len) 359906eeb2adSek { 36004445fffbSMatthew Ahrens (void) strlcpy(string, basename(argv[0]), len); 36014445fffbSMatthew Ahrens for (int i = 1; i < argc; i++) { 36024445fffbSMatthew Ahrens (void) strlcat(string, " ", len); 36034445fffbSMatthew Ahrens (void) strlcat(string, argv[i], len); 36042a6b87f0Sek } 36052a6b87f0Sek } 36062a6b87f0Sek 36072a6b87f0Sek int 36084445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message) 36092a6b87f0Sek { 36104445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 }; 36114445fffbSMatthew Ahrens nvlist_t *args; 36124445fffbSMatthew Ahrens int err; 36134445fffbSMatthew Ahrens 36144445fffbSMatthew Ahrens args = fnvlist_alloc(); 36154445fffbSMatthew Ahrens fnvlist_add_string(args, "message", message); 36164445fffbSMatthew Ahrens err = zcmd_write_src_nvlist(hdl, &zc, args); 36174445fffbSMatthew Ahrens if (err == 0) 36184445fffbSMatthew Ahrens err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc); 36194445fffbSMatthew Ahrens nvlist_free(args); 36204445fffbSMatthew Ahrens zcmd_free_nvlists(&zc); 36214445fffbSMatthew Ahrens return (err); 362206eeb2adSek } 362306eeb2adSek 362406eeb2adSek /* 362506eeb2adSek * Perform ioctl to get some command history of a pool. 362606eeb2adSek * 362706eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 362806eeb2adSek * logical offset of the history buffer to start reading from. 362906eeb2adSek * 363006eeb2adSek * Upon return, 'off' is the next logical offset to read from and 363106eeb2adSek * 'len' is the actual amount of bytes read into 'buf'. 363206eeb2adSek */ 363306eeb2adSek static int 363406eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 363506eeb2adSek { 363606eeb2adSek zfs_cmd_t zc = { 0 }; 363706eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl; 363806eeb2adSek 363906eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 364006eeb2adSek 364106eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf; 364206eeb2adSek zc.zc_history_len = *len; 364306eeb2adSek zc.zc_history_offset = *off; 364406eeb2adSek 364506eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 364606eeb2adSek switch (errno) { 364706eeb2adSek case EPERM: 3648ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 3649ece3d9b3Slling dgettext(TEXT_DOMAIN, 365006eeb2adSek "cannot show history for pool '%s'"), 365106eeb2adSek zhp->zpool_name)); 365206eeb2adSek case ENOENT: 3653ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 365406eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool " 365506eeb2adSek "'%s'"), zhp->zpool_name)); 3656d7306b64Sek case ENOTSUP: 3657d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3658d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool " 3659d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name)); 366006eeb2adSek default: 3661ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 366206eeb2adSek dgettext(TEXT_DOMAIN, 366306eeb2adSek "cannot get history for '%s'"), zhp->zpool_name)); 366406eeb2adSek } 366506eeb2adSek } 366606eeb2adSek 366706eeb2adSek *len = zc.zc_history_len; 366806eeb2adSek *off = zc.zc_history_offset; 366906eeb2adSek 367006eeb2adSek return (0); 367106eeb2adSek } 367206eeb2adSek 367306eeb2adSek /* 367406eeb2adSek * Process the buffer of nvlists, unpacking and storing each nvlist record 367506eeb2adSek * into 'records'. 'leftover' is set to the number of bytes that weren't 367606eeb2adSek * processed as there wasn't a complete record. 367706eeb2adSek */ 36788f18d1faSGeorge Wilson int 367906eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 368006eeb2adSek nvlist_t ***records, uint_t *numrecords) 368106eeb2adSek { 368206eeb2adSek uint64_t reclen; 368306eeb2adSek nvlist_t *nv; 368406eeb2adSek int i; 368506eeb2adSek 368606eeb2adSek while (bytes_read > sizeof (reclen)) { 368706eeb2adSek 368806eeb2adSek /* get length of packed record (stored as little endian) */ 368906eeb2adSek for (i = 0, reclen = 0; i < sizeof (reclen); i++) 369006eeb2adSek reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 369106eeb2adSek 369206eeb2adSek if (bytes_read < sizeof (reclen) + reclen) 369306eeb2adSek break; 369406eeb2adSek 369506eeb2adSek /* unpack record */ 369606eeb2adSek if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 369706eeb2adSek return (ENOMEM); 369806eeb2adSek bytes_read -= sizeof (reclen) + reclen; 369906eeb2adSek buf += sizeof (reclen) + reclen; 370006eeb2adSek 370106eeb2adSek /* add record to nvlist array */ 370206eeb2adSek (*numrecords)++; 370306eeb2adSek if (ISP2(*numrecords + 1)) { 370406eeb2adSek *records = realloc(*records, 370506eeb2adSek *numrecords * 2 * sizeof (nvlist_t *)); 370606eeb2adSek } 370706eeb2adSek (*records)[*numrecords - 1] = nv; 370806eeb2adSek } 370906eeb2adSek 371006eeb2adSek *leftover = bytes_read; 371106eeb2adSek return (0); 371206eeb2adSek } 371306eeb2adSek 371406eeb2adSek /* 371506eeb2adSek * Retrieve the command history of a pool. 371606eeb2adSek */ 371706eeb2adSek int 371806eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 371906eeb2adSek { 37203339867aSMatthew Ahrens char *buf; 37213339867aSMatthew Ahrens int buflen = 128 * 1024; 372206eeb2adSek uint64_t off = 0; 372306eeb2adSek nvlist_t **records = NULL; 372406eeb2adSek uint_t numrecords = 0; 372506eeb2adSek int err, i; 372606eeb2adSek 37273339867aSMatthew Ahrens buf = malloc(buflen); 37283339867aSMatthew Ahrens if (buf == NULL) 37293339867aSMatthew Ahrens return (ENOMEM); 373006eeb2adSek do { 37313339867aSMatthew Ahrens uint64_t bytes_read = buflen; 373206eeb2adSek uint64_t leftover; 373306eeb2adSek 373406eeb2adSek if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 373506eeb2adSek break; 373606eeb2adSek 373706eeb2adSek /* if nothing else was read in, we're at EOF, just return */ 373806eeb2adSek if (!bytes_read) 373906eeb2adSek break; 374006eeb2adSek 374106eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read, 374206eeb2adSek &leftover, &records, &numrecords)) != 0) 374306eeb2adSek break; 374406eeb2adSek off -= leftover; 37453339867aSMatthew Ahrens if (leftover == bytes_read) { 37463339867aSMatthew Ahrens /* 37473339867aSMatthew Ahrens * no progress made, because buffer is not big enough 37483339867aSMatthew Ahrens * to hold this record; resize and retry. 37493339867aSMatthew Ahrens */ 37503339867aSMatthew Ahrens buflen *= 2; 37513339867aSMatthew Ahrens free(buf); 37523339867aSMatthew Ahrens buf = malloc(buflen); 37533339867aSMatthew Ahrens if (buf == NULL) 37543339867aSMatthew Ahrens return (ENOMEM); 37553339867aSMatthew Ahrens } 375606eeb2adSek 375706eeb2adSek /* CONSTCOND */ 375806eeb2adSek } while (1); 375906eeb2adSek 37603339867aSMatthew Ahrens free(buf); 37613339867aSMatthew Ahrens 376206eeb2adSek if (!err) { 376306eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 376406eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 376506eeb2adSek records, numrecords) == 0); 376606eeb2adSek } 376706eeb2adSek for (i = 0; i < numrecords; i++) 376806eeb2adSek nvlist_free(records[i]); 376906eeb2adSek free(records); 377006eeb2adSek 377106eeb2adSek return (err); 377206eeb2adSek } 377355434c77Sek 377455434c77Sek void 377555434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 377655434c77Sek char *pathname, size_t len) 377755434c77Sek { 377855434c77Sek zfs_cmd_t zc = { 0 }; 377955434c77Sek boolean_t mounted = B_FALSE; 378055434c77Sek char *mntpnt = NULL; 37819adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 378255434c77Sek 378355434c77Sek if (dsobj == 0) { 378455434c77Sek /* special case for the MOS */ 378555434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 378655434c77Sek return; 378755434c77Sek } 378855434c77Sek 378955434c77Sek /* get the dataset's name */ 379055434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 379155434c77Sek zc.zc_obj = dsobj; 379255434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, 379355434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 379455434c77Sek /* just write out a path of two object numbers */ 379555434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 379655434c77Sek dsobj, obj); 379755434c77Sek return; 379855434c77Sek } 379955434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 380055434c77Sek 380155434c77Sek /* find out if the dataset is mounted */ 380255434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 380355434c77Sek 380455434c77Sek /* get the corrupted object's path */ 380555434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 380655434c77Sek zc.zc_obj = obj; 380755434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 380855434c77Sek &zc) == 0) { 380955434c77Sek if (mounted) { 381055434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt, 381155434c77Sek zc.zc_value); 381255434c77Sek } else { 381355434c77Sek (void) snprintf(pathname, len, "%s:%s", 381455434c77Sek dsname, zc.zc_value); 381555434c77Sek } 381655434c77Sek } else { 381755434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 381855434c77Sek } 381955434c77Sek free(mntpnt); 382055434c77Sek } 3821b1b8ab34Slling 382215e6edf1Sgw /* 382315e6edf1Sgw * Read the EFI label from the config, if a label does not exist then 382415e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL 382515e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI 3826*7855d95bSToomas Soome * partition. If the caller has passed a non-NULL boolean argument, then 3827*7855d95bSToomas Soome * we set it to indicate if the disk does have efi system partition. 382815e6edf1Sgw */ 382915e6edf1Sgw static int 3830*7855d95bSToomas Soome read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system) 383115e6edf1Sgw { 383215e6edf1Sgw char *path; 383315e6edf1Sgw int fd; 383415e6edf1Sgw char diskname[MAXPATHLEN]; 3835*7855d95bSToomas Soome boolean_t boot = B_FALSE; 383615e6edf1Sgw int err = -1; 3837*7855d95bSToomas Soome int slice; 383815e6edf1Sgw 383915e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 384015e6edf1Sgw return (err); 384115e6edf1Sgw 38426401734dSWill Andrews (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT, 384315e6edf1Sgw strrchr(path, '/')); 384415e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 384515e6edf1Sgw struct dk_gpt *vtoc; 384615e6edf1Sgw 384715e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 3848*7855d95bSToomas Soome for (slice = 0; slice < vtoc->efi_nparts; slice++) { 3849*7855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_SYSTEM) 3850*7855d95bSToomas Soome boot = B_TRUE; 3851*7855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_USR) 3852*7855d95bSToomas Soome break; 3853*7855d95bSToomas Soome } 3854*7855d95bSToomas Soome if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR) 3855*7855d95bSToomas Soome *sb = vtoc->efi_parts[slice].p_start; 3856*7855d95bSToomas Soome if (system != NULL) 3857*7855d95bSToomas Soome *system = boot; 385815e6edf1Sgw efi_free(vtoc); 385915e6edf1Sgw } 386015e6edf1Sgw (void) close(fd); 386115e6edf1Sgw } 386215e6edf1Sgw return (err); 386315e6edf1Sgw } 386415e6edf1Sgw 38658488aeb5Staylor /* 38668488aeb5Staylor * determine where a partition starts on a disk in the current 38678488aeb5Staylor * configuration 38688488aeb5Staylor */ 38698488aeb5Staylor static diskaddr_t 38708488aeb5Staylor find_start_block(nvlist_t *config) 38718488aeb5Staylor { 38728488aeb5Staylor nvlist_t **child; 38738488aeb5Staylor uint_t c, children; 38748488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 38758488aeb5Staylor uint64_t wholedisk; 38768488aeb5Staylor 38778488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 38788488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 38798488aeb5Staylor if (nvlist_lookup_uint64(config, 38808488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 38818488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 38828488aeb5Staylor return (MAXOFFSET_T); 38838488aeb5Staylor } 3884*7855d95bSToomas Soome if (read_efi_label(config, &sb, NULL) < 0) 388515e6edf1Sgw sb = MAXOFFSET_T; 38868488aeb5Staylor return (sb); 38878488aeb5Staylor } 38888488aeb5Staylor 38898488aeb5Staylor for (c = 0; c < children; c++) { 38908488aeb5Staylor sb = find_start_block(child[c]); 38918488aeb5Staylor if (sb != MAXOFFSET_T) { 38928488aeb5Staylor return (sb); 38938488aeb5Staylor } 38948488aeb5Staylor } 38958488aeb5Staylor return (MAXOFFSET_T); 38968488aeb5Staylor } 38978488aeb5Staylor 38988488aeb5Staylor /* 38998488aeb5Staylor * Label an individual disk. The name provided is the short name, 39008488aeb5Staylor * stripped of any leading /dev path. 39018488aeb5Staylor */ 39028488aeb5Staylor int 3903*7855d95bSToomas Soome zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name, 3904*7855d95bSToomas Soome zpool_boot_label_t boot_type, uint64_t boot_size, int *slice) 39058488aeb5Staylor { 39068488aeb5Staylor char path[MAXPATHLEN]; 39078488aeb5Staylor struct dk_gpt *vtoc; 39088488aeb5Staylor int fd; 39098488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 39108488aeb5Staylor uint64_t slice_size; 39118488aeb5Staylor diskaddr_t start_block; 39128488aeb5Staylor char errbuf[1024]; 39138488aeb5Staylor 3914c6ef114fSmmusante /* prepare an error message just in case */ 3915c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 3916c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 3917c6ef114fSmmusante 39188488aeb5Staylor if (zhp) { 39198488aeb5Staylor nvlist_t *nvroot; 39208488aeb5Staylor 39218488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 39228488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 39238488aeb5Staylor 39248488aeb5Staylor if (zhp->zpool_start_block == 0) 39258488aeb5Staylor start_block = find_start_block(nvroot); 39268488aeb5Staylor else 39278488aeb5Staylor start_block = zhp->zpool_start_block; 39288488aeb5Staylor zhp->zpool_start_block = start_block; 39298488aeb5Staylor } else { 39308488aeb5Staylor /* new pool */ 39318488aeb5Staylor start_block = NEW_START_BLOCK; 39328488aeb5Staylor } 39338488aeb5Staylor 39346401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name, 39358488aeb5Staylor BACKUP_SLICE); 39368488aeb5Staylor 39378488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 39388488aeb5Staylor /* 39398488aeb5Staylor * This shouldn't happen. We've long since verified that this 39408488aeb5Staylor * is a valid device. 39418488aeb5Staylor */ 3942c6ef114fSmmusante zfs_error_aux(hdl, 3943c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 39448488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 39458488aeb5Staylor } 39468488aeb5Staylor 39478488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 39488488aeb5Staylor /* 39498488aeb5Staylor * The only way this can fail is if we run out of memory, or we 39508488aeb5Staylor * were unable to read the disk's capacity 39518488aeb5Staylor */ 39528488aeb5Staylor if (errno == ENOMEM) 39538488aeb5Staylor (void) no_memory(hdl); 39548488aeb5Staylor 39558488aeb5Staylor (void) close(fd); 3956c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3957c6ef114fSmmusante "unable to read disk capacity"), name); 39588488aeb5Staylor 39598488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 39608488aeb5Staylor } 39618488aeb5Staylor 39628488aeb5Staylor /* 39638488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 39648488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 39658488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 39668488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 39678488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 39688488aeb5Staylor * can get, in the absence of V_OTHER. 39698488aeb5Staylor */ 3970*7855d95bSToomas Soome /* first fix the partition start block */ 3971*7855d95bSToomas Soome if (start_block == MAXOFFSET_T) 3972*7855d95bSToomas Soome start_block = NEW_START_BLOCK; 39738488aeb5Staylor 3974*7855d95bSToomas Soome /* 3975*7855d95bSToomas Soome * EFI System partition is using slice 0. 3976*7855d95bSToomas Soome * ZFS is on slice 1 and slice 8 is reserved. 3977*7855d95bSToomas Soome * We assume the GPT partition table without system 3978*7855d95bSToomas Soome * partition has zfs p_start == NEW_START_BLOCK. 3979*7855d95bSToomas Soome * If start_block != NEW_START_BLOCK, it means we have 3980*7855d95bSToomas Soome * system partition. Correct solution would be to query/cache vtoc 3981*7855d95bSToomas Soome * from existing vdev member. 3982*7855d95bSToomas Soome */ 3983*7855d95bSToomas Soome if (boot_type == ZPOOL_CREATE_BOOT_LABEL) { 3984*7855d95bSToomas Soome if (boot_size % vtoc->efi_lbasize != 0) { 3985*7855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3986*7855d95bSToomas Soome "boot partition size must be a multiple of %d"), 3987*7855d95bSToomas Soome vtoc->efi_lbasize); 3988*7855d95bSToomas Soome (void) close(fd); 3989*7855d95bSToomas Soome efi_free(vtoc); 3990*7855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 3991*7855d95bSToomas Soome } 3992*7855d95bSToomas Soome /* 3993*7855d95bSToomas Soome * System partition size checks. 3994*7855d95bSToomas Soome * Note the 1MB is quite arbitrary value, since we 3995*7855d95bSToomas Soome * are creating dedicated pool, it should be enough 3996*7855d95bSToomas Soome * to hold fat + efi bootloader. May need to be 3997*7855d95bSToomas Soome * adjusted if the bootloader size will grow. 3998*7855d95bSToomas Soome */ 3999*7855d95bSToomas Soome if (boot_size < 1024 * 1024) { 4000*7855d95bSToomas Soome char buf[64]; 4001*7855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf)); 4002*7855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4003*7855d95bSToomas Soome "Specified size %s for EFI System partition is too " 4004*7855d95bSToomas Soome "small, the minimum size is 1MB."), buf); 4005*7855d95bSToomas Soome (void) close(fd); 4006*7855d95bSToomas Soome efi_free(vtoc); 4007*7855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 4008*7855d95bSToomas Soome } 4009*7855d95bSToomas Soome /* 33MB is tested with mkfs -F pcfs */ 4010*7855d95bSToomas Soome if (hdl->libzfs_printerr && 4011*7855d95bSToomas Soome ((vtoc->efi_lbasize == 512 && 4012*7855d95bSToomas Soome boot_size < 33 * 1024 * 1024) || 4013*7855d95bSToomas Soome (vtoc->efi_lbasize == 4096 && 4014*7855d95bSToomas Soome boot_size < 256 * 1024 * 1024))) { 4015*7855d95bSToomas Soome char buf[64]; 4016*7855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf)); 4017*7855d95bSToomas Soome (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 4018*7855d95bSToomas Soome "Warning: EFI System partition size %s is " 4019*7855d95bSToomas Soome "not allowing to create FAT32 file\nsystem, which " 4020*7855d95bSToomas Soome "may result in unbootable system.\n"), buf); 4021*7855d95bSToomas Soome } 4022*7855d95bSToomas Soome /* Adjust zfs partition start by size of system partition. */ 4023*7855d95bSToomas Soome start_block += boot_size / vtoc->efi_lbasize; 4024*7855d95bSToomas Soome } 4025*7855d95bSToomas Soome 4026*7855d95bSToomas Soome if (start_block == NEW_START_BLOCK) { 4027*7855d95bSToomas Soome /* 4028*7855d95bSToomas Soome * Use default layout. 4029*7855d95bSToomas Soome * ZFS is on slice 0 and slice 8 is reserved. 4030*7855d95bSToomas Soome */ 4031*7855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1; 4032*7855d95bSToomas Soome slice_size -= EFI_MIN_RESV_SIZE; 4033*7855d95bSToomas Soome slice_size -= start_block; 4034*7855d95bSToomas Soome if (slice != NULL) 4035*7855d95bSToomas Soome *slice = 0; 4036*7855d95bSToomas Soome 4037*7855d95bSToomas Soome vtoc->efi_parts[0].p_start = start_block; 4038*7855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size; 4039*7855d95bSToomas Soome 4040*7855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_USR; 4041*7855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 4042*7855d95bSToomas Soome 4043*7855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block; 4044*7855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv; 4045*7855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED; 4046*7855d95bSToomas Soome } else { 4047*7855d95bSToomas Soome slice_size = start_block - NEW_START_BLOCK; 4048*7855d95bSToomas Soome vtoc->efi_parts[0].p_start = NEW_START_BLOCK; 4049*7855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size; 4050*7855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_SYSTEM; 4051*7855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "loader"); 4052*7855d95bSToomas Soome if (slice != NULL) 4053*7855d95bSToomas Soome *slice = 1; 4054*7855d95bSToomas Soome /* prepare slice 1 */ 4055*7855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1 - slice_size; 4056*7855d95bSToomas Soome slice_size -= resv; 4057*7855d95bSToomas Soome slice_size -= NEW_START_BLOCK; 4058*7855d95bSToomas Soome vtoc->efi_parts[1].p_start = start_block; 4059*7855d95bSToomas Soome vtoc->efi_parts[1].p_size = slice_size; 4060*7855d95bSToomas Soome vtoc->efi_parts[1].p_tag = V_USR; 4061*7855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[1].p_name, "zfs"); 4062*7855d95bSToomas Soome 4063*7855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block; 4064*7855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv; 4065*7855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED; 4066*7855d95bSToomas Soome } 40678488aeb5Staylor 40688488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 40698488aeb5Staylor /* 40708488aeb5Staylor * Some block drivers (like pcata) may not support EFI 40718488aeb5Staylor * GPT labels. Print out a helpful error message dir- 40728488aeb5Staylor * ecting the user to manually label the disk and give 40738488aeb5Staylor * a specific slice. 40748488aeb5Staylor */ 40758488aeb5Staylor (void) close(fd); 40768488aeb5Staylor efi_free(vtoc); 40778488aeb5Staylor 40788488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4079c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 40808488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 40818488aeb5Staylor } 40828488aeb5Staylor 40838488aeb5Staylor (void) close(fd); 40848488aeb5Staylor efi_free(vtoc); 40858488aeb5Staylor return (0); 40868488aeb5Staylor } 4087e7cbe64fSgw 4088e7cbe64fSgw static boolean_t 4089e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 4090e7cbe64fSgw { 4091e7cbe64fSgw char *type; 4092e7cbe64fSgw nvlist_t **child; 4093e7cbe64fSgw uint_t children, c; 4094e7cbe64fSgw 4095e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 4096810e43b2SBill Pijewski if (strcmp(type, VDEV_TYPE_FILE) == 0 || 409788ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 || 4098e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) { 4099e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4100e7cbe64fSgw "vdev type '%s' is not supported"), type); 4101e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 4102e7cbe64fSgw return (B_FALSE); 4103e7cbe64fSgw } 4104e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 4105e7cbe64fSgw &child, &children) == 0) { 4106e7cbe64fSgw for (c = 0; c < children; c++) { 4107e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 4108e7cbe64fSgw return (B_FALSE); 4109e7cbe64fSgw } 4110e7cbe64fSgw } 4111e7cbe64fSgw return (B_TRUE); 4112e7cbe64fSgw } 4113e7cbe64fSgw 4114e7cbe64fSgw /* 4115810e43b2SBill Pijewski * Check if this zvol is allowable for use as a dump device; zero if 4116810e43b2SBill Pijewski * it is, > 0 if it isn't, < 0 if it isn't a zvol. 4117810e43b2SBill Pijewski * 4118810e43b2SBill Pijewski * Allowable storage configurations include mirrors, all raidz variants, and 4119810e43b2SBill Pijewski * pools with log, cache, and spare devices. Pools which are backed by files or 4120810e43b2SBill Pijewski * have missing/hole vdevs are not suitable. 4121e7cbe64fSgw */ 4122e7cbe64fSgw int 4123e7cbe64fSgw zvol_check_dump_config(char *arg) 4124e7cbe64fSgw { 4125e7cbe64fSgw zpool_handle_t *zhp = NULL; 4126e7cbe64fSgw nvlist_t *config, *nvroot; 4127e7cbe64fSgw char *p, *volname; 4128e7cbe64fSgw nvlist_t **top; 4129e7cbe64fSgw uint_t toplevels; 4130e7cbe64fSgw libzfs_handle_t *hdl; 4131e7cbe64fSgw char errbuf[1024]; 41329adfa60dSMatthew Ahrens char poolname[ZFS_MAX_DATASET_NAME_LEN]; 4133e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR); 4134e7cbe64fSgw int ret = 1; 4135e7cbe64fSgw 4136e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 4137e7cbe64fSgw return (-1); 4138e7cbe64fSgw } 4139e7cbe64fSgw 4140e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4141e7cbe64fSgw "dump is not supported on device '%s'"), arg); 4142e7cbe64fSgw 4143e7cbe64fSgw if ((hdl = libzfs_init()) == NULL) 4144e7cbe64fSgw return (1); 4145e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE); 4146e7cbe64fSgw 4147e7cbe64fSgw volname = arg + pathlen; 4148e7cbe64fSgw 4149e7cbe64fSgw /* check the configuration of the pool */ 4150e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) { 4151e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4152e7cbe64fSgw "malformed dataset name")); 4153e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4154e7cbe64fSgw return (1); 41559adfa60dSMatthew Ahrens } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) { 4156e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4157e7cbe64fSgw "dataset name is too long")); 4158e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 4159e7cbe64fSgw return (1); 4160e7cbe64fSgw } else { 4161e7cbe64fSgw (void) strncpy(poolname, volname, p - volname); 4162e7cbe64fSgw poolname[p - volname] = '\0'; 4163e7cbe64fSgw } 4164e7cbe64fSgw 4165e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) { 4166e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4167e7cbe64fSgw "could not open pool '%s'"), poolname); 4168e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4169e7cbe64fSgw goto out; 4170e7cbe64fSgw } 4171e7cbe64fSgw config = zpool_get_config(zhp, NULL); 4172e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4173e7cbe64fSgw &nvroot) != 0) { 4174e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4175e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname); 4176e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 4177e7cbe64fSgw goto out; 4178e7cbe64fSgw } 4179e7cbe64fSgw 4180e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 4181e7cbe64fSgw &top, &toplevels) == 0); 4182e7cbe64fSgw 4183e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 4184e7cbe64fSgw goto out; 4185e7cbe64fSgw } 4186e7cbe64fSgw ret = 0; 4187e7cbe64fSgw 4188e7cbe64fSgw out: 4189e7cbe64fSgw if (zhp) 4190e7cbe64fSgw zpool_close(zhp); 4191e7cbe64fSgw libzfs_fini(hdl); 4192e7cbe64fSgw return (ret); 4193e7cbe64fSgw } 4194