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 /* 2312247SGeorge.Wilson@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 27789Sahrens * This file contains all the routines used when modifying on-disk SPA state. 28789Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 29789Sahrens * pool. 30789Sahrens */ 31789Sahrens 32789Sahrens #include <sys/zfs_context.h> 331544Seschrock #include <sys/fm/fs/zfs.h> 34789Sahrens #include <sys/spa_impl.h> 35789Sahrens #include <sys/zio.h> 36789Sahrens #include <sys/zio_checksum.h> 37789Sahrens #include <sys/dmu.h> 38789Sahrens #include <sys/dmu_tx.h> 39789Sahrens #include <sys/zap.h> 40789Sahrens #include <sys/zil.h> 4110922SJeff.Bonwick@Sun.COM #include <sys/ddt.h> 42789Sahrens #include <sys/vdev_impl.h> 43789Sahrens #include <sys/metaslab.h> 4410594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h> 45789Sahrens #include <sys/uberblock_impl.h> 46789Sahrens #include <sys/txg.h> 47789Sahrens #include <sys/avl.h> 48789Sahrens #include <sys/dmu_traverse.h> 493912Slling #include <sys/dmu_objset.h> 50789Sahrens #include <sys/unique.h> 51789Sahrens #include <sys/dsl_pool.h> 523912Slling #include <sys/dsl_dataset.h> 53789Sahrens #include <sys/dsl_dir.h> 54789Sahrens #include <sys/dsl_prop.h> 553912Slling #include <sys/dsl_synctask.h> 56789Sahrens #include <sys/fs/zfs.h> 575450Sbrendan #include <sys/arc.h> 58789Sahrens #include <sys/callb.h> 593975Sek110237 #include <sys/systeminfo.h> 606423Sgw25295 #include <sys/spa_boot.h> 619816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h> 6212296SLin.Ling@Sun.COM #include <sys/dsl_scan.h> 63789Sahrens 648662SJordan.Vaughan@Sun.com #ifdef _KERNEL 6511173SJonathan.Adams@Sun.COM #include <sys/bootprops.h> 6611173SJonathan.Adams@Sun.COM #include <sys/callb.h> 6711173SJonathan.Adams@Sun.COM #include <sys/cpupart.h> 6811173SJonathan.Adams@Sun.COM #include <sys/pool.h> 6911173SJonathan.Adams@Sun.COM #include <sys/sysdc.h> 708662SJordan.Vaughan@Sun.com #include <sys/zone.h> 718662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 728662SJordan.Vaughan@Sun.com 735094Slling #include "zfs_prop.h" 745913Sperrin #include "zfs_comutil.h" 755094Slling 7611173SJonathan.Adams@Sun.COM typedef enum zti_modes { 779515SJonathan.Adams@Sun.COM zti_mode_fixed, /* value is # of threads (min 1) */ 789515SJonathan.Adams@Sun.COM zti_mode_online_percent, /* value is % of online CPUs */ 7911173SJonathan.Adams@Sun.COM zti_mode_batch, /* cpu-intensive; value is ignored */ 8011146SGeorge.Wilson@Sun.COM zti_mode_null, /* don't create a taskq */ 819515SJonathan.Adams@Sun.COM zti_nmodes 8211173SJonathan.Adams@Sun.COM } zti_modes_t; 832986Sek110237 8411146SGeorge.Wilson@Sun.COM #define ZTI_FIX(n) { zti_mode_fixed, (n) } 8511146SGeorge.Wilson@Sun.COM #define ZTI_PCT(n) { zti_mode_online_percent, (n) } 8611173SJonathan.Adams@Sun.COM #define ZTI_BATCH { zti_mode_batch, 0 } 8711146SGeorge.Wilson@Sun.COM #define ZTI_NULL { zti_mode_null, 0 } 8811146SGeorge.Wilson@Sun.COM 8911146SGeorge.Wilson@Sun.COM #define ZTI_ONE ZTI_FIX(1) 909515SJonathan.Adams@Sun.COM 919515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info { 9211146SGeorge.Wilson@Sun.COM enum zti_modes zti_mode; 9311146SGeorge.Wilson@Sun.COM uint_t zti_value; 949515SJonathan.Adams@Sun.COM } zio_taskq_info_t; 959515SJonathan.Adams@Sun.COM 969515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 9711173SJonathan.Adams@Sun.COM "issue", "issue_high", "intr", "intr_high" 989515SJonathan.Adams@Sun.COM }; 999515SJonathan.Adams@Sun.COM 10011146SGeorge.Wilson@Sun.COM /* 10111146SGeorge.Wilson@Sun.COM * Define the taskq threads for the following I/O types: 10211146SGeorge.Wilson@Sun.COM * NULL, READ, WRITE, FREE, CLAIM, and IOCTL 10311146SGeorge.Wilson@Sun.COM */ 10411146SGeorge.Wilson@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 10511146SGeorge.Wilson@Sun.COM /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 10611146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 10711173SJonathan.Adams@Sun.COM { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, 10811173SJonathan.Adams@Sun.COM { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) }, 10912450SGeorge.Wilson@Sun.COM { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11011146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11111146SGeorge.Wilson@Sun.COM { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 1129515SJonathan.Adams@Sun.COM }; 1139515SJonathan.Adams@Sun.COM 11412296SLin.Ling@Sun.COM static dsl_syncfunc_t spa_sync_props; 1157214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa); 11611422SMark.Musante@Sun.COM static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 11711422SMark.Musante@Sun.COM spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 11811422SMark.Musante@Sun.COM char **ereport); 1195094Slling 12011173SJonathan.Adams@Sun.COM uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */ 12111173SJonathan.Adams@Sun.COM id_t zio_taskq_psrset_bind = PS_NONE; 12211173SJonathan.Adams@Sun.COM boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 12311173SJonathan.Adams@Sun.COM uint_t zio_taskq_basedc = 80; /* base duty cycle */ 12411173SJonathan.Adams@Sun.COM 12511173SJonathan.Adams@Sun.COM boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 12611173SJonathan.Adams@Sun.COM 12711173SJonathan.Adams@Sun.COM /* 12811173SJonathan.Adams@Sun.COM * This (illegal) pool name is used when temporarily importing a spa_t in order 12911173SJonathan.Adams@Sun.COM * to get the vdev stats associated with the imported devices. 13011173SJonathan.Adams@Sun.COM */ 13111173SJonathan.Adams@Sun.COM #define TRYIMPORT_NAME "$import" 13211173SJonathan.Adams@Sun.COM 1335094Slling /* 1345094Slling * ========================================================================== 1355094Slling * SPA properties routines 1365094Slling * ========================================================================== 1375094Slling */ 1385094Slling 1395094Slling /* 1405094Slling * Add a (source=src, propname=propval) list to an nvlist. 1415094Slling */ 1425949Slling static void 1435094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 1445094Slling uint64_t intval, zprop_source_t src) 1455094Slling { 1465094Slling const char *propname = zpool_prop_to_name(prop); 1475094Slling nvlist_t *propval; 1485949Slling 1495949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1505949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 1515949Slling 1525949Slling if (strval != NULL) 1535949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1545949Slling else 1555949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 1565949Slling 1575949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 1585094Slling nvlist_free(propval); 1595094Slling } 1605094Slling 1615094Slling /* 1625094Slling * Get property values from the spa configuration. 1635094Slling */ 1645949Slling static void 1655094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1665094Slling { 1678525SEric.Schrock@Sun.COM uint64_t size; 16810956SGeorge.Wilson@Sun.COM uint64_t alloc; 1695094Slling uint64_t cap, version; 1705094Slling zprop_source_t src = ZPROP_SRC_NONE; 1716643Seschrock spa_config_dirent_t *dp; 1725094Slling 1737754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 1747754SJeff.Bonwick@Sun.COM 1758525SEric.Schrock@Sun.COM if (spa->spa_root_vdev != NULL) { 17610956SGeorge.Wilson@Sun.COM alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 17710922SJeff.Bonwick@Sun.COM size = metaslab_class_get_space(spa_normal_class(spa)); 1788525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 1798525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 18010956SGeorge.Wilson@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 18110956SGeorge.Wilson@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 18210956SGeorge.Wilson@Sun.COM size - alloc, src); 18310956SGeorge.Wilson@Sun.COM 18410956SGeorge.Wilson@Sun.COM cap = (size == 0) ? 0 : (alloc * 100 / size); 1858525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1868525SEric.Schrock@Sun.COM 18710922SJeff.Bonwick@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 18810922SJeff.Bonwick@Sun.COM ddt_get_pool_dedup_ratio(spa), src); 18910922SJeff.Bonwick@Sun.COM 1908525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1918525SEric.Schrock@Sun.COM spa->spa_root_vdev->vdev_state, src); 1928525SEric.Schrock@Sun.COM 1938525SEric.Schrock@Sun.COM version = spa_version(spa); 1948525SEric.Schrock@Sun.COM if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1958525SEric.Schrock@Sun.COM src = ZPROP_SRC_DEFAULT; 1968525SEric.Schrock@Sun.COM else 1978525SEric.Schrock@Sun.COM src = ZPROP_SRC_LOCAL; 1988525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 1998525SEric.Schrock@Sun.COM } 2005949Slling 2015949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 2025949Slling 2035949Slling if (spa->spa_root != NULL) 2045949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2055949Slling 0, ZPROP_SRC_LOCAL); 2065094Slling 2076643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 2086643Seschrock if (dp->scd_path == NULL) { 2095949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 2106643Seschrock "none", 0, ZPROP_SRC_LOCAL); 2116643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2125949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 2136643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2145363Seschrock } 2155363Seschrock } 2165094Slling } 2175094Slling 2185094Slling /* 2195094Slling * Get zpool property values. 2205094Slling */ 2215094Slling int 2225094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 2235094Slling { 22410922SJeff.Bonwick@Sun.COM objset_t *mos = spa->spa_meta_objset; 2255094Slling zap_cursor_t zc; 2265094Slling zap_attribute_t za; 2275094Slling int err; 2285094Slling 2295949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2305094Slling 2317754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 2327754SJeff.Bonwick@Sun.COM 2335094Slling /* 2345094Slling * Get properties from the spa config. 2355094Slling */ 2365949Slling spa_prop_get_config(spa, nvp); 2375094Slling 2385094Slling /* If no pool property object, no more prop to get. */ 23911619SGeorge.Wilson@Sun.COM if (mos == NULL || spa->spa_pool_props_object == 0) { 2405094Slling mutex_exit(&spa->spa_props_lock); 2415094Slling return (0); 2425094Slling } 2435094Slling 2445094Slling /* 2455094Slling * Get properties from the MOS pool property object. 2465094Slling */ 2475094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 2485094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 2495094Slling zap_cursor_advance(&zc)) { 2505094Slling uint64_t intval = 0; 2515094Slling char *strval = NULL; 2525094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 2535094Slling zpool_prop_t prop; 2545094Slling 2555094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 2565094Slling continue; 2575094Slling 2585094Slling switch (za.za_integer_length) { 2595094Slling case 8: 2605094Slling /* integer property */ 2615094Slling if (za.za_first_integer != 2625094Slling zpool_prop_default_numeric(prop)) 2635094Slling src = ZPROP_SRC_LOCAL; 2645094Slling 2655094Slling if (prop == ZPOOL_PROP_BOOTFS) { 2665094Slling dsl_pool_t *dp; 2675094Slling dsl_dataset_t *ds = NULL; 2685094Slling 2695094Slling dp = spa_get_dsl(spa); 2705094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2716689Smaybee if (err = dsl_dataset_hold_obj(dp, 2726689Smaybee za.za_first_integer, FTAG, &ds)) { 2735094Slling rw_exit(&dp->dp_config_rwlock); 2745094Slling break; 2755094Slling } 2765094Slling 2775094Slling strval = kmem_alloc( 2785094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2795094Slling KM_SLEEP); 2805094Slling dsl_dataset_name(ds, strval); 2816689Smaybee dsl_dataset_rele(ds, FTAG); 2825094Slling rw_exit(&dp->dp_config_rwlock); 2835094Slling } else { 2845094Slling strval = NULL; 2855094Slling intval = za.za_first_integer; 2865094Slling } 2875094Slling 2885949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2895094Slling 2905094Slling if (strval != NULL) 2915094Slling kmem_free(strval, 2925094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2935094Slling 2945094Slling break; 2955094Slling 2965094Slling case 1: 2975094Slling /* string property */ 2985094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 2995094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 3005094Slling za.za_name, 1, za.za_num_integers, strval); 3015094Slling if (err) { 3025094Slling kmem_free(strval, za.za_num_integers); 3035094Slling break; 3045094Slling } 3055949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 3065094Slling kmem_free(strval, za.za_num_integers); 3075094Slling break; 3085094Slling 3095094Slling default: 3105094Slling break; 3115094Slling } 3125094Slling } 3135094Slling zap_cursor_fini(&zc); 3145094Slling mutex_exit(&spa->spa_props_lock); 3155094Slling out: 3165094Slling if (err && err != ENOENT) { 3175094Slling nvlist_free(*nvp); 3185949Slling *nvp = NULL; 3195094Slling return (err); 3205094Slling } 3215094Slling 3225094Slling return (0); 3235094Slling } 3245094Slling 3255094Slling /* 3265094Slling * Validate the given pool properties nvlist and modify the list 3275094Slling * for the property values to be set. 3285094Slling */ 3295094Slling static int 3305094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 3315094Slling { 3325094Slling nvpair_t *elem; 3335094Slling int error = 0, reset_bootfs = 0; 3345094Slling uint64_t objnum; 3355094Slling 3365094Slling elem = NULL; 3375094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3385094Slling zpool_prop_t prop; 3395094Slling char *propname, *strval; 3405094Slling uint64_t intval; 3415094Slling objset_t *os; 3425363Seschrock char *slash; 3435094Slling 3445094Slling propname = nvpair_name(elem); 3455094Slling 3465094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 3475094Slling return (EINVAL); 3485094Slling 3495094Slling switch (prop) { 3505094Slling case ZPOOL_PROP_VERSION: 3515094Slling error = nvpair_value_uint64(elem, &intval); 3525094Slling if (!error && 3535094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 3545094Slling error = EINVAL; 3555094Slling break; 3565094Slling 3575094Slling case ZPOOL_PROP_DELEGATION: 3585094Slling case ZPOOL_PROP_AUTOREPLACE: 3597538SRichard.Morris@Sun.COM case ZPOOL_PROP_LISTSNAPS: 3609816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 3615094Slling error = nvpair_value_uint64(elem, &intval); 3625094Slling if (!error && intval > 1) 3635094Slling error = EINVAL; 3645094Slling break; 3655094Slling 3665094Slling case ZPOOL_PROP_BOOTFS: 3679630SJeff.Bonwick@Sun.COM /* 3689630SJeff.Bonwick@Sun.COM * If the pool version is less than SPA_VERSION_BOOTFS, 3699630SJeff.Bonwick@Sun.COM * or the pool is still being created (version == 0), 3709630SJeff.Bonwick@Sun.COM * the bootfs property cannot be set. 3719630SJeff.Bonwick@Sun.COM */ 3725094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3735094Slling error = ENOTSUP; 3745094Slling break; 3755094Slling } 3765094Slling 3775094Slling /* 3787042Sgw25295 * Make sure the vdev config is bootable 3795094Slling */ 3807042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3815094Slling error = ENOTSUP; 3825094Slling break; 3835094Slling } 3845094Slling 3855094Slling reset_bootfs = 1; 3865094Slling 3875094Slling error = nvpair_value_string(elem, &strval); 3885094Slling 3895094Slling if (!error) { 3907042Sgw25295 uint64_t compress; 3917042Sgw25295 3925094Slling if (strval == NULL || strval[0] == '\0') { 3935094Slling objnum = zpool_prop_default_numeric( 3945094Slling ZPOOL_PROP_BOOTFS); 3955094Slling break; 3965094Slling } 3975094Slling 39810298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(strval, FTAG, &os)) 3995094Slling break; 4007042Sgw25295 40110298SMatthew.Ahrens@Sun.COM /* Must be ZPL and not gzip compressed. */ 40210298SMatthew.Ahrens@Sun.COM 40310298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 40410298SMatthew.Ahrens@Sun.COM error = ENOTSUP; 40510298SMatthew.Ahrens@Sun.COM } else if ((error = dsl_prop_get_integer(strval, 4067042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 4077042Sgw25295 &compress, NULL)) == 0 && 4087042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 4097042Sgw25295 error = ENOTSUP; 4107042Sgw25295 } else { 4117042Sgw25295 objnum = dmu_objset_id(os); 4127042Sgw25295 } 41310298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 4145094Slling } 4155094Slling break; 4167754SJeff.Bonwick@Sun.COM 4175329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 4185329Sgw25295 error = nvpair_value_uint64(elem, &intval); 4195329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 4205329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 4215329Sgw25295 error = EINVAL; 4225329Sgw25295 4235329Sgw25295 /* 4245329Sgw25295 * This is a special case which only occurs when 4255329Sgw25295 * the pool has completely failed. This allows 4265329Sgw25295 * the user to change the in-core failmode property 4275329Sgw25295 * without syncing it out to disk (I/Os might 4285329Sgw25295 * currently be blocked). We do this by returning 4295329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 4305329Sgw25295 * into thinking we encountered a property validation 4315329Sgw25295 * error. 4325329Sgw25295 */ 4337754SJeff.Bonwick@Sun.COM if (!error && spa_suspended(spa)) { 4345329Sgw25295 spa->spa_failmode = intval; 4355329Sgw25295 error = EIO; 4365329Sgw25295 } 4375329Sgw25295 break; 4385363Seschrock 4395363Seschrock case ZPOOL_PROP_CACHEFILE: 4405363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4415363Seschrock break; 4425363Seschrock 4435363Seschrock if (strval[0] == '\0') 4445363Seschrock break; 4455363Seschrock 4465363Seschrock if (strcmp(strval, "none") == 0) 4475363Seschrock break; 4485363Seschrock 4495363Seschrock if (strval[0] != '/') { 4505363Seschrock error = EINVAL; 4515363Seschrock break; 4525363Seschrock } 4535363Seschrock 4545363Seschrock slash = strrchr(strval, '/'); 4555363Seschrock ASSERT(slash != NULL); 4565363Seschrock 4575363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4585363Seschrock strcmp(slash, "/..") == 0) 4595363Seschrock error = EINVAL; 4605363Seschrock break; 46110922SJeff.Bonwick@Sun.COM 46210922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 46310922SJeff.Bonwick@Sun.COM if (spa_version(spa) < SPA_VERSION_DEDUP) 46410922SJeff.Bonwick@Sun.COM error = ENOTSUP; 46510922SJeff.Bonwick@Sun.COM else 46610922SJeff.Bonwick@Sun.COM error = nvpair_value_uint64(elem, &intval); 46710922SJeff.Bonwick@Sun.COM if (error == 0 && 46810922SJeff.Bonwick@Sun.COM intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 46910922SJeff.Bonwick@Sun.COM error = EINVAL; 47010922SJeff.Bonwick@Sun.COM break; 4715094Slling } 4725094Slling 4735094Slling if (error) 4745094Slling break; 4755094Slling } 4765094Slling 4775094Slling if (!error && reset_bootfs) { 4785094Slling error = nvlist_remove(props, 4795094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 4805094Slling 4815094Slling if (!error) { 4825094Slling error = nvlist_add_uint64(props, 4835094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 4845094Slling } 4855094Slling } 4865094Slling 4875094Slling return (error); 4885094Slling } 4895094Slling 4908525SEric.Schrock@Sun.COM void 4918525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 4928525SEric.Schrock@Sun.COM { 4938525SEric.Schrock@Sun.COM char *cachefile; 4948525SEric.Schrock@Sun.COM spa_config_dirent_t *dp; 4958525SEric.Schrock@Sun.COM 4968525SEric.Schrock@Sun.COM if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 4978525SEric.Schrock@Sun.COM &cachefile) != 0) 4988525SEric.Schrock@Sun.COM return; 4998525SEric.Schrock@Sun.COM 5008525SEric.Schrock@Sun.COM dp = kmem_alloc(sizeof (spa_config_dirent_t), 5018525SEric.Schrock@Sun.COM KM_SLEEP); 5028525SEric.Schrock@Sun.COM 5038525SEric.Schrock@Sun.COM if (cachefile[0] == '\0') 5048525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(spa_config_path); 5058525SEric.Schrock@Sun.COM else if (strcmp(cachefile, "none") == 0) 5068525SEric.Schrock@Sun.COM dp->scd_path = NULL; 5078525SEric.Schrock@Sun.COM else 5088525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(cachefile); 5098525SEric.Schrock@Sun.COM 5108525SEric.Schrock@Sun.COM list_insert_head(&spa->spa_config_list, dp); 5118525SEric.Schrock@Sun.COM if (need_sync) 5128525SEric.Schrock@Sun.COM spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 5138525SEric.Schrock@Sun.COM } 5148525SEric.Schrock@Sun.COM 5155094Slling int 5165094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 5175094Slling { 5185094Slling int error; 5198525SEric.Schrock@Sun.COM nvpair_t *elem; 5208525SEric.Schrock@Sun.COM boolean_t need_sync = B_FALSE; 5218525SEric.Schrock@Sun.COM zpool_prop_t prop; 5225094Slling 5235094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 5245094Slling return (error); 5255094Slling 5268525SEric.Schrock@Sun.COM elem = NULL; 5278525SEric.Schrock@Sun.COM while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 5288525SEric.Schrock@Sun.COM if ((prop = zpool_name_to_prop( 5298525SEric.Schrock@Sun.COM nvpair_name(elem))) == ZPROP_INVAL) 5308525SEric.Schrock@Sun.COM return (EINVAL); 5318525SEric.Schrock@Sun.COM 5328525SEric.Schrock@Sun.COM if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 5338525SEric.Schrock@Sun.COM continue; 5348525SEric.Schrock@Sun.COM 5358525SEric.Schrock@Sun.COM need_sync = B_TRUE; 5368525SEric.Schrock@Sun.COM break; 5378525SEric.Schrock@Sun.COM } 5388525SEric.Schrock@Sun.COM 5398525SEric.Schrock@Sun.COM if (need_sync) 5408525SEric.Schrock@Sun.COM return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 5418525SEric.Schrock@Sun.COM spa, nvp, 3)); 5428525SEric.Schrock@Sun.COM else 5438525SEric.Schrock@Sun.COM return (0); 5445094Slling } 5455094Slling 5465094Slling /* 5475094Slling * If the bootfs property value is dsobj, clear it. 5485094Slling */ 5495094Slling void 5505094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 5515094Slling { 5525094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 5535094Slling VERIFY(zap_remove(spa->spa_meta_objset, 5545094Slling spa->spa_pool_props_object, 5555094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 5565094Slling spa->spa_bootfs = 0; 5575094Slling } 5585094Slling } 5595094Slling 560789Sahrens /* 561789Sahrens * ========================================================================== 562789Sahrens * SPA state manipulation (open/create/destroy/import/export) 563789Sahrens * ========================================================================== 564789Sahrens */ 565789Sahrens 5661544Seschrock static int 5671544Seschrock spa_error_entry_compare(const void *a, const void *b) 5681544Seschrock { 5691544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 5701544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 5711544Seschrock int ret; 5721544Seschrock 5731544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 5741544Seschrock sizeof (zbookmark_t)); 5751544Seschrock 5761544Seschrock if (ret < 0) 5771544Seschrock return (-1); 5781544Seschrock else if (ret > 0) 5791544Seschrock return (1); 5801544Seschrock else 5811544Seschrock return (0); 5821544Seschrock } 5831544Seschrock 5841544Seschrock /* 5851544Seschrock * Utility function which retrieves copies of the current logs and 5861544Seschrock * re-initializes them in the process. 5871544Seschrock */ 5881544Seschrock void 5891544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 5901544Seschrock { 5911544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 5921544Seschrock 5931544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 5941544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 5951544Seschrock 5961544Seschrock avl_create(&spa->spa_errlist_scrub, 5971544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5981544Seschrock offsetof(spa_error_entry_t, se_avl)); 5991544Seschrock avl_create(&spa->spa_errlist_last, 6001544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 6011544Seschrock offsetof(spa_error_entry_t, se_avl)); 6021544Seschrock } 6031544Seschrock 60411173SJonathan.Adams@Sun.COM static taskq_t * 60511173SJonathan.Adams@Sun.COM spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode, 60611173SJonathan.Adams@Sun.COM uint_t value) 60711173SJonathan.Adams@Sun.COM { 60811173SJonathan.Adams@Sun.COM uint_t flags = TASKQ_PREPOPULATE; 60911173SJonathan.Adams@Sun.COM boolean_t batch = B_FALSE; 61011173SJonathan.Adams@Sun.COM 61111173SJonathan.Adams@Sun.COM switch (mode) { 61211173SJonathan.Adams@Sun.COM case zti_mode_null: 61311173SJonathan.Adams@Sun.COM return (NULL); /* no taskq needed */ 61411173SJonathan.Adams@Sun.COM 61511173SJonathan.Adams@Sun.COM case zti_mode_fixed: 61611173SJonathan.Adams@Sun.COM ASSERT3U(value, >=, 1); 61711173SJonathan.Adams@Sun.COM value = MAX(value, 1); 61811173SJonathan.Adams@Sun.COM break; 61911173SJonathan.Adams@Sun.COM 62011173SJonathan.Adams@Sun.COM case zti_mode_batch: 62111173SJonathan.Adams@Sun.COM batch = B_TRUE; 62211173SJonathan.Adams@Sun.COM flags |= TASKQ_THREADS_CPU_PCT; 62311173SJonathan.Adams@Sun.COM value = zio_taskq_batch_pct; 62411173SJonathan.Adams@Sun.COM break; 62511173SJonathan.Adams@Sun.COM 62611173SJonathan.Adams@Sun.COM case zti_mode_online_percent: 62711173SJonathan.Adams@Sun.COM flags |= TASKQ_THREADS_CPU_PCT; 62811173SJonathan.Adams@Sun.COM break; 62911173SJonathan.Adams@Sun.COM 63011173SJonathan.Adams@Sun.COM default: 63111173SJonathan.Adams@Sun.COM panic("unrecognized mode for %s taskq (%u:%u) in " 63211173SJonathan.Adams@Sun.COM "spa_activate()", 63311173SJonathan.Adams@Sun.COM name, mode, value); 63411173SJonathan.Adams@Sun.COM break; 63511173SJonathan.Adams@Sun.COM } 63611173SJonathan.Adams@Sun.COM 63711173SJonathan.Adams@Sun.COM if (zio_taskq_sysdc && spa->spa_proc != &p0) { 63811173SJonathan.Adams@Sun.COM if (batch) 63911173SJonathan.Adams@Sun.COM flags |= TASKQ_DC_BATCH; 64011173SJonathan.Adams@Sun.COM 64111173SJonathan.Adams@Sun.COM return (taskq_create_sysdc(name, value, 50, INT_MAX, 64211173SJonathan.Adams@Sun.COM spa->spa_proc, zio_taskq_basedc, flags)); 64311173SJonathan.Adams@Sun.COM } 64411173SJonathan.Adams@Sun.COM return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX, 64511173SJonathan.Adams@Sun.COM spa->spa_proc, flags)); 64611173SJonathan.Adams@Sun.COM } 64711173SJonathan.Adams@Sun.COM 64811173SJonathan.Adams@Sun.COM static void 64911173SJonathan.Adams@Sun.COM spa_create_zio_taskqs(spa_t *spa) 65011173SJonathan.Adams@Sun.COM { 65111173SJonathan.Adams@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 65211173SJonathan.Adams@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 65311173SJonathan.Adams@Sun.COM const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 65411173SJonathan.Adams@Sun.COM enum zti_modes mode = ztip->zti_mode; 65511173SJonathan.Adams@Sun.COM uint_t value = ztip->zti_value; 65611173SJonathan.Adams@Sun.COM char name[32]; 65711173SJonathan.Adams@Sun.COM 65811173SJonathan.Adams@Sun.COM (void) snprintf(name, sizeof (name), 65911173SJonathan.Adams@Sun.COM "%s_%s", zio_type_name[t], zio_taskq_types[q]); 66011173SJonathan.Adams@Sun.COM 66111173SJonathan.Adams@Sun.COM spa->spa_zio_taskq[t][q] = 66211173SJonathan.Adams@Sun.COM spa_taskq_create(spa, name, mode, value); 66311173SJonathan.Adams@Sun.COM } 66411173SJonathan.Adams@Sun.COM } 66511173SJonathan.Adams@Sun.COM } 66611173SJonathan.Adams@Sun.COM 66711173SJonathan.Adams@Sun.COM #ifdef _KERNEL 66811173SJonathan.Adams@Sun.COM static void 66911173SJonathan.Adams@Sun.COM spa_thread(void *arg) 67011173SJonathan.Adams@Sun.COM { 67111173SJonathan.Adams@Sun.COM callb_cpr_t cprinfo; 67211173SJonathan.Adams@Sun.COM 67311173SJonathan.Adams@Sun.COM spa_t *spa = arg; 67411173SJonathan.Adams@Sun.COM user_t *pu = PTOU(curproc); 67511173SJonathan.Adams@Sun.COM 67611173SJonathan.Adams@Sun.COM CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 67711173SJonathan.Adams@Sun.COM spa->spa_name); 67811173SJonathan.Adams@Sun.COM 67911173SJonathan.Adams@Sun.COM ASSERT(curproc != &p0); 68011173SJonathan.Adams@Sun.COM (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 68111173SJonathan.Adams@Sun.COM "zpool-%s", spa->spa_name); 68211173SJonathan.Adams@Sun.COM (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 68311173SJonathan.Adams@Sun.COM 68411173SJonathan.Adams@Sun.COM /* bind this thread to the requested psrset */ 68511173SJonathan.Adams@Sun.COM if (zio_taskq_psrset_bind != PS_NONE) { 68611173SJonathan.Adams@Sun.COM pool_lock(); 68711173SJonathan.Adams@Sun.COM mutex_enter(&cpu_lock); 68811173SJonathan.Adams@Sun.COM mutex_enter(&pidlock); 68911173SJonathan.Adams@Sun.COM mutex_enter(&curproc->p_lock); 69011173SJonathan.Adams@Sun.COM 69111173SJonathan.Adams@Sun.COM if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 69211173SJonathan.Adams@Sun.COM 0, NULL, NULL) == 0) { 69311173SJonathan.Adams@Sun.COM curthread->t_bind_pset = zio_taskq_psrset_bind; 69411173SJonathan.Adams@Sun.COM } else { 69511173SJonathan.Adams@Sun.COM cmn_err(CE_WARN, 69611173SJonathan.Adams@Sun.COM "Couldn't bind process for zfs pool \"%s\" to " 69711173SJonathan.Adams@Sun.COM "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 69811173SJonathan.Adams@Sun.COM } 69911173SJonathan.Adams@Sun.COM 70011173SJonathan.Adams@Sun.COM mutex_exit(&curproc->p_lock); 70111173SJonathan.Adams@Sun.COM mutex_exit(&pidlock); 70211173SJonathan.Adams@Sun.COM mutex_exit(&cpu_lock); 70311173SJonathan.Adams@Sun.COM pool_unlock(); 70411173SJonathan.Adams@Sun.COM } 70511173SJonathan.Adams@Sun.COM 70611173SJonathan.Adams@Sun.COM if (zio_taskq_sysdc) { 70711173SJonathan.Adams@Sun.COM sysdc_thread_enter(curthread, 100, 0); 70811173SJonathan.Adams@Sun.COM } 70911173SJonathan.Adams@Sun.COM 71011173SJonathan.Adams@Sun.COM spa->spa_proc = curproc; 71111173SJonathan.Adams@Sun.COM spa->spa_did = curthread->t_did; 71211173SJonathan.Adams@Sun.COM 71311173SJonathan.Adams@Sun.COM spa_create_zio_taskqs(spa); 71411173SJonathan.Adams@Sun.COM 71511173SJonathan.Adams@Sun.COM mutex_enter(&spa->spa_proc_lock); 71611173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 71711173SJonathan.Adams@Sun.COM 71811173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_ACTIVE; 71911173SJonathan.Adams@Sun.COM cv_broadcast(&spa->spa_proc_cv); 72011173SJonathan.Adams@Sun.COM 72111173SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_BEGIN(&cprinfo); 72211173SJonathan.Adams@Sun.COM while (spa->spa_proc_state == SPA_PROC_ACTIVE) 72311173SJonathan.Adams@Sun.COM cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 72411173SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 72511173SJonathan.Adams@Sun.COM 72611173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 72711173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_GONE; 72811173SJonathan.Adams@Sun.COM spa->spa_proc = &p0; 72911173SJonathan.Adams@Sun.COM cv_broadcast(&spa->spa_proc_cv); 73011173SJonathan.Adams@Sun.COM CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 73111173SJonathan.Adams@Sun.COM 73211173SJonathan.Adams@Sun.COM mutex_enter(&curproc->p_lock); 73311173SJonathan.Adams@Sun.COM lwp_exit(); 73411173SJonathan.Adams@Sun.COM } 73511173SJonathan.Adams@Sun.COM #endif 73611173SJonathan.Adams@Sun.COM 737789Sahrens /* 738789Sahrens * Activate an uninitialized pool. 739789Sahrens */ 740789Sahrens static void 7418241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode) 742789Sahrens { 743789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 744789Sahrens 745789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 7468241SJeff.Bonwick@Sun.COM spa->spa_mode = mode; 747789Sahrens 74810594SGeorge.Wilson@Sun.COM spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 74910594SGeorge.Wilson@Sun.COM spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 750789Sahrens 75111173SJonathan.Adams@Sun.COM /* Try to create a covering process */ 75211173SJonathan.Adams@Sun.COM mutex_enter(&spa->spa_proc_lock); 75311173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 75411173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc == &p0); 75511173SJonathan.Adams@Sun.COM spa->spa_did = 0; 75611173SJonathan.Adams@Sun.COM 75711173SJonathan.Adams@Sun.COM /* Only create a process if we're going to be around a while. */ 75811173SJonathan.Adams@Sun.COM if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 75911173SJonathan.Adams@Sun.COM if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 76011173SJonathan.Adams@Sun.COM NULL, 0) == 0) { 76111173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_CREATED; 76211173SJonathan.Adams@Sun.COM while (spa->spa_proc_state == SPA_PROC_CREATED) { 76311173SJonathan.Adams@Sun.COM cv_wait(&spa->spa_proc_cv, 76411173SJonathan.Adams@Sun.COM &spa->spa_proc_lock); 7659515SJonathan.Adams@Sun.COM } 76611173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 76711173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc != &p0); 76811173SJonathan.Adams@Sun.COM ASSERT(spa->spa_did != 0); 76911173SJonathan.Adams@Sun.COM } else { 77011173SJonathan.Adams@Sun.COM #ifdef _KERNEL 77111173SJonathan.Adams@Sun.COM cmn_err(CE_WARN, 77211173SJonathan.Adams@Sun.COM "Couldn't create process for zfs pool \"%s\"\n", 77311173SJonathan.Adams@Sun.COM spa->spa_name); 77411173SJonathan.Adams@Sun.COM #endif 7757754SJeff.Bonwick@Sun.COM } 776789Sahrens } 77711173SJonathan.Adams@Sun.COM mutex_exit(&spa->spa_proc_lock); 77811173SJonathan.Adams@Sun.COM 77911173SJonathan.Adams@Sun.COM /* If we didn't create a process, we need to create our taskqs. */ 78011173SJonathan.Adams@Sun.COM if (spa->spa_proc == &p0) { 78111173SJonathan.Adams@Sun.COM spa_create_zio_taskqs(spa); 78211173SJonathan.Adams@Sun.COM } 783789Sahrens 7847754SJeff.Bonwick@Sun.COM list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 7857754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_config_dirty_node)); 7867754SJeff.Bonwick@Sun.COM list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 7877754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_state_dirty_node)); 788789Sahrens 789789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 790789Sahrens offsetof(struct vdev, vdev_txg_node)); 7911544Seschrock 7921544Seschrock avl_create(&spa->spa_errlist_scrub, 7931544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 7941544Seschrock offsetof(spa_error_entry_t, se_avl)); 7951544Seschrock avl_create(&spa->spa_errlist_last, 7961544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 7971544Seschrock offsetof(spa_error_entry_t, se_avl)); 798789Sahrens } 799789Sahrens 800789Sahrens /* 801789Sahrens * Opposite of spa_activate(). 802789Sahrens */ 803789Sahrens static void 804789Sahrens spa_deactivate(spa_t *spa) 805789Sahrens { 806789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 807789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 808789Sahrens ASSERT(spa->spa_root_vdev == NULL); 8099630SJeff.Bonwick@Sun.COM ASSERT(spa->spa_async_zio_root == NULL); 810789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 811789Sahrens 812789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 813789Sahrens 8147754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_config_dirty_list); 8157754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_state_dirty_list); 8167754SJeff.Bonwick@Sun.COM 8177754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 8187754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 81911146SGeorge.Wilson@Sun.COM if (spa->spa_zio_taskq[t][q] != NULL) 82011146SGeorge.Wilson@Sun.COM taskq_destroy(spa->spa_zio_taskq[t][q]); 8217754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 8227754SJeff.Bonwick@Sun.COM } 823789Sahrens } 824789Sahrens 825789Sahrens metaslab_class_destroy(spa->spa_normal_class); 826789Sahrens spa->spa_normal_class = NULL; 827789Sahrens 8284527Sperrin metaslab_class_destroy(spa->spa_log_class); 8294527Sperrin spa->spa_log_class = NULL; 8304527Sperrin 8311544Seschrock /* 8321544Seschrock * If this was part of an import or the open otherwise failed, we may 8331544Seschrock * still have errors left in the queues. Empty them just in case. 8341544Seschrock */ 8351544Seschrock spa_errlog_drain(spa); 8361544Seschrock 8371544Seschrock avl_destroy(&spa->spa_errlist_scrub); 8381544Seschrock avl_destroy(&spa->spa_errlist_last); 8391544Seschrock 840789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 84111173SJonathan.Adams@Sun.COM 84211173SJonathan.Adams@Sun.COM mutex_enter(&spa->spa_proc_lock); 84311173SJonathan.Adams@Sun.COM if (spa->spa_proc_state != SPA_PROC_NONE) { 84411173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 84511173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_DEACTIVATE; 84611173SJonathan.Adams@Sun.COM cv_broadcast(&spa->spa_proc_cv); 84711173SJonathan.Adams@Sun.COM while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 84811173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc != &p0); 84911173SJonathan.Adams@Sun.COM cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 85011173SJonathan.Adams@Sun.COM } 85111173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 85211173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_NONE; 85311173SJonathan.Adams@Sun.COM } 85411173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc == &p0); 85511173SJonathan.Adams@Sun.COM mutex_exit(&spa->spa_proc_lock); 85611173SJonathan.Adams@Sun.COM 85711173SJonathan.Adams@Sun.COM /* 85811173SJonathan.Adams@Sun.COM * We want to make sure spa_thread() has actually exited the ZFS 85911173SJonathan.Adams@Sun.COM * module, so that the module can't be unloaded out from underneath 86011173SJonathan.Adams@Sun.COM * it. 86111173SJonathan.Adams@Sun.COM */ 86211173SJonathan.Adams@Sun.COM if (spa->spa_did != 0) { 86311173SJonathan.Adams@Sun.COM thread_join(spa->spa_did); 86411173SJonathan.Adams@Sun.COM spa->spa_did = 0; 86511173SJonathan.Adams@Sun.COM } 866789Sahrens } 867789Sahrens 868789Sahrens /* 869789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 870789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 871789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 872789Sahrens * All vdev validation is done by the vdev_alloc() routine. 873789Sahrens */ 8742082Seschrock static int 8752082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 8762082Seschrock uint_t id, int atype) 877789Sahrens { 878789Sahrens nvlist_t **child; 8799816SGeorge.Wilson@Sun.COM uint_t children; 8802082Seschrock int error; 8812082Seschrock 8822082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 8832082Seschrock return (error); 8842082Seschrock 8852082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 8862082Seschrock return (0); 887789Sahrens 8887754SJeff.Bonwick@Sun.COM error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 8897754SJeff.Bonwick@Sun.COM &child, &children); 8907754SJeff.Bonwick@Sun.COM 8917754SJeff.Bonwick@Sun.COM if (error == ENOENT) 8927754SJeff.Bonwick@Sun.COM return (0); 8937754SJeff.Bonwick@Sun.COM 8947754SJeff.Bonwick@Sun.COM if (error) { 8952082Seschrock vdev_free(*vdp); 8962082Seschrock *vdp = NULL; 8972082Seschrock return (EINVAL); 898789Sahrens } 899789Sahrens 9009816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 9012082Seschrock vdev_t *vd; 9022082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 9032082Seschrock atype)) != 0) { 9042082Seschrock vdev_free(*vdp); 9052082Seschrock *vdp = NULL; 9062082Seschrock return (error); 907789Sahrens } 908789Sahrens } 909789Sahrens 9102082Seschrock ASSERT(*vdp != NULL); 9112082Seschrock 9122082Seschrock return (0); 913789Sahrens } 914789Sahrens 915789Sahrens /* 916789Sahrens * Opposite of spa_load(). 917789Sahrens */ 918789Sahrens static void 919789Sahrens spa_unload(spa_t *spa) 920789Sahrens { 9212082Seschrock int i; 9222082Seschrock 9237754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 9247754SJeff.Bonwick@Sun.COM 925789Sahrens /* 9261544Seschrock * Stop async tasks. 9271544Seschrock */ 9281544Seschrock spa_async_suspend(spa); 9291544Seschrock 9301544Seschrock /* 931789Sahrens * Stop syncing. 932789Sahrens */ 933789Sahrens if (spa->spa_sync_on) { 934789Sahrens txg_sync_stop(spa->spa_dsl_pool); 935789Sahrens spa->spa_sync_on = B_FALSE; 936789Sahrens } 937789Sahrens 938789Sahrens /* 9397754SJeff.Bonwick@Sun.COM * Wait for any outstanding async I/O to complete. 940789Sahrens */ 9419234SGeorge.Wilson@Sun.COM if (spa->spa_async_zio_root != NULL) { 9429234SGeorge.Wilson@Sun.COM (void) zio_wait(spa->spa_async_zio_root); 9439234SGeorge.Wilson@Sun.COM spa->spa_async_zio_root = NULL; 9449234SGeorge.Wilson@Sun.COM } 945789Sahrens 94612470SMatthew.Ahrens@Sun.COM bpobj_close(&spa->spa_deferred_bpobj); 94712470SMatthew.Ahrens@Sun.COM 948789Sahrens /* 949789Sahrens * Close the dsl pool. 950789Sahrens */ 951789Sahrens if (spa->spa_dsl_pool) { 952789Sahrens dsl_pool_close(spa->spa_dsl_pool); 953789Sahrens spa->spa_dsl_pool = NULL; 95411619SGeorge.Wilson@Sun.COM spa->spa_meta_objset = NULL; 955789Sahrens } 956789Sahrens 95710922SJeff.Bonwick@Sun.COM ddt_unload(spa); 95810922SJeff.Bonwick@Sun.COM 9598241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 9608241SJeff.Bonwick@Sun.COM 9618241SJeff.Bonwick@Sun.COM /* 9628241SJeff.Bonwick@Sun.COM * Drop and purge level 2 cache 9638241SJeff.Bonwick@Sun.COM */ 9648241SJeff.Bonwick@Sun.COM spa_l2cache_drop(spa); 9658241SJeff.Bonwick@Sun.COM 966789Sahrens /* 967789Sahrens * Close all vdevs. 968789Sahrens */ 9691585Sbonwick if (spa->spa_root_vdev) 970789Sahrens vdev_free(spa->spa_root_vdev); 9711585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 9721544Seschrock 9735450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 9745450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 9755450Sbrendan if (spa->spa_spares.sav_vdevs) { 9765450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 9775450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 9785450Sbrendan spa->spa_spares.sav_vdevs = NULL; 9795450Sbrendan } 9805450Sbrendan if (spa->spa_spares.sav_config) { 9815450Sbrendan nvlist_free(spa->spa_spares.sav_config); 9825450Sbrendan spa->spa_spares.sav_config = NULL; 9832082Seschrock } 9847377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 9855450Sbrendan 9865450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 9875450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 9885450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 9895450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 9905450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 9915450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 9925450Sbrendan } 9935450Sbrendan if (spa->spa_l2cache.sav_config) { 9945450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 9955450Sbrendan spa->spa_l2cache.sav_config = NULL; 9962082Seschrock } 9977377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 9982082Seschrock 9991544Seschrock spa->spa_async_suspended = 0; 10008241SJeff.Bonwick@Sun.COM 10018241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1002789Sahrens } 1003789Sahrens 1004789Sahrens /* 10052082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 10062082Seschrock * this pool. When this is called, we have some form of basic information in 10075450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 10085450Sbrendan * then re-generate a more complete list including status information. 10092082Seschrock */ 10102082Seschrock static void 10112082Seschrock spa_load_spares(spa_t *spa) 10122082Seschrock { 10132082Seschrock nvlist_t **spares; 10142082Seschrock uint_t nspares; 10152082Seschrock int i; 10163377Seschrock vdev_t *vd, *tvd; 10172082Seschrock 10187754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 10197754SJeff.Bonwick@Sun.COM 10202082Seschrock /* 10212082Seschrock * First, close and free any existing spare vdevs. 10222082Seschrock */ 10235450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 10245450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 10253377Seschrock 10263377Seschrock /* Undo the call to spa_activate() below */ 10276643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 10286643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 10293377Seschrock spa_spare_remove(tvd); 10303377Seschrock vdev_close(vd); 10313377Seschrock vdev_free(vd); 10322082Seschrock } 10333377Seschrock 10345450Sbrendan if (spa->spa_spares.sav_vdevs) 10355450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 10365450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 10375450Sbrendan 10385450Sbrendan if (spa->spa_spares.sav_config == NULL) 10392082Seschrock nspares = 0; 10402082Seschrock else 10415450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 10422082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 10432082Seschrock 10445450Sbrendan spa->spa_spares.sav_count = (int)nspares; 10455450Sbrendan spa->spa_spares.sav_vdevs = NULL; 10462082Seschrock 10472082Seschrock if (nspares == 0) 10482082Seschrock return; 10492082Seschrock 10502082Seschrock /* 10512082Seschrock * Construct the array of vdevs, opening them to get status in the 10523377Seschrock * process. For each spare, there is potentially two different vdev_t 10533377Seschrock * structures associated with it: one in the list of spares (used only 10543377Seschrock * for basic validation purposes) and one in the active vdev 10553377Seschrock * configuration (if it's spared in). During this phase we open and 10563377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 10573377Seschrock * active configuration, then we also mark this vdev as an active spare. 10582082Seschrock */ 10595450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 10605450Sbrendan KM_SLEEP); 10615450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 10622082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 10632082Seschrock VDEV_ALLOC_SPARE) == 0); 10642082Seschrock ASSERT(vd != NULL); 10652082Seschrock 10665450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 10672082Seschrock 10686643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 10696643Seschrock B_FALSE)) != NULL) { 10703377Seschrock if (!tvd->vdev_isspare) 10713377Seschrock spa_spare_add(tvd); 10723377Seschrock 10733377Seschrock /* 10743377Seschrock * We only mark the spare active if we were successfully 10753377Seschrock * able to load the vdev. Otherwise, importing a pool 10763377Seschrock * with a bad active spare would result in strange 10773377Seschrock * behavior, because multiple pool would think the spare 10783377Seschrock * is actively in use. 10793377Seschrock * 10803377Seschrock * There is a vulnerability here to an equally bizarre 10813377Seschrock * circumstance, where a dead active spare is later 10823377Seschrock * brought back to life (onlined or otherwise). Given 10833377Seschrock * the rarity of this scenario, and the extra complexity 10843377Seschrock * it adds, we ignore the possibility. 10853377Seschrock */ 10863377Seschrock if (!vdev_is_dead(tvd)) 10873377Seschrock spa_spare_activate(tvd); 10883377Seschrock } 10893377Seschrock 10907754SJeff.Bonwick@Sun.COM vd->vdev_top = vd; 10919425SEric.Schrock@Sun.COM vd->vdev_aux = &spa->spa_spares; 10927754SJeff.Bonwick@Sun.COM 10932082Seschrock if (vdev_open(vd) != 0) 10942082Seschrock continue; 10952082Seschrock 10965450Sbrendan if (vdev_validate_aux(vd) == 0) 10975450Sbrendan spa_spare_add(vd); 10982082Seschrock } 10992082Seschrock 11002082Seschrock /* 11012082Seschrock * Recompute the stashed list of spares, with status information 11022082Seschrock * this time. 11032082Seschrock */ 11045450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 11052082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 11062082Seschrock 11075450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 11085450Sbrendan KM_SLEEP); 11095450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 11105450Sbrendan spares[i] = vdev_config_generate(spa, 111112296SLin.Ling@Sun.COM spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 11125450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 11135450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 11145450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 11152082Seschrock nvlist_free(spares[i]); 11165450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 11175450Sbrendan } 11185450Sbrendan 11195450Sbrendan /* 11205450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 11215450Sbrendan * this pool. When this is called, we have some form of basic information in 11225450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 11235450Sbrendan * then re-generate a more complete list including status information. 11245450Sbrendan * Devices which are already active have their details maintained, and are 11255450Sbrendan * not re-opened. 11265450Sbrendan */ 11275450Sbrendan static void 11285450Sbrendan spa_load_l2cache(spa_t *spa) 11295450Sbrendan { 11305450Sbrendan nvlist_t **l2cache; 11315450Sbrendan uint_t nl2cache; 11325450Sbrendan int i, j, oldnvdevs; 11339816SGeorge.Wilson@Sun.COM uint64_t guid; 11345450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 11355450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 11365450Sbrendan 11377754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 11387754SJeff.Bonwick@Sun.COM 11395450Sbrendan if (sav->sav_config != NULL) { 11405450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 11415450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 11425450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 11435450Sbrendan } else { 11445450Sbrendan nl2cache = 0; 11455450Sbrendan } 11465450Sbrendan 11475450Sbrendan oldvdevs = sav->sav_vdevs; 11485450Sbrendan oldnvdevs = sav->sav_count; 11495450Sbrendan sav->sav_vdevs = NULL; 11505450Sbrendan sav->sav_count = 0; 11515450Sbrendan 11525450Sbrendan /* 11535450Sbrendan * Process new nvlist of vdevs. 11545450Sbrendan */ 11555450Sbrendan for (i = 0; i < nl2cache; i++) { 11565450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 11575450Sbrendan &guid) == 0); 11585450Sbrendan 11595450Sbrendan newvdevs[i] = NULL; 11605450Sbrendan for (j = 0; j < oldnvdevs; j++) { 11615450Sbrendan vd = oldvdevs[j]; 11625450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 11635450Sbrendan /* 11645450Sbrendan * Retain previous vdev for add/remove ops. 11655450Sbrendan */ 11665450Sbrendan newvdevs[i] = vd; 11675450Sbrendan oldvdevs[j] = NULL; 11685450Sbrendan break; 11695450Sbrendan } 11705450Sbrendan } 11715450Sbrendan 11725450Sbrendan if (newvdevs[i] == NULL) { 11735450Sbrendan /* 11745450Sbrendan * Create new vdev 11755450Sbrendan */ 11765450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 11775450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 11785450Sbrendan ASSERT(vd != NULL); 11795450Sbrendan newvdevs[i] = vd; 11805450Sbrendan 11815450Sbrendan /* 11825450Sbrendan * Commit this vdev as an l2cache device, 11835450Sbrendan * even if it fails to open. 11845450Sbrendan */ 11855450Sbrendan spa_l2cache_add(vd); 11865450Sbrendan 11876643Seschrock vd->vdev_top = vd; 11886643Seschrock vd->vdev_aux = sav; 11896643Seschrock 11906643Seschrock spa_l2cache_activate(vd); 11916643Seschrock 11925450Sbrendan if (vdev_open(vd) != 0) 11935450Sbrendan continue; 11945450Sbrendan 11955450Sbrendan (void) vdev_validate_aux(vd); 11965450Sbrendan 11979816SGeorge.Wilson@Sun.COM if (!vdev_is_dead(vd)) 11989816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa, vd); 11995450Sbrendan } 12005450Sbrendan } 12015450Sbrendan 12025450Sbrendan /* 12035450Sbrendan * Purge vdevs that were dropped 12045450Sbrendan */ 12055450Sbrendan for (i = 0; i < oldnvdevs; i++) { 12065450Sbrendan uint64_t pool; 12075450Sbrendan 12085450Sbrendan vd = oldvdevs[i]; 12095450Sbrendan if (vd != NULL) { 12108241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 12118241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 12125450Sbrendan l2arc_remove_vdev(vd); 12135450Sbrendan (void) vdev_close(vd); 12145450Sbrendan spa_l2cache_remove(vd); 12155450Sbrendan } 12165450Sbrendan } 12175450Sbrendan 12185450Sbrendan if (oldvdevs) 12195450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 12205450Sbrendan 12215450Sbrendan if (sav->sav_config == NULL) 12225450Sbrendan goto out; 12235450Sbrendan 12245450Sbrendan sav->sav_vdevs = newvdevs; 12255450Sbrendan sav->sav_count = (int)nl2cache; 12265450Sbrendan 12275450Sbrendan /* 12285450Sbrendan * Recompute the stashed list of l2cache devices, with status 12295450Sbrendan * information this time. 12305450Sbrendan */ 12315450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 12325450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 12335450Sbrendan 12345450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 12355450Sbrendan for (i = 0; i < sav->sav_count; i++) 12365450Sbrendan l2cache[i] = vdev_config_generate(spa, 123712296SLin.Ling@Sun.COM sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 12385450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 12395450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 12405450Sbrendan out: 12415450Sbrendan for (i = 0; i < sav->sav_count; i++) 12425450Sbrendan nvlist_free(l2cache[i]); 12435450Sbrendan if (sav->sav_count) 12445450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 12452082Seschrock } 12462082Seschrock 12472082Seschrock static int 12482082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 12492082Seschrock { 12502082Seschrock dmu_buf_t *db; 12512082Seschrock char *packed = NULL; 12522082Seschrock size_t nvsize = 0; 12532082Seschrock int error; 12542082Seschrock *value = NULL; 12552082Seschrock 12562082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 12572082Seschrock nvsize = *(uint64_t *)db->db_data; 12582082Seschrock dmu_buf_rele(db, FTAG); 12592082Seschrock 12602082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 12619512SNeil.Perrin@Sun.COM error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 12629512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 12632082Seschrock if (error == 0) 12642082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 12652082Seschrock kmem_free(packed, nvsize); 12662082Seschrock 12672082Seschrock return (error); 12682082Seschrock } 12692082Seschrock 12702082Seschrock /* 12714451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 12724451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 12734451Seschrock */ 12744451Seschrock static void 12754451Seschrock spa_check_removed(vdev_t *vd) 12764451Seschrock { 12779816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 12784451Seschrock spa_check_removed(vd->vdev_child[c]); 12794451Seschrock 12804451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 12814451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 12824451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 12834451Seschrock } 12844451Seschrock } 12854451Seschrock 12864451Seschrock /* 1287*12949SGeorge.Wilson@Sun.COM * Validate the current config against the MOS config 12889701SGeorge.Wilson@Sun.COM */ 1289*12949SGeorge.Wilson@Sun.COM static boolean_t 1290*12949SGeorge.Wilson@Sun.COM spa_config_valid(spa_t *spa, nvlist_t *config) 12919701SGeorge.Wilson@Sun.COM { 1292*12949SGeorge.Wilson@Sun.COM vdev_t *mrvd, *rvd = spa->spa_root_vdev; 1293*12949SGeorge.Wilson@Sun.COM nvlist_t *nv; 1294*12949SGeorge.Wilson@Sun.COM 1295*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 1296*12949SGeorge.Wilson@Sun.COM 1297*12949SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1298*12949SGeorge.Wilson@Sun.COM VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 1299*12949SGeorge.Wilson@Sun.COM 1300*12949SGeorge.Wilson@Sun.COM ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1301*12949SGeorge.Wilson@Sun.COM 1302*12949SGeorge.Wilson@Sun.COM /* 1303*12949SGeorge.Wilson@Sun.COM * If we're doing a normal import, then build up any additional 1304*12949SGeorge.Wilson@Sun.COM * diagnostic information about missing devices in this config. 1305*12949SGeorge.Wilson@Sun.COM * We'll pass this up to the user for further processing. 1306*12949SGeorge.Wilson@Sun.COM */ 1307*12949SGeorge.Wilson@Sun.COM if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 1308*12949SGeorge.Wilson@Sun.COM nvlist_t **child, *nv; 1309*12949SGeorge.Wilson@Sun.COM uint64_t idx = 0; 1310*12949SGeorge.Wilson@Sun.COM 1311*12949SGeorge.Wilson@Sun.COM child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 1312*12949SGeorge.Wilson@Sun.COM KM_SLEEP); 1313*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1314*12949SGeorge.Wilson@Sun.COM 1315*12949SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 1316*12949SGeorge.Wilson@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 1317*12949SGeorge.Wilson@Sun.COM vdev_t *mtvd = mrvd->vdev_child[c]; 1318*12949SGeorge.Wilson@Sun.COM 1319*12949SGeorge.Wilson@Sun.COM if (tvd->vdev_ops == &vdev_missing_ops && 1320*12949SGeorge.Wilson@Sun.COM mtvd->vdev_ops != &vdev_missing_ops && 1321*12949SGeorge.Wilson@Sun.COM mtvd->vdev_islog) 1322*12949SGeorge.Wilson@Sun.COM child[idx++] = vdev_config_generate(spa, mtvd, 1323*12949SGeorge.Wilson@Sun.COM B_FALSE, 0); 1324*12949SGeorge.Wilson@Sun.COM } 1325*12949SGeorge.Wilson@Sun.COM 1326*12949SGeorge.Wilson@Sun.COM if (idx) { 1327*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist_array(nv, 1328*12949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 1329*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist(spa->spa_load_info, 1330*12949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 1331*12949SGeorge.Wilson@Sun.COM 1332*12949SGeorge.Wilson@Sun.COM for (int i = 0; i < idx; i++) 1333*12949SGeorge.Wilson@Sun.COM nvlist_free(child[i]); 1334*12949SGeorge.Wilson@Sun.COM } 1335*12949SGeorge.Wilson@Sun.COM nvlist_free(nv); 1336*12949SGeorge.Wilson@Sun.COM kmem_free(child, rvd->vdev_children * sizeof (char **)); 1337*12949SGeorge.Wilson@Sun.COM } 133810594SGeorge.Wilson@Sun.COM 133910594SGeorge.Wilson@Sun.COM /* 1340*12949SGeorge.Wilson@Sun.COM * Compare the root vdev tree with the information we have 1341*12949SGeorge.Wilson@Sun.COM * from the MOS config (mrvd). Check each top-level vdev 1342*12949SGeorge.Wilson@Sun.COM * with the corresponding MOS config top-level (mtvd). 134310594SGeorge.Wilson@Sun.COM */ 134410594SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 1345*12949SGeorge.Wilson@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 1346*12949SGeorge.Wilson@Sun.COM vdev_t *mtvd = mrvd->vdev_child[c]; 1347*12949SGeorge.Wilson@Sun.COM 1348*12949SGeorge.Wilson@Sun.COM /* 1349*12949SGeorge.Wilson@Sun.COM * Resolve any "missing" vdevs in the current configuration. 1350*12949SGeorge.Wilson@Sun.COM * If we find that the MOS config has more accurate information 1351*12949SGeorge.Wilson@Sun.COM * about the top-level vdev then use that vdev instead. 1352*12949SGeorge.Wilson@Sun.COM */ 1353*12949SGeorge.Wilson@Sun.COM if (tvd->vdev_ops == &vdev_missing_ops && 1354*12949SGeorge.Wilson@Sun.COM mtvd->vdev_ops != &vdev_missing_ops) { 1355*12949SGeorge.Wilson@Sun.COM 1356*12949SGeorge.Wilson@Sun.COM if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 1357*12949SGeorge.Wilson@Sun.COM continue; 1358*12949SGeorge.Wilson@Sun.COM 1359*12949SGeorge.Wilson@Sun.COM /* 1360*12949SGeorge.Wilson@Sun.COM * Device specific actions. 1361*12949SGeorge.Wilson@Sun.COM */ 1362*12949SGeorge.Wilson@Sun.COM if (mtvd->vdev_islog) { 1363*12949SGeorge.Wilson@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 1364*12949SGeorge.Wilson@Sun.COM } else { 1365*12949SGeorge.Wilson@Sun.COM /* 1366*12949SGeorge.Wilson@Sun.COM * XXX - once we have 'readonly' pool 1367*12949SGeorge.Wilson@Sun.COM * support we should be able to handle 1368*12949SGeorge.Wilson@Sun.COM * missing data devices by transitioning 1369*12949SGeorge.Wilson@Sun.COM * the pool to readonly. 1370*12949SGeorge.Wilson@Sun.COM */ 1371*12949SGeorge.Wilson@Sun.COM continue; 1372*12949SGeorge.Wilson@Sun.COM } 1373*12949SGeorge.Wilson@Sun.COM 1374*12949SGeorge.Wilson@Sun.COM /* 1375*12949SGeorge.Wilson@Sun.COM * Swap the missing vdev with the data we were 1376*12949SGeorge.Wilson@Sun.COM * able to obtain from the MOS config. 1377*12949SGeorge.Wilson@Sun.COM */ 1378*12949SGeorge.Wilson@Sun.COM vdev_remove_child(rvd, tvd); 1379*12949SGeorge.Wilson@Sun.COM vdev_remove_child(mrvd, mtvd); 1380*12949SGeorge.Wilson@Sun.COM 1381*12949SGeorge.Wilson@Sun.COM vdev_add_child(rvd, mtvd); 1382*12949SGeorge.Wilson@Sun.COM vdev_add_child(mrvd, tvd); 1383*12949SGeorge.Wilson@Sun.COM 1384*12949SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1385*12949SGeorge.Wilson@Sun.COM vdev_load(mtvd); 1386*12949SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1387*12949SGeorge.Wilson@Sun.COM 1388*12949SGeorge.Wilson@Sun.COM vdev_reopen(rvd); 1389*12949SGeorge.Wilson@Sun.COM } else if (mtvd->vdev_islog) { 1390*12949SGeorge.Wilson@Sun.COM /* 1391*12949SGeorge.Wilson@Sun.COM * Load the slog device's state from the MOS config 1392*12949SGeorge.Wilson@Sun.COM * since it's possible that the label does not 1393*12949SGeorge.Wilson@Sun.COM * contain the most up-to-date information. 1394*12949SGeorge.Wilson@Sun.COM */ 1395*12949SGeorge.Wilson@Sun.COM vdev_load_log_state(tvd, mtvd); 1396*12949SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 1397*12949SGeorge.Wilson@Sun.COM } 13989701SGeorge.Wilson@Sun.COM } 1399*12949SGeorge.Wilson@Sun.COM vdev_free(mrvd); 140010594SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1401*12949SGeorge.Wilson@Sun.COM 1402*12949SGeorge.Wilson@Sun.COM /* 1403*12949SGeorge.Wilson@Sun.COM * Ensure we were able to validate the config. 1404*12949SGeorge.Wilson@Sun.COM */ 1405*12949SGeorge.Wilson@Sun.COM return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 14069701SGeorge.Wilson@Sun.COM } 14079701SGeorge.Wilson@Sun.COM 14089701SGeorge.Wilson@Sun.COM /* 14097294Sperrin * Check for missing log devices 14107294Sperrin */ 1411*12949SGeorge.Wilson@Sun.COM static int 14127294Sperrin spa_check_logs(spa_t *spa) 14137294Sperrin { 14147294Sperrin switch (spa->spa_log_state) { 14157294Sperrin case SPA_LOG_MISSING: 14167294Sperrin /* need to recheck in case slog has been restored */ 14177294Sperrin case SPA_LOG_UNKNOWN: 14187294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 14197294Sperrin DS_FIND_CHILDREN)) { 142011422SMark.Musante@Sun.COM spa_set_log_state(spa, SPA_LOG_MISSING); 14217294Sperrin return (1); 14227294Sperrin } 14237294Sperrin break; 14247294Sperrin } 14257294Sperrin return (0); 14267294Sperrin } 14277294Sperrin 142811422SMark.Musante@Sun.COM static boolean_t 142911422SMark.Musante@Sun.COM spa_passivate_log(spa_t *spa) 143011422SMark.Musante@Sun.COM { 143111422SMark.Musante@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 143211422SMark.Musante@Sun.COM boolean_t slog_found = B_FALSE; 143311422SMark.Musante@Sun.COM 143411422SMark.Musante@Sun.COM ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 143511422SMark.Musante@Sun.COM 143611422SMark.Musante@Sun.COM if (!spa_has_slogs(spa)) 143711422SMark.Musante@Sun.COM return (B_FALSE); 143811422SMark.Musante@Sun.COM 143911422SMark.Musante@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 144011422SMark.Musante@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 144111422SMark.Musante@Sun.COM metaslab_group_t *mg = tvd->vdev_mg; 144211422SMark.Musante@Sun.COM 144311422SMark.Musante@Sun.COM if (tvd->vdev_islog) { 144411422SMark.Musante@Sun.COM metaslab_group_passivate(mg); 144511422SMark.Musante@Sun.COM slog_found = B_TRUE; 144611422SMark.Musante@Sun.COM } 144711422SMark.Musante@Sun.COM } 144811422SMark.Musante@Sun.COM 144911422SMark.Musante@Sun.COM return (slog_found); 145011422SMark.Musante@Sun.COM } 145111422SMark.Musante@Sun.COM 145211422SMark.Musante@Sun.COM static void 145311422SMark.Musante@Sun.COM spa_activate_log(spa_t *spa) 145411422SMark.Musante@Sun.COM { 145511422SMark.Musante@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 145611422SMark.Musante@Sun.COM 145711422SMark.Musante@Sun.COM ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 145811422SMark.Musante@Sun.COM 145911422SMark.Musante@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 146011422SMark.Musante@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 146111422SMark.Musante@Sun.COM metaslab_group_t *mg = tvd->vdev_mg; 146211422SMark.Musante@Sun.COM 146311422SMark.Musante@Sun.COM if (tvd->vdev_islog) 146411422SMark.Musante@Sun.COM metaslab_group_activate(mg); 146511422SMark.Musante@Sun.COM } 146611422SMark.Musante@Sun.COM } 146711422SMark.Musante@Sun.COM 146811422SMark.Musante@Sun.COM int 146911422SMark.Musante@Sun.COM spa_offline_log(spa_t *spa) 147011422SMark.Musante@Sun.COM { 147111422SMark.Musante@Sun.COM int error = 0; 147211422SMark.Musante@Sun.COM 147311422SMark.Musante@Sun.COM if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 147411422SMark.Musante@Sun.COM NULL, DS_FIND_CHILDREN)) == 0) { 147511422SMark.Musante@Sun.COM 147611422SMark.Musante@Sun.COM /* 147711422SMark.Musante@Sun.COM * We successfully offlined the log device, sync out the 147811422SMark.Musante@Sun.COM * current txg so that the "stubby" block can be removed 147911422SMark.Musante@Sun.COM * by zil_sync(). 148011422SMark.Musante@Sun.COM */ 148111422SMark.Musante@Sun.COM txg_wait_synced(spa->spa_dsl_pool, 0); 148211422SMark.Musante@Sun.COM } 148311422SMark.Musante@Sun.COM return (error); 148411422SMark.Musante@Sun.COM } 148511422SMark.Musante@Sun.COM 148610672SEric.Schrock@Sun.COM static void 148710672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav) 148810672SEric.Schrock@Sun.COM { 148910922SJeff.Bonwick@Sun.COM for (int i = 0; i < sav->sav_count; i++) 149010672SEric.Schrock@Sun.COM spa_check_removed(sav->sav_vdevs[i]); 149110672SEric.Schrock@Sun.COM } 149210672SEric.Schrock@Sun.COM 149310922SJeff.Bonwick@Sun.COM void 149410922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio) 149510922SJeff.Bonwick@Sun.COM { 149610922SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 149710922SJeff.Bonwick@Sun.COM 149810922SJeff.Bonwick@Sun.COM if (zio->io_error) 149910922SJeff.Bonwick@Sun.COM return; 150010922SJeff.Bonwick@Sun.COM 150110922SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 150210922SJeff.Bonwick@Sun.COM if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 150310922SJeff.Bonwick@Sun.COM spa->spa_claim_max_txg = zio->io_bp->blk_birth; 150410922SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 150510922SJeff.Bonwick@Sun.COM } 150610922SJeff.Bonwick@Sun.COM 150710921STim.Haley@Sun.COM typedef struct spa_load_error { 150811727SVictor.Latushkin@Sun.COM uint64_t sle_meta_count; 150910921STim.Haley@Sun.COM uint64_t sle_data_count; 151010921STim.Haley@Sun.COM } spa_load_error_t; 151110921STim.Haley@Sun.COM 151210921STim.Haley@Sun.COM static void 151310921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio) 151410921STim.Haley@Sun.COM { 151510921STim.Haley@Sun.COM blkptr_t *bp = zio->io_bp; 151610921STim.Haley@Sun.COM spa_load_error_t *sle = zio->io_private; 151710921STim.Haley@Sun.COM dmu_object_type_t type = BP_GET_TYPE(bp); 151810921STim.Haley@Sun.COM int error = zio->io_error; 151910921STim.Haley@Sun.COM 152010921STim.Haley@Sun.COM if (error) { 152110921STim.Haley@Sun.COM if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) && 152210921STim.Haley@Sun.COM type != DMU_OT_INTENT_LOG) 152311727SVictor.Latushkin@Sun.COM atomic_add_64(&sle->sle_meta_count, 1); 152410921STim.Haley@Sun.COM else 152510921STim.Haley@Sun.COM atomic_add_64(&sle->sle_data_count, 1); 152610921STim.Haley@Sun.COM } 152710921STim.Haley@Sun.COM zio_data_buf_free(zio->io_data, zio->io_size); 152810921STim.Haley@Sun.COM } 152910921STim.Haley@Sun.COM 153010921STim.Haley@Sun.COM /*ARGSUSED*/ 153110921STim.Haley@Sun.COM static int 153210922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 153312296SLin.Ling@Sun.COM arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 153410921STim.Haley@Sun.COM { 153510921STim.Haley@Sun.COM if (bp != NULL) { 153610921STim.Haley@Sun.COM zio_t *rio = arg; 153710921STim.Haley@Sun.COM size_t size = BP_GET_PSIZE(bp); 153810921STim.Haley@Sun.COM void *data = zio_data_buf_alloc(size); 153910921STim.Haley@Sun.COM 154010921STim.Haley@Sun.COM zio_nowait(zio_read(rio, spa, bp, data, size, 154110921STim.Haley@Sun.COM spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 154210921STim.Haley@Sun.COM ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 154310921STim.Haley@Sun.COM ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 154410921STim.Haley@Sun.COM } 154510921STim.Haley@Sun.COM return (0); 154610921STim.Haley@Sun.COM } 154710921STim.Haley@Sun.COM 154810921STim.Haley@Sun.COM static int 154910921STim.Haley@Sun.COM spa_load_verify(spa_t *spa) 155010921STim.Haley@Sun.COM { 155110921STim.Haley@Sun.COM zio_t *rio; 155210921STim.Haley@Sun.COM spa_load_error_t sle = { 0 }; 155310921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 155410921STim.Haley@Sun.COM boolean_t verify_ok = B_FALSE; 155510921STim.Haley@Sun.COM int error; 155610921STim.Haley@Sun.COM 155711727SVictor.Latushkin@Sun.COM zpool_get_rewind_policy(spa->spa_config, &policy); 155811727SVictor.Latushkin@Sun.COM 155911727SVictor.Latushkin@Sun.COM if (policy.zrp_request & ZPOOL_NEVER_REWIND) 156011727SVictor.Latushkin@Sun.COM return (0); 156111727SVictor.Latushkin@Sun.COM 156210921STim.Haley@Sun.COM rio = zio_root(spa, NULL, &sle, 156310921STim.Haley@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 156410921STim.Haley@Sun.COM 156511125SJeff.Bonwick@Sun.COM error = traverse_pool(spa, spa->spa_verify_min_txg, 156611125SJeff.Bonwick@Sun.COM TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); 156710921STim.Haley@Sun.COM 156810921STim.Haley@Sun.COM (void) zio_wait(rio); 156910921STim.Haley@Sun.COM 157011727SVictor.Latushkin@Sun.COM spa->spa_load_meta_errors = sle.sle_meta_count; 157110921STim.Haley@Sun.COM spa->spa_load_data_errors = sle.sle_data_count; 157210921STim.Haley@Sun.COM 157311727SVictor.Latushkin@Sun.COM if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 157410921STim.Haley@Sun.COM sle.sle_data_count <= policy.zrp_maxdata) { 1575*12949SGeorge.Wilson@Sun.COM int64_t loss = 0; 1576*12949SGeorge.Wilson@Sun.COM 157710921STim.Haley@Sun.COM verify_ok = B_TRUE; 157810921STim.Haley@Sun.COM spa->spa_load_txg = spa->spa_uberblock.ub_txg; 157910921STim.Haley@Sun.COM spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 1580*12949SGeorge.Wilson@Sun.COM 1581*12949SGeorge.Wilson@Sun.COM loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 1582*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(spa->spa_load_info, 1583*12949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 1584*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_int64(spa->spa_load_info, 1585*12949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 1586*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(spa->spa_load_info, 1587*12949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 158811026STim.Haley@Sun.COM } else { 158911026STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 159010921STim.Haley@Sun.COM } 159110921STim.Haley@Sun.COM 159210921STim.Haley@Sun.COM if (error) { 159310921STim.Haley@Sun.COM if (error != ENXIO && error != EIO) 159410921STim.Haley@Sun.COM error = EIO; 159510921STim.Haley@Sun.COM return (error); 159610921STim.Haley@Sun.COM } 159710921STim.Haley@Sun.COM 159810921STim.Haley@Sun.COM return (verify_ok ? 0 : EIO); 159910921STim.Haley@Sun.COM } 160010921STim.Haley@Sun.COM 16017294Sperrin /* 160211422SMark.Musante@Sun.COM * Find a value in the pool props object. 160311422SMark.Musante@Sun.COM */ 160411422SMark.Musante@Sun.COM static void 160511422SMark.Musante@Sun.COM spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 160611422SMark.Musante@Sun.COM { 160711422SMark.Musante@Sun.COM (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 160811422SMark.Musante@Sun.COM zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 160911422SMark.Musante@Sun.COM } 161011422SMark.Musante@Sun.COM 161111422SMark.Musante@Sun.COM /* 161211422SMark.Musante@Sun.COM * Find a value in the pool directory object. 161311422SMark.Musante@Sun.COM */ 161411422SMark.Musante@Sun.COM static int 161511422SMark.Musante@Sun.COM spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 161611422SMark.Musante@Sun.COM { 161711422SMark.Musante@Sun.COM return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 161811422SMark.Musante@Sun.COM name, sizeof (uint64_t), 1, val)); 161911422SMark.Musante@Sun.COM } 162011422SMark.Musante@Sun.COM 162111422SMark.Musante@Sun.COM static int 162211422SMark.Musante@Sun.COM spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 162311422SMark.Musante@Sun.COM { 162411422SMark.Musante@Sun.COM vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 162511422SMark.Musante@Sun.COM return (err); 162611422SMark.Musante@Sun.COM } 162711422SMark.Musante@Sun.COM 162811422SMark.Musante@Sun.COM /* 162911422SMark.Musante@Sun.COM * Fix up config after a partly-completed split. This is done with the 163011422SMark.Musante@Sun.COM * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 163111422SMark.Musante@Sun.COM * pool have that entry in their config, but only the splitting one contains 163211422SMark.Musante@Sun.COM * a list of all the guids of the vdevs that are being split off. 163311422SMark.Musante@Sun.COM * 163411422SMark.Musante@Sun.COM * This function determines what to do with that list: either rejoin 163511422SMark.Musante@Sun.COM * all the disks to the pool, or complete the splitting process. To attempt 163611422SMark.Musante@Sun.COM * the rejoin, each disk that is offlined is marked online again, and 163711422SMark.Musante@Sun.COM * we do a reopen() call. If the vdev label for every disk that was 163811422SMark.Musante@Sun.COM * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 163911422SMark.Musante@Sun.COM * then we call vdev_split() on each disk, and complete the split. 164011422SMark.Musante@Sun.COM * 164111497SMark.Musante@Sun.COM * Otherwise we leave the config alone, with all the vdevs in place in 164211497SMark.Musante@Sun.COM * the original pool. 164311422SMark.Musante@Sun.COM */ 164411422SMark.Musante@Sun.COM static void 164511422SMark.Musante@Sun.COM spa_try_repair(spa_t *spa, nvlist_t *config) 164611422SMark.Musante@Sun.COM { 164711422SMark.Musante@Sun.COM uint_t extracted; 164811422SMark.Musante@Sun.COM uint64_t *glist; 164911422SMark.Musante@Sun.COM uint_t i, gcount; 165011422SMark.Musante@Sun.COM nvlist_t *nvl; 165111422SMark.Musante@Sun.COM vdev_t **vd; 165211422SMark.Musante@Sun.COM boolean_t attempt_reopen; 165311422SMark.Musante@Sun.COM 165411422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 165511422SMark.Musante@Sun.COM return; 165611422SMark.Musante@Sun.COM 165711422SMark.Musante@Sun.COM /* check that the config is complete */ 165811422SMark.Musante@Sun.COM if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 165911422SMark.Musante@Sun.COM &glist, &gcount) != 0) 166011422SMark.Musante@Sun.COM return; 166111422SMark.Musante@Sun.COM 166211422SMark.Musante@Sun.COM vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 166311422SMark.Musante@Sun.COM 166411422SMark.Musante@Sun.COM /* attempt to online all the vdevs & validate */ 166511422SMark.Musante@Sun.COM attempt_reopen = B_TRUE; 166611422SMark.Musante@Sun.COM for (i = 0; i < gcount; i++) { 166711422SMark.Musante@Sun.COM if (glist[i] == 0) /* vdev is hole */ 166811422SMark.Musante@Sun.COM continue; 166911422SMark.Musante@Sun.COM 167011422SMark.Musante@Sun.COM vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 167111422SMark.Musante@Sun.COM if (vd[i] == NULL) { 167211422SMark.Musante@Sun.COM /* 167311422SMark.Musante@Sun.COM * Don't bother attempting to reopen the disks; 167411422SMark.Musante@Sun.COM * just do the split. 167511422SMark.Musante@Sun.COM */ 167611422SMark.Musante@Sun.COM attempt_reopen = B_FALSE; 167711422SMark.Musante@Sun.COM } else { 167811422SMark.Musante@Sun.COM /* attempt to re-online it */ 167911422SMark.Musante@Sun.COM vd[i]->vdev_offline = B_FALSE; 168011422SMark.Musante@Sun.COM } 168111422SMark.Musante@Sun.COM } 168211422SMark.Musante@Sun.COM 168311422SMark.Musante@Sun.COM if (attempt_reopen) { 168411422SMark.Musante@Sun.COM vdev_reopen(spa->spa_root_vdev); 168511422SMark.Musante@Sun.COM 168611422SMark.Musante@Sun.COM /* check each device to see what state it's in */ 168711422SMark.Musante@Sun.COM for (extracted = 0, i = 0; i < gcount; i++) { 168811422SMark.Musante@Sun.COM if (vd[i] != NULL && 168911422SMark.Musante@Sun.COM vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 169011422SMark.Musante@Sun.COM break; 169111422SMark.Musante@Sun.COM ++extracted; 169211422SMark.Musante@Sun.COM } 169311422SMark.Musante@Sun.COM } 169411422SMark.Musante@Sun.COM 169511422SMark.Musante@Sun.COM /* 169611422SMark.Musante@Sun.COM * If every disk has been moved to the new pool, or if we never 169711422SMark.Musante@Sun.COM * even attempted to look at them, then we split them off for 169811422SMark.Musante@Sun.COM * good. 169911422SMark.Musante@Sun.COM */ 170011422SMark.Musante@Sun.COM if (!attempt_reopen || gcount == extracted) { 170111422SMark.Musante@Sun.COM for (i = 0; i < gcount; i++) 170211422SMark.Musante@Sun.COM if (vd[i] != NULL) 170311422SMark.Musante@Sun.COM vdev_split(vd[i]); 170411422SMark.Musante@Sun.COM vdev_reopen(spa->spa_root_vdev); 170511422SMark.Musante@Sun.COM } 170611422SMark.Musante@Sun.COM 170711422SMark.Musante@Sun.COM kmem_free(vd, gcount * sizeof (vdev_t *)); 170811422SMark.Musante@Sun.COM } 170911422SMark.Musante@Sun.COM 171011422SMark.Musante@Sun.COM static int 171111422SMark.Musante@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 171211422SMark.Musante@Sun.COM boolean_t mosconfig) 171311422SMark.Musante@Sun.COM { 171411422SMark.Musante@Sun.COM nvlist_t *config = spa->spa_config; 171511422SMark.Musante@Sun.COM char *ereport = FM_EREPORT_ZFS_POOL; 171611422SMark.Musante@Sun.COM int error; 171711422SMark.Musante@Sun.COM uint64_t pool_guid; 171811422SMark.Musante@Sun.COM nvlist_t *nvl; 171911422SMark.Musante@Sun.COM 172011422SMark.Musante@Sun.COM if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 172111422SMark.Musante@Sun.COM return (EINVAL); 172211422SMark.Musante@Sun.COM 172311422SMark.Musante@Sun.COM /* 172411422SMark.Musante@Sun.COM * Versioning wasn't explicitly added to the label until later, so if 172511422SMark.Musante@Sun.COM * it's not present treat it as the initial version. 172611422SMark.Musante@Sun.COM */ 172711422SMark.Musante@Sun.COM if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 172811422SMark.Musante@Sun.COM &spa->spa_ubsync.ub_version) != 0) 172911422SMark.Musante@Sun.COM spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 173011422SMark.Musante@Sun.COM 173111422SMark.Musante@Sun.COM (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 173211422SMark.Musante@Sun.COM &spa->spa_config_txg); 173311422SMark.Musante@Sun.COM 173411422SMark.Musante@Sun.COM if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 173511422SMark.Musante@Sun.COM spa_guid_exists(pool_guid, 0)) { 173611422SMark.Musante@Sun.COM error = EEXIST; 173711422SMark.Musante@Sun.COM } else { 173811422SMark.Musante@Sun.COM spa->spa_load_guid = pool_guid; 173911422SMark.Musante@Sun.COM 174011422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 174111422SMark.Musante@Sun.COM &nvl) == 0) { 174211422SMark.Musante@Sun.COM VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 174311422SMark.Musante@Sun.COM KM_SLEEP) == 0); 174411422SMark.Musante@Sun.COM } 174511422SMark.Musante@Sun.COM 174612817STim.Haley@Sun.COM gethrestime(&spa->spa_loaded_ts); 174711422SMark.Musante@Sun.COM error = spa_load_impl(spa, pool_guid, config, state, type, 174811422SMark.Musante@Sun.COM mosconfig, &ereport); 174911422SMark.Musante@Sun.COM } 175011422SMark.Musante@Sun.COM 175111422SMark.Musante@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 175212817STim.Haley@Sun.COM if (error) { 175312817STim.Haley@Sun.COM if (error != EEXIST) { 175412817STim.Haley@Sun.COM spa->spa_loaded_ts.tv_sec = 0; 175512817STim.Haley@Sun.COM spa->spa_loaded_ts.tv_nsec = 0; 175612817STim.Haley@Sun.COM } 175712817STim.Haley@Sun.COM if (error != EBADF) { 175812817STim.Haley@Sun.COM zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 175912817STim.Haley@Sun.COM } 176012817STim.Haley@Sun.COM } 176111422SMark.Musante@Sun.COM spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 176211422SMark.Musante@Sun.COM spa->spa_ena = 0; 176311422SMark.Musante@Sun.COM 176411422SMark.Musante@Sun.COM return (error); 176511422SMark.Musante@Sun.COM } 176611422SMark.Musante@Sun.COM 176711422SMark.Musante@Sun.COM /* 1768789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 17691544Seschrock * source of configuration information. 1770789Sahrens */ 1771789Sahrens static int 177211422SMark.Musante@Sun.COM spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 177311422SMark.Musante@Sun.COM spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 177411422SMark.Musante@Sun.COM char **ereport) 1775789Sahrens { 1776789Sahrens int error = 0; 177711810SMark.Musante@Sun.COM nvlist_t *nvroot = NULL; 1778789Sahrens vdev_t *rvd; 1779789Sahrens uberblock_t *ub = &spa->spa_uberblock; 1780*12949SGeorge.Wilson@Sun.COM uint64_t children, config_cache_txg = spa->spa_config_txg; 17818241SJeff.Bonwick@Sun.COM int orig_mode = spa->spa_mode; 178211422SMark.Musante@Sun.COM int parse; 178312470SMatthew.Ahrens@Sun.COM uint64_t obj; 1784789Sahrens 17858241SJeff.Bonwick@Sun.COM /* 17868241SJeff.Bonwick@Sun.COM * If this is an untrusted config, access the pool in read-only mode. 17878241SJeff.Bonwick@Sun.COM * This prevents things like resilvering recently removed devices. 17888241SJeff.Bonwick@Sun.COM */ 17898241SJeff.Bonwick@Sun.COM if (!mosconfig) 17908241SJeff.Bonwick@Sun.COM spa->spa_mode = FREAD; 17918241SJeff.Bonwick@Sun.COM 17927754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 17937754SJeff.Bonwick@Sun.COM 17941544Seschrock spa->spa_load_state = state; 17951635Sbonwick 179611422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 179711422SMark.Musante@Sun.COM return (EINVAL); 179811422SMark.Musante@Sun.COM 179911422SMark.Musante@Sun.COM parse = (type == SPA_IMPORT_EXISTING ? 180011422SMark.Musante@Sun.COM VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 18012174Seschrock 1802789Sahrens /* 18039234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 18049234SGeorge.Wilson@Sun.COM */ 18059630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 18069630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 18079234SGeorge.Wilson@Sun.COM 18089234SGeorge.Wilson@Sun.COM /* 18092082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 18102082Seschrock * value that will be returned by spa_version() since parsing the 18112082Seschrock * configuration requires knowing the version number. 1812789Sahrens */ 18137754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 181411422SMark.Musante@Sun.COM error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 18157754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1816789Sahrens 18172082Seschrock if (error != 0) 181811422SMark.Musante@Sun.COM return (error); 1819789Sahrens 18201585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 182111422SMark.Musante@Sun.COM 182211422SMark.Musante@Sun.COM if (type != SPA_IMPORT_ASSEMBLE) { 182311422SMark.Musante@Sun.COM ASSERT(spa_guid(spa) == pool_guid); 182411422SMark.Musante@Sun.COM } 1825789Sahrens 1826789Sahrens /* 1827789Sahrens * Try to open all vdevs, loading each label in the process. 1828789Sahrens */ 18297754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 18304070Smc142369 error = vdev_open(rvd); 18317754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 18324070Smc142369 if (error != 0) 183311422SMark.Musante@Sun.COM return (error); 1834789Sahrens 1835789Sahrens /* 18369276SMark.Musante@Sun.COM * We need to validate the vdev labels against the configuration that 18379276SMark.Musante@Sun.COM * we have in hand, which is dependent on the setting of mosconfig. If 18389276SMark.Musante@Sun.COM * mosconfig is true then we're validating the vdev labels based on 183911422SMark.Musante@Sun.COM * that config. Otherwise, we're validating against the cached config 18409276SMark.Musante@Sun.COM * (zpool.cache) that was read when we loaded the zfs module, and then 18419276SMark.Musante@Sun.COM * later we will recursively call spa_load() and validate against 18429276SMark.Musante@Sun.COM * the vdev config. 184311422SMark.Musante@Sun.COM * 184411422SMark.Musante@Sun.COM * If we're assembling a new pool that's been split off from an 184511422SMark.Musante@Sun.COM * existing pool, the labels haven't yet been updated so we skip 184611422SMark.Musante@Sun.COM * validation for now. 18471986Seschrock */ 184811422SMark.Musante@Sun.COM if (type != SPA_IMPORT_ASSEMBLE) { 184911422SMark.Musante@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 185011422SMark.Musante@Sun.COM error = vdev_validate(rvd); 185111422SMark.Musante@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 185211422SMark.Musante@Sun.COM 185311422SMark.Musante@Sun.COM if (error != 0) 185411422SMark.Musante@Sun.COM return (error); 185511422SMark.Musante@Sun.COM 185611422SMark.Musante@Sun.COM if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 185711422SMark.Musante@Sun.COM return (ENXIO); 18581986Seschrock } 18591986Seschrock 18601986Seschrock /* 1861789Sahrens * Find the best uberblock. 1862789Sahrens */ 18637754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1864789Sahrens 1865789Sahrens /* 1866789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1867789Sahrens */ 186811422SMark.Musante@Sun.COM if (ub->ub_txg == 0) 186911422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 18701544Seschrock 18711544Seschrock /* 18721544Seschrock * If the pool is newer than the code, we can't open it. 18731544Seschrock */ 187411422SMark.Musante@Sun.COM if (ub->ub_version > SPA_VERSION) 187511422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 1876789Sahrens 1877789Sahrens /* 1878789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 1879*12949SGeorge.Wilson@Sun.COM * incomplete configuration. We first check to see if the pool 1880*12949SGeorge.Wilson@Sun.COM * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 1881*12949SGeorge.Wilson@Sun.COM * If it is, defer the vdev_guid_sum check till later so we 1882*12949SGeorge.Wilson@Sun.COM * can handle missing vdevs. 1883789Sahrens */ 1884*12949SGeorge.Wilson@Sun.COM if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 1885*12949SGeorge.Wilson@Sun.COM &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 188611422SMark.Musante@Sun.COM rvd->vdev_guid_sum != ub->ub_guid_sum) 188711422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 188811422SMark.Musante@Sun.COM 188911422SMark.Musante@Sun.COM if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 189011422SMark.Musante@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 189111422SMark.Musante@Sun.COM spa_try_repair(spa, config); 189211422SMark.Musante@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 189311422SMark.Musante@Sun.COM nvlist_free(spa->spa_config_splitting); 189411422SMark.Musante@Sun.COM spa->spa_config_splitting = NULL; 1895789Sahrens } 1896789Sahrens 1897789Sahrens /* 1898789Sahrens * Initialize internal SPA structures. 1899789Sahrens */ 1900789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1901789Sahrens spa->spa_ubsync = spa->spa_uberblock; 190210921STim.Haley@Sun.COM spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 190311727SVictor.Latushkin@Sun.COM TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 190410921STim.Haley@Sun.COM spa->spa_first_txg = spa->spa_last_ubsync_txg ? 190510921STim.Haley@Sun.COM spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 190610922SJeff.Bonwick@Sun.COM spa->spa_claim_max_txg = spa->spa_first_txg; 190712296SLin.Ling@Sun.COM spa->spa_prev_software_version = ub->ub_software_version; 190810922SJeff.Bonwick@Sun.COM 19091544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 191011422SMark.Musante@Sun.COM if (error) 191111422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1912789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1913789Sahrens 191411422SMark.Musante@Sun.COM if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 191511422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 191611422SMark.Musante@Sun.COM 1917789Sahrens if (!mosconfig) { 19183975Sek110237 uint64_t hostid; 191911810SMark.Musante@Sun.COM nvlist_t *policy = NULL, *nvconfig; 192011810SMark.Musante@Sun.COM 192111810SMark.Musante@Sun.COM if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 192211810SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 19232082Seschrock 192410594SGeorge.Wilson@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 19257706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 19263975Sek110237 char *hostname; 19273975Sek110237 unsigned long myhostid = 0; 19283975Sek110237 192910594SGeorge.Wilson@Sun.COM VERIFY(nvlist_lookup_string(nvconfig, 19303975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 19313975Sek110237 19328662SJordan.Vaughan@Sun.com #ifdef _KERNEL 19338662SJordan.Vaughan@Sun.com myhostid = zone_get_hostid(NULL); 19348662SJordan.Vaughan@Sun.com #else /* _KERNEL */ 19358662SJordan.Vaughan@Sun.com /* 19368662SJordan.Vaughan@Sun.com * We're emulating the system's hostid in userland, so 19378662SJordan.Vaughan@Sun.com * we can't use zone_get_hostid(). 19388662SJordan.Vaughan@Sun.com */ 19393975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 19408662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 19414178Slling if (hostid != 0 && myhostid != 0 && 19428662SJordan.Vaughan@Sun.com hostid != myhostid) { 194311810SMark.Musante@Sun.COM nvlist_free(nvconfig); 19443975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 19453975Sek110237 "loaded as it was last accessed by " 19467706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 19473975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 19487754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 19493975Sek110237 (unsigned long)hostid); 195011422SMark.Musante@Sun.COM return (EBADF); 19513975Sek110237 } 19523975Sek110237 } 195311727SVictor.Latushkin@Sun.COM if (nvlist_lookup_nvlist(spa->spa_config, 195411727SVictor.Latushkin@Sun.COM ZPOOL_REWIND_POLICY, &policy) == 0) 195511727SVictor.Latushkin@Sun.COM VERIFY(nvlist_add_nvlist(nvconfig, 195611727SVictor.Latushkin@Sun.COM ZPOOL_REWIND_POLICY, policy) == 0); 19573975Sek110237 195810594SGeorge.Wilson@Sun.COM spa_config_set(spa, nvconfig); 1959789Sahrens spa_unload(spa); 1960789Sahrens spa_deactivate(spa); 19618241SJeff.Bonwick@Sun.COM spa_activate(spa, orig_mode); 1962789Sahrens 196311422SMark.Musante@Sun.COM return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 19641544Seschrock } 19651544Seschrock 196612470SMatthew.Ahrens@Sun.COM if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 196712470SMatthew.Ahrens@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 196812470SMatthew.Ahrens@Sun.COM error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 196912470SMatthew.Ahrens@Sun.COM if (error != 0) 197011422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1971789Sahrens 19721544Seschrock /* 19732082Seschrock * Load the bit that tells us to use the new accounting function 19742082Seschrock * (raid-z deflation). If we have an older pool, this will not 19752082Seschrock * be present. 19762082Seschrock */ 197711422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 197811422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 197911422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 19802082Seschrock 198112296SLin.Ling@Sun.COM error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 198212296SLin.Ling@Sun.COM &spa->spa_creation_version); 198312296SLin.Ling@Sun.COM if (error != 0 && error != ENOENT) 198412296SLin.Ling@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 198512296SLin.Ling@Sun.COM 19862082Seschrock /* 19871544Seschrock * Load the persistent error log. If we have an older pool, this will 19881544Seschrock * not be present. 19891544Seschrock */ 199011422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 199111422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 199211422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 199311422SMark.Musante@Sun.COM 199411422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 199511422SMark.Musante@Sun.COM &spa->spa_errlog_scrub); 199611422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 199711422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1998789Sahrens 1999789Sahrens /* 20002926Sek110237 * Load the history object. If we have an older pool, this 20012926Sek110237 * will not be present. 20022926Sek110237 */ 200311422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 200411422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 200511422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 200611422SMark.Musante@Sun.COM 200711422SMark.Musante@Sun.COM /* 200811422SMark.Musante@Sun.COM * If we're assembling the pool from the split-off vdevs of 200911422SMark.Musante@Sun.COM * an existing pool, we don't want to attach the spares & cache 201011422SMark.Musante@Sun.COM * devices. 201111422SMark.Musante@Sun.COM */ 20122926Sek110237 20132926Sek110237 /* 20142082Seschrock * Load any hot spares for this pool. 20152082Seschrock */ 201611422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 201711422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 201811422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 201911422SMark.Musante@Sun.COM if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 20204577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 20215450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 202211422SMark.Musante@Sun.COM &spa->spa_spares.sav_config) != 0) 202311422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20242082Seschrock 20257754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20262082Seschrock spa_load_spares(spa); 20277754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 202811422SMark.Musante@Sun.COM } else if (error == 0) { 202911422SMark.Musante@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 20302082Seschrock } 20312082Seschrock 20325450Sbrendan /* 20335450Sbrendan * Load any level 2 ARC devices for this pool. 20345450Sbrendan */ 203511422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 20365450Sbrendan &spa->spa_l2cache.sav_object); 203711422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 203811422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 203911422SMark.Musante@Sun.COM if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 20405450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 20415450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 204211422SMark.Musante@Sun.COM &spa->spa_l2cache.sav_config) != 0) 204311422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20445450Sbrendan 20457754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20465450Sbrendan spa_load_l2cache(spa); 20477754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 204811422SMark.Musante@Sun.COM } else if (error == 0) { 204911422SMark.Musante@Sun.COM spa->spa_l2cache.sav_sync = B_TRUE; 20505450Sbrendan } 20515450Sbrendan 20525094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20534543Smarks 205411422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 205511422SMark.Musante@Sun.COM if (error && error != ENOENT) 205611422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20573912Slling 20583912Slling if (error == 0) { 205911422SMark.Musante@Sun.COM uint64_t autoreplace; 206011422SMark.Musante@Sun.COM 206111422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 206211422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 206311422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 206411422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 206511422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 206611422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 206711422SMark.Musante@Sun.COM &spa->spa_dedup_ditto); 206811422SMark.Musante@Sun.COM 206910672SEric.Schrock@Sun.COM spa->spa_autoreplace = (autoreplace != 0); 20703912Slling } 20713912Slling 20722082Seschrock /* 20734451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 20744451Seschrock * the ZFS DE that it should not issue any faults for unopenable 20754451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 20764451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 20774451Seschrock * over. 20784451Seschrock */ 207910672SEric.Schrock@Sun.COM if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 20804451Seschrock spa_check_removed(spa->spa_root_vdev); 208110672SEric.Schrock@Sun.COM /* 208210672SEric.Schrock@Sun.COM * For the import case, this is done in spa_import(), because 208310672SEric.Schrock@Sun.COM * at this point we're using the spare definitions from 208410672SEric.Schrock@Sun.COM * the MOS config, not necessarily from the userland config. 208510672SEric.Schrock@Sun.COM */ 208610672SEric.Schrock@Sun.COM if (state != SPA_LOAD_IMPORT) { 208710672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 208810672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 208910672SEric.Schrock@Sun.COM } 209010672SEric.Schrock@Sun.COM } 20914451Seschrock 20924451Seschrock /* 20931986Seschrock * Load the vdev state for all toplevel vdevs. 2094789Sahrens */ 20951986Seschrock vdev_load(rvd); 2096789Sahrens 2097789Sahrens /* 2098789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2099789Sahrens */ 21007754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2101789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 21027754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2103789Sahrens 2104789Sahrens /* 210510922SJeff.Bonwick@Sun.COM * Load the DDTs (dedup tables). 210610922SJeff.Bonwick@Sun.COM */ 210710922SJeff.Bonwick@Sun.COM error = ddt_load(spa); 210811422SMark.Musante@Sun.COM if (error != 0) 210911422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 211010922SJeff.Bonwick@Sun.COM 211110956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 211210956SGeorge.Wilson@Sun.COM 211310922SJeff.Bonwick@Sun.COM /* 2114*12949SGeorge.Wilson@Sun.COM * Validate the config, using the MOS config to fill in any 2115*12949SGeorge.Wilson@Sun.COM * information which might be missing. If we fail to validate 2116*12949SGeorge.Wilson@Sun.COM * the config then declare the pool unfit for use. If we're 2117*12949SGeorge.Wilson@Sun.COM * assembling a pool from a split, the log is not transferred 2118*12949SGeorge.Wilson@Sun.COM * over. 211910922SJeff.Bonwick@Sun.COM */ 212011422SMark.Musante@Sun.COM if (type != SPA_IMPORT_ASSEMBLE) { 212111810SMark.Musante@Sun.COM nvlist_t *nvconfig; 212211810SMark.Musante@Sun.COM 212311810SMark.Musante@Sun.COM if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 212411810SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 212511810SMark.Musante@Sun.COM 2126*12949SGeorge.Wilson@Sun.COM if (!spa_config_valid(spa, nvconfig)) { 2127*12949SGeorge.Wilson@Sun.COM nvlist_free(nvconfig); 2128*12949SGeorge.Wilson@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 2129*12949SGeorge.Wilson@Sun.COM ENXIO)); 2130*12949SGeorge.Wilson@Sun.COM } 213111422SMark.Musante@Sun.COM nvlist_free(nvconfig); 213211422SMark.Musante@Sun.COM 2133*12949SGeorge.Wilson@Sun.COM /* 2134*12949SGeorge.Wilson@Sun.COM * Now that we've validate the config, check the state of the 2135*12949SGeorge.Wilson@Sun.COM * root vdev. If it can't be opened, it indicates one or 2136*12949SGeorge.Wilson@Sun.COM * more toplevel vdevs are faulted. 2137*12949SGeorge.Wilson@Sun.COM */ 2138*12949SGeorge.Wilson@Sun.COM if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2139*12949SGeorge.Wilson@Sun.COM return (ENXIO); 2140*12949SGeorge.Wilson@Sun.COM 214111422SMark.Musante@Sun.COM if (spa_check_logs(spa)) { 214211422SMark.Musante@Sun.COM *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 214311422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 214411422SMark.Musante@Sun.COM } 214510922SJeff.Bonwick@Sun.COM } 214610922SJeff.Bonwick@Sun.COM 2147*12949SGeorge.Wilson@Sun.COM /* 2148*12949SGeorge.Wilson@Sun.COM * We've successfully opened the pool, verify that we're ready 2149*12949SGeorge.Wilson@Sun.COM * to start pushing transactions. 2150*12949SGeorge.Wilson@Sun.COM */ 2151*12949SGeorge.Wilson@Sun.COM if (state != SPA_LOAD_TRYIMPORT) { 2152*12949SGeorge.Wilson@Sun.COM if (error = spa_load_verify(spa)) 2153*12949SGeorge.Wilson@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2154*12949SGeorge.Wilson@Sun.COM error)); 2155*12949SGeorge.Wilson@Sun.COM } 2156*12949SGeorge.Wilson@Sun.COM 215710921STim.Haley@Sun.COM if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 215810921STim.Haley@Sun.COM spa->spa_load_max_txg == UINT64_MAX)) { 21591635Sbonwick dmu_tx_t *tx; 21601635Sbonwick int need_update = B_FALSE; 21618241SJeff.Bonwick@Sun.COM 21628241SJeff.Bonwick@Sun.COM ASSERT(state != SPA_LOAD_TRYIMPORT); 21631601Sbonwick 21641635Sbonwick /* 21651635Sbonwick * Claim log blocks that haven't been committed yet. 21661635Sbonwick * This must all happen in a single txg. 216710922SJeff.Bonwick@Sun.COM * Note: spa_claim_max_txg is updated by spa_claim_notify(), 216810922SJeff.Bonwick@Sun.COM * invoked from zil_claim_log_block()'s i/o done callback. 216910921STim.Haley@Sun.COM * Price of rollback is that we abandon the log. 21701635Sbonwick */ 217110922SJeff.Bonwick@Sun.COM spa->spa_claiming = B_TRUE; 217210922SJeff.Bonwick@Sun.COM 21731601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 2174789Sahrens spa_first_txg(spa)); 21757754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 21762417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2177789Sahrens dmu_tx_commit(tx); 2178789Sahrens 217910922SJeff.Bonwick@Sun.COM spa->spa_claiming = B_FALSE; 218010922SJeff.Bonwick@Sun.COM 218111422SMark.Musante@Sun.COM spa_set_log_state(spa, SPA_LOG_GOOD); 2182789Sahrens spa->spa_sync_on = B_TRUE; 2183789Sahrens txg_sync_start(spa->spa_dsl_pool); 2184789Sahrens 2185789Sahrens /* 218610922SJeff.Bonwick@Sun.COM * Wait for all claims to sync. We sync up to the highest 218710922SJeff.Bonwick@Sun.COM * claimed log block birth time so that claimed log blocks 218810922SJeff.Bonwick@Sun.COM * don't appear to be from the future. spa_claim_max_txg 218910922SJeff.Bonwick@Sun.COM * will have been set for us by either zil_check_log_chain() 219010922SJeff.Bonwick@Sun.COM * (invoked from spa_check_logs()) or zil_claim() above. 2191789Sahrens */ 219210922SJeff.Bonwick@Sun.COM txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 21931585Sbonwick 21941585Sbonwick /* 21951635Sbonwick * If the config cache is stale, or we have uninitialized 21961635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 219710100SLin.Ling@Sun.COM * 2198*12949SGeorge.Wilson@Sun.COM * If this is a verbatim import, trust the current 219910100SLin.Ling@Sun.COM * in-core spa_config and update the disk labels. 22001585Sbonwick */ 22011635Sbonwick if (config_cache_txg != spa->spa_config_txg || 2202*12949SGeorge.Wilson@Sun.COM state == SPA_LOAD_IMPORT || 2203*12949SGeorge.Wilson@Sun.COM state == SPA_LOAD_RECOVER || 2204*12949SGeorge.Wilson@Sun.COM (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 22051635Sbonwick need_update = B_TRUE; 22061635Sbonwick 22078241SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) 22081635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 22091635Sbonwick need_update = B_TRUE; 22101585Sbonwick 22111585Sbonwick /* 22121635Sbonwick * Update the config cache asychronously in case we're the 22131635Sbonwick * root pool, in which case the config cache isn't writable yet. 22141585Sbonwick */ 22151635Sbonwick if (need_update) 22161635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 22178241SJeff.Bonwick@Sun.COM 22188241SJeff.Bonwick@Sun.COM /* 22198241SJeff.Bonwick@Sun.COM * Check all DTLs to see if anything needs resilvering. 22208241SJeff.Bonwick@Sun.COM */ 222112296SLin.Ling@Sun.COM if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 222212296SLin.Ling@Sun.COM vdev_resilver_needed(rvd, NULL, NULL)) 22238241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 222410298SMatthew.Ahrens@Sun.COM 222510298SMatthew.Ahrens@Sun.COM /* 222610298SMatthew.Ahrens@Sun.COM * Delete any inconsistent datasets. 222710298SMatthew.Ahrens@Sun.COM */ 222810298SMatthew.Ahrens@Sun.COM (void) dmu_objset_find(spa_name(spa), 222910298SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 223010342Schris.kirby@sun.com 223110342Schris.kirby@sun.com /* 223210342Schris.kirby@sun.com * Clean up any stale temporary dataset userrefs. 223310342Schris.kirby@sun.com */ 223410342Schris.kirby@sun.com dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2235789Sahrens } 2236789Sahrens 223711422SMark.Musante@Sun.COM return (0); 2238789Sahrens } 2239789Sahrens 224010921STim.Haley@Sun.COM static int 224110921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 224210921STim.Haley@Sun.COM { 224310921STim.Haley@Sun.COM spa_unload(spa); 224410921STim.Haley@Sun.COM spa_deactivate(spa); 224510921STim.Haley@Sun.COM 224610921STim.Haley@Sun.COM spa->spa_load_max_txg--; 224710921STim.Haley@Sun.COM 224810921STim.Haley@Sun.COM spa_activate(spa, spa_mode_global); 224910921STim.Haley@Sun.COM spa_async_suspend(spa); 225010921STim.Haley@Sun.COM 225111422SMark.Musante@Sun.COM return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 225210921STim.Haley@Sun.COM } 225310921STim.Haley@Sun.COM 225410921STim.Haley@Sun.COM static int 225510921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 225611727SVictor.Latushkin@Sun.COM uint64_t max_request, int rewind_flags) 225710921STim.Haley@Sun.COM { 225810921STim.Haley@Sun.COM nvlist_t *config = NULL; 225910921STim.Haley@Sun.COM int load_error, rewind_error; 226011727SVictor.Latushkin@Sun.COM uint64_t safe_rewind_txg; 226110921STim.Haley@Sun.COM uint64_t min_txg; 226210921STim.Haley@Sun.COM 226311026STim.Haley@Sun.COM if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 226410921STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_load_txg; 226511422SMark.Musante@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 226611026STim.Haley@Sun.COM } else { 226710921STim.Haley@Sun.COM spa->spa_load_max_txg = max_request; 226811026STim.Haley@Sun.COM } 226910921STim.Haley@Sun.COM 227011422SMark.Musante@Sun.COM load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 227111422SMark.Musante@Sun.COM mosconfig); 227210921STim.Haley@Sun.COM if (load_error == 0) 227310921STim.Haley@Sun.COM return (0); 227410921STim.Haley@Sun.COM 227510921STim.Haley@Sun.COM if (spa->spa_root_vdev != NULL) 227610921STim.Haley@Sun.COM config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 227710921STim.Haley@Sun.COM 227810921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 227910921STim.Haley@Sun.COM spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 228010921STim.Haley@Sun.COM 228111727SVictor.Latushkin@Sun.COM if (rewind_flags & ZPOOL_NEVER_REWIND) { 228210921STim.Haley@Sun.COM nvlist_free(config); 228310921STim.Haley@Sun.COM return (load_error); 228410921STim.Haley@Sun.COM } 228510921STim.Haley@Sun.COM 228610921STim.Haley@Sun.COM /* Price of rolling back is discarding txgs, including log */ 228710921STim.Haley@Sun.COM if (state == SPA_LOAD_RECOVER) 228811422SMark.Musante@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 228910921STim.Haley@Sun.COM 229011727SVictor.Latushkin@Sun.COM spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 229111727SVictor.Latushkin@Sun.COM safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 229211727SVictor.Latushkin@Sun.COM min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 229311727SVictor.Latushkin@Sun.COM TXG_INITIAL : safe_rewind_txg; 229411727SVictor.Latushkin@Sun.COM 229511727SVictor.Latushkin@Sun.COM /* 229611727SVictor.Latushkin@Sun.COM * Continue as long as we're finding errors, we're still within 229711727SVictor.Latushkin@Sun.COM * the acceptable rewind range, and we're still finding uberblocks 229811727SVictor.Latushkin@Sun.COM */ 229911727SVictor.Latushkin@Sun.COM while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 230011727SVictor.Latushkin@Sun.COM spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 230111727SVictor.Latushkin@Sun.COM if (spa->spa_load_max_txg < safe_rewind_txg) 230210921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_TRUE; 230310921STim.Haley@Sun.COM rewind_error = spa_load_retry(spa, state, mosconfig); 230410921STim.Haley@Sun.COM } 230510921STim.Haley@Sun.COM 230610921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_FALSE; 230710921STim.Haley@Sun.COM spa->spa_load_max_txg = UINT64_MAX; 230810921STim.Haley@Sun.COM 230910921STim.Haley@Sun.COM if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 231010921STim.Haley@Sun.COM spa_config_set(spa, config); 231110921STim.Haley@Sun.COM 231210921STim.Haley@Sun.COM return (state == SPA_LOAD_RECOVER ? rewind_error : load_error); 231310921STim.Haley@Sun.COM } 231410921STim.Haley@Sun.COM 2315789Sahrens /* 2316789Sahrens * Pool Open/Import 2317789Sahrens * 2318789Sahrens * The import case is identical to an open except that the configuration is sent 2319789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 2320789Sahrens * case of an open, the pool configuration will exist in the 23214451Seschrock * POOL_STATE_UNINITIALIZED state. 2322789Sahrens * 2323789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 2324789Sahrens * the same time open the pool, without having to keep around the spa_t in some 2325789Sahrens * ambiguous state. 2326789Sahrens */ 2327789Sahrens static int 232810921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 232910921STim.Haley@Sun.COM nvlist_t **config) 2330789Sahrens { 2331789Sahrens spa_t *spa; 2332*12949SGeorge.Wilson@Sun.COM spa_load_state_t state = SPA_LOAD_OPEN; 2333789Sahrens int error; 2334789Sahrens int locked = B_FALSE; 2335789Sahrens 2336789Sahrens *spapp = NULL; 2337789Sahrens 2338789Sahrens /* 2339789Sahrens * As disgusting as this is, we need to support recursive calls to this 2340789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 2341789Sahrens * up calling spa_open() again. The real fix is to figure out how to 2342789Sahrens * avoid dsl_dir_open() calling this in the first place. 2343789Sahrens */ 2344789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 2345789Sahrens mutex_enter(&spa_namespace_lock); 2346789Sahrens locked = B_TRUE; 2347789Sahrens } 2348789Sahrens 2349789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2350789Sahrens if (locked) 2351789Sahrens mutex_exit(&spa_namespace_lock); 2352789Sahrens return (ENOENT); 2353789Sahrens } 235410921STim.Haley@Sun.COM 2355789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 235611819STim.Haley@Sun.COM zpool_rewind_policy_t policy; 235711819STim.Haley@Sun.COM 235811819STim.Haley@Sun.COM zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 235911819STim.Haley@Sun.COM &policy); 236011819STim.Haley@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 236111819STim.Haley@Sun.COM state = SPA_LOAD_RECOVER; 2362789Sahrens 23638241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2364789Sahrens 236510921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 236610921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 236710921STim.Haley@Sun.COM 236810921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 236911727SVictor.Latushkin@Sun.COM policy.zrp_request); 2370789Sahrens 2371789Sahrens if (error == EBADF) { 2372789Sahrens /* 23731986Seschrock * If vdev_validate() returns failure (indicated by 23741986Seschrock * EBADF), it indicates that one of the vdevs indicates 23751986Seschrock * that the pool has been exported or destroyed. If 23761986Seschrock * this is the case, the config cache is out of sync and 23771986Seschrock * we should remove the pool from the namespace. 2378789Sahrens */ 2379789Sahrens spa_unload(spa); 2380789Sahrens spa_deactivate(spa); 23816643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2382789Sahrens spa_remove(spa); 2383789Sahrens if (locked) 2384789Sahrens mutex_exit(&spa_namespace_lock); 2385789Sahrens return (ENOENT); 23861544Seschrock } 23871544Seschrock 23881544Seschrock if (error) { 2389789Sahrens /* 2390789Sahrens * We can't open the pool, but we still have useful 2391789Sahrens * information: the state of each vdev after the 2392789Sahrens * attempted vdev_open(). Return this to the user. 2393789Sahrens */ 2394*12949SGeorge.Wilson@Sun.COM if (config != NULL && spa->spa_config) { 239510921STim.Haley@Sun.COM VERIFY(nvlist_dup(spa->spa_config, config, 239610921STim.Haley@Sun.COM KM_SLEEP) == 0); 2397*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist(*config, 2398*12949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_LOAD_INFO, 2399*12949SGeorge.Wilson@Sun.COM spa->spa_load_info) == 0); 2400*12949SGeorge.Wilson@Sun.COM } 2401789Sahrens spa_unload(spa); 2402789Sahrens spa_deactivate(spa); 240310921STim.Haley@Sun.COM spa->spa_last_open_failed = error; 2404789Sahrens if (locked) 2405789Sahrens mutex_exit(&spa_namespace_lock); 2406789Sahrens *spapp = NULL; 2407789Sahrens return (error); 2408789Sahrens } 2409789Sahrens } 2410789Sahrens 2411789Sahrens spa_open_ref(spa, tag); 24124451Seschrock 241310921STim.Haley@Sun.COM if (config != NULL) 241410921STim.Haley@Sun.COM *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 241510921STim.Haley@Sun.COM 2416*12949SGeorge.Wilson@Sun.COM /* 2417*12949SGeorge.Wilson@Sun.COM * If we've recovered the pool, pass back any information we 2418*12949SGeorge.Wilson@Sun.COM * gathered while doing the load. 2419*12949SGeorge.Wilson@Sun.COM */ 2420*12949SGeorge.Wilson@Sun.COM if (state == SPA_LOAD_RECOVER) { 2421*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 2422*12949SGeorge.Wilson@Sun.COM spa->spa_load_info) == 0); 2423*12949SGeorge.Wilson@Sun.COM } 2424*12949SGeorge.Wilson@Sun.COM 242511026STim.Haley@Sun.COM if (locked) { 242611026STim.Haley@Sun.COM spa->spa_last_open_failed = 0; 242711026STim.Haley@Sun.COM spa->spa_last_ubsync_txg = 0; 242811026STim.Haley@Sun.COM spa->spa_load_txg = 0; 2429789Sahrens mutex_exit(&spa_namespace_lock); 243011026STim.Haley@Sun.COM } 2431789Sahrens 2432789Sahrens *spapp = spa; 2433789Sahrens 2434789Sahrens return (0); 2435789Sahrens } 2436789Sahrens 2437789Sahrens int 243810921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 243910921STim.Haley@Sun.COM nvlist_t **config) 244010921STim.Haley@Sun.COM { 244110921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, policy, config)); 244210921STim.Haley@Sun.COM } 244310921STim.Haley@Sun.COM 244410921STim.Haley@Sun.COM int 2445789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2446789Sahrens { 244710921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, NULL, NULL)); 2448789Sahrens } 2449789Sahrens 24501544Seschrock /* 24511544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 24521544Seschrock * preventing it from being exported or destroyed. 24531544Seschrock */ 24541544Seschrock spa_t * 24551544Seschrock spa_inject_addref(char *name) 24561544Seschrock { 24571544Seschrock spa_t *spa; 24581544Seschrock 24591544Seschrock mutex_enter(&spa_namespace_lock); 24601544Seschrock if ((spa = spa_lookup(name)) == NULL) { 24611544Seschrock mutex_exit(&spa_namespace_lock); 24621544Seschrock return (NULL); 24631544Seschrock } 24641544Seschrock spa->spa_inject_ref++; 24651544Seschrock mutex_exit(&spa_namespace_lock); 24661544Seschrock 24671544Seschrock return (spa); 24681544Seschrock } 24691544Seschrock 24701544Seschrock void 24711544Seschrock spa_inject_delref(spa_t *spa) 24721544Seschrock { 24731544Seschrock mutex_enter(&spa_namespace_lock); 24741544Seschrock spa->spa_inject_ref--; 24751544Seschrock mutex_exit(&spa_namespace_lock); 24761544Seschrock } 24771544Seschrock 24785450Sbrendan /* 24795450Sbrendan * Add spares device information to the nvlist. 24805450Sbrendan */ 24812082Seschrock static void 24822082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 24832082Seschrock { 24842082Seschrock nvlist_t **spares; 24852082Seschrock uint_t i, nspares; 24862082Seschrock nvlist_t *nvroot; 24872082Seschrock uint64_t guid; 24882082Seschrock vdev_stat_t *vs; 24892082Seschrock uint_t vsc; 24903377Seschrock uint64_t pool; 24912082Seschrock 24929425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 24939425SEric.Schrock@Sun.COM 24945450Sbrendan if (spa->spa_spares.sav_count == 0) 24952082Seschrock return; 24962082Seschrock 24972082Seschrock VERIFY(nvlist_lookup_nvlist(config, 24982082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 24995450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 25002082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 25012082Seschrock if (nspares != 0) { 25022082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 25032082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 25042082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 25052082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 25062082Seschrock 25072082Seschrock /* 25082082Seschrock * Go through and find any spares which have since been 25092082Seschrock * repurposed as an active spare. If this is the case, update 25102082Seschrock * their status appropriately. 25112082Seschrock */ 25122082Seschrock for (i = 0; i < nspares; i++) { 25132082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 25142082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 25157214Slling if (spa_spare_exists(guid, &pool, NULL) && 25167214Slling pool != 0ULL) { 25172082Seschrock VERIFY(nvlist_lookup_uint64_array( 251812296SLin.Ling@Sun.COM spares[i], ZPOOL_CONFIG_VDEV_STATS, 25192082Seschrock (uint64_t **)&vs, &vsc) == 0); 25202082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 25212082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 25222082Seschrock } 25232082Seschrock } 25242082Seschrock } 25252082Seschrock } 25262082Seschrock 25275450Sbrendan /* 25285450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 25295450Sbrendan */ 25305450Sbrendan static void 25315450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 25325450Sbrendan { 25335450Sbrendan nvlist_t **l2cache; 25345450Sbrendan uint_t i, j, nl2cache; 25355450Sbrendan nvlist_t *nvroot; 25365450Sbrendan uint64_t guid; 25375450Sbrendan vdev_t *vd; 25385450Sbrendan vdev_stat_t *vs; 25395450Sbrendan uint_t vsc; 25405450Sbrendan 25419425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 25429425SEric.Schrock@Sun.COM 25435450Sbrendan if (spa->spa_l2cache.sav_count == 0) 25445450Sbrendan return; 25455450Sbrendan 25465450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 25475450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 25485450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 25495450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 25505450Sbrendan if (nl2cache != 0) { 25515450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 25525450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 25535450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 25545450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 25555450Sbrendan 25565450Sbrendan /* 25575450Sbrendan * Update level 2 cache device stats. 25585450Sbrendan */ 25595450Sbrendan 25605450Sbrendan for (i = 0; i < nl2cache; i++) { 25615450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 25625450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 25635450Sbrendan 25645450Sbrendan vd = NULL; 25655450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 25665450Sbrendan if (guid == 25675450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 25685450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 25695450Sbrendan break; 25705450Sbrendan } 25715450Sbrendan } 25725450Sbrendan ASSERT(vd != NULL); 25735450Sbrendan 25745450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 257512296SLin.Ling@Sun.COM ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 257612296SLin.Ling@Sun.COM == 0); 25775450Sbrendan vdev_get_stats(vd, vs); 25785450Sbrendan } 25795450Sbrendan } 25805450Sbrendan } 25815450Sbrendan 2582789Sahrens int 25831544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 2584789Sahrens { 2585789Sahrens int error; 2586789Sahrens spa_t *spa; 2587789Sahrens 2588789Sahrens *config = NULL; 258910921STim.Haley@Sun.COM error = spa_open_common(name, &spa, FTAG, NULL, config); 2590789Sahrens 25919425SEric.Schrock@Sun.COM if (spa != NULL) { 25929425SEric.Schrock@Sun.COM /* 25939425SEric.Schrock@Sun.COM * This still leaves a window of inconsistency where the spares 25949425SEric.Schrock@Sun.COM * or l2cache devices could change and the config would be 25959425SEric.Schrock@Sun.COM * self-inconsistent. 25969425SEric.Schrock@Sun.COM */ 25979425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 25989425SEric.Schrock@Sun.COM 25999425SEric.Schrock@Sun.COM if (*config != NULL) { 260012817STim.Haley@Sun.COM uint64_t loadtimes[2]; 260112817STim.Haley@Sun.COM 260212817STim.Haley@Sun.COM loadtimes[0] = spa->spa_loaded_ts.tv_sec; 260312817STim.Haley@Sun.COM loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 260412817STim.Haley@Sun.COM VERIFY(nvlist_add_uint64_array(*config, 260512817STim.Haley@Sun.COM ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 260612817STim.Haley@Sun.COM 26077754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 26089425SEric.Schrock@Sun.COM ZPOOL_CONFIG_ERRCOUNT, 26099425SEric.Schrock@Sun.COM spa_get_errlog_size(spa)) == 0); 26109425SEric.Schrock@Sun.COM 26119425SEric.Schrock@Sun.COM if (spa_suspended(spa)) 26129425SEric.Schrock@Sun.COM VERIFY(nvlist_add_uint64(*config, 26139425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SUSPENDED, 26149425SEric.Schrock@Sun.COM spa->spa_failmode) == 0); 26159425SEric.Schrock@Sun.COM 26169425SEric.Schrock@Sun.COM spa_add_spares(spa, *config); 26179425SEric.Schrock@Sun.COM spa_add_l2cache(spa, *config); 26189425SEric.Schrock@Sun.COM } 26192082Seschrock } 26202082Seschrock 26211544Seschrock /* 26221544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 26231544Seschrock * and call spa_lookup() directly. 26241544Seschrock */ 26251544Seschrock if (altroot) { 26261544Seschrock if (spa == NULL) { 26271544Seschrock mutex_enter(&spa_namespace_lock); 26281544Seschrock spa = spa_lookup(name); 26291544Seschrock if (spa) 26301544Seschrock spa_altroot(spa, altroot, buflen); 26311544Seschrock else 26321544Seschrock altroot[0] = '\0'; 26331544Seschrock spa = NULL; 26341544Seschrock mutex_exit(&spa_namespace_lock); 26351544Seschrock } else { 26361544Seschrock spa_altroot(spa, altroot, buflen); 26371544Seschrock } 26381544Seschrock } 26391544Seschrock 26409425SEric.Schrock@Sun.COM if (spa != NULL) { 26419425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 2642789Sahrens spa_close(spa, FTAG); 26439425SEric.Schrock@Sun.COM } 2644789Sahrens 2645789Sahrens return (error); 2646789Sahrens } 2647789Sahrens 2648789Sahrens /* 26495450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 26505450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 26515450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 26525450Sbrendan * specified, as long as they are well-formed. 26532082Seschrock */ 26542082Seschrock static int 26555450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 26565450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 26575450Sbrendan vdev_labeltype_t label) 26582082Seschrock { 26595450Sbrendan nvlist_t **dev; 26605450Sbrendan uint_t i, ndev; 26612082Seschrock vdev_t *vd; 26622082Seschrock int error; 26632082Seschrock 26647754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 26657754SJeff.Bonwick@Sun.COM 26662082Seschrock /* 26675450Sbrendan * It's acceptable to have no devs specified. 26682082Seschrock */ 26695450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 26702082Seschrock return (0); 26712082Seschrock 26725450Sbrendan if (ndev == 0) 26732082Seschrock return (EINVAL); 26742082Seschrock 26752082Seschrock /* 26765450Sbrendan * Make sure the pool is formatted with a version that supports this 26775450Sbrendan * device type. 26782082Seschrock */ 26795450Sbrendan if (spa_version(spa) < version) 26802082Seschrock return (ENOTSUP); 26812082Seschrock 26823377Seschrock /* 26835450Sbrendan * Set the pending device list so we correctly handle device in-use 26843377Seschrock * checking. 26853377Seschrock */ 26865450Sbrendan sav->sav_pending = dev; 26875450Sbrendan sav->sav_npending = ndev; 26885450Sbrendan 26895450Sbrendan for (i = 0; i < ndev; i++) { 26905450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 26912082Seschrock mode)) != 0) 26923377Seschrock goto out; 26932082Seschrock 26942082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 26952082Seschrock vdev_free(vd); 26963377Seschrock error = EINVAL; 26973377Seschrock goto out; 26982082Seschrock } 26992082Seschrock 27005450Sbrendan /* 27017754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 27027754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 27035450Sbrendan */ 27047754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 27055450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 27065450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 27075450Sbrendan error = ENOTBLK; 27085450Sbrendan goto out; 27095450Sbrendan } 27107754SJeff.Bonwick@Sun.COM #endif 27112082Seschrock vd->vdev_top = vd; 27123377Seschrock 27133377Seschrock if ((error = vdev_open(vd)) == 0 && 27145450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 27155450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 27163377Seschrock vd->vdev_guid) == 0); 27172082Seschrock } 27182082Seschrock 27192082Seschrock vdev_free(vd); 27203377Seschrock 27215450Sbrendan if (error && 27225450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 27233377Seschrock goto out; 27243377Seschrock else 27253377Seschrock error = 0; 27262082Seschrock } 27272082Seschrock 27283377Seschrock out: 27295450Sbrendan sav->sav_pending = NULL; 27305450Sbrendan sav->sav_npending = 0; 27313377Seschrock return (error); 27322082Seschrock } 27332082Seschrock 27345450Sbrendan static int 27355450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 27365450Sbrendan { 27375450Sbrendan int error; 27385450Sbrendan 27397754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 27407754SJeff.Bonwick@Sun.COM 27415450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 27425450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 27435450Sbrendan VDEV_LABEL_SPARE)) != 0) { 27445450Sbrendan return (error); 27455450Sbrendan } 27465450Sbrendan 27475450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 27485450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 27495450Sbrendan VDEV_LABEL_L2CACHE)); 27505450Sbrendan } 27515450Sbrendan 27525450Sbrendan static void 27535450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 27545450Sbrendan const char *config) 27555450Sbrendan { 27565450Sbrendan int i; 27575450Sbrendan 27585450Sbrendan if (sav->sav_config != NULL) { 27595450Sbrendan nvlist_t **olddevs; 27605450Sbrendan uint_t oldndevs; 27615450Sbrendan nvlist_t **newdevs; 27625450Sbrendan 27635450Sbrendan /* 27645450Sbrendan * Generate new dev list by concatentating with the 27655450Sbrendan * current dev list. 27665450Sbrendan */ 27675450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 27685450Sbrendan &olddevs, &oldndevs) == 0); 27695450Sbrendan 27705450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 27715450Sbrendan (ndevs + oldndevs), KM_SLEEP); 27725450Sbrendan for (i = 0; i < oldndevs; i++) 27735450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 27745450Sbrendan KM_SLEEP) == 0); 27755450Sbrendan for (i = 0; i < ndevs; i++) 27765450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 27775450Sbrendan KM_SLEEP) == 0); 27785450Sbrendan 27795450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 27805450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 27815450Sbrendan 27825450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 27835450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 27845450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 27855450Sbrendan nvlist_free(newdevs[i]); 27865450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 27875450Sbrendan } else { 27885450Sbrendan /* 27895450Sbrendan * Generate a new dev list. 27905450Sbrendan */ 27915450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 27925450Sbrendan KM_SLEEP) == 0); 27935450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 27945450Sbrendan devs, ndevs) == 0); 27955450Sbrendan } 27965450Sbrendan } 27975450Sbrendan 27985450Sbrendan /* 27995450Sbrendan * Stop and drop level 2 ARC devices 28005450Sbrendan */ 28015450Sbrendan void 28025450Sbrendan spa_l2cache_drop(spa_t *spa) 28035450Sbrendan { 28045450Sbrendan vdev_t *vd; 28055450Sbrendan int i; 28065450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 28075450Sbrendan 28085450Sbrendan for (i = 0; i < sav->sav_count; i++) { 28095450Sbrendan uint64_t pool; 28105450Sbrendan 28115450Sbrendan vd = sav->sav_vdevs[i]; 28125450Sbrendan ASSERT(vd != NULL); 28135450Sbrendan 28148241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 28158241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 28165450Sbrendan l2arc_remove_vdev(vd); 28175450Sbrendan if (vd->vdev_isl2cache) 28185450Sbrendan spa_l2cache_remove(vd); 28195450Sbrendan vdev_clear_stats(vd); 28205450Sbrendan (void) vdev_close(vd); 28215450Sbrendan } 28225450Sbrendan } 28235450Sbrendan 28242082Seschrock /* 2825789Sahrens * Pool Creation 2826789Sahrens */ 2827789Sahrens int 28285094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 28297184Stimh const char *history_str, nvlist_t *zplprops) 2830789Sahrens { 2831789Sahrens spa_t *spa; 28325094Slling char *altroot = NULL; 28331635Sbonwick vdev_t *rvd; 2834789Sahrens dsl_pool_t *dp; 2835789Sahrens dmu_tx_t *tx; 28369816SGeorge.Wilson@Sun.COM int error = 0; 2837789Sahrens uint64_t txg = TXG_INITIAL; 28385450Sbrendan nvlist_t **spares, **l2cache; 28395450Sbrendan uint_t nspares, nl2cache; 284012470SMatthew.Ahrens@Sun.COM uint64_t version, obj; 2841789Sahrens 2842789Sahrens /* 2843789Sahrens * If this pool already exists, return failure. 2844789Sahrens */ 2845789Sahrens mutex_enter(&spa_namespace_lock); 2846789Sahrens if (spa_lookup(pool) != NULL) { 2847789Sahrens mutex_exit(&spa_namespace_lock); 2848789Sahrens return (EEXIST); 2849789Sahrens } 2850789Sahrens 2851789Sahrens /* 2852789Sahrens * Allocate a new spa_t structure. 2853789Sahrens */ 28545094Slling (void) nvlist_lookup_string(props, 28555094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 285610921STim.Haley@Sun.COM spa = spa_add(pool, NULL, altroot); 28578241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2858789Sahrens 28595094Slling if (props && (error = spa_prop_validate(spa, props))) { 28605094Slling spa_deactivate(spa); 28615094Slling spa_remove(spa); 28626643Seschrock mutex_exit(&spa_namespace_lock); 28635094Slling return (error); 28645094Slling } 28655094Slling 28665094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 28675094Slling &version) != 0) 28685094Slling version = SPA_VERSION; 28695094Slling ASSERT(version <= SPA_VERSION); 287010922SJeff.Bonwick@Sun.COM 287110922SJeff.Bonwick@Sun.COM spa->spa_first_txg = txg; 287210922SJeff.Bonwick@Sun.COM spa->spa_uberblock.ub_txg = txg - 1; 28735094Slling spa->spa_uberblock.ub_version = version; 2874789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2875789Sahrens 28761635Sbonwick /* 28779234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 28789234SGeorge.Wilson@Sun.COM */ 28799630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 28809630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 28819234SGeorge.Wilson@Sun.COM 28829234SGeorge.Wilson@Sun.COM /* 28831635Sbonwick * Create the root vdev. 28841635Sbonwick */ 28857754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 28861635Sbonwick 28872082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 28882082Seschrock 28892082Seschrock ASSERT(error != 0 || rvd != NULL); 28902082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 28912082Seschrock 28925913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 28931635Sbonwick error = EINVAL; 28942082Seschrock 28952082Seschrock if (error == 0 && 28962082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 28975450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 28982082Seschrock VDEV_ALLOC_ADD)) == 0) { 28999816SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 29009816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(rvd->vdev_child[c]); 29019816SGeorge.Wilson@Sun.COM vdev_expand(rvd->vdev_child[c], txg); 29029816SGeorge.Wilson@Sun.COM } 29031635Sbonwick } 29041635Sbonwick 29057754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2906789Sahrens 29072082Seschrock if (error != 0) { 2908789Sahrens spa_unload(spa); 2909789Sahrens spa_deactivate(spa); 2910789Sahrens spa_remove(spa); 2911789Sahrens mutex_exit(&spa_namespace_lock); 2912789Sahrens return (error); 2913789Sahrens } 2914789Sahrens 29152082Seschrock /* 29162082Seschrock * Get the list of spares, if specified. 29172082Seschrock */ 29182082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29192082Seschrock &spares, &nspares) == 0) { 29205450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 29212082Seschrock KM_SLEEP) == 0); 29225450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 29232082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 29247754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29252082Seschrock spa_load_spares(spa); 29267754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29275450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 29285450Sbrendan } 29295450Sbrendan 29305450Sbrendan /* 29315450Sbrendan * Get the list of level 2 cache devices, if specified. 29325450Sbrendan */ 29335450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 29345450Sbrendan &l2cache, &nl2cache) == 0) { 29355450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 29365450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 29375450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 29385450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 29397754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29405450Sbrendan spa_load_l2cache(spa); 29417754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29425450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 29432082Seschrock } 29442082Seschrock 29457184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2946789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2947789Sahrens 294810956SGeorge.Wilson@Sun.COM /* 294910956SGeorge.Wilson@Sun.COM * Create DDTs (dedup tables). 295010956SGeorge.Wilson@Sun.COM */ 295110956SGeorge.Wilson@Sun.COM ddt_create(spa); 295210956SGeorge.Wilson@Sun.COM 295310956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 295410956SGeorge.Wilson@Sun.COM 2955789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2956789Sahrens 2957789Sahrens /* 2958789Sahrens * Create the pool config object. 2959789Sahrens */ 2960789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 29617497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2962789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2963789Sahrens 29641544Seschrock if (zap_add(spa->spa_meta_objset, 2965789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 29661544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 29671544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 29681544Seschrock } 2969789Sahrens 297012296SLin.Ling@Sun.COM if (zap_add(spa->spa_meta_objset, 297112296SLin.Ling@Sun.COM DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 297212296SLin.Ling@Sun.COM sizeof (uint64_t), 1, &version, tx) != 0) { 297312296SLin.Ling@Sun.COM cmn_err(CE_PANIC, "failed to add pool version"); 297412296SLin.Ling@Sun.COM } 297512296SLin.Ling@Sun.COM 29765094Slling /* Newly created pools with the right version are always deflated. */ 29775094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 29785094Slling spa->spa_deflate = TRUE; 29795094Slling if (zap_add(spa->spa_meta_objset, 29805094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 29815094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 29825094Slling cmn_err(CE_PANIC, "failed to add deflate"); 29835094Slling } 29842082Seschrock } 29852082Seschrock 2986789Sahrens /* 298712470SMatthew.Ahrens@Sun.COM * Create the deferred-free bpobj. Turn off compression 2988789Sahrens * because sync-to-convergence takes longer if the blocksize 2989789Sahrens * keeps changing. 2990789Sahrens */ 299112470SMatthew.Ahrens@Sun.COM obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 299212470SMatthew.Ahrens@Sun.COM dmu_object_set_compress(spa->spa_meta_objset, obj, 299312470SMatthew.Ahrens@Sun.COM ZIO_COMPRESS_OFF, tx); 29941544Seschrock if (zap_add(spa->spa_meta_objset, 299512470SMatthew.Ahrens@Sun.COM DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 299612470SMatthew.Ahrens@Sun.COM sizeof (uint64_t), 1, &obj, tx) != 0) { 299712470SMatthew.Ahrens@Sun.COM cmn_err(CE_PANIC, "failed to add bpobj"); 29981544Seschrock } 299912470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 300012470SMatthew.Ahrens@Sun.COM spa->spa_meta_objset, obj)); 3001789Sahrens 30022926Sek110237 /* 30032926Sek110237 * Create the pool's history object. 30042926Sek110237 */ 30055094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 30065094Slling spa_history_create_obj(spa, tx); 30075094Slling 30085094Slling /* 30095094Slling * Set pool properties. 30105094Slling */ 30115094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 30125094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 30135329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 30149816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 301510922SJeff.Bonwick@Sun.COM 30168525SEric.Schrock@Sun.COM if (props != NULL) { 30178525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 301812296SLin.Ling@Sun.COM spa_sync_props(spa, props, tx); 30198525SEric.Schrock@Sun.COM } 30202926Sek110237 3021789Sahrens dmu_tx_commit(tx); 3022789Sahrens 3023789Sahrens spa->spa_sync_on = B_TRUE; 3024789Sahrens txg_sync_start(spa->spa_dsl_pool); 3025789Sahrens 3026789Sahrens /* 3027789Sahrens * We explicitly wait for the first transaction to complete so that our 3028789Sahrens * bean counters are appropriately updated. 3029789Sahrens */ 3030789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3031789Sahrens 30326643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3033789Sahrens 30345094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 30354715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 30369946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_CREATE); 30374715Sek110237 30388667SGeorge.Wilson@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 30398667SGeorge.Wilson@Sun.COM 3040789Sahrens mutex_exit(&spa_namespace_lock); 3041789Sahrens 3042789Sahrens return (0); 3043789Sahrens } 3044789Sahrens 30456423Sgw25295 #ifdef _KERNEL 30466423Sgw25295 /* 30479790SLin.Ling@Sun.COM * Get the root pool information from the root disk, then import the root pool 30489790SLin.Ling@Sun.COM * during the system boot up time. 30496423Sgw25295 */ 30509790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 30519790SLin.Ling@Sun.COM 30529790SLin.Ling@Sun.COM static nvlist_t * 30539790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 30546423Sgw25295 { 30559790SLin.Ling@Sun.COM nvlist_t *config; 30566423Sgw25295 nvlist_t *nvtop, *nvroot; 30576423Sgw25295 uint64_t pgid; 30586423Sgw25295 30599790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 30609790SLin.Ling@Sun.COM return (NULL); 30619790SLin.Ling@Sun.COM 30626423Sgw25295 /* 30636423Sgw25295 * Add this top-level vdev to the child array. 30646423Sgw25295 */ 30659790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 30669790SLin.Ling@Sun.COM &nvtop) == 0); 30679790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 30689790SLin.Ling@Sun.COM &pgid) == 0); 30699790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 30706423Sgw25295 30716423Sgw25295 /* 30726423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 30736423Sgw25295 */ 30746423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 30759790SLin.Ling@Sun.COM VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 30769790SLin.Ling@Sun.COM VDEV_TYPE_ROOT) == 0); 30776423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 30786423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 30796423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 30806423Sgw25295 &nvtop, 1) == 0); 30816423Sgw25295 30826423Sgw25295 /* 30836423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 30846423Sgw25295 * this pool's configuration (remove the old, add the new). 30856423Sgw25295 */ 30866423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 30876423Sgw25295 nvlist_free(nvroot); 30889790SLin.Ling@Sun.COM return (config); 30896423Sgw25295 } 30906423Sgw25295 30916423Sgw25295 /* 30929790SLin.Ling@Sun.COM * Walk the vdev tree and see if we can find a device with "better" 30939790SLin.Ling@Sun.COM * configuration. A configuration is "better" if the label on that 30949790SLin.Ling@Sun.COM * device has a more recent txg. 30956423Sgw25295 */ 30969790SLin.Ling@Sun.COM static void 30979790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 30987147Staylor { 30999816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 31009790SLin.Ling@Sun.COM spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 31019790SLin.Ling@Sun.COM 31029790SLin.Ling@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 31039790SLin.Ling@Sun.COM nvlist_t *label; 31049790SLin.Ling@Sun.COM uint64_t label_txg; 31059790SLin.Ling@Sun.COM 31069790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 31079790SLin.Ling@Sun.COM &label) != 0) 31089790SLin.Ling@Sun.COM return; 31099790SLin.Ling@Sun.COM 31109790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 31119790SLin.Ling@Sun.COM &label_txg) == 0); 31129790SLin.Ling@Sun.COM 31139790SLin.Ling@Sun.COM /* 31149790SLin.Ling@Sun.COM * Do we have a better boot device? 31159790SLin.Ling@Sun.COM */ 31169790SLin.Ling@Sun.COM if (label_txg > *txg) { 31179790SLin.Ling@Sun.COM *txg = label_txg; 31189790SLin.Ling@Sun.COM *avd = vd; 31197147Staylor } 31209790SLin.Ling@Sun.COM nvlist_free(label); 31217147Staylor } 31227147Staylor } 31237147Staylor 31246423Sgw25295 /* 31256423Sgw25295 * Import a root pool. 31266423Sgw25295 * 31277147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 31287147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 31297147Staylor * The GRUB "findroot" command will return the vdev we should boot. 31306423Sgw25295 * 31316423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 31326423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 31336423Sgw25295 * e.g. 31346423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 31356423Sgw25295 */ 31366423Sgw25295 int 31377147Staylor spa_import_rootpool(char *devpath, char *devid) 31386423Sgw25295 { 31399790SLin.Ling@Sun.COM spa_t *spa; 31409790SLin.Ling@Sun.COM vdev_t *rvd, *bvd, *avd = NULL; 31419790SLin.Ling@Sun.COM nvlist_t *config, *nvtop; 31429790SLin.Ling@Sun.COM uint64_t guid, txg; 31436423Sgw25295 char *pname; 31446423Sgw25295 int error; 31456423Sgw25295 31466423Sgw25295 /* 31479790SLin.Ling@Sun.COM * Read the label from the boot device and generate a configuration. 31486423Sgw25295 */ 314910822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 315010822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL) 315110822SJack.Meng@Sun.COM if (config == NULL) { 315210822SJack.Meng@Sun.COM if (strstr(devpath, "/iscsi/ssd") != NULL) { 315310822SJack.Meng@Sun.COM /* iscsi boot */ 315410822SJack.Meng@Sun.COM get_iscsi_bootpath_phy(devpath); 315510822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 315610822SJack.Meng@Sun.COM } 315710822SJack.Meng@Sun.COM } 315810822SJack.Meng@Sun.COM #endif 315910822SJack.Meng@Sun.COM if (config == NULL) { 31609790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 31619790SLin.Ling@Sun.COM devpath); 31629790SLin.Ling@Sun.COM return (EIO); 31639790SLin.Ling@Sun.COM } 31649790SLin.Ling@Sun.COM 31659790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 31669790SLin.Ling@Sun.COM &pname) == 0); 31679790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 31686423Sgw25295 31699425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 31709425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pname)) != NULL) { 31719425SEric.Schrock@Sun.COM /* 31729425SEric.Schrock@Sun.COM * Remove the existing root pool from the namespace so that we 31739425SEric.Schrock@Sun.COM * can replace it with the correct config we just read in. 31749425SEric.Schrock@Sun.COM */ 31759425SEric.Schrock@Sun.COM spa_remove(spa); 31769425SEric.Schrock@Sun.COM } 31779425SEric.Schrock@Sun.COM 317810921STim.Haley@Sun.COM spa = spa_add(pname, config, NULL); 31799425SEric.Schrock@Sun.COM spa->spa_is_root = B_TRUE; 3180*12949SGeorge.Wilson@Sun.COM spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 31819790SLin.Ling@Sun.COM 31829790SLin.Ling@Sun.COM /* 31839790SLin.Ling@Sun.COM * Build up a vdev tree based on the boot device's label config. 31849790SLin.Ling@Sun.COM */ 31859790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 31869790SLin.Ling@Sun.COM &nvtop) == 0); 31879790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 31889790SLin.Ling@Sun.COM error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 31899790SLin.Ling@Sun.COM VDEV_ALLOC_ROOTPOOL); 31909790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 31919790SLin.Ling@Sun.COM if (error) { 31929790SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 31939790SLin.Ling@Sun.COM nvlist_free(config); 31949790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 31959790SLin.Ling@Sun.COM pname); 31969790SLin.Ling@Sun.COM return (error); 31979790SLin.Ling@Sun.COM } 31989790SLin.Ling@Sun.COM 31999790SLin.Ling@Sun.COM /* 32009790SLin.Ling@Sun.COM * Get the boot vdev. 32019790SLin.Ling@Sun.COM */ 32029790SLin.Ling@Sun.COM if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 32039790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 32049790SLin.Ling@Sun.COM (u_longlong_t)guid); 32059790SLin.Ling@Sun.COM error = ENOENT; 32069790SLin.Ling@Sun.COM goto out; 32079790SLin.Ling@Sun.COM } 32089790SLin.Ling@Sun.COM 32099790SLin.Ling@Sun.COM /* 32109790SLin.Ling@Sun.COM * Determine if there is a better boot device. 32119790SLin.Ling@Sun.COM */ 32129790SLin.Ling@Sun.COM avd = bvd; 32139790SLin.Ling@Sun.COM spa_alt_rootvdev(rvd, &avd, &txg); 32149790SLin.Ling@Sun.COM if (avd != bvd) { 32159790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 32169790SLin.Ling@Sun.COM "try booting from '%s'", avd->vdev_path); 32179790SLin.Ling@Sun.COM error = EINVAL; 32189790SLin.Ling@Sun.COM goto out; 32199790SLin.Ling@Sun.COM } 32209790SLin.Ling@Sun.COM 32219790SLin.Ling@Sun.COM /* 32229790SLin.Ling@Sun.COM * If the boot device is part of a spare vdev then ensure that 32239790SLin.Ling@Sun.COM * we're booting off the active spare. 32249790SLin.Ling@Sun.COM */ 32259790SLin.Ling@Sun.COM if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 32269790SLin.Ling@Sun.COM !bvd->vdev_isspare) { 32279790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is currently spared. Please " 32289790SLin.Ling@Sun.COM "try booting from '%s'", 32299790SLin.Ling@Sun.COM bvd->vdev_parent->vdev_child[1]->vdev_path); 32309790SLin.Ling@Sun.COM error = EINVAL; 32319790SLin.Ling@Sun.COM goto out; 32329790SLin.Ling@Sun.COM } 32339790SLin.Ling@Sun.COM 32349790SLin.Ling@Sun.COM error = 0; 32359946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 32369790SLin.Ling@Sun.COM out: 32379790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 32389790SLin.Ling@Sun.COM vdev_free(rvd); 32399790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 32409425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 32416423Sgw25295 32429790SLin.Ling@Sun.COM nvlist_free(config); 32436423Sgw25295 return (error); 32446423Sgw25295 } 32459790SLin.Ling@Sun.COM 32466423Sgw25295 #endif 32476423Sgw25295 32486423Sgw25295 /* 32496423Sgw25295 * Import a non-root pool into the system. 32506423Sgw25295 */ 32516423Sgw25295 int 3252*12949SGeorge.Wilson@Sun.COM spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 32536423Sgw25295 { 32549425SEric.Schrock@Sun.COM spa_t *spa; 32559425SEric.Schrock@Sun.COM char *altroot = NULL; 325610921STim.Haley@Sun.COM spa_load_state_t state = SPA_LOAD_IMPORT; 325710921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 32589425SEric.Schrock@Sun.COM int error; 32599425SEric.Schrock@Sun.COM nvlist_t *nvroot; 32609425SEric.Schrock@Sun.COM nvlist_t **spares, **l2cache; 32619425SEric.Schrock@Sun.COM uint_t nspares, nl2cache; 32629425SEric.Schrock@Sun.COM 32639425SEric.Schrock@Sun.COM /* 32649425SEric.Schrock@Sun.COM * If a pool with this name exists, return failure. 32659425SEric.Schrock@Sun.COM */ 32669425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 326711422SMark.Musante@Sun.COM if (spa_lookup(pool) != NULL) { 32689425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 32699425SEric.Schrock@Sun.COM return (EEXIST); 32709425SEric.Schrock@Sun.COM } 32719425SEric.Schrock@Sun.COM 32729425SEric.Schrock@Sun.COM /* 32739425SEric.Schrock@Sun.COM * Create and initialize the spa structure. 32749425SEric.Schrock@Sun.COM */ 32759425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 32769425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 327710921STim.Haley@Sun.COM spa = spa_add(pool, config, altroot); 3278*12949SGeorge.Wilson@Sun.COM spa->spa_import_flags = flags; 3279*12949SGeorge.Wilson@Sun.COM 3280*12949SGeorge.Wilson@Sun.COM /* 3281*12949SGeorge.Wilson@Sun.COM * Verbatim import - Take a pool and insert it into the namespace 3282*12949SGeorge.Wilson@Sun.COM * as if it had been loaded at boot. 3283*12949SGeorge.Wilson@Sun.COM */ 3284*12949SGeorge.Wilson@Sun.COM if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 3285*12949SGeorge.Wilson@Sun.COM if (props != NULL) 3286*12949SGeorge.Wilson@Sun.COM spa_configfile_set(spa, props, B_FALSE); 3287*12949SGeorge.Wilson@Sun.COM 3288*12949SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 3289*12949SGeorge.Wilson@Sun.COM 3290*12949SGeorge.Wilson@Sun.COM mutex_exit(&spa_namespace_lock); 3291*12949SGeorge.Wilson@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 3292*12949SGeorge.Wilson@Sun.COM 3293*12949SGeorge.Wilson@Sun.COM return (0); 3294*12949SGeorge.Wilson@Sun.COM } 3295*12949SGeorge.Wilson@Sun.COM 32969425SEric.Schrock@Sun.COM spa_activate(spa, spa_mode_global); 32979425SEric.Schrock@Sun.COM 32989425SEric.Schrock@Sun.COM /* 32999630SJeff.Bonwick@Sun.COM * Don't start async tasks until we know everything is healthy. 33009630SJeff.Bonwick@Sun.COM */ 33019630SJeff.Bonwick@Sun.COM spa_async_suspend(spa); 33029630SJeff.Bonwick@Sun.COM 3303*12949SGeorge.Wilson@Sun.COM zpool_get_rewind_policy(config, &policy); 3304*12949SGeorge.Wilson@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 3305*12949SGeorge.Wilson@Sun.COM state = SPA_LOAD_RECOVER; 3306*12949SGeorge.Wilson@Sun.COM 33079630SJeff.Bonwick@Sun.COM /* 33089425SEric.Schrock@Sun.COM * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 33099425SEric.Schrock@Sun.COM * because the user-supplied config is actually the one to trust when 33109425SEric.Schrock@Sun.COM * doing an import. 33119425SEric.Schrock@Sun.COM */ 331210921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 331310921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 3314*12949SGeorge.Wilson@Sun.COM 331510921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 331611727SVictor.Latushkin@Sun.COM policy.zrp_request); 331710921STim.Haley@Sun.COM 331810921STim.Haley@Sun.COM /* 3319*12949SGeorge.Wilson@Sun.COM * Propagate anything learned while loading the pool and pass it 3320*12949SGeorge.Wilson@Sun.COM * back to caller (i.e. rewind info, missing devices, etc). 332110921STim.Haley@Sun.COM */ 3322*12949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 3323*12949SGeorge.Wilson@Sun.COM spa->spa_load_info) == 0); 33249425SEric.Schrock@Sun.COM 33259425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33269425SEric.Schrock@Sun.COM /* 33279425SEric.Schrock@Sun.COM * Toss any existing sparelist, as it doesn't have any validity 33289425SEric.Schrock@Sun.COM * anymore, and conflicts with spa_has_spare(). 33299425SEric.Schrock@Sun.COM */ 33309425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) { 33319425SEric.Schrock@Sun.COM nvlist_free(spa->spa_spares.sav_config); 33329425SEric.Schrock@Sun.COM spa->spa_spares.sav_config = NULL; 33339425SEric.Schrock@Sun.COM spa_load_spares(spa); 33349425SEric.Schrock@Sun.COM } 33359425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) { 33369425SEric.Schrock@Sun.COM nvlist_free(spa->spa_l2cache.sav_config); 33379425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_config = NULL; 33389425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 33399425SEric.Schrock@Sun.COM } 33409425SEric.Schrock@Sun.COM 33419425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 33429425SEric.Schrock@Sun.COM &nvroot) == 0); 33439425SEric.Schrock@Sun.COM if (error == 0) 33449425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 33459425SEric.Schrock@Sun.COM VDEV_ALLOC_SPARE); 33469425SEric.Schrock@Sun.COM if (error == 0) 33479425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 33489425SEric.Schrock@Sun.COM VDEV_ALLOC_L2CACHE); 33499425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 33509425SEric.Schrock@Sun.COM 33519425SEric.Schrock@Sun.COM if (props != NULL) 33529425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 33539425SEric.Schrock@Sun.COM 33549425SEric.Schrock@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 33559425SEric.Schrock@Sun.COM (error = spa_prop_set(spa, props)))) { 33569425SEric.Schrock@Sun.COM spa_unload(spa); 33579425SEric.Schrock@Sun.COM spa_deactivate(spa); 33589425SEric.Schrock@Sun.COM spa_remove(spa); 33599425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 33609425SEric.Schrock@Sun.COM return (error); 33619425SEric.Schrock@Sun.COM } 33629425SEric.Schrock@Sun.COM 336312600SLin.Ling@Sun.COM spa_async_resume(spa); 336412600SLin.Ling@Sun.COM 33659425SEric.Schrock@Sun.COM /* 33669425SEric.Schrock@Sun.COM * Override any spares and level 2 cache devices as specified by 33679425SEric.Schrock@Sun.COM * the user, as these may have correct device names/devids, etc. 33689425SEric.Schrock@Sun.COM */ 33699425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 33709425SEric.Schrock@Sun.COM &spares, &nspares) == 0) { 33719425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) 33729425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_spares.sav_config, 33739425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 33749425SEric.Schrock@Sun.COM else 33759425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 33769425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 33779425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 33789425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 33799425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33809425SEric.Schrock@Sun.COM spa_load_spares(spa); 33819425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 33829425SEric.Schrock@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 33839425SEric.Schrock@Sun.COM } 33849425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 33859425SEric.Schrock@Sun.COM &l2cache, &nl2cache) == 0) { 33869425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) 33879425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 33889425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 33899425SEric.Schrock@Sun.COM else 33909425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 33919425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 33929425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 33939425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 33949425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33959425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 33969425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 33979425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_sync = B_TRUE; 33989425SEric.Schrock@Sun.COM } 33999425SEric.Schrock@Sun.COM 340010672SEric.Schrock@Sun.COM /* 340110672SEric.Schrock@Sun.COM * Check for any removed devices. 340210672SEric.Schrock@Sun.COM */ 340310672SEric.Schrock@Sun.COM if (spa->spa_autoreplace) { 340410672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 340510672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 340610672SEric.Schrock@Sun.COM } 340710672SEric.Schrock@Sun.COM 34089425SEric.Schrock@Sun.COM if (spa_writeable(spa)) { 34099425SEric.Schrock@Sun.COM /* 34109425SEric.Schrock@Sun.COM * Update the config cache to include the newly-imported pool. 34119425SEric.Schrock@Sun.COM */ 341210100SLin.Ling@Sun.COM spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 34139425SEric.Schrock@Sun.COM } 34149425SEric.Schrock@Sun.COM 34159816SGeorge.Wilson@Sun.COM /* 34169816SGeorge.Wilson@Sun.COM * It's possible that the pool was expanded while it was exported. 34179816SGeorge.Wilson@Sun.COM * We kick off an async task to handle this for us. 34189816SGeorge.Wilson@Sun.COM */ 34199816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 34209816SGeorge.Wilson@Sun.COM 34219425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 34229946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 34239425SEric.Schrock@Sun.COM 34249425SEric.Schrock@Sun.COM return (0); 34256643Seschrock } 34266643Seschrock 3427789Sahrens nvlist_t * 3428789Sahrens spa_tryimport(nvlist_t *tryconfig) 3429789Sahrens { 3430789Sahrens nvlist_t *config = NULL; 3431789Sahrens char *poolname; 3432789Sahrens spa_t *spa; 3433789Sahrens uint64_t state; 34348680SLin.Ling@Sun.COM int error; 3435789Sahrens 3436789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3437789Sahrens return (NULL); 3438789Sahrens 3439789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3440789Sahrens return (NULL); 3441789Sahrens 34421635Sbonwick /* 34431635Sbonwick * Create and initialize the spa structure. 34441635Sbonwick */ 3445789Sahrens mutex_enter(&spa_namespace_lock); 344610921STim.Haley@Sun.COM spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 34478241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 3448789Sahrens 3449789Sahrens /* 34501635Sbonwick * Pass off the heavy lifting to spa_load(). 34511732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 34521732Sbonwick * is actually the one to trust when doing an import. 3453789Sahrens */ 345411422SMark.Musante@Sun.COM error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 3455789Sahrens 3456789Sahrens /* 3457789Sahrens * If 'tryconfig' was at least parsable, return the current config. 3458789Sahrens */ 3459789Sahrens if (spa->spa_root_vdev != NULL) { 3460789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3461789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3462789Sahrens poolname) == 0); 3463789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3464789Sahrens state) == 0); 34653975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 34663975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 34672082Seschrock 34682082Seschrock /* 34696423Sgw25295 * If the bootfs property exists on this pool then we 34706423Sgw25295 * copy it out so that external consumers can tell which 34716423Sgw25295 * pools are bootable. 34726423Sgw25295 */ 34738680SLin.Ling@Sun.COM if ((!error || error == EEXIST) && spa->spa_bootfs) { 34746423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 34756423Sgw25295 34766423Sgw25295 /* 34776423Sgw25295 * We have to play games with the name since the 34786423Sgw25295 * pool was opened as TRYIMPORT_NAME. 34796423Sgw25295 */ 34807754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 34816423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 34826423Sgw25295 char *cp; 34836423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 34846423Sgw25295 34856423Sgw25295 cp = strchr(tmpname, '/'); 34866423Sgw25295 if (cp == NULL) { 34876423Sgw25295 (void) strlcpy(dsname, tmpname, 34886423Sgw25295 MAXPATHLEN); 34896423Sgw25295 } else { 34906423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 34916423Sgw25295 "%s/%s", poolname, ++cp); 34926423Sgw25295 } 34936423Sgw25295 VERIFY(nvlist_add_string(config, 34946423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 34956423Sgw25295 kmem_free(dsname, MAXPATHLEN); 34966423Sgw25295 } 34976423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 34986423Sgw25295 } 34996423Sgw25295 35006423Sgw25295 /* 35015450Sbrendan * Add the list of hot spares and level 2 cache devices. 35022082Seschrock */ 35039425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 35042082Seschrock spa_add_spares(spa, config); 35055450Sbrendan spa_add_l2cache(spa, config); 35069425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 3507789Sahrens } 3508789Sahrens 3509789Sahrens spa_unload(spa); 3510789Sahrens spa_deactivate(spa); 3511789Sahrens spa_remove(spa); 3512789Sahrens mutex_exit(&spa_namespace_lock); 3513789Sahrens 3514789Sahrens return (config); 3515789Sahrens } 3516789Sahrens 3517789Sahrens /* 3518789Sahrens * Pool export/destroy 3519789Sahrens * 3520789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 3521789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 3522789Sahrens * update the pool state and sync all the labels to disk, removing the 35238211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 35248211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 3525789Sahrens */ 3526789Sahrens static int 35277214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 35288211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 3529789Sahrens { 3530789Sahrens spa_t *spa; 3531789Sahrens 35321775Sbillm if (oldconfig) 35331775Sbillm *oldconfig = NULL; 35341775Sbillm 35358241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 3536789Sahrens return (EROFS); 3537789Sahrens 3538789Sahrens mutex_enter(&spa_namespace_lock); 3539789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3540789Sahrens mutex_exit(&spa_namespace_lock); 3541789Sahrens return (ENOENT); 3542789Sahrens } 3543789Sahrens 3544789Sahrens /* 35451544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 35461544Seschrock * reacquire the namespace lock, and see if we can export. 35471544Seschrock */ 35481544Seschrock spa_open_ref(spa, FTAG); 35491544Seschrock mutex_exit(&spa_namespace_lock); 35501544Seschrock spa_async_suspend(spa); 35511544Seschrock mutex_enter(&spa_namespace_lock); 35521544Seschrock spa_close(spa, FTAG); 35531544Seschrock 35541544Seschrock /* 3555789Sahrens * The pool will be in core if it's openable, 3556789Sahrens * in which case we can modify its state. 3557789Sahrens */ 3558789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 3559789Sahrens /* 3560789Sahrens * Objsets may be open only because they're dirty, so we 3561789Sahrens * have to force it to sync before checking spa_refcnt. 3562789Sahrens */ 3563789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 3564789Sahrens 35651544Seschrock /* 35661544Seschrock * A pool cannot be exported or destroyed if there are active 35671544Seschrock * references. If we are resetting a pool, allow references by 35681544Seschrock * fault injection handlers. 35691544Seschrock */ 35701544Seschrock if (!spa_refcount_zero(spa) || 35711544Seschrock (spa->spa_inject_ref != 0 && 35721544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 35731544Seschrock spa_async_resume(spa); 3574789Sahrens mutex_exit(&spa_namespace_lock); 3575789Sahrens return (EBUSY); 3576789Sahrens } 3577789Sahrens 3578789Sahrens /* 35797214Slling * A pool cannot be exported if it has an active shared spare. 35807214Slling * This is to prevent other pools stealing the active spare 35817214Slling * from an exported pool. At user's own will, such pool can 35827214Slling * be forcedly exported. 35837214Slling */ 35847214Slling if (!force && new_state == POOL_STATE_EXPORTED && 35857214Slling spa_has_active_shared_spare(spa)) { 35867214Slling spa_async_resume(spa); 35877214Slling mutex_exit(&spa_namespace_lock); 35887214Slling return (EXDEV); 35897214Slling } 35907214Slling 35917214Slling /* 3592789Sahrens * We want this to be reflected on every label, 3593789Sahrens * so mark them all dirty. spa_unload() will do the 3594789Sahrens * final sync that pushes these changes out. 3595789Sahrens */ 35968211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 35977754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 35981544Seschrock spa->spa_state = new_state; 359912296SLin.Ling@Sun.COM spa->spa_final_txg = spa_last_synced_txg(spa) + 360012296SLin.Ling@Sun.COM TXG_DEFER_SIZE + 1; 36011544Seschrock vdev_config_dirty(spa->spa_root_vdev); 36027754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 36031544Seschrock } 3604789Sahrens } 3605789Sahrens 36064451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 36074451Seschrock 3608789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3609789Sahrens spa_unload(spa); 3610789Sahrens spa_deactivate(spa); 3611789Sahrens } 3612789Sahrens 36131775Sbillm if (oldconfig && spa->spa_config) 36141775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 36151775Sbillm 36161544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 36178211SGeorge.Wilson@Sun.COM if (!hardforce) 36188211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 36191544Seschrock spa_remove(spa); 36201544Seschrock } 3621789Sahrens mutex_exit(&spa_namespace_lock); 3622789Sahrens 3623789Sahrens return (0); 3624789Sahrens } 3625789Sahrens 3626789Sahrens /* 3627789Sahrens * Destroy a storage pool. 3628789Sahrens */ 3629789Sahrens int 3630789Sahrens spa_destroy(char *pool) 3631789Sahrens { 36328211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 36338211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 3634789Sahrens } 3635789Sahrens 3636789Sahrens /* 3637789Sahrens * Export a storage pool. 3638789Sahrens */ 3639789Sahrens int 36408211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 36418211SGeorge.Wilson@Sun.COM boolean_t hardforce) 3642789Sahrens { 36438211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 36448211SGeorge.Wilson@Sun.COM force, hardforce)); 3645789Sahrens } 3646789Sahrens 3647789Sahrens /* 36481544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 36491544Seschrock * from the namespace in any way. 36501544Seschrock */ 36511544Seschrock int 36521544Seschrock spa_reset(char *pool) 36531544Seschrock { 36547214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 36558211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 36561544Seschrock } 36571544Seschrock 36581544Seschrock /* 3659789Sahrens * ========================================================================== 3660789Sahrens * Device manipulation 3661789Sahrens * ========================================================================== 3662789Sahrens */ 3663789Sahrens 3664789Sahrens /* 36654527Sperrin * Add a device to a storage pool. 3666789Sahrens */ 3667789Sahrens int 3668789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 3669789Sahrens { 367010594SGeorge.Wilson@Sun.COM uint64_t txg, id; 36718241SJeff.Bonwick@Sun.COM int error; 3672789Sahrens vdev_t *rvd = spa->spa_root_vdev; 36731585Sbonwick vdev_t *vd, *tvd; 36745450Sbrendan nvlist_t **spares, **l2cache; 36755450Sbrendan uint_t nspares, nl2cache; 3676789Sahrens 3677789Sahrens txg = spa_vdev_enter(spa); 3678789Sahrens 36792082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 36802082Seschrock VDEV_ALLOC_ADD)) != 0) 36812082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 36822082Seschrock 36837754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 3684789Sahrens 36855450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 36865450Sbrendan &nspares) != 0) 36872082Seschrock nspares = 0; 36882082Seschrock 36895450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 36905450Sbrendan &nl2cache) != 0) 36915450Sbrendan nl2cache = 0; 36925450Sbrendan 36937754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 36942082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 36957754SJeff.Bonwick@Sun.COM 36967754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 36977754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 36987754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 36992082Seschrock 37003377Seschrock /* 37015450Sbrendan * We must validate the spares and l2cache devices after checking the 37025450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 37033377Seschrock */ 37047754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 37053377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 37063377Seschrock 37073377Seschrock /* 37083377Seschrock * Transfer each new top-level vdev from vd to rvd. 37093377Seschrock */ 37108241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 371110594SGeorge.Wilson@Sun.COM 371210594SGeorge.Wilson@Sun.COM /* 371310594SGeorge.Wilson@Sun.COM * Set the vdev id to the first hole, if one exists. 371410594SGeorge.Wilson@Sun.COM */ 371510594SGeorge.Wilson@Sun.COM for (id = 0; id < rvd->vdev_children; id++) { 371610594SGeorge.Wilson@Sun.COM if (rvd->vdev_child[id]->vdev_ishole) { 371710594SGeorge.Wilson@Sun.COM vdev_free(rvd->vdev_child[id]); 371810594SGeorge.Wilson@Sun.COM break; 371910594SGeorge.Wilson@Sun.COM } 372010594SGeorge.Wilson@Sun.COM } 37213377Seschrock tvd = vd->vdev_child[c]; 37223377Seschrock vdev_remove_child(vd, tvd); 372310594SGeorge.Wilson@Sun.COM tvd->vdev_id = id; 37243377Seschrock vdev_add_child(rvd, tvd); 37253377Seschrock vdev_config_dirty(tvd); 37263377Seschrock } 37273377Seschrock 37282082Seschrock if (nspares != 0) { 37295450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 37305450Sbrendan ZPOOL_CONFIG_SPARES); 37312082Seschrock spa_load_spares(spa); 37325450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 37335450Sbrendan } 37345450Sbrendan 37355450Sbrendan if (nl2cache != 0) { 37365450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 37375450Sbrendan ZPOOL_CONFIG_L2CACHE); 37385450Sbrendan spa_load_l2cache(spa); 37395450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3740789Sahrens } 3741789Sahrens 3742789Sahrens /* 37431585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 37441585Sbonwick * If other threads start allocating from these vdevs before we 37451585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 37461585Sbonwick * fail to open the pool because there are DVAs that the config cache 37471585Sbonwick * can't translate. Therefore, we first add the vdevs without 37481585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 37491635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 37501585Sbonwick * 37511585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 37521585Sbonwick * if we lose power at any point in this sequence, the remaining 37531585Sbonwick * steps will be completed the next time we load the pool. 3754789Sahrens */ 37551635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 37561585Sbonwick 37571635Sbonwick mutex_enter(&spa_namespace_lock); 37581635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 37591635Sbonwick mutex_exit(&spa_namespace_lock); 3760789Sahrens 37611635Sbonwick return (0); 3762789Sahrens } 3763789Sahrens 3764789Sahrens /* 3765789Sahrens * Attach a device to a mirror. The arguments are the path to any device 3766789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 3767789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 3768789Sahrens * 3769789Sahrens * If 'replacing' is specified, the new device is intended to replace the 3770789Sahrens * existing device; in this case the two devices are made into their own 37714451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 3772789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 3773789Sahrens * extra rules: you can't attach to it after it's been created, and upon 3774789Sahrens * completion of resilvering, the first disk (the one being replaced) 3775789Sahrens * is automatically detached. 3776789Sahrens */ 3777789Sahrens int 37781544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3779789Sahrens { 378012296SLin.Ling@Sun.COM uint64_t txg, dtl_max_txg; 3781789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3782789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 37832082Seschrock vdev_ops_t *pvops; 37847313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 37857313SEric.Kustarz@Sun.COM int newvd_isspare; 37867313SEric.Kustarz@Sun.COM int error; 3787789Sahrens 3788789Sahrens txg = spa_vdev_enter(spa); 3789789Sahrens 37906643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3791789Sahrens 3792789Sahrens if (oldvd == NULL) 3793789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3794789Sahrens 37951585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 37961585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 37971585Sbonwick 3798789Sahrens pvd = oldvd->vdev_parent; 3799789Sahrens 38002082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 38014451Seschrock VDEV_ALLOC_ADD)) != 0) 38024451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 38034451Seschrock 38044451Seschrock if (newrootvd->vdev_children != 1) 3805789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3806789Sahrens 3807789Sahrens newvd = newrootvd->vdev_child[0]; 3808789Sahrens 3809789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3810789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3811789Sahrens 38122082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3813789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3814789Sahrens 38154527Sperrin /* 38164527Sperrin * Spares can't replace logs 38174527Sperrin */ 38187326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 38194527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38204527Sperrin 38212082Seschrock if (!replacing) { 38222082Seschrock /* 38232082Seschrock * For attach, the only allowable parent is a mirror or the root 38242082Seschrock * vdev. 38252082Seschrock */ 38262082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 38272082Seschrock pvd->vdev_ops != &vdev_root_ops) 38282082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38292082Seschrock 38302082Seschrock pvops = &vdev_mirror_ops; 38312082Seschrock } else { 38322082Seschrock /* 38332082Seschrock * Active hot spares can only be replaced by inactive hot 38342082Seschrock * spares. 38352082Seschrock */ 38362082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 38372082Seschrock pvd->vdev_child[1] == oldvd && 38382082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 38392082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38402082Seschrock 38412082Seschrock /* 38422082Seschrock * If the source is a hot spare, and the parent isn't already a 38432082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 38443377Seschrock * want to create a replacing vdev. The user is not allowed to 38453377Seschrock * attach to a spared vdev child unless the 'isspare' state is 38463377Seschrock * the same (spare replaces spare, non-spare replaces 38473377Seschrock * non-spare). 38482082Seschrock */ 38492082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) 38502082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38513377Seschrock else if (pvd->vdev_ops == &vdev_spare_ops && 38523377Seschrock newvd->vdev_isspare != oldvd->vdev_isspare) 38533377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38542082Seschrock else if (pvd->vdev_ops != &vdev_spare_ops && 38552082Seschrock newvd->vdev_isspare) 38562082Seschrock pvops = &vdev_spare_ops; 38572082Seschrock else 38582082Seschrock pvops = &vdev_replacing_ops; 38592082Seschrock } 38602082Seschrock 38611175Slling /* 38629816SGeorge.Wilson@Sun.COM * Make sure the new device is big enough. 38631175Slling */ 38649816SGeorge.Wilson@Sun.COM if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3865789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3866789Sahrens 38671732Sbonwick /* 38681732Sbonwick * The new device cannot have a higher alignment requirement 38691732Sbonwick * than the top-level vdev. 38701732Sbonwick */ 38711732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3872789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3873789Sahrens 3874789Sahrens /* 3875789Sahrens * If this is an in-place replacement, update oldvd's path and devid 3876789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3877789Sahrens */ 3878789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3879789Sahrens spa_strfree(oldvd->vdev_path); 3880789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3881789Sahrens KM_SLEEP); 3882789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3883789Sahrens newvd->vdev_path, "old"); 3884789Sahrens if (oldvd->vdev_devid != NULL) { 3885789Sahrens spa_strfree(oldvd->vdev_devid); 3886789Sahrens oldvd->vdev_devid = NULL; 3887789Sahrens } 3888789Sahrens } 3889789Sahrens 3890789Sahrens /* 38912082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 38922082Seschrock * mirror/replacing/spare vdev above oldvd. 3893789Sahrens */ 3894789Sahrens if (pvd->vdev_ops != pvops) 3895789Sahrens pvd = vdev_add_parent(oldvd, pvops); 3896789Sahrens 3897789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3898789Sahrens ASSERT(pvd->vdev_ops == pvops); 3899789Sahrens ASSERT(oldvd->vdev_parent == pvd); 3900789Sahrens 3901789Sahrens /* 3902789Sahrens * Extract the new device from its root and add it to pvd. 3903789Sahrens */ 3904789Sahrens vdev_remove_child(newrootvd, newvd); 3905789Sahrens newvd->vdev_id = pvd->vdev_children; 390610594SGeorge.Wilson@Sun.COM newvd->vdev_crtxg = oldvd->vdev_crtxg; 3907789Sahrens vdev_add_child(pvd, newvd); 3908789Sahrens 3909789Sahrens tvd = newvd->vdev_top; 3910789Sahrens ASSERT(pvd->vdev_top == tvd); 3911789Sahrens ASSERT(tvd->vdev_parent == rvd); 3912789Sahrens 3913789Sahrens vdev_config_dirty(tvd); 3914789Sahrens 3915789Sahrens /* 391612296SLin.Ling@Sun.COM * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 391712296SLin.Ling@Sun.COM * for any dmu_sync-ed blocks. It will propagate upward when 391812296SLin.Ling@Sun.COM * spa_vdev_exit() calls vdev_dtl_reassess(). 3919789Sahrens */ 392012296SLin.Ling@Sun.COM dtl_max_txg = txg + TXG_CONCURRENT_STATES; 392112296SLin.Ling@Sun.COM 392212296SLin.Ling@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 392312296SLin.Ling@Sun.COM dtl_max_txg - TXG_INITIAL); 3924789Sahrens 39259425SEric.Schrock@Sun.COM if (newvd->vdev_isspare) { 39263377Seschrock spa_spare_activate(newvd); 39279425SEric.Schrock@Sun.COM spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 39289425SEric.Schrock@Sun.COM } 39299425SEric.Schrock@Sun.COM 39307754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 39317754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 39327313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 39331544Seschrock 3934789Sahrens /* 3935789Sahrens * Mark newvd's DTL dirty in this txg. 3936789Sahrens */ 39371732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3938789Sahrens 393912296SLin.Ling@Sun.COM /* 394012296SLin.Ling@Sun.COM * Restart the resilver 394112296SLin.Ling@Sun.COM */ 394212296SLin.Ling@Sun.COM dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 394312296SLin.Ling@Sun.COM 394412296SLin.Ling@Sun.COM /* 394512296SLin.Ling@Sun.COM * Commit the config 394612296SLin.Ling@Sun.COM */ 394712296SLin.Ling@Sun.COM (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 394812296SLin.Ling@Sun.COM 394912296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL, 395012296SLin.Ling@Sun.COM "%s vdev=%s %s vdev=%s", 39519946SMark.Musante@Sun.COM replacing && newvd_isspare ? "spare in" : 39529946SMark.Musante@Sun.COM replacing ? "replace" : "attach", newvdpath, 39539946SMark.Musante@Sun.COM replacing ? "for" : "to", oldvdpath); 39547313SEric.Kustarz@Sun.COM 39557313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 39567313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 39577313SEric.Kustarz@Sun.COM 395812865Slori.alt@oracle.com if (spa->spa_bootfs) 395912865Slori.alt@oracle.com spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 396012865Slori.alt@oracle.com 3961789Sahrens return (0); 3962789Sahrens } 3963789Sahrens 3964789Sahrens /* 3965789Sahrens * Detach a device from a mirror or replacing vdev. 3966789Sahrens * If 'replace_done' is specified, only detach if the parent 3967789Sahrens * is a replacing vdev. 3968789Sahrens */ 3969789Sahrens int 39708241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3971789Sahrens { 3972789Sahrens uint64_t txg; 39738241SJeff.Bonwick@Sun.COM int error; 3974789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3975789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 39762082Seschrock boolean_t unspare = B_FALSE; 39772082Seschrock uint64_t unspare_guid; 39786673Seschrock size_t len; 397911422SMark.Musante@Sun.COM char *vdpath; 3980789Sahrens 3981789Sahrens txg = spa_vdev_enter(spa); 3982789Sahrens 39836643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3984789Sahrens 3985789Sahrens if (vd == NULL) 3986789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3987789Sahrens 39881585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 39891585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 39901585Sbonwick 3991789Sahrens pvd = vd->vdev_parent; 3992789Sahrens 3993789Sahrens /* 39948241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 39958241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 39968241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 39978241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 39988241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 39998241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 40008241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 40018241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 40028241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 40038241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 40048241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 40058241SJeff.Bonwick@Sun.COM */ 40068241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 40078241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 40088241SJeff.Bonwick@Sun.COM 40098241SJeff.Bonwick@Sun.COM /* 4010789Sahrens * If replace_done is specified, only remove this device if it's 40112082Seschrock * the first child of a replacing vdev. For the 'spare' vdev, either 40122082Seschrock * disk can be removed. 4013789Sahrens */ 40142082Seschrock if (replace_done) { 40152082Seschrock if (pvd->vdev_ops == &vdev_replacing_ops) { 40162082Seschrock if (vd->vdev_id != 0) 40172082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 40182082Seschrock } else if (pvd->vdev_ops != &vdev_spare_ops) { 40192082Seschrock return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 40202082Seschrock } 40212082Seschrock } 40222082Seschrock 40232082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 40244577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4025789Sahrens 4026789Sahrens /* 40272082Seschrock * Only mirror, replacing, and spare vdevs support detach. 4028789Sahrens */ 4029789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 40302082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 40312082Seschrock pvd->vdev_ops != &vdev_spare_ops) 4032789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4033789Sahrens 4034789Sahrens /* 40358241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 40368241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 4037789Sahrens */ 40388241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 4039789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4040789Sahrens 40418241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 40428241SJeff.Bonwick@Sun.COM 4043789Sahrens /* 40446673Seschrock * If we are detaching the second disk from a replacing vdev, then 40456673Seschrock * check to see if we changed the original vdev's path to have "/old" 40466673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 40476673Seschrock */ 40486673Seschrock if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 40496673Seschrock pvd->vdev_child[0]->vdev_path != NULL && 40506673Seschrock pvd->vdev_child[1]->vdev_path != NULL) { 40516673Seschrock ASSERT(pvd->vdev_child[1] == vd); 40526673Seschrock cvd = pvd->vdev_child[0]; 40536673Seschrock len = strlen(vd->vdev_path); 40546673Seschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 40556673Seschrock strcmp(cvd->vdev_path + len, "/old") == 0) { 40566673Seschrock spa_strfree(cvd->vdev_path); 40576673Seschrock cvd->vdev_path = spa_strdup(vd->vdev_path); 40586673Seschrock } 40596673Seschrock } 40606673Seschrock 40616673Seschrock /* 40622082Seschrock * If we are detaching the original disk from a spare, then it implies 40632082Seschrock * that the spare should become a real disk, and be removed from the 40642082Seschrock * active spare list for the pool. 40652082Seschrock */ 40662082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 40678241SJeff.Bonwick@Sun.COM vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 40682082Seschrock unspare = B_TRUE; 40692082Seschrock 40702082Seschrock /* 4071789Sahrens * Erase the disk labels so the disk can be used for other things. 4072789Sahrens * This must be done after all other error cases are handled, 4073789Sahrens * but before we disembowel vd (so we can still do I/O to it). 4074789Sahrens * But if we can't do it, don't treat the error as fatal -- 4075789Sahrens * it may be that the unwritability of the disk is the reason 4076789Sahrens * it's being detached! 4077789Sahrens */ 40783377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4079789Sahrens 4080789Sahrens /* 4081789Sahrens * Remove vd from its parent and compact the parent's children. 4082789Sahrens */ 4083789Sahrens vdev_remove_child(pvd, vd); 4084789Sahrens vdev_compact_children(pvd); 4085789Sahrens 4086789Sahrens /* 4087789Sahrens * Remember one of the remaining children so we can get tvd below. 4088789Sahrens */ 4089789Sahrens cvd = pvd->vdev_child[0]; 4090789Sahrens 4091789Sahrens /* 40922082Seschrock * If we need to remove the remaining child from the list of hot spares, 40938241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 40948241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 40958241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 40968241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 40978241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 40988241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 40992082Seschrock */ 41002082Seschrock if (unspare) { 41012082Seschrock ASSERT(cvd->vdev_isspare); 41023377Seschrock spa_spare_remove(cvd); 41032082Seschrock unspare_guid = cvd->vdev_guid; 41048241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 41052082Seschrock } 41062082Seschrock 41072082Seschrock /* 4108789Sahrens * If the parent mirror/replacing vdev only has one child, 4109789Sahrens * the parent is no longer needed. Remove it from the tree. 4110789Sahrens */ 4111789Sahrens if (pvd->vdev_children == 1) 4112789Sahrens vdev_remove_parent(cvd); 4113789Sahrens 4114789Sahrens /* 4115789Sahrens * We don't set tvd until now because the parent we just removed 4116789Sahrens * may have been the previous top-level vdev. 4117789Sahrens */ 4118789Sahrens tvd = cvd->vdev_top; 4119789Sahrens ASSERT(tvd->vdev_parent == rvd); 4120789Sahrens 4121789Sahrens /* 41223377Seschrock * Reevaluate the parent vdev state. 4123789Sahrens */ 41244451Seschrock vdev_propagate_state(cvd); 4125789Sahrens 4126789Sahrens /* 41279816SGeorge.Wilson@Sun.COM * If the 'autoexpand' property is set on the pool then automatically 41289816SGeorge.Wilson@Sun.COM * try to expand the size of the pool. For example if the device we 41299816SGeorge.Wilson@Sun.COM * just detached was smaller than the others, it may be possible to 41309816SGeorge.Wilson@Sun.COM * add metaslabs (i.e. grow the pool). We need to reopen the vdev 41319816SGeorge.Wilson@Sun.COM * first so that we can obtain the updated sizes of the leaf vdevs. 4132789Sahrens */ 41339816SGeorge.Wilson@Sun.COM if (spa->spa_autoexpand) { 41349816SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 41359816SGeorge.Wilson@Sun.COM vdev_expand(tvd, txg); 41369816SGeorge.Wilson@Sun.COM } 4137789Sahrens 4138789Sahrens vdev_config_dirty(tvd); 4139789Sahrens 4140789Sahrens /* 41413377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 41423377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 41433377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 41443377Seschrock * prevent vd from being accessed after it's freed. 4145789Sahrens */ 414611422SMark.Musante@Sun.COM vdpath = spa_strdup(vd->vdev_path); 41478241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 4148789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 41491732Sbonwick vd->vdev_detached = B_TRUE; 41501732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4151789Sahrens 41524451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 41534451Seschrock 41542082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 41552082Seschrock 415612296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL, 415711422SMark.Musante@Sun.COM "vdev=%s", vdpath); 415811422SMark.Musante@Sun.COM spa_strfree(vdpath); 415911422SMark.Musante@Sun.COM 41602082Seschrock /* 41613377Seschrock * If this was the removal of the original device in a hot spare vdev, 41623377Seschrock * then we want to go through and remove the device from the hot spare 41633377Seschrock * list of every other pool. 41642082Seschrock */ 41652082Seschrock if (unspare) { 41668241SJeff.Bonwick@Sun.COM spa_t *myspa = spa; 41672082Seschrock spa = NULL; 41682082Seschrock mutex_enter(&spa_namespace_lock); 41692082Seschrock while ((spa = spa_next(spa)) != NULL) { 41702082Seschrock if (spa->spa_state != POOL_STATE_ACTIVE) 41712082Seschrock continue; 41728241SJeff.Bonwick@Sun.COM if (spa == myspa) 41738241SJeff.Bonwick@Sun.COM continue; 41747793SJeff.Bonwick@Sun.COM spa_open_ref(spa, FTAG); 41757793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 417612296SLin.Ling@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, 417712296SLin.Ling@Sun.COM B_TRUE); 41787793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 41797793SJeff.Bonwick@Sun.COM spa_close(spa, FTAG); 41802082Seschrock } 41812082Seschrock mutex_exit(&spa_namespace_lock); 41822082Seschrock } 41832082Seschrock 41842082Seschrock return (error); 41852082Seschrock } 41862082Seschrock 418711422SMark.Musante@Sun.COM /* 418811422SMark.Musante@Sun.COM * Split a set of devices from their mirrors, and create a new pool from them. 418911422SMark.Musante@Sun.COM */ 419011422SMark.Musante@Sun.COM int 419111422SMark.Musante@Sun.COM spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 419211422SMark.Musante@Sun.COM nvlist_t *props, boolean_t exp) 419311422SMark.Musante@Sun.COM { 419411422SMark.Musante@Sun.COM int error = 0; 419511422SMark.Musante@Sun.COM uint64_t txg, *glist; 419611422SMark.Musante@Sun.COM spa_t *newspa; 419711422SMark.Musante@Sun.COM uint_t c, children, lastlog; 419811422SMark.Musante@Sun.COM nvlist_t **child, *nvl, *tmp; 419911422SMark.Musante@Sun.COM dmu_tx_t *tx; 420011422SMark.Musante@Sun.COM char *altroot = NULL; 420111422SMark.Musante@Sun.COM vdev_t *rvd, **vml = NULL; /* vdev modify list */ 420211422SMark.Musante@Sun.COM boolean_t activate_slog; 420311422SMark.Musante@Sun.COM 420411422SMark.Musante@Sun.COM if (!spa_writeable(spa)) 420511422SMark.Musante@Sun.COM return (EROFS); 420611422SMark.Musante@Sun.COM 420711422SMark.Musante@Sun.COM txg = spa_vdev_enter(spa); 420811422SMark.Musante@Sun.COM 420911422SMark.Musante@Sun.COM /* clear the log and flush everything up to now */ 421011422SMark.Musante@Sun.COM activate_slog = spa_passivate_log(spa); 421111422SMark.Musante@Sun.COM (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 421211422SMark.Musante@Sun.COM error = spa_offline_log(spa); 421311422SMark.Musante@Sun.COM txg = spa_vdev_config_enter(spa); 421411422SMark.Musante@Sun.COM 421511422SMark.Musante@Sun.COM if (activate_slog) 421611422SMark.Musante@Sun.COM spa_activate_log(spa); 421711422SMark.Musante@Sun.COM 421811422SMark.Musante@Sun.COM if (error != 0) 421911422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 422011422SMark.Musante@Sun.COM 422111422SMark.Musante@Sun.COM /* check new spa name before going any further */ 422211422SMark.Musante@Sun.COM if (spa_lookup(newname) != NULL) 422311422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 422411422SMark.Musante@Sun.COM 422511422SMark.Musante@Sun.COM /* 422611422SMark.Musante@Sun.COM * scan through all the children to ensure they're all mirrors 422711422SMark.Musante@Sun.COM */ 422811422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 422911422SMark.Musante@Sun.COM nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 423011422SMark.Musante@Sun.COM &children) != 0) 423111422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 423211422SMark.Musante@Sun.COM 423311422SMark.Musante@Sun.COM /* first, check to ensure we've got the right child count */ 423411422SMark.Musante@Sun.COM rvd = spa->spa_root_vdev; 423511422SMark.Musante@Sun.COM lastlog = 0; 423611422SMark.Musante@Sun.COM for (c = 0; c < rvd->vdev_children; c++) { 423711422SMark.Musante@Sun.COM vdev_t *vd = rvd->vdev_child[c]; 423811422SMark.Musante@Sun.COM 423911422SMark.Musante@Sun.COM /* don't count the holes & logs as children */ 424011422SMark.Musante@Sun.COM if (vd->vdev_islog || vd->vdev_ishole) { 424111422SMark.Musante@Sun.COM if (lastlog == 0) 424211422SMark.Musante@Sun.COM lastlog = c; 424311422SMark.Musante@Sun.COM continue; 424411422SMark.Musante@Sun.COM } 424511422SMark.Musante@Sun.COM 424611422SMark.Musante@Sun.COM lastlog = 0; 424711422SMark.Musante@Sun.COM } 424811422SMark.Musante@Sun.COM if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 424911422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 425011422SMark.Musante@Sun.COM 425111422SMark.Musante@Sun.COM /* next, ensure no spare or cache devices are part of the split */ 425211422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 425311422SMark.Musante@Sun.COM nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 425411422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 425511422SMark.Musante@Sun.COM 425611422SMark.Musante@Sun.COM vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 425711422SMark.Musante@Sun.COM glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 425811422SMark.Musante@Sun.COM 425911422SMark.Musante@Sun.COM /* then, loop over each vdev and validate it */ 426011422SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 426111422SMark.Musante@Sun.COM uint64_t is_hole = 0; 426211422SMark.Musante@Sun.COM 426311422SMark.Musante@Sun.COM (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 426411422SMark.Musante@Sun.COM &is_hole); 426511422SMark.Musante@Sun.COM 426611422SMark.Musante@Sun.COM if (is_hole != 0) { 426711422SMark.Musante@Sun.COM if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 426811422SMark.Musante@Sun.COM spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 426911422SMark.Musante@Sun.COM continue; 427011422SMark.Musante@Sun.COM } else { 427111422SMark.Musante@Sun.COM error = EINVAL; 427211422SMark.Musante@Sun.COM break; 427311422SMark.Musante@Sun.COM } 427411422SMark.Musante@Sun.COM } 427511422SMark.Musante@Sun.COM 427611422SMark.Musante@Sun.COM /* which disk is going to be split? */ 427711422SMark.Musante@Sun.COM if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 427811422SMark.Musante@Sun.COM &glist[c]) != 0) { 427911422SMark.Musante@Sun.COM error = EINVAL; 428011422SMark.Musante@Sun.COM break; 428111422SMark.Musante@Sun.COM } 428211422SMark.Musante@Sun.COM 428311422SMark.Musante@Sun.COM /* look it up in the spa */ 428411422SMark.Musante@Sun.COM vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 428511422SMark.Musante@Sun.COM if (vml[c] == NULL) { 428611422SMark.Musante@Sun.COM error = ENODEV; 428711422SMark.Musante@Sun.COM break; 428811422SMark.Musante@Sun.COM } 428911422SMark.Musante@Sun.COM 429011422SMark.Musante@Sun.COM /* make sure there's nothing stopping the split */ 429111422SMark.Musante@Sun.COM if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 429211422SMark.Musante@Sun.COM vml[c]->vdev_islog || 429311422SMark.Musante@Sun.COM vml[c]->vdev_ishole || 429411422SMark.Musante@Sun.COM vml[c]->vdev_isspare || 429511422SMark.Musante@Sun.COM vml[c]->vdev_isl2cache || 429611422SMark.Musante@Sun.COM !vdev_writeable(vml[c]) || 429711497SMark.Musante@Sun.COM vml[c]->vdev_children != 0 || 429811422SMark.Musante@Sun.COM vml[c]->vdev_state != VDEV_STATE_HEALTHY || 429911422SMark.Musante@Sun.COM c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 430011422SMark.Musante@Sun.COM error = EINVAL; 430111422SMark.Musante@Sun.COM break; 430211422SMark.Musante@Sun.COM } 430311422SMark.Musante@Sun.COM 430411422SMark.Musante@Sun.COM if (vdev_dtl_required(vml[c])) { 430511422SMark.Musante@Sun.COM error = EBUSY; 430611422SMark.Musante@Sun.COM break; 430711422SMark.Musante@Sun.COM } 430811422SMark.Musante@Sun.COM 430911422SMark.Musante@Sun.COM /* we need certain info from the top level */ 431011422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 431111422SMark.Musante@Sun.COM vml[c]->vdev_top->vdev_ms_array) == 0); 431211422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 431311422SMark.Musante@Sun.COM vml[c]->vdev_top->vdev_ms_shift) == 0); 431411422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 431511422SMark.Musante@Sun.COM vml[c]->vdev_top->vdev_asize) == 0); 431611422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 431711422SMark.Musante@Sun.COM vml[c]->vdev_top->vdev_ashift) == 0); 431811422SMark.Musante@Sun.COM } 431911422SMark.Musante@Sun.COM 432011422SMark.Musante@Sun.COM if (error != 0) { 432111422SMark.Musante@Sun.COM kmem_free(vml, children * sizeof (vdev_t *)); 432211422SMark.Musante@Sun.COM kmem_free(glist, children * sizeof (uint64_t)); 432311422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 432411422SMark.Musante@Sun.COM } 432511422SMark.Musante@Sun.COM 432611422SMark.Musante@Sun.COM /* stop writers from using the disks */ 432711422SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 432811422SMark.Musante@Sun.COM if (vml[c] != NULL) 432911422SMark.Musante@Sun.COM vml[c]->vdev_offline = B_TRUE; 433011422SMark.Musante@Sun.COM } 433111422SMark.Musante@Sun.COM vdev_reopen(spa->spa_root_vdev); 433211422SMark.Musante@Sun.COM 433311422SMark.Musante@Sun.COM /* 433411422SMark.Musante@Sun.COM * Temporarily record the splitting vdevs in the spa config. This 433511422SMark.Musante@Sun.COM * will disappear once the config is regenerated. 433611422SMark.Musante@Sun.COM */ 433711422SMark.Musante@Sun.COM VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 433811422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 433911422SMark.Musante@Sun.COM glist, children) == 0); 434011422SMark.Musante@Sun.COM kmem_free(glist, children * sizeof (uint64_t)); 434111422SMark.Musante@Sun.COM 434211864SMark.Musante@Sun.COM mutex_enter(&spa->spa_props_lock); 434311422SMark.Musante@Sun.COM VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 434411422SMark.Musante@Sun.COM nvl) == 0); 434511864SMark.Musante@Sun.COM mutex_exit(&spa->spa_props_lock); 434611422SMark.Musante@Sun.COM spa->spa_config_splitting = nvl; 434711422SMark.Musante@Sun.COM vdev_config_dirty(spa->spa_root_vdev); 434811422SMark.Musante@Sun.COM 434911422SMark.Musante@Sun.COM /* configure and create the new pool */ 435011422SMark.Musante@Sun.COM VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 435111422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 435211422SMark.Musante@Sun.COM exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 435311422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 435411422SMark.Musante@Sun.COM spa_version(spa)) == 0); 435511422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 435611422SMark.Musante@Sun.COM spa->spa_config_txg) == 0); 435711422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 435811422SMark.Musante@Sun.COM spa_generate_guid(NULL)) == 0); 435911422SMark.Musante@Sun.COM (void) nvlist_lookup_string(props, 436011422SMark.Musante@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 436111422SMark.Musante@Sun.COM 436211497SMark.Musante@Sun.COM /* add the new pool to the namespace */ 436311422SMark.Musante@Sun.COM newspa = spa_add(newname, config, altroot); 436411422SMark.Musante@Sun.COM newspa->spa_config_txg = spa->spa_config_txg; 436511422SMark.Musante@Sun.COM spa_set_log_state(newspa, SPA_LOG_CLEAR); 436611422SMark.Musante@Sun.COM 436711422SMark.Musante@Sun.COM /* release the spa config lock, retaining the namespace lock */ 436811422SMark.Musante@Sun.COM spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 436911422SMark.Musante@Sun.COM 437011422SMark.Musante@Sun.COM if (zio_injection_enabled) 437111422SMark.Musante@Sun.COM zio_handle_panic_injection(spa, FTAG, 1); 437211422SMark.Musante@Sun.COM 437311422SMark.Musante@Sun.COM spa_activate(newspa, spa_mode_global); 437411422SMark.Musante@Sun.COM spa_async_suspend(newspa); 437511422SMark.Musante@Sun.COM 437611422SMark.Musante@Sun.COM /* create the new pool from the disks of the original pool */ 437711422SMark.Musante@Sun.COM error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 437811422SMark.Musante@Sun.COM if (error) 437911422SMark.Musante@Sun.COM goto out; 438011422SMark.Musante@Sun.COM 438111422SMark.Musante@Sun.COM /* if that worked, generate a real config for the new pool */ 438211422SMark.Musante@Sun.COM if (newspa->spa_root_vdev != NULL) { 438311422SMark.Musante@Sun.COM VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 438411422SMark.Musante@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 438511422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 438611422SMark.Musante@Sun.COM ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 438711422SMark.Musante@Sun.COM spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 438811422SMark.Musante@Sun.COM B_TRUE)); 438911422SMark.Musante@Sun.COM } 439011422SMark.Musante@Sun.COM 439111422SMark.Musante@Sun.COM /* set the props */ 439211422SMark.Musante@Sun.COM if (props != NULL) { 439311422SMark.Musante@Sun.COM spa_configfile_set(newspa, props, B_FALSE); 439411422SMark.Musante@Sun.COM error = spa_prop_set(newspa, props); 439511422SMark.Musante@Sun.COM if (error) 439611422SMark.Musante@Sun.COM goto out; 439711422SMark.Musante@Sun.COM } 439811422SMark.Musante@Sun.COM 439911422SMark.Musante@Sun.COM /* flush everything */ 440011422SMark.Musante@Sun.COM txg = spa_vdev_config_enter(newspa); 440111422SMark.Musante@Sun.COM vdev_config_dirty(newspa->spa_root_vdev); 440211422SMark.Musante@Sun.COM (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 440311422SMark.Musante@Sun.COM 440411422SMark.Musante@Sun.COM if (zio_injection_enabled) 440511422SMark.Musante@Sun.COM zio_handle_panic_injection(spa, FTAG, 2); 440611422SMark.Musante@Sun.COM 440711422SMark.Musante@Sun.COM spa_async_resume(newspa); 440811422SMark.Musante@Sun.COM 440911422SMark.Musante@Sun.COM /* finally, update the original pool's config */ 441011422SMark.Musante@Sun.COM txg = spa_vdev_config_enter(spa); 441111422SMark.Musante@Sun.COM tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 441211422SMark.Musante@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 441311422SMark.Musante@Sun.COM if (error != 0) 441411422SMark.Musante@Sun.COM dmu_tx_abort(tx); 441511422SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 441611422SMark.Musante@Sun.COM if (vml[c] != NULL) { 441711422SMark.Musante@Sun.COM vdev_split(vml[c]); 441811422SMark.Musante@Sun.COM if (error == 0) 441912296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_VDEV_DETACH, 442012296SLin.Ling@Sun.COM spa, tx, "vdev=%s", 442111422SMark.Musante@Sun.COM vml[c]->vdev_path); 442211422SMark.Musante@Sun.COM vdev_free(vml[c]); 442311422SMark.Musante@Sun.COM } 442411422SMark.Musante@Sun.COM } 442511422SMark.Musante@Sun.COM vdev_config_dirty(spa->spa_root_vdev); 442611422SMark.Musante@Sun.COM spa->spa_config_splitting = NULL; 442711422SMark.Musante@Sun.COM nvlist_free(nvl); 442811422SMark.Musante@Sun.COM if (error == 0) 442911422SMark.Musante@Sun.COM dmu_tx_commit(tx); 443011422SMark.Musante@Sun.COM (void) spa_vdev_exit(spa, NULL, txg, 0); 443111422SMark.Musante@Sun.COM 443211422SMark.Musante@Sun.COM if (zio_injection_enabled) 443311422SMark.Musante@Sun.COM zio_handle_panic_injection(spa, FTAG, 3); 443411422SMark.Musante@Sun.COM 443511422SMark.Musante@Sun.COM /* split is complete; log a history record */ 443612296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL, 443711422SMark.Musante@Sun.COM "split new pool %s from pool %s", newname, spa_name(spa)); 443811422SMark.Musante@Sun.COM 443911422SMark.Musante@Sun.COM kmem_free(vml, children * sizeof (vdev_t *)); 444011422SMark.Musante@Sun.COM 444111422SMark.Musante@Sun.COM /* if we're not going to mount the filesystems in userland, export */ 444211422SMark.Musante@Sun.COM if (exp) 444311422SMark.Musante@Sun.COM error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 444411422SMark.Musante@Sun.COM B_FALSE, B_FALSE); 444511422SMark.Musante@Sun.COM 444611422SMark.Musante@Sun.COM return (error); 444711422SMark.Musante@Sun.COM 444811422SMark.Musante@Sun.COM out: 444911422SMark.Musante@Sun.COM spa_unload(newspa); 445011422SMark.Musante@Sun.COM spa_deactivate(newspa); 445111422SMark.Musante@Sun.COM spa_remove(newspa); 445211422SMark.Musante@Sun.COM 445311422SMark.Musante@Sun.COM txg = spa_vdev_config_enter(spa); 445411864SMark.Musante@Sun.COM 445511864SMark.Musante@Sun.COM /* re-online all offlined disks */ 445611864SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 445711864SMark.Musante@Sun.COM if (vml[c] != NULL) 445811864SMark.Musante@Sun.COM vml[c]->vdev_offline = B_FALSE; 445911864SMark.Musante@Sun.COM } 446011864SMark.Musante@Sun.COM vdev_reopen(spa->spa_root_vdev); 446111864SMark.Musante@Sun.COM 446211422SMark.Musante@Sun.COM nvlist_free(spa->spa_config_splitting); 446311422SMark.Musante@Sun.COM spa->spa_config_splitting = NULL; 446411497SMark.Musante@Sun.COM (void) spa_vdev_exit(spa, NULL, txg, error); 446511422SMark.Musante@Sun.COM 446611422SMark.Musante@Sun.COM kmem_free(vml, children * sizeof (vdev_t *)); 446711422SMark.Musante@Sun.COM return (error); 446811422SMark.Musante@Sun.COM } 446911422SMark.Musante@Sun.COM 44707754SJeff.Bonwick@Sun.COM static nvlist_t * 44717754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 44722082Seschrock { 44737754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 44747754SJeff.Bonwick@Sun.COM uint64_t guid; 44757754SJeff.Bonwick@Sun.COM 44767754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 44777754SJeff.Bonwick@Sun.COM &guid) == 0); 44787754SJeff.Bonwick@Sun.COM 44797754SJeff.Bonwick@Sun.COM if (guid == target_guid) 44807754SJeff.Bonwick@Sun.COM return (nvpp[i]); 44812082Seschrock } 44822082Seschrock 44837754SJeff.Bonwick@Sun.COM return (NULL); 44845450Sbrendan } 44855450Sbrendan 44867754SJeff.Bonwick@Sun.COM static void 44877754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 44887754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 44895450Sbrendan { 44907754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 44917754SJeff.Bonwick@Sun.COM 44927754SJeff.Bonwick@Sun.COM if (count > 1) 44937754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 44947754SJeff.Bonwick@Sun.COM 44957754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 44967754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 44977754SJeff.Bonwick@Sun.COM continue; 44987754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 44995450Sbrendan } 45005450Sbrendan 45017754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 45027754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 45037754SJeff.Bonwick@Sun.COM 45047754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 45057754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 45067754SJeff.Bonwick@Sun.COM 45077754SJeff.Bonwick@Sun.COM if (count > 1) 45087754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 45095450Sbrendan } 45105450Sbrendan 45115450Sbrendan /* 451210594SGeorge.Wilson@Sun.COM * Evacuate the device. 451310594SGeorge.Wilson@Sun.COM */ 451412296SLin.Ling@Sun.COM static int 451510594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 451610594SGeorge.Wilson@Sun.COM { 451712296SLin.Ling@Sun.COM uint64_t txg; 451810974SJeff.Bonwick@Sun.COM int error = 0; 451910594SGeorge.Wilson@Sun.COM 452010594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 452110594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 452210922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 452310594SGeorge.Wilson@Sun.COM 452410594SGeorge.Wilson@Sun.COM /* 452510594SGeorge.Wilson@Sun.COM * Evacuate the device. We don't hold the config lock as writer 452610594SGeorge.Wilson@Sun.COM * since we need to do I/O but we do keep the 452710594SGeorge.Wilson@Sun.COM * spa_namespace_lock held. Once this completes the device 452810594SGeorge.Wilson@Sun.COM * should no longer have any blocks allocated on it. 452910594SGeorge.Wilson@Sun.COM */ 453010594SGeorge.Wilson@Sun.COM if (vd->vdev_islog) { 453112296SLin.Ling@Sun.COM if (vd->vdev_stat.vs_alloc != 0) 453212296SLin.Ling@Sun.COM error = spa_offline_log(spa); 453310974SJeff.Bonwick@Sun.COM } else { 453412296SLin.Ling@Sun.COM error = ENOTSUP; 453510594SGeorge.Wilson@Sun.COM } 453610594SGeorge.Wilson@Sun.COM 453710974SJeff.Bonwick@Sun.COM if (error) 453810974SJeff.Bonwick@Sun.COM return (error); 453910974SJeff.Bonwick@Sun.COM 454010594SGeorge.Wilson@Sun.COM /* 454110974SJeff.Bonwick@Sun.COM * The evacuation succeeded. Remove any remaining MOS metadata 454210974SJeff.Bonwick@Sun.COM * associated with this vdev, and wait for these changes to sync. 454310594SGeorge.Wilson@Sun.COM */ 454412296SLin.Ling@Sun.COM ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 454510594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 454610594SGeorge.Wilson@Sun.COM vd->vdev_removing = B_TRUE; 454710594SGeorge.Wilson@Sun.COM vdev_dirty(vd, 0, NULL, txg); 454810594SGeorge.Wilson@Sun.COM vdev_config_dirty(vd); 454910594SGeorge.Wilson@Sun.COM spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 455010594SGeorge.Wilson@Sun.COM 455110594SGeorge.Wilson@Sun.COM return (0); 455210594SGeorge.Wilson@Sun.COM } 455310594SGeorge.Wilson@Sun.COM 455410594SGeorge.Wilson@Sun.COM /* 455510594SGeorge.Wilson@Sun.COM * Complete the removal by cleaning up the namespace. 455610594SGeorge.Wilson@Sun.COM */ 455712296SLin.Ling@Sun.COM static void 455810974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 455910594SGeorge.Wilson@Sun.COM { 456010594SGeorge.Wilson@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 456110594SGeorge.Wilson@Sun.COM uint64_t id = vd->vdev_id; 456210594SGeorge.Wilson@Sun.COM boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 456310594SGeorge.Wilson@Sun.COM 456410594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 456510594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 456610922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 456710594SGeorge.Wilson@Sun.COM 456812296SLin.Ling@Sun.COM /* 456912296SLin.Ling@Sun.COM * Only remove any devices which are empty. 457012296SLin.Ling@Sun.COM */ 457112296SLin.Ling@Sun.COM if (vd->vdev_stat.vs_alloc != 0) 457212296SLin.Ling@Sun.COM return; 457312296SLin.Ling@Sun.COM 457410594SGeorge.Wilson@Sun.COM (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 457510922SJeff.Bonwick@Sun.COM 457610922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_state_dirty_node)) 457710922SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 457810922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_config_dirty_node)) 457910922SJeff.Bonwick@Sun.COM vdev_config_clean(vd); 458010922SJeff.Bonwick@Sun.COM 458110594SGeorge.Wilson@Sun.COM vdev_free(vd); 458210594SGeorge.Wilson@Sun.COM 458310594SGeorge.Wilson@Sun.COM if (last_vdev) { 458410594SGeorge.Wilson@Sun.COM vdev_compact_children(rvd); 458510594SGeorge.Wilson@Sun.COM } else { 458610594SGeorge.Wilson@Sun.COM vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 458710594SGeorge.Wilson@Sun.COM vdev_add_child(rvd, vd); 458810594SGeorge.Wilson@Sun.COM } 458912352SLin.Ling@Sun.COM vdev_config_dirty(rvd); 459012352SLin.Ling@Sun.COM 459112352SLin.Ling@Sun.COM /* 459212352SLin.Ling@Sun.COM * Reassess the health of our root vdev. 459312352SLin.Ling@Sun.COM */ 459412352SLin.Ling@Sun.COM vdev_reopen(rvd); 459510594SGeorge.Wilson@Sun.COM } 459610594SGeorge.Wilson@Sun.COM 459710594SGeorge.Wilson@Sun.COM /* 459812296SLin.Ling@Sun.COM * Remove a device from the pool - 459912296SLin.Ling@Sun.COM * 460012296SLin.Ling@Sun.COM * Removing a device from the vdev namespace requires several steps 460112296SLin.Ling@Sun.COM * and can take a significant amount of time. As a result we use 460212296SLin.Ling@Sun.COM * the spa_vdev_config_[enter/exit] functions which allow us to 460312296SLin.Ling@Sun.COM * grab and release the spa_config_lock while still holding the namespace 460412296SLin.Ling@Sun.COM * lock. During each step the configuration is synced out. 460512296SLin.Ling@Sun.COM */ 460612296SLin.Ling@Sun.COM 460712296SLin.Ling@Sun.COM /* 46085450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 460910594SGeorge.Wilson@Sun.COM * spares, slogs, and level 2 ARC devices. 46105450Sbrendan */ 46115450Sbrendan int 46125450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 46135450Sbrendan { 46145450Sbrendan vdev_t *vd; 461510974SJeff.Bonwick@Sun.COM metaslab_group_t *mg; 46167754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 461710594SGeorge.Wilson@Sun.COM uint64_t txg = 0; 46185450Sbrendan uint_t nspares, nl2cache; 46195450Sbrendan int error = 0; 46208241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 46218241SJeff.Bonwick@Sun.COM 46228241SJeff.Bonwick@Sun.COM if (!locked) 46238241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 46245450Sbrendan 46256643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 46265450Sbrendan 46275450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 46285450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 46297754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 46307754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 46317754SJeff.Bonwick@Sun.COM /* 46327754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 46337754SJeff.Bonwick@Sun.COM * in this pool. 46347754SJeff.Bonwick@Sun.COM */ 46357754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 46367754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 46377754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 46387754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 46397754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 46407754SJeff.Bonwick@Sun.COM } else { 46417754SJeff.Bonwick@Sun.COM error = EBUSY; 46427754SJeff.Bonwick@Sun.COM } 46437754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 46445450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 46457754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 46467754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 46477754SJeff.Bonwick@Sun.COM /* 46487754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 46497754SJeff.Bonwick@Sun.COM */ 46507754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 46517754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 46525450Sbrendan spa_load_l2cache(spa); 46535450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 465410594SGeorge.Wilson@Sun.COM } else if (vd != NULL && vd->vdev_islog) { 465510594SGeorge.Wilson@Sun.COM ASSERT(!locked); 465610922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 465710594SGeorge.Wilson@Sun.COM 465810594SGeorge.Wilson@Sun.COM /* 465910594SGeorge.Wilson@Sun.COM * XXX - Once we have bp-rewrite this should 466010594SGeorge.Wilson@Sun.COM * become the common case. 466110594SGeorge.Wilson@Sun.COM */ 466210594SGeorge.Wilson@Sun.COM 466310974SJeff.Bonwick@Sun.COM mg = vd->vdev_mg; 466410974SJeff.Bonwick@Sun.COM 466510594SGeorge.Wilson@Sun.COM /* 466610974SJeff.Bonwick@Sun.COM * Stop allocating from this vdev. 466710594SGeorge.Wilson@Sun.COM */ 466810974SJeff.Bonwick@Sun.COM metaslab_group_passivate(mg); 466910594SGeorge.Wilson@Sun.COM 467010922SJeff.Bonwick@Sun.COM /* 467110922SJeff.Bonwick@Sun.COM * Wait for the youngest allocations and frees to sync, 467210922SJeff.Bonwick@Sun.COM * and then wait for the deferral of those frees to finish. 467310922SJeff.Bonwick@Sun.COM */ 467410922SJeff.Bonwick@Sun.COM spa_vdev_config_exit(spa, NULL, 467510922SJeff.Bonwick@Sun.COM txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 467610922SJeff.Bonwick@Sun.COM 467710974SJeff.Bonwick@Sun.COM /* 467810974SJeff.Bonwick@Sun.COM * Attempt to evacuate the vdev. 467910974SJeff.Bonwick@Sun.COM */ 468010974SJeff.Bonwick@Sun.COM error = spa_vdev_remove_evacuate(spa, vd); 468110974SJeff.Bonwick@Sun.COM 468210594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 468310594SGeorge.Wilson@Sun.COM 468410974SJeff.Bonwick@Sun.COM /* 468510974SJeff.Bonwick@Sun.COM * If we couldn't evacuate the vdev, unwind. 468610974SJeff.Bonwick@Sun.COM */ 468710974SJeff.Bonwick@Sun.COM if (error) { 468810974SJeff.Bonwick@Sun.COM metaslab_group_activate(mg); 468910974SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 469010974SJeff.Bonwick@Sun.COM } 469110974SJeff.Bonwick@Sun.COM 469210974SJeff.Bonwick@Sun.COM /* 469310974SJeff.Bonwick@Sun.COM * Clean up the vdev namespace. 469410974SJeff.Bonwick@Sun.COM */ 469510974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa, vd); 469610594SGeorge.Wilson@Sun.COM 46977754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 46987754SJeff.Bonwick@Sun.COM /* 46997754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 47007754SJeff.Bonwick@Sun.COM */ 47017754SJeff.Bonwick@Sun.COM error = ENOTSUP; 47027754SJeff.Bonwick@Sun.COM } else { 47037754SJeff.Bonwick@Sun.COM /* 47047754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 47057754SJeff.Bonwick@Sun.COM */ 47067754SJeff.Bonwick@Sun.COM error = ENOENT; 47075450Sbrendan } 47082082Seschrock 47098241SJeff.Bonwick@Sun.COM if (!locked) 47108241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 47118241SJeff.Bonwick@Sun.COM 47128241SJeff.Bonwick@Sun.COM return (error); 4713789Sahrens } 4714789Sahrens 4715789Sahrens /* 47164451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 47174451Seschrock * current spared, so we can detach it. 4718789Sahrens */ 47191544Seschrock static vdev_t * 47204451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 4721789Sahrens { 47221544Seschrock vdev_t *newvd, *oldvd; 47239816SGeorge.Wilson@Sun.COM 47249816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 47254451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 47261544Seschrock if (oldvd != NULL) 47271544Seschrock return (oldvd); 47281544Seschrock } 4729789Sahrens 47304451Seschrock /* 47314451Seschrock * Check for a completed replacement. 47324451Seschrock */ 4733789Sahrens if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 47341544Seschrock oldvd = vd->vdev_child[0]; 47351544Seschrock newvd = vd->vdev_child[1]; 4736789Sahrens 47378241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 473811820SVictor.Latushkin@Sun.COM vdev_dtl_empty(newvd, DTL_OUTAGE) && 47398241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 47401544Seschrock return (oldvd); 47411544Seschrock } 4742789Sahrens 47434451Seschrock /* 47444451Seschrock * Check for a completed resilver with the 'unspare' flag set. 47454451Seschrock */ 47464451Seschrock if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 47474451Seschrock newvd = vd->vdev_child[0]; 47484451Seschrock oldvd = vd->vdev_child[1]; 47494451Seschrock 47504451Seschrock if (newvd->vdev_unspare && 47518241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 475211820SVictor.Latushkin@Sun.COM vdev_dtl_empty(newvd, DTL_OUTAGE) && 47538241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) { 47544451Seschrock newvd->vdev_unspare = 0; 47554451Seschrock return (oldvd); 47564451Seschrock } 47574451Seschrock } 47584451Seschrock 47591544Seschrock return (NULL); 4760789Sahrens } 4761789Sahrens 47621544Seschrock static void 47634451Seschrock spa_vdev_resilver_done(spa_t *spa) 4764789Sahrens { 47658241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 47668241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 47678241SJeff.Bonwick@Sun.COM 47688241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4769789Sahrens 47704451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 47718241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 47728241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 47731544Seschrock guid = vd->vdev_guid; 47748241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 47758241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 47768241SJeff.Bonwick@Sun.COM sguid = 0; 47772082Seschrock /* 47782082Seschrock * If we have just finished replacing a hot spared device, then 47792082Seschrock * we need to detach the parent's first child (the original hot 47802082Seschrock * spare) as well. 47812082Seschrock */ 47828241SJeff.Bonwick@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 47832082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 47848241SJeff.Bonwick@Sun.COM ASSERT(ppvd->vdev_children == 2); 47858241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 47862082Seschrock } 47878241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 47888241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 47891544Seschrock return; 47908241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 47912082Seschrock return; 47928241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4793789Sahrens } 4794789Sahrens 47958241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 4796789Sahrens } 4797789Sahrens 4798789Sahrens /* 479911041SEric.Taylor@Sun.COM * Update the stored path or FRU for this vdev. 48001354Seschrock */ 48011354Seschrock int 48029425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 48039425SEric.Schrock@Sun.COM boolean_t ispath) 48041354Seschrock { 48056643Seschrock vdev_t *vd; 480611817SGeorge.Wilson@Sun.COM boolean_t sync = B_FALSE; 480711041SEric.Taylor@Sun.COM 480811041SEric.Taylor@Sun.COM spa_vdev_state_enter(spa, SCL_ALL); 48091354Seschrock 48109425SEric.Schrock@Sun.COM if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 481111041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOENT)); 48121354Seschrock 48131585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 481411041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 48151585Sbonwick 48169425SEric.Schrock@Sun.COM if (ispath) { 481711817SGeorge.Wilson@Sun.COM if (strcmp(value, vd->vdev_path) != 0) { 481811817SGeorge.Wilson@Sun.COM spa_strfree(vd->vdev_path); 481911817SGeorge.Wilson@Sun.COM vd->vdev_path = spa_strdup(value); 482011817SGeorge.Wilson@Sun.COM sync = B_TRUE; 482111817SGeorge.Wilson@Sun.COM } 48229425SEric.Schrock@Sun.COM } else { 482311817SGeorge.Wilson@Sun.COM if (vd->vdev_fru == NULL) { 482411817SGeorge.Wilson@Sun.COM vd->vdev_fru = spa_strdup(value); 482511817SGeorge.Wilson@Sun.COM sync = B_TRUE; 482611817SGeorge.Wilson@Sun.COM } else if (strcmp(value, vd->vdev_fru) != 0) { 48279425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 482811817SGeorge.Wilson@Sun.COM vd->vdev_fru = spa_strdup(value); 482911817SGeorge.Wilson@Sun.COM sync = B_TRUE; 483011817SGeorge.Wilson@Sun.COM } 48319425SEric.Schrock@Sun.COM } 48321354Seschrock 483311817SGeorge.Wilson@Sun.COM return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 48341354Seschrock } 48351354Seschrock 48369425SEric.Schrock@Sun.COM int 48379425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 48389425SEric.Schrock@Sun.COM { 48399425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 48409425SEric.Schrock@Sun.COM } 48419425SEric.Schrock@Sun.COM 48429425SEric.Schrock@Sun.COM int 48439425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 48449425SEric.Schrock@Sun.COM { 48459425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 48469425SEric.Schrock@Sun.COM } 48479425SEric.Schrock@Sun.COM 48481354Seschrock /* 4849789Sahrens * ========================================================================== 485012296SLin.Ling@Sun.COM * SPA Scanning 4851789Sahrens * ========================================================================== 4852789Sahrens */ 4853789Sahrens 48547046Sahrens int 485512296SLin.Ling@Sun.COM spa_scan_stop(spa_t *spa) 4856789Sahrens { 48577754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 485812296SLin.Ling@Sun.COM if (dsl_scan_resilvering(spa->spa_dsl_pool)) 485912296SLin.Ling@Sun.COM return (EBUSY); 486012296SLin.Ling@Sun.COM return (dsl_scan_cancel(spa->spa_dsl_pool)); 486112296SLin.Ling@Sun.COM } 486212296SLin.Ling@Sun.COM 486312296SLin.Ling@Sun.COM int 486412296SLin.Ling@Sun.COM spa_scan(spa_t *spa, pool_scan_func_t func) 486512296SLin.Ling@Sun.COM { 486612296SLin.Ling@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 486712296SLin.Ling@Sun.COM 486812296SLin.Ling@Sun.COM if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 4869789Sahrens return (ENOTSUP); 4870789Sahrens 4871789Sahrens /* 48727046Sahrens * If a resilver was requested, but there is no DTL on a 48737046Sahrens * writeable leaf device, we have nothing to do. 4874789Sahrens */ 487512296SLin.Ling@Sun.COM if (func == POOL_SCAN_RESILVER && 48767046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 48777046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 48781544Seschrock return (0); 48791544Seschrock } 4880789Sahrens 488112296SLin.Ling@Sun.COM return (dsl_scan(spa->spa_dsl_pool, func)); 4882789Sahrens } 4883789Sahrens 48841544Seschrock /* 48851544Seschrock * ========================================================================== 48861544Seschrock * SPA async task processing 48871544Seschrock * ========================================================================== 48881544Seschrock */ 48891544Seschrock 48901544Seschrock static void 48914451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 4892789Sahrens { 48937361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 489412247SGeorge.Wilson@Sun.COM vd->vdev_remove_wanted = B_FALSE; 489512247SGeorge.Wilson@Sun.COM vd->vdev_delayed_close = B_FALSE; 48967361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 489710575SEric.Schrock@Sun.COM 489810575SEric.Schrock@Sun.COM /* 489910575SEric.Schrock@Sun.COM * We want to clear the stats, but we don't want to do a full 490010575SEric.Schrock@Sun.COM * vdev_clear() as that will cause us to throw away 490110575SEric.Schrock@Sun.COM * degraded/faulted state as well as attempt to reopen the 490210575SEric.Schrock@Sun.COM * device, all of which is a waste. 490310575SEric.Schrock@Sun.COM */ 490410575SEric.Schrock@Sun.COM vd->vdev_stat.vs_read_errors = 0; 490510575SEric.Schrock@Sun.COM vd->vdev_stat.vs_write_errors = 0; 490610575SEric.Schrock@Sun.COM vd->vdev_stat.vs_checksum_errors = 0; 490710575SEric.Schrock@Sun.COM 49087754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 49091544Seschrock } 49107361SBrendan.Gregg@Sun.COM 49117754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 49127361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 49131544Seschrock } 49141544Seschrock 49151544Seschrock static void 49167754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 49177754SJeff.Bonwick@Sun.COM { 49187754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 491912247SGeorge.Wilson@Sun.COM vd->vdev_probe_wanted = B_FALSE; 49207754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 49217754SJeff.Bonwick@Sun.COM } 49227754SJeff.Bonwick@Sun.COM 49237754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 49247754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 49257754SJeff.Bonwick@Sun.COM } 49267754SJeff.Bonwick@Sun.COM 49277754SJeff.Bonwick@Sun.COM static void 49289816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd) 49299816SGeorge.Wilson@Sun.COM { 49309816SGeorge.Wilson@Sun.COM sysevent_id_t eid; 49319816SGeorge.Wilson@Sun.COM nvlist_t *attr; 49329816SGeorge.Wilson@Sun.COM char *physpath; 49339816SGeorge.Wilson@Sun.COM 49349816SGeorge.Wilson@Sun.COM if (!spa->spa_autoexpand) 49359816SGeorge.Wilson@Sun.COM return; 49369816SGeorge.Wilson@Sun.COM 49379816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 49389816SGeorge.Wilson@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 49399816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, cvd); 49409816SGeorge.Wilson@Sun.COM } 49419816SGeorge.Wilson@Sun.COM 49429816SGeorge.Wilson@Sun.COM if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 49439816SGeorge.Wilson@Sun.COM return; 49449816SGeorge.Wilson@Sun.COM 49459816SGeorge.Wilson@Sun.COM physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 49469816SGeorge.Wilson@Sun.COM (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 49479816SGeorge.Wilson@Sun.COM 49489816SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 49499816SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 49509816SGeorge.Wilson@Sun.COM 49519816SGeorge.Wilson@Sun.COM (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 49529816SGeorge.Wilson@Sun.COM ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 49539816SGeorge.Wilson@Sun.COM 49549816SGeorge.Wilson@Sun.COM nvlist_free(attr); 49559816SGeorge.Wilson@Sun.COM kmem_free(physpath, MAXPATHLEN); 49569816SGeorge.Wilson@Sun.COM } 49579816SGeorge.Wilson@Sun.COM 49589816SGeorge.Wilson@Sun.COM static void 49591544Seschrock spa_async_thread(spa_t *spa) 49601544Seschrock { 49617754SJeff.Bonwick@Sun.COM int tasks; 49621544Seschrock 49631544Seschrock ASSERT(spa->spa_sync_on); 4964789Sahrens 49651544Seschrock mutex_enter(&spa->spa_async_lock); 49661544Seschrock tasks = spa->spa_async_tasks; 49671544Seschrock spa->spa_async_tasks = 0; 49681544Seschrock mutex_exit(&spa->spa_async_lock); 49691544Seschrock 49701544Seschrock /* 49711635Sbonwick * See if the config needs to be updated. 49721635Sbonwick */ 49731635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 497410922SJeff.Bonwick@Sun.COM uint64_t old_space, new_space; 49759816SGeorge.Wilson@Sun.COM 49761635Sbonwick mutex_enter(&spa_namespace_lock); 497710922SJeff.Bonwick@Sun.COM old_space = metaslab_class_get_space(spa_normal_class(spa)); 49781635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 497910922SJeff.Bonwick@Sun.COM new_space = metaslab_class_get_space(spa_normal_class(spa)); 49801635Sbonwick mutex_exit(&spa_namespace_lock); 49819816SGeorge.Wilson@Sun.COM 49829816SGeorge.Wilson@Sun.COM /* 49839816SGeorge.Wilson@Sun.COM * If the pool grew as a result of the config update, 49849816SGeorge.Wilson@Sun.COM * then log an internal history event. 49859816SGeorge.Wilson@Sun.COM */ 498610922SJeff.Bonwick@Sun.COM if (new_space != old_space) { 498712296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_VDEV_ONLINE, 498812296SLin.Ling@Sun.COM spa, NULL, 49899946SMark.Musante@Sun.COM "pool '%s' size: %llu(+%llu)", 499010922SJeff.Bonwick@Sun.COM spa_name(spa), new_space, new_space - old_space); 49919816SGeorge.Wilson@Sun.COM } 49921635Sbonwick } 49931635Sbonwick 49941635Sbonwick /* 49954451Seschrock * See if any devices need to be marked REMOVED. 49961544Seschrock */ 49977754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 499810685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 49994451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 50007754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 50017361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 50027754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 50037361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 50047754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 50057754SJeff.Bonwick@Sun.COM } 50067754SJeff.Bonwick@Sun.COM 50079816SGeorge.Wilson@Sun.COM if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 50089816SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 50099816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, spa->spa_root_vdev); 50109816SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 50119816SGeorge.Wilson@Sun.COM } 50129816SGeorge.Wilson@Sun.COM 50137754SJeff.Bonwick@Sun.COM /* 50147754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 50157754SJeff.Bonwick@Sun.COM */ 50167754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 501710685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 50187754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 50197754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 50204451Seschrock } 50211544Seschrock 50221544Seschrock /* 50231544Seschrock * If any devices are done replacing, detach them. 50241544Seschrock */ 50254451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 50264451Seschrock spa_vdev_resilver_done(spa); 5027789Sahrens 50281544Seschrock /* 50291544Seschrock * Kick off a resilver. 50301544Seschrock */ 50317046Sahrens if (tasks & SPA_ASYNC_RESILVER) 503212296SLin.Ling@Sun.COM dsl_resilver_restart(spa->spa_dsl_pool, 0); 50331544Seschrock 50341544Seschrock /* 50351544Seschrock * Let the world know that we're done. 50361544Seschrock */ 50371544Seschrock mutex_enter(&spa->spa_async_lock); 50381544Seschrock spa->spa_async_thread = NULL; 50391544Seschrock cv_broadcast(&spa->spa_async_cv); 50401544Seschrock mutex_exit(&spa->spa_async_lock); 50411544Seschrock thread_exit(); 50421544Seschrock } 50431544Seschrock 50441544Seschrock void 50451544Seschrock spa_async_suspend(spa_t *spa) 50461544Seschrock { 50471544Seschrock mutex_enter(&spa->spa_async_lock); 50481544Seschrock spa->spa_async_suspended++; 50491544Seschrock while (spa->spa_async_thread != NULL) 50501544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 50511544Seschrock mutex_exit(&spa->spa_async_lock); 50521544Seschrock } 50531544Seschrock 50541544Seschrock void 50551544Seschrock spa_async_resume(spa_t *spa) 50561544Seschrock { 50571544Seschrock mutex_enter(&spa->spa_async_lock); 50581544Seschrock ASSERT(spa->spa_async_suspended != 0); 50591544Seschrock spa->spa_async_suspended--; 50601544Seschrock mutex_exit(&spa->spa_async_lock); 50611544Seschrock } 50621544Seschrock 50631544Seschrock static void 50641544Seschrock spa_async_dispatch(spa_t *spa) 50651544Seschrock { 50661544Seschrock mutex_enter(&spa->spa_async_lock); 50671544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 50681635Sbonwick spa->spa_async_thread == NULL && 50691635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 50701544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 50711544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 50721544Seschrock mutex_exit(&spa->spa_async_lock); 50731544Seschrock } 50741544Seschrock 50751544Seschrock void 50761544Seschrock spa_async_request(spa_t *spa, int task) 50771544Seschrock { 507812296SLin.Ling@Sun.COM zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 50791544Seschrock mutex_enter(&spa->spa_async_lock); 50801544Seschrock spa->spa_async_tasks |= task; 50811544Seschrock mutex_exit(&spa->spa_async_lock); 5082789Sahrens } 5083789Sahrens 5084789Sahrens /* 5085789Sahrens * ========================================================================== 5086789Sahrens * SPA syncing routines 5087789Sahrens * ========================================================================== 5088789Sahrens */ 508912470SMatthew.Ahrens@Sun.COM 509012470SMatthew.Ahrens@Sun.COM static int 509112470SMatthew.Ahrens@Sun.COM bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5092789Sahrens { 509312470SMatthew.Ahrens@Sun.COM bpobj_t *bpo = arg; 509412470SMatthew.Ahrens@Sun.COM bpobj_enqueue(bpo, bp, tx); 509512470SMatthew.Ahrens@Sun.COM return (0); 509610922SJeff.Bonwick@Sun.COM } 509710922SJeff.Bonwick@Sun.COM 509812470SMatthew.Ahrens@Sun.COM static int 509912470SMatthew.Ahrens@Sun.COM spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 510010922SJeff.Bonwick@Sun.COM { 510110922SJeff.Bonwick@Sun.COM zio_t *zio = arg; 510210922SJeff.Bonwick@Sun.COM 510310922SJeff.Bonwick@Sun.COM zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 510410922SJeff.Bonwick@Sun.COM zio->io_flags)); 510512470SMatthew.Ahrens@Sun.COM return (0); 5106789Sahrens } 5107789Sahrens 5108789Sahrens static void 51092082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 51102082Seschrock { 51112082Seschrock char *packed = NULL; 51127497STim.Haley@Sun.COM size_t bufsize; 51132082Seschrock size_t nvsize = 0; 51142082Seschrock dmu_buf_t *db; 51152082Seschrock 51162082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 51172082Seschrock 51187497STim.Haley@Sun.COM /* 51197497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 51207497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 51217497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 51227497STim.Haley@Sun.COM */ 51237497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 51247497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 51252082Seschrock 51262082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 51272082Seschrock KM_SLEEP) == 0); 51287497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 51297497STim.Haley@Sun.COM 51307497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 51317497STim.Haley@Sun.COM 51327497STim.Haley@Sun.COM kmem_free(packed, bufsize); 51332082Seschrock 51342082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 51352082Seschrock dmu_buf_will_dirty(db, tx); 51362082Seschrock *(uint64_t *)db->db_data = nvsize; 51372082Seschrock dmu_buf_rele(db, FTAG); 51382082Seschrock } 51392082Seschrock 51402082Seschrock static void 51415450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 51425450Sbrendan const char *config, const char *entry) 51432082Seschrock { 51442082Seschrock nvlist_t *nvroot; 51455450Sbrendan nvlist_t **list; 51462082Seschrock int i; 51472082Seschrock 51485450Sbrendan if (!sav->sav_sync) 51492082Seschrock return; 51502082Seschrock 51512082Seschrock /* 51525450Sbrendan * Update the MOS nvlist describing the list of available devices. 51535450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 51544451Seschrock * valid and the vdevs are labeled appropriately. 51552082Seschrock */ 51565450Sbrendan if (sav->sav_object == 0) { 51575450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 51585450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 51595450Sbrendan sizeof (uint64_t), tx); 51602082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 51615450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 51625450Sbrendan &sav->sav_object, tx) == 0); 51632082Seschrock } 51642082Seschrock 51652082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 51665450Sbrendan if (sav->sav_count == 0) { 51675450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 51682082Seschrock } else { 51695450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 51705450Sbrendan for (i = 0; i < sav->sav_count; i++) 51715450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 517212296SLin.Ling@Sun.COM B_FALSE, VDEV_CONFIG_L2CACHE); 51735450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 51745450Sbrendan sav->sav_count) == 0); 51755450Sbrendan for (i = 0; i < sav->sav_count; i++) 51765450Sbrendan nvlist_free(list[i]); 51775450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 51782082Seschrock } 51792082Seschrock 51805450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 51812926Sek110237 nvlist_free(nvroot); 51822082Seschrock 51835450Sbrendan sav->sav_sync = B_FALSE; 51842082Seschrock } 51852082Seschrock 51862082Seschrock static void 5187789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 5188789Sahrens { 5189789Sahrens nvlist_t *config; 5190789Sahrens 51917754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 5192789Sahrens return; 5193789Sahrens 51947754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 51957754SJeff.Bonwick@Sun.COM 51967754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 51977754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 51987754SJeff.Bonwick@Sun.COM 51997754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 5200789Sahrens 52011635Sbonwick if (spa->spa_config_syncing) 52021635Sbonwick nvlist_free(spa->spa_config_syncing); 52031635Sbonwick spa->spa_config_syncing = config; 5204789Sahrens 52052082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 5206789Sahrens } 5207789Sahrens 52085094Slling /* 52095094Slling * Set zpool properties. 52105094Slling */ 52113912Slling static void 521212296SLin.Ling@Sun.COM spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 52133912Slling { 52143912Slling spa_t *spa = arg1; 52155094Slling objset_t *mos = spa->spa_meta_objset; 52163912Slling nvlist_t *nvp = arg2; 52175094Slling nvpair_t *elem; 52184451Seschrock uint64_t intval; 52196643Seschrock char *strval; 52205094Slling zpool_prop_t prop; 52215094Slling const char *propname; 52225094Slling zprop_type_t proptype; 52235094Slling 52247754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 52257754SJeff.Bonwick@Sun.COM 52265094Slling elem = NULL; 52275094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 52285094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 52295094Slling case ZPOOL_PROP_VERSION: 52305094Slling /* 52315094Slling * Only set version for non-zpool-creation cases 52325094Slling * (set/import). spa_create() needs special care 52335094Slling * for version setting. 52345094Slling */ 52355094Slling if (tx->tx_txg != TXG_INITIAL) { 52365094Slling VERIFY(nvpair_value_uint64(elem, 52375094Slling &intval) == 0); 52385094Slling ASSERT(intval <= SPA_VERSION); 52395094Slling ASSERT(intval >= spa_version(spa)); 52405094Slling spa->spa_uberblock.ub_version = intval; 52415094Slling vdev_config_dirty(spa->spa_root_vdev); 52425094Slling } 52435094Slling break; 52445094Slling 52455094Slling case ZPOOL_PROP_ALTROOT: 52465094Slling /* 52475094Slling * 'altroot' is a non-persistent property. It should 52485094Slling * have been set temporarily at creation or import time. 52495094Slling */ 52505094Slling ASSERT(spa->spa_root != NULL); 52515094Slling break; 52525094Slling 52535363Seschrock case ZPOOL_PROP_CACHEFILE: 52545094Slling /* 52558525SEric.Schrock@Sun.COM * 'cachefile' is also a non-persisitent property. 52565094Slling */ 52574543Smarks break; 52585094Slling default: 52595094Slling /* 52605094Slling * Set pool property values in the poolprops mos object. 52615094Slling */ 52625094Slling if (spa->spa_pool_props_object == 0) { 52635094Slling VERIFY((spa->spa_pool_props_object = 52645094Slling zap_create(mos, DMU_OT_POOL_PROPS, 52655094Slling DMU_OT_NONE, 0, tx)) > 0); 52665094Slling 52675094Slling VERIFY(zap_update(mos, 52685094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 52695094Slling 8, 1, &spa->spa_pool_props_object, tx) 52705094Slling == 0); 52715094Slling } 52725094Slling 52735094Slling /* normalize the property name */ 52745094Slling propname = zpool_prop_to_name(prop); 52755094Slling proptype = zpool_prop_get_type(prop); 52765094Slling 52775094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 52785094Slling ASSERT(proptype == PROP_TYPE_STRING); 52795094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 52805094Slling VERIFY(zap_update(mos, 52815094Slling spa->spa_pool_props_object, propname, 52825094Slling 1, strlen(strval) + 1, strval, tx) == 0); 52835094Slling 52845094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 52855094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 52865094Slling 52875094Slling if (proptype == PROP_TYPE_INDEX) { 52885094Slling const char *unused; 52895094Slling VERIFY(zpool_prop_index_to_string( 52905094Slling prop, intval, &unused) == 0); 52915094Slling } 52925094Slling VERIFY(zap_update(mos, 52935094Slling spa->spa_pool_props_object, propname, 52945094Slling 8, 1, &intval, tx) == 0); 52955094Slling } else { 52965094Slling ASSERT(0); /* not allowed */ 52975094Slling } 52985094Slling 52995329Sgw25295 switch (prop) { 53005329Sgw25295 case ZPOOL_PROP_DELEGATION: 53015094Slling spa->spa_delegation = intval; 53025329Sgw25295 break; 53035329Sgw25295 case ZPOOL_PROP_BOOTFS: 53045094Slling spa->spa_bootfs = intval; 53055329Sgw25295 break; 53065329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 53075329Sgw25295 spa->spa_failmode = intval; 53085329Sgw25295 break; 53099816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 53109816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = intval; 531112318SEric.Taylor@Sun.COM if (tx->tx_txg != TXG_INITIAL) 531212318SEric.Taylor@Sun.COM spa_async_request(spa, 531312318SEric.Taylor@Sun.COM SPA_ASYNC_AUTOEXPAND); 53149816SGeorge.Wilson@Sun.COM break; 531510922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 531610922SJeff.Bonwick@Sun.COM spa->spa_dedup_ditto = intval; 531710922SJeff.Bonwick@Sun.COM break; 53185329Sgw25295 default: 53195329Sgw25295 break; 53205329Sgw25295 } 53213912Slling } 53225094Slling 53235094Slling /* log internal history if this is not a zpool create */ 53245094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 53255094Slling tx->tx_txg != TXG_INITIAL) { 532612296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_PROPSET, 532712296SLin.Ling@Sun.COM spa, tx, "%s %lld %s", 53287754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 53295094Slling } 53303912Slling } 53317754SJeff.Bonwick@Sun.COM 53327754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 53333912Slling } 53343912Slling 5335789Sahrens /* 533612470SMatthew.Ahrens@Sun.COM * Perform one-time upgrade on-disk changes. spa_version() does not 533712470SMatthew.Ahrens@Sun.COM * reflect the new version this txg, so there must be no changes this 533812470SMatthew.Ahrens@Sun.COM * txg to anything that the upgrade code depends on after it executes. 533912470SMatthew.Ahrens@Sun.COM * Therefore this must be called after dsl_pool_sync() does the sync 534012470SMatthew.Ahrens@Sun.COM * tasks. 534112470SMatthew.Ahrens@Sun.COM */ 534212470SMatthew.Ahrens@Sun.COM static void 534312470SMatthew.Ahrens@Sun.COM spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 534412470SMatthew.Ahrens@Sun.COM { 534512470SMatthew.Ahrens@Sun.COM dsl_pool_t *dp = spa->spa_dsl_pool; 534612470SMatthew.Ahrens@Sun.COM 534712470SMatthew.Ahrens@Sun.COM ASSERT(spa->spa_sync_pass == 1); 534812470SMatthew.Ahrens@Sun.COM 534912470SMatthew.Ahrens@Sun.COM if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 535012470SMatthew.Ahrens@Sun.COM spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 535112470SMatthew.Ahrens@Sun.COM dsl_pool_create_origin(dp, tx); 535212470SMatthew.Ahrens@Sun.COM 535312470SMatthew.Ahrens@Sun.COM /* Keeping the origin open increases spa_minref */ 535412470SMatthew.Ahrens@Sun.COM spa->spa_minref += 3; 535512470SMatthew.Ahrens@Sun.COM } 535612470SMatthew.Ahrens@Sun.COM 535712470SMatthew.Ahrens@Sun.COM if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 535812470SMatthew.Ahrens@Sun.COM spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 535912470SMatthew.Ahrens@Sun.COM dsl_pool_upgrade_clones(dp, tx); 536012470SMatthew.Ahrens@Sun.COM } 536112470SMatthew.Ahrens@Sun.COM 536212470SMatthew.Ahrens@Sun.COM if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 536312470SMatthew.Ahrens@Sun.COM spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 536412470SMatthew.Ahrens@Sun.COM dsl_pool_upgrade_dir_clones(dp, tx); 536512470SMatthew.Ahrens@Sun.COM 536612470SMatthew.Ahrens@Sun.COM /* Keeping the freedir open increases spa_minref */ 536712470SMatthew.Ahrens@Sun.COM spa->spa_minref += 3; 536812470SMatthew.Ahrens@Sun.COM } 536912470SMatthew.Ahrens@Sun.COM } 537012470SMatthew.Ahrens@Sun.COM 537112470SMatthew.Ahrens@Sun.COM /* 5372789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 5373789Sahrens * part of the process, so we iterate until it converges. 5374789Sahrens */ 5375789Sahrens void 5376789Sahrens spa_sync(spa_t *spa, uint64_t txg) 5377789Sahrens { 5378789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5379789Sahrens objset_t *mos = spa->spa_meta_objset; 538012470SMatthew.Ahrens@Sun.COM bpobj_t *defer_bpo = &spa->spa_deferred_bpobj; 538110922SJeff.Bonwick@Sun.COM bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 53821635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 5383789Sahrens vdev_t *vd; 5384789Sahrens dmu_tx_t *tx; 53857754SJeff.Bonwick@Sun.COM int error; 5386789Sahrens 5387789Sahrens /* 5388789Sahrens * Lock out configuration changes. 5389789Sahrens */ 53907754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5391789Sahrens 5392789Sahrens spa->spa_syncing_txg = txg; 5393789Sahrens spa->spa_sync_pass = 0; 5394789Sahrens 53957754SJeff.Bonwick@Sun.COM /* 53967754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 53977754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 53987754SJeff.Bonwick@Sun.COM */ 53997754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 54008241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 54018241SJeff.Bonwick@Sun.COM /* 54028241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 54038241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 54048241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 54058241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 54068241SJeff.Bonwick@Sun.COM * into the root vdev tree. 54078241SJeff.Bonwick@Sun.COM */ 54088241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 54098241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 54108241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 54118241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 54128241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 54138241SJeff.Bonwick@Sun.COM } 54148241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 54158241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 54167754SJeff.Bonwick@Sun.COM } 54177754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 54187754SJeff.Bonwick@Sun.COM 54192082Seschrock tx = dmu_tx_create_assigned(dp, txg); 54202082Seschrock 54212082Seschrock /* 54224577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 54232082Seschrock * set spa_deflate if we have no raid-z vdevs. 54242082Seschrock */ 54254577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 54264577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 54272082Seschrock int i; 54282082Seschrock 54292082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 54302082Seschrock vd = rvd->vdev_child[i]; 54312082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 54322082Seschrock break; 54332082Seschrock } 54342082Seschrock if (i == rvd->vdev_children) { 54352082Seschrock spa->spa_deflate = TRUE; 54362082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 54372082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 54382082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 54392082Seschrock } 54402082Seschrock } 54412082Seschrock 5442789Sahrens /* 544312296SLin.Ling@Sun.COM * If anything has changed in this txg, or if someone is waiting 544412296SLin.Ling@Sun.COM * for this txg to sync (eg, spa_vdev_remove()), push the 544512296SLin.Ling@Sun.COM * deferred frees from the previous txg. If not, leave them 544612296SLin.Ling@Sun.COM * alone so that we don't generate work on an otherwise idle 544712296SLin.Ling@Sun.COM * system. 5448789Sahrens */ 5449789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 54502329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 545112296SLin.Ling@Sun.COM !txg_list_empty(&dp->dp_sync_tasks, txg) || 545212470SMatthew.Ahrens@Sun.COM ((dsl_scan_active(dp->dp_scan) || 545312470SMatthew.Ahrens@Sun.COM txg_sync_waiting(dp)) && !spa_shutting_down(spa))) { 545412470SMatthew.Ahrens@Sun.COM zio_t *zio = zio_root(spa, NULL, NULL, 0); 545512470SMatthew.Ahrens@Sun.COM VERIFY3U(bpobj_iterate(defer_bpo, 545612470SMatthew.Ahrens@Sun.COM spa_free_sync_cb, zio, tx), ==, 0); 545712470SMatthew.Ahrens@Sun.COM VERIFY3U(zio_wait(zio), ==, 0); 545812470SMatthew.Ahrens@Sun.COM } 5459789Sahrens 5460789Sahrens /* 5461789Sahrens * Iterate to convergence. 5462789Sahrens */ 5463789Sahrens do { 546410922SJeff.Bonwick@Sun.COM int pass = ++spa->spa_sync_pass; 5465789Sahrens 5466789Sahrens spa_sync_config_object(spa, tx); 54675450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 54685450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 54695450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 54705450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 54711544Seschrock spa_errlog_sync(spa, txg); 5472789Sahrens dsl_pool_sync(dp, txg); 5473789Sahrens 547410922SJeff.Bonwick@Sun.COM if (pass <= SYNC_PASS_DEFERRED_FREE) { 547510922SJeff.Bonwick@Sun.COM zio_t *zio = zio_root(spa, NULL, NULL, 0); 547612470SMatthew.Ahrens@Sun.COM bplist_iterate(free_bpl, spa_free_sync_cb, 547712470SMatthew.Ahrens@Sun.COM zio, tx); 547810922SJeff.Bonwick@Sun.COM VERIFY(zio_wait(zio) == 0); 547910922SJeff.Bonwick@Sun.COM } else { 548012470SMatthew.Ahrens@Sun.COM bplist_iterate(free_bpl, bpobj_enqueue_cb, 548112470SMatthew.Ahrens@Sun.COM defer_bpo, tx); 5482789Sahrens } 5483789Sahrens 548410922SJeff.Bonwick@Sun.COM ddt_sync(spa, txg); 548512296SLin.Ling@Sun.COM dsl_scan_sync(dp, tx); 548611619SGeorge.Wilson@Sun.COM 548710922SJeff.Bonwick@Sun.COM while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 548810922SJeff.Bonwick@Sun.COM vdev_sync(vd, txg); 548910922SJeff.Bonwick@Sun.COM 549012470SMatthew.Ahrens@Sun.COM if (pass == 1) 549112470SMatthew.Ahrens@Sun.COM spa_sync_upgrades(spa, tx); 549212470SMatthew.Ahrens@Sun.COM 549310922SJeff.Bonwick@Sun.COM } while (dmu_objset_is_dirty(mos, txg)); 549410922SJeff.Bonwick@Sun.COM 5495789Sahrens /* 5496789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 5497789Sahrens * to commit the transaction group. 54981635Sbonwick * 54995688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 55005688Sbonwick * random top-level vdevs that are known to be visible in the 55017754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 55027754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 5503789Sahrens */ 55047754SJeff.Bonwick@Sun.COM for (;;) { 55057754SJeff.Bonwick@Sun.COM /* 55067754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 55077754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 55087754SJeff.Bonwick@Sun.COM */ 55097754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 55107754SJeff.Bonwick@Sun.COM 55117754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 55127754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 55137754SJeff.Bonwick@Sun.COM int svdcount = 0; 55147754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 55157754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 55169816SGeorge.Wilson@Sun.COM 55179816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 55187754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 55197754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 55207754SJeff.Bonwick@Sun.COM continue; 55217754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 55227754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 55237754SJeff.Bonwick@Sun.COM break; 55247754SJeff.Bonwick@Sun.COM } 55259725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 55269725SEric.Schrock@Sun.COM if (error != 0) 55279725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, 55289725SEric.Schrock@Sun.COM B_TRUE); 55297754SJeff.Bonwick@Sun.COM } else { 55307754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 55319725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_FALSE); 55329725SEric.Schrock@Sun.COM if (error != 0) 55339725SEric.Schrock@Sun.COM error = vdev_config_sync(rvd->vdev_child, 55349725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_TRUE); 55351635Sbonwick } 55367754SJeff.Bonwick@Sun.COM 55377754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 55387754SJeff.Bonwick@Sun.COM 55397754SJeff.Bonwick@Sun.COM if (error == 0) 55407754SJeff.Bonwick@Sun.COM break; 55417754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 55427754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 55431635Sbonwick } 55442082Seschrock dmu_tx_commit(tx); 55452082Seschrock 55461635Sbonwick /* 55471635Sbonwick * Clear the dirty config list. 55481635Sbonwick */ 55497754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 55501635Sbonwick vdev_config_clean(vd); 55511635Sbonwick 55521635Sbonwick /* 55531635Sbonwick * Now that the new config has synced transactionally, 55541635Sbonwick * let it become visible to the config cache. 55551635Sbonwick */ 55561635Sbonwick if (spa->spa_config_syncing != NULL) { 55571635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 55581635Sbonwick spa->spa_config_txg = txg; 55591635Sbonwick spa->spa_config_syncing = NULL; 55601635Sbonwick } 5561789Sahrens 5562789Sahrens spa->spa_ubsync = spa->spa_uberblock; 5563789Sahrens 556410922SJeff.Bonwick@Sun.COM dsl_pool_sync_done(dp, txg); 5565789Sahrens 5566789Sahrens /* 5567789Sahrens * Update usable space statistics. 5568789Sahrens */ 5569789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 5570789Sahrens vdev_sync_done(vd, txg); 5571789Sahrens 557210956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 557310956SGeorge.Wilson@Sun.COM 5574789Sahrens /* 5575789Sahrens * It had better be the case that we didn't dirty anything 55762082Seschrock * since vdev_config_sync(). 5577789Sahrens */ 5578789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 5579789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 5580789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 558110922SJeff.Bonwick@Sun.COM 558210922SJeff.Bonwick@Sun.COM spa->spa_sync_pass = 0; 5583789Sahrens 55847754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 55851544Seschrock 558610921STim.Haley@Sun.COM spa_handle_ignored_writes(spa); 558710921STim.Haley@Sun.COM 55881544Seschrock /* 55891544Seschrock * If any async tasks have been requested, kick them off. 55901544Seschrock */ 55911544Seschrock spa_async_dispatch(spa); 5592789Sahrens } 5593789Sahrens 5594789Sahrens /* 5595789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 5596789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 5597789Sahrens * sync. 5598789Sahrens */ 5599789Sahrens void 5600789Sahrens spa_sync_allpools(void) 5601789Sahrens { 5602789Sahrens spa_t *spa = NULL; 5603789Sahrens mutex_enter(&spa_namespace_lock); 5604789Sahrens while ((spa = spa_next(spa)) != NULL) { 56057754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 5606789Sahrens continue; 5607789Sahrens spa_open_ref(spa, FTAG); 5608789Sahrens mutex_exit(&spa_namespace_lock); 5609789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 5610789Sahrens mutex_enter(&spa_namespace_lock); 5611789Sahrens spa_close(spa, FTAG); 5612789Sahrens } 5613789Sahrens mutex_exit(&spa_namespace_lock); 5614789Sahrens } 5615789Sahrens 5616789Sahrens /* 5617789Sahrens * ========================================================================== 5618789Sahrens * Miscellaneous routines 5619789Sahrens * ========================================================================== 5620789Sahrens */ 5621789Sahrens 5622789Sahrens /* 5623789Sahrens * Remove all pools in the system. 5624789Sahrens */ 5625789Sahrens void 5626789Sahrens spa_evict_all(void) 5627789Sahrens { 5628789Sahrens spa_t *spa; 5629789Sahrens 5630789Sahrens /* 5631789Sahrens * Remove all cached state. All pools should be closed now, 5632789Sahrens * so every spa in the AVL tree should be unreferenced. 5633789Sahrens */ 5634789Sahrens mutex_enter(&spa_namespace_lock); 5635789Sahrens while ((spa = spa_next(NULL)) != NULL) { 5636789Sahrens /* 56371544Seschrock * Stop async tasks. The async thread may need to detach 56381544Seschrock * a device that's been replaced, which requires grabbing 56391544Seschrock * spa_namespace_lock, so we must drop it here. 5640789Sahrens */ 5641789Sahrens spa_open_ref(spa, FTAG); 5642789Sahrens mutex_exit(&spa_namespace_lock); 56431544Seschrock spa_async_suspend(spa); 56444808Sek110237 mutex_enter(&spa_namespace_lock); 5645789Sahrens spa_close(spa, FTAG); 5646789Sahrens 5647789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 5648789Sahrens spa_unload(spa); 5649789Sahrens spa_deactivate(spa); 5650789Sahrens } 5651789Sahrens spa_remove(spa); 5652789Sahrens } 5653789Sahrens mutex_exit(&spa_namespace_lock); 5654789Sahrens } 56551544Seschrock 56561544Seschrock vdev_t * 56579425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 56581544Seschrock { 56596643Seschrock vdev_t *vd; 56606643Seschrock int i; 56616643Seschrock 56626643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 56636643Seschrock return (vd); 56646643Seschrock 56659425SEric.Schrock@Sun.COM if (aux) { 56666643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 56676643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 56686643Seschrock if (vd->vdev_guid == guid) 56696643Seschrock return (vd); 56706643Seschrock } 56719425SEric.Schrock@Sun.COM 56729425SEric.Schrock@Sun.COM for (i = 0; i < spa->spa_spares.sav_count; i++) { 56739425SEric.Schrock@Sun.COM vd = spa->spa_spares.sav_vdevs[i]; 56749425SEric.Schrock@Sun.COM if (vd->vdev_guid == guid) 56759425SEric.Schrock@Sun.COM return (vd); 56769425SEric.Schrock@Sun.COM } 56776643Seschrock } 56786643Seschrock 56796643Seschrock return (NULL); 56801544Seschrock } 56811760Seschrock 56821760Seschrock void 56835094Slling spa_upgrade(spa_t *spa, uint64_t version) 56841760Seschrock { 56857754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 56861760Seschrock 56871760Seschrock /* 56881760Seschrock * This should only be called for a non-faulted pool, and since a 56891760Seschrock * future version would result in an unopenable pool, this shouldn't be 56901760Seschrock * possible. 56911760Seschrock */ 56924577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 56935094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 56945094Slling 56955094Slling spa->spa_uberblock.ub_version = version; 56961760Seschrock vdev_config_dirty(spa->spa_root_vdev); 56971760Seschrock 56987754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 56992082Seschrock 57002082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 57011760Seschrock } 57022082Seschrock 57032082Seschrock boolean_t 57042082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 57052082Seschrock { 57062082Seschrock int i; 57073377Seschrock uint64_t spareguid; 57085450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 57095450Sbrendan 57105450Sbrendan for (i = 0; i < sav->sav_count; i++) 57115450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 57122082Seschrock return (B_TRUE); 57132082Seschrock 57145450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 57155450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 57165450Sbrendan &spareguid) == 0 && spareguid == guid) 57173377Seschrock return (B_TRUE); 57183377Seschrock } 57193377Seschrock 57202082Seschrock return (B_FALSE); 57212082Seschrock } 57223912Slling 57234451Seschrock /* 57247214Slling * Check if a pool has an active shared spare device. 57257214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 57267214Slling */ 57277214Slling static boolean_t 57287214Slling spa_has_active_shared_spare(spa_t *spa) 57297214Slling { 57307214Slling int i, refcnt; 57317214Slling uint64_t pool; 57327214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 57337214Slling 57347214Slling for (i = 0; i < sav->sav_count; i++) { 57357214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 57367214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 57377214Slling refcnt > 2) 57387214Slling return (B_TRUE); 57397214Slling } 57407214Slling 57417214Slling return (B_FALSE); 57427214Slling } 57437214Slling 57447214Slling /* 57454451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 57464451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 57474451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 57484451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 57494451Seschrock * or zdb as real changes. 57504451Seschrock */ 57514451Seschrock void 57524451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 57534451Seschrock { 57544451Seschrock #ifdef _KERNEL 57554451Seschrock sysevent_t *ev; 57564451Seschrock sysevent_attr_list_t *attr = NULL; 57574451Seschrock sysevent_value_t value; 57584451Seschrock sysevent_id_t eid; 57594451Seschrock 57604451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 57614451Seschrock SE_SLEEP); 57624451Seschrock 57634451Seschrock value.value_type = SE_DATA_TYPE_STRING; 57644451Seschrock value.value.sv_string = spa_name(spa); 57654451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 57664451Seschrock goto done; 57674451Seschrock 57684451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 57694451Seschrock value.value.sv_uint64 = spa_guid(spa); 57704451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 57714451Seschrock goto done; 57724451Seschrock 57734451Seschrock if (vd) { 57744451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 57754451Seschrock value.value.sv_uint64 = vd->vdev_guid; 57764451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 57774451Seschrock SE_SLEEP) != 0) 57784451Seschrock goto done; 57794451Seschrock 57804451Seschrock if (vd->vdev_path) { 57814451Seschrock value.value_type = SE_DATA_TYPE_STRING; 57824451Seschrock value.value.sv_string = vd->vdev_path; 57834451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 57844451Seschrock &value, SE_SLEEP) != 0) 57854451Seschrock goto done; 57864451Seschrock } 57874451Seschrock } 57884451Seschrock 57895756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 57905756Seschrock goto done; 57915756Seschrock attr = NULL; 57925756Seschrock 57934451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 57944451Seschrock 57954451Seschrock done: 57964451Seschrock if (attr) 57974451Seschrock sysevent_free_attr(attr); 57984451Seschrock sysevent_free(ev); 57994451Seschrock #endif 58004451Seschrock } 5801