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); 715094Slling 725094Slling /* 735094Slling * ========================================================================== 745094Slling * SPA properties routines 755094Slling * ========================================================================== 765094Slling */ 775094Slling 785094Slling /* 795094Slling * Add a (source=src, propname=propval) list to an nvlist. 805094Slling */ 815949Slling static void 825094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 835094Slling uint64_t intval, zprop_source_t src) 845094Slling { 855094Slling const char *propname = zpool_prop_to_name(prop); 865094Slling nvlist_t *propval; 875949Slling 885949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 895949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 905949Slling 915949Slling if (strval != NULL) 925949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 935949Slling else 945949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 955949Slling 965949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 975094Slling nvlist_free(propval); 985094Slling } 995094Slling 1005094Slling /* 1015094Slling * Get property values from the spa configuration. 1025094Slling */ 1035949Slling static void 1045094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1055094Slling { 1065094Slling uint64_t size = spa_get_space(spa); 1075094Slling uint64_t used = spa_get_alloc(spa); 1085094Slling uint64_t cap, version; 1095094Slling zprop_source_t src = ZPROP_SRC_NONE; 1106643Seschrock spa_config_dirent_t *dp; 1115094Slling 1125094Slling /* 1135094Slling * readonly properties 1145094Slling */ 1155949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src); 1165949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 1175949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 1185949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src); 1195094Slling 1205094Slling cap = (size == 0) ? 0 : (used * 100 / size); 1215949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1225949Slling 1235949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 1245949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1255949Slling spa->spa_root_vdev->vdev_state, src); 1265094Slling 1275094Slling /* 1285094Slling * settable properties that are not stored in the pool property object. 1295094Slling */ 1305094Slling version = spa_version(spa); 1315094Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1325094Slling src = ZPROP_SRC_DEFAULT; 1335094Slling else 1345094Slling src = ZPROP_SRC_LOCAL; 1355949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1365949Slling 1375949Slling if (spa->spa_root != NULL) 1385949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 1395949Slling 0, ZPROP_SRC_LOCAL); 1405094Slling 1416643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 1426643Seschrock if (dp->scd_path == NULL) { 1435949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1446643Seschrock "none", 0, ZPROP_SRC_LOCAL); 1456643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1465949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 1476643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 1485363Seschrock } 1495363Seschrock } 1505094Slling } 1515094Slling 1525094Slling /* 1535094Slling * Get zpool property values. 1545094Slling */ 1555094Slling int 1565094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 1575094Slling { 1585094Slling zap_cursor_t zc; 1595094Slling zap_attribute_t za; 1605094Slling objset_t *mos = spa->spa_meta_objset; 1615094Slling int err; 1625094Slling 1635949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1645094Slling 1655094Slling /* 1665094Slling * Get properties from the spa config. 1675094Slling */ 1685949Slling spa_prop_get_config(spa, nvp); 1695094Slling 1705094Slling mutex_enter(&spa->spa_props_lock); 1715094Slling /* If no pool property object, no more prop to get. */ 1725094Slling if (spa->spa_pool_props_object == 0) { 1735094Slling mutex_exit(&spa->spa_props_lock); 1745094Slling return (0); 1755094Slling } 1765094Slling 1775094Slling /* 1785094Slling * Get properties from the MOS pool property object. 1795094Slling */ 1805094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 1815094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 1825094Slling zap_cursor_advance(&zc)) { 1835094Slling uint64_t intval = 0; 1845094Slling char *strval = NULL; 1855094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 1865094Slling zpool_prop_t prop; 1875094Slling 1885094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 1895094Slling continue; 1905094Slling 1915094Slling switch (za.za_integer_length) { 1925094Slling case 8: 1935094Slling /* integer property */ 1945094Slling if (za.za_first_integer != 1955094Slling zpool_prop_default_numeric(prop)) 1965094Slling src = ZPROP_SRC_LOCAL; 1975094Slling 1985094Slling if (prop == ZPOOL_PROP_BOOTFS) { 1995094Slling dsl_pool_t *dp; 2005094Slling dsl_dataset_t *ds = NULL; 2015094Slling 2025094Slling dp = spa_get_dsl(spa); 2035094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2046689Smaybee if (err = dsl_dataset_hold_obj(dp, 2056689Smaybee za.za_first_integer, FTAG, &ds)) { 2065094Slling rw_exit(&dp->dp_config_rwlock); 2075094Slling break; 2085094Slling } 2095094Slling 2105094Slling strval = kmem_alloc( 2115094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2125094Slling KM_SLEEP); 2135094Slling dsl_dataset_name(ds, strval); 2146689Smaybee dsl_dataset_rele(ds, FTAG); 2155094Slling rw_exit(&dp->dp_config_rwlock); 2165094Slling } else { 2175094Slling strval = NULL; 2185094Slling intval = za.za_first_integer; 2195094Slling } 2205094Slling 2215949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2225094Slling 2235094Slling if (strval != NULL) 2245094Slling kmem_free(strval, 2255094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2265094Slling 2275094Slling break; 2285094Slling 2295094Slling case 1: 2305094Slling /* string property */ 2315094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2325094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2335094Slling za.za_name, 1, za.za_num_integers, strval); 2345094Slling if (err) { 2355094Slling kmem_free(strval, za.za_num_integers); 2365094Slling break; 2375094Slling } 2385949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2395094Slling kmem_free(strval, za.za_num_integers); 2405094Slling break; 2415094Slling 2425094Slling default: 2435094Slling break; 2445094Slling } 2455094Slling } 2465094Slling zap_cursor_fini(&zc); 2475094Slling mutex_exit(&spa->spa_props_lock); 2485094Slling out: 2495094Slling if (err && err != ENOENT) { 2505094Slling nvlist_free(*nvp); 2515949Slling *nvp = NULL; 2525094Slling return (err); 2535094Slling } 2545094Slling 2555094Slling return (0); 2565094Slling } 2575094Slling 2585094Slling /* 2595094Slling * Validate the given pool properties nvlist and modify the list 2605094Slling * for the property values to be set. 2615094Slling */ 2625094Slling static int 2635094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 2645094Slling { 2655094Slling nvpair_t *elem; 2665094Slling int error = 0, reset_bootfs = 0; 2675094Slling uint64_t objnum; 2685094Slling 2695094Slling elem = NULL; 2705094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2715094Slling zpool_prop_t prop; 2725094Slling char *propname, *strval; 2735094Slling uint64_t intval; 2745094Slling objset_t *os; 2755363Seschrock char *slash; 2765094Slling 2775094Slling propname = nvpair_name(elem); 2785094Slling 2795094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 2805094Slling return (EINVAL); 2815094Slling 2825094Slling switch (prop) { 2835094Slling case ZPOOL_PROP_VERSION: 2845094Slling error = nvpair_value_uint64(elem, &intval); 2855094Slling if (!error && 2865094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 2875094Slling error = EINVAL; 2885094Slling break; 2895094Slling 2905094Slling case ZPOOL_PROP_DELEGATION: 2915094Slling case ZPOOL_PROP_AUTOREPLACE: 2925094Slling error = nvpair_value_uint64(elem, &intval); 2935094Slling if (!error && intval > 1) 2945094Slling error = EINVAL; 2955094Slling break; 2965094Slling 2975094Slling case ZPOOL_PROP_BOOTFS: 2985094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 2995094Slling error = ENOTSUP; 3005094Slling break; 3015094Slling } 3025094Slling 3035094Slling /* 3047042Sgw25295 * Make sure the vdev config is bootable 3055094Slling */ 3067042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3075094Slling error = ENOTSUP; 3085094Slling break; 3095094Slling } 3105094Slling 3115094Slling reset_bootfs = 1; 3125094Slling 3135094Slling error = nvpair_value_string(elem, &strval); 3145094Slling 3155094Slling if (!error) { 3167042Sgw25295 uint64_t compress; 3177042Sgw25295 3185094Slling if (strval == NULL || strval[0] == '\0') { 3195094Slling objnum = zpool_prop_default_numeric( 3205094Slling ZPOOL_PROP_BOOTFS); 3215094Slling break; 3225094Slling } 3235094Slling 3245094Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 3256689Smaybee DS_MODE_USER | DS_MODE_READONLY, &os)) 3265094Slling break; 3277042Sgw25295 3287042Sgw25295 /* We don't support gzip bootable datasets */ 3297042Sgw25295 if ((error = dsl_prop_get_integer(strval, 3307042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 3317042Sgw25295 &compress, NULL)) == 0 && 3327042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 3337042Sgw25295 error = ENOTSUP; 3347042Sgw25295 } else { 3357042Sgw25295 objnum = dmu_objset_id(os); 3367042Sgw25295 } 3375094Slling dmu_objset_close(os); 3385094Slling } 3395094Slling break; 3405329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3415329Sgw25295 error = nvpair_value_uint64(elem, &intval); 3425329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3435329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 3445329Sgw25295 error = EINVAL; 3455329Sgw25295 3465329Sgw25295 /* 3475329Sgw25295 * This is a special case which only occurs when 3485329Sgw25295 * the pool has completely failed. This allows 3495329Sgw25295 * the user to change the in-core failmode property 3505329Sgw25295 * without syncing it out to disk (I/Os might 3515329Sgw25295 * currently be blocked). We do this by returning 3525329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 3535329Sgw25295 * into thinking we encountered a property validation 3545329Sgw25295 * error. 3555329Sgw25295 */ 3565329Sgw25295 if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) { 3575329Sgw25295 spa->spa_failmode = intval; 3585329Sgw25295 error = EIO; 3595329Sgw25295 } 3605329Sgw25295 break; 3615363Seschrock 3625363Seschrock case ZPOOL_PROP_CACHEFILE: 3635363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3645363Seschrock break; 3655363Seschrock 3665363Seschrock if (strval[0] == '\0') 3675363Seschrock break; 3685363Seschrock 3695363Seschrock if (strcmp(strval, "none") == 0) 3705363Seschrock break; 3715363Seschrock 3725363Seschrock if (strval[0] != '/') { 3735363Seschrock error = EINVAL; 3745363Seschrock break; 3755363Seschrock } 3765363Seschrock 3775363Seschrock slash = strrchr(strval, '/'); 3785363Seschrock ASSERT(slash != NULL); 3795363Seschrock 3805363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3815363Seschrock strcmp(slash, "/..") == 0) 3825363Seschrock error = EINVAL; 3835363Seschrock break; 3845094Slling } 3855094Slling 3865094Slling if (error) 3875094Slling break; 3885094Slling } 3895094Slling 3905094Slling if (!error && reset_bootfs) { 3915094Slling error = nvlist_remove(props, 3925094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 3935094Slling 3945094Slling if (!error) { 3955094Slling error = nvlist_add_uint64(props, 3965094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 3975094Slling } 3985094Slling } 3995094Slling 4005094Slling return (error); 4015094Slling } 4025094Slling 4035094Slling int 4045094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4055094Slling { 4065094Slling int error; 4075094Slling 4085094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 4095094Slling return (error); 4105094Slling 4115094Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 4125094Slling spa, nvp, 3)); 4135094Slling } 4145094Slling 4155094Slling /* 4165094Slling * If the bootfs property value is dsobj, clear it. 4175094Slling */ 4185094Slling void 4195094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 4205094Slling { 4215094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 4225094Slling VERIFY(zap_remove(spa->spa_meta_objset, 4235094Slling spa->spa_pool_props_object, 4245094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 4255094Slling spa->spa_bootfs = 0; 4265094Slling } 4275094Slling } 4285094Slling 429789Sahrens /* 430789Sahrens * ========================================================================== 431789Sahrens * SPA state manipulation (open/create/destroy/import/export) 432789Sahrens * ========================================================================== 433789Sahrens */ 434789Sahrens 4351544Seschrock static int 4361544Seschrock spa_error_entry_compare(const void *a, const void *b) 4371544Seschrock { 4381544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 4391544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 4401544Seschrock int ret; 4411544Seschrock 4421544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 4431544Seschrock sizeof (zbookmark_t)); 4441544Seschrock 4451544Seschrock if (ret < 0) 4461544Seschrock return (-1); 4471544Seschrock else if (ret > 0) 4481544Seschrock return (1); 4491544Seschrock else 4501544Seschrock return (0); 4511544Seschrock } 4521544Seschrock 4531544Seschrock /* 4541544Seschrock * Utility function which retrieves copies of the current logs and 4551544Seschrock * re-initializes them in the process. 4561544Seschrock */ 4571544Seschrock void 4581544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 4591544Seschrock { 4601544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 4611544Seschrock 4621544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 4631544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 4641544Seschrock 4651544Seschrock avl_create(&spa->spa_errlist_scrub, 4661544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4671544Seschrock offsetof(spa_error_entry_t, se_avl)); 4681544Seschrock avl_create(&spa->spa_errlist_last, 4691544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4701544Seschrock offsetof(spa_error_entry_t, se_avl)); 4711544Seschrock } 4721544Seschrock 473789Sahrens /* 474789Sahrens * Activate an uninitialized pool. 475789Sahrens */ 476789Sahrens static void 477789Sahrens spa_activate(spa_t *spa) 478789Sahrens { 479789Sahrens int t; 480789Sahrens 481789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 482789Sahrens 483789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 484789Sahrens 485789Sahrens spa->spa_normal_class = metaslab_class_create(); 4864527Sperrin spa->spa_log_class = metaslab_class_create(); 487789Sahrens 488789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 489789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 4902986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 491789Sahrens TASKQ_PREPOPULATE); 492789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 4932986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 494789Sahrens TASKQ_PREPOPULATE); 495789Sahrens } 496789Sahrens 497789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 498789Sahrens offsetof(vdev_t, vdev_dirty_node)); 4995329Sgw25295 list_create(&spa->spa_zio_list, sizeof (zio_t), 5005329Sgw25295 offsetof(zio_t, zio_link_node)); 501789Sahrens 502789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 503789Sahrens offsetof(struct vdev, vdev_txg_node)); 5041544Seschrock 5051544Seschrock avl_create(&spa->spa_errlist_scrub, 5061544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5071544Seschrock offsetof(spa_error_entry_t, se_avl)); 5081544Seschrock avl_create(&spa->spa_errlist_last, 5091544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5101544Seschrock offsetof(spa_error_entry_t, se_avl)); 511789Sahrens } 512789Sahrens 513789Sahrens /* 514789Sahrens * Opposite of spa_activate(). 515789Sahrens */ 516789Sahrens static void 517789Sahrens spa_deactivate(spa_t *spa) 518789Sahrens { 519789Sahrens int t; 520789Sahrens 521789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 522789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 523789Sahrens ASSERT(spa->spa_root_vdev == NULL); 524789Sahrens 525789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 526789Sahrens 527789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 528789Sahrens 529789Sahrens list_destroy(&spa->spa_dirty_list); 5305329Sgw25295 list_destroy(&spa->spa_zio_list); 531789Sahrens 532789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 533789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 534789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 535789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 536789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 537789Sahrens } 538789Sahrens 539789Sahrens metaslab_class_destroy(spa->spa_normal_class); 540789Sahrens spa->spa_normal_class = NULL; 541789Sahrens 5424527Sperrin metaslab_class_destroy(spa->spa_log_class); 5434527Sperrin spa->spa_log_class = NULL; 5444527Sperrin 5451544Seschrock /* 5461544Seschrock * If this was part of an import or the open otherwise failed, we may 5471544Seschrock * still have errors left in the queues. Empty them just in case. 5481544Seschrock */ 5491544Seschrock spa_errlog_drain(spa); 5501544Seschrock 5511544Seschrock avl_destroy(&spa->spa_errlist_scrub); 5521544Seschrock avl_destroy(&spa->spa_errlist_last); 5531544Seschrock 554789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 555789Sahrens } 556789Sahrens 557789Sahrens /* 558789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 559789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 560789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 561789Sahrens * All vdev validation is done by the vdev_alloc() routine. 562789Sahrens */ 5632082Seschrock static int 5642082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 5652082Seschrock uint_t id, int atype) 566789Sahrens { 567789Sahrens nvlist_t **child; 568789Sahrens uint_t c, children; 5692082Seschrock int error; 5702082Seschrock 5712082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 5722082Seschrock return (error); 5732082Seschrock 5742082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 5752082Seschrock return (0); 576789Sahrens 577789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 578789Sahrens &child, &children) != 0) { 5792082Seschrock vdev_free(*vdp); 5802082Seschrock *vdp = NULL; 5812082Seschrock return (EINVAL); 582789Sahrens } 583789Sahrens 584789Sahrens for (c = 0; c < children; c++) { 5852082Seschrock vdev_t *vd; 5862082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 5872082Seschrock atype)) != 0) { 5882082Seschrock vdev_free(*vdp); 5892082Seschrock *vdp = NULL; 5902082Seschrock return (error); 591789Sahrens } 592789Sahrens } 593789Sahrens 5942082Seschrock ASSERT(*vdp != NULL); 5952082Seschrock 5962082Seschrock return (0); 597789Sahrens } 598789Sahrens 599789Sahrens /* 600789Sahrens * Opposite of spa_load(). 601789Sahrens */ 602789Sahrens static void 603789Sahrens spa_unload(spa_t *spa) 604789Sahrens { 6052082Seschrock int i; 6062082Seschrock 607789Sahrens /* 6081544Seschrock * Stop async tasks. 6091544Seschrock */ 6101544Seschrock spa_async_suspend(spa); 6111544Seschrock 6121544Seschrock /* 613789Sahrens * Stop syncing. 614789Sahrens */ 615789Sahrens if (spa->spa_sync_on) { 616789Sahrens txg_sync_stop(spa->spa_dsl_pool); 617789Sahrens spa->spa_sync_on = B_FALSE; 618789Sahrens } 619789Sahrens 620789Sahrens /* 621789Sahrens * Wait for any outstanding prefetch I/O to complete. 622789Sahrens */ 6231544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 6241544Seschrock spa_config_exit(spa, FTAG); 625789Sahrens 626789Sahrens /* 6275450Sbrendan * Drop and purge level 2 cache 6285450Sbrendan */ 6295450Sbrendan spa_l2cache_drop(spa); 6305450Sbrendan 6315450Sbrendan /* 632789Sahrens * Close the dsl pool. 633789Sahrens */ 634789Sahrens if (spa->spa_dsl_pool) { 635789Sahrens dsl_pool_close(spa->spa_dsl_pool); 636789Sahrens spa->spa_dsl_pool = NULL; 637789Sahrens } 638789Sahrens 639789Sahrens /* 640789Sahrens * Close all vdevs. 641789Sahrens */ 6421585Sbonwick if (spa->spa_root_vdev) 643789Sahrens vdev_free(spa->spa_root_vdev); 6441585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 6451544Seschrock 6465450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 6475450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 6485450Sbrendan if (spa->spa_spares.sav_vdevs) { 6495450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 6505450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 6515450Sbrendan spa->spa_spares.sav_vdevs = NULL; 6525450Sbrendan } 6535450Sbrendan if (spa->spa_spares.sav_config) { 6545450Sbrendan nvlist_free(spa->spa_spares.sav_config); 6555450Sbrendan spa->spa_spares.sav_config = NULL; 6562082Seschrock } 6575450Sbrendan 6585450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 6595450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 6605450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 6615450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 6625450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 6635450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 6645450Sbrendan } 6655450Sbrendan if (spa->spa_l2cache.sav_config) { 6665450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 6675450Sbrendan spa->spa_l2cache.sav_config = NULL; 6682082Seschrock } 6692082Seschrock 6701544Seschrock spa->spa_async_suspended = 0; 671789Sahrens } 672789Sahrens 673789Sahrens /* 6742082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 6752082Seschrock * this pool. When this is called, we have some form of basic information in 6765450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 6775450Sbrendan * then re-generate a more complete list including status information. 6782082Seschrock */ 6792082Seschrock static void 6802082Seschrock spa_load_spares(spa_t *spa) 6812082Seschrock { 6822082Seschrock nvlist_t **spares; 6832082Seschrock uint_t nspares; 6842082Seschrock int i; 6853377Seschrock vdev_t *vd, *tvd; 6862082Seschrock 6872082Seschrock /* 6882082Seschrock * First, close and free any existing spare vdevs. 6892082Seschrock */ 6905450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 6915450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 6923377Seschrock 6933377Seschrock /* Undo the call to spa_activate() below */ 6946643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 6956643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 6963377Seschrock spa_spare_remove(tvd); 6973377Seschrock vdev_close(vd); 6983377Seschrock vdev_free(vd); 6992082Seschrock } 7003377Seschrock 7015450Sbrendan if (spa->spa_spares.sav_vdevs) 7025450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7035450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7045450Sbrendan 7055450Sbrendan if (spa->spa_spares.sav_config == NULL) 7062082Seschrock nspares = 0; 7072082Seschrock else 7085450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 7092082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 7102082Seschrock 7115450Sbrendan spa->spa_spares.sav_count = (int)nspares; 7125450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7132082Seschrock 7142082Seschrock if (nspares == 0) 7152082Seschrock return; 7162082Seschrock 7172082Seschrock /* 7182082Seschrock * Construct the array of vdevs, opening them to get status in the 7193377Seschrock * process. For each spare, there is potentially two different vdev_t 7203377Seschrock * structures associated with it: one in the list of spares (used only 7213377Seschrock * for basic validation purposes) and one in the active vdev 7223377Seschrock * configuration (if it's spared in). During this phase we open and 7233377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 7243377Seschrock * active configuration, then we also mark this vdev as an active spare. 7252082Seschrock */ 7265450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 7275450Sbrendan KM_SLEEP); 7285450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7292082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 7302082Seschrock VDEV_ALLOC_SPARE) == 0); 7312082Seschrock ASSERT(vd != NULL); 7322082Seschrock 7335450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 7342082Seschrock 7356643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7366643Seschrock B_FALSE)) != NULL) { 7373377Seschrock if (!tvd->vdev_isspare) 7383377Seschrock spa_spare_add(tvd); 7393377Seschrock 7403377Seschrock /* 7413377Seschrock * We only mark the spare active if we were successfully 7423377Seschrock * able to load the vdev. Otherwise, importing a pool 7433377Seschrock * with a bad active spare would result in strange 7443377Seschrock * behavior, because multiple pool would think the spare 7453377Seschrock * is actively in use. 7463377Seschrock * 7473377Seschrock * There is a vulnerability here to an equally bizarre 7483377Seschrock * circumstance, where a dead active spare is later 7493377Seschrock * brought back to life (onlined or otherwise). Given 7503377Seschrock * the rarity of this scenario, and the extra complexity 7513377Seschrock * it adds, we ignore the possibility. 7523377Seschrock */ 7533377Seschrock if (!vdev_is_dead(tvd)) 7543377Seschrock spa_spare_activate(tvd); 7553377Seschrock } 7563377Seschrock 7572082Seschrock if (vdev_open(vd) != 0) 7582082Seschrock continue; 7592082Seschrock 7602082Seschrock vd->vdev_top = vd; 7615450Sbrendan if (vdev_validate_aux(vd) == 0) 7625450Sbrendan spa_spare_add(vd); 7632082Seschrock } 7642082Seschrock 7652082Seschrock /* 7662082Seschrock * Recompute the stashed list of spares, with status information 7672082Seschrock * this time. 7682082Seschrock */ 7695450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 7702082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 7712082Seschrock 7725450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 7735450Sbrendan KM_SLEEP); 7745450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7755450Sbrendan spares[i] = vdev_config_generate(spa, 7765450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 7775450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 7785450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 7795450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7802082Seschrock nvlist_free(spares[i]); 7815450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 7825450Sbrendan } 7835450Sbrendan 7845450Sbrendan /* 7855450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 7865450Sbrendan * this pool. When this is called, we have some form of basic information in 7875450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 7885450Sbrendan * then re-generate a more complete list including status information. 7895450Sbrendan * Devices which are already active have their details maintained, and are 7905450Sbrendan * not re-opened. 7915450Sbrendan */ 7925450Sbrendan static void 7935450Sbrendan spa_load_l2cache(spa_t *spa) 7945450Sbrendan { 7955450Sbrendan nvlist_t **l2cache; 7965450Sbrendan uint_t nl2cache; 7975450Sbrendan int i, j, oldnvdevs; 7986643Seschrock uint64_t guid, size; 7995450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 8005450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 8015450Sbrendan 8025450Sbrendan if (sav->sav_config != NULL) { 8035450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 8045450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 8055450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 8065450Sbrendan } else { 8075450Sbrendan nl2cache = 0; 8085450Sbrendan } 8095450Sbrendan 8105450Sbrendan oldvdevs = sav->sav_vdevs; 8115450Sbrendan oldnvdevs = sav->sav_count; 8125450Sbrendan sav->sav_vdevs = NULL; 8135450Sbrendan sav->sav_count = 0; 8145450Sbrendan 8155450Sbrendan /* 8165450Sbrendan * Process new nvlist of vdevs. 8175450Sbrendan */ 8185450Sbrendan for (i = 0; i < nl2cache; i++) { 8195450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 8205450Sbrendan &guid) == 0); 8215450Sbrendan 8225450Sbrendan newvdevs[i] = NULL; 8235450Sbrendan for (j = 0; j < oldnvdevs; j++) { 8245450Sbrendan vd = oldvdevs[j]; 8255450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 8265450Sbrendan /* 8275450Sbrendan * Retain previous vdev for add/remove ops. 8285450Sbrendan */ 8295450Sbrendan newvdevs[i] = vd; 8305450Sbrendan oldvdevs[j] = NULL; 8315450Sbrendan break; 8325450Sbrendan } 8335450Sbrendan } 8345450Sbrendan 8355450Sbrendan if (newvdevs[i] == NULL) { 8365450Sbrendan /* 8375450Sbrendan * Create new vdev 8385450Sbrendan */ 8395450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 8405450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 8415450Sbrendan ASSERT(vd != NULL); 8425450Sbrendan newvdevs[i] = vd; 8435450Sbrendan 8445450Sbrendan /* 8455450Sbrendan * Commit this vdev as an l2cache device, 8465450Sbrendan * even if it fails to open. 8475450Sbrendan */ 8485450Sbrendan spa_l2cache_add(vd); 8495450Sbrendan 8506643Seschrock vd->vdev_top = vd; 8516643Seschrock vd->vdev_aux = sav; 8526643Seschrock 8536643Seschrock spa_l2cache_activate(vd); 8546643Seschrock 8555450Sbrendan if (vdev_open(vd) != 0) 8565450Sbrendan continue; 8575450Sbrendan 8585450Sbrendan (void) vdev_validate_aux(vd); 8595450Sbrendan 8605450Sbrendan if (!vdev_is_dead(vd)) { 8615450Sbrendan size = vdev_get_rsize(vd); 8626643Seschrock l2arc_add_vdev(spa, vd, 8636643Seschrock VDEV_LABEL_START_SIZE, 8646643Seschrock size - VDEV_LABEL_START_SIZE); 8655450Sbrendan } 8665450Sbrendan } 8675450Sbrendan } 8685450Sbrendan 8695450Sbrendan /* 8705450Sbrendan * Purge vdevs that were dropped 8715450Sbrendan */ 8725450Sbrendan for (i = 0; i < oldnvdevs; i++) { 8735450Sbrendan uint64_t pool; 8745450Sbrendan 8755450Sbrendan vd = oldvdevs[i]; 8765450Sbrendan if (vd != NULL) { 8775450Sbrendan if (spa_mode & FWRITE && 8785450Sbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && 8796643Seschrock pool != 0ULL && 8806643Seschrock l2arc_vdev_present(vd)) { 8815450Sbrendan l2arc_remove_vdev(vd); 8825450Sbrendan } 8835450Sbrendan (void) vdev_close(vd); 8845450Sbrendan spa_l2cache_remove(vd); 8855450Sbrendan } 8865450Sbrendan } 8875450Sbrendan 8885450Sbrendan if (oldvdevs) 8895450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 8905450Sbrendan 8915450Sbrendan if (sav->sav_config == NULL) 8925450Sbrendan goto out; 8935450Sbrendan 8945450Sbrendan sav->sav_vdevs = newvdevs; 8955450Sbrendan sav->sav_count = (int)nl2cache; 8965450Sbrendan 8975450Sbrendan /* 8985450Sbrendan * Recompute the stashed list of l2cache devices, with status 8995450Sbrendan * information this time. 9005450Sbrendan */ 9015450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 9025450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 9035450Sbrendan 9045450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 9055450Sbrendan for (i = 0; i < sav->sav_count; i++) 9065450Sbrendan l2cache[i] = vdev_config_generate(spa, 9075450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 9085450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 9095450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 9105450Sbrendan out: 9115450Sbrendan for (i = 0; i < sav->sav_count; i++) 9125450Sbrendan nvlist_free(l2cache[i]); 9135450Sbrendan if (sav->sav_count) 9145450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 9152082Seschrock } 9162082Seschrock 9172082Seschrock static int 9182082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 9192082Seschrock { 9202082Seschrock dmu_buf_t *db; 9212082Seschrock char *packed = NULL; 9222082Seschrock size_t nvsize = 0; 9232082Seschrock int error; 9242082Seschrock *value = NULL; 9252082Seschrock 9262082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 9272082Seschrock nvsize = *(uint64_t *)db->db_data; 9282082Seschrock dmu_buf_rele(db, FTAG); 9292082Seschrock 9302082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 9312082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 9322082Seschrock if (error == 0) 9332082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 9342082Seschrock kmem_free(packed, nvsize); 9352082Seschrock 9362082Seschrock return (error); 9372082Seschrock } 9382082Seschrock 9392082Seschrock /* 9404451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9414451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9424451Seschrock */ 9434451Seschrock static void 9444451Seschrock spa_check_removed(vdev_t *vd) 9454451Seschrock { 9464451Seschrock int c; 9474451Seschrock 9484451Seschrock for (c = 0; c < vd->vdev_children; c++) 9494451Seschrock spa_check_removed(vd->vdev_child[c]); 9504451Seschrock 9514451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9524451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9534451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9544451Seschrock } 9554451Seschrock } 9564451Seschrock 9574451Seschrock /* 958789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 9591544Seschrock * source of configuration information. 960789Sahrens */ 961789Sahrens static int 9621544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 963789Sahrens { 964789Sahrens int error = 0; 965789Sahrens nvlist_t *nvroot = NULL; 966789Sahrens vdev_t *rvd; 967789Sahrens uberblock_t *ub = &spa->spa_uberblock; 9681635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 969789Sahrens uint64_t pool_guid; 9702082Seschrock uint64_t version; 971789Sahrens zio_t *zio; 9724451Seschrock uint64_t autoreplace = 0; 973789Sahrens 9741544Seschrock spa->spa_load_state = state; 9751635Sbonwick 976789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 9771733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 9781544Seschrock error = EINVAL; 9791544Seschrock goto out; 9801544Seschrock } 981789Sahrens 9822082Seschrock /* 9832082Seschrock * Versioning wasn't explicitly added to the label until later, so if 9842082Seschrock * it's not present treat it as the initial version. 9852082Seschrock */ 9862082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 9874577Sahrens version = SPA_VERSION_INITIAL; 9882082Seschrock 9891733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 9901733Sbonwick &spa->spa_config_txg); 9911733Sbonwick 9921635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 9931544Seschrock spa_guid_exists(pool_guid, 0)) { 9941544Seschrock error = EEXIST; 9951544Seschrock goto out; 9961544Seschrock } 997789Sahrens 9982174Seschrock spa->spa_load_guid = pool_guid; 9992174Seschrock 1000789Sahrens /* 10012082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 10022082Seschrock * value that will be returned by spa_version() since parsing the 10032082Seschrock * configuration requires knowing the version number. 1004789Sahrens */ 10051544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 10062082Seschrock spa->spa_ubsync.ub_version = version; 10072082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 10081544Seschrock spa_config_exit(spa, FTAG); 1009789Sahrens 10102082Seschrock if (error != 0) 10111544Seschrock goto out; 1012789Sahrens 10131585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1014789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1015789Sahrens 1016789Sahrens /* 1017789Sahrens * Try to open all vdevs, loading each label in the process. 1018789Sahrens */ 10194070Smc142369 error = vdev_open(rvd); 10204070Smc142369 if (error != 0) 10211544Seschrock goto out; 1022789Sahrens 1023789Sahrens /* 10241986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 10251986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 10261986Seschrock * flag. 10271986Seschrock */ 10281986Seschrock spa_config_enter(spa, RW_READER, FTAG); 10291986Seschrock error = vdev_validate(rvd); 10301986Seschrock spa_config_exit(spa, FTAG); 10311986Seschrock 10324070Smc142369 if (error != 0) 10331986Seschrock goto out; 10341986Seschrock 10351986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 10361986Seschrock error = ENXIO; 10371986Seschrock goto out; 10381986Seschrock } 10391986Seschrock 10401986Seschrock /* 1041789Sahrens * Find the best uberblock. 1042789Sahrens */ 1043789Sahrens bzero(ub, sizeof (uberblock_t)); 1044789Sahrens 1045789Sahrens zio = zio_root(spa, NULL, NULL, 1046789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1047789Sahrens vdev_uberblock_load(zio, rvd, ub); 1048789Sahrens error = zio_wait(zio); 1049789Sahrens 1050789Sahrens /* 1051789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1052789Sahrens */ 1053789Sahrens if (ub->ub_txg == 0) { 10541760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10551760Seschrock VDEV_AUX_CORRUPT_DATA); 10561544Seschrock error = ENXIO; 10571544Seschrock goto out; 10581544Seschrock } 10591544Seschrock 10601544Seschrock /* 10611544Seschrock * If the pool is newer than the code, we can't open it. 10621544Seschrock */ 10634577Sahrens if (ub->ub_version > SPA_VERSION) { 10641760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10651760Seschrock VDEV_AUX_VERSION_NEWER); 10661544Seschrock error = ENOTSUP; 10671544Seschrock goto out; 1068789Sahrens } 1069789Sahrens 1070789Sahrens /* 1071789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1072789Sahrens * incomplete configuration. 1073789Sahrens */ 10741732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 10751544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10761544Seschrock VDEV_AUX_BAD_GUID_SUM); 10771544Seschrock error = ENXIO; 10781544Seschrock goto out; 1079789Sahrens } 1080789Sahrens 1081789Sahrens /* 1082789Sahrens * Initialize internal SPA structures. 1083789Sahrens */ 1084789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1085789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1086789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 10871544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 10881544Seschrock if (error) { 10891544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10901544Seschrock VDEV_AUX_CORRUPT_DATA); 10911544Seschrock goto out; 10921544Seschrock } 1093789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1094789Sahrens 10951544Seschrock if (zap_lookup(spa->spa_meta_objset, 1096789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 10971544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 10981544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10991544Seschrock VDEV_AUX_CORRUPT_DATA); 11001544Seschrock error = EIO; 11011544Seschrock goto out; 11021544Seschrock } 1103789Sahrens 1104789Sahrens if (!mosconfig) { 11052082Seschrock nvlist_t *newconfig; 11063975Sek110237 uint64_t hostid; 11072082Seschrock 11082082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 11091544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11101544Seschrock VDEV_AUX_CORRUPT_DATA); 11111544Seschrock error = EIO; 11121544Seschrock goto out; 11131544Seschrock } 1114789Sahrens 11153975Sek110237 if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 11163975Sek110237 &hostid) == 0) { 11173975Sek110237 char *hostname; 11183975Sek110237 unsigned long myhostid = 0; 11193975Sek110237 11203975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 11213975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 11223975Sek110237 11233975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 11244178Slling if (hostid != 0 && myhostid != 0 && 11254178Slling (unsigned long)hostid != myhostid) { 11263975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 11273975Sek110237 "loaded as it was last accessed by " 11283975Sek110237 "another system (host: %s hostid: 0x%lx). " 11293975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 11303975Sek110237 spa->spa_name, hostname, 11313975Sek110237 (unsigned long)hostid); 11323975Sek110237 error = EBADF; 11333975Sek110237 goto out; 11343975Sek110237 } 11353975Sek110237 } 11363975Sek110237 1137789Sahrens spa_config_set(spa, newconfig); 1138789Sahrens spa_unload(spa); 1139789Sahrens spa_deactivate(spa); 1140789Sahrens spa_activate(spa); 1141789Sahrens 11421544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 11431544Seschrock } 11441544Seschrock 11451544Seschrock if (zap_lookup(spa->spa_meta_objset, 11461544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 11471544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 11481544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11491544Seschrock VDEV_AUX_CORRUPT_DATA); 11501544Seschrock error = EIO; 11511544Seschrock goto out; 1152789Sahrens } 1153789Sahrens 11541544Seschrock /* 11552082Seschrock * Load the bit that tells us to use the new accounting function 11562082Seschrock * (raid-z deflation). If we have an older pool, this will not 11572082Seschrock * be present. 11582082Seschrock */ 11592082Seschrock error = zap_lookup(spa->spa_meta_objset, 11602082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 11612082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 11622082Seschrock if (error != 0 && error != ENOENT) { 11632082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11642082Seschrock VDEV_AUX_CORRUPT_DATA); 11652082Seschrock error = EIO; 11662082Seschrock goto out; 11672082Seschrock } 11682082Seschrock 11692082Seschrock /* 11701544Seschrock * Load the persistent error log. If we have an older pool, this will 11711544Seschrock * not be present. 11721544Seschrock */ 11731544Seschrock error = zap_lookup(spa->spa_meta_objset, 11741544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 11751544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 11761807Sbonwick if (error != 0 && error != ENOENT) { 11771544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11781544Seschrock VDEV_AUX_CORRUPT_DATA); 11791544Seschrock error = EIO; 11801544Seschrock goto out; 11811544Seschrock } 11821544Seschrock 11831544Seschrock error = zap_lookup(spa->spa_meta_objset, 11841544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 11851544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 11861544Seschrock if (error != 0 && error != ENOENT) { 11871544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11881544Seschrock VDEV_AUX_CORRUPT_DATA); 11891544Seschrock error = EIO; 11901544Seschrock goto out; 11911544Seschrock } 1192789Sahrens 1193789Sahrens /* 11942926Sek110237 * Load the history object. If we have an older pool, this 11952926Sek110237 * will not be present. 11962926Sek110237 */ 11972926Sek110237 error = zap_lookup(spa->spa_meta_objset, 11982926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 11992926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 12002926Sek110237 if (error != 0 && error != ENOENT) { 12012926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12022926Sek110237 VDEV_AUX_CORRUPT_DATA); 12032926Sek110237 error = EIO; 12042926Sek110237 goto out; 12052926Sek110237 } 12062926Sek110237 12072926Sek110237 /* 12082082Seschrock * Load any hot spares for this pool. 12092082Seschrock */ 12102082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12115450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 12122082Seschrock if (error != 0 && error != ENOENT) { 12132082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12142082Seschrock VDEV_AUX_CORRUPT_DATA); 12152082Seschrock error = EIO; 12162082Seschrock goto out; 12172082Seschrock } 12182082Seschrock if (error == 0) { 12194577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 12205450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 12215450Sbrendan &spa->spa_spares.sav_config) != 0) { 12222082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12232082Seschrock VDEV_AUX_CORRUPT_DATA); 12242082Seschrock error = EIO; 12252082Seschrock goto out; 12262082Seschrock } 12272082Seschrock 12282082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12292082Seschrock spa_load_spares(spa); 12302082Seschrock spa_config_exit(spa, FTAG); 12312082Seschrock } 12322082Seschrock 12335450Sbrendan /* 12345450Sbrendan * Load any level 2 ARC devices for this pool. 12355450Sbrendan */ 12365450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12375450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 12385450Sbrendan &spa->spa_l2cache.sav_object); 12395450Sbrendan if (error != 0 && error != ENOENT) { 12405450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12415450Sbrendan VDEV_AUX_CORRUPT_DATA); 12425450Sbrendan error = EIO; 12435450Sbrendan goto out; 12445450Sbrendan } 12455450Sbrendan if (error == 0) { 12465450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 12475450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 12485450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 12495450Sbrendan vdev_set_state(rvd, B_TRUE, 12505450Sbrendan VDEV_STATE_CANT_OPEN, 12515450Sbrendan VDEV_AUX_CORRUPT_DATA); 12525450Sbrendan error = EIO; 12535450Sbrendan goto out; 12545450Sbrendan } 12555450Sbrendan 12565450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 12575450Sbrendan spa_load_l2cache(spa); 12585450Sbrendan spa_config_exit(spa, FTAG); 12595450Sbrendan } 12605450Sbrendan 12615094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 12624543Smarks 12633912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12643912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 12653912Slling 12663912Slling if (error && error != ENOENT) { 12673912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12683912Slling VDEV_AUX_CORRUPT_DATA); 12693912Slling error = EIO; 12703912Slling goto out; 12713912Slling } 12723912Slling 12733912Slling if (error == 0) { 12743912Slling (void) zap_lookup(spa->spa_meta_objset, 12753912Slling spa->spa_pool_props_object, 12764451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 12773912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 12784451Seschrock (void) zap_lookup(spa->spa_meta_objset, 12794451Seschrock spa->spa_pool_props_object, 12804451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 12814451Seschrock sizeof (uint64_t), 1, &autoreplace); 12824543Smarks (void) zap_lookup(spa->spa_meta_objset, 12834543Smarks spa->spa_pool_props_object, 12844543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 12854543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 12865329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 12875329Sgw25295 spa->spa_pool_props_object, 12885329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 12895329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 12903912Slling } 12913912Slling 12922082Seschrock /* 12934451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 12944451Seschrock * the ZFS DE that it should not issue any faults for unopenable 12954451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 12964451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 12974451Seschrock * over. 12984451Seschrock */ 12995756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 13004451Seschrock spa_check_removed(spa->spa_root_vdev); 13014451Seschrock 13024451Seschrock /* 13031986Seschrock * Load the vdev state for all toplevel vdevs. 1304789Sahrens */ 13051986Seschrock vdev_load(rvd); 1306789Sahrens 1307789Sahrens /* 1308789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1309789Sahrens */ 13101544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1311789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 13121544Seschrock spa_config_exit(spa, FTAG); 1313789Sahrens 1314789Sahrens /* 1315789Sahrens * Check the state of the root vdev. If it can't be opened, it 1316789Sahrens * indicates one or more toplevel vdevs are faulted. 1317789Sahrens */ 13181544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13191544Seschrock error = ENXIO; 13201544Seschrock goto out; 13211544Seschrock } 1322789Sahrens 13231544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 13241635Sbonwick dmu_tx_t *tx; 13251635Sbonwick int need_update = B_FALSE; 13261585Sbonwick int c; 13271601Sbonwick 13281635Sbonwick /* 13291635Sbonwick * Claim log blocks that haven't been committed yet. 13301635Sbonwick * This must all happen in a single txg. 13311635Sbonwick */ 13321601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1333789Sahrens spa_first_txg(spa)); 13342417Sahrens (void) dmu_objset_find(spa->spa_name, 13352417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1336789Sahrens dmu_tx_commit(tx); 1337789Sahrens 1338789Sahrens spa->spa_sync_on = B_TRUE; 1339789Sahrens txg_sync_start(spa->spa_dsl_pool); 1340789Sahrens 1341789Sahrens /* 1342789Sahrens * Wait for all claims to sync. 1343789Sahrens */ 1344789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 13451585Sbonwick 13461585Sbonwick /* 13471635Sbonwick * If the config cache is stale, or we have uninitialized 13481635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 13491585Sbonwick */ 13501635Sbonwick if (config_cache_txg != spa->spa_config_txg || 13511635Sbonwick state == SPA_LOAD_IMPORT) 13521635Sbonwick need_update = B_TRUE; 13531635Sbonwick 13541635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 13551635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 13561635Sbonwick need_update = B_TRUE; 13571585Sbonwick 13581585Sbonwick /* 13591635Sbonwick * Update the config cache asychronously in case we're the 13601635Sbonwick * root pool, in which case the config cache isn't writable yet. 13611585Sbonwick */ 13621635Sbonwick if (need_update) 13631635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1364789Sahrens } 1365789Sahrens 13661544Seschrock error = 0; 13671544Seschrock out: 1368*7046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 13692082Seschrock if (error && error != EBADF) 13701544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 13711544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 13721544Seschrock spa->spa_ena = 0; 13731544Seschrock 13741544Seschrock return (error); 1375789Sahrens } 1376789Sahrens 1377789Sahrens /* 1378789Sahrens * Pool Open/Import 1379789Sahrens * 1380789Sahrens * The import case is identical to an open except that the configuration is sent 1381789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1382789Sahrens * case of an open, the pool configuration will exist in the 13834451Seschrock * POOL_STATE_UNINITIALIZED state. 1384789Sahrens * 1385789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1386789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1387789Sahrens * ambiguous state. 1388789Sahrens */ 1389789Sahrens static int 1390789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1391789Sahrens { 1392789Sahrens spa_t *spa; 1393789Sahrens int error; 1394789Sahrens int locked = B_FALSE; 1395789Sahrens 1396789Sahrens *spapp = NULL; 1397789Sahrens 1398789Sahrens /* 1399789Sahrens * As disgusting as this is, we need to support recursive calls to this 1400789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1401789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1402789Sahrens * avoid dsl_dir_open() calling this in the first place. 1403789Sahrens */ 1404789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1405789Sahrens mutex_enter(&spa_namespace_lock); 1406789Sahrens locked = B_TRUE; 1407789Sahrens } 1408789Sahrens 1409789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1410789Sahrens if (locked) 1411789Sahrens mutex_exit(&spa_namespace_lock); 1412789Sahrens return (ENOENT); 1413789Sahrens } 1414789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1415789Sahrens 1416789Sahrens spa_activate(spa); 1417789Sahrens 14181635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1419789Sahrens 1420789Sahrens if (error == EBADF) { 1421789Sahrens /* 14221986Seschrock * If vdev_validate() returns failure (indicated by 14231986Seschrock * EBADF), it indicates that one of the vdevs indicates 14241986Seschrock * that the pool has been exported or destroyed. If 14251986Seschrock * this is the case, the config cache is out of sync and 14261986Seschrock * we should remove the pool from the namespace. 1427789Sahrens */ 1428789Sahrens spa_unload(spa); 1429789Sahrens spa_deactivate(spa); 14306643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1431789Sahrens spa_remove(spa); 1432789Sahrens if (locked) 1433789Sahrens mutex_exit(&spa_namespace_lock); 1434789Sahrens return (ENOENT); 14351544Seschrock } 14361544Seschrock 14371544Seschrock if (error) { 1438789Sahrens /* 1439789Sahrens * We can't open the pool, but we still have useful 1440789Sahrens * information: the state of each vdev after the 1441789Sahrens * attempted vdev_open(). Return this to the user. 1442789Sahrens */ 14431635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 14441635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1445789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1446789Sahrens B_TRUE); 14471635Sbonwick spa_config_exit(spa, FTAG); 14481635Sbonwick } 1449789Sahrens spa_unload(spa); 1450789Sahrens spa_deactivate(spa); 14511544Seschrock spa->spa_last_open_failed = B_TRUE; 1452789Sahrens if (locked) 1453789Sahrens mutex_exit(&spa_namespace_lock); 1454789Sahrens *spapp = NULL; 1455789Sahrens return (error); 14561544Seschrock } else { 14571544Seschrock spa->spa_last_open_failed = B_FALSE; 1458789Sahrens } 1459789Sahrens } 1460789Sahrens 1461789Sahrens spa_open_ref(spa, tag); 14624451Seschrock 1463789Sahrens if (locked) 1464789Sahrens mutex_exit(&spa_namespace_lock); 1465789Sahrens 1466789Sahrens *spapp = spa; 1467789Sahrens 1468789Sahrens if (config != NULL) { 14691544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1470789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 14711544Seschrock spa_config_exit(spa, FTAG); 1472789Sahrens } 1473789Sahrens 1474789Sahrens return (0); 1475789Sahrens } 1476789Sahrens 1477789Sahrens int 1478789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1479789Sahrens { 1480789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1481789Sahrens } 1482789Sahrens 14831544Seschrock /* 14841544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 14851544Seschrock * preventing it from being exported or destroyed. 14861544Seschrock */ 14871544Seschrock spa_t * 14881544Seschrock spa_inject_addref(char *name) 14891544Seschrock { 14901544Seschrock spa_t *spa; 14911544Seschrock 14921544Seschrock mutex_enter(&spa_namespace_lock); 14931544Seschrock if ((spa = spa_lookup(name)) == NULL) { 14941544Seschrock mutex_exit(&spa_namespace_lock); 14951544Seschrock return (NULL); 14961544Seschrock } 14971544Seschrock spa->spa_inject_ref++; 14981544Seschrock mutex_exit(&spa_namespace_lock); 14991544Seschrock 15001544Seschrock return (spa); 15011544Seschrock } 15021544Seschrock 15031544Seschrock void 15041544Seschrock spa_inject_delref(spa_t *spa) 15051544Seschrock { 15061544Seschrock mutex_enter(&spa_namespace_lock); 15071544Seschrock spa->spa_inject_ref--; 15081544Seschrock mutex_exit(&spa_namespace_lock); 15091544Seschrock } 15101544Seschrock 15115450Sbrendan /* 15125450Sbrendan * Add spares device information to the nvlist. 15135450Sbrendan */ 15142082Seschrock static void 15152082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 15162082Seschrock { 15172082Seschrock nvlist_t **spares; 15182082Seschrock uint_t i, nspares; 15192082Seschrock nvlist_t *nvroot; 15202082Seschrock uint64_t guid; 15212082Seschrock vdev_stat_t *vs; 15222082Seschrock uint_t vsc; 15233377Seschrock uint64_t pool; 15242082Seschrock 15255450Sbrendan if (spa->spa_spares.sav_count == 0) 15262082Seschrock return; 15272082Seschrock 15282082Seschrock VERIFY(nvlist_lookup_nvlist(config, 15292082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15305450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 15312082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15322082Seschrock if (nspares != 0) { 15332082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 15342082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 15352082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 15362082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15372082Seschrock 15382082Seschrock /* 15392082Seschrock * Go through and find any spares which have since been 15402082Seschrock * repurposed as an active spare. If this is the case, update 15412082Seschrock * their status appropriately. 15422082Seschrock */ 15432082Seschrock for (i = 0; i < nspares; i++) { 15442082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 15452082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 15463377Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 15472082Seschrock VERIFY(nvlist_lookup_uint64_array( 15482082Seschrock spares[i], ZPOOL_CONFIG_STATS, 15492082Seschrock (uint64_t **)&vs, &vsc) == 0); 15502082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 15512082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 15522082Seschrock } 15532082Seschrock } 15542082Seschrock } 15552082Seschrock } 15562082Seschrock 15575450Sbrendan /* 15585450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 15595450Sbrendan */ 15605450Sbrendan static void 15615450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 15625450Sbrendan { 15635450Sbrendan nvlist_t **l2cache; 15645450Sbrendan uint_t i, j, nl2cache; 15655450Sbrendan nvlist_t *nvroot; 15665450Sbrendan uint64_t guid; 15675450Sbrendan vdev_t *vd; 15685450Sbrendan vdev_stat_t *vs; 15695450Sbrendan uint_t vsc; 15705450Sbrendan 15715450Sbrendan if (spa->spa_l2cache.sav_count == 0) 15725450Sbrendan return; 15735450Sbrendan 15745450Sbrendan spa_config_enter(spa, RW_READER, FTAG); 15755450Sbrendan 15765450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 15775450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15785450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 15795450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 15805450Sbrendan if (nl2cache != 0) { 15815450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 15825450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 15835450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 15845450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 15855450Sbrendan 15865450Sbrendan /* 15875450Sbrendan * Update level 2 cache device stats. 15885450Sbrendan */ 15895450Sbrendan 15905450Sbrendan for (i = 0; i < nl2cache; i++) { 15915450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 15925450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 15935450Sbrendan 15945450Sbrendan vd = NULL; 15955450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 15965450Sbrendan if (guid == 15975450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 15985450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 15995450Sbrendan break; 16005450Sbrendan } 16015450Sbrendan } 16025450Sbrendan ASSERT(vd != NULL); 16035450Sbrendan 16045450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 16055450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 16065450Sbrendan vdev_get_stats(vd, vs); 16075450Sbrendan } 16085450Sbrendan } 16095450Sbrendan 16105450Sbrendan spa_config_exit(spa, FTAG); 16115450Sbrendan } 16125450Sbrendan 1613789Sahrens int 16141544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1615789Sahrens { 1616789Sahrens int error; 1617789Sahrens spa_t *spa; 1618789Sahrens 1619789Sahrens *config = NULL; 1620789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1621789Sahrens 16222082Seschrock if (spa && *config != NULL) { 16231544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 16241544Seschrock spa_get_errlog_size(spa)) == 0); 16251544Seschrock 16262082Seschrock spa_add_spares(spa, *config); 16275450Sbrendan spa_add_l2cache(spa, *config); 16282082Seschrock } 16292082Seschrock 16301544Seschrock /* 16311544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 16321544Seschrock * and call spa_lookup() directly. 16331544Seschrock */ 16341544Seschrock if (altroot) { 16351544Seschrock if (spa == NULL) { 16361544Seschrock mutex_enter(&spa_namespace_lock); 16371544Seschrock spa = spa_lookup(name); 16381544Seschrock if (spa) 16391544Seschrock spa_altroot(spa, altroot, buflen); 16401544Seschrock else 16411544Seschrock altroot[0] = '\0'; 16421544Seschrock spa = NULL; 16431544Seschrock mutex_exit(&spa_namespace_lock); 16441544Seschrock } else { 16451544Seschrock spa_altroot(spa, altroot, buflen); 16461544Seschrock } 16471544Seschrock } 16481544Seschrock 1649789Sahrens if (spa != NULL) 1650789Sahrens spa_close(spa, FTAG); 1651789Sahrens 1652789Sahrens return (error); 1653789Sahrens } 1654789Sahrens 1655789Sahrens /* 16565450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 16575450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 16585450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 16595450Sbrendan * specified, as long as they are well-formed. 16602082Seschrock */ 16612082Seschrock static int 16625450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 16635450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 16645450Sbrendan vdev_labeltype_t label) 16652082Seschrock { 16665450Sbrendan nvlist_t **dev; 16675450Sbrendan uint_t i, ndev; 16682082Seschrock vdev_t *vd; 16692082Seschrock int error; 16702082Seschrock 16712082Seschrock /* 16725450Sbrendan * It's acceptable to have no devs specified. 16732082Seschrock */ 16745450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 16752082Seschrock return (0); 16762082Seschrock 16775450Sbrendan if (ndev == 0) 16782082Seschrock return (EINVAL); 16792082Seschrock 16802082Seschrock /* 16815450Sbrendan * Make sure the pool is formatted with a version that supports this 16825450Sbrendan * device type. 16832082Seschrock */ 16845450Sbrendan if (spa_version(spa) < version) 16852082Seschrock return (ENOTSUP); 16862082Seschrock 16873377Seschrock /* 16885450Sbrendan * Set the pending device list so we correctly handle device in-use 16893377Seschrock * checking. 16903377Seschrock */ 16915450Sbrendan sav->sav_pending = dev; 16925450Sbrendan sav->sav_npending = ndev; 16935450Sbrendan 16945450Sbrendan for (i = 0; i < ndev; i++) { 16955450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 16962082Seschrock mode)) != 0) 16973377Seschrock goto out; 16982082Seschrock 16992082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 17002082Seschrock vdev_free(vd); 17013377Seschrock error = EINVAL; 17023377Seschrock goto out; 17032082Seschrock } 17042082Seschrock 17055450Sbrendan /* 17065450Sbrendan * The L2ARC currently only supports disk devices. 17075450Sbrendan */ 17085450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 17095450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 17105450Sbrendan error = ENOTBLK; 17115450Sbrendan goto out; 17125450Sbrendan } 17135450Sbrendan 17142082Seschrock vd->vdev_top = vd; 17153377Seschrock 17163377Seschrock if ((error = vdev_open(vd)) == 0 && 17175450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 17185450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 17193377Seschrock vd->vdev_guid) == 0); 17202082Seschrock } 17212082Seschrock 17222082Seschrock vdev_free(vd); 17233377Seschrock 17245450Sbrendan if (error && 17255450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 17263377Seschrock goto out; 17273377Seschrock else 17283377Seschrock error = 0; 17292082Seschrock } 17302082Seschrock 17313377Seschrock out: 17325450Sbrendan sav->sav_pending = NULL; 17335450Sbrendan sav->sav_npending = 0; 17343377Seschrock return (error); 17352082Seschrock } 17362082Seschrock 17375450Sbrendan static int 17385450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 17395450Sbrendan { 17405450Sbrendan int error; 17415450Sbrendan 17425450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17435450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 17445450Sbrendan VDEV_LABEL_SPARE)) != 0) { 17455450Sbrendan return (error); 17465450Sbrendan } 17475450Sbrendan 17485450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17495450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 17505450Sbrendan VDEV_LABEL_L2CACHE)); 17515450Sbrendan } 17525450Sbrendan 17535450Sbrendan static void 17545450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 17555450Sbrendan const char *config) 17565450Sbrendan { 17575450Sbrendan int i; 17585450Sbrendan 17595450Sbrendan if (sav->sav_config != NULL) { 17605450Sbrendan nvlist_t **olddevs; 17615450Sbrendan uint_t oldndevs; 17625450Sbrendan nvlist_t **newdevs; 17635450Sbrendan 17645450Sbrendan /* 17655450Sbrendan * Generate new dev list by concatentating with the 17665450Sbrendan * current dev list. 17675450Sbrendan */ 17685450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 17695450Sbrendan &olddevs, &oldndevs) == 0); 17705450Sbrendan 17715450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 17725450Sbrendan (ndevs + oldndevs), KM_SLEEP); 17735450Sbrendan for (i = 0; i < oldndevs; i++) 17745450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 17755450Sbrendan KM_SLEEP) == 0); 17765450Sbrendan for (i = 0; i < ndevs; i++) 17775450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 17785450Sbrendan KM_SLEEP) == 0); 17795450Sbrendan 17805450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 17815450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 17825450Sbrendan 17835450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 17845450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 17855450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 17865450Sbrendan nvlist_free(newdevs[i]); 17875450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 17885450Sbrendan } else { 17895450Sbrendan /* 17905450Sbrendan * Generate a new dev list. 17915450Sbrendan */ 17925450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 17935450Sbrendan KM_SLEEP) == 0); 17945450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 17955450Sbrendan devs, ndevs) == 0); 17965450Sbrendan } 17975450Sbrendan } 17985450Sbrendan 17995450Sbrendan /* 18005450Sbrendan * Stop and drop level 2 ARC devices 18015450Sbrendan */ 18025450Sbrendan void 18035450Sbrendan spa_l2cache_drop(spa_t *spa) 18045450Sbrendan { 18055450Sbrendan vdev_t *vd; 18065450Sbrendan int i; 18075450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 18085450Sbrendan 18095450Sbrendan for (i = 0; i < sav->sav_count; i++) { 18105450Sbrendan uint64_t pool; 18115450Sbrendan 18125450Sbrendan vd = sav->sav_vdevs[i]; 18135450Sbrendan ASSERT(vd != NULL); 18145450Sbrendan 18155450Sbrendan if (spa_mode & FWRITE && 18166643Seschrock spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL && 18176643Seschrock l2arc_vdev_present(vd)) { 18185450Sbrendan l2arc_remove_vdev(vd); 18195450Sbrendan } 18205450Sbrendan if (vd->vdev_isl2cache) 18215450Sbrendan spa_l2cache_remove(vd); 18225450Sbrendan vdev_clear_stats(vd); 18235450Sbrendan (void) vdev_close(vd); 18245450Sbrendan } 18255450Sbrendan } 18265450Sbrendan 18272082Seschrock /* 1828789Sahrens * Pool Creation 1829789Sahrens */ 1830789Sahrens int 18315094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 18324715Sek110237 const char *history_str) 1833789Sahrens { 1834789Sahrens spa_t *spa; 18355094Slling char *altroot = NULL; 18361635Sbonwick vdev_t *rvd; 1837789Sahrens dsl_pool_t *dp; 1838789Sahrens dmu_tx_t *tx; 18392082Seschrock int c, error = 0; 1840789Sahrens uint64_t txg = TXG_INITIAL; 18415450Sbrendan nvlist_t **spares, **l2cache; 18425450Sbrendan uint_t nspares, nl2cache; 18435094Slling uint64_t version; 1844789Sahrens 1845789Sahrens /* 1846789Sahrens * If this pool already exists, return failure. 1847789Sahrens */ 1848789Sahrens mutex_enter(&spa_namespace_lock); 1849789Sahrens if (spa_lookup(pool) != NULL) { 1850789Sahrens mutex_exit(&spa_namespace_lock); 1851789Sahrens return (EEXIST); 1852789Sahrens } 1853789Sahrens 1854789Sahrens /* 1855789Sahrens * Allocate a new spa_t structure. 1856789Sahrens */ 18575094Slling (void) nvlist_lookup_string(props, 18585094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 18591635Sbonwick spa = spa_add(pool, altroot); 1860789Sahrens spa_activate(spa); 1861789Sahrens 1862789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 18635094Slling 18645094Slling if (props && (error = spa_prop_validate(spa, props))) { 18655094Slling spa_unload(spa); 18665094Slling spa_deactivate(spa); 18675094Slling spa_remove(spa); 18686643Seschrock mutex_exit(&spa_namespace_lock); 18695094Slling return (error); 18705094Slling } 18715094Slling 18725094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 18735094Slling &version) != 0) 18745094Slling version = SPA_VERSION; 18755094Slling ASSERT(version <= SPA_VERSION); 18765094Slling spa->spa_uberblock.ub_version = version; 1877789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1878789Sahrens 18791635Sbonwick /* 18801635Sbonwick * Create the root vdev. 18811635Sbonwick */ 18821635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 18831635Sbonwick 18842082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 18852082Seschrock 18862082Seschrock ASSERT(error != 0 || rvd != NULL); 18872082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 18882082Seschrock 18895913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 18901635Sbonwick error = EINVAL; 18912082Seschrock 18922082Seschrock if (error == 0 && 18932082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 18945450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 18952082Seschrock VDEV_ALLOC_ADD)) == 0) { 18962082Seschrock for (c = 0; c < rvd->vdev_children; c++) 18972082Seschrock vdev_init(rvd->vdev_child[c], txg); 18982082Seschrock vdev_config_dirty(rvd); 18991635Sbonwick } 19001635Sbonwick 19011635Sbonwick spa_config_exit(spa, FTAG); 1902789Sahrens 19032082Seschrock if (error != 0) { 1904789Sahrens spa_unload(spa); 1905789Sahrens spa_deactivate(spa); 1906789Sahrens spa_remove(spa); 1907789Sahrens mutex_exit(&spa_namespace_lock); 1908789Sahrens return (error); 1909789Sahrens } 1910789Sahrens 19112082Seschrock /* 19122082Seschrock * Get the list of spares, if specified. 19132082Seschrock */ 19142082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 19152082Seschrock &spares, &nspares) == 0) { 19165450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 19172082Seschrock KM_SLEEP) == 0); 19185450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 19192082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 19202082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 19212082Seschrock spa_load_spares(spa); 19222082Seschrock spa_config_exit(spa, FTAG); 19235450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 19245450Sbrendan } 19255450Sbrendan 19265450Sbrendan /* 19275450Sbrendan * Get the list of level 2 cache devices, if specified. 19285450Sbrendan */ 19295450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 19305450Sbrendan &l2cache, &nl2cache) == 0) { 19315450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 19325450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 19335450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 19345450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 19355450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 19365450Sbrendan spa_load_l2cache(spa); 19375450Sbrendan spa_config_exit(spa, FTAG); 19385450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 19392082Seschrock } 19402082Seschrock 1941789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1942789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1943789Sahrens 1944789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1945789Sahrens 1946789Sahrens /* 1947789Sahrens * Create the pool config object. 1948789Sahrens */ 1949789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1950789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1951789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1952789Sahrens 19531544Seschrock if (zap_add(spa->spa_meta_objset, 1954789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 19551544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 19561544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 19571544Seschrock } 1958789Sahrens 19595094Slling /* Newly created pools with the right version are always deflated. */ 19605094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 19615094Slling spa->spa_deflate = TRUE; 19625094Slling if (zap_add(spa->spa_meta_objset, 19635094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 19645094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 19655094Slling cmn_err(CE_PANIC, "failed to add deflate"); 19665094Slling } 19672082Seschrock } 19682082Seschrock 1969789Sahrens /* 1970789Sahrens * Create the deferred-free bplist object. Turn off compression 1971789Sahrens * because sync-to-convergence takes longer if the blocksize 1972789Sahrens * keeps changing. 1973789Sahrens */ 1974789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1975789Sahrens 1 << 14, tx); 1976789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1977789Sahrens ZIO_COMPRESS_OFF, tx); 1978789Sahrens 19791544Seschrock if (zap_add(spa->spa_meta_objset, 1980789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 19811544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 19821544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 19831544Seschrock } 1984789Sahrens 19852926Sek110237 /* 19862926Sek110237 * Create the pool's history object. 19872926Sek110237 */ 19885094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 19895094Slling spa_history_create_obj(spa, tx); 19905094Slling 19915094Slling /* 19925094Slling * Set pool properties. 19935094Slling */ 19945094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 19955094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 19965329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 19975094Slling if (props) 19985094Slling spa_sync_props(spa, props, CRED(), tx); 19992926Sek110237 2000789Sahrens dmu_tx_commit(tx); 2001789Sahrens 2002789Sahrens spa->spa_sync_on = B_TRUE; 2003789Sahrens txg_sync_start(spa->spa_dsl_pool); 2004789Sahrens 2005789Sahrens /* 2006789Sahrens * We explicitly wait for the first transaction to complete so that our 2007789Sahrens * bean counters are appropriately updated. 2008789Sahrens */ 2009789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2010789Sahrens 20116643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2012789Sahrens 20135094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 20144715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 20154715Sek110237 2016789Sahrens mutex_exit(&spa_namespace_lock); 2017789Sahrens 2018*7046Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 2019*7046Sahrens 2020789Sahrens return (0); 2021789Sahrens } 2022789Sahrens 2023789Sahrens /* 2024789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2025789Sahrens * then call spa_load() to do the dirty work. 2026789Sahrens */ 20276423Sgw25295 static int 20286423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 20296643Seschrock boolean_t isroot, boolean_t allowfaulted) 2030789Sahrens { 2031789Sahrens spa_t *spa; 20325094Slling char *altroot = NULL; 20336643Seschrock int error, loaderr; 20342082Seschrock nvlist_t *nvroot; 20355450Sbrendan nvlist_t **spares, **l2cache; 20365450Sbrendan uint_t nspares, nl2cache; 2037789Sahrens 2038789Sahrens /* 2039789Sahrens * If a pool with this name exists, return failure. 2040789Sahrens */ 2041789Sahrens mutex_enter(&spa_namespace_lock); 2042789Sahrens if (spa_lookup(pool) != NULL) { 2043789Sahrens mutex_exit(&spa_namespace_lock); 2044789Sahrens return (EEXIST); 2045789Sahrens } 2046789Sahrens 2047789Sahrens /* 20481635Sbonwick * Create and initialize the spa structure. 2049789Sahrens */ 20505094Slling (void) nvlist_lookup_string(props, 20515094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 20521635Sbonwick spa = spa_add(pool, altroot); 2053789Sahrens spa_activate(spa); 2054789Sahrens 20556643Seschrock if (allowfaulted) 20566643Seschrock spa->spa_import_faulted = B_TRUE; 20576673Seschrock spa->spa_is_root = isroot; 20586643Seschrock 2059789Sahrens /* 20601635Sbonwick * Pass off the heavy lifting to spa_load(). 2061*7046Sahrens * Pass TRUE for mosconfig (unless this is a root pool) because 2062*7046Sahrens * the user-supplied config is actually the one to trust when 2063*7046Sahrens * doing an import. 20641601Sbonwick */ 2065*7046Sahrens loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot); 2066789Sahrens 20672082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20682082Seschrock /* 20692082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 20702082Seschrock * and conflicts with spa_has_spare(). 20712082Seschrock */ 20726423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 20735450Sbrendan nvlist_free(spa->spa_spares.sav_config); 20745450Sbrendan spa->spa_spares.sav_config = NULL; 20752082Seschrock spa_load_spares(spa); 20762082Seschrock } 20776423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 20785450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 20795450Sbrendan spa->spa_l2cache.sav_config = NULL; 20805450Sbrendan spa_load_l2cache(spa); 20815450Sbrendan } 20822082Seschrock 20832082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 20842082Seschrock &nvroot) == 0); 20855450Sbrendan if (error == 0) 20865450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 20875450Sbrendan if (error == 0) 20885450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 20895450Sbrendan VDEV_ALLOC_L2CACHE); 20902082Seschrock spa_config_exit(spa, FTAG); 20912082Seschrock 20925094Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 20936643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 20946643Seschrock /* 20956643Seschrock * If we failed to load the pool, but 'allowfaulted' is 20966643Seschrock * set, then manually set the config as if the config 20976643Seschrock * passed in was specified in the cache file. 20986643Seschrock */ 20996643Seschrock error = 0; 21006643Seschrock spa->spa_import_faulted = B_FALSE; 21016643Seschrock if (spa->spa_config == NULL) { 21026643Seschrock spa_config_enter(spa, RW_READER, FTAG); 21036643Seschrock spa->spa_config = spa_config_generate(spa, 21046643Seschrock NULL, -1ULL, B_TRUE); 21056643Seschrock spa_config_exit(spa, FTAG); 21066643Seschrock } 21076643Seschrock spa_unload(spa); 21086643Seschrock spa_deactivate(spa); 21096643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 21106643Seschrock } else { 21116643Seschrock spa_unload(spa); 21126643Seschrock spa_deactivate(spa); 21136643Seschrock spa_remove(spa); 21146643Seschrock } 2115789Sahrens mutex_exit(&spa_namespace_lock); 2116789Sahrens return (error); 2117789Sahrens } 2118789Sahrens 21191635Sbonwick /* 21205450Sbrendan * Override any spares and level 2 cache devices as specified by 21215450Sbrendan * the user, as these may have correct device names/devids, etc. 21222082Seschrock */ 21232082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 21242082Seschrock &spares, &nspares) == 0) { 21255450Sbrendan if (spa->spa_spares.sav_config) 21265450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 21272082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 21282082Seschrock else 21295450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 21302082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 21315450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 21322082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 21332082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21342082Seschrock spa_load_spares(spa); 21352082Seschrock spa_config_exit(spa, FTAG); 21365450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 21375450Sbrendan } 21385450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 21395450Sbrendan &l2cache, &nl2cache) == 0) { 21405450Sbrendan if (spa->spa_l2cache.sav_config) 21415450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 21425450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 21435450Sbrendan else 21445450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 21455450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 21465450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 21475450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21485450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 21495450Sbrendan spa_load_l2cache(spa); 21505450Sbrendan spa_config_exit(spa, FTAG); 21515450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 21522082Seschrock } 21532082Seschrock 21546643Seschrock if (spa_mode & FWRITE) { 21556643Seschrock /* 21566643Seschrock * Update the config cache to include the newly-imported pool. 21576643Seschrock */ 21586423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 21596643Seschrock } 21606643Seschrock 21616643Seschrock spa->spa_import_faulted = B_FALSE; 21624451Seschrock mutex_exit(&spa_namespace_lock); 21634451Seschrock 2164789Sahrens return (0); 2165789Sahrens } 2166789Sahrens 21676423Sgw25295 #ifdef _KERNEL 21686423Sgw25295 /* 21696423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 21706423Sgw25295 * device label. 21716423Sgw25295 */ 21726423Sgw25295 static void 21736423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 21746423Sgw25295 { 21756423Sgw25295 nvlist_t *nvtop, *nvroot; 21766423Sgw25295 uint64_t pgid; 21776423Sgw25295 21786423Sgw25295 /* 21796423Sgw25295 * Add this top-level vdev to the child array. 21806423Sgw25295 */ 21816423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 21826423Sgw25295 == 0); 21836423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 21846423Sgw25295 == 0); 21856423Sgw25295 21866423Sgw25295 /* 21876423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 21886423Sgw25295 */ 21896423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 21906423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 21916423Sgw25295 == 0); 21926423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 21936423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 21946423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 21956423Sgw25295 &nvtop, 1) == 0); 21966423Sgw25295 21976423Sgw25295 /* 21986423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 21996423Sgw25295 * this pool's configuration (remove the old, add the new). 22006423Sgw25295 */ 22016423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 22026423Sgw25295 nvlist_free(nvroot); 22036423Sgw25295 } 22046423Sgw25295 22056423Sgw25295 /* 22066423Sgw25295 * Get the root pool information from the root disk, then import the root pool 22076423Sgw25295 * during the system boot up time. 22086423Sgw25295 */ 22096423Sgw25295 extern nvlist_t *vdev_disk_read_rootlabel(char *); 22106423Sgw25295 22116423Sgw25295 void 22126423Sgw25295 spa_check_rootconf(char *devpath, char **bestdev, nvlist_t **bestconf, 22136423Sgw25295 uint64_t *besttxg) 22146423Sgw25295 { 22156423Sgw25295 nvlist_t *config; 22166423Sgw25295 uint64_t txg; 22176423Sgw25295 22186423Sgw25295 if ((config = vdev_disk_read_rootlabel(devpath)) == NULL) 22196423Sgw25295 return; 22206423Sgw25295 22216423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 22226423Sgw25295 22236423Sgw25295 if (txg > *besttxg) { 22246423Sgw25295 *besttxg = txg; 22256423Sgw25295 if (*bestconf != NULL) 22266423Sgw25295 nvlist_free(*bestconf); 22276423Sgw25295 *bestconf = config; 22286423Sgw25295 *bestdev = devpath; 22296423Sgw25295 } 22306423Sgw25295 } 22316423Sgw25295 22326423Sgw25295 boolean_t 22336423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 22346423Sgw25295 { 22356423Sgw25295 uint64_t ival; 22366423Sgw25295 22376423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 22386423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 22396423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, &ival) == 0 || 22406423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 22416423Sgw25295 return (B_FALSE); 22426423Sgw25295 22436423Sgw25295 return (B_TRUE); 22446423Sgw25295 } 22456423Sgw25295 22466423Sgw25295 /* 22476423Sgw25295 * Import a root pool. 22486423Sgw25295 * 22496423Sgw25295 * For x86. devpath_list will consist the physpath name of the vdev in a single 22506423Sgw25295 * disk root pool or a list of physnames for the vdevs in a mirrored rootpool. 22516423Sgw25295 * e.g. 22526423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a /pci@1f,o/ide@d/disk@2,0:a" 22536423Sgw25295 * 22546423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 22556423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 22566423Sgw25295 * e.g. 22576423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 22586423Sgw25295 */ 22596423Sgw25295 int 22606423Sgw25295 spa_import_rootpool(char *devpath_list) 22616423Sgw25295 { 22626423Sgw25295 nvlist_t *conf = NULL; 22636423Sgw25295 char *dev = NULL; 22646423Sgw25295 char *pname; 22656423Sgw25295 int error; 22666423Sgw25295 22676423Sgw25295 /* 22686423Sgw25295 * Get the vdev pathname and configuation from the most 22696423Sgw25295 * recently updated vdev (highest txg). 22706423Sgw25295 */ 22716423Sgw25295 if (error = spa_get_rootconf(devpath_list, &dev, &conf)) 22726423Sgw25295 goto msg_out; 22736423Sgw25295 22746423Sgw25295 /* 22756423Sgw25295 * Add type "root" vdev to the config. 22766423Sgw25295 */ 22776423Sgw25295 spa_build_rootpool_config(conf); 22786423Sgw25295 22796423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 22806423Sgw25295 22816673Seschrock /* 22826673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 22836673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 22846673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 22856673Seschrock * pool open, not import. 22866673Seschrock */ 22876673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 22886423Sgw25295 if (error == EEXIST) 22896423Sgw25295 error = 0; 22906423Sgw25295 22916423Sgw25295 nvlist_free(conf); 22926423Sgw25295 return (error); 22936423Sgw25295 22946423Sgw25295 msg_out: 22956423Sgw25295 cmn_err(CE_NOTE, "\n\n" 22966423Sgw25295 " *************************************************** \n" 22976423Sgw25295 " * This device is not bootable! * \n" 22986423Sgw25295 " * It is either offlined or detached or faulted. * \n" 22996423Sgw25295 " * Please try to boot from a different device. * \n" 23006423Sgw25295 " *************************************************** \n\n"); 23016423Sgw25295 23026423Sgw25295 return (error); 23036423Sgw25295 } 23046423Sgw25295 #endif 23056423Sgw25295 23066423Sgw25295 /* 23076423Sgw25295 * Import a non-root pool into the system. 23086423Sgw25295 */ 23096423Sgw25295 int 23106423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 23116423Sgw25295 { 23126643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 23136423Sgw25295 } 23146423Sgw25295 23156643Seschrock int 23166643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 23176643Seschrock { 23186643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 23196643Seschrock } 23206643Seschrock 23216643Seschrock 2322789Sahrens /* 2323789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2324789Sahrens * to get the vdev stats associated with the imported devices. 2325789Sahrens */ 2326789Sahrens #define TRYIMPORT_NAME "$import" 2327789Sahrens 2328789Sahrens nvlist_t * 2329789Sahrens spa_tryimport(nvlist_t *tryconfig) 2330789Sahrens { 2331789Sahrens nvlist_t *config = NULL; 2332789Sahrens char *poolname; 2333789Sahrens spa_t *spa; 2334789Sahrens uint64_t state; 2335789Sahrens 2336789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2337789Sahrens return (NULL); 2338789Sahrens 2339789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2340789Sahrens return (NULL); 2341789Sahrens 23421635Sbonwick /* 23431635Sbonwick * Create and initialize the spa structure. 23441635Sbonwick */ 2345789Sahrens mutex_enter(&spa_namespace_lock); 23461635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2347789Sahrens spa_activate(spa); 2348789Sahrens 2349789Sahrens /* 23501635Sbonwick * Pass off the heavy lifting to spa_load(). 23511732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 23521732Sbonwick * is actually the one to trust when doing an import. 2353789Sahrens */ 23541732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2355789Sahrens 2356789Sahrens /* 2357789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2358789Sahrens */ 2359789Sahrens if (spa->spa_root_vdev != NULL) { 23601635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 2361789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 23621635Sbonwick spa_config_exit(spa, FTAG); 2363789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2364789Sahrens poolname) == 0); 2365789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2366789Sahrens state) == 0); 23673975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 23683975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 23692082Seschrock 23702082Seschrock /* 23716423Sgw25295 * If the bootfs property exists on this pool then we 23726423Sgw25295 * copy it out so that external consumers can tell which 23736423Sgw25295 * pools are bootable. 23746423Sgw25295 */ 23756423Sgw25295 if (spa->spa_bootfs) { 23766423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 23776423Sgw25295 23786423Sgw25295 /* 23796423Sgw25295 * We have to play games with the name since the 23806423Sgw25295 * pool was opened as TRYIMPORT_NAME. 23816423Sgw25295 */ 23826423Sgw25295 if (dsl_dsobj_to_dsname(spa->spa_name, 23836423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 23846423Sgw25295 char *cp; 23856423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 23866423Sgw25295 23876423Sgw25295 cp = strchr(tmpname, '/'); 23886423Sgw25295 if (cp == NULL) { 23896423Sgw25295 (void) strlcpy(dsname, tmpname, 23906423Sgw25295 MAXPATHLEN); 23916423Sgw25295 } else { 23926423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 23936423Sgw25295 "%s/%s", poolname, ++cp); 23946423Sgw25295 } 23956423Sgw25295 VERIFY(nvlist_add_string(config, 23966423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 23976423Sgw25295 kmem_free(dsname, MAXPATHLEN); 23986423Sgw25295 } 23996423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 24006423Sgw25295 } 24016423Sgw25295 24026423Sgw25295 /* 24035450Sbrendan * Add the list of hot spares and level 2 cache devices. 24042082Seschrock */ 24052082Seschrock spa_add_spares(spa, config); 24065450Sbrendan spa_add_l2cache(spa, config); 2407789Sahrens } 2408789Sahrens 2409789Sahrens spa_unload(spa); 2410789Sahrens spa_deactivate(spa); 2411789Sahrens spa_remove(spa); 2412789Sahrens mutex_exit(&spa_namespace_lock); 2413789Sahrens 2414789Sahrens return (config); 2415789Sahrens } 2416789Sahrens 2417789Sahrens /* 2418789Sahrens * Pool export/destroy 2419789Sahrens * 2420789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2421789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2422789Sahrens * update the pool state and sync all the labels to disk, removing the 2423789Sahrens * configuration from the cache afterwards. 2424789Sahrens */ 2425789Sahrens static int 24261775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 2427789Sahrens { 2428789Sahrens spa_t *spa; 2429789Sahrens 24301775Sbillm if (oldconfig) 24311775Sbillm *oldconfig = NULL; 24321775Sbillm 2433789Sahrens if (!(spa_mode & FWRITE)) 2434789Sahrens return (EROFS); 2435789Sahrens 2436789Sahrens mutex_enter(&spa_namespace_lock); 2437789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2438789Sahrens mutex_exit(&spa_namespace_lock); 2439789Sahrens return (ENOENT); 2440789Sahrens } 2441789Sahrens 2442789Sahrens /* 24431544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 24441544Seschrock * reacquire the namespace lock, and see if we can export. 24451544Seschrock */ 24461544Seschrock spa_open_ref(spa, FTAG); 24471544Seschrock mutex_exit(&spa_namespace_lock); 24481544Seschrock spa_async_suspend(spa); 24491544Seschrock mutex_enter(&spa_namespace_lock); 24501544Seschrock spa_close(spa, FTAG); 24511544Seschrock 24521544Seschrock /* 2453789Sahrens * The pool will be in core if it's openable, 2454789Sahrens * in which case we can modify its state. 2455789Sahrens */ 2456789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2457789Sahrens /* 2458789Sahrens * Objsets may be open only because they're dirty, so we 2459789Sahrens * have to force it to sync before checking spa_refcnt. 2460789Sahrens */ 2461789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2462789Sahrens 24631544Seschrock /* 24641544Seschrock * A pool cannot be exported or destroyed if there are active 24651544Seschrock * references. If we are resetting a pool, allow references by 24661544Seschrock * fault injection handlers. 24671544Seschrock */ 24681544Seschrock if (!spa_refcount_zero(spa) || 24691544Seschrock (spa->spa_inject_ref != 0 && 24701544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 24711544Seschrock spa_async_resume(spa); 2472789Sahrens mutex_exit(&spa_namespace_lock); 2473789Sahrens return (EBUSY); 2474789Sahrens } 2475789Sahrens 2476789Sahrens /* 2477789Sahrens * We want this to be reflected on every label, 2478789Sahrens * so mark them all dirty. spa_unload() will do the 2479789Sahrens * final sync that pushes these changes out. 2480789Sahrens */ 24811544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 24821601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 24831544Seschrock spa->spa_state = new_state; 24841635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 24851544Seschrock vdev_config_dirty(spa->spa_root_vdev); 24861601Sbonwick spa_config_exit(spa, FTAG); 24871544Seschrock } 2488789Sahrens } 2489789Sahrens 24904451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 24914451Seschrock 2492789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2493789Sahrens spa_unload(spa); 2494789Sahrens spa_deactivate(spa); 2495789Sahrens } 2496789Sahrens 24971775Sbillm if (oldconfig && spa->spa_config) 24981775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 24991775Sbillm 25001544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 25016643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 25021544Seschrock spa_remove(spa); 25031544Seschrock } 2504789Sahrens mutex_exit(&spa_namespace_lock); 2505789Sahrens 2506789Sahrens return (0); 2507789Sahrens } 2508789Sahrens 2509789Sahrens /* 2510789Sahrens * Destroy a storage pool. 2511789Sahrens */ 2512789Sahrens int 2513789Sahrens spa_destroy(char *pool) 2514789Sahrens { 25151775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 2516789Sahrens } 2517789Sahrens 2518789Sahrens /* 2519789Sahrens * Export a storage pool. 2520789Sahrens */ 2521789Sahrens int 25221775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 2523789Sahrens { 25241775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 2525789Sahrens } 2526789Sahrens 2527789Sahrens /* 25281544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 25291544Seschrock * from the namespace in any way. 25301544Seschrock */ 25311544Seschrock int 25321544Seschrock spa_reset(char *pool) 25331544Seschrock { 25341775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 25351544Seschrock } 25361544Seschrock 25371544Seschrock /* 2538789Sahrens * ========================================================================== 2539789Sahrens * Device manipulation 2540789Sahrens * ========================================================================== 2541789Sahrens */ 2542789Sahrens 2543789Sahrens /* 25444527Sperrin * Add a device to a storage pool. 2545789Sahrens */ 2546789Sahrens int 2547789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2548789Sahrens { 2549789Sahrens uint64_t txg; 25501635Sbonwick int c, error; 2551789Sahrens vdev_t *rvd = spa->spa_root_vdev; 25521585Sbonwick vdev_t *vd, *tvd; 25535450Sbrendan nvlist_t **spares, **l2cache; 25545450Sbrendan uint_t nspares, nl2cache; 2555789Sahrens 2556789Sahrens txg = spa_vdev_enter(spa); 2557789Sahrens 25582082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 25592082Seschrock VDEV_ALLOC_ADD)) != 0) 25602082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 25612082Seschrock 25623377Seschrock spa->spa_pending_vdev = vd; 2563789Sahrens 25645450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 25655450Sbrendan &nspares) != 0) 25662082Seschrock nspares = 0; 25672082Seschrock 25685450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 25695450Sbrendan &nl2cache) != 0) 25705450Sbrendan nl2cache = 0; 25715450Sbrendan 25725450Sbrendan if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) { 25733377Seschrock spa->spa_pending_vdev = NULL; 25742082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 25753377Seschrock } 25762082Seschrock 25772082Seschrock if (vd->vdev_children != 0) { 25783377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 25793377Seschrock spa->spa_pending_vdev = NULL; 25802082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 25812082Seschrock } 25822082Seschrock } 25832082Seschrock 25843377Seschrock /* 25855450Sbrendan * We must validate the spares and l2cache devices after checking the 25865450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 25873377Seschrock */ 25885450Sbrendan if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) { 25893377Seschrock spa->spa_pending_vdev = NULL; 25903377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 25913377Seschrock } 25923377Seschrock 25933377Seschrock spa->spa_pending_vdev = NULL; 25943377Seschrock 25953377Seschrock /* 25963377Seschrock * Transfer each new top-level vdev from vd to rvd. 25973377Seschrock */ 25983377Seschrock for (c = 0; c < vd->vdev_children; c++) { 25993377Seschrock tvd = vd->vdev_child[c]; 26003377Seschrock vdev_remove_child(vd, tvd); 26013377Seschrock tvd->vdev_id = rvd->vdev_children; 26023377Seschrock vdev_add_child(rvd, tvd); 26033377Seschrock vdev_config_dirty(tvd); 26043377Seschrock } 26053377Seschrock 26062082Seschrock if (nspares != 0) { 26075450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 26085450Sbrendan ZPOOL_CONFIG_SPARES); 26092082Seschrock spa_load_spares(spa); 26105450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 26115450Sbrendan } 26125450Sbrendan 26135450Sbrendan if (nl2cache != 0) { 26145450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 26155450Sbrendan ZPOOL_CONFIG_L2CACHE); 26165450Sbrendan spa_load_l2cache(spa); 26175450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2618789Sahrens } 2619789Sahrens 2620789Sahrens /* 26211585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 26221585Sbonwick * If other threads start allocating from these vdevs before we 26231585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 26241585Sbonwick * fail to open the pool because there are DVAs that the config cache 26251585Sbonwick * can't translate. Therefore, we first add the vdevs without 26261585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 26271635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 26281585Sbonwick * 26291585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 26301585Sbonwick * if we lose power at any point in this sequence, the remaining 26311585Sbonwick * steps will be completed the next time we load the pool. 2632789Sahrens */ 26331635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 26341585Sbonwick 26351635Sbonwick mutex_enter(&spa_namespace_lock); 26361635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 26371635Sbonwick mutex_exit(&spa_namespace_lock); 2638789Sahrens 26391635Sbonwick return (0); 2640789Sahrens } 2641789Sahrens 2642789Sahrens /* 2643789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2644789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2645789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2646789Sahrens * 2647789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2648789Sahrens * existing device; in this case the two devices are made into their own 26494451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2650789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2651789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2652789Sahrens * completion of resilvering, the first disk (the one being replaced) 2653789Sahrens * is automatically detached. 2654789Sahrens */ 2655789Sahrens int 26561544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2657789Sahrens { 2658789Sahrens uint64_t txg, open_txg; 2659789Sahrens int error; 2660789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2661789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 26622082Seschrock vdev_ops_t *pvops; 26634527Sperrin int is_log; 2664789Sahrens 2665789Sahrens txg = spa_vdev_enter(spa); 2666789Sahrens 26676643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2668789Sahrens 2669789Sahrens if (oldvd == NULL) 2670789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2671789Sahrens 26721585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 26731585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 26741585Sbonwick 2675789Sahrens pvd = oldvd->vdev_parent; 2676789Sahrens 26772082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 26784451Seschrock VDEV_ALLOC_ADD)) != 0) 26794451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 26804451Seschrock 26814451Seschrock if (newrootvd->vdev_children != 1) 2682789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2683789Sahrens 2684789Sahrens newvd = newrootvd->vdev_child[0]; 2685789Sahrens 2686789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2687789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2688789Sahrens 26892082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2690789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2691789Sahrens 26924527Sperrin /* 26934527Sperrin * Spares can't replace logs 26944527Sperrin */ 26954527Sperrin is_log = oldvd->vdev_islog; 26964527Sperrin if (is_log && newvd->vdev_isspare) 26974527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 26984527Sperrin 26992082Seschrock if (!replacing) { 27002082Seschrock /* 27012082Seschrock * For attach, the only allowable parent is a mirror or the root 27022082Seschrock * vdev. 27032082Seschrock */ 27042082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 27052082Seschrock pvd->vdev_ops != &vdev_root_ops) 27062082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27072082Seschrock 27082082Seschrock pvops = &vdev_mirror_ops; 27092082Seschrock } else { 27102082Seschrock /* 27112082Seschrock * Active hot spares can only be replaced by inactive hot 27122082Seschrock * spares. 27132082Seschrock */ 27142082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 27152082Seschrock pvd->vdev_child[1] == oldvd && 27162082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 27172082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27182082Seschrock 27192082Seschrock /* 27202082Seschrock * If the source is a hot spare, and the parent isn't already a 27212082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 27223377Seschrock * want to create a replacing vdev. The user is not allowed to 27233377Seschrock * attach to a spared vdev child unless the 'isspare' state is 27243377Seschrock * the same (spare replaces spare, non-spare replaces 27253377Seschrock * non-spare). 27262082Seschrock */ 27272082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 27282082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27293377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 27303377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 27313377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27322082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 27332082Seschrock newvd->vdev_isspare) 27342082Seschrock pvops = &vdev_spare_ops; 27352082Seschrock else 27362082Seschrock pvops = &vdev_replacing_ops; 27372082Seschrock } 27382082Seschrock 27391175Slling /* 27401175Slling * Compare the new device size with the replaceable/attachable 27411175Slling * device size. 27421175Slling */ 27431175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2744789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2745789Sahrens 27461732Sbonwick /* 27471732Sbonwick * The new device cannot have a higher alignment requirement 27481732Sbonwick * than the top-level vdev. 27491732Sbonwick */ 27501732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2751789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2752789Sahrens 2753789Sahrens /* 2754789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2755789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2756789Sahrens */ 2757789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2758789Sahrens spa_strfree(oldvd->vdev_path); 2759789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2760789Sahrens KM_SLEEP); 2761789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2762789Sahrens newvd->vdev_path, "old"); 2763789Sahrens if (oldvd->vdev_devid != NULL) { 2764789Sahrens spa_strfree(oldvd->vdev_devid); 2765789Sahrens oldvd->vdev_devid = NULL; 2766789Sahrens } 2767789Sahrens } 2768789Sahrens 2769789Sahrens /* 27702082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 27712082Seschrock * mirror/replacing/spare vdev above oldvd. 2772789Sahrens */ 2773789Sahrens if (pvd->vdev_ops != pvops) 2774789Sahrens pvd = vdev_add_parent(oldvd, pvops); 2775789Sahrens 2776789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2777789Sahrens ASSERT(pvd->vdev_ops == pvops); 2778789Sahrens ASSERT(oldvd->vdev_parent == pvd); 2779789Sahrens 2780789Sahrens /* 2781789Sahrens * Extract the new device from its root and add it to pvd. 2782789Sahrens */ 2783789Sahrens vdev_remove_child(newrootvd, newvd); 2784789Sahrens newvd->vdev_id = pvd->vdev_children; 2785789Sahrens vdev_add_child(pvd, newvd); 2786789Sahrens 27871544Seschrock /* 27881544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 27891544Seschrock * the addition of newvd may have decreased our parent's asize. 27901544Seschrock */ 27911544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 27921544Seschrock 2793789Sahrens tvd = newvd->vdev_top; 2794789Sahrens ASSERT(pvd->vdev_top == tvd); 2795789Sahrens ASSERT(tvd->vdev_parent == rvd); 2796789Sahrens 2797789Sahrens vdev_config_dirty(tvd); 2798789Sahrens 2799789Sahrens /* 2800789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2801789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2802789Sahrens */ 2803789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2804789Sahrens 2805789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2806789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2807789Sahrens open_txg - TXG_INITIAL + 1); 2808789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2809789Sahrens 28103377Seschrock if (newvd->vdev_isspare) 28113377Seschrock spa_spare_activate(newvd); 28121544Seschrock 2813789Sahrens /* 2814789Sahrens * Mark newvd's DTL dirty in this txg. 2815789Sahrens */ 28161732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2817789Sahrens 2818789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2819789Sahrens 2820789Sahrens /* 2821*7046Sahrens * Kick off a resilver to update newvd. 2822789Sahrens */ 2823*7046Sahrens VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 2824789Sahrens 2825789Sahrens return (0); 2826789Sahrens } 2827789Sahrens 2828789Sahrens /* 2829789Sahrens * Detach a device from a mirror or replacing vdev. 2830789Sahrens * If 'replace_done' is specified, only detach if the parent 2831789Sahrens * is a replacing vdev. 2832789Sahrens */ 2833789Sahrens int 28341544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2835789Sahrens { 2836789Sahrens uint64_t txg; 2837789Sahrens int c, t, error; 2838789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2839789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 28402082Seschrock boolean_t unspare = B_FALSE; 28412082Seschrock uint64_t unspare_guid; 28426673Seschrock size_t len; 2843789Sahrens 2844789Sahrens txg = spa_vdev_enter(spa); 2845789Sahrens 28466643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 2847789Sahrens 2848789Sahrens if (vd == NULL) 2849789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2850789Sahrens 28511585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 28521585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28531585Sbonwick 2854789Sahrens pvd = vd->vdev_parent; 2855789Sahrens 2856789Sahrens /* 2857789Sahrens * If replace_done is specified, only remove this device if it's 28582082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 28592082Seschrock * disk can be removed. 2860789Sahrens */ 28612082Seschrock if (replace_done) { 28622082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 28632082Seschrock if (vd->vdev_id != 0) 28642082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28652082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 28662082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28672082Seschrock } 28682082Seschrock } 28692082Seschrock 28702082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 28714577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 2872789Sahrens 2873789Sahrens /* 28742082Seschrock * Only mirror, replacing, and spare vdevs support detach. 2875789Sahrens */ 2876789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 28772082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 28782082Seschrock pvd->vdev_ops != &vdev_spare_ops) 2879789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2880789Sahrens 2881789Sahrens /* 2882789Sahrens * If there's only one replica, you can't detach it. 2883789Sahrens */ 2884789Sahrens if (pvd->vdev_children <= 1) 2885789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2886789Sahrens 2887789Sahrens /* 2888789Sahrens * If all siblings have non-empty DTLs, this device may have the only 2889789Sahrens * valid copy of the data, which means we cannot safely detach it. 2890789Sahrens * 2891789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 2892789Sahrens * precise DTL check. 2893789Sahrens */ 2894789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 2895789Sahrens uint64_t dirty; 2896789Sahrens 2897789Sahrens cvd = pvd->vdev_child[c]; 2898789Sahrens if (cvd == vd) 2899789Sahrens continue; 2900789Sahrens if (vdev_is_dead(cvd)) 2901789Sahrens continue; 2902789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 2903789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 2904789Sahrens cvd->vdev_dtl_scrub.sm_space; 2905789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 2906789Sahrens if (!dirty) 2907789Sahrens break; 2908789Sahrens } 29092082Seschrock 29102082Seschrock /* 29112082Seschrock * If we are a replacing or spare vdev, then we can always detach the 29122082Seschrock * latter child, as that is how one cancels the operation. 29132082Seschrock */ 29142082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 29152082Seschrock c == pvd->vdev_children) 2916789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2917789Sahrens 2918789Sahrens /* 29196673Seschrock * If we are detaching the second disk from a replacing vdev, then 29206673Seschrock * check to see if we changed the original vdev's path to have "/old" 29216673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 29226673Seschrock */ 29236673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 29246673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 29256673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 29266673Seschrock ASSERT(pvd->vdev_child[1] == vd); 29276673Seschrock cvd = pvd->vdev_child[0]; 29286673Seschrock len = strlen(vd->vdev_path); 29296673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 29306673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 29316673Seschrock spa_strfree(cvd->vdev_path); 29326673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 29336673Seschrock } 29346673Seschrock } 29356673Seschrock 29366673Seschrock /* 29372082Seschrock * If we are detaching the original disk from a spare, then it implies 29382082Seschrock * that the spare should become a real disk, and be removed from the 29392082Seschrock * active spare list for the pool. 29402082Seschrock */ 29412082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 29422082Seschrock vd->vdev_id == 0) 29432082Seschrock unspare = B_TRUE; 29442082Seschrock 29452082Seschrock /* 2946789Sahrens * Erase the disk labels so the disk can be used for other things. 2947789Sahrens * This must be done after all other error cases are handled, 2948789Sahrens * but before we disembowel vd (so we can still do I/O to it). 2949789Sahrens * But if we can't do it, don't treat the error as fatal -- 2950789Sahrens * it may be that the unwritability of the disk is the reason 2951789Sahrens * it's being detached! 2952789Sahrens */ 29533377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 2954789Sahrens 2955789Sahrens /* 2956789Sahrens * Remove vd from its parent and compact the parent's children. 2957789Sahrens */ 2958789Sahrens vdev_remove_child(pvd, vd); 2959789Sahrens vdev_compact_children(pvd); 2960789Sahrens 2961789Sahrens /* 2962789Sahrens * Remember one of the remaining children so we can get tvd below. 2963789Sahrens */ 2964789Sahrens cvd = pvd->vdev_child[0]; 2965789Sahrens 2966789Sahrens /* 29672082Seschrock * If we need to remove the remaining child from the list of hot spares, 29682082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 29692082Seschrock * must do this before vdev_remove_parent(), because that can change the 29702082Seschrock * GUID if it creates a new toplevel GUID. 29712082Seschrock */ 29722082Seschrock if (unspare) { 29732082Seschrock ASSERT(cvd->vdev_isspare); 29743377Seschrock spa_spare_remove(cvd); 29752082Seschrock unspare_guid = cvd->vdev_guid; 29762082Seschrock } 29772082Seschrock 29782082Seschrock /* 2979789Sahrens * If the parent mirror/replacing vdev only has one child, 2980789Sahrens * the parent is no longer needed. Remove it from the tree. 2981789Sahrens */ 2982789Sahrens if (pvd->vdev_children == 1) 2983789Sahrens vdev_remove_parent(cvd); 2984789Sahrens 2985789Sahrens /* 2986789Sahrens * We don't set tvd until now because the parent we just removed 2987789Sahrens * may have been the previous top-level vdev. 2988789Sahrens */ 2989789Sahrens tvd = cvd->vdev_top; 2990789Sahrens ASSERT(tvd->vdev_parent == rvd); 2991789Sahrens 2992789Sahrens /* 29933377Seschrock * Reevaluate the parent vdev state. 2994789Sahrens */ 29954451Seschrock vdev_propagate_state(cvd); 2996789Sahrens 2997789Sahrens /* 29983377Seschrock * If the device we just detached was smaller than the others, it may be 29993377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 30003377Seschrock * can't fail because the existing metaslabs are already in core, so 30013377Seschrock * there's nothing to read from disk. 3002789Sahrens */ 30031732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3004789Sahrens 3005789Sahrens vdev_config_dirty(tvd); 3006789Sahrens 3007789Sahrens /* 30083377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 30093377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 30103377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 30113377Seschrock * prevent vd from being accessed after it's freed. 3012789Sahrens */ 3013789Sahrens for (t = 0; t < TXG_SIZE; t++) 3014789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 30151732Sbonwick vd->vdev_detached = B_TRUE; 30161732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3017789Sahrens 30184451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 30194451Seschrock 30202082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 30212082Seschrock 30222082Seschrock /* 30233377Seschrock * If this was the removal of the original device in a hot spare vdev, 30243377Seschrock * then we want to go through and remove the device from the hot spare 30253377Seschrock * list of every other pool. 30262082Seschrock */ 30272082Seschrock if (unspare) { 30282082Seschrock spa = NULL; 30292082Seschrock mutex_enter(&spa_namespace_lock); 30302082Seschrock while ((spa = spa_next(spa)) != NULL) { 30312082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 30322082Seschrock continue; 30332082Seschrock 30342082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 30352082Seschrock } 30362082Seschrock mutex_exit(&spa_namespace_lock); 30372082Seschrock } 30382082Seschrock 30392082Seschrock return (error); 30402082Seschrock } 30412082Seschrock 30422082Seschrock /* 30435450Sbrendan * Remove a spares vdev from the nvlist config. 30442082Seschrock */ 30455450Sbrendan static int 30465450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare, 30475450Sbrendan nvlist_t **spares, int nspares, vdev_t *vd) 30482082Seschrock { 30495450Sbrendan nvlist_t *nv, **newspares; 30505450Sbrendan int i, j; 30512082Seschrock 30522082Seschrock nv = NULL; 30535450Sbrendan for (i = 0; i < nspares; i++) { 30545450Sbrendan uint64_t theguid; 30555450Sbrendan 30565450Sbrendan VERIFY(nvlist_lookup_uint64(spares[i], 30575450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 30585450Sbrendan if (theguid == guid) { 30595450Sbrendan nv = spares[i]; 30605450Sbrendan break; 30612082Seschrock } 30622082Seschrock } 30632082Seschrock 30642082Seschrock /* 30655450Sbrendan * Only remove the hot spare if it's not currently in use in this pool. 30662082Seschrock */ 30675450Sbrendan if (nv == NULL && vd == NULL) 30685450Sbrendan return (ENOENT); 30695450Sbrendan 30705450Sbrendan if (nv == NULL && vd != NULL) 30715450Sbrendan return (ENOTSUP); 30725450Sbrendan 30735450Sbrendan if (!unspare && nv != NULL && vd != NULL) 30745450Sbrendan return (EBUSY); 30752082Seschrock 30762082Seschrock if (nspares == 1) { 30772082Seschrock newspares = NULL; 30782082Seschrock } else { 30792082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 30802082Seschrock KM_SLEEP); 30812082Seschrock for (i = 0, j = 0; i < nspares; i++) { 30822082Seschrock if (spares[i] != nv) 30832082Seschrock VERIFY(nvlist_dup(spares[i], 30842082Seschrock &newspares[j++], KM_SLEEP) == 0); 30852082Seschrock } 30862082Seschrock } 30872082Seschrock 30885450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES, 30892082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 30905450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 30915450Sbrendan ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0); 30922082Seschrock for (i = 0; i < nspares - 1; i++) 30932082Seschrock nvlist_free(newspares[i]); 30942082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 30955450Sbrendan 30965450Sbrendan return (0); 30975450Sbrendan } 30985450Sbrendan 30995450Sbrendan /* 31005450Sbrendan * Remove an l2cache vdev from the nvlist config. 31015450Sbrendan */ 31025450Sbrendan static int 31035450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache, 31045450Sbrendan int nl2cache, vdev_t *vd) 31055450Sbrendan { 31065450Sbrendan nvlist_t *nv, **newl2cache; 31075450Sbrendan int i, j; 31085450Sbrendan 31095450Sbrendan nv = NULL; 31105450Sbrendan for (i = 0; i < nl2cache; i++) { 31115450Sbrendan uint64_t theguid; 31125450Sbrendan 31135450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 31145450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 31155450Sbrendan if (theguid == guid) { 31165450Sbrendan nv = l2cache[i]; 31175450Sbrendan break; 31185450Sbrendan } 31195450Sbrendan } 31205450Sbrendan 31215450Sbrendan if (vd == NULL) { 31225450Sbrendan for (i = 0; i < nl2cache; i++) { 31235450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) { 31245450Sbrendan vd = sav->sav_vdevs[i]; 31255450Sbrendan break; 31265450Sbrendan } 31275450Sbrendan } 31285450Sbrendan } 31295450Sbrendan 31305450Sbrendan if (nv == NULL && vd == NULL) 31315450Sbrendan return (ENOENT); 31325450Sbrendan 31335450Sbrendan if (nv == NULL && vd != NULL) 31345450Sbrendan return (ENOTSUP); 31355450Sbrendan 31365450Sbrendan if (nl2cache == 1) { 31375450Sbrendan newl2cache = NULL; 31385450Sbrendan } else { 31395450Sbrendan newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *), 31405450Sbrendan KM_SLEEP); 31415450Sbrendan for (i = 0, j = 0; i < nl2cache; i++) { 31425450Sbrendan if (l2cache[i] != nv) 31435450Sbrendan VERIFY(nvlist_dup(l2cache[i], 31445450Sbrendan &newl2cache[j++], KM_SLEEP) == 0); 31455450Sbrendan } 31465450Sbrendan } 31475450Sbrendan 31485450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 31495450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 31505450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 31515450Sbrendan ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0); 31525450Sbrendan for (i = 0; i < nl2cache - 1; i++) 31535450Sbrendan nvlist_free(newl2cache[i]); 31545450Sbrendan kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *)); 31555450Sbrendan 31565450Sbrendan return (0); 31575450Sbrendan } 31585450Sbrendan 31595450Sbrendan /* 31605450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 31615450Sbrendan * spares and level 2 ARC devices. 31625450Sbrendan */ 31635450Sbrendan int 31645450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 31655450Sbrendan { 31665450Sbrendan vdev_t *vd; 31675450Sbrendan nvlist_t **spares, **l2cache; 31685450Sbrendan uint_t nspares, nl2cache; 31695450Sbrendan int error = 0; 31705450Sbrendan 31715450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 31725450Sbrendan 31736643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 31745450Sbrendan 31755450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 31765450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 31775450Sbrendan ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 31785450Sbrendan if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare, 31795450Sbrendan spares, nspares, vd)) != 0) 31805450Sbrendan goto out; 31815450Sbrendan spa_load_spares(spa); 31825450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 31835450Sbrendan goto out; 31845450Sbrendan } 31855450Sbrendan 31865450Sbrendan if (spa->spa_l2cache.sav_vdevs != NULL && 31875450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 31885450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) { 31895450Sbrendan if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid, 31905450Sbrendan l2cache, nl2cache, vd)) != 0) 31915450Sbrendan goto out; 31925450Sbrendan spa_load_l2cache(spa); 31935450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 31945450Sbrendan } 31952082Seschrock 31962082Seschrock out: 31972082Seschrock spa_config_exit(spa, FTAG); 31985450Sbrendan return (error); 3199789Sahrens } 3200789Sahrens 3201789Sahrens /* 32024451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 32034451Seschrock * current spared, so we can detach it. 3204789Sahrens */ 32051544Seschrock static vdev_t * 32064451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3207789Sahrens { 32081544Seschrock vdev_t *newvd, *oldvd; 3209789Sahrens int c; 3210789Sahrens 32111544Seschrock for (c = 0; c < vd->vdev_children; c++) { 32124451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 32131544Seschrock if (oldvd != NULL) 32141544Seschrock return (oldvd); 32151544Seschrock } 3216789Sahrens 32174451Seschrock /* 32184451Seschrock * Check for a completed replacement. 32194451Seschrock */ 3220789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 32211544Seschrock oldvd = vd->vdev_child[0]; 32221544Seschrock newvd = vd->vdev_child[1]; 3223789Sahrens 32241544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 32251544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 32261544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 32271544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32281544Seschrock return (oldvd); 32291544Seschrock } 32301544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32311544Seschrock } 3232789Sahrens 32334451Seschrock /* 32344451Seschrock * Check for a completed resilver with the 'unspare' flag set. 32354451Seschrock */ 32364451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 32374451Seschrock newvd = vd->vdev_child[0]; 32384451Seschrock oldvd = vd->vdev_child[1]; 32394451Seschrock 32404451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 32414451Seschrock if (newvd->vdev_unspare && 32424451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 32434451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 32444451Seschrock newvd->vdev_unspare = 0; 32454451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32464451Seschrock return (oldvd); 32474451Seschrock } 32484451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32494451Seschrock } 32504451Seschrock 32511544Seschrock return (NULL); 3252789Sahrens } 3253789Sahrens 32541544Seschrock static void 32554451Seschrock spa_vdev_resilver_done(spa_t *spa) 3256789Sahrens { 32571544Seschrock vdev_t *vd; 32582082Seschrock vdev_t *pvd; 32591544Seschrock uint64_t guid; 32602082Seschrock uint64_t pguid = 0; 3261789Sahrens 32621544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3263789Sahrens 32644451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 32651544Seschrock guid = vd->vdev_guid; 32662082Seschrock /* 32672082Seschrock * If we have just finished replacing a hot spared device, then 32682082Seschrock * we need to detach the parent's first child (the original hot 32692082Seschrock * spare) as well. 32702082Seschrock */ 32712082Seschrock pvd = vd->vdev_parent; 32722082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 32732082Seschrock pvd->vdev_id == 0) { 32742082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 32752082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 32762082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 32772082Seschrock } 32781544Seschrock spa_config_exit(spa, FTAG); 32791544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 32801544Seschrock return; 32812082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 32822082Seschrock return; 32831544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3284789Sahrens } 3285789Sahrens 32861544Seschrock spa_config_exit(spa, FTAG); 3287789Sahrens } 3288789Sahrens 3289789Sahrens /* 32901354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 32911354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 32921354Seschrock */ 32931354Seschrock int 32941354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 32951354Seschrock { 32966643Seschrock vdev_t *vd; 32971354Seschrock uint64_t txg; 32981354Seschrock 32991354Seschrock txg = spa_vdev_enter(spa); 33001354Seschrock 33016643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 33022082Seschrock /* 33036643Seschrock * Determine if this is a reference to a hot spare device. If 33046643Seschrock * it is, update the path manually as there is no associated 33056643Seschrock * vdev_t that can be synced to disk. 33062082Seschrock */ 33076643Seschrock nvlist_t **spares; 33086643Seschrock uint_t i, nspares; 33095450Sbrendan 33105450Sbrendan if (spa->spa_spares.sav_config != NULL) { 33115450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 33125450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 33135450Sbrendan &spares, &nspares) == 0); 33142082Seschrock for (i = 0; i < nspares; i++) { 33152082Seschrock uint64_t theguid; 33162082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 33172082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 33185450Sbrendan if (theguid == guid) { 33195450Sbrendan VERIFY(nvlist_add_string(spares[i], 33205450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 33215450Sbrendan spa_load_spares(spa); 33225450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 33235450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 33245450Sbrendan 0)); 33255450Sbrendan } 33262082Seschrock } 33272082Seschrock } 33285450Sbrendan 33295450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 33302082Seschrock } 33311354Seschrock 33321585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 33331585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 33341585Sbonwick 33351354Seschrock spa_strfree(vd->vdev_path); 33361354Seschrock vd->vdev_path = spa_strdup(newpath); 33371354Seschrock 33381354Seschrock vdev_config_dirty(vd->vdev_top); 33391354Seschrock 33401354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 33411354Seschrock } 33421354Seschrock 33431354Seschrock /* 3344789Sahrens * ========================================================================== 3345789Sahrens * SPA Scrubbing 3346789Sahrens * ========================================================================== 3347789Sahrens */ 3348789Sahrens 3349*7046Sahrens int 3350*7046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type) 3351789Sahrens { 33524808Sek110237 ASSERT(!spa_config_held(spa, RW_WRITER)); 33534808Sek110237 3354789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3355789Sahrens return (ENOTSUP); 3356789Sahrens 3357789Sahrens /* 3358*7046Sahrens * If a resilver was requested, but there is no DTL on a 3359*7046Sahrens * writeable leaf device, we have nothing to do. 3360789Sahrens */ 3361*7046Sahrens if (type == POOL_SCRUB_RESILVER && 3362*7046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 3363*7046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 33641544Seschrock return (0); 33651544Seschrock } 3366789Sahrens 3367*7046Sahrens if (type == POOL_SCRUB_EVERYTHING && 3368*7046Sahrens spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 3369*7046Sahrens spa->spa_dsl_pool->dp_scrub_isresilver) 3370*7046Sahrens return (EBUSY); 3371*7046Sahrens 3372*7046Sahrens if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 3373*7046Sahrens return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 3374*7046Sahrens } else if (type == POOL_SCRUB_NONE) { 3375*7046Sahrens return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 33761544Seschrock } else { 3377*7046Sahrens return (EINVAL); 33781544Seschrock } 3379789Sahrens } 3380789Sahrens 33811544Seschrock /* 33821544Seschrock * ========================================================================== 33831544Seschrock * SPA async task processing 33841544Seschrock * ========================================================================== 33851544Seschrock */ 33861544Seschrock 33871544Seschrock static void 33884451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3389789Sahrens { 33901544Seschrock vdev_t *tvd; 33911544Seschrock int c; 33921544Seschrock 33934451Seschrock for (c = 0; c < vd->vdev_children; c++) { 33944451Seschrock tvd = vd->vdev_child[c]; 33954451Seschrock if (tvd->vdev_remove_wanted) { 33964451Seschrock tvd->vdev_remove_wanted = 0; 33974451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 33984451Seschrock VDEV_AUX_NONE); 33995329Sgw25295 vdev_clear(spa, tvd, B_TRUE); 34004451Seschrock vdev_config_dirty(tvd->vdev_top); 34011544Seschrock } 34024451Seschrock spa_async_remove(spa, tvd); 34031544Seschrock } 34041544Seschrock } 34051544Seschrock 34061544Seschrock static void 34071544Seschrock spa_async_thread(spa_t *spa) 34081544Seschrock { 34091544Seschrock int tasks; 34104451Seschrock uint64_t txg; 34111544Seschrock 34121544Seschrock ASSERT(spa->spa_sync_on); 3413789Sahrens 34141544Seschrock mutex_enter(&spa->spa_async_lock); 34151544Seschrock tasks = spa->spa_async_tasks; 34161544Seschrock spa->spa_async_tasks = 0; 34171544Seschrock mutex_exit(&spa->spa_async_lock); 34181544Seschrock 34191544Seschrock /* 34201635Sbonwick * See if the config needs to be updated. 34211635Sbonwick */ 34221635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 34231635Sbonwick mutex_enter(&spa_namespace_lock); 34241635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 34251635Sbonwick mutex_exit(&spa_namespace_lock); 34261635Sbonwick } 34271635Sbonwick 34281635Sbonwick /* 34294451Seschrock * See if any devices need to be marked REMOVED. 34305329Sgw25295 * 34315329Sgw25295 * XXX - We avoid doing this when we are in 34325329Sgw25295 * I/O failure state since spa_vdev_enter() grabs 34335329Sgw25295 * the namespace lock and would not be able to obtain 34345329Sgw25295 * the writer config lock. 34351544Seschrock */ 34365329Sgw25295 if (tasks & SPA_ASYNC_REMOVE && 34375329Sgw25295 spa_state(spa) != POOL_STATE_IO_FAILURE) { 34384451Seschrock txg = spa_vdev_enter(spa); 34394451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 34404451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 34414451Seschrock } 34421544Seschrock 34431544Seschrock /* 34441544Seschrock * If any devices are done replacing, detach them. 34451544Seschrock */ 34464451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 34474451Seschrock spa_vdev_resilver_done(spa); 3448789Sahrens 34491544Seschrock /* 34501544Seschrock * Kick off a resilver. 34511544Seschrock */ 3452*7046Sahrens if (tasks & SPA_ASYNC_RESILVER) 3453*7046Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 34541544Seschrock 34551544Seschrock /* 34561544Seschrock * Let the world know that we're done. 34571544Seschrock */ 34581544Seschrock mutex_enter(&spa->spa_async_lock); 34591544Seschrock spa->spa_async_thread = NULL; 34601544Seschrock cv_broadcast(&spa->spa_async_cv); 34611544Seschrock mutex_exit(&spa->spa_async_lock); 34621544Seschrock thread_exit(); 34631544Seschrock } 34641544Seschrock 34651544Seschrock void 34661544Seschrock spa_async_suspend(spa_t *spa) 34671544Seschrock { 34681544Seschrock mutex_enter(&spa->spa_async_lock); 34691544Seschrock spa->spa_async_suspended++; 34701544Seschrock while (spa->spa_async_thread != NULL) 34711544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 34721544Seschrock mutex_exit(&spa->spa_async_lock); 34731544Seschrock } 34741544Seschrock 34751544Seschrock void 34761544Seschrock spa_async_resume(spa_t *spa) 34771544Seschrock { 34781544Seschrock mutex_enter(&spa->spa_async_lock); 34791544Seschrock ASSERT(spa->spa_async_suspended != 0); 34801544Seschrock spa->spa_async_suspended--; 34811544Seschrock mutex_exit(&spa->spa_async_lock); 34821544Seschrock } 34831544Seschrock 34841544Seschrock static void 34851544Seschrock spa_async_dispatch(spa_t *spa) 34861544Seschrock { 34871544Seschrock mutex_enter(&spa->spa_async_lock); 34881544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 34891635Sbonwick spa->spa_async_thread == NULL && 34901635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 34911544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 34921544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 34931544Seschrock mutex_exit(&spa->spa_async_lock); 34941544Seschrock } 34951544Seschrock 34961544Seschrock void 34971544Seschrock spa_async_request(spa_t *spa, int task) 34981544Seschrock { 34991544Seschrock mutex_enter(&spa->spa_async_lock); 35001544Seschrock spa->spa_async_tasks |= task; 35011544Seschrock mutex_exit(&spa->spa_async_lock); 3502789Sahrens } 3503789Sahrens 3504789Sahrens /* 3505789Sahrens * ========================================================================== 3506789Sahrens * SPA syncing routines 3507789Sahrens * ========================================================================== 3508789Sahrens */ 3509789Sahrens 3510789Sahrens static void 3511789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3512789Sahrens { 3513789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3514789Sahrens dmu_tx_t *tx; 3515789Sahrens blkptr_t blk; 3516789Sahrens uint64_t itor = 0; 3517789Sahrens zio_t *zio; 3518789Sahrens int error; 3519789Sahrens uint8_t c = 1; 3520789Sahrens 3521789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3522789Sahrens 3523789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3524789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3525789Sahrens 3526789Sahrens error = zio_wait(zio); 3527789Sahrens ASSERT3U(error, ==, 0); 3528789Sahrens 3529789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3530789Sahrens bplist_vacate(bpl, tx); 3531789Sahrens 3532789Sahrens /* 3533789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3534789Sahrens * (Usually only the first block is needed.) 3535789Sahrens */ 3536789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3537789Sahrens dmu_tx_commit(tx); 3538789Sahrens } 3539789Sahrens 3540789Sahrens static void 35412082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 35422082Seschrock { 35432082Seschrock char *packed = NULL; 35442082Seschrock size_t nvsize = 0; 35452082Seschrock dmu_buf_t *db; 35462082Seschrock 35472082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 35482082Seschrock 35492082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 35502082Seschrock 35512082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 35522082Seschrock KM_SLEEP) == 0); 35532082Seschrock 35542082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 35552082Seschrock 35562082Seschrock kmem_free(packed, nvsize); 35572082Seschrock 35582082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 35592082Seschrock dmu_buf_will_dirty(db, tx); 35602082Seschrock *(uint64_t *)db->db_data = nvsize; 35612082Seschrock dmu_buf_rele(db, FTAG); 35622082Seschrock } 35632082Seschrock 35642082Seschrock static void 35655450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 35665450Sbrendan const char *config, const char *entry) 35672082Seschrock { 35682082Seschrock nvlist_t *nvroot; 35695450Sbrendan nvlist_t **list; 35702082Seschrock int i; 35712082Seschrock 35725450Sbrendan if (!sav->sav_sync) 35732082Seschrock return; 35742082Seschrock 35752082Seschrock /* 35765450Sbrendan * Update the MOS nvlist describing the list of available devices. 35775450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 35784451Seschrock * valid and the vdevs are labeled appropriately. 35792082Seschrock */ 35805450Sbrendan if (sav->sav_object == 0) { 35815450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 35825450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 35835450Sbrendan sizeof (uint64_t), tx); 35842082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 35855450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 35865450Sbrendan &sav->sav_object, tx) == 0); 35872082Seschrock } 35882082Seschrock 35892082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 35905450Sbrendan if (sav->sav_count == 0) { 35915450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 35922082Seschrock } else { 35935450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 35945450Sbrendan for (i = 0; i < sav->sav_count; i++) 35955450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 35965450Sbrendan B_FALSE, B_FALSE, B_TRUE); 35975450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 35985450Sbrendan sav->sav_count) == 0); 35995450Sbrendan for (i = 0; i < sav->sav_count; i++) 36005450Sbrendan nvlist_free(list[i]); 36015450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 36022082Seschrock } 36032082Seschrock 36045450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 36052926Sek110237 nvlist_free(nvroot); 36062082Seschrock 36075450Sbrendan sav->sav_sync = B_FALSE; 36082082Seschrock } 36092082Seschrock 36102082Seschrock static void 3611789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3612789Sahrens { 3613789Sahrens nvlist_t *config; 3614789Sahrens 3615789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 3616789Sahrens return; 3617789Sahrens 3618789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 3619789Sahrens 36201635Sbonwick if (spa->spa_config_syncing) 36211635Sbonwick nvlist_free(spa->spa_config_syncing); 36221635Sbonwick spa->spa_config_syncing = config; 3623789Sahrens 36242082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 3625789Sahrens } 3626789Sahrens 36275094Slling /* 36285094Slling * Set zpool properties. 36295094Slling */ 36303912Slling static void 36314543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 36323912Slling { 36333912Slling spa_t *spa = arg1; 36345094Slling objset_t *mos = spa->spa_meta_objset; 36353912Slling nvlist_t *nvp = arg2; 36365094Slling nvpair_t *elem; 36374451Seschrock uint64_t intval; 36386643Seschrock char *strval; 36395094Slling zpool_prop_t prop; 36405094Slling const char *propname; 36415094Slling zprop_type_t proptype; 36426643Seschrock spa_config_dirent_t *dp; 36435094Slling 36445094Slling elem = NULL; 36455094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 36465094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 36475094Slling case ZPOOL_PROP_VERSION: 36485094Slling /* 36495094Slling * Only set version for non-zpool-creation cases 36505094Slling * (set/import). spa_create() needs special care 36515094Slling * for version setting. 36525094Slling */ 36535094Slling if (tx->tx_txg != TXG_INITIAL) { 36545094Slling VERIFY(nvpair_value_uint64(elem, 36555094Slling &intval) == 0); 36565094Slling ASSERT(intval <= SPA_VERSION); 36575094Slling ASSERT(intval >= spa_version(spa)); 36585094Slling spa->spa_uberblock.ub_version = intval; 36595094Slling vdev_config_dirty(spa->spa_root_vdev); 36605094Slling } 36615094Slling break; 36625094Slling 36635094Slling case ZPOOL_PROP_ALTROOT: 36645094Slling /* 36655094Slling * 'altroot' is a non-persistent property. It should 36665094Slling * have been set temporarily at creation or import time. 36675094Slling */ 36685094Slling ASSERT(spa->spa_root != NULL); 36695094Slling break; 36705094Slling 36715363Seschrock case ZPOOL_PROP_CACHEFILE: 36725094Slling /* 36735363Seschrock * 'cachefile' is a non-persistent property, but note 36745363Seschrock * an async request that the config cache needs to be 36755363Seschrock * udpated. 36765094Slling */ 36775363Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 36786643Seschrock 36796643Seschrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 36806643Seschrock KM_SLEEP); 36816643Seschrock 36826643Seschrock if (strval[0] == '\0') 36836643Seschrock dp->scd_path = spa_strdup(spa_config_path); 36846643Seschrock else if (strcmp(strval, "none") == 0) 36856643Seschrock dp->scd_path = NULL; 36866643Seschrock else 36876643Seschrock dp->scd_path = spa_strdup(strval); 36886643Seschrock 36896643Seschrock list_insert_head(&spa->spa_config_list, dp); 36905363Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 36914543Smarks break; 36925094Slling default: 36935094Slling /* 36945094Slling * Set pool property values in the poolprops mos object. 36955094Slling */ 36965094Slling mutex_enter(&spa->spa_props_lock); 36975094Slling if (spa->spa_pool_props_object == 0) { 36985094Slling objset_t *mos = spa->spa_meta_objset; 36995094Slling 37005094Slling VERIFY((spa->spa_pool_props_object = 37015094Slling zap_create(mos, DMU_OT_POOL_PROPS, 37025094Slling DMU_OT_NONE, 0, tx)) > 0); 37035094Slling 37045094Slling VERIFY(zap_update(mos, 37055094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 37065094Slling 8, 1, &spa->spa_pool_props_object, tx) 37075094Slling == 0); 37085094Slling } 37095094Slling mutex_exit(&spa->spa_props_lock); 37105094Slling 37115094Slling /* normalize the property name */ 37125094Slling propname = zpool_prop_to_name(prop); 37135094Slling proptype = zpool_prop_get_type(prop); 37145094Slling 37155094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 37165094Slling ASSERT(proptype == PROP_TYPE_STRING); 37175094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 37185094Slling VERIFY(zap_update(mos, 37195094Slling spa->spa_pool_props_object, propname, 37205094Slling 1, strlen(strval) + 1, strval, tx) == 0); 37215094Slling 37225094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 37235094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 37245094Slling 37255094Slling if (proptype == PROP_TYPE_INDEX) { 37265094Slling const char *unused; 37275094Slling VERIFY(zpool_prop_index_to_string( 37285094Slling prop, intval, &unused) == 0); 37295094Slling } 37305094Slling VERIFY(zap_update(mos, 37315094Slling spa->spa_pool_props_object, propname, 37325094Slling 8, 1, &intval, tx) == 0); 37335094Slling } else { 37345094Slling ASSERT(0); /* not allowed */ 37355094Slling } 37365094Slling 37375329Sgw25295 switch (prop) { 37385329Sgw25295 case ZPOOL_PROP_DELEGATION: 37395094Slling spa->spa_delegation = intval; 37405329Sgw25295 break; 37415329Sgw25295 case ZPOOL_PROP_BOOTFS: 37425094Slling spa->spa_bootfs = intval; 37435329Sgw25295 break; 37445329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 37455329Sgw25295 spa->spa_failmode = intval; 37465329Sgw25295 break; 37475329Sgw25295 default: 37485329Sgw25295 break; 37495329Sgw25295 } 37503912Slling } 37515094Slling 37525094Slling /* log internal history if this is not a zpool create */ 37535094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 37545094Slling tx->tx_txg != TXG_INITIAL) { 37555094Slling spa_history_internal_log(LOG_POOL_PROPSET, 37565094Slling spa, tx, cr, "%s %lld %s", 37575094Slling nvpair_name(elem), intval, spa->spa_name); 37585094Slling } 37593912Slling } 37603912Slling } 37613912Slling 3762789Sahrens /* 3763789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 3764789Sahrens * part of the process, so we iterate until it converges. 3765789Sahrens */ 3766789Sahrens void 3767789Sahrens spa_sync(spa_t *spa, uint64_t txg) 3768789Sahrens { 3769789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 3770789Sahrens objset_t *mos = spa->spa_meta_objset; 3771789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 37721635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3773789Sahrens vdev_t *vd; 3774789Sahrens dmu_tx_t *tx; 3775789Sahrens int dirty_vdevs; 3776789Sahrens 3777789Sahrens /* 3778789Sahrens * Lock out configuration changes. 3779789Sahrens */ 37801544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3781789Sahrens 3782789Sahrens spa->spa_syncing_txg = txg; 3783789Sahrens spa->spa_sync_pass = 0; 3784789Sahrens 37851544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3786789Sahrens 37872082Seschrock tx = dmu_tx_create_assigned(dp, txg); 37882082Seschrock 37892082Seschrock /* 37904577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 37912082Seschrock * set spa_deflate if we have no raid-z vdevs. 37922082Seschrock */ 37934577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 37944577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 37952082Seschrock int i; 37962082Seschrock 37972082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 37982082Seschrock vd = rvd->vdev_child[i]; 37992082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 38002082Seschrock break; 38012082Seschrock } 38022082Seschrock if (i == rvd->vdev_children) { 38032082Seschrock spa->spa_deflate = TRUE; 38042082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 38052082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 38062082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 38072082Seschrock } 38082082Seschrock } 38092082Seschrock 3810*7046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 3811*7046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 3812*7046Sahrens dsl_pool_create_origin(dp, tx); 3813*7046Sahrens 3814*7046Sahrens /* Keeping the origin open increases spa_minref */ 3815*7046Sahrens spa->spa_minref += 3; 3816*7046Sahrens } 3817*7046Sahrens 3818*7046Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 3819*7046Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 3820*7046Sahrens dsl_pool_upgrade_clones(dp, tx); 3821*7046Sahrens } 3822*7046Sahrens 3823789Sahrens /* 3824789Sahrens * If anything has changed in this txg, push the deferred frees 3825789Sahrens * from the previous txg. If not, leave them alone so that we 3826789Sahrens * don't generate work on an otherwise idle system. 3827789Sahrens */ 3828789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 38292329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 38302329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3831789Sahrens spa_sync_deferred_frees(spa, txg); 3832789Sahrens 3833789Sahrens /* 3834789Sahrens * Iterate to convergence. 3835789Sahrens */ 3836789Sahrens do { 3837789Sahrens spa->spa_sync_pass++; 3838789Sahrens 3839789Sahrens spa_sync_config_object(spa, tx); 38405450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 38415450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 38425450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 38435450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 38441544Seschrock spa_errlog_sync(spa, txg); 3845789Sahrens dsl_pool_sync(dp, txg); 3846789Sahrens 3847789Sahrens dirty_vdevs = 0; 3848789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3849789Sahrens vdev_sync(vd, txg); 3850789Sahrens dirty_vdevs++; 3851789Sahrens } 3852789Sahrens 3853789Sahrens bplist_sync(bpl, tx); 3854789Sahrens } while (dirty_vdevs); 3855789Sahrens 3856789Sahrens bplist_close(bpl); 3857789Sahrens 3858789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3859789Sahrens 3860789Sahrens /* 3861789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 3862789Sahrens * to commit the transaction group. 38631635Sbonwick * 38645688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 38655688Sbonwick * random top-level vdevs that are known to be visible in the 38665688Sbonwick * config cache (see spa_vdev_add() for details). If there *are* 38675688Sbonwick * dirty vdevs -- or if the sync to our random subset fails -- 38685688Sbonwick * then sync the uberblock to all vdevs. 3869789Sahrens */ 38705688Sbonwick if (list_is_empty(&spa->spa_dirty_list)) { 38716615Sgw25295 vdev_t *svd[SPA_DVAS_PER_BP]; 38726615Sgw25295 int svdcount = 0; 38731635Sbonwick int children = rvd->vdev_children; 38741635Sbonwick int c0 = spa_get_random(children); 38751635Sbonwick int c; 38761635Sbonwick 38771635Sbonwick for (c = 0; c < children; c++) { 38781635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 38795688Sbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 38801635Sbonwick continue; 38815688Sbonwick svd[svdcount++] = vd; 38825688Sbonwick if (svdcount == SPA_DVAS_PER_BP) 38831635Sbonwick break; 38841635Sbonwick } 38856615Sgw25295 vdev_config_sync(svd, svdcount, txg); 38866615Sgw25295 } else { 38876615Sgw25295 vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg); 38881635Sbonwick } 38892082Seschrock dmu_tx_commit(tx); 38902082Seschrock 38911635Sbonwick /* 38921635Sbonwick * Clear the dirty config list. 38931635Sbonwick */ 38941635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 38951635Sbonwick vdev_config_clean(vd); 38961635Sbonwick 38971635Sbonwick /* 38981635Sbonwick * Now that the new config has synced transactionally, 38991635Sbonwick * let it become visible to the config cache. 39001635Sbonwick */ 39011635Sbonwick if (spa->spa_config_syncing != NULL) { 39021635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 39031635Sbonwick spa->spa_config_txg = txg; 39041635Sbonwick spa->spa_config_syncing = NULL; 39051635Sbonwick } 3906789Sahrens 3907*7046Sahrens spa->spa_traverse_wanted = B_TRUE; 3908789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3909*7046Sahrens spa->spa_traverse_wanted = B_FALSE; 3910789Sahrens spa->spa_ubsync = spa->spa_uberblock; 3911789Sahrens rw_exit(&spa->spa_traverse_lock); 3912789Sahrens 3913789Sahrens /* 3914789Sahrens * Clean up the ZIL records for the synced txg. 3915789Sahrens */ 3916789Sahrens dsl_pool_zil_clean(dp); 3917789Sahrens 3918789Sahrens /* 3919789Sahrens * Update usable space statistics. 3920789Sahrens */ 3921789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3922789Sahrens vdev_sync_done(vd, txg); 3923789Sahrens 3924789Sahrens /* 3925789Sahrens * It had better be the case that we didn't dirty anything 39262082Seschrock * since vdev_config_sync(). 3927789Sahrens */ 3928789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3929789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3930789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3931789Sahrens ASSERT(bpl->bpl_queue == NULL); 3932789Sahrens 39331544Seschrock spa_config_exit(spa, FTAG); 39341544Seschrock 39351544Seschrock /* 39361544Seschrock * If any async tasks have been requested, kick them off. 39371544Seschrock */ 39381544Seschrock spa_async_dispatch(spa); 3939789Sahrens } 3940789Sahrens 3941789Sahrens /* 3942789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 3943789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 3944789Sahrens * sync. 3945789Sahrens */ 3946789Sahrens void 3947789Sahrens spa_sync_allpools(void) 3948789Sahrens { 3949789Sahrens spa_t *spa = NULL; 3950789Sahrens mutex_enter(&spa_namespace_lock); 3951789Sahrens while ((spa = spa_next(spa)) != NULL) { 3952789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 3953789Sahrens continue; 3954789Sahrens spa_open_ref(spa, FTAG); 3955789Sahrens mutex_exit(&spa_namespace_lock); 3956789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 3957789Sahrens mutex_enter(&spa_namespace_lock); 3958789Sahrens spa_close(spa, FTAG); 3959789Sahrens } 3960789Sahrens mutex_exit(&spa_namespace_lock); 3961789Sahrens } 3962789Sahrens 3963789Sahrens /* 3964789Sahrens * ========================================================================== 3965789Sahrens * Miscellaneous routines 3966789Sahrens * ========================================================================== 3967789Sahrens */ 3968789Sahrens 3969789Sahrens /* 3970789Sahrens * Remove all pools in the system. 3971789Sahrens */ 3972789Sahrens void 3973789Sahrens spa_evict_all(void) 3974789Sahrens { 3975789Sahrens spa_t *spa; 3976789Sahrens 3977789Sahrens /* 3978789Sahrens * Remove all cached state. All pools should be closed now, 3979789Sahrens * so every spa in the AVL tree should be unreferenced. 3980789Sahrens */ 3981789Sahrens mutex_enter(&spa_namespace_lock); 3982789Sahrens while ((spa = spa_next(NULL)) != NULL) { 3983789Sahrens /* 39841544Seschrock * Stop async tasks. The async thread may need to detach 39851544Seschrock * a device that's been replaced, which requires grabbing 39861544Seschrock * spa_namespace_lock, so we must drop it here. 3987789Sahrens */ 3988789Sahrens spa_open_ref(spa, FTAG); 3989789Sahrens mutex_exit(&spa_namespace_lock); 39901544Seschrock spa_async_suspend(spa); 39914808Sek110237 mutex_enter(&spa_namespace_lock); 3992789Sahrens spa_close(spa, FTAG); 3993789Sahrens 3994789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3995789Sahrens spa_unload(spa); 3996789Sahrens spa_deactivate(spa); 3997789Sahrens } 3998789Sahrens spa_remove(spa); 3999789Sahrens } 4000789Sahrens mutex_exit(&spa_namespace_lock); 4001789Sahrens } 40021544Seschrock 40031544Seschrock vdev_t * 40046643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 40051544Seschrock { 40066643Seschrock vdev_t *vd; 40076643Seschrock int i; 40086643Seschrock 40096643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 40106643Seschrock return (vd); 40116643Seschrock 40126643Seschrock if (l2cache) { 40136643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 40146643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 40156643Seschrock if (vd->vdev_guid == guid) 40166643Seschrock return (vd); 40176643Seschrock } 40186643Seschrock } 40196643Seschrock 40206643Seschrock return (NULL); 40211544Seschrock } 40221760Seschrock 40231760Seschrock void 40245094Slling spa_upgrade(spa_t *spa, uint64_t version) 40251760Seschrock { 40261760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 40271760Seschrock 40281760Seschrock /* 40291760Seschrock * This should only be called for a non-faulted pool, and since a 40301760Seschrock * future version would result in an unopenable pool, this shouldn't be 40311760Seschrock * possible. 40321760Seschrock */ 40334577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 40345094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 40355094Slling 40365094Slling spa->spa_uberblock.ub_version = version; 40371760Seschrock vdev_config_dirty(spa->spa_root_vdev); 40381760Seschrock 40391760Seschrock spa_config_exit(spa, FTAG); 40402082Seschrock 40412082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 40421760Seschrock } 40432082Seschrock 40442082Seschrock boolean_t 40452082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 40462082Seschrock { 40472082Seschrock int i; 40483377Seschrock uint64_t spareguid; 40495450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 40505450Sbrendan 40515450Sbrendan for (i = 0; i < sav->sav_count; i++) 40525450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 40532082Seschrock return (B_TRUE); 40542082Seschrock 40555450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 40565450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 40575450Sbrendan &spareguid) == 0 && spareguid == guid) 40583377Seschrock return (B_TRUE); 40593377Seschrock } 40603377Seschrock 40612082Seschrock return (B_FALSE); 40622082Seschrock } 40633912Slling 40644451Seschrock /* 40654451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 40664451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 40674451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 40684451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 40694451Seschrock * or zdb as real changes. 40704451Seschrock */ 40714451Seschrock void 40724451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 40734451Seschrock { 40744451Seschrock #ifdef _KERNEL 40754451Seschrock sysevent_t *ev; 40764451Seschrock sysevent_attr_list_t *attr = NULL; 40774451Seschrock sysevent_value_t value; 40784451Seschrock sysevent_id_t eid; 40794451Seschrock 40804451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 40814451Seschrock SE_SLEEP); 40824451Seschrock 40834451Seschrock value.value_type = SE_DATA_TYPE_STRING; 40844451Seschrock value.value.sv_string = spa_name(spa); 40854451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 40864451Seschrock goto done; 40874451Seschrock 40884451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 40894451Seschrock value.value.sv_uint64 = spa_guid(spa); 40904451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 40914451Seschrock goto done; 40924451Seschrock 40934451Seschrock if (vd) { 40944451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 40954451Seschrock value.value.sv_uint64 = vd->vdev_guid; 40964451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 40974451Seschrock SE_SLEEP) != 0) 40984451Seschrock goto done; 40994451Seschrock 41004451Seschrock if (vd->vdev_path) { 41014451Seschrock value.value_type = SE_DATA_TYPE_STRING; 41024451Seschrock value.value.sv_string = vd->vdev_path; 41034451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 41044451Seschrock &value, SE_SLEEP) != 0) 41054451Seschrock goto done; 41064451Seschrock } 41074451Seschrock } 41084451Seschrock 41095756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 41105756Seschrock goto done; 41115756Seschrock attr = NULL; 41125756Seschrock 41134451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 41144451Seschrock 41154451Seschrock done: 41164451Seschrock if (attr) 41174451Seschrock sysevent_free_attr(attr); 41184451Seschrock sysevent_free(ev); 41194451Seschrock #endif 41204451Seschrock } 4121