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); 2045094Slling if (err = dsl_dataset_open_obj(dp, 2055094Slling za.za_first_integer, NULL, DS_MODE_NONE, 2065094Slling FTAG, &ds)) { 2075094Slling rw_exit(&dp->dp_config_rwlock); 2085094Slling break; 2095094Slling } 2105094Slling 2115094Slling strval = kmem_alloc( 2125094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2135094Slling KM_SLEEP); 2145094Slling dsl_dataset_name(ds, strval); 2155094Slling dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 2165094Slling rw_exit(&dp->dp_config_rwlock); 2175094Slling } else { 2185094Slling strval = NULL; 2195094Slling intval = za.za_first_integer; 2205094Slling } 2215094Slling 2225949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2235094Slling 2245094Slling if (strval != NULL) 2255094Slling kmem_free(strval, 2265094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2275094Slling 2285094Slling break; 2295094Slling 2305094Slling case 1: 2315094Slling /* string property */ 2325094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2335094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 2345094Slling za.za_name, 1, za.za_num_integers, strval); 2355094Slling if (err) { 2365094Slling kmem_free(strval, za.za_num_integers); 2375094Slling break; 2385094Slling } 2395949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 2405094Slling kmem_free(strval, za.za_num_integers); 2415094Slling break; 2425094Slling 2435094Slling default: 2445094Slling break; 2455094Slling } 2465094Slling } 2475094Slling zap_cursor_fini(&zc); 2485094Slling mutex_exit(&spa->spa_props_lock); 2495094Slling out: 2505094Slling if (err && err != ENOENT) { 2515094Slling nvlist_free(*nvp); 2525949Slling *nvp = NULL; 2535094Slling return (err); 2545094Slling } 2555094Slling 2565094Slling return (0); 2575094Slling } 2585094Slling 2595094Slling /* 2605094Slling * Validate the given pool properties nvlist and modify the list 2615094Slling * for the property values to be set. 2625094Slling */ 2635094Slling static int 2645094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 2655094Slling { 2665094Slling nvpair_t *elem; 2675094Slling int error = 0, reset_bootfs = 0; 2685094Slling uint64_t objnum; 2695094Slling 2705094Slling elem = NULL; 2715094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2725094Slling zpool_prop_t prop; 2735094Slling char *propname, *strval; 2745094Slling uint64_t intval; 2755094Slling vdev_t *rvdev; 2765094Slling char *vdev_type; 2775094Slling objset_t *os; 2785363Seschrock char *slash; 2795094Slling 2805094Slling propname = nvpair_name(elem); 2815094Slling 2825094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 2835094Slling return (EINVAL); 2845094Slling 2855094Slling switch (prop) { 2865094Slling case ZPOOL_PROP_VERSION: 2875094Slling error = nvpair_value_uint64(elem, &intval); 2885094Slling if (!error && 2895094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 2905094Slling error = EINVAL; 2915094Slling break; 2925094Slling 2935094Slling case ZPOOL_PROP_DELEGATION: 2945094Slling case ZPOOL_PROP_AUTOREPLACE: 2955094Slling error = nvpair_value_uint64(elem, &intval); 2965094Slling if (!error && intval > 1) 2975094Slling error = EINVAL; 2985094Slling break; 2995094Slling 3005094Slling case ZPOOL_PROP_BOOTFS: 3015094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3025094Slling error = ENOTSUP; 3035094Slling break; 3045094Slling } 3055094Slling 3065094Slling /* 3075094Slling * A bootable filesystem can not be on a RAIDZ pool 3085094Slling * nor a striped pool with more than 1 device. 3095094Slling */ 3105094Slling rvdev = spa->spa_root_vdev; 3115094Slling vdev_type = 3125094Slling rvdev->vdev_child[0]->vdev_ops->vdev_op_type; 3135094Slling if (rvdev->vdev_children > 1 || 3145094Slling strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 3155094Slling strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 3165094Slling error = ENOTSUP; 3175094Slling break; 3185094Slling } 3195094Slling 3205094Slling reset_bootfs = 1; 3215094Slling 3225094Slling error = nvpair_value_string(elem, &strval); 3235094Slling 3245094Slling if (!error) { 3255094Slling if (strval == NULL || strval[0] == '\0') { 3265094Slling objnum = zpool_prop_default_numeric( 3275094Slling ZPOOL_PROP_BOOTFS); 3285094Slling break; 3295094Slling } 3305094Slling 3315094Slling if (error = dmu_objset_open(strval, DMU_OST_ZFS, 3325094Slling DS_MODE_STANDARD | DS_MODE_READONLY, &os)) 3335094Slling break; 3345094Slling objnum = dmu_objset_id(os); 3355094Slling dmu_objset_close(os); 3365094Slling } 3375094Slling break; 3385329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 3395329Sgw25295 error = nvpair_value_uint64(elem, &intval); 3405329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 3415329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 3425329Sgw25295 error = EINVAL; 3435329Sgw25295 3445329Sgw25295 /* 3455329Sgw25295 * This is a special case which only occurs when 3465329Sgw25295 * the pool has completely failed. This allows 3475329Sgw25295 * the user to change the in-core failmode property 3485329Sgw25295 * without syncing it out to disk (I/Os might 3495329Sgw25295 * currently be blocked). We do this by returning 3505329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 3515329Sgw25295 * into thinking we encountered a property validation 3525329Sgw25295 * error. 3535329Sgw25295 */ 3545329Sgw25295 if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) { 3555329Sgw25295 spa->spa_failmode = intval; 3565329Sgw25295 error = EIO; 3575329Sgw25295 } 3585329Sgw25295 break; 3595363Seschrock 3605363Seschrock case ZPOOL_PROP_CACHEFILE: 3615363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 3625363Seschrock break; 3635363Seschrock 3645363Seschrock if (strval[0] == '\0') 3655363Seschrock break; 3665363Seschrock 3675363Seschrock if (strcmp(strval, "none") == 0) 3685363Seschrock break; 3695363Seschrock 3705363Seschrock if (strval[0] != '/') { 3715363Seschrock error = EINVAL; 3725363Seschrock break; 3735363Seschrock } 3745363Seschrock 3755363Seschrock slash = strrchr(strval, '/'); 3765363Seschrock ASSERT(slash != NULL); 3775363Seschrock 3785363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 3795363Seschrock strcmp(slash, "/..") == 0) 3805363Seschrock error = EINVAL; 3815363Seschrock break; 3825094Slling } 3835094Slling 3845094Slling if (error) 3855094Slling break; 3865094Slling } 3875094Slling 3885094Slling if (!error && reset_bootfs) { 3895094Slling error = nvlist_remove(props, 3905094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 3915094Slling 3925094Slling if (!error) { 3935094Slling error = nvlist_add_uint64(props, 3945094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 3955094Slling } 3965094Slling } 3975094Slling 3985094Slling return (error); 3995094Slling } 4005094Slling 4015094Slling int 4025094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 4035094Slling { 4045094Slling int error; 4055094Slling 4065094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 4075094Slling return (error); 4085094Slling 4095094Slling return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 4105094Slling spa, nvp, 3)); 4115094Slling } 4125094Slling 4135094Slling /* 4145094Slling * If the bootfs property value is dsobj, clear it. 4155094Slling */ 4165094Slling void 4175094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 4185094Slling { 4195094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 4205094Slling VERIFY(zap_remove(spa->spa_meta_objset, 4215094Slling spa->spa_pool_props_object, 4225094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 4235094Slling spa->spa_bootfs = 0; 4245094Slling } 4255094Slling } 4265094Slling 427789Sahrens /* 428789Sahrens * ========================================================================== 429789Sahrens * SPA state manipulation (open/create/destroy/import/export) 430789Sahrens * ========================================================================== 431789Sahrens */ 432789Sahrens 4331544Seschrock static int 4341544Seschrock spa_error_entry_compare(const void *a, const void *b) 4351544Seschrock { 4361544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 4371544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 4381544Seschrock int ret; 4391544Seschrock 4401544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 4411544Seschrock sizeof (zbookmark_t)); 4421544Seschrock 4431544Seschrock if (ret < 0) 4441544Seschrock return (-1); 4451544Seschrock else if (ret > 0) 4461544Seschrock return (1); 4471544Seschrock else 4481544Seschrock return (0); 4491544Seschrock } 4501544Seschrock 4511544Seschrock /* 4521544Seschrock * Utility function which retrieves copies of the current logs and 4531544Seschrock * re-initializes them in the process. 4541544Seschrock */ 4551544Seschrock void 4561544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 4571544Seschrock { 4581544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 4591544Seschrock 4601544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 4611544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 4621544Seschrock 4631544Seschrock avl_create(&spa->spa_errlist_scrub, 4641544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4651544Seschrock offsetof(spa_error_entry_t, se_avl)); 4661544Seschrock avl_create(&spa->spa_errlist_last, 4671544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 4681544Seschrock offsetof(spa_error_entry_t, se_avl)); 4691544Seschrock } 4701544Seschrock 471789Sahrens /* 472789Sahrens * Activate an uninitialized pool. 473789Sahrens */ 474789Sahrens static void 475789Sahrens spa_activate(spa_t *spa) 476789Sahrens { 477789Sahrens int t; 478789Sahrens 479789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 480789Sahrens 481789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 482789Sahrens 483789Sahrens spa->spa_normal_class = metaslab_class_create(); 4844527Sperrin spa->spa_log_class = metaslab_class_create(); 485789Sahrens 486789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 487789Sahrens spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 4882986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 489789Sahrens TASKQ_PREPOPULATE); 490789Sahrens spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 4912986Sek110237 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 492789Sahrens TASKQ_PREPOPULATE); 493789Sahrens } 494789Sahrens 495789Sahrens list_create(&spa->spa_dirty_list, sizeof (vdev_t), 496789Sahrens offsetof(vdev_t, vdev_dirty_node)); 4975329Sgw25295 list_create(&spa->spa_zio_list, sizeof (zio_t), 4985329Sgw25295 offsetof(zio_t, zio_link_node)); 499789Sahrens 500789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 501789Sahrens offsetof(struct vdev, vdev_txg_node)); 5021544Seschrock 5031544Seschrock avl_create(&spa->spa_errlist_scrub, 5041544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5051544Seschrock offsetof(spa_error_entry_t, se_avl)); 5061544Seschrock avl_create(&spa->spa_errlist_last, 5071544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5081544Seschrock offsetof(spa_error_entry_t, se_avl)); 509789Sahrens } 510789Sahrens 511789Sahrens /* 512789Sahrens * Opposite of spa_activate(). 513789Sahrens */ 514789Sahrens static void 515789Sahrens spa_deactivate(spa_t *spa) 516789Sahrens { 517789Sahrens int t; 518789Sahrens 519789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 520789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 521789Sahrens ASSERT(spa->spa_root_vdev == NULL); 522789Sahrens 523789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 524789Sahrens 525789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 526789Sahrens 527789Sahrens list_destroy(&spa->spa_dirty_list); 5285329Sgw25295 list_destroy(&spa->spa_zio_list); 529789Sahrens 530789Sahrens for (t = 0; t < ZIO_TYPES; t++) { 531789Sahrens taskq_destroy(spa->spa_zio_issue_taskq[t]); 532789Sahrens taskq_destroy(spa->spa_zio_intr_taskq[t]); 533789Sahrens spa->spa_zio_issue_taskq[t] = NULL; 534789Sahrens spa->spa_zio_intr_taskq[t] = NULL; 535789Sahrens } 536789Sahrens 537789Sahrens metaslab_class_destroy(spa->spa_normal_class); 538789Sahrens spa->spa_normal_class = NULL; 539789Sahrens 5404527Sperrin metaslab_class_destroy(spa->spa_log_class); 5414527Sperrin spa->spa_log_class = NULL; 5424527Sperrin 5431544Seschrock /* 5441544Seschrock * If this was part of an import or the open otherwise failed, we may 5451544Seschrock * still have errors left in the queues. Empty them just in case. 5461544Seschrock */ 5471544Seschrock spa_errlog_drain(spa); 5481544Seschrock 5491544Seschrock avl_destroy(&spa->spa_errlist_scrub); 5501544Seschrock avl_destroy(&spa->spa_errlist_last); 5511544Seschrock 552789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 553789Sahrens } 554789Sahrens 555789Sahrens /* 556789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 557789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 558789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 559789Sahrens * All vdev validation is done by the vdev_alloc() routine. 560789Sahrens */ 5612082Seschrock static int 5622082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 5632082Seschrock uint_t id, int atype) 564789Sahrens { 565789Sahrens nvlist_t **child; 566789Sahrens uint_t c, children; 5672082Seschrock int error; 5682082Seschrock 5692082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 5702082Seschrock return (error); 5712082Seschrock 5722082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 5732082Seschrock return (0); 574789Sahrens 575789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 576789Sahrens &child, &children) != 0) { 5772082Seschrock vdev_free(*vdp); 5782082Seschrock *vdp = NULL; 5792082Seschrock return (EINVAL); 580789Sahrens } 581789Sahrens 582789Sahrens for (c = 0; c < children; c++) { 5832082Seschrock vdev_t *vd; 5842082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 5852082Seschrock atype)) != 0) { 5862082Seschrock vdev_free(*vdp); 5872082Seschrock *vdp = NULL; 5882082Seschrock return (error); 589789Sahrens } 590789Sahrens } 591789Sahrens 5922082Seschrock ASSERT(*vdp != NULL); 5932082Seschrock 5942082Seschrock return (0); 595789Sahrens } 596789Sahrens 597789Sahrens /* 598789Sahrens * Opposite of spa_load(). 599789Sahrens */ 600789Sahrens static void 601789Sahrens spa_unload(spa_t *spa) 602789Sahrens { 6032082Seschrock int i; 6042082Seschrock 605789Sahrens /* 6061544Seschrock * Stop async tasks. 6071544Seschrock */ 6081544Seschrock spa_async_suspend(spa); 6091544Seschrock 6101544Seschrock /* 611789Sahrens * Stop syncing. 612789Sahrens */ 613789Sahrens if (spa->spa_sync_on) { 614789Sahrens txg_sync_stop(spa->spa_dsl_pool); 615789Sahrens spa->spa_sync_on = B_FALSE; 616789Sahrens } 617789Sahrens 618789Sahrens /* 619789Sahrens * Wait for any outstanding prefetch I/O to complete. 620789Sahrens */ 6211544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 6221544Seschrock spa_config_exit(spa, FTAG); 623789Sahrens 624789Sahrens /* 6255450Sbrendan * Drop and purge level 2 cache 6265450Sbrendan */ 6275450Sbrendan spa_l2cache_drop(spa); 6285450Sbrendan 6295450Sbrendan /* 630789Sahrens * Close the dsl pool. 631789Sahrens */ 632789Sahrens if (spa->spa_dsl_pool) { 633789Sahrens dsl_pool_close(spa->spa_dsl_pool); 634789Sahrens spa->spa_dsl_pool = NULL; 635789Sahrens } 636789Sahrens 637789Sahrens /* 638789Sahrens * Close all vdevs. 639789Sahrens */ 6401585Sbonwick if (spa->spa_root_vdev) 641789Sahrens vdev_free(spa->spa_root_vdev); 6421585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 6431544Seschrock 6445450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 6455450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 6465450Sbrendan if (spa->spa_spares.sav_vdevs) { 6475450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 6485450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 6495450Sbrendan spa->spa_spares.sav_vdevs = NULL; 6505450Sbrendan } 6515450Sbrendan if (spa->spa_spares.sav_config) { 6525450Sbrendan nvlist_free(spa->spa_spares.sav_config); 6535450Sbrendan spa->spa_spares.sav_config = NULL; 6542082Seschrock } 6555450Sbrendan 6565450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 6575450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 6585450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 6595450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 6605450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 6615450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 6625450Sbrendan } 6635450Sbrendan if (spa->spa_l2cache.sav_config) { 6645450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 6655450Sbrendan spa->spa_l2cache.sav_config = NULL; 6662082Seschrock } 6672082Seschrock 6681544Seschrock spa->spa_async_suspended = 0; 669789Sahrens } 670789Sahrens 671789Sahrens /* 6722082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 6732082Seschrock * this pool. When this is called, we have some form of basic information in 6745450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 6755450Sbrendan * then re-generate a more complete list including status information. 6762082Seschrock */ 6772082Seschrock static void 6782082Seschrock spa_load_spares(spa_t *spa) 6792082Seschrock { 6802082Seschrock nvlist_t **spares; 6812082Seschrock uint_t nspares; 6822082Seschrock int i; 6833377Seschrock vdev_t *vd, *tvd; 6842082Seschrock 6852082Seschrock /* 6862082Seschrock * First, close and free any existing spare vdevs. 6872082Seschrock */ 6885450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 6895450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 6903377Seschrock 6913377Seschrock /* Undo the call to spa_activate() below */ 6926643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 6936643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 6943377Seschrock spa_spare_remove(tvd); 6953377Seschrock vdev_close(vd); 6963377Seschrock vdev_free(vd); 6972082Seschrock } 6983377Seschrock 6995450Sbrendan if (spa->spa_spares.sav_vdevs) 7005450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 7015450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 7025450Sbrendan 7035450Sbrendan if (spa->spa_spares.sav_config == NULL) 7042082Seschrock nspares = 0; 7052082Seschrock else 7065450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 7072082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 7082082Seschrock 7095450Sbrendan spa->spa_spares.sav_count = (int)nspares; 7105450Sbrendan spa->spa_spares.sav_vdevs = NULL; 7112082Seschrock 7122082Seschrock if (nspares == 0) 7132082Seschrock return; 7142082Seschrock 7152082Seschrock /* 7162082Seschrock * Construct the array of vdevs, opening them to get status in the 7173377Seschrock * process. For each spare, there is potentially two different vdev_t 7183377Seschrock * structures associated with it: one in the list of spares (used only 7193377Seschrock * for basic validation purposes) and one in the active vdev 7203377Seschrock * configuration (if it's spared in). During this phase we open and 7213377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 7223377Seschrock * active configuration, then we also mark this vdev as an active spare. 7232082Seschrock */ 7245450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 7255450Sbrendan KM_SLEEP); 7265450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 7272082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 7282082Seschrock VDEV_ALLOC_SPARE) == 0); 7292082Seschrock ASSERT(vd != NULL); 7302082Seschrock 7315450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 7322082Seschrock 7336643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 7346643Seschrock B_FALSE)) != NULL) { 7353377Seschrock if (!tvd->vdev_isspare) 7363377Seschrock spa_spare_add(tvd); 7373377Seschrock 7383377Seschrock /* 7393377Seschrock * We only mark the spare active if we were successfully 7403377Seschrock * able to load the vdev. Otherwise, importing a pool 7413377Seschrock * with a bad active spare would result in strange 7423377Seschrock * behavior, because multiple pool would think the spare 7433377Seschrock * is actively in use. 7443377Seschrock * 7453377Seschrock * There is a vulnerability here to an equally bizarre 7463377Seschrock * circumstance, where a dead active spare is later 7473377Seschrock * brought back to life (onlined or otherwise). Given 7483377Seschrock * the rarity of this scenario, and the extra complexity 7493377Seschrock * it adds, we ignore the possibility. 7503377Seschrock */ 7513377Seschrock if (!vdev_is_dead(tvd)) 7523377Seschrock spa_spare_activate(tvd); 7533377Seschrock } 7543377Seschrock 7552082Seschrock if (vdev_open(vd) != 0) 7562082Seschrock continue; 7572082Seschrock 7582082Seschrock vd->vdev_top = vd; 7595450Sbrendan if (vdev_validate_aux(vd) == 0) 7605450Sbrendan spa_spare_add(vd); 7612082Seschrock } 7622082Seschrock 7632082Seschrock /* 7642082Seschrock * Recompute the stashed list of spares, with status information 7652082Seschrock * this time. 7662082Seschrock */ 7675450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 7682082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 7692082Seschrock 7705450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 7715450Sbrendan KM_SLEEP); 7725450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7735450Sbrendan spares[i] = vdev_config_generate(spa, 7745450Sbrendan spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 7755450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 7765450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 7775450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 7782082Seschrock nvlist_free(spares[i]); 7795450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 7805450Sbrendan } 7815450Sbrendan 7825450Sbrendan /* 7835450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 7845450Sbrendan * this pool. When this is called, we have some form of basic information in 7855450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 7865450Sbrendan * then re-generate a more complete list including status information. 7875450Sbrendan * Devices which are already active have their details maintained, and are 7885450Sbrendan * not re-opened. 7895450Sbrendan */ 7905450Sbrendan static void 7915450Sbrendan spa_load_l2cache(spa_t *spa) 7925450Sbrendan { 7935450Sbrendan nvlist_t **l2cache; 7945450Sbrendan uint_t nl2cache; 7955450Sbrendan int i, j, oldnvdevs; 7966643Seschrock uint64_t guid, size; 7975450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 7985450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 7995450Sbrendan 8005450Sbrendan if (sav->sav_config != NULL) { 8015450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 8025450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 8035450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 8045450Sbrendan } else { 8055450Sbrendan nl2cache = 0; 8065450Sbrendan } 8075450Sbrendan 8085450Sbrendan oldvdevs = sav->sav_vdevs; 8095450Sbrendan oldnvdevs = sav->sav_count; 8105450Sbrendan sav->sav_vdevs = NULL; 8115450Sbrendan sav->sav_count = 0; 8125450Sbrendan 8135450Sbrendan /* 8145450Sbrendan * Process new nvlist of vdevs. 8155450Sbrendan */ 8165450Sbrendan for (i = 0; i < nl2cache; i++) { 8175450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 8185450Sbrendan &guid) == 0); 8195450Sbrendan 8205450Sbrendan newvdevs[i] = NULL; 8215450Sbrendan for (j = 0; j < oldnvdevs; j++) { 8225450Sbrendan vd = oldvdevs[j]; 8235450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 8245450Sbrendan /* 8255450Sbrendan * Retain previous vdev for add/remove ops. 8265450Sbrendan */ 8275450Sbrendan newvdevs[i] = vd; 8285450Sbrendan oldvdevs[j] = NULL; 8295450Sbrendan break; 8305450Sbrendan } 8315450Sbrendan } 8325450Sbrendan 8335450Sbrendan if (newvdevs[i] == NULL) { 8345450Sbrendan /* 8355450Sbrendan * Create new vdev 8365450Sbrendan */ 8375450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 8385450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 8395450Sbrendan ASSERT(vd != NULL); 8405450Sbrendan newvdevs[i] = vd; 8415450Sbrendan 8425450Sbrendan /* 8435450Sbrendan * Commit this vdev as an l2cache device, 8445450Sbrendan * even if it fails to open. 8455450Sbrendan */ 8465450Sbrendan spa_l2cache_add(vd); 8475450Sbrendan 8486643Seschrock vd->vdev_top = vd; 8496643Seschrock vd->vdev_aux = sav; 8506643Seschrock 8516643Seschrock spa_l2cache_activate(vd); 8526643Seschrock 8535450Sbrendan if (vdev_open(vd) != 0) 8545450Sbrendan continue; 8555450Sbrendan 8565450Sbrendan (void) vdev_validate_aux(vd); 8575450Sbrendan 8585450Sbrendan if (!vdev_is_dead(vd)) { 8595450Sbrendan size = vdev_get_rsize(vd); 8606643Seschrock l2arc_add_vdev(spa, vd, 8616643Seschrock VDEV_LABEL_START_SIZE, 8626643Seschrock size - VDEV_LABEL_START_SIZE); 8635450Sbrendan } 8645450Sbrendan } 8655450Sbrendan } 8665450Sbrendan 8675450Sbrendan /* 8685450Sbrendan * Purge vdevs that were dropped 8695450Sbrendan */ 8705450Sbrendan for (i = 0; i < oldnvdevs; i++) { 8715450Sbrendan uint64_t pool; 8725450Sbrendan 8735450Sbrendan vd = oldvdevs[i]; 8745450Sbrendan if (vd != NULL) { 8755450Sbrendan if (spa_mode & FWRITE && 8765450Sbrendan spa_l2cache_exists(vd->vdev_guid, &pool) && 8776643Seschrock pool != 0ULL && 8786643Seschrock l2arc_vdev_present(vd)) { 8795450Sbrendan l2arc_remove_vdev(vd); 8805450Sbrendan } 8815450Sbrendan (void) vdev_close(vd); 8825450Sbrendan spa_l2cache_remove(vd); 8835450Sbrendan } 8845450Sbrendan } 8855450Sbrendan 8865450Sbrendan if (oldvdevs) 8875450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 8885450Sbrendan 8895450Sbrendan if (sav->sav_config == NULL) 8905450Sbrendan goto out; 8915450Sbrendan 8925450Sbrendan sav->sav_vdevs = newvdevs; 8935450Sbrendan sav->sav_count = (int)nl2cache; 8945450Sbrendan 8955450Sbrendan /* 8965450Sbrendan * Recompute the stashed list of l2cache devices, with status 8975450Sbrendan * information this time. 8985450Sbrendan */ 8995450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 9005450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 9015450Sbrendan 9025450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 9035450Sbrendan for (i = 0; i < sav->sav_count; i++) 9045450Sbrendan l2cache[i] = vdev_config_generate(spa, 9055450Sbrendan sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 9065450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 9075450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 9085450Sbrendan out: 9095450Sbrendan for (i = 0; i < sav->sav_count; i++) 9105450Sbrendan nvlist_free(l2cache[i]); 9115450Sbrendan if (sav->sav_count) 9125450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 9132082Seschrock } 9142082Seschrock 9152082Seschrock static int 9162082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 9172082Seschrock { 9182082Seschrock dmu_buf_t *db; 9192082Seschrock char *packed = NULL; 9202082Seschrock size_t nvsize = 0; 9212082Seschrock int error; 9222082Seschrock *value = NULL; 9232082Seschrock 9242082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 9252082Seschrock nvsize = *(uint64_t *)db->db_data; 9262082Seschrock dmu_buf_rele(db, FTAG); 9272082Seschrock 9282082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 9292082Seschrock error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 9302082Seschrock if (error == 0) 9312082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 9322082Seschrock kmem_free(packed, nvsize); 9332082Seschrock 9342082Seschrock return (error); 9352082Seschrock } 9362082Seschrock 9372082Seschrock /* 9384451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 9394451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 9404451Seschrock */ 9414451Seschrock static void 9424451Seschrock spa_check_removed(vdev_t *vd) 9434451Seschrock { 9444451Seschrock int c; 9454451Seschrock 9464451Seschrock for (c = 0; c < vd->vdev_children; c++) 9474451Seschrock spa_check_removed(vd->vdev_child[c]); 9484451Seschrock 9494451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 9504451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 9514451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 9524451Seschrock } 9534451Seschrock } 9544451Seschrock 9554451Seschrock /* 956789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 9571544Seschrock * source of configuration information. 958789Sahrens */ 959789Sahrens static int 9601544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 961789Sahrens { 962789Sahrens int error = 0; 963789Sahrens nvlist_t *nvroot = NULL; 964789Sahrens vdev_t *rvd; 965789Sahrens uberblock_t *ub = &spa->spa_uberblock; 9661635Sbonwick uint64_t config_cache_txg = spa->spa_config_txg; 967789Sahrens uint64_t pool_guid; 9682082Seschrock uint64_t version; 969789Sahrens zio_t *zio; 9704451Seschrock uint64_t autoreplace = 0; 971789Sahrens 9721544Seschrock spa->spa_load_state = state; 9731635Sbonwick 974789Sahrens if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 9751733Sbonwick nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 9761544Seschrock error = EINVAL; 9771544Seschrock goto out; 9781544Seschrock } 979789Sahrens 9802082Seschrock /* 9812082Seschrock * Versioning wasn't explicitly added to the label until later, so if 9822082Seschrock * it's not present treat it as the initial version. 9832082Seschrock */ 9842082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 9854577Sahrens version = SPA_VERSION_INITIAL; 9862082Seschrock 9871733Sbonwick (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 9881733Sbonwick &spa->spa_config_txg); 9891733Sbonwick 9901635Sbonwick if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 9911544Seschrock spa_guid_exists(pool_guid, 0)) { 9921544Seschrock error = EEXIST; 9931544Seschrock goto out; 9941544Seschrock } 995789Sahrens 9962174Seschrock spa->spa_load_guid = pool_guid; 9972174Seschrock 998789Sahrens /* 9992082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 10002082Seschrock * value that will be returned by spa_version() since parsing the 10012082Seschrock * configuration requires knowing the version number. 1002789Sahrens */ 10031544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 10042082Seschrock spa->spa_ubsync.ub_version = version; 10052082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 10061544Seschrock spa_config_exit(spa, FTAG); 1007789Sahrens 10082082Seschrock if (error != 0) 10091544Seschrock goto out; 1010789Sahrens 10111585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 1012789Sahrens ASSERT(spa_guid(spa) == pool_guid); 1013789Sahrens 1014789Sahrens /* 1015789Sahrens * Try to open all vdevs, loading each label in the process. 1016789Sahrens */ 10174070Smc142369 error = vdev_open(rvd); 10184070Smc142369 if (error != 0) 10191544Seschrock goto out; 1020789Sahrens 1021789Sahrens /* 10221986Seschrock * Validate the labels for all leaf vdevs. We need to grab the config 10231986Seschrock * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 10241986Seschrock * flag. 10251986Seschrock */ 10261986Seschrock spa_config_enter(spa, RW_READER, FTAG); 10271986Seschrock error = vdev_validate(rvd); 10281986Seschrock spa_config_exit(spa, FTAG); 10291986Seschrock 10304070Smc142369 if (error != 0) 10311986Seschrock goto out; 10321986Seschrock 10331986Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 10341986Seschrock error = ENXIO; 10351986Seschrock goto out; 10361986Seschrock } 10371986Seschrock 10381986Seschrock /* 1039789Sahrens * Find the best uberblock. 1040789Sahrens */ 1041789Sahrens bzero(ub, sizeof (uberblock_t)); 1042789Sahrens 1043789Sahrens zio = zio_root(spa, NULL, NULL, 1044789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1045789Sahrens vdev_uberblock_load(zio, rvd, ub); 1046789Sahrens error = zio_wait(zio); 1047789Sahrens 1048789Sahrens /* 1049789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1050789Sahrens */ 1051789Sahrens if (ub->ub_txg == 0) { 10521760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10531760Seschrock VDEV_AUX_CORRUPT_DATA); 10541544Seschrock error = ENXIO; 10551544Seschrock goto out; 10561544Seschrock } 10571544Seschrock 10581544Seschrock /* 10591544Seschrock * If the pool is newer than the code, we can't open it. 10601544Seschrock */ 10614577Sahrens if (ub->ub_version > SPA_VERSION) { 10621760Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10631760Seschrock VDEV_AUX_VERSION_NEWER); 10641544Seschrock error = ENOTSUP; 10651544Seschrock goto out; 1066789Sahrens } 1067789Sahrens 1068789Sahrens /* 1069789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1070789Sahrens * incomplete configuration. 1071789Sahrens */ 10721732Sbonwick if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 10731544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10741544Seschrock VDEV_AUX_BAD_GUID_SUM); 10751544Seschrock error = ENXIO; 10761544Seschrock goto out; 1077789Sahrens } 1078789Sahrens 1079789Sahrens /* 1080789Sahrens * Initialize internal SPA structures. 1081789Sahrens */ 1082789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1083789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1084789Sahrens spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 10851544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 10861544Seschrock if (error) { 10871544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10881544Seschrock VDEV_AUX_CORRUPT_DATA); 10891544Seschrock goto out; 10901544Seschrock } 1091789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1092789Sahrens 10931544Seschrock if (zap_lookup(spa->spa_meta_objset, 1094789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 10951544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 10961544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 10971544Seschrock VDEV_AUX_CORRUPT_DATA); 10981544Seschrock error = EIO; 10991544Seschrock goto out; 11001544Seschrock } 1101789Sahrens 1102789Sahrens if (!mosconfig) { 11032082Seschrock nvlist_t *newconfig; 11043975Sek110237 uint64_t hostid; 11052082Seschrock 11062082Seschrock if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 11071544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11081544Seschrock VDEV_AUX_CORRUPT_DATA); 11091544Seschrock error = EIO; 11101544Seschrock goto out; 11111544Seschrock } 1112789Sahrens 11133975Sek110237 if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 11143975Sek110237 &hostid) == 0) { 11153975Sek110237 char *hostname; 11163975Sek110237 unsigned long myhostid = 0; 11173975Sek110237 11183975Sek110237 VERIFY(nvlist_lookup_string(newconfig, 11193975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 11203975Sek110237 11213975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 11224178Slling if (hostid != 0 && myhostid != 0 && 11234178Slling (unsigned long)hostid != myhostid) { 11243975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 11253975Sek110237 "loaded as it was last accessed by " 11263975Sek110237 "another system (host: %s hostid: 0x%lx). " 11273975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 11283975Sek110237 spa->spa_name, hostname, 11293975Sek110237 (unsigned long)hostid); 11303975Sek110237 error = EBADF; 11313975Sek110237 goto out; 11323975Sek110237 } 11333975Sek110237 } 11343975Sek110237 1135789Sahrens spa_config_set(spa, newconfig); 1136789Sahrens spa_unload(spa); 1137789Sahrens spa_deactivate(spa); 1138789Sahrens spa_activate(spa); 1139789Sahrens 11401544Seschrock return (spa_load(spa, newconfig, state, B_TRUE)); 11411544Seschrock } 11421544Seschrock 11431544Seschrock if (zap_lookup(spa->spa_meta_objset, 11441544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 11451544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 11461544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11471544Seschrock VDEV_AUX_CORRUPT_DATA); 11481544Seschrock error = EIO; 11491544Seschrock goto out; 1150789Sahrens } 1151789Sahrens 11521544Seschrock /* 11532082Seschrock * Load the bit that tells us to use the new accounting function 11542082Seschrock * (raid-z deflation). If we have an older pool, this will not 11552082Seschrock * be present. 11562082Seschrock */ 11572082Seschrock error = zap_lookup(spa->spa_meta_objset, 11582082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 11592082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate); 11602082Seschrock if (error != 0 && error != ENOENT) { 11612082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11622082Seschrock VDEV_AUX_CORRUPT_DATA); 11632082Seschrock error = EIO; 11642082Seschrock goto out; 11652082Seschrock } 11662082Seschrock 11672082Seschrock /* 11681544Seschrock * Load the persistent error log. If we have an older pool, this will 11691544Seschrock * not be present. 11701544Seschrock */ 11711544Seschrock error = zap_lookup(spa->spa_meta_objset, 11721544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 11731544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_last); 11741807Sbonwick if (error != 0 && error != ENOENT) { 11751544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11761544Seschrock VDEV_AUX_CORRUPT_DATA); 11771544Seschrock error = EIO; 11781544Seschrock goto out; 11791544Seschrock } 11801544Seschrock 11811544Seschrock error = zap_lookup(spa->spa_meta_objset, 11821544Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 11831544Seschrock sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 11841544Seschrock if (error != 0 && error != ENOENT) { 11851544Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 11861544Seschrock VDEV_AUX_CORRUPT_DATA); 11871544Seschrock error = EIO; 11881544Seschrock goto out; 11891544Seschrock } 1190789Sahrens 1191789Sahrens /* 11922926Sek110237 * Load the history object. If we have an older pool, this 11932926Sek110237 * will not be present. 11942926Sek110237 */ 11952926Sek110237 error = zap_lookup(spa->spa_meta_objset, 11962926Sek110237 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 11972926Sek110237 sizeof (uint64_t), 1, &spa->spa_history); 11982926Sek110237 if (error != 0 && error != ENOENT) { 11992926Sek110237 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12002926Sek110237 VDEV_AUX_CORRUPT_DATA); 12012926Sek110237 error = EIO; 12022926Sek110237 goto out; 12032926Sek110237 } 12042926Sek110237 12052926Sek110237 /* 12062082Seschrock * Load any hot spares for this pool. 12072082Seschrock */ 12082082Seschrock error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12095450Sbrendan DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 12102082Seschrock if (error != 0 && error != ENOENT) { 12112082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12122082Seschrock VDEV_AUX_CORRUPT_DATA); 12132082Seschrock error = EIO; 12142082Seschrock goto out; 12152082Seschrock } 12162082Seschrock if (error == 0) { 12174577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 12185450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 12195450Sbrendan &spa->spa_spares.sav_config) != 0) { 12202082Seschrock vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12212082Seschrock VDEV_AUX_CORRUPT_DATA); 12222082Seschrock error = EIO; 12232082Seschrock goto out; 12242082Seschrock } 12252082Seschrock 12262082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 12272082Seschrock spa_load_spares(spa); 12282082Seschrock spa_config_exit(spa, FTAG); 12292082Seschrock } 12302082Seschrock 12315450Sbrendan /* 12325450Sbrendan * Load any level 2 ARC devices for this pool. 12335450Sbrendan */ 12345450Sbrendan error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12355450Sbrendan DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 12365450Sbrendan &spa->spa_l2cache.sav_object); 12375450Sbrendan if (error != 0 && error != ENOENT) { 12385450Sbrendan vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12395450Sbrendan VDEV_AUX_CORRUPT_DATA); 12405450Sbrendan error = EIO; 12415450Sbrendan goto out; 12425450Sbrendan } 12435450Sbrendan if (error == 0) { 12445450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 12455450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 12465450Sbrendan &spa->spa_l2cache.sav_config) != 0) { 12475450Sbrendan vdev_set_state(rvd, B_TRUE, 12485450Sbrendan VDEV_STATE_CANT_OPEN, 12495450Sbrendan VDEV_AUX_CORRUPT_DATA); 12505450Sbrendan error = EIO; 12515450Sbrendan goto out; 12525450Sbrendan } 12535450Sbrendan 12545450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 12555450Sbrendan spa_load_l2cache(spa); 12565450Sbrendan spa_config_exit(spa, FTAG); 12575450Sbrendan } 12585450Sbrendan 12595094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 12604543Smarks 12613912Slling error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 12623912Slling DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 12633912Slling 12643912Slling if (error && error != ENOENT) { 12653912Slling vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 12663912Slling VDEV_AUX_CORRUPT_DATA); 12673912Slling error = EIO; 12683912Slling goto out; 12693912Slling } 12703912Slling 12713912Slling if (error == 0) { 12723912Slling (void) zap_lookup(spa->spa_meta_objset, 12733912Slling spa->spa_pool_props_object, 12744451Seschrock zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 12753912Slling sizeof (uint64_t), 1, &spa->spa_bootfs); 12764451Seschrock (void) zap_lookup(spa->spa_meta_objset, 12774451Seschrock spa->spa_pool_props_object, 12784451Seschrock zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 12794451Seschrock sizeof (uint64_t), 1, &autoreplace); 12804543Smarks (void) zap_lookup(spa->spa_meta_objset, 12814543Smarks spa->spa_pool_props_object, 12824543Smarks zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 12834543Smarks sizeof (uint64_t), 1, &spa->spa_delegation); 12845329Sgw25295 (void) zap_lookup(spa->spa_meta_objset, 12855329Sgw25295 spa->spa_pool_props_object, 12865329Sgw25295 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 12875329Sgw25295 sizeof (uint64_t), 1, &spa->spa_failmode); 12883912Slling } 12893912Slling 12902082Seschrock /* 12914451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 12924451Seschrock * the ZFS DE that it should not issue any faults for unopenable 12934451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 12944451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 12954451Seschrock * over. 12964451Seschrock */ 12975756Seschrock if (autoreplace && state != SPA_LOAD_TRYIMPORT) 12984451Seschrock spa_check_removed(spa->spa_root_vdev); 12994451Seschrock 13004451Seschrock /* 13011986Seschrock * Load the vdev state for all toplevel vdevs. 1302789Sahrens */ 13031986Seschrock vdev_load(rvd); 1304789Sahrens 1305789Sahrens /* 1306789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 1307789Sahrens */ 13081544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 1309789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 13101544Seschrock spa_config_exit(spa, FTAG); 1311789Sahrens 1312789Sahrens /* 1313789Sahrens * Check the state of the root vdev. If it can't be opened, it 1314789Sahrens * indicates one or more toplevel vdevs are faulted. 1315789Sahrens */ 13161544Seschrock if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 13171544Seschrock error = ENXIO; 13181544Seschrock goto out; 13191544Seschrock } 1320789Sahrens 13211544Seschrock if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 13221635Sbonwick dmu_tx_t *tx; 13231635Sbonwick int need_update = B_FALSE; 13241585Sbonwick int c; 13251601Sbonwick 13261635Sbonwick /* 13271635Sbonwick * Claim log blocks that haven't been committed yet. 13281635Sbonwick * This must all happen in a single txg. 13291635Sbonwick */ 13301601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1331789Sahrens spa_first_txg(spa)); 13322417Sahrens (void) dmu_objset_find(spa->spa_name, 13332417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 1334789Sahrens dmu_tx_commit(tx); 1335789Sahrens 1336789Sahrens spa->spa_sync_on = B_TRUE; 1337789Sahrens txg_sync_start(spa->spa_dsl_pool); 1338789Sahrens 1339789Sahrens /* 1340789Sahrens * Wait for all claims to sync. 1341789Sahrens */ 1342789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 13431585Sbonwick 13441585Sbonwick /* 13451635Sbonwick * If the config cache is stale, or we have uninitialized 13461635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 13471585Sbonwick */ 13481635Sbonwick if (config_cache_txg != spa->spa_config_txg || 13491635Sbonwick state == SPA_LOAD_IMPORT) 13501635Sbonwick need_update = B_TRUE; 13511635Sbonwick 13521635Sbonwick for (c = 0; c < rvd->vdev_children; c++) 13531635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 13541635Sbonwick need_update = B_TRUE; 13551585Sbonwick 13561585Sbonwick /* 13571635Sbonwick * Update the config cache asychronously in case we're the 13581635Sbonwick * root pool, in which case the config cache isn't writable yet. 13591585Sbonwick */ 13601635Sbonwick if (need_update) 13611635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1362789Sahrens } 1363789Sahrens 13641544Seschrock error = 0; 13651544Seschrock out: 13662082Seschrock if (error && error != EBADF) 13671544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 13681544Seschrock spa->spa_load_state = SPA_LOAD_NONE; 13691544Seschrock spa->spa_ena = 0; 13701544Seschrock 13711544Seschrock return (error); 1372789Sahrens } 1373789Sahrens 1374789Sahrens /* 1375789Sahrens * Pool Open/Import 1376789Sahrens * 1377789Sahrens * The import case is identical to an open except that the configuration is sent 1378789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 1379789Sahrens * case of an open, the pool configuration will exist in the 13804451Seschrock * POOL_STATE_UNINITIALIZED state. 1381789Sahrens * 1382789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 1383789Sahrens * the same time open the pool, without having to keep around the spa_t in some 1384789Sahrens * ambiguous state. 1385789Sahrens */ 1386789Sahrens static int 1387789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 1388789Sahrens { 1389789Sahrens spa_t *spa; 1390789Sahrens int error; 1391789Sahrens int loaded = B_FALSE; 1392789Sahrens int locked = B_FALSE; 1393789Sahrens 1394789Sahrens *spapp = NULL; 1395789Sahrens 1396789Sahrens /* 1397789Sahrens * As disgusting as this is, we need to support recursive calls to this 1398789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 1399789Sahrens * up calling spa_open() again. The real fix is to figure out how to 1400789Sahrens * avoid dsl_dir_open() calling this in the first place. 1401789Sahrens */ 1402789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 1403789Sahrens mutex_enter(&spa_namespace_lock); 1404789Sahrens locked = B_TRUE; 1405789Sahrens } 1406789Sahrens 1407789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 1408789Sahrens if (locked) 1409789Sahrens mutex_exit(&spa_namespace_lock); 1410789Sahrens return (ENOENT); 1411789Sahrens } 1412789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1413789Sahrens 1414789Sahrens spa_activate(spa); 1415789Sahrens 14161635Sbonwick error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 1417789Sahrens 1418789Sahrens if (error == EBADF) { 1419789Sahrens /* 14201986Seschrock * If vdev_validate() returns failure (indicated by 14211986Seschrock * EBADF), it indicates that one of the vdevs indicates 14221986Seschrock * that the pool has been exported or destroyed. If 14231986Seschrock * this is the case, the config cache is out of sync and 14241986Seschrock * we should remove the pool from the namespace. 1425789Sahrens */ 1426789Sahrens spa_unload(spa); 1427789Sahrens spa_deactivate(spa); 14286643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 1429789Sahrens spa_remove(spa); 1430789Sahrens if (locked) 1431789Sahrens mutex_exit(&spa_namespace_lock); 1432789Sahrens return (ENOENT); 14331544Seschrock } 14341544Seschrock 14351544Seschrock if (error) { 1436789Sahrens /* 1437789Sahrens * We can't open the pool, but we still have useful 1438789Sahrens * information: the state of each vdev after the 1439789Sahrens * attempted vdev_open(). Return this to the user. 1440789Sahrens */ 14411635Sbonwick if (config != NULL && spa->spa_root_vdev != NULL) { 14421635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 1443789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, 1444789Sahrens B_TRUE); 14451635Sbonwick spa_config_exit(spa, FTAG); 14461635Sbonwick } 1447789Sahrens spa_unload(spa); 1448789Sahrens spa_deactivate(spa); 14491544Seschrock spa->spa_last_open_failed = B_TRUE; 1450789Sahrens if (locked) 1451789Sahrens mutex_exit(&spa_namespace_lock); 1452789Sahrens *spapp = NULL; 1453789Sahrens return (error); 14541544Seschrock } else { 14551544Seschrock spa->spa_last_open_failed = B_FALSE; 1456789Sahrens } 1457789Sahrens 1458789Sahrens loaded = B_TRUE; 1459789Sahrens } 1460789Sahrens 1461789Sahrens spa_open_ref(spa, tag); 14624451Seschrock 14634451Seschrock /* 14644451Seschrock * If we just loaded the pool, resilver anything that's out of date. 14654451Seschrock */ 14664451Seschrock if (loaded && (spa_mode & FWRITE)) 14674451Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 14684451Seschrock 1469789Sahrens if (locked) 1470789Sahrens mutex_exit(&spa_namespace_lock); 1471789Sahrens 1472789Sahrens *spapp = spa; 1473789Sahrens 1474789Sahrens if (config != NULL) { 14751544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1476789Sahrens *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 14771544Seschrock spa_config_exit(spa, FTAG); 1478789Sahrens } 1479789Sahrens 1480789Sahrens return (0); 1481789Sahrens } 1482789Sahrens 1483789Sahrens int 1484789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 1485789Sahrens { 1486789Sahrens return (spa_open_common(name, spapp, tag, NULL)); 1487789Sahrens } 1488789Sahrens 14891544Seschrock /* 14901544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 14911544Seschrock * preventing it from being exported or destroyed. 14921544Seschrock */ 14931544Seschrock spa_t * 14941544Seschrock spa_inject_addref(char *name) 14951544Seschrock { 14961544Seschrock spa_t *spa; 14971544Seschrock 14981544Seschrock mutex_enter(&spa_namespace_lock); 14991544Seschrock if ((spa = spa_lookup(name)) == NULL) { 15001544Seschrock mutex_exit(&spa_namespace_lock); 15011544Seschrock return (NULL); 15021544Seschrock } 15031544Seschrock spa->spa_inject_ref++; 15041544Seschrock mutex_exit(&spa_namespace_lock); 15051544Seschrock 15061544Seschrock return (spa); 15071544Seschrock } 15081544Seschrock 15091544Seschrock void 15101544Seschrock spa_inject_delref(spa_t *spa) 15111544Seschrock { 15121544Seschrock mutex_enter(&spa_namespace_lock); 15131544Seschrock spa->spa_inject_ref--; 15141544Seschrock mutex_exit(&spa_namespace_lock); 15151544Seschrock } 15161544Seschrock 15175450Sbrendan /* 15185450Sbrendan * Add spares device information to the nvlist. 15195450Sbrendan */ 15202082Seschrock static void 15212082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 15222082Seschrock { 15232082Seschrock nvlist_t **spares; 15242082Seschrock uint_t i, nspares; 15252082Seschrock nvlist_t *nvroot; 15262082Seschrock uint64_t guid; 15272082Seschrock vdev_stat_t *vs; 15282082Seschrock uint_t vsc; 15293377Seschrock uint64_t pool; 15302082Seschrock 15315450Sbrendan if (spa->spa_spares.sav_count == 0) 15322082Seschrock return; 15332082Seschrock 15342082Seschrock VERIFY(nvlist_lookup_nvlist(config, 15352082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15365450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 15372082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15382082Seschrock if (nspares != 0) { 15392082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 15402082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 15412082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 15422082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 15432082Seschrock 15442082Seschrock /* 15452082Seschrock * Go through and find any spares which have since been 15462082Seschrock * repurposed as an active spare. If this is the case, update 15472082Seschrock * their status appropriately. 15482082Seschrock */ 15492082Seschrock for (i = 0; i < nspares; i++) { 15502082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 15512082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 15523377Seschrock if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 15532082Seschrock VERIFY(nvlist_lookup_uint64_array( 15542082Seschrock spares[i], ZPOOL_CONFIG_STATS, 15552082Seschrock (uint64_t **)&vs, &vsc) == 0); 15562082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 15572082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 15582082Seschrock } 15592082Seschrock } 15602082Seschrock } 15612082Seschrock } 15622082Seschrock 15635450Sbrendan /* 15645450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 15655450Sbrendan */ 15665450Sbrendan static void 15675450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 15685450Sbrendan { 15695450Sbrendan nvlist_t **l2cache; 15705450Sbrendan uint_t i, j, nl2cache; 15715450Sbrendan nvlist_t *nvroot; 15725450Sbrendan uint64_t guid; 15735450Sbrendan vdev_t *vd; 15745450Sbrendan vdev_stat_t *vs; 15755450Sbrendan uint_t vsc; 15765450Sbrendan 15775450Sbrendan if (spa->spa_l2cache.sav_count == 0) 15785450Sbrendan return; 15795450Sbrendan 15805450Sbrendan spa_config_enter(spa, RW_READER, FTAG); 15815450Sbrendan 15825450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 15835450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 15845450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 15855450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 15865450Sbrendan if (nl2cache != 0) { 15875450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 15885450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 15895450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 15905450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 15915450Sbrendan 15925450Sbrendan /* 15935450Sbrendan * Update level 2 cache device stats. 15945450Sbrendan */ 15955450Sbrendan 15965450Sbrendan for (i = 0; i < nl2cache; i++) { 15975450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 15985450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 15995450Sbrendan 16005450Sbrendan vd = NULL; 16015450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 16025450Sbrendan if (guid == 16035450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 16045450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 16055450Sbrendan break; 16065450Sbrendan } 16075450Sbrendan } 16085450Sbrendan ASSERT(vd != NULL); 16095450Sbrendan 16105450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 16115450Sbrendan ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 16125450Sbrendan vdev_get_stats(vd, vs); 16135450Sbrendan } 16145450Sbrendan } 16155450Sbrendan 16165450Sbrendan spa_config_exit(spa, FTAG); 16175450Sbrendan } 16185450Sbrendan 1619789Sahrens int 16201544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1621789Sahrens { 1622789Sahrens int error; 1623789Sahrens spa_t *spa; 1624789Sahrens 1625789Sahrens *config = NULL; 1626789Sahrens error = spa_open_common(name, &spa, FTAG, config); 1627789Sahrens 16282082Seschrock if (spa && *config != NULL) { 16291544Seschrock VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 16301544Seschrock spa_get_errlog_size(spa)) == 0); 16311544Seschrock 16322082Seschrock spa_add_spares(spa, *config); 16335450Sbrendan spa_add_l2cache(spa, *config); 16342082Seschrock } 16352082Seschrock 16361544Seschrock /* 16371544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 16381544Seschrock * and call spa_lookup() directly. 16391544Seschrock */ 16401544Seschrock if (altroot) { 16411544Seschrock if (spa == NULL) { 16421544Seschrock mutex_enter(&spa_namespace_lock); 16431544Seschrock spa = spa_lookup(name); 16441544Seschrock if (spa) 16451544Seschrock spa_altroot(spa, altroot, buflen); 16461544Seschrock else 16471544Seschrock altroot[0] = '\0'; 16481544Seschrock spa = NULL; 16491544Seschrock mutex_exit(&spa_namespace_lock); 16501544Seschrock } else { 16511544Seschrock spa_altroot(spa, altroot, buflen); 16521544Seschrock } 16531544Seschrock } 16541544Seschrock 1655789Sahrens if (spa != NULL) 1656789Sahrens spa_close(spa, FTAG); 1657789Sahrens 1658789Sahrens return (error); 1659789Sahrens } 1660789Sahrens 1661789Sahrens /* 16625450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 16635450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 16645450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 16655450Sbrendan * specified, as long as they are well-formed. 16662082Seschrock */ 16672082Seschrock static int 16685450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 16695450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 16705450Sbrendan vdev_labeltype_t label) 16712082Seschrock { 16725450Sbrendan nvlist_t **dev; 16735450Sbrendan uint_t i, ndev; 16742082Seschrock vdev_t *vd; 16752082Seschrock int error; 16762082Seschrock 16772082Seschrock /* 16785450Sbrendan * It's acceptable to have no devs specified. 16792082Seschrock */ 16805450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 16812082Seschrock return (0); 16822082Seschrock 16835450Sbrendan if (ndev == 0) 16842082Seschrock return (EINVAL); 16852082Seschrock 16862082Seschrock /* 16875450Sbrendan * Make sure the pool is formatted with a version that supports this 16885450Sbrendan * device type. 16892082Seschrock */ 16905450Sbrendan if (spa_version(spa) < version) 16912082Seschrock return (ENOTSUP); 16922082Seschrock 16933377Seschrock /* 16945450Sbrendan * Set the pending device list so we correctly handle device in-use 16953377Seschrock * checking. 16963377Seschrock */ 16975450Sbrendan sav->sav_pending = dev; 16985450Sbrendan sav->sav_npending = ndev; 16995450Sbrendan 17005450Sbrendan for (i = 0; i < ndev; i++) { 17015450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 17022082Seschrock mode)) != 0) 17033377Seschrock goto out; 17042082Seschrock 17052082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 17062082Seschrock vdev_free(vd); 17073377Seschrock error = EINVAL; 17083377Seschrock goto out; 17092082Seschrock } 17102082Seschrock 17115450Sbrendan /* 17125450Sbrendan * The L2ARC currently only supports disk devices. 17135450Sbrendan */ 17145450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 17155450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 17165450Sbrendan error = ENOTBLK; 17175450Sbrendan goto out; 17185450Sbrendan } 17195450Sbrendan 17202082Seschrock vd->vdev_top = vd; 17213377Seschrock 17223377Seschrock if ((error = vdev_open(vd)) == 0 && 17235450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 17245450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 17253377Seschrock vd->vdev_guid) == 0); 17262082Seschrock } 17272082Seschrock 17282082Seschrock vdev_free(vd); 17293377Seschrock 17305450Sbrendan if (error && 17315450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 17323377Seschrock goto out; 17333377Seschrock else 17343377Seschrock error = 0; 17352082Seschrock } 17362082Seschrock 17373377Seschrock out: 17385450Sbrendan sav->sav_pending = NULL; 17395450Sbrendan sav->sav_npending = 0; 17403377Seschrock return (error); 17412082Seschrock } 17422082Seschrock 17435450Sbrendan static int 17445450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 17455450Sbrendan { 17465450Sbrendan int error; 17475450Sbrendan 17485450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17495450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 17505450Sbrendan VDEV_LABEL_SPARE)) != 0) { 17515450Sbrendan return (error); 17525450Sbrendan } 17535450Sbrendan 17545450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 17555450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 17565450Sbrendan VDEV_LABEL_L2CACHE)); 17575450Sbrendan } 17585450Sbrendan 17595450Sbrendan static void 17605450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 17615450Sbrendan const char *config) 17625450Sbrendan { 17635450Sbrendan int i; 17645450Sbrendan 17655450Sbrendan if (sav->sav_config != NULL) { 17665450Sbrendan nvlist_t **olddevs; 17675450Sbrendan uint_t oldndevs; 17685450Sbrendan nvlist_t **newdevs; 17695450Sbrendan 17705450Sbrendan /* 17715450Sbrendan * Generate new dev list by concatentating with the 17725450Sbrendan * current dev list. 17735450Sbrendan */ 17745450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 17755450Sbrendan &olddevs, &oldndevs) == 0); 17765450Sbrendan 17775450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 17785450Sbrendan (ndevs + oldndevs), KM_SLEEP); 17795450Sbrendan for (i = 0; i < oldndevs; i++) 17805450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 17815450Sbrendan KM_SLEEP) == 0); 17825450Sbrendan for (i = 0; i < ndevs; i++) 17835450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 17845450Sbrendan KM_SLEEP) == 0); 17855450Sbrendan 17865450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 17875450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 17885450Sbrendan 17895450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 17905450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 17915450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 17925450Sbrendan nvlist_free(newdevs[i]); 17935450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 17945450Sbrendan } else { 17955450Sbrendan /* 17965450Sbrendan * Generate a new dev list. 17975450Sbrendan */ 17985450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 17995450Sbrendan KM_SLEEP) == 0); 18005450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 18015450Sbrendan devs, ndevs) == 0); 18025450Sbrendan } 18035450Sbrendan } 18045450Sbrendan 18055450Sbrendan /* 18065450Sbrendan * Stop and drop level 2 ARC devices 18075450Sbrendan */ 18085450Sbrendan void 18095450Sbrendan spa_l2cache_drop(spa_t *spa) 18105450Sbrendan { 18115450Sbrendan vdev_t *vd; 18125450Sbrendan int i; 18135450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 18145450Sbrendan 18155450Sbrendan for (i = 0; i < sav->sav_count; i++) { 18165450Sbrendan uint64_t pool; 18175450Sbrendan 18185450Sbrendan vd = sav->sav_vdevs[i]; 18195450Sbrendan ASSERT(vd != NULL); 18205450Sbrendan 18215450Sbrendan if (spa_mode & FWRITE && 18226643Seschrock spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL && 18236643Seschrock l2arc_vdev_present(vd)) { 18245450Sbrendan l2arc_remove_vdev(vd); 18255450Sbrendan } 18265450Sbrendan if (vd->vdev_isl2cache) 18275450Sbrendan spa_l2cache_remove(vd); 18285450Sbrendan vdev_clear_stats(vd); 18295450Sbrendan (void) vdev_close(vd); 18305450Sbrendan } 18315450Sbrendan } 18325450Sbrendan 18332082Seschrock /* 1834789Sahrens * Pool Creation 1835789Sahrens */ 1836789Sahrens int 18375094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 18384715Sek110237 const char *history_str) 1839789Sahrens { 1840789Sahrens spa_t *spa; 18415094Slling char *altroot = NULL; 18421635Sbonwick vdev_t *rvd; 1843789Sahrens dsl_pool_t *dp; 1844789Sahrens dmu_tx_t *tx; 18452082Seschrock int c, error = 0; 1846789Sahrens uint64_t txg = TXG_INITIAL; 18475450Sbrendan nvlist_t **spares, **l2cache; 18485450Sbrendan uint_t nspares, nl2cache; 18495094Slling uint64_t version; 1850789Sahrens 1851789Sahrens /* 1852789Sahrens * If this pool already exists, return failure. 1853789Sahrens */ 1854789Sahrens mutex_enter(&spa_namespace_lock); 1855789Sahrens if (spa_lookup(pool) != NULL) { 1856789Sahrens mutex_exit(&spa_namespace_lock); 1857789Sahrens return (EEXIST); 1858789Sahrens } 1859789Sahrens 1860789Sahrens /* 1861789Sahrens * Allocate a new spa_t structure. 1862789Sahrens */ 18635094Slling (void) nvlist_lookup_string(props, 18645094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 18651635Sbonwick spa = spa_add(pool, altroot); 1866789Sahrens spa_activate(spa); 1867789Sahrens 1868789Sahrens spa->spa_uberblock.ub_txg = txg - 1; 18695094Slling 18705094Slling if (props && (error = spa_prop_validate(spa, props))) { 18715094Slling spa_unload(spa); 18725094Slling spa_deactivate(spa); 18735094Slling spa_remove(spa); 18746643Seschrock mutex_exit(&spa_namespace_lock); 18755094Slling return (error); 18765094Slling } 18775094Slling 18785094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 18795094Slling &version) != 0) 18805094Slling version = SPA_VERSION; 18815094Slling ASSERT(version <= SPA_VERSION); 18825094Slling spa->spa_uberblock.ub_version = version; 1883789Sahrens spa->spa_ubsync = spa->spa_uberblock; 1884789Sahrens 18851635Sbonwick /* 18861635Sbonwick * Create the root vdev. 18871635Sbonwick */ 18881635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 18891635Sbonwick 18902082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 18912082Seschrock 18922082Seschrock ASSERT(error != 0 || rvd != NULL); 18932082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 18942082Seschrock 18955913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 18961635Sbonwick error = EINVAL; 18972082Seschrock 18982082Seschrock if (error == 0 && 18992082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 19005450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 19012082Seschrock VDEV_ALLOC_ADD)) == 0) { 19022082Seschrock for (c = 0; c < rvd->vdev_children; c++) 19032082Seschrock vdev_init(rvd->vdev_child[c], txg); 19042082Seschrock vdev_config_dirty(rvd); 19051635Sbonwick } 19061635Sbonwick 19071635Sbonwick spa_config_exit(spa, FTAG); 1908789Sahrens 19092082Seschrock if (error != 0) { 1910789Sahrens spa_unload(spa); 1911789Sahrens spa_deactivate(spa); 1912789Sahrens spa_remove(spa); 1913789Sahrens mutex_exit(&spa_namespace_lock); 1914789Sahrens return (error); 1915789Sahrens } 1916789Sahrens 19172082Seschrock /* 19182082Seschrock * Get the list of spares, if specified. 19192082Seschrock */ 19202082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 19212082Seschrock &spares, &nspares) == 0) { 19225450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 19232082Seschrock KM_SLEEP) == 0); 19245450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 19252082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 19262082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 19272082Seschrock spa_load_spares(spa); 19282082Seschrock spa_config_exit(spa, FTAG); 19295450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 19305450Sbrendan } 19315450Sbrendan 19325450Sbrendan /* 19335450Sbrendan * Get the list of level 2 cache devices, if specified. 19345450Sbrendan */ 19355450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 19365450Sbrendan &l2cache, &nl2cache) == 0) { 19375450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 19385450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 19395450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 19405450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 19415450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 19425450Sbrendan spa_load_l2cache(spa); 19435450Sbrendan spa_config_exit(spa, FTAG); 19445450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 19452082Seschrock } 19462082Seschrock 1947789Sahrens spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1948789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 1949789Sahrens 1950789Sahrens tx = dmu_tx_create_assigned(dp, txg); 1951789Sahrens 1952789Sahrens /* 1953789Sahrens * Create the pool config object. 1954789Sahrens */ 1955789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1956789Sahrens DMU_OT_PACKED_NVLIST, 1 << 14, 1957789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1958789Sahrens 19591544Seschrock if (zap_add(spa->spa_meta_objset, 1960789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 19611544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 19621544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 19631544Seschrock } 1964789Sahrens 19655094Slling /* Newly created pools with the right version are always deflated. */ 19665094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 19675094Slling spa->spa_deflate = TRUE; 19685094Slling if (zap_add(spa->spa_meta_objset, 19695094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 19705094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 19715094Slling cmn_err(CE_PANIC, "failed to add deflate"); 19725094Slling } 19732082Seschrock } 19742082Seschrock 1975789Sahrens /* 1976789Sahrens * Create the deferred-free bplist object. Turn off compression 1977789Sahrens * because sync-to-convergence takes longer if the blocksize 1978789Sahrens * keeps changing. 1979789Sahrens */ 1980789Sahrens spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1981789Sahrens 1 << 14, tx); 1982789Sahrens dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1983789Sahrens ZIO_COMPRESS_OFF, tx); 1984789Sahrens 19851544Seschrock if (zap_add(spa->spa_meta_objset, 1986789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 19871544Seschrock sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 19881544Seschrock cmn_err(CE_PANIC, "failed to add bplist"); 19891544Seschrock } 1990789Sahrens 19912926Sek110237 /* 19922926Sek110237 * Create the pool's history object. 19932926Sek110237 */ 19945094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 19955094Slling spa_history_create_obj(spa, tx); 19965094Slling 19975094Slling /* 19985094Slling * Set pool properties. 19995094Slling */ 20005094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 20015094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20025329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 20035094Slling if (props) 20045094Slling spa_sync_props(spa, props, CRED(), tx); 20052926Sek110237 2006789Sahrens dmu_tx_commit(tx); 2007789Sahrens 2008789Sahrens spa->spa_sync_on = B_TRUE; 2009789Sahrens txg_sync_start(spa->spa_dsl_pool); 2010789Sahrens 2011789Sahrens /* 2012789Sahrens * We explicitly wait for the first transaction to complete so that our 2013789Sahrens * bean counters are appropriately updated. 2014789Sahrens */ 2015789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 2016789Sahrens 20176643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2018789Sahrens 20195094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 20204715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 20214715Sek110237 2022789Sahrens mutex_exit(&spa_namespace_lock); 2023789Sahrens 2024789Sahrens return (0); 2025789Sahrens } 2026789Sahrens 2027789Sahrens /* 2028789Sahrens * Import the given pool into the system. We set up the necessary spa_t and 2029789Sahrens * then call spa_load() to do the dirty work. 2030789Sahrens */ 20316423Sgw25295 static int 20326423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props, 20336643Seschrock boolean_t isroot, boolean_t allowfaulted) 2034789Sahrens { 2035789Sahrens spa_t *spa; 20365094Slling char *altroot = NULL; 20376643Seschrock int error, loaderr; 20382082Seschrock nvlist_t *nvroot; 20395450Sbrendan nvlist_t **spares, **l2cache; 20405450Sbrendan uint_t nspares, nl2cache; 20416423Sgw25295 int mosconfig = isroot? B_FALSE : B_TRUE; 2042789Sahrens 2043789Sahrens /* 2044789Sahrens * If a pool with this name exists, return failure. 2045789Sahrens */ 2046789Sahrens mutex_enter(&spa_namespace_lock); 2047789Sahrens if (spa_lookup(pool) != NULL) { 2048789Sahrens mutex_exit(&spa_namespace_lock); 2049789Sahrens return (EEXIST); 2050789Sahrens } 2051789Sahrens 2052789Sahrens /* 20531635Sbonwick * Create and initialize the spa structure. 2054789Sahrens */ 20555094Slling (void) nvlist_lookup_string(props, 20565094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 20571635Sbonwick spa = spa_add(pool, altroot); 2058789Sahrens spa_activate(spa); 2059789Sahrens 20606643Seschrock if (allowfaulted) 20616643Seschrock spa->spa_import_faulted = B_TRUE; 2062*6673Seschrock spa->spa_is_root = isroot; 20636643Seschrock 2064789Sahrens /* 20651635Sbonwick * Pass off the heavy lifting to spa_load(). 20661732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 20671732Sbonwick * is actually the one to trust when doing an import. 20681601Sbonwick */ 20696643Seschrock loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, mosconfig); 2070789Sahrens 20712082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20722082Seschrock /* 20732082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 20742082Seschrock * and conflicts with spa_has_spare(). 20752082Seschrock */ 20766423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 20775450Sbrendan nvlist_free(spa->spa_spares.sav_config); 20785450Sbrendan spa->spa_spares.sav_config = NULL; 20792082Seschrock spa_load_spares(spa); 20802082Seschrock } 20816423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 20825450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 20835450Sbrendan spa->spa_l2cache.sav_config = NULL; 20845450Sbrendan spa_load_l2cache(spa); 20855450Sbrendan } 20862082Seschrock 20872082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 20882082Seschrock &nvroot) == 0); 20895450Sbrendan if (error == 0) 20905450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 20915450Sbrendan if (error == 0) 20925450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 20935450Sbrendan VDEV_ALLOC_L2CACHE); 20942082Seschrock spa_config_exit(spa, FTAG); 20952082Seschrock 20965094Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 20976643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 20986643Seschrock /* 20996643Seschrock * If we failed to load the pool, but 'allowfaulted' is 21006643Seschrock * set, then manually set the config as if the config 21016643Seschrock * passed in was specified in the cache file. 21026643Seschrock */ 21036643Seschrock error = 0; 21046643Seschrock spa->spa_import_faulted = B_FALSE; 21056643Seschrock if (spa->spa_config == NULL) { 21066643Seschrock spa_config_enter(spa, RW_READER, FTAG); 21076643Seschrock spa->spa_config = spa_config_generate(spa, 21086643Seschrock NULL, -1ULL, B_TRUE); 21096643Seschrock spa_config_exit(spa, FTAG); 21106643Seschrock } 21116643Seschrock spa_unload(spa); 21126643Seschrock spa_deactivate(spa); 21136643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 21146643Seschrock } else { 21156643Seschrock spa_unload(spa); 21166643Seschrock spa_deactivate(spa); 21176643Seschrock spa_remove(spa); 21186643Seschrock } 2119789Sahrens mutex_exit(&spa_namespace_lock); 2120789Sahrens return (error); 2121789Sahrens } 2122789Sahrens 21231635Sbonwick /* 21245450Sbrendan * Override any spares and level 2 cache devices as specified by 21255450Sbrendan * the user, as these may have correct device names/devids, etc. 21262082Seschrock */ 21272082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 21282082Seschrock &spares, &nspares) == 0) { 21295450Sbrendan if (spa->spa_spares.sav_config) 21305450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 21312082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 21322082Seschrock else 21335450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 21342082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 21355450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 21362082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 21372082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21382082Seschrock spa_load_spares(spa); 21392082Seschrock spa_config_exit(spa, FTAG); 21405450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 21415450Sbrendan } 21425450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 21435450Sbrendan &l2cache, &nl2cache) == 0) { 21445450Sbrendan if (spa->spa_l2cache.sav_config) 21455450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 21465450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 21475450Sbrendan else 21485450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 21495450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 21505450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 21515450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21525450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 21535450Sbrendan spa_load_l2cache(spa); 21545450Sbrendan spa_config_exit(spa, FTAG); 21555450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 21562082Seschrock } 21572082Seschrock 21586643Seschrock if (spa_mode & FWRITE) { 21596643Seschrock /* 21606643Seschrock * Update the config cache to include the newly-imported pool. 21616643Seschrock */ 21626423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 21631635Sbonwick 21646643Seschrock /* 21656643Seschrock * Resilver anything that's out of date. 21666643Seschrock */ 21676643Seschrock if (!isroot) 21686643Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, 21696643Seschrock B_TRUE) == 0); 21706643Seschrock } 21716643Seschrock 21726643Seschrock spa->spa_import_faulted = B_FALSE; 21734451Seschrock mutex_exit(&spa_namespace_lock); 21744451Seschrock 2175789Sahrens return (0); 2176789Sahrens } 2177789Sahrens 21786423Sgw25295 #ifdef _KERNEL 21796423Sgw25295 /* 21806423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 21816423Sgw25295 * device label. 21826423Sgw25295 */ 21836423Sgw25295 static void 21846423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 21856423Sgw25295 { 21866423Sgw25295 nvlist_t *nvtop, *nvroot; 21876423Sgw25295 uint64_t pgid; 21886423Sgw25295 21896423Sgw25295 /* 21906423Sgw25295 * Add this top-level vdev to the child array. 21916423Sgw25295 */ 21926423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 21936423Sgw25295 == 0); 21946423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 21956423Sgw25295 == 0); 21966423Sgw25295 21976423Sgw25295 /* 21986423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 21996423Sgw25295 */ 22006423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 22016423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 22026423Sgw25295 == 0); 22036423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 22046423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 22056423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 22066423Sgw25295 &nvtop, 1) == 0); 22076423Sgw25295 22086423Sgw25295 /* 22096423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 22106423Sgw25295 * this pool's configuration (remove the old, add the new). 22116423Sgw25295 */ 22126423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 22136423Sgw25295 nvlist_free(nvroot); 22146423Sgw25295 } 22156423Sgw25295 22166423Sgw25295 /* 22176423Sgw25295 * Get the root pool information from the root disk, then import the root pool 22186423Sgw25295 * during the system boot up time. 22196423Sgw25295 */ 22206423Sgw25295 extern nvlist_t *vdev_disk_read_rootlabel(char *); 22216423Sgw25295 22226423Sgw25295 void 22236423Sgw25295 spa_check_rootconf(char *devpath, char **bestdev, nvlist_t **bestconf, 22246423Sgw25295 uint64_t *besttxg) 22256423Sgw25295 { 22266423Sgw25295 nvlist_t *config; 22276423Sgw25295 uint64_t txg; 22286423Sgw25295 22296423Sgw25295 if ((config = vdev_disk_read_rootlabel(devpath)) == NULL) 22306423Sgw25295 return; 22316423Sgw25295 22326423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 22336423Sgw25295 22346423Sgw25295 if (txg > *besttxg) { 22356423Sgw25295 *besttxg = txg; 22366423Sgw25295 if (*bestconf != NULL) 22376423Sgw25295 nvlist_free(*bestconf); 22386423Sgw25295 *bestconf = config; 22396423Sgw25295 *bestdev = devpath; 22406423Sgw25295 } 22416423Sgw25295 } 22426423Sgw25295 22436423Sgw25295 boolean_t 22446423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 22456423Sgw25295 { 22466423Sgw25295 uint64_t ival; 22476423Sgw25295 22486423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 22496423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 22506423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, &ival) == 0 || 22516423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 22526423Sgw25295 return (B_FALSE); 22536423Sgw25295 22546423Sgw25295 return (B_TRUE); 22556423Sgw25295 } 22566423Sgw25295 22576423Sgw25295 /* 22586423Sgw25295 * Import a root pool. 22596423Sgw25295 * 22606423Sgw25295 * For x86. devpath_list will consist the physpath name of the vdev in a single 22616423Sgw25295 * disk root pool or a list of physnames for the vdevs in a mirrored rootpool. 22626423Sgw25295 * e.g. 22636423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a /pci@1f,o/ide@d/disk@2,0:a" 22646423Sgw25295 * 22656423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 22666423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 22676423Sgw25295 * e.g. 22686423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 22696423Sgw25295 */ 22706423Sgw25295 int 22716423Sgw25295 spa_import_rootpool(char *devpath_list) 22726423Sgw25295 { 22736423Sgw25295 nvlist_t *conf = NULL; 22746423Sgw25295 char *dev = NULL; 22756423Sgw25295 char *pname; 22766423Sgw25295 int error; 22776423Sgw25295 22786423Sgw25295 /* 22796423Sgw25295 * Get the vdev pathname and configuation from the most 22806423Sgw25295 * recently updated vdev (highest txg). 22816423Sgw25295 */ 22826423Sgw25295 if (error = spa_get_rootconf(devpath_list, &dev, &conf)) 22836423Sgw25295 goto msg_out; 22846423Sgw25295 22856423Sgw25295 /* 22866423Sgw25295 * Add type "root" vdev to the config. 22876423Sgw25295 */ 22886423Sgw25295 spa_build_rootpool_config(conf); 22896423Sgw25295 22906423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 22916423Sgw25295 2292*6673Seschrock /* 2293*6673Seschrock * We specify 'allowfaulted' for this to be treated like spa_open() 2294*6673Seschrock * instead of spa_import(). This prevents us from marking vdevs as 2295*6673Seschrock * persistently unavailable, and generates FMA ereports as if it were a 2296*6673Seschrock * pool open, not import. 2297*6673Seschrock */ 2298*6673Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE); 22996423Sgw25295 if (error == EEXIST) 23006423Sgw25295 error = 0; 23016423Sgw25295 23026423Sgw25295 nvlist_free(conf); 23036423Sgw25295 return (error); 23046423Sgw25295 23056423Sgw25295 msg_out: 23066423Sgw25295 cmn_err(CE_NOTE, "\n\n" 23076423Sgw25295 " *************************************************** \n" 23086423Sgw25295 " * This device is not bootable! * \n" 23096423Sgw25295 " * It is either offlined or detached or faulted. * \n" 23106423Sgw25295 " * Please try to boot from a different device. * \n" 23116423Sgw25295 " *************************************************** \n\n"); 23126423Sgw25295 23136423Sgw25295 return (error); 23146423Sgw25295 } 23156423Sgw25295 #endif 23166423Sgw25295 23176423Sgw25295 /* 23186423Sgw25295 * Import a non-root pool into the system. 23196423Sgw25295 */ 23206423Sgw25295 int 23216423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 23226423Sgw25295 { 23236643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 23246423Sgw25295 } 23256423Sgw25295 23266643Seschrock int 23276643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 23286643Seschrock { 23296643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 23306643Seschrock } 23316643Seschrock 23326643Seschrock 2333789Sahrens /* 2334789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2335789Sahrens * to get the vdev stats associated with the imported devices. 2336789Sahrens */ 2337789Sahrens #define TRYIMPORT_NAME "$import" 2338789Sahrens 2339789Sahrens nvlist_t * 2340789Sahrens spa_tryimport(nvlist_t *tryconfig) 2341789Sahrens { 2342789Sahrens nvlist_t *config = NULL; 2343789Sahrens char *poolname; 2344789Sahrens spa_t *spa; 2345789Sahrens uint64_t state; 2346789Sahrens 2347789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2348789Sahrens return (NULL); 2349789Sahrens 2350789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2351789Sahrens return (NULL); 2352789Sahrens 23531635Sbonwick /* 23541635Sbonwick * Create and initialize the spa structure. 23551635Sbonwick */ 2356789Sahrens mutex_enter(&spa_namespace_lock); 23571635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2358789Sahrens spa_activate(spa); 2359789Sahrens 2360789Sahrens /* 23611635Sbonwick * Pass off the heavy lifting to spa_load(). 23621732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 23631732Sbonwick * is actually the one to trust when doing an import. 2364789Sahrens */ 23651732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2366789Sahrens 2367789Sahrens /* 2368789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2369789Sahrens */ 2370789Sahrens if (spa->spa_root_vdev != NULL) { 23711635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 2372789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 23731635Sbonwick spa_config_exit(spa, FTAG); 2374789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2375789Sahrens poolname) == 0); 2376789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2377789Sahrens state) == 0); 23783975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 23793975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 23802082Seschrock 23812082Seschrock /* 23826423Sgw25295 * If the bootfs property exists on this pool then we 23836423Sgw25295 * copy it out so that external consumers can tell which 23846423Sgw25295 * pools are bootable. 23856423Sgw25295 */ 23866423Sgw25295 if (spa->spa_bootfs) { 23876423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 23886423Sgw25295 23896423Sgw25295 /* 23906423Sgw25295 * We have to play games with the name since the 23916423Sgw25295 * pool was opened as TRYIMPORT_NAME. 23926423Sgw25295 */ 23936423Sgw25295 if (dsl_dsobj_to_dsname(spa->spa_name, 23946423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 23956423Sgw25295 char *cp; 23966423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 23976423Sgw25295 23986423Sgw25295 cp = strchr(tmpname, '/'); 23996423Sgw25295 if (cp == NULL) { 24006423Sgw25295 (void) strlcpy(dsname, tmpname, 24016423Sgw25295 MAXPATHLEN); 24026423Sgw25295 } else { 24036423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 24046423Sgw25295 "%s/%s", poolname, ++cp); 24056423Sgw25295 } 24066423Sgw25295 VERIFY(nvlist_add_string(config, 24076423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 24086423Sgw25295 kmem_free(dsname, MAXPATHLEN); 24096423Sgw25295 } 24106423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 24116423Sgw25295 } 24126423Sgw25295 24136423Sgw25295 /* 24145450Sbrendan * Add the list of hot spares and level 2 cache devices. 24152082Seschrock */ 24162082Seschrock spa_add_spares(spa, config); 24175450Sbrendan spa_add_l2cache(spa, config); 2418789Sahrens } 2419789Sahrens 2420789Sahrens spa_unload(spa); 2421789Sahrens spa_deactivate(spa); 2422789Sahrens spa_remove(spa); 2423789Sahrens mutex_exit(&spa_namespace_lock); 2424789Sahrens 2425789Sahrens return (config); 2426789Sahrens } 2427789Sahrens 2428789Sahrens /* 2429789Sahrens * Pool export/destroy 2430789Sahrens * 2431789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2432789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2433789Sahrens * update the pool state and sync all the labels to disk, removing the 2434789Sahrens * configuration from the cache afterwards. 2435789Sahrens */ 2436789Sahrens static int 24371775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 2438789Sahrens { 2439789Sahrens spa_t *spa; 2440789Sahrens 24411775Sbillm if (oldconfig) 24421775Sbillm *oldconfig = NULL; 24431775Sbillm 2444789Sahrens if (!(spa_mode & FWRITE)) 2445789Sahrens return (EROFS); 2446789Sahrens 2447789Sahrens mutex_enter(&spa_namespace_lock); 2448789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2449789Sahrens mutex_exit(&spa_namespace_lock); 2450789Sahrens return (ENOENT); 2451789Sahrens } 2452789Sahrens 2453789Sahrens /* 24541544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 24551544Seschrock * reacquire the namespace lock, and see if we can export. 24561544Seschrock */ 24571544Seschrock spa_open_ref(spa, FTAG); 24581544Seschrock mutex_exit(&spa_namespace_lock); 24591544Seschrock spa_async_suspend(spa); 24601544Seschrock mutex_enter(&spa_namespace_lock); 24611544Seschrock spa_close(spa, FTAG); 24621544Seschrock 24631544Seschrock /* 2464789Sahrens * The pool will be in core if it's openable, 2465789Sahrens * in which case we can modify its state. 2466789Sahrens */ 2467789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2468789Sahrens /* 2469789Sahrens * Objsets may be open only because they're dirty, so we 2470789Sahrens * have to force it to sync before checking spa_refcnt. 2471789Sahrens */ 2472789Sahrens spa_scrub_suspend(spa); 2473789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2474789Sahrens 24751544Seschrock /* 24761544Seschrock * A pool cannot be exported or destroyed if there are active 24771544Seschrock * references. If we are resetting a pool, allow references by 24781544Seschrock * fault injection handlers. 24791544Seschrock */ 24801544Seschrock if (!spa_refcount_zero(spa) || 24811544Seschrock (spa->spa_inject_ref != 0 && 24821544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 2483789Sahrens spa_scrub_resume(spa); 24841544Seschrock spa_async_resume(spa); 2485789Sahrens mutex_exit(&spa_namespace_lock); 2486789Sahrens return (EBUSY); 2487789Sahrens } 2488789Sahrens 2489789Sahrens spa_scrub_resume(spa); 2490789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 2491789Sahrens 2492789Sahrens /* 2493789Sahrens * We want this to be reflected on every label, 2494789Sahrens * so mark them all dirty. spa_unload() will do the 2495789Sahrens * final sync that pushes these changes out. 2496789Sahrens */ 24971544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 24981601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 24991544Seschrock spa->spa_state = new_state; 25001635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 25011544Seschrock vdev_config_dirty(spa->spa_root_vdev); 25021601Sbonwick spa_config_exit(spa, FTAG); 25031544Seschrock } 2504789Sahrens } 2505789Sahrens 25064451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 25074451Seschrock 2508789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2509789Sahrens spa_unload(spa); 2510789Sahrens spa_deactivate(spa); 2511789Sahrens } 2512789Sahrens 25131775Sbillm if (oldconfig && spa->spa_config) 25141775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 25151775Sbillm 25161544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 25176643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 25181544Seschrock spa_remove(spa); 25191544Seschrock } 2520789Sahrens mutex_exit(&spa_namespace_lock); 2521789Sahrens 2522789Sahrens return (0); 2523789Sahrens } 2524789Sahrens 2525789Sahrens /* 2526789Sahrens * Destroy a storage pool. 2527789Sahrens */ 2528789Sahrens int 2529789Sahrens spa_destroy(char *pool) 2530789Sahrens { 25311775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 2532789Sahrens } 2533789Sahrens 2534789Sahrens /* 2535789Sahrens * Export a storage pool. 2536789Sahrens */ 2537789Sahrens int 25381775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 2539789Sahrens { 25401775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 2541789Sahrens } 2542789Sahrens 2543789Sahrens /* 25441544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 25451544Seschrock * from the namespace in any way. 25461544Seschrock */ 25471544Seschrock int 25481544Seschrock spa_reset(char *pool) 25491544Seschrock { 25501775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 25511544Seschrock } 25521544Seschrock 25531544Seschrock 25541544Seschrock /* 2555789Sahrens * ========================================================================== 2556789Sahrens * Device manipulation 2557789Sahrens * ========================================================================== 2558789Sahrens */ 2559789Sahrens 2560789Sahrens /* 25614527Sperrin * Add a device to a storage pool. 2562789Sahrens */ 2563789Sahrens int 2564789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2565789Sahrens { 2566789Sahrens uint64_t txg; 25671635Sbonwick int c, error; 2568789Sahrens vdev_t *rvd = spa->spa_root_vdev; 25691585Sbonwick vdev_t *vd, *tvd; 25705450Sbrendan nvlist_t **spares, **l2cache; 25715450Sbrendan uint_t nspares, nl2cache; 2572789Sahrens 2573789Sahrens txg = spa_vdev_enter(spa); 2574789Sahrens 25752082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 25762082Seschrock VDEV_ALLOC_ADD)) != 0) 25772082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 25782082Seschrock 25793377Seschrock spa->spa_pending_vdev = vd; 2580789Sahrens 25815450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 25825450Sbrendan &nspares) != 0) 25832082Seschrock nspares = 0; 25842082Seschrock 25855450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 25865450Sbrendan &nl2cache) != 0) 25875450Sbrendan nl2cache = 0; 25885450Sbrendan 25895450Sbrendan if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) { 25903377Seschrock spa->spa_pending_vdev = NULL; 25912082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 25923377Seschrock } 25932082Seschrock 25942082Seschrock if (vd->vdev_children != 0) { 25953377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 25963377Seschrock spa->spa_pending_vdev = NULL; 25972082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 25982082Seschrock } 25992082Seschrock } 26002082Seschrock 26013377Seschrock /* 26025450Sbrendan * We must validate the spares and l2cache devices after checking the 26035450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 26043377Seschrock */ 26055450Sbrendan if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) { 26063377Seschrock spa->spa_pending_vdev = NULL; 26073377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 26083377Seschrock } 26093377Seschrock 26103377Seschrock spa->spa_pending_vdev = NULL; 26113377Seschrock 26123377Seschrock /* 26133377Seschrock * Transfer each new top-level vdev from vd to rvd. 26143377Seschrock */ 26153377Seschrock for (c = 0; c < vd->vdev_children; c++) { 26163377Seschrock tvd = vd->vdev_child[c]; 26173377Seschrock vdev_remove_child(vd, tvd); 26183377Seschrock tvd->vdev_id = rvd->vdev_children; 26193377Seschrock vdev_add_child(rvd, tvd); 26203377Seschrock vdev_config_dirty(tvd); 26213377Seschrock } 26223377Seschrock 26232082Seschrock if (nspares != 0) { 26245450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 26255450Sbrendan ZPOOL_CONFIG_SPARES); 26262082Seschrock spa_load_spares(spa); 26275450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 26285450Sbrendan } 26295450Sbrendan 26305450Sbrendan if (nl2cache != 0) { 26315450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 26325450Sbrendan ZPOOL_CONFIG_L2CACHE); 26335450Sbrendan spa_load_l2cache(spa); 26345450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2635789Sahrens } 2636789Sahrens 2637789Sahrens /* 26381585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 26391585Sbonwick * If other threads start allocating from these vdevs before we 26401585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 26411585Sbonwick * fail to open the pool because there are DVAs that the config cache 26421585Sbonwick * can't translate. Therefore, we first add the vdevs without 26431585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 26441635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 26451585Sbonwick * 26461585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 26471585Sbonwick * if we lose power at any point in this sequence, the remaining 26481585Sbonwick * steps will be completed the next time we load the pool. 2649789Sahrens */ 26501635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 26511585Sbonwick 26521635Sbonwick mutex_enter(&spa_namespace_lock); 26531635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 26541635Sbonwick mutex_exit(&spa_namespace_lock); 2655789Sahrens 26561635Sbonwick return (0); 2657789Sahrens } 2658789Sahrens 2659789Sahrens /* 2660789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2661789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2662789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2663789Sahrens * 2664789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2665789Sahrens * existing device; in this case the two devices are made into their own 26664451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2667789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2668789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2669789Sahrens * completion of resilvering, the first disk (the one being replaced) 2670789Sahrens * is automatically detached. 2671789Sahrens */ 2672789Sahrens int 26731544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2674789Sahrens { 2675789Sahrens uint64_t txg, open_txg; 2676789Sahrens int error; 2677789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2678789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 26792082Seschrock vdev_ops_t *pvops; 26804527Sperrin int is_log; 2681789Sahrens 2682789Sahrens txg = spa_vdev_enter(spa); 2683789Sahrens 26846643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2685789Sahrens 2686789Sahrens if (oldvd == NULL) 2687789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2688789Sahrens 26891585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 26901585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 26911585Sbonwick 2692789Sahrens pvd = oldvd->vdev_parent; 2693789Sahrens 26942082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 26954451Seschrock VDEV_ALLOC_ADD)) != 0) 26964451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 26974451Seschrock 26984451Seschrock if (newrootvd->vdev_children != 1) 2699789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2700789Sahrens 2701789Sahrens newvd = newrootvd->vdev_child[0]; 2702789Sahrens 2703789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2704789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2705789Sahrens 27062082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2707789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2708789Sahrens 27094527Sperrin /* 27104527Sperrin * Spares can't replace logs 27114527Sperrin */ 27124527Sperrin is_log = oldvd->vdev_islog; 27134527Sperrin if (is_log && newvd->vdev_isspare) 27144527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27154527Sperrin 27162082Seschrock if (!replacing) { 27172082Seschrock /* 27182082Seschrock * For attach, the only allowable parent is a mirror or the root 27192082Seschrock * vdev. 27202082Seschrock */ 27212082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 27222082Seschrock pvd->vdev_ops != &vdev_root_ops) 27232082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27242082Seschrock 27252082Seschrock pvops = &vdev_mirror_ops; 27262082Seschrock } else { 27272082Seschrock /* 27282082Seschrock * Active hot spares can only be replaced by inactive hot 27292082Seschrock * spares. 27302082Seschrock */ 27312082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 27322082Seschrock pvd->vdev_child[1] == oldvd && 27332082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 27342082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27352082Seschrock 27362082Seschrock /* 27372082Seschrock * If the source is a hot spare, and the parent isn't already a 27382082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 27393377Seschrock * want to create a replacing vdev. The user is not allowed to 27403377Seschrock * attach to a spared vdev child unless the 'isspare' state is 27413377Seschrock * the same (spare replaces spare, non-spare replaces 27423377Seschrock * non-spare). 27432082Seschrock */ 27442082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 27452082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27463377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 27473377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 27483377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27492082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 27502082Seschrock newvd->vdev_isspare) 27512082Seschrock pvops = &vdev_spare_ops; 27522082Seschrock else 27532082Seschrock pvops = &vdev_replacing_ops; 27542082Seschrock } 27552082Seschrock 27561175Slling /* 27571175Slling * Compare the new device size with the replaceable/attachable 27581175Slling * device size. 27591175Slling */ 27601175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2761789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2762789Sahrens 27631732Sbonwick /* 27641732Sbonwick * The new device cannot have a higher alignment requirement 27651732Sbonwick * than the top-level vdev. 27661732Sbonwick */ 27671732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2768789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2769789Sahrens 2770789Sahrens /* 2771789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2772789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2773789Sahrens */ 2774789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2775789Sahrens spa_strfree(oldvd->vdev_path); 2776789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2777789Sahrens KM_SLEEP); 2778789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2779789Sahrens newvd->vdev_path, "old"); 2780789Sahrens if (oldvd->vdev_devid != NULL) { 2781789Sahrens spa_strfree(oldvd->vdev_devid); 2782789Sahrens oldvd->vdev_devid = NULL; 2783789Sahrens } 2784789Sahrens } 2785789Sahrens 2786789Sahrens /* 27872082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 27882082Seschrock * mirror/replacing/spare vdev above oldvd. 2789789Sahrens */ 2790789Sahrens if (pvd->vdev_ops != pvops) 2791789Sahrens pvd = vdev_add_parent(oldvd, pvops); 2792789Sahrens 2793789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2794789Sahrens ASSERT(pvd->vdev_ops == pvops); 2795789Sahrens ASSERT(oldvd->vdev_parent == pvd); 2796789Sahrens 2797789Sahrens /* 2798789Sahrens * Extract the new device from its root and add it to pvd. 2799789Sahrens */ 2800789Sahrens vdev_remove_child(newrootvd, newvd); 2801789Sahrens newvd->vdev_id = pvd->vdev_children; 2802789Sahrens vdev_add_child(pvd, newvd); 2803789Sahrens 28041544Seschrock /* 28051544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 28061544Seschrock * the addition of newvd may have decreased our parent's asize. 28071544Seschrock */ 28081544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 28091544Seschrock 2810789Sahrens tvd = newvd->vdev_top; 2811789Sahrens ASSERT(pvd->vdev_top == tvd); 2812789Sahrens ASSERT(tvd->vdev_parent == rvd); 2813789Sahrens 2814789Sahrens vdev_config_dirty(tvd); 2815789Sahrens 2816789Sahrens /* 2817789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2818789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2819789Sahrens */ 2820789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2821789Sahrens 2822789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2823789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2824789Sahrens open_txg - TXG_INITIAL + 1); 2825789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2826789Sahrens 28273377Seschrock if (newvd->vdev_isspare) 28283377Seschrock spa_spare_activate(newvd); 28291544Seschrock 2830789Sahrens /* 2831789Sahrens * Mark newvd's DTL dirty in this txg. 2832789Sahrens */ 28331732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2834789Sahrens 2835789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2836789Sahrens 2837789Sahrens /* 28384451Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 28394451Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 2840789Sahrens */ 28414451Seschrock mutex_enter(&spa_namespace_lock); 2842789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 28434451Seschrock mutex_exit(&spa_namespace_lock); 2844789Sahrens 2845789Sahrens return (0); 2846789Sahrens } 2847789Sahrens 2848789Sahrens /* 2849789Sahrens * Detach a device from a mirror or replacing vdev. 2850789Sahrens * If 'replace_done' is specified, only detach if the parent 2851789Sahrens * is a replacing vdev. 2852789Sahrens */ 2853789Sahrens int 28541544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2855789Sahrens { 2856789Sahrens uint64_t txg; 2857789Sahrens int c, t, error; 2858789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2859789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 28602082Seschrock boolean_t unspare = B_FALSE; 28612082Seschrock uint64_t unspare_guid; 2862*6673Seschrock size_t len; 2863789Sahrens 2864789Sahrens txg = spa_vdev_enter(spa); 2865789Sahrens 28666643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 2867789Sahrens 2868789Sahrens if (vd == NULL) 2869789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2870789Sahrens 28711585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 28721585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28731585Sbonwick 2874789Sahrens pvd = vd->vdev_parent; 2875789Sahrens 2876789Sahrens /* 2877789Sahrens * If replace_done is specified, only remove this device if it's 28782082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 28792082Seschrock * disk can be removed. 2880789Sahrens */ 28812082Seschrock if (replace_done) { 28822082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 28832082Seschrock if (vd->vdev_id != 0) 28842082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28852082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 28862082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28872082Seschrock } 28882082Seschrock } 28892082Seschrock 28902082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 28914577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 2892789Sahrens 2893789Sahrens /* 28942082Seschrock * Only mirror, replacing, and spare vdevs support detach. 2895789Sahrens */ 2896789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 28972082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 28982082Seschrock pvd->vdev_ops != &vdev_spare_ops) 2899789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2900789Sahrens 2901789Sahrens /* 2902789Sahrens * If there's only one replica, you can't detach it. 2903789Sahrens */ 2904789Sahrens if (pvd->vdev_children <= 1) 2905789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2906789Sahrens 2907789Sahrens /* 2908789Sahrens * If all siblings have non-empty DTLs, this device may have the only 2909789Sahrens * valid copy of the data, which means we cannot safely detach it. 2910789Sahrens * 2911789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 2912789Sahrens * precise DTL check. 2913789Sahrens */ 2914789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 2915789Sahrens uint64_t dirty; 2916789Sahrens 2917789Sahrens cvd = pvd->vdev_child[c]; 2918789Sahrens if (cvd == vd) 2919789Sahrens continue; 2920789Sahrens if (vdev_is_dead(cvd)) 2921789Sahrens continue; 2922789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 2923789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 2924789Sahrens cvd->vdev_dtl_scrub.sm_space; 2925789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 2926789Sahrens if (!dirty) 2927789Sahrens break; 2928789Sahrens } 29292082Seschrock 29302082Seschrock /* 29312082Seschrock * If we are a replacing or spare vdev, then we can always detach the 29322082Seschrock * latter child, as that is how one cancels the operation. 29332082Seschrock */ 29342082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 29352082Seschrock c == pvd->vdev_children) 2936789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2937789Sahrens 2938789Sahrens /* 2939*6673Seschrock * If we are detaching the second disk from a replacing vdev, then 2940*6673Seschrock * check to see if we changed the original vdev's path to have "/old" 2941*6673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 2942*6673Seschrock */ 2943*6673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 2944*6673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 2945*6673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 2946*6673Seschrock ASSERT(pvd->vdev_child[1] == vd); 2947*6673Seschrock cvd = pvd->vdev_child[0]; 2948*6673Seschrock len = strlen(vd->vdev_path); 2949*6673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 2950*6673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 2951*6673Seschrock spa_strfree(cvd->vdev_path); 2952*6673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 2953*6673Seschrock } 2954*6673Seschrock } 2955*6673Seschrock 2956*6673Seschrock /* 29572082Seschrock * If we are detaching the original disk from a spare, then it implies 29582082Seschrock * that the spare should become a real disk, and be removed from the 29592082Seschrock * active spare list for the pool. 29602082Seschrock */ 29612082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 29622082Seschrock vd->vdev_id == 0) 29632082Seschrock unspare = B_TRUE; 29642082Seschrock 29652082Seschrock /* 2966789Sahrens * Erase the disk labels so the disk can be used for other things. 2967789Sahrens * This must be done after all other error cases are handled, 2968789Sahrens * but before we disembowel vd (so we can still do I/O to it). 2969789Sahrens * But if we can't do it, don't treat the error as fatal -- 2970789Sahrens * it may be that the unwritability of the disk is the reason 2971789Sahrens * it's being detached! 2972789Sahrens */ 29733377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 2974789Sahrens 2975789Sahrens /* 2976789Sahrens * Remove vd from its parent and compact the parent's children. 2977789Sahrens */ 2978789Sahrens vdev_remove_child(pvd, vd); 2979789Sahrens vdev_compact_children(pvd); 2980789Sahrens 2981789Sahrens /* 2982789Sahrens * Remember one of the remaining children so we can get tvd below. 2983789Sahrens */ 2984789Sahrens cvd = pvd->vdev_child[0]; 2985789Sahrens 2986789Sahrens /* 29872082Seschrock * If we need to remove the remaining child from the list of hot spares, 29882082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 29892082Seschrock * must do this before vdev_remove_parent(), because that can change the 29902082Seschrock * GUID if it creates a new toplevel GUID. 29912082Seschrock */ 29922082Seschrock if (unspare) { 29932082Seschrock ASSERT(cvd->vdev_isspare); 29943377Seschrock spa_spare_remove(cvd); 29952082Seschrock unspare_guid = cvd->vdev_guid; 29962082Seschrock } 29972082Seschrock 29982082Seschrock /* 2999789Sahrens * If the parent mirror/replacing vdev only has one child, 3000789Sahrens * the parent is no longer needed. Remove it from the tree. 3001789Sahrens */ 3002789Sahrens if (pvd->vdev_children == 1) 3003789Sahrens vdev_remove_parent(cvd); 3004789Sahrens 3005789Sahrens /* 3006789Sahrens * We don't set tvd until now because the parent we just removed 3007789Sahrens * may have been the previous top-level vdev. 3008789Sahrens */ 3009789Sahrens tvd = cvd->vdev_top; 3010789Sahrens ASSERT(tvd->vdev_parent == rvd); 3011789Sahrens 3012789Sahrens /* 30133377Seschrock * Reevaluate the parent vdev state. 3014789Sahrens */ 30154451Seschrock vdev_propagate_state(cvd); 3016789Sahrens 3017789Sahrens /* 30183377Seschrock * If the device we just detached was smaller than the others, it may be 30193377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 30203377Seschrock * can't fail because the existing metaslabs are already in core, so 30213377Seschrock * there's nothing to read from disk. 3022789Sahrens */ 30231732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 3024789Sahrens 3025789Sahrens vdev_config_dirty(tvd); 3026789Sahrens 3027789Sahrens /* 30283377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 30293377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 30303377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 30313377Seschrock * prevent vd from being accessed after it's freed. 3032789Sahrens */ 3033789Sahrens for (t = 0; t < TXG_SIZE; t++) 3034789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 30351732Sbonwick vd->vdev_detached = B_TRUE; 30361732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3037789Sahrens 30384451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 30394451Seschrock 30402082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 30412082Seschrock 30422082Seschrock /* 30433377Seschrock * If this was the removal of the original device in a hot spare vdev, 30443377Seschrock * then we want to go through and remove the device from the hot spare 30453377Seschrock * list of every other pool. 30462082Seschrock */ 30472082Seschrock if (unspare) { 30482082Seschrock spa = NULL; 30492082Seschrock mutex_enter(&spa_namespace_lock); 30502082Seschrock while ((spa = spa_next(spa)) != NULL) { 30512082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 30522082Seschrock continue; 30532082Seschrock 30542082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 30552082Seschrock } 30562082Seschrock mutex_exit(&spa_namespace_lock); 30572082Seschrock } 30582082Seschrock 30592082Seschrock return (error); 30602082Seschrock } 30612082Seschrock 30622082Seschrock /* 30635450Sbrendan * Remove a spares vdev from the nvlist config. 30642082Seschrock */ 30655450Sbrendan static int 30665450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare, 30675450Sbrendan nvlist_t **spares, int nspares, vdev_t *vd) 30682082Seschrock { 30695450Sbrendan nvlist_t *nv, **newspares; 30705450Sbrendan int i, j; 30712082Seschrock 30722082Seschrock nv = NULL; 30735450Sbrendan for (i = 0; i < nspares; i++) { 30745450Sbrendan uint64_t theguid; 30755450Sbrendan 30765450Sbrendan VERIFY(nvlist_lookup_uint64(spares[i], 30775450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 30785450Sbrendan if (theguid == guid) { 30795450Sbrendan nv = spares[i]; 30805450Sbrendan break; 30812082Seschrock } 30822082Seschrock } 30832082Seschrock 30842082Seschrock /* 30855450Sbrendan * Only remove the hot spare if it's not currently in use in this pool. 30862082Seschrock */ 30875450Sbrendan if (nv == NULL && vd == NULL) 30885450Sbrendan return (ENOENT); 30895450Sbrendan 30905450Sbrendan if (nv == NULL && vd != NULL) 30915450Sbrendan return (ENOTSUP); 30925450Sbrendan 30935450Sbrendan if (!unspare && nv != NULL && vd != NULL) 30945450Sbrendan return (EBUSY); 30952082Seschrock 30962082Seschrock if (nspares == 1) { 30972082Seschrock newspares = NULL; 30982082Seschrock } else { 30992082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 31002082Seschrock KM_SLEEP); 31012082Seschrock for (i = 0, j = 0; i < nspares; i++) { 31022082Seschrock if (spares[i] != nv) 31032082Seschrock VERIFY(nvlist_dup(spares[i], 31042082Seschrock &newspares[j++], KM_SLEEP) == 0); 31052082Seschrock } 31062082Seschrock } 31072082Seschrock 31085450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES, 31092082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 31105450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 31115450Sbrendan ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0); 31122082Seschrock for (i = 0; i < nspares - 1; i++) 31132082Seschrock nvlist_free(newspares[i]); 31142082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 31155450Sbrendan 31165450Sbrendan return (0); 31175450Sbrendan } 31185450Sbrendan 31195450Sbrendan /* 31205450Sbrendan * Remove an l2cache vdev from the nvlist config. 31215450Sbrendan */ 31225450Sbrendan static int 31235450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache, 31245450Sbrendan int nl2cache, vdev_t *vd) 31255450Sbrendan { 31265450Sbrendan nvlist_t *nv, **newl2cache; 31275450Sbrendan int i, j; 31285450Sbrendan 31295450Sbrendan nv = NULL; 31305450Sbrendan for (i = 0; i < nl2cache; i++) { 31315450Sbrendan uint64_t theguid; 31325450Sbrendan 31335450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 31345450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 31355450Sbrendan if (theguid == guid) { 31365450Sbrendan nv = l2cache[i]; 31375450Sbrendan break; 31385450Sbrendan } 31395450Sbrendan } 31405450Sbrendan 31415450Sbrendan if (vd == NULL) { 31425450Sbrendan for (i = 0; i < nl2cache; i++) { 31435450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) { 31445450Sbrendan vd = sav->sav_vdevs[i]; 31455450Sbrendan break; 31465450Sbrendan } 31475450Sbrendan } 31485450Sbrendan } 31495450Sbrendan 31505450Sbrendan if (nv == NULL && vd == NULL) 31515450Sbrendan return (ENOENT); 31525450Sbrendan 31535450Sbrendan if (nv == NULL && vd != NULL) 31545450Sbrendan return (ENOTSUP); 31555450Sbrendan 31565450Sbrendan if (nl2cache == 1) { 31575450Sbrendan newl2cache = NULL; 31585450Sbrendan } else { 31595450Sbrendan newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *), 31605450Sbrendan KM_SLEEP); 31615450Sbrendan for (i = 0, j = 0; i < nl2cache; i++) { 31625450Sbrendan if (l2cache[i] != nv) 31635450Sbrendan VERIFY(nvlist_dup(l2cache[i], 31645450Sbrendan &newl2cache[j++], KM_SLEEP) == 0); 31655450Sbrendan } 31665450Sbrendan } 31675450Sbrendan 31685450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 31695450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 31705450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 31715450Sbrendan ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0); 31725450Sbrendan for (i = 0; i < nl2cache - 1; i++) 31735450Sbrendan nvlist_free(newl2cache[i]); 31745450Sbrendan kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *)); 31755450Sbrendan 31765450Sbrendan return (0); 31775450Sbrendan } 31785450Sbrendan 31795450Sbrendan /* 31805450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 31815450Sbrendan * spares and level 2 ARC devices. 31825450Sbrendan */ 31835450Sbrendan int 31845450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 31855450Sbrendan { 31865450Sbrendan vdev_t *vd; 31875450Sbrendan nvlist_t **spares, **l2cache; 31885450Sbrendan uint_t nspares, nl2cache; 31895450Sbrendan int error = 0; 31905450Sbrendan 31915450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 31925450Sbrendan 31936643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 31945450Sbrendan 31955450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 31965450Sbrendan spa_spare_exists(guid, NULL) && 31975450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 31985450Sbrendan ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 31995450Sbrendan if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare, 32005450Sbrendan spares, nspares, vd)) != 0) 32015450Sbrendan goto out; 32025450Sbrendan spa_load_spares(spa); 32035450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 32045450Sbrendan goto out; 32055450Sbrendan } 32065450Sbrendan 32075450Sbrendan if (spa->spa_l2cache.sav_vdevs != NULL && 32085450Sbrendan spa_l2cache_exists(guid, NULL) && 32095450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 32105450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) { 32115450Sbrendan if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid, 32125450Sbrendan l2cache, nl2cache, vd)) != 0) 32135450Sbrendan goto out; 32145450Sbrendan spa_load_l2cache(spa); 32155450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 32165450Sbrendan } 32172082Seschrock 32182082Seschrock out: 32192082Seschrock spa_config_exit(spa, FTAG); 32205450Sbrendan return (error); 3221789Sahrens } 3222789Sahrens 3223789Sahrens /* 32244451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 32254451Seschrock * current spared, so we can detach it. 3226789Sahrens */ 32271544Seschrock static vdev_t * 32284451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3229789Sahrens { 32301544Seschrock vdev_t *newvd, *oldvd; 3231789Sahrens int c; 3232789Sahrens 32331544Seschrock for (c = 0; c < vd->vdev_children; c++) { 32344451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 32351544Seschrock if (oldvd != NULL) 32361544Seschrock return (oldvd); 32371544Seschrock } 3238789Sahrens 32394451Seschrock /* 32404451Seschrock * Check for a completed replacement. 32414451Seschrock */ 3242789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 32431544Seschrock oldvd = vd->vdev_child[0]; 32441544Seschrock newvd = vd->vdev_child[1]; 3245789Sahrens 32461544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 32471544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 32481544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 32491544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32501544Seschrock return (oldvd); 32511544Seschrock } 32521544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32531544Seschrock } 3254789Sahrens 32554451Seschrock /* 32564451Seschrock * Check for a completed resilver with the 'unspare' flag set. 32574451Seschrock */ 32584451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 32594451Seschrock newvd = vd->vdev_child[0]; 32604451Seschrock oldvd = vd->vdev_child[1]; 32614451Seschrock 32624451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 32634451Seschrock if (newvd->vdev_unspare && 32644451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 32654451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 32664451Seschrock newvd->vdev_unspare = 0; 32674451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32684451Seschrock return (oldvd); 32694451Seschrock } 32704451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32714451Seschrock } 32724451Seschrock 32731544Seschrock return (NULL); 3274789Sahrens } 3275789Sahrens 32761544Seschrock static void 32774451Seschrock spa_vdev_resilver_done(spa_t *spa) 3278789Sahrens { 32791544Seschrock vdev_t *vd; 32802082Seschrock vdev_t *pvd; 32811544Seschrock uint64_t guid; 32822082Seschrock uint64_t pguid = 0; 3283789Sahrens 32841544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3285789Sahrens 32864451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 32871544Seschrock guid = vd->vdev_guid; 32882082Seschrock /* 32892082Seschrock * If we have just finished replacing a hot spared device, then 32902082Seschrock * we need to detach the parent's first child (the original hot 32912082Seschrock * spare) as well. 32922082Seschrock */ 32932082Seschrock pvd = vd->vdev_parent; 32942082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 32952082Seschrock pvd->vdev_id == 0) { 32962082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 32972082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 32982082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 32992082Seschrock } 33001544Seschrock spa_config_exit(spa, FTAG); 33011544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 33021544Seschrock return; 33032082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 33042082Seschrock return; 33051544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3306789Sahrens } 3307789Sahrens 33081544Seschrock spa_config_exit(spa, FTAG); 3309789Sahrens } 3310789Sahrens 3311789Sahrens /* 33121354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 33131354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 33141354Seschrock */ 33151354Seschrock int 33161354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 33171354Seschrock { 33186643Seschrock vdev_t *vd; 33191354Seschrock uint64_t txg; 33201354Seschrock 33211354Seschrock txg = spa_vdev_enter(spa); 33221354Seschrock 33236643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 33242082Seschrock /* 33256643Seschrock * Determine if this is a reference to a hot spare device. If 33266643Seschrock * it is, update the path manually as there is no associated 33276643Seschrock * vdev_t that can be synced to disk. 33282082Seschrock */ 33296643Seschrock nvlist_t **spares; 33306643Seschrock uint_t i, nspares; 33315450Sbrendan 33325450Sbrendan if (spa->spa_spares.sav_config != NULL) { 33335450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 33345450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 33355450Sbrendan &spares, &nspares) == 0); 33362082Seschrock for (i = 0; i < nspares; i++) { 33372082Seschrock uint64_t theguid; 33382082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 33392082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 33405450Sbrendan if (theguid == guid) { 33415450Sbrendan VERIFY(nvlist_add_string(spares[i], 33425450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 33435450Sbrendan spa_load_spares(spa); 33445450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 33455450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 33465450Sbrendan 0)); 33475450Sbrendan } 33482082Seschrock } 33492082Seschrock } 33505450Sbrendan 33515450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 33522082Seschrock } 33531354Seschrock 33541585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 33551585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 33561585Sbonwick 33571354Seschrock spa_strfree(vd->vdev_path); 33581354Seschrock vd->vdev_path = spa_strdup(newpath); 33591354Seschrock 33601354Seschrock vdev_config_dirty(vd->vdev_top); 33611354Seschrock 33621354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 33631354Seschrock } 33641354Seschrock 33651354Seschrock /* 3366789Sahrens * ========================================================================== 3367789Sahrens * SPA Scrubbing 3368789Sahrens * ========================================================================== 3369789Sahrens */ 3370789Sahrens 3371789Sahrens static void 3372789Sahrens spa_scrub_io_done(zio_t *zio) 3373789Sahrens { 3374789Sahrens spa_t *spa = zio->io_spa; 3375789Sahrens 33764309Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 3377789Sahrens 3378789Sahrens mutex_enter(&spa->spa_scrub_lock); 33791544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 33801775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 3381789Sahrens spa->spa_scrub_errors++; 3382789Sahrens mutex_enter(&vd->vdev_stat_lock); 3383789Sahrens vd->vdev_stat.vs_scrub_errors++; 3384789Sahrens mutex_exit(&vd->vdev_stat_lock); 3385789Sahrens } 33863697Smishra 33873697Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 33881544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 33893697Smishra 33903697Smishra ASSERT(spa->spa_scrub_inflight >= 0); 33913697Smishra 33921544Seschrock mutex_exit(&spa->spa_scrub_lock); 3393789Sahrens } 3394789Sahrens 3395789Sahrens static void 33961544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 33971544Seschrock zbookmark_t *zb) 3398789Sahrens { 3399789Sahrens size_t size = BP_GET_LSIZE(bp); 34003697Smishra void *data; 3401789Sahrens 3402789Sahrens mutex_enter(&spa->spa_scrub_lock); 34033697Smishra /* 34043697Smishra * Do not give too much work to vdev(s). 34053697Smishra */ 34063697Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 34073697Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 34083697Smishra } 3409789Sahrens spa->spa_scrub_inflight++; 3410789Sahrens mutex_exit(&spa->spa_scrub_lock); 3411789Sahrens 34124309Smaybee data = arc_data_buf_alloc(size); 34133697Smishra 34141544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 34151544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 34161544Seschrock 34171807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 34181544Seschrock 3419789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 34201544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 3421789Sahrens } 3422789Sahrens 3423789Sahrens /* ARGSUSED */ 3424789Sahrens static int 3425789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 3426789Sahrens { 3427789Sahrens blkptr_t *bp = &bc->bc_blkptr; 34281775Sbillm vdev_t *vd = spa->spa_root_vdev; 34291775Sbillm dva_t *dva = bp->blk_dva; 34301775Sbillm int needs_resilver = B_FALSE; 34311775Sbillm int d; 3432789Sahrens 34331775Sbillm if (bc->bc_errno) { 3434789Sahrens /* 3435789Sahrens * We can't scrub this block, but we can continue to scrub 3436789Sahrens * the rest of the pool. Note the error and move along. 3437789Sahrens */ 3438789Sahrens mutex_enter(&spa->spa_scrub_lock); 3439789Sahrens spa->spa_scrub_errors++; 3440789Sahrens mutex_exit(&spa->spa_scrub_lock); 3441789Sahrens 34421775Sbillm mutex_enter(&vd->vdev_stat_lock); 34431775Sbillm vd->vdev_stat.vs_scrub_errors++; 34441775Sbillm mutex_exit(&vd->vdev_stat_lock); 3445789Sahrens 3446789Sahrens return (ERESTART); 3447789Sahrens } 3448789Sahrens 3449789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 3450789Sahrens 34511775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 34521775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 34531775Sbillm 34541775Sbillm ASSERT(vd != NULL); 34551775Sbillm 34561775Sbillm /* 34571775Sbillm * Keep track of how much data we've examined so that 34581775Sbillm * zpool(1M) status can make useful progress reports. 34591775Sbillm */ 34601775Sbillm mutex_enter(&vd->vdev_stat_lock); 34611775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 34621775Sbillm mutex_exit(&vd->vdev_stat_lock); 3463789Sahrens 34641775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 34651775Sbillm if (DVA_GET_GANG(&dva[d])) { 34661775Sbillm /* 34671775Sbillm * Gang members may be spread across multiple 34681775Sbillm * vdevs, so the best we can do is look at the 34691775Sbillm * pool-wide DTL. 34701775Sbillm * XXX -- it would be better to change our 34711775Sbillm * allocation policy to ensure that this can't 34721775Sbillm * happen. 34731775Sbillm */ 34741775Sbillm vd = spa->spa_root_vdev; 34751775Sbillm } 34761775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 34771775Sbillm bp->blk_birth, 1)) 34781775Sbillm needs_resilver = B_TRUE; 3479789Sahrens } 34801775Sbillm } 34811775Sbillm 34821775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 3483789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 34841544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 34851775Sbillm else if (needs_resilver) 34861775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 34871775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 3488789Sahrens 3489789Sahrens return (0); 3490789Sahrens } 3491789Sahrens 3492789Sahrens static void 3493789Sahrens spa_scrub_thread(spa_t *spa) 3494789Sahrens { 3495789Sahrens callb_cpr_t cprinfo; 3496789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 3497789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3498789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 3499789Sahrens int error = 0; 3500789Sahrens boolean_t complete; 3501789Sahrens 3502789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 3503789Sahrens 3504797Sbonwick /* 3505797Sbonwick * If we're restarting due to a snapshot create/delete, 3506797Sbonwick * wait for that to complete. 3507797Sbonwick */ 3508797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 3509797Sbonwick 35101544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 35111544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 35121544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 35131544Seschrock 35141544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 35151544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 3516789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 3517789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 35181544Seschrock spa_config_exit(spa, FTAG); 3519789Sahrens 3520789Sahrens mutex_enter(&spa->spa_scrub_lock); 3521789Sahrens spa->spa_scrub_errors = 0; 3522789Sahrens spa->spa_scrub_active = 1; 35231544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 3524789Sahrens 3525789Sahrens while (!spa->spa_scrub_stop) { 3526789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 35271544Seschrock while (spa->spa_scrub_suspended) { 3528789Sahrens spa->spa_scrub_active = 0; 3529789Sahrens cv_broadcast(&spa->spa_scrub_cv); 3530789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3531789Sahrens spa->spa_scrub_active = 1; 3532789Sahrens } 3533789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 3534789Sahrens 3535789Sahrens if (spa->spa_scrub_restart_txg != 0) 3536789Sahrens break; 3537789Sahrens 3538789Sahrens mutex_exit(&spa->spa_scrub_lock); 3539789Sahrens error = traverse_more(th); 3540789Sahrens mutex_enter(&spa->spa_scrub_lock); 3541789Sahrens if (error != EAGAIN) 3542789Sahrens break; 3543789Sahrens } 3544789Sahrens 3545789Sahrens while (spa->spa_scrub_inflight) 3546789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3547789Sahrens 35481601Sbonwick spa->spa_scrub_active = 0; 35491601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 35501601Sbonwick 35511601Sbonwick mutex_exit(&spa->spa_scrub_lock); 35521601Sbonwick 35531601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 35541601Sbonwick 35551601Sbonwick mutex_enter(&spa->spa_scrub_lock); 35561601Sbonwick 35571601Sbonwick /* 35581601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 35591601Sbonwick * AND the spa config lock to synchronize with any config changes 35601601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 35611601Sbonwick */ 3562789Sahrens if (spa->spa_scrub_restart_txg != 0) 3563789Sahrens error = ERESTART; 3564789Sahrens 35651544Seschrock if (spa->spa_scrub_stop) 35661544Seschrock error = EINTR; 35671544Seschrock 3568789Sahrens /* 35691544Seschrock * Even if there were uncorrectable errors, we consider the scrub 35701544Seschrock * completed. The downside is that if there is a transient error during 35711544Seschrock * a resilver, we won't resilver the data properly to the target. But 35721544Seschrock * if the damage is permanent (more likely) we will resilver forever, 35731544Seschrock * which isn't really acceptable. Since there is enough information for 35741544Seschrock * the user to know what has failed and why, this seems like a more 35751544Seschrock * tractable approach. 3576789Sahrens */ 35771544Seschrock complete = (error == 0); 3578789Sahrens 35791544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 35801544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 3581789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 3582789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 3583789Sahrens 3584789Sahrens mutex_exit(&spa->spa_scrub_lock); 3585789Sahrens 3586789Sahrens /* 3587789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 3588789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 3589789Sahrens */ 3590789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 3591789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 3592789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 35931544Seschrock spa_errlog_rotate(spa); 35941601Sbonwick 35954451Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 35964451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 35974451Seschrock 35981544Seschrock spa_config_exit(spa, FTAG); 3599789Sahrens 3600789Sahrens mutex_enter(&spa->spa_scrub_lock); 3601789Sahrens 36021544Seschrock /* 36031544Seschrock * We may have finished replacing a device. 36041544Seschrock * Let the async thread assess this and handle the detach. 36051544Seschrock */ 36064451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3607789Sahrens 3608789Sahrens /* 3609789Sahrens * If we were told to restart, our final act is to start a new scrub. 3610789Sahrens */ 3611789Sahrens if (error == ERESTART) 36121544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 36131544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 3614789Sahrens 36151544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 36161544Seschrock spa->spa_scrub_active = 0; 36171544Seschrock spa->spa_scrub_thread = NULL; 36181544Seschrock cv_broadcast(&spa->spa_scrub_cv); 3619789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 3620789Sahrens thread_exit(); 3621789Sahrens } 3622789Sahrens 3623789Sahrens void 3624789Sahrens spa_scrub_suspend(spa_t *spa) 3625789Sahrens { 3626789Sahrens mutex_enter(&spa->spa_scrub_lock); 36271544Seschrock spa->spa_scrub_suspended++; 3628789Sahrens while (spa->spa_scrub_active) { 3629789Sahrens cv_broadcast(&spa->spa_scrub_cv); 3630789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3631789Sahrens } 3632789Sahrens while (spa->spa_scrub_inflight) 3633789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3634789Sahrens mutex_exit(&spa->spa_scrub_lock); 3635789Sahrens } 3636789Sahrens 3637789Sahrens void 3638789Sahrens spa_scrub_resume(spa_t *spa) 3639789Sahrens { 3640789Sahrens mutex_enter(&spa->spa_scrub_lock); 36411544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 36421544Seschrock if (--spa->spa_scrub_suspended == 0) 3643789Sahrens cv_broadcast(&spa->spa_scrub_cv); 3644789Sahrens mutex_exit(&spa->spa_scrub_lock); 3645789Sahrens } 3646789Sahrens 3647789Sahrens void 3648789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 3649789Sahrens { 3650789Sahrens /* 3651789Sahrens * Something happened (e.g. snapshot create/delete) that means 3652789Sahrens * we must restart any in-progress scrubs. The itinerary will 3653789Sahrens * fix this properly. 3654789Sahrens */ 3655789Sahrens mutex_enter(&spa->spa_scrub_lock); 3656789Sahrens spa->spa_scrub_restart_txg = txg; 3657789Sahrens mutex_exit(&spa->spa_scrub_lock); 3658789Sahrens } 3659789Sahrens 36601544Seschrock int 36611544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 3662789Sahrens { 3663789Sahrens space_seg_t *ss; 3664789Sahrens uint64_t mintxg, maxtxg; 3665789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3666789Sahrens 36674808Sek110237 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 36684808Sek110237 ASSERT(!spa_config_held(spa, RW_WRITER)); 36694808Sek110237 3670789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3671789Sahrens return (ENOTSUP); 3672789Sahrens 36731544Seschrock mutex_enter(&spa->spa_scrub_lock); 36741544Seschrock 3675789Sahrens /* 3676789Sahrens * If there's a scrub or resilver already in progress, stop it. 3677789Sahrens */ 3678789Sahrens while (spa->spa_scrub_thread != NULL) { 3679789Sahrens /* 3680789Sahrens * Don't stop a resilver unless forced. 3681789Sahrens */ 36821544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 36831544Seschrock mutex_exit(&spa->spa_scrub_lock); 3684789Sahrens return (EBUSY); 36851544Seschrock } 3686789Sahrens spa->spa_scrub_stop = 1; 3687789Sahrens cv_broadcast(&spa->spa_scrub_cv); 3688789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3689789Sahrens } 3690789Sahrens 3691789Sahrens /* 3692789Sahrens * Terminate the previous traverse. 3693789Sahrens */ 3694789Sahrens if (spa->spa_scrub_th != NULL) { 3695789Sahrens traverse_fini(spa->spa_scrub_th); 3696789Sahrens spa->spa_scrub_th = NULL; 3697789Sahrens } 3698789Sahrens 36991544Seschrock if (rvd == NULL) { 37001544Seschrock ASSERT(spa->spa_scrub_stop == 0); 37011544Seschrock ASSERT(spa->spa_scrub_type == type); 37021544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 37031544Seschrock mutex_exit(&spa->spa_scrub_lock); 37041544Seschrock return (0); 37051544Seschrock } 3706789Sahrens 3707789Sahrens mintxg = TXG_INITIAL - 1; 3708789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 3709789Sahrens 37101544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 3711789Sahrens 37121544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 37131544Seschrock /* 37141544Seschrock * The pool-wide DTL is empty. 37151732Sbonwick * If this is a resilver, there's nothing to do except 37161732Sbonwick * check whether any in-progress replacements have completed. 37171544Seschrock */ 37181732Sbonwick if (type == POOL_SCRUB_RESILVER) { 37191544Seschrock type = POOL_SCRUB_NONE; 37204451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 37211732Sbonwick } 37221544Seschrock } else { 37231544Seschrock /* 37241544Seschrock * The pool-wide DTL is non-empty. 37251544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 37261544Seschrock */ 37271544Seschrock if (type == POOL_SCRUB_EVERYTHING) 37281544Seschrock type = POOL_SCRUB_RESILVER; 37291544Seschrock } 3730789Sahrens 37311544Seschrock if (type == POOL_SCRUB_RESILVER) { 3732789Sahrens /* 3733789Sahrens * Determine the resilvering boundaries. 3734789Sahrens * 3735789Sahrens * Note: (mintxg, maxtxg) is an open interval, 3736789Sahrens * i.e. mintxg and maxtxg themselves are not included. 3737789Sahrens * 3738789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 3739789Sahrens * so we don't claim to resilver a txg that's still changing. 3740789Sahrens */ 3741789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 37421544Seschrock mintxg = ss->ss_start - 1; 3743789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 37441544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 37454451Seschrock 37464451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 3747789Sahrens } 3748789Sahrens 37491544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 37501544Seschrock 37511544Seschrock spa->spa_scrub_stop = 0; 37521544Seschrock spa->spa_scrub_type = type; 37531544Seschrock spa->spa_scrub_restart_txg = 0; 37541544Seschrock 37551544Seschrock if (type != POOL_SCRUB_NONE) { 37561544Seschrock spa->spa_scrub_mintxg = mintxg; 3757789Sahrens spa->spa_scrub_maxtxg = maxtxg; 3758789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 37591635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 37601635Sbonwick ZIO_FLAG_CANFAIL); 3761789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 3762789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 3763789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 3764789Sahrens } 3765789Sahrens 37661544Seschrock mutex_exit(&spa->spa_scrub_lock); 37671544Seschrock 3768789Sahrens return (0); 3769789Sahrens } 3770789Sahrens 37711544Seschrock /* 37721544Seschrock * ========================================================================== 37731544Seschrock * SPA async task processing 37741544Seschrock * ========================================================================== 37751544Seschrock */ 37761544Seschrock 37771544Seschrock static void 37784451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3779789Sahrens { 37801544Seschrock vdev_t *tvd; 37811544Seschrock int c; 37821544Seschrock 37834451Seschrock for (c = 0; c < vd->vdev_children; c++) { 37844451Seschrock tvd = vd->vdev_child[c]; 37854451Seschrock if (tvd->vdev_remove_wanted) { 37864451Seschrock tvd->vdev_remove_wanted = 0; 37874451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 37884451Seschrock VDEV_AUX_NONE); 37895329Sgw25295 vdev_clear(spa, tvd, B_TRUE); 37904451Seschrock vdev_config_dirty(tvd->vdev_top); 37911544Seschrock } 37924451Seschrock spa_async_remove(spa, tvd); 37931544Seschrock } 37941544Seschrock } 37951544Seschrock 37961544Seschrock static void 37971544Seschrock spa_async_thread(spa_t *spa) 37981544Seschrock { 37991544Seschrock int tasks; 38004451Seschrock uint64_t txg; 38011544Seschrock 38021544Seschrock ASSERT(spa->spa_sync_on); 3803789Sahrens 38041544Seschrock mutex_enter(&spa->spa_async_lock); 38051544Seschrock tasks = spa->spa_async_tasks; 38061544Seschrock spa->spa_async_tasks = 0; 38071544Seschrock mutex_exit(&spa->spa_async_lock); 38081544Seschrock 38091544Seschrock /* 38101635Sbonwick * See if the config needs to be updated. 38111635Sbonwick */ 38121635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 38131635Sbonwick mutex_enter(&spa_namespace_lock); 38141635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 38151635Sbonwick mutex_exit(&spa_namespace_lock); 38161635Sbonwick } 38171635Sbonwick 38181635Sbonwick /* 38194451Seschrock * See if any devices need to be marked REMOVED. 38205329Sgw25295 * 38215329Sgw25295 * XXX - We avoid doing this when we are in 38225329Sgw25295 * I/O failure state since spa_vdev_enter() grabs 38235329Sgw25295 * the namespace lock and would not be able to obtain 38245329Sgw25295 * the writer config lock. 38251544Seschrock */ 38265329Sgw25295 if (tasks & SPA_ASYNC_REMOVE && 38275329Sgw25295 spa_state(spa) != POOL_STATE_IO_FAILURE) { 38284451Seschrock txg = spa_vdev_enter(spa); 38294451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 38304451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 38314451Seschrock } 38321544Seschrock 38331544Seschrock /* 38341544Seschrock * If any devices are done replacing, detach them. 38351544Seschrock */ 38364451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 38374451Seschrock spa_vdev_resilver_done(spa); 3838789Sahrens 38391544Seschrock /* 38404451Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 38414451Seschrock * scrub which can become a resilver), we need to hold 38424451Seschrock * spa_namespace_lock() because the sysevent we post via 38434451Seschrock * spa_event_notify() needs to get the name of the pool. 38441544Seschrock */ 38454451Seschrock if (tasks & SPA_ASYNC_SCRUB) { 38464451Seschrock mutex_enter(&spa_namespace_lock); 38471544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 38484451Seschrock mutex_exit(&spa_namespace_lock); 38494451Seschrock } 38501544Seschrock 38511544Seschrock /* 38521544Seschrock * Kick off a resilver. 38531544Seschrock */ 38544451Seschrock if (tasks & SPA_ASYNC_RESILVER) { 38554451Seschrock mutex_enter(&spa_namespace_lock); 38561544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 38574451Seschrock mutex_exit(&spa_namespace_lock); 38584451Seschrock } 38591544Seschrock 38601544Seschrock /* 38611544Seschrock * Let the world know that we're done. 38621544Seschrock */ 38631544Seschrock mutex_enter(&spa->spa_async_lock); 38641544Seschrock spa->spa_async_thread = NULL; 38651544Seschrock cv_broadcast(&spa->spa_async_cv); 38661544Seschrock mutex_exit(&spa->spa_async_lock); 38671544Seschrock thread_exit(); 38681544Seschrock } 38691544Seschrock 38701544Seschrock void 38711544Seschrock spa_async_suspend(spa_t *spa) 38721544Seschrock { 38731544Seschrock mutex_enter(&spa->spa_async_lock); 38741544Seschrock spa->spa_async_suspended++; 38751544Seschrock while (spa->spa_async_thread != NULL) 38761544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 38771544Seschrock mutex_exit(&spa->spa_async_lock); 38781544Seschrock } 38791544Seschrock 38801544Seschrock void 38811544Seschrock spa_async_resume(spa_t *spa) 38821544Seschrock { 38831544Seschrock mutex_enter(&spa->spa_async_lock); 38841544Seschrock ASSERT(spa->spa_async_suspended != 0); 38851544Seschrock spa->spa_async_suspended--; 38861544Seschrock mutex_exit(&spa->spa_async_lock); 38871544Seschrock } 38881544Seschrock 38891544Seschrock static void 38901544Seschrock spa_async_dispatch(spa_t *spa) 38911544Seschrock { 38921544Seschrock mutex_enter(&spa->spa_async_lock); 38931544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 38941635Sbonwick spa->spa_async_thread == NULL && 38951635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 38961544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 38971544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 38981544Seschrock mutex_exit(&spa->spa_async_lock); 38991544Seschrock } 39001544Seschrock 39011544Seschrock void 39021544Seschrock spa_async_request(spa_t *spa, int task) 39031544Seschrock { 39041544Seschrock mutex_enter(&spa->spa_async_lock); 39051544Seschrock spa->spa_async_tasks |= task; 39061544Seschrock mutex_exit(&spa->spa_async_lock); 3907789Sahrens } 3908789Sahrens 3909789Sahrens /* 3910789Sahrens * ========================================================================== 3911789Sahrens * SPA syncing routines 3912789Sahrens * ========================================================================== 3913789Sahrens */ 3914789Sahrens 3915789Sahrens static void 3916789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3917789Sahrens { 3918789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3919789Sahrens dmu_tx_t *tx; 3920789Sahrens blkptr_t blk; 3921789Sahrens uint64_t itor = 0; 3922789Sahrens zio_t *zio; 3923789Sahrens int error; 3924789Sahrens uint8_t c = 1; 3925789Sahrens 3926789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3927789Sahrens 3928789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3929789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3930789Sahrens 3931789Sahrens error = zio_wait(zio); 3932789Sahrens ASSERT3U(error, ==, 0); 3933789Sahrens 3934789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3935789Sahrens bplist_vacate(bpl, tx); 3936789Sahrens 3937789Sahrens /* 3938789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3939789Sahrens * (Usually only the first block is needed.) 3940789Sahrens */ 3941789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3942789Sahrens dmu_tx_commit(tx); 3943789Sahrens } 3944789Sahrens 3945789Sahrens static void 39462082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 39472082Seschrock { 39482082Seschrock char *packed = NULL; 39492082Seschrock size_t nvsize = 0; 39502082Seschrock dmu_buf_t *db; 39512082Seschrock 39522082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 39532082Seschrock 39542082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 39552082Seschrock 39562082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 39572082Seschrock KM_SLEEP) == 0); 39582082Seschrock 39592082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 39602082Seschrock 39612082Seschrock kmem_free(packed, nvsize); 39622082Seschrock 39632082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 39642082Seschrock dmu_buf_will_dirty(db, tx); 39652082Seschrock *(uint64_t *)db->db_data = nvsize; 39662082Seschrock dmu_buf_rele(db, FTAG); 39672082Seschrock } 39682082Seschrock 39692082Seschrock static void 39705450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 39715450Sbrendan const char *config, const char *entry) 39722082Seschrock { 39732082Seschrock nvlist_t *nvroot; 39745450Sbrendan nvlist_t **list; 39752082Seschrock int i; 39762082Seschrock 39775450Sbrendan if (!sav->sav_sync) 39782082Seschrock return; 39792082Seschrock 39802082Seschrock /* 39815450Sbrendan * Update the MOS nvlist describing the list of available devices. 39825450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 39834451Seschrock * valid and the vdevs are labeled appropriately. 39842082Seschrock */ 39855450Sbrendan if (sav->sav_object == 0) { 39865450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 39875450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 39885450Sbrendan sizeof (uint64_t), tx); 39892082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 39905450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 39915450Sbrendan &sav->sav_object, tx) == 0); 39922082Seschrock } 39932082Seschrock 39942082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 39955450Sbrendan if (sav->sav_count == 0) { 39965450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 39972082Seschrock } else { 39985450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 39995450Sbrendan for (i = 0; i < sav->sav_count; i++) 40005450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 40015450Sbrendan B_FALSE, B_FALSE, B_TRUE); 40025450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 40035450Sbrendan sav->sav_count) == 0); 40045450Sbrendan for (i = 0; i < sav->sav_count; i++) 40055450Sbrendan nvlist_free(list[i]); 40065450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 40072082Seschrock } 40082082Seschrock 40095450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 40102926Sek110237 nvlist_free(nvroot); 40112082Seschrock 40125450Sbrendan sav->sav_sync = B_FALSE; 40132082Seschrock } 40142082Seschrock 40152082Seschrock static void 4016789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 4017789Sahrens { 4018789Sahrens nvlist_t *config; 4019789Sahrens 4020789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 4021789Sahrens return; 4022789Sahrens 4023789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 4024789Sahrens 40251635Sbonwick if (spa->spa_config_syncing) 40261635Sbonwick nvlist_free(spa->spa_config_syncing); 40271635Sbonwick spa->spa_config_syncing = config; 4028789Sahrens 40292082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 4030789Sahrens } 4031789Sahrens 40325094Slling /* 40335094Slling * Set zpool properties. 40345094Slling */ 40353912Slling static void 40364543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 40373912Slling { 40383912Slling spa_t *spa = arg1; 40395094Slling objset_t *mos = spa->spa_meta_objset; 40403912Slling nvlist_t *nvp = arg2; 40415094Slling nvpair_t *elem; 40424451Seschrock uint64_t intval; 40436643Seschrock char *strval; 40445094Slling zpool_prop_t prop; 40455094Slling const char *propname; 40465094Slling zprop_type_t proptype; 40476643Seschrock spa_config_dirent_t *dp; 40485094Slling 40495094Slling elem = NULL; 40505094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 40515094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 40525094Slling case ZPOOL_PROP_VERSION: 40535094Slling /* 40545094Slling * Only set version for non-zpool-creation cases 40555094Slling * (set/import). spa_create() needs special care 40565094Slling * for version setting. 40575094Slling */ 40585094Slling if (tx->tx_txg != TXG_INITIAL) { 40595094Slling VERIFY(nvpair_value_uint64(elem, 40605094Slling &intval) == 0); 40615094Slling ASSERT(intval <= SPA_VERSION); 40625094Slling ASSERT(intval >= spa_version(spa)); 40635094Slling spa->spa_uberblock.ub_version = intval; 40645094Slling vdev_config_dirty(spa->spa_root_vdev); 40655094Slling } 40665094Slling break; 40675094Slling 40685094Slling case ZPOOL_PROP_ALTROOT: 40695094Slling /* 40705094Slling * 'altroot' is a non-persistent property. It should 40715094Slling * have been set temporarily at creation or import time. 40725094Slling */ 40735094Slling ASSERT(spa->spa_root != NULL); 40745094Slling break; 40755094Slling 40765363Seschrock case ZPOOL_PROP_CACHEFILE: 40775094Slling /* 40785363Seschrock * 'cachefile' is a non-persistent property, but note 40795363Seschrock * an async request that the config cache needs to be 40805363Seschrock * udpated. 40815094Slling */ 40825363Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 40836643Seschrock 40846643Seschrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 40856643Seschrock KM_SLEEP); 40866643Seschrock 40876643Seschrock if (strval[0] == '\0') 40886643Seschrock dp->scd_path = spa_strdup(spa_config_path); 40896643Seschrock else if (strcmp(strval, "none") == 0) 40906643Seschrock dp->scd_path = NULL; 40916643Seschrock else 40926643Seschrock dp->scd_path = spa_strdup(strval); 40936643Seschrock 40946643Seschrock list_insert_head(&spa->spa_config_list, dp); 40955363Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 40964543Smarks break; 40975094Slling default: 40985094Slling /* 40995094Slling * Set pool property values in the poolprops mos object. 41005094Slling */ 41015094Slling mutex_enter(&spa->spa_props_lock); 41025094Slling if (spa->spa_pool_props_object == 0) { 41035094Slling objset_t *mos = spa->spa_meta_objset; 41045094Slling 41055094Slling VERIFY((spa->spa_pool_props_object = 41065094Slling zap_create(mos, DMU_OT_POOL_PROPS, 41075094Slling DMU_OT_NONE, 0, tx)) > 0); 41085094Slling 41095094Slling VERIFY(zap_update(mos, 41105094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 41115094Slling 8, 1, &spa->spa_pool_props_object, tx) 41125094Slling == 0); 41135094Slling } 41145094Slling mutex_exit(&spa->spa_props_lock); 41155094Slling 41165094Slling /* normalize the property name */ 41175094Slling propname = zpool_prop_to_name(prop); 41185094Slling proptype = zpool_prop_get_type(prop); 41195094Slling 41205094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 41215094Slling ASSERT(proptype == PROP_TYPE_STRING); 41225094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 41235094Slling VERIFY(zap_update(mos, 41245094Slling spa->spa_pool_props_object, propname, 41255094Slling 1, strlen(strval) + 1, strval, tx) == 0); 41265094Slling 41275094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 41285094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 41295094Slling 41305094Slling if (proptype == PROP_TYPE_INDEX) { 41315094Slling const char *unused; 41325094Slling VERIFY(zpool_prop_index_to_string( 41335094Slling prop, intval, &unused) == 0); 41345094Slling } 41355094Slling VERIFY(zap_update(mos, 41365094Slling spa->spa_pool_props_object, propname, 41375094Slling 8, 1, &intval, tx) == 0); 41385094Slling } else { 41395094Slling ASSERT(0); /* not allowed */ 41405094Slling } 41415094Slling 41425329Sgw25295 switch (prop) { 41435329Sgw25295 case ZPOOL_PROP_DELEGATION: 41445094Slling spa->spa_delegation = intval; 41455329Sgw25295 break; 41465329Sgw25295 case ZPOOL_PROP_BOOTFS: 41475094Slling spa->spa_bootfs = intval; 41485329Sgw25295 break; 41495329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 41505329Sgw25295 spa->spa_failmode = intval; 41515329Sgw25295 break; 41525329Sgw25295 default: 41535329Sgw25295 break; 41545329Sgw25295 } 41553912Slling } 41565094Slling 41575094Slling /* log internal history if this is not a zpool create */ 41585094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 41595094Slling tx->tx_txg != TXG_INITIAL) { 41605094Slling spa_history_internal_log(LOG_POOL_PROPSET, 41615094Slling spa, tx, cr, "%s %lld %s", 41625094Slling nvpair_name(elem), intval, spa->spa_name); 41635094Slling } 41643912Slling } 41653912Slling } 41663912Slling 4167789Sahrens /* 4168789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4169789Sahrens * part of the process, so we iterate until it converges. 4170789Sahrens */ 4171789Sahrens void 4172789Sahrens spa_sync(spa_t *spa, uint64_t txg) 4173789Sahrens { 4174789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4175789Sahrens objset_t *mos = spa->spa_meta_objset; 4176789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 41771635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 4178789Sahrens vdev_t *vd; 4179789Sahrens dmu_tx_t *tx; 4180789Sahrens int dirty_vdevs; 4181789Sahrens 4182789Sahrens /* 4183789Sahrens * Lock out configuration changes. 4184789Sahrens */ 41851544Seschrock spa_config_enter(spa, RW_READER, FTAG); 4186789Sahrens 4187789Sahrens spa->spa_syncing_txg = txg; 4188789Sahrens spa->spa_sync_pass = 0; 4189789Sahrens 41901544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4191789Sahrens 41922082Seschrock tx = dmu_tx_create_assigned(dp, txg); 41932082Seschrock 41942082Seschrock /* 41954577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 41962082Seschrock * set spa_deflate if we have no raid-z vdevs. 41972082Seschrock */ 41984577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 41994577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 42002082Seschrock int i; 42012082Seschrock 42022082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 42032082Seschrock vd = rvd->vdev_child[i]; 42042082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 42052082Seschrock break; 42062082Seschrock } 42072082Seschrock if (i == rvd->vdev_children) { 42082082Seschrock spa->spa_deflate = TRUE; 42092082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 42102082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 42112082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 42122082Seschrock } 42132082Seschrock } 42142082Seschrock 4215789Sahrens /* 4216789Sahrens * If anything has changed in this txg, push the deferred frees 4217789Sahrens * from the previous txg. If not, leave them alone so that we 4218789Sahrens * don't generate work on an otherwise idle system. 4219789Sahrens */ 4220789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 42212329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 42222329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 4223789Sahrens spa_sync_deferred_frees(spa, txg); 4224789Sahrens 4225789Sahrens /* 4226789Sahrens * Iterate to convergence. 4227789Sahrens */ 4228789Sahrens do { 4229789Sahrens spa->spa_sync_pass++; 4230789Sahrens 4231789Sahrens spa_sync_config_object(spa, tx); 42325450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 42335450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 42345450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 42355450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 42361544Seschrock spa_errlog_sync(spa, txg); 4237789Sahrens dsl_pool_sync(dp, txg); 4238789Sahrens 4239789Sahrens dirty_vdevs = 0; 4240789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4241789Sahrens vdev_sync(vd, txg); 4242789Sahrens dirty_vdevs++; 4243789Sahrens } 4244789Sahrens 4245789Sahrens bplist_sync(bpl, tx); 4246789Sahrens } while (dirty_vdevs); 4247789Sahrens 4248789Sahrens bplist_close(bpl); 4249789Sahrens 4250789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4251789Sahrens 4252789Sahrens /* 4253789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4254789Sahrens * to commit the transaction group. 42551635Sbonwick * 42565688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 42575688Sbonwick * random top-level vdevs that are known to be visible in the 42585688Sbonwick * config cache (see spa_vdev_add() for details). If there *are* 42595688Sbonwick * dirty vdevs -- or if the sync to our random subset fails -- 42605688Sbonwick * then sync the uberblock to all vdevs. 4261789Sahrens */ 42625688Sbonwick if (list_is_empty(&spa->spa_dirty_list)) { 42636615Sgw25295 vdev_t *svd[SPA_DVAS_PER_BP]; 42646615Sgw25295 int svdcount = 0; 42651635Sbonwick int children = rvd->vdev_children; 42661635Sbonwick int c0 = spa_get_random(children); 42671635Sbonwick int c; 42681635Sbonwick 42691635Sbonwick for (c = 0; c < children; c++) { 42701635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 42715688Sbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 42721635Sbonwick continue; 42735688Sbonwick svd[svdcount++] = vd; 42745688Sbonwick if (svdcount == SPA_DVAS_PER_BP) 42751635Sbonwick break; 42761635Sbonwick } 42776615Sgw25295 vdev_config_sync(svd, svdcount, txg); 42786615Sgw25295 } else { 42796615Sgw25295 vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg); 42801635Sbonwick } 42812082Seschrock dmu_tx_commit(tx); 42822082Seschrock 42831635Sbonwick /* 42841635Sbonwick * Clear the dirty config list. 42851635Sbonwick */ 42861635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 42871635Sbonwick vdev_config_clean(vd); 42881635Sbonwick 42891635Sbonwick /* 42901635Sbonwick * Now that the new config has synced transactionally, 42911635Sbonwick * let it become visible to the config cache. 42921635Sbonwick */ 42931635Sbonwick if (spa->spa_config_syncing != NULL) { 42941635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 42951635Sbonwick spa->spa_config_txg = txg; 42961635Sbonwick spa->spa_config_syncing = NULL; 42971635Sbonwick } 4298789Sahrens 4299789Sahrens /* 4300789Sahrens * Make a stable copy of the fully synced uberblock. 4301789Sahrens * We use this as the root for pool traversals. 4302789Sahrens */ 4303789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 4304789Sahrens 4305789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 4306789Sahrens 4307789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 4308789Sahrens spa->spa_traverse_wanted = 0; 4309789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4310789Sahrens rw_exit(&spa->spa_traverse_lock); 4311789Sahrens 4312789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 4313789Sahrens 4314789Sahrens /* 4315789Sahrens * Clean up the ZIL records for the synced txg. 4316789Sahrens */ 4317789Sahrens dsl_pool_zil_clean(dp); 4318789Sahrens 4319789Sahrens /* 4320789Sahrens * Update usable space statistics. 4321789Sahrens */ 4322789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4323789Sahrens vdev_sync_done(vd, txg); 4324789Sahrens 4325789Sahrens /* 4326789Sahrens * It had better be the case that we didn't dirty anything 43272082Seschrock * since vdev_config_sync(). 4328789Sahrens */ 4329789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4330789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4331789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4332789Sahrens ASSERT(bpl->bpl_queue == NULL); 4333789Sahrens 43341544Seschrock spa_config_exit(spa, FTAG); 43351544Seschrock 43361544Seschrock /* 43371544Seschrock * If any async tasks have been requested, kick them off. 43381544Seschrock */ 43391544Seschrock spa_async_dispatch(spa); 4340789Sahrens } 4341789Sahrens 4342789Sahrens /* 4343789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4344789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4345789Sahrens * sync. 4346789Sahrens */ 4347789Sahrens void 4348789Sahrens spa_sync_allpools(void) 4349789Sahrens { 4350789Sahrens spa_t *spa = NULL; 4351789Sahrens mutex_enter(&spa_namespace_lock); 4352789Sahrens while ((spa = spa_next(spa)) != NULL) { 4353789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 4354789Sahrens continue; 4355789Sahrens spa_open_ref(spa, FTAG); 4356789Sahrens mutex_exit(&spa_namespace_lock); 4357789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4358789Sahrens mutex_enter(&spa_namespace_lock); 4359789Sahrens spa_close(spa, FTAG); 4360789Sahrens } 4361789Sahrens mutex_exit(&spa_namespace_lock); 4362789Sahrens } 4363789Sahrens 4364789Sahrens /* 4365789Sahrens * ========================================================================== 4366789Sahrens * Miscellaneous routines 4367789Sahrens * ========================================================================== 4368789Sahrens */ 4369789Sahrens 4370789Sahrens /* 4371789Sahrens * Remove all pools in the system. 4372789Sahrens */ 4373789Sahrens void 4374789Sahrens spa_evict_all(void) 4375789Sahrens { 4376789Sahrens spa_t *spa; 4377789Sahrens 4378789Sahrens /* 4379789Sahrens * Remove all cached state. All pools should be closed now, 4380789Sahrens * so every spa in the AVL tree should be unreferenced. 4381789Sahrens */ 4382789Sahrens mutex_enter(&spa_namespace_lock); 4383789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4384789Sahrens /* 43851544Seschrock * Stop async tasks. The async thread may need to detach 43861544Seschrock * a device that's been replaced, which requires grabbing 43871544Seschrock * spa_namespace_lock, so we must drop it here. 4388789Sahrens */ 4389789Sahrens spa_open_ref(spa, FTAG); 4390789Sahrens mutex_exit(&spa_namespace_lock); 43911544Seschrock spa_async_suspend(spa); 43924808Sek110237 mutex_enter(&spa_namespace_lock); 4393789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 4394789Sahrens spa_close(spa, FTAG); 4395789Sahrens 4396789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4397789Sahrens spa_unload(spa); 4398789Sahrens spa_deactivate(spa); 4399789Sahrens } 4400789Sahrens spa_remove(spa); 4401789Sahrens } 4402789Sahrens mutex_exit(&spa_namespace_lock); 4403789Sahrens } 44041544Seschrock 44051544Seschrock vdev_t * 44066643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 44071544Seschrock { 44086643Seschrock vdev_t *vd; 44096643Seschrock int i; 44106643Seschrock 44116643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 44126643Seschrock return (vd); 44136643Seschrock 44146643Seschrock if (l2cache) { 44156643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 44166643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 44176643Seschrock if (vd->vdev_guid == guid) 44186643Seschrock return (vd); 44196643Seschrock } 44206643Seschrock } 44216643Seschrock 44226643Seschrock return (NULL); 44231544Seschrock } 44241760Seschrock 44251760Seschrock void 44265094Slling spa_upgrade(spa_t *spa, uint64_t version) 44271760Seschrock { 44281760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 44291760Seschrock 44301760Seschrock /* 44311760Seschrock * This should only be called for a non-faulted pool, and since a 44321760Seschrock * future version would result in an unopenable pool, this shouldn't be 44331760Seschrock * possible. 44341760Seschrock */ 44354577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 44365094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 44375094Slling 44385094Slling spa->spa_uberblock.ub_version = version; 44391760Seschrock vdev_config_dirty(spa->spa_root_vdev); 44401760Seschrock 44411760Seschrock spa_config_exit(spa, FTAG); 44422082Seschrock 44432082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 44441760Seschrock } 44452082Seschrock 44462082Seschrock boolean_t 44472082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 44482082Seschrock { 44492082Seschrock int i; 44503377Seschrock uint64_t spareguid; 44515450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 44525450Sbrendan 44535450Sbrendan for (i = 0; i < sav->sav_count; i++) 44545450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 44552082Seschrock return (B_TRUE); 44562082Seschrock 44575450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 44585450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 44595450Sbrendan &spareguid) == 0 && spareguid == guid) 44603377Seschrock return (B_TRUE); 44613377Seschrock } 44623377Seschrock 44632082Seschrock return (B_FALSE); 44642082Seschrock } 44653912Slling 44664451Seschrock /* 44674451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 44684451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 44694451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 44704451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 44714451Seschrock * or zdb as real changes. 44724451Seschrock */ 44734451Seschrock void 44744451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 44754451Seschrock { 44764451Seschrock #ifdef _KERNEL 44774451Seschrock sysevent_t *ev; 44784451Seschrock sysevent_attr_list_t *attr = NULL; 44794451Seschrock sysevent_value_t value; 44804451Seschrock sysevent_id_t eid; 44814451Seschrock 44824451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 44834451Seschrock SE_SLEEP); 44844451Seschrock 44854451Seschrock value.value_type = SE_DATA_TYPE_STRING; 44864451Seschrock value.value.sv_string = spa_name(spa); 44874451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 44884451Seschrock goto done; 44894451Seschrock 44904451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 44914451Seschrock value.value.sv_uint64 = spa_guid(spa); 44924451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 44934451Seschrock goto done; 44944451Seschrock 44954451Seschrock if (vd) { 44964451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 44974451Seschrock value.value.sv_uint64 = vd->vdev_guid; 44984451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 44994451Seschrock SE_SLEEP) != 0) 45004451Seschrock goto done; 45014451Seschrock 45024451Seschrock if (vd->vdev_path) { 45034451Seschrock value.value_type = SE_DATA_TYPE_STRING; 45044451Seschrock value.value.sv_string = vd->vdev_path; 45054451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 45064451Seschrock &value, SE_SLEEP) != 0) 45074451Seschrock goto done; 45084451Seschrock } 45094451Seschrock } 45104451Seschrock 45115756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 45125756Seschrock goto done; 45135756Seschrock attr = NULL; 45145756Seschrock 45154451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 45164451Seschrock 45174451Seschrock done: 45184451Seschrock if (attr) 45194451Seschrock sysevent_free_attr(attr); 45204451Seschrock sysevent_free(ev); 45214451Seschrock #endif 45224451Seschrock } 4523