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 */ 73*9230SGeorge.Wilson@Sun.COM { 8, 8 }, /* ZIO_TYPE_READ */ 74*9230SGeorge.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 */ 6867754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_async_root_lock); 6877754SJeff.Bonwick@Sun.COM while (spa->spa_async_root_count != 0) 6887754SJeff.Bonwick@Sun.COM cv_wait(&spa->spa_async_root_cv, &spa->spa_async_root_lock); 6897754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_async_root_lock); 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 /* 11102082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 11112082Seschrock * value that will be returned by spa_version() since parsing the 11122082Seschrock * configuration requires knowing the version number. 1113789Sahrens */ 11147754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 11152082Seschrock spa->spa_ubsync.ub_version = version; 11162082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 11177754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1118789Sahrens 11192082Seschrock if (error != 0) 11201544Seschrock goto out; 1121789Sahrens 11221585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1123789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1124789Sahrens 1125789Sahrens /* 1126789Sahrens * Try to open all vdevs, loading each label in the process. 1127789Sahrens */ 11287754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 11294070Smc142369 error = vdev_open(rvd); 11307754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 11314070Smc142369 if (error != 0) 11321544Seschrock goto out; 1133789Sahrens 1134789Sahrens /* 11351986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 11367754SJeff.Bonwick@Sun.COM * lock because all label I/O is done with ZIO_FLAG_CONFIG_WRITER. 11371986Seschrock */ 11388241SJeff.Bonwick@Sun.COM if (mosconfig) { 11398241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 11408241SJeff.Bonwick@Sun.COM error = vdev_validate(rvd); 11418241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 11428241SJeff.Bonwick@Sun.COM if (error != 0) 11438241SJeff.Bonwick@Sun.COM goto out; 11448241SJeff.Bonwick@Sun.COM } 11451986Seschrock 11461986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 11471986Seschrock error = ENXIO; 11481986Seschrock goto out; 11491986Seschrock } 11501986Seschrock 11511986Seschrock /* 1152789Sahrens * Find the best uberblock. 1153789Sahrens */ 11547754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1155789Sahrens 1156789Sahrens /* 1157789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1158789Sahrens */ 1159789Sahrens if (ub->ub_txg == 0) { 11601760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11611760Seschrock VDEV_AUX_CORRUPT_DATA); 11621544Seschrock error = ENXIO; 11631544Seschrock goto out; 11641544Seschrock } 11651544Seschrock 11661544Seschrock /* 11671544Seschrock * If the pool is newer than the code, we can't open it. 11681544Seschrock */ 11694577Sahrens if (ub->ub_version > SPA_VERSION) { 11701760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11711760Seschrock VDEV_AUX_VERSION_NEWER); 11721544Seschrock error = ENOTSUP; 11731544Seschrock goto out; 1174789Sahrens } 1175789Sahrens 1176789Sahrens /* 1177789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1178789Sahrens * incomplete configuration. 1179789Sahrens */ 11801732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 11811544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11821544Seschrock VDEV_AUX_BAD_GUID_SUM); 11831544Seschrock error = ENXIO; 11841544Seschrock goto out; 1185789Sahrens } 1186789Sahrens 1187789Sahrens /* 1188789Sahrens * Initialize internal SPA structures. 1189789Sahrens */ 1190789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1191789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1192789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 11931544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 11941544Seschrock if (error) { 11951544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11961544Seschrock VDEV_AUX_CORRUPT_DATA); 11971544Seschrock goto out; 11981544Seschrock } 1199789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1200789Sahrens 12011544Seschrock if (zap_lookup(spa->spa_meta_objset, 1202789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 12031544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 12041544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12051544Seschrock VDEV_AUX_CORRUPT_DATA); 12061544Seschrock error = EIO; 12071544Seschrock goto out; 12081544Seschrock } 1209789Sahrens 1210789Sahrens if (!mosconfig) { 12112082Seschrock nvlist_t *newconfig; 12123975Sek110237 uint64_t hostid; 12132082Seschrock 12142082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 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 12217706SLin.Ling@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig, 12227706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 12233975Sek110237 char *hostname; 12243975Sek110237 unsigned long myhostid = 0; 12253975Sek110237 12263975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 12273975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 12283975Sek110237 12298662SJordan.Vaughan@Sun.com #ifdef _KERNEL 12308662SJordan.Vaughan@Sun.com myhostid = zone_get_hostid(NULL); 12318662SJordan.Vaughan@Sun.com #else /* _KERNEL */ 12328662SJordan.Vaughan@Sun.com /* 12338662SJordan.Vaughan@Sun.com * We're emulating the system's hostid in userland, so 12348662SJordan.Vaughan@Sun.com * we can't use zone_get_hostid(). 12358662SJordan.Vaughan@Sun.com */ 12363975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 12378662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 12384178Slling if (hostid != 0 && myhostid != 0 && 12398662SJordan.Vaughan@Sun.com hostid != myhostid) { 12403975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 12413975Sek110237 "loaded as it was last accessed by " 12427706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 12433975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 12447754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 12453975Sek110237 (unsigned long)hostid); 12463975Sek110237 error = EBADF; 12473975Sek110237 goto out; 12483975Sek110237 } 12493975Sek110237 } 12503975Sek110237 1251789Sahrens spa_config_set(spa, newconfig); 1252789Sahrens spa_unload(spa); 1253789Sahrens spa_deactivate(spa); 12548241SJeff.Bonwick@Sun.COM spa_activate(spa, orig_mode); 1255789Sahrens 12561544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 12571544Seschrock } 12581544Seschrock 12591544Seschrock if (zap_lookup(spa->spa_meta_objset, 12601544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 12611544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 12621544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12631544Seschrock VDEV_AUX_CORRUPT_DATA); 12641544Seschrock error = EIO; 12651544Seschrock goto out; 1266789Sahrens } 1267789Sahrens 12681544Seschrock /* 12692082Seschrock * Load the bit that tells us to use the new accounting function 12702082Seschrock * (raid-z deflation). If we have an older pool, this will not 12712082Seschrock * be present. 12722082Seschrock */ 12732082Seschrock error = zap_lookup(spa->spa_meta_objset, 12742082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 12752082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 12762082Seschrock if (error != 0 && error != ENOENT) { 12772082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12782082Seschrock VDEV_AUX_CORRUPT_DATA); 12792082Seschrock error = EIO; 12802082Seschrock goto out; 12812082Seschrock } 12822082Seschrock 12832082Seschrock /* 12841544Seschrock * Load the persistent error log. If we have an older pool, this will 12851544Seschrock * not be present. 12861544Seschrock */ 12871544Seschrock error = zap_lookup(spa->spa_meta_objset, 12881544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 12891544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 12901807Sbonwick if (error != 0 && error != ENOENT) { 12911544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12921544Seschrock VDEV_AUX_CORRUPT_DATA); 12931544Seschrock error = EIO; 12941544Seschrock goto out; 12951544Seschrock } 12961544Seschrock 12971544Seschrock error = zap_lookup(spa->spa_meta_objset, 12981544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 12991544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 13001544Seschrock if (error != 0 && error != ENOENT) { 13011544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13021544Seschrock VDEV_AUX_CORRUPT_DATA); 13031544Seschrock error = EIO; 13041544Seschrock goto out; 13051544Seschrock } 1306789Sahrens 1307789Sahrens /* 13082926Sek110237 * Load the history object. If we have an older pool, this 13092926Sek110237 * will not be present. 13102926Sek110237 */ 13112926Sek110237 error = zap_lookup(spa->spa_meta_objset, 13122926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 13132926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 13142926Sek110237 if (error != 0 && error != ENOENT) { 13152926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13162926Sek110237 VDEV_AUX_CORRUPT_DATA); 13172926Sek110237 error = EIO; 13182926Sek110237 goto out; 13192926Sek110237 } 13202926Sek110237 13212926Sek110237 /* 13222082Seschrock * Load any hot spares for this pool. 13232082Seschrock */ 13242082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13255450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 13262082Seschrock if (error != 0 && error != ENOENT) { 13272082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13282082Seschrock VDEV_AUX_CORRUPT_DATA); 13292082Seschrock error = EIO; 13302082Seschrock goto out; 13312082Seschrock } 13322082Seschrock if (error == 0) { 13334577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 13345450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 13355450Sbrendan &spa->spa_spares.sav_config) != 0) { 13362082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13372082Seschrock VDEV_AUX_CORRUPT_DATA); 13382082Seschrock error = EIO; 13392082Seschrock goto out; 13402082Seschrock } 13412082Seschrock 13427754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13432082Seschrock spa_load_spares(spa); 13447754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13452082Seschrock } 13462082Seschrock 13475450Sbrendan /* 13485450Sbrendan * Load any level 2 ARC devices for this pool. 13495450Sbrendan */ 13505450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13515450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 13525450Sbrendan &spa->spa_l2cache.sav_object); 13535450Sbrendan if (error != 0 && error != ENOENT) { 13545450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13555450Sbrendan VDEV_AUX_CORRUPT_DATA); 13565450Sbrendan error = EIO; 13575450Sbrendan goto out; 13585450Sbrendan } 13595450Sbrendan if (error == 0) { 13605450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 13615450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 13625450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 13635450Sbrendan vdev_set_state(rvd, B_TRUE, 13645450Sbrendan VDEV_STATE_CANT_OPEN, 13655450Sbrendan VDEV_AUX_CORRUPT_DATA); 13665450Sbrendan error = EIO; 13675450Sbrendan goto out; 13685450Sbrendan } 13695450Sbrendan 13707754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13715450Sbrendan spa_load_l2cache(spa); 13727754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13735450Sbrendan } 13745450Sbrendan 13757294Sperrin if (spa_check_logs(spa)) { 13767294Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13777294Sperrin VDEV_AUX_BAD_LOG); 13787294Sperrin error = ENXIO; 13797294Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 13807294Sperrin goto out; 13817294Sperrin } 13827294Sperrin 13837294Sperrin 13845094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 13854543Smarks 13863912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 13873912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 13883912Slling 13893912Slling if (error && error != ENOENT) { 13903912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13913912Slling VDEV_AUX_CORRUPT_DATA); 13923912Slling error = EIO; 13933912Slling goto out; 13943912Slling } 13953912Slling 13963912Slling if (error == 0) { 13973912Slling (void) zap_lookup(spa->spa_meta_objset, 13983912Slling spa->spa_pool_props_object, 13994451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 14003912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 14014451Seschrock (void) zap_lookup(spa->spa_meta_objset, 14024451Seschrock spa->spa_pool_props_object, 14034451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 14044451Seschrock sizeof (uint64_t), 1, &autoreplace); 14054543Smarks (void) zap_lookup(spa->spa_meta_objset, 14064543Smarks spa->spa_pool_props_object, 14074543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 14084543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 14095329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 14105329Sgw25295 spa->spa_pool_props_object, 14115329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 14125329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 14133912Slling } 14143912Slling 14152082Seschrock /* 14164451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 14174451Seschrock * the ZFS DE that it should not issue any faults for unopenable 14184451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 14194451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 14204451Seschrock * over. 14214451Seschrock */ 14225756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 14234451Seschrock spa_check_removed(spa->spa_root_vdev); 14244451Seschrock 14254451Seschrock /* 14261986Seschrock * Load the vdev state for all toplevel vdevs. 1427789Sahrens */ 14281986Seschrock vdev_load(rvd); 1429789Sahrens 1430789Sahrens /* 1431789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1432789Sahrens */ 14337754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1434789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 14357754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1436789Sahrens 1437789Sahrens /* 1438789Sahrens * Check the state of the root vdev. If it can't be opened, it 1439789Sahrens * indicates one or more toplevel vdevs are faulted. 1440789Sahrens */ 14411544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 14421544Seschrock error = ENXIO; 14431544Seschrock goto out; 14441544Seschrock } 1445789Sahrens 14468241SJeff.Bonwick@Sun.COM if (spa_writeable(spa)) { 14471635Sbonwick dmu_tx_t *tx; 14481635Sbonwick int need_update = B_FALSE; 14498241SJeff.Bonwick@Sun.COM 14508241SJeff.Bonwick@Sun.COM ASSERT(state != SPA_LOAD_TRYIMPORT); 14511601Sbonwick 14521635Sbonwick /* 14531635Sbonwick * Claim log blocks that haven't been committed yet. 14541635Sbonwick * This must all happen in a single txg. 14551635Sbonwick */ 14561601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1457789Sahrens spa_first_txg(spa)); 14587754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 14592417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1460789Sahrens dmu_tx_commit(tx); 1461789Sahrens 1462789Sahrens spa->spa_sync_on = B_TRUE; 1463789Sahrens txg_sync_start(spa->spa_dsl_pool); 1464789Sahrens 1465789Sahrens /* 1466789Sahrens * Wait for all claims to sync. 1467789Sahrens */ 1468789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 14691585Sbonwick 14701585Sbonwick /* 14711635Sbonwick * If the config cache is stale, or we have uninitialized 14721635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 14731585Sbonwick */ 14741635Sbonwick if (config_cache_txg != spa->spa_config_txg || 14751635Sbonwick state == SPA_LOAD_IMPORT) 14761635Sbonwick need_update = B_TRUE; 14771635Sbonwick 14788241SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) 14791635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 14801635Sbonwick need_update = B_TRUE; 14811585Sbonwick 14821585Sbonwick /* 14831635Sbonwick * Update the config cache asychronously in case we're the 14841635Sbonwick * root pool, in which case the config cache isn't writable yet. 14851585Sbonwick */ 14861635Sbonwick if (need_update) 14871635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 14888241SJeff.Bonwick@Sun.COM 14898241SJeff.Bonwick@Sun.COM /* 14908241SJeff.Bonwick@Sun.COM * Check all DTLs to see if anything needs resilvering. 14918241SJeff.Bonwick@Sun.COM */ 14928241SJeff.Bonwick@Sun.COM if (vdev_resilver_needed(rvd, NULL, NULL)) 14938241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 1494789Sahrens } 1495789Sahrens 14961544Seschrock error = 0; 14971544Seschrock out: 14987046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 14992082Seschrock if (error && error != EBADF) 15007294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 15011544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 15021544Seschrock spa->spa_ena = 0; 15031544Seschrock 15041544Seschrock return (error); 1505789Sahrens } 1506789Sahrens 1507789Sahrens /* 1508789Sahrens * Pool Open/Import 1509789Sahrens * 1510789Sahrens * The import case is identical to an open except that the configuration is sent 1511789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1512789Sahrens * case of an open, the pool configuration will exist in the 15134451Seschrock * POOL_STATE_UNINITIALIZED state. 1514789Sahrens * 1515789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1516789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1517789Sahrens * ambiguous state. 1518789Sahrens */ 1519789Sahrens static int 1520789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1521789Sahrens { 1522789Sahrens spa_t *spa; 1523789Sahrens int error; 1524789Sahrens int locked = B_FALSE; 1525789Sahrens 1526789Sahrens *spapp = NULL; 1527789Sahrens 1528789Sahrens /* 1529789Sahrens * As disgusting as this is, we need to support recursive calls to this 1530789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1531789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1532789Sahrens * avoid dsl_dir_open() calling this in the first place. 1533789Sahrens */ 1534789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1535789Sahrens mutex_enter(&spa_namespace_lock); 1536789Sahrens locked = B_TRUE; 1537789Sahrens } 1538789Sahrens 1539789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1540789Sahrens if (locked) 1541789Sahrens mutex_exit(&spa_namespace_lock); 1542789Sahrens return (ENOENT); 1543789Sahrens } 1544789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1545789Sahrens 15468241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1547789Sahrens 15481635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1549789Sahrens 1550789Sahrens if (error == EBADF) { 1551789Sahrens /* 15521986Seschrock * If vdev_validate() returns failure (indicated by 15531986Seschrock * EBADF), it indicates that one of the vdevs indicates 15541986Seschrock * that the pool has been exported or destroyed. If 15551986Seschrock * this is the case, the config cache is out of sync and 15561986Seschrock * we should remove the pool from the namespace. 1557789Sahrens */ 1558789Sahrens spa_unload(spa); 1559789Sahrens spa_deactivate(spa); 15606643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1561789Sahrens spa_remove(spa); 1562789Sahrens if (locked) 1563789Sahrens mutex_exit(&spa_namespace_lock); 1564789Sahrens return (ENOENT); 15651544Seschrock } 15661544Seschrock 15671544Seschrock if (error) { 1568789Sahrens /* 1569789Sahrens * We can't open the pool, but we still have useful 1570789Sahrens * information: the state of each vdev after the 1571789Sahrens * attempted vdev_open(). Return this to the user. 1572789Sahrens */ 15737754SJeff.Bonwick@Sun.COM if (config != NULL && spa->spa_root_vdev != NULL) 1574789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1575789Sahrens B_TRUE); 1576789Sahrens spa_unload(spa); 1577789Sahrens spa_deactivate(spa); 15781544Seschrock spa->spa_last_open_failed = B_TRUE; 1579789Sahrens if (locked) 1580789Sahrens mutex_exit(&spa_namespace_lock); 1581789Sahrens *spapp = NULL; 1582789Sahrens return (error); 15831544Seschrock } else { 15841544Seschrock spa->spa_last_open_failed = B_FALSE; 1585789Sahrens } 1586789Sahrens } 1587789Sahrens 1588789Sahrens spa_open_ref(spa, tag); 15894451Seschrock 1590789Sahrens if (locked) 1591789Sahrens mutex_exit(&spa_namespace_lock); 1592789Sahrens 1593789Sahrens *spapp = spa; 1594789Sahrens 15957754SJeff.Bonwick@Sun.COM if (config != NULL) 1596789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1597789Sahrens 1598789Sahrens return (0); 1599789Sahrens } 1600789Sahrens 1601789Sahrens int 1602789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1603789Sahrens { 1604789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1605789Sahrens } 1606789Sahrens 16071544Seschrock /* 16081544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 16091544Seschrock * preventing it from being exported or destroyed. 16101544Seschrock */ 16111544Seschrock spa_t * 16121544Seschrock spa_inject_addref(char *name) 16131544Seschrock { 16141544Seschrock spa_t *spa; 16151544Seschrock 16161544Seschrock mutex_enter(&spa_namespace_lock); 16171544Seschrock if ((spa = spa_lookup(name)) == NULL) { 16181544Seschrock mutex_exit(&spa_namespace_lock); 16191544Seschrock return (NULL); 16201544Seschrock } 16211544Seschrock spa->spa_inject_ref++; 16221544Seschrock mutex_exit(&spa_namespace_lock); 16231544Seschrock 16241544Seschrock return (spa); 16251544Seschrock } 16261544Seschrock 16271544Seschrock void 16281544Seschrock spa_inject_delref(spa_t *spa) 16291544Seschrock { 16301544Seschrock mutex_enter(&spa_namespace_lock); 16311544Seschrock spa->spa_inject_ref--; 16321544Seschrock mutex_exit(&spa_namespace_lock); 16331544Seschrock } 16341544Seschrock 16355450Sbrendan /* 16365450Sbrendan * Add spares device information to the nvlist. 16375450Sbrendan */ 16382082Seschrock static void 16392082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 16402082Seschrock { 16412082Seschrock nvlist_t **spares; 16422082Seschrock uint_t i, nspares; 16432082Seschrock nvlist_t *nvroot; 16442082Seschrock uint64_t guid; 16452082Seschrock vdev_stat_t *vs; 16462082Seschrock uint_t vsc; 16473377Seschrock uint64_t pool; 16482082Seschrock 16495450Sbrendan if (spa->spa_spares.sav_count == 0) 16502082Seschrock return; 16512082Seschrock 16522082Seschrock VERIFY(nvlist_lookup_nvlist(config, 16532082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 16545450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 16552082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 16562082Seschrock if (nspares != 0) { 16572082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 16582082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 16592082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 16602082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 16612082Seschrock 16622082Seschrock /* 16632082Seschrock * Go through and find any spares which have since been 16642082Seschrock * repurposed as an active spare. If this is the case, update 16652082Seschrock * their status appropriately. 16662082Seschrock */ 16672082Seschrock for (i = 0; i < nspares; i++) { 16682082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 16692082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 16707214Slling if (spa_spare_exists(guid, &pool, NULL) && 16717214Slling pool != 0ULL) { 16722082Seschrock VERIFY(nvlist_lookup_uint64_array( 16732082Seschrock spares[i], ZPOOL_CONFIG_STATS, 16742082Seschrock (uint64_t **)&vs, &vsc) == 0); 16752082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 16762082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 16772082Seschrock } 16782082Seschrock } 16792082Seschrock } 16802082Seschrock } 16812082Seschrock 16825450Sbrendan /* 16835450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 16845450Sbrendan */ 16855450Sbrendan static void 16865450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 16875450Sbrendan { 16885450Sbrendan nvlist_t **l2cache; 16895450Sbrendan uint_t i, j, nl2cache; 16905450Sbrendan nvlist_t *nvroot; 16915450Sbrendan uint64_t guid; 16925450Sbrendan vdev_t *vd; 16935450Sbrendan vdev_stat_t *vs; 16945450Sbrendan uint_t vsc; 16955450Sbrendan 16965450Sbrendan if (spa->spa_l2cache.sav_count == 0) 16975450Sbrendan return; 16985450Sbrendan 16997754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 17005450Sbrendan 17015450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 17025450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 17035450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 17045450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 17055450Sbrendan if (nl2cache != 0) { 17065450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 17075450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 17085450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 17095450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 17105450Sbrendan 17115450Sbrendan /* 17125450Sbrendan * Update level 2 cache device stats. 17135450Sbrendan */ 17145450Sbrendan 17155450Sbrendan for (i = 0; i < nl2cache; i++) { 17165450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 17175450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 17185450Sbrendan 17195450Sbrendan vd = NULL; 17205450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 17215450Sbrendan if (guid == 17225450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 17235450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 17245450Sbrendan break; 17255450Sbrendan } 17265450Sbrendan } 17275450Sbrendan ASSERT(vd != NULL); 17285450Sbrendan 17295450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 17305450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 17315450Sbrendan vdev_get_stats(vd, vs); 17325450Sbrendan } 17335450Sbrendan } 17345450Sbrendan 17357754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 17365450Sbrendan } 17375450Sbrendan 1738789Sahrens int 17391544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1740789Sahrens { 1741789Sahrens int error; 1742789Sahrens spa_t *spa; 1743789Sahrens 1744789Sahrens *config = NULL; 1745789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1746789Sahrens 17472082Seschrock if (spa && *config != NULL) { 17481544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 17491544Seschrock spa_get_errlog_size(spa)) == 0); 17501544Seschrock 17517754SJeff.Bonwick@Sun.COM if (spa_suspended(spa)) 17527754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 17537754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0); 17547754SJeff.Bonwick@Sun.COM 17552082Seschrock spa_add_spares(spa, *config); 17565450Sbrendan spa_add_l2cache(spa, *config); 17572082Seschrock } 17582082Seschrock 17591544Seschrock /* 17601544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 17611544Seschrock * and call spa_lookup() directly. 17621544Seschrock */ 17631544Seschrock if (altroot) { 17641544Seschrock if (spa == NULL) { 17651544Seschrock mutex_enter(&spa_namespace_lock); 17661544Seschrock spa = spa_lookup(name); 17671544Seschrock if (spa) 17681544Seschrock spa_altroot(spa, altroot, buflen); 17691544Seschrock else 17701544Seschrock altroot[0] = '\0'; 17711544Seschrock spa = NULL; 17721544Seschrock mutex_exit(&spa_namespace_lock); 17731544Seschrock } else { 17741544Seschrock spa_altroot(spa, altroot, buflen); 17751544Seschrock } 17761544Seschrock } 17771544Seschrock 1778789Sahrens if (spa != NULL) 1779789Sahrens spa_close(spa, FTAG); 1780789Sahrens 1781789Sahrens return (error); 1782789Sahrens } 1783789Sahrens 1784789Sahrens /* 17855450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 17865450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 17875450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 17885450Sbrendan * specified, as long as they are well-formed. 17892082Seschrock */ 17902082Seschrock static int 17915450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 17925450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 17935450Sbrendan vdev_labeltype_t label) 17942082Seschrock { 17955450Sbrendan nvlist_t **dev; 17965450Sbrendan uint_t i, ndev; 17972082Seschrock vdev_t *vd; 17982082Seschrock int error; 17992082Seschrock 18007754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 18017754SJeff.Bonwick@Sun.COM 18022082Seschrock /* 18035450Sbrendan * It's acceptable to have no devs specified. 18042082Seschrock */ 18055450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 18062082Seschrock return (0); 18072082Seschrock 18085450Sbrendan if (ndev == 0) 18092082Seschrock return (EINVAL); 18102082Seschrock 18112082Seschrock /* 18125450Sbrendan * Make sure the pool is formatted with a version that supports this 18135450Sbrendan * device type. 18142082Seschrock */ 18155450Sbrendan if (spa_version(spa) < version) 18162082Seschrock return (ENOTSUP); 18172082Seschrock 18183377Seschrock /* 18195450Sbrendan * Set the pending device list so we correctly handle device in-use 18203377Seschrock * checking. 18213377Seschrock */ 18225450Sbrendan sav->sav_pending = dev; 18235450Sbrendan sav->sav_npending = ndev; 18245450Sbrendan 18255450Sbrendan for (i = 0; i < ndev; i++) { 18265450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 18272082Seschrock mode)) != 0) 18283377Seschrock goto out; 18292082Seschrock 18302082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 18312082Seschrock vdev_free(vd); 18323377Seschrock error = EINVAL; 18333377Seschrock goto out; 18342082Seschrock } 18352082Seschrock 18365450Sbrendan /* 18377754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 18387754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 18395450Sbrendan */ 18407754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 18415450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 18425450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 18435450Sbrendan error = ENOTBLK; 18445450Sbrendan goto out; 18455450Sbrendan } 18467754SJeff.Bonwick@Sun.COM #endif 18472082Seschrock vd->vdev_top = vd; 18483377Seschrock 18493377Seschrock if ((error = vdev_open(vd)) == 0 && 18505450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 18515450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 18523377Seschrock vd->vdev_guid) == 0); 18532082Seschrock } 18542082Seschrock 18552082Seschrock vdev_free(vd); 18563377Seschrock 18575450Sbrendan if (error && 18585450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 18593377Seschrock goto out; 18603377Seschrock else 18613377Seschrock error = 0; 18622082Seschrock } 18632082Seschrock 18643377Seschrock out: 18655450Sbrendan sav->sav_pending = NULL; 18665450Sbrendan sav->sav_npending = 0; 18673377Seschrock return (error); 18682082Seschrock } 18692082Seschrock 18705450Sbrendan static int 18715450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 18725450Sbrendan { 18735450Sbrendan int error; 18745450Sbrendan 18757754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 18767754SJeff.Bonwick@Sun.COM 18775450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 18785450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 18795450Sbrendan VDEV_LABEL_SPARE)) != 0) { 18805450Sbrendan return (error); 18815450Sbrendan } 18825450Sbrendan 18835450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 18845450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 18855450Sbrendan VDEV_LABEL_L2CACHE)); 18865450Sbrendan } 18875450Sbrendan 18885450Sbrendan static void 18895450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 18905450Sbrendan const char *config) 18915450Sbrendan { 18925450Sbrendan int i; 18935450Sbrendan 18945450Sbrendan if (sav->sav_config != NULL) { 18955450Sbrendan nvlist_t **olddevs; 18965450Sbrendan uint_t oldndevs; 18975450Sbrendan nvlist_t **newdevs; 18985450Sbrendan 18995450Sbrendan /* 19005450Sbrendan * Generate new dev list by concatentating with the 19015450Sbrendan * current dev list. 19025450Sbrendan */ 19035450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 19045450Sbrendan &olddevs, &oldndevs) == 0); 19055450Sbrendan 19065450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 19075450Sbrendan (ndevs + oldndevs), KM_SLEEP); 19085450Sbrendan for (i = 0; i < oldndevs; i++) 19095450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 19105450Sbrendan KM_SLEEP) == 0); 19115450Sbrendan for (i = 0; i < ndevs; i++) 19125450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 19135450Sbrendan KM_SLEEP) == 0); 19145450Sbrendan 19155450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 19165450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 19175450Sbrendan 19185450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 19195450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 19205450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 19215450Sbrendan nvlist_free(newdevs[i]); 19225450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 19235450Sbrendan } else { 19245450Sbrendan /* 19255450Sbrendan * Generate a new dev list. 19265450Sbrendan */ 19275450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 19285450Sbrendan KM_SLEEP) == 0); 19295450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 19305450Sbrendan devs, ndevs) == 0); 19315450Sbrendan } 19325450Sbrendan } 19335450Sbrendan 19345450Sbrendan /* 19355450Sbrendan * Stop and drop level 2 ARC devices 19365450Sbrendan */ 19375450Sbrendan void 19385450Sbrendan spa_l2cache_drop(spa_t *spa) 19395450Sbrendan { 19405450Sbrendan vdev_t *vd; 19415450Sbrendan int i; 19425450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 19435450Sbrendan 19445450Sbrendan for (i = 0; i < sav->sav_count; i++) { 19455450Sbrendan uint64_t pool; 19465450Sbrendan 19475450Sbrendan vd = sav->sav_vdevs[i]; 19485450Sbrendan ASSERT(vd != NULL); 19495450Sbrendan 19508241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 19518241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 19525450Sbrendan l2arc_remove_vdev(vd); 19535450Sbrendan if (vd->vdev_isl2cache) 19545450Sbrendan spa_l2cache_remove(vd); 19555450Sbrendan vdev_clear_stats(vd); 19565450Sbrendan (void) vdev_close(vd); 19575450Sbrendan } 19585450Sbrendan } 19595450Sbrendan 19602082Seschrock /* 1961789Sahrens * Pool Creation 1962789Sahrens */ 1963789Sahrens int 19645094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 19657184Stimh const char *history_str, nvlist_t *zplprops) 1966789Sahrens { 1967789Sahrens spa_t *spa; 19685094Slling char *altroot = NULL; 19691635Sbonwick vdev_t *rvd; 1970789Sahrens dsl_pool_t *dp; 1971789Sahrens dmu_tx_t *tx; 19722082Seschrock int c, error = 0; 1973789Sahrens uint64_t txg = TXG_INITIAL; 19745450Sbrendan nvlist_t **spares, **l2cache; 19755450Sbrendan uint_t nspares, nl2cache; 19765094Slling uint64_t version; 1977789Sahrens 1978789Sahrens /* 1979789Sahrens * If this pool already exists, return failure. 1980789Sahrens */ 1981789Sahrens mutex_enter(&spa_namespace_lock); 1982789Sahrens if (spa_lookup(pool) != NULL) { 1983789Sahrens mutex_exit(&spa_namespace_lock); 1984789Sahrens return (EEXIST); 1985789Sahrens } 1986789Sahrens 1987789Sahrens /* 1988789Sahrens * Allocate a new spa_t structure. 1989789Sahrens */ 19905094Slling (void) nvlist_lookup_string(props, 19915094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 19921635Sbonwick spa = spa_add(pool, altroot); 19938241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1994789Sahrens 1995789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 19965094Slling 19975094Slling if (props && (error = spa_prop_validate(spa, props))) { 19985094Slling spa_unload(spa); 19995094Slling spa_deactivate(spa); 20005094Slling spa_remove(spa); 20016643Seschrock mutex_exit(&spa_namespace_lock); 20025094Slling return (error); 20035094Slling } 20045094Slling 20055094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 20065094Slling &version) != 0) 20075094Slling version = SPA_VERSION; 20085094Slling ASSERT(version <= SPA_VERSION); 20095094Slling spa->spa_uberblock.ub_version = version; 2010789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2011789Sahrens 20121635Sbonwick /* 20131635Sbonwick * Create the root vdev. 20141635Sbonwick */ 20157754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20161635Sbonwick 20172082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 20182082Seschrock 20192082Seschrock ASSERT(error != 0 || rvd != NULL); 20202082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 20212082Seschrock 20225913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 20231635Sbonwick error = EINVAL; 20242082Seschrock 20252082Seschrock if (error == 0 && 20262082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 20275450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 20282082Seschrock VDEV_ALLOC_ADD)) == 0) { 20292082Seschrock for (c = 0; c < rvd->vdev_children; c++) 20302082Seschrock vdev_init(rvd->vdev_child[c], txg); 20312082Seschrock vdev_config_dirty(rvd); 20321635Sbonwick } 20331635Sbonwick 20347754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2035789Sahrens 20362082Seschrock if (error != 0) { 2037789Sahrens spa_unload(spa); 2038789Sahrens spa_deactivate(spa); 2039789Sahrens spa_remove(spa); 2040789Sahrens mutex_exit(&spa_namespace_lock); 2041789Sahrens return (error); 2042789Sahrens } 2043789Sahrens 20442082Seschrock /* 20452082Seschrock * Get the list of spares, if specified. 20462082Seschrock */ 20472082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 20482082Seschrock &spares, &nspares) == 0) { 20495450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 20502082Seschrock KM_SLEEP) == 0); 20515450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 20522082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 20537754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20542082Seschrock spa_load_spares(spa); 20557754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 20565450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 20575450Sbrendan } 20585450Sbrendan 20595450Sbrendan /* 20605450Sbrendan * Get the list of level 2 cache devices, if specified. 20615450Sbrendan */ 20625450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 20635450Sbrendan &l2cache, &nl2cache) == 0) { 20645450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 20655450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 20665450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 20675450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 20687754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20695450Sbrendan spa_load_l2cache(spa); 20707754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 20715450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 20722082Seschrock } 20732082Seschrock 20747184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2075789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2076789Sahrens 2077789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2078789Sahrens 2079789Sahrens /* 2080789Sahrens * Create the pool config object. 2081789Sahrens */ 2082789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 20837497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2084789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2085789Sahrens 20861544Seschrock if (zap_add(spa->spa_meta_objset, 2087789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 20881544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 20891544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 20901544Seschrock } 2091789Sahrens 20925094Slling /* Newly created pools with the right version are always deflated. */ 20935094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 20945094Slling spa->spa_deflate = TRUE; 20955094Slling if (zap_add(spa->spa_meta_objset, 20965094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 20975094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 20985094Slling cmn_err(CE_PANIC, "failed to add deflate"); 20995094Slling } 21002082Seschrock } 21012082Seschrock 2102789Sahrens /* 2103789Sahrens * Create the deferred-free bplist object. Turn off compression 2104789Sahrens * because sync-to-convergence takes longer if the blocksize 2105789Sahrens * keeps changing. 2106789Sahrens */ 2107789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2108789Sahrens 1 << 14, tx); 2109789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2110789Sahrens ZIO_COMPRESS_OFF, tx); 2111789Sahrens 21121544Seschrock if (zap_add(spa->spa_meta_objset, 2113789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 21141544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 21151544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 21161544Seschrock } 2117789Sahrens 21182926Sek110237 /* 21192926Sek110237 * Create the pool's history object. 21202926Sek110237 */ 21215094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 21225094Slling spa_history_create_obj(spa, tx); 21235094Slling 21245094Slling /* 21255094Slling * Set pool properties. 21265094Slling */ 21275094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 21285094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 21295329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 21308525SEric.Schrock@Sun.COM if (props != NULL) { 21318525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 21325094Slling spa_sync_props(spa, props, CRED(), tx); 21338525SEric.Schrock@Sun.COM } 21342926Sek110237 2135789Sahrens dmu_tx_commit(tx); 2136789Sahrens 2137789Sahrens spa->spa_sync_on = B_TRUE; 2138789Sahrens txg_sync_start(spa->spa_dsl_pool); 2139789Sahrens 2140789Sahrens /* 2141789Sahrens * We explicitly wait for the first transaction to complete so that our 2142789Sahrens * bean counters are appropriately updated. 2143789Sahrens */ 2144789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2145789Sahrens 21466643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2147789Sahrens 21485094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 21494715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 21504715Sek110237 21518667SGeorge.Wilson@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 21528667SGeorge.Wilson@Sun.COM 2153789Sahrens mutex_exit(&spa_namespace_lock); 2154789Sahrens 2155789Sahrens return (0); 2156789Sahrens } 2157789Sahrens 2158789Sahrens /* 2159789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2160789Sahrens * then call spa_load() to do the dirty work. 2161789Sahrens */ 21626423Sgw25295 static int 21636423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 21646643Seschrock boolean_t isroot, boolean_t allowfaulted) 2165789Sahrens { 2166789Sahrens spa_t *spa; 21675094Slling char *altroot = NULL; 21686643Seschrock int error, loaderr; 21692082Seschrock nvlist_t *nvroot; 21705450Sbrendan nvlist_t **spares, **l2cache; 21715450Sbrendan uint_t nspares, nl2cache; 2172789Sahrens 2173789Sahrens /* 2174789Sahrens * If a pool with this name exists, return failure. 2175789Sahrens */ 2176789Sahrens mutex_enter(&spa_namespace_lock); 21777897SLin.Ling@Sun.COM if ((spa = spa_lookup(pool)) != NULL) { 21787897SLin.Ling@Sun.COM if (isroot) { 21797897SLin.Ling@Sun.COM /* 21807897SLin.Ling@Sun.COM * Remove the existing root pool from the 21817897SLin.Ling@Sun.COM * namespace so that we can replace it with 21827897SLin.Ling@Sun.COM * the correct config we just read in. 21837897SLin.Ling@Sun.COM */ 21847897SLin.Ling@Sun.COM ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 21857897SLin.Ling@Sun.COM spa_remove(spa); 21867897SLin.Ling@Sun.COM } else { 21877897SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 21887897SLin.Ling@Sun.COM return (EEXIST); 21897897SLin.Ling@Sun.COM } 2190789Sahrens } 2191789Sahrens 2192789Sahrens /* 21931635Sbonwick * Create and initialize the spa structure. 2194789Sahrens */ 21955094Slling (void) nvlist_lookup_string(props, 21965094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 21971635Sbonwick spa = spa_add(pool, altroot); 21988241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2199789Sahrens 22006643Seschrock if (allowfaulted) 22016643Seschrock spa->spa_import_faulted = B_TRUE; 22026673Seschrock spa->spa_is_root = isroot; 22036643Seschrock 2204789Sahrens /* 22051635Sbonwick * Pass off the heavy lifting to spa_load(). 22067046Sahrens * Pass TRUE for mosconfig (unless this is a root pool) because 22077046Sahrens * the user-supplied config is actually the one to trust when 22087046Sahrens * doing an import. 22091601Sbonwick */ 22107046Sahrens loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot); 2211789Sahrens 22127754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22132082Seschrock /* 22142082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 22152082Seschrock * and conflicts with spa_has_spare(). 22162082Seschrock */ 22176423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 22185450Sbrendan nvlist_free(spa->spa_spares.sav_config); 22195450Sbrendan spa->spa_spares.sav_config = NULL; 22202082Seschrock spa_load_spares(spa); 22212082Seschrock } 22226423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 22235450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 22245450Sbrendan spa->spa_l2cache.sav_config = NULL; 22255450Sbrendan spa_load_l2cache(spa); 22265450Sbrendan } 22272082Seschrock 22282082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 22292082Seschrock &nvroot) == 0); 22305450Sbrendan if (error == 0) 22315450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 22325450Sbrendan if (error == 0) 22335450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 22345450Sbrendan VDEV_ALLOC_L2CACHE); 22357754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 22362082Seschrock 22378525SEric.Schrock@Sun.COM if (props != NULL) 22388525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 22398525SEric.Schrock@Sun.COM 22408241SJeff.Bonwick@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 22418241SJeff.Bonwick@Sun.COM (error = spa_prop_set(spa, props)))) { 22426643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 22436643Seschrock /* 22446643Seschrock * If we failed to load the pool, but 'allowfaulted' is 22456643Seschrock * set, then manually set the config as if the config 22466643Seschrock * passed in was specified in the cache file. 22476643Seschrock */ 22486643Seschrock error = 0; 22496643Seschrock spa->spa_import_faulted = B_FALSE; 22507754SJeff.Bonwick@Sun.COM if (spa->spa_config == NULL) 22516643Seschrock spa->spa_config = spa_config_generate(spa, 22526643Seschrock NULL, -1ULL, B_TRUE); 22536643Seschrock spa_unload(spa); 22546643Seschrock spa_deactivate(spa); 22556643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 22566643Seschrock } else { 22576643Seschrock spa_unload(spa); 22586643Seschrock spa_deactivate(spa); 22596643Seschrock spa_remove(spa); 22606643Seschrock } 2261789Sahrens mutex_exit(&spa_namespace_lock); 2262789Sahrens return (error); 2263789Sahrens } 2264789Sahrens 22651635Sbonwick /* 22665450Sbrendan * Override any spares and level 2 cache devices as specified by 22675450Sbrendan * the user, as these may have correct device names/devids, etc. 22682082Seschrock */ 22692082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 22702082Seschrock &spares, &nspares) == 0) { 22715450Sbrendan if (spa->spa_spares.sav_config) 22725450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 22732082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 22742082Seschrock else 22755450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 22762082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 22775450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 22782082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 22797754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22802082Seschrock spa_load_spares(spa); 22817754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 22825450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 22835450Sbrendan } 22845450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 22855450Sbrendan &l2cache, &nl2cache) == 0) { 22865450Sbrendan if (spa->spa_l2cache.sav_config) 22875450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 22885450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 22895450Sbrendan else 22905450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 22915450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 22925450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 22935450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 22947754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22955450Sbrendan spa_load_l2cache(spa); 22967754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 22975450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 22982082Seschrock } 22992082Seschrock 23008241SJeff.Bonwick@Sun.COM if (spa_writeable(spa)) { 23016643Seschrock /* 23026643Seschrock * Update the config cache to include the newly-imported pool. 23036643Seschrock */ 23046423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 23056643Seschrock } 23066643Seschrock 23076643Seschrock spa->spa_import_faulted = B_FALSE; 23084451Seschrock mutex_exit(&spa_namespace_lock); 23094451Seschrock 2310789Sahrens return (0); 2311789Sahrens } 2312789Sahrens 23136423Sgw25295 #ifdef _KERNEL 23146423Sgw25295 /* 23156423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 23166423Sgw25295 * device label. 23176423Sgw25295 */ 23186423Sgw25295 static void 23196423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 23206423Sgw25295 { 23216423Sgw25295 nvlist_t *nvtop, *nvroot; 23226423Sgw25295 uint64_t pgid; 23236423Sgw25295 23246423Sgw25295 /* 23256423Sgw25295 * Add this top-level vdev to the child array. 23266423Sgw25295 */ 23276423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 23286423Sgw25295 == 0); 23296423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 23306423Sgw25295 == 0); 23316423Sgw25295 23326423Sgw25295 /* 23336423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 23346423Sgw25295 */ 23356423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 23366423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 23376423Sgw25295 == 0); 23386423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 23396423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 23406423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 23416423Sgw25295 &nvtop, 1) == 0); 23426423Sgw25295 23436423Sgw25295 /* 23446423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 23456423Sgw25295 * this pool's configuration (remove the old, add the new). 23466423Sgw25295 */ 23476423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 23486423Sgw25295 nvlist_free(nvroot); 23496423Sgw25295 } 23506423Sgw25295 23516423Sgw25295 /* 23526423Sgw25295 * Get the root pool information from the root disk, then import the root pool 23536423Sgw25295 * during the system boot up time. 23546423Sgw25295 */ 23557539SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 23567147Staylor 23577147Staylor int 23587147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf, 23596423Sgw25295 uint64_t *besttxg) 23606423Sgw25295 { 23616423Sgw25295 nvlist_t *config; 23626423Sgw25295 uint64_t txg; 23637539SLin.Ling@Sun.COM int error; 23647539SLin.Ling@Sun.COM 23657539SLin.Ling@Sun.COM if (error = vdev_disk_read_rootlabel(devpath, devid, &config)) 23667539SLin.Ling@Sun.COM return (error); 23676423Sgw25295 23686423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 23696423Sgw25295 23707147Staylor if (bestconf != NULL) 23716423Sgw25295 *bestconf = config; 23727539SLin.Ling@Sun.COM else 23737539SLin.Ling@Sun.COM nvlist_free(config); 23747147Staylor *besttxg = txg; 23757147Staylor return (0); 23766423Sgw25295 } 23776423Sgw25295 23786423Sgw25295 boolean_t 23796423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 23806423Sgw25295 { 23816423Sgw25295 uint64_t ival; 23826423Sgw25295 23836423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 23846423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 23856423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 23866423Sgw25295 return (B_FALSE); 23876423Sgw25295 23886423Sgw25295 return (B_TRUE); 23896423Sgw25295 } 23906423Sgw25295 23917147Staylor 23927147Staylor /* 23937147Staylor * Given the boot device's physical path or devid, check if the device 23947147Staylor * is in a valid state. If so, return the configuration from the vdev 23957147Staylor * label. 23967147Staylor */ 23977147Staylor int 23987147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf) 23997147Staylor { 24007147Staylor nvlist_t *conf = NULL; 24017147Staylor uint64_t txg = 0; 24027147Staylor nvlist_t *nvtop, **child; 24037147Staylor char *type; 24047147Staylor char *bootpath = NULL; 24057147Staylor uint_t children, c; 24067147Staylor char *tmp; 24077539SLin.Ling@Sun.COM int error; 24087147Staylor 24097147Staylor if (devpath && ((tmp = strchr(devpath, ' ')) != NULL)) 24107147Staylor *tmp = '\0'; 24117539SLin.Ling@Sun.COM if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) { 24127147Staylor cmn_err(CE_NOTE, "error reading device label"); 24137539SLin.Ling@Sun.COM return (error); 24147147Staylor } 24157147Staylor if (txg == 0) { 24167147Staylor cmn_err(CE_NOTE, "this device is detached"); 24177147Staylor nvlist_free(conf); 24187147Staylor return (EINVAL); 24197147Staylor } 24207147Staylor 24217147Staylor VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE, 24227147Staylor &nvtop) == 0); 24237147Staylor VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0); 24247147Staylor 24257147Staylor if (strcmp(type, VDEV_TYPE_DISK) == 0) { 24267147Staylor if (spa_rootdev_validate(nvtop)) { 24277147Staylor goto out; 24287147Staylor } else { 24297147Staylor nvlist_free(conf); 24307147Staylor return (EINVAL); 24317147Staylor } 24327147Staylor } 24337147Staylor 24347147Staylor ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0); 24357147Staylor 24367147Staylor VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN, 24377147Staylor &child, &children) == 0); 24387147Staylor 24397147Staylor /* 24407147Staylor * Go thru vdevs in the mirror to see if the given device 24417147Staylor * has the most recent txg. Only the device with the most 24427147Staylor * recent txg has valid information and should be booted. 24437147Staylor */ 24447147Staylor for (c = 0; c < children; c++) { 24457147Staylor char *cdevid, *cpath; 24467147Staylor uint64_t tmptxg; 24477147Staylor 24488242SLin.Ling@Sun.COM cpath = NULL; 24498242SLin.Ling@Sun.COM cdevid = NULL; 24507147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH, 24518242SLin.Ling@Sun.COM &cpath) != 0 && nvlist_lookup_string(child[c], 24528242SLin.Ling@Sun.COM ZPOOL_CONFIG_DEVID, &cdevid) != 0) 24537147Staylor return (EINVAL); 24547687SLin.Ling@Sun.COM if ((spa_check_rootconf(cpath, cdevid, NULL, 24557687SLin.Ling@Sun.COM &tmptxg) == 0) && (tmptxg > txg)) { 24567147Staylor txg = tmptxg; 24577147Staylor VERIFY(nvlist_lookup_string(child[c], 24587147Staylor ZPOOL_CONFIG_PATH, &bootpath) == 0); 24597147Staylor } 24607147Staylor } 24617147Staylor 24627147Staylor /* Does the best device match the one we've booted from? */ 24637147Staylor if (bootpath) { 24647147Staylor cmn_err(CE_NOTE, "try booting from '%s'", bootpath); 24657147Staylor return (EINVAL); 24667147Staylor } 24677147Staylor out: 24687147Staylor *bestconf = conf; 24697147Staylor return (0); 24707147Staylor } 24717147Staylor 24726423Sgw25295 /* 24736423Sgw25295 * Import a root pool. 24746423Sgw25295 * 24757147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 24767147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 24777147Staylor * The GRUB "findroot" command will return the vdev we should boot. 24786423Sgw25295 * 24796423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 24806423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 24816423Sgw25295 * e.g. 24826423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 24836423Sgw25295 */ 24846423Sgw25295 int 24857147Staylor spa_import_rootpool(char *devpath, char *devid) 24866423Sgw25295 { 24876423Sgw25295 nvlist_t *conf = NULL; 24886423Sgw25295 char *pname; 24896423Sgw25295 int error; 24906423Sgw25295 24916423Sgw25295 /* 24926423Sgw25295 * Get the vdev pathname and configuation from the most 24936423Sgw25295 * recently updated vdev (highest txg). 24946423Sgw25295 */ 24957147Staylor if (error = spa_get_rootconf(devpath, devid, &conf)) 24966423Sgw25295 goto msg_out; 24976423Sgw25295 24986423Sgw25295 /* 24996423Sgw25295 * Add type "root" vdev to the config. 25006423Sgw25295 */ 25016423Sgw25295 spa_build_rootpool_config(conf); 25026423Sgw25295 25036423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 25046423Sgw25295 25056673Seschrock /* 25066673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 25076673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 25086673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 25096673Seschrock * pool open, not import. 25106673Seschrock */ 25116673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 25127897SLin.Ling@Sun.COM ASSERT(error != EEXIST); 25136423Sgw25295 25146423Sgw25295 nvlist_free(conf); 25156423Sgw25295 return (error); 25166423Sgw25295 25176423Sgw25295 msg_out: 25187147Staylor cmn_err(CE_NOTE, "\n" 25196423Sgw25295 " *************************************************** \n" 25206423Sgw25295 " * This device is not bootable! * \n" 25216423Sgw25295 " * It is either offlined or detached or faulted. * \n" 25226423Sgw25295 " * Please try to boot from a different device. * \n" 25237147Staylor " *************************************************** "); 25246423Sgw25295 25256423Sgw25295 return (error); 25266423Sgw25295 } 25276423Sgw25295 #endif 25286423Sgw25295 25296423Sgw25295 /* 25306423Sgw25295 * Import a non-root pool into the system. 25316423Sgw25295 */ 25326423Sgw25295 int 25336423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 25346423Sgw25295 { 25356643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 25366423Sgw25295 } 25376423Sgw25295 25386643Seschrock int 25396643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 25406643Seschrock { 25416643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 25426643Seschrock } 25436643Seschrock 25446643Seschrock 2545789Sahrens /* 2546789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2547789Sahrens * to get the vdev stats associated with the imported devices. 2548789Sahrens */ 2549789Sahrens #define TRYIMPORT_NAME "$import" 2550789Sahrens 2551789Sahrens nvlist_t * 2552789Sahrens spa_tryimport(nvlist_t *tryconfig) 2553789Sahrens { 2554789Sahrens nvlist_t *config = NULL; 2555789Sahrens char *poolname; 2556789Sahrens spa_t *spa; 2557789Sahrens uint64_t state; 25588680SLin.Ling@Sun.COM int error; 2559789Sahrens 2560789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2561789Sahrens return (NULL); 2562789Sahrens 2563789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2564789Sahrens return (NULL); 2565789Sahrens 25661635Sbonwick /* 25671635Sbonwick * Create and initialize the spa structure. 25681635Sbonwick */ 2569789Sahrens mutex_enter(&spa_namespace_lock); 25701635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 25718241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 2572789Sahrens 2573789Sahrens /* 25741635Sbonwick * Pass off the heavy lifting to spa_load(). 25751732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 25761732Sbonwick * is actually the one to trust when doing an import. 2577789Sahrens */ 25788680SLin.Ling@Sun.COM error = spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2579789Sahrens 2580789Sahrens /* 2581789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2582789Sahrens */ 2583789Sahrens if (spa->spa_root_vdev != NULL) { 2584789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2585789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2586789Sahrens poolname) == 0); 2587789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2588789Sahrens state) == 0); 25893975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 25903975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 25912082Seschrock 25922082Seschrock /* 25936423Sgw25295 * If the bootfs property exists on this pool then we 25946423Sgw25295 * copy it out so that external consumers can tell which 25956423Sgw25295 * pools are bootable. 25966423Sgw25295 */ 25978680SLin.Ling@Sun.COM if ((!error || error == EEXIST) && spa->spa_bootfs) { 25986423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 25996423Sgw25295 26006423Sgw25295 /* 26016423Sgw25295 * We have to play games with the name since the 26026423Sgw25295 * pool was opened as TRYIMPORT_NAME. 26036423Sgw25295 */ 26047754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 26056423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 26066423Sgw25295 char *cp; 26076423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 26086423Sgw25295 26096423Sgw25295 cp = strchr(tmpname, '/'); 26106423Sgw25295 if (cp == NULL) { 26116423Sgw25295 (void) strlcpy(dsname, tmpname, 26126423Sgw25295 MAXPATHLEN); 26136423Sgw25295 } else { 26146423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 26156423Sgw25295 "%s/%s", poolname, ++cp); 26166423Sgw25295 } 26176423Sgw25295 VERIFY(nvlist_add_string(config, 26186423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 26196423Sgw25295 kmem_free(dsname, MAXPATHLEN); 26206423Sgw25295 } 26216423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 26226423Sgw25295 } 26236423Sgw25295 26246423Sgw25295 /* 26255450Sbrendan * Add the list of hot spares and level 2 cache devices. 26262082Seschrock */ 26272082Seschrock spa_add_spares(spa, config); 26285450Sbrendan spa_add_l2cache(spa, config); 2629789Sahrens } 2630789Sahrens 2631789Sahrens spa_unload(spa); 2632789Sahrens spa_deactivate(spa); 2633789Sahrens spa_remove(spa); 2634789Sahrens mutex_exit(&spa_namespace_lock); 2635789Sahrens 2636789Sahrens return (config); 2637789Sahrens } 2638789Sahrens 2639789Sahrens /* 2640789Sahrens * Pool export/destroy 2641789Sahrens * 2642789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2643789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2644789Sahrens * update the pool state and sync all the labels to disk, removing the 26458211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 26468211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 2647789Sahrens */ 2648789Sahrens static int 26497214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 26508211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 2651789Sahrens { 2652789Sahrens spa_t *spa; 2653789Sahrens 26541775Sbillm if (oldconfig) 26551775Sbillm *oldconfig = NULL; 26561775Sbillm 26578241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 2658789Sahrens return (EROFS); 2659789Sahrens 2660789Sahrens mutex_enter(&spa_namespace_lock); 2661789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2662789Sahrens mutex_exit(&spa_namespace_lock); 2663789Sahrens return (ENOENT); 2664789Sahrens } 2665789Sahrens 2666789Sahrens /* 26671544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 26681544Seschrock * reacquire the namespace lock, and see if we can export. 26691544Seschrock */ 26701544Seschrock spa_open_ref(spa, FTAG); 26711544Seschrock mutex_exit(&spa_namespace_lock); 26721544Seschrock spa_async_suspend(spa); 26731544Seschrock mutex_enter(&spa_namespace_lock); 26741544Seschrock spa_close(spa, FTAG); 26751544Seschrock 26761544Seschrock /* 2677789Sahrens * The pool will be in core if it's openable, 2678789Sahrens * in which case we can modify its state. 2679789Sahrens */ 2680789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2681789Sahrens /* 2682789Sahrens * Objsets may be open only because they're dirty, so we 2683789Sahrens * have to force it to sync before checking spa_refcnt. 2684789Sahrens */ 2685789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2686789Sahrens 26871544Seschrock /* 26881544Seschrock * A pool cannot be exported or destroyed if there are active 26891544Seschrock * references. If we are resetting a pool, allow references by 26901544Seschrock * fault injection handlers. 26911544Seschrock */ 26921544Seschrock if (!spa_refcount_zero(spa) || 26931544Seschrock (spa->spa_inject_ref != 0 && 26941544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 26951544Seschrock spa_async_resume(spa); 2696789Sahrens mutex_exit(&spa_namespace_lock); 2697789Sahrens return (EBUSY); 2698789Sahrens } 2699789Sahrens 2700789Sahrens /* 27017214Slling * A pool cannot be exported if it has an active shared spare. 27027214Slling * This is to prevent other pools stealing the active spare 27037214Slling * from an exported pool. At user's own will, such pool can 27047214Slling * be forcedly exported. 27057214Slling */ 27067214Slling if (!force && new_state == POOL_STATE_EXPORTED && 27077214Slling spa_has_active_shared_spare(spa)) { 27087214Slling spa_async_resume(spa); 27097214Slling mutex_exit(&spa_namespace_lock); 27107214Slling return (EXDEV); 27117214Slling } 27127214Slling 27137214Slling /* 2714789Sahrens * We want this to be reflected on every label, 2715789Sahrens * so mark them all dirty. spa_unload() will do the 2716789Sahrens * final sync that pushes these changes out. 2717789Sahrens */ 27188211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 27197754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27201544Seschrock spa->spa_state = new_state; 27211635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 27221544Seschrock vdev_config_dirty(spa->spa_root_vdev); 27237754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 27241544Seschrock } 2725789Sahrens } 2726789Sahrens 27274451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 27284451Seschrock 2729789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2730789Sahrens spa_unload(spa); 2731789Sahrens spa_deactivate(spa); 2732789Sahrens } 2733789Sahrens 27341775Sbillm if (oldconfig && spa->spa_config) 27351775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 27361775Sbillm 27371544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 27388211SGeorge.Wilson@Sun.COM if (!hardforce) 27398211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 27401544Seschrock spa_remove(spa); 27411544Seschrock } 2742789Sahrens mutex_exit(&spa_namespace_lock); 2743789Sahrens 2744789Sahrens return (0); 2745789Sahrens } 2746789Sahrens 2747789Sahrens /* 2748789Sahrens * Destroy a storage pool. 2749789Sahrens */ 2750789Sahrens int 2751789Sahrens spa_destroy(char *pool) 2752789Sahrens { 27538211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 27548211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 2755789Sahrens } 2756789Sahrens 2757789Sahrens /* 2758789Sahrens * Export a storage pool. 2759789Sahrens */ 2760789Sahrens int 27618211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 27628211SGeorge.Wilson@Sun.COM boolean_t hardforce) 2763789Sahrens { 27648211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 27658211SGeorge.Wilson@Sun.COM force, hardforce)); 2766789Sahrens } 2767789Sahrens 2768789Sahrens /* 27691544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 27701544Seschrock * from the namespace in any way. 27711544Seschrock */ 27721544Seschrock int 27731544Seschrock spa_reset(char *pool) 27741544Seschrock { 27757214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 27768211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 27771544Seschrock } 27781544Seschrock 27791544Seschrock /* 2780789Sahrens * ========================================================================== 2781789Sahrens * Device manipulation 2782789Sahrens * ========================================================================== 2783789Sahrens */ 2784789Sahrens 2785789Sahrens /* 27864527Sperrin * Add a device to a storage pool. 2787789Sahrens */ 2788789Sahrens int 2789789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2790789Sahrens { 2791789Sahrens uint64_t txg; 27928241SJeff.Bonwick@Sun.COM int error; 2793789Sahrens vdev_t *rvd = spa->spa_root_vdev; 27941585Sbonwick vdev_t *vd, *tvd; 27955450Sbrendan nvlist_t **spares, **l2cache; 27965450Sbrendan uint_t nspares, nl2cache; 2797789Sahrens 2798789Sahrens txg = spa_vdev_enter(spa); 2799789Sahrens 28002082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 28012082Seschrock VDEV_ALLOC_ADD)) != 0) 28022082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 28032082Seschrock 28047754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 2805789Sahrens 28065450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 28075450Sbrendan &nspares) != 0) 28082082Seschrock nspares = 0; 28092082Seschrock 28105450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 28115450Sbrendan &nl2cache) != 0) 28125450Sbrendan nl2cache = 0; 28135450Sbrendan 28147754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 28152082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 28167754SJeff.Bonwick@Sun.COM 28177754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 28187754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 28197754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 28202082Seschrock 28213377Seschrock /* 28225450Sbrendan * We must validate the spares and l2cache devices after checking the 28235450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 28243377Seschrock */ 28257754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 28263377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 28273377Seschrock 28283377Seschrock /* 28293377Seschrock * Transfer each new top-level vdev from vd to rvd. 28303377Seschrock */ 28318241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 28323377Seschrock tvd = vd->vdev_child[c]; 28333377Seschrock vdev_remove_child(vd, tvd); 28343377Seschrock tvd->vdev_id = rvd->vdev_children; 28353377Seschrock vdev_add_child(rvd, tvd); 28363377Seschrock vdev_config_dirty(tvd); 28373377Seschrock } 28383377Seschrock 28392082Seschrock if (nspares != 0) { 28405450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 28415450Sbrendan ZPOOL_CONFIG_SPARES); 28422082Seschrock spa_load_spares(spa); 28435450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 28445450Sbrendan } 28455450Sbrendan 28465450Sbrendan if (nl2cache != 0) { 28475450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 28485450Sbrendan ZPOOL_CONFIG_L2CACHE); 28495450Sbrendan spa_load_l2cache(spa); 28505450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2851789Sahrens } 2852789Sahrens 2853789Sahrens /* 28541585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 28551585Sbonwick * If other threads start allocating from these vdevs before we 28561585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 28571585Sbonwick * fail to open the pool because there are DVAs that the config cache 28581585Sbonwick * can't translate. Therefore, we first add the vdevs without 28591585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 28601635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 28611585Sbonwick * 28621585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 28631585Sbonwick * if we lose power at any point in this sequence, the remaining 28641585Sbonwick * steps will be completed the next time we load the pool. 2865789Sahrens */ 28661635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 28671585Sbonwick 28681635Sbonwick mutex_enter(&spa_namespace_lock); 28691635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 28701635Sbonwick mutex_exit(&spa_namespace_lock); 2871789Sahrens 28721635Sbonwick return (0); 2873789Sahrens } 2874789Sahrens 2875789Sahrens /* 2876789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2877789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2878789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2879789Sahrens * 2880789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2881789Sahrens * existing device; in this case the two devices are made into their own 28824451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2883789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2884789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2885789Sahrens * completion of resilvering, the first disk (the one being replaced) 2886789Sahrens * is automatically detached. 2887789Sahrens */ 2888789Sahrens int 28891544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2890789Sahrens { 2891789Sahrens uint64_t txg, open_txg; 2892789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2893789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 28942082Seschrock vdev_ops_t *pvops; 28957313SEric.Kustarz@Sun.COM dmu_tx_t *tx; 28967313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 28977313SEric.Kustarz@Sun.COM int newvd_isspare; 28987313SEric.Kustarz@Sun.COM int error; 2899789Sahrens 2900789Sahrens txg = spa_vdev_enter(spa); 2901789Sahrens 29026643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2903789Sahrens 2904789Sahrens if (oldvd == NULL) 2905789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2906789Sahrens 29071585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 29081585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 29091585Sbonwick 2910789Sahrens pvd = oldvd->vdev_parent; 2911789Sahrens 29122082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 29134451Seschrock VDEV_ALLOC_ADD)) != 0) 29144451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 29154451Seschrock 29164451Seschrock if (newrootvd->vdev_children != 1) 2917789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2918789Sahrens 2919789Sahrens newvd = newrootvd->vdev_child[0]; 2920789Sahrens 2921789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2922789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2923789Sahrens 29242082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2925789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2926789Sahrens 29274527Sperrin /* 29284527Sperrin * Spares can't replace logs 29294527Sperrin */ 29307326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 29314527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29324527Sperrin 29332082Seschrock if (!replacing) { 29342082Seschrock /* 29352082Seschrock * For attach, the only allowable parent is a mirror or the root 29362082Seschrock * vdev. 29372082Seschrock */ 29382082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 29392082Seschrock pvd->vdev_ops != &vdev_root_ops) 29402082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29412082Seschrock 29422082Seschrock pvops = &vdev_mirror_ops; 29432082Seschrock } else { 29442082Seschrock /* 29452082Seschrock * Active hot spares can only be replaced by inactive hot 29462082Seschrock * spares. 29472082Seschrock */ 29482082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 29492082Seschrock pvd->vdev_child[1] == oldvd && 29502082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 29512082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29522082Seschrock 29532082Seschrock /* 29542082Seschrock * If the source is a hot spare, and the parent isn't already a 29552082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 29563377Seschrock * want to create a replacing vdev. The user is not allowed to 29573377Seschrock * attach to a spared vdev child unless the 'isspare' state is 29583377Seschrock * the same (spare replaces spare, non-spare replaces 29593377Seschrock * non-spare). 29602082Seschrock */ 29612082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 29622082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29633377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 29643377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 29653377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 29662082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 29672082Seschrock newvd->vdev_isspare) 29682082Seschrock pvops = &vdev_spare_ops; 29692082Seschrock else 29702082Seschrock pvops = &vdev_replacing_ops; 29712082Seschrock } 29722082Seschrock 29731175Slling /* 29741175Slling * Compare the new device size with the replaceable/attachable 29751175Slling * device size. 29761175Slling */ 29771175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2978789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2979789Sahrens 29801732Sbonwick /* 29811732Sbonwick * The new device cannot have a higher alignment requirement 29821732Sbonwick * than the top-level vdev. 29831732Sbonwick */ 29841732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2985789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2986789Sahrens 2987789Sahrens /* 2988789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2989789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2990789Sahrens */ 2991789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2992789Sahrens spa_strfree(oldvd->vdev_path); 2993789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2994789Sahrens KM_SLEEP); 2995789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2996789Sahrens newvd->vdev_path, "old"); 2997789Sahrens if (oldvd->vdev_devid != NULL) { 2998789Sahrens spa_strfree(oldvd->vdev_devid); 2999789Sahrens oldvd->vdev_devid = NULL; 3000789Sahrens } 3001789Sahrens } 3002789Sahrens 3003789Sahrens /* 30042082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 30052082Seschrock * mirror/replacing/spare vdev above oldvd. 3006789Sahrens */ 3007789Sahrens if (pvd->vdev_ops != pvops) 3008789Sahrens pvd = vdev_add_parent(oldvd, pvops); 3009789Sahrens 3010789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3011789Sahrens ASSERT(pvd->vdev_ops == pvops); 3012789Sahrens ASSERT(oldvd->vdev_parent == pvd); 3013789Sahrens 3014789Sahrens /* 3015789Sahrens * Extract the new device from its root and add it to pvd. 3016789Sahrens */ 3017789Sahrens vdev_remove_child(newrootvd, newvd); 3018789Sahrens newvd->vdev_id = pvd->vdev_children; 3019789Sahrens vdev_add_child(pvd, newvd); 3020789Sahrens 30211544Seschrock /* 30221544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 30231544Seschrock * the addition of newvd may have decreased our parent's asize. 30241544Seschrock */ 30251544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 30261544Seschrock 3027789Sahrens tvd = newvd->vdev_top; 3028789Sahrens ASSERT(pvd->vdev_top == tvd); 3029789Sahrens ASSERT(tvd->vdev_parent == rvd); 3030789Sahrens 3031789Sahrens vdev_config_dirty(tvd); 3032789Sahrens 3033789Sahrens /* 3034789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3035789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3036789Sahrens */ 3037789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 3038789Sahrens 30398241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, 30408241SJeff.Bonwick@Sun.COM TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3041789Sahrens 30423377Seschrock if (newvd->vdev_isspare) 30433377Seschrock spa_spare_activate(newvd); 30447754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 30457754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 30467313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 30471544Seschrock 3048789Sahrens /* 3049789Sahrens * Mark newvd's DTL dirty in this txg. 3050789Sahrens */ 30511732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3052789Sahrens 3053789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3054789Sahrens 30557313SEric.Kustarz@Sun.COM tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 30567313SEric.Kustarz@Sun.COM if (dmu_tx_assign(tx, TXG_WAIT) == 0) { 30577313SEric.Kustarz@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx, 30587313SEric.Kustarz@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 30597313SEric.Kustarz@Sun.COM replacing && newvd_isspare ? "spare in" : 30607313SEric.Kustarz@Sun.COM replacing ? "replace" : "attach", newvdpath, 30617313SEric.Kustarz@Sun.COM replacing ? "for" : "to", oldvdpath); 30627313SEric.Kustarz@Sun.COM dmu_tx_commit(tx); 30637313SEric.Kustarz@Sun.COM } else { 30647313SEric.Kustarz@Sun.COM dmu_tx_abort(tx); 30657313SEric.Kustarz@Sun.COM } 30667313SEric.Kustarz@Sun.COM 30677313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 30687313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 30697313SEric.Kustarz@Sun.COM 3070789Sahrens /* 30717046Sahrens * Kick off a resilver to update newvd. 3072789Sahrens */ 30737046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3074789Sahrens 3075789Sahrens return (0); 3076789Sahrens } 3077789Sahrens 3078789Sahrens /* 3079789Sahrens * Detach a device from a mirror or replacing vdev. 3080789Sahrens * If 'replace_done' is specified, only detach if the parent 3081789Sahrens * is a replacing vdev. 3082789Sahrens */ 3083789Sahrens int 30848241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3085789Sahrens { 3086789Sahrens uint64_t txg; 30878241SJeff.Bonwick@Sun.COM int error; 3088789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3089789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 30902082Seschrock boolean_t unspare = B_FALSE; 30912082Seschrock uint64_t unspare_guid; 30926673Seschrock size_t len; 3093789Sahrens 3094789Sahrens txg = spa_vdev_enter(spa); 3095789Sahrens 30966643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3097789Sahrens 3098789Sahrens if (vd == NULL) 3099789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3100789Sahrens 31011585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 31021585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 31031585Sbonwick 3104789Sahrens pvd = vd->vdev_parent; 3105789Sahrens 3106789Sahrens /* 31078241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 31088241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 31098241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 31108241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 31118241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 31128241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 31138241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 31148241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 31158241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 31168241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 31178241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 31188241SJeff.Bonwick@Sun.COM */ 31198241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 31208241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 31218241SJeff.Bonwick@Sun.COM 31228241SJeff.Bonwick@Sun.COM /* 3123789Sahrens * If replace_done is specified, only remove this device if it's 31242082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 31252082Seschrock * disk can be removed. 3126789Sahrens */ 31272082Seschrock if (replace_done) { 31282082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 31292082Seschrock if (vd->vdev_id != 0) 31302082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 31312082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 31322082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 31332082Seschrock } 31342082Seschrock } 31352082Seschrock 31362082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 31374577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3138789Sahrens 3139789Sahrens /* 31402082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3141789Sahrens */ 3142789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 31432082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 31442082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3145789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3146789Sahrens 3147789Sahrens /* 31488241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 31498241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 3150789Sahrens */ 31518241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 3152789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3153789Sahrens 31548241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 31558241SJeff.Bonwick@Sun.COM 3156789Sahrens /* 31576673Seschrock * If we are detaching the second disk from a replacing vdev, then 31586673Seschrock * check to see if we changed the original vdev's path to have "/old" 31596673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 31606673Seschrock */ 31616673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 31626673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 31636673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 31646673Seschrock ASSERT(pvd->vdev_child[1] == vd); 31656673Seschrock cvd = pvd->vdev_child[0]; 31666673Seschrock len = strlen(vd->vdev_path); 31676673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 31686673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 31696673Seschrock spa_strfree(cvd->vdev_path); 31706673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 31716673Seschrock } 31726673Seschrock } 31736673Seschrock 31746673Seschrock /* 31752082Seschrock * If we are detaching the original disk from a spare, then it implies 31762082Seschrock * that the spare should become a real disk, and be removed from the 31772082Seschrock * active spare list for the pool. 31782082Seschrock */ 31792082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 31808241SJeff.Bonwick@Sun.COM vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 31812082Seschrock unspare = B_TRUE; 31822082Seschrock 31832082Seschrock /* 3184789Sahrens * Erase the disk labels so the disk can be used for other things. 3185789Sahrens * This must be done after all other error cases are handled, 3186789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3187789Sahrens * But if we can't do it, don't treat the error as fatal -- 3188789Sahrens * it may be that the unwritability of the disk is the reason 3189789Sahrens * it's being detached! 3190789Sahrens */ 31913377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3192789Sahrens 3193789Sahrens /* 3194789Sahrens * Remove vd from its parent and compact the parent's children. 3195789Sahrens */ 3196789Sahrens vdev_remove_child(pvd, vd); 3197789Sahrens vdev_compact_children(pvd); 3198789Sahrens 3199789Sahrens /* 3200789Sahrens * Remember one of the remaining children so we can get tvd below. 3201789Sahrens */ 3202789Sahrens cvd = pvd->vdev_child[0]; 3203789Sahrens 3204789Sahrens /* 32052082Seschrock * If we need to remove the remaining child from the list of hot spares, 32068241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 32078241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 32088241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 32098241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 32108241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 32118241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 32122082Seschrock */ 32132082Seschrock if (unspare) { 32142082Seschrock ASSERT(cvd->vdev_isspare); 32153377Seschrock spa_spare_remove(cvd); 32162082Seschrock unspare_guid = cvd->vdev_guid; 32178241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 32182082Seschrock } 32192082Seschrock 32202082Seschrock /* 3221789Sahrens * If the parent mirror/replacing vdev only has one child, 3222789Sahrens * the parent is no longer needed. Remove it from the tree. 3223789Sahrens */ 3224789Sahrens if (pvd->vdev_children == 1) 3225789Sahrens vdev_remove_parent(cvd); 3226789Sahrens 3227789Sahrens /* 3228789Sahrens * We don't set tvd until now because the parent we just removed 3229789Sahrens * may have been the previous top-level vdev. 3230789Sahrens */ 3231789Sahrens tvd = cvd->vdev_top; 3232789Sahrens ASSERT(tvd->vdev_parent == rvd); 3233789Sahrens 3234789Sahrens /* 32353377Seschrock * Reevaluate the parent vdev state. 3236789Sahrens */ 32374451Seschrock vdev_propagate_state(cvd); 3238789Sahrens 3239789Sahrens /* 32403377Seschrock * If the device we just detached was smaller than the others, it may be 32413377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 32423377Seschrock * can't fail because the existing metaslabs are already in core, so 32433377Seschrock * there's nothing to read from disk. 3244789Sahrens */ 32451732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3246789Sahrens 3247789Sahrens vdev_config_dirty(tvd); 3248789Sahrens 3249789Sahrens /* 32503377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 32513377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 32523377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 32533377Seschrock * prevent vd from being accessed after it's freed. 3254789Sahrens */ 32558241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 3256789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 32571732Sbonwick vd->vdev_detached = B_TRUE; 32581732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3259789Sahrens 32604451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 32614451Seschrock 32622082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 32632082Seschrock 32642082Seschrock /* 32653377Seschrock * If this was the removal of the original device in a hot spare vdev, 32663377Seschrock * then we want to go through and remove the device from the hot spare 32673377Seschrock * list of every other pool. 32682082Seschrock */ 32692082Seschrock if (unspare) { 32708241SJeff.Bonwick@Sun.COM spa_t *myspa = spa; 32712082Seschrock spa = NULL; 32722082Seschrock mutex_enter(&spa_namespace_lock); 32732082Seschrock while ((spa = spa_next(spa)) != NULL) { 32742082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 32752082Seschrock continue; 32768241SJeff.Bonwick@Sun.COM if (spa == myspa) 32778241SJeff.Bonwick@Sun.COM continue; 32787793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 32797793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 32802082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 32817793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 32827793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 32832082Seschrock } 32842082Seschrock mutex_exit(&spa_namespace_lock); 32852082Seschrock } 32862082Seschrock 32872082Seschrock return (error); 32882082Seschrock } 32892082Seschrock 32907754SJeff.Bonwick@Sun.COM static nvlist_t * 32917754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 32922082Seschrock { 32937754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 32947754SJeff.Bonwick@Sun.COM uint64_t guid; 32957754SJeff.Bonwick@Sun.COM 32967754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 32977754SJeff.Bonwick@Sun.COM &guid) == 0); 32987754SJeff.Bonwick@Sun.COM 32997754SJeff.Bonwick@Sun.COM if (guid == target_guid) 33007754SJeff.Bonwick@Sun.COM return (nvpp[i]); 33012082Seschrock } 33022082Seschrock 33037754SJeff.Bonwick@Sun.COM return (NULL); 33045450Sbrendan } 33055450Sbrendan 33067754SJeff.Bonwick@Sun.COM static void 33077754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 33087754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 33095450Sbrendan { 33107754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 33117754SJeff.Bonwick@Sun.COM 33127754SJeff.Bonwick@Sun.COM if (count > 1) 33137754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 33147754SJeff.Bonwick@Sun.COM 33157754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 33167754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 33177754SJeff.Bonwick@Sun.COM continue; 33187754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 33195450Sbrendan } 33205450Sbrendan 33217754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 33227754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 33237754SJeff.Bonwick@Sun.COM 33247754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 33257754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 33267754SJeff.Bonwick@Sun.COM 33277754SJeff.Bonwick@Sun.COM if (count > 1) 33287754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 33295450Sbrendan } 33305450Sbrendan 33315450Sbrendan /* 33325450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 33335450Sbrendan * spares and level 2 ARC devices. 33345450Sbrendan */ 33355450Sbrendan int 33365450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 33375450Sbrendan { 33385450Sbrendan vdev_t *vd; 33397754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 33405450Sbrendan uint_t nspares, nl2cache; 33418241SJeff.Bonwick@Sun.COM uint64_t txg = 0; 33425450Sbrendan int error = 0; 33438241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 33448241SJeff.Bonwick@Sun.COM 33458241SJeff.Bonwick@Sun.COM if (!locked) 33468241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 33475450Sbrendan 33486643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 33495450Sbrendan 33505450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 33515450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 33527754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 33537754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 33547754SJeff.Bonwick@Sun.COM /* 33557754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 33567754SJeff.Bonwick@Sun.COM * in this pool. 33577754SJeff.Bonwick@Sun.COM */ 33587754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 33597754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 33607754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 33617754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 33627754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 33637754SJeff.Bonwick@Sun.COM } else { 33647754SJeff.Bonwick@Sun.COM error = EBUSY; 33657754SJeff.Bonwick@Sun.COM } 33667754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 33675450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 33687754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 33697754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 33707754SJeff.Bonwick@Sun.COM /* 33717754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 33727754SJeff.Bonwick@Sun.COM */ 33737754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 33747754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 33755450Sbrendan spa_load_l2cache(spa); 33765450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 33777754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 33787754SJeff.Bonwick@Sun.COM /* 33797754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 33807754SJeff.Bonwick@Sun.COM */ 33817754SJeff.Bonwick@Sun.COM error = ENOTSUP; 33827754SJeff.Bonwick@Sun.COM } else { 33837754SJeff.Bonwick@Sun.COM /* 33847754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 33857754SJeff.Bonwick@Sun.COM */ 33867754SJeff.Bonwick@Sun.COM error = ENOENT; 33875450Sbrendan } 33882082Seschrock 33898241SJeff.Bonwick@Sun.COM if (!locked) 33908241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 33918241SJeff.Bonwick@Sun.COM 33928241SJeff.Bonwick@Sun.COM return (error); 3393789Sahrens } 3394789Sahrens 3395789Sahrens /* 33964451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 33974451Seschrock * current spared, so we can detach it. 3398789Sahrens */ 33991544Seschrock static vdev_t * 34004451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3401789Sahrens { 34021544Seschrock vdev_t *newvd, *oldvd; 3403789Sahrens int c; 3404789Sahrens 34051544Seschrock for (c = 0; c < vd->vdev_children; c++) { 34064451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 34071544Seschrock if (oldvd != NULL) 34081544Seschrock return (oldvd); 34091544Seschrock } 3410789Sahrens 34114451Seschrock /* 34124451Seschrock * Check for a completed replacement. 34134451Seschrock */ 3414789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 34151544Seschrock oldvd = vd->vdev_child[0]; 34161544Seschrock newvd = vd->vdev_child[1]; 3417789Sahrens 34188241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 34198241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 34201544Seschrock return (oldvd); 34211544Seschrock } 3422789Sahrens 34234451Seschrock /* 34244451Seschrock * Check for a completed resilver with the 'unspare' flag set. 34254451Seschrock */ 34264451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 34274451Seschrock newvd = vd->vdev_child[0]; 34284451Seschrock oldvd = vd->vdev_child[1]; 34294451Seschrock 34304451Seschrock if (newvd->vdev_unspare && 34318241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 34328241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) { 34334451Seschrock newvd->vdev_unspare = 0; 34344451Seschrock return (oldvd); 34354451Seschrock } 34364451Seschrock } 34374451Seschrock 34381544Seschrock return (NULL); 3439789Sahrens } 3440789Sahrens 34411544Seschrock static void 34424451Seschrock spa_vdev_resilver_done(spa_t *spa) 3443789Sahrens { 34448241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 34458241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 34468241SJeff.Bonwick@Sun.COM 34478241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3448789Sahrens 34494451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 34508241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 34518241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 34521544Seschrock guid = vd->vdev_guid; 34538241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 34548241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 34558241SJeff.Bonwick@Sun.COM sguid = 0; 34562082Seschrock /* 34572082Seschrock * If we have just finished replacing a hot spared device, then 34582082Seschrock * we need to detach the parent's first child (the original hot 34592082Seschrock * spare) as well. 34602082Seschrock */ 34618241SJeff.Bonwick@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 34622082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 34638241SJeff.Bonwick@Sun.COM ASSERT(ppvd->vdev_children == 2); 34648241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 34652082Seschrock } 34668241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 34678241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 34681544Seschrock return; 34698241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 34702082Seschrock return; 34718241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3472789Sahrens } 3473789Sahrens 34748241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 3475789Sahrens } 3476789Sahrens 3477789Sahrens /* 34781354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 34791354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 34801354Seschrock */ 34811354Seschrock int 34821354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 34831354Seschrock { 34846643Seschrock vdev_t *vd; 34851354Seschrock uint64_t txg; 34861354Seschrock 34871354Seschrock txg = spa_vdev_enter(spa); 34881354Seschrock 34896643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 34902082Seschrock /* 34916643Seschrock * Determine if this is a reference to a hot spare device. If 34926643Seschrock * it is, update the path manually as there is no associated 34936643Seschrock * vdev_t that can be synced to disk. 34942082Seschrock */ 34956643Seschrock nvlist_t **spares; 34966643Seschrock uint_t i, nspares; 34975450Sbrendan 34985450Sbrendan if (spa->spa_spares.sav_config != NULL) { 34995450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 35005450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 35015450Sbrendan &spares, &nspares) == 0); 35022082Seschrock for (i = 0; i < nspares; i++) { 35032082Seschrock uint64_t theguid; 35042082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 35052082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 35065450Sbrendan if (theguid == guid) { 35075450Sbrendan VERIFY(nvlist_add_string(spares[i], 35085450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 35095450Sbrendan spa_load_spares(spa); 35105450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 35115450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 35125450Sbrendan 0)); 35135450Sbrendan } 35142082Seschrock } 35152082Seschrock } 35165450Sbrendan 35175450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 35182082Seschrock } 35191354Seschrock 35201585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 35211585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35221585Sbonwick 35231354Seschrock spa_strfree(vd->vdev_path); 35241354Seschrock vd->vdev_path = spa_strdup(newpath); 35251354Seschrock 35261354Seschrock vdev_config_dirty(vd->vdev_top); 35271354Seschrock 35281354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 35291354Seschrock } 35301354Seschrock 35311354Seschrock /* 3532789Sahrens * ========================================================================== 3533789Sahrens * SPA Scrubbing 3534789Sahrens * ========================================================================== 3535789Sahrens */ 3536789Sahrens 35377046Sahrens int 35387046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3539789Sahrens { 35407754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 35414808Sek110237 3542789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3543789Sahrens return (ENOTSUP); 3544789Sahrens 3545789Sahrens /* 35467046Sahrens * If a resilver was requested, but there is no DTL on a 35477046Sahrens * writeable leaf device, we have nothing to do. 3548789Sahrens */ 35497046Sahrens if (type == POOL_SCRUB_RESILVER && 35507046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 35517046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 35521544Seschrock return (0); 35531544Seschrock } 3554789Sahrens 35557046Sahrens if (type == POOL_SCRUB_EVERYTHING && 35567046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 35577046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 35587046Sahrens return (EBUSY); 35597046Sahrens 35607046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 35617046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 35627046Sahrens } else if (type == POOL_SCRUB_NONE) { 35637046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 35641544Seschrock } else { 35657046Sahrens return (EINVAL); 35661544Seschrock } 3567789Sahrens } 3568789Sahrens 35691544Seschrock /* 35701544Seschrock * ========================================================================== 35711544Seschrock * SPA async task processing 35721544Seschrock * ========================================================================== 35731544Seschrock */ 35741544Seschrock 35751544Seschrock static void 35764451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3577789Sahrens { 35787361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 35797361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 35807361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 35817754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 35827754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 35831544Seschrock } 35847361SBrendan.Gregg@Sun.COM 35857754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 35867361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 35871544Seschrock } 35881544Seschrock 35891544Seschrock static void 35907754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 35917754SJeff.Bonwick@Sun.COM { 35927754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 35937754SJeff.Bonwick@Sun.COM vd->vdev_probe_wanted = 0; 35947754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 35957754SJeff.Bonwick@Sun.COM } 35967754SJeff.Bonwick@Sun.COM 35977754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 35987754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 35997754SJeff.Bonwick@Sun.COM } 36007754SJeff.Bonwick@Sun.COM 36017754SJeff.Bonwick@Sun.COM static void 36021544Seschrock spa_async_thread(spa_t *spa) 36031544Seschrock { 36047754SJeff.Bonwick@Sun.COM int tasks; 36051544Seschrock 36061544Seschrock ASSERT(spa->spa_sync_on); 3607789Sahrens 36081544Seschrock mutex_enter(&spa->spa_async_lock); 36091544Seschrock tasks = spa->spa_async_tasks; 36101544Seschrock spa->spa_async_tasks = 0; 36111544Seschrock mutex_exit(&spa->spa_async_lock); 36121544Seschrock 36131544Seschrock /* 36141635Sbonwick * See if the config needs to be updated. 36151635Sbonwick */ 36161635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 36171635Sbonwick mutex_enter(&spa_namespace_lock); 36181635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 36191635Sbonwick mutex_exit(&spa_namespace_lock); 36201635Sbonwick } 36211635Sbonwick 36221635Sbonwick /* 36234451Seschrock * See if any devices need to be marked REMOVED. 36241544Seschrock */ 36257754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 36267754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 36274451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 36287754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 36297361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 36307754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 36317361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 36327754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 36337754SJeff.Bonwick@Sun.COM } 36347754SJeff.Bonwick@Sun.COM 36357754SJeff.Bonwick@Sun.COM /* 36367754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 36377754SJeff.Bonwick@Sun.COM */ 36387754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 36397754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 36407754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 36417754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 36424451Seschrock } 36431544Seschrock 36441544Seschrock /* 36451544Seschrock * If any devices are done replacing, detach them. 36461544Seschrock */ 36474451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 36484451Seschrock spa_vdev_resilver_done(spa); 3649789Sahrens 36501544Seschrock /* 36511544Seschrock * Kick off a resilver. 36521544Seschrock */ 36537046Sahrens if (tasks & SPA_ASYNC_RESILVER) 36547046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 36551544Seschrock 36561544Seschrock /* 36571544Seschrock * Let the world know that we're done. 36581544Seschrock */ 36591544Seschrock mutex_enter(&spa->spa_async_lock); 36601544Seschrock spa->spa_async_thread = NULL; 36611544Seschrock cv_broadcast(&spa->spa_async_cv); 36621544Seschrock mutex_exit(&spa->spa_async_lock); 36631544Seschrock thread_exit(); 36641544Seschrock } 36651544Seschrock 36661544Seschrock void 36671544Seschrock spa_async_suspend(spa_t *spa) 36681544Seschrock { 36691544Seschrock mutex_enter(&spa->spa_async_lock); 36701544Seschrock spa->spa_async_suspended++; 36711544Seschrock while (spa->spa_async_thread != NULL) 36721544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 36731544Seschrock mutex_exit(&spa->spa_async_lock); 36741544Seschrock } 36751544Seschrock 36761544Seschrock void 36771544Seschrock spa_async_resume(spa_t *spa) 36781544Seschrock { 36791544Seschrock mutex_enter(&spa->spa_async_lock); 36801544Seschrock ASSERT(spa->spa_async_suspended != 0); 36811544Seschrock spa->spa_async_suspended--; 36821544Seschrock mutex_exit(&spa->spa_async_lock); 36831544Seschrock } 36841544Seschrock 36851544Seschrock static void 36861544Seschrock spa_async_dispatch(spa_t *spa) 36871544Seschrock { 36881544Seschrock mutex_enter(&spa->spa_async_lock); 36891544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 36901635Sbonwick spa->spa_async_thread == NULL && 36911635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 36921544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 36931544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 36941544Seschrock mutex_exit(&spa->spa_async_lock); 36951544Seschrock } 36961544Seschrock 36971544Seschrock void 36981544Seschrock spa_async_request(spa_t *spa, int task) 36991544Seschrock { 37001544Seschrock mutex_enter(&spa->spa_async_lock); 37011544Seschrock spa->spa_async_tasks |= task; 37021544Seschrock mutex_exit(&spa->spa_async_lock); 3703789Sahrens } 3704789Sahrens 3705789Sahrens /* 3706789Sahrens * ========================================================================== 3707789Sahrens * SPA syncing routines 3708789Sahrens * ========================================================================== 3709789Sahrens */ 3710789Sahrens 3711789Sahrens static void 3712789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3713789Sahrens { 3714789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3715789Sahrens dmu_tx_t *tx; 3716789Sahrens blkptr_t blk; 3717789Sahrens uint64_t itor = 0; 3718789Sahrens zio_t *zio; 3719789Sahrens int error; 3720789Sahrens uint8_t c = 1; 3721789Sahrens 37227754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 37237754SJeff.Bonwick@Sun.COM 37247754SJeff.Bonwick@Sun.COM while (bplist_iterate(bpl, &itor, &blk) == 0) { 37257754SJeff.Bonwick@Sun.COM ASSERT(blk.blk_birth < txg); 37267754SJeff.Bonwick@Sun.COM zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL, 37277754SJeff.Bonwick@Sun.COM ZIO_FLAG_MUSTSUCCEED)); 37287754SJeff.Bonwick@Sun.COM } 3729789Sahrens 3730789Sahrens error = zio_wait(zio); 3731789Sahrens ASSERT3U(error, ==, 0); 3732789Sahrens 3733789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3734789Sahrens bplist_vacate(bpl, tx); 3735789Sahrens 3736789Sahrens /* 3737789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3738789Sahrens * (Usually only the first block is needed.) 3739789Sahrens */ 3740789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3741789Sahrens dmu_tx_commit(tx); 3742789Sahrens } 3743789Sahrens 3744789Sahrens static void 37452082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 37462082Seschrock { 37472082Seschrock char *packed = NULL; 37487497STim.Haley@Sun.COM size_t bufsize; 37492082Seschrock size_t nvsize = 0; 37502082Seschrock dmu_buf_t *db; 37512082Seschrock 37522082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 37532082Seschrock 37547497STim.Haley@Sun.COM /* 37557497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 37567497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 37577497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 37587497STim.Haley@Sun.COM */ 37597497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 37607497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 37612082Seschrock 37622082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 37632082Seschrock KM_SLEEP) == 0); 37647497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 37657497STim.Haley@Sun.COM 37667497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 37677497STim.Haley@Sun.COM 37687497STim.Haley@Sun.COM kmem_free(packed, bufsize); 37692082Seschrock 37702082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 37712082Seschrock dmu_buf_will_dirty(db, tx); 37722082Seschrock *(uint64_t *)db->db_data = nvsize; 37732082Seschrock dmu_buf_rele(db, FTAG); 37742082Seschrock } 37752082Seschrock 37762082Seschrock static void 37775450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 37785450Sbrendan const char *config, const char *entry) 37792082Seschrock { 37802082Seschrock nvlist_t *nvroot; 37815450Sbrendan nvlist_t **list; 37822082Seschrock int i; 37832082Seschrock 37845450Sbrendan if (!sav->sav_sync) 37852082Seschrock return; 37862082Seschrock 37872082Seschrock /* 37885450Sbrendan * Update the MOS nvlist describing the list of available devices. 37895450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 37904451Seschrock * valid and the vdevs are labeled appropriately. 37912082Seschrock */ 37925450Sbrendan if (sav->sav_object == 0) { 37935450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 37945450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 37955450Sbrendan sizeof (uint64_t), tx); 37962082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 37975450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 37985450Sbrendan &sav->sav_object, tx) == 0); 37992082Seschrock } 38002082Seschrock 38012082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 38025450Sbrendan if (sav->sav_count == 0) { 38035450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 38042082Seschrock } else { 38055450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 38065450Sbrendan for (i = 0; i < sav->sav_count; i++) 38075450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 38085450Sbrendan B_FALSE, B_FALSE, B_TRUE); 38095450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 38105450Sbrendan sav->sav_count) == 0); 38115450Sbrendan for (i = 0; i < sav->sav_count; i++) 38125450Sbrendan nvlist_free(list[i]); 38135450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 38142082Seschrock } 38152082Seschrock 38165450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 38172926Sek110237 nvlist_free(nvroot); 38182082Seschrock 38195450Sbrendan sav->sav_sync = B_FALSE; 38202082Seschrock } 38212082Seschrock 38222082Seschrock static void 3823789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3824789Sahrens { 3825789Sahrens nvlist_t *config; 3826789Sahrens 38277754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 3828789Sahrens return; 3829789Sahrens 38307754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 38317754SJeff.Bonwick@Sun.COM 38327754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 38337754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 38347754SJeff.Bonwick@Sun.COM 38357754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 3836789Sahrens 38371635Sbonwick if (spa->spa_config_syncing) 38381635Sbonwick nvlist_free(spa->spa_config_syncing); 38391635Sbonwick spa->spa_config_syncing = config; 3840789Sahrens 38412082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3842789Sahrens } 3843789Sahrens 38445094Slling /* 38455094Slling * Set zpool properties. 38465094Slling */ 38473912Slling static void 38484543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 38493912Slling { 38503912Slling spa_t *spa = arg1; 38515094Slling objset_t *mos = spa->spa_meta_objset; 38523912Slling nvlist_t *nvp = arg2; 38535094Slling nvpair_t *elem; 38544451Seschrock uint64_t intval; 38556643Seschrock char *strval; 38565094Slling zpool_prop_t prop; 38575094Slling const char *propname; 38585094Slling zprop_type_t proptype; 38595094Slling 38607754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 38617754SJeff.Bonwick@Sun.COM 38625094Slling elem = NULL; 38635094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 38645094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 38655094Slling case ZPOOL_PROP_VERSION: 38665094Slling /* 38675094Slling * Only set version for non-zpool-creation cases 38685094Slling * (set/import). spa_create() needs special care 38695094Slling * for version setting. 38705094Slling */ 38715094Slling if (tx->tx_txg != TXG_INITIAL) { 38725094Slling VERIFY(nvpair_value_uint64(elem, 38735094Slling &intval) == 0); 38745094Slling ASSERT(intval <= SPA_VERSION); 38755094Slling ASSERT(intval >= spa_version(spa)); 38765094Slling spa->spa_uberblock.ub_version = intval; 38775094Slling vdev_config_dirty(spa->spa_root_vdev); 38785094Slling } 38795094Slling break; 38805094Slling 38815094Slling case ZPOOL_PROP_ALTROOT: 38825094Slling /* 38835094Slling * 'altroot' is a non-persistent property. It should 38845094Slling * have been set temporarily at creation or import time. 38855094Slling */ 38865094Slling ASSERT(spa->spa_root != NULL); 38875094Slling break; 38885094Slling 38895363Seschrock case ZPOOL_PROP_CACHEFILE: 38905094Slling /* 38918525SEric.Schrock@Sun.COM * 'cachefile' is also a non-persisitent property. 38925094Slling */ 38934543Smarks break; 38945094Slling default: 38955094Slling /* 38965094Slling * Set pool property values in the poolprops mos object. 38975094Slling */ 38985094Slling if (spa->spa_pool_props_object == 0) { 38995094Slling objset_t *mos = spa->spa_meta_objset; 39005094Slling 39015094Slling VERIFY((spa->spa_pool_props_object = 39025094Slling zap_create(mos, DMU_OT_POOL_PROPS, 39035094Slling DMU_OT_NONE, 0, tx)) > 0); 39045094Slling 39055094Slling VERIFY(zap_update(mos, 39065094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 39075094Slling 8, 1, &spa->spa_pool_props_object, tx) 39085094Slling == 0); 39095094Slling } 39105094Slling 39115094Slling /* normalize the property name */ 39125094Slling propname = zpool_prop_to_name(prop); 39135094Slling proptype = zpool_prop_get_type(prop); 39145094Slling 39155094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 39165094Slling ASSERT(proptype == PROP_TYPE_STRING); 39175094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 39185094Slling VERIFY(zap_update(mos, 39195094Slling spa->spa_pool_props_object, propname, 39205094Slling 1, strlen(strval) + 1, strval, tx) == 0); 39215094Slling 39225094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 39235094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 39245094Slling 39255094Slling if (proptype == PROP_TYPE_INDEX) { 39265094Slling const char *unused; 39275094Slling VERIFY(zpool_prop_index_to_string( 39285094Slling prop, intval, &unused) == 0); 39295094Slling } 39305094Slling VERIFY(zap_update(mos, 39315094Slling spa->spa_pool_props_object, propname, 39325094Slling 8, 1, &intval, tx) == 0); 39335094Slling } else { 39345094Slling ASSERT(0); /* not allowed */ 39355094Slling } 39365094Slling 39375329Sgw25295 switch (prop) { 39385329Sgw25295 case ZPOOL_PROP_DELEGATION: 39395094Slling spa->spa_delegation = intval; 39405329Sgw25295 break; 39415329Sgw25295 case ZPOOL_PROP_BOOTFS: 39425094Slling spa->spa_bootfs = intval; 39435329Sgw25295 break; 39445329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 39455329Sgw25295 spa->spa_failmode = intval; 39465329Sgw25295 break; 39475329Sgw25295 default: 39485329Sgw25295 break; 39495329Sgw25295 } 39503912Slling } 39515094Slling 39525094Slling /* log internal history if this is not a zpool create */ 39535094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 39545094Slling tx->tx_txg != TXG_INITIAL) { 39555094Slling spa_history_internal_log(LOG_POOL_PROPSET, 39565094Slling spa, tx, cr, "%s %lld %s", 39577754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 39585094Slling } 39593912Slling } 39607754SJeff.Bonwick@Sun.COM 39617754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 39623912Slling } 39633912Slling 3964789Sahrens /* 3965789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3966789Sahrens * part of the process, so we iterate until it converges. 3967789Sahrens */ 3968789Sahrens void 3969789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3970789Sahrens { 3971789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3972789Sahrens objset_t *mos = spa->spa_meta_objset; 3973789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 39741635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3975789Sahrens vdev_t *vd; 3976789Sahrens dmu_tx_t *tx; 3977789Sahrens int dirty_vdevs; 39787754SJeff.Bonwick@Sun.COM int error; 3979789Sahrens 3980789Sahrens /* 3981789Sahrens * Lock out configuration changes. 3982789Sahrens */ 39837754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3984789Sahrens 3985789Sahrens spa->spa_syncing_txg = txg; 3986789Sahrens spa->spa_sync_pass = 0; 3987789Sahrens 39887754SJeff.Bonwick@Sun.COM /* 39897754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 39907754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 39917754SJeff.Bonwick@Sun.COM */ 39927754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 39938241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 39948241SJeff.Bonwick@Sun.COM /* 39958241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 39968241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 39978241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 39988241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 39998241SJeff.Bonwick@Sun.COM * into the root vdev tree. 40008241SJeff.Bonwick@Sun.COM */ 40018241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 40028241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 40038241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 40048241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 40058241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 40068241SJeff.Bonwick@Sun.COM } 40078241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 40088241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 40097754SJeff.Bonwick@Sun.COM } 40107754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 40117754SJeff.Bonwick@Sun.COM 40121544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4013789Sahrens 40142082Seschrock tx = dmu_tx_create_assigned(dp, txg); 40152082Seschrock 40162082Seschrock /* 40174577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 40182082Seschrock * set spa_deflate if we have no raid-z vdevs. 40192082Seschrock */ 40204577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 40214577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 40222082Seschrock int i; 40232082Seschrock 40242082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 40252082Seschrock vd = rvd->vdev_child[i]; 40262082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 40272082Seschrock break; 40282082Seschrock } 40292082Seschrock if (i == rvd->vdev_children) { 40302082Seschrock spa->spa_deflate = TRUE; 40312082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 40322082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 40332082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 40342082Seschrock } 40352082Seschrock } 40362082Seschrock 40377046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 40387046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 40397046Sahrens dsl_pool_create_origin(dp, tx); 40407046Sahrens 40417046Sahrens /* Keeping the origin open increases spa_minref */ 40427046Sahrens spa->spa_minref += 3; 40437046Sahrens } 40447046Sahrens 40457046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 40467046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 40477046Sahrens dsl_pool_upgrade_clones(dp, tx); 40487046Sahrens } 40497046Sahrens 4050789Sahrens /* 4051789Sahrens * If anything has changed in this txg, push the deferred frees 4052789Sahrens * from the previous txg. If not, leave them alone so that we 4053789Sahrens * don't generate work on an otherwise idle system. 4054789Sahrens */ 4055789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 40562329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 40572329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 4058789Sahrens spa_sync_deferred_frees(spa, txg); 4059789Sahrens 4060789Sahrens /* 4061789Sahrens * Iterate to convergence. 4062789Sahrens */ 4063789Sahrens do { 4064789Sahrens spa->spa_sync_pass++; 4065789Sahrens 4066789Sahrens spa_sync_config_object(spa, tx); 40675450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 40685450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 40695450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 40705450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 40711544Seschrock spa_errlog_sync(spa, txg); 4072789Sahrens dsl_pool_sync(dp, txg); 4073789Sahrens 4074789Sahrens dirty_vdevs = 0; 4075789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4076789Sahrens vdev_sync(vd, txg); 4077789Sahrens dirty_vdevs++; 4078789Sahrens } 4079789Sahrens 4080789Sahrens bplist_sync(bpl, tx); 4081789Sahrens } while (dirty_vdevs); 4082789Sahrens 4083789Sahrens bplist_close(bpl); 4084789Sahrens 4085789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4086789Sahrens 4087789Sahrens /* 4088789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4089789Sahrens * to commit the transaction group. 40901635Sbonwick * 40915688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 40925688Sbonwick * random top-level vdevs that are known to be visible in the 40937754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 40947754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4095789Sahrens */ 40967754SJeff.Bonwick@Sun.COM for (;;) { 40977754SJeff.Bonwick@Sun.COM /* 40987754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 40997754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 41007754SJeff.Bonwick@Sun.COM */ 41017754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 41027754SJeff.Bonwick@Sun.COM 41037754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 41047754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 41057754SJeff.Bonwick@Sun.COM int svdcount = 0; 41067754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 41077754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 41087754SJeff.Bonwick@Sun.COM int c; 41097754SJeff.Bonwick@Sun.COM 41107754SJeff.Bonwick@Sun.COM for (c = 0; c < children; c++) { 41117754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 41127754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 41137754SJeff.Bonwick@Sun.COM continue; 41147754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 41157754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 41167754SJeff.Bonwick@Sun.COM break; 41177754SJeff.Bonwick@Sun.COM } 41187754SJeff.Bonwick@Sun.COM error = vdev_config_sync(svd, svdcount, txg); 41197754SJeff.Bonwick@Sun.COM } else { 41207754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 41217754SJeff.Bonwick@Sun.COM rvd->vdev_children, txg); 41221635Sbonwick } 41237754SJeff.Bonwick@Sun.COM 41247754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 41257754SJeff.Bonwick@Sun.COM 41267754SJeff.Bonwick@Sun.COM if (error == 0) 41277754SJeff.Bonwick@Sun.COM break; 41287754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 41297754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 41301635Sbonwick } 41312082Seschrock dmu_tx_commit(tx); 41322082Seschrock 41331635Sbonwick /* 41341635Sbonwick * Clear the dirty config list. 41351635Sbonwick */ 41367754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 41371635Sbonwick vdev_config_clean(vd); 41381635Sbonwick 41391635Sbonwick /* 41401635Sbonwick * Now that the new config has synced transactionally, 41411635Sbonwick * let it become visible to the config cache. 41421635Sbonwick */ 41431635Sbonwick if (spa->spa_config_syncing != NULL) { 41441635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 41451635Sbonwick spa->spa_config_txg = txg; 41461635Sbonwick spa->spa_config_syncing = NULL; 41471635Sbonwick } 4148789Sahrens 4149789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4150789Sahrens 4151789Sahrens /* 4152789Sahrens * Clean up the ZIL records for the synced txg. 4153789Sahrens */ 4154789Sahrens dsl_pool_zil_clean(dp); 4155789Sahrens 4156789Sahrens /* 4157789Sahrens * Update usable space statistics. 4158789Sahrens */ 4159789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4160789Sahrens vdev_sync_done(vd, txg); 4161789Sahrens 4162789Sahrens /* 4163789Sahrens * It had better be the case that we didn't dirty anything 41642082Seschrock * since vdev_config_sync(). 4165789Sahrens */ 4166789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4167789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4168789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4169789Sahrens ASSERT(bpl->bpl_queue == NULL); 4170789Sahrens 41717754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 41721544Seschrock 41731544Seschrock /* 41741544Seschrock * If any async tasks have been requested, kick them off. 41751544Seschrock */ 41761544Seschrock spa_async_dispatch(spa); 4177789Sahrens } 4178789Sahrens 4179789Sahrens /* 4180789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4181789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4182789Sahrens * sync. 4183789Sahrens */ 4184789Sahrens void 4185789Sahrens spa_sync_allpools(void) 4186789Sahrens { 4187789Sahrens spa_t *spa = NULL; 4188789Sahrens mutex_enter(&spa_namespace_lock); 4189789Sahrens while ((spa = spa_next(spa)) != NULL) { 41907754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4191789Sahrens continue; 4192789Sahrens spa_open_ref(spa, FTAG); 4193789Sahrens mutex_exit(&spa_namespace_lock); 4194789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4195789Sahrens mutex_enter(&spa_namespace_lock); 4196789Sahrens spa_close(spa, FTAG); 4197789Sahrens } 4198789Sahrens mutex_exit(&spa_namespace_lock); 4199789Sahrens } 4200789Sahrens 4201789Sahrens /* 4202789Sahrens * ========================================================================== 4203789Sahrens * Miscellaneous routines 4204789Sahrens * ========================================================================== 4205789Sahrens */ 4206789Sahrens 4207789Sahrens /* 4208789Sahrens * Remove all pools in the system. 4209789Sahrens */ 4210789Sahrens void 4211789Sahrens spa_evict_all(void) 4212789Sahrens { 4213789Sahrens spa_t *spa; 4214789Sahrens 4215789Sahrens /* 4216789Sahrens * Remove all cached state. All pools should be closed now, 4217789Sahrens * so every spa in the AVL tree should be unreferenced. 4218789Sahrens */ 4219789Sahrens mutex_enter(&spa_namespace_lock); 4220789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4221789Sahrens /* 42221544Seschrock * Stop async tasks. The async thread may need to detach 42231544Seschrock * a device that's been replaced, which requires grabbing 42241544Seschrock * spa_namespace_lock, so we must drop it here. 4225789Sahrens */ 4226789Sahrens spa_open_ref(spa, FTAG); 4227789Sahrens mutex_exit(&spa_namespace_lock); 42281544Seschrock spa_async_suspend(spa); 42294808Sek110237 mutex_enter(&spa_namespace_lock); 4230789Sahrens spa_close(spa, FTAG); 4231789Sahrens 4232789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4233789Sahrens spa_unload(spa); 4234789Sahrens spa_deactivate(spa); 4235789Sahrens } 4236789Sahrens spa_remove(spa); 4237789Sahrens } 4238789Sahrens mutex_exit(&spa_namespace_lock); 4239789Sahrens } 42401544Seschrock 42411544Seschrock vdev_t * 42426643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 42431544Seschrock { 42446643Seschrock vdev_t *vd; 42456643Seschrock int i; 42466643Seschrock 42476643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 42486643Seschrock return (vd); 42496643Seschrock 42506643Seschrock if (l2cache) { 42516643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 42526643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 42536643Seschrock if (vd->vdev_guid == guid) 42546643Seschrock return (vd); 42556643Seschrock } 42566643Seschrock } 42576643Seschrock 42586643Seschrock return (NULL); 42591544Seschrock } 42601760Seschrock 42611760Seschrock void 42625094Slling spa_upgrade(spa_t *spa, uint64_t version) 42631760Seschrock { 42647754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 42651760Seschrock 42661760Seschrock /* 42671760Seschrock * This should only be called for a non-faulted pool, and since a 42681760Seschrock * future version would result in an unopenable pool, this shouldn't be 42691760Seschrock * possible. 42701760Seschrock */ 42714577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 42725094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 42735094Slling 42745094Slling spa->spa_uberblock.ub_version = version; 42751760Seschrock vdev_config_dirty(spa->spa_root_vdev); 42761760Seschrock 42777754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 42782082Seschrock 42792082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 42801760Seschrock } 42812082Seschrock 42822082Seschrock boolean_t 42832082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 42842082Seschrock { 42852082Seschrock int i; 42863377Seschrock uint64_t spareguid; 42875450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 42885450Sbrendan 42895450Sbrendan for (i = 0; i < sav->sav_count; i++) 42905450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 42912082Seschrock return (B_TRUE); 42922082Seschrock 42935450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 42945450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 42955450Sbrendan &spareguid) == 0 && spareguid == guid) 42963377Seschrock return (B_TRUE); 42973377Seschrock } 42983377Seschrock 42992082Seschrock return (B_FALSE); 43002082Seschrock } 43013912Slling 43024451Seschrock /* 43037214Slling * Check if a pool has an active shared spare device. 43047214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 43057214Slling */ 43067214Slling static boolean_t 43077214Slling spa_has_active_shared_spare(spa_t *spa) 43087214Slling { 43097214Slling int i, refcnt; 43107214Slling uint64_t pool; 43117214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 43127214Slling 43137214Slling for (i = 0; i < sav->sav_count; i++) { 43147214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 43157214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 43167214Slling refcnt > 2) 43177214Slling return (B_TRUE); 43187214Slling } 43197214Slling 43207214Slling return (B_FALSE); 43217214Slling } 43227214Slling 43237214Slling /* 43244451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 43254451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 43264451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 43274451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 43284451Seschrock * or zdb as real changes. 43294451Seschrock */ 43304451Seschrock void 43314451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 43324451Seschrock { 43334451Seschrock #ifdef _KERNEL 43344451Seschrock sysevent_t *ev; 43354451Seschrock sysevent_attr_list_t *attr = NULL; 43364451Seschrock sysevent_value_t value; 43374451Seschrock sysevent_id_t eid; 43384451Seschrock 43394451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 43404451Seschrock SE_SLEEP); 43414451Seschrock 43424451Seschrock value.value_type = SE_DATA_TYPE_STRING; 43434451Seschrock value.value.sv_string = spa_name(spa); 43444451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 43454451Seschrock goto done; 43464451Seschrock 43474451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 43484451Seschrock value.value.sv_uint64 = spa_guid(spa); 43494451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 43504451Seschrock goto done; 43514451Seschrock 43524451Seschrock if (vd) { 43534451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 43544451Seschrock value.value.sv_uint64 = vd->vdev_guid; 43554451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 43564451Seschrock SE_SLEEP) != 0) 43574451Seschrock goto done; 43584451Seschrock 43594451Seschrock if (vd->vdev_path) { 43604451Seschrock value.value_type = SE_DATA_TYPE_STRING; 43614451Seschrock value.value.sv_string = vd->vdev_path; 43624451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 43634451Seschrock &value, SE_SLEEP) != 0) 43644451Seschrock goto done; 43654451Seschrock } 43664451Seschrock } 43674451Seschrock 43685756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 43695756Seschrock goto done; 43705756Seschrock attr = NULL; 43715756Seschrock 43724451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 43734451Seschrock 43744451Seschrock done: 43754451Seschrock if (attr) 43764451Seschrock sysevent_free_attr(attr); 43774451Seschrock sysevent_free(ev); 43784451Seschrock #endif 43794451Seschrock } 4380