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> 616423Sgw25295 #include <sys/spa_boot.h> 629816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h> 63789Sahrens 648662SJordan.Vaughan@Sun.com #ifdef _KERNEL 658662SJordan.Vaughan@Sun.com #include <sys/zone.h> 6610822SJack.Meng@Sun.COM #include <sys/bootprops.h> 678662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 688662SJordan.Vaughan@Sun.com 695094Slling #include "zfs_prop.h" 705913Sperrin #include "zfs_comutil.h" 715094Slling 729515SJonathan.Adams@Sun.COM enum zti_modes { 739515SJonathan.Adams@Sun.COM zti_mode_fixed, /* value is # of threads (min 1) */ 749515SJonathan.Adams@Sun.COM zti_mode_online_percent, /* value is % of online CPUs */ 759515SJonathan.Adams@Sun.COM zti_mode_tune, /* fill from zio_taskq_tune_* */ 76*11146SGeorge.Wilson@Sun.COM zti_mode_null, /* don't create a taskq */ 779515SJonathan.Adams@Sun.COM zti_nmodes 787754SJeff.Bonwick@Sun.COM }; 792986Sek110237 80*11146SGeorge.Wilson@Sun.COM #define ZTI_FIX(n) { zti_mode_fixed, (n) } 81*11146SGeorge.Wilson@Sun.COM #define ZTI_PCT(n) { zti_mode_online_percent, (n) } 82*11146SGeorge.Wilson@Sun.COM #define ZTI_TUNE { zti_mode_tune, 0 } 83*11146SGeorge.Wilson@Sun.COM #define ZTI_NULL { zti_mode_null, 0 } 84*11146SGeorge.Wilson@Sun.COM 85*11146SGeorge.Wilson@Sun.COM #define ZTI_ONE ZTI_FIX(1) 869515SJonathan.Adams@Sun.COM 879515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info { 88*11146SGeorge.Wilson@Sun.COM enum zti_modes zti_mode; 89*11146SGeorge.Wilson@Sun.COM uint_t zti_value; 909515SJonathan.Adams@Sun.COM } zio_taskq_info_t; 919515SJonathan.Adams@Sun.COM 929515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 93*11146SGeorge.Wilson@Sun.COM "issue", "issue_high", "intr", "intr_high" 949515SJonathan.Adams@Sun.COM }; 959515SJonathan.Adams@Sun.COM 96*11146SGeorge.Wilson@Sun.COM /* 97*11146SGeorge.Wilson@Sun.COM * Define the taskq threads for the following I/O types: 98*11146SGeorge.Wilson@Sun.COM * NULL, READ, WRITE, FREE, CLAIM, and IOCTL 99*11146SGeorge.Wilson@Sun.COM */ 100*11146SGeorge.Wilson@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 101*11146SGeorge.Wilson@Sun.COM /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 102*11146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 103*11146SGeorge.Wilson@Sun.COM { ZTI_FIX(8), ZTI_NULL, ZTI_TUNE, ZTI_NULL }, 104*11146SGeorge.Wilson@Sun.COM { ZTI_TUNE, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) }, 105*11146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 106*11146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 107*11146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 1089515SJonathan.Adams@Sun.COM }; 1099515SJonathan.Adams@Sun.COM 1109515SJonathan.Adams@Sun.COM enum zti_modes zio_taskq_tune_mode = zti_mode_online_percent; 1119515SJonathan.Adams@Sun.COM uint_t zio_taskq_tune_value = 80; /* #threads = 80% of # online CPUs */ 1129515SJonathan.Adams@Sun.COM 1135094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 1147214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1155094Slling 1165094Slling /* 1175094Slling * ========================================================================== 1185094Slling * SPA properties routines 1195094Slling * ========================================================================== 1205094Slling */ 1215094Slling 1225094Slling /* 1235094Slling * Add a (source=src, propname=propval) list to an nvlist. 1245094Slling */ 1255949Slling static void 1265094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 1275094Slling uint64_t intval, zprop_source_t src) 1285094Slling { 1295094Slling const char *propname = zpool_prop_to_name(prop); 1305094Slling nvlist_t *propval; 1315949Slling 1325949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1335949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 1345949Slling 1355949Slling if (strval != NULL) 1365949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1375949Slling else 1385949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 1395949Slling 1405949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 1415094Slling nvlist_free(propval); 1425094Slling } 1435094Slling 1445094Slling /* 1455094Slling * Get property values from the spa configuration. 1465094Slling */ 1475949Slling static void 1485094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1495094Slling { 1508525SEric.Schrock@Sun.COM uint64_t size; 15110956SGeorge.Wilson@Sun.COM uint64_t alloc; 1525094Slling uint64_t cap, version; 1535094Slling zprop_source_t src = ZPROP_SRC_NONE; 1546643Seschrock spa_config_dirent_t *dp; 1555094Slling 1567754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 1577754SJeff.Bonwick@Sun.COM 1588525SEric.Schrock@Sun.COM if (spa->spa_root_vdev != NULL) { 15910956SGeorge.Wilson@Sun.COM alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 16010922SJeff.Bonwick@Sun.COM size = metaslab_class_get_space(spa_normal_class(spa)); 1618525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 1628525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 16310956SGeorge.Wilson@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 16410956SGeorge.Wilson@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 16510956SGeorge.Wilson@Sun.COM size - alloc, src); 16610956SGeorge.Wilson@Sun.COM 16710956SGeorge.Wilson@Sun.COM cap = (size == 0) ? 0 : (alloc * 100 / size); 1688525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1698525SEric.Schrock@Sun.COM 17010922SJeff.Bonwick@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 17110922SJeff.Bonwick@Sun.COM ddt_get_pool_dedup_ratio(spa), src); 17210922SJeff.Bonwick@Sun.COM 1738525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1748525SEric.Schrock@Sun.COM spa->spa_root_vdev->vdev_state, src); 1758525SEric.Schrock@Sun.COM 1768525SEric.Schrock@Sun.COM version = spa_version(spa); 1778525SEric.Schrock@Sun.COM if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1788525SEric.Schrock@Sun.COM src = ZPROP_SRC_DEFAULT; 1798525SEric.Schrock@Sun.COM else 1808525SEric.Schrock@Sun.COM src = ZPROP_SRC_LOCAL; 1818525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1828525SEric.Schrock@Sun.COM } 1835949Slling 1845949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1855949Slling 1865949Slling if (spa->spa_root != NULL) 1875949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1885949Slling 0, ZPROP_SRC_LOCAL); 1895094Slling 1906643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 1916643Seschrock if (dp->scd_path == NULL) { 1925949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1936643Seschrock "none", 0, ZPROP_SRC_LOCAL); 1946643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1955949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1966643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1975363Seschrock } 1985363Seschrock } 1995094Slling } 2005094Slling 2015094Slling /* 2025094Slling * Get zpool property values. 2035094Slling */ 2045094Slling int 2055094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 2065094Slling { 20710922SJeff.Bonwick@Sun.COM objset_t *mos = spa->spa_meta_objset; 2085094Slling zap_cursor_t zc; 2095094Slling zap_attribute_t za; 2105094Slling int err; 2115094Slling 2125949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2135094Slling 2147754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 2157754SJeff.Bonwick@Sun.COM 2165094Slling /* 2175094Slling * Get properties from the spa config. 2185094Slling */ 2195949Slling spa_prop_get_config(spa, nvp); 2205094Slling 2215094Slling /* If no pool property object, no more prop to get. */ 2225094Slling if (spa->spa_pool_props_object == 0) { 2235094Slling mutex_exit(&spa->spa_props_lock); 2245094Slling return (0); 2255094Slling } 2265094Slling 2275094Slling /* 2285094Slling * Get properties from the MOS pool property object. 2295094Slling */ 2305094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 2315094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 2325094Slling zap_cursor_advance(&zc)) { 2335094Slling uint64_t intval = 0; 2345094Slling char *strval = NULL; 2355094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 2365094Slling zpool_prop_t prop; 2375094Slling 2385094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 2395094Slling continue; 2405094Slling 2415094Slling switch (za.za_integer_length) { 2425094Slling case 8: 2435094Slling /* integer property */ 2445094Slling if (za.za_first_integer != 2455094Slling zpool_prop_default_numeric(prop)) 2465094Slling src = ZPROP_SRC_LOCAL; 2475094Slling 2485094Slling if (prop == ZPOOL_PROP_BOOTFS) { 2495094Slling dsl_pool_t *dp; 2505094Slling dsl_dataset_t *ds = NULL; 2515094Slling 2525094Slling dp = spa_get_dsl(spa); 2535094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2546689Smaybee if (err = dsl_dataset_hold_obj(dp, 2556689Smaybee za.za_first_integer, FTAG, &ds)) { 2565094Slling rw_exit(&dp->dp_config_rwlock); 2575094Slling break; 2585094Slling } 2595094Slling 2605094Slling strval = kmem_alloc( 2615094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2625094Slling KM_SLEEP); 2635094Slling dsl_dataset_name(ds, strval); 2646689Smaybee dsl_dataset_rele(ds, FTAG); 2655094Slling rw_exit(&dp->dp_config_rwlock); 2665094Slling } else { 2675094Slling strval = NULL; 2685094Slling intval = za.za_first_integer; 2695094Slling } 2705094Slling 2715949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2725094Slling 2735094Slling if (strval != NULL) 2745094Slling kmem_free(strval, 2755094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2765094Slling 2775094Slling break; 2785094Slling 2795094Slling case 1: 2805094Slling /* string property */ 2815094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2825094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2835094Slling za.za_name, 1, za.za_num_integers, strval); 2845094Slling if (err) { 2855094Slling kmem_free(strval, za.za_num_integers); 2865094Slling break; 2875094Slling } 2885949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2895094Slling kmem_free(strval, za.za_num_integers); 2905094Slling break; 2915094Slling 2925094Slling default: 2935094Slling break; 2945094Slling } 2955094Slling } 2965094Slling zap_cursor_fini(&zc); 2975094Slling mutex_exit(&spa->spa_props_lock); 2985094Slling out: 2995094Slling if (err && err != ENOENT) { 3005094Slling nvlist_free(*nvp); 3015949Slling *nvp = NULL; 3025094Slling return (err); 3035094Slling } 3045094Slling 3055094Slling return (0); 3065094Slling } 3075094Slling 3085094Slling /* 3095094Slling * Validate the given pool properties nvlist and modify the list 3105094Slling * for the property values to be set. 3115094Slling */ 3125094Slling static int 3135094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 3145094Slling { 3155094Slling nvpair_t *elem; 3165094Slling int error = 0, reset_bootfs = 0; 3175094Slling uint64_t objnum; 3185094Slling 3195094Slling elem = NULL; 3205094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3215094Slling zpool_prop_t prop; 3225094Slling char *propname, *strval; 3235094Slling uint64_t intval; 3245094Slling objset_t *os; 3255363Seschrock char *slash; 3265094Slling 3275094Slling propname = nvpair_name(elem); 3285094Slling 3295094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 3305094Slling return (EINVAL); 3315094Slling 3325094Slling switch (prop) { 3335094Slling case ZPOOL_PROP_VERSION: 3345094Slling error = nvpair_value_uint64(elem, &intval); 3355094Slling if (!error && 3365094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 3375094Slling error = EINVAL; 3385094Slling break; 3395094Slling 3405094Slling case ZPOOL_PROP_DELEGATION: 3415094Slling case ZPOOL_PROP_AUTOREPLACE: 3427538SRichard.Morris@Sun.COM case ZPOOL_PROP_LISTSNAPS: 3439816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 3445094Slling error = nvpair_value_uint64(elem, &intval); 3455094Slling if (!error && intval > 1) 3465094Slling error = EINVAL; 3475094Slling break; 3485094Slling 3495094Slling case ZPOOL_PROP_BOOTFS: 3509630SJeff.Bonwick@Sun.COM /* 3519630SJeff.Bonwick@Sun.COM * If the pool version is less than SPA_VERSION_BOOTFS, 3529630SJeff.Bonwick@Sun.COM * or the pool is still being created (version == 0), 3539630SJeff.Bonwick@Sun.COM * the bootfs property cannot be set. 3549630SJeff.Bonwick@Sun.COM */ 3555094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3565094Slling error = ENOTSUP; 3575094Slling break; 3585094Slling } 3595094Slling 3605094Slling /* 3617042Sgw25295 * Make sure the vdev config is bootable 3625094Slling */ 3637042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3645094Slling error = ENOTSUP; 3655094Slling break; 3665094Slling } 3675094Slling 3685094Slling reset_bootfs = 1; 3695094Slling 3705094Slling error = nvpair_value_string(elem, &strval); 3715094Slling 3725094Slling if (!error) { 3737042Sgw25295 uint64_t compress; 3747042Sgw25295 3755094Slling if (strval == NULL || strval[0] == '\0') { 3765094Slling objnum = zpool_prop_default_numeric( 3775094Slling ZPOOL_PROP_BOOTFS); 3785094Slling break; 3795094Slling } 3805094Slling 38110298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(strval, FTAG, &os)) 3825094Slling break; 3837042Sgw25295 38410298SMatthew.Ahrens@Sun.COM /* Must be ZPL and not gzip compressed. */ 38510298SMatthew.Ahrens@Sun.COM 38610298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 38710298SMatthew.Ahrens@Sun.COM error = ENOTSUP; 38810298SMatthew.Ahrens@Sun.COM } else if ((error = dsl_prop_get_integer(strval, 3897042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3907042Sgw25295 &compress, NULL)) == 0 && 3917042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3927042Sgw25295 error = ENOTSUP; 3937042Sgw25295 } else { 3947042Sgw25295 objnum = dmu_objset_id(os); 3957042Sgw25295 } 39610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 3975094Slling } 3985094Slling break; 3997754SJeff.Bonwick@Sun.COM 4005329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 4015329Sgw25295 error = nvpair_value_uint64(elem, &intval); 4025329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 4035329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 4045329Sgw25295 error = EINVAL; 4055329Sgw25295 4065329Sgw25295 /* 4075329Sgw25295 * This is a special case which only occurs when 4085329Sgw25295 * the pool has completely failed. This allows 4095329Sgw25295 * the user to change the in-core failmode property 4105329Sgw25295 * without syncing it out to disk (I/Os might 4115329Sgw25295 * currently be blocked). We do this by returning 4125329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 4135329Sgw25295 * into thinking we encountered a property validation 4145329Sgw25295 * error. 4155329Sgw25295 */ 4167754SJeff.Bonwick@Sun.COM if (!error && spa_suspended(spa)) { 4175329Sgw25295 spa->spa_failmode = intval; 4185329Sgw25295 error = EIO; 4195329Sgw25295 } 4205329Sgw25295 break; 4215363Seschrock 4225363Seschrock case ZPOOL_PROP_CACHEFILE: 4235363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4245363Seschrock break; 4255363Seschrock 4265363Seschrock if (strval[0] == '\0') 4275363Seschrock break; 4285363Seschrock 4295363Seschrock if (strcmp(strval, "none") == 0) 4305363Seschrock break; 4315363Seschrock 4325363Seschrock if (strval[0] != '/') { 4335363Seschrock error = EINVAL; 4345363Seschrock break; 4355363Seschrock } 4365363Seschrock 4375363Seschrock slash = strrchr(strval, '/'); 4385363Seschrock ASSERT(slash != NULL); 4395363Seschrock 4405363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4415363Seschrock strcmp(slash, "/..") == 0) 4425363Seschrock error = EINVAL; 4435363Seschrock break; 44410922SJeff.Bonwick@Sun.COM 44510922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 44610922SJeff.Bonwick@Sun.COM if (spa_version(spa) < SPA_VERSION_DEDUP) 44710922SJeff.Bonwick@Sun.COM error = ENOTSUP; 44810922SJeff.Bonwick@Sun.COM else 44910922SJeff.Bonwick@Sun.COM error = nvpair_value_uint64(elem, &intval); 45010922SJeff.Bonwick@Sun.COM if (error == 0 && 45110922SJeff.Bonwick@Sun.COM intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 45210922SJeff.Bonwick@Sun.COM error = EINVAL; 45310922SJeff.Bonwick@Sun.COM break; 4545094Slling } 4555094Slling 4565094Slling if (error) 4575094Slling break; 4585094Slling } 4595094Slling 4605094Slling if (!error && reset_bootfs) { 4615094Slling error = nvlist_remove(props, 4625094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 4635094Slling 4645094Slling if (!error) { 4655094Slling error = nvlist_add_uint64(props, 4665094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 4675094Slling } 4685094Slling } 4695094Slling 4705094Slling return (error); 4715094Slling } 4725094Slling 4738525SEric.Schrock@Sun.COM void 4748525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 4758525SEric.Schrock@Sun.COM { 4768525SEric.Schrock@Sun.COM char *cachefile; 4778525SEric.Schrock@Sun.COM spa_config_dirent_t *dp; 4788525SEric.Schrock@Sun.COM 4798525SEric.Schrock@Sun.COM if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 4808525SEric.Schrock@Sun.COM &cachefile) != 0) 4818525SEric.Schrock@Sun.COM return; 4828525SEric.Schrock@Sun.COM 4838525SEric.Schrock@Sun.COM dp = kmem_alloc(sizeof (spa_config_dirent_t), 4848525SEric.Schrock@Sun.COM KM_SLEEP); 4858525SEric.Schrock@Sun.COM 4868525SEric.Schrock@Sun.COM if (cachefile[0] == '\0') 4878525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(spa_config_path); 4888525SEric.Schrock@Sun.COM else if (strcmp(cachefile, "none") == 0) 4898525SEric.Schrock@Sun.COM dp->scd_path = NULL; 4908525SEric.Schrock@Sun.COM else 4918525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(cachefile); 4928525SEric.Schrock@Sun.COM 4938525SEric.Schrock@Sun.COM list_insert_head(&spa->spa_config_list, dp); 4948525SEric.Schrock@Sun.COM if (need_sync) 4958525SEric.Schrock@Sun.COM spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 4968525SEric.Schrock@Sun.COM } 4978525SEric.Schrock@Sun.COM 4985094Slling int 4995094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 5005094Slling { 5015094Slling int error; 5028525SEric.Schrock@Sun.COM nvpair_t *elem; 5038525SEric.Schrock@Sun.COM boolean_t need_sync = B_FALSE; 5048525SEric.Schrock@Sun.COM zpool_prop_t prop; 5055094Slling 5065094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 5075094Slling return (error); 5085094Slling 5098525SEric.Schrock@Sun.COM elem = NULL; 5108525SEric.Schrock@Sun.COM while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 5118525SEric.Schrock@Sun.COM if ((prop = zpool_name_to_prop( 5128525SEric.Schrock@Sun.COM nvpair_name(elem))) == ZPROP_INVAL) 5138525SEric.Schrock@Sun.COM return (EINVAL); 5148525SEric.Schrock@Sun.COM 5158525SEric.Schrock@Sun.COM if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 5168525SEric.Schrock@Sun.COM continue; 5178525SEric.Schrock@Sun.COM 5188525SEric.Schrock@Sun.COM need_sync = B_TRUE; 5198525SEric.Schrock@Sun.COM break; 5208525SEric.Schrock@Sun.COM } 5218525SEric.Schrock@Sun.COM 5228525SEric.Schrock@Sun.COM if (need_sync) 5238525SEric.Schrock@Sun.COM return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 5248525SEric.Schrock@Sun.COM spa, nvp, 3)); 5258525SEric.Schrock@Sun.COM else 5268525SEric.Schrock@Sun.COM return (0); 5275094Slling } 5285094Slling 5295094Slling /* 5305094Slling * If the bootfs property value is dsobj, clear it. 5315094Slling */ 5325094Slling void 5335094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 5345094Slling { 5355094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 5365094Slling VERIFY(zap_remove(spa->spa_meta_objset, 5375094Slling spa->spa_pool_props_object, 5385094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 5395094Slling spa->spa_bootfs = 0; 5405094Slling } 5415094Slling } 5425094Slling 543789Sahrens /* 544789Sahrens * ========================================================================== 545789Sahrens * SPA state manipulation (open/create/destroy/import/export) 546789Sahrens * ========================================================================== 547789Sahrens */ 548789Sahrens 5491544Seschrock static int 5501544Seschrock spa_error_entry_compare(const void *a, const void *b) 5511544Seschrock { 5521544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 5531544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 5541544Seschrock int ret; 5551544Seschrock 5561544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 5571544Seschrock sizeof (zbookmark_t)); 5581544Seschrock 5591544Seschrock if (ret < 0) 5601544Seschrock return (-1); 5611544Seschrock else if (ret > 0) 5621544Seschrock return (1); 5631544Seschrock else 5641544Seschrock return (0); 5651544Seschrock } 5661544Seschrock 5671544Seschrock /* 5681544Seschrock * Utility function which retrieves copies of the current logs and 5691544Seschrock * re-initializes them in the process. 5701544Seschrock */ 5711544Seschrock void 5721544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 5731544Seschrock { 5741544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 5751544Seschrock 5761544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 5771544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 5781544Seschrock 5791544Seschrock avl_create(&spa->spa_errlist_scrub, 5801544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5811544Seschrock offsetof(spa_error_entry_t, se_avl)); 5821544Seschrock avl_create(&spa->spa_errlist_last, 5831544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5841544Seschrock offsetof(spa_error_entry_t, se_avl)); 5851544Seschrock } 5861544Seschrock 587789Sahrens /* 588789Sahrens * Activate an uninitialized pool. 589789Sahrens */ 590789Sahrens static void 5918241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode) 592789Sahrens { 593789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 594789Sahrens 595789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 5968241SJeff.Bonwick@Sun.COM spa->spa_mode = mode; 597789Sahrens 59810594SGeorge.Wilson@Sun.COM spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 59910594SGeorge.Wilson@Sun.COM spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 600789Sahrens 6017754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 6027754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 603*11146SGeorge.Wilson@Sun.COM const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 604*11146SGeorge.Wilson@Sun.COM enum zti_modes mode = ztip->zti_mode; 605*11146SGeorge.Wilson@Sun.COM uint_t value = ztip->zti_value; 6069515SJonathan.Adams@Sun.COM char name[32]; 6079515SJonathan.Adams@Sun.COM 6089515SJonathan.Adams@Sun.COM (void) snprintf(name, sizeof (name), 609*11146SGeorge.Wilson@Sun.COM "%s_%s", zio_type_name[t], zio_taskq_types[q]); 6109515SJonathan.Adams@Sun.COM 6119515SJonathan.Adams@Sun.COM if (mode == zti_mode_tune) { 6129515SJonathan.Adams@Sun.COM mode = zio_taskq_tune_mode; 6139515SJonathan.Adams@Sun.COM value = zio_taskq_tune_value; 6149515SJonathan.Adams@Sun.COM if (mode == zti_mode_tune) 6159515SJonathan.Adams@Sun.COM mode = zti_mode_online_percent; 6169515SJonathan.Adams@Sun.COM } 6179515SJonathan.Adams@Sun.COM 6189515SJonathan.Adams@Sun.COM switch (mode) { 6199515SJonathan.Adams@Sun.COM case zti_mode_fixed: 6209515SJonathan.Adams@Sun.COM ASSERT3U(value, >=, 1); 6219515SJonathan.Adams@Sun.COM value = MAX(value, 1); 6229515SJonathan.Adams@Sun.COM 6239515SJonathan.Adams@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create(name, 6249515SJonathan.Adams@Sun.COM value, maxclsyspri, 50, INT_MAX, 6259515SJonathan.Adams@Sun.COM TASKQ_PREPOPULATE); 6269515SJonathan.Adams@Sun.COM break; 6279515SJonathan.Adams@Sun.COM 6289515SJonathan.Adams@Sun.COM case zti_mode_online_percent: 6299515SJonathan.Adams@Sun.COM spa->spa_zio_taskq[t][q] = taskq_create(name, 6309515SJonathan.Adams@Sun.COM value, maxclsyspri, 50, INT_MAX, 6319515SJonathan.Adams@Sun.COM TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); 6329515SJonathan.Adams@Sun.COM break; 6339515SJonathan.Adams@Sun.COM 634*11146SGeorge.Wilson@Sun.COM case zti_mode_null: 635*11146SGeorge.Wilson@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 636*11146SGeorge.Wilson@Sun.COM break; 637*11146SGeorge.Wilson@Sun.COM 6389515SJonathan.Adams@Sun.COM case zti_mode_tune: 6399515SJonathan.Adams@Sun.COM default: 6409515SJonathan.Adams@Sun.COM panic("unrecognized mode for " 6419515SJonathan.Adams@Sun.COM "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) " 6429515SJonathan.Adams@Sun.COM "in spa_activate()", 6439515SJonathan.Adams@Sun.COM t, q, mode, value); 6449515SJonathan.Adams@Sun.COM break; 6459515SJonathan.Adams@Sun.COM } 6467754SJeff.Bonwick@Sun.COM } 647789Sahrens } 648789Sahrens 6497754SJeff.Bonwick@Sun.COM list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 6507754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_config_dirty_node)); 6517754SJeff.Bonwick@Sun.COM list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 6527754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_state_dirty_node)); 653789Sahrens 654789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 655789Sahrens offsetof(struct vdev, vdev_txg_node)); 6561544Seschrock 6571544Seschrock avl_create(&spa->spa_errlist_scrub, 6581544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 6591544Seschrock offsetof(spa_error_entry_t, se_avl)); 6601544Seschrock avl_create(&spa->spa_errlist_last, 6611544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 6621544Seschrock offsetof(spa_error_entry_t, se_avl)); 663789Sahrens } 664789Sahrens 665789Sahrens /* 666789Sahrens * Opposite of spa_activate(). 667789Sahrens */ 668789Sahrens static void 669789Sahrens spa_deactivate(spa_t *spa) 670789Sahrens { 671789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 672789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 673789Sahrens ASSERT(spa->spa_root_vdev == NULL); 6749630SJeff.Bonwick@Sun.COM ASSERT(spa->spa_async_zio_root == NULL); 675789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 676789Sahrens 677789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 678789Sahrens 6797754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_config_dirty_list); 6807754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_state_dirty_list); 6817754SJeff.Bonwick@Sun.COM 6827754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 6837754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 684*11146SGeorge.Wilson@Sun.COM if (spa->spa_zio_taskq[t][q] != NULL) 685*11146SGeorge.Wilson@Sun.COM taskq_destroy(spa->spa_zio_taskq[t][q]); 6867754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 6877754SJeff.Bonwick@Sun.COM } 688789Sahrens } 689789Sahrens 690789Sahrens metaslab_class_destroy(spa->spa_normal_class); 691789Sahrens spa->spa_normal_class = NULL; 692789Sahrens 6934527Sperrin metaslab_class_destroy(spa->spa_log_class); 6944527Sperrin spa->spa_log_class = NULL; 6954527Sperrin 6961544Seschrock /* 6971544Seschrock * If this was part of an import or the open otherwise failed, we may 6981544Seschrock * still have errors left in the queues. Empty them just in case. 6991544Seschrock */ 7001544Seschrock spa_errlog_drain(spa); 7011544Seschrock 7021544Seschrock avl_destroy(&spa->spa_errlist_scrub); 7031544Seschrock avl_destroy(&spa->spa_errlist_last); 7041544Seschrock 705789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 706789Sahrens } 707789Sahrens 708789Sahrens /* 709789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 710789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 711789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 712789Sahrens * All vdev validation is done by the vdev_alloc() routine. 713789Sahrens */ 7142082Seschrock static int 7152082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 7162082Seschrock uint_t id, int atype) 717789Sahrens { 718789Sahrens nvlist_t **child; 7199816SGeorge.Wilson@Sun.COM uint_t children; 7202082Seschrock int error; 7212082Seschrock 7222082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 7232082Seschrock return (error); 7242082Seschrock 7252082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 7262082Seschrock return (0); 727789Sahrens 7287754SJeff.Bonwick@Sun.COM error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 7297754SJeff.Bonwick@Sun.COM &child, &children); 7307754SJeff.Bonwick@Sun.COM 7317754SJeff.Bonwick@Sun.COM if (error == ENOENT) 7327754SJeff.Bonwick@Sun.COM return (0); 7337754SJeff.Bonwick@Sun.COM 7347754SJeff.Bonwick@Sun.COM if (error) { 7352082Seschrock vdev_free(*vdp); 7362082Seschrock *vdp = NULL; 7372082Seschrock return (EINVAL); 738789Sahrens } 739789Sahrens 7409816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 7412082Seschrock vdev_t *vd; 7422082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 7432082Seschrock atype)) != 0) { 7442082Seschrock vdev_free(*vdp); 7452082Seschrock *vdp = NULL; 7462082Seschrock return (error); 747789Sahrens } 748789Sahrens } 749789Sahrens 7502082Seschrock ASSERT(*vdp != NULL); 7512082Seschrock 7522082Seschrock return (0); 753789Sahrens } 754789Sahrens 755789Sahrens /* 756789Sahrens * Opposite of spa_load(). 757789Sahrens */ 758789Sahrens static void 759789Sahrens spa_unload(spa_t *spa) 760789Sahrens { 7612082Seschrock int i; 7622082Seschrock 7637754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 7647754SJeff.Bonwick@Sun.COM 765789Sahrens /* 7661544Seschrock * Stop async tasks. 7671544Seschrock */ 7681544Seschrock spa_async_suspend(spa); 7691544Seschrock 7701544Seschrock /* 771789Sahrens * Stop syncing. 772789Sahrens */ 773789Sahrens if (spa->spa_sync_on) { 774789Sahrens txg_sync_stop(spa->spa_dsl_pool); 775789Sahrens spa->spa_sync_on = B_FALSE; 776789Sahrens } 777789Sahrens 778789Sahrens /* 7797754SJeff.Bonwick@Sun.COM * Wait for any outstanding async I/O to complete. 780789Sahrens */ 7819234SGeorge.Wilson@Sun.COM if (spa->spa_async_zio_root != NULL) { 7829234SGeorge.Wilson@Sun.COM (void) zio_wait(spa->spa_async_zio_root); 7839234SGeorge.Wilson@Sun.COM spa->spa_async_zio_root = NULL; 7849234SGeorge.Wilson@Sun.COM } 785789Sahrens 786789Sahrens /* 787789Sahrens * Close the dsl pool. 788789Sahrens */ 789789Sahrens if (spa->spa_dsl_pool) { 790789Sahrens dsl_pool_close(spa->spa_dsl_pool); 791789Sahrens spa->spa_dsl_pool = NULL; 792789Sahrens } 793789Sahrens 79410922SJeff.Bonwick@Sun.COM ddt_unload(spa); 79510922SJeff.Bonwick@Sun.COM 7968241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7978241SJeff.Bonwick@Sun.COM 7988241SJeff.Bonwick@Sun.COM /* 7998241SJeff.Bonwick@Sun.COM * Drop and purge level 2 cache 8008241SJeff.Bonwick@Sun.COM */ 8018241SJeff.Bonwick@Sun.COM spa_l2cache_drop(spa); 8028241SJeff.Bonwick@Sun.COM 803789Sahrens /* 804789Sahrens * Close all vdevs. 805789Sahrens */ 8061585Sbonwick if (spa->spa_root_vdev) 807789Sahrens vdev_free(spa->spa_root_vdev); 8081585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 8091544Seschrock 8105450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 8115450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 8125450Sbrendan if (spa->spa_spares.sav_vdevs) { 8135450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 8145450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 8155450Sbrendan spa->spa_spares.sav_vdevs = NULL; 8165450Sbrendan } 8175450Sbrendan if (spa->spa_spares.sav_config) { 8185450Sbrendan nvlist_free(spa->spa_spares.sav_config); 8195450Sbrendan spa->spa_spares.sav_config = NULL; 8202082Seschrock } 8217377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 8225450Sbrendan 8235450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 8245450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 8255450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 8265450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 8275450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 8285450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 8295450Sbrendan } 8305450Sbrendan if (spa->spa_l2cache.sav_config) { 8315450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 8325450Sbrendan spa->spa_l2cache.sav_config = NULL; 8332082Seschrock } 8347377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 8352082Seschrock 8361544Seschrock spa->spa_async_suspended = 0; 8378241SJeff.Bonwick@Sun.COM 8388241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 839789Sahrens } 840789Sahrens 841789Sahrens /* 8422082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 8432082Seschrock * this pool. When this is called, we have some form of basic information in 8445450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 8455450Sbrendan * then re-generate a more complete list including status information. 8462082Seschrock */ 8472082Seschrock static void 8482082Seschrock spa_load_spares(spa_t *spa) 8492082Seschrock { 8502082Seschrock nvlist_t **spares; 8512082Seschrock uint_t nspares; 8522082Seschrock int i; 8533377Seschrock vdev_t *vd, *tvd; 8542082Seschrock 8557754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 8567754SJeff.Bonwick@Sun.COM 8572082Seschrock /* 8582082Seschrock * First, close and free any existing spare vdevs. 8592082Seschrock */ 8605450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 8615450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 8623377Seschrock 8633377Seschrock /* Undo the call to spa_activate() below */ 8646643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 8656643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 8663377Seschrock spa_spare_remove(tvd); 8673377Seschrock vdev_close(vd); 8683377Seschrock vdev_free(vd); 8692082Seschrock } 8703377Seschrock 8715450Sbrendan if (spa->spa_spares.sav_vdevs) 8725450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 8735450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 8745450Sbrendan 8755450Sbrendan if (spa->spa_spares.sav_config == NULL) 8762082Seschrock nspares = 0; 8772082Seschrock else 8785450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 8792082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 8802082Seschrock 8815450Sbrendan spa->spa_spares.sav_count = (int)nspares; 8825450Sbrendan spa->spa_spares.sav_vdevs = NULL; 8832082Seschrock 8842082Seschrock if (nspares == 0) 8852082Seschrock return; 8862082Seschrock 8872082Seschrock /* 8882082Seschrock * Construct the array of vdevs, opening them to get status in the 8893377Seschrock * process. For each spare, there is potentially two different vdev_t 8903377Seschrock * structures associated with it: one in the list of spares (used only 8913377Seschrock * for basic validation purposes) and one in the active vdev 8923377Seschrock * configuration (if it's spared in). During this phase we open and 8933377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 8943377Seschrock * active configuration, then we also mark this vdev as an active spare. 8952082Seschrock */ 8965450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 8975450Sbrendan KM_SLEEP); 8985450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 8992082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 9002082Seschrock VDEV_ALLOC_SPARE) == 0); 9012082Seschrock ASSERT(vd != NULL); 9022082Seschrock 9035450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 9042082Seschrock 9056643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 9066643Seschrock B_FALSE)) != NULL) { 9073377Seschrock if (!tvd->vdev_isspare) 9083377Seschrock spa_spare_add(tvd); 9093377Seschrock 9103377Seschrock /* 9113377Seschrock * We only mark the spare active if we were successfully 9123377Seschrock * able to load the vdev. Otherwise, importing a pool 9133377Seschrock * with a bad active spare would result in strange 9143377Seschrock * behavior, because multiple pool would think the spare 9153377Seschrock * is actively in use. 9163377Seschrock * 9173377Seschrock * There is a vulnerability here to an equally bizarre 9183377Seschrock * circumstance, where a dead active spare is later 9193377Seschrock * brought back to life (onlined or otherwise). Given 9203377Seschrock * the rarity of this scenario, and the extra complexity 9213377Seschrock * it adds, we ignore the possibility. 9223377Seschrock */ 9233377Seschrock if (!vdev_is_dead(tvd)) 9243377Seschrock spa_spare_activate(tvd); 9253377Seschrock } 9263377Seschrock 9277754SJeff.Bonwick@Sun.COM vd->vdev_top = vd; 9289425SEric.Schrock@Sun.COM vd->vdev_aux = &spa->spa_spares; 9297754SJeff.Bonwick@Sun.COM 9302082Seschrock if (vdev_open(vd) != 0) 9312082Seschrock continue; 9322082Seschrock 9335450Sbrendan if (vdev_validate_aux(vd) == 0) 9345450Sbrendan spa_spare_add(vd); 9352082Seschrock } 9362082Seschrock 9372082Seschrock /* 9382082Seschrock * Recompute the stashed list of spares, with status information 9392082Seschrock * this time. 9402082Seschrock */ 9415450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 9422082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 9432082Seschrock 9445450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 9455450Sbrendan KM_SLEEP); 9465450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 9475450Sbrendan spares[i] = vdev_config_generate(spa, 9485450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 9495450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 9505450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 9515450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 9522082Seschrock nvlist_free(spares[i]); 9535450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 9545450Sbrendan } 9555450Sbrendan 9565450Sbrendan /* 9575450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 9585450Sbrendan * this pool. When this is called, we have some form of basic information in 9595450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 9605450Sbrendan * then re-generate a more complete list including status information. 9615450Sbrendan * Devices which are already active have their details maintained, and are 9625450Sbrendan * not re-opened. 9635450Sbrendan */ 9645450Sbrendan static void 9655450Sbrendan spa_load_l2cache(spa_t *spa) 9665450Sbrendan { 9675450Sbrendan nvlist_t **l2cache; 9685450Sbrendan uint_t nl2cache; 9695450Sbrendan int i, j, oldnvdevs; 9709816SGeorge.Wilson@Sun.COM uint64_t guid; 9715450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 9725450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 9735450Sbrendan 9747754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 9757754SJeff.Bonwick@Sun.COM 9765450Sbrendan if (sav->sav_config != NULL) { 9775450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 9785450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 9795450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 9805450Sbrendan } else { 9815450Sbrendan nl2cache = 0; 9825450Sbrendan } 9835450Sbrendan 9845450Sbrendan oldvdevs = sav->sav_vdevs; 9855450Sbrendan oldnvdevs = sav->sav_count; 9865450Sbrendan sav->sav_vdevs = NULL; 9875450Sbrendan sav->sav_count = 0; 9885450Sbrendan 9895450Sbrendan /* 9905450Sbrendan * Process new nvlist of vdevs. 9915450Sbrendan */ 9925450Sbrendan for (i = 0; i < nl2cache; i++) { 9935450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 9945450Sbrendan &guid) == 0); 9955450Sbrendan 9965450Sbrendan newvdevs[i] = NULL; 9975450Sbrendan for (j = 0; j < oldnvdevs; j++) { 9985450Sbrendan vd = oldvdevs[j]; 9995450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 10005450Sbrendan /* 10015450Sbrendan * Retain previous vdev for add/remove ops. 10025450Sbrendan */ 10035450Sbrendan newvdevs[i] = vd; 10045450Sbrendan oldvdevs[j] = NULL; 10055450Sbrendan break; 10065450Sbrendan } 10075450Sbrendan } 10085450Sbrendan 10095450Sbrendan if (newvdevs[i] == NULL) { 10105450Sbrendan /* 10115450Sbrendan * Create new vdev 10125450Sbrendan */ 10135450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 10145450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 10155450Sbrendan ASSERT(vd != NULL); 10165450Sbrendan newvdevs[i] = vd; 10175450Sbrendan 10185450Sbrendan /* 10195450Sbrendan * Commit this vdev as an l2cache device, 10205450Sbrendan * even if it fails to open. 10215450Sbrendan */ 10225450Sbrendan spa_l2cache_add(vd); 10235450Sbrendan 10246643Seschrock vd->vdev_top = vd; 10256643Seschrock vd->vdev_aux = sav; 10266643Seschrock 10276643Seschrock spa_l2cache_activate(vd); 10286643Seschrock 10295450Sbrendan if (vdev_open(vd) != 0) 10305450Sbrendan continue; 10315450Sbrendan 10325450Sbrendan (void) vdev_validate_aux(vd); 10335450Sbrendan 10349816SGeorge.Wilson@Sun.COM if (!vdev_is_dead(vd)) 10359816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa, vd); 10365450Sbrendan } 10375450Sbrendan } 10385450Sbrendan 10395450Sbrendan /* 10405450Sbrendan * Purge vdevs that were dropped 10415450Sbrendan */ 10425450Sbrendan for (i = 0; i < oldnvdevs; i++) { 10435450Sbrendan uint64_t pool; 10445450Sbrendan 10455450Sbrendan vd = oldvdevs[i]; 10465450Sbrendan if (vd != NULL) { 10478241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 10488241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 10495450Sbrendan l2arc_remove_vdev(vd); 10505450Sbrendan (void) vdev_close(vd); 10515450Sbrendan spa_l2cache_remove(vd); 10525450Sbrendan } 10535450Sbrendan } 10545450Sbrendan 10555450Sbrendan if (oldvdevs) 10565450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 10575450Sbrendan 10585450Sbrendan if (sav->sav_config == NULL) 10595450Sbrendan goto out; 10605450Sbrendan 10615450Sbrendan sav->sav_vdevs = newvdevs; 10625450Sbrendan sav->sav_count = (int)nl2cache; 10635450Sbrendan 10645450Sbrendan /* 10655450Sbrendan * Recompute the stashed list of l2cache devices, with status 10665450Sbrendan * information this time. 10675450Sbrendan */ 10685450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 10695450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 10705450Sbrendan 10715450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 10725450Sbrendan for (i = 0; i < sav->sav_count; i++) 10735450Sbrendan l2cache[i] = vdev_config_generate(spa, 10745450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 10755450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 10765450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 10775450Sbrendan out: 10785450Sbrendan for (i = 0; i < sav->sav_count; i++) 10795450Sbrendan nvlist_free(l2cache[i]); 10805450Sbrendan if (sav->sav_count) 10815450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 10822082Seschrock } 10832082Seschrock 10842082Seschrock static int 10852082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 10862082Seschrock { 10872082Seschrock dmu_buf_t *db; 10882082Seschrock char *packed = NULL; 10892082Seschrock size_t nvsize = 0; 10902082Seschrock int error; 10912082Seschrock *value = NULL; 10922082Seschrock 10932082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 10942082Seschrock nvsize = *(uint64_t *)db->db_data; 10952082Seschrock dmu_buf_rele(db, FTAG); 10962082Seschrock 10972082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 10989512SNeil.Perrin@Sun.COM error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 10999512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 11002082Seschrock if (error == 0) 11012082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 11022082Seschrock kmem_free(packed, nvsize); 11032082Seschrock 11042082Seschrock return (error); 11052082Seschrock } 11062082Seschrock 11072082Seschrock /* 11084451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 11094451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 11104451Seschrock */ 11114451Seschrock static void 11124451Seschrock spa_check_removed(vdev_t *vd) 11134451Seschrock { 11149816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 11154451Seschrock spa_check_removed(vd->vdev_child[c]); 11164451Seschrock 11174451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 11184451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 11194451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 11204451Seschrock } 11214451Seschrock } 11224451Seschrock 11234451Seschrock /* 11249701SGeorge.Wilson@Sun.COM * Load the slog device state from the config object since it's possible 11259701SGeorge.Wilson@Sun.COM * that the label does not contain the most up-to-date information. 11269701SGeorge.Wilson@Sun.COM */ 11279701SGeorge.Wilson@Sun.COM void 112810594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv) 11299701SGeorge.Wilson@Sun.COM { 113010594SGeorge.Wilson@Sun.COM vdev_t *ovd, *rvd = spa->spa_root_vdev; 113110594SGeorge.Wilson@Sun.COM 113210594SGeorge.Wilson@Sun.COM /* 113310594SGeorge.Wilson@Sun.COM * Load the original root vdev tree from the passed config. 113410594SGeorge.Wilson@Sun.COM */ 113510594SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 113610594SGeorge.Wilson@Sun.COM VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 113710594SGeorge.Wilson@Sun.COM 113810594SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 113910594SGeorge.Wilson@Sun.COM vdev_t *cvd = rvd->vdev_child[c]; 114010594SGeorge.Wilson@Sun.COM if (cvd->vdev_islog) 114110594SGeorge.Wilson@Sun.COM vdev_load_log_state(cvd, ovd->vdev_child[c]); 11429701SGeorge.Wilson@Sun.COM } 114310594SGeorge.Wilson@Sun.COM vdev_free(ovd); 114410594SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 11459701SGeorge.Wilson@Sun.COM } 11469701SGeorge.Wilson@Sun.COM 11479701SGeorge.Wilson@Sun.COM /* 11487294Sperrin * Check for missing log devices 11497294Sperrin */ 11507294Sperrin int 11517294Sperrin spa_check_logs(spa_t *spa) 11527294Sperrin { 11537294Sperrin switch (spa->spa_log_state) { 11547294Sperrin case SPA_LOG_MISSING: 11557294Sperrin /* need to recheck in case slog has been restored */ 11567294Sperrin case SPA_LOG_UNKNOWN: 11577294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 11587294Sperrin DS_FIND_CHILDREN)) { 11597294Sperrin spa->spa_log_state = SPA_LOG_MISSING; 11607294Sperrin return (1); 11617294Sperrin } 11627294Sperrin break; 11637294Sperrin } 11647294Sperrin return (0); 11657294Sperrin } 11667294Sperrin 116710672SEric.Schrock@Sun.COM static void 116810672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav) 116910672SEric.Schrock@Sun.COM { 117010922SJeff.Bonwick@Sun.COM for (int i = 0; i < sav->sav_count; i++) 117110672SEric.Schrock@Sun.COM spa_check_removed(sav->sav_vdevs[i]); 117210672SEric.Schrock@Sun.COM } 117310672SEric.Schrock@Sun.COM 117410922SJeff.Bonwick@Sun.COM void 117510922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio) 117610922SJeff.Bonwick@Sun.COM { 117710922SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 117810922SJeff.Bonwick@Sun.COM 117910922SJeff.Bonwick@Sun.COM if (zio->io_error) 118010922SJeff.Bonwick@Sun.COM return; 118110922SJeff.Bonwick@Sun.COM 118210922SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 118310922SJeff.Bonwick@Sun.COM if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 118410922SJeff.Bonwick@Sun.COM spa->spa_claim_max_txg = zio->io_bp->blk_birth; 118510922SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 118610922SJeff.Bonwick@Sun.COM } 118710922SJeff.Bonwick@Sun.COM 118810921STim.Haley@Sun.COM typedef struct spa_load_error { 118910921STim.Haley@Sun.COM uint64_t sle_metadata_count; 119010921STim.Haley@Sun.COM uint64_t sle_data_count; 119110921STim.Haley@Sun.COM } spa_load_error_t; 119210921STim.Haley@Sun.COM 119310921STim.Haley@Sun.COM static void 119410921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio) 119510921STim.Haley@Sun.COM { 119610921STim.Haley@Sun.COM blkptr_t *bp = zio->io_bp; 119710921STim.Haley@Sun.COM spa_load_error_t *sle = zio->io_private; 119810921STim.Haley@Sun.COM dmu_object_type_t type = BP_GET_TYPE(bp); 119910921STim.Haley@Sun.COM int error = zio->io_error; 120010921STim.Haley@Sun.COM 120110921STim.Haley@Sun.COM if (error) { 120210921STim.Haley@Sun.COM if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) && 120310921STim.Haley@Sun.COM type != DMU_OT_INTENT_LOG) 120410921STim.Haley@Sun.COM atomic_add_64(&sle->sle_metadata_count, 1); 120510921STim.Haley@Sun.COM else 120610921STim.Haley@Sun.COM atomic_add_64(&sle->sle_data_count, 1); 120710921STim.Haley@Sun.COM } 120810921STim.Haley@Sun.COM zio_data_buf_free(zio->io_data, zio->io_size); 120910921STim.Haley@Sun.COM } 121010921STim.Haley@Sun.COM 121110921STim.Haley@Sun.COM /*ARGSUSED*/ 121210921STim.Haley@Sun.COM static int 121310922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 121410922SJeff.Bonwick@Sun.COM const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 121510921STim.Haley@Sun.COM { 121610921STim.Haley@Sun.COM if (bp != NULL) { 121710921STim.Haley@Sun.COM zio_t *rio = arg; 121810921STim.Haley@Sun.COM size_t size = BP_GET_PSIZE(bp); 121910921STim.Haley@Sun.COM void *data = zio_data_buf_alloc(size); 122010921STim.Haley@Sun.COM 122110921STim.Haley@Sun.COM zio_nowait(zio_read(rio, spa, bp, data, size, 122210921STim.Haley@Sun.COM spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 122310921STim.Haley@Sun.COM ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 122410921STim.Haley@Sun.COM ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 122510921STim.Haley@Sun.COM } 122610921STim.Haley@Sun.COM return (0); 122710921STim.Haley@Sun.COM } 122810921STim.Haley@Sun.COM 122910921STim.Haley@Sun.COM static int 123010921STim.Haley@Sun.COM spa_load_verify(spa_t *spa) 123110921STim.Haley@Sun.COM { 123210921STim.Haley@Sun.COM zio_t *rio; 123310921STim.Haley@Sun.COM spa_load_error_t sle = { 0 }; 123410921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 123510921STim.Haley@Sun.COM boolean_t verify_ok = B_FALSE; 123610921STim.Haley@Sun.COM int error; 123710921STim.Haley@Sun.COM 123810921STim.Haley@Sun.COM rio = zio_root(spa, NULL, &sle, 123910921STim.Haley@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 124010921STim.Haley@Sun.COM 124111125SJeff.Bonwick@Sun.COM error = traverse_pool(spa, spa->spa_verify_min_txg, 124211125SJeff.Bonwick@Sun.COM TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); 124310921STim.Haley@Sun.COM 124410921STim.Haley@Sun.COM (void) zio_wait(rio); 124510921STim.Haley@Sun.COM 124610921STim.Haley@Sun.COM zpool_get_rewind_policy(spa->spa_config, &policy); 124710921STim.Haley@Sun.COM 124810921STim.Haley@Sun.COM spa->spa_load_meta_errors = sle.sle_metadata_count; 124910921STim.Haley@Sun.COM spa->spa_load_data_errors = sle.sle_data_count; 125010921STim.Haley@Sun.COM 125110921STim.Haley@Sun.COM if (!error && sle.sle_metadata_count <= policy.zrp_maxmeta && 125210921STim.Haley@Sun.COM sle.sle_data_count <= policy.zrp_maxdata) { 125310921STim.Haley@Sun.COM verify_ok = B_TRUE; 125410921STim.Haley@Sun.COM spa->spa_load_txg = spa->spa_uberblock.ub_txg; 125510921STim.Haley@Sun.COM spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 125611026STim.Haley@Sun.COM } else { 125711026STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 125810921STim.Haley@Sun.COM } 125910921STim.Haley@Sun.COM 126010921STim.Haley@Sun.COM if (error) { 126110921STim.Haley@Sun.COM if (error != ENXIO && error != EIO) 126210921STim.Haley@Sun.COM error = EIO; 126310921STim.Haley@Sun.COM return (error); 126410921STim.Haley@Sun.COM } 126510921STim.Haley@Sun.COM 126610921STim.Haley@Sun.COM return (verify_ok ? 0 : EIO); 126710921STim.Haley@Sun.COM } 126810921STim.Haley@Sun.COM 12697294Sperrin /* 1270789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 12711544Seschrock * source of configuration information. 1272789Sahrens */ 1273789Sahrens static int 127410921STim.Haley@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, int mosconfig) 1275789Sahrens { 1276789Sahrens int error = 0; 127710594SGeorge.Wilson@Sun.COM nvlist_t *nvconfig, *nvroot = NULL; 1278789Sahrens vdev_t *rvd; 1279789Sahrens uberblock_t *ub = &spa->spa_uberblock; 12801635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 1281789Sahrens uint64_t pool_guid; 12822082Seschrock uint64_t version; 12834451Seschrock uint64_t autoreplace = 0; 12848241SJeff.Bonwick@Sun.COM int orig_mode = spa->spa_mode; 12857294Sperrin char *ereport = FM_EREPORT_ZFS_POOL; 128610921STim.Haley@Sun.COM nvlist_t *config = spa->spa_config; 1287789Sahrens 12888241SJeff.Bonwick@Sun.COM /* 12898241SJeff.Bonwick@Sun.COM * If this is an untrusted config, access the pool in read-only mode. 12908241SJeff.Bonwick@Sun.COM * This prevents things like resilvering recently removed devices. 12918241SJeff.Bonwick@Sun.COM */ 12928241SJeff.Bonwick@Sun.COM if (!mosconfig) 12938241SJeff.Bonwick@Sun.COM spa->spa_mode = FREAD; 12948241SJeff.Bonwick@Sun.COM 12957754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 12967754SJeff.Bonwick@Sun.COM 12971544Seschrock spa->spa_load_state = state; 12981635Sbonwick 1299789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 13001733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 13011544Seschrock error = EINVAL; 13021544Seschrock goto out; 13031544Seschrock } 1304789Sahrens 13052082Seschrock /* 13062082Seschrock * Versioning wasn't explicitly added to the label until later, so if 13072082Seschrock * it's not present treat it as the initial version. 13082082Seschrock */ 13092082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 13104577Sahrens version = SPA_VERSION_INITIAL; 13112082Seschrock 13121733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 13131733Sbonwick &spa->spa_config_txg); 13141733Sbonwick 13151635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 13161544Seschrock spa_guid_exists(pool_guid, 0)) { 13171544Seschrock error = EEXIST; 13181544Seschrock goto out; 13191544Seschrock } 1320789Sahrens 13212174Seschrock spa->spa_load_guid = pool_guid; 13222174Seschrock 1323789Sahrens /* 13249234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 13259234SGeorge.Wilson@Sun.COM */ 13269630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 13279630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 13289234SGeorge.Wilson@Sun.COM 13299234SGeorge.Wilson@Sun.COM /* 13302082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 13312082Seschrock * value that will be returned by spa_version() since parsing the 13322082Seschrock * configuration requires knowing the version number. 1333789Sahrens */ 13347754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13352082Seschrock spa->spa_ubsync.ub_version = version; 13362082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 13377754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1338789Sahrens 13392082Seschrock if (error != 0) 13401544Seschrock goto out; 1341789Sahrens 13421585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1343789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1344789Sahrens 1345789Sahrens /* 1346789Sahrens * Try to open all vdevs, loading each label in the process. 1347789Sahrens */ 13487754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13494070Smc142369 error = vdev_open(rvd); 13507754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13514070Smc142369 if (error != 0) 13521544Seschrock goto out; 1353789Sahrens 1354789Sahrens /* 13559276SMark.Musante@Sun.COM * We need to validate the vdev labels against the configuration that 13569276SMark.Musante@Sun.COM * we have in hand, which is dependent on the setting of mosconfig. If 13579276SMark.Musante@Sun.COM * mosconfig is true then we're validating the vdev labels based on 13589276SMark.Musante@Sun.COM * that config. Otherwise, we're validating against the cached config 13599276SMark.Musante@Sun.COM * (zpool.cache) that was read when we loaded the zfs module, and then 13609276SMark.Musante@Sun.COM * later we will recursively call spa_load() and validate against 13619276SMark.Musante@Sun.COM * the vdev config. 13621986Seschrock */ 13639276SMark.Musante@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13649276SMark.Musante@Sun.COM error = vdev_validate(rvd); 13659276SMark.Musante@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 13669276SMark.Musante@Sun.COM if (error != 0) 13679276SMark.Musante@Sun.COM goto out; 13681986Seschrock 13691986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13701986Seschrock error = ENXIO; 13711986Seschrock goto out; 13721986Seschrock } 13731986Seschrock 13741986Seschrock /* 1375789Sahrens * Find the best uberblock. 1376789Sahrens */ 13777754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1378789Sahrens 1379789Sahrens /* 1380789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1381789Sahrens */ 1382789Sahrens if (ub->ub_txg == 0) { 13831760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13841760Seschrock VDEV_AUX_CORRUPT_DATA); 13851544Seschrock error = ENXIO; 13861544Seschrock goto out; 13871544Seschrock } 13881544Seschrock 13891544Seschrock /* 13901544Seschrock * If the pool is newer than the code, we can't open it. 13911544Seschrock */ 13924577Sahrens if (ub->ub_version > SPA_VERSION) { 13931760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 13941760Seschrock VDEV_AUX_VERSION_NEWER); 13951544Seschrock error = ENOTSUP; 13961544Seschrock goto out; 1397789Sahrens } 1398789Sahrens 1399789Sahrens /* 1400789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1401789Sahrens * incomplete configuration. 1402789Sahrens */ 14031732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 14041544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14051544Seschrock VDEV_AUX_BAD_GUID_SUM); 14061544Seschrock error = ENXIO; 14071544Seschrock goto out; 1408789Sahrens } 1409789Sahrens 1410789Sahrens /* 1411789Sahrens * Initialize internal SPA structures. 1412789Sahrens */ 1413789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1414789Sahrens spa->spa_ubsync = spa->spa_uberblock; 141510921STim.Haley@Sun.COM spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 141610921STim.Haley@Sun.COM TXG_INITIAL : spa_last_synced_txg(spa) - TXG_DEFER_SIZE; 141710921STim.Haley@Sun.COM spa->spa_first_txg = spa->spa_last_ubsync_txg ? 141810921STim.Haley@Sun.COM spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 141910922SJeff.Bonwick@Sun.COM spa->spa_claim_max_txg = spa->spa_first_txg; 142010922SJeff.Bonwick@Sun.COM 14211544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 14221544Seschrock if (error) { 14231544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14241544Seschrock VDEV_AUX_CORRUPT_DATA); 142510921STim.Haley@Sun.COM error = EIO; 14261544Seschrock goto out; 14271544Seschrock } 1428789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1429789Sahrens 14301544Seschrock if (zap_lookup(spa->spa_meta_objset, 1431789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 14321544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 14331544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14341544Seschrock VDEV_AUX_CORRUPT_DATA); 14351544Seschrock error = EIO; 14361544Seschrock goto out; 14371544Seschrock } 1438789Sahrens 143910594SGeorge.Wilson@Sun.COM if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) { 144010594SGeorge.Wilson@Sun.COM vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 144110594SGeorge.Wilson@Sun.COM VDEV_AUX_CORRUPT_DATA); 144210594SGeorge.Wilson@Sun.COM error = EIO; 144310594SGeorge.Wilson@Sun.COM goto out; 144410594SGeorge.Wilson@Sun.COM } 144510594SGeorge.Wilson@Sun.COM 1446789Sahrens if (!mosconfig) { 14473975Sek110237 uint64_t hostid; 14482082Seschrock 144910594SGeorge.Wilson@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 14507706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 14513975Sek110237 char *hostname; 14523975Sek110237 unsigned long myhostid = 0; 14533975Sek110237 145410594SGeorge.Wilson@Sun.COM VERIFY(nvlist_lookup_string(nvconfig, 14553975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 14563975Sek110237 14578662SJordan.Vaughan@Sun.com #ifdef _KERNEL 14588662SJordan.Vaughan@Sun.com myhostid = zone_get_hostid(NULL); 14598662SJordan.Vaughan@Sun.com #else /* _KERNEL */ 14608662SJordan.Vaughan@Sun.com /* 14618662SJordan.Vaughan@Sun.com * We're emulating the system's hostid in userland, so 14628662SJordan.Vaughan@Sun.com * we can't use zone_get_hostid(). 14638662SJordan.Vaughan@Sun.com */ 14643975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 14658662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 14664178Slling if (hostid != 0 && myhostid != 0 && 14678662SJordan.Vaughan@Sun.com hostid != myhostid) { 14683975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 14693975Sek110237 "loaded as it was last accessed by " 14707706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 14713975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 14727754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 14733975Sek110237 (unsigned long)hostid); 14743975Sek110237 error = EBADF; 14753975Sek110237 goto out; 14763975Sek110237 } 14773975Sek110237 } 14783975Sek110237 147910594SGeorge.Wilson@Sun.COM spa_config_set(spa, nvconfig); 1480789Sahrens spa_unload(spa); 1481789Sahrens spa_deactivate(spa); 14828241SJeff.Bonwick@Sun.COM spa_activate(spa, orig_mode); 1483789Sahrens 148410921STim.Haley@Sun.COM return (spa_load(spa, state, B_TRUE)); 14851544Seschrock } 14861544Seschrock 14871544Seschrock if (zap_lookup(spa->spa_meta_objset, 14881544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 148910922SJeff.Bonwick@Sun.COM sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj) != 0) { 14901544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 14911544Seschrock VDEV_AUX_CORRUPT_DATA); 14921544Seschrock error = EIO; 14931544Seschrock goto out; 1494789Sahrens } 1495789Sahrens 14961544Seschrock /* 14972082Seschrock * Load the bit that tells us to use the new accounting function 14982082Seschrock * (raid-z deflation). If we have an older pool, this will not 14992082Seschrock * be present. 15002082Seschrock */ 15012082Seschrock error = zap_lookup(spa->spa_meta_objset, 15022082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 15032082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 15042082Seschrock if (error != 0 && error != ENOENT) { 15052082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15062082Seschrock VDEV_AUX_CORRUPT_DATA); 15072082Seschrock error = EIO; 15082082Seschrock goto out; 15092082Seschrock } 15102082Seschrock 15112082Seschrock /* 15121544Seschrock * Load the persistent error log. If we have an older pool, this will 15131544Seschrock * not be present. 15141544Seschrock */ 15151544Seschrock error = zap_lookup(spa->spa_meta_objset, 15161544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 15171544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 15181807Sbonwick if (error != 0 && error != ENOENT) { 15191544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15201544Seschrock VDEV_AUX_CORRUPT_DATA); 15211544Seschrock error = EIO; 15221544Seschrock goto out; 15231544Seschrock } 15241544Seschrock 15251544Seschrock error = zap_lookup(spa->spa_meta_objset, 15261544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 15271544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 15281544Seschrock if (error != 0 && error != ENOENT) { 15291544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15301544Seschrock VDEV_AUX_CORRUPT_DATA); 15311544Seschrock error = EIO; 15321544Seschrock goto out; 15331544Seschrock } 1534789Sahrens 1535789Sahrens /* 15362926Sek110237 * Load the history object. If we have an older pool, this 15372926Sek110237 * will not be present. 15382926Sek110237 */ 15392926Sek110237 error = zap_lookup(spa->spa_meta_objset, 15402926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 15412926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 15422926Sek110237 if (error != 0 && error != ENOENT) { 15432926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15442926Sek110237 VDEV_AUX_CORRUPT_DATA); 15452926Sek110237 error = EIO; 15462926Sek110237 goto out; 15472926Sek110237 } 15482926Sek110237 15492926Sek110237 /* 15502082Seschrock * Load any hot spares for this pool. 15512082Seschrock */ 15522082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 15535450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 15542082Seschrock if (error != 0 && error != ENOENT) { 15552082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15562082Seschrock VDEV_AUX_CORRUPT_DATA); 15572082Seschrock error = EIO; 15582082Seschrock goto out; 15592082Seschrock } 15602082Seschrock if (error == 0) { 15614577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 15625450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 15635450Sbrendan &spa->spa_spares.sav_config) != 0) { 15642082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15652082Seschrock VDEV_AUX_CORRUPT_DATA); 15662082Seschrock error = EIO; 15672082Seschrock goto out; 15682082Seschrock } 15692082Seschrock 15707754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 15712082Seschrock spa_load_spares(spa); 15727754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 15732082Seschrock } 15742082Seschrock 15755450Sbrendan /* 15765450Sbrendan * Load any level 2 ARC devices for this pool. 15775450Sbrendan */ 15785450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 15795450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 15805450Sbrendan &spa->spa_l2cache.sav_object); 15815450Sbrendan if (error != 0 && error != ENOENT) { 15825450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 15835450Sbrendan VDEV_AUX_CORRUPT_DATA); 15845450Sbrendan error = EIO; 15855450Sbrendan goto out; 15865450Sbrendan } 15875450Sbrendan if (error == 0) { 15885450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 15895450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 15905450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 15915450Sbrendan vdev_set_state(rvd, B_TRUE, 15925450Sbrendan VDEV_STATE_CANT_OPEN, 15935450Sbrendan VDEV_AUX_CORRUPT_DATA); 15945450Sbrendan error = EIO; 15955450Sbrendan goto out; 15965450Sbrendan } 15975450Sbrendan 15987754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 15995450Sbrendan spa_load_l2cache(spa); 16007754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 16015450Sbrendan } 16025450Sbrendan 16035094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 16044543Smarks 16053912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 16063912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 16073912Slling 16083912Slling if (error && error != ENOENT) { 16093912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 16103912Slling VDEV_AUX_CORRUPT_DATA); 16113912Slling error = EIO; 16123912Slling goto out; 16133912Slling } 16143912Slling 16153912Slling if (error == 0) { 16163912Slling (void) zap_lookup(spa->spa_meta_objset, 16173912Slling spa->spa_pool_props_object, 16184451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 16193912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 16204451Seschrock (void) zap_lookup(spa->spa_meta_objset, 16214451Seschrock spa->spa_pool_props_object, 16224451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 16234451Seschrock sizeof (uint64_t), 1, &autoreplace); 162410672SEric.Schrock@Sun.COM spa->spa_autoreplace = (autoreplace != 0); 16254543Smarks (void) zap_lookup(spa->spa_meta_objset, 16264543Smarks spa->spa_pool_props_object, 16274543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 16284543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 16295329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 16305329Sgw25295 spa->spa_pool_props_object, 16315329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 16325329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 16339816SGeorge.Wilson@Sun.COM (void) zap_lookup(spa->spa_meta_objset, 16349816SGeorge.Wilson@Sun.COM spa->spa_pool_props_object, 16359816SGeorge.Wilson@Sun.COM zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND), 16369816SGeorge.Wilson@Sun.COM sizeof (uint64_t), 1, &spa->spa_autoexpand); 163710922SJeff.Bonwick@Sun.COM (void) zap_lookup(spa->spa_meta_objset, 163810922SJeff.Bonwick@Sun.COM spa->spa_pool_props_object, 163910922SJeff.Bonwick@Sun.COM zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO), 164010922SJeff.Bonwick@Sun.COM sizeof (uint64_t), 1, &spa->spa_dedup_ditto); 16413912Slling } 16423912Slling 16432082Seschrock /* 16444451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 16454451Seschrock * the ZFS DE that it should not issue any faults for unopenable 16464451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 16474451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 16484451Seschrock * over. 16494451Seschrock */ 165010672SEric.Schrock@Sun.COM if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 16514451Seschrock spa_check_removed(spa->spa_root_vdev); 165210672SEric.Schrock@Sun.COM /* 165310672SEric.Schrock@Sun.COM * For the import case, this is done in spa_import(), because 165410672SEric.Schrock@Sun.COM * at this point we're using the spare definitions from 165510672SEric.Schrock@Sun.COM * the MOS config, not necessarily from the userland config. 165610672SEric.Schrock@Sun.COM */ 165710672SEric.Schrock@Sun.COM if (state != SPA_LOAD_IMPORT) { 165810672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 165910672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 166010672SEric.Schrock@Sun.COM } 166110672SEric.Schrock@Sun.COM } 16624451Seschrock 16634451Seschrock /* 16641986Seschrock * Load the vdev state for all toplevel vdevs. 1665789Sahrens */ 16661986Seschrock vdev_load(rvd); 1667789Sahrens 1668789Sahrens /* 1669789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1670789Sahrens */ 16717754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1672789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 16737754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1674789Sahrens 1675789Sahrens /* 1676789Sahrens * Check the state of the root vdev. If it can't be opened, it 1677789Sahrens * indicates one or more toplevel vdevs are faulted. 1678789Sahrens */ 16791544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 16801544Seschrock error = ENXIO; 16811544Seschrock goto out; 16821544Seschrock } 1683789Sahrens 168410922SJeff.Bonwick@Sun.COM /* 168510922SJeff.Bonwick@Sun.COM * Load the DDTs (dedup tables). 168610922SJeff.Bonwick@Sun.COM */ 168710922SJeff.Bonwick@Sun.COM error = ddt_load(spa); 168810922SJeff.Bonwick@Sun.COM if (error != 0) { 168910922SJeff.Bonwick@Sun.COM vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 169010922SJeff.Bonwick@Sun.COM VDEV_AUX_CORRUPT_DATA); 169110922SJeff.Bonwick@Sun.COM error = EIO; 169210922SJeff.Bonwick@Sun.COM goto out; 169310922SJeff.Bonwick@Sun.COM } 169410922SJeff.Bonwick@Sun.COM 169510956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 169610956SGeorge.Wilson@Sun.COM 169710921STim.Haley@Sun.COM if (state != SPA_LOAD_TRYIMPORT) { 169810921STim.Haley@Sun.COM error = spa_load_verify(spa); 169910921STim.Haley@Sun.COM if (error) { 170010921STim.Haley@Sun.COM vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 170110921STim.Haley@Sun.COM VDEV_AUX_CORRUPT_DATA); 170210921STim.Haley@Sun.COM goto out; 170310921STim.Haley@Sun.COM } 170410921STim.Haley@Sun.COM } 170510921STim.Haley@Sun.COM 170610922SJeff.Bonwick@Sun.COM /* 170710922SJeff.Bonwick@Sun.COM * Load the intent log state and check log integrity. 170810922SJeff.Bonwick@Sun.COM */ 170910922SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE, 171010922SJeff.Bonwick@Sun.COM &nvroot) == 0); 171110922SJeff.Bonwick@Sun.COM spa_load_log_state(spa, nvroot); 171210922SJeff.Bonwick@Sun.COM nvlist_free(nvconfig); 171310922SJeff.Bonwick@Sun.COM 171410922SJeff.Bonwick@Sun.COM if (spa_check_logs(spa)) { 171510922SJeff.Bonwick@Sun.COM vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 171610922SJeff.Bonwick@Sun.COM VDEV_AUX_BAD_LOG); 171710922SJeff.Bonwick@Sun.COM error = ENXIO; 171810922SJeff.Bonwick@Sun.COM ereport = FM_EREPORT_ZFS_LOG_REPLAY; 171910922SJeff.Bonwick@Sun.COM goto out; 172010922SJeff.Bonwick@Sun.COM } 172110922SJeff.Bonwick@Sun.COM 172210921STim.Haley@Sun.COM if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 172310921STim.Haley@Sun.COM spa->spa_load_max_txg == UINT64_MAX)) { 17241635Sbonwick dmu_tx_t *tx; 17251635Sbonwick int need_update = B_FALSE; 17268241SJeff.Bonwick@Sun.COM 17278241SJeff.Bonwick@Sun.COM ASSERT(state != SPA_LOAD_TRYIMPORT); 17281601Sbonwick 17291635Sbonwick /* 17301635Sbonwick * Claim log blocks that haven't been committed yet. 17311635Sbonwick * This must all happen in a single txg. 173210922SJeff.Bonwick@Sun.COM * Note: spa_claim_max_txg is updated by spa_claim_notify(), 173310922SJeff.Bonwick@Sun.COM * invoked from zil_claim_log_block()'s i/o done callback. 173410921STim.Haley@Sun.COM * Price of rollback is that we abandon the log. 17351635Sbonwick */ 173610922SJeff.Bonwick@Sun.COM spa->spa_claiming = B_TRUE; 173710922SJeff.Bonwick@Sun.COM 17381601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1739789Sahrens spa_first_txg(spa)); 17407754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 17412417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1742789Sahrens dmu_tx_commit(tx); 1743789Sahrens 174410922SJeff.Bonwick@Sun.COM spa->spa_claiming = B_FALSE; 174510922SJeff.Bonwick@Sun.COM 17469701SGeorge.Wilson@Sun.COM spa->spa_log_state = SPA_LOG_GOOD; 1747789Sahrens spa->spa_sync_on = B_TRUE; 1748789Sahrens txg_sync_start(spa->spa_dsl_pool); 1749789Sahrens 1750789Sahrens /* 175110922SJeff.Bonwick@Sun.COM * Wait for all claims to sync. We sync up to the highest 175210922SJeff.Bonwick@Sun.COM * claimed log block birth time so that claimed log blocks 175310922SJeff.Bonwick@Sun.COM * don't appear to be from the future. spa_claim_max_txg 175410922SJeff.Bonwick@Sun.COM * will have been set for us by either zil_check_log_chain() 175510922SJeff.Bonwick@Sun.COM * (invoked from spa_check_logs()) or zil_claim() above. 1756789Sahrens */ 175710922SJeff.Bonwick@Sun.COM txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 17581585Sbonwick 17591585Sbonwick /* 17601635Sbonwick * If the config cache is stale, or we have uninitialized 17611635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 176210100SLin.Ling@Sun.COM * 176310100SLin.Ling@Sun.COM * If spa_load_verbatim is true, trust the current 176410100SLin.Ling@Sun.COM * in-core spa_config and update the disk labels. 17651585Sbonwick */ 17661635Sbonwick if (config_cache_txg != spa->spa_config_txg || 176710921STim.Haley@Sun.COM state == SPA_LOAD_IMPORT || spa->spa_load_verbatim || 176810921STim.Haley@Sun.COM state == SPA_LOAD_RECOVER) 17691635Sbonwick need_update = B_TRUE; 17701635Sbonwick 17718241SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) 17721635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 17731635Sbonwick need_update = B_TRUE; 17741585Sbonwick 17751585Sbonwick /* 17761635Sbonwick * Update the config cache asychronously in case we're the 17771635Sbonwick * root pool, in which case the config cache isn't writable yet. 17781585Sbonwick */ 17791635Sbonwick if (need_update) 17801635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 17818241SJeff.Bonwick@Sun.COM 17828241SJeff.Bonwick@Sun.COM /* 17838241SJeff.Bonwick@Sun.COM * Check all DTLs to see if anything needs resilvering. 17848241SJeff.Bonwick@Sun.COM */ 17858241SJeff.Bonwick@Sun.COM if (vdev_resilver_needed(rvd, NULL, NULL)) 17868241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 178710298SMatthew.Ahrens@Sun.COM 178810298SMatthew.Ahrens@Sun.COM /* 178910298SMatthew.Ahrens@Sun.COM * Delete any inconsistent datasets. 179010298SMatthew.Ahrens@Sun.COM */ 179110298SMatthew.Ahrens@Sun.COM (void) dmu_objset_find(spa_name(spa), 179210298SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 179310342Schris.kirby@sun.com 179410342Schris.kirby@sun.com /* 179510342Schris.kirby@sun.com * Clean up any stale temporary dataset userrefs. 179610342Schris.kirby@sun.com */ 179710342Schris.kirby@sun.com dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 1798789Sahrens } 1799789Sahrens 18001544Seschrock error = 0; 18011544Seschrock out: 180210921STim.Haley@Sun.COM 18037046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 18042082Seschrock if (error && error != EBADF) 18057294Sperrin zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 18061544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 18071544Seschrock spa->spa_ena = 0; 18081544Seschrock 18091544Seschrock return (error); 1810789Sahrens } 1811789Sahrens 181210921STim.Haley@Sun.COM static int 181310921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 181410921STim.Haley@Sun.COM { 181510921STim.Haley@Sun.COM spa_unload(spa); 181610921STim.Haley@Sun.COM spa_deactivate(spa); 181710921STim.Haley@Sun.COM 181810921STim.Haley@Sun.COM spa->spa_load_max_txg--; 181910921STim.Haley@Sun.COM 182010921STim.Haley@Sun.COM spa_activate(spa, spa_mode_global); 182110921STim.Haley@Sun.COM spa_async_suspend(spa); 182210921STim.Haley@Sun.COM 182310921STim.Haley@Sun.COM return (spa_load(spa, state, mosconfig)); 182410921STim.Haley@Sun.COM } 182510921STim.Haley@Sun.COM 182610921STim.Haley@Sun.COM static int 182710921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 182810921STim.Haley@Sun.COM uint64_t max_request, boolean_t extreme) 182910921STim.Haley@Sun.COM { 183010921STim.Haley@Sun.COM nvlist_t *config = NULL; 183110921STim.Haley@Sun.COM int load_error, rewind_error; 183210921STim.Haley@Sun.COM uint64_t safe_rollback_txg; 183310921STim.Haley@Sun.COM uint64_t min_txg; 183410921STim.Haley@Sun.COM 183511026STim.Haley@Sun.COM if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 183610921STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_load_txg; 183711026STim.Haley@Sun.COM spa->spa_log_state = SPA_LOG_CLEAR; 183811026STim.Haley@Sun.COM } else { 183910921STim.Haley@Sun.COM spa->spa_load_max_txg = max_request; 184011026STim.Haley@Sun.COM } 184110921STim.Haley@Sun.COM 184210921STim.Haley@Sun.COM load_error = rewind_error = spa_load(spa, state, mosconfig); 184310921STim.Haley@Sun.COM if (load_error == 0) 184410921STim.Haley@Sun.COM return (0); 184510921STim.Haley@Sun.COM 184610921STim.Haley@Sun.COM if (spa->spa_root_vdev != NULL) 184710921STim.Haley@Sun.COM config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 184810921STim.Haley@Sun.COM 184910921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 185010921STim.Haley@Sun.COM spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 185110921STim.Haley@Sun.COM 185210921STim.Haley@Sun.COM /* specific txg requested */ 185310921STim.Haley@Sun.COM if (spa->spa_load_max_txg != UINT64_MAX && !extreme) { 185410921STim.Haley@Sun.COM nvlist_free(config); 185510921STim.Haley@Sun.COM return (load_error); 185610921STim.Haley@Sun.COM } 185710921STim.Haley@Sun.COM 185810921STim.Haley@Sun.COM /* Price of rolling back is discarding txgs, including log */ 185910921STim.Haley@Sun.COM if (state == SPA_LOAD_RECOVER) 186010921STim.Haley@Sun.COM spa->spa_log_state = SPA_LOG_CLEAR; 186110921STim.Haley@Sun.COM 186210921STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 186310921STim.Haley@Sun.COM safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE; 186410921STim.Haley@Sun.COM 186510921STim.Haley@Sun.COM min_txg = extreme ? TXG_INITIAL : safe_rollback_txg; 186610921STim.Haley@Sun.COM while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) { 186710921STim.Haley@Sun.COM if (spa->spa_load_max_txg < safe_rollback_txg) 186810921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_TRUE; 186910921STim.Haley@Sun.COM rewind_error = spa_load_retry(spa, state, mosconfig); 187010921STim.Haley@Sun.COM } 187110921STim.Haley@Sun.COM 187210921STim.Haley@Sun.COM if (config) 187310921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa, config); 187410921STim.Haley@Sun.COM 187510921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_FALSE; 187610921STim.Haley@Sun.COM spa->spa_load_max_txg = UINT64_MAX; 187710921STim.Haley@Sun.COM 187810921STim.Haley@Sun.COM if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 187910921STim.Haley@Sun.COM spa_config_set(spa, config); 188010921STim.Haley@Sun.COM 188110921STim.Haley@Sun.COM return (state == SPA_LOAD_RECOVER ? rewind_error : load_error); 188210921STim.Haley@Sun.COM } 188310921STim.Haley@Sun.COM 1884789Sahrens /* 1885789Sahrens * Pool Open/Import 1886789Sahrens * 1887789Sahrens * The import case is identical to an open except that the configuration is sent 1888789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1889789Sahrens * case of an open, the pool configuration will exist in the 18904451Seschrock * POOL_STATE_UNINITIALIZED state. 1891789Sahrens * 1892789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1893789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1894789Sahrens * ambiguous state. 1895789Sahrens */ 1896789Sahrens static int 189710921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 189810921STim.Haley@Sun.COM nvlist_t **config) 1899789Sahrens { 1900789Sahrens spa_t *spa; 190110921STim.Haley@Sun.COM boolean_t norewind; 190210921STim.Haley@Sun.COM boolean_t extreme; 190310921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 190410921STim.Haley@Sun.COM spa_load_state_t state = SPA_LOAD_OPEN; 1905789Sahrens int error; 1906789Sahrens int locked = B_FALSE; 1907789Sahrens 1908789Sahrens *spapp = NULL; 1909789Sahrens 191010921STim.Haley@Sun.COM zpool_get_rewind_policy(nvpolicy, &policy); 191110921STim.Haley@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 191210921STim.Haley@Sun.COM state = SPA_LOAD_RECOVER; 191310921STim.Haley@Sun.COM norewind = (policy.zrp_request == ZPOOL_NO_REWIND); 191410921STim.Haley@Sun.COM extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0); 191510921STim.Haley@Sun.COM 1916789Sahrens /* 1917789Sahrens * As disgusting as this is, we need to support recursive calls to this 1918789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1919789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1920789Sahrens * avoid dsl_dir_open() calling this in the first place. 1921789Sahrens */ 1922789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1923789Sahrens mutex_enter(&spa_namespace_lock); 1924789Sahrens locked = B_TRUE; 1925789Sahrens } 1926789Sahrens 1927789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1928789Sahrens if (locked) 1929789Sahrens mutex_exit(&spa_namespace_lock); 1930789Sahrens return (ENOENT); 1931789Sahrens } 193210921STim.Haley@Sun.COM 1933789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1934789Sahrens 19358241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1936789Sahrens 193710921STim.Haley@Sun.COM if (spa->spa_last_open_failed && norewind) { 193810921STim.Haley@Sun.COM if (config != NULL && spa->spa_config) 193910921STim.Haley@Sun.COM VERIFY(nvlist_dup(spa->spa_config, 194010921STim.Haley@Sun.COM config, KM_SLEEP) == 0); 194110921STim.Haley@Sun.COM spa_deactivate(spa); 194210921STim.Haley@Sun.COM if (locked) 194310921STim.Haley@Sun.COM mutex_exit(&spa_namespace_lock); 194410921STim.Haley@Sun.COM return (spa->spa_last_open_failed); 194510921STim.Haley@Sun.COM } 194610921STim.Haley@Sun.COM 194710921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 194810921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 194910921STim.Haley@Sun.COM 195010921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 195110921STim.Haley@Sun.COM extreme); 1952789Sahrens 1953789Sahrens if (error == EBADF) { 1954789Sahrens /* 19551986Seschrock * If vdev_validate() returns failure (indicated by 19561986Seschrock * EBADF), it indicates that one of the vdevs indicates 19571986Seschrock * that the pool has been exported or destroyed. If 19581986Seschrock * this is the case, the config cache is out of sync and 19591986Seschrock * we should remove the pool from the namespace. 1960789Sahrens */ 1961789Sahrens spa_unload(spa); 1962789Sahrens spa_deactivate(spa); 19636643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1964789Sahrens spa_remove(spa); 1965789Sahrens if (locked) 1966789Sahrens mutex_exit(&spa_namespace_lock); 1967789Sahrens return (ENOENT); 19681544Seschrock } 19691544Seschrock 19701544Seschrock if (error) { 1971789Sahrens /* 1972789Sahrens * We can't open the pool, but we still have useful 1973789Sahrens * information: the state of each vdev after the 1974789Sahrens * attempted vdev_open(). Return this to the user. 1975789Sahrens */ 197610921STim.Haley@Sun.COM if (config != NULL && spa->spa_config) 197710921STim.Haley@Sun.COM VERIFY(nvlist_dup(spa->spa_config, config, 197810921STim.Haley@Sun.COM KM_SLEEP) == 0); 1979789Sahrens spa_unload(spa); 1980789Sahrens spa_deactivate(spa); 198110921STim.Haley@Sun.COM spa->spa_last_open_failed = error; 1982789Sahrens if (locked) 1983789Sahrens mutex_exit(&spa_namespace_lock); 1984789Sahrens *spapp = NULL; 1985789Sahrens return (error); 1986789Sahrens } 198710921STim.Haley@Sun.COM 1988789Sahrens } 1989789Sahrens 1990789Sahrens spa_open_ref(spa, tag); 19914451Seschrock 199210921STim.Haley@Sun.COM 199310921STim.Haley@Sun.COM if (config != NULL) 199410921STim.Haley@Sun.COM *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 199510921STim.Haley@Sun.COM 199611026STim.Haley@Sun.COM if (locked) { 199711026STim.Haley@Sun.COM spa->spa_last_open_failed = 0; 199811026STim.Haley@Sun.COM spa->spa_last_ubsync_txg = 0; 199911026STim.Haley@Sun.COM spa->spa_load_txg = 0; 2000789Sahrens mutex_exit(&spa_namespace_lock); 200111026STim.Haley@Sun.COM } 2002789Sahrens 2003789Sahrens *spapp = spa; 2004789Sahrens 2005789Sahrens return (0); 2006789Sahrens } 2007789Sahrens 2008789Sahrens int 200910921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 201010921STim.Haley@Sun.COM nvlist_t **config) 201110921STim.Haley@Sun.COM { 201210921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, policy, config)); 201310921STim.Haley@Sun.COM } 201410921STim.Haley@Sun.COM 201510921STim.Haley@Sun.COM int 2016789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2017789Sahrens { 201810921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, NULL, NULL)); 2019789Sahrens } 2020789Sahrens 20211544Seschrock /* 20221544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 20231544Seschrock * preventing it from being exported or destroyed. 20241544Seschrock */ 20251544Seschrock spa_t * 20261544Seschrock spa_inject_addref(char *name) 20271544Seschrock { 20281544Seschrock spa_t *spa; 20291544Seschrock 20301544Seschrock mutex_enter(&spa_namespace_lock); 20311544Seschrock if ((spa = spa_lookup(name)) == NULL) { 20321544Seschrock mutex_exit(&spa_namespace_lock); 20331544Seschrock return (NULL); 20341544Seschrock } 20351544Seschrock spa->spa_inject_ref++; 20361544Seschrock mutex_exit(&spa_namespace_lock); 20371544Seschrock 20381544Seschrock return (spa); 20391544Seschrock } 20401544Seschrock 20411544Seschrock void 20421544Seschrock spa_inject_delref(spa_t *spa) 20431544Seschrock { 20441544Seschrock mutex_enter(&spa_namespace_lock); 20451544Seschrock spa->spa_inject_ref--; 20461544Seschrock mutex_exit(&spa_namespace_lock); 20471544Seschrock } 20481544Seschrock 20495450Sbrendan /* 20505450Sbrendan * Add spares device information to the nvlist. 20515450Sbrendan */ 20522082Seschrock static void 20532082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 20542082Seschrock { 20552082Seschrock nvlist_t **spares; 20562082Seschrock uint_t i, nspares; 20572082Seschrock nvlist_t *nvroot; 20582082Seschrock uint64_t guid; 20592082Seschrock vdev_stat_t *vs; 20602082Seschrock uint_t vsc; 20613377Seschrock uint64_t pool; 20622082Seschrock 20639425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 20649425SEric.Schrock@Sun.COM 20655450Sbrendan if (spa->spa_spares.sav_count == 0) 20662082Seschrock return; 20672082Seschrock 20682082Seschrock VERIFY(nvlist_lookup_nvlist(config, 20692082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 20705450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 20712082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 20722082Seschrock if (nspares != 0) { 20732082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 20742082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 20752082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 20762082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 20772082Seschrock 20782082Seschrock /* 20792082Seschrock * Go through and find any spares which have since been 20802082Seschrock * repurposed as an active spare. If this is the case, update 20812082Seschrock * their status appropriately. 20822082Seschrock */ 20832082Seschrock for (i = 0; i < nspares; i++) { 20842082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 20852082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 20867214Slling if (spa_spare_exists(guid, &pool, NULL) && 20877214Slling pool != 0ULL) { 20882082Seschrock VERIFY(nvlist_lookup_uint64_array( 20892082Seschrock spares[i], ZPOOL_CONFIG_STATS, 20902082Seschrock (uint64_t **)&vs, &vsc) == 0); 20912082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 20922082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 20932082Seschrock } 20942082Seschrock } 20952082Seschrock } 20962082Seschrock } 20972082Seschrock 20985450Sbrendan /* 20995450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 21005450Sbrendan */ 21015450Sbrendan static void 21025450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 21035450Sbrendan { 21045450Sbrendan nvlist_t **l2cache; 21055450Sbrendan uint_t i, j, nl2cache; 21065450Sbrendan nvlist_t *nvroot; 21075450Sbrendan uint64_t guid; 21085450Sbrendan vdev_t *vd; 21095450Sbrendan vdev_stat_t *vs; 21105450Sbrendan uint_t vsc; 21115450Sbrendan 21129425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 21139425SEric.Schrock@Sun.COM 21145450Sbrendan if (spa->spa_l2cache.sav_count == 0) 21155450Sbrendan return; 21165450Sbrendan 21175450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 21185450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 21195450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 21205450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 21215450Sbrendan if (nl2cache != 0) { 21225450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 21235450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21245450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 21255450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 21265450Sbrendan 21275450Sbrendan /* 21285450Sbrendan * Update level 2 cache device stats. 21295450Sbrendan */ 21305450Sbrendan 21315450Sbrendan for (i = 0; i < nl2cache; i++) { 21325450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 21335450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 21345450Sbrendan 21355450Sbrendan vd = NULL; 21365450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 21375450Sbrendan if (guid == 21385450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 21395450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 21405450Sbrendan break; 21415450Sbrendan } 21425450Sbrendan } 21435450Sbrendan ASSERT(vd != NULL); 21445450Sbrendan 21455450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 21465450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 21475450Sbrendan vdev_get_stats(vd, vs); 21485450Sbrendan } 21495450Sbrendan } 21505450Sbrendan } 21515450Sbrendan 2152789Sahrens int 21531544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 2154789Sahrens { 2155789Sahrens int error; 2156789Sahrens spa_t *spa; 2157789Sahrens 2158789Sahrens *config = NULL; 215910921STim.Haley@Sun.COM error = spa_open_common(name, &spa, FTAG, NULL, config); 2160789Sahrens 21619425SEric.Schrock@Sun.COM if (spa != NULL) { 21629425SEric.Schrock@Sun.COM /* 21639425SEric.Schrock@Sun.COM * This still leaves a window of inconsistency where the spares 21649425SEric.Schrock@Sun.COM * or l2cache devices could change and the config would be 21659425SEric.Schrock@Sun.COM * self-inconsistent. 21669425SEric.Schrock@Sun.COM */ 21679425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 21689425SEric.Schrock@Sun.COM 21699425SEric.Schrock@Sun.COM if (*config != NULL) { 21707754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 21719425SEric.Schrock@Sun.COM ZPOOL_CONFIG_ERRCOUNT, 21729425SEric.Schrock@Sun.COM spa_get_errlog_size(spa)) == 0); 21739425SEric.Schrock@Sun.COM 21749425SEric.Schrock@Sun.COM if (spa_suspended(spa)) 21759425SEric.Schrock@Sun.COM VERIFY(nvlist_add_uint64(*config, 21769425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SUSPENDED, 21779425SEric.Schrock@Sun.COM spa->spa_failmode) == 0); 21789425SEric.Schrock@Sun.COM 21799425SEric.Schrock@Sun.COM spa_add_spares(spa, *config); 21809425SEric.Schrock@Sun.COM spa_add_l2cache(spa, *config); 21819425SEric.Schrock@Sun.COM } 21822082Seschrock } 21832082Seschrock 21841544Seschrock /* 21851544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 21861544Seschrock * and call spa_lookup() directly. 21871544Seschrock */ 21881544Seschrock if (altroot) { 21891544Seschrock if (spa == NULL) { 21901544Seschrock mutex_enter(&spa_namespace_lock); 21911544Seschrock spa = spa_lookup(name); 21921544Seschrock if (spa) 21931544Seschrock spa_altroot(spa, altroot, buflen); 21941544Seschrock else 21951544Seschrock altroot[0] = '\0'; 21961544Seschrock spa = NULL; 21971544Seschrock mutex_exit(&spa_namespace_lock); 21981544Seschrock } else { 21991544Seschrock spa_altroot(spa, altroot, buflen); 22001544Seschrock } 22011544Seschrock } 22021544Seschrock 22039425SEric.Schrock@Sun.COM if (spa != NULL) { 22049425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 2205789Sahrens spa_close(spa, FTAG); 22069425SEric.Schrock@Sun.COM } 2207789Sahrens 2208789Sahrens return (error); 2209789Sahrens } 2210789Sahrens 2211789Sahrens /* 22125450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 22135450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 22145450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 22155450Sbrendan * specified, as long as they are well-formed. 22162082Seschrock */ 22172082Seschrock static int 22185450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 22195450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 22205450Sbrendan vdev_labeltype_t label) 22212082Seschrock { 22225450Sbrendan nvlist_t **dev; 22235450Sbrendan uint_t i, ndev; 22242082Seschrock vdev_t *vd; 22252082Seschrock int error; 22262082Seschrock 22277754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 22287754SJeff.Bonwick@Sun.COM 22292082Seschrock /* 22305450Sbrendan * It's acceptable to have no devs specified. 22312082Seschrock */ 22325450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 22332082Seschrock return (0); 22342082Seschrock 22355450Sbrendan if (ndev == 0) 22362082Seschrock return (EINVAL); 22372082Seschrock 22382082Seschrock /* 22395450Sbrendan * Make sure the pool is formatted with a version that supports this 22405450Sbrendan * device type. 22412082Seschrock */ 22425450Sbrendan if (spa_version(spa) < version) 22432082Seschrock return (ENOTSUP); 22442082Seschrock 22453377Seschrock /* 22465450Sbrendan * Set the pending device list so we correctly handle device in-use 22473377Seschrock * checking. 22483377Seschrock */ 22495450Sbrendan sav->sav_pending = dev; 22505450Sbrendan sav->sav_npending = ndev; 22515450Sbrendan 22525450Sbrendan for (i = 0; i < ndev; i++) { 22535450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 22542082Seschrock mode)) != 0) 22553377Seschrock goto out; 22562082Seschrock 22572082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 22582082Seschrock vdev_free(vd); 22593377Seschrock error = EINVAL; 22603377Seschrock goto out; 22612082Seschrock } 22622082Seschrock 22635450Sbrendan /* 22647754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 22657754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 22665450Sbrendan */ 22677754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 22685450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 22695450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 22705450Sbrendan error = ENOTBLK; 22715450Sbrendan goto out; 22725450Sbrendan } 22737754SJeff.Bonwick@Sun.COM #endif 22742082Seschrock vd->vdev_top = vd; 22753377Seschrock 22763377Seschrock if ((error = vdev_open(vd)) == 0 && 22775450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 22785450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 22793377Seschrock vd->vdev_guid) == 0); 22802082Seschrock } 22812082Seschrock 22822082Seschrock vdev_free(vd); 22833377Seschrock 22845450Sbrendan if (error && 22855450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 22863377Seschrock goto out; 22873377Seschrock else 22883377Seschrock error = 0; 22892082Seschrock } 22902082Seschrock 22913377Seschrock out: 22925450Sbrendan sav->sav_pending = NULL; 22935450Sbrendan sav->sav_npending = 0; 22943377Seschrock return (error); 22952082Seschrock } 22962082Seschrock 22975450Sbrendan static int 22985450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 22995450Sbrendan { 23005450Sbrendan int error; 23015450Sbrendan 23027754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 23037754SJeff.Bonwick@Sun.COM 23045450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 23055450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 23065450Sbrendan VDEV_LABEL_SPARE)) != 0) { 23075450Sbrendan return (error); 23085450Sbrendan } 23095450Sbrendan 23105450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 23115450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 23125450Sbrendan VDEV_LABEL_L2CACHE)); 23135450Sbrendan } 23145450Sbrendan 23155450Sbrendan static void 23165450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 23175450Sbrendan const char *config) 23185450Sbrendan { 23195450Sbrendan int i; 23205450Sbrendan 23215450Sbrendan if (sav->sav_config != NULL) { 23225450Sbrendan nvlist_t **olddevs; 23235450Sbrendan uint_t oldndevs; 23245450Sbrendan nvlist_t **newdevs; 23255450Sbrendan 23265450Sbrendan /* 23275450Sbrendan * Generate new dev list by concatentating with the 23285450Sbrendan * current dev list. 23295450Sbrendan */ 23305450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 23315450Sbrendan &olddevs, &oldndevs) == 0); 23325450Sbrendan 23335450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 23345450Sbrendan (ndevs + oldndevs), KM_SLEEP); 23355450Sbrendan for (i = 0; i < oldndevs; i++) 23365450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 23375450Sbrendan KM_SLEEP) == 0); 23385450Sbrendan for (i = 0; i < ndevs; i++) 23395450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 23405450Sbrendan KM_SLEEP) == 0); 23415450Sbrendan 23425450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 23435450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 23445450Sbrendan 23455450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 23465450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 23475450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 23485450Sbrendan nvlist_free(newdevs[i]); 23495450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 23505450Sbrendan } else { 23515450Sbrendan /* 23525450Sbrendan * Generate a new dev list. 23535450Sbrendan */ 23545450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 23555450Sbrendan KM_SLEEP) == 0); 23565450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 23575450Sbrendan devs, ndevs) == 0); 23585450Sbrendan } 23595450Sbrendan } 23605450Sbrendan 23615450Sbrendan /* 23625450Sbrendan * Stop and drop level 2 ARC devices 23635450Sbrendan */ 23645450Sbrendan void 23655450Sbrendan spa_l2cache_drop(spa_t *spa) 23665450Sbrendan { 23675450Sbrendan vdev_t *vd; 23685450Sbrendan int i; 23695450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 23705450Sbrendan 23715450Sbrendan for (i = 0; i < sav->sav_count; i++) { 23725450Sbrendan uint64_t pool; 23735450Sbrendan 23745450Sbrendan vd = sav->sav_vdevs[i]; 23755450Sbrendan ASSERT(vd != NULL); 23765450Sbrendan 23778241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 23788241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 23795450Sbrendan l2arc_remove_vdev(vd); 23805450Sbrendan if (vd->vdev_isl2cache) 23815450Sbrendan spa_l2cache_remove(vd); 23825450Sbrendan vdev_clear_stats(vd); 23835450Sbrendan (void) vdev_close(vd); 23845450Sbrendan } 23855450Sbrendan } 23865450Sbrendan 23872082Seschrock /* 2388789Sahrens * Pool Creation 2389789Sahrens */ 2390789Sahrens int 23915094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 23927184Stimh const char *history_str, nvlist_t *zplprops) 2393789Sahrens { 2394789Sahrens spa_t *spa; 23955094Slling char *altroot = NULL; 23961635Sbonwick vdev_t *rvd; 2397789Sahrens dsl_pool_t *dp; 2398789Sahrens dmu_tx_t *tx; 23999816SGeorge.Wilson@Sun.COM int error = 0; 2400789Sahrens uint64_t txg = TXG_INITIAL; 24015450Sbrendan nvlist_t **spares, **l2cache; 24025450Sbrendan uint_t nspares, nl2cache; 24035094Slling uint64_t version; 2404789Sahrens 2405789Sahrens /* 2406789Sahrens * If this pool already exists, return failure. 2407789Sahrens */ 2408789Sahrens mutex_enter(&spa_namespace_lock); 2409789Sahrens if (spa_lookup(pool) != NULL) { 2410789Sahrens mutex_exit(&spa_namespace_lock); 2411789Sahrens return (EEXIST); 2412789Sahrens } 2413789Sahrens 2414789Sahrens /* 2415789Sahrens * Allocate a new spa_t structure. 2416789Sahrens */ 24175094Slling (void) nvlist_lookup_string(props, 24185094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 241910921STim.Haley@Sun.COM spa = spa_add(pool, NULL, altroot); 24208241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2421789Sahrens 24225094Slling if (props && (error = spa_prop_validate(spa, props))) { 24235094Slling spa_deactivate(spa); 24245094Slling spa_remove(spa); 24256643Seschrock mutex_exit(&spa_namespace_lock); 24265094Slling return (error); 24275094Slling } 24285094Slling 24295094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 24305094Slling &version) != 0) 24315094Slling version = SPA_VERSION; 24325094Slling ASSERT(version <= SPA_VERSION); 243310922SJeff.Bonwick@Sun.COM 243410922SJeff.Bonwick@Sun.COM spa->spa_first_txg = txg; 243510922SJeff.Bonwick@Sun.COM spa->spa_uberblock.ub_txg = txg - 1; 24365094Slling spa->spa_uberblock.ub_version = version; 2437789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2438789Sahrens 24391635Sbonwick /* 24409234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 24419234SGeorge.Wilson@Sun.COM */ 24429630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 24439630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 24449234SGeorge.Wilson@Sun.COM 24459234SGeorge.Wilson@Sun.COM /* 24461635Sbonwick * Create the root vdev. 24471635Sbonwick */ 24487754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24491635Sbonwick 24502082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 24512082Seschrock 24522082Seschrock ASSERT(error != 0 || rvd != NULL); 24532082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 24542082Seschrock 24555913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 24561635Sbonwick error = EINVAL; 24572082Seschrock 24582082Seschrock if (error == 0 && 24592082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 24605450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 24612082Seschrock VDEV_ALLOC_ADD)) == 0) { 24629816SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 24639816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(rvd->vdev_child[c]); 24649816SGeorge.Wilson@Sun.COM vdev_expand(rvd->vdev_child[c], txg); 24659816SGeorge.Wilson@Sun.COM } 24661635Sbonwick } 24671635Sbonwick 24687754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2469789Sahrens 24702082Seschrock if (error != 0) { 2471789Sahrens spa_unload(spa); 2472789Sahrens spa_deactivate(spa); 2473789Sahrens spa_remove(spa); 2474789Sahrens mutex_exit(&spa_namespace_lock); 2475789Sahrens return (error); 2476789Sahrens } 2477789Sahrens 24782082Seschrock /* 24792082Seschrock * Get the list of spares, if specified. 24802082Seschrock */ 24812082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 24822082Seschrock &spares, &nspares) == 0) { 24835450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 24842082Seschrock KM_SLEEP) == 0); 24855450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 24862082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 24877754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24882082Seschrock spa_load_spares(spa); 24897754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 24905450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 24915450Sbrendan } 24925450Sbrendan 24935450Sbrendan /* 24945450Sbrendan * Get the list of level 2 cache devices, if specified. 24955450Sbrendan */ 24965450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 24975450Sbrendan &l2cache, &nl2cache) == 0) { 24985450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 24995450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 25005450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 25015450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 25027754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 25035450Sbrendan spa_load_l2cache(spa); 25047754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 25055450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 25062082Seschrock } 25072082Seschrock 25087184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2509789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2510789Sahrens 251110956SGeorge.Wilson@Sun.COM /* 251210956SGeorge.Wilson@Sun.COM * Create DDTs (dedup tables). 251310956SGeorge.Wilson@Sun.COM */ 251410956SGeorge.Wilson@Sun.COM ddt_create(spa); 251510956SGeorge.Wilson@Sun.COM 251610956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 251710956SGeorge.Wilson@Sun.COM 2518789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2519789Sahrens 2520789Sahrens /* 2521789Sahrens * Create the pool config object. 2522789Sahrens */ 2523789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 25247497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2525789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2526789Sahrens 25271544Seschrock if (zap_add(spa->spa_meta_objset, 2528789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 25291544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 25301544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 25311544Seschrock } 2532789Sahrens 25335094Slling /* Newly created pools with the right version are always deflated. */ 25345094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 25355094Slling spa->spa_deflate = TRUE; 25365094Slling if (zap_add(spa->spa_meta_objset, 25375094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 25385094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 25395094Slling cmn_err(CE_PANIC, "failed to add deflate"); 25405094Slling } 25412082Seschrock } 25422082Seschrock 2543789Sahrens /* 2544789Sahrens * Create the deferred-free bplist object. Turn off compression 2545789Sahrens * because sync-to-convergence takes longer if the blocksize 2546789Sahrens * keeps changing. 2547789Sahrens */ 254810922SJeff.Bonwick@Sun.COM spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset, 2549789Sahrens 1 << 14, tx); 255010922SJeff.Bonwick@Sun.COM dmu_object_set_compress(spa->spa_meta_objset, 255110922SJeff.Bonwick@Sun.COM spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx); 2552789Sahrens 25531544Seschrock if (zap_add(spa->spa_meta_objset, 2554789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 255510922SJeff.Bonwick@Sun.COM sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) { 25561544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 25571544Seschrock } 2558789Sahrens 25592926Sek110237 /* 25602926Sek110237 * Create the pool's history object. 25612926Sek110237 */ 25625094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 25635094Slling spa_history_create_obj(spa, tx); 25645094Slling 25655094Slling /* 25665094Slling * Set pool properties. 25675094Slling */ 25685094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 25695094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 25705329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 25719816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 257210922SJeff.Bonwick@Sun.COM 25738525SEric.Schrock@Sun.COM if (props != NULL) { 25748525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 25755094Slling spa_sync_props(spa, props, CRED(), tx); 25768525SEric.Schrock@Sun.COM } 25772926Sek110237 2578789Sahrens dmu_tx_commit(tx); 2579789Sahrens 2580789Sahrens spa->spa_sync_on = B_TRUE; 2581789Sahrens txg_sync_start(spa->spa_dsl_pool); 2582789Sahrens 2583789Sahrens /* 2584789Sahrens * We explicitly wait for the first transaction to complete so that our 2585789Sahrens * bean counters are appropriately updated. 2586789Sahrens */ 2587789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2588789Sahrens 25896643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2590789Sahrens 25915094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 25924715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 25939946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_CREATE); 25944715Sek110237 25958667SGeorge.Wilson@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 25968667SGeorge.Wilson@Sun.COM 2597789Sahrens mutex_exit(&spa_namespace_lock); 2598789Sahrens 2599789Sahrens return (0); 2600789Sahrens } 2601789Sahrens 26026423Sgw25295 #ifdef _KERNEL 26036423Sgw25295 /* 26049790SLin.Ling@Sun.COM * Get the root pool information from the root disk, then import the root pool 26059790SLin.Ling@Sun.COM * during the system boot up time. 26066423Sgw25295 */ 26079790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 26089790SLin.Ling@Sun.COM 26099790SLin.Ling@Sun.COM static nvlist_t * 26109790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 26116423Sgw25295 { 26129790SLin.Ling@Sun.COM nvlist_t *config; 26136423Sgw25295 nvlist_t *nvtop, *nvroot; 26146423Sgw25295 uint64_t pgid; 26156423Sgw25295 26169790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 26179790SLin.Ling@Sun.COM return (NULL); 26189790SLin.Ling@Sun.COM 26196423Sgw25295 /* 26206423Sgw25295 * Add this top-level vdev to the child array. 26216423Sgw25295 */ 26229790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 26239790SLin.Ling@Sun.COM &nvtop) == 0); 26249790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 26259790SLin.Ling@Sun.COM &pgid) == 0); 26269790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 26276423Sgw25295 26286423Sgw25295 /* 26296423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 26306423Sgw25295 */ 26316423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 26329790SLin.Ling@Sun.COM VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 26339790SLin.Ling@Sun.COM VDEV_TYPE_ROOT) == 0); 26346423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 26356423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 26366423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 26376423Sgw25295 &nvtop, 1) == 0); 26386423Sgw25295 26396423Sgw25295 /* 26406423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 26416423Sgw25295 * this pool's configuration (remove the old, add the new). 26426423Sgw25295 */ 26436423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 26446423Sgw25295 nvlist_free(nvroot); 26459790SLin.Ling@Sun.COM return (config); 26466423Sgw25295 } 26476423Sgw25295 26486423Sgw25295 /* 26499790SLin.Ling@Sun.COM * Walk the vdev tree and see if we can find a device with "better" 26509790SLin.Ling@Sun.COM * configuration. A configuration is "better" if the label on that 26519790SLin.Ling@Sun.COM * device has a more recent txg. 26526423Sgw25295 */ 26539790SLin.Ling@Sun.COM static void 26549790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 26557147Staylor { 26569816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 26579790SLin.Ling@Sun.COM spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 26589790SLin.Ling@Sun.COM 26599790SLin.Ling@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 26609790SLin.Ling@Sun.COM nvlist_t *label; 26619790SLin.Ling@Sun.COM uint64_t label_txg; 26629790SLin.Ling@Sun.COM 26639790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 26649790SLin.Ling@Sun.COM &label) != 0) 26659790SLin.Ling@Sun.COM return; 26669790SLin.Ling@Sun.COM 26679790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 26689790SLin.Ling@Sun.COM &label_txg) == 0); 26699790SLin.Ling@Sun.COM 26709790SLin.Ling@Sun.COM /* 26719790SLin.Ling@Sun.COM * Do we have a better boot device? 26729790SLin.Ling@Sun.COM */ 26739790SLin.Ling@Sun.COM if (label_txg > *txg) { 26749790SLin.Ling@Sun.COM *txg = label_txg; 26759790SLin.Ling@Sun.COM *avd = vd; 26767147Staylor } 26779790SLin.Ling@Sun.COM nvlist_free(label); 26787147Staylor } 26797147Staylor } 26807147Staylor 26816423Sgw25295 /* 26826423Sgw25295 * Import a root pool. 26836423Sgw25295 * 26847147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 26857147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 26867147Staylor * The GRUB "findroot" command will return the vdev we should boot. 26876423Sgw25295 * 26886423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 26896423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 26906423Sgw25295 * e.g. 26916423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 26926423Sgw25295 */ 26936423Sgw25295 int 26947147Staylor spa_import_rootpool(char *devpath, char *devid) 26956423Sgw25295 { 26969790SLin.Ling@Sun.COM spa_t *spa; 26979790SLin.Ling@Sun.COM vdev_t *rvd, *bvd, *avd = NULL; 26989790SLin.Ling@Sun.COM nvlist_t *config, *nvtop; 26999790SLin.Ling@Sun.COM uint64_t guid, txg; 27006423Sgw25295 char *pname; 27016423Sgw25295 int error; 27026423Sgw25295 27036423Sgw25295 /* 27049790SLin.Ling@Sun.COM * Read the label from the boot device and generate a configuration. 27056423Sgw25295 */ 270610822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 270710822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL) 270810822SJack.Meng@Sun.COM if (config == NULL) { 270910822SJack.Meng@Sun.COM if (strstr(devpath, "/iscsi/ssd") != NULL) { 271010822SJack.Meng@Sun.COM /* iscsi boot */ 271110822SJack.Meng@Sun.COM get_iscsi_bootpath_phy(devpath); 271210822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 271310822SJack.Meng@Sun.COM } 271410822SJack.Meng@Sun.COM } 271510822SJack.Meng@Sun.COM #endif 271610822SJack.Meng@Sun.COM if (config == NULL) { 27179790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 27189790SLin.Ling@Sun.COM devpath); 27199790SLin.Ling@Sun.COM return (EIO); 27209790SLin.Ling@Sun.COM } 27219790SLin.Ling@Sun.COM 27229790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 27239790SLin.Ling@Sun.COM &pname) == 0); 27249790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 27256423Sgw25295 27269425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 27279425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pname)) != NULL) { 27289425SEric.Schrock@Sun.COM /* 27299425SEric.Schrock@Sun.COM * Remove the existing root pool from the namespace so that we 27309425SEric.Schrock@Sun.COM * can replace it with the correct config we just read in. 27319425SEric.Schrock@Sun.COM */ 27329425SEric.Schrock@Sun.COM spa_remove(spa); 27339425SEric.Schrock@Sun.COM } 27349425SEric.Schrock@Sun.COM 273510921STim.Haley@Sun.COM spa = spa_add(pname, config, NULL); 27369425SEric.Schrock@Sun.COM spa->spa_is_root = B_TRUE; 273710100SLin.Ling@Sun.COM spa->spa_load_verbatim = B_TRUE; 27389790SLin.Ling@Sun.COM 27399790SLin.Ling@Sun.COM /* 27409790SLin.Ling@Sun.COM * Build up a vdev tree based on the boot device's label config. 27419790SLin.Ling@Sun.COM */ 27429790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 27439790SLin.Ling@Sun.COM &nvtop) == 0); 27449790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27459790SLin.Ling@Sun.COM error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 27469790SLin.Ling@Sun.COM VDEV_ALLOC_ROOTPOOL); 27479790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 27489790SLin.Ling@Sun.COM if (error) { 27499790SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 27509790SLin.Ling@Sun.COM nvlist_free(config); 27519790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 27529790SLin.Ling@Sun.COM pname); 27539790SLin.Ling@Sun.COM return (error); 27549790SLin.Ling@Sun.COM } 27559790SLin.Ling@Sun.COM 27569790SLin.Ling@Sun.COM /* 27579790SLin.Ling@Sun.COM * Get the boot vdev. 27589790SLin.Ling@Sun.COM */ 27599790SLin.Ling@Sun.COM if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 27609790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 27619790SLin.Ling@Sun.COM (u_longlong_t)guid); 27629790SLin.Ling@Sun.COM error = ENOENT; 27639790SLin.Ling@Sun.COM goto out; 27649790SLin.Ling@Sun.COM } 27659790SLin.Ling@Sun.COM 27669790SLin.Ling@Sun.COM /* 27679790SLin.Ling@Sun.COM * Determine if there is a better boot device. 27689790SLin.Ling@Sun.COM */ 27699790SLin.Ling@Sun.COM avd = bvd; 27709790SLin.Ling@Sun.COM spa_alt_rootvdev(rvd, &avd, &txg); 27719790SLin.Ling@Sun.COM if (avd != bvd) { 27729790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 27739790SLin.Ling@Sun.COM "try booting from '%s'", avd->vdev_path); 27749790SLin.Ling@Sun.COM error = EINVAL; 27759790SLin.Ling@Sun.COM goto out; 27769790SLin.Ling@Sun.COM } 27779790SLin.Ling@Sun.COM 27789790SLin.Ling@Sun.COM /* 27799790SLin.Ling@Sun.COM * If the boot device is part of a spare vdev then ensure that 27809790SLin.Ling@Sun.COM * we're booting off the active spare. 27819790SLin.Ling@Sun.COM */ 27829790SLin.Ling@Sun.COM if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 27839790SLin.Ling@Sun.COM !bvd->vdev_isspare) { 27849790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is currently spared. Please " 27859790SLin.Ling@Sun.COM "try booting from '%s'", 27869790SLin.Ling@Sun.COM bvd->vdev_parent->vdev_child[1]->vdev_path); 27879790SLin.Ling@Sun.COM error = EINVAL; 27889790SLin.Ling@Sun.COM goto out; 27899790SLin.Ling@Sun.COM } 27909790SLin.Ling@Sun.COM 27919790SLin.Ling@Sun.COM error = 0; 27929946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 27939790SLin.Ling@Sun.COM out: 27949790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27959790SLin.Ling@Sun.COM vdev_free(rvd); 27969790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 27979425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 27986423Sgw25295 27999790SLin.Ling@Sun.COM nvlist_free(config); 28006423Sgw25295 return (error); 28016423Sgw25295 } 28029790SLin.Ling@Sun.COM 28036423Sgw25295 #endif 28046423Sgw25295 28056423Sgw25295 /* 28069425SEric.Schrock@Sun.COM * Take a pool and insert it into the namespace as if it had been loaded at 28079425SEric.Schrock@Sun.COM * boot. 28089425SEric.Schrock@Sun.COM */ 28099425SEric.Schrock@Sun.COM int 28109425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props) 28119425SEric.Schrock@Sun.COM { 28129425SEric.Schrock@Sun.COM spa_t *spa; 281310921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 28149425SEric.Schrock@Sun.COM char *altroot = NULL; 28159425SEric.Schrock@Sun.COM 28169425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 28179425SEric.Schrock@Sun.COM if (spa_lookup(pool) != NULL) { 28189425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28199425SEric.Schrock@Sun.COM return (EEXIST); 28209425SEric.Schrock@Sun.COM } 28219425SEric.Schrock@Sun.COM 28229425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 28239425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 282410921STim.Haley@Sun.COM spa = spa_add(pool, config, altroot); 282510921STim.Haley@Sun.COM 282610921STim.Haley@Sun.COM zpool_get_rewind_policy(config, &policy); 282710921STim.Haley@Sun.COM spa->spa_load_max_txg = policy.zrp_txg; 28289425SEric.Schrock@Sun.COM 282910100SLin.Ling@Sun.COM spa->spa_load_verbatim = B_TRUE; 283010000SVictor.Latushkin@Sun.COM 28319425SEric.Schrock@Sun.COM if (props != NULL) 28329425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 28339425SEric.Schrock@Sun.COM 28349425SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 28359425SEric.Schrock@Sun.COM 28369425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28379946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 28389425SEric.Schrock@Sun.COM 28399425SEric.Schrock@Sun.COM return (0); 28409425SEric.Schrock@Sun.COM } 28419425SEric.Schrock@Sun.COM 28429425SEric.Schrock@Sun.COM /* 28436423Sgw25295 * Import a non-root pool into the system. 28446423Sgw25295 */ 28456423Sgw25295 int 28466423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 28476423Sgw25295 { 28489425SEric.Schrock@Sun.COM spa_t *spa; 28499425SEric.Schrock@Sun.COM char *altroot = NULL; 285010921STim.Haley@Sun.COM spa_load_state_t state = SPA_LOAD_IMPORT; 285110921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 28529425SEric.Schrock@Sun.COM int error; 28539425SEric.Schrock@Sun.COM nvlist_t *nvroot; 28549425SEric.Schrock@Sun.COM nvlist_t **spares, **l2cache; 28559425SEric.Schrock@Sun.COM uint_t nspares, nl2cache; 28569425SEric.Schrock@Sun.COM 28579425SEric.Schrock@Sun.COM /* 28589425SEric.Schrock@Sun.COM * If a pool with this name exists, return failure. 28599425SEric.Schrock@Sun.COM */ 28609425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 28619425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pool)) != NULL) { 28629425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28639425SEric.Schrock@Sun.COM return (EEXIST); 28649425SEric.Schrock@Sun.COM } 28659425SEric.Schrock@Sun.COM 286610921STim.Haley@Sun.COM zpool_get_rewind_policy(config, &policy); 286710921STim.Haley@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 286810921STim.Haley@Sun.COM state = SPA_LOAD_RECOVER; 286910921STim.Haley@Sun.COM 28709425SEric.Schrock@Sun.COM /* 28719425SEric.Schrock@Sun.COM * Create and initialize the spa structure. 28729425SEric.Schrock@Sun.COM */ 28739425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 28749425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 287510921STim.Haley@Sun.COM spa = spa_add(pool, config, altroot); 28769425SEric.Schrock@Sun.COM spa_activate(spa, spa_mode_global); 28779425SEric.Schrock@Sun.COM 28789425SEric.Schrock@Sun.COM /* 28799630SJeff.Bonwick@Sun.COM * Don't start async tasks until we know everything is healthy. 28809630SJeff.Bonwick@Sun.COM */ 28819630SJeff.Bonwick@Sun.COM spa_async_suspend(spa); 28829630SJeff.Bonwick@Sun.COM 28839630SJeff.Bonwick@Sun.COM /* 28849425SEric.Schrock@Sun.COM * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 28859425SEric.Schrock@Sun.COM * because the user-supplied config is actually the one to trust when 28869425SEric.Schrock@Sun.COM * doing an import. 28879425SEric.Schrock@Sun.COM */ 288810921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 288910921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 289010921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 289110921STim.Haley@Sun.COM ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0)); 289210921STim.Haley@Sun.COM 289310921STim.Haley@Sun.COM /* 289410921STim.Haley@Sun.COM * Propagate anything learned about failing or best txgs 289510921STim.Haley@Sun.COM * back to caller 289610921STim.Haley@Sun.COM */ 289710921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa, config); 28989425SEric.Schrock@Sun.COM 28999425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29009425SEric.Schrock@Sun.COM /* 29019425SEric.Schrock@Sun.COM * Toss any existing sparelist, as it doesn't have any validity 29029425SEric.Schrock@Sun.COM * anymore, and conflicts with spa_has_spare(). 29039425SEric.Schrock@Sun.COM */ 29049425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) { 29059425SEric.Schrock@Sun.COM nvlist_free(spa->spa_spares.sav_config); 29069425SEric.Schrock@Sun.COM spa->spa_spares.sav_config = NULL; 29079425SEric.Schrock@Sun.COM spa_load_spares(spa); 29089425SEric.Schrock@Sun.COM } 29099425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) { 29109425SEric.Schrock@Sun.COM nvlist_free(spa->spa_l2cache.sav_config); 29119425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_config = NULL; 29129425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 29139425SEric.Schrock@Sun.COM } 29149425SEric.Schrock@Sun.COM 29159425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 29169425SEric.Schrock@Sun.COM &nvroot) == 0); 29179425SEric.Schrock@Sun.COM if (error == 0) 29189425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 29199425SEric.Schrock@Sun.COM VDEV_ALLOC_SPARE); 29209425SEric.Schrock@Sun.COM if (error == 0) 29219425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 29229425SEric.Schrock@Sun.COM VDEV_ALLOC_L2CACHE); 29239425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29249425SEric.Schrock@Sun.COM 29259425SEric.Schrock@Sun.COM if (props != NULL) 29269425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 29279425SEric.Schrock@Sun.COM 29289425SEric.Schrock@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 29299425SEric.Schrock@Sun.COM (error = spa_prop_set(spa, props)))) { 29309425SEric.Schrock@Sun.COM spa_unload(spa); 29319425SEric.Schrock@Sun.COM spa_deactivate(spa); 29329425SEric.Schrock@Sun.COM spa_remove(spa); 29339425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 29349425SEric.Schrock@Sun.COM return (error); 29359425SEric.Schrock@Sun.COM } 29369425SEric.Schrock@Sun.COM 29379630SJeff.Bonwick@Sun.COM spa_async_resume(spa); 29389630SJeff.Bonwick@Sun.COM 29399425SEric.Schrock@Sun.COM /* 29409425SEric.Schrock@Sun.COM * Override any spares and level 2 cache devices as specified by 29419425SEric.Schrock@Sun.COM * the user, as these may have correct device names/devids, etc. 29429425SEric.Schrock@Sun.COM */ 29439425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29449425SEric.Schrock@Sun.COM &spares, &nspares) == 0) { 29459425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) 29469425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_spares.sav_config, 29479425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 29489425SEric.Schrock@Sun.COM else 29499425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 29509425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 29519425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 29529425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 29539425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29549425SEric.Schrock@Sun.COM spa_load_spares(spa); 29559425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29569425SEric.Schrock@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 29579425SEric.Schrock@Sun.COM } 29589425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 29599425SEric.Schrock@Sun.COM &l2cache, &nl2cache) == 0) { 29609425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) 29619425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 29629425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 29639425SEric.Schrock@Sun.COM else 29649425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 29659425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 29669425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 29679425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 29689425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29699425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 29709425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29719425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_sync = B_TRUE; 29729425SEric.Schrock@Sun.COM } 29739425SEric.Schrock@Sun.COM 297410672SEric.Schrock@Sun.COM /* 297510672SEric.Schrock@Sun.COM * Check for any removed devices. 297610672SEric.Schrock@Sun.COM */ 297710672SEric.Schrock@Sun.COM if (spa->spa_autoreplace) { 297810672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 297910672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 298010672SEric.Schrock@Sun.COM } 298110672SEric.Schrock@Sun.COM 29829425SEric.Schrock@Sun.COM if (spa_writeable(spa)) { 29839425SEric.Schrock@Sun.COM /* 29849425SEric.Schrock@Sun.COM * Update the config cache to include the newly-imported pool. 29859425SEric.Schrock@Sun.COM */ 298610100SLin.Ling@Sun.COM spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 29879425SEric.Schrock@Sun.COM } 29889425SEric.Schrock@Sun.COM 29899816SGeorge.Wilson@Sun.COM /* 29909816SGeorge.Wilson@Sun.COM * It's possible that the pool was expanded while it was exported. 29919816SGeorge.Wilson@Sun.COM * We kick off an async task to handle this for us. 29929816SGeorge.Wilson@Sun.COM */ 29939816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 29949816SGeorge.Wilson@Sun.COM 29959425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 29969946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 29979425SEric.Schrock@Sun.COM 29989425SEric.Schrock@Sun.COM return (0); 29996643Seschrock } 30006643Seschrock 30016643Seschrock 3002789Sahrens /* 3003789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 3004789Sahrens * to get the vdev stats associated with the imported devices. 3005789Sahrens */ 3006789Sahrens #define TRYIMPORT_NAME "$import" 3007789Sahrens 3008789Sahrens nvlist_t * 3009789Sahrens spa_tryimport(nvlist_t *tryconfig) 3010789Sahrens { 3011789Sahrens nvlist_t *config = NULL; 3012789Sahrens char *poolname; 3013789Sahrens spa_t *spa; 3014789Sahrens uint64_t state; 30158680SLin.Ling@Sun.COM int error; 3016789Sahrens 3017789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3018789Sahrens return (NULL); 3019789Sahrens 3020789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3021789Sahrens return (NULL); 3022789Sahrens 30231635Sbonwick /* 30241635Sbonwick * Create and initialize the spa structure. 30251635Sbonwick */ 3026789Sahrens mutex_enter(&spa_namespace_lock); 302710921STim.Haley@Sun.COM spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 30288241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 3029789Sahrens 3030789Sahrens /* 30311635Sbonwick * Pass off the heavy lifting to spa_load(). 30321732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 30331732Sbonwick * is actually the one to trust when doing an import. 3034789Sahrens */ 303510921STim.Haley@Sun.COM error = spa_load(spa, SPA_LOAD_TRYIMPORT, B_TRUE); 3036789Sahrens 3037789Sahrens /* 3038789Sahrens * If 'tryconfig' was at least parsable, return the current config. 3039789Sahrens */ 3040789Sahrens if (spa->spa_root_vdev != NULL) { 3041789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3042789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3043789Sahrens poolname) == 0); 3044789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3045789Sahrens state) == 0); 30463975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 30473975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 30482082Seschrock 30492082Seschrock /* 30506423Sgw25295 * If the bootfs property exists on this pool then we 30516423Sgw25295 * copy it out so that external consumers can tell which 30526423Sgw25295 * pools are bootable. 30536423Sgw25295 */ 30548680SLin.Ling@Sun.COM if ((!error || error == EEXIST) && spa->spa_bootfs) { 30556423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 30566423Sgw25295 30576423Sgw25295 /* 30586423Sgw25295 * We have to play games with the name since the 30596423Sgw25295 * pool was opened as TRYIMPORT_NAME. 30606423Sgw25295 */ 30617754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 30626423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 30636423Sgw25295 char *cp; 30646423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 30656423Sgw25295 30666423Sgw25295 cp = strchr(tmpname, '/'); 30676423Sgw25295 if (cp == NULL) { 30686423Sgw25295 (void) strlcpy(dsname, tmpname, 30696423Sgw25295 MAXPATHLEN); 30706423Sgw25295 } else { 30716423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 30726423Sgw25295 "%s/%s", poolname, ++cp); 30736423Sgw25295 } 30746423Sgw25295 VERIFY(nvlist_add_string(config, 30756423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 30766423Sgw25295 kmem_free(dsname, MAXPATHLEN); 30776423Sgw25295 } 30786423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 30796423Sgw25295 } 30806423Sgw25295 30816423Sgw25295 /* 30825450Sbrendan * Add the list of hot spares and level 2 cache devices. 30832082Seschrock */ 30849425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 30852082Seschrock spa_add_spares(spa, config); 30865450Sbrendan spa_add_l2cache(spa, config); 30879425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 3088789Sahrens } 3089789Sahrens 3090789Sahrens spa_unload(spa); 3091789Sahrens spa_deactivate(spa); 3092789Sahrens spa_remove(spa); 3093789Sahrens mutex_exit(&spa_namespace_lock); 3094789Sahrens 3095789Sahrens return (config); 3096789Sahrens } 3097789Sahrens 3098789Sahrens /* 3099789Sahrens * Pool export/destroy 3100789Sahrens * 3101789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 3102789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 3103789Sahrens * update the pool state and sync all the labels to disk, removing the 31048211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 31058211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 3106789Sahrens */ 3107789Sahrens static int 31087214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 31098211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 3110789Sahrens { 3111789Sahrens spa_t *spa; 3112789Sahrens 31131775Sbillm if (oldconfig) 31141775Sbillm *oldconfig = NULL; 31151775Sbillm 31168241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 3117789Sahrens return (EROFS); 3118789Sahrens 3119789Sahrens mutex_enter(&spa_namespace_lock); 3120789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3121789Sahrens mutex_exit(&spa_namespace_lock); 3122789Sahrens return (ENOENT); 3123789Sahrens } 3124789Sahrens 3125789Sahrens /* 31261544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 31271544Seschrock * reacquire the namespace lock, and see if we can export. 31281544Seschrock */ 31291544Seschrock spa_open_ref(spa, FTAG); 31301544Seschrock mutex_exit(&spa_namespace_lock); 31311544Seschrock spa_async_suspend(spa); 31321544Seschrock mutex_enter(&spa_namespace_lock); 31331544Seschrock spa_close(spa, FTAG); 31341544Seschrock 31351544Seschrock /* 3136789Sahrens * The pool will be in core if it's openable, 3137789Sahrens * in which case we can modify its state. 3138789Sahrens */ 3139789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 3140789Sahrens /* 3141789Sahrens * Objsets may be open only because they're dirty, so we 3142789Sahrens * have to force it to sync before checking spa_refcnt. 3143789Sahrens */ 3144789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 3145789Sahrens 31461544Seschrock /* 31471544Seschrock * A pool cannot be exported or destroyed if there are active 31481544Seschrock * references. If we are resetting a pool, allow references by 31491544Seschrock * fault injection handlers. 31501544Seschrock */ 31511544Seschrock if (!spa_refcount_zero(spa) || 31521544Seschrock (spa->spa_inject_ref != 0 && 31531544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 31541544Seschrock spa_async_resume(spa); 3155789Sahrens mutex_exit(&spa_namespace_lock); 3156789Sahrens return (EBUSY); 3157789Sahrens } 3158789Sahrens 3159789Sahrens /* 31607214Slling * A pool cannot be exported if it has an active shared spare. 31617214Slling * This is to prevent other pools stealing the active spare 31627214Slling * from an exported pool. At user's own will, such pool can 31637214Slling * be forcedly exported. 31647214Slling */ 31657214Slling if (!force && new_state == POOL_STATE_EXPORTED && 31667214Slling spa_has_active_shared_spare(spa)) { 31677214Slling spa_async_resume(spa); 31687214Slling mutex_exit(&spa_namespace_lock); 31697214Slling return (EXDEV); 31707214Slling } 31717214Slling 31727214Slling /* 3173789Sahrens * We want this to be reflected on every label, 3174789Sahrens * so mark them all dirty. spa_unload() will do the 3175789Sahrens * final sync that pushes these changes out. 3176789Sahrens */ 31778211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 31787754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 31791544Seschrock spa->spa_state = new_state; 31801635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 31811544Seschrock vdev_config_dirty(spa->spa_root_vdev); 31827754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 31831544Seschrock } 3184789Sahrens } 3185789Sahrens 31864451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 31874451Seschrock 3188789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3189789Sahrens spa_unload(spa); 3190789Sahrens spa_deactivate(spa); 3191789Sahrens } 3192789Sahrens 31931775Sbillm if (oldconfig && spa->spa_config) 31941775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 31951775Sbillm 31961544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 31978211SGeorge.Wilson@Sun.COM if (!hardforce) 31988211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 31991544Seschrock spa_remove(spa); 32001544Seschrock } 3201789Sahrens mutex_exit(&spa_namespace_lock); 3202789Sahrens 3203789Sahrens return (0); 3204789Sahrens } 3205789Sahrens 3206789Sahrens /* 3207789Sahrens * Destroy a storage pool. 3208789Sahrens */ 3209789Sahrens int 3210789Sahrens spa_destroy(char *pool) 3211789Sahrens { 32128211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 32138211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 3214789Sahrens } 3215789Sahrens 3216789Sahrens /* 3217789Sahrens * Export a storage pool. 3218789Sahrens */ 3219789Sahrens int 32208211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 32218211SGeorge.Wilson@Sun.COM boolean_t hardforce) 3222789Sahrens { 32238211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 32248211SGeorge.Wilson@Sun.COM force, hardforce)); 3225789Sahrens } 3226789Sahrens 3227789Sahrens /* 32281544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 32291544Seschrock * from the namespace in any way. 32301544Seschrock */ 32311544Seschrock int 32321544Seschrock spa_reset(char *pool) 32331544Seschrock { 32347214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 32358211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 32361544Seschrock } 32371544Seschrock 32381544Seschrock /* 3239789Sahrens * ========================================================================== 3240789Sahrens * Device manipulation 3241789Sahrens * ========================================================================== 3242789Sahrens */ 3243789Sahrens 3244789Sahrens /* 32454527Sperrin * Add a device to a storage pool. 3246789Sahrens */ 3247789Sahrens int 3248789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 3249789Sahrens { 325010594SGeorge.Wilson@Sun.COM uint64_t txg, id; 32518241SJeff.Bonwick@Sun.COM int error; 3252789Sahrens vdev_t *rvd = spa->spa_root_vdev; 32531585Sbonwick vdev_t *vd, *tvd; 32545450Sbrendan nvlist_t **spares, **l2cache; 32555450Sbrendan uint_t nspares, nl2cache; 3256789Sahrens 3257789Sahrens txg = spa_vdev_enter(spa); 3258789Sahrens 32592082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 32602082Seschrock VDEV_ALLOC_ADD)) != 0) 32612082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 32622082Seschrock 32637754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 3264789Sahrens 32655450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 32665450Sbrendan &nspares) != 0) 32672082Seschrock nspares = 0; 32682082Seschrock 32695450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 32705450Sbrendan &nl2cache) != 0) 32715450Sbrendan nl2cache = 0; 32725450Sbrendan 32737754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 32742082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 32757754SJeff.Bonwick@Sun.COM 32767754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 32777754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 32787754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 32792082Seschrock 32803377Seschrock /* 32815450Sbrendan * We must validate the spares and l2cache devices after checking the 32825450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 32833377Seschrock */ 32847754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 32853377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 32863377Seschrock 32873377Seschrock /* 32883377Seschrock * Transfer each new top-level vdev from vd to rvd. 32893377Seschrock */ 32908241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 329110594SGeorge.Wilson@Sun.COM 329210594SGeorge.Wilson@Sun.COM /* 329310594SGeorge.Wilson@Sun.COM * Set the vdev id to the first hole, if one exists. 329410594SGeorge.Wilson@Sun.COM */ 329510594SGeorge.Wilson@Sun.COM for (id = 0; id < rvd->vdev_children; id++) { 329610594SGeorge.Wilson@Sun.COM if (rvd->vdev_child[id]->vdev_ishole) { 329710594SGeorge.Wilson@Sun.COM vdev_free(rvd->vdev_child[id]); 329810594SGeorge.Wilson@Sun.COM break; 329910594SGeorge.Wilson@Sun.COM } 330010594SGeorge.Wilson@Sun.COM } 33013377Seschrock tvd = vd->vdev_child[c]; 33023377Seschrock vdev_remove_child(vd, tvd); 330310594SGeorge.Wilson@Sun.COM tvd->vdev_id = id; 33043377Seschrock vdev_add_child(rvd, tvd); 33053377Seschrock vdev_config_dirty(tvd); 33063377Seschrock } 33073377Seschrock 33082082Seschrock if (nspares != 0) { 33095450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 33105450Sbrendan ZPOOL_CONFIG_SPARES); 33112082Seschrock spa_load_spares(spa); 33125450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 33135450Sbrendan } 33145450Sbrendan 33155450Sbrendan if (nl2cache != 0) { 33165450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 33175450Sbrendan ZPOOL_CONFIG_L2CACHE); 33185450Sbrendan spa_load_l2cache(spa); 33195450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3320789Sahrens } 3321789Sahrens 3322789Sahrens /* 33231585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 33241585Sbonwick * If other threads start allocating from these vdevs before we 33251585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 33261585Sbonwick * fail to open the pool because there are DVAs that the config cache 33271585Sbonwick * can't translate. Therefore, we first add the vdevs without 33281585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 33291635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 33301585Sbonwick * 33311585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 33321585Sbonwick * if we lose power at any point in this sequence, the remaining 33331585Sbonwick * steps will be completed the next time we load the pool. 3334789Sahrens */ 33351635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 33361585Sbonwick 33371635Sbonwick mutex_enter(&spa_namespace_lock); 33381635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 33391635Sbonwick mutex_exit(&spa_namespace_lock); 3340789Sahrens 33411635Sbonwick return (0); 3342789Sahrens } 3343789Sahrens 3344789Sahrens /* 3345789Sahrens * Attach a device to a mirror. The arguments are the path to any device 3346789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 3347789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 3348789Sahrens * 3349789Sahrens * If 'replacing' is specified, the new device is intended to replace the 3350789Sahrens * existing device; in this case the two devices are made into their own 33514451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 3352789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 3353789Sahrens * extra rules: you can't attach to it after it's been created, and upon 3354789Sahrens * completion of resilvering, the first disk (the one being replaced) 3355789Sahrens * is automatically detached. 3356789Sahrens */ 3357789Sahrens int 33581544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3359789Sahrens { 3360789Sahrens uint64_t txg, open_txg; 3361789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3362789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 33632082Seschrock vdev_ops_t *pvops; 33647313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 33657313SEric.Kustarz@Sun.COM int newvd_isspare; 33667313SEric.Kustarz@Sun.COM int error; 3367789Sahrens 3368789Sahrens txg = spa_vdev_enter(spa); 3369789Sahrens 33706643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3371789Sahrens 3372789Sahrens if (oldvd == NULL) 3373789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3374789Sahrens 33751585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 33761585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 33771585Sbonwick 3378789Sahrens pvd = oldvd->vdev_parent; 3379789Sahrens 33802082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 33814451Seschrock VDEV_ALLOC_ADD)) != 0) 33824451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 33834451Seschrock 33844451Seschrock if (newrootvd->vdev_children != 1) 3385789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3386789Sahrens 3387789Sahrens newvd = newrootvd->vdev_child[0]; 3388789Sahrens 3389789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3390789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3391789Sahrens 33922082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3393789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3394789Sahrens 33954527Sperrin /* 33964527Sperrin * Spares can't replace logs 33974527Sperrin */ 33987326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 33994527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34004527Sperrin 34012082Seschrock if (!replacing) { 34022082Seschrock /* 34032082Seschrock * For attach, the only allowable parent is a mirror or the root 34042082Seschrock * vdev. 34052082Seschrock */ 34062082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 34072082Seschrock pvd->vdev_ops != &vdev_root_ops) 34082082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34092082Seschrock 34102082Seschrock pvops = &vdev_mirror_ops; 34112082Seschrock } else { 34122082Seschrock /* 34132082Seschrock * Active hot spares can only be replaced by inactive hot 34142082Seschrock * spares. 34152082Seschrock */ 34162082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 34172082Seschrock pvd->vdev_child[1] == oldvd && 34182082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 34192082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34202082Seschrock 34212082Seschrock /* 34222082Seschrock * If the source is a hot spare, and the parent isn't already a 34232082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 34243377Seschrock * want to create a replacing vdev. The user is not allowed to 34253377Seschrock * attach to a spared vdev child unless the 'isspare' state is 34263377Seschrock * the same (spare replaces spare, non-spare replaces 34273377Seschrock * non-spare). 34282082Seschrock */ 34292082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 34302082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34313377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 34323377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 34333377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34342082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 34352082Seschrock newvd->vdev_isspare) 34362082Seschrock pvops = &vdev_spare_ops; 34372082Seschrock else 34382082Seschrock pvops = &vdev_replacing_ops; 34392082Seschrock } 34402082Seschrock 34411175Slling /* 34429816SGeorge.Wilson@Sun.COM * Make sure the new device is big enough. 34431175Slling */ 34449816SGeorge.Wilson@Sun.COM if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3445789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3446789Sahrens 34471732Sbonwick /* 34481732Sbonwick * The new device cannot have a higher alignment requirement 34491732Sbonwick * than the top-level vdev. 34501732Sbonwick */ 34511732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3452789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3453789Sahrens 3454789Sahrens /* 3455789Sahrens * If this is an in-place replacement, update oldvd's path and devid 3456789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3457789Sahrens */ 3458789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3459789Sahrens spa_strfree(oldvd->vdev_path); 3460789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3461789Sahrens KM_SLEEP); 3462789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3463789Sahrens newvd->vdev_path, "old"); 3464789Sahrens if (oldvd->vdev_devid != NULL) { 3465789Sahrens spa_strfree(oldvd->vdev_devid); 3466789Sahrens oldvd->vdev_devid = NULL; 3467789Sahrens } 3468789Sahrens } 3469789Sahrens 3470789Sahrens /* 34712082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 34722082Seschrock * mirror/replacing/spare vdev above oldvd. 3473789Sahrens */ 3474789Sahrens if (pvd->vdev_ops != pvops) 3475789Sahrens pvd = vdev_add_parent(oldvd, pvops); 3476789Sahrens 3477789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3478789Sahrens ASSERT(pvd->vdev_ops == pvops); 3479789Sahrens ASSERT(oldvd->vdev_parent == pvd); 3480789Sahrens 3481789Sahrens /* 3482789Sahrens * Extract the new device from its root and add it to pvd. 3483789Sahrens */ 3484789Sahrens vdev_remove_child(newrootvd, newvd); 3485789Sahrens newvd->vdev_id = pvd->vdev_children; 348610594SGeorge.Wilson@Sun.COM newvd->vdev_crtxg = oldvd->vdev_crtxg; 3487789Sahrens vdev_add_child(pvd, newvd); 3488789Sahrens 3489789Sahrens tvd = newvd->vdev_top; 3490789Sahrens ASSERT(pvd->vdev_top == tvd); 3491789Sahrens ASSERT(tvd->vdev_parent == rvd); 3492789Sahrens 3493789Sahrens vdev_config_dirty(tvd); 3494789Sahrens 3495789Sahrens /* 3496789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3497789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3498789Sahrens */ 3499789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 3500789Sahrens 35018241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, 35028241SJeff.Bonwick@Sun.COM TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3503789Sahrens 35049425SEric.Schrock@Sun.COM if (newvd->vdev_isspare) { 35053377Seschrock spa_spare_activate(newvd); 35069425SEric.Schrock@Sun.COM spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 35079425SEric.Schrock@Sun.COM } 35089425SEric.Schrock@Sun.COM 35097754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 35107754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 35117313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 35121544Seschrock 3513789Sahrens /* 3514789Sahrens * Mark newvd's DTL dirty in this txg. 3515789Sahrens */ 35161732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3517789Sahrens 3518789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3519789Sahrens 35209946SMark.Musante@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL, 35219946SMark.Musante@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 35229946SMark.Musante@Sun.COM replacing && newvd_isspare ? "spare in" : 35239946SMark.Musante@Sun.COM replacing ? "replace" : "attach", newvdpath, 35249946SMark.Musante@Sun.COM replacing ? "for" : "to", oldvdpath); 35257313SEric.Kustarz@Sun.COM 35267313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 35277313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 35287313SEric.Kustarz@Sun.COM 3529789Sahrens /* 35307046Sahrens * Kick off a resilver to update newvd. 3531789Sahrens */ 35327046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3533789Sahrens 3534789Sahrens return (0); 3535789Sahrens } 3536789Sahrens 3537789Sahrens /* 3538789Sahrens * Detach a device from a mirror or replacing vdev. 3539789Sahrens * If 'replace_done' is specified, only detach if the parent 3540789Sahrens * is a replacing vdev. 3541789Sahrens */ 3542789Sahrens int 35438241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3544789Sahrens { 3545789Sahrens uint64_t txg; 35468241SJeff.Bonwick@Sun.COM int error; 3547789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3548789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 35492082Seschrock boolean_t unspare = B_FALSE; 35502082Seschrock uint64_t unspare_guid; 35516673Seschrock size_t len; 3552789Sahrens 3553789Sahrens txg = spa_vdev_enter(spa); 3554789Sahrens 35556643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3556789Sahrens 3557789Sahrens if (vd == NULL) 3558789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3559789Sahrens 35601585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 35611585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35621585Sbonwick 3563789Sahrens pvd = vd->vdev_parent; 3564789Sahrens 3565789Sahrens /* 35668241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 35678241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 35688241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 35698241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 35708241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 35718241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 35728241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 35738241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 35748241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 35758241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 35768241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 35778241SJeff.Bonwick@Sun.COM */ 35788241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 35798241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 35808241SJeff.Bonwick@Sun.COM 35818241SJeff.Bonwick@Sun.COM /* 3582789Sahrens * If replace_done is specified, only remove this device if it's 35832082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 35842082Seschrock * disk can be removed. 3585789Sahrens */ 35862082Seschrock if (replace_done) { 35872082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 35882082Seschrock if (vd->vdev_id != 0) 35892082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35902082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 35912082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35922082Seschrock } 35932082Seschrock } 35942082Seschrock 35952082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 35964577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3597789Sahrens 3598789Sahrens /* 35992082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3600789Sahrens */ 3601789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 36022082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 36032082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3604789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3605789Sahrens 3606789Sahrens /* 36078241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 36088241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 3609789Sahrens */ 36108241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 3611789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3612789Sahrens 36138241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 36148241SJeff.Bonwick@Sun.COM 3615789Sahrens /* 36166673Seschrock * If we are detaching the second disk from a replacing vdev, then 36176673Seschrock * check to see if we changed the original vdev's path to have "/old" 36186673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 36196673Seschrock */ 36206673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 36216673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 36226673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 36236673Seschrock ASSERT(pvd->vdev_child[1] == vd); 36246673Seschrock cvd = pvd->vdev_child[0]; 36256673Seschrock len = strlen(vd->vdev_path); 36266673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 36276673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 36286673Seschrock spa_strfree(cvd->vdev_path); 36296673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 36306673Seschrock } 36316673Seschrock } 36326673Seschrock 36336673Seschrock /* 36342082Seschrock * If we are detaching the original disk from a spare, then it implies 36352082Seschrock * that the spare should become a real disk, and be removed from the 36362082Seschrock * active spare list for the pool. 36372082Seschrock */ 36382082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 36398241SJeff.Bonwick@Sun.COM vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 36402082Seschrock unspare = B_TRUE; 36412082Seschrock 36422082Seschrock /* 3643789Sahrens * Erase the disk labels so the disk can be used for other things. 3644789Sahrens * This must be done after all other error cases are handled, 3645789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3646789Sahrens * But if we can't do it, don't treat the error as fatal -- 3647789Sahrens * it may be that the unwritability of the disk is the reason 3648789Sahrens * it's being detached! 3649789Sahrens */ 36503377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3651789Sahrens 3652789Sahrens /* 3653789Sahrens * Remove vd from its parent and compact the parent's children. 3654789Sahrens */ 3655789Sahrens vdev_remove_child(pvd, vd); 3656789Sahrens vdev_compact_children(pvd); 3657789Sahrens 3658789Sahrens /* 3659789Sahrens * Remember one of the remaining children so we can get tvd below. 3660789Sahrens */ 3661789Sahrens cvd = pvd->vdev_child[0]; 3662789Sahrens 3663789Sahrens /* 36642082Seschrock * If we need to remove the remaining child from the list of hot spares, 36658241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 36668241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 36678241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 36688241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 36698241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 36708241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 36712082Seschrock */ 36722082Seschrock if (unspare) { 36732082Seschrock ASSERT(cvd->vdev_isspare); 36743377Seschrock spa_spare_remove(cvd); 36752082Seschrock unspare_guid = cvd->vdev_guid; 36768241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 36772082Seschrock } 36782082Seschrock 36792082Seschrock /* 3680789Sahrens * If the parent mirror/replacing vdev only has one child, 3681789Sahrens * the parent is no longer needed. Remove it from the tree. 3682789Sahrens */ 3683789Sahrens if (pvd->vdev_children == 1) 3684789Sahrens vdev_remove_parent(cvd); 3685789Sahrens 3686789Sahrens /* 3687789Sahrens * We don't set tvd until now because the parent we just removed 3688789Sahrens * may have been the previous top-level vdev. 3689789Sahrens */ 3690789Sahrens tvd = cvd->vdev_top; 3691789Sahrens ASSERT(tvd->vdev_parent == rvd); 3692789Sahrens 3693789Sahrens /* 36943377Seschrock * Reevaluate the parent vdev state. 3695789Sahrens */ 36964451Seschrock vdev_propagate_state(cvd); 3697789Sahrens 3698789Sahrens /* 36999816SGeorge.Wilson@Sun.COM * If the 'autoexpand' property is set on the pool then automatically 37009816SGeorge.Wilson@Sun.COM * try to expand the size of the pool. For example if the device we 37019816SGeorge.Wilson@Sun.COM * just detached was smaller than the others, it may be possible to 37029816SGeorge.Wilson@Sun.COM * add metaslabs (i.e. grow the pool). We need to reopen the vdev 37039816SGeorge.Wilson@Sun.COM * first so that we can obtain the updated sizes of the leaf vdevs. 3704789Sahrens */ 37059816SGeorge.Wilson@Sun.COM if (spa->spa_autoexpand) { 37069816SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 37079816SGeorge.Wilson@Sun.COM vdev_expand(tvd, txg); 37089816SGeorge.Wilson@Sun.COM } 3709789Sahrens 3710789Sahrens vdev_config_dirty(tvd); 3711789Sahrens 3712789Sahrens /* 37133377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 37143377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 37153377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 37163377Seschrock * prevent vd from being accessed after it's freed. 3717789Sahrens */ 37188241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 3719789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 37201732Sbonwick vd->vdev_detached = B_TRUE; 37211732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3722789Sahrens 37234451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 37244451Seschrock 37252082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 37262082Seschrock 37272082Seschrock /* 37283377Seschrock * If this was the removal of the original device in a hot spare vdev, 37293377Seschrock * then we want to go through and remove the device from the hot spare 37303377Seschrock * list of every other pool. 37312082Seschrock */ 37322082Seschrock if (unspare) { 37338241SJeff.Bonwick@Sun.COM spa_t *myspa = spa; 37342082Seschrock spa = NULL; 37352082Seschrock mutex_enter(&spa_namespace_lock); 37362082Seschrock while ((spa = spa_next(spa)) != NULL) { 37372082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 37382082Seschrock continue; 37398241SJeff.Bonwick@Sun.COM if (spa == myspa) 37408241SJeff.Bonwick@Sun.COM continue; 37417793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 37427793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 37432082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 37447793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 37457793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 37462082Seschrock } 37472082Seschrock mutex_exit(&spa_namespace_lock); 37482082Seschrock } 37492082Seschrock 37502082Seschrock return (error); 37512082Seschrock } 37522082Seschrock 37537754SJeff.Bonwick@Sun.COM static nvlist_t * 37547754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 37552082Seschrock { 37567754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 37577754SJeff.Bonwick@Sun.COM uint64_t guid; 37587754SJeff.Bonwick@Sun.COM 37597754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 37607754SJeff.Bonwick@Sun.COM &guid) == 0); 37617754SJeff.Bonwick@Sun.COM 37627754SJeff.Bonwick@Sun.COM if (guid == target_guid) 37637754SJeff.Bonwick@Sun.COM return (nvpp[i]); 37642082Seschrock } 37652082Seschrock 37667754SJeff.Bonwick@Sun.COM return (NULL); 37675450Sbrendan } 37685450Sbrendan 37697754SJeff.Bonwick@Sun.COM static void 37707754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 37717754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 37725450Sbrendan { 37737754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 37747754SJeff.Bonwick@Sun.COM 37757754SJeff.Bonwick@Sun.COM if (count > 1) 37767754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 37777754SJeff.Bonwick@Sun.COM 37787754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 37797754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 37807754SJeff.Bonwick@Sun.COM continue; 37817754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 37825450Sbrendan } 37835450Sbrendan 37847754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 37857754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 37867754SJeff.Bonwick@Sun.COM 37877754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 37887754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 37897754SJeff.Bonwick@Sun.COM 37907754SJeff.Bonwick@Sun.COM if (count > 1) 37917754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 37925450Sbrendan } 37935450Sbrendan 37945450Sbrendan /* 379510594SGeorge.Wilson@Sun.COM * Removing a device from the vdev namespace requires several steps 379610594SGeorge.Wilson@Sun.COM * and can take a significant amount of time. As a result we use 379710594SGeorge.Wilson@Sun.COM * the spa_vdev_config_[enter/exit] functions which allow us to 379810594SGeorge.Wilson@Sun.COM * grab and release the spa_config_lock while still holding the namespace 379910594SGeorge.Wilson@Sun.COM * lock. During each step the configuration is synced out. 380010594SGeorge.Wilson@Sun.COM */ 380110594SGeorge.Wilson@Sun.COM 380210594SGeorge.Wilson@Sun.COM /* 380310594SGeorge.Wilson@Sun.COM * Evacuate the device. 380410594SGeorge.Wilson@Sun.COM */ 380510594SGeorge.Wilson@Sun.COM int 380610594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 380710594SGeorge.Wilson@Sun.COM { 380810974SJeff.Bonwick@Sun.COM int error = 0; 380910594SGeorge.Wilson@Sun.COM uint64_t txg; 381010594SGeorge.Wilson@Sun.COM 381110594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 381210594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 381310922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 381410594SGeorge.Wilson@Sun.COM 381510594SGeorge.Wilson@Sun.COM /* 381610594SGeorge.Wilson@Sun.COM * Evacuate the device. We don't hold the config lock as writer 381710594SGeorge.Wilson@Sun.COM * since we need to do I/O but we do keep the 381810594SGeorge.Wilson@Sun.COM * spa_namespace_lock held. Once this completes the device 381910594SGeorge.Wilson@Sun.COM * should no longer have any blocks allocated on it. 382010594SGeorge.Wilson@Sun.COM */ 382110594SGeorge.Wilson@Sun.COM if (vd->vdev_islog) { 382210974SJeff.Bonwick@Sun.COM error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 382310974SJeff.Bonwick@Sun.COM NULL, DS_FIND_CHILDREN); 382410974SJeff.Bonwick@Sun.COM } else { 382510974SJeff.Bonwick@Sun.COM error = ENOTSUP; /* until we have bp rewrite */ 382610594SGeorge.Wilson@Sun.COM } 382710594SGeorge.Wilson@Sun.COM 382810974SJeff.Bonwick@Sun.COM txg_wait_synced(spa_get_dsl(spa), 0); 382910974SJeff.Bonwick@Sun.COM 383010974SJeff.Bonwick@Sun.COM if (error) 383110974SJeff.Bonwick@Sun.COM return (error); 383210974SJeff.Bonwick@Sun.COM 383310594SGeorge.Wilson@Sun.COM /* 383410974SJeff.Bonwick@Sun.COM * The evacuation succeeded. Remove any remaining MOS metadata 383510974SJeff.Bonwick@Sun.COM * associated with this vdev, and wait for these changes to sync. 383610594SGeorge.Wilson@Sun.COM */ 383710594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 383810594SGeorge.Wilson@Sun.COM vd->vdev_removing = B_TRUE; 383910594SGeorge.Wilson@Sun.COM vdev_dirty(vd, 0, NULL, txg); 384010594SGeorge.Wilson@Sun.COM vdev_config_dirty(vd); 384110594SGeorge.Wilson@Sun.COM spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 384210594SGeorge.Wilson@Sun.COM 384310594SGeorge.Wilson@Sun.COM return (0); 384410594SGeorge.Wilson@Sun.COM } 384510594SGeorge.Wilson@Sun.COM 384610594SGeorge.Wilson@Sun.COM /* 384710594SGeorge.Wilson@Sun.COM * Complete the removal by cleaning up the namespace. 384810594SGeorge.Wilson@Sun.COM */ 384910594SGeorge.Wilson@Sun.COM void 385010974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 385110594SGeorge.Wilson@Sun.COM { 385210594SGeorge.Wilson@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 385310594SGeorge.Wilson@Sun.COM uint64_t id = vd->vdev_id; 385410594SGeorge.Wilson@Sun.COM boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 385510594SGeorge.Wilson@Sun.COM 385610594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 385710594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 385810922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 385910594SGeorge.Wilson@Sun.COM 386010594SGeorge.Wilson@Sun.COM (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 386110922SJeff.Bonwick@Sun.COM 386210922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_state_dirty_node)) 386310922SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 386410922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_config_dirty_node)) 386510922SJeff.Bonwick@Sun.COM vdev_config_clean(vd); 386610922SJeff.Bonwick@Sun.COM 386710594SGeorge.Wilson@Sun.COM vdev_free(vd); 386810594SGeorge.Wilson@Sun.COM 386910594SGeorge.Wilson@Sun.COM if (last_vdev) { 387010594SGeorge.Wilson@Sun.COM vdev_compact_children(rvd); 387110594SGeorge.Wilson@Sun.COM } else { 387210594SGeorge.Wilson@Sun.COM vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 387310594SGeorge.Wilson@Sun.COM vdev_add_child(rvd, vd); 387410594SGeorge.Wilson@Sun.COM } 387510594SGeorge.Wilson@Sun.COM vdev_config_dirty(rvd); 387610594SGeorge.Wilson@Sun.COM 387710594SGeorge.Wilson@Sun.COM /* 387810594SGeorge.Wilson@Sun.COM * Reassess the health of our root vdev. 387910594SGeorge.Wilson@Sun.COM */ 388010594SGeorge.Wilson@Sun.COM vdev_reopen(rvd); 388110594SGeorge.Wilson@Sun.COM } 388210594SGeorge.Wilson@Sun.COM 388310594SGeorge.Wilson@Sun.COM /* 38845450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 388510594SGeorge.Wilson@Sun.COM * spares, slogs, and level 2 ARC devices. 38865450Sbrendan */ 38875450Sbrendan int 38885450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 38895450Sbrendan { 38905450Sbrendan vdev_t *vd; 389110974SJeff.Bonwick@Sun.COM metaslab_group_t *mg; 38927754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 389310594SGeorge.Wilson@Sun.COM uint64_t txg = 0; 38945450Sbrendan uint_t nspares, nl2cache; 38955450Sbrendan int error = 0; 38968241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 38978241SJeff.Bonwick@Sun.COM 38988241SJeff.Bonwick@Sun.COM if (!locked) 38998241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 39005450Sbrendan 39016643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 39025450Sbrendan 39035450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 39045450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 39057754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 39067754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 39077754SJeff.Bonwick@Sun.COM /* 39087754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 39097754SJeff.Bonwick@Sun.COM * in this pool. 39107754SJeff.Bonwick@Sun.COM */ 39117754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 39127754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 39137754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 39147754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 39157754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 39167754SJeff.Bonwick@Sun.COM } else { 39177754SJeff.Bonwick@Sun.COM error = EBUSY; 39187754SJeff.Bonwick@Sun.COM } 39197754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 39205450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 39217754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 39227754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 39237754SJeff.Bonwick@Sun.COM /* 39247754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 39257754SJeff.Bonwick@Sun.COM */ 39267754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 39277754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 39285450Sbrendan spa_load_l2cache(spa); 39295450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 393010594SGeorge.Wilson@Sun.COM } else if (vd != NULL && vd->vdev_islog) { 393110594SGeorge.Wilson@Sun.COM ASSERT(!locked); 393210922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 393310594SGeorge.Wilson@Sun.COM 393410594SGeorge.Wilson@Sun.COM /* 393510594SGeorge.Wilson@Sun.COM * XXX - Once we have bp-rewrite this should 393610594SGeorge.Wilson@Sun.COM * become the common case. 393710594SGeorge.Wilson@Sun.COM */ 393810594SGeorge.Wilson@Sun.COM 393910974SJeff.Bonwick@Sun.COM mg = vd->vdev_mg; 394010974SJeff.Bonwick@Sun.COM 394110594SGeorge.Wilson@Sun.COM /* 394210974SJeff.Bonwick@Sun.COM * Stop allocating from this vdev. 394310594SGeorge.Wilson@Sun.COM */ 394410974SJeff.Bonwick@Sun.COM metaslab_group_passivate(mg); 394510594SGeorge.Wilson@Sun.COM 394610922SJeff.Bonwick@Sun.COM /* 394710922SJeff.Bonwick@Sun.COM * Wait for the youngest allocations and frees to sync, 394810922SJeff.Bonwick@Sun.COM * and then wait for the deferral of those frees to finish. 394910922SJeff.Bonwick@Sun.COM */ 395010922SJeff.Bonwick@Sun.COM spa_vdev_config_exit(spa, NULL, 395110922SJeff.Bonwick@Sun.COM txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 395210922SJeff.Bonwick@Sun.COM 395310974SJeff.Bonwick@Sun.COM /* 395410974SJeff.Bonwick@Sun.COM * Attempt to evacuate the vdev. 395510974SJeff.Bonwick@Sun.COM */ 395610974SJeff.Bonwick@Sun.COM error = spa_vdev_remove_evacuate(spa, vd); 395710974SJeff.Bonwick@Sun.COM 395810594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 395910594SGeorge.Wilson@Sun.COM 396010974SJeff.Bonwick@Sun.COM /* 396110974SJeff.Bonwick@Sun.COM * If we couldn't evacuate the vdev, unwind. 396210974SJeff.Bonwick@Sun.COM */ 396310974SJeff.Bonwick@Sun.COM if (error) { 396410974SJeff.Bonwick@Sun.COM metaslab_group_activate(mg); 396510974SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 396610974SJeff.Bonwick@Sun.COM } 396710974SJeff.Bonwick@Sun.COM 396810974SJeff.Bonwick@Sun.COM /* 396910974SJeff.Bonwick@Sun.COM * Clean up the vdev namespace. 397010974SJeff.Bonwick@Sun.COM */ 397110974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa, vd); 397210594SGeorge.Wilson@Sun.COM 39737754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 39747754SJeff.Bonwick@Sun.COM /* 39757754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 39767754SJeff.Bonwick@Sun.COM */ 39777754SJeff.Bonwick@Sun.COM error = ENOTSUP; 39787754SJeff.Bonwick@Sun.COM } else { 39797754SJeff.Bonwick@Sun.COM /* 39807754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 39817754SJeff.Bonwick@Sun.COM */ 39827754SJeff.Bonwick@Sun.COM error = ENOENT; 39835450Sbrendan } 39842082Seschrock 39858241SJeff.Bonwick@Sun.COM if (!locked) 39868241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 39878241SJeff.Bonwick@Sun.COM 39888241SJeff.Bonwick@Sun.COM return (error); 3989789Sahrens } 3990789Sahrens 3991789Sahrens /* 39924451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 39934451Seschrock * current spared, so we can detach it. 3994789Sahrens */ 39951544Seschrock static vdev_t * 39964451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3997789Sahrens { 39981544Seschrock vdev_t *newvd, *oldvd; 39999816SGeorge.Wilson@Sun.COM 40009816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 40014451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 40021544Seschrock if (oldvd != NULL) 40031544Seschrock return (oldvd); 40041544Seschrock } 4005789Sahrens 40064451Seschrock /* 40074451Seschrock * Check for a completed replacement. 40084451Seschrock */ 4009789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 40101544Seschrock oldvd = vd->vdev_child[0]; 40111544Seschrock newvd = vd->vdev_child[1]; 4012789Sahrens 40138241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 40148241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 40151544Seschrock return (oldvd); 40161544Seschrock } 4017789Sahrens 40184451Seschrock /* 40194451Seschrock * Check for a completed resilver with the 'unspare' flag set. 40204451Seschrock */ 40214451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 40224451Seschrock newvd = vd->vdev_child[0]; 40234451Seschrock oldvd = vd->vdev_child[1]; 40244451Seschrock 40254451Seschrock if (newvd->vdev_unspare && 40268241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 40278241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) { 40284451Seschrock newvd->vdev_unspare = 0; 40294451Seschrock return (oldvd); 40304451Seschrock } 40314451Seschrock } 40324451Seschrock 40331544Seschrock return (NULL); 4034789Sahrens } 4035789Sahrens 40361544Seschrock static void 40374451Seschrock spa_vdev_resilver_done(spa_t *spa) 4038789Sahrens { 40398241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 40408241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 40418241SJeff.Bonwick@Sun.COM 40428241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4043789Sahrens 40444451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 40458241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 40468241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 40471544Seschrock guid = vd->vdev_guid; 40488241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 40498241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 40508241SJeff.Bonwick@Sun.COM sguid = 0; 40512082Seschrock /* 40522082Seschrock * If we have just finished replacing a hot spared device, then 40532082Seschrock * we need to detach the parent's first child (the original hot 40542082Seschrock * spare) as well. 40552082Seschrock */ 40568241SJeff.Bonwick@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 40572082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 40588241SJeff.Bonwick@Sun.COM ASSERT(ppvd->vdev_children == 2); 40598241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 40602082Seschrock } 40618241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 40628241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 40631544Seschrock return; 40648241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 40652082Seschrock return; 40668241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4067789Sahrens } 4068789Sahrens 40698241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 4070789Sahrens } 4071789Sahrens 4072789Sahrens /* 407311041SEric.Taylor@Sun.COM * Update the stored path or FRU for this vdev. 40741354Seschrock */ 40751354Seschrock int 40769425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 40779425SEric.Schrock@Sun.COM boolean_t ispath) 40781354Seschrock { 40796643Seschrock vdev_t *vd; 408011041SEric.Taylor@Sun.COM 408111041SEric.Taylor@Sun.COM spa_vdev_state_enter(spa, SCL_ALL); 40821354Seschrock 40839425SEric.Schrock@Sun.COM if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 408411041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOENT)); 40851354Seschrock 40861585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 408711041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 40881585Sbonwick 40899425SEric.Schrock@Sun.COM if (ispath) { 40909425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_path); 40919425SEric.Schrock@Sun.COM vd->vdev_path = spa_strdup(value); 40929425SEric.Schrock@Sun.COM } else { 40939425SEric.Schrock@Sun.COM if (vd->vdev_fru != NULL) 40949425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 40959425SEric.Schrock@Sun.COM vd->vdev_fru = spa_strdup(value); 40969425SEric.Schrock@Sun.COM } 40971354Seschrock 409811041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 40991354Seschrock } 41001354Seschrock 41019425SEric.Schrock@Sun.COM int 41029425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 41039425SEric.Schrock@Sun.COM { 41049425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 41059425SEric.Schrock@Sun.COM } 41069425SEric.Schrock@Sun.COM 41079425SEric.Schrock@Sun.COM int 41089425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 41099425SEric.Schrock@Sun.COM { 41109425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 41119425SEric.Schrock@Sun.COM } 41129425SEric.Schrock@Sun.COM 41131354Seschrock /* 4114789Sahrens * ========================================================================== 4115789Sahrens * SPA Scrubbing 4116789Sahrens * ========================================================================== 4117789Sahrens */ 4118789Sahrens 41197046Sahrens int 41207046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 4121789Sahrens { 41227754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 41234808Sek110237 4124789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 4125789Sahrens return (ENOTSUP); 4126789Sahrens 4127789Sahrens /* 41287046Sahrens * If a resilver was requested, but there is no DTL on a 41297046Sahrens * writeable leaf device, we have nothing to do. 4130789Sahrens */ 41317046Sahrens if (type == POOL_SCRUB_RESILVER && 41327046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 41337046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 41341544Seschrock return (0); 41351544Seschrock } 4136789Sahrens 41377046Sahrens if (type == POOL_SCRUB_EVERYTHING && 41387046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 41397046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 41407046Sahrens return (EBUSY); 41417046Sahrens 41427046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 41437046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 41447046Sahrens } else if (type == POOL_SCRUB_NONE) { 41457046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 41461544Seschrock } else { 41477046Sahrens return (EINVAL); 41481544Seschrock } 4149789Sahrens } 4150789Sahrens 41511544Seschrock /* 41521544Seschrock * ========================================================================== 41531544Seschrock * SPA async task processing 41541544Seschrock * ========================================================================== 41551544Seschrock */ 41561544Seschrock 41571544Seschrock static void 41584451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 4159789Sahrens { 41607361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 41617361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 41627361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 416310575SEric.Schrock@Sun.COM 416410575SEric.Schrock@Sun.COM /* 416510575SEric.Schrock@Sun.COM * We want to clear the stats, but we don't want to do a full 416610575SEric.Schrock@Sun.COM * vdev_clear() as that will cause us to throw away 416710575SEric.Schrock@Sun.COM * degraded/faulted state as well as attempt to reopen the 416810575SEric.Schrock@Sun.COM * device, all of which is a waste. 416910575SEric.Schrock@Sun.COM */ 417010575SEric.Schrock@Sun.COM vd->vdev_stat.vs_read_errors = 0; 417110575SEric.Schrock@Sun.COM vd->vdev_stat.vs_write_errors = 0; 417210575SEric.Schrock@Sun.COM vd->vdev_stat.vs_checksum_errors = 0; 417310575SEric.Schrock@Sun.COM 41747754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 41751544Seschrock } 41767361SBrendan.Gregg@Sun.COM 41777754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 41787361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 41791544Seschrock } 41801544Seschrock 41811544Seschrock static void 41827754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 41837754SJeff.Bonwick@Sun.COM { 41847754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 41857754SJeff.Bonwick@Sun.COM vd->vdev_probe_wanted = 0; 41867754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 41877754SJeff.Bonwick@Sun.COM } 41887754SJeff.Bonwick@Sun.COM 41897754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 41907754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 41917754SJeff.Bonwick@Sun.COM } 41927754SJeff.Bonwick@Sun.COM 41937754SJeff.Bonwick@Sun.COM static void 41949816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd) 41959816SGeorge.Wilson@Sun.COM { 41969816SGeorge.Wilson@Sun.COM sysevent_id_t eid; 41979816SGeorge.Wilson@Sun.COM nvlist_t *attr; 41989816SGeorge.Wilson@Sun.COM char *physpath; 41999816SGeorge.Wilson@Sun.COM 42009816SGeorge.Wilson@Sun.COM if (!spa->spa_autoexpand) 42019816SGeorge.Wilson@Sun.COM return; 42029816SGeorge.Wilson@Sun.COM 42039816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 42049816SGeorge.Wilson@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 42059816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, cvd); 42069816SGeorge.Wilson@Sun.COM } 42079816SGeorge.Wilson@Sun.COM 42089816SGeorge.Wilson@Sun.COM if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 42099816SGeorge.Wilson@Sun.COM return; 42109816SGeorge.Wilson@Sun.COM 42119816SGeorge.Wilson@Sun.COM physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 42129816SGeorge.Wilson@Sun.COM (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 42139816SGeorge.Wilson@Sun.COM 42149816SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 42159816SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 42169816SGeorge.Wilson@Sun.COM 42179816SGeorge.Wilson@Sun.COM (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 42189816SGeorge.Wilson@Sun.COM ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 42199816SGeorge.Wilson@Sun.COM 42209816SGeorge.Wilson@Sun.COM nvlist_free(attr); 42219816SGeorge.Wilson@Sun.COM kmem_free(physpath, MAXPATHLEN); 42229816SGeorge.Wilson@Sun.COM } 42239816SGeorge.Wilson@Sun.COM 42249816SGeorge.Wilson@Sun.COM static void 42251544Seschrock spa_async_thread(spa_t *spa) 42261544Seschrock { 42277754SJeff.Bonwick@Sun.COM int tasks; 42281544Seschrock 42291544Seschrock ASSERT(spa->spa_sync_on); 4230789Sahrens 42311544Seschrock mutex_enter(&spa->spa_async_lock); 42321544Seschrock tasks = spa->spa_async_tasks; 42331544Seschrock spa->spa_async_tasks = 0; 42341544Seschrock mutex_exit(&spa->spa_async_lock); 42351544Seschrock 42361544Seschrock /* 42371635Sbonwick * See if the config needs to be updated. 42381635Sbonwick */ 42391635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 424010922SJeff.Bonwick@Sun.COM uint64_t old_space, new_space; 42419816SGeorge.Wilson@Sun.COM 42421635Sbonwick mutex_enter(&spa_namespace_lock); 424310922SJeff.Bonwick@Sun.COM old_space = metaslab_class_get_space(spa_normal_class(spa)); 42441635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 424510922SJeff.Bonwick@Sun.COM new_space = metaslab_class_get_space(spa_normal_class(spa)); 42461635Sbonwick mutex_exit(&spa_namespace_lock); 42479816SGeorge.Wilson@Sun.COM 42489816SGeorge.Wilson@Sun.COM /* 42499816SGeorge.Wilson@Sun.COM * If the pool grew as a result of the config update, 42509816SGeorge.Wilson@Sun.COM * then log an internal history event. 42519816SGeorge.Wilson@Sun.COM */ 425210922SJeff.Bonwick@Sun.COM if (new_space != old_space) { 42539946SMark.Musante@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ONLINE, 42549946SMark.Musante@Sun.COM spa, NULL, CRED(), 42559946SMark.Musante@Sun.COM "pool '%s' size: %llu(+%llu)", 425610922SJeff.Bonwick@Sun.COM spa_name(spa), new_space, new_space - old_space); 42579816SGeorge.Wilson@Sun.COM } 42581635Sbonwick } 42591635Sbonwick 42601635Sbonwick /* 42614451Seschrock * See if any devices need to be marked REMOVED. 42621544Seschrock */ 42637754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 426410685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 42654451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 42667754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 42677361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 42687754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 42697361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 42707754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 42717754SJeff.Bonwick@Sun.COM } 42727754SJeff.Bonwick@Sun.COM 42739816SGeorge.Wilson@Sun.COM if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 42749816SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 42759816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, spa->spa_root_vdev); 42769816SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 42779816SGeorge.Wilson@Sun.COM } 42789816SGeorge.Wilson@Sun.COM 42797754SJeff.Bonwick@Sun.COM /* 42807754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 42817754SJeff.Bonwick@Sun.COM */ 42827754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 428310685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 42847754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 42857754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 42864451Seschrock } 42871544Seschrock 42881544Seschrock /* 42891544Seschrock * If any devices are done replacing, detach them. 42901544Seschrock */ 42914451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 42924451Seschrock spa_vdev_resilver_done(spa); 4293789Sahrens 42941544Seschrock /* 42951544Seschrock * Kick off a resilver. 42961544Seschrock */ 42977046Sahrens if (tasks & SPA_ASYNC_RESILVER) 42987046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 42991544Seschrock 43001544Seschrock /* 43011544Seschrock * Let the world know that we're done. 43021544Seschrock */ 43031544Seschrock mutex_enter(&spa->spa_async_lock); 43041544Seschrock spa->spa_async_thread = NULL; 43051544Seschrock cv_broadcast(&spa->spa_async_cv); 43061544Seschrock mutex_exit(&spa->spa_async_lock); 43071544Seschrock thread_exit(); 43081544Seschrock } 43091544Seschrock 43101544Seschrock void 43111544Seschrock spa_async_suspend(spa_t *spa) 43121544Seschrock { 43131544Seschrock mutex_enter(&spa->spa_async_lock); 43141544Seschrock spa->spa_async_suspended++; 43151544Seschrock while (spa->spa_async_thread != NULL) 43161544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 43171544Seschrock mutex_exit(&spa->spa_async_lock); 43181544Seschrock } 43191544Seschrock 43201544Seschrock void 43211544Seschrock spa_async_resume(spa_t *spa) 43221544Seschrock { 43231544Seschrock mutex_enter(&spa->spa_async_lock); 43241544Seschrock ASSERT(spa->spa_async_suspended != 0); 43251544Seschrock spa->spa_async_suspended--; 43261544Seschrock mutex_exit(&spa->spa_async_lock); 43271544Seschrock } 43281544Seschrock 43291544Seschrock static void 43301544Seschrock spa_async_dispatch(spa_t *spa) 43311544Seschrock { 43321544Seschrock mutex_enter(&spa->spa_async_lock); 43331544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 43341635Sbonwick spa->spa_async_thread == NULL && 43351635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 43361544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 43371544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 43381544Seschrock mutex_exit(&spa->spa_async_lock); 43391544Seschrock } 43401544Seschrock 43411544Seschrock void 43421544Seschrock spa_async_request(spa_t *spa, int task) 43431544Seschrock { 43441544Seschrock mutex_enter(&spa->spa_async_lock); 43451544Seschrock spa->spa_async_tasks |= task; 43461544Seschrock mutex_exit(&spa->spa_async_lock); 4347789Sahrens } 4348789Sahrens 4349789Sahrens /* 4350789Sahrens * ========================================================================== 4351789Sahrens * SPA syncing routines 4352789Sahrens * ========================================================================== 4353789Sahrens */ 4354789Sahrens static void 435510922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg) 4356789Sahrens { 4357789Sahrens blkptr_t blk; 4358789Sahrens uint64_t itor = 0; 4359789Sahrens uint8_t c = 1; 4360789Sahrens 43617754SJeff.Bonwick@Sun.COM while (bplist_iterate(bpl, &itor, &blk) == 0) { 43627754SJeff.Bonwick@Sun.COM ASSERT(blk.blk_birth < txg); 436310922SJeff.Bonwick@Sun.COM zio_free(spa, txg, &blk); 43647754SJeff.Bonwick@Sun.COM } 4365789Sahrens 4366789Sahrens bplist_vacate(bpl, tx); 4367789Sahrens 4368789Sahrens /* 4369789Sahrens * Pre-dirty the first block so we sync to convergence faster. 4370789Sahrens * (Usually only the first block is needed.) 4371789Sahrens */ 437210922SJeff.Bonwick@Sun.COM dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx); 437310922SJeff.Bonwick@Sun.COM } 437410922SJeff.Bonwick@Sun.COM 437510922SJeff.Bonwick@Sun.COM static void 437610922SJeff.Bonwick@Sun.COM spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 437710922SJeff.Bonwick@Sun.COM { 437810922SJeff.Bonwick@Sun.COM zio_t *zio = arg; 437910922SJeff.Bonwick@Sun.COM 438010922SJeff.Bonwick@Sun.COM zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 438110922SJeff.Bonwick@Sun.COM zio->io_flags)); 4382789Sahrens } 4383789Sahrens 4384789Sahrens static void 43852082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 43862082Seschrock { 43872082Seschrock char *packed = NULL; 43887497STim.Haley@Sun.COM size_t bufsize; 43892082Seschrock size_t nvsize = 0; 43902082Seschrock dmu_buf_t *db; 43912082Seschrock 43922082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 43932082Seschrock 43947497STim.Haley@Sun.COM /* 43957497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 43967497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 43977497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 43987497STim.Haley@Sun.COM */ 43997497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 44007497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 44012082Seschrock 44022082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 44032082Seschrock KM_SLEEP) == 0); 44047497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 44057497STim.Haley@Sun.COM 44067497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 44077497STim.Haley@Sun.COM 44087497STim.Haley@Sun.COM kmem_free(packed, bufsize); 44092082Seschrock 44102082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 44112082Seschrock dmu_buf_will_dirty(db, tx); 44122082Seschrock *(uint64_t *)db->db_data = nvsize; 44132082Seschrock dmu_buf_rele(db, FTAG); 44142082Seschrock } 44152082Seschrock 44162082Seschrock static void 44175450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 44185450Sbrendan const char *config, const char *entry) 44192082Seschrock { 44202082Seschrock nvlist_t *nvroot; 44215450Sbrendan nvlist_t **list; 44222082Seschrock int i; 44232082Seschrock 44245450Sbrendan if (!sav->sav_sync) 44252082Seschrock return; 44262082Seschrock 44272082Seschrock /* 44285450Sbrendan * Update the MOS nvlist describing the list of available devices. 44295450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 44304451Seschrock * valid and the vdevs are labeled appropriately. 44312082Seschrock */ 44325450Sbrendan if (sav->sav_object == 0) { 44335450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 44345450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 44355450Sbrendan sizeof (uint64_t), tx); 44362082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 44375450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 44385450Sbrendan &sav->sav_object, tx) == 0); 44392082Seschrock } 44402082Seschrock 44412082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 44425450Sbrendan if (sav->sav_count == 0) { 44435450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 44442082Seschrock } else { 44455450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 44465450Sbrendan for (i = 0; i < sav->sav_count; i++) 44475450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 44485450Sbrendan B_FALSE, B_FALSE, B_TRUE); 44495450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 44505450Sbrendan sav->sav_count) == 0); 44515450Sbrendan for (i = 0; i < sav->sav_count; i++) 44525450Sbrendan nvlist_free(list[i]); 44535450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 44542082Seschrock } 44552082Seschrock 44565450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 44572926Sek110237 nvlist_free(nvroot); 44582082Seschrock 44595450Sbrendan sav->sav_sync = B_FALSE; 44602082Seschrock } 44612082Seschrock 44622082Seschrock static void 4463789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 4464789Sahrens { 4465789Sahrens nvlist_t *config; 4466789Sahrens 44677754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 4468789Sahrens return; 4469789Sahrens 44707754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 44717754SJeff.Bonwick@Sun.COM 44727754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 44737754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 44747754SJeff.Bonwick@Sun.COM 44757754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 4476789Sahrens 44771635Sbonwick if (spa->spa_config_syncing) 44781635Sbonwick nvlist_free(spa->spa_config_syncing); 44791635Sbonwick spa->spa_config_syncing = config; 4480789Sahrens 44812082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 4482789Sahrens } 4483789Sahrens 44845094Slling /* 44855094Slling * Set zpool properties. 44865094Slling */ 44873912Slling static void 44884543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 44893912Slling { 44903912Slling spa_t *spa = arg1; 44915094Slling objset_t *mos = spa->spa_meta_objset; 44923912Slling nvlist_t *nvp = arg2; 44935094Slling nvpair_t *elem; 44944451Seschrock uint64_t intval; 44956643Seschrock char *strval; 44965094Slling zpool_prop_t prop; 44975094Slling const char *propname; 44985094Slling zprop_type_t proptype; 44995094Slling 45007754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 45017754SJeff.Bonwick@Sun.COM 45025094Slling elem = NULL; 45035094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 45045094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 45055094Slling case ZPOOL_PROP_VERSION: 45065094Slling /* 45075094Slling * Only set version for non-zpool-creation cases 45085094Slling * (set/import). spa_create() needs special care 45095094Slling * for version setting. 45105094Slling */ 45115094Slling if (tx->tx_txg != TXG_INITIAL) { 45125094Slling VERIFY(nvpair_value_uint64(elem, 45135094Slling &intval) == 0); 45145094Slling ASSERT(intval <= SPA_VERSION); 45155094Slling ASSERT(intval >= spa_version(spa)); 45165094Slling spa->spa_uberblock.ub_version = intval; 45175094Slling vdev_config_dirty(spa->spa_root_vdev); 45185094Slling } 45195094Slling break; 45205094Slling 45215094Slling case ZPOOL_PROP_ALTROOT: 45225094Slling /* 45235094Slling * 'altroot' is a non-persistent property. It should 45245094Slling * have been set temporarily at creation or import time. 45255094Slling */ 45265094Slling ASSERT(spa->spa_root != NULL); 45275094Slling break; 45285094Slling 45295363Seschrock case ZPOOL_PROP_CACHEFILE: 45305094Slling /* 45318525SEric.Schrock@Sun.COM * 'cachefile' is also a non-persisitent property. 45325094Slling */ 45334543Smarks break; 45345094Slling default: 45355094Slling /* 45365094Slling * Set pool property values in the poolprops mos object. 45375094Slling */ 45385094Slling if (spa->spa_pool_props_object == 0) { 45395094Slling VERIFY((spa->spa_pool_props_object = 45405094Slling zap_create(mos, DMU_OT_POOL_PROPS, 45415094Slling DMU_OT_NONE, 0, tx)) > 0); 45425094Slling 45435094Slling VERIFY(zap_update(mos, 45445094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 45455094Slling 8, 1, &spa->spa_pool_props_object, tx) 45465094Slling == 0); 45475094Slling } 45485094Slling 45495094Slling /* normalize the property name */ 45505094Slling propname = zpool_prop_to_name(prop); 45515094Slling proptype = zpool_prop_get_type(prop); 45525094Slling 45535094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 45545094Slling ASSERT(proptype == PROP_TYPE_STRING); 45555094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 45565094Slling VERIFY(zap_update(mos, 45575094Slling spa->spa_pool_props_object, propname, 45585094Slling 1, strlen(strval) + 1, strval, tx) == 0); 45595094Slling 45605094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 45615094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 45625094Slling 45635094Slling if (proptype == PROP_TYPE_INDEX) { 45645094Slling const char *unused; 45655094Slling VERIFY(zpool_prop_index_to_string( 45665094Slling prop, intval, &unused) == 0); 45675094Slling } 45685094Slling VERIFY(zap_update(mos, 45695094Slling spa->spa_pool_props_object, propname, 45705094Slling 8, 1, &intval, tx) == 0); 45715094Slling } else { 45725094Slling ASSERT(0); /* not allowed */ 45735094Slling } 45745094Slling 45755329Sgw25295 switch (prop) { 45765329Sgw25295 case ZPOOL_PROP_DELEGATION: 45775094Slling spa->spa_delegation = intval; 45785329Sgw25295 break; 45795329Sgw25295 case ZPOOL_PROP_BOOTFS: 45805094Slling spa->spa_bootfs = intval; 45815329Sgw25295 break; 45825329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 45835329Sgw25295 spa->spa_failmode = intval; 45845329Sgw25295 break; 45859816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 45869816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = intval; 45879816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 45889816SGeorge.Wilson@Sun.COM break; 458910922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 459010922SJeff.Bonwick@Sun.COM spa->spa_dedup_ditto = intval; 459110922SJeff.Bonwick@Sun.COM break; 45925329Sgw25295 default: 45935329Sgw25295 break; 45945329Sgw25295 } 45953912Slling } 45965094Slling 45975094Slling /* log internal history if this is not a zpool create */ 45985094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 45995094Slling tx->tx_txg != TXG_INITIAL) { 46005094Slling spa_history_internal_log(LOG_POOL_PROPSET, 46015094Slling spa, tx, cr, "%s %lld %s", 46027754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 46035094Slling } 46043912Slling } 46057754SJeff.Bonwick@Sun.COM 46067754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 46073912Slling } 46083912Slling 4609789Sahrens /* 4610789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4611789Sahrens * part of the process, so we iterate until it converges. 4612789Sahrens */ 4613789Sahrens void 4614789Sahrens spa_sync(spa_t *spa, uint64_t txg) 4615789Sahrens { 4616789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4617789Sahrens objset_t *mos = spa->spa_meta_objset; 461810922SJeff.Bonwick@Sun.COM bplist_t *defer_bpl = &spa->spa_deferred_bplist; 461910922SJeff.Bonwick@Sun.COM bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 46201635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 4621789Sahrens vdev_t *vd; 4622789Sahrens dmu_tx_t *tx; 46237754SJeff.Bonwick@Sun.COM int error; 4624789Sahrens 4625789Sahrens /* 4626789Sahrens * Lock out configuration changes. 4627789Sahrens */ 46287754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4629789Sahrens 4630789Sahrens spa->spa_syncing_txg = txg; 4631789Sahrens spa->spa_sync_pass = 0; 4632789Sahrens 46337754SJeff.Bonwick@Sun.COM /* 46347754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 46357754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 46367754SJeff.Bonwick@Sun.COM */ 46377754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 46388241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 46398241SJeff.Bonwick@Sun.COM /* 46408241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 46418241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 46428241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 46438241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 46448241SJeff.Bonwick@Sun.COM * into the root vdev tree. 46458241SJeff.Bonwick@Sun.COM */ 46468241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 46478241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 46488241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 46498241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 46508241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 46518241SJeff.Bonwick@Sun.COM } 46528241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 46538241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 46547754SJeff.Bonwick@Sun.COM } 46557754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 46567754SJeff.Bonwick@Sun.COM 465710922SJeff.Bonwick@Sun.COM VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj)); 4658789Sahrens 46592082Seschrock tx = dmu_tx_create_assigned(dp, txg); 46602082Seschrock 46612082Seschrock /* 46624577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 46632082Seschrock * set spa_deflate if we have no raid-z vdevs. 46642082Seschrock */ 46654577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 46664577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 46672082Seschrock int i; 46682082Seschrock 46692082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 46702082Seschrock vd = rvd->vdev_child[i]; 46712082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 46722082Seschrock break; 46732082Seschrock } 46742082Seschrock if (i == rvd->vdev_children) { 46752082Seschrock spa->spa_deflate = TRUE; 46762082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 46772082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 46782082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 46792082Seschrock } 46802082Seschrock } 46812082Seschrock 46827046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 46837046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 46847046Sahrens dsl_pool_create_origin(dp, tx); 46857046Sahrens 46867046Sahrens /* Keeping the origin open increases spa_minref */ 46877046Sahrens spa->spa_minref += 3; 46887046Sahrens } 46897046Sahrens 46907046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 46917046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 46927046Sahrens dsl_pool_upgrade_clones(dp, tx); 46937046Sahrens } 46947046Sahrens 4695789Sahrens /* 4696789Sahrens * If anything has changed in this txg, push the deferred frees 4697789Sahrens * from the previous txg. If not, leave them alone so that we 4698789Sahrens * don't generate work on an otherwise idle system. 4699789Sahrens */ 4700789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 47012329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 47022329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 470310922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa, defer_bpl, tx, txg); 4704789Sahrens 4705789Sahrens /* 4706789Sahrens * Iterate to convergence. 4707789Sahrens */ 4708789Sahrens do { 470910922SJeff.Bonwick@Sun.COM int pass = ++spa->spa_sync_pass; 4710789Sahrens 4711789Sahrens spa_sync_config_object(spa, tx); 47125450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 47135450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 47145450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 47155450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 47161544Seschrock spa_errlog_sync(spa, txg); 4717789Sahrens dsl_pool_sync(dp, txg); 4718789Sahrens 471910922SJeff.Bonwick@Sun.COM if (pass <= SYNC_PASS_DEFERRED_FREE) { 472010922SJeff.Bonwick@Sun.COM zio_t *zio = zio_root(spa, NULL, NULL, 0); 472110922SJeff.Bonwick@Sun.COM bplist_sync(free_bpl, spa_sync_free, zio, tx); 472210922SJeff.Bonwick@Sun.COM VERIFY(zio_wait(zio) == 0); 472310922SJeff.Bonwick@Sun.COM } else { 472410922SJeff.Bonwick@Sun.COM bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx); 4725789Sahrens } 4726789Sahrens 472710922SJeff.Bonwick@Sun.COM ddt_sync(spa, txg); 472810922SJeff.Bonwick@Sun.COM 472910922SJeff.Bonwick@Sun.COM while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 473010922SJeff.Bonwick@Sun.COM vdev_sync(vd, txg); 473110922SJeff.Bonwick@Sun.COM 473210922SJeff.Bonwick@Sun.COM } while (dmu_objset_is_dirty(mos, txg)); 473310922SJeff.Bonwick@Sun.COM 473410922SJeff.Bonwick@Sun.COM ASSERT(free_bpl->bpl_queue == NULL); 473510922SJeff.Bonwick@Sun.COM 473610922SJeff.Bonwick@Sun.COM bplist_close(defer_bpl); 4737789Sahrens 4738789Sahrens /* 4739789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4740789Sahrens * to commit the transaction group. 47411635Sbonwick * 47425688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 47435688Sbonwick * random top-level vdevs that are known to be visible in the 47447754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 47457754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4746789Sahrens */ 47477754SJeff.Bonwick@Sun.COM for (;;) { 47487754SJeff.Bonwick@Sun.COM /* 47497754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 47507754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 47517754SJeff.Bonwick@Sun.COM */ 47527754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 47537754SJeff.Bonwick@Sun.COM 47547754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 47557754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 47567754SJeff.Bonwick@Sun.COM int svdcount = 0; 47577754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 47587754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 47599816SGeorge.Wilson@Sun.COM 47609816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 47617754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 47627754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 47637754SJeff.Bonwick@Sun.COM continue; 47647754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 47657754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 47667754SJeff.Bonwick@Sun.COM break; 47677754SJeff.Bonwick@Sun.COM } 47689725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 47699725SEric.Schrock@Sun.COM if (error != 0) 47709725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, 47719725SEric.Schrock@Sun.COM B_TRUE); 47727754SJeff.Bonwick@Sun.COM } else { 47737754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 47749725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_FALSE); 47759725SEric.Schrock@Sun.COM if (error != 0) 47769725SEric.Schrock@Sun.COM error = vdev_config_sync(rvd->vdev_child, 47779725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_TRUE); 47781635Sbonwick } 47797754SJeff.Bonwick@Sun.COM 47807754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 47817754SJeff.Bonwick@Sun.COM 47827754SJeff.Bonwick@Sun.COM if (error == 0) 47837754SJeff.Bonwick@Sun.COM break; 47847754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 47857754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 47861635Sbonwick } 47872082Seschrock dmu_tx_commit(tx); 47882082Seschrock 47891635Sbonwick /* 47901635Sbonwick * Clear the dirty config list. 47911635Sbonwick */ 47927754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 47931635Sbonwick vdev_config_clean(vd); 47941635Sbonwick 47951635Sbonwick /* 47961635Sbonwick * Now that the new config has synced transactionally, 47971635Sbonwick * let it become visible to the config cache. 47981635Sbonwick */ 47991635Sbonwick if (spa->spa_config_syncing != NULL) { 48001635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 48011635Sbonwick spa->spa_config_txg = txg; 48021635Sbonwick spa->spa_config_syncing = NULL; 48031635Sbonwick } 4804789Sahrens 4805789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4806789Sahrens 480710922SJeff.Bonwick@Sun.COM dsl_pool_sync_done(dp, txg); 4808789Sahrens 4809789Sahrens /* 4810789Sahrens * Update usable space statistics. 4811789Sahrens */ 4812789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4813789Sahrens vdev_sync_done(vd, txg); 4814789Sahrens 481510956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 481610956SGeorge.Wilson@Sun.COM 4817789Sahrens /* 4818789Sahrens * It had better be the case that we didn't dirty anything 48192082Seschrock * since vdev_config_sync(). 4820789Sahrens */ 4821789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4822789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4823789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 482410922SJeff.Bonwick@Sun.COM ASSERT(defer_bpl->bpl_queue == NULL); 482510922SJeff.Bonwick@Sun.COM ASSERT(free_bpl->bpl_queue == NULL); 482610922SJeff.Bonwick@Sun.COM 482710922SJeff.Bonwick@Sun.COM spa->spa_sync_pass = 0; 4828789Sahrens 48297754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 48301544Seschrock 483110921STim.Haley@Sun.COM spa_handle_ignored_writes(spa); 483210921STim.Haley@Sun.COM 48331544Seschrock /* 48341544Seschrock * If any async tasks have been requested, kick them off. 48351544Seschrock */ 48361544Seschrock spa_async_dispatch(spa); 4837789Sahrens } 4838789Sahrens 4839789Sahrens /* 4840789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4841789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4842789Sahrens * sync. 4843789Sahrens */ 4844789Sahrens void 4845789Sahrens spa_sync_allpools(void) 4846789Sahrens { 4847789Sahrens spa_t *spa = NULL; 4848789Sahrens mutex_enter(&spa_namespace_lock); 4849789Sahrens while ((spa = spa_next(spa)) != NULL) { 48507754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4851789Sahrens continue; 4852789Sahrens spa_open_ref(spa, FTAG); 4853789Sahrens mutex_exit(&spa_namespace_lock); 4854789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4855789Sahrens mutex_enter(&spa_namespace_lock); 4856789Sahrens spa_close(spa, FTAG); 4857789Sahrens } 4858789Sahrens mutex_exit(&spa_namespace_lock); 4859789Sahrens } 4860789Sahrens 4861789Sahrens /* 4862789Sahrens * ========================================================================== 4863789Sahrens * Miscellaneous routines 4864789Sahrens * ========================================================================== 4865789Sahrens */ 4866789Sahrens 4867789Sahrens /* 4868789Sahrens * Remove all pools in the system. 4869789Sahrens */ 4870789Sahrens void 4871789Sahrens spa_evict_all(void) 4872789Sahrens { 4873789Sahrens spa_t *spa; 4874789Sahrens 4875789Sahrens /* 4876789Sahrens * Remove all cached state. All pools should be closed now, 4877789Sahrens * so every spa in the AVL tree should be unreferenced. 4878789Sahrens */ 4879789Sahrens mutex_enter(&spa_namespace_lock); 4880789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4881789Sahrens /* 48821544Seschrock * Stop async tasks. The async thread may need to detach 48831544Seschrock * a device that's been replaced, which requires grabbing 48841544Seschrock * spa_namespace_lock, so we must drop it here. 4885789Sahrens */ 4886789Sahrens spa_open_ref(spa, FTAG); 4887789Sahrens mutex_exit(&spa_namespace_lock); 48881544Seschrock spa_async_suspend(spa); 48894808Sek110237 mutex_enter(&spa_namespace_lock); 4890789Sahrens spa_close(spa, FTAG); 4891789Sahrens 4892789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4893789Sahrens spa_unload(spa); 4894789Sahrens spa_deactivate(spa); 4895789Sahrens } 4896789Sahrens spa_remove(spa); 4897789Sahrens } 4898789Sahrens mutex_exit(&spa_namespace_lock); 4899789Sahrens } 49001544Seschrock 49011544Seschrock vdev_t * 49029425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 49031544Seschrock { 49046643Seschrock vdev_t *vd; 49056643Seschrock int i; 49066643Seschrock 49076643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 49086643Seschrock return (vd); 49096643Seschrock 49109425SEric.Schrock@Sun.COM if (aux) { 49116643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 49126643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 49136643Seschrock if (vd->vdev_guid == guid) 49146643Seschrock return (vd); 49156643Seschrock } 49169425SEric.Schrock@Sun.COM 49179425SEric.Schrock@Sun.COM for (i = 0; i < spa->spa_spares.sav_count; i++) { 49189425SEric.Schrock@Sun.COM vd = spa->spa_spares.sav_vdevs[i]; 49199425SEric.Schrock@Sun.COM if (vd->vdev_guid == guid) 49209425SEric.Schrock@Sun.COM return (vd); 49219425SEric.Schrock@Sun.COM } 49226643Seschrock } 49236643Seschrock 49246643Seschrock return (NULL); 49251544Seschrock } 49261760Seschrock 49271760Seschrock void 49285094Slling spa_upgrade(spa_t *spa, uint64_t version) 49291760Seschrock { 49307754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 49311760Seschrock 49321760Seschrock /* 49331760Seschrock * This should only be called for a non-faulted pool, and since a 49341760Seschrock * future version would result in an unopenable pool, this shouldn't be 49351760Seschrock * possible. 49361760Seschrock */ 49374577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 49385094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 49395094Slling 49405094Slling spa->spa_uberblock.ub_version = version; 49411760Seschrock vdev_config_dirty(spa->spa_root_vdev); 49421760Seschrock 49437754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 49442082Seschrock 49452082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 49461760Seschrock } 49472082Seschrock 49482082Seschrock boolean_t 49492082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 49502082Seschrock { 49512082Seschrock int i; 49523377Seschrock uint64_t spareguid; 49535450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 49545450Sbrendan 49555450Sbrendan for (i = 0; i < sav->sav_count; i++) 49565450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 49572082Seschrock return (B_TRUE); 49582082Seschrock 49595450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 49605450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 49615450Sbrendan &spareguid) == 0 && spareguid == guid) 49623377Seschrock return (B_TRUE); 49633377Seschrock } 49643377Seschrock 49652082Seschrock return (B_FALSE); 49662082Seschrock } 49673912Slling 49684451Seschrock /* 49697214Slling * Check if a pool has an active shared spare device. 49707214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 49717214Slling */ 49727214Slling static boolean_t 49737214Slling spa_has_active_shared_spare(spa_t *spa) 49747214Slling { 49757214Slling int i, refcnt; 49767214Slling uint64_t pool; 49777214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 49787214Slling 49797214Slling for (i = 0; i < sav->sav_count; i++) { 49807214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 49817214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 49827214Slling refcnt > 2) 49837214Slling return (B_TRUE); 49847214Slling } 49857214Slling 49867214Slling return (B_FALSE); 49877214Slling } 49887214Slling 49897214Slling /* 49904451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 49914451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 49924451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 49934451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 49944451Seschrock * or zdb as real changes. 49954451Seschrock */ 49964451Seschrock void 49974451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 49984451Seschrock { 49994451Seschrock #ifdef _KERNEL 50004451Seschrock sysevent_t *ev; 50014451Seschrock sysevent_attr_list_t *attr = NULL; 50024451Seschrock sysevent_value_t value; 50034451Seschrock sysevent_id_t eid; 50044451Seschrock 50054451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 50064451Seschrock SE_SLEEP); 50074451Seschrock 50084451Seschrock value.value_type = SE_DATA_TYPE_STRING; 50094451Seschrock value.value.sv_string = spa_name(spa); 50104451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 50114451Seschrock goto done; 50124451Seschrock 50134451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 50144451Seschrock value.value.sv_uint64 = spa_guid(spa); 50154451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 50164451Seschrock goto done; 50174451Seschrock 50184451Seschrock if (vd) { 50194451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 50204451Seschrock value.value.sv_uint64 = vd->vdev_guid; 50214451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 50224451Seschrock SE_SLEEP) != 0) 50234451Seschrock goto done; 50244451Seschrock 50254451Seschrock if (vd->vdev_path) { 50264451Seschrock value.value_type = SE_DATA_TYPE_STRING; 50274451Seschrock value.value.sv_string = vd->vdev_path; 50284451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 50294451Seschrock &value, SE_SLEEP) != 0) 50304451Seschrock goto done; 50314451Seschrock } 50324451Seschrock } 50334451Seschrock 50345756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 50355756Seschrock goto done; 50365756Seschrock attr = NULL; 50375756Seschrock 50384451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 50394451Seschrock 50404451Seschrock done: 50414451Seschrock if (attr) 50424451Seschrock sysevent_free_attr(attr); 50434451Seschrock sysevent_free(ev); 50444451Seschrock #endif 50454451Seschrock } 5046