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> 629816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h> 63789Sahrens 648662SJordan.Vaughan@Sun.com #ifdef _KERNEL 658662SJordan.Vaughan@Sun.com #include <sys/zone.h> 668662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 678662SJordan.Vaughan@Sun.com 685094Slling #include "zfs_prop.h" 695913Sperrin #include "zfs_comutil.h" 705094Slling 719515SJonathan.Adams@Sun.COM enum zti_modes { 729515SJonathan.Adams@Sun.COM zti_mode_fixed, /* value is # of threads (min 1) */ 739515SJonathan.Adams@Sun.COM zti_mode_online_percent, /* value is % of online CPUs */ 749515SJonathan.Adams@Sun.COM zti_mode_tune, /* fill from zio_taskq_tune_* */ 759515SJonathan.Adams@Sun.COM zti_nmodes 767754SJeff.Bonwick@Sun.COM }; 772986Sek110237 789515SJonathan.Adams@Sun.COM #define ZTI_THREAD_FIX(n) { zti_mode_fixed, (n) } 799515SJonathan.Adams@Sun.COM #define ZTI_THREAD_PCT(n) { zti_mode_online_percent, (n) } 809515SJonathan.Adams@Sun.COM #define ZTI_THREAD_TUNE { zti_mode_tune, 0 } 819515SJonathan.Adams@Sun.COM 829515SJonathan.Adams@Sun.COM #define ZTI_THREAD_ONE ZTI_THREAD_FIX(1) 839515SJonathan.Adams@Sun.COM 849515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info { 859515SJonathan.Adams@Sun.COM const char *zti_name; 869515SJonathan.Adams@Sun.COM struct { 879515SJonathan.Adams@Sun.COM enum zti_modes zti_mode; 889515SJonathan.Adams@Sun.COM uint_t zti_value; 899515SJonathan.Adams@Sun.COM } zti_nthreads[ZIO_TASKQ_TYPES]; 909515SJonathan.Adams@Sun.COM } zio_taskq_info_t; 919515SJonathan.Adams@Sun.COM 929515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 939515SJonathan.Adams@Sun.COM "issue", "intr" 949515SJonathan.Adams@Sun.COM }; 959515SJonathan.Adams@Sun.COM 969515SJonathan.Adams@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES] = { 979515SJonathan.Adams@Sun.COM /* ISSUE INTR */ 989515SJonathan.Adams@Sun.COM { "spa_zio_null", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 999515SJonathan.Adams@Sun.COM { "spa_zio_read", { ZTI_THREAD_FIX(8), ZTI_THREAD_TUNE } }, 1009515SJonathan.Adams@Sun.COM { "spa_zio_write", { ZTI_THREAD_TUNE, ZTI_THREAD_FIX(8) } }, 1019515SJonathan.Adams@Sun.COM { "spa_zio_free", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1029515SJonathan.Adams@Sun.COM { "spa_zio_claim", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1039515SJonathan.Adams@Sun.COM { "spa_zio_ioctl", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1049515SJonathan.Adams@Sun.COM }; 1059515SJonathan.Adams@Sun.COM 1069515SJonathan.Adams@Sun.COM enum zti_modes zio_taskq_tune_mode = zti_mode_online_percent; 1079515SJonathan.Adams@Sun.COM uint_t zio_taskq_tune_value = 80; /* #threads = 80% of # online CPUs */ 1089515SJonathan.Adams@Sun.COM 1095094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 1107214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1115094Slling 1125094Slling /* 1135094Slling * ========================================================================== 1145094Slling * SPA properties routines 1155094Slling * ========================================================================== 1165094Slling */ 1175094Slling 1185094Slling /* 1195094Slling * Add a (source=src, propname=propval) list to an nvlist. 1205094Slling */ 1215949Slling static void 1225094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 1235094Slling uint64_t intval, zprop_source_t src) 1245094Slling { 1255094Slling const char *propname = zpool_prop_to_name(prop); 1265094Slling nvlist_t *propval; 1275949Slling 1285949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1295949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 1305949Slling 1315949Slling if (strval != NULL) 1325949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1335949Slling else 1345949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 1355949Slling 1365949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 1375094Slling nvlist_free(propval); 1385094Slling } 1395094Slling 1405094Slling /* 1415094Slling * Get property values from the spa configuration. 1425094Slling */ 1435949Slling static void 1445094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1455094Slling { 1468525SEric.Schrock@Sun.COM uint64_t size; 1478525SEric.Schrock@Sun.COM uint64_t used; 1485094Slling uint64_t cap, version; 1495094Slling zprop_source_t src = ZPROP_SRC_NONE; 1506643Seschrock spa_config_dirent_t *dp; 1515094Slling 1527754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 1537754SJeff.Bonwick@Sun.COM 1548525SEric.Schrock@Sun.COM if (spa->spa_root_vdev != NULL) { 1558525SEric.Schrock@Sun.COM size = spa_get_space(spa); 1568525SEric.Schrock@Sun.COM used = spa_get_alloc(spa); 1578525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 1588525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 1598525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 1608525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, 1618525SEric.Schrock@Sun.COM size - used, src); 1628525SEric.Schrock@Sun.COM 1638525SEric.Schrock@Sun.COM cap = (size == 0) ? 0 : (used * 100 / size); 1648525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1658525SEric.Schrock@Sun.COM 1668525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1678525SEric.Schrock@Sun.COM spa->spa_root_vdev->vdev_state, src); 1688525SEric.Schrock@Sun.COM 1698525SEric.Schrock@Sun.COM version = spa_version(spa); 1708525SEric.Schrock@Sun.COM if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1718525SEric.Schrock@Sun.COM src = ZPROP_SRC_DEFAULT; 1728525SEric.Schrock@Sun.COM else 1738525SEric.Schrock@Sun.COM src = ZPROP_SRC_LOCAL; 1748525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1758525SEric.Schrock@Sun.COM } 1765949Slling 1775949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1785949Slling 1795949Slling if (spa->spa_root != NULL) 1805949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1815949Slling 0, ZPROP_SRC_LOCAL); 1825094Slling 1836643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 1846643Seschrock if (dp->scd_path == NULL) { 1855949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1866643Seschrock "none", 0, ZPROP_SRC_LOCAL); 1876643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1885949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1896643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1905363Seschrock } 1915363Seschrock } 1925094Slling } 1935094Slling 1945094Slling /* 1955094Slling * Get zpool property values. 1965094Slling */ 1975094Slling int 1985094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 1995094Slling { 2005094Slling zap_cursor_t zc; 2015094Slling zap_attribute_t za; 2025094Slling objset_t *mos = spa->spa_meta_objset; 2035094Slling int err; 2045094Slling 2055949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2065094Slling 2077754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 2087754SJeff.Bonwick@Sun.COM 2095094Slling /* 2105094Slling * Get properties from the spa config. 2115094Slling */ 2125949Slling spa_prop_get_config(spa, nvp); 2135094Slling 2145094Slling /* If no pool property object, no more prop to get. */ 2155094Slling if (spa->spa_pool_props_object == 0) { 2165094Slling mutex_exit(&spa->spa_props_lock); 2175094Slling return (0); 2185094Slling } 2195094Slling 2205094Slling /* 2215094Slling * Get properties from the MOS pool property object. 2225094Slling */ 2235094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 2245094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 2255094Slling zap_cursor_advance(&zc)) { 2265094Slling uint64_t intval = 0; 2275094Slling char *strval = NULL; 2285094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 2295094Slling zpool_prop_t prop; 2305094Slling 2315094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 2325094Slling continue; 2335094Slling 2345094Slling switch (za.za_integer_length) { 2355094Slling case 8: 2365094Slling /* integer property */ 2375094Slling if (za.za_first_integer != 2385094Slling zpool_prop_default_numeric(prop)) 2395094Slling src = ZPROP_SRC_LOCAL; 2405094Slling 2415094Slling if (prop == ZPOOL_PROP_BOOTFS) { 2425094Slling dsl_pool_t *dp; 2435094Slling dsl_dataset_t *ds = NULL; 2445094Slling 2455094Slling dp = spa_get_dsl(spa); 2465094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2476689Smaybee if (err = dsl_dataset_hold_obj(dp, 2486689Smaybee za.za_first_integer, FTAG, &ds)) { 2495094Slling rw_exit(&dp->dp_config_rwlock); 2505094Slling break; 2515094Slling } 2525094Slling 2535094Slling strval = kmem_alloc( 2545094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2555094Slling KM_SLEEP); 2565094Slling dsl_dataset_name(ds, strval); 2576689Smaybee dsl_dataset_rele(ds, FTAG); 2585094Slling rw_exit(&dp->dp_config_rwlock); 2595094Slling } else { 2605094Slling strval = NULL; 2615094Slling intval = za.za_first_integer; 2625094Slling } 2635094Slling 2645949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2655094Slling 2665094Slling if (strval != NULL) 2675094Slling kmem_free(strval, 2685094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2695094Slling 2705094Slling break; 2715094Slling 2725094Slling case 1: 2735094Slling /* string property */ 2745094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2755094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2765094Slling za.za_name, 1, za.za_num_integers, strval); 2775094Slling if (err) { 2785094Slling kmem_free(strval, za.za_num_integers); 2795094Slling break; 2805094Slling } 2815949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2825094Slling kmem_free(strval, za.za_num_integers); 2835094Slling break; 2845094Slling 2855094Slling default: 2865094Slling break; 2875094Slling } 2885094Slling } 2895094Slling zap_cursor_fini(&zc); 2905094Slling mutex_exit(&spa->spa_props_lock); 2915094Slling out: 2925094Slling if (err && err != ENOENT) { 2935094Slling nvlist_free(*nvp); 2945949Slling *nvp = NULL; 2955094Slling return (err); 2965094Slling } 2975094Slling 2985094Slling return (0); 2995094Slling } 3005094Slling 3015094Slling /* 3025094Slling * Validate the given pool properties nvlist and modify the list 3035094Slling * for the property values to be set. 3045094Slling */ 3055094Slling static int 3065094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 3075094Slling { 3085094Slling nvpair_t *elem; 3095094Slling int error = 0, reset_bootfs = 0; 3105094Slling uint64_t objnum; 3115094Slling 3125094Slling elem = NULL; 3135094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3145094Slling zpool_prop_t prop; 3155094Slling char *propname, *strval; 3165094Slling uint64_t intval; 3175094Slling objset_t *os; 3185363Seschrock char *slash; 3195094Slling 3205094Slling propname = nvpair_name(elem); 3215094Slling 3225094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 3235094Slling return (EINVAL); 3245094Slling 3255094Slling switch (prop) { 3265094Slling case ZPOOL_PROP_VERSION: 3275094Slling error = nvpair_value_uint64(elem, &intval); 3285094Slling if (!error && 3295094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 3305094Slling error = EINVAL; 3315094Slling break; 3325094Slling 3335094Slling case ZPOOL_PROP_DELEGATION: 3345094Slling case ZPOOL_PROP_AUTOREPLACE: 3357538SRichard.Morris@Sun.COM case ZPOOL_PROP_LISTSNAPS: 3369816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 3375094Slling error = nvpair_value_uint64(elem, &intval); 3385094Slling if (!error && intval > 1) 3395094Slling error = EINVAL; 3405094Slling break; 3415094Slling 3425094Slling case ZPOOL_PROP_BOOTFS: 3439630SJeff.Bonwick@Sun.COM /* 3449630SJeff.Bonwick@Sun.COM * If the pool version is less than SPA_VERSION_BOOTFS, 3459630SJeff.Bonwick@Sun.COM * or the pool is still being created (version == 0), 3469630SJeff.Bonwick@Sun.COM * the bootfs property cannot be set. 3479630SJeff.Bonwick@Sun.COM */ 3485094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3495094Slling error = ENOTSUP; 3505094Slling break; 3515094Slling } 3525094Slling 3535094Slling /* 3547042Sgw25295 * Make sure the vdev config is bootable 3555094Slling */ 3567042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3575094Slling error = ENOTSUP; 3585094Slling break; 3595094Slling } 3605094Slling 3615094Slling reset_bootfs = 1; 3625094Slling 3635094Slling error = nvpair_value_string(elem, &strval); 3645094Slling 3655094Slling if (!error) { 3667042Sgw25295 uint64_t compress; 3677042Sgw25295 3685094Slling if (strval == NULL || strval[0] == '\0') { 3695094Slling objnum = zpool_prop_default_numeric( 3705094Slling ZPOOL_PROP_BOOTFS); 3715094Slling break; 3725094Slling } 3735094Slling 3745094Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 3756689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os)) 3765094Slling break; 3777042Sgw25295 3787042Sgw25295 /* We don't support gzip bootable datasets */ 3797042Sgw25295 if ((error = dsl_prop_get_integer(strval, 3807042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3817042Sgw25295 &compress, NULL)) == 0 && 3827042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3837042Sgw25295 error = ENOTSUP; 3847042Sgw25295 } else { 3857042Sgw25295 objnum = dmu_objset_id(os); 3867042Sgw25295 } 3875094Slling dmu_objset_close(os); 3885094Slling } 3895094Slling break; 3907754SJeff.Bonwick@Sun.COM 3915329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3925329Sgw25295 error = nvpair_value_uint64(elem, &intval); 3935329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3945329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 3955329Sgw25295 error = EINVAL; 3965329Sgw25295 3975329Sgw25295 /* 3985329Sgw25295 * This is a special case which only occurs when 3995329Sgw25295 * the pool has completely failed. This allows 4005329Sgw25295 * the user to change the in-core failmode property 4015329Sgw25295 * without syncing it out to disk (I/Os might 4025329Sgw25295 * currently be blocked). We do this by returning 4035329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 4045329Sgw25295 * into thinking we encountered a property validation 4055329Sgw25295 * error. 4065329Sgw25295 */ 4077754SJeff.Bonwick@Sun.COM if (!error && spa_suspended(spa)) { 4085329Sgw25295 spa->spa_failmode = intval; 4095329Sgw25295 error = EIO; 4105329Sgw25295 } 4115329Sgw25295 break; 4125363Seschrock 4135363Seschrock case ZPOOL_PROP_CACHEFILE: 4145363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4155363Seschrock break; 4165363Seschrock 4175363Seschrock if (strval[0] == '\0') 4185363Seschrock break; 4195363Seschrock 4205363Seschrock if (strcmp(strval, "none") == 0) 4215363Seschrock break; 4225363Seschrock 4235363Seschrock if (strval[0] != '/') { 4245363Seschrock error = EINVAL; 4255363Seschrock break; 4265363Seschrock } 4275363Seschrock 4285363Seschrock slash = strrchr(strval, '/'); 4295363Seschrock ASSERT(slash != NULL); 4305363Seschrock 4315363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4325363Seschrock strcmp(slash, "/..") == 0) 4335363Seschrock error = EINVAL; 4345363Seschrock break; 4355094Slling } 4365094Slling 4375094Slling if (error) 4385094Slling break; 4395094Slling } 4405094Slling 4415094Slling if (!error && reset_bootfs) { 4425094Slling error = nvlist_remove(props, 4435094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 4445094Slling 4455094Slling if (!error) { 4465094Slling error = nvlist_add_uint64(props, 4475094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 4485094Slling } 4495094Slling } 4505094Slling 4515094Slling return (error); 4525094Slling } 4535094Slling 4548525SEric.Schrock@Sun.COM void 4558525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 4568525SEric.Schrock@Sun.COM { 4578525SEric.Schrock@Sun.COM char *cachefile; 4588525SEric.Schrock@Sun.COM spa_config_dirent_t *dp; 4598525SEric.Schrock@Sun.COM 4608525SEric.Schrock@Sun.COM if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 4618525SEric.Schrock@Sun.COM &cachefile) != 0) 4628525SEric.Schrock@Sun.COM return; 4638525SEric.Schrock@Sun.COM 4648525SEric.Schrock@Sun.COM dp = kmem_alloc(sizeof (spa_config_dirent_t), 4658525SEric.Schrock@Sun.COM KM_SLEEP); 4668525SEric.Schrock@Sun.COM 4678525SEric.Schrock@Sun.COM if (cachefile[0] == '\0') 4688525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(spa_config_path); 4698525SEric.Schrock@Sun.COM else if (strcmp(cachefile, "none") == 0) 4708525SEric.Schrock@Sun.COM dp->scd_path = NULL; 4718525SEric.Schrock@Sun.COM else 4728525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(cachefile); 4738525SEric.Schrock@Sun.COM 4748525SEric.Schrock@Sun.COM list_insert_head(&spa->spa_config_list, dp); 4758525SEric.Schrock@Sun.COM if (need_sync) 4768525SEric.Schrock@Sun.COM spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 4778525SEric.Schrock@Sun.COM } 4788525SEric.Schrock@Sun.COM 4795094Slling int 4805094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4815094Slling { 4825094Slling int error; 4838525SEric.Schrock@Sun.COM nvpair_t *elem; 4848525SEric.Schrock@Sun.COM boolean_t need_sync = B_FALSE; 4858525SEric.Schrock@Sun.COM zpool_prop_t prop; 4865094Slling 4875094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 4885094Slling return (error); 4895094Slling 4908525SEric.Schrock@Sun.COM elem = NULL; 4918525SEric.Schrock@Sun.COM while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 4928525SEric.Schrock@Sun.COM if ((prop = zpool_name_to_prop( 4938525SEric.Schrock@Sun.COM nvpair_name(elem))) == ZPROP_INVAL) 4948525SEric.Schrock@Sun.COM return (EINVAL); 4958525SEric.Schrock@Sun.COM 4968525SEric.Schrock@Sun.COM if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 4978525SEric.Schrock@Sun.COM continue; 4988525SEric.Schrock@Sun.COM 4998525SEric.Schrock@Sun.COM need_sync = B_TRUE; 5008525SEric.Schrock@Sun.COM break; 5018525SEric.Schrock@Sun.COM } 5028525SEric.Schrock@Sun.COM 5038525SEric.Schrock@Sun.COM if (need_sync) 5048525SEric.Schrock@Sun.COM return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 5058525SEric.Schrock@Sun.COM spa, nvp, 3)); 5068525SEric.Schrock@Sun.COM else 5078525SEric.Schrock@Sun.COM return (0); 5085094Slling } 5095094Slling 5105094Slling /* 5115094Slling * If the bootfs property value is dsobj, clear it. 5125094Slling */ 5135094Slling void 5145094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 5155094Slling { 5165094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 5175094Slling VERIFY(zap_remove(spa->spa_meta_objset, 5185094Slling spa->spa_pool_props_object, 5195094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 5205094Slling spa->spa_bootfs = 0; 5215094Slling } 5225094Slling } 5235094Slling 524789Sahrens /* 525789Sahrens * ========================================================================== 526789Sahrens * SPA state manipulation (open/create/destroy/import/export) 527789Sahrens * ========================================================================== 528789Sahrens */ 529789Sahrens 5301544Seschrock static int 5311544Seschrock spa_error_entry_compare(const void *a, const void *b) 5321544Seschrock { 5331544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 5341544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 5351544Seschrock int ret; 5361544Seschrock 5371544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 5381544Seschrock sizeof (zbookmark_t)); 5391544Seschrock 5401544Seschrock if (ret < 0) 5411544Seschrock return (-1); 5421544Seschrock else if (ret > 0) 5431544Seschrock return (1); 5441544Seschrock else 5451544Seschrock return (0); 5461544Seschrock } 5471544Seschrock 5481544Seschrock /* 5491544Seschrock * Utility function which retrieves copies of the current logs and 5501544Seschrock * re-initializes them in the process. 5511544Seschrock */ 5521544Seschrock void 5531544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 5541544Seschrock { 5551544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 5561544Seschrock 5571544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 5581544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 5591544Seschrock 5601544Seschrock avl_create(&spa->spa_errlist_scrub, 5611544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5621544Seschrock offsetof(spa_error_entry_t, se_avl)); 5631544Seschrock avl_create(&spa->spa_errlist_last, 5641544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5651544Seschrock offsetof(spa_error_entry_t, se_avl)); 5661544Seschrock } 5671544Seschrock 568789Sahrens /* 569789Sahrens * Activate an uninitialized pool. 570789Sahrens */ 571789Sahrens static void 5728241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode) 573789Sahrens { 574789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 575789Sahrens 576789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 5778241SJeff.Bonwick@Sun.COM spa->spa_mode = mode; 578789Sahrens 5799480SGeorge.Wilson@Sun.COM spa->spa_normal_class = metaslab_class_create(zfs_metaslab_ops); 5809480SGeorge.Wilson@Sun.COM spa->spa_log_class = metaslab_class_create(zfs_metaslab_ops); 581789Sahrens 5827754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 5839515SJonathan.Adams@Sun.COM const zio_taskq_info_t *ztip = &zio_taskqs[t]; 5847754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 5859515SJonathan.Adams@Sun.COM enum zti_modes mode = ztip->zti_nthreads[q].zti_mode; 5869515SJonathan.Adams@Sun.COM uint_t value = ztip->zti_nthreads[q].zti_value; 5879515SJonathan.Adams@Sun.COM char name[32]; 5889515SJonathan.Adams@Sun.COM 5899515SJonathan.Adams@Sun.COM (void) snprintf(name, sizeof (name), 5909515SJonathan.Adams@Sun.COM "%s_%s", ztip->zti_name, zio_taskq_types[q]); 5919515SJonathan.Adams@Sun.COM 5929515SJonathan.Adams@Sun.COM if (mode == zti_mode_tune) { 5939515SJonathan.Adams@Sun.COM mode = zio_taskq_tune_mode; 5949515SJonathan.Adams@Sun.COM value = zio_taskq_tune_value; 5959515SJonathan.Adams@Sun.COM if (mode == zti_mode_tune) 5969515SJonathan.Adams@Sun.COM mode = zti_mode_online_percent; 5979515SJonathan.Adams@Sun.COM } 5989515SJonathan.Adams@Sun.COM 5999515SJonathan.Adams@Sun.COM switch (mode) { 6009515SJonathan.Adams@Sun.COM case zti_mode_fixed: 6019515SJonathan.Adams@Sun.COM ASSERT3U(value, >=, 1); 6029515SJonathan.Adams@Sun.COM value = MAX(value, 1); 6039515SJonathan.Adams@Sun.COM 6049515SJonathan.Adams@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create(name, 6059515SJonathan.Adams@Sun.COM value, maxclsyspri, 50, INT_MAX, 6069515SJonathan.Adams@Sun.COM TASKQ_PREPOPULATE); 6079515SJonathan.Adams@Sun.COM break; 6089515SJonathan.Adams@Sun.COM 6099515SJonathan.Adams@Sun.COM case zti_mode_online_percent: 6109515SJonathan.Adams@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create(name, 6119515SJonathan.Adams@Sun.COM value, maxclsyspri, 50, INT_MAX, 6129515SJonathan.Adams@Sun.COM TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); 6139515SJonathan.Adams@Sun.COM break; 6149515SJonathan.Adams@Sun.COM 6159515SJonathan.Adams@Sun.COM case zti_mode_tune: 6169515SJonathan.Adams@Sun.COM default: 6179515SJonathan.Adams@Sun.COM panic("unrecognized mode for " 6189515SJonathan.Adams@Sun.COM "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) " 6199515SJonathan.Adams@Sun.COM "in spa_activate()", 6209515SJonathan.Adams@Sun.COM t, q, mode, value); 6219515SJonathan.Adams@Sun.COM break; 6229515SJonathan.Adams@Sun.COM } 6237754SJeff.Bonwick@Sun.COM } 624789Sahrens } 625789Sahrens 6267754SJeff.Bonwick@Sun.COM list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 6277754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_config_dirty_node)); 6287754SJeff.Bonwick@Sun.COM list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 6297754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_state_dirty_node)); 630789Sahrens 631789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 632789Sahrens offsetof(struct vdev, vdev_txg_node)); 6331544Seschrock 6341544Seschrock avl_create(&spa->spa_errlist_scrub, 6351544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 6361544Seschrock offsetof(spa_error_entry_t, se_avl)); 6371544Seschrock avl_create(&spa->spa_errlist_last, 6381544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 6391544Seschrock offsetof(spa_error_entry_t, se_avl)); 640789Sahrens } 641789Sahrens 642789Sahrens /* 643789Sahrens * Opposite of spa_activate(). 644789Sahrens */ 645789Sahrens static void 646789Sahrens spa_deactivate(spa_t *spa) 647789Sahrens { 648789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 649789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 650789Sahrens ASSERT(spa->spa_root_vdev == NULL); 6519630SJeff.Bonwick@Sun.COM ASSERT(spa->spa_async_zio_root == NULL); 652789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 653789Sahrens 654789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 655789Sahrens 6567754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_config_dirty_list); 6577754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_state_dirty_list); 6587754SJeff.Bonwick@Sun.COM 6597754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 6607754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 6617754SJeff.Bonwick@Sun.COM taskq_destroy(spa->spa_zio_taskq[t][q]); 6627754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 6637754SJeff.Bonwick@Sun.COM } 664789Sahrens } 665789Sahrens 666789Sahrens metaslab_class_destroy(spa->spa_normal_class); 667789Sahrens spa->spa_normal_class = NULL; 668789Sahrens 6694527Sperrin metaslab_class_destroy(spa->spa_log_class); 6704527Sperrin spa->spa_log_class = NULL; 6714527Sperrin 6721544Seschrock /* 6731544Seschrock * If this was part of an import or the open otherwise failed, we may 6741544Seschrock * still have errors left in the queues. Empty them just in case. 6751544Seschrock */ 6761544Seschrock spa_errlog_drain(spa); 6771544Seschrock 6781544Seschrock avl_destroy(&spa->spa_errlist_scrub); 6791544Seschrock avl_destroy(&spa->spa_errlist_last); 6801544Seschrock 681789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 682789Sahrens } 683789Sahrens 684789Sahrens /* 685789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 686789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 687789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 688789Sahrens * All vdev validation is done by the vdev_alloc() routine. 689789Sahrens */ 6902082Seschrock static int 6912082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 6922082Seschrock uint_t id, int atype) 693789Sahrens { 694789Sahrens nvlist_t **child; 6959816SGeorge.Wilson@Sun.COM uint_t children; 6962082Seschrock int error; 6972082Seschrock 6982082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 6992082Seschrock return (error); 7002082Seschrock 7012082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 7022082Seschrock return (0); 703789Sahrens 7047754SJeff.Bonwick@Sun.COM error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 7057754SJeff.Bonwick@Sun.COM &child, &children); 7067754SJeff.Bonwick@Sun.COM 7077754SJeff.Bonwick@Sun.COM if (error == ENOENT) 7087754SJeff.Bonwick@Sun.COM return (0); 7097754SJeff.Bonwick@Sun.COM 7107754SJeff.Bonwick@Sun.COM if (error) { 7112082Seschrock vdev_free(*vdp); 7122082Seschrock *vdp = NULL; 7132082Seschrock return (EINVAL); 714789Sahrens } 715789Sahrens 7169816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 7172082Seschrock vdev_t *vd; 7182082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 7192082Seschrock atype)) != 0) { 7202082Seschrock vdev_free(*vdp); 7212082Seschrock *vdp = NULL; 7222082Seschrock return (error); 723789Sahrens } 724789Sahrens } 725789Sahrens 7262082Seschrock ASSERT(*vdp != NULL); 7272082Seschrock 7282082Seschrock return (0); 729789Sahrens } 730789Sahrens 731789Sahrens /* 732789Sahrens * Opposite of spa_load(). 733789Sahrens */ 734789Sahrens static void 735789Sahrens spa_unload(spa_t *spa) 736789Sahrens { 7372082Seschrock int i; 7382082Seschrock 7397754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 7407754SJeff.Bonwick@Sun.COM 741789Sahrens /* 7421544Seschrock * Stop async tasks. 7431544Seschrock */ 7441544Seschrock spa_async_suspend(spa); 7451544Seschrock 7461544Seschrock /* 747789Sahrens * Stop syncing. 748789Sahrens */ 749789Sahrens if (spa->spa_sync_on) { 750789Sahrens txg_sync_stop(spa->spa_dsl_pool); 751789Sahrens spa->spa_sync_on = B_FALSE; 752789Sahrens } 753789Sahrens 754789Sahrens /* 7557754SJeff.Bonwick@Sun.COM * Wait for any outstanding async I/O to complete. 756789Sahrens */ 7579234SGeorge.Wilson@Sun.COM if (spa->spa_async_zio_root != NULL) { 7589234SGeorge.Wilson@Sun.COM (void) zio_wait(spa->spa_async_zio_root); 7599234SGeorge.Wilson@Sun.COM spa->spa_async_zio_root = NULL; 7609234SGeorge.Wilson@Sun.COM } 761789Sahrens 762789Sahrens /* 763789Sahrens * Close the dsl pool. 764789Sahrens */ 765789Sahrens if (spa->spa_dsl_pool) { 766789Sahrens dsl_pool_close(spa->spa_dsl_pool); 767789Sahrens spa->spa_dsl_pool = NULL; 768789Sahrens } 769789Sahrens 7708241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7718241SJeff.Bonwick@Sun.COM 7728241SJeff.Bonwick@Sun.COM /* 7738241SJeff.Bonwick@Sun.COM * Drop and purge level 2 cache 7748241SJeff.Bonwick@Sun.COM */ 7758241SJeff.Bonwick@Sun.COM spa_l2cache_drop(spa); 7768241SJeff.Bonwick@Sun.COM 777789Sahrens /* 778789Sahrens * Close all vdevs. 779789Sahrens */ 7801585Sbonwick if (spa->spa_root_vdev) 781789Sahrens vdev_free(spa->spa_root_vdev); 7821585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 7831544Seschrock 7845450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7855450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 7865450Sbrendan if (spa->spa_spares.sav_vdevs) { 7875450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7885450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7895450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7905450Sbrendan } 7915450Sbrendan if (spa->spa_spares.sav_config) { 7925450Sbrendan nvlist_free(spa->spa_spares.sav_config); 7935450Sbrendan spa->spa_spares.sav_config = NULL; 7942082Seschrock } 7957377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 7965450Sbrendan 7975450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 7985450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 7995450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 8005450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 8015450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 8025450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 8035450Sbrendan } 8045450Sbrendan if (spa->spa_l2cache.sav_config) { 8055450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 8065450Sbrendan spa->spa_l2cache.sav_config = NULL; 8072082Seschrock } 8087377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 8092082Seschrock 8101544Seschrock spa->spa_async_suspended = 0; 8118241SJeff.Bonwick@Sun.COM 8128241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 813789Sahrens } 814789Sahrens 815789Sahrens /* 8162082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 8172082Seschrock * this pool. When this is called, we have some form of basic information in 8185450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 8195450Sbrendan * then re-generate a more complete list including status information. 8202082Seschrock */ 8212082Seschrock static void 8222082Seschrock spa_load_spares(spa_t *spa) 8232082Seschrock { 8242082Seschrock nvlist_t **spares; 8252082Seschrock uint_t nspares; 8262082Seschrock int i; 8273377Seschrock vdev_t *vd, *tvd; 8282082Seschrock 8297754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 8307754SJeff.Bonwick@Sun.COM 8312082Seschrock /* 8322082Seschrock * First, close and free any existing spare vdevs. 8332082Seschrock */ 8345450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 8355450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 8363377Seschrock 8373377Seschrock /* Undo the call to spa_activate() below */ 8386643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 8396643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 8403377Seschrock spa_spare_remove(tvd); 8413377Seschrock vdev_close(vd); 8423377Seschrock vdev_free(vd); 8432082Seschrock } 8443377Seschrock 8455450Sbrendan if (spa->spa_spares.sav_vdevs) 8465450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 8475450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 8485450Sbrendan 8495450Sbrendan if (spa->spa_spares.sav_config == NULL) 8502082Seschrock nspares = 0; 8512082Seschrock else 8525450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 8532082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 8542082Seschrock 8555450Sbrendan spa->spa_spares.sav_count = (int)nspares; 8565450Sbrendan spa->spa_spares.sav_vdevs = NULL; 8572082Seschrock 8582082Seschrock if (nspares == 0) 8592082Seschrock return; 8602082Seschrock 8612082Seschrock /* 8622082Seschrock * Construct the array of vdevs, opening them to get status in the 8633377Seschrock * process. For each spare, there is potentially two different vdev_t 8643377Seschrock * structures associated with it: one in the list of spares (used only 8653377Seschrock * for basic validation purposes) and one in the active vdev 8663377Seschrock * configuration (if it's spared in). During this phase we open and 8673377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 8683377Seschrock * active configuration, then we also mark this vdev as an active spare. 8692082Seschrock */ 8705450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 8715450Sbrendan KM_SLEEP); 8725450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 8732082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 8742082Seschrock VDEV_ALLOC_SPARE) == 0); 8752082Seschrock ASSERT(vd != NULL); 8762082Seschrock 8775450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 8782082Seschrock 8796643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 8806643Seschrock B_FALSE)) != NULL) { 8813377Seschrock if (!tvd->vdev_isspare) 8823377Seschrock spa_spare_add(tvd); 8833377Seschrock 8843377Seschrock /* 8853377Seschrock * We only mark the spare active if we were successfully 8863377Seschrock * able to load the vdev. Otherwise, importing a pool 8873377Seschrock * with a bad active spare would result in strange 8883377Seschrock * behavior, because multiple pool would think the spare 8893377Seschrock * is actively in use. 8903377Seschrock * 8913377Seschrock * There is a vulnerability here to an equally bizarre 8923377Seschrock * circumstance, where a dead active spare is later 8933377Seschrock * brought back to life (onlined or otherwise). Given 8943377Seschrock * the rarity of this scenario, and the extra complexity 8953377Seschrock * it adds, we ignore the possibility. 8963377Seschrock */ 8973377Seschrock if (!vdev_is_dead(tvd)) 8983377Seschrock spa_spare_activate(tvd); 8993377Seschrock } 9003377Seschrock 9017754SJeff.Bonwick@Sun.COM vd->vdev_top = vd; 9029425SEric.Schrock@Sun.COM vd->vdev_aux = &spa->spa_spares; 9037754SJeff.Bonwick@Sun.COM 9042082Seschrock if (vdev_open(vd) != 0) 9052082Seschrock continue; 9062082Seschrock 9075450Sbrendan if (vdev_validate_aux(vd) == 0) 9085450Sbrendan spa_spare_add(vd); 9092082Seschrock } 9102082Seschrock 9112082Seschrock /* 9122082Seschrock * Recompute the stashed list of spares, with status information 9132082Seschrock * this time. 9142082Seschrock */ 9155450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 9162082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 9172082Seschrock 9185450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 9195450Sbrendan KM_SLEEP); 9205450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 9215450Sbrendan spares[i] = vdev_config_generate(spa, 9225450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 9235450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 9245450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 9255450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 9262082Seschrock nvlist_free(spares[i]); 9275450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 9285450Sbrendan } 9295450Sbrendan 9305450Sbrendan /* 9315450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 9325450Sbrendan * this pool. When this is called, we have some form of basic information in 9335450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 9345450Sbrendan * then re-generate a more complete list including status information. 9355450Sbrendan * Devices which are already active have their details maintained, and are 9365450Sbrendan * not re-opened. 9375450Sbrendan */ 9385450Sbrendan static void 9395450Sbrendan spa_load_l2cache(spa_t *spa) 9405450Sbrendan { 9415450Sbrendan nvlist_t **l2cache; 9425450Sbrendan uint_t nl2cache; 9435450Sbrendan int i, j, oldnvdevs; 9449816SGeorge.Wilson@Sun.COM uint64_t guid; 9455450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 9465450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 9475450Sbrendan 9487754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 9497754SJeff.Bonwick@Sun.COM 9505450Sbrendan if (sav->sav_config != NULL) { 9515450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 9525450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 9535450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 9545450Sbrendan } else { 9555450Sbrendan nl2cache = 0; 9565450Sbrendan } 9575450Sbrendan 9585450Sbrendan oldvdevs = sav->sav_vdevs; 9595450Sbrendan oldnvdevs = sav->sav_count; 9605450Sbrendan sav->sav_vdevs = NULL; 9615450Sbrendan sav->sav_count = 0; 9625450Sbrendan 9635450Sbrendan /* 9645450Sbrendan * Process new nvlist of vdevs. 9655450Sbrendan */ 9665450Sbrendan for (i = 0; i < nl2cache; i++) { 9675450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 9685450Sbrendan &guid) == 0); 9695450Sbrendan 9705450Sbrendan newvdevs[i] = NULL; 9715450Sbrendan for (j = 0; j < oldnvdevs; j++) { 9725450Sbrendan vd = oldvdevs[j]; 9735450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 9745450Sbrendan /* 9755450Sbrendan * Retain previous vdev for add/remove ops. 9765450Sbrendan */ 9775450Sbrendan newvdevs[i] = vd; 9785450Sbrendan oldvdevs[j] = NULL; 9795450Sbrendan break; 9805450Sbrendan } 9815450Sbrendan } 9825450Sbrendan 9835450Sbrendan if (newvdevs[i] == NULL) { 9845450Sbrendan /* 9855450Sbrendan * Create new vdev 9865450Sbrendan */ 9875450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 9885450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 9895450Sbrendan ASSERT(vd != NULL); 9905450Sbrendan newvdevs[i] = vd; 9915450Sbrendan 9925450Sbrendan /* 9935450Sbrendan * Commit this vdev as an l2cache device, 9945450Sbrendan * even if it fails to open. 9955450Sbrendan */ 9965450Sbrendan spa_l2cache_add(vd); 9975450Sbrendan 9986643Seschrock vd->vdev_top = vd; 9996643Seschrock vd->vdev_aux = sav; 10006643Seschrock 10016643Seschrock spa_l2cache_activate(vd); 10026643Seschrock 10035450Sbrendan if (vdev_open(vd) != 0) 10045450Sbrendan continue; 10055450Sbrendan 10065450Sbrendan (void) vdev_validate_aux(vd); 10075450Sbrendan 10089816SGeorge.Wilson@Sun.COM if (!vdev_is_dead(vd)) 10099816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa, vd); 10105450Sbrendan } 10115450Sbrendan } 10125450Sbrendan 10135450Sbrendan /* 10145450Sbrendan * Purge vdevs that were dropped 10155450Sbrendan */ 10165450Sbrendan for (i = 0; i < oldnvdevs; i++) { 10175450Sbrendan uint64_t pool; 10185450Sbrendan 10195450Sbrendan vd = oldvdevs[i]; 10205450Sbrendan if (vd != NULL) { 10218241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 10228241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 10235450Sbrendan l2arc_remove_vdev(vd); 10245450Sbrendan (void) vdev_close(vd); 10255450Sbrendan spa_l2cache_remove(vd); 10265450Sbrendan } 10275450Sbrendan } 10285450Sbrendan 10295450Sbrendan if (oldvdevs) 10305450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 10315450Sbrendan 10325450Sbrendan if (sav->sav_config == NULL) 10335450Sbrendan goto out; 10345450Sbrendan 10355450Sbrendan sav->sav_vdevs = newvdevs; 10365450Sbrendan sav->sav_count = (int)nl2cache; 10375450Sbrendan 10385450Sbrendan /* 10395450Sbrendan * Recompute the stashed list of l2cache devices, with status 10405450Sbrendan * information this time. 10415450Sbrendan */ 10425450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 10435450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 10445450Sbrendan 10455450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 10465450Sbrendan for (i = 0; i < sav->sav_count; i++) 10475450Sbrendan l2cache[i] = vdev_config_generate(spa, 10485450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 10495450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 10505450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 10515450Sbrendan out: 10525450Sbrendan for (i = 0; i < sav->sav_count; i++) 10535450Sbrendan nvlist_free(l2cache[i]); 10545450Sbrendan if (sav->sav_count) 10555450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 10562082Seschrock } 10572082Seschrock 10582082Seschrock static int 10592082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 10602082Seschrock { 10612082Seschrock dmu_buf_t *db; 10622082Seschrock char *packed = NULL; 10632082Seschrock size_t nvsize = 0; 10642082Seschrock int error; 10652082Seschrock *value = NULL; 10662082Seschrock 10672082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 10682082Seschrock nvsize = *(uint64_t *)db->db_data; 10692082Seschrock dmu_buf_rele(db, FTAG); 10702082Seschrock 10712082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 10729512SNeil.Perrin@Sun.COM error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 10739512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 10742082Seschrock if (error == 0) 10752082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 10762082Seschrock kmem_free(packed, nvsize); 10772082Seschrock 10782082Seschrock return (error); 10792082Seschrock } 10802082Seschrock 10812082Seschrock /* 10824451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 10834451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 10844451Seschrock */ 10854451Seschrock static void 10864451Seschrock spa_check_removed(vdev_t *vd) 10874451Seschrock { 10889816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 10894451Seschrock spa_check_removed(vd->vdev_child[c]); 10904451Seschrock 10914451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 10924451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 10934451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 10944451Seschrock } 10954451Seschrock } 10964451Seschrock 10974451Seschrock /* 10989701SGeorge.Wilson@Sun.COM * Load the slog device state from the config object since it's possible 10999701SGeorge.Wilson@Sun.COM * that the label does not contain the most up-to-date information. 11009701SGeorge.Wilson@Sun.COM */ 11019701SGeorge.Wilson@Sun.COM void 11029701SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa) 11039701SGeorge.Wilson@Sun.COM { 11049701SGeorge.Wilson@Sun.COM nvlist_t *nv, *nvroot, **child; 11059701SGeorge.Wilson@Sun.COM uint64_t is_log; 11069816SGeorge.Wilson@Sun.COM uint_t children; 11079701SGeorge.Wilson@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 11089701SGeorge.Wilson@Sun.COM 11099701SGeorge.Wilson@Sun.COM VERIFY(load_nvlist(spa, spa->spa_config_object, &nv) == 0); 11109701SGeorge.Wilson@Sun.COM VERIFY(nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 11119701SGeorge.Wilson@Sun.COM VERIFY(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 11129701SGeorge.Wilson@Sun.COM &child, &children) == 0); 11139701SGeorge.Wilson@Sun.COM 11149816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 11159701SGeorge.Wilson@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 11169701SGeorge.Wilson@Sun.COM 11179701SGeorge.Wilson@Sun.COM if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 11189701SGeorge.Wilson@Sun.COM &is_log) == 0 && is_log) 11199701SGeorge.Wilson@Sun.COM vdev_load_log_state(tvd, child[c]); 11209701SGeorge.Wilson@Sun.COM } 11219701SGeorge.Wilson@Sun.COM nvlist_free(nv); 11229701SGeorge.Wilson@Sun.COM } 11239701SGeorge.Wilson@Sun.COM 11249701SGeorge.Wilson@Sun.COM /* 11257294Sperrin * Check for missing log devices 11267294Sperrin */ 11277294Sperrin int 11287294Sperrin spa_check_logs(spa_t *spa) 11297294Sperrin { 11307294Sperrin switch (spa->spa_log_state) { 11317294Sperrin case SPA_LOG_MISSING: 11327294Sperrin /* need to recheck in case slog has been restored */ 11337294Sperrin case SPA_LOG_UNKNOWN: 11347294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 11357294Sperrin DS_FIND_CHILDREN)) { 11367294Sperrin spa->spa_log_state = SPA_LOG_MISSING; 11377294Sperrin return (1); 11387294Sperrin } 11397294Sperrin break; 11407294Sperrin } 11417294Sperrin return (0); 11427294Sperrin } 11437294Sperrin 11447294Sperrin /* 1145789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 11461544Seschrock * source of configuration information. 1147789Sahrens */ 1148789Sahrens static int 11491544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 1150789Sahrens { 1151789Sahrens int error = 0; 1152789Sahrens nvlist_t *nvroot = NULL; 1153789Sahrens vdev_t *rvd; 1154789Sahrens uberblock_t *ub = &spa->spa_uberblock; 11551635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 1156789Sahrens uint64_t pool_guid; 11572082Seschrock uint64_t version; 11584451Seschrock uint64_t autoreplace = 0; 11598241SJeff.Bonwick@Sun.COM int orig_mode = spa->spa_mode; 11607294Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 1161789Sahrens 11628241SJeff.Bonwick@Sun.COM /* 11638241SJeff.Bonwick@Sun.COM * If this is an untrusted config, access the pool in read-only mode. 11648241SJeff.Bonwick@Sun.COM * This prevents things like resilvering recently removed devices. 11658241SJeff.Bonwick@Sun.COM */ 11668241SJeff.Bonwick@Sun.COM if (!mosconfig) 11678241SJeff.Bonwick@Sun.COM spa->spa_mode = FREAD; 11688241SJeff.Bonwick@Sun.COM 11697754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 11707754SJeff.Bonwick@Sun.COM 11711544Seschrock spa->spa_load_state = state; 11721635Sbonwick 1173789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 11741733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 11751544Seschrock error = EINVAL; 11761544Seschrock goto out; 11771544Seschrock } 1178789Sahrens 11792082Seschrock /* 11802082Seschrock * Versioning wasn't explicitly added to the label until later, so if 11812082Seschrock * it's not present treat it as the initial version. 11822082Seschrock */ 11832082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 11844577Sahrens version = SPA_VERSION_INITIAL; 11852082Seschrock 11861733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 11871733Sbonwick &spa->spa_config_txg); 11881733Sbonwick 11891635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 11901544Seschrock spa_guid_exists(pool_guid, 0)) { 11911544Seschrock error = EEXIST; 11921544Seschrock goto out; 11931544Seschrock } 1194789Sahrens 11952174Seschrock spa->spa_load_guid = pool_guid; 11962174Seschrock 1197789Sahrens /* 11989234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 11999234SGeorge.Wilson@Sun.COM */ 12009630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 12019630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 12029234SGeorge.Wilson@Sun.COM 12039234SGeorge.Wilson@Sun.COM /* 12042082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 12052082Seschrock * value that will be returned by spa_version() since parsing the 12062082Seschrock * configuration requires knowing the version number. 1207789Sahrens */ 12087754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12092082Seschrock spa->spa_ubsync.ub_version = version; 12102082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 12117754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1212789Sahrens 12132082Seschrock if (error != 0) 12141544Seschrock goto out; 1215789Sahrens 12161585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1217789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1218789Sahrens 1219789Sahrens /* 1220789Sahrens * Try to open all vdevs, loading each label in the process. 1221789Sahrens */ 12227754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12234070Smc142369 error = vdev_open(rvd); 12247754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 12254070Smc142369 if (error != 0) 12261544Seschrock goto out; 1227789Sahrens 1228789Sahrens /* 12299276SMark.Musante@Sun.COM * We need to validate the vdev labels against the configuration that 12309276SMark.Musante@Sun.COM * we have in hand, which is dependent on the setting of mosconfig. If 12319276SMark.Musante@Sun.COM * mosconfig is true then we're validating the vdev labels based on 12329276SMark.Musante@Sun.COM * that config. Otherwise, we're validating against the cached config 12339276SMark.Musante@Sun.COM * (zpool.cache) that was read when we loaded the zfs module, and then 12349276SMark.Musante@Sun.COM * later we will recursively call spa_load() and validate against 12359276SMark.Musante@Sun.COM * the vdev config. 12361986Seschrock */ 12379276SMark.Musante@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12389276SMark.Musante@Sun.COM error = vdev_validate(rvd); 12399276SMark.Musante@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 12409276SMark.Musante@Sun.COM if (error != 0) 12419276SMark.Musante@Sun.COM goto out; 12421986Seschrock 12431986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 12441986Seschrock error = ENXIO; 12451986Seschrock goto out; 12461986Seschrock } 12471986Seschrock 12481986Seschrock /* 1249789Sahrens * Find the best uberblock. 1250789Sahrens */ 12517754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1252789Sahrens 1253789Sahrens /* 1254789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1255789Sahrens */ 1256789Sahrens if (ub->ub_txg == 0) { 12571760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12581760Seschrock VDEV_AUX_CORRUPT_DATA); 12591544Seschrock error = ENXIO; 12601544Seschrock goto out; 12611544Seschrock } 12621544Seschrock 12631544Seschrock /* 12641544Seschrock * If the pool is newer than the code, we can't open it. 12651544Seschrock */ 12664577Sahrens if (ub->ub_version > SPA_VERSION) { 12671760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12681760Seschrock VDEV_AUX_VERSION_NEWER); 12691544Seschrock error = ENOTSUP; 12701544Seschrock goto out; 1271789Sahrens } 1272789Sahrens 1273789Sahrens /* 1274789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1275789Sahrens * incomplete configuration. 1276789Sahrens */ 12771732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 12781544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12791544Seschrock VDEV_AUX_BAD_GUID_SUM); 12801544Seschrock error = ENXIO; 12811544Seschrock goto out; 1282789Sahrens } 1283789Sahrens 1284789Sahrens /* 1285789Sahrens * Initialize internal SPA structures. 1286789Sahrens */ 1287789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1288789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1289789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 12901544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 12911544Seschrock if (error) { 12921544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12931544Seschrock VDEV_AUX_CORRUPT_DATA); 12941544Seschrock goto out; 12951544Seschrock } 1296789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1297789Sahrens 12981544Seschrock if (zap_lookup(spa->spa_meta_objset, 1299789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 13001544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 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 if (!mosconfig) { 13082082Seschrock nvlist_t *newconfig; 13093975Sek110237 uint64_t hostid; 13102082Seschrock 13112082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 13121544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13131544Seschrock VDEV_AUX_CORRUPT_DATA); 13141544Seschrock error = EIO; 13151544Seschrock goto out; 13161544Seschrock } 1317789Sahrens 13187706SLin.Ling@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig, 13197706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 13203975Sek110237 char *hostname; 13213975Sek110237 unsigned long myhostid = 0; 13223975Sek110237 13233975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 13243975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 13253975Sek110237 13268662SJordan.Vaughan@Sun.com #ifdef _KERNEL 13278662SJordan.Vaughan@Sun.com myhostid = zone_get_hostid(NULL); 13288662SJordan.Vaughan@Sun.com #else /* _KERNEL */ 13298662SJordan.Vaughan@Sun.com /* 13308662SJordan.Vaughan@Sun.com * We're emulating the system's hostid in userland, so 13318662SJordan.Vaughan@Sun.com * we can't use zone_get_hostid(). 13328662SJordan.Vaughan@Sun.com */ 13333975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 13348662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 13354178Slling if (hostid != 0 && myhostid != 0 && 13368662SJordan.Vaughan@Sun.com hostid != myhostid) { 13373975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 13383975Sek110237 "loaded as it was last accessed by " 13397706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 13403975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 13417754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 13423975Sek110237 (unsigned long)hostid); 13433975Sek110237 error = EBADF; 13443975Sek110237 goto out; 13453975Sek110237 } 13463975Sek110237 } 13473975Sek110237 1348789Sahrens spa_config_set(spa, newconfig); 1349789Sahrens spa_unload(spa); 1350789Sahrens spa_deactivate(spa); 13518241SJeff.Bonwick@Sun.COM spa_activate(spa, orig_mode); 1352789Sahrens 13531544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 13541544Seschrock } 13551544Seschrock 13561544Seschrock if (zap_lookup(spa->spa_meta_objset, 13571544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 13581544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 13591544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13601544Seschrock VDEV_AUX_CORRUPT_DATA); 13611544Seschrock error = EIO; 13621544Seschrock goto out; 1363789Sahrens } 1364789Sahrens 13651544Seschrock /* 13662082Seschrock * Load the bit that tells us to use the new accounting function 13672082Seschrock * (raid-z deflation). If we have an older pool, this will not 13682082Seschrock * be present. 13692082Seschrock */ 13702082Seschrock error = zap_lookup(spa->spa_meta_objset, 13712082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 13722082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 13732082Seschrock if (error != 0 && error != ENOENT) { 13742082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13752082Seschrock VDEV_AUX_CORRUPT_DATA); 13762082Seschrock error = EIO; 13772082Seschrock goto out; 13782082Seschrock } 13792082Seschrock 13802082Seschrock /* 13811544Seschrock * Load the persistent error log. If we have an older pool, this will 13821544Seschrock * not be present. 13831544Seschrock */ 13841544Seschrock error = zap_lookup(spa->spa_meta_objset, 13851544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 13861544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 13871807Sbonwick if (error != 0 && error != ENOENT) { 13881544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13891544Seschrock VDEV_AUX_CORRUPT_DATA); 13901544Seschrock error = EIO; 13911544Seschrock goto out; 13921544Seschrock } 13931544Seschrock 13941544Seschrock error = zap_lookup(spa->spa_meta_objset, 13951544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 13961544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 13971544Seschrock if (error != 0 && error != ENOENT) { 13981544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13991544Seschrock VDEV_AUX_CORRUPT_DATA); 14001544Seschrock error = EIO; 14011544Seschrock goto out; 14021544Seschrock } 1403789Sahrens 1404789Sahrens /* 14052926Sek110237 * Load the history object. If we have an older pool, this 14062926Sek110237 * will not be present. 14072926Sek110237 */ 14082926Sek110237 error = zap_lookup(spa->spa_meta_objset, 14092926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 14102926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 14112926Sek110237 if (error != 0 && error != ENOENT) { 14122926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14132926Sek110237 VDEV_AUX_CORRUPT_DATA); 14142926Sek110237 error = EIO; 14152926Sek110237 goto out; 14162926Sek110237 } 14172926Sek110237 14182926Sek110237 /* 14192082Seschrock * Load any hot spares for this pool. 14202082Seschrock */ 14212082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 14225450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 14232082Seschrock if (error != 0 && error != ENOENT) { 14242082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14252082Seschrock VDEV_AUX_CORRUPT_DATA); 14262082Seschrock error = EIO; 14272082Seschrock goto out; 14282082Seschrock } 14292082Seschrock if (error == 0) { 14304577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 14315450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 14325450Sbrendan &spa->spa_spares.sav_config) != 0) { 14332082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14342082Seschrock VDEV_AUX_CORRUPT_DATA); 14352082Seschrock error = EIO; 14362082Seschrock goto out; 14372082Seschrock } 14382082Seschrock 14397754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 14402082Seschrock spa_load_spares(spa); 14417754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 14422082Seschrock } 14432082Seschrock 14445450Sbrendan /* 14455450Sbrendan * Load any level 2 ARC devices for this pool. 14465450Sbrendan */ 14475450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 14485450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 14495450Sbrendan &spa->spa_l2cache.sav_object); 14505450Sbrendan if (error != 0 && error != ENOENT) { 14515450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14525450Sbrendan VDEV_AUX_CORRUPT_DATA); 14535450Sbrendan error = EIO; 14545450Sbrendan goto out; 14555450Sbrendan } 14565450Sbrendan if (error == 0) { 14575450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 14585450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 14595450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 14605450Sbrendan vdev_set_state(rvd, B_TRUE, 14615450Sbrendan VDEV_STATE_CANT_OPEN, 14625450Sbrendan VDEV_AUX_CORRUPT_DATA); 14635450Sbrendan error = EIO; 14645450Sbrendan goto out; 14655450Sbrendan } 14665450Sbrendan 14677754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 14685450Sbrendan spa_load_l2cache(spa); 14697754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 14705450Sbrendan } 14715450Sbrendan 14729701SGeorge.Wilson@Sun.COM spa_load_log_state(spa); 14739701SGeorge.Wilson@Sun.COM 14747294Sperrin if (spa_check_logs(spa)) { 14757294Sperrin vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14767294Sperrin VDEV_AUX_BAD_LOG); 14777294Sperrin error = ENXIO; 14787294Sperrin ereport = FM_EREPORT_ZFS_LOG_REPLAY; 14797294Sperrin goto out; 14807294Sperrin } 14817294Sperrin 14827294Sperrin 14835094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 14844543Smarks 14853912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 14863912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 14873912Slling 14883912Slling if (error && error != ENOENT) { 14893912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14903912Slling VDEV_AUX_CORRUPT_DATA); 14913912Slling error = EIO; 14923912Slling goto out; 14933912Slling } 14943912Slling 14953912Slling if (error == 0) { 14963912Slling (void) zap_lookup(spa->spa_meta_objset, 14973912Slling spa->spa_pool_props_object, 14984451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 14993912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 15004451Seschrock (void) zap_lookup(spa->spa_meta_objset, 15014451Seschrock spa->spa_pool_props_object, 15024451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 15034451Seschrock sizeof (uint64_t), 1, &autoreplace); 15044543Smarks (void) zap_lookup(spa->spa_meta_objset, 15054543Smarks spa->spa_pool_props_object, 15064543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 15074543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 15085329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 15095329Sgw25295 spa->spa_pool_props_object, 15105329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 15115329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 15129816SGeorge.Wilson@Sun.COM (void) zap_lookup(spa->spa_meta_objset, 15139816SGeorge.Wilson@Sun.COM spa->spa_pool_props_object, 15149816SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND), 15159816SGeorge.Wilson@Sun.COM sizeof (uint64_t), 1, &spa->spa_autoexpand); 15163912Slling } 15173912Slling 15182082Seschrock /* 15194451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 15204451Seschrock * the ZFS DE that it should not issue any faults for unopenable 15214451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 15224451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 15234451Seschrock * over. 15244451Seschrock */ 15255756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 15264451Seschrock spa_check_removed(spa->spa_root_vdev); 15274451Seschrock 15284451Seschrock /* 15291986Seschrock * Load the vdev state for all toplevel vdevs. 1530789Sahrens */ 15311986Seschrock vdev_load(rvd); 1532789Sahrens 1533789Sahrens /* 1534789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1535789Sahrens */ 15367754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1537789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 15387754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1539789Sahrens 1540789Sahrens /* 1541789Sahrens * Check the state of the root vdev. If it can't be opened, it 1542789Sahrens * indicates one or more toplevel vdevs are faulted. 1543789Sahrens */ 15441544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 15451544Seschrock error = ENXIO; 15461544Seschrock goto out; 15471544Seschrock } 1548789Sahrens 15498241SJeff.Bonwick@Sun.COM if (spa_writeable(spa)) { 15501635Sbonwick dmu_tx_t *tx; 15511635Sbonwick int need_update = B_FALSE; 15528241SJeff.Bonwick@Sun.COM 15538241SJeff.Bonwick@Sun.COM ASSERT(state != SPA_LOAD_TRYIMPORT); 15541601Sbonwick 15551635Sbonwick /* 15561635Sbonwick * Claim log blocks that haven't been committed yet. 15571635Sbonwick * This must all happen in a single txg. 15581635Sbonwick */ 15591601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1560789Sahrens spa_first_txg(spa)); 15617754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 15622417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1563789Sahrens dmu_tx_commit(tx); 1564789Sahrens 15659701SGeorge.Wilson@Sun.COM spa->spa_log_state = SPA_LOG_GOOD; 1566789Sahrens spa->spa_sync_on = B_TRUE; 1567789Sahrens txg_sync_start(spa->spa_dsl_pool); 1568789Sahrens 1569789Sahrens /* 1570789Sahrens * Wait for all claims to sync. 1571789Sahrens */ 1572789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 15731585Sbonwick 15741585Sbonwick /* 15751635Sbonwick * If the config cache is stale, or we have uninitialized 15761635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 1577*10100SLin.Ling@Sun.COM * 1578*10100SLin.Ling@Sun.COM * If spa_load_verbatim is true, trust the current 1579*10100SLin.Ling@Sun.COM * in-core spa_config and update the disk labels. 15801585Sbonwick */ 15811635Sbonwick if (config_cache_txg != spa->spa_config_txg || 1582*10100SLin.Ling@Sun.COM state == SPA_LOAD_IMPORT || spa->spa_load_verbatim) 15831635Sbonwick need_update = B_TRUE; 15841635Sbonwick 15858241SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) 15861635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 15871635Sbonwick need_update = B_TRUE; 15881585Sbonwick 15891585Sbonwick /* 15901635Sbonwick * Update the config cache asychronously in case we're the 15911635Sbonwick * root pool, in which case the config cache isn't writable yet. 15921585Sbonwick */ 15931635Sbonwick if (need_update) 15941635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 15958241SJeff.Bonwick@Sun.COM 15968241SJeff.Bonwick@Sun.COM /* 15978241SJeff.Bonwick@Sun.COM * Check all DTLs to see if anything needs resilvering. 15988241SJeff.Bonwick@Sun.COM */ 15998241SJeff.Bonwick@Sun.COM if (vdev_resilver_needed(rvd, NULL, NULL)) 16008241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 1601789Sahrens } 1602789Sahrens 16031544Seschrock error = 0; 16041544Seschrock out: 16057046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 16062082Seschrock if (error && error != EBADF) 16077294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 16081544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 16091544Seschrock spa->spa_ena = 0; 16101544Seschrock 16111544Seschrock return (error); 1612789Sahrens } 1613789Sahrens 1614789Sahrens /* 1615789Sahrens * Pool Open/Import 1616789Sahrens * 1617789Sahrens * The import case is identical to an open except that the configuration is sent 1618789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1619789Sahrens * case of an open, the pool configuration will exist in the 16204451Seschrock * POOL_STATE_UNINITIALIZED state. 1621789Sahrens * 1622789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1623789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1624789Sahrens * ambiguous state. 1625789Sahrens */ 1626789Sahrens static int 1627789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1628789Sahrens { 1629789Sahrens spa_t *spa; 1630789Sahrens int error; 1631789Sahrens int locked = B_FALSE; 1632789Sahrens 1633789Sahrens *spapp = NULL; 1634789Sahrens 1635789Sahrens /* 1636789Sahrens * As disgusting as this is, we need to support recursive calls to this 1637789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1638789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1639789Sahrens * avoid dsl_dir_open() calling this in the first place. 1640789Sahrens */ 1641789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1642789Sahrens mutex_enter(&spa_namespace_lock); 1643789Sahrens locked = B_TRUE; 1644789Sahrens } 1645789Sahrens 1646789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1647789Sahrens if (locked) 1648789Sahrens mutex_exit(&spa_namespace_lock); 1649789Sahrens return (ENOENT); 1650789Sahrens } 1651789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1652789Sahrens 16538241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1654789Sahrens 16551635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1656789Sahrens 1657789Sahrens if (error == EBADF) { 1658789Sahrens /* 16591986Seschrock * If vdev_validate() returns failure (indicated by 16601986Seschrock * EBADF), it indicates that one of the vdevs indicates 16611986Seschrock * that the pool has been exported or destroyed. If 16621986Seschrock * this is the case, the config cache is out of sync and 16631986Seschrock * we should remove the pool from the namespace. 1664789Sahrens */ 1665789Sahrens spa_unload(spa); 1666789Sahrens spa_deactivate(spa); 16676643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1668789Sahrens spa_remove(spa); 1669789Sahrens if (locked) 1670789Sahrens mutex_exit(&spa_namespace_lock); 1671789Sahrens return (ENOENT); 16721544Seschrock } 16731544Seschrock 16741544Seschrock if (error) { 1675789Sahrens /* 1676789Sahrens * We can't open the pool, but we still have useful 1677789Sahrens * information: the state of each vdev after the 1678789Sahrens * attempted vdev_open(). Return this to the user. 1679789Sahrens */ 16807754SJeff.Bonwick@Sun.COM if (config != NULL && spa->spa_root_vdev != NULL) 1681789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1682789Sahrens B_TRUE); 1683789Sahrens spa_unload(spa); 1684789Sahrens spa_deactivate(spa); 16851544Seschrock spa->spa_last_open_failed = B_TRUE; 1686789Sahrens if (locked) 1687789Sahrens mutex_exit(&spa_namespace_lock); 1688789Sahrens *spapp = NULL; 1689789Sahrens return (error); 16901544Seschrock } else { 16911544Seschrock spa->spa_last_open_failed = B_FALSE; 1692789Sahrens } 1693789Sahrens } 1694789Sahrens 1695789Sahrens spa_open_ref(spa, tag); 16964451Seschrock 1697789Sahrens if (locked) 1698789Sahrens mutex_exit(&spa_namespace_lock); 1699789Sahrens 1700789Sahrens *spapp = spa; 1701789Sahrens 17027754SJeff.Bonwick@Sun.COM if (config != NULL) 1703789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1704789Sahrens 1705789Sahrens return (0); 1706789Sahrens } 1707789Sahrens 1708789Sahrens int 1709789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1710789Sahrens { 1711789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1712789Sahrens } 1713789Sahrens 17141544Seschrock /* 17151544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 17161544Seschrock * preventing it from being exported or destroyed. 17171544Seschrock */ 17181544Seschrock spa_t * 17191544Seschrock spa_inject_addref(char *name) 17201544Seschrock { 17211544Seschrock spa_t *spa; 17221544Seschrock 17231544Seschrock mutex_enter(&spa_namespace_lock); 17241544Seschrock if ((spa = spa_lookup(name)) == NULL) { 17251544Seschrock mutex_exit(&spa_namespace_lock); 17261544Seschrock return (NULL); 17271544Seschrock } 17281544Seschrock spa->spa_inject_ref++; 17291544Seschrock mutex_exit(&spa_namespace_lock); 17301544Seschrock 17311544Seschrock return (spa); 17321544Seschrock } 17331544Seschrock 17341544Seschrock void 17351544Seschrock spa_inject_delref(spa_t *spa) 17361544Seschrock { 17371544Seschrock mutex_enter(&spa_namespace_lock); 17381544Seschrock spa->spa_inject_ref--; 17391544Seschrock mutex_exit(&spa_namespace_lock); 17401544Seschrock } 17411544Seschrock 17425450Sbrendan /* 17435450Sbrendan * Add spares device information to the nvlist. 17445450Sbrendan */ 17452082Seschrock static void 17462082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 17472082Seschrock { 17482082Seschrock nvlist_t **spares; 17492082Seschrock uint_t i, nspares; 17502082Seschrock nvlist_t *nvroot; 17512082Seschrock uint64_t guid; 17522082Seschrock vdev_stat_t *vs; 17532082Seschrock uint_t vsc; 17543377Seschrock uint64_t pool; 17552082Seschrock 17569425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 17579425SEric.Schrock@Sun.COM 17585450Sbrendan if (spa->spa_spares.sav_count == 0) 17592082Seschrock return; 17602082Seschrock 17612082Seschrock VERIFY(nvlist_lookup_nvlist(config, 17622082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 17635450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 17642082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 17652082Seschrock if (nspares != 0) { 17662082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 17672082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 17682082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 17692082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 17702082Seschrock 17712082Seschrock /* 17722082Seschrock * Go through and find any spares which have since been 17732082Seschrock * repurposed as an active spare. If this is the case, update 17742082Seschrock * their status appropriately. 17752082Seschrock */ 17762082Seschrock for (i = 0; i < nspares; i++) { 17772082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 17782082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 17797214Slling if (spa_spare_exists(guid, &pool, NULL) && 17807214Slling pool != 0ULL) { 17812082Seschrock VERIFY(nvlist_lookup_uint64_array( 17822082Seschrock spares[i], ZPOOL_CONFIG_STATS, 17832082Seschrock (uint64_t **)&vs, &vsc) == 0); 17842082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 17852082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 17862082Seschrock } 17872082Seschrock } 17882082Seschrock } 17892082Seschrock } 17902082Seschrock 17915450Sbrendan /* 17925450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 17935450Sbrendan */ 17945450Sbrendan static void 17955450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 17965450Sbrendan { 17975450Sbrendan nvlist_t **l2cache; 17985450Sbrendan uint_t i, j, nl2cache; 17995450Sbrendan nvlist_t *nvroot; 18005450Sbrendan uint64_t guid; 18015450Sbrendan vdev_t *vd; 18025450Sbrendan vdev_stat_t *vs; 18035450Sbrendan uint_t vsc; 18045450Sbrendan 18059425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 18069425SEric.Schrock@Sun.COM 18075450Sbrendan if (spa->spa_l2cache.sav_count == 0) 18085450Sbrendan return; 18095450Sbrendan 18105450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 18115450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 18125450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 18135450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 18145450Sbrendan if (nl2cache != 0) { 18155450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 18165450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 18175450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 18185450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 18195450Sbrendan 18205450Sbrendan /* 18215450Sbrendan * Update level 2 cache device stats. 18225450Sbrendan */ 18235450Sbrendan 18245450Sbrendan for (i = 0; i < nl2cache; i++) { 18255450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 18265450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 18275450Sbrendan 18285450Sbrendan vd = NULL; 18295450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 18305450Sbrendan if (guid == 18315450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 18325450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 18335450Sbrendan break; 18345450Sbrendan } 18355450Sbrendan } 18365450Sbrendan ASSERT(vd != NULL); 18375450Sbrendan 18385450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 18395450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 18405450Sbrendan vdev_get_stats(vd, vs); 18415450Sbrendan } 18425450Sbrendan } 18435450Sbrendan } 18445450Sbrendan 1845789Sahrens int 18461544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1847789Sahrens { 1848789Sahrens int error; 1849789Sahrens spa_t *spa; 1850789Sahrens 1851789Sahrens *config = NULL; 1852789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1853789Sahrens 18549425SEric.Schrock@Sun.COM if (spa != NULL) { 18559425SEric.Schrock@Sun.COM /* 18569425SEric.Schrock@Sun.COM * This still leaves a window of inconsistency where the spares 18579425SEric.Schrock@Sun.COM * or l2cache devices could change and the config would be 18589425SEric.Schrock@Sun.COM * self-inconsistent. 18599425SEric.Schrock@Sun.COM */ 18609425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 18619425SEric.Schrock@Sun.COM 18629425SEric.Schrock@Sun.COM if (*config != NULL) { 18637754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 18649425SEric.Schrock@Sun.COM ZPOOL_CONFIG_ERRCOUNT, 18659425SEric.Schrock@Sun.COM spa_get_errlog_size(spa)) == 0); 18669425SEric.Schrock@Sun.COM 18679425SEric.Schrock@Sun.COM if (spa_suspended(spa)) 18689425SEric.Schrock@Sun.COM VERIFY(nvlist_add_uint64(*config, 18699425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SUSPENDED, 18709425SEric.Schrock@Sun.COM spa->spa_failmode) == 0); 18719425SEric.Schrock@Sun.COM 18729425SEric.Schrock@Sun.COM spa_add_spares(spa, *config); 18739425SEric.Schrock@Sun.COM spa_add_l2cache(spa, *config); 18749425SEric.Schrock@Sun.COM } 18752082Seschrock } 18762082Seschrock 18771544Seschrock /* 18781544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 18791544Seschrock * and call spa_lookup() directly. 18801544Seschrock */ 18811544Seschrock if (altroot) { 18821544Seschrock if (spa == NULL) { 18831544Seschrock mutex_enter(&spa_namespace_lock); 18841544Seschrock spa = spa_lookup(name); 18851544Seschrock if (spa) 18861544Seschrock spa_altroot(spa, altroot, buflen); 18871544Seschrock else 18881544Seschrock altroot[0] = '\0'; 18891544Seschrock spa = NULL; 18901544Seschrock mutex_exit(&spa_namespace_lock); 18911544Seschrock } else { 18921544Seschrock spa_altroot(spa, altroot, buflen); 18931544Seschrock } 18941544Seschrock } 18951544Seschrock 18969425SEric.Schrock@Sun.COM if (spa != NULL) { 18979425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 1898789Sahrens spa_close(spa, FTAG); 18999425SEric.Schrock@Sun.COM } 1900789Sahrens 1901789Sahrens return (error); 1902789Sahrens } 1903789Sahrens 1904789Sahrens /* 19055450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 19065450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 19075450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 19085450Sbrendan * specified, as long as they are well-formed. 19092082Seschrock */ 19102082Seschrock static int 19115450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 19125450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 19135450Sbrendan vdev_labeltype_t label) 19142082Seschrock { 19155450Sbrendan nvlist_t **dev; 19165450Sbrendan uint_t i, ndev; 19172082Seschrock vdev_t *vd; 19182082Seschrock int error; 19192082Seschrock 19207754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 19217754SJeff.Bonwick@Sun.COM 19222082Seschrock /* 19235450Sbrendan * It's acceptable to have no devs specified. 19242082Seschrock */ 19255450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 19262082Seschrock return (0); 19272082Seschrock 19285450Sbrendan if (ndev == 0) 19292082Seschrock return (EINVAL); 19302082Seschrock 19312082Seschrock /* 19325450Sbrendan * Make sure the pool is formatted with a version that supports this 19335450Sbrendan * device type. 19342082Seschrock */ 19355450Sbrendan if (spa_version(spa) < version) 19362082Seschrock return (ENOTSUP); 19372082Seschrock 19383377Seschrock /* 19395450Sbrendan * Set the pending device list so we correctly handle device in-use 19403377Seschrock * checking. 19413377Seschrock */ 19425450Sbrendan sav->sav_pending = dev; 19435450Sbrendan sav->sav_npending = ndev; 19445450Sbrendan 19455450Sbrendan for (i = 0; i < ndev; i++) { 19465450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 19472082Seschrock mode)) != 0) 19483377Seschrock goto out; 19492082Seschrock 19502082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 19512082Seschrock vdev_free(vd); 19523377Seschrock error = EINVAL; 19533377Seschrock goto out; 19542082Seschrock } 19552082Seschrock 19565450Sbrendan /* 19577754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 19587754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 19595450Sbrendan */ 19607754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 19615450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 19625450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 19635450Sbrendan error = ENOTBLK; 19645450Sbrendan goto out; 19655450Sbrendan } 19667754SJeff.Bonwick@Sun.COM #endif 19672082Seschrock vd->vdev_top = vd; 19683377Seschrock 19693377Seschrock if ((error = vdev_open(vd)) == 0 && 19705450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 19715450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 19723377Seschrock vd->vdev_guid) == 0); 19732082Seschrock } 19742082Seschrock 19752082Seschrock vdev_free(vd); 19763377Seschrock 19775450Sbrendan if (error && 19785450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 19793377Seschrock goto out; 19803377Seschrock else 19813377Seschrock error = 0; 19822082Seschrock } 19832082Seschrock 19843377Seschrock out: 19855450Sbrendan sav->sav_pending = NULL; 19865450Sbrendan sav->sav_npending = 0; 19873377Seschrock return (error); 19882082Seschrock } 19892082Seschrock 19905450Sbrendan static int 19915450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 19925450Sbrendan { 19935450Sbrendan int error; 19945450Sbrendan 19957754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 19967754SJeff.Bonwick@Sun.COM 19975450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 19985450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 19995450Sbrendan VDEV_LABEL_SPARE)) != 0) { 20005450Sbrendan return (error); 20015450Sbrendan } 20025450Sbrendan 20035450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 20045450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 20055450Sbrendan VDEV_LABEL_L2CACHE)); 20065450Sbrendan } 20075450Sbrendan 20085450Sbrendan static void 20095450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 20105450Sbrendan const char *config) 20115450Sbrendan { 20125450Sbrendan int i; 20135450Sbrendan 20145450Sbrendan if (sav->sav_config != NULL) { 20155450Sbrendan nvlist_t **olddevs; 20165450Sbrendan uint_t oldndevs; 20175450Sbrendan nvlist_t **newdevs; 20185450Sbrendan 20195450Sbrendan /* 20205450Sbrendan * Generate new dev list by concatentating with the 20215450Sbrendan * current dev list. 20225450Sbrendan */ 20235450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 20245450Sbrendan &olddevs, &oldndevs) == 0); 20255450Sbrendan 20265450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 20275450Sbrendan (ndevs + oldndevs), KM_SLEEP); 20285450Sbrendan for (i = 0; i < oldndevs; i++) 20295450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 20305450Sbrendan KM_SLEEP) == 0); 20315450Sbrendan for (i = 0; i < ndevs; i++) 20325450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 20335450Sbrendan KM_SLEEP) == 0); 20345450Sbrendan 20355450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 20365450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 20375450Sbrendan 20385450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 20395450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 20405450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 20415450Sbrendan nvlist_free(newdevs[i]); 20425450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 20435450Sbrendan } else { 20445450Sbrendan /* 20455450Sbrendan * Generate a new dev list. 20465450Sbrendan */ 20475450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 20485450Sbrendan KM_SLEEP) == 0); 20495450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 20505450Sbrendan devs, ndevs) == 0); 20515450Sbrendan } 20525450Sbrendan } 20535450Sbrendan 20545450Sbrendan /* 20555450Sbrendan * Stop and drop level 2 ARC devices 20565450Sbrendan */ 20575450Sbrendan void 20585450Sbrendan spa_l2cache_drop(spa_t *spa) 20595450Sbrendan { 20605450Sbrendan vdev_t *vd; 20615450Sbrendan int i; 20625450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 20635450Sbrendan 20645450Sbrendan for (i = 0; i < sav->sav_count; i++) { 20655450Sbrendan uint64_t pool; 20665450Sbrendan 20675450Sbrendan vd = sav->sav_vdevs[i]; 20685450Sbrendan ASSERT(vd != NULL); 20695450Sbrendan 20708241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 20718241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 20725450Sbrendan l2arc_remove_vdev(vd); 20735450Sbrendan if (vd->vdev_isl2cache) 20745450Sbrendan spa_l2cache_remove(vd); 20755450Sbrendan vdev_clear_stats(vd); 20765450Sbrendan (void) vdev_close(vd); 20775450Sbrendan } 20785450Sbrendan } 20795450Sbrendan 20802082Seschrock /* 2081789Sahrens * Pool Creation 2082789Sahrens */ 2083789Sahrens int 20845094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 20857184Stimh const char *history_str, nvlist_t *zplprops) 2086789Sahrens { 2087789Sahrens spa_t *spa; 20885094Slling char *altroot = NULL; 20891635Sbonwick vdev_t *rvd; 2090789Sahrens dsl_pool_t *dp; 2091789Sahrens dmu_tx_t *tx; 20929816SGeorge.Wilson@Sun.COM int error = 0; 2093789Sahrens uint64_t txg = TXG_INITIAL; 20945450Sbrendan nvlist_t **spares, **l2cache; 20955450Sbrendan uint_t nspares, nl2cache; 20965094Slling uint64_t version; 2097789Sahrens 2098789Sahrens /* 2099789Sahrens * If this pool already exists, return failure. 2100789Sahrens */ 2101789Sahrens mutex_enter(&spa_namespace_lock); 2102789Sahrens if (spa_lookup(pool) != NULL) { 2103789Sahrens mutex_exit(&spa_namespace_lock); 2104789Sahrens return (EEXIST); 2105789Sahrens } 2106789Sahrens 2107789Sahrens /* 2108789Sahrens * Allocate a new spa_t structure. 2109789Sahrens */ 21105094Slling (void) nvlist_lookup_string(props, 21115094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 21121635Sbonwick spa = spa_add(pool, altroot); 21138241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2114789Sahrens 2115789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 21165094Slling 21175094Slling if (props && (error = spa_prop_validate(spa, props))) { 21185094Slling spa_deactivate(spa); 21195094Slling spa_remove(spa); 21206643Seschrock mutex_exit(&spa_namespace_lock); 21215094Slling return (error); 21225094Slling } 21235094Slling 21245094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 21255094Slling &version) != 0) 21265094Slling version = SPA_VERSION; 21275094Slling ASSERT(version <= SPA_VERSION); 21285094Slling spa->spa_uberblock.ub_version = version; 2129789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2130789Sahrens 21311635Sbonwick /* 21329234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 21339234SGeorge.Wilson@Sun.COM */ 21349630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 21359630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 21369234SGeorge.Wilson@Sun.COM 21379234SGeorge.Wilson@Sun.COM /* 21381635Sbonwick * Create the root vdev. 21391635Sbonwick */ 21407754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21411635Sbonwick 21422082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 21432082Seschrock 21442082Seschrock ASSERT(error != 0 || rvd != NULL); 21452082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 21462082Seschrock 21475913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 21481635Sbonwick error = EINVAL; 21492082Seschrock 21502082Seschrock if (error == 0 && 21512082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 21525450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 21532082Seschrock VDEV_ALLOC_ADD)) == 0) { 21549816SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 21559816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(rvd->vdev_child[c]); 21569816SGeorge.Wilson@Sun.COM vdev_expand(rvd->vdev_child[c], txg); 21579816SGeorge.Wilson@Sun.COM } 21581635Sbonwick } 21591635Sbonwick 21607754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2161789Sahrens 21622082Seschrock if (error != 0) { 2163789Sahrens spa_unload(spa); 2164789Sahrens spa_deactivate(spa); 2165789Sahrens spa_remove(spa); 2166789Sahrens mutex_exit(&spa_namespace_lock); 2167789Sahrens return (error); 2168789Sahrens } 2169789Sahrens 21702082Seschrock /* 21712082Seschrock * Get the list of spares, if specified. 21722082Seschrock */ 21732082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 21742082Seschrock &spares, &nspares) == 0) { 21755450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 21762082Seschrock KM_SLEEP) == 0); 21775450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 21782082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 21797754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21802082Seschrock spa_load_spares(spa); 21817754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 21825450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 21835450Sbrendan } 21845450Sbrendan 21855450Sbrendan /* 21865450Sbrendan * Get the list of level 2 cache devices, if specified. 21875450Sbrendan */ 21885450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 21895450Sbrendan &l2cache, &nl2cache) == 0) { 21905450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 21915450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 21925450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 21935450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21947754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21955450Sbrendan spa_load_l2cache(spa); 21967754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 21975450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 21982082Seschrock } 21992082Seschrock 22007184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2201789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2202789Sahrens 2203789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2204789Sahrens 2205789Sahrens /* 2206789Sahrens * Create the pool config object. 2207789Sahrens */ 2208789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 22097497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2210789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2211789Sahrens 22121544Seschrock if (zap_add(spa->spa_meta_objset, 2213789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 22141544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 22151544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 22161544Seschrock } 2217789Sahrens 22185094Slling /* Newly created pools with the right version are always deflated. */ 22195094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 22205094Slling spa->spa_deflate = TRUE; 22215094Slling if (zap_add(spa->spa_meta_objset, 22225094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 22235094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 22245094Slling cmn_err(CE_PANIC, "failed to add deflate"); 22255094Slling } 22262082Seschrock } 22272082Seschrock 2228789Sahrens /* 2229789Sahrens * Create the deferred-free bplist object. Turn off compression 2230789Sahrens * because sync-to-convergence takes longer if the blocksize 2231789Sahrens * keeps changing. 2232789Sahrens */ 2233789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 2234789Sahrens 1 << 14, tx); 2235789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 2236789Sahrens ZIO_COMPRESS_OFF, tx); 2237789Sahrens 22381544Seschrock if (zap_add(spa->spa_meta_objset, 2239789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 22401544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 22411544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 22421544Seschrock } 2243789Sahrens 22442926Sek110237 /* 22452926Sek110237 * Create the pool's history object. 22462926Sek110237 */ 22475094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 22485094Slling spa_history_create_obj(spa, tx); 22495094Slling 22505094Slling /* 22515094Slling * Set pool properties. 22525094Slling */ 22535094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 22545094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 22555329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 22569816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 22578525SEric.Schrock@Sun.COM if (props != NULL) { 22588525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 22595094Slling spa_sync_props(spa, props, CRED(), tx); 22608525SEric.Schrock@Sun.COM } 22612926Sek110237 2262789Sahrens dmu_tx_commit(tx); 2263789Sahrens 2264789Sahrens spa->spa_sync_on = B_TRUE; 2265789Sahrens txg_sync_start(spa->spa_dsl_pool); 2266789Sahrens 2267789Sahrens /* 2268789Sahrens * We explicitly wait for the first transaction to complete so that our 2269789Sahrens * bean counters are appropriately updated. 2270789Sahrens */ 2271789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2272789Sahrens 22736643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2274789Sahrens 22755094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 22764715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 22779946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_CREATE); 22784715Sek110237 22798667SGeorge.Wilson@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 22808667SGeorge.Wilson@Sun.COM 2281789Sahrens mutex_exit(&spa_namespace_lock); 2282789Sahrens 2283789Sahrens return (0); 2284789Sahrens } 2285789Sahrens 22866423Sgw25295 #ifdef _KERNEL 22876423Sgw25295 /* 22889790SLin.Ling@Sun.COM * Get the root pool information from the root disk, then import the root pool 22899790SLin.Ling@Sun.COM * during the system boot up time. 22906423Sgw25295 */ 22919790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 22929790SLin.Ling@Sun.COM 22939790SLin.Ling@Sun.COM static nvlist_t * 22949790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 22956423Sgw25295 { 22969790SLin.Ling@Sun.COM nvlist_t *config; 22976423Sgw25295 nvlist_t *nvtop, *nvroot; 22986423Sgw25295 uint64_t pgid; 22996423Sgw25295 23009790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 23019790SLin.Ling@Sun.COM return (NULL); 23029790SLin.Ling@Sun.COM 23036423Sgw25295 /* 23046423Sgw25295 * Add this top-level vdev to the child array. 23056423Sgw25295 */ 23069790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 23079790SLin.Ling@Sun.COM &nvtop) == 0); 23089790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 23099790SLin.Ling@Sun.COM &pgid) == 0); 23109790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 23116423Sgw25295 23126423Sgw25295 /* 23136423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 23146423Sgw25295 */ 23156423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 23169790SLin.Ling@Sun.COM VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 23179790SLin.Ling@Sun.COM VDEV_TYPE_ROOT) == 0); 23186423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 23196423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 23206423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 23216423Sgw25295 &nvtop, 1) == 0); 23226423Sgw25295 23236423Sgw25295 /* 23246423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 23256423Sgw25295 * this pool's configuration (remove the old, add the new). 23266423Sgw25295 */ 23276423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 23286423Sgw25295 nvlist_free(nvroot); 23299790SLin.Ling@Sun.COM return (config); 23306423Sgw25295 } 23316423Sgw25295 23326423Sgw25295 /* 23339790SLin.Ling@Sun.COM * Walk the vdev tree and see if we can find a device with "better" 23349790SLin.Ling@Sun.COM * configuration. A configuration is "better" if the label on that 23359790SLin.Ling@Sun.COM * device has a more recent txg. 23366423Sgw25295 */ 23379790SLin.Ling@Sun.COM static void 23389790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 23397147Staylor { 23409816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 23419790SLin.Ling@Sun.COM spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 23429790SLin.Ling@Sun.COM 23439790SLin.Ling@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 23449790SLin.Ling@Sun.COM nvlist_t *label; 23459790SLin.Ling@Sun.COM uint64_t label_txg; 23469790SLin.Ling@Sun.COM 23479790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 23489790SLin.Ling@Sun.COM &label) != 0) 23499790SLin.Ling@Sun.COM return; 23509790SLin.Ling@Sun.COM 23519790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 23529790SLin.Ling@Sun.COM &label_txg) == 0); 23539790SLin.Ling@Sun.COM 23549790SLin.Ling@Sun.COM /* 23559790SLin.Ling@Sun.COM * Do we have a better boot device? 23569790SLin.Ling@Sun.COM */ 23579790SLin.Ling@Sun.COM if (label_txg > *txg) { 23589790SLin.Ling@Sun.COM *txg = label_txg; 23599790SLin.Ling@Sun.COM *avd = vd; 23607147Staylor } 23619790SLin.Ling@Sun.COM nvlist_free(label); 23627147Staylor } 23637147Staylor } 23647147Staylor 23656423Sgw25295 /* 23666423Sgw25295 * Import a root pool. 23676423Sgw25295 * 23687147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 23697147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 23707147Staylor * The GRUB "findroot" command will return the vdev we should boot. 23716423Sgw25295 * 23726423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 23736423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 23746423Sgw25295 * e.g. 23756423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 23766423Sgw25295 */ 23776423Sgw25295 int 23787147Staylor spa_import_rootpool(char *devpath, char *devid) 23796423Sgw25295 { 23809790SLin.Ling@Sun.COM spa_t *spa; 23819790SLin.Ling@Sun.COM vdev_t *rvd, *bvd, *avd = NULL; 23829790SLin.Ling@Sun.COM nvlist_t *config, *nvtop; 23839790SLin.Ling@Sun.COM uint64_t guid, txg; 23846423Sgw25295 char *pname; 23856423Sgw25295 int error; 23866423Sgw25295 23876423Sgw25295 /* 23889790SLin.Ling@Sun.COM * Read the label from the boot device and generate a configuration. 23896423Sgw25295 */ 23909790SLin.Ling@Sun.COM if ((config = spa_generate_rootconf(devpath, devid, &guid)) == NULL) { 23919790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 23929790SLin.Ling@Sun.COM devpath); 23939790SLin.Ling@Sun.COM return (EIO); 23949790SLin.Ling@Sun.COM } 23959790SLin.Ling@Sun.COM 23969790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 23979790SLin.Ling@Sun.COM &pname) == 0); 23989790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 23996423Sgw25295 24009425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 24019425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pname)) != NULL) { 24029425SEric.Schrock@Sun.COM /* 24039425SEric.Schrock@Sun.COM * Remove the existing root pool from the namespace so that we 24049425SEric.Schrock@Sun.COM * can replace it with the correct config we just read in. 24059425SEric.Schrock@Sun.COM */ 24069425SEric.Schrock@Sun.COM spa_remove(spa); 24079425SEric.Schrock@Sun.COM } 24089425SEric.Schrock@Sun.COM 24099425SEric.Schrock@Sun.COM spa = spa_add(pname, NULL); 24109425SEric.Schrock@Sun.COM spa->spa_is_root = B_TRUE; 2411*10100SLin.Ling@Sun.COM spa->spa_load_verbatim = B_TRUE; 24129790SLin.Ling@Sun.COM 24139790SLin.Ling@Sun.COM /* 24149790SLin.Ling@Sun.COM * Build up a vdev tree based on the boot device's label config. 24159790SLin.Ling@Sun.COM */ 24169790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 24179790SLin.Ling@Sun.COM &nvtop) == 0); 24189790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24199790SLin.Ling@Sun.COM error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 24209790SLin.Ling@Sun.COM VDEV_ALLOC_ROOTPOOL); 24219790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 24229790SLin.Ling@Sun.COM if (error) { 24239790SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 24249790SLin.Ling@Sun.COM nvlist_free(config); 24259790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 24269790SLin.Ling@Sun.COM pname); 24279790SLin.Ling@Sun.COM return (error); 24289790SLin.Ling@Sun.COM } 24299790SLin.Ling@Sun.COM 24309790SLin.Ling@Sun.COM /* 24319790SLin.Ling@Sun.COM * Get the boot vdev. 24329790SLin.Ling@Sun.COM */ 24339790SLin.Ling@Sun.COM if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 24349790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 24359790SLin.Ling@Sun.COM (u_longlong_t)guid); 24369790SLin.Ling@Sun.COM error = ENOENT; 24379790SLin.Ling@Sun.COM goto out; 24389790SLin.Ling@Sun.COM } 24399790SLin.Ling@Sun.COM 24409790SLin.Ling@Sun.COM /* 24419790SLin.Ling@Sun.COM * Determine if there is a better boot device. 24429790SLin.Ling@Sun.COM */ 24439790SLin.Ling@Sun.COM avd = bvd; 24449790SLin.Ling@Sun.COM spa_alt_rootvdev(rvd, &avd, &txg); 24459790SLin.Ling@Sun.COM if (avd != bvd) { 24469790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 24479790SLin.Ling@Sun.COM "try booting from '%s'", avd->vdev_path); 24489790SLin.Ling@Sun.COM error = EINVAL; 24499790SLin.Ling@Sun.COM goto out; 24509790SLin.Ling@Sun.COM } 24519790SLin.Ling@Sun.COM 24529790SLin.Ling@Sun.COM /* 24539790SLin.Ling@Sun.COM * If the boot device is part of a spare vdev then ensure that 24549790SLin.Ling@Sun.COM * we're booting off the active spare. 24559790SLin.Ling@Sun.COM */ 24569790SLin.Ling@Sun.COM if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 24579790SLin.Ling@Sun.COM !bvd->vdev_isspare) { 24589790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is currently spared. Please " 24599790SLin.Ling@Sun.COM "try booting from '%s'", 24609790SLin.Ling@Sun.COM bvd->vdev_parent->vdev_child[1]->vdev_path); 24619790SLin.Ling@Sun.COM error = EINVAL; 24629790SLin.Ling@Sun.COM goto out; 24639790SLin.Ling@Sun.COM } 24649790SLin.Ling@Sun.COM 24659790SLin.Ling@Sun.COM VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 24669790SLin.Ling@Sun.COM error = 0; 24679946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 24689790SLin.Ling@Sun.COM out: 24699790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24709790SLin.Ling@Sun.COM vdev_free(rvd); 24719790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 24729425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 24736423Sgw25295 24749790SLin.Ling@Sun.COM nvlist_free(config); 24756423Sgw25295 return (error); 24766423Sgw25295 } 24779790SLin.Ling@Sun.COM 24786423Sgw25295 #endif 24796423Sgw25295 24806423Sgw25295 /* 24819425SEric.Schrock@Sun.COM * Take a pool and insert it into the namespace as if it had been loaded at 24829425SEric.Schrock@Sun.COM * boot. 24839425SEric.Schrock@Sun.COM */ 24849425SEric.Schrock@Sun.COM int 24859425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props) 24869425SEric.Schrock@Sun.COM { 24879425SEric.Schrock@Sun.COM spa_t *spa; 24889425SEric.Schrock@Sun.COM char *altroot = NULL; 24899425SEric.Schrock@Sun.COM 24909425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 24919425SEric.Schrock@Sun.COM if (spa_lookup(pool) != NULL) { 24929425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 24939425SEric.Schrock@Sun.COM return (EEXIST); 24949425SEric.Schrock@Sun.COM } 24959425SEric.Schrock@Sun.COM 24969425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 24979425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 24989425SEric.Schrock@Sun.COM spa = spa_add(pool, altroot); 24999425SEric.Schrock@Sun.COM 2500*10100SLin.Ling@Sun.COM spa->spa_load_verbatim = B_TRUE; 250110000SVictor.Latushkin@Sun.COM 25029425SEric.Schrock@Sun.COM VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 25039425SEric.Schrock@Sun.COM 25049425SEric.Schrock@Sun.COM if (props != NULL) 25059425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 25069425SEric.Schrock@Sun.COM 25079425SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 25089425SEric.Schrock@Sun.COM 25099425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 25109946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 25119425SEric.Schrock@Sun.COM 25129425SEric.Schrock@Sun.COM return (0); 25139425SEric.Schrock@Sun.COM } 25149425SEric.Schrock@Sun.COM 25159425SEric.Schrock@Sun.COM /* 25166423Sgw25295 * Import a non-root pool into the system. 25176423Sgw25295 */ 25186423Sgw25295 int 25196423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 25206423Sgw25295 { 25219425SEric.Schrock@Sun.COM spa_t *spa; 25229425SEric.Schrock@Sun.COM char *altroot = NULL; 25239425SEric.Schrock@Sun.COM int error; 25249425SEric.Schrock@Sun.COM nvlist_t *nvroot; 25259425SEric.Schrock@Sun.COM nvlist_t **spares, **l2cache; 25269425SEric.Schrock@Sun.COM uint_t nspares, nl2cache; 25279425SEric.Schrock@Sun.COM 25289425SEric.Schrock@Sun.COM /* 25299425SEric.Schrock@Sun.COM * If a pool with this name exists, return failure. 25309425SEric.Schrock@Sun.COM */ 25319425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 25329425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pool)) != NULL) { 25339425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 25349425SEric.Schrock@Sun.COM return (EEXIST); 25359425SEric.Schrock@Sun.COM } 25369425SEric.Schrock@Sun.COM 25379425SEric.Schrock@Sun.COM /* 25389425SEric.Schrock@Sun.COM * Create and initialize the spa structure. 25399425SEric.Schrock@Sun.COM */ 25409425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 25419425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 25429425SEric.Schrock@Sun.COM spa = spa_add(pool, altroot); 25439425SEric.Schrock@Sun.COM spa_activate(spa, spa_mode_global); 25449425SEric.Schrock@Sun.COM 25459425SEric.Schrock@Sun.COM /* 25469630SJeff.Bonwick@Sun.COM * Don't start async tasks until we know everything is healthy. 25479630SJeff.Bonwick@Sun.COM */ 25489630SJeff.Bonwick@Sun.COM spa_async_suspend(spa); 25499630SJeff.Bonwick@Sun.COM 25509630SJeff.Bonwick@Sun.COM /* 25519425SEric.Schrock@Sun.COM * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 25529425SEric.Schrock@Sun.COM * because the user-supplied config is actually the one to trust when 25539425SEric.Schrock@Sun.COM * doing an import. 25549425SEric.Schrock@Sun.COM */ 25559425SEric.Schrock@Sun.COM error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 25569425SEric.Schrock@Sun.COM 25579425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 25589425SEric.Schrock@Sun.COM /* 25599425SEric.Schrock@Sun.COM * Toss any existing sparelist, as it doesn't have any validity 25609425SEric.Schrock@Sun.COM * anymore, and conflicts with spa_has_spare(). 25619425SEric.Schrock@Sun.COM */ 25629425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) { 25639425SEric.Schrock@Sun.COM nvlist_free(spa->spa_spares.sav_config); 25649425SEric.Schrock@Sun.COM spa->spa_spares.sav_config = NULL; 25659425SEric.Schrock@Sun.COM spa_load_spares(spa); 25669425SEric.Schrock@Sun.COM } 25679425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) { 25689425SEric.Schrock@Sun.COM nvlist_free(spa->spa_l2cache.sav_config); 25699425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_config = NULL; 25709425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 25719425SEric.Schrock@Sun.COM } 25729425SEric.Schrock@Sun.COM 25739425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 25749425SEric.Schrock@Sun.COM &nvroot) == 0); 25759425SEric.Schrock@Sun.COM if (error == 0) 25769425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 25779425SEric.Schrock@Sun.COM VDEV_ALLOC_SPARE); 25789425SEric.Schrock@Sun.COM if (error == 0) 25799425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 25809425SEric.Schrock@Sun.COM VDEV_ALLOC_L2CACHE); 25819425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 25829425SEric.Schrock@Sun.COM 25839425SEric.Schrock@Sun.COM if (props != NULL) 25849425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 25859425SEric.Schrock@Sun.COM 25869425SEric.Schrock@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 25879425SEric.Schrock@Sun.COM (error = spa_prop_set(spa, props)))) { 25889425SEric.Schrock@Sun.COM spa_unload(spa); 25899425SEric.Schrock@Sun.COM spa_deactivate(spa); 25909425SEric.Schrock@Sun.COM spa_remove(spa); 25919425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 25929425SEric.Schrock@Sun.COM return (error); 25939425SEric.Schrock@Sun.COM } 25949425SEric.Schrock@Sun.COM 25959630SJeff.Bonwick@Sun.COM spa_async_resume(spa); 25969630SJeff.Bonwick@Sun.COM 25979425SEric.Schrock@Sun.COM /* 25989425SEric.Schrock@Sun.COM * Override any spares and level 2 cache devices as specified by 25999425SEric.Schrock@Sun.COM * the user, as these may have correct device names/devids, etc. 26009425SEric.Schrock@Sun.COM */ 26019425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 26029425SEric.Schrock@Sun.COM &spares, &nspares) == 0) { 26039425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) 26049425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_spares.sav_config, 26059425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 26069425SEric.Schrock@Sun.COM else 26079425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 26089425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 26099425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 26109425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 26119425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26129425SEric.Schrock@Sun.COM spa_load_spares(spa); 26139425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 26149425SEric.Schrock@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 26159425SEric.Schrock@Sun.COM } 26169425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 26179425SEric.Schrock@Sun.COM &l2cache, &nl2cache) == 0) { 26189425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) 26199425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 26209425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 26219425SEric.Schrock@Sun.COM else 26229425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 26239425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 26249425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 26259425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 26269425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26279425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 26289425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 26299425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_sync = B_TRUE; 26309425SEric.Schrock@Sun.COM } 26319425SEric.Schrock@Sun.COM 26329425SEric.Schrock@Sun.COM if (spa_writeable(spa)) { 26339425SEric.Schrock@Sun.COM /* 26349425SEric.Schrock@Sun.COM * Update the config cache to include the newly-imported pool. 26359425SEric.Schrock@Sun.COM */ 2636*10100SLin.Ling@Sun.COM spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 26379425SEric.Schrock@Sun.COM } 26389425SEric.Schrock@Sun.COM 26399816SGeorge.Wilson@Sun.COM /* 26409816SGeorge.Wilson@Sun.COM * It's possible that the pool was expanded while it was exported. 26419816SGeorge.Wilson@Sun.COM * We kick off an async task to handle this for us. 26429816SGeorge.Wilson@Sun.COM */ 26439816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 26449816SGeorge.Wilson@Sun.COM 26459425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 26469946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 26479425SEric.Schrock@Sun.COM 26489425SEric.Schrock@Sun.COM return (0); 26496643Seschrock } 26506643Seschrock 26516643Seschrock 2652789Sahrens /* 2653789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2654789Sahrens * to get the vdev stats associated with the imported devices. 2655789Sahrens */ 2656789Sahrens #define TRYIMPORT_NAME "$import" 2657789Sahrens 2658789Sahrens nvlist_t * 2659789Sahrens spa_tryimport(nvlist_t *tryconfig) 2660789Sahrens { 2661789Sahrens nvlist_t *config = NULL; 2662789Sahrens char *poolname; 2663789Sahrens spa_t *spa; 2664789Sahrens uint64_t state; 26658680SLin.Ling@Sun.COM int error; 2666789Sahrens 2667789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2668789Sahrens return (NULL); 2669789Sahrens 2670789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2671789Sahrens return (NULL); 2672789Sahrens 26731635Sbonwick /* 26741635Sbonwick * Create and initialize the spa structure. 26751635Sbonwick */ 2676789Sahrens mutex_enter(&spa_namespace_lock); 26771635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 26788241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 2679789Sahrens 2680789Sahrens /* 26811635Sbonwick * Pass off the heavy lifting to spa_load(). 26821732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 26831732Sbonwick * is actually the one to trust when doing an import. 2684789Sahrens */ 26858680SLin.Ling@Sun.COM error = spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2686789Sahrens 2687789Sahrens /* 2688789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2689789Sahrens */ 2690789Sahrens if (spa->spa_root_vdev != NULL) { 2691789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2692789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2693789Sahrens poolname) == 0); 2694789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2695789Sahrens state) == 0); 26963975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 26973975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 26982082Seschrock 26992082Seschrock /* 27006423Sgw25295 * If the bootfs property exists on this pool then we 27016423Sgw25295 * copy it out so that external consumers can tell which 27026423Sgw25295 * pools are bootable. 27036423Sgw25295 */ 27048680SLin.Ling@Sun.COM if ((!error || error == EEXIST) && spa->spa_bootfs) { 27056423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 27066423Sgw25295 27076423Sgw25295 /* 27086423Sgw25295 * We have to play games with the name since the 27096423Sgw25295 * pool was opened as TRYIMPORT_NAME. 27106423Sgw25295 */ 27117754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 27126423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 27136423Sgw25295 char *cp; 27146423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 27156423Sgw25295 27166423Sgw25295 cp = strchr(tmpname, '/'); 27176423Sgw25295 if (cp == NULL) { 27186423Sgw25295 (void) strlcpy(dsname, tmpname, 27196423Sgw25295 MAXPATHLEN); 27206423Sgw25295 } else { 27216423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 27226423Sgw25295 "%s/%s", poolname, ++cp); 27236423Sgw25295 } 27246423Sgw25295 VERIFY(nvlist_add_string(config, 27256423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 27266423Sgw25295 kmem_free(dsname, MAXPATHLEN); 27276423Sgw25295 } 27286423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 27296423Sgw25295 } 27306423Sgw25295 27316423Sgw25295 /* 27325450Sbrendan * Add the list of hot spares and level 2 cache devices. 27332082Seschrock */ 27349425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 27352082Seschrock spa_add_spares(spa, config); 27365450Sbrendan spa_add_l2cache(spa, config); 27379425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 2738789Sahrens } 2739789Sahrens 2740789Sahrens spa_unload(spa); 2741789Sahrens spa_deactivate(spa); 2742789Sahrens spa_remove(spa); 2743789Sahrens mutex_exit(&spa_namespace_lock); 2744789Sahrens 2745789Sahrens return (config); 2746789Sahrens } 2747789Sahrens 2748789Sahrens /* 2749789Sahrens * Pool export/destroy 2750789Sahrens * 2751789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2752789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2753789Sahrens * update the pool state and sync all the labels to disk, removing the 27548211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 27558211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 2756789Sahrens */ 2757789Sahrens static int 27587214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 27598211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 2760789Sahrens { 2761789Sahrens spa_t *spa; 2762789Sahrens 27631775Sbillm if (oldconfig) 27641775Sbillm *oldconfig = NULL; 27651775Sbillm 27668241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 2767789Sahrens return (EROFS); 2768789Sahrens 2769789Sahrens mutex_enter(&spa_namespace_lock); 2770789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2771789Sahrens mutex_exit(&spa_namespace_lock); 2772789Sahrens return (ENOENT); 2773789Sahrens } 2774789Sahrens 2775789Sahrens /* 27761544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 27771544Seschrock * reacquire the namespace lock, and see if we can export. 27781544Seschrock */ 27791544Seschrock spa_open_ref(spa, FTAG); 27801544Seschrock mutex_exit(&spa_namespace_lock); 27811544Seschrock spa_async_suspend(spa); 27821544Seschrock mutex_enter(&spa_namespace_lock); 27831544Seschrock spa_close(spa, FTAG); 27841544Seschrock 27851544Seschrock /* 2786789Sahrens * The pool will be in core if it's openable, 2787789Sahrens * in which case we can modify its state. 2788789Sahrens */ 2789789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2790789Sahrens /* 2791789Sahrens * Objsets may be open only because they're dirty, so we 2792789Sahrens * have to force it to sync before checking spa_refcnt. 2793789Sahrens */ 2794789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2795789Sahrens 27961544Seschrock /* 27971544Seschrock * A pool cannot be exported or destroyed if there are active 27981544Seschrock * references. If we are resetting a pool, allow references by 27991544Seschrock * fault injection handlers. 28001544Seschrock */ 28011544Seschrock if (!spa_refcount_zero(spa) || 28021544Seschrock (spa->spa_inject_ref != 0 && 28031544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 28041544Seschrock spa_async_resume(spa); 2805789Sahrens mutex_exit(&spa_namespace_lock); 2806789Sahrens return (EBUSY); 2807789Sahrens } 2808789Sahrens 2809789Sahrens /* 28107214Slling * A pool cannot be exported if it has an active shared spare. 28117214Slling * This is to prevent other pools stealing the active spare 28127214Slling * from an exported pool. At user's own will, such pool can 28137214Slling * be forcedly exported. 28147214Slling */ 28157214Slling if (!force && new_state == POOL_STATE_EXPORTED && 28167214Slling spa_has_active_shared_spare(spa)) { 28177214Slling spa_async_resume(spa); 28187214Slling mutex_exit(&spa_namespace_lock); 28197214Slling return (EXDEV); 28207214Slling } 28217214Slling 28227214Slling /* 2823789Sahrens * We want this to be reflected on every label, 2824789Sahrens * so mark them all dirty. spa_unload() will do the 2825789Sahrens * final sync that pushes these changes out. 2826789Sahrens */ 28278211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 28287754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 28291544Seschrock spa->spa_state = new_state; 28301635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 28311544Seschrock vdev_config_dirty(spa->spa_root_vdev); 28327754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 28331544Seschrock } 2834789Sahrens } 2835789Sahrens 28364451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 28374451Seschrock 2838789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2839789Sahrens spa_unload(spa); 2840789Sahrens spa_deactivate(spa); 2841789Sahrens } 2842789Sahrens 28431775Sbillm if (oldconfig && spa->spa_config) 28441775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 28451775Sbillm 28461544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 28478211SGeorge.Wilson@Sun.COM if (!hardforce) 28488211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 28491544Seschrock spa_remove(spa); 28501544Seschrock } 2851789Sahrens mutex_exit(&spa_namespace_lock); 2852789Sahrens 2853789Sahrens return (0); 2854789Sahrens } 2855789Sahrens 2856789Sahrens /* 2857789Sahrens * Destroy a storage pool. 2858789Sahrens */ 2859789Sahrens int 2860789Sahrens spa_destroy(char *pool) 2861789Sahrens { 28628211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 28638211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 2864789Sahrens } 2865789Sahrens 2866789Sahrens /* 2867789Sahrens * Export a storage pool. 2868789Sahrens */ 2869789Sahrens int 28708211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 28718211SGeorge.Wilson@Sun.COM boolean_t hardforce) 2872789Sahrens { 28738211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 28748211SGeorge.Wilson@Sun.COM force, hardforce)); 2875789Sahrens } 2876789Sahrens 2877789Sahrens /* 28781544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 28791544Seschrock * from the namespace in any way. 28801544Seschrock */ 28811544Seschrock int 28821544Seschrock spa_reset(char *pool) 28831544Seschrock { 28847214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 28858211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 28861544Seschrock } 28871544Seschrock 28881544Seschrock /* 2889789Sahrens * ========================================================================== 2890789Sahrens * Device manipulation 2891789Sahrens * ========================================================================== 2892789Sahrens */ 2893789Sahrens 2894789Sahrens /* 28954527Sperrin * Add a device to a storage pool. 2896789Sahrens */ 2897789Sahrens int 2898789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2899789Sahrens { 2900789Sahrens uint64_t txg; 29018241SJeff.Bonwick@Sun.COM int error; 2902789Sahrens vdev_t *rvd = spa->spa_root_vdev; 29031585Sbonwick vdev_t *vd, *tvd; 29045450Sbrendan nvlist_t **spares, **l2cache; 29055450Sbrendan uint_t nspares, nl2cache; 2906789Sahrens 2907789Sahrens txg = spa_vdev_enter(spa); 2908789Sahrens 29092082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 29102082Seschrock VDEV_ALLOC_ADD)) != 0) 29112082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 29122082Seschrock 29137754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 2914789Sahrens 29155450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 29165450Sbrendan &nspares) != 0) 29172082Seschrock nspares = 0; 29182082Seschrock 29195450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 29205450Sbrendan &nl2cache) != 0) 29215450Sbrendan nl2cache = 0; 29225450Sbrendan 29237754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 29242082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 29257754SJeff.Bonwick@Sun.COM 29267754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 29277754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 29287754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 29292082Seschrock 29303377Seschrock /* 29315450Sbrendan * We must validate the spares and l2cache devices after checking the 29325450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 29333377Seschrock */ 29347754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 29353377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 29363377Seschrock 29373377Seschrock /* 29383377Seschrock * Transfer each new top-level vdev from vd to rvd. 29393377Seschrock */ 29408241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 29413377Seschrock tvd = vd->vdev_child[c]; 29423377Seschrock vdev_remove_child(vd, tvd); 29433377Seschrock tvd->vdev_id = rvd->vdev_children; 29443377Seschrock vdev_add_child(rvd, tvd); 29453377Seschrock vdev_config_dirty(tvd); 29463377Seschrock } 29473377Seschrock 29482082Seschrock if (nspares != 0) { 29495450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 29505450Sbrendan ZPOOL_CONFIG_SPARES); 29512082Seschrock spa_load_spares(spa); 29525450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 29535450Sbrendan } 29545450Sbrendan 29555450Sbrendan if (nl2cache != 0) { 29565450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 29575450Sbrendan ZPOOL_CONFIG_L2CACHE); 29585450Sbrendan spa_load_l2cache(spa); 29595450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2960789Sahrens } 2961789Sahrens 2962789Sahrens /* 29631585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 29641585Sbonwick * If other threads start allocating from these vdevs before we 29651585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 29661585Sbonwick * fail to open the pool because there are DVAs that the config cache 29671585Sbonwick * can't translate. Therefore, we first add the vdevs without 29681585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 29691635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 29701585Sbonwick * 29711585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 29721585Sbonwick * if we lose power at any point in this sequence, the remaining 29731585Sbonwick * steps will be completed the next time we load the pool. 2974789Sahrens */ 29751635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 29761585Sbonwick 29771635Sbonwick mutex_enter(&spa_namespace_lock); 29781635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 29791635Sbonwick mutex_exit(&spa_namespace_lock); 2980789Sahrens 29811635Sbonwick return (0); 2982789Sahrens } 2983789Sahrens 2984789Sahrens /* 2985789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2986789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2987789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2988789Sahrens * 2989789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2990789Sahrens * existing device; in this case the two devices are made into their own 29914451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2992789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2993789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2994789Sahrens * completion of resilvering, the first disk (the one being replaced) 2995789Sahrens * is automatically detached. 2996789Sahrens */ 2997789Sahrens int 29981544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2999789Sahrens { 3000789Sahrens uint64_t txg, open_txg; 3001789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3002789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 30032082Seschrock vdev_ops_t *pvops; 30047313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 30057313SEric.Kustarz@Sun.COM int newvd_isspare; 30067313SEric.Kustarz@Sun.COM int error; 3007789Sahrens 3008789Sahrens txg = spa_vdev_enter(spa); 3009789Sahrens 30106643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3011789Sahrens 3012789Sahrens if (oldvd == NULL) 3013789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3014789Sahrens 30151585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 30161585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 30171585Sbonwick 3018789Sahrens pvd = oldvd->vdev_parent; 3019789Sahrens 30202082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 30214451Seschrock VDEV_ALLOC_ADD)) != 0) 30224451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 30234451Seschrock 30244451Seschrock if (newrootvd->vdev_children != 1) 3025789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3026789Sahrens 3027789Sahrens newvd = newrootvd->vdev_child[0]; 3028789Sahrens 3029789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3030789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3031789Sahrens 30322082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3033789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3034789Sahrens 30354527Sperrin /* 30364527Sperrin * Spares can't replace logs 30374527Sperrin */ 30387326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 30394527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 30404527Sperrin 30412082Seschrock if (!replacing) { 30422082Seschrock /* 30432082Seschrock * For attach, the only allowable parent is a mirror or the root 30442082Seschrock * vdev. 30452082Seschrock */ 30462082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 30472082Seschrock pvd->vdev_ops != &vdev_root_ops) 30482082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 30492082Seschrock 30502082Seschrock pvops = &vdev_mirror_ops; 30512082Seschrock } else { 30522082Seschrock /* 30532082Seschrock * Active hot spares can only be replaced by inactive hot 30542082Seschrock * spares. 30552082Seschrock */ 30562082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 30572082Seschrock pvd->vdev_child[1] == oldvd && 30582082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 30592082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 30602082Seschrock 30612082Seschrock /* 30622082Seschrock * If the source is a hot spare, and the parent isn't already a 30632082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 30643377Seschrock * want to create a replacing vdev. The user is not allowed to 30653377Seschrock * attach to a spared vdev child unless the 'isspare' state is 30663377Seschrock * the same (spare replaces spare, non-spare replaces 30673377Seschrock * non-spare). 30682082Seschrock */ 30692082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 30702082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 30713377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 30723377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 30733377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 30742082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 30752082Seschrock newvd->vdev_isspare) 30762082Seschrock pvops = &vdev_spare_ops; 30772082Seschrock else 30782082Seschrock pvops = &vdev_replacing_ops; 30792082Seschrock } 30802082Seschrock 30811175Slling /* 30829816SGeorge.Wilson@Sun.COM * Make sure the new device is big enough. 30831175Slling */ 30849816SGeorge.Wilson@Sun.COM if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3085789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3086789Sahrens 30871732Sbonwick /* 30881732Sbonwick * The new device cannot have a higher alignment requirement 30891732Sbonwick * than the top-level vdev. 30901732Sbonwick */ 30911732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3092789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3093789Sahrens 3094789Sahrens /* 3095789Sahrens * If this is an in-place replacement, update oldvd's path and devid 3096789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3097789Sahrens */ 3098789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3099789Sahrens spa_strfree(oldvd->vdev_path); 3100789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3101789Sahrens KM_SLEEP); 3102789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3103789Sahrens newvd->vdev_path, "old"); 3104789Sahrens if (oldvd->vdev_devid != NULL) { 3105789Sahrens spa_strfree(oldvd->vdev_devid); 3106789Sahrens oldvd->vdev_devid = NULL; 3107789Sahrens } 3108789Sahrens } 3109789Sahrens 3110789Sahrens /* 31112082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 31122082Seschrock * mirror/replacing/spare vdev above oldvd. 3113789Sahrens */ 3114789Sahrens if (pvd->vdev_ops != pvops) 3115789Sahrens pvd = vdev_add_parent(oldvd, pvops); 3116789Sahrens 3117789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3118789Sahrens ASSERT(pvd->vdev_ops == pvops); 3119789Sahrens ASSERT(oldvd->vdev_parent == pvd); 3120789Sahrens 3121789Sahrens /* 3122789Sahrens * Extract the new device from its root and add it to pvd. 3123789Sahrens */ 3124789Sahrens vdev_remove_child(newrootvd, newvd); 3125789Sahrens newvd->vdev_id = pvd->vdev_children; 3126789Sahrens vdev_add_child(pvd, newvd); 3127789Sahrens 3128789Sahrens tvd = newvd->vdev_top; 3129789Sahrens ASSERT(pvd->vdev_top == tvd); 3130789Sahrens ASSERT(tvd->vdev_parent == rvd); 3131789Sahrens 3132789Sahrens vdev_config_dirty(tvd); 3133789Sahrens 3134789Sahrens /* 3135789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3136789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3137789Sahrens */ 3138789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 3139789Sahrens 31408241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, 31418241SJeff.Bonwick@Sun.COM TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3142789Sahrens 31439425SEric.Schrock@Sun.COM if (newvd->vdev_isspare) { 31443377Seschrock spa_spare_activate(newvd); 31459425SEric.Schrock@Sun.COM spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 31469425SEric.Schrock@Sun.COM } 31479425SEric.Schrock@Sun.COM 31487754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 31497754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 31507313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 31511544Seschrock 3152789Sahrens /* 3153789Sahrens * Mark newvd's DTL dirty in this txg. 3154789Sahrens */ 31551732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3156789Sahrens 3157789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3158789Sahrens 31599946SMark.Musante@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL, 31609946SMark.Musante@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 31619946SMark.Musante@Sun.COM replacing && newvd_isspare ? "spare in" : 31629946SMark.Musante@Sun.COM replacing ? "replace" : "attach", newvdpath, 31639946SMark.Musante@Sun.COM replacing ? "for" : "to", oldvdpath); 31647313SEric.Kustarz@Sun.COM 31657313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 31667313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 31677313SEric.Kustarz@Sun.COM 3168789Sahrens /* 31697046Sahrens * Kick off a resilver to update newvd. 3170789Sahrens */ 31717046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3172789Sahrens 3173789Sahrens return (0); 3174789Sahrens } 3175789Sahrens 3176789Sahrens /* 3177789Sahrens * Detach a device from a mirror or replacing vdev. 3178789Sahrens * If 'replace_done' is specified, only detach if the parent 3179789Sahrens * is a replacing vdev. 3180789Sahrens */ 3181789Sahrens int 31828241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3183789Sahrens { 3184789Sahrens uint64_t txg; 31858241SJeff.Bonwick@Sun.COM int error; 3186789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3187789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 31882082Seschrock boolean_t unspare = B_FALSE; 31892082Seschrock uint64_t unspare_guid; 31906673Seschrock size_t len; 3191789Sahrens 3192789Sahrens txg = spa_vdev_enter(spa); 3193789Sahrens 31946643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3195789Sahrens 3196789Sahrens if (vd == NULL) 3197789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3198789Sahrens 31991585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 32001585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 32011585Sbonwick 3202789Sahrens pvd = vd->vdev_parent; 3203789Sahrens 3204789Sahrens /* 32058241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 32068241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 32078241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 32088241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 32098241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 32108241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 32118241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 32128241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 32138241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 32148241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 32158241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 32168241SJeff.Bonwick@Sun.COM */ 32178241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 32188241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 32198241SJeff.Bonwick@Sun.COM 32208241SJeff.Bonwick@Sun.COM /* 3221789Sahrens * If replace_done is specified, only remove this device if it's 32222082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 32232082Seschrock * disk can be removed. 3224789Sahrens */ 32252082Seschrock if (replace_done) { 32262082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 32272082Seschrock if (vd->vdev_id != 0) 32282082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 32292082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 32302082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 32312082Seschrock } 32322082Seschrock } 32332082Seschrock 32342082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 32354577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3236789Sahrens 3237789Sahrens /* 32382082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3239789Sahrens */ 3240789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 32412082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 32422082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3243789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3244789Sahrens 3245789Sahrens /* 32468241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 32478241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 3248789Sahrens */ 32498241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 3250789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3251789Sahrens 32528241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 32538241SJeff.Bonwick@Sun.COM 3254789Sahrens /* 32556673Seschrock * If we are detaching the second disk from a replacing vdev, then 32566673Seschrock * check to see if we changed the original vdev's path to have "/old" 32576673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 32586673Seschrock */ 32596673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 32606673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 32616673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 32626673Seschrock ASSERT(pvd->vdev_child[1] == vd); 32636673Seschrock cvd = pvd->vdev_child[0]; 32646673Seschrock len = strlen(vd->vdev_path); 32656673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 32666673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 32676673Seschrock spa_strfree(cvd->vdev_path); 32686673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 32696673Seschrock } 32706673Seschrock } 32716673Seschrock 32726673Seschrock /* 32732082Seschrock * If we are detaching the original disk from a spare, then it implies 32742082Seschrock * that the spare should become a real disk, and be removed from the 32752082Seschrock * active spare list for the pool. 32762082Seschrock */ 32772082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 32788241SJeff.Bonwick@Sun.COM vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 32792082Seschrock unspare = B_TRUE; 32802082Seschrock 32812082Seschrock /* 3282789Sahrens * Erase the disk labels so the disk can be used for other things. 3283789Sahrens * This must be done after all other error cases are handled, 3284789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3285789Sahrens * But if we can't do it, don't treat the error as fatal -- 3286789Sahrens * it may be that the unwritability of the disk is the reason 3287789Sahrens * it's being detached! 3288789Sahrens */ 32893377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3290789Sahrens 3291789Sahrens /* 3292789Sahrens * Remove vd from its parent and compact the parent's children. 3293789Sahrens */ 3294789Sahrens vdev_remove_child(pvd, vd); 3295789Sahrens vdev_compact_children(pvd); 3296789Sahrens 3297789Sahrens /* 3298789Sahrens * Remember one of the remaining children so we can get tvd below. 3299789Sahrens */ 3300789Sahrens cvd = pvd->vdev_child[0]; 3301789Sahrens 3302789Sahrens /* 33032082Seschrock * If we need to remove the remaining child from the list of hot spares, 33048241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 33058241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 33068241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 33078241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 33088241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 33098241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 33102082Seschrock */ 33112082Seschrock if (unspare) { 33122082Seschrock ASSERT(cvd->vdev_isspare); 33133377Seschrock spa_spare_remove(cvd); 33142082Seschrock unspare_guid = cvd->vdev_guid; 33158241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 33162082Seschrock } 33172082Seschrock 33182082Seschrock /* 3319789Sahrens * If the parent mirror/replacing vdev only has one child, 3320789Sahrens * the parent is no longer needed. Remove it from the tree. 3321789Sahrens */ 3322789Sahrens if (pvd->vdev_children == 1) 3323789Sahrens vdev_remove_parent(cvd); 3324789Sahrens 3325789Sahrens /* 3326789Sahrens * We don't set tvd until now because the parent we just removed 3327789Sahrens * may have been the previous top-level vdev. 3328789Sahrens */ 3329789Sahrens tvd = cvd->vdev_top; 3330789Sahrens ASSERT(tvd->vdev_parent == rvd); 3331789Sahrens 3332789Sahrens /* 33333377Seschrock * Reevaluate the parent vdev state. 3334789Sahrens */ 33354451Seschrock vdev_propagate_state(cvd); 3336789Sahrens 3337789Sahrens /* 33389816SGeorge.Wilson@Sun.COM * If the 'autoexpand' property is set on the pool then automatically 33399816SGeorge.Wilson@Sun.COM * try to expand the size of the pool. For example if the device we 33409816SGeorge.Wilson@Sun.COM * just detached was smaller than the others, it may be possible to 33419816SGeorge.Wilson@Sun.COM * add metaslabs (i.e. grow the pool). We need to reopen the vdev 33429816SGeorge.Wilson@Sun.COM * first so that we can obtain the updated sizes of the leaf vdevs. 3343789Sahrens */ 33449816SGeorge.Wilson@Sun.COM if (spa->spa_autoexpand) { 33459816SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 33469816SGeorge.Wilson@Sun.COM vdev_expand(tvd, txg); 33479816SGeorge.Wilson@Sun.COM } 3348789Sahrens 3349789Sahrens vdev_config_dirty(tvd); 3350789Sahrens 3351789Sahrens /* 33523377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 33533377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 33543377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 33553377Seschrock * prevent vd from being accessed after it's freed. 3356789Sahrens */ 33578241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 3358789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 33591732Sbonwick vd->vdev_detached = B_TRUE; 33601732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3361789Sahrens 33624451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 33634451Seschrock 33642082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 33652082Seschrock 33662082Seschrock /* 33673377Seschrock * If this was the removal of the original device in a hot spare vdev, 33683377Seschrock * then we want to go through and remove the device from the hot spare 33693377Seschrock * list of every other pool. 33702082Seschrock */ 33712082Seschrock if (unspare) { 33728241SJeff.Bonwick@Sun.COM spa_t *myspa = spa; 33732082Seschrock spa = NULL; 33742082Seschrock mutex_enter(&spa_namespace_lock); 33752082Seschrock while ((spa = spa_next(spa)) != NULL) { 33762082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 33772082Seschrock continue; 33788241SJeff.Bonwick@Sun.COM if (spa == myspa) 33798241SJeff.Bonwick@Sun.COM continue; 33807793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 33817793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 33822082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 33837793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 33847793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 33852082Seschrock } 33862082Seschrock mutex_exit(&spa_namespace_lock); 33872082Seschrock } 33882082Seschrock 33892082Seschrock return (error); 33902082Seschrock } 33912082Seschrock 33927754SJeff.Bonwick@Sun.COM static nvlist_t * 33937754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 33942082Seschrock { 33957754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 33967754SJeff.Bonwick@Sun.COM uint64_t guid; 33977754SJeff.Bonwick@Sun.COM 33987754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 33997754SJeff.Bonwick@Sun.COM &guid) == 0); 34007754SJeff.Bonwick@Sun.COM 34017754SJeff.Bonwick@Sun.COM if (guid == target_guid) 34027754SJeff.Bonwick@Sun.COM return (nvpp[i]); 34032082Seschrock } 34042082Seschrock 34057754SJeff.Bonwick@Sun.COM return (NULL); 34065450Sbrendan } 34075450Sbrendan 34087754SJeff.Bonwick@Sun.COM static void 34097754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 34107754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 34115450Sbrendan { 34127754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 34137754SJeff.Bonwick@Sun.COM 34147754SJeff.Bonwick@Sun.COM if (count > 1) 34157754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 34167754SJeff.Bonwick@Sun.COM 34177754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 34187754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 34197754SJeff.Bonwick@Sun.COM continue; 34207754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 34215450Sbrendan } 34225450Sbrendan 34237754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 34247754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 34257754SJeff.Bonwick@Sun.COM 34267754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 34277754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 34287754SJeff.Bonwick@Sun.COM 34297754SJeff.Bonwick@Sun.COM if (count > 1) 34307754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 34315450Sbrendan } 34325450Sbrendan 34335450Sbrendan /* 34345450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 34355450Sbrendan * spares and level 2 ARC devices. 34365450Sbrendan */ 34375450Sbrendan int 34385450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 34395450Sbrendan { 34405450Sbrendan vdev_t *vd; 34417754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 34425450Sbrendan uint_t nspares, nl2cache; 34438241SJeff.Bonwick@Sun.COM uint64_t txg = 0; 34445450Sbrendan int error = 0; 34458241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 34468241SJeff.Bonwick@Sun.COM 34478241SJeff.Bonwick@Sun.COM if (!locked) 34488241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 34495450Sbrendan 34506643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 34515450Sbrendan 34525450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 34535450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 34547754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 34557754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 34567754SJeff.Bonwick@Sun.COM /* 34577754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 34587754SJeff.Bonwick@Sun.COM * in this pool. 34597754SJeff.Bonwick@Sun.COM */ 34607754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 34617754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 34627754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 34637754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 34647754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 34657754SJeff.Bonwick@Sun.COM } else { 34667754SJeff.Bonwick@Sun.COM error = EBUSY; 34677754SJeff.Bonwick@Sun.COM } 34687754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 34695450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 34707754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 34717754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 34727754SJeff.Bonwick@Sun.COM /* 34737754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 34747754SJeff.Bonwick@Sun.COM */ 34757754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 34767754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 34775450Sbrendan spa_load_l2cache(spa); 34785450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 34797754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 34807754SJeff.Bonwick@Sun.COM /* 34817754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 34827754SJeff.Bonwick@Sun.COM */ 34837754SJeff.Bonwick@Sun.COM error = ENOTSUP; 34847754SJeff.Bonwick@Sun.COM } else { 34857754SJeff.Bonwick@Sun.COM /* 34867754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 34877754SJeff.Bonwick@Sun.COM */ 34887754SJeff.Bonwick@Sun.COM error = ENOENT; 34895450Sbrendan } 34902082Seschrock 34918241SJeff.Bonwick@Sun.COM if (!locked) 34928241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 34938241SJeff.Bonwick@Sun.COM 34948241SJeff.Bonwick@Sun.COM return (error); 3495789Sahrens } 3496789Sahrens 3497789Sahrens /* 34984451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 34994451Seschrock * current spared, so we can detach it. 3500789Sahrens */ 35011544Seschrock static vdev_t * 35024451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3503789Sahrens { 35041544Seschrock vdev_t *newvd, *oldvd; 35059816SGeorge.Wilson@Sun.COM 35069816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 35074451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 35081544Seschrock if (oldvd != NULL) 35091544Seschrock return (oldvd); 35101544Seschrock } 3511789Sahrens 35124451Seschrock /* 35134451Seschrock * Check for a completed replacement. 35144451Seschrock */ 3515789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 35161544Seschrock oldvd = vd->vdev_child[0]; 35171544Seschrock newvd = vd->vdev_child[1]; 3518789Sahrens 35198241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 35208241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 35211544Seschrock return (oldvd); 35221544Seschrock } 3523789Sahrens 35244451Seschrock /* 35254451Seschrock * Check for a completed resilver with the 'unspare' flag set. 35264451Seschrock */ 35274451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 35284451Seschrock newvd = vd->vdev_child[0]; 35294451Seschrock oldvd = vd->vdev_child[1]; 35304451Seschrock 35314451Seschrock if (newvd->vdev_unspare && 35328241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 35338241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) { 35344451Seschrock newvd->vdev_unspare = 0; 35354451Seschrock return (oldvd); 35364451Seschrock } 35374451Seschrock } 35384451Seschrock 35391544Seschrock return (NULL); 3540789Sahrens } 3541789Sahrens 35421544Seschrock static void 35434451Seschrock spa_vdev_resilver_done(spa_t *spa) 3544789Sahrens { 35458241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 35468241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 35478241SJeff.Bonwick@Sun.COM 35488241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3549789Sahrens 35504451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 35518241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 35528241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 35531544Seschrock guid = vd->vdev_guid; 35548241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 35558241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 35568241SJeff.Bonwick@Sun.COM sguid = 0; 35572082Seschrock /* 35582082Seschrock * If we have just finished replacing a hot spared device, then 35592082Seschrock * we need to detach the parent's first child (the original hot 35602082Seschrock * spare) as well. 35612082Seschrock */ 35628241SJeff.Bonwick@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 35632082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 35648241SJeff.Bonwick@Sun.COM ASSERT(ppvd->vdev_children == 2); 35658241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 35662082Seschrock } 35678241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 35688241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 35691544Seschrock return; 35708241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 35712082Seschrock return; 35728241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3573789Sahrens } 3574789Sahrens 35758241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 3576789Sahrens } 3577789Sahrens 3578789Sahrens /* 35799425SEric.Schrock@Sun.COM * Update the stored path or FRU for this vdev. Dirty the vdev configuration, 35809425SEric.Schrock@Sun.COM * relying on spa_vdev_enter/exit() to synchronize the labels and cache. 35811354Seschrock */ 35821354Seschrock int 35839425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 35849425SEric.Schrock@Sun.COM boolean_t ispath) 35851354Seschrock { 35866643Seschrock vdev_t *vd; 35871354Seschrock uint64_t txg; 35881354Seschrock 35891354Seschrock txg = spa_vdev_enter(spa); 35901354Seschrock 35919425SEric.Schrock@Sun.COM if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 35925450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 35931354Seschrock 35941585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 35951585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35961585Sbonwick 35979425SEric.Schrock@Sun.COM if (ispath) { 35989425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_path); 35999425SEric.Schrock@Sun.COM vd->vdev_path = spa_strdup(value); 36009425SEric.Schrock@Sun.COM } else { 36019425SEric.Schrock@Sun.COM if (vd->vdev_fru != NULL) 36029425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 36039425SEric.Schrock@Sun.COM vd->vdev_fru = spa_strdup(value); 36049425SEric.Schrock@Sun.COM } 36051354Seschrock 36061354Seschrock vdev_config_dirty(vd->vdev_top); 36071354Seschrock 36081354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 36091354Seschrock } 36101354Seschrock 36119425SEric.Schrock@Sun.COM int 36129425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 36139425SEric.Schrock@Sun.COM { 36149425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 36159425SEric.Schrock@Sun.COM } 36169425SEric.Schrock@Sun.COM 36179425SEric.Schrock@Sun.COM int 36189425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 36199425SEric.Schrock@Sun.COM { 36209425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 36219425SEric.Schrock@Sun.COM } 36229425SEric.Schrock@Sun.COM 36231354Seschrock /* 3624789Sahrens * ========================================================================== 3625789Sahrens * SPA Scrubbing 3626789Sahrens * ========================================================================== 3627789Sahrens */ 3628789Sahrens 36297046Sahrens int 36307046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3631789Sahrens { 36327754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 36334808Sek110237 3634789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3635789Sahrens return (ENOTSUP); 3636789Sahrens 3637789Sahrens /* 36387046Sahrens * If a resilver was requested, but there is no DTL on a 36397046Sahrens * writeable leaf device, we have nothing to do. 3640789Sahrens */ 36417046Sahrens if (type == POOL_SCRUB_RESILVER && 36427046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 36437046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 36441544Seschrock return (0); 36451544Seschrock } 3646789Sahrens 36477046Sahrens if (type == POOL_SCRUB_EVERYTHING && 36487046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 36497046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 36507046Sahrens return (EBUSY); 36517046Sahrens 36527046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 36537046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 36547046Sahrens } else if (type == POOL_SCRUB_NONE) { 36557046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 36561544Seschrock } else { 36577046Sahrens return (EINVAL); 36581544Seschrock } 3659789Sahrens } 3660789Sahrens 36611544Seschrock /* 36621544Seschrock * ========================================================================== 36631544Seschrock * SPA async task processing 36641544Seschrock * ========================================================================== 36651544Seschrock */ 36661544Seschrock 36671544Seschrock static void 36684451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3669789Sahrens { 36707361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 36717361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 36727361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 36737754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 36747754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 36751544Seschrock } 36767361SBrendan.Gregg@Sun.COM 36777754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 36787361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 36791544Seschrock } 36801544Seschrock 36811544Seschrock static void 36827754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 36837754SJeff.Bonwick@Sun.COM { 36847754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 36857754SJeff.Bonwick@Sun.COM vd->vdev_probe_wanted = 0; 36867754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 36877754SJeff.Bonwick@Sun.COM } 36887754SJeff.Bonwick@Sun.COM 36897754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 36907754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 36917754SJeff.Bonwick@Sun.COM } 36927754SJeff.Bonwick@Sun.COM 36937754SJeff.Bonwick@Sun.COM static void 36949816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd) 36959816SGeorge.Wilson@Sun.COM { 36969816SGeorge.Wilson@Sun.COM sysevent_id_t eid; 36979816SGeorge.Wilson@Sun.COM nvlist_t *attr; 36989816SGeorge.Wilson@Sun.COM char *physpath; 36999816SGeorge.Wilson@Sun.COM 37009816SGeorge.Wilson@Sun.COM if (!spa->spa_autoexpand) 37019816SGeorge.Wilson@Sun.COM return; 37029816SGeorge.Wilson@Sun.COM 37039816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 37049816SGeorge.Wilson@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 37059816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, cvd); 37069816SGeorge.Wilson@Sun.COM } 37079816SGeorge.Wilson@Sun.COM 37089816SGeorge.Wilson@Sun.COM if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 37099816SGeorge.Wilson@Sun.COM return; 37109816SGeorge.Wilson@Sun.COM 37119816SGeorge.Wilson@Sun.COM physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 37129816SGeorge.Wilson@Sun.COM (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 37139816SGeorge.Wilson@Sun.COM 37149816SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 37159816SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 37169816SGeorge.Wilson@Sun.COM 37179816SGeorge.Wilson@Sun.COM (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 37189816SGeorge.Wilson@Sun.COM ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 37199816SGeorge.Wilson@Sun.COM 37209816SGeorge.Wilson@Sun.COM nvlist_free(attr); 37219816SGeorge.Wilson@Sun.COM kmem_free(physpath, MAXPATHLEN); 37229816SGeorge.Wilson@Sun.COM } 37239816SGeorge.Wilson@Sun.COM 37249816SGeorge.Wilson@Sun.COM static void 37251544Seschrock spa_async_thread(spa_t *spa) 37261544Seschrock { 37277754SJeff.Bonwick@Sun.COM int tasks; 37281544Seschrock 37291544Seschrock ASSERT(spa->spa_sync_on); 3730789Sahrens 37311544Seschrock mutex_enter(&spa->spa_async_lock); 37321544Seschrock tasks = spa->spa_async_tasks; 37331544Seschrock spa->spa_async_tasks = 0; 37341544Seschrock mutex_exit(&spa->spa_async_lock); 37351544Seschrock 37361544Seschrock /* 37371635Sbonwick * See if the config needs to be updated. 37381635Sbonwick */ 37391635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 37409816SGeorge.Wilson@Sun.COM uint64_t oldsz, space_update; 37419816SGeorge.Wilson@Sun.COM 37421635Sbonwick mutex_enter(&spa_namespace_lock); 37439816SGeorge.Wilson@Sun.COM oldsz = spa_get_space(spa); 37441635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 37459816SGeorge.Wilson@Sun.COM space_update = spa_get_space(spa) - oldsz; 37461635Sbonwick mutex_exit(&spa_namespace_lock); 37479816SGeorge.Wilson@Sun.COM 37489816SGeorge.Wilson@Sun.COM /* 37499816SGeorge.Wilson@Sun.COM * If the pool grew as a result of the config update, 37509816SGeorge.Wilson@Sun.COM * then log an internal history event. 37519816SGeorge.Wilson@Sun.COM */ 37529816SGeorge.Wilson@Sun.COM if (space_update) { 37539946SMark.Musante@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ONLINE, 37549946SMark.Musante@Sun.COM spa, NULL, CRED(), 37559946SMark.Musante@Sun.COM "pool '%s' size: %llu(+%llu)", 37569946SMark.Musante@Sun.COM spa_name(spa), spa_get_space(spa), 37579946SMark.Musante@Sun.COM space_update); 37589816SGeorge.Wilson@Sun.COM } 37591635Sbonwick } 37601635Sbonwick 37611635Sbonwick /* 37624451Seschrock * See if any devices need to be marked REMOVED. 37631544Seschrock */ 37647754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 37657754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 37664451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 37677754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 37687361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 37697754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 37707361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 37717754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 37727754SJeff.Bonwick@Sun.COM } 37737754SJeff.Bonwick@Sun.COM 37749816SGeorge.Wilson@Sun.COM if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 37759816SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 37769816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, spa->spa_root_vdev); 37779816SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 37789816SGeorge.Wilson@Sun.COM } 37799816SGeorge.Wilson@Sun.COM 37807754SJeff.Bonwick@Sun.COM /* 37817754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 37827754SJeff.Bonwick@Sun.COM */ 37837754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 37847754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 37857754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 37867754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 37874451Seschrock } 37881544Seschrock 37891544Seschrock /* 37901544Seschrock * If any devices are done replacing, detach them. 37911544Seschrock */ 37924451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 37934451Seschrock spa_vdev_resilver_done(spa); 3794789Sahrens 37951544Seschrock /* 37961544Seschrock * Kick off a resilver. 37971544Seschrock */ 37987046Sahrens if (tasks & SPA_ASYNC_RESILVER) 37997046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 38001544Seschrock 38011544Seschrock /* 38021544Seschrock * Let the world know that we're done. 38031544Seschrock */ 38041544Seschrock mutex_enter(&spa->spa_async_lock); 38051544Seschrock spa->spa_async_thread = NULL; 38061544Seschrock cv_broadcast(&spa->spa_async_cv); 38071544Seschrock mutex_exit(&spa->spa_async_lock); 38081544Seschrock thread_exit(); 38091544Seschrock } 38101544Seschrock 38111544Seschrock void 38121544Seschrock spa_async_suspend(spa_t *spa) 38131544Seschrock { 38141544Seschrock mutex_enter(&spa->spa_async_lock); 38151544Seschrock spa->spa_async_suspended++; 38161544Seschrock while (spa->spa_async_thread != NULL) 38171544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 38181544Seschrock mutex_exit(&spa->spa_async_lock); 38191544Seschrock } 38201544Seschrock 38211544Seschrock void 38221544Seschrock spa_async_resume(spa_t *spa) 38231544Seschrock { 38241544Seschrock mutex_enter(&spa->spa_async_lock); 38251544Seschrock ASSERT(spa->spa_async_suspended != 0); 38261544Seschrock spa->spa_async_suspended--; 38271544Seschrock mutex_exit(&spa->spa_async_lock); 38281544Seschrock } 38291544Seschrock 38301544Seschrock static void 38311544Seschrock spa_async_dispatch(spa_t *spa) 38321544Seschrock { 38331544Seschrock mutex_enter(&spa->spa_async_lock); 38341544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 38351635Sbonwick spa->spa_async_thread == NULL && 38361635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 38371544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 38381544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 38391544Seschrock mutex_exit(&spa->spa_async_lock); 38401544Seschrock } 38411544Seschrock 38421544Seschrock void 38431544Seschrock spa_async_request(spa_t *spa, int task) 38441544Seschrock { 38451544Seschrock mutex_enter(&spa->spa_async_lock); 38461544Seschrock spa->spa_async_tasks |= task; 38471544Seschrock mutex_exit(&spa->spa_async_lock); 3848789Sahrens } 3849789Sahrens 3850789Sahrens /* 3851789Sahrens * ========================================================================== 3852789Sahrens * SPA syncing routines 3853789Sahrens * ========================================================================== 3854789Sahrens */ 3855789Sahrens 3856789Sahrens static void 3857789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3858789Sahrens { 3859789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3860789Sahrens dmu_tx_t *tx; 3861789Sahrens blkptr_t blk; 3862789Sahrens uint64_t itor = 0; 3863789Sahrens zio_t *zio; 3864789Sahrens int error; 3865789Sahrens uint8_t c = 1; 3866789Sahrens 38677754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 38687754SJeff.Bonwick@Sun.COM 38697754SJeff.Bonwick@Sun.COM while (bplist_iterate(bpl, &itor, &blk) == 0) { 38707754SJeff.Bonwick@Sun.COM ASSERT(blk.blk_birth < txg); 38717754SJeff.Bonwick@Sun.COM zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL, 38727754SJeff.Bonwick@Sun.COM ZIO_FLAG_MUSTSUCCEED)); 38737754SJeff.Bonwick@Sun.COM } 3874789Sahrens 3875789Sahrens error = zio_wait(zio); 3876789Sahrens ASSERT3U(error, ==, 0); 3877789Sahrens 3878789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3879789Sahrens bplist_vacate(bpl, tx); 3880789Sahrens 3881789Sahrens /* 3882789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3883789Sahrens * (Usually only the first block is needed.) 3884789Sahrens */ 3885789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3886789Sahrens dmu_tx_commit(tx); 3887789Sahrens } 3888789Sahrens 3889789Sahrens static void 38902082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 38912082Seschrock { 38922082Seschrock char *packed = NULL; 38937497STim.Haley@Sun.COM size_t bufsize; 38942082Seschrock size_t nvsize = 0; 38952082Seschrock dmu_buf_t *db; 38962082Seschrock 38972082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 38982082Seschrock 38997497STim.Haley@Sun.COM /* 39007497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 39017497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 39027497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 39037497STim.Haley@Sun.COM */ 39047497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 39057497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 39062082Seschrock 39072082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 39082082Seschrock KM_SLEEP) == 0); 39097497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 39107497STim.Haley@Sun.COM 39117497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 39127497STim.Haley@Sun.COM 39137497STim.Haley@Sun.COM kmem_free(packed, bufsize); 39142082Seschrock 39152082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 39162082Seschrock dmu_buf_will_dirty(db, tx); 39172082Seschrock *(uint64_t *)db->db_data = nvsize; 39182082Seschrock dmu_buf_rele(db, FTAG); 39192082Seschrock } 39202082Seschrock 39212082Seschrock static void 39225450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 39235450Sbrendan const char *config, const char *entry) 39242082Seschrock { 39252082Seschrock nvlist_t *nvroot; 39265450Sbrendan nvlist_t **list; 39272082Seschrock int i; 39282082Seschrock 39295450Sbrendan if (!sav->sav_sync) 39302082Seschrock return; 39312082Seschrock 39322082Seschrock /* 39335450Sbrendan * Update the MOS nvlist describing the list of available devices. 39345450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 39354451Seschrock * valid and the vdevs are labeled appropriately. 39362082Seschrock */ 39375450Sbrendan if (sav->sav_object == 0) { 39385450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 39395450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 39405450Sbrendan sizeof (uint64_t), tx); 39412082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 39425450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 39435450Sbrendan &sav->sav_object, tx) == 0); 39442082Seschrock } 39452082Seschrock 39462082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 39475450Sbrendan if (sav->sav_count == 0) { 39485450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 39492082Seschrock } else { 39505450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 39515450Sbrendan for (i = 0; i < sav->sav_count; i++) 39525450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 39535450Sbrendan B_FALSE, B_FALSE, B_TRUE); 39545450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 39555450Sbrendan sav->sav_count) == 0); 39565450Sbrendan for (i = 0; i < sav->sav_count; i++) 39575450Sbrendan nvlist_free(list[i]); 39585450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 39592082Seschrock } 39602082Seschrock 39615450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 39622926Sek110237 nvlist_free(nvroot); 39632082Seschrock 39645450Sbrendan sav->sav_sync = B_FALSE; 39652082Seschrock } 39662082Seschrock 39672082Seschrock static void 3968789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3969789Sahrens { 3970789Sahrens nvlist_t *config; 3971789Sahrens 39727754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 3973789Sahrens return; 3974789Sahrens 39757754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 39767754SJeff.Bonwick@Sun.COM 39777754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 39787754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 39797754SJeff.Bonwick@Sun.COM 39807754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 3981789Sahrens 39821635Sbonwick if (spa->spa_config_syncing) 39831635Sbonwick nvlist_free(spa->spa_config_syncing); 39841635Sbonwick spa->spa_config_syncing = config; 3985789Sahrens 39862082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3987789Sahrens } 3988789Sahrens 39895094Slling /* 39905094Slling * Set zpool properties. 39915094Slling */ 39923912Slling static void 39934543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 39943912Slling { 39953912Slling spa_t *spa = arg1; 39965094Slling objset_t *mos = spa->spa_meta_objset; 39973912Slling nvlist_t *nvp = arg2; 39985094Slling nvpair_t *elem; 39994451Seschrock uint64_t intval; 40006643Seschrock char *strval; 40015094Slling zpool_prop_t prop; 40025094Slling const char *propname; 40035094Slling zprop_type_t proptype; 40045094Slling 40057754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 40067754SJeff.Bonwick@Sun.COM 40075094Slling elem = NULL; 40085094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 40095094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 40105094Slling case ZPOOL_PROP_VERSION: 40115094Slling /* 40125094Slling * Only set version for non-zpool-creation cases 40135094Slling * (set/import). spa_create() needs special care 40145094Slling * for version setting. 40155094Slling */ 40165094Slling if (tx->tx_txg != TXG_INITIAL) { 40175094Slling VERIFY(nvpair_value_uint64(elem, 40185094Slling &intval) == 0); 40195094Slling ASSERT(intval <= SPA_VERSION); 40205094Slling ASSERT(intval >= spa_version(spa)); 40215094Slling spa->spa_uberblock.ub_version = intval; 40225094Slling vdev_config_dirty(spa->spa_root_vdev); 40235094Slling } 40245094Slling break; 40255094Slling 40265094Slling case ZPOOL_PROP_ALTROOT: 40275094Slling /* 40285094Slling * 'altroot' is a non-persistent property. It should 40295094Slling * have been set temporarily at creation or import time. 40305094Slling */ 40315094Slling ASSERT(spa->spa_root != NULL); 40325094Slling break; 40335094Slling 40345363Seschrock case ZPOOL_PROP_CACHEFILE: 40355094Slling /* 40368525SEric.Schrock@Sun.COM * 'cachefile' is also a non-persisitent property. 40375094Slling */ 40384543Smarks break; 40395094Slling default: 40405094Slling /* 40415094Slling * Set pool property values in the poolprops mos object. 40425094Slling */ 40435094Slling if (spa->spa_pool_props_object == 0) { 40445094Slling objset_t *mos = spa->spa_meta_objset; 40455094Slling 40465094Slling VERIFY((spa->spa_pool_props_object = 40475094Slling zap_create(mos, DMU_OT_POOL_PROPS, 40485094Slling DMU_OT_NONE, 0, tx)) > 0); 40495094Slling 40505094Slling VERIFY(zap_update(mos, 40515094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 40525094Slling 8, 1, &spa->spa_pool_props_object, tx) 40535094Slling == 0); 40545094Slling } 40555094Slling 40565094Slling /* normalize the property name */ 40575094Slling propname = zpool_prop_to_name(prop); 40585094Slling proptype = zpool_prop_get_type(prop); 40595094Slling 40605094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 40615094Slling ASSERT(proptype == PROP_TYPE_STRING); 40625094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 40635094Slling VERIFY(zap_update(mos, 40645094Slling spa->spa_pool_props_object, propname, 40655094Slling 1, strlen(strval) + 1, strval, tx) == 0); 40665094Slling 40675094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 40685094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 40695094Slling 40705094Slling if (proptype == PROP_TYPE_INDEX) { 40715094Slling const char *unused; 40725094Slling VERIFY(zpool_prop_index_to_string( 40735094Slling prop, intval, &unused) == 0); 40745094Slling } 40755094Slling VERIFY(zap_update(mos, 40765094Slling spa->spa_pool_props_object, propname, 40775094Slling 8, 1, &intval, tx) == 0); 40785094Slling } else { 40795094Slling ASSERT(0); /* not allowed */ 40805094Slling } 40815094Slling 40825329Sgw25295 switch (prop) { 40835329Sgw25295 case ZPOOL_PROP_DELEGATION: 40845094Slling spa->spa_delegation = intval; 40855329Sgw25295 break; 40865329Sgw25295 case ZPOOL_PROP_BOOTFS: 40875094Slling spa->spa_bootfs = intval; 40885329Sgw25295 break; 40895329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 40905329Sgw25295 spa->spa_failmode = intval; 40915329Sgw25295 break; 40929816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 40939816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = intval; 40949816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 40959816SGeorge.Wilson@Sun.COM break; 40965329Sgw25295 default: 40975329Sgw25295 break; 40985329Sgw25295 } 40993912Slling } 41005094Slling 41015094Slling /* log internal history if this is not a zpool create */ 41025094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 41035094Slling tx->tx_txg != TXG_INITIAL) { 41045094Slling spa_history_internal_log(LOG_POOL_PROPSET, 41055094Slling spa, tx, cr, "%s %lld %s", 41067754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 41075094Slling } 41083912Slling } 41097754SJeff.Bonwick@Sun.COM 41107754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 41113912Slling } 41123912Slling 4113789Sahrens /* 4114789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4115789Sahrens * part of the process, so we iterate until it converges. 4116789Sahrens */ 4117789Sahrens void 4118789Sahrens spa_sync(spa_t *spa, uint64_t txg) 4119789Sahrens { 4120789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4121789Sahrens objset_t *mos = spa->spa_meta_objset; 4122789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 41231635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 4124789Sahrens vdev_t *vd; 4125789Sahrens dmu_tx_t *tx; 4126789Sahrens int dirty_vdevs; 41277754SJeff.Bonwick@Sun.COM int error; 4128789Sahrens 4129789Sahrens /* 4130789Sahrens * Lock out configuration changes. 4131789Sahrens */ 41327754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4133789Sahrens 4134789Sahrens spa->spa_syncing_txg = txg; 4135789Sahrens spa->spa_sync_pass = 0; 4136789Sahrens 41377754SJeff.Bonwick@Sun.COM /* 41387754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 41397754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 41407754SJeff.Bonwick@Sun.COM */ 41417754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 41428241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 41438241SJeff.Bonwick@Sun.COM /* 41448241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 41458241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 41468241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 41478241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 41488241SJeff.Bonwick@Sun.COM * into the root vdev tree. 41498241SJeff.Bonwick@Sun.COM */ 41508241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 41518241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 41528241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 41538241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 41548241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 41558241SJeff.Bonwick@Sun.COM } 41568241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 41578241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 41587754SJeff.Bonwick@Sun.COM } 41597754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 41607754SJeff.Bonwick@Sun.COM 41611544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4162789Sahrens 41632082Seschrock tx = dmu_tx_create_assigned(dp, txg); 41642082Seschrock 41652082Seschrock /* 41664577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 41672082Seschrock * set spa_deflate if we have no raid-z vdevs. 41682082Seschrock */ 41694577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 41704577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 41712082Seschrock int i; 41722082Seschrock 41732082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 41742082Seschrock vd = rvd->vdev_child[i]; 41752082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 41762082Seschrock break; 41772082Seschrock } 41782082Seschrock if (i == rvd->vdev_children) { 41792082Seschrock spa->spa_deflate = TRUE; 41802082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 41812082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 41822082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 41832082Seschrock } 41842082Seschrock } 41852082Seschrock 41867046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 41877046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 41887046Sahrens dsl_pool_create_origin(dp, tx); 41897046Sahrens 41907046Sahrens /* Keeping the origin open increases spa_minref */ 41917046Sahrens spa->spa_minref += 3; 41927046Sahrens } 41937046Sahrens 41947046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 41957046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 41967046Sahrens dsl_pool_upgrade_clones(dp, tx); 41977046Sahrens } 41987046Sahrens 4199789Sahrens /* 4200789Sahrens * If anything has changed in this txg, push the deferred frees 4201789Sahrens * from the previous txg. If not, leave them alone so that we 4202789Sahrens * don't generate work on an otherwise idle system. 4203789Sahrens */ 4204789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 42052329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 42062329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 4207789Sahrens spa_sync_deferred_frees(spa, txg); 4208789Sahrens 4209789Sahrens /* 4210789Sahrens * Iterate to convergence. 4211789Sahrens */ 4212789Sahrens do { 4213789Sahrens spa->spa_sync_pass++; 4214789Sahrens 4215789Sahrens spa_sync_config_object(spa, tx); 42165450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 42175450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 42185450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 42195450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 42201544Seschrock spa_errlog_sync(spa, txg); 4221789Sahrens dsl_pool_sync(dp, txg); 4222789Sahrens 4223789Sahrens dirty_vdevs = 0; 4224789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4225789Sahrens vdev_sync(vd, txg); 4226789Sahrens dirty_vdevs++; 4227789Sahrens } 4228789Sahrens 4229789Sahrens bplist_sync(bpl, tx); 4230789Sahrens } while (dirty_vdevs); 4231789Sahrens 4232789Sahrens bplist_close(bpl); 4233789Sahrens 4234789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4235789Sahrens 4236789Sahrens /* 4237789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4238789Sahrens * to commit the transaction group. 42391635Sbonwick * 42405688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 42415688Sbonwick * random top-level vdevs that are known to be visible in the 42427754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 42437754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4244789Sahrens */ 42457754SJeff.Bonwick@Sun.COM for (;;) { 42467754SJeff.Bonwick@Sun.COM /* 42477754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 42487754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 42497754SJeff.Bonwick@Sun.COM */ 42507754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 42517754SJeff.Bonwick@Sun.COM 42527754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 42537754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 42547754SJeff.Bonwick@Sun.COM int svdcount = 0; 42557754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 42567754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 42579816SGeorge.Wilson@Sun.COM 42589816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 42597754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 42607754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 42617754SJeff.Bonwick@Sun.COM continue; 42627754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 42637754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 42647754SJeff.Bonwick@Sun.COM break; 42657754SJeff.Bonwick@Sun.COM } 42669725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 42679725SEric.Schrock@Sun.COM if (error != 0) 42689725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, 42699725SEric.Schrock@Sun.COM B_TRUE); 42707754SJeff.Bonwick@Sun.COM } else { 42717754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 42729725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_FALSE); 42739725SEric.Schrock@Sun.COM if (error != 0) 42749725SEric.Schrock@Sun.COM error = vdev_config_sync(rvd->vdev_child, 42759725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_TRUE); 42761635Sbonwick } 42777754SJeff.Bonwick@Sun.COM 42787754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 42797754SJeff.Bonwick@Sun.COM 42807754SJeff.Bonwick@Sun.COM if (error == 0) 42817754SJeff.Bonwick@Sun.COM break; 42827754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 42837754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 42841635Sbonwick } 42852082Seschrock dmu_tx_commit(tx); 42862082Seschrock 42871635Sbonwick /* 42881635Sbonwick * Clear the dirty config list. 42891635Sbonwick */ 42907754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 42911635Sbonwick vdev_config_clean(vd); 42921635Sbonwick 42931635Sbonwick /* 42941635Sbonwick * Now that the new config has synced transactionally, 42951635Sbonwick * let it become visible to the config cache. 42961635Sbonwick */ 42971635Sbonwick if (spa->spa_config_syncing != NULL) { 42981635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 42991635Sbonwick spa->spa_config_txg = txg; 43001635Sbonwick spa->spa_config_syncing = NULL; 43011635Sbonwick } 4302789Sahrens 4303789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4304789Sahrens 4305789Sahrens /* 4306789Sahrens * Clean up the ZIL records for the synced txg. 4307789Sahrens */ 4308789Sahrens dsl_pool_zil_clean(dp); 4309789Sahrens 4310789Sahrens /* 4311789Sahrens * Update usable space statistics. 4312789Sahrens */ 4313789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4314789Sahrens vdev_sync_done(vd, txg); 4315789Sahrens 4316789Sahrens /* 4317789Sahrens * It had better be the case that we didn't dirty anything 43182082Seschrock * since vdev_config_sync(). 4319789Sahrens */ 4320789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4321789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4322789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4323789Sahrens ASSERT(bpl->bpl_queue == NULL); 4324789Sahrens 43257754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 43261544Seschrock 43271544Seschrock /* 43281544Seschrock * If any async tasks have been requested, kick them off. 43291544Seschrock */ 43301544Seschrock spa_async_dispatch(spa); 4331789Sahrens } 4332789Sahrens 4333789Sahrens /* 4334789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4335789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4336789Sahrens * sync. 4337789Sahrens */ 4338789Sahrens void 4339789Sahrens spa_sync_allpools(void) 4340789Sahrens { 4341789Sahrens spa_t *spa = NULL; 4342789Sahrens mutex_enter(&spa_namespace_lock); 4343789Sahrens while ((spa = spa_next(spa)) != NULL) { 43447754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4345789Sahrens continue; 4346789Sahrens spa_open_ref(spa, FTAG); 4347789Sahrens mutex_exit(&spa_namespace_lock); 4348789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4349789Sahrens mutex_enter(&spa_namespace_lock); 4350789Sahrens spa_close(spa, FTAG); 4351789Sahrens } 4352789Sahrens mutex_exit(&spa_namespace_lock); 4353789Sahrens } 4354789Sahrens 4355789Sahrens /* 4356789Sahrens * ========================================================================== 4357789Sahrens * Miscellaneous routines 4358789Sahrens * ========================================================================== 4359789Sahrens */ 4360789Sahrens 4361789Sahrens /* 4362789Sahrens * Remove all pools in the system. 4363789Sahrens */ 4364789Sahrens void 4365789Sahrens spa_evict_all(void) 4366789Sahrens { 4367789Sahrens spa_t *spa; 4368789Sahrens 4369789Sahrens /* 4370789Sahrens * Remove all cached state. All pools should be closed now, 4371789Sahrens * so every spa in the AVL tree should be unreferenced. 4372789Sahrens */ 4373789Sahrens mutex_enter(&spa_namespace_lock); 4374789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4375789Sahrens /* 43761544Seschrock * Stop async tasks. The async thread may need to detach 43771544Seschrock * a device that's been replaced, which requires grabbing 43781544Seschrock * spa_namespace_lock, so we must drop it here. 4379789Sahrens */ 4380789Sahrens spa_open_ref(spa, FTAG); 4381789Sahrens mutex_exit(&spa_namespace_lock); 43821544Seschrock spa_async_suspend(spa); 43834808Sek110237 mutex_enter(&spa_namespace_lock); 4384789Sahrens spa_close(spa, FTAG); 4385789Sahrens 4386789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4387789Sahrens spa_unload(spa); 4388789Sahrens spa_deactivate(spa); 4389789Sahrens } 4390789Sahrens spa_remove(spa); 4391789Sahrens } 4392789Sahrens mutex_exit(&spa_namespace_lock); 4393789Sahrens } 43941544Seschrock 43951544Seschrock vdev_t * 43969425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 43971544Seschrock { 43986643Seschrock vdev_t *vd; 43996643Seschrock int i; 44006643Seschrock 44016643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 44026643Seschrock return (vd); 44036643Seschrock 44049425SEric.Schrock@Sun.COM if (aux) { 44056643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 44066643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 44076643Seschrock if (vd->vdev_guid == guid) 44086643Seschrock return (vd); 44096643Seschrock } 44109425SEric.Schrock@Sun.COM 44119425SEric.Schrock@Sun.COM for (i = 0; i < spa->spa_spares.sav_count; i++) { 44129425SEric.Schrock@Sun.COM vd = spa->spa_spares.sav_vdevs[i]; 44139425SEric.Schrock@Sun.COM if (vd->vdev_guid == guid) 44149425SEric.Schrock@Sun.COM return (vd); 44159425SEric.Schrock@Sun.COM } 44166643Seschrock } 44176643Seschrock 44186643Seschrock return (NULL); 44191544Seschrock } 44201760Seschrock 44211760Seschrock void 44225094Slling spa_upgrade(spa_t *spa, uint64_t version) 44231760Seschrock { 44247754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 44251760Seschrock 44261760Seschrock /* 44271760Seschrock * This should only be called for a non-faulted pool, and since a 44281760Seschrock * future version would result in an unopenable pool, this shouldn't be 44291760Seschrock * possible. 44301760Seschrock */ 44314577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 44325094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 44335094Slling 44345094Slling spa->spa_uberblock.ub_version = version; 44351760Seschrock vdev_config_dirty(spa->spa_root_vdev); 44361760Seschrock 44377754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 44382082Seschrock 44392082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 44401760Seschrock } 44412082Seschrock 44422082Seschrock boolean_t 44432082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 44442082Seschrock { 44452082Seschrock int i; 44463377Seschrock uint64_t spareguid; 44475450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 44485450Sbrendan 44495450Sbrendan for (i = 0; i < sav->sav_count; i++) 44505450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 44512082Seschrock return (B_TRUE); 44522082Seschrock 44535450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 44545450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 44555450Sbrendan &spareguid) == 0 && spareguid == guid) 44563377Seschrock return (B_TRUE); 44573377Seschrock } 44583377Seschrock 44592082Seschrock return (B_FALSE); 44602082Seschrock } 44613912Slling 44624451Seschrock /* 44637214Slling * Check if a pool has an active shared spare device. 44647214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 44657214Slling */ 44667214Slling static boolean_t 44677214Slling spa_has_active_shared_spare(spa_t *spa) 44687214Slling { 44697214Slling int i, refcnt; 44707214Slling uint64_t pool; 44717214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 44727214Slling 44737214Slling for (i = 0; i < sav->sav_count; i++) { 44747214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 44757214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 44767214Slling refcnt > 2) 44777214Slling return (B_TRUE); 44787214Slling } 44797214Slling 44807214Slling return (B_FALSE); 44817214Slling } 44827214Slling 44837214Slling /* 44844451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 44854451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 44864451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 44874451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 44884451Seschrock * or zdb as real changes. 44894451Seschrock */ 44904451Seschrock void 44914451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 44924451Seschrock { 44934451Seschrock #ifdef _KERNEL 44944451Seschrock sysevent_t *ev; 44954451Seschrock sysevent_attr_list_t *attr = NULL; 44964451Seschrock sysevent_value_t value; 44974451Seschrock sysevent_id_t eid; 44984451Seschrock 44994451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 45004451Seschrock SE_SLEEP); 45014451Seschrock 45024451Seschrock value.value_type = SE_DATA_TYPE_STRING; 45034451Seschrock value.value.sv_string = spa_name(spa); 45044451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 45054451Seschrock goto done; 45064451Seschrock 45074451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 45084451Seschrock value.value.sv_uint64 = spa_guid(spa); 45094451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 45104451Seschrock goto done; 45114451Seschrock 45124451Seschrock if (vd) { 45134451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 45144451Seschrock value.value.sv_uint64 = vd->vdev_guid; 45154451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 45164451Seschrock SE_SLEEP) != 0) 45174451Seschrock goto done; 45184451Seschrock 45194451Seschrock if (vd->vdev_path) { 45204451Seschrock value.value_type = SE_DATA_TYPE_STRING; 45214451Seschrock value.value.sv_string = vd->vdev_path; 45224451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 45234451Seschrock &value, SE_SLEEP) != 0) 45244451Seschrock goto done; 45254451Seschrock } 45264451Seschrock } 45274451Seschrock 45285756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 45295756Seschrock goto done; 45305756Seschrock attr = NULL; 45315756Seschrock 45324451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 45334451Seschrock 45344451Seschrock done: 45354451Seschrock if (attr) 45364451Seschrock sysevent_free_attr(attr); 45374451Seschrock sysevent_free(ev); 45384451Seschrock #endif 45394451Seschrock } 4540