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: 2915094Slling error = nvpair_value_uint64(elem, &intval); 2925094Slling if (!error && intval > 1) 2935094Slling error = EINVAL; 2945094Slling break; 2955094Slling 2965094Slling case ZPOOL_PROP_BOOTFS: 2975094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 2985094Slling error = ENOTSUP; 2995094Slling break; 3005094Slling } 3015094Slling 3025094Slling /* 3037042Sgw25295 * Make sure the vdev config is bootable 3045094Slling */ 3057042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3065094Slling error = ENOTSUP; 3075094Slling break; 3085094Slling } 3095094Slling 3105094Slling reset_bootfs = 1; 3115094Slling 3125094Slling error = nvpair_value_string(elem, &strval); 3135094Slling 3145094Slling if (!error) { 3157042Sgw25295 uint64_t compress; 3167042Sgw25295 3175094Slling if (strval == NULL || strval[0] == '\0') { 3185094Slling objnum = zpool_prop_default_numeric( 3195094Slling ZPOOL_PROP_BOOTFS); 3205094Slling break; 3215094Slling } 3225094Slling 3235094Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 3246689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os)) 3255094Slling break; 3267042Sgw25295 3277042Sgw25295 /* We don't support gzip bootable datasets */ 3287042Sgw25295 if ((error = dsl_prop_get_integer(strval, 3297042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3307042Sgw25295 &compress, NULL)) == 0 && 3317042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3327042Sgw25295 error = ENOTSUP; 3337042Sgw25295 } else { 3347042Sgw25295 objnum = dmu_objset_id(os); 3357042Sgw25295 } 3365094Slling dmu_objset_close(os); 3375094Slling } 3385094Slling break; 3395329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3405329Sgw25295 error = nvpair_value_uint64(elem, &intval); 3415329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3425329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 3435329Sgw25295 error = EINVAL; 3445329Sgw25295 3455329Sgw25295 /* 3465329Sgw25295 * This is a special case which only occurs when 3475329Sgw25295 * the pool has completely failed. This allows 3485329Sgw25295 * the user to change the in-core failmode property 3495329Sgw25295 * without syncing it out to disk (I/Os might 3505329Sgw25295 * currently be blocked). We do this by returning 3515329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 3525329Sgw25295 * into thinking we encountered a property validation 3535329Sgw25295 * error. 3545329Sgw25295 */ 3555329Sgw25295 if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) { 3565329Sgw25295 spa->spa_failmode = intval; 3575329Sgw25295 error = EIO; 3585329Sgw25295 } 3595329Sgw25295 break; 3605363Seschrock 3615363Seschrock case ZPOOL_PROP_CACHEFILE: 3625363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3635363Seschrock break; 3645363Seschrock 3655363Seschrock if (strval[0] == '\0') 3665363Seschrock break; 3675363Seschrock 3685363Seschrock if (strcmp(strval, "none") == 0) 3695363Seschrock break; 3705363Seschrock 3715363Seschrock if (strval[0] != '/') { 3725363Seschrock error = EINVAL; 3735363Seschrock break; 3745363Seschrock } 3755363Seschrock 3765363Seschrock slash = strrchr(strval, '/'); 3775363Seschrock ASSERT(slash != NULL); 3785363Seschrock 3795363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3805363Seschrock strcmp(slash, "/..") == 0) 3815363Seschrock error = EINVAL; 3825363Seschrock break; 3835094Slling } 3845094Slling 3855094Slling if (error) 3865094Slling break; 3875094Slling } 3885094Slling 3895094Slling if (!error && reset_bootfs) { 3905094Slling error = nvlist_remove(props, 3915094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 3925094Slling 3935094Slling if (!error) { 3945094Slling error = nvlist_add_uint64(props, 3955094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 3965094Slling } 3975094Slling } 3985094Slling 3995094Slling return (error); 4005094Slling } 4015094Slling 4025094Slling int 4035094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4045094Slling { 4055094Slling int error; 4065094Slling 4075094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 4085094Slling return (error); 4095094Slling 4105094Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 4115094Slling spa, nvp, 3)); 4125094Slling } 4135094Slling 4145094Slling /* 4155094Slling * If the bootfs property value is dsobj, clear it. 4165094Slling */ 4175094Slling void 4185094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 4195094Slling { 4205094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 4215094Slling VERIFY(zap_remove(spa->spa_meta_objset, 4225094Slling spa->spa_pool_props_object, 4235094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 4245094Slling spa->spa_bootfs = 0; 4255094Slling } 4265094Slling } 4275094Slling 428789Sahrens /* 429789Sahrens * ========================================================================== 430789Sahrens * SPA state manipulation (open/create/destroy/import/export) 431789Sahrens * ========================================================================== 432789Sahrens */ 433789Sahrens 4341544Seschrock static int 4351544Seschrock spa_error_entry_compare(const void *a, const void *b) 4361544Seschrock { 4371544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 4381544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 4391544Seschrock int ret; 4401544Seschrock 4411544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 4421544Seschrock sizeof (zbookmark_t)); 4431544Seschrock 4441544Seschrock if (ret < 0) 4451544Seschrock return (-1); 4461544Seschrock else if (ret > 0) 4471544Seschrock return (1); 4481544Seschrock else 4491544Seschrock return (0); 4501544Seschrock } 4511544Seschrock 4521544Seschrock /* 4531544Seschrock * Utility function which retrieves copies of the current logs and 4541544Seschrock * re-initializes them in the process. 4551544Seschrock */ 4561544Seschrock void 4571544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 4581544Seschrock { 4591544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 4601544Seschrock 4611544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 4621544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 4631544Seschrock 4641544Seschrock avl_create(&spa->spa_errlist_scrub, 4651544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4661544Seschrock offsetof(spa_error_entry_t, se_avl)); 4671544Seschrock avl_create(&spa->spa_errlist_last, 4681544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4691544Seschrock offsetof(spa_error_entry_t, se_avl)); 4701544Seschrock } 4711544Seschrock 472789Sahrens /* 473789Sahrens * Activate an uninitialized pool. 474789Sahrens */ 475789Sahrens static void 476789Sahrens spa_activate(spa_t *spa) 477789Sahrens { 478789Sahrens int t; 479789Sahrens 480789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 481789Sahrens 482789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 483789Sahrens 484789Sahrens spa->spa_normal_class = metaslab_class_create(); 4854527Sperrin spa->spa_log_class = metaslab_class_create(); 486789Sahrens 487789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 488789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 4892986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 490789Sahrens TASKQ_PREPOPULATE); 491789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 4922986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 493789Sahrens TASKQ_PREPOPULATE); 494789Sahrens } 495789Sahrens 496789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 497789Sahrens offsetof(vdev_t, vdev_dirty_node)); 4985329Sgw25295 list_create(&spa->spa_zio_list, sizeof (zio_t), 4995329Sgw25295 offsetof(zio_t, zio_link_node)); 500789Sahrens 501789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 502789Sahrens offsetof(struct vdev, vdev_txg_node)); 5031544Seschrock 5041544Seschrock avl_create(&spa->spa_errlist_scrub, 5051544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5061544Seschrock offsetof(spa_error_entry_t, se_avl)); 5071544Seschrock avl_create(&spa->spa_errlist_last, 5081544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5091544Seschrock offsetof(spa_error_entry_t, se_avl)); 510789Sahrens } 511789Sahrens 512789Sahrens /* 513789Sahrens * Opposite of spa_activate(). 514789Sahrens */ 515789Sahrens static void 516789Sahrens spa_deactivate(spa_t *spa) 517789Sahrens { 518789Sahrens int t; 519789Sahrens 520789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 521789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 522789Sahrens ASSERT(spa->spa_root_vdev == NULL); 523789Sahrens 524789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 525789Sahrens 526789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 527789Sahrens 528789Sahrens list_destroy(&spa->spa_dirty_list); 5295329Sgw25295 list_destroy(&spa->spa_zio_list); 530789Sahrens 531789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 532789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 533789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 534789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 535789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 536789Sahrens } 537789Sahrens 538789Sahrens metaslab_class_destroy(spa->spa_normal_class); 539789Sahrens spa->spa_normal_class = NULL; 540789Sahrens 5414527Sperrin metaslab_class_destroy(spa->spa_log_class); 5424527Sperrin spa->spa_log_class = NULL; 5434527Sperrin 5441544Seschrock /* 5451544Seschrock * If this was part of an import or the open otherwise failed, we may 5461544Seschrock * still have errors left in the queues. Empty them just in case. 5471544Seschrock */ 5481544Seschrock spa_errlog_drain(spa); 5491544Seschrock 5501544Seschrock avl_destroy(&spa->spa_errlist_scrub); 5511544Seschrock avl_destroy(&spa->spa_errlist_last); 5521544Seschrock 553789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 554789Sahrens } 555789Sahrens 556789Sahrens /* 557789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 558789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 559789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 560789Sahrens * All vdev validation is done by the vdev_alloc() routine. 561789Sahrens */ 5622082Seschrock static int 5632082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 5642082Seschrock uint_t id, int atype) 565789Sahrens { 566789Sahrens nvlist_t **child; 567789Sahrens uint_t c, children; 5682082Seschrock int error; 5692082Seschrock 5702082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 5712082Seschrock return (error); 5722082Seschrock 5732082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 5742082Seschrock return (0); 575789Sahrens 576789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 577789Sahrens &child, &children) != 0) { 5782082Seschrock vdev_free(*vdp); 5792082Seschrock *vdp = NULL; 5802082Seschrock return (EINVAL); 581789Sahrens } 582789Sahrens 583789Sahrens for (c = 0; c < children; c++) { 5842082Seschrock vdev_t *vd; 5852082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 5862082Seschrock atype)) != 0) { 5872082Seschrock vdev_free(*vdp); 5882082Seschrock *vdp = NULL; 5892082Seschrock return (error); 590789Sahrens } 591789Sahrens } 592789Sahrens 5932082Seschrock ASSERT(*vdp != NULL); 5942082Seschrock 5952082Seschrock return (0); 596789Sahrens } 597789Sahrens 598789Sahrens /* 599789Sahrens * Opposite of spa_load(). 600789Sahrens */ 601789Sahrens static void 602789Sahrens spa_unload(spa_t *spa) 603789Sahrens { 6042082Seschrock int i; 6052082Seschrock 606789Sahrens /* 6071544Seschrock * Stop async tasks. 6081544Seschrock */ 6091544Seschrock spa_async_suspend(spa); 6101544Seschrock 6111544Seschrock /* 612789Sahrens * Stop syncing. 613789Sahrens */ 614789Sahrens if (spa->spa_sync_on) { 615789Sahrens txg_sync_stop(spa->spa_dsl_pool); 616789Sahrens spa->spa_sync_on = B_FALSE; 617789Sahrens } 618789Sahrens 619789Sahrens /* 620789Sahrens * Wait for any outstanding prefetch I/O to complete. 621789Sahrens */ 6221544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 6231544Seschrock spa_config_exit(spa, FTAG); 624789Sahrens 625789Sahrens /* 6265450Sbrendan * Drop and purge level 2 cache 6275450Sbrendan */ 6285450Sbrendan spa_l2cache_drop(spa); 6295450Sbrendan 6305450Sbrendan /* 631789Sahrens * Close the dsl pool. 632789Sahrens */ 633789Sahrens if (spa->spa_dsl_pool) { 634789Sahrens dsl_pool_close(spa->spa_dsl_pool); 635789Sahrens spa->spa_dsl_pool = NULL; 636789Sahrens } 637789Sahrens 638789Sahrens /* 639789Sahrens * Close all vdevs. 640789Sahrens */ 6411585Sbonwick if (spa->spa_root_vdev) 642789Sahrens vdev_free(spa->spa_root_vdev); 6431585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 6441544Seschrock 6455450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 6465450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 6475450Sbrendan if (spa->spa_spares.sav_vdevs) { 6485450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 6495450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 6505450Sbrendan spa->spa_spares.sav_vdevs = NULL; 6515450Sbrendan } 6525450Sbrendan if (spa->spa_spares.sav_config) { 6535450Sbrendan nvlist_free(spa->spa_spares.sav_config); 6545450Sbrendan spa->spa_spares.sav_config = NULL; 6552082Seschrock } 6567377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 6575450Sbrendan 6585450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 6595450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 6605450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 6615450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 6625450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 6635450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 6645450Sbrendan } 6655450Sbrendan if (spa->spa_l2cache.sav_config) { 6665450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 6675450Sbrendan spa->spa_l2cache.sav_config = NULL; 6682082Seschrock } 6697377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 6702082Seschrock 6711544Seschrock spa->spa_async_suspended = 0; 672789Sahrens } 673789Sahrens 674789Sahrens /* 6752082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 6762082Seschrock * this pool. When this is called, we have some form of basic information in 6775450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 6785450Sbrendan * then re-generate a more complete list including status information. 6792082Seschrock */ 6802082Seschrock static void 6812082Seschrock spa_load_spares(spa_t *spa) 6822082Seschrock { 6832082Seschrock nvlist_t **spares; 6842082Seschrock uint_t nspares; 6852082Seschrock int i; 6863377Seschrock vdev_t *vd, *tvd; 6872082Seschrock 6882082Seschrock /* 6892082Seschrock * First, close and free any existing spare vdevs. 6902082Seschrock */ 6915450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 6925450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 6933377Seschrock 6943377Seschrock /* Undo the call to spa_activate() below */ 6956643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 6966643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 6973377Seschrock spa_spare_remove(tvd); 6983377Seschrock vdev_close(vd); 6993377Seschrock vdev_free(vd); 7002082Seschrock } 7013377Seschrock 7025450Sbrendan if (spa->spa_spares.sav_vdevs) 7035450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7045450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7055450Sbrendan 7065450Sbrendan if (spa->spa_spares.sav_config == NULL) 7072082Seschrock nspares = 0; 7082082Seschrock else 7095450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 7102082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 7112082Seschrock 7125450Sbrendan spa->spa_spares.sav_count = (int)nspares; 7135450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7142082Seschrock 7152082Seschrock if (nspares == 0) 7162082Seschrock return; 7172082Seschrock 7182082Seschrock /* 7192082Seschrock * Construct the array of vdevs, opening them to get status in the 7203377Seschrock * process. For each spare, there is potentially two different vdev_t 7213377Seschrock * structures associated with it: one in the list of spares (used only 7223377Seschrock * for basic validation purposes) and one in the active vdev 7233377Seschrock * configuration (if it's spared in). During this phase we open and 7243377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 7253377Seschrock * active configuration, then we also mark this vdev as an active spare. 7262082Seschrock */ 7275450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 7285450Sbrendan KM_SLEEP); 7295450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7302082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 7312082Seschrock VDEV_ALLOC_SPARE) == 0); 7322082Seschrock ASSERT(vd != NULL); 7332082Seschrock 7345450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 7352082Seschrock 7366643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7376643Seschrock B_FALSE)) != NULL) { 7383377Seschrock if (!tvd->vdev_isspare) 7393377Seschrock spa_spare_add(tvd); 7403377Seschrock 7413377Seschrock /* 7423377Seschrock * We only mark the spare active if we were successfully 7433377Seschrock * able to load the vdev. Otherwise, importing a pool 7443377Seschrock * with a bad active spare would result in strange 7453377Seschrock * behavior, because multiple pool would think the spare 7463377Seschrock * is actively in use. 7473377Seschrock * 7483377Seschrock * There is a vulnerability here to an equally bizarre 7493377Seschrock * circumstance, where a dead active spare is later 7503377Seschrock * brought back to life (onlined or otherwise). Given 7513377Seschrock * the rarity of this scenario, and the extra complexity 7523377Seschrock * it adds, we ignore the possibility. 7533377Seschrock */ 7543377Seschrock if (!vdev_is_dead(tvd)) 7553377Seschrock spa_spare_activate(tvd); 7563377Seschrock } 7573377Seschrock 7582082Seschrock if (vdev_open(vd) != 0) 7592082Seschrock continue; 7602082Seschrock 7612082Seschrock vd->vdev_top = vd; 7625450Sbrendan if (vdev_validate_aux(vd) == 0) 7635450Sbrendan spa_spare_add(vd); 7642082Seschrock } 7652082Seschrock 7662082Seschrock /* 7672082Seschrock * Recompute the stashed list of spares, with status information 7682082Seschrock * this time. 7692082Seschrock */ 7705450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 7712082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 7722082Seschrock 7735450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 7745450Sbrendan KM_SLEEP); 7755450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7765450Sbrendan spares[i] = vdev_config_generate(spa, 7775450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 7785450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 7795450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 7805450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7812082Seschrock nvlist_free(spares[i]); 7825450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 7835450Sbrendan } 7845450Sbrendan 7855450Sbrendan /* 7865450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 7875450Sbrendan * this pool. When this is called, we have some form of basic information in 7885450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 7895450Sbrendan * then re-generate a more complete list including status information. 7905450Sbrendan * Devices which are already active have their details maintained, and are 7915450Sbrendan * not re-opened. 7925450Sbrendan */ 7935450Sbrendan static void 7945450Sbrendan spa_load_l2cache(spa_t *spa) 7955450Sbrendan { 7965450Sbrendan nvlist_t **l2cache; 7975450Sbrendan uint_t nl2cache; 7985450Sbrendan int i, j, oldnvdevs; 7996643Seschrock uint64_t guid, size; 8005450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 8015450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 8025450Sbrendan 8035450Sbrendan if (sav->sav_config != NULL) { 8045450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 8055450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 8065450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 8075450Sbrendan } else { 8085450Sbrendan nl2cache = 0; 8095450Sbrendan } 8105450Sbrendan 8115450Sbrendan oldvdevs = sav->sav_vdevs; 8125450Sbrendan oldnvdevs = sav->sav_count; 8135450Sbrendan sav->sav_vdevs = NULL; 8145450Sbrendan sav->sav_count = 0; 8155450Sbrendan 8165450Sbrendan /* 8175450Sbrendan * Process new nvlist of vdevs. 8185450Sbrendan */ 8195450Sbrendan for (i = 0; i < nl2cache; i++) { 8205450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 8215450Sbrendan &guid) == 0); 8225450Sbrendan 8235450Sbrendan newvdevs[i] = NULL; 8245450Sbrendan for (j = 0; j < oldnvdevs; j++) { 8255450Sbrendan vd = oldvdevs[j]; 8265450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 8275450Sbrendan /* 8285450Sbrendan * Retain previous vdev for add/remove ops. 8295450Sbrendan */ 8305450Sbrendan newvdevs[i] = vd; 8315450Sbrendan oldvdevs[j] = NULL; 8325450Sbrendan break; 8335450Sbrendan } 8345450Sbrendan } 8355450Sbrendan 8365450Sbrendan if (newvdevs[i] == NULL) { 8375450Sbrendan /* 8385450Sbrendan * Create new vdev 8395450Sbrendan */ 8405450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 8415450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 8425450Sbrendan ASSERT(vd != NULL); 8435450Sbrendan newvdevs[i] = vd; 8445450Sbrendan 8455450Sbrendan /* 8465450Sbrendan * Commit this vdev as an l2cache device, 8475450Sbrendan * even if it fails to open. 8485450Sbrendan */ 8495450Sbrendan spa_l2cache_add(vd); 8505450Sbrendan 8516643Seschrock vd->vdev_top = vd; 8526643Seschrock vd->vdev_aux = sav; 8536643Seschrock 8546643Seschrock spa_l2cache_activate(vd); 8556643Seschrock 8565450Sbrendan if (vdev_open(vd) != 0) 8575450Sbrendan continue; 8585450Sbrendan 8595450Sbrendan (void) vdev_validate_aux(vd); 8605450Sbrendan 8615450Sbrendan if (!vdev_is_dead(vd)) { 8625450Sbrendan size = vdev_get_rsize(vd); 8636643Seschrock l2arc_add_vdev(spa, vd, 8646643Seschrock VDEV_LABEL_START_SIZE, 8656643Seschrock size - VDEV_LABEL_START_SIZE); 8665450Sbrendan } 8675450Sbrendan } 8685450Sbrendan } 8695450Sbrendan 8705450Sbrendan /* 8715450Sbrendan * Purge vdevs that were dropped 8725450Sbrendan */ 8735450Sbrendan for (i = 0; i < oldnvdevs; i++) { 8745450Sbrendan uint64_t pool; 8755450Sbrendan 8765450Sbrendan vd = oldvdevs[i]; 8775450Sbrendan if (vd != NULL) { 8785450Sbrendan if (spa_mode & FWRITE && 8795450Sbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && 8806643Seschrock pool != 0ULL && 8816643Seschrock l2arc_vdev_present(vd)) { 8825450Sbrendan l2arc_remove_vdev(vd); 8835450Sbrendan } 8845450Sbrendan (void) vdev_close(vd); 8855450Sbrendan spa_l2cache_remove(vd); 8865450Sbrendan } 8875450Sbrendan } 8885450Sbrendan 8895450Sbrendan if (oldvdevs) 8905450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 8915450Sbrendan 8925450Sbrendan if (sav->sav_config == NULL) 8935450Sbrendan goto out; 8945450Sbrendan 8955450Sbrendan sav->sav_vdevs = newvdevs; 8965450Sbrendan sav->sav_count = (int)nl2cache; 8975450Sbrendan 8985450Sbrendan /* 8995450Sbrendan * Recompute the stashed list of l2cache devices, with status 9005450Sbrendan * information this time. 9015450Sbrendan */ 9025450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 9035450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 9045450Sbrendan 9055450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 9065450Sbrendan for (i = 0; i < sav->sav_count; i++) 9075450Sbrendan l2cache[i] = vdev_config_generate(spa, 9085450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 9095450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 9105450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 9115450Sbrendan out: 9125450Sbrendan for (i = 0; i < sav->sav_count; i++) 9135450Sbrendan nvlist_free(l2cache[i]); 9145450Sbrendan if (sav->sav_count) 9155450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 9162082Seschrock } 9172082Seschrock 9182082Seschrock static int 9192082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 9202082Seschrock { 9212082Seschrock dmu_buf_t *db; 9222082Seschrock char *packed = NULL; 9232082Seschrock size_t nvsize = 0; 9242082Seschrock int error; 9252082Seschrock *value = NULL; 9262082Seschrock 9272082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 9282082Seschrock nvsize = *(uint64_t *)db->db_data; 9292082Seschrock dmu_buf_rele(db, FTAG); 9302082Seschrock 9312082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 9322082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 9332082Seschrock if (error == 0) 9342082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 9352082Seschrock kmem_free(packed, nvsize); 9362082Seschrock 9372082Seschrock return (error); 9382082Seschrock } 9392082Seschrock 9402082Seschrock /* 9414451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9424451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9434451Seschrock */ 9444451Seschrock static void 9454451Seschrock spa_check_removed(vdev_t *vd) 9464451Seschrock { 9474451Seschrock int c; 9484451Seschrock 9494451Seschrock for (c = 0; c < vd->vdev_children; c++) 9504451Seschrock spa_check_removed(vd->vdev_child[c]); 9514451Seschrock 9524451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9534451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9544451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9554451Seschrock } 9564451Seschrock } 9574451Seschrock 9584451Seschrock /* 9597294Sperrin * Check for missing log devices 9607294Sperrin */ 9617294Sperrin int 9627294Sperrin spa_check_logs(spa_t *spa) 9637294Sperrin { 9647294Sperrin switch (spa->spa_log_state) { 9657294Sperrin case SPA_LOG_MISSING: 9667294Sperrin /* need to recheck in case slog has been restored */ 9677294Sperrin case SPA_LOG_UNKNOWN: 9687294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 9697294Sperrin DS_FIND_CHILDREN)) { 9707294Sperrin spa->spa_log_state = SPA_LOG_MISSING; 9717294Sperrin return (1); 9727294Sperrin } 9737294Sperrin break; 9747294Sperrin 9757294Sperrin case SPA_LOG_CLEAR: 9767294Sperrin (void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL, 9777294Sperrin DS_FIND_CHILDREN); 9787294Sperrin break; 9797294Sperrin } 9807294Sperrin spa->spa_log_state = SPA_LOG_GOOD; 9817294Sperrin return (0); 9827294Sperrin } 9837294Sperrin 9847294Sperrin /* 985789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 9861544Seschrock * source of configuration information. 987789Sahrens */ 988789Sahrens static int 9891544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 990789Sahrens { 991789Sahrens int error = 0; 992789Sahrens nvlist_t *nvroot = NULL; 993789Sahrens vdev_t *rvd; 994789Sahrens uberblock_t *ub = &spa->spa_uberblock; 9951635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 996789Sahrens uint64_t pool_guid; 9972082Seschrock uint64_t version; 998789Sahrens zio_t *zio; 9994451Seschrock uint64_t autoreplace = 0; 10007294Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 1001789Sahrens 10021544Seschrock spa->spa_load_state = state; 10031635Sbonwick 1004789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 10051733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 10061544Seschrock error = EINVAL; 10071544Seschrock goto out; 10081544Seschrock } 1009789Sahrens 10102082Seschrock /* 10112082Seschrock * Versioning wasn't explicitly added to the label until later, so if 10122082Seschrock * it's not present treat it as the initial version. 10132082Seschrock */ 10142082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 10154577Sahrens version = SPA_VERSION_INITIAL; 10162082Seschrock 10171733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 10181733Sbonwick &spa->spa_config_txg); 10191733Sbonwick 10201635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 10211544Seschrock spa_guid_exists(pool_guid, 0)) { 10221544Seschrock error = EEXIST; 10231544Seschrock goto out; 10241544Seschrock } 1025789Sahrens 10262174Seschrock spa->spa_load_guid = pool_guid; 10272174Seschrock 1028789Sahrens /* 10292082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 10302082Seschrock * value that will be returned by spa_version() since parsing the 10312082Seschrock * configuration requires knowing the version number. 1032789Sahrens */ 10331544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 10342082Seschrock spa->spa_ubsync.ub_version = version; 10352082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 10361544Seschrock spa_config_exit(spa, FTAG); 1037789Sahrens 10382082Seschrock if (error != 0) 10391544Seschrock goto out; 1040789Sahrens 10411585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1042789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1043789Sahrens 1044789Sahrens /* 1045789Sahrens * Try to open all vdevs, loading each label in the process. 1046789Sahrens */ 10474070Smc142369 error = vdev_open(rvd); 10484070Smc142369 if (error != 0) 10491544Seschrock goto out; 1050789Sahrens 1051789Sahrens /* 10521986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 10531986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 10541986Seschrock * flag. 10551986Seschrock */ 10561986Seschrock spa_config_enter(spa, RW_READER, FTAG); 10571986Seschrock error = vdev_validate(rvd); 10581986Seschrock spa_config_exit(spa, FTAG); 10591986Seschrock 10604070Smc142369 if (error != 0) 10611986Seschrock goto out; 10621986Seschrock 10631986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 10641986Seschrock error = ENXIO; 10651986Seschrock goto out; 10661986Seschrock } 10671986Seschrock 10681986Seschrock /* 1069789Sahrens * Find the best uberblock. 1070789Sahrens */ 1071789Sahrens bzero(ub, sizeof (uberblock_t)); 1072789Sahrens 1073789Sahrens zio = zio_root(spa, NULL, NULL, 1074789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1075789Sahrens vdev_uberblock_load(zio, rvd, ub); 1076789Sahrens error = zio_wait(zio); 1077789Sahrens 1078789Sahrens /* 1079789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1080789Sahrens */ 1081789Sahrens if (ub->ub_txg == 0) { 10821760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10831760Seschrock VDEV_AUX_CORRUPT_DATA); 10841544Seschrock error = ENXIO; 10851544Seschrock goto out; 10861544Seschrock } 10871544Seschrock 10881544Seschrock /* 10891544Seschrock * If the pool is newer than the code, we can't open it. 10901544Seschrock */ 10914577Sahrens if (ub->ub_version > SPA_VERSION) { 10921760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10931760Seschrock VDEV_AUX_VERSION_NEWER); 10941544Seschrock error = ENOTSUP; 10951544Seschrock goto out; 1096789Sahrens } 1097789Sahrens 1098789Sahrens /* 1099789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1100789Sahrens * incomplete configuration. 1101789Sahrens */ 11021732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 11031544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11041544Seschrock VDEV_AUX_BAD_GUID_SUM); 11051544Seschrock error = ENXIO; 11061544Seschrock goto out; 1107789Sahrens } 1108789Sahrens 1109789Sahrens /* 1110789Sahrens * Initialize internal SPA structures. 1111789Sahrens */ 1112789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1113789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1114789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 11151544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 11161544Seschrock if (error) { 11171544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11181544Seschrock VDEV_AUX_CORRUPT_DATA); 11191544Seschrock goto out; 11201544Seschrock } 1121789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1122789Sahrens 11231544Seschrock if (zap_lookup(spa->spa_meta_objset, 1124789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 11251544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 11261544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11271544Seschrock VDEV_AUX_CORRUPT_DATA); 11281544Seschrock error = EIO; 11291544Seschrock goto out; 11301544Seschrock } 1131789Sahrens 1132789Sahrens if (!mosconfig) { 11332082Seschrock nvlist_t *newconfig; 11343975Sek110237 uint64_t hostid; 11352082Seschrock 11362082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 11371544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11381544Seschrock VDEV_AUX_CORRUPT_DATA); 11391544Seschrock error = EIO; 11401544Seschrock goto out; 11411544Seschrock } 1142789Sahrens 11433975Sek110237 if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 11443975Sek110237 &hostid) == 0) { 11453975Sek110237 char *hostname; 11463975Sek110237 unsigned long myhostid = 0; 11473975Sek110237 11483975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 11493975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 11503975Sek110237 11513975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 11524178Slling if (hostid != 0 && myhostid != 0 && 11534178Slling (unsigned long)hostid != myhostid) { 11543975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 11553975Sek110237 "loaded as it was last accessed by " 11563975Sek110237 "another system (host: %s hostid: 0x%lx). " 11573975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 11583975Sek110237 spa->spa_name, hostname, 11593975Sek110237 (unsigned long)hostid); 11603975Sek110237 error = EBADF; 11613975Sek110237 goto out; 11623975Sek110237 } 11633975Sek110237 } 11643975Sek110237 1165789Sahrens spa_config_set(spa, newconfig); 1166789Sahrens spa_unload(spa); 1167789Sahrens spa_deactivate(spa); 1168789Sahrens spa_activate(spa); 1169789Sahrens 11701544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 11711544Seschrock } 11721544Seschrock 11731544Seschrock if (zap_lookup(spa->spa_meta_objset, 11741544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 11751544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 11761544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11771544Seschrock VDEV_AUX_CORRUPT_DATA); 11781544Seschrock error = EIO; 11791544Seschrock goto out; 1180789Sahrens } 1181789Sahrens 11821544Seschrock /* 11832082Seschrock * Load the bit that tells us to use the new accounting function 11842082Seschrock * (raid-z deflation). If we have an older pool, this will not 11852082Seschrock * be present. 11862082Seschrock */ 11872082Seschrock error = zap_lookup(spa->spa_meta_objset, 11882082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 11892082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 11902082Seschrock if (error != 0 && error != ENOENT) { 11912082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11922082Seschrock VDEV_AUX_CORRUPT_DATA); 11932082Seschrock error = EIO; 11942082Seschrock goto out; 11952082Seschrock } 11962082Seschrock 11972082Seschrock /* 11981544Seschrock * Load the persistent error log. If we have an older pool, this will 11991544Seschrock * not be present. 12001544Seschrock */ 12011544Seschrock error = zap_lookup(spa->spa_meta_objset, 12021544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 12031544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 12041807Sbonwick if (error != 0 && error != ENOENT) { 12051544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12061544Seschrock VDEV_AUX_CORRUPT_DATA); 12071544Seschrock error = EIO; 12081544Seschrock goto out; 12091544Seschrock } 12101544Seschrock 12111544Seschrock error = zap_lookup(spa->spa_meta_objset, 12121544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 12131544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 12141544Seschrock if (error != 0 && error != ENOENT) { 12151544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12161544Seschrock VDEV_AUX_CORRUPT_DATA); 12171544Seschrock error = EIO; 12181544Seschrock goto out; 12191544Seschrock } 1220789Sahrens 1221789Sahrens /* 12222926Sek110237 * Load the history object. If we have an older pool, this 12232926Sek110237 * will not be present. 12242926Sek110237 */ 12252926Sek110237 error = zap_lookup(spa->spa_meta_objset, 12262926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 12272926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 12282926Sek110237 if (error != 0 && error != ENOENT) { 12292926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12302926Sek110237 VDEV_AUX_CORRUPT_DATA); 12312926Sek110237 error = EIO; 12322926Sek110237 goto out; 12332926Sek110237 } 12342926Sek110237 12352926Sek110237 /* 12362082Seschrock * Load any hot spares for this pool. 12372082Seschrock */ 12382082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12395450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 12402082Seschrock if (error != 0 && error != ENOENT) { 12412082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12422082Seschrock VDEV_AUX_CORRUPT_DATA); 12432082Seschrock error = EIO; 12442082Seschrock goto out; 12452082Seschrock } 12462082Seschrock if (error == 0) { 12474577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 12485450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 12495450Sbrendan &spa->spa_spares.sav_config) != 0) { 12502082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12512082Seschrock VDEV_AUX_CORRUPT_DATA); 12522082Seschrock error = EIO; 12532082Seschrock goto out; 12542082Seschrock } 12552082Seschrock 12562082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12572082Seschrock spa_load_spares(spa); 12582082Seschrock spa_config_exit(spa, FTAG); 12592082Seschrock } 12602082Seschrock 12615450Sbrendan /* 12625450Sbrendan * Load any level 2 ARC devices for this pool. 12635450Sbrendan */ 12645450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12655450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 12665450Sbrendan &spa->spa_l2cache.sav_object); 12675450Sbrendan if (error != 0 && error != ENOENT) { 12685450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12695450Sbrendan VDEV_AUX_CORRUPT_DATA); 12705450Sbrendan error = EIO; 12715450Sbrendan goto out; 12725450Sbrendan } 12735450Sbrendan if (error == 0) { 12745450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 12755450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 12765450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 12775450Sbrendan vdev_set_state(rvd, B_TRUE, 12785450Sbrendan VDEV_STATE_CANT_OPEN, 12795450Sbrendan VDEV_AUX_CORRUPT_DATA); 12805450Sbrendan error = EIO; 12815450Sbrendan goto out; 12825450Sbrendan } 12835450Sbrendan 12845450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 12855450Sbrendan spa_load_l2cache(spa); 12865450Sbrendan spa_config_exit(spa, FTAG); 12875450Sbrendan } 12885450Sbrendan 12897294Sperrin if (spa_check_logs(spa)) { 12907294Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12917294Sperrin VDEV_AUX_BAD_LOG); 12927294Sperrin error = ENXIO; 12937294Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 12947294Sperrin goto out; 12957294Sperrin } 12967294Sperrin 12977294Sperrin 12985094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 12994543Smarks 13003912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13013912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 13023912Slling 13033912Slling if (error && error != ENOENT) { 13043912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13053912Slling VDEV_AUX_CORRUPT_DATA); 13063912Slling error = EIO; 13073912Slling goto out; 13083912Slling } 13093912Slling 13103912Slling if (error == 0) { 13113912Slling (void) zap_lookup(spa->spa_meta_objset, 13123912Slling spa->spa_pool_props_object, 13134451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 13143912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 13154451Seschrock (void) zap_lookup(spa->spa_meta_objset, 13164451Seschrock spa->spa_pool_props_object, 13174451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 13184451Seschrock sizeof (uint64_t), 1, &autoreplace); 13194543Smarks (void) zap_lookup(spa->spa_meta_objset, 13204543Smarks spa->spa_pool_props_object, 13214543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 13224543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 13235329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 13245329Sgw25295 spa->spa_pool_props_object, 13255329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 13265329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 13273912Slling } 13283912Slling 13292082Seschrock /* 13304451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 13314451Seschrock * the ZFS DE that it should not issue any faults for unopenable 13324451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 13334451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 13344451Seschrock * over. 13354451Seschrock */ 13365756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 13374451Seschrock spa_check_removed(spa->spa_root_vdev); 13384451Seschrock 13394451Seschrock /* 13401986Seschrock * Load the vdev state for all toplevel vdevs. 1341789Sahrens */ 13421986Seschrock vdev_load(rvd); 1343789Sahrens 1344789Sahrens /* 1345789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1346789Sahrens */ 13471544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1348789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 13491544Seschrock spa_config_exit(spa, FTAG); 1350789Sahrens 1351789Sahrens /* 1352789Sahrens * Check the state of the root vdev. If it can't be opened, it 1353789Sahrens * indicates one or more toplevel vdevs are faulted. 1354789Sahrens */ 13551544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13561544Seschrock error = ENXIO; 13571544Seschrock goto out; 13581544Seschrock } 1359789Sahrens 13601544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 13611635Sbonwick dmu_tx_t *tx; 13621635Sbonwick int need_update = B_FALSE; 13631585Sbonwick int c; 13641601Sbonwick 13651635Sbonwick /* 13661635Sbonwick * Claim log blocks that haven't been committed yet. 13671635Sbonwick * This must all happen in a single txg. 13681635Sbonwick */ 13691601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1370789Sahrens spa_first_txg(spa)); 13712417Sahrens (void) dmu_objset_find(spa->spa_name, 13722417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1373789Sahrens dmu_tx_commit(tx); 1374789Sahrens 1375789Sahrens spa->spa_sync_on = B_TRUE; 1376789Sahrens txg_sync_start(spa->spa_dsl_pool); 1377789Sahrens 1378789Sahrens /* 1379789Sahrens * Wait for all claims to sync. 1380789Sahrens */ 1381789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 13821585Sbonwick 13831585Sbonwick /* 13841635Sbonwick * If the config cache is stale, or we have uninitialized 13851635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 13861585Sbonwick */ 13871635Sbonwick if (config_cache_txg != spa->spa_config_txg || 13881635Sbonwick state == SPA_LOAD_IMPORT) 13891635Sbonwick need_update = B_TRUE; 13901635Sbonwick 13911635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 13921635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 13931635Sbonwick need_update = B_TRUE; 13941585Sbonwick 13951585Sbonwick /* 13961635Sbonwick * Update the config cache asychronously in case we're the 13971635Sbonwick * root pool, in which case the config cache isn't writable yet. 13981585Sbonwick */ 13991635Sbonwick if (need_update) 14001635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1401789Sahrens } 1402789Sahrens 14031544Seschrock error = 0; 14041544Seschrock out: 14057046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 14062082Seschrock if (error && error != EBADF) 14077294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 14081544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 14091544Seschrock spa->spa_ena = 0; 14101544Seschrock 14111544Seschrock return (error); 1412789Sahrens } 1413789Sahrens 1414789Sahrens /* 1415789Sahrens * Pool Open/Import 1416789Sahrens * 1417789Sahrens * The import case is identical to an open except that the configuration is sent 1418789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1419789Sahrens * case of an open, the pool configuration will exist in the 14204451Seschrock * POOL_STATE_UNINITIALIZED state. 1421789Sahrens * 1422789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1423789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1424789Sahrens * ambiguous state. 1425789Sahrens */ 1426789Sahrens static int 1427789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1428789Sahrens { 1429789Sahrens spa_t *spa; 1430789Sahrens int error; 1431789Sahrens int locked = B_FALSE; 1432789Sahrens 1433789Sahrens *spapp = NULL; 1434789Sahrens 1435789Sahrens /* 1436789Sahrens * As disgusting as this is, we need to support recursive calls to this 1437789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1438789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1439789Sahrens * avoid dsl_dir_open() calling this in the first place. 1440789Sahrens */ 1441789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1442789Sahrens mutex_enter(&spa_namespace_lock); 1443789Sahrens locked = B_TRUE; 1444789Sahrens } 1445789Sahrens 1446789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1447789Sahrens if (locked) 1448789Sahrens mutex_exit(&spa_namespace_lock); 1449789Sahrens return (ENOENT); 1450789Sahrens } 1451789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1452789Sahrens 1453789Sahrens spa_activate(spa); 1454789Sahrens 14551635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1456789Sahrens 1457789Sahrens if (error == EBADF) { 1458789Sahrens /* 14591986Seschrock * If vdev_validate() returns failure (indicated by 14601986Seschrock * EBADF), it indicates that one of the vdevs indicates 14611986Seschrock * that the pool has been exported or destroyed. If 14621986Seschrock * this is the case, the config cache is out of sync and 14631986Seschrock * we should remove the pool from the namespace. 1464789Sahrens */ 1465789Sahrens spa_unload(spa); 1466789Sahrens spa_deactivate(spa); 14676643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1468789Sahrens spa_remove(spa); 1469789Sahrens if (locked) 1470789Sahrens mutex_exit(&spa_namespace_lock); 1471789Sahrens return (ENOENT); 14721544Seschrock } 14731544Seschrock 14741544Seschrock if (error) { 1475789Sahrens /* 1476789Sahrens * We can't open the pool, but we still have useful 1477789Sahrens * information: the state of each vdev after the 1478789Sahrens * attempted vdev_open(). Return this to the user. 1479789Sahrens */ 14801635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 14811635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1482789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1483789Sahrens B_TRUE); 14841635Sbonwick spa_config_exit(spa, FTAG); 14851635Sbonwick } 1486789Sahrens spa_unload(spa); 1487789Sahrens spa_deactivate(spa); 14881544Seschrock spa->spa_last_open_failed = B_TRUE; 1489789Sahrens if (locked) 1490789Sahrens mutex_exit(&spa_namespace_lock); 1491789Sahrens *spapp = NULL; 1492789Sahrens return (error); 14931544Seschrock } else { 14941544Seschrock spa->spa_last_open_failed = B_FALSE; 1495789Sahrens } 1496789Sahrens } 1497789Sahrens 1498789Sahrens spa_open_ref(spa, tag); 14994451Seschrock 1500789Sahrens if (locked) 1501789Sahrens mutex_exit(&spa_namespace_lock); 1502789Sahrens 1503789Sahrens *spapp = spa; 1504789Sahrens 1505789Sahrens if (config != NULL) { 15061544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1507789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 15081544Seschrock spa_config_exit(spa, FTAG); 1509789Sahrens } 1510789Sahrens 1511789Sahrens return (0); 1512789Sahrens } 1513789Sahrens 1514789Sahrens int 1515789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1516789Sahrens { 1517789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1518789Sahrens } 1519789Sahrens 15201544Seschrock /* 15211544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 15221544Seschrock * preventing it from being exported or destroyed. 15231544Seschrock */ 15241544Seschrock spa_t * 15251544Seschrock spa_inject_addref(char *name) 15261544Seschrock { 15271544Seschrock spa_t *spa; 15281544Seschrock 15291544Seschrock mutex_enter(&spa_namespace_lock); 15301544Seschrock if ((spa = spa_lookup(name)) == NULL) { 15311544Seschrock mutex_exit(&spa_namespace_lock); 15321544Seschrock return (NULL); 15331544Seschrock } 15341544Seschrock spa->spa_inject_ref++; 15351544Seschrock mutex_exit(&spa_namespace_lock); 15361544Seschrock 15371544Seschrock return (spa); 15381544Seschrock } 15391544Seschrock 15401544Seschrock void 15411544Seschrock spa_inject_delref(spa_t *spa) 15421544Seschrock { 15431544Seschrock mutex_enter(&spa_namespace_lock); 15441544Seschrock spa->spa_inject_ref--; 15451544Seschrock mutex_exit(&spa_namespace_lock); 15461544Seschrock } 15471544Seschrock 15485450Sbrendan /* 15495450Sbrendan * Add spares device information to the nvlist. 15505450Sbrendan */ 15512082Seschrock static void 15522082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 15532082Seschrock { 15542082Seschrock nvlist_t **spares; 15552082Seschrock uint_t i, nspares; 15562082Seschrock nvlist_t *nvroot; 15572082Seschrock uint64_t guid; 15582082Seschrock vdev_stat_t *vs; 15592082Seschrock uint_t vsc; 15603377Seschrock uint64_t pool; 15612082Seschrock 15625450Sbrendan if (spa->spa_spares.sav_count == 0) 15632082Seschrock return; 15642082Seschrock 15652082Seschrock VERIFY(nvlist_lookup_nvlist(config, 15662082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15675450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 15682082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15692082Seschrock if (nspares != 0) { 15702082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 15712082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 15722082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 15732082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15742082Seschrock 15752082Seschrock /* 15762082Seschrock * Go through and find any spares which have since been 15772082Seschrock * repurposed as an active spare. If this is the case, update 15782082Seschrock * their status appropriately. 15792082Seschrock */ 15802082Seschrock for (i = 0; i < nspares; i++) { 15812082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 15822082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 15837214Slling if (spa_spare_exists(guid, &pool, NULL) && 15847214Slling pool != 0ULL) { 15852082Seschrock VERIFY(nvlist_lookup_uint64_array( 15862082Seschrock spares[i], ZPOOL_CONFIG_STATS, 15872082Seschrock (uint64_t **)&vs, &vsc) == 0); 15882082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 15892082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 15902082Seschrock } 15912082Seschrock } 15922082Seschrock } 15932082Seschrock } 15942082Seschrock 15955450Sbrendan /* 15965450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 15975450Sbrendan */ 15985450Sbrendan static void 15995450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 16005450Sbrendan { 16015450Sbrendan nvlist_t **l2cache; 16025450Sbrendan uint_t i, j, nl2cache; 16035450Sbrendan nvlist_t *nvroot; 16045450Sbrendan uint64_t guid; 16055450Sbrendan vdev_t *vd; 16065450Sbrendan vdev_stat_t *vs; 16075450Sbrendan uint_t vsc; 16085450Sbrendan 16095450Sbrendan if (spa->spa_l2cache.sav_count == 0) 16105450Sbrendan return; 16115450Sbrendan 16125450Sbrendan spa_config_enter(spa, RW_READER, FTAG); 16135450Sbrendan 16145450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 16155450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 16165450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 16175450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 16185450Sbrendan if (nl2cache != 0) { 16195450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 16205450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 16215450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 16225450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 16235450Sbrendan 16245450Sbrendan /* 16255450Sbrendan * Update level 2 cache device stats. 16265450Sbrendan */ 16275450Sbrendan 16285450Sbrendan for (i = 0; i < nl2cache; i++) { 16295450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 16305450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 16315450Sbrendan 16325450Sbrendan vd = NULL; 16335450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 16345450Sbrendan if (guid == 16355450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 16365450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 16375450Sbrendan break; 16385450Sbrendan } 16395450Sbrendan } 16405450Sbrendan ASSERT(vd != NULL); 16415450Sbrendan 16425450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 16435450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 16445450Sbrendan vdev_get_stats(vd, vs); 16455450Sbrendan } 16465450Sbrendan } 16475450Sbrendan 16485450Sbrendan spa_config_exit(spa, FTAG); 16495450Sbrendan } 16505450Sbrendan 1651789Sahrens int 16521544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1653789Sahrens { 1654789Sahrens int error; 1655789Sahrens spa_t *spa; 1656789Sahrens 1657789Sahrens *config = NULL; 1658789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1659789Sahrens 16602082Seschrock if (spa && *config != NULL) { 16611544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 16621544Seschrock spa_get_errlog_size(spa)) == 0); 16631544Seschrock 16642082Seschrock spa_add_spares(spa, *config); 16655450Sbrendan spa_add_l2cache(spa, *config); 16662082Seschrock } 16672082Seschrock 16681544Seschrock /* 16691544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 16701544Seschrock * and call spa_lookup() directly. 16711544Seschrock */ 16721544Seschrock if (altroot) { 16731544Seschrock if (spa == NULL) { 16741544Seschrock mutex_enter(&spa_namespace_lock); 16751544Seschrock spa = spa_lookup(name); 16761544Seschrock if (spa) 16771544Seschrock spa_altroot(spa, altroot, buflen); 16781544Seschrock else 16791544Seschrock altroot[0] = '\0'; 16801544Seschrock spa = NULL; 16811544Seschrock mutex_exit(&spa_namespace_lock); 16821544Seschrock } else { 16831544Seschrock spa_altroot(spa, altroot, buflen); 16841544Seschrock } 16851544Seschrock } 16861544Seschrock 1687789Sahrens if (spa != NULL) 1688789Sahrens spa_close(spa, FTAG); 1689789Sahrens 1690789Sahrens return (error); 1691789Sahrens } 1692789Sahrens 1693789Sahrens /* 16945450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 16955450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 16965450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 16975450Sbrendan * specified, as long as they are well-formed. 16982082Seschrock */ 16992082Seschrock static int 17005450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 17015450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 17025450Sbrendan vdev_labeltype_t label) 17032082Seschrock { 17045450Sbrendan nvlist_t **dev; 17055450Sbrendan uint_t i, ndev; 17062082Seschrock vdev_t *vd; 17072082Seschrock int error; 17082082Seschrock 17092082Seschrock /* 17105450Sbrendan * It's acceptable to have no devs specified. 17112082Seschrock */ 17125450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 17132082Seschrock return (0); 17142082Seschrock 17155450Sbrendan if (ndev == 0) 17162082Seschrock return (EINVAL); 17172082Seschrock 17182082Seschrock /* 17195450Sbrendan * Make sure the pool is formatted with a version that supports this 17205450Sbrendan * device type. 17212082Seschrock */ 17225450Sbrendan if (spa_version(spa) < version) 17232082Seschrock return (ENOTSUP); 17242082Seschrock 17253377Seschrock /* 17265450Sbrendan * Set the pending device list so we correctly handle device in-use 17273377Seschrock * checking. 17283377Seschrock */ 17295450Sbrendan sav->sav_pending = dev; 17305450Sbrendan sav->sav_npending = ndev; 17315450Sbrendan 17325450Sbrendan for (i = 0; i < ndev; i++) { 17335450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 17342082Seschrock mode)) != 0) 17353377Seschrock goto out; 17362082Seschrock 17372082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 17382082Seschrock vdev_free(vd); 17393377Seschrock error = EINVAL; 17403377Seschrock goto out; 17412082Seschrock } 17422082Seschrock 17435450Sbrendan /* 17445450Sbrendan * The L2ARC currently only supports disk devices. 17455450Sbrendan */ 17465450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 17475450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 17485450Sbrendan error = ENOTBLK; 17495450Sbrendan goto out; 17505450Sbrendan } 17515450Sbrendan 17522082Seschrock vd->vdev_top = vd; 17533377Seschrock 17543377Seschrock if ((error = vdev_open(vd)) == 0 && 17555450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 17565450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 17573377Seschrock vd->vdev_guid) == 0); 17582082Seschrock } 17592082Seschrock 17602082Seschrock vdev_free(vd); 17613377Seschrock 17625450Sbrendan if (error && 17635450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 17643377Seschrock goto out; 17653377Seschrock else 17663377Seschrock error = 0; 17672082Seschrock } 17682082Seschrock 17693377Seschrock out: 17705450Sbrendan sav->sav_pending = NULL; 17715450Sbrendan sav->sav_npending = 0; 17723377Seschrock return (error); 17732082Seschrock } 17742082Seschrock 17755450Sbrendan static int 17765450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 17775450Sbrendan { 17785450Sbrendan int error; 17795450Sbrendan 17805450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17815450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 17825450Sbrendan VDEV_LABEL_SPARE)) != 0) { 17835450Sbrendan return (error); 17845450Sbrendan } 17855450Sbrendan 17865450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17875450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 17885450Sbrendan VDEV_LABEL_L2CACHE)); 17895450Sbrendan } 17905450Sbrendan 17915450Sbrendan static void 17925450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 17935450Sbrendan const char *config) 17945450Sbrendan { 17955450Sbrendan int i; 17965450Sbrendan 17975450Sbrendan if (sav->sav_config != NULL) { 17985450Sbrendan nvlist_t **olddevs; 17995450Sbrendan uint_t oldndevs; 18005450Sbrendan nvlist_t **newdevs; 18015450Sbrendan 18025450Sbrendan /* 18035450Sbrendan * Generate new dev list by concatentating with the 18045450Sbrendan * current dev list. 18055450Sbrendan */ 18065450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 18075450Sbrendan &olddevs, &oldndevs) == 0); 18085450Sbrendan 18095450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 18105450Sbrendan (ndevs + oldndevs), KM_SLEEP); 18115450Sbrendan for (i = 0; i < oldndevs; i++) 18125450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 18135450Sbrendan KM_SLEEP) == 0); 18145450Sbrendan for (i = 0; i < ndevs; i++) 18155450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 18165450Sbrendan KM_SLEEP) == 0); 18175450Sbrendan 18185450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 18195450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 18205450Sbrendan 18215450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 18225450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 18235450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 18245450Sbrendan nvlist_free(newdevs[i]); 18255450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 18265450Sbrendan } else { 18275450Sbrendan /* 18285450Sbrendan * Generate a new dev list. 18295450Sbrendan */ 18305450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 18315450Sbrendan KM_SLEEP) == 0); 18325450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 18335450Sbrendan devs, ndevs) == 0); 18345450Sbrendan } 18355450Sbrendan } 18365450Sbrendan 18375450Sbrendan /* 18385450Sbrendan * Stop and drop level 2 ARC devices 18395450Sbrendan */ 18405450Sbrendan void 18415450Sbrendan spa_l2cache_drop(spa_t *spa) 18425450Sbrendan { 18435450Sbrendan vdev_t *vd; 18445450Sbrendan int i; 18455450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 18465450Sbrendan 18475450Sbrendan for (i = 0; i < sav->sav_count; i++) { 18485450Sbrendan uint64_t pool; 18495450Sbrendan 18505450Sbrendan vd = sav->sav_vdevs[i]; 18515450Sbrendan ASSERT(vd != NULL); 18525450Sbrendan 18535450Sbrendan if (spa_mode & FWRITE && 18546643Seschrock spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL && 18556643Seschrock l2arc_vdev_present(vd)) { 18565450Sbrendan l2arc_remove_vdev(vd); 18575450Sbrendan } 18585450Sbrendan if (vd->vdev_isl2cache) 18595450Sbrendan spa_l2cache_remove(vd); 18605450Sbrendan vdev_clear_stats(vd); 18615450Sbrendan (void) vdev_close(vd); 18625450Sbrendan } 18635450Sbrendan } 18645450Sbrendan 18652082Seschrock /* 1866789Sahrens * Pool Creation 1867789Sahrens */ 1868789Sahrens int 18695094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 18707184Stimh const char *history_str, nvlist_t *zplprops) 1871789Sahrens { 1872789Sahrens spa_t *spa; 18735094Slling char *altroot = NULL; 18741635Sbonwick vdev_t *rvd; 1875789Sahrens dsl_pool_t *dp; 1876789Sahrens dmu_tx_t *tx; 18772082Seschrock int c, error = 0; 1878789Sahrens uint64_t txg = TXG_INITIAL; 18795450Sbrendan nvlist_t **spares, **l2cache; 18805450Sbrendan uint_t nspares, nl2cache; 18815094Slling uint64_t version; 1882789Sahrens 1883789Sahrens /* 1884789Sahrens * If this pool already exists, return failure. 1885789Sahrens */ 1886789Sahrens mutex_enter(&spa_namespace_lock); 1887789Sahrens if (spa_lookup(pool) != NULL) { 1888789Sahrens mutex_exit(&spa_namespace_lock); 1889789Sahrens return (EEXIST); 1890789Sahrens } 1891789Sahrens 1892789Sahrens /* 1893789Sahrens * Allocate a new spa_t structure. 1894789Sahrens */ 18955094Slling (void) nvlist_lookup_string(props, 18965094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 18971635Sbonwick spa = spa_add(pool, altroot); 1898789Sahrens spa_activate(spa); 1899789Sahrens 1900789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 19015094Slling 19025094Slling if (props && (error = spa_prop_validate(spa, props))) { 19035094Slling spa_unload(spa); 19045094Slling spa_deactivate(spa); 19055094Slling spa_remove(spa); 19066643Seschrock mutex_exit(&spa_namespace_lock); 19075094Slling return (error); 19085094Slling } 19095094Slling 19105094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 19115094Slling &version) != 0) 19125094Slling version = SPA_VERSION; 19135094Slling ASSERT(version <= SPA_VERSION); 19145094Slling spa->spa_uberblock.ub_version = version; 1915789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1916789Sahrens 19171635Sbonwick /* 19181635Sbonwick * Create the root vdev. 19191635Sbonwick */ 19201635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 19211635Sbonwick 19222082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 19232082Seschrock 19242082Seschrock ASSERT(error != 0 || rvd != NULL); 19252082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 19262082Seschrock 19275913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 19281635Sbonwick error = EINVAL; 19292082Seschrock 19302082Seschrock if (error == 0 && 19312082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 19325450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 19332082Seschrock VDEV_ALLOC_ADD)) == 0) { 19342082Seschrock for (c = 0; c < rvd->vdev_children; c++) 19352082Seschrock vdev_init(rvd->vdev_child[c], txg); 19362082Seschrock vdev_config_dirty(rvd); 19371635Sbonwick } 19381635Sbonwick 19391635Sbonwick spa_config_exit(spa, FTAG); 1940789Sahrens 19412082Seschrock if (error != 0) { 1942789Sahrens spa_unload(spa); 1943789Sahrens spa_deactivate(spa); 1944789Sahrens spa_remove(spa); 1945789Sahrens mutex_exit(&spa_namespace_lock); 1946789Sahrens return (error); 1947789Sahrens } 1948789Sahrens 19492082Seschrock /* 19502082Seschrock * Get the list of spares, if specified. 19512082Seschrock */ 19522082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 19532082Seschrock &spares, &nspares) == 0) { 19545450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 19552082Seschrock KM_SLEEP) == 0); 19565450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 19572082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 19582082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 19592082Seschrock spa_load_spares(spa); 19602082Seschrock spa_config_exit(spa, FTAG); 19615450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 19625450Sbrendan } 19635450Sbrendan 19645450Sbrendan /* 19655450Sbrendan * Get the list of level 2 cache devices, if specified. 19665450Sbrendan */ 19675450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 19685450Sbrendan &l2cache, &nl2cache) == 0) { 19695450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 19705450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 19715450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 19725450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 19735450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 19745450Sbrendan spa_load_l2cache(spa); 19755450Sbrendan spa_config_exit(spa, FTAG); 19765450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 19772082Seschrock } 19782082Seschrock 19797184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 1980789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1981789Sahrens 1982789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1983789Sahrens 1984789Sahrens /* 1985789Sahrens * Create the pool config object. 1986789Sahrens */ 1987789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1988*7497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 1989789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1990789Sahrens 19911544Seschrock if (zap_add(spa->spa_meta_objset, 1992789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 19931544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 19941544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 19951544Seschrock } 1996789Sahrens 19975094Slling /* Newly created pools with the right version are always deflated. */ 19985094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 19995094Slling spa->spa_deflate = TRUE; 20005094Slling if (zap_add(spa->spa_meta_objset, 20015094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 20025094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 20035094Slling cmn_err(CE_PANIC, "failed to add deflate"); 20045094Slling } 20052082Seschrock } 20062082Seschrock 2007789Sahrens /* 2008789Sahrens * Create the deferred-free bplist object. Turn off compression 2009789Sahrens * because sync-to-convergence takes longer if the blocksize 2010789Sahrens * keeps changing. 2011789Sahrens */ 2012789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2013789Sahrens 1 << 14, tx); 2014789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2015789Sahrens ZIO_COMPRESS_OFF, tx); 2016789Sahrens 20171544Seschrock if (zap_add(spa->spa_meta_objset, 2018789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 20191544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 20201544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 20211544Seschrock } 2022789Sahrens 20232926Sek110237 /* 20242926Sek110237 * Create the pool's history object. 20252926Sek110237 */ 20265094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 20275094Slling spa_history_create_obj(spa, tx); 20285094Slling 20295094Slling /* 20305094Slling * Set pool properties. 20315094Slling */ 20325094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 20335094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20345329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 20355094Slling if (props) 20365094Slling spa_sync_props(spa, props, CRED(), tx); 20372926Sek110237 2038789Sahrens dmu_tx_commit(tx); 2039789Sahrens 2040789Sahrens spa->spa_sync_on = B_TRUE; 2041789Sahrens txg_sync_start(spa->spa_dsl_pool); 2042789Sahrens 2043789Sahrens /* 2044789Sahrens * We explicitly wait for the first transaction to complete so that our 2045789Sahrens * bean counters are appropriately updated. 2046789Sahrens */ 2047789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2048789Sahrens 20496643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2050789Sahrens 20515094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 20524715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 20534715Sek110237 2054789Sahrens mutex_exit(&spa_namespace_lock); 2055789Sahrens 20567046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 20577046Sahrens 2058789Sahrens return (0); 2059789Sahrens } 2060789Sahrens 2061789Sahrens /* 2062789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2063789Sahrens * then call spa_load() to do the dirty work. 2064789Sahrens */ 20656423Sgw25295 static int 20666423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 20676643Seschrock boolean_t isroot, boolean_t allowfaulted) 2068789Sahrens { 2069789Sahrens spa_t *spa; 20705094Slling char *altroot = NULL; 20716643Seschrock int error, loaderr; 20722082Seschrock nvlist_t *nvroot; 20735450Sbrendan nvlist_t **spares, **l2cache; 20745450Sbrendan uint_t nspares, nl2cache; 2075789Sahrens 2076789Sahrens /* 2077789Sahrens * If a pool with this name exists, return failure. 2078789Sahrens */ 2079789Sahrens mutex_enter(&spa_namespace_lock); 2080789Sahrens if (spa_lookup(pool) != NULL) { 2081789Sahrens mutex_exit(&spa_namespace_lock); 2082789Sahrens return (EEXIST); 2083789Sahrens } 2084789Sahrens 2085789Sahrens /* 20861635Sbonwick * Create and initialize the spa structure. 2087789Sahrens */ 20885094Slling (void) nvlist_lookup_string(props, 20895094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 20901635Sbonwick spa = spa_add(pool, altroot); 2091789Sahrens spa_activate(spa); 2092789Sahrens 20936643Seschrock if (allowfaulted) 20946643Seschrock spa->spa_import_faulted = B_TRUE; 20956673Seschrock spa->spa_is_root = isroot; 20966643Seschrock 2097789Sahrens /* 20981635Sbonwick * Pass off the heavy lifting to spa_load(). 20997046Sahrens * Pass TRUE for mosconfig (unless this is a root pool) because 21007046Sahrens * the user-supplied config is actually the one to trust when 21017046Sahrens * doing an import. 21021601Sbonwick */ 21037046Sahrens loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot); 2104789Sahrens 21052082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21062082Seschrock /* 21072082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 21082082Seschrock * and conflicts with spa_has_spare(). 21092082Seschrock */ 21106423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 21115450Sbrendan nvlist_free(spa->spa_spares.sav_config); 21125450Sbrendan spa->spa_spares.sav_config = NULL; 21132082Seschrock spa_load_spares(spa); 21142082Seschrock } 21156423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 21165450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 21175450Sbrendan spa->spa_l2cache.sav_config = NULL; 21185450Sbrendan spa_load_l2cache(spa); 21195450Sbrendan } 21202082Seschrock 21212082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 21222082Seschrock &nvroot) == 0); 21235450Sbrendan if (error == 0) 21245450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 21255450Sbrendan if (error == 0) 21265450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 21275450Sbrendan VDEV_ALLOC_L2CACHE); 21282082Seschrock spa_config_exit(spa, FTAG); 21292082Seschrock 21305094Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 21316643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 21326643Seschrock /* 21336643Seschrock * If we failed to load the pool, but 'allowfaulted' is 21346643Seschrock * set, then manually set the config as if the config 21356643Seschrock * passed in was specified in the cache file. 21366643Seschrock */ 21376643Seschrock error = 0; 21386643Seschrock spa->spa_import_faulted = B_FALSE; 21396643Seschrock if (spa->spa_config == NULL) { 21406643Seschrock spa_config_enter(spa, RW_READER, FTAG); 21416643Seschrock spa->spa_config = spa_config_generate(spa, 21426643Seschrock NULL, -1ULL, B_TRUE); 21436643Seschrock spa_config_exit(spa, FTAG); 21446643Seschrock } 21456643Seschrock spa_unload(spa); 21466643Seschrock spa_deactivate(spa); 21476643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 21486643Seschrock } else { 21496643Seschrock spa_unload(spa); 21506643Seschrock spa_deactivate(spa); 21516643Seschrock spa_remove(spa); 21526643Seschrock } 2153789Sahrens mutex_exit(&spa_namespace_lock); 2154789Sahrens return (error); 2155789Sahrens } 2156789Sahrens 21571635Sbonwick /* 21585450Sbrendan * Override any spares and level 2 cache devices as specified by 21595450Sbrendan * the user, as these may have correct device names/devids, etc. 21602082Seschrock */ 21612082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 21622082Seschrock &spares, &nspares) == 0) { 21635450Sbrendan if (spa->spa_spares.sav_config) 21645450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 21652082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 21662082Seschrock else 21675450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 21682082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 21695450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 21702082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 21712082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21722082Seschrock spa_load_spares(spa); 21732082Seschrock spa_config_exit(spa, FTAG); 21745450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 21755450Sbrendan } 21765450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 21775450Sbrendan &l2cache, &nl2cache) == 0) { 21785450Sbrendan if (spa->spa_l2cache.sav_config) 21795450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 21805450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 21815450Sbrendan else 21825450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 21835450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 21845450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 21855450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21865450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 21875450Sbrendan spa_load_l2cache(spa); 21885450Sbrendan spa_config_exit(spa, FTAG); 21895450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 21902082Seschrock } 21912082Seschrock 21926643Seschrock if (spa_mode & FWRITE) { 21936643Seschrock /* 21946643Seschrock * Update the config cache to include the newly-imported pool. 21956643Seschrock */ 21966423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 21976643Seschrock } 21986643Seschrock 21996643Seschrock spa->spa_import_faulted = B_FALSE; 22004451Seschrock mutex_exit(&spa_namespace_lock); 22014451Seschrock 2202789Sahrens return (0); 2203789Sahrens } 2204789Sahrens 22056423Sgw25295 #ifdef _KERNEL 22066423Sgw25295 /* 22076423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 22086423Sgw25295 * device label. 22096423Sgw25295 */ 22106423Sgw25295 static void 22116423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 22126423Sgw25295 { 22136423Sgw25295 nvlist_t *nvtop, *nvroot; 22146423Sgw25295 uint64_t pgid; 22156423Sgw25295 22166423Sgw25295 /* 22176423Sgw25295 * Add this top-level vdev to the child array. 22186423Sgw25295 */ 22196423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 22206423Sgw25295 == 0); 22216423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 22226423Sgw25295 == 0); 22236423Sgw25295 22246423Sgw25295 /* 22256423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 22266423Sgw25295 */ 22276423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 22286423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 22296423Sgw25295 == 0); 22306423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 22316423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 22326423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 22336423Sgw25295 &nvtop, 1) == 0); 22346423Sgw25295 22356423Sgw25295 /* 22366423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 22376423Sgw25295 * this pool's configuration (remove the old, add the new). 22386423Sgw25295 */ 22396423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 22406423Sgw25295 nvlist_free(nvroot); 22416423Sgw25295 } 22426423Sgw25295 22436423Sgw25295 /* 22446423Sgw25295 * Get the root pool information from the root disk, then import the root pool 22456423Sgw25295 * during the system boot up time. 22466423Sgw25295 */ 22477147Staylor extern nvlist_t *vdev_disk_read_rootlabel(char *, char *); 22487147Staylor 22497147Staylor int 22507147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf, 22516423Sgw25295 uint64_t *besttxg) 22526423Sgw25295 { 22536423Sgw25295 nvlist_t *config; 22546423Sgw25295 uint64_t txg; 22556423Sgw25295 22567147Staylor if ((config = vdev_disk_read_rootlabel(devpath, devid)) == NULL) 22577147Staylor return (-1); 22586423Sgw25295 22596423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 22606423Sgw25295 22617147Staylor if (bestconf != NULL) 22626423Sgw25295 *bestconf = config; 22637147Staylor *besttxg = txg; 22647147Staylor return (0); 22656423Sgw25295 } 22666423Sgw25295 22676423Sgw25295 boolean_t 22686423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 22696423Sgw25295 { 22706423Sgw25295 uint64_t ival; 22716423Sgw25295 22726423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 22736423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 22746423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 22756423Sgw25295 return (B_FALSE); 22766423Sgw25295 22776423Sgw25295 return (B_TRUE); 22786423Sgw25295 } 22796423Sgw25295 22807147Staylor 22817147Staylor /* 22827147Staylor * Given the boot device's physical path or devid, check if the device 22837147Staylor * is in a valid state. If so, return the configuration from the vdev 22847147Staylor * label. 22857147Staylor */ 22867147Staylor int 22877147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf) 22887147Staylor { 22897147Staylor nvlist_t *conf = NULL; 22907147Staylor uint64_t txg = 0; 22917147Staylor nvlist_t *nvtop, **child; 22927147Staylor char *type; 22937147Staylor char *bootpath = NULL; 22947147Staylor uint_t children, c; 22957147Staylor char *tmp; 22967147Staylor 22977147Staylor if (devpath && ((tmp = strchr(devpath, ' ')) != NULL)) 22987147Staylor *tmp = '\0'; 22997147Staylor if (spa_check_rootconf(devpath, devid, &conf, &txg) < 0) { 23007147Staylor cmn_err(CE_NOTE, "error reading device label"); 23017147Staylor nvlist_free(conf); 23027147Staylor return (EINVAL); 23037147Staylor } 23047147Staylor if (txg == 0) { 23057147Staylor cmn_err(CE_NOTE, "this device is detached"); 23067147Staylor nvlist_free(conf); 23077147Staylor return (EINVAL); 23087147Staylor } 23097147Staylor 23107147Staylor VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE, 23117147Staylor &nvtop) == 0); 23127147Staylor VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0); 23137147Staylor 23147147Staylor if (strcmp(type, VDEV_TYPE_DISK) == 0) { 23157147Staylor if (spa_rootdev_validate(nvtop)) { 23167147Staylor goto out; 23177147Staylor } else { 23187147Staylor nvlist_free(conf); 23197147Staylor return (EINVAL); 23207147Staylor } 23217147Staylor } 23227147Staylor 23237147Staylor ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0); 23247147Staylor 23257147Staylor VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN, 23267147Staylor &child, &children) == 0); 23277147Staylor 23287147Staylor /* 23297147Staylor * Go thru vdevs in the mirror to see if the given device 23307147Staylor * has the most recent txg. Only the device with the most 23317147Staylor * recent txg has valid information and should be booted. 23327147Staylor */ 23337147Staylor for (c = 0; c < children; c++) { 23347147Staylor char *cdevid, *cpath; 23357147Staylor uint64_t tmptxg; 23367147Staylor 23377147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH, 23387147Staylor &cpath) != 0) 23397147Staylor return (EINVAL); 23407147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_DEVID, 23417147Staylor &cdevid) != 0) 23427147Staylor return (EINVAL); 23437147Staylor if ((spa_check_rootconf(cpath, cdevid, NULL, 23447147Staylor &tmptxg) == 0) && (tmptxg > txg)) { 23457147Staylor txg = tmptxg; 23467147Staylor VERIFY(nvlist_lookup_string(child[c], 23477147Staylor ZPOOL_CONFIG_PATH, &bootpath) == 0); 23487147Staylor } 23497147Staylor } 23507147Staylor 23517147Staylor /* Does the best device match the one we've booted from? */ 23527147Staylor if (bootpath) { 23537147Staylor cmn_err(CE_NOTE, "try booting from '%s'", bootpath); 23547147Staylor return (EINVAL); 23557147Staylor } 23567147Staylor out: 23577147Staylor *bestconf = conf; 23587147Staylor return (0); 23597147Staylor } 23607147Staylor 23616423Sgw25295 /* 23626423Sgw25295 * Import a root pool. 23636423Sgw25295 * 23647147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 23657147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 23667147Staylor * The GRUB "findroot" command will return the vdev we should boot. 23676423Sgw25295 * 23686423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 23696423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 23706423Sgw25295 * e.g. 23716423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 23726423Sgw25295 */ 23736423Sgw25295 int 23747147Staylor spa_import_rootpool(char *devpath, char *devid) 23756423Sgw25295 { 23766423Sgw25295 nvlist_t *conf = NULL; 23776423Sgw25295 char *pname; 23786423Sgw25295 int error; 23796423Sgw25295 23806423Sgw25295 /* 23816423Sgw25295 * Get the vdev pathname and configuation from the most 23826423Sgw25295 * recently updated vdev (highest txg). 23836423Sgw25295 */ 23847147Staylor if (error = spa_get_rootconf(devpath, devid, &conf)) 23856423Sgw25295 goto msg_out; 23866423Sgw25295 23876423Sgw25295 /* 23886423Sgw25295 * Add type "root" vdev to the config. 23896423Sgw25295 */ 23906423Sgw25295 spa_build_rootpool_config(conf); 23916423Sgw25295 23926423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 23936423Sgw25295 23946673Seschrock /* 23956673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 23966673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 23976673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 23986673Seschrock * pool open, not import. 23996673Seschrock */ 24006673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 24016423Sgw25295 if (error == EEXIST) 24026423Sgw25295 error = 0; 24036423Sgw25295 24046423Sgw25295 nvlist_free(conf); 24056423Sgw25295 return (error); 24066423Sgw25295 24076423Sgw25295 msg_out: 24087147Staylor cmn_err(CE_NOTE, "\n" 24096423Sgw25295 " *************************************************** \n" 24106423Sgw25295 " * This device is not bootable! * \n" 24116423Sgw25295 " * It is either offlined or detached or faulted. * \n" 24126423Sgw25295 " * Please try to boot from a different device. * \n" 24137147Staylor " *************************************************** "); 24146423Sgw25295 24156423Sgw25295 return (error); 24166423Sgw25295 } 24176423Sgw25295 #endif 24186423Sgw25295 24196423Sgw25295 /* 24206423Sgw25295 * Import a non-root pool into the system. 24216423Sgw25295 */ 24226423Sgw25295 int 24236423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 24246423Sgw25295 { 24256643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 24266423Sgw25295 } 24276423Sgw25295 24286643Seschrock int 24296643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 24306643Seschrock { 24316643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 24326643Seschrock } 24336643Seschrock 24346643Seschrock 2435789Sahrens /* 2436789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2437789Sahrens * to get the vdev stats associated with the imported devices. 2438789Sahrens */ 2439789Sahrens #define TRYIMPORT_NAME "$import" 2440789Sahrens 2441789Sahrens nvlist_t * 2442789Sahrens spa_tryimport(nvlist_t *tryconfig) 2443789Sahrens { 2444789Sahrens nvlist_t *config = NULL; 2445789Sahrens char *poolname; 2446789Sahrens spa_t *spa; 2447789Sahrens uint64_t state; 2448789Sahrens 2449789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2450789Sahrens return (NULL); 2451789Sahrens 2452789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2453789Sahrens return (NULL); 2454789Sahrens 24551635Sbonwick /* 24561635Sbonwick * Create and initialize the spa structure. 24571635Sbonwick */ 2458789Sahrens mutex_enter(&spa_namespace_lock); 24591635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2460789Sahrens spa_activate(spa); 2461789Sahrens 2462789Sahrens /* 24631635Sbonwick * Pass off the heavy lifting to spa_load(). 24641732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 24651732Sbonwick * is actually the one to trust when doing an import. 2466789Sahrens */ 24671732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2468789Sahrens 2469789Sahrens /* 2470789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2471789Sahrens */ 2472789Sahrens if (spa->spa_root_vdev != NULL) { 24731635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 2474789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 24751635Sbonwick spa_config_exit(spa, FTAG); 2476789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2477789Sahrens poolname) == 0); 2478789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2479789Sahrens state) == 0); 24803975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 24813975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 24822082Seschrock 24832082Seschrock /* 24846423Sgw25295 * If the bootfs property exists on this pool then we 24856423Sgw25295 * copy it out so that external consumers can tell which 24866423Sgw25295 * pools are bootable. 24876423Sgw25295 */ 24886423Sgw25295 if (spa->spa_bootfs) { 24896423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 24906423Sgw25295 24916423Sgw25295 /* 24926423Sgw25295 * We have to play games with the name since the 24936423Sgw25295 * pool was opened as TRYIMPORT_NAME. 24946423Sgw25295 */ 24956423Sgw25295 if (dsl_dsobj_to_dsname(spa->spa_name, 24966423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 24976423Sgw25295 char *cp; 24986423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 24996423Sgw25295 25006423Sgw25295 cp = strchr(tmpname, '/'); 25016423Sgw25295 if (cp == NULL) { 25026423Sgw25295 (void) strlcpy(dsname, tmpname, 25036423Sgw25295 MAXPATHLEN); 25046423Sgw25295 } else { 25056423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 25066423Sgw25295 "%s/%s", poolname, ++cp); 25076423Sgw25295 } 25086423Sgw25295 VERIFY(nvlist_add_string(config, 25096423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 25106423Sgw25295 kmem_free(dsname, MAXPATHLEN); 25116423Sgw25295 } 25126423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 25136423Sgw25295 } 25146423Sgw25295 25156423Sgw25295 /* 25165450Sbrendan * Add the list of hot spares and level 2 cache devices. 25172082Seschrock */ 25182082Seschrock spa_add_spares(spa, config); 25195450Sbrendan spa_add_l2cache(spa, config); 2520789Sahrens } 2521789Sahrens 2522789Sahrens spa_unload(spa); 2523789Sahrens spa_deactivate(spa); 2524789Sahrens spa_remove(spa); 2525789Sahrens mutex_exit(&spa_namespace_lock); 2526789Sahrens 2527789Sahrens return (config); 2528789Sahrens } 2529789Sahrens 2530789Sahrens /* 2531789Sahrens * Pool export/destroy 2532789Sahrens * 2533789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2534789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2535789Sahrens * update the pool state and sync all the labels to disk, removing the 2536789Sahrens * configuration from the cache afterwards. 2537789Sahrens */ 2538789Sahrens static int 25397214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 25407214Slling boolean_t force) 2541789Sahrens { 2542789Sahrens spa_t *spa; 2543789Sahrens 25441775Sbillm if (oldconfig) 25451775Sbillm *oldconfig = NULL; 25461775Sbillm 2547789Sahrens if (!(spa_mode & FWRITE)) 2548789Sahrens return (EROFS); 2549789Sahrens 2550789Sahrens mutex_enter(&spa_namespace_lock); 2551789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2552789Sahrens mutex_exit(&spa_namespace_lock); 2553789Sahrens return (ENOENT); 2554789Sahrens } 2555789Sahrens 2556789Sahrens /* 25571544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 25581544Seschrock * reacquire the namespace lock, and see if we can export. 25591544Seschrock */ 25601544Seschrock spa_open_ref(spa, FTAG); 25611544Seschrock mutex_exit(&spa_namespace_lock); 25621544Seschrock spa_async_suspend(spa); 25631544Seschrock mutex_enter(&spa_namespace_lock); 25641544Seschrock spa_close(spa, FTAG); 25651544Seschrock 25661544Seschrock /* 2567789Sahrens * The pool will be in core if it's openable, 2568789Sahrens * in which case we can modify its state. 2569789Sahrens */ 2570789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2571789Sahrens /* 2572789Sahrens * Objsets may be open only because they're dirty, so we 2573789Sahrens * have to force it to sync before checking spa_refcnt. 2574789Sahrens */ 2575789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2576789Sahrens 25771544Seschrock /* 25781544Seschrock * A pool cannot be exported or destroyed if there are active 25791544Seschrock * references. If we are resetting a pool, allow references by 25801544Seschrock * fault injection handlers. 25811544Seschrock */ 25821544Seschrock if (!spa_refcount_zero(spa) || 25831544Seschrock (spa->spa_inject_ref != 0 && 25841544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 25851544Seschrock spa_async_resume(spa); 2586789Sahrens mutex_exit(&spa_namespace_lock); 2587789Sahrens return (EBUSY); 2588789Sahrens } 2589789Sahrens 2590789Sahrens /* 25917214Slling * A pool cannot be exported if it has an active shared spare. 25927214Slling * This is to prevent other pools stealing the active spare 25937214Slling * from an exported pool. At user's own will, such pool can 25947214Slling * be forcedly exported. 25957214Slling */ 25967214Slling if (!force && new_state == POOL_STATE_EXPORTED && 25977214Slling spa_has_active_shared_spare(spa)) { 25987214Slling spa_async_resume(spa); 25997214Slling mutex_exit(&spa_namespace_lock); 26007214Slling return (EXDEV); 26017214Slling } 26027214Slling 26037214Slling /* 2604789Sahrens * We want this to be reflected on every label, 2605789Sahrens * so mark them all dirty. spa_unload() will do the 2606789Sahrens * final sync that pushes these changes out. 2607789Sahrens */ 26081544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 26091601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 26101544Seschrock spa->spa_state = new_state; 26111635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 26121544Seschrock vdev_config_dirty(spa->spa_root_vdev); 26131601Sbonwick spa_config_exit(spa, FTAG); 26141544Seschrock } 2615789Sahrens } 2616789Sahrens 26174451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 26184451Seschrock 2619789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2620789Sahrens spa_unload(spa); 2621789Sahrens spa_deactivate(spa); 2622789Sahrens } 2623789Sahrens 26241775Sbillm if (oldconfig && spa->spa_config) 26251775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 26261775Sbillm 26271544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 26286643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 26291544Seschrock spa_remove(spa); 26301544Seschrock } 2631789Sahrens mutex_exit(&spa_namespace_lock); 2632789Sahrens 2633789Sahrens return (0); 2634789Sahrens } 2635789Sahrens 2636789Sahrens /* 2637789Sahrens * Destroy a storage pool. 2638789Sahrens */ 2639789Sahrens int 2640789Sahrens spa_destroy(char *pool) 2641789Sahrens { 26427214Slling return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, B_FALSE)); 2643789Sahrens } 2644789Sahrens 2645789Sahrens /* 2646789Sahrens * Export a storage pool. 2647789Sahrens */ 2648789Sahrens int 26497214Slling spa_export(char *pool, nvlist_t **oldconfig, boolean_t force) 2650789Sahrens { 26517214Slling return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, force)); 2652789Sahrens } 2653789Sahrens 2654789Sahrens /* 26551544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 26561544Seschrock * from the namespace in any way. 26571544Seschrock */ 26581544Seschrock int 26591544Seschrock spa_reset(char *pool) 26601544Seschrock { 26617214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 26627214Slling B_FALSE)); 26631544Seschrock } 26641544Seschrock 26651544Seschrock /* 2666789Sahrens * ========================================================================== 2667789Sahrens * Device manipulation 2668789Sahrens * ========================================================================== 2669789Sahrens */ 2670789Sahrens 2671789Sahrens /* 26724527Sperrin * Add a device to a storage pool. 2673789Sahrens */ 2674789Sahrens int 2675789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2676789Sahrens { 2677789Sahrens uint64_t txg; 26781635Sbonwick int c, error; 2679789Sahrens vdev_t *rvd = spa->spa_root_vdev; 26801585Sbonwick vdev_t *vd, *tvd; 26815450Sbrendan nvlist_t **spares, **l2cache; 26825450Sbrendan uint_t nspares, nl2cache; 2683789Sahrens 2684789Sahrens txg = spa_vdev_enter(spa); 2685789Sahrens 26862082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 26872082Seschrock VDEV_ALLOC_ADD)) != 0) 26882082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 26892082Seschrock 26903377Seschrock spa->spa_pending_vdev = vd; 2691789Sahrens 26925450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 26935450Sbrendan &nspares) != 0) 26942082Seschrock nspares = 0; 26952082Seschrock 26965450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 26975450Sbrendan &nl2cache) != 0) 26985450Sbrendan nl2cache = 0; 26995450Sbrendan 27005450Sbrendan if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) { 27013377Seschrock spa->spa_pending_vdev = NULL; 27022082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 27033377Seschrock } 27042082Seschrock 27052082Seschrock if (vd->vdev_children != 0) { 27063377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 27073377Seschrock spa->spa_pending_vdev = NULL; 27082082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 27092082Seschrock } 27102082Seschrock } 27112082Seschrock 27123377Seschrock /* 27135450Sbrendan * We must validate the spares and l2cache devices after checking the 27145450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 27153377Seschrock */ 27165450Sbrendan if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) { 27173377Seschrock spa->spa_pending_vdev = NULL; 27183377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 27193377Seschrock } 27203377Seschrock 27213377Seschrock spa->spa_pending_vdev = NULL; 27223377Seschrock 27233377Seschrock /* 27243377Seschrock * Transfer each new top-level vdev from vd to rvd. 27253377Seschrock */ 27263377Seschrock for (c = 0; c < vd->vdev_children; c++) { 27273377Seschrock tvd = vd->vdev_child[c]; 27283377Seschrock vdev_remove_child(vd, tvd); 27293377Seschrock tvd->vdev_id = rvd->vdev_children; 27303377Seschrock vdev_add_child(rvd, tvd); 27313377Seschrock vdev_config_dirty(tvd); 27323377Seschrock } 27333377Seschrock 27342082Seschrock if (nspares != 0) { 27355450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 27365450Sbrendan ZPOOL_CONFIG_SPARES); 27372082Seschrock spa_load_spares(spa); 27385450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 27395450Sbrendan } 27405450Sbrendan 27415450Sbrendan if (nl2cache != 0) { 27425450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 27435450Sbrendan ZPOOL_CONFIG_L2CACHE); 27445450Sbrendan spa_load_l2cache(spa); 27455450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2746789Sahrens } 2747789Sahrens 2748789Sahrens /* 27491585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 27501585Sbonwick * If other threads start allocating from these vdevs before we 27511585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 27521585Sbonwick * fail to open the pool because there are DVAs that the config cache 27531585Sbonwick * can't translate. Therefore, we first add the vdevs without 27541585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 27551635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 27561585Sbonwick * 27571585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 27581585Sbonwick * if we lose power at any point in this sequence, the remaining 27591585Sbonwick * steps will be completed the next time we load the pool. 2760789Sahrens */ 27611635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 27621585Sbonwick 27631635Sbonwick mutex_enter(&spa_namespace_lock); 27641635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27651635Sbonwick mutex_exit(&spa_namespace_lock); 2766789Sahrens 27671635Sbonwick return (0); 2768789Sahrens } 2769789Sahrens 2770789Sahrens /* 2771789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2772789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2773789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2774789Sahrens * 2775789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2776789Sahrens * existing device; in this case the two devices are made into their own 27774451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2778789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2779789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2780789Sahrens * completion of resilvering, the first disk (the one being replaced) 2781789Sahrens * is automatically detached. 2782789Sahrens */ 2783789Sahrens int 27841544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2785789Sahrens { 2786789Sahrens uint64_t txg, open_txg; 2787789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2788789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 27892082Seschrock vdev_ops_t *pvops; 27907313SEric.Kustarz@Sun.COM dmu_tx_t *tx; 27917313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 27927313SEric.Kustarz@Sun.COM int newvd_isspare; 27937313SEric.Kustarz@Sun.COM int error; 2794789Sahrens 2795789Sahrens txg = spa_vdev_enter(spa); 2796789Sahrens 27976643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2798789Sahrens 2799789Sahrens if (oldvd == NULL) 2800789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2801789Sahrens 28021585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 28031585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28041585Sbonwick 2805789Sahrens pvd = oldvd->vdev_parent; 2806789Sahrens 28072082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 28084451Seschrock VDEV_ALLOC_ADD)) != 0) 28094451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 28104451Seschrock 28114451Seschrock if (newrootvd->vdev_children != 1) 2812789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2813789Sahrens 2814789Sahrens newvd = newrootvd->vdev_child[0]; 2815789Sahrens 2816789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2817789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2818789Sahrens 28192082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2820789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2821789Sahrens 28224527Sperrin /* 28234527Sperrin * Spares can't replace logs 28244527Sperrin */ 28257326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 28264527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28274527Sperrin 28282082Seschrock if (!replacing) { 28292082Seschrock /* 28302082Seschrock * For attach, the only allowable parent is a mirror or the root 28312082Seschrock * vdev. 28322082Seschrock */ 28332082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 28342082Seschrock pvd->vdev_ops != &vdev_root_ops) 28352082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28362082Seschrock 28372082Seschrock pvops = &vdev_mirror_ops; 28382082Seschrock } else { 28392082Seschrock /* 28402082Seschrock * Active hot spares can only be replaced by inactive hot 28412082Seschrock * spares. 28422082Seschrock */ 28432082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 28442082Seschrock pvd->vdev_child[1] == oldvd && 28452082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 28462082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28472082Seschrock 28482082Seschrock /* 28492082Seschrock * If the source is a hot spare, and the parent isn't already a 28502082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 28513377Seschrock * want to create a replacing vdev. The user is not allowed to 28523377Seschrock * attach to a spared vdev child unless the 'isspare' state is 28533377Seschrock * the same (spare replaces spare, non-spare replaces 28543377Seschrock * non-spare). 28552082Seschrock */ 28562082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 28572082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28583377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 28593377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 28603377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28612082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 28622082Seschrock newvd->vdev_isspare) 28632082Seschrock pvops = &vdev_spare_ops; 28642082Seschrock else 28652082Seschrock pvops = &vdev_replacing_ops; 28662082Seschrock } 28672082Seschrock 28681175Slling /* 28691175Slling * Compare the new device size with the replaceable/attachable 28701175Slling * device size. 28711175Slling */ 28721175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2873789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2874789Sahrens 28751732Sbonwick /* 28761732Sbonwick * The new device cannot have a higher alignment requirement 28771732Sbonwick * than the top-level vdev. 28781732Sbonwick */ 28791732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2880789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2881789Sahrens 2882789Sahrens /* 2883789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2884789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2885789Sahrens */ 2886789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2887789Sahrens spa_strfree(oldvd->vdev_path); 2888789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2889789Sahrens KM_SLEEP); 2890789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2891789Sahrens newvd->vdev_path, "old"); 2892789Sahrens if (oldvd->vdev_devid != NULL) { 2893789Sahrens spa_strfree(oldvd->vdev_devid); 2894789Sahrens oldvd->vdev_devid = NULL; 2895789Sahrens } 2896789Sahrens } 2897789Sahrens 2898789Sahrens /* 28992082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 29002082Seschrock * mirror/replacing/spare vdev above oldvd. 2901789Sahrens */ 2902789Sahrens if (pvd->vdev_ops != pvops) 2903789Sahrens pvd = vdev_add_parent(oldvd, pvops); 2904789Sahrens 2905789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2906789Sahrens ASSERT(pvd->vdev_ops == pvops); 2907789Sahrens ASSERT(oldvd->vdev_parent == pvd); 2908789Sahrens 2909789Sahrens /* 2910789Sahrens * Extract the new device from its root and add it to pvd. 2911789Sahrens */ 2912789Sahrens vdev_remove_child(newrootvd, newvd); 2913789Sahrens newvd->vdev_id = pvd->vdev_children; 2914789Sahrens vdev_add_child(pvd, newvd); 2915789Sahrens 29161544Seschrock /* 29171544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 29181544Seschrock * the addition of newvd may have decreased our parent's asize. 29191544Seschrock */ 29201544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 29211544Seschrock 2922789Sahrens tvd = newvd->vdev_top; 2923789Sahrens ASSERT(pvd->vdev_top == tvd); 2924789Sahrens ASSERT(tvd->vdev_parent == rvd); 2925789Sahrens 2926789Sahrens vdev_config_dirty(tvd); 2927789Sahrens 2928789Sahrens /* 2929789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2930789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2931789Sahrens */ 2932789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2933789Sahrens 2934789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2935789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2936789Sahrens open_txg - TXG_INITIAL + 1); 2937789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2938789Sahrens 29393377Seschrock if (newvd->vdev_isspare) 29403377Seschrock spa_spare_activate(newvd); 29417313SEric.Kustarz@Sun.COM oldvdpath = spa_strdup(vdev_description(oldvd)); 29427313SEric.Kustarz@Sun.COM newvdpath = spa_strdup(vdev_description(newvd)); 29437313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 29441544Seschrock 2945789Sahrens /* 2946789Sahrens * Mark newvd's DTL dirty in this txg. 2947789Sahrens */ 29481732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2949789Sahrens 2950789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2951789Sahrens 29527313SEric.Kustarz@Sun.COM tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 29537313SEric.Kustarz@Sun.COM if (dmu_tx_assign(tx, TXG_WAIT) == 0) { 29547313SEric.Kustarz@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx, 29557313SEric.Kustarz@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 29567313SEric.Kustarz@Sun.COM replacing && newvd_isspare ? "spare in" : 29577313SEric.Kustarz@Sun.COM replacing ? "replace" : "attach", newvdpath, 29587313SEric.Kustarz@Sun.COM replacing ? "for" : "to", oldvdpath); 29597313SEric.Kustarz@Sun.COM dmu_tx_commit(tx); 29607313SEric.Kustarz@Sun.COM } else { 29617313SEric.Kustarz@Sun.COM dmu_tx_abort(tx); 29627313SEric.Kustarz@Sun.COM } 29637313SEric.Kustarz@Sun.COM 29647313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 29657313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 29667313SEric.Kustarz@Sun.COM 2967789Sahrens /* 29687046Sahrens * Kick off a resilver to update newvd. 2969789Sahrens */ 29707046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 2971789Sahrens 2972789Sahrens return (0); 2973789Sahrens } 2974789Sahrens 2975789Sahrens /* 2976789Sahrens * Detach a device from a mirror or replacing vdev. 2977789Sahrens * If 'replace_done' is specified, only detach if the parent 2978789Sahrens * is a replacing vdev. 2979789Sahrens */ 2980789Sahrens int 29811544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2982789Sahrens { 2983789Sahrens uint64_t txg; 2984789Sahrens int c, t, error; 2985789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2986789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 29872082Seschrock boolean_t unspare = B_FALSE; 29882082Seschrock uint64_t unspare_guid; 29896673Seschrock size_t len; 2990789Sahrens 2991789Sahrens txg = spa_vdev_enter(spa); 2992789Sahrens 29936643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 2994789Sahrens 2995789Sahrens if (vd == NULL) 2996789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2997789Sahrens 29981585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 29991585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30001585Sbonwick 3001789Sahrens pvd = vd->vdev_parent; 3002789Sahrens 3003789Sahrens /* 3004789Sahrens * If replace_done is specified, only remove this device if it's 30052082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 30062082Seschrock * disk can be removed. 3007789Sahrens */ 30082082Seschrock if (replace_done) { 30092082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 30102082Seschrock if (vd->vdev_id != 0) 30112082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30122082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 30132082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30142082Seschrock } 30152082Seschrock } 30162082Seschrock 30172082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 30184577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3019789Sahrens 3020789Sahrens /* 30212082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3022789Sahrens */ 3023789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 30242082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 30252082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3026789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3027789Sahrens 3028789Sahrens /* 3029789Sahrens * If there's only one replica, you can't detach it. 3030789Sahrens */ 3031789Sahrens if (pvd->vdev_children <= 1) 3032789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3033789Sahrens 3034789Sahrens /* 3035789Sahrens * If all siblings have non-empty DTLs, this device may have the only 3036789Sahrens * valid copy of the data, which means we cannot safely detach it. 3037789Sahrens * 3038789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 3039789Sahrens * precise DTL check. 3040789Sahrens */ 3041789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 3042789Sahrens uint64_t dirty; 3043789Sahrens 3044789Sahrens cvd = pvd->vdev_child[c]; 3045789Sahrens if (cvd == vd) 3046789Sahrens continue; 3047789Sahrens if (vdev_is_dead(cvd)) 3048789Sahrens continue; 3049789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 3050789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 3051789Sahrens cvd->vdev_dtl_scrub.sm_space; 3052789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 3053789Sahrens if (!dirty) 3054789Sahrens break; 3055789Sahrens } 30562082Seschrock 30572082Seschrock /* 30582082Seschrock * If we are a replacing or spare vdev, then we can always detach the 30592082Seschrock * latter child, as that is how one cancels the operation. 30602082Seschrock */ 30612082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 30622082Seschrock c == pvd->vdev_children) 3063789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3064789Sahrens 3065789Sahrens /* 30666673Seschrock * If we are detaching the second disk from a replacing vdev, then 30676673Seschrock * check to see if we changed the original vdev's path to have "/old" 30686673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 30696673Seschrock */ 30706673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 30716673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 30726673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 30736673Seschrock ASSERT(pvd->vdev_child[1] == vd); 30746673Seschrock cvd = pvd->vdev_child[0]; 30756673Seschrock len = strlen(vd->vdev_path); 30766673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 30776673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 30786673Seschrock spa_strfree(cvd->vdev_path); 30796673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 30806673Seschrock } 30816673Seschrock } 30826673Seschrock 30836673Seschrock /* 30842082Seschrock * If we are detaching the original disk from a spare, then it implies 30852082Seschrock * that the spare should become a real disk, and be removed from the 30862082Seschrock * active spare list for the pool. 30872082Seschrock */ 30882082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 30892082Seschrock vd->vdev_id == 0) 30902082Seschrock unspare = B_TRUE; 30912082Seschrock 30922082Seschrock /* 3093789Sahrens * Erase the disk labels so the disk can be used for other things. 3094789Sahrens * This must be done after all other error cases are handled, 3095789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3096789Sahrens * But if we can't do it, don't treat the error as fatal -- 3097789Sahrens * it may be that the unwritability of the disk is the reason 3098789Sahrens * it's being detached! 3099789Sahrens */ 31003377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3101789Sahrens 3102789Sahrens /* 3103789Sahrens * Remove vd from its parent and compact the parent's children. 3104789Sahrens */ 3105789Sahrens vdev_remove_child(pvd, vd); 3106789Sahrens vdev_compact_children(pvd); 3107789Sahrens 3108789Sahrens /* 3109789Sahrens * Remember one of the remaining children so we can get tvd below. 3110789Sahrens */ 3111789Sahrens cvd = pvd->vdev_child[0]; 3112789Sahrens 3113789Sahrens /* 31142082Seschrock * If we need to remove the remaining child from the list of hot spares, 31152082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 31162082Seschrock * must do this before vdev_remove_parent(), because that can change the 31172082Seschrock * GUID if it creates a new toplevel GUID. 31182082Seschrock */ 31192082Seschrock if (unspare) { 31202082Seschrock ASSERT(cvd->vdev_isspare); 31213377Seschrock spa_spare_remove(cvd); 31222082Seschrock unspare_guid = cvd->vdev_guid; 31232082Seschrock } 31242082Seschrock 31252082Seschrock /* 3126789Sahrens * If the parent mirror/replacing vdev only has one child, 3127789Sahrens * the parent is no longer needed. Remove it from the tree. 3128789Sahrens */ 3129789Sahrens if (pvd->vdev_children == 1) 3130789Sahrens vdev_remove_parent(cvd); 3131789Sahrens 3132789Sahrens /* 3133789Sahrens * We don't set tvd until now because the parent we just removed 3134789Sahrens * may have been the previous top-level vdev. 3135789Sahrens */ 3136789Sahrens tvd = cvd->vdev_top; 3137789Sahrens ASSERT(tvd->vdev_parent == rvd); 3138789Sahrens 3139789Sahrens /* 31403377Seschrock * Reevaluate the parent vdev state. 3141789Sahrens */ 31424451Seschrock vdev_propagate_state(cvd); 3143789Sahrens 3144789Sahrens /* 31453377Seschrock * If the device we just detached was smaller than the others, it may be 31463377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 31473377Seschrock * can't fail because the existing metaslabs are already in core, so 31483377Seschrock * there's nothing to read from disk. 3149789Sahrens */ 31501732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3151789Sahrens 3152789Sahrens vdev_config_dirty(tvd); 3153789Sahrens 3154789Sahrens /* 31553377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 31563377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 31573377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 31583377Seschrock * prevent vd from being accessed after it's freed. 3159789Sahrens */ 3160789Sahrens for (t = 0; t < TXG_SIZE; t++) 3161789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 31621732Sbonwick vd->vdev_detached = B_TRUE; 31631732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3164789Sahrens 31654451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 31664451Seschrock 31672082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 31682082Seschrock 31692082Seschrock /* 31703377Seschrock * If this was the removal of the original device in a hot spare vdev, 31713377Seschrock * then we want to go through and remove the device from the hot spare 31723377Seschrock * list of every other pool. 31732082Seschrock */ 31742082Seschrock if (unspare) { 31752082Seschrock spa = NULL; 31762082Seschrock mutex_enter(&spa_namespace_lock); 31772082Seschrock while ((spa = spa_next(spa)) != NULL) { 31782082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 31792082Seschrock continue; 31802082Seschrock 31812082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 31822082Seschrock } 31832082Seschrock mutex_exit(&spa_namespace_lock); 31842082Seschrock } 31852082Seschrock 31862082Seschrock return (error); 31872082Seschrock } 31882082Seschrock 31892082Seschrock /* 31905450Sbrendan * Remove a spares vdev from the nvlist config. 31912082Seschrock */ 31925450Sbrendan static int 31935450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare, 31945450Sbrendan nvlist_t **spares, int nspares, vdev_t *vd) 31952082Seschrock { 31965450Sbrendan nvlist_t *nv, **newspares; 31975450Sbrendan int i, j; 31982082Seschrock 31992082Seschrock nv = NULL; 32005450Sbrendan for (i = 0; i < nspares; i++) { 32015450Sbrendan uint64_t theguid; 32025450Sbrendan 32035450Sbrendan VERIFY(nvlist_lookup_uint64(spares[i], 32045450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 32055450Sbrendan if (theguid == guid) { 32065450Sbrendan nv = spares[i]; 32075450Sbrendan break; 32082082Seschrock } 32092082Seschrock } 32102082Seschrock 32112082Seschrock /* 32125450Sbrendan * Only remove the hot spare if it's not currently in use in this pool. 32132082Seschrock */ 32145450Sbrendan if (nv == NULL && vd == NULL) 32155450Sbrendan return (ENOENT); 32165450Sbrendan 32175450Sbrendan if (nv == NULL && vd != NULL) 32185450Sbrendan return (ENOTSUP); 32195450Sbrendan 32205450Sbrendan if (!unspare && nv != NULL && vd != NULL) 32215450Sbrendan return (EBUSY); 32222082Seschrock 32232082Seschrock if (nspares == 1) { 32242082Seschrock newspares = NULL; 32252082Seschrock } else { 32262082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 32272082Seschrock KM_SLEEP); 32282082Seschrock for (i = 0, j = 0; i < nspares; i++) { 32292082Seschrock if (spares[i] != nv) 32302082Seschrock VERIFY(nvlist_dup(spares[i], 32312082Seschrock &newspares[j++], KM_SLEEP) == 0); 32322082Seschrock } 32332082Seschrock } 32342082Seschrock 32355450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES, 32362082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 32375450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 32385450Sbrendan ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0); 32392082Seschrock for (i = 0; i < nspares - 1; i++) 32402082Seschrock nvlist_free(newspares[i]); 32412082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 32425450Sbrendan 32435450Sbrendan return (0); 32445450Sbrendan } 32455450Sbrendan 32465450Sbrendan /* 32475450Sbrendan * Remove an l2cache vdev from the nvlist config. 32485450Sbrendan */ 32495450Sbrendan static int 32505450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache, 32515450Sbrendan int nl2cache, vdev_t *vd) 32525450Sbrendan { 32535450Sbrendan nvlist_t *nv, **newl2cache; 32545450Sbrendan int i, j; 32555450Sbrendan 32565450Sbrendan nv = NULL; 32575450Sbrendan for (i = 0; i < nl2cache; i++) { 32585450Sbrendan uint64_t theguid; 32595450Sbrendan 32605450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 32615450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 32625450Sbrendan if (theguid == guid) { 32635450Sbrendan nv = l2cache[i]; 32645450Sbrendan break; 32655450Sbrendan } 32665450Sbrendan } 32675450Sbrendan 32685450Sbrendan if (vd == NULL) { 32695450Sbrendan for (i = 0; i < nl2cache; i++) { 32705450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) { 32715450Sbrendan vd = sav->sav_vdevs[i]; 32725450Sbrendan break; 32735450Sbrendan } 32745450Sbrendan } 32755450Sbrendan } 32765450Sbrendan 32775450Sbrendan if (nv == NULL && vd == NULL) 32785450Sbrendan return (ENOENT); 32795450Sbrendan 32805450Sbrendan if (nv == NULL && vd != NULL) 32815450Sbrendan return (ENOTSUP); 32825450Sbrendan 32835450Sbrendan if (nl2cache == 1) { 32845450Sbrendan newl2cache = NULL; 32855450Sbrendan } else { 32865450Sbrendan newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *), 32875450Sbrendan KM_SLEEP); 32885450Sbrendan for (i = 0, j = 0; i < nl2cache; i++) { 32895450Sbrendan if (l2cache[i] != nv) 32905450Sbrendan VERIFY(nvlist_dup(l2cache[i], 32915450Sbrendan &newl2cache[j++], KM_SLEEP) == 0); 32925450Sbrendan } 32935450Sbrendan } 32945450Sbrendan 32955450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 32965450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 32975450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 32985450Sbrendan ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0); 32995450Sbrendan for (i = 0; i < nl2cache - 1; i++) 33005450Sbrendan nvlist_free(newl2cache[i]); 33015450Sbrendan kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *)); 33025450Sbrendan 33035450Sbrendan return (0); 33045450Sbrendan } 33055450Sbrendan 33065450Sbrendan /* 33075450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 33085450Sbrendan * spares and level 2 ARC devices. 33095450Sbrendan */ 33105450Sbrendan int 33115450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 33125450Sbrendan { 33135450Sbrendan vdev_t *vd; 33145450Sbrendan nvlist_t **spares, **l2cache; 33155450Sbrendan uint_t nspares, nl2cache; 33165450Sbrendan int error = 0; 33175450Sbrendan 33185450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 33195450Sbrendan 33206643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 33215450Sbrendan 33225450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 33235450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 33245450Sbrendan ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 33255450Sbrendan if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare, 33265450Sbrendan spares, nspares, vd)) != 0) 33277361SBrendan.Gregg@Sun.COM goto cache; 33285450Sbrendan spa_load_spares(spa); 33295450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 33305450Sbrendan goto out; 33315450Sbrendan } 33325450Sbrendan 33337361SBrendan.Gregg@Sun.COM cache: 33345450Sbrendan if (spa->spa_l2cache.sav_vdevs != NULL && 33355450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 33365450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) { 33375450Sbrendan if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid, 33385450Sbrendan l2cache, nl2cache, vd)) != 0) 33395450Sbrendan goto out; 33405450Sbrendan spa_load_l2cache(spa); 33415450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 33425450Sbrendan } 33432082Seschrock 33442082Seschrock out: 33452082Seschrock spa_config_exit(spa, FTAG); 33465450Sbrendan return (error); 3347789Sahrens } 3348789Sahrens 3349789Sahrens /* 33504451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 33514451Seschrock * current spared, so we can detach it. 3352789Sahrens */ 33531544Seschrock static vdev_t * 33544451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3355789Sahrens { 33561544Seschrock vdev_t *newvd, *oldvd; 3357789Sahrens int c; 3358789Sahrens 33591544Seschrock for (c = 0; c < vd->vdev_children; c++) { 33604451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 33611544Seschrock if (oldvd != NULL) 33621544Seschrock return (oldvd); 33631544Seschrock } 3364789Sahrens 33654451Seschrock /* 33664451Seschrock * Check for a completed replacement. 33674451Seschrock */ 3368789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 33691544Seschrock oldvd = vd->vdev_child[0]; 33701544Seschrock newvd = vd->vdev_child[1]; 3371789Sahrens 33721544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 33731544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 33741544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 33751544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33761544Seschrock return (oldvd); 33771544Seschrock } 33781544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33791544Seschrock } 3380789Sahrens 33814451Seschrock /* 33824451Seschrock * Check for a completed resilver with the 'unspare' flag set. 33834451Seschrock */ 33844451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 33854451Seschrock newvd = vd->vdev_child[0]; 33864451Seschrock oldvd = vd->vdev_child[1]; 33874451Seschrock 33884451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 33894451Seschrock if (newvd->vdev_unspare && 33904451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 33914451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 33924451Seschrock newvd->vdev_unspare = 0; 33934451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33944451Seschrock return (oldvd); 33954451Seschrock } 33964451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33974451Seschrock } 33984451Seschrock 33991544Seschrock return (NULL); 3400789Sahrens } 3401789Sahrens 34021544Seschrock static void 34034451Seschrock spa_vdev_resilver_done(spa_t *spa) 3404789Sahrens { 34051544Seschrock vdev_t *vd; 34062082Seschrock vdev_t *pvd; 34071544Seschrock uint64_t guid; 34082082Seschrock uint64_t pguid = 0; 3409789Sahrens 34101544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3411789Sahrens 34124451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 34131544Seschrock guid = vd->vdev_guid; 34142082Seschrock /* 34152082Seschrock * If we have just finished replacing a hot spared device, then 34162082Seschrock * we need to detach the parent's first child (the original hot 34172082Seschrock * spare) as well. 34182082Seschrock */ 34192082Seschrock pvd = vd->vdev_parent; 34202082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 34212082Seschrock pvd->vdev_id == 0) { 34222082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 34232082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 34242082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 34252082Seschrock } 34261544Seschrock spa_config_exit(spa, FTAG); 34271544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 34281544Seschrock return; 34292082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 34302082Seschrock return; 34311544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3432789Sahrens } 3433789Sahrens 34341544Seschrock spa_config_exit(spa, FTAG); 3435789Sahrens } 3436789Sahrens 3437789Sahrens /* 34381354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 34391354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 34401354Seschrock */ 34411354Seschrock int 34421354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 34431354Seschrock { 34446643Seschrock vdev_t *vd; 34451354Seschrock uint64_t txg; 34461354Seschrock 34471354Seschrock txg = spa_vdev_enter(spa); 34481354Seschrock 34496643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 34502082Seschrock /* 34516643Seschrock * Determine if this is a reference to a hot spare device. If 34526643Seschrock * it is, update the path manually as there is no associated 34536643Seschrock * vdev_t that can be synced to disk. 34542082Seschrock */ 34556643Seschrock nvlist_t **spares; 34566643Seschrock uint_t i, nspares; 34575450Sbrendan 34585450Sbrendan if (spa->spa_spares.sav_config != NULL) { 34595450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 34605450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 34615450Sbrendan &spares, &nspares) == 0); 34622082Seschrock for (i = 0; i < nspares; i++) { 34632082Seschrock uint64_t theguid; 34642082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 34652082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 34665450Sbrendan if (theguid == guid) { 34675450Sbrendan VERIFY(nvlist_add_string(spares[i], 34685450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 34695450Sbrendan spa_load_spares(spa); 34705450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 34715450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 34725450Sbrendan 0)); 34735450Sbrendan } 34742082Seschrock } 34752082Seschrock } 34765450Sbrendan 34775450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 34782082Seschrock } 34791354Seschrock 34801585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 34811585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 34821585Sbonwick 34831354Seschrock spa_strfree(vd->vdev_path); 34841354Seschrock vd->vdev_path = spa_strdup(newpath); 34851354Seschrock 34861354Seschrock vdev_config_dirty(vd->vdev_top); 34871354Seschrock 34881354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 34891354Seschrock } 34901354Seschrock 34911354Seschrock /* 3492789Sahrens * ========================================================================== 3493789Sahrens * SPA Scrubbing 3494789Sahrens * ========================================================================== 3495789Sahrens */ 3496789Sahrens 34977046Sahrens int 34987046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3499789Sahrens { 35004808Sek110237 ASSERT(!spa_config_held(spa, RW_WRITER)); 35014808Sek110237 3502789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3503789Sahrens return (ENOTSUP); 3504789Sahrens 3505789Sahrens /* 35067046Sahrens * If a resilver was requested, but there is no DTL on a 35077046Sahrens * writeable leaf device, we have nothing to do. 3508789Sahrens */ 35097046Sahrens if (type == POOL_SCRUB_RESILVER && 35107046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 35117046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 35121544Seschrock return (0); 35131544Seschrock } 3514789Sahrens 35157046Sahrens if (type == POOL_SCRUB_EVERYTHING && 35167046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 35177046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 35187046Sahrens return (EBUSY); 35197046Sahrens 35207046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 35217046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 35227046Sahrens } else if (type == POOL_SCRUB_NONE) { 35237046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 35241544Seschrock } else { 35257046Sahrens return (EINVAL); 35261544Seschrock } 3527789Sahrens } 3528789Sahrens 35291544Seschrock /* 35301544Seschrock * ========================================================================== 35311544Seschrock * SPA async task processing 35321544Seschrock * ========================================================================== 35331544Seschrock */ 35341544Seschrock 35351544Seschrock static void 35364451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3537789Sahrens { 35381544Seschrock int c; 35391544Seschrock 35407361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 35417361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 35427361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 35437361SBrendan.Gregg@Sun.COM vdev_clear(spa, vd, B_TRUE); 35447361SBrendan.Gregg@Sun.COM vdev_config_dirty(vd->vdev_top); 35451544Seschrock } 35467361SBrendan.Gregg@Sun.COM 35477361SBrendan.Gregg@Sun.COM for (c = 0; c < vd->vdev_children; c++) 35487361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 35491544Seschrock } 35501544Seschrock 35511544Seschrock static void 35521544Seschrock spa_async_thread(spa_t *spa) 35531544Seschrock { 35547361SBrendan.Gregg@Sun.COM int tasks, i; 35554451Seschrock uint64_t txg; 35561544Seschrock 35571544Seschrock ASSERT(spa->spa_sync_on); 3558789Sahrens 35591544Seschrock mutex_enter(&spa->spa_async_lock); 35601544Seschrock tasks = spa->spa_async_tasks; 35611544Seschrock spa->spa_async_tasks = 0; 35621544Seschrock mutex_exit(&spa->spa_async_lock); 35631544Seschrock 35641544Seschrock /* 35651635Sbonwick * See if the config needs to be updated. 35661635Sbonwick */ 35671635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 35681635Sbonwick mutex_enter(&spa_namespace_lock); 35691635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 35701635Sbonwick mutex_exit(&spa_namespace_lock); 35711635Sbonwick } 35721635Sbonwick 35731635Sbonwick /* 35744451Seschrock * See if any devices need to be marked REMOVED. 35755329Sgw25295 * 35765329Sgw25295 * XXX - We avoid doing this when we are in 35775329Sgw25295 * I/O failure state since spa_vdev_enter() grabs 35785329Sgw25295 * the namespace lock and would not be able to obtain 35795329Sgw25295 * the writer config lock. 35801544Seschrock */ 35815329Sgw25295 if (tasks & SPA_ASYNC_REMOVE && 35825329Sgw25295 spa_state(spa) != POOL_STATE_IO_FAILURE) { 35834451Seschrock txg = spa_vdev_enter(spa); 35844451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 35857361SBrendan.Gregg@Sun.COM for (i = 0; i < spa->spa_l2cache.sav_count; i++) 35867361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 35877361SBrendan.Gregg@Sun.COM for (i = 0; i < spa->spa_spares.sav_count; i++) 35887361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 35894451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 35904451Seschrock } 35911544Seschrock 35921544Seschrock /* 35931544Seschrock * If any devices are done replacing, detach them. 35941544Seschrock */ 35954451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 35964451Seschrock spa_vdev_resilver_done(spa); 3597789Sahrens 35981544Seschrock /* 35991544Seschrock * Kick off a resilver. 36001544Seschrock */ 36017046Sahrens if (tasks & SPA_ASYNC_RESILVER) 36027046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 36031544Seschrock 36041544Seschrock /* 36051544Seschrock * Let the world know that we're done. 36061544Seschrock */ 36071544Seschrock mutex_enter(&spa->spa_async_lock); 36081544Seschrock spa->spa_async_thread = NULL; 36091544Seschrock cv_broadcast(&spa->spa_async_cv); 36101544Seschrock mutex_exit(&spa->spa_async_lock); 36111544Seschrock thread_exit(); 36121544Seschrock } 36131544Seschrock 36141544Seschrock void 36151544Seschrock spa_async_suspend(spa_t *spa) 36161544Seschrock { 36171544Seschrock mutex_enter(&spa->spa_async_lock); 36181544Seschrock spa->spa_async_suspended++; 36191544Seschrock while (spa->spa_async_thread != NULL) 36201544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 36211544Seschrock mutex_exit(&spa->spa_async_lock); 36221544Seschrock } 36231544Seschrock 36241544Seschrock void 36251544Seschrock spa_async_resume(spa_t *spa) 36261544Seschrock { 36271544Seschrock mutex_enter(&spa->spa_async_lock); 36281544Seschrock ASSERT(spa->spa_async_suspended != 0); 36291544Seschrock spa->spa_async_suspended--; 36301544Seschrock mutex_exit(&spa->spa_async_lock); 36311544Seschrock } 36321544Seschrock 36331544Seschrock static void 36341544Seschrock spa_async_dispatch(spa_t *spa) 36351544Seschrock { 36361544Seschrock mutex_enter(&spa->spa_async_lock); 36371544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 36381635Sbonwick spa->spa_async_thread == NULL && 36391635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 36401544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 36411544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 36421544Seschrock mutex_exit(&spa->spa_async_lock); 36431544Seschrock } 36441544Seschrock 36451544Seschrock void 36461544Seschrock spa_async_request(spa_t *spa, int task) 36471544Seschrock { 36481544Seschrock mutex_enter(&spa->spa_async_lock); 36491544Seschrock spa->spa_async_tasks |= task; 36501544Seschrock mutex_exit(&spa->spa_async_lock); 3651789Sahrens } 3652789Sahrens 3653789Sahrens /* 3654789Sahrens * ========================================================================== 3655789Sahrens * SPA syncing routines 3656789Sahrens * ========================================================================== 3657789Sahrens */ 3658789Sahrens 3659789Sahrens static void 3660789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3661789Sahrens { 3662789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3663789Sahrens dmu_tx_t *tx; 3664789Sahrens blkptr_t blk; 3665789Sahrens uint64_t itor = 0; 3666789Sahrens zio_t *zio; 3667789Sahrens int error; 3668789Sahrens uint8_t c = 1; 3669789Sahrens 3670789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3671789Sahrens 3672789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3673789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3674789Sahrens 3675789Sahrens error = zio_wait(zio); 3676789Sahrens ASSERT3U(error, ==, 0); 3677789Sahrens 3678789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3679789Sahrens bplist_vacate(bpl, tx); 3680789Sahrens 3681789Sahrens /* 3682789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3683789Sahrens * (Usually only the first block is needed.) 3684789Sahrens */ 3685789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3686789Sahrens dmu_tx_commit(tx); 3687789Sahrens } 3688789Sahrens 3689789Sahrens static void 36902082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 36912082Seschrock { 36922082Seschrock char *packed = NULL; 3693*7497STim.Haley@Sun.COM size_t bufsize; 36942082Seschrock size_t nvsize = 0; 36952082Seschrock dmu_buf_t *db; 36962082Seschrock 36972082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 36982082Seschrock 3699*7497STim.Haley@Sun.COM /* 3700*7497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 3701*7497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 3702*7497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 3703*7497STim.Haley@Sun.COM */ 3704*7497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 3705*7497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 37062082Seschrock 37072082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 37082082Seschrock KM_SLEEP) == 0); 3709*7497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 3710*7497STim.Haley@Sun.COM 3711*7497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 3712*7497STim.Haley@Sun.COM 3713*7497STim.Haley@Sun.COM kmem_free(packed, bufsize); 37142082Seschrock 37152082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 37162082Seschrock dmu_buf_will_dirty(db, tx); 37172082Seschrock *(uint64_t *)db->db_data = nvsize; 37182082Seschrock dmu_buf_rele(db, FTAG); 37192082Seschrock } 37202082Seschrock 37212082Seschrock static void 37225450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 37235450Sbrendan const char *config, const char *entry) 37242082Seschrock { 37252082Seschrock nvlist_t *nvroot; 37265450Sbrendan nvlist_t **list; 37272082Seschrock int i; 37282082Seschrock 37295450Sbrendan if (!sav->sav_sync) 37302082Seschrock return; 37312082Seschrock 37322082Seschrock /* 37335450Sbrendan * Update the MOS nvlist describing the list of available devices. 37345450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 37354451Seschrock * valid and the vdevs are labeled appropriately. 37362082Seschrock */ 37375450Sbrendan if (sav->sav_object == 0) { 37385450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 37395450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 37405450Sbrendan sizeof (uint64_t), tx); 37412082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 37425450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 37435450Sbrendan &sav->sav_object, tx) == 0); 37442082Seschrock } 37452082Seschrock 37462082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 37475450Sbrendan if (sav->sav_count == 0) { 37485450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 37492082Seschrock } else { 37505450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 37515450Sbrendan for (i = 0; i < sav->sav_count; i++) 37525450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 37535450Sbrendan B_FALSE, B_FALSE, B_TRUE); 37545450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 37555450Sbrendan sav->sav_count) == 0); 37565450Sbrendan for (i = 0; i < sav->sav_count; i++) 37575450Sbrendan nvlist_free(list[i]); 37585450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 37592082Seschrock } 37602082Seschrock 37615450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 37622926Sek110237 nvlist_free(nvroot); 37632082Seschrock 37645450Sbrendan sav->sav_sync = B_FALSE; 37652082Seschrock } 37662082Seschrock 37672082Seschrock static void 3768789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3769789Sahrens { 3770789Sahrens nvlist_t *config; 3771789Sahrens 3772789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 3773789Sahrens return; 3774789Sahrens 3775789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 3776789Sahrens 37771635Sbonwick if (spa->spa_config_syncing) 37781635Sbonwick nvlist_free(spa->spa_config_syncing); 37791635Sbonwick spa->spa_config_syncing = config; 3780789Sahrens 37812082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3782789Sahrens } 3783789Sahrens 37845094Slling /* 37855094Slling * Set zpool properties. 37865094Slling */ 37873912Slling static void 37884543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 37893912Slling { 37903912Slling spa_t *spa = arg1; 37915094Slling objset_t *mos = spa->spa_meta_objset; 37923912Slling nvlist_t *nvp = arg2; 37935094Slling nvpair_t *elem; 37944451Seschrock uint64_t intval; 37956643Seschrock char *strval; 37965094Slling zpool_prop_t prop; 37975094Slling const char *propname; 37985094Slling zprop_type_t proptype; 37996643Seschrock spa_config_dirent_t *dp; 38005094Slling 38015094Slling elem = NULL; 38025094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 38035094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 38045094Slling case ZPOOL_PROP_VERSION: 38055094Slling /* 38065094Slling * Only set version for non-zpool-creation cases 38075094Slling * (set/import). spa_create() needs special care 38085094Slling * for version setting. 38095094Slling */ 38105094Slling if (tx->tx_txg != TXG_INITIAL) { 38115094Slling VERIFY(nvpair_value_uint64(elem, 38125094Slling &intval) == 0); 38135094Slling ASSERT(intval <= SPA_VERSION); 38145094Slling ASSERT(intval >= spa_version(spa)); 38155094Slling spa->spa_uberblock.ub_version = intval; 38165094Slling vdev_config_dirty(spa->spa_root_vdev); 38175094Slling } 38185094Slling break; 38195094Slling 38205094Slling case ZPOOL_PROP_ALTROOT: 38215094Slling /* 38225094Slling * 'altroot' is a non-persistent property. It should 38235094Slling * have been set temporarily at creation or import time. 38245094Slling */ 38255094Slling ASSERT(spa->spa_root != NULL); 38265094Slling break; 38275094Slling 38285363Seschrock case ZPOOL_PROP_CACHEFILE: 38295094Slling /* 38305363Seschrock * 'cachefile' is a non-persistent property, but note 38315363Seschrock * an async request that the config cache needs to be 38325363Seschrock * udpated. 38335094Slling */ 38345363Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 38356643Seschrock 38366643Seschrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 38376643Seschrock KM_SLEEP); 38386643Seschrock 38396643Seschrock if (strval[0] == '\0') 38406643Seschrock dp->scd_path = spa_strdup(spa_config_path); 38416643Seschrock else if (strcmp(strval, "none") == 0) 38426643Seschrock dp->scd_path = NULL; 38436643Seschrock else 38446643Seschrock dp->scd_path = spa_strdup(strval); 38456643Seschrock 38466643Seschrock list_insert_head(&spa->spa_config_list, dp); 38475363Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 38484543Smarks break; 38495094Slling default: 38505094Slling /* 38515094Slling * Set pool property values in the poolprops mos object. 38525094Slling */ 38535094Slling mutex_enter(&spa->spa_props_lock); 38545094Slling if (spa->spa_pool_props_object == 0) { 38555094Slling objset_t *mos = spa->spa_meta_objset; 38565094Slling 38575094Slling VERIFY((spa->spa_pool_props_object = 38585094Slling zap_create(mos, DMU_OT_POOL_PROPS, 38595094Slling DMU_OT_NONE, 0, tx)) > 0); 38605094Slling 38615094Slling VERIFY(zap_update(mos, 38625094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 38635094Slling 8, 1, &spa->spa_pool_props_object, tx) 38645094Slling == 0); 38655094Slling } 38665094Slling mutex_exit(&spa->spa_props_lock); 38675094Slling 38685094Slling /* normalize the property name */ 38695094Slling propname = zpool_prop_to_name(prop); 38705094Slling proptype = zpool_prop_get_type(prop); 38715094Slling 38725094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 38735094Slling ASSERT(proptype == PROP_TYPE_STRING); 38745094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 38755094Slling VERIFY(zap_update(mos, 38765094Slling spa->spa_pool_props_object, propname, 38775094Slling 1, strlen(strval) + 1, strval, tx) == 0); 38785094Slling 38795094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 38805094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 38815094Slling 38825094Slling if (proptype == PROP_TYPE_INDEX) { 38835094Slling const char *unused; 38845094Slling VERIFY(zpool_prop_index_to_string( 38855094Slling prop, intval, &unused) == 0); 38865094Slling } 38875094Slling VERIFY(zap_update(mos, 38885094Slling spa->spa_pool_props_object, propname, 38895094Slling 8, 1, &intval, tx) == 0); 38905094Slling } else { 38915094Slling ASSERT(0); /* not allowed */ 38925094Slling } 38935094Slling 38945329Sgw25295 switch (prop) { 38955329Sgw25295 case ZPOOL_PROP_DELEGATION: 38965094Slling spa->spa_delegation = intval; 38975329Sgw25295 break; 38985329Sgw25295 case ZPOOL_PROP_BOOTFS: 38995094Slling spa->spa_bootfs = intval; 39005329Sgw25295 break; 39015329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 39025329Sgw25295 spa->spa_failmode = intval; 39035329Sgw25295 break; 39045329Sgw25295 default: 39055329Sgw25295 break; 39065329Sgw25295 } 39073912Slling } 39085094Slling 39095094Slling /* log internal history if this is not a zpool create */ 39105094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 39115094Slling tx->tx_txg != TXG_INITIAL) { 39125094Slling spa_history_internal_log(LOG_POOL_PROPSET, 39135094Slling spa, tx, cr, "%s %lld %s", 39145094Slling nvpair_name(elem), intval, spa->spa_name); 39155094Slling } 39163912Slling } 39173912Slling } 39183912Slling 3919789Sahrens /* 3920789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3921789Sahrens * part of the process, so we iterate until it converges. 3922789Sahrens */ 3923789Sahrens void 3924789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3925789Sahrens { 3926789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3927789Sahrens objset_t *mos = spa->spa_meta_objset; 3928789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 39291635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3930789Sahrens vdev_t *vd; 3931789Sahrens dmu_tx_t *tx; 3932789Sahrens int dirty_vdevs; 3933789Sahrens 3934789Sahrens /* 3935789Sahrens * Lock out configuration changes. 3936789Sahrens */ 39371544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3938789Sahrens 3939789Sahrens spa->spa_syncing_txg = txg; 3940789Sahrens spa->spa_sync_pass = 0; 3941789Sahrens 39421544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3943789Sahrens 39442082Seschrock tx = dmu_tx_create_assigned(dp, txg); 39452082Seschrock 39462082Seschrock /* 39474577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 39482082Seschrock * set spa_deflate if we have no raid-z vdevs. 39492082Seschrock */ 39504577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 39514577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 39522082Seschrock int i; 39532082Seschrock 39542082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 39552082Seschrock vd = rvd->vdev_child[i]; 39562082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 39572082Seschrock break; 39582082Seschrock } 39592082Seschrock if (i == rvd->vdev_children) { 39602082Seschrock spa->spa_deflate = TRUE; 39612082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 39622082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 39632082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 39642082Seschrock } 39652082Seschrock } 39662082Seschrock 39677046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 39687046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 39697046Sahrens dsl_pool_create_origin(dp, tx); 39707046Sahrens 39717046Sahrens /* Keeping the origin open increases spa_minref */ 39727046Sahrens spa->spa_minref += 3; 39737046Sahrens } 39747046Sahrens 39757046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 39767046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 39777046Sahrens dsl_pool_upgrade_clones(dp, tx); 39787046Sahrens } 39797046Sahrens 3980789Sahrens /* 3981789Sahrens * If anything has changed in this txg, push the deferred frees 3982789Sahrens * from the previous txg. If not, leave them alone so that we 3983789Sahrens * don't generate work on an otherwise idle system. 3984789Sahrens */ 3985789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 39862329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 39872329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3988789Sahrens spa_sync_deferred_frees(spa, txg); 3989789Sahrens 3990789Sahrens /* 3991789Sahrens * Iterate to convergence. 3992789Sahrens */ 3993789Sahrens do { 3994789Sahrens spa->spa_sync_pass++; 3995789Sahrens 3996789Sahrens spa_sync_config_object(spa, tx); 39975450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 39985450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 39995450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 40005450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 40011544Seschrock spa_errlog_sync(spa, txg); 4002789Sahrens dsl_pool_sync(dp, txg); 4003789Sahrens 4004789Sahrens dirty_vdevs = 0; 4005789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4006789Sahrens vdev_sync(vd, txg); 4007789Sahrens dirty_vdevs++; 4008789Sahrens } 4009789Sahrens 4010789Sahrens bplist_sync(bpl, tx); 4011789Sahrens } while (dirty_vdevs); 4012789Sahrens 4013789Sahrens bplist_close(bpl); 4014789Sahrens 4015789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4016789Sahrens 4017789Sahrens /* 4018789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4019789Sahrens * to commit the transaction group. 40201635Sbonwick * 40215688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 40225688Sbonwick * random top-level vdevs that are known to be visible in the 40235688Sbonwick * config cache (see spa_vdev_add() for details). If there *are* 40245688Sbonwick * dirty vdevs -- or if the sync to our random subset fails -- 40255688Sbonwick * then sync the uberblock to all vdevs. 4026789Sahrens */ 40275688Sbonwick if (list_is_empty(&spa->spa_dirty_list)) { 40286615Sgw25295 vdev_t *svd[SPA_DVAS_PER_BP]; 40296615Sgw25295 int svdcount = 0; 40301635Sbonwick int children = rvd->vdev_children; 40311635Sbonwick int c0 = spa_get_random(children); 40321635Sbonwick int c; 40331635Sbonwick 40341635Sbonwick for (c = 0; c < children; c++) { 40351635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 40365688Sbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 40371635Sbonwick continue; 40385688Sbonwick svd[svdcount++] = vd; 40395688Sbonwick if (svdcount == SPA_DVAS_PER_BP) 40401635Sbonwick break; 40411635Sbonwick } 40426615Sgw25295 vdev_config_sync(svd, svdcount, txg); 40436615Sgw25295 } else { 40446615Sgw25295 vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg); 40451635Sbonwick } 40462082Seschrock dmu_tx_commit(tx); 40472082Seschrock 40481635Sbonwick /* 40491635Sbonwick * Clear the dirty config list. 40501635Sbonwick */ 40511635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 40521635Sbonwick vdev_config_clean(vd); 40531635Sbonwick 40541635Sbonwick /* 40551635Sbonwick * Now that the new config has synced transactionally, 40561635Sbonwick * let it become visible to the config cache. 40571635Sbonwick */ 40581635Sbonwick if (spa->spa_config_syncing != NULL) { 40591635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 40601635Sbonwick spa->spa_config_txg = txg; 40611635Sbonwick spa->spa_config_syncing = NULL; 40621635Sbonwick } 4063789Sahrens 40647046Sahrens spa->spa_traverse_wanted = B_TRUE; 4065789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 40667046Sahrens spa->spa_traverse_wanted = B_FALSE; 4067789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4068789Sahrens rw_exit(&spa->spa_traverse_lock); 4069789Sahrens 4070789Sahrens /* 4071789Sahrens * Clean up the ZIL records for the synced txg. 4072789Sahrens */ 4073789Sahrens dsl_pool_zil_clean(dp); 4074789Sahrens 4075789Sahrens /* 4076789Sahrens * Update usable space statistics. 4077789Sahrens */ 4078789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4079789Sahrens vdev_sync_done(vd, txg); 4080789Sahrens 4081789Sahrens /* 4082789Sahrens * It had better be the case that we didn't dirty anything 40832082Seschrock * since vdev_config_sync(). 4084789Sahrens */ 4085789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4086789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4087789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4088789Sahrens ASSERT(bpl->bpl_queue == NULL); 4089789Sahrens 40901544Seschrock spa_config_exit(spa, FTAG); 40911544Seschrock 40921544Seschrock /* 40931544Seschrock * If any async tasks have been requested, kick them off. 40941544Seschrock */ 40951544Seschrock spa_async_dispatch(spa); 4096789Sahrens } 4097789Sahrens 4098789Sahrens /* 4099789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4100789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4101789Sahrens * sync. 4102789Sahrens */ 4103789Sahrens void 4104789Sahrens spa_sync_allpools(void) 4105789Sahrens { 4106789Sahrens spa_t *spa = NULL; 4107789Sahrens mutex_enter(&spa_namespace_lock); 4108789Sahrens while ((spa = spa_next(spa)) != NULL) { 4109789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 4110789Sahrens continue; 4111789Sahrens spa_open_ref(spa, FTAG); 4112789Sahrens mutex_exit(&spa_namespace_lock); 4113789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4114789Sahrens mutex_enter(&spa_namespace_lock); 4115789Sahrens spa_close(spa, FTAG); 4116789Sahrens } 4117789Sahrens mutex_exit(&spa_namespace_lock); 4118789Sahrens } 4119789Sahrens 4120789Sahrens /* 4121789Sahrens * ========================================================================== 4122789Sahrens * Miscellaneous routines 4123789Sahrens * ========================================================================== 4124789Sahrens */ 4125789Sahrens 4126789Sahrens /* 4127789Sahrens * Remove all pools in the system. 4128789Sahrens */ 4129789Sahrens void 4130789Sahrens spa_evict_all(void) 4131789Sahrens { 4132789Sahrens spa_t *spa; 4133789Sahrens 4134789Sahrens /* 4135789Sahrens * Remove all cached state. All pools should be closed now, 4136789Sahrens * so every spa in the AVL tree should be unreferenced. 4137789Sahrens */ 4138789Sahrens mutex_enter(&spa_namespace_lock); 4139789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4140789Sahrens /* 41411544Seschrock * Stop async tasks. The async thread may need to detach 41421544Seschrock * a device that's been replaced, which requires grabbing 41431544Seschrock * spa_namespace_lock, so we must drop it here. 4144789Sahrens */ 4145789Sahrens spa_open_ref(spa, FTAG); 4146789Sahrens mutex_exit(&spa_namespace_lock); 41471544Seschrock spa_async_suspend(spa); 41484808Sek110237 mutex_enter(&spa_namespace_lock); 4149789Sahrens spa_close(spa, FTAG); 4150789Sahrens 4151789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4152789Sahrens spa_unload(spa); 4153789Sahrens spa_deactivate(spa); 4154789Sahrens } 4155789Sahrens spa_remove(spa); 4156789Sahrens } 4157789Sahrens mutex_exit(&spa_namespace_lock); 4158789Sahrens } 41591544Seschrock 41601544Seschrock vdev_t * 41616643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 41621544Seschrock { 41636643Seschrock vdev_t *vd; 41646643Seschrock int i; 41656643Seschrock 41666643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 41676643Seschrock return (vd); 41686643Seschrock 41696643Seschrock if (l2cache) { 41706643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 41716643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 41726643Seschrock if (vd->vdev_guid == guid) 41736643Seschrock return (vd); 41746643Seschrock } 41756643Seschrock } 41766643Seschrock 41776643Seschrock return (NULL); 41781544Seschrock } 41791760Seschrock 41801760Seschrock void 41815094Slling spa_upgrade(spa_t *spa, uint64_t version) 41821760Seschrock { 41831760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 41841760Seschrock 41851760Seschrock /* 41861760Seschrock * This should only be called for a non-faulted pool, and since a 41871760Seschrock * future version would result in an unopenable pool, this shouldn't be 41881760Seschrock * possible. 41891760Seschrock */ 41904577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 41915094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 41925094Slling 41935094Slling spa->spa_uberblock.ub_version = version; 41941760Seschrock vdev_config_dirty(spa->spa_root_vdev); 41951760Seschrock 41961760Seschrock spa_config_exit(spa, FTAG); 41972082Seschrock 41982082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 41991760Seschrock } 42002082Seschrock 42012082Seschrock boolean_t 42022082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 42032082Seschrock { 42042082Seschrock int i; 42053377Seschrock uint64_t spareguid; 42065450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 42075450Sbrendan 42085450Sbrendan for (i = 0; i < sav->sav_count; i++) 42095450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 42102082Seschrock return (B_TRUE); 42112082Seschrock 42125450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 42135450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 42145450Sbrendan &spareguid) == 0 && spareguid == guid) 42153377Seschrock return (B_TRUE); 42163377Seschrock } 42173377Seschrock 42182082Seschrock return (B_FALSE); 42192082Seschrock } 42203912Slling 42214451Seschrock /* 42227214Slling * Check if a pool has an active shared spare device. 42237214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 42247214Slling */ 42257214Slling static boolean_t 42267214Slling spa_has_active_shared_spare(spa_t *spa) 42277214Slling { 42287214Slling int i, refcnt; 42297214Slling uint64_t pool; 42307214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 42317214Slling 42327214Slling for (i = 0; i < sav->sav_count; i++) { 42337214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 42347214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 42357214Slling refcnt > 2) 42367214Slling return (B_TRUE); 42377214Slling } 42387214Slling 42397214Slling return (B_FALSE); 42407214Slling } 42417214Slling 42427214Slling /* 42434451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 42444451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 42454451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 42464451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 42474451Seschrock * or zdb as real changes. 42484451Seschrock */ 42494451Seschrock void 42504451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 42514451Seschrock { 42524451Seschrock #ifdef _KERNEL 42534451Seschrock sysevent_t *ev; 42544451Seschrock sysevent_attr_list_t *attr = NULL; 42554451Seschrock sysevent_value_t value; 42564451Seschrock sysevent_id_t eid; 42574451Seschrock 42584451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 42594451Seschrock SE_SLEEP); 42604451Seschrock 42614451Seschrock value.value_type = SE_DATA_TYPE_STRING; 42624451Seschrock value.value.sv_string = spa_name(spa); 42634451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 42644451Seschrock goto done; 42654451Seschrock 42664451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 42674451Seschrock value.value.sv_uint64 = spa_guid(spa); 42684451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 42694451Seschrock goto done; 42704451Seschrock 42714451Seschrock if (vd) { 42724451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 42734451Seschrock value.value.sv_uint64 = vd->vdev_guid; 42744451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 42754451Seschrock SE_SLEEP) != 0) 42764451Seschrock goto done; 42774451Seschrock 42784451Seschrock if (vd->vdev_path) { 42794451Seschrock value.value_type = SE_DATA_TYPE_STRING; 42804451Seschrock value.value.sv_string = vd->vdev_path; 42814451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 42824451Seschrock &value, SE_SLEEP) != 0) 42834451Seschrock goto done; 42844451Seschrock } 42854451Seschrock } 42864451Seschrock 42875756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 42885756Seschrock goto done; 42895756Seschrock attr = NULL; 42905756Seschrock 42914451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 42924451Seschrock 42934451Seschrock done: 42944451Seschrock if (attr) 42954451Seschrock sysevent_free_attr(attr); 42964451Seschrock sysevent_free(ev); 42974451Seschrock #endif 42984451Seschrock } 4299