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_* */ 7611146SGeorge.Wilson@Sun.COM zti_mode_null, /* don't create a taskq */ 779515SJonathan.Adams@Sun.COM zti_nmodes 787754SJeff.Bonwick@Sun.COM }; 792986Sek110237 8011146SGeorge.Wilson@Sun.COM #define ZTI_FIX(n) { zti_mode_fixed, (n) } 8111146SGeorge.Wilson@Sun.COM #define ZTI_PCT(n) { zti_mode_online_percent, (n) } 8211146SGeorge.Wilson@Sun.COM #define ZTI_TUNE { zti_mode_tune, 0 } 8311146SGeorge.Wilson@Sun.COM #define ZTI_NULL { zti_mode_null, 0 } 8411146SGeorge.Wilson@Sun.COM 8511146SGeorge.Wilson@Sun.COM #define ZTI_ONE ZTI_FIX(1) 869515SJonathan.Adams@Sun.COM 879515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info { 8811146SGeorge.Wilson@Sun.COM enum zti_modes zti_mode; 8911146SGeorge.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] = { 9311146SGeorge.Wilson@Sun.COM "issue", "issue_high", "intr", "intr_high" 949515SJonathan.Adams@Sun.COM }; 959515SJonathan.Adams@Sun.COM 9611146SGeorge.Wilson@Sun.COM /* 9711146SGeorge.Wilson@Sun.COM * Define the taskq threads for the following I/O types: 9811146SGeorge.Wilson@Sun.COM * NULL, READ, WRITE, FREE, CLAIM, and IOCTL 9911146SGeorge.Wilson@Sun.COM */ 10011146SGeorge.Wilson@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 10111146SGeorge.Wilson@Sun.COM /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 10211146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 10311146SGeorge.Wilson@Sun.COM { ZTI_FIX(8), ZTI_NULL, ZTI_TUNE, ZTI_NULL }, 10411146SGeorge.Wilson@Sun.COM { ZTI_TUNE, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) }, 10511146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 10611146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 10711146SGeorge.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++) { 60311146SGeorge.Wilson@Sun.COM const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 60411146SGeorge.Wilson@Sun.COM enum zti_modes mode = ztip->zti_mode; 60511146SGeorge.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), 60911146SGeorge.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 63411146SGeorge.Wilson@Sun.COM case zti_mode_null: 63511146SGeorge.Wilson@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 63611146SGeorge.Wilson@Sun.COM break; 63711146SGeorge.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++) { 68411146SGeorge.Wilson@Sun.COM if (spa->spa_zio_taskq[t][q] != NULL) 68511146SGeorge.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); 1806*11149SGeorge.Wilson@Sun.COM 1807*11149SGeorge.Wilson@Sun.COM spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 18081544Seschrock spa->spa_ena = 0; 18091544Seschrock 18101544Seschrock return (error); 1811789Sahrens } 1812789Sahrens 181310921STim.Haley@Sun.COM static int 181410921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 181510921STim.Haley@Sun.COM { 181610921STim.Haley@Sun.COM spa_unload(spa); 181710921STim.Haley@Sun.COM spa_deactivate(spa); 181810921STim.Haley@Sun.COM 181910921STim.Haley@Sun.COM spa->spa_load_max_txg--; 182010921STim.Haley@Sun.COM 182110921STim.Haley@Sun.COM spa_activate(spa, spa_mode_global); 182210921STim.Haley@Sun.COM spa_async_suspend(spa); 182310921STim.Haley@Sun.COM 182410921STim.Haley@Sun.COM return (spa_load(spa, state, mosconfig)); 182510921STim.Haley@Sun.COM } 182610921STim.Haley@Sun.COM 182710921STim.Haley@Sun.COM static int 182810921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 182910921STim.Haley@Sun.COM uint64_t max_request, boolean_t extreme) 183010921STim.Haley@Sun.COM { 183110921STim.Haley@Sun.COM nvlist_t *config = NULL; 183210921STim.Haley@Sun.COM int load_error, rewind_error; 183310921STim.Haley@Sun.COM uint64_t safe_rollback_txg; 183410921STim.Haley@Sun.COM uint64_t min_txg; 183510921STim.Haley@Sun.COM 183611026STim.Haley@Sun.COM if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 183710921STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_load_txg; 183811026STim.Haley@Sun.COM spa->spa_log_state = SPA_LOG_CLEAR; 183911026STim.Haley@Sun.COM } else { 184010921STim.Haley@Sun.COM spa->spa_load_max_txg = max_request; 184111026STim.Haley@Sun.COM } 184210921STim.Haley@Sun.COM 184310921STim.Haley@Sun.COM load_error = rewind_error = spa_load(spa, state, mosconfig); 184410921STim.Haley@Sun.COM if (load_error == 0) 184510921STim.Haley@Sun.COM return (0); 184610921STim.Haley@Sun.COM 184710921STim.Haley@Sun.COM if (spa->spa_root_vdev != NULL) 184810921STim.Haley@Sun.COM config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 184910921STim.Haley@Sun.COM 185010921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 185110921STim.Haley@Sun.COM spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 185210921STim.Haley@Sun.COM 185310921STim.Haley@Sun.COM /* specific txg requested */ 185410921STim.Haley@Sun.COM if (spa->spa_load_max_txg != UINT64_MAX && !extreme) { 185510921STim.Haley@Sun.COM nvlist_free(config); 185610921STim.Haley@Sun.COM return (load_error); 185710921STim.Haley@Sun.COM } 185810921STim.Haley@Sun.COM 185910921STim.Haley@Sun.COM /* Price of rolling back is discarding txgs, including log */ 186010921STim.Haley@Sun.COM if (state == SPA_LOAD_RECOVER) 186110921STim.Haley@Sun.COM spa->spa_log_state = SPA_LOG_CLEAR; 186210921STim.Haley@Sun.COM 186310921STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 186410921STim.Haley@Sun.COM safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE; 186510921STim.Haley@Sun.COM 186610921STim.Haley@Sun.COM min_txg = extreme ? TXG_INITIAL : safe_rollback_txg; 186710921STim.Haley@Sun.COM while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) { 186810921STim.Haley@Sun.COM if (spa->spa_load_max_txg < safe_rollback_txg) 186910921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_TRUE; 187010921STim.Haley@Sun.COM rewind_error = spa_load_retry(spa, state, mosconfig); 187110921STim.Haley@Sun.COM } 187210921STim.Haley@Sun.COM 187310921STim.Haley@Sun.COM if (config) 187410921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa, config); 187510921STim.Haley@Sun.COM 187610921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_FALSE; 187710921STim.Haley@Sun.COM spa->spa_load_max_txg = UINT64_MAX; 187810921STim.Haley@Sun.COM 187910921STim.Haley@Sun.COM if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 188010921STim.Haley@Sun.COM spa_config_set(spa, config); 188110921STim.Haley@Sun.COM 188210921STim.Haley@Sun.COM return (state == SPA_LOAD_RECOVER ? rewind_error : load_error); 188310921STim.Haley@Sun.COM } 188410921STim.Haley@Sun.COM 1885789Sahrens /* 1886789Sahrens * Pool Open/Import 1887789Sahrens * 1888789Sahrens * The import case is identical to an open except that the configuration is sent 1889789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1890789Sahrens * case of an open, the pool configuration will exist in the 18914451Seschrock * POOL_STATE_UNINITIALIZED state. 1892789Sahrens * 1893789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1894789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1895789Sahrens * ambiguous state. 1896789Sahrens */ 1897789Sahrens static int 189810921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 189910921STim.Haley@Sun.COM nvlist_t **config) 1900789Sahrens { 1901789Sahrens spa_t *spa; 190210921STim.Haley@Sun.COM boolean_t norewind; 190310921STim.Haley@Sun.COM boolean_t extreme; 190410921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 190510921STim.Haley@Sun.COM spa_load_state_t state = SPA_LOAD_OPEN; 1906789Sahrens int error; 1907789Sahrens int locked = B_FALSE; 1908789Sahrens 1909789Sahrens *spapp = NULL; 1910789Sahrens 191110921STim.Haley@Sun.COM zpool_get_rewind_policy(nvpolicy, &policy); 191210921STim.Haley@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 191310921STim.Haley@Sun.COM state = SPA_LOAD_RECOVER; 191410921STim.Haley@Sun.COM norewind = (policy.zrp_request == ZPOOL_NO_REWIND); 191510921STim.Haley@Sun.COM extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0); 191610921STim.Haley@Sun.COM 1917789Sahrens /* 1918789Sahrens * As disgusting as this is, we need to support recursive calls to this 1919789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1920789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1921789Sahrens * avoid dsl_dir_open() calling this in the first place. 1922789Sahrens */ 1923789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1924789Sahrens mutex_enter(&spa_namespace_lock); 1925789Sahrens locked = B_TRUE; 1926789Sahrens } 1927789Sahrens 1928789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1929789Sahrens if (locked) 1930789Sahrens mutex_exit(&spa_namespace_lock); 1931789Sahrens return (ENOENT); 1932789Sahrens } 193310921STim.Haley@Sun.COM 1934789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1935789Sahrens 19368241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 1937789Sahrens 193810921STim.Haley@Sun.COM if (spa->spa_last_open_failed && norewind) { 193910921STim.Haley@Sun.COM if (config != NULL && spa->spa_config) 194010921STim.Haley@Sun.COM VERIFY(nvlist_dup(spa->spa_config, 194110921STim.Haley@Sun.COM config, KM_SLEEP) == 0); 194210921STim.Haley@Sun.COM spa_deactivate(spa); 194310921STim.Haley@Sun.COM if (locked) 194410921STim.Haley@Sun.COM mutex_exit(&spa_namespace_lock); 194510921STim.Haley@Sun.COM return (spa->spa_last_open_failed); 194610921STim.Haley@Sun.COM } 194710921STim.Haley@Sun.COM 194810921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 194910921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 195010921STim.Haley@Sun.COM 195110921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 195210921STim.Haley@Sun.COM extreme); 1953789Sahrens 1954789Sahrens if (error == EBADF) { 1955789Sahrens /* 19561986Seschrock * If vdev_validate() returns failure (indicated by 19571986Seschrock * EBADF), it indicates that one of the vdevs indicates 19581986Seschrock * that the pool has been exported or destroyed. If 19591986Seschrock * this is the case, the config cache is out of sync and 19601986Seschrock * we should remove the pool from the namespace. 1961789Sahrens */ 1962789Sahrens spa_unload(spa); 1963789Sahrens spa_deactivate(spa); 19646643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1965789Sahrens spa_remove(spa); 1966789Sahrens if (locked) 1967789Sahrens mutex_exit(&spa_namespace_lock); 1968789Sahrens return (ENOENT); 19691544Seschrock } 19701544Seschrock 19711544Seschrock if (error) { 1972789Sahrens /* 1973789Sahrens * We can't open the pool, but we still have useful 1974789Sahrens * information: the state of each vdev after the 1975789Sahrens * attempted vdev_open(). Return this to the user. 1976789Sahrens */ 197710921STim.Haley@Sun.COM if (config != NULL && spa->spa_config) 197810921STim.Haley@Sun.COM VERIFY(nvlist_dup(spa->spa_config, config, 197910921STim.Haley@Sun.COM KM_SLEEP) == 0); 1980789Sahrens spa_unload(spa); 1981789Sahrens spa_deactivate(spa); 198210921STim.Haley@Sun.COM spa->spa_last_open_failed = error; 1983789Sahrens if (locked) 1984789Sahrens mutex_exit(&spa_namespace_lock); 1985789Sahrens *spapp = NULL; 1986789Sahrens return (error); 1987789Sahrens } 198810921STim.Haley@Sun.COM 1989789Sahrens } 1990789Sahrens 1991789Sahrens spa_open_ref(spa, tag); 19924451Seschrock 199310921STim.Haley@Sun.COM 199410921STim.Haley@Sun.COM if (config != NULL) 199510921STim.Haley@Sun.COM *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 199610921STim.Haley@Sun.COM 199711026STim.Haley@Sun.COM if (locked) { 199811026STim.Haley@Sun.COM spa->spa_last_open_failed = 0; 199911026STim.Haley@Sun.COM spa->spa_last_ubsync_txg = 0; 200011026STim.Haley@Sun.COM spa->spa_load_txg = 0; 2001789Sahrens mutex_exit(&spa_namespace_lock); 200211026STim.Haley@Sun.COM } 2003789Sahrens 2004789Sahrens *spapp = spa; 2005789Sahrens 2006789Sahrens return (0); 2007789Sahrens } 2008789Sahrens 2009789Sahrens int 201010921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 201110921STim.Haley@Sun.COM nvlist_t **config) 201210921STim.Haley@Sun.COM { 201310921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, policy, config)); 201410921STim.Haley@Sun.COM } 201510921STim.Haley@Sun.COM 201610921STim.Haley@Sun.COM int 2017789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2018789Sahrens { 201910921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, NULL, NULL)); 2020789Sahrens } 2021789Sahrens 20221544Seschrock /* 20231544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 20241544Seschrock * preventing it from being exported or destroyed. 20251544Seschrock */ 20261544Seschrock spa_t * 20271544Seschrock spa_inject_addref(char *name) 20281544Seschrock { 20291544Seschrock spa_t *spa; 20301544Seschrock 20311544Seschrock mutex_enter(&spa_namespace_lock); 20321544Seschrock if ((spa = spa_lookup(name)) == NULL) { 20331544Seschrock mutex_exit(&spa_namespace_lock); 20341544Seschrock return (NULL); 20351544Seschrock } 20361544Seschrock spa->spa_inject_ref++; 20371544Seschrock mutex_exit(&spa_namespace_lock); 20381544Seschrock 20391544Seschrock return (spa); 20401544Seschrock } 20411544Seschrock 20421544Seschrock void 20431544Seschrock spa_inject_delref(spa_t *spa) 20441544Seschrock { 20451544Seschrock mutex_enter(&spa_namespace_lock); 20461544Seschrock spa->spa_inject_ref--; 20471544Seschrock mutex_exit(&spa_namespace_lock); 20481544Seschrock } 20491544Seschrock 20505450Sbrendan /* 20515450Sbrendan * Add spares device information to the nvlist. 20525450Sbrendan */ 20532082Seschrock static void 20542082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 20552082Seschrock { 20562082Seschrock nvlist_t **spares; 20572082Seschrock uint_t i, nspares; 20582082Seschrock nvlist_t *nvroot; 20592082Seschrock uint64_t guid; 20602082Seschrock vdev_stat_t *vs; 20612082Seschrock uint_t vsc; 20623377Seschrock uint64_t pool; 20632082Seschrock 20649425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 20659425SEric.Schrock@Sun.COM 20665450Sbrendan if (spa->spa_spares.sav_count == 0) 20672082Seschrock return; 20682082Seschrock 20692082Seschrock VERIFY(nvlist_lookup_nvlist(config, 20702082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 20715450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 20722082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 20732082Seschrock if (nspares != 0) { 20742082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 20752082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 20762082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 20772082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 20782082Seschrock 20792082Seschrock /* 20802082Seschrock * Go through and find any spares which have since been 20812082Seschrock * repurposed as an active spare. If this is the case, update 20822082Seschrock * their status appropriately. 20832082Seschrock */ 20842082Seschrock for (i = 0; i < nspares; i++) { 20852082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 20862082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 20877214Slling if (spa_spare_exists(guid, &pool, NULL) && 20887214Slling pool != 0ULL) { 20892082Seschrock VERIFY(nvlist_lookup_uint64_array( 20902082Seschrock spares[i], ZPOOL_CONFIG_STATS, 20912082Seschrock (uint64_t **)&vs, &vsc) == 0); 20922082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 20932082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 20942082Seschrock } 20952082Seschrock } 20962082Seschrock } 20972082Seschrock } 20982082Seschrock 20995450Sbrendan /* 21005450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 21015450Sbrendan */ 21025450Sbrendan static void 21035450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 21045450Sbrendan { 21055450Sbrendan nvlist_t **l2cache; 21065450Sbrendan uint_t i, j, nl2cache; 21075450Sbrendan nvlist_t *nvroot; 21085450Sbrendan uint64_t guid; 21095450Sbrendan vdev_t *vd; 21105450Sbrendan vdev_stat_t *vs; 21115450Sbrendan uint_t vsc; 21125450Sbrendan 21139425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 21149425SEric.Schrock@Sun.COM 21155450Sbrendan if (spa->spa_l2cache.sav_count == 0) 21165450Sbrendan return; 21175450Sbrendan 21185450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 21195450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 21205450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 21215450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 21225450Sbrendan if (nl2cache != 0) { 21235450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 21245450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21255450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 21265450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 21275450Sbrendan 21285450Sbrendan /* 21295450Sbrendan * Update level 2 cache device stats. 21305450Sbrendan */ 21315450Sbrendan 21325450Sbrendan for (i = 0; i < nl2cache; i++) { 21335450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 21345450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 21355450Sbrendan 21365450Sbrendan vd = NULL; 21375450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 21385450Sbrendan if (guid == 21395450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 21405450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 21415450Sbrendan break; 21425450Sbrendan } 21435450Sbrendan } 21445450Sbrendan ASSERT(vd != NULL); 21455450Sbrendan 21465450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 21475450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 21485450Sbrendan vdev_get_stats(vd, vs); 21495450Sbrendan } 21505450Sbrendan } 21515450Sbrendan } 21525450Sbrendan 2153789Sahrens int 21541544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 2155789Sahrens { 2156789Sahrens int error; 2157789Sahrens spa_t *spa; 2158789Sahrens 2159789Sahrens *config = NULL; 216010921STim.Haley@Sun.COM error = spa_open_common(name, &spa, FTAG, NULL, config); 2161789Sahrens 21629425SEric.Schrock@Sun.COM if (spa != NULL) { 21639425SEric.Schrock@Sun.COM /* 21649425SEric.Schrock@Sun.COM * This still leaves a window of inconsistency where the spares 21659425SEric.Schrock@Sun.COM * or l2cache devices could change and the config would be 21669425SEric.Schrock@Sun.COM * self-inconsistent. 21679425SEric.Schrock@Sun.COM */ 21689425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 21699425SEric.Schrock@Sun.COM 21709425SEric.Schrock@Sun.COM if (*config != NULL) { 21717754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 21729425SEric.Schrock@Sun.COM ZPOOL_CONFIG_ERRCOUNT, 21739425SEric.Schrock@Sun.COM spa_get_errlog_size(spa)) == 0); 21749425SEric.Schrock@Sun.COM 21759425SEric.Schrock@Sun.COM if (spa_suspended(spa)) 21769425SEric.Schrock@Sun.COM VERIFY(nvlist_add_uint64(*config, 21779425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SUSPENDED, 21789425SEric.Schrock@Sun.COM spa->spa_failmode) == 0); 21799425SEric.Schrock@Sun.COM 21809425SEric.Schrock@Sun.COM spa_add_spares(spa, *config); 21819425SEric.Schrock@Sun.COM spa_add_l2cache(spa, *config); 21829425SEric.Schrock@Sun.COM } 21832082Seschrock } 21842082Seschrock 21851544Seschrock /* 21861544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 21871544Seschrock * and call spa_lookup() directly. 21881544Seschrock */ 21891544Seschrock if (altroot) { 21901544Seschrock if (spa == NULL) { 21911544Seschrock mutex_enter(&spa_namespace_lock); 21921544Seschrock spa = spa_lookup(name); 21931544Seschrock if (spa) 21941544Seschrock spa_altroot(spa, altroot, buflen); 21951544Seschrock else 21961544Seschrock altroot[0] = '\0'; 21971544Seschrock spa = NULL; 21981544Seschrock mutex_exit(&spa_namespace_lock); 21991544Seschrock } else { 22001544Seschrock spa_altroot(spa, altroot, buflen); 22011544Seschrock } 22021544Seschrock } 22031544Seschrock 22049425SEric.Schrock@Sun.COM if (spa != NULL) { 22059425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 2206789Sahrens spa_close(spa, FTAG); 22079425SEric.Schrock@Sun.COM } 2208789Sahrens 2209789Sahrens return (error); 2210789Sahrens } 2211789Sahrens 2212789Sahrens /* 22135450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 22145450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 22155450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 22165450Sbrendan * specified, as long as they are well-formed. 22172082Seschrock */ 22182082Seschrock static int 22195450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 22205450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 22215450Sbrendan vdev_labeltype_t label) 22222082Seschrock { 22235450Sbrendan nvlist_t **dev; 22245450Sbrendan uint_t i, ndev; 22252082Seschrock vdev_t *vd; 22262082Seschrock int error; 22272082Seschrock 22287754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 22297754SJeff.Bonwick@Sun.COM 22302082Seschrock /* 22315450Sbrendan * It's acceptable to have no devs specified. 22322082Seschrock */ 22335450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 22342082Seschrock return (0); 22352082Seschrock 22365450Sbrendan if (ndev == 0) 22372082Seschrock return (EINVAL); 22382082Seschrock 22392082Seschrock /* 22405450Sbrendan * Make sure the pool is formatted with a version that supports this 22415450Sbrendan * device type. 22422082Seschrock */ 22435450Sbrendan if (spa_version(spa) < version) 22442082Seschrock return (ENOTSUP); 22452082Seschrock 22463377Seschrock /* 22475450Sbrendan * Set the pending device list so we correctly handle device in-use 22483377Seschrock * checking. 22493377Seschrock */ 22505450Sbrendan sav->sav_pending = dev; 22515450Sbrendan sav->sav_npending = ndev; 22525450Sbrendan 22535450Sbrendan for (i = 0; i < ndev; i++) { 22545450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 22552082Seschrock mode)) != 0) 22563377Seschrock goto out; 22572082Seschrock 22582082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 22592082Seschrock vdev_free(vd); 22603377Seschrock error = EINVAL; 22613377Seschrock goto out; 22622082Seschrock } 22632082Seschrock 22645450Sbrendan /* 22657754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 22667754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 22675450Sbrendan */ 22687754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 22695450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 22705450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 22715450Sbrendan error = ENOTBLK; 22725450Sbrendan goto out; 22735450Sbrendan } 22747754SJeff.Bonwick@Sun.COM #endif 22752082Seschrock vd->vdev_top = vd; 22763377Seschrock 22773377Seschrock if ((error = vdev_open(vd)) == 0 && 22785450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 22795450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 22803377Seschrock vd->vdev_guid) == 0); 22812082Seschrock } 22822082Seschrock 22832082Seschrock vdev_free(vd); 22843377Seschrock 22855450Sbrendan if (error && 22865450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 22873377Seschrock goto out; 22883377Seschrock else 22893377Seschrock error = 0; 22902082Seschrock } 22912082Seschrock 22923377Seschrock out: 22935450Sbrendan sav->sav_pending = NULL; 22945450Sbrendan sav->sav_npending = 0; 22953377Seschrock return (error); 22962082Seschrock } 22972082Seschrock 22985450Sbrendan static int 22995450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 23005450Sbrendan { 23015450Sbrendan int error; 23025450Sbrendan 23037754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 23047754SJeff.Bonwick@Sun.COM 23055450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 23065450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 23075450Sbrendan VDEV_LABEL_SPARE)) != 0) { 23085450Sbrendan return (error); 23095450Sbrendan } 23105450Sbrendan 23115450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 23125450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 23135450Sbrendan VDEV_LABEL_L2CACHE)); 23145450Sbrendan } 23155450Sbrendan 23165450Sbrendan static void 23175450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 23185450Sbrendan const char *config) 23195450Sbrendan { 23205450Sbrendan int i; 23215450Sbrendan 23225450Sbrendan if (sav->sav_config != NULL) { 23235450Sbrendan nvlist_t **olddevs; 23245450Sbrendan uint_t oldndevs; 23255450Sbrendan nvlist_t **newdevs; 23265450Sbrendan 23275450Sbrendan /* 23285450Sbrendan * Generate new dev list by concatentating with the 23295450Sbrendan * current dev list. 23305450Sbrendan */ 23315450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 23325450Sbrendan &olddevs, &oldndevs) == 0); 23335450Sbrendan 23345450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 23355450Sbrendan (ndevs + oldndevs), KM_SLEEP); 23365450Sbrendan for (i = 0; i < oldndevs; i++) 23375450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 23385450Sbrendan KM_SLEEP) == 0); 23395450Sbrendan for (i = 0; i < ndevs; i++) 23405450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 23415450Sbrendan KM_SLEEP) == 0); 23425450Sbrendan 23435450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 23445450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 23455450Sbrendan 23465450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 23475450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 23485450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 23495450Sbrendan nvlist_free(newdevs[i]); 23505450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 23515450Sbrendan } else { 23525450Sbrendan /* 23535450Sbrendan * Generate a new dev list. 23545450Sbrendan */ 23555450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 23565450Sbrendan KM_SLEEP) == 0); 23575450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 23585450Sbrendan devs, ndevs) == 0); 23595450Sbrendan } 23605450Sbrendan } 23615450Sbrendan 23625450Sbrendan /* 23635450Sbrendan * Stop and drop level 2 ARC devices 23645450Sbrendan */ 23655450Sbrendan void 23665450Sbrendan spa_l2cache_drop(spa_t *spa) 23675450Sbrendan { 23685450Sbrendan vdev_t *vd; 23695450Sbrendan int i; 23705450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 23715450Sbrendan 23725450Sbrendan for (i = 0; i < sav->sav_count; i++) { 23735450Sbrendan uint64_t pool; 23745450Sbrendan 23755450Sbrendan vd = sav->sav_vdevs[i]; 23765450Sbrendan ASSERT(vd != NULL); 23775450Sbrendan 23788241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 23798241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 23805450Sbrendan l2arc_remove_vdev(vd); 23815450Sbrendan if (vd->vdev_isl2cache) 23825450Sbrendan spa_l2cache_remove(vd); 23835450Sbrendan vdev_clear_stats(vd); 23845450Sbrendan (void) vdev_close(vd); 23855450Sbrendan } 23865450Sbrendan } 23875450Sbrendan 23882082Seschrock /* 2389789Sahrens * Pool Creation 2390789Sahrens */ 2391789Sahrens int 23925094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 23937184Stimh const char *history_str, nvlist_t *zplprops) 2394789Sahrens { 2395789Sahrens spa_t *spa; 23965094Slling char *altroot = NULL; 23971635Sbonwick vdev_t *rvd; 2398789Sahrens dsl_pool_t *dp; 2399789Sahrens dmu_tx_t *tx; 24009816SGeorge.Wilson@Sun.COM int error = 0; 2401789Sahrens uint64_t txg = TXG_INITIAL; 24025450Sbrendan nvlist_t **spares, **l2cache; 24035450Sbrendan uint_t nspares, nl2cache; 24045094Slling uint64_t version; 2405789Sahrens 2406789Sahrens /* 2407789Sahrens * If this pool already exists, return failure. 2408789Sahrens */ 2409789Sahrens mutex_enter(&spa_namespace_lock); 2410789Sahrens if (spa_lookup(pool) != NULL) { 2411789Sahrens mutex_exit(&spa_namespace_lock); 2412789Sahrens return (EEXIST); 2413789Sahrens } 2414789Sahrens 2415789Sahrens /* 2416789Sahrens * Allocate a new spa_t structure. 2417789Sahrens */ 24185094Slling (void) nvlist_lookup_string(props, 24195094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 242010921STim.Haley@Sun.COM spa = spa_add(pool, NULL, altroot); 24218241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2422789Sahrens 24235094Slling if (props && (error = spa_prop_validate(spa, props))) { 24245094Slling spa_deactivate(spa); 24255094Slling spa_remove(spa); 24266643Seschrock mutex_exit(&spa_namespace_lock); 24275094Slling return (error); 24285094Slling } 24295094Slling 24305094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 24315094Slling &version) != 0) 24325094Slling version = SPA_VERSION; 24335094Slling ASSERT(version <= SPA_VERSION); 243410922SJeff.Bonwick@Sun.COM 243510922SJeff.Bonwick@Sun.COM spa->spa_first_txg = txg; 243610922SJeff.Bonwick@Sun.COM spa->spa_uberblock.ub_txg = txg - 1; 24375094Slling spa->spa_uberblock.ub_version = version; 2438789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2439789Sahrens 24401635Sbonwick /* 24419234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 24429234SGeorge.Wilson@Sun.COM */ 24439630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 24449630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 24459234SGeorge.Wilson@Sun.COM 24469234SGeorge.Wilson@Sun.COM /* 24471635Sbonwick * Create the root vdev. 24481635Sbonwick */ 24497754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24501635Sbonwick 24512082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 24522082Seschrock 24532082Seschrock ASSERT(error != 0 || rvd != NULL); 24542082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 24552082Seschrock 24565913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 24571635Sbonwick error = EINVAL; 24582082Seschrock 24592082Seschrock if (error == 0 && 24602082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 24615450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 24622082Seschrock VDEV_ALLOC_ADD)) == 0) { 24639816SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 24649816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(rvd->vdev_child[c]); 24659816SGeorge.Wilson@Sun.COM vdev_expand(rvd->vdev_child[c], txg); 24669816SGeorge.Wilson@Sun.COM } 24671635Sbonwick } 24681635Sbonwick 24697754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2470789Sahrens 24712082Seschrock if (error != 0) { 2472789Sahrens spa_unload(spa); 2473789Sahrens spa_deactivate(spa); 2474789Sahrens spa_remove(spa); 2475789Sahrens mutex_exit(&spa_namespace_lock); 2476789Sahrens return (error); 2477789Sahrens } 2478789Sahrens 24792082Seschrock /* 24802082Seschrock * Get the list of spares, if specified. 24812082Seschrock */ 24822082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 24832082Seschrock &spares, &nspares) == 0) { 24845450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 24852082Seschrock KM_SLEEP) == 0); 24865450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 24872082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 24887754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24892082Seschrock spa_load_spares(spa); 24907754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 24915450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 24925450Sbrendan } 24935450Sbrendan 24945450Sbrendan /* 24955450Sbrendan * Get the list of level 2 cache devices, if specified. 24965450Sbrendan */ 24975450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 24985450Sbrendan &l2cache, &nl2cache) == 0) { 24995450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 25005450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 25015450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 25025450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 25037754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 25045450Sbrendan spa_load_l2cache(spa); 25057754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 25065450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 25072082Seschrock } 25082082Seschrock 25097184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2510789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2511789Sahrens 251210956SGeorge.Wilson@Sun.COM /* 251310956SGeorge.Wilson@Sun.COM * Create DDTs (dedup tables). 251410956SGeorge.Wilson@Sun.COM */ 251510956SGeorge.Wilson@Sun.COM ddt_create(spa); 251610956SGeorge.Wilson@Sun.COM 251710956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 251810956SGeorge.Wilson@Sun.COM 2519789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2520789Sahrens 2521789Sahrens /* 2522789Sahrens * Create the pool config object. 2523789Sahrens */ 2524789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 25257497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2526789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2527789Sahrens 25281544Seschrock if (zap_add(spa->spa_meta_objset, 2529789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 25301544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 25311544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 25321544Seschrock } 2533789Sahrens 25345094Slling /* Newly created pools with the right version are always deflated. */ 25355094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 25365094Slling spa->spa_deflate = TRUE; 25375094Slling if (zap_add(spa->spa_meta_objset, 25385094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 25395094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 25405094Slling cmn_err(CE_PANIC, "failed to add deflate"); 25415094Slling } 25422082Seschrock } 25432082Seschrock 2544789Sahrens /* 2545789Sahrens * Create the deferred-free bplist object. Turn off compression 2546789Sahrens * because sync-to-convergence takes longer if the blocksize 2547789Sahrens * keeps changing. 2548789Sahrens */ 254910922SJeff.Bonwick@Sun.COM spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset, 2550789Sahrens 1 << 14, tx); 255110922SJeff.Bonwick@Sun.COM dmu_object_set_compress(spa->spa_meta_objset, 255210922SJeff.Bonwick@Sun.COM spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx); 2553789Sahrens 25541544Seschrock if (zap_add(spa->spa_meta_objset, 2555789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 255610922SJeff.Bonwick@Sun.COM sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) { 25571544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 25581544Seschrock } 2559789Sahrens 25602926Sek110237 /* 25612926Sek110237 * Create the pool's history object. 25622926Sek110237 */ 25635094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 25645094Slling spa_history_create_obj(spa, tx); 25655094Slling 25665094Slling /* 25675094Slling * Set pool properties. 25685094Slling */ 25695094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 25705094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 25715329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 25729816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 257310922SJeff.Bonwick@Sun.COM 25748525SEric.Schrock@Sun.COM if (props != NULL) { 25758525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 25765094Slling spa_sync_props(spa, props, CRED(), tx); 25778525SEric.Schrock@Sun.COM } 25782926Sek110237 2579789Sahrens dmu_tx_commit(tx); 2580789Sahrens 2581789Sahrens spa->spa_sync_on = B_TRUE; 2582789Sahrens txg_sync_start(spa->spa_dsl_pool); 2583789Sahrens 2584789Sahrens /* 2585789Sahrens * We explicitly wait for the first transaction to complete so that our 2586789Sahrens * bean counters are appropriately updated. 2587789Sahrens */ 2588789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2589789Sahrens 25906643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2591789Sahrens 25925094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 25934715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 25949946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_CREATE); 25954715Sek110237 25968667SGeorge.Wilson@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 25978667SGeorge.Wilson@Sun.COM 2598789Sahrens mutex_exit(&spa_namespace_lock); 2599789Sahrens 2600789Sahrens return (0); 2601789Sahrens } 2602789Sahrens 26036423Sgw25295 #ifdef _KERNEL 26046423Sgw25295 /* 26059790SLin.Ling@Sun.COM * Get the root pool information from the root disk, then import the root pool 26069790SLin.Ling@Sun.COM * during the system boot up time. 26076423Sgw25295 */ 26089790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 26099790SLin.Ling@Sun.COM 26109790SLin.Ling@Sun.COM static nvlist_t * 26119790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 26126423Sgw25295 { 26139790SLin.Ling@Sun.COM nvlist_t *config; 26146423Sgw25295 nvlist_t *nvtop, *nvroot; 26156423Sgw25295 uint64_t pgid; 26166423Sgw25295 26179790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 26189790SLin.Ling@Sun.COM return (NULL); 26199790SLin.Ling@Sun.COM 26206423Sgw25295 /* 26216423Sgw25295 * Add this top-level vdev to the child array. 26226423Sgw25295 */ 26239790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 26249790SLin.Ling@Sun.COM &nvtop) == 0); 26259790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 26269790SLin.Ling@Sun.COM &pgid) == 0); 26279790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 26286423Sgw25295 26296423Sgw25295 /* 26306423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 26316423Sgw25295 */ 26326423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 26339790SLin.Ling@Sun.COM VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 26349790SLin.Ling@Sun.COM VDEV_TYPE_ROOT) == 0); 26356423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 26366423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 26376423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 26386423Sgw25295 &nvtop, 1) == 0); 26396423Sgw25295 26406423Sgw25295 /* 26416423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 26426423Sgw25295 * this pool's configuration (remove the old, add the new). 26436423Sgw25295 */ 26446423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 26456423Sgw25295 nvlist_free(nvroot); 26469790SLin.Ling@Sun.COM return (config); 26476423Sgw25295 } 26486423Sgw25295 26496423Sgw25295 /* 26509790SLin.Ling@Sun.COM * Walk the vdev tree and see if we can find a device with "better" 26519790SLin.Ling@Sun.COM * configuration. A configuration is "better" if the label on that 26529790SLin.Ling@Sun.COM * device has a more recent txg. 26536423Sgw25295 */ 26549790SLin.Ling@Sun.COM static void 26559790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 26567147Staylor { 26579816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 26589790SLin.Ling@Sun.COM spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 26599790SLin.Ling@Sun.COM 26609790SLin.Ling@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 26619790SLin.Ling@Sun.COM nvlist_t *label; 26629790SLin.Ling@Sun.COM uint64_t label_txg; 26639790SLin.Ling@Sun.COM 26649790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 26659790SLin.Ling@Sun.COM &label) != 0) 26669790SLin.Ling@Sun.COM return; 26679790SLin.Ling@Sun.COM 26689790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 26699790SLin.Ling@Sun.COM &label_txg) == 0); 26709790SLin.Ling@Sun.COM 26719790SLin.Ling@Sun.COM /* 26729790SLin.Ling@Sun.COM * Do we have a better boot device? 26739790SLin.Ling@Sun.COM */ 26749790SLin.Ling@Sun.COM if (label_txg > *txg) { 26759790SLin.Ling@Sun.COM *txg = label_txg; 26769790SLin.Ling@Sun.COM *avd = vd; 26777147Staylor } 26789790SLin.Ling@Sun.COM nvlist_free(label); 26797147Staylor } 26807147Staylor } 26817147Staylor 26826423Sgw25295 /* 26836423Sgw25295 * Import a root pool. 26846423Sgw25295 * 26857147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 26867147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 26877147Staylor * The GRUB "findroot" command will return the vdev we should boot. 26886423Sgw25295 * 26896423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 26906423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 26916423Sgw25295 * e.g. 26926423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 26936423Sgw25295 */ 26946423Sgw25295 int 26957147Staylor spa_import_rootpool(char *devpath, char *devid) 26966423Sgw25295 { 26979790SLin.Ling@Sun.COM spa_t *spa; 26989790SLin.Ling@Sun.COM vdev_t *rvd, *bvd, *avd = NULL; 26999790SLin.Ling@Sun.COM nvlist_t *config, *nvtop; 27009790SLin.Ling@Sun.COM uint64_t guid, txg; 27016423Sgw25295 char *pname; 27026423Sgw25295 int error; 27036423Sgw25295 27046423Sgw25295 /* 27059790SLin.Ling@Sun.COM * Read the label from the boot device and generate a configuration. 27066423Sgw25295 */ 270710822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 270810822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL) 270910822SJack.Meng@Sun.COM if (config == NULL) { 271010822SJack.Meng@Sun.COM if (strstr(devpath, "/iscsi/ssd") != NULL) { 271110822SJack.Meng@Sun.COM /* iscsi boot */ 271210822SJack.Meng@Sun.COM get_iscsi_bootpath_phy(devpath); 271310822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 271410822SJack.Meng@Sun.COM } 271510822SJack.Meng@Sun.COM } 271610822SJack.Meng@Sun.COM #endif 271710822SJack.Meng@Sun.COM if (config == NULL) { 27189790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 27199790SLin.Ling@Sun.COM devpath); 27209790SLin.Ling@Sun.COM return (EIO); 27219790SLin.Ling@Sun.COM } 27229790SLin.Ling@Sun.COM 27239790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 27249790SLin.Ling@Sun.COM &pname) == 0); 27259790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 27266423Sgw25295 27279425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 27289425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pname)) != NULL) { 27299425SEric.Schrock@Sun.COM /* 27309425SEric.Schrock@Sun.COM * Remove the existing root pool from the namespace so that we 27319425SEric.Schrock@Sun.COM * can replace it with the correct config we just read in. 27329425SEric.Schrock@Sun.COM */ 27339425SEric.Schrock@Sun.COM spa_remove(spa); 27349425SEric.Schrock@Sun.COM } 27359425SEric.Schrock@Sun.COM 273610921STim.Haley@Sun.COM spa = spa_add(pname, config, NULL); 27379425SEric.Schrock@Sun.COM spa->spa_is_root = B_TRUE; 273810100SLin.Ling@Sun.COM spa->spa_load_verbatim = B_TRUE; 27399790SLin.Ling@Sun.COM 27409790SLin.Ling@Sun.COM /* 27419790SLin.Ling@Sun.COM * Build up a vdev tree based on the boot device's label config. 27429790SLin.Ling@Sun.COM */ 27439790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 27449790SLin.Ling@Sun.COM &nvtop) == 0); 27459790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27469790SLin.Ling@Sun.COM error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 27479790SLin.Ling@Sun.COM VDEV_ALLOC_ROOTPOOL); 27489790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 27499790SLin.Ling@Sun.COM if (error) { 27509790SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 27519790SLin.Ling@Sun.COM nvlist_free(config); 27529790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 27539790SLin.Ling@Sun.COM pname); 27549790SLin.Ling@Sun.COM return (error); 27559790SLin.Ling@Sun.COM } 27569790SLin.Ling@Sun.COM 27579790SLin.Ling@Sun.COM /* 27589790SLin.Ling@Sun.COM * Get the boot vdev. 27599790SLin.Ling@Sun.COM */ 27609790SLin.Ling@Sun.COM if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 27619790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 27629790SLin.Ling@Sun.COM (u_longlong_t)guid); 27639790SLin.Ling@Sun.COM error = ENOENT; 27649790SLin.Ling@Sun.COM goto out; 27659790SLin.Ling@Sun.COM } 27669790SLin.Ling@Sun.COM 27679790SLin.Ling@Sun.COM /* 27689790SLin.Ling@Sun.COM * Determine if there is a better boot device. 27699790SLin.Ling@Sun.COM */ 27709790SLin.Ling@Sun.COM avd = bvd; 27719790SLin.Ling@Sun.COM spa_alt_rootvdev(rvd, &avd, &txg); 27729790SLin.Ling@Sun.COM if (avd != bvd) { 27739790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 27749790SLin.Ling@Sun.COM "try booting from '%s'", avd->vdev_path); 27759790SLin.Ling@Sun.COM error = EINVAL; 27769790SLin.Ling@Sun.COM goto out; 27779790SLin.Ling@Sun.COM } 27789790SLin.Ling@Sun.COM 27799790SLin.Ling@Sun.COM /* 27809790SLin.Ling@Sun.COM * If the boot device is part of a spare vdev then ensure that 27819790SLin.Ling@Sun.COM * we're booting off the active spare. 27829790SLin.Ling@Sun.COM */ 27839790SLin.Ling@Sun.COM if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 27849790SLin.Ling@Sun.COM !bvd->vdev_isspare) { 27859790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is currently spared. Please " 27869790SLin.Ling@Sun.COM "try booting from '%s'", 27879790SLin.Ling@Sun.COM bvd->vdev_parent->vdev_child[1]->vdev_path); 27889790SLin.Ling@Sun.COM error = EINVAL; 27899790SLin.Ling@Sun.COM goto out; 27909790SLin.Ling@Sun.COM } 27919790SLin.Ling@Sun.COM 27929790SLin.Ling@Sun.COM error = 0; 27939946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 27949790SLin.Ling@Sun.COM out: 27959790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27969790SLin.Ling@Sun.COM vdev_free(rvd); 27979790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 27989425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 27996423Sgw25295 28009790SLin.Ling@Sun.COM nvlist_free(config); 28016423Sgw25295 return (error); 28026423Sgw25295 } 28039790SLin.Ling@Sun.COM 28046423Sgw25295 #endif 28056423Sgw25295 28066423Sgw25295 /* 28079425SEric.Schrock@Sun.COM * Take a pool and insert it into the namespace as if it had been loaded at 28089425SEric.Schrock@Sun.COM * boot. 28099425SEric.Schrock@Sun.COM */ 28109425SEric.Schrock@Sun.COM int 28119425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props) 28129425SEric.Schrock@Sun.COM { 28139425SEric.Schrock@Sun.COM spa_t *spa; 281410921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 28159425SEric.Schrock@Sun.COM char *altroot = NULL; 28169425SEric.Schrock@Sun.COM 28179425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 28189425SEric.Schrock@Sun.COM if (spa_lookup(pool) != NULL) { 28199425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28209425SEric.Schrock@Sun.COM return (EEXIST); 28219425SEric.Schrock@Sun.COM } 28229425SEric.Schrock@Sun.COM 28239425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 28249425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 282510921STim.Haley@Sun.COM spa = spa_add(pool, config, altroot); 282610921STim.Haley@Sun.COM 282710921STim.Haley@Sun.COM zpool_get_rewind_policy(config, &policy); 282810921STim.Haley@Sun.COM spa->spa_load_max_txg = policy.zrp_txg; 28299425SEric.Schrock@Sun.COM 283010100SLin.Ling@Sun.COM spa->spa_load_verbatim = B_TRUE; 283110000SVictor.Latushkin@Sun.COM 28329425SEric.Schrock@Sun.COM if (props != NULL) 28339425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 28349425SEric.Schrock@Sun.COM 28359425SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 28369425SEric.Schrock@Sun.COM 28379425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28389946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 28399425SEric.Schrock@Sun.COM 28409425SEric.Schrock@Sun.COM return (0); 28419425SEric.Schrock@Sun.COM } 28429425SEric.Schrock@Sun.COM 28439425SEric.Schrock@Sun.COM /* 28446423Sgw25295 * Import a non-root pool into the system. 28456423Sgw25295 */ 28466423Sgw25295 int 28476423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 28486423Sgw25295 { 28499425SEric.Schrock@Sun.COM spa_t *spa; 28509425SEric.Schrock@Sun.COM char *altroot = NULL; 285110921STim.Haley@Sun.COM spa_load_state_t state = SPA_LOAD_IMPORT; 285210921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 28539425SEric.Schrock@Sun.COM int error; 28549425SEric.Schrock@Sun.COM nvlist_t *nvroot; 28559425SEric.Schrock@Sun.COM nvlist_t **spares, **l2cache; 28569425SEric.Schrock@Sun.COM uint_t nspares, nl2cache; 28579425SEric.Schrock@Sun.COM 28589425SEric.Schrock@Sun.COM /* 28599425SEric.Schrock@Sun.COM * If a pool with this name exists, return failure. 28609425SEric.Schrock@Sun.COM */ 28619425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 28629425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pool)) != NULL) { 28639425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 28649425SEric.Schrock@Sun.COM return (EEXIST); 28659425SEric.Schrock@Sun.COM } 28669425SEric.Schrock@Sun.COM 286710921STim.Haley@Sun.COM zpool_get_rewind_policy(config, &policy); 286810921STim.Haley@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 286910921STim.Haley@Sun.COM state = SPA_LOAD_RECOVER; 287010921STim.Haley@Sun.COM 28719425SEric.Schrock@Sun.COM /* 28729425SEric.Schrock@Sun.COM * Create and initialize the spa structure. 28739425SEric.Schrock@Sun.COM */ 28749425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 28759425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 287610921STim.Haley@Sun.COM spa = spa_add(pool, config, altroot); 28779425SEric.Schrock@Sun.COM spa_activate(spa, spa_mode_global); 28789425SEric.Schrock@Sun.COM 28799425SEric.Schrock@Sun.COM /* 28809630SJeff.Bonwick@Sun.COM * Don't start async tasks until we know everything is healthy. 28819630SJeff.Bonwick@Sun.COM */ 28829630SJeff.Bonwick@Sun.COM spa_async_suspend(spa); 28839630SJeff.Bonwick@Sun.COM 28849630SJeff.Bonwick@Sun.COM /* 28859425SEric.Schrock@Sun.COM * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 28869425SEric.Schrock@Sun.COM * because the user-supplied config is actually the one to trust when 28879425SEric.Schrock@Sun.COM * doing an import. 28889425SEric.Schrock@Sun.COM */ 288910921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 289010921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 289110921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 289210921STim.Haley@Sun.COM ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0)); 289310921STim.Haley@Sun.COM 289410921STim.Haley@Sun.COM /* 289510921STim.Haley@Sun.COM * Propagate anything learned about failing or best txgs 289610921STim.Haley@Sun.COM * back to caller 289710921STim.Haley@Sun.COM */ 289810921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa, config); 28999425SEric.Schrock@Sun.COM 29009425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29019425SEric.Schrock@Sun.COM /* 29029425SEric.Schrock@Sun.COM * Toss any existing sparelist, as it doesn't have any validity 29039425SEric.Schrock@Sun.COM * anymore, and conflicts with spa_has_spare(). 29049425SEric.Schrock@Sun.COM */ 29059425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) { 29069425SEric.Schrock@Sun.COM nvlist_free(spa->spa_spares.sav_config); 29079425SEric.Schrock@Sun.COM spa->spa_spares.sav_config = NULL; 29089425SEric.Schrock@Sun.COM spa_load_spares(spa); 29099425SEric.Schrock@Sun.COM } 29109425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) { 29119425SEric.Schrock@Sun.COM nvlist_free(spa->spa_l2cache.sav_config); 29129425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_config = NULL; 29139425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 29149425SEric.Schrock@Sun.COM } 29159425SEric.Schrock@Sun.COM 29169425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 29179425SEric.Schrock@Sun.COM &nvroot) == 0); 29189425SEric.Schrock@Sun.COM if (error == 0) 29199425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 29209425SEric.Schrock@Sun.COM VDEV_ALLOC_SPARE); 29219425SEric.Schrock@Sun.COM if (error == 0) 29229425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 29239425SEric.Schrock@Sun.COM VDEV_ALLOC_L2CACHE); 29249425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29259425SEric.Schrock@Sun.COM 29269425SEric.Schrock@Sun.COM if (props != NULL) 29279425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 29289425SEric.Schrock@Sun.COM 29299425SEric.Schrock@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 29309425SEric.Schrock@Sun.COM (error = spa_prop_set(spa, props)))) { 29319425SEric.Schrock@Sun.COM spa_unload(spa); 29329425SEric.Schrock@Sun.COM spa_deactivate(spa); 29339425SEric.Schrock@Sun.COM spa_remove(spa); 29349425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 29359425SEric.Schrock@Sun.COM return (error); 29369425SEric.Schrock@Sun.COM } 29379425SEric.Schrock@Sun.COM 29389630SJeff.Bonwick@Sun.COM spa_async_resume(spa); 29399630SJeff.Bonwick@Sun.COM 29409425SEric.Schrock@Sun.COM /* 29419425SEric.Schrock@Sun.COM * Override any spares and level 2 cache devices as specified by 29429425SEric.Schrock@Sun.COM * the user, as these may have correct device names/devids, etc. 29439425SEric.Schrock@Sun.COM */ 29449425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29459425SEric.Schrock@Sun.COM &spares, &nspares) == 0) { 29469425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) 29479425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_spares.sav_config, 29489425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 29499425SEric.Schrock@Sun.COM else 29509425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 29519425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 29529425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 29539425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 29549425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29559425SEric.Schrock@Sun.COM spa_load_spares(spa); 29569425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29579425SEric.Schrock@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 29589425SEric.Schrock@Sun.COM } 29599425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 29609425SEric.Schrock@Sun.COM &l2cache, &nl2cache) == 0) { 29619425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) 29629425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 29639425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 29649425SEric.Schrock@Sun.COM else 29659425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 29669425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 29679425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 29689425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 29699425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29709425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 29719425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29729425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_sync = B_TRUE; 29739425SEric.Schrock@Sun.COM } 29749425SEric.Schrock@Sun.COM 297510672SEric.Schrock@Sun.COM /* 297610672SEric.Schrock@Sun.COM * Check for any removed devices. 297710672SEric.Schrock@Sun.COM */ 297810672SEric.Schrock@Sun.COM if (spa->spa_autoreplace) { 297910672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 298010672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 298110672SEric.Schrock@Sun.COM } 298210672SEric.Schrock@Sun.COM 29839425SEric.Schrock@Sun.COM if (spa_writeable(spa)) { 29849425SEric.Schrock@Sun.COM /* 29859425SEric.Schrock@Sun.COM * Update the config cache to include the newly-imported pool. 29869425SEric.Schrock@Sun.COM */ 298710100SLin.Ling@Sun.COM spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 29889425SEric.Schrock@Sun.COM } 29899425SEric.Schrock@Sun.COM 29909816SGeorge.Wilson@Sun.COM /* 29919816SGeorge.Wilson@Sun.COM * It's possible that the pool was expanded while it was exported. 29929816SGeorge.Wilson@Sun.COM * We kick off an async task to handle this for us. 29939816SGeorge.Wilson@Sun.COM */ 29949816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 29959816SGeorge.Wilson@Sun.COM 29969425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 29979946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 29989425SEric.Schrock@Sun.COM 29999425SEric.Schrock@Sun.COM return (0); 30006643Seschrock } 30016643Seschrock 30026643Seschrock 3003789Sahrens /* 3004789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 3005789Sahrens * to get the vdev stats associated with the imported devices. 3006789Sahrens */ 3007789Sahrens #define TRYIMPORT_NAME "$import" 3008789Sahrens 3009789Sahrens nvlist_t * 3010789Sahrens spa_tryimport(nvlist_t *tryconfig) 3011789Sahrens { 3012789Sahrens nvlist_t *config = NULL; 3013789Sahrens char *poolname; 3014789Sahrens spa_t *spa; 3015789Sahrens uint64_t state; 30168680SLin.Ling@Sun.COM int error; 3017789Sahrens 3018789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3019789Sahrens return (NULL); 3020789Sahrens 3021789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3022789Sahrens return (NULL); 3023789Sahrens 30241635Sbonwick /* 30251635Sbonwick * Create and initialize the spa structure. 30261635Sbonwick */ 3027789Sahrens mutex_enter(&spa_namespace_lock); 302810921STim.Haley@Sun.COM spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 30298241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 3030789Sahrens 3031789Sahrens /* 30321635Sbonwick * Pass off the heavy lifting to spa_load(). 30331732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 30341732Sbonwick * is actually the one to trust when doing an import. 3035789Sahrens */ 303610921STim.Haley@Sun.COM error = spa_load(spa, SPA_LOAD_TRYIMPORT, B_TRUE); 3037789Sahrens 3038789Sahrens /* 3039789Sahrens * If 'tryconfig' was at least parsable, return the current config. 3040789Sahrens */ 3041789Sahrens if (spa->spa_root_vdev != NULL) { 3042789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3043789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3044789Sahrens poolname) == 0); 3045789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3046789Sahrens state) == 0); 30473975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 30483975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 30492082Seschrock 30502082Seschrock /* 30516423Sgw25295 * If the bootfs property exists on this pool then we 30526423Sgw25295 * copy it out so that external consumers can tell which 30536423Sgw25295 * pools are bootable. 30546423Sgw25295 */ 30558680SLin.Ling@Sun.COM if ((!error || error == EEXIST) && spa->spa_bootfs) { 30566423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 30576423Sgw25295 30586423Sgw25295 /* 30596423Sgw25295 * We have to play games with the name since the 30606423Sgw25295 * pool was opened as TRYIMPORT_NAME. 30616423Sgw25295 */ 30627754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 30636423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 30646423Sgw25295 char *cp; 30656423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 30666423Sgw25295 30676423Sgw25295 cp = strchr(tmpname, '/'); 30686423Sgw25295 if (cp == NULL) { 30696423Sgw25295 (void) strlcpy(dsname, tmpname, 30706423Sgw25295 MAXPATHLEN); 30716423Sgw25295 } else { 30726423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 30736423Sgw25295 "%s/%s", poolname, ++cp); 30746423Sgw25295 } 30756423Sgw25295 VERIFY(nvlist_add_string(config, 30766423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 30776423Sgw25295 kmem_free(dsname, MAXPATHLEN); 30786423Sgw25295 } 30796423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 30806423Sgw25295 } 30816423Sgw25295 30826423Sgw25295 /* 30835450Sbrendan * Add the list of hot spares and level 2 cache devices. 30842082Seschrock */ 30859425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 30862082Seschrock spa_add_spares(spa, config); 30875450Sbrendan spa_add_l2cache(spa, config); 30889425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 3089789Sahrens } 3090789Sahrens 3091789Sahrens spa_unload(spa); 3092789Sahrens spa_deactivate(spa); 3093789Sahrens spa_remove(spa); 3094789Sahrens mutex_exit(&spa_namespace_lock); 3095789Sahrens 3096789Sahrens return (config); 3097789Sahrens } 3098789Sahrens 3099789Sahrens /* 3100789Sahrens * Pool export/destroy 3101789Sahrens * 3102789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 3103789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 3104789Sahrens * update the pool state and sync all the labels to disk, removing the 31058211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 31068211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 3107789Sahrens */ 3108789Sahrens static int 31097214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 31108211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 3111789Sahrens { 3112789Sahrens spa_t *spa; 3113789Sahrens 31141775Sbillm if (oldconfig) 31151775Sbillm *oldconfig = NULL; 31161775Sbillm 31178241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 3118789Sahrens return (EROFS); 3119789Sahrens 3120789Sahrens mutex_enter(&spa_namespace_lock); 3121789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3122789Sahrens mutex_exit(&spa_namespace_lock); 3123789Sahrens return (ENOENT); 3124789Sahrens } 3125789Sahrens 3126789Sahrens /* 31271544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 31281544Seschrock * reacquire the namespace lock, and see if we can export. 31291544Seschrock */ 31301544Seschrock spa_open_ref(spa, FTAG); 31311544Seschrock mutex_exit(&spa_namespace_lock); 31321544Seschrock spa_async_suspend(spa); 31331544Seschrock mutex_enter(&spa_namespace_lock); 31341544Seschrock spa_close(spa, FTAG); 31351544Seschrock 31361544Seschrock /* 3137789Sahrens * The pool will be in core if it's openable, 3138789Sahrens * in which case we can modify its state. 3139789Sahrens */ 3140789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 3141789Sahrens /* 3142789Sahrens * Objsets may be open only because they're dirty, so we 3143789Sahrens * have to force it to sync before checking spa_refcnt. 3144789Sahrens */ 3145789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 3146789Sahrens 31471544Seschrock /* 31481544Seschrock * A pool cannot be exported or destroyed if there are active 31491544Seschrock * references. If we are resetting a pool, allow references by 31501544Seschrock * fault injection handlers. 31511544Seschrock */ 31521544Seschrock if (!spa_refcount_zero(spa) || 31531544Seschrock (spa->spa_inject_ref != 0 && 31541544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 31551544Seschrock spa_async_resume(spa); 3156789Sahrens mutex_exit(&spa_namespace_lock); 3157789Sahrens return (EBUSY); 3158789Sahrens } 3159789Sahrens 3160789Sahrens /* 31617214Slling * A pool cannot be exported if it has an active shared spare. 31627214Slling * This is to prevent other pools stealing the active spare 31637214Slling * from an exported pool. At user's own will, such pool can 31647214Slling * be forcedly exported. 31657214Slling */ 31667214Slling if (!force && new_state == POOL_STATE_EXPORTED && 31677214Slling spa_has_active_shared_spare(spa)) { 31687214Slling spa_async_resume(spa); 31697214Slling mutex_exit(&spa_namespace_lock); 31707214Slling return (EXDEV); 31717214Slling } 31727214Slling 31737214Slling /* 3174789Sahrens * We want this to be reflected on every label, 3175789Sahrens * so mark them all dirty. spa_unload() will do the 3176789Sahrens * final sync that pushes these changes out. 3177789Sahrens */ 31788211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 31797754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 31801544Seschrock spa->spa_state = new_state; 31811635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 31821544Seschrock vdev_config_dirty(spa->spa_root_vdev); 31837754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 31841544Seschrock } 3185789Sahrens } 3186789Sahrens 31874451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 31884451Seschrock 3189789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3190789Sahrens spa_unload(spa); 3191789Sahrens spa_deactivate(spa); 3192789Sahrens } 3193789Sahrens 31941775Sbillm if (oldconfig && spa->spa_config) 31951775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 31961775Sbillm 31971544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 31988211SGeorge.Wilson@Sun.COM if (!hardforce) 31998211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 32001544Seschrock spa_remove(spa); 32011544Seschrock } 3202789Sahrens mutex_exit(&spa_namespace_lock); 3203789Sahrens 3204789Sahrens return (0); 3205789Sahrens } 3206789Sahrens 3207789Sahrens /* 3208789Sahrens * Destroy a storage pool. 3209789Sahrens */ 3210789Sahrens int 3211789Sahrens spa_destroy(char *pool) 3212789Sahrens { 32138211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 32148211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 3215789Sahrens } 3216789Sahrens 3217789Sahrens /* 3218789Sahrens * Export a storage pool. 3219789Sahrens */ 3220789Sahrens int 32218211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 32228211SGeorge.Wilson@Sun.COM boolean_t hardforce) 3223789Sahrens { 32248211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 32258211SGeorge.Wilson@Sun.COM force, hardforce)); 3226789Sahrens } 3227789Sahrens 3228789Sahrens /* 32291544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 32301544Seschrock * from the namespace in any way. 32311544Seschrock */ 32321544Seschrock int 32331544Seschrock spa_reset(char *pool) 32341544Seschrock { 32357214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 32368211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 32371544Seschrock } 32381544Seschrock 32391544Seschrock /* 3240789Sahrens * ========================================================================== 3241789Sahrens * Device manipulation 3242789Sahrens * ========================================================================== 3243789Sahrens */ 3244789Sahrens 3245789Sahrens /* 32464527Sperrin * Add a device to a storage pool. 3247789Sahrens */ 3248789Sahrens int 3249789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 3250789Sahrens { 325110594SGeorge.Wilson@Sun.COM uint64_t txg, id; 32528241SJeff.Bonwick@Sun.COM int error; 3253789Sahrens vdev_t *rvd = spa->spa_root_vdev; 32541585Sbonwick vdev_t *vd, *tvd; 32555450Sbrendan nvlist_t **spares, **l2cache; 32565450Sbrendan uint_t nspares, nl2cache; 3257789Sahrens 3258789Sahrens txg = spa_vdev_enter(spa); 3259789Sahrens 32602082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 32612082Seschrock VDEV_ALLOC_ADD)) != 0) 32622082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 32632082Seschrock 32647754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 3265789Sahrens 32665450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 32675450Sbrendan &nspares) != 0) 32682082Seschrock nspares = 0; 32692082Seschrock 32705450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 32715450Sbrendan &nl2cache) != 0) 32725450Sbrendan nl2cache = 0; 32735450Sbrendan 32747754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 32752082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 32767754SJeff.Bonwick@Sun.COM 32777754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 32787754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 32797754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 32802082Seschrock 32813377Seschrock /* 32825450Sbrendan * We must validate the spares and l2cache devices after checking the 32835450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 32843377Seschrock */ 32857754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 32863377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 32873377Seschrock 32883377Seschrock /* 32893377Seschrock * Transfer each new top-level vdev from vd to rvd. 32903377Seschrock */ 32918241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 329210594SGeorge.Wilson@Sun.COM 329310594SGeorge.Wilson@Sun.COM /* 329410594SGeorge.Wilson@Sun.COM * Set the vdev id to the first hole, if one exists. 329510594SGeorge.Wilson@Sun.COM */ 329610594SGeorge.Wilson@Sun.COM for (id = 0; id < rvd->vdev_children; id++) { 329710594SGeorge.Wilson@Sun.COM if (rvd->vdev_child[id]->vdev_ishole) { 329810594SGeorge.Wilson@Sun.COM vdev_free(rvd->vdev_child[id]); 329910594SGeorge.Wilson@Sun.COM break; 330010594SGeorge.Wilson@Sun.COM } 330110594SGeorge.Wilson@Sun.COM } 33023377Seschrock tvd = vd->vdev_child[c]; 33033377Seschrock vdev_remove_child(vd, tvd); 330410594SGeorge.Wilson@Sun.COM tvd->vdev_id = id; 33053377Seschrock vdev_add_child(rvd, tvd); 33063377Seschrock vdev_config_dirty(tvd); 33073377Seschrock } 33083377Seschrock 33092082Seschrock if (nspares != 0) { 33105450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 33115450Sbrendan ZPOOL_CONFIG_SPARES); 33122082Seschrock spa_load_spares(spa); 33135450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 33145450Sbrendan } 33155450Sbrendan 33165450Sbrendan if (nl2cache != 0) { 33175450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 33185450Sbrendan ZPOOL_CONFIG_L2CACHE); 33195450Sbrendan spa_load_l2cache(spa); 33205450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3321789Sahrens } 3322789Sahrens 3323789Sahrens /* 33241585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 33251585Sbonwick * If other threads start allocating from these vdevs before we 33261585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 33271585Sbonwick * fail to open the pool because there are DVAs that the config cache 33281585Sbonwick * can't translate. Therefore, we first add the vdevs without 33291585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 33301635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 33311585Sbonwick * 33321585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 33331585Sbonwick * if we lose power at any point in this sequence, the remaining 33341585Sbonwick * steps will be completed the next time we load the pool. 3335789Sahrens */ 33361635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 33371585Sbonwick 33381635Sbonwick mutex_enter(&spa_namespace_lock); 33391635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 33401635Sbonwick mutex_exit(&spa_namespace_lock); 3341789Sahrens 33421635Sbonwick return (0); 3343789Sahrens } 3344789Sahrens 3345789Sahrens /* 3346789Sahrens * Attach a device to a mirror. The arguments are the path to any device 3347789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 3348789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 3349789Sahrens * 3350789Sahrens * If 'replacing' is specified, the new device is intended to replace the 3351789Sahrens * existing device; in this case the two devices are made into their own 33524451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 3353789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 3354789Sahrens * extra rules: you can't attach to it after it's been created, and upon 3355789Sahrens * completion of resilvering, the first disk (the one being replaced) 3356789Sahrens * is automatically detached. 3357789Sahrens */ 3358789Sahrens int 33591544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3360789Sahrens { 3361789Sahrens uint64_t txg, open_txg; 3362789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3363789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 33642082Seschrock vdev_ops_t *pvops; 33657313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 33667313SEric.Kustarz@Sun.COM int newvd_isspare; 33677313SEric.Kustarz@Sun.COM int error; 3368789Sahrens 3369789Sahrens txg = spa_vdev_enter(spa); 3370789Sahrens 33716643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3372789Sahrens 3373789Sahrens if (oldvd == NULL) 3374789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3375789Sahrens 33761585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 33771585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 33781585Sbonwick 3379789Sahrens pvd = oldvd->vdev_parent; 3380789Sahrens 33812082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 33824451Seschrock VDEV_ALLOC_ADD)) != 0) 33834451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 33844451Seschrock 33854451Seschrock if (newrootvd->vdev_children != 1) 3386789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3387789Sahrens 3388789Sahrens newvd = newrootvd->vdev_child[0]; 3389789Sahrens 3390789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3391789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3392789Sahrens 33932082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3394789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3395789Sahrens 33964527Sperrin /* 33974527Sperrin * Spares can't replace logs 33984527Sperrin */ 33997326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 34004527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34014527Sperrin 34022082Seschrock if (!replacing) { 34032082Seschrock /* 34042082Seschrock * For attach, the only allowable parent is a mirror or the root 34052082Seschrock * vdev. 34062082Seschrock */ 34072082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 34082082Seschrock pvd->vdev_ops != &vdev_root_ops) 34092082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34102082Seschrock 34112082Seschrock pvops = &vdev_mirror_ops; 34122082Seschrock } else { 34132082Seschrock /* 34142082Seschrock * Active hot spares can only be replaced by inactive hot 34152082Seschrock * spares. 34162082Seschrock */ 34172082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 34182082Seschrock pvd->vdev_child[1] == oldvd && 34192082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 34202082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34212082Seschrock 34222082Seschrock /* 34232082Seschrock * If the source is a hot spare, and the parent isn't already a 34242082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 34253377Seschrock * want to create a replacing vdev. The user is not allowed to 34263377Seschrock * attach to a spared vdev child unless the 'isspare' state is 34273377Seschrock * the same (spare replaces spare, non-spare replaces 34283377Seschrock * non-spare). 34292082Seschrock */ 34302082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 34312082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34323377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 34333377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 34343377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 34352082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 34362082Seschrock newvd->vdev_isspare) 34372082Seschrock pvops = &vdev_spare_ops; 34382082Seschrock else 34392082Seschrock pvops = &vdev_replacing_ops; 34402082Seschrock } 34412082Seschrock 34421175Slling /* 34439816SGeorge.Wilson@Sun.COM * Make sure the new device is big enough. 34441175Slling */ 34459816SGeorge.Wilson@Sun.COM if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3446789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3447789Sahrens 34481732Sbonwick /* 34491732Sbonwick * The new device cannot have a higher alignment requirement 34501732Sbonwick * than the top-level vdev. 34511732Sbonwick */ 34521732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3453789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3454789Sahrens 3455789Sahrens /* 3456789Sahrens * If this is an in-place replacement, update oldvd's path and devid 3457789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3458789Sahrens */ 3459789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3460789Sahrens spa_strfree(oldvd->vdev_path); 3461789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3462789Sahrens KM_SLEEP); 3463789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3464789Sahrens newvd->vdev_path, "old"); 3465789Sahrens if (oldvd->vdev_devid != NULL) { 3466789Sahrens spa_strfree(oldvd->vdev_devid); 3467789Sahrens oldvd->vdev_devid = NULL; 3468789Sahrens } 3469789Sahrens } 3470789Sahrens 3471789Sahrens /* 34722082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 34732082Seschrock * mirror/replacing/spare vdev above oldvd. 3474789Sahrens */ 3475789Sahrens if (pvd->vdev_ops != pvops) 3476789Sahrens pvd = vdev_add_parent(oldvd, pvops); 3477789Sahrens 3478789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3479789Sahrens ASSERT(pvd->vdev_ops == pvops); 3480789Sahrens ASSERT(oldvd->vdev_parent == pvd); 3481789Sahrens 3482789Sahrens /* 3483789Sahrens * Extract the new device from its root and add it to pvd. 3484789Sahrens */ 3485789Sahrens vdev_remove_child(newrootvd, newvd); 3486789Sahrens newvd->vdev_id = pvd->vdev_children; 348710594SGeorge.Wilson@Sun.COM newvd->vdev_crtxg = oldvd->vdev_crtxg; 3488789Sahrens vdev_add_child(pvd, newvd); 3489789Sahrens 3490789Sahrens tvd = newvd->vdev_top; 3491789Sahrens ASSERT(pvd->vdev_top == tvd); 3492789Sahrens ASSERT(tvd->vdev_parent == rvd); 3493789Sahrens 3494789Sahrens vdev_config_dirty(tvd); 3495789Sahrens 3496789Sahrens /* 3497789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3498789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3499789Sahrens */ 3500789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 3501789Sahrens 35028241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, 35038241SJeff.Bonwick@Sun.COM TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3504789Sahrens 35059425SEric.Schrock@Sun.COM if (newvd->vdev_isspare) { 35063377Seschrock spa_spare_activate(newvd); 35079425SEric.Schrock@Sun.COM spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 35089425SEric.Schrock@Sun.COM } 35099425SEric.Schrock@Sun.COM 35107754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 35117754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 35127313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 35131544Seschrock 3514789Sahrens /* 3515789Sahrens * Mark newvd's DTL dirty in this txg. 3516789Sahrens */ 35171732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3518789Sahrens 3519789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3520789Sahrens 35219946SMark.Musante@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL, 35229946SMark.Musante@Sun.COM CRED(), "%s vdev=%s %s vdev=%s", 35239946SMark.Musante@Sun.COM replacing && newvd_isspare ? "spare in" : 35249946SMark.Musante@Sun.COM replacing ? "replace" : "attach", newvdpath, 35259946SMark.Musante@Sun.COM replacing ? "for" : "to", oldvdpath); 35267313SEric.Kustarz@Sun.COM 35277313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 35287313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 35297313SEric.Kustarz@Sun.COM 3530789Sahrens /* 35317046Sahrens * Kick off a resilver to update newvd. 3532789Sahrens */ 35337046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3534789Sahrens 3535789Sahrens return (0); 3536789Sahrens } 3537789Sahrens 3538789Sahrens /* 3539789Sahrens * Detach a device from a mirror or replacing vdev. 3540789Sahrens * If 'replace_done' is specified, only detach if the parent 3541789Sahrens * is a replacing vdev. 3542789Sahrens */ 3543789Sahrens int 35448241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3545789Sahrens { 3546789Sahrens uint64_t txg; 35478241SJeff.Bonwick@Sun.COM int error; 3548789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3549789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 35502082Seschrock boolean_t unspare = B_FALSE; 35512082Seschrock uint64_t unspare_guid; 35526673Seschrock size_t len; 3553789Sahrens 3554789Sahrens txg = spa_vdev_enter(spa); 3555789Sahrens 35566643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3557789Sahrens 3558789Sahrens if (vd == NULL) 3559789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3560789Sahrens 35611585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 35621585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35631585Sbonwick 3564789Sahrens pvd = vd->vdev_parent; 3565789Sahrens 3566789Sahrens /* 35678241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 35688241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 35698241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 35708241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 35718241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 35728241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 35738241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 35748241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 35758241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 35768241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 35778241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 35788241SJeff.Bonwick@Sun.COM */ 35798241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 35808241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 35818241SJeff.Bonwick@Sun.COM 35828241SJeff.Bonwick@Sun.COM /* 3583789Sahrens * If replace_done is specified, only remove this device if it's 35842082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 35852082Seschrock * disk can be removed. 3586789Sahrens */ 35872082Seschrock if (replace_done) { 35882082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 35892082Seschrock if (vd->vdev_id != 0) 35902082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35912082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 35922082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 35932082Seschrock } 35942082Seschrock } 35952082Seschrock 35962082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 35974577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 3598789Sahrens 3599789Sahrens /* 36002082Seschrock * Only mirror, replacing, and spare vdevs support detach. 3601789Sahrens */ 3602789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 36032082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 36042082Seschrock pvd->vdev_ops != &vdev_spare_ops) 3605789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3606789Sahrens 3607789Sahrens /* 36088241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 36098241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 3610789Sahrens */ 36118241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 3612789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3613789Sahrens 36148241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 36158241SJeff.Bonwick@Sun.COM 3616789Sahrens /* 36176673Seschrock * If we are detaching the second disk from a replacing vdev, then 36186673Seschrock * check to see if we changed the original vdev's path to have "/old" 36196673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 36206673Seschrock */ 36216673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 36226673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 36236673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 36246673Seschrock ASSERT(pvd->vdev_child[1] == vd); 36256673Seschrock cvd = pvd->vdev_child[0]; 36266673Seschrock len = strlen(vd->vdev_path); 36276673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 36286673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 36296673Seschrock spa_strfree(cvd->vdev_path); 36306673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 36316673Seschrock } 36326673Seschrock } 36336673Seschrock 36346673Seschrock /* 36352082Seschrock * If we are detaching the original disk from a spare, then it implies 36362082Seschrock * that the spare should become a real disk, and be removed from the 36372082Seschrock * active spare list for the pool. 36382082Seschrock */ 36392082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 36408241SJeff.Bonwick@Sun.COM vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 36412082Seschrock unspare = B_TRUE; 36422082Seschrock 36432082Seschrock /* 3644789Sahrens * Erase the disk labels so the disk can be used for other things. 3645789Sahrens * This must be done after all other error cases are handled, 3646789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3647789Sahrens * But if we can't do it, don't treat the error as fatal -- 3648789Sahrens * it may be that the unwritability of the disk is the reason 3649789Sahrens * it's being detached! 3650789Sahrens */ 36513377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3652789Sahrens 3653789Sahrens /* 3654789Sahrens * Remove vd from its parent and compact the parent's children. 3655789Sahrens */ 3656789Sahrens vdev_remove_child(pvd, vd); 3657789Sahrens vdev_compact_children(pvd); 3658789Sahrens 3659789Sahrens /* 3660789Sahrens * Remember one of the remaining children so we can get tvd below. 3661789Sahrens */ 3662789Sahrens cvd = pvd->vdev_child[0]; 3663789Sahrens 3664789Sahrens /* 36652082Seschrock * If we need to remove the remaining child from the list of hot spares, 36668241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 36678241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 36688241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 36698241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 36708241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 36718241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 36722082Seschrock */ 36732082Seschrock if (unspare) { 36742082Seschrock ASSERT(cvd->vdev_isspare); 36753377Seschrock spa_spare_remove(cvd); 36762082Seschrock unspare_guid = cvd->vdev_guid; 36778241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 36782082Seschrock } 36792082Seschrock 36802082Seschrock /* 3681789Sahrens * If the parent mirror/replacing vdev only has one child, 3682789Sahrens * the parent is no longer needed. Remove it from the tree. 3683789Sahrens */ 3684789Sahrens if (pvd->vdev_children == 1) 3685789Sahrens vdev_remove_parent(cvd); 3686789Sahrens 3687789Sahrens /* 3688789Sahrens * We don't set tvd until now because the parent we just removed 3689789Sahrens * may have been the previous top-level vdev. 3690789Sahrens */ 3691789Sahrens tvd = cvd->vdev_top; 3692789Sahrens ASSERT(tvd->vdev_parent == rvd); 3693789Sahrens 3694789Sahrens /* 36953377Seschrock * Reevaluate the parent vdev state. 3696789Sahrens */ 36974451Seschrock vdev_propagate_state(cvd); 3698789Sahrens 3699789Sahrens /* 37009816SGeorge.Wilson@Sun.COM * If the 'autoexpand' property is set on the pool then automatically 37019816SGeorge.Wilson@Sun.COM * try to expand the size of the pool. For example if the device we 37029816SGeorge.Wilson@Sun.COM * just detached was smaller than the others, it may be possible to 37039816SGeorge.Wilson@Sun.COM * add metaslabs (i.e. grow the pool). We need to reopen the vdev 37049816SGeorge.Wilson@Sun.COM * first so that we can obtain the updated sizes of the leaf vdevs. 3705789Sahrens */ 37069816SGeorge.Wilson@Sun.COM if (spa->spa_autoexpand) { 37079816SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 37089816SGeorge.Wilson@Sun.COM vdev_expand(tvd, txg); 37099816SGeorge.Wilson@Sun.COM } 3710789Sahrens 3711789Sahrens vdev_config_dirty(tvd); 3712789Sahrens 3713789Sahrens /* 37143377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 37153377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 37163377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 37173377Seschrock * prevent vd from being accessed after it's freed. 3718789Sahrens */ 37198241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 3720789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 37211732Sbonwick vd->vdev_detached = B_TRUE; 37221732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3723789Sahrens 37244451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 37254451Seschrock 37262082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 37272082Seschrock 37282082Seschrock /* 37293377Seschrock * If this was the removal of the original device in a hot spare vdev, 37303377Seschrock * then we want to go through and remove the device from the hot spare 37313377Seschrock * list of every other pool. 37322082Seschrock */ 37332082Seschrock if (unspare) { 37348241SJeff.Bonwick@Sun.COM spa_t *myspa = spa; 37352082Seschrock spa = NULL; 37362082Seschrock mutex_enter(&spa_namespace_lock); 37372082Seschrock while ((spa = spa_next(spa)) != NULL) { 37382082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 37392082Seschrock continue; 37408241SJeff.Bonwick@Sun.COM if (spa == myspa) 37418241SJeff.Bonwick@Sun.COM continue; 37427793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 37437793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 37442082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 37457793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 37467793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 37472082Seschrock } 37482082Seschrock mutex_exit(&spa_namespace_lock); 37492082Seschrock } 37502082Seschrock 37512082Seschrock return (error); 37522082Seschrock } 37532082Seschrock 37547754SJeff.Bonwick@Sun.COM static nvlist_t * 37557754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 37562082Seschrock { 37577754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 37587754SJeff.Bonwick@Sun.COM uint64_t guid; 37597754SJeff.Bonwick@Sun.COM 37607754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 37617754SJeff.Bonwick@Sun.COM &guid) == 0); 37627754SJeff.Bonwick@Sun.COM 37637754SJeff.Bonwick@Sun.COM if (guid == target_guid) 37647754SJeff.Bonwick@Sun.COM return (nvpp[i]); 37652082Seschrock } 37662082Seschrock 37677754SJeff.Bonwick@Sun.COM return (NULL); 37685450Sbrendan } 37695450Sbrendan 37707754SJeff.Bonwick@Sun.COM static void 37717754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 37727754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 37735450Sbrendan { 37747754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 37757754SJeff.Bonwick@Sun.COM 37767754SJeff.Bonwick@Sun.COM if (count > 1) 37777754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 37787754SJeff.Bonwick@Sun.COM 37797754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 37807754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 37817754SJeff.Bonwick@Sun.COM continue; 37827754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 37835450Sbrendan } 37845450Sbrendan 37857754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 37867754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 37877754SJeff.Bonwick@Sun.COM 37887754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 37897754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 37907754SJeff.Bonwick@Sun.COM 37917754SJeff.Bonwick@Sun.COM if (count > 1) 37927754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 37935450Sbrendan } 37945450Sbrendan 37955450Sbrendan /* 379610594SGeorge.Wilson@Sun.COM * Removing a device from the vdev namespace requires several steps 379710594SGeorge.Wilson@Sun.COM * and can take a significant amount of time. As a result we use 379810594SGeorge.Wilson@Sun.COM * the spa_vdev_config_[enter/exit] functions which allow us to 379910594SGeorge.Wilson@Sun.COM * grab and release the spa_config_lock while still holding the namespace 380010594SGeorge.Wilson@Sun.COM * lock. During each step the configuration is synced out. 380110594SGeorge.Wilson@Sun.COM */ 380210594SGeorge.Wilson@Sun.COM 380310594SGeorge.Wilson@Sun.COM /* 380410594SGeorge.Wilson@Sun.COM * Evacuate the device. 380510594SGeorge.Wilson@Sun.COM */ 380610594SGeorge.Wilson@Sun.COM int 380710594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 380810594SGeorge.Wilson@Sun.COM { 380910974SJeff.Bonwick@Sun.COM int error = 0; 381010594SGeorge.Wilson@Sun.COM uint64_t txg; 381110594SGeorge.Wilson@Sun.COM 381210594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 381310594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 381410922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 381510594SGeorge.Wilson@Sun.COM 381610594SGeorge.Wilson@Sun.COM /* 381710594SGeorge.Wilson@Sun.COM * Evacuate the device. We don't hold the config lock as writer 381810594SGeorge.Wilson@Sun.COM * since we need to do I/O but we do keep the 381910594SGeorge.Wilson@Sun.COM * spa_namespace_lock held. Once this completes the device 382010594SGeorge.Wilson@Sun.COM * should no longer have any blocks allocated on it. 382110594SGeorge.Wilson@Sun.COM */ 382210594SGeorge.Wilson@Sun.COM if (vd->vdev_islog) { 382310974SJeff.Bonwick@Sun.COM error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 382410974SJeff.Bonwick@Sun.COM NULL, DS_FIND_CHILDREN); 382510974SJeff.Bonwick@Sun.COM } else { 382610974SJeff.Bonwick@Sun.COM error = ENOTSUP; /* until we have bp rewrite */ 382710594SGeorge.Wilson@Sun.COM } 382810594SGeorge.Wilson@Sun.COM 382910974SJeff.Bonwick@Sun.COM txg_wait_synced(spa_get_dsl(spa), 0); 383010974SJeff.Bonwick@Sun.COM 383110974SJeff.Bonwick@Sun.COM if (error) 383210974SJeff.Bonwick@Sun.COM return (error); 383310974SJeff.Bonwick@Sun.COM 383410594SGeorge.Wilson@Sun.COM /* 383510974SJeff.Bonwick@Sun.COM * The evacuation succeeded. Remove any remaining MOS metadata 383610974SJeff.Bonwick@Sun.COM * associated with this vdev, and wait for these changes to sync. 383710594SGeorge.Wilson@Sun.COM */ 383810594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 383910594SGeorge.Wilson@Sun.COM vd->vdev_removing = B_TRUE; 384010594SGeorge.Wilson@Sun.COM vdev_dirty(vd, 0, NULL, txg); 384110594SGeorge.Wilson@Sun.COM vdev_config_dirty(vd); 384210594SGeorge.Wilson@Sun.COM spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 384310594SGeorge.Wilson@Sun.COM 384410594SGeorge.Wilson@Sun.COM return (0); 384510594SGeorge.Wilson@Sun.COM } 384610594SGeorge.Wilson@Sun.COM 384710594SGeorge.Wilson@Sun.COM /* 384810594SGeorge.Wilson@Sun.COM * Complete the removal by cleaning up the namespace. 384910594SGeorge.Wilson@Sun.COM */ 385010594SGeorge.Wilson@Sun.COM void 385110974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 385210594SGeorge.Wilson@Sun.COM { 385310594SGeorge.Wilson@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 385410594SGeorge.Wilson@Sun.COM uint64_t id = vd->vdev_id; 385510594SGeorge.Wilson@Sun.COM boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 385610594SGeorge.Wilson@Sun.COM 385710594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 385810594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 385910922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 386010594SGeorge.Wilson@Sun.COM 386110594SGeorge.Wilson@Sun.COM (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 386210922SJeff.Bonwick@Sun.COM 386310922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_state_dirty_node)) 386410922SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 386510922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_config_dirty_node)) 386610922SJeff.Bonwick@Sun.COM vdev_config_clean(vd); 386710922SJeff.Bonwick@Sun.COM 386810594SGeorge.Wilson@Sun.COM vdev_free(vd); 386910594SGeorge.Wilson@Sun.COM 387010594SGeorge.Wilson@Sun.COM if (last_vdev) { 387110594SGeorge.Wilson@Sun.COM vdev_compact_children(rvd); 387210594SGeorge.Wilson@Sun.COM } else { 387310594SGeorge.Wilson@Sun.COM vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 387410594SGeorge.Wilson@Sun.COM vdev_add_child(rvd, vd); 387510594SGeorge.Wilson@Sun.COM } 387610594SGeorge.Wilson@Sun.COM vdev_config_dirty(rvd); 387710594SGeorge.Wilson@Sun.COM 387810594SGeorge.Wilson@Sun.COM /* 387910594SGeorge.Wilson@Sun.COM * Reassess the health of our root vdev. 388010594SGeorge.Wilson@Sun.COM */ 388110594SGeorge.Wilson@Sun.COM vdev_reopen(rvd); 388210594SGeorge.Wilson@Sun.COM } 388310594SGeorge.Wilson@Sun.COM 388410594SGeorge.Wilson@Sun.COM /* 38855450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 388610594SGeorge.Wilson@Sun.COM * spares, slogs, and level 2 ARC devices. 38875450Sbrendan */ 38885450Sbrendan int 38895450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 38905450Sbrendan { 38915450Sbrendan vdev_t *vd; 389210974SJeff.Bonwick@Sun.COM metaslab_group_t *mg; 38937754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 389410594SGeorge.Wilson@Sun.COM uint64_t txg = 0; 38955450Sbrendan uint_t nspares, nl2cache; 38965450Sbrendan int error = 0; 38978241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 38988241SJeff.Bonwick@Sun.COM 38998241SJeff.Bonwick@Sun.COM if (!locked) 39008241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 39015450Sbrendan 39026643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 39035450Sbrendan 39045450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 39055450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 39067754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 39077754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 39087754SJeff.Bonwick@Sun.COM /* 39097754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 39107754SJeff.Bonwick@Sun.COM * in this pool. 39117754SJeff.Bonwick@Sun.COM */ 39127754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 39137754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 39147754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 39157754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 39167754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 39177754SJeff.Bonwick@Sun.COM } else { 39187754SJeff.Bonwick@Sun.COM error = EBUSY; 39197754SJeff.Bonwick@Sun.COM } 39207754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 39215450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 39227754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 39237754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 39247754SJeff.Bonwick@Sun.COM /* 39257754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 39267754SJeff.Bonwick@Sun.COM */ 39277754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 39287754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 39295450Sbrendan spa_load_l2cache(spa); 39305450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 393110594SGeorge.Wilson@Sun.COM } else if (vd != NULL && vd->vdev_islog) { 393210594SGeorge.Wilson@Sun.COM ASSERT(!locked); 393310922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 393410594SGeorge.Wilson@Sun.COM 393510594SGeorge.Wilson@Sun.COM /* 393610594SGeorge.Wilson@Sun.COM * XXX - Once we have bp-rewrite this should 393710594SGeorge.Wilson@Sun.COM * become the common case. 393810594SGeorge.Wilson@Sun.COM */ 393910594SGeorge.Wilson@Sun.COM 394010974SJeff.Bonwick@Sun.COM mg = vd->vdev_mg; 394110974SJeff.Bonwick@Sun.COM 394210594SGeorge.Wilson@Sun.COM /* 394310974SJeff.Bonwick@Sun.COM * Stop allocating from this vdev. 394410594SGeorge.Wilson@Sun.COM */ 394510974SJeff.Bonwick@Sun.COM metaslab_group_passivate(mg); 394610594SGeorge.Wilson@Sun.COM 394710922SJeff.Bonwick@Sun.COM /* 394810922SJeff.Bonwick@Sun.COM * Wait for the youngest allocations and frees to sync, 394910922SJeff.Bonwick@Sun.COM * and then wait for the deferral of those frees to finish. 395010922SJeff.Bonwick@Sun.COM */ 395110922SJeff.Bonwick@Sun.COM spa_vdev_config_exit(spa, NULL, 395210922SJeff.Bonwick@Sun.COM txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 395310922SJeff.Bonwick@Sun.COM 395410974SJeff.Bonwick@Sun.COM /* 395510974SJeff.Bonwick@Sun.COM * Attempt to evacuate the vdev. 395610974SJeff.Bonwick@Sun.COM */ 395710974SJeff.Bonwick@Sun.COM error = spa_vdev_remove_evacuate(spa, vd); 395810974SJeff.Bonwick@Sun.COM 395910594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 396010594SGeorge.Wilson@Sun.COM 396110974SJeff.Bonwick@Sun.COM /* 396210974SJeff.Bonwick@Sun.COM * If we couldn't evacuate the vdev, unwind. 396310974SJeff.Bonwick@Sun.COM */ 396410974SJeff.Bonwick@Sun.COM if (error) { 396510974SJeff.Bonwick@Sun.COM metaslab_group_activate(mg); 396610974SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 396710974SJeff.Bonwick@Sun.COM } 396810974SJeff.Bonwick@Sun.COM 396910974SJeff.Bonwick@Sun.COM /* 397010974SJeff.Bonwick@Sun.COM * Clean up the vdev namespace. 397110974SJeff.Bonwick@Sun.COM */ 397210974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa, vd); 397310594SGeorge.Wilson@Sun.COM 39747754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 39757754SJeff.Bonwick@Sun.COM /* 39767754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 39777754SJeff.Bonwick@Sun.COM */ 39787754SJeff.Bonwick@Sun.COM error = ENOTSUP; 39797754SJeff.Bonwick@Sun.COM } else { 39807754SJeff.Bonwick@Sun.COM /* 39817754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 39827754SJeff.Bonwick@Sun.COM */ 39837754SJeff.Bonwick@Sun.COM error = ENOENT; 39845450Sbrendan } 39852082Seschrock 39868241SJeff.Bonwick@Sun.COM if (!locked) 39878241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 39888241SJeff.Bonwick@Sun.COM 39898241SJeff.Bonwick@Sun.COM return (error); 3990789Sahrens } 3991789Sahrens 3992789Sahrens /* 39934451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 39944451Seschrock * current spared, so we can detach it. 3995789Sahrens */ 39961544Seschrock static vdev_t * 39974451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3998789Sahrens { 39991544Seschrock vdev_t *newvd, *oldvd; 40009816SGeorge.Wilson@Sun.COM 40019816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 40024451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 40031544Seschrock if (oldvd != NULL) 40041544Seschrock return (oldvd); 40051544Seschrock } 4006789Sahrens 40074451Seschrock /* 40084451Seschrock * Check for a completed replacement. 40094451Seschrock */ 4010789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 40111544Seschrock oldvd = vd->vdev_child[0]; 40121544Seschrock newvd = vd->vdev_child[1]; 4013789Sahrens 40148241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 40158241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 40161544Seschrock return (oldvd); 40171544Seschrock } 4018789Sahrens 40194451Seschrock /* 40204451Seschrock * Check for a completed resilver with the 'unspare' flag set. 40214451Seschrock */ 40224451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 40234451Seschrock newvd = vd->vdev_child[0]; 40244451Seschrock oldvd = vd->vdev_child[1]; 40254451Seschrock 40264451Seschrock if (newvd->vdev_unspare && 40278241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 40288241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) { 40294451Seschrock newvd->vdev_unspare = 0; 40304451Seschrock return (oldvd); 40314451Seschrock } 40324451Seschrock } 40334451Seschrock 40341544Seschrock return (NULL); 4035789Sahrens } 4036789Sahrens 40371544Seschrock static void 40384451Seschrock spa_vdev_resilver_done(spa_t *spa) 4039789Sahrens { 40408241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 40418241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 40428241SJeff.Bonwick@Sun.COM 40438241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4044789Sahrens 40454451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 40468241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 40478241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 40481544Seschrock guid = vd->vdev_guid; 40498241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 40508241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 40518241SJeff.Bonwick@Sun.COM sguid = 0; 40522082Seschrock /* 40532082Seschrock * If we have just finished replacing a hot spared device, then 40542082Seschrock * we need to detach the parent's first child (the original hot 40552082Seschrock * spare) as well. 40562082Seschrock */ 40578241SJeff.Bonwick@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 40582082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 40598241SJeff.Bonwick@Sun.COM ASSERT(ppvd->vdev_children == 2); 40608241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 40612082Seschrock } 40628241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 40638241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 40641544Seschrock return; 40658241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 40662082Seschrock return; 40678241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4068789Sahrens } 4069789Sahrens 40708241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 4071789Sahrens } 4072789Sahrens 4073789Sahrens /* 407411041SEric.Taylor@Sun.COM * Update the stored path or FRU for this vdev. 40751354Seschrock */ 40761354Seschrock int 40779425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 40789425SEric.Schrock@Sun.COM boolean_t ispath) 40791354Seschrock { 40806643Seschrock vdev_t *vd; 408111041SEric.Taylor@Sun.COM 408211041SEric.Taylor@Sun.COM spa_vdev_state_enter(spa, SCL_ALL); 40831354Seschrock 40849425SEric.Schrock@Sun.COM if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 408511041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOENT)); 40861354Seschrock 40871585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 408811041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 40891585Sbonwick 40909425SEric.Schrock@Sun.COM if (ispath) { 40919425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_path); 40929425SEric.Schrock@Sun.COM vd->vdev_path = spa_strdup(value); 40939425SEric.Schrock@Sun.COM } else { 40949425SEric.Schrock@Sun.COM if (vd->vdev_fru != NULL) 40959425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 40969425SEric.Schrock@Sun.COM vd->vdev_fru = spa_strdup(value); 40979425SEric.Schrock@Sun.COM } 40981354Seschrock 409911041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 41001354Seschrock } 41011354Seschrock 41029425SEric.Schrock@Sun.COM int 41039425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 41049425SEric.Schrock@Sun.COM { 41059425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 41069425SEric.Schrock@Sun.COM } 41079425SEric.Schrock@Sun.COM 41089425SEric.Schrock@Sun.COM int 41099425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 41109425SEric.Schrock@Sun.COM { 41119425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 41129425SEric.Schrock@Sun.COM } 41139425SEric.Schrock@Sun.COM 41141354Seschrock /* 4115789Sahrens * ========================================================================== 4116789Sahrens * SPA Scrubbing 4117789Sahrens * ========================================================================== 4118789Sahrens */ 4119789Sahrens 41207046Sahrens int 41217046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 4122789Sahrens { 41237754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 41244808Sek110237 4125789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 4126789Sahrens return (ENOTSUP); 4127789Sahrens 4128789Sahrens /* 41297046Sahrens * If a resilver was requested, but there is no DTL on a 41307046Sahrens * writeable leaf device, we have nothing to do. 4131789Sahrens */ 41327046Sahrens if (type == POOL_SCRUB_RESILVER && 41337046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 41347046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 41351544Seschrock return (0); 41361544Seschrock } 4137789Sahrens 41387046Sahrens if (type == POOL_SCRUB_EVERYTHING && 41397046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 41407046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 41417046Sahrens return (EBUSY); 41427046Sahrens 41437046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 41447046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 41457046Sahrens } else if (type == POOL_SCRUB_NONE) { 41467046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 41471544Seschrock } else { 41487046Sahrens return (EINVAL); 41491544Seschrock } 4150789Sahrens } 4151789Sahrens 41521544Seschrock /* 41531544Seschrock * ========================================================================== 41541544Seschrock * SPA async task processing 41551544Seschrock * ========================================================================== 41561544Seschrock */ 41571544Seschrock 41581544Seschrock static void 41594451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 4160789Sahrens { 41617361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 41627361SBrendan.Gregg@Sun.COM vd->vdev_remove_wanted = 0; 41637361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 416410575SEric.Schrock@Sun.COM 416510575SEric.Schrock@Sun.COM /* 416610575SEric.Schrock@Sun.COM * We want to clear the stats, but we don't want to do a full 416710575SEric.Schrock@Sun.COM * vdev_clear() as that will cause us to throw away 416810575SEric.Schrock@Sun.COM * degraded/faulted state as well as attempt to reopen the 416910575SEric.Schrock@Sun.COM * device, all of which is a waste. 417010575SEric.Schrock@Sun.COM */ 417110575SEric.Schrock@Sun.COM vd->vdev_stat.vs_read_errors = 0; 417210575SEric.Schrock@Sun.COM vd->vdev_stat.vs_write_errors = 0; 417310575SEric.Schrock@Sun.COM vd->vdev_stat.vs_checksum_errors = 0; 417410575SEric.Schrock@Sun.COM 41757754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 41761544Seschrock } 41777361SBrendan.Gregg@Sun.COM 41787754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 41797361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 41801544Seschrock } 41811544Seschrock 41821544Seschrock static void 41837754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 41847754SJeff.Bonwick@Sun.COM { 41857754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 41867754SJeff.Bonwick@Sun.COM vd->vdev_probe_wanted = 0; 41877754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 41887754SJeff.Bonwick@Sun.COM } 41897754SJeff.Bonwick@Sun.COM 41907754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 41917754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 41927754SJeff.Bonwick@Sun.COM } 41937754SJeff.Bonwick@Sun.COM 41947754SJeff.Bonwick@Sun.COM static void 41959816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd) 41969816SGeorge.Wilson@Sun.COM { 41979816SGeorge.Wilson@Sun.COM sysevent_id_t eid; 41989816SGeorge.Wilson@Sun.COM nvlist_t *attr; 41999816SGeorge.Wilson@Sun.COM char *physpath; 42009816SGeorge.Wilson@Sun.COM 42019816SGeorge.Wilson@Sun.COM if (!spa->spa_autoexpand) 42029816SGeorge.Wilson@Sun.COM return; 42039816SGeorge.Wilson@Sun.COM 42049816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 42059816SGeorge.Wilson@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 42069816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, cvd); 42079816SGeorge.Wilson@Sun.COM } 42089816SGeorge.Wilson@Sun.COM 42099816SGeorge.Wilson@Sun.COM if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 42109816SGeorge.Wilson@Sun.COM return; 42119816SGeorge.Wilson@Sun.COM 42129816SGeorge.Wilson@Sun.COM physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 42139816SGeorge.Wilson@Sun.COM (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 42149816SGeorge.Wilson@Sun.COM 42159816SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 42169816SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 42179816SGeorge.Wilson@Sun.COM 42189816SGeorge.Wilson@Sun.COM (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 42199816SGeorge.Wilson@Sun.COM ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 42209816SGeorge.Wilson@Sun.COM 42219816SGeorge.Wilson@Sun.COM nvlist_free(attr); 42229816SGeorge.Wilson@Sun.COM kmem_free(physpath, MAXPATHLEN); 42239816SGeorge.Wilson@Sun.COM } 42249816SGeorge.Wilson@Sun.COM 42259816SGeorge.Wilson@Sun.COM static void 42261544Seschrock spa_async_thread(spa_t *spa) 42271544Seschrock { 42287754SJeff.Bonwick@Sun.COM int tasks; 42291544Seschrock 42301544Seschrock ASSERT(spa->spa_sync_on); 4231789Sahrens 42321544Seschrock mutex_enter(&spa->spa_async_lock); 42331544Seschrock tasks = spa->spa_async_tasks; 42341544Seschrock spa->spa_async_tasks = 0; 42351544Seschrock mutex_exit(&spa->spa_async_lock); 42361544Seschrock 42371544Seschrock /* 42381635Sbonwick * See if the config needs to be updated. 42391635Sbonwick */ 42401635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 424110922SJeff.Bonwick@Sun.COM uint64_t old_space, new_space; 42429816SGeorge.Wilson@Sun.COM 42431635Sbonwick mutex_enter(&spa_namespace_lock); 424410922SJeff.Bonwick@Sun.COM old_space = metaslab_class_get_space(spa_normal_class(spa)); 42451635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 424610922SJeff.Bonwick@Sun.COM new_space = metaslab_class_get_space(spa_normal_class(spa)); 42471635Sbonwick mutex_exit(&spa_namespace_lock); 42489816SGeorge.Wilson@Sun.COM 42499816SGeorge.Wilson@Sun.COM /* 42509816SGeorge.Wilson@Sun.COM * If the pool grew as a result of the config update, 42519816SGeorge.Wilson@Sun.COM * then log an internal history event. 42529816SGeorge.Wilson@Sun.COM */ 425310922SJeff.Bonwick@Sun.COM if (new_space != old_space) { 42549946SMark.Musante@Sun.COM spa_history_internal_log(LOG_POOL_VDEV_ONLINE, 42559946SMark.Musante@Sun.COM spa, NULL, CRED(), 42569946SMark.Musante@Sun.COM "pool '%s' size: %llu(+%llu)", 425710922SJeff.Bonwick@Sun.COM spa_name(spa), new_space, new_space - old_space); 42589816SGeorge.Wilson@Sun.COM } 42591635Sbonwick } 42601635Sbonwick 42611635Sbonwick /* 42624451Seschrock * See if any devices need to be marked REMOVED. 42631544Seschrock */ 42647754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 426510685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 42664451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 42677754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 42687361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 42697754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 42707361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 42717754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 42727754SJeff.Bonwick@Sun.COM } 42737754SJeff.Bonwick@Sun.COM 42749816SGeorge.Wilson@Sun.COM if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 42759816SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 42769816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, spa->spa_root_vdev); 42779816SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 42789816SGeorge.Wilson@Sun.COM } 42799816SGeorge.Wilson@Sun.COM 42807754SJeff.Bonwick@Sun.COM /* 42817754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 42827754SJeff.Bonwick@Sun.COM */ 42837754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 428410685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 42857754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 42867754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 42874451Seschrock } 42881544Seschrock 42891544Seschrock /* 42901544Seschrock * If any devices are done replacing, detach them. 42911544Seschrock */ 42924451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 42934451Seschrock spa_vdev_resilver_done(spa); 4294789Sahrens 42951544Seschrock /* 42961544Seschrock * Kick off a resilver. 42971544Seschrock */ 42987046Sahrens if (tasks & SPA_ASYNC_RESILVER) 42997046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 43001544Seschrock 43011544Seschrock /* 43021544Seschrock * Let the world know that we're done. 43031544Seschrock */ 43041544Seschrock mutex_enter(&spa->spa_async_lock); 43051544Seschrock spa->spa_async_thread = NULL; 43061544Seschrock cv_broadcast(&spa->spa_async_cv); 43071544Seschrock mutex_exit(&spa->spa_async_lock); 43081544Seschrock thread_exit(); 43091544Seschrock } 43101544Seschrock 43111544Seschrock void 43121544Seschrock spa_async_suspend(spa_t *spa) 43131544Seschrock { 43141544Seschrock mutex_enter(&spa->spa_async_lock); 43151544Seschrock spa->spa_async_suspended++; 43161544Seschrock while (spa->spa_async_thread != NULL) 43171544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 43181544Seschrock mutex_exit(&spa->spa_async_lock); 43191544Seschrock } 43201544Seschrock 43211544Seschrock void 43221544Seschrock spa_async_resume(spa_t *spa) 43231544Seschrock { 43241544Seschrock mutex_enter(&spa->spa_async_lock); 43251544Seschrock ASSERT(spa->spa_async_suspended != 0); 43261544Seschrock spa->spa_async_suspended--; 43271544Seschrock mutex_exit(&spa->spa_async_lock); 43281544Seschrock } 43291544Seschrock 43301544Seschrock static void 43311544Seschrock spa_async_dispatch(spa_t *spa) 43321544Seschrock { 43331544Seschrock mutex_enter(&spa->spa_async_lock); 43341544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 43351635Sbonwick spa->spa_async_thread == NULL && 43361635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 43371544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 43381544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 43391544Seschrock mutex_exit(&spa->spa_async_lock); 43401544Seschrock } 43411544Seschrock 43421544Seschrock void 43431544Seschrock spa_async_request(spa_t *spa, int task) 43441544Seschrock { 43451544Seschrock mutex_enter(&spa->spa_async_lock); 43461544Seschrock spa->spa_async_tasks |= task; 43471544Seschrock mutex_exit(&spa->spa_async_lock); 4348789Sahrens } 4349789Sahrens 4350789Sahrens /* 4351789Sahrens * ========================================================================== 4352789Sahrens * SPA syncing routines 4353789Sahrens * ========================================================================== 4354789Sahrens */ 4355789Sahrens static void 435610922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg) 4357789Sahrens { 4358789Sahrens blkptr_t blk; 4359789Sahrens uint64_t itor = 0; 4360789Sahrens uint8_t c = 1; 4361789Sahrens 43627754SJeff.Bonwick@Sun.COM while (bplist_iterate(bpl, &itor, &blk) == 0) { 43637754SJeff.Bonwick@Sun.COM ASSERT(blk.blk_birth < txg); 436410922SJeff.Bonwick@Sun.COM zio_free(spa, txg, &blk); 43657754SJeff.Bonwick@Sun.COM } 4366789Sahrens 4367789Sahrens bplist_vacate(bpl, tx); 4368789Sahrens 4369789Sahrens /* 4370789Sahrens * Pre-dirty the first block so we sync to convergence faster. 4371789Sahrens * (Usually only the first block is needed.) 4372789Sahrens */ 437310922SJeff.Bonwick@Sun.COM dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx); 437410922SJeff.Bonwick@Sun.COM } 437510922SJeff.Bonwick@Sun.COM 437610922SJeff.Bonwick@Sun.COM static void 437710922SJeff.Bonwick@Sun.COM spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 437810922SJeff.Bonwick@Sun.COM { 437910922SJeff.Bonwick@Sun.COM zio_t *zio = arg; 438010922SJeff.Bonwick@Sun.COM 438110922SJeff.Bonwick@Sun.COM zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 438210922SJeff.Bonwick@Sun.COM zio->io_flags)); 4383789Sahrens } 4384789Sahrens 4385789Sahrens static void 43862082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 43872082Seschrock { 43882082Seschrock char *packed = NULL; 43897497STim.Haley@Sun.COM size_t bufsize; 43902082Seschrock size_t nvsize = 0; 43912082Seschrock dmu_buf_t *db; 43922082Seschrock 43932082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 43942082Seschrock 43957497STim.Haley@Sun.COM /* 43967497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 43977497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 43987497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 43997497STim.Haley@Sun.COM */ 44007497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 44017497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 44022082Seschrock 44032082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 44042082Seschrock KM_SLEEP) == 0); 44057497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 44067497STim.Haley@Sun.COM 44077497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 44087497STim.Haley@Sun.COM 44097497STim.Haley@Sun.COM kmem_free(packed, bufsize); 44102082Seschrock 44112082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 44122082Seschrock dmu_buf_will_dirty(db, tx); 44132082Seschrock *(uint64_t *)db->db_data = nvsize; 44142082Seschrock dmu_buf_rele(db, FTAG); 44152082Seschrock } 44162082Seschrock 44172082Seschrock static void 44185450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 44195450Sbrendan const char *config, const char *entry) 44202082Seschrock { 44212082Seschrock nvlist_t *nvroot; 44225450Sbrendan nvlist_t **list; 44232082Seschrock int i; 44242082Seschrock 44255450Sbrendan if (!sav->sav_sync) 44262082Seschrock return; 44272082Seschrock 44282082Seschrock /* 44295450Sbrendan * Update the MOS nvlist describing the list of available devices. 44305450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 44314451Seschrock * valid and the vdevs are labeled appropriately. 44322082Seschrock */ 44335450Sbrendan if (sav->sav_object == 0) { 44345450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 44355450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 44365450Sbrendan sizeof (uint64_t), tx); 44372082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 44385450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 44395450Sbrendan &sav->sav_object, tx) == 0); 44402082Seschrock } 44412082Seschrock 44422082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 44435450Sbrendan if (sav->sav_count == 0) { 44445450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 44452082Seschrock } else { 44465450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 44475450Sbrendan for (i = 0; i < sav->sav_count; i++) 44485450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 44495450Sbrendan B_FALSE, B_FALSE, B_TRUE); 44505450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 44515450Sbrendan sav->sav_count) == 0); 44525450Sbrendan for (i = 0; i < sav->sav_count; i++) 44535450Sbrendan nvlist_free(list[i]); 44545450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 44552082Seschrock } 44562082Seschrock 44575450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 44582926Sek110237 nvlist_free(nvroot); 44592082Seschrock 44605450Sbrendan sav->sav_sync = B_FALSE; 44612082Seschrock } 44622082Seschrock 44632082Seschrock static void 4464789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 4465789Sahrens { 4466789Sahrens nvlist_t *config; 4467789Sahrens 44687754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 4469789Sahrens return; 4470789Sahrens 44717754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 44727754SJeff.Bonwick@Sun.COM 44737754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 44747754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 44757754SJeff.Bonwick@Sun.COM 44767754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 4477789Sahrens 44781635Sbonwick if (spa->spa_config_syncing) 44791635Sbonwick nvlist_free(spa->spa_config_syncing); 44801635Sbonwick spa->spa_config_syncing = config; 4481789Sahrens 44822082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 4483789Sahrens } 4484789Sahrens 44855094Slling /* 44865094Slling * Set zpool properties. 44875094Slling */ 44883912Slling static void 44894543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 44903912Slling { 44913912Slling spa_t *spa = arg1; 44925094Slling objset_t *mos = spa->spa_meta_objset; 44933912Slling nvlist_t *nvp = arg2; 44945094Slling nvpair_t *elem; 44954451Seschrock uint64_t intval; 44966643Seschrock char *strval; 44975094Slling zpool_prop_t prop; 44985094Slling const char *propname; 44995094Slling zprop_type_t proptype; 45005094Slling 45017754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 45027754SJeff.Bonwick@Sun.COM 45035094Slling elem = NULL; 45045094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 45055094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 45065094Slling case ZPOOL_PROP_VERSION: 45075094Slling /* 45085094Slling * Only set version for non-zpool-creation cases 45095094Slling * (set/import). spa_create() needs special care 45105094Slling * for version setting. 45115094Slling */ 45125094Slling if (tx->tx_txg != TXG_INITIAL) { 45135094Slling VERIFY(nvpair_value_uint64(elem, 45145094Slling &intval) == 0); 45155094Slling ASSERT(intval <= SPA_VERSION); 45165094Slling ASSERT(intval >= spa_version(spa)); 45175094Slling spa->spa_uberblock.ub_version = intval; 45185094Slling vdev_config_dirty(spa->spa_root_vdev); 45195094Slling } 45205094Slling break; 45215094Slling 45225094Slling case ZPOOL_PROP_ALTROOT: 45235094Slling /* 45245094Slling * 'altroot' is a non-persistent property. It should 45255094Slling * have been set temporarily at creation or import time. 45265094Slling */ 45275094Slling ASSERT(spa->spa_root != NULL); 45285094Slling break; 45295094Slling 45305363Seschrock case ZPOOL_PROP_CACHEFILE: 45315094Slling /* 45328525SEric.Schrock@Sun.COM * 'cachefile' is also a non-persisitent property. 45335094Slling */ 45344543Smarks break; 45355094Slling default: 45365094Slling /* 45375094Slling * Set pool property values in the poolprops mos object. 45385094Slling */ 45395094Slling if (spa->spa_pool_props_object == 0) { 45405094Slling VERIFY((spa->spa_pool_props_object = 45415094Slling zap_create(mos, DMU_OT_POOL_PROPS, 45425094Slling DMU_OT_NONE, 0, tx)) > 0); 45435094Slling 45445094Slling VERIFY(zap_update(mos, 45455094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 45465094Slling 8, 1, &spa->spa_pool_props_object, tx) 45475094Slling == 0); 45485094Slling } 45495094Slling 45505094Slling /* normalize the property name */ 45515094Slling propname = zpool_prop_to_name(prop); 45525094Slling proptype = zpool_prop_get_type(prop); 45535094Slling 45545094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 45555094Slling ASSERT(proptype == PROP_TYPE_STRING); 45565094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 45575094Slling VERIFY(zap_update(mos, 45585094Slling spa->spa_pool_props_object, propname, 45595094Slling 1, strlen(strval) + 1, strval, tx) == 0); 45605094Slling 45615094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 45625094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 45635094Slling 45645094Slling if (proptype == PROP_TYPE_INDEX) { 45655094Slling const char *unused; 45665094Slling VERIFY(zpool_prop_index_to_string( 45675094Slling prop, intval, &unused) == 0); 45685094Slling } 45695094Slling VERIFY(zap_update(mos, 45705094Slling spa->spa_pool_props_object, propname, 45715094Slling 8, 1, &intval, tx) == 0); 45725094Slling } else { 45735094Slling ASSERT(0); /* not allowed */ 45745094Slling } 45755094Slling 45765329Sgw25295 switch (prop) { 45775329Sgw25295 case ZPOOL_PROP_DELEGATION: 45785094Slling spa->spa_delegation = intval; 45795329Sgw25295 break; 45805329Sgw25295 case ZPOOL_PROP_BOOTFS: 45815094Slling spa->spa_bootfs = intval; 45825329Sgw25295 break; 45835329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 45845329Sgw25295 spa->spa_failmode = intval; 45855329Sgw25295 break; 45869816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 45879816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = intval; 45889816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 45899816SGeorge.Wilson@Sun.COM break; 459010922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 459110922SJeff.Bonwick@Sun.COM spa->spa_dedup_ditto = intval; 459210922SJeff.Bonwick@Sun.COM break; 45935329Sgw25295 default: 45945329Sgw25295 break; 45955329Sgw25295 } 45963912Slling } 45975094Slling 45985094Slling /* log internal history if this is not a zpool create */ 45995094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 46005094Slling tx->tx_txg != TXG_INITIAL) { 46015094Slling spa_history_internal_log(LOG_POOL_PROPSET, 46025094Slling spa, tx, cr, "%s %lld %s", 46037754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 46045094Slling } 46053912Slling } 46067754SJeff.Bonwick@Sun.COM 46077754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 46083912Slling } 46093912Slling 4610789Sahrens /* 4611789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4612789Sahrens * part of the process, so we iterate until it converges. 4613789Sahrens */ 4614789Sahrens void 4615789Sahrens spa_sync(spa_t *spa, uint64_t txg) 4616789Sahrens { 4617789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4618789Sahrens objset_t *mos = spa->spa_meta_objset; 461910922SJeff.Bonwick@Sun.COM bplist_t *defer_bpl = &spa->spa_deferred_bplist; 462010922SJeff.Bonwick@Sun.COM bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 46211635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 4622789Sahrens vdev_t *vd; 4623789Sahrens dmu_tx_t *tx; 46247754SJeff.Bonwick@Sun.COM int error; 4625789Sahrens 4626789Sahrens /* 4627789Sahrens * Lock out configuration changes. 4628789Sahrens */ 46297754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4630789Sahrens 4631789Sahrens spa->spa_syncing_txg = txg; 4632789Sahrens spa->spa_sync_pass = 0; 4633789Sahrens 46347754SJeff.Bonwick@Sun.COM /* 46357754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 46367754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 46377754SJeff.Bonwick@Sun.COM */ 46387754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 46398241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 46408241SJeff.Bonwick@Sun.COM /* 46418241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 46428241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 46438241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 46448241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 46458241SJeff.Bonwick@Sun.COM * into the root vdev tree. 46468241SJeff.Bonwick@Sun.COM */ 46478241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 46488241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 46498241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 46508241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 46518241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 46528241SJeff.Bonwick@Sun.COM } 46538241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 46548241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 46557754SJeff.Bonwick@Sun.COM } 46567754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 46577754SJeff.Bonwick@Sun.COM 465810922SJeff.Bonwick@Sun.COM VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj)); 4659789Sahrens 46602082Seschrock tx = dmu_tx_create_assigned(dp, txg); 46612082Seschrock 46622082Seschrock /* 46634577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 46642082Seschrock * set spa_deflate if we have no raid-z vdevs. 46652082Seschrock */ 46664577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 46674577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 46682082Seschrock int i; 46692082Seschrock 46702082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 46712082Seschrock vd = rvd->vdev_child[i]; 46722082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 46732082Seschrock break; 46742082Seschrock } 46752082Seschrock if (i == rvd->vdev_children) { 46762082Seschrock spa->spa_deflate = TRUE; 46772082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 46782082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 46792082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 46802082Seschrock } 46812082Seschrock } 46822082Seschrock 46837046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 46847046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 46857046Sahrens dsl_pool_create_origin(dp, tx); 46867046Sahrens 46877046Sahrens /* Keeping the origin open increases spa_minref */ 46887046Sahrens spa->spa_minref += 3; 46897046Sahrens } 46907046Sahrens 46917046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 46927046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 46937046Sahrens dsl_pool_upgrade_clones(dp, tx); 46947046Sahrens } 46957046Sahrens 4696789Sahrens /* 4697789Sahrens * If anything has changed in this txg, push the deferred frees 4698789Sahrens * from the previous txg. If not, leave them alone so that we 4699789Sahrens * don't generate work on an otherwise idle system. 4700789Sahrens */ 4701789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 47022329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 47032329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 470410922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa, defer_bpl, tx, txg); 4705789Sahrens 4706789Sahrens /* 4707789Sahrens * Iterate to convergence. 4708789Sahrens */ 4709789Sahrens do { 471010922SJeff.Bonwick@Sun.COM int pass = ++spa->spa_sync_pass; 4711789Sahrens 4712789Sahrens spa_sync_config_object(spa, tx); 47135450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 47145450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 47155450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 47165450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 47171544Seschrock spa_errlog_sync(spa, txg); 4718789Sahrens dsl_pool_sync(dp, txg); 4719789Sahrens 472010922SJeff.Bonwick@Sun.COM if (pass <= SYNC_PASS_DEFERRED_FREE) { 472110922SJeff.Bonwick@Sun.COM zio_t *zio = zio_root(spa, NULL, NULL, 0); 472210922SJeff.Bonwick@Sun.COM bplist_sync(free_bpl, spa_sync_free, zio, tx); 472310922SJeff.Bonwick@Sun.COM VERIFY(zio_wait(zio) == 0); 472410922SJeff.Bonwick@Sun.COM } else { 472510922SJeff.Bonwick@Sun.COM bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx); 4726789Sahrens } 4727789Sahrens 472810922SJeff.Bonwick@Sun.COM ddt_sync(spa, txg); 472910922SJeff.Bonwick@Sun.COM 473010922SJeff.Bonwick@Sun.COM while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 473110922SJeff.Bonwick@Sun.COM vdev_sync(vd, txg); 473210922SJeff.Bonwick@Sun.COM 473310922SJeff.Bonwick@Sun.COM } while (dmu_objset_is_dirty(mos, txg)); 473410922SJeff.Bonwick@Sun.COM 473510922SJeff.Bonwick@Sun.COM ASSERT(free_bpl->bpl_queue == NULL); 473610922SJeff.Bonwick@Sun.COM 473710922SJeff.Bonwick@Sun.COM bplist_close(defer_bpl); 4738789Sahrens 4739789Sahrens /* 4740789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4741789Sahrens * to commit the transaction group. 47421635Sbonwick * 47435688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 47445688Sbonwick * random top-level vdevs that are known to be visible in the 47457754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 47467754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4747789Sahrens */ 47487754SJeff.Bonwick@Sun.COM for (;;) { 47497754SJeff.Bonwick@Sun.COM /* 47507754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 47517754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 47527754SJeff.Bonwick@Sun.COM */ 47537754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 47547754SJeff.Bonwick@Sun.COM 47557754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 47567754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 47577754SJeff.Bonwick@Sun.COM int svdcount = 0; 47587754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 47597754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 47609816SGeorge.Wilson@Sun.COM 47619816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 47627754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 47637754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 47647754SJeff.Bonwick@Sun.COM continue; 47657754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 47667754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 47677754SJeff.Bonwick@Sun.COM break; 47687754SJeff.Bonwick@Sun.COM } 47699725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 47709725SEric.Schrock@Sun.COM if (error != 0) 47719725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, 47729725SEric.Schrock@Sun.COM B_TRUE); 47737754SJeff.Bonwick@Sun.COM } else { 47747754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 47759725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_FALSE); 47769725SEric.Schrock@Sun.COM if (error != 0) 47779725SEric.Schrock@Sun.COM error = vdev_config_sync(rvd->vdev_child, 47789725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_TRUE); 47791635Sbonwick } 47807754SJeff.Bonwick@Sun.COM 47817754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 47827754SJeff.Bonwick@Sun.COM 47837754SJeff.Bonwick@Sun.COM if (error == 0) 47847754SJeff.Bonwick@Sun.COM break; 47857754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 47867754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 47871635Sbonwick } 47882082Seschrock dmu_tx_commit(tx); 47892082Seschrock 47901635Sbonwick /* 47911635Sbonwick * Clear the dirty config list. 47921635Sbonwick */ 47937754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 47941635Sbonwick vdev_config_clean(vd); 47951635Sbonwick 47961635Sbonwick /* 47971635Sbonwick * Now that the new config has synced transactionally, 47981635Sbonwick * let it become visible to the config cache. 47991635Sbonwick */ 48001635Sbonwick if (spa->spa_config_syncing != NULL) { 48011635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 48021635Sbonwick spa->spa_config_txg = txg; 48031635Sbonwick spa->spa_config_syncing = NULL; 48041635Sbonwick } 4805789Sahrens 4806789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4807789Sahrens 480810922SJeff.Bonwick@Sun.COM dsl_pool_sync_done(dp, txg); 4809789Sahrens 4810789Sahrens /* 4811789Sahrens * Update usable space statistics. 4812789Sahrens */ 4813789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4814789Sahrens vdev_sync_done(vd, txg); 4815789Sahrens 481610956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 481710956SGeorge.Wilson@Sun.COM 4818789Sahrens /* 4819789Sahrens * It had better be the case that we didn't dirty anything 48202082Seschrock * since vdev_config_sync(). 4821789Sahrens */ 4822789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4823789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4824789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 482510922SJeff.Bonwick@Sun.COM ASSERT(defer_bpl->bpl_queue == NULL); 482610922SJeff.Bonwick@Sun.COM ASSERT(free_bpl->bpl_queue == NULL); 482710922SJeff.Bonwick@Sun.COM 482810922SJeff.Bonwick@Sun.COM spa->spa_sync_pass = 0; 4829789Sahrens 48307754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 48311544Seschrock 483210921STim.Haley@Sun.COM spa_handle_ignored_writes(spa); 483310921STim.Haley@Sun.COM 48341544Seschrock /* 48351544Seschrock * If any async tasks have been requested, kick them off. 48361544Seschrock */ 48371544Seschrock spa_async_dispatch(spa); 4838789Sahrens } 4839789Sahrens 4840789Sahrens /* 4841789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4842789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4843789Sahrens * sync. 4844789Sahrens */ 4845789Sahrens void 4846789Sahrens spa_sync_allpools(void) 4847789Sahrens { 4848789Sahrens spa_t *spa = NULL; 4849789Sahrens mutex_enter(&spa_namespace_lock); 4850789Sahrens while ((spa = spa_next(spa)) != NULL) { 48517754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4852789Sahrens continue; 4853789Sahrens spa_open_ref(spa, FTAG); 4854789Sahrens mutex_exit(&spa_namespace_lock); 4855789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4856789Sahrens mutex_enter(&spa_namespace_lock); 4857789Sahrens spa_close(spa, FTAG); 4858789Sahrens } 4859789Sahrens mutex_exit(&spa_namespace_lock); 4860789Sahrens } 4861789Sahrens 4862789Sahrens /* 4863789Sahrens * ========================================================================== 4864789Sahrens * Miscellaneous routines 4865789Sahrens * ========================================================================== 4866789Sahrens */ 4867789Sahrens 4868789Sahrens /* 4869789Sahrens * Remove all pools in the system. 4870789Sahrens */ 4871789Sahrens void 4872789Sahrens spa_evict_all(void) 4873789Sahrens { 4874789Sahrens spa_t *spa; 4875789Sahrens 4876789Sahrens /* 4877789Sahrens * Remove all cached state. All pools should be closed now, 4878789Sahrens * so every spa in the AVL tree should be unreferenced. 4879789Sahrens */ 4880789Sahrens mutex_enter(&spa_namespace_lock); 4881789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4882789Sahrens /* 48831544Seschrock * Stop async tasks. The async thread may need to detach 48841544Seschrock * a device that's been replaced, which requires grabbing 48851544Seschrock * spa_namespace_lock, so we must drop it here. 4886789Sahrens */ 4887789Sahrens spa_open_ref(spa, FTAG); 4888789Sahrens mutex_exit(&spa_namespace_lock); 48891544Seschrock spa_async_suspend(spa); 48904808Sek110237 mutex_enter(&spa_namespace_lock); 4891789Sahrens spa_close(spa, FTAG); 4892789Sahrens 4893789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4894789Sahrens spa_unload(spa); 4895789Sahrens spa_deactivate(spa); 4896789Sahrens } 4897789Sahrens spa_remove(spa); 4898789Sahrens } 4899789Sahrens mutex_exit(&spa_namespace_lock); 4900789Sahrens } 49011544Seschrock 49021544Seschrock vdev_t * 49039425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 49041544Seschrock { 49056643Seschrock vdev_t *vd; 49066643Seschrock int i; 49076643Seschrock 49086643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 49096643Seschrock return (vd); 49106643Seschrock 49119425SEric.Schrock@Sun.COM if (aux) { 49126643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 49136643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 49146643Seschrock if (vd->vdev_guid == guid) 49156643Seschrock return (vd); 49166643Seschrock } 49179425SEric.Schrock@Sun.COM 49189425SEric.Schrock@Sun.COM for (i = 0; i < spa->spa_spares.sav_count; i++) { 49199425SEric.Schrock@Sun.COM vd = spa->spa_spares.sav_vdevs[i]; 49209425SEric.Schrock@Sun.COM if (vd->vdev_guid == guid) 49219425SEric.Schrock@Sun.COM return (vd); 49229425SEric.Schrock@Sun.COM } 49236643Seschrock } 49246643Seschrock 49256643Seschrock return (NULL); 49261544Seschrock } 49271760Seschrock 49281760Seschrock void 49295094Slling spa_upgrade(spa_t *spa, uint64_t version) 49301760Seschrock { 49317754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 49321760Seschrock 49331760Seschrock /* 49341760Seschrock * This should only be called for a non-faulted pool, and since a 49351760Seschrock * future version would result in an unopenable pool, this shouldn't be 49361760Seschrock * possible. 49371760Seschrock */ 49384577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 49395094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 49405094Slling 49415094Slling spa->spa_uberblock.ub_version = version; 49421760Seschrock vdev_config_dirty(spa->spa_root_vdev); 49431760Seschrock 49447754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 49452082Seschrock 49462082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 49471760Seschrock } 49482082Seschrock 49492082Seschrock boolean_t 49502082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 49512082Seschrock { 49522082Seschrock int i; 49533377Seschrock uint64_t spareguid; 49545450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 49555450Sbrendan 49565450Sbrendan for (i = 0; i < sav->sav_count; i++) 49575450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 49582082Seschrock return (B_TRUE); 49592082Seschrock 49605450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 49615450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 49625450Sbrendan &spareguid) == 0 && spareguid == guid) 49633377Seschrock return (B_TRUE); 49643377Seschrock } 49653377Seschrock 49662082Seschrock return (B_FALSE); 49672082Seschrock } 49683912Slling 49694451Seschrock /* 49707214Slling * Check if a pool has an active shared spare device. 49717214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 49727214Slling */ 49737214Slling static boolean_t 49747214Slling spa_has_active_shared_spare(spa_t *spa) 49757214Slling { 49767214Slling int i, refcnt; 49777214Slling uint64_t pool; 49787214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 49797214Slling 49807214Slling for (i = 0; i < sav->sav_count; i++) { 49817214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 49827214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 49837214Slling refcnt > 2) 49847214Slling return (B_TRUE); 49857214Slling } 49867214Slling 49877214Slling return (B_FALSE); 49887214Slling } 49897214Slling 49907214Slling /* 49914451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 49924451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 49934451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 49944451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 49954451Seschrock * or zdb as real changes. 49964451Seschrock */ 49974451Seschrock void 49984451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 49994451Seschrock { 50004451Seschrock #ifdef _KERNEL 50014451Seschrock sysevent_t *ev; 50024451Seschrock sysevent_attr_list_t *attr = NULL; 50034451Seschrock sysevent_value_t value; 50044451Seschrock sysevent_id_t eid; 50054451Seschrock 50064451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 50074451Seschrock SE_SLEEP); 50084451Seschrock 50094451Seschrock value.value_type = SE_DATA_TYPE_STRING; 50104451Seschrock value.value.sv_string = spa_name(spa); 50114451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 50124451Seschrock goto done; 50134451Seschrock 50144451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 50154451Seschrock value.value.sv_uint64 = spa_guid(spa); 50164451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 50174451Seschrock goto done; 50184451Seschrock 50194451Seschrock if (vd) { 50204451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 50214451Seschrock value.value.sv_uint64 = vd->vdev_guid; 50224451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 50234451Seschrock SE_SLEEP) != 0) 50244451Seschrock goto done; 50254451Seschrock 50264451Seschrock if (vd->vdev_path) { 50274451Seschrock value.value_type = SE_DATA_TYPE_STRING; 50284451Seschrock value.value.sv_string = vd->vdev_path; 50294451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 50304451Seschrock &value, SE_SLEEP) != 0) 50314451Seschrock goto done; 50324451Seschrock } 50334451Seschrock } 50344451Seschrock 50355756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 50365756Seschrock goto done; 50375756Seschrock attr = NULL; 50385756Seschrock 50394451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 50404451Seschrock 50414451Seschrock done: 50424451Seschrock if (attr) 50434451Seschrock sysevent_free_attr(attr); 50444451Seschrock sysevent_free(ev); 50454451Seschrock #endif 50464451Seschrock } 5047