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 /* 235756Seschrock * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens /* 30789Sahrens * This file contains all the routines used when modifying on-disk SPA state. 31789Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 32789Sahrens * pool. 33789Sahrens */ 34789Sahrens 35789Sahrens #include <sys/zfs_context.h> 361544Seschrock #include <sys/fm/fs/zfs.h> 37789Sahrens #include <sys/spa_impl.h> 38789Sahrens #include <sys/zio.h> 39789Sahrens #include <sys/zio_checksum.h> 40789Sahrens #include <sys/zio_compress.h> 41789Sahrens #include <sys/dmu.h> 42789Sahrens #include <sys/dmu_tx.h> 43789Sahrens #include <sys/zap.h> 44789Sahrens #include <sys/zil.h> 45789Sahrens #include <sys/vdev_impl.h> 46789Sahrens #include <sys/metaslab.h> 47789Sahrens #include <sys/uberblock_impl.h> 48789Sahrens #include <sys/txg.h> 49789Sahrens #include <sys/avl.h> 50789Sahrens #include <sys/dmu_traverse.h> 513912Slling #include <sys/dmu_objset.h> 52789Sahrens #include <sys/unique.h> 53789Sahrens #include <sys/dsl_pool.h> 543912Slling #include <sys/dsl_dataset.h> 55789Sahrens #include <sys/dsl_dir.h> 56789Sahrens #include <sys/dsl_prop.h> 573912Slling #include <sys/dsl_synctask.h> 58789Sahrens #include <sys/fs/zfs.h> 595450Sbrendan #include <sys/arc.h> 60789Sahrens #include <sys/callb.h> 613975Sek110237 #include <sys/systeminfo.h> 623975Sek110237 #include <sys/sunddi.h> 636423Sgw25295 #include <sys/spa_boot.h> 64789Sahrens 655094Slling #include "zfs_prop.h" 665913Sperrin #include "zfs_comutil.h" 675094Slling 682986Sek110237 int zio_taskq_threads = 8; 692986Sek110237 705094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 71*7214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa); 725094Slling 735094Slling /* 745094Slling * ========================================================================== 755094Slling * SPA properties routines 765094Slling * ========================================================================== 775094Slling */ 785094Slling 795094Slling /* 805094Slling * Add a (source=src, propname=propval) list to an nvlist. 815094Slling */ 825949Slling static void 835094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 845094Slling uint64_t intval, zprop_source_t src) 855094Slling { 865094Slling const char *propname = zpool_prop_to_name(prop); 875094Slling nvlist_t *propval; 885949Slling 895949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 905949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 915949Slling 925949Slling if (strval != NULL) 935949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 945949Slling else 955949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 965949Slling 975949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 985094Slling nvlist_free(propval); 995094Slling } 1005094Slling 1015094Slling /* 1025094Slling * Get property values from the spa configuration. 1035094Slling */ 1045949Slling static void 1055094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1065094Slling { 1075094Slling uint64_t size = spa_get_space(spa); 1085094Slling uint64_t used = spa_get_alloc(spa); 1095094Slling uint64_t cap, version; 1105094Slling zprop_source_t src = ZPROP_SRC_NONE; 1116643Seschrock spa_config_dirent_t *dp; 1125094Slling 1135094Slling /* 1145094Slling * readonly properties 1155094Slling */ 1165949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src); 1175949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 1185949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 1195949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src); 1205094Slling 1215094Slling cap = (size == 0) ? 0 : (used * 100 / size); 1225949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1235949Slling 1245949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1255949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1265949Slling spa->spa_root_vdev->vdev_state, src); 1275094Slling 1285094Slling /* 1295094Slling * settable properties that are not stored in the pool property object. 1305094Slling */ 1315094Slling version = spa_version(spa); 1325094Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1335094Slling src = ZPROP_SRC_DEFAULT; 1345094Slling else 1355094Slling src = ZPROP_SRC_LOCAL; 1365949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1375949Slling 1385949Slling if (spa->spa_root != NULL) 1395949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1405949Slling 0, ZPROP_SRC_LOCAL); 1415094Slling 1426643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 1436643Seschrock if (dp->scd_path == NULL) { 1445949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1456643Seschrock "none", 0, ZPROP_SRC_LOCAL); 1466643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1475949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1486643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1495363Seschrock } 1505363Seschrock } 1515094Slling } 1525094Slling 1535094Slling /* 1545094Slling * Get zpool property values. 1555094Slling */ 1565094Slling int 1575094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 1585094Slling { 1595094Slling zap_cursor_t zc; 1605094Slling zap_attribute_t za; 1615094Slling objset_t *mos = spa->spa_meta_objset; 1625094Slling int err; 1635094Slling 1645949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1655094Slling 1665094Slling /* 1675094Slling * Get properties from the spa config. 1685094Slling */ 1695949Slling spa_prop_get_config(spa, nvp); 1705094Slling 1715094Slling mutex_enter(&spa->spa_props_lock); 1725094Slling /* If no pool property object, no more prop to get. */ 1735094Slling if (spa->spa_pool_props_object == 0) { 1745094Slling mutex_exit(&spa->spa_props_lock); 1755094Slling return (0); 1765094Slling } 1775094Slling 1785094Slling /* 1795094Slling * Get properties from the MOS pool property object. 1805094Slling */ 1815094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 1825094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 1835094Slling zap_cursor_advance(&zc)) { 1845094Slling uint64_t intval = 0; 1855094Slling char *strval = NULL; 1865094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 1875094Slling zpool_prop_t prop; 1885094Slling 1895094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 1905094Slling continue; 1915094Slling 1925094Slling switch (za.za_integer_length) { 1935094Slling case 8: 1945094Slling /* integer property */ 1955094Slling if (za.za_first_integer != 1965094Slling zpool_prop_default_numeric(prop)) 1975094Slling src = ZPROP_SRC_LOCAL; 1985094Slling 1995094Slling if (prop == ZPOOL_PROP_BOOTFS) { 2005094Slling dsl_pool_t *dp; 2015094Slling dsl_dataset_t *ds = NULL; 2025094Slling 2035094Slling dp = spa_get_dsl(spa); 2045094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2056689Smaybee if (err = dsl_dataset_hold_obj(dp, 2066689Smaybee za.za_first_integer, FTAG, &ds)) { 2075094Slling rw_exit(&dp->dp_config_rwlock); 2085094Slling break; 2095094Slling } 2105094Slling 2115094Slling strval = kmem_alloc( 2125094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2135094Slling KM_SLEEP); 2145094Slling dsl_dataset_name(ds, strval); 2156689Smaybee dsl_dataset_rele(ds, FTAG); 2165094Slling rw_exit(&dp->dp_config_rwlock); 2175094Slling } else { 2185094Slling strval = NULL; 2195094Slling intval = za.za_first_integer; 2205094Slling } 2215094Slling 2225949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2235094Slling 2245094Slling if (strval != NULL) 2255094Slling kmem_free(strval, 2265094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2275094Slling 2285094Slling break; 2295094Slling 2305094Slling case 1: 2315094Slling /* string property */ 2325094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2335094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2345094Slling za.za_name, 1, za.za_num_integers, strval); 2355094Slling if (err) { 2365094Slling kmem_free(strval, za.za_num_integers); 2375094Slling break; 2385094Slling } 2395949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2405094Slling kmem_free(strval, za.za_num_integers); 2415094Slling break; 2425094Slling 2435094Slling default: 2445094Slling break; 2455094Slling } 2465094Slling } 2475094Slling zap_cursor_fini(&zc); 2485094Slling mutex_exit(&spa->spa_props_lock); 2495094Slling out: 2505094Slling if (err && err != ENOENT) { 2515094Slling nvlist_free(*nvp); 2525949Slling *nvp = NULL; 2535094Slling return (err); 2545094Slling } 2555094Slling 2565094Slling return (0); 2575094Slling } 2585094Slling 2595094Slling /* 2605094Slling * Validate the given pool properties nvlist and modify the list 2615094Slling * for the property values to be set. 2625094Slling */ 2635094Slling static int 2645094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 2655094Slling { 2665094Slling nvpair_t *elem; 2675094Slling int error = 0, reset_bootfs = 0; 2685094Slling uint64_t objnum; 2695094Slling 2705094Slling elem = NULL; 2715094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2725094Slling zpool_prop_t prop; 2735094Slling char *propname, *strval; 2745094Slling uint64_t intval; 2755094Slling objset_t *os; 2765363Seschrock char *slash; 2775094Slling 2785094Slling propname = nvpair_name(elem); 2795094Slling 2805094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 2815094Slling return (EINVAL); 2825094Slling 2835094Slling switch (prop) { 2845094Slling case ZPOOL_PROP_VERSION: 2855094Slling error = nvpair_value_uint64(elem, &intval); 2865094Slling if (!error && 2875094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 2885094Slling error = EINVAL; 2895094Slling break; 2905094Slling 2915094Slling case ZPOOL_PROP_DELEGATION: 2925094Slling case ZPOOL_PROP_AUTOREPLACE: 2935094Slling error = nvpair_value_uint64(elem, &intval); 2945094Slling if (!error && intval > 1) 2955094Slling error = EINVAL; 2965094Slling break; 2975094Slling 2985094Slling case ZPOOL_PROP_BOOTFS: 2995094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3005094Slling error = ENOTSUP; 3015094Slling break; 3025094Slling } 3035094Slling 3045094Slling /* 3057042Sgw25295 * Make sure the vdev config is bootable 3065094Slling */ 3077042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3085094Slling error = ENOTSUP; 3095094Slling break; 3105094Slling } 3115094Slling 3125094Slling reset_bootfs = 1; 3135094Slling 3145094Slling error = nvpair_value_string(elem, &strval); 3155094Slling 3165094Slling if (!error) { 3177042Sgw25295 uint64_t compress; 3187042Sgw25295 3195094Slling if (strval == NULL || strval[0] == '\0') { 3205094Slling objnum = zpool_prop_default_numeric( 3215094Slling ZPOOL_PROP_BOOTFS); 3225094Slling break; 3235094Slling } 3245094Slling 3255094Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 3266689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os)) 3275094Slling break; 3287042Sgw25295 3297042Sgw25295 /* We don't support gzip bootable datasets */ 3307042Sgw25295 if ((error = dsl_prop_get_integer(strval, 3317042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3327042Sgw25295 &compress, NULL)) == 0 && 3337042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3347042Sgw25295 error = ENOTSUP; 3357042Sgw25295 } else { 3367042Sgw25295 objnum = dmu_objset_id(os); 3377042Sgw25295 } 3385094Slling dmu_objset_close(os); 3395094Slling } 3405094Slling break; 3415329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3425329Sgw25295 error = nvpair_value_uint64(elem, &intval); 3435329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3445329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 3455329Sgw25295 error = EINVAL; 3465329Sgw25295 3475329Sgw25295 /* 3485329Sgw25295 * This is a special case which only occurs when 3495329Sgw25295 * the pool has completely failed. This allows 3505329Sgw25295 * the user to change the in-core failmode property 3515329Sgw25295 * without syncing it out to disk (I/Os might 3525329Sgw25295 * currently be blocked). We do this by returning 3535329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 3545329Sgw25295 * into thinking we encountered a property validation 3555329Sgw25295 * error. 3565329Sgw25295 */ 3575329Sgw25295 if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) { 3585329Sgw25295 spa->spa_failmode = intval; 3595329Sgw25295 error = EIO; 3605329Sgw25295 } 3615329Sgw25295 break; 3625363Seschrock 3635363Seschrock case ZPOOL_PROP_CACHEFILE: 3645363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3655363Seschrock break; 3665363Seschrock 3675363Seschrock if (strval[0] == '\0') 3685363Seschrock break; 3695363Seschrock 3705363Seschrock if (strcmp(strval, "none") == 0) 3715363Seschrock break; 3725363Seschrock 3735363Seschrock if (strval[0] != '/') { 3745363Seschrock error = EINVAL; 3755363Seschrock break; 3765363Seschrock } 3775363Seschrock 3785363Seschrock slash = strrchr(strval, '/'); 3795363Seschrock ASSERT(slash != NULL); 3805363Seschrock 3815363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3825363Seschrock strcmp(slash, "/..") == 0) 3835363Seschrock error = EINVAL; 3845363Seschrock break; 3855094Slling } 3865094Slling 3875094Slling if (error) 3885094Slling break; 3895094Slling } 3905094Slling 3915094Slling if (!error && reset_bootfs) { 3925094Slling error = nvlist_remove(props, 3935094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 3945094Slling 3955094Slling if (!error) { 3965094Slling error = nvlist_add_uint64(props, 3975094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 3985094Slling } 3995094Slling } 4005094Slling 4015094Slling return (error); 4025094Slling } 4035094Slling 4045094Slling int 4055094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4065094Slling { 4075094Slling int error; 4085094Slling 4095094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 4105094Slling return (error); 4115094Slling 4125094Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 4135094Slling spa, nvp, 3)); 4145094Slling } 4155094Slling 4165094Slling /* 4175094Slling * If the bootfs property value is dsobj, clear it. 4185094Slling */ 4195094Slling void 4205094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 4215094Slling { 4225094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 4235094Slling VERIFY(zap_remove(spa->spa_meta_objset, 4245094Slling spa->spa_pool_props_object, 4255094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 4265094Slling spa->spa_bootfs = 0; 4275094Slling } 4285094Slling } 4295094Slling 430789Sahrens /* 431789Sahrens * ========================================================================== 432789Sahrens * SPA state manipulation (open/create/destroy/import/export) 433789Sahrens * ========================================================================== 434789Sahrens */ 435789Sahrens 4361544Seschrock static int 4371544Seschrock spa_error_entry_compare(const void *a, const void *b) 4381544Seschrock { 4391544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 4401544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 4411544Seschrock int ret; 4421544Seschrock 4431544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 4441544Seschrock sizeof (zbookmark_t)); 4451544Seschrock 4461544Seschrock if (ret < 0) 4471544Seschrock return (-1); 4481544Seschrock else if (ret > 0) 4491544Seschrock return (1); 4501544Seschrock else 4511544Seschrock return (0); 4521544Seschrock } 4531544Seschrock 4541544Seschrock /* 4551544Seschrock * Utility function which retrieves copies of the current logs and 4561544Seschrock * re-initializes them in the process. 4571544Seschrock */ 4581544Seschrock void 4591544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 4601544Seschrock { 4611544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 4621544Seschrock 4631544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 4641544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 4651544Seschrock 4661544Seschrock avl_create(&spa->spa_errlist_scrub, 4671544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4681544Seschrock offsetof(spa_error_entry_t, se_avl)); 4691544Seschrock avl_create(&spa->spa_errlist_last, 4701544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4711544Seschrock offsetof(spa_error_entry_t, se_avl)); 4721544Seschrock } 4731544Seschrock 474789Sahrens /* 475789Sahrens * Activate an uninitialized pool. 476789Sahrens */ 477789Sahrens static void 478789Sahrens spa_activate(spa_t *spa) 479789Sahrens { 480789Sahrens int t; 481789Sahrens 482789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 483789Sahrens 484789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 485789Sahrens 486789Sahrens spa->spa_normal_class = metaslab_class_create(); 4874527Sperrin spa->spa_log_class = metaslab_class_create(); 488789Sahrens 489789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 490789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 4912986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 492789Sahrens TASKQ_PREPOPULATE); 493789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 4942986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 495789Sahrens TASKQ_PREPOPULATE); 496789Sahrens } 497789Sahrens 498789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 499789Sahrens offsetof(vdev_t, vdev_dirty_node)); 5005329Sgw25295 list_create(&spa->spa_zio_list, sizeof (zio_t), 5015329Sgw25295 offsetof(zio_t, zio_link_node)); 502789Sahrens 503789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 504789Sahrens offsetof(struct vdev, vdev_txg_node)); 5051544Seschrock 5061544Seschrock avl_create(&spa->spa_errlist_scrub, 5071544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5081544Seschrock offsetof(spa_error_entry_t, se_avl)); 5091544Seschrock avl_create(&spa->spa_errlist_last, 5101544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5111544Seschrock offsetof(spa_error_entry_t, se_avl)); 512789Sahrens } 513789Sahrens 514789Sahrens /* 515789Sahrens * Opposite of spa_activate(). 516789Sahrens */ 517789Sahrens static void 518789Sahrens spa_deactivate(spa_t *spa) 519789Sahrens { 520789Sahrens int t; 521789Sahrens 522789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 523789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 524789Sahrens ASSERT(spa->spa_root_vdev == NULL); 525789Sahrens 526789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 527789Sahrens 528789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 529789Sahrens 530789Sahrens list_destroy(&spa->spa_dirty_list); 5315329Sgw25295 list_destroy(&spa->spa_zio_list); 532789Sahrens 533789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 534789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 535789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 536789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 537789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 538789Sahrens } 539789Sahrens 540789Sahrens metaslab_class_destroy(spa->spa_normal_class); 541789Sahrens spa->spa_normal_class = NULL; 542789Sahrens 5434527Sperrin metaslab_class_destroy(spa->spa_log_class); 5444527Sperrin spa->spa_log_class = NULL; 5454527Sperrin 5461544Seschrock /* 5471544Seschrock * If this was part of an import or the open otherwise failed, we may 5481544Seschrock * still have errors left in the queues. Empty them just in case. 5491544Seschrock */ 5501544Seschrock spa_errlog_drain(spa); 5511544Seschrock 5521544Seschrock avl_destroy(&spa->spa_errlist_scrub); 5531544Seschrock avl_destroy(&spa->spa_errlist_last); 5541544Seschrock 555789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 556789Sahrens } 557789Sahrens 558789Sahrens /* 559789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 560789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 561789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 562789Sahrens * All vdev validation is done by the vdev_alloc() routine. 563789Sahrens */ 5642082Seschrock static int 5652082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 5662082Seschrock uint_t id, int atype) 567789Sahrens { 568789Sahrens nvlist_t **child; 569789Sahrens uint_t c, children; 5702082Seschrock int error; 5712082Seschrock 5722082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 5732082Seschrock return (error); 5742082Seschrock 5752082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 5762082Seschrock return (0); 577789Sahrens 578789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 579789Sahrens &child, &children) != 0) { 5802082Seschrock vdev_free(*vdp); 5812082Seschrock *vdp = NULL; 5822082Seschrock return (EINVAL); 583789Sahrens } 584789Sahrens 585789Sahrens for (c = 0; c < children; c++) { 5862082Seschrock vdev_t *vd; 5872082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 5882082Seschrock atype)) != 0) { 5892082Seschrock vdev_free(*vdp); 5902082Seschrock *vdp = NULL; 5912082Seschrock return (error); 592789Sahrens } 593789Sahrens } 594789Sahrens 5952082Seschrock ASSERT(*vdp != NULL); 5962082Seschrock 5972082Seschrock return (0); 598789Sahrens } 599789Sahrens 600789Sahrens /* 601789Sahrens * Opposite of spa_load(). 602789Sahrens */ 603789Sahrens static void 604789Sahrens spa_unload(spa_t *spa) 605789Sahrens { 6062082Seschrock int i; 6072082Seschrock 608789Sahrens /* 6091544Seschrock * Stop async tasks. 6101544Seschrock */ 6111544Seschrock spa_async_suspend(spa); 6121544Seschrock 6131544Seschrock /* 614789Sahrens * Stop syncing. 615789Sahrens */ 616789Sahrens if (spa->spa_sync_on) { 617789Sahrens txg_sync_stop(spa->spa_dsl_pool); 618789Sahrens spa->spa_sync_on = B_FALSE; 619789Sahrens } 620789Sahrens 621789Sahrens /* 622789Sahrens * Wait for any outstanding prefetch I/O to complete. 623789Sahrens */ 6241544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 6251544Seschrock spa_config_exit(spa, FTAG); 626789Sahrens 627789Sahrens /* 6285450Sbrendan * Drop and purge level 2 cache 6295450Sbrendan */ 6305450Sbrendan spa_l2cache_drop(spa); 6315450Sbrendan 6325450Sbrendan /* 633789Sahrens * Close the dsl pool. 634789Sahrens */ 635789Sahrens if (spa->spa_dsl_pool) { 636789Sahrens dsl_pool_close(spa->spa_dsl_pool); 637789Sahrens spa->spa_dsl_pool = NULL; 638789Sahrens } 639789Sahrens 640789Sahrens /* 641789Sahrens * Close all vdevs. 642789Sahrens */ 6431585Sbonwick if (spa->spa_root_vdev) 644789Sahrens vdev_free(spa->spa_root_vdev); 6451585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 6461544Seschrock 6475450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 6485450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 6495450Sbrendan if (spa->spa_spares.sav_vdevs) { 6505450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 6515450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 6525450Sbrendan spa->spa_spares.sav_vdevs = NULL; 6535450Sbrendan } 6545450Sbrendan if (spa->spa_spares.sav_config) { 6555450Sbrendan nvlist_free(spa->spa_spares.sav_config); 6565450Sbrendan spa->spa_spares.sav_config = NULL; 6572082Seschrock } 6585450Sbrendan 6595450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 6605450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 6615450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 6625450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 6635450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 6645450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 6655450Sbrendan } 6665450Sbrendan if (spa->spa_l2cache.sav_config) { 6675450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 6685450Sbrendan spa->spa_l2cache.sav_config = NULL; 6692082Seschrock } 6702082Seschrock 6711544Seschrock spa->spa_async_suspended = 0; 672789Sahrens } 673789Sahrens 674789Sahrens /* 6752082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 6762082Seschrock * this pool. When this is called, we have some form of basic information in 6775450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 6785450Sbrendan * then re-generate a more complete list including status information. 6792082Seschrock */ 6802082Seschrock static void 6812082Seschrock spa_load_spares(spa_t *spa) 6822082Seschrock { 6832082Seschrock nvlist_t **spares; 6842082Seschrock uint_t nspares; 6852082Seschrock int i; 6863377Seschrock vdev_t *vd, *tvd; 6872082Seschrock 6882082Seschrock /* 6892082Seschrock * First, close and free any existing spare vdevs. 6902082Seschrock */ 6915450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 6925450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 6933377Seschrock 6943377Seschrock /* Undo the call to spa_activate() below */ 6956643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 6966643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 6973377Seschrock spa_spare_remove(tvd); 6983377Seschrock vdev_close(vd); 6993377Seschrock vdev_free(vd); 7002082Seschrock } 7013377Seschrock 7025450Sbrendan if (spa->spa_spares.sav_vdevs) 7035450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7045450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7055450Sbrendan 7065450Sbrendan if (spa->spa_spares.sav_config == NULL) 7072082Seschrock nspares = 0; 7082082Seschrock else 7095450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 7102082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 7112082Seschrock 7125450Sbrendan spa->spa_spares.sav_count = (int)nspares; 7135450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7142082Seschrock 7152082Seschrock if (nspares == 0) 7162082Seschrock return; 7172082Seschrock 7182082Seschrock /* 7192082Seschrock * Construct the array of vdevs, opening them to get status in the 7203377Seschrock * process. For each spare, there is potentially two different vdev_t 7213377Seschrock * structures associated with it: one in the list of spares (used only 7223377Seschrock * for basic validation purposes) and one in the active vdev 7233377Seschrock * configuration (if it's spared in). During this phase we open and 7243377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 7253377Seschrock * active configuration, then we also mark this vdev as an active spare. 7262082Seschrock */ 7275450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 7285450Sbrendan KM_SLEEP); 7295450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7302082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 7312082Seschrock VDEV_ALLOC_SPARE) == 0); 7322082Seschrock ASSERT(vd != NULL); 7332082Seschrock 7345450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 7352082Seschrock 7366643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7376643Seschrock B_FALSE)) != NULL) { 7383377Seschrock if (!tvd->vdev_isspare) 7393377Seschrock spa_spare_add(tvd); 7403377Seschrock 7413377Seschrock /* 7423377Seschrock * We only mark the spare active if we were successfully 7433377Seschrock * able to load the vdev. Otherwise, importing a pool 7443377Seschrock * with a bad active spare would result in strange 7453377Seschrock * behavior, because multiple pool would think the spare 7463377Seschrock * is actively in use. 7473377Seschrock * 7483377Seschrock * There is a vulnerability here to an equally bizarre 7493377Seschrock * circumstance, where a dead active spare is later 7503377Seschrock * brought back to life (onlined or otherwise). Given 7513377Seschrock * the rarity of this scenario, and the extra complexity 7523377Seschrock * it adds, we ignore the possibility. 7533377Seschrock */ 7543377Seschrock if (!vdev_is_dead(tvd)) 7553377Seschrock spa_spare_activate(tvd); 7563377Seschrock } 7573377Seschrock 7582082Seschrock if (vdev_open(vd) != 0) 7592082Seschrock continue; 7602082Seschrock 7612082Seschrock vd->vdev_top = vd; 7625450Sbrendan if (vdev_validate_aux(vd) == 0) 7635450Sbrendan spa_spare_add(vd); 7642082Seschrock } 7652082Seschrock 7662082Seschrock /* 7672082Seschrock * Recompute the stashed list of spares, with status information 7682082Seschrock * this time. 7692082Seschrock */ 7705450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 7712082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 7722082Seschrock 7735450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 7745450Sbrendan KM_SLEEP); 7755450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7765450Sbrendan spares[i] = vdev_config_generate(spa, 7775450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 7785450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 7795450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 7805450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7812082Seschrock nvlist_free(spares[i]); 7825450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 7835450Sbrendan } 7845450Sbrendan 7855450Sbrendan /* 7865450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 7875450Sbrendan * this pool. When this is called, we have some form of basic information in 7885450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 7895450Sbrendan * then re-generate a more complete list including status information. 7905450Sbrendan * Devices which are already active have their details maintained, and are 7915450Sbrendan * not re-opened. 7925450Sbrendan */ 7935450Sbrendan static void 7945450Sbrendan spa_load_l2cache(spa_t *spa) 7955450Sbrendan { 7965450Sbrendan nvlist_t **l2cache; 7975450Sbrendan uint_t nl2cache; 7985450Sbrendan int i, j, oldnvdevs; 7996643Seschrock uint64_t guid, size; 8005450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 8015450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 8025450Sbrendan 8035450Sbrendan if (sav->sav_config != NULL) { 8045450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 8055450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 8065450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 8075450Sbrendan } else { 8085450Sbrendan nl2cache = 0; 8095450Sbrendan } 8105450Sbrendan 8115450Sbrendan oldvdevs = sav->sav_vdevs; 8125450Sbrendan oldnvdevs = sav->sav_count; 8135450Sbrendan sav->sav_vdevs = NULL; 8145450Sbrendan sav->sav_count = 0; 8155450Sbrendan 8165450Sbrendan /* 8175450Sbrendan * Process new nvlist of vdevs. 8185450Sbrendan */ 8195450Sbrendan for (i = 0; i < nl2cache; i++) { 8205450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 8215450Sbrendan &guid) == 0); 8225450Sbrendan 8235450Sbrendan newvdevs[i] = NULL; 8245450Sbrendan for (j = 0; j < oldnvdevs; j++) { 8255450Sbrendan vd = oldvdevs[j]; 8265450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 8275450Sbrendan /* 8285450Sbrendan * Retain previous vdev for add/remove ops. 8295450Sbrendan */ 8305450Sbrendan newvdevs[i] = vd; 8315450Sbrendan oldvdevs[j] = NULL; 8325450Sbrendan break; 8335450Sbrendan } 8345450Sbrendan } 8355450Sbrendan 8365450Sbrendan if (newvdevs[i] == NULL) { 8375450Sbrendan /* 8385450Sbrendan * Create new vdev 8395450Sbrendan */ 8405450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 8415450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 8425450Sbrendan ASSERT(vd != NULL); 8435450Sbrendan newvdevs[i] = vd; 8445450Sbrendan 8455450Sbrendan /* 8465450Sbrendan * Commit this vdev as an l2cache device, 8475450Sbrendan * even if it fails to open. 8485450Sbrendan */ 8495450Sbrendan spa_l2cache_add(vd); 8505450Sbrendan 8516643Seschrock vd->vdev_top = vd; 8526643Seschrock vd->vdev_aux = sav; 8536643Seschrock 8546643Seschrock spa_l2cache_activate(vd); 8556643Seschrock 8565450Sbrendan if (vdev_open(vd) != 0) 8575450Sbrendan continue; 8585450Sbrendan 8595450Sbrendan (void) vdev_validate_aux(vd); 8605450Sbrendan 8615450Sbrendan if (!vdev_is_dead(vd)) { 8625450Sbrendan size = vdev_get_rsize(vd); 8636643Seschrock l2arc_add_vdev(spa, vd, 8646643Seschrock VDEV_LABEL_START_SIZE, 8656643Seschrock size - VDEV_LABEL_START_SIZE); 8665450Sbrendan } 8675450Sbrendan } 8685450Sbrendan } 8695450Sbrendan 8705450Sbrendan /* 8715450Sbrendan * Purge vdevs that were dropped 8725450Sbrendan */ 8735450Sbrendan for (i = 0; i < oldnvdevs; i++) { 8745450Sbrendan uint64_t pool; 8755450Sbrendan 8765450Sbrendan vd = oldvdevs[i]; 8775450Sbrendan if (vd != NULL) { 8785450Sbrendan if (spa_mode & FWRITE && 8795450Sbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && 8806643Seschrock pool != 0ULL && 8816643Seschrock l2arc_vdev_present(vd)) { 8825450Sbrendan l2arc_remove_vdev(vd); 8835450Sbrendan } 8845450Sbrendan (void) vdev_close(vd); 8855450Sbrendan spa_l2cache_remove(vd); 8865450Sbrendan } 8875450Sbrendan } 8885450Sbrendan 8895450Sbrendan if (oldvdevs) 8905450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 8915450Sbrendan 8925450Sbrendan if (sav->sav_config == NULL) 8935450Sbrendan goto out; 8945450Sbrendan 8955450Sbrendan sav->sav_vdevs = newvdevs; 8965450Sbrendan sav->sav_count = (int)nl2cache; 8975450Sbrendan 8985450Sbrendan /* 8995450Sbrendan * Recompute the stashed list of l2cache devices, with status 9005450Sbrendan * information this time. 9015450Sbrendan */ 9025450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 9035450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 9045450Sbrendan 9055450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 9065450Sbrendan for (i = 0; i < sav->sav_count; i++) 9075450Sbrendan l2cache[i] = vdev_config_generate(spa, 9085450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 9095450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 9105450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 9115450Sbrendan out: 9125450Sbrendan for (i = 0; i < sav->sav_count; i++) 9135450Sbrendan nvlist_free(l2cache[i]); 9145450Sbrendan if (sav->sav_count) 9155450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 9162082Seschrock } 9172082Seschrock 9182082Seschrock static int 9192082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 9202082Seschrock { 9212082Seschrock dmu_buf_t *db; 9222082Seschrock char *packed = NULL; 9232082Seschrock size_t nvsize = 0; 9242082Seschrock int error; 9252082Seschrock *value = NULL; 9262082Seschrock 9272082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 9282082Seschrock nvsize = *(uint64_t *)db->db_data; 9292082Seschrock dmu_buf_rele(db, FTAG); 9302082Seschrock 9312082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 9322082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 9332082Seschrock if (error == 0) 9342082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 9352082Seschrock kmem_free(packed, nvsize); 9362082Seschrock 9372082Seschrock return (error); 9382082Seschrock } 9392082Seschrock 9402082Seschrock /* 9414451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9424451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9434451Seschrock */ 9444451Seschrock static void 9454451Seschrock spa_check_removed(vdev_t *vd) 9464451Seschrock { 9474451Seschrock int c; 9484451Seschrock 9494451Seschrock for (c = 0; c < vd->vdev_children; c++) 9504451Seschrock spa_check_removed(vd->vdev_child[c]); 9514451Seschrock 9524451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9534451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9544451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9554451Seschrock } 9564451Seschrock } 9574451Seschrock 9584451Seschrock /* 959789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 9601544Seschrock * source of configuration information. 961789Sahrens */ 962789Sahrens static int 9631544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 964789Sahrens { 965789Sahrens int error = 0; 966789Sahrens nvlist_t *nvroot = NULL; 967789Sahrens vdev_t *rvd; 968789Sahrens uberblock_t *ub = &spa->spa_uberblock; 9691635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 970789Sahrens uint64_t pool_guid; 9712082Seschrock uint64_t version; 972789Sahrens zio_t *zio; 9734451Seschrock uint64_t autoreplace = 0; 974789Sahrens 9751544Seschrock spa->spa_load_state = state; 9761635Sbonwick 977789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 9781733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 9791544Seschrock error = EINVAL; 9801544Seschrock goto out; 9811544Seschrock } 982789Sahrens 9832082Seschrock /* 9842082Seschrock * Versioning wasn't explicitly added to the label until later, so if 9852082Seschrock * it's not present treat it as the initial version. 9862082Seschrock */ 9872082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 9884577Sahrens version = SPA_VERSION_INITIAL; 9892082Seschrock 9901733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 9911733Sbonwick &spa->spa_config_txg); 9921733Sbonwick 9931635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 9941544Seschrock spa_guid_exists(pool_guid, 0)) { 9951544Seschrock error = EEXIST; 9961544Seschrock goto out; 9971544Seschrock } 998789Sahrens 9992174Seschrock spa->spa_load_guid = pool_guid; 10002174Seschrock 1001789Sahrens /* 10022082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 10032082Seschrock * value that will be returned by spa_version() since parsing the 10042082Seschrock * configuration requires knowing the version number. 1005789Sahrens */ 10061544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 10072082Seschrock spa->spa_ubsync.ub_version = version; 10082082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 10091544Seschrock spa_config_exit(spa, FTAG); 1010789Sahrens 10112082Seschrock if (error != 0) 10121544Seschrock goto out; 1013789Sahrens 10141585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1015789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1016789Sahrens 1017789Sahrens /* 1018789Sahrens * Try to open all vdevs, loading each label in the process. 1019789Sahrens */ 10204070Smc142369 error = vdev_open(rvd); 10214070Smc142369 if (error != 0) 10221544Seschrock goto out; 1023789Sahrens 1024789Sahrens /* 10251986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 10261986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 10271986Seschrock * flag. 10281986Seschrock */ 10291986Seschrock spa_config_enter(spa, RW_READER, FTAG); 10301986Seschrock error = vdev_validate(rvd); 10311986Seschrock spa_config_exit(spa, FTAG); 10321986Seschrock 10334070Smc142369 if (error != 0) 10341986Seschrock goto out; 10351986Seschrock 10361986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 10371986Seschrock error = ENXIO; 10381986Seschrock goto out; 10391986Seschrock } 10401986Seschrock 10411986Seschrock /* 1042789Sahrens * Find the best uberblock. 1043789Sahrens */ 1044789Sahrens bzero(ub, sizeof (uberblock_t)); 1045789Sahrens 1046789Sahrens zio = zio_root(spa, NULL, NULL, 1047789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1048789Sahrens vdev_uberblock_load(zio, rvd, ub); 1049789Sahrens error = zio_wait(zio); 1050789Sahrens 1051789Sahrens /* 1052789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1053789Sahrens */ 1054789Sahrens if (ub->ub_txg == 0) { 10551760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10561760Seschrock VDEV_AUX_CORRUPT_DATA); 10571544Seschrock error = ENXIO; 10581544Seschrock goto out; 10591544Seschrock } 10601544Seschrock 10611544Seschrock /* 10621544Seschrock * If the pool is newer than the code, we can't open it. 10631544Seschrock */ 10644577Sahrens if (ub->ub_version > SPA_VERSION) { 10651760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10661760Seschrock VDEV_AUX_VERSION_NEWER); 10671544Seschrock error = ENOTSUP; 10681544Seschrock goto out; 1069789Sahrens } 1070789Sahrens 1071789Sahrens /* 1072789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1073789Sahrens * incomplete configuration. 1074789Sahrens */ 10751732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 10761544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10771544Seschrock VDEV_AUX_BAD_GUID_SUM); 10781544Seschrock error = ENXIO; 10791544Seschrock goto out; 1080789Sahrens } 1081789Sahrens 1082789Sahrens /* 1083789Sahrens * Initialize internal SPA structures. 1084789Sahrens */ 1085789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1086789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1087789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 10881544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 10891544Seschrock if (error) { 10901544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10911544Seschrock VDEV_AUX_CORRUPT_DATA); 10921544Seschrock goto out; 10931544Seschrock } 1094789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1095789Sahrens 10961544Seschrock if (zap_lookup(spa->spa_meta_objset, 1097789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 10981544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 10991544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11001544Seschrock VDEV_AUX_CORRUPT_DATA); 11011544Seschrock error = EIO; 11021544Seschrock goto out; 11031544Seschrock } 1104789Sahrens 1105789Sahrens if (!mosconfig) { 11062082Seschrock nvlist_t *newconfig; 11073975Sek110237 uint64_t hostid; 11082082Seschrock 11092082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 11101544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11111544Seschrock VDEV_AUX_CORRUPT_DATA); 11121544Seschrock error = EIO; 11131544Seschrock goto out; 11141544Seschrock } 1115789Sahrens 11163975Sek110237 if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 11173975Sek110237 &hostid) == 0) { 11183975Sek110237 char *hostname; 11193975Sek110237 unsigned long myhostid = 0; 11203975Sek110237 11213975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 11223975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 11233975Sek110237 11243975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 11254178Slling if (hostid != 0 && myhostid != 0 && 11264178Slling (unsigned long)hostid != myhostid) { 11273975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 11283975Sek110237 "loaded as it was last accessed by " 11293975Sek110237 "another system (host: %s hostid: 0x%lx). " 11303975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 11313975Sek110237 spa->spa_name, hostname, 11323975Sek110237 (unsigned long)hostid); 11333975Sek110237 error = EBADF; 11343975Sek110237 goto out; 11353975Sek110237 } 11363975Sek110237 } 11373975Sek110237 1138789Sahrens spa_config_set(spa, newconfig); 1139789Sahrens spa_unload(spa); 1140789Sahrens spa_deactivate(spa); 1141789Sahrens spa_activate(spa); 1142789Sahrens 11431544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 11441544Seschrock } 11451544Seschrock 11461544Seschrock if (zap_lookup(spa->spa_meta_objset, 11471544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 11481544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 11491544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11501544Seschrock VDEV_AUX_CORRUPT_DATA); 11511544Seschrock error = EIO; 11521544Seschrock goto out; 1153789Sahrens } 1154789Sahrens 11551544Seschrock /* 11562082Seschrock * Load the bit that tells us to use the new accounting function 11572082Seschrock * (raid-z deflation). If we have an older pool, this will not 11582082Seschrock * be present. 11592082Seschrock */ 11602082Seschrock error = zap_lookup(spa->spa_meta_objset, 11612082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 11622082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 11632082Seschrock if (error != 0 && error != ENOENT) { 11642082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11652082Seschrock VDEV_AUX_CORRUPT_DATA); 11662082Seschrock error = EIO; 11672082Seschrock goto out; 11682082Seschrock } 11692082Seschrock 11702082Seschrock /* 11711544Seschrock * Load the persistent error log. If we have an older pool, this will 11721544Seschrock * not be present. 11731544Seschrock */ 11741544Seschrock error = zap_lookup(spa->spa_meta_objset, 11751544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 11761544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 11771807Sbonwick if (error != 0 && error != ENOENT) { 11781544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11791544Seschrock VDEV_AUX_CORRUPT_DATA); 11801544Seschrock error = EIO; 11811544Seschrock goto out; 11821544Seschrock } 11831544Seschrock 11841544Seschrock error = zap_lookup(spa->spa_meta_objset, 11851544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 11861544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 11871544Seschrock if (error != 0 && error != ENOENT) { 11881544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11891544Seschrock VDEV_AUX_CORRUPT_DATA); 11901544Seschrock error = EIO; 11911544Seschrock goto out; 11921544Seschrock } 1193789Sahrens 1194789Sahrens /* 11952926Sek110237 * Load the history object. If we have an older pool, this 11962926Sek110237 * will not be present. 11972926Sek110237 */ 11982926Sek110237 error = zap_lookup(spa->spa_meta_objset, 11992926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 12002926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 12012926Sek110237 if (error != 0 && error != ENOENT) { 12022926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12032926Sek110237 VDEV_AUX_CORRUPT_DATA); 12042926Sek110237 error = EIO; 12052926Sek110237 goto out; 12062926Sek110237 } 12072926Sek110237 12082926Sek110237 /* 12092082Seschrock * Load any hot spares for this pool. 12102082Seschrock */ 12112082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12125450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 12132082Seschrock if (error != 0 && error != ENOENT) { 12142082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12152082Seschrock VDEV_AUX_CORRUPT_DATA); 12162082Seschrock error = EIO; 12172082Seschrock goto out; 12182082Seschrock } 12192082Seschrock if (error == 0) { 12204577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 12215450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 12225450Sbrendan &spa->spa_spares.sav_config) != 0) { 12232082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12242082Seschrock VDEV_AUX_CORRUPT_DATA); 12252082Seschrock error = EIO; 12262082Seschrock goto out; 12272082Seschrock } 12282082Seschrock 12292082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12302082Seschrock spa_load_spares(spa); 12312082Seschrock spa_config_exit(spa, FTAG); 12322082Seschrock } 12332082Seschrock 12345450Sbrendan /* 12355450Sbrendan * Load any level 2 ARC devices for this pool. 12365450Sbrendan */ 12375450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12385450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 12395450Sbrendan &spa->spa_l2cache.sav_object); 12405450Sbrendan if (error != 0 && error != ENOENT) { 12415450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12425450Sbrendan VDEV_AUX_CORRUPT_DATA); 12435450Sbrendan error = EIO; 12445450Sbrendan goto out; 12455450Sbrendan } 12465450Sbrendan if (error == 0) { 12475450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 12485450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 12495450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 12505450Sbrendan vdev_set_state(rvd, B_TRUE, 12515450Sbrendan VDEV_STATE_CANT_OPEN, 12525450Sbrendan VDEV_AUX_CORRUPT_DATA); 12535450Sbrendan error = EIO; 12545450Sbrendan goto out; 12555450Sbrendan } 12565450Sbrendan 12575450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 12585450Sbrendan spa_load_l2cache(spa); 12595450Sbrendan spa_config_exit(spa, FTAG); 12605450Sbrendan } 12615450Sbrendan 12625094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 12634543Smarks 12643912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12653912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 12663912Slling 12673912Slling if (error && error != ENOENT) { 12683912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12693912Slling VDEV_AUX_CORRUPT_DATA); 12703912Slling error = EIO; 12713912Slling goto out; 12723912Slling } 12733912Slling 12743912Slling if (error == 0) { 12753912Slling (void) zap_lookup(spa->spa_meta_objset, 12763912Slling spa->spa_pool_props_object, 12774451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 12783912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 12794451Seschrock (void) zap_lookup(spa->spa_meta_objset, 12804451Seschrock spa->spa_pool_props_object, 12814451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 12824451Seschrock sizeof (uint64_t), 1, &autoreplace); 12834543Smarks (void) zap_lookup(spa->spa_meta_objset, 12844543Smarks spa->spa_pool_props_object, 12854543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 12864543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 12875329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 12885329Sgw25295 spa->spa_pool_props_object, 12895329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 12905329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 12913912Slling } 12923912Slling 12932082Seschrock /* 12944451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 12954451Seschrock * the ZFS DE that it should not issue any faults for unopenable 12964451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 12974451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 12984451Seschrock * over. 12994451Seschrock */ 13005756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 13014451Seschrock spa_check_removed(spa->spa_root_vdev); 13024451Seschrock 13034451Seschrock /* 13041986Seschrock * Load the vdev state for all toplevel vdevs. 1305789Sahrens */ 13061986Seschrock vdev_load(rvd); 1307789Sahrens 1308789Sahrens /* 1309789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1310789Sahrens */ 13111544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1312789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 13131544Seschrock spa_config_exit(spa, FTAG); 1314789Sahrens 1315789Sahrens /* 1316789Sahrens * Check the state of the root vdev. If it can't be opened, it 1317789Sahrens * indicates one or more toplevel vdevs are faulted. 1318789Sahrens */ 13191544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13201544Seschrock error = ENXIO; 13211544Seschrock goto out; 13221544Seschrock } 1323789Sahrens 13241544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 13251635Sbonwick dmu_tx_t *tx; 13261635Sbonwick int need_update = B_FALSE; 13271585Sbonwick int c; 13281601Sbonwick 13291635Sbonwick /* 13301635Sbonwick * Claim log blocks that haven't been committed yet. 13311635Sbonwick * This must all happen in a single txg. 13321635Sbonwick */ 13331601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1334789Sahrens spa_first_txg(spa)); 13352417Sahrens (void) dmu_objset_find(spa->spa_name, 13362417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1337789Sahrens dmu_tx_commit(tx); 1338789Sahrens 1339789Sahrens spa->spa_sync_on = B_TRUE; 1340789Sahrens txg_sync_start(spa->spa_dsl_pool); 1341789Sahrens 1342789Sahrens /* 1343789Sahrens * Wait for all claims to sync. 1344789Sahrens */ 1345789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 13461585Sbonwick 13471585Sbonwick /* 13481635Sbonwick * If the config cache is stale, or we have uninitialized 13491635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 13501585Sbonwick */ 13511635Sbonwick if (config_cache_txg != spa->spa_config_txg || 13521635Sbonwick state == SPA_LOAD_IMPORT) 13531635Sbonwick need_update = B_TRUE; 13541635Sbonwick 13551635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 13561635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 13571635Sbonwick need_update = B_TRUE; 13581585Sbonwick 13591585Sbonwick /* 13601635Sbonwick * Update the config cache asychronously in case we're the 13611635Sbonwick * root pool, in which case the config cache isn't writable yet. 13621585Sbonwick */ 13631635Sbonwick if (need_update) 13641635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1365789Sahrens } 1366789Sahrens 13671544Seschrock error = 0; 13681544Seschrock out: 13697046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 13702082Seschrock if (error && error != EBADF) 13711544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 13721544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 13731544Seschrock spa->spa_ena = 0; 13741544Seschrock 13751544Seschrock return (error); 1376789Sahrens } 1377789Sahrens 1378789Sahrens /* 1379789Sahrens * Pool Open/Import 1380789Sahrens * 1381789Sahrens * The import case is identical to an open except that the configuration is sent 1382789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1383789Sahrens * case of an open, the pool configuration will exist in the 13844451Seschrock * POOL_STATE_UNINITIALIZED state. 1385789Sahrens * 1386789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1387789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1388789Sahrens * ambiguous state. 1389789Sahrens */ 1390789Sahrens static int 1391789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1392789Sahrens { 1393789Sahrens spa_t *spa; 1394789Sahrens int error; 1395789Sahrens int locked = B_FALSE; 1396789Sahrens 1397789Sahrens *spapp = NULL; 1398789Sahrens 1399789Sahrens /* 1400789Sahrens * As disgusting as this is, we need to support recursive calls to this 1401789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1402789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1403789Sahrens * avoid dsl_dir_open() calling this in the first place. 1404789Sahrens */ 1405789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1406789Sahrens mutex_enter(&spa_namespace_lock); 1407789Sahrens locked = B_TRUE; 1408789Sahrens } 1409789Sahrens 1410789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1411789Sahrens if (locked) 1412789Sahrens mutex_exit(&spa_namespace_lock); 1413789Sahrens return (ENOENT); 1414789Sahrens } 1415789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1416789Sahrens 1417789Sahrens spa_activate(spa); 1418789Sahrens 14191635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1420789Sahrens 1421789Sahrens if (error == EBADF) { 1422789Sahrens /* 14231986Seschrock * If vdev_validate() returns failure (indicated by 14241986Seschrock * EBADF), it indicates that one of the vdevs indicates 14251986Seschrock * that the pool has been exported or destroyed. If 14261986Seschrock * this is the case, the config cache is out of sync and 14271986Seschrock * we should remove the pool from the namespace. 1428789Sahrens */ 1429789Sahrens spa_unload(spa); 1430789Sahrens spa_deactivate(spa); 14316643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1432789Sahrens spa_remove(spa); 1433789Sahrens if (locked) 1434789Sahrens mutex_exit(&spa_namespace_lock); 1435789Sahrens return (ENOENT); 14361544Seschrock } 14371544Seschrock 14381544Seschrock if (error) { 1439789Sahrens /* 1440789Sahrens * We can't open the pool, but we still have useful 1441789Sahrens * information: the state of each vdev after the 1442789Sahrens * attempted vdev_open(). Return this to the user. 1443789Sahrens */ 14441635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 14451635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1446789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1447789Sahrens B_TRUE); 14481635Sbonwick spa_config_exit(spa, FTAG); 14491635Sbonwick } 1450789Sahrens spa_unload(spa); 1451789Sahrens spa_deactivate(spa); 14521544Seschrock spa->spa_last_open_failed = B_TRUE; 1453789Sahrens if (locked) 1454789Sahrens mutex_exit(&spa_namespace_lock); 1455789Sahrens *spapp = NULL; 1456789Sahrens return (error); 14571544Seschrock } else { 14581544Seschrock spa->spa_last_open_failed = B_FALSE; 1459789Sahrens } 1460789Sahrens } 1461789Sahrens 1462789Sahrens spa_open_ref(spa, tag); 14634451Seschrock 1464789Sahrens if (locked) 1465789Sahrens mutex_exit(&spa_namespace_lock); 1466789Sahrens 1467789Sahrens *spapp = spa; 1468789Sahrens 1469789Sahrens if (config != NULL) { 14701544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1471789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 14721544Seschrock spa_config_exit(spa, FTAG); 1473789Sahrens } 1474789Sahrens 1475789Sahrens return (0); 1476789Sahrens } 1477789Sahrens 1478789Sahrens int 1479789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1480789Sahrens { 1481789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1482789Sahrens } 1483789Sahrens 14841544Seschrock /* 14851544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 14861544Seschrock * preventing it from being exported or destroyed. 14871544Seschrock */ 14881544Seschrock spa_t * 14891544Seschrock spa_inject_addref(char *name) 14901544Seschrock { 14911544Seschrock spa_t *spa; 14921544Seschrock 14931544Seschrock mutex_enter(&spa_namespace_lock); 14941544Seschrock if ((spa = spa_lookup(name)) == NULL) { 14951544Seschrock mutex_exit(&spa_namespace_lock); 14961544Seschrock return (NULL); 14971544Seschrock } 14981544Seschrock spa->spa_inject_ref++; 14991544Seschrock mutex_exit(&spa_namespace_lock); 15001544Seschrock 15011544Seschrock return (spa); 15021544Seschrock } 15031544Seschrock 15041544Seschrock void 15051544Seschrock spa_inject_delref(spa_t *spa) 15061544Seschrock { 15071544Seschrock mutex_enter(&spa_namespace_lock); 15081544Seschrock spa->spa_inject_ref--; 15091544Seschrock mutex_exit(&spa_namespace_lock); 15101544Seschrock } 15111544Seschrock 15125450Sbrendan /* 15135450Sbrendan * Add spares device information to the nvlist. 15145450Sbrendan */ 15152082Seschrock static void 15162082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 15172082Seschrock { 15182082Seschrock nvlist_t **spares; 15192082Seschrock uint_t i, nspares; 15202082Seschrock nvlist_t *nvroot; 15212082Seschrock uint64_t guid; 15222082Seschrock vdev_stat_t *vs; 15232082Seschrock uint_t vsc; 15243377Seschrock uint64_t pool; 15252082Seschrock 15265450Sbrendan if (spa->spa_spares.sav_count == 0) 15272082Seschrock return; 15282082Seschrock 15292082Seschrock VERIFY(nvlist_lookup_nvlist(config, 15302082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15315450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 15322082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15332082Seschrock if (nspares != 0) { 15342082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 15352082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 15362082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 15372082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15382082Seschrock 15392082Seschrock /* 15402082Seschrock * Go through and find any spares which have since been 15412082Seschrock * repurposed as an active spare. If this is the case, update 15422082Seschrock * their status appropriately. 15432082Seschrock */ 15442082Seschrock for (i = 0; i < nspares; i++) { 15452082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 15462082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 1547*7214Slling if (spa_spare_exists(guid, &pool, NULL) && 1548*7214Slling pool != 0ULL) { 15492082Seschrock VERIFY(nvlist_lookup_uint64_array( 15502082Seschrock spares[i], ZPOOL_CONFIG_STATS, 15512082Seschrock (uint64_t **)&vs, &vsc) == 0); 15522082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 15532082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 15542082Seschrock } 15552082Seschrock } 15562082Seschrock } 15572082Seschrock } 15582082Seschrock 15595450Sbrendan /* 15605450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 15615450Sbrendan */ 15625450Sbrendan static void 15635450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 15645450Sbrendan { 15655450Sbrendan nvlist_t **l2cache; 15665450Sbrendan uint_t i, j, nl2cache; 15675450Sbrendan nvlist_t *nvroot; 15685450Sbrendan uint64_t guid; 15695450Sbrendan vdev_t *vd; 15705450Sbrendan vdev_stat_t *vs; 15715450Sbrendan uint_t vsc; 15725450Sbrendan 15735450Sbrendan if (spa->spa_l2cache.sav_count == 0) 15745450Sbrendan return; 15755450Sbrendan 15765450Sbrendan spa_config_enter(spa, RW_READER, FTAG); 15775450Sbrendan 15785450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 15795450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15805450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 15815450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 15825450Sbrendan if (nl2cache != 0) { 15835450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 15845450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 15855450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 15865450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 15875450Sbrendan 15885450Sbrendan /* 15895450Sbrendan * Update level 2 cache device stats. 15905450Sbrendan */ 15915450Sbrendan 15925450Sbrendan for (i = 0; i < nl2cache; i++) { 15935450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 15945450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 15955450Sbrendan 15965450Sbrendan vd = NULL; 15975450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 15985450Sbrendan if (guid == 15995450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 16005450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 16015450Sbrendan break; 16025450Sbrendan } 16035450Sbrendan } 16045450Sbrendan ASSERT(vd != NULL); 16055450Sbrendan 16065450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 16075450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 16085450Sbrendan vdev_get_stats(vd, vs); 16095450Sbrendan } 16105450Sbrendan } 16115450Sbrendan 16125450Sbrendan spa_config_exit(spa, FTAG); 16135450Sbrendan } 16145450Sbrendan 1615789Sahrens int 16161544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1617789Sahrens { 1618789Sahrens int error; 1619789Sahrens spa_t *spa; 1620789Sahrens 1621789Sahrens *config = NULL; 1622789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1623789Sahrens 16242082Seschrock if (spa && *config != NULL) { 16251544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 16261544Seschrock spa_get_errlog_size(spa)) == 0); 16271544Seschrock 16282082Seschrock spa_add_spares(spa, *config); 16295450Sbrendan spa_add_l2cache(spa, *config); 16302082Seschrock } 16312082Seschrock 16321544Seschrock /* 16331544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 16341544Seschrock * and call spa_lookup() directly. 16351544Seschrock */ 16361544Seschrock if (altroot) { 16371544Seschrock if (spa == NULL) { 16381544Seschrock mutex_enter(&spa_namespace_lock); 16391544Seschrock spa = spa_lookup(name); 16401544Seschrock if (spa) 16411544Seschrock spa_altroot(spa, altroot, buflen); 16421544Seschrock else 16431544Seschrock altroot[0] = '\0'; 16441544Seschrock spa = NULL; 16451544Seschrock mutex_exit(&spa_namespace_lock); 16461544Seschrock } else { 16471544Seschrock spa_altroot(spa, altroot, buflen); 16481544Seschrock } 16491544Seschrock } 16501544Seschrock 1651789Sahrens if (spa != NULL) 1652789Sahrens spa_close(spa, FTAG); 1653789Sahrens 1654789Sahrens return (error); 1655789Sahrens } 1656789Sahrens 1657789Sahrens /* 16585450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 16595450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 16605450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 16615450Sbrendan * specified, as long as they are well-formed. 16622082Seschrock */ 16632082Seschrock static int 16645450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 16655450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 16665450Sbrendan vdev_labeltype_t label) 16672082Seschrock { 16685450Sbrendan nvlist_t **dev; 16695450Sbrendan uint_t i, ndev; 16702082Seschrock vdev_t *vd; 16712082Seschrock int error; 16722082Seschrock 16732082Seschrock /* 16745450Sbrendan * It's acceptable to have no devs specified. 16752082Seschrock */ 16765450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 16772082Seschrock return (0); 16782082Seschrock 16795450Sbrendan if (ndev == 0) 16802082Seschrock return (EINVAL); 16812082Seschrock 16822082Seschrock /* 16835450Sbrendan * Make sure the pool is formatted with a version that supports this 16845450Sbrendan * device type. 16852082Seschrock */ 16865450Sbrendan if (spa_version(spa) < version) 16872082Seschrock return (ENOTSUP); 16882082Seschrock 16893377Seschrock /* 16905450Sbrendan * Set the pending device list so we correctly handle device in-use 16913377Seschrock * checking. 16923377Seschrock */ 16935450Sbrendan sav->sav_pending = dev; 16945450Sbrendan sav->sav_npending = ndev; 16955450Sbrendan 16965450Sbrendan for (i = 0; i < ndev; i++) { 16975450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 16982082Seschrock mode)) != 0) 16993377Seschrock goto out; 17002082Seschrock 17012082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 17022082Seschrock vdev_free(vd); 17033377Seschrock error = EINVAL; 17043377Seschrock goto out; 17052082Seschrock } 17062082Seschrock 17075450Sbrendan /* 17085450Sbrendan * The L2ARC currently only supports disk devices. 17095450Sbrendan */ 17105450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 17115450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 17125450Sbrendan error = ENOTBLK; 17135450Sbrendan goto out; 17145450Sbrendan } 17155450Sbrendan 17162082Seschrock vd->vdev_top = vd; 17173377Seschrock 17183377Seschrock if ((error = vdev_open(vd)) == 0 && 17195450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 17205450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 17213377Seschrock vd->vdev_guid) == 0); 17222082Seschrock } 17232082Seschrock 17242082Seschrock vdev_free(vd); 17253377Seschrock 17265450Sbrendan if (error && 17275450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 17283377Seschrock goto out; 17293377Seschrock else 17303377Seschrock error = 0; 17312082Seschrock } 17322082Seschrock 17333377Seschrock out: 17345450Sbrendan sav->sav_pending = NULL; 17355450Sbrendan sav->sav_npending = 0; 17363377Seschrock return (error); 17372082Seschrock } 17382082Seschrock 17395450Sbrendan static int 17405450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 17415450Sbrendan { 17425450Sbrendan int error; 17435450Sbrendan 17445450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17455450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 17465450Sbrendan VDEV_LABEL_SPARE)) != 0) { 17475450Sbrendan return (error); 17485450Sbrendan } 17495450Sbrendan 17505450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17515450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 17525450Sbrendan VDEV_LABEL_L2CACHE)); 17535450Sbrendan } 17545450Sbrendan 17555450Sbrendan static void 17565450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 17575450Sbrendan const char *config) 17585450Sbrendan { 17595450Sbrendan int i; 17605450Sbrendan 17615450Sbrendan if (sav->sav_config != NULL) { 17625450Sbrendan nvlist_t **olddevs; 17635450Sbrendan uint_t oldndevs; 17645450Sbrendan nvlist_t **newdevs; 17655450Sbrendan 17665450Sbrendan /* 17675450Sbrendan * Generate new dev list by concatentating with the 17685450Sbrendan * current dev list. 17695450Sbrendan */ 17705450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 17715450Sbrendan &olddevs, &oldndevs) == 0); 17725450Sbrendan 17735450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 17745450Sbrendan (ndevs + oldndevs), KM_SLEEP); 17755450Sbrendan for (i = 0; i < oldndevs; i++) 17765450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 17775450Sbrendan KM_SLEEP) == 0); 17785450Sbrendan for (i = 0; i < ndevs; i++) 17795450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 17805450Sbrendan KM_SLEEP) == 0); 17815450Sbrendan 17825450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 17835450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 17845450Sbrendan 17855450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 17865450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 17875450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 17885450Sbrendan nvlist_free(newdevs[i]); 17895450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 17905450Sbrendan } else { 17915450Sbrendan /* 17925450Sbrendan * Generate a new dev list. 17935450Sbrendan */ 17945450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 17955450Sbrendan KM_SLEEP) == 0); 17965450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 17975450Sbrendan devs, ndevs) == 0); 17985450Sbrendan } 17995450Sbrendan } 18005450Sbrendan 18015450Sbrendan /* 18025450Sbrendan * Stop and drop level 2 ARC devices 18035450Sbrendan */ 18045450Sbrendan void 18055450Sbrendan spa_l2cache_drop(spa_t *spa) 18065450Sbrendan { 18075450Sbrendan vdev_t *vd; 18085450Sbrendan int i; 18095450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 18105450Sbrendan 18115450Sbrendan for (i = 0; i < sav->sav_count; i++) { 18125450Sbrendan uint64_t pool; 18135450Sbrendan 18145450Sbrendan vd = sav->sav_vdevs[i]; 18155450Sbrendan ASSERT(vd != NULL); 18165450Sbrendan 18175450Sbrendan if (spa_mode & FWRITE && 18186643Seschrock spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL && 18196643Seschrock l2arc_vdev_present(vd)) { 18205450Sbrendan l2arc_remove_vdev(vd); 18215450Sbrendan } 18225450Sbrendan if (vd->vdev_isl2cache) 18235450Sbrendan spa_l2cache_remove(vd); 18245450Sbrendan vdev_clear_stats(vd); 18255450Sbrendan (void) vdev_close(vd); 18265450Sbrendan } 18275450Sbrendan } 18285450Sbrendan 18292082Seschrock /* 1830789Sahrens * Pool Creation 1831789Sahrens */ 1832789Sahrens int 18335094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 18347184Stimh const char *history_str, nvlist_t *zplprops) 1835789Sahrens { 1836789Sahrens spa_t *spa; 18375094Slling char *altroot = NULL; 18381635Sbonwick vdev_t *rvd; 1839789Sahrens dsl_pool_t *dp; 1840789Sahrens dmu_tx_t *tx; 18412082Seschrock int c, error = 0; 1842789Sahrens uint64_t txg = TXG_INITIAL; 18435450Sbrendan nvlist_t **spares, **l2cache; 18445450Sbrendan uint_t nspares, nl2cache; 18455094Slling uint64_t version; 1846789Sahrens 1847789Sahrens /* 1848789Sahrens * If this pool already exists, return failure. 1849789Sahrens */ 1850789Sahrens mutex_enter(&spa_namespace_lock); 1851789Sahrens if (spa_lookup(pool) != NULL) { 1852789Sahrens mutex_exit(&spa_namespace_lock); 1853789Sahrens return (EEXIST); 1854789Sahrens } 1855789Sahrens 1856789Sahrens /* 1857789Sahrens * Allocate a new spa_t structure. 1858789Sahrens */ 18595094Slling (void) nvlist_lookup_string(props, 18605094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 18611635Sbonwick spa = spa_add(pool, altroot); 1862789Sahrens spa_activate(spa); 1863789Sahrens 1864789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 18655094Slling 18665094Slling if (props && (error = spa_prop_validate(spa, props))) { 18675094Slling spa_unload(spa); 18685094Slling spa_deactivate(spa); 18695094Slling spa_remove(spa); 18706643Seschrock mutex_exit(&spa_namespace_lock); 18715094Slling return (error); 18725094Slling } 18735094Slling 18745094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 18755094Slling &version) != 0) 18765094Slling version = SPA_VERSION; 18775094Slling ASSERT(version <= SPA_VERSION); 18785094Slling spa->spa_uberblock.ub_version = version; 1879789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1880789Sahrens 18811635Sbonwick /* 18821635Sbonwick * Create the root vdev. 18831635Sbonwick */ 18841635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 18851635Sbonwick 18862082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 18872082Seschrock 18882082Seschrock ASSERT(error != 0 || rvd != NULL); 18892082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 18902082Seschrock 18915913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 18921635Sbonwick error = EINVAL; 18932082Seschrock 18942082Seschrock if (error == 0 && 18952082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 18965450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 18972082Seschrock VDEV_ALLOC_ADD)) == 0) { 18982082Seschrock for (c = 0; c < rvd->vdev_children; c++) 18992082Seschrock vdev_init(rvd->vdev_child[c], txg); 19002082Seschrock vdev_config_dirty(rvd); 19011635Sbonwick } 19021635Sbonwick 19031635Sbonwick spa_config_exit(spa, FTAG); 1904789Sahrens 19052082Seschrock if (error != 0) { 1906789Sahrens spa_unload(spa); 1907789Sahrens spa_deactivate(spa); 1908789Sahrens spa_remove(spa); 1909789Sahrens mutex_exit(&spa_namespace_lock); 1910789Sahrens return (error); 1911789Sahrens } 1912789Sahrens 19132082Seschrock /* 19142082Seschrock * Get the list of spares, if specified. 19152082Seschrock */ 19162082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 19172082Seschrock &spares, &nspares) == 0) { 19185450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 19192082Seschrock KM_SLEEP) == 0); 19205450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 19212082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 19222082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 19232082Seschrock spa_load_spares(spa); 19242082Seschrock spa_config_exit(spa, FTAG); 19255450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 19265450Sbrendan } 19275450Sbrendan 19285450Sbrendan /* 19295450Sbrendan * Get the list of level 2 cache devices, if specified. 19305450Sbrendan */ 19315450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 19325450Sbrendan &l2cache, &nl2cache) == 0) { 19335450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 19345450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 19355450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 19365450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 19375450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 19385450Sbrendan spa_load_l2cache(spa); 19395450Sbrendan spa_config_exit(spa, FTAG); 19405450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 19412082Seschrock } 19422082Seschrock 19437184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 1944789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1945789Sahrens 1946789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1947789Sahrens 1948789Sahrens /* 1949789Sahrens * Create the pool config object. 1950789Sahrens */ 1951789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1952789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1953789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1954789Sahrens 19551544Seschrock if (zap_add(spa->spa_meta_objset, 1956789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 19571544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 19581544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 19591544Seschrock } 1960789Sahrens 19615094Slling /* Newly created pools with the right version are always deflated. */ 19625094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 19635094Slling spa->spa_deflate = TRUE; 19645094Slling if (zap_add(spa->spa_meta_objset, 19655094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 19665094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 19675094Slling cmn_err(CE_PANIC, "failed to add deflate"); 19685094Slling } 19692082Seschrock } 19702082Seschrock 1971789Sahrens /* 1972789Sahrens * Create the deferred-free bplist object. Turn off compression 1973789Sahrens * because sync-to-convergence takes longer if the blocksize 1974789Sahrens * keeps changing. 1975789Sahrens */ 1976789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1977789Sahrens 1 << 14, tx); 1978789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1979789Sahrens ZIO_COMPRESS_OFF, tx); 1980789Sahrens 19811544Seschrock if (zap_add(spa->spa_meta_objset, 1982789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 19831544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 19841544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 19851544Seschrock } 1986789Sahrens 19872926Sek110237 /* 19882926Sek110237 * Create the pool's history object. 19892926Sek110237 */ 19905094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 19915094Slling spa_history_create_obj(spa, tx); 19925094Slling 19935094Slling /* 19945094Slling * Set pool properties. 19955094Slling */ 19965094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 19975094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 19985329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 19995094Slling if (props) 20005094Slling spa_sync_props(spa, props, CRED(), tx); 20012926Sek110237 2002789Sahrens dmu_tx_commit(tx); 2003789Sahrens 2004789Sahrens spa->spa_sync_on = B_TRUE; 2005789Sahrens txg_sync_start(spa->spa_dsl_pool); 2006789Sahrens 2007789Sahrens /* 2008789Sahrens * We explicitly wait for the first transaction to complete so that our 2009789Sahrens * bean counters are appropriately updated. 2010789Sahrens */ 2011789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2012789Sahrens 20136643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2014789Sahrens 20155094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 20164715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 20174715Sek110237 2018789Sahrens mutex_exit(&spa_namespace_lock); 2019789Sahrens 20207046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 20217046Sahrens 2022789Sahrens return (0); 2023789Sahrens } 2024789Sahrens 2025789Sahrens /* 2026789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2027789Sahrens * then call spa_load() to do the dirty work. 2028789Sahrens */ 20296423Sgw25295 static int 20306423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 20316643Seschrock boolean_t isroot, boolean_t allowfaulted) 2032789Sahrens { 2033789Sahrens spa_t *spa; 20345094Slling char *altroot = NULL; 20356643Seschrock int error, loaderr; 20362082Seschrock nvlist_t *nvroot; 20375450Sbrendan nvlist_t **spares, **l2cache; 20385450Sbrendan uint_t nspares, nl2cache; 2039789Sahrens 2040789Sahrens /* 2041789Sahrens * If a pool with this name exists, return failure. 2042789Sahrens */ 2043789Sahrens mutex_enter(&spa_namespace_lock); 2044789Sahrens if (spa_lookup(pool) != NULL) { 2045789Sahrens mutex_exit(&spa_namespace_lock); 2046789Sahrens return (EEXIST); 2047789Sahrens } 2048789Sahrens 2049789Sahrens /* 20501635Sbonwick * Create and initialize the spa structure. 2051789Sahrens */ 20525094Slling (void) nvlist_lookup_string(props, 20535094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 20541635Sbonwick spa = spa_add(pool, altroot); 2055789Sahrens spa_activate(spa); 2056789Sahrens 20576643Seschrock if (allowfaulted) 20586643Seschrock spa->spa_import_faulted = B_TRUE; 20596673Seschrock spa->spa_is_root = isroot; 20606643Seschrock 2061789Sahrens /* 20621635Sbonwick * Pass off the heavy lifting to spa_load(). 20637046Sahrens * Pass TRUE for mosconfig (unless this is a root pool) because 20647046Sahrens * the user-supplied config is actually the one to trust when 20657046Sahrens * doing an import. 20661601Sbonwick */ 20677046Sahrens loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot); 2068789Sahrens 20692082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20702082Seschrock /* 20712082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 20722082Seschrock * and conflicts with spa_has_spare(). 20732082Seschrock */ 20746423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 20755450Sbrendan nvlist_free(spa->spa_spares.sav_config); 20765450Sbrendan spa->spa_spares.sav_config = NULL; 20772082Seschrock spa_load_spares(spa); 20782082Seschrock } 20796423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 20805450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 20815450Sbrendan spa->spa_l2cache.sav_config = NULL; 20825450Sbrendan spa_load_l2cache(spa); 20835450Sbrendan } 20842082Seschrock 20852082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 20862082Seschrock &nvroot) == 0); 20875450Sbrendan if (error == 0) 20885450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 20895450Sbrendan if (error == 0) 20905450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 20915450Sbrendan VDEV_ALLOC_L2CACHE); 20922082Seschrock spa_config_exit(spa, FTAG); 20932082Seschrock 20945094Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 20956643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 20966643Seschrock /* 20976643Seschrock * If we failed to load the pool, but 'allowfaulted' is 20986643Seschrock * set, then manually set the config as if the config 20996643Seschrock * passed in was specified in the cache file. 21006643Seschrock */ 21016643Seschrock error = 0; 21026643Seschrock spa->spa_import_faulted = B_FALSE; 21036643Seschrock if (spa->spa_config == NULL) { 21046643Seschrock spa_config_enter(spa, RW_READER, FTAG); 21056643Seschrock spa->spa_config = spa_config_generate(spa, 21066643Seschrock NULL, -1ULL, B_TRUE); 21076643Seschrock spa_config_exit(spa, FTAG); 21086643Seschrock } 21096643Seschrock spa_unload(spa); 21106643Seschrock spa_deactivate(spa); 21116643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 21126643Seschrock } else { 21136643Seschrock spa_unload(spa); 21146643Seschrock spa_deactivate(spa); 21156643Seschrock spa_remove(spa); 21166643Seschrock } 2117789Sahrens mutex_exit(&spa_namespace_lock); 2118789Sahrens return (error); 2119789Sahrens } 2120789Sahrens 21211635Sbonwick /* 21225450Sbrendan * Override any spares and level 2 cache devices as specified by 21235450Sbrendan * the user, as these may have correct device names/devids, etc. 21242082Seschrock */ 21252082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 21262082Seschrock &spares, &nspares) == 0) { 21275450Sbrendan if (spa->spa_spares.sav_config) 21285450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 21292082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 21302082Seschrock else 21315450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 21322082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 21335450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 21342082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 21352082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21362082Seschrock spa_load_spares(spa); 21372082Seschrock spa_config_exit(spa, FTAG); 21385450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 21395450Sbrendan } 21405450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 21415450Sbrendan &l2cache, &nl2cache) == 0) { 21425450Sbrendan if (spa->spa_l2cache.sav_config) 21435450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 21445450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 21455450Sbrendan else 21465450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 21475450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 21485450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 21495450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21505450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 21515450Sbrendan spa_load_l2cache(spa); 21525450Sbrendan spa_config_exit(spa, FTAG); 21535450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 21542082Seschrock } 21552082Seschrock 21566643Seschrock if (spa_mode & FWRITE) { 21576643Seschrock /* 21586643Seschrock * Update the config cache to include the newly-imported pool. 21596643Seschrock */ 21606423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 21616643Seschrock } 21626643Seschrock 21636643Seschrock spa->spa_import_faulted = B_FALSE; 21644451Seschrock mutex_exit(&spa_namespace_lock); 21654451Seschrock 2166789Sahrens return (0); 2167789Sahrens } 2168789Sahrens 21696423Sgw25295 #ifdef _KERNEL 21706423Sgw25295 /* 21716423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 21726423Sgw25295 * device label. 21736423Sgw25295 */ 21746423Sgw25295 static void 21756423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 21766423Sgw25295 { 21776423Sgw25295 nvlist_t *nvtop, *nvroot; 21786423Sgw25295 uint64_t pgid; 21796423Sgw25295 21806423Sgw25295 /* 21816423Sgw25295 * Add this top-level vdev to the child array. 21826423Sgw25295 */ 21836423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 21846423Sgw25295 == 0); 21856423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 21866423Sgw25295 == 0); 21876423Sgw25295 21886423Sgw25295 /* 21896423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 21906423Sgw25295 */ 21916423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 21926423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 21936423Sgw25295 == 0); 21946423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 21956423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 21966423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 21976423Sgw25295 &nvtop, 1) == 0); 21986423Sgw25295 21996423Sgw25295 /* 22006423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 22016423Sgw25295 * this pool's configuration (remove the old, add the new). 22026423Sgw25295 */ 22036423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 22046423Sgw25295 nvlist_free(nvroot); 22056423Sgw25295 } 22066423Sgw25295 22076423Sgw25295 /* 22086423Sgw25295 * Get the root pool information from the root disk, then import the root pool 22096423Sgw25295 * during the system boot up time. 22106423Sgw25295 */ 22117147Staylor extern nvlist_t *vdev_disk_read_rootlabel(char *, char *); 22127147Staylor 22137147Staylor int 22147147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf, 22156423Sgw25295 uint64_t *besttxg) 22166423Sgw25295 { 22176423Sgw25295 nvlist_t *config; 22186423Sgw25295 uint64_t txg; 22196423Sgw25295 22207147Staylor if ((config = vdev_disk_read_rootlabel(devpath, devid)) == NULL) 22217147Staylor return (-1); 22226423Sgw25295 22236423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 22246423Sgw25295 22257147Staylor if (bestconf != NULL) 22266423Sgw25295 *bestconf = config; 22277147Staylor *besttxg = txg; 22287147Staylor return (0); 22296423Sgw25295 } 22306423Sgw25295 22316423Sgw25295 boolean_t 22326423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 22336423Sgw25295 { 22346423Sgw25295 uint64_t ival; 22356423Sgw25295 22366423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 22376423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 22386423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 22396423Sgw25295 return (B_FALSE); 22406423Sgw25295 22416423Sgw25295 return (B_TRUE); 22426423Sgw25295 } 22436423Sgw25295 22447147Staylor 22457147Staylor /* 22467147Staylor * Given the boot device's physical path or devid, check if the device 22477147Staylor * is in a valid state. If so, return the configuration from the vdev 22487147Staylor * label. 22497147Staylor */ 22507147Staylor int 22517147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf) 22527147Staylor { 22537147Staylor nvlist_t *conf = NULL; 22547147Staylor uint64_t txg = 0; 22557147Staylor nvlist_t *nvtop, **child; 22567147Staylor char *type; 22577147Staylor char *bootpath = NULL; 22587147Staylor uint_t children, c; 22597147Staylor char *tmp; 22607147Staylor 22617147Staylor if (devpath && ((tmp = strchr(devpath, ' ')) != NULL)) 22627147Staylor *tmp = '\0'; 22637147Staylor if (spa_check_rootconf(devpath, devid, &conf, &txg) < 0) { 22647147Staylor cmn_err(CE_NOTE, "error reading device label"); 22657147Staylor nvlist_free(conf); 22667147Staylor return (EINVAL); 22677147Staylor } 22687147Staylor if (txg == 0) { 22697147Staylor cmn_err(CE_NOTE, "this device is detached"); 22707147Staylor nvlist_free(conf); 22717147Staylor return (EINVAL); 22727147Staylor } 22737147Staylor 22747147Staylor VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE, 22757147Staylor &nvtop) == 0); 22767147Staylor VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0); 22777147Staylor 22787147Staylor if (strcmp(type, VDEV_TYPE_DISK) == 0) { 22797147Staylor if (spa_rootdev_validate(nvtop)) { 22807147Staylor goto out; 22817147Staylor } else { 22827147Staylor nvlist_free(conf); 22837147Staylor return (EINVAL); 22847147Staylor } 22857147Staylor } 22867147Staylor 22877147Staylor ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0); 22887147Staylor 22897147Staylor VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN, 22907147Staylor &child, &children) == 0); 22917147Staylor 22927147Staylor /* 22937147Staylor * Go thru vdevs in the mirror to see if the given device 22947147Staylor * has the most recent txg. Only the device with the most 22957147Staylor * recent txg has valid information and should be booted. 22967147Staylor */ 22977147Staylor for (c = 0; c < children; c++) { 22987147Staylor char *cdevid, *cpath; 22997147Staylor uint64_t tmptxg; 23007147Staylor 23017147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH, 23027147Staylor &cpath) != 0) 23037147Staylor return (EINVAL); 23047147Staylor if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_DEVID, 23057147Staylor &cdevid) != 0) 23067147Staylor return (EINVAL); 23077147Staylor if ((spa_check_rootconf(cpath, cdevid, NULL, 23087147Staylor &tmptxg) == 0) && (tmptxg > txg)) { 23097147Staylor txg = tmptxg; 23107147Staylor VERIFY(nvlist_lookup_string(child[c], 23117147Staylor ZPOOL_CONFIG_PATH, &bootpath) == 0); 23127147Staylor } 23137147Staylor } 23147147Staylor 23157147Staylor /* Does the best device match the one we've booted from? */ 23167147Staylor if (bootpath) { 23177147Staylor cmn_err(CE_NOTE, "try booting from '%s'", bootpath); 23187147Staylor return (EINVAL); 23197147Staylor } 23207147Staylor out: 23217147Staylor *bestconf = conf; 23227147Staylor return (0); 23237147Staylor } 23247147Staylor 23256423Sgw25295 /* 23266423Sgw25295 * Import a root pool. 23276423Sgw25295 * 23287147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 23297147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 23307147Staylor * The GRUB "findroot" command will return the vdev we should boot. 23316423Sgw25295 * 23326423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 23336423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 23346423Sgw25295 * e.g. 23356423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 23366423Sgw25295 */ 23376423Sgw25295 int 23387147Staylor spa_import_rootpool(char *devpath, char *devid) 23396423Sgw25295 { 23406423Sgw25295 nvlist_t *conf = NULL; 23416423Sgw25295 char *pname; 23426423Sgw25295 int error; 23436423Sgw25295 23446423Sgw25295 /* 23456423Sgw25295 * Get the vdev pathname and configuation from the most 23466423Sgw25295 * recently updated vdev (highest txg). 23476423Sgw25295 */ 23487147Staylor if (error = spa_get_rootconf(devpath, devid, &conf)) 23496423Sgw25295 goto msg_out; 23506423Sgw25295 23516423Sgw25295 /* 23526423Sgw25295 * Add type "root" vdev to the config. 23536423Sgw25295 */ 23546423Sgw25295 spa_build_rootpool_config(conf); 23556423Sgw25295 23566423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 23576423Sgw25295 23586673Seschrock /* 23596673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 23606673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 23616673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 23626673Seschrock * pool open, not import. 23636673Seschrock */ 23646673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 23656423Sgw25295 if (error == EEXIST) 23666423Sgw25295 error = 0; 23676423Sgw25295 23686423Sgw25295 nvlist_free(conf); 23696423Sgw25295 return (error); 23706423Sgw25295 23716423Sgw25295 msg_out: 23727147Staylor cmn_err(CE_NOTE, "\n" 23736423Sgw25295 " *************************************************** \n" 23746423Sgw25295 " * This device is not bootable! * \n" 23756423Sgw25295 " * It is either offlined or detached or faulted. * \n" 23766423Sgw25295 " * Please try to boot from a different device. * \n" 23777147Staylor " *************************************************** "); 23786423Sgw25295 23796423Sgw25295 return (error); 23806423Sgw25295 } 23816423Sgw25295 #endif 23826423Sgw25295 23836423Sgw25295 /* 23846423Sgw25295 * Import a non-root pool into the system. 23856423Sgw25295 */ 23866423Sgw25295 int 23876423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 23886423Sgw25295 { 23896643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 23906423Sgw25295 } 23916423Sgw25295 23926643Seschrock int 23936643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 23946643Seschrock { 23956643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 23966643Seschrock } 23976643Seschrock 23986643Seschrock 2399789Sahrens /* 2400789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2401789Sahrens * to get the vdev stats associated with the imported devices. 2402789Sahrens */ 2403789Sahrens #define TRYIMPORT_NAME "$import" 2404789Sahrens 2405789Sahrens nvlist_t * 2406789Sahrens spa_tryimport(nvlist_t *tryconfig) 2407789Sahrens { 2408789Sahrens nvlist_t *config = NULL; 2409789Sahrens char *poolname; 2410789Sahrens spa_t *spa; 2411789Sahrens uint64_t state; 2412789Sahrens 2413789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2414789Sahrens return (NULL); 2415789Sahrens 2416789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2417789Sahrens return (NULL); 2418789Sahrens 24191635Sbonwick /* 24201635Sbonwick * Create and initialize the spa structure. 24211635Sbonwick */ 2422789Sahrens mutex_enter(&spa_namespace_lock); 24231635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2424789Sahrens spa_activate(spa); 2425789Sahrens 2426789Sahrens /* 24271635Sbonwick * Pass off the heavy lifting to spa_load(). 24281732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 24291732Sbonwick * is actually the one to trust when doing an import. 2430789Sahrens */ 24311732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2432789Sahrens 2433789Sahrens /* 2434789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2435789Sahrens */ 2436789Sahrens if (spa->spa_root_vdev != NULL) { 24371635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 2438789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 24391635Sbonwick spa_config_exit(spa, FTAG); 2440789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2441789Sahrens poolname) == 0); 2442789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2443789Sahrens state) == 0); 24443975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 24453975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 24462082Seschrock 24472082Seschrock /* 24486423Sgw25295 * If the bootfs property exists on this pool then we 24496423Sgw25295 * copy it out so that external consumers can tell which 24506423Sgw25295 * pools are bootable. 24516423Sgw25295 */ 24526423Sgw25295 if (spa->spa_bootfs) { 24536423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 24546423Sgw25295 24556423Sgw25295 /* 24566423Sgw25295 * We have to play games with the name since the 24576423Sgw25295 * pool was opened as TRYIMPORT_NAME. 24586423Sgw25295 */ 24596423Sgw25295 if (dsl_dsobj_to_dsname(spa->spa_name, 24606423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 24616423Sgw25295 char *cp; 24626423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 24636423Sgw25295 24646423Sgw25295 cp = strchr(tmpname, '/'); 24656423Sgw25295 if (cp == NULL) { 24666423Sgw25295 (void) strlcpy(dsname, tmpname, 24676423Sgw25295 MAXPATHLEN); 24686423Sgw25295 } else { 24696423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 24706423Sgw25295 "%s/%s", poolname, ++cp); 24716423Sgw25295 } 24726423Sgw25295 VERIFY(nvlist_add_string(config, 24736423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 24746423Sgw25295 kmem_free(dsname, MAXPATHLEN); 24756423Sgw25295 } 24766423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 24776423Sgw25295 } 24786423Sgw25295 24796423Sgw25295 /* 24805450Sbrendan * Add the list of hot spares and level 2 cache devices. 24812082Seschrock */ 24822082Seschrock spa_add_spares(spa, config); 24835450Sbrendan spa_add_l2cache(spa, config); 2484789Sahrens } 2485789Sahrens 2486789Sahrens spa_unload(spa); 2487789Sahrens spa_deactivate(spa); 2488789Sahrens spa_remove(spa); 2489789Sahrens mutex_exit(&spa_namespace_lock); 2490789Sahrens 2491789Sahrens return (config); 2492789Sahrens } 2493789Sahrens 2494789Sahrens /* 2495789Sahrens * Pool export/destroy 2496789Sahrens * 2497789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2498789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2499789Sahrens * update the pool state and sync all the labels to disk, removing the 2500789Sahrens * configuration from the cache afterwards. 2501789Sahrens */ 2502789Sahrens static int 2503*7214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 2504*7214Slling boolean_t force) 2505789Sahrens { 2506789Sahrens spa_t *spa; 2507789Sahrens 25081775Sbillm if (oldconfig) 25091775Sbillm *oldconfig = NULL; 25101775Sbillm 2511789Sahrens if (!(spa_mode & FWRITE)) 2512789Sahrens return (EROFS); 2513789Sahrens 2514789Sahrens mutex_enter(&spa_namespace_lock); 2515789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2516789Sahrens mutex_exit(&spa_namespace_lock); 2517789Sahrens return (ENOENT); 2518789Sahrens } 2519789Sahrens 2520789Sahrens /* 25211544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 25221544Seschrock * reacquire the namespace lock, and see if we can export. 25231544Seschrock */ 25241544Seschrock spa_open_ref(spa, FTAG); 25251544Seschrock mutex_exit(&spa_namespace_lock); 25261544Seschrock spa_async_suspend(spa); 25271544Seschrock mutex_enter(&spa_namespace_lock); 25281544Seschrock spa_close(spa, FTAG); 25291544Seschrock 25301544Seschrock /* 2531789Sahrens * The pool will be in core if it's openable, 2532789Sahrens * in which case we can modify its state. 2533789Sahrens */ 2534789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2535789Sahrens /* 2536789Sahrens * Objsets may be open only because they're dirty, so we 2537789Sahrens * have to force it to sync before checking spa_refcnt. 2538789Sahrens */ 2539789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2540789Sahrens 25411544Seschrock /* 25421544Seschrock * A pool cannot be exported or destroyed if there are active 25431544Seschrock * references. If we are resetting a pool, allow references by 25441544Seschrock * fault injection handlers. 25451544Seschrock */ 25461544Seschrock if (!spa_refcount_zero(spa) || 25471544Seschrock (spa->spa_inject_ref != 0 && 25481544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 25491544Seschrock spa_async_resume(spa); 2550789Sahrens mutex_exit(&spa_namespace_lock); 2551789Sahrens return (EBUSY); 2552789Sahrens } 2553789Sahrens 2554789Sahrens /* 2555*7214Slling * A pool cannot be exported if it has an active shared spare. 2556*7214Slling * This is to prevent other pools stealing the active spare 2557*7214Slling * from an exported pool. At user's own will, such pool can 2558*7214Slling * be forcedly exported. 2559*7214Slling */ 2560*7214Slling if (!force && new_state == POOL_STATE_EXPORTED && 2561*7214Slling spa_has_active_shared_spare(spa)) { 2562*7214Slling spa_async_resume(spa); 2563*7214Slling mutex_exit(&spa_namespace_lock); 2564*7214Slling return (EXDEV); 2565*7214Slling } 2566*7214Slling 2567*7214Slling /* 2568789Sahrens * We want this to be reflected on every label, 2569789Sahrens * so mark them all dirty. spa_unload() will do the 2570789Sahrens * final sync that pushes these changes out. 2571789Sahrens */ 25721544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 25731601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 25741544Seschrock spa->spa_state = new_state; 25751635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 25761544Seschrock vdev_config_dirty(spa->spa_root_vdev); 25771601Sbonwick spa_config_exit(spa, FTAG); 25781544Seschrock } 2579789Sahrens } 2580789Sahrens 25814451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 25824451Seschrock 2583789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2584789Sahrens spa_unload(spa); 2585789Sahrens spa_deactivate(spa); 2586789Sahrens } 2587789Sahrens 25881775Sbillm if (oldconfig && spa->spa_config) 25891775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 25901775Sbillm 25911544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 25926643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 25931544Seschrock spa_remove(spa); 25941544Seschrock } 2595789Sahrens mutex_exit(&spa_namespace_lock); 2596789Sahrens 2597789Sahrens return (0); 2598789Sahrens } 2599789Sahrens 2600789Sahrens /* 2601789Sahrens * Destroy a storage pool. 2602789Sahrens */ 2603789Sahrens int 2604789Sahrens spa_destroy(char *pool) 2605789Sahrens { 2606*7214Slling return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, B_FALSE)); 2607789Sahrens } 2608789Sahrens 2609789Sahrens /* 2610789Sahrens * Export a storage pool. 2611789Sahrens */ 2612789Sahrens int 2613*7214Slling spa_export(char *pool, nvlist_t **oldconfig, boolean_t force) 2614789Sahrens { 2615*7214Slling return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, force)); 2616789Sahrens } 2617789Sahrens 2618789Sahrens /* 26191544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 26201544Seschrock * from the namespace in any way. 26211544Seschrock */ 26221544Seschrock int 26231544Seschrock spa_reset(char *pool) 26241544Seschrock { 2625*7214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 2626*7214Slling B_FALSE)); 26271544Seschrock } 26281544Seschrock 26291544Seschrock /* 2630789Sahrens * ========================================================================== 2631789Sahrens * Device manipulation 2632789Sahrens * ========================================================================== 2633789Sahrens */ 2634789Sahrens 2635789Sahrens /* 26364527Sperrin * Add a device to a storage pool. 2637789Sahrens */ 2638789Sahrens int 2639789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2640789Sahrens { 2641789Sahrens uint64_t txg; 26421635Sbonwick int c, error; 2643789Sahrens vdev_t *rvd = spa->spa_root_vdev; 26441585Sbonwick vdev_t *vd, *tvd; 26455450Sbrendan nvlist_t **spares, **l2cache; 26465450Sbrendan uint_t nspares, nl2cache; 2647789Sahrens 2648789Sahrens txg = spa_vdev_enter(spa); 2649789Sahrens 26502082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 26512082Seschrock VDEV_ALLOC_ADD)) != 0) 26522082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 26532082Seschrock 26543377Seschrock spa->spa_pending_vdev = vd; 2655789Sahrens 26565450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 26575450Sbrendan &nspares) != 0) 26582082Seschrock nspares = 0; 26592082Seschrock 26605450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 26615450Sbrendan &nl2cache) != 0) 26625450Sbrendan nl2cache = 0; 26635450Sbrendan 26645450Sbrendan if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) { 26653377Seschrock spa->spa_pending_vdev = NULL; 26662082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 26673377Seschrock } 26682082Seschrock 26692082Seschrock if (vd->vdev_children != 0) { 26703377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 26713377Seschrock spa->spa_pending_vdev = NULL; 26722082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 26732082Seschrock } 26742082Seschrock } 26752082Seschrock 26763377Seschrock /* 26775450Sbrendan * We must validate the spares and l2cache devices after checking the 26785450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 26793377Seschrock */ 26805450Sbrendan if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) { 26813377Seschrock spa->spa_pending_vdev = NULL; 26823377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 26833377Seschrock } 26843377Seschrock 26853377Seschrock spa->spa_pending_vdev = NULL; 26863377Seschrock 26873377Seschrock /* 26883377Seschrock * Transfer each new top-level vdev from vd to rvd. 26893377Seschrock */ 26903377Seschrock for (c = 0; c < vd->vdev_children; c++) { 26913377Seschrock tvd = vd->vdev_child[c]; 26923377Seschrock vdev_remove_child(vd, tvd); 26933377Seschrock tvd->vdev_id = rvd->vdev_children; 26943377Seschrock vdev_add_child(rvd, tvd); 26953377Seschrock vdev_config_dirty(tvd); 26963377Seschrock } 26973377Seschrock 26982082Seschrock if (nspares != 0) { 26995450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 27005450Sbrendan ZPOOL_CONFIG_SPARES); 27012082Seschrock spa_load_spares(spa); 27025450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 27035450Sbrendan } 27045450Sbrendan 27055450Sbrendan if (nl2cache != 0) { 27065450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 27075450Sbrendan ZPOOL_CONFIG_L2CACHE); 27085450Sbrendan spa_load_l2cache(spa); 27095450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2710789Sahrens } 2711789Sahrens 2712789Sahrens /* 27131585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 27141585Sbonwick * If other threads start allocating from these vdevs before we 27151585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 27161585Sbonwick * fail to open the pool because there are DVAs that the config cache 27171585Sbonwick * can't translate. Therefore, we first add the vdevs without 27181585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 27191635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 27201585Sbonwick * 27211585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 27221585Sbonwick * if we lose power at any point in this sequence, the remaining 27231585Sbonwick * steps will be completed the next time we load the pool. 2724789Sahrens */ 27251635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 27261585Sbonwick 27271635Sbonwick mutex_enter(&spa_namespace_lock); 27281635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 27291635Sbonwick mutex_exit(&spa_namespace_lock); 2730789Sahrens 27311635Sbonwick return (0); 2732789Sahrens } 2733789Sahrens 2734789Sahrens /* 2735789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2736789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2737789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2738789Sahrens * 2739789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2740789Sahrens * existing device; in this case the two devices are made into their own 27414451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2742789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2743789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2744789Sahrens * completion of resilvering, the first disk (the one being replaced) 2745789Sahrens * is automatically detached. 2746789Sahrens */ 2747789Sahrens int 27481544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2749789Sahrens { 2750789Sahrens uint64_t txg, open_txg; 2751789Sahrens int error; 2752789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2753789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 27542082Seschrock vdev_ops_t *pvops; 27554527Sperrin int is_log; 2756789Sahrens 2757789Sahrens txg = spa_vdev_enter(spa); 2758789Sahrens 27596643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2760789Sahrens 2761789Sahrens if (oldvd == NULL) 2762789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2763789Sahrens 27641585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 27651585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 27661585Sbonwick 2767789Sahrens pvd = oldvd->vdev_parent; 2768789Sahrens 27692082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 27704451Seschrock VDEV_ALLOC_ADD)) != 0) 27714451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 27724451Seschrock 27734451Seschrock if (newrootvd->vdev_children != 1) 2774789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2775789Sahrens 2776789Sahrens newvd = newrootvd->vdev_child[0]; 2777789Sahrens 2778789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2779789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2780789Sahrens 27812082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2782789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2783789Sahrens 27844527Sperrin /* 27854527Sperrin * Spares can't replace logs 27864527Sperrin */ 27874527Sperrin is_log = oldvd->vdev_islog; 27884527Sperrin if (is_log && newvd->vdev_isspare) 27894527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27904527Sperrin 27912082Seschrock if (!replacing) { 27922082Seschrock /* 27932082Seschrock * For attach, the only allowable parent is a mirror or the root 27942082Seschrock * vdev. 27952082Seschrock */ 27962082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 27972082Seschrock pvd->vdev_ops != &vdev_root_ops) 27982082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27992082Seschrock 28002082Seschrock pvops = &vdev_mirror_ops; 28012082Seschrock } else { 28022082Seschrock /* 28032082Seschrock * Active hot spares can only be replaced by inactive hot 28042082Seschrock * spares. 28052082Seschrock */ 28062082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 28072082Seschrock pvd->vdev_child[1] == oldvd && 28082082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 28092082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28102082Seschrock 28112082Seschrock /* 28122082Seschrock * If the source is a hot spare, and the parent isn't already a 28132082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 28143377Seschrock * want to create a replacing vdev. The user is not allowed to 28153377Seschrock * attach to a spared vdev child unless the 'isspare' state is 28163377Seschrock * the same (spare replaces spare, non-spare replaces 28173377Seschrock * non-spare). 28182082Seschrock */ 28192082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 28202082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28213377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 28223377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 28233377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 28242082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 28252082Seschrock newvd->vdev_isspare) 28262082Seschrock pvops = &vdev_spare_ops; 28272082Seschrock else 28282082Seschrock pvops = &vdev_replacing_ops; 28292082Seschrock } 28302082Seschrock 28311175Slling /* 28321175Slling * Compare the new device size with the replaceable/attachable 28331175Slling * device size. 28341175Slling */ 28351175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2836789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2837789Sahrens 28381732Sbonwick /* 28391732Sbonwick * The new device cannot have a higher alignment requirement 28401732Sbonwick * than the top-level vdev. 28411732Sbonwick */ 28421732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2843789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2844789Sahrens 2845789Sahrens /* 2846789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2847789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2848789Sahrens */ 2849789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2850789Sahrens spa_strfree(oldvd->vdev_path); 2851789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2852789Sahrens KM_SLEEP); 2853789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2854789Sahrens newvd->vdev_path, "old"); 2855789Sahrens if (oldvd->vdev_devid != NULL) { 2856789Sahrens spa_strfree(oldvd->vdev_devid); 2857789Sahrens oldvd->vdev_devid = NULL; 2858789Sahrens } 2859789Sahrens } 2860789Sahrens 2861789Sahrens /* 28622082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 28632082Seschrock * mirror/replacing/spare vdev above oldvd. 2864789Sahrens */ 2865789Sahrens if (pvd->vdev_ops != pvops) 2866789Sahrens pvd = vdev_add_parent(oldvd, pvops); 2867789Sahrens 2868789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2869789Sahrens ASSERT(pvd->vdev_ops == pvops); 2870789Sahrens ASSERT(oldvd->vdev_parent == pvd); 2871789Sahrens 2872789Sahrens /* 2873789Sahrens * Extract the new device from its root and add it to pvd. 2874789Sahrens */ 2875789Sahrens vdev_remove_child(newrootvd, newvd); 2876789Sahrens newvd->vdev_id = pvd->vdev_children; 2877789Sahrens vdev_add_child(pvd, newvd); 2878789Sahrens 28791544Seschrock /* 28801544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 28811544Seschrock * the addition of newvd may have decreased our parent's asize. 28821544Seschrock */ 28831544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 28841544Seschrock 2885789Sahrens tvd = newvd->vdev_top; 2886789Sahrens ASSERT(pvd->vdev_top == tvd); 2887789Sahrens ASSERT(tvd->vdev_parent == rvd); 2888789Sahrens 2889789Sahrens vdev_config_dirty(tvd); 2890789Sahrens 2891789Sahrens /* 2892789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2893789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2894789Sahrens */ 2895789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2896789Sahrens 2897789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2898789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2899789Sahrens open_txg - TXG_INITIAL + 1); 2900789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2901789Sahrens 29023377Seschrock if (newvd->vdev_isspare) 29033377Seschrock spa_spare_activate(newvd); 29041544Seschrock 2905789Sahrens /* 2906789Sahrens * Mark newvd's DTL dirty in this txg. 2907789Sahrens */ 29081732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2909789Sahrens 2910789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2911789Sahrens 2912789Sahrens /* 29137046Sahrens * Kick off a resilver to update newvd. 2914789Sahrens */ 29157046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 2916789Sahrens 2917789Sahrens return (0); 2918789Sahrens } 2919789Sahrens 2920789Sahrens /* 2921789Sahrens * Detach a device from a mirror or replacing vdev. 2922789Sahrens * If 'replace_done' is specified, only detach if the parent 2923789Sahrens * is a replacing vdev. 2924789Sahrens */ 2925789Sahrens int 29261544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2927789Sahrens { 2928789Sahrens uint64_t txg; 2929789Sahrens int c, t, error; 2930789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2931789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 29322082Seschrock boolean_t unspare = B_FALSE; 29332082Seschrock uint64_t unspare_guid; 29346673Seschrock size_t len; 2935789Sahrens 2936789Sahrens txg = spa_vdev_enter(spa); 2937789Sahrens 29386643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 2939789Sahrens 2940789Sahrens if (vd == NULL) 2941789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2942789Sahrens 29431585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 29441585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 29451585Sbonwick 2946789Sahrens pvd = vd->vdev_parent; 2947789Sahrens 2948789Sahrens /* 2949789Sahrens * If replace_done is specified, only remove this device if it's 29502082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 29512082Seschrock * disk can be removed. 2952789Sahrens */ 29532082Seschrock if (replace_done) { 29542082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 29552082Seschrock if (vd->vdev_id != 0) 29562082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 29572082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 29582082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 29592082Seschrock } 29602082Seschrock } 29612082Seschrock 29622082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 29634577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 2964789Sahrens 2965789Sahrens /* 29662082Seschrock * Only mirror, replacing, and spare vdevs support detach. 2967789Sahrens */ 2968789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 29692082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 29702082Seschrock pvd->vdev_ops != &vdev_spare_ops) 2971789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2972789Sahrens 2973789Sahrens /* 2974789Sahrens * If there's only one replica, you can't detach it. 2975789Sahrens */ 2976789Sahrens if (pvd->vdev_children <= 1) 2977789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2978789Sahrens 2979789Sahrens /* 2980789Sahrens * If all siblings have non-empty DTLs, this device may have the only 2981789Sahrens * valid copy of the data, which means we cannot safely detach it. 2982789Sahrens * 2983789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 2984789Sahrens * precise DTL check. 2985789Sahrens */ 2986789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 2987789Sahrens uint64_t dirty; 2988789Sahrens 2989789Sahrens cvd = pvd->vdev_child[c]; 2990789Sahrens if (cvd == vd) 2991789Sahrens continue; 2992789Sahrens if (vdev_is_dead(cvd)) 2993789Sahrens continue; 2994789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 2995789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 2996789Sahrens cvd->vdev_dtl_scrub.sm_space; 2997789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 2998789Sahrens if (!dirty) 2999789Sahrens break; 3000789Sahrens } 30012082Seschrock 30022082Seschrock /* 30032082Seschrock * If we are a replacing or spare vdev, then we can always detach the 30042082Seschrock * latter child, as that is how one cancels the operation. 30052082Seschrock */ 30062082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 30072082Seschrock c == pvd->vdev_children) 3008789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3009789Sahrens 3010789Sahrens /* 30116673Seschrock * If we are detaching the second disk from a replacing vdev, then 30126673Seschrock * check to see if we changed the original vdev's path to have "/old" 30136673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 30146673Seschrock */ 30156673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 30166673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 30176673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 30186673Seschrock ASSERT(pvd->vdev_child[1] == vd); 30196673Seschrock cvd = pvd->vdev_child[0]; 30206673Seschrock len = strlen(vd->vdev_path); 30216673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 30226673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 30236673Seschrock spa_strfree(cvd->vdev_path); 30246673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 30256673Seschrock } 30266673Seschrock } 30276673Seschrock 30286673Seschrock /* 30292082Seschrock * If we are detaching the original disk from a spare, then it implies 30302082Seschrock * that the spare should become a real disk, and be removed from the 30312082Seschrock * active spare list for the pool. 30322082Seschrock */ 30332082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 30342082Seschrock vd->vdev_id == 0) 30352082Seschrock unspare = B_TRUE; 30362082Seschrock 30372082Seschrock /* 3038789Sahrens * Erase the disk labels so the disk can be used for other things. 3039789Sahrens * This must be done after all other error cases are handled, 3040789Sahrens * but before we disembowel vd (so we can still do I/O to it). 3041789Sahrens * But if we can't do it, don't treat the error as fatal -- 3042789Sahrens * it may be that the unwritability of the disk is the reason 3043789Sahrens * it's being detached! 3044789Sahrens */ 30453377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3046789Sahrens 3047789Sahrens /* 3048789Sahrens * Remove vd from its parent and compact the parent's children. 3049789Sahrens */ 3050789Sahrens vdev_remove_child(pvd, vd); 3051789Sahrens vdev_compact_children(pvd); 3052789Sahrens 3053789Sahrens /* 3054789Sahrens * Remember one of the remaining children so we can get tvd below. 3055789Sahrens */ 3056789Sahrens cvd = pvd->vdev_child[0]; 3057789Sahrens 3058789Sahrens /* 30592082Seschrock * If we need to remove the remaining child from the list of hot spares, 30602082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 30612082Seschrock * must do this before vdev_remove_parent(), because that can change the 30622082Seschrock * GUID if it creates a new toplevel GUID. 30632082Seschrock */ 30642082Seschrock if (unspare) { 30652082Seschrock ASSERT(cvd->vdev_isspare); 30663377Seschrock spa_spare_remove(cvd); 30672082Seschrock unspare_guid = cvd->vdev_guid; 30682082Seschrock } 30692082Seschrock 30702082Seschrock /* 3071789Sahrens * If the parent mirror/replacing vdev only has one child, 3072789Sahrens * the parent is no longer needed. Remove it from the tree. 3073789Sahrens */ 3074789Sahrens if (pvd->vdev_children == 1) 3075789Sahrens vdev_remove_parent(cvd); 3076789Sahrens 3077789Sahrens /* 3078789Sahrens * We don't set tvd until now because the parent we just removed 3079789Sahrens * may have been the previous top-level vdev. 3080789Sahrens */ 3081789Sahrens tvd = cvd->vdev_top; 3082789Sahrens ASSERT(tvd->vdev_parent == rvd); 3083789Sahrens 3084789Sahrens /* 30853377Seschrock * Reevaluate the parent vdev state. 3086789Sahrens */ 30874451Seschrock vdev_propagate_state(cvd); 3088789Sahrens 3089789Sahrens /* 30903377Seschrock * If the device we just detached was smaller than the others, it may be 30913377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 30923377Seschrock * can't fail because the existing metaslabs are already in core, so 30933377Seschrock * there's nothing to read from disk. 3094789Sahrens */ 30951732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3096789Sahrens 3097789Sahrens vdev_config_dirty(tvd); 3098789Sahrens 3099789Sahrens /* 31003377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 31013377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 31023377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 31033377Seschrock * prevent vd from being accessed after it's freed. 3104789Sahrens */ 3105789Sahrens for (t = 0; t < TXG_SIZE; t++) 3106789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 31071732Sbonwick vd->vdev_detached = B_TRUE; 31081732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3109789Sahrens 31104451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 31114451Seschrock 31122082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 31132082Seschrock 31142082Seschrock /* 31153377Seschrock * If this was the removal of the original device in a hot spare vdev, 31163377Seschrock * then we want to go through and remove the device from the hot spare 31173377Seschrock * list of every other pool. 31182082Seschrock */ 31192082Seschrock if (unspare) { 31202082Seschrock spa = NULL; 31212082Seschrock mutex_enter(&spa_namespace_lock); 31222082Seschrock while ((spa = spa_next(spa)) != NULL) { 31232082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 31242082Seschrock continue; 31252082Seschrock 31262082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 31272082Seschrock } 31282082Seschrock mutex_exit(&spa_namespace_lock); 31292082Seschrock } 31302082Seschrock 31312082Seschrock return (error); 31322082Seschrock } 31332082Seschrock 31342082Seschrock /* 31355450Sbrendan * Remove a spares vdev from the nvlist config. 31362082Seschrock */ 31375450Sbrendan static int 31385450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare, 31395450Sbrendan nvlist_t **spares, int nspares, vdev_t *vd) 31402082Seschrock { 31415450Sbrendan nvlist_t *nv, **newspares; 31425450Sbrendan int i, j; 31432082Seschrock 31442082Seschrock nv = NULL; 31455450Sbrendan for (i = 0; i < nspares; i++) { 31465450Sbrendan uint64_t theguid; 31475450Sbrendan 31485450Sbrendan VERIFY(nvlist_lookup_uint64(spares[i], 31495450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 31505450Sbrendan if (theguid == guid) { 31515450Sbrendan nv = spares[i]; 31525450Sbrendan break; 31532082Seschrock } 31542082Seschrock } 31552082Seschrock 31562082Seschrock /* 31575450Sbrendan * Only remove the hot spare if it's not currently in use in this pool. 31582082Seschrock */ 31595450Sbrendan if (nv == NULL && vd == NULL) 31605450Sbrendan return (ENOENT); 31615450Sbrendan 31625450Sbrendan if (nv == NULL && vd != NULL) 31635450Sbrendan return (ENOTSUP); 31645450Sbrendan 31655450Sbrendan if (!unspare && nv != NULL && vd != NULL) 31665450Sbrendan return (EBUSY); 31672082Seschrock 31682082Seschrock if (nspares == 1) { 31692082Seschrock newspares = NULL; 31702082Seschrock } else { 31712082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 31722082Seschrock KM_SLEEP); 31732082Seschrock for (i = 0, j = 0; i < nspares; i++) { 31742082Seschrock if (spares[i] != nv) 31752082Seschrock VERIFY(nvlist_dup(spares[i], 31762082Seschrock &newspares[j++], KM_SLEEP) == 0); 31772082Seschrock } 31782082Seschrock } 31792082Seschrock 31805450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES, 31812082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 31825450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 31835450Sbrendan ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0); 31842082Seschrock for (i = 0; i < nspares - 1; i++) 31852082Seschrock nvlist_free(newspares[i]); 31862082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 31875450Sbrendan 31885450Sbrendan return (0); 31895450Sbrendan } 31905450Sbrendan 31915450Sbrendan /* 31925450Sbrendan * Remove an l2cache vdev from the nvlist config. 31935450Sbrendan */ 31945450Sbrendan static int 31955450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache, 31965450Sbrendan int nl2cache, vdev_t *vd) 31975450Sbrendan { 31985450Sbrendan nvlist_t *nv, **newl2cache; 31995450Sbrendan int i, j; 32005450Sbrendan 32015450Sbrendan nv = NULL; 32025450Sbrendan for (i = 0; i < nl2cache; i++) { 32035450Sbrendan uint64_t theguid; 32045450Sbrendan 32055450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 32065450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 32075450Sbrendan if (theguid == guid) { 32085450Sbrendan nv = l2cache[i]; 32095450Sbrendan break; 32105450Sbrendan } 32115450Sbrendan } 32125450Sbrendan 32135450Sbrendan if (vd == NULL) { 32145450Sbrendan for (i = 0; i < nl2cache; i++) { 32155450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) { 32165450Sbrendan vd = sav->sav_vdevs[i]; 32175450Sbrendan break; 32185450Sbrendan } 32195450Sbrendan } 32205450Sbrendan } 32215450Sbrendan 32225450Sbrendan if (nv == NULL && vd == NULL) 32235450Sbrendan return (ENOENT); 32245450Sbrendan 32255450Sbrendan if (nv == NULL && vd != NULL) 32265450Sbrendan return (ENOTSUP); 32275450Sbrendan 32285450Sbrendan if (nl2cache == 1) { 32295450Sbrendan newl2cache = NULL; 32305450Sbrendan } else { 32315450Sbrendan newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *), 32325450Sbrendan KM_SLEEP); 32335450Sbrendan for (i = 0, j = 0; i < nl2cache; i++) { 32345450Sbrendan if (l2cache[i] != nv) 32355450Sbrendan VERIFY(nvlist_dup(l2cache[i], 32365450Sbrendan &newl2cache[j++], KM_SLEEP) == 0); 32375450Sbrendan } 32385450Sbrendan } 32395450Sbrendan 32405450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 32415450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 32425450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 32435450Sbrendan ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0); 32445450Sbrendan for (i = 0; i < nl2cache - 1; i++) 32455450Sbrendan nvlist_free(newl2cache[i]); 32465450Sbrendan kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *)); 32475450Sbrendan 32485450Sbrendan return (0); 32495450Sbrendan } 32505450Sbrendan 32515450Sbrendan /* 32525450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 32535450Sbrendan * spares and level 2 ARC devices. 32545450Sbrendan */ 32555450Sbrendan int 32565450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 32575450Sbrendan { 32585450Sbrendan vdev_t *vd; 32595450Sbrendan nvlist_t **spares, **l2cache; 32605450Sbrendan uint_t nspares, nl2cache; 32615450Sbrendan int error = 0; 32625450Sbrendan 32635450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 32645450Sbrendan 32656643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 32665450Sbrendan 32675450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 32685450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 32695450Sbrendan ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 32705450Sbrendan if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare, 32715450Sbrendan spares, nspares, vd)) != 0) 32725450Sbrendan goto out; 32735450Sbrendan spa_load_spares(spa); 32745450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 32755450Sbrendan goto out; 32765450Sbrendan } 32775450Sbrendan 32785450Sbrendan if (spa->spa_l2cache.sav_vdevs != NULL && 32795450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 32805450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) { 32815450Sbrendan if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid, 32825450Sbrendan l2cache, nl2cache, vd)) != 0) 32835450Sbrendan goto out; 32845450Sbrendan spa_load_l2cache(spa); 32855450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 32865450Sbrendan } 32872082Seschrock 32882082Seschrock out: 32892082Seschrock spa_config_exit(spa, FTAG); 32905450Sbrendan return (error); 3291789Sahrens } 3292789Sahrens 3293789Sahrens /* 32944451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 32954451Seschrock * current spared, so we can detach it. 3296789Sahrens */ 32971544Seschrock static vdev_t * 32984451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3299789Sahrens { 33001544Seschrock vdev_t *newvd, *oldvd; 3301789Sahrens int c; 3302789Sahrens 33031544Seschrock for (c = 0; c < vd->vdev_children; c++) { 33044451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 33051544Seschrock if (oldvd != NULL) 33061544Seschrock return (oldvd); 33071544Seschrock } 3308789Sahrens 33094451Seschrock /* 33104451Seschrock * Check for a completed replacement. 33114451Seschrock */ 3312789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 33131544Seschrock oldvd = vd->vdev_child[0]; 33141544Seschrock newvd = vd->vdev_child[1]; 3315789Sahrens 33161544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 33171544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 33181544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 33191544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33201544Seschrock return (oldvd); 33211544Seschrock } 33221544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33231544Seschrock } 3324789Sahrens 33254451Seschrock /* 33264451Seschrock * Check for a completed resilver with the 'unspare' flag set. 33274451Seschrock */ 33284451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 33294451Seschrock newvd = vd->vdev_child[0]; 33304451Seschrock oldvd = vd->vdev_child[1]; 33314451Seschrock 33324451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 33334451Seschrock if (newvd->vdev_unspare && 33344451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 33354451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 33364451Seschrock newvd->vdev_unspare = 0; 33374451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33384451Seschrock return (oldvd); 33394451Seschrock } 33404451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 33414451Seschrock } 33424451Seschrock 33431544Seschrock return (NULL); 3344789Sahrens } 3345789Sahrens 33461544Seschrock static void 33474451Seschrock spa_vdev_resilver_done(spa_t *spa) 3348789Sahrens { 33491544Seschrock vdev_t *vd; 33502082Seschrock vdev_t *pvd; 33511544Seschrock uint64_t guid; 33522082Seschrock uint64_t pguid = 0; 3353789Sahrens 33541544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3355789Sahrens 33564451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 33571544Seschrock guid = vd->vdev_guid; 33582082Seschrock /* 33592082Seschrock * If we have just finished replacing a hot spared device, then 33602082Seschrock * we need to detach the parent's first child (the original hot 33612082Seschrock * spare) as well. 33622082Seschrock */ 33632082Seschrock pvd = vd->vdev_parent; 33642082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 33652082Seschrock pvd->vdev_id == 0) { 33662082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 33672082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 33682082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 33692082Seschrock } 33701544Seschrock spa_config_exit(spa, FTAG); 33711544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 33721544Seschrock return; 33732082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 33742082Seschrock return; 33751544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3376789Sahrens } 3377789Sahrens 33781544Seschrock spa_config_exit(spa, FTAG); 3379789Sahrens } 3380789Sahrens 3381789Sahrens /* 33821354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 33831354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 33841354Seschrock */ 33851354Seschrock int 33861354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 33871354Seschrock { 33886643Seschrock vdev_t *vd; 33891354Seschrock uint64_t txg; 33901354Seschrock 33911354Seschrock txg = spa_vdev_enter(spa); 33921354Seschrock 33936643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 33942082Seschrock /* 33956643Seschrock * Determine if this is a reference to a hot spare device. If 33966643Seschrock * it is, update the path manually as there is no associated 33976643Seschrock * vdev_t that can be synced to disk. 33982082Seschrock */ 33996643Seschrock nvlist_t **spares; 34006643Seschrock uint_t i, nspares; 34015450Sbrendan 34025450Sbrendan if (spa->spa_spares.sav_config != NULL) { 34035450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 34045450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 34055450Sbrendan &spares, &nspares) == 0); 34062082Seschrock for (i = 0; i < nspares; i++) { 34072082Seschrock uint64_t theguid; 34082082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 34092082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 34105450Sbrendan if (theguid == guid) { 34115450Sbrendan VERIFY(nvlist_add_string(spares[i], 34125450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 34135450Sbrendan spa_load_spares(spa); 34145450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 34155450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 34165450Sbrendan 0)); 34175450Sbrendan } 34182082Seschrock } 34192082Seschrock } 34205450Sbrendan 34215450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 34222082Seschrock } 34231354Seschrock 34241585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 34251585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 34261585Sbonwick 34271354Seschrock spa_strfree(vd->vdev_path); 34281354Seschrock vd->vdev_path = spa_strdup(newpath); 34291354Seschrock 34301354Seschrock vdev_config_dirty(vd->vdev_top); 34311354Seschrock 34321354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 34331354Seschrock } 34341354Seschrock 34351354Seschrock /* 3436789Sahrens * ========================================================================== 3437789Sahrens * SPA Scrubbing 3438789Sahrens * ========================================================================== 3439789Sahrens */ 3440789Sahrens 34417046Sahrens int 34427046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3443789Sahrens { 34444808Sek110237 ASSERT(!spa_config_held(spa, RW_WRITER)); 34454808Sek110237 3446789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3447789Sahrens return (ENOTSUP); 3448789Sahrens 3449789Sahrens /* 34507046Sahrens * If a resilver was requested, but there is no DTL on a 34517046Sahrens * writeable leaf device, we have nothing to do. 3452789Sahrens */ 34537046Sahrens if (type == POOL_SCRUB_RESILVER && 34547046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 34557046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 34561544Seschrock return (0); 34571544Seschrock } 3458789Sahrens 34597046Sahrens if (type == POOL_SCRUB_EVERYTHING && 34607046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 34617046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 34627046Sahrens return (EBUSY); 34637046Sahrens 34647046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 34657046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 34667046Sahrens } else if (type == POOL_SCRUB_NONE) { 34677046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 34681544Seschrock } else { 34697046Sahrens return (EINVAL); 34701544Seschrock } 3471789Sahrens } 3472789Sahrens 34731544Seschrock /* 34741544Seschrock * ========================================================================== 34751544Seschrock * SPA async task processing 34761544Seschrock * ========================================================================== 34771544Seschrock */ 34781544Seschrock 34791544Seschrock static void 34804451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3481789Sahrens { 34821544Seschrock vdev_t *tvd; 34831544Seschrock int c; 34841544Seschrock 34854451Seschrock for (c = 0; c < vd->vdev_children; c++) { 34864451Seschrock tvd = vd->vdev_child[c]; 34874451Seschrock if (tvd->vdev_remove_wanted) { 34884451Seschrock tvd->vdev_remove_wanted = 0; 34894451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 34904451Seschrock VDEV_AUX_NONE); 34915329Sgw25295 vdev_clear(spa, tvd, B_TRUE); 34924451Seschrock vdev_config_dirty(tvd->vdev_top); 34931544Seschrock } 34944451Seschrock spa_async_remove(spa, tvd); 34951544Seschrock } 34961544Seschrock } 34971544Seschrock 34981544Seschrock static void 34991544Seschrock spa_async_thread(spa_t *spa) 35001544Seschrock { 35011544Seschrock int tasks; 35024451Seschrock uint64_t txg; 35031544Seschrock 35041544Seschrock ASSERT(spa->spa_sync_on); 3505789Sahrens 35061544Seschrock mutex_enter(&spa->spa_async_lock); 35071544Seschrock tasks = spa->spa_async_tasks; 35081544Seschrock spa->spa_async_tasks = 0; 35091544Seschrock mutex_exit(&spa->spa_async_lock); 35101544Seschrock 35111544Seschrock /* 35121635Sbonwick * See if the config needs to be updated. 35131635Sbonwick */ 35141635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 35151635Sbonwick mutex_enter(&spa_namespace_lock); 35161635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 35171635Sbonwick mutex_exit(&spa_namespace_lock); 35181635Sbonwick } 35191635Sbonwick 35201635Sbonwick /* 35214451Seschrock * See if any devices need to be marked REMOVED. 35225329Sgw25295 * 35235329Sgw25295 * XXX - We avoid doing this when we are in 35245329Sgw25295 * I/O failure state since spa_vdev_enter() grabs 35255329Sgw25295 * the namespace lock and would not be able to obtain 35265329Sgw25295 * the writer config lock. 35271544Seschrock */ 35285329Sgw25295 if (tasks & SPA_ASYNC_REMOVE && 35295329Sgw25295 spa_state(spa) != POOL_STATE_IO_FAILURE) { 35304451Seschrock txg = spa_vdev_enter(spa); 35314451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 35324451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 35334451Seschrock } 35341544Seschrock 35351544Seschrock /* 35361544Seschrock * If any devices are done replacing, detach them. 35371544Seschrock */ 35384451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 35394451Seschrock spa_vdev_resilver_done(spa); 3540789Sahrens 35411544Seschrock /* 35421544Seschrock * Kick off a resilver. 35431544Seschrock */ 35447046Sahrens if (tasks & SPA_ASYNC_RESILVER) 35457046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 35461544Seschrock 35471544Seschrock /* 35481544Seschrock * Let the world know that we're done. 35491544Seschrock */ 35501544Seschrock mutex_enter(&spa->spa_async_lock); 35511544Seschrock spa->spa_async_thread = NULL; 35521544Seschrock cv_broadcast(&spa->spa_async_cv); 35531544Seschrock mutex_exit(&spa->spa_async_lock); 35541544Seschrock thread_exit(); 35551544Seschrock } 35561544Seschrock 35571544Seschrock void 35581544Seschrock spa_async_suspend(spa_t *spa) 35591544Seschrock { 35601544Seschrock mutex_enter(&spa->spa_async_lock); 35611544Seschrock spa->spa_async_suspended++; 35621544Seschrock while (spa->spa_async_thread != NULL) 35631544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 35641544Seschrock mutex_exit(&spa->spa_async_lock); 35651544Seschrock } 35661544Seschrock 35671544Seschrock void 35681544Seschrock spa_async_resume(spa_t *spa) 35691544Seschrock { 35701544Seschrock mutex_enter(&spa->spa_async_lock); 35711544Seschrock ASSERT(spa->spa_async_suspended != 0); 35721544Seschrock spa->spa_async_suspended--; 35731544Seschrock mutex_exit(&spa->spa_async_lock); 35741544Seschrock } 35751544Seschrock 35761544Seschrock static void 35771544Seschrock spa_async_dispatch(spa_t *spa) 35781544Seschrock { 35791544Seschrock mutex_enter(&spa->spa_async_lock); 35801544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 35811635Sbonwick spa->spa_async_thread == NULL && 35821635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 35831544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 35841544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 35851544Seschrock mutex_exit(&spa->spa_async_lock); 35861544Seschrock } 35871544Seschrock 35881544Seschrock void 35891544Seschrock spa_async_request(spa_t *spa, int task) 35901544Seschrock { 35911544Seschrock mutex_enter(&spa->spa_async_lock); 35921544Seschrock spa->spa_async_tasks |= task; 35931544Seschrock mutex_exit(&spa->spa_async_lock); 3594789Sahrens } 3595789Sahrens 3596789Sahrens /* 3597789Sahrens * ========================================================================== 3598789Sahrens * SPA syncing routines 3599789Sahrens * ========================================================================== 3600789Sahrens */ 3601789Sahrens 3602789Sahrens static void 3603789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3604789Sahrens { 3605789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3606789Sahrens dmu_tx_t *tx; 3607789Sahrens blkptr_t blk; 3608789Sahrens uint64_t itor = 0; 3609789Sahrens zio_t *zio; 3610789Sahrens int error; 3611789Sahrens uint8_t c = 1; 3612789Sahrens 3613789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3614789Sahrens 3615789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3616789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3617789Sahrens 3618789Sahrens error = zio_wait(zio); 3619789Sahrens ASSERT3U(error, ==, 0); 3620789Sahrens 3621789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3622789Sahrens bplist_vacate(bpl, tx); 3623789Sahrens 3624789Sahrens /* 3625789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3626789Sahrens * (Usually only the first block is needed.) 3627789Sahrens */ 3628789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3629789Sahrens dmu_tx_commit(tx); 3630789Sahrens } 3631789Sahrens 3632789Sahrens static void 36332082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 36342082Seschrock { 36352082Seschrock char *packed = NULL; 36362082Seschrock size_t nvsize = 0; 36372082Seschrock dmu_buf_t *db; 36382082Seschrock 36392082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 36402082Seschrock 36412082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 36422082Seschrock 36432082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 36442082Seschrock KM_SLEEP) == 0); 36452082Seschrock 36462082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 36472082Seschrock 36482082Seschrock kmem_free(packed, nvsize); 36492082Seschrock 36502082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 36512082Seschrock dmu_buf_will_dirty(db, tx); 36522082Seschrock *(uint64_t *)db->db_data = nvsize; 36532082Seschrock dmu_buf_rele(db, FTAG); 36542082Seschrock } 36552082Seschrock 36562082Seschrock static void 36575450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 36585450Sbrendan const char *config, const char *entry) 36592082Seschrock { 36602082Seschrock nvlist_t *nvroot; 36615450Sbrendan nvlist_t **list; 36622082Seschrock int i; 36632082Seschrock 36645450Sbrendan if (!sav->sav_sync) 36652082Seschrock return; 36662082Seschrock 36672082Seschrock /* 36685450Sbrendan * Update the MOS nvlist describing the list of available devices. 36695450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 36704451Seschrock * valid and the vdevs are labeled appropriately. 36712082Seschrock */ 36725450Sbrendan if (sav->sav_object == 0) { 36735450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 36745450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 36755450Sbrendan sizeof (uint64_t), tx); 36762082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 36775450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 36785450Sbrendan &sav->sav_object, tx) == 0); 36792082Seschrock } 36802082Seschrock 36812082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 36825450Sbrendan if (sav->sav_count == 0) { 36835450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 36842082Seschrock } else { 36855450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 36865450Sbrendan for (i = 0; i < sav->sav_count; i++) 36875450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 36885450Sbrendan B_FALSE, B_FALSE, B_TRUE); 36895450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 36905450Sbrendan sav->sav_count) == 0); 36915450Sbrendan for (i = 0; i < sav->sav_count; i++) 36925450Sbrendan nvlist_free(list[i]); 36935450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 36942082Seschrock } 36952082Seschrock 36965450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 36972926Sek110237 nvlist_free(nvroot); 36982082Seschrock 36995450Sbrendan sav->sav_sync = B_FALSE; 37002082Seschrock } 37012082Seschrock 37022082Seschrock static void 3703789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3704789Sahrens { 3705789Sahrens nvlist_t *config; 3706789Sahrens 3707789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 3708789Sahrens return; 3709789Sahrens 3710789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 3711789Sahrens 37121635Sbonwick if (spa->spa_config_syncing) 37131635Sbonwick nvlist_free(spa->spa_config_syncing); 37141635Sbonwick spa->spa_config_syncing = config; 3715789Sahrens 37162082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3717789Sahrens } 3718789Sahrens 37195094Slling /* 37205094Slling * Set zpool properties. 37215094Slling */ 37223912Slling static void 37234543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 37243912Slling { 37253912Slling spa_t *spa = arg1; 37265094Slling objset_t *mos = spa->spa_meta_objset; 37273912Slling nvlist_t *nvp = arg2; 37285094Slling nvpair_t *elem; 37294451Seschrock uint64_t intval; 37306643Seschrock char *strval; 37315094Slling zpool_prop_t prop; 37325094Slling const char *propname; 37335094Slling zprop_type_t proptype; 37346643Seschrock spa_config_dirent_t *dp; 37355094Slling 37365094Slling elem = NULL; 37375094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 37385094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 37395094Slling case ZPOOL_PROP_VERSION: 37405094Slling /* 37415094Slling * Only set version for non-zpool-creation cases 37425094Slling * (set/import). spa_create() needs special care 37435094Slling * for version setting. 37445094Slling */ 37455094Slling if (tx->tx_txg != TXG_INITIAL) { 37465094Slling VERIFY(nvpair_value_uint64(elem, 37475094Slling &intval) == 0); 37485094Slling ASSERT(intval <= SPA_VERSION); 37495094Slling ASSERT(intval >= spa_version(spa)); 37505094Slling spa->spa_uberblock.ub_version = intval; 37515094Slling vdev_config_dirty(spa->spa_root_vdev); 37525094Slling } 37535094Slling break; 37545094Slling 37555094Slling case ZPOOL_PROP_ALTROOT: 37565094Slling /* 37575094Slling * 'altroot' is a non-persistent property. It should 37585094Slling * have been set temporarily at creation or import time. 37595094Slling */ 37605094Slling ASSERT(spa->spa_root != NULL); 37615094Slling break; 37625094Slling 37635363Seschrock case ZPOOL_PROP_CACHEFILE: 37645094Slling /* 37655363Seschrock * 'cachefile' is a non-persistent property, but note 37665363Seschrock * an async request that the config cache needs to be 37675363Seschrock * udpated. 37685094Slling */ 37695363Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 37706643Seschrock 37716643Seschrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 37726643Seschrock KM_SLEEP); 37736643Seschrock 37746643Seschrock if (strval[0] == '\0') 37756643Seschrock dp->scd_path = spa_strdup(spa_config_path); 37766643Seschrock else if (strcmp(strval, "none") == 0) 37776643Seschrock dp->scd_path = NULL; 37786643Seschrock else 37796643Seschrock dp->scd_path = spa_strdup(strval); 37806643Seschrock 37816643Seschrock list_insert_head(&spa->spa_config_list, dp); 37825363Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 37834543Smarks break; 37845094Slling default: 37855094Slling /* 37865094Slling * Set pool property values in the poolprops mos object. 37875094Slling */ 37885094Slling mutex_enter(&spa->spa_props_lock); 37895094Slling if (spa->spa_pool_props_object == 0) { 37905094Slling objset_t *mos = spa->spa_meta_objset; 37915094Slling 37925094Slling VERIFY((spa->spa_pool_props_object = 37935094Slling zap_create(mos, DMU_OT_POOL_PROPS, 37945094Slling DMU_OT_NONE, 0, tx)) > 0); 37955094Slling 37965094Slling VERIFY(zap_update(mos, 37975094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 37985094Slling 8, 1, &spa->spa_pool_props_object, tx) 37995094Slling == 0); 38005094Slling } 38015094Slling mutex_exit(&spa->spa_props_lock); 38025094Slling 38035094Slling /* normalize the property name */ 38045094Slling propname = zpool_prop_to_name(prop); 38055094Slling proptype = zpool_prop_get_type(prop); 38065094Slling 38075094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 38085094Slling ASSERT(proptype == PROP_TYPE_STRING); 38095094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 38105094Slling VERIFY(zap_update(mos, 38115094Slling spa->spa_pool_props_object, propname, 38125094Slling 1, strlen(strval) + 1, strval, tx) == 0); 38135094Slling 38145094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 38155094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 38165094Slling 38175094Slling if (proptype == PROP_TYPE_INDEX) { 38185094Slling const char *unused; 38195094Slling VERIFY(zpool_prop_index_to_string( 38205094Slling prop, intval, &unused) == 0); 38215094Slling } 38225094Slling VERIFY(zap_update(mos, 38235094Slling spa->spa_pool_props_object, propname, 38245094Slling 8, 1, &intval, tx) == 0); 38255094Slling } else { 38265094Slling ASSERT(0); /* not allowed */ 38275094Slling } 38285094Slling 38295329Sgw25295 switch (prop) { 38305329Sgw25295 case ZPOOL_PROP_DELEGATION: 38315094Slling spa->spa_delegation = intval; 38325329Sgw25295 break; 38335329Sgw25295 case ZPOOL_PROP_BOOTFS: 38345094Slling spa->spa_bootfs = intval; 38355329Sgw25295 break; 38365329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 38375329Sgw25295 spa->spa_failmode = intval; 38385329Sgw25295 break; 38395329Sgw25295 default: 38405329Sgw25295 break; 38415329Sgw25295 } 38423912Slling } 38435094Slling 38445094Slling /* log internal history if this is not a zpool create */ 38455094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 38465094Slling tx->tx_txg != TXG_INITIAL) { 38475094Slling spa_history_internal_log(LOG_POOL_PROPSET, 38485094Slling spa, tx, cr, "%s %lld %s", 38495094Slling nvpair_name(elem), intval, spa->spa_name); 38505094Slling } 38513912Slling } 38523912Slling } 38533912Slling 3854789Sahrens /* 3855789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3856789Sahrens * part of the process, so we iterate until it converges. 3857789Sahrens */ 3858789Sahrens void 3859789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3860789Sahrens { 3861789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3862789Sahrens objset_t *mos = spa->spa_meta_objset; 3863789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 38641635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3865789Sahrens vdev_t *vd; 3866789Sahrens dmu_tx_t *tx; 3867789Sahrens int dirty_vdevs; 3868789Sahrens 3869789Sahrens /* 3870789Sahrens * Lock out configuration changes. 3871789Sahrens */ 38721544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3873789Sahrens 3874789Sahrens spa->spa_syncing_txg = txg; 3875789Sahrens spa->spa_sync_pass = 0; 3876789Sahrens 38771544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3878789Sahrens 38792082Seschrock tx = dmu_tx_create_assigned(dp, txg); 38802082Seschrock 38812082Seschrock /* 38824577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 38832082Seschrock * set spa_deflate if we have no raid-z vdevs. 38842082Seschrock */ 38854577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 38864577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 38872082Seschrock int i; 38882082Seschrock 38892082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 38902082Seschrock vd = rvd->vdev_child[i]; 38912082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 38922082Seschrock break; 38932082Seschrock } 38942082Seschrock if (i == rvd->vdev_children) { 38952082Seschrock spa->spa_deflate = TRUE; 38962082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 38972082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 38982082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 38992082Seschrock } 39002082Seschrock } 39012082Seschrock 39027046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 39037046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 39047046Sahrens dsl_pool_create_origin(dp, tx); 39057046Sahrens 39067046Sahrens /* Keeping the origin open increases spa_minref */ 39077046Sahrens spa->spa_minref += 3; 39087046Sahrens } 39097046Sahrens 39107046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 39117046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 39127046Sahrens dsl_pool_upgrade_clones(dp, tx); 39137046Sahrens } 39147046Sahrens 3915789Sahrens /* 3916789Sahrens * If anything has changed in this txg, push the deferred frees 3917789Sahrens * from the previous txg. If not, leave them alone so that we 3918789Sahrens * don't generate work on an otherwise idle system. 3919789Sahrens */ 3920789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 39212329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 39222329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3923789Sahrens spa_sync_deferred_frees(spa, txg); 3924789Sahrens 3925789Sahrens /* 3926789Sahrens * Iterate to convergence. 3927789Sahrens */ 3928789Sahrens do { 3929789Sahrens spa->spa_sync_pass++; 3930789Sahrens 3931789Sahrens spa_sync_config_object(spa, tx); 39325450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 39335450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 39345450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 39355450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 39361544Seschrock spa_errlog_sync(spa, txg); 3937789Sahrens dsl_pool_sync(dp, txg); 3938789Sahrens 3939789Sahrens dirty_vdevs = 0; 3940789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3941789Sahrens vdev_sync(vd, txg); 3942789Sahrens dirty_vdevs++; 3943789Sahrens } 3944789Sahrens 3945789Sahrens bplist_sync(bpl, tx); 3946789Sahrens } while (dirty_vdevs); 3947789Sahrens 3948789Sahrens bplist_close(bpl); 3949789Sahrens 3950789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3951789Sahrens 3952789Sahrens /* 3953789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3954789Sahrens * to commit the transaction group. 39551635Sbonwick * 39565688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 39575688Sbonwick * random top-level vdevs that are known to be visible in the 39585688Sbonwick * config cache (see spa_vdev_add() for details). If there *are* 39595688Sbonwick * dirty vdevs -- or if the sync to our random subset fails -- 39605688Sbonwick * then sync the uberblock to all vdevs. 3961789Sahrens */ 39625688Sbonwick if (list_is_empty(&spa->spa_dirty_list)) { 39636615Sgw25295 vdev_t *svd[SPA_DVAS_PER_BP]; 39646615Sgw25295 int svdcount = 0; 39651635Sbonwick int children = rvd->vdev_children; 39661635Sbonwick int c0 = spa_get_random(children); 39671635Sbonwick int c; 39681635Sbonwick 39691635Sbonwick for (c = 0; c < children; c++) { 39701635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 39715688Sbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 39721635Sbonwick continue; 39735688Sbonwick svd[svdcount++] = vd; 39745688Sbonwick if (svdcount == SPA_DVAS_PER_BP) 39751635Sbonwick break; 39761635Sbonwick } 39776615Sgw25295 vdev_config_sync(svd, svdcount, txg); 39786615Sgw25295 } else { 39796615Sgw25295 vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg); 39801635Sbonwick } 39812082Seschrock dmu_tx_commit(tx); 39822082Seschrock 39831635Sbonwick /* 39841635Sbonwick * Clear the dirty config list. 39851635Sbonwick */ 39861635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 39871635Sbonwick vdev_config_clean(vd); 39881635Sbonwick 39891635Sbonwick /* 39901635Sbonwick * Now that the new config has synced transactionally, 39911635Sbonwick * let it become visible to the config cache. 39921635Sbonwick */ 39931635Sbonwick if (spa->spa_config_syncing != NULL) { 39941635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 39951635Sbonwick spa->spa_config_txg = txg; 39961635Sbonwick spa->spa_config_syncing = NULL; 39971635Sbonwick } 3998789Sahrens 39997046Sahrens spa->spa_traverse_wanted = B_TRUE; 4000789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 40017046Sahrens spa->spa_traverse_wanted = B_FALSE; 4002789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4003789Sahrens rw_exit(&spa->spa_traverse_lock); 4004789Sahrens 4005789Sahrens /* 4006789Sahrens * Clean up the ZIL records for the synced txg. 4007789Sahrens */ 4008789Sahrens dsl_pool_zil_clean(dp); 4009789Sahrens 4010789Sahrens /* 4011789Sahrens * Update usable space statistics. 4012789Sahrens */ 4013789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4014789Sahrens vdev_sync_done(vd, txg); 4015789Sahrens 4016789Sahrens /* 4017789Sahrens * It had better be the case that we didn't dirty anything 40182082Seschrock * since vdev_config_sync(). 4019789Sahrens */ 4020789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4021789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4022789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4023789Sahrens ASSERT(bpl->bpl_queue == NULL); 4024789Sahrens 40251544Seschrock spa_config_exit(spa, FTAG); 40261544Seschrock 40271544Seschrock /* 40281544Seschrock * If any async tasks have been requested, kick them off. 40291544Seschrock */ 40301544Seschrock spa_async_dispatch(spa); 4031789Sahrens } 4032789Sahrens 4033789Sahrens /* 4034789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4035789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4036789Sahrens * sync. 4037789Sahrens */ 4038789Sahrens void 4039789Sahrens spa_sync_allpools(void) 4040789Sahrens { 4041789Sahrens spa_t *spa = NULL; 4042789Sahrens mutex_enter(&spa_namespace_lock); 4043789Sahrens while ((spa = spa_next(spa)) != NULL) { 4044789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 4045789Sahrens continue; 4046789Sahrens spa_open_ref(spa, FTAG); 4047789Sahrens mutex_exit(&spa_namespace_lock); 4048789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4049789Sahrens mutex_enter(&spa_namespace_lock); 4050789Sahrens spa_close(spa, FTAG); 4051789Sahrens } 4052789Sahrens mutex_exit(&spa_namespace_lock); 4053789Sahrens } 4054789Sahrens 4055789Sahrens /* 4056789Sahrens * ========================================================================== 4057789Sahrens * Miscellaneous routines 4058789Sahrens * ========================================================================== 4059789Sahrens */ 4060789Sahrens 4061789Sahrens /* 4062789Sahrens * Remove all pools in the system. 4063789Sahrens */ 4064789Sahrens void 4065789Sahrens spa_evict_all(void) 4066789Sahrens { 4067789Sahrens spa_t *spa; 4068789Sahrens 4069789Sahrens /* 4070789Sahrens * Remove all cached state. All pools should be closed now, 4071789Sahrens * so every spa in the AVL tree should be unreferenced. 4072789Sahrens */ 4073789Sahrens mutex_enter(&spa_namespace_lock); 4074789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4075789Sahrens /* 40761544Seschrock * Stop async tasks. The async thread may need to detach 40771544Seschrock * a device that's been replaced, which requires grabbing 40781544Seschrock * spa_namespace_lock, so we must drop it here. 4079789Sahrens */ 4080789Sahrens spa_open_ref(spa, FTAG); 4081789Sahrens mutex_exit(&spa_namespace_lock); 40821544Seschrock spa_async_suspend(spa); 40834808Sek110237 mutex_enter(&spa_namespace_lock); 4084789Sahrens spa_close(spa, FTAG); 4085789Sahrens 4086789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4087789Sahrens spa_unload(spa); 4088789Sahrens spa_deactivate(spa); 4089789Sahrens } 4090789Sahrens spa_remove(spa); 4091789Sahrens } 4092789Sahrens mutex_exit(&spa_namespace_lock); 4093789Sahrens } 40941544Seschrock 40951544Seschrock vdev_t * 40966643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 40971544Seschrock { 40986643Seschrock vdev_t *vd; 40996643Seschrock int i; 41006643Seschrock 41016643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 41026643Seschrock return (vd); 41036643Seschrock 41046643Seschrock if (l2cache) { 41056643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 41066643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 41076643Seschrock if (vd->vdev_guid == guid) 41086643Seschrock return (vd); 41096643Seschrock } 41106643Seschrock } 41116643Seschrock 41126643Seschrock return (NULL); 41131544Seschrock } 41141760Seschrock 41151760Seschrock void 41165094Slling spa_upgrade(spa_t *spa, uint64_t version) 41171760Seschrock { 41181760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 41191760Seschrock 41201760Seschrock /* 41211760Seschrock * This should only be called for a non-faulted pool, and since a 41221760Seschrock * future version would result in an unopenable pool, this shouldn't be 41231760Seschrock * possible. 41241760Seschrock */ 41254577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 41265094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 41275094Slling 41285094Slling spa->spa_uberblock.ub_version = version; 41291760Seschrock vdev_config_dirty(spa->spa_root_vdev); 41301760Seschrock 41311760Seschrock spa_config_exit(spa, FTAG); 41322082Seschrock 41332082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 41341760Seschrock } 41352082Seschrock 41362082Seschrock boolean_t 41372082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 41382082Seschrock { 41392082Seschrock int i; 41403377Seschrock uint64_t spareguid; 41415450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 41425450Sbrendan 41435450Sbrendan for (i = 0; i < sav->sav_count; i++) 41445450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 41452082Seschrock return (B_TRUE); 41462082Seschrock 41475450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 41485450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 41495450Sbrendan &spareguid) == 0 && spareguid == guid) 41503377Seschrock return (B_TRUE); 41513377Seschrock } 41523377Seschrock 41532082Seschrock return (B_FALSE); 41542082Seschrock } 41553912Slling 41564451Seschrock /* 4157*7214Slling * Check if a pool has an active shared spare device. 4158*7214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 4159*7214Slling */ 4160*7214Slling static boolean_t 4161*7214Slling spa_has_active_shared_spare(spa_t *spa) 4162*7214Slling { 4163*7214Slling int i, refcnt; 4164*7214Slling uint64_t pool; 4165*7214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 4166*7214Slling 4167*7214Slling for (i = 0; i < sav->sav_count; i++) { 4168*7214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 4169*7214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 4170*7214Slling refcnt > 2) 4171*7214Slling return (B_TRUE); 4172*7214Slling } 4173*7214Slling 4174*7214Slling return (B_FALSE); 4175*7214Slling } 4176*7214Slling 4177*7214Slling /* 41784451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 41794451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 41804451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 41814451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 41824451Seschrock * or zdb as real changes. 41834451Seschrock */ 41844451Seschrock void 41854451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 41864451Seschrock { 41874451Seschrock #ifdef _KERNEL 41884451Seschrock sysevent_t *ev; 41894451Seschrock sysevent_attr_list_t *attr = NULL; 41904451Seschrock sysevent_value_t value; 41914451Seschrock sysevent_id_t eid; 41924451Seschrock 41934451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 41944451Seschrock SE_SLEEP); 41954451Seschrock 41964451Seschrock value.value_type = SE_DATA_TYPE_STRING; 41974451Seschrock value.value.sv_string = spa_name(spa); 41984451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 41994451Seschrock goto done; 42004451Seschrock 42014451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 42024451Seschrock value.value.sv_uint64 = spa_guid(spa); 42034451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 42044451Seschrock goto done; 42054451Seschrock 42064451Seschrock if (vd) { 42074451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 42084451Seschrock value.value.sv_uint64 = vd->vdev_guid; 42094451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 42104451Seschrock SE_SLEEP) != 0) 42114451Seschrock goto done; 42124451Seschrock 42134451Seschrock if (vd->vdev_path) { 42144451Seschrock value.value_type = SE_DATA_TYPE_STRING; 42154451Seschrock value.value.sv_string = vd->vdev_path; 42164451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 42174451Seschrock &value, SE_SLEEP) != 0) 42184451Seschrock goto done; 42194451Seschrock } 42204451Seschrock } 42214451Seschrock 42225756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 42235756Seschrock goto done; 42245756Seschrock attr = NULL; 42255756Seschrock 42264451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 42274451Seschrock 42284451Seschrock done: 42294451Seschrock if (attr) 42304451Seschrock sysevent_free_attr(attr); 42314451Seschrock sysevent_free(ev); 42324451Seschrock #endif 42334451Seschrock } 4234