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 667754SJeff.Bonwick@Sun.COM int zio_taskq_threads[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 677754SJeff.Bonwick@Sun.COM /* ISSUE INTR */ 687754SJeff.Bonwick@Sun.COM { 1, 1 }, /* ZIO_TYPE_NULL */ 697754SJeff.Bonwick@Sun.COM { 1, 8 }, /* ZIO_TYPE_READ */ 707754SJeff.Bonwick@Sun.COM { 8, 1 }, /* ZIO_TYPE_WRITE */ 717754SJeff.Bonwick@Sun.COM { 1, 1 }, /* ZIO_TYPE_FREE */ 727754SJeff.Bonwick@Sun.COM { 1, 1 }, /* ZIO_TYPE_CLAIM */ 737754SJeff.Bonwick@Sun.COM { 1, 1 }, /* ZIO_TYPE_IOCTL */ 747754SJeff.Bonwick@Sun.COM }; 752986Sek110237 765094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 777214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa); 785094Slling 795094Slling /* 805094Slling * ========================================================================== 815094Slling * SPA properties routines 825094Slling * ========================================================================== 835094Slling */ 845094Slling 855094Slling /* 865094Slling * Add a (source=src, propname=propval) list to an nvlist. 875094Slling */ 885949Slling static void 895094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 905094Slling uint64_t intval, zprop_source_t src) 915094Slling { 925094Slling const char *propname = zpool_prop_to_name(prop); 935094Slling nvlist_t *propval; 945949Slling 955949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 965949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 975949Slling 985949Slling if (strval != NULL) 995949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1005949Slling else 1015949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 1025949Slling 1035949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 1045094Slling nvlist_free(propval); 1055094Slling } 1065094Slling 1075094Slling /* 1085094Slling * Get property values from the spa configuration. 1095094Slling */ 1105949Slling static void 1115094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1125094Slling { 1135094Slling uint64_t size = spa_get_space(spa); 1145094Slling uint64_t used = spa_get_alloc(spa); 1155094Slling uint64_t cap, version; 1165094Slling zprop_source_t src = ZPROP_SRC_NONE; 1176643Seschrock spa_config_dirent_t *dp; 1185094Slling 1197754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 1207754SJeff.Bonwick@Sun.COM 1215094Slling /* 1225094Slling * readonly properties 1235094Slling */ 1247754SJeff.Bonwick@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 1255949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 1265949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 1275949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src); 1285094Slling 1295094Slling cap = (size == 0) ? 0 : (used * 100 / size); 1305949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1315949Slling 1325949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1335949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1345949Slling spa->spa_root_vdev->vdev_state, src); 1355094Slling 1365094Slling /* 1375094Slling * settable properties that are not stored in the pool property object. 1385094Slling */ 1395094Slling version = spa_version(spa); 1405094Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1415094Slling src = ZPROP_SRC_DEFAULT; 1425094Slling else 1435094Slling src = ZPROP_SRC_LOCAL; 1445949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1455949Slling 1465949Slling if (spa->spa_root != NULL) 1475949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1485949Slling 0, ZPROP_SRC_LOCAL); 1495094Slling 1506643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 1516643Seschrock if (dp->scd_path == NULL) { 1525949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1536643Seschrock "none", 0, ZPROP_SRC_LOCAL); 1546643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1555949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1566643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1575363Seschrock } 1585363Seschrock } 1595094Slling } 1605094Slling 1615094Slling /* 1625094Slling * Get zpool property values. 1635094Slling */ 1645094Slling int 1655094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 1665094Slling { 1675094Slling zap_cursor_t zc; 1685094Slling zap_attribute_t za; 1695094Slling objset_t *mos = spa->spa_meta_objset; 1705094Slling int err; 1715094Slling 1725949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1735094Slling 1747754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 1757754SJeff.Bonwick@Sun.COM 1765094Slling /* 1775094Slling * Get properties from the spa config. 1785094Slling */ 1795949Slling spa_prop_get_config(spa, nvp); 1805094Slling 1815094Slling /* If no pool property object, no more prop to get. */ 1825094Slling if (spa->spa_pool_props_object == 0) { 1835094Slling mutex_exit(&spa->spa_props_lock); 1845094Slling return (0); 1855094Slling } 1865094Slling 1875094Slling /* 1885094Slling * Get properties from the MOS pool property object. 1895094Slling */ 1905094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 1915094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 1925094Slling zap_cursor_advance(&zc)) { 1935094Slling uint64_t intval = 0; 1945094Slling char *strval = NULL; 1955094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 1965094Slling zpool_prop_t prop; 1975094Slling 1985094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 1995094Slling continue; 2005094Slling 2015094Slling switch (za.za_integer_length) { 2025094Slling case 8: 2035094Slling /* integer property */ 2045094Slling if (za.za_first_integer != 2055094Slling zpool_prop_default_numeric(prop)) 2065094Slling src = ZPROP_SRC_LOCAL; 2075094Slling 2085094Slling if (prop == ZPOOL_PROP_BOOTFS) { 2095094Slling dsl_pool_t *dp; 2105094Slling dsl_dataset_t *ds = NULL; 2115094Slling 2125094Slling dp = spa_get_dsl(spa); 2135094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2146689Smaybee if (err = dsl_dataset_hold_obj(dp, 2156689Smaybee za.za_first_integer, FTAG, &ds)) { 2165094Slling rw_exit(&dp->dp_config_rwlock); 2175094Slling break; 2185094Slling } 2195094Slling 2205094Slling strval = kmem_alloc( 2215094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2225094Slling KM_SLEEP); 2235094Slling dsl_dataset_name(ds, strval); 2246689Smaybee dsl_dataset_rele(ds, FTAG); 2255094Slling rw_exit(&dp->dp_config_rwlock); 2265094Slling } else { 2275094Slling strval = NULL; 2285094Slling intval = za.za_first_integer; 2295094Slling } 2305094Slling 2315949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2325094Slling 2335094Slling if (strval != NULL) 2345094Slling kmem_free(strval, 2355094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2365094Slling 2375094Slling break; 2385094Slling 2395094Slling case 1: 2405094Slling /* string property */ 2415094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2425094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2435094Slling za.za_name, 1, za.za_num_integers, strval); 2445094Slling if (err) { 2455094Slling kmem_free(strval, za.za_num_integers); 2465094Slling break; 2475094Slling } 2485949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2495094Slling kmem_free(strval, za.za_num_integers); 2505094Slling break; 2515094Slling 2525094Slling default: 2535094Slling break; 2545094Slling } 2555094Slling } 2565094Slling zap_cursor_fini(&zc); 2575094Slling mutex_exit(&spa->spa_props_lock); 2585094Slling out: 2595094Slling if (err && err != ENOENT) { 2605094Slling nvlist_free(*nvp); 2615949Slling *nvp = NULL; 2625094Slling return (err); 2635094Slling } 2645094Slling 2655094Slling return (0); 2665094Slling } 2675094Slling 2685094Slling /* 2695094Slling * Validate the given pool properties nvlist and modify the list 2705094Slling * for the property values to be set. 2715094Slling */ 2725094Slling static int 2735094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 2745094Slling { 2755094Slling nvpair_t *elem; 2765094Slling int error = 0, reset_bootfs = 0; 2775094Slling uint64_t objnum; 2785094Slling 2795094Slling elem = NULL; 2805094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2815094Slling zpool_prop_t prop; 2825094Slling char *propname, *strval; 2835094Slling uint64_t intval; 2845094Slling objset_t *os; 2855363Seschrock char *slash; 2865094Slling 2875094Slling propname = nvpair_name(elem); 2885094Slling 2895094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 2905094Slling return (EINVAL); 2915094Slling 2925094Slling switch (prop) { 2935094Slling case ZPOOL_PROP_VERSION: 2945094Slling error = nvpair_value_uint64(elem, &intval); 2955094Slling if (!error && 2965094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 2975094Slling error = EINVAL; 2985094Slling break; 2995094Slling 3005094Slling case ZPOOL_PROP_DELEGATION: 3015094Slling case ZPOOL_PROP_AUTOREPLACE: 3027538SRichard.Morris@Sun.COM case ZPOOL_PROP_LISTSNAPS: 3035094Slling error = nvpair_value_uint64(elem, &intval); 3045094Slling if (!error && intval > 1) 3055094Slling error = EINVAL; 3065094Slling break; 3075094Slling 3085094Slling case ZPOOL_PROP_BOOTFS: 3095094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3105094Slling error = ENOTSUP; 3115094Slling break; 3125094Slling } 3135094Slling 3145094Slling /* 3157042Sgw25295 * Make sure the vdev config is bootable 3165094Slling */ 3177042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3185094Slling error = ENOTSUP; 3195094Slling break; 3205094Slling } 3215094Slling 3225094Slling reset_bootfs = 1; 3235094Slling 3245094Slling error = nvpair_value_string(elem, &strval); 3255094Slling 3265094Slling if (!error) { 3277042Sgw25295 uint64_t compress; 3287042Sgw25295 3295094Slling if (strval == NULL || strval[0] == '\0') { 3305094Slling objnum = zpool_prop_default_numeric( 3315094Slling ZPOOL_PROP_BOOTFS); 3325094Slling break; 3335094Slling } 3345094Slling 3355094Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 3366689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os)) 3375094Slling break; 3387042Sgw25295 3397042Sgw25295 /* We don't support gzip bootable datasets */ 3407042Sgw25295 if ((error = dsl_prop_get_integer(strval, 3417042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3427042Sgw25295 &compress, NULL)) == 0 && 3437042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3447042Sgw25295 error = ENOTSUP; 3457042Sgw25295 } else { 3467042Sgw25295 objnum = dmu_objset_id(os); 3477042Sgw25295 } 3485094Slling dmu_objset_close(os); 3495094Slling } 3505094Slling break; 3517754SJeff.Bonwick@Sun.COM 3525329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3535329Sgw25295 error = nvpair_value_uint64(elem, &intval); 3545329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3555329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 3565329Sgw25295 error = EINVAL; 3575329Sgw25295 3585329Sgw25295 /* 3595329Sgw25295 * This is a special case which only occurs when 3605329Sgw25295 * the pool has completely failed. This allows 3615329Sgw25295 * the user to change the in-core failmode property 3625329Sgw25295 * without syncing it out to disk (I/Os might 3635329Sgw25295 * currently be blocked). We do this by returning 3645329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 3655329Sgw25295 * into thinking we encountered a property validation 3665329Sgw25295 * error. 3675329Sgw25295 */ 3687754SJeff.Bonwick@Sun.COM if (!error && spa_suspended(spa)) { 3695329Sgw25295 spa->spa_failmode = intval; 3705329Sgw25295 error = EIO; 3715329Sgw25295 } 3725329Sgw25295 break; 3735363Seschrock 3745363Seschrock case ZPOOL_PROP_CACHEFILE: 3755363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3765363Seschrock break; 3775363Seschrock 3785363Seschrock if (strval[0] == '\0') 3795363Seschrock break; 3805363Seschrock 3815363Seschrock if (strcmp(strval, "none") == 0) 3825363Seschrock break; 3835363Seschrock 3845363Seschrock if (strval[0] != '/') { 3855363Seschrock error = EINVAL; 3865363Seschrock break; 3875363Seschrock } 3885363Seschrock 3895363Seschrock slash = strrchr(strval, '/'); 3905363Seschrock ASSERT(slash != NULL); 3915363Seschrock 3925363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3935363Seschrock strcmp(slash, "/..") == 0) 3945363Seschrock error = EINVAL; 3955363Seschrock break; 3965094Slling } 3975094Slling 3985094Slling if (error) 3995094Slling break; 4005094Slling } 4015094Slling 4025094Slling if (!error && reset_bootfs) { 4035094Slling error = nvlist_remove(props, 4045094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 4055094Slling 4065094Slling if (!error) { 4075094Slling error = nvlist_add_uint64(props, 4085094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 4095094Slling } 4105094Slling } 4115094Slling 4125094Slling return (error); 4135094Slling } 4145094Slling 4155094Slling int 4165094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4175094Slling { 4185094Slling int error; 4195094Slling 4205094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 4215094Slling return (error); 4225094Slling 4235094Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 4245094Slling spa, nvp, 3)); 4255094Slling } 4265094Slling 4275094Slling /* 4285094Slling * If the bootfs property value is dsobj, clear it. 4295094Slling */ 4305094Slling void 4315094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 4325094Slling { 4335094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 4345094Slling VERIFY(zap_remove(spa->spa_meta_objset, 4355094Slling spa->spa_pool_props_object, 4365094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 4375094Slling spa->spa_bootfs = 0; 4385094Slling } 4395094Slling } 4405094Slling 441789Sahrens /* 442789Sahrens * ========================================================================== 443789Sahrens * SPA state manipulation (open/create/destroy/import/export) 444789Sahrens * ========================================================================== 445789Sahrens */ 446789Sahrens 4471544Seschrock static int 4481544Seschrock spa_error_entry_compare(const void *a, const void *b) 4491544Seschrock { 4501544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 4511544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 4521544Seschrock int ret; 4531544Seschrock 4541544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 4551544Seschrock sizeof (zbookmark_t)); 4561544Seschrock 4571544Seschrock if (ret < 0) 4581544Seschrock return (-1); 4591544Seschrock else if (ret > 0) 4601544Seschrock return (1); 4611544Seschrock else 4621544Seschrock return (0); 4631544Seschrock } 4641544Seschrock 4651544Seschrock /* 4661544Seschrock * Utility function which retrieves copies of the current logs and 4671544Seschrock * re-initializes them in the process. 4681544Seschrock */ 4691544Seschrock void 4701544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 4711544Seschrock { 4721544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 4731544Seschrock 4741544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 4751544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 4761544Seschrock 4771544Seschrock avl_create(&spa->spa_errlist_scrub, 4781544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4791544Seschrock offsetof(spa_error_entry_t, se_avl)); 4801544Seschrock avl_create(&spa->spa_errlist_last, 4811544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4821544Seschrock offsetof(spa_error_entry_t, se_avl)); 4831544Seschrock } 4841544Seschrock 485789Sahrens /* 486789Sahrens * Activate an uninitialized pool. 487789Sahrens */ 488789Sahrens static void 4898241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode) 490789Sahrens { 491789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 492789Sahrens 493789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 4948241SJeff.Bonwick@Sun.COM spa->spa_mode = mode; 495789Sahrens 496789Sahrens spa->spa_normal_class = metaslab_class_create(); 4974527Sperrin spa->spa_log_class = metaslab_class_create(); 498789Sahrens 4997754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 5007754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5017754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create("spa_zio", 5027754SJeff.Bonwick@Sun.COM zio_taskq_threads[t][q], maxclsyspri, 50, 5037754SJeff.Bonwick@Sun.COM INT_MAX, TASKQ_PREPOPULATE); 5047754SJeff.Bonwick@Sun.COM } 505789Sahrens } 506789Sahrens 5077754SJeff.Bonwick@Sun.COM list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 5087754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_config_dirty_node)); 5097754SJeff.Bonwick@Sun.COM list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 5107754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_state_dirty_node)); 511789Sahrens 512789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 513789Sahrens offsetof(struct vdev, vdev_txg_node)); 5141544Seschrock 5151544Seschrock avl_create(&spa->spa_errlist_scrub, 5161544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5171544Seschrock offsetof(spa_error_entry_t, se_avl)); 5181544Seschrock avl_create(&spa->spa_errlist_last, 5191544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5201544Seschrock offsetof(spa_error_entry_t, se_avl)); 521789Sahrens } 522789Sahrens 523789Sahrens /* 524789Sahrens * Opposite of spa_activate(). 525789Sahrens */ 526789Sahrens static void 527789Sahrens spa_deactivate(spa_t *spa) 528789Sahrens { 529789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 530789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 531789Sahrens ASSERT(spa->spa_root_vdev == NULL); 532789Sahrens 533789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 534789Sahrens 535789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 536789Sahrens 5377754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_config_dirty_list); 5387754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_state_dirty_list); 5397754SJeff.Bonwick@Sun.COM 5407754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 5417754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5427754SJeff.Bonwick@Sun.COM taskq_destroy(spa->spa_zio_taskq[t][q]); 5437754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 5447754SJeff.Bonwick@Sun.COM } 545789Sahrens } 546789Sahrens 547789Sahrens metaslab_class_destroy(spa->spa_normal_class); 548789Sahrens spa->spa_normal_class = NULL; 549789Sahrens 5504527Sperrin metaslab_class_destroy(spa->spa_log_class); 5514527Sperrin spa->spa_log_class = NULL; 5524527Sperrin 5531544Seschrock /* 5541544Seschrock * If this was part of an import or the open otherwise failed, we may 5551544Seschrock * still have errors left in the queues. Empty them just in case. 5561544Seschrock */ 5571544Seschrock spa_errlog_drain(spa); 5581544Seschrock 5591544Seschrock avl_destroy(&spa->spa_errlist_scrub); 5601544Seschrock avl_destroy(&spa->spa_errlist_last); 5611544Seschrock 562789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 563789Sahrens } 564789Sahrens 565789Sahrens /* 566789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 567789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 568789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 569789Sahrens * All vdev validation is done by the vdev_alloc() routine. 570789Sahrens */ 5712082Seschrock static int 5722082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 5732082Seschrock uint_t id, int atype) 574789Sahrens { 575789Sahrens nvlist_t **child; 576789Sahrens uint_t c, children; 5772082Seschrock int error; 5782082Seschrock 5792082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 5802082Seschrock return (error); 5812082Seschrock 5822082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 5832082Seschrock return (0); 584789Sahrens 5857754SJeff.Bonwick@Sun.COM error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 5867754SJeff.Bonwick@Sun.COM &child, &children); 5877754SJeff.Bonwick@Sun.COM 5887754SJeff.Bonwick@Sun.COM if (error == ENOENT) 5897754SJeff.Bonwick@Sun.COM return (0); 5907754SJeff.Bonwick@Sun.COM 5917754SJeff.Bonwick@Sun.COM if (error) { 5922082Seschrock vdev_free(*vdp); 5932082Seschrock *vdp = NULL; 5942082Seschrock return (EINVAL); 595789Sahrens } 596789Sahrens 597789Sahrens for (c = 0; c < children; c++) { 5982082Seschrock vdev_t *vd; 5992082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 6002082Seschrock atype)) != 0) { 6012082Seschrock vdev_free(*vdp); 6022082Seschrock *vdp = NULL; 6032082Seschrock return (error); 604789Sahrens } 605789Sahrens } 606789Sahrens 6072082Seschrock ASSERT(*vdp != NULL); 6082082Seschrock 6092082Seschrock return (0); 610789Sahrens } 611789Sahrens 612789Sahrens /* 613789Sahrens * Opposite of spa_load(). 614789Sahrens */ 615789Sahrens static void 616789Sahrens spa_unload(spa_t *spa) 617789Sahrens { 6182082Seschrock int i; 6192082Seschrock 6207754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6217754SJeff.Bonwick@Sun.COM 622789Sahrens /* 6231544Seschrock * Stop async tasks. 6241544Seschrock */ 6251544Seschrock spa_async_suspend(spa); 6261544Seschrock 6271544Seschrock /* 628789Sahrens * Stop syncing. 629789Sahrens */ 630789Sahrens if (spa->spa_sync_on) { 631789Sahrens txg_sync_stop(spa->spa_dsl_pool); 632789Sahrens spa->spa_sync_on = B_FALSE; 633789Sahrens } 634789Sahrens 635789Sahrens /* 6367754SJeff.Bonwick@Sun.COM * Wait for any outstanding async I/O to complete. 637789Sahrens */ 6387754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_async_root_lock); 6397754SJeff.Bonwick@Sun.COM while (spa->spa_async_root_count != 0) 6407754SJeff.Bonwick@Sun.COM cv_wait(&spa->spa_async_root_cv, &spa->spa_async_root_lock); 6417754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_async_root_lock); 642789Sahrens 643789Sahrens /* 644789Sahrens * Close the dsl pool. 645789Sahrens */ 646789Sahrens if (spa->spa_dsl_pool) { 647789Sahrens dsl_pool_close(spa->spa_dsl_pool); 648789Sahrens spa->spa_dsl_pool = NULL; 649789Sahrens } 650789Sahrens 6518241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6528241SJeff.Bonwick@Sun.COM 6538241SJeff.Bonwick@Sun.COM /* 6548241SJeff.Bonwick@Sun.COM * Drop and purge level 2 cache 6558241SJeff.Bonwick@Sun.COM */ 6568241SJeff.Bonwick@Sun.COM spa_l2cache_drop(spa); 6578241SJeff.Bonwick@Sun.COM 658789Sahrens /* 659789Sahrens * Close all vdevs. 660789Sahrens */ 6611585Sbonwick if (spa->spa_root_vdev) 662789Sahrens vdev_free(spa->spa_root_vdev); 6631585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 6641544Seschrock 6655450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 6665450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 6675450Sbrendan if (spa->spa_spares.sav_vdevs) { 6685450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 6695450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 6705450Sbrendan spa->spa_spares.sav_vdevs = NULL; 6715450Sbrendan } 6725450Sbrendan if (spa->spa_spares.sav_config) { 6735450Sbrendan nvlist_free(spa->spa_spares.sav_config); 6745450Sbrendan spa->spa_spares.sav_config = NULL; 6752082Seschrock } 6767377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 6775450Sbrendan 6785450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 6795450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 6805450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 6815450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 6825450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 6835450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 6845450Sbrendan } 6855450Sbrendan if (spa->spa_l2cache.sav_config) { 6865450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 6875450Sbrendan spa->spa_l2cache.sav_config = NULL; 6882082Seschrock } 6897377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 6902082Seschrock 6911544Seschrock spa->spa_async_suspended = 0; 6928241SJeff.Bonwick@Sun.COM 6938241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 694789Sahrens } 695789Sahrens 696789Sahrens /* 6972082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 6982082Seschrock * this pool. When this is called, we have some form of basic information in 6995450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 7005450Sbrendan * then re-generate a more complete list including status information. 7012082Seschrock */ 7022082Seschrock static void 7032082Seschrock spa_load_spares(spa_t *spa) 7042082Seschrock { 7052082Seschrock nvlist_t **spares; 7062082Seschrock uint_t nspares; 7072082Seschrock int i; 7083377Seschrock vdev_t *vd, *tvd; 7092082Seschrock 7107754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 7117754SJeff.Bonwick@Sun.COM 7122082Seschrock /* 7132082Seschrock * First, close and free any existing spare vdevs. 7142082Seschrock */ 7155450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7165450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 7173377Seschrock 7183377Seschrock /* Undo the call to spa_activate() below */ 7196643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7206643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 7213377Seschrock spa_spare_remove(tvd); 7223377Seschrock vdev_close(vd); 7233377Seschrock vdev_free(vd); 7242082Seschrock } 7253377Seschrock 7265450Sbrendan if (spa->spa_spares.sav_vdevs) 7275450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7285450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7295450Sbrendan 7305450Sbrendan if (spa->spa_spares.sav_config == NULL) 7312082Seschrock nspares = 0; 7322082Seschrock else 7335450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 7342082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 7352082Seschrock 7365450Sbrendan spa->spa_spares.sav_count = (int)nspares; 7375450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7382082Seschrock 7392082Seschrock if (nspares == 0) 7402082Seschrock return; 7412082Seschrock 7422082Seschrock /* 7432082Seschrock * Construct the array of vdevs, opening them to get status in the 7443377Seschrock * process. For each spare, there is potentially two different vdev_t 7453377Seschrock * structures associated with it: one in the list of spares (used only 7463377Seschrock * for basic validation purposes) and one in the active vdev 7473377Seschrock * configuration (if it's spared in). During this phase we open and 7483377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 7493377Seschrock * active configuration, then we also mark this vdev as an active spare. 7502082Seschrock */ 7515450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 7525450Sbrendan KM_SLEEP); 7535450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7542082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 7552082Seschrock VDEV_ALLOC_SPARE) == 0); 7562082Seschrock ASSERT(vd != NULL); 7572082Seschrock 7585450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 7592082Seschrock 7606643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7616643Seschrock B_FALSE)) != NULL) { 7623377Seschrock if (!tvd->vdev_isspare) 7633377Seschrock spa_spare_add(tvd); 7643377Seschrock 7653377Seschrock /* 7663377Seschrock * We only mark the spare active if we were successfully 7673377Seschrock * able to load the vdev. Otherwise, importing a pool 7683377Seschrock * with a bad active spare would result in strange 7693377Seschrock * behavior, because multiple pool would think the spare 7703377Seschrock * is actively in use. 7713377Seschrock * 7723377Seschrock * There is a vulnerability here to an equally bizarre 7733377Seschrock * circumstance, where a dead active spare is later 7743377Seschrock * brought back to life (onlined or otherwise). Given 7753377Seschrock * the rarity of this scenario, and the extra complexity 7763377Seschrock * it adds, we ignore the possibility. 7773377Seschrock */ 7783377Seschrock if (!vdev_is_dead(tvd)) 7793377Seschrock spa_spare_activate(tvd); 7803377Seschrock } 7813377Seschrock 7827754SJeff.Bonwick@Sun.COM vd->vdev_top = vd; 7837754SJeff.Bonwick@Sun.COM 7842082Seschrock if (vdev_open(vd) != 0) 7852082Seschrock continue; 7862082Seschrock 7875450Sbrendan if (vdev_validate_aux(vd) == 0) 7885450Sbrendan spa_spare_add(vd); 7892082Seschrock } 7902082Seschrock 7912082Seschrock /* 7922082Seschrock * Recompute the stashed list of spares, with status information 7932082Seschrock * this time. 7942082Seschrock */ 7955450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 7962082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 7972082Seschrock 7985450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 7995450Sbrendan KM_SLEEP); 8005450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 8015450Sbrendan spares[i] = vdev_config_generate(spa, 8025450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 8035450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 8045450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 8055450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 8062082Seschrock nvlist_free(spares[i]); 8075450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 8085450Sbrendan } 8095450Sbrendan 8105450Sbrendan /* 8115450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 8125450Sbrendan * this pool. When this is called, we have some form of basic information in 8135450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 8145450Sbrendan * then re-generate a more complete list including status information. 8155450Sbrendan * Devices which are already active have their details maintained, and are 8165450Sbrendan * not re-opened. 8175450Sbrendan */ 8185450Sbrendan static void 8195450Sbrendan spa_load_l2cache(spa_t *spa) 8205450Sbrendan { 8215450Sbrendan nvlist_t **l2cache; 8225450Sbrendan uint_t nl2cache; 8235450Sbrendan int i, j, oldnvdevs; 8246643Seschrock uint64_t guid, size; 8255450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 8265450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 8275450Sbrendan 8287754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 8297754SJeff.Bonwick@Sun.COM 8305450Sbrendan if (sav->sav_config != NULL) { 8315450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 8325450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 8335450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 8345450Sbrendan } else { 8355450Sbrendan nl2cache = 0; 8365450Sbrendan } 8375450Sbrendan 8385450Sbrendan oldvdevs = sav->sav_vdevs; 8395450Sbrendan oldnvdevs = sav->sav_count; 8405450Sbrendan sav->sav_vdevs = NULL; 8415450Sbrendan sav->sav_count = 0; 8425450Sbrendan 8435450Sbrendan /* 8445450Sbrendan * Process new nvlist of vdevs. 8455450Sbrendan */ 8465450Sbrendan for (i = 0; i < nl2cache; i++) { 8475450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 8485450Sbrendan &guid) == 0); 8495450Sbrendan 8505450Sbrendan newvdevs[i] = NULL; 8515450Sbrendan for (j = 0; j < oldnvdevs; j++) { 8525450Sbrendan vd = oldvdevs[j]; 8535450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 8545450Sbrendan /* 8555450Sbrendan * Retain previous vdev for add/remove ops. 8565450Sbrendan */ 8575450Sbrendan newvdevs[i] = vd; 8585450Sbrendan oldvdevs[j] = NULL; 8595450Sbrendan break; 8605450Sbrendan } 8615450Sbrendan } 8625450Sbrendan 8635450Sbrendan if (newvdevs[i] == NULL) { 8645450Sbrendan /* 8655450Sbrendan * Create new vdev 8665450Sbrendan */ 8675450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 8685450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 8695450Sbrendan ASSERT(vd != NULL); 8705450Sbrendan newvdevs[i] = vd; 8715450Sbrendan 8725450Sbrendan /* 8735450Sbrendan * Commit this vdev as an l2cache device, 8745450Sbrendan * even if it fails to open. 8755450Sbrendan */ 8765450Sbrendan spa_l2cache_add(vd); 8775450Sbrendan 8786643Seschrock vd->vdev_top = vd; 8796643Seschrock vd->vdev_aux = sav; 8806643Seschrock 8816643Seschrock spa_l2cache_activate(vd); 8826643Seschrock 8835450Sbrendan if (vdev_open(vd) != 0) 8845450Sbrendan continue; 8855450Sbrendan 8865450Sbrendan (void) vdev_validate_aux(vd); 8875450Sbrendan 8885450Sbrendan if (!vdev_is_dead(vd)) { 8895450Sbrendan size = vdev_get_rsize(vd); 8906643Seschrock l2arc_add_vdev(spa, vd, 8916643Seschrock VDEV_LABEL_START_SIZE, 8926643Seschrock size - VDEV_LABEL_START_SIZE); 8935450Sbrendan } 8945450Sbrendan } 8955450Sbrendan } 8965450Sbrendan 8975450Sbrendan /* 8985450Sbrendan * Purge vdevs that were dropped 8995450Sbrendan */ 9005450Sbrendan for (i = 0; i < oldnvdevs; i++) { 9015450Sbrendan uint64_t pool; 9025450Sbrendan 9035450Sbrendan vd = oldvdevs[i]; 9045450Sbrendan if (vd != NULL) { 9058241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 9068241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 9075450Sbrendan l2arc_remove_vdev(vd); 9085450Sbrendan (void) vdev_close(vd); 9095450Sbrendan spa_l2cache_remove(vd); 9105450Sbrendan } 9115450Sbrendan } 9125450Sbrendan 9135450Sbrendan if (oldvdevs) 9145450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 9155450Sbrendan 9165450Sbrendan if (sav->sav_config == NULL) 9175450Sbrendan goto out; 9185450Sbrendan 9195450Sbrendan sav->sav_vdevs = newvdevs; 9205450Sbrendan sav->sav_count = (int)nl2cache; 9215450Sbrendan 9225450Sbrendan /* 9235450Sbrendan * Recompute the stashed list of l2cache devices, with status 9245450Sbrendan * information this time. 9255450Sbrendan */ 9265450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 9275450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 9285450Sbrendan 9295450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 9305450Sbrendan for (i = 0; i < sav->sav_count; i++) 9315450Sbrendan l2cache[i] = vdev_config_generate(spa, 9325450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 9335450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 9345450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 9355450Sbrendan out: 9365450Sbrendan for (i = 0; i < sav->sav_count; i++) 9375450Sbrendan nvlist_free(l2cache[i]); 9385450Sbrendan if (sav->sav_count) 9395450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 9402082Seschrock } 9412082Seschrock 9422082Seschrock static int 9432082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 9442082Seschrock { 9452082Seschrock dmu_buf_t *db; 9462082Seschrock char *packed = NULL; 9472082Seschrock size_t nvsize = 0; 9482082Seschrock int error; 9492082Seschrock *value = NULL; 9502082Seschrock 9512082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 9522082Seschrock nvsize = *(uint64_t *)db->db_data; 9532082Seschrock dmu_buf_rele(db, FTAG); 9542082Seschrock 9552082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 9562082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 9572082Seschrock if (error == 0) 9582082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 9592082Seschrock kmem_free(packed, nvsize); 9602082Seschrock 9612082Seschrock return (error); 9622082Seschrock } 9632082Seschrock 9642082Seschrock /* 9654451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9664451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9674451Seschrock */ 9684451Seschrock static void 9694451Seschrock spa_check_removed(vdev_t *vd) 9704451Seschrock { 9714451Seschrock int c; 9724451Seschrock 9734451Seschrock for (c = 0; c < vd->vdev_children; c++) 9744451Seschrock spa_check_removed(vd->vdev_child[c]); 9754451Seschrock 9764451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9774451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9784451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9794451Seschrock } 9804451Seschrock } 9814451Seschrock 9824451Seschrock /* 9837294Sperrin * Check for missing log devices 9847294Sperrin */ 9857294Sperrin int 9867294Sperrin spa_check_logs(spa_t *spa) 9877294Sperrin { 9887294Sperrin switch (spa->spa_log_state) { 9897294Sperrin case SPA_LOG_MISSING: 9907294Sperrin /* need to recheck in case slog has been restored */ 9917294Sperrin case SPA_LOG_UNKNOWN: 9927294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 9937294Sperrin DS_FIND_CHILDREN)) { 9947294Sperrin spa->spa_log_state = SPA_LOG_MISSING; 9957294Sperrin return (1); 9967294Sperrin } 9977294Sperrin break; 9987294Sperrin 9997294Sperrin case SPA_LOG_CLEAR: 10007294Sperrin (void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL, 10017294Sperrin DS_FIND_CHILDREN); 10027294Sperrin break; 10037294Sperrin } 10047294Sperrin spa->spa_log_state = SPA_LOG_GOOD; 10057294Sperrin return (0); 10067294Sperrin } 10077294Sperrin 10087294Sperrin /* 1009789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 10101544Seschrock * source of configuration information. 1011789Sahrens */ 1012789Sahrens static int 10131544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 1014789Sahrens { 1015789Sahrens int error = 0; 1016789Sahrens nvlist_t *nvroot = NULL; 1017789Sahrens vdev_t *rvd; 1018789Sahrens uberblock_t *ub = &spa->spa_uberblock; 10191635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 1020789Sahrens uint64_t pool_guid; 10212082Seschrock uint64_t version; 10224451Seschrock uint64_t autoreplace = 0; 10238241SJeff.Bonwick@Sun.COM int orig_mode = spa->spa_mode; 10247294Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 1025789Sahrens 10268241SJeff.Bonwick@Sun.COM /* 10278241SJeff.Bonwick@Sun.COM * If this is an untrusted config, access the pool in read-only mode. 10288241SJeff.Bonwick@Sun.COM * This prevents things like resilvering recently removed devices. 10298241SJeff.Bonwick@Sun.COM */ 10308241SJeff.Bonwick@Sun.COM if (!mosconfig) 10318241SJeff.Bonwick@Sun.COM spa->spa_mode = FREAD; 10328241SJeff.Bonwick@Sun.COM 10337754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 10347754SJeff.Bonwick@Sun.COM 10351544Seschrock spa->spa_load_state = state; 10361635Sbonwick 1037789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 10381733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 10391544Seschrock error = EINVAL; 10401544Seschrock goto out; 10411544Seschrock } 1042789Sahrens 10432082Seschrock /* 10442082Seschrock * Versioning wasn't explicitly added to the label until later, so if 10452082Seschrock * it's not present treat it as the initial version. 10462082Seschrock */ 10472082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 10484577Sahrens version = SPA_VERSION_INITIAL; 10492082Seschrock 10501733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 10511733Sbonwick &spa->spa_config_txg); 10521733Sbonwick 10531635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 10541544Seschrock spa_guid_exists(pool_guid, 0)) { 10551544Seschrock error = EEXIST; 10561544Seschrock goto out; 10571544Seschrock } 1058789Sahrens 10592174Seschrock spa->spa_load_guid = pool_guid; 10602174Seschrock 1061789Sahrens /* 10622082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 10632082Seschrock * value that will be returned by spa_version() since parsing the 10642082Seschrock * configuration requires knowing the version number. 1065789Sahrens */ 10667754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 10672082Seschrock spa->spa_ubsync.ub_version = version; 10682082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 10697754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1070789Sahrens 10712082Seschrock if (error != 0) 10721544Seschrock goto out; 1073789Sahrens 10741585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1075789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1076789Sahrens 1077789Sahrens /* 1078789Sahrens * Try to open all vdevs, loading each label in the process. 1079789Sahrens */ 10807754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 10814070Smc142369 error = vdev_open(rvd); 10827754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 10834070Smc142369 if (error != 0) 10841544Seschrock goto out; 1085789Sahrens 1086789Sahrens /* 10871986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 10887754SJeff.Bonwick@Sun.COM * lock because all label I/O is done with ZIO_FLAG_CONFIG_WRITER. 10891986Seschrock */ 10908241SJeff.Bonwick@Sun.COM if (mosconfig) { 10918241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 10928241SJeff.Bonwick@Sun.COM error = vdev_validate(rvd); 10938241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 10948241SJeff.Bonwick@Sun.COM if (error != 0) 10958241SJeff.Bonwick@Sun.COM goto out; 10968241SJeff.Bonwick@Sun.COM } 10971986Seschrock 10981986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 10991986Seschrock error = ENXIO; 11001986Seschrock goto out; 11011986Seschrock } 11021986Seschrock 11031986Seschrock /* 1104789Sahrens * Find the best uberblock. 1105789Sahrens */ 11067754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1107789Sahrens 1108789Sahrens /* 1109789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1110789Sahrens */ 1111789Sahrens if (ub->ub_txg == 0) { 11121760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11131760Seschrock VDEV_AUX_CORRUPT_DATA); 11141544Seschrock error = ENXIO; 11151544Seschrock goto out; 11161544Seschrock } 11171544Seschrock 11181544Seschrock /* 11191544Seschrock * If the pool is newer than the code, we can't open it. 11201544Seschrock */ 11214577Sahrens if (ub->ub_version > SPA_VERSION) { 11221760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11231760Seschrock VDEV_AUX_VERSION_NEWER); 11241544Seschrock error = ENOTSUP; 11251544Seschrock goto out; 1126789Sahrens } 1127789Sahrens 1128789Sahrens /* 1129789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1130789Sahrens * incomplete configuration. 1131789Sahrens */ 11321732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 11331544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11341544Seschrock VDEV_AUX_BAD_GUID_SUM); 11351544Seschrock error = ENXIO; 11361544Seschrock goto out; 1137789Sahrens } 1138789Sahrens 1139789Sahrens /* 1140789Sahrens * Initialize internal SPA structures. 1141789Sahrens */ 1142789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1143789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1144789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 11451544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 11461544Seschrock if (error) { 11471544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11481544Seschrock VDEV_AUX_CORRUPT_DATA); 11491544Seschrock goto out; 11501544Seschrock } 1151789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1152789Sahrens 11531544Seschrock if (zap_lookup(spa->spa_meta_objset, 1154789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 11551544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 11561544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11571544Seschrock VDEV_AUX_CORRUPT_DATA); 11581544Seschrock error = EIO; 11591544Seschrock goto out; 11601544Seschrock } 1161789Sahrens 1162789Sahrens if (!mosconfig) { 11632082Seschrock nvlist_t *newconfig; 11643975Sek110237 uint64_t hostid; 11652082Seschrock 11662082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 11671544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11681544Seschrock VDEV_AUX_CORRUPT_DATA); 11691544Seschrock error = EIO; 11701544Seschrock goto out; 11711544Seschrock } 1172789Sahrens 11737706SLin.Ling@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig, 11747706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 11753975Sek110237 char *hostname; 11763975Sek110237 unsigned long myhostid = 0; 11773975Sek110237 11783975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 11793975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 11803975Sek110237 11813975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 11824178Slling if (hostid != 0 && myhostid != 0 && 11834178Slling (unsigned long)hostid != myhostid) { 11843975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 11853975Sek110237 "loaded as it was last accessed by " 11867706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 11873975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 11887754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 11893975Sek110237 (unsigned long)hostid); 11903975Sek110237 error = EBADF; 11913975Sek110237 goto out; 11923975Sek110237 } 11933975Sek110237 } 11943975Sek110237 1195789Sahrens spa_config_set(spa, newconfig); 1196789Sahrens spa_unload(spa); 1197789Sahrens spa_deactivate(spa); 11988241SJeff.Bonwick@Sun.COM spa_activate(spa, orig_mode); 1199789Sahrens 12001544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 12011544Seschrock } 12021544Seschrock 12031544Seschrock if (zap_lookup(spa->spa_meta_objset, 12041544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 12051544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 12061544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12071544Seschrock VDEV_AUX_CORRUPT_DATA); 12081544Seschrock error = EIO; 12091544Seschrock goto out; 1210789Sahrens } 1211789Sahrens 12121544Seschrock /* 12132082Seschrock * Load the bit that tells us to use the new accounting function 12142082Seschrock * (raid-z deflation). If we have an older pool, this will not 12152082Seschrock * be present. 12162082Seschrock */ 12172082Seschrock error = zap_lookup(spa->spa_meta_objset, 12182082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 12192082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 12202082Seschrock if (error != 0 && error != ENOENT) { 12212082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12222082Seschrock VDEV_AUX_CORRUPT_DATA); 12232082Seschrock error = EIO; 12242082Seschrock goto out; 12252082Seschrock } 12262082Seschrock 12272082Seschrock /* 12281544Seschrock * Load the persistent error log. If we have an older pool, this will 12291544Seschrock * not be present. 12301544Seschrock */ 12311544Seschrock error = zap_lookup(spa->spa_meta_objset, 12321544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 12331544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 12341807Sbonwick if (error != 0 && error != ENOENT) { 12351544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12361544Seschrock VDEV_AUX_CORRUPT_DATA); 12371544Seschrock error = EIO; 12381544Seschrock goto out; 12391544Seschrock } 12401544Seschrock 12411544Seschrock error = zap_lookup(spa->spa_meta_objset, 12421544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 12431544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 12441544Seschrock if (error != 0 && error != ENOENT) { 12451544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12461544Seschrock VDEV_AUX_CORRUPT_DATA); 12471544Seschrock error = EIO; 12481544Seschrock goto out; 12491544Seschrock } 1250789Sahrens 1251789Sahrens /* 12522926Sek110237 * Load the history object. If we have an older pool, this 12532926Sek110237 * will not be present. 12542926Sek110237 */ 12552926Sek110237 error = zap_lookup(spa->spa_meta_objset, 12562926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 12572926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 12582926Sek110237 if (error != 0 && error != ENOENT) { 12592926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12602926Sek110237 VDEV_AUX_CORRUPT_DATA); 12612926Sek110237 error = EIO; 12622926Sek110237 goto out; 12632926Sek110237 } 12642926Sek110237 12652926Sek110237 /* 12662082Seschrock * Load any hot spares for this pool. 12672082Seschrock */ 12682082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12695450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 12702082Seschrock if (error != 0 && error != ENOENT) { 12712082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12722082Seschrock VDEV_AUX_CORRUPT_DATA); 12732082Seschrock error = EIO; 12742082Seschrock goto out; 12752082Seschrock } 12762082Seschrock if (error == 0) { 12774577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 12785450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 12795450Sbrendan &spa->spa_spares.sav_config) != 0) { 12802082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12812082Seschrock VDEV_AUX_CORRUPT_DATA); 12822082Seschrock error = EIO; 12832082Seschrock goto out; 12842082Seschrock } 12852082Seschrock 12867754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12872082Seschrock spa_load_spares(spa); 12887754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 12892082Seschrock } 12902082Seschrock 12915450Sbrendan /* 12925450Sbrendan * Load any level 2 ARC devices for this pool. 12935450Sbrendan */ 12945450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12955450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 12965450Sbrendan &spa->spa_l2cache.sav_object); 12975450Sbrendan if (error != 0 && error != ENOENT) { 12985450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12995450Sbrendan VDEV_AUX_CORRUPT_DATA); 13005450Sbrendan error = EIO; 13015450Sbrendan goto out; 13025450Sbrendan } 13035450Sbrendan if (error == 0) { 13045450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 13055450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 13065450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 13075450Sbrendan vdev_set_state(rvd, B_TRUE, 13085450Sbrendan VDEV_STATE_CANT_OPEN, 13095450Sbrendan VDEV_AUX_CORRUPT_DATA); 13105450Sbrendan error = EIO; 13115450Sbrendan goto out; 13125450Sbrendan } 13135450Sbrendan 13147754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13155450Sbrendan spa_load_l2cache(spa); 13167754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13175450Sbrendan } 13185450Sbrendan 13197294Sperrin if (spa_check_logs(spa)) { 13207294Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13217294Sperrin VDEV_AUX_BAD_LOG); 13227294Sperrin error = ENXIO; 13237294Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 13247294Sperrin goto out; 13257294Sperrin } 13267294Sperrin 13277294Sperrin 13285094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 13294543Smarks 13303912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13313912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 13323912Slling 13333912Slling if (error && error != ENOENT) { 13343912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13353912Slling VDEV_AUX_CORRUPT_DATA); 13363912Slling error = EIO; 13373912Slling goto out; 13383912Slling } 13393912Slling 13403912Slling if (error == 0) { 13413912Slling (void) zap_lookup(spa->spa_meta_objset, 13423912Slling spa->spa_pool_props_object, 13434451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 13443912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 13454451Seschrock (void) zap_lookup(spa->spa_meta_objset, 13464451Seschrock spa->spa_pool_props_object, 13474451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 13484451Seschrock sizeof (uint64_t), 1, &autoreplace); 13494543Smarks (void) zap_lookup(spa->spa_meta_objset, 13504543Smarks spa->spa_pool_props_object, 13514543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 13524543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 13535329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 13545329Sgw25295 spa->spa_pool_props_object, 13555329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 13565329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 13573912Slling } 13583912Slling 13592082Seschrock /* 13604451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 13614451Seschrock * the ZFS DE that it should not issue any faults for unopenable 13624451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 13634451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 13644451Seschrock * over. 13654451Seschrock */ 13665756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 13674451Seschrock spa_check_removed(spa->spa_root_vdev); 13684451Seschrock 13694451Seschrock /* 13701986Seschrock * Load the vdev state for all toplevel vdevs. 1371789Sahrens */ 13721986Seschrock vdev_load(rvd); 1373789Sahrens 1374789Sahrens /* 1375789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1376789Sahrens */ 13777754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1378789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 13797754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1380789Sahrens 1381789Sahrens /* 1382789Sahrens * Check the state of the root vdev. If it can't be opened, it 1383789Sahrens * indicates one or more toplevel vdevs are faulted. 1384789Sahrens */ 13851544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13861544Seschrock error = ENXIO; 13871544Seschrock goto out; 13881544Seschrock } 1389789Sahrens 13908241SJeff.Bonwick@Sun.COM if (spa_writeable(spa)) { 13911635Sbonwick dmu_tx_t *tx; 13921635Sbonwick int need_update = B_FALSE; 13938241SJeff.Bonwick@Sun.COM 13948241SJeff.Bonwick@Sun.COM ASSERT(state != SPA_LOAD_TRYIMPORT); 13951601Sbonwick 13961635Sbonwick /* 13971635Sbonwick * Claim log blocks that haven't been committed yet. 13981635Sbonwick * This must all happen in a single txg. 13991635Sbonwick */ 14001601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1401789Sahrens spa_first_txg(spa)); 14027754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 14032417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1404789Sahrens dmu_tx_commit(tx); 1405789Sahrens 1406789Sahrens spa->spa_sync_on = B_TRUE; 1407789Sahrens txg_sync_start(spa->spa_dsl_pool); 1408789Sahrens 1409789Sahrens /* 1410789Sahrens * Wait for all claims to sync. 1411789Sahrens */ 1412789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 14131585Sbonwick 14141585Sbonwick /* 14151635Sbonwick * If the config cache is stale, or we have uninitialized 14161635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 14171585Sbonwick */ 14181635Sbonwick if (config_cache_txg != spa->spa_config_txg || 14191635Sbonwick state == SPA_LOAD_IMPORT) 14201635Sbonwick need_update = B_TRUE; 14211635Sbonwick 14228241SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) 14231635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 14241635Sbonwick need_update = B_TRUE; 14251585Sbonwick 14261585Sbonwick /* 14271635Sbonwick * Update the config cache asychronously in case we're the 14281635Sbonwick * root pool, in which case the config cache isn't writable yet. 14291585Sbonwick */ 14301635Sbonwick if (need_update) 14311635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 14328241SJeff.Bonwick@Sun.COM 14338241SJeff.Bonwick@Sun.COM /* 14348241SJeff.Bonwick@Sun.COM * Check all DTLs to see if anything needs resilvering. 14358241SJeff.Bonwick@Sun.COM */ 14368241SJeff.Bonwick@Sun.COM if (vdev_resilver_needed(rvd, NULL, NULL)) 14378241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 1438789Sahrens } 1439789Sahrens 14401544Seschrock error = 0; 14411544Seschrock out: 14427046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 14432082Seschrock if (error && error != EBADF) 14447294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 14451544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 14461544Seschrock spa->spa_ena = 0; 14471544Seschrock 14481544Seschrock return (error); 1449789Sahrens } 1450789Sahrens 1451789Sahrens /* 1452789Sahrens * Pool Open/Import 1453789Sahrens * 1454789Sahrens * The import case is identical to an open except that the configuration is sent 1455789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1456789Sahrens * case of an open, the pool configuration will exist in the 14574451Seschrock * POOL_STATE_UNINITIALIZED state. 1458789Sahrens * 1459789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1460789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1461789Sahrens * ambiguous state. 1462789Sahrens */ 1463789Sahrens static int 1464789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1465789Sahrens { 1466789Sahrens spa_t *spa; 1467789Sahrens int error; 1468789Sahrens int locked = B_FALSE; 1469789Sahrens 1470789Sahrens *spapp = NULL; 1471789Sahrens 1472789Sahrens /* 1473789Sahrens * As disgusting as this is, we need to support recursive calls to this 1474789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1475789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1476789Sahrens * avoid dsl_dir_open() calling this in the first place. 1477789Sahrens */ 1478789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1479789Sahrens mutex_enter(&spa_namespace_lock); 1480789Sahrens locked = B_TRUE; 1481789Sahrens } 1482789Sahrens 1483789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1484789Sahrens if (locked) 1485789Sahrens mutex_exit(&spa_namespace_lock); 1486789Sahrens return (ENOENT); 1487789Sahrens } 1488789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1489789Sahrens 14908241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1491789Sahrens 14921635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1493789Sahrens 1494789Sahrens if (error == EBADF) { 1495789Sahrens /* 14961986Seschrock * If vdev_validate() returns failure (indicated by 14971986Seschrock * EBADF), it indicates that one of the vdevs indicates 14981986Seschrock * that the pool has been exported or destroyed. If 14991986Seschrock * this is the case, the config cache is out of sync and 15001986Seschrock * we should remove the pool from the namespace. 1501789Sahrens */ 1502789Sahrens spa_unload(spa); 1503789Sahrens spa_deactivate(spa); 15046643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1505789Sahrens spa_remove(spa); 1506789Sahrens if (locked) 1507789Sahrens mutex_exit(&spa_namespace_lock); 1508789Sahrens return (ENOENT); 15091544Seschrock } 15101544Seschrock 15111544Seschrock if (error) { 1512789Sahrens /* 1513789Sahrens * We can't open the pool, but we still have useful 1514789Sahrens * information: the state of each vdev after the 1515789Sahrens * attempted vdev_open(). Return this to the user. 1516789Sahrens */ 15177754SJeff.Bonwick@Sun.COM if (config != NULL && spa->spa_root_vdev != NULL) 1518789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1519789Sahrens B_TRUE); 1520789Sahrens spa_unload(spa); 1521789Sahrens spa_deactivate(spa); 15221544Seschrock spa->spa_last_open_failed = B_TRUE; 1523789Sahrens if (locked) 1524789Sahrens mutex_exit(&spa_namespace_lock); 1525789Sahrens *spapp = NULL; 1526789Sahrens return (error); 15271544Seschrock } else { 15281544Seschrock spa->spa_last_open_failed = B_FALSE; 1529789Sahrens } 1530789Sahrens } 1531789Sahrens 1532789Sahrens spa_open_ref(spa, tag); 15334451Seschrock 1534789Sahrens if (locked) 1535789Sahrens mutex_exit(&spa_namespace_lock); 1536789Sahrens 1537789Sahrens *spapp = spa; 1538789Sahrens 15397754SJeff.Bonwick@Sun.COM if (config != NULL) 1540789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1541789Sahrens 1542789Sahrens return (0); 1543789Sahrens } 1544789Sahrens 1545789Sahrens int 1546789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1547789Sahrens { 1548789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1549789Sahrens } 1550789Sahrens 15511544Seschrock /* 15521544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 15531544Seschrock * preventing it from being exported or destroyed. 15541544Seschrock */ 15551544Seschrock spa_t * 15561544Seschrock spa_inject_addref(char *name) 15571544Seschrock { 15581544Seschrock spa_t *spa; 15591544Seschrock 15601544Seschrock mutex_enter(&spa_namespace_lock); 15611544Seschrock if ((spa = spa_lookup(name)) == NULL) { 15621544Seschrock mutex_exit(&spa_namespace_lock); 15631544Seschrock return (NULL); 15641544Seschrock } 15651544Seschrock spa->spa_inject_ref++; 15661544Seschrock mutex_exit(&spa_namespace_lock); 15671544Seschrock 15681544Seschrock return (spa); 15691544Seschrock } 15701544Seschrock 15711544Seschrock void 15721544Seschrock spa_inject_delref(spa_t *spa) 15731544Seschrock { 15741544Seschrock mutex_enter(&spa_namespace_lock); 15751544Seschrock spa->spa_inject_ref--; 15761544Seschrock mutex_exit(&spa_namespace_lock); 15771544Seschrock } 15781544Seschrock 15795450Sbrendan /* 15805450Sbrendan * Add spares device information to the nvlist. 15815450Sbrendan */ 15822082Seschrock static void 15832082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 15842082Seschrock { 15852082Seschrock nvlist_t **spares; 15862082Seschrock uint_t i, nspares; 15872082Seschrock nvlist_t *nvroot; 15882082Seschrock uint64_t guid; 15892082Seschrock vdev_stat_t *vs; 15902082Seschrock uint_t vsc; 15913377Seschrock uint64_t pool; 15922082Seschrock 15935450Sbrendan if (spa->spa_spares.sav_count == 0) 15942082Seschrock return; 15952082Seschrock 15962082Seschrock VERIFY(nvlist_lookup_nvlist(config, 15972082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15985450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 15992082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 16002082Seschrock if (nspares != 0) { 16012082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 16022082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16032082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 16042082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 16052082Seschrock 16062082Seschrock /* 16072082Seschrock * Go through and find any spares which have since been 16082082Seschrock * repurposed as an active spare. If this is the case, update 16092082Seschrock * their status appropriately. 16102082Seschrock */ 16112082Seschrock for (i = 0; i < nspares; i++) { 16122082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 16132082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 16147214Slling if (spa_spare_exists(guid, &pool, NULL) && 16157214Slling pool != 0ULL) { 16162082Seschrock VERIFY(nvlist_lookup_uint64_array( 16172082Seschrock spares[i], ZPOOL_CONFIG_STATS, 16182082Seschrock (uint64_t **)&vs, &vsc) == 0); 16192082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 16202082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 16212082Seschrock } 16222082Seschrock } 16232082Seschrock } 16242082Seschrock } 16252082Seschrock 16265450Sbrendan /* 16275450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 16285450Sbrendan */ 16295450Sbrendan static void 16305450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 16315450Sbrendan { 16325450Sbrendan nvlist_t **l2cache; 16335450Sbrendan uint_t i, j, nl2cache; 16345450Sbrendan nvlist_t *nvroot; 16355450Sbrendan uint64_t guid; 16365450Sbrendan vdev_t *vd; 16375450Sbrendan vdev_stat_t *vs; 16385450Sbrendan uint_t vsc; 16395450Sbrendan 16405450Sbrendan if (spa->spa_l2cache.sav_count == 0) 16415450Sbrendan return; 16425450Sbrendan 16437754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 16445450Sbrendan 16455450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 16465450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 16475450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 16485450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 16495450Sbrendan if (nl2cache != 0) { 16505450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 16515450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 16525450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 16535450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 16545450Sbrendan 16555450Sbrendan /* 16565450Sbrendan * Update level 2 cache device stats. 16575450Sbrendan */ 16585450Sbrendan 16595450Sbrendan for (i = 0; i < nl2cache; i++) { 16605450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 16615450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 16625450Sbrendan 16635450Sbrendan vd = NULL; 16645450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 16655450Sbrendan if (guid == 16665450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 16675450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 16685450Sbrendan break; 16695450Sbrendan } 16705450Sbrendan } 16715450Sbrendan ASSERT(vd != NULL); 16725450Sbrendan 16735450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 16745450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 16755450Sbrendan vdev_get_stats(vd, vs); 16765450Sbrendan } 16775450Sbrendan } 16785450Sbrendan 16797754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 16805450Sbrendan } 16815450Sbrendan 1682789Sahrens int 16831544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1684789Sahrens { 1685789Sahrens int error; 1686789Sahrens spa_t *spa; 1687789Sahrens 1688789Sahrens *config = NULL; 1689789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1690789Sahrens 16912082Seschrock if (spa && *config != NULL) { 16921544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 16931544Seschrock spa_get_errlog_size(spa)) == 0); 16941544Seschrock 16957754SJeff.Bonwick@Sun.COM if (spa_suspended(spa)) 16967754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 16977754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0); 16987754SJeff.Bonwick@Sun.COM 16992082Seschrock spa_add_spares(spa, *config); 17005450Sbrendan spa_add_l2cache(spa, *config); 17012082Seschrock } 17022082Seschrock 17031544Seschrock /* 17041544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 17051544Seschrock * and call spa_lookup() directly. 17061544Seschrock */ 17071544Seschrock if (altroot) { 17081544Seschrock if (spa == NULL) { 17091544Seschrock mutex_enter(&spa_namespace_lock); 17101544Seschrock spa = spa_lookup(name); 17111544Seschrock if (spa) 17121544Seschrock spa_altroot(spa, altroot, buflen); 17131544Seschrock else 17141544Seschrock altroot[0] = '\0'; 17151544Seschrock spa = NULL; 17161544Seschrock mutex_exit(&spa_namespace_lock); 17171544Seschrock } else { 17181544Seschrock spa_altroot(spa, altroot, buflen); 17191544Seschrock } 17201544Seschrock } 17211544Seschrock 1722789Sahrens if (spa != NULL) 1723789Sahrens spa_close(spa, FTAG); 1724789Sahrens 1725789Sahrens return (error); 1726789Sahrens } 1727789Sahrens 1728789Sahrens /* 17295450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 17305450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 17315450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 17325450Sbrendan * specified, as long as they are well-formed. 17332082Seschrock */ 17342082Seschrock static int 17355450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 17365450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 17375450Sbrendan vdev_labeltype_t label) 17382082Seschrock { 17395450Sbrendan nvlist_t **dev; 17405450Sbrendan uint_t i, ndev; 17412082Seschrock vdev_t *vd; 17422082Seschrock int error; 17432082Seschrock 17447754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 17457754SJeff.Bonwick@Sun.COM 17462082Seschrock /* 17475450Sbrendan * It's acceptable to have no devs specified. 17482082Seschrock */ 17495450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 17502082Seschrock return (0); 17512082Seschrock 17525450Sbrendan if (ndev == 0) 17532082Seschrock return (EINVAL); 17542082Seschrock 17552082Seschrock /* 17565450Sbrendan * Make sure the pool is formatted with a version that supports this 17575450Sbrendan * device type. 17582082Seschrock */ 17595450Sbrendan if (spa_version(spa) < version) 17602082Seschrock return (ENOTSUP); 17612082Seschrock 17623377Seschrock /* 17635450Sbrendan * Set the pending device list so we correctly handle device in-use 17643377Seschrock * checking. 17653377Seschrock */ 17665450Sbrendan sav->sav_pending = dev; 17675450Sbrendan sav->sav_npending = ndev; 17685450Sbrendan 17695450Sbrendan for (i = 0; i < ndev; i++) { 17705450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 17712082Seschrock mode)) != 0) 17723377Seschrock goto out; 17732082Seschrock 17742082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 17752082Seschrock vdev_free(vd); 17763377Seschrock error = EINVAL; 17773377Seschrock goto out; 17782082Seschrock } 17792082Seschrock 17805450Sbrendan /* 17817754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 17827754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 17835450Sbrendan */ 17847754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 17855450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 17865450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 17875450Sbrendan error = ENOTBLK; 17885450Sbrendan goto out; 17895450Sbrendan } 17907754SJeff.Bonwick@Sun.COM #endif 17912082Seschrock vd->vdev_top = vd; 17923377Seschrock 17933377Seschrock if ((error = vdev_open(vd)) == 0 && 17945450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 17955450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 17963377Seschrock vd->vdev_guid) == 0); 17972082Seschrock } 17982082Seschrock 17992082Seschrock vdev_free(vd); 18003377Seschrock 18015450Sbrendan if (error && 18025450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 18033377Seschrock goto out; 18043377Seschrock else 18053377Seschrock error = 0; 18062082Seschrock } 18072082Seschrock 18083377Seschrock out: 18095450Sbrendan sav->sav_pending = NULL; 18105450Sbrendan sav->sav_npending = 0; 18113377Seschrock return (error); 18122082Seschrock } 18132082Seschrock 18145450Sbrendan static int 18155450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 18165450Sbrendan { 18175450Sbrendan int error; 18185450Sbrendan 18197754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 18207754SJeff.Bonwick@Sun.COM 18215450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 18225450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 18235450Sbrendan VDEV_LABEL_SPARE)) != 0) { 18245450Sbrendan return (error); 18255450Sbrendan } 18265450Sbrendan 18275450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 18285450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 18295450Sbrendan VDEV_LABEL_L2CACHE)); 18305450Sbrendan } 18315450Sbrendan 18325450Sbrendan static void 18335450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 18345450Sbrendan const char *config) 18355450Sbrendan { 18365450Sbrendan int i; 18375450Sbrendan 18385450Sbrendan if (sav->sav_config != NULL) { 18395450Sbrendan nvlist_t **olddevs; 18405450Sbrendan uint_t oldndevs; 18415450Sbrendan nvlist_t **newdevs; 18425450Sbrendan 18435450Sbrendan /* 18445450Sbrendan * Generate new dev list by concatentating with the 18455450Sbrendan * current dev list. 18465450Sbrendan */ 18475450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 18485450Sbrendan &olddevs, &oldndevs) == 0); 18495450Sbrendan 18505450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 18515450Sbrendan (ndevs + oldndevs), KM_SLEEP); 18525450Sbrendan for (i = 0; i < oldndevs; i++) 18535450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 18545450Sbrendan KM_SLEEP) == 0); 18555450Sbrendan for (i = 0; i < ndevs; i++) 18565450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 18575450Sbrendan KM_SLEEP) == 0); 18585450Sbrendan 18595450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 18605450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 18615450Sbrendan 18625450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 18635450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 18645450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 18655450Sbrendan nvlist_free(newdevs[i]); 18665450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 18675450Sbrendan } else { 18685450Sbrendan /* 18695450Sbrendan * Generate a new dev list. 18705450Sbrendan */ 18715450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 18725450Sbrendan KM_SLEEP) == 0); 18735450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 18745450Sbrendan devs, ndevs) == 0); 18755450Sbrendan } 18765450Sbrendan } 18775450Sbrendan 18785450Sbrendan /* 18795450Sbrendan * Stop and drop level 2 ARC devices 18805450Sbrendan */ 18815450Sbrendan void 18825450Sbrendan spa_l2cache_drop(spa_t *spa) 18835450Sbrendan { 18845450Sbrendan vdev_t *vd; 18855450Sbrendan int i; 18865450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 18875450Sbrendan 18885450Sbrendan for (i = 0; i < sav->sav_count; i++) { 18895450Sbrendan uint64_t pool; 18905450Sbrendan 18915450Sbrendan vd = sav->sav_vdevs[i]; 18925450Sbrendan ASSERT(vd != NULL); 18935450Sbrendan 18948241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 18958241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 18965450Sbrendan l2arc_remove_vdev(vd); 18975450Sbrendan if (vd->vdev_isl2cache) 18985450Sbrendan spa_l2cache_remove(vd); 18995450Sbrendan vdev_clear_stats(vd); 19005450Sbrendan (void) vdev_close(vd); 19015450Sbrendan } 19025450Sbrendan } 19035450Sbrendan 19042082Seschrock /* 1905789Sahrens * Pool Creation 1906789Sahrens */ 1907789Sahrens int 19085094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 19097184Stimh const char *history_str, nvlist_t *zplprops) 1910789Sahrens { 1911789Sahrens spa_t *spa; 19125094Slling char *altroot = NULL; 19131635Sbonwick vdev_t *rvd; 1914789Sahrens dsl_pool_t *dp; 1915789Sahrens dmu_tx_t *tx; 19162082Seschrock int c, error = 0; 1917789Sahrens uint64_t txg = TXG_INITIAL; 19185450Sbrendan nvlist_t **spares, **l2cache; 19195450Sbrendan uint_t nspares, nl2cache; 19205094Slling uint64_t version; 1921789Sahrens 1922789Sahrens /* 1923789Sahrens * If this pool already exists, return failure. 1924789Sahrens */ 1925789Sahrens mutex_enter(&spa_namespace_lock); 1926789Sahrens if (spa_lookup(pool) != NULL) { 1927789Sahrens mutex_exit(&spa_namespace_lock); 1928789Sahrens return (EEXIST); 1929789Sahrens } 1930789Sahrens 1931789Sahrens /* 1932789Sahrens * Allocate a new spa_t structure. 1933789Sahrens */ 19345094Slling (void) nvlist_lookup_string(props, 19355094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 19361635Sbonwick spa = spa_add(pool, altroot); 19378241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1938789Sahrens 1939789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 19405094Slling 19415094Slling if (props && (error = spa_prop_validate(spa, props))) { 19425094Slling spa_unload(spa); 19435094Slling spa_deactivate(spa); 19445094Slling spa_remove(spa); 19456643Seschrock mutex_exit(&spa_namespace_lock); 19465094Slling return (error); 19475094Slling } 19485094Slling 19495094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 19505094Slling &version) != 0) 19515094Slling version = SPA_VERSION; 19525094Slling ASSERT(version <= SPA_VERSION); 19535094Slling spa->spa_uberblock.ub_version = version; 1954789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1955789Sahrens 19561635Sbonwick /* 19571635Sbonwick * Create the root vdev. 19581635Sbonwick */ 19597754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 19601635Sbonwick 19612082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 19622082Seschrock 19632082Seschrock ASSERT(error != 0 || rvd != NULL); 19642082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 19652082Seschrock 19665913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 19671635Sbonwick error = EINVAL; 19682082Seschrock 19692082Seschrock if (error == 0 && 19702082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 19715450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 19722082Seschrock VDEV_ALLOC_ADD)) == 0) { 19732082Seschrock for (c = 0; c < rvd->vdev_children; c++) 19742082Seschrock vdev_init(rvd->vdev_child[c], txg); 19752082Seschrock vdev_config_dirty(rvd); 19761635Sbonwick } 19771635Sbonwick 19787754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1979789Sahrens 19802082Seschrock if (error != 0) { 1981789Sahrens spa_unload(spa); 1982789Sahrens spa_deactivate(spa); 1983789Sahrens spa_remove(spa); 1984789Sahrens mutex_exit(&spa_namespace_lock); 1985789Sahrens return (error); 1986789Sahrens } 1987789Sahrens 19882082Seschrock /* 19892082Seschrock * Get the list of spares, if specified. 19902082Seschrock */ 19912082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 19922082Seschrock &spares, &nspares) == 0) { 19935450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 19942082Seschrock KM_SLEEP) == 0); 19955450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 19962082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 19977754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 19982082Seschrock spa_load_spares(spa); 19997754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 20005450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 20015450Sbrendan } 20025450Sbrendan 20035450Sbrendan /* 20045450Sbrendan * Get the list of level 2 cache devices, if specified. 20055450Sbrendan */ 20065450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 20075450Sbrendan &l2cache, &nl2cache) == 0) { 20085450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 20095450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 20105450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 20115450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 20127754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20135450Sbrendan spa_load_l2cache(spa); 20147754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 20155450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 20162082Seschrock } 20172082Seschrock 20187184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2019789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2020789Sahrens 2021789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2022789Sahrens 2023789Sahrens /* 2024789Sahrens * Create the pool config object. 2025789Sahrens */ 2026789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 20277497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2028789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2029789Sahrens 20301544Seschrock if (zap_add(spa->spa_meta_objset, 2031789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 20321544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 20331544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 20341544Seschrock } 2035789Sahrens 20365094Slling /* Newly created pools with the right version are always deflated. */ 20375094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 20385094Slling spa->spa_deflate = TRUE; 20395094Slling if (zap_add(spa->spa_meta_objset, 20405094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 20415094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 20425094Slling cmn_err(CE_PANIC, "failed to add deflate"); 20435094Slling } 20442082Seschrock } 20452082Seschrock 2046789Sahrens /* 2047789Sahrens * Create the deferred-free bplist object. Turn off compression 2048789Sahrens * because sync-to-convergence takes longer if the blocksize 2049789Sahrens * keeps changing. 2050789Sahrens */ 2051789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2052789Sahrens 1 << 14, tx); 2053789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2054789Sahrens ZIO_COMPRESS_OFF, tx); 2055789Sahrens 20561544Seschrock if (zap_add(spa->spa_meta_objset, 2057789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 20581544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 20591544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 20601544Seschrock } 2061789Sahrens 20622926Sek110237 /* 20632926Sek110237 * Create the pool's history object. 20642926Sek110237 */ 20655094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 20665094Slling spa_history_create_obj(spa, tx); 20675094Slling 20685094Slling /* 20695094Slling * Set pool properties. 20705094Slling */ 20715094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 20725094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20735329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 20745094Slling if (props) 20755094Slling spa_sync_props(spa, props, CRED(), tx); 20762926Sek110237 2077789Sahrens dmu_tx_commit(tx); 2078789Sahrens 2079789Sahrens spa->spa_sync_on = B_TRUE; 2080789Sahrens txg_sync_start(spa->spa_dsl_pool); 2081789Sahrens 2082789Sahrens /* 2083789Sahrens * We explicitly wait for the first transaction to complete so that our 2084789Sahrens * bean counters are appropriately updated. 2085789Sahrens */ 2086789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2087789Sahrens 20886643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2089789Sahrens 20905094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 20914715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 20924715Sek110237 2093789Sahrens mutex_exit(&spa_namespace_lock); 2094789Sahrens 20957046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 20967046Sahrens 2097789Sahrens return (0); 2098789Sahrens } 2099789Sahrens 2100789Sahrens /* 2101789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2102789Sahrens * then call spa_load() to do the dirty work. 2103789Sahrens */ 21046423Sgw25295 static int 21056423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 21066643Seschrock boolean_t isroot, boolean_t allowfaulted) 2107789Sahrens { 2108789Sahrens spa_t *spa; 21095094Slling char *altroot = NULL; 21106643Seschrock int error, loaderr; 21112082Seschrock nvlist_t *nvroot; 21125450Sbrendan nvlist_t **spares, **l2cache; 21135450Sbrendan uint_t nspares, nl2cache; 2114789Sahrens 2115789Sahrens /* 2116789Sahrens * If a pool with this name exists, return failure. 2117789Sahrens */ 2118789Sahrens mutex_enter(&spa_namespace_lock); 21197897SLin.Ling@Sun.COM if ((spa = spa_lookup(pool)) != NULL) { 21207897SLin.Ling@Sun.COM if (isroot) { 21217897SLin.Ling@Sun.COM /* 21227897SLin.Ling@Sun.COM * Remove the existing root pool from the 21237897SLin.Ling@Sun.COM * namespace so that we can replace it with 21247897SLin.Ling@Sun.COM * the correct config we just read in. 21257897SLin.Ling@Sun.COM */ 21267897SLin.Ling@Sun.COM ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 21277897SLin.Ling@Sun.COM spa_remove(spa); 21287897SLin.Ling@Sun.COM } else { 21297897SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 21307897SLin.Ling@Sun.COM return (EEXIST); 21317897SLin.Ling@Sun.COM } 2132789Sahrens } 2133789Sahrens 2134789Sahrens /* 21351635Sbonwick * Create and initialize the spa structure. 2136789Sahrens */ 21375094Slling (void) nvlist_lookup_string(props, 21385094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 21391635Sbonwick spa = spa_add(pool, altroot); 21408241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2141789Sahrens 21426643Seschrock if (allowfaulted) 21436643Seschrock spa->spa_import_faulted = B_TRUE; 21446673Seschrock spa->spa_is_root = isroot; 21456643Seschrock 2146789Sahrens /* 21471635Sbonwick * Pass off the heavy lifting to spa_load(). 21487046Sahrens * Pass TRUE for mosconfig (unless this is a root pool) because 21497046Sahrens * the user-supplied config is actually the one to trust when 21507046Sahrens * doing an import. 21511601Sbonwick */ 21527046Sahrens loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot); 2153789Sahrens 21547754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21552082Seschrock /* 21562082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 21572082Seschrock * and conflicts with spa_has_spare(). 21582082Seschrock */ 21596423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 21605450Sbrendan nvlist_free(spa->spa_spares.sav_config); 21615450Sbrendan spa->spa_spares.sav_config = NULL; 21622082Seschrock spa_load_spares(spa); 21632082Seschrock } 21646423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 21655450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 21665450Sbrendan spa->spa_l2cache.sav_config = NULL; 21675450Sbrendan spa_load_l2cache(spa); 21685450Sbrendan } 21692082Seschrock 21702082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 21712082Seschrock &nvroot) == 0); 21725450Sbrendan if (error == 0) 21735450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 21745450Sbrendan if (error == 0) 21755450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 21765450Sbrendan VDEV_ALLOC_L2CACHE); 21777754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 21782082Seschrock 21798241SJeff.Bonwick@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 21808241SJeff.Bonwick@Sun.COM (error = spa_prop_set(spa, props)))) { 21816643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 21826643Seschrock /* 21836643Seschrock * If we failed to load the pool, but 'allowfaulted' is 21846643Seschrock * set, then manually set the config as if the config 21856643Seschrock * passed in was specified in the cache file. 21866643Seschrock */ 21876643Seschrock error = 0; 21886643Seschrock spa->spa_import_faulted = B_FALSE; 21897754SJeff.Bonwick@Sun.COM if (spa->spa_config == NULL) 21906643Seschrock spa->spa_config = spa_config_generate(spa, 21916643Seschrock NULL, -1ULL, B_TRUE); 21926643Seschrock spa_unload(spa); 21936643Seschrock spa_deactivate(spa); 21946643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 21956643Seschrock } else { 21966643Seschrock spa_unload(spa); 21976643Seschrock spa_deactivate(spa); 21986643Seschrock spa_remove(spa); 21996643Seschrock } 2200789Sahrens mutex_exit(&spa_namespace_lock); 2201789Sahrens return (error); 2202789Sahrens } 2203789Sahrens 22041635Sbonwick /* 22055450Sbrendan * Override any spares and level 2 cache devices as specified by 22065450Sbrendan * the user, as these may have correct device names/devids, etc. 22072082Seschrock */ 22082082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 22092082Seschrock &spares, &nspares) == 0) { 22105450Sbrendan if (spa->spa_spares.sav_config) 22115450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 22122082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 22132082Seschrock else 22145450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 22152082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 22165450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 22172082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 22187754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22192082Seschrock spa_load_spares(spa); 22207754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 22215450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 22225450Sbrendan } 22235450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 22245450Sbrendan &l2cache, &nl2cache) == 0) { 22255450Sbrendan if (spa->spa_l2cache.sav_config) 22265450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 22275450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 22285450Sbrendan else 22295450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 22305450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 22315450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 22325450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 22337754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22345450Sbrendan spa_load_l2cache(spa); 22357754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 22365450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 22372082Seschrock } 22382082Seschrock 22398241SJeff.Bonwick@Sun.COM if (spa_writeable(spa)) { 22406643Seschrock /* 22416643Seschrock * Update the config cache to include the newly-imported pool. 22426643Seschrock */ 22436423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 22446643Seschrock } 22456643Seschrock 22466643Seschrock spa->spa_import_faulted = B_FALSE; 22474451Seschrock mutex_exit(&spa_namespace_lock); 22484451Seschrock 2249789Sahrens return (0); 2250789Sahrens } 2251789Sahrens 22526423Sgw25295 #ifdef _KERNEL 22536423Sgw25295 /* 22546423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 22556423Sgw25295 * device label. 22566423Sgw25295 */ 22576423Sgw25295 static void 22586423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 22596423Sgw25295 { 22606423Sgw25295 nvlist_t *nvtop, *nvroot; 22616423Sgw25295 uint64_t pgid; 22626423Sgw25295 22636423Sgw25295 /* 22646423Sgw25295 * Add this top-level vdev to the child array. 22656423Sgw25295 */ 22666423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 22676423Sgw25295 == 0); 22686423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 22696423Sgw25295 == 0); 22706423Sgw25295 22716423Sgw25295 /* 22726423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 22736423Sgw25295 */ 22746423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 22756423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 22766423Sgw25295 == 0); 22776423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 22786423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 22796423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 22806423Sgw25295 &nvtop, 1) == 0); 22816423Sgw25295 22826423Sgw25295 /* 22836423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 22846423Sgw25295 * this pool's configuration (remove the old, add the new). 22856423Sgw25295 */ 22866423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 22876423Sgw25295 nvlist_free(nvroot); 22886423Sgw25295 } 22896423Sgw25295 22906423Sgw25295 /* 22916423Sgw25295 * Get the root pool information from the root disk, then import the root pool 22926423Sgw25295 * during the system boot up time. 22936423Sgw25295 */ 22947539SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 22957147Staylor 22967147Staylor int 22977147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf, 22986423Sgw25295 uint64_t *besttxg) 22996423Sgw25295 { 23006423Sgw25295 nvlist_t *config; 23016423Sgw25295 uint64_t txg; 23027539SLin.Ling@Sun.COM int error; 23037539SLin.Ling@Sun.COM 23047539SLin.Ling@Sun.COM if (error = vdev_disk_read_rootlabel(devpath, devid, &config)) 23057539SLin.Ling@Sun.COM return (error); 23066423Sgw25295 23076423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 23086423Sgw25295 23097147Staylor if (bestconf != NULL) 23106423Sgw25295 *bestconf = config; 23117539SLin.Ling@Sun.COM else 23127539SLin.Ling@Sun.COM nvlist_free(config); 23137147Staylor *besttxg = txg; 23147147Staylor return (0); 23156423Sgw25295 } 23166423Sgw25295 23176423Sgw25295 boolean_t 23186423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 23196423Sgw25295 { 23206423Sgw25295 uint64_t ival; 23216423Sgw25295 23226423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 23236423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 23246423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 23256423Sgw25295 return (B_FALSE); 23266423Sgw25295 23276423Sgw25295 return (B_TRUE); 23286423Sgw25295 } 23296423Sgw25295 23307147Staylor 23317147Staylor /* 23327147Staylor * Given the boot device's physical path or devid, check if the device 23337147Staylor * is in a valid state. If so, return the configuration from the vdev 23347147Staylor * label. 23357147Staylor */ 23367147Staylor int 23377147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf) 23387147Staylor { 23397147Staylor nvlist_t *conf = NULL; 23407147Staylor uint64_t txg = 0; 23417147Staylor nvlist_t *nvtop, **child; 23427147Staylor char *type; 23437147Staylor char *bootpath = NULL; 23447147Staylor uint_t children, c; 23457147Staylor char *tmp; 23467539SLin.Ling@Sun.COM int error; 23477147Staylor 23487147Staylor if (devpath && ((tmp = strchr(devpath, ' ')) != NULL)) 23497147Staylor *tmp = '\0'; 23507539SLin.Ling@Sun.COM if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) { 23517147Staylor cmn_err(CE_NOTE, "error reading device label"); 23527539SLin.Ling@Sun.COM return (error); 23537147Staylor } 23547147Staylor if (txg == 0) { 23557147Staylor cmn_err(CE_NOTE, "this device is detached"); 23567147Staylor nvlist_free(conf); 23577147Staylor return (EINVAL); 23587147Staylor } 23597147Staylor 23607147Staylor VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE, 23617147Staylor &nvtop) == 0); 23627147Staylor VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0); 23637147Staylor 23647147Staylor if (strcmp(type, VDEV_TYPE_DISK) == 0) { 23657147Staylor if (spa_rootdev_validate(nvtop)) { 23667147Staylor goto out; 23677147Staylor } else { 23687147Staylor nvlist_free(conf); 23697147Staylor return (EINVAL); 23707147Staylor } 23717147Staylor } 23727147Staylor 23737147Staylor ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0); 23747147Staylor 23757147Staylor VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN, 23767147Staylor &child, &children) == 0); 23777147Staylor 23787147Staylor /* 23797147Staylor * Go thru vdevs in the mirror to see if the given device 23807147Staylor * has the most recent txg. Only the device with the most 23817147Staylor * recent txg has valid information and should be booted. 23827147Staylor */ 23837147Staylor for (c = 0; c < children; c++) { 23847147Staylor char *cdevid, *cpath; 23857147Staylor uint64_t tmptxg; 23867147Staylor 2387*8242SLin.Ling@Sun.COM cpath = NULL; 2388*8242SLin.Ling@Sun.COM cdevid = NULL; 23897147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH, 2390*8242SLin.Ling@Sun.COM &cpath) != 0 && nvlist_lookup_string(child[c], 2391*8242SLin.Ling@Sun.COM ZPOOL_CONFIG_DEVID, &cdevid) != 0) 23927147Staylor return (EINVAL); 23937687SLin.Ling@Sun.COM if ((spa_check_rootconf(cpath, cdevid, NULL, 23947687SLin.Ling@Sun.COM &tmptxg) == 0) && (tmptxg > txg)) { 23957147Staylor txg = tmptxg; 23967147Staylor VERIFY(nvlist_lookup_string(child[c], 23977147Staylor ZPOOL_CONFIG_PATH, &bootpath) == 0); 23987147Staylor } 23997147Staylor } 24007147Staylor 24017147Staylor /* Does the best device match the one we've booted from? */ 24027147Staylor if (bootpath) { 24037147Staylor cmn_err(CE_NOTE, "try booting from '%s'", bootpath); 24047147Staylor return (EINVAL); 24057147Staylor } 24067147Staylor out: 24077147Staylor *bestconf = conf; 24087147Staylor return (0); 24097147Staylor } 24107147Staylor 24116423Sgw25295 /* 24126423Sgw25295 * Import a root pool. 24136423Sgw25295 * 24147147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 24157147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 24167147Staylor * The GRUB "findroot" command will return the vdev we should boot. 24176423Sgw25295 * 24186423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 24196423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 24206423Sgw25295 * e.g. 24216423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 24226423Sgw25295 */ 24236423Sgw25295 int 24247147Staylor spa_import_rootpool(char *devpath, char *devid) 24256423Sgw25295 { 24266423Sgw25295 nvlist_t *conf = NULL; 24276423Sgw25295 char *pname; 24286423Sgw25295 int error; 24296423Sgw25295 24306423Sgw25295 /* 24316423Sgw25295 * Get the vdev pathname and configuation from the most 24326423Sgw25295 * recently updated vdev (highest txg). 24336423Sgw25295 */ 24347147Staylor if (error = spa_get_rootconf(devpath, devid, &conf)) 24356423Sgw25295 goto msg_out; 24366423Sgw25295 24376423Sgw25295 /* 24386423Sgw25295 * Add type "root" vdev to the config. 24396423Sgw25295 */ 24406423Sgw25295 spa_build_rootpool_config(conf); 24416423Sgw25295 24426423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 24436423Sgw25295 24446673Seschrock /* 24456673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 24466673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 24476673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 24486673Seschrock * pool open, not import. 24496673Seschrock */ 24506673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 24517897SLin.Ling@Sun.COM ASSERT(error != EEXIST); 24526423Sgw25295 24536423Sgw25295 nvlist_free(conf); 24546423Sgw25295 return (error); 24556423Sgw25295 24566423Sgw25295 msg_out: 24577147Staylor cmn_err(CE_NOTE, "\n" 24586423Sgw25295 " *************************************************** \n" 24596423Sgw25295 " * This device is not bootable! * \n" 24606423Sgw25295 " * It is either offlined or detached or faulted. * \n" 24616423Sgw25295 " * Please try to boot from a different device. * \n" 24627147Staylor " *************************************************** "); 24636423Sgw25295 24646423Sgw25295 return (error); 24656423Sgw25295 } 24666423Sgw25295 #endif 24676423Sgw25295 24686423Sgw25295 /* 24696423Sgw25295 * Import a non-root pool into the system. 24706423Sgw25295 */ 24716423Sgw25295 int 24726423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 24736423Sgw25295 { 24746643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 24756423Sgw25295 } 24766423Sgw25295 24776643Seschrock int 24786643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 24796643Seschrock { 24806643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 24816643Seschrock } 24826643Seschrock 24836643Seschrock 2484789Sahrens /* 2485789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2486789Sahrens * to get the vdev stats associated with the imported devices. 2487789Sahrens */ 2488789Sahrens #define TRYIMPORT_NAME "$import" 2489789Sahrens 2490789Sahrens nvlist_t * 2491789Sahrens spa_tryimport(nvlist_t *tryconfig) 2492789Sahrens { 2493789Sahrens nvlist_t *config = NULL; 2494789Sahrens char *poolname; 2495789Sahrens spa_t *spa; 2496789Sahrens uint64_t state; 2497789Sahrens 2498789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2499789Sahrens return (NULL); 2500789Sahrens 2501789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2502789Sahrens return (NULL); 2503789Sahrens 25041635Sbonwick /* 25051635Sbonwick * Create and initialize the spa structure. 25061635Sbonwick */ 2507789Sahrens mutex_enter(&spa_namespace_lock); 25081635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 25098241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 2510789Sahrens 2511789Sahrens /* 25121635Sbonwick * Pass off the heavy lifting to spa_load(). 25131732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 25141732Sbonwick * is actually the one to trust when doing an import. 2515789Sahrens */ 25161732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2517789Sahrens 2518789Sahrens /* 2519789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2520789Sahrens */ 2521789Sahrens if (spa->spa_root_vdev != NULL) { 2522789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2523789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2524789Sahrens poolname) == 0); 2525789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2526789Sahrens state) == 0); 25273975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 25283975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 25292082Seschrock 25302082Seschrock /* 25316423Sgw25295 * If the bootfs property exists on this pool then we 25326423Sgw25295 * copy it out so that external consumers can tell which 25336423Sgw25295 * pools are bootable. 25346423Sgw25295 */ 25356423Sgw25295 if (spa->spa_bootfs) { 25366423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 25376423Sgw25295 25386423Sgw25295 /* 25396423Sgw25295 * We have to play games with the name since the 25406423Sgw25295 * pool was opened as TRYIMPORT_NAME. 25416423Sgw25295 */ 25427754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 25436423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 25446423Sgw25295 char *cp; 25456423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 25466423Sgw25295 25476423Sgw25295 cp = strchr(tmpname, '/'); 25486423Sgw25295 if (cp == NULL) { 25496423Sgw25295 (void) strlcpy(dsname, tmpname, 25506423Sgw25295 MAXPATHLEN); 25516423Sgw25295 } else { 25526423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 25536423Sgw25295 "%s/%s", poolname, ++cp); 25546423Sgw25295 } 25556423Sgw25295 VERIFY(nvlist_add_string(config, 25566423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 25576423Sgw25295 kmem_free(dsname, MAXPATHLEN); 25586423Sgw25295 } 25596423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 25606423Sgw25295 } 25616423Sgw25295 25626423Sgw25295 /* 25635450Sbrendan * Add the list of hot spares and level 2 cache devices. 25642082Seschrock */ 25652082Seschrock spa_add_spares(spa, config); 25665450Sbrendan spa_add_l2cache(spa, config); 2567789Sahrens } 2568789Sahrens 2569789Sahrens spa_unload(spa); 2570789Sahrens spa_deactivate(spa); 2571789Sahrens spa_remove(spa); 2572789Sahrens mutex_exit(&spa_namespace_lock); 2573789Sahrens 2574789Sahrens return (config); 2575789Sahrens } 2576789Sahrens 2577789Sahrens /* 2578789Sahrens * Pool export/destroy 2579789Sahrens * 2580789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2581789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2582789Sahrens * update the pool state and sync all the labels to disk, removing the 25838211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 25848211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 2585789Sahrens */ 2586789Sahrens static int 25877214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 25888211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 2589789Sahrens { 2590789Sahrens spa_t *spa; 2591789Sahrens 25921775Sbillm if (oldconfig) 25931775Sbillm *oldconfig = NULL; 25941775Sbillm 25958241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 2596789Sahrens return (EROFS); 2597789Sahrens 2598789Sahrens mutex_enter(&spa_namespace_lock); 2599789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2600789Sahrens mutex_exit(&spa_namespace_lock); 2601789Sahrens return (ENOENT); 2602789Sahrens } 2603789Sahrens 2604789Sahrens /* 26051544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 26061544Seschrock * reacquire the namespace lock, and see if we can export. 26071544Seschrock */ 26081544Seschrock spa_open_ref(spa, FTAG); 26091544Seschrock mutex_exit(&spa_namespace_lock); 26101544Seschrock spa_async_suspend(spa); 26111544Seschrock mutex_enter(&spa_namespace_lock); 26121544Seschrock spa_close(spa, FTAG); 26131544Seschrock 26141544Seschrock /* 2615789Sahrens * The pool will be in core if it's openable, 2616789Sahrens * in which case we can modify its state. 2617789Sahrens */ 2618789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2619789Sahrens /* 2620789Sahrens * Objsets may be open only because they're dirty, so we 2621789Sahrens * have to force it to sync before checking spa_refcnt. 2622789Sahrens */ 2623789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2624789Sahrens 26251544Seschrock /* 26261544Seschrock * A pool cannot be exported or destroyed if there are active 26271544Seschrock * references. If we are resetting a pool, allow references by 26281544Seschrock * fault injection handlers. 26291544Seschrock */ 26301544Seschrock if (!spa_refcount_zero(spa) || 26311544Seschrock (spa->spa_inject_ref != 0 && 26321544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 26331544Seschrock spa_async_resume(spa); 2634789Sahrens mutex_exit(&spa_namespace_lock); 2635789Sahrens return (EBUSY); 2636789Sahrens } 2637789Sahrens 2638789Sahrens /* 26397214Slling * A pool cannot be exported if it has an active shared spare. 26407214Slling * This is to prevent other pools stealing the active spare 26417214Slling * from an exported pool. At user's own will, such pool can 26427214Slling * be forcedly exported. 26437214Slling */ 26447214Slling if (!force && new_state == POOL_STATE_EXPORTED && 26457214Slling spa_has_active_shared_spare(spa)) { 26467214Slling spa_async_resume(spa); 26477214Slling mutex_exit(&spa_namespace_lock); 26487214Slling return (EXDEV); 26497214Slling } 26507214Slling 26517214Slling /* 2652789Sahrens * We want this to be reflected on every label, 2653789Sahrens * so mark them all dirty. spa_unload() will do the 2654789Sahrens * final sync that pushes these changes out. 2655789Sahrens */ 26568211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 26577754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26581544Seschrock spa->spa_state = new_state; 26591635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 26601544Seschrock vdev_config_dirty(spa->spa_root_vdev); 26617754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 26621544Seschrock } 2663789Sahrens } 2664789Sahrens 26654451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 26664451Seschrock 2667789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2668789Sahrens spa_unload(spa); 2669789Sahrens spa_deactivate(spa); 2670789Sahrens } 2671789Sahrens 26721775Sbillm if (oldconfig && spa->spa_config) 26731775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 26741775Sbillm 26751544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 26768211SGeorge.Wilson@Sun.COM if (!hardforce) 26778211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 26781544Seschrock spa_remove(spa); 26791544Seschrock } 2680789Sahrens mutex_exit(&spa_namespace_lock); 2681789Sahrens 2682789Sahrens return (0); 2683789Sahrens } 2684789Sahrens 2685789Sahrens /* 2686789Sahrens * Destroy a storage pool. 2687789Sahrens */ 2688789Sahrens int 2689789Sahrens spa_destroy(char *pool) 2690789Sahrens { 26918211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 26928211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 2693789Sahrens } 2694789Sahrens 2695789Sahrens /* 2696789Sahrens * Export a storage pool. 2697789Sahrens */ 2698789Sahrens int 26998211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 27008211SGeorge.Wilson@Sun.COM boolean_t hardforce) 2701789Sahrens { 27028211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 27038211SGeorge.Wilson@Sun.COM force, hardforce)); 2704789Sahrens } 2705789Sahrens 2706789Sahrens /* 27071544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 27081544Seschrock * from the namespace in any way. 27091544Seschrock */ 27101544Seschrock int 27111544Seschrock spa_reset(char *pool) 27121544Seschrock { 27137214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 27148211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 27151544Seschrock } 27161544Seschrock 27171544Seschrock /* 2718789Sahrens * ========================================================================== 2719789Sahrens * Device manipulation 2720789Sahrens * ========================================================================== 2721789Sahrens */ 2722789Sahrens 2723789Sahrens /* 27244527Sperrin * Add a device to a storage pool. 2725789Sahrens */ 2726789Sahrens int 2727789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2728789Sahrens { 2729789Sahrens uint64_t txg; 27308241SJeff.Bonwick@Sun.COM int error; 2731789Sahrens vdev_t *rvd = spa->spa_root_vdev; 27321585Sbonwick vdev_t *vd, *tvd; 27335450Sbrendan nvlist_t **spares, **l2cache; 27345450Sbrendan uint_t nspares, nl2cache; 2735789Sahrens 2736789Sahrens txg = spa_vdev_enter(spa); 2737789Sahrens 27382082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 27392082Seschrock VDEV_ALLOC_ADD)) != 0) 27402082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 27412082Seschrock 27427754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 2743789Sahrens 27445450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 27455450Sbrendan &nspares) != 0) 27462082Seschrock nspares = 0; 27472082Seschrock 27485450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 27495450Sbrendan &nl2cache) != 0) 27505450Sbrendan nl2cache = 0; 27515450Sbrendan 27527754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 27532082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 27547754SJeff.Bonwick@Sun.COM 27557754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 27567754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 27577754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 27582082Seschrock 27593377Seschrock /* 27605450Sbrendan * We must validate the spares and l2cache devices after checking the 27615450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 27623377Seschrock */ 27637754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 27643377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 27653377Seschrock 27663377Seschrock /* 27673377Seschrock * Transfer each new top-level vdev from vd to rvd. 27683377Seschrock */ 27698241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 27703377Seschrock tvd = vd->vdev_child[c]; 27713377Seschrock vdev_remove_child(vd, tvd); 27723377Seschrock tvd->vdev_id = rvd->vdev_children; 27733377Seschrock vdev_add_child(rvd, tvd); 27743377Seschrock vdev_config_dirty(tvd); 27753377Seschrock } 27763377Seschrock 27772082Seschrock if (nspares != 0) { 27785450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 27795450Sbrendan ZPOOL_CONFIG_SPARES); 27802082Seschrock spa_load_spares(spa); 27815450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 27825450Sbrendan } 27835450Sbrendan 27845450Sbrendan if (nl2cache != 0) { 27855450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 27865450Sbrendan ZPOOL_CONFIG_L2CACHE); 27875450Sbrendan spa_load_l2cache(spa); 27885450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2789789Sahrens } 2790789Sahrens 2791789Sahrens /* 27921585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 27931585Sbonwick * If other threads start allocating from these vdevs before we 27941585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 27951585Sbonwick * fail to open the pool because there are DVAs that the config cache 27961585Sbonwick * can't translate. Therefore, we first add the vdevs without 27971585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 27981635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 27991585Sbonwick * 28001585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 28011585Sbonwick * if we lose power at any point in this sequence, the remaining 28021585Sbonwick * steps will be completed the next time we load the pool. 2803789Sahrens */ 28041635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 28051585Sbonwick 28061635Sbonwick mutex_enter(&spa_namespace_lock); 28071635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 28081635Sbonwick mutex_exit(&spa_namespace_lock); 2809789Sahrens 28101635Sbonwick return (0); 2811789Sahrens } 2812789Sahrens 2813789Sahrens /* 2814789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2815789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2816789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2817789Sahrens * 2818789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2819789Sahrens * existing device; in this case the two devices are made into their own 28204451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2821789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2822789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2823789Sahrens * completion of resilvering, the first disk (the one being replaced) 2824789Sahrens * is automatically detached. 2825789Sahrens */ 2826789Sahrens int 28271544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2828789Sahrens { 2829789Sahrens uint64_t txg, open_txg; 2830789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2831789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 28322082Seschrock vdev_ops_t *pvops; 28337313SEric.Kustarz@Sun.COM dmu_tx_t *tx; 28347313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 28357313SEric.Kustarz@Sun.COM int newvd_isspare; 28367313SEric.Kustarz@Sun.COM int error; 2837789Sahrens 2838789Sahrens txg = spa_vdev_enter(spa); 2839789Sahrens 28406643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2841789Sahrens 2842789Sahrens if (oldvd == NULL) 2843789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2844789Sahrens 28451585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 28461585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28471585Sbonwick 2848789Sahrens pvd = oldvd->vdev_parent; 2849789Sahrens 28502082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 28514451Seschrock VDEV_ALLOC_ADD)) != 0) 28524451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 28534451Seschrock 28544451Seschrock if (newrootvd->vdev_children != 1) 2855789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2856789Sahrens 2857789Sahrens newvd = newrootvd->vdev_child[0]; 2858789Sahrens 2859789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2860789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2861789Sahrens 28622082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2863789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2864789Sahrens 28654527Sperrin /* 28664527Sperrin * Spares can't replace logs 28674527Sperrin */ 28687326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 28694527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28704527Sperrin 28712082Seschrock if (!replacing) { 28722082Seschrock /* 28732082Seschrock * For attach, the only allowable parent is a mirror or the root 28742082Seschrock * vdev. 28752082Seschrock */ 28762082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 28772082Seschrock pvd->vdev_ops != &vdev_root_ops) 28782082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28792082Seschrock 28802082Seschrock pvops = &vdev_mirror_ops; 28812082Seschrock } else { 28822082Seschrock /* 28832082Seschrock * Active hot spares can only be replaced by inactive hot 28842082Seschrock * spares. 28852082Seschrock */ 28862082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 28872082Seschrock pvd->vdev_child[1] == oldvd && 28882082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 28892082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28902082Seschrock 28912082Seschrock /* 28922082Seschrock * If the source is a hot spare, and the parent isn't already a 28932082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 28943377Seschrock * want to create a replacing vdev. The user is not allowed to 28953377Seschrock * attach to a spared vdev child unless the 'isspare' state is 28963377Seschrock * the same (spare replaces spare, non-spare replaces 28973377Seschrock * non-spare). 28982082Seschrock */ 28992082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 29002082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29013377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 29023377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 29033377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29042082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 29052082Seschrock newvd->vdev_isspare) 29062082Seschrock pvops = &vdev_spare_ops; 29072082Seschrock else 29082082Seschrock pvops = &vdev_replacing_ops; 29092082Seschrock } 29102082Seschrock 29111175Slling /* 29121175Slling * Compare the new device size with the replaceable/attachable 29131175Slling * device size. 29141175Slling */ 29151175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2916789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2917789Sahrens 29181732Sbonwick /* 29191732Sbonwick * The new device cannot have a higher alignment requirement 29201732Sbonwick * than the top-level vdev. 29211732Sbonwick */ 29221732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2923789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2924789Sahrens 2925789Sahrens /* 2926789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2927789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2928789Sahrens */ 2929789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2930789Sahrens spa_strfree(oldvd->vdev_path); 2931789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2932789Sahrens KM_SLEEP); 2933789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2934789Sahrens newvd->vdev_path, "old"); 2935789Sahrens if (oldvd->vdev_devid != NULL) { 2936789Sahrens spa_strfree(oldvd->vdev_devid); 2937789Sahrens oldvd->vdev_devid = NULL; 2938789Sahrens } 2939789Sahrens } 2940789Sahrens 2941789Sahrens /* 29422082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 29432082Seschrock * mirror/replacing/spare vdev above oldvd. 2944789Sahrens */ 2945789Sahrens if (pvd->vdev_ops != pvops) 2946789Sahrens pvd = vdev_add_parent(oldvd, pvops); 2947789Sahrens 2948789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2949789Sahrens ASSERT(pvd->vdev_ops == pvops); 2950789Sahrens ASSERT(oldvd->vdev_parent == pvd); 2951789Sahrens 2952789Sahrens /* 2953789Sahrens * Extract the new device from its root and add it to pvd. 2954789Sahrens */ 2955789Sahrens vdev_remove_child(newrootvd, newvd); 2956789Sahrens newvd->vdev_id = pvd->vdev_children; 2957789Sahrens vdev_add_child(pvd, newvd); 2958789Sahrens 29591544Seschrock /* 29601544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 29611544Seschrock * the addition of newvd may have decreased our parent's asize. 29621544Seschrock */ 29631544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 29641544Seschrock 2965789Sahrens tvd = newvd->vdev_top; 2966789Sahrens ASSERT(pvd->vdev_top == tvd); 2967789Sahrens ASSERT(tvd->vdev_parent == rvd); 2968789Sahrens 2969789Sahrens vdev_config_dirty(tvd); 2970789Sahrens 2971789Sahrens /* 2972789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2973789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2974789Sahrens */ 2975789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2976789Sahrens 29778241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, 29788241SJeff.Bonwick@Sun.COM TXG_INITIAL, open_txg - TXG_INITIAL + 1); 2979789Sahrens 29803377Seschrock if (newvd->vdev_isspare) 29813377Seschrock spa_spare_activate(newvd); 29827754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 29837754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 29847313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 29851544Seschrock 2986789Sahrens /* 2987789Sahrens * Mark newvd's DTL dirty in this txg. 2988789Sahrens */ 29891732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2990789Sahrens 2991789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2992789Sahrens 29937313SEric.Kustarz@Sun.COM tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 29947313SEric.Kustarz@Sun.COM if (dmu_tx_assign(tx, TXG_WAIT) == 0) { 29957313SEric.Kustarz@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx, 29967313SEric.Kustarz@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 29977313SEric.Kustarz@Sun.COM replacing && newvd_isspare ? "spare in" : 29987313SEric.Kustarz@Sun.COM replacing ? "replace" : "attach", newvdpath, 29997313SEric.Kustarz@Sun.COM replacing ? "for" : "to", oldvdpath); 30007313SEric.Kustarz@Sun.COM dmu_tx_commit(tx); 30017313SEric.Kustarz@Sun.COM } else { 30027313SEric.Kustarz@Sun.COM dmu_tx_abort(tx); 30037313SEric.Kustarz@Sun.COM } 30047313SEric.Kustarz@Sun.COM 30057313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 30067313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 30077313SEric.Kustarz@Sun.COM 3008789Sahrens /* 30097046Sahrens * Kick off a resilver to update newvd. 3010789Sahrens */ 30117046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3012789Sahrens 3013789Sahrens return (0); 3014789Sahrens } 3015789Sahrens 3016789Sahrens /* 3017789Sahrens * Detach a device from a mirror or replacing vdev. 3018789Sahrens * If 'replace_done' is specified, only detach if the parent 3019789Sahrens * is a replacing vdev. 3020789Sahrens */ 3021789Sahrens int 30228241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3023789Sahrens { 3024789Sahrens uint64_t txg; 30258241SJeff.Bonwick@Sun.COM int error; 3026789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3027789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 30282082Seschrock boolean_t unspare = B_FALSE; 30292082Seschrock uint64_t unspare_guid; 30306673Seschrock size_t len; 3031789Sahrens 3032789Sahrens txg = spa_vdev_enter(spa); 3033789Sahrens 30346643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3035789Sahrens 3036789Sahrens if (vd == NULL) 3037789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3038789Sahrens 30391585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 30401585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30411585Sbonwick 3042789Sahrens pvd = vd->vdev_parent; 3043789Sahrens 3044789Sahrens /* 30458241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 30468241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 30478241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 30488241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 30498241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 30508241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 30518241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 30528241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 30538241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 30548241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 30558241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 30568241SJeff.Bonwick@Sun.COM */ 30578241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 30588241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 30598241SJeff.Bonwick@Sun.COM 30608241SJeff.Bonwick@Sun.COM /* 3061789Sahrens * If replace_done is specified, only remove this device if it's 30622082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 30632082Seschrock * disk can be removed. 3064789Sahrens */ 30652082Seschrock if (replace_done) { 30662082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 30672082Seschrock if (vd->vdev_id != 0) 30682082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30692082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 30702082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30712082Seschrock } 30722082Seschrock } 30732082Seschrock 30742082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 30754577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3076789Sahrens 3077789Sahrens /* 30782082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3079789Sahrens */ 3080789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 30812082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 30822082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3083789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3084789Sahrens 3085789Sahrens /* 30868241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 30878241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 3088789Sahrens */ 30898241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 3090789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3091789Sahrens 30928241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 30938241SJeff.Bonwick@Sun.COM 3094789Sahrens /* 30956673Seschrock * If we are detaching the second disk from a replacing vdev, then 30966673Seschrock * check to see if we changed the original vdev's path to have "/old" 30976673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 30986673Seschrock */ 30996673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 31006673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 31016673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 31026673Seschrock ASSERT(pvd->vdev_child[1] == vd); 31036673Seschrock cvd = pvd->vdev_child[0]; 31046673Seschrock len = strlen(vd->vdev_path); 31056673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 31066673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 31076673Seschrock spa_strfree(cvd->vdev_path); 31086673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 31096673Seschrock } 31106673Seschrock } 31116673Seschrock 31126673Seschrock /* 31132082Seschrock * If we are detaching the original disk from a spare, then it implies 31142082Seschrock * that the spare should become a real disk, and be removed from the 31152082Seschrock * active spare list for the pool. 31162082Seschrock */ 31172082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 31188241SJeff.Bonwick@Sun.COM vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 31192082Seschrock unspare = B_TRUE; 31202082Seschrock 31212082Seschrock /* 3122789Sahrens * Erase the disk labels so the disk can be used for other things. 3123789Sahrens * This must be done after all other error cases are handled, 3124789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3125789Sahrens * But if we can't do it, don't treat the error as fatal -- 3126789Sahrens * it may be that the unwritability of the disk is the reason 3127789Sahrens * it's being detached! 3128789Sahrens */ 31293377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3130789Sahrens 3131789Sahrens /* 3132789Sahrens * Remove vd from its parent and compact the parent's children. 3133789Sahrens */ 3134789Sahrens vdev_remove_child(pvd, vd); 3135789Sahrens vdev_compact_children(pvd); 3136789Sahrens 3137789Sahrens /* 3138789Sahrens * Remember one of the remaining children so we can get tvd below. 3139789Sahrens */ 3140789Sahrens cvd = pvd->vdev_child[0]; 3141789Sahrens 3142789Sahrens /* 31432082Seschrock * If we need to remove the remaining child from the list of hot spares, 31448241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 31458241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 31468241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 31478241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 31488241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 31498241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 31502082Seschrock */ 31512082Seschrock if (unspare) { 31522082Seschrock ASSERT(cvd->vdev_isspare); 31533377Seschrock spa_spare_remove(cvd); 31542082Seschrock unspare_guid = cvd->vdev_guid; 31558241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 31562082Seschrock } 31572082Seschrock 31582082Seschrock /* 3159789Sahrens * If the parent mirror/replacing vdev only has one child, 3160789Sahrens * the parent is no longer needed. Remove it from the tree. 3161789Sahrens */ 3162789Sahrens if (pvd->vdev_children == 1) 3163789Sahrens vdev_remove_parent(cvd); 3164789Sahrens 3165789Sahrens /* 3166789Sahrens * We don't set tvd until now because the parent we just removed 3167789Sahrens * may have been the previous top-level vdev. 3168789Sahrens */ 3169789Sahrens tvd = cvd->vdev_top; 3170789Sahrens ASSERT(tvd->vdev_parent == rvd); 3171789Sahrens 3172789Sahrens /* 31733377Seschrock * Reevaluate the parent vdev state. 3174789Sahrens */ 31754451Seschrock vdev_propagate_state(cvd); 3176789Sahrens 3177789Sahrens /* 31783377Seschrock * If the device we just detached was smaller than the others, it may be 31793377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 31803377Seschrock * can't fail because the existing metaslabs are already in core, so 31813377Seschrock * there's nothing to read from disk. 3182789Sahrens */ 31831732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3184789Sahrens 3185789Sahrens vdev_config_dirty(tvd); 3186789Sahrens 3187789Sahrens /* 31883377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 31893377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 31903377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 31913377Seschrock * prevent vd from being accessed after it's freed. 3192789Sahrens */ 31938241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 3194789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 31951732Sbonwick vd->vdev_detached = B_TRUE; 31961732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3197789Sahrens 31984451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 31994451Seschrock 32002082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 32012082Seschrock 32022082Seschrock /* 32033377Seschrock * If this was the removal of the original device in a hot spare vdev, 32043377Seschrock * then we want to go through and remove the device from the hot spare 32053377Seschrock * list of every other pool. 32062082Seschrock */ 32072082Seschrock if (unspare) { 32088241SJeff.Bonwick@Sun.COM spa_t *myspa = spa; 32092082Seschrock spa = NULL; 32102082Seschrock mutex_enter(&spa_namespace_lock); 32112082Seschrock while ((spa = spa_next(spa)) != NULL) { 32122082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 32132082Seschrock continue; 32148241SJeff.Bonwick@Sun.COM if (spa == myspa) 32158241SJeff.Bonwick@Sun.COM continue; 32167793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 32177793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 32182082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 32197793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 32207793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 32212082Seschrock } 32222082Seschrock mutex_exit(&spa_namespace_lock); 32232082Seschrock } 32242082Seschrock 32252082Seschrock return (error); 32262082Seschrock } 32272082Seschrock 32287754SJeff.Bonwick@Sun.COM static nvlist_t * 32297754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 32302082Seschrock { 32317754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 32327754SJeff.Bonwick@Sun.COM uint64_t guid; 32337754SJeff.Bonwick@Sun.COM 32347754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 32357754SJeff.Bonwick@Sun.COM &guid) == 0); 32367754SJeff.Bonwick@Sun.COM 32377754SJeff.Bonwick@Sun.COM if (guid == target_guid) 32387754SJeff.Bonwick@Sun.COM return (nvpp[i]); 32392082Seschrock } 32402082Seschrock 32417754SJeff.Bonwick@Sun.COM return (NULL); 32425450Sbrendan } 32435450Sbrendan 32447754SJeff.Bonwick@Sun.COM static void 32457754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 32467754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 32475450Sbrendan { 32487754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 32497754SJeff.Bonwick@Sun.COM 32507754SJeff.Bonwick@Sun.COM if (count > 1) 32517754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 32527754SJeff.Bonwick@Sun.COM 32537754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 32547754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 32557754SJeff.Bonwick@Sun.COM continue; 32567754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 32575450Sbrendan } 32585450Sbrendan 32597754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 32607754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 32617754SJeff.Bonwick@Sun.COM 32627754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 32637754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 32647754SJeff.Bonwick@Sun.COM 32657754SJeff.Bonwick@Sun.COM if (count > 1) 32667754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 32675450Sbrendan } 32685450Sbrendan 32695450Sbrendan /* 32705450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 32715450Sbrendan * spares and level 2 ARC devices. 32725450Sbrendan */ 32735450Sbrendan int 32745450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 32755450Sbrendan { 32765450Sbrendan vdev_t *vd; 32777754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 32785450Sbrendan uint_t nspares, nl2cache; 32798241SJeff.Bonwick@Sun.COM uint64_t txg = 0; 32805450Sbrendan int error = 0; 32818241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 32828241SJeff.Bonwick@Sun.COM 32838241SJeff.Bonwick@Sun.COM if (!locked) 32848241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 32855450Sbrendan 32866643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 32875450Sbrendan 32885450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 32895450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 32907754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 32917754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 32927754SJeff.Bonwick@Sun.COM /* 32937754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 32947754SJeff.Bonwick@Sun.COM * in this pool. 32957754SJeff.Bonwick@Sun.COM */ 32967754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 32977754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 32987754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 32997754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 33007754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 33017754SJeff.Bonwick@Sun.COM } else { 33027754SJeff.Bonwick@Sun.COM error = EBUSY; 33037754SJeff.Bonwick@Sun.COM } 33047754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 33055450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 33067754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 33077754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 33087754SJeff.Bonwick@Sun.COM /* 33097754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 33107754SJeff.Bonwick@Sun.COM */ 33117754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 33127754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 33135450Sbrendan spa_load_l2cache(spa); 33145450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 33157754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 33167754SJeff.Bonwick@Sun.COM /* 33177754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 33187754SJeff.Bonwick@Sun.COM */ 33197754SJeff.Bonwick@Sun.COM error = ENOTSUP; 33207754SJeff.Bonwick@Sun.COM } else { 33217754SJeff.Bonwick@Sun.COM /* 33227754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 33237754SJeff.Bonwick@Sun.COM */ 33247754SJeff.Bonwick@Sun.COM error = ENOENT; 33255450Sbrendan } 33262082Seschrock 33278241SJeff.Bonwick@Sun.COM if (!locked) 33288241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 33298241SJeff.Bonwick@Sun.COM 33308241SJeff.Bonwick@Sun.COM return (error); 3331789Sahrens } 3332789Sahrens 3333789Sahrens /* 33344451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 33354451Seschrock * current spared, so we can detach it. 3336789Sahrens */ 33371544Seschrock static vdev_t * 33384451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3339789Sahrens { 33401544Seschrock vdev_t *newvd, *oldvd; 3341789Sahrens int c; 3342789Sahrens 33431544Seschrock for (c = 0; c < vd->vdev_children; c++) { 33444451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 33451544Seschrock if (oldvd != NULL) 33461544Seschrock return (oldvd); 33471544Seschrock } 3348789Sahrens 33494451Seschrock /* 33504451Seschrock * Check for a completed replacement. 33514451Seschrock */ 3352789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 33531544Seschrock oldvd = vd->vdev_child[0]; 33541544Seschrock newvd = vd->vdev_child[1]; 3355789Sahrens 33568241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 33578241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 33581544Seschrock return (oldvd); 33591544Seschrock } 3360789Sahrens 33614451Seschrock /* 33624451Seschrock * Check for a completed resilver with the 'unspare' flag set. 33634451Seschrock */ 33644451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 33654451Seschrock newvd = vd->vdev_child[0]; 33664451Seschrock oldvd = vd->vdev_child[1]; 33674451Seschrock 33684451Seschrock if (newvd->vdev_unspare && 33698241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 33708241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) { 33714451Seschrock newvd->vdev_unspare = 0; 33724451Seschrock return (oldvd); 33734451Seschrock } 33744451Seschrock } 33754451Seschrock 33761544Seschrock return (NULL); 3377789Sahrens } 3378789Sahrens 33791544Seschrock static void 33804451Seschrock spa_vdev_resilver_done(spa_t *spa) 3381789Sahrens { 33828241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 33838241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 33848241SJeff.Bonwick@Sun.COM 33858241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3386789Sahrens 33874451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 33888241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 33898241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 33901544Seschrock guid = vd->vdev_guid; 33918241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 33928241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 33938241SJeff.Bonwick@Sun.COM sguid = 0; 33942082Seschrock /* 33952082Seschrock * If we have just finished replacing a hot spared device, then 33962082Seschrock * we need to detach the parent's first child (the original hot 33972082Seschrock * spare) as well. 33982082Seschrock */ 33998241SJeff.Bonwick@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 34002082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 34018241SJeff.Bonwick@Sun.COM ASSERT(ppvd->vdev_children == 2); 34028241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 34032082Seschrock } 34048241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 34058241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 34061544Seschrock return; 34078241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 34082082Seschrock return; 34098241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3410789Sahrens } 3411789Sahrens 34128241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 3413789Sahrens } 3414789Sahrens 3415789Sahrens /* 34161354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 34171354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 34181354Seschrock */ 34191354Seschrock int 34201354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 34211354Seschrock { 34226643Seschrock vdev_t *vd; 34231354Seschrock uint64_t txg; 34241354Seschrock 34251354Seschrock txg = spa_vdev_enter(spa); 34261354Seschrock 34276643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 34282082Seschrock /* 34296643Seschrock * Determine if this is a reference to a hot spare device. If 34306643Seschrock * it is, update the path manually as there is no associated 34316643Seschrock * vdev_t that can be synced to disk. 34322082Seschrock */ 34336643Seschrock nvlist_t **spares; 34346643Seschrock uint_t i, nspares; 34355450Sbrendan 34365450Sbrendan if (spa->spa_spares.sav_config != NULL) { 34375450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 34385450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 34395450Sbrendan &spares, &nspares) == 0); 34402082Seschrock for (i = 0; i < nspares; i++) { 34412082Seschrock uint64_t theguid; 34422082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 34432082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 34445450Sbrendan if (theguid == guid) { 34455450Sbrendan VERIFY(nvlist_add_string(spares[i], 34465450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 34475450Sbrendan spa_load_spares(spa); 34485450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 34495450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 34505450Sbrendan 0)); 34515450Sbrendan } 34522082Seschrock } 34532082Seschrock } 34545450Sbrendan 34555450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 34562082Seschrock } 34571354Seschrock 34581585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 34591585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 34601585Sbonwick 34611354Seschrock spa_strfree(vd->vdev_path); 34621354Seschrock vd->vdev_path = spa_strdup(newpath); 34631354Seschrock 34641354Seschrock vdev_config_dirty(vd->vdev_top); 34651354Seschrock 34661354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 34671354Seschrock } 34681354Seschrock 34691354Seschrock /* 3470789Sahrens * ========================================================================== 3471789Sahrens * SPA Scrubbing 3472789Sahrens * ========================================================================== 3473789Sahrens */ 3474789Sahrens 34757046Sahrens int 34767046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3477789Sahrens { 34787754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 34794808Sek110237 3480789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3481789Sahrens return (ENOTSUP); 3482789Sahrens 3483789Sahrens /* 34847046Sahrens * If a resilver was requested, but there is no DTL on a 34857046Sahrens * writeable leaf device, we have nothing to do. 3486789Sahrens */ 34877046Sahrens if (type == POOL_SCRUB_RESILVER && 34887046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 34897046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 34901544Seschrock return (0); 34911544Seschrock } 3492789Sahrens 34937046Sahrens if (type == POOL_SCRUB_EVERYTHING && 34947046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 34957046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 34967046Sahrens return (EBUSY); 34977046Sahrens 34987046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 34997046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 35007046Sahrens } else if (type == POOL_SCRUB_NONE) { 35017046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 35021544Seschrock } else { 35037046Sahrens return (EINVAL); 35041544Seschrock } 3505789Sahrens } 3506789Sahrens 35071544Seschrock /* 35081544Seschrock * ========================================================================== 35091544Seschrock * SPA async task processing 35101544Seschrock * ========================================================================== 35111544Seschrock */ 35121544Seschrock 35131544Seschrock static void 35144451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3515789Sahrens { 35167361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 35177361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 35187361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 35197754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 35207754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 35211544Seschrock } 35227361SBrendan.Gregg@Sun.COM 35237754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 35247361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 35251544Seschrock } 35261544Seschrock 35271544Seschrock static void 35287754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 35297754SJeff.Bonwick@Sun.COM { 35307754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 35317754SJeff.Bonwick@Sun.COM vd->vdev_probe_wanted = 0; 35327754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 35337754SJeff.Bonwick@Sun.COM } 35347754SJeff.Bonwick@Sun.COM 35357754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 35367754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 35377754SJeff.Bonwick@Sun.COM } 35387754SJeff.Bonwick@Sun.COM 35397754SJeff.Bonwick@Sun.COM static void 35401544Seschrock spa_async_thread(spa_t *spa) 35411544Seschrock { 35427754SJeff.Bonwick@Sun.COM int tasks; 35431544Seschrock 35441544Seschrock ASSERT(spa->spa_sync_on); 3545789Sahrens 35461544Seschrock mutex_enter(&spa->spa_async_lock); 35471544Seschrock tasks = spa->spa_async_tasks; 35481544Seschrock spa->spa_async_tasks = 0; 35491544Seschrock mutex_exit(&spa->spa_async_lock); 35501544Seschrock 35511544Seschrock /* 35521635Sbonwick * See if the config needs to be updated. 35531635Sbonwick */ 35541635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 35551635Sbonwick mutex_enter(&spa_namespace_lock); 35561635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 35571635Sbonwick mutex_exit(&spa_namespace_lock); 35581635Sbonwick } 35591635Sbonwick 35601635Sbonwick /* 35614451Seschrock * See if any devices need to be marked REMOVED. 35621544Seschrock */ 35637754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 35647754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 35654451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 35667754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 35677361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 35687754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 35697361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 35707754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 35717754SJeff.Bonwick@Sun.COM } 35727754SJeff.Bonwick@Sun.COM 35737754SJeff.Bonwick@Sun.COM /* 35747754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 35757754SJeff.Bonwick@Sun.COM */ 35767754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 35777754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 35787754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 35797754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 35804451Seschrock } 35811544Seschrock 35821544Seschrock /* 35831544Seschrock * If any devices are done replacing, detach them. 35841544Seschrock */ 35854451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 35864451Seschrock spa_vdev_resilver_done(spa); 3587789Sahrens 35881544Seschrock /* 35891544Seschrock * Kick off a resilver. 35901544Seschrock */ 35917046Sahrens if (tasks & SPA_ASYNC_RESILVER) 35927046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 35931544Seschrock 35941544Seschrock /* 35951544Seschrock * Let the world know that we're done. 35961544Seschrock */ 35971544Seschrock mutex_enter(&spa->spa_async_lock); 35981544Seschrock spa->spa_async_thread = NULL; 35991544Seschrock cv_broadcast(&spa->spa_async_cv); 36001544Seschrock mutex_exit(&spa->spa_async_lock); 36011544Seschrock thread_exit(); 36021544Seschrock } 36031544Seschrock 36041544Seschrock void 36051544Seschrock spa_async_suspend(spa_t *spa) 36061544Seschrock { 36071544Seschrock mutex_enter(&spa->spa_async_lock); 36081544Seschrock spa->spa_async_suspended++; 36091544Seschrock while (spa->spa_async_thread != NULL) 36101544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 36111544Seschrock mutex_exit(&spa->spa_async_lock); 36121544Seschrock } 36131544Seschrock 36141544Seschrock void 36151544Seschrock spa_async_resume(spa_t *spa) 36161544Seschrock { 36171544Seschrock mutex_enter(&spa->spa_async_lock); 36181544Seschrock ASSERT(spa->spa_async_suspended != 0); 36191544Seschrock spa->spa_async_suspended--; 36201544Seschrock mutex_exit(&spa->spa_async_lock); 36211544Seschrock } 36221544Seschrock 36231544Seschrock static void 36241544Seschrock spa_async_dispatch(spa_t *spa) 36251544Seschrock { 36261544Seschrock mutex_enter(&spa->spa_async_lock); 36271544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 36281635Sbonwick spa->spa_async_thread == NULL && 36291635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 36301544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 36311544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 36321544Seschrock mutex_exit(&spa->spa_async_lock); 36331544Seschrock } 36341544Seschrock 36351544Seschrock void 36361544Seschrock spa_async_request(spa_t *spa, int task) 36371544Seschrock { 36381544Seschrock mutex_enter(&spa->spa_async_lock); 36391544Seschrock spa->spa_async_tasks |= task; 36401544Seschrock mutex_exit(&spa->spa_async_lock); 3641789Sahrens } 3642789Sahrens 3643789Sahrens /* 3644789Sahrens * ========================================================================== 3645789Sahrens * SPA syncing routines 3646789Sahrens * ========================================================================== 3647789Sahrens */ 3648789Sahrens 3649789Sahrens static void 3650789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3651789Sahrens { 3652789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3653789Sahrens dmu_tx_t *tx; 3654789Sahrens blkptr_t blk; 3655789Sahrens uint64_t itor = 0; 3656789Sahrens zio_t *zio; 3657789Sahrens int error; 3658789Sahrens uint8_t c = 1; 3659789Sahrens 36607754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 36617754SJeff.Bonwick@Sun.COM 36627754SJeff.Bonwick@Sun.COM while (bplist_iterate(bpl, &itor, &blk) == 0) { 36637754SJeff.Bonwick@Sun.COM ASSERT(blk.blk_birth < txg); 36647754SJeff.Bonwick@Sun.COM zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL, 36657754SJeff.Bonwick@Sun.COM ZIO_FLAG_MUSTSUCCEED)); 36667754SJeff.Bonwick@Sun.COM } 3667789Sahrens 3668789Sahrens error = zio_wait(zio); 3669789Sahrens ASSERT3U(error, ==, 0); 3670789Sahrens 3671789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3672789Sahrens bplist_vacate(bpl, tx); 3673789Sahrens 3674789Sahrens /* 3675789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3676789Sahrens * (Usually only the first block is needed.) 3677789Sahrens */ 3678789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3679789Sahrens dmu_tx_commit(tx); 3680789Sahrens } 3681789Sahrens 3682789Sahrens static void 36832082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 36842082Seschrock { 36852082Seschrock char *packed = NULL; 36867497STim.Haley@Sun.COM size_t bufsize; 36872082Seschrock size_t nvsize = 0; 36882082Seschrock dmu_buf_t *db; 36892082Seschrock 36902082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 36912082Seschrock 36927497STim.Haley@Sun.COM /* 36937497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 36947497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 36957497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 36967497STim.Haley@Sun.COM */ 36977497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 36987497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 36992082Seschrock 37002082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 37012082Seschrock KM_SLEEP) == 0); 37027497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 37037497STim.Haley@Sun.COM 37047497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 37057497STim.Haley@Sun.COM 37067497STim.Haley@Sun.COM kmem_free(packed, bufsize); 37072082Seschrock 37082082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 37092082Seschrock dmu_buf_will_dirty(db, tx); 37102082Seschrock *(uint64_t *)db->db_data = nvsize; 37112082Seschrock dmu_buf_rele(db, FTAG); 37122082Seschrock } 37132082Seschrock 37142082Seschrock static void 37155450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 37165450Sbrendan const char *config, const char *entry) 37172082Seschrock { 37182082Seschrock nvlist_t *nvroot; 37195450Sbrendan nvlist_t **list; 37202082Seschrock int i; 37212082Seschrock 37225450Sbrendan if (!sav->sav_sync) 37232082Seschrock return; 37242082Seschrock 37252082Seschrock /* 37265450Sbrendan * Update the MOS nvlist describing the list of available devices. 37275450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 37284451Seschrock * valid and the vdevs are labeled appropriately. 37292082Seschrock */ 37305450Sbrendan if (sav->sav_object == 0) { 37315450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 37325450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 37335450Sbrendan sizeof (uint64_t), tx); 37342082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 37355450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 37365450Sbrendan &sav->sav_object, tx) == 0); 37372082Seschrock } 37382082Seschrock 37392082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 37405450Sbrendan if (sav->sav_count == 0) { 37415450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 37422082Seschrock } else { 37435450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 37445450Sbrendan for (i = 0; i < sav->sav_count; i++) 37455450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 37465450Sbrendan B_FALSE, B_FALSE, B_TRUE); 37475450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 37485450Sbrendan sav->sav_count) == 0); 37495450Sbrendan for (i = 0; i < sav->sav_count; i++) 37505450Sbrendan nvlist_free(list[i]); 37515450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 37522082Seschrock } 37532082Seschrock 37545450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 37552926Sek110237 nvlist_free(nvroot); 37562082Seschrock 37575450Sbrendan sav->sav_sync = B_FALSE; 37582082Seschrock } 37592082Seschrock 37602082Seschrock static void 3761789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3762789Sahrens { 3763789Sahrens nvlist_t *config; 3764789Sahrens 37657754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 3766789Sahrens return; 3767789Sahrens 37687754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 37697754SJeff.Bonwick@Sun.COM 37707754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 37717754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 37727754SJeff.Bonwick@Sun.COM 37737754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 3774789Sahrens 37751635Sbonwick if (spa->spa_config_syncing) 37761635Sbonwick nvlist_free(spa->spa_config_syncing); 37771635Sbonwick spa->spa_config_syncing = config; 3778789Sahrens 37792082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3780789Sahrens } 3781789Sahrens 37825094Slling /* 37835094Slling * Set zpool properties. 37845094Slling */ 37853912Slling static void 37864543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 37873912Slling { 37883912Slling spa_t *spa = arg1; 37895094Slling objset_t *mos = spa->spa_meta_objset; 37903912Slling nvlist_t *nvp = arg2; 37915094Slling nvpair_t *elem; 37924451Seschrock uint64_t intval; 37936643Seschrock char *strval; 37945094Slling zpool_prop_t prop; 37955094Slling const char *propname; 37965094Slling zprop_type_t proptype; 37976643Seschrock spa_config_dirent_t *dp; 37985094Slling 37997754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 38007754SJeff.Bonwick@Sun.COM 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 38367754SJeff.Bonwick@Sun.COM dp = kmem_alloc(sizeof (spa_config_dirent_t), KM_SLEEP); 38376643Seschrock 38386643Seschrock if (strval[0] == '\0') 38396643Seschrock dp->scd_path = spa_strdup(spa_config_path); 38406643Seschrock else if (strcmp(strval, "none") == 0) 38416643Seschrock dp->scd_path = NULL; 38426643Seschrock else 38436643Seschrock dp->scd_path = spa_strdup(strval); 38446643Seschrock 38456643Seschrock list_insert_head(&spa->spa_config_list, dp); 38465363Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 38474543Smarks break; 38485094Slling default: 38495094Slling /* 38505094Slling * Set pool property values in the poolprops mos object. 38515094Slling */ 38525094Slling if (spa->spa_pool_props_object == 0) { 38535094Slling objset_t *mos = spa->spa_meta_objset; 38545094Slling 38555094Slling VERIFY((spa->spa_pool_props_object = 38565094Slling zap_create(mos, DMU_OT_POOL_PROPS, 38575094Slling DMU_OT_NONE, 0, tx)) > 0); 38585094Slling 38595094Slling VERIFY(zap_update(mos, 38605094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 38615094Slling 8, 1, &spa->spa_pool_props_object, tx) 38625094Slling == 0); 38635094Slling } 38645094Slling 38655094Slling /* normalize the property name */ 38665094Slling propname = zpool_prop_to_name(prop); 38675094Slling proptype = zpool_prop_get_type(prop); 38685094Slling 38695094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 38705094Slling ASSERT(proptype == PROP_TYPE_STRING); 38715094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 38725094Slling VERIFY(zap_update(mos, 38735094Slling spa->spa_pool_props_object, propname, 38745094Slling 1, strlen(strval) + 1, strval, tx) == 0); 38755094Slling 38765094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 38775094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 38785094Slling 38795094Slling if (proptype == PROP_TYPE_INDEX) { 38805094Slling const char *unused; 38815094Slling VERIFY(zpool_prop_index_to_string( 38825094Slling prop, intval, &unused) == 0); 38835094Slling } 38845094Slling VERIFY(zap_update(mos, 38855094Slling spa->spa_pool_props_object, propname, 38865094Slling 8, 1, &intval, tx) == 0); 38875094Slling } else { 38885094Slling ASSERT(0); /* not allowed */ 38895094Slling } 38905094Slling 38915329Sgw25295 switch (prop) { 38925329Sgw25295 case ZPOOL_PROP_DELEGATION: 38935094Slling spa->spa_delegation = intval; 38945329Sgw25295 break; 38955329Sgw25295 case ZPOOL_PROP_BOOTFS: 38965094Slling spa->spa_bootfs = intval; 38975329Sgw25295 break; 38985329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 38995329Sgw25295 spa->spa_failmode = intval; 39005329Sgw25295 break; 39015329Sgw25295 default: 39025329Sgw25295 break; 39035329Sgw25295 } 39043912Slling } 39055094Slling 39065094Slling /* log internal history if this is not a zpool create */ 39075094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 39085094Slling tx->tx_txg != TXG_INITIAL) { 39095094Slling spa_history_internal_log(LOG_POOL_PROPSET, 39105094Slling spa, tx, cr, "%s %lld %s", 39117754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 39125094Slling } 39133912Slling } 39147754SJeff.Bonwick@Sun.COM 39157754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 39163912Slling } 39173912Slling 3918789Sahrens /* 3919789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3920789Sahrens * part of the process, so we iterate until it converges. 3921789Sahrens */ 3922789Sahrens void 3923789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3924789Sahrens { 3925789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3926789Sahrens objset_t *mos = spa->spa_meta_objset; 3927789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 39281635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3929789Sahrens vdev_t *vd; 3930789Sahrens dmu_tx_t *tx; 3931789Sahrens int dirty_vdevs; 39327754SJeff.Bonwick@Sun.COM int error; 3933789Sahrens 3934789Sahrens /* 3935789Sahrens * Lock out configuration changes. 3936789Sahrens */ 39377754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3938789Sahrens 3939789Sahrens spa->spa_syncing_txg = txg; 3940789Sahrens spa->spa_sync_pass = 0; 3941789Sahrens 39427754SJeff.Bonwick@Sun.COM /* 39437754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 39447754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 39457754SJeff.Bonwick@Sun.COM */ 39467754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 39478241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 39488241SJeff.Bonwick@Sun.COM /* 39498241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 39508241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 39518241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 39528241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 39538241SJeff.Bonwick@Sun.COM * into the root vdev tree. 39548241SJeff.Bonwick@Sun.COM */ 39558241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 39568241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 39578241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 39588241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 39598241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 39608241SJeff.Bonwick@Sun.COM } 39618241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 39628241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 39637754SJeff.Bonwick@Sun.COM } 39647754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 39657754SJeff.Bonwick@Sun.COM 39661544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3967789Sahrens 39682082Seschrock tx = dmu_tx_create_assigned(dp, txg); 39692082Seschrock 39702082Seschrock /* 39714577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 39722082Seschrock * set spa_deflate if we have no raid-z vdevs. 39732082Seschrock */ 39744577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 39754577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 39762082Seschrock int i; 39772082Seschrock 39782082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 39792082Seschrock vd = rvd->vdev_child[i]; 39802082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 39812082Seschrock break; 39822082Seschrock } 39832082Seschrock if (i == rvd->vdev_children) { 39842082Seschrock spa->spa_deflate = TRUE; 39852082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 39862082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 39872082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 39882082Seschrock } 39892082Seschrock } 39902082Seschrock 39917046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 39927046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 39937046Sahrens dsl_pool_create_origin(dp, tx); 39947046Sahrens 39957046Sahrens /* Keeping the origin open increases spa_minref */ 39967046Sahrens spa->spa_minref += 3; 39977046Sahrens } 39987046Sahrens 39997046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 40007046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 40017046Sahrens dsl_pool_upgrade_clones(dp, tx); 40027046Sahrens } 40037046Sahrens 4004789Sahrens /* 4005789Sahrens * If anything has changed in this txg, push the deferred frees 4006789Sahrens * from the previous txg. If not, leave them alone so that we 4007789Sahrens * don't generate work on an otherwise idle system. 4008789Sahrens */ 4009789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 40102329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 40112329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 4012789Sahrens spa_sync_deferred_frees(spa, txg); 4013789Sahrens 4014789Sahrens /* 4015789Sahrens * Iterate to convergence. 4016789Sahrens */ 4017789Sahrens do { 4018789Sahrens spa->spa_sync_pass++; 4019789Sahrens 4020789Sahrens spa_sync_config_object(spa, tx); 40215450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 40225450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 40235450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 40245450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 40251544Seschrock spa_errlog_sync(spa, txg); 4026789Sahrens dsl_pool_sync(dp, txg); 4027789Sahrens 4028789Sahrens dirty_vdevs = 0; 4029789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4030789Sahrens vdev_sync(vd, txg); 4031789Sahrens dirty_vdevs++; 4032789Sahrens } 4033789Sahrens 4034789Sahrens bplist_sync(bpl, tx); 4035789Sahrens } while (dirty_vdevs); 4036789Sahrens 4037789Sahrens bplist_close(bpl); 4038789Sahrens 4039789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4040789Sahrens 4041789Sahrens /* 4042789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4043789Sahrens * to commit the transaction group. 40441635Sbonwick * 40455688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 40465688Sbonwick * random top-level vdevs that are known to be visible in the 40477754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 40487754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4049789Sahrens */ 40507754SJeff.Bonwick@Sun.COM for (;;) { 40517754SJeff.Bonwick@Sun.COM /* 40527754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 40537754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 40547754SJeff.Bonwick@Sun.COM */ 40557754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 40567754SJeff.Bonwick@Sun.COM 40577754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 40587754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 40597754SJeff.Bonwick@Sun.COM int svdcount = 0; 40607754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 40617754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 40627754SJeff.Bonwick@Sun.COM int c; 40637754SJeff.Bonwick@Sun.COM 40647754SJeff.Bonwick@Sun.COM for (c = 0; c < children; c++) { 40657754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 40667754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 40677754SJeff.Bonwick@Sun.COM continue; 40687754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 40697754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 40707754SJeff.Bonwick@Sun.COM break; 40717754SJeff.Bonwick@Sun.COM } 40727754SJeff.Bonwick@Sun.COM error = vdev_config_sync(svd, svdcount, txg); 40737754SJeff.Bonwick@Sun.COM } else { 40747754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 40757754SJeff.Bonwick@Sun.COM rvd->vdev_children, txg); 40761635Sbonwick } 40777754SJeff.Bonwick@Sun.COM 40787754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 40797754SJeff.Bonwick@Sun.COM 40807754SJeff.Bonwick@Sun.COM if (error == 0) 40817754SJeff.Bonwick@Sun.COM break; 40827754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 40837754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 40841635Sbonwick } 40852082Seschrock dmu_tx_commit(tx); 40862082Seschrock 40871635Sbonwick /* 40881635Sbonwick * Clear the dirty config list. 40891635Sbonwick */ 40907754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 40911635Sbonwick vdev_config_clean(vd); 40921635Sbonwick 40931635Sbonwick /* 40941635Sbonwick * Now that the new config has synced transactionally, 40951635Sbonwick * let it become visible to the config cache. 40961635Sbonwick */ 40971635Sbonwick if (spa->spa_config_syncing != NULL) { 40981635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 40991635Sbonwick spa->spa_config_txg = txg; 41001635Sbonwick spa->spa_config_syncing = NULL; 41011635Sbonwick } 4102789Sahrens 4103789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4104789Sahrens 4105789Sahrens /* 4106789Sahrens * Clean up the ZIL records for the synced txg. 4107789Sahrens */ 4108789Sahrens dsl_pool_zil_clean(dp); 4109789Sahrens 4110789Sahrens /* 4111789Sahrens * Update usable space statistics. 4112789Sahrens */ 4113789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4114789Sahrens vdev_sync_done(vd, txg); 4115789Sahrens 4116789Sahrens /* 4117789Sahrens * It had better be the case that we didn't dirty anything 41182082Seschrock * since vdev_config_sync(). 4119789Sahrens */ 4120789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4121789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4122789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4123789Sahrens ASSERT(bpl->bpl_queue == NULL); 4124789Sahrens 41257754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 41261544Seschrock 41271544Seschrock /* 41281544Seschrock * If any async tasks have been requested, kick them off. 41291544Seschrock */ 41301544Seschrock spa_async_dispatch(spa); 4131789Sahrens } 4132789Sahrens 4133789Sahrens /* 4134789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4135789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4136789Sahrens * sync. 4137789Sahrens */ 4138789Sahrens void 4139789Sahrens spa_sync_allpools(void) 4140789Sahrens { 4141789Sahrens spa_t *spa = NULL; 4142789Sahrens mutex_enter(&spa_namespace_lock); 4143789Sahrens while ((spa = spa_next(spa)) != NULL) { 41447754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4145789Sahrens continue; 4146789Sahrens spa_open_ref(spa, FTAG); 4147789Sahrens mutex_exit(&spa_namespace_lock); 4148789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4149789Sahrens mutex_enter(&spa_namespace_lock); 4150789Sahrens spa_close(spa, FTAG); 4151789Sahrens } 4152789Sahrens mutex_exit(&spa_namespace_lock); 4153789Sahrens } 4154789Sahrens 4155789Sahrens /* 4156789Sahrens * ========================================================================== 4157789Sahrens * Miscellaneous routines 4158789Sahrens * ========================================================================== 4159789Sahrens */ 4160789Sahrens 4161789Sahrens /* 4162789Sahrens * Remove all pools in the system. 4163789Sahrens */ 4164789Sahrens void 4165789Sahrens spa_evict_all(void) 4166789Sahrens { 4167789Sahrens spa_t *spa; 4168789Sahrens 4169789Sahrens /* 4170789Sahrens * Remove all cached state. All pools should be closed now, 4171789Sahrens * so every spa in the AVL tree should be unreferenced. 4172789Sahrens */ 4173789Sahrens mutex_enter(&spa_namespace_lock); 4174789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4175789Sahrens /* 41761544Seschrock * Stop async tasks. The async thread may need to detach 41771544Seschrock * a device that's been replaced, which requires grabbing 41781544Seschrock * spa_namespace_lock, so we must drop it here. 4179789Sahrens */ 4180789Sahrens spa_open_ref(spa, FTAG); 4181789Sahrens mutex_exit(&spa_namespace_lock); 41821544Seschrock spa_async_suspend(spa); 41834808Sek110237 mutex_enter(&spa_namespace_lock); 4184789Sahrens spa_close(spa, FTAG); 4185789Sahrens 4186789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4187789Sahrens spa_unload(spa); 4188789Sahrens spa_deactivate(spa); 4189789Sahrens } 4190789Sahrens spa_remove(spa); 4191789Sahrens } 4192789Sahrens mutex_exit(&spa_namespace_lock); 4193789Sahrens } 41941544Seschrock 41951544Seschrock vdev_t * 41966643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 41971544Seschrock { 41986643Seschrock vdev_t *vd; 41996643Seschrock int i; 42006643Seschrock 42016643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 42026643Seschrock return (vd); 42036643Seschrock 42046643Seschrock if (l2cache) { 42056643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 42066643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 42076643Seschrock if (vd->vdev_guid == guid) 42086643Seschrock return (vd); 42096643Seschrock } 42106643Seschrock } 42116643Seschrock 42126643Seschrock return (NULL); 42131544Seschrock } 42141760Seschrock 42151760Seschrock void 42165094Slling spa_upgrade(spa_t *spa, uint64_t version) 42171760Seschrock { 42187754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 42191760Seschrock 42201760Seschrock /* 42211760Seschrock * This should only be called for a non-faulted pool, and since a 42221760Seschrock * future version would result in an unopenable pool, this shouldn't be 42231760Seschrock * possible. 42241760Seschrock */ 42254577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 42265094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 42275094Slling 42285094Slling spa->spa_uberblock.ub_version = version; 42291760Seschrock vdev_config_dirty(spa->spa_root_vdev); 42301760Seschrock 42317754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 42322082Seschrock 42332082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 42341760Seschrock } 42352082Seschrock 42362082Seschrock boolean_t 42372082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 42382082Seschrock { 42392082Seschrock int i; 42403377Seschrock uint64_t spareguid; 42415450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 42425450Sbrendan 42435450Sbrendan for (i = 0; i < sav->sav_count; i++) 42445450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 42452082Seschrock return (B_TRUE); 42462082Seschrock 42475450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 42485450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 42495450Sbrendan &spareguid) == 0 && spareguid == guid) 42503377Seschrock return (B_TRUE); 42513377Seschrock } 42523377Seschrock 42532082Seschrock return (B_FALSE); 42542082Seschrock } 42553912Slling 42564451Seschrock /* 42577214Slling * Check if a pool has an active shared spare device. 42587214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 42597214Slling */ 42607214Slling static boolean_t 42617214Slling spa_has_active_shared_spare(spa_t *spa) 42627214Slling { 42637214Slling int i, refcnt; 42647214Slling uint64_t pool; 42657214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 42667214Slling 42677214Slling for (i = 0; i < sav->sav_count; i++) { 42687214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 42697214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 42707214Slling refcnt > 2) 42717214Slling return (B_TRUE); 42727214Slling } 42737214Slling 42747214Slling return (B_FALSE); 42757214Slling } 42767214Slling 42777214Slling /* 42784451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 42794451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 42804451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 42814451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 42824451Seschrock * or zdb as real changes. 42834451Seschrock */ 42844451Seschrock void 42854451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 42864451Seschrock { 42874451Seschrock #ifdef _KERNEL 42884451Seschrock sysevent_t *ev; 42894451Seschrock sysevent_attr_list_t *attr = NULL; 42904451Seschrock sysevent_value_t value; 42914451Seschrock sysevent_id_t eid; 42924451Seschrock 42934451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 42944451Seschrock SE_SLEEP); 42954451Seschrock 42964451Seschrock value.value_type = SE_DATA_TYPE_STRING; 42974451Seschrock value.value.sv_string = spa_name(spa); 42984451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 42994451Seschrock goto done; 43004451Seschrock 43014451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 43024451Seschrock value.value.sv_uint64 = spa_guid(spa); 43034451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 43044451Seschrock goto done; 43054451Seschrock 43064451Seschrock if (vd) { 43074451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 43084451Seschrock value.value.sv_uint64 = vd->vdev_guid; 43094451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 43104451Seschrock SE_SLEEP) != 0) 43114451Seschrock goto done; 43124451Seschrock 43134451Seschrock if (vd->vdev_path) { 43144451Seschrock value.value_type = SE_DATA_TYPE_STRING; 43154451Seschrock value.value.sv_string = vd->vdev_path; 43164451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 43174451Seschrock &value, SE_SLEEP) != 0) 43184451Seschrock goto done; 43194451Seschrock } 43204451Seschrock } 43214451Seschrock 43225756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 43235756Seschrock goto done; 43245756Seschrock attr = NULL; 43255756Seschrock 43264451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 43274451Seschrock 43284451Seschrock done: 43294451Seschrock if (attr) 43304451Seschrock sysevent_free_attr(attr); 43314451Seschrock sysevent_free(ev); 43324451Seschrock #endif 43334451Seschrock } 4334