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/dmu.h> 39789Sahrens #include <sys/dmu_tx.h> 40789Sahrens #include <sys/zap.h> 41789Sahrens #include <sys/zil.h> 4210922SJeff.Bonwick@Sun.COM #include <sys/ddt.h> 43789Sahrens #include <sys/vdev_impl.h> 44789Sahrens #include <sys/metaslab.h> 4510594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h> 46789Sahrens #include <sys/uberblock_impl.h> 47789Sahrens #include <sys/txg.h> 48789Sahrens #include <sys/avl.h> 49789Sahrens #include <sys/dmu_traverse.h> 503912Slling #include <sys/dmu_objset.h> 51789Sahrens #include <sys/unique.h> 52789Sahrens #include <sys/dsl_pool.h> 533912Slling #include <sys/dsl_dataset.h> 54789Sahrens #include <sys/dsl_dir.h> 55789Sahrens #include <sys/dsl_prop.h> 563912Slling #include <sys/dsl_synctask.h> 57789Sahrens #include <sys/fs/zfs.h> 585450Sbrendan #include <sys/arc.h> 59789Sahrens #include <sys/callb.h> 603975Sek110237 #include <sys/systeminfo.h> 613975Sek110237 #include <sys/sunddi.h> 626423Sgw25295 #include <sys/spa_boot.h> 639816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h> 64789Sahrens 658662SJordan.Vaughan@Sun.com #ifdef _KERNEL 668662SJordan.Vaughan@Sun.com #include <sys/zone.h> 6710822SJack.Meng@Sun.COM #include <sys/bootprops.h> 688662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 698662SJordan.Vaughan@Sun.com 705094Slling #include "zfs_prop.h" 715913Sperrin #include "zfs_comutil.h" 725094Slling 739515SJonathan.Adams@Sun.COM enum zti_modes { 749515SJonathan.Adams@Sun.COM zti_mode_fixed, /* value is # of threads (min 1) */ 759515SJonathan.Adams@Sun.COM zti_mode_online_percent, /* value is % of online CPUs */ 769515SJonathan.Adams@Sun.COM zti_mode_tune, /* fill from zio_taskq_tune_* */ 779515SJonathan.Adams@Sun.COM zti_nmodes 787754SJeff.Bonwick@Sun.COM }; 792986Sek110237 809515SJonathan.Adams@Sun.COM #define ZTI_THREAD_FIX(n) { zti_mode_fixed, (n) } 819515SJonathan.Adams@Sun.COM #define ZTI_THREAD_PCT(n) { zti_mode_online_percent, (n) } 829515SJonathan.Adams@Sun.COM #define ZTI_THREAD_TUNE { zti_mode_tune, 0 } 839515SJonathan.Adams@Sun.COM 849515SJonathan.Adams@Sun.COM #define ZTI_THREAD_ONE ZTI_THREAD_FIX(1) 859515SJonathan.Adams@Sun.COM 869515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info { 879515SJonathan.Adams@Sun.COM const char *zti_name; 889515SJonathan.Adams@Sun.COM struct { 899515SJonathan.Adams@Sun.COM enum zti_modes zti_mode; 909515SJonathan.Adams@Sun.COM uint_t zti_value; 919515SJonathan.Adams@Sun.COM } zti_nthreads[ZIO_TASKQ_TYPES]; 929515SJonathan.Adams@Sun.COM } zio_taskq_info_t; 939515SJonathan.Adams@Sun.COM 949515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 959515SJonathan.Adams@Sun.COM "issue", "intr" 969515SJonathan.Adams@Sun.COM }; 979515SJonathan.Adams@Sun.COM 989515SJonathan.Adams@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES] = { 999515SJonathan.Adams@Sun.COM /* ISSUE INTR */ 1009515SJonathan.Adams@Sun.COM { "spa_zio_null", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1019515SJonathan.Adams@Sun.COM { "spa_zio_read", { ZTI_THREAD_FIX(8), ZTI_THREAD_TUNE } }, 1029515SJonathan.Adams@Sun.COM { "spa_zio_write", { ZTI_THREAD_TUNE, ZTI_THREAD_FIX(8) } }, 1039515SJonathan.Adams@Sun.COM { "spa_zio_free", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1049515SJonathan.Adams@Sun.COM { "spa_zio_claim", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1059515SJonathan.Adams@Sun.COM { "spa_zio_ioctl", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 1069515SJonathan.Adams@Sun.COM }; 1079515SJonathan.Adams@Sun.COM 1089515SJonathan.Adams@Sun.COM enum zti_modes zio_taskq_tune_mode = zti_mode_online_percent; 1099515SJonathan.Adams@Sun.COM uint_t zio_taskq_tune_value = 80; /* #threads = 80% of # online CPUs */ 1109515SJonathan.Adams@Sun.COM 1115094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 1127214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1135094Slling 1145094Slling /* 1155094Slling * ========================================================================== 1165094Slling * SPA properties routines 1175094Slling * ========================================================================== 1185094Slling */ 1195094Slling 1205094Slling /* 1215094Slling * Add a (source=src, propname=propval) list to an nvlist. 1225094Slling */ 1235949Slling static void 1245094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 1255094Slling uint64_t intval, zprop_source_t src) 1265094Slling { 1275094Slling const char *propname = zpool_prop_to_name(prop); 1285094Slling nvlist_t *propval; 1295949Slling 1305949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1315949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 1325949Slling 1335949Slling if (strval != NULL) 1345949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1355949Slling else 1365949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 1375949Slling 1385949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 1395094Slling nvlist_free(propval); 1405094Slling } 1415094Slling 1425094Slling /* 1435094Slling * Get property values from the spa configuration. 1445094Slling */ 1455949Slling static void 1465094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1475094Slling { 1488525SEric.Schrock@Sun.COM uint64_t size; 14910956SGeorge.Wilson@Sun.COM uint64_t alloc; 1505094Slling uint64_t cap, version; 1515094Slling zprop_source_t src = ZPROP_SRC_NONE; 1526643Seschrock spa_config_dirent_t *dp; 1535094Slling 1547754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 1557754SJeff.Bonwick@Sun.COM 1568525SEric.Schrock@Sun.COM if (spa->spa_root_vdev != NULL) { 15710956SGeorge.Wilson@Sun.COM alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 15810922SJeff.Bonwick@Sun.COM size = metaslab_class_get_space(spa_normal_class(spa)); 1598525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 1608525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 16110956SGeorge.Wilson@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 16210956SGeorge.Wilson@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 16310956SGeorge.Wilson@Sun.COM size - alloc, src); 16410956SGeorge.Wilson@Sun.COM 16510956SGeorge.Wilson@Sun.COM cap = (size == 0) ? 0 : (alloc * 100 / size); 1668525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1678525SEric.Schrock@Sun.COM 16810922SJeff.Bonwick@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 16910922SJeff.Bonwick@Sun.COM ddt_get_pool_dedup_ratio(spa), src); 17010922SJeff.Bonwick@Sun.COM 1718525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1728525SEric.Schrock@Sun.COM spa->spa_root_vdev->vdev_state, src); 1738525SEric.Schrock@Sun.COM 1748525SEric.Schrock@Sun.COM version = spa_version(spa); 1758525SEric.Schrock@Sun.COM if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1768525SEric.Schrock@Sun.COM src = ZPROP_SRC_DEFAULT; 1778525SEric.Schrock@Sun.COM else 1788525SEric.Schrock@Sun.COM src = ZPROP_SRC_LOCAL; 1798525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1808525SEric.Schrock@Sun.COM } 1815949Slling 1825949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1835949Slling 1845949Slling if (spa->spa_root != NULL) 1855949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1865949Slling 0, ZPROP_SRC_LOCAL); 1875094Slling 1886643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 1896643Seschrock if (dp->scd_path == NULL) { 1905949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1916643Seschrock "none", 0, ZPROP_SRC_LOCAL); 1926643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1935949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1946643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1955363Seschrock } 1965363Seschrock } 1975094Slling } 1985094Slling 1995094Slling /* 2005094Slling * Get zpool property values. 2015094Slling */ 2025094Slling int 2035094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 2045094Slling { 20510922SJeff.Bonwick@Sun.COM objset_t *mos = spa->spa_meta_objset; 2065094Slling zap_cursor_t zc; 2075094Slling zap_attribute_t za; 2085094Slling int err; 2095094Slling 2105949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2115094Slling 2127754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 2137754SJeff.Bonwick@Sun.COM 2145094Slling /* 2155094Slling * Get properties from the spa config. 2165094Slling */ 2175949Slling spa_prop_get_config(spa, nvp); 2185094Slling 2195094Slling /* If no pool property object, no more prop to get. */ 2205094Slling if (spa->spa_pool_props_object == 0) { 2215094Slling mutex_exit(&spa->spa_props_lock); 2225094Slling return (0); 2235094Slling } 2245094Slling 2255094Slling /* 2265094Slling * Get properties from the MOS pool property object. 2275094Slling */ 2285094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 2295094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 2305094Slling zap_cursor_advance(&zc)) { 2315094Slling uint64_t intval = 0; 2325094Slling char *strval = NULL; 2335094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 2345094Slling zpool_prop_t prop; 2355094Slling 2365094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 2375094Slling continue; 2385094Slling 2395094Slling switch (za.za_integer_length) { 2405094Slling case 8: 2415094Slling /* integer property */ 2425094Slling if (za.za_first_integer != 2435094Slling zpool_prop_default_numeric(prop)) 2445094Slling src = ZPROP_SRC_LOCAL; 2455094Slling 2465094Slling if (prop == ZPOOL_PROP_BOOTFS) { 2475094Slling dsl_pool_t *dp; 2485094Slling dsl_dataset_t *ds = NULL; 2495094Slling 2505094Slling dp = spa_get_dsl(spa); 2515094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2526689Smaybee if (err = dsl_dataset_hold_obj(dp, 2536689Smaybee za.za_first_integer, FTAG, &ds)) { 2545094Slling rw_exit(&dp->dp_config_rwlock); 2555094Slling break; 2565094Slling } 2575094Slling 2585094Slling strval = kmem_alloc( 2595094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2605094Slling KM_SLEEP); 2615094Slling dsl_dataset_name(ds, strval); 2626689Smaybee dsl_dataset_rele(ds, FTAG); 2635094Slling rw_exit(&dp->dp_config_rwlock); 2645094Slling } else { 2655094Slling strval = NULL; 2665094Slling intval = za.za_first_integer; 2675094Slling } 2685094Slling 2695949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2705094Slling 2715094Slling if (strval != NULL) 2725094Slling kmem_free(strval, 2735094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2745094Slling 2755094Slling break; 2765094Slling 2775094Slling case 1: 2785094Slling /* string property */ 2795094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2805094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2815094Slling za.za_name, 1, za.za_num_integers, strval); 2825094Slling if (err) { 2835094Slling kmem_free(strval, za.za_num_integers); 2845094Slling break; 2855094Slling } 2865949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2875094Slling kmem_free(strval, za.za_num_integers); 2885094Slling break; 2895094Slling 2905094Slling default: 2915094Slling break; 2925094Slling } 2935094Slling } 2945094Slling zap_cursor_fini(&zc); 2955094Slling mutex_exit(&spa->spa_props_lock); 2965094Slling out: 2975094Slling if (err && err != ENOENT) { 2985094Slling nvlist_free(*nvp); 2995949Slling *nvp = NULL; 3005094Slling return (err); 3015094Slling } 3025094Slling 3035094Slling return (0); 3045094Slling } 3055094Slling 3065094Slling /* 3075094Slling * Validate the given pool properties nvlist and modify the list 3085094Slling * for the property values to be set. 3095094Slling */ 3105094Slling static int 3115094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 3125094Slling { 3135094Slling nvpair_t *elem; 3145094Slling int error = 0, reset_bootfs = 0; 3155094Slling uint64_t objnum; 3165094Slling 3175094Slling elem = NULL; 3185094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3195094Slling zpool_prop_t prop; 3205094Slling char *propname, *strval; 3215094Slling uint64_t intval; 3225094Slling objset_t *os; 3235363Seschrock char *slash; 3245094Slling 3255094Slling propname = nvpair_name(elem); 3265094Slling 3275094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 3285094Slling return (EINVAL); 3295094Slling 3305094Slling switch (prop) { 3315094Slling case ZPOOL_PROP_VERSION: 3325094Slling error = nvpair_value_uint64(elem, &intval); 3335094Slling if (!error && 3345094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 3355094Slling error = EINVAL; 3365094Slling break; 3375094Slling 3385094Slling case ZPOOL_PROP_DELEGATION: 3395094Slling case ZPOOL_PROP_AUTOREPLACE: 3407538SRichard.Morris@Sun.COM case ZPOOL_PROP_LISTSNAPS: 3419816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 3425094Slling error = nvpair_value_uint64(elem, &intval); 3435094Slling if (!error && intval > 1) 3445094Slling error = EINVAL; 3455094Slling break; 3465094Slling 3475094Slling case ZPOOL_PROP_BOOTFS: 3489630SJeff.Bonwick@Sun.COM /* 3499630SJeff.Bonwick@Sun.COM * If the pool version is less than SPA_VERSION_BOOTFS, 3509630SJeff.Bonwick@Sun.COM * or the pool is still being created (version == 0), 3519630SJeff.Bonwick@Sun.COM * the bootfs property cannot be set. 3529630SJeff.Bonwick@Sun.COM */ 3535094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3545094Slling error = ENOTSUP; 3555094Slling break; 3565094Slling } 3575094Slling 3585094Slling /* 3597042Sgw25295 * Make sure the vdev config is bootable 3605094Slling */ 3617042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3625094Slling error = ENOTSUP; 3635094Slling break; 3645094Slling } 3655094Slling 3665094Slling reset_bootfs = 1; 3675094Slling 3685094Slling error = nvpair_value_string(elem, &strval); 3695094Slling 3705094Slling if (!error) { 3717042Sgw25295 uint64_t compress; 3727042Sgw25295 3735094Slling if (strval == NULL || strval[0] == '\0') { 3745094Slling objnum = zpool_prop_default_numeric( 3755094Slling ZPOOL_PROP_BOOTFS); 3765094Slling break; 3775094Slling } 3785094Slling 37910298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(strval, FTAG, &os)) 3805094Slling break; 3817042Sgw25295 38210298SMatthew.Ahrens@Sun.COM /* Must be ZPL and not gzip compressed. */ 38310298SMatthew.Ahrens@Sun.COM 38410298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 38510298SMatthew.Ahrens@Sun.COM error = ENOTSUP; 38610298SMatthew.Ahrens@Sun.COM } else if ((error = dsl_prop_get_integer(strval, 3877042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3887042Sgw25295 &compress, NULL)) == 0 && 3897042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3907042Sgw25295 error = ENOTSUP; 3917042Sgw25295 } else { 3927042Sgw25295 objnum = dmu_objset_id(os); 3937042Sgw25295 } 39410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 3955094Slling } 3965094Slling break; 3977754SJeff.Bonwick@Sun.COM 3985329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3995329Sgw25295 error = nvpair_value_uint64(elem, &intval); 4005329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 4015329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 4025329Sgw25295 error = EINVAL; 4035329Sgw25295 4045329Sgw25295 /* 4055329Sgw25295 * This is a special case which only occurs when 4065329Sgw25295 * the pool has completely failed. This allows 4075329Sgw25295 * the user to change the in-core failmode property 4085329Sgw25295 * without syncing it out to disk (I/Os might 4095329Sgw25295 * currently be blocked). We do this by returning 4105329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 4115329Sgw25295 * into thinking we encountered a property validation 4125329Sgw25295 * error. 4135329Sgw25295 */ 4147754SJeff.Bonwick@Sun.COM if (!error && spa_suspended(spa)) { 4155329Sgw25295 spa->spa_failmode = intval; 4165329Sgw25295 error = EIO; 4175329Sgw25295 } 4185329Sgw25295 break; 4195363Seschrock 4205363Seschrock case ZPOOL_PROP_CACHEFILE: 4215363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4225363Seschrock break; 4235363Seschrock 4245363Seschrock if (strval[0] == '\0') 4255363Seschrock break; 4265363Seschrock 4275363Seschrock if (strcmp(strval, "none") == 0) 4285363Seschrock break; 4295363Seschrock 4305363Seschrock if (strval[0] != '/') { 4315363Seschrock error = EINVAL; 4325363Seschrock break; 4335363Seschrock } 4345363Seschrock 4355363Seschrock slash = strrchr(strval, '/'); 4365363Seschrock ASSERT(slash != NULL); 4375363Seschrock 4385363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4395363Seschrock strcmp(slash, "/..") == 0) 4405363Seschrock error = EINVAL; 4415363Seschrock break; 44210922SJeff.Bonwick@Sun.COM 44310922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 44410922SJeff.Bonwick@Sun.COM if (spa_version(spa) < SPA_VERSION_DEDUP) 44510922SJeff.Bonwick@Sun.COM error = ENOTSUP; 44610922SJeff.Bonwick@Sun.COM else 44710922SJeff.Bonwick@Sun.COM error = nvpair_value_uint64(elem, &intval); 44810922SJeff.Bonwick@Sun.COM if (error == 0 && 44910922SJeff.Bonwick@Sun.COM intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 45010922SJeff.Bonwick@Sun.COM error = EINVAL; 45110922SJeff.Bonwick@Sun.COM break; 4525094Slling } 4535094Slling 4545094Slling if (error) 4555094Slling break; 4565094Slling } 4575094Slling 4585094Slling if (!error && reset_bootfs) { 4595094Slling error = nvlist_remove(props, 4605094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 4615094Slling 4625094Slling if (!error) { 4635094Slling error = nvlist_add_uint64(props, 4645094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 4655094Slling } 4665094Slling } 4675094Slling 4685094Slling return (error); 4695094Slling } 4705094Slling 4718525SEric.Schrock@Sun.COM void 4728525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 4738525SEric.Schrock@Sun.COM { 4748525SEric.Schrock@Sun.COM char *cachefile; 4758525SEric.Schrock@Sun.COM spa_config_dirent_t *dp; 4768525SEric.Schrock@Sun.COM 4778525SEric.Schrock@Sun.COM if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 4788525SEric.Schrock@Sun.COM &cachefile) != 0) 4798525SEric.Schrock@Sun.COM return; 4808525SEric.Schrock@Sun.COM 4818525SEric.Schrock@Sun.COM dp = kmem_alloc(sizeof (spa_config_dirent_t), 4828525SEric.Schrock@Sun.COM KM_SLEEP); 4838525SEric.Schrock@Sun.COM 4848525SEric.Schrock@Sun.COM if (cachefile[0] == '\0') 4858525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(spa_config_path); 4868525SEric.Schrock@Sun.COM else if (strcmp(cachefile, "none") == 0) 4878525SEric.Schrock@Sun.COM dp->scd_path = NULL; 4888525SEric.Schrock@Sun.COM else 4898525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(cachefile); 4908525SEric.Schrock@Sun.COM 4918525SEric.Schrock@Sun.COM list_insert_head(&spa->spa_config_list, dp); 4928525SEric.Schrock@Sun.COM if (need_sync) 4938525SEric.Schrock@Sun.COM spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 4948525SEric.Schrock@Sun.COM } 4958525SEric.Schrock@Sun.COM 4965094Slling int 4975094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4985094Slling { 4995094Slling int error; 5008525SEric.Schrock@Sun.COM nvpair_t *elem; 5018525SEric.Schrock@Sun.COM boolean_t need_sync = B_FALSE; 5028525SEric.Schrock@Sun.COM zpool_prop_t prop; 5035094Slling 5045094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 5055094Slling return (error); 5065094Slling 5078525SEric.Schrock@Sun.COM elem = NULL; 5088525SEric.Schrock@Sun.COM while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 5098525SEric.Schrock@Sun.COM if ((prop = zpool_name_to_prop( 5108525SEric.Schrock@Sun.COM nvpair_name(elem))) == ZPROP_INVAL) 5118525SEric.Schrock@Sun.COM return (EINVAL); 5128525SEric.Schrock@Sun.COM 5138525SEric.Schrock@Sun.COM if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 5148525SEric.Schrock@Sun.COM continue; 5158525SEric.Schrock@Sun.COM 5168525SEric.Schrock@Sun.COM need_sync = B_TRUE; 5178525SEric.Schrock@Sun.COM break; 5188525SEric.Schrock@Sun.COM } 5198525SEric.Schrock@Sun.COM 5208525SEric.Schrock@Sun.COM if (need_sync) 5218525SEric.Schrock@Sun.COM return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 5228525SEric.Schrock@Sun.COM spa, nvp, 3)); 5238525SEric.Schrock@Sun.COM else 5248525SEric.Schrock@Sun.COM return (0); 5255094Slling } 5265094Slling 5275094Slling /* 5285094Slling * If the bootfs property value is dsobj, clear it. 5295094Slling */ 5305094Slling void 5315094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 5325094Slling { 5335094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 5345094Slling VERIFY(zap_remove(spa->spa_meta_objset, 5355094Slling spa->spa_pool_props_object, 5365094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 5375094Slling spa->spa_bootfs = 0; 5385094Slling } 5395094Slling } 5405094Slling 541789Sahrens /* 542789Sahrens * ========================================================================== 543789Sahrens * SPA state manipulation (open/create/destroy/import/export) 544789Sahrens * ========================================================================== 545789Sahrens */ 546789Sahrens 5471544Seschrock static int 5481544Seschrock spa_error_entry_compare(const void *a, const void *b) 5491544Seschrock { 5501544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 5511544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 5521544Seschrock int ret; 5531544Seschrock 5541544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 5551544Seschrock sizeof (zbookmark_t)); 5561544Seschrock 5571544Seschrock if (ret < 0) 5581544Seschrock return (-1); 5591544Seschrock else if (ret > 0) 5601544Seschrock return (1); 5611544Seschrock else 5621544Seschrock return (0); 5631544Seschrock } 5641544Seschrock 5651544Seschrock /* 5661544Seschrock * Utility function which retrieves copies of the current logs and 5671544Seschrock * re-initializes them in the process. 5681544Seschrock */ 5691544Seschrock void 5701544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 5711544Seschrock { 5721544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 5731544Seschrock 5741544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 5751544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 5761544Seschrock 5771544Seschrock avl_create(&spa->spa_errlist_scrub, 5781544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5791544Seschrock offsetof(spa_error_entry_t, se_avl)); 5801544Seschrock avl_create(&spa->spa_errlist_last, 5811544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5821544Seschrock offsetof(spa_error_entry_t, se_avl)); 5831544Seschrock } 5841544Seschrock 585789Sahrens /* 586789Sahrens * Activate an uninitialized pool. 587789Sahrens */ 588789Sahrens static void 5898241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode) 590789Sahrens { 591789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 592789Sahrens 593789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 5948241SJeff.Bonwick@Sun.COM spa->spa_mode = mode; 595789Sahrens 59610594SGeorge.Wilson@Sun.COM spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 59710594SGeorge.Wilson@Sun.COM spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 598789Sahrens 5997754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 6009515SJonathan.Adams@Sun.COM const zio_taskq_info_t *ztip = &zio_taskqs[t]; 6017754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 6029515SJonathan.Adams@Sun.COM enum zti_modes mode = ztip->zti_nthreads[q].zti_mode; 6039515SJonathan.Adams@Sun.COM uint_t value = ztip->zti_nthreads[q].zti_value; 6049515SJonathan.Adams@Sun.COM char name[32]; 6059515SJonathan.Adams@Sun.COM 6069515SJonathan.Adams@Sun.COM (void) snprintf(name, sizeof (name), 6079515SJonathan.Adams@Sun.COM "%s_%s", ztip->zti_name, zio_taskq_types[q]); 6089515SJonathan.Adams@Sun.COM 6099515SJonathan.Adams@Sun.COM if (mode == zti_mode_tune) { 6109515SJonathan.Adams@Sun.COM mode = zio_taskq_tune_mode; 6119515SJonathan.Adams@Sun.COM value = zio_taskq_tune_value; 6129515SJonathan.Adams@Sun.COM if (mode == zti_mode_tune) 6139515SJonathan.Adams@Sun.COM mode = zti_mode_online_percent; 6149515SJonathan.Adams@Sun.COM } 6159515SJonathan.Adams@Sun.COM 6169515SJonathan.Adams@Sun.COM switch (mode) { 6179515SJonathan.Adams@Sun.COM case zti_mode_fixed: 6189515SJonathan.Adams@Sun.COM ASSERT3U(value, >=, 1); 6199515SJonathan.Adams@Sun.COM value = MAX(value, 1); 6209515SJonathan.Adams@Sun.COM 6219515SJonathan.Adams@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create(name, 6229515SJonathan.Adams@Sun.COM value, maxclsyspri, 50, INT_MAX, 6239515SJonathan.Adams@Sun.COM TASKQ_PREPOPULATE); 6249515SJonathan.Adams@Sun.COM break; 6259515SJonathan.Adams@Sun.COM 6269515SJonathan.Adams@Sun.COM case zti_mode_online_percent: 6279515SJonathan.Adams@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create(name, 6289515SJonathan.Adams@Sun.COM value, maxclsyspri, 50, INT_MAX, 6299515SJonathan.Adams@Sun.COM TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); 6309515SJonathan.Adams@Sun.COM break; 6319515SJonathan.Adams@Sun.COM 6329515SJonathan.Adams@Sun.COM case zti_mode_tune: 6339515SJonathan.Adams@Sun.COM default: 6349515SJonathan.Adams@Sun.COM panic("unrecognized mode for " 6359515SJonathan.Adams@Sun.COM "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) " 6369515SJonathan.Adams@Sun.COM "in spa_activate()", 6379515SJonathan.Adams@Sun.COM t, q, mode, value); 6389515SJonathan.Adams@Sun.COM break; 6399515SJonathan.Adams@Sun.COM } 6407754SJeff.Bonwick@Sun.COM } 641789Sahrens } 642789Sahrens 6437754SJeff.Bonwick@Sun.COM list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 6447754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_config_dirty_node)); 6457754SJeff.Bonwick@Sun.COM list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 6467754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_state_dirty_node)); 647789Sahrens 648789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 649789Sahrens offsetof(struct vdev, vdev_txg_node)); 6501544Seschrock 6511544Seschrock avl_create(&spa->spa_errlist_scrub, 6521544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 6531544Seschrock offsetof(spa_error_entry_t, se_avl)); 6541544Seschrock avl_create(&spa->spa_errlist_last, 6551544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 6561544Seschrock offsetof(spa_error_entry_t, se_avl)); 657789Sahrens } 658789Sahrens 659789Sahrens /* 660789Sahrens * Opposite of spa_activate(). 661789Sahrens */ 662789Sahrens static void 663789Sahrens spa_deactivate(spa_t *spa) 664789Sahrens { 665789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 666789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 667789Sahrens ASSERT(spa->spa_root_vdev == NULL); 6689630SJeff.Bonwick@Sun.COM ASSERT(spa->spa_async_zio_root == NULL); 669789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 670789Sahrens 671789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 672789Sahrens 6737754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_config_dirty_list); 6747754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_state_dirty_list); 6757754SJeff.Bonwick@Sun.COM 6767754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 6777754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 6787754SJeff.Bonwick@Sun.COM taskq_destroy(spa->spa_zio_taskq[t][q]); 6797754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 6807754SJeff.Bonwick@Sun.COM } 681789Sahrens } 682789Sahrens 683789Sahrens metaslab_class_destroy(spa->spa_normal_class); 684789Sahrens spa->spa_normal_class = NULL; 685789Sahrens 6864527Sperrin metaslab_class_destroy(spa->spa_log_class); 6874527Sperrin spa->spa_log_class = NULL; 6884527Sperrin 6891544Seschrock /* 6901544Seschrock * If this was part of an import or the open otherwise failed, we may 6911544Seschrock * still have errors left in the queues. Empty them just in case. 6921544Seschrock */ 6931544Seschrock spa_errlog_drain(spa); 6941544Seschrock 6951544Seschrock avl_destroy(&spa->spa_errlist_scrub); 6961544Seschrock avl_destroy(&spa->spa_errlist_last); 6971544Seschrock 698789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 699789Sahrens } 700789Sahrens 701789Sahrens /* 702789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 703789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 704789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 705789Sahrens * All vdev validation is done by the vdev_alloc() routine. 706789Sahrens */ 7072082Seschrock static int 7082082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 7092082Seschrock uint_t id, int atype) 710789Sahrens { 711789Sahrens nvlist_t **child; 7129816SGeorge.Wilson@Sun.COM uint_t children; 7132082Seschrock int error; 7142082Seschrock 7152082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 7162082Seschrock return (error); 7172082Seschrock 7182082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 7192082Seschrock return (0); 720789Sahrens 7217754SJeff.Bonwick@Sun.COM error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 7227754SJeff.Bonwick@Sun.COM &child, &children); 7237754SJeff.Bonwick@Sun.COM 7247754SJeff.Bonwick@Sun.COM if (error == ENOENT) 7257754SJeff.Bonwick@Sun.COM return (0); 7267754SJeff.Bonwick@Sun.COM 7277754SJeff.Bonwick@Sun.COM if (error) { 7282082Seschrock vdev_free(*vdp); 7292082Seschrock *vdp = NULL; 7302082Seschrock return (EINVAL); 731789Sahrens } 732789Sahrens 7339816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 7342082Seschrock vdev_t *vd; 7352082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 7362082Seschrock atype)) != 0) { 7372082Seschrock vdev_free(*vdp); 7382082Seschrock *vdp = NULL; 7392082Seschrock return (error); 740789Sahrens } 741789Sahrens } 742789Sahrens 7432082Seschrock ASSERT(*vdp != NULL); 7442082Seschrock 7452082Seschrock return (0); 746789Sahrens } 747789Sahrens 748789Sahrens /* 749789Sahrens * Opposite of spa_load(). 750789Sahrens */ 751789Sahrens static void 752789Sahrens spa_unload(spa_t *spa) 753789Sahrens { 7542082Seschrock int i; 7552082Seschrock 7567754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 7577754SJeff.Bonwick@Sun.COM 758789Sahrens /* 7591544Seschrock * Stop async tasks. 7601544Seschrock */ 7611544Seschrock spa_async_suspend(spa); 7621544Seschrock 7631544Seschrock /* 764789Sahrens * Stop syncing. 765789Sahrens */ 766789Sahrens if (spa->spa_sync_on) { 767789Sahrens txg_sync_stop(spa->spa_dsl_pool); 768789Sahrens spa->spa_sync_on = B_FALSE; 769789Sahrens } 770789Sahrens 771789Sahrens /* 7727754SJeff.Bonwick@Sun.COM * Wait for any outstanding async I/O to complete. 773789Sahrens */ 7749234SGeorge.Wilson@Sun.COM if (spa->spa_async_zio_root != NULL) { 7759234SGeorge.Wilson@Sun.COM (void) zio_wait(spa->spa_async_zio_root); 7769234SGeorge.Wilson@Sun.COM spa->spa_async_zio_root = NULL; 7779234SGeorge.Wilson@Sun.COM } 778789Sahrens 779789Sahrens /* 780789Sahrens * Close the dsl pool. 781789Sahrens */ 782789Sahrens if (spa->spa_dsl_pool) { 783789Sahrens dsl_pool_close(spa->spa_dsl_pool); 784789Sahrens spa->spa_dsl_pool = NULL; 785789Sahrens } 786789Sahrens 78710922SJeff.Bonwick@Sun.COM ddt_unload(spa); 78810922SJeff.Bonwick@Sun.COM 7898241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7908241SJeff.Bonwick@Sun.COM 7918241SJeff.Bonwick@Sun.COM /* 7928241SJeff.Bonwick@Sun.COM * Drop and purge level 2 cache 7938241SJeff.Bonwick@Sun.COM */ 7948241SJeff.Bonwick@Sun.COM spa_l2cache_drop(spa); 7958241SJeff.Bonwick@Sun.COM 796789Sahrens /* 797789Sahrens * Close all vdevs. 798789Sahrens */ 7991585Sbonwick if (spa->spa_root_vdev) 800789Sahrens vdev_free(spa->spa_root_vdev); 8011585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 8021544Seschrock 8035450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 8045450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 8055450Sbrendan if (spa->spa_spares.sav_vdevs) { 8065450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 8075450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 8085450Sbrendan spa->spa_spares.sav_vdevs = NULL; 8095450Sbrendan } 8105450Sbrendan if (spa->spa_spares.sav_config) { 8115450Sbrendan nvlist_free(spa->spa_spares.sav_config); 8125450Sbrendan spa->spa_spares.sav_config = NULL; 8132082Seschrock } 8147377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 8155450Sbrendan 8165450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 8175450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 8185450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 8195450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 8205450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 8215450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 8225450Sbrendan } 8235450Sbrendan if (spa->spa_l2cache.sav_config) { 8245450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 8255450Sbrendan spa->spa_l2cache.sav_config = NULL; 8262082Seschrock } 8277377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 8282082Seschrock 8291544Seschrock spa->spa_async_suspended = 0; 8308241SJeff.Bonwick@Sun.COM 8318241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 832789Sahrens } 833789Sahrens 834789Sahrens /* 8352082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 8362082Seschrock * this pool. When this is called, we have some form of basic information in 8375450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 8385450Sbrendan * then re-generate a more complete list including status information. 8392082Seschrock */ 8402082Seschrock static void 8412082Seschrock spa_load_spares(spa_t *spa) 8422082Seschrock { 8432082Seschrock nvlist_t **spares; 8442082Seschrock uint_t nspares; 8452082Seschrock int i; 8463377Seschrock vdev_t *vd, *tvd; 8472082Seschrock 8487754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 8497754SJeff.Bonwick@Sun.COM 8502082Seschrock /* 8512082Seschrock * First, close and free any existing spare vdevs. 8522082Seschrock */ 8535450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 8545450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 8553377Seschrock 8563377Seschrock /* Undo the call to spa_activate() below */ 8576643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 8586643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 8593377Seschrock spa_spare_remove(tvd); 8603377Seschrock vdev_close(vd); 8613377Seschrock vdev_free(vd); 8622082Seschrock } 8633377Seschrock 8645450Sbrendan if (spa->spa_spares.sav_vdevs) 8655450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 8665450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 8675450Sbrendan 8685450Sbrendan if (spa->spa_spares.sav_config == NULL) 8692082Seschrock nspares = 0; 8702082Seschrock else 8715450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 8722082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 8732082Seschrock 8745450Sbrendan spa->spa_spares.sav_count = (int)nspares; 8755450Sbrendan spa->spa_spares.sav_vdevs = NULL; 8762082Seschrock 8772082Seschrock if (nspares == 0) 8782082Seschrock return; 8792082Seschrock 8802082Seschrock /* 8812082Seschrock * Construct the array of vdevs, opening them to get status in the 8823377Seschrock * process. For each spare, there is potentially two different vdev_t 8833377Seschrock * structures associated with it: one in the list of spares (used only 8843377Seschrock * for basic validation purposes) and one in the active vdev 8853377Seschrock * configuration (if it's spared in). During this phase we open and 8863377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 8873377Seschrock * active configuration, then we also mark this vdev as an active spare. 8882082Seschrock */ 8895450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 8905450Sbrendan KM_SLEEP); 8915450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 8922082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 8932082Seschrock VDEV_ALLOC_SPARE) == 0); 8942082Seschrock ASSERT(vd != NULL); 8952082Seschrock 8965450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 8972082Seschrock 8986643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 8996643Seschrock B_FALSE)) != NULL) { 9003377Seschrock if (!tvd->vdev_isspare) 9013377Seschrock spa_spare_add(tvd); 9023377Seschrock 9033377Seschrock /* 9043377Seschrock * We only mark the spare active if we were successfully 9053377Seschrock * able to load the vdev. Otherwise, importing a pool 9063377Seschrock * with a bad active spare would result in strange 9073377Seschrock * behavior, because multiple pool would think the spare 9083377Seschrock * is actively in use. 9093377Seschrock * 9103377Seschrock * There is a vulnerability here to an equally bizarre 9113377Seschrock * circumstance, where a dead active spare is later 9123377Seschrock * brought back to life (onlined or otherwise). Given 9133377Seschrock * the rarity of this scenario, and the extra complexity 9143377Seschrock * it adds, we ignore the possibility. 9153377Seschrock */ 9163377Seschrock if (!vdev_is_dead(tvd)) 9173377Seschrock spa_spare_activate(tvd); 9183377Seschrock } 9193377Seschrock 9207754SJeff.Bonwick@Sun.COM vd->vdev_top = vd; 9219425SEric.Schrock@Sun.COM vd->vdev_aux = &spa->spa_spares; 9227754SJeff.Bonwick@Sun.COM 9232082Seschrock if (vdev_open(vd) != 0) 9242082Seschrock continue; 9252082Seschrock 9265450Sbrendan if (vdev_validate_aux(vd) == 0) 9275450Sbrendan spa_spare_add(vd); 9282082Seschrock } 9292082Seschrock 9302082Seschrock /* 9312082Seschrock * Recompute the stashed list of spares, with status information 9322082Seschrock * this time. 9332082Seschrock */ 9345450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 9352082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 9362082Seschrock 9375450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 9385450Sbrendan KM_SLEEP); 9395450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 9405450Sbrendan spares[i] = vdev_config_generate(spa, 9415450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 9425450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 9435450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 9445450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 9452082Seschrock nvlist_free(spares[i]); 9465450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 9475450Sbrendan } 9485450Sbrendan 9495450Sbrendan /* 9505450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 9515450Sbrendan * this pool. When this is called, we have some form of basic information in 9525450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 9535450Sbrendan * then re-generate a more complete list including status information. 9545450Sbrendan * Devices which are already active have their details maintained, and are 9555450Sbrendan * not re-opened. 9565450Sbrendan */ 9575450Sbrendan static void 9585450Sbrendan spa_load_l2cache(spa_t *spa) 9595450Sbrendan { 9605450Sbrendan nvlist_t **l2cache; 9615450Sbrendan uint_t nl2cache; 9625450Sbrendan int i, j, oldnvdevs; 9639816SGeorge.Wilson@Sun.COM uint64_t guid; 9645450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 9655450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 9665450Sbrendan 9677754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 9687754SJeff.Bonwick@Sun.COM 9695450Sbrendan if (sav->sav_config != NULL) { 9705450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 9715450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 9725450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 9735450Sbrendan } else { 9745450Sbrendan nl2cache = 0; 9755450Sbrendan } 9765450Sbrendan 9775450Sbrendan oldvdevs = sav->sav_vdevs; 9785450Sbrendan oldnvdevs = sav->sav_count; 9795450Sbrendan sav->sav_vdevs = NULL; 9805450Sbrendan sav->sav_count = 0; 9815450Sbrendan 9825450Sbrendan /* 9835450Sbrendan * Process new nvlist of vdevs. 9845450Sbrendan */ 9855450Sbrendan for (i = 0; i < nl2cache; i++) { 9865450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 9875450Sbrendan &guid) == 0); 9885450Sbrendan 9895450Sbrendan newvdevs[i] = NULL; 9905450Sbrendan for (j = 0; j < oldnvdevs; j++) { 9915450Sbrendan vd = oldvdevs[j]; 9925450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 9935450Sbrendan /* 9945450Sbrendan * Retain previous vdev for add/remove ops. 9955450Sbrendan */ 9965450Sbrendan newvdevs[i] = vd; 9975450Sbrendan oldvdevs[j] = NULL; 9985450Sbrendan break; 9995450Sbrendan } 10005450Sbrendan } 10015450Sbrendan 10025450Sbrendan if (newvdevs[i] == NULL) { 10035450Sbrendan /* 10045450Sbrendan * Create new vdev 10055450Sbrendan */ 10065450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 10075450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 10085450Sbrendan ASSERT(vd != NULL); 10095450Sbrendan newvdevs[i] = vd; 10105450Sbrendan 10115450Sbrendan /* 10125450Sbrendan * Commit this vdev as an l2cache device, 10135450Sbrendan * even if it fails to open. 10145450Sbrendan */ 10155450Sbrendan spa_l2cache_add(vd); 10165450Sbrendan 10176643Seschrock vd->vdev_top = vd; 10186643Seschrock vd->vdev_aux = sav; 10196643Seschrock 10206643Seschrock spa_l2cache_activate(vd); 10216643Seschrock 10225450Sbrendan if (vdev_open(vd) != 0) 10235450Sbrendan continue; 10245450Sbrendan 10255450Sbrendan (void) vdev_validate_aux(vd); 10265450Sbrendan 10279816SGeorge.Wilson@Sun.COM if (!vdev_is_dead(vd)) 10289816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa, vd); 10295450Sbrendan } 10305450Sbrendan } 10315450Sbrendan 10325450Sbrendan /* 10335450Sbrendan * Purge vdevs that were dropped 10345450Sbrendan */ 10355450Sbrendan for (i = 0; i < oldnvdevs; i++) { 10365450Sbrendan uint64_t pool; 10375450Sbrendan 10385450Sbrendan vd = oldvdevs[i]; 10395450Sbrendan if (vd != NULL) { 10408241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 10418241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 10425450Sbrendan l2arc_remove_vdev(vd); 10435450Sbrendan (void) vdev_close(vd); 10445450Sbrendan spa_l2cache_remove(vd); 10455450Sbrendan } 10465450Sbrendan } 10475450Sbrendan 10485450Sbrendan if (oldvdevs) 10495450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 10505450Sbrendan 10515450Sbrendan if (sav->sav_config == NULL) 10525450Sbrendan goto out; 10535450Sbrendan 10545450Sbrendan sav->sav_vdevs = newvdevs; 10555450Sbrendan sav->sav_count = (int)nl2cache; 10565450Sbrendan 10575450Sbrendan /* 10585450Sbrendan * Recompute the stashed list of l2cache devices, with status 10595450Sbrendan * information this time. 10605450Sbrendan */ 10615450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 10625450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 10635450Sbrendan 10645450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 10655450Sbrendan for (i = 0; i < sav->sav_count; i++) 10665450Sbrendan l2cache[i] = vdev_config_generate(spa, 10675450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 10685450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 10695450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 10705450Sbrendan out: 10715450Sbrendan for (i = 0; i < sav->sav_count; i++) 10725450Sbrendan nvlist_free(l2cache[i]); 10735450Sbrendan if (sav->sav_count) 10745450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 10752082Seschrock } 10762082Seschrock 10772082Seschrock static int 10782082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 10792082Seschrock { 10802082Seschrock dmu_buf_t *db; 10812082Seschrock char *packed = NULL; 10822082Seschrock size_t nvsize = 0; 10832082Seschrock int error; 10842082Seschrock *value = NULL; 10852082Seschrock 10862082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 10872082Seschrock nvsize = *(uint64_t *)db->db_data; 10882082Seschrock dmu_buf_rele(db, FTAG); 10892082Seschrock 10902082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 10919512SNeil.Perrin@Sun.COM error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 10929512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 10932082Seschrock if (error == 0) 10942082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 10952082Seschrock kmem_free(packed, nvsize); 10962082Seschrock 10972082Seschrock return (error); 10982082Seschrock } 10992082Seschrock 11002082Seschrock /* 11014451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 11024451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 11034451Seschrock */ 11044451Seschrock static void 11054451Seschrock spa_check_removed(vdev_t *vd) 11064451Seschrock { 11079816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 11084451Seschrock spa_check_removed(vd->vdev_child[c]); 11094451Seschrock 11104451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 11114451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 11124451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 11134451Seschrock } 11144451Seschrock } 11154451Seschrock 11164451Seschrock /* 11179701SGeorge.Wilson@Sun.COM * Load the slog device state from the config object since it's possible 11189701SGeorge.Wilson@Sun.COM * that the label does not contain the most up-to-date information. 11199701SGeorge.Wilson@Sun.COM */ 11209701SGeorge.Wilson@Sun.COM void 112110594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv) 11229701SGeorge.Wilson@Sun.COM { 112310594SGeorge.Wilson@Sun.COM vdev_t *ovd, *rvd = spa->spa_root_vdev; 112410594SGeorge.Wilson@Sun.COM 112510594SGeorge.Wilson@Sun.COM /* 112610594SGeorge.Wilson@Sun.COM * Load the original root vdev tree from the passed config. 112710594SGeorge.Wilson@Sun.COM */ 112810594SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 112910594SGeorge.Wilson@Sun.COM VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 113010594SGeorge.Wilson@Sun.COM 113110594SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 113210594SGeorge.Wilson@Sun.COM vdev_t *cvd = rvd->vdev_child[c]; 113310594SGeorge.Wilson@Sun.COM if (cvd->vdev_islog) 113410594SGeorge.Wilson@Sun.COM vdev_load_log_state(cvd, ovd->vdev_child[c]); 11359701SGeorge.Wilson@Sun.COM } 113610594SGeorge.Wilson@Sun.COM vdev_free(ovd); 113710594SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 11389701SGeorge.Wilson@Sun.COM } 11399701SGeorge.Wilson@Sun.COM 11409701SGeorge.Wilson@Sun.COM /* 11417294Sperrin * Check for missing log devices 11427294Sperrin */ 11437294Sperrin int 11447294Sperrin spa_check_logs(spa_t *spa) 11457294Sperrin { 11467294Sperrin switch (spa->spa_log_state) { 11477294Sperrin case SPA_LOG_MISSING: 11487294Sperrin /* need to recheck in case slog has been restored */ 11497294Sperrin case SPA_LOG_UNKNOWN: 11507294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 11517294Sperrin DS_FIND_CHILDREN)) { 11527294Sperrin spa->spa_log_state = SPA_LOG_MISSING; 11537294Sperrin return (1); 11547294Sperrin } 11557294Sperrin break; 11567294Sperrin } 11577294Sperrin return (0); 11587294Sperrin } 11597294Sperrin 116010672SEric.Schrock@Sun.COM static void 116110672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav) 116210672SEric.Schrock@Sun.COM { 116310922SJeff.Bonwick@Sun.COM for (int i = 0; i < sav->sav_count; i++) 116410672SEric.Schrock@Sun.COM spa_check_removed(sav->sav_vdevs[i]); 116510672SEric.Schrock@Sun.COM } 116610672SEric.Schrock@Sun.COM 116710922SJeff.Bonwick@Sun.COM void 116810922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio) 116910922SJeff.Bonwick@Sun.COM { 117010922SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 117110922SJeff.Bonwick@Sun.COM 117210922SJeff.Bonwick@Sun.COM if (zio->io_error) 117310922SJeff.Bonwick@Sun.COM return; 117410922SJeff.Bonwick@Sun.COM 117510922SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 117610922SJeff.Bonwick@Sun.COM if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 117710922SJeff.Bonwick@Sun.COM spa->spa_claim_max_txg = zio->io_bp->blk_birth; 117810922SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 117910922SJeff.Bonwick@Sun.COM } 118010922SJeff.Bonwick@Sun.COM 118110921STim.Haley@Sun.COM typedef struct spa_load_error { 118210921STim.Haley@Sun.COM uint64_t sle_metadata_count; 118310921STim.Haley@Sun.COM uint64_t sle_data_count; 118410921STim.Haley@Sun.COM } spa_load_error_t; 118510921STim.Haley@Sun.COM 118610921STim.Haley@Sun.COM static void 118710921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio) 118810921STim.Haley@Sun.COM { 118910921STim.Haley@Sun.COM blkptr_t *bp = zio->io_bp; 119010921STim.Haley@Sun.COM spa_load_error_t *sle = zio->io_private; 119110921STim.Haley@Sun.COM dmu_object_type_t type = BP_GET_TYPE(bp); 119210921STim.Haley@Sun.COM int error = zio->io_error; 119310921STim.Haley@Sun.COM 119410921STim.Haley@Sun.COM if (error) { 119510921STim.Haley@Sun.COM if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) && 119610921STim.Haley@Sun.COM type != DMU_OT_INTENT_LOG) 119710921STim.Haley@Sun.COM atomic_add_64(&sle->sle_metadata_count, 1); 119810921STim.Haley@Sun.COM else 119910921STim.Haley@Sun.COM atomic_add_64(&sle->sle_data_count, 1); 120010921STim.Haley@Sun.COM } 120110921STim.Haley@Sun.COM zio_data_buf_free(zio->io_data, zio->io_size); 120210921STim.Haley@Sun.COM } 120310921STim.Haley@Sun.COM 120410921STim.Haley@Sun.COM /*ARGSUSED*/ 120510921STim.Haley@Sun.COM static int 120610922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 120710922SJeff.Bonwick@Sun.COM const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 120810921STim.Haley@Sun.COM { 120910921STim.Haley@Sun.COM if (bp != NULL) { 121010921STim.Haley@Sun.COM zio_t *rio = arg; 121110921STim.Haley@Sun.COM size_t size = BP_GET_PSIZE(bp); 121210921STim.Haley@Sun.COM void *data = zio_data_buf_alloc(size); 121310921STim.Haley@Sun.COM 121410921STim.Haley@Sun.COM zio_nowait(zio_read(rio, spa, bp, data, size, 121510921STim.Haley@Sun.COM spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 121610921STim.Haley@Sun.COM ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 121710921STim.Haley@Sun.COM ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 121810921STim.Haley@Sun.COM } 121910921STim.Haley@Sun.COM return (0); 122010921STim.Haley@Sun.COM } 122110921STim.Haley@Sun.COM 122210921STim.Haley@Sun.COM static int 122310921STim.Haley@Sun.COM spa_load_verify(spa_t *spa) 122410921STim.Haley@Sun.COM { 122510921STim.Haley@Sun.COM zio_t *rio; 122610921STim.Haley@Sun.COM spa_load_error_t sle = { 0 }; 122710921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 122810921STim.Haley@Sun.COM boolean_t verify_ok = B_FALSE; 122910921STim.Haley@Sun.COM int error; 123010921STim.Haley@Sun.COM 123110921STim.Haley@Sun.COM rio = zio_root(spa, NULL, &sle, 123210921STim.Haley@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 123310921STim.Haley@Sun.COM 123410921STim.Haley@Sun.COM error = traverse_pool(spa, spa_load_verify_cb, rio, 123510921STim.Haley@Sun.COM spa->spa_verify_min_txg); 123610921STim.Haley@Sun.COM 123710921STim.Haley@Sun.COM (void) zio_wait(rio); 123810921STim.Haley@Sun.COM 123910921STim.Haley@Sun.COM zpool_get_rewind_policy(spa->spa_config, &policy); 124010921STim.Haley@Sun.COM 124110921STim.Haley@Sun.COM spa->spa_load_meta_errors = sle.sle_metadata_count; 124210921STim.Haley@Sun.COM spa->spa_load_data_errors = sle.sle_data_count; 124310921STim.Haley@Sun.COM 124410921STim.Haley@Sun.COM if (!error && sle.sle_metadata_count <= policy.zrp_maxmeta && 124510921STim.Haley@Sun.COM sle.sle_data_count <= policy.zrp_maxdata) { 124610921STim.Haley@Sun.COM verify_ok = B_TRUE; 124710921STim.Haley@Sun.COM spa->spa_load_txg = spa->spa_uberblock.ub_txg; 124810921STim.Haley@Sun.COM spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 124910921STim.Haley@Sun.COM } 125010921STim.Haley@Sun.COM 125110921STim.Haley@Sun.COM if (error) { 125210921STim.Haley@Sun.COM if (error != ENXIO && error != EIO) 125310921STim.Haley@Sun.COM error = EIO; 125410921STim.Haley@Sun.COM return (error); 125510921STim.Haley@Sun.COM } 125610921STim.Haley@Sun.COM 125710921STim.Haley@Sun.COM return (verify_ok ? 0 : EIO); 125810921STim.Haley@Sun.COM } 125910921STim.Haley@Sun.COM 12607294Sperrin /* 1261789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 12621544Seschrock * source of configuration information. 1263789Sahrens */ 1264789Sahrens static int 126510921STim.Haley@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, int mosconfig) 1266789Sahrens { 1267789Sahrens int error = 0; 126810594SGeorge.Wilson@Sun.COM nvlist_t *nvconfig, *nvroot = NULL; 1269789Sahrens vdev_t *rvd; 1270789Sahrens uberblock_t *ub = &spa->spa_uberblock; 12711635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 1272789Sahrens uint64_t pool_guid; 12732082Seschrock uint64_t version; 12744451Seschrock uint64_t autoreplace = 0; 12758241SJeff.Bonwick@Sun.COM int orig_mode = spa->spa_mode; 12767294Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 127710921STim.Haley@Sun.COM nvlist_t *config = spa->spa_config; 1278789Sahrens 12798241SJeff.Bonwick@Sun.COM /* 12808241SJeff.Bonwick@Sun.COM * If this is an untrusted config, access the pool in read-only mode. 12818241SJeff.Bonwick@Sun.COM * This prevents things like resilvering recently removed devices. 12828241SJeff.Bonwick@Sun.COM */ 12838241SJeff.Bonwick@Sun.COM if (!mosconfig) 12848241SJeff.Bonwick@Sun.COM spa->spa_mode = FREAD; 12858241SJeff.Bonwick@Sun.COM 12867754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 12877754SJeff.Bonwick@Sun.COM 12881544Seschrock spa->spa_load_state = state; 12891635Sbonwick 1290789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 12911733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 12921544Seschrock error = EINVAL; 12931544Seschrock goto out; 12941544Seschrock } 1295789Sahrens 12962082Seschrock /* 12972082Seschrock * Versioning wasn't explicitly added to the label until later, so if 12982082Seschrock * it's not present treat it as the initial version. 12992082Seschrock */ 13002082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 13014577Sahrens version = SPA_VERSION_INITIAL; 13022082Seschrock 13031733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 13041733Sbonwick &spa->spa_config_txg); 13051733Sbonwick 13061635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 13071544Seschrock spa_guid_exists(pool_guid, 0)) { 13081544Seschrock error = EEXIST; 13091544Seschrock goto out; 13101544Seschrock } 1311789Sahrens 13122174Seschrock spa->spa_load_guid = pool_guid; 13132174Seschrock 1314789Sahrens /* 13159234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 13169234SGeorge.Wilson@Sun.COM */ 13179630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 13189630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 13199234SGeorge.Wilson@Sun.COM 13209234SGeorge.Wilson@Sun.COM /* 13212082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 13222082Seschrock * value that will be returned by spa_version() since parsing the 13232082Seschrock * configuration requires knowing the version number. 1324789Sahrens */ 13257754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13262082Seschrock spa->spa_ubsync.ub_version = version; 13272082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 13287754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1329789Sahrens 13302082Seschrock if (error != 0) 13311544Seschrock goto out; 1332789Sahrens 13331585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1334789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1335789Sahrens 1336789Sahrens /* 1337789Sahrens * Try to open all vdevs, loading each label in the process. 1338789Sahrens */ 13397754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13404070Smc142369 error = vdev_open(rvd); 13417754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13424070Smc142369 if (error != 0) 13431544Seschrock goto out; 1344789Sahrens 1345789Sahrens /* 13469276SMark.Musante@Sun.COM * We need to validate the vdev labels against the configuration that 13479276SMark.Musante@Sun.COM * we have in hand, which is dependent on the setting of mosconfig. If 13489276SMark.Musante@Sun.COM * mosconfig is true then we're validating the vdev labels based on 13499276SMark.Musante@Sun.COM * that config. Otherwise, we're validating against the cached config 13509276SMark.Musante@Sun.COM * (zpool.cache) that was read when we loaded the zfs module, and then 13519276SMark.Musante@Sun.COM * later we will recursively call spa_load() and validate against 13529276SMark.Musante@Sun.COM * the vdev config. 13531986Seschrock */ 13549276SMark.Musante@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13559276SMark.Musante@Sun.COM error = vdev_validate(rvd); 13569276SMark.Musante@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13579276SMark.Musante@Sun.COM if (error != 0) 13589276SMark.Musante@Sun.COM goto out; 13591986Seschrock 13601986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13611986Seschrock error = ENXIO; 13621986Seschrock goto out; 13631986Seschrock } 13641986Seschrock 13651986Seschrock /* 1366789Sahrens * Find the best uberblock. 1367789Sahrens */ 13687754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1369789Sahrens 1370789Sahrens /* 1371789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1372789Sahrens */ 1373789Sahrens if (ub->ub_txg == 0) { 13741760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13751760Seschrock VDEV_AUX_CORRUPT_DATA); 13761544Seschrock error = ENXIO; 13771544Seschrock goto out; 13781544Seschrock } 13791544Seschrock 13801544Seschrock /* 13811544Seschrock * If the pool is newer than the code, we can't open it. 13821544Seschrock */ 13834577Sahrens if (ub->ub_version > SPA_VERSION) { 13841760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13851760Seschrock VDEV_AUX_VERSION_NEWER); 13861544Seschrock error = ENOTSUP; 13871544Seschrock goto out; 1388789Sahrens } 1389789Sahrens 1390789Sahrens /* 1391789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1392789Sahrens * incomplete configuration. 1393789Sahrens */ 13941732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 13951544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13961544Seschrock VDEV_AUX_BAD_GUID_SUM); 13971544Seschrock error = ENXIO; 13981544Seschrock goto out; 1399789Sahrens } 1400789Sahrens 1401789Sahrens /* 1402789Sahrens * Initialize internal SPA structures. 1403789Sahrens */ 1404789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1405789Sahrens spa->spa_ubsync = spa->spa_uberblock; 140610921STim.Haley@Sun.COM spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 140710921STim.Haley@Sun.COM TXG_INITIAL : spa_last_synced_txg(spa) - TXG_DEFER_SIZE; 140810921STim.Haley@Sun.COM spa->spa_first_txg = spa->spa_last_ubsync_txg ? 140910921STim.Haley@Sun.COM spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 141010922SJeff.Bonwick@Sun.COM spa->spa_claim_max_txg = spa->spa_first_txg; 141110922SJeff.Bonwick@Sun.COM 14121544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 14131544Seschrock if (error) { 14141544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14151544Seschrock VDEV_AUX_CORRUPT_DATA); 141610921STim.Haley@Sun.COM error = EIO; 14171544Seschrock goto out; 14181544Seschrock } 1419789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1420789Sahrens 14211544Seschrock if (zap_lookup(spa->spa_meta_objset, 1422789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 14231544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 14241544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14251544Seschrock VDEV_AUX_CORRUPT_DATA); 14261544Seschrock error = EIO; 14271544Seschrock goto out; 14281544Seschrock } 1429789Sahrens 143010594SGeorge.Wilson@Sun.COM if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) { 143110594SGeorge.Wilson@Sun.COM vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 143210594SGeorge.Wilson@Sun.COM VDEV_AUX_CORRUPT_DATA); 143310594SGeorge.Wilson@Sun.COM error = EIO; 143410594SGeorge.Wilson@Sun.COM goto out; 143510594SGeorge.Wilson@Sun.COM } 143610594SGeorge.Wilson@Sun.COM 1437789Sahrens if (!mosconfig) { 14383975Sek110237 uint64_t hostid; 14392082Seschrock 144010594SGeorge.Wilson@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 14417706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 14423975Sek110237 char *hostname; 14433975Sek110237 unsigned long myhostid = 0; 14443975Sek110237 144510594SGeorge.Wilson@Sun.COM VERIFY(nvlist_lookup_string(nvconfig, 14463975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 14473975Sek110237 14488662SJordan.Vaughan@Sun.com #ifdef _KERNEL 14498662SJordan.Vaughan@Sun.com myhostid = zone_get_hostid(NULL); 14508662SJordan.Vaughan@Sun.com #else /* _KERNEL */ 14518662SJordan.Vaughan@Sun.com /* 14528662SJordan.Vaughan@Sun.com * We're emulating the system's hostid in userland, so 14538662SJordan.Vaughan@Sun.com * we can't use zone_get_hostid(). 14548662SJordan.Vaughan@Sun.com */ 14553975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 14568662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 14574178Slling if (hostid != 0 && myhostid != 0 && 14588662SJordan.Vaughan@Sun.com hostid != myhostid) { 14593975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 14603975Sek110237 "loaded as it was last accessed by " 14617706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 14623975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 14637754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 14643975Sek110237 (unsigned long)hostid); 14653975Sek110237 error = EBADF; 14663975Sek110237 goto out; 14673975Sek110237 } 14683975Sek110237 } 14693975Sek110237 147010594SGeorge.Wilson@Sun.COM spa_config_set(spa, nvconfig); 1471789Sahrens spa_unload(spa); 1472789Sahrens spa_deactivate(spa); 14738241SJeff.Bonwick@Sun.COM spa_activate(spa, orig_mode); 1474789Sahrens 147510921STim.Haley@Sun.COM return (spa_load(spa, state, B_TRUE)); 14761544Seschrock } 14771544Seschrock 14781544Seschrock if (zap_lookup(spa->spa_meta_objset, 14791544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 148010922SJeff.Bonwick@Sun.COM sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj) != 0) { 14811544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14821544Seschrock VDEV_AUX_CORRUPT_DATA); 14831544Seschrock error = EIO; 14841544Seschrock goto out; 1485789Sahrens } 1486789Sahrens 14871544Seschrock /* 14882082Seschrock * Load the bit that tells us to use the new accounting function 14892082Seschrock * (raid-z deflation). If we have an older pool, this will not 14902082Seschrock * be present. 14912082Seschrock */ 14922082Seschrock error = zap_lookup(spa->spa_meta_objset, 14932082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 14942082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 14952082Seschrock if (error != 0 && error != ENOENT) { 14962082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14972082Seschrock VDEV_AUX_CORRUPT_DATA); 14982082Seschrock error = EIO; 14992082Seschrock goto out; 15002082Seschrock } 15012082Seschrock 15022082Seschrock /* 15031544Seschrock * Load the persistent error log. If we have an older pool, this will 15041544Seschrock * not be present. 15051544Seschrock */ 15061544Seschrock error = zap_lookup(spa->spa_meta_objset, 15071544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 15081544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 15091807Sbonwick if (error != 0 && error != ENOENT) { 15101544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15111544Seschrock VDEV_AUX_CORRUPT_DATA); 15121544Seschrock error = EIO; 15131544Seschrock goto out; 15141544Seschrock } 15151544Seschrock 15161544Seschrock error = zap_lookup(spa->spa_meta_objset, 15171544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 15181544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 15191544Seschrock if (error != 0 && error != ENOENT) { 15201544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15211544Seschrock VDEV_AUX_CORRUPT_DATA); 15221544Seschrock error = EIO; 15231544Seschrock goto out; 15241544Seschrock } 1525789Sahrens 1526789Sahrens /* 15272926Sek110237 * Load the history object. If we have an older pool, this 15282926Sek110237 * will not be present. 15292926Sek110237 */ 15302926Sek110237 error = zap_lookup(spa->spa_meta_objset, 15312926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 15322926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 15332926Sek110237 if (error != 0 && error != ENOENT) { 15342926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15352926Sek110237 VDEV_AUX_CORRUPT_DATA); 15362926Sek110237 error = EIO; 15372926Sek110237 goto out; 15382926Sek110237 } 15392926Sek110237 15402926Sek110237 /* 15412082Seschrock * Load any hot spares for this pool. 15422082Seschrock */ 15432082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 15445450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 15452082Seschrock if (error != 0 && error != ENOENT) { 15462082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15472082Seschrock VDEV_AUX_CORRUPT_DATA); 15482082Seschrock error = EIO; 15492082Seschrock goto out; 15502082Seschrock } 15512082Seschrock if (error == 0) { 15524577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 15535450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 15545450Sbrendan &spa->spa_spares.sav_config) != 0) { 15552082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15562082Seschrock VDEV_AUX_CORRUPT_DATA); 15572082Seschrock error = EIO; 15582082Seschrock goto out; 15592082Seschrock } 15602082Seschrock 15617754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 15622082Seschrock spa_load_spares(spa); 15637754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 15642082Seschrock } 15652082Seschrock 15665450Sbrendan /* 15675450Sbrendan * Load any level 2 ARC devices for this pool. 15685450Sbrendan */ 15695450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 15705450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 15715450Sbrendan &spa->spa_l2cache.sav_object); 15725450Sbrendan if (error != 0 && error != ENOENT) { 15735450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15745450Sbrendan VDEV_AUX_CORRUPT_DATA); 15755450Sbrendan error = EIO; 15765450Sbrendan goto out; 15775450Sbrendan } 15785450Sbrendan if (error == 0) { 15795450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 15805450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 15815450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 15825450Sbrendan vdev_set_state(rvd, B_TRUE, 15835450Sbrendan VDEV_STATE_CANT_OPEN, 15845450Sbrendan VDEV_AUX_CORRUPT_DATA); 15855450Sbrendan error = EIO; 15865450Sbrendan goto out; 15875450Sbrendan } 15885450Sbrendan 15897754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 15905450Sbrendan spa_load_l2cache(spa); 15917754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 15925450Sbrendan } 15935450Sbrendan 15945094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 15954543Smarks 15963912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 15973912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 15983912Slling 15993912Slling if (error && error != ENOENT) { 16003912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 16013912Slling VDEV_AUX_CORRUPT_DATA); 16023912Slling error = EIO; 16033912Slling goto out; 16043912Slling } 16053912Slling 16063912Slling if (error == 0) { 16073912Slling (void) zap_lookup(spa->spa_meta_objset, 16083912Slling spa->spa_pool_props_object, 16094451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 16103912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 16114451Seschrock (void) zap_lookup(spa->spa_meta_objset, 16124451Seschrock spa->spa_pool_props_object, 16134451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 16144451Seschrock sizeof (uint64_t), 1, &autoreplace); 161510672SEric.Schrock@Sun.COM spa->spa_autoreplace = (autoreplace != 0); 16164543Smarks (void) zap_lookup(spa->spa_meta_objset, 16174543Smarks spa->spa_pool_props_object, 16184543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 16194543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 16205329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 16215329Sgw25295 spa->spa_pool_props_object, 16225329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 16235329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 16249816SGeorge.Wilson@Sun.COM (void) zap_lookup(spa->spa_meta_objset, 16259816SGeorge.Wilson@Sun.COM spa->spa_pool_props_object, 16269816SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND), 16279816SGeorge.Wilson@Sun.COM sizeof (uint64_t), 1, &spa->spa_autoexpand); 162810922SJeff.Bonwick@Sun.COM (void) zap_lookup(spa->spa_meta_objset, 162910922SJeff.Bonwick@Sun.COM spa->spa_pool_props_object, 163010922SJeff.Bonwick@Sun.COM zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO), 163110922SJeff.Bonwick@Sun.COM sizeof (uint64_t), 1, &spa->spa_dedup_ditto); 16323912Slling } 16333912Slling 16342082Seschrock /* 16354451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 16364451Seschrock * the ZFS DE that it should not issue any faults for unopenable 16374451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 16384451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 16394451Seschrock * over. 16404451Seschrock */ 164110672SEric.Schrock@Sun.COM if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 16424451Seschrock spa_check_removed(spa->spa_root_vdev); 164310672SEric.Schrock@Sun.COM /* 164410672SEric.Schrock@Sun.COM * For the import case, this is done in spa_import(), because 164510672SEric.Schrock@Sun.COM * at this point we're using the spare definitions from 164610672SEric.Schrock@Sun.COM * the MOS config, not necessarily from the userland config. 164710672SEric.Schrock@Sun.COM */ 164810672SEric.Schrock@Sun.COM if (state != SPA_LOAD_IMPORT) { 164910672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 165010672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 165110672SEric.Schrock@Sun.COM } 165210672SEric.Schrock@Sun.COM } 16534451Seschrock 16544451Seschrock /* 16551986Seschrock * Load the vdev state for all toplevel vdevs. 1656789Sahrens */ 16571986Seschrock vdev_load(rvd); 1658789Sahrens 1659789Sahrens /* 1660789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1661789Sahrens */ 16627754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1663789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 16647754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1665789Sahrens 1666789Sahrens /* 1667789Sahrens * Check the state of the root vdev. If it can't be opened, it 1668789Sahrens * indicates one or more toplevel vdevs are faulted. 1669789Sahrens */ 16701544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 16711544Seschrock error = ENXIO; 16721544Seschrock goto out; 16731544Seschrock } 1674789Sahrens 167510922SJeff.Bonwick@Sun.COM /* 167610922SJeff.Bonwick@Sun.COM * Load the DDTs (dedup tables). 167710922SJeff.Bonwick@Sun.COM */ 167810922SJeff.Bonwick@Sun.COM error = ddt_load(spa); 167910922SJeff.Bonwick@Sun.COM if (error != 0) { 168010922SJeff.Bonwick@Sun.COM vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 168110922SJeff.Bonwick@Sun.COM VDEV_AUX_CORRUPT_DATA); 168210922SJeff.Bonwick@Sun.COM error = EIO; 168310922SJeff.Bonwick@Sun.COM goto out; 168410922SJeff.Bonwick@Sun.COM } 168510922SJeff.Bonwick@Sun.COM 168610956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 168710956SGeorge.Wilson@Sun.COM 168810921STim.Haley@Sun.COM if (state != SPA_LOAD_TRYIMPORT) { 168910921STim.Haley@Sun.COM error = spa_load_verify(spa); 169010921STim.Haley@Sun.COM if (error) { 169110921STim.Haley@Sun.COM vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 169210921STim.Haley@Sun.COM VDEV_AUX_CORRUPT_DATA); 169310921STim.Haley@Sun.COM goto out; 169410921STim.Haley@Sun.COM } 169510921STim.Haley@Sun.COM } 169610921STim.Haley@Sun.COM 169710922SJeff.Bonwick@Sun.COM /* 169810922SJeff.Bonwick@Sun.COM * Load the intent log state and check log integrity. 169910922SJeff.Bonwick@Sun.COM */ 170010922SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE, 170110922SJeff.Bonwick@Sun.COM &nvroot) == 0); 170210922SJeff.Bonwick@Sun.COM spa_load_log_state(spa, nvroot); 170310922SJeff.Bonwick@Sun.COM nvlist_free(nvconfig); 170410922SJeff.Bonwick@Sun.COM 170510922SJeff.Bonwick@Sun.COM if (spa_check_logs(spa)) { 170610922SJeff.Bonwick@Sun.COM vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 170710922SJeff.Bonwick@Sun.COM VDEV_AUX_BAD_LOG); 170810922SJeff.Bonwick@Sun.COM error = ENXIO; 170910922SJeff.Bonwick@Sun.COM ereport = FM_EREPORT_ZFS_LOG_REPLAY; 171010922SJeff.Bonwick@Sun.COM goto out; 171110922SJeff.Bonwick@Sun.COM } 171210922SJeff.Bonwick@Sun.COM 171310921STim.Haley@Sun.COM if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 171410921STim.Haley@Sun.COM spa->spa_load_max_txg == UINT64_MAX)) { 17151635Sbonwick dmu_tx_t *tx; 17161635Sbonwick int need_update = B_FALSE; 17178241SJeff.Bonwick@Sun.COM 17188241SJeff.Bonwick@Sun.COM ASSERT(state != SPA_LOAD_TRYIMPORT); 17191601Sbonwick 17201635Sbonwick /* 17211635Sbonwick * Claim log blocks that haven't been committed yet. 17221635Sbonwick * This must all happen in a single txg. 172310922SJeff.Bonwick@Sun.COM * Note: spa_claim_max_txg is updated by spa_claim_notify(), 172410922SJeff.Bonwick@Sun.COM * invoked from zil_claim_log_block()'s i/o done callback. 172510921STim.Haley@Sun.COM * Price of rollback is that we abandon the log. 17261635Sbonwick */ 172710922SJeff.Bonwick@Sun.COM spa->spa_claiming = B_TRUE; 172810922SJeff.Bonwick@Sun.COM 17291601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1730789Sahrens spa_first_txg(spa)); 17317754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 17322417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1733789Sahrens dmu_tx_commit(tx); 1734789Sahrens 173510922SJeff.Bonwick@Sun.COM spa->spa_claiming = B_FALSE; 173610922SJeff.Bonwick@Sun.COM 17379701SGeorge.Wilson@Sun.COM spa->spa_log_state = SPA_LOG_GOOD; 1738789Sahrens spa->spa_sync_on = B_TRUE; 1739789Sahrens txg_sync_start(spa->spa_dsl_pool); 1740789Sahrens 1741789Sahrens /* 174210922SJeff.Bonwick@Sun.COM * Wait for all claims to sync. We sync up to the highest 174310922SJeff.Bonwick@Sun.COM * claimed log block birth time so that claimed log blocks 174410922SJeff.Bonwick@Sun.COM * don't appear to be from the future. spa_claim_max_txg 174510922SJeff.Bonwick@Sun.COM * will have been set for us by either zil_check_log_chain() 174610922SJeff.Bonwick@Sun.COM * (invoked from spa_check_logs()) or zil_claim() above. 1747789Sahrens */ 174810922SJeff.Bonwick@Sun.COM txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 17491585Sbonwick 17501585Sbonwick /* 17511635Sbonwick * If the config cache is stale, or we have uninitialized 17521635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 175310100SLin.Ling@Sun.COM * 175410100SLin.Ling@Sun.COM * If spa_load_verbatim is true, trust the current 175510100SLin.Ling@Sun.COM * in-core spa_config and update the disk labels. 17561585Sbonwick */ 17571635Sbonwick if (config_cache_txg != spa->spa_config_txg || 175810921STim.Haley@Sun.COM state == SPA_LOAD_IMPORT || spa->spa_load_verbatim || 175910921STim.Haley@Sun.COM state == SPA_LOAD_RECOVER) 17601635Sbonwick need_update = B_TRUE; 17611635Sbonwick 17628241SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) 17631635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 17641635Sbonwick need_update = B_TRUE; 17651585Sbonwick 17661585Sbonwick /* 17671635Sbonwick * Update the config cache asychronously in case we're the 17681635Sbonwick * root pool, in which case the config cache isn't writable yet. 17691585Sbonwick */ 17701635Sbonwick if (need_update) 17711635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 17728241SJeff.Bonwick@Sun.COM 17738241SJeff.Bonwick@Sun.COM /* 17748241SJeff.Bonwick@Sun.COM * Check all DTLs to see if anything needs resilvering. 17758241SJeff.Bonwick@Sun.COM */ 17768241SJeff.Bonwick@Sun.COM if (vdev_resilver_needed(rvd, NULL, NULL)) 17778241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 177810298SMatthew.Ahrens@Sun.COM 177910298SMatthew.Ahrens@Sun.COM /* 178010298SMatthew.Ahrens@Sun.COM * Delete any inconsistent datasets. 178110298SMatthew.Ahrens@Sun.COM */ 178210298SMatthew.Ahrens@Sun.COM (void) dmu_objset_find(spa_name(spa), 178310298SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 178410342Schris.kirby@sun.com 178510342Schris.kirby@sun.com /* 178610342Schris.kirby@sun.com * Clean up any stale temporary dataset userrefs. 178710342Schris.kirby@sun.com */ 178810342Schris.kirby@sun.com dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 1789789Sahrens } 1790789Sahrens 17911544Seschrock error = 0; 17921544Seschrock out: 179310921STim.Haley@Sun.COM 17947046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 17952082Seschrock if (error && error != EBADF) 17967294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 17971544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 17981544Seschrock spa->spa_ena = 0; 17991544Seschrock 18001544Seschrock return (error); 1801789Sahrens } 1802789Sahrens 180310921STim.Haley@Sun.COM static int 180410921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 180510921STim.Haley@Sun.COM { 180610921STim.Haley@Sun.COM spa_unload(spa); 180710921STim.Haley@Sun.COM spa_deactivate(spa); 180810921STim.Haley@Sun.COM 180910921STim.Haley@Sun.COM spa->spa_load_max_txg--; 181010921STim.Haley@Sun.COM 181110921STim.Haley@Sun.COM spa_activate(spa, spa_mode_global); 181210921STim.Haley@Sun.COM spa_async_suspend(spa); 181310921STim.Haley@Sun.COM 181410921STim.Haley@Sun.COM return (spa_load(spa, state, mosconfig)); 181510921STim.Haley@Sun.COM } 181610921STim.Haley@Sun.COM 181710921STim.Haley@Sun.COM static int 181810921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 181910921STim.Haley@Sun.COM uint64_t max_request, boolean_t extreme) 182010921STim.Haley@Sun.COM { 182110921STim.Haley@Sun.COM nvlist_t *config = NULL; 182210921STim.Haley@Sun.COM int load_error, rewind_error; 182310921STim.Haley@Sun.COM uint64_t safe_rollback_txg; 182410921STim.Haley@Sun.COM uint64_t min_txg; 182510921STim.Haley@Sun.COM 182610921STim.Haley@Sun.COM if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) 182710921STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_load_txg; 182810921STim.Haley@Sun.COM else 182910921STim.Haley@Sun.COM spa->spa_load_max_txg = max_request; 183010921STim.Haley@Sun.COM 183110921STim.Haley@Sun.COM load_error = rewind_error = spa_load(spa, state, mosconfig); 183210921STim.Haley@Sun.COM if (load_error == 0) 183310921STim.Haley@Sun.COM return (0); 183410921STim.Haley@Sun.COM 183510921STim.Haley@Sun.COM if (spa->spa_root_vdev != NULL) 183610921STim.Haley@Sun.COM config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 183710921STim.Haley@Sun.COM 183810921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 183910921STim.Haley@Sun.COM spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 184010921STim.Haley@Sun.COM 184110921STim.Haley@Sun.COM /* specific txg requested */ 184210921STim.Haley@Sun.COM if (spa->spa_load_max_txg != UINT64_MAX && !extreme) { 184310921STim.Haley@Sun.COM nvlist_free(config); 184410921STim.Haley@Sun.COM return (load_error); 184510921STim.Haley@Sun.COM } 184610921STim.Haley@Sun.COM 184710921STim.Haley@Sun.COM /* Price of rolling back is discarding txgs, including log */ 184810921STim.Haley@Sun.COM if (state == SPA_LOAD_RECOVER) 184910921STim.Haley@Sun.COM spa->spa_log_state = SPA_LOG_CLEAR; 185010921STim.Haley@Sun.COM 185110921STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 185210921STim.Haley@Sun.COM safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE; 185310921STim.Haley@Sun.COM 185410921STim.Haley@Sun.COM min_txg = extreme ? TXG_INITIAL : safe_rollback_txg; 185510921STim.Haley@Sun.COM while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) { 185610921STim.Haley@Sun.COM if (spa->spa_load_max_txg < safe_rollback_txg) 185710921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_TRUE; 185810921STim.Haley@Sun.COM rewind_error = spa_load_retry(spa, state, mosconfig); 185910921STim.Haley@Sun.COM } 186010921STim.Haley@Sun.COM 186110921STim.Haley@Sun.COM if (config) 186210921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa, config); 186310921STim.Haley@Sun.COM 186410921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_FALSE; 186510921STim.Haley@Sun.COM spa->spa_load_max_txg = UINT64_MAX; 186610921STim.Haley@Sun.COM 186710921STim.Haley@Sun.COM if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 186810921STim.Haley@Sun.COM spa_config_set(spa, config); 186910921STim.Haley@Sun.COM 187010921STim.Haley@Sun.COM return (state == SPA_LOAD_RECOVER ? rewind_error : load_error); 187110921STim.Haley@Sun.COM } 187210921STim.Haley@Sun.COM 1873789Sahrens /* 1874789Sahrens * Pool Open/Import 1875789Sahrens * 1876789Sahrens * The import case is identical to an open except that the configuration is sent 1877789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1878789Sahrens * case of an open, the pool configuration will exist in the 18794451Seschrock * POOL_STATE_UNINITIALIZED state. 1880789Sahrens * 1881789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1882789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1883789Sahrens * ambiguous state. 1884789Sahrens */ 1885789Sahrens static int 188610921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 188710921STim.Haley@Sun.COM nvlist_t **config) 1888789Sahrens { 1889789Sahrens spa_t *spa; 189010921STim.Haley@Sun.COM boolean_t norewind; 189110921STim.Haley@Sun.COM boolean_t extreme; 189210921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 189310921STim.Haley@Sun.COM spa_load_state_t state = SPA_LOAD_OPEN; 1894789Sahrens int error; 1895789Sahrens int locked = B_FALSE; 1896789Sahrens 1897789Sahrens *spapp = NULL; 1898789Sahrens 189910921STim.Haley@Sun.COM zpool_get_rewind_policy(nvpolicy, &policy); 190010921STim.Haley@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 190110921STim.Haley@Sun.COM state = SPA_LOAD_RECOVER; 190210921STim.Haley@Sun.COM norewind = (policy.zrp_request == ZPOOL_NO_REWIND); 190310921STim.Haley@Sun.COM extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0); 190410921STim.Haley@Sun.COM 1905789Sahrens /* 1906789Sahrens * As disgusting as this is, we need to support recursive calls to this 1907789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1908789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1909789Sahrens * avoid dsl_dir_open() calling this in the first place. 1910789Sahrens */ 1911789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1912789Sahrens mutex_enter(&spa_namespace_lock); 1913789Sahrens locked = B_TRUE; 1914789Sahrens } 1915789Sahrens 1916789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1917789Sahrens if (locked) 1918789Sahrens mutex_exit(&spa_namespace_lock); 1919789Sahrens return (ENOENT); 1920789Sahrens } 192110921STim.Haley@Sun.COM 1922789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1923789Sahrens 19248241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1925789Sahrens 192610921STim.Haley@Sun.COM if (spa->spa_last_open_failed && norewind) { 192710921STim.Haley@Sun.COM if (config != NULL && spa->spa_config) 192810921STim.Haley@Sun.COM VERIFY(nvlist_dup(spa->spa_config, 192910921STim.Haley@Sun.COM config, KM_SLEEP) == 0); 193010921STim.Haley@Sun.COM spa_deactivate(spa); 193110921STim.Haley@Sun.COM if (locked) 193210921STim.Haley@Sun.COM mutex_exit(&spa_namespace_lock); 193310921STim.Haley@Sun.COM return (spa->spa_last_open_failed); 193410921STim.Haley@Sun.COM } 193510921STim.Haley@Sun.COM 193610921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 193710921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 193810921STim.Haley@Sun.COM 193910921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 194010921STim.Haley@Sun.COM extreme); 1941789Sahrens 1942789Sahrens if (error == EBADF) { 1943789Sahrens /* 19441986Seschrock * If vdev_validate() returns failure (indicated by 19451986Seschrock * EBADF), it indicates that one of the vdevs indicates 19461986Seschrock * that the pool has been exported or destroyed. If 19471986Seschrock * this is the case, the config cache is out of sync and 19481986Seschrock * we should remove the pool from the namespace. 1949789Sahrens */ 1950789Sahrens spa_unload(spa); 1951789Sahrens spa_deactivate(spa); 19526643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1953789Sahrens spa_remove(spa); 1954789Sahrens if (locked) 1955789Sahrens mutex_exit(&spa_namespace_lock); 1956789Sahrens return (ENOENT); 19571544Seschrock } 19581544Seschrock 19591544Seschrock if (error) { 1960789Sahrens /* 1961789Sahrens * We can't open the pool, but we still have useful 1962789Sahrens * information: the state of each vdev after the 1963789Sahrens * attempted vdev_open(). Return this to the user. 1964789Sahrens */ 196510921STim.Haley@Sun.COM if (config != NULL && spa->spa_config) 196610921STim.Haley@Sun.COM VERIFY(nvlist_dup(spa->spa_config, config, 196710921STim.Haley@Sun.COM KM_SLEEP) == 0); 1968789Sahrens spa_unload(spa); 1969789Sahrens spa_deactivate(spa); 197010921STim.Haley@Sun.COM spa->spa_last_open_failed = error; 1971789Sahrens if (locked) 1972789Sahrens mutex_exit(&spa_namespace_lock); 1973789Sahrens *spapp = NULL; 1974789Sahrens return (error); 1975789Sahrens } 197610921STim.Haley@Sun.COM 1977789Sahrens } 1978789Sahrens 1979789Sahrens spa_open_ref(spa, tag); 19804451Seschrock 198110921STim.Haley@Sun.COM spa->spa_last_open_failed = 0; 198210921STim.Haley@Sun.COM 198310921STim.Haley@Sun.COM if (config != NULL) 198410921STim.Haley@Sun.COM *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 198510921STim.Haley@Sun.COM 198610921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = 0; 198710921STim.Haley@Sun.COM spa->spa_load_txg = 0; 198810921STim.Haley@Sun.COM 1989789Sahrens if (locked) 1990789Sahrens mutex_exit(&spa_namespace_lock); 1991789Sahrens 1992789Sahrens *spapp = spa; 1993789Sahrens 1994789Sahrens return (0); 1995789Sahrens } 1996789Sahrens 1997789Sahrens int 199810921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 199910921STim.Haley@Sun.COM nvlist_t **config) 200010921STim.Haley@Sun.COM { 200110921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, policy, config)); 200210921STim.Haley@Sun.COM } 200310921STim.Haley@Sun.COM 200410921STim.Haley@Sun.COM int 2005789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2006789Sahrens { 200710921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, NULL, NULL)); 2008789Sahrens } 2009789Sahrens 20101544Seschrock /* 20111544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 20121544Seschrock * preventing it from being exported or destroyed. 20131544Seschrock */ 20141544Seschrock spa_t * 20151544Seschrock spa_inject_addref(char *name) 20161544Seschrock { 20171544Seschrock spa_t *spa; 20181544Seschrock 20191544Seschrock mutex_enter(&spa_namespace_lock); 20201544Seschrock if ((spa = spa_lookup(name)) == NULL) { 20211544Seschrock mutex_exit(&spa_namespace_lock); 20221544Seschrock return (NULL); 20231544Seschrock } 20241544Seschrock spa->spa_inject_ref++; 20251544Seschrock mutex_exit(&spa_namespace_lock); 20261544Seschrock 20271544Seschrock return (spa); 20281544Seschrock } 20291544Seschrock 20301544Seschrock void 20311544Seschrock spa_inject_delref(spa_t *spa) 20321544Seschrock { 20331544Seschrock mutex_enter(&spa_namespace_lock); 20341544Seschrock spa->spa_inject_ref--; 20351544Seschrock mutex_exit(&spa_namespace_lock); 20361544Seschrock } 20371544Seschrock 20385450Sbrendan /* 20395450Sbrendan * Add spares device information to the nvlist. 20405450Sbrendan */ 20412082Seschrock static void 20422082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 20432082Seschrock { 20442082Seschrock nvlist_t **spares; 20452082Seschrock uint_t i, nspares; 20462082Seschrock nvlist_t *nvroot; 20472082Seschrock uint64_t guid; 20482082Seschrock vdev_stat_t *vs; 20492082Seschrock uint_t vsc; 20503377Seschrock uint64_t pool; 20512082Seschrock 20529425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 20539425SEric.Schrock@Sun.COM 20545450Sbrendan if (spa->spa_spares.sav_count == 0) 20552082Seschrock return; 20562082Seschrock 20572082Seschrock VERIFY(nvlist_lookup_nvlist(config, 20582082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 20595450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 20602082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 20612082Seschrock if (nspares != 0) { 20622082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 20632082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 20642082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 20652082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 20662082Seschrock 20672082Seschrock /* 20682082Seschrock * Go through and find any spares which have since been 20692082Seschrock * repurposed as an active spare. If this is the case, update 20702082Seschrock * their status appropriately. 20712082Seschrock */ 20722082Seschrock for (i = 0; i < nspares; i++) { 20732082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 20742082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 20757214Slling if (spa_spare_exists(guid, &pool, NULL) && 20767214Slling pool != 0ULL) { 20772082Seschrock VERIFY(nvlist_lookup_uint64_array( 20782082Seschrock spares[i], ZPOOL_CONFIG_STATS, 20792082Seschrock (uint64_t **)&vs, &vsc) == 0); 20802082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 20812082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 20822082Seschrock } 20832082Seschrock } 20842082Seschrock } 20852082Seschrock } 20862082Seschrock 20875450Sbrendan /* 20885450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 20895450Sbrendan */ 20905450Sbrendan static void 20915450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 20925450Sbrendan { 20935450Sbrendan nvlist_t **l2cache; 20945450Sbrendan uint_t i, j, nl2cache; 20955450Sbrendan nvlist_t *nvroot; 20965450Sbrendan uint64_t guid; 20975450Sbrendan vdev_t *vd; 20985450Sbrendan vdev_stat_t *vs; 20995450Sbrendan uint_t vsc; 21005450Sbrendan 21019425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 21029425SEric.Schrock@Sun.COM 21035450Sbrendan if (spa->spa_l2cache.sav_count == 0) 21045450Sbrendan return; 21055450Sbrendan 21065450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 21075450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 21085450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 21095450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 21105450Sbrendan if (nl2cache != 0) { 21115450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 21125450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21135450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 21145450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 21155450Sbrendan 21165450Sbrendan /* 21175450Sbrendan * Update level 2 cache device stats. 21185450Sbrendan */ 21195450Sbrendan 21205450Sbrendan for (i = 0; i < nl2cache; i++) { 21215450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 21225450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 21235450Sbrendan 21245450Sbrendan vd = NULL; 21255450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 21265450Sbrendan if (guid == 21275450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 21285450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 21295450Sbrendan break; 21305450Sbrendan } 21315450Sbrendan } 21325450Sbrendan ASSERT(vd != NULL); 21335450Sbrendan 21345450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 21355450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 21365450Sbrendan vdev_get_stats(vd, vs); 21375450Sbrendan } 21385450Sbrendan } 21395450Sbrendan } 21405450Sbrendan 2141789Sahrens int 21421544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 2143789Sahrens { 2144789Sahrens int error; 2145789Sahrens spa_t *spa; 2146789Sahrens 2147789Sahrens *config = NULL; 214810921STim.Haley@Sun.COM error = spa_open_common(name, &spa, FTAG, NULL, config); 2149789Sahrens 21509425SEric.Schrock@Sun.COM if (spa != NULL) { 21519425SEric.Schrock@Sun.COM /* 21529425SEric.Schrock@Sun.COM * This still leaves a window of inconsistency where the spares 21539425SEric.Schrock@Sun.COM * or l2cache devices could change and the config would be 21549425SEric.Schrock@Sun.COM * self-inconsistent. 21559425SEric.Schrock@Sun.COM */ 21569425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 21579425SEric.Schrock@Sun.COM 21589425SEric.Schrock@Sun.COM if (*config != NULL) { 21597754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 21609425SEric.Schrock@Sun.COM ZPOOL_CONFIG_ERRCOUNT, 21619425SEric.Schrock@Sun.COM spa_get_errlog_size(spa)) == 0); 21629425SEric.Schrock@Sun.COM 21639425SEric.Schrock@Sun.COM if (spa_suspended(spa)) 21649425SEric.Schrock@Sun.COM VERIFY(nvlist_add_uint64(*config, 21659425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SUSPENDED, 21669425SEric.Schrock@Sun.COM spa->spa_failmode) == 0); 21679425SEric.Schrock@Sun.COM 21689425SEric.Schrock@Sun.COM spa_add_spares(spa, *config); 21699425SEric.Schrock@Sun.COM spa_add_l2cache(spa, *config); 21709425SEric.Schrock@Sun.COM } 21712082Seschrock } 21722082Seschrock 21731544Seschrock /* 21741544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 21751544Seschrock * and call spa_lookup() directly. 21761544Seschrock */ 21771544Seschrock if (altroot) { 21781544Seschrock if (spa == NULL) { 21791544Seschrock mutex_enter(&spa_namespace_lock); 21801544Seschrock spa = spa_lookup(name); 21811544Seschrock if (spa) 21821544Seschrock spa_altroot(spa, altroot, buflen); 21831544Seschrock else 21841544Seschrock altroot[0] = '\0'; 21851544Seschrock spa = NULL; 21861544Seschrock mutex_exit(&spa_namespace_lock); 21871544Seschrock } else { 21881544Seschrock spa_altroot(spa, altroot, buflen); 21891544Seschrock } 21901544Seschrock } 21911544Seschrock 21929425SEric.Schrock@Sun.COM if (spa != NULL) { 21939425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 2194789Sahrens spa_close(spa, FTAG); 21959425SEric.Schrock@Sun.COM } 2196789Sahrens 2197789Sahrens return (error); 2198789Sahrens } 2199789Sahrens 2200789Sahrens /* 22015450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 22025450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 22035450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 22045450Sbrendan * specified, as long as they are well-formed. 22052082Seschrock */ 22062082Seschrock static int 22075450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 22085450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 22095450Sbrendan vdev_labeltype_t label) 22102082Seschrock { 22115450Sbrendan nvlist_t **dev; 22125450Sbrendan uint_t i, ndev; 22132082Seschrock vdev_t *vd; 22142082Seschrock int error; 22152082Seschrock 22167754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 22177754SJeff.Bonwick@Sun.COM 22182082Seschrock /* 22195450Sbrendan * It's acceptable to have no devs specified. 22202082Seschrock */ 22215450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 22222082Seschrock return (0); 22232082Seschrock 22245450Sbrendan if (ndev == 0) 22252082Seschrock return (EINVAL); 22262082Seschrock 22272082Seschrock /* 22285450Sbrendan * Make sure the pool is formatted with a version that supports this 22295450Sbrendan * device type. 22302082Seschrock */ 22315450Sbrendan if (spa_version(spa) < version) 22322082Seschrock return (ENOTSUP); 22332082Seschrock 22343377Seschrock /* 22355450Sbrendan * Set the pending device list so we correctly handle device in-use 22363377Seschrock * checking. 22373377Seschrock */ 22385450Sbrendan sav->sav_pending = dev; 22395450Sbrendan sav->sav_npending = ndev; 22405450Sbrendan 22415450Sbrendan for (i = 0; i < ndev; i++) { 22425450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 22432082Seschrock mode)) != 0) 22443377Seschrock goto out; 22452082Seschrock 22462082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 22472082Seschrock vdev_free(vd); 22483377Seschrock error = EINVAL; 22493377Seschrock goto out; 22502082Seschrock } 22512082Seschrock 22525450Sbrendan /* 22537754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 22547754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 22555450Sbrendan */ 22567754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 22575450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 22585450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 22595450Sbrendan error = ENOTBLK; 22605450Sbrendan goto out; 22615450Sbrendan } 22627754SJeff.Bonwick@Sun.COM #endif 22632082Seschrock vd->vdev_top = vd; 22643377Seschrock 22653377Seschrock if ((error = vdev_open(vd)) == 0 && 22665450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 22675450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 22683377Seschrock vd->vdev_guid) == 0); 22692082Seschrock } 22702082Seschrock 22712082Seschrock vdev_free(vd); 22723377Seschrock 22735450Sbrendan if (error && 22745450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 22753377Seschrock goto out; 22763377Seschrock else 22773377Seschrock error = 0; 22782082Seschrock } 22792082Seschrock 22803377Seschrock out: 22815450Sbrendan sav->sav_pending = NULL; 22825450Sbrendan sav->sav_npending = 0; 22833377Seschrock return (error); 22842082Seschrock } 22852082Seschrock 22865450Sbrendan static int 22875450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 22885450Sbrendan { 22895450Sbrendan int error; 22905450Sbrendan 22917754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 22927754SJeff.Bonwick@Sun.COM 22935450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 22945450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 22955450Sbrendan VDEV_LABEL_SPARE)) != 0) { 22965450Sbrendan return (error); 22975450Sbrendan } 22985450Sbrendan 22995450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 23005450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 23015450Sbrendan VDEV_LABEL_L2CACHE)); 23025450Sbrendan } 23035450Sbrendan 23045450Sbrendan static void 23055450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 23065450Sbrendan const char *config) 23075450Sbrendan { 23085450Sbrendan int i; 23095450Sbrendan 23105450Sbrendan if (sav->sav_config != NULL) { 23115450Sbrendan nvlist_t **olddevs; 23125450Sbrendan uint_t oldndevs; 23135450Sbrendan nvlist_t **newdevs; 23145450Sbrendan 23155450Sbrendan /* 23165450Sbrendan * Generate new dev list by concatentating with the 23175450Sbrendan * current dev list. 23185450Sbrendan */ 23195450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 23205450Sbrendan &olddevs, &oldndevs) == 0); 23215450Sbrendan 23225450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 23235450Sbrendan (ndevs + oldndevs), KM_SLEEP); 23245450Sbrendan for (i = 0; i < oldndevs; i++) 23255450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 23265450Sbrendan KM_SLEEP) == 0); 23275450Sbrendan for (i = 0; i < ndevs; i++) 23285450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 23295450Sbrendan KM_SLEEP) == 0); 23305450Sbrendan 23315450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 23325450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 23335450Sbrendan 23345450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 23355450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 23365450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 23375450Sbrendan nvlist_free(newdevs[i]); 23385450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 23395450Sbrendan } else { 23405450Sbrendan /* 23415450Sbrendan * Generate a new dev list. 23425450Sbrendan */ 23435450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 23445450Sbrendan KM_SLEEP) == 0); 23455450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 23465450Sbrendan devs, ndevs) == 0); 23475450Sbrendan } 23485450Sbrendan } 23495450Sbrendan 23505450Sbrendan /* 23515450Sbrendan * Stop and drop level 2 ARC devices 23525450Sbrendan */ 23535450Sbrendan void 23545450Sbrendan spa_l2cache_drop(spa_t *spa) 23555450Sbrendan { 23565450Sbrendan vdev_t *vd; 23575450Sbrendan int i; 23585450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 23595450Sbrendan 23605450Sbrendan for (i = 0; i < sav->sav_count; i++) { 23615450Sbrendan uint64_t pool; 23625450Sbrendan 23635450Sbrendan vd = sav->sav_vdevs[i]; 23645450Sbrendan ASSERT(vd != NULL); 23655450Sbrendan 23668241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 23678241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 23685450Sbrendan l2arc_remove_vdev(vd); 23695450Sbrendan if (vd->vdev_isl2cache) 23705450Sbrendan spa_l2cache_remove(vd); 23715450Sbrendan vdev_clear_stats(vd); 23725450Sbrendan (void) vdev_close(vd); 23735450Sbrendan } 23745450Sbrendan } 23755450Sbrendan 23762082Seschrock /* 2377789Sahrens * Pool Creation 2378789Sahrens */ 2379789Sahrens int 23805094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 23817184Stimh const char *history_str, nvlist_t *zplprops) 2382789Sahrens { 2383789Sahrens spa_t *spa; 23845094Slling char *altroot = NULL; 23851635Sbonwick vdev_t *rvd; 2386789Sahrens dsl_pool_t *dp; 2387789Sahrens dmu_tx_t *tx; 23889816SGeorge.Wilson@Sun.COM int error = 0; 2389789Sahrens uint64_t txg = TXG_INITIAL; 23905450Sbrendan nvlist_t **spares, **l2cache; 23915450Sbrendan uint_t nspares, nl2cache; 23925094Slling uint64_t version; 2393789Sahrens 2394789Sahrens /* 2395789Sahrens * If this pool already exists, return failure. 2396789Sahrens */ 2397789Sahrens mutex_enter(&spa_namespace_lock); 2398789Sahrens if (spa_lookup(pool) != NULL) { 2399789Sahrens mutex_exit(&spa_namespace_lock); 2400789Sahrens return (EEXIST); 2401789Sahrens } 2402789Sahrens 2403789Sahrens /* 2404789Sahrens * Allocate a new spa_t structure. 2405789Sahrens */ 24065094Slling (void) nvlist_lookup_string(props, 24075094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 240810921STim.Haley@Sun.COM spa = spa_add(pool, NULL, altroot); 24098241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2410789Sahrens 24115094Slling if (props && (error = spa_prop_validate(spa, props))) { 24125094Slling spa_deactivate(spa); 24135094Slling spa_remove(spa); 24146643Seschrock mutex_exit(&spa_namespace_lock); 24155094Slling return (error); 24165094Slling } 24175094Slling 24185094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 24195094Slling &version) != 0) 24205094Slling version = SPA_VERSION; 24215094Slling ASSERT(version <= SPA_VERSION); 242210922SJeff.Bonwick@Sun.COM 242310922SJeff.Bonwick@Sun.COM spa->spa_first_txg = txg; 242410922SJeff.Bonwick@Sun.COM spa->spa_uberblock.ub_txg = txg - 1; 24255094Slling spa->spa_uberblock.ub_version = version; 2426789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2427789Sahrens 24281635Sbonwick /* 24299234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 24309234SGeorge.Wilson@Sun.COM */ 24319630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 24329630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 24339234SGeorge.Wilson@Sun.COM 24349234SGeorge.Wilson@Sun.COM /* 24351635Sbonwick * Create the root vdev. 24361635Sbonwick */ 24377754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24381635Sbonwick 24392082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 24402082Seschrock 24412082Seschrock ASSERT(error != 0 || rvd != NULL); 24422082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 24432082Seschrock 24445913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 24451635Sbonwick error = EINVAL; 24462082Seschrock 24472082Seschrock if (error == 0 && 24482082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 24495450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 24502082Seschrock VDEV_ALLOC_ADD)) == 0) { 24519816SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 24529816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(rvd->vdev_child[c]); 24539816SGeorge.Wilson@Sun.COM vdev_expand(rvd->vdev_child[c], txg); 24549816SGeorge.Wilson@Sun.COM } 24551635Sbonwick } 24561635Sbonwick 24577754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2458789Sahrens 24592082Seschrock if (error != 0) { 2460789Sahrens spa_unload(spa); 2461789Sahrens spa_deactivate(spa); 2462789Sahrens spa_remove(spa); 2463789Sahrens mutex_exit(&spa_namespace_lock); 2464789Sahrens return (error); 2465789Sahrens } 2466789Sahrens 24672082Seschrock /* 24682082Seschrock * Get the list of spares, if specified. 24692082Seschrock */ 24702082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 24712082Seschrock &spares, &nspares) == 0) { 24725450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 24732082Seschrock KM_SLEEP) == 0); 24745450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 24752082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 24767754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24772082Seschrock spa_load_spares(spa); 24787754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 24795450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 24805450Sbrendan } 24815450Sbrendan 24825450Sbrendan /* 24835450Sbrendan * Get the list of level 2 cache devices, if specified. 24845450Sbrendan */ 24855450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 24865450Sbrendan &l2cache, &nl2cache) == 0) { 24875450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 24885450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 24895450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 24905450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 24917754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24925450Sbrendan spa_load_l2cache(spa); 24937754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 24945450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 24952082Seschrock } 24962082Seschrock 24977184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2498789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2499789Sahrens 250010956SGeorge.Wilson@Sun.COM /* 250110956SGeorge.Wilson@Sun.COM * Create DDTs (dedup tables). 250210956SGeorge.Wilson@Sun.COM */ 250310956SGeorge.Wilson@Sun.COM ddt_create(spa); 250410956SGeorge.Wilson@Sun.COM 250510956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 250610956SGeorge.Wilson@Sun.COM 2507789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2508789Sahrens 2509789Sahrens /* 2510789Sahrens * Create the pool config object. 2511789Sahrens */ 2512789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 25137497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2514789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2515789Sahrens 25161544Seschrock if (zap_add(spa->spa_meta_objset, 2517789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 25181544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 25191544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 25201544Seschrock } 2521789Sahrens 25225094Slling /* Newly created pools with the right version are always deflated. */ 25235094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 25245094Slling spa->spa_deflate = TRUE; 25255094Slling if (zap_add(spa->spa_meta_objset, 25265094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 25275094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 25285094Slling cmn_err(CE_PANIC, "failed to add deflate"); 25295094Slling } 25302082Seschrock } 25312082Seschrock 2532789Sahrens /* 2533789Sahrens * Create the deferred-free bplist object. Turn off compression 2534789Sahrens * because sync-to-convergence takes longer if the blocksize 2535789Sahrens * keeps changing. 2536789Sahrens */ 253710922SJeff.Bonwick@Sun.COM spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset, 2538789Sahrens 1 << 14, tx); 253910922SJeff.Bonwick@Sun.COM dmu_object_set_compress(spa->spa_meta_objset, 254010922SJeff.Bonwick@Sun.COM spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx); 2541789Sahrens 25421544Seschrock if (zap_add(spa->spa_meta_objset, 2543789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 254410922SJeff.Bonwick@Sun.COM sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) { 25451544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 25461544Seschrock } 2547789Sahrens 25482926Sek110237 /* 25492926Sek110237 * Create the pool's history object. 25502926Sek110237 */ 25515094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 25525094Slling spa_history_create_obj(spa, tx); 25535094Slling 25545094Slling /* 25555094Slling * Set pool properties. 25565094Slling */ 25575094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 25585094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 25595329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 25609816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 256110922SJeff.Bonwick@Sun.COM 25628525SEric.Schrock@Sun.COM if (props != NULL) { 25638525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 25645094Slling spa_sync_props(spa, props, CRED(), tx); 25658525SEric.Schrock@Sun.COM } 25662926Sek110237 2567789Sahrens dmu_tx_commit(tx); 2568789Sahrens 2569789Sahrens spa->spa_sync_on = B_TRUE; 2570789Sahrens txg_sync_start(spa->spa_dsl_pool); 2571789Sahrens 2572789Sahrens /* 2573789Sahrens * We explicitly wait for the first transaction to complete so that our 2574789Sahrens * bean counters are appropriately updated. 2575789Sahrens */ 2576789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2577789Sahrens 25786643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2579789Sahrens 25805094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 25814715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 25829946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_CREATE); 25834715Sek110237 25848667SGeorge.Wilson@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 25858667SGeorge.Wilson@Sun.COM 2586789Sahrens mutex_exit(&spa_namespace_lock); 2587789Sahrens 2588789Sahrens return (0); 2589789Sahrens } 2590789Sahrens 25916423Sgw25295 #ifdef _KERNEL 25926423Sgw25295 /* 25939790SLin.Ling@Sun.COM * Get the root pool information from the root disk, then import the root pool 25949790SLin.Ling@Sun.COM * during the system boot up time. 25956423Sgw25295 */ 25969790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 25979790SLin.Ling@Sun.COM 25989790SLin.Ling@Sun.COM static nvlist_t * 25999790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 26006423Sgw25295 { 26019790SLin.Ling@Sun.COM nvlist_t *config; 26026423Sgw25295 nvlist_t *nvtop, *nvroot; 26036423Sgw25295 uint64_t pgid; 26046423Sgw25295 26059790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 26069790SLin.Ling@Sun.COM return (NULL); 26079790SLin.Ling@Sun.COM 26086423Sgw25295 /* 26096423Sgw25295 * Add this top-level vdev to the child array. 26106423Sgw25295 */ 26119790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 26129790SLin.Ling@Sun.COM &nvtop) == 0); 26139790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 26149790SLin.Ling@Sun.COM &pgid) == 0); 26159790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 26166423Sgw25295 26176423Sgw25295 /* 26186423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 26196423Sgw25295 */ 26206423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 26219790SLin.Ling@Sun.COM VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 26229790SLin.Ling@Sun.COM VDEV_TYPE_ROOT) == 0); 26236423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 26246423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 26256423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 26266423Sgw25295 &nvtop, 1) == 0); 26276423Sgw25295 26286423Sgw25295 /* 26296423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 26306423Sgw25295 * this pool's configuration (remove the old, add the new). 26316423Sgw25295 */ 26326423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 26336423Sgw25295 nvlist_free(nvroot); 26349790SLin.Ling@Sun.COM return (config); 26356423Sgw25295 } 26366423Sgw25295 26376423Sgw25295 /* 26389790SLin.Ling@Sun.COM * Walk the vdev tree and see if we can find a device with "better" 26399790SLin.Ling@Sun.COM * configuration. A configuration is "better" if the label on that 26409790SLin.Ling@Sun.COM * device has a more recent txg. 26416423Sgw25295 */ 26429790SLin.Ling@Sun.COM static void 26439790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 26447147Staylor { 26459816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 26469790SLin.Ling@Sun.COM spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 26479790SLin.Ling@Sun.COM 26489790SLin.Ling@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 26499790SLin.Ling@Sun.COM nvlist_t *label; 26509790SLin.Ling@Sun.COM uint64_t label_txg; 26519790SLin.Ling@Sun.COM 26529790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 26539790SLin.Ling@Sun.COM &label) != 0) 26549790SLin.Ling@Sun.COM return; 26559790SLin.Ling@Sun.COM 26569790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 26579790SLin.Ling@Sun.COM &label_txg) == 0); 26589790SLin.Ling@Sun.COM 26599790SLin.Ling@Sun.COM /* 26609790SLin.Ling@Sun.COM * Do we have a better boot device? 26619790SLin.Ling@Sun.COM */ 26629790SLin.Ling@Sun.COM if (label_txg > *txg) { 26639790SLin.Ling@Sun.COM *txg = label_txg; 26649790SLin.Ling@Sun.COM *avd = vd; 26657147Staylor } 26669790SLin.Ling@Sun.COM nvlist_free(label); 26677147Staylor } 26687147Staylor } 26697147Staylor 26706423Sgw25295 /* 26716423Sgw25295 * Import a root pool. 26726423Sgw25295 * 26737147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 26747147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 26757147Staylor * The GRUB "findroot" command will return the vdev we should boot. 26766423Sgw25295 * 26776423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 26786423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 26796423Sgw25295 * e.g. 26806423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 26816423Sgw25295 */ 26826423Sgw25295 int 26837147Staylor spa_import_rootpool(char *devpath, char *devid) 26846423Sgw25295 { 26859790SLin.Ling@Sun.COM spa_t *spa; 26869790SLin.Ling@Sun.COM vdev_t *rvd, *bvd, *avd = NULL; 26879790SLin.Ling@Sun.COM nvlist_t *config, *nvtop; 26889790SLin.Ling@Sun.COM uint64_t guid, txg; 26896423Sgw25295 char *pname; 26906423Sgw25295 int error; 26916423Sgw25295 26926423Sgw25295 /* 26939790SLin.Ling@Sun.COM * Read the label from the boot device and generate a configuration. 26946423Sgw25295 */ 269510822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 269610822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL) 269710822SJack.Meng@Sun.COM if (config == NULL) { 269810822SJack.Meng@Sun.COM if (strstr(devpath, "/iscsi/ssd") != NULL) { 269910822SJack.Meng@Sun.COM /* iscsi boot */ 270010822SJack.Meng@Sun.COM get_iscsi_bootpath_phy(devpath); 270110822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 270210822SJack.Meng@Sun.COM } 270310822SJack.Meng@Sun.COM } 270410822SJack.Meng@Sun.COM #endif 270510822SJack.Meng@Sun.COM if (config == NULL) { 27069790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 27079790SLin.Ling@Sun.COM devpath); 27089790SLin.Ling@Sun.COM return (EIO); 27099790SLin.Ling@Sun.COM } 27109790SLin.Ling@Sun.COM 27119790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 27129790SLin.Ling@Sun.COM &pname) == 0); 27139790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 27146423Sgw25295 27159425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 27169425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pname)) != NULL) { 27179425SEric.Schrock@Sun.COM /* 27189425SEric.Schrock@Sun.COM * Remove the existing root pool from the namespace so that we 27199425SEric.Schrock@Sun.COM * can replace it with the correct config we just read in. 27209425SEric.Schrock@Sun.COM */ 27219425SEric.Schrock@Sun.COM spa_remove(spa); 27229425SEric.Schrock@Sun.COM } 27239425SEric.Schrock@Sun.COM 272410921STim.Haley@Sun.COM spa = spa_add(pname, config, NULL); 27259425SEric.Schrock@Sun.COM spa->spa_is_root = B_TRUE; 272610100SLin.Ling@Sun.COM spa->spa_load_verbatim = B_TRUE; 27279790SLin.Ling@Sun.COM 27289790SLin.Ling@Sun.COM /* 27299790SLin.Ling@Sun.COM * Build up a vdev tree based on the boot device's label config. 27309790SLin.Ling@Sun.COM */ 27319790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 27329790SLin.Ling@Sun.COM &nvtop) == 0); 27339790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27349790SLin.Ling@Sun.COM error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 27359790SLin.Ling@Sun.COM VDEV_ALLOC_ROOTPOOL); 27369790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 27379790SLin.Ling@Sun.COM if (error) { 27389790SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 27399790SLin.Ling@Sun.COM nvlist_free(config); 27409790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 27419790SLin.Ling@Sun.COM pname); 27429790SLin.Ling@Sun.COM return (error); 27439790SLin.Ling@Sun.COM } 27449790SLin.Ling@Sun.COM 27459790SLin.Ling@Sun.COM /* 27469790SLin.Ling@Sun.COM * Get the boot vdev. 27479790SLin.Ling@Sun.COM */ 27489790SLin.Ling@Sun.COM if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 27499790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 27509790SLin.Ling@Sun.COM (u_longlong_t)guid); 27519790SLin.Ling@Sun.COM error = ENOENT; 27529790SLin.Ling@Sun.COM goto out; 27539790SLin.Ling@Sun.COM } 27549790SLin.Ling@Sun.COM 27559790SLin.Ling@Sun.COM /* 27569790SLin.Ling@Sun.COM * Determine if there is a better boot device. 27579790SLin.Ling@Sun.COM */ 27589790SLin.Ling@Sun.COM avd = bvd; 27599790SLin.Ling@Sun.COM spa_alt_rootvdev(rvd, &avd, &txg); 27609790SLin.Ling@Sun.COM if (avd != bvd) { 27619790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 27629790SLin.Ling@Sun.COM "try booting from '%s'", avd->vdev_path); 27639790SLin.Ling@Sun.COM error = EINVAL; 27649790SLin.Ling@Sun.COM goto out; 27659790SLin.Ling@Sun.COM } 27669790SLin.Ling@Sun.COM 27679790SLin.Ling@Sun.COM /* 27689790SLin.Ling@Sun.COM * If the boot device is part of a spare vdev then ensure that 27699790SLin.Ling@Sun.COM * we're booting off the active spare. 27709790SLin.Ling@Sun.COM */ 27719790SLin.Ling@Sun.COM if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 27729790SLin.Ling@Sun.COM !bvd->vdev_isspare) { 27739790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is currently spared. Please " 27749790SLin.Ling@Sun.COM "try booting from '%s'", 27759790SLin.Ling@Sun.COM bvd->vdev_parent->vdev_child[1]->vdev_path); 27769790SLin.Ling@Sun.COM error = EINVAL; 27779790SLin.Ling@Sun.COM goto out; 27789790SLin.Ling@Sun.COM } 27799790SLin.Ling@Sun.COM 27809790SLin.Ling@Sun.COM error = 0; 27819946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 27829790SLin.Ling@Sun.COM out: 27839790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27849790SLin.Ling@Sun.COM vdev_free(rvd); 27859790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 27869425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 27876423Sgw25295 27889790SLin.Ling@Sun.COM nvlist_free(config); 27896423Sgw25295 return (error); 27906423Sgw25295 } 27919790SLin.Ling@Sun.COM 27926423Sgw25295 #endif 27936423Sgw25295 27946423Sgw25295 /* 27959425SEric.Schrock@Sun.COM * Take a pool and insert it into the namespace as if it had been loaded at 27969425SEric.Schrock@Sun.COM * boot. 27979425SEric.Schrock@Sun.COM */ 27989425SEric.Schrock@Sun.COM int 27999425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props) 28009425SEric.Schrock@Sun.COM { 28019425SEric.Schrock@Sun.COM spa_t *spa; 280210921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 28039425SEric.Schrock@Sun.COM char *altroot = NULL; 28049425SEric.Schrock@Sun.COM 28059425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 28069425SEric.Schrock@Sun.COM if (spa_lookup(pool) != NULL) { 28079425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28089425SEric.Schrock@Sun.COM return (EEXIST); 28099425SEric.Schrock@Sun.COM } 28109425SEric.Schrock@Sun.COM 28119425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 28129425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 281310921STim.Haley@Sun.COM spa = spa_add(pool, config, altroot); 281410921STim.Haley@Sun.COM 281510921STim.Haley@Sun.COM zpool_get_rewind_policy(config, &policy); 281610921STim.Haley@Sun.COM spa->spa_load_max_txg = policy.zrp_txg; 28179425SEric.Schrock@Sun.COM 281810100SLin.Ling@Sun.COM spa->spa_load_verbatim = B_TRUE; 281910000SVictor.Latushkin@Sun.COM 28209425SEric.Schrock@Sun.COM if (props != NULL) 28219425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 28229425SEric.Schrock@Sun.COM 28239425SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 28249425SEric.Schrock@Sun.COM 28259425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28269946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 28279425SEric.Schrock@Sun.COM 28289425SEric.Schrock@Sun.COM return (0); 28299425SEric.Schrock@Sun.COM } 28309425SEric.Schrock@Sun.COM 28319425SEric.Schrock@Sun.COM /* 28326423Sgw25295 * Import a non-root pool into the system. 28336423Sgw25295 */ 28346423Sgw25295 int 28356423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 28366423Sgw25295 { 28379425SEric.Schrock@Sun.COM spa_t *spa; 28389425SEric.Schrock@Sun.COM char *altroot = NULL; 283910921STim.Haley@Sun.COM spa_load_state_t state = SPA_LOAD_IMPORT; 284010921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 28419425SEric.Schrock@Sun.COM int error; 28429425SEric.Schrock@Sun.COM nvlist_t *nvroot; 28439425SEric.Schrock@Sun.COM nvlist_t **spares, **l2cache; 28449425SEric.Schrock@Sun.COM uint_t nspares, nl2cache; 28459425SEric.Schrock@Sun.COM 28469425SEric.Schrock@Sun.COM /* 28479425SEric.Schrock@Sun.COM * If a pool with this name exists, return failure. 28489425SEric.Schrock@Sun.COM */ 28499425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 28509425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pool)) != NULL) { 28519425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28529425SEric.Schrock@Sun.COM return (EEXIST); 28539425SEric.Schrock@Sun.COM } 28549425SEric.Schrock@Sun.COM 285510921STim.Haley@Sun.COM zpool_get_rewind_policy(config, &policy); 285610921STim.Haley@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 285710921STim.Haley@Sun.COM state = SPA_LOAD_RECOVER; 285810921STim.Haley@Sun.COM 28599425SEric.Schrock@Sun.COM /* 28609425SEric.Schrock@Sun.COM * Create and initialize the spa structure. 28619425SEric.Schrock@Sun.COM */ 28629425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 28639425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 286410921STim.Haley@Sun.COM spa = spa_add(pool, config, altroot); 28659425SEric.Schrock@Sun.COM spa_activate(spa, spa_mode_global); 28669425SEric.Schrock@Sun.COM 28679425SEric.Schrock@Sun.COM /* 28689630SJeff.Bonwick@Sun.COM * Don't start async tasks until we know everything is healthy. 28699630SJeff.Bonwick@Sun.COM */ 28709630SJeff.Bonwick@Sun.COM spa_async_suspend(spa); 28719630SJeff.Bonwick@Sun.COM 28729630SJeff.Bonwick@Sun.COM /* 28739425SEric.Schrock@Sun.COM * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 28749425SEric.Schrock@Sun.COM * because the user-supplied config is actually the one to trust when 28759425SEric.Schrock@Sun.COM * doing an import. 28769425SEric.Schrock@Sun.COM */ 287710921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 287810921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 287910921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 288010921STim.Haley@Sun.COM ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0)); 288110921STim.Haley@Sun.COM 288210921STim.Haley@Sun.COM /* 288310921STim.Haley@Sun.COM * Propagate anything learned about failing or best txgs 288410921STim.Haley@Sun.COM * back to caller 288510921STim.Haley@Sun.COM */ 288610921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa, config); 28879425SEric.Schrock@Sun.COM 28889425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 28899425SEric.Schrock@Sun.COM /* 28909425SEric.Schrock@Sun.COM * Toss any existing sparelist, as it doesn't have any validity 28919425SEric.Schrock@Sun.COM * anymore, and conflicts with spa_has_spare(). 28929425SEric.Schrock@Sun.COM */ 28939425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) { 28949425SEric.Schrock@Sun.COM nvlist_free(spa->spa_spares.sav_config); 28959425SEric.Schrock@Sun.COM spa->spa_spares.sav_config = NULL; 28969425SEric.Schrock@Sun.COM spa_load_spares(spa); 28979425SEric.Schrock@Sun.COM } 28989425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) { 28999425SEric.Schrock@Sun.COM nvlist_free(spa->spa_l2cache.sav_config); 29009425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_config = NULL; 29019425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 29029425SEric.Schrock@Sun.COM } 29039425SEric.Schrock@Sun.COM 29049425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 29059425SEric.Schrock@Sun.COM &nvroot) == 0); 29069425SEric.Schrock@Sun.COM if (error == 0) 29079425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 29089425SEric.Schrock@Sun.COM VDEV_ALLOC_SPARE); 29099425SEric.Schrock@Sun.COM if (error == 0) 29109425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 29119425SEric.Schrock@Sun.COM VDEV_ALLOC_L2CACHE); 29129425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29139425SEric.Schrock@Sun.COM 29149425SEric.Schrock@Sun.COM if (props != NULL) 29159425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 29169425SEric.Schrock@Sun.COM 29179425SEric.Schrock@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 29189425SEric.Schrock@Sun.COM (error = spa_prop_set(spa, props)))) { 29199425SEric.Schrock@Sun.COM spa_unload(spa); 29209425SEric.Schrock@Sun.COM spa_deactivate(spa); 29219425SEric.Schrock@Sun.COM spa_remove(spa); 29229425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 29239425SEric.Schrock@Sun.COM return (error); 29249425SEric.Schrock@Sun.COM } 29259425SEric.Schrock@Sun.COM 29269630SJeff.Bonwick@Sun.COM spa_async_resume(spa); 29279630SJeff.Bonwick@Sun.COM 29289425SEric.Schrock@Sun.COM /* 29299425SEric.Schrock@Sun.COM * Override any spares and level 2 cache devices as specified by 29309425SEric.Schrock@Sun.COM * the user, as these may have correct device names/devids, etc. 29319425SEric.Schrock@Sun.COM */ 29329425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29339425SEric.Schrock@Sun.COM &spares, &nspares) == 0) { 29349425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) 29359425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_spares.sav_config, 29369425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 29379425SEric.Schrock@Sun.COM else 29389425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 29399425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 29409425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 29419425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 29429425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29439425SEric.Schrock@Sun.COM spa_load_spares(spa); 29449425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29459425SEric.Schrock@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 29469425SEric.Schrock@Sun.COM } 29479425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 29489425SEric.Schrock@Sun.COM &l2cache, &nl2cache) == 0) { 29499425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) 29509425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 29519425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 29529425SEric.Schrock@Sun.COM else 29539425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 29549425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 29559425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 29569425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 29579425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29589425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 29599425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29609425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_sync = B_TRUE; 29619425SEric.Schrock@Sun.COM } 29629425SEric.Schrock@Sun.COM 296310672SEric.Schrock@Sun.COM /* 296410672SEric.Schrock@Sun.COM * Check for any removed devices. 296510672SEric.Schrock@Sun.COM */ 296610672SEric.Schrock@Sun.COM if (spa->spa_autoreplace) { 296710672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 296810672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 296910672SEric.Schrock@Sun.COM } 297010672SEric.Schrock@Sun.COM 29719425SEric.Schrock@Sun.COM if (spa_writeable(spa)) { 29729425SEric.Schrock@Sun.COM /* 29739425SEric.Schrock@Sun.COM * Update the config cache to include the newly-imported pool. 29749425SEric.Schrock@Sun.COM */ 297510100SLin.Ling@Sun.COM spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 29769425SEric.Schrock@Sun.COM } 29779425SEric.Schrock@Sun.COM 29789816SGeorge.Wilson@Sun.COM /* 29799816SGeorge.Wilson@Sun.COM * It's possible that the pool was expanded while it was exported. 29809816SGeorge.Wilson@Sun.COM * We kick off an async task to handle this for us. 29819816SGeorge.Wilson@Sun.COM */ 29829816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 29839816SGeorge.Wilson@Sun.COM 29849425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 29859946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 29869425SEric.Schrock@Sun.COM 29879425SEric.Schrock@Sun.COM return (0); 29886643Seschrock } 29896643Seschrock 29906643Seschrock 2991789Sahrens /* 2992789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2993789Sahrens * to get the vdev stats associated with the imported devices. 2994789Sahrens */ 2995789Sahrens #define TRYIMPORT_NAME "$import" 2996789Sahrens 2997789Sahrens nvlist_t * 2998789Sahrens spa_tryimport(nvlist_t *tryconfig) 2999789Sahrens { 3000789Sahrens nvlist_t *config = NULL; 3001789Sahrens char *poolname; 3002789Sahrens spa_t *spa; 3003789Sahrens uint64_t state; 30048680SLin.Ling@Sun.COM int error; 3005789Sahrens 3006789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3007789Sahrens return (NULL); 3008789Sahrens 3009789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3010789Sahrens return (NULL); 3011789Sahrens 30121635Sbonwick /* 30131635Sbonwick * Create and initialize the spa structure. 30141635Sbonwick */ 3015789Sahrens mutex_enter(&spa_namespace_lock); 301610921STim.Haley@Sun.COM spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 30178241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 3018789Sahrens 3019789Sahrens /* 30201635Sbonwick * Pass off the heavy lifting to spa_load(). 30211732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 30221732Sbonwick * is actually the one to trust when doing an import. 3023789Sahrens */ 302410921STim.Haley@Sun.COM error = spa_load(spa, SPA_LOAD_TRYIMPORT, B_TRUE); 3025789Sahrens 3026789Sahrens /* 3027789Sahrens * If 'tryconfig' was at least parsable, return the current config. 3028789Sahrens */ 3029789Sahrens if (spa->spa_root_vdev != NULL) { 3030789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3031789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3032789Sahrens poolname) == 0); 3033789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3034789Sahrens state) == 0); 30353975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 30363975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 30372082Seschrock 30382082Seschrock /* 30396423Sgw25295 * If the bootfs property exists on this pool then we 30406423Sgw25295 * copy it out so that external consumers can tell which 30416423Sgw25295 * pools are bootable. 30426423Sgw25295 */ 30438680SLin.Ling@Sun.COM if ((!error || error == EEXIST) && spa->spa_bootfs) { 30446423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 30456423Sgw25295 30466423Sgw25295 /* 30476423Sgw25295 * We have to play games with the name since the 30486423Sgw25295 * pool was opened as TRYIMPORT_NAME. 30496423Sgw25295 */ 30507754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 30516423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 30526423Sgw25295 char *cp; 30536423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 30546423Sgw25295 30556423Sgw25295 cp = strchr(tmpname, '/'); 30566423Sgw25295 if (cp == NULL) { 30576423Sgw25295 (void) strlcpy(dsname, tmpname, 30586423Sgw25295 MAXPATHLEN); 30596423Sgw25295 } else { 30606423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 30616423Sgw25295 "%s/%s", poolname, ++cp); 30626423Sgw25295 } 30636423Sgw25295 VERIFY(nvlist_add_string(config, 30646423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 30656423Sgw25295 kmem_free(dsname, MAXPATHLEN); 30666423Sgw25295 } 30676423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 30686423Sgw25295 } 30696423Sgw25295 30706423Sgw25295 /* 30715450Sbrendan * Add the list of hot spares and level 2 cache devices. 30722082Seschrock */ 30739425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 30742082Seschrock spa_add_spares(spa, config); 30755450Sbrendan spa_add_l2cache(spa, config); 30769425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 3077789Sahrens } 3078789Sahrens 3079789Sahrens spa_unload(spa); 3080789Sahrens spa_deactivate(spa); 3081789Sahrens spa_remove(spa); 3082789Sahrens mutex_exit(&spa_namespace_lock); 3083789Sahrens 3084789Sahrens return (config); 3085789Sahrens } 3086789Sahrens 3087789Sahrens /* 3088789Sahrens * Pool export/destroy 3089789Sahrens * 3090789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 3091789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 3092789Sahrens * update the pool state and sync all the labels to disk, removing the 30938211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 30948211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 3095789Sahrens */ 3096789Sahrens static int 30977214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 30988211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 3099789Sahrens { 3100789Sahrens spa_t *spa; 3101789Sahrens 31021775Sbillm if (oldconfig) 31031775Sbillm *oldconfig = NULL; 31041775Sbillm 31058241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 3106789Sahrens return (EROFS); 3107789Sahrens 3108789Sahrens mutex_enter(&spa_namespace_lock); 3109789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3110789Sahrens mutex_exit(&spa_namespace_lock); 3111789Sahrens return (ENOENT); 3112789Sahrens } 3113789Sahrens 3114789Sahrens /* 31151544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 31161544Seschrock * reacquire the namespace lock, and see if we can export. 31171544Seschrock */ 31181544Seschrock spa_open_ref(spa, FTAG); 31191544Seschrock mutex_exit(&spa_namespace_lock); 31201544Seschrock spa_async_suspend(spa); 31211544Seschrock mutex_enter(&spa_namespace_lock); 31221544Seschrock spa_close(spa, FTAG); 31231544Seschrock 31241544Seschrock /* 3125789Sahrens * The pool will be in core if it's openable, 3126789Sahrens * in which case we can modify its state. 3127789Sahrens */ 3128789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 3129789Sahrens /* 3130789Sahrens * Objsets may be open only because they're dirty, so we 3131789Sahrens * have to force it to sync before checking spa_refcnt. 3132789Sahrens */ 3133789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 3134789Sahrens 31351544Seschrock /* 31361544Seschrock * A pool cannot be exported or destroyed if there are active 31371544Seschrock * references. If we are resetting a pool, allow references by 31381544Seschrock * fault injection handlers. 31391544Seschrock */ 31401544Seschrock if (!spa_refcount_zero(spa) || 31411544Seschrock (spa->spa_inject_ref != 0 && 31421544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 31431544Seschrock spa_async_resume(spa); 3144789Sahrens mutex_exit(&spa_namespace_lock); 3145789Sahrens return (EBUSY); 3146789Sahrens } 3147789Sahrens 3148789Sahrens /* 31497214Slling * A pool cannot be exported if it has an active shared spare. 31507214Slling * This is to prevent other pools stealing the active spare 31517214Slling * from an exported pool. At user's own will, such pool can 31527214Slling * be forcedly exported. 31537214Slling */ 31547214Slling if (!force && new_state == POOL_STATE_EXPORTED && 31557214Slling spa_has_active_shared_spare(spa)) { 31567214Slling spa_async_resume(spa); 31577214Slling mutex_exit(&spa_namespace_lock); 31587214Slling return (EXDEV); 31597214Slling } 31607214Slling 31617214Slling /* 3162789Sahrens * We want this to be reflected on every label, 3163789Sahrens * so mark them all dirty. spa_unload() will do the 3164789Sahrens * final sync that pushes these changes out. 3165789Sahrens */ 31668211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 31677754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 31681544Seschrock spa->spa_state = new_state; 31691635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 31701544Seschrock vdev_config_dirty(spa->spa_root_vdev); 31717754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 31721544Seschrock } 3173789Sahrens } 3174789Sahrens 31754451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 31764451Seschrock 3177789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3178789Sahrens spa_unload(spa); 3179789Sahrens spa_deactivate(spa); 3180789Sahrens } 3181789Sahrens 31821775Sbillm if (oldconfig && spa->spa_config) 31831775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 31841775Sbillm 31851544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 31868211SGeorge.Wilson@Sun.COM if (!hardforce) 31878211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 31881544Seschrock spa_remove(spa); 31891544Seschrock } 3190789Sahrens mutex_exit(&spa_namespace_lock); 3191789Sahrens 3192789Sahrens return (0); 3193789Sahrens } 3194789Sahrens 3195789Sahrens /* 3196789Sahrens * Destroy a storage pool. 3197789Sahrens */ 3198789Sahrens int 3199789Sahrens spa_destroy(char *pool) 3200789Sahrens { 32018211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 32028211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 3203789Sahrens } 3204789Sahrens 3205789Sahrens /* 3206789Sahrens * Export a storage pool. 3207789Sahrens */ 3208789Sahrens int 32098211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 32108211SGeorge.Wilson@Sun.COM boolean_t hardforce) 3211789Sahrens { 32128211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 32138211SGeorge.Wilson@Sun.COM force, hardforce)); 3214789Sahrens } 3215789Sahrens 3216789Sahrens /* 32171544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 32181544Seschrock * from the namespace in any way. 32191544Seschrock */ 32201544Seschrock int 32211544Seschrock spa_reset(char *pool) 32221544Seschrock { 32237214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 32248211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 32251544Seschrock } 32261544Seschrock 32271544Seschrock /* 3228789Sahrens * ========================================================================== 3229789Sahrens * Device manipulation 3230789Sahrens * ========================================================================== 3231789Sahrens */ 3232789Sahrens 3233789Sahrens /* 32344527Sperrin * Add a device to a storage pool. 3235789Sahrens */ 3236789Sahrens int 3237789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 3238789Sahrens { 323910594SGeorge.Wilson@Sun.COM uint64_t txg, id; 32408241SJeff.Bonwick@Sun.COM int error; 3241789Sahrens vdev_t *rvd = spa->spa_root_vdev; 32421585Sbonwick vdev_t *vd, *tvd; 32435450Sbrendan nvlist_t **spares, **l2cache; 32445450Sbrendan uint_t nspares, nl2cache; 3245789Sahrens 3246789Sahrens txg = spa_vdev_enter(spa); 3247789Sahrens 32482082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 32492082Seschrock VDEV_ALLOC_ADD)) != 0) 32502082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 32512082Seschrock 32527754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 3253789Sahrens 32545450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 32555450Sbrendan &nspares) != 0) 32562082Seschrock nspares = 0; 32572082Seschrock 32585450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 32595450Sbrendan &nl2cache) != 0) 32605450Sbrendan nl2cache = 0; 32615450Sbrendan 32627754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 32632082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 32647754SJeff.Bonwick@Sun.COM 32657754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 32667754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 32677754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 32682082Seschrock 32693377Seschrock /* 32705450Sbrendan * We must validate the spares and l2cache devices after checking the 32715450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 32723377Seschrock */ 32737754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 32743377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 32753377Seschrock 32763377Seschrock /* 32773377Seschrock * Transfer each new top-level vdev from vd to rvd. 32783377Seschrock */ 32798241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 328010594SGeorge.Wilson@Sun.COM 328110594SGeorge.Wilson@Sun.COM /* 328210594SGeorge.Wilson@Sun.COM * Set the vdev id to the first hole, if one exists. 328310594SGeorge.Wilson@Sun.COM */ 328410594SGeorge.Wilson@Sun.COM for (id = 0; id < rvd->vdev_children; id++) { 328510594SGeorge.Wilson@Sun.COM if (rvd->vdev_child[id]->vdev_ishole) { 328610594SGeorge.Wilson@Sun.COM vdev_free(rvd->vdev_child[id]); 328710594SGeorge.Wilson@Sun.COM break; 328810594SGeorge.Wilson@Sun.COM } 328910594SGeorge.Wilson@Sun.COM } 32903377Seschrock tvd = vd->vdev_child[c]; 32913377Seschrock vdev_remove_child(vd, tvd); 329210594SGeorge.Wilson@Sun.COM tvd->vdev_id = id; 32933377Seschrock vdev_add_child(rvd, tvd); 32943377Seschrock vdev_config_dirty(tvd); 32953377Seschrock } 32963377Seschrock 32972082Seschrock if (nspares != 0) { 32985450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 32995450Sbrendan ZPOOL_CONFIG_SPARES); 33002082Seschrock spa_load_spares(spa); 33015450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 33025450Sbrendan } 33035450Sbrendan 33045450Sbrendan if (nl2cache != 0) { 33055450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 33065450Sbrendan ZPOOL_CONFIG_L2CACHE); 33075450Sbrendan spa_load_l2cache(spa); 33085450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3309789Sahrens } 3310789Sahrens 3311789Sahrens /* 33121585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 33131585Sbonwick * If other threads start allocating from these vdevs before we 33141585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 33151585Sbonwick * fail to open the pool because there are DVAs that the config cache 33161585Sbonwick * can't translate. Therefore, we first add the vdevs without 33171585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 33181635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 33191585Sbonwick * 33201585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 33211585Sbonwick * if we lose power at any point in this sequence, the remaining 33221585Sbonwick * steps will be completed the next time we load the pool. 3323789Sahrens */ 33241635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 33251585Sbonwick 33261635Sbonwick mutex_enter(&spa_namespace_lock); 33271635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 33281635Sbonwick mutex_exit(&spa_namespace_lock); 3329789Sahrens 33301635Sbonwick return (0); 3331789Sahrens } 3332789Sahrens 3333789Sahrens /* 3334789Sahrens * Attach a device to a mirror. The arguments are the path to any device 3335789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 3336789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 3337789Sahrens * 3338789Sahrens * If 'replacing' is specified, the new device is intended to replace the 3339789Sahrens * existing device; in this case the two devices are made into their own 33404451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 3341789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 3342789Sahrens * extra rules: you can't attach to it after it's been created, and upon 3343789Sahrens * completion of resilvering, the first disk (the one being replaced) 3344789Sahrens * is automatically detached. 3345789Sahrens */ 3346789Sahrens int 33471544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3348789Sahrens { 3349789Sahrens uint64_t txg, open_txg; 3350789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3351789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 33522082Seschrock vdev_ops_t *pvops; 33537313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 33547313SEric.Kustarz@Sun.COM int newvd_isspare; 33557313SEric.Kustarz@Sun.COM int error; 3356789Sahrens 3357789Sahrens txg = spa_vdev_enter(spa); 3358789Sahrens 33596643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3360789Sahrens 3361789Sahrens if (oldvd == NULL) 3362789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3363789Sahrens 33641585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 33651585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 33661585Sbonwick 3367789Sahrens pvd = oldvd->vdev_parent; 3368789Sahrens 33692082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 33704451Seschrock VDEV_ALLOC_ADD)) != 0) 33714451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 33724451Seschrock 33734451Seschrock if (newrootvd->vdev_children != 1) 3374789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3375789Sahrens 3376789Sahrens newvd = newrootvd->vdev_child[0]; 3377789Sahrens 3378789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3379789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3380789Sahrens 33812082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3382789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3383789Sahrens 33844527Sperrin /* 33854527Sperrin * Spares can't replace logs 33864527Sperrin */ 33877326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 33884527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 33894527Sperrin 33902082Seschrock if (!replacing) { 33912082Seschrock /* 33922082Seschrock * For attach, the only allowable parent is a mirror or the root 33932082Seschrock * vdev. 33942082Seschrock */ 33952082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 33962082Seschrock pvd->vdev_ops != &vdev_root_ops) 33972082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 33982082Seschrock 33992082Seschrock pvops = &vdev_mirror_ops; 34002082Seschrock } else { 34012082Seschrock /* 34022082Seschrock * Active hot spares can only be replaced by inactive hot 34032082Seschrock * spares. 34042082Seschrock */ 34052082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 34062082Seschrock pvd->vdev_child[1] == oldvd && 34072082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 34082082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34092082Seschrock 34102082Seschrock /* 34112082Seschrock * If the source is a hot spare, and the parent isn't already a 34122082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 34133377Seschrock * want to create a replacing vdev. The user is not allowed to 34143377Seschrock * attach to a spared vdev child unless the 'isspare' state is 34153377Seschrock * the same (spare replaces spare, non-spare replaces 34163377Seschrock * non-spare). 34172082Seschrock */ 34182082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 34192082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34203377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 34213377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 34223377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34232082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 34242082Seschrock newvd->vdev_isspare) 34252082Seschrock pvops = &vdev_spare_ops; 34262082Seschrock else 34272082Seschrock pvops = &vdev_replacing_ops; 34282082Seschrock } 34292082Seschrock 34301175Slling /* 34319816SGeorge.Wilson@Sun.COM * Make sure the new device is big enough. 34321175Slling */ 34339816SGeorge.Wilson@Sun.COM if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3434789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3435789Sahrens 34361732Sbonwick /* 34371732Sbonwick * The new device cannot have a higher alignment requirement 34381732Sbonwick * than the top-level vdev. 34391732Sbonwick */ 34401732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3441789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3442789Sahrens 3443789Sahrens /* 3444789Sahrens * If this is an in-place replacement, update oldvd's path and devid 3445789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3446789Sahrens */ 3447789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3448789Sahrens spa_strfree(oldvd->vdev_path); 3449789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3450789Sahrens KM_SLEEP); 3451789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3452789Sahrens newvd->vdev_path, "old"); 3453789Sahrens if (oldvd->vdev_devid != NULL) { 3454789Sahrens spa_strfree(oldvd->vdev_devid); 3455789Sahrens oldvd->vdev_devid = NULL; 3456789Sahrens } 3457789Sahrens } 3458789Sahrens 3459789Sahrens /* 34602082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 34612082Seschrock * mirror/replacing/spare vdev above oldvd. 3462789Sahrens */ 3463789Sahrens if (pvd->vdev_ops != pvops) 3464789Sahrens pvd = vdev_add_parent(oldvd, pvops); 3465789Sahrens 3466789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3467789Sahrens ASSERT(pvd->vdev_ops == pvops); 3468789Sahrens ASSERT(oldvd->vdev_parent == pvd); 3469789Sahrens 3470789Sahrens /* 3471789Sahrens * Extract the new device from its root and add it to pvd. 3472789Sahrens */ 3473789Sahrens vdev_remove_child(newrootvd, newvd); 3474789Sahrens newvd->vdev_id = pvd->vdev_children; 347510594SGeorge.Wilson@Sun.COM newvd->vdev_crtxg = oldvd->vdev_crtxg; 3476789Sahrens vdev_add_child(pvd, newvd); 3477789Sahrens 3478789Sahrens tvd = newvd->vdev_top; 3479789Sahrens ASSERT(pvd->vdev_top == tvd); 3480789Sahrens ASSERT(tvd->vdev_parent == rvd); 3481789Sahrens 3482789Sahrens vdev_config_dirty(tvd); 3483789Sahrens 3484789Sahrens /* 3485789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3486789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3487789Sahrens */ 3488789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 3489789Sahrens 34908241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, 34918241SJeff.Bonwick@Sun.COM TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3492789Sahrens 34939425SEric.Schrock@Sun.COM if (newvd->vdev_isspare) { 34943377Seschrock spa_spare_activate(newvd); 34959425SEric.Schrock@Sun.COM spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 34969425SEric.Schrock@Sun.COM } 34979425SEric.Schrock@Sun.COM 34987754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 34997754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 35007313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 35011544Seschrock 3502789Sahrens /* 3503789Sahrens * Mark newvd's DTL dirty in this txg. 3504789Sahrens */ 35051732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3506789Sahrens 3507789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3508789Sahrens 35099946SMark.Musante@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL, 35109946SMark.Musante@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 35119946SMark.Musante@Sun.COM replacing && newvd_isspare ? "spare in" : 35129946SMark.Musante@Sun.COM replacing ? "replace" : "attach", newvdpath, 35139946SMark.Musante@Sun.COM replacing ? "for" : "to", oldvdpath); 35147313SEric.Kustarz@Sun.COM 35157313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 35167313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 35177313SEric.Kustarz@Sun.COM 3518789Sahrens /* 35197046Sahrens * Kick off a resilver to update newvd. 3520789Sahrens */ 35217046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3522789Sahrens 3523789Sahrens return (0); 3524789Sahrens } 3525789Sahrens 3526789Sahrens /* 3527789Sahrens * Detach a device from a mirror or replacing vdev. 3528789Sahrens * If 'replace_done' is specified, only detach if the parent 3529789Sahrens * is a replacing vdev. 3530789Sahrens */ 3531789Sahrens int 35328241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3533789Sahrens { 3534789Sahrens uint64_t txg; 35358241SJeff.Bonwick@Sun.COM int error; 3536789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3537789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 35382082Seschrock boolean_t unspare = B_FALSE; 35392082Seschrock uint64_t unspare_guid; 35406673Seschrock size_t len; 3541789Sahrens 3542789Sahrens txg = spa_vdev_enter(spa); 3543789Sahrens 35446643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3545789Sahrens 3546789Sahrens if (vd == NULL) 3547789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3548789Sahrens 35491585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 35501585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35511585Sbonwick 3552789Sahrens pvd = vd->vdev_parent; 3553789Sahrens 3554789Sahrens /* 35558241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 35568241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 35578241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 35588241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 35598241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 35608241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 35618241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 35628241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 35638241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 35648241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 35658241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 35668241SJeff.Bonwick@Sun.COM */ 35678241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 35688241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 35698241SJeff.Bonwick@Sun.COM 35708241SJeff.Bonwick@Sun.COM /* 3571789Sahrens * If replace_done is specified, only remove this device if it's 35722082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 35732082Seschrock * disk can be removed. 3574789Sahrens */ 35752082Seschrock if (replace_done) { 35762082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 35772082Seschrock if (vd->vdev_id != 0) 35782082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35792082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 35802082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35812082Seschrock } 35822082Seschrock } 35832082Seschrock 35842082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 35854577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3586789Sahrens 3587789Sahrens /* 35882082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3589789Sahrens */ 3590789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 35912082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 35922082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3593789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3594789Sahrens 3595789Sahrens /* 35968241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 35978241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 3598789Sahrens */ 35998241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 3600789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3601789Sahrens 36028241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 36038241SJeff.Bonwick@Sun.COM 3604789Sahrens /* 36056673Seschrock * If we are detaching the second disk from a replacing vdev, then 36066673Seschrock * check to see if we changed the original vdev's path to have "/old" 36076673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 36086673Seschrock */ 36096673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 36106673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 36116673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 36126673Seschrock ASSERT(pvd->vdev_child[1] == vd); 36136673Seschrock cvd = pvd->vdev_child[0]; 36146673Seschrock len = strlen(vd->vdev_path); 36156673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 36166673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 36176673Seschrock spa_strfree(cvd->vdev_path); 36186673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 36196673Seschrock } 36206673Seschrock } 36216673Seschrock 36226673Seschrock /* 36232082Seschrock * If we are detaching the original disk from a spare, then it implies 36242082Seschrock * that the spare should become a real disk, and be removed from the 36252082Seschrock * active spare list for the pool. 36262082Seschrock */ 36272082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 36288241SJeff.Bonwick@Sun.COM vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 36292082Seschrock unspare = B_TRUE; 36302082Seschrock 36312082Seschrock /* 3632789Sahrens * Erase the disk labels so the disk can be used for other things. 3633789Sahrens * This must be done after all other error cases are handled, 3634789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3635789Sahrens * But if we can't do it, don't treat the error as fatal -- 3636789Sahrens * it may be that the unwritability of the disk is the reason 3637789Sahrens * it's being detached! 3638789Sahrens */ 36393377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3640789Sahrens 3641789Sahrens /* 3642789Sahrens * Remove vd from its parent and compact the parent's children. 3643789Sahrens */ 3644789Sahrens vdev_remove_child(pvd, vd); 3645789Sahrens vdev_compact_children(pvd); 3646789Sahrens 3647789Sahrens /* 3648789Sahrens * Remember one of the remaining children so we can get tvd below. 3649789Sahrens */ 3650789Sahrens cvd = pvd->vdev_child[0]; 3651789Sahrens 3652789Sahrens /* 36532082Seschrock * If we need to remove the remaining child from the list of hot spares, 36548241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 36558241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 36568241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 36578241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 36588241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 36598241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 36602082Seschrock */ 36612082Seschrock if (unspare) { 36622082Seschrock ASSERT(cvd->vdev_isspare); 36633377Seschrock spa_spare_remove(cvd); 36642082Seschrock unspare_guid = cvd->vdev_guid; 36658241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 36662082Seschrock } 36672082Seschrock 36682082Seschrock /* 3669789Sahrens * If the parent mirror/replacing vdev only has one child, 3670789Sahrens * the parent is no longer needed. Remove it from the tree. 3671789Sahrens */ 3672789Sahrens if (pvd->vdev_children == 1) 3673789Sahrens vdev_remove_parent(cvd); 3674789Sahrens 3675789Sahrens /* 3676789Sahrens * We don't set tvd until now because the parent we just removed 3677789Sahrens * may have been the previous top-level vdev. 3678789Sahrens */ 3679789Sahrens tvd = cvd->vdev_top; 3680789Sahrens ASSERT(tvd->vdev_parent == rvd); 3681789Sahrens 3682789Sahrens /* 36833377Seschrock * Reevaluate the parent vdev state. 3684789Sahrens */ 36854451Seschrock vdev_propagate_state(cvd); 3686789Sahrens 3687789Sahrens /* 36889816SGeorge.Wilson@Sun.COM * If the 'autoexpand' property is set on the pool then automatically 36899816SGeorge.Wilson@Sun.COM * try to expand the size of the pool. For example if the device we 36909816SGeorge.Wilson@Sun.COM * just detached was smaller than the others, it may be possible to 36919816SGeorge.Wilson@Sun.COM * add metaslabs (i.e. grow the pool). We need to reopen the vdev 36929816SGeorge.Wilson@Sun.COM * first so that we can obtain the updated sizes of the leaf vdevs. 3693789Sahrens */ 36949816SGeorge.Wilson@Sun.COM if (spa->spa_autoexpand) { 36959816SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 36969816SGeorge.Wilson@Sun.COM vdev_expand(tvd, txg); 36979816SGeorge.Wilson@Sun.COM } 3698789Sahrens 3699789Sahrens vdev_config_dirty(tvd); 3700789Sahrens 3701789Sahrens /* 37023377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 37033377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 37043377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 37053377Seschrock * prevent vd from being accessed after it's freed. 3706789Sahrens */ 37078241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 3708789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 37091732Sbonwick vd->vdev_detached = B_TRUE; 37101732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3711789Sahrens 37124451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 37134451Seschrock 37142082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 37152082Seschrock 37162082Seschrock /* 37173377Seschrock * If this was the removal of the original device in a hot spare vdev, 37183377Seschrock * then we want to go through and remove the device from the hot spare 37193377Seschrock * list of every other pool. 37202082Seschrock */ 37212082Seschrock if (unspare) { 37228241SJeff.Bonwick@Sun.COM spa_t *myspa = spa; 37232082Seschrock spa = NULL; 37242082Seschrock mutex_enter(&spa_namespace_lock); 37252082Seschrock while ((spa = spa_next(spa)) != NULL) { 37262082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 37272082Seschrock continue; 37288241SJeff.Bonwick@Sun.COM if (spa == myspa) 37298241SJeff.Bonwick@Sun.COM continue; 37307793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 37317793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 37322082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 37337793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 37347793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 37352082Seschrock } 37362082Seschrock mutex_exit(&spa_namespace_lock); 37372082Seschrock } 37382082Seschrock 37392082Seschrock return (error); 37402082Seschrock } 37412082Seschrock 37427754SJeff.Bonwick@Sun.COM static nvlist_t * 37437754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 37442082Seschrock { 37457754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 37467754SJeff.Bonwick@Sun.COM uint64_t guid; 37477754SJeff.Bonwick@Sun.COM 37487754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 37497754SJeff.Bonwick@Sun.COM &guid) == 0); 37507754SJeff.Bonwick@Sun.COM 37517754SJeff.Bonwick@Sun.COM if (guid == target_guid) 37527754SJeff.Bonwick@Sun.COM return (nvpp[i]); 37532082Seschrock } 37542082Seschrock 37557754SJeff.Bonwick@Sun.COM return (NULL); 37565450Sbrendan } 37575450Sbrendan 37587754SJeff.Bonwick@Sun.COM static void 37597754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 37607754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 37615450Sbrendan { 37627754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 37637754SJeff.Bonwick@Sun.COM 37647754SJeff.Bonwick@Sun.COM if (count > 1) 37657754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 37667754SJeff.Bonwick@Sun.COM 37677754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 37687754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 37697754SJeff.Bonwick@Sun.COM continue; 37707754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 37715450Sbrendan } 37725450Sbrendan 37737754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 37747754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 37757754SJeff.Bonwick@Sun.COM 37767754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 37777754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 37787754SJeff.Bonwick@Sun.COM 37797754SJeff.Bonwick@Sun.COM if (count > 1) 37807754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 37815450Sbrendan } 37825450Sbrendan 37835450Sbrendan /* 378410594SGeorge.Wilson@Sun.COM * Removing a device from the vdev namespace requires several steps 378510594SGeorge.Wilson@Sun.COM * and can take a significant amount of time. As a result we use 378610594SGeorge.Wilson@Sun.COM * the spa_vdev_config_[enter/exit] functions which allow us to 378710594SGeorge.Wilson@Sun.COM * grab and release the spa_config_lock while still holding the namespace 378810594SGeorge.Wilson@Sun.COM * lock. During each step the configuration is synced out. 378910594SGeorge.Wilson@Sun.COM */ 379010594SGeorge.Wilson@Sun.COM 379110594SGeorge.Wilson@Sun.COM /* 379210594SGeorge.Wilson@Sun.COM * Evacuate the device. 379310594SGeorge.Wilson@Sun.COM */ 379410594SGeorge.Wilson@Sun.COM int 379510594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 379610594SGeorge.Wilson@Sun.COM { 3797*10974SJeff.Bonwick@Sun.COM int error = 0; 379810594SGeorge.Wilson@Sun.COM uint64_t txg; 379910594SGeorge.Wilson@Sun.COM 380010594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 380110594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 380210922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 380310594SGeorge.Wilson@Sun.COM 380410594SGeorge.Wilson@Sun.COM /* 380510594SGeorge.Wilson@Sun.COM * Evacuate the device. We don't hold the config lock as writer 380610594SGeorge.Wilson@Sun.COM * since we need to do I/O but we do keep the 380710594SGeorge.Wilson@Sun.COM * spa_namespace_lock held. Once this completes the device 380810594SGeorge.Wilson@Sun.COM * should no longer have any blocks allocated on it. 380910594SGeorge.Wilson@Sun.COM */ 381010594SGeorge.Wilson@Sun.COM if (vd->vdev_islog) { 3811*10974SJeff.Bonwick@Sun.COM error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 3812*10974SJeff.Bonwick@Sun.COM NULL, DS_FIND_CHILDREN); 3813*10974SJeff.Bonwick@Sun.COM } else { 3814*10974SJeff.Bonwick@Sun.COM error = ENOTSUP; /* until we have bp rewrite */ 381510594SGeorge.Wilson@Sun.COM } 381610594SGeorge.Wilson@Sun.COM 3817*10974SJeff.Bonwick@Sun.COM txg_wait_synced(spa_get_dsl(spa), 0); 3818*10974SJeff.Bonwick@Sun.COM 3819*10974SJeff.Bonwick@Sun.COM if (error) 3820*10974SJeff.Bonwick@Sun.COM return (error); 3821*10974SJeff.Bonwick@Sun.COM 382210594SGeorge.Wilson@Sun.COM /* 3823*10974SJeff.Bonwick@Sun.COM * The evacuation succeeded. Remove any remaining MOS metadata 3824*10974SJeff.Bonwick@Sun.COM * associated with this vdev, and wait for these changes to sync. 382510594SGeorge.Wilson@Sun.COM */ 382610594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 382710594SGeorge.Wilson@Sun.COM vd->vdev_removing = B_TRUE; 382810594SGeorge.Wilson@Sun.COM vdev_dirty(vd, 0, NULL, txg); 382910594SGeorge.Wilson@Sun.COM vdev_config_dirty(vd); 383010594SGeorge.Wilson@Sun.COM spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 383110594SGeorge.Wilson@Sun.COM 383210594SGeorge.Wilson@Sun.COM return (0); 383310594SGeorge.Wilson@Sun.COM } 383410594SGeorge.Wilson@Sun.COM 383510594SGeorge.Wilson@Sun.COM /* 383610594SGeorge.Wilson@Sun.COM * Complete the removal by cleaning up the namespace. 383710594SGeorge.Wilson@Sun.COM */ 383810594SGeorge.Wilson@Sun.COM void 3839*10974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 384010594SGeorge.Wilson@Sun.COM { 384110594SGeorge.Wilson@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 384210594SGeorge.Wilson@Sun.COM uint64_t id = vd->vdev_id; 384310594SGeorge.Wilson@Sun.COM boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 384410594SGeorge.Wilson@Sun.COM 384510594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 384610594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 384710922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 384810594SGeorge.Wilson@Sun.COM 384910594SGeorge.Wilson@Sun.COM (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 385010922SJeff.Bonwick@Sun.COM 385110922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_state_dirty_node)) 385210922SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 385310922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_config_dirty_node)) 385410922SJeff.Bonwick@Sun.COM vdev_config_clean(vd); 385510922SJeff.Bonwick@Sun.COM 385610594SGeorge.Wilson@Sun.COM vdev_free(vd); 385710594SGeorge.Wilson@Sun.COM 385810594SGeorge.Wilson@Sun.COM if (last_vdev) { 385910594SGeorge.Wilson@Sun.COM vdev_compact_children(rvd); 386010594SGeorge.Wilson@Sun.COM } else { 386110594SGeorge.Wilson@Sun.COM vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 386210594SGeorge.Wilson@Sun.COM vdev_add_child(rvd, vd); 386310594SGeorge.Wilson@Sun.COM } 386410594SGeorge.Wilson@Sun.COM vdev_config_dirty(rvd); 386510594SGeorge.Wilson@Sun.COM 386610594SGeorge.Wilson@Sun.COM /* 386710594SGeorge.Wilson@Sun.COM * Reassess the health of our root vdev. 386810594SGeorge.Wilson@Sun.COM */ 386910594SGeorge.Wilson@Sun.COM vdev_reopen(rvd); 387010594SGeorge.Wilson@Sun.COM } 387110594SGeorge.Wilson@Sun.COM 387210594SGeorge.Wilson@Sun.COM /* 38735450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 387410594SGeorge.Wilson@Sun.COM * spares, slogs, and level 2 ARC devices. 38755450Sbrendan */ 38765450Sbrendan int 38775450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 38785450Sbrendan { 38795450Sbrendan vdev_t *vd; 3880*10974SJeff.Bonwick@Sun.COM metaslab_group_t *mg; 38817754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 388210594SGeorge.Wilson@Sun.COM uint64_t txg = 0; 38835450Sbrendan uint_t nspares, nl2cache; 38845450Sbrendan int error = 0; 38858241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 38868241SJeff.Bonwick@Sun.COM 38878241SJeff.Bonwick@Sun.COM if (!locked) 38888241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 38895450Sbrendan 38906643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 38915450Sbrendan 38925450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 38935450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 38947754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 38957754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 38967754SJeff.Bonwick@Sun.COM /* 38977754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 38987754SJeff.Bonwick@Sun.COM * in this pool. 38997754SJeff.Bonwick@Sun.COM */ 39007754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 39017754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 39027754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 39037754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 39047754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 39057754SJeff.Bonwick@Sun.COM } else { 39067754SJeff.Bonwick@Sun.COM error = EBUSY; 39077754SJeff.Bonwick@Sun.COM } 39087754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 39095450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 39107754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 39117754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 39127754SJeff.Bonwick@Sun.COM /* 39137754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 39147754SJeff.Bonwick@Sun.COM */ 39157754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 39167754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 39175450Sbrendan spa_load_l2cache(spa); 39185450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 391910594SGeorge.Wilson@Sun.COM } else if (vd != NULL && vd->vdev_islog) { 392010594SGeorge.Wilson@Sun.COM ASSERT(!locked); 392110922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 392210594SGeorge.Wilson@Sun.COM 392310594SGeorge.Wilson@Sun.COM /* 392410594SGeorge.Wilson@Sun.COM * XXX - Once we have bp-rewrite this should 392510594SGeorge.Wilson@Sun.COM * become the common case. 392610594SGeorge.Wilson@Sun.COM */ 392710594SGeorge.Wilson@Sun.COM 3928*10974SJeff.Bonwick@Sun.COM mg = vd->vdev_mg; 3929*10974SJeff.Bonwick@Sun.COM 393010594SGeorge.Wilson@Sun.COM /* 3931*10974SJeff.Bonwick@Sun.COM * Stop allocating from this vdev. 393210594SGeorge.Wilson@Sun.COM */ 3933*10974SJeff.Bonwick@Sun.COM metaslab_group_passivate(mg); 393410594SGeorge.Wilson@Sun.COM 393510922SJeff.Bonwick@Sun.COM /* 393610922SJeff.Bonwick@Sun.COM * Wait for the youngest allocations and frees to sync, 393710922SJeff.Bonwick@Sun.COM * and then wait for the deferral of those frees to finish. 393810922SJeff.Bonwick@Sun.COM */ 393910922SJeff.Bonwick@Sun.COM spa_vdev_config_exit(spa, NULL, 394010922SJeff.Bonwick@Sun.COM txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 394110922SJeff.Bonwick@Sun.COM 3942*10974SJeff.Bonwick@Sun.COM /* 3943*10974SJeff.Bonwick@Sun.COM * Attempt to evacuate the vdev. 3944*10974SJeff.Bonwick@Sun.COM */ 3945*10974SJeff.Bonwick@Sun.COM error = spa_vdev_remove_evacuate(spa, vd); 3946*10974SJeff.Bonwick@Sun.COM 394710594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 394810594SGeorge.Wilson@Sun.COM 3949*10974SJeff.Bonwick@Sun.COM /* 3950*10974SJeff.Bonwick@Sun.COM * If we couldn't evacuate the vdev, unwind. 3951*10974SJeff.Bonwick@Sun.COM */ 3952*10974SJeff.Bonwick@Sun.COM if (error) { 3953*10974SJeff.Bonwick@Sun.COM metaslab_group_activate(mg); 3954*10974SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 3955*10974SJeff.Bonwick@Sun.COM } 3956*10974SJeff.Bonwick@Sun.COM 3957*10974SJeff.Bonwick@Sun.COM /* 3958*10974SJeff.Bonwick@Sun.COM * Clean up the vdev namespace. 3959*10974SJeff.Bonwick@Sun.COM */ 3960*10974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa, vd); 396110594SGeorge.Wilson@Sun.COM 39627754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 39637754SJeff.Bonwick@Sun.COM /* 39647754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 39657754SJeff.Bonwick@Sun.COM */ 39667754SJeff.Bonwick@Sun.COM error = ENOTSUP; 39677754SJeff.Bonwick@Sun.COM } else { 39687754SJeff.Bonwick@Sun.COM /* 39697754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 39707754SJeff.Bonwick@Sun.COM */ 39717754SJeff.Bonwick@Sun.COM error = ENOENT; 39725450Sbrendan } 39732082Seschrock 39748241SJeff.Bonwick@Sun.COM if (!locked) 39758241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 39768241SJeff.Bonwick@Sun.COM 39778241SJeff.Bonwick@Sun.COM return (error); 3978789Sahrens } 3979789Sahrens 3980789Sahrens /* 39814451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 39824451Seschrock * current spared, so we can detach it. 3983789Sahrens */ 39841544Seschrock static vdev_t * 39854451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3986789Sahrens { 39871544Seschrock vdev_t *newvd, *oldvd; 39889816SGeorge.Wilson@Sun.COM 39899816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 39904451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 39911544Seschrock if (oldvd != NULL) 39921544Seschrock return (oldvd); 39931544Seschrock } 3994789Sahrens 39954451Seschrock /* 39964451Seschrock * Check for a completed replacement. 39974451Seschrock */ 3998789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 39991544Seschrock oldvd = vd->vdev_child[0]; 40001544Seschrock newvd = vd->vdev_child[1]; 4001789Sahrens 40028241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 40038241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 40041544Seschrock return (oldvd); 40051544Seschrock } 4006789Sahrens 40074451Seschrock /* 40084451Seschrock * Check for a completed resilver with the 'unspare' flag set. 40094451Seschrock */ 40104451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 40114451Seschrock newvd = vd->vdev_child[0]; 40124451Seschrock oldvd = vd->vdev_child[1]; 40134451Seschrock 40144451Seschrock if (newvd->vdev_unspare && 40158241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 40168241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) { 40174451Seschrock newvd->vdev_unspare = 0; 40184451Seschrock return (oldvd); 40194451Seschrock } 40204451Seschrock } 40214451Seschrock 40221544Seschrock return (NULL); 4023789Sahrens } 4024789Sahrens 40251544Seschrock static void 40264451Seschrock spa_vdev_resilver_done(spa_t *spa) 4027789Sahrens { 40288241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 40298241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 40308241SJeff.Bonwick@Sun.COM 40318241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4032789Sahrens 40334451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 40348241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 40358241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 40361544Seschrock guid = vd->vdev_guid; 40378241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 40388241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 40398241SJeff.Bonwick@Sun.COM sguid = 0; 40402082Seschrock /* 40412082Seschrock * If we have just finished replacing a hot spared device, then 40422082Seschrock * we need to detach the parent's first child (the original hot 40432082Seschrock * spare) as well. 40442082Seschrock */ 40458241SJeff.Bonwick@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 40462082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 40478241SJeff.Bonwick@Sun.COM ASSERT(ppvd->vdev_children == 2); 40488241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 40492082Seschrock } 40508241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 40518241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 40521544Seschrock return; 40538241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 40542082Seschrock return; 40558241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4056789Sahrens } 4057789Sahrens 40588241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 4059789Sahrens } 4060789Sahrens 4061789Sahrens /* 40629425SEric.Schrock@Sun.COM * Update the stored path or FRU for this vdev. Dirty the vdev configuration, 40639425SEric.Schrock@Sun.COM * relying on spa_vdev_enter/exit() to synchronize the labels and cache. 40641354Seschrock */ 40651354Seschrock int 40669425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 40679425SEric.Schrock@Sun.COM boolean_t ispath) 40681354Seschrock { 40696643Seschrock vdev_t *vd; 40701354Seschrock uint64_t txg; 40711354Seschrock 40721354Seschrock txg = spa_vdev_enter(spa); 40731354Seschrock 40749425SEric.Schrock@Sun.COM if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 40755450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 40761354Seschrock 40771585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 40781585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 40791585Sbonwick 40809425SEric.Schrock@Sun.COM if (ispath) { 40819425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_path); 40829425SEric.Schrock@Sun.COM vd->vdev_path = spa_strdup(value); 40839425SEric.Schrock@Sun.COM } else { 40849425SEric.Schrock@Sun.COM if (vd->vdev_fru != NULL) 40859425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 40869425SEric.Schrock@Sun.COM vd->vdev_fru = spa_strdup(value); 40879425SEric.Schrock@Sun.COM } 40881354Seschrock 40891354Seschrock vdev_config_dirty(vd->vdev_top); 40901354Seschrock 40911354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 40921354Seschrock } 40931354Seschrock 40949425SEric.Schrock@Sun.COM int 40959425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 40969425SEric.Schrock@Sun.COM { 40979425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 40989425SEric.Schrock@Sun.COM } 40999425SEric.Schrock@Sun.COM 41009425SEric.Schrock@Sun.COM int 41019425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 41029425SEric.Schrock@Sun.COM { 41039425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 41049425SEric.Schrock@Sun.COM } 41059425SEric.Schrock@Sun.COM 41061354Seschrock /* 4107789Sahrens * ========================================================================== 4108789Sahrens * SPA Scrubbing 4109789Sahrens * ========================================================================== 4110789Sahrens */ 4111789Sahrens 41127046Sahrens int 41137046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 4114789Sahrens { 41157754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 41164808Sek110237 4117789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 4118789Sahrens return (ENOTSUP); 4119789Sahrens 4120789Sahrens /* 41217046Sahrens * If a resilver was requested, but there is no DTL on a 41227046Sahrens * writeable leaf device, we have nothing to do. 4123789Sahrens */ 41247046Sahrens if (type == POOL_SCRUB_RESILVER && 41257046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 41267046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 41271544Seschrock return (0); 41281544Seschrock } 4129789Sahrens 41307046Sahrens if (type == POOL_SCRUB_EVERYTHING && 41317046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 41327046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 41337046Sahrens return (EBUSY); 41347046Sahrens 41357046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 41367046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 41377046Sahrens } else if (type == POOL_SCRUB_NONE) { 41387046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 41391544Seschrock } else { 41407046Sahrens return (EINVAL); 41411544Seschrock } 4142789Sahrens } 4143789Sahrens 41441544Seschrock /* 41451544Seschrock * ========================================================================== 41461544Seschrock * SPA async task processing 41471544Seschrock * ========================================================================== 41481544Seschrock */ 41491544Seschrock 41501544Seschrock static void 41514451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 4152789Sahrens { 41537361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 41547361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 41557361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 415610575SEric.Schrock@Sun.COM 415710575SEric.Schrock@Sun.COM /* 415810575SEric.Schrock@Sun.COM * We want to clear the stats, but we don't want to do a full 415910575SEric.Schrock@Sun.COM * vdev_clear() as that will cause us to throw away 416010575SEric.Schrock@Sun.COM * degraded/faulted state as well as attempt to reopen the 416110575SEric.Schrock@Sun.COM * device, all of which is a waste. 416210575SEric.Schrock@Sun.COM */ 416310575SEric.Schrock@Sun.COM vd->vdev_stat.vs_read_errors = 0; 416410575SEric.Schrock@Sun.COM vd->vdev_stat.vs_write_errors = 0; 416510575SEric.Schrock@Sun.COM vd->vdev_stat.vs_checksum_errors = 0; 416610575SEric.Schrock@Sun.COM 41677754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 41681544Seschrock } 41697361SBrendan.Gregg@Sun.COM 41707754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 41717361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 41721544Seschrock } 41731544Seschrock 41741544Seschrock static void 41757754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 41767754SJeff.Bonwick@Sun.COM { 41777754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 41787754SJeff.Bonwick@Sun.COM vd->vdev_probe_wanted = 0; 41797754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 41807754SJeff.Bonwick@Sun.COM } 41817754SJeff.Bonwick@Sun.COM 41827754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 41837754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 41847754SJeff.Bonwick@Sun.COM } 41857754SJeff.Bonwick@Sun.COM 41867754SJeff.Bonwick@Sun.COM static void 41879816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd) 41889816SGeorge.Wilson@Sun.COM { 41899816SGeorge.Wilson@Sun.COM sysevent_id_t eid; 41909816SGeorge.Wilson@Sun.COM nvlist_t *attr; 41919816SGeorge.Wilson@Sun.COM char *physpath; 41929816SGeorge.Wilson@Sun.COM 41939816SGeorge.Wilson@Sun.COM if (!spa->spa_autoexpand) 41949816SGeorge.Wilson@Sun.COM return; 41959816SGeorge.Wilson@Sun.COM 41969816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 41979816SGeorge.Wilson@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 41989816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, cvd); 41999816SGeorge.Wilson@Sun.COM } 42009816SGeorge.Wilson@Sun.COM 42019816SGeorge.Wilson@Sun.COM if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 42029816SGeorge.Wilson@Sun.COM return; 42039816SGeorge.Wilson@Sun.COM 42049816SGeorge.Wilson@Sun.COM physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 42059816SGeorge.Wilson@Sun.COM (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 42069816SGeorge.Wilson@Sun.COM 42079816SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 42089816SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 42099816SGeorge.Wilson@Sun.COM 42109816SGeorge.Wilson@Sun.COM (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 42119816SGeorge.Wilson@Sun.COM ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 42129816SGeorge.Wilson@Sun.COM 42139816SGeorge.Wilson@Sun.COM nvlist_free(attr); 42149816SGeorge.Wilson@Sun.COM kmem_free(physpath, MAXPATHLEN); 42159816SGeorge.Wilson@Sun.COM } 42169816SGeorge.Wilson@Sun.COM 42179816SGeorge.Wilson@Sun.COM static void 42181544Seschrock spa_async_thread(spa_t *spa) 42191544Seschrock { 42207754SJeff.Bonwick@Sun.COM int tasks; 42211544Seschrock 42221544Seschrock ASSERT(spa->spa_sync_on); 4223789Sahrens 42241544Seschrock mutex_enter(&spa->spa_async_lock); 42251544Seschrock tasks = spa->spa_async_tasks; 42261544Seschrock spa->spa_async_tasks = 0; 42271544Seschrock mutex_exit(&spa->spa_async_lock); 42281544Seschrock 42291544Seschrock /* 42301635Sbonwick * See if the config needs to be updated. 42311635Sbonwick */ 42321635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 423310922SJeff.Bonwick@Sun.COM uint64_t old_space, new_space; 42349816SGeorge.Wilson@Sun.COM 42351635Sbonwick mutex_enter(&spa_namespace_lock); 423610922SJeff.Bonwick@Sun.COM old_space = metaslab_class_get_space(spa_normal_class(spa)); 42371635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 423810922SJeff.Bonwick@Sun.COM new_space = metaslab_class_get_space(spa_normal_class(spa)); 42391635Sbonwick mutex_exit(&spa_namespace_lock); 42409816SGeorge.Wilson@Sun.COM 42419816SGeorge.Wilson@Sun.COM /* 42429816SGeorge.Wilson@Sun.COM * If the pool grew as a result of the config update, 42439816SGeorge.Wilson@Sun.COM * then log an internal history event. 42449816SGeorge.Wilson@Sun.COM */ 424510922SJeff.Bonwick@Sun.COM if (new_space != old_space) { 42469946SMark.Musante@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ONLINE, 42479946SMark.Musante@Sun.COM spa, NULL, CRED(), 42489946SMark.Musante@Sun.COM "pool '%s' size: %llu(+%llu)", 424910922SJeff.Bonwick@Sun.COM spa_name(spa), new_space, new_space - old_space); 42509816SGeorge.Wilson@Sun.COM } 42511635Sbonwick } 42521635Sbonwick 42531635Sbonwick /* 42544451Seschrock * See if any devices need to be marked REMOVED. 42551544Seschrock */ 42567754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 425710685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 42584451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 42597754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 42607361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 42617754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 42627361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 42637754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 42647754SJeff.Bonwick@Sun.COM } 42657754SJeff.Bonwick@Sun.COM 42669816SGeorge.Wilson@Sun.COM if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 42679816SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 42689816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, spa->spa_root_vdev); 42699816SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 42709816SGeorge.Wilson@Sun.COM } 42719816SGeorge.Wilson@Sun.COM 42727754SJeff.Bonwick@Sun.COM /* 42737754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 42747754SJeff.Bonwick@Sun.COM */ 42757754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 427610685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 42777754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 42787754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 42794451Seschrock } 42801544Seschrock 42811544Seschrock /* 42821544Seschrock * If any devices are done replacing, detach them. 42831544Seschrock */ 42844451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 42854451Seschrock spa_vdev_resilver_done(spa); 4286789Sahrens 42871544Seschrock /* 42881544Seschrock * Kick off a resilver. 42891544Seschrock */ 42907046Sahrens if (tasks & SPA_ASYNC_RESILVER) 42917046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 42921544Seschrock 42931544Seschrock /* 42941544Seschrock * Let the world know that we're done. 42951544Seschrock */ 42961544Seschrock mutex_enter(&spa->spa_async_lock); 42971544Seschrock spa->spa_async_thread = NULL; 42981544Seschrock cv_broadcast(&spa->spa_async_cv); 42991544Seschrock mutex_exit(&spa->spa_async_lock); 43001544Seschrock thread_exit(); 43011544Seschrock } 43021544Seschrock 43031544Seschrock void 43041544Seschrock spa_async_suspend(spa_t *spa) 43051544Seschrock { 43061544Seschrock mutex_enter(&spa->spa_async_lock); 43071544Seschrock spa->spa_async_suspended++; 43081544Seschrock while (spa->spa_async_thread != NULL) 43091544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 43101544Seschrock mutex_exit(&spa->spa_async_lock); 43111544Seschrock } 43121544Seschrock 43131544Seschrock void 43141544Seschrock spa_async_resume(spa_t *spa) 43151544Seschrock { 43161544Seschrock mutex_enter(&spa->spa_async_lock); 43171544Seschrock ASSERT(spa->spa_async_suspended != 0); 43181544Seschrock spa->spa_async_suspended--; 43191544Seschrock mutex_exit(&spa->spa_async_lock); 43201544Seschrock } 43211544Seschrock 43221544Seschrock static void 43231544Seschrock spa_async_dispatch(spa_t *spa) 43241544Seschrock { 43251544Seschrock mutex_enter(&spa->spa_async_lock); 43261544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 43271635Sbonwick spa->spa_async_thread == NULL && 43281635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 43291544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 43301544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 43311544Seschrock mutex_exit(&spa->spa_async_lock); 43321544Seschrock } 43331544Seschrock 43341544Seschrock void 43351544Seschrock spa_async_request(spa_t *spa, int task) 43361544Seschrock { 43371544Seschrock mutex_enter(&spa->spa_async_lock); 43381544Seschrock spa->spa_async_tasks |= task; 43391544Seschrock mutex_exit(&spa->spa_async_lock); 4340789Sahrens } 4341789Sahrens 4342789Sahrens /* 4343789Sahrens * ========================================================================== 4344789Sahrens * SPA syncing routines 4345789Sahrens * ========================================================================== 4346789Sahrens */ 4347789Sahrens static void 434810922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg) 4349789Sahrens { 4350789Sahrens blkptr_t blk; 4351789Sahrens uint64_t itor = 0; 4352789Sahrens uint8_t c = 1; 4353789Sahrens 43547754SJeff.Bonwick@Sun.COM while (bplist_iterate(bpl, &itor, &blk) == 0) { 43557754SJeff.Bonwick@Sun.COM ASSERT(blk.blk_birth < txg); 435610922SJeff.Bonwick@Sun.COM zio_free(spa, txg, &blk); 43577754SJeff.Bonwick@Sun.COM } 4358789Sahrens 4359789Sahrens bplist_vacate(bpl, tx); 4360789Sahrens 4361789Sahrens /* 4362789Sahrens * Pre-dirty the first block so we sync to convergence faster. 4363789Sahrens * (Usually only the first block is needed.) 4364789Sahrens */ 436510922SJeff.Bonwick@Sun.COM dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx); 436610922SJeff.Bonwick@Sun.COM } 436710922SJeff.Bonwick@Sun.COM 436810922SJeff.Bonwick@Sun.COM static void 436910922SJeff.Bonwick@Sun.COM spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 437010922SJeff.Bonwick@Sun.COM { 437110922SJeff.Bonwick@Sun.COM zio_t *zio = arg; 437210922SJeff.Bonwick@Sun.COM 437310922SJeff.Bonwick@Sun.COM zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 437410922SJeff.Bonwick@Sun.COM zio->io_flags)); 4375789Sahrens } 4376789Sahrens 4377789Sahrens static void 43782082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 43792082Seschrock { 43802082Seschrock char *packed = NULL; 43817497STim.Haley@Sun.COM size_t bufsize; 43822082Seschrock size_t nvsize = 0; 43832082Seschrock dmu_buf_t *db; 43842082Seschrock 43852082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 43862082Seschrock 43877497STim.Haley@Sun.COM /* 43887497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 43897497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 43907497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 43917497STim.Haley@Sun.COM */ 43927497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 43937497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 43942082Seschrock 43952082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 43962082Seschrock KM_SLEEP) == 0); 43977497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 43987497STim.Haley@Sun.COM 43997497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 44007497STim.Haley@Sun.COM 44017497STim.Haley@Sun.COM kmem_free(packed, bufsize); 44022082Seschrock 44032082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 44042082Seschrock dmu_buf_will_dirty(db, tx); 44052082Seschrock *(uint64_t *)db->db_data = nvsize; 44062082Seschrock dmu_buf_rele(db, FTAG); 44072082Seschrock } 44082082Seschrock 44092082Seschrock static void 44105450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 44115450Sbrendan const char *config, const char *entry) 44122082Seschrock { 44132082Seschrock nvlist_t *nvroot; 44145450Sbrendan nvlist_t **list; 44152082Seschrock int i; 44162082Seschrock 44175450Sbrendan if (!sav->sav_sync) 44182082Seschrock return; 44192082Seschrock 44202082Seschrock /* 44215450Sbrendan * Update the MOS nvlist describing the list of available devices. 44225450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 44234451Seschrock * valid and the vdevs are labeled appropriately. 44242082Seschrock */ 44255450Sbrendan if (sav->sav_object == 0) { 44265450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 44275450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 44285450Sbrendan sizeof (uint64_t), tx); 44292082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 44305450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 44315450Sbrendan &sav->sav_object, tx) == 0); 44322082Seschrock } 44332082Seschrock 44342082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 44355450Sbrendan if (sav->sav_count == 0) { 44365450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 44372082Seschrock } else { 44385450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 44395450Sbrendan for (i = 0; i < sav->sav_count; i++) 44405450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 44415450Sbrendan B_FALSE, B_FALSE, B_TRUE); 44425450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 44435450Sbrendan sav->sav_count) == 0); 44445450Sbrendan for (i = 0; i < sav->sav_count; i++) 44455450Sbrendan nvlist_free(list[i]); 44465450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 44472082Seschrock } 44482082Seschrock 44495450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 44502926Sek110237 nvlist_free(nvroot); 44512082Seschrock 44525450Sbrendan sav->sav_sync = B_FALSE; 44532082Seschrock } 44542082Seschrock 44552082Seschrock static void 4456789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 4457789Sahrens { 4458789Sahrens nvlist_t *config; 4459789Sahrens 44607754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 4461789Sahrens return; 4462789Sahrens 44637754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 44647754SJeff.Bonwick@Sun.COM 44657754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 44667754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 44677754SJeff.Bonwick@Sun.COM 44687754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 4469789Sahrens 44701635Sbonwick if (spa->spa_config_syncing) 44711635Sbonwick nvlist_free(spa->spa_config_syncing); 44721635Sbonwick spa->spa_config_syncing = config; 4473789Sahrens 44742082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 4475789Sahrens } 4476789Sahrens 44775094Slling /* 44785094Slling * Set zpool properties. 44795094Slling */ 44803912Slling static void 44814543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 44823912Slling { 44833912Slling spa_t *spa = arg1; 44845094Slling objset_t *mos = spa->spa_meta_objset; 44853912Slling nvlist_t *nvp = arg2; 44865094Slling nvpair_t *elem; 44874451Seschrock uint64_t intval; 44886643Seschrock char *strval; 44895094Slling zpool_prop_t prop; 44905094Slling const char *propname; 44915094Slling zprop_type_t proptype; 44925094Slling 44937754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 44947754SJeff.Bonwick@Sun.COM 44955094Slling elem = NULL; 44965094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 44975094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 44985094Slling case ZPOOL_PROP_VERSION: 44995094Slling /* 45005094Slling * Only set version for non-zpool-creation cases 45015094Slling * (set/import). spa_create() needs special care 45025094Slling * for version setting. 45035094Slling */ 45045094Slling if (tx->tx_txg != TXG_INITIAL) { 45055094Slling VERIFY(nvpair_value_uint64(elem, 45065094Slling &intval) == 0); 45075094Slling ASSERT(intval <= SPA_VERSION); 45085094Slling ASSERT(intval >= spa_version(spa)); 45095094Slling spa->spa_uberblock.ub_version = intval; 45105094Slling vdev_config_dirty(spa->spa_root_vdev); 45115094Slling } 45125094Slling break; 45135094Slling 45145094Slling case ZPOOL_PROP_ALTROOT: 45155094Slling /* 45165094Slling * 'altroot' is a non-persistent property. It should 45175094Slling * have been set temporarily at creation or import time. 45185094Slling */ 45195094Slling ASSERT(spa->spa_root != NULL); 45205094Slling break; 45215094Slling 45225363Seschrock case ZPOOL_PROP_CACHEFILE: 45235094Slling /* 45248525SEric.Schrock@Sun.COM * 'cachefile' is also a non-persisitent property. 45255094Slling */ 45264543Smarks break; 45275094Slling default: 45285094Slling /* 45295094Slling * Set pool property values in the poolprops mos object. 45305094Slling */ 45315094Slling if (spa->spa_pool_props_object == 0) { 45325094Slling VERIFY((spa->spa_pool_props_object = 45335094Slling zap_create(mos, DMU_OT_POOL_PROPS, 45345094Slling DMU_OT_NONE, 0, tx)) > 0); 45355094Slling 45365094Slling VERIFY(zap_update(mos, 45375094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 45385094Slling 8, 1, &spa->spa_pool_props_object, tx) 45395094Slling == 0); 45405094Slling } 45415094Slling 45425094Slling /* normalize the property name */ 45435094Slling propname = zpool_prop_to_name(prop); 45445094Slling proptype = zpool_prop_get_type(prop); 45455094Slling 45465094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 45475094Slling ASSERT(proptype == PROP_TYPE_STRING); 45485094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 45495094Slling VERIFY(zap_update(mos, 45505094Slling spa->spa_pool_props_object, propname, 45515094Slling 1, strlen(strval) + 1, strval, tx) == 0); 45525094Slling 45535094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 45545094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 45555094Slling 45565094Slling if (proptype == PROP_TYPE_INDEX) { 45575094Slling const char *unused; 45585094Slling VERIFY(zpool_prop_index_to_string( 45595094Slling prop, intval, &unused) == 0); 45605094Slling } 45615094Slling VERIFY(zap_update(mos, 45625094Slling spa->spa_pool_props_object, propname, 45635094Slling 8, 1, &intval, tx) == 0); 45645094Slling } else { 45655094Slling ASSERT(0); /* not allowed */ 45665094Slling } 45675094Slling 45685329Sgw25295 switch (prop) { 45695329Sgw25295 case ZPOOL_PROP_DELEGATION: 45705094Slling spa->spa_delegation = intval; 45715329Sgw25295 break; 45725329Sgw25295 case ZPOOL_PROP_BOOTFS: 45735094Slling spa->spa_bootfs = intval; 45745329Sgw25295 break; 45755329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 45765329Sgw25295 spa->spa_failmode = intval; 45775329Sgw25295 break; 45789816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 45799816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = intval; 45809816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 45819816SGeorge.Wilson@Sun.COM break; 458210922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 458310922SJeff.Bonwick@Sun.COM spa->spa_dedup_ditto = intval; 458410922SJeff.Bonwick@Sun.COM break; 45855329Sgw25295 default: 45865329Sgw25295 break; 45875329Sgw25295 } 45883912Slling } 45895094Slling 45905094Slling /* log internal history if this is not a zpool create */ 45915094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 45925094Slling tx->tx_txg != TXG_INITIAL) { 45935094Slling spa_history_internal_log(LOG_POOL_PROPSET, 45945094Slling spa, tx, cr, "%s %lld %s", 45957754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 45965094Slling } 45973912Slling } 45987754SJeff.Bonwick@Sun.COM 45997754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 46003912Slling } 46013912Slling 4602789Sahrens /* 4603789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4604789Sahrens * part of the process, so we iterate until it converges. 4605789Sahrens */ 4606789Sahrens void 4607789Sahrens spa_sync(spa_t *spa, uint64_t txg) 4608789Sahrens { 4609789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4610789Sahrens objset_t *mos = spa->spa_meta_objset; 461110922SJeff.Bonwick@Sun.COM bplist_t *defer_bpl = &spa->spa_deferred_bplist; 461210922SJeff.Bonwick@Sun.COM bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 46131635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 4614789Sahrens vdev_t *vd; 4615789Sahrens dmu_tx_t *tx; 46167754SJeff.Bonwick@Sun.COM int error; 4617789Sahrens 4618789Sahrens /* 4619789Sahrens * Lock out configuration changes. 4620789Sahrens */ 46217754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4622789Sahrens 4623789Sahrens spa->spa_syncing_txg = txg; 4624789Sahrens spa->spa_sync_pass = 0; 4625789Sahrens 46267754SJeff.Bonwick@Sun.COM /* 46277754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 46287754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 46297754SJeff.Bonwick@Sun.COM */ 46307754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 46318241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 46328241SJeff.Bonwick@Sun.COM /* 46338241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 46348241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 46358241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 46368241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 46378241SJeff.Bonwick@Sun.COM * into the root vdev tree. 46388241SJeff.Bonwick@Sun.COM */ 46398241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 46408241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 46418241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 46428241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 46438241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 46448241SJeff.Bonwick@Sun.COM } 46458241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 46468241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 46477754SJeff.Bonwick@Sun.COM } 46487754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 46497754SJeff.Bonwick@Sun.COM 465010922SJeff.Bonwick@Sun.COM VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj)); 4651789Sahrens 46522082Seschrock tx = dmu_tx_create_assigned(dp, txg); 46532082Seschrock 46542082Seschrock /* 46554577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 46562082Seschrock * set spa_deflate if we have no raid-z vdevs. 46572082Seschrock */ 46584577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 46594577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 46602082Seschrock int i; 46612082Seschrock 46622082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 46632082Seschrock vd = rvd->vdev_child[i]; 46642082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 46652082Seschrock break; 46662082Seschrock } 46672082Seschrock if (i == rvd->vdev_children) { 46682082Seschrock spa->spa_deflate = TRUE; 46692082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 46702082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 46712082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 46722082Seschrock } 46732082Seschrock } 46742082Seschrock 46757046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 46767046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 46777046Sahrens dsl_pool_create_origin(dp, tx); 46787046Sahrens 46797046Sahrens /* Keeping the origin open increases spa_minref */ 46807046Sahrens spa->spa_minref += 3; 46817046Sahrens } 46827046Sahrens 46837046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 46847046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 46857046Sahrens dsl_pool_upgrade_clones(dp, tx); 46867046Sahrens } 46877046Sahrens 4688789Sahrens /* 4689789Sahrens * If anything has changed in this txg, push the deferred frees 4690789Sahrens * from the previous txg. If not, leave them alone so that we 4691789Sahrens * don't generate work on an otherwise idle system. 4692789Sahrens */ 4693789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 46942329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 46952329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 469610922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa, defer_bpl, tx, txg); 4697789Sahrens 4698789Sahrens /* 4699789Sahrens * Iterate to convergence. 4700789Sahrens */ 4701789Sahrens do { 470210922SJeff.Bonwick@Sun.COM int pass = ++spa->spa_sync_pass; 4703789Sahrens 4704789Sahrens spa_sync_config_object(spa, tx); 47055450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 47065450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 47075450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 47085450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 47091544Seschrock spa_errlog_sync(spa, txg); 4710789Sahrens dsl_pool_sync(dp, txg); 4711789Sahrens 471210922SJeff.Bonwick@Sun.COM if (pass <= SYNC_PASS_DEFERRED_FREE) { 471310922SJeff.Bonwick@Sun.COM zio_t *zio = zio_root(spa, NULL, NULL, 0); 471410922SJeff.Bonwick@Sun.COM bplist_sync(free_bpl, spa_sync_free, zio, tx); 471510922SJeff.Bonwick@Sun.COM VERIFY(zio_wait(zio) == 0); 471610922SJeff.Bonwick@Sun.COM } else { 471710922SJeff.Bonwick@Sun.COM bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx); 4718789Sahrens } 4719789Sahrens 472010922SJeff.Bonwick@Sun.COM ddt_sync(spa, txg); 472110922SJeff.Bonwick@Sun.COM 472210922SJeff.Bonwick@Sun.COM while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 472310922SJeff.Bonwick@Sun.COM vdev_sync(vd, txg); 472410922SJeff.Bonwick@Sun.COM 472510922SJeff.Bonwick@Sun.COM } while (dmu_objset_is_dirty(mos, txg)); 472610922SJeff.Bonwick@Sun.COM 472710922SJeff.Bonwick@Sun.COM ASSERT(free_bpl->bpl_queue == NULL); 472810922SJeff.Bonwick@Sun.COM 472910922SJeff.Bonwick@Sun.COM bplist_close(defer_bpl); 4730789Sahrens 4731789Sahrens /* 4732789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4733789Sahrens * to commit the transaction group. 47341635Sbonwick * 47355688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 47365688Sbonwick * random top-level vdevs that are known to be visible in the 47377754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 47387754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4739789Sahrens */ 47407754SJeff.Bonwick@Sun.COM for (;;) { 47417754SJeff.Bonwick@Sun.COM /* 47427754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 47437754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 47447754SJeff.Bonwick@Sun.COM */ 47457754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 47467754SJeff.Bonwick@Sun.COM 47477754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 47487754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 47497754SJeff.Bonwick@Sun.COM int svdcount = 0; 47507754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 47517754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 47529816SGeorge.Wilson@Sun.COM 47539816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 47547754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 47557754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 47567754SJeff.Bonwick@Sun.COM continue; 47577754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 47587754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 47597754SJeff.Bonwick@Sun.COM break; 47607754SJeff.Bonwick@Sun.COM } 47619725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 47629725SEric.Schrock@Sun.COM if (error != 0) 47639725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, 47649725SEric.Schrock@Sun.COM B_TRUE); 47657754SJeff.Bonwick@Sun.COM } else { 47667754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 47679725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_FALSE); 47689725SEric.Schrock@Sun.COM if (error != 0) 47699725SEric.Schrock@Sun.COM error = vdev_config_sync(rvd->vdev_child, 47709725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_TRUE); 47711635Sbonwick } 47727754SJeff.Bonwick@Sun.COM 47737754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 47747754SJeff.Bonwick@Sun.COM 47757754SJeff.Bonwick@Sun.COM if (error == 0) 47767754SJeff.Bonwick@Sun.COM break; 47777754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 47787754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 47791635Sbonwick } 47802082Seschrock dmu_tx_commit(tx); 47812082Seschrock 47821635Sbonwick /* 47831635Sbonwick * Clear the dirty config list. 47841635Sbonwick */ 47857754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 47861635Sbonwick vdev_config_clean(vd); 47871635Sbonwick 47881635Sbonwick /* 47891635Sbonwick * Now that the new config has synced transactionally, 47901635Sbonwick * let it become visible to the config cache. 47911635Sbonwick */ 47921635Sbonwick if (spa->spa_config_syncing != NULL) { 47931635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 47941635Sbonwick spa->spa_config_txg = txg; 47951635Sbonwick spa->spa_config_syncing = NULL; 47961635Sbonwick } 4797789Sahrens 4798789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4799789Sahrens 480010922SJeff.Bonwick@Sun.COM dsl_pool_sync_done(dp, txg); 4801789Sahrens 4802789Sahrens /* 4803789Sahrens * Update usable space statistics. 4804789Sahrens */ 4805789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4806789Sahrens vdev_sync_done(vd, txg); 4807789Sahrens 480810956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 480910956SGeorge.Wilson@Sun.COM 4810789Sahrens /* 4811789Sahrens * It had better be the case that we didn't dirty anything 48122082Seschrock * since vdev_config_sync(). 4813789Sahrens */ 4814789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4815789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4816789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 481710922SJeff.Bonwick@Sun.COM ASSERT(defer_bpl->bpl_queue == NULL); 481810922SJeff.Bonwick@Sun.COM ASSERT(free_bpl->bpl_queue == NULL); 481910922SJeff.Bonwick@Sun.COM 482010922SJeff.Bonwick@Sun.COM spa->spa_sync_pass = 0; 4821789Sahrens 48227754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 48231544Seschrock 482410921STim.Haley@Sun.COM spa_handle_ignored_writes(spa); 482510921STim.Haley@Sun.COM 48261544Seschrock /* 48271544Seschrock * If any async tasks have been requested, kick them off. 48281544Seschrock */ 48291544Seschrock spa_async_dispatch(spa); 4830789Sahrens } 4831789Sahrens 4832789Sahrens /* 4833789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4834789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4835789Sahrens * sync. 4836789Sahrens */ 4837789Sahrens void 4838789Sahrens spa_sync_allpools(void) 4839789Sahrens { 4840789Sahrens spa_t *spa = NULL; 4841789Sahrens mutex_enter(&spa_namespace_lock); 4842789Sahrens while ((spa = spa_next(spa)) != NULL) { 48437754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4844789Sahrens continue; 4845789Sahrens spa_open_ref(spa, FTAG); 4846789Sahrens mutex_exit(&spa_namespace_lock); 4847789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4848789Sahrens mutex_enter(&spa_namespace_lock); 4849789Sahrens spa_close(spa, FTAG); 4850789Sahrens } 4851789Sahrens mutex_exit(&spa_namespace_lock); 4852789Sahrens } 4853789Sahrens 4854789Sahrens /* 4855789Sahrens * ========================================================================== 4856789Sahrens * Miscellaneous routines 4857789Sahrens * ========================================================================== 4858789Sahrens */ 4859789Sahrens 4860789Sahrens /* 4861789Sahrens * Remove all pools in the system. 4862789Sahrens */ 4863789Sahrens void 4864789Sahrens spa_evict_all(void) 4865789Sahrens { 4866789Sahrens spa_t *spa; 4867789Sahrens 4868789Sahrens /* 4869789Sahrens * Remove all cached state. All pools should be closed now, 4870789Sahrens * so every spa in the AVL tree should be unreferenced. 4871789Sahrens */ 4872789Sahrens mutex_enter(&spa_namespace_lock); 4873789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4874789Sahrens /* 48751544Seschrock * Stop async tasks. The async thread may need to detach 48761544Seschrock * a device that's been replaced, which requires grabbing 48771544Seschrock * spa_namespace_lock, so we must drop it here. 4878789Sahrens */ 4879789Sahrens spa_open_ref(spa, FTAG); 4880789Sahrens mutex_exit(&spa_namespace_lock); 48811544Seschrock spa_async_suspend(spa); 48824808Sek110237 mutex_enter(&spa_namespace_lock); 4883789Sahrens spa_close(spa, FTAG); 4884789Sahrens 4885789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4886789Sahrens spa_unload(spa); 4887789Sahrens spa_deactivate(spa); 4888789Sahrens } 4889789Sahrens spa_remove(spa); 4890789Sahrens } 4891789Sahrens mutex_exit(&spa_namespace_lock); 4892789Sahrens } 48931544Seschrock 48941544Seschrock vdev_t * 48959425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 48961544Seschrock { 48976643Seschrock vdev_t *vd; 48986643Seschrock int i; 48996643Seschrock 49006643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 49016643Seschrock return (vd); 49026643Seschrock 49039425SEric.Schrock@Sun.COM if (aux) { 49046643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 49056643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 49066643Seschrock if (vd->vdev_guid == guid) 49076643Seschrock return (vd); 49086643Seschrock } 49099425SEric.Schrock@Sun.COM 49109425SEric.Schrock@Sun.COM for (i = 0; i < spa->spa_spares.sav_count; i++) { 49119425SEric.Schrock@Sun.COM vd = spa->spa_spares.sav_vdevs[i]; 49129425SEric.Schrock@Sun.COM if (vd->vdev_guid == guid) 49139425SEric.Schrock@Sun.COM return (vd); 49149425SEric.Schrock@Sun.COM } 49156643Seschrock } 49166643Seschrock 49176643Seschrock return (NULL); 49181544Seschrock } 49191760Seschrock 49201760Seschrock void 49215094Slling spa_upgrade(spa_t *spa, uint64_t version) 49221760Seschrock { 49237754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 49241760Seschrock 49251760Seschrock /* 49261760Seschrock * This should only be called for a non-faulted pool, and since a 49271760Seschrock * future version would result in an unopenable pool, this shouldn't be 49281760Seschrock * possible. 49291760Seschrock */ 49304577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 49315094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 49325094Slling 49335094Slling spa->spa_uberblock.ub_version = version; 49341760Seschrock vdev_config_dirty(spa->spa_root_vdev); 49351760Seschrock 49367754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 49372082Seschrock 49382082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 49391760Seschrock } 49402082Seschrock 49412082Seschrock boolean_t 49422082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 49432082Seschrock { 49442082Seschrock int i; 49453377Seschrock uint64_t spareguid; 49465450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 49475450Sbrendan 49485450Sbrendan for (i = 0; i < sav->sav_count; i++) 49495450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 49502082Seschrock return (B_TRUE); 49512082Seschrock 49525450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 49535450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 49545450Sbrendan &spareguid) == 0 && spareguid == guid) 49553377Seschrock return (B_TRUE); 49563377Seschrock } 49573377Seschrock 49582082Seschrock return (B_FALSE); 49592082Seschrock } 49603912Slling 49614451Seschrock /* 49627214Slling * Check if a pool has an active shared spare device. 49637214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 49647214Slling */ 49657214Slling static boolean_t 49667214Slling spa_has_active_shared_spare(spa_t *spa) 49677214Slling { 49687214Slling int i, refcnt; 49697214Slling uint64_t pool; 49707214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 49717214Slling 49727214Slling for (i = 0; i < sav->sav_count; i++) { 49737214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 49747214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 49757214Slling refcnt > 2) 49767214Slling return (B_TRUE); 49777214Slling } 49787214Slling 49797214Slling return (B_FALSE); 49807214Slling } 49817214Slling 49827214Slling /* 49834451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 49844451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 49854451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 49864451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 49874451Seschrock * or zdb as real changes. 49884451Seschrock */ 49894451Seschrock void 49904451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 49914451Seschrock { 49924451Seschrock #ifdef _KERNEL 49934451Seschrock sysevent_t *ev; 49944451Seschrock sysevent_attr_list_t *attr = NULL; 49954451Seschrock sysevent_value_t value; 49964451Seschrock sysevent_id_t eid; 49974451Seschrock 49984451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 49994451Seschrock SE_SLEEP); 50004451Seschrock 50014451Seschrock value.value_type = SE_DATA_TYPE_STRING; 50024451Seschrock value.value.sv_string = spa_name(spa); 50034451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 50044451Seschrock goto done; 50054451Seschrock 50064451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 50074451Seschrock value.value.sv_uint64 = spa_guid(spa); 50084451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 50094451Seschrock goto done; 50104451Seschrock 50114451Seschrock if (vd) { 50124451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 50134451Seschrock value.value.sv_uint64 = vd->vdev_guid; 50144451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 50154451Seschrock SE_SLEEP) != 0) 50164451Seschrock goto done; 50174451Seschrock 50184451Seschrock if (vd->vdev_path) { 50194451Seschrock value.value_type = SE_DATA_TYPE_STRING; 50204451Seschrock value.value.sv_string = vd->vdev_path; 50214451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 50224451Seschrock &value, SE_SLEEP) != 0) 50234451Seschrock goto done; 50244451Seschrock } 50254451Seschrock } 50264451Seschrock 50275756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 50285756Seschrock goto done; 50295756Seschrock attr = NULL; 50305756Seschrock 50314451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 50324451Seschrock 50334451Seschrock done: 50344451Seschrock if (attr) 50354451Seschrock sysevent_free_attr(attr); 50364451Seschrock sysevent_free(ev); 50374451Seschrock #endif 50384451Seschrock } 5039