1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 212082Seschrock 22789Sahrens /* 235756Seschrock * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens /* 28789Sahrens * This file contains all the routines used when modifying on-disk SPA state. 29789Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 30789Sahrens * pool. 31789Sahrens */ 32789Sahrens 33789Sahrens #include <sys/zfs_context.h> 341544Seschrock #include <sys/fm/fs/zfs.h> 35789Sahrens #include <sys/spa_impl.h> 36789Sahrens #include <sys/zio.h> 37789Sahrens #include <sys/zio_checksum.h> 38789Sahrens #include <sys/zio_compress.h> 39789Sahrens #include <sys/dmu.h> 40789Sahrens #include <sys/dmu_tx.h> 41789Sahrens #include <sys/zap.h> 42789Sahrens #include <sys/zil.h> 43789Sahrens #include <sys/vdev_impl.h> 44789Sahrens #include <sys/metaslab.h> 45789Sahrens #include <sys/uberblock_impl.h> 46789Sahrens #include <sys/txg.h> 47789Sahrens #include <sys/avl.h> 48789Sahrens #include <sys/dmu_traverse.h> 493912Slling #include <sys/dmu_objset.h> 50789Sahrens #include <sys/unique.h> 51789Sahrens #include <sys/dsl_pool.h> 523912Slling #include <sys/dsl_dataset.h> 53789Sahrens #include <sys/dsl_dir.h> 54789Sahrens #include <sys/dsl_prop.h> 553912Slling #include <sys/dsl_synctask.h> 56789Sahrens #include <sys/fs/zfs.h> 575450Sbrendan #include <sys/arc.h> 58789Sahrens #include <sys/callb.h> 593975Sek110237 #include <sys/systeminfo.h> 603975Sek110237 #include <sys/sunddi.h> 616423Sgw25295 #include <sys/spa_boot.h> 62789Sahrens 635094Slling #include "zfs_prop.h" 645913Sperrin #include "zfs_comutil.h" 655094Slling 662986Sek110237 int zio_taskq_threads = 8; 672986Sek110237 685094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 697214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa); 705094Slling 715094Slling /* 725094Slling * ========================================================================== 735094Slling * SPA properties routines 745094Slling * ========================================================================== 755094Slling */ 765094Slling 775094Slling /* 785094Slling * Add a (source=src, propname=propval) list to an nvlist. 795094Slling */ 805949Slling static void 815094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 825094Slling uint64_t intval, zprop_source_t src) 835094Slling { 845094Slling const char *propname = zpool_prop_to_name(prop); 855094Slling nvlist_t *propval; 865949Slling 875949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 885949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 895949Slling 905949Slling if (strval != NULL) 915949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 925949Slling else 935949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 945949Slling 955949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 965094Slling nvlist_free(propval); 975094Slling } 985094Slling 995094Slling /* 1005094Slling * Get property values from the spa configuration. 1015094Slling */ 1025949Slling static void 1035094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1045094Slling { 1055094Slling uint64_t size = spa_get_space(spa); 1065094Slling uint64_t used = spa_get_alloc(spa); 1075094Slling uint64_t cap, version; 1085094Slling zprop_source_t src = ZPROP_SRC_NONE; 1096643Seschrock spa_config_dirent_t *dp; 1105094Slling 1115094Slling /* 1125094Slling * readonly properties 1135094Slling */ 1145949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src); 1155949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 1165949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 1175949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src); 1185094Slling 1195094Slling cap = (size == 0) ? 0 : (used * 100 / size); 1205949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1215949Slling 1225949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1235949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1245949Slling spa->spa_root_vdev->vdev_state, src); 1255094Slling 1265094Slling /* 1275094Slling * settable properties that are not stored in the pool property object. 1285094Slling */ 1295094Slling version = spa_version(spa); 1305094Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1315094Slling src = ZPROP_SRC_DEFAULT; 1325094Slling else 1335094Slling src = ZPROP_SRC_LOCAL; 1345949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1355949Slling 1365949Slling if (spa->spa_root != NULL) 1375949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1385949Slling 0, ZPROP_SRC_LOCAL); 1395094Slling 1406643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 1416643Seschrock if (dp->scd_path == NULL) { 1425949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1436643Seschrock "none", 0, ZPROP_SRC_LOCAL); 1446643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1455949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1466643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1475363Seschrock } 1485363Seschrock } 1495094Slling } 1505094Slling 1515094Slling /* 1525094Slling * Get zpool property values. 1535094Slling */ 1545094Slling int 1555094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 1565094Slling { 1575094Slling zap_cursor_t zc; 1585094Slling zap_attribute_t za; 1595094Slling objset_t *mos = spa->spa_meta_objset; 1605094Slling int err; 1615094Slling 1625949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1635094Slling 1645094Slling /* 1655094Slling * Get properties from the spa config. 1665094Slling */ 1675949Slling spa_prop_get_config(spa, nvp); 1685094Slling 1695094Slling mutex_enter(&spa->spa_props_lock); 1705094Slling /* If no pool property object, no more prop to get. */ 1715094Slling if (spa->spa_pool_props_object == 0) { 1725094Slling mutex_exit(&spa->spa_props_lock); 1735094Slling return (0); 1745094Slling } 1755094Slling 1765094Slling /* 1775094Slling * Get properties from the MOS pool property object. 1785094Slling */ 1795094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 1805094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 1815094Slling zap_cursor_advance(&zc)) { 1825094Slling uint64_t intval = 0; 1835094Slling char *strval = NULL; 1845094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 1855094Slling zpool_prop_t prop; 1865094Slling 1875094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 1885094Slling continue; 1895094Slling 1905094Slling switch (za.za_integer_length) { 1915094Slling case 8: 1925094Slling /* integer property */ 1935094Slling if (za.za_first_integer != 1945094Slling zpool_prop_default_numeric(prop)) 1955094Slling src = ZPROP_SRC_LOCAL; 1965094Slling 1975094Slling if (prop == ZPOOL_PROP_BOOTFS) { 1985094Slling dsl_pool_t *dp; 1995094Slling dsl_dataset_t *ds = NULL; 2005094Slling 2015094Slling dp = spa_get_dsl(spa); 2025094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2036689Smaybee if (err = dsl_dataset_hold_obj(dp, 2046689Smaybee za.za_first_integer, FTAG, &ds)) { 2055094Slling rw_exit(&dp->dp_config_rwlock); 2065094Slling break; 2075094Slling } 2085094Slling 2095094Slling strval = kmem_alloc( 2105094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2115094Slling KM_SLEEP); 2125094Slling dsl_dataset_name(ds, strval); 2136689Smaybee dsl_dataset_rele(ds, FTAG); 2145094Slling rw_exit(&dp->dp_config_rwlock); 2155094Slling } else { 2165094Slling strval = NULL; 2175094Slling intval = za.za_first_integer; 2185094Slling } 2195094Slling 2205949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2215094Slling 2225094Slling if (strval != NULL) 2235094Slling kmem_free(strval, 2245094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2255094Slling 2265094Slling break; 2275094Slling 2285094Slling case 1: 2295094Slling /* string property */ 2305094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2315094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2325094Slling za.za_name, 1, za.za_num_integers, strval); 2335094Slling if (err) { 2345094Slling kmem_free(strval, za.za_num_integers); 2355094Slling break; 2365094Slling } 2375949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2385094Slling kmem_free(strval, za.za_num_integers); 2395094Slling break; 2405094Slling 2415094Slling default: 2425094Slling break; 2435094Slling } 2445094Slling } 2455094Slling zap_cursor_fini(&zc); 2465094Slling mutex_exit(&spa->spa_props_lock); 2475094Slling out: 2485094Slling if (err && err != ENOENT) { 2495094Slling nvlist_free(*nvp); 2505949Slling *nvp = NULL; 2515094Slling return (err); 2525094Slling } 2535094Slling 2545094Slling return (0); 2555094Slling } 2565094Slling 2575094Slling /* 2585094Slling * Validate the given pool properties nvlist and modify the list 2595094Slling * for the property values to be set. 2605094Slling */ 2615094Slling static int 2625094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 2635094Slling { 2645094Slling nvpair_t *elem; 2655094Slling int error = 0, reset_bootfs = 0; 2665094Slling uint64_t objnum; 2675094Slling 2685094Slling elem = NULL; 2695094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2705094Slling zpool_prop_t prop; 2715094Slling char *propname, *strval; 2725094Slling uint64_t intval; 2735094Slling objset_t *os; 2745363Seschrock char *slash; 2755094Slling 2765094Slling propname = nvpair_name(elem); 2775094Slling 2785094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 2795094Slling return (EINVAL); 2805094Slling 2815094Slling switch (prop) { 2825094Slling case ZPOOL_PROP_VERSION: 2835094Slling error = nvpair_value_uint64(elem, &intval); 2845094Slling if (!error && 2855094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 2865094Slling error = EINVAL; 2875094Slling break; 2885094Slling 2895094Slling case ZPOOL_PROP_DELEGATION: 2905094Slling case ZPOOL_PROP_AUTOREPLACE: 2917538SRichard.Morris@Sun.COM case ZPOOL_PROP_LISTSNAPS: 2925094Slling error = nvpair_value_uint64(elem, &intval); 2935094Slling if (!error && intval > 1) 2945094Slling error = EINVAL; 2955094Slling break; 2965094Slling 2975094Slling case ZPOOL_PROP_BOOTFS: 2985094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 2995094Slling error = ENOTSUP; 3005094Slling break; 3015094Slling } 3025094Slling 3035094Slling /* 3047042Sgw25295 * Make sure the vdev config is bootable 3055094Slling */ 3067042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3075094Slling error = ENOTSUP; 3085094Slling break; 3095094Slling } 3105094Slling 3115094Slling reset_bootfs = 1; 3125094Slling 3135094Slling error = nvpair_value_string(elem, &strval); 3145094Slling 3155094Slling if (!error) { 3167042Sgw25295 uint64_t compress; 3177042Sgw25295 3185094Slling if (strval == NULL || strval[0] == '\0') { 3195094Slling objnum = zpool_prop_default_numeric( 3205094Slling ZPOOL_PROP_BOOTFS); 3215094Slling break; 3225094Slling } 3235094Slling 3245094Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 3256689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os)) 3265094Slling break; 3277042Sgw25295 3287042Sgw25295 /* We don't support gzip bootable datasets */ 3297042Sgw25295 if ((error = dsl_prop_get_integer(strval, 3307042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3317042Sgw25295 &compress, NULL)) == 0 && 3327042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3337042Sgw25295 error = ENOTSUP; 3347042Sgw25295 } else { 3357042Sgw25295 objnum = dmu_objset_id(os); 3367042Sgw25295 } 3375094Slling dmu_objset_close(os); 3385094Slling } 3395094Slling break; 3405329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3415329Sgw25295 error = nvpair_value_uint64(elem, &intval); 3425329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3435329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 3445329Sgw25295 error = EINVAL; 3455329Sgw25295 3465329Sgw25295 /* 3475329Sgw25295 * This is a special case which only occurs when 3485329Sgw25295 * the pool has completely failed. This allows 3495329Sgw25295 * the user to change the in-core failmode property 3505329Sgw25295 * without syncing it out to disk (I/Os might 3515329Sgw25295 * currently be blocked). We do this by returning 3525329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 3535329Sgw25295 * into thinking we encountered a property validation 3545329Sgw25295 * error. 3555329Sgw25295 */ 3565329Sgw25295 if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) { 3575329Sgw25295 spa->spa_failmode = intval; 3585329Sgw25295 error = EIO; 3595329Sgw25295 } 3605329Sgw25295 break; 3615363Seschrock 3625363Seschrock case ZPOOL_PROP_CACHEFILE: 3635363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3645363Seschrock break; 3655363Seschrock 3665363Seschrock if (strval[0] == '\0') 3675363Seschrock break; 3685363Seschrock 3695363Seschrock if (strcmp(strval, "none") == 0) 3705363Seschrock break; 3715363Seschrock 3725363Seschrock if (strval[0] != '/') { 3735363Seschrock error = EINVAL; 3745363Seschrock break; 3755363Seschrock } 3765363Seschrock 3775363Seschrock slash = strrchr(strval, '/'); 3785363Seschrock ASSERT(slash != NULL); 3795363Seschrock 3805363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3815363Seschrock strcmp(slash, "/..") == 0) 3825363Seschrock error = EINVAL; 3835363Seschrock break; 3845094Slling } 3855094Slling 3865094Slling if (error) 3875094Slling break; 3885094Slling } 3895094Slling 3905094Slling if (!error && reset_bootfs) { 3915094Slling error = nvlist_remove(props, 3925094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 3935094Slling 3945094Slling if (!error) { 3955094Slling error = nvlist_add_uint64(props, 3965094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 3975094Slling } 3985094Slling } 3995094Slling 4005094Slling return (error); 4015094Slling } 4025094Slling 4035094Slling int 4045094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4055094Slling { 4065094Slling int error; 4075094Slling 4085094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 4095094Slling return (error); 4105094Slling 4115094Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 4125094Slling spa, nvp, 3)); 4135094Slling } 4145094Slling 4155094Slling /* 4165094Slling * If the bootfs property value is dsobj, clear it. 4175094Slling */ 4185094Slling void 4195094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 4205094Slling { 4215094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 4225094Slling VERIFY(zap_remove(spa->spa_meta_objset, 4235094Slling spa->spa_pool_props_object, 4245094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 4255094Slling spa->spa_bootfs = 0; 4265094Slling } 4275094Slling } 4285094Slling 429789Sahrens /* 430789Sahrens * ========================================================================== 431789Sahrens * SPA state manipulation (open/create/destroy/import/export) 432789Sahrens * ========================================================================== 433789Sahrens */ 434789Sahrens 4351544Seschrock static int 4361544Seschrock spa_error_entry_compare(const void *a, const void *b) 4371544Seschrock { 4381544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 4391544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 4401544Seschrock int ret; 4411544Seschrock 4421544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 4431544Seschrock sizeof (zbookmark_t)); 4441544Seschrock 4451544Seschrock if (ret < 0) 4461544Seschrock return (-1); 4471544Seschrock else if (ret > 0) 4481544Seschrock return (1); 4491544Seschrock else 4501544Seschrock return (0); 4511544Seschrock } 4521544Seschrock 4531544Seschrock /* 4541544Seschrock * Utility function which retrieves copies of the current logs and 4551544Seschrock * re-initializes them in the process. 4561544Seschrock */ 4571544Seschrock void 4581544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 4591544Seschrock { 4601544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 4611544Seschrock 4621544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 4631544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 4641544Seschrock 4651544Seschrock avl_create(&spa->spa_errlist_scrub, 4661544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4671544Seschrock offsetof(spa_error_entry_t, se_avl)); 4681544Seschrock avl_create(&spa->spa_errlist_last, 4691544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4701544Seschrock offsetof(spa_error_entry_t, se_avl)); 4711544Seschrock } 4721544Seschrock 473789Sahrens /* 474789Sahrens * Activate an uninitialized pool. 475789Sahrens */ 476789Sahrens static void 477789Sahrens spa_activate(spa_t *spa) 478789Sahrens { 479789Sahrens int t; 480789Sahrens 481789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 482789Sahrens 483789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 484789Sahrens 485789Sahrens spa->spa_normal_class = metaslab_class_create(); 4864527Sperrin spa->spa_log_class = metaslab_class_create(); 487789Sahrens 488789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 489789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 4902986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 491789Sahrens TASKQ_PREPOPULATE); 492789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 4932986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 494789Sahrens TASKQ_PREPOPULATE); 495789Sahrens } 496789Sahrens 497789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 498789Sahrens offsetof(vdev_t, vdev_dirty_node)); 4995329Sgw25295 list_create(&spa->spa_zio_list, sizeof (zio_t), 5005329Sgw25295 offsetof(zio_t, zio_link_node)); 501789Sahrens 502789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 503789Sahrens offsetof(struct vdev, vdev_txg_node)); 5041544Seschrock 5051544Seschrock avl_create(&spa->spa_errlist_scrub, 5061544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5071544Seschrock offsetof(spa_error_entry_t, se_avl)); 5081544Seschrock avl_create(&spa->spa_errlist_last, 5091544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5101544Seschrock offsetof(spa_error_entry_t, se_avl)); 511789Sahrens } 512789Sahrens 513789Sahrens /* 514789Sahrens * Opposite of spa_activate(). 515789Sahrens */ 516789Sahrens static void 517789Sahrens spa_deactivate(spa_t *spa) 518789Sahrens { 519789Sahrens int t; 520789Sahrens 521789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 522789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 523789Sahrens ASSERT(spa->spa_root_vdev == NULL); 524789Sahrens 525789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 526789Sahrens 527789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 528789Sahrens 529789Sahrens list_destroy(&spa->spa_dirty_list); 5305329Sgw25295 list_destroy(&spa->spa_zio_list); 531789Sahrens 532789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 533789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 534789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 535789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 536789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 537789Sahrens } 538789Sahrens 539789Sahrens metaslab_class_destroy(spa->spa_normal_class); 540789Sahrens spa->spa_normal_class = NULL; 541789Sahrens 5424527Sperrin metaslab_class_destroy(spa->spa_log_class); 5434527Sperrin spa->spa_log_class = NULL; 5444527Sperrin 5451544Seschrock /* 5461544Seschrock * If this was part of an import or the open otherwise failed, we may 5471544Seschrock * still have errors left in the queues. Empty them just in case. 5481544Seschrock */ 5491544Seschrock spa_errlog_drain(spa); 5501544Seschrock 5511544Seschrock avl_destroy(&spa->spa_errlist_scrub); 5521544Seschrock avl_destroy(&spa->spa_errlist_last); 5531544Seschrock 554789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 555789Sahrens } 556789Sahrens 557789Sahrens /* 558789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 559789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 560789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 561789Sahrens * All vdev validation is done by the vdev_alloc() routine. 562789Sahrens */ 5632082Seschrock static int 5642082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 5652082Seschrock uint_t id, int atype) 566789Sahrens { 567789Sahrens nvlist_t **child; 568789Sahrens uint_t c, children; 5692082Seschrock int error; 5702082Seschrock 5712082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 5722082Seschrock return (error); 5732082Seschrock 5742082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 5752082Seschrock return (0); 576789Sahrens 577789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 578789Sahrens &child, &children) != 0) { 5792082Seschrock vdev_free(*vdp); 5802082Seschrock *vdp = NULL; 5812082Seschrock return (EINVAL); 582789Sahrens } 583789Sahrens 584789Sahrens for (c = 0; c < children; c++) { 5852082Seschrock vdev_t *vd; 5862082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 5872082Seschrock atype)) != 0) { 5882082Seschrock vdev_free(*vdp); 5892082Seschrock *vdp = NULL; 5902082Seschrock return (error); 591789Sahrens } 592789Sahrens } 593789Sahrens 5942082Seschrock ASSERT(*vdp != NULL); 5952082Seschrock 5962082Seschrock return (0); 597789Sahrens } 598789Sahrens 599789Sahrens /* 600789Sahrens * Opposite of spa_load(). 601789Sahrens */ 602789Sahrens static void 603789Sahrens spa_unload(spa_t *spa) 604789Sahrens { 6052082Seschrock int i; 6062082Seschrock 607789Sahrens /* 6081544Seschrock * Stop async tasks. 6091544Seschrock */ 6101544Seschrock spa_async_suspend(spa); 6111544Seschrock 6121544Seschrock /* 613789Sahrens * Stop syncing. 614789Sahrens */ 615789Sahrens if (spa->spa_sync_on) { 616789Sahrens txg_sync_stop(spa->spa_dsl_pool); 617789Sahrens spa->spa_sync_on = B_FALSE; 618789Sahrens } 619789Sahrens 620789Sahrens /* 621789Sahrens * Wait for any outstanding prefetch I/O to complete. 622789Sahrens */ 6231544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 6241544Seschrock spa_config_exit(spa, FTAG); 625789Sahrens 626789Sahrens /* 6275450Sbrendan * Drop and purge level 2 cache 6285450Sbrendan */ 6295450Sbrendan spa_l2cache_drop(spa); 6305450Sbrendan 6315450Sbrendan /* 632789Sahrens * Close the dsl pool. 633789Sahrens */ 634789Sahrens if (spa->spa_dsl_pool) { 635789Sahrens dsl_pool_close(spa->spa_dsl_pool); 636789Sahrens spa->spa_dsl_pool = NULL; 637789Sahrens } 638789Sahrens 639789Sahrens /* 640789Sahrens * Close all vdevs. 641789Sahrens */ 6421585Sbonwick if (spa->spa_root_vdev) 643789Sahrens vdev_free(spa->spa_root_vdev); 6441585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 6451544Seschrock 6465450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 6475450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 6485450Sbrendan if (spa->spa_spares.sav_vdevs) { 6495450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 6505450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 6515450Sbrendan spa->spa_spares.sav_vdevs = NULL; 6525450Sbrendan } 6535450Sbrendan if (spa->spa_spares.sav_config) { 6545450Sbrendan nvlist_free(spa->spa_spares.sav_config); 6555450Sbrendan spa->spa_spares.sav_config = NULL; 6562082Seschrock } 6577377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 6585450Sbrendan 6595450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 6605450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 6615450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 6625450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 6635450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 6645450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 6655450Sbrendan } 6665450Sbrendan if (spa->spa_l2cache.sav_config) { 6675450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 6685450Sbrendan spa->spa_l2cache.sav_config = NULL; 6692082Seschrock } 6707377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 6712082Seschrock 6721544Seschrock spa->spa_async_suspended = 0; 673789Sahrens } 674789Sahrens 675789Sahrens /* 6762082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 6772082Seschrock * this pool. When this is called, we have some form of basic information in 6785450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 6795450Sbrendan * then re-generate a more complete list including status information. 6802082Seschrock */ 6812082Seschrock static void 6822082Seschrock spa_load_spares(spa_t *spa) 6832082Seschrock { 6842082Seschrock nvlist_t **spares; 6852082Seschrock uint_t nspares; 6862082Seschrock int i; 6873377Seschrock vdev_t *vd, *tvd; 6882082Seschrock 6892082Seschrock /* 6902082Seschrock * First, close and free any existing spare vdevs. 6912082Seschrock */ 6925450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 6935450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 6943377Seschrock 6953377Seschrock /* Undo the call to spa_activate() below */ 6966643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 6976643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 6983377Seschrock spa_spare_remove(tvd); 6993377Seschrock vdev_close(vd); 7003377Seschrock vdev_free(vd); 7012082Seschrock } 7023377Seschrock 7035450Sbrendan if (spa->spa_spares.sav_vdevs) 7045450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7055450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7065450Sbrendan 7075450Sbrendan if (spa->spa_spares.sav_config == NULL) 7082082Seschrock nspares = 0; 7092082Seschrock else 7105450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 7112082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 7122082Seschrock 7135450Sbrendan spa->spa_spares.sav_count = (int)nspares; 7145450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7152082Seschrock 7162082Seschrock if (nspares == 0) 7172082Seschrock return; 7182082Seschrock 7192082Seschrock /* 7202082Seschrock * Construct the array of vdevs, opening them to get status in the 7213377Seschrock * process. For each spare, there is potentially two different vdev_t 7223377Seschrock * structures associated with it: one in the list of spares (used only 7233377Seschrock * for basic validation purposes) and one in the active vdev 7243377Seschrock * configuration (if it's spared in). During this phase we open and 7253377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 7263377Seschrock * active configuration, then we also mark this vdev as an active spare. 7272082Seschrock */ 7285450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 7295450Sbrendan KM_SLEEP); 7305450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7312082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 7322082Seschrock VDEV_ALLOC_SPARE) == 0); 7332082Seschrock ASSERT(vd != NULL); 7342082Seschrock 7355450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 7362082Seschrock 7376643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7386643Seschrock B_FALSE)) != NULL) { 7393377Seschrock if (!tvd->vdev_isspare) 7403377Seschrock spa_spare_add(tvd); 7413377Seschrock 7423377Seschrock /* 7433377Seschrock * We only mark the spare active if we were successfully 7443377Seschrock * able to load the vdev. Otherwise, importing a pool 7453377Seschrock * with a bad active spare would result in strange 7463377Seschrock * behavior, because multiple pool would think the spare 7473377Seschrock * is actively in use. 7483377Seschrock * 7493377Seschrock * There is a vulnerability here to an equally bizarre 7503377Seschrock * circumstance, where a dead active spare is later 7513377Seschrock * brought back to life (onlined or otherwise). Given 7523377Seschrock * the rarity of this scenario, and the extra complexity 7533377Seschrock * it adds, we ignore the possibility. 7543377Seschrock */ 7553377Seschrock if (!vdev_is_dead(tvd)) 7563377Seschrock spa_spare_activate(tvd); 7573377Seschrock } 7583377Seschrock 7592082Seschrock if (vdev_open(vd) != 0) 7602082Seschrock continue; 7612082Seschrock 7622082Seschrock vd->vdev_top = vd; 7635450Sbrendan if (vdev_validate_aux(vd) == 0) 7645450Sbrendan spa_spare_add(vd); 7652082Seschrock } 7662082Seschrock 7672082Seschrock /* 7682082Seschrock * Recompute the stashed list of spares, with status information 7692082Seschrock * this time. 7702082Seschrock */ 7715450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 7722082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 7732082Seschrock 7745450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 7755450Sbrendan KM_SLEEP); 7765450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7775450Sbrendan spares[i] = vdev_config_generate(spa, 7785450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 7795450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 7805450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 7815450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7822082Seschrock nvlist_free(spares[i]); 7835450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 7845450Sbrendan } 7855450Sbrendan 7865450Sbrendan /* 7875450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 7885450Sbrendan * this pool. When this is called, we have some form of basic information in 7895450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 7905450Sbrendan * then re-generate a more complete list including status information. 7915450Sbrendan * Devices which are already active have their details maintained, and are 7925450Sbrendan * not re-opened. 7935450Sbrendan */ 7945450Sbrendan static void 7955450Sbrendan spa_load_l2cache(spa_t *spa) 7965450Sbrendan { 7975450Sbrendan nvlist_t **l2cache; 7985450Sbrendan uint_t nl2cache; 7995450Sbrendan int i, j, oldnvdevs; 8006643Seschrock uint64_t guid, size; 8015450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 8025450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 8035450Sbrendan 8045450Sbrendan if (sav->sav_config != NULL) { 8055450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 8065450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 8075450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 8085450Sbrendan } else { 8095450Sbrendan nl2cache = 0; 8105450Sbrendan } 8115450Sbrendan 8125450Sbrendan oldvdevs = sav->sav_vdevs; 8135450Sbrendan oldnvdevs = sav->sav_count; 8145450Sbrendan sav->sav_vdevs = NULL; 8155450Sbrendan sav->sav_count = 0; 8165450Sbrendan 8175450Sbrendan /* 8185450Sbrendan * Process new nvlist of vdevs. 8195450Sbrendan */ 8205450Sbrendan for (i = 0; i < nl2cache; i++) { 8215450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 8225450Sbrendan &guid) == 0); 8235450Sbrendan 8245450Sbrendan newvdevs[i] = NULL; 8255450Sbrendan for (j = 0; j < oldnvdevs; j++) { 8265450Sbrendan vd = oldvdevs[j]; 8275450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 8285450Sbrendan /* 8295450Sbrendan * Retain previous vdev for add/remove ops. 8305450Sbrendan */ 8315450Sbrendan newvdevs[i] = vd; 8325450Sbrendan oldvdevs[j] = NULL; 8335450Sbrendan break; 8345450Sbrendan } 8355450Sbrendan } 8365450Sbrendan 8375450Sbrendan if (newvdevs[i] == NULL) { 8385450Sbrendan /* 8395450Sbrendan * Create new vdev 8405450Sbrendan */ 8415450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 8425450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 8435450Sbrendan ASSERT(vd != NULL); 8445450Sbrendan newvdevs[i] = vd; 8455450Sbrendan 8465450Sbrendan /* 8475450Sbrendan * Commit this vdev as an l2cache device, 8485450Sbrendan * even if it fails to open. 8495450Sbrendan */ 8505450Sbrendan spa_l2cache_add(vd); 8515450Sbrendan 8526643Seschrock vd->vdev_top = vd; 8536643Seschrock vd->vdev_aux = sav; 8546643Seschrock 8556643Seschrock spa_l2cache_activate(vd); 8566643Seschrock 8575450Sbrendan if (vdev_open(vd) != 0) 8585450Sbrendan continue; 8595450Sbrendan 8605450Sbrendan (void) vdev_validate_aux(vd); 8615450Sbrendan 8625450Sbrendan if (!vdev_is_dead(vd)) { 8635450Sbrendan size = vdev_get_rsize(vd); 8646643Seschrock l2arc_add_vdev(spa, vd, 8656643Seschrock VDEV_LABEL_START_SIZE, 8666643Seschrock size - VDEV_LABEL_START_SIZE); 8675450Sbrendan } 8685450Sbrendan } 8695450Sbrendan } 8705450Sbrendan 8715450Sbrendan /* 8725450Sbrendan * Purge vdevs that were dropped 8735450Sbrendan */ 8745450Sbrendan for (i = 0; i < oldnvdevs; i++) { 8755450Sbrendan uint64_t pool; 8765450Sbrendan 8775450Sbrendan vd = oldvdevs[i]; 8785450Sbrendan if (vd != NULL) { 8795450Sbrendan if (spa_mode & FWRITE && 8805450Sbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && 8816643Seschrock pool != 0ULL && 8826643Seschrock l2arc_vdev_present(vd)) { 8835450Sbrendan l2arc_remove_vdev(vd); 8845450Sbrendan } 8855450Sbrendan (void) vdev_close(vd); 8865450Sbrendan spa_l2cache_remove(vd); 8875450Sbrendan } 8885450Sbrendan } 8895450Sbrendan 8905450Sbrendan if (oldvdevs) 8915450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 8925450Sbrendan 8935450Sbrendan if (sav->sav_config == NULL) 8945450Sbrendan goto out; 8955450Sbrendan 8965450Sbrendan sav->sav_vdevs = newvdevs; 8975450Sbrendan sav->sav_count = (int)nl2cache; 8985450Sbrendan 8995450Sbrendan /* 9005450Sbrendan * Recompute the stashed list of l2cache devices, with status 9015450Sbrendan * information this time. 9025450Sbrendan */ 9035450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 9045450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 9055450Sbrendan 9065450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 9075450Sbrendan for (i = 0; i < sav->sav_count; i++) 9085450Sbrendan l2cache[i] = vdev_config_generate(spa, 9095450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 9105450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 9115450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 9125450Sbrendan out: 9135450Sbrendan for (i = 0; i < sav->sav_count; i++) 9145450Sbrendan nvlist_free(l2cache[i]); 9155450Sbrendan if (sav->sav_count) 9165450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 9172082Seschrock } 9182082Seschrock 9192082Seschrock static int 9202082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 9212082Seschrock { 9222082Seschrock dmu_buf_t *db; 9232082Seschrock char *packed = NULL; 9242082Seschrock size_t nvsize = 0; 9252082Seschrock int error; 9262082Seschrock *value = NULL; 9272082Seschrock 9282082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 9292082Seschrock nvsize = *(uint64_t *)db->db_data; 9302082Seschrock dmu_buf_rele(db, FTAG); 9312082Seschrock 9322082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 9332082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 9342082Seschrock if (error == 0) 9352082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 9362082Seschrock kmem_free(packed, nvsize); 9372082Seschrock 9382082Seschrock return (error); 9392082Seschrock } 9402082Seschrock 9412082Seschrock /* 9424451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9434451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9444451Seschrock */ 9454451Seschrock static void 9464451Seschrock spa_check_removed(vdev_t *vd) 9474451Seschrock { 9484451Seschrock int c; 9494451Seschrock 9504451Seschrock for (c = 0; c < vd->vdev_children; c++) 9514451Seschrock spa_check_removed(vd->vdev_child[c]); 9524451Seschrock 9534451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9544451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9554451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9564451Seschrock } 9574451Seschrock } 9584451Seschrock 9594451Seschrock /* 9607294Sperrin * Check for missing log devices 9617294Sperrin */ 9627294Sperrin int 9637294Sperrin spa_check_logs(spa_t *spa) 9647294Sperrin { 9657294Sperrin switch (spa->spa_log_state) { 9667294Sperrin case SPA_LOG_MISSING: 9677294Sperrin /* need to recheck in case slog has been restored */ 9687294Sperrin case SPA_LOG_UNKNOWN: 9697294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 9707294Sperrin DS_FIND_CHILDREN)) { 9717294Sperrin spa->spa_log_state = SPA_LOG_MISSING; 9727294Sperrin return (1); 9737294Sperrin } 9747294Sperrin break; 9757294Sperrin 9767294Sperrin case SPA_LOG_CLEAR: 9777294Sperrin (void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL, 9787294Sperrin DS_FIND_CHILDREN); 9797294Sperrin break; 9807294Sperrin } 9817294Sperrin spa->spa_log_state = SPA_LOG_GOOD; 9827294Sperrin return (0); 9837294Sperrin } 9847294Sperrin 9857294Sperrin /* 986789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 9871544Seschrock * source of configuration information. 988789Sahrens */ 989789Sahrens static int 9901544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 991789Sahrens { 992789Sahrens int error = 0; 993789Sahrens nvlist_t *nvroot = NULL; 994789Sahrens vdev_t *rvd; 995789Sahrens uberblock_t *ub = &spa->spa_uberblock; 9961635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 997789Sahrens uint64_t pool_guid; 9982082Seschrock uint64_t version; 999789Sahrens zio_t *zio; 10004451Seschrock uint64_t autoreplace = 0; 10017294Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 1002789Sahrens 10031544Seschrock spa->spa_load_state = state; 10041635Sbonwick 1005789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 10061733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 10071544Seschrock error = EINVAL; 10081544Seschrock goto out; 10091544Seschrock } 1010789Sahrens 10112082Seschrock /* 10122082Seschrock * Versioning wasn't explicitly added to the label until later, so if 10132082Seschrock * it's not present treat it as the initial version. 10142082Seschrock */ 10152082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 10164577Sahrens version = SPA_VERSION_INITIAL; 10172082Seschrock 10181733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 10191733Sbonwick &spa->spa_config_txg); 10201733Sbonwick 10211635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 10221544Seschrock spa_guid_exists(pool_guid, 0)) { 10231544Seschrock error = EEXIST; 10241544Seschrock goto out; 10251544Seschrock } 1026789Sahrens 10272174Seschrock spa->spa_load_guid = pool_guid; 10282174Seschrock 1029789Sahrens /* 10302082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 10312082Seschrock * value that will be returned by spa_version() since parsing the 10322082Seschrock * configuration requires knowing the version number. 1033789Sahrens */ 10341544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 10352082Seschrock spa->spa_ubsync.ub_version = version; 10362082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 10371544Seschrock spa_config_exit(spa, FTAG); 1038789Sahrens 10392082Seschrock if (error != 0) 10401544Seschrock goto out; 1041789Sahrens 10421585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1043789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1044789Sahrens 1045789Sahrens /* 1046789Sahrens * Try to open all vdevs, loading each label in the process. 1047789Sahrens */ 10484070Smc142369 error = vdev_open(rvd); 10494070Smc142369 if (error != 0) 10501544Seschrock goto out; 1051789Sahrens 1052789Sahrens /* 10531986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 10541986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 10551986Seschrock * flag. 10561986Seschrock */ 10571986Seschrock spa_config_enter(spa, RW_READER, FTAG); 10581986Seschrock error = vdev_validate(rvd); 10591986Seschrock spa_config_exit(spa, FTAG); 10601986Seschrock 10614070Smc142369 if (error != 0) 10621986Seschrock goto out; 10631986Seschrock 10641986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 10651986Seschrock error = ENXIO; 10661986Seschrock goto out; 10671986Seschrock } 10681986Seschrock 10691986Seschrock /* 1070789Sahrens * Find the best uberblock. 1071789Sahrens */ 1072789Sahrens bzero(ub, sizeof (uberblock_t)); 1073789Sahrens 1074789Sahrens zio = zio_root(spa, NULL, NULL, 1075789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1076789Sahrens vdev_uberblock_load(zio, rvd, ub); 1077789Sahrens error = zio_wait(zio); 1078789Sahrens 1079789Sahrens /* 1080789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1081789Sahrens */ 1082789Sahrens if (ub->ub_txg == 0) { 10831760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10841760Seschrock VDEV_AUX_CORRUPT_DATA); 10851544Seschrock error = ENXIO; 10861544Seschrock goto out; 10871544Seschrock } 10881544Seschrock 10891544Seschrock /* 10901544Seschrock * If the pool is newer than the code, we can't open it. 10911544Seschrock */ 10924577Sahrens if (ub->ub_version > SPA_VERSION) { 10931760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10941760Seschrock VDEV_AUX_VERSION_NEWER); 10951544Seschrock error = ENOTSUP; 10961544Seschrock goto out; 1097789Sahrens } 1098789Sahrens 1099789Sahrens /* 1100789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1101789Sahrens * incomplete configuration. 1102789Sahrens */ 11031732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 11041544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11051544Seschrock VDEV_AUX_BAD_GUID_SUM); 11061544Seschrock error = ENXIO; 11071544Seschrock goto out; 1108789Sahrens } 1109789Sahrens 1110789Sahrens /* 1111789Sahrens * Initialize internal SPA structures. 1112789Sahrens */ 1113789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1114789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1115789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 11161544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 11171544Seschrock if (error) { 11181544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11191544Seschrock VDEV_AUX_CORRUPT_DATA); 11201544Seschrock goto out; 11211544Seschrock } 1122789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1123789Sahrens 11241544Seschrock if (zap_lookup(spa->spa_meta_objset, 1125789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 11261544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 11271544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11281544Seschrock VDEV_AUX_CORRUPT_DATA); 11291544Seschrock error = EIO; 11301544Seschrock goto out; 11311544Seschrock } 1132789Sahrens 1133789Sahrens if (!mosconfig) { 11342082Seschrock nvlist_t *newconfig; 11353975Sek110237 uint64_t hostid; 11362082Seschrock 11372082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 11381544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11391544Seschrock VDEV_AUX_CORRUPT_DATA); 11401544Seschrock error = EIO; 11411544Seschrock goto out; 11421544Seschrock } 1143789Sahrens 1144*7706SLin.Ling@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig, 1145*7706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 11463975Sek110237 char *hostname; 11473975Sek110237 unsigned long myhostid = 0; 11483975Sek110237 11493975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 11503975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 11513975Sek110237 11523975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 11534178Slling if (hostid != 0 && myhostid != 0 && 11544178Slling (unsigned long)hostid != myhostid) { 11553975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 11563975Sek110237 "loaded as it was last accessed by " 1157*7706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 11583975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 11593975Sek110237 spa->spa_name, hostname, 11603975Sek110237 (unsigned long)hostid); 11613975Sek110237 error = EBADF; 11623975Sek110237 goto out; 11633975Sek110237 } 11643975Sek110237 } 11653975Sek110237 1166789Sahrens spa_config_set(spa, newconfig); 1167789Sahrens spa_unload(spa); 1168789Sahrens spa_deactivate(spa); 1169789Sahrens spa_activate(spa); 1170789Sahrens 11711544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 11721544Seschrock } 11731544Seschrock 11741544Seschrock if (zap_lookup(spa->spa_meta_objset, 11751544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 11761544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 11771544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11781544Seschrock VDEV_AUX_CORRUPT_DATA); 11791544Seschrock error = EIO; 11801544Seschrock goto out; 1181789Sahrens } 1182789Sahrens 11831544Seschrock /* 11842082Seschrock * Load the bit that tells us to use the new accounting function 11852082Seschrock * (raid-z deflation). If we have an older pool, this will not 11862082Seschrock * be present. 11872082Seschrock */ 11882082Seschrock error = zap_lookup(spa->spa_meta_objset, 11892082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 11902082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 11912082Seschrock if (error != 0 && error != ENOENT) { 11922082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11932082Seschrock VDEV_AUX_CORRUPT_DATA); 11942082Seschrock error = EIO; 11952082Seschrock goto out; 11962082Seschrock } 11972082Seschrock 11982082Seschrock /* 11991544Seschrock * Load the persistent error log. If we have an older pool, this will 12001544Seschrock * not be present. 12011544Seschrock */ 12021544Seschrock error = zap_lookup(spa->spa_meta_objset, 12031544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 12041544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 12051807Sbonwick if (error != 0 && error != ENOENT) { 12061544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12071544Seschrock VDEV_AUX_CORRUPT_DATA); 12081544Seschrock error = EIO; 12091544Seschrock goto out; 12101544Seschrock } 12111544Seschrock 12121544Seschrock error = zap_lookup(spa->spa_meta_objset, 12131544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 12141544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 12151544Seschrock if (error != 0 && error != ENOENT) { 12161544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12171544Seschrock VDEV_AUX_CORRUPT_DATA); 12181544Seschrock error = EIO; 12191544Seschrock goto out; 12201544Seschrock } 1221789Sahrens 1222789Sahrens /* 12232926Sek110237 * Load the history object. If we have an older pool, this 12242926Sek110237 * will not be present. 12252926Sek110237 */ 12262926Sek110237 error = zap_lookup(spa->spa_meta_objset, 12272926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 12282926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 12292926Sek110237 if (error != 0 && error != ENOENT) { 12302926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12312926Sek110237 VDEV_AUX_CORRUPT_DATA); 12322926Sek110237 error = EIO; 12332926Sek110237 goto out; 12342926Sek110237 } 12352926Sek110237 12362926Sek110237 /* 12372082Seschrock * Load any hot spares for this pool. 12382082Seschrock */ 12392082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12405450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 12412082Seschrock if (error != 0 && error != ENOENT) { 12422082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12432082Seschrock VDEV_AUX_CORRUPT_DATA); 12442082Seschrock error = EIO; 12452082Seschrock goto out; 12462082Seschrock } 12472082Seschrock if (error == 0) { 12484577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 12495450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 12505450Sbrendan &spa->spa_spares.sav_config) != 0) { 12512082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12522082Seschrock VDEV_AUX_CORRUPT_DATA); 12532082Seschrock error = EIO; 12542082Seschrock goto out; 12552082Seschrock } 12562082Seschrock 12572082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12582082Seschrock spa_load_spares(spa); 12592082Seschrock spa_config_exit(spa, FTAG); 12602082Seschrock } 12612082Seschrock 12625450Sbrendan /* 12635450Sbrendan * Load any level 2 ARC devices for this pool. 12645450Sbrendan */ 12655450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12665450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 12675450Sbrendan &spa->spa_l2cache.sav_object); 12685450Sbrendan if (error != 0 && error != ENOENT) { 12695450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12705450Sbrendan VDEV_AUX_CORRUPT_DATA); 12715450Sbrendan error = EIO; 12725450Sbrendan goto out; 12735450Sbrendan } 12745450Sbrendan if (error == 0) { 12755450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 12765450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 12775450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 12785450Sbrendan vdev_set_state(rvd, B_TRUE, 12795450Sbrendan VDEV_STATE_CANT_OPEN, 12805450Sbrendan VDEV_AUX_CORRUPT_DATA); 12815450Sbrendan error = EIO; 12825450Sbrendan goto out; 12835450Sbrendan } 12845450Sbrendan 12855450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 12865450Sbrendan spa_load_l2cache(spa); 12875450Sbrendan spa_config_exit(spa, FTAG); 12885450Sbrendan } 12895450Sbrendan 12907294Sperrin if (spa_check_logs(spa)) { 12917294Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12927294Sperrin VDEV_AUX_BAD_LOG); 12937294Sperrin error = ENXIO; 12947294Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 12957294Sperrin goto out; 12967294Sperrin } 12977294Sperrin 12987294Sperrin 12995094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 13004543Smarks 13013912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13023912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 13033912Slling 13043912Slling if (error && error != ENOENT) { 13053912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13063912Slling VDEV_AUX_CORRUPT_DATA); 13073912Slling error = EIO; 13083912Slling goto out; 13093912Slling } 13103912Slling 13113912Slling if (error == 0) { 13123912Slling (void) zap_lookup(spa->spa_meta_objset, 13133912Slling spa->spa_pool_props_object, 13144451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 13153912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 13164451Seschrock (void) zap_lookup(spa->spa_meta_objset, 13174451Seschrock spa->spa_pool_props_object, 13184451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 13194451Seschrock sizeof (uint64_t), 1, &autoreplace); 13204543Smarks (void) zap_lookup(spa->spa_meta_objset, 13214543Smarks spa->spa_pool_props_object, 13224543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 13234543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 13245329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 13255329Sgw25295 spa->spa_pool_props_object, 13265329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 13275329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 13283912Slling } 13293912Slling 13302082Seschrock /* 13314451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 13324451Seschrock * the ZFS DE that it should not issue any faults for unopenable 13334451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 13344451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 13354451Seschrock * over. 13364451Seschrock */ 13375756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 13384451Seschrock spa_check_removed(spa->spa_root_vdev); 13394451Seschrock 13404451Seschrock /* 13411986Seschrock * Load the vdev state for all toplevel vdevs. 1342789Sahrens */ 13431986Seschrock vdev_load(rvd); 1344789Sahrens 1345789Sahrens /* 1346789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1347789Sahrens */ 13481544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1349789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 13501544Seschrock spa_config_exit(spa, FTAG); 1351789Sahrens 1352789Sahrens /* 1353789Sahrens * Check the state of the root vdev. If it can't be opened, it 1354789Sahrens * indicates one or more toplevel vdevs are faulted. 1355789Sahrens */ 13561544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13571544Seschrock error = ENXIO; 13581544Seschrock goto out; 13591544Seschrock } 1360789Sahrens 13611544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 13621635Sbonwick dmu_tx_t *tx; 13631635Sbonwick int need_update = B_FALSE; 13641585Sbonwick int c; 13651601Sbonwick 13661635Sbonwick /* 13671635Sbonwick * Claim log blocks that haven't been committed yet. 13681635Sbonwick * This must all happen in a single txg. 13691635Sbonwick */ 13701601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1371789Sahrens spa_first_txg(spa)); 13722417Sahrens (void) dmu_objset_find(spa->spa_name, 13732417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1374789Sahrens dmu_tx_commit(tx); 1375789Sahrens 1376789Sahrens spa->spa_sync_on = B_TRUE; 1377789Sahrens txg_sync_start(spa->spa_dsl_pool); 1378789Sahrens 1379789Sahrens /* 1380789Sahrens * Wait for all claims to sync. 1381789Sahrens */ 1382789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 13831585Sbonwick 13841585Sbonwick /* 13851635Sbonwick * If the config cache is stale, or we have uninitialized 13861635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 13871585Sbonwick */ 13881635Sbonwick if (config_cache_txg != spa->spa_config_txg || 13891635Sbonwick state == SPA_LOAD_IMPORT) 13901635Sbonwick need_update = B_TRUE; 13911635Sbonwick 13921635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 13931635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 13941635Sbonwick need_update = B_TRUE; 13951585Sbonwick 13961585Sbonwick /* 13971635Sbonwick * Update the config cache asychronously in case we're the 13981635Sbonwick * root pool, in which case the config cache isn't writable yet. 13991585Sbonwick */ 14001635Sbonwick if (need_update) 14011635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1402789Sahrens } 1403789Sahrens 14041544Seschrock error = 0; 14051544Seschrock out: 14067046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 14072082Seschrock if (error && error != EBADF) 14087294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 14091544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 14101544Seschrock spa->spa_ena = 0; 14111544Seschrock 14121544Seschrock return (error); 1413789Sahrens } 1414789Sahrens 1415789Sahrens /* 1416789Sahrens * Pool Open/Import 1417789Sahrens * 1418789Sahrens * The import case is identical to an open except that the configuration is sent 1419789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1420789Sahrens * case of an open, the pool configuration will exist in the 14214451Seschrock * POOL_STATE_UNINITIALIZED state. 1422789Sahrens * 1423789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1424789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1425789Sahrens * ambiguous state. 1426789Sahrens */ 1427789Sahrens static int 1428789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1429789Sahrens { 1430789Sahrens spa_t *spa; 1431789Sahrens int error; 1432789Sahrens int locked = B_FALSE; 1433789Sahrens 1434789Sahrens *spapp = NULL; 1435789Sahrens 1436789Sahrens /* 1437789Sahrens * As disgusting as this is, we need to support recursive calls to this 1438789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1439789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1440789Sahrens * avoid dsl_dir_open() calling this in the first place. 1441789Sahrens */ 1442789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1443789Sahrens mutex_enter(&spa_namespace_lock); 1444789Sahrens locked = B_TRUE; 1445789Sahrens } 1446789Sahrens 1447789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1448789Sahrens if (locked) 1449789Sahrens mutex_exit(&spa_namespace_lock); 1450789Sahrens return (ENOENT); 1451789Sahrens } 1452789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1453789Sahrens 1454789Sahrens spa_activate(spa); 1455789Sahrens 14561635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1457789Sahrens 1458789Sahrens if (error == EBADF) { 1459789Sahrens /* 14601986Seschrock * If vdev_validate() returns failure (indicated by 14611986Seschrock * EBADF), it indicates that one of the vdevs indicates 14621986Seschrock * that the pool has been exported or destroyed. If 14631986Seschrock * this is the case, the config cache is out of sync and 14641986Seschrock * we should remove the pool from the namespace. 1465789Sahrens */ 1466789Sahrens spa_unload(spa); 1467789Sahrens spa_deactivate(spa); 14686643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1469789Sahrens spa_remove(spa); 1470789Sahrens if (locked) 1471789Sahrens mutex_exit(&spa_namespace_lock); 1472789Sahrens return (ENOENT); 14731544Seschrock } 14741544Seschrock 14751544Seschrock if (error) { 1476789Sahrens /* 1477789Sahrens * We can't open the pool, but we still have useful 1478789Sahrens * information: the state of each vdev after the 1479789Sahrens * attempted vdev_open(). Return this to the user. 1480789Sahrens */ 14811635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 14821635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1483789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1484789Sahrens B_TRUE); 14851635Sbonwick spa_config_exit(spa, FTAG); 14861635Sbonwick } 1487789Sahrens spa_unload(spa); 1488789Sahrens spa_deactivate(spa); 14891544Seschrock spa->spa_last_open_failed = B_TRUE; 1490789Sahrens if (locked) 1491789Sahrens mutex_exit(&spa_namespace_lock); 1492789Sahrens *spapp = NULL; 1493789Sahrens return (error); 14941544Seschrock } else { 14951544Seschrock spa->spa_last_open_failed = B_FALSE; 1496789Sahrens } 1497789Sahrens } 1498789Sahrens 1499789Sahrens spa_open_ref(spa, tag); 15004451Seschrock 1501789Sahrens if (locked) 1502789Sahrens mutex_exit(&spa_namespace_lock); 1503789Sahrens 1504789Sahrens *spapp = spa; 1505789Sahrens 1506789Sahrens if (config != NULL) { 15071544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1508789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 15091544Seschrock spa_config_exit(spa, FTAG); 1510789Sahrens } 1511789Sahrens 1512789Sahrens return (0); 1513789Sahrens } 1514789Sahrens 1515789Sahrens int 1516789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1517789Sahrens { 1518789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1519789Sahrens } 1520789Sahrens 15211544Seschrock /* 15221544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 15231544Seschrock * preventing it from being exported or destroyed. 15241544Seschrock */ 15251544Seschrock spa_t * 15261544Seschrock spa_inject_addref(char *name) 15271544Seschrock { 15281544Seschrock spa_t *spa; 15291544Seschrock 15301544Seschrock mutex_enter(&spa_namespace_lock); 15311544Seschrock if ((spa = spa_lookup(name)) == NULL) { 15321544Seschrock mutex_exit(&spa_namespace_lock); 15331544Seschrock return (NULL); 15341544Seschrock } 15351544Seschrock spa->spa_inject_ref++; 15361544Seschrock mutex_exit(&spa_namespace_lock); 15371544Seschrock 15381544Seschrock return (spa); 15391544Seschrock } 15401544Seschrock 15411544Seschrock void 15421544Seschrock spa_inject_delref(spa_t *spa) 15431544Seschrock { 15441544Seschrock mutex_enter(&spa_namespace_lock); 15451544Seschrock spa->spa_inject_ref--; 15461544Seschrock mutex_exit(&spa_namespace_lock); 15471544Seschrock } 15481544Seschrock 15495450Sbrendan /* 15505450Sbrendan * Add spares device information to the nvlist. 15515450Sbrendan */ 15522082Seschrock static void 15532082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 15542082Seschrock { 15552082Seschrock nvlist_t **spares; 15562082Seschrock uint_t i, nspares; 15572082Seschrock nvlist_t *nvroot; 15582082Seschrock uint64_t guid; 15592082Seschrock vdev_stat_t *vs; 15602082Seschrock uint_t vsc; 15613377Seschrock uint64_t pool; 15622082Seschrock 15635450Sbrendan if (spa->spa_spares.sav_count == 0) 15642082Seschrock return; 15652082Seschrock 15662082Seschrock VERIFY(nvlist_lookup_nvlist(config, 15672082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15685450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 15692082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15702082Seschrock if (nspares != 0) { 15712082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 15722082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 15732082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 15742082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15752082Seschrock 15762082Seschrock /* 15772082Seschrock * Go through and find any spares which have since been 15782082Seschrock * repurposed as an active spare. If this is the case, update 15792082Seschrock * their status appropriately. 15802082Seschrock */ 15812082Seschrock for (i = 0; i < nspares; i++) { 15822082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 15832082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 15847214Slling if (spa_spare_exists(guid, &pool, NULL) && 15857214Slling pool != 0ULL) { 15862082Seschrock VERIFY(nvlist_lookup_uint64_array( 15872082Seschrock spares[i], ZPOOL_CONFIG_STATS, 15882082Seschrock (uint64_t **)&vs, &vsc) == 0); 15892082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 15902082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 15912082Seschrock } 15922082Seschrock } 15932082Seschrock } 15942082Seschrock } 15952082Seschrock 15965450Sbrendan /* 15975450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 15985450Sbrendan */ 15995450Sbrendan static void 16005450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 16015450Sbrendan { 16025450Sbrendan nvlist_t **l2cache; 16035450Sbrendan uint_t i, j, nl2cache; 16045450Sbrendan nvlist_t *nvroot; 16055450Sbrendan uint64_t guid; 16065450Sbrendan vdev_t *vd; 16075450Sbrendan vdev_stat_t *vs; 16085450Sbrendan uint_t vsc; 16095450Sbrendan 16105450Sbrendan if (spa->spa_l2cache.sav_count == 0) 16115450Sbrendan return; 16125450Sbrendan 16135450Sbrendan spa_config_enter(spa, RW_READER, FTAG); 16145450Sbrendan 16155450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 16165450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 16175450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 16185450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 16195450Sbrendan if (nl2cache != 0) { 16205450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 16215450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 16225450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 16235450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 16245450Sbrendan 16255450Sbrendan /* 16265450Sbrendan * Update level 2 cache device stats. 16275450Sbrendan */ 16285450Sbrendan 16295450Sbrendan for (i = 0; i < nl2cache; i++) { 16305450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 16315450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 16325450Sbrendan 16335450Sbrendan vd = NULL; 16345450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 16355450Sbrendan if (guid == 16365450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 16375450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 16385450Sbrendan break; 16395450Sbrendan } 16405450Sbrendan } 16415450Sbrendan ASSERT(vd != NULL); 16425450Sbrendan 16435450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 16445450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 16455450Sbrendan vdev_get_stats(vd, vs); 16465450Sbrendan } 16475450Sbrendan } 16485450Sbrendan 16495450Sbrendan spa_config_exit(spa, FTAG); 16505450Sbrendan } 16515450Sbrendan 1652789Sahrens int 16531544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1654789Sahrens { 1655789Sahrens int error; 1656789Sahrens spa_t *spa; 1657789Sahrens 1658789Sahrens *config = NULL; 1659789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1660789Sahrens 16612082Seschrock if (spa && *config != NULL) { 16621544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 16631544Seschrock spa_get_errlog_size(spa)) == 0); 16641544Seschrock 16652082Seschrock spa_add_spares(spa, *config); 16665450Sbrendan spa_add_l2cache(spa, *config); 16672082Seschrock } 16682082Seschrock 16691544Seschrock /* 16701544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 16711544Seschrock * and call spa_lookup() directly. 16721544Seschrock */ 16731544Seschrock if (altroot) { 16741544Seschrock if (spa == NULL) { 16751544Seschrock mutex_enter(&spa_namespace_lock); 16761544Seschrock spa = spa_lookup(name); 16771544Seschrock if (spa) 16781544Seschrock spa_altroot(spa, altroot, buflen); 16791544Seschrock else 16801544Seschrock altroot[0] = '\0'; 16811544Seschrock spa = NULL; 16821544Seschrock mutex_exit(&spa_namespace_lock); 16831544Seschrock } else { 16841544Seschrock spa_altroot(spa, altroot, buflen); 16851544Seschrock } 16861544Seschrock } 16871544Seschrock 1688789Sahrens if (spa != NULL) 1689789Sahrens spa_close(spa, FTAG); 1690789Sahrens 1691789Sahrens return (error); 1692789Sahrens } 1693789Sahrens 1694789Sahrens /* 16955450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 16965450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 16975450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 16985450Sbrendan * specified, as long as they are well-formed. 16992082Seschrock */ 17002082Seschrock static int 17015450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 17025450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 17035450Sbrendan vdev_labeltype_t label) 17042082Seschrock { 17055450Sbrendan nvlist_t **dev; 17065450Sbrendan uint_t i, ndev; 17072082Seschrock vdev_t *vd; 17082082Seschrock int error; 17092082Seschrock 17102082Seschrock /* 17115450Sbrendan * It's acceptable to have no devs specified. 17122082Seschrock */ 17135450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 17142082Seschrock return (0); 17152082Seschrock 17165450Sbrendan if (ndev == 0) 17172082Seschrock return (EINVAL); 17182082Seschrock 17192082Seschrock /* 17205450Sbrendan * Make sure the pool is formatted with a version that supports this 17215450Sbrendan * device type. 17222082Seschrock */ 17235450Sbrendan if (spa_version(spa) < version) 17242082Seschrock return (ENOTSUP); 17252082Seschrock 17263377Seschrock /* 17275450Sbrendan * Set the pending device list so we correctly handle device in-use 17283377Seschrock * checking. 17293377Seschrock */ 17305450Sbrendan sav->sav_pending = dev; 17315450Sbrendan sav->sav_npending = ndev; 17325450Sbrendan 17335450Sbrendan for (i = 0; i < ndev; i++) { 17345450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 17352082Seschrock mode)) != 0) 17363377Seschrock goto out; 17372082Seschrock 17382082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 17392082Seschrock vdev_free(vd); 17403377Seschrock error = EINVAL; 17413377Seschrock goto out; 17422082Seschrock } 17432082Seschrock 17445450Sbrendan /* 17455450Sbrendan * The L2ARC currently only supports disk devices. 17465450Sbrendan */ 17475450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 17485450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 17495450Sbrendan error = ENOTBLK; 17505450Sbrendan goto out; 17515450Sbrendan } 17525450Sbrendan 17532082Seschrock vd->vdev_top = vd; 17543377Seschrock 17553377Seschrock if ((error = vdev_open(vd)) == 0 && 17565450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 17575450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 17583377Seschrock vd->vdev_guid) == 0); 17592082Seschrock } 17602082Seschrock 17612082Seschrock vdev_free(vd); 17623377Seschrock 17635450Sbrendan if (error && 17645450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 17653377Seschrock goto out; 17663377Seschrock else 17673377Seschrock error = 0; 17682082Seschrock } 17692082Seschrock 17703377Seschrock out: 17715450Sbrendan sav->sav_pending = NULL; 17725450Sbrendan sav->sav_npending = 0; 17733377Seschrock return (error); 17742082Seschrock } 17752082Seschrock 17765450Sbrendan static int 17775450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 17785450Sbrendan { 17795450Sbrendan int error; 17805450Sbrendan 17815450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17825450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 17835450Sbrendan VDEV_LABEL_SPARE)) != 0) { 17845450Sbrendan return (error); 17855450Sbrendan } 17865450Sbrendan 17875450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17885450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 17895450Sbrendan VDEV_LABEL_L2CACHE)); 17905450Sbrendan } 17915450Sbrendan 17925450Sbrendan static void 17935450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 17945450Sbrendan const char *config) 17955450Sbrendan { 17965450Sbrendan int i; 17975450Sbrendan 17985450Sbrendan if (sav->sav_config != NULL) { 17995450Sbrendan nvlist_t **olddevs; 18005450Sbrendan uint_t oldndevs; 18015450Sbrendan nvlist_t **newdevs; 18025450Sbrendan 18035450Sbrendan /* 18045450Sbrendan * Generate new dev list by concatentating with the 18055450Sbrendan * current dev list. 18065450Sbrendan */ 18075450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 18085450Sbrendan &olddevs, &oldndevs) == 0); 18095450Sbrendan 18105450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 18115450Sbrendan (ndevs + oldndevs), KM_SLEEP); 18125450Sbrendan for (i = 0; i < oldndevs; i++) 18135450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 18145450Sbrendan KM_SLEEP) == 0); 18155450Sbrendan for (i = 0; i < ndevs; i++) 18165450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 18175450Sbrendan KM_SLEEP) == 0); 18185450Sbrendan 18195450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 18205450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 18215450Sbrendan 18225450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 18235450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 18245450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 18255450Sbrendan nvlist_free(newdevs[i]); 18265450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 18275450Sbrendan } else { 18285450Sbrendan /* 18295450Sbrendan * Generate a new dev list. 18305450Sbrendan */ 18315450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 18325450Sbrendan KM_SLEEP) == 0); 18335450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 18345450Sbrendan devs, ndevs) == 0); 18355450Sbrendan } 18365450Sbrendan } 18375450Sbrendan 18385450Sbrendan /* 18395450Sbrendan * Stop and drop level 2 ARC devices 18405450Sbrendan */ 18415450Sbrendan void 18425450Sbrendan spa_l2cache_drop(spa_t *spa) 18435450Sbrendan { 18445450Sbrendan vdev_t *vd; 18455450Sbrendan int i; 18465450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 18475450Sbrendan 18485450Sbrendan for (i = 0; i < sav->sav_count; i++) { 18495450Sbrendan uint64_t pool; 18505450Sbrendan 18515450Sbrendan vd = sav->sav_vdevs[i]; 18525450Sbrendan ASSERT(vd != NULL); 18535450Sbrendan 18545450Sbrendan if (spa_mode & FWRITE && 18556643Seschrock spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL && 18566643Seschrock l2arc_vdev_present(vd)) { 18575450Sbrendan l2arc_remove_vdev(vd); 18585450Sbrendan } 18595450Sbrendan if (vd->vdev_isl2cache) 18605450Sbrendan spa_l2cache_remove(vd); 18615450Sbrendan vdev_clear_stats(vd); 18625450Sbrendan (void) vdev_close(vd); 18635450Sbrendan } 18645450Sbrendan } 18655450Sbrendan 18662082Seschrock /* 1867789Sahrens * Pool Creation 1868789Sahrens */ 1869789Sahrens int 18705094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 18717184Stimh const char *history_str, nvlist_t *zplprops) 1872789Sahrens { 1873789Sahrens spa_t *spa; 18745094Slling char *altroot = NULL; 18751635Sbonwick vdev_t *rvd; 1876789Sahrens dsl_pool_t *dp; 1877789Sahrens dmu_tx_t *tx; 18782082Seschrock int c, error = 0; 1879789Sahrens uint64_t txg = TXG_INITIAL; 18805450Sbrendan nvlist_t **spares, **l2cache; 18815450Sbrendan uint_t nspares, nl2cache; 18825094Slling uint64_t version; 1883789Sahrens 1884789Sahrens /* 1885789Sahrens * If this pool already exists, return failure. 1886789Sahrens */ 1887789Sahrens mutex_enter(&spa_namespace_lock); 1888789Sahrens if (spa_lookup(pool) != NULL) { 1889789Sahrens mutex_exit(&spa_namespace_lock); 1890789Sahrens return (EEXIST); 1891789Sahrens } 1892789Sahrens 1893789Sahrens /* 1894789Sahrens * Allocate a new spa_t structure. 1895789Sahrens */ 18965094Slling (void) nvlist_lookup_string(props, 18975094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 18981635Sbonwick spa = spa_add(pool, altroot); 1899789Sahrens spa_activate(spa); 1900789Sahrens 1901789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 19025094Slling 19035094Slling if (props && (error = spa_prop_validate(spa, props))) { 19045094Slling spa_unload(spa); 19055094Slling spa_deactivate(spa); 19065094Slling spa_remove(spa); 19076643Seschrock mutex_exit(&spa_namespace_lock); 19085094Slling return (error); 19095094Slling } 19105094Slling 19115094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 19125094Slling &version) != 0) 19135094Slling version = SPA_VERSION; 19145094Slling ASSERT(version <= SPA_VERSION); 19155094Slling spa->spa_uberblock.ub_version = version; 1916789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1917789Sahrens 19181635Sbonwick /* 19191635Sbonwick * Create the root vdev. 19201635Sbonwick */ 19211635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 19221635Sbonwick 19232082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 19242082Seschrock 19252082Seschrock ASSERT(error != 0 || rvd != NULL); 19262082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 19272082Seschrock 19285913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 19291635Sbonwick error = EINVAL; 19302082Seschrock 19312082Seschrock if (error == 0 && 19322082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 19335450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 19342082Seschrock VDEV_ALLOC_ADD)) == 0) { 19352082Seschrock for (c = 0; c < rvd->vdev_children; c++) 19362082Seschrock vdev_init(rvd->vdev_child[c], txg); 19372082Seschrock vdev_config_dirty(rvd); 19381635Sbonwick } 19391635Sbonwick 19401635Sbonwick spa_config_exit(spa, FTAG); 1941789Sahrens 19422082Seschrock if (error != 0) { 1943789Sahrens spa_unload(spa); 1944789Sahrens spa_deactivate(spa); 1945789Sahrens spa_remove(spa); 1946789Sahrens mutex_exit(&spa_namespace_lock); 1947789Sahrens return (error); 1948789Sahrens } 1949789Sahrens 19502082Seschrock /* 19512082Seschrock * Get the list of spares, if specified. 19522082Seschrock */ 19532082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 19542082Seschrock &spares, &nspares) == 0) { 19555450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 19562082Seschrock KM_SLEEP) == 0); 19575450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 19582082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 19592082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 19602082Seschrock spa_load_spares(spa); 19612082Seschrock spa_config_exit(spa, FTAG); 19625450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 19635450Sbrendan } 19645450Sbrendan 19655450Sbrendan /* 19665450Sbrendan * Get the list of level 2 cache devices, if specified. 19675450Sbrendan */ 19685450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 19695450Sbrendan &l2cache, &nl2cache) == 0) { 19705450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 19715450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 19725450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 19735450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 19745450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 19755450Sbrendan spa_load_l2cache(spa); 19765450Sbrendan spa_config_exit(spa, FTAG); 19775450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 19782082Seschrock } 19792082Seschrock 19807184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 1981789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1982789Sahrens 1983789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1984789Sahrens 1985789Sahrens /* 1986789Sahrens * Create the pool config object. 1987789Sahrens */ 1988789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 19897497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 1990789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1991789Sahrens 19921544Seschrock if (zap_add(spa->spa_meta_objset, 1993789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 19941544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 19951544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 19961544Seschrock } 1997789Sahrens 19985094Slling /* Newly created pools with the right version are always deflated. */ 19995094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 20005094Slling spa->spa_deflate = TRUE; 20015094Slling if (zap_add(spa->spa_meta_objset, 20025094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 20035094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 20045094Slling cmn_err(CE_PANIC, "failed to add deflate"); 20055094Slling } 20062082Seschrock } 20072082Seschrock 2008789Sahrens /* 2009789Sahrens * Create the deferred-free bplist object. Turn off compression 2010789Sahrens * because sync-to-convergence takes longer if the blocksize 2011789Sahrens * keeps changing. 2012789Sahrens */ 2013789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2014789Sahrens 1 << 14, tx); 2015789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2016789Sahrens ZIO_COMPRESS_OFF, tx); 2017789Sahrens 20181544Seschrock if (zap_add(spa->spa_meta_objset, 2019789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 20201544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 20211544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 20221544Seschrock } 2023789Sahrens 20242926Sek110237 /* 20252926Sek110237 * Create the pool's history object. 20262926Sek110237 */ 20275094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 20285094Slling spa_history_create_obj(spa, tx); 20295094Slling 20305094Slling /* 20315094Slling * Set pool properties. 20325094Slling */ 20335094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 20345094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20355329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 20365094Slling if (props) 20375094Slling spa_sync_props(spa, props, CRED(), tx); 20382926Sek110237 2039789Sahrens dmu_tx_commit(tx); 2040789Sahrens 2041789Sahrens spa->spa_sync_on = B_TRUE; 2042789Sahrens txg_sync_start(spa->spa_dsl_pool); 2043789Sahrens 2044789Sahrens /* 2045789Sahrens * We explicitly wait for the first transaction to complete so that our 2046789Sahrens * bean counters are appropriately updated. 2047789Sahrens */ 2048789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2049789Sahrens 20506643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2051789Sahrens 20525094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 20534715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 20544715Sek110237 2055789Sahrens mutex_exit(&spa_namespace_lock); 2056789Sahrens 20577046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 20587046Sahrens 2059789Sahrens return (0); 2060789Sahrens } 2061789Sahrens 2062789Sahrens /* 2063789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2064789Sahrens * then call spa_load() to do the dirty work. 2065789Sahrens */ 20666423Sgw25295 static int 20676423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 20686643Seschrock boolean_t isroot, boolean_t allowfaulted) 2069789Sahrens { 2070789Sahrens spa_t *spa; 20715094Slling char *altroot = NULL; 20726643Seschrock int error, loaderr; 20732082Seschrock nvlist_t *nvroot; 20745450Sbrendan nvlist_t **spares, **l2cache; 20755450Sbrendan uint_t nspares, nl2cache; 2076789Sahrens 2077789Sahrens /* 2078789Sahrens * If a pool with this name exists, return failure. 2079789Sahrens */ 2080789Sahrens mutex_enter(&spa_namespace_lock); 2081789Sahrens if (spa_lookup(pool) != NULL) { 2082789Sahrens mutex_exit(&spa_namespace_lock); 2083789Sahrens return (EEXIST); 2084789Sahrens } 2085789Sahrens 2086789Sahrens /* 20871635Sbonwick * Create and initialize the spa structure. 2088789Sahrens */ 20895094Slling (void) nvlist_lookup_string(props, 20905094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 20911635Sbonwick spa = spa_add(pool, altroot); 2092789Sahrens spa_activate(spa); 2093789Sahrens 20946643Seschrock if (allowfaulted) 20956643Seschrock spa->spa_import_faulted = B_TRUE; 20966673Seschrock spa->spa_is_root = isroot; 20976643Seschrock 2098789Sahrens /* 20991635Sbonwick * Pass off the heavy lifting to spa_load(). 21007046Sahrens * Pass TRUE for mosconfig (unless this is a root pool) because 21017046Sahrens * the user-supplied config is actually the one to trust when 21027046Sahrens * doing an import. 21031601Sbonwick */ 21047046Sahrens loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot); 2105789Sahrens 21062082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21072082Seschrock /* 21082082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 21092082Seschrock * and conflicts with spa_has_spare(). 21102082Seschrock */ 21116423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 21125450Sbrendan nvlist_free(spa->spa_spares.sav_config); 21135450Sbrendan spa->spa_spares.sav_config = NULL; 21142082Seschrock spa_load_spares(spa); 21152082Seschrock } 21166423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 21175450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 21185450Sbrendan spa->spa_l2cache.sav_config = NULL; 21195450Sbrendan spa_load_l2cache(spa); 21205450Sbrendan } 21212082Seschrock 21222082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 21232082Seschrock &nvroot) == 0); 21245450Sbrendan if (error == 0) 21255450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 21265450Sbrendan if (error == 0) 21275450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 21285450Sbrendan VDEV_ALLOC_L2CACHE); 21292082Seschrock spa_config_exit(spa, FTAG); 21302082Seschrock 21315094Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 21326643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 21336643Seschrock /* 21346643Seschrock * If we failed to load the pool, but 'allowfaulted' is 21356643Seschrock * set, then manually set the config as if the config 21366643Seschrock * passed in was specified in the cache file. 21376643Seschrock */ 21386643Seschrock error = 0; 21396643Seschrock spa->spa_import_faulted = B_FALSE; 21406643Seschrock if (spa->spa_config == NULL) { 21416643Seschrock spa_config_enter(spa, RW_READER, FTAG); 21426643Seschrock spa->spa_config = spa_config_generate(spa, 21436643Seschrock NULL, -1ULL, B_TRUE); 21446643Seschrock spa_config_exit(spa, FTAG); 21456643Seschrock } 21466643Seschrock spa_unload(spa); 21476643Seschrock spa_deactivate(spa); 21486643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 21496643Seschrock } else { 21506643Seschrock spa_unload(spa); 21516643Seschrock spa_deactivate(spa); 21526643Seschrock spa_remove(spa); 21536643Seschrock } 2154789Sahrens mutex_exit(&spa_namespace_lock); 2155789Sahrens return (error); 2156789Sahrens } 2157789Sahrens 21581635Sbonwick /* 21595450Sbrendan * Override any spares and level 2 cache devices as specified by 21605450Sbrendan * the user, as these may have correct device names/devids, etc. 21612082Seschrock */ 21622082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 21632082Seschrock &spares, &nspares) == 0) { 21645450Sbrendan if (spa->spa_spares.sav_config) 21655450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 21662082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 21672082Seschrock else 21685450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 21692082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 21705450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 21712082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 21722082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21732082Seschrock spa_load_spares(spa); 21742082Seschrock spa_config_exit(spa, FTAG); 21755450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 21765450Sbrendan } 21775450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 21785450Sbrendan &l2cache, &nl2cache) == 0) { 21795450Sbrendan if (spa->spa_l2cache.sav_config) 21805450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 21815450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 21825450Sbrendan else 21835450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 21845450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 21855450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 21865450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21875450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 21885450Sbrendan spa_load_l2cache(spa); 21895450Sbrendan spa_config_exit(spa, FTAG); 21905450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 21912082Seschrock } 21922082Seschrock 21936643Seschrock if (spa_mode & FWRITE) { 21946643Seschrock /* 21956643Seschrock * Update the config cache to include the newly-imported pool. 21966643Seschrock */ 21976423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 21986643Seschrock } 21996643Seschrock 22006643Seschrock spa->spa_import_faulted = B_FALSE; 22014451Seschrock mutex_exit(&spa_namespace_lock); 22024451Seschrock 2203789Sahrens return (0); 2204789Sahrens } 2205789Sahrens 22066423Sgw25295 #ifdef _KERNEL 22076423Sgw25295 /* 22086423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 22096423Sgw25295 * device label. 22106423Sgw25295 */ 22116423Sgw25295 static void 22126423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 22136423Sgw25295 { 22146423Sgw25295 nvlist_t *nvtop, *nvroot; 22156423Sgw25295 uint64_t pgid; 22166423Sgw25295 22176423Sgw25295 /* 22186423Sgw25295 * Add this top-level vdev to the child array. 22196423Sgw25295 */ 22206423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 22216423Sgw25295 == 0); 22226423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 22236423Sgw25295 == 0); 22246423Sgw25295 22256423Sgw25295 /* 22266423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 22276423Sgw25295 */ 22286423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 22296423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 22306423Sgw25295 == 0); 22316423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 22326423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 22336423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 22346423Sgw25295 &nvtop, 1) == 0); 22356423Sgw25295 22366423Sgw25295 /* 22376423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 22386423Sgw25295 * this pool's configuration (remove the old, add the new). 22396423Sgw25295 */ 22406423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 22416423Sgw25295 nvlist_free(nvroot); 22426423Sgw25295 } 22436423Sgw25295 22446423Sgw25295 /* 22456423Sgw25295 * Get the root pool information from the root disk, then import the root pool 22466423Sgw25295 * during the system boot up time. 22476423Sgw25295 */ 22487539SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 22497147Staylor 22507147Staylor int 22517147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf, 22526423Sgw25295 uint64_t *besttxg) 22536423Sgw25295 { 22546423Sgw25295 nvlist_t *config; 22556423Sgw25295 uint64_t txg; 22567539SLin.Ling@Sun.COM int error; 22577539SLin.Ling@Sun.COM 22587539SLin.Ling@Sun.COM if (error = vdev_disk_read_rootlabel(devpath, devid, &config)) 22597539SLin.Ling@Sun.COM return (error); 22606423Sgw25295 22616423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 22626423Sgw25295 22637147Staylor if (bestconf != NULL) 22646423Sgw25295 *bestconf = config; 22657539SLin.Ling@Sun.COM else 22667539SLin.Ling@Sun.COM nvlist_free(config); 22677147Staylor *besttxg = txg; 22687147Staylor return (0); 22696423Sgw25295 } 22706423Sgw25295 22716423Sgw25295 boolean_t 22726423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 22736423Sgw25295 { 22746423Sgw25295 uint64_t ival; 22756423Sgw25295 22766423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 22776423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 22786423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 22796423Sgw25295 return (B_FALSE); 22806423Sgw25295 22816423Sgw25295 return (B_TRUE); 22826423Sgw25295 } 22836423Sgw25295 22847147Staylor 22857147Staylor /* 22867147Staylor * Given the boot device's physical path or devid, check if the device 22877147Staylor * is in a valid state. If so, return the configuration from the vdev 22887147Staylor * label. 22897147Staylor */ 22907147Staylor int 22917147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf) 22927147Staylor { 22937147Staylor nvlist_t *conf = NULL; 22947147Staylor uint64_t txg = 0; 22957147Staylor nvlist_t *nvtop, **child; 22967147Staylor char *type; 22977147Staylor char *bootpath = NULL; 22987147Staylor uint_t children, c; 22997147Staylor char *tmp; 23007539SLin.Ling@Sun.COM int error; 23017147Staylor 23027147Staylor if (devpath && ((tmp = strchr(devpath, ' ')) != NULL)) 23037147Staylor *tmp = '\0'; 23047539SLin.Ling@Sun.COM if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) { 23057147Staylor cmn_err(CE_NOTE, "error reading device label"); 23067539SLin.Ling@Sun.COM return (error); 23077147Staylor } 23087147Staylor if (txg == 0) { 23097147Staylor cmn_err(CE_NOTE, "this device is detached"); 23107147Staylor nvlist_free(conf); 23117147Staylor return (EINVAL); 23127147Staylor } 23137147Staylor 23147147Staylor VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE, 23157147Staylor &nvtop) == 0); 23167147Staylor VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0); 23177147Staylor 23187147Staylor if (strcmp(type, VDEV_TYPE_DISK) == 0) { 23197147Staylor if (spa_rootdev_validate(nvtop)) { 23207147Staylor goto out; 23217147Staylor } else { 23227147Staylor nvlist_free(conf); 23237147Staylor return (EINVAL); 23247147Staylor } 23257147Staylor } 23267147Staylor 23277147Staylor ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0); 23287147Staylor 23297147Staylor VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN, 23307147Staylor &child, &children) == 0); 23317147Staylor 23327147Staylor /* 23337147Staylor * Go thru vdevs in the mirror to see if the given device 23347147Staylor * has the most recent txg. Only the device with the most 23357147Staylor * recent txg has valid information and should be booted. 23367147Staylor */ 23377147Staylor for (c = 0; c < children; c++) { 23387147Staylor char *cdevid, *cpath; 23397147Staylor uint64_t tmptxg; 23407147Staylor 23417147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH, 23427147Staylor &cpath) != 0) 23437147Staylor return (EINVAL); 23447147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_DEVID, 23457147Staylor &cdevid) != 0) 23467147Staylor return (EINVAL); 23477687SLin.Ling@Sun.COM if ((spa_check_rootconf(cpath, cdevid, NULL, 23487687SLin.Ling@Sun.COM &tmptxg) == 0) && (tmptxg > txg)) { 23497147Staylor txg = tmptxg; 23507147Staylor VERIFY(nvlist_lookup_string(child[c], 23517147Staylor ZPOOL_CONFIG_PATH, &bootpath) == 0); 23527147Staylor } 23537147Staylor } 23547147Staylor 23557147Staylor /* Does the best device match the one we've booted from? */ 23567147Staylor if (bootpath) { 23577147Staylor cmn_err(CE_NOTE, "try booting from '%s'", bootpath); 23587147Staylor return (EINVAL); 23597147Staylor } 23607147Staylor out: 23617147Staylor *bestconf = conf; 23627147Staylor return (0); 23637147Staylor } 23647147Staylor 23656423Sgw25295 /* 23666423Sgw25295 * Import a root pool. 23676423Sgw25295 * 23687147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 23697147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 23707147Staylor * The GRUB "findroot" command will return the vdev we should boot. 23716423Sgw25295 * 23726423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 23736423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 23746423Sgw25295 * e.g. 23756423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 23766423Sgw25295 */ 23776423Sgw25295 int 23787147Staylor spa_import_rootpool(char *devpath, char *devid) 23796423Sgw25295 { 23806423Sgw25295 nvlist_t *conf = NULL; 23816423Sgw25295 char *pname; 23826423Sgw25295 int error; 23836423Sgw25295 23846423Sgw25295 /* 23856423Sgw25295 * Get the vdev pathname and configuation from the most 23866423Sgw25295 * recently updated vdev (highest txg). 23876423Sgw25295 */ 23887147Staylor if (error = spa_get_rootconf(devpath, devid, &conf)) 23896423Sgw25295 goto msg_out; 23906423Sgw25295 23916423Sgw25295 /* 23926423Sgw25295 * Add type "root" vdev to the config. 23936423Sgw25295 */ 23946423Sgw25295 spa_build_rootpool_config(conf); 23956423Sgw25295 23966423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 23976423Sgw25295 23986673Seschrock /* 23996673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 24006673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 24016673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 24026673Seschrock * pool open, not import. 24036673Seschrock */ 24046673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 24056423Sgw25295 if (error == EEXIST) 24066423Sgw25295 error = 0; 24076423Sgw25295 24086423Sgw25295 nvlist_free(conf); 24096423Sgw25295 return (error); 24106423Sgw25295 24116423Sgw25295 msg_out: 24127147Staylor cmn_err(CE_NOTE, "\n" 24136423Sgw25295 " *************************************************** \n" 24146423Sgw25295 " * This device is not bootable! * \n" 24156423Sgw25295 " * It is either offlined or detached or faulted. * \n" 24166423Sgw25295 " * Please try to boot from a different device. * \n" 24177147Staylor " *************************************************** "); 24186423Sgw25295 24196423Sgw25295 return (error); 24206423Sgw25295 } 24216423Sgw25295 #endif 24226423Sgw25295 24236423Sgw25295 /* 24246423Sgw25295 * Import a non-root pool into the system. 24256423Sgw25295 */ 24266423Sgw25295 int 24276423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 24286423Sgw25295 { 24296643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 24306423Sgw25295 } 24316423Sgw25295 24326643Seschrock int 24336643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 24346643Seschrock { 24356643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 24366643Seschrock } 24376643Seschrock 24386643Seschrock 2439789Sahrens /* 2440789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2441789Sahrens * to get the vdev stats associated with the imported devices. 2442789Sahrens */ 2443789Sahrens #define TRYIMPORT_NAME "$import" 2444789Sahrens 2445789Sahrens nvlist_t * 2446789Sahrens spa_tryimport(nvlist_t *tryconfig) 2447789Sahrens { 2448789Sahrens nvlist_t *config = NULL; 2449789Sahrens char *poolname; 2450789Sahrens spa_t *spa; 2451789Sahrens uint64_t state; 2452789Sahrens 2453789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2454789Sahrens return (NULL); 2455789Sahrens 2456789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2457789Sahrens return (NULL); 2458789Sahrens 24591635Sbonwick /* 24601635Sbonwick * Create and initialize the spa structure. 24611635Sbonwick */ 2462789Sahrens mutex_enter(&spa_namespace_lock); 24631635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2464789Sahrens spa_activate(spa); 2465789Sahrens 2466789Sahrens /* 24671635Sbonwick * Pass off the heavy lifting to spa_load(). 24681732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 24691732Sbonwick * is actually the one to trust when doing an import. 2470789Sahrens */ 24711732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2472789Sahrens 2473789Sahrens /* 2474789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2475789Sahrens */ 2476789Sahrens if (spa->spa_root_vdev != NULL) { 24771635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 2478789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 24791635Sbonwick spa_config_exit(spa, FTAG); 2480789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2481789Sahrens poolname) == 0); 2482789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2483789Sahrens state) == 0); 24843975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 24853975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 24862082Seschrock 24872082Seschrock /* 24886423Sgw25295 * If the bootfs property exists on this pool then we 24896423Sgw25295 * copy it out so that external consumers can tell which 24906423Sgw25295 * pools are bootable. 24916423Sgw25295 */ 24926423Sgw25295 if (spa->spa_bootfs) { 24936423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 24946423Sgw25295 24956423Sgw25295 /* 24966423Sgw25295 * We have to play games with the name since the 24976423Sgw25295 * pool was opened as TRYIMPORT_NAME. 24986423Sgw25295 */ 24996423Sgw25295 if (dsl_dsobj_to_dsname(spa->spa_name, 25006423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 25016423Sgw25295 char *cp; 25026423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 25036423Sgw25295 25046423Sgw25295 cp = strchr(tmpname, '/'); 25056423Sgw25295 if (cp == NULL) { 25066423Sgw25295 (void) strlcpy(dsname, tmpname, 25076423Sgw25295 MAXPATHLEN); 25086423Sgw25295 } else { 25096423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 25106423Sgw25295 "%s/%s", poolname, ++cp); 25116423Sgw25295 } 25126423Sgw25295 VERIFY(nvlist_add_string(config, 25136423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 25146423Sgw25295 kmem_free(dsname, MAXPATHLEN); 25156423Sgw25295 } 25166423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 25176423Sgw25295 } 25186423Sgw25295 25196423Sgw25295 /* 25205450Sbrendan * Add the list of hot spares and level 2 cache devices. 25212082Seschrock */ 25222082Seschrock spa_add_spares(spa, config); 25235450Sbrendan spa_add_l2cache(spa, config); 2524789Sahrens } 2525789Sahrens 2526789Sahrens spa_unload(spa); 2527789Sahrens spa_deactivate(spa); 2528789Sahrens spa_remove(spa); 2529789Sahrens mutex_exit(&spa_namespace_lock); 2530789Sahrens 2531789Sahrens return (config); 2532789Sahrens } 2533789Sahrens 2534789Sahrens /* 2535789Sahrens * Pool export/destroy 2536789Sahrens * 2537789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2538789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2539789Sahrens * update the pool state and sync all the labels to disk, removing the 2540789Sahrens * configuration from the cache afterwards. 2541789Sahrens */ 2542789Sahrens static int 25437214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 25447214Slling boolean_t force) 2545789Sahrens { 2546789Sahrens spa_t *spa; 2547789Sahrens 25481775Sbillm if (oldconfig) 25491775Sbillm *oldconfig = NULL; 25501775Sbillm 2551789Sahrens if (!(spa_mode & FWRITE)) 2552789Sahrens return (EROFS); 2553789Sahrens 2554789Sahrens mutex_enter(&spa_namespace_lock); 2555789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2556789Sahrens mutex_exit(&spa_namespace_lock); 2557789Sahrens return (ENOENT); 2558789Sahrens } 2559789Sahrens 2560789Sahrens /* 25611544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 25621544Seschrock * reacquire the namespace lock, and see if we can export. 25631544Seschrock */ 25641544Seschrock spa_open_ref(spa, FTAG); 25651544Seschrock mutex_exit(&spa_namespace_lock); 25661544Seschrock spa_async_suspend(spa); 25671544Seschrock mutex_enter(&spa_namespace_lock); 25681544Seschrock spa_close(spa, FTAG); 25691544Seschrock 25701544Seschrock /* 2571789Sahrens * The pool will be in core if it's openable, 2572789Sahrens * in which case we can modify its state. 2573789Sahrens */ 2574789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2575789Sahrens /* 2576789Sahrens * Objsets may be open only because they're dirty, so we 2577789Sahrens * have to force it to sync before checking spa_refcnt. 2578789Sahrens */ 2579789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2580789Sahrens 25811544Seschrock /* 25821544Seschrock * A pool cannot be exported or destroyed if there are active 25831544Seschrock * references. If we are resetting a pool, allow references by 25841544Seschrock * fault injection handlers. 25851544Seschrock */ 25861544Seschrock if (!spa_refcount_zero(spa) || 25871544Seschrock (spa->spa_inject_ref != 0 && 25881544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 25891544Seschrock spa_async_resume(spa); 2590789Sahrens mutex_exit(&spa_namespace_lock); 2591789Sahrens return (EBUSY); 2592789Sahrens } 2593789Sahrens 2594789Sahrens /* 25957214Slling * A pool cannot be exported if it has an active shared spare. 25967214Slling * This is to prevent other pools stealing the active spare 25977214Slling * from an exported pool. At user's own will, such pool can 25987214Slling * be forcedly exported. 25997214Slling */ 26007214Slling if (!force && new_state == POOL_STATE_EXPORTED && 26017214Slling spa_has_active_shared_spare(spa)) { 26027214Slling spa_async_resume(spa); 26037214Slling mutex_exit(&spa_namespace_lock); 26047214Slling return (EXDEV); 26057214Slling } 26067214Slling 26077214Slling /* 2608789Sahrens * We want this to be reflected on every label, 2609789Sahrens * so mark them all dirty. spa_unload() will do the 2610789Sahrens * final sync that pushes these changes out. 2611789Sahrens */ 26121544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 26131601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 26141544Seschrock spa->spa_state = new_state; 26151635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 26161544Seschrock vdev_config_dirty(spa->spa_root_vdev); 26171601Sbonwick spa_config_exit(spa, FTAG); 26181544Seschrock } 2619789Sahrens } 2620789Sahrens 26214451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 26224451Seschrock 2623789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2624789Sahrens spa_unload(spa); 2625789Sahrens spa_deactivate(spa); 2626789Sahrens } 2627789Sahrens 26281775Sbillm if (oldconfig && spa->spa_config) 26291775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 26301775Sbillm 26311544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 26326643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 26331544Seschrock spa_remove(spa); 26341544Seschrock } 2635789Sahrens mutex_exit(&spa_namespace_lock); 2636789Sahrens 2637789Sahrens return (0); 2638789Sahrens } 2639789Sahrens 2640789Sahrens /* 2641789Sahrens * Destroy a storage pool. 2642789Sahrens */ 2643789Sahrens int 2644789Sahrens spa_destroy(char *pool) 2645789Sahrens { 26467214Slling return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, B_FALSE)); 2647789Sahrens } 2648789Sahrens 2649789Sahrens /* 2650789Sahrens * Export a storage pool. 2651789Sahrens */ 2652789Sahrens int 26537214Slling spa_export(char *pool, nvlist_t **oldconfig, boolean_t force) 2654789Sahrens { 26557214Slling return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, force)); 2656789Sahrens } 2657789Sahrens 2658789Sahrens /* 26591544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 26601544Seschrock * from the namespace in any way. 26611544Seschrock */ 26621544Seschrock int 26631544Seschrock spa_reset(char *pool) 26641544Seschrock { 26657214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 26667214Slling B_FALSE)); 26671544Seschrock } 26681544Seschrock 26691544Seschrock /* 2670789Sahrens * ========================================================================== 2671789Sahrens * Device manipulation 2672789Sahrens * ========================================================================== 2673789Sahrens */ 2674789Sahrens 2675789Sahrens /* 26764527Sperrin * Add a device to a storage pool. 2677789Sahrens */ 2678789Sahrens int 2679789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2680789Sahrens { 2681789Sahrens uint64_t txg; 26821635Sbonwick int c, error; 2683789Sahrens vdev_t *rvd = spa->spa_root_vdev; 26841585Sbonwick vdev_t *vd, *tvd; 26855450Sbrendan nvlist_t **spares, **l2cache; 26865450Sbrendan uint_t nspares, nl2cache; 2687789Sahrens 2688789Sahrens txg = spa_vdev_enter(spa); 2689789Sahrens 26902082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 26912082Seschrock VDEV_ALLOC_ADD)) != 0) 26922082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 26932082Seschrock 26943377Seschrock spa->spa_pending_vdev = vd; 2695789Sahrens 26965450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 26975450Sbrendan &nspares) != 0) 26982082Seschrock nspares = 0; 26992082Seschrock 27005450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 27015450Sbrendan &nl2cache) != 0) 27025450Sbrendan nl2cache = 0; 27035450Sbrendan 27045450Sbrendan if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) { 27053377Seschrock spa->spa_pending_vdev = NULL; 27062082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 27073377Seschrock } 27082082Seschrock 27092082Seschrock if (vd->vdev_children != 0) { 27103377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 27113377Seschrock spa->spa_pending_vdev = NULL; 27122082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 27132082Seschrock } 27142082Seschrock } 27152082Seschrock 27163377Seschrock /* 27175450Sbrendan * We must validate the spares and l2cache devices after checking the 27185450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 27193377Seschrock */ 27205450Sbrendan if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) { 27213377Seschrock spa->spa_pending_vdev = NULL; 27223377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 27233377Seschrock } 27243377Seschrock 27253377Seschrock spa->spa_pending_vdev = NULL; 27263377Seschrock 27273377Seschrock /* 27283377Seschrock * Transfer each new top-level vdev from vd to rvd. 27293377Seschrock */ 27303377Seschrock for (c = 0; c < vd->vdev_children; c++) { 27313377Seschrock tvd = vd->vdev_child[c]; 27323377Seschrock vdev_remove_child(vd, tvd); 27333377Seschrock tvd->vdev_id = rvd->vdev_children; 27343377Seschrock vdev_add_child(rvd, tvd); 27353377Seschrock vdev_config_dirty(tvd); 27363377Seschrock } 27373377Seschrock 27382082Seschrock if (nspares != 0) { 27395450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 27405450Sbrendan ZPOOL_CONFIG_SPARES); 27412082Seschrock spa_load_spares(spa); 27425450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 27435450Sbrendan } 27445450Sbrendan 27455450Sbrendan if (nl2cache != 0) { 27465450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 27475450Sbrendan ZPOOL_CONFIG_L2CACHE); 27485450Sbrendan spa_load_l2cache(spa); 27495450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2750789Sahrens } 2751789Sahrens 2752789Sahrens /* 27531585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 27541585Sbonwick * If other threads start allocating from these vdevs before we 27551585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 27561585Sbonwick * fail to open the pool because there are DVAs that the config cache 27571585Sbonwick * can't translate. Therefore, we first add the vdevs without 27581585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 27591635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 27601585Sbonwick * 27611585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 27621585Sbonwick * if we lose power at any point in this sequence, the remaining 27631585Sbonwick * steps will be completed the next time we load the pool. 2764789Sahrens */ 27651635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 27661585Sbonwick 27671635Sbonwick mutex_enter(&spa_namespace_lock); 27681635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27691635Sbonwick mutex_exit(&spa_namespace_lock); 2770789Sahrens 27711635Sbonwick return (0); 2772789Sahrens } 2773789Sahrens 2774789Sahrens /* 2775789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2776789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2777789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2778789Sahrens * 2779789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2780789Sahrens * existing device; in this case the two devices are made into their own 27814451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2782789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2783789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2784789Sahrens * completion of resilvering, the first disk (the one being replaced) 2785789Sahrens * is automatically detached. 2786789Sahrens */ 2787789Sahrens int 27881544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2789789Sahrens { 2790789Sahrens uint64_t txg, open_txg; 2791789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2792789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 27932082Seschrock vdev_ops_t *pvops; 27947313SEric.Kustarz@Sun.COM dmu_tx_t *tx; 27957313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 27967313SEric.Kustarz@Sun.COM int newvd_isspare; 27977313SEric.Kustarz@Sun.COM int error; 2798789Sahrens 2799789Sahrens txg = spa_vdev_enter(spa); 2800789Sahrens 28016643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2802789Sahrens 2803789Sahrens if (oldvd == NULL) 2804789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2805789Sahrens 28061585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 28071585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28081585Sbonwick 2809789Sahrens pvd = oldvd->vdev_parent; 2810789Sahrens 28112082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 28124451Seschrock VDEV_ALLOC_ADD)) != 0) 28134451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 28144451Seschrock 28154451Seschrock if (newrootvd->vdev_children != 1) 2816789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2817789Sahrens 2818789Sahrens newvd = newrootvd->vdev_child[0]; 2819789Sahrens 2820789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2821789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2822789Sahrens 28232082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2824789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2825789Sahrens 28264527Sperrin /* 28274527Sperrin * Spares can't replace logs 28284527Sperrin */ 28297326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 28304527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28314527Sperrin 28322082Seschrock if (!replacing) { 28332082Seschrock /* 28342082Seschrock * For attach, the only allowable parent is a mirror or the root 28352082Seschrock * vdev. 28362082Seschrock */ 28372082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 28382082Seschrock pvd->vdev_ops != &vdev_root_ops) 28392082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28402082Seschrock 28412082Seschrock pvops = &vdev_mirror_ops; 28422082Seschrock } else { 28432082Seschrock /* 28442082Seschrock * Active hot spares can only be replaced by inactive hot 28452082Seschrock * spares. 28462082Seschrock */ 28472082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 28482082Seschrock pvd->vdev_child[1] == oldvd && 28492082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 28502082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28512082Seschrock 28522082Seschrock /* 28532082Seschrock * If the source is a hot spare, and the parent isn't already a 28542082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 28553377Seschrock * want to create a replacing vdev. The user is not allowed to 28563377Seschrock * attach to a spared vdev child unless the 'isspare' state is 28573377Seschrock * the same (spare replaces spare, non-spare replaces 28583377Seschrock * non-spare). 28592082Seschrock */ 28602082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 28612082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28623377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 28633377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 28643377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28652082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 28662082Seschrock newvd->vdev_isspare) 28672082Seschrock pvops = &vdev_spare_ops; 28682082Seschrock else 28692082Seschrock pvops = &vdev_replacing_ops; 28702082Seschrock } 28712082Seschrock 28721175Slling /* 28731175Slling * Compare the new device size with the replaceable/attachable 28741175Slling * device size. 28751175Slling */ 28761175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2877789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2878789Sahrens 28791732Sbonwick /* 28801732Sbonwick * The new device cannot have a higher alignment requirement 28811732Sbonwick * than the top-level vdev. 28821732Sbonwick */ 28831732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2884789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2885789Sahrens 2886789Sahrens /* 2887789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2888789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2889789Sahrens */ 2890789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2891789Sahrens spa_strfree(oldvd->vdev_path); 2892789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2893789Sahrens KM_SLEEP); 2894789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2895789Sahrens newvd->vdev_path, "old"); 2896789Sahrens if (oldvd->vdev_devid != NULL) { 2897789Sahrens spa_strfree(oldvd->vdev_devid); 2898789Sahrens oldvd->vdev_devid = NULL; 2899789Sahrens } 2900789Sahrens } 2901789Sahrens 2902789Sahrens /* 29032082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 29042082Seschrock * mirror/replacing/spare vdev above oldvd. 2905789Sahrens */ 2906789Sahrens if (pvd->vdev_ops != pvops) 2907789Sahrens pvd = vdev_add_parent(oldvd, pvops); 2908789Sahrens 2909789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2910789Sahrens ASSERT(pvd->vdev_ops == pvops); 2911789Sahrens ASSERT(oldvd->vdev_parent == pvd); 2912789Sahrens 2913789Sahrens /* 2914789Sahrens * Extract the new device from its root and add it to pvd. 2915789Sahrens */ 2916789Sahrens vdev_remove_child(newrootvd, newvd); 2917789Sahrens newvd->vdev_id = pvd->vdev_children; 2918789Sahrens vdev_add_child(pvd, newvd); 2919789Sahrens 29201544Seschrock /* 29211544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 29221544Seschrock * the addition of newvd may have decreased our parent's asize. 29231544Seschrock */ 29241544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 29251544Seschrock 2926789Sahrens tvd = newvd->vdev_top; 2927789Sahrens ASSERT(pvd->vdev_top == tvd); 2928789Sahrens ASSERT(tvd->vdev_parent == rvd); 2929789Sahrens 2930789Sahrens vdev_config_dirty(tvd); 2931789Sahrens 2932789Sahrens /* 2933789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2934789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2935789Sahrens */ 2936789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2937789Sahrens 2938789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2939789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2940789Sahrens open_txg - TXG_INITIAL + 1); 2941789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2942789Sahrens 29433377Seschrock if (newvd->vdev_isspare) 29443377Seschrock spa_spare_activate(newvd); 29457313SEric.Kustarz@Sun.COM oldvdpath = spa_strdup(vdev_description(oldvd)); 29467313SEric.Kustarz@Sun.COM newvdpath = spa_strdup(vdev_description(newvd)); 29477313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 29481544Seschrock 2949789Sahrens /* 2950789Sahrens * Mark newvd's DTL dirty in this txg. 2951789Sahrens */ 29521732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2953789Sahrens 2954789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2955789Sahrens 29567313SEric.Kustarz@Sun.COM tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 29577313SEric.Kustarz@Sun.COM if (dmu_tx_assign(tx, TXG_WAIT) == 0) { 29587313SEric.Kustarz@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx, 29597313SEric.Kustarz@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 29607313SEric.Kustarz@Sun.COM replacing && newvd_isspare ? "spare in" : 29617313SEric.Kustarz@Sun.COM replacing ? "replace" : "attach", newvdpath, 29627313SEric.Kustarz@Sun.COM replacing ? "for" : "to", oldvdpath); 29637313SEric.Kustarz@Sun.COM dmu_tx_commit(tx); 29647313SEric.Kustarz@Sun.COM } else { 29657313SEric.Kustarz@Sun.COM dmu_tx_abort(tx); 29667313SEric.Kustarz@Sun.COM } 29677313SEric.Kustarz@Sun.COM 29687313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 29697313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 29707313SEric.Kustarz@Sun.COM 2971789Sahrens /* 29727046Sahrens * Kick off a resilver to update newvd. 2973789Sahrens */ 29747046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 2975789Sahrens 2976789Sahrens return (0); 2977789Sahrens } 2978789Sahrens 2979789Sahrens /* 2980789Sahrens * Detach a device from a mirror or replacing vdev. 2981789Sahrens * If 'replace_done' is specified, only detach if the parent 2982789Sahrens * is a replacing vdev. 2983789Sahrens */ 2984789Sahrens int 29851544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2986789Sahrens { 2987789Sahrens uint64_t txg; 2988789Sahrens int c, t, error; 2989789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2990789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 29912082Seschrock boolean_t unspare = B_FALSE; 29922082Seschrock uint64_t unspare_guid; 29936673Seschrock size_t len; 2994789Sahrens 2995789Sahrens txg = spa_vdev_enter(spa); 2996789Sahrens 29976643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 2998789Sahrens 2999789Sahrens if (vd == NULL) 3000789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3001789Sahrens 30021585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 30031585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30041585Sbonwick 3005789Sahrens pvd = vd->vdev_parent; 3006789Sahrens 3007789Sahrens /* 3008789Sahrens * If replace_done is specified, only remove this device if it's 30092082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 30102082Seschrock * disk can be removed. 3011789Sahrens */ 30122082Seschrock if (replace_done) { 30132082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 30142082Seschrock if (vd->vdev_id != 0) 30152082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30162082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 30172082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30182082Seschrock } 30192082Seschrock } 30202082Seschrock 30212082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 30224577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3023789Sahrens 3024789Sahrens /* 30252082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3026789Sahrens */ 3027789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 30282082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 30292082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3030789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3031789Sahrens 3032789Sahrens /* 3033789Sahrens * If there's only one replica, you can't detach it. 3034789Sahrens */ 3035789Sahrens if (pvd->vdev_children <= 1) 3036789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3037789Sahrens 3038789Sahrens /* 3039789Sahrens * If all siblings have non-empty DTLs, this device may have the only 3040789Sahrens * valid copy of the data, which means we cannot safely detach it. 3041789Sahrens * 3042789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 3043789Sahrens * precise DTL check. 3044789Sahrens */ 3045789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 3046789Sahrens uint64_t dirty; 3047789Sahrens 3048789Sahrens cvd = pvd->vdev_child[c]; 3049789Sahrens if (cvd == vd) 3050789Sahrens continue; 3051789Sahrens if (vdev_is_dead(cvd)) 3052789Sahrens continue; 3053789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 3054789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 3055789Sahrens cvd->vdev_dtl_scrub.sm_space; 3056789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 3057789Sahrens if (!dirty) 3058789Sahrens break; 3059789Sahrens } 30602082Seschrock 30612082Seschrock /* 30622082Seschrock * If we are a replacing or spare vdev, then we can always detach the 30632082Seschrock * latter child, as that is how one cancels the operation. 30642082Seschrock */ 30652082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 30662082Seschrock c == pvd->vdev_children) 3067789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3068789Sahrens 3069789Sahrens /* 30706673Seschrock * If we are detaching the second disk from a replacing vdev, then 30716673Seschrock * check to see if we changed the original vdev's path to have "/old" 30726673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 30736673Seschrock */ 30746673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 30756673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 30766673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 30776673Seschrock ASSERT(pvd->vdev_child[1] == vd); 30786673Seschrock cvd = pvd->vdev_child[0]; 30796673Seschrock len = strlen(vd->vdev_path); 30806673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 30816673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 30826673Seschrock spa_strfree(cvd->vdev_path); 30836673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 30846673Seschrock } 30856673Seschrock } 30866673Seschrock 30876673Seschrock /* 30882082Seschrock * If we are detaching the original disk from a spare, then it implies 30892082Seschrock * that the spare should become a real disk, and be removed from the 30902082Seschrock * active spare list for the pool. 30912082Seschrock */ 30922082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 30932082Seschrock vd->vdev_id == 0) 30942082Seschrock unspare = B_TRUE; 30952082Seschrock 30962082Seschrock /* 3097789Sahrens * Erase the disk labels so the disk can be used for other things. 3098789Sahrens * This must be done after all other error cases are handled, 3099789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3100789Sahrens * But if we can't do it, don't treat the error as fatal -- 3101789Sahrens * it may be that the unwritability of the disk is the reason 3102789Sahrens * it's being detached! 3103789Sahrens */ 31043377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3105789Sahrens 3106789Sahrens /* 3107789Sahrens * Remove vd from its parent and compact the parent's children. 3108789Sahrens */ 3109789Sahrens vdev_remove_child(pvd, vd); 3110789Sahrens vdev_compact_children(pvd); 3111789Sahrens 3112789Sahrens /* 3113789Sahrens * Remember one of the remaining children so we can get tvd below. 3114789Sahrens */ 3115789Sahrens cvd = pvd->vdev_child[0]; 3116789Sahrens 3117789Sahrens /* 31182082Seschrock * If we need to remove the remaining child from the list of hot spares, 31192082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 31202082Seschrock * must do this before vdev_remove_parent(), because that can change the 31212082Seschrock * GUID if it creates a new toplevel GUID. 31222082Seschrock */ 31232082Seschrock if (unspare) { 31242082Seschrock ASSERT(cvd->vdev_isspare); 31253377Seschrock spa_spare_remove(cvd); 31262082Seschrock unspare_guid = cvd->vdev_guid; 31272082Seschrock } 31282082Seschrock 31292082Seschrock /* 3130789Sahrens * If the parent mirror/replacing vdev only has one child, 3131789Sahrens * the parent is no longer needed. Remove it from the tree. 3132789Sahrens */ 3133789Sahrens if (pvd->vdev_children == 1) 3134789Sahrens vdev_remove_parent(cvd); 3135789Sahrens 3136789Sahrens /* 3137789Sahrens * We don't set tvd until now because the parent we just removed 3138789Sahrens * may have been the previous top-level vdev. 3139789Sahrens */ 3140789Sahrens tvd = cvd->vdev_top; 3141789Sahrens ASSERT(tvd->vdev_parent == rvd); 3142789Sahrens 3143789Sahrens /* 31443377Seschrock * Reevaluate the parent vdev state. 3145789Sahrens */ 31464451Seschrock vdev_propagate_state(cvd); 3147789Sahrens 3148789Sahrens /* 31493377Seschrock * If the device we just detached was smaller than the others, it may be 31503377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 31513377Seschrock * can't fail because the existing metaslabs are already in core, so 31523377Seschrock * there's nothing to read from disk. 3153789Sahrens */ 31541732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3155789Sahrens 3156789Sahrens vdev_config_dirty(tvd); 3157789Sahrens 3158789Sahrens /* 31593377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 31603377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 31613377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 31623377Seschrock * prevent vd from being accessed after it's freed. 3163789Sahrens */ 3164789Sahrens for (t = 0; t < TXG_SIZE; t++) 3165789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 31661732Sbonwick vd->vdev_detached = B_TRUE; 31671732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3168789Sahrens 31694451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 31704451Seschrock 31712082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 31722082Seschrock 31732082Seschrock /* 31743377Seschrock * If this was the removal of the original device in a hot spare vdev, 31753377Seschrock * then we want to go through and remove the device from the hot spare 31763377Seschrock * list of every other pool. 31772082Seschrock */ 31782082Seschrock if (unspare) { 31792082Seschrock spa = NULL; 31802082Seschrock mutex_enter(&spa_namespace_lock); 31812082Seschrock while ((spa = spa_next(spa)) != NULL) { 31822082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 31832082Seschrock continue; 31842082Seschrock 31852082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 31862082Seschrock } 31872082Seschrock mutex_exit(&spa_namespace_lock); 31882082Seschrock } 31892082Seschrock 31902082Seschrock return (error); 31912082Seschrock } 31922082Seschrock 31932082Seschrock /* 31945450Sbrendan * Remove a spares vdev from the nvlist config. 31952082Seschrock */ 31965450Sbrendan static int 31975450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare, 31985450Sbrendan nvlist_t **spares, int nspares, vdev_t *vd) 31992082Seschrock { 32005450Sbrendan nvlist_t *nv, **newspares; 32015450Sbrendan int i, j; 32022082Seschrock 32032082Seschrock nv = NULL; 32045450Sbrendan for (i = 0; i < nspares; i++) { 32055450Sbrendan uint64_t theguid; 32065450Sbrendan 32075450Sbrendan VERIFY(nvlist_lookup_uint64(spares[i], 32085450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 32095450Sbrendan if (theguid == guid) { 32105450Sbrendan nv = spares[i]; 32115450Sbrendan break; 32122082Seschrock } 32132082Seschrock } 32142082Seschrock 32152082Seschrock /* 32165450Sbrendan * Only remove the hot spare if it's not currently in use in this pool. 32172082Seschrock */ 32185450Sbrendan if (nv == NULL && vd == NULL) 32195450Sbrendan return (ENOENT); 32205450Sbrendan 32215450Sbrendan if (nv == NULL && vd != NULL) 32225450Sbrendan return (ENOTSUP); 32235450Sbrendan 32245450Sbrendan if (!unspare && nv != NULL && vd != NULL) 32255450Sbrendan return (EBUSY); 32262082Seschrock 32272082Seschrock if (nspares == 1) { 32282082Seschrock newspares = NULL; 32292082Seschrock } else { 32302082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 32312082Seschrock KM_SLEEP); 32322082Seschrock for (i = 0, j = 0; i < nspares; i++) { 32332082Seschrock if (spares[i] != nv) 32342082Seschrock VERIFY(nvlist_dup(spares[i], 32352082Seschrock &newspares[j++], KM_SLEEP) == 0); 32362082Seschrock } 32372082Seschrock } 32382082Seschrock 32395450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES, 32402082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 32415450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 32425450Sbrendan ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0); 32432082Seschrock for (i = 0; i < nspares - 1; i++) 32442082Seschrock nvlist_free(newspares[i]); 32452082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 32465450Sbrendan 32475450Sbrendan return (0); 32485450Sbrendan } 32495450Sbrendan 32505450Sbrendan /* 32515450Sbrendan * Remove an l2cache vdev from the nvlist config. 32525450Sbrendan */ 32535450Sbrendan static int 32545450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache, 32555450Sbrendan int nl2cache, vdev_t *vd) 32565450Sbrendan { 32575450Sbrendan nvlist_t *nv, **newl2cache; 32585450Sbrendan int i, j; 32595450Sbrendan 32605450Sbrendan nv = NULL; 32615450Sbrendan for (i = 0; i < nl2cache; i++) { 32625450Sbrendan uint64_t theguid; 32635450Sbrendan 32645450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 32655450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 32665450Sbrendan if (theguid == guid) { 32675450Sbrendan nv = l2cache[i]; 32685450Sbrendan break; 32695450Sbrendan } 32705450Sbrendan } 32715450Sbrendan 32725450Sbrendan if (vd == NULL) { 32735450Sbrendan for (i = 0; i < nl2cache; i++) { 32745450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) { 32755450Sbrendan vd = sav->sav_vdevs[i]; 32765450Sbrendan break; 32775450Sbrendan } 32785450Sbrendan } 32795450Sbrendan } 32805450Sbrendan 32815450Sbrendan if (nv == NULL && vd == NULL) 32825450Sbrendan return (ENOENT); 32835450Sbrendan 32845450Sbrendan if (nv == NULL && vd != NULL) 32855450Sbrendan return (ENOTSUP); 32865450Sbrendan 32875450Sbrendan if (nl2cache == 1) { 32885450Sbrendan newl2cache = NULL; 32895450Sbrendan } else { 32905450Sbrendan newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *), 32915450Sbrendan KM_SLEEP); 32925450Sbrendan for (i = 0, j = 0; i < nl2cache; i++) { 32935450Sbrendan if (l2cache[i] != nv) 32945450Sbrendan VERIFY(nvlist_dup(l2cache[i], 32955450Sbrendan &newl2cache[j++], KM_SLEEP) == 0); 32965450Sbrendan } 32975450Sbrendan } 32985450Sbrendan 32995450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 33005450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 33015450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 33025450Sbrendan ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0); 33035450Sbrendan for (i = 0; i < nl2cache - 1; i++) 33045450Sbrendan nvlist_free(newl2cache[i]); 33055450Sbrendan kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *)); 33065450Sbrendan 33075450Sbrendan return (0); 33085450Sbrendan } 33095450Sbrendan 33105450Sbrendan /* 33115450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 33125450Sbrendan * spares and level 2 ARC devices. 33135450Sbrendan */ 33145450Sbrendan int 33155450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 33165450Sbrendan { 33175450Sbrendan vdev_t *vd; 33185450Sbrendan nvlist_t **spares, **l2cache; 33195450Sbrendan uint_t nspares, nl2cache; 33205450Sbrendan int error = 0; 33215450Sbrendan 33225450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 33235450Sbrendan 33246643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 33255450Sbrendan 33265450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 33275450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 33285450Sbrendan ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 33295450Sbrendan if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare, 33305450Sbrendan spares, nspares, vd)) != 0) 33317361SBrendan.Gregg@Sun.COM goto cache; 33325450Sbrendan spa_load_spares(spa); 33335450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 33345450Sbrendan goto out; 33355450Sbrendan } 33365450Sbrendan 33377361SBrendan.Gregg@Sun.COM cache: 33385450Sbrendan if (spa->spa_l2cache.sav_vdevs != NULL && 33395450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 33405450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) { 33415450Sbrendan if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid, 33425450Sbrendan l2cache, nl2cache, vd)) != 0) 33435450Sbrendan goto out; 33445450Sbrendan spa_load_l2cache(spa); 33455450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 33465450Sbrendan } 33472082Seschrock 33482082Seschrock out: 33492082Seschrock spa_config_exit(spa, FTAG); 33505450Sbrendan return (error); 3351789Sahrens } 3352789Sahrens 3353789Sahrens /* 33544451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 33554451Seschrock * current spared, so we can detach it. 3356789Sahrens */ 33571544Seschrock static vdev_t * 33584451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3359789Sahrens { 33601544Seschrock vdev_t *newvd, *oldvd; 3361789Sahrens int c; 3362789Sahrens 33631544Seschrock for (c = 0; c < vd->vdev_children; c++) { 33644451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 33651544Seschrock if (oldvd != NULL) 33661544Seschrock return (oldvd); 33671544Seschrock } 3368789Sahrens 33694451Seschrock /* 33704451Seschrock * Check for a completed replacement. 33714451Seschrock */ 3372789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 33731544Seschrock oldvd = vd->vdev_child[0]; 33741544Seschrock newvd = vd->vdev_child[1]; 3375789Sahrens 33761544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 33771544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 33781544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 33791544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33801544Seschrock return (oldvd); 33811544Seschrock } 33821544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33831544Seschrock } 3384789Sahrens 33854451Seschrock /* 33864451Seschrock * Check for a completed resilver with the 'unspare' flag set. 33874451Seschrock */ 33884451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 33894451Seschrock newvd = vd->vdev_child[0]; 33904451Seschrock oldvd = vd->vdev_child[1]; 33914451Seschrock 33924451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 33934451Seschrock if (newvd->vdev_unspare && 33944451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 33954451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 33964451Seschrock newvd->vdev_unspare = 0; 33974451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33984451Seschrock return (oldvd); 33994451Seschrock } 34004451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 34014451Seschrock } 34024451Seschrock 34031544Seschrock return (NULL); 3404789Sahrens } 3405789Sahrens 34061544Seschrock static void 34074451Seschrock spa_vdev_resilver_done(spa_t *spa) 3408789Sahrens { 34091544Seschrock vdev_t *vd; 34102082Seschrock vdev_t *pvd; 34111544Seschrock uint64_t guid; 34122082Seschrock uint64_t pguid = 0; 3413789Sahrens 34141544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3415789Sahrens 34164451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 34171544Seschrock guid = vd->vdev_guid; 34182082Seschrock /* 34192082Seschrock * If we have just finished replacing a hot spared device, then 34202082Seschrock * we need to detach the parent's first child (the original hot 34212082Seschrock * spare) as well. 34222082Seschrock */ 34232082Seschrock pvd = vd->vdev_parent; 34242082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 34252082Seschrock pvd->vdev_id == 0) { 34262082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 34272082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 34282082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 34292082Seschrock } 34301544Seschrock spa_config_exit(spa, FTAG); 34311544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 34321544Seschrock return; 34332082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 34342082Seschrock return; 34351544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3436789Sahrens } 3437789Sahrens 34381544Seschrock spa_config_exit(spa, FTAG); 3439789Sahrens } 3440789Sahrens 3441789Sahrens /* 34421354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 34431354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 34441354Seschrock */ 34451354Seschrock int 34461354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 34471354Seschrock { 34486643Seschrock vdev_t *vd; 34491354Seschrock uint64_t txg; 34501354Seschrock 34511354Seschrock txg = spa_vdev_enter(spa); 34521354Seschrock 34536643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 34542082Seschrock /* 34556643Seschrock * Determine if this is a reference to a hot spare device. If 34566643Seschrock * it is, update the path manually as there is no associated 34576643Seschrock * vdev_t that can be synced to disk. 34582082Seschrock */ 34596643Seschrock nvlist_t **spares; 34606643Seschrock uint_t i, nspares; 34615450Sbrendan 34625450Sbrendan if (spa->spa_spares.sav_config != NULL) { 34635450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 34645450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 34655450Sbrendan &spares, &nspares) == 0); 34662082Seschrock for (i = 0; i < nspares; i++) { 34672082Seschrock uint64_t theguid; 34682082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 34692082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 34705450Sbrendan if (theguid == guid) { 34715450Sbrendan VERIFY(nvlist_add_string(spares[i], 34725450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 34735450Sbrendan spa_load_spares(spa); 34745450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 34755450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 34765450Sbrendan 0)); 34775450Sbrendan } 34782082Seschrock } 34792082Seschrock } 34805450Sbrendan 34815450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 34822082Seschrock } 34831354Seschrock 34841585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 34851585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 34861585Sbonwick 34871354Seschrock spa_strfree(vd->vdev_path); 34881354Seschrock vd->vdev_path = spa_strdup(newpath); 34891354Seschrock 34901354Seschrock vdev_config_dirty(vd->vdev_top); 34911354Seschrock 34921354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 34931354Seschrock } 34941354Seschrock 34951354Seschrock /* 3496789Sahrens * ========================================================================== 3497789Sahrens * SPA Scrubbing 3498789Sahrens * ========================================================================== 3499789Sahrens */ 3500789Sahrens 35017046Sahrens int 35027046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3503789Sahrens { 35044808Sek110237 ASSERT(!spa_config_held(spa, RW_WRITER)); 35054808Sek110237 3506789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3507789Sahrens return (ENOTSUP); 3508789Sahrens 3509789Sahrens /* 35107046Sahrens * If a resilver was requested, but there is no DTL on a 35117046Sahrens * writeable leaf device, we have nothing to do. 3512789Sahrens */ 35137046Sahrens if (type == POOL_SCRUB_RESILVER && 35147046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 35157046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 35161544Seschrock return (0); 35171544Seschrock } 3518789Sahrens 35197046Sahrens if (type == POOL_SCRUB_EVERYTHING && 35207046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 35217046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 35227046Sahrens return (EBUSY); 35237046Sahrens 35247046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 35257046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 35267046Sahrens } else if (type == POOL_SCRUB_NONE) { 35277046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 35281544Seschrock } else { 35297046Sahrens return (EINVAL); 35301544Seschrock } 3531789Sahrens } 3532789Sahrens 35331544Seschrock /* 35341544Seschrock * ========================================================================== 35351544Seschrock * SPA async task processing 35361544Seschrock * ========================================================================== 35371544Seschrock */ 35381544Seschrock 35391544Seschrock static void 35404451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3541789Sahrens { 35421544Seschrock int c; 35431544Seschrock 35447361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 35457361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 35467361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 35477361SBrendan.Gregg@Sun.COM vdev_clear(spa, vd, B_TRUE); 35487361SBrendan.Gregg@Sun.COM vdev_config_dirty(vd->vdev_top); 35491544Seschrock } 35507361SBrendan.Gregg@Sun.COM 35517361SBrendan.Gregg@Sun.COM for (c = 0; c < vd->vdev_children; c++) 35527361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 35531544Seschrock } 35541544Seschrock 35551544Seschrock static void 35561544Seschrock spa_async_thread(spa_t *spa) 35571544Seschrock { 35587361SBrendan.Gregg@Sun.COM int tasks, i; 35594451Seschrock uint64_t txg; 35601544Seschrock 35611544Seschrock ASSERT(spa->spa_sync_on); 3562789Sahrens 35631544Seschrock mutex_enter(&spa->spa_async_lock); 35641544Seschrock tasks = spa->spa_async_tasks; 35651544Seschrock spa->spa_async_tasks = 0; 35661544Seschrock mutex_exit(&spa->spa_async_lock); 35671544Seschrock 35681544Seschrock /* 35691635Sbonwick * See if the config needs to be updated. 35701635Sbonwick */ 35711635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 35721635Sbonwick mutex_enter(&spa_namespace_lock); 35731635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 35741635Sbonwick mutex_exit(&spa_namespace_lock); 35751635Sbonwick } 35761635Sbonwick 35771635Sbonwick /* 35784451Seschrock * See if any devices need to be marked REMOVED. 35795329Sgw25295 * 35805329Sgw25295 * XXX - We avoid doing this when we are in 35815329Sgw25295 * I/O failure state since spa_vdev_enter() grabs 35825329Sgw25295 * the namespace lock and would not be able to obtain 35835329Sgw25295 * the writer config lock. 35841544Seschrock */ 35855329Sgw25295 if (tasks & SPA_ASYNC_REMOVE && 35865329Sgw25295 spa_state(spa) != POOL_STATE_IO_FAILURE) { 35874451Seschrock txg = spa_vdev_enter(spa); 35884451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 35897361SBrendan.Gregg@Sun.COM for (i = 0; i < spa->spa_l2cache.sav_count; i++) 35907361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 35917361SBrendan.Gregg@Sun.COM for (i = 0; i < spa->spa_spares.sav_count; i++) 35927361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 35934451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 35944451Seschrock } 35951544Seschrock 35961544Seschrock /* 35971544Seschrock * If any devices are done replacing, detach them. 35981544Seschrock */ 35994451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 36004451Seschrock spa_vdev_resilver_done(spa); 3601789Sahrens 36021544Seschrock /* 36031544Seschrock * Kick off a resilver. 36041544Seschrock */ 36057046Sahrens if (tasks & SPA_ASYNC_RESILVER) 36067046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 36071544Seschrock 36081544Seschrock /* 36091544Seschrock * Let the world know that we're done. 36101544Seschrock */ 36111544Seschrock mutex_enter(&spa->spa_async_lock); 36121544Seschrock spa->spa_async_thread = NULL; 36131544Seschrock cv_broadcast(&spa->spa_async_cv); 36141544Seschrock mutex_exit(&spa->spa_async_lock); 36151544Seschrock thread_exit(); 36161544Seschrock } 36171544Seschrock 36181544Seschrock void 36191544Seschrock spa_async_suspend(spa_t *spa) 36201544Seschrock { 36211544Seschrock mutex_enter(&spa->spa_async_lock); 36221544Seschrock spa->spa_async_suspended++; 36231544Seschrock while (spa->spa_async_thread != NULL) 36241544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 36251544Seschrock mutex_exit(&spa->spa_async_lock); 36261544Seschrock } 36271544Seschrock 36281544Seschrock void 36291544Seschrock spa_async_resume(spa_t *spa) 36301544Seschrock { 36311544Seschrock mutex_enter(&spa->spa_async_lock); 36321544Seschrock ASSERT(spa->spa_async_suspended != 0); 36331544Seschrock spa->spa_async_suspended--; 36341544Seschrock mutex_exit(&spa->spa_async_lock); 36351544Seschrock } 36361544Seschrock 36371544Seschrock static void 36381544Seschrock spa_async_dispatch(spa_t *spa) 36391544Seschrock { 36401544Seschrock mutex_enter(&spa->spa_async_lock); 36411544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 36421635Sbonwick spa->spa_async_thread == NULL && 36431635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 36441544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 36451544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 36461544Seschrock mutex_exit(&spa->spa_async_lock); 36471544Seschrock } 36481544Seschrock 36491544Seschrock void 36501544Seschrock spa_async_request(spa_t *spa, int task) 36511544Seschrock { 36521544Seschrock mutex_enter(&spa->spa_async_lock); 36531544Seschrock spa->spa_async_tasks |= task; 36541544Seschrock mutex_exit(&spa->spa_async_lock); 3655789Sahrens } 3656789Sahrens 3657789Sahrens /* 3658789Sahrens * ========================================================================== 3659789Sahrens * SPA syncing routines 3660789Sahrens * ========================================================================== 3661789Sahrens */ 3662789Sahrens 3663789Sahrens static void 3664789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3665789Sahrens { 3666789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3667789Sahrens dmu_tx_t *tx; 3668789Sahrens blkptr_t blk; 3669789Sahrens uint64_t itor = 0; 3670789Sahrens zio_t *zio; 3671789Sahrens int error; 3672789Sahrens uint8_t c = 1; 3673789Sahrens 3674789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3675789Sahrens 3676789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3677789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3678789Sahrens 3679789Sahrens error = zio_wait(zio); 3680789Sahrens ASSERT3U(error, ==, 0); 3681789Sahrens 3682789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3683789Sahrens bplist_vacate(bpl, tx); 3684789Sahrens 3685789Sahrens /* 3686789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3687789Sahrens * (Usually only the first block is needed.) 3688789Sahrens */ 3689789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3690789Sahrens dmu_tx_commit(tx); 3691789Sahrens } 3692789Sahrens 3693789Sahrens static void 36942082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 36952082Seschrock { 36962082Seschrock char *packed = NULL; 36977497STim.Haley@Sun.COM size_t bufsize; 36982082Seschrock size_t nvsize = 0; 36992082Seschrock dmu_buf_t *db; 37002082Seschrock 37012082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 37022082Seschrock 37037497STim.Haley@Sun.COM /* 37047497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 37057497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 37067497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 37077497STim.Haley@Sun.COM */ 37087497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 37097497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 37102082Seschrock 37112082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 37122082Seschrock KM_SLEEP) == 0); 37137497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 37147497STim.Haley@Sun.COM 37157497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 37167497STim.Haley@Sun.COM 37177497STim.Haley@Sun.COM kmem_free(packed, bufsize); 37182082Seschrock 37192082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 37202082Seschrock dmu_buf_will_dirty(db, tx); 37212082Seschrock *(uint64_t *)db->db_data = nvsize; 37222082Seschrock dmu_buf_rele(db, FTAG); 37232082Seschrock } 37242082Seschrock 37252082Seschrock static void 37265450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 37275450Sbrendan const char *config, const char *entry) 37282082Seschrock { 37292082Seschrock nvlist_t *nvroot; 37305450Sbrendan nvlist_t **list; 37312082Seschrock int i; 37322082Seschrock 37335450Sbrendan if (!sav->sav_sync) 37342082Seschrock return; 37352082Seschrock 37362082Seschrock /* 37375450Sbrendan * Update the MOS nvlist describing the list of available devices. 37385450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 37394451Seschrock * valid and the vdevs are labeled appropriately. 37402082Seschrock */ 37415450Sbrendan if (sav->sav_object == 0) { 37425450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 37435450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 37445450Sbrendan sizeof (uint64_t), tx); 37452082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 37465450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 37475450Sbrendan &sav->sav_object, tx) == 0); 37482082Seschrock } 37492082Seschrock 37502082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 37515450Sbrendan if (sav->sav_count == 0) { 37525450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 37532082Seschrock } else { 37545450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 37555450Sbrendan for (i = 0; i < sav->sav_count; i++) 37565450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 37575450Sbrendan B_FALSE, B_FALSE, B_TRUE); 37585450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 37595450Sbrendan sav->sav_count) == 0); 37605450Sbrendan for (i = 0; i < sav->sav_count; i++) 37615450Sbrendan nvlist_free(list[i]); 37625450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 37632082Seschrock } 37642082Seschrock 37655450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 37662926Sek110237 nvlist_free(nvroot); 37672082Seschrock 37685450Sbrendan sav->sav_sync = B_FALSE; 37692082Seschrock } 37702082Seschrock 37712082Seschrock static void 3772789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3773789Sahrens { 3774789Sahrens nvlist_t *config; 3775789Sahrens 3776789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 3777789Sahrens return; 3778789Sahrens 3779789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 3780789Sahrens 37811635Sbonwick if (spa->spa_config_syncing) 37821635Sbonwick nvlist_free(spa->spa_config_syncing); 37831635Sbonwick spa->spa_config_syncing = config; 3784789Sahrens 37852082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3786789Sahrens } 3787789Sahrens 37885094Slling /* 37895094Slling * Set zpool properties. 37905094Slling */ 37913912Slling static void 37924543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 37933912Slling { 37943912Slling spa_t *spa = arg1; 37955094Slling objset_t *mos = spa->spa_meta_objset; 37963912Slling nvlist_t *nvp = arg2; 37975094Slling nvpair_t *elem; 37984451Seschrock uint64_t intval; 37996643Seschrock char *strval; 38005094Slling zpool_prop_t prop; 38015094Slling const char *propname; 38025094Slling zprop_type_t proptype; 38036643Seschrock spa_config_dirent_t *dp; 38045094Slling 38055094Slling elem = NULL; 38065094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 38075094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 38085094Slling case ZPOOL_PROP_VERSION: 38095094Slling /* 38105094Slling * Only set version for non-zpool-creation cases 38115094Slling * (set/import). spa_create() needs special care 38125094Slling * for version setting. 38135094Slling */ 38145094Slling if (tx->tx_txg != TXG_INITIAL) { 38155094Slling VERIFY(nvpair_value_uint64(elem, 38165094Slling &intval) == 0); 38175094Slling ASSERT(intval <= SPA_VERSION); 38185094Slling ASSERT(intval >= spa_version(spa)); 38195094Slling spa->spa_uberblock.ub_version = intval; 38205094Slling vdev_config_dirty(spa->spa_root_vdev); 38215094Slling } 38225094Slling break; 38235094Slling 38245094Slling case ZPOOL_PROP_ALTROOT: 38255094Slling /* 38265094Slling * 'altroot' is a non-persistent property. It should 38275094Slling * have been set temporarily at creation or import time. 38285094Slling */ 38295094Slling ASSERT(spa->spa_root != NULL); 38305094Slling break; 38315094Slling 38325363Seschrock case ZPOOL_PROP_CACHEFILE: 38335094Slling /* 38345363Seschrock * 'cachefile' is a non-persistent property, but note 38355363Seschrock * an async request that the config cache needs to be 38365363Seschrock * udpated. 38375094Slling */ 38385363Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 38396643Seschrock 38406643Seschrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 38416643Seschrock KM_SLEEP); 38426643Seschrock 38436643Seschrock if (strval[0] == '\0') 38446643Seschrock dp->scd_path = spa_strdup(spa_config_path); 38456643Seschrock else if (strcmp(strval, "none") == 0) 38466643Seschrock dp->scd_path = NULL; 38476643Seschrock else 38486643Seschrock dp->scd_path = spa_strdup(strval); 38496643Seschrock 38506643Seschrock list_insert_head(&spa->spa_config_list, dp); 38515363Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 38524543Smarks break; 38535094Slling default: 38545094Slling /* 38555094Slling * Set pool property values in the poolprops mos object. 38565094Slling */ 38575094Slling mutex_enter(&spa->spa_props_lock); 38585094Slling if (spa->spa_pool_props_object == 0) { 38595094Slling objset_t *mos = spa->spa_meta_objset; 38605094Slling 38615094Slling VERIFY((spa->spa_pool_props_object = 38625094Slling zap_create(mos, DMU_OT_POOL_PROPS, 38635094Slling DMU_OT_NONE, 0, tx)) > 0); 38645094Slling 38655094Slling VERIFY(zap_update(mos, 38665094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 38675094Slling 8, 1, &spa->spa_pool_props_object, tx) 38685094Slling == 0); 38695094Slling } 38705094Slling mutex_exit(&spa->spa_props_lock); 38715094Slling 38725094Slling /* normalize the property name */ 38735094Slling propname = zpool_prop_to_name(prop); 38745094Slling proptype = zpool_prop_get_type(prop); 38755094Slling 38765094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 38775094Slling ASSERT(proptype == PROP_TYPE_STRING); 38785094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 38795094Slling VERIFY(zap_update(mos, 38805094Slling spa->spa_pool_props_object, propname, 38815094Slling 1, strlen(strval) + 1, strval, tx) == 0); 38825094Slling 38835094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 38845094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 38855094Slling 38865094Slling if (proptype == PROP_TYPE_INDEX) { 38875094Slling const char *unused; 38885094Slling VERIFY(zpool_prop_index_to_string( 38895094Slling prop, intval, &unused) == 0); 38905094Slling } 38915094Slling VERIFY(zap_update(mos, 38925094Slling spa->spa_pool_props_object, propname, 38935094Slling 8, 1, &intval, tx) == 0); 38945094Slling } else { 38955094Slling ASSERT(0); /* not allowed */ 38965094Slling } 38975094Slling 38985329Sgw25295 switch (prop) { 38995329Sgw25295 case ZPOOL_PROP_DELEGATION: 39005094Slling spa->spa_delegation = intval; 39015329Sgw25295 break; 39025329Sgw25295 case ZPOOL_PROP_BOOTFS: 39035094Slling spa->spa_bootfs = intval; 39045329Sgw25295 break; 39055329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 39065329Sgw25295 spa->spa_failmode = intval; 39075329Sgw25295 break; 39085329Sgw25295 default: 39095329Sgw25295 break; 39105329Sgw25295 } 39113912Slling } 39125094Slling 39135094Slling /* log internal history if this is not a zpool create */ 39145094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 39155094Slling tx->tx_txg != TXG_INITIAL) { 39165094Slling spa_history_internal_log(LOG_POOL_PROPSET, 39175094Slling spa, tx, cr, "%s %lld %s", 39185094Slling nvpair_name(elem), intval, spa->spa_name); 39195094Slling } 39203912Slling } 39213912Slling } 39223912Slling 3923789Sahrens /* 3924789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3925789Sahrens * part of the process, so we iterate until it converges. 3926789Sahrens */ 3927789Sahrens void 3928789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3929789Sahrens { 3930789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3931789Sahrens objset_t *mos = spa->spa_meta_objset; 3932789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 39331635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3934789Sahrens vdev_t *vd; 3935789Sahrens dmu_tx_t *tx; 3936789Sahrens int dirty_vdevs; 3937789Sahrens 3938789Sahrens /* 3939789Sahrens * Lock out configuration changes. 3940789Sahrens */ 39411544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3942789Sahrens 3943789Sahrens spa->spa_syncing_txg = txg; 3944789Sahrens spa->spa_sync_pass = 0; 3945789Sahrens 39461544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3947789Sahrens 39482082Seschrock tx = dmu_tx_create_assigned(dp, txg); 39492082Seschrock 39502082Seschrock /* 39514577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 39522082Seschrock * set spa_deflate if we have no raid-z vdevs. 39532082Seschrock */ 39544577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 39554577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 39562082Seschrock int i; 39572082Seschrock 39582082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 39592082Seschrock vd = rvd->vdev_child[i]; 39602082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 39612082Seschrock break; 39622082Seschrock } 39632082Seschrock if (i == rvd->vdev_children) { 39642082Seschrock spa->spa_deflate = TRUE; 39652082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 39662082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 39672082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 39682082Seschrock } 39692082Seschrock } 39702082Seschrock 39717046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 39727046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 39737046Sahrens dsl_pool_create_origin(dp, tx); 39747046Sahrens 39757046Sahrens /* Keeping the origin open increases spa_minref */ 39767046Sahrens spa->spa_minref += 3; 39777046Sahrens } 39787046Sahrens 39797046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 39807046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 39817046Sahrens dsl_pool_upgrade_clones(dp, tx); 39827046Sahrens } 39837046Sahrens 3984789Sahrens /* 3985789Sahrens * If anything has changed in this txg, push the deferred frees 3986789Sahrens * from the previous txg. If not, leave them alone so that we 3987789Sahrens * don't generate work on an otherwise idle system. 3988789Sahrens */ 3989789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 39902329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 39912329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3992789Sahrens spa_sync_deferred_frees(spa, txg); 3993789Sahrens 3994789Sahrens /* 3995789Sahrens * Iterate to convergence. 3996789Sahrens */ 3997789Sahrens do { 3998789Sahrens spa->spa_sync_pass++; 3999789Sahrens 4000789Sahrens spa_sync_config_object(spa, tx); 40015450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 40025450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 40035450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 40045450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 40051544Seschrock spa_errlog_sync(spa, txg); 4006789Sahrens dsl_pool_sync(dp, txg); 4007789Sahrens 4008789Sahrens dirty_vdevs = 0; 4009789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4010789Sahrens vdev_sync(vd, txg); 4011789Sahrens dirty_vdevs++; 4012789Sahrens } 4013789Sahrens 4014789Sahrens bplist_sync(bpl, tx); 4015789Sahrens } while (dirty_vdevs); 4016789Sahrens 4017789Sahrens bplist_close(bpl); 4018789Sahrens 4019789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4020789Sahrens 4021789Sahrens /* 4022789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4023789Sahrens * to commit the transaction group. 40241635Sbonwick * 40255688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 40265688Sbonwick * random top-level vdevs that are known to be visible in the 40275688Sbonwick * config cache (see spa_vdev_add() for details). If there *are* 40285688Sbonwick * dirty vdevs -- or if the sync to our random subset fails -- 40295688Sbonwick * then sync the uberblock to all vdevs. 4030789Sahrens */ 40315688Sbonwick if (list_is_empty(&spa->spa_dirty_list)) { 40326615Sgw25295 vdev_t *svd[SPA_DVAS_PER_BP]; 40336615Sgw25295 int svdcount = 0; 40341635Sbonwick int children = rvd->vdev_children; 40351635Sbonwick int c0 = spa_get_random(children); 40361635Sbonwick int c; 40371635Sbonwick 40381635Sbonwick for (c = 0; c < children; c++) { 40391635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 40405688Sbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 40411635Sbonwick continue; 40425688Sbonwick svd[svdcount++] = vd; 40435688Sbonwick if (svdcount == SPA_DVAS_PER_BP) 40441635Sbonwick break; 40451635Sbonwick } 40466615Sgw25295 vdev_config_sync(svd, svdcount, txg); 40476615Sgw25295 } else { 40486615Sgw25295 vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg); 40491635Sbonwick } 40502082Seschrock dmu_tx_commit(tx); 40512082Seschrock 40521635Sbonwick /* 40531635Sbonwick * Clear the dirty config list. 40541635Sbonwick */ 40551635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 40561635Sbonwick vdev_config_clean(vd); 40571635Sbonwick 40581635Sbonwick /* 40591635Sbonwick * Now that the new config has synced transactionally, 40601635Sbonwick * let it become visible to the config cache. 40611635Sbonwick */ 40621635Sbonwick if (spa->spa_config_syncing != NULL) { 40631635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 40641635Sbonwick spa->spa_config_txg = txg; 40651635Sbonwick spa->spa_config_syncing = NULL; 40661635Sbonwick } 4067789Sahrens 40687046Sahrens spa->spa_traverse_wanted = B_TRUE; 4069789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 40707046Sahrens spa->spa_traverse_wanted = B_FALSE; 4071789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4072789Sahrens rw_exit(&spa->spa_traverse_lock); 4073789Sahrens 4074789Sahrens /* 4075789Sahrens * Clean up the ZIL records for the synced txg. 4076789Sahrens */ 4077789Sahrens dsl_pool_zil_clean(dp); 4078789Sahrens 4079789Sahrens /* 4080789Sahrens * Update usable space statistics. 4081789Sahrens */ 4082789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4083789Sahrens vdev_sync_done(vd, txg); 4084789Sahrens 4085789Sahrens /* 4086789Sahrens * It had better be the case that we didn't dirty anything 40872082Seschrock * since vdev_config_sync(). 4088789Sahrens */ 4089789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4090789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4091789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4092789Sahrens ASSERT(bpl->bpl_queue == NULL); 4093789Sahrens 40941544Seschrock spa_config_exit(spa, FTAG); 40951544Seschrock 40961544Seschrock /* 40971544Seschrock * If any async tasks have been requested, kick them off. 40981544Seschrock */ 40991544Seschrock spa_async_dispatch(spa); 4100789Sahrens } 4101789Sahrens 4102789Sahrens /* 4103789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4104789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4105789Sahrens * sync. 4106789Sahrens */ 4107789Sahrens void 4108789Sahrens spa_sync_allpools(void) 4109789Sahrens { 4110789Sahrens spa_t *spa = NULL; 4111789Sahrens mutex_enter(&spa_namespace_lock); 4112789Sahrens while ((spa = spa_next(spa)) != NULL) { 4113789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 4114789Sahrens continue; 4115789Sahrens spa_open_ref(spa, FTAG); 4116789Sahrens mutex_exit(&spa_namespace_lock); 4117789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4118789Sahrens mutex_enter(&spa_namespace_lock); 4119789Sahrens spa_close(spa, FTAG); 4120789Sahrens } 4121789Sahrens mutex_exit(&spa_namespace_lock); 4122789Sahrens } 4123789Sahrens 4124789Sahrens /* 4125789Sahrens * ========================================================================== 4126789Sahrens * Miscellaneous routines 4127789Sahrens * ========================================================================== 4128789Sahrens */ 4129789Sahrens 4130789Sahrens /* 4131789Sahrens * Remove all pools in the system. 4132789Sahrens */ 4133789Sahrens void 4134789Sahrens spa_evict_all(void) 4135789Sahrens { 4136789Sahrens spa_t *spa; 4137789Sahrens 4138789Sahrens /* 4139789Sahrens * Remove all cached state. All pools should be closed now, 4140789Sahrens * so every spa in the AVL tree should be unreferenced. 4141789Sahrens */ 4142789Sahrens mutex_enter(&spa_namespace_lock); 4143789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4144789Sahrens /* 41451544Seschrock * Stop async tasks. The async thread may need to detach 41461544Seschrock * a device that's been replaced, which requires grabbing 41471544Seschrock * spa_namespace_lock, so we must drop it here. 4148789Sahrens */ 4149789Sahrens spa_open_ref(spa, FTAG); 4150789Sahrens mutex_exit(&spa_namespace_lock); 41511544Seschrock spa_async_suspend(spa); 41524808Sek110237 mutex_enter(&spa_namespace_lock); 4153789Sahrens spa_close(spa, FTAG); 4154789Sahrens 4155789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4156789Sahrens spa_unload(spa); 4157789Sahrens spa_deactivate(spa); 4158789Sahrens } 4159789Sahrens spa_remove(spa); 4160789Sahrens } 4161789Sahrens mutex_exit(&spa_namespace_lock); 4162789Sahrens } 41631544Seschrock 41641544Seschrock vdev_t * 41656643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 41661544Seschrock { 41676643Seschrock vdev_t *vd; 41686643Seschrock int i; 41696643Seschrock 41706643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 41716643Seschrock return (vd); 41726643Seschrock 41736643Seschrock if (l2cache) { 41746643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 41756643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 41766643Seschrock if (vd->vdev_guid == guid) 41776643Seschrock return (vd); 41786643Seschrock } 41796643Seschrock } 41806643Seschrock 41816643Seschrock return (NULL); 41821544Seschrock } 41831760Seschrock 41841760Seschrock void 41855094Slling spa_upgrade(spa_t *spa, uint64_t version) 41861760Seschrock { 41871760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 41881760Seschrock 41891760Seschrock /* 41901760Seschrock * This should only be called for a non-faulted pool, and since a 41911760Seschrock * future version would result in an unopenable pool, this shouldn't be 41921760Seschrock * possible. 41931760Seschrock */ 41944577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 41955094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 41965094Slling 41975094Slling spa->spa_uberblock.ub_version = version; 41981760Seschrock vdev_config_dirty(spa->spa_root_vdev); 41991760Seschrock 42001760Seschrock spa_config_exit(spa, FTAG); 42012082Seschrock 42022082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 42031760Seschrock } 42042082Seschrock 42052082Seschrock boolean_t 42062082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 42072082Seschrock { 42082082Seschrock int i; 42093377Seschrock uint64_t spareguid; 42105450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 42115450Sbrendan 42125450Sbrendan for (i = 0; i < sav->sav_count; i++) 42135450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 42142082Seschrock return (B_TRUE); 42152082Seschrock 42165450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 42175450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 42185450Sbrendan &spareguid) == 0 && spareguid == guid) 42193377Seschrock return (B_TRUE); 42203377Seschrock } 42213377Seschrock 42222082Seschrock return (B_FALSE); 42232082Seschrock } 42243912Slling 42254451Seschrock /* 42267214Slling * Check if a pool has an active shared spare device. 42277214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 42287214Slling */ 42297214Slling static boolean_t 42307214Slling spa_has_active_shared_spare(spa_t *spa) 42317214Slling { 42327214Slling int i, refcnt; 42337214Slling uint64_t pool; 42347214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 42357214Slling 42367214Slling for (i = 0; i < sav->sav_count; i++) { 42377214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 42387214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 42397214Slling refcnt > 2) 42407214Slling return (B_TRUE); 42417214Slling } 42427214Slling 42437214Slling return (B_FALSE); 42447214Slling } 42457214Slling 42467214Slling /* 42474451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 42484451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 42494451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 42504451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 42514451Seschrock * or zdb as real changes. 42524451Seschrock */ 42534451Seschrock void 42544451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 42554451Seschrock { 42564451Seschrock #ifdef _KERNEL 42574451Seschrock sysevent_t *ev; 42584451Seschrock sysevent_attr_list_t *attr = NULL; 42594451Seschrock sysevent_value_t value; 42604451Seschrock sysevent_id_t eid; 42614451Seschrock 42624451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 42634451Seschrock SE_SLEEP); 42644451Seschrock 42654451Seschrock value.value_type = SE_DATA_TYPE_STRING; 42664451Seschrock value.value.sv_string = spa_name(spa); 42674451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 42684451Seschrock goto done; 42694451Seschrock 42704451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 42714451Seschrock value.value.sv_uint64 = spa_guid(spa); 42724451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 42734451Seschrock goto done; 42744451Seschrock 42754451Seschrock if (vd) { 42764451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 42774451Seschrock value.value.sv_uint64 = vd->vdev_guid; 42784451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 42794451Seschrock SE_SLEEP) != 0) 42804451Seschrock goto done; 42814451Seschrock 42824451Seschrock if (vd->vdev_path) { 42834451Seschrock value.value_type = SE_DATA_TYPE_STRING; 42844451Seschrock value.value.sv_string = vd->vdev_path; 42854451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 42864451Seschrock &value, SE_SLEEP) != 0) 42874451Seschrock goto done; 42884451Seschrock } 42894451Seschrock } 42904451Seschrock 42915756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 42925756Seschrock goto done; 42935756Seschrock attr = NULL; 42945756Seschrock 42954451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 42964451Seschrock 42974451Seschrock done: 42984451Seschrock if (attr) 42994451Seschrock sysevent_free_attr(attr); 43004451Seschrock sysevent_free(ev); 43014451Seschrock #endif 43024451Seschrock } 4303