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 /* 238525SEric.Schrock@Sun.COM * Copyright 2009 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 638662SJordan.Vaughan@Sun.com #ifdef _KERNEL 648662SJordan.Vaughan@Sun.com #include <sys/zone.h> 658662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 668662SJordan.Vaughan@Sun.com 675094Slling #include "zfs_prop.h" 685913Sperrin #include "zfs_comutil.h" 695094Slling 707754SJeff.Bonwick@Sun.COM int zio_taskq_threads[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 717754SJeff.Bonwick@Sun.COM /* ISSUE INTR */ 727754SJeff.Bonwick@Sun.COM { 1, 1 }, /* ZIO_TYPE_NULL */ 739230SGeorge.Wilson@Sun.COM { 8, 8 }, /* ZIO_TYPE_READ */ 749230SGeorge.Wilson@Sun.COM { 8, 8 }, /* ZIO_TYPE_WRITE */ 757754SJeff.Bonwick@Sun.COM { 1, 1 }, /* ZIO_TYPE_FREE */ 767754SJeff.Bonwick@Sun.COM { 1, 1 }, /* ZIO_TYPE_CLAIM */ 777754SJeff.Bonwick@Sun.COM { 1, 1 }, /* ZIO_TYPE_IOCTL */ 787754SJeff.Bonwick@Sun.COM }; 792986Sek110237 805094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 817214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa); 825094Slling 835094Slling /* 845094Slling * ========================================================================== 855094Slling * SPA properties routines 865094Slling * ========================================================================== 875094Slling */ 885094Slling 895094Slling /* 905094Slling * Add a (source=src, propname=propval) list to an nvlist. 915094Slling */ 925949Slling static void 935094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 945094Slling uint64_t intval, zprop_source_t src) 955094Slling { 965094Slling const char *propname = zpool_prop_to_name(prop); 975094Slling nvlist_t *propval; 985949Slling 995949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1005949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 1015949Slling 1025949Slling if (strval != NULL) 1035949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1045949Slling else 1055949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 1065949Slling 1075949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 1085094Slling nvlist_free(propval); 1095094Slling } 1105094Slling 1115094Slling /* 1125094Slling * Get property values from the spa configuration. 1135094Slling */ 1145949Slling static void 1155094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1165094Slling { 1178525SEric.Schrock@Sun.COM uint64_t size; 1188525SEric.Schrock@Sun.COM uint64_t used; 1195094Slling uint64_t cap, version; 1205094Slling zprop_source_t src = ZPROP_SRC_NONE; 1216643Seschrock spa_config_dirent_t *dp; 1225094Slling 1237754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 1247754SJeff.Bonwick@Sun.COM 1258525SEric.Schrock@Sun.COM if (spa->spa_root_vdev != NULL) { 1268525SEric.Schrock@Sun.COM size = spa_get_space(spa); 1278525SEric.Schrock@Sun.COM used = spa_get_alloc(spa); 1288525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 1298525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 1308525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 1318525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, 1328525SEric.Schrock@Sun.COM size - used, src); 1338525SEric.Schrock@Sun.COM 1348525SEric.Schrock@Sun.COM cap = (size == 0) ? 0 : (used * 100 / size); 1358525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1368525SEric.Schrock@Sun.COM 1378525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1388525SEric.Schrock@Sun.COM spa->spa_root_vdev->vdev_state, src); 1398525SEric.Schrock@Sun.COM 1408525SEric.Schrock@Sun.COM version = spa_version(spa); 1418525SEric.Schrock@Sun.COM if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1428525SEric.Schrock@Sun.COM src = ZPROP_SRC_DEFAULT; 1438525SEric.Schrock@Sun.COM else 1448525SEric.Schrock@Sun.COM src = ZPROP_SRC_LOCAL; 1458525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1468525SEric.Schrock@Sun.COM } 1475949Slling 1485949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1495949Slling 1505949Slling if (spa->spa_root != NULL) 1515949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1525949Slling 0, ZPROP_SRC_LOCAL); 1535094Slling 1546643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 1556643Seschrock if (dp->scd_path == NULL) { 1565949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1576643Seschrock "none", 0, ZPROP_SRC_LOCAL); 1586643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1595949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1606643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1615363Seschrock } 1625363Seschrock } 1635094Slling } 1645094Slling 1655094Slling /* 1665094Slling * Get zpool property values. 1675094Slling */ 1685094Slling int 1695094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 1705094Slling { 1715094Slling zap_cursor_t zc; 1725094Slling zap_attribute_t za; 1735094Slling objset_t *mos = spa->spa_meta_objset; 1745094Slling int err; 1755094Slling 1765949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1775094Slling 1787754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 1797754SJeff.Bonwick@Sun.COM 1805094Slling /* 1815094Slling * Get properties from the spa config. 1825094Slling */ 1835949Slling spa_prop_get_config(spa, nvp); 1845094Slling 1855094Slling /* If no pool property object, no more prop to get. */ 1865094Slling if (spa->spa_pool_props_object == 0) { 1875094Slling mutex_exit(&spa->spa_props_lock); 1885094Slling return (0); 1895094Slling } 1905094Slling 1915094Slling /* 1925094Slling * Get properties from the MOS pool property object. 1935094Slling */ 1945094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 1955094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 1965094Slling zap_cursor_advance(&zc)) { 1975094Slling uint64_t intval = 0; 1985094Slling char *strval = NULL; 1995094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 2005094Slling zpool_prop_t prop; 2015094Slling 2025094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 2035094Slling continue; 2045094Slling 2055094Slling switch (za.za_integer_length) { 2065094Slling case 8: 2075094Slling /* integer property */ 2085094Slling if (za.za_first_integer != 2095094Slling zpool_prop_default_numeric(prop)) 2105094Slling src = ZPROP_SRC_LOCAL; 2115094Slling 2125094Slling if (prop == ZPOOL_PROP_BOOTFS) { 2135094Slling dsl_pool_t *dp; 2145094Slling dsl_dataset_t *ds = NULL; 2155094Slling 2165094Slling dp = spa_get_dsl(spa); 2175094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2186689Smaybee if (err = dsl_dataset_hold_obj(dp, 2196689Smaybee za.za_first_integer, FTAG, &ds)) { 2205094Slling rw_exit(&dp->dp_config_rwlock); 2215094Slling break; 2225094Slling } 2235094Slling 2245094Slling strval = kmem_alloc( 2255094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2265094Slling KM_SLEEP); 2275094Slling dsl_dataset_name(ds, strval); 2286689Smaybee dsl_dataset_rele(ds, FTAG); 2295094Slling rw_exit(&dp->dp_config_rwlock); 2305094Slling } else { 2315094Slling strval = NULL; 2325094Slling intval = za.za_first_integer; 2335094Slling } 2345094Slling 2355949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2365094Slling 2375094Slling if (strval != NULL) 2385094Slling kmem_free(strval, 2395094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2405094Slling 2415094Slling break; 2425094Slling 2435094Slling case 1: 2445094Slling /* string property */ 2455094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2465094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2475094Slling za.za_name, 1, za.za_num_integers, strval); 2485094Slling if (err) { 2495094Slling kmem_free(strval, za.za_num_integers); 2505094Slling break; 2515094Slling } 2525949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2535094Slling kmem_free(strval, za.za_num_integers); 2545094Slling break; 2555094Slling 2565094Slling default: 2575094Slling break; 2585094Slling } 2595094Slling } 2605094Slling zap_cursor_fini(&zc); 2615094Slling mutex_exit(&spa->spa_props_lock); 2625094Slling out: 2635094Slling if (err && err != ENOENT) { 2645094Slling nvlist_free(*nvp); 2655949Slling *nvp = NULL; 2665094Slling return (err); 2675094Slling } 2685094Slling 2695094Slling return (0); 2705094Slling } 2715094Slling 2725094Slling /* 2735094Slling * Validate the given pool properties nvlist and modify the list 2745094Slling * for the property values to be set. 2755094Slling */ 2765094Slling static int 2775094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 2785094Slling { 2795094Slling nvpair_t *elem; 2805094Slling int error = 0, reset_bootfs = 0; 2815094Slling uint64_t objnum; 2825094Slling 2835094Slling elem = NULL; 2845094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2855094Slling zpool_prop_t prop; 2865094Slling char *propname, *strval; 2875094Slling uint64_t intval; 2885094Slling objset_t *os; 2895363Seschrock char *slash; 2905094Slling 2915094Slling propname = nvpair_name(elem); 2925094Slling 2935094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 2945094Slling return (EINVAL); 2955094Slling 2965094Slling switch (prop) { 2975094Slling case ZPOOL_PROP_VERSION: 2985094Slling error = nvpair_value_uint64(elem, &intval); 2995094Slling if (!error && 3005094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 3015094Slling error = EINVAL; 3025094Slling break; 3035094Slling 3045094Slling case ZPOOL_PROP_DELEGATION: 3055094Slling case ZPOOL_PROP_AUTOREPLACE: 3067538SRichard.Morris@Sun.COM case ZPOOL_PROP_LISTSNAPS: 3075094Slling error = nvpair_value_uint64(elem, &intval); 3085094Slling if (!error && intval > 1) 3095094Slling error = EINVAL; 3105094Slling break; 3115094Slling 3125094Slling case ZPOOL_PROP_BOOTFS: 3135094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3145094Slling error = ENOTSUP; 3155094Slling break; 3165094Slling } 3175094Slling 3185094Slling /* 3197042Sgw25295 * Make sure the vdev config is bootable 3205094Slling */ 3217042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3225094Slling error = ENOTSUP; 3235094Slling break; 3245094Slling } 3255094Slling 3265094Slling reset_bootfs = 1; 3275094Slling 3285094Slling error = nvpair_value_string(elem, &strval); 3295094Slling 3305094Slling if (!error) { 3317042Sgw25295 uint64_t compress; 3327042Sgw25295 3335094Slling if (strval == NULL || strval[0] == '\0') { 3345094Slling objnum = zpool_prop_default_numeric( 3355094Slling ZPOOL_PROP_BOOTFS); 3365094Slling break; 3375094Slling } 3385094Slling 3395094Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 3406689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os)) 3415094Slling break; 3427042Sgw25295 3437042Sgw25295 /* We don't support gzip bootable datasets */ 3447042Sgw25295 if ((error = dsl_prop_get_integer(strval, 3457042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3467042Sgw25295 &compress, NULL)) == 0 && 3477042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3487042Sgw25295 error = ENOTSUP; 3497042Sgw25295 } else { 3507042Sgw25295 objnum = dmu_objset_id(os); 3517042Sgw25295 } 3525094Slling dmu_objset_close(os); 3535094Slling } 3545094Slling break; 3557754SJeff.Bonwick@Sun.COM 3565329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3575329Sgw25295 error = nvpair_value_uint64(elem, &intval); 3585329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3595329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 3605329Sgw25295 error = EINVAL; 3615329Sgw25295 3625329Sgw25295 /* 3635329Sgw25295 * This is a special case which only occurs when 3645329Sgw25295 * the pool has completely failed. This allows 3655329Sgw25295 * the user to change the in-core failmode property 3665329Sgw25295 * without syncing it out to disk (I/Os might 3675329Sgw25295 * currently be blocked). We do this by returning 3685329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 3695329Sgw25295 * into thinking we encountered a property validation 3705329Sgw25295 * error. 3715329Sgw25295 */ 3727754SJeff.Bonwick@Sun.COM if (!error && spa_suspended(spa)) { 3735329Sgw25295 spa->spa_failmode = intval; 3745329Sgw25295 error = EIO; 3755329Sgw25295 } 3765329Sgw25295 break; 3775363Seschrock 3785363Seschrock case ZPOOL_PROP_CACHEFILE: 3795363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3805363Seschrock break; 3815363Seschrock 3825363Seschrock if (strval[0] == '\0') 3835363Seschrock break; 3845363Seschrock 3855363Seschrock if (strcmp(strval, "none") == 0) 3865363Seschrock break; 3875363Seschrock 3885363Seschrock if (strval[0] != '/') { 3895363Seschrock error = EINVAL; 3905363Seschrock break; 3915363Seschrock } 3925363Seschrock 3935363Seschrock slash = strrchr(strval, '/'); 3945363Seschrock ASSERT(slash != NULL); 3955363Seschrock 3965363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3975363Seschrock strcmp(slash, "/..") == 0) 3985363Seschrock error = EINVAL; 3995363Seschrock break; 4005094Slling } 4015094Slling 4025094Slling if (error) 4035094Slling break; 4045094Slling } 4055094Slling 4065094Slling if (!error && reset_bootfs) { 4075094Slling error = nvlist_remove(props, 4085094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 4095094Slling 4105094Slling if (!error) { 4115094Slling error = nvlist_add_uint64(props, 4125094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 4135094Slling } 4145094Slling } 4155094Slling 4165094Slling return (error); 4175094Slling } 4185094Slling 4198525SEric.Schrock@Sun.COM void 4208525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 4218525SEric.Schrock@Sun.COM { 4228525SEric.Schrock@Sun.COM char *cachefile; 4238525SEric.Schrock@Sun.COM spa_config_dirent_t *dp; 4248525SEric.Schrock@Sun.COM 4258525SEric.Schrock@Sun.COM if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 4268525SEric.Schrock@Sun.COM &cachefile) != 0) 4278525SEric.Schrock@Sun.COM return; 4288525SEric.Schrock@Sun.COM 4298525SEric.Schrock@Sun.COM dp = kmem_alloc(sizeof (spa_config_dirent_t), 4308525SEric.Schrock@Sun.COM KM_SLEEP); 4318525SEric.Schrock@Sun.COM 4328525SEric.Schrock@Sun.COM if (cachefile[0] == '\0') 4338525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(spa_config_path); 4348525SEric.Schrock@Sun.COM else if (strcmp(cachefile, "none") == 0) 4358525SEric.Schrock@Sun.COM dp->scd_path = NULL; 4368525SEric.Schrock@Sun.COM else 4378525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(cachefile); 4388525SEric.Schrock@Sun.COM 4398525SEric.Schrock@Sun.COM list_insert_head(&spa->spa_config_list, dp); 4408525SEric.Schrock@Sun.COM if (need_sync) 4418525SEric.Schrock@Sun.COM spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 4428525SEric.Schrock@Sun.COM } 4438525SEric.Schrock@Sun.COM 4445094Slling int 4455094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4465094Slling { 4475094Slling int error; 4488525SEric.Schrock@Sun.COM nvpair_t *elem; 4498525SEric.Schrock@Sun.COM boolean_t need_sync = B_FALSE; 4508525SEric.Schrock@Sun.COM zpool_prop_t prop; 4515094Slling 4525094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 4535094Slling return (error); 4545094Slling 4558525SEric.Schrock@Sun.COM elem = NULL; 4568525SEric.Schrock@Sun.COM while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 4578525SEric.Schrock@Sun.COM if ((prop = zpool_name_to_prop( 4588525SEric.Schrock@Sun.COM nvpair_name(elem))) == ZPROP_INVAL) 4598525SEric.Schrock@Sun.COM return (EINVAL); 4608525SEric.Schrock@Sun.COM 4618525SEric.Schrock@Sun.COM if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 4628525SEric.Schrock@Sun.COM continue; 4638525SEric.Schrock@Sun.COM 4648525SEric.Schrock@Sun.COM need_sync = B_TRUE; 4658525SEric.Schrock@Sun.COM break; 4668525SEric.Schrock@Sun.COM } 4678525SEric.Schrock@Sun.COM 4688525SEric.Schrock@Sun.COM if (need_sync) 4698525SEric.Schrock@Sun.COM return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 4708525SEric.Schrock@Sun.COM spa, nvp, 3)); 4718525SEric.Schrock@Sun.COM else 4728525SEric.Schrock@Sun.COM return (0); 4735094Slling } 4745094Slling 4755094Slling /* 4765094Slling * If the bootfs property value is dsobj, clear it. 4775094Slling */ 4785094Slling void 4795094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 4805094Slling { 4815094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 4825094Slling VERIFY(zap_remove(spa->spa_meta_objset, 4835094Slling spa->spa_pool_props_object, 4845094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 4855094Slling spa->spa_bootfs = 0; 4865094Slling } 4875094Slling } 4885094Slling 489789Sahrens /* 490789Sahrens * ========================================================================== 491789Sahrens * SPA state manipulation (open/create/destroy/import/export) 492789Sahrens * ========================================================================== 493789Sahrens */ 494789Sahrens 4951544Seschrock static int 4961544Seschrock spa_error_entry_compare(const void *a, const void *b) 4971544Seschrock { 4981544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 4991544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 5001544Seschrock int ret; 5011544Seschrock 5021544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 5031544Seschrock sizeof (zbookmark_t)); 5041544Seschrock 5051544Seschrock if (ret < 0) 5061544Seschrock return (-1); 5071544Seschrock else if (ret > 0) 5081544Seschrock return (1); 5091544Seschrock else 5101544Seschrock return (0); 5111544Seschrock } 5121544Seschrock 5131544Seschrock /* 5141544Seschrock * Utility function which retrieves copies of the current logs and 5151544Seschrock * re-initializes them in the process. 5161544Seschrock */ 5171544Seschrock void 5181544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 5191544Seschrock { 5201544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 5211544Seschrock 5221544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 5231544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 5241544Seschrock 5251544Seschrock avl_create(&spa->spa_errlist_scrub, 5261544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5271544Seschrock offsetof(spa_error_entry_t, se_avl)); 5281544Seschrock avl_create(&spa->spa_errlist_last, 5291544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5301544Seschrock offsetof(spa_error_entry_t, se_avl)); 5311544Seschrock } 5321544Seschrock 533789Sahrens /* 534789Sahrens * Activate an uninitialized pool. 535789Sahrens */ 536789Sahrens static void 5378241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode) 538789Sahrens { 539789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 540789Sahrens 541789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 5428241SJeff.Bonwick@Sun.COM spa->spa_mode = mode; 543789Sahrens 544789Sahrens spa->spa_normal_class = metaslab_class_create(); 5454527Sperrin spa->spa_log_class = metaslab_class_create(); 546789Sahrens 5477754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 5487754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5497754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create("spa_zio", 5507754SJeff.Bonwick@Sun.COM zio_taskq_threads[t][q], maxclsyspri, 50, 5517754SJeff.Bonwick@Sun.COM INT_MAX, TASKQ_PREPOPULATE); 5527754SJeff.Bonwick@Sun.COM } 553789Sahrens } 554789Sahrens 5557754SJeff.Bonwick@Sun.COM list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 5567754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_config_dirty_node)); 5577754SJeff.Bonwick@Sun.COM list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 5587754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_state_dirty_node)); 559789Sahrens 560789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 561789Sahrens offsetof(struct vdev, vdev_txg_node)); 5621544Seschrock 5631544Seschrock avl_create(&spa->spa_errlist_scrub, 5641544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5651544Seschrock offsetof(spa_error_entry_t, se_avl)); 5661544Seschrock avl_create(&spa->spa_errlist_last, 5671544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5681544Seschrock offsetof(spa_error_entry_t, se_avl)); 569789Sahrens } 570789Sahrens 571789Sahrens /* 572789Sahrens * Opposite of spa_activate(). 573789Sahrens */ 574789Sahrens static void 575789Sahrens spa_deactivate(spa_t *spa) 576789Sahrens { 577789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 578789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 579789Sahrens ASSERT(spa->spa_root_vdev == NULL); 580789Sahrens 581789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 582789Sahrens 583789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 584789Sahrens 5857754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_config_dirty_list); 5867754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_state_dirty_list); 5877754SJeff.Bonwick@Sun.COM 5887754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 5897754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5907754SJeff.Bonwick@Sun.COM taskq_destroy(spa->spa_zio_taskq[t][q]); 5917754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 5927754SJeff.Bonwick@Sun.COM } 593789Sahrens } 594789Sahrens 595789Sahrens metaslab_class_destroy(spa->spa_normal_class); 596789Sahrens spa->spa_normal_class = NULL; 597789Sahrens 5984527Sperrin metaslab_class_destroy(spa->spa_log_class); 5994527Sperrin spa->spa_log_class = NULL; 6004527Sperrin 6011544Seschrock /* 6021544Seschrock * If this was part of an import or the open otherwise failed, we may 6031544Seschrock * still have errors left in the queues. Empty them just in case. 6041544Seschrock */ 6051544Seschrock spa_errlog_drain(spa); 6061544Seschrock 6071544Seschrock avl_destroy(&spa->spa_errlist_scrub); 6081544Seschrock avl_destroy(&spa->spa_errlist_last); 6091544Seschrock 610789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 611789Sahrens } 612789Sahrens 613789Sahrens /* 614789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 615789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 616789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 617789Sahrens * All vdev validation is done by the vdev_alloc() routine. 618789Sahrens */ 6192082Seschrock static int 6202082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 6212082Seschrock uint_t id, int atype) 622789Sahrens { 623789Sahrens nvlist_t **child; 624789Sahrens uint_t c, children; 6252082Seschrock int error; 6262082Seschrock 6272082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 6282082Seschrock return (error); 6292082Seschrock 6302082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 6312082Seschrock return (0); 632789Sahrens 6337754SJeff.Bonwick@Sun.COM error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 6347754SJeff.Bonwick@Sun.COM &child, &children); 6357754SJeff.Bonwick@Sun.COM 6367754SJeff.Bonwick@Sun.COM if (error == ENOENT) 6377754SJeff.Bonwick@Sun.COM return (0); 6387754SJeff.Bonwick@Sun.COM 6397754SJeff.Bonwick@Sun.COM if (error) { 6402082Seschrock vdev_free(*vdp); 6412082Seschrock *vdp = NULL; 6422082Seschrock return (EINVAL); 643789Sahrens } 644789Sahrens 645789Sahrens for (c = 0; c < children; c++) { 6462082Seschrock vdev_t *vd; 6472082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 6482082Seschrock atype)) != 0) { 6492082Seschrock vdev_free(*vdp); 6502082Seschrock *vdp = NULL; 6512082Seschrock return (error); 652789Sahrens } 653789Sahrens } 654789Sahrens 6552082Seschrock ASSERT(*vdp != NULL); 6562082Seschrock 6572082Seschrock return (0); 658789Sahrens } 659789Sahrens 660789Sahrens /* 661789Sahrens * Opposite of spa_load(). 662789Sahrens */ 663789Sahrens static void 664789Sahrens spa_unload(spa_t *spa) 665789Sahrens { 6662082Seschrock int i; 6672082Seschrock 6687754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6697754SJeff.Bonwick@Sun.COM 670789Sahrens /* 6711544Seschrock * Stop async tasks. 6721544Seschrock */ 6731544Seschrock spa_async_suspend(spa); 6741544Seschrock 6751544Seschrock /* 676789Sahrens * Stop syncing. 677789Sahrens */ 678789Sahrens if (spa->spa_sync_on) { 679789Sahrens txg_sync_stop(spa->spa_dsl_pool); 680789Sahrens spa->spa_sync_on = B_FALSE; 681789Sahrens } 682789Sahrens 683789Sahrens /* 6847754SJeff.Bonwick@Sun.COM * Wait for any outstanding async I/O to complete. 685789Sahrens */ 6869234SGeorge.Wilson@Sun.COM if (spa->spa_async_zio_root != NULL) { 6879234SGeorge.Wilson@Sun.COM (void) zio_wait(spa->spa_async_zio_root); 6889234SGeorge.Wilson@Sun.COM spa->spa_async_zio_root = NULL; 6899234SGeorge.Wilson@Sun.COM } 690789Sahrens 691789Sahrens /* 692789Sahrens * Close the dsl pool. 693789Sahrens */ 694789Sahrens if (spa->spa_dsl_pool) { 695789Sahrens dsl_pool_close(spa->spa_dsl_pool); 696789Sahrens spa->spa_dsl_pool = NULL; 697789Sahrens } 698789Sahrens 6998241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7008241SJeff.Bonwick@Sun.COM 7018241SJeff.Bonwick@Sun.COM /* 7028241SJeff.Bonwick@Sun.COM * Drop and purge level 2 cache 7038241SJeff.Bonwick@Sun.COM */ 7048241SJeff.Bonwick@Sun.COM spa_l2cache_drop(spa); 7058241SJeff.Bonwick@Sun.COM 706789Sahrens /* 707789Sahrens * Close all vdevs. 708789Sahrens */ 7091585Sbonwick if (spa->spa_root_vdev) 710789Sahrens vdev_free(spa->spa_root_vdev); 7111585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 7121544Seschrock 7135450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7145450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 7155450Sbrendan if (spa->spa_spares.sav_vdevs) { 7165450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7175450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7185450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7195450Sbrendan } 7205450Sbrendan if (spa->spa_spares.sav_config) { 7215450Sbrendan nvlist_free(spa->spa_spares.sav_config); 7225450Sbrendan spa->spa_spares.sav_config = NULL; 7232082Seschrock } 7247377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 7255450Sbrendan 7265450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 7275450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 7285450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 7295450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 7305450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 7315450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 7325450Sbrendan } 7335450Sbrendan if (spa->spa_l2cache.sav_config) { 7345450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 7355450Sbrendan spa->spa_l2cache.sav_config = NULL; 7362082Seschrock } 7377377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 7382082Seschrock 7391544Seschrock spa->spa_async_suspended = 0; 7408241SJeff.Bonwick@Sun.COM 7418241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 742789Sahrens } 743789Sahrens 744789Sahrens /* 7452082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 7462082Seschrock * this pool. When this is called, we have some form of basic information in 7475450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 7485450Sbrendan * then re-generate a more complete list including status information. 7492082Seschrock */ 7502082Seschrock static void 7512082Seschrock spa_load_spares(spa_t *spa) 7522082Seschrock { 7532082Seschrock nvlist_t **spares; 7542082Seschrock uint_t nspares; 7552082Seschrock int i; 7563377Seschrock vdev_t *vd, *tvd; 7572082Seschrock 7587754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 7597754SJeff.Bonwick@Sun.COM 7602082Seschrock /* 7612082Seschrock * First, close and free any existing spare vdevs. 7622082Seschrock */ 7635450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7645450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 7653377Seschrock 7663377Seschrock /* Undo the call to spa_activate() below */ 7676643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7686643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 7693377Seschrock spa_spare_remove(tvd); 7703377Seschrock vdev_close(vd); 7713377Seschrock vdev_free(vd); 7722082Seschrock } 7733377Seschrock 7745450Sbrendan if (spa->spa_spares.sav_vdevs) 7755450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7765450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7775450Sbrendan 7785450Sbrendan if (spa->spa_spares.sav_config == NULL) 7792082Seschrock nspares = 0; 7802082Seschrock else 7815450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 7822082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 7832082Seschrock 7845450Sbrendan spa->spa_spares.sav_count = (int)nspares; 7855450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7862082Seschrock 7872082Seschrock if (nspares == 0) 7882082Seschrock return; 7892082Seschrock 7902082Seschrock /* 7912082Seschrock * Construct the array of vdevs, opening them to get status in the 7923377Seschrock * process. For each spare, there is potentially two different vdev_t 7933377Seschrock * structures associated with it: one in the list of spares (used only 7943377Seschrock * for basic validation purposes) and one in the active vdev 7953377Seschrock * configuration (if it's spared in). During this phase we open and 7963377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 7973377Seschrock * active configuration, then we also mark this vdev as an active spare. 7982082Seschrock */ 7995450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 8005450Sbrendan KM_SLEEP); 8015450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 8022082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 8032082Seschrock VDEV_ALLOC_SPARE) == 0); 8042082Seschrock ASSERT(vd != NULL); 8052082Seschrock 8065450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 8072082Seschrock 8086643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 8096643Seschrock B_FALSE)) != NULL) { 8103377Seschrock if (!tvd->vdev_isspare) 8113377Seschrock spa_spare_add(tvd); 8123377Seschrock 8133377Seschrock /* 8143377Seschrock * We only mark the spare active if we were successfully 8153377Seschrock * able to load the vdev. Otherwise, importing a pool 8163377Seschrock * with a bad active spare would result in strange 8173377Seschrock * behavior, because multiple pool would think the spare 8183377Seschrock * is actively in use. 8193377Seschrock * 8203377Seschrock * There is a vulnerability here to an equally bizarre 8213377Seschrock * circumstance, where a dead active spare is later 8223377Seschrock * brought back to life (onlined or otherwise). Given 8233377Seschrock * the rarity of this scenario, and the extra complexity 8243377Seschrock * it adds, we ignore the possibility. 8253377Seschrock */ 8263377Seschrock if (!vdev_is_dead(tvd)) 8273377Seschrock spa_spare_activate(tvd); 8283377Seschrock } 8293377Seschrock 8307754SJeff.Bonwick@Sun.COM vd->vdev_top = vd; 8317754SJeff.Bonwick@Sun.COM 8322082Seschrock if (vdev_open(vd) != 0) 8332082Seschrock continue; 8342082Seschrock 8355450Sbrendan if (vdev_validate_aux(vd) == 0) 8365450Sbrendan spa_spare_add(vd); 8372082Seschrock } 8382082Seschrock 8392082Seschrock /* 8402082Seschrock * Recompute the stashed list of spares, with status information 8412082Seschrock * this time. 8422082Seschrock */ 8435450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 8442082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 8452082Seschrock 8465450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 8475450Sbrendan KM_SLEEP); 8485450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 8495450Sbrendan spares[i] = vdev_config_generate(spa, 8505450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 8515450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 8525450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 8535450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 8542082Seschrock nvlist_free(spares[i]); 8555450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 8565450Sbrendan } 8575450Sbrendan 8585450Sbrendan /* 8595450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 8605450Sbrendan * this pool. When this is called, we have some form of basic information in 8615450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 8625450Sbrendan * then re-generate a more complete list including status information. 8635450Sbrendan * Devices which are already active have their details maintained, and are 8645450Sbrendan * not re-opened. 8655450Sbrendan */ 8665450Sbrendan static void 8675450Sbrendan spa_load_l2cache(spa_t *spa) 8685450Sbrendan { 8695450Sbrendan nvlist_t **l2cache; 8705450Sbrendan uint_t nl2cache; 8715450Sbrendan int i, j, oldnvdevs; 8726643Seschrock uint64_t guid, size; 8735450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 8745450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 8755450Sbrendan 8767754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 8777754SJeff.Bonwick@Sun.COM 8785450Sbrendan if (sav->sav_config != NULL) { 8795450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 8805450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 8815450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 8825450Sbrendan } else { 8835450Sbrendan nl2cache = 0; 8845450Sbrendan } 8855450Sbrendan 8865450Sbrendan oldvdevs = sav->sav_vdevs; 8875450Sbrendan oldnvdevs = sav->sav_count; 8885450Sbrendan sav->sav_vdevs = NULL; 8895450Sbrendan sav->sav_count = 0; 8905450Sbrendan 8915450Sbrendan /* 8925450Sbrendan * Process new nvlist of vdevs. 8935450Sbrendan */ 8945450Sbrendan for (i = 0; i < nl2cache; i++) { 8955450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 8965450Sbrendan &guid) == 0); 8975450Sbrendan 8985450Sbrendan newvdevs[i] = NULL; 8995450Sbrendan for (j = 0; j < oldnvdevs; j++) { 9005450Sbrendan vd = oldvdevs[j]; 9015450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 9025450Sbrendan /* 9035450Sbrendan * Retain previous vdev for add/remove ops. 9045450Sbrendan */ 9055450Sbrendan newvdevs[i] = vd; 9065450Sbrendan oldvdevs[j] = NULL; 9075450Sbrendan break; 9085450Sbrendan } 9095450Sbrendan } 9105450Sbrendan 9115450Sbrendan if (newvdevs[i] == NULL) { 9125450Sbrendan /* 9135450Sbrendan * Create new vdev 9145450Sbrendan */ 9155450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 9165450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 9175450Sbrendan ASSERT(vd != NULL); 9185450Sbrendan newvdevs[i] = vd; 9195450Sbrendan 9205450Sbrendan /* 9215450Sbrendan * Commit this vdev as an l2cache device, 9225450Sbrendan * even if it fails to open. 9235450Sbrendan */ 9245450Sbrendan spa_l2cache_add(vd); 9255450Sbrendan 9266643Seschrock vd->vdev_top = vd; 9276643Seschrock vd->vdev_aux = sav; 9286643Seschrock 9296643Seschrock spa_l2cache_activate(vd); 9306643Seschrock 9315450Sbrendan if (vdev_open(vd) != 0) 9325450Sbrendan continue; 9335450Sbrendan 9345450Sbrendan (void) vdev_validate_aux(vd); 9355450Sbrendan 9365450Sbrendan if (!vdev_is_dead(vd)) { 9375450Sbrendan size = vdev_get_rsize(vd); 9386643Seschrock l2arc_add_vdev(spa, vd, 9396643Seschrock VDEV_LABEL_START_SIZE, 9406643Seschrock size - VDEV_LABEL_START_SIZE); 9415450Sbrendan } 9425450Sbrendan } 9435450Sbrendan } 9445450Sbrendan 9455450Sbrendan /* 9465450Sbrendan * Purge vdevs that were dropped 9475450Sbrendan */ 9485450Sbrendan for (i = 0; i < oldnvdevs; i++) { 9495450Sbrendan uint64_t pool; 9505450Sbrendan 9515450Sbrendan vd = oldvdevs[i]; 9525450Sbrendan if (vd != NULL) { 9538241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 9548241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 9555450Sbrendan l2arc_remove_vdev(vd); 9565450Sbrendan (void) vdev_close(vd); 9575450Sbrendan spa_l2cache_remove(vd); 9585450Sbrendan } 9595450Sbrendan } 9605450Sbrendan 9615450Sbrendan if (oldvdevs) 9625450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 9635450Sbrendan 9645450Sbrendan if (sav->sav_config == NULL) 9655450Sbrendan goto out; 9665450Sbrendan 9675450Sbrendan sav->sav_vdevs = newvdevs; 9685450Sbrendan sav->sav_count = (int)nl2cache; 9695450Sbrendan 9705450Sbrendan /* 9715450Sbrendan * Recompute the stashed list of l2cache devices, with status 9725450Sbrendan * information this time. 9735450Sbrendan */ 9745450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 9755450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 9765450Sbrendan 9775450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 9785450Sbrendan for (i = 0; i < sav->sav_count; i++) 9795450Sbrendan l2cache[i] = vdev_config_generate(spa, 9805450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 9815450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 9825450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 9835450Sbrendan out: 9845450Sbrendan for (i = 0; i < sav->sav_count; i++) 9855450Sbrendan nvlist_free(l2cache[i]); 9865450Sbrendan if (sav->sav_count) 9875450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 9882082Seschrock } 9892082Seschrock 9902082Seschrock static int 9912082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 9922082Seschrock { 9932082Seschrock dmu_buf_t *db; 9942082Seschrock char *packed = NULL; 9952082Seschrock size_t nvsize = 0; 9962082Seschrock int error; 9972082Seschrock *value = NULL; 9982082Seschrock 9992082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 10002082Seschrock nvsize = *(uint64_t *)db->db_data; 10012082Seschrock dmu_buf_rele(db, FTAG); 10022082Seschrock 10032082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 10042082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 10052082Seschrock if (error == 0) 10062082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 10072082Seschrock kmem_free(packed, nvsize); 10082082Seschrock 10092082Seschrock return (error); 10102082Seschrock } 10112082Seschrock 10122082Seschrock /* 10134451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 10144451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 10154451Seschrock */ 10164451Seschrock static void 10174451Seschrock spa_check_removed(vdev_t *vd) 10184451Seschrock { 10194451Seschrock int c; 10204451Seschrock 10214451Seschrock for (c = 0; c < vd->vdev_children; c++) 10224451Seschrock spa_check_removed(vd->vdev_child[c]); 10234451Seschrock 10244451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 10254451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 10264451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 10274451Seschrock } 10284451Seschrock } 10294451Seschrock 10304451Seschrock /* 10317294Sperrin * Check for missing log devices 10327294Sperrin */ 10337294Sperrin int 10347294Sperrin spa_check_logs(spa_t *spa) 10357294Sperrin { 10367294Sperrin switch (spa->spa_log_state) { 10377294Sperrin case SPA_LOG_MISSING: 10387294Sperrin /* need to recheck in case slog has been restored */ 10397294Sperrin case SPA_LOG_UNKNOWN: 10407294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 10417294Sperrin DS_FIND_CHILDREN)) { 10427294Sperrin spa->spa_log_state = SPA_LOG_MISSING; 10437294Sperrin return (1); 10447294Sperrin } 10457294Sperrin break; 10467294Sperrin 10477294Sperrin case SPA_LOG_CLEAR: 10487294Sperrin (void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL, 10497294Sperrin DS_FIND_CHILDREN); 10507294Sperrin break; 10517294Sperrin } 10527294Sperrin spa->spa_log_state = SPA_LOG_GOOD; 10537294Sperrin return (0); 10547294Sperrin } 10557294Sperrin 10567294Sperrin /* 1057789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 10581544Seschrock * source of configuration information. 1059789Sahrens */ 1060789Sahrens static int 10611544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 1062789Sahrens { 1063789Sahrens int error = 0; 1064789Sahrens nvlist_t *nvroot = NULL; 1065789Sahrens vdev_t *rvd; 1066789Sahrens uberblock_t *ub = &spa->spa_uberblock; 10671635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 1068789Sahrens uint64_t pool_guid; 10692082Seschrock uint64_t version; 10704451Seschrock uint64_t autoreplace = 0; 10718241SJeff.Bonwick@Sun.COM int orig_mode = spa->spa_mode; 10727294Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 1073789Sahrens 10748241SJeff.Bonwick@Sun.COM /* 10758241SJeff.Bonwick@Sun.COM * If this is an untrusted config, access the pool in read-only mode. 10768241SJeff.Bonwick@Sun.COM * This prevents things like resilvering recently removed devices. 10778241SJeff.Bonwick@Sun.COM */ 10788241SJeff.Bonwick@Sun.COM if (!mosconfig) 10798241SJeff.Bonwick@Sun.COM spa->spa_mode = FREAD; 10808241SJeff.Bonwick@Sun.COM 10817754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 10827754SJeff.Bonwick@Sun.COM 10831544Seschrock spa->spa_load_state = state; 10841635Sbonwick 1085789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 10861733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 10871544Seschrock error = EINVAL; 10881544Seschrock goto out; 10891544Seschrock } 1090789Sahrens 10912082Seschrock /* 10922082Seschrock * Versioning wasn't explicitly added to the label until later, so if 10932082Seschrock * it's not present treat it as the initial version. 10942082Seschrock */ 10952082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 10964577Sahrens version = SPA_VERSION_INITIAL; 10972082Seschrock 10981733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 10991733Sbonwick &spa->spa_config_txg); 11001733Sbonwick 11011635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 11021544Seschrock spa_guid_exists(pool_guid, 0)) { 11031544Seschrock error = EEXIST; 11041544Seschrock goto out; 11051544Seschrock } 1106789Sahrens 11072174Seschrock spa->spa_load_guid = pool_guid; 11082174Seschrock 1109789Sahrens /* 11109234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 11119234SGeorge.Wilson@Sun.COM */ 11129234SGeorge.Wilson@Sun.COM if (spa->spa_async_zio_root == NULL) 11139234SGeorge.Wilson@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 11149234SGeorge.Wilson@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 11159234SGeorge.Wilson@Sun.COM ZIO_FLAG_GODFATHER); 11169234SGeorge.Wilson@Sun.COM 11179234SGeorge.Wilson@Sun.COM /* 11182082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 11192082Seschrock * value that will be returned by spa_version() since parsing the 11202082Seschrock * configuration requires knowing the version number. 1121789Sahrens */ 11227754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 11232082Seschrock spa->spa_ubsync.ub_version = version; 11242082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 11257754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1126789Sahrens 11272082Seschrock if (error != 0) 11281544Seschrock goto out; 1129789Sahrens 11301585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1131789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1132789Sahrens 1133789Sahrens /* 1134789Sahrens * Try to open all vdevs, loading each label in the process. 1135789Sahrens */ 11367754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 11374070Smc142369 error = vdev_open(rvd); 11387754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 11394070Smc142369 if (error != 0) 11401544Seschrock goto out; 1141789Sahrens 1142789Sahrens /* 1143*9276SMark.Musante@Sun.COM * We need to validate the vdev labels against the configuration that 1144*9276SMark.Musante@Sun.COM * we have in hand, which is dependent on the setting of mosconfig. If 1145*9276SMark.Musante@Sun.COM * mosconfig is true then we're validating the vdev labels based on 1146*9276SMark.Musante@Sun.COM * that config. Otherwise, we're validating against the cached config 1147*9276SMark.Musante@Sun.COM * (zpool.cache) that was read when we loaded the zfs module, and then 1148*9276SMark.Musante@Sun.COM * later we will recursively call spa_load() and validate against 1149*9276SMark.Musante@Sun.COM * the vdev config. 11501986Seschrock */ 1151*9276SMark.Musante@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1152*9276SMark.Musante@Sun.COM error = vdev_validate(rvd); 1153*9276SMark.Musante@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1154*9276SMark.Musante@Sun.COM if (error != 0) 1155*9276SMark.Musante@Sun.COM goto out; 11561986Seschrock 11571986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 11581986Seschrock error = ENXIO; 11591986Seschrock goto out; 11601986Seschrock } 11611986Seschrock 11621986Seschrock /* 1163789Sahrens * Find the best uberblock. 1164789Sahrens */ 11657754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1166789Sahrens 1167789Sahrens /* 1168789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1169789Sahrens */ 1170789Sahrens if (ub->ub_txg == 0) { 11711760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11721760Seschrock VDEV_AUX_CORRUPT_DATA); 11731544Seschrock error = ENXIO; 11741544Seschrock goto out; 11751544Seschrock } 11761544Seschrock 11771544Seschrock /* 11781544Seschrock * If the pool is newer than the code, we can't open it. 11791544Seschrock */ 11804577Sahrens if (ub->ub_version > SPA_VERSION) { 11811760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11821760Seschrock VDEV_AUX_VERSION_NEWER); 11831544Seschrock error = ENOTSUP; 11841544Seschrock goto out; 1185789Sahrens } 1186789Sahrens 1187789Sahrens /* 1188789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1189789Sahrens * incomplete configuration. 1190789Sahrens */ 11911732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 11921544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11931544Seschrock VDEV_AUX_BAD_GUID_SUM); 11941544Seschrock error = ENXIO; 11951544Seschrock goto out; 1196789Sahrens } 1197789Sahrens 1198789Sahrens /* 1199789Sahrens * Initialize internal SPA structures. 1200789Sahrens */ 1201789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1202789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1203789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 12041544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 12051544Seschrock if (error) { 12061544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12071544Seschrock VDEV_AUX_CORRUPT_DATA); 12081544Seschrock goto out; 12091544Seschrock } 1210789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1211789Sahrens 12121544Seschrock if (zap_lookup(spa->spa_meta_objset, 1213789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 12141544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 12151544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12161544Seschrock VDEV_AUX_CORRUPT_DATA); 12171544Seschrock error = EIO; 12181544Seschrock goto out; 12191544Seschrock } 1220789Sahrens 1221789Sahrens if (!mosconfig) { 12222082Seschrock nvlist_t *newconfig; 12233975Sek110237 uint64_t hostid; 12242082Seschrock 12252082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 12261544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12271544Seschrock VDEV_AUX_CORRUPT_DATA); 12281544Seschrock error = EIO; 12291544Seschrock goto out; 12301544Seschrock } 1231789Sahrens 12327706SLin.Ling@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig, 12337706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 12343975Sek110237 char *hostname; 12353975Sek110237 unsigned long myhostid = 0; 12363975Sek110237 12373975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 12383975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 12393975Sek110237 12408662SJordan.Vaughan@Sun.com #ifdef _KERNEL 12418662SJordan.Vaughan@Sun.com myhostid = zone_get_hostid(NULL); 12428662SJordan.Vaughan@Sun.com #else /* _KERNEL */ 12438662SJordan.Vaughan@Sun.com /* 12448662SJordan.Vaughan@Sun.com * We're emulating the system's hostid in userland, so 12458662SJordan.Vaughan@Sun.com * we can't use zone_get_hostid(). 12468662SJordan.Vaughan@Sun.com */ 12473975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 12488662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 12494178Slling if (hostid != 0 && myhostid != 0 && 12508662SJordan.Vaughan@Sun.com hostid != myhostid) { 12513975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 12523975Sek110237 "loaded as it was last accessed by " 12537706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 12543975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 12557754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 12563975Sek110237 (unsigned long)hostid); 12573975Sek110237 error = EBADF; 12583975Sek110237 goto out; 12593975Sek110237 } 12603975Sek110237 } 12613975Sek110237 1262789Sahrens spa_config_set(spa, newconfig); 1263789Sahrens spa_unload(spa); 1264789Sahrens spa_deactivate(spa); 12658241SJeff.Bonwick@Sun.COM spa_activate(spa, orig_mode); 1266789Sahrens 12671544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 12681544Seschrock } 12691544Seschrock 12701544Seschrock if (zap_lookup(spa->spa_meta_objset, 12711544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 12721544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 12731544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12741544Seschrock VDEV_AUX_CORRUPT_DATA); 12751544Seschrock error = EIO; 12761544Seschrock goto out; 1277789Sahrens } 1278789Sahrens 12791544Seschrock /* 12802082Seschrock * Load the bit that tells us to use the new accounting function 12812082Seschrock * (raid-z deflation). If we have an older pool, this will not 12822082Seschrock * be present. 12832082Seschrock */ 12842082Seschrock error = zap_lookup(spa->spa_meta_objset, 12852082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 12862082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 12872082Seschrock if (error != 0 && error != ENOENT) { 12882082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12892082Seschrock VDEV_AUX_CORRUPT_DATA); 12902082Seschrock error = EIO; 12912082Seschrock goto out; 12922082Seschrock } 12932082Seschrock 12942082Seschrock /* 12951544Seschrock * Load the persistent error log. If we have an older pool, this will 12961544Seschrock * not be present. 12971544Seschrock */ 12981544Seschrock error = zap_lookup(spa->spa_meta_objset, 12991544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 13001544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 13011807Sbonwick if (error != 0 && error != ENOENT) { 13021544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13031544Seschrock VDEV_AUX_CORRUPT_DATA); 13041544Seschrock error = EIO; 13051544Seschrock goto out; 13061544Seschrock } 13071544Seschrock 13081544Seschrock error = zap_lookup(spa->spa_meta_objset, 13091544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 13101544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 13111544Seschrock if (error != 0 && error != ENOENT) { 13121544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13131544Seschrock VDEV_AUX_CORRUPT_DATA); 13141544Seschrock error = EIO; 13151544Seschrock goto out; 13161544Seschrock } 1317789Sahrens 1318789Sahrens /* 13192926Sek110237 * Load the history object. If we have an older pool, this 13202926Sek110237 * will not be present. 13212926Sek110237 */ 13222926Sek110237 error = zap_lookup(spa->spa_meta_objset, 13232926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 13242926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 13252926Sek110237 if (error != 0 && error != ENOENT) { 13262926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13272926Sek110237 VDEV_AUX_CORRUPT_DATA); 13282926Sek110237 error = EIO; 13292926Sek110237 goto out; 13302926Sek110237 } 13312926Sek110237 13322926Sek110237 /* 13332082Seschrock * Load any hot spares for this pool. 13342082Seschrock */ 13352082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13365450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 13372082Seschrock if (error != 0 && error != ENOENT) { 13382082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13392082Seschrock VDEV_AUX_CORRUPT_DATA); 13402082Seschrock error = EIO; 13412082Seschrock goto out; 13422082Seschrock } 13432082Seschrock if (error == 0) { 13444577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 13455450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 13465450Sbrendan &spa->spa_spares.sav_config) != 0) { 13472082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13482082Seschrock VDEV_AUX_CORRUPT_DATA); 13492082Seschrock error = EIO; 13502082Seschrock goto out; 13512082Seschrock } 13522082Seschrock 13537754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13542082Seschrock spa_load_spares(spa); 13557754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13562082Seschrock } 13572082Seschrock 13585450Sbrendan /* 13595450Sbrendan * Load any level 2 ARC devices for this pool. 13605450Sbrendan */ 13615450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13625450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 13635450Sbrendan &spa->spa_l2cache.sav_object); 13645450Sbrendan if (error != 0 && error != ENOENT) { 13655450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13665450Sbrendan VDEV_AUX_CORRUPT_DATA); 13675450Sbrendan error = EIO; 13685450Sbrendan goto out; 13695450Sbrendan } 13705450Sbrendan if (error == 0) { 13715450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 13725450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 13735450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 13745450Sbrendan vdev_set_state(rvd, B_TRUE, 13755450Sbrendan VDEV_STATE_CANT_OPEN, 13765450Sbrendan VDEV_AUX_CORRUPT_DATA); 13775450Sbrendan error = EIO; 13785450Sbrendan goto out; 13795450Sbrendan } 13805450Sbrendan 13817754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13825450Sbrendan spa_load_l2cache(spa); 13837754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13845450Sbrendan } 13855450Sbrendan 13867294Sperrin if (spa_check_logs(spa)) { 13877294Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13887294Sperrin VDEV_AUX_BAD_LOG); 13897294Sperrin error = ENXIO; 13907294Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 13917294Sperrin goto out; 13927294Sperrin } 13937294Sperrin 13947294Sperrin 13955094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 13964543Smarks 13973912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13983912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 13993912Slling 14003912Slling if (error && error != ENOENT) { 14013912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14023912Slling VDEV_AUX_CORRUPT_DATA); 14033912Slling error = EIO; 14043912Slling goto out; 14053912Slling } 14063912Slling 14073912Slling if (error == 0) { 14083912Slling (void) zap_lookup(spa->spa_meta_objset, 14093912Slling spa->spa_pool_props_object, 14104451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 14113912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 14124451Seschrock (void) zap_lookup(spa->spa_meta_objset, 14134451Seschrock spa->spa_pool_props_object, 14144451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 14154451Seschrock sizeof (uint64_t), 1, &autoreplace); 14164543Smarks (void) zap_lookup(spa->spa_meta_objset, 14174543Smarks spa->spa_pool_props_object, 14184543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 14194543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 14205329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 14215329Sgw25295 spa->spa_pool_props_object, 14225329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 14235329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 14243912Slling } 14253912Slling 14262082Seschrock /* 14274451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 14284451Seschrock * the ZFS DE that it should not issue any faults for unopenable 14294451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 14304451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 14314451Seschrock * over. 14324451Seschrock */ 14335756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 14344451Seschrock spa_check_removed(spa->spa_root_vdev); 14354451Seschrock 14364451Seschrock /* 14371986Seschrock * Load the vdev state for all toplevel vdevs. 1438789Sahrens */ 14391986Seschrock vdev_load(rvd); 1440789Sahrens 1441789Sahrens /* 1442789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1443789Sahrens */ 14447754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1445789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 14467754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1447789Sahrens 1448789Sahrens /* 1449789Sahrens * Check the state of the root vdev. If it can't be opened, it 1450789Sahrens * indicates one or more toplevel vdevs are faulted. 1451789Sahrens */ 14521544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 14531544Seschrock error = ENXIO; 14541544Seschrock goto out; 14551544Seschrock } 1456789Sahrens 14578241SJeff.Bonwick@Sun.COM if (spa_writeable(spa)) { 14581635Sbonwick dmu_tx_t *tx; 14591635Sbonwick int need_update = B_FALSE; 14608241SJeff.Bonwick@Sun.COM 14618241SJeff.Bonwick@Sun.COM ASSERT(state != SPA_LOAD_TRYIMPORT); 14621601Sbonwick 14631635Sbonwick /* 14641635Sbonwick * Claim log blocks that haven't been committed yet. 14651635Sbonwick * This must all happen in a single txg. 14661635Sbonwick */ 14671601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1468789Sahrens spa_first_txg(spa)); 14697754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 14702417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1471789Sahrens dmu_tx_commit(tx); 1472789Sahrens 1473789Sahrens spa->spa_sync_on = B_TRUE; 1474789Sahrens txg_sync_start(spa->spa_dsl_pool); 1475789Sahrens 1476789Sahrens /* 1477789Sahrens * Wait for all claims to sync. 1478789Sahrens */ 1479789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 14801585Sbonwick 14811585Sbonwick /* 14821635Sbonwick * If the config cache is stale, or we have uninitialized 14831635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 14841585Sbonwick */ 14851635Sbonwick if (config_cache_txg != spa->spa_config_txg || 14861635Sbonwick state == SPA_LOAD_IMPORT) 14871635Sbonwick need_update = B_TRUE; 14881635Sbonwick 14898241SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) 14901635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 14911635Sbonwick need_update = B_TRUE; 14921585Sbonwick 14931585Sbonwick /* 14941635Sbonwick * Update the config cache asychronously in case we're the 14951635Sbonwick * root pool, in which case the config cache isn't writable yet. 14961585Sbonwick */ 14971635Sbonwick if (need_update) 14981635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 14998241SJeff.Bonwick@Sun.COM 15008241SJeff.Bonwick@Sun.COM /* 15018241SJeff.Bonwick@Sun.COM * Check all DTLs to see if anything needs resilvering. 15028241SJeff.Bonwick@Sun.COM */ 15038241SJeff.Bonwick@Sun.COM if (vdev_resilver_needed(rvd, NULL, NULL)) 15048241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 1505789Sahrens } 1506789Sahrens 15071544Seschrock error = 0; 15081544Seschrock out: 15097046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 15102082Seschrock if (error && error != EBADF) 15117294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 15121544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 15131544Seschrock spa->spa_ena = 0; 15141544Seschrock 15151544Seschrock return (error); 1516789Sahrens } 1517789Sahrens 1518789Sahrens /* 1519789Sahrens * Pool Open/Import 1520789Sahrens * 1521789Sahrens * The import case is identical to an open except that the configuration is sent 1522789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1523789Sahrens * case of an open, the pool configuration will exist in the 15244451Seschrock * POOL_STATE_UNINITIALIZED state. 1525789Sahrens * 1526789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1527789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1528789Sahrens * ambiguous state. 1529789Sahrens */ 1530789Sahrens static int 1531789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1532789Sahrens { 1533789Sahrens spa_t *spa; 1534789Sahrens int error; 1535789Sahrens int locked = B_FALSE; 1536789Sahrens 1537789Sahrens *spapp = NULL; 1538789Sahrens 1539789Sahrens /* 1540789Sahrens * As disgusting as this is, we need to support recursive calls to this 1541789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1542789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1543789Sahrens * avoid dsl_dir_open() calling this in the first place. 1544789Sahrens */ 1545789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1546789Sahrens mutex_enter(&spa_namespace_lock); 1547789Sahrens locked = B_TRUE; 1548789Sahrens } 1549789Sahrens 1550789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1551789Sahrens if (locked) 1552789Sahrens mutex_exit(&spa_namespace_lock); 1553789Sahrens return (ENOENT); 1554789Sahrens } 1555789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1556789Sahrens 15578241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1558789Sahrens 15591635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1560789Sahrens 1561789Sahrens if (error == EBADF) { 1562789Sahrens /* 15631986Seschrock * If vdev_validate() returns failure (indicated by 15641986Seschrock * EBADF), it indicates that one of the vdevs indicates 15651986Seschrock * that the pool has been exported or destroyed. If 15661986Seschrock * this is the case, the config cache is out of sync and 15671986Seschrock * we should remove the pool from the namespace. 1568789Sahrens */ 1569789Sahrens spa_unload(spa); 1570789Sahrens spa_deactivate(spa); 15716643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1572789Sahrens spa_remove(spa); 1573789Sahrens if (locked) 1574789Sahrens mutex_exit(&spa_namespace_lock); 1575789Sahrens return (ENOENT); 15761544Seschrock } 15771544Seschrock 15781544Seschrock if (error) { 1579789Sahrens /* 1580789Sahrens * We can't open the pool, but we still have useful 1581789Sahrens * information: the state of each vdev after the 1582789Sahrens * attempted vdev_open(). Return this to the user. 1583789Sahrens */ 15847754SJeff.Bonwick@Sun.COM if (config != NULL && spa->spa_root_vdev != NULL) 1585789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1586789Sahrens B_TRUE); 1587789Sahrens spa_unload(spa); 1588789Sahrens spa_deactivate(spa); 15891544Seschrock spa->spa_last_open_failed = B_TRUE; 1590789Sahrens if (locked) 1591789Sahrens mutex_exit(&spa_namespace_lock); 1592789Sahrens *spapp = NULL; 1593789Sahrens return (error); 15941544Seschrock } else { 15951544Seschrock spa->spa_last_open_failed = B_FALSE; 1596789Sahrens } 1597789Sahrens } 1598789Sahrens 1599789Sahrens spa_open_ref(spa, tag); 16004451Seschrock 1601789Sahrens if (locked) 1602789Sahrens mutex_exit(&spa_namespace_lock); 1603789Sahrens 1604789Sahrens *spapp = spa; 1605789Sahrens 16067754SJeff.Bonwick@Sun.COM if (config != NULL) 1607789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1608789Sahrens 1609789Sahrens return (0); 1610789Sahrens } 1611789Sahrens 1612789Sahrens int 1613789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1614789Sahrens { 1615789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1616789Sahrens } 1617789Sahrens 16181544Seschrock /* 16191544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 16201544Seschrock * preventing it from being exported or destroyed. 16211544Seschrock */ 16221544Seschrock spa_t * 16231544Seschrock spa_inject_addref(char *name) 16241544Seschrock { 16251544Seschrock spa_t *spa; 16261544Seschrock 16271544Seschrock mutex_enter(&spa_namespace_lock); 16281544Seschrock if ((spa = spa_lookup(name)) == NULL) { 16291544Seschrock mutex_exit(&spa_namespace_lock); 16301544Seschrock return (NULL); 16311544Seschrock } 16321544Seschrock spa->spa_inject_ref++; 16331544Seschrock mutex_exit(&spa_namespace_lock); 16341544Seschrock 16351544Seschrock return (spa); 16361544Seschrock } 16371544Seschrock 16381544Seschrock void 16391544Seschrock spa_inject_delref(spa_t *spa) 16401544Seschrock { 16411544Seschrock mutex_enter(&spa_namespace_lock); 16421544Seschrock spa->spa_inject_ref--; 16431544Seschrock mutex_exit(&spa_namespace_lock); 16441544Seschrock } 16451544Seschrock 16465450Sbrendan /* 16475450Sbrendan * Add spares device information to the nvlist. 16485450Sbrendan */ 16492082Seschrock static void 16502082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 16512082Seschrock { 16522082Seschrock nvlist_t **spares; 16532082Seschrock uint_t i, nspares; 16542082Seschrock nvlist_t *nvroot; 16552082Seschrock uint64_t guid; 16562082Seschrock vdev_stat_t *vs; 16572082Seschrock uint_t vsc; 16583377Seschrock uint64_t pool; 16592082Seschrock 16605450Sbrendan if (spa->spa_spares.sav_count == 0) 16612082Seschrock return; 16622082Seschrock 16632082Seschrock VERIFY(nvlist_lookup_nvlist(config, 16642082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 16655450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 16662082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 16672082Seschrock if (nspares != 0) { 16682082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 16692082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16702082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 16712082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 16722082Seschrock 16732082Seschrock /* 16742082Seschrock * Go through and find any spares which have since been 16752082Seschrock * repurposed as an active spare. If this is the case, update 16762082Seschrock * their status appropriately. 16772082Seschrock */ 16782082Seschrock for (i = 0; i < nspares; i++) { 16792082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 16802082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 16817214Slling if (spa_spare_exists(guid, &pool, NULL) && 16827214Slling pool != 0ULL) { 16832082Seschrock VERIFY(nvlist_lookup_uint64_array( 16842082Seschrock spares[i], ZPOOL_CONFIG_STATS, 16852082Seschrock (uint64_t **)&vs, &vsc) == 0); 16862082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 16872082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 16882082Seschrock } 16892082Seschrock } 16902082Seschrock } 16912082Seschrock } 16922082Seschrock 16935450Sbrendan /* 16945450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 16955450Sbrendan */ 16965450Sbrendan static void 16975450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 16985450Sbrendan { 16995450Sbrendan nvlist_t **l2cache; 17005450Sbrendan uint_t i, j, nl2cache; 17015450Sbrendan nvlist_t *nvroot; 17025450Sbrendan uint64_t guid; 17035450Sbrendan vdev_t *vd; 17045450Sbrendan vdev_stat_t *vs; 17055450Sbrendan uint_t vsc; 17065450Sbrendan 17075450Sbrendan if (spa->spa_l2cache.sav_count == 0) 17085450Sbrendan return; 17095450Sbrendan 17107754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 17115450Sbrendan 17125450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 17135450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 17145450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 17155450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 17165450Sbrendan if (nl2cache != 0) { 17175450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 17185450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 17195450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 17205450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 17215450Sbrendan 17225450Sbrendan /* 17235450Sbrendan * Update level 2 cache device stats. 17245450Sbrendan */ 17255450Sbrendan 17265450Sbrendan for (i = 0; i < nl2cache; i++) { 17275450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 17285450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 17295450Sbrendan 17305450Sbrendan vd = NULL; 17315450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 17325450Sbrendan if (guid == 17335450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 17345450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 17355450Sbrendan break; 17365450Sbrendan } 17375450Sbrendan } 17385450Sbrendan ASSERT(vd != NULL); 17395450Sbrendan 17405450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 17415450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 17425450Sbrendan vdev_get_stats(vd, vs); 17435450Sbrendan } 17445450Sbrendan } 17455450Sbrendan 17467754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 17475450Sbrendan } 17485450Sbrendan 1749789Sahrens int 17501544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1751789Sahrens { 1752789Sahrens int error; 1753789Sahrens spa_t *spa; 1754789Sahrens 1755789Sahrens *config = NULL; 1756789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1757789Sahrens 17582082Seschrock if (spa && *config != NULL) { 17591544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 17601544Seschrock spa_get_errlog_size(spa)) == 0); 17611544Seschrock 17627754SJeff.Bonwick@Sun.COM if (spa_suspended(spa)) 17637754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 17647754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0); 17657754SJeff.Bonwick@Sun.COM 17662082Seschrock spa_add_spares(spa, *config); 17675450Sbrendan spa_add_l2cache(spa, *config); 17682082Seschrock } 17692082Seschrock 17701544Seschrock /* 17711544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 17721544Seschrock * and call spa_lookup() directly. 17731544Seschrock */ 17741544Seschrock if (altroot) { 17751544Seschrock if (spa == NULL) { 17761544Seschrock mutex_enter(&spa_namespace_lock); 17771544Seschrock spa = spa_lookup(name); 17781544Seschrock if (spa) 17791544Seschrock spa_altroot(spa, altroot, buflen); 17801544Seschrock else 17811544Seschrock altroot[0] = '\0'; 17821544Seschrock spa = NULL; 17831544Seschrock mutex_exit(&spa_namespace_lock); 17841544Seschrock } else { 17851544Seschrock spa_altroot(spa, altroot, buflen); 17861544Seschrock } 17871544Seschrock } 17881544Seschrock 1789789Sahrens if (spa != NULL) 1790789Sahrens spa_close(spa, FTAG); 1791789Sahrens 1792789Sahrens return (error); 1793789Sahrens } 1794789Sahrens 1795789Sahrens /* 17965450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 17975450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 17985450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 17995450Sbrendan * specified, as long as they are well-formed. 18002082Seschrock */ 18012082Seschrock static int 18025450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 18035450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 18045450Sbrendan vdev_labeltype_t label) 18052082Seschrock { 18065450Sbrendan nvlist_t **dev; 18075450Sbrendan uint_t i, ndev; 18082082Seschrock vdev_t *vd; 18092082Seschrock int error; 18102082Seschrock 18117754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 18127754SJeff.Bonwick@Sun.COM 18132082Seschrock /* 18145450Sbrendan * It's acceptable to have no devs specified. 18152082Seschrock */ 18165450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 18172082Seschrock return (0); 18182082Seschrock 18195450Sbrendan if (ndev == 0) 18202082Seschrock return (EINVAL); 18212082Seschrock 18222082Seschrock /* 18235450Sbrendan * Make sure the pool is formatted with a version that supports this 18245450Sbrendan * device type. 18252082Seschrock */ 18265450Sbrendan if (spa_version(spa) < version) 18272082Seschrock return (ENOTSUP); 18282082Seschrock 18293377Seschrock /* 18305450Sbrendan * Set the pending device list so we correctly handle device in-use 18313377Seschrock * checking. 18323377Seschrock */ 18335450Sbrendan sav->sav_pending = dev; 18345450Sbrendan sav->sav_npending = ndev; 18355450Sbrendan 18365450Sbrendan for (i = 0; i < ndev; i++) { 18375450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 18382082Seschrock mode)) != 0) 18393377Seschrock goto out; 18402082Seschrock 18412082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 18422082Seschrock vdev_free(vd); 18433377Seschrock error = EINVAL; 18443377Seschrock goto out; 18452082Seschrock } 18462082Seschrock 18475450Sbrendan /* 18487754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 18497754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 18505450Sbrendan */ 18517754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 18525450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 18535450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 18545450Sbrendan error = ENOTBLK; 18555450Sbrendan goto out; 18565450Sbrendan } 18577754SJeff.Bonwick@Sun.COM #endif 18582082Seschrock vd->vdev_top = vd; 18593377Seschrock 18603377Seschrock if ((error = vdev_open(vd)) == 0 && 18615450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 18625450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 18633377Seschrock vd->vdev_guid) == 0); 18642082Seschrock } 18652082Seschrock 18662082Seschrock vdev_free(vd); 18673377Seschrock 18685450Sbrendan if (error && 18695450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 18703377Seschrock goto out; 18713377Seschrock else 18723377Seschrock error = 0; 18732082Seschrock } 18742082Seschrock 18753377Seschrock out: 18765450Sbrendan sav->sav_pending = NULL; 18775450Sbrendan sav->sav_npending = 0; 18783377Seschrock return (error); 18792082Seschrock } 18802082Seschrock 18815450Sbrendan static int 18825450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 18835450Sbrendan { 18845450Sbrendan int error; 18855450Sbrendan 18867754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 18877754SJeff.Bonwick@Sun.COM 18885450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 18895450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 18905450Sbrendan VDEV_LABEL_SPARE)) != 0) { 18915450Sbrendan return (error); 18925450Sbrendan } 18935450Sbrendan 18945450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 18955450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 18965450Sbrendan VDEV_LABEL_L2CACHE)); 18975450Sbrendan } 18985450Sbrendan 18995450Sbrendan static void 19005450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 19015450Sbrendan const char *config) 19025450Sbrendan { 19035450Sbrendan int i; 19045450Sbrendan 19055450Sbrendan if (sav->sav_config != NULL) { 19065450Sbrendan nvlist_t **olddevs; 19075450Sbrendan uint_t oldndevs; 19085450Sbrendan nvlist_t **newdevs; 19095450Sbrendan 19105450Sbrendan /* 19115450Sbrendan * Generate new dev list by concatentating with the 19125450Sbrendan * current dev list. 19135450Sbrendan */ 19145450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 19155450Sbrendan &olddevs, &oldndevs) == 0); 19165450Sbrendan 19175450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 19185450Sbrendan (ndevs + oldndevs), KM_SLEEP); 19195450Sbrendan for (i = 0; i < oldndevs; i++) 19205450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 19215450Sbrendan KM_SLEEP) == 0); 19225450Sbrendan for (i = 0; i < ndevs; i++) 19235450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 19245450Sbrendan KM_SLEEP) == 0); 19255450Sbrendan 19265450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 19275450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 19285450Sbrendan 19295450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 19305450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 19315450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 19325450Sbrendan nvlist_free(newdevs[i]); 19335450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 19345450Sbrendan } else { 19355450Sbrendan /* 19365450Sbrendan * Generate a new dev list. 19375450Sbrendan */ 19385450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 19395450Sbrendan KM_SLEEP) == 0); 19405450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 19415450Sbrendan devs, ndevs) == 0); 19425450Sbrendan } 19435450Sbrendan } 19445450Sbrendan 19455450Sbrendan /* 19465450Sbrendan * Stop and drop level 2 ARC devices 19475450Sbrendan */ 19485450Sbrendan void 19495450Sbrendan spa_l2cache_drop(spa_t *spa) 19505450Sbrendan { 19515450Sbrendan vdev_t *vd; 19525450Sbrendan int i; 19535450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 19545450Sbrendan 19555450Sbrendan for (i = 0; i < sav->sav_count; i++) { 19565450Sbrendan uint64_t pool; 19575450Sbrendan 19585450Sbrendan vd = sav->sav_vdevs[i]; 19595450Sbrendan ASSERT(vd != NULL); 19605450Sbrendan 19618241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 19628241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 19635450Sbrendan l2arc_remove_vdev(vd); 19645450Sbrendan if (vd->vdev_isl2cache) 19655450Sbrendan spa_l2cache_remove(vd); 19665450Sbrendan vdev_clear_stats(vd); 19675450Sbrendan (void) vdev_close(vd); 19685450Sbrendan } 19695450Sbrendan } 19705450Sbrendan 19712082Seschrock /* 1972789Sahrens * Pool Creation 1973789Sahrens */ 1974789Sahrens int 19755094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 19767184Stimh const char *history_str, nvlist_t *zplprops) 1977789Sahrens { 1978789Sahrens spa_t *spa; 19795094Slling char *altroot = NULL; 19801635Sbonwick vdev_t *rvd; 1981789Sahrens dsl_pool_t *dp; 1982789Sahrens dmu_tx_t *tx; 19832082Seschrock int c, error = 0; 1984789Sahrens uint64_t txg = TXG_INITIAL; 19855450Sbrendan nvlist_t **spares, **l2cache; 19865450Sbrendan uint_t nspares, nl2cache; 19875094Slling uint64_t version; 1988789Sahrens 1989789Sahrens /* 1990789Sahrens * If this pool already exists, return failure. 1991789Sahrens */ 1992789Sahrens mutex_enter(&spa_namespace_lock); 1993789Sahrens if (spa_lookup(pool) != NULL) { 1994789Sahrens mutex_exit(&spa_namespace_lock); 1995789Sahrens return (EEXIST); 1996789Sahrens } 1997789Sahrens 1998789Sahrens /* 1999789Sahrens * Allocate a new spa_t structure. 2000789Sahrens */ 20015094Slling (void) nvlist_lookup_string(props, 20025094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 20031635Sbonwick spa = spa_add(pool, altroot); 20048241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2005789Sahrens 2006789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 20075094Slling 20085094Slling if (props && (error = spa_prop_validate(spa, props))) { 20095094Slling spa_unload(spa); 20105094Slling spa_deactivate(spa); 20115094Slling spa_remove(spa); 20126643Seschrock mutex_exit(&spa_namespace_lock); 20135094Slling return (error); 20145094Slling } 20155094Slling 20165094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 20175094Slling &version) != 0) 20185094Slling version = SPA_VERSION; 20195094Slling ASSERT(version <= SPA_VERSION); 20205094Slling spa->spa_uberblock.ub_version = version; 2021789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2022789Sahrens 20231635Sbonwick /* 20249234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 20259234SGeorge.Wilson@Sun.COM */ 20269234SGeorge.Wilson@Sun.COM if (spa->spa_async_zio_root == NULL) 20279234SGeorge.Wilson@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 20289234SGeorge.Wilson@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 20299234SGeorge.Wilson@Sun.COM ZIO_FLAG_GODFATHER); 20309234SGeorge.Wilson@Sun.COM 20319234SGeorge.Wilson@Sun.COM /* 20321635Sbonwick * Create the root vdev. 20331635Sbonwick */ 20347754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20351635Sbonwick 20362082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 20372082Seschrock 20382082Seschrock ASSERT(error != 0 || rvd != NULL); 20392082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 20402082Seschrock 20415913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 20421635Sbonwick error = EINVAL; 20432082Seschrock 20442082Seschrock if (error == 0 && 20452082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 20465450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 20472082Seschrock VDEV_ALLOC_ADD)) == 0) { 20482082Seschrock for (c = 0; c < rvd->vdev_children; c++) 20492082Seschrock vdev_init(rvd->vdev_child[c], txg); 20502082Seschrock vdev_config_dirty(rvd); 20511635Sbonwick } 20521635Sbonwick 20537754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2054789Sahrens 20552082Seschrock if (error != 0) { 2056789Sahrens spa_unload(spa); 2057789Sahrens spa_deactivate(spa); 2058789Sahrens spa_remove(spa); 2059789Sahrens mutex_exit(&spa_namespace_lock); 2060789Sahrens return (error); 2061789Sahrens } 2062789Sahrens 20632082Seschrock /* 20642082Seschrock * Get the list of spares, if specified. 20652082Seschrock */ 20662082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 20672082Seschrock &spares, &nspares) == 0) { 20685450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 20692082Seschrock KM_SLEEP) == 0); 20705450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 20712082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 20727754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20732082Seschrock spa_load_spares(spa); 20747754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 20755450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 20765450Sbrendan } 20775450Sbrendan 20785450Sbrendan /* 20795450Sbrendan * Get the list of level 2 cache devices, if specified. 20805450Sbrendan */ 20815450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 20825450Sbrendan &l2cache, &nl2cache) == 0) { 20835450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 20845450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 20855450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 20865450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 20877754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20885450Sbrendan spa_load_l2cache(spa); 20897754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 20905450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 20912082Seschrock } 20922082Seschrock 20937184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2094789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2095789Sahrens 2096789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2097789Sahrens 2098789Sahrens /* 2099789Sahrens * Create the pool config object. 2100789Sahrens */ 2101789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 21027497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2103789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2104789Sahrens 21051544Seschrock if (zap_add(spa->spa_meta_objset, 2106789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 21071544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 21081544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 21091544Seschrock } 2110789Sahrens 21115094Slling /* Newly created pools with the right version are always deflated. */ 21125094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 21135094Slling spa->spa_deflate = TRUE; 21145094Slling if (zap_add(spa->spa_meta_objset, 21155094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 21165094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 21175094Slling cmn_err(CE_PANIC, "failed to add deflate"); 21185094Slling } 21192082Seschrock } 21202082Seschrock 2121789Sahrens /* 2122789Sahrens * Create the deferred-free bplist object. Turn off compression 2123789Sahrens * because sync-to-convergence takes longer if the blocksize 2124789Sahrens * keeps changing. 2125789Sahrens */ 2126789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2127789Sahrens 1 << 14, tx); 2128789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2129789Sahrens ZIO_COMPRESS_OFF, tx); 2130789Sahrens 21311544Seschrock if (zap_add(spa->spa_meta_objset, 2132789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 21331544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 21341544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 21351544Seschrock } 2136789Sahrens 21372926Sek110237 /* 21382926Sek110237 * Create the pool's history object. 21392926Sek110237 */ 21405094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 21415094Slling spa_history_create_obj(spa, tx); 21425094Slling 21435094Slling /* 21445094Slling * Set pool properties. 21455094Slling */ 21465094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 21475094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 21485329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 21498525SEric.Schrock@Sun.COM if (props != NULL) { 21508525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 21515094Slling spa_sync_props(spa, props, CRED(), tx); 21528525SEric.Schrock@Sun.COM } 21532926Sek110237 2154789Sahrens dmu_tx_commit(tx); 2155789Sahrens 2156789Sahrens spa->spa_sync_on = B_TRUE; 2157789Sahrens txg_sync_start(spa->spa_dsl_pool); 2158789Sahrens 2159789Sahrens /* 2160789Sahrens * We explicitly wait for the first transaction to complete so that our 2161789Sahrens * bean counters are appropriately updated. 2162789Sahrens */ 2163789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2164789Sahrens 21656643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2166789Sahrens 21675094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 21684715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 21694715Sek110237 21708667SGeorge.Wilson@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 21718667SGeorge.Wilson@Sun.COM 2172789Sahrens mutex_exit(&spa_namespace_lock); 2173789Sahrens 2174789Sahrens return (0); 2175789Sahrens } 2176789Sahrens 2177789Sahrens /* 2178789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2179789Sahrens * then call spa_load() to do the dirty work. 2180789Sahrens */ 21816423Sgw25295 static int 21826423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 21836643Seschrock boolean_t isroot, boolean_t allowfaulted) 2184789Sahrens { 2185789Sahrens spa_t *spa; 21865094Slling char *altroot = NULL; 21876643Seschrock int error, loaderr; 21882082Seschrock nvlist_t *nvroot; 21895450Sbrendan nvlist_t **spares, **l2cache; 21905450Sbrendan uint_t nspares, nl2cache; 2191789Sahrens 2192789Sahrens /* 2193789Sahrens * If a pool with this name exists, return failure. 2194789Sahrens */ 2195789Sahrens mutex_enter(&spa_namespace_lock); 21967897SLin.Ling@Sun.COM if ((spa = spa_lookup(pool)) != NULL) { 21977897SLin.Ling@Sun.COM if (isroot) { 21987897SLin.Ling@Sun.COM /* 21997897SLin.Ling@Sun.COM * Remove the existing root pool from the 22007897SLin.Ling@Sun.COM * namespace so that we can replace it with 22017897SLin.Ling@Sun.COM * the correct config we just read in. 22027897SLin.Ling@Sun.COM */ 22037897SLin.Ling@Sun.COM ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 22047897SLin.Ling@Sun.COM spa_remove(spa); 22057897SLin.Ling@Sun.COM } else { 22067897SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 22077897SLin.Ling@Sun.COM return (EEXIST); 22087897SLin.Ling@Sun.COM } 2209789Sahrens } 2210789Sahrens 2211789Sahrens /* 22121635Sbonwick * Create and initialize the spa structure. 2213789Sahrens */ 22145094Slling (void) nvlist_lookup_string(props, 22155094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 22161635Sbonwick spa = spa_add(pool, altroot); 22178241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2218789Sahrens 22196643Seschrock if (allowfaulted) 22206643Seschrock spa->spa_import_faulted = B_TRUE; 22216673Seschrock spa->spa_is_root = isroot; 22226643Seschrock 2223789Sahrens /* 22241635Sbonwick * Pass off the heavy lifting to spa_load(). 22257046Sahrens * Pass TRUE for mosconfig (unless this is a root pool) because 22267046Sahrens * the user-supplied config is actually the one to trust when 22277046Sahrens * doing an import. 22281601Sbonwick */ 22297046Sahrens loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot); 2230789Sahrens 22317754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22322082Seschrock /* 22332082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 22342082Seschrock * and conflicts with spa_has_spare(). 22352082Seschrock */ 22366423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 22375450Sbrendan nvlist_free(spa->spa_spares.sav_config); 22385450Sbrendan spa->spa_spares.sav_config = NULL; 22392082Seschrock spa_load_spares(spa); 22402082Seschrock } 22416423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 22425450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 22435450Sbrendan spa->spa_l2cache.sav_config = NULL; 22445450Sbrendan spa_load_l2cache(spa); 22455450Sbrendan } 22462082Seschrock 22472082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 22482082Seschrock &nvroot) == 0); 22495450Sbrendan if (error == 0) 22505450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 22515450Sbrendan if (error == 0) 22525450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 22535450Sbrendan VDEV_ALLOC_L2CACHE); 22547754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 22552082Seschrock 22568525SEric.Schrock@Sun.COM if (props != NULL) 22578525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 22588525SEric.Schrock@Sun.COM 22598241SJeff.Bonwick@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 22608241SJeff.Bonwick@Sun.COM (error = spa_prop_set(spa, props)))) { 22616643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 22626643Seschrock /* 22636643Seschrock * If we failed to load the pool, but 'allowfaulted' is 22646643Seschrock * set, then manually set the config as if the config 22656643Seschrock * passed in was specified in the cache file. 22666643Seschrock */ 22676643Seschrock error = 0; 22686643Seschrock spa->spa_import_faulted = B_FALSE; 22697754SJeff.Bonwick@Sun.COM if (spa->spa_config == NULL) 22706643Seschrock spa->spa_config = spa_config_generate(spa, 22716643Seschrock NULL, -1ULL, B_TRUE); 22726643Seschrock spa_unload(spa); 22736643Seschrock spa_deactivate(spa); 22746643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 22756643Seschrock } else { 22766643Seschrock spa_unload(spa); 22776643Seschrock spa_deactivate(spa); 22786643Seschrock spa_remove(spa); 22796643Seschrock } 2280789Sahrens mutex_exit(&spa_namespace_lock); 2281789Sahrens return (error); 2282789Sahrens } 2283789Sahrens 22841635Sbonwick /* 22855450Sbrendan * Override any spares and level 2 cache devices as specified by 22865450Sbrendan * the user, as these may have correct device names/devids, etc. 22872082Seschrock */ 22882082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 22892082Seschrock &spares, &nspares) == 0) { 22905450Sbrendan if (spa->spa_spares.sav_config) 22915450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 22922082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 22932082Seschrock else 22945450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 22952082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 22965450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 22972082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 22987754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22992082Seschrock spa_load_spares(spa); 23007754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 23015450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 23025450Sbrendan } 23035450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 23045450Sbrendan &l2cache, &nl2cache) == 0) { 23055450Sbrendan if (spa->spa_l2cache.sav_config) 23065450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 23075450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 23085450Sbrendan else 23095450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 23105450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 23115450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 23125450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 23137754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 23145450Sbrendan spa_load_l2cache(spa); 23157754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 23165450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 23172082Seschrock } 23182082Seschrock 23198241SJeff.Bonwick@Sun.COM if (spa_writeable(spa)) { 23206643Seschrock /* 23216643Seschrock * Update the config cache to include the newly-imported pool. 23226643Seschrock */ 23236423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 23246643Seschrock } 23256643Seschrock 23266643Seschrock spa->spa_import_faulted = B_FALSE; 23274451Seschrock mutex_exit(&spa_namespace_lock); 23284451Seschrock 2329789Sahrens return (0); 2330789Sahrens } 2331789Sahrens 23326423Sgw25295 #ifdef _KERNEL 23336423Sgw25295 /* 23346423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 23356423Sgw25295 * device label. 23366423Sgw25295 */ 23376423Sgw25295 static void 23386423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 23396423Sgw25295 { 23406423Sgw25295 nvlist_t *nvtop, *nvroot; 23416423Sgw25295 uint64_t pgid; 23426423Sgw25295 23436423Sgw25295 /* 23446423Sgw25295 * Add this top-level vdev to the child array. 23456423Sgw25295 */ 23466423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 23476423Sgw25295 == 0); 23486423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 23496423Sgw25295 == 0); 23506423Sgw25295 23516423Sgw25295 /* 23526423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 23536423Sgw25295 */ 23546423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 23556423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 23566423Sgw25295 == 0); 23576423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 23586423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 23596423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 23606423Sgw25295 &nvtop, 1) == 0); 23616423Sgw25295 23626423Sgw25295 /* 23636423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 23646423Sgw25295 * this pool's configuration (remove the old, add the new). 23656423Sgw25295 */ 23666423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 23676423Sgw25295 nvlist_free(nvroot); 23686423Sgw25295 } 23696423Sgw25295 23706423Sgw25295 /* 23716423Sgw25295 * Get the root pool information from the root disk, then import the root pool 23726423Sgw25295 * during the system boot up time. 23736423Sgw25295 */ 23747539SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 23757147Staylor 23767147Staylor int 23777147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf, 23786423Sgw25295 uint64_t *besttxg) 23796423Sgw25295 { 23806423Sgw25295 nvlist_t *config; 23816423Sgw25295 uint64_t txg; 23827539SLin.Ling@Sun.COM int error; 23837539SLin.Ling@Sun.COM 23847539SLin.Ling@Sun.COM if (error = vdev_disk_read_rootlabel(devpath, devid, &config)) 23857539SLin.Ling@Sun.COM return (error); 23866423Sgw25295 23876423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 23886423Sgw25295 23897147Staylor if (bestconf != NULL) 23906423Sgw25295 *bestconf = config; 23917539SLin.Ling@Sun.COM else 23927539SLin.Ling@Sun.COM nvlist_free(config); 23937147Staylor *besttxg = txg; 23947147Staylor return (0); 23956423Sgw25295 } 23966423Sgw25295 23976423Sgw25295 boolean_t 23986423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 23996423Sgw25295 { 24006423Sgw25295 uint64_t ival; 24016423Sgw25295 24026423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 24036423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 24046423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 24056423Sgw25295 return (B_FALSE); 24066423Sgw25295 24076423Sgw25295 return (B_TRUE); 24086423Sgw25295 } 24096423Sgw25295 24107147Staylor 24117147Staylor /* 24127147Staylor * Given the boot device's physical path or devid, check if the device 24137147Staylor * is in a valid state. If so, return the configuration from the vdev 24147147Staylor * label. 24157147Staylor */ 24167147Staylor int 24177147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf) 24187147Staylor { 24197147Staylor nvlist_t *conf = NULL; 24207147Staylor uint64_t txg = 0; 24217147Staylor nvlist_t *nvtop, **child; 24227147Staylor char *type; 24237147Staylor char *bootpath = NULL; 24247147Staylor uint_t children, c; 24257147Staylor char *tmp; 24267539SLin.Ling@Sun.COM int error; 24277147Staylor 24287147Staylor if (devpath && ((tmp = strchr(devpath, ' ')) != NULL)) 24297147Staylor *tmp = '\0'; 24307539SLin.Ling@Sun.COM if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) { 24317147Staylor cmn_err(CE_NOTE, "error reading device label"); 24327539SLin.Ling@Sun.COM return (error); 24337147Staylor } 24347147Staylor if (txg == 0) { 24357147Staylor cmn_err(CE_NOTE, "this device is detached"); 24367147Staylor nvlist_free(conf); 24377147Staylor return (EINVAL); 24387147Staylor } 24397147Staylor 24407147Staylor VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE, 24417147Staylor &nvtop) == 0); 24427147Staylor VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0); 24437147Staylor 24447147Staylor if (strcmp(type, VDEV_TYPE_DISK) == 0) { 24457147Staylor if (spa_rootdev_validate(nvtop)) { 24467147Staylor goto out; 24477147Staylor } else { 24487147Staylor nvlist_free(conf); 24497147Staylor return (EINVAL); 24507147Staylor } 24517147Staylor } 24527147Staylor 24537147Staylor ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0); 24547147Staylor 24557147Staylor VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN, 24567147Staylor &child, &children) == 0); 24577147Staylor 24587147Staylor /* 24597147Staylor * Go thru vdevs in the mirror to see if the given device 24607147Staylor * has the most recent txg. Only the device with the most 24617147Staylor * recent txg has valid information and should be booted. 24627147Staylor */ 24637147Staylor for (c = 0; c < children; c++) { 24647147Staylor char *cdevid, *cpath; 24657147Staylor uint64_t tmptxg; 24667147Staylor 24678242SLin.Ling@Sun.COM cpath = NULL; 24688242SLin.Ling@Sun.COM cdevid = NULL; 24697147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH, 24708242SLin.Ling@Sun.COM &cpath) != 0 && nvlist_lookup_string(child[c], 24718242SLin.Ling@Sun.COM ZPOOL_CONFIG_DEVID, &cdevid) != 0) 24727147Staylor return (EINVAL); 24737687SLin.Ling@Sun.COM if ((spa_check_rootconf(cpath, cdevid, NULL, 24747687SLin.Ling@Sun.COM &tmptxg) == 0) && (tmptxg > txg)) { 24757147Staylor txg = tmptxg; 24767147Staylor VERIFY(nvlist_lookup_string(child[c], 24777147Staylor ZPOOL_CONFIG_PATH, &bootpath) == 0); 24787147Staylor } 24797147Staylor } 24807147Staylor 24817147Staylor /* Does the best device match the one we've booted from? */ 24827147Staylor if (bootpath) { 24837147Staylor cmn_err(CE_NOTE, "try booting from '%s'", bootpath); 24847147Staylor return (EINVAL); 24857147Staylor } 24867147Staylor out: 24877147Staylor *bestconf = conf; 24887147Staylor return (0); 24897147Staylor } 24907147Staylor 24916423Sgw25295 /* 24926423Sgw25295 * Import a root pool. 24936423Sgw25295 * 24947147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 24957147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 24967147Staylor * The GRUB "findroot" command will return the vdev we should boot. 24976423Sgw25295 * 24986423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 24996423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 25006423Sgw25295 * e.g. 25016423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 25026423Sgw25295 */ 25036423Sgw25295 int 25047147Staylor spa_import_rootpool(char *devpath, char *devid) 25056423Sgw25295 { 25066423Sgw25295 nvlist_t *conf = NULL; 25076423Sgw25295 char *pname; 25086423Sgw25295 int error; 25096423Sgw25295 25106423Sgw25295 /* 25116423Sgw25295 * Get the vdev pathname and configuation from the most 25126423Sgw25295 * recently updated vdev (highest txg). 25136423Sgw25295 */ 25147147Staylor if (error = spa_get_rootconf(devpath, devid, &conf)) 25156423Sgw25295 goto msg_out; 25166423Sgw25295 25176423Sgw25295 /* 25186423Sgw25295 * Add type "root" vdev to the config. 25196423Sgw25295 */ 25206423Sgw25295 spa_build_rootpool_config(conf); 25216423Sgw25295 25226423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 25236423Sgw25295 25246673Seschrock /* 25256673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 25266673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 25276673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 25286673Seschrock * pool open, not import. 25296673Seschrock */ 25306673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 25317897SLin.Ling@Sun.COM ASSERT(error != EEXIST); 25326423Sgw25295 25336423Sgw25295 nvlist_free(conf); 25346423Sgw25295 return (error); 25356423Sgw25295 25366423Sgw25295 msg_out: 25377147Staylor cmn_err(CE_NOTE, "\n" 25386423Sgw25295 " *************************************************** \n" 25396423Sgw25295 " * This device is not bootable! * \n" 25406423Sgw25295 " * It is either offlined or detached or faulted. * \n" 25416423Sgw25295 " * Please try to boot from a different device. * \n" 25427147Staylor " *************************************************** "); 25436423Sgw25295 25446423Sgw25295 return (error); 25456423Sgw25295 } 25466423Sgw25295 #endif 25476423Sgw25295 25486423Sgw25295 /* 25496423Sgw25295 * Import a non-root pool into the system. 25506423Sgw25295 */ 25516423Sgw25295 int 25526423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 25536423Sgw25295 { 25546643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 25556423Sgw25295 } 25566423Sgw25295 25576643Seschrock int 25586643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 25596643Seschrock { 25606643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 25616643Seschrock } 25626643Seschrock 25636643Seschrock 2564789Sahrens /* 2565789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2566789Sahrens * to get the vdev stats associated with the imported devices. 2567789Sahrens */ 2568789Sahrens #define TRYIMPORT_NAME "$import" 2569789Sahrens 2570789Sahrens nvlist_t * 2571789Sahrens spa_tryimport(nvlist_t *tryconfig) 2572789Sahrens { 2573789Sahrens nvlist_t *config = NULL; 2574789Sahrens char *poolname; 2575789Sahrens spa_t *spa; 2576789Sahrens uint64_t state; 25778680SLin.Ling@Sun.COM int error; 2578789Sahrens 2579789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2580789Sahrens return (NULL); 2581789Sahrens 2582789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2583789Sahrens return (NULL); 2584789Sahrens 25851635Sbonwick /* 25861635Sbonwick * Create and initialize the spa structure. 25871635Sbonwick */ 2588789Sahrens mutex_enter(&spa_namespace_lock); 25891635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 25908241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 2591789Sahrens 2592789Sahrens /* 25931635Sbonwick * Pass off the heavy lifting to spa_load(). 25941732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 25951732Sbonwick * is actually the one to trust when doing an import. 2596789Sahrens */ 25978680SLin.Ling@Sun.COM error = spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2598789Sahrens 2599789Sahrens /* 2600789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2601789Sahrens */ 2602789Sahrens if (spa->spa_root_vdev != NULL) { 2603789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2604789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2605789Sahrens poolname) == 0); 2606789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2607789Sahrens state) == 0); 26083975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 26093975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 26102082Seschrock 26112082Seschrock /* 26126423Sgw25295 * If the bootfs property exists on this pool then we 26136423Sgw25295 * copy it out so that external consumers can tell which 26146423Sgw25295 * pools are bootable. 26156423Sgw25295 */ 26168680SLin.Ling@Sun.COM if ((!error || error == EEXIST) && spa->spa_bootfs) { 26176423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 26186423Sgw25295 26196423Sgw25295 /* 26206423Sgw25295 * We have to play games with the name since the 26216423Sgw25295 * pool was opened as TRYIMPORT_NAME. 26226423Sgw25295 */ 26237754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 26246423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 26256423Sgw25295 char *cp; 26266423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 26276423Sgw25295 26286423Sgw25295 cp = strchr(tmpname, '/'); 26296423Sgw25295 if (cp == NULL) { 26306423Sgw25295 (void) strlcpy(dsname, tmpname, 26316423Sgw25295 MAXPATHLEN); 26326423Sgw25295 } else { 26336423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 26346423Sgw25295 "%s/%s", poolname, ++cp); 26356423Sgw25295 } 26366423Sgw25295 VERIFY(nvlist_add_string(config, 26376423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 26386423Sgw25295 kmem_free(dsname, MAXPATHLEN); 26396423Sgw25295 } 26406423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 26416423Sgw25295 } 26426423Sgw25295 26436423Sgw25295 /* 26445450Sbrendan * Add the list of hot spares and level 2 cache devices. 26452082Seschrock */ 26462082Seschrock spa_add_spares(spa, config); 26475450Sbrendan spa_add_l2cache(spa, config); 2648789Sahrens } 2649789Sahrens 2650789Sahrens spa_unload(spa); 2651789Sahrens spa_deactivate(spa); 2652789Sahrens spa_remove(spa); 2653789Sahrens mutex_exit(&spa_namespace_lock); 2654789Sahrens 2655789Sahrens return (config); 2656789Sahrens } 2657789Sahrens 2658789Sahrens /* 2659789Sahrens * Pool export/destroy 2660789Sahrens * 2661789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2662789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2663789Sahrens * update the pool state and sync all the labels to disk, removing the 26648211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 26658211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 2666789Sahrens */ 2667789Sahrens static int 26687214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 26698211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 2670789Sahrens { 2671789Sahrens spa_t *spa; 2672789Sahrens 26731775Sbillm if (oldconfig) 26741775Sbillm *oldconfig = NULL; 26751775Sbillm 26768241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 2677789Sahrens return (EROFS); 2678789Sahrens 2679789Sahrens mutex_enter(&spa_namespace_lock); 2680789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2681789Sahrens mutex_exit(&spa_namespace_lock); 2682789Sahrens return (ENOENT); 2683789Sahrens } 2684789Sahrens 2685789Sahrens /* 26861544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 26871544Seschrock * reacquire the namespace lock, and see if we can export. 26881544Seschrock */ 26891544Seschrock spa_open_ref(spa, FTAG); 26901544Seschrock mutex_exit(&spa_namespace_lock); 26911544Seschrock spa_async_suspend(spa); 26921544Seschrock mutex_enter(&spa_namespace_lock); 26931544Seschrock spa_close(spa, FTAG); 26941544Seschrock 26951544Seschrock /* 2696789Sahrens * The pool will be in core if it's openable, 2697789Sahrens * in which case we can modify its state. 2698789Sahrens */ 2699789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2700789Sahrens /* 2701789Sahrens * Objsets may be open only because they're dirty, so we 2702789Sahrens * have to force it to sync before checking spa_refcnt. 2703789Sahrens */ 2704789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2705789Sahrens 27061544Seschrock /* 27071544Seschrock * A pool cannot be exported or destroyed if there are active 27081544Seschrock * references. If we are resetting a pool, allow references by 27091544Seschrock * fault injection handlers. 27101544Seschrock */ 27111544Seschrock if (!spa_refcount_zero(spa) || 27121544Seschrock (spa->spa_inject_ref != 0 && 27131544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 27141544Seschrock spa_async_resume(spa); 2715789Sahrens mutex_exit(&spa_namespace_lock); 2716789Sahrens return (EBUSY); 2717789Sahrens } 2718789Sahrens 2719789Sahrens /* 27207214Slling * A pool cannot be exported if it has an active shared spare. 27217214Slling * This is to prevent other pools stealing the active spare 27227214Slling * from an exported pool. At user's own will, such pool can 27237214Slling * be forcedly exported. 27247214Slling */ 27257214Slling if (!force && new_state == POOL_STATE_EXPORTED && 27267214Slling spa_has_active_shared_spare(spa)) { 27277214Slling spa_async_resume(spa); 27287214Slling mutex_exit(&spa_namespace_lock); 27297214Slling return (EXDEV); 27307214Slling } 27317214Slling 27327214Slling /* 2733789Sahrens * We want this to be reflected on every label, 2734789Sahrens * so mark them all dirty. spa_unload() will do the 2735789Sahrens * final sync that pushes these changes out. 2736789Sahrens */ 27378211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 27387754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27391544Seschrock spa->spa_state = new_state; 27401635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 27411544Seschrock vdev_config_dirty(spa->spa_root_vdev); 27427754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 27431544Seschrock } 2744789Sahrens } 2745789Sahrens 27464451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 27474451Seschrock 2748789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2749789Sahrens spa_unload(spa); 2750789Sahrens spa_deactivate(spa); 2751789Sahrens } 2752789Sahrens 27531775Sbillm if (oldconfig && spa->spa_config) 27541775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 27551775Sbillm 27561544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 27578211SGeorge.Wilson@Sun.COM if (!hardforce) 27588211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 27591544Seschrock spa_remove(spa); 27601544Seschrock } 2761789Sahrens mutex_exit(&spa_namespace_lock); 2762789Sahrens 2763789Sahrens return (0); 2764789Sahrens } 2765789Sahrens 2766789Sahrens /* 2767789Sahrens * Destroy a storage pool. 2768789Sahrens */ 2769789Sahrens int 2770789Sahrens spa_destroy(char *pool) 2771789Sahrens { 27728211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 27738211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 2774789Sahrens } 2775789Sahrens 2776789Sahrens /* 2777789Sahrens * Export a storage pool. 2778789Sahrens */ 2779789Sahrens int 27808211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 27818211SGeorge.Wilson@Sun.COM boolean_t hardforce) 2782789Sahrens { 27838211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 27848211SGeorge.Wilson@Sun.COM force, hardforce)); 2785789Sahrens } 2786789Sahrens 2787789Sahrens /* 27881544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 27891544Seschrock * from the namespace in any way. 27901544Seschrock */ 27911544Seschrock int 27921544Seschrock spa_reset(char *pool) 27931544Seschrock { 27947214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 27958211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 27961544Seschrock } 27971544Seschrock 27981544Seschrock /* 2799789Sahrens * ========================================================================== 2800789Sahrens * Device manipulation 2801789Sahrens * ========================================================================== 2802789Sahrens */ 2803789Sahrens 2804789Sahrens /* 28054527Sperrin * Add a device to a storage pool. 2806789Sahrens */ 2807789Sahrens int 2808789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2809789Sahrens { 2810789Sahrens uint64_t txg; 28118241SJeff.Bonwick@Sun.COM int error; 2812789Sahrens vdev_t *rvd = spa->spa_root_vdev; 28131585Sbonwick vdev_t *vd, *tvd; 28145450Sbrendan nvlist_t **spares, **l2cache; 28155450Sbrendan uint_t nspares, nl2cache; 2816789Sahrens 2817789Sahrens txg = spa_vdev_enter(spa); 2818789Sahrens 28192082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 28202082Seschrock VDEV_ALLOC_ADD)) != 0) 28212082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 28222082Seschrock 28237754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 2824789Sahrens 28255450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 28265450Sbrendan &nspares) != 0) 28272082Seschrock nspares = 0; 28282082Seschrock 28295450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 28305450Sbrendan &nl2cache) != 0) 28315450Sbrendan nl2cache = 0; 28325450Sbrendan 28337754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 28342082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 28357754SJeff.Bonwick@Sun.COM 28367754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 28377754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 28387754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 28392082Seschrock 28403377Seschrock /* 28415450Sbrendan * We must validate the spares and l2cache devices after checking the 28425450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 28433377Seschrock */ 28447754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 28453377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 28463377Seschrock 28473377Seschrock /* 28483377Seschrock * Transfer each new top-level vdev from vd to rvd. 28493377Seschrock */ 28508241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 28513377Seschrock tvd = vd->vdev_child[c]; 28523377Seschrock vdev_remove_child(vd, tvd); 28533377Seschrock tvd->vdev_id = rvd->vdev_children; 28543377Seschrock vdev_add_child(rvd, tvd); 28553377Seschrock vdev_config_dirty(tvd); 28563377Seschrock } 28573377Seschrock 28582082Seschrock if (nspares != 0) { 28595450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 28605450Sbrendan ZPOOL_CONFIG_SPARES); 28612082Seschrock spa_load_spares(spa); 28625450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 28635450Sbrendan } 28645450Sbrendan 28655450Sbrendan if (nl2cache != 0) { 28665450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 28675450Sbrendan ZPOOL_CONFIG_L2CACHE); 28685450Sbrendan spa_load_l2cache(spa); 28695450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2870789Sahrens } 2871789Sahrens 2872789Sahrens /* 28731585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 28741585Sbonwick * If other threads start allocating from these vdevs before we 28751585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 28761585Sbonwick * fail to open the pool because there are DVAs that the config cache 28771585Sbonwick * can't translate. Therefore, we first add the vdevs without 28781585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 28791635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 28801585Sbonwick * 28811585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 28821585Sbonwick * if we lose power at any point in this sequence, the remaining 28831585Sbonwick * steps will be completed the next time we load the pool. 2884789Sahrens */ 28851635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 28861585Sbonwick 28871635Sbonwick mutex_enter(&spa_namespace_lock); 28881635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 28891635Sbonwick mutex_exit(&spa_namespace_lock); 2890789Sahrens 28911635Sbonwick return (0); 2892789Sahrens } 2893789Sahrens 2894789Sahrens /* 2895789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2896789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2897789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2898789Sahrens * 2899789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2900789Sahrens * existing device; in this case the two devices are made into their own 29014451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2902789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2903789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2904789Sahrens * completion of resilvering, the first disk (the one being replaced) 2905789Sahrens * is automatically detached. 2906789Sahrens */ 2907789Sahrens int 29081544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2909789Sahrens { 2910789Sahrens uint64_t txg, open_txg; 2911789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2912789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 29132082Seschrock vdev_ops_t *pvops; 29147313SEric.Kustarz@Sun.COM dmu_tx_t *tx; 29157313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 29167313SEric.Kustarz@Sun.COM int newvd_isspare; 29177313SEric.Kustarz@Sun.COM int error; 2918789Sahrens 2919789Sahrens txg = spa_vdev_enter(spa); 2920789Sahrens 29216643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2922789Sahrens 2923789Sahrens if (oldvd == NULL) 2924789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2925789Sahrens 29261585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 29271585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 29281585Sbonwick 2929789Sahrens pvd = oldvd->vdev_parent; 2930789Sahrens 29312082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 29324451Seschrock VDEV_ALLOC_ADD)) != 0) 29334451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 29344451Seschrock 29354451Seschrock if (newrootvd->vdev_children != 1) 2936789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2937789Sahrens 2938789Sahrens newvd = newrootvd->vdev_child[0]; 2939789Sahrens 2940789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2941789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2942789Sahrens 29432082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2944789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2945789Sahrens 29464527Sperrin /* 29474527Sperrin * Spares can't replace logs 29484527Sperrin */ 29497326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 29504527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29514527Sperrin 29522082Seschrock if (!replacing) { 29532082Seschrock /* 29542082Seschrock * For attach, the only allowable parent is a mirror or the root 29552082Seschrock * vdev. 29562082Seschrock */ 29572082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 29582082Seschrock pvd->vdev_ops != &vdev_root_ops) 29592082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29602082Seschrock 29612082Seschrock pvops = &vdev_mirror_ops; 29622082Seschrock } else { 29632082Seschrock /* 29642082Seschrock * Active hot spares can only be replaced by inactive hot 29652082Seschrock * spares. 29662082Seschrock */ 29672082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 29682082Seschrock pvd->vdev_child[1] == oldvd && 29692082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 29702082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29712082Seschrock 29722082Seschrock /* 29732082Seschrock * If the source is a hot spare, and the parent isn't already a 29742082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 29753377Seschrock * want to create a replacing vdev. The user is not allowed to 29763377Seschrock * attach to a spared vdev child unless the 'isspare' state is 29773377Seschrock * the same (spare replaces spare, non-spare replaces 29783377Seschrock * non-spare). 29792082Seschrock */ 29802082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 29812082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29823377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 29833377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 29843377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29852082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 29862082Seschrock newvd->vdev_isspare) 29872082Seschrock pvops = &vdev_spare_ops; 29882082Seschrock else 29892082Seschrock pvops = &vdev_replacing_ops; 29902082Seschrock } 29912082Seschrock 29921175Slling /* 29931175Slling * Compare the new device size with the replaceable/attachable 29941175Slling * device size. 29951175Slling */ 29961175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2997789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2998789Sahrens 29991732Sbonwick /* 30001732Sbonwick * The new device cannot have a higher alignment requirement 30011732Sbonwick * than the top-level vdev. 30021732Sbonwick */ 30031732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3004789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3005789Sahrens 3006789Sahrens /* 3007789Sahrens * If this is an in-place replacement, update oldvd's path and devid 3008789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3009789Sahrens */ 3010789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3011789Sahrens spa_strfree(oldvd->vdev_path); 3012789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3013789Sahrens KM_SLEEP); 3014789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3015789Sahrens newvd->vdev_path, "old"); 3016789Sahrens if (oldvd->vdev_devid != NULL) { 3017789Sahrens spa_strfree(oldvd->vdev_devid); 3018789Sahrens oldvd->vdev_devid = NULL; 3019789Sahrens } 3020789Sahrens } 3021789Sahrens 3022789Sahrens /* 30232082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 30242082Seschrock * mirror/replacing/spare vdev above oldvd. 3025789Sahrens */ 3026789Sahrens if (pvd->vdev_ops != pvops) 3027789Sahrens pvd = vdev_add_parent(oldvd, pvops); 3028789Sahrens 3029789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3030789Sahrens ASSERT(pvd->vdev_ops == pvops); 3031789Sahrens ASSERT(oldvd->vdev_parent == pvd); 3032789Sahrens 3033789Sahrens /* 3034789Sahrens * Extract the new device from its root and add it to pvd. 3035789Sahrens */ 3036789Sahrens vdev_remove_child(newrootvd, newvd); 3037789Sahrens newvd->vdev_id = pvd->vdev_children; 3038789Sahrens vdev_add_child(pvd, newvd); 3039789Sahrens 30401544Seschrock /* 30411544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 30421544Seschrock * the addition of newvd may have decreased our parent's asize. 30431544Seschrock */ 30441544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 30451544Seschrock 3046789Sahrens tvd = newvd->vdev_top; 3047789Sahrens ASSERT(pvd->vdev_top == tvd); 3048789Sahrens ASSERT(tvd->vdev_parent == rvd); 3049789Sahrens 3050789Sahrens vdev_config_dirty(tvd); 3051789Sahrens 3052789Sahrens /* 3053789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3054789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3055789Sahrens */ 3056789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 3057789Sahrens 30588241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, 30598241SJeff.Bonwick@Sun.COM TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3060789Sahrens 30613377Seschrock if (newvd->vdev_isspare) 30623377Seschrock spa_spare_activate(newvd); 30637754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 30647754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 30657313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 30661544Seschrock 3067789Sahrens /* 3068789Sahrens * Mark newvd's DTL dirty in this txg. 3069789Sahrens */ 30701732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3071789Sahrens 3072789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3073789Sahrens 30747313SEric.Kustarz@Sun.COM tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 30757313SEric.Kustarz@Sun.COM if (dmu_tx_assign(tx, TXG_WAIT) == 0) { 30767313SEric.Kustarz@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx, 30777313SEric.Kustarz@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 30787313SEric.Kustarz@Sun.COM replacing && newvd_isspare ? "spare in" : 30797313SEric.Kustarz@Sun.COM replacing ? "replace" : "attach", newvdpath, 30807313SEric.Kustarz@Sun.COM replacing ? "for" : "to", oldvdpath); 30817313SEric.Kustarz@Sun.COM dmu_tx_commit(tx); 30827313SEric.Kustarz@Sun.COM } else { 30837313SEric.Kustarz@Sun.COM dmu_tx_abort(tx); 30847313SEric.Kustarz@Sun.COM } 30857313SEric.Kustarz@Sun.COM 30867313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 30877313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 30887313SEric.Kustarz@Sun.COM 3089789Sahrens /* 30907046Sahrens * Kick off a resilver to update newvd. 3091789Sahrens */ 30927046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3093789Sahrens 3094789Sahrens return (0); 3095789Sahrens } 3096789Sahrens 3097789Sahrens /* 3098789Sahrens * Detach a device from a mirror or replacing vdev. 3099789Sahrens * If 'replace_done' is specified, only detach if the parent 3100789Sahrens * is a replacing vdev. 3101789Sahrens */ 3102789Sahrens int 31038241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3104789Sahrens { 3105789Sahrens uint64_t txg; 31068241SJeff.Bonwick@Sun.COM int error; 3107789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3108789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 31092082Seschrock boolean_t unspare = B_FALSE; 31102082Seschrock uint64_t unspare_guid; 31116673Seschrock size_t len; 3112789Sahrens 3113789Sahrens txg = spa_vdev_enter(spa); 3114789Sahrens 31156643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3116789Sahrens 3117789Sahrens if (vd == NULL) 3118789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3119789Sahrens 31201585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 31211585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 31221585Sbonwick 3123789Sahrens pvd = vd->vdev_parent; 3124789Sahrens 3125789Sahrens /* 31268241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 31278241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 31288241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 31298241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 31308241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 31318241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 31328241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 31338241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 31348241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 31358241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 31368241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 31378241SJeff.Bonwick@Sun.COM */ 31388241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 31398241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 31408241SJeff.Bonwick@Sun.COM 31418241SJeff.Bonwick@Sun.COM /* 3142789Sahrens * If replace_done is specified, only remove this device if it's 31432082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 31442082Seschrock * disk can be removed. 3145789Sahrens */ 31462082Seschrock if (replace_done) { 31472082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 31482082Seschrock if (vd->vdev_id != 0) 31492082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 31502082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 31512082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 31522082Seschrock } 31532082Seschrock } 31542082Seschrock 31552082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 31564577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3157789Sahrens 3158789Sahrens /* 31592082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3160789Sahrens */ 3161789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 31622082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 31632082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3164789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3165789Sahrens 3166789Sahrens /* 31678241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 31688241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 3169789Sahrens */ 31708241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 3171789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3172789Sahrens 31738241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 31748241SJeff.Bonwick@Sun.COM 3175789Sahrens /* 31766673Seschrock * If we are detaching the second disk from a replacing vdev, then 31776673Seschrock * check to see if we changed the original vdev's path to have "/old" 31786673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 31796673Seschrock */ 31806673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 31816673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 31826673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 31836673Seschrock ASSERT(pvd->vdev_child[1] == vd); 31846673Seschrock cvd = pvd->vdev_child[0]; 31856673Seschrock len = strlen(vd->vdev_path); 31866673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 31876673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 31886673Seschrock spa_strfree(cvd->vdev_path); 31896673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 31906673Seschrock } 31916673Seschrock } 31926673Seschrock 31936673Seschrock /* 31942082Seschrock * If we are detaching the original disk from a spare, then it implies 31952082Seschrock * that the spare should become a real disk, and be removed from the 31962082Seschrock * active spare list for the pool. 31972082Seschrock */ 31982082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 31998241SJeff.Bonwick@Sun.COM vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 32002082Seschrock unspare = B_TRUE; 32012082Seschrock 32022082Seschrock /* 3203789Sahrens * Erase the disk labels so the disk can be used for other things. 3204789Sahrens * This must be done after all other error cases are handled, 3205789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3206789Sahrens * But if we can't do it, don't treat the error as fatal -- 3207789Sahrens * it may be that the unwritability of the disk is the reason 3208789Sahrens * it's being detached! 3209789Sahrens */ 32103377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3211789Sahrens 3212789Sahrens /* 3213789Sahrens * Remove vd from its parent and compact the parent's children. 3214789Sahrens */ 3215789Sahrens vdev_remove_child(pvd, vd); 3216789Sahrens vdev_compact_children(pvd); 3217789Sahrens 3218789Sahrens /* 3219789Sahrens * Remember one of the remaining children so we can get tvd below. 3220789Sahrens */ 3221789Sahrens cvd = pvd->vdev_child[0]; 3222789Sahrens 3223789Sahrens /* 32242082Seschrock * If we need to remove the remaining child from the list of hot spares, 32258241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 32268241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 32278241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 32288241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 32298241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 32308241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 32312082Seschrock */ 32322082Seschrock if (unspare) { 32332082Seschrock ASSERT(cvd->vdev_isspare); 32343377Seschrock spa_spare_remove(cvd); 32352082Seschrock unspare_guid = cvd->vdev_guid; 32368241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 32372082Seschrock } 32382082Seschrock 32392082Seschrock /* 3240789Sahrens * If the parent mirror/replacing vdev only has one child, 3241789Sahrens * the parent is no longer needed. Remove it from the tree. 3242789Sahrens */ 3243789Sahrens if (pvd->vdev_children == 1) 3244789Sahrens vdev_remove_parent(cvd); 3245789Sahrens 3246789Sahrens /* 3247789Sahrens * We don't set tvd until now because the parent we just removed 3248789Sahrens * may have been the previous top-level vdev. 3249789Sahrens */ 3250789Sahrens tvd = cvd->vdev_top; 3251789Sahrens ASSERT(tvd->vdev_parent == rvd); 3252789Sahrens 3253789Sahrens /* 32543377Seschrock * Reevaluate the parent vdev state. 3255789Sahrens */ 32564451Seschrock vdev_propagate_state(cvd); 3257789Sahrens 3258789Sahrens /* 32593377Seschrock * If the device we just detached was smaller than the others, it may be 32603377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 32613377Seschrock * can't fail because the existing metaslabs are already in core, so 32623377Seschrock * there's nothing to read from disk. 3263789Sahrens */ 32641732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3265789Sahrens 3266789Sahrens vdev_config_dirty(tvd); 3267789Sahrens 3268789Sahrens /* 32693377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 32703377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 32713377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 32723377Seschrock * prevent vd from being accessed after it's freed. 3273789Sahrens */ 32748241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 3275789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 32761732Sbonwick vd->vdev_detached = B_TRUE; 32771732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3278789Sahrens 32794451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 32804451Seschrock 32812082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 32822082Seschrock 32832082Seschrock /* 32843377Seschrock * If this was the removal of the original device in a hot spare vdev, 32853377Seschrock * then we want to go through and remove the device from the hot spare 32863377Seschrock * list of every other pool. 32872082Seschrock */ 32882082Seschrock if (unspare) { 32898241SJeff.Bonwick@Sun.COM spa_t *myspa = spa; 32902082Seschrock spa = NULL; 32912082Seschrock mutex_enter(&spa_namespace_lock); 32922082Seschrock while ((spa = spa_next(spa)) != NULL) { 32932082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 32942082Seschrock continue; 32958241SJeff.Bonwick@Sun.COM if (spa == myspa) 32968241SJeff.Bonwick@Sun.COM continue; 32977793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 32987793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 32992082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 33007793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 33017793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 33022082Seschrock } 33032082Seschrock mutex_exit(&spa_namespace_lock); 33042082Seschrock } 33052082Seschrock 33062082Seschrock return (error); 33072082Seschrock } 33082082Seschrock 33097754SJeff.Bonwick@Sun.COM static nvlist_t * 33107754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 33112082Seschrock { 33127754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 33137754SJeff.Bonwick@Sun.COM uint64_t guid; 33147754SJeff.Bonwick@Sun.COM 33157754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 33167754SJeff.Bonwick@Sun.COM &guid) == 0); 33177754SJeff.Bonwick@Sun.COM 33187754SJeff.Bonwick@Sun.COM if (guid == target_guid) 33197754SJeff.Bonwick@Sun.COM return (nvpp[i]); 33202082Seschrock } 33212082Seschrock 33227754SJeff.Bonwick@Sun.COM return (NULL); 33235450Sbrendan } 33245450Sbrendan 33257754SJeff.Bonwick@Sun.COM static void 33267754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 33277754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 33285450Sbrendan { 33297754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 33307754SJeff.Bonwick@Sun.COM 33317754SJeff.Bonwick@Sun.COM if (count > 1) 33327754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 33337754SJeff.Bonwick@Sun.COM 33347754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 33357754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 33367754SJeff.Bonwick@Sun.COM continue; 33377754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 33385450Sbrendan } 33395450Sbrendan 33407754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 33417754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 33427754SJeff.Bonwick@Sun.COM 33437754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 33447754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 33457754SJeff.Bonwick@Sun.COM 33467754SJeff.Bonwick@Sun.COM if (count > 1) 33477754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 33485450Sbrendan } 33495450Sbrendan 33505450Sbrendan /* 33515450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 33525450Sbrendan * spares and level 2 ARC devices. 33535450Sbrendan */ 33545450Sbrendan int 33555450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 33565450Sbrendan { 33575450Sbrendan vdev_t *vd; 33587754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 33595450Sbrendan uint_t nspares, nl2cache; 33608241SJeff.Bonwick@Sun.COM uint64_t txg = 0; 33615450Sbrendan int error = 0; 33628241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 33638241SJeff.Bonwick@Sun.COM 33648241SJeff.Bonwick@Sun.COM if (!locked) 33658241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 33665450Sbrendan 33676643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 33685450Sbrendan 33695450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 33705450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 33717754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 33727754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 33737754SJeff.Bonwick@Sun.COM /* 33747754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 33757754SJeff.Bonwick@Sun.COM * in this pool. 33767754SJeff.Bonwick@Sun.COM */ 33777754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 33787754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 33797754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 33807754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 33817754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 33827754SJeff.Bonwick@Sun.COM } else { 33837754SJeff.Bonwick@Sun.COM error = EBUSY; 33847754SJeff.Bonwick@Sun.COM } 33857754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 33865450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 33877754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 33887754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 33897754SJeff.Bonwick@Sun.COM /* 33907754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 33917754SJeff.Bonwick@Sun.COM */ 33927754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 33937754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 33945450Sbrendan spa_load_l2cache(spa); 33955450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 33967754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 33977754SJeff.Bonwick@Sun.COM /* 33987754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 33997754SJeff.Bonwick@Sun.COM */ 34007754SJeff.Bonwick@Sun.COM error = ENOTSUP; 34017754SJeff.Bonwick@Sun.COM } else { 34027754SJeff.Bonwick@Sun.COM /* 34037754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 34047754SJeff.Bonwick@Sun.COM */ 34057754SJeff.Bonwick@Sun.COM error = ENOENT; 34065450Sbrendan } 34072082Seschrock 34088241SJeff.Bonwick@Sun.COM if (!locked) 34098241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 34108241SJeff.Bonwick@Sun.COM 34118241SJeff.Bonwick@Sun.COM return (error); 3412789Sahrens } 3413789Sahrens 3414789Sahrens /* 34154451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 34164451Seschrock * current spared, so we can detach it. 3417789Sahrens */ 34181544Seschrock static vdev_t * 34194451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3420789Sahrens { 34211544Seschrock vdev_t *newvd, *oldvd; 3422789Sahrens int c; 3423789Sahrens 34241544Seschrock for (c = 0; c < vd->vdev_children; c++) { 34254451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 34261544Seschrock if (oldvd != NULL) 34271544Seschrock return (oldvd); 34281544Seschrock } 3429789Sahrens 34304451Seschrock /* 34314451Seschrock * Check for a completed replacement. 34324451Seschrock */ 3433789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 34341544Seschrock oldvd = vd->vdev_child[0]; 34351544Seschrock newvd = vd->vdev_child[1]; 3436789Sahrens 34378241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 34388241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 34391544Seschrock return (oldvd); 34401544Seschrock } 3441789Sahrens 34424451Seschrock /* 34434451Seschrock * Check for a completed resilver with the 'unspare' flag set. 34444451Seschrock */ 34454451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 34464451Seschrock newvd = vd->vdev_child[0]; 34474451Seschrock oldvd = vd->vdev_child[1]; 34484451Seschrock 34494451Seschrock if (newvd->vdev_unspare && 34508241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 34518241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) { 34524451Seschrock newvd->vdev_unspare = 0; 34534451Seschrock return (oldvd); 34544451Seschrock } 34554451Seschrock } 34564451Seschrock 34571544Seschrock return (NULL); 3458789Sahrens } 3459789Sahrens 34601544Seschrock static void 34614451Seschrock spa_vdev_resilver_done(spa_t *spa) 3462789Sahrens { 34638241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 34648241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 34658241SJeff.Bonwick@Sun.COM 34668241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3467789Sahrens 34684451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 34698241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 34708241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 34711544Seschrock guid = vd->vdev_guid; 34728241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 34738241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 34748241SJeff.Bonwick@Sun.COM sguid = 0; 34752082Seschrock /* 34762082Seschrock * If we have just finished replacing a hot spared device, then 34772082Seschrock * we need to detach the parent's first child (the original hot 34782082Seschrock * spare) as well. 34792082Seschrock */ 34808241SJeff.Bonwick@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 34812082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 34828241SJeff.Bonwick@Sun.COM ASSERT(ppvd->vdev_children == 2); 34838241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 34842082Seschrock } 34858241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 34868241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 34871544Seschrock return; 34888241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 34892082Seschrock return; 34908241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3491789Sahrens } 3492789Sahrens 34938241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 3494789Sahrens } 3495789Sahrens 3496789Sahrens /* 34971354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 34981354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 34991354Seschrock */ 35001354Seschrock int 35011354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 35021354Seschrock { 35036643Seschrock vdev_t *vd; 35041354Seschrock uint64_t txg; 35051354Seschrock 35061354Seschrock txg = spa_vdev_enter(spa); 35071354Seschrock 35086643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 35092082Seschrock /* 35106643Seschrock * Determine if this is a reference to a hot spare device. If 35116643Seschrock * it is, update the path manually as there is no associated 35126643Seschrock * vdev_t that can be synced to disk. 35132082Seschrock */ 35146643Seschrock nvlist_t **spares; 35156643Seschrock uint_t i, nspares; 35165450Sbrendan 35175450Sbrendan if (spa->spa_spares.sav_config != NULL) { 35185450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 35195450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 35205450Sbrendan &spares, &nspares) == 0); 35212082Seschrock for (i = 0; i < nspares; i++) { 35222082Seschrock uint64_t theguid; 35232082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 35242082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 35255450Sbrendan if (theguid == guid) { 35265450Sbrendan VERIFY(nvlist_add_string(spares[i], 35275450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 35285450Sbrendan spa_load_spares(spa); 35295450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 35305450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 35315450Sbrendan 0)); 35325450Sbrendan } 35332082Seschrock } 35342082Seschrock } 35355450Sbrendan 35365450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 35372082Seschrock } 35381354Seschrock 35391585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 35401585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35411585Sbonwick 35421354Seschrock spa_strfree(vd->vdev_path); 35431354Seschrock vd->vdev_path = spa_strdup(newpath); 35441354Seschrock 35451354Seschrock vdev_config_dirty(vd->vdev_top); 35461354Seschrock 35471354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 35481354Seschrock } 35491354Seschrock 35501354Seschrock /* 3551789Sahrens * ========================================================================== 3552789Sahrens * SPA Scrubbing 3553789Sahrens * ========================================================================== 3554789Sahrens */ 3555789Sahrens 35567046Sahrens int 35577046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3558789Sahrens { 35597754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 35604808Sek110237 3561789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3562789Sahrens return (ENOTSUP); 3563789Sahrens 3564789Sahrens /* 35657046Sahrens * If a resilver was requested, but there is no DTL on a 35667046Sahrens * writeable leaf device, we have nothing to do. 3567789Sahrens */ 35687046Sahrens if (type == POOL_SCRUB_RESILVER && 35697046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 35707046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 35711544Seschrock return (0); 35721544Seschrock } 3573789Sahrens 35747046Sahrens if (type == POOL_SCRUB_EVERYTHING && 35757046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 35767046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 35777046Sahrens return (EBUSY); 35787046Sahrens 35797046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 35807046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 35817046Sahrens } else if (type == POOL_SCRUB_NONE) { 35827046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 35831544Seschrock } else { 35847046Sahrens return (EINVAL); 35851544Seschrock } 3586789Sahrens } 3587789Sahrens 35881544Seschrock /* 35891544Seschrock * ========================================================================== 35901544Seschrock * SPA async task processing 35911544Seschrock * ========================================================================== 35921544Seschrock */ 35931544Seschrock 35941544Seschrock static void 35954451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3596789Sahrens { 35977361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 35987361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 35997361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 36007754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 36017754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 36021544Seschrock } 36037361SBrendan.Gregg@Sun.COM 36047754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 36057361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 36061544Seschrock } 36071544Seschrock 36081544Seschrock static void 36097754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 36107754SJeff.Bonwick@Sun.COM { 36117754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 36127754SJeff.Bonwick@Sun.COM vd->vdev_probe_wanted = 0; 36137754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 36147754SJeff.Bonwick@Sun.COM } 36157754SJeff.Bonwick@Sun.COM 36167754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 36177754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 36187754SJeff.Bonwick@Sun.COM } 36197754SJeff.Bonwick@Sun.COM 36207754SJeff.Bonwick@Sun.COM static void 36211544Seschrock spa_async_thread(spa_t *spa) 36221544Seschrock { 36237754SJeff.Bonwick@Sun.COM int tasks; 36241544Seschrock 36251544Seschrock ASSERT(spa->spa_sync_on); 3626789Sahrens 36271544Seschrock mutex_enter(&spa->spa_async_lock); 36281544Seschrock tasks = spa->spa_async_tasks; 36291544Seschrock spa->spa_async_tasks = 0; 36301544Seschrock mutex_exit(&spa->spa_async_lock); 36311544Seschrock 36321544Seschrock /* 36331635Sbonwick * See if the config needs to be updated. 36341635Sbonwick */ 36351635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 36361635Sbonwick mutex_enter(&spa_namespace_lock); 36371635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 36381635Sbonwick mutex_exit(&spa_namespace_lock); 36391635Sbonwick } 36401635Sbonwick 36411635Sbonwick /* 36424451Seschrock * See if any devices need to be marked REMOVED. 36431544Seschrock */ 36447754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 36457754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 36464451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 36477754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 36487361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 36497754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 36507361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 36517754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 36527754SJeff.Bonwick@Sun.COM } 36537754SJeff.Bonwick@Sun.COM 36547754SJeff.Bonwick@Sun.COM /* 36557754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 36567754SJeff.Bonwick@Sun.COM */ 36577754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 36587754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 36597754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 36607754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 36614451Seschrock } 36621544Seschrock 36631544Seschrock /* 36641544Seschrock * If any devices are done replacing, detach them. 36651544Seschrock */ 36664451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 36674451Seschrock spa_vdev_resilver_done(spa); 3668789Sahrens 36691544Seschrock /* 36701544Seschrock * Kick off a resilver. 36711544Seschrock */ 36727046Sahrens if (tasks & SPA_ASYNC_RESILVER) 36737046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 36741544Seschrock 36751544Seschrock /* 36761544Seschrock * Let the world know that we're done. 36771544Seschrock */ 36781544Seschrock mutex_enter(&spa->spa_async_lock); 36791544Seschrock spa->spa_async_thread = NULL; 36801544Seschrock cv_broadcast(&spa->spa_async_cv); 36811544Seschrock mutex_exit(&spa->spa_async_lock); 36821544Seschrock thread_exit(); 36831544Seschrock } 36841544Seschrock 36851544Seschrock void 36861544Seschrock spa_async_suspend(spa_t *spa) 36871544Seschrock { 36881544Seschrock mutex_enter(&spa->spa_async_lock); 36891544Seschrock spa->spa_async_suspended++; 36901544Seschrock while (spa->spa_async_thread != NULL) 36911544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 36921544Seschrock mutex_exit(&spa->spa_async_lock); 36931544Seschrock } 36941544Seschrock 36951544Seschrock void 36961544Seschrock spa_async_resume(spa_t *spa) 36971544Seschrock { 36981544Seschrock mutex_enter(&spa->spa_async_lock); 36991544Seschrock ASSERT(spa->spa_async_suspended != 0); 37001544Seschrock spa->spa_async_suspended--; 37011544Seschrock mutex_exit(&spa->spa_async_lock); 37021544Seschrock } 37031544Seschrock 37041544Seschrock static void 37051544Seschrock spa_async_dispatch(spa_t *spa) 37061544Seschrock { 37071544Seschrock mutex_enter(&spa->spa_async_lock); 37081544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 37091635Sbonwick spa->spa_async_thread == NULL && 37101635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 37111544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 37121544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 37131544Seschrock mutex_exit(&spa->spa_async_lock); 37141544Seschrock } 37151544Seschrock 37161544Seschrock void 37171544Seschrock spa_async_request(spa_t *spa, int task) 37181544Seschrock { 37191544Seschrock mutex_enter(&spa->spa_async_lock); 37201544Seschrock spa->spa_async_tasks |= task; 37211544Seschrock mutex_exit(&spa->spa_async_lock); 3722789Sahrens } 3723789Sahrens 3724789Sahrens /* 3725789Sahrens * ========================================================================== 3726789Sahrens * SPA syncing routines 3727789Sahrens * ========================================================================== 3728789Sahrens */ 3729789Sahrens 3730789Sahrens static void 3731789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3732789Sahrens { 3733789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3734789Sahrens dmu_tx_t *tx; 3735789Sahrens blkptr_t blk; 3736789Sahrens uint64_t itor = 0; 3737789Sahrens zio_t *zio; 3738789Sahrens int error; 3739789Sahrens uint8_t c = 1; 3740789Sahrens 37417754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 37427754SJeff.Bonwick@Sun.COM 37437754SJeff.Bonwick@Sun.COM while (bplist_iterate(bpl, &itor, &blk) == 0) { 37447754SJeff.Bonwick@Sun.COM ASSERT(blk.blk_birth < txg); 37457754SJeff.Bonwick@Sun.COM zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL, 37467754SJeff.Bonwick@Sun.COM ZIO_FLAG_MUSTSUCCEED)); 37477754SJeff.Bonwick@Sun.COM } 3748789Sahrens 3749789Sahrens error = zio_wait(zio); 3750789Sahrens ASSERT3U(error, ==, 0); 3751789Sahrens 3752789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3753789Sahrens bplist_vacate(bpl, tx); 3754789Sahrens 3755789Sahrens /* 3756789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3757789Sahrens * (Usually only the first block is needed.) 3758789Sahrens */ 3759789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3760789Sahrens dmu_tx_commit(tx); 3761789Sahrens } 3762789Sahrens 3763789Sahrens static void 37642082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 37652082Seschrock { 37662082Seschrock char *packed = NULL; 37677497STim.Haley@Sun.COM size_t bufsize; 37682082Seschrock size_t nvsize = 0; 37692082Seschrock dmu_buf_t *db; 37702082Seschrock 37712082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 37722082Seschrock 37737497STim.Haley@Sun.COM /* 37747497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 37757497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 37767497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 37777497STim.Haley@Sun.COM */ 37787497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 37797497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 37802082Seschrock 37812082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 37822082Seschrock KM_SLEEP) == 0); 37837497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 37847497STim.Haley@Sun.COM 37857497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 37867497STim.Haley@Sun.COM 37877497STim.Haley@Sun.COM kmem_free(packed, bufsize); 37882082Seschrock 37892082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 37902082Seschrock dmu_buf_will_dirty(db, tx); 37912082Seschrock *(uint64_t *)db->db_data = nvsize; 37922082Seschrock dmu_buf_rele(db, FTAG); 37932082Seschrock } 37942082Seschrock 37952082Seschrock static void 37965450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 37975450Sbrendan const char *config, const char *entry) 37982082Seschrock { 37992082Seschrock nvlist_t *nvroot; 38005450Sbrendan nvlist_t **list; 38012082Seschrock int i; 38022082Seschrock 38035450Sbrendan if (!sav->sav_sync) 38042082Seschrock return; 38052082Seschrock 38062082Seschrock /* 38075450Sbrendan * Update the MOS nvlist describing the list of available devices. 38085450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 38094451Seschrock * valid and the vdevs are labeled appropriately. 38102082Seschrock */ 38115450Sbrendan if (sav->sav_object == 0) { 38125450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 38135450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 38145450Sbrendan sizeof (uint64_t), tx); 38152082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 38165450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 38175450Sbrendan &sav->sav_object, tx) == 0); 38182082Seschrock } 38192082Seschrock 38202082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 38215450Sbrendan if (sav->sav_count == 0) { 38225450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 38232082Seschrock } else { 38245450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 38255450Sbrendan for (i = 0; i < sav->sav_count; i++) 38265450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 38275450Sbrendan B_FALSE, B_FALSE, B_TRUE); 38285450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 38295450Sbrendan sav->sav_count) == 0); 38305450Sbrendan for (i = 0; i < sav->sav_count; i++) 38315450Sbrendan nvlist_free(list[i]); 38325450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 38332082Seschrock } 38342082Seschrock 38355450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 38362926Sek110237 nvlist_free(nvroot); 38372082Seschrock 38385450Sbrendan sav->sav_sync = B_FALSE; 38392082Seschrock } 38402082Seschrock 38412082Seschrock static void 3842789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3843789Sahrens { 3844789Sahrens nvlist_t *config; 3845789Sahrens 38467754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 3847789Sahrens return; 3848789Sahrens 38497754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 38507754SJeff.Bonwick@Sun.COM 38517754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 38527754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 38537754SJeff.Bonwick@Sun.COM 38547754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 3855789Sahrens 38561635Sbonwick if (spa->spa_config_syncing) 38571635Sbonwick nvlist_free(spa->spa_config_syncing); 38581635Sbonwick spa->spa_config_syncing = config; 3859789Sahrens 38602082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3861789Sahrens } 3862789Sahrens 38635094Slling /* 38645094Slling * Set zpool properties. 38655094Slling */ 38663912Slling static void 38674543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 38683912Slling { 38693912Slling spa_t *spa = arg1; 38705094Slling objset_t *mos = spa->spa_meta_objset; 38713912Slling nvlist_t *nvp = arg2; 38725094Slling nvpair_t *elem; 38734451Seschrock uint64_t intval; 38746643Seschrock char *strval; 38755094Slling zpool_prop_t prop; 38765094Slling const char *propname; 38775094Slling zprop_type_t proptype; 38785094Slling 38797754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 38807754SJeff.Bonwick@Sun.COM 38815094Slling elem = NULL; 38825094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 38835094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 38845094Slling case ZPOOL_PROP_VERSION: 38855094Slling /* 38865094Slling * Only set version for non-zpool-creation cases 38875094Slling * (set/import). spa_create() needs special care 38885094Slling * for version setting. 38895094Slling */ 38905094Slling if (tx->tx_txg != TXG_INITIAL) { 38915094Slling VERIFY(nvpair_value_uint64(elem, 38925094Slling &intval) == 0); 38935094Slling ASSERT(intval <= SPA_VERSION); 38945094Slling ASSERT(intval >= spa_version(spa)); 38955094Slling spa->spa_uberblock.ub_version = intval; 38965094Slling vdev_config_dirty(spa->spa_root_vdev); 38975094Slling } 38985094Slling break; 38995094Slling 39005094Slling case ZPOOL_PROP_ALTROOT: 39015094Slling /* 39025094Slling * 'altroot' is a non-persistent property. It should 39035094Slling * have been set temporarily at creation or import time. 39045094Slling */ 39055094Slling ASSERT(spa->spa_root != NULL); 39065094Slling break; 39075094Slling 39085363Seschrock case ZPOOL_PROP_CACHEFILE: 39095094Slling /* 39108525SEric.Schrock@Sun.COM * 'cachefile' is also a non-persisitent property. 39115094Slling */ 39124543Smarks break; 39135094Slling default: 39145094Slling /* 39155094Slling * Set pool property values in the poolprops mos object. 39165094Slling */ 39175094Slling if (spa->spa_pool_props_object == 0) { 39185094Slling objset_t *mos = spa->spa_meta_objset; 39195094Slling 39205094Slling VERIFY((spa->spa_pool_props_object = 39215094Slling zap_create(mos, DMU_OT_POOL_PROPS, 39225094Slling DMU_OT_NONE, 0, tx)) > 0); 39235094Slling 39245094Slling VERIFY(zap_update(mos, 39255094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 39265094Slling 8, 1, &spa->spa_pool_props_object, tx) 39275094Slling == 0); 39285094Slling } 39295094Slling 39305094Slling /* normalize the property name */ 39315094Slling propname = zpool_prop_to_name(prop); 39325094Slling proptype = zpool_prop_get_type(prop); 39335094Slling 39345094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 39355094Slling ASSERT(proptype == PROP_TYPE_STRING); 39365094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 39375094Slling VERIFY(zap_update(mos, 39385094Slling spa->spa_pool_props_object, propname, 39395094Slling 1, strlen(strval) + 1, strval, tx) == 0); 39405094Slling 39415094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 39425094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 39435094Slling 39445094Slling if (proptype == PROP_TYPE_INDEX) { 39455094Slling const char *unused; 39465094Slling VERIFY(zpool_prop_index_to_string( 39475094Slling prop, intval, &unused) == 0); 39485094Slling } 39495094Slling VERIFY(zap_update(mos, 39505094Slling spa->spa_pool_props_object, propname, 39515094Slling 8, 1, &intval, tx) == 0); 39525094Slling } else { 39535094Slling ASSERT(0); /* not allowed */ 39545094Slling } 39555094Slling 39565329Sgw25295 switch (prop) { 39575329Sgw25295 case ZPOOL_PROP_DELEGATION: 39585094Slling spa->spa_delegation = intval; 39595329Sgw25295 break; 39605329Sgw25295 case ZPOOL_PROP_BOOTFS: 39615094Slling spa->spa_bootfs = intval; 39625329Sgw25295 break; 39635329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 39645329Sgw25295 spa->spa_failmode = intval; 39655329Sgw25295 break; 39665329Sgw25295 default: 39675329Sgw25295 break; 39685329Sgw25295 } 39693912Slling } 39705094Slling 39715094Slling /* log internal history if this is not a zpool create */ 39725094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 39735094Slling tx->tx_txg != TXG_INITIAL) { 39745094Slling spa_history_internal_log(LOG_POOL_PROPSET, 39755094Slling spa, tx, cr, "%s %lld %s", 39767754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 39775094Slling } 39783912Slling } 39797754SJeff.Bonwick@Sun.COM 39807754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 39813912Slling } 39823912Slling 3983789Sahrens /* 3984789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3985789Sahrens * part of the process, so we iterate until it converges. 3986789Sahrens */ 3987789Sahrens void 3988789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3989789Sahrens { 3990789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3991789Sahrens objset_t *mos = spa->spa_meta_objset; 3992789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 39931635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3994789Sahrens vdev_t *vd; 3995789Sahrens dmu_tx_t *tx; 3996789Sahrens int dirty_vdevs; 39977754SJeff.Bonwick@Sun.COM int error; 3998789Sahrens 3999789Sahrens /* 4000789Sahrens * Lock out configuration changes. 4001789Sahrens */ 40027754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4003789Sahrens 4004789Sahrens spa->spa_syncing_txg = txg; 4005789Sahrens spa->spa_sync_pass = 0; 4006789Sahrens 40077754SJeff.Bonwick@Sun.COM /* 40087754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 40097754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 40107754SJeff.Bonwick@Sun.COM */ 40117754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 40128241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 40138241SJeff.Bonwick@Sun.COM /* 40148241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 40158241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 40168241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 40178241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 40188241SJeff.Bonwick@Sun.COM * into the root vdev tree. 40198241SJeff.Bonwick@Sun.COM */ 40208241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 40218241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 40228241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 40238241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 40248241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 40258241SJeff.Bonwick@Sun.COM } 40268241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 40278241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 40287754SJeff.Bonwick@Sun.COM } 40297754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 40307754SJeff.Bonwick@Sun.COM 40311544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4032789Sahrens 40332082Seschrock tx = dmu_tx_create_assigned(dp, txg); 40342082Seschrock 40352082Seschrock /* 40364577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 40372082Seschrock * set spa_deflate if we have no raid-z vdevs. 40382082Seschrock */ 40394577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 40404577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 40412082Seschrock int i; 40422082Seschrock 40432082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 40442082Seschrock vd = rvd->vdev_child[i]; 40452082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 40462082Seschrock break; 40472082Seschrock } 40482082Seschrock if (i == rvd->vdev_children) { 40492082Seschrock spa->spa_deflate = TRUE; 40502082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 40512082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 40522082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 40532082Seschrock } 40542082Seschrock } 40552082Seschrock 40567046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 40577046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 40587046Sahrens dsl_pool_create_origin(dp, tx); 40597046Sahrens 40607046Sahrens /* Keeping the origin open increases spa_minref */ 40617046Sahrens spa->spa_minref += 3; 40627046Sahrens } 40637046Sahrens 40647046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 40657046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 40667046Sahrens dsl_pool_upgrade_clones(dp, tx); 40677046Sahrens } 40687046Sahrens 4069789Sahrens /* 4070789Sahrens * If anything has changed in this txg, push the deferred frees 4071789Sahrens * from the previous txg. If not, leave them alone so that we 4072789Sahrens * don't generate work on an otherwise idle system. 4073789Sahrens */ 4074789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 40752329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 40762329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 4077789Sahrens spa_sync_deferred_frees(spa, txg); 4078789Sahrens 4079789Sahrens /* 4080789Sahrens * Iterate to convergence. 4081789Sahrens */ 4082789Sahrens do { 4083789Sahrens spa->spa_sync_pass++; 4084789Sahrens 4085789Sahrens spa_sync_config_object(spa, tx); 40865450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 40875450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 40885450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 40895450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 40901544Seschrock spa_errlog_sync(spa, txg); 4091789Sahrens dsl_pool_sync(dp, txg); 4092789Sahrens 4093789Sahrens dirty_vdevs = 0; 4094789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4095789Sahrens vdev_sync(vd, txg); 4096789Sahrens dirty_vdevs++; 4097789Sahrens } 4098789Sahrens 4099789Sahrens bplist_sync(bpl, tx); 4100789Sahrens } while (dirty_vdevs); 4101789Sahrens 4102789Sahrens bplist_close(bpl); 4103789Sahrens 4104789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4105789Sahrens 4106789Sahrens /* 4107789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4108789Sahrens * to commit the transaction group. 41091635Sbonwick * 41105688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 41115688Sbonwick * random top-level vdevs that are known to be visible in the 41127754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 41137754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4114789Sahrens */ 41157754SJeff.Bonwick@Sun.COM for (;;) { 41167754SJeff.Bonwick@Sun.COM /* 41177754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 41187754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 41197754SJeff.Bonwick@Sun.COM */ 41207754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 41217754SJeff.Bonwick@Sun.COM 41227754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 41237754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 41247754SJeff.Bonwick@Sun.COM int svdcount = 0; 41257754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 41267754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 41277754SJeff.Bonwick@Sun.COM int c; 41287754SJeff.Bonwick@Sun.COM 41297754SJeff.Bonwick@Sun.COM for (c = 0; c < children; c++) { 41307754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 41317754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 41327754SJeff.Bonwick@Sun.COM continue; 41337754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 41347754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 41357754SJeff.Bonwick@Sun.COM break; 41367754SJeff.Bonwick@Sun.COM } 41377754SJeff.Bonwick@Sun.COM error = vdev_config_sync(svd, svdcount, txg); 41387754SJeff.Bonwick@Sun.COM } else { 41397754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 41407754SJeff.Bonwick@Sun.COM rvd->vdev_children, txg); 41411635Sbonwick } 41427754SJeff.Bonwick@Sun.COM 41437754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 41447754SJeff.Bonwick@Sun.COM 41457754SJeff.Bonwick@Sun.COM if (error == 0) 41467754SJeff.Bonwick@Sun.COM break; 41477754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 41487754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 41491635Sbonwick } 41502082Seschrock dmu_tx_commit(tx); 41512082Seschrock 41521635Sbonwick /* 41531635Sbonwick * Clear the dirty config list. 41541635Sbonwick */ 41557754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 41561635Sbonwick vdev_config_clean(vd); 41571635Sbonwick 41581635Sbonwick /* 41591635Sbonwick * Now that the new config has synced transactionally, 41601635Sbonwick * let it become visible to the config cache. 41611635Sbonwick */ 41621635Sbonwick if (spa->spa_config_syncing != NULL) { 41631635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 41641635Sbonwick spa->spa_config_txg = txg; 41651635Sbonwick spa->spa_config_syncing = NULL; 41661635Sbonwick } 4167789Sahrens 4168789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4169789Sahrens 4170789Sahrens /* 4171789Sahrens * Clean up the ZIL records for the synced txg. 4172789Sahrens */ 4173789Sahrens dsl_pool_zil_clean(dp); 4174789Sahrens 4175789Sahrens /* 4176789Sahrens * Update usable space statistics. 4177789Sahrens */ 4178789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4179789Sahrens vdev_sync_done(vd, txg); 4180789Sahrens 4181789Sahrens /* 4182789Sahrens * It had better be the case that we didn't dirty anything 41832082Seschrock * since vdev_config_sync(). 4184789Sahrens */ 4185789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4186789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4187789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4188789Sahrens ASSERT(bpl->bpl_queue == NULL); 4189789Sahrens 41907754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 41911544Seschrock 41921544Seschrock /* 41931544Seschrock * If any async tasks have been requested, kick them off. 41941544Seschrock */ 41951544Seschrock spa_async_dispatch(spa); 4196789Sahrens } 4197789Sahrens 4198789Sahrens /* 4199789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4200789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4201789Sahrens * sync. 4202789Sahrens */ 4203789Sahrens void 4204789Sahrens spa_sync_allpools(void) 4205789Sahrens { 4206789Sahrens spa_t *spa = NULL; 4207789Sahrens mutex_enter(&spa_namespace_lock); 4208789Sahrens while ((spa = spa_next(spa)) != NULL) { 42097754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4210789Sahrens continue; 4211789Sahrens spa_open_ref(spa, FTAG); 4212789Sahrens mutex_exit(&spa_namespace_lock); 4213789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4214789Sahrens mutex_enter(&spa_namespace_lock); 4215789Sahrens spa_close(spa, FTAG); 4216789Sahrens } 4217789Sahrens mutex_exit(&spa_namespace_lock); 4218789Sahrens } 4219789Sahrens 4220789Sahrens /* 4221789Sahrens * ========================================================================== 4222789Sahrens * Miscellaneous routines 4223789Sahrens * ========================================================================== 4224789Sahrens */ 4225789Sahrens 4226789Sahrens /* 4227789Sahrens * Remove all pools in the system. 4228789Sahrens */ 4229789Sahrens void 4230789Sahrens spa_evict_all(void) 4231789Sahrens { 4232789Sahrens spa_t *spa; 4233789Sahrens 4234789Sahrens /* 4235789Sahrens * Remove all cached state. All pools should be closed now, 4236789Sahrens * so every spa in the AVL tree should be unreferenced. 4237789Sahrens */ 4238789Sahrens mutex_enter(&spa_namespace_lock); 4239789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4240789Sahrens /* 42411544Seschrock * Stop async tasks. The async thread may need to detach 42421544Seschrock * a device that's been replaced, which requires grabbing 42431544Seschrock * spa_namespace_lock, so we must drop it here. 4244789Sahrens */ 4245789Sahrens spa_open_ref(spa, FTAG); 4246789Sahrens mutex_exit(&spa_namespace_lock); 42471544Seschrock spa_async_suspend(spa); 42484808Sek110237 mutex_enter(&spa_namespace_lock); 4249789Sahrens spa_close(spa, FTAG); 4250789Sahrens 4251789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4252789Sahrens spa_unload(spa); 4253789Sahrens spa_deactivate(spa); 4254789Sahrens } 4255789Sahrens spa_remove(spa); 4256789Sahrens } 4257789Sahrens mutex_exit(&spa_namespace_lock); 4258789Sahrens } 42591544Seschrock 42601544Seschrock vdev_t * 42616643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 42621544Seschrock { 42636643Seschrock vdev_t *vd; 42646643Seschrock int i; 42656643Seschrock 42666643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 42676643Seschrock return (vd); 42686643Seschrock 42696643Seschrock if (l2cache) { 42706643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 42716643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 42726643Seschrock if (vd->vdev_guid == guid) 42736643Seschrock return (vd); 42746643Seschrock } 42756643Seschrock } 42766643Seschrock 42776643Seschrock return (NULL); 42781544Seschrock } 42791760Seschrock 42801760Seschrock void 42815094Slling spa_upgrade(spa_t *spa, uint64_t version) 42821760Seschrock { 42837754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 42841760Seschrock 42851760Seschrock /* 42861760Seschrock * This should only be called for a non-faulted pool, and since a 42871760Seschrock * future version would result in an unopenable pool, this shouldn't be 42881760Seschrock * possible. 42891760Seschrock */ 42904577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 42915094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 42925094Slling 42935094Slling spa->spa_uberblock.ub_version = version; 42941760Seschrock vdev_config_dirty(spa->spa_root_vdev); 42951760Seschrock 42967754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 42972082Seschrock 42982082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 42991760Seschrock } 43002082Seschrock 43012082Seschrock boolean_t 43022082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 43032082Seschrock { 43042082Seschrock int i; 43053377Seschrock uint64_t spareguid; 43065450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 43075450Sbrendan 43085450Sbrendan for (i = 0; i < sav->sav_count; i++) 43095450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 43102082Seschrock return (B_TRUE); 43112082Seschrock 43125450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 43135450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 43145450Sbrendan &spareguid) == 0 && spareguid == guid) 43153377Seschrock return (B_TRUE); 43163377Seschrock } 43173377Seschrock 43182082Seschrock return (B_FALSE); 43192082Seschrock } 43203912Slling 43214451Seschrock /* 43227214Slling * Check if a pool has an active shared spare device. 43237214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 43247214Slling */ 43257214Slling static boolean_t 43267214Slling spa_has_active_shared_spare(spa_t *spa) 43277214Slling { 43287214Slling int i, refcnt; 43297214Slling uint64_t pool; 43307214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 43317214Slling 43327214Slling for (i = 0; i < sav->sav_count; i++) { 43337214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 43347214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 43357214Slling refcnt > 2) 43367214Slling return (B_TRUE); 43377214Slling } 43387214Slling 43397214Slling return (B_FALSE); 43407214Slling } 43417214Slling 43427214Slling /* 43434451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 43444451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 43454451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 43464451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 43474451Seschrock * or zdb as real changes. 43484451Seschrock */ 43494451Seschrock void 43504451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 43514451Seschrock { 43524451Seschrock #ifdef _KERNEL 43534451Seschrock sysevent_t *ev; 43544451Seschrock sysevent_attr_list_t *attr = NULL; 43554451Seschrock sysevent_value_t value; 43564451Seschrock sysevent_id_t eid; 43574451Seschrock 43584451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 43594451Seschrock SE_SLEEP); 43604451Seschrock 43614451Seschrock value.value_type = SE_DATA_TYPE_STRING; 43624451Seschrock value.value.sv_string = spa_name(spa); 43634451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 43644451Seschrock goto done; 43654451Seschrock 43664451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 43674451Seschrock value.value.sv_uint64 = spa_guid(spa); 43684451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 43694451Seschrock goto done; 43704451Seschrock 43714451Seschrock if (vd) { 43724451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 43734451Seschrock value.value.sv_uint64 = vd->vdev_guid; 43744451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 43754451Seschrock SE_SLEEP) != 0) 43764451Seschrock goto done; 43774451Seschrock 43784451Seschrock if (vd->vdev_path) { 43794451Seschrock value.value_type = SE_DATA_TYPE_STRING; 43804451Seschrock value.value.sv_string = vd->vdev_path; 43814451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 43824451Seschrock &value, SE_SLEEP) != 0) 43834451Seschrock goto done; 43844451Seschrock } 43854451Seschrock } 43864451Seschrock 43875756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 43885756Seschrock goto done; 43895756Seschrock attr = NULL; 43905756Seschrock 43914451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 43924451Seschrock 43934451Seschrock done: 43944451Seschrock if (attr) 43954451Seschrock sysevent_free_attr(attr); 43964451Seschrock sysevent_free(ev); 43974451Seschrock #endif 43984451Seschrock } 4399