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; 110*6643Seschrock 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 141*6643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 142*6643Seschrock if (dp->scd_path == NULL) { 1435949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 144*6643Seschrock "none", 0, ZPROP_SRC_LOCAL); 145*6643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 1465949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 147*6643Seschrock 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 */ 692*6643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 693*6643Seschrock 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 733*6643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 734*6643Seschrock 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; 796*6643Seschrock 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 848*6643Seschrock vd->vdev_top = vd; 849*6643Seschrock vd->vdev_aux = sav; 850*6643Seschrock 851*6643Seschrock spa_l2cache_activate(vd); 852*6643Seschrock 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); 860*6643Seschrock l2arc_add_vdev(spa, vd, 861*6643Seschrock VDEV_LABEL_START_SIZE, 862*6643Seschrock 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) && 877*6643Seschrock pool != 0ULL && 878*6643Seschrock 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); 1428*6643Seschrock 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 && 1822*6643Seschrock spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL && 1823*6643Seschrock 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); 1874*6643Seschrock 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 2017*6643Seschrock 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, 2033*6643Seschrock boolean_t isroot, boolean_t allowfaulted) 2034789Sahrens { 2035789Sahrens spa_t *spa; 20365094Slling char *altroot = NULL; 2037*6643Seschrock 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 2060*6643Seschrock if (allowfaulted) 2061*6643Seschrock spa->spa_import_faulted = B_TRUE; 2062*6643Seschrock 2063789Sahrens /* 20641635Sbonwick * Pass off the heavy lifting to spa_load(). 20651732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 20661732Sbonwick * is actually the one to trust when doing an import. 20671601Sbonwick */ 2068*6643Seschrock loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, mosconfig); 2069789Sahrens 20702082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 20712082Seschrock /* 20722082Seschrock * Toss any existing sparelist, as it doesn't have any validity anymore, 20732082Seschrock * and conflicts with spa_has_spare(). 20742082Seschrock */ 20756423Sgw25295 if (!isroot && spa->spa_spares.sav_config) { 20765450Sbrendan nvlist_free(spa->spa_spares.sav_config); 20775450Sbrendan spa->spa_spares.sav_config = NULL; 20782082Seschrock spa_load_spares(spa); 20792082Seschrock } 20806423Sgw25295 if (!isroot && spa->spa_l2cache.sav_config) { 20815450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 20825450Sbrendan spa->spa_l2cache.sav_config = NULL; 20835450Sbrendan spa_load_l2cache(spa); 20845450Sbrendan } 20852082Seschrock 20862082Seschrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 20872082Seschrock &nvroot) == 0); 20885450Sbrendan if (error == 0) 20895450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE); 20905450Sbrendan if (error == 0) 20915450Sbrendan error = spa_validate_aux(spa, nvroot, -1ULL, 20925450Sbrendan VDEV_ALLOC_L2CACHE); 20932082Seschrock spa_config_exit(spa, FTAG); 20942082Seschrock 20955094Slling if (error != 0 || (props && (error = spa_prop_set(spa, props)))) { 2096*6643Seschrock if (loaderr != 0 && loaderr != EINVAL && allowfaulted) { 2097*6643Seschrock /* 2098*6643Seschrock * If we failed to load the pool, but 'allowfaulted' is 2099*6643Seschrock * set, then manually set the config as if the config 2100*6643Seschrock * passed in was specified in the cache file. 2101*6643Seschrock */ 2102*6643Seschrock error = 0; 2103*6643Seschrock spa->spa_import_faulted = B_FALSE; 2104*6643Seschrock if (spa->spa_config == NULL) { 2105*6643Seschrock spa_config_enter(spa, RW_READER, FTAG); 2106*6643Seschrock spa->spa_config = spa_config_generate(spa, 2107*6643Seschrock NULL, -1ULL, B_TRUE); 2108*6643Seschrock spa_config_exit(spa, FTAG); 2109*6643Seschrock } 2110*6643Seschrock spa_unload(spa); 2111*6643Seschrock spa_deactivate(spa); 2112*6643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 2113*6643Seschrock } else { 2114*6643Seschrock spa_unload(spa); 2115*6643Seschrock spa_deactivate(spa); 2116*6643Seschrock spa_remove(spa); 2117*6643Seschrock } 2118789Sahrens mutex_exit(&spa_namespace_lock); 2119789Sahrens return (error); 2120789Sahrens } 2121789Sahrens 21221635Sbonwick /* 21235450Sbrendan * Override any spares and level 2 cache devices as specified by 21245450Sbrendan * the user, as these may have correct device names/devids, etc. 21252082Seschrock */ 21262082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 21272082Seschrock &spares, &nspares) == 0) { 21285450Sbrendan if (spa->spa_spares.sav_config) 21295450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, 21302082Seschrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 21312082Seschrock else 21325450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 21332082Seschrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 21345450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 21352082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 21362082Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 21372082Seschrock spa_load_spares(spa); 21382082Seschrock spa_config_exit(spa, FTAG); 21395450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 21405450Sbrendan } 21415450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 21425450Sbrendan &l2cache, &nl2cache) == 0) { 21435450Sbrendan if (spa->spa_l2cache.sav_config) 21445450Sbrendan VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 21455450Sbrendan ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 21465450Sbrendan else 21475450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 21485450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 21495450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 21505450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 21515450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 21525450Sbrendan spa_load_l2cache(spa); 21535450Sbrendan spa_config_exit(spa, FTAG); 21545450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 21552082Seschrock } 21562082Seschrock 2157*6643Seschrock if (spa_mode & FWRITE) { 2158*6643Seschrock /* 2159*6643Seschrock * Update the config cache to include the newly-imported pool. 2160*6643Seschrock */ 21616423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot); 21621635Sbonwick 2163*6643Seschrock /* 2164*6643Seschrock * Resilver anything that's out of date. 2165*6643Seschrock */ 2166*6643Seschrock if (!isroot) 2167*6643Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, 2168*6643Seschrock B_TRUE) == 0); 2169*6643Seschrock } 2170*6643Seschrock 2171*6643Seschrock spa->spa_import_faulted = B_FALSE; 21724451Seschrock mutex_exit(&spa_namespace_lock); 21734451Seschrock 2174789Sahrens return (0); 2175789Sahrens } 2176789Sahrens 21776423Sgw25295 #ifdef _KERNEL 21786423Sgw25295 /* 21796423Sgw25295 * Build a "root" vdev for a top level vdev read in from a rootpool 21806423Sgw25295 * device label. 21816423Sgw25295 */ 21826423Sgw25295 static void 21836423Sgw25295 spa_build_rootpool_config(nvlist_t *config) 21846423Sgw25295 { 21856423Sgw25295 nvlist_t *nvtop, *nvroot; 21866423Sgw25295 uint64_t pgid; 21876423Sgw25295 21886423Sgw25295 /* 21896423Sgw25295 * Add this top-level vdev to the child array. 21906423Sgw25295 */ 21916423Sgw25295 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop) 21926423Sgw25295 == 0); 21936423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid) 21946423Sgw25295 == 0); 21956423Sgw25295 21966423Sgw25295 /* 21976423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 21986423Sgw25295 */ 21996423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 22006423Sgw25295 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) 22016423Sgw25295 == 0); 22026423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 22036423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 22046423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 22056423Sgw25295 &nvtop, 1) == 0); 22066423Sgw25295 22076423Sgw25295 /* 22086423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 22096423Sgw25295 * this pool's configuration (remove the old, add the new). 22106423Sgw25295 */ 22116423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 22126423Sgw25295 nvlist_free(nvroot); 22136423Sgw25295 } 22146423Sgw25295 22156423Sgw25295 /* 22166423Sgw25295 * Get the root pool information from the root disk, then import the root pool 22176423Sgw25295 * during the system boot up time. 22186423Sgw25295 */ 22196423Sgw25295 extern nvlist_t *vdev_disk_read_rootlabel(char *); 22206423Sgw25295 22216423Sgw25295 void 22226423Sgw25295 spa_check_rootconf(char *devpath, char **bestdev, nvlist_t **bestconf, 22236423Sgw25295 uint64_t *besttxg) 22246423Sgw25295 { 22256423Sgw25295 nvlist_t *config; 22266423Sgw25295 uint64_t txg; 22276423Sgw25295 22286423Sgw25295 if ((config = vdev_disk_read_rootlabel(devpath)) == NULL) 22296423Sgw25295 return; 22306423Sgw25295 22316423Sgw25295 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 22326423Sgw25295 22336423Sgw25295 if (txg > *besttxg) { 22346423Sgw25295 *besttxg = txg; 22356423Sgw25295 if (*bestconf != NULL) 22366423Sgw25295 nvlist_free(*bestconf); 22376423Sgw25295 *bestconf = config; 22386423Sgw25295 *bestdev = devpath; 22396423Sgw25295 } 22406423Sgw25295 } 22416423Sgw25295 22426423Sgw25295 boolean_t 22436423Sgw25295 spa_rootdev_validate(nvlist_t *nv) 22446423Sgw25295 { 22456423Sgw25295 uint64_t ival; 22466423Sgw25295 22476423Sgw25295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 22486423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 22496423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, &ival) == 0 || 22506423Sgw25295 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 22516423Sgw25295 return (B_FALSE); 22526423Sgw25295 22536423Sgw25295 return (B_TRUE); 22546423Sgw25295 } 22556423Sgw25295 22566423Sgw25295 /* 22576423Sgw25295 * Import a root pool. 22586423Sgw25295 * 22596423Sgw25295 * For x86. devpath_list will consist the physpath name of the vdev in a single 22606423Sgw25295 * disk root pool or a list of physnames for the vdevs in a mirrored rootpool. 22616423Sgw25295 * e.g. 22626423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a /pci@1f,o/ide@d/disk@2,0:a" 22636423Sgw25295 * 22646423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 22656423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 22666423Sgw25295 * e.g. 22676423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 22686423Sgw25295 */ 22696423Sgw25295 int 22706423Sgw25295 spa_import_rootpool(char *devpath_list) 22716423Sgw25295 { 22726423Sgw25295 nvlist_t *conf = NULL; 22736423Sgw25295 char *dev = NULL; 22746423Sgw25295 char *pname; 22756423Sgw25295 int error; 22766423Sgw25295 22776423Sgw25295 /* 22786423Sgw25295 * Get the vdev pathname and configuation from the most 22796423Sgw25295 * recently updated vdev (highest txg). 22806423Sgw25295 */ 22816423Sgw25295 if (error = spa_get_rootconf(devpath_list, &dev, &conf)) 22826423Sgw25295 goto msg_out; 22836423Sgw25295 22846423Sgw25295 /* 22856423Sgw25295 * Add type "root" vdev to the config. 22866423Sgw25295 */ 22876423Sgw25295 spa_build_rootpool_config(conf); 22886423Sgw25295 22896423Sgw25295 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0); 22906423Sgw25295 2291*6643Seschrock error = spa_import_common(pname, conf, NULL, B_TRUE, B_FALSE); 22926423Sgw25295 if (error == EEXIST) 22936423Sgw25295 error = 0; 22946423Sgw25295 22956423Sgw25295 nvlist_free(conf); 22966423Sgw25295 return (error); 22976423Sgw25295 22986423Sgw25295 msg_out: 22996423Sgw25295 cmn_err(CE_NOTE, "\n\n" 23006423Sgw25295 " *************************************************** \n" 23016423Sgw25295 " * This device is not bootable! * \n" 23026423Sgw25295 " * It is either offlined or detached or faulted. * \n" 23036423Sgw25295 " * Please try to boot from a different device. * \n" 23046423Sgw25295 " *************************************************** \n\n"); 23056423Sgw25295 23066423Sgw25295 return (error); 23076423Sgw25295 } 23086423Sgw25295 #endif 23096423Sgw25295 23106423Sgw25295 /* 23116423Sgw25295 * Import a non-root pool into the system. 23126423Sgw25295 */ 23136423Sgw25295 int 23146423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 23156423Sgw25295 { 2316*6643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_FALSE)); 23176423Sgw25295 } 23186423Sgw25295 2319*6643Seschrock int 2320*6643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props) 2321*6643Seschrock { 2322*6643Seschrock return (spa_import_common(pool, config, props, B_FALSE, B_TRUE)); 2323*6643Seschrock } 2324*6643Seschrock 2325*6643Seschrock 2326789Sahrens /* 2327789Sahrens * This (illegal) pool name is used when temporarily importing a spa_t in order 2328789Sahrens * to get the vdev stats associated with the imported devices. 2329789Sahrens */ 2330789Sahrens #define TRYIMPORT_NAME "$import" 2331789Sahrens 2332789Sahrens nvlist_t * 2333789Sahrens spa_tryimport(nvlist_t *tryconfig) 2334789Sahrens { 2335789Sahrens nvlist_t *config = NULL; 2336789Sahrens char *poolname; 2337789Sahrens spa_t *spa; 2338789Sahrens uint64_t state; 2339789Sahrens 2340789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 2341789Sahrens return (NULL); 2342789Sahrens 2343789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 2344789Sahrens return (NULL); 2345789Sahrens 23461635Sbonwick /* 23471635Sbonwick * Create and initialize the spa structure. 23481635Sbonwick */ 2349789Sahrens mutex_enter(&spa_namespace_lock); 23501635Sbonwick spa = spa_add(TRYIMPORT_NAME, NULL); 2351789Sahrens spa_activate(spa); 2352789Sahrens 2353789Sahrens /* 23541635Sbonwick * Pass off the heavy lifting to spa_load(). 23551732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 23561732Sbonwick * is actually the one to trust when doing an import. 2357789Sahrens */ 23581732Sbonwick (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 2359789Sahrens 2360789Sahrens /* 2361789Sahrens * If 'tryconfig' was at least parsable, return the current config. 2362789Sahrens */ 2363789Sahrens if (spa->spa_root_vdev != NULL) { 23641635Sbonwick spa_config_enter(spa, RW_READER, FTAG); 2365789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 23661635Sbonwick spa_config_exit(spa, FTAG); 2367789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 2368789Sahrens poolname) == 0); 2369789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2370789Sahrens state) == 0); 23713975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 23723975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 23732082Seschrock 23742082Seschrock /* 23756423Sgw25295 * If the bootfs property exists on this pool then we 23766423Sgw25295 * copy it out so that external consumers can tell which 23776423Sgw25295 * pools are bootable. 23786423Sgw25295 */ 23796423Sgw25295 if (spa->spa_bootfs) { 23806423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 23816423Sgw25295 23826423Sgw25295 /* 23836423Sgw25295 * We have to play games with the name since the 23846423Sgw25295 * pool was opened as TRYIMPORT_NAME. 23856423Sgw25295 */ 23866423Sgw25295 if (dsl_dsobj_to_dsname(spa->spa_name, 23876423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 23886423Sgw25295 char *cp; 23896423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 23906423Sgw25295 23916423Sgw25295 cp = strchr(tmpname, '/'); 23926423Sgw25295 if (cp == NULL) { 23936423Sgw25295 (void) strlcpy(dsname, tmpname, 23946423Sgw25295 MAXPATHLEN); 23956423Sgw25295 } else { 23966423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 23976423Sgw25295 "%s/%s", poolname, ++cp); 23986423Sgw25295 } 23996423Sgw25295 VERIFY(nvlist_add_string(config, 24006423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 24016423Sgw25295 kmem_free(dsname, MAXPATHLEN); 24026423Sgw25295 } 24036423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 24046423Sgw25295 } 24056423Sgw25295 24066423Sgw25295 /* 24075450Sbrendan * Add the list of hot spares and level 2 cache devices. 24082082Seschrock */ 24092082Seschrock spa_add_spares(spa, config); 24105450Sbrendan spa_add_l2cache(spa, config); 2411789Sahrens } 2412789Sahrens 2413789Sahrens spa_unload(spa); 2414789Sahrens spa_deactivate(spa); 2415789Sahrens spa_remove(spa); 2416789Sahrens mutex_exit(&spa_namespace_lock); 2417789Sahrens 2418789Sahrens return (config); 2419789Sahrens } 2420789Sahrens 2421789Sahrens /* 2422789Sahrens * Pool export/destroy 2423789Sahrens * 2424789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 2425789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 2426789Sahrens * update the pool state and sync all the labels to disk, removing the 2427789Sahrens * configuration from the cache afterwards. 2428789Sahrens */ 2429789Sahrens static int 24301775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 2431789Sahrens { 2432789Sahrens spa_t *spa; 2433789Sahrens 24341775Sbillm if (oldconfig) 24351775Sbillm *oldconfig = NULL; 24361775Sbillm 2437789Sahrens if (!(spa_mode & FWRITE)) 2438789Sahrens return (EROFS); 2439789Sahrens 2440789Sahrens mutex_enter(&spa_namespace_lock); 2441789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2442789Sahrens mutex_exit(&spa_namespace_lock); 2443789Sahrens return (ENOENT); 2444789Sahrens } 2445789Sahrens 2446789Sahrens /* 24471544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 24481544Seschrock * reacquire the namespace lock, and see if we can export. 24491544Seschrock */ 24501544Seschrock spa_open_ref(spa, FTAG); 24511544Seschrock mutex_exit(&spa_namespace_lock); 24521544Seschrock spa_async_suspend(spa); 24531544Seschrock mutex_enter(&spa_namespace_lock); 24541544Seschrock spa_close(spa, FTAG); 24551544Seschrock 24561544Seschrock /* 2457789Sahrens * The pool will be in core if it's openable, 2458789Sahrens * in which case we can modify its state. 2459789Sahrens */ 2460789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 2461789Sahrens /* 2462789Sahrens * Objsets may be open only because they're dirty, so we 2463789Sahrens * have to force it to sync before checking spa_refcnt. 2464789Sahrens */ 2465789Sahrens spa_scrub_suspend(spa); 2466789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 2467789Sahrens 24681544Seschrock /* 24691544Seschrock * A pool cannot be exported or destroyed if there are active 24701544Seschrock * references. If we are resetting a pool, allow references by 24711544Seschrock * fault injection handlers. 24721544Seschrock */ 24731544Seschrock if (!spa_refcount_zero(spa) || 24741544Seschrock (spa->spa_inject_ref != 0 && 24751544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 2476789Sahrens spa_scrub_resume(spa); 24771544Seschrock spa_async_resume(spa); 2478789Sahrens mutex_exit(&spa_namespace_lock); 2479789Sahrens return (EBUSY); 2480789Sahrens } 2481789Sahrens 2482789Sahrens spa_scrub_resume(spa); 2483789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 2484789Sahrens 2485789Sahrens /* 2486789Sahrens * We want this to be reflected on every label, 2487789Sahrens * so mark them all dirty. spa_unload() will do the 2488789Sahrens * final sync that pushes these changes out. 2489789Sahrens */ 24901544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 24911601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 24921544Seschrock spa->spa_state = new_state; 24931635Sbonwick spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 24941544Seschrock vdev_config_dirty(spa->spa_root_vdev); 24951601Sbonwick spa_config_exit(spa, FTAG); 24961544Seschrock } 2497789Sahrens } 2498789Sahrens 24994451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 25004451Seschrock 2501789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 2502789Sahrens spa_unload(spa); 2503789Sahrens spa_deactivate(spa); 2504789Sahrens } 2505789Sahrens 25061775Sbillm if (oldconfig && spa->spa_config) 25071775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 25081775Sbillm 25091544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 2510*6643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 25111544Seschrock spa_remove(spa); 25121544Seschrock } 2513789Sahrens mutex_exit(&spa_namespace_lock); 2514789Sahrens 2515789Sahrens return (0); 2516789Sahrens } 2517789Sahrens 2518789Sahrens /* 2519789Sahrens * Destroy a storage pool. 2520789Sahrens */ 2521789Sahrens int 2522789Sahrens spa_destroy(char *pool) 2523789Sahrens { 25241775Sbillm return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 2525789Sahrens } 2526789Sahrens 2527789Sahrens /* 2528789Sahrens * Export a storage pool. 2529789Sahrens */ 2530789Sahrens int 25311775Sbillm spa_export(char *pool, nvlist_t **oldconfig) 2532789Sahrens { 25331775Sbillm return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 2534789Sahrens } 2535789Sahrens 2536789Sahrens /* 25371544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 25381544Seschrock * from the namespace in any way. 25391544Seschrock */ 25401544Seschrock int 25411544Seschrock spa_reset(char *pool) 25421544Seschrock { 25431775Sbillm return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 25441544Seschrock } 25451544Seschrock 25461544Seschrock 25471544Seschrock /* 2548789Sahrens * ========================================================================== 2549789Sahrens * Device manipulation 2550789Sahrens * ========================================================================== 2551789Sahrens */ 2552789Sahrens 2553789Sahrens /* 25544527Sperrin * Add a device to a storage pool. 2555789Sahrens */ 2556789Sahrens int 2557789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 2558789Sahrens { 2559789Sahrens uint64_t txg; 25601635Sbonwick int c, error; 2561789Sahrens vdev_t *rvd = spa->spa_root_vdev; 25621585Sbonwick vdev_t *vd, *tvd; 25635450Sbrendan nvlist_t **spares, **l2cache; 25645450Sbrendan uint_t nspares, nl2cache; 2565789Sahrens 2566789Sahrens txg = spa_vdev_enter(spa); 2567789Sahrens 25682082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 25692082Seschrock VDEV_ALLOC_ADD)) != 0) 25702082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 25712082Seschrock 25723377Seschrock spa->spa_pending_vdev = vd; 2573789Sahrens 25745450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 25755450Sbrendan &nspares) != 0) 25762082Seschrock nspares = 0; 25772082Seschrock 25785450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 25795450Sbrendan &nl2cache) != 0) 25805450Sbrendan nl2cache = 0; 25815450Sbrendan 25825450Sbrendan if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) { 25833377Seschrock spa->spa_pending_vdev = NULL; 25842082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 25853377Seschrock } 25862082Seschrock 25872082Seschrock if (vd->vdev_children != 0) { 25883377Seschrock if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 25893377Seschrock spa->spa_pending_vdev = NULL; 25902082Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 25912082Seschrock } 25922082Seschrock } 25932082Seschrock 25943377Seschrock /* 25955450Sbrendan * We must validate the spares and l2cache devices after checking the 25965450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 25973377Seschrock */ 25985450Sbrendan if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) { 25993377Seschrock spa->spa_pending_vdev = NULL; 26003377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 26013377Seschrock } 26023377Seschrock 26033377Seschrock spa->spa_pending_vdev = NULL; 26043377Seschrock 26053377Seschrock /* 26063377Seschrock * Transfer each new top-level vdev from vd to rvd. 26073377Seschrock */ 26083377Seschrock for (c = 0; c < vd->vdev_children; c++) { 26093377Seschrock tvd = vd->vdev_child[c]; 26103377Seschrock vdev_remove_child(vd, tvd); 26113377Seschrock tvd->vdev_id = rvd->vdev_children; 26123377Seschrock vdev_add_child(rvd, tvd); 26133377Seschrock vdev_config_dirty(tvd); 26143377Seschrock } 26153377Seschrock 26162082Seschrock if (nspares != 0) { 26175450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 26185450Sbrendan ZPOOL_CONFIG_SPARES); 26192082Seschrock spa_load_spares(spa); 26205450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 26215450Sbrendan } 26225450Sbrendan 26235450Sbrendan if (nl2cache != 0) { 26245450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 26255450Sbrendan ZPOOL_CONFIG_L2CACHE); 26265450Sbrendan spa_load_l2cache(spa); 26275450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 2628789Sahrens } 2629789Sahrens 2630789Sahrens /* 26311585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 26321585Sbonwick * If other threads start allocating from these vdevs before we 26331585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 26341585Sbonwick * fail to open the pool because there are DVAs that the config cache 26351585Sbonwick * can't translate. Therefore, we first add the vdevs without 26361585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 26371635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 26381585Sbonwick * 26391585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 26401585Sbonwick * if we lose power at any point in this sequence, the remaining 26411585Sbonwick * steps will be completed the next time we load the pool. 2642789Sahrens */ 26431635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 26441585Sbonwick 26451635Sbonwick mutex_enter(&spa_namespace_lock); 26461635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 26471635Sbonwick mutex_exit(&spa_namespace_lock); 2648789Sahrens 26491635Sbonwick return (0); 2650789Sahrens } 2651789Sahrens 2652789Sahrens /* 2653789Sahrens * Attach a device to a mirror. The arguments are the path to any device 2654789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 2655789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 2656789Sahrens * 2657789Sahrens * If 'replacing' is specified, the new device is intended to replace the 2658789Sahrens * existing device; in this case the two devices are made into their own 26594451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 2660789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 2661789Sahrens * extra rules: you can't attach to it after it's been created, and upon 2662789Sahrens * completion of resilvering, the first disk (the one being replaced) 2663789Sahrens * is automatically detached. 2664789Sahrens */ 2665789Sahrens int 26661544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 2667789Sahrens { 2668789Sahrens uint64_t txg, open_txg; 2669789Sahrens int error; 2670789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2671789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 26722082Seschrock vdev_ops_t *pvops; 26734527Sperrin int is_log; 2674789Sahrens 2675789Sahrens txg = spa_vdev_enter(spa); 2676789Sahrens 2677*6643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 2678789Sahrens 2679789Sahrens if (oldvd == NULL) 2680789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2681789Sahrens 26821585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 26831585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 26841585Sbonwick 2685789Sahrens pvd = oldvd->vdev_parent; 2686789Sahrens 26872082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 26884451Seschrock VDEV_ALLOC_ADD)) != 0) 26894451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 26904451Seschrock 26914451Seschrock if (newrootvd->vdev_children != 1) 2692789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2693789Sahrens 2694789Sahrens newvd = newrootvd->vdev_child[0]; 2695789Sahrens 2696789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 2697789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 2698789Sahrens 26992082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 2700789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 2701789Sahrens 27024527Sperrin /* 27034527Sperrin * Spares can't replace logs 27044527Sperrin */ 27054527Sperrin is_log = oldvd->vdev_islog; 27064527Sperrin if (is_log && newvd->vdev_isspare) 27074527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27084527Sperrin 27092082Seschrock if (!replacing) { 27102082Seschrock /* 27112082Seschrock * For attach, the only allowable parent is a mirror or the root 27122082Seschrock * vdev. 27132082Seschrock */ 27142082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 27152082Seschrock pvd->vdev_ops != &vdev_root_ops) 27162082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27172082Seschrock 27182082Seschrock pvops = &vdev_mirror_ops; 27192082Seschrock } else { 27202082Seschrock /* 27212082Seschrock * Active hot spares can only be replaced by inactive hot 27222082Seschrock * spares. 27232082Seschrock */ 27242082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 27252082Seschrock pvd->vdev_child[1] == oldvd && 27262082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 27272082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27282082Seschrock 27292082Seschrock /* 27302082Seschrock * If the source is a hot spare, and the parent isn't already a 27312082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 27323377Seschrock * want to create a replacing vdev. The user is not allowed to 27333377Seschrock * attach to a spared vdev child unless the 'isspare' state is 27343377Seschrock * the same (spare replaces spare, non-spare replaces 27353377Seschrock * non-spare). 27362082Seschrock */ 27372082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 27382082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27393377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 27403377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 27413377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 27422082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 27432082Seschrock newvd->vdev_isspare) 27442082Seschrock pvops = &vdev_spare_ops; 27452082Seschrock else 27462082Seschrock pvops = &vdev_replacing_ops; 27472082Seschrock } 27482082Seschrock 27491175Slling /* 27501175Slling * Compare the new device size with the replaceable/attachable 27511175Slling * device size. 27521175Slling */ 27531175Slling if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 2754789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 2755789Sahrens 27561732Sbonwick /* 27571732Sbonwick * The new device cannot have a higher alignment requirement 27581732Sbonwick * than the top-level vdev. 27591732Sbonwick */ 27601732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 2761789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 2762789Sahrens 2763789Sahrens /* 2764789Sahrens * If this is an in-place replacement, update oldvd's path and devid 2765789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 2766789Sahrens */ 2767789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 2768789Sahrens spa_strfree(oldvd->vdev_path); 2769789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 2770789Sahrens KM_SLEEP); 2771789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 2772789Sahrens newvd->vdev_path, "old"); 2773789Sahrens if (oldvd->vdev_devid != NULL) { 2774789Sahrens spa_strfree(oldvd->vdev_devid); 2775789Sahrens oldvd->vdev_devid = NULL; 2776789Sahrens } 2777789Sahrens } 2778789Sahrens 2779789Sahrens /* 27802082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 27812082Seschrock * mirror/replacing/spare vdev above oldvd. 2782789Sahrens */ 2783789Sahrens if (pvd->vdev_ops != pvops) 2784789Sahrens pvd = vdev_add_parent(oldvd, pvops); 2785789Sahrens 2786789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 2787789Sahrens ASSERT(pvd->vdev_ops == pvops); 2788789Sahrens ASSERT(oldvd->vdev_parent == pvd); 2789789Sahrens 2790789Sahrens /* 2791789Sahrens * Extract the new device from its root and add it to pvd. 2792789Sahrens */ 2793789Sahrens vdev_remove_child(newrootvd, newvd); 2794789Sahrens newvd->vdev_id = pvd->vdev_children; 2795789Sahrens vdev_add_child(pvd, newvd); 2796789Sahrens 27971544Seschrock /* 27981544Seschrock * If newvd is smaller than oldvd, but larger than its rsize, 27991544Seschrock * the addition of newvd may have decreased our parent's asize. 28001544Seschrock */ 28011544Seschrock pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 28021544Seschrock 2803789Sahrens tvd = newvd->vdev_top; 2804789Sahrens ASSERT(pvd->vdev_top == tvd); 2805789Sahrens ASSERT(tvd->vdev_parent == rvd); 2806789Sahrens 2807789Sahrens vdev_config_dirty(tvd); 2808789Sahrens 2809789Sahrens /* 2810789Sahrens * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 2811789Sahrens * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 2812789Sahrens */ 2813789Sahrens open_txg = txg + TXG_CONCURRENT_STATES - 1; 2814789Sahrens 2815789Sahrens mutex_enter(&newvd->vdev_dtl_lock); 2816789Sahrens space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 2817789Sahrens open_txg - TXG_INITIAL + 1); 2818789Sahrens mutex_exit(&newvd->vdev_dtl_lock); 2819789Sahrens 28203377Seschrock if (newvd->vdev_isspare) 28213377Seschrock spa_spare_activate(newvd); 28221544Seschrock 2823789Sahrens /* 2824789Sahrens * Mark newvd's DTL dirty in this txg. 2825789Sahrens */ 28261732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 2827789Sahrens 2828789Sahrens (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 2829789Sahrens 2830789Sahrens /* 28314451Seschrock * Kick off a resilver to update newvd. We need to grab the namespace 28324451Seschrock * lock because spa_scrub() needs to post a sysevent with the pool name. 2833789Sahrens */ 28344451Seschrock mutex_enter(&spa_namespace_lock); 2835789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 28364451Seschrock mutex_exit(&spa_namespace_lock); 2837789Sahrens 2838789Sahrens return (0); 2839789Sahrens } 2840789Sahrens 2841789Sahrens /* 2842789Sahrens * Detach a device from a mirror or replacing vdev. 2843789Sahrens * If 'replace_done' is specified, only detach if the parent 2844789Sahrens * is a replacing vdev. 2845789Sahrens */ 2846789Sahrens int 28471544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 2848789Sahrens { 2849789Sahrens uint64_t txg; 2850789Sahrens int c, t, error; 2851789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2852789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 28532082Seschrock boolean_t unspare = B_FALSE; 28542082Seschrock uint64_t unspare_guid; 2855789Sahrens 2856789Sahrens txg = spa_vdev_enter(spa); 2857789Sahrens 2858*6643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 2859789Sahrens 2860789Sahrens if (vd == NULL) 2861789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 2862789Sahrens 28631585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 28641585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28651585Sbonwick 2866789Sahrens pvd = vd->vdev_parent; 2867789Sahrens 2868789Sahrens /* 2869789Sahrens * If replace_done is specified, only remove this device if it's 28702082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 28712082Seschrock * disk can be removed. 2872789Sahrens */ 28732082Seschrock if (replace_done) { 28742082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 28752082Seschrock if (vd->vdev_id != 0) 28762082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28772082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 28782082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 28792082Seschrock } 28802082Seschrock } 28812082Seschrock 28822082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 28834577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 2884789Sahrens 2885789Sahrens /* 28862082Seschrock * Only mirror, replacing, and spare vdevs support detach. 2887789Sahrens */ 2888789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 28892082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 28902082Seschrock pvd->vdev_ops != &vdev_spare_ops) 2891789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2892789Sahrens 2893789Sahrens /* 2894789Sahrens * If there's only one replica, you can't detach it. 2895789Sahrens */ 2896789Sahrens if (pvd->vdev_children <= 1) 2897789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2898789Sahrens 2899789Sahrens /* 2900789Sahrens * If all siblings have non-empty DTLs, this device may have the only 2901789Sahrens * valid copy of the data, which means we cannot safely detach it. 2902789Sahrens * 2903789Sahrens * XXX -- as in the vdev_offline() case, we really want a more 2904789Sahrens * precise DTL check. 2905789Sahrens */ 2906789Sahrens for (c = 0; c < pvd->vdev_children; c++) { 2907789Sahrens uint64_t dirty; 2908789Sahrens 2909789Sahrens cvd = pvd->vdev_child[c]; 2910789Sahrens if (cvd == vd) 2911789Sahrens continue; 2912789Sahrens if (vdev_is_dead(cvd)) 2913789Sahrens continue; 2914789Sahrens mutex_enter(&cvd->vdev_dtl_lock); 2915789Sahrens dirty = cvd->vdev_dtl_map.sm_space | 2916789Sahrens cvd->vdev_dtl_scrub.sm_space; 2917789Sahrens mutex_exit(&cvd->vdev_dtl_lock); 2918789Sahrens if (!dirty) 2919789Sahrens break; 2920789Sahrens } 29212082Seschrock 29222082Seschrock /* 29232082Seschrock * If we are a replacing or spare vdev, then we can always detach the 29242082Seschrock * latter child, as that is how one cancels the operation. 29252082Seschrock */ 29262082Seschrock if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 29272082Seschrock c == pvd->vdev_children) 2928789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 2929789Sahrens 2930789Sahrens /* 29312082Seschrock * If we are detaching the original disk from a spare, then it implies 29322082Seschrock * that the spare should become a real disk, and be removed from the 29332082Seschrock * active spare list for the pool. 29342082Seschrock */ 29352082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 29362082Seschrock vd->vdev_id == 0) 29372082Seschrock unspare = B_TRUE; 29382082Seschrock 29392082Seschrock /* 2940789Sahrens * Erase the disk labels so the disk can be used for other things. 2941789Sahrens * This must be done after all other error cases are handled, 2942789Sahrens * but before we disembowel vd (so we can still do I/O to it). 2943789Sahrens * But if we can't do it, don't treat the error as fatal -- 2944789Sahrens * it may be that the unwritability of the disk is the reason 2945789Sahrens * it's being detached! 2946789Sahrens */ 29473377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 2948789Sahrens 2949789Sahrens /* 2950789Sahrens * Remove vd from its parent and compact the parent's children. 2951789Sahrens */ 2952789Sahrens vdev_remove_child(pvd, vd); 2953789Sahrens vdev_compact_children(pvd); 2954789Sahrens 2955789Sahrens /* 2956789Sahrens * Remember one of the remaining children so we can get tvd below. 2957789Sahrens */ 2958789Sahrens cvd = pvd->vdev_child[0]; 2959789Sahrens 2960789Sahrens /* 29612082Seschrock * If we need to remove the remaining child from the list of hot spares, 29622082Seschrock * do it now, marking the vdev as no longer a spare in the process. We 29632082Seschrock * must do this before vdev_remove_parent(), because that can change the 29642082Seschrock * GUID if it creates a new toplevel GUID. 29652082Seschrock */ 29662082Seschrock if (unspare) { 29672082Seschrock ASSERT(cvd->vdev_isspare); 29683377Seschrock spa_spare_remove(cvd); 29692082Seschrock unspare_guid = cvd->vdev_guid; 29702082Seschrock } 29712082Seschrock 29722082Seschrock /* 2973789Sahrens * If the parent mirror/replacing vdev only has one child, 2974789Sahrens * the parent is no longer needed. Remove it from the tree. 2975789Sahrens */ 2976789Sahrens if (pvd->vdev_children == 1) 2977789Sahrens vdev_remove_parent(cvd); 2978789Sahrens 2979789Sahrens /* 2980789Sahrens * We don't set tvd until now because the parent we just removed 2981789Sahrens * may have been the previous top-level vdev. 2982789Sahrens */ 2983789Sahrens tvd = cvd->vdev_top; 2984789Sahrens ASSERT(tvd->vdev_parent == rvd); 2985789Sahrens 2986789Sahrens /* 29873377Seschrock * Reevaluate the parent vdev state. 2988789Sahrens */ 29894451Seschrock vdev_propagate_state(cvd); 2990789Sahrens 2991789Sahrens /* 29923377Seschrock * If the device we just detached was smaller than the others, it may be 29933377Seschrock * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 29943377Seschrock * can't fail because the existing metaslabs are already in core, so 29953377Seschrock * there's nothing to read from disk. 2996789Sahrens */ 29971732Sbonwick VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2998789Sahrens 2999789Sahrens vdev_config_dirty(tvd); 3000789Sahrens 3001789Sahrens /* 30023377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 30033377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 30043377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 30053377Seschrock * prevent vd from being accessed after it's freed. 3006789Sahrens */ 3007789Sahrens for (t = 0; t < TXG_SIZE; t++) 3008789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 30091732Sbonwick vd->vdev_detached = B_TRUE; 30101732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 3011789Sahrens 30124451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 30134451Seschrock 30142082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 30152082Seschrock 30162082Seschrock /* 30173377Seschrock * If this was the removal of the original device in a hot spare vdev, 30183377Seschrock * then we want to go through and remove the device from the hot spare 30193377Seschrock * list of every other pool. 30202082Seschrock */ 30212082Seschrock if (unspare) { 30222082Seschrock spa = NULL; 30232082Seschrock mutex_enter(&spa_namespace_lock); 30242082Seschrock while ((spa = spa_next(spa)) != NULL) { 30252082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 30262082Seschrock continue; 30272082Seschrock 30282082Seschrock (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 30292082Seschrock } 30302082Seschrock mutex_exit(&spa_namespace_lock); 30312082Seschrock } 30322082Seschrock 30332082Seschrock return (error); 30342082Seschrock } 30352082Seschrock 30362082Seschrock /* 30375450Sbrendan * Remove a spares vdev from the nvlist config. 30382082Seschrock */ 30395450Sbrendan static int 30405450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare, 30415450Sbrendan nvlist_t **spares, int nspares, vdev_t *vd) 30422082Seschrock { 30435450Sbrendan nvlist_t *nv, **newspares; 30445450Sbrendan int i, j; 30452082Seschrock 30462082Seschrock nv = NULL; 30475450Sbrendan for (i = 0; i < nspares; i++) { 30485450Sbrendan uint64_t theguid; 30495450Sbrendan 30505450Sbrendan VERIFY(nvlist_lookup_uint64(spares[i], 30515450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 30525450Sbrendan if (theguid == guid) { 30535450Sbrendan nv = spares[i]; 30545450Sbrendan break; 30552082Seschrock } 30562082Seschrock } 30572082Seschrock 30582082Seschrock /* 30595450Sbrendan * Only remove the hot spare if it's not currently in use in this pool. 30602082Seschrock */ 30615450Sbrendan if (nv == NULL && vd == NULL) 30625450Sbrendan return (ENOENT); 30635450Sbrendan 30645450Sbrendan if (nv == NULL && vd != NULL) 30655450Sbrendan return (ENOTSUP); 30665450Sbrendan 30675450Sbrendan if (!unspare && nv != NULL && vd != NULL) 30685450Sbrendan return (EBUSY); 30692082Seschrock 30702082Seschrock if (nspares == 1) { 30712082Seschrock newspares = NULL; 30722082Seschrock } else { 30732082Seschrock newspares = kmem_alloc((nspares - 1) * sizeof (void *), 30742082Seschrock KM_SLEEP); 30752082Seschrock for (i = 0, j = 0; i < nspares; i++) { 30762082Seschrock if (spares[i] != nv) 30772082Seschrock VERIFY(nvlist_dup(spares[i], 30782082Seschrock &newspares[j++], KM_SLEEP) == 0); 30792082Seschrock } 30802082Seschrock } 30812082Seschrock 30825450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES, 30832082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 30845450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 30855450Sbrendan ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0); 30862082Seschrock for (i = 0; i < nspares - 1; i++) 30872082Seschrock nvlist_free(newspares[i]); 30882082Seschrock kmem_free(newspares, (nspares - 1) * sizeof (void *)); 30895450Sbrendan 30905450Sbrendan return (0); 30915450Sbrendan } 30925450Sbrendan 30935450Sbrendan /* 30945450Sbrendan * Remove an l2cache vdev from the nvlist config. 30955450Sbrendan */ 30965450Sbrendan static int 30975450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache, 30985450Sbrendan int nl2cache, vdev_t *vd) 30995450Sbrendan { 31005450Sbrendan nvlist_t *nv, **newl2cache; 31015450Sbrendan int i, j; 31025450Sbrendan 31035450Sbrendan nv = NULL; 31045450Sbrendan for (i = 0; i < nl2cache; i++) { 31055450Sbrendan uint64_t theguid; 31065450Sbrendan 31075450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 31085450Sbrendan ZPOOL_CONFIG_GUID, &theguid) == 0); 31095450Sbrendan if (theguid == guid) { 31105450Sbrendan nv = l2cache[i]; 31115450Sbrendan break; 31125450Sbrendan } 31135450Sbrendan } 31145450Sbrendan 31155450Sbrendan if (vd == NULL) { 31165450Sbrendan for (i = 0; i < nl2cache; i++) { 31175450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) { 31185450Sbrendan vd = sav->sav_vdevs[i]; 31195450Sbrendan break; 31205450Sbrendan } 31215450Sbrendan } 31225450Sbrendan } 31235450Sbrendan 31245450Sbrendan if (nv == NULL && vd == NULL) 31255450Sbrendan return (ENOENT); 31265450Sbrendan 31275450Sbrendan if (nv == NULL && vd != NULL) 31285450Sbrendan return (ENOTSUP); 31295450Sbrendan 31305450Sbrendan if (nl2cache == 1) { 31315450Sbrendan newl2cache = NULL; 31325450Sbrendan } else { 31335450Sbrendan newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *), 31345450Sbrendan KM_SLEEP); 31355450Sbrendan for (i = 0, j = 0; i < nl2cache; i++) { 31365450Sbrendan if (l2cache[i] != nv) 31375450Sbrendan VERIFY(nvlist_dup(l2cache[i], 31385450Sbrendan &newl2cache[j++], KM_SLEEP) == 0); 31395450Sbrendan } 31405450Sbrendan } 31415450Sbrendan 31425450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 31435450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 31445450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 31455450Sbrendan ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0); 31465450Sbrendan for (i = 0; i < nl2cache - 1; i++) 31475450Sbrendan nvlist_free(newl2cache[i]); 31485450Sbrendan kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *)); 31495450Sbrendan 31505450Sbrendan return (0); 31515450Sbrendan } 31525450Sbrendan 31535450Sbrendan /* 31545450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 31555450Sbrendan * spares and level 2 ARC devices. 31565450Sbrendan */ 31575450Sbrendan int 31585450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 31595450Sbrendan { 31605450Sbrendan vdev_t *vd; 31615450Sbrendan nvlist_t **spares, **l2cache; 31625450Sbrendan uint_t nspares, nl2cache; 31635450Sbrendan int error = 0; 31645450Sbrendan 31655450Sbrendan spa_config_enter(spa, RW_WRITER, FTAG); 31665450Sbrendan 3167*6643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 31685450Sbrendan 31695450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 31705450Sbrendan spa_spare_exists(guid, NULL) && 31715450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 31725450Sbrendan ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) { 31735450Sbrendan if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare, 31745450Sbrendan spares, nspares, vd)) != 0) 31755450Sbrendan goto out; 31765450Sbrendan spa_load_spares(spa); 31775450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 31785450Sbrendan goto out; 31795450Sbrendan } 31805450Sbrendan 31815450Sbrendan if (spa->spa_l2cache.sav_vdevs != NULL && 31825450Sbrendan spa_l2cache_exists(guid, NULL) && 31835450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 31845450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) { 31855450Sbrendan if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid, 31865450Sbrendan l2cache, nl2cache, vd)) != 0) 31875450Sbrendan goto out; 31885450Sbrendan spa_load_l2cache(spa); 31895450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 31905450Sbrendan } 31912082Seschrock 31922082Seschrock out: 31932082Seschrock spa_config_exit(spa, FTAG); 31945450Sbrendan return (error); 3195789Sahrens } 3196789Sahrens 3197789Sahrens /* 31984451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 31994451Seschrock * current spared, so we can detach it. 3200789Sahrens */ 32011544Seschrock static vdev_t * 32024451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 3203789Sahrens { 32041544Seschrock vdev_t *newvd, *oldvd; 3205789Sahrens int c; 3206789Sahrens 32071544Seschrock for (c = 0; c < vd->vdev_children; c++) { 32084451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 32091544Seschrock if (oldvd != NULL) 32101544Seschrock return (oldvd); 32111544Seschrock } 3212789Sahrens 32134451Seschrock /* 32144451Seschrock * Check for a completed replacement. 32154451Seschrock */ 3216789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 32171544Seschrock oldvd = vd->vdev_child[0]; 32181544Seschrock newvd = vd->vdev_child[1]; 3219789Sahrens 32201544Seschrock mutex_enter(&newvd->vdev_dtl_lock); 32211544Seschrock if (newvd->vdev_dtl_map.sm_space == 0 && 32221544Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 32231544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32241544Seschrock return (oldvd); 32251544Seschrock } 32261544Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32271544Seschrock } 3228789Sahrens 32294451Seschrock /* 32304451Seschrock * Check for a completed resilver with the 'unspare' flag set. 32314451Seschrock */ 32324451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 32334451Seschrock newvd = vd->vdev_child[0]; 32344451Seschrock oldvd = vd->vdev_child[1]; 32354451Seschrock 32364451Seschrock mutex_enter(&newvd->vdev_dtl_lock); 32374451Seschrock if (newvd->vdev_unspare && 32384451Seschrock newvd->vdev_dtl_map.sm_space == 0 && 32394451Seschrock newvd->vdev_dtl_scrub.sm_space == 0) { 32404451Seschrock newvd->vdev_unspare = 0; 32414451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32424451Seschrock return (oldvd); 32434451Seschrock } 32444451Seschrock mutex_exit(&newvd->vdev_dtl_lock); 32454451Seschrock } 32464451Seschrock 32471544Seschrock return (NULL); 3248789Sahrens } 3249789Sahrens 32501544Seschrock static void 32514451Seschrock spa_vdev_resilver_done(spa_t *spa) 3252789Sahrens { 32531544Seschrock vdev_t *vd; 32542082Seschrock vdev_t *pvd; 32551544Seschrock uint64_t guid; 32562082Seschrock uint64_t pguid = 0; 3257789Sahrens 32581544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3259789Sahrens 32604451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 32611544Seschrock guid = vd->vdev_guid; 32622082Seschrock /* 32632082Seschrock * If we have just finished replacing a hot spared device, then 32642082Seschrock * we need to detach the parent's first child (the original hot 32652082Seschrock * spare) as well. 32662082Seschrock */ 32672082Seschrock pvd = vd->vdev_parent; 32682082Seschrock if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 32692082Seschrock pvd->vdev_id == 0) { 32702082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 32712082Seschrock ASSERT(pvd->vdev_parent->vdev_children == 2); 32722082Seschrock pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 32732082Seschrock } 32741544Seschrock spa_config_exit(spa, FTAG); 32751544Seschrock if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 32761544Seschrock return; 32772082Seschrock if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 32782082Seschrock return; 32791544Seschrock spa_config_enter(spa, RW_READER, FTAG); 3280789Sahrens } 3281789Sahrens 32821544Seschrock spa_config_exit(spa, FTAG); 3283789Sahrens } 3284789Sahrens 3285789Sahrens /* 32861354Seschrock * Update the stored path for this vdev. Dirty the vdev configuration, relying 32871354Seschrock * on spa_vdev_enter/exit() to synchronize the labels and cache. 32881354Seschrock */ 32891354Seschrock int 32901354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 32911354Seschrock { 3292*6643Seschrock vdev_t *vd; 32931354Seschrock uint64_t txg; 32941354Seschrock 32951354Seschrock txg = spa_vdev_enter(spa); 32961354Seschrock 3297*6643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) { 32982082Seschrock /* 3299*6643Seschrock * Determine if this is a reference to a hot spare device. If 3300*6643Seschrock * it is, update the path manually as there is no associated 3301*6643Seschrock * vdev_t that can be synced to disk. 33022082Seschrock */ 3303*6643Seschrock nvlist_t **spares; 3304*6643Seschrock uint_t i, nspares; 33055450Sbrendan 33065450Sbrendan if (spa->spa_spares.sav_config != NULL) { 33075450Sbrendan VERIFY(nvlist_lookup_nvlist_array( 33085450Sbrendan spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 33095450Sbrendan &spares, &nspares) == 0); 33102082Seschrock for (i = 0; i < nspares; i++) { 33112082Seschrock uint64_t theguid; 33122082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 33132082Seschrock ZPOOL_CONFIG_GUID, &theguid) == 0); 33145450Sbrendan if (theguid == guid) { 33155450Sbrendan VERIFY(nvlist_add_string(spares[i], 33165450Sbrendan ZPOOL_CONFIG_PATH, newpath) == 0); 33175450Sbrendan spa_load_spares(spa); 33185450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 33195450Sbrendan return (spa_vdev_exit(spa, NULL, txg, 33205450Sbrendan 0)); 33215450Sbrendan } 33222082Seschrock } 33232082Seschrock } 33245450Sbrendan 33255450Sbrendan return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 33262082Seschrock } 33271354Seschrock 33281585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 33291585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 33301585Sbonwick 33311354Seschrock spa_strfree(vd->vdev_path); 33321354Seschrock vd->vdev_path = spa_strdup(newpath); 33331354Seschrock 33341354Seschrock vdev_config_dirty(vd->vdev_top); 33351354Seschrock 33361354Seschrock return (spa_vdev_exit(spa, NULL, txg, 0)); 33371354Seschrock } 33381354Seschrock 33391354Seschrock /* 3340789Sahrens * ========================================================================== 3341789Sahrens * SPA Scrubbing 3342789Sahrens * ========================================================================== 3343789Sahrens */ 3344789Sahrens 3345789Sahrens static void 3346789Sahrens spa_scrub_io_done(zio_t *zio) 3347789Sahrens { 3348789Sahrens spa_t *spa = zio->io_spa; 3349789Sahrens 33504309Smaybee arc_data_buf_free(zio->io_data, zio->io_size); 3351789Sahrens 3352789Sahrens mutex_enter(&spa->spa_scrub_lock); 33531544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 33541775Sbillm vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 3355789Sahrens spa->spa_scrub_errors++; 3356789Sahrens mutex_enter(&vd->vdev_stat_lock); 3357789Sahrens vd->vdev_stat.vs_scrub_errors++; 3358789Sahrens mutex_exit(&vd->vdev_stat_lock); 3359789Sahrens } 33603697Smishra 33613697Smishra if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 33621544Seschrock cv_broadcast(&spa->spa_scrub_io_cv); 33633697Smishra 33643697Smishra ASSERT(spa->spa_scrub_inflight >= 0); 33653697Smishra 33661544Seschrock mutex_exit(&spa->spa_scrub_lock); 3367789Sahrens } 3368789Sahrens 3369789Sahrens static void 33701544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 33711544Seschrock zbookmark_t *zb) 3372789Sahrens { 3373789Sahrens size_t size = BP_GET_LSIZE(bp); 33743697Smishra void *data; 3375789Sahrens 3376789Sahrens mutex_enter(&spa->spa_scrub_lock); 33773697Smishra /* 33783697Smishra * Do not give too much work to vdev(s). 33793697Smishra */ 33803697Smishra while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 33813697Smishra cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 33823697Smishra } 3383789Sahrens spa->spa_scrub_inflight++; 3384789Sahrens mutex_exit(&spa->spa_scrub_lock); 3385789Sahrens 33864309Smaybee data = arc_data_buf_alloc(size); 33873697Smishra 33881544Seschrock if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 33891544Seschrock flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 33901544Seschrock 33911807Sbonwick flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 33921544Seschrock 3393789Sahrens zio_nowait(zio_read(NULL, spa, bp, data, size, 33941544Seschrock spa_scrub_io_done, NULL, priority, flags, zb)); 3395789Sahrens } 3396789Sahrens 3397789Sahrens /* ARGSUSED */ 3398789Sahrens static int 3399789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 3400789Sahrens { 3401789Sahrens blkptr_t *bp = &bc->bc_blkptr; 34021775Sbillm vdev_t *vd = spa->spa_root_vdev; 34031775Sbillm dva_t *dva = bp->blk_dva; 34041775Sbillm int needs_resilver = B_FALSE; 34051775Sbillm int d; 3406789Sahrens 34071775Sbillm if (bc->bc_errno) { 3408789Sahrens /* 3409789Sahrens * We can't scrub this block, but we can continue to scrub 3410789Sahrens * the rest of the pool. Note the error and move along. 3411789Sahrens */ 3412789Sahrens mutex_enter(&spa->spa_scrub_lock); 3413789Sahrens spa->spa_scrub_errors++; 3414789Sahrens mutex_exit(&spa->spa_scrub_lock); 3415789Sahrens 34161775Sbillm mutex_enter(&vd->vdev_stat_lock); 34171775Sbillm vd->vdev_stat.vs_scrub_errors++; 34181775Sbillm mutex_exit(&vd->vdev_stat_lock); 3419789Sahrens 3420789Sahrens return (ERESTART); 3421789Sahrens } 3422789Sahrens 3423789Sahrens ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 3424789Sahrens 34251775Sbillm for (d = 0; d < BP_GET_NDVAS(bp); d++) { 34261775Sbillm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 34271775Sbillm 34281775Sbillm ASSERT(vd != NULL); 34291775Sbillm 34301775Sbillm /* 34311775Sbillm * Keep track of how much data we've examined so that 34321775Sbillm * zpool(1M) status can make useful progress reports. 34331775Sbillm */ 34341775Sbillm mutex_enter(&vd->vdev_stat_lock); 34351775Sbillm vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 34361775Sbillm mutex_exit(&vd->vdev_stat_lock); 3437789Sahrens 34381775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 34391775Sbillm if (DVA_GET_GANG(&dva[d])) { 34401775Sbillm /* 34411775Sbillm * Gang members may be spread across multiple 34421775Sbillm * vdevs, so the best we can do is look at the 34431775Sbillm * pool-wide DTL. 34441775Sbillm * XXX -- it would be better to change our 34451775Sbillm * allocation policy to ensure that this can't 34461775Sbillm * happen. 34471775Sbillm */ 34481775Sbillm vd = spa->spa_root_vdev; 34491775Sbillm } 34501775Sbillm if (vdev_dtl_contains(&vd->vdev_dtl_map, 34511775Sbillm bp->blk_birth, 1)) 34521775Sbillm needs_resilver = B_TRUE; 3453789Sahrens } 34541775Sbillm } 34551775Sbillm 34561775Sbillm if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 3457789Sahrens spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 34581544Seschrock ZIO_FLAG_SCRUB, &bc->bc_bookmark); 34591775Sbillm else if (needs_resilver) 34601775Sbillm spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 34611775Sbillm ZIO_FLAG_RESILVER, &bc->bc_bookmark); 3462789Sahrens 3463789Sahrens return (0); 3464789Sahrens } 3465789Sahrens 3466789Sahrens static void 3467789Sahrens spa_scrub_thread(spa_t *spa) 3468789Sahrens { 3469789Sahrens callb_cpr_t cprinfo; 3470789Sahrens traverse_handle_t *th = spa->spa_scrub_th; 3471789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3472789Sahrens pool_scrub_type_t scrub_type = spa->spa_scrub_type; 3473789Sahrens int error = 0; 3474789Sahrens boolean_t complete; 3475789Sahrens 3476789Sahrens CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 3477789Sahrens 3478797Sbonwick /* 3479797Sbonwick * If we're restarting due to a snapshot create/delete, 3480797Sbonwick * wait for that to complete. 3481797Sbonwick */ 3482797Sbonwick txg_wait_synced(spa_get_dsl(spa), 0); 3483797Sbonwick 34841544Seschrock dprintf("start %s mintxg=%llu maxtxg=%llu\n", 34851544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 34861544Seschrock spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 34871544Seschrock 34881544Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 34891544Seschrock vdev_reopen(rvd); /* purge all vdev caches */ 3490789Sahrens vdev_config_dirty(rvd); /* rewrite all disk labels */ 3491789Sahrens vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 34921544Seschrock spa_config_exit(spa, FTAG); 3493789Sahrens 3494789Sahrens mutex_enter(&spa->spa_scrub_lock); 3495789Sahrens spa->spa_scrub_errors = 0; 3496789Sahrens spa->spa_scrub_active = 1; 34971544Seschrock ASSERT(spa->spa_scrub_inflight == 0); 3498789Sahrens 3499789Sahrens while (!spa->spa_scrub_stop) { 3500789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 35011544Seschrock while (spa->spa_scrub_suspended) { 3502789Sahrens spa->spa_scrub_active = 0; 3503789Sahrens cv_broadcast(&spa->spa_scrub_cv); 3504789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3505789Sahrens spa->spa_scrub_active = 1; 3506789Sahrens } 3507789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 3508789Sahrens 3509789Sahrens if (spa->spa_scrub_restart_txg != 0) 3510789Sahrens break; 3511789Sahrens 3512789Sahrens mutex_exit(&spa->spa_scrub_lock); 3513789Sahrens error = traverse_more(th); 3514789Sahrens mutex_enter(&spa->spa_scrub_lock); 3515789Sahrens if (error != EAGAIN) 3516789Sahrens break; 3517789Sahrens } 3518789Sahrens 3519789Sahrens while (spa->spa_scrub_inflight) 3520789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3521789Sahrens 35221601Sbonwick spa->spa_scrub_active = 0; 35231601Sbonwick cv_broadcast(&spa->spa_scrub_cv); 35241601Sbonwick 35251601Sbonwick mutex_exit(&spa->spa_scrub_lock); 35261601Sbonwick 35271601Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 35281601Sbonwick 35291601Sbonwick mutex_enter(&spa->spa_scrub_lock); 35301601Sbonwick 35311601Sbonwick /* 35321601Sbonwick * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 35331601Sbonwick * AND the spa config lock to synchronize with any config changes 35341601Sbonwick * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 35351601Sbonwick */ 3536789Sahrens if (spa->spa_scrub_restart_txg != 0) 3537789Sahrens error = ERESTART; 3538789Sahrens 35391544Seschrock if (spa->spa_scrub_stop) 35401544Seschrock error = EINTR; 35411544Seschrock 3542789Sahrens /* 35431544Seschrock * Even if there were uncorrectable errors, we consider the scrub 35441544Seschrock * completed. The downside is that if there is a transient error during 35451544Seschrock * a resilver, we won't resilver the data properly to the target. But 35461544Seschrock * if the damage is permanent (more likely) we will resilver forever, 35471544Seschrock * which isn't really acceptable. Since there is enough information for 35481544Seschrock * the user to know what has failed and why, this seems like a more 35491544Seschrock * tractable approach. 3550789Sahrens */ 35511544Seschrock complete = (error == 0); 3552789Sahrens 35531544Seschrock dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 35541544Seschrock scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 3555789Sahrens spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 3556789Sahrens error, spa->spa_scrub_errors, spa->spa_scrub_stop); 3557789Sahrens 3558789Sahrens mutex_exit(&spa->spa_scrub_lock); 3559789Sahrens 3560789Sahrens /* 3561789Sahrens * If the scrub/resilver completed, update all DTLs to reflect this. 3562789Sahrens * Whether it succeeded or not, vacate all temporary scrub DTLs. 3563789Sahrens */ 3564789Sahrens vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 3565789Sahrens complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 3566789Sahrens vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 35671544Seschrock spa_errlog_rotate(spa); 35681601Sbonwick 35694451Seschrock if (scrub_type == POOL_SCRUB_RESILVER && complete) 35704451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 35714451Seschrock 35721544Seschrock spa_config_exit(spa, FTAG); 3573789Sahrens 3574789Sahrens mutex_enter(&spa->spa_scrub_lock); 3575789Sahrens 35761544Seschrock /* 35771544Seschrock * We may have finished replacing a device. 35781544Seschrock * Let the async thread assess this and handle the detach. 35791544Seschrock */ 35804451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3581789Sahrens 3582789Sahrens /* 3583789Sahrens * If we were told to restart, our final act is to start a new scrub. 3584789Sahrens */ 3585789Sahrens if (error == ERESTART) 35861544Seschrock spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 35871544Seschrock SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 3588789Sahrens 35891544Seschrock spa->spa_scrub_type = POOL_SCRUB_NONE; 35901544Seschrock spa->spa_scrub_active = 0; 35911544Seschrock spa->spa_scrub_thread = NULL; 35921544Seschrock cv_broadcast(&spa->spa_scrub_cv); 3593789Sahrens CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 3594789Sahrens thread_exit(); 3595789Sahrens } 3596789Sahrens 3597789Sahrens void 3598789Sahrens spa_scrub_suspend(spa_t *spa) 3599789Sahrens { 3600789Sahrens mutex_enter(&spa->spa_scrub_lock); 36011544Seschrock spa->spa_scrub_suspended++; 3602789Sahrens while (spa->spa_scrub_active) { 3603789Sahrens cv_broadcast(&spa->spa_scrub_cv); 3604789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3605789Sahrens } 3606789Sahrens while (spa->spa_scrub_inflight) 3607789Sahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3608789Sahrens mutex_exit(&spa->spa_scrub_lock); 3609789Sahrens } 3610789Sahrens 3611789Sahrens void 3612789Sahrens spa_scrub_resume(spa_t *spa) 3613789Sahrens { 3614789Sahrens mutex_enter(&spa->spa_scrub_lock); 36151544Seschrock ASSERT(spa->spa_scrub_suspended != 0); 36161544Seschrock if (--spa->spa_scrub_suspended == 0) 3617789Sahrens cv_broadcast(&spa->spa_scrub_cv); 3618789Sahrens mutex_exit(&spa->spa_scrub_lock); 3619789Sahrens } 3620789Sahrens 3621789Sahrens void 3622789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg) 3623789Sahrens { 3624789Sahrens /* 3625789Sahrens * Something happened (e.g. snapshot create/delete) that means 3626789Sahrens * we must restart any in-progress scrubs. The itinerary will 3627789Sahrens * fix this properly. 3628789Sahrens */ 3629789Sahrens mutex_enter(&spa->spa_scrub_lock); 3630789Sahrens spa->spa_scrub_restart_txg = txg; 3631789Sahrens mutex_exit(&spa->spa_scrub_lock); 3632789Sahrens } 3633789Sahrens 36341544Seschrock int 36351544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 3636789Sahrens { 3637789Sahrens space_seg_t *ss; 3638789Sahrens uint64_t mintxg, maxtxg; 3639789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3640789Sahrens 36414808Sek110237 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 36424808Sek110237 ASSERT(!spa_config_held(spa, RW_WRITER)); 36434808Sek110237 3644789Sahrens if ((uint_t)type >= POOL_SCRUB_TYPES) 3645789Sahrens return (ENOTSUP); 3646789Sahrens 36471544Seschrock mutex_enter(&spa->spa_scrub_lock); 36481544Seschrock 3649789Sahrens /* 3650789Sahrens * If there's a scrub or resilver already in progress, stop it. 3651789Sahrens */ 3652789Sahrens while (spa->spa_scrub_thread != NULL) { 3653789Sahrens /* 3654789Sahrens * Don't stop a resilver unless forced. 3655789Sahrens */ 36561544Seschrock if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 36571544Seschrock mutex_exit(&spa->spa_scrub_lock); 3658789Sahrens return (EBUSY); 36591544Seschrock } 3660789Sahrens spa->spa_scrub_stop = 1; 3661789Sahrens cv_broadcast(&spa->spa_scrub_cv); 3662789Sahrens cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 3663789Sahrens } 3664789Sahrens 3665789Sahrens /* 3666789Sahrens * Terminate the previous traverse. 3667789Sahrens */ 3668789Sahrens if (spa->spa_scrub_th != NULL) { 3669789Sahrens traverse_fini(spa->spa_scrub_th); 3670789Sahrens spa->spa_scrub_th = NULL; 3671789Sahrens } 3672789Sahrens 36731544Seschrock if (rvd == NULL) { 36741544Seschrock ASSERT(spa->spa_scrub_stop == 0); 36751544Seschrock ASSERT(spa->spa_scrub_type == type); 36761544Seschrock ASSERT(spa->spa_scrub_restart_txg == 0); 36771544Seschrock mutex_exit(&spa->spa_scrub_lock); 36781544Seschrock return (0); 36791544Seschrock } 3680789Sahrens 3681789Sahrens mintxg = TXG_INITIAL - 1; 3682789Sahrens maxtxg = spa_last_synced_txg(spa) + 1; 3683789Sahrens 36841544Seschrock mutex_enter(&rvd->vdev_dtl_lock); 3685789Sahrens 36861544Seschrock if (rvd->vdev_dtl_map.sm_space == 0) { 36871544Seschrock /* 36881544Seschrock * The pool-wide DTL is empty. 36891732Sbonwick * If this is a resilver, there's nothing to do except 36901732Sbonwick * check whether any in-progress replacements have completed. 36911544Seschrock */ 36921732Sbonwick if (type == POOL_SCRUB_RESILVER) { 36931544Seschrock type = POOL_SCRUB_NONE; 36944451Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 36951732Sbonwick } 36961544Seschrock } else { 36971544Seschrock /* 36981544Seschrock * The pool-wide DTL is non-empty. 36991544Seschrock * If this is a normal scrub, upgrade to a resilver instead. 37001544Seschrock */ 37011544Seschrock if (type == POOL_SCRUB_EVERYTHING) 37021544Seschrock type = POOL_SCRUB_RESILVER; 37031544Seschrock } 3704789Sahrens 37051544Seschrock if (type == POOL_SCRUB_RESILVER) { 3706789Sahrens /* 3707789Sahrens * Determine the resilvering boundaries. 3708789Sahrens * 3709789Sahrens * Note: (mintxg, maxtxg) is an open interval, 3710789Sahrens * i.e. mintxg and maxtxg themselves are not included. 3711789Sahrens * 3712789Sahrens * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 3713789Sahrens * so we don't claim to resilver a txg that's still changing. 3714789Sahrens */ 3715789Sahrens ss = avl_first(&rvd->vdev_dtl_map.sm_root); 37161544Seschrock mintxg = ss->ss_start - 1; 3717789Sahrens ss = avl_last(&rvd->vdev_dtl_map.sm_root); 37181544Seschrock maxtxg = MIN(ss->ss_end, maxtxg); 37194451Seschrock 37204451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 3721789Sahrens } 3722789Sahrens 37231544Seschrock mutex_exit(&rvd->vdev_dtl_lock); 37241544Seschrock 37251544Seschrock spa->spa_scrub_stop = 0; 37261544Seschrock spa->spa_scrub_type = type; 37271544Seschrock spa->spa_scrub_restart_txg = 0; 37281544Seschrock 37291544Seschrock if (type != POOL_SCRUB_NONE) { 37301544Seschrock spa->spa_scrub_mintxg = mintxg; 3731789Sahrens spa->spa_scrub_maxtxg = maxtxg; 3732789Sahrens spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 37331635Sbonwick ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 37341635Sbonwick ZIO_FLAG_CANFAIL); 3735789Sahrens traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 3736789Sahrens spa->spa_scrub_thread = thread_create(NULL, 0, 3737789Sahrens spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 3738789Sahrens } 3739789Sahrens 37401544Seschrock mutex_exit(&spa->spa_scrub_lock); 37411544Seschrock 3742789Sahrens return (0); 3743789Sahrens } 3744789Sahrens 37451544Seschrock /* 37461544Seschrock * ========================================================================== 37471544Seschrock * SPA async task processing 37481544Seschrock * ========================================================================== 37491544Seschrock */ 37501544Seschrock 37511544Seschrock static void 37524451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 3753789Sahrens { 37541544Seschrock vdev_t *tvd; 37551544Seschrock int c; 37561544Seschrock 37574451Seschrock for (c = 0; c < vd->vdev_children; c++) { 37584451Seschrock tvd = vd->vdev_child[c]; 37594451Seschrock if (tvd->vdev_remove_wanted) { 37604451Seschrock tvd->vdev_remove_wanted = 0; 37614451Seschrock vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 37624451Seschrock VDEV_AUX_NONE); 37635329Sgw25295 vdev_clear(spa, tvd, B_TRUE); 37644451Seschrock vdev_config_dirty(tvd->vdev_top); 37651544Seschrock } 37664451Seschrock spa_async_remove(spa, tvd); 37671544Seschrock } 37681544Seschrock } 37691544Seschrock 37701544Seschrock static void 37711544Seschrock spa_async_thread(spa_t *spa) 37721544Seschrock { 37731544Seschrock int tasks; 37744451Seschrock uint64_t txg; 37751544Seschrock 37761544Seschrock ASSERT(spa->spa_sync_on); 3777789Sahrens 37781544Seschrock mutex_enter(&spa->spa_async_lock); 37791544Seschrock tasks = spa->spa_async_tasks; 37801544Seschrock spa->spa_async_tasks = 0; 37811544Seschrock mutex_exit(&spa->spa_async_lock); 37821544Seschrock 37831544Seschrock /* 37841635Sbonwick * See if the config needs to be updated. 37851635Sbonwick */ 37861635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 37871635Sbonwick mutex_enter(&spa_namespace_lock); 37881635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 37891635Sbonwick mutex_exit(&spa_namespace_lock); 37901635Sbonwick } 37911635Sbonwick 37921635Sbonwick /* 37934451Seschrock * See if any devices need to be marked REMOVED. 37945329Sgw25295 * 37955329Sgw25295 * XXX - We avoid doing this when we are in 37965329Sgw25295 * I/O failure state since spa_vdev_enter() grabs 37975329Sgw25295 * the namespace lock and would not be able to obtain 37985329Sgw25295 * the writer config lock. 37991544Seschrock */ 38005329Sgw25295 if (tasks & SPA_ASYNC_REMOVE && 38015329Sgw25295 spa_state(spa) != POOL_STATE_IO_FAILURE) { 38024451Seschrock txg = spa_vdev_enter(spa); 38034451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 38044451Seschrock (void) spa_vdev_exit(spa, NULL, txg, 0); 38054451Seschrock } 38061544Seschrock 38071544Seschrock /* 38081544Seschrock * If any devices are done replacing, detach them. 38091544Seschrock */ 38104451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 38114451Seschrock spa_vdev_resilver_done(spa); 3812789Sahrens 38131544Seschrock /* 38144451Seschrock * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 38154451Seschrock * scrub which can become a resilver), we need to hold 38164451Seschrock * spa_namespace_lock() because the sysevent we post via 38174451Seschrock * spa_event_notify() needs to get the name of the pool. 38181544Seschrock */ 38194451Seschrock if (tasks & SPA_ASYNC_SCRUB) { 38204451Seschrock mutex_enter(&spa_namespace_lock); 38211544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 38224451Seschrock mutex_exit(&spa_namespace_lock); 38234451Seschrock } 38241544Seschrock 38251544Seschrock /* 38261544Seschrock * Kick off a resilver. 38271544Seschrock */ 38284451Seschrock if (tasks & SPA_ASYNC_RESILVER) { 38294451Seschrock mutex_enter(&spa_namespace_lock); 38301544Seschrock VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 38314451Seschrock mutex_exit(&spa_namespace_lock); 38324451Seschrock } 38331544Seschrock 38341544Seschrock /* 38351544Seschrock * Let the world know that we're done. 38361544Seschrock */ 38371544Seschrock mutex_enter(&spa->spa_async_lock); 38381544Seschrock spa->spa_async_thread = NULL; 38391544Seschrock cv_broadcast(&spa->spa_async_cv); 38401544Seschrock mutex_exit(&spa->spa_async_lock); 38411544Seschrock thread_exit(); 38421544Seschrock } 38431544Seschrock 38441544Seschrock void 38451544Seschrock spa_async_suspend(spa_t *spa) 38461544Seschrock { 38471544Seschrock mutex_enter(&spa->spa_async_lock); 38481544Seschrock spa->spa_async_suspended++; 38491544Seschrock while (spa->spa_async_thread != NULL) 38501544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 38511544Seschrock mutex_exit(&spa->spa_async_lock); 38521544Seschrock } 38531544Seschrock 38541544Seschrock void 38551544Seschrock spa_async_resume(spa_t *spa) 38561544Seschrock { 38571544Seschrock mutex_enter(&spa->spa_async_lock); 38581544Seschrock ASSERT(spa->spa_async_suspended != 0); 38591544Seschrock spa->spa_async_suspended--; 38601544Seschrock mutex_exit(&spa->spa_async_lock); 38611544Seschrock } 38621544Seschrock 38631544Seschrock static void 38641544Seschrock spa_async_dispatch(spa_t *spa) 38651544Seschrock { 38661544Seschrock mutex_enter(&spa->spa_async_lock); 38671544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 38681635Sbonwick spa->spa_async_thread == NULL && 38691635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 38701544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 38711544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 38721544Seschrock mutex_exit(&spa->spa_async_lock); 38731544Seschrock } 38741544Seschrock 38751544Seschrock void 38761544Seschrock spa_async_request(spa_t *spa, int task) 38771544Seschrock { 38781544Seschrock mutex_enter(&spa->spa_async_lock); 38791544Seschrock spa->spa_async_tasks |= task; 38801544Seschrock mutex_exit(&spa->spa_async_lock); 3881789Sahrens } 3882789Sahrens 3883789Sahrens /* 3884789Sahrens * ========================================================================== 3885789Sahrens * SPA syncing routines 3886789Sahrens * ========================================================================== 3887789Sahrens */ 3888789Sahrens 3889789Sahrens static void 3890789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 3891789Sahrens { 3892789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 3893789Sahrens dmu_tx_t *tx; 3894789Sahrens blkptr_t blk; 3895789Sahrens uint64_t itor = 0; 3896789Sahrens zio_t *zio; 3897789Sahrens int error; 3898789Sahrens uint8_t c = 1; 3899789Sahrens 3900789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 3901789Sahrens 3902789Sahrens while (bplist_iterate(bpl, &itor, &blk) == 0) 3903789Sahrens zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 3904789Sahrens 3905789Sahrens error = zio_wait(zio); 3906789Sahrens ASSERT3U(error, ==, 0); 3907789Sahrens 3908789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3909789Sahrens bplist_vacate(bpl, tx); 3910789Sahrens 3911789Sahrens /* 3912789Sahrens * Pre-dirty the first block so we sync to convergence faster. 3913789Sahrens * (Usually only the first block is needed.) 3914789Sahrens */ 3915789Sahrens dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 3916789Sahrens dmu_tx_commit(tx); 3917789Sahrens } 3918789Sahrens 3919789Sahrens static void 39202082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 39212082Seschrock { 39222082Seschrock char *packed = NULL; 39232082Seschrock size_t nvsize = 0; 39242082Seschrock dmu_buf_t *db; 39252082Seschrock 39262082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 39272082Seschrock 39282082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 39292082Seschrock 39302082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 39312082Seschrock KM_SLEEP) == 0); 39322082Seschrock 39332082Seschrock dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 39342082Seschrock 39352082Seschrock kmem_free(packed, nvsize); 39362082Seschrock 39372082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 39382082Seschrock dmu_buf_will_dirty(db, tx); 39392082Seschrock *(uint64_t *)db->db_data = nvsize; 39402082Seschrock dmu_buf_rele(db, FTAG); 39412082Seschrock } 39422082Seschrock 39432082Seschrock static void 39445450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 39455450Sbrendan const char *config, const char *entry) 39462082Seschrock { 39472082Seschrock nvlist_t *nvroot; 39485450Sbrendan nvlist_t **list; 39492082Seschrock int i; 39502082Seschrock 39515450Sbrendan if (!sav->sav_sync) 39522082Seschrock return; 39532082Seschrock 39542082Seschrock /* 39555450Sbrendan * Update the MOS nvlist describing the list of available devices. 39565450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 39574451Seschrock * valid and the vdevs are labeled appropriately. 39582082Seschrock */ 39595450Sbrendan if (sav->sav_object == 0) { 39605450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 39615450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 39625450Sbrendan sizeof (uint64_t), tx); 39632082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 39645450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 39655450Sbrendan &sav->sav_object, tx) == 0); 39662082Seschrock } 39672082Seschrock 39682082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 39695450Sbrendan if (sav->sav_count == 0) { 39705450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 39712082Seschrock } else { 39725450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 39735450Sbrendan for (i = 0; i < sav->sav_count; i++) 39745450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 39755450Sbrendan B_FALSE, B_FALSE, B_TRUE); 39765450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 39775450Sbrendan sav->sav_count) == 0); 39785450Sbrendan for (i = 0; i < sav->sav_count; i++) 39795450Sbrendan nvlist_free(list[i]); 39805450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 39812082Seschrock } 39822082Seschrock 39835450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 39842926Sek110237 nvlist_free(nvroot); 39852082Seschrock 39865450Sbrendan sav->sav_sync = B_FALSE; 39872082Seschrock } 39882082Seschrock 39892082Seschrock static void 3990789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 3991789Sahrens { 3992789Sahrens nvlist_t *config; 3993789Sahrens 3994789Sahrens if (list_is_empty(&spa->spa_dirty_list)) 3995789Sahrens return; 3996789Sahrens 3997789Sahrens config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 3998789Sahrens 39991635Sbonwick if (spa->spa_config_syncing) 40001635Sbonwick nvlist_free(spa->spa_config_syncing); 40011635Sbonwick spa->spa_config_syncing = config; 4002789Sahrens 40032082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 4004789Sahrens } 4005789Sahrens 40065094Slling /* 40075094Slling * Set zpool properties. 40085094Slling */ 40093912Slling static void 40104543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 40113912Slling { 40123912Slling spa_t *spa = arg1; 40135094Slling objset_t *mos = spa->spa_meta_objset; 40143912Slling nvlist_t *nvp = arg2; 40155094Slling nvpair_t *elem; 40164451Seschrock uint64_t intval; 4017*6643Seschrock char *strval; 40185094Slling zpool_prop_t prop; 40195094Slling const char *propname; 40205094Slling zprop_type_t proptype; 4021*6643Seschrock spa_config_dirent_t *dp; 40225094Slling 40235094Slling elem = NULL; 40245094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 40255094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 40265094Slling case ZPOOL_PROP_VERSION: 40275094Slling /* 40285094Slling * Only set version for non-zpool-creation cases 40295094Slling * (set/import). spa_create() needs special care 40305094Slling * for version setting. 40315094Slling */ 40325094Slling if (tx->tx_txg != TXG_INITIAL) { 40335094Slling VERIFY(nvpair_value_uint64(elem, 40345094Slling &intval) == 0); 40355094Slling ASSERT(intval <= SPA_VERSION); 40365094Slling ASSERT(intval >= spa_version(spa)); 40375094Slling spa->spa_uberblock.ub_version = intval; 40385094Slling vdev_config_dirty(spa->spa_root_vdev); 40395094Slling } 40405094Slling break; 40415094Slling 40425094Slling case ZPOOL_PROP_ALTROOT: 40435094Slling /* 40445094Slling * 'altroot' is a non-persistent property. It should 40455094Slling * have been set temporarily at creation or import time. 40465094Slling */ 40475094Slling ASSERT(spa->spa_root != NULL); 40485094Slling break; 40495094Slling 40505363Seschrock case ZPOOL_PROP_CACHEFILE: 40515094Slling /* 40525363Seschrock * 'cachefile' is a non-persistent property, but note 40535363Seschrock * an async request that the config cache needs to be 40545363Seschrock * udpated. 40555094Slling */ 40565363Seschrock VERIFY(nvpair_value_string(elem, &strval) == 0); 4057*6643Seschrock 4058*6643Seschrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 4059*6643Seschrock KM_SLEEP); 4060*6643Seschrock 4061*6643Seschrock if (strval[0] == '\0') 4062*6643Seschrock dp->scd_path = spa_strdup(spa_config_path); 4063*6643Seschrock else if (strcmp(strval, "none") == 0) 4064*6643Seschrock dp->scd_path = NULL; 4065*6643Seschrock else 4066*6643Seschrock dp->scd_path = spa_strdup(strval); 4067*6643Seschrock 4068*6643Seschrock list_insert_head(&spa->spa_config_list, dp); 40695363Seschrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 40704543Smarks break; 40715094Slling default: 40725094Slling /* 40735094Slling * Set pool property values in the poolprops mos object. 40745094Slling */ 40755094Slling mutex_enter(&spa->spa_props_lock); 40765094Slling if (spa->spa_pool_props_object == 0) { 40775094Slling objset_t *mos = spa->spa_meta_objset; 40785094Slling 40795094Slling VERIFY((spa->spa_pool_props_object = 40805094Slling zap_create(mos, DMU_OT_POOL_PROPS, 40815094Slling DMU_OT_NONE, 0, tx)) > 0); 40825094Slling 40835094Slling VERIFY(zap_update(mos, 40845094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 40855094Slling 8, 1, &spa->spa_pool_props_object, tx) 40865094Slling == 0); 40875094Slling } 40885094Slling mutex_exit(&spa->spa_props_lock); 40895094Slling 40905094Slling /* normalize the property name */ 40915094Slling propname = zpool_prop_to_name(prop); 40925094Slling proptype = zpool_prop_get_type(prop); 40935094Slling 40945094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 40955094Slling ASSERT(proptype == PROP_TYPE_STRING); 40965094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 40975094Slling VERIFY(zap_update(mos, 40985094Slling spa->spa_pool_props_object, propname, 40995094Slling 1, strlen(strval) + 1, strval, tx) == 0); 41005094Slling 41015094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 41025094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 41035094Slling 41045094Slling if (proptype == PROP_TYPE_INDEX) { 41055094Slling const char *unused; 41065094Slling VERIFY(zpool_prop_index_to_string( 41075094Slling prop, intval, &unused) == 0); 41085094Slling } 41095094Slling VERIFY(zap_update(mos, 41105094Slling spa->spa_pool_props_object, propname, 41115094Slling 8, 1, &intval, tx) == 0); 41125094Slling } else { 41135094Slling ASSERT(0); /* not allowed */ 41145094Slling } 41155094Slling 41165329Sgw25295 switch (prop) { 41175329Sgw25295 case ZPOOL_PROP_DELEGATION: 41185094Slling spa->spa_delegation = intval; 41195329Sgw25295 break; 41205329Sgw25295 case ZPOOL_PROP_BOOTFS: 41215094Slling spa->spa_bootfs = intval; 41225329Sgw25295 break; 41235329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 41245329Sgw25295 spa->spa_failmode = intval; 41255329Sgw25295 break; 41265329Sgw25295 default: 41275329Sgw25295 break; 41285329Sgw25295 } 41293912Slling } 41305094Slling 41315094Slling /* log internal history if this is not a zpool create */ 41325094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 41335094Slling tx->tx_txg != TXG_INITIAL) { 41345094Slling spa_history_internal_log(LOG_POOL_PROPSET, 41355094Slling spa, tx, cr, "%s %lld %s", 41365094Slling nvpair_name(elem), intval, spa->spa_name); 41375094Slling } 41383912Slling } 41393912Slling } 41403912Slling 4141789Sahrens /* 4142789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 4143789Sahrens * part of the process, so we iterate until it converges. 4144789Sahrens */ 4145789Sahrens void 4146789Sahrens spa_sync(spa_t *spa, uint64_t txg) 4147789Sahrens { 4148789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 4149789Sahrens objset_t *mos = spa->spa_meta_objset; 4150789Sahrens bplist_t *bpl = &spa->spa_sync_bplist; 41511635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 4152789Sahrens vdev_t *vd; 4153789Sahrens dmu_tx_t *tx; 4154789Sahrens int dirty_vdevs; 4155789Sahrens 4156789Sahrens /* 4157789Sahrens * Lock out configuration changes. 4158789Sahrens */ 41591544Seschrock spa_config_enter(spa, RW_READER, FTAG); 4160789Sahrens 4161789Sahrens spa->spa_syncing_txg = txg; 4162789Sahrens spa->spa_sync_pass = 0; 4163789Sahrens 41641544Seschrock VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 4165789Sahrens 41662082Seschrock tx = dmu_tx_create_assigned(dp, txg); 41672082Seschrock 41682082Seschrock /* 41694577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 41702082Seschrock * set spa_deflate if we have no raid-z vdevs. 41712082Seschrock */ 41724577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 41734577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 41742082Seschrock int i; 41752082Seschrock 41762082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 41772082Seschrock vd = rvd->vdev_child[i]; 41782082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 41792082Seschrock break; 41802082Seschrock } 41812082Seschrock if (i == rvd->vdev_children) { 41822082Seschrock spa->spa_deflate = TRUE; 41832082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 41842082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 41852082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 41862082Seschrock } 41872082Seschrock } 41882082Seschrock 4189789Sahrens /* 4190789Sahrens * If anything has changed in this txg, push the deferred frees 4191789Sahrens * from the previous txg. If not, leave them alone so that we 4192789Sahrens * don't generate work on an otherwise idle system. 4193789Sahrens */ 4194789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 41952329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 41962329Sek110237 !txg_list_empty(&dp->dp_sync_tasks, txg)) 4197789Sahrens spa_sync_deferred_frees(spa, txg); 4198789Sahrens 4199789Sahrens /* 4200789Sahrens * Iterate to convergence. 4201789Sahrens */ 4202789Sahrens do { 4203789Sahrens spa->spa_sync_pass++; 4204789Sahrens 4205789Sahrens spa_sync_config_object(spa, tx); 42065450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 42075450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 42085450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 42095450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 42101544Seschrock spa_errlog_sync(spa, txg); 4211789Sahrens dsl_pool_sync(dp, txg); 4212789Sahrens 4213789Sahrens dirty_vdevs = 0; 4214789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 4215789Sahrens vdev_sync(vd, txg); 4216789Sahrens dirty_vdevs++; 4217789Sahrens } 4218789Sahrens 4219789Sahrens bplist_sync(bpl, tx); 4220789Sahrens } while (dirty_vdevs); 4221789Sahrens 4222789Sahrens bplist_close(bpl); 4223789Sahrens 4224789Sahrens dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 4225789Sahrens 4226789Sahrens /* 4227789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 4228789Sahrens * to commit the transaction group. 42291635Sbonwick * 42305688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 42315688Sbonwick * random top-level vdevs that are known to be visible in the 42325688Sbonwick * config cache (see spa_vdev_add() for details). If there *are* 42335688Sbonwick * dirty vdevs -- or if the sync to our random subset fails -- 42345688Sbonwick * then sync the uberblock to all vdevs. 4235789Sahrens */ 42365688Sbonwick if (list_is_empty(&spa->spa_dirty_list)) { 42376615Sgw25295 vdev_t *svd[SPA_DVAS_PER_BP]; 42386615Sgw25295 int svdcount = 0; 42391635Sbonwick int children = rvd->vdev_children; 42401635Sbonwick int c0 = spa_get_random(children); 42411635Sbonwick int c; 42421635Sbonwick 42431635Sbonwick for (c = 0; c < children; c++) { 42441635Sbonwick vd = rvd->vdev_child[(c0 + c) % children]; 42455688Sbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 42461635Sbonwick continue; 42475688Sbonwick svd[svdcount++] = vd; 42485688Sbonwick if (svdcount == SPA_DVAS_PER_BP) 42491635Sbonwick break; 42501635Sbonwick } 42516615Sgw25295 vdev_config_sync(svd, svdcount, txg); 42526615Sgw25295 } else { 42536615Sgw25295 vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg); 42541635Sbonwick } 42552082Seschrock dmu_tx_commit(tx); 42562082Seschrock 42571635Sbonwick /* 42581635Sbonwick * Clear the dirty config list. 42591635Sbonwick */ 42601635Sbonwick while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 42611635Sbonwick vdev_config_clean(vd); 42621635Sbonwick 42631635Sbonwick /* 42641635Sbonwick * Now that the new config has synced transactionally, 42651635Sbonwick * let it become visible to the config cache. 42661635Sbonwick */ 42671635Sbonwick if (spa->spa_config_syncing != NULL) { 42681635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 42691635Sbonwick spa->spa_config_txg = txg; 42701635Sbonwick spa->spa_config_syncing = NULL; 42711635Sbonwick } 4272789Sahrens 4273789Sahrens /* 4274789Sahrens * Make a stable copy of the fully synced uberblock. 4275789Sahrens * We use this as the root for pool traversals. 4276789Sahrens */ 4277789Sahrens spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 4278789Sahrens 4279789Sahrens spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 4280789Sahrens 4281789Sahrens rw_enter(&spa->spa_traverse_lock, RW_WRITER); 4282789Sahrens spa->spa_traverse_wanted = 0; 4283789Sahrens spa->spa_ubsync = spa->spa_uberblock; 4284789Sahrens rw_exit(&spa->spa_traverse_lock); 4285789Sahrens 4286789Sahrens spa_scrub_resume(spa); /* resume scrub with new ubsync */ 4287789Sahrens 4288789Sahrens /* 4289789Sahrens * Clean up the ZIL records for the synced txg. 4290789Sahrens */ 4291789Sahrens dsl_pool_zil_clean(dp); 4292789Sahrens 4293789Sahrens /* 4294789Sahrens * Update usable space statistics. 4295789Sahrens */ 4296789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4297789Sahrens vdev_sync_done(vd, txg); 4298789Sahrens 4299789Sahrens /* 4300789Sahrens * It had better be the case that we didn't dirty anything 43012082Seschrock * since vdev_config_sync(). 4302789Sahrens */ 4303789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4304789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4305789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4306789Sahrens ASSERT(bpl->bpl_queue == NULL); 4307789Sahrens 43081544Seschrock spa_config_exit(spa, FTAG); 43091544Seschrock 43101544Seschrock /* 43111544Seschrock * If any async tasks have been requested, kick them off. 43121544Seschrock */ 43131544Seschrock spa_async_dispatch(spa); 4314789Sahrens } 4315789Sahrens 4316789Sahrens /* 4317789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 4318789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 4319789Sahrens * sync. 4320789Sahrens */ 4321789Sahrens void 4322789Sahrens spa_sync_allpools(void) 4323789Sahrens { 4324789Sahrens spa_t *spa = NULL; 4325789Sahrens mutex_enter(&spa_namespace_lock); 4326789Sahrens while ((spa = spa_next(spa)) != NULL) { 4327789Sahrens if (spa_state(spa) != POOL_STATE_ACTIVE) 4328789Sahrens continue; 4329789Sahrens spa_open_ref(spa, FTAG); 4330789Sahrens mutex_exit(&spa_namespace_lock); 4331789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 4332789Sahrens mutex_enter(&spa_namespace_lock); 4333789Sahrens spa_close(spa, FTAG); 4334789Sahrens } 4335789Sahrens mutex_exit(&spa_namespace_lock); 4336789Sahrens } 4337789Sahrens 4338789Sahrens /* 4339789Sahrens * ========================================================================== 4340789Sahrens * Miscellaneous routines 4341789Sahrens * ========================================================================== 4342789Sahrens */ 4343789Sahrens 4344789Sahrens /* 4345789Sahrens * Remove all pools in the system. 4346789Sahrens */ 4347789Sahrens void 4348789Sahrens spa_evict_all(void) 4349789Sahrens { 4350789Sahrens spa_t *spa; 4351789Sahrens 4352789Sahrens /* 4353789Sahrens * Remove all cached state. All pools should be closed now, 4354789Sahrens * so every spa in the AVL tree should be unreferenced. 4355789Sahrens */ 4356789Sahrens mutex_enter(&spa_namespace_lock); 4357789Sahrens while ((spa = spa_next(NULL)) != NULL) { 4358789Sahrens /* 43591544Seschrock * Stop async tasks. The async thread may need to detach 43601544Seschrock * a device that's been replaced, which requires grabbing 43611544Seschrock * spa_namespace_lock, so we must drop it here. 4362789Sahrens */ 4363789Sahrens spa_open_ref(spa, FTAG); 4364789Sahrens mutex_exit(&spa_namespace_lock); 43651544Seschrock spa_async_suspend(spa); 43664808Sek110237 mutex_enter(&spa_namespace_lock); 4367789Sahrens VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 4368789Sahrens spa_close(spa, FTAG); 4369789Sahrens 4370789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4371789Sahrens spa_unload(spa); 4372789Sahrens spa_deactivate(spa); 4373789Sahrens } 4374789Sahrens spa_remove(spa); 4375789Sahrens } 4376789Sahrens mutex_exit(&spa_namespace_lock); 4377789Sahrens } 43781544Seschrock 43791544Seschrock vdev_t * 4380*6643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache) 43811544Seschrock { 4382*6643Seschrock vdev_t *vd; 4383*6643Seschrock int i; 4384*6643Seschrock 4385*6643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 4386*6643Seschrock return (vd); 4387*6643Seschrock 4388*6643Seschrock if (l2cache) { 4389*6643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 4390*6643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 4391*6643Seschrock if (vd->vdev_guid == guid) 4392*6643Seschrock return (vd); 4393*6643Seschrock } 4394*6643Seschrock } 4395*6643Seschrock 4396*6643Seschrock return (NULL); 43971544Seschrock } 43981760Seschrock 43991760Seschrock void 44005094Slling spa_upgrade(spa_t *spa, uint64_t version) 44011760Seschrock { 44021760Seschrock spa_config_enter(spa, RW_WRITER, FTAG); 44031760Seschrock 44041760Seschrock /* 44051760Seschrock * This should only be called for a non-faulted pool, and since a 44061760Seschrock * future version would result in an unopenable pool, this shouldn't be 44071760Seschrock * possible. 44081760Seschrock */ 44094577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 44105094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 44115094Slling 44125094Slling spa->spa_uberblock.ub_version = version; 44131760Seschrock vdev_config_dirty(spa->spa_root_vdev); 44141760Seschrock 44151760Seschrock spa_config_exit(spa, FTAG); 44162082Seschrock 44172082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 44181760Seschrock } 44192082Seschrock 44202082Seschrock boolean_t 44212082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 44222082Seschrock { 44232082Seschrock int i; 44243377Seschrock uint64_t spareguid; 44255450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 44265450Sbrendan 44275450Sbrendan for (i = 0; i < sav->sav_count; i++) 44285450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 44292082Seschrock return (B_TRUE); 44302082Seschrock 44315450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 44325450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 44335450Sbrendan &spareguid) == 0 && spareguid == guid) 44343377Seschrock return (B_TRUE); 44353377Seschrock } 44363377Seschrock 44372082Seschrock return (B_FALSE); 44382082Seschrock } 44393912Slling 44404451Seschrock /* 44414451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 44424451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 44434451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 44444451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 44454451Seschrock * or zdb as real changes. 44464451Seschrock */ 44474451Seschrock void 44484451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 44494451Seschrock { 44504451Seschrock #ifdef _KERNEL 44514451Seschrock sysevent_t *ev; 44524451Seschrock sysevent_attr_list_t *attr = NULL; 44534451Seschrock sysevent_value_t value; 44544451Seschrock sysevent_id_t eid; 44554451Seschrock 44564451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 44574451Seschrock SE_SLEEP); 44584451Seschrock 44594451Seschrock value.value_type = SE_DATA_TYPE_STRING; 44604451Seschrock value.value.sv_string = spa_name(spa); 44614451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 44624451Seschrock goto done; 44634451Seschrock 44644451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 44654451Seschrock value.value.sv_uint64 = spa_guid(spa); 44664451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 44674451Seschrock goto done; 44684451Seschrock 44694451Seschrock if (vd) { 44704451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 44714451Seschrock value.value.sv_uint64 = vd->vdev_guid; 44724451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 44734451Seschrock SE_SLEEP) != 0) 44744451Seschrock goto done; 44754451Seschrock 44764451Seschrock if (vd->vdev_path) { 44774451Seschrock value.value_type = SE_DATA_TYPE_STRING; 44784451Seschrock value.value.sv_string = vd->vdev_path; 44794451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 44804451Seschrock &value, SE_SLEEP) != 0) 44814451Seschrock goto done; 44824451Seschrock } 44834451Seschrock } 44844451Seschrock 44855756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 44865756Seschrock goto done; 44875756Seschrock attr = NULL; 44885756Seschrock 44894451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 44904451Seschrock 44914451Seschrock done: 44924451Seschrock if (attr) 44934451Seschrock sysevent_free_attr(attr); 44944451Seschrock sysevent_free(ev); 44954451Seschrock #endif 44964451Seschrock } 4497