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 489789Sahrens spa_activate(spa_t *spa) 490789Sahrens { 491789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 492789Sahrens 493789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 494789Sahrens 495789Sahrens spa->spa_normal_class = metaslab_class_create(); 4964527Sperrin spa->spa_log_class = metaslab_class_create(); 497789Sahrens 4987754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 4997754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5007754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create("spa_zio", 5017754SJeff.Bonwick@Sun.COM zio_taskq_threads[t][q], maxclsyspri, 50, 5027754SJeff.Bonwick@Sun.COM INT_MAX, TASKQ_PREPOPULATE); 5037754SJeff.Bonwick@Sun.COM } 504789Sahrens } 505789Sahrens 5067754SJeff.Bonwick@Sun.COM list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 5077754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_config_dirty_node)); 5087754SJeff.Bonwick@Sun.COM list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 5097754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_state_dirty_node)); 510789Sahrens 511789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 512789Sahrens offsetof(struct vdev, vdev_txg_node)); 5131544Seschrock 5141544Seschrock avl_create(&spa->spa_errlist_scrub, 5151544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5161544Seschrock offsetof(spa_error_entry_t, se_avl)); 5171544Seschrock avl_create(&spa->spa_errlist_last, 5181544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5191544Seschrock offsetof(spa_error_entry_t, se_avl)); 520789Sahrens } 521789Sahrens 522789Sahrens /* 523789Sahrens * Opposite of spa_activate(). 524789Sahrens */ 525789Sahrens static void 526789Sahrens spa_deactivate(spa_t *spa) 527789Sahrens { 528789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 529789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 530789Sahrens ASSERT(spa->spa_root_vdev == NULL); 531789Sahrens 532789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 533789Sahrens 534789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 535789Sahrens 5367754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_config_dirty_list); 5377754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_state_dirty_list); 5387754SJeff.Bonwick@Sun.COM 5397754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 5407754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5417754SJeff.Bonwick@Sun.COM taskq_destroy(spa->spa_zio_taskq[t][q]); 5427754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 5437754SJeff.Bonwick@Sun.COM } 544789Sahrens } 545789Sahrens 546789Sahrens metaslab_class_destroy(spa->spa_normal_class); 547789Sahrens spa->spa_normal_class = NULL; 548789Sahrens 5494527Sperrin metaslab_class_destroy(spa->spa_log_class); 5504527Sperrin spa->spa_log_class = NULL; 5514527Sperrin 5521544Seschrock /* 5531544Seschrock * If this was part of an import or the open otherwise failed, we may 5541544Seschrock * still have errors left in the queues. Empty them just in case. 5551544Seschrock */ 5561544Seschrock spa_errlog_drain(spa); 5571544Seschrock 5581544Seschrock avl_destroy(&spa->spa_errlist_scrub); 5591544Seschrock avl_destroy(&spa->spa_errlist_last); 5601544Seschrock 561789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 562789Sahrens } 563789Sahrens 564789Sahrens /* 565789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 566789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 567789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 568789Sahrens * All vdev validation is done by the vdev_alloc() routine. 569789Sahrens */ 5702082Seschrock static int 5712082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 5722082Seschrock uint_t id, int atype) 573789Sahrens { 574789Sahrens nvlist_t **child; 575789Sahrens uint_t c, children; 5762082Seschrock int error; 5772082Seschrock 5782082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 5792082Seschrock return (error); 5802082Seschrock 5812082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 5822082Seschrock return (0); 583789Sahrens 5847754SJeff.Bonwick@Sun.COM error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 5857754SJeff.Bonwick@Sun.COM &child, &children); 5867754SJeff.Bonwick@Sun.COM 5877754SJeff.Bonwick@Sun.COM if (error == ENOENT) 5887754SJeff.Bonwick@Sun.COM return (0); 5897754SJeff.Bonwick@Sun.COM 5907754SJeff.Bonwick@Sun.COM if (error) { 5912082Seschrock vdev_free(*vdp); 5922082Seschrock *vdp = NULL; 5932082Seschrock return (EINVAL); 594789Sahrens } 595789Sahrens 596789Sahrens for (c = 0; c < children; c++) { 5972082Seschrock vdev_t *vd; 5982082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 5992082Seschrock atype)) != 0) { 6002082Seschrock vdev_free(*vdp); 6012082Seschrock *vdp = NULL; 6022082Seschrock return (error); 603789Sahrens } 604789Sahrens } 605789Sahrens 6062082Seschrock ASSERT(*vdp != NULL); 6072082Seschrock 6082082Seschrock return (0); 609789Sahrens } 610789Sahrens 611789Sahrens /* 612789Sahrens * Opposite of spa_load(). 613789Sahrens */ 614789Sahrens static void 615789Sahrens spa_unload(spa_t *spa) 616789Sahrens { 6172082Seschrock int i; 6182082Seschrock 6197754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6207754SJeff.Bonwick@Sun.COM 621789Sahrens /* 6221544Seschrock * Stop async tasks. 6231544Seschrock */ 6241544Seschrock spa_async_suspend(spa); 6251544Seschrock 6261544Seschrock /* 627789Sahrens * Stop syncing. 628789Sahrens */ 629789Sahrens if (spa->spa_sync_on) { 630789Sahrens txg_sync_stop(spa->spa_dsl_pool); 631789Sahrens spa->spa_sync_on = B_FALSE; 632789Sahrens } 633789Sahrens 634789Sahrens /* 6357754SJeff.Bonwick@Sun.COM * Wait for any outstanding async I/O to complete. 636789Sahrens */ 6377754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_async_root_lock); 6387754SJeff.Bonwick@Sun.COM while (spa->spa_async_root_count != 0) 6397754SJeff.Bonwick@Sun.COM cv_wait(&spa->spa_async_root_cv, &spa->spa_async_root_lock); 6407754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_async_root_lock); 641789Sahrens 642789Sahrens /* 6435450Sbrendan * Drop and purge level 2 cache 6445450Sbrendan */ 6455450Sbrendan spa_l2cache_drop(spa); 6465450Sbrendan 6475450Sbrendan /* 648789Sahrens * Close the dsl pool. 649789Sahrens */ 650789Sahrens if (spa->spa_dsl_pool) { 651789Sahrens dsl_pool_close(spa->spa_dsl_pool); 652789Sahrens spa->spa_dsl_pool = NULL; 653789Sahrens } 654789Sahrens 655789Sahrens /* 656789Sahrens * Close all vdevs. 657789Sahrens */ 6581585Sbonwick if (spa->spa_root_vdev) 659789Sahrens vdev_free(spa->spa_root_vdev); 6601585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 6611544Seschrock 6625450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 6635450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 6645450Sbrendan if (spa->spa_spares.sav_vdevs) { 6655450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 6665450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 6675450Sbrendan spa->spa_spares.sav_vdevs = NULL; 6685450Sbrendan } 6695450Sbrendan if (spa->spa_spares.sav_config) { 6705450Sbrendan nvlist_free(spa->spa_spares.sav_config); 6715450Sbrendan spa->spa_spares.sav_config = NULL; 6722082Seschrock } 6737377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 6745450Sbrendan 6755450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 6765450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 6775450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 6785450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 6795450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 6805450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 6815450Sbrendan } 6825450Sbrendan if (spa->spa_l2cache.sav_config) { 6835450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 6845450Sbrendan spa->spa_l2cache.sav_config = NULL; 6852082Seschrock } 6867377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 6872082Seschrock 6881544Seschrock spa->spa_async_suspended = 0; 689789Sahrens } 690789Sahrens 691789Sahrens /* 6922082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 6932082Seschrock * this pool. When this is called, we have some form of basic information in 6945450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 6955450Sbrendan * then re-generate a more complete list including status information. 6962082Seschrock */ 6972082Seschrock static void 6982082Seschrock spa_load_spares(spa_t *spa) 6992082Seschrock { 7002082Seschrock nvlist_t **spares; 7012082Seschrock uint_t nspares; 7022082Seschrock int i; 7033377Seschrock vdev_t *vd, *tvd; 7042082Seschrock 7057754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 7067754SJeff.Bonwick@Sun.COM 7072082Seschrock /* 7082082Seschrock * First, close and free any existing spare vdevs. 7092082Seschrock */ 7105450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7115450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 7123377Seschrock 7133377Seschrock /* Undo the call to spa_activate() below */ 7146643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7156643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 7163377Seschrock spa_spare_remove(tvd); 7173377Seschrock vdev_close(vd); 7183377Seschrock vdev_free(vd); 7192082Seschrock } 7203377Seschrock 7215450Sbrendan if (spa->spa_spares.sav_vdevs) 7225450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7235450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7245450Sbrendan 7255450Sbrendan if (spa->spa_spares.sav_config == NULL) 7262082Seschrock nspares = 0; 7272082Seschrock else 7285450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 7292082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 7302082Seschrock 7315450Sbrendan spa->spa_spares.sav_count = (int)nspares; 7325450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7332082Seschrock 7342082Seschrock if (nspares == 0) 7352082Seschrock return; 7362082Seschrock 7372082Seschrock /* 7382082Seschrock * Construct the array of vdevs, opening them to get status in the 7393377Seschrock * process. For each spare, there is potentially two different vdev_t 7403377Seschrock * structures associated with it: one in the list of spares (used only 7413377Seschrock * for basic validation purposes) and one in the active vdev 7423377Seschrock * configuration (if it's spared in). During this phase we open and 7433377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 7443377Seschrock * active configuration, then we also mark this vdev as an active spare. 7452082Seschrock */ 7465450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 7475450Sbrendan KM_SLEEP); 7485450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7492082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 7502082Seschrock VDEV_ALLOC_SPARE) == 0); 7512082Seschrock ASSERT(vd != NULL); 7522082Seschrock 7535450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 7542082Seschrock 7556643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7566643Seschrock B_FALSE)) != NULL) { 7573377Seschrock if (!tvd->vdev_isspare) 7583377Seschrock spa_spare_add(tvd); 7593377Seschrock 7603377Seschrock /* 7613377Seschrock * We only mark the spare active if we were successfully 7623377Seschrock * able to load the vdev. Otherwise, importing a pool 7633377Seschrock * with a bad active spare would result in strange 7643377Seschrock * behavior, because multiple pool would think the spare 7653377Seschrock * is actively in use. 7663377Seschrock * 7673377Seschrock * There is a vulnerability here to an equally bizarre 7683377Seschrock * circumstance, where a dead active spare is later 7693377Seschrock * brought back to life (onlined or otherwise). Given 7703377Seschrock * the rarity of this scenario, and the extra complexity 7713377Seschrock * it adds, we ignore the possibility. 7723377Seschrock */ 7733377Seschrock if (!vdev_is_dead(tvd)) 7743377Seschrock spa_spare_activate(tvd); 7753377Seschrock } 7763377Seschrock 7777754SJeff.Bonwick@Sun.COM vd->vdev_top = vd; 7787754SJeff.Bonwick@Sun.COM 7792082Seschrock if (vdev_open(vd) != 0) 7802082Seschrock continue; 7812082Seschrock 7825450Sbrendan if (vdev_validate_aux(vd) == 0) 7835450Sbrendan spa_spare_add(vd); 7842082Seschrock } 7852082Seschrock 7862082Seschrock /* 7872082Seschrock * Recompute the stashed list of spares, with status information 7882082Seschrock * this time. 7892082Seschrock */ 7905450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 7912082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 7922082Seschrock 7935450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 7945450Sbrendan KM_SLEEP); 7955450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7965450Sbrendan spares[i] = vdev_config_generate(spa, 7975450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 7985450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 7995450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 8005450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 8012082Seschrock nvlist_free(spares[i]); 8025450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 8035450Sbrendan } 8045450Sbrendan 8055450Sbrendan /* 8065450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 8075450Sbrendan * this pool. When this is called, we have some form of basic information in 8085450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 8095450Sbrendan * then re-generate a more complete list including status information. 8105450Sbrendan * Devices which are already active have their details maintained, and are 8115450Sbrendan * not re-opened. 8125450Sbrendan */ 8135450Sbrendan static void 8145450Sbrendan spa_load_l2cache(spa_t *spa) 8155450Sbrendan { 8165450Sbrendan nvlist_t **l2cache; 8175450Sbrendan uint_t nl2cache; 8185450Sbrendan int i, j, oldnvdevs; 8196643Seschrock uint64_t guid, size; 8205450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 8215450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 8225450Sbrendan 8237754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 8247754SJeff.Bonwick@Sun.COM 8255450Sbrendan if (sav->sav_config != NULL) { 8265450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 8275450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 8285450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 8295450Sbrendan } else { 8305450Sbrendan nl2cache = 0; 8315450Sbrendan } 8325450Sbrendan 8335450Sbrendan oldvdevs = sav->sav_vdevs; 8345450Sbrendan oldnvdevs = sav->sav_count; 8355450Sbrendan sav->sav_vdevs = NULL; 8365450Sbrendan sav->sav_count = 0; 8375450Sbrendan 8385450Sbrendan /* 8395450Sbrendan * Process new nvlist of vdevs. 8405450Sbrendan */ 8415450Sbrendan for (i = 0; i < nl2cache; i++) { 8425450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 8435450Sbrendan &guid) == 0); 8445450Sbrendan 8455450Sbrendan newvdevs[i] = NULL; 8465450Sbrendan for (j = 0; j < oldnvdevs; j++) { 8475450Sbrendan vd = oldvdevs[j]; 8485450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 8495450Sbrendan /* 8505450Sbrendan * Retain previous vdev for add/remove ops. 8515450Sbrendan */ 8525450Sbrendan newvdevs[i] = vd; 8535450Sbrendan oldvdevs[j] = NULL; 8545450Sbrendan break; 8555450Sbrendan } 8565450Sbrendan } 8575450Sbrendan 8585450Sbrendan if (newvdevs[i] == NULL) { 8595450Sbrendan /* 8605450Sbrendan * Create new vdev 8615450Sbrendan */ 8625450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 8635450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 8645450Sbrendan ASSERT(vd != NULL); 8655450Sbrendan newvdevs[i] = vd; 8665450Sbrendan 8675450Sbrendan /* 8685450Sbrendan * Commit this vdev as an l2cache device, 8695450Sbrendan * even if it fails to open. 8705450Sbrendan */ 8715450Sbrendan spa_l2cache_add(vd); 8725450Sbrendan 8736643Seschrock vd->vdev_top = vd; 8746643Seschrock vd->vdev_aux = sav; 8756643Seschrock 8766643Seschrock spa_l2cache_activate(vd); 8776643Seschrock 8785450Sbrendan if (vdev_open(vd) != 0) 8795450Sbrendan continue; 8805450Sbrendan 8815450Sbrendan (void) vdev_validate_aux(vd); 8825450Sbrendan 8835450Sbrendan if (!vdev_is_dead(vd)) { 8845450Sbrendan size = vdev_get_rsize(vd); 8856643Seschrock l2arc_add_vdev(spa, vd, 8866643Seschrock VDEV_LABEL_START_SIZE, 8876643Seschrock size - VDEV_LABEL_START_SIZE); 8885450Sbrendan } 8895450Sbrendan } 8905450Sbrendan } 8915450Sbrendan 8925450Sbrendan /* 8935450Sbrendan * Purge vdevs that were dropped 8945450Sbrendan */ 8955450Sbrendan for (i = 0; i < oldnvdevs; i++) { 8965450Sbrendan uint64_t pool; 8975450Sbrendan 8985450Sbrendan vd = oldvdevs[i]; 8995450Sbrendan if (vd != NULL) { 9007754SJeff.Bonwick@Sun.COM if ((spa_mode & FWRITE) && 9015450Sbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && 9026643Seschrock pool != 0ULL && 9036643Seschrock l2arc_vdev_present(vd)) { 9045450Sbrendan l2arc_remove_vdev(vd); 9055450Sbrendan } 9065450Sbrendan (void) vdev_close(vd); 9075450Sbrendan spa_l2cache_remove(vd); 9085450Sbrendan } 9095450Sbrendan } 9105450Sbrendan 9115450Sbrendan if (oldvdevs) 9125450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 9135450Sbrendan 9145450Sbrendan if (sav->sav_config == NULL) 9155450Sbrendan goto out; 9165450Sbrendan 9175450Sbrendan sav->sav_vdevs = newvdevs; 9185450Sbrendan sav->sav_count = (int)nl2cache; 9195450Sbrendan 9205450Sbrendan /* 9215450Sbrendan * Recompute the stashed list of l2cache devices, with status 9225450Sbrendan * information this time. 9235450Sbrendan */ 9245450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 9255450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 9265450Sbrendan 9275450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 9285450Sbrendan for (i = 0; i < sav->sav_count; i++) 9295450Sbrendan l2cache[i] = vdev_config_generate(spa, 9305450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 9315450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 9325450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 9335450Sbrendan out: 9345450Sbrendan for (i = 0; i < sav->sav_count; i++) 9355450Sbrendan nvlist_free(l2cache[i]); 9365450Sbrendan if (sav->sav_count) 9375450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 9382082Seschrock } 9392082Seschrock 9402082Seschrock static int 9412082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 9422082Seschrock { 9432082Seschrock dmu_buf_t *db; 9442082Seschrock char *packed = NULL; 9452082Seschrock size_t nvsize = 0; 9462082Seschrock int error; 9472082Seschrock *value = NULL; 9482082Seschrock 9492082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 9502082Seschrock nvsize = *(uint64_t *)db->db_data; 9512082Seschrock dmu_buf_rele(db, FTAG); 9522082Seschrock 9532082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 9542082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 9552082Seschrock if (error == 0) 9562082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 9572082Seschrock kmem_free(packed, nvsize); 9582082Seschrock 9592082Seschrock return (error); 9602082Seschrock } 9612082Seschrock 9622082Seschrock /* 9634451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9644451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9654451Seschrock */ 9664451Seschrock static void 9674451Seschrock spa_check_removed(vdev_t *vd) 9684451Seschrock { 9694451Seschrock int c; 9704451Seschrock 9714451Seschrock for (c = 0; c < vd->vdev_children; c++) 9724451Seschrock spa_check_removed(vd->vdev_child[c]); 9734451Seschrock 9744451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9754451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9764451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9774451Seschrock } 9784451Seschrock } 9794451Seschrock 9804451Seschrock /* 9817294Sperrin * Check for missing log devices 9827294Sperrin */ 9837294Sperrin int 9847294Sperrin spa_check_logs(spa_t *spa) 9857294Sperrin { 9867294Sperrin switch (spa->spa_log_state) { 9877294Sperrin case SPA_LOG_MISSING: 9887294Sperrin /* need to recheck in case slog has been restored */ 9897294Sperrin case SPA_LOG_UNKNOWN: 9907294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 9917294Sperrin DS_FIND_CHILDREN)) { 9927294Sperrin spa->spa_log_state = SPA_LOG_MISSING; 9937294Sperrin return (1); 9947294Sperrin } 9957294Sperrin break; 9967294Sperrin 9977294Sperrin case SPA_LOG_CLEAR: 9987294Sperrin (void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL, 9997294Sperrin DS_FIND_CHILDREN); 10007294Sperrin break; 10017294Sperrin } 10027294Sperrin spa->spa_log_state = SPA_LOG_GOOD; 10037294Sperrin return (0); 10047294Sperrin } 10057294Sperrin 10067294Sperrin /* 1007789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 10081544Seschrock * source of configuration information. 1009789Sahrens */ 1010789Sahrens static int 10111544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 1012789Sahrens { 1013789Sahrens int error = 0; 1014789Sahrens nvlist_t *nvroot = NULL; 1015789Sahrens vdev_t *rvd; 1016789Sahrens uberblock_t *ub = &spa->spa_uberblock; 10171635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 1018789Sahrens uint64_t pool_guid; 10192082Seschrock uint64_t version; 10204451Seschrock uint64_t autoreplace = 0; 10217294Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 1022789Sahrens 10237754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 10247754SJeff.Bonwick@Sun.COM 10251544Seschrock spa->spa_load_state = state; 10261635Sbonwick 1027789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 10281733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 10291544Seschrock error = EINVAL; 10301544Seschrock goto out; 10311544Seschrock } 1032789Sahrens 10332082Seschrock /* 10342082Seschrock * Versioning wasn't explicitly added to the label until later, so if 10352082Seschrock * it's not present treat it as the initial version. 10362082Seschrock */ 10372082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 10384577Sahrens version = SPA_VERSION_INITIAL; 10392082Seschrock 10401733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 10411733Sbonwick &spa->spa_config_txg); 10421733Sbonwick 10431635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 10441544Seschrock spa_guid_exists(pool_guid, 0)) { 10451544Seschrock error = EEXIST; 10461544Seschrock goto out; 10471544Seschrock } 1048789Sahrens 10492174Seschrock spa->spa_load_guid = pool_guid; 10502174Seschrock 1051789Sahrens /* 10522082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 10532082Seschrock * value that will be returned by spa_version() since parsing the 10542082Seschrock * configuration requires knowing the version number. 1055789Sahrens */ 10567754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 10572082Seschrock spa->spa_ubsync.ub_version = version; 10582082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 10597754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1060789Sahrens 10612082Seschrock if (error != 0) 10621544Seschrock goto out; 1063789Sahrens 10641585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1065789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1066789Sahrens 1067789Sahrens /* 1068789Sahrens * Try to open all vdevs, loading each label in the process. 1069789Sahrens */ 10707754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 10714070Smc142369 error = vdev_open(rvd); 10727754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 10734070Smc142369 if (error != 0) 10741544Seschrock goto out; 1075789Sahrens 1076789Sahrens /* 10771986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 10787754SJeff.Bonwick@Sun.COM * lock because all label I/O is done with ZIO_FLAG_CONFIG_WRITER. 10791986Seschrock */ 10807754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 10811986Seschrock error = vdev_validate(rvd); 10827754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 10831986Seschrock 10844070Smc142369 if (error != 0) 10851986Seschrock goto out; 10861986Seschrock 10871986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 10881986Seschrock error = ENXIO; 10891986Seschrock goto out; 10901986Seschrock } 10911986Seschrock 10921986Seschrock /* 1093789Sahrens * Find the best uberblock. 1094789Sahrens */ 10957754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1096789Sahrens 1097789Sahrens /* 1098789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1099789Sahrens */ 1100789Sahrens if (ub->ub_txg == 0) { 11011760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11021760Seschrock VDEV_AUX_CORRUPT_DATA); 11031544Seschrock error = ENXIO; 11041544Seschrock goto out; 11051544Seschrock } 11061544Seschrock 11071544Seschrock /* 11081544Seschrock * If the pool is newer than the code, we can't open it. 11091544Seschrock */ 11104577Sahrens if (ub->ub_version > SPA_VERSION) { 11111760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11121760Seschrock VDEV_AUX_VERSION_NEWER); 11131544Seschrock error = ENOTSUP; 11141544Seschrock goto out; 1115789Sahrens } 1116789Sahrens 1117789Sahrens /* 1118789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1119789Sahrens * incomplete configuration. 1120789Sahrens */ 11211732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 11221544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11231544Seschrock VDEV_AUX_BAD_GUID_SUM); 11241544Seschrock error = ENXIO; 11251544Seschrock goto out; 1126789Sahrens } 1127789Sahrens 1128789Sahrens /* 1129789Sahrens * Initialize internal SPA structures. 1130789Sahrens */ 1131789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1132789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1133789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 11341544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 11351544Seschrock if (error) { 11361544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11371544Seschrock VDEV_AUX_CORRUPT_DATA); 11381544Seschrock goto out; 11391544Seschrock } 1140789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1141789Sahrens 11421544Seschrock if (zap_lookup(spa->spa_meta_objset, 1143789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 11441544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 11451544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11461544Seschrock VDEV_AUX_CORRUPT_DATA); 11471544Seschrock error = EIO; 11481544Seschrock goto out; 11491544Seschrock } 1150789Sahrens 1151789Sahrens if (!mosconfig) { 11522082Seschrock nvlist_t *newconfig; 11533975Sek110237 uint64_t hostid; 11542082Seschrock 11552082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 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 11627706SLin.Ling@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig, 11637706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 11643975Sek110237 char *hostname; 11653975Sek110237 unsigned long myhostid = 0; 11663975Sek110237 11673975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 11683975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 11693975Sek110237 11703975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 11714178Slling if (hostid != 0 && myhostid != 0 && 11724178Slling (unsigned long)hostid != myhostid) { 11733975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 11743975Sek110237 "loaded as it was last accessed by " 11757706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 11763975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 11777754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 11783975Sek110237 (unsigned long)hostid); 11793975Sek110237 error = EBADF; 11803975Sek110237 goto out; 11813975Sek110237 } 11823975Sek110237 } 11833975Sek110237 1184789Sahrens spa_config_set(spa, newconfig); 1185789Sahrens spa_unload(spa); 1186789Sahrens spa_deactivate(spa); 1187789Sahrens spa_activate(spa); 1188789Sahrens 11891544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 11901544Seschrock } 11911544Seschrock 11921544Seschrock if (zap_lookup(spa->spa_meta_objset, 11931544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 11941544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 11951544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11961544Seschrock VDEV_AUX_CORRUPT_DATA); 11971544Seschrock error = EIO; 11981544Seschrock goto out; 1199789Sahrens } 1200789Sahrens 12011544Seschrock /* 12022082Seschrock * Load the bit that tells us to use the new accounting function 12032082Seschrock * (raid-z deflation). If we have an older pool, this will not 12042082Seschrock * be present. 12052082Seschrock */ 12062082Seschrock error = zap_lookup(spa->spa_meta_objset, 12072082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 12082082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 12092082Seschrock if (error != 0 && error != ENOENT) { 12102082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12112082Seschrock VDEV_AUX_CORRUPT_DATA); 12122082Seschrock error = EIO; 12132082Seschrock goto out; 12142082Seschrock } 12152082Seschrock 12162082Seschrock /* 12171544Seschrock * Load the persistent error log. If we have an older pool, this will 12181544Seschrock * not be present. 12191544Seschrock */ 12201544Seschrock error = zap_lookup(spa->spa_meta_objset, 12211544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 12221544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 12231807Sbonwick if (error != 0 && error != ENOENT) { 12241544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12251544Seschrock VDEV_AUX_CORRUPT_DATA); 12261544Seschrock error = EIO; 12271544Seschrock goto out; 12281544Seschrock } 12291544Seschrock 12301544Seschrock error = zap_lookup(spa->spa_meta_objset, 12311544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 12321544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 12331544Seschrock if (error != 0 && error != ENOENT) { 12341544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12351544Seschrock VDEV_AUX_CORRUPT_DATA); 12361544Seschrock error = EIO; 12371544Seschrock goto out; 12381544Seschrock } 1239789Sahrens 1240789Sahrens /* 12412926Sek110237 * Load the history object. If we have an older pool, this 12422926Sek110237 * will not be present. 12432926Sek110237 */ 12442926Sek110237 error = zap_lookup(spa->spa_meta_objset, 12452926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 12462926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 12472926Sek110237 if (error != 0 && error != ENOENT) { 12482926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12492926Sek110237 VDEV_AUX_CORRUPT_DATA); 12502926Sek110237 error = EIO; 12512926Sek110237 goto out; 12522926Sek110237 } 12532926Sek110237 12542926Sek110237 /* 12552082Seschrock * Load any hot spares for this pool. 12562082Seschrock */ 12572082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12585450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 12592082Seschrock if (error != 0 && error != ENOENT) { 12602082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12612082Seschrock VDEV_AUX_CORRUPT_DATA); 12622082Seschrock error = EIO; 12632082Seschrock goto out; 12642082Seschrock } 12652082Seschrock if (error == 0) { 12664577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 12675450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 12685450Sbrendan &spa->spa_spares.sav_config) != 0) { 12692082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12702082Seschrock VDEV_AUX_CORRUPT_DATA); 12712082Seschrock error = EIO; 12722082Seschrock goto out; 12732082Seschrock } 12742082Seschrock 12757754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12762082Seschrock spa_load_spares(spa); 12777754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 12782082Seschrock } 12792082Seschrock 12805450Sbrendan /* 12815450Sbrendan * Load any level 2 ARC devices for this pool. 12825450Sbrendan */ 12835450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12845450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 12855450Sbrendan &spa->spa_l2cache.sav_object); 12865450Sbrendan if (error != 0 && error != ENOENT) { 12875450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12885450Sbrendan VDEV_AUX_CORRUPT_DATA); 12895450Sbrendan error = EIO; 12905450Sbrendan goto out; 12915450Sbrendan } 12925450Sbrendan if (error == 0) { 12935450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 12945450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 12955450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 12965450Sbrendan vdev_set_state(rvd, B_TRUE, 12975450Sbrendan VDEV_STATE_CANT_OPEN, 12985450Sbrendan VDEV_AUX_CORRUPT_DATA); 12995450Sbrendan error = EIO; 13005450Sbrendan goto out; 13015450Sbrendan } 13025450Sbrendan 13037754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13045450Sbrendan spa_load_l2cache(spa); 13057754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13065450Sbrendan } 13075450Sbrendan 13087294Sperrin if (spa_check_logs(spa)) { 13097294Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13107294Sperrin VDEV_AUX_BAD_LOG); 13117294Sperrin error = ENXIO; 13127294Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 13137294Sperrin goto out; 13147294Sperrin } 13157294Sperrin 13167294Sperrin 13175094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 13184543Smarks 13193912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13203912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 13213912Slling 13223912Slling if (error && error != ENOENT) { 13233912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13243912Slling VDEV_AUX_CORRUPT_DATA); 13253912Slling error = EIO; 13263912Slling goto out; 13273912Slling } 13283912Slling 13293912Slling if (error == 0) { 13303912Slling (void) zap_lookup(spa->spa_meta_objset, 13313912Slling spa->spa_pool_props_object, 13324451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 13333912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 13344451Seschrock (void) zap_lookup(spa->spa_meta_objset, 13354451Seschrock spa->spa_pool_props_object, 13364451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 13374451Seschrock sizeof (uint64_t), 1, &autoreplace); 13384543Smarks (void) zap_lookup(spa->spa_meta_objset, 13394543Smarks spa->spa_pool_props_object, 13404543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 13414543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 13425329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 13435329Sgw25295 spa->spa_pool_props_object, 13445329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 13455329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 13463912Slling } 13473912Slling 13482082Seschrock /* 13494451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 13504451Seschrock * the ZFS DE that it should not issue any faults for unopenable 13514451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 13524451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 13534451Seschrock * over. 13544451Seschrock */ 13555756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 13564451Seschrock spa_check_removed(spa->spa_root_vdev); 13574451Seschrock 13584451Seschrock /* 13591986Seschrock * Load the vdev state for all toplevel vdevs. 1360789Sahrens */ 13611986Seschrock vdev_load(rvd); 1362789Sahrens 1363789Sahrens /* 1364789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1365789Sahrens */ 13667754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1367789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 13687754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1369789Sahrens 1370789Sahrens /* 1371789Sahrens * Check the state of the root vdev. If it can't be opened, it 1372789Sahrens * indicates one or more toplevel vdevs are faulted. 1373789Sahrens */ 13741544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13751544Seschrock error = ENXIO; 13761544Seschrock goto out; 13771544Seschrock } 1378789Sahrens 13791544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 13801635Sbonwick dmu_tx_t *tx; 13811635Sbonwick int need_update = B_FALSE; 13821585Sbonwick int c; 13831601Sbonwick 13841635Sbonwick /* 13851635Sbonwick * Claim log blocks that haven't been committed yet. 13861635Sbonwick * This must all happen in a single txg. 13871635Sbonwick */ 13881601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1389789Sahrens spa_first_txg(spa)); 13907754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 13912417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1392789Sahrens dmu_tx_commit(tx); 1393789Sahrens 1394789Sahrens spa->spa_sync_on = B_TRUE; 1395789Sahrens txg_sync_start(spa->spa_dsl_pool); 1396789Sahrens 1397789Sahrens /* 1398789Sahrens * Wait for all claims to sync. 1399789Sahrens */ 1400789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 14011585Sbonwick 14021585Sbonwick /* 14031635Sbonwick * If the config cache is stale, or we have uninitialized 14041635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 14051585Sbonwick */ 14061635Sbonwick if (config_cache_txg != spa->spa_config_txg || 14071635Sbonwick state == SPA_LOAD_IMPORT) 14081635Sbonwick need_update = B_TRUE; 14091635Sbonwick 14101635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 14111635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 14121635Sbonwick need_update = B_TRUE; 14131585Sbonwick 14141585Sbonwick /* 14151635Sbonwick * Update the config cache asychronously in case we're the 14161635Sbonwick * root pool, in which case the config cache isn't writable yet. 14171585Sbonwick */ 14181635Sbonwick if (need_update) 14191635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1420789Sahrens } 1421789Sahrens 14221544Seschrock error = 0; 14231544Seschrock out: 14247046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 14252082Seschrock if (error && error != EBADF) 14267294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 14271544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 14281544Seschrock spa->spa_ena = 0; 14291544Seschrock 14301544Seschrock return (error); 1431789Sahrens } 1432789Sahrens 1433789Sahrens /* 1434789Sahrens * Pool Open/Import 1435789Sahrens * 1436789Sahrens * The import case is identical to an open except that the configuration is sent 1437789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1438789Sahrens * case of an open, the pool configuration will exist in the 14394451Seschrock * POOL_STATE_UNINITIALIZED state. 1440789Sahrens * 1441789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1442789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1443789Sahrens * ambiguous state. 1444789Sahrens */ 1445789Sahrens static int 1446789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1447789Sahrens { 1448789Sahrens spa_t *spa; 1449789Sahrens int error; 1450789Sahrens int locked = B_FALSE; 1451789Sahrens 1452789Sahrens *spapp = NULL; 1453789Sahrens 1454789Sahrens /* 1455789Sahrens * As disgusting as this is, we need to support recursive calls to this 1456789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1457789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1458789Sahrens * avoid dsl_dir_open() calling this in the first place. 1459789Sahrens */ 1460789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1461789Sahrens mutex_enter(&spa_namespace_lock); 1462789Sahrens locked = B_TRUE; 1463789Sahrens } 1464789Sahrens 1465789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1466789Sahrens if (locked) 1467789Sahrens mutex_exit(&spa_namespace_lock); 1468789Sahrens return (ENOENT); 1469789Sahrens } 1470789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1471789Sahrens 1472789Sahrens spa_activate(spa); 1473789Sahrens 14741635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1475789Sahrens 1476789Sahrens if (error == EBADF) { 1477789Sahrens /* 14781986Seschrock * If vdev_validate() returns failure (indicated by 14791986Seschrock * EBADF), it indicates that one of the vdevs indicates 14801986Seschrock * that the pool has been exported or destroyed. If 14811986Seschrock * this is the case, the config cache is out of sync and 14821986Seschrock * we should remove the pool from the namespace. 1483789Sahrens */ 1484789Sahrens spa_unload(spa); 1485789Sahrens spa_deactivate(spa); 14866643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1487789Sahrens spa_remove(spa); 1488789Sahrens if (locked) 1489789Sahrens mutex_exit(&spa_namespace_lock); 1490789Sahrens return (ENOENT); 14911544Seschrock } 14921544Seschrock 14931544Seschrock if (error) { 1494789Sahrens /* 1495789Sahrens * We can't open the pool, but we still have useful 1496789Sahrens * information: the state of each vdev after the 1497789Sahrens * attempted vdev_open(). Return this to the user. 1498789Sahrens */ 14997754SJeff.Bonwick@Sun.COM if (config != NULL && spa->spa_root_vdev != NULL) 1500789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1501789Sahrens B_TRUE); 1502789Sahrens spa_unload(spa); 1503789Sahrens spa_deactivate(spa); 15041544Seschrock spa->spa_last_open_failed = B_TRUE; 1505789Sahrens if (locked) 1506789Sahrens mutex_exit(&spa_namespace_lock); 1507789Sahrens *spapp = NULL; 1508789Sahrens return (error); 15091544Seschrock } else { 15101544Seschrock spa->spa_last_open_failed = B_FALSE; 1511789Sahrens } 1512789Sahrens } 1513789Sahrens 1514789Sahrens spa_open_ref(spa, tag); 15154451Seschrock 1516789Sahrens if (locked) 1517789Sahrens mutex_exit(&spa_namespace_lock); 1518789Sahrens 1519789Sahrens *spapp = spa; 1520789Sahrens 15217754SJeff.Bonwick@Sun.COM if (config != NULL) 1522789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1523789Sahrens 1524789Sahrens return (0); 1525789Sahrens } 1526789Sahrens 1527789Sahrens int 1528789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1529789Sahrens { 1530789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1531789Sahrens } 1532789Sahrens 15331544Seschrock /* 15341544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 15351544Seschrock * preventing it from being exported or destroyed. 15361544Seschrock */ 15371544Seschrock spa_t * 15381544Seschrock spa_inject_addref(char *name) 15391544Seschrock { 15401544Seschrock spa_t *spa; 15411544Seschrock 15421544Seschrock mutex_enter(&spa_namespace_lock); 15431544Seschrock if ((spa = spa_lookup(name)) == NULL) { 15441544Seschrock mutex_exit(&spa_namespace_lock); 15451544Seschrock return (NULL); 15461544Seschrock } 15471544Seschrock spa->spa_inject_ref++; 15481544Seschrock mutex_exit(&spa_namespace_lock); 15491544Seschrock 15501544Seschrock return (spa); 15511544Seschrock } 15521544Seschrock 15531544Seschrock void 15541544Seschrock spa_inject_delref(spa_t *spa) 15551544Seschrock { 15561544Seschrock mutex_enter(&spa_namespace_lock); 15571544Seschrock spa->spa_inject_ref--; 15581544Seschrock mutex_exit(&spa_namespace_lock); 15591544Seschrock } 15601544Seschrock 15615450Sbrendan /* 15625450Sbrendan * Add spares device information to the nvlist. 15635450Sbrendan */ 15642082Seschrock static void 15652082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 15662082Seschrock { 15672082Seschrock nvlist_t **spares; 15682082Seschrock uint_t i, nspares; 15692082Seschrock nvlist_t *nvroot; 15702082Seschrock uint64_t guid; 15712082Seschrock vdev_stat_t *vs; 15722082Seschrock uint_t vsc; 15733377Seschrock uint64_t pool; 15742082Seschrock 15755450Sbrendan if (spa->spa_spares.sav_count == 0) 15762082Seschrock return; 15772082Seschrock 15782082Seschrock VERIFY(nvlist_lookup_nvlist(config, 15792082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15805450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 15812082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15822082Seschrock if (nspares != 0) { 15832082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 15842082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 15852082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 15862082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15872082Seschrock 15882082Seschrock /* 15892082Seschrock * Go through and find any spares which have since been 15902082Seschrock * repurposed as an active spare. If this is the case, update 15912082Seschrock * their status appropriately. 15922082Seschrock */ 15932082Seschrock for (i = 0; i < nspares; i++) { 15942082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 15952082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 15967214Slling if (spa_spare_exists(guid, &pool, NULL) && 15977214Slling pool != 0ULL) { 15982082Seschrock VERIFY(nvlist_lookup_uint64_array( 15992082Seschrock spares[i], ZPOOL_CONFIG_STATS, 16002082Seschrock (uint64_t **)&vs, &vsc) == 0); 16012082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 16022082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 16032082Seschrock } 16042082Seschrock } 16052082Seschrock } 16062082Seschrock } 16072082Seschrock 16085450Sbrendan /* 16095450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 16105450Sbrendan */ 16115450Sbrendan static void 16125450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 16135450Sbrendan { 16145450Sbrendan nvlist_t **l2cache; 16155450Sbrendan uint_t i, j, nl2cache; 16165450Sbrendan nvlist_t *nvroot; 16175450Sbrendan uint64_t guid; 16185450Sbrendan vdev_t *vd; 16195450Sbrendan vdev_stat_t *vs; 16205450Sbrendan uint_t vsc; 16215450Sbrendan 16225450Sbrendan if (spa->spa_l2cache.sav_count == 0) 16235450Sbrendan return; 16245450Sbrendan 16257754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 16265450Sbrendan 16275450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 16285450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 16295450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 16305450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 16315450Sbrendan if (nl2cache != 0) { 16325450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 16335450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 16345450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 16355450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 16365450Sbrendan 16375450Sbrendan /* 16385450Sbrendan * Update level 2 cache device stats. 16395450Sbrendan */ 16405450Sbrendan 16415450Sbrendan for (i = 0; i < nl2cache; i++) { 16425450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 16435450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 16445450Sbrendan 16455450Sbrendan vd = NULL; 16465450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 16475450Sbrendan if (guid == 16485450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 16495450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 16505450Sbrendan break; 16515450Sbrendan } 16525450Sbrendan } 16535450Sbrendan ASSERT(vd != NULL); 16545450Sbrendan 16555450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 16565450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 16575450Sbrendan vdev_get_stats(vd, vs); 16585450Sbrendan } 16595450Sbrendan } 16605450Sbrendan 16617754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 16625450Sbrendan } 16635450Sbrendan 1664789Sahrens int 16651544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1666789Sahrens { 1667789Sahrens int error; 1668789Sahrens spa_t *spa; 1669789Sahrens 1670789Sahrens *config = NULL; 1671789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1672789Sahrens 16732082Seschrock if (spa && *config != NULL) { 16741544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 16751544Seschrock spa_get_errlog_size(spa)) == 0); 16761544Seschrock 16777754SJeff.Bonwick@Sun.COM if (spa_suspended(spa)) 16787754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 16797754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0); 16807754SJeff.Bonwick@Sun.COM 16812082Seschrock spa_add_spares(spa, *config); 16825450Sbrendan spa_add_l2cache(spa, *config); 16832082Seschrock } 16842082Seschrock 16851544Seschrock /* 16861544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 16871544Seschrock * and call spa_lookup() directly. 16881544Seschrock */ 16891544Seschrock if (altroot) { 16901544Seschrock if (spa == NULL) { 16911544Seschrock mutex_enter(&spa_namespace_lock); 16921544Seschrock spa = spa_lookup(name); 16931544Seschrock if (spa) 16941544Seschrock spa_altroot(spa, altroot, buflen); 16951544Seschrock else 16961544Seschrock altroot[0] = '\0'; 16971544Seschrock spa = NULL; 16981544Seschrock mutex_exit(&spa_namespace_lock); 16991544Seschrock } else { 17001544Seschrock spa_altroot(spa, altroot, buflen); 17011544Seschrock } 17021544Seschrock } 17031544Seschrock 1704789Sahrens if (spa != NULL) 1705789Sahrens spa_close(spa, FTAG); 1706789Sahrens 1707789Sahrens return (error); 1708789Sahrens } 1709789Sahrens 1710789Sahrens /* 17115450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 17125450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 17135450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 17145450Sbrendan * specified, as long as they are well-formed. 17152082Seschrock */ 17162082Seschrock static int 17175450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 17185450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 17195450Sbrendan vdev_labeltype_t label) 17202082Seschrock { 17215450Sbrendan nvlist_t **dev; 17225450Sbrendan uint_t i, ndev; 17232082Seschrock vdev_t *vd; 17242082Seschrock int error; 17252082Seschrock 17267754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 17277754SJeff.Bonwick@Sun.COM 17282082Seschrock /* 17295450Sbrendan * It's acceptable to have no devs specified. 17302082Seschrock */ 17315450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 17322082Seschrock return (0); 17332082Seschrock 17345450Sbrendan if (ndev == 0) 17352082Seschrock return (EINVAL); 17362082Seschrock 17372082Seschrock /* 17385450Sbrendan * Make sure the pool is formatted with a version that supports this 17395450Sbrendan * device type. 17402082Seschrock */ 17415450Sbrendan if (spa_version(spa) < version) 17422082Seschrock return (ENOTSUP); 17432082Seschrock 17443377Seschrock /* 17455450Sbrendan * Set the pending device list so we correctly handle device in-use 17463377Seschrock * checking. 17473377Seschrock */ 17485450Sbrendan sav->sav_pending = dev; 17495450Sbrendan sav->sav_npending = ndev; 17505450Sbrendan 17515450Sbrendan for (i = 0; i < ndev; i++) { 17525450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 17532082Seschrock mode)) != 0) 17543377Seschrock goto out; 17552082Seschrock 17562082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 17572082Seschrock vdev_free(vd); 17583377Seschrock error = EINVAL; 17593377Seschrock goto out; 17602082Seschrock } 17612082Seschrock 17625450Sbrendan /* 17637754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 17647754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 17655450Sbrendan */ 17667754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 17675450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 17685450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 17695450Sbrendan error = ENOTBLK; 17705450Sbrendan goto out; 17715450Sbrendan } 17727754SJeff.Bonwick@Sun.COM #endif 17732082Seschrock vd->vdev_top = vd; 17743377Seschrock 17753377Seschrock if ((error = vdev_open(vd)) == 0 && 17765450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 17775450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 17783377Seschrock vd->vdev_guid) == 0); 17792082Seschrock } 17802082Seschrock 17812082Seschrock vdev_free(vd); 17823377Seschrock 17835450Sbrendan if (error && 17845450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 17853377Seschrock goto out; 17863377Seschrock else 17873377Seschrock error = 0; 17882082Seschrock } 17892082Seschrock 17903377Seschrock out: 17915450Sbrendan sav->sav_pending = NULL; 17925450Sbrendan sav->sav_npending = 0; 17933377Seschrock return (error); 17942082Seschrock } 17952082Seschrock 17965450Sbrendan static int 17975450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 17985450Sbrendan { 17995450Sbrendan int error; 18005450Sbrendan 18017754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 18027754SJeff.Bonwick@Sun.COM 18035450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 18045450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 18055450Sbrendan VDEV_LABEL_SPARE)) != 0) { 18065450Sbrendan return (error); 18075450Sbrendan } 18085450Sbrendan 18095450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 18105450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 18115450Sbrendan VDEV_LABEL_L2CACHE)); 18125450Sbrendan } 18135450Sbrendan 18145450Sbrendan static void 18155450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 18165450Sbrendan const char *config) 18175450Sbrendan { 18185450Sbrendan int i; 18195450Sbrendan 18205450Sbrendan if (sav->sav_config != NULL) { 18215450Sbrendan nvlist_t **olddevs; 18225450Sbrendan uint_t oldndevs; 18235450Sbrendan nvlist_t **newdevs; 18245450Sbrendan 18255450Sbrendan /* 18265450Sbrendan * Generate new dev list by concatentating with the 18275450Sbrendan * current dev list. 18285450Sbrendan */ 18295450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 18305450Sbrendan &olddevs, &oldndevs) == 0); 18315450Sbrendan 18325450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 18335450Sbrendan (ndevs + oldndevs), KM_SLEEP); 18345450Sbrendan for (i = 0; i < oldndevs; i++) 18355450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 18365450Sbrendan KM_SLEEP) == 0); 18375450Sbrendan for (i = 0; i < ndevs; i++) 18385450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 18395450Sbrendan KM_SLEEP) == 0); 18405450Sbrendan 18415450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 18425450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 18435450Sbrendan 18445450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 18455450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 18465450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 18475450Sbrendan nvlist_free(newdevs[i]); 18485450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 18495450Sbrendan } else { 18505450Sbrendan /* 18515450Sbrendan * Generate a new dev list. 18525450Sbrendan */ 18535450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 18545450Sbrendan KM_SLEEP) == 0); 18555450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 18565450Sbrendan devs, ndevs) == 0); 18575450Sbrendan } 18585450Sbrendan } 18595450Sbrendan 18605450Sbrendan /* 18615450Sbrendan * Stop and drop level 2 ARC devices 18625450Sbrendan */ 18635450Sbrendan void 18645450Sbrendan spa_l2cache_drop(spa_t *spa) 18655450Sbrendan { 18665450Sbrendan vdev_t *vd; 18675450Sbrendan int i; 18685450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 18695450Sbrendan 18705450Sbrendan for (i = 0; i < sav->sav_count; i++) { 18715450Sbrendan uint64_t pool; 18725450Sbrendan 18735450Sbrendan vd = sav->sav_vdevs[i]; 18745450Sbrendan ASSERT(vd != NULL); 18755450Sbrendan 18767754SJeff.Bonwick@Sun.COM if ((spa_mode & FWRITE) && 18776643Seschrock spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL && 18786643Seschrock l2arc_vdev_present(vd)) { 18795450Sbrendan l2arc_remove_vdev(vd); 18805450Sbrendan } 18815450Sbrendan if (vd->vdev_isl2cache) 18825450Sbrendan spa_l2cache_remove(vd); 18835450Sbrendan vdev_clear_stats(vd); 18845450Sbrendan (void) vdev_close(vd); 18855450Sbrendan } 18865450Sbrendan } 18875450Sbrendan 18882082Seschrock /* 1889789Sahrens * Pool Creation 1890789Sahrens */ 1891789Sahrens int 18925094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 18937184Stimh const char *history_str, nvlist_t *zplprops) 1894789Sahrens { 1895789Sahrens spa_t *spa; 18965094Slling char *altroot = NULL; 18971635Sbonwick vdev_t *rvd; 1898789Sahrens dsl_pool_t *dp; 1899789Sahrens dmu_tx_t *tx; 19002082Seschrock int c, error = 0; 1901789Sahrens uint64_t txg = TXG_INITIAL; 19025450Sbrendan nvlist_t **spares, **l2cache; 19035450Sbrendan uint_t nspares, nl2cache; 19045094Slling uint64_t version; 1905789Sahrens 1906789Sahrens /* 1907789Sahrens * If this pool already exists, return failure. 1908789Sahrens */ 1909789Sahrens mutex_enter(&spa_namespace_lock); 1910789Sahrens if (spa_lookup(pool) != NULL) { 1911789Sahrens mutex_exit(&spa_namespace_lock); 1912789Sahrens return (EEXIST); 1913789Sahrens } 1914789Sahrens 1915789Sahrens /* 1916789Sahrens * Allocate a new spa_t structure. 1917789Sahrens */ 19185094Slling (void) nvlist_lookup_string(props, 19195094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 19201635Sbonwick spa = spa_add(pool, altroot); 1921789Sahrens spa_activate(spa); 1922789Sahrens 1923789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 19245094Slling 19255094Slling if (props && (error = spa_prop_validate(spa, props))) { 19265094Slling spa_unload(spa); 19275094Slling spa_deactivate(spa); 19285094Slling spa_remove(spa); 19296643Seschrock mutex_exit(&spa_namespace_lock); 19305094Slling return (error); 19315094Slling } 19325094Slling 19335094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 19345094Slling &version) != 0) 19355094Slling version = SPA_VERSION; 19365094Slling ASSERT(version <= SPA_VERSION); 19375094Slling spa->spa_uberblock.ub_version = version; 1938789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1939789Sahrens 19401635Sbonwick /* 19411635Sbonwick * Create the root vdev. 19421635Sbonwick */ 19437754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 19441635Sbonwick 19452082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 19462082Seschrock 19472082Seschrock ASSERT(error != 0 || rvd != NULL); 19482082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 19492082Seschrock 19505913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 19511635Sbonwick error = EINVAL; 19522082Seschrock 19532082Seschrock if (error == 0 && 19542082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 19555450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 19562082Seschrock VDEV_ALLOC_ADD)) == 0) { 19572082Seschrock for (c = 0; c < rvd->vdev_children; c++) 19582082Seschrock vdev_init(rvd->vdev_child[c], txg); 19592082Seschrock vdev_config_dirty(rvd); 19601635Sbonwick } 19611635Sbonwick 19627754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1963789Sahrens 19642082Seschrock if (error != 0) { 1965789Sahrens spa_unload(spa); 1966789Sahrens spa_deactivate(spa); 1967789Sahrens spa_remove(spa); 1968789Sahrens mutex_exit(&spa_namespace_lock); 1969789Sahrens return (error); 1970789Sahrens } 1971789Sahrens 19722082Seschrock /* 19732082Seschrock * Get the list of spares, if specified. 19742082Seschrock */ 19752082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 19762082Seschrock &spares, &nspares) == 0) { 19775450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 19782082Seschrock KM_SLEEP) == 0); 19795450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 19802082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 19817754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 19822082Seschrock spa_load_spares(spa); 19837754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 19845450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 19855450Sbrendan } 19865450Sbrendan 19875450Sbrendan /* 19885450Sbrendan * Get the list of level 2 cache devices, if specified. 19895450Sbrendan */ 19905450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 19915450Sbrendan &l2cache, &nl2cache) == 0) { 19925450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 19935450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 19945450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 19955450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 19967754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 19975450Sbrendan spa_load_l2cache(spa); 19987754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 19995450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 20002082Seschrock } 20012082Seschrock 20027184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2003789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2004789Sahrens 2005789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2006789Sahrens 2007789Sahrens /* 2008789Sahrens * Create the pool config object. 2009789Sahrens */ 2010789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 20117497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2012789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2013789Sahrens 20141544Seschrock if (zap_add(spa->spa_meta_objset, 2015789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 20161544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 20171544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 20181544Seschrock } 2019789Sahrens 20205094Slling /* Newly created pools with the right version are always deflated. */ 20215094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 20225094Slling spa->spa_deflate = TRUE; 20235094Slling if (zap_add(spa->spa_meta_objset, 20245094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 20255094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 20265094Slling cmn_err(CE_PANIC, "failed to add deflate"); 20275094Slling } 20282082Seschrock } 20292082Seschrock 2030789Sahrens /* 2031789Sahrens * Create the deferred-free bplist object. Turn off compression 2032789Sahrens * because sync-to-convergence takes longer if the blocksize 2033789Sahrens * keeps changing. 2034789Sahrens */ 2035789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2036789Sahrens 1 << 14, tx); 2037789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2038789Sahrens ZIO_COMPRESS_OFF, tx); 2039789Sahrens 20401544Seschrock if (zap_add(spa->spa_meta_objset, 2041789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 20421544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 20431544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 20441544Seschrock } 2045789Sahrens 20462926Sek110237 /* 20472926Sek110237 * Create the pool's history object. 20482926Sek110237 */ 20495094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 20505094Slling spa_history_create_obj(spa, tx); 20515094Slling 20525094Slling /* 20535094Slling * Set pool properties. 20545094Slling */ 20555094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 20565094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20575329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 20585094Slling if (props) 20595094Slling spa_sync_props(spa, props, CRED(), tx); 20602926Sek110237 2061789Sahrens dmu_tx_commit(tx); 2062789Sahrens 2063789Sahrens spa->spa_sync_on = B_TRUE; 2064789Sahrens txg_sync_start(spa->spa_dsl_pool); 2065789Sahrens 2066789Sahrens /* 2067789Sahrens * We explicitly wait for the first transaction to complete so that our 2068789Sahrens * bean counters are appropriately updated. 2069789Sahrens */ 2070789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2071789Sahrens 20726643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2073789Sahrens 20745094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 20754715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 20764715Sek110237 2077789Sahrens mutex_exit(&spa_namespace_lock); 2078789Sahrens 20797046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 20807046Sahrens 2081789Sahrens return (0); 2082789Sahrens } 2083789Sahrens 2084789Sahrens /* 2085789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2086789Sahrens * then call spa_load() to do the dirty work. 2087789Sahrens */ 20886423Sgw25295 static int 20896423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 20906643Seschrock boolean_t isroot, boolean_t allowfaulted) 2091789Sahrens { 2092789Sahrens spa_t *spa; 20935094Slling char *altroot = NULL; 20946643Seschrock int error, loaderr; 20952082Seschrock nvlist_t *nvroot; 20965450Sbrendan nvlist_t **spares, **l2cache; 20975450Sbrendan uint_t nspares, nl2cache; 2098789Sahrens 2099789Sahrens /* 2100789Sahrens * If a pool with this name exists, return failure. 2101789Sahrens */ 2102789Sahrens mutex_enter(&spa_namespace_lock); 2103789Sahrens if (spa_lookup(pool) != NULL) { 2104789Sahrens mutex_exit(&spa_namespace_lock); 2105789Sahrens return (EEXIST); 2106789Sahrens } 2107789Sahrens 2108789Sahrens /* 21091635Sbonwick * Create and initialize the spa structure. 2110789Sahrens */ 21115094Slling (void) nvlist_lookup_string(props, 21125094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 21131635Sbonwick spa = spa_add(pool, altroot); 2114789Sahrens spa_activate(spa); 2115789Sahrens 21166643Seschrock if (allowfaulted) 21176643Seschrock spa->spa_import_faulted = B_TRUE; 21186673Seschrock spa->spa_is_root = isroot; 21196643Seschrock 2120789Sahrens /* 21211635Sbonwick * Pass off the heavy lifting to spa_load(). 21227046Sahrens * Pass TRUE for mosconfig (unless this is a root pool) because 21237046Sahrens * the user-supplied config is actually the one to trust when 21247046Sahrens * doing an import. 21251601Sbonwick */ 21267046Sahrens loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot); 2127789Sahrens 21287754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21292082Seschrock /* 21302082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 21312082Seschrock * and conflicts with spa_has_spare(). 21322082Seschrock */ 21336423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 21345450Sbrendan nvlist_free(spa->spa_spares.sav_config); 21355450Sbrendan spa->spa_spares.sav_config = NULL; 21362082Seschrock spa_load_spares(spa); 21372082Seschrock } 21386423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 21395450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 21405450Sbrendan spa->spa_l2cache.sav_config = NULL; 21415450Sbrendan spa_load_l2cache(spa); 21425450Sbrendan } 21432082Seschrock 21442082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 21452082Seschrock &nvroot) == 0); 21465450Sbrendan if (error == 0) 21475450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 21485450Sbrendan if (error == 0) 21495450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 21505450Sbrendan VDEV_ALLOC_L2CACHE); 21517754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 21522082Seschrock 21535094Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 21546643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 21556643Seschrock /* 21566643Seschrock * If we failed to load the pool, but 'allowfaulted' is 21576643Seschrock * set, then manually set the config as if the config 21586643Seschrock * passed in was specified in the cache file. 21596643Seschrock */ 21606643Seschrock error = 0; 21616643Seschrock spa->spa_import_faulted = B_FALSE; 21627754SJeff.Bonwick@Sun.COM if (spa->spa_config == NULL) 21636643Seschrock spa->spa_config = spa_config_generate(spa, 21646643Seschrock NULL, -1ULL, B_TRUE); 21656643Seschrock spa_unload(spa); 21666643Seschrock spa_deactivate(spa); 21676643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 21686643Seschrock } else { 21696643Seschrock spa_unload(spa); 21706643Seschrock spa_deactivate(spa); 21716643Seschrock spa_remove(spa); 21726643Seschrock } 2173789Sahrens mutex_exit(&spa_namespace_lock); 2174789Sahrens return (error); 2175789Sahrens } 2176789Sahrens 21771635Sbonwick /* 21785450Sbrendan * Override any spares and level 2 cache devices as specified by 21795450Sbrendan * the user, as these may have correct device names/devids, etc. 21802082Seschrock */ 21812082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 21822082Seschrock &spares, &nspares) == 0) { 21835450Sbrendan if (spa->spa_spares.sav_config) 21845450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 21852082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 21862082Seschrock else 21875450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 21882082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 21895450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 21902082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 21917754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21922082Seschrock spa_load_spares(spa); 21937754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 21945450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 21955450Sbrendan } 21965450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 21975450Sbrendan &l2cache, &nl2cache) == 0) { 21985450Sbrendan if (spa->spa_l2cache.sav_config) 21995450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 22005450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 22015450Sbrendan else 22025450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 22035450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 22045450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 22055450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 22067754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22075450Sbrendan spa_load_l2cache(spa); 22087754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 22095450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 22102082Seschrock } 22112082Seschrock 22126643Seschrock if (spa_mode & FWRITE) { 22136643Seschrock /* 22146643Seschrock * Update the config cache to include the newly-imported pool. 22156643Seschrock */ 22166423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 22176643Seschrock } 22186643Seschrock 22196643Seschrock spa->spa_import_faulted = B_FALSE; 22204451Seschrock mutex_exit(&spa_namespace_lock); 22214451Seschrock 2222789Sahrens return (0); 2223789Sahrens } 2224789Sahrens 22256423Sgw25295 #ifdef _KERNEL 22266423Sgw25295 /* 22276423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 22286423Sgw25295 * device label. 22296423Sgw25295 */ 22306423Sgw25295 static void 22316423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 22326423Sgw25295 { 22336423Sgw25295 nvlist_t *nvtop, *nvroot; 22346423Sgw25295 uint64_t pgid; 22356423Sgw25295 22366423Sgw25295 /* 22376423Sgw25295 * Add this top-level vdev to the child array. 22386423Sgw25295 */ 22396423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 22406423Sgw25295 == 0); 22416423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 22426423Sgw25295 == 0); 22436423Sgw25295 22446423Sgw25295 /* 22456423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 22466423Sgw25295 */ 22476423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 22486423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 22496423Sgw25295 == 0); 22506423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 22516423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 22526423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 22536423Sgw25295 &nvtop, 1) == 0); 22546423Sgw25295 22556423Sgw25295 /* 22566423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 22576423Sgw25295 * this pool's configuration (remove the old, add the new). 22586423Sgw25295 */ 22596423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 22606423Sgw25295 nvlist_free(nvroot); 22616423Sgw25295 } 22626423Sgw25295 22636423Sgw25295 /* 22646423Sgw25295 * Get the root pool information from the root disk, then import the root pool 22656423Sgw25295 * during the system boot up time. 22666423Sgw25295 */ 22677539SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 22687147Staylor 22697147Staylor int 22707147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf, 22716423Sgw25295 uint64_t *besttxg) 22726423Sgw25295 { 22736423Sgw25295 nvlist_t *config; 22746423Sgw25295 uint64_t txg; 22757539SLin.Ling@Sun.COM int error; 22767539SLin.Ling@Sun.COM 22777539SLin.Ling@Sun.COM if (error = vdev_disk_read_rootlabel(devpath, devid, &config)) 22787539SLin.Ling@Sun.COM return (error); 22796423Sgw25295 22806423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 22816423Sgw25295 22827147Staylor if (bestconf != NULL) 22836423Sgw25295 *bestconf = config; 22847539SLin.Ling@Sun.COM else 22857539SLin.Ling@Sun.COM nvlist_free(config); 22867147Staylor *besttxg = txg; 22877147Staylor return (0); 22886423Sgw25295 } 22896423Sgw25295 22906423Sgw25295 boolean_t 22916423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 22926423Sgw25295 { 22936423Sgw25295 uint64_t ival; 22946423Sgw25295 22956423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 22966423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 22976423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 22986423Sgw25295 return (B_FALSE); 22996423Sgw25295 23006423Sgw25295 return (B_TRUE); 23016423Sgw25295 } 23026423Sgw25295 23037147Staylor 23047147Staylor /* 23057147Staylor * Given the boot device's physical path or devid, check if the device 23067147Staylor * is in a valid state. If so, return the configuration from the vdev 23077147Staylor * label. 23087147Staylor */ 23097147Staylor int 23107147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf) 23117147Staylor { 23127147Staylor nvlist_t *conf = NULL; 23137147Staylor uint64_t txg = 0; 23147147Staylor nvlist_t *nvtop, **child; 23157147Staylor char *type; 23167147Staylor char *bootpath = NULL; 23177147Staylor uint_t children, c; 23187147Staylor char *tmp; 23197539SLin.Ling@Sun.COM int error; 23207147Staylor 23217147Staylor if (devpath && ((tmp = strchr(devpath, ' ')) != NULL)) 23227147Staylor *tmp = '\0'; 23237539SLin.Ling@Sun.COM if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) { 23247147Staylor cmn_err(CE_NOTE, "error reading device label"); 23257539SLin.Ling@Sun.COM return (error); 23267147Staylor } 23277147Staylor if (txg == 0) { 23287147Staylor cmn_err(CE_NOTE, "this device is detached"); 23297147Staylor nvlist_free(conf); 23307147Staylor return (EINVAL); 23317147Staylor } 23327147Staylor 23337147Staylor VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE, 23347147Staylor &nvtop) == 0); 23357147Staylor VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0); 23367147Staylor 23377147Staylor if (strcmp(type, VDEV_TYPE_DISK) == 0) { 23387147Staylor if (spa_rootdev_validate(nvtop)) { 23397147Staylor goto out; 23407147Staylor } else { 23417147Staylor nvlist_free(conf); 23427147Staylor return (EINVAL); 23437147Staylor } 23447147Staylor } 23457147Staylor 23467147Staylor ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0); 23477147Staylor 23487147Staylor VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN, 23497147Staylor &child, &children) == 0); 23507147Staylor 23517147Staylor /* 23527147Staylor * Go thru vdevs in the mirror to see if the given device 23537147Staylor * has the most recent txg. Only the device with the most 23547147Staylor * recent txg has valid information and should be booted. 23557147Staylor */ 23567147Staylor for (c = 0; c < children; c++) { 23577147Staylor char *cdevid, *cpath; 23587147Staylor uint64_t tmptxg; 23597147Staylor 23607147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH, 23617147Staylor &cpath) != 0) 23627147Staylor return (EINVAL); 23637147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_DEVID, 23647147Staylor &cdevid) != 0) 23657147Staylor return (EINVAL); 23667687SLin.Ling@Sun.COM if ((spa_check_rootconf(cpath, cdevid, NULL, 23677687SLin.Ling@Sun.COM &tmptxg) == 0) && (tmptxg > txg)) { 23687147Staylor txg = tmptxg; 23697147Staylor VERIFY(nvlist_lookup_string(child[c], 23707147Staylor ZPOOL_CONFIG_PATH, &bootpath) == 0); 23717147Staylor } 23727147Staylor } 23737147Staylor 23747147Staylor /* Does the best device match the one we've booted from? */ 23757147Staylor if (bootpath) { 23767147Staylor cmn_err(CE_NOTE, "try booting from '%s'", bootpath); 23777147Staylor return (EINVAL); 23787147Staylor } 23797147Staylor out: 23807147Staylor *bestconf = conf; 23817147Staylor return (0); 23827147Staylor } 23837147Staylor 23846423Sgw25295 /* 23856423Sgw25295 * Import a root pool. 23866423Sgw25295 * 23877147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 23887147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 23897147Staylor * The GRUB "findroot" command will return the vdev we should boot. 23906423Sgw25295 * 23916423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 23926423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 23936423Sgw25295 * e.g. 23946423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 23956423Sgw25295 */ 23966423Sgw25295 int 23977147Staylor spa_import_rootpool(char *devpath, char *devid) 23986423Sgw25295 { 23996423Sgw25295 nvlist_t *conf = NULL; 24006423Sgw25295 char *pname; 24016423Sgw25295 int error; 24026423Sgw25295 24036423Sgw25295 /* 24046423Sgw25295 * Get the vdev pathname and configuation from the most 24056423Sgw25295 * recently updated vdev (highest txg). 24066423Sgw25295 */ 24077147Staylor if (error = spa_get_rootconf(devpath, devid, &conf)) 24086423Sgw25295 goto msg_out; 24096423Sgw25295 24106423Sgw25295 /* 24116423Sgw25295 * Add type "root" vdev to the config. 24126423Sgw25295 */ 24136423Sgw25295 spa_build_rootpool_config(conf); 24146423Sgw25295 24156423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 24166423Sgw25295 24176673Seschrock /* 24186673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 24196673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 24206673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 24216673Seschrock * pool open, not import. 24226673Seschrock */ 24236673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 24246423Sgw25295 if (error == EEXIST) 24256423Sgw25295 error = 0; 24266423Sgw25295 24276423Sgw25295 nvlist_free(conf); 24286423Sgw25295 return (error); 24296423Sgw25295 24306423Sgw25295 msg_out: 24317147Staylor cmn_err(CE_NOTE, "\n" 24326423Sgw25295 " *************************************************** \n" 24336423Sgw25295 " * This device is not bootable! * \n" 24346423Sgw25295 " * It is either offlined or detached or faulted. * \n" 24356423Sgw25295 " * Please try to boot from a different device. * \n" 24367147Staylor " *************************************************** "); 24376423Sgw25295 24386423Sgw25295 return (error); 24396423Sgw25295 } 24406423Sgw25295 #endif 24416423Sgw25295 24426423Sgw25295 /* 24436423Sgw25295 * Import a non-root pool into the system. 24446423Sgw25295 */ 24456423Sgw25295 int 24466423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 24476423Sgw25295 { 24486643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 24496423Sgw25295 } 24506423Sgw25295 24516643Seschrock int 24526643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 24536643Seschrock { 24546643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 24556643Seschrock } 24566643Seschrock 24576643Seschrock 2458789Sahrens /* 2459789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2460789Sahrens * to get the vdev stats associated with the imported devices. 2461789Sahrens */ 2462789Sahrens #define TRYIMPORT_NAME "$import" 2463789Sahrens 2464789Sahrens nvlist_t * 2465789Sahrens spa_tryimport(nvlist_t *tryconfig) 2466789Sahrens { 2467789Sahrens nvlist_t *config = NULL; 2468789Sahrens char *poolname; 2469789Sahrens spa_t *spa; 2470789Sahrens uint64_t state; 2471789Sahrens 2472789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2473789Sahrens return (NULL); 2474789Sahrens 2475789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2476789Sahrens return (NULL); 2477789Sahrens 24781635Sbonwick /* 24791635Sbonwick * Create and initialize the spa structure. 24801635Sbonwick */ 2481789Sahrens mutex_enter(&spa_namespace_lock); 24821635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2483789Sahrens spa_activate(spa); 2484789Sahrens 2485789Sahrens /* 24861635Sbonwick * Pass off the heavy lifting to spa_load(). 24871732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 24881732Sbonwick * is actually the one to trust when doing an import. 2489789Sahrens */ 24901732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2491789Sahrens 2492789Sahrens /* 2493789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2494789Sahrens */ 2495789Sahrens if (spa->spa_root_vdev != NULL) { 2496789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2497789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2498789Sahrens poolname) == 0); 2499789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2500789Sahrens state) == 0); 25013975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 25023975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 25032082Seschrock 25042082Seschrock /* 25056423Sgw25295 * If the bootfs property exists on this pool then we 25066423Sgw25295 * copy it out so that external consumers can tell which 25076423Sgw25295 * pools are bootable. 25086423Sgw25295 */ 25096423Sgw25295 if (spa->spa_bootfs) { 25106423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 25116423Sgw25295 25126423Sgw25295 /* 25136423Sgw25295 * We have to play games with the name since the 25146423Sgw25295 * pool was opened as TRYIMPORT_NAME. 25156423Sgw25295 */ 25167754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 25176423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 25186423Sgw25295 char *cp; 25196423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 25206423Sgw25295 25216423Sgw25295 cp = strchr(tmpname, '/'); 25226423Sgw25295 if (cp == NULL) { 25236423Sgw25295 (void) strlcpy(dsname, tmpname, 25246423Sgw25295 MAXPATHLEN); 25256423Sgw25295 } else { 25266423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 25276423Sgw25295 "%s/%s", poolname, ++cp); 25286423Sgw25295 } 25296423Sgw25295 VERIFY(nvlist_add_string(config, 25306423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 25316423Sgw25295 kmem_free(dsname, MAXPATHLEN); 25326423Sgw25295 } 25336423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 25346423Sgw25295 } 25356423Sgw25295 25366423Sgw25295 /* 25375450Sbrendan * Add the list of hot spares and level 2 cache devices. 25382082Seschrock */ 25392082Seschrock spa_add_spares(spa, config); 25405450Sbrendan spa_add_l2cache(spa, config); 2541789Sahrens } 2542789Sahrens 2543789Sahrens spa_unload(spa); 2544789Sahrens spa_deactivate(spa); 2545789Sahrens spa_remove(spa); 2546789Sahrens mutex_exit(&spa_namespace_lock); 2547789Sahrens 2548789Sahrens return (config); 2549789Sahrens } 2550789Sahrens 2551789Sahrens /* 2552789Sahrens * Pool export/destroy 2553789Sahrens * 2554789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2555789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2556789Sahrens * update the pool state and sync all the labels to disk, removing the 2557789Sahrens * configuration from the cache afterwards. 2558789Sahrens */ 2559789Sahrens static int 25607214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 25617214Slling boolean_t force) 2562789Sahrens { 2563789Sahrens spa_t *spa; 2564789Sahrens 25651775Sbillm if (oldconfig) 25661775Sbillm *oldconfig = NULL; 25671775Sbillm 2568789Sahrens if (!(spa_mode & FWRITE)) 2569789Sahrens return (EROFS); 2570789Sahrens 2571789Sahrens mutex_enter(&spa_namespace_lock); 2572789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2573789Sahrens mutex_exit(&spa_namespace_lock); 2574789Sahrens return (ENOENT); 2575789Sahrens } 2576789Sahrens 2577789Sahrens /* 25781544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 25791544Seschrock * reacquire the namespace lock, and see if we can export. 25801544Seschrock */ 25811544Seschrock spa_open_ref(spa, FTAG); 25821544Seschrock mutex_exit(&spa_namespace_lock); 25831544Seschrock spa_async_suspend(spa); 25841544Seschrock mutex_enter(&spa_namespace_lock); 25851544Seschrock spa_close(spa, FTAG); 25861544Seschrock 25871544Seschrock /* 2588789Sahrens * The pool will be in core if it's openable, 2589789Sahrens * in which case we can modify its state. 2590789Sahrens */ 2591789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2592789Sahrens /* 2593789Sahrens * Objsets may be open only because they're dirty, so we 2594789Sahrens * have to force it to sync before checking spa_refcnt. 2595789Sahrens */ 2596789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2597789Sahrens 25981544Seschrock /* 25991544Seschrock * A pool cannot be exported or destroyed if there are active 26001544Seschrock * references. If we are resetting a pool, allow references by 26011544Seschrock * fault injection handlers. 26021544Seschrock */ 26031544Seschrock if (!spa_refcount_zero(spa) || 26041544Seschrock (spa->spa_inject_ref != 0 && 26051544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 26061544Seschrock spa_async_resume(spa); 2607789Sahrens mutex_exit(&spa_namespace_lock); 2608789Sahrens return (EBUSY); 2609789Sahrens } 2610789Sahrens 2611789Sahrens /* 26127214Slling * A pool cannot be exported if it has an active shared spare. 26137214Slling * This is to prevent other pools stealing the active spare 26147214Slling * from an exported pool. At user's own will, such pool can 26157214Slling * be forcedly exported. 26167214Slling */ 26177214Slling if (!force && new_state == POOL_STATE_EXPORTED && 26187214Slling spa_has_active_shared_spare(spa)) { 26197214Slling spa_async_resume(spa); 26207214Slling mutex_exit(&spa_namespace_lock); 26217214Slling return (EXDEV); 26227214Slling } 26237214Slling 26247214Slling /* 2625789Sahrens * We want this to be reflected on every label, 2626789Sahrens * so mark them all dirty. spa_unload() will do the 2627789Sahrens * final sync that pushes these changes out. 2628789Sahrens */ 26291544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 26307754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26311544Seschrock spa->spa_state = new_state; 26321635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 26331544Seschrock vdev_config_dirty(spa->spa_root_vdev); 26347754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 26351544Seschrock } 2636789Sahrens } 2637789Sahrens 26384451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 26394451Seschrock 2640789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2641789Sahrens spa_unload(spa); 2642789Sahrens spa_deactivate(spa); 2643789Sahrens } 2644789Sahrens 26451775Sbillm if (oldconfig && spa->spa_config) 26461775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 26471775Sbillm 26481544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 26496643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 26501544Seschrock spa_remove(spa); 26511544Seschrock } 2652789Sahrens mutex_exit(&spa_namespace_lock); 2653789Sahrens 2654789Sahrens return (0); 2655789Sahrens } 2656789Sahrens 2657789Sahrens /* 2658789Sahrens * Destroy a storage pool. 2659789Sahrens */ 2660789Sahrens int 2661789Sahrens spa_destroy(char *pool) 2662789Sahrens { 26637214Slling return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, B_FALSE)); 2664789Sahrens } 2665789Sahrens 2666789Sahrens /* 2667789Sahrens * Export a storage pool. 2668789Sahrens */ 2669789Sahrens int 26707214Slling spa_export(char *pool, nvlist_t **oldconfig, boolean_t force) 2671789Sahrens { 26727214Slling return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, force)); 2673789Sahrens } 2674789Sahrens 2675789Sahrens /* 26761544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 26771544Seschrock * from the namespace in any way. 26781544Seschrock */ 26791544Seschrock int 26801544Seschrock spa_reset(char *pool) 26811544Seschrock { 26827214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 26837214Slling B_FALSE)); 26841544Seschrock } 26851544Seschrock 26861544Seschrock /* 2687789Sahrens * ========================================================================== 2688789Sahrens * Device manipulation 2689789Sahrens * ========================================================================== 2690789Sahrens */ 2691789Sahrens 2692789Sahrens /* 26934527Sperrin * Add a device to a storage pool. 2694789Sahrens */ 2695789Sahrens int 2696789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2697789Sahrens { 2698789Sahrens uint64_t txg; 26991635Sbonwick int c, error; 2700789Sahrens vdev_t *rvd = spa->spa_root_vdev; 27011585Sbonwick vdev_t *vd, *tvd; 27025450Sbrendan nvlist_t **spares, **l2cache; 27035450Sbrendan uint_t nspares, nl2cache; 2704789Sahrens 2705789Sahrens txg = spa_vdev_enter(spa); 2706789Sahrens 27072082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 27082082Seschrock VDEV_ALLOC_ADD)) != 0) 27092082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 27102082Seschrock 27117754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 2712789Sahrens 27135450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 27145450Sbrendan &nspares) != 0) 27152082Seschrock nspares = 0; 27162082Seschrock 27175450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 27185450Sbrendan &nl2cache) != 0) 27195450Sbrendan nl2cache = 0; 27205450Sbrendan 27217754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 27222082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 27237754SJeff.Bonwick@Sun.COM 27247754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 27257754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 27267754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 27272082Seschrock 27283377Seschrock /* 27295450Sbrendan * We must validate the spares and l2cache devices after checking the 27305450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 27313377Seschrock */ 27327754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 27333377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 27343377Seschrock 27353377Seschrock /* 27363377Seschrock * Transfer each new top-level vdev from vd to rvd. 27373377Seschrock */ 27383377Seschrock for (c = 0; c < vd->vdev_children; c++) { 27393377Seschrock tvd = vd->vdev_child[c]; 27403377Seschrock vdev_remove_child(vd, tvd); 27413377Seschrock tvd->vdev_id = rvd->vdev_children; 27423377Seschrock vdev_add_child(rvd, tvd); 27433377Seschrock vdev_config_dirty(tvd); 27443377Seschrock } 27453377Seschrock 27462082Seschrock if (nspares != 0) { 27475450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 27485450Sbrendan ZPOOL_CONFIG_SPARES); 27492082Seschrock spa_load_spares(spa); 27505450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 27515450Sbrendan } 27525450Sbrendan 27535450Sbrendan if (nl2cache != 0) { 27545450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 27555450Sbrendan ZPOOL_CONFIG_L2CACHE); 27565450Sbrendan spa_load_l2cache(spa); 27575450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2758789Sahrens } 2759789Sahrens 2760789Sahrens /* 27611585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 27621585Sbonwick * If other threads start allocating from these vdevs before we 27631585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 27641585Sbonwick * fail to open the pool because there are DVAs that the config cache 27651585Sbonwick * can't translate. Therefore, we first add the vdevs without 27661585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 27671635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 27681585Sbonwick * 27691585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 27701585Sbonwick * if we lose power at any point in this sequence, the remaining 27711585Sbonwick * steps will be completed the next time we load the pool. 2772789Sahrens */ 27731635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 27741585Sbonwick 27751635Sbonwick mutex_enter(&spa_namespace_lock); 27761635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27771635Sbonwick mutex_exit(&spa_namespace_lock); 2778789Sahrens 27791635Sbonwick return (0); 2780789Sahrens } 2781789Sahrens 2782789Sahrens /* 2783789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2784789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2785789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2786789Sahrens * 2787789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2788789Sahrens * existing device; in this case the two devices are made into their own 27894451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2790789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2791789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2792789Sahrens * completion of resilvering, the first disk (the one being replaced) 2793789Sahrens * is automatically detached. 2794789Sahrens */ 2795789Sahrens int 27961544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2797789Sahrens { 2798789Sahrens uint64_t txg, open_txg; 2799789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2800789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 28012082Seschrock vdev_ops_t *pvops; 28027313SEric.Kustarz@Sun.COM dmu_tx_t *tx; 28037313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 28047313SEric.Kustarz@Sun.COM int newvd_isspare; 28057313SEric.Kustarz@Sun.COM int error; 2806789Sahrens 2807789Sahrens txg = spa_vdev_enter(spa); 2808789Sahrens 28096643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2810789Sahrens 2811789Sahrens if (oldvd == NULL) 2812789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2813789Sahrens 28141585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 28151585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28161585Sbonwick 2817789Sahrens pvd = oldvd->vdev_parent; 2818789Sahrens 28192082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 28204451Seschrock VDEV_ALLOC_ADD)) != 0) 28214451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 28224451Seschrock 28234451Seschrock if (newrootvd->vdev_children != 1) 2824789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2825789Sahrens 2826789Sahrens newvd = newrootvd->vdev_child[0]; 2827789Sahrens 2828789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2829789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2830789Sahrens 28312082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2832789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2833789Sahrens 28344527Sperrin /* 28354527Sperrin * Spares can't replace logs 28364527Sperrin */ 28377326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 28384527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28394527Sperrin 28402082Seschrock if (!replacing) { 28412082Seschrock /* 28422082Seschrock * For attach, the only allowable parent is a mirror or the root 28432082Seschrock * vdev. 28442082Seschrock */ 28452082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 28462082Seschrock pvd->vdev_ops != &vdev_root_ops) 28472082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28482082Seschrock 28492082Seschrock pvops = &vdev_mirror_ops; 28502082Seschrock } else { 28512082Seschrock /* 28522082Seschrock * Active hot spares can only be replaced by inactive hot 28532082Seschrock * spares. 28542082Seschrock */ 28552082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 28562082Seschrock pvd->vdev_child[1] == oldvd && 28572082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 28582082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28592082Seschrock 28602082Seschrock /* 28612082Seschrock * If the source is a hot spare, and the parent isn't already a 28622082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 28633377Seschrock * want to create a replacing vdev. The user is not allowed to 28643377Seschrock * attach to a spared vdev child unless the 'isspare' state is 28653377Seschrock * the same (spare replaces spare, non-spare replaces 28663377Seschrock * non-spare). 28672082Seschrock */ 28682082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 28692082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28703377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 28713377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 28723377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28732082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 28742082Seschrock newvd->vdev_isspare) 28752082Seschrock pvops = &vdev_spare_ops; 28762082Seschrock else 28772082Seschrock pvops = &vdev_replacing_ops; 28782082Seschrock } 28792082Seschrock 28801175Slling /* 28811175Slling * Compare the new device size with the replaceable/attachable 28821175Slling * device size. 28831175Slling */ 28841175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2885789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2886789Sahrens 28871732Sbonwick /* 28881732Sbonwick * The new device cannot have a higher alignment requirement 28891732Sbonwick * than the top-level vdev. 28901732Sbonwick */ 28911732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2892789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2893789Sahrens 2894789Sahrens /* 2895789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2896789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2897789Sahrens */ 2898789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2899789Sahrens spa_strfree(oldvd->vdev_path); 2900789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2901789Sahrens KM_SLEEP); 2902789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2903789Sahrens newvd->vdev_path, "old"); 2904789Sahrens if (oldvd->vdev_devid != NULL) { 2905789Sahrens spa_strfree(oldvd->vdev_devid); 2906789Sahrens oldvd->vdev_devid = NULL; 2907789Sahrens } 2908789Sahrens } 2909789Sahrens 2910789Sahrens /* 29112082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 29122082Seschrock * mirror/replacing/spare vdev above oldvd. 2913789Sahrens */ 2914789Sahrens if (pvd->vdev_ops != pvops) 2915789Sahrens pvd = vdev_add_parent(oldvd, pvops); 2916789Sahrens 2917789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2918789Sahrens ASSERT(pvd->vdev_ops == pvops); 2919789Sahrens ASSERT(oldvd->vdev_parent == pvd); 2920789Sahrens 2921789Sahrens /* 2922789Sahrens * Extract the new device from its root and add it to pvd. 2923789Sahrens */ 2924789Sahrens vdev_remove_child(newrootvd, newvd); 2925789Sahrens newvd->vdev_id = pvd->vdev_children; 2926789Sahrens vdev_add_child(pvd, newvd); 2927789Sahrens 29281544Seschrock /* 29291544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 29301544Seschrock * the addition of newvd may have decreased our parent's asize. 29311544Seschrock */ 29321544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 29331544Seschrock 2934789Sahrens tvd = newvd->vdev_top; 2935789Sahrens ASSERT(pvd->vdev_top == tvd); 2936789Sahrens ASSERT(tvd->vdev_parent == rvd); 2937789Sahrens 2938789Sahrens vdev_config_dirty(tvd); 2939789Sahrens 2940789Sahrens /* 2941789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2942789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2943789Sahrens */ 2944789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2945789Sahrens 2946789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2947789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2948789Sahrens open_txg - TXG_INITIAL + 1); 2949789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2950789Sahrens 29513377Seschrock if (newvd->vdev_isspare) 29523377Seschrock spa_spare_activate(newvd); 29537754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 29547754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 29557313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 29561544Seschrock 2957789Sahrens /* 2958789Sahrens * Mark newvd's DTL dirty in this txg. 2959789Sahrens */ 29601732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2961789Sahrens 2962789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2963789Sahrens 29647313SEric.Kustarz@Sun.COM tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 29657313SEric.Kustarz@Sun.COM if (dmu_tx_assign(tx, TXG_WAIT) == 0) { 29667313SEric.Kustarz@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx, 29677313SEric.Kustarz@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 29687313SEric.Kustarz@Sun.COM replacing && newvd_isspare ? "spare in" : 29697313SEric.Kustarz@Sun.COM replacing ? "replace" : "attach", newvdpath, 29707313SEric.Kustarz@Sun.COM replacing ? "for" : "to", oldvdpath); 29717313SEric.Kustarz@Sun.COM dmu_tx_commit(tx); 29727313SEric.Kustarz@Sun.COM } else { 29737313SEric.Kustarz@Sun.COM dmu_tx_abort(tx); 29747313SEric.Kustarz@Sun.COM } 29757313SEric.Kustarz@Sun.COM 29767313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 29777313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 29787313SEric.Kustarz@Sun.COM 2979789Sahrens /* 29807046Sahrens * Kick off a resilver to update newvd. 2981789Sahrens */ 29827046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 2983789Sahrens 2984789Sahrens return (0); 2985789Sahrens } 2986789Sahrens 2987789Sahrens /* 2988789Sahrens * Detach a device from a mirror or replacing vdev. 2989789Sahrens * If 'replace_done' is specified, only detach if the parent 2990789Sahrens * is a replacing vdev. 2991789Sahrens */ 2992789Sahrens int 29931544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2994789Sahrens { 2995789Sahrens uint64_t txg; 2996789Sahrens int c, t, error; 2997789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2998789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 29992082Seschrock boolean_t unspare = B_FALSE; 30002082Seschrock uint64_t unspare_guid; 30016673Seschrock size_t len; 3002789Sahrens 3003789Sahrens txg = spa_vdev_enter(spa); 3004789Sahrens 30056643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3006789Sahrens 3007789Sahrens if (vd == NULL) 3008789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3009789Sahrens 30101585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 30111585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30121585Sbonwick 3013789Sahrens pvd = vd->vdev_parent; 3014789Sahrens 3015789Sahrens /* 3016789Sahrens * If replace_done is specified, only remove this device if it's 30172082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 30182082Seschrock * disk can be removed. 3019789Sahrens */ 30202082Seschrock if (replace_done) { 30212082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 30222082Seschrock if (vd->vdev_id != 0) 30232082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30242082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 30252082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30262082Seschrock } 30272082Seschrock } 30282082Seschrock 30292082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 30304577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3031789Sahrens 3032789Sahrens /* 30332082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3034789Sahrens */ 3035789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 30362082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 30372082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3038789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3039789Sahrens 3040789Sahrens /* 3041789Sahrens * If there's only one replica, you can't detach it. 3042789Sahrens */ 3043789Sahrens if (pvd->vdev_children <= 1) 3044789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3045789Sahrens 3046789Sahrens /* 3047789Sahrens * If all siblings have non-empty DTLs, this device may have the only 3048789Sahrens * valid copy of the data, which means we cannot safely detach it. 3049789Sahrens * 3050789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 3051789Sahrens * precise DTL check. 3052789Sahrens */ 3053789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 3054789Sahrens uint64_t dirty; 3055789Sahrens 3056789Sahrens cvd = pvd->vdev_child[c]; 3057789Sahrens if (cvd == vd) 3058789Sahrens continue; 3059789Sahrens if (vdev_is_dead(cvd)) 3060789Sahrens continue; 3061789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 3062789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 3063789Sahrens cvd->vdev_dtl_scrub.sm_space; 3064789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 3065789Sahrens if (!dirty) 3066789Sahrens break; 3067789Sahrens } 30682082Seschrock 30697754SJeff.Bonwick@Sun.COM if (c == pvd->vdev_children) 3070789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3071789Sahrens 3072789Sahrens /* 30736673Seschrock * If we are detaching the second disk from a replacing vdev, then 30746673Seschrock * check to see if we changed the original vdev's path to have "/old" 30756673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 30766673Seschrock */ 30776673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 30786673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 30796673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 30806673Seschrock ASSERT(pvd->vdev_child[1] == vd); 30816673Seschrock cvd = pvd->vdev_child[0]; 30826673Seschrock len = strlen(vd->vdev_path); 30836673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 30846673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 30856673Seschrock spa_strfree(cvd->vdev_path); 30866673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 30876673Seschrock } 30886673Seschrock } 30896673Seschrock 30906673Seschrock /* 30912082Seschrock * If we are detaching the original disk from a spare, then it implies 30922082Seschrock * that the spare should become a real disk, and be removed from the 30932082Seschrock * active spare list for the pool. 30942082Seschrock */ 30952082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 30962082Seschrock vd->vdev_id == 0) 30972082Seschrock unspare = B_TRUE; 30982082Seschrock 30992082Seschrock /* 3100789Sahrens * Erase the disk labels so the disk can be used for other things. 3101789Sahrens * This must be done after all other error cases are handled, 3102789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3103789Sahrens * But if we can't do it, don't treat the error as fatal -- 3104789Sahrens * it may be that the unwritability of the disk is the reason 3105789Sahrens * it's being detached! 3106789Sahrens */ 31073377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3108789Sahrens 3109789Sahrens /* 3110789Sahrens * Remove vd from its parent and compact the parent's children. 3111789Sahrens */ 3112789Sahrens vdev_remove_child(pvd, vd); 3113789Sahrens vdev_compact_children(pvd); 3114789Sahrens 3115789Sahrens /* 3116789Sahrens * Remember one of the remaining children so we can get tvd below. 3117789Sahrens */ 3118789Sahrens cvd = pvd->vdev_child[0]; 3119789Sahrens 3120789Sahrens /* 31212082Seschrock * If we need to remove the remaining child from the list of hot spares, 31222082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 31232082Seschrock * must do this before vdev_remove_parent(), because that can change the 31242082Seschrock * GUID if it creates a new toplevel GUID. 31252082Seschrock */ 31262082Seschrock if (unspare) { 31272082Seschrock ASSERT(cvd->vdev_isspare); 31283377Seschrock spa_spare_remove(cvd); 31292082Seschrock unspare_guid = cvd->vdev_guid; 31302082Seschrock } 31312082Seschrock 31322082Seschrock /* 3133789Sahrens * If the parent mirror/replacing vdev only has one child, 3134789Sahrens * the parent is no longer needed. Remove it from the tree. 3135789Sahrens */ 3136789Sahrens if (pvd->vdev_children == 1) 3137789Sahrens vdev_remove_parent(cvd); 3138789Sahrens 3139789Sahrens /* 3140789Sahrens * We don't set tvd until now because the parent we just removed 3141789Sahrens * may have been the previous top-level vdev. 3142789Sahrens */ 3143789Sahrens tvd = cvd->vdev_top; 3144789Sahrens ASSERT(tvd->vdev_parent == rvd); 3145789Sahrens 3146789Sahrens /* 31473377Seschrock * Reevaluate the parent vdev state. 3148789Sahrens */ 31494451Seschrock vdev_propagate_state(cvd); 3150789Sahrens 3151789Sahrens /* 31523377Seschrock * If the device we just detached was smaller than the others, it may be 31533377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 31543377Seschrock * can't fail because the existing metaslabs are already in core, so 31553377Seschrock * there's nothing to read from disk. 3156789Sahrens */ 31571732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3158789Sahrens 3159789Sahrens vdev_config_dirty(tvd); 3160789Sahrens 3161789Sahrens /* 31623377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 31633377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 31643377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 31653377Seschrock * prevent vd from being accessed after it's freed. 3166789Sahrens */ 3167789Sahrens for (t = 0; t < TXG_SIZE; t++) 3168789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 31691732Sbonwick vd->vdev_detached = B_TRUE; 31701732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3171789Sahrens 31724451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 31734451Seschrock 31742082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 31752082Seschrock 31762082Seschrock /* 31773377Seschrock * If this was the removal of the original device in a hot spare vdev, 31783377Seschrock * then we want to go through and remove the device from the hot spare 31793377Seschrock * list of every other pool. 31802082Seschrock */ 31812082Seschrock if (unspare) { 31822082Seschrock spa = NULL; 31832082Seschrock mutex_enter(&spa_namespace_lock); 31842082Seschrock while ((spa = spa_next(spa)) != NULL) { 31852082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 31862082Seschrock continue; 3187*7793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 3188*7793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 31892082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 3190*7793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 3191*7793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 31922082Seschrock } 31932082Seschrock mutex_exit(&spa_namespace_lock); 31942082Seschrock } 31952082Seschrock 31962082Seschrock return (error); 31972082Seschrock } 31982082Seschrock 31997754SJeff.Bonwick@Sun.COM static nvlist_t * 32007754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 32012082Seschrock { 32027754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 32037754SJeff.Bonwick@Sun.COM uint64_t guid; 32047754SJeff.Bonwick@Sun.COM 32057754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 32067754SJeff.Bonwick@Sun.COM &guid) == 0); 32077754SJeff.Bonwick@Sun.COM 32087754SJeff.Bonwick@Sun.COM if (guid == target_guid) 32097754SJeff.Bonwick@Sun.COM return (nvpp[i]); 32102082Seschrock } 32112082Seschrock 32127754SJeff.Bonwick@Sun.COM return (NULL); 32135450Sbrendan } 32145450Sbrendan 32157754SJeff.Bonwick@Sun.COM static void 32167754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 32177754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 32185450Sbrendan { 32197754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 32207754SJeff.Bonwick@Sun.COM 32217754SJeff.Bonwick@Sun.COM if (count > 1) 32227754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 32237754SJeff.Bonwick@Sun.COM 32247754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 32257754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 32267754SJeff.Bonwick@Sun.COM continue; 32277754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 32285450Sbrendan } 32295450Sbrendan 32307754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 32317754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 32327754SJeff.Bonwick@Sun.COM 32337754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 32347754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 32357754SJeff.Bonwick@Sun.COM 32367754SJeff.Bonwick@Sun.COM if (count > 1) 32377754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 32385450Sbrendan } 32395450Sbrendan 32405450Sbrendan /* 32415450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 32425450Sbrendan * spares and level 2 ARC devices. 32435450Sbrendan */ 32445450Sbrendan int 32455450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 32465450Sbrendan { 32475450Sbrendan vdev_t *vd; 32487754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 32495450Sbrendan uint_t nspares, nl2cache; 32507754SJeff.Bonwick@Sun.COM uint64_t txg; 32515450Sbrendan int error = 0; 32525450Sbrendan 32537754SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 32545450Sbrendan 32556643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 32565450Sbrendan 32575450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 32585450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 32597754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 32607754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 32617754SJeff.Bonwick@Sun.COM /* 32627754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 32637754SJeff.Bonwick@Sun.COM * in this pool. 32647754SJeff.Bonwick@Sun.COM */ 32657754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 32667754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 32677754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 32687754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 32697754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 32707754SJeff.Bonwick@Sun.COM } else { 32717754SJeff.Bonwick@Sun.COM error = EBUSY; 32727754SJeff.Bonwick@Sun.COM } 32737754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 32745450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 32757754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 32767754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 32777754SJeff.Bonwick@Sun.COM /* 32787754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 32797754SJeff.Bonwick@Sun.COM */ 32807754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 32817754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 32825450Sbrendan spa_load_l2cache(spa); 32835450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 32847754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 32857754SJeff.Bonwick@Sun.COM /* 32867754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 32877754SJeff.Bonwick@Sun.COM */ 32887754SJeff.Bonwick@Sun.COM error = ENOTSUP; 32897754SJeff.Bonwick@Sun.COM } else { 32907754SJeff.Bonwick@Sun.COM /* 32917754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 32927754SJeff.Bonwick@Sun.COM */ 32937754SJeff.Bonwick@Sun.COM error = ENOENT; 32945450Sbrendan } 32952082Seschrock 32967754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 3297789Sahrens } 3298789Sahrens 3299789Sahrens /* 33004451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 33014451Seschrock * current spared, so we can detach it. 3302789Sahrens */ 33031544Seschrock static vdev_t * 33044451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3305789Sahrens { 33061544Seschrock vdev_t *newvd, *oldvd; 3307789Sahrens int c; 3308789Sahrens 33091544Seschrock for (c = 0; c < vd->vdev_children; c++) { 33104451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 33111544Seschrock if (oldvd != NULL) 33121544Seschrock return (oldvd); 33131544Seschrock } 3314789Sahrens 33154451Seschrock /* 33164451Seschrock * Check for a completed replacement. 33174451Seschrock */ 3318789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 33191544Seschrock oldvd = vd->vdev_child[0]; 33201544Seschrock newvd = vd->vdev_child[1]; 3321789Sahrens 33221544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 33231544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 33241544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 33251544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33261544Seschrock return (oldvd); 33271544Seschrock } 33281544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33291544Seschrock } 3330789Sahrens 33314451Seschrock /* 33324451Seschrock * Check for a completed resilver with the 'unspare' flag set. 33334451Seschrock */ 33344451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 33354451Seschrock newvd = vd->vdev_child[0]; 33364451Seschrock oldvd = vd->vdev_child[1]; 33374451Seschrock 33384451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 33394451Seschrock if (newvd->vdev_unspare && 33404451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 33414451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 33424451Seschrock newvd->vdev_unspare = 0; 33434451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33444451Seschrock return (oldvd); 33454451Seschrock } 33464451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33474451Seschrock } 33484451Seschrock 33491544Seschrock return (NULL); 3350789Sahrens } 3351789Sahrens 33521544Seschrock static void 33534451Seschrock spa_vdev_resilver_done(spa_t *spa) 3354789Sahrens { 33551544Seschrock vdev_t *vd; 33562082Seschrock vdev_t *pvd; 33571544Seschrock uint64_t guid; 33582082Seschrock uint64_t pguid = 0; 3359789Sahrens 33607754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3361789Sahrens 33624451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 33631544Seschrock guid = vd->vdev_guid; 33642082Seschrock /* 33652082Seschrock * If we have just finished replacing a hot spared device, then 33662082Seschrock * we need to detach the parent's first child (the original hot 33672082Seschrock * spare) as well. 33682082Seschrock */ 33692082Seschrock pvd = vd->vdev_parent; 33702082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 33712082Seschrock pvd->vdev_id == 0) { 33722082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 33732082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 33742082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 33752082Seschrock } 33767754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 33771544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 33781544Seschrock return; 33792082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 33802082Seschrock return; 33817754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3382789Sahrens } 3383789Sahrens 33847754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 3385789Sahrens } 3386789Sahrens 3387789Sahrens /* 33881354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 33891354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 33901354Seschrock */ 33911354Seschrock int 33921354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 33931354Seschrock { 33946643Seschrock vdev_t *vd; 33951354Seschrock uint64_t txg; 33961354Seschrock 33971354Seschrock txg = spa_vdev_enter(spa); 33981354Seschrock 33996643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 34002082Seschrock /* 34016643Seschrock * Determine if this is a reference to a hot spare device. If 34026643Seschrock * it is, update the path manually as there is no associated 34036643Seschrock * vdev_t that can be synced to disk. 34042082Seschrock */ 34056643Seschrock nvlist_t **spares; 34066643Seschrock uint_t i, nspares; 34075450Sbrendan 34085450Sbrendan if (spa->spa_spares.sav_config != NULL) { 34095450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 34105450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 34115450Sbrendan &spares, &nspares) == 0); 34122082Seschrock for (i = 0; i < nspares; i++) { 34132082Seschrock uint64_t theguid; 34142082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 34152082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 34165450Sbrendan if (theguid == guid) { 34175450Sbrendan VERIFY(nvlist_add_string(spares[i], 34185450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 34195450Sbrendan spa_load_spares(spa); 34205450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 34215450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 34225450Sbrendan 0)); 34235450Sbrendan } 34242082Seschrock } 34252082Seschrock } 34265450Sbrendan 34275450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 34282082Seschrock } 34291354Seschrock 34301585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 34311585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 34321585Sbonwick 34331354Seschrock spa_strfree(vd->vdev_path); 34341354Seschrock vd->vdev_path = spa_strdup(newpath); 34351354Seschrock 34361354Seschrock vdev_config_dirty(vd->vdev_top); 34371354Seschrock 34381354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 34391354Seschrock } 34401354Seschrock 34411354Seschrock /* 3442789Sahrens * ========================================================================== 3443789Sahrens * SPA Scrubbing 3444789Sahrens * ========================================================================== 3445789Sahrens */ 3446789Sahrens 34477046Sahrens int 34487046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3449789Sahrens { 34507754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 34514808Sek110237 3452789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3453789Sahrens return (ENOTSUP); 3454789Sahrens 3455789Sahrens /* 34567046Sahrens * If a resilver was requested, but there is no DTL on a 34577046Sahrens * writeable leaf device, we have nothing to do. 3458789Sahrens */ 34597046Sahrens if (type == POOL_SCRUB_RESILVER && 34607046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 34617046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 34621544Seschrock return (0); 34631544Seschrock } 3464789Sahrens 34657046Sahrens if (type == POOL_SCRUB_EVERYTHING && 34667046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 34677046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 34687046Sahrens return (EBUSY); 34697046Sahrens 34707046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 34717046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 34727046Sahrens } else if (type == POOL_SCRUB_NONE) { 34737046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 34741544Seschrock } else { 34757046Sahrens return (EINVAL); 34761544Seschrock } 3477789Sahrens } 3478789Sahrens 34791544Seschrock /* 34801544Seschrock * ========================================================================== 34811544Seschrock * SPA async task processing 34821544Seschrock * ========================================================================== 34831544Seschrock */ 34841544Seschrock 34851544Seschrock static void 34864451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3487789Sahrens { 34887361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 34897361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 34907361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 34917754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 34927754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 34931544Seschrock } 34947361SBrendan.Gregg@Sun.COM 34957754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 34967361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 34971544Seschrock } 34981544Seschrock 34991544Seschrock static void 35007754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 35017754SJeff.Bonwick@Sun.COM { 35027754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 35037754SJeff.Bonwick@Sun.COM vd->vdev_probe_wanted = 0; 35047754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 35057754SJeff.Bonwick@Sun.COM } 35067754SJeff.Bonwick@Sun.COM 35077754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 35087754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 35097754SJeff.Bonwick@Sun.COM } 35107754SJeff.Bonwick@Sun.COM 35117754SJeff.Bonwick@Sun.COM static void 35121544Seschrock spa_async_thread(spa_t *spa) 35131544Seschrock { 35147754SJeff.Bonwick@Sun.COM int tasks; 35151544Seschrock 35161544Seschrock ASSERT(spa->spa_sync_on); 3517789Sahrens 35181544Seschrock mutex_enter(&spa->spa_async_lock); 35191544Seschrock tasks = spa->spa_async_tasks; 35201544Seschrock spa->spa_async_tasks = 0; 35211544Seschrock mutex_exit(&spa->spa_async_lock); 35221544Seschrock 35231544Seschrock /* 35241635Sbonwick * See if the config needs to be updated. 35251635Sbonwick */ 35261635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 35271635Sbonwick mutex_enter(&spa_namespace_lock); 35281635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 35291635Sbonwick mutex_exit(&spa_namespace_lock); 35301635Sbonwick } 35311635Sbonwick 35321635Sbonwick /* 35334451Seschrock * See if any devices need to be marked REMOVED. 35341544Seschrock */ 35357754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 35367754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 35374451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 35387754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 35397361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 35407754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 35417361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 35427754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 35437754SJeff.Bonwick@Sun.COM } 35447754SJeff.Bonwick@Sun.COM 35457754SJeff.Bonwick@Sun.COM /* 35467754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 35477754SJeff.Bonwick@Sun.COM */ 35487754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 35497754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 35507754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 35517754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 35524451Seschrock } 35531544Seschrock 35541544Seschrock /* 35551544Seschrock * If any devices are done replacing, detach them. 35561544Seschrock */ 35574451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 35584451Seschrock spa_vdev_resilver_done(spa); 3559789Sahrens 35601544Seschrock /* 35611544Seschrock * Kick off a resilver. 35621544Seschrock */ 35637046Sahrens if (tasks & SPA_ASYNC_RESILVER) 35647046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 35651544Seschrock 35661544Seschrock /* 35671544Seschrock * Let the world know that we're done. 35681544Seschrock */ 35691544Seschrock mutex_enter(&spa->spa_async_lock); 35701544Seschrock spa->spa_async_thread = NULL; 35711544Seschrock cv_broadcast(&spa->spa_async_cv); 35721544Seschrock mutex_exit(&spa->spa_async_lock); 35731544Seschrock thread_exit(); 35741544Seschrock } 35751544Seschrock 35761544Seschrock void 35771544Seschrock spa_async_suspend(spa_t *spa) 35781544Seschrock { 35791544Seschrock mutex_enter(&spa->spa_async_lock); 35801544Seschrock spa->spa_async_suspended++; 35811544Seschrock while (spa->spa_async_thread != NULL) 35821544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 35831544Seschrock mutex_exit(&spa->spa_async_lock); 35841544Seschrock } 35851544Seschrock 35861544Seschrock void 35871544Seschrock spa_async_resume(spa_t *spa) 35881544Seschrock { 35891544Seschrock mutex_enter(&spa->spa_async_lock); 35901544Seschrock ASSERT(spa->spa_async_suspended != 0); 35911544Seschrock spa->spa_async_suspended--; 35921544Seschrock mutex_exit(&spa->spa_async_lock); 35931544Seschrock } 35941544Seschrock 35951544Seschrock static void 35961544Seschrock spa_async_dispatch(spa_t *spa) 35971544Seschrock { 35981544Seschrock mutex_enter(&spa->spa_async_lock); 35991544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 36001635Sbonwick spa->spa_async_thread == NULL && 36011635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 36021544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 36031544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 36041544Seschrock mutex_exit(&spa->spa_async_lock); 36051544Seschrock } 36061544Seschrock 36071544Seschrock void 36081544Seschrock spa_async_request(spa_t *spa, int task) 36091544Seschrock { 36101544Seschrock mutex_enter(&spa->spa_async_lock); 36111544Seschrock spa->spa_async_tasks |= task; 36121544Seschrock mutex_exit(&spa->spa_async_lock); 3613789Sahrens } 3614789Sahrens 3615789Sahrens /* 3616789Sahrens * ========================================================================== 3617789Sahrens * SPA syncing routines 3618789Sahrens * ========================================================================== 3619789Sahrens */ 3620789Sahrens 3621789Sahrens static void 3622789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3623789Sahrens { 3624789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3625789Sahrens dmu_tx_t *tx; 3626789Sahrens blkptr_t blk; 3627789Sahrens uint64_t itor = 0; 3628789Sahrens zio_t *zio; 3629789Sahrens int error; 3630789Sahrens uint8_t c = 1; 3631789Sahrens 36327754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 36337754SJeff.Bonwick@Sun.COM 36347754SJeff.Bonwick@Sun.COM while (bplist_iterate(bpl, &itor, &blk) == 0) { 36357754SJeff.Bonwick@Sun.COM ASSERT(blk.blk_birth < txg); 36367754SJeff.Bonwick@Sun.COM zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL, 36377754SJeff.Bonwick@Sun.COM ZIO_FLAG_MUSTSUCCEED)); 36387754SJeff.Bonwick@Sun.COM } 3639789Sahrens 3640789Sahrens error = zio_wait(zio); 3641789Sahrens ASSERT3U(error, ==, 0); 3642789Sahrens 3643789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3644789Sahrens bplist_vacate(bpl, tx); 3645789Sahrens 3646789Sahrens /* 3647789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3648789Sahrens * (Usually only the first block is needed.) 3649789Sahrens */ 3650789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3651789Sahrens dmu_tx_commit(tx); 3652789Sahrens } 3653789Sahrens 3654789Sahrens static void 36552082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 36562082Seschrock { 36572082Seschrock char *packed = NULL; 36587497STim.Haley@Sun.COM size_t bufsize; 36592082Seschrock size_t nvsize = 0; 36602082Seschrock dmu_buf_t *db; 36612082Seschrock 36622082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 36632082Seschrock 36647497STim.Haley@Sun.COM /* 36657497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 36667497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 36677497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 36687497STim.Haley@Sun.COM */ 36697497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 36707497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 36712082Seschrock 36722082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 36732082Seschrock KM_SLEEP) == 0); 36747497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 36757497STim.Haley@Sun.COM 36767497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 36777497STim.Haley@Sun.COM 36787497STim.Haley@Sun.COM kmem_free(packed, bufsize); 36792082Seschrock 36802082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 36812082Seschrock dmu_buf_will_dirty(db, tx); 36822082Seschrock *(uint64_t *)db->db_data = nvsize; 36832082Seschrock dmu_buf_rele(db, FTAG); 36842082Seschrock } 36852082Seschrock 36862082Seschrock static void 36875450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 36885450Sbrendan const char *config, const char *entry) 36892082Seschrock { 36902082Seschrock nvlist_t *nvroot; 36915450Sbrendan nvlist_t **list; 36922082Seschrock int i; 36932082Seschrock 36945450Sbrendan if (!sav->sav_sync) 36952082Seschrock return; 36962082Seschrock 36972082Seschrock /* 36985450Sbrendan * Update the MOS nvlist describing the list of available devices. 36995450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 37004451Seschrock * valid and the vdevs are labeled appropriately. 37012082Seschrock */ 37025450Sbrendan if (sav->sav_object == 0) { 37035450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 37045450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 37055450Sbrendan sizeof (uint64_t), tx); 37062082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 37075450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 37085450Sbrendan &sav->sav_object, tx) == 0); 37092082Seschrock } 37102082Seschrock 37112082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 37125450Sbrendan if (sav->sav_count == 0) { 37135450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 37142082Seschrock } else { 37155450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 37165450Sbrendan for (i = 0; i < sav->sav_count; i++) 37175450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 37185450Sbrendan B_FALSE, B_FALSE, B_TRUE); 37195450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 37205450Sbrendan sav->sav_count) == 0); 37215450Sbrendan for (i = 0; i < sav->sav_count; i++) 37225450Sbrendan nvlist_free(list[i]); 37235450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 37242082Seschrock } 37252082Seschrock 37265450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 37272926Sek110237 nvlist_free(nvroot); 37282082Seschrock 37295450Sbrendan sav->sav_sync = B_FALSE; 37302082Seschrock } 37312082Seschrock 37322082Seschrock static void 3733789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3734789Sahrens { 3735789Sahrens nvlist_t *config; 3736789Sahrens 37377754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 3738789Sahrens return; 3739789Sahrens 37407754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 37417754SJeff.Bonwick@Sun.COM 37427754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 37437754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 37447754SJeff.Bonwick@Sun.COM 37457754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 3746789Sahrens 37471635Sbonwick if (spa->spa_config_syncing) 37481635Sbonwick nvlist_free(spa->spa_config_syncing); 37491635Sbonwick spa->spa_config_syncing = config; 3750789Sahrens 37512082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3752789Sahrens } 3753789Sahrens 37545094Slling /* 37555094Slling * Set zpool properties. 37565094Slling */ 37573912Slling static void 37584543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 37593912Slling { 37603912Slling spa_t *spa = arg1; 37615094Slling objset_t *mos = spa->spa_meta_objset; 37623912Slling nvlist_t *nvp = arg2; 37635094Slling nvpair_t *elem; 37644451Seschrock uint64_t intval; 37656643Seschrock char *strval; 37665094Slling zpool_prop_t prop; 37675094Slling const char *propname; 37685094Slling zprop_type_t proptype; 37696643Seschrock spa_config_dirent_t *dp; 37705094Slling 37717754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 37727754SJeff.Bonwick@Sun.COM 37735094Slling elem = NULL; 37745094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 37755094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 37765094Slling case ZPOOL_PROP_VERSION: 37775094Slling /* 37785094Slling * Only set version for non-zpool-creation cases 37795094Slling * (set/import). spa_create() needs special care 37805094Slling * for version setting. 37815094Slling */ 37825094Slling if (tx->tx_txg != TXG_INITIAL) { 37835094Slling VERIFY(nvpair_value_uint64(elem, 37845094Slling &intval) == 0); 37855094Slling ASSERT(intval <= SPA_VERSION); 37865094Slling ASSERT(intval >= spa_version(spa)); 37875094Slling spa->spa_uberblock.ub_version = intval; 37885094Slling vdev_config_dirty(spa->spa_root_vdev); 37895094Slling } 37905094Slling break; 37915094Slling 37925094Slling case ZPOOL_PROP_ALTROOT: 37935094Slling /* 37945094Slling * 'altroot' is a non-persistent property. It should 37955094Slling * have been set temporarily at creation or import time. 37965094Slling */ 37975094Slling ASSERT(spa->spa_root != NULL); 37985094Slling break; 37995094Slling 38005363Seschrock case ZPOOL_PROP_CACHEFILE: 38015094Slling /* 38025363Seschrock * 'cachefile' is a non-persistent property, but note 38035363Seschrock * an async request that the config cache needs to be 38045363Seschrock * udpated. 38055094Slling */ 38065363Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 38076643Seschrock 38087754SJeff.Bonwick@Sun.COM dp = kmem_alloc(sizeof (spa_config_dirent_t), KM_SLEEP); 38096643Seschrock 38106643Seschrock if (strval[0] == '\0') 38116643Seschrock dp->scd_path = spa_strdup(spa_config_path); 38126643Seschrock else if (strcmp(strval, "none") == 0) 38136643Seschrock dp->scd_path = NULL; 38146643Seschrock else 38156643Seschrock dp->scd_path = spa_strdup(strval); 38166643Seschrock 38176643Seschrock list_insert_head(&spa->spa_config_list, dp); 38185363Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 38194543Smarks break; 38205094Slling default: 38215094Slling /* 38225094Slling * Set pool property values in the poolprops mos object. 38235094Slling */ 38245094Slling if (spa->spa_pool_props_object == 0) { 38255094Slling objset_t *mos = spa->spa_meta_objset; 38265094Slling 38275094Slling VERIFY((spa->spa_pool_props_object = 38285094Slling zap_create(mos, DMU_OT_POOL_PROPS, 38295094Slling DMU_OT_NONE, 0, tx)) > 0); 38305094Slling 38315094Slling VERIFY(zap_update(mos, 38325094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 38335094Slling 8, 1, &spa->spa_pool_props_object, tx) 38345094Slling == 0); 38355094Slling } 38365094Slling 38375094Slling /* normalize the property name */ 38385094Slling propname = zpool_prop_to_name(prop); 38395094Slling proptype = zpool_prop_get_type(prop); 38405094Slling 38415094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 38425094Slling ASSERT(proptype == PROP_TYPE_STRING); 38435094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 38445094Slling VERIFY(zap_update(mos, 38455094Slling spa->spa_pool_props_object, propname, 38465094Slling 1, strlen(strval) + 1, strval, tx) == 0); 38475094Slling 38485094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 38495094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 38505094Slling 38515094Slling if (proptype == PROP_TYPE_INDEX) { 38525094Slling const char *unused; 38535094Slling VERIFY(zpool_prop_index_to_string( 38545094Slling prop, intval, &unused) == 0); 38555094Slling } 38565094Slling VERIFY(zap_update(mos, 38575094Slling spa->spa_pool_props_object, propname, 38585094Slling 8, 1, &intval, tx) == 0); 38595094Slling } else { 38605094Slling ASSERT(0); /* not allowed */ 38615094Slling } 38625094Slling 38635329Sgw25295 switch (prop) { 38645329Sgw25295 case ZPOOL_PROP_DELEGATION: 38655094Slling spa->spa_delegation = intval; 38665329Sgw25295 break; 38675329Sgw25295 case ZPOOL_PROP_BOOTFS: 38685094Slling spa->spa_bootfs = intval; 38695329Sgw25295 break; 38705329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 38715329Sgw25295 spa->spa_failmode = intval; 38725329Sgw25295 break; 38735329Sgw25295 default: 38745329Sgw25295 break; 38755329Sgw25295 } 38763912Slling } 38775094Slling 38785094Slling /* log internal history if this is not a zpool create */ 38795094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 38805094Slling tx->tx_txg != TXG_INITIAL) { 38815094Slling spa_history_internal_log(LOG_POOL_PROPSET, 38825094Slling spa, tx, cr, "%s %lld %s", 38837754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 38845094Slling } 38853912Slling } 38867754SJeff.Bonwick@Sun.COM 38877754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 38883912Slling } 38893912Slling 3890789Sahrens /* 3891789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3892789Sahrens * part of the process, so we iterate until it converges. 3893789Sahrens */ 3894789Sahrens void 3895789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3896789Sahrens { 3897789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3898789Sahrens objset_t *mos = spa->spa_meta_objset; 3899789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 39001635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3901789Sahrens vdev_t *vd; 3902789Sahrens dmu_tx_t *tx; 3903789Sahrens int dirty_vdevs; 39047754SJeff.Bonwick@Sun.COM int error; 3905789Sahrens 3906789Sahrens /* 3907789Sahrens * Lock out configuration changes. 3908789Sahrens */ 39097754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3910789Sahrens 3911789Sahrens spa->spa_syncing_txg = txg; 3912789Sahrens spa->spa_sync_pass = 0; 3913789Sahrens 39147754SJeff.Bonwick@Sun.COM /* 39157754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 39167754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 39177754SJeff.Bonwick@Sun.COM */ 39187754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 39197754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 39207754SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 39217754SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 39227754SJeff.Bonwick@Sun.COM } 39237754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 39247754SJeff.Bonwick@Sun.COM 39251544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3926789Sahrens 39272082Seschrock tx = dmu_tx_create_assigned(dp, txg); 39282082Seschrock 39292082Seschrock /* 39304577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 39312082Seschrock * set spa_deflate if we have no raid-z vdevs. 39322082Seschrock */ 39334577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 39344577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 39352082Seschrock int i; 39362082Seschrock 39372082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 39382082Seschrock vd = rvd->vdev_child[i]; 39392082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 39402082Seschrock break; 39412082Seschrock } 39422082Seschrock if (i == rvd->vdev_children) { 39432082Seschrock spa->spa_deflate = TRUE; 39442082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 39452082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 39462082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 39472082Seschrock } 39482082Seschrock } 39492082Seschrock 39507046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 39517046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 39527046Sahrens dsl_pool_create_origin(dp, tx); 39537046Sahrens 39547046Sahrens /* Keeping the origin open increases spa_minref */ 39557046Sahrens spa->spa_minref += 3; 39567046Sahrens } 39577046Sahrens 39587046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 39597046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 39607046Sahrens dsl_pool_upgrade_clones(dp, tx); 39617046Sahrens } 39627046Sahrens 3963789Sahrens /* 3964789Sahrens * If anything has changed in this txg, push the deferred frees 3965789Sahrens * from the previous txg. If not, leave them alone so that we 3966789Sahrens * don't generate work on an otherwise idle system. 3967789Sahrens */ 3968789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 39692329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 39702329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3971789Sahrens spa_sync_deferred_frees(spa, txg); 3972789Sahrens 3973789Sahrens /* 3974789Sahrens * Iterate to convergence. 3975789Sahrens */ 3976789Sahrens do { 3977789Sahrens spa->spa_sync_pass++; 3978789Sahrens 3979789Sahrens spa_sync_config_object(spa, tx); 39805450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 39815450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 39825450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 39835450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 39841544Seschrock spa_errlog_sync(spa, txg); 3985789Sahrens dsl_pool_sync(dp, txg); 3986789Sahrens 3987789Sahrens dirty_vdevs = 0; 3988789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3989789Sahrens vdev_sync(vd, txg); 3990789Sahrens dirty_vdevs++; 3991789Sahrens } 3992789Sahrens 3993789Sahrens bplist_sync(bpl, tx); 3994789Sahrens } while (dirty_vdevs); 3995789Sahrens 3996789Sahrens bplist_close(bpl); 3997789Sahrens 3998789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3999789Sahrens 4000789Sahrens /* 4001789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4002789Sahrens * to commit the transaction group. 40031635Sbonwick * 40045688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 40055688Sbonwick * random top-level vdevs that are known to be visible in the 40067754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 40077754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4008789Sahrens */ 40097754SJeff.Bonwick@Sun.COM for (;;) { 40107754SJeff.Bonwick@Sun.COM /* 40117754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 40127754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 40137754SJeff.Bonwick@Sun.COM */ 40147754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 40157754SJeff.Bonwick@Sun.COM 40167754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 40177754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 40187754SJeff.Bonwick@Sun.COM int svdcount = 0; 40197754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 40207754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 40217754SJeff.Bonwick@Sun.COM int c; 40227754SJeff.Bonwick@Sun.COM 40237754SJeff.Bonwick@Sun.COM for (c = 0; c < children; c++) { 40247754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 40257754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 40267754SJeff.Bonwick@Sun.COM continue; 40277754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 40287754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 40297754SJeff.Bonwick@Sun.COM break; 40307754SJeff.Bonwick@Sun.COM } 40317754SJeff.Bonwick@Sun.COM error = vdev_config_sync(svd, svdcount, txg); 40327754SJeff.Bonwick@Sun.COM } else { 40337754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 40347754SJeff.Bonwick@Sun.COM rvd->vdev_children, txg); 40351635Sbonwick } 40367754SJeff.Bonwick@Sun.COM 40377754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 40387754SJeff.Bonwick@Sun.COM 40397754SJeff.Bonwick@Sun.COM if (error == 0) 40407754SJeff.Bonwick@Sun.COM break; 40417754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 40427754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 40431635Sbonwick } 40442082Seschrock dmu_tx_commit(tx); 40452082Seschrock 40461635Sbonwick /* 40471635Sbonwick * Clear the dirty config list. 40481635Sbonwick */ 40497754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 40501635Sbonwick vdev_config_clean(vd); 40511635Sbonwick 40521635Sbonwick /* 40531635Sbonwick * Now that the new config has synced transactionally, 40541635Sbonwick * let it become visible to the config cache. 40551635Sbonwick */ 40561635Sbonwick if (spa->spa_config_syncing != NULL) { 40571635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 40581635Sbonwick spa->spa_config_txg = txg; 40591635Sbonwick spa->spa_config_syncing = NULL; 40601635Sbonwick } 4061789Sahrens 40627046Sahrens spa->spa_traverse_wanted = B_TRUE; 4063789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 40647046Sahrens spa->spa_traverse_wanted = B_FALSE; 4065789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4066789Sahrens rw_exit(&spa->spa_traverse_lock); 4067789Sahrens 4068789Sahrens /* 4069789Sahrens * Clean up the ZIL records for the synced txg. 4070789Sahrens */ 4071789Sahrens dsl_pool_zil_clean(dp); 4072789Sahrens 4073789Sahrens /* 4074789Sahrens * Update usable space statistics. 4075789Sahrens */ 4076789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4077789Sahrens vdev_sync_done(vd, txg); 4078789Sahrens 4079789Sahrens /* 4080789Sahrens * It had better be the case that we didn't dirty anything 40812082Seschrock * since vdev_config_sync(). 4082789Sahrens */ 4083789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4084789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4085789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4086789Sahrens ASSERT(bpl->bpl_queue == NULL); 4087789Sahrens 40887754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 40891544Seschrock 40901544Seschrock /* 40911544Seschrock * If any async tasks have been requested, kick them off. 40921544Seschrock */ 40931544Seschrock spa_async_dispatch(spa); 4094789Sahrens } 4095789Sahrens 4096789Sahrens /* 4097789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4098789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4099789Sahrens * sync. 4100789Sahrens */ 4101789Sahrens void 4102789Sahrens spa_sync_allpools(void) 4103789Sahrens { 4104789Sahrens spa_t *spa = NULL; 4105789Sahrens mutex_enter(&spa_namespace_lock); 4106789Sahrens while ((spa = spa_next(spa)) != NULL) { 41077754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4108789Sahrens continue; 4109789Sahrens spa_open_ref(spa, FTAG); 4110789Sahrens mutex_exit(&spa_namespace_lock); 4111789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4112789Sahrens mutex_enter(&spa_namespace_lock); 4113789Sahrens spa_close(spa, FTAG); 4114789Sahrens } 4115789Sahrens mutex_exit(&spa_namespace_lock); 4116789Sahrens } 4117789Sahrens 4118789Sahrens /* 4119789Sahrens * ========================================================================== 4120789Sahrens * Miscellaneous routines 4121789Sahrens * ========================================================================== 4122789Sahrens */ 4123789Sahrens 4124789Sahrens /* 4125789Sahrens * Remove all pools in the system. 4126789Sahrens */ 4127789Sahrens void 4128789Sahrens spa_evict_all(void) 4129789Sahrens { 4130789Sahrens spa_t *spa; 4131789Sahrens 4132789Sahrens /* 4133789Sahrens * Remove all cached state. All pools should be closed now, 4134789Sahrens * so every spa in the AVL tree should be unreferenced. 4135789Sahrens */ 4136789Sahrens mutex_enter(&spa_namespace_lock); 4137789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4138789Sahrens /* 41391544Seschrock * Stop async tasks. The async thread may need to detach 41401544Seschrock * a device that's been replaced, which requires grabbing 41411544Seschrock * spa_namespace_lock, so we must drop it here. 4142789Sahrens */ 4143789Sahrens spa_open_ref(spa, FTAG); 4144789Sahrens mutex_exit(&spa_namespace_lock); 41451544Seschrock spa_async_suspend(spa); 41464808Sek110237 mutex_enter(&spa_namespace_lock); 4147789Sahrens spa_close(spa, FTAG); 4148789Sahrens 4149789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4150789Sahrens spa_unload(spa); 4151789Sahrens spa_deactivate(spa); 4152789Sahrens } 4153789Sahrens spa_remove(spa); 4154789Sahrens } 4155789Sahrens mutex_exit(&spa_namespace_lock); 4156789Sahrens } 41571544Seschrock 41581544Seschrock vdev_t * 41596643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 41601544Seschrock { 41616643Seschrock vdev_t *vd; 41626643Seschrock int i; 41636643Seschrock 41646643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 41656643Seschrock return (vd); 41666643Seschrock 41676643Seschrock if (l2cache) { 41686643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 41696643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 41706643Seschrock if (vd->vdev_guid == guid) 41716643Seschrock return (vd); 41726643Seschrock } 41736643Seschrock } 41746643Seschrock 41756643Seschrock return (NULL); 41761544Seschrock } 41771760Seschrock 41781760Seschrock void 41795094Slling spa_upgrade(spa_t *spa, uint64_t version) 41801760Seschrock { 41817754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41821760Seschrock 41831760Seschrock /* 41841760Seschrock * This should only be called for a non-faulted pool, and since a 41851760Seschrock * future version would result in an unopenable pool, this shouldn't be 41861760Seschrock * possible. 41871760Seschrock */ 41884577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 41895094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 41905094Slling 41915094Slling spa->spa_uberblock.ub_version = version; 41921760Seschrock vdev_config_dirty(spa->spa_root_vdev); 41931760Seschrock 41947754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 41952082Seschrock 41962082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 41971760Seschrock } 41982082Seschrock 41992082Seschrock boolean_t 42002082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 42012082Seschrock { 42022082Seschrock int i; 42033377Seschrock uint64_t spareguid; 42045450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 42055450Sbrendan 42065450Sbrendan for (i = 0; i < sav->sav_count; i++) 42075450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 42082082Seschrock return (B_TRUE); 42092082Seschrock 42105450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 42115450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 42125450Sbrendan &spareguid) == 0 && spareguid == guid) 42133377Seschrock return (B_TRUE); 42143377Seschrock } 42153377Seschrock 42162082Seschrock return (B_FALSE); 42172082Seschrock } 42183912Slling 42194451Seschrock /* 42207214Slling * Check if a pool has an active shared spare device. 42217214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 42227214Slling */ 42237214Slling static boolean_t 42247214Slling spa_has_active_shared_spare(spa_t *spa) 42257214Slling { 42267214Slling int i, refcnt; 42277214Slling uint64_t pool; 42287214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 42297214Slling 42307214Slling for (i = 0; i < sav->sav_count; i++) { 42317214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 42327214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 42337214Slling refcnt > 2) 42347214Slling return (B_TRUE); 42357214Slling } 42367214Slling 42377214Slling return (B_FALSE); 42387214Slling } 42397214Slling 42407214Slling /* 42414451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 42424451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 42434451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 42444451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 42454451Seschrock * or zdb as real changes. 42464451Seschrock */ 42474451Seschrock void 42484451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 42494451Seschrock { 42504451Seschrock #ifdef _KERNEL 42514451Seschrock sysevent_t *ev; 42524451Seschrock sysevent_attr_list_t *attr = NULL; 42534451Seschrock sysevent_value_t value; 42544451Seschrock sysevent_id_t eid; 42554451Seschrock 42564451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 42574451Seschrock SE_SLEEP); 42584451Seschrock 42594451Seschrock value.value_type = SE_DATA_TYPE_STRING; 42604451Seschrock value.value.sv_string = spa_name(spa); 42614451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 42624451Seschrock goto done; 42634451Seschrock 42644451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 42654451Seschrock value.value.sv_uint64 = spa_guid(spa); 42664451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 42674451Seschrock goto done; 42684451Seschrock 42694451Seschrock if (vd) { 42704451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 42714451Seschrock value.value.sv_uint64 = vd->vdev_guid; 42724451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 42734451Seschrock SE_SLEEP) != 0) 42744451Seschrock goto done; 42754451Seschrock 42764451Seschrock if (vd->vdev_path) { 42774451Seschrock value.value_type = SE_DATA_TYPE_STRING; 42784451Seschrock value.value.sv_string = vd->vdev_path; 42794451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 42804451Seschrock &value, SE_SLEEP) != 0) 42814451Seschrock goto done; 42824451Seschrock } 42834451Seschrock } 42844451Seschrock 42855756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 42865756Seschrock goto done; 42875756Seschrock attr = NULL; 42885756Seschrock 42894451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 42904451Seschrock 42914451Seschrock done: 42924451Seschrock if (attr) 42934451Seschrock sysevent_free_attr(attr); 42944451Seschrock sysevent_free(ev); 42954451Seschrock #endif 42964451Seschrock } 4297