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); 119*13037SMark.Musante@Sun.COM static void spa_vdev_resilver_done(spa_t *spa); 1205094Slling 12111173SJonathan.Adams@Sun.COM uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */ 12211173SJonathan.Adams@Sun.COM id_t zio_taskq_psrset_bind = PS_NONE; 12311173SJonathan.Adams@Sun.COM boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 12411173SJonathan.Adams@Sun.COM uint_t zio_taskq_basedc = 80; /* base duty cycle */ 12511173SJonathan.Adams@Sun.COM 12611173SJonathan.Adams@Sun.COM boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 12711173SJonathan.Adams@Sun.COM 12811173SJonathan.Adams@Sun.COM /* 12911173SJonathan.Adams@Sun.COM * This (illegal) pool name is used when temporarily importing a spa_t in order 13011173SJonathan.Adams@Sun.COM * to get the vdev stats associated with the imported devices. 13111173SJonathan.Adams@Sun.COM */ 13211173SJonathan.Adams@Sun.COM #define TRYIMPORT_NAME "$import" 13311173SJonathan.Adams@Sun.COM 1345094Slling /* 1355094Slling * ========================================================================== 1365094Slling * SPA properties routines 1375094Slling * ========================================================================== 1385094Slling */ 1395094Slling 1405094Slling /* 1415094Slling * Add a (source=src, propname=propval) list to an nvlist. 1425094Slling */ 1435949Slling static void 1445094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 1455094Slling uint64_t intval, zprop_source_t src) 1465094Slling { 1475094Slling const char *propname = zpool_prop_to_name(prop); 1485094Slling nvlist_t *propval; 1495949Slling 1505949Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1515949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 1525949Slling 1535949Slling if (strval != NULL) 1545949Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1555949Slling else 1565949Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 1575949Slling 1585949Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 1595094Slling nvlist_free(propval); 1605094Slling } 1615094Slling 1625094Slling /* 1635094Slling * Get property values from the spa configuration. 1645094Slling */ 1655949Slling static void 1665094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 1675094Slling { 1688525SEric.Schrock@Sun.COM uint64_t size; 16910956SGeorge.Wilson@Sun.COM uint64_t alloc; 1705094Slling uint64_t cap, version; 1715094Slling zprop_source_t src = ZPROP_SRC_NONE; 1726643Seschrock spa_config_dirent_t *dp; 1735094Slling 1747754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 1757754SJeff.Bonwick@Sun.COM 1768525SEric.Schrock@Sun.COM if (spa->spa_root_vdev != NULL) { 17710956SGeorge.Wilson@Sun.COM alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 17810922SJeff.Bonwick@Sun.COM size = metaslab_class_get_space(spa_normal_class(spa)); 1798525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 1808525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 18110956SGeorge.Wilson@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 18210956SGeorge.Wilson@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 18310956SGeorge.Wilson@Sun.COM size - alloc, src); 18410956SGeorge.Wilson@Sun.COM 18510956SGeorge.Wilson@Sun.COM cap = (size == 0) ? 0 : (alloc * 100 / size); 1868525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 1878525SEric.Schrock@Sun.COM 18810922SJeff.Bonwick@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 18910922SJeff.Bonwick@Sun.COM ddt_get_pool_dedup_ratio(spa), src); 19010922SJeff.Bonwick@Sun.COM 1918525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 1928525SEric.Schrock@Sun.COM spa->spa_root_vdev->vdev_state, src); 1938525SEric.Schrock@Sun.COM 1948525SEric.Schrock@Sun.COM version = spa_version(spa); 1958525SEric.Schrock@Sun.COM if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 1968525SEric.Schrock@Sun.COM src = ZPROP_SRC_DEFAULT; 1978525SEric.Schrock@Sun.COM else 1988525SEric.Schrock@Sun.COM src = ZPROP_SRC_LOCAL; 1998525SEric.Schrock@Sun.COM spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 2008525SEric.Schrock@Sun.COM } 2015949Slling 2025949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 2035949Slling 2045949Slling if (spa->spa_root != NULL) 2055949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2065949Slling 0, ZPROP_SRC_LOCAL); 2075094Slling 2086643Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 2096643Seschrock if (dp->scd_path == NULL) { 2105949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 2116643Seschrock "none", 0, ZPROP_SRC_LOCAL); 2126643Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2135949Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 2146643Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2155363Seschrock } 2165363Seschrock } 2175094Slling } 2185094Slling 2195094Slling /* 2205094Slling * Get zpool property values. 2215094Slling */ 2225094Slling int 2235094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 2245094Slling { 22510922SJeff.Bonwick@Sun.COM objset_t *mos = spa->spa_meta_objset; 2265094Slling zap_cursor_t zc; 2275094Slling zap_attribute_t za; 2285094Slling int err; 2295094Slling 2305949Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2315094Slling 2327754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 2337754SJeff.Bonwick@Sun.COM 2345094Slling /* 2355094Slling * Get properties from the spa config. 2365094Slling */ 2375949Slling spa_prop_get_config(spa, nvp); 2385094Slling 2395094Slling /* If no pool property object, no more prop to get. */ 24011619SGeorge.Wilson@Sun.COM if (mos == NULL || spa->spa_pool_props_object == 0) { 2415094Slling mutex_exit(&spa->spa_props_lock); 2425094Slling return (0); 2435094Slling } 2445094Slling 2455094Slling /* 2465094Slling * Get properties from the MOS pool property object. 2475094Slling */ 2485094Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 2495094Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 2505094Slling zap_cursor_advance(&zc)) { 2515094Slling uint64_t intval = 0; 2525094Slling char *strval = NULL; 2535094Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 2545094Slling zpool_prop_t prop; 2555094Slling 2565094Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 2575094Slling continue; 2585094Slling 2595094Slling switch (za.za_integer_length) { 2605094Slling case 8: 2615094Slling /* integer property */ 2625094Slling if (za.za_first_integer != 2635094Slling zpool_prop_default_numeric(prop)) 2645094Slling src = ZPROP_SRC_LOCAL; 2655094Slling 2665094Slling if (prop == ZPOOL_PROP_BOOTFS) { 2675094Slling dsl_pool_t *dp; 2685094Slling dsl_dataset_t *ds = NULL; 2695094Slling 2705094Slling dp = spa_get_dsl(spa); 2715094Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2726689Smaybee if (err = dsl_dataset_hold_obj(dp, 2736689Smaybee za.za_first_integer, FTAG, &ds)) { 2745094Slling rw_exit(&dp->dp_config_rwlock); 2755094Slling break; 2765094Slling } 2775094Slling 2785094Slling strval = kmem_alloc( 2795094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 2805094Slling KM_SLEEP); 2815094Slling dsl_dataset_name(ds, strval); 2826689Smaybee dsl_dataset_rele(ds, FTAG); 2835094Slling rw_exit(&dp->dp_config_rwlock); 2845094Slling } else { 2855094Slling strval = NULL; 2865094Slling intval = za.za_first_integer; 2875094Slling } 2885094Slling 2895949Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 2905094Slling 2915094Slling if (strval != NULL) 2925094Slling kmem_free(strval, 2935094Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 2945094Slling 2955094Slling break; 2965094Slling 2975094Slling case 1: 2985094Slling /* string property */ 2995094Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 3005094Slling err = zap_lookup(mos, spa->spa_pool_props_object, 3015094Slling za.za_name, 1, za.za_num_integers, strval); 3025094Slling if (err) { 3035094Slling kmem_free(strval, za.za_num_integers); 3045094Slling break; 3055094Slling } 3065949Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 3075094Slling kmem_free(strval, za.za_num_integers); 3085094Slling break; 3095094Slling 3105094Slling default: 3115094Slling break; 3125094Slling } 3135094Slling } 3145094Slling zap_cursor_fini(&zc); 3155094Slling mutex_exit(&spa->spa_props_lock); 3165094Slling out: 3175094Slling if (err && err != ENOENT) { 3185094Slling nvlist_free(*nvp); 3195949Slling *nvp = NULL; 3205094Slling return (err); 3215094Slling } 3225094Slling 3235094Slling return (0); 3245094Slling } 3255094Slling 3265094Slling /* 3275094Slling * Validate the given pool properties nvlist and modify the list 3285094Slling * for the property values to be set. 3295094Slling */ 3305094Slling static int 3315094Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 3325094Slling { 3335094Slling nvpair_t *elem; 3345094Slling int error = 0, reset_bootfs = 0; 3355094Slling uint64_t objnum; 3365094Slling 3375094Slling elem = NULL; 3385094Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 3395094Slling zpool_prop_t prop; 3405094Slling char *propname, *strval; 3415094Slling uint64_t intval; 3425094Slling objset_t *os; 3435363Seschrock char *slash; 3445094Slling 3455094Slling propname = nvpair_name(elem); 3465094Slling 3475094Slling if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 3485094Slling return (EINVAL); 3495094Slling 3505094Slling switch (prop) { 3515094Slling case ZPOOL_PROP_VERSION: 3525094Slling error = nvpair_value_uint64(elem, &intval); 3535094Slling if (!error && 3545094Slling (intval < spa_version(spa) || intval > SPA_VERSION)) 3555094Slling error = EINVAL; 3565094Slling break; 3575094Slling 3585094Slling case ZPOOL_PROP_DELEGATION: 3595094Slling case ZPOOL_PROP_AUTOREPLACE: 3607538SRichard.Morris@Sun.COM case ZPOOL_PROP_LISTSNAPS: 3619816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 3625094Slling error = nvpair_value_uint64(elem, &intval); 3635094Slling if (!error && intval > 1) 3645094Slling error = EINVAL; 3655094Slling break; 3665094Slling 3675094Slling case ZPOOL_PROP_BOOTFS: 3689630SJeff.Bonwick@Sun.COM /* 3699630SJeff.Bonwick@Sun.COM * If the pool version is less than SPA_VERSION_BOOTFS, 3709630SJeff.Bonwick@Sun.COM * or the pool is still being created (version == 0), 3719630SJeff.Bonwick@Sun.COM * the bootfs property cannot be set. 3729630SJeff.Bonwick@Sun.COM */ 3735094Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 3745094Slling error = ENOTSUP; 3755094Slling break; 3765094Slling } 3775094Slling 3785094Slling /* 3797042Sgw25295 * Make sure the vdev config is bootable 3805094Slling */ 3817042Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) { 3825094Slling error = ENOTSUP; 3835094Slling break; 3845094Slling } 3855094Slling 3865094Slling reset_bootfs = 1; 3875094Slling 3885094Slling error = nvpair_value_string(elem, &strval); 3895094Slling 3905094Slling if (!error) { 3917042Sgw25295 uint64_t compress; 3927042Sgw25295 3935094Slling if (strval == NULL || strval[0] == '\0') { 3945094Slling objnum = zpool_prop_default_numeric( 3955094Slling ZPOOL_PROP_BOOTFS); 3965094Slling break; 3975094Slling } 3985094Slling 39910298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(strval, FTAG, &os)) 4005094Slling break; 4017042Sgw25295 40210298SMatthew.Ahrens@Sun.COM /* Must be ZPL and not gzip compressed. */ 40310298SMatthew.Ahrens@Sun.COM 40410298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 40510298SMatthew.Ahrens@Sun.COM error = ENOTSUP; 40610298SMatthew.Ahrens@Sun.COM } else if ((error = dsl_prop_get_integer(strval, 4077042Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 4087042Sgw25295 &compress, NULL)) == 0 && 4097042Sgw25295 !BOOTFS_COMPRESS_VALID(compress)) { 4107042Sgw25295 error = ENOTSUP; 4117042Sgw25295 } else { 4127042Sgw25295 objnum = dmu_objset_id(os); 4137042Sgw25295 } 41410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 4155094Slling } 4165094Slling break; 4177754SJeff.Bonwick@Sun.COM 4185329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 4195329Sgw25295 error = nvpair_value_uint64(elem, &intval); 4205329Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 4215329Sgw25295 intval > ZIO_FAILURE_MODE_PANIC)) 4225329Sgw25295 error = EINVAL; 4235329Sgw25295 4245329Sgw25295 /* 4255329Sgw25295 * This is a special case which only occurs when 4265329Sgw25295 * the pool has completely failed. This allows 4275329Sgw25295 * the user to change the in-core failmode property 4285329Sgw25295 * without syncing it out to disk (I/Os might 4295329Sgw25295 * currently be blocked). We do this by returning 4305329Sgw25295 * EIO to the caller (spa_prop_set) to trick it 4315329Sgw25295 * into thinking we encountered a property validation 4325329Sgw25295 * error. 4335329Sgw25295 */ 4347754SJeff.Bonwick@Sun.COM if (!error && spa_suspended(spa)) { 4355329Sgw25295 spa->spa_failmode = intval; 4365329Sgw25295 error = EIO; 4375329Sgw25295 } 4385329Sgw25295 break; 4395363Seschrock 4405363Seschrock case ZPOOL_PROP_CACHEFILE: 4415363Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 4425363Seschrock break; 4435363Seschrock 4445363Seschrock if (strval[0] == '\0') 4455363Seschrock break; 4465363Seschrock 4475363Seschrock if (strcmp(strval, "none") == 0) 4485363Seschrock break; 4495363Seschrock 4505363Seschrock if (strval[0] != '/') { 4515363Seschrock error = EINVAL; 4525363Seschrock break; 4535363Seschrock } 4545363Seschrock 4555363Seschrock slash = strrchr(strval, '/'); 4565363Seschrock ASSERT(slash != NULL); 4575363Seschrock 4585363Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 4595363Seschrock strcmp(slash, "/..") == 0) 4605363Seschrock error = EINVAL; 4615363Seschrock break; 46210922SJeff.Bonwick@Sun.COM 46310922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 46410922SJeff.Bonwick@Sun.COM if (spa_version(spa) < SPA_VERSION_DEDUP) 46510922SJeff.Bonwick@Sun.COM error = ENOTSUP; 46610922SJeff.Bonwick@Sun.COM else 46710922SJeff.Bonwick@Sun.COM error = nvpair_value_uint64(elem, &intval); 46810922SJeff.Bonwick@Sun.COM if (error == 0 && 46910922SJeff.Bonwick@Sun.COM intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 47010922SJeff.Bonwick@Sun.COM error = EINVAL; 47110922SJeff.Bonwick@Sun.COM break; 4725094Slling } 4735094Slling 4745094Slling if (error) 4755094Slling break; 4765094Slling } 4775094Slling 4785094Slling if (!error && reset_bootfs) { 4795094Slling error = nvlist_remove(props, 4805094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 4815094Slling 4825094Slling if (!error) { 4835094Slling error = nvlist_add_uint64(props, 4845094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 4855094Slling } 4865094Slling } 4875094Slling 4885094Slling return (error); 4895094Slling } 4905094Slling 4918525SEric.Schrock@Sun.COM void 4928525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 4938525SEric.Schrock@Sun.COM { 4948525SEric.Schrock@Sun.COM char *cachefile; 4958525SEric.Schrock@Sun.COM spa_config_dirent_t *dp; 4968525SEric.Schrock@Sun.COM 4978525SEric.Schrock@Sun.COM if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 4988525SEric.Schrock@Sun.COM &cachefile) != 0) 4998525SEric.Schrock@Sun.COM return; 5008525SEric.Schrock@Sun.COM 5018525SEric.Schrock@Sun.COM dp = kmem_alloc(sizeof (spa_config_dirent_t), 5028525SEric.Schrock@Sun.COM KM_SLEEP); 5038525SEric.Schrock@Sun.COM 5048525SEric.Schrock@Sun.COM if (cachefile[0] == '\0') 5058525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(spa_config_path); 5068525SEric.Schrock@Sun.COM else if (strcmp(cachefile, "none") == 0) 5078525SEric.Schrock@Sun.COM dp->scd_path = NULL; 5088525SEric.Schrock@Sun.COM else 5098525SEric.Schrock@Sun.COM dp->scd_path = spa_strdup(cachefile); 5108525SEric.Schrock@Sun.COM 5118525SEric.Schrock@Sun.COM list_insert_head(&spa->spa_config_list, dp); 5128525SEric.Schrock@Sun.COM if (need_sync) 5138525SEric.Schrock@Sun.COM spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 5148525SEric.Schrock@Sun.COM } 5158525SEric.Schrock@Sun.COM 5165094Slling int 5175094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 5185094Slling { 5195094Slling int error; 5208525SEric.Schrock@Sun.COM nvpair_t *elem; 5218525SEric.Schrock@Sun.COM boolean_t need_sync = B_FALSE; 5228525SEric.Schrock@Sun.COM zpool_prop_t prop; 5235094Slling 5245094Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 5255094Slling return (error); 5265094Slling 5278525SEric.Schrock@Sun.COM elem = NULL; 5288525SEric.Schrock@Sun.COM while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 5298525SEric.Schrock@Sun.COM if ((prop = zpool_name_to_prop( 5308525SEric.Schrock@Sun.COM nvpair_name(elem))) == ZPROP_INVAL) 5318525SEric.Schrock@Sun.COM return (EINVAL); 5328525SEric.Schrock@Sun.COM 5338525SEric.Schrock@Sun.COM if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 5348525SEric.Schrock@Sun.COM continue; 5358525SEric.Schrock@Sun.COM 5368525SEric.Schrock@Sun.COM need_sync = B_TRUE; 5378525SEric.Schrock@Sun.COM break; 5388525SEric.Schrock@Sun.COM } 5398525SEric.Schrock@Sun.COM 5408525SEric.Schrock@Sun.COM if (need_sync) 5418525SEric.Schrock@Sun.COM return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 5428525SEric.Schrock@Sun.COM spa, nvp, 3)); 5438525SEric.Schrock@Sun.COM else 5448525SEric.Schrock@Sun.COM return (0); 5455094Slling } 5465094Slling 5475094Slling /* 5485094Slling * If the bootfs property value is dsobj, clear it. 5495094Slling */ 5505094Slling void 5515094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 5525094Slling { 5535094Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 5545094Slling VERIFY(zap_remove(spa->spa_meta_objset, 5555094Slling spa->spa_pool_props_object, 5565094Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 5575094Slling spa->spa_bootfs = 0; 5585094Slling } 5595094Slling } 5605094Slling 561789Sahrens /* 562789Sahrens * ========================================================================== 563789Sahrens * SPA state manipulation (open/create/destroy/import/export) 564789Sahrens * ========================================================================== 565789Sahrens */ 566789Sahrens 5671544Seschrock static int 5681544Seschrock spa_error_entry_compare(const void *a, const void *b) 5691544Seschrock { 5701544Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 5711544Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 5721544Seschrock int ret; 5731544Seschrock 5741544Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 5751544Seschrock sizeof (zbookmark_t)); 5761544Seschrock 5771544Seschrock if (ret < 0) 5781544Seschrock return (-1); 5791544Seschrock else if (ret > 0) 5801544Seschrock return (1); 5811544Seschrock else 5821544Seschrock return (0); 5831544Seschrock } 5841544Seschrock 5851544Seschrock /* 5861544Seschrock * Utility function which retrieves copies of the current logs and 5871544Seschrock * re-initializes them in the process. 5881544Seschrock */ 5891544Seschrock void 5901544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 5911544Seschrock { 5921544Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 5931544Seschrock 5941544Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 5951544Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 5961544Seschrock 5971544Seschrock avl_create(&spa->spa_errlist_scrub, 5981544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 5991544Seschrock offsetof(spa_error_entry_t, se_avl)); 6001544Seschrock avl_create(&spa->spa_errlist_last, 6011544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 6021544Seschrock offsetof(spa_error_entry_t, se_avl)); 6031544Seschrock } 6041544Seschrock 60511173SJonathan.Adams@Sun.COM static taskq_t * 60611173SJonathan.Adams@Sun.COM spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode, 60711173SJonathan.Adams@Sun.COM uint_t value) 60811173SJonathan.Adams@Sun.COM { 60911173SJonathan.Adams@Sun.COM uint_t flags = TASKQ_PREPOPULATE; 61011173SJonathan.Adams@Sun.COM boolean_t batch = B_FALSE; 61111173SJonathan.Adams@Sun.COM 61211173SJonathan.Adams@Sun.COM switch (mode) { 61311173SJonathan.Adams@Sun.COM case zti_mode_null: 61411173SJonathan.Adams@Sun.COM return (NULL); /* no taskq needed */ 61511173SJonathan.Adams@Sun.COM 61611173SJonathan.Adams@Sun.COM case zti_mode_fixed: 61711173SJonathan.Adams@Sun.COM ASSERT3U(value, >=, 1); 61811173SJonathan.Adams@Sun.COM value = MAX(value, 1); 61911173SJonathan.Adams@Sun.COM break; 62011173SJonathan.Adams@Sun.COM 62111173SJonathan.Adams@Sun.COM case zti_mode_batch: 62211173SJonathan.Adams@Sun.COM batch = B_TRUE; 62311173SJonathan.Adams@Sun.COM flags |= TASKQ_THREADS_CPU_PCT; 62411173SJonathan.Adams@Sun.COM value = zio_taskq_batch_pct; 62511173SJonathan.Adams@Sun.COM break; 62611173SJonathan.Adams@Sun.COM 62711173SJonathan.Adams@Sun.COM case zti_mode_online_percent: 62811173SJonathan.Adams@Sun.COM flags |= TASKQ_THREADS_CPU_PCT; 62911173SJonathan.Adams@Sun.COM break; 63011173SJonathan.Adams@Sun.COM 63111173SJonathan.Adams@Sun.COM default: 63211173SJonathan.Adams@Sun.COM panic("unrecognized mode for %s taskq (%u:%u) in " 63311173SJonathan.Adams@Sun.COM "spa_activate()", 63411173SJonathan.Adams@Sun.COM name, mode, value); 63511173SJonathan.Adams@Sun.COM break; 63611173SJonathan.Adams@Sun.COM } 63711173SJonathan.Adams@Sun.COM 63811173SJonathan.Adams@Sun.COM if (zio_taskq_sysdc && spa->spa_proc != &p0) { 63911173SJonathan.Adams@Sun.COM if (batch) 64011173SJonathan.Adams@Sun.COM flags |= TASKQ_DC_BATCH; 64111173SJonathan.Adams@Sun.COM 64211173SJonathan.Adams@Sun.COM return (taskq_create_sysdc(name, value, 50, INT_MAX, 64311173SJonathan.Adams@Sun.COM spa->spa_proc, zio_taskq_basedc, flags)); 64411173SJonathan.Adams@Sun.COM } 64511173SJonathan.Adams@Sun.COM return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX, 64611173SJonathan.Adams@Sun.COM spa->spa_proc, flags)); 64711173SJonathan.Adams@Sun.COM } 64811173SJonathan.Adams@Sun.COM 64911173SJonathan.Adams@Sun.COM static void 65011173SJonathan.Adams@Sun.COM spa_create_zio_taskqs(spa_t *spa) 65111173SJonathan.Adams@Sun.COM { 65211173SJonathan.Adams@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 65311173SJonathan.Adams@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 65411173SJonathan.Adams@Sun.COM const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 65511173SJonathan.Adams@Sun.COM enum zti_modes mode = ztip->zti_mode; 65611173SJonathan.Adams@Sun.COM uint_t value = ztip->zti_value; 65711173SJonathan.Adams@Sun.COM char name[32]; 65811173SJonathan.Adams@Sun.COM 65911173SJonathan.Adams@Sun.COM (void) snprintf(name, sizeof (name), 66011173SJonathan.Adams@Sun.COM "%s_%s", zio_type_name[t], zio_taskq_types[q]); 66111173SJonathan.Adams@Sun.COM 66211173SJonathan.Adams@Sun.COM spa->spa_zio_taskq[t][q] = 66311173SJonathan.Adams@Sun.COM spa_taskq_create(spa, name, mode, value); 66411173SJonathan.Adams@Sun.COM } 66511173SJonathan.Adams@Sun.COM } 66611173SJonathan.Adams@Sun.COM } 66711173SJonathan.Adams@Sun.COM 66811173SJonathan.Adams@Sun.COM #ifdef _KERNEL 66911173SJonathan.Adams@Sun.COM static void 67011173SJonathan.Adams@Sun.COM spa_thread(void *arg) 67111173SJonathan.Adams@Sun.COM { 67211173SJonathan.Adams@Sun.COM callb_cpr_t cprinfo; 67311173SJonathan.Adams@Sun.COM 67411173SJonathan.Adams@Sun.COM spa_t *spa = arg; 67511173SJonathan.Adams@Sun.COM user_t *pu = PTOU(curproc); 67611173SJonathan.Adams@Sun.COM 67711173SJonathan.Adams@Sun.COM CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 67811173SJonathan.Adams@Sun.COM spa->spa_name); 67911173SJonathan.Adams@Sun.COM 68011173SJonathan.Adams@Sun.COM ASSERT(curproc != &p0); 68111173SJonathan.Adams@Sun.COM (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 68211173SJonathan.Adams@Sun.COM "zpool-%s", spa->spa_name); 68311173SJonathan.Adams@Sun.COM (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 68411173SJonathan.Adams@Sun.COM 68511173SJonathan.Adams@Sun.COM /* bind this thread to the requested psrset */ 68611173SJonathan.Adams@Sun.COM if (zio_taskq_psrset_bind != PS_NONE) { 68711173SJonathan.Adams@Sun.COM pool_lock(); 68811173SJonathan.Adams@Sun.COM mutex_enter(&cpu_lock); 68911173SJonathan.Adams@Sun.COM mutex_enter(&pidlock); 69011173SJonathan.Adams@Sun.COM mutex_enter(&curproc->p_lock); 69111173SJonathan.Adams@Sun.COM 69211173SJonathan.Adams@Sun.COM if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 69311173SJonathan.Adams@Sun.COM 0, NULL, NULL) == 0) { 69411173SJonathan.Adams@Sun.COM curthread->t_bind_pset = zio_taskq_psrset_bind; 69511173SJonathan.Adams@Sun.COM } else { 69611173SJonathan.Adams@Sun.COM cmn_err(CE_WARN, 69711173SJonathan.Adams@Sun.COM "Couldn't bind process for zfs pool \"%s\" to " 69811173SJonathan.Adams@Sun.COM "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 69911173SJonathan.Adams@Sun.COM } 70011173SJonathan.Adams@Sun.COM 70111173SJonathan.Adams@Sun.COM mutex_exit(&curproc->p_lock); 70211173SJonathan.Adams@Sun.COM mutex_exit(&pidlock); 70311173SJonathan.Adams@Sun.COM mutex_exit(&cpu_lock); 70411173SJonathan.Adams@Sun.COM pool_unlock(); 70511173SJonathan.Adams@Sun.COM } 70611173SJonathan.Adams@Sun.COM 70711173SJonathan.Adams@Sun.COM if (zio_taskq_sysdc) { 70811173SJonathan.Adams@Sun.COM sysdc_thread_enter(curthread, 100, 0); 70911173SJonathan.Adams@Sun.COM } 71011173SJonathan.Adams@Sun.COM 71111173SJonathan.Adams@Sun.COM spa->spa_proc = curproc; 71211173SJonathan.Adams@Sun.COM spa->spa_did = curthread->t_did; 71311173SJonathan.Adams@Sun.COM 71411173SJonathan.Adams@Sun.COM spa_create_zio_taskqs(spa); 71511173SJonathan.Adams@Sun.COM 71611173SJonathan.Adams@Sun.COM mutex_enter(&spa->spa_proc_lock); 71711173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 71811173SJonathan.Adams@Sun.COM 71911173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_ACTIVE; 72011173SJonathan.Adams@Sun.COM cv_broadcast(&spa->spa_proc_cv); 72111173SJonathan.Adams@Sun.COM 72211173SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_BEGIN(&cprinfo); 72311173SJonathan.Adams@Sun.COM while (spa->spa_proc_state == SPA_PROC_ACTIVE) 72411173SJonathan.Adams@Sun.COM cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 72511173SJonathan.Adams@Sun.COM CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 72611173SJonathan.Adams@Sun.COM 72711173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 72811173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_GONE; 72911173SJonathan.Adams@Sun.COM spa->spa_proc = &p0; 73011173SJonathan.Adams@Sun.COM cv_broadcast(&spa->spa_proc_cv); 73111173SJonathan.Adams@Sun.COM CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 73211173SJonathan.Adams@Sun.COM 73311173SJonathan.Adams@Sun.COM mutex_enter(&curproc->p_lock); 73411173SJonathan.Adams@Sun.COM lwp_exit(); 73511173SJonathan.Adams@Sun.COM } 73611173SJonathan.Adams@Sun.COM #endif 73711173SJonathan.Adams@Sun.COM 738789Sahrens /* 739789Sahrens * Activate an uninitialized pool. 740789Sahrens */ 741789Sahrens static void 7428241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode) 743789Sahrens { 744789Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 745789Sahrens 746789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 7478241SJeff.Bonwick@Sun.COM spa->spa_mode = mode; 748789Sahrens 74910594SGeorge.Wilson@Sun.COM spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 75010594SGeorge.Wilson@Sun.COM spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 751789Sahrens 75211173SJonathan.Adams@Sun.COM /* Try to create a covering process */ 75311173SJonathan.Adams@Sun.COM mutex_enter(&spa->spa_proc_lock); 75411173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 75511173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc == &p0); 75611173SJonathan.Adams@Sun.COM spa->spa_did = 0; 75711173SJonathan.Adams@Sun.COM 75811173SJonathan.Adams@Sun.COM /* Only create a process if we're going to be around a while. */ 75911173SJonathan.Adams@Sun.COM if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 76011173SJonathan.Adams@Sun.COM if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 76111173SJonathan.Adams@Sun.COM NULL, 0) == 0) { 76211173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_CREATED; 76311173SJonathan.Adams@Sun.COM while (spa->spa_proc_state == SPA_PROC_CREATED) { 76411173SJonathan.Adams@Sun.COM cv_wait(&spa->spa_proc_cv, 76511173SJonathan.Adams@Sun.COM &spa->spa_proc_lock); 7669515SJonathan.Adams@Sun.COM } 76711173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 76811173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc != &p0); 76911173SJonathan.Adams@Sun.COM ASSERT(spa->spa_did != 0); 77011173SJonathan.Adams@Sun.COM } else { 77111173SJonathan.Adams@Sun.COM #ifdef _KERNEL 77211173SJonathan.Adams@Sun.COM cmn_err(CE_WARN, 77311173SJonathan.Adams@Sun.COM "Couldn't create process for zfs pool \"%s\"\n", 77411173SJonathan.Adams@Sun.COM spa->spa_name); 77511173SJonathan.Adams@Sun.COM #endif 7767754SJeff.Bonwick@Sun.COM } 777789Sahrens } 77811173SJonathan.Adams@Sun.COM mutex_exit(&spa->spa_proc_lock); 77911173SJonathan.Adams@Sun.COM 78011173SJonathan.Adams@Sun.COM /* If we didn't create a process, we need to create our taskqs. */ 78111173SJonathan.Adams@Sun.COM if (spa->spa_proc == &p0) { 78211173SJonathan.Adams@Sun.COM spa_create_zio_taskqs(spa); 78311173SJonathan.Adams@Sun.COM } 784789Sahrens 7857754SJeff.Bonwick@Sun.COM list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 7867754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_config_dirty_node)); 7877754SJeff.Bonwick@Sun.COM list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 7887754SJeff.Bonwick@Sun.COM offsetof(vdev_t, vdev_state_dirty_node)); 789789Sahrens 790789Sahrens txg_list_create(&spa->spa_vdev_txg_list, 791789Sahrens offsetof(struct vdev, vdev_txg_node)); 7921544Seschrock 7931544Seschrock avl_create(&spa->spa_errlist_scrub, 7941544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 7951544Seschrock offsetof(spa_error_entry_t, se_avl)); 7961544Seschrock avl_create(&spa->spa_errlist_last, 7971544Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 7981544Seschrock offsetof(spa_error_entry_t, se_avl)); 799789Sahrens } 800789Sahrens 801789Sahrens /* 802789Sahrens * Opposite of spa_activate(). 803789Sahrens */ 804789Sahrens static void 805789Sahrens spa_deactivate(spa_t *spa) 806789Sahrens { 807789Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 808789Sahrens ASSERT(spa->spa_dsl_pool == NULL); 809789Sahrens ASSERT(spa->spa_root_vdev == NULL); 8109630SJeff.Bonwick@Sun.COM ASSERT(spa->spa_async_zio_root == NULL); 811789Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 812789Sahrens 813789Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 814789Sahrens 8157754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_config_dirty_list); 8167754SJeff.Bonwick@Sun.COM list_destroy(&spa->spa_state_dirty_list); 8177754SJeff.Bonwick@Sun.COM 8187754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 8197754SJeff.Bonwick@Sun.COM for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 82011146SGeorge.Wilson@Sun.COM if (spa->spa_zio_taskq[t][q] != NULL) 82111146SGeorge.Wilson@Sun.COM taskq_destroy(spa->spa_zio_taskq[t][q]); 8227754SJeff.Bonwick@Sun.COM spa->spa_zio_taskq[t][q] = NULL; 8237754SJeff.Bonwick@Sun.COM } 824789Sahrens } 825789Sahrens 826789Sahrens metaslab_class_destroy(spa->spa_normal_class); 827789Sahrens spa->spa_normal_class = NULL; 828789Sahrens 8294527Sperrin metaslab_class_destroy(spa->spa_log_class); 8304527Sperrin spa->spa_log_class = NULL; 8314527Sperrin 8321544Seschrock /* 8331544Seschrock * If this was part of an import or the open otherwise failed, we may 8341544Seschrock * still have errors left in the queues. Empty them just in case. 8351544Seschrock */ 8361544Seschrock spa_errlog_drain(spa); 8371544Seschrock 8381544Seschrock avl_destroy(&spa->spa_errlist_scrub); 8391544Seschrock avl_destroy(&spa->spa_errlist_last); 8401544Seschrock 841789Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 84211173SJonathan.Adams@Sun.COM 84311173SJonathan.Adams@Sun.COM mutex_enter(&spa->spa_proc_lock); 84411173SJonathan.Adams@Sun.COM if (spa->spa_proc_state != SPA_PROC_NONE) { 84511173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 84611173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_DEACTIVATE; 84711173SJonathan.Adams@Sun.COM cv_broadcast(&spa->spa_proc_cv); 84811173SJonathan.Adams@Sun.COM while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 84911173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc != &p0); 85011173SJonathan.Adams@Sun.COM cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 85111173SJonathan.Adams@Sun.COM } 85211173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 85311173SJonathan.Adams@Sun.COM spa->spa_proc_state = SPA_PROC_NONE; 85411173SJonathan.Adams@Sun.COM } 85511173SJonathan.Adams@Sun.COM ASSERT(spa->spa_proc == &p0); 85611173SJonathan.Adams@Sun.COM mutex_exit(&spa->spa_proc_lock); 85711173SJonathan.Adams@Sun.COM 85811173SJonathan.Adams@Sun.COM /* 85911173SJonathan.Adams@Sun.COM * We want to make sure spa_thread() has actually exited the ZFS 86011173SJonathan.Adams@Sun.COM * module, so that the module can't be unloaded out from underneath 86111173SJonathan.Adams@Sun.COM * it. 86211173SJonathan.Adams@Sun.COM */ 86311173SJonathan.Adams@Sun.COM if (spa->spa_did != 0) { 86411173SJonathan.Adams@Sun.COM thread_join(spa->spa_did); 86511173SJonathan.Adams@Sun.COM spa->spa_did = 0; 86611173SJonathan.Adams@Sun.COM } 867789Sahrens } 868789Sahrens 869789Sahrens /* 870789Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 871789Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 872789Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 873789Sahrens * All vdev validation is done by the vdev_alloc() routine. 874789Sahrens */ 8752082Seschrock static int 8762082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 8772082Seschrock uint_t id, int atype) 878789Sahrens { 879789Sahrens nvlist_t **child; 8809816SGeorge.Wilson@Sun.COM uint_t children; 8812082Seschrock int error; 8822082Seschrock 8832082Seschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 8842082Seschrock return (error); 8852082Seschrock 8862082Seschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 8872082Seschrock return (0); 888789Sahrens 8897754SJeff.Bonwick@Sun.COM error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 8907754SJeff.Bonwick@Sun.COM &child, &children); 8917754SJeff.Bonwick@Sun.COM 8927754SJeff.Bonwick@Sun.COM if (error == ENOENT) 8937754SJeff.Bonwick@Sun.COM return (0); 8947754SJeff.Bonwick@Sun.COM 8957754SJeff.Bonwick@Sun.COM if (error) { 8962082Seschrock vdev_free(*vdp); 8972082Seschrock *vdp = NULL; 8982082Seschrock return (EINVAL); 899789Sahrens } 900789Sahrens 9019816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 9022082Seschrock vdev_t *vd; 9032082Seschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 9042082Seschrock atype)) != 0) { 9052082Seschrock vdev_free(*vdp); 9062082Seschrock *vdp = NULL; 9072082Seschrock return (error); 908789Sahrens } 909789Sahrens } 910789Sahrens 9112082Seschrock ASSERT(*vdp != NULL); 9122082Seschrock 9132082Seschrock return (0); 914789Sahrens } 915789Sahrens 916789Sahrens /* 917789Sahrens * Opposite of spa_load(). 918789Sahrens */ 919789Sahrens static void 920789Sahrens spa_unload(spa_t *spa) 921789Sahrens { 9222082Seschrock int i; 9232082Seschrock 9247754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 9257754SJeff.Bonwick@Sun.COM 926789Sahrens /* 9271544Seschrock * Stop async tasks. 9281544Seschrock */ 9291544Seschrock spa_async_suspend(spa); 9301544Seschrock 9311544Seschrock /* 932789Sahrens * Stop syncing. 933789Sahrens */ 934789Sahrens if (spa->spa_sync_on) { 935789Sahrens txg_sync_stop(spa->spa_dsl_pool); 936789Sahrens spa->spa_sync_on = B_FALSE; 937789Sahrens } 938789Sahrens 939789Sahrens /* 9407754SJeff.Bonwick@Sun.COM * Wait for any outstanding async I/O to complete. 941789Sahrens */ 9429234SGeorge.Wilson@Sun.COM if (spa->spa_async_zio_root != NULL) { 9439234SGeorge.Wilson@Sun.COM (void) zio_wait(spa->spa_async_zio_root); 9449234SGeorge.Wilson@Sun.COM spa->spa_async_zio_root = NULL; 9459234SGeorge.Wilson@Sun.COM } 946789Sahrens 94712470SMatthew.Ahrens@Sun.COM bpobj_close(&spa->spa_deferred_bpobj); 94812470SMatthew.Ahrens@Sun.COM 949789Sahrens /* 950789Sahrens * Close the dsl pool. 951789Sahrens */ 952789Sahrens if (spa->spa_dsl_pool) { 953789Sahrens dsl_pool_close(spa->spa_dsl_pool); 954789Sahrens spa->spa_dsl_pool = NULL; 95511619SGeorge.Wilson@Sun.COM spa->spa_meta_objset = NULL; 956789Sahrens } 957789Sahrens 95810922SJeff.Bonwick@Sun.COM ddt_unload(spa); 95910922SJeff.Bonwick@Sun.COM 9608241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 9618241SJeff.Bonwick@Sun.COM 9628241SJeff.Bonwick@Sun.COM /* 9638241SJeff.Bonwick@Sun.COM * Drop and purge level 2 cache 9648241SJeff.Bonwick@Sun.COM */ 9658241SJeff.Bonwick@Sun.COM spa_l2cache_drop(spa); 9668241SJeff.Bonwick@Sun.COM 967789Sahrens /* 968789Sahrens * Close all vdevs. 969789Sahrens */ 9701585Sbonwick if (spa->spa_root_vdev) 971789Sahrens vdev_free(spa->spa_root_vdev); 9721585Sbonwick ASSERT(spa->spa_root_vdev == NULL); 9731544Seschrock 9745450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 9755450Sbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 9765450Sbrendan if (spa->spa_spares.sav_vdevs) { 9775450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 9785450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 9795450Sbrendan spa->spa_spares.sav_vdevs = NULL; 9805450Sbrendan } 9815450Sbrendan if (spa->spa_spares.sav_config) { 9825450Sbrendan nvlist_free(spa->spa_spares.sav_config); 9835450Sbrendan spa->spa_spares.sav_config = NULL; 9842082Seschrock } 9857377SEric.Schrock@Sun.COM spa->spa_spares.sav_count = 0; 9865450Sbrendan 9875450Sbrendan for (i = 0; i < spa->spa_l2cache.sav_count; i++) 9885450Sbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 9895450Sbrendan if (spa->spa_l2cache.sav_vdevs) { 9905450Sbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 9915450Sbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 9925450Sbrendan spa->spa_l2cache.sav_vdevs = NULL; 9935450Sbrendan } 9945450Sbrendan if (spa->spa_l2cache.sav_config) { 9955450Sbrendan nvlist_free(spa->spa_l2cache.sav_config); 9965450Sbrendan spa->spa_l2cache.sav_config = NULL; 9972082Seschrock } 9987377SEric.Schrock@Sun.COM spa->spa_l2cache.sav_count = 0; 9992082Seschrock 10001544Seschrock spa->spa_async_suspended = 0; 10018241SJeff.Bonwick@Sun.COM 10028241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1003789Sahrens } 1004789Sahrens 1005789Sahrens /* 10062082Seschrock * Load (or re-load) the current list of vdevs describing the active spares for 10072082Seschrock * this pool. When this is called, we have some form of basic information in 10085450Sbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 10095450Sbrendan * then re-generate a more complete list including status information. 10102082Seschrock */ 10112082Seschrock static void 10122082Seschrock spa_load_spares(spa_t *spa) 10132082Seschrock { 10142082Seschrock nvlist_t **spares; 10152082Seschrock uint_t nspares; 10162082Seschrock int i; 10173377Seschrock vdev_t *vd, *tvd; 10182082Seschrock 10197754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 10207754SJeff.Bonwick@Sun.COM 10212082Seschrock /* 10222082Seschrock * First, close and free any existing spare vdevs. 10232082Seschrock */ 10245450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 10255450Sbrendan vd = spa->spa_spares.sav_vdevs[i]; 10263377Seschrock 10273377Seschrock /* Undo the call to spa_activate() below */ 10286643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 10296643Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 10303377Seschrock spa_spare_remove(tvd); 10313377Seschrock vdev_close(vd); 10323377Seschrock vdev_free(vd); 10332082Seschrock } 10343377Seschrock 10355450Sbrendan if (spa->spa_spares.sav_vdevs) 10365450Sbrendan kmem_free(spa->spa_spares.sav_vdevs, 10375450Sbrendan spa->spa_spares.sav_count * sizeof (void *)); 10385450Sbrendan 10395450Sbrendan if (spa->spa_spares.sav_config == NULL) 10402082Seschrock nspares = 0; 10412082Seschrock else 10425450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 10432082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 10442082Seschrock 10455450Sbrendan spa->spa_spares.sav_count = (int)nspares; 10465450Sbrendan spa->spa_spares.sav_vdevs = NULL; 10472082Seschrock 10482082Seschrock if (nspares == 0) 10492082Seschrock return; 10502082Seschrock 10512082Seschrock /* 10522082Seschrock * Construct the array of vdevs, opening them to get status in the 10533377Seschrock * process. For each spare, there is potentially two different vdev_t 10543377Seschrock * structures associated with it: one in the list of spares (used only 10553377Seschrock * for basic validation purposes) and one in the active vdev 10563377Seschrock * configuration (if it's spared in). During this phase we open and 10573377Seschrock * validate each vdev on the spare list. If the vdev also exists in the 10583377Seschrock * active configuration, then we also mark this vdev as an active spare. 10592082Seschrock */ 10605450Sbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 10615450Sbrendan KM_SLEEP); 10625450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 10632082Seschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 10642082Seschrock VDEV_ALLOC_SPARE) == 0); 10652082Seschrock ASSERT(vd != NULL); 10662082Seschrock 10675450Sbrendan spa->spa_spares.sav_vdevs[i] = vd; 10682082Seschrock 10696643Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 10706643Seschrock B_FALSE)) != NULL) { 10713377Seschrock if (!tvd->vdev_isspare) 10723377Seschrock spa_spare_add(tvd); 10733377Seschrock 10743377Seschrock /* 10753377Seschrock * We only mark the spare active if we were successfully 10763377Seschrock * able to load the vdev. Otherwise, importing a pool 10773377Seschrock * with a bad active spare would result in strange 10783377Seschrock * behavior, because multiple pool would think the spare 10793377Seschrock * is actively in use. 10803377Seschrock * 10813377Seschrock * There is a vulnerability here to an equally bizarre 10823377Seschrock * circumstance, where a dead active spare is later 10833377Seschrock * brought back to life (onlined or otherwise). Given 10843377Seschrock * the rarity of this scenario, and the extra complexity 10853377Seschrock * it adds, we ignore the possibility. 10863377Seschrock */ 10873377Seschrock if (!vdev_is_dead(tvd)) 10883377Seschrock spa_spare_activate(tvd); 10893377Seschrock } 10903377Seschrock 10917754SJeff.Bonwick@Sun.COM vd->vdev_top = vd; 10929425SEric.Schrock@Sun.COM vd->vdev_aux = &spa->spa_spares; 10937754SJeff.Bonwick@Sun.COM 10942082Seschrock if (vdev_open(vd) != 0) 10952082Seschrock continue; 10962082Seschrock 10975450Sbrendan if (vdev_validate_aux(vd) == 0) 10985450Sbrendan spa_spare_add(vd); 10992082Seschrock } 11002082Seschrock 11012082Seschrock /* 11022082Seschrock * Recompute the stashed list of spares, with status information 11032082Seschrock * this time. 11042082Seschrock */ 11055450Sbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 11062082Seschrock DATA_TYPE_NVLIST_ARRAY) == 0); 11072082Seschrock 11085450Sbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 11095450Sbrendan KM_SLEEP); 11105450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 11115450Sbrendan spares[i] = vdev_config_generate(spa, 111212296SLin.Ling@Sun.COM spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 11135450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 11145450Sbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 11155450Sbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 11162082Seschrock nvlist_free(spares[i]); 11175450Sbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 11185450Sbrendan } 11195450Sbrendan 11205450Sbrendan /* 11215450Sbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 11225450Sbrendan * this pool. When this is called, we have some form of basic information in 11235450Sbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 11245450Sbrendan * then re-generate a more complete list including status information. 11255450Sbrendan * Devices which are already active have their details maintained, and are 11265450Sbrendan * not re-opened. 11275450Sbrendan */ 11285450Sbrendan static void 11295450Sbrendan spa_load_l2cache(spa_t *spa) 11305450Sbrendan { 11315450Sbrendan nvlist_t **l2cache; 11325450Sbrendan uint_t nl2cache; 11335450Sbrendan int i, j, oldnvdevs; 11349816SGeorge.Wilson@Sun.COM uint64_t guid; 11355450Sbrendan vdev_t *vd, **oldvdevs, **newvdevs; 11365450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 11375450Sbrendan 11387754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 11397754SJeff.Bonwick@Sun.COM 11405450Sbrendan if (sav->sav_config != NULL) { 11415450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 11425450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 11435450Sbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 11445450Sbrendan } else { 11455450Sbrendan nl2cache = 0; 11465450Sbrendan } 11475450Sbrendan 11485450Sbrendan oldvdevs = sav->sav_vdevs; 11495450Sbrendan oldnvdevs = sav->sav_count; 11505450Sbrendan sav->sav_vdevs = NULL; 11515450Sbrendan sav->sav_count = 0; 11525450Sbrendan 11535450Sbrendan /* 11545450Sbrendan * Process new nvlist of vdevs. 11555450Sbrendan */ 11565450Sbrendan for (i = 0; i < nl2cache; i++) { 11575450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 11585450Sbrendan &guid) == 0); 11595450Sbrendan 11605450Sbrendan newvdevs[i] = NULL; 11615450Sbrendan for (j = 0; j < oldnvdevs; j++) { 11625450Sbrendan vd = oldvdevs[j]; 11635450Sbrendan if (vd != NULL && guid == vd->vdev_guid) { 11645450Sbrendan /* 11655450Sbrendan * Retain previous vdev for add/remove ops. 11665450Sbrendan */ 11675450Sbrendan newvdevs[i] = vd; 11685450Sbrendan oldvdevs[j] = NULL; 11695450Sbrendan break; 11705450Sbrendan } 11715450Sbrendan } 11725450Sbrendan 11735450Sbrendan if (newvdevs[i] == NULL) { 11745450Sbrendan /* 11755450Sbrendan * Create new vdev 11765450Sbrendan */ 11775450Sbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 11785450Sbrendan VDEV_ALLOC_L2CACHE) == 0); 11795450Sbrendan ASSERT(vd != NULL); 11805450Sbrendan newvdevs[i] = vd; 11815450Sbrendan 11825450Sbrendan /* 11835450Sbrendan * Commit this vdev as an l2cache device, 11845450Sbrendan * even if it fails to open. 11855450Sbrendan */ 11865450Sbrendan spa_l2cache_add(vd); 11875450Sbrendan 11886643Seschrock vd->vdev_top = vd; 11896643Seschrock vd->vdev_aux = sav; 11906643Seschrock 11916643Seschrock spa_l2cache_activate(vd); 11926643Seschrock 11935450Sbrendan if (vdev_open(vd) != 0) 11945450Sbrendan continue; 11955450Sbrendan 11965450Sbrendan (void) vdev_validate_aux(vd); 11975450Sbrendan 11989816SGeorge.Wilson@Sun.COM if (!vdev_is_dead(vd)) 11999816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa, vd); 12005450Sbrendan } 12015450Sbrendan } 12025450Sbrendan 12035450Sbrendan /* 12045450Sbrendan * Purge vdevs that were dropped 12055450Sbrendan */ 12065450Sbrendan for (i = 0; i < oldnvdevs; i++) { 12075450Sbrendan uint64_t pool; 12085450Sbrendan 12095450Sbrendan vd = oldvdevs[i]; 12105450Sbrendan if (vd != NULL) { 12118241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 12128241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 12135450Sbrendan l2arc_remove_vdev(vd); 12145450Sbrendan (void) vdev_close(vd); 12155450Sbrendan spa_l2cache_remove(vd); 12165450Sbrendan } 12175450Sbrendan } 12185450Sbrendan 12195450Sbrendan if (oldvdevs) 12205450Sbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 12215450Sbrendan 12225450Sbrendan if (sav->sav_config == NULL) 12235450Sbrendan goto out; 12245450Sbrendan 12255450Sbrendan sav->sav_vdevs = newvdevs; 12265450Sbrendan sav->sav_count = (int)nl2cache; 12275450Sbrendan 12285450Sbrendan /* 12295450Sbrendan * Recompute the stashed list of l2cache devices, with status 12305450Sbrendan * information this time. 12315450Sbrendan */ 12325450Sbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 12335450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 12345450Sbrendan 12355450Sbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 12365450Sbrendan for (i = 0; i < sav->sav_count; i++) 12375450Sbrendan l2cache[i] = vdev_config_generate(spa, 123812296SLin.Ling@Sun.COM sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 12395450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 12405450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 12415450Sbrendan out: 12425450Sbrendan for (i = 0; i < sav->sav_count; i++) 12435450Sbrendan nvlist_free(l2cache[i]); 12445450Sbrendan if (sav->sav_count) 12455450Sbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 12462082Seschrock } 12472082Seschrock 12482082Seschrock static int 12492082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 12502082Seschrock { 12512082Seschrock dmu_buf_t *db; 12522082Seschrock char *packed = NULL; 12532082Seschrock size_t nvsize = 0; 12542082Seschrock int error; 12552082Seschrock *value = NULL; 12562082Seschrock 12572082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 12582082Seschrock nvsize = *(uint64_t *)db->db_data; 12592082Seschrock dmu_buf_rele(db, FTAG); 12602082Seschrock 12612082Seschrock packed = kmem_alloc(nvsize, KM_SLEEP); 12629512SNeil.Perrin@Sun.COM error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 12639512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 12642082Seschrock if (error == 0) 12652082Seschrock error = nvlist_unpack(packed, nvsize, value, 0); 12662082Seschrock kmem_free(packed, nvsize); 12672082Seschrock 12682082Seschrock return (error); 12692082Seschrock } 12702082Seschrock 12712082Seschrock /* 12724451Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 12734451Seschrock * sysevent to notify the autoreplace code that the device has been removed. 12744451Seschrock */ 12754451Seschrock static void 12764451Seschrock spa_check_removed(vdev_t *vd) 12774451Seschrock { 12789816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 12794451Seschrock spa_check_removed(vd->vdev_child[c]); 12804451Seschrock 12814451Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 12824451Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 12834451Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 12844451Seschrock } 12854451Seschrock } 12864451Seschrock 12874451Seschrock /* 128812949SGeorge.Wilson@Sun.COM * Validate the current config against the MOS config 12899701SGeorge.Wilson@Sun.COM */ 129012949SGeorge.Wilson@Sun.COM static boolean_t 129112949SGeorge.Wilson@Sun.COM spa_config_valid(spa_t *spa, nvlist_t *config) 12929701SGeorge.Wilson@Sun.COM { 129312949SGeorge.Wilson@Sun.COM vdev_t *mrvd, *rvd = spa->spa_root_vdev; 129412949SGeorge.Wilson@Sun.COM nvlist_t *nv; 129512949SGeorge.Wilson@Sun.COM 129612949SGeorge.Wilson@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 129712949SGeorge.Wilson@Sun.COM 129812949SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 129912949SGeorge.Wilson@Sun.COM VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 130012949SGeorge.Wilson@Sun.COM 130112949SGeorge.Wilson@Sun.COM ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 130212949SGeorge.Wilson@Sun.COM 130312949SGeorge.Wilson@Sun.COM /* 130412949SGeorge.Wilson@Sun.COM * If we're doing a normal import, then build up any additional 130512949SGeorge.Wilson@Sun.COM * diagnostic information about missing devices in this config. 130612949SGeorge.Wilson@Sun.COM * We'll pass this up to the user for further processing. 130712949SGeorge.Wilson@Sun.COM */ 130812949SGeorge.Wilson@Sun.COM if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 130912949SGeorge.Wilson@Sun.COM nvlist_t **child, *nv; 131012949SGeorge.Wilson@Sun.COM uint64_t idx = 0; 131112949SGeorge.Wilson@Sun.COM 131212949SGeorge.Wilson@Sun.COM child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 131312949SGeorge.Wilson@Sun.COM KM_SLEEP); 131412949SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 131512949SGeorge.Wilson@Sun.COM 131612949SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 131712949SGeorge.Wilson@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 131812949SGeorge.Wilson@Sun.COM vdev_t *mtvd = mrvd->vdev_child[c]; 131912949SGeorge.Wilson@Sun.COM 132012949SGeorge.Wilson@Sun.COM if (tvd->vdev_ops == &vdev_missing_ops && 132112949SGeorge.Wilson@Sun.COM mtvd->vdev_ops != &vdev_missing_ops && 132212949SGeorge.Wilson@Sun.COM mtvd->vdev_islog) 132312949SGeorge.Wilson@Sun.COM child[idx++] = vdev_config_generate(spa, mtvd, 132412949SGeorge.Wilson@Sun.COM B_FALSE, 0); 132512949SGeorge.Wilson@Sun.COM } 132612949SGeorge.Wilson@Sun.COM 132712949SGeorge.Wilson@Sun.COM if (idx) { 132812949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist_array(nv, 132912949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 133012949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist(spa->spa_load_info, 133112949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 133212949SGeorge.Wilson@Sun.COM 133312949SGeorge.Wilson@Sun.COM for (int i = 0; i < idx; i++) 133412949SGeorge.Wilson@Sun.COM nvlist_free(child[i]); 133512949SGeorge.Wilson@Sun.COM } 133612949SGeorge.Wilson@Sun.COM nvlist_free(nv); 133712949SGeorge.Wilson@Sun.COM kmem_free(child, rvd->vdev_children * sizeof (char **)); 133812949SGeorge.Wilson@Sun.COM } 133910594SGeorge.Wilson@Sun.COM 134010594SGeorge.Wilson@Sun.COM /* 134112949SGeorge.Wilson@Sun.COM * Compare the root vdev tree with the information we have 134212949SGeorge.Wilson@Sun.COM * from the MOS config (mrvd). Check each top-level vdev 134312949SGeorge.Wilson@Sun.COM * with the corresponding MOS config top-level (mtvd). 134410594SGeorge.Wilson@Sun.COM */ 134510594SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 134612949SGeorge.Wilson@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 134712949SGeorge.Wilson@Sun.COM vdev_t *mtvd = mrvd->vdev_child[c]; 134812949SGeorge.Wilson@Sun.COM 134912949SGeorge.Wilson@Sun.COM /* 135012949SGeorge.Wilson@Sun.COM * Resolve any "missing" vdevs in the current configuration. 135112949SGeorge.Wilson@Sun.COM * If we find that the MOS config has more accurate information 135212949SGeorge.Wilson@Sun.COM * about the top-level vdev then use that vdev instead. 135312949SGeorge.Wilson@Sun.COM */ 135412949SGeorge.Wilson@Sun.COM if (tvd->vdev_ops == &vdev_missing_ops && 135512949SGeorge.Wilson@Sun.COM mtvd->vdev_ops != &vdev_missing_ops) { 135612949SGeorge.Wilson@Sun.COM 135712949SGeorge.Wilson@Sun.COM if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 135812949SGeorge.Wilson@Sun.COM continue; 135912949SGeorge.Wilson@Sun.COM 136012949SGeorge.Wilson@Sun.COM /* 136112949SGeorge.Wilson@Sun.COM * Device specific actions. 136212949SGeorge.Wilson@Sun.COM */ 136312949SGeorge.Wilson@Sun.COM if (mtvd->vdev_islog) { 136412949SGeorge.Wilson@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 136512949SGeorge.Wilson@Sun.COM } else { 136612949SGeorge.Wilson@Sun.COM /* 136712949SGeorge.Wilson@Sun.COM * XXX - once we have 'readonly' pool 136812949SGeorge.Wilson@Sun.COM * support we should be able to handle 136912949SGeorge.Wilson@Sun.COM * missing data devices by transitioning 137012949SGeorge.Wilson@Sun.COM * the pool to readonly. 137112949SGeorge.Wilson@Sun.COM */ 137212949SGeorge.Wilson@Sun.COM continue; 137312949SGeorge.Wilson@Sun.COM } 137412949SGeorge.Wilson@Sun.COM 137512949SGeorge.Wilson@Sun.COM /* 137612949SGeorge.Wilson@Sun.COM * Swap the missing vdev with the data we were 137712949SGeorge.Wilson@Sun.COM * able to obtain from the MOS config. 137812949SGeorge.Wilson@Sun.COM */ 137912949SGeorge.Wilson@Sun.COM vdev_remove_child(rvd, tvd); 138012949SGeorge.Wilson@Sun.COM vdev_remove_child(mrvd, mtvd); 138112949SGeorge.Wilson@Sun.COM 138212949SGeorge.Wilson@Sun.COM vdev_add_child(rvd, mtvd); 138312949SGeorge.Wilson@Sun.COM vdev_add_child(mrvd, tvd); 138412949SGeorge.Wilson@Sun.COM 138512949SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 138612949SGeorge.Wilson@Sun.COM vdev_load(mtvd); 138712949SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 138812949SGeorge.Wilson@Sun.COM 138912949SGeorge.Wilson@Sun.COM vdev_reopen(rvd); 139012949SGeorge.Wilson@Sun.COM } else if (mtvd->vdev_islog) { 139112949SGeorge.Wilson@Sun.COM /* 139212949SGeorge.Wilson@Sun.COM * Load the slog device's state from the MOS config 139312949SGeorge.Wilson@Sun.COM * since it's possible that the label does not 139412949SGeorge.Wilson@Sun.COM * contain the most up-to-date information. 139512949SGeorge.Wilson@Sun.COM */ 139612949SGeorge.Wilson@Sun.COM vdev_load_log_state(tvd, mtvd); 139712949SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 139812949SGeorge.Wilson@Sun.COM } 13999701SGeorge.Wilson@Sun.COM } 140012949SGeorge.Wilson@Sun.COM vdev_free(mrvd); 140110594SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 140212949SGeorge.Wilson@Sun.COM 140312949SGeorge.Wilson@Sun.COM /* 140412949SGeorge.Wilson@Sun.COM * Ensure we were able to validate the config. 140512949SGeorge.Wilson@Sun.COM */ 140612949SGeorge.Wilson@Sun.COM return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 14079701SGeorge.Wilson@Sun.COM } 14089701SGeorge.Wilson@Sun.COM 14099701SGeorge.Wilson@Sun.COM /* 14107294Sperrin * Check for missing log devices 14117294Sperrin */ 141212949SGeorge.Wilson@Sun.COM static int 14137294Sperrin spa_check_logs(spa_t *spa) 14147294Sperrin { 14157294Sperrin switch (spa->spa_log_state) { 14167294Sperrin case SPA_LOG_MISSING: 14177294Sperrin /* need to recheck in case slog has been restored */ 14187294Sperrin case SPA_LOG_UNKNOWN: 14197294Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 14207294Sperrin DS_FIND_CHILDREN)) { 142111422SMark.Musante@Sun.COM spa_set_log_state(spa, SPA_LOG_MISSING); 14227294Sperrin return (1); 14237294Sperrin } 14247294Sperrin break; 14257294Sperrin } 14267294Sperrin return (0); 14277294Sperrin } 14287294Sperrin 142911422SMark.Musante@Sun.COM static boolean_t 143011422SMark.Musante@Sun.COM spa_passivate_log(spa_t *spa) 143111422SMark.Musante@Sun.COM { 143211422SMark.Musante@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 143311422SMark.Musante@Sun.COM boolean_t slog_found = B_FALSE; 143411422SMark.Musante@Sun.COM 143511422SMark.Musante@Sun.COM ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 143611422SMark.Musante@Sun.COM 143711422SMark.Musante@Sun.COM if (!spa_has_slogs(spa)) 143811422SMark.Musante@Sun.COM return (B_FALSE); 143911422SMark.Musante@Sun.COM 144011422SMark.Musante@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 144111422SMark.Musante@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 144211422SMark.Musante@Sun.COM metaslab_group_t *mg = tvd->vdev_mg; 144311422SMark.Musante@Sun.COM 144411422SMark.Musante@Sun.COM if (tvd->vdev_islog) { 144511422SMark.Musante@Sun.COM metaslab_group_passivate(mg); 144611422SMark.Musante@Sun.COM slog_found = B_TRUE; 144711422SMark.Musante@Sun.COM } 144811422SMark.Musante@Sun.COM } 144911422SMark.Musante@Sun.COM 145011422SMark.Musante@Sun.COM return (slog_found); 145111422SMark.Musante@Sun.COM } 145211422SMark.Musante@Sun.COM 145311422SMark.Musante@Sun.COM static void 145411422SMark.Musante@Sun.COM spa_activate_log(spa_t *spa) 145511422SMark.Musante@Sun.COM { 145611422SMark.Musante@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 145711422SMark.Musante@Sun.COM 145811422SMark.Musante@Sun.COM ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 145911422SMark.Musante@Sun.COM 146011422SMark.Musante@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 146111422SMark.Musante@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 146211422SMark.Musante@Sun.COM metaslab_group_t *mg = tvd->vdev_mg; 146311422SMark.Musante@Sun.COM 146411422SMark.Musante@Sun.COM if (tvd->vdev_islog) 146511422SMark.Musante@Sun.COM metaslab_group_activate(mg); 146611422SMark.Musante@Sun.COM } 146711422SMark.Musante@Sun.COM } 146811422SMark.Musante@Sun.COM 146911422SMark.Musante@Sun.COM int 147011422SMark.Musante@Sun.COM spa_offline_log(spa_t *spa) 147111422SMark.Musante@Sun.COM { 147211422SMark.Musante@Sun.COM int error = 0; 147311422SMark.Musante@Sun.COM 147411422SMark.Musante@Sun.COM if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 147511422SMark.Musante@Sun.COM NULL, DS_FIND_CHILDREN)) == 0) { 147611422SMark.Musante@Sun.COM 147711422SMark.Musante@Sun.COM /* 147811422SMark.Musante@Sun.COM * We successfully offlined the log device, sync out the 147911422SMark.Musante@Sun.COM * current txg so that the "stubby" block can be removed 148011422SMark.Musante@Sun.COM * by zil_sync(). 148111422SMark.Musante@Sun.COM */ 148211422SMark.Musante@Sun.COM txg_wait_synced(spa->spa_dsl_pool, 0); 148311422SMark.Musante@Sun.COM } 148411422SMark.Musante@Sun.COM return (error); 148511422SMark.Musante@Sun.COM } 148611422SMark.Musante@Sun.COM 148710672SEric.Schrock@Sun.COM static void 148810672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav) 148910672SEric.Schrock@Sun.COM { 149010922SJeff.Bonwick@Sun.COM for (int i = 0; i < sav->sav_count; i++) 149110672SEric.Schrock@Sun.COM spa_check_removed(sav->sav_vdevs[i]); 149210672SEric.Schrock@Sun.COM } 149310672SEric.Schrock@Sun.COM 149410922SJeff.Bonwick@Sun.COM void 149510922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio) 149610922SJeff.Bonwick@Sun.COM { 149710922SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 149810922SJeff.Bonwick@Sun.COM 149910922SJeff.Bonwick@Sun.COM if (zio->io_error) 150010922SJeff.Bonwick@Sun.COM return; 150110922SJeff.Bonwick@Sun.COM 150210922SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 150310922SJeff.Bonwick@Sun.COM if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 150410922SJeff.Bonwick@Sun.COM spa->spa_claim_max_txg = zio->io_bp->blk_birth; 150510922SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 150610922SJeff.Bonwick@Sun.COM } 150710922SJeff.Bonwick@Sun.COM 150810921STim.Haley@Sun.COM typedef struct spa_load_error { 150911727SVictor.Latushkin@Sun.COM uint64_t sle_meta_count; 151010921STim.Haley@Sun.COM uint64_t sle_data_count; 151110921STim.Haley@Sun.COM } spa_load_error_t; 151210921STim.Haley@Sun.COM 151310921STim.Haley@Sun.COM static void 151410921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio) 151510921STim.Haley@Sun.COM { 151610921STim.Haley@Sun.COM blkptr_t *bp = zio->io_bp; 151710921STim.Haley@Sun.COM spa_load_error_t *sle = zio->io_private; 151810921STim.Haley@Sun.COM dmu_object_type_t type = BP_GET_TYPE(bp); 151910921STim.Haley@Sun.COM int error = zio->io_error; 152010921STim.Haley@Sun.COM 152110921STim.Haley@Sun.COM if (error) { 152210921STim.Haley@Sun.COM if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) && 152310921STim.Haley@Sun.COM type != DMU_OT_INTENT_LOG) 152411727SVictor.Latushkin@Sun.COM atomic_add_64(&sle->sle_meta_count, 1); 152510921STim.Haley@Sun.COM else 152610921STim.Haley@Sun.COM atomic_add_64(&sle->sle_data_count, 1); 152710921STim.Haley@Sun.COM } 152810921STim.Haley@Sun.COM zio_data_buf_free(zio->io_data, zio->io_size); 152910921STim.Haley@Sun.COM } 153010921STim.Haley@Sun.COM 153110921STim.Haley@Sun.COM /*ARGSUSED*/ 153210921STim.Haley@Sun.COM static int 153310922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 153412296SLin.Ling@Sun.COM arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 153510921STim.Haley@Sun.COM { 153610921STim.Haley@Sun.COM if (bp != NULL) { 153710921STim.Haley@Sun.COM zio_t *rio = arg; 153810921STim.Haley@Sun.COM size_t size = BP_GET_PSIZE(bp); 153910921STim.Haley@Sun.COM void *data = zio_data_buf_alloc(size); 154010921STim.Haley@Sun.COM 154110921STim.Haley@Sun.COM zio_nowait(zio_read(rio, spa, bp, data, size, 154210921STim.Haley@Sun.COM spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 154310921STim.Haley@Sun.COM ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 154410921STim.Haley@Sun.COM ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 154510921STim.Haley@Sun.COM } 154610921STim.Haley@Sun.COM return (0); 154710921STim.Haley@Sun.COM } 154810921STim.Haley@Sun.COM 154910921STim.Haley@Sun.COM static int 155010921STim.Haley@Sun.COM spa_load_verify(spa_t *spa) 155110921STim.Haley@Sun.COM { 155210921STim.Haley@Sun.COM zio_t *rio; 155310921STim.Haley@Sun.COM spa_load_error_t sle = { 0 }; 155410921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 155510921STim.Haley@Sun.COM boolean_t verify_ok = B_FALSE; 155610921STim.Haley@Sun.COM int error; 155710921STim.Haley@Sun.COM 155811727SVictor.Latushkin@Sun.COM zpool_get_rewind_policy(spa->spa_config, &policy); 155911727SVictor.Latushkin@Sun.COM 156011727SVictor.Latushkin@Sun.COM if (policy.zrp_request & ZPOOL_NEVER_REWIND) 156111727SVictor.Latushkin@Sun.COM return (0); 156211727SVictor.Latushkin@Sun.COM 156310921STim.Haley@Sun.COM rio = zio_root(spa, NULL, &sle, 156410921STim.Haley@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 156510921STim.Haley@Sun.COM 156611125SJeff.Bonwick@Sun.COM error = traverse_pool(spa, spa->spa_verify_min_txg, 156711125SJeff.Bonwick@Sun.COM TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); 156810921STim.Haley@Sun.COM 156910921STim.Haley@Sun.COM (void) zio_wait(rio); 157010921STim.Haley@Sun.COM 157111727SVictor.Latushkin@Sun.COM spa->spa_load_meta_errors = sle.sle_meta_count; 157210921STim.Haley@Sun.COM spa->spa_load_data_errors = sle.sle_data_count; 157310921STim.Haley@Sun.COM 157411727SVictor.Latushkin@Sun.COM if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 157510921STim.Haley@Sun.COM sle.sle_data_count <= policy.zrp_maxdata) { 157612949SGeorge.Wilson@Sun.COM int64_t loss = 0; 157712949SGeorge.Wilson@Sun.COM 157810921STim.Haley@Sun.COM verify_ok = B_TRUE; 157910921STim.Haley@Sun.COM spa->spa_load_txg = spa->spa_uberblock.ub_txg; 158010921STim.Haley@Sun.COM spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 158112949SGeorge.Wilson@Sun.COM 158212949SGeorge.Wilson@Sun.COM loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 158312949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(spa->spa_load_info, 158412949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 158512949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_int64(spa->spa_load_info, 158612949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 158712949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(spa->spa_load_info, 158812949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 158911026STim.Haley@Sun.COM } else { 159011026STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 159110921STim.Haley@Sun.COM } 159210921STim.Haley@Sun.COM 159310921STim.Haley@Sun.COM if (error) { 159410921STim.Haley@Sun.COM if (error != ENXIO && error != EIO) 159510921STim.Haley@Sun.COM error = EIO; 159610921STim.Haley@Sun.COM return (error); 159710921STim.Haley@Sun.COM } 159810921STim.Haley@Sun.COM 159910921STim.Haley@Sun.COM return (verify_ok ? 0 : EIO); 160010921STim.Haley@Sun.COM } 160110921STim.Haley@Sun.COM 16027294Sperrin /* 160311422SMark.Musante@Sun.COM * Find a value in the pool props object. 160411422SMark.Musante@Sun.COM */ 160511422SMark.Musante@Sun.COM static void 160611422SMark.Musante@Sun.COM spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 160711422SMark.Musante@Sun.COM { 160811422SMark.Musante@Sun.COM (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 160911422SMark.Musante@Sun.COM zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 161011422SMark.Musante@Sun.COM } 161111422SMark.Musante@Sun.COM 161211422SMark.Musante@Sun.COM /* 161311422SMark.Musante@Sun.COM * Find a value in the pool directory object. 161411422SMark.Musante@Sun.COM */ 161511422SMark.Musante@Sun.COM static int 161611422SMark.Musante@Sun.COM spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 161711422SMark.Musante@Sun.COM { 161811422SMark.Musante@Sun.COM return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 161911422SMark.Musante@Sun.COM name, sizeof (uint64_t), 1, val)); 162011422SMark.Musante@Sun.COM } 162111422SMark.Musante@Sun.COM 162211422SMark.Musante@Sun.COM static int 162311422SMark.Musante@Sun.COM spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 162411422SMark.Musante@Sun.COM { 162511422SMark.Musante@Sun.COM vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 162611422SMark.Musante@Sun.COM return (err); 162711422SMark.Musante@Sun.COM } 162811422SMark.Musante@Sun.COM 162911422SMark.Musante@Sun.COM /* 163011422SMark.Musante@Sun.COM * Fix up config after a partly-completed split. This is done with the 163111422SMark.Musante@Sun.COM * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 163211422SMark.Musante@Sun.COM * pool have that entry in their config, but only the splitting one contains 163311422SMark.Musante@Sun.COM * a list of all the guids of the vdevs that are being split off. 163411422SMark.Musante@Sun.COM * 163511422SMark.Musante@Sun.COM * This function determines what to do with that list: either rejoin 163611422SMark.Musante@Sun.COM * all the disks to the pool, or complete the splitting process. To attempt 163711422SMark.Musante@Sun.COM * the rejoin, each disk that is offlined is marked online again, and 163811422SMark.Musante@Sun.COM * we do a reopen() call. If the vdev label for every disk that was 163911422SMark.Musante@Sun.COM * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 164011422SMark.Musante@Sun.COM * then we call vdev_split() on each disk, and complete the split. 164111422SMark.Musante@Sun.COM * 164211497SMark.Musante@Sun.COM * Otherwise we leave the config alone, with all the vdevs in place in 164311497SMark.Musante@Sun.COM * the original pool. 164411422SMark.Musante@Sun.COM */ 164511422SMark.Musante@Sun.COM static void 164611422SMark.Musante@Sun.COM spa_try_repair(spa_t *spa, nvlist_t *config) 164711422SMark.Musante@Sun.COM { 164811422SMark.Musante@Sun.COM uint_t extracted; 164911422SMark.Musante@Sun.COM uint64_t *glist; 165011422SMark.Musante@Sun.COM uint_t i, gcount; 165111422SMark.Musante@Sun.COM nvlist_t *nvl; 165211422SMark.Musante@Sun.COM vdev_t **vd; 165311422SMark.Musante@Sun.COM boolean_t attempt_reopen; 165411422SMark.Musante@Sun.COM 165511422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 165611422SMark.Musante@Sun.COM return; 165711422SMark.Musante@Sun.COM 165811422SMark.Musante@Sun.COM /* check that the config is complete */ 165911422SMark.Musante@Sun.COM if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 166011422SMark.Musante@Sun.COM &glist, &gcount) != 0) 166111422SMark.Musante@Sun.COM return; 166211422SMark.Musante@Sun.COM 166311422SMark.Musante@Sun.COM vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 166411422SMark.Musante@Sun.COM 166511422SMark.Musante@Sun.COM /* attempt to online all the vdevs & validate */ 166611422SMark.Musante@Sun.COM attempt_reopen = B_TRUE; 166711422SMark.Musante@Sun.COM for (i = 0; i < gcount; i++) { 166811422SMark.Musante@Sun.COM if (glist[i] == 0) /* vdev is hole */ 166911422SMark.Musante@Sun.COM continue; 167011422SMark.Musante@Sun.COM 167111422SMark.Musante@Sun.COM vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 167211422SMark.Musante@Sun.COM if (vd[i] == NULL) { 167311422SMark.Musante@Sun.COM /* 167411422SMark.Musante@Sun.COM * Don't bother attempting to reopen the disks; 167511422SMark.Musante@Sun.COM * just do the split. 167611422SMark.Musante@Sun.COM */ 167711422SMark.Musante@Sun.COM attempt_reopen = B_FALSE; 167811422SMark.Musante@Sun.COM } else { 167911422SMark.Musante@Sun.COM /* attempt to re-online it */ 168011422SMark.Musante@Sun.COM vd[i]->vdev_offline = B_FALSE; 168111422SMark.Musante@Sun.COM } 168211422SMark.Musante@Sun.COM } 168311422SMark.Musante@Sun.COM 168411422SMark.Musante@Sun.COM if (attempt_reopen) { 168511422SMark.Musante@Sun.COM vdev_reopen(spa->spa_root_vdev); 168611422SMark.Musante@Sun.COM 168711422SMark.Musante@Sun.COM /* check each device to see what state it's in */ 168811422SMark.Musante@Sun.COM for (extracted = 0, i = 0; i < gcount; i++) { 168911422SMark.Musante@Sun.COM if (vd[i] != NULL && 169011422SMark.Musante@Sun.COM vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 169111422SMark.Musante@Sun.COM break; 169211422SMark.Musante@Sun.COM ++extracted; 169311422SMark.Musante@Sun.COM } 169411422SMark.Musante@Sun.COM } 169511422SMark.Musante@Sun.COM 169611422SMark.Musante@Sun.COM /* 169711422SMark.Musante@Sun.COM * If every disk has been moved to the new pool, or if we never 169811422SMark.Musante@Sun.COM * even attempted to look at them, then we split them off for 169911422SMark.Musante@Sun.COM * good. 170011422SMark.Musante@Sun.COM */ 170111422SMark.Musante@Sun.COM if (!attempt_reopen || gcount == extracted) { 170211422SMark.Musante@Sun.COM for (i = 0; i < gcount; i++) 170311422SMark.Musante@Sun.COM if (vd[i] != NULL) 170411422SMark.Musante@Sun.COM vdev_split(vd[i]); 170511422SMark.Musante@Sun.COM vdev_reopen(spa->spa_root_vdev); 170611422SMark.Musante@Sun.COM } 170711422SMark.Musante@Sun.COM 170811422SMark.Musante@Sun.COM kmem_free(vd, gcount * sizeof (vdev_t *)); 170911422SMark.Musante@Sun.COM } 171011422SMark.Musante@Sun.COM 171111422SMark.Musante@Sun.COM static int 171211422SMark.Musante@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 171311422SMark.Musante@Sun.COM boolean_t mosconfig) 171411422SMark.Musante@Sun.COM { 171511422SMark.Musante@Sun.COM nvlist_t *config = spa->spa_config; 171611422SMark.Musante@Sun.COM char *ereport = FM_EREPORT_ZFS_POOL; 171711422SMark.Musante@Sun.COM int error; 171811422SMark.Musante@Sun.COM uint64_t pool_guid; 171911422SMark.Musante@Sun.COM nvlist_t *nvl; 172011422SMark.Musante@Sun.COM 172111422SMark.Musante@Sun.COM if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 172211422SMark.Musante@Sun.COM return (EINVAL); 172311422SMark.Musante@Sun.COM 172411422SMark.Musante@Sun.COM /* 172511422SMark.Musante@Sun.COM * Versioning wasn't explicitly added to the label until later, so if 172611422SMark.Musante@Sun.COM * it's not present treat it as the initial version. 172711422SMark.Musante@Sun.COM */ 172811422SMark.Musante@Sun.COM if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 172911422SMark.Musante@Sun.COM &spa->spa_ubsync.ub_version) != 0) 173011422SMark.Musante@Sun.COM spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 173111422SMark.Musante@Sun.COM 173211422SMark.Musante@Sun.COM (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 173311422SMark.Musante@Sun.COM &spa->spa_config_txg); 173411422SMark.Musante@Sun.COM 173511422SMark.Musante@Sun.COM if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 173611422SMark.Musante@Sun.COM spa_guid_exists(pool_guid, 0)) { 173711422SMark.Musante@Sun.COM error = EEXIST; 173811422SMark.Musante@Sun.COM } else { 173911422SMark.Musante@Sun.COM spa->spa_load_guid = pool_guid; 174011422SMark.Musante@Sun.COM 174111422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 174211422SMark.Musante@Sun.COM &nvl) == 0) { 174311422SMark.Musante@Sun.COM VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 174411422SMark.Musante@Sun.COM KM_SLEEP) == 0); 174511422SMark.Musante@Sun.COM } 174611422SMark.Musante@Sun.COM 174712817STim.Haley@Sun.COM gethrestime(&spa->spa_loaded_ts); 174811422SMark.Musante@Sun.COM error = spa_load_impl(spa, pool_guid, config, state, type, 174911422SMark.Musante@Sun.COM mosconfig, &ereport); 175011422SMark.Musante@Sun.COM } 175111422SMark.Musante@Sun.COM 175211422SMark.Musante@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 175312817STim.Haley@Sun.COM if (error) { 175412817STim.Haley@Sun.COM if (error != EEXIST) { 175512817STim.Haley@Sun.COM spa->spa_loaded_ts.tv_sec = 0; 175612817STim.Haley@Sun.COM spa->spa_loaded_ts.tv_nsec = 0; 175712817STim.Haley@Sun.COM } 175812817STim.Haley@Sun.COM if (error != EBADF) { 175912817STim.Haley@Sun.COM zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 176012817STim.Haley@Sun.COM } 176112817STim.Haley@Sun.COM } 176211422SMark.Musante@Sun.COM spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 176311422SMark.Musante@Sun.COM spa->spa_ena = 0; 176411422SMark.Musante@Sun.COM 176511422SMark.Musante@Sun.COM return (error); 176611422SMark.Musante@Sun.COM } 176711422SMark.Musante@Sun.COM 176811422SMark.Musante@Sun.COM /* 1769789Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 17701544Seschrock * source of configuration information. 1771789Sahrens */ 1772789Sahrens static int 177311422SMark.Musante@Sun.COM spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 177411422SMark.Musante@Sun.COM spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 177511422SMark.Musante@Sun.COM char **ereport) 1776789Sahrens { 1777789Sahrens int error = 0; 177811810SMark.Musante@Sun.COM nvlist_t *nvroot = NULL; 1779789Sahrens vdev_t *rvd; 1780789Sahrens uberblock_t *ub = &spa->spa_uberblock; 178112949SGeorge.Wilson@Sun.COM uint64_t children, config_cache_txg = spa->spa_config_txg; 17828241SJeff.Bonwick@Sun.COM int orig_mode = spa->spa_mode; 178311422SMark.Musante@Sun.COM int parse; 178412470SMatthew.Ahrens@Sun.COM uint64_t obj; 1785789Sahrens 17868241SJeff.Bonwick@Sun.COM /* 17878241SJeff.Bonwick@Sun.COM * If this is an untrusted config, access the pool in read-only mode. 17888241SJeff.Bonwick@Sun.COM * This prevents things like resilvering recently removed devices. 17898241SJeff.Bonwick@Sun.COM */ 17908241SJeff.Bonwick@Sun.COM if (!mosconfig) 17918241SJeff.Bonwick@Sun.COM spa->spa_mode = FREAD; 17928241SJeff.Bonwick@Sun.COM 17937754SJeff.Bonwick@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 17947754SJeff.Bonwick@Sun.COM 17951544Seschrock spa->spa_load_state = state; 17961635Sbonwick 179711422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 179811422SMark.Musante@Sun.COM return (EINVAL); 179911422SMark.Musante@Sun.COM 180011422SMark.Musante@Sun.COM parse = (type == SPA_IMPORT_EXISTING ? 180111422SMark.Musante@Sun.COM VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 18022174Seschrock 1803789Sahrens /* 18049234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 18059234SGeorge.Wilson@Sun.COM */ 18069630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 18079630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 18089234SGeorge.Wilson@Sun.COM 18099234SGeorge.Wilson@Sun.COM /* 18102082Seschrock * Parse the configuration into a vdev tree. We explicitly set the 18112082Seschrock * value that will be returned by spa_version() since parsing the 18122082Seschrock * configuration requires knowing the version number. 1813789Sahrens */ 18147754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 181511422SMark.Musante@Sun.COM error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 18167754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 1817789Sahrens 18182082Seschrock if (error != 0) 181911422SMark.Musante@Sun.COM return (error); 1820789Sahrens 18211585Sbonwick ASSERT(spa->spa_root_vdev == rvd); 182211422SMark.Musante@Sun.COM 182311422SMark.Musante@Sun.COM if (type != SPA_IMPORT_ASSEMBLE) { 182411422SMark.Musante@Sun.COM ASSERT(spa_guid(spa) == pool_guid); 182511422SMark.Musante@Sun.COM } 1826789Sahrens 1827789Sahrens /* 1828789Sahrens * Try to open all vdevs, loading each label in the process. 1829789Sahrens */ 18307754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 18314070Smc142369 error = vdev_open(rvd); 18327754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 18334070Smc142369 if (error != 0) 183411422SMark.Musante@Sun.COM return (error); 1835789Sahrens 1836789Sahrens /* 18379276SMark.Musante@Sun.COM * We need to validate the vdev labels against the configuration that 18389276SMark.Musante@Sun.COM * we have in hand, which is dependent on the setting of mosconfig. If 18399276SMark.Musante@Sun.COM * mosconfig is true then we're validating the vdev labels based on 184011422SMark.Musante@Sun.COM * that config. Otherwise, we're validating against the cached config 18419276SMark.Musante@Sun.COM * (zpool.cache) that was read when we loaded the zfs module, and then 18429276SMark.Musante@Sun.COM * later we will recursively call spa_load() and validate against 18439276SMark.Musante@Sun.COM * the vdev config. 184411422SMark.Musante@Sun.COM * 184511422SMark.Musante@Sun.COM * If we're assembling a new pool that's been split off from an 184611422SMark.Musante@Sun.COM * existing pool, the labels haven't yet been updated so we skip 184711422SMark.Musante@Sun.COM * validation for now. 18481986Seschrock */ 184911422SMark.Musante@Sun.COM if (type != SPA_IMPORT_ASSEMBLE) { 185011422SMark.Musante@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 185111422SMark.Musante@Sun.COM error = vdev_validate(rvd); 185211422SMark.Musante@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 185311422SMark.Musante@Sun.COM 185411422SMark.Musante@Sun.COM if (error != 0) 185511422SMark.Musante@Sun.COM return (error); 185611422SMark.Musante@Sun.COM 185711422SMark.Musante@Sun.COM if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 185811422SMark.Musante@Sun.COM return (ENXIO); 18591986Seschrock } 18601986Seschrock 18611986Seschrock /* 1862789Sahrens * Find the best uberblock. 1863789Sahrens */ 18647754SJeff.Bonwick@Sun.COM vdev_uberblock_load(NULL, rvd, ub); 1865789Sahrens 1866789Sahrens /* 1867789Sahrens * If we weren't able to find a single valid uberblock, return failure. 1868789Sahrens */ 186911422SMark.Musante@Sun.COM if (ub->ub_txg == 0) 187011422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 18711544Seschrock 18721544Seschrock /* 18731544Seschrock * If the pool is newer than the code, we can't open it. 18741544Seschrock */ 187511422SMark.Musante@Sun.COM if (ub->ub_version > SPA_VERSION) 187611422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 1877789Sahrens 1878789Sahrens /* 1879789Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 188012949SGeorge.Wilson@Sun.COM * incomplete configuration. We first check to see if the pool 188112949SGeorge.Wilson@Sun.COM * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 188212949SGeorge.Wilson@Sun.COM * If it is, defer the vdev_guid_sum check till later so we 188312949SGeorge.Wilson@Sun.COM * can handle missing vdevs. 1884789Sahrens */ 188512949SGeorge.Wilson@Sun.COM if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 188612949SGeorge.Wilson@Sun.COM &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 188711422SMark.Musante@Sun.COM rvd->vdev_guid_sum != ub->ub_guid_sum) 188811422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 188911422SMark.Musante@Sun.COM 189011422SMark.Musante@Sun.COM if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 189111422SMark.Musante@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 189211422SMark.Musante@Sun.COM spa_try_repair(spa, config); 189311422SMark.Musante@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 189411422SMark.Musante@Sun.COM nvlist_free(spa->spa_config_splitting); 189511422SMark.Musante@Sun.COM spa->spa_config_splitting = NULL; 1896789Sahrens } 1897789Sahrens 1898789Sahrens /* 1899789Sahrens * Initialize internal SPA structures. 1900789Sahrens */ 1901789Sahrens spa->spa_state = POOL_STATE_ACTIVE; 1902789Sahrens spa->spa_ubsync = spa->spa_uberblock; 190310921STim.Haley@Sun.COM spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 190411727SVictor.Latushkin@Sun.COM TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 190510921STim.Haley@Sun.COM spa->spa_first_txg = spa->spa_last_ubsync_txg ? 190610921STim.Haley@Sun.COM spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 190710922SJeff.Bonwick@Sun.COM spa->spa_claim_max_txg = spa->spa_first_txg; 190812296SLin.Ling@Sun.COM spa->spa_prev_software_version = ub->ub_software_version; 190910922SJeff.Bonwick@Sun.COM 19101544Seschrock error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 191111422SMark.Musante@Sun.COM if (error) 191211422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1913789Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1914789Sahrens 191511422SMark.Musante@Sun.COM if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 191611422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 191711422SMark.Musante@Sun.COM 1918789Sahrens if (!mosconfig) { 19193975Sek110237 uint64_t hostid; 192011810SMark.Musante@Sun.COM nvlist_t *policy = NULL, *nvconfig; 192111810SMark.Musante@Sun.COM 192211810SMark.Musante@Sun.COM if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 192311810SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 19242082Seschrock 192510594SGeorge.Wilson@Sun.COM if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 19267706SLin.Ling@Sun.COM ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 19273975Sek110237 char *hostname; 19283975Sek110237 unsigned long myhostid = 0; 19293975Sek110237 193010594SGeorge.Wilson@Sun.COM VERIFY(nvlist_lookup_string(nvconfig, 19313975Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 19323975Sek110237 19338662SJordan.Vaughan@Sun.com #ifdef _KERNEL 19348662SJordan.Vaughan@Sun.com myhostid = zone_get_hostid(NULL); 19358662SJordan.Vaughan@Sun.com #else /* _KERNEL */ 19368662SJordan.Vaughan@Sun.com /* 19378662SJordan.Vaughan@Sun.com * We're emulating the system's hostid in userland, so 19388662SJordan.Vaughan@Sun.com * we can't use zone_get_hostid(). 19398662SJordan.Vaughan@Sun.com */ 19403975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 19418662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 19424178Slling if (hostid != 0 && myhostid != 0 && 19438662SJordan.Vaughan@Sun.com hostid != myhostid) { 194411810SMark.Musante@Sun.COM nvlist_free(nvconfig); 19453975Sek110237 cmn_err(CE_WARN, "pool '%s' could not be " 19463975Sek110237 "loaded as it was last accessed by " 19477706SLin.Ling@Sun.COM "another system (host: %s hostid: 0x%lx). " 19483975Sek110237 "See: http://www.sun.com/msg/ZFS-8000-EY", 19497754SJeff.Bonwick@Sun.COM spa_name(spa), hostname, 19503975Sek110237 (unsigned long)hostid); 195111422SMark.Musante@Sun.COM return (EBADF); 19523975Sek110237 } 19533975Sek110237 } 195411727SVictor.Latushkin@Sun.COM if (nvlist_lookup_nvlist(spa->spa_config, 195511727SVictor.Latushkin@Sun.COM ZPOOL_REWIND_POLICY, &policy) == 0) 195611727SVictor.Latushkin@Sun.COM VERIFY(nvlist_add_nvlist(nvconfig, 195711727SVictor.Latushkin@Sun.COM ZPOOL_REWIND_POLICY, policy) == 0); 19583975Sek110237 195910594SGeorge.Wilson@Sun.COM spa_config_set(spa, nvconfig); 1960789Sahrens spa_unload(spa); 1961789Sahrens spa_deactivate(spa); 19628241SJeff.Bonwick@Sun.COM spa_activate(spa, orig_mode); 1963789Sahrens 196411422SMark.Musante@Sun.COM return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 19651544Seschrock } 19661544Seschrock 196712470SMatthew.Ahrens@Sun.COM if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 196812470SMatthew.Ahrens@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 196912470SMatthew.Ahrens@Sun.COM error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 197012470SMatthew.Ahrens@Sun.COM if (error != 0) 197111422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1972789Sahrens 19731544Seschrock /* 19742082Seschrock * Load the bit that tells us to use the new accounting function 19752082Seschrock * (raid-z deflation). If we have an older pool, this will not 19762082Seschrock * be present. 19772082Seschrock */ 197811422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 197911422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 198011422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 19812082Seschrock 198212296SLin.Ling@Sun.COM error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 198312296SLin.Ling@Sun.COM &spa->spa_creation_version); 198412296SLin.Ling@Sun.COM if (error != 0 && error != ENOENT) 198512296SLin.Ling@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 198612296SLin.Ling@Sun.COM 19872082Seschrock /* 19881544Seschrock * Load the persistent error log. If we have an older pool, this will 19891544Seschrock * not be present. 19901544Seschrock */ 199111422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 199211422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 199311422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 199411422SMark.Musante@Sun.COM 199511422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 199611422SMark.Musante@Sun.COM &spa->spa_errlog_scrub); 199711422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 199811422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 1999789Sahrens 2000789Sahrens /* 20012926Sek110237 * Load the history object. If we have an older pool, this 20022926Sek110237 * will not be present. 20032926Sek110237 */ 200411422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 200511422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 200611422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 200711422SMark.Musante@Sun.COM 200811422SMark.Musante@Sun.COM /* 200911422SMark.Musante@Sun.COM * If we're assembling the pool from the split-off vdevs of 201011422SMark.Musante@Sun.COM * an existing pool, we don't want to attach the spares & cache 201111422SMark.Musante@Sun.COM * devices. 201211422SMark.Musante@Sun.COM */ 20132926Sek110237 20142926Sek110237 /* 20152082Seschrock * Load any hot spares for this pool. 20162082Seschrock */ 201711422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 201811422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 201911422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 202011422SMark.Musante@Sun.COM if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 20214577Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 20225450Sbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 202311422SMark.Musante@Sun.COM &spa->spa_spares.sav_config) != 0) 202411422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20252082Seschrock 20267754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20272082Seschrock spa_load_spares(spa); 20287754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 202911422SMark.Musante@Sun.COM } else if (error == 0) { 203011422SMark.Musante@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 20312082Seschrock } 20322082Seschrock 20335450Sbrendan /* 20345450Sbrendan * Load any level 2 ARC devices for this pool. 20355450Sbrendan */ 203611422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 20375450Sbrendan &spa->spa_l2cache.sav_object); 203811422SMark.Musante@Sun.COM if (error != 0 && error != ENOENT) 203911422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 204011422SMark.Musante@Sun.COM if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 20415450Sbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 20425450Sbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 204311422SMark.Musante@Sun.COM &spa->spa_l2cache.sav_config) != 0) 204411422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20455450Sbrendan 20467754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20475450Sbrendan spa_load_l2cache(spa); 20487754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 204911422SMark.Musante@Sun.COM } else if (error == 0) { 205011422SMark.Musante@Sun.COM spa->spa_l2cache.sav_sync = B_TRUE; 20515450Sbrendan } 20525450Sbrendan 20535094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 20544543Smarks 205511422SMark.Musante@Sun.COM error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 205611422SMark.Musante@Sun.COM if (error && error != ENOENT) 205711422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 20583912Slling 20593912Slling if (error == 0) { 206011422SMark.Musante@Sun.COM uint64_t autoreplace; 206111422SMark.Musante@Sun.COM 206211422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 206311422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 206411422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 206511422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 206611422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 206711422SMark.Musante@Sun.COM spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 206811422SMark.Musante@Sun.COM &spa->spa_dedup_ditto); 206911422SMark.Musante@Sun.COM 207010672SEric.Schrock@Sun.COM spa->spa_autoreplace = (autoreplace != 0); 20713912Slling } 20723912Slling 20732082Seschrock /* 20744451Seschrock * If the 'autoreplace' property is set, then post a resource notifying 20754451Seschrock * the ZFS DE that it should not issue any faults for unopenable 20764451Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 20774451Seschrock * unopenable vdevs so that the normal autoreplace handler can take 20784451Seschrock * over. 20794451Seschrock */ 208010672SEric.Schrock@Sun.COM if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 20814451Seschrock spa_check_removed(spa->spa_root_vdev); 208210672SEric.Schrock@Sun.COM /* 208310672SEric.Schrock@Sun.COM * For the import case, this is done in spa_import(), because 208410672SEric.Schrock@Sun.COM * at this point we're using the spare definitions from 208510672SEric.Schrock@Sun.COM * the MOS config, not necessarily from the userland config. 208610672SEric.Schrock@Sun.COM */ 208710672SEric.Schrock@Sun.COM if (state != SPA_LOAD_IMPORT) { 208810672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 208910672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 209010672SEric.Schrock@Sun.COM } 209110672SEric.Schrock@Sun.COM } 20924451Seschrock 20934451Seschrock /* 20941986Seschrock * Load the vdev state for all toplevel vdevs. 2095789Sahrens */ 20961986Seschrock vdev_load(rvd); 2097789Sahrens 2098789Sahrens /* 2099789Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2100789Sahrens */ 21017754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2102789Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 21037754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2104789Sahrens 2105789Sahrens /* 210610922SJeff.Bonwick@Sun.COM * Load the DDTs (dedup tables). 210710922SJeff.Bonwick@Sun.COM */ 210810922SJeff.Bonwick@Sun.COM error = ddt_load(spa); 210911422SMark.Musante@Sun.COM if (error != 0) 211011422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 211110922SJeff.Bonwick@Sun.COM 211210956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 211310956SGeorge.Wilson@Sun.COM 211410922SJeff.Bonwick@Sun.COM /* 211512949SGeorge.Wilson@Sun.COM * Validate the config, using the MOS config to fill in any 211612949SGeorge.Wilson@Sun.COM * information which might be missing. If we fail to validate 211712949SGeorge.Wilson@Sun.COM * the config then declare the pool unfit for use. If we're 211812949SGeorge.Wilson@Sun.COM * assembling a pool from a split, the log is not transferred 211912949SGeorge.Wilson@Sun.COM * over. 212010922SJeff.Bonwick@Sun.COM */ 212111422SMark.Musante@Sun.COM if (type != SPA_IMPORT_ASSEMBLE) { 212211810SMark.Musante@Sun.COM nvlist_t *nvconfig; 212311810SMark.Musante@Sun.COM 212411810SMark.Musante@Sun.COM if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 212511810SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 212611810SMark.Musante@Sun.COM 212712949SGeorge.Wilson@Sun.COM if (!spa_config_valid(spa, nvconfig)) { 212812949SGeorge.Wilson@Sun.COM nvlist_free(nvconfig); 212912949SGeorge.Wilson@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 213012949SGeorge.Wilson@Sun.COM ENXIO)); 213112949SGeorge.Wilson@Sun.COM } 213211422SMark.Musante@Sun.COM nvlist_free(nvconfig); 213311422SMark.Musante@Sun.COM 213412949SGeorge.Wilson@Sun.COM /* 213512949SGeorge.Wilson@Sun.COM * Now that we've validate the config, check the state of the 213612949SGeorge.Wilson@Sun.COM * root vdev. If it can't be opened, it indicates one or 213712949SGeorge.Wilson@Sun.COM * more toplevel vdevs are faulted. 213812949SGeorge.Wilson@Sun.COM */ 213912949SGeorge.Wilson@Sun.COM if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 214012949SGeorge.Wilson@Sun.COM return (ENXIO); 214112949SGeorge.Wilson@Sun.COM 214211422SMark.Musante@Sun.COM if (spa_check_logs(spa)) { 214311422SMark.Musante@Sun.COM *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 214411422SMark.Musante@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 214511422SMark.Musante@Sun.COM } 214610922SJeff.Bonwick@Sun.COM } 214710922SJeff.Bonwick@Sun.COM 214812949SGeorge.Wilson@Sun.COM /* 214912949SGeorge.Wilson@Sun.COM * We've successfully opened the pool, verify that we're ready 215012949SGeorge.Wilson@Sun.COM * to start pushing transactions. 215112949SGeorge.Wilson@Sun.COM */ 215212949SGeorge.Wilson@Sun.COM if (state != SPA_LOAD_TRYIMPORT) { 215312949SGeorge.Wilson@Sun.COM if (error = spa_load_verify(spa)) 215412949SGeorge.Wilson@Sun.COM return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 215512949SGeorge.Wilson@Sun.COM error)); 215612949SGeorge.Wilson@Sun.COM } 215712949SGeorge.Wilson@Sun.COM 215810921STim.Haley@Sun.COM if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 215910921STim.Haley@Sun.COM spa->spa_load_max_txg == UINT64_MAX)) { 21601635Sbonwick dmu_tx_t *tx; 21611635Sbonwick int need_update = B_FALSE; 21628241SJeff.Bonwick@Sun.COM 21638241SJeff.Bonwick@Sun.COM ASSERT(state != SPA_LOAD_TRYIMPORT); 21641601Sbonwick 21651635Sbonwick /* 21661635Sbonwick * Claim log blocks that haven't been committed yet. 21671635Sbonwick * This must all happen in a single txg. 216810922SJeff.Bonwick@Sun.COM * Note: spa_claim_max_txg is updated by spa_claim_notify(), 216910922SJeff.Bonwick@Sun.COM * invoked from zil_claim_log_block()'s i/o done callback. 217010921STim.Haley@Sun.COM * Price of rollback is that we abandon the log. 21711635Sbonwick */ 217210922SJeff.Bonwick@Sun.COM spa->spa_claiming = B_TRUE; 217310922SJeff.Bonwick@Sun.COM 21741601Sbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 2175789Sahrens spa_first_txg(spa)); 21767754SJeff.Bonwick@Sun.COM (void) dmu_objset_find(spa_name(spa), 21772417Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2178789Sahrens dmu_tx_commit(tx); 2179789Sahrens 218010922SJeff.Bonwick@Sun.COM spa->spa_claiming = B_FALSE; 218110922SJeff.Bonwick@Sun.COM 218211422SMark.Musante@Sun.COM spa_set_log_state(spa, SPA_LOG_GOOD); 2183789Sahrens spa->spa_sync_on = B_TRUE; 2184789Sahrens txg_sync_start(spa->spa_dsl_pool); 2185789Sahrens 2186789Sahrens /* 218710922SJeff.Bonwick@Sun.COM * Wait for all claims to sync. We sync up to the highest 218810922SJeff.Bonwick@Sun.COM * claimed log block birth time so that claimed log blocks 218910922SJeff.Bonwick@Sun.COM * don't appear to be from the future. spa_claim_max_txg 219010922SJeff.Bonwick@Sun.COM * will have been set for us by either zil_check_log_chain() 219110922SJeff.Bonwick@Sun.COM * (invoked from spa_check_logs()) or zil_claim() above. 2192789Sahrens */ 219310922SJeff.Bonwick@Sun.COM txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 21941585Sbonwick 21951585Sbonwick /* 21961635Sbonwick * If the config cache is stale, or we have uninitialized 21971635Sbonwick * metaslabs (see spa_vdev_add()), then update the config. 219810100SLin.Ling@Sun.COM * 219912949SGeorge.Wilson@Sun.COM * If this is a verbatim import, trust the current 220010100SLin.Ling@Sun.COM * in-core spa_config and update the disk labels. 22011585Sbonwick */ 22021635Sbonwick if (config_cache_txg != spa->spa_config_txg || 220312949SGeorge.Wilson@Sun.COM state == SPA_LOAD_IMPORT || 220412949SGeorge.Wilson@Sun.COM state == SPA_LOAD_RECOVER || 220512949SGeorge.Wilson@Sun.COM (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 22061635Sbonwick need_update = B_TRUE; 22071635Sbonwick 22088241SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) 22091635Sbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 22101635Sbonwick need_update = B_TRUE; 22111585Sbonwick 22121585Sbonwick /* 22131635Sbonwick * Update the config cache asychronously in case we're the 22141635Sbonwick * root pool, in which case the config cache isn't writable yet. 22151585Sbonwick */ 22161635Sbonwick if (need_update) 22171635Sbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 22188241SJeff.Bonwick@Sun.COM 22198241SJeff.Bonwick@Sun.COM /* 22208241SJeff.Bonwick@Sun.COM * Check all DTLs to see if anything needs resilvering. 22218241SJeff.Bonwick@Sun.COM */ 222212296SLin.Ling@Sun.COM if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 222312296SLin.Ling@Sun.COM vdev_resilver_needed(rvd, NULL, NULL)) 22248241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 222510298SMatthew.Ahrens@Sun.COM 222610298SMatthew.Ahrens@Sun.COM /* 222710298SMatthew.Ahrens@Sun.COM * Delete any inconsistent datasets. 222810298SMatthew.Ahrens@Sun.COM */ 222910298SMatthew.Ahrens@Sun.COM (void) dmu_objset_find(spa_name(spa), 223010298SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 223110342Schris.kirby@sun.com 223210342Schris.kirby@sun.com /* 223310342Schris.kirby@sun.com * Clean up any stale temporary dataset userrefs. 223410342Schris.kirby@sun.com */ 223510342Schris.kirby@sun.com dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2236789Sahrens } 2237789Sahrens 223811422SMark.Musante@Sun.COM return (0); 2239789Sahrens } 2240789Sahrens 224110921STim.Haley@Sun.COM static int 224210921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 224310921STim.Haley@Sun.COM { 224410921STim.Haley@Sun.COM spa_unload(spa); 224510921STim.Haley@Sun.COM spa_deactivate(spa); 224610921STim.Haley@Sun.COM 224710921STim.Haley@Sun.COM spa->spa_load_max_txg--; 224810921STim.Haley@Sun.COM 224910921STim.Haley@Sun.COM spa_activate(spa, spa_mode_global); 225010921STim.Haley@Sun.COM spa_async_suspend(spa); 225110921STim.Haley@Sun.COM 225211422SMark.Musante@Sun.COM return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 225310921STim.Haley@Sun.COM } 225410921STim.Haley@Sun.COM 225510921STim.Haley@Sun.COM static int 225610921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 225711727SVictor.Latushkin@Sun.COM uint64_t max_request, int rewind_flags) 225810921STim.Haley@Sun.COM { 225910921STim.Haley@Sun.COM nvlist_t *config = NULL; 226010921STim.Haley@Sun.COM int load_error, rewind_error; 226111727SVictor.Latushkin@Sun.COM uint64_t safe_rewind_txg; 226210921STim.Haley@Sun.COM uint64_t min_txg; 226310921STim.Haley@Sun.COM 226411026STim.Haley@Sun.COM if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 226510921STim.Haley@Sun.COM spa->spa_load_max_txg = spa->spa_load_txg; 226611422SMark.Musante@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 226711026STim.Haley@Sun.COM } else { 226810921STim.Haley@Sun.COM spa->spa_load_max_txg = max_request; 226911026STim.Haley@Sun.COM } 227010921STim.Haley@Sun.COM 227111422SMark.Musante@Sun.COM load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 227211422SMark.Musante@Sun.COM mosconfig); 227310921STim.Haley@Sun.COM if (load_error == 0) 227410921STim.Haley@Sun.COM return (0); 227510921STim.Haley@Sun.COM 227610921STim.Haley@Sun.COM if (spa->spa_root_vdev != NULL) 227710921STim.Haley@Sun.COM config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 227810921STim.Haley@Sun.COM 227910921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 228010921STim.Haley@Sun.COM spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 228110921STim.Haley@Sun.COM 228211727SVictor.Latushkin@Sun.COM if (rewind_flags & ZPOOL_NEVER_REWIND) { 228310921STim.Haley@Sun.COM nvlist_free(config); 228410921STim.Haley@Sun.COM return (load_error); 228510921STim.Haley@Sun.COM } 228610921STim.Haley@Sun.COM 228710921STim.Haley@Sun.COM /* Price of rolling back is discarding txgs, including log */ 228810921STim.Haley@Sun.COM if (state == SPA_LOAD_RECOVER) 228911422SMark.Musante@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 229010921STim.Haley@Sun.COM 229111727SVictor.Latushkin@Sun.COM spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 229211727SVictor.Latushkin@Sun.COM safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 229311727SVictor.Latushkin@Sun.COM min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 229411727SVictor.Latushkin@Sun.COM TXG_INITIAL : safe_rewind_txg; 229511727SVictor.Latushkin@Sun.COM 229611727SVictor.Latushkin@Sun.COM /* 229711727SVictor.Latushkin@Sun.COM * Continue as long as we're finding errors, we're still within 229811727SVictor.Latushkin@Sun.COM * the acceptable rewind range, and we're still finding uberblocks 229911727SVictor.Latushkin@Sun.COM */ 230011727SVictor.Latushkin@Sun.COM while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 230111727SVictor.Latushkin@Sun.COM spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 230211727SVictor.Latushkin@Sun.COM if (spa->spa_load_max_txg < safe_rewind_txg) 230310921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_TRUE; 230410921STim.Haley@Sun.COM rewind_error = spa_load_retry(spa, state, mosconfig); 230510921STim.Haley@Sun.COM } 230610921STim.Haley@Sun.COM 230710921STim.Haley@Sun.COM spa->spa_extreme_rewind = B_FALSE; 230810921STim.Haley@Sun.COM spa->spa_load_max_txg = UINT64_MAX; 230910921STim.Haley@Sun.COM 231010921STim.Haley@Sun.COM if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 231110921STim.Haley@Sun.COM spa_config_set(spa, config); 231210921STim.Haley@Sun.COM 231310921STim.Haley@Sun.COM return (state == SPA_LOAD_RECOVER ? rewind_error : load_error); 231410921STim.Haley@Sun.COM } 231510921STim.Haley@Sun.COM 2316789Sahrens /* 2317789Sahrens * Pool Open/Import 2318789Sahrens * 2319789Sahrens * The import case is identical to an open except that the configuration is sent 2320789Sahrens * down from userland, instead of grabbed from the configuration cache. For the 2321789Sahrens * case of an open, the pool configuration will exist in the 23224451Seschrock * POOL_STATE_UNINITIALIZED state. 2323789Sahrens * 2324789Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 2325789Sahrens * the same time open the pool, without having to keep around the spa_t in some 2326789Sahrens * ambiguous state. 2327789Sahrens */ 2328789Sahrens static int 232910921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 233010921STim.Haley@Sun.COM nvlist_t **config) 2331789Sahrens { 2332789Sahrens spa_t *spa; 233312949SGeorge.Wilson@Sun.COM spa_load_state_t state = SPA_LOAD_OPEN; 2334789Sahrens int error; 2335789Sahrens int locked = B_FALSE; 2336789Sahrens 2337789Sahrens *spapp = NULL; 2338789Sahrens 2339789Sahrens /* 2340789Sahrens * As disgusting as this is, we need to support recursive calls to this 2341789Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 2342789Sahrens * up calling spa_open() again. The real fix is to figure out how to 2343789Sahrens * avoid dsl_dir_open() calling this in the first place. 2344789Sahrens */ 2345789Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 2346789Sahrens mutex_enter(&spa_namespace_lock); 2347789Sahrens locked = B_TRUE; 2348789Sahrens } 2349789Sahrens 2350789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2351789Sahrens if (locked) 2352789Sahrens mutex_exit(&spa_namespace_lock); 2353789Sahrens return (ENOENT); 2354789Sahrens } 235510921STim.Haley@Sun.COM 2356789Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 235711819STim.Haley@Sun.COM zpool_rewind_policy_t policy; 235811819STim.Haley@Sun.COM 235911819STim.Haley@Sun.COM zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 236011819STim.Haley@Sun.COM &policy); 236111819STim.Haley@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 236211819STim.Haley@Sun.COM state = SPA_LOAD_RECOVER; 2363789Sahrens 23648241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2365789Sahrens 236610921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 236710921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 236810921STim.Haley@Sun.COM 236910921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 237011727SVictor.Latushkin@Sun.COM policy.zrp_request); 2371789Sahrens 2372789Sahrens if (error == EBADF) { 2373789Sahrens /* 23741986Seschrock * If vdev_validate() returns failure (indicated by 23751986Seschrock * EBADF), it indicates that one of the vdevs indicates 23761986Seschrock * that the pool has been exported or destroyed. If 23771986Seschrock * this is the case, the config cache is out of sync and 23781986Seschrock * we should remove the pool from the namespace. 2379789Sahrens */ 2380789Sahrens spa_unload(spa); 2381789Sahrens spa_deactivate(spa); 23826643Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2383789Sahrens spa_remove(spa); 2384789Sahrens if (locked) 2385789Sahrens mutex_exit(&spa_namespace_lock); 2386789Sahrens return (ENOENT); 23871544Seschrock } 23881544Seschrock 23891544Seschrock if (error) { 2390789Sahrens /* 2391789Sahrens * We can't open the pool, but we still have useful 2392789Sahrens * information: the state of each vdev after the 2393789Sahrens * attempted vdev_open(). Return this to the user. 2394789Sahrens */ 239512949SGeorge.Wilson@Sun.COM if (config != NULL && spa->spa_config) { 239610921STim.Haley@Sun.COM VERIFY(nvlist_dup(spa->spa_config, config, 239710921STim.Haley@Sun.COM KM_SLEEP) == 0); 239812949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist(*config, 239912949SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_LOAD_INFO, 240012949SGeorge.Wilson@Sun.COM spa->spa_load_info) == 0); 240112949SGeorge.Wilson@Sun.COM } 2402789Sahrens spa_unload(spa); 2403789Sahrens spa_deactivate(spa); 240410921STim.Haley@Sun.COM spa->spa_last_open_failed = error; 2405789Sahrens if (locked) 2406789Sahrens mutex_exit(&spa_namespace_lock); 2407789Sahrens *spapp = NULL; 2408789Sahrens return (error); 2409789Sahrens } 2410789Sahrens } 2411789Sahrens 2412789Sahrens spa_open_ref(spa, tag); 24134451Seschrock 241410921STim.Haley@Sun.COM if (config != NULL) 241510921STim.Haley@Sun.COM *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 241610921STim.Haley@Sun.COM 241712949SGeorge.Wilson@Sun.COM /* 241812949SGeorge.Wilson@Sun.COM * If we've recovered the pool, pass back any information we 241912949SGeorge.Wilson@Sun.COM * gathered while doing the load. 242012949SGeorge.Wilson@Sun.COM */ 242112949SGeorge.Wilson@Sun.COM if (state == SPA_LOAD_RECOVER) { 242212949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 242312949SGeorge.Wilson@Sun.COM spa->spa_load_info) == 0); 242412949SGeorge.Wilson@Sun.COM } 242512949SGeorge.Wilson@Sun.COM 242611026STim.Haley@Sun.COM if (locked) { 242711026STim.Haley@Sun.COM spa->spa_last_open_failed = 0; 242811026STim.Haley@Sun.COM spa->spa_last_ubsync_txg = 0; 242911026STim.Haley@Sun.COM spa->spa_load_txg = 0; 2430789Sahrens mutex_exit(&spa_namespace_lock); 243111026STim.Haley@Sun.COM } 2432789Sahrens 2433789Sahrens *spapp = spa; 2434789Sahrens 2435789Sahrens return (0); 2436789Sahrens } 2437789Sahrens 2438789Sahrens int 243910921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 244010921STim.Haley@Sun.COM nvlist_t **config) 244110921STim.Haley@Sun.COM { 244210921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, policy, config)); 244310921STim.Haley@Sun.COM } 244410921STim.Haley@Sun.COM 244510921STim.Haley@Sun.COM int 2446789Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2447789Sahrens { 244810921STim.Haley@Sun.COM return (spa_open_common(name, spapp, tag, NULL, NULL)); 2449789Sahrens } 2450789Sahrens 24511544Seschrock /* 24521544Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 24531544Seschrock * preventing it from being exported or destroyed. 24541544Seschrock */ 24551544Seschrock spa_t * 24561544Seschrock spa_inject_addref(char *name) 24571544Seschrock { 24581544Seschrock spa_t *spa; 24591544Seschrock 24601544Seschrock mutex_enter(&spa_namespace_lock); 24611544Seschrock if ((spa = spa_lookup(name)) == NULL) { 24621544Seschrock mutex_exit(&spa_namespace_lock); 24631544Seschrock return (NULL); 24641544Seschrock } 24651544Seschrock spa->spa_inject_ref++; 24661544Seschrock mutex_exit(&spa_namespace_lock); 24671544Seschrock 24681544Seschrock return (spa); 24691544Seschrock } 24701544Seschrock 24711544Seschrock void 24721544Seschrock spa_inject_delref(spa_t *spa) 24731544Seschrock { 24741544Seschrock mutex_enter(&spa_namespace_lock); 24751544Seschrock spa->spa_inject_ref--; 24761544Seschrock mutex_exit(&spa_namespace_lock); 24771544Seschrock } 24781544Seschrock 24795450Sbrendan /* 24805450Sbrendan * Add spares device information to the nvlist. 24815450Sbrendan */ 24822082Seschrock static void 24832082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config) 24842082Seschrock { 24852082Seschrock nvlist_t **spares; 24862082Seschrock uint_t i, nspares; 24872082Seschrock nvlist_t *nvroot; 24882082Seschrock uint64_t guid; 24892082Seschrock vdev_stat_t *vs; 24902082Seschrock uint_t vsc; 24913377Seschrock uint64_t pool; 24922082Seschrock 24939425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 24949425SEric.Schrock@Sun.COM 24955450Sbrendan if (spa->spa_spares.sav_count == 0) 24962082Seschrock return; 24972082Seschrock 24982082Seschrock VERIFY(nvlist_lookup_nvlist(config, 24992082Seschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 25005450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 25012082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 25022082Seschrock if (nspares != 0) { 25032082Seschrock VERIFY(nvlist_add_nvlist_array(nvroot, 25042082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 25052082Seschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 25062082Seschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 25072082Seschrock 25082082Seschrock /* 25092082Seschrock * Go through and find any spares which have since been 25102082Seschrock * repurposed as an active spare. If this is the case, update 25112082Seschrock * their status appropriately. 25122082Seschrock */ 25132082Seschrock for (i = 0; i < nspares; i++) { 25142082Seschrock VERIFY(nvlist_lookup_uint64(spares[i], 25152082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0); 25167214Slling if (spa_spare_exists(guid, &pool, NULL) && 25177214Slling pool != 0ULL) { 25182082Seschrock VERIFY(nvlist_lookup_uint64_array( 251912296SLin.Ling@Sun.COM spares[i], ZPOOL_CONFIG_VDEV_STATS, 25202082Seschrock (uint64_t **)&vs, &vsc) == 0); 25212082Seschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 25222082Seschrock vs->vs_aux = VDEV_AUX_SPARED; 25232082Seschrock } 25242082Seschrock } 25252082Seschrock } 25262082Seschrock } 25272082Seschrock 25285450Sbrendan /* 25295450Sbrendan * Add l2cache device information to the nvlist, including vdev stats. 25305450Sbrendan */ 25315450Sbrendan static void 25325450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 25335450Sbrendan { 25345450Sbrendan nvlist_t **l2cache; 25355450Sbrendan uint_t i, j, nl2cache; 25365450Sbrendan nvlist_t *nvroot; 25375450Sbrendan uint64_t guid; 25385450Sbrendan vdev_t *vd; 25395450Sbrendan vdev_stat_t *vs; 25405450Sbrendan uint_t vsc; 25415450Sbrendan 25429425SEric.Schrock@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 25439425SEric.Schrock@Sun.COM 25445450Sbrendan if (spa->spa_l2cache.sav_count == 0) 25455450Sbrendan return; 25465450Sbrendan 25475450Sbrendan VERIFY(nvlist_lookup_nvlist(config, 25485450Sbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 25495450Sbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 25505450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 25515450Sbrendan if (nl2cache != 0) { 25525450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 25535450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 25545450Sbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 25555450Sbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 25565450Sbrendan 25575450Sbrendan /* 25585450Sbrendan * Update level 2 cache device stats. 25595450Sbrendan */ 25605450Sbrendan 25615450Sbrendan for (i = 0; i < nl2cache; i++) { 25625450Sbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 25635450Sbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 25645450Sbrendan 25655450Sbrendan vd = NULL; 25665450Sbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 25675450Sbrendan if (guid == 25685450Sbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 25695450Sbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 25705450Sbrendan break; 25715450Sbrendan } 25725450Sbrendan } 25735450Sbrendan ASSERT(vd != NULL); 25745450Sbrendan 25755450Sbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 257612296SLin.Ling@Sun.COM ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 257712296SLin.Ling@Sun.COM == 0); 25785450Sbrendan vdev_get_stats(vd, vs); 25795450Sbrendan } 25805450Sbrendan } 25815450Sbrendan } 25825450Sbrendan 2583789Sahrens int 25841544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 2585789Sahrens { 2586789Sahrens int error; 2587789Sahrens spa_t *spa; 2588789Sahrens 2589789Sahrens *config = NULL; 259010921STim.Haley@Sun.COM error = spa_open_common(name, &spa, FTAG, NULL, config); 2591789Sahrens 25929425SEric.Schrock@Sun.COM if (spa != NULL) { 25939425SEric.Schrock@Sun.COM /* 25949425SEric.Schrock@Sun.COM * This still leaves a window of inconsistency where the spares 25959425SEric.Schrock@Sun.COM * or l2cache devices could change and the config would be 25969425SEric.Schrock@Sun.COM * self-inconsistent. 25979425SEric.Schrock@Sun.COM */ 25989425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 25999425SEric.Schrock@Sun.COM 26009425SEric.Schrock@Sun.COM if (*config != NULL) { 260112817STim.Haley@Sun.COM uint64_t loadtimes[2]; 260212817STim.Haley@Sun.COM 260312817STim.Haley@Sun.COM loadtimes[0] = spa->spa_loaded_ts.tv_sec; 260412817STim.Haley@Sun.COM loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 260512817STim.Haley@Sun.COM VERIFY(nvlist_add_uint64_array(*config, 260612817STim.Haley@Sun.COM ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 260712817STim.Haley@Sun.COM 26087754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_uint64(*config, 26099425SEric.Schrock@Sun.COM ZPOOL_CONFIG_ERRCOUNT, 26109425SEric.Schrock@Sun.COM spa_get_errlog_size(spa)) == 0); 26119425SEric.Schrock@Sun.COM 26129425SEric.Schrock@Sun.COM if (spa_suspended(spa)) 26139425SEric.Schrock@Sun.COM VERIFY(nvlist_add_uint64(*config, 26149425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SUSPENDED, 26159425SEric.Schrock@Sun.COM spa->spa_failmode) == 0); 26169425SEric.Schrock@Sun.COM 26179425SEric.Schrock@Sun.COM spa_add_spares(spa, *config); 26189425SEric.Schrock@Sun.COM spa_add_l2cache(spa, *config); 26199425SEric.Schrock@Sun.COM } 26202082Seschrock } 26212082Seschrock 26221544Seschrock /* 26231544Seschrock * We want to get the alternate root even for faulted pools, so we cheat 26241544Seschrock * and call spa_lookup() directly. 26251544Seschrock */ 26261544Seschrock if (altroot) { 26271544Seschrock if (spa == NULL) { 26281544Seschrock mutex_enter(&spa_namespace_lock); 26291544Seschrock spa = spa_lookup(name); 26301544Seschrock if (spa) 26311544Seschrock spa_altroot(spa, altroot, buflen); 26321544Seschrock else 26331544Seschrock altroot[0] = '\0'; 26341544Seschrock spa = NULL; 26351544Seschrock mutex_exit(&spa_namespace_lock); 26361544Seschrock } else { 26371544Seschrock spa_altroot(spa, altroot, buflen); 26381544Seschrock } 26391544Seschrock } 26401544Seschrock 26419425SEric.Schrock@Sun.COM if (spa != NULL) { 26429425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 2643789Sahrens spa_close(spa, FTAG); 26449425SEric.Schrock@Sun.COM } 2645789Sahrens 2646789Sahrens return (error); 2647789Sahrens } 2648789Sahrens 2649789Sahrens /* 26505450Sbrendan * Validate that the auxiliary device array is well formed. We must have an 26515450Sbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 26525450Sbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 26535450Sbrendan * specified, as long as they are well-formed. 26542082Seschrock */ 26552082Seschrock static int 26565450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 26575450Sbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 26585450Sbrendan vdev_labeltype_t label) 26592082Seschrock { 26605450Sbrendan nvlist_t **dev; 26615450Sbrendan uint_t i, ndev; 26622082Seschrock vdev_t *vd; 26632082Seschrock int error; 26642082Seschrock 26657754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 26667754SJeff.Bonwick@Sun.COM 26672082Seschrock /* 26685450Sbrendan * It's acceptable to have no devs specified. 26692082Seschrock */ 26705450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 26712082Seschrock return (0); 26722082Seschrock 26735450Sbrendan if (ndev == 0) 26742082Seschrock return (EINVAL); 26752082Seschrock 26762082Seschrock /* 26775450Sbrendan * Make sure the pool is formatted with a version that supports this 26785450Sbrendan * device type. 26792082Seschrock */ 26805450Sbrendan if (spa_version(spa) < version) 26812082Seschrock return (ENOTSUP); 26822082Seschrock 26833377Seschrock /* 26845450Sbrendan * Set the pending device list so we correctly handle device in-use 26853377Seschrock * checking. 26863377Seschrock */ 26875450Sbrendan sav->sav_pending = dev; 26885450Sbrendan sav->sav_npending = ndev; 26895450Sbrendan 26905450Sbrendan for (i = 0; i < ndev; i++) { 26915450Sbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 26922082Seschrock mode)) != 0) 26933377Seschrock goto out; 26942082Seschrock 26952082Seschrock if (!vd->vdev_ops->vdev_op_leaf) { 26962082Seschrock vdev_free(vd); 26973377Seschrock error = EINVAL; 26983377Seschrock goto out; 26992082Seschrock } 27002082Seschrock 27015450Sbrendan /* 27027754SJeff.Bonwick@Sun.COM * The L2ARC currently only supports disk devices in 27037754SJeff.Bonwick@Sun.COM * kernel context. For user-level testing, we allow it. 27045450Sbrendan */ 27057754SJeff.Bonwick@Sun.COM #ifdef _KERNEL 27065450Sbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 27075450Sbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 27085450Sbrendan error = ENOTBLK; 27095450Sbrendan goto out; 27105450Sbrendan } 27117754SJeff.Bonwick@Sun.COM #endif 27122082Seschrock vd->vdev_top = vd; 27133377Seschrock 27143377Seschrock if ((error = vdev_open(vd)) == 0 && 27155450Sbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 27165450Sbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 27173377Seschrock vd->vdev_guid) == 0); 27182082Seschrock } 27192082Seschrock 27202082Seschrock vdev_free(vd); 27213377Seschrock 27225450Sbrendan if (error && 27235450Sbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 27243377Seschrock goto out; 27253377Seschrock else 27263377Seschrock error = 0; 27272082Seschrock } 27282082Seschrock 27293377Seschrock out: 27305450Sbrendan sav->sav_pending = NULL; 27315450Sbrendan sav->sav_npending = 0; 27323377Seschrock return (error); 27332082Seschrock } 27342082Seschrock 27355450Sbrendan static int 27365450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 27375450Sbrendan { 27385450Sbrendan int error; 27395450Sbrendan 27407754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 27417754SJeff.Bonwick@Sun.COM 27425450Sbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 27435450Sbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 27445450Sbrendan VDEV_LABEL_SPARE)) != 0) { 27455450Sbrendan return (error); 27465450Sbrendan } 27475450Sbrendan 27485450Sbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 27495450Sbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 27505450Sbrendan VDEV_LABEL_L2CACHE)); 27515450Sbrendan } 27525450Sbrendan 27535450Sbrendan static void 27545450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 27555450Sbrendan const char *config) 27565450Sbrendan { 27575450Sbrendan int i; 27585450Sbrendan 27595450Sbrendan if (sav->sav_config != NULL) { 27605450Sbrendan nvlist_t **olddevs; 27615450Sbrendan uint_t oldndevs; 27625450Sbrendan nvlist_t **newdevs; 27635450Sbrendan 27645450Sbrendan /* 27655450Sbrendan * Generate new dev list by concatentating with the 27665450Sbrendan * current dev list. 27675450Sbrendan */ 27685450Sbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 27695450Sbrendan &olddevs, &oldndevs) == 0); 27705450Sbrendan 27715450Sbrendan newdevs = kmem_alloc(sizeof (void *) * 27725450Sbrendan (ndevs + oldndevs), KM_SLEEP); 27735450Sbrendan for (i = 0; i < oldndevs; i++) 27745450Sbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 27755450Sbrendan KM_SLEEP) == 0); 27765450Sbrendan for (i = 0; i < ndevs; i++) 27775450Sbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 27785450Sbrendan KM_SLEEP) == 0); 27795450Sbrendan 27805450Sbrendan VERIFY(nvlist_remove(sav->sav_config, config, 27815450Sbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 27825450Sbrendan 27835450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 27845450Sbrendan config, newdevs, ndevs + oldndevs) == 0); 27855450Sbrendan for (i = 0; i < oldndevs + ndevs; i++) 27865450Sbrendan nvlist_free(newdevs[i]); 27875450Sbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 27885450Sbrendan } else { 27895450Sbrendan /* 27905450Sbrendan * Generate a new dev list. 27915450Sbrendan */ 27925450Sbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 27935450Sbrendan KM_SLEEP) == 0); 27945450Sbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 27955450Sbrendan devs, ndevs) == 0); 27965450Sbrendan } 27975450Sbrendan } 27985450Sbrendan 27995450Sbrendan /* 28005450Sbrendan * Stop and drop level 2 ARC devices 28015450Sbrendan */ 28025450Sbrendan void 28035450Sbrendan spa_l2cache_drop(spa_t *spa) 28045450Sbrendan { 28055450Sbrendan vdev_t *vd; 28065450Sbrendan int i; 28075450Sbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 28085450Sbrendan 28095450Sbrendan for (i = 0; i < sav->sav_count; i++) { 28105450Sbrendan uint64_t pool; 28115450Sbrendan 28125450Sbrendan vd = sav->sav_vdevs[i]; 28135450Sbrendan ASSERT(vd != NULL); 28145450Sbrendan 28158241SJeff.Bonwick@Sun.COM if (spa_l2cache_exists(vd->vdev_guid, &pool) && 28168241SJeff.Bonwick@Sun.COM pool != 0ULL && l2arc_vdev_present(vd)) 28175450Sbrendan l2arc_remove_vdev(vd); 28185450Sbrendan if (vd->vdev_isl2cache) 28195450Sbrendan spa_l2cache_remove(vd); 28205450Sbrendan vdev_clear_stats(vd); 28215450Sbrendan (void) vdev_close(vd); 28225450Sbrendan } 28235450Sbrendan } 28245450Sbrendan 28252082Seschrock /* 2826789Sahrens * Pool Creation 2827789Sahrens */ 2828789Sahrens int 28295094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 28307184Stimh const char *history_str, nvlist_t *zplprops) 2831789Sahrens { 2832789Sahrens spa_t *spa; 28335094Slling char *altroot = NULL; 28341635Sbonwick vdev_t *rvd; 2835789Sahrens dsl_pool_t *dp; 2836789Sahrens dmu_tx_t *tx; 28379816SGeorge.Wilson@Sun.COM int error = 0; 2838789Sahrens uint64_t txg = TXG_INITIAL; 28395450Sbrendan nvlist_t **spares, **l2cache; 28405450Sbrendan uint_t nspares, nl2cache; 284112470SMatthew.Ahrens@Sun.COM uint64_t version, obj; 2842789Sahrens 2843789Sahrens /* 2844789Sahrens * If this pool already exists, return failure. 2845789Sahrens */ 2846789Sahrens mutex_enter(&spa_namespace_lock); 2847789Sahrens if (spa_lookup(pool) != NULL) { 2848789Sahrens mutex_exit(&spa_namespace_lock); 2849789Sahrens return (EEXIST); 2850789Sahrens } 2851789Sahrens 2852789Sahrens /* 2853789Sahrens * Allocate a new spa_t structure. 2854789Sahrens */ 28555094Slling (void) nvlist_lookup_string(props, 28565094Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 285710921STim.Haley@Sun.COM spa = spa_add(pool, NULL, altroot); 28588241SJeff.Bonwick@Sun.COM spa_activate(spa, spa_mode_global); 2859789Sahrens 28605094Slling if (props && (error = spa_prop_validate(spa, props))) { 28615094Slling spa_deactivate(spa); 28625094Slling spa_remove(spa); 28636643Seschrock mutex_exit(&spa_namespace_lock); 28645094Slling return (error); 28655094Slling } 28665094Slling 28675094Slling if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 28685094Slling &version) != 0) 28695094Slling version = SPA_VERSION; 28705094Slling ASSERT(version <= SPA_VERSION); 287110922SJeff.Bonwick@Sun.COM 287210922SJeff.Bonwick@Sun.COM spa->spa_first_txg = txg; 287310922SJeff.Bonwick@Sun.COM spa->spa_uberblock.ub_txg = txg - 1; 28745094Slling spa->spa_uberblock.ub_version = version; 2875789Sahrens spa->spa_ubsync = spa->spa_uberblock; 2876789Sahrens 28771635Sbonwick /* 28789234SGeorge.Wilson@Sun.COM * Create "The Godfather" zio to hold all async IOs 28799234SGeorge.Wilson@Sun.COM */ 28809630SJeff.Bonwick@Sun.COM spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 28819630SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 28829234SGeorge.Wilson@Sun.COM 28839234SGeorge.Wilson@Sun.COM /* 28841635Sbonwick * Create the root vdev. 28851635Sbonwick */ 28867754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 28871635Sbonwick 28882082Seschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 28892082Seschrock 28902082Seschrock ASSERT(error != 0 || rvd != NULL); 28912082Seschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 28922082Seschrock 28935913Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 28941635Sbonwick error = EINVAL; 28952082Seschrock 28962082Seschrock if (error == 0 && 28972082Seschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 28985450Sbrendan (error = spa_validate_aux(spa, nvroot, txg, 28992082Seschrock VDEV_ALLOC_ADD)) == 0) { 29009816SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 29019816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(rvd->vdev_child[c]); 29029816SGeorge.Wilson@Sun.COM vdev_expand(rvd->vdev_child[c], txg); 29039816SGeorge.Wilson@Sun.COM } 29041635Sbonwick } 29051635Sbonwick 29067754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 2907789Sahrens 29082082Seschrock if (error != 0) { 2909789Sahrens spa_unload(spa); 2910789Sahrens spa_deactivate(spa); 2911789Sahrens spa_remove(spa); 2912789Sahrens mutex_exit(&spa_namespace_lock); 2913789Sahrens return (error); 2914789Sahrens } 2915789Sahrens 29162082Seschrock /* 29172082Seschrock * Get the list of spares, if specified. 29182082Seschrock */ 29192082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 29202082Seschrock &spares, &nspares) == 0) { 29215450Sbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 29222082Seschrock KM_SLEEP) == 0); 29235450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 29242082Seschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 29257754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29262082Seschrock spa_load_spares(spa); 29277754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29285450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 29295450Sbrendan } 29305450Sbrendan 29315450Sbrendan /* 29325450Sbrendan * Get the list of level 2 cache devices, if specified. 29335450Sbrendan */ 29345450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 29355450Sbrendan &l2cache, &nl2cache) == 0) { 29365450Sbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 29375450Sbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 29385450Sbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 29395450Sbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 29407754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 29415450Sbrendan spa_load_l2cache(spa); 29427754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 29435450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 29442082Seschrock } 29452082Seschrock 29467184Stimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2947789Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 2948789Sahrens 294910956SGeorge.Wilson@Sun.COM /* 295010956SGeorge.Wilson@Sun.COM * Create DDTs (dedup tables). 295110956SGeorge.Wilson@Sun.COM */ 295210956SGeorge.Wilson@Sun.COM ddt_create(spa); 295310956SGeorge.Wilson@Sun.COM 295410956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 295510956SGeorge.Wilson@Sun.COM 2956789Sahrens tx = dmu_tx_create_assigned(dp, txg); 2957789Sahrens 2958789Sahrens /* 2959789Sahrens * Create the pool config object. 2960789Sahrens */ 2961789Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 29627497STim.Haley@Sun.COM DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2963789Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2964789Sahrens 29651544Seschrock if (zap_add(spa->spa_meta_objset, 2966789Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 29671544Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 29681544Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 29691544Seschrock } 2970789Sahrens 297112296SLin.Ling@Sun.COM if (zap_add(spa->spa_meta_objset, 297212296SLin.Ling@Sun.COM DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 297312296SLin.Ling@Sun.COM sizeof (uint64_t), 1, &version, tx) != 0) { 297412296SLin.Ling@Sun.COM cmn_err(CE_PANIC, "failed to add pool version"); 297512296SLin.Ling@Sun.COM } 297612296SLin.Ling@Sun.COM 29775094Slling /* Newly created pools with the right version are always deflated. */ 29785094Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 29795094Slling spa->spa_deflate = TRUE; 29805094Slling if (zap_add(spa->spa_meta_objset, 29815094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 29825094Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 29835094Slling cmn_err(CE_PANIC, "failed to add deflate"); 29845094Slling } 29852082Seschrock } 29862082Seschrock 2987789Sahrens /* 298812470SMatthew.Ahrens@Sun.COM * Create the deferred-free bpobj. Turn off compression 2989789Sahrens * because sync-to-convergence takes longer if the blocksize 2990789Sahrens * keeps changing. 2991789Sahrens */ 299212470SMatthew.Ahrens@Sun.COM obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 299312470SMatthew.Ahrens@Sun.COM dmu_object_set_compress(spa->spa_meta_objset, obj, 299412470SMatthew.Ahrens@Sun.COM ZIO_COMPRESS_OFF, tx); 29951544Seschrock if (zap_add(spa->spa_meta_objset, 299612470SMatthew.Ahrens@Sun.COM DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 299712470SMatthew.Ahrens@Sun.COM sizeof (uint64_t), 1, &obj, tx) != 0) { 299812470SMatthew.Ahrens@Sun.COM cmn_err(CE_PANIC, "failed to add bpobj"); 29991544Seschrock } 300012470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 300112470SMatthew.Ahrens@Sun.COM spa->spa_meta_objset, obj)); 3002789Sahrens 30032926Sek110237 /* 30042926Sek110237 * Create the pool's history object. 30052926Sek110237 */ 30065094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 30075094Slling spa_history_create_obj(spa, tx); 30085094Slling 30095094Slling /* 30105094Slling * Set pool properties. 30115094Slling */ 30125094Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 30135094Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 30145329Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 30159816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 301610922SJeff.Bonwick@Sun.COM 30178525SEric.Schrock@Sun.COM if (props != NULL) { 30188525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 301912296SLin.Ling@Sun.COM spa_sync_props(spa, props, tx); 30208525SEric.Schrock@Sun.COM } 30212926Sek110237 3022789Sahrens dmu_tx_commit(tx); 3023789Sahrens 3024789Sahrens spa->spa_sync_on = B_TRUE; 3025789Sahrens txg_sync_start(spa->spa_dsl_pool); 3026789Sahrens 3027789Sahrens /* 3028789Sahrens * We explicitly wait for the first transaction to complete so that our 3029789Sahrens * bean counters are appropriately updated. 3030789Sahrens */ 3031789Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3032789Sahrens 30336643Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3034789Sahrens 30355094Slling if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 30364715Sek110237 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 30379946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_CREATE); 30384715Sek110237 30398667SGeorge.Wilson@Sun.COM spa->spa_minref = refcount_count(&spa->spa_refcount); 30408667SGeorge.Wilson@Sun.COM 3041789Sahrens mutex_exit(&spa_namespace_lock); 3042789Sahrens 3043789Sahrens return (0); 3044789Sahrens } 3045789Sahrens 30466423Sgw25295 #ifdef _KERNEL 30476423Sgw25295 /* 30489790SLin.Ling@Sun.COM * Get the root pool information from the root disk, then import the root pool 30499790SLin.Ling@Sun.COM * during the system boot up time. 30506423Sgw25295 */ 30519790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 30529790SLin.Ling@Sun.COM 30539790SLin.Ling@Sun.COM static nvlist_t * 30549790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 30556423Sgw25295 { 30569790SLin.Ling@Sun.COM nvlist_t *config; 30576423Sgw25295 nvlist_t *nvtop, *nvroot; 30586423Sgw25295 uint64_t pgid; 30596423Sgw25295 30609790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 30619790SLin.Ling@Sun.COM return (NULL); 30629790SLin.Ling@Sun.COM 30636423Sgw25295 /* 30646423Sgw25295 * Add this top-level vdev to the child array. 30656423Sgw25295 */ 30669790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 30679790SLin.Ling@Sun.COM &nvtop) == 0); 30689790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 30699790SLin.Ling@Sun.COM &pgid) == 0); 30709790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 30716423Sgw25295 30726423Sgw25295 /* 30736423Sgw25295 * Put this pool's top-level vdevs into a root vdev. 30746423Sgw25295 */ 30756423Sgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 30769790SLin.Ling@Sun.COM VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 30779790SLin.Ling@Sun.COM VDEV_TYPE_ROOT) == 0); 30786423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 30796423Sgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 30806423Sgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 30816423Sgw25295 &nvtop, 1) == 0); 30826423Sgw25295 30836423Sgw25295 /* 30846423Sgw25295 * Replace the existing vdev_tree with the new root vdev in 30856423Sgw25295 * this pool's configuration (remove the old, add the new). 30866423Sgw25295 */ 30876423Sgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 30886423Sgw25295 nvlist_free(nvroot); 30899790SLin.Ling@Sun.COM return (config); 30906423Sgw25295 } 30916423Sgw25295 30926423Sgw25295 /* 30939790SLin.Ling@Sun.COM * Walk the vdev tree and see if we can find a device with "better" 30949790SLin.Ling@Sun.COM * configuration. A configuration is "better" if the label on that 30959790SLin.Ling@Sun.COM * device has a more recent txg. 30966423Sgw25295 */ 30979790SLin.Ling@Sun.COM static void 30989790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 30997147Staylor { 31009816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 31019790SLin.Ling@Sun.COM spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 31029790SLin.Ling@Sun.COM 31039790SLin.Ling@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 31049790SLin.Ling@Sun.COM nvlist_t *label; 31059790SLin.Ling@Sun.COM uint64_t label_txg; 31069790SLin.Ling@Sun.COM 31079790SLin.Ling@Sun.COM if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 31089790SLin.Ling@Sun.COM &label) != 0) 31099790SLin.Ling@Sun.COM return; 31109790SLin.Ling@Sun.COM 31119790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 31129790SLin.Ling@Sun.COM &label_txg) == 0); 31139790SLin.Ling@Sun.COM 31149790SLin.Ling@Sun.COM /* 31159790SLin.Ling@Sun.COM * Do we have a better boot device? 31169790SLin.Ling@Sun.COM */ 31179790SLin.Ling@Sun.COM if (label_txg > *txg) { 31189790SLin.Ling@Sun.COM *txg = label_txg; 31199790SLin.Ling@Sun.COM *avd = vd; 31207147Staylor } 31219790SLin.Ling@Sun.COM nvlist_free(label); 31227147Staylor } 31237147Staylor } 31247147Staylor 31256423Sgw25295 /* 31266423Sgw25295 * Import a root pool. 31276423Sgw25295 * 31287147Staylor * For x86. devpath_list will consist of devid and/or physpath name of 31297147Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 31307147Staylor * The GRUB "findroot" command will return the vdev we should boot. 31316423Sgw25295 * 31326423Sgw25295 * For Sparc, devpath_list consists the physpath name of the booting device 31336423Sgw25295 * no matter the rootpool is a single device pool or a mirrored pool. 31346423Sgw25295 * e.g. 31356423Sgw25295 * "/pci@1f,0/ide@d/disk@0,0:a" 31366423Sgw25295 */ 31376423Sgw25295 int 31387147Staylor spa_import_rootpool(char *devpath, char *devid) 31396423Sgw25295 { 31409790SLin.Ling@Sun.COM spa_t *spa; 31419790SLin.Ling@Sun.COM vdev_t *rvd, *bvd, *avd = NULL; 31429790SLin.Ling@Sun.COM nvlist_t *config, *nvtop; 31439790SLin.Ling@Sun.COM uint64_t guid, txg; 31446423Sgw25295 char *pname; 31456423Sgw25295 int error; 31466423Sgw25295 31476423Sgw25295 /* 31489790SLin.Ling@Sun.COM * Read the label from the boot device and generate a configuration. 31496423Sgw25295 */ 315010822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 315110822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL) 315210822SJack.Meng@Sun.COM if (config == NULL) { 315310822SJack.Meng@Sun.COM if (strstr(devpath, "/iscsi/ssd") != NULL) { 315410822SJack.Meng@Sun.COM /* iscsi boot */ 315510822SJack.Meng@Sun.COM get_iscsi_bootpath_phy(devpath); 315610822SJack.Meng@Sun.COM config = spa_generate_rootconf(devpath, devid, &guid); 315710822SJack.Meng@Sun.COM } 315810822SJack.Meng@Sun.COM } 315910822SJack.Meng@Sun.COM #endif 316010822SJack.Meng@Sun.COM if (config == NULL) { 31619790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 31629790SLin.Ling@Sun.COM devpath); 31639790SLin.Ling@Sun.COM return (EIO); 31649790SLin.Ling@Sun.COM } 31659790SLin.Ling@Sun.COM 31669790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 31679790SLin.Ling@Sun.COM &pname) == 0); 31689790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 31696423Sgw25295 31709425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 31719425SEric.Schrock@Sun.COM if ((spa = spa_lookup(pname)) != NULL) { 31729425SEric.Schrock@Sun.COM /* 31739425SEric.Schrock@Sun.COM * Remove the existing root pool from the namespace so that we 31749425SEric.Schrock@Sun.COM * can replace it with the correct config we just read in. 31759425SEric.Schrock@Sun.COM */ 31769425SEric.Schrock@Sun.COM spa_remove(spa); 31779425SEric.Schrock@Sun.COM } 31789425SEric.Schrock@Sun.COM 317910921STim.Haley@Sun.COM spa = spa_add(pname, config, NULL); 31809425SEric.Schrock@Sun.COM spa->spa_is_root = B_TRUE; 318112949SGeorge.Wilson@Sun.COM spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 31829790SLin.Ling@Sun.COM 31839790SLin.Ling@Sun.COM /* 31849790SLin.Ling@Sun.COM * Build up a vdev tree based on the boot device's label config. 31859790SLin.Ling@Sun.COM */ 31869790SLin.Ling@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 31879790SLin.Ling@Sun.COM &nvtop) == 0); 31889790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 31899790SLin.Ling@Sun.COM error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 31909790SLin.Ling@Sun.COM VDEV_ALLOC_ROOTPOOL); 31919790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 31929790SLin.Ling@Sun.COM if (error) { 31939790SLin.Ling@Sun.COM mutex_exit(&spa_namespace_lock); 31949790SLin.Ling@Sun.COM nvlist_free(config); 31959790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 31969790SLin.Ling@Sun.COM pname); 31979790SLin.Ling@Sun.COM return (error); 31989790SLin.Ling@Sun.COM } 31999790SLin.Ling@Sun.COM 32009790SLin.Ling@Sun.COM /* 32019790SLin.Ling@Sun.COM * Get the boot vdev. 32029790SLin.Ling@Sun.COM */ 32039790SLin.Ling@Sun.COM if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 32049790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 32059790SLin.Ling@Sun.COM (u_longlong_t)guid); 32069790SLin.Ling@Sun.COM error = ENOENT; 32079790SLin.Ling@Sun.COM goto out; 32089790SLin.Ling@Sun.COM } 32099790SLin.Ling@Sun.COM 32109790SLin.Ling@Sun.COM /* 32119790SLin.Ling@Sun.COM * Determine if there is a better boot device. 32129790SLin.Ling@Sun.COM */ 32139790SLin.Ling@Sun.COM avd = bvd; 32149790SLin.Ling@Sun.COM spa_alt_rootvdev(rvd, &avd, &txg); 32159790SLin.Ling@Sun.COM if (avd != bvd) { 32169790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 32179790SLin.Ling@Sun.COM "try booting from '%s'", avd->vdev_path); 32189790SLin.Ling@Sun.COM error = EINVAL; 32199790SLin.Ling@Sun.COM goto out; 32209790SLin.Ling@Sun.COM } 32219790SLin.Ling@Sun.COM 32229790SLin.Ling@Sun.COM /* 32239790SLin.Ling@Sun.COM * If the boot device is part of a spare vdev then ensure that 32249790SLin.Ling@Sun.COM * we're booting off the active spare. 32259790SLin.Ling@Sun.COM */ 32269790SLin.Ling@Sun.COM if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 32279790SLin.Ling@Sun.COM !bvd->vdev_isspare) { 32289790SLin.Ling@Sun.COM cmn_err(CE_NOTE, "The boot device is currently spared. Please " 32299790SLin.Ling@Sun.COM "try booting from '%s'", 3230*13037SMark.Musante@Sun.COM bvd->vdev_parent-> 3231*13037SMark.Musante@Sun.COM vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 32329790SLin.Ling@Sun.COM error = EINVAL; 32339790SLin.Ling@Sun.COM goto out; 32349790SLin.Ling@Sun.COM } 32359790SLin.Ling@Sun.COM 32369790SLin.Ling@Sun.COM error = 0; 32379946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 32389790SLin.Ling@Sun.COM out: 32399790SLin.Ling@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 32409790SLin.Ling@Sun.COM vdev_free(rvd); 32419790SLin.Ling@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 32429425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 32436423Sgw25295 32449790SLin.Ling@Sun.COM nvlist_free(config); 32456423Sgw25295 return (error); 32466423Sgw25295 } 32479790SLin.Ling@Sun.COM 32486423Sgw25295 #endif 32496423Sgw25295 32506423Sgw25295 /* 32516423Sgw25295 * Import a non-root pool into the system. 32526423Sgw25295 */ 32536423Sgw25295 int 325412949SGeorge.Wilson@Sun.COM spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 32556423Sgw25295 { 32569425SEric.Schrock@Sun.COM spa_t *spa; 32579425SEric.Schrock@Sun.COM char *altroot = NULL; 325810921STim.Haley@Sun.COM spa_load_state_t state = SPA_LOAD_IMPORT; 325910921STim.Haley@Sun.COM zpool_rewind_policy_t policy; 32609425SEric.Schrock@Sun.COM int error; 32619425SEric.Schrock@Sun.COM nvlist_t *nvroot; 32629425SEric.Schrock@Sun.COM nvlist_t **spares, **l2cache; 32639425SEric.Schrock@Sun.COM uint_t nspares, nl2cache; 32649425SEric.Schrock@Sun.COM 32659425SEric.Schrock@Sun.COM /* 32669425SEric.Schrock@Sun.COM * If a pool with this name exists, return failure. 32679425SEric.Schrock@Sun.COM */ 32689425SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 326911422SMark.Musante@Sun.COM if (spa_lookup(pool) != NULL) { 32709425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 32719425SEric.Schrock@Sun.COM return (EEXIST); 32729425SEric.Schrock@Sun.COM } 32739425SEric.Schrock@Sun.COM 32749425SEric.Schrock@Sun.COM /* 32759425SEric.Schrock@Sun.COM * Create and initialize the spa structure. 32769425SEric.Schrock@Sun.COM */ 32779425SEric.Schrock@Sun.COM (void) nvlist_lookup_string(props, 32789425SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 327910921STim.Haley@Sun.COM spa = spa_add(pool, config, altroot); 328012949SGeorge.Wilson@Sun.COM spa->spa_import_flags = flags; 328112949SGeorge.Wilson@Sun.COM 328212949SGeorge.Wilson@Sun.COM /* 328312949SGeorge.Wilson@Sun.COM * Verbatim import - Take a pool and insert it into the namespace 328412949SGeorge.Wilson@Sun.COM * as if it had been loaded at boot. 328512949SGeorge.Wilson@Sun.COM */ 328612949SGeorge.Wilson@Sun.COM if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 328712949SGeorge.Wilson@Sun.COM if (props != NULL) 328812949SGeorge.Wilson@Sun.COM spa_configfile_set(spa, props, B_FALSE); 328912949SGeorge.Wilson@Sun.COM 329012949SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 329112949SGeorge.Wilson@Sun.COM 329212949SGeorge.Wilson@Sun.COM mutex_exit(&spa_namespace_lock); 329312949SGeorge.Wilson@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 329412949SGeorge.Wilson@Sun.COM 329512949SGeorge.Wilson@Sun.COM return (0); 329612949SGeorge.Wilson@Sun.COM } 329712949SGeorge.Wilson@Sun.COM 32989425SEric.Schrock@Sun.COM spa_activate(spa, spa_mode_global); 32999425SEric.Schrock@Sun.COM 33009425SEric.Schrock@Sun.COM /* 33019630SJeff.Bonwick@Sun.COM * Don't start async tasks until we know everything is healthy. 33029630SJeff.Bonwick@Sun.COM */ 33039630SJeff.Bonwick@Sun.COM spa_async_suspend(spa); 33049630SJeff.Bonwick@Sun.COM 330512949SGeorge.Wilson@Sun.COM zpool_get_rewind_policy(config, &policy); 330612949SGeorge.Wilson@Sun.COM if (policy.zrp_request & ZPOOL_DO_REWIND) 330712949SGeorge.Wilson@Sun.COM state = SPA_LOAD_RECOVER; 330812949SGeorge.Wilson@Sun.COM 33099630SJeff.Bonwick@Sun.COM /* 33109425SEric.Schrock@Sun.COM * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 33119425SEric.Schrock@Sun.COM * because the user-supplied config is actually the one to trust when 33129425SEric.Schrock@Sun.COM * doing an import. 33139425SEric.Schrock@Sun.COM */ 331410921STim.Haley@Sun.COM if (state != SPA_LOAD_RECOVER) 331510921STim.Haley@Sun.COM spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 331612949SGeorge.Wilson@Sun.COM 331710921STim.Haley@Sun.COM error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 331811727SVictor.Latushkin@Sun.COM policy.zrp_request); 331910921STim.Haley@Sun.COM 332010921STim.Haley@Sun.COM /* 332112949SGeorge.Wilson@Sun.COM * Propagate anything learned while loading the pool and pass it 332212949SGeorge.Wilson@Sun.COM * back to caller (i.e. rewind info, missing devices, etc). 332310921STim.Haley@Sun.COM */ 332412949SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 332512949SGeorge.Wilson@Sun.COM spa->spa_load_info) == 0); 33269425SEric.Schrock@Sun.COM 33279425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33289425SEric.Schrock@Sun.COM /* 33299425SEric.Schrock@Sun.COM * Toss any existing sparelist, as it doesn't have any validity 33309425SEric.Schrock@Sun.COM * anymore, and conflicts with spa_has_spare(). 33319425SEric.Schrock@Sun.COM */ 33329425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) { 33339425SEric.Schrock@Sun.COM nvlist_free(spa->spa_spares.sav_config); 33349425SEric.Schrock@Sun.COM spa->spa_spares.sav_config = NULL; 33359425SEric.Schrock@Sun.COM spa_load_spares(spa); 33369425SEric.Schrock@Sun.COM } 33379425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) { 33389425SEric.Schrock@Sun.COM nvlist_free(spa->spa_l2cache.sav_config); 33399425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_config = NULL; 33409425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 33419425SEric.Schrock@Sun.COM } 33429425SEric.Schrock@Sun.COM 33439425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 33449425SEric.Schrock@Sun.COM &nvroot) == 0); 33459425SEric.Schrock@Sun.COM if (error == 0) 33469425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 33479425SEric.Schrock@Sun.COM VDEV_ALLOC_SPARE); 33489425SEric.Schrock@Sun.COM if (error == 0) 33499425SEric.Schrock@Sun.COM error = spa_validate_aux(spa, nvroot, -1ULL, 33509425SEric.Schrock@Sun.COM VDEV_ALLOC_L2CACHE); 33519425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 33529425SEric.Schrock@Sun.COM 33539425SEric.Schrock@Sun.COM if (props != NULL) 33549425SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 33559425SEric.Schrock@Sun.COM 33569425SEric.Schrock@Sun.COM if (error != 0 || (props && spa_writeable(spa) && 33579425SEric.Schrock@Sun.COM (error = spa_prop_set(spa, props)))) { 33589425SEric.Schrock@Sun.COM spa_unload(spa); 33599425SEric.Schrock@Sun.COM spa_deactivate(spa); 33609425SEric.Schrock@Sun.COM spa_remove(spa); 33619425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 33629425SEric.Schrock@Sun.COM return (error); 33639425SEric.Schrock@Sun.COM } 33649425SEric.Schrock@Sun.COM 336512600SLin.Ling@Sun.COM spa_async_resume(spa); 336612600SLin.Ling@Sun.COM 33679425SEric.Schrock@Sun.COM /* 33689425SEric.Schrock@Sun.COM * Override any spares and level 2 cache devices as specified by 33699425SEric.Schrock@Sun.COM * the user, as these may have correct device names/devids, etc. 33709425SEric.Schrock@Sun.COM */ 33719425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 33729425SEric.Schrock@Sun.COM &spares, &nspares) == 0) { 33739425SEric.Schrock@Sun.COM if (spa->spa_spares.sav_config) 33749425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_spares.sav_config, 33759425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 33769425SEric.Schrock@Sun.COM else 33779425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 33789425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 33799425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 33809425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 33819425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33829425SEric.Schrock@Sun.COM spa_load_spares(spa); 33839425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 33849425SEric.Schrock@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 33859425SEric.Schrock@Sun.COM } 33869425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 33879425SEric.Schrock@Sun.COM &l2cache, &nl2cache) == 0) { 33889425SEric.Schrock@Sun.COM if (spa->spa_l2cache.sav_config) 33899425SEric.Schrock@Sun.COM VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 33909425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 33919425SEric.Schrock@Sun.COM else 33929425SEric.Schrock@Sun.COM VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 33939425SEric.Schrock@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 33949425SEric.Schrock@Sun.COM VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 33959425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 33969425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33979425SEric.Schrock@Sun.COM spa_load_l2cache(spa); 33989425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 33999425SEric.Schrock@Sun.COM spa->spa_l2cache.sav_sync = B_TRUE; 34009425SEric.Schrock@Sun.COM } 34019425SEric.Schrock@Sun.COM 340210672SEric.Schrock@Sun.COM /* 340310672SEric.Schrock@Sun.COM * Check for any removed devices. 340410672SEric.Schrock@Sun.COM */ 340510672SEric.Schrock@Sun.COM if (spa->spa_autoreplace) { 340610672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_spares); 340710672SEric.Schrock@Sun.COM spa_aux_check_removed(&spa->spa_l2cache); 340810672SEric.Schrock@Sun.COM } 340910672SEric.Schrock@Sun.COM 34109425SEric.Schrock@Sun.COM if (spa_writeable(spa)) { 34119425SEric.Schrock@Sun.COM /* 34129425SEric.Schrock@Sun.COM * Update the config cache to include the newly-imported pool. 34139425SEric.Schrock@Sun.COM */ 341410100SLin.Ling@Sun.COM spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 34159425SEric.Schrock@Sun.COM } 34169425SEric.Schrock@Sun.COM 34179816SGeorge.Wilson@Sun.COM /* 34189816SGeorge.Wilson@Sun.COM * It's possible that the pool was expanded while it was exported. 34199816SGeorge.Wilson@Sun.COM * We kick off an async task to handle this for us. 34209816SGeorge.Wilson@Sun.COM */ 34219816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 34229816SGeorge.Wilson@Sun.COM 34239425SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 34249946SMark.Musante@Sun.COM spa_history_log_version(spa, LOG_POOL_IMPORT); 34259425SEric.Schrock@Sun.COM 34269425SEric.Schrock@Sun.COM return (0); 34276643Seschrock } 34286643Seschrock 3429789Sahrens nvlist_t * 3430789Sahrens spa_tryimport(nvlist_t *tryconfig) 3431789Sahrens { 3432789Sahrens nvlist_t *config = NULL; 3433789Sahrens char *poolname; 3434789Sahrens spa_t *spa; 3435789Sahrens uint64_t state; 34368680SLin.Ling@Sun.COM int error; 3437789Sahrens 3438789Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3439789Sahrens return (NULL); 3440789Sahrens 3441789Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3442789Sahrens return (NULL); 3443789Sahrens 34441635Sbonwick /* 34451635Sbonwick * Create and initialize the spa structure. 34461635Sbonwick */ 3447789Sahrens mutex_enter(&spa_namespace_lock); 344810921STim.Haley@Sun.COM spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 34498241SJeff.Bonwick@Sun.COM spa_activate(spa, FREAD); 3450789Sahrens 3451789Sahrens /* 34521635Sbonwick * Pass off the heavy lifting to spa_load(). 34531732Sbonwick * Pass TRUE for mosconfig because the user-supplied config 34541732Sbonwick * is actually the one to trust when doing an import. 3455789Sahrens */ 345611422SMark.Musante@Sun.COM error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 3457789Sahrens 3458789Sahrens /* 3459789Sahrens * If 'tryconfig' was at least parsable, return the current config. 3460789Sahrens */ 3461789Sahrens if (spa->spa_root_vdev != NULL) { 3462789Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3463789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3464789Sahrens poolname) == 0); 3465789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3466789Sahrens state) == 0); 34673975Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 34683975Sek110237 spa->spa_uberblock.ub_timestamp) == 0); 34692082Seschrock 34702082Seschrock /* 34716423Sgw25295 * If the bootfs property exists on this pool then we 34726423Sgw25295 * copy it out so that external consumers can tell which 34736423Sgw25295 * pools are bootable. 34746423Sgw25295 */ 34758680SLin.Ling@Sun.COM if ((!error || error == EEXIST) && spa->spa_bootfs) { 34766423Sgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 34776423Sgw25295 34786423Sgw25295 /* 34796423Sgw25295 * We have to play games with the name since the 34806423Sgw25295 * pool was opened as TRYIMPORT_NAME. 34816423Sgw25295 */ 34827754SJeff.Bonwick@Sun.COM if (dsl_dsobj_to_dsname(spa_name(spa), 34836423Sgw25295 spa->spa_bootfs, tmpname) == 0) { 34846423Sgw25295 char *cp; 34856423Sgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 34866423Sgw25295 34876423Sgw25295 cp = strchr(tmpname, '/'); 34886423Sgw25295 if (cp == NULL) { 34896423Sgw25295 (void) strlcpy(dsname, tmpname, 34906423Sgw25295 MAXPATHLEN); 34916423Sgw25295 } else { 34926423Sgw25295 (void) snprintf(dsname, MAXPATHLEN, 34936423Sgw25295 "%s/%s", poolname, ++cp); 34946423Sgw25295 } 34956423Sgw25295 VERIFY(nvlist_add_string(config, 34966423Sgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 34976423Sgw25295 kmem_free(dsname, MAXPATHLEN); 34986423Sgw25295 } 34996423Sgw25295 kmem_free(tmpname, MAXPATHLEN); 35006423Sgw25295 } 35016423Sgw25295 35026423Sgw25295 /* 35035450Sbrendan * Add the list of hot spares and level 2 cache devices. 35042082Seschrock */ 35059425SEric.Schrock@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 35062082Seschrock spa_add_spares(spa, config); 35075450Sbrendan spa_add_l2cache(spa, config); 35089425SEric.Schrock@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 3509789Sahrens } 3510789Sahrens 3511789Sahrens spa_unload(spa); 3512789Sahrens spa_deactivate(spa); 3513789Sahrens spa_remove(spa); 3514789Sahrens mutex_exit(&spa_namespace_lock); 3515789Sahrens 3516789Sahrens return (config); 3517789Sahrens } 3518789Sahrens 3519789Sahrens /* 3520789Sahrens * Pool export/destroy 3521789Sahrens * 3522789Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 3523789Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 3524789Sahrens * update the pool state and sync all the labels to disk, removing the 35258211SGeorge.Wilson@Sun.COM * configuration from the cache afterwards. If the 'hardforce' flag is set, then 35268211SGeorge.Wilson@Sun.COM * we don't sync the labels or remove the configuration cache. 3527789Sahrens */ 3528789Sahrens static int 35297214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 35308211SGeorge.Wilson@Sun.COM boolean_t force, boolean_t hardforce) 3531789Sahrens { 3532789Sahrens spa_t *spa; 3533789Sahrens 35341775Sbillm if (oldconfig) 35351775Sbillm *oldconfig = NULL; 35361775Sbillm 35378241SJeff.Bonwick@Sun.COM if (!(spa_mode_global & FWRITE)) 3538789Sahrens return (EROFS); 3539789Sahrens 3540789Sahrens mutex_enter(&spa_namespace_lock); 3541789Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3542789Sahrens mutex_exit(&spa_namespace_lock); 3543789Sahrens return (ENOENT); 3544789Sahrens } 3545789Sahrens 3546789Sahrens /* 35471544Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 35481544Seschrock * reacquire the namespace lock, and see if we can export. 35491544Seschrock */ 35501544Seschrock spa_open_ref(spa, FTAG); 35511544Seschrock mutex_exit(&spa_namespace_lock); 35521544Seschrock spa_async_suspend(spa); 35531544Seschrock mutex_enter(&spa_namespace_lock); 35541544Seschrock spa_close(spa, FTAG); 35551544Seschrock 35561544Seschrock /* 3557789Sahrens * The pool will be in core if it's openable, 3558789Sahrens * in which case we can modify its state. 3559789Sahrens */ 3560789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 3561789Sahrens /* 3562789Sahrens * Objsets may be open only because they're dirty, so we 3563789Sahrens * have to force it to sync before checking spa_refcnt. 3564789Sahrens */ 3565789Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 3566789Sahrens 35671544Seschrock /* 35681544Seschrock * A pool cannot be exported or destroyed if there are active 35691544Seschrock * references. If we are resetting a pool, allow references by 35701544Seschrock * fault injection handlers. 35711544Seschrock */ 35721544Seschrock if (!spa_refcount_zero(spa) || 35731544Seschrock (spa->spa_inject_ref != 0 && 35741544Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 35751544Seschrock spa_async_resume(spa); 3576789Sahrens mutex_exit(&spa_namespace_lock); 3577789Sahrens return (EBUSY); 3578789Sahrens } 3579789Sahrens 3580789Sahrens /* 35817214Slling * A pool cannot be exported if it has an active shared spare. 35827214Slling * This is to prevent other pools stealing the active spare 35837214Slling * from an exported pool. At user's own will, such pool can 35847214Slling * be forcedly exported. 35857214Slling */ 35867214Slling if (!force && new_state == POOL_STATE_EXPORTED && 35877214Slling spa_has_active_shared_spare(spa)) { 35887214Slling spa_async_resume(spa); 35897214Slling mutex_exit(&spa_namespace_lock); 35907214Slling return (EXDEV); 35917214Slling } 35927214Slling 35937214Slling /* 3594789Sahrens * We want this to be reflected on every label, 3595789Sahrens * so mark them all dirty. spa_unload() will do the 3596789Sahrens * final sync that pushes these changes out. 3597789Sahrens */ 35988211SGeorge.Wilson@Sun.COM if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 35997754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 36001544Seschrock spa->spa_state = new_state; 360112296SLin.Ling@Sun.COM spa->spa_final_txg = spa_last_synced_txg(spa) + 360212296SLin.Ling@Sun.COM TXG_DEFER_SIZE + 1; 36031544Seschrock vdev_config_dirty(spa->spa_root_vdev); 36047754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 36051544Seschrock } 3606789Sahrens } 3607789Sahrens 36084451Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 36094451Seschrock 3610789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3611789Sahrens spa_unload(spa); 3612789Sahrens spa_deactivate(spa); 3613789Sahrens } 3614789Sahrens 36151775Sbillm if (oldconfig && spa->spa_config) 36161775Sbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 36171775Sbillm 36181544Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 36198211SGeorge.Wilson@Sun.COM if (!hardforce) 36208211SGeorge.Wilson@Sun.COM spa_config_sync(spa, B_TRUE, B_TRUE); 36211544Seschrock spa_remove(spa); 36221544Seschrock } 3623789Sahrens mutex_exit(&spa_namespace_lock); 3624789Sahrens 3625789Sahrens return (0); 3626789Sahrens } 3627789Sahrens 3628789Sahrens /* 3629789Sahrens * Destroy a storage pool. 3630789Sahrens */ 3631789Sahrens int 3632789Sahrens spa_destroy(char *pool) 3633789Sahrens { 36348211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 36358211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 3636789Sahrens } 3637789Sahrens 3638789Sahrens /* 3639789Sahrens * Export a storage pool. 3640789Sahrens */ 3641789Sahrens int 36428211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 36438211SGeorge.Wilson@Sun.COM boolean_t hardforce) 3644789Sahrens { 36458211SGeorge.Wilson@Sun.COM return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 36468211SGeorge.Wilson@Sun.COM force, hardforce)); 3647789Sahrens } 3648789Sahrens 3649789Sahrens /* 36501544Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 36511544Seschrock * from the namespace in any way. 36521544Seschrock */ 36531544Seschrock int 36541544Seschrock spa_reset(char *pool) 36551544Seschrock { 36567214Slling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 36578211SGeorge.Wilson@Sun.COM B_FALSE, B_FALSE)); 36581544Seschrock } 36591544Seschrock 36601544Seschrock /* 3661789Sahrens * ========================================================================== 3662789Sahrens * Device manipulation 3663789Sahrens * ========================================================================== 3664789Sahrens */ 3665789Sahrens 3666789Sahrens /* 36674527Sperrin * Add a device to a storage pool. 3668789Sahrens */ 3669789Sahrens int 3670789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 3671789Sahrens { 367210594SGeorge.Wilson@Sun.COM uint64_t txg, id; 36738241SJeff.Bonwick@Sun.COM int error; 3674789Sahrens vdev_t *rvd = spa->spa_root_vdev; 36751585Sbonwick vdev_t *vd, *tvd; 36765450Sbrendan nvlist_t **spares, **l2cache; 36775450Sbrendan uint_t nspares, nl2cache; 3678789Sahrens 3679789Sahrens txg = spa_vdev_enter(spa); 3680789Sahrens 36812082Seschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 36822082Seschrock VDEV_ALLOC_ADD)) != 0) 36832082Seschrock return (spa_vdev_exit(spa, NULL, txg, error)); 36842082Seschrock 36857754SJeff.Bonwick@Sun.COM spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 3686789Sahrens 36875450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 36885450Sbrendan &nspares) != 0) 36892082Seschrock nspares = 0; 36902082Seschrock 36915450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 36925450Sbrendan &nl2cache) != 0) 36935450Sbrendan nl2cache = 0; 36945450Sbrendan 36957754SJeff.Bonwick@Sun.COM if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 36962082Seschrock return (spa_vdev_exit(spa, vd, txg, EINVAL)); 36977754SJeff.Bonwick@Sun.COM 36987754SJeff.Bonwick@Sun.COM if (vd->vdev_children != 0 && 36997754SJeff.Bonwick@Sun.COM (error = vdev_create(vd, txg, B_FALSE)) != 0) 37007754SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, vd, txg, error)); 37012082Seschrock 37023377Seschrock /* 37035450Sbrendan * We must validate the spares and l2cache devices after checking the 37045450Sbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 37053377Seschrock */ 37067754SJeff.Bonwick@Sun.COM if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 37073377Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 37083377Seschrock 37093377Seschrock /* 37103377Seschrock * Transfer each new top-level vdev from vd to rvd. 37113377Seschrock */ 37128241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 371310594SGeorge.Wilson@Sun.COM 371410594SGeorge.Wilson@Sun.COM /* 371510594SGeorge.Wilson@Sun.COM * Set the vdev id to the first hole, if one exists. 371610594SGeorge.Wilson@Sun.COM */ 371710594SGeorge.Wilson@Sun.COM for (id = 0; id < rvd->vdev_children; id++) { 371810594SGeorge.Wilson@Sun.COM if (rvd->vdev_child[id]->vdev_ishole) { 371910594SGeorge.Wilson@Sun.COM vdev_free(rvd->vdev_child[id]); 372010594SGeorge.Wilson@Sun.COM break; 372110594SGeorge.Wilson@Sun.COM } 372210594SGeorge.Wilson@Sun.COM } 37233377Seschrock tvd = vd->vdev_child[c]; 37243377Seschrock vdev_remove_child(vd, tvd); 372510594SGeorge.Wilson@Sun.COM tvd->vdev_id = id; 37263377Seschrock vdev_add_child(rvd, tvd); 37273377Seschrock vdev_config_dirty(tvd); 37283377Seschrock } 37293377Seschrock 37302082Seschrock if (nspares != 0) { 37315450Sbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 37325450Sbrendan ZPOOL_CONFIG_SPARES); 37332082Seschrock spa_load_spares(spa); 37345450Sbrendan spa->spa_spares.sav_sync = B_TRUE; 37355450Sbrendan } 37365450Sbrendan 37375450Sbrendan if (nl2cache != 0) { 37385450Sbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 37395450Sbrendan ZPOOL_CONFIG_L2CACHE); 37405450Sbrendan spa_load_l2cache(spa); 37415450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 3742789Sahrens } 3743789Sahrens 3744789Sahrens /* 37451585Sbonwick * We have to be careful when adding new vdevs to an existing pool. 37461585Sbonwick * If other threads start allocating from these vdevs before we 37471585Sbonwick * sync the config cache, and we lose power, then upon reboot we may 37481585Sbonwick * fail to open the pool because there are DVAs that the config cache 37491585Sbonwick * can't translate. Therefore, we first add the vdevs without 37501585Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 37511635Sbonwick * and then let spa_config_update() initialize the new metaslabs. 37521585Sbonwick * 37531585Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 37541585Sbonwick * if we lose power at any point in this sequence, the remaining 37551585Sbonwick * steps will be completed the next time we load the pool. 3756789Sahrens */ 37571635Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 37581585Sbonwick 37591635Sbonwick mutex_enter(&spa_namespace_lock); 37601635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 37611635Sbonwick mutex_exit(&spa_namespace_lock); 3762789Sahrens 37631635Sbonwick return (0); 3764789Sahrens } 3765789Sahrens 3766789Sahrens /* 3767789Sahrens * Attach a device to a mirror. The arguments are the path to any device 3768789Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 3769789Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 3770789Sahrens * 3771789Sahrens * If 'replacing' is specified, the new device is intended to replace the 3772789Sahrens * existing device; in this case the two devices are made into their own 37734451Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 3774789Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 3775789Sahrens * extra rules: you can't attach to it after it's been created, and upon 3776789Sahrens * completion of resilvering, the first disk (the one being replaced) 3777789Sahrens * is automatically detached. 3778789Sahrens */ 3779789Sahrens int 37801544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3781789Sahrens { 378212296SLin.Ling@Sun.COM uint64_t txg, dtl_max_txg; 3783789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3784789Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 37852082Seschrock vdev_ops_t *pvops; 37867313SEric.Kustarz@Sun.COM char *oldvdpath, *newvdpath; 37877313SEric.Kustarz@Sun.COM int newvd_isspare; 37887313SEric.Kustarz@Sun.COM int error; 3789789Sahrens 3790789Sahrens txg = spa_vdev_enter(spa); 3791789Sahrens 37926643Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3793789Sahrens 3794789Sahrens if (oldvd == NULL) 3795789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3796789Sahrens 37971585Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 37981585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 37991585Sbonwick 3800789Sahrens pvd = oldvd->vdev_parent; 3801789Sahrens 38022082Seschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 38034451Seschrock VDEV_ALLOC_ADD)) != 0) 38044451Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 38054451Seschrock 38064451Seschrock if (newrootvd->vdev_children != 1) 3807789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3808789Sahrens 3809789Sahrens newvd = newrootvd->vdev_child[0]; 3810789Sahrens 3811789Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 3812789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3813789Sahrens 38142082Seschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3815789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 3816789Sahrens 38174527Sperrin /* 38184527Sperrin * Spares can't replace logs 38194527Sperrin */ 38207326SEric.Schrock@Sun.COM if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 38214527Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38224527Sperrin 38232082Seschrock if (!replacing) { 38242082Seschrock /* 38252082Seschrock * For attach, the only allowable parent is a mirror or the root 38262082Seschrock * vdev. 38272082Seschrock */ 38282082Seschrock if (pvd->vdev_ops != &vdev_mirror_ops && 38292082Seschrock pvd->vdev_ops != &vdev_root_ops) 38302082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38312082Seschrock 38322082Seschrock pvops = &vdev_mirror_ops; 38332082Seschrock } else { 38342082Seschrock /* 38352082Seschrock * Active hot spares can only be replaced by inactive hot 38362082Seschrock * spares. 38372082Seschrock */ 38382082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 3839*13037SMark.Musante@Sun.COM oldvd->vdev_isspare && 38402082Seschrock !spa_has_spare(spa, newvd->vdev_guid)) 38412082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 38422082Seschrock 38432082Seschrock /* 38442082Seschrock * If the source is a hot spare, and the parent isn't already a 38452082Seschrock * spare, then we want to create a new hot spare. Otherwise, we 38463377Seschrock * want to create a replacing vdev. The user is not allowed to 38473377Seschrock * attach to a spared vdev child unless the 'isspare' state is 38483377Seschrock * the same (spare replaces spare, non-spare replaces 38493377Seschrock * non-spare). 38502082Seschrock */ 3851*13037SMark.Musante@Sun.COM if (pvd->vdev_ops == &vdev_replacing_ops && 3852*13037SMark.Musante@Sun.COM spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 38532082Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3854*13037SMark.Musante@Sun.COM } else if (pvd->vdev_ops == &vdev_spare_ops && 3855*13037SMark.Musante@Sun.COM newvd->vdev_isspare != oldvd->vdev_isspare) { 38563377Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3857*13037SMark.Musante@Sun.COM } 3858*13037SMark.Musante@Sun.COM 3859*13037SMark.Musante@Sun.COM if (newvd->vdev_isspare) 38602082Seschrock pvops = &vdev_spare_ops; 38612082Seschrock else 38622082Seschrock pvops = &vdev_replacing_ops; 38632082Seschrock } 38642082Seschrock 38651175Slling /* 38669816SGeorge.Wilson@Sun.COM * Make sure the new device is big enough. 38671175Slling */ 38689816SGeorge.Wilson@Sun.COM if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3869789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3870789Sahrens 38711732Sbonwick /* 38721732Sbonwick * The new device cannot have a higher alignment requirement 38731732Sbonwick * than the top-level vdev. 38741732Sbonwick */ 38751732Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3876789Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3877789Sahrens 3878789Sahrens /* 3879789Sahrens * If this is an in-place replacement, update oldvd's path and devid 3880789Sahrens * to make it distinguishable from newvd, and unopenable from now on. 3881789Sahrens */ 3882789Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3883789Sahrens spa_strfree(oldvd->vdev_path); 3884789Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3885789Sahrens KM_SLEEP); 3886789Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 3887789Sahrens newvd->vdev_path, "old"); 3888789Sahrens if (oldvd->vdev_devid != NULL) { 3889789Sahrens spa_strfree(oldvd->vdev_devid); 3890789Sahrens oldvd->vdev_devid = NULL; 3891789Sahrens } 3892789Sahrens } 3893789Sahrens 3894*13037SMark.Musante@Sun.COM /* mark the device being resilvered */ 3895*13037SMark.Musante@Sun.COM newvd->vdev_resilvering = B_TRUE; 3896*13037SMark.Musante@Sun.COM 3897789Sahrens /* 38982082Seschrock * If the parent is not a mirror, or if we're replacing, insert the new 38992082Seschrock * mirror/replacing/spare vdev above oldvd. 3900789Sahrens */ 3901789Sahrens if (pvd->vdev_ops != pvops) 3902789Sahrens pvd = vdev_add_parent(oldvd, pvops); 3903789Sahrens 3904789Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 3905789Sahrens ASSERT(pvd->vdev_ops == pvops); 3906789Sahrens ASSERT(oldvd->vdev_parent == pvd); 3907789Sahrens 3908789Sahrens /* 3909789Sahrens * Extract the new device from its root and add it to pvd. 3910789Sahrens */ 3911789Sahrens vdev_remove_child(newrootvd, newvd); 3912789Sahrens newvd->vdev_id = pvd->vdev_children; 391310594SGeorge.Wilson@Sun.COM newvd->vdev_crtxg = oldvd->vdev_crtxg; 3914789Sahrens vdev_add_child(pvd, newvd); 3915789Sahrens 3916789Sahrens tvd = newvd->vdev_top; 3917789Sahrens ASSERT(pvd->vdev_top == tvd); 3918789Sahrens ASSERT(tvd->vdev_parent == rvd); 3919789Sahrens 3920789Sahrens vdev_config_dirty(tvd); 3921789Sahrens 3922789Sahrens /* 392312296SLin.Ling@Sun.COM * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 392412296SLin.Ling@Sun.COM * for any dmu_sync-ed blocks. It will propagate upward when 392512296SLin.Ling@Sun.COM * spa_vdev_exit() calls vdev_dtl_reassess(). 3926789Sahrens */ 392712296SLin.Ling@Sun.COM dtl_max_txg = txg + TXG_CONCURRENT_STATES; 392812296SLin.Ling@Sun.COM 392912296SLin.Ling@Sun.COM vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 393012296SLin.Ling@Sun.COM dtl_max_txg - TXG_INITIAL); 3931789Sahrens 39329425SEric.Schrock@Sun.COM if (newvd->vdev_isspare) { 39333377Seschrock spa_spare_activate(newvd); 39349425SEric.Schrock@Sun.COM spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 39359425SEric.Schrock@Sun.COM } 39369425SEric.Schrock@Sun.COM 39377754SJeff.Bonwick@Sun.COM oldvdpath = spa_strdup(oldvd->vdev_path); 39387754SJeff.Bonwick@Sun.COM newvdpath = spa_strdup(newvd->vdev_path); 39397313SEric.Kustarz@Sun.COM newvd_isspare = newvd->vdev_isspare; 39401544Seschrock 3941789Sahrens /* 3942789Sahrens * Mark newvd's DTL dirty in this txg. 3943789Sahrens */ 39441732Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 3945789Sahrens 394612296SLin.Ling@Sun.COM /* 394712296SLin.Ling@Sun.COM * Restart the resilver 394812296SLin.Ling@Sun.COM */ 394912296SLin.Ling@Sun.COM dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 395012296SLin.Ling@Sun.COM 395112296SLin.Ling@Sun.COM /* 395212296SLin.Ling@Sun.COM * Commit the config 395312296SLin.Ling@Sun.COM */ 395412296SLin.Ling@Sun.COM (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 395512296SLin.Ling@Sun.COM 395612296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL, 395712296SLin.Ling@Sun.COM "%s vdev=%s %s vdev=%s", 39589946SMark.Musante@Sun.COM replacing && newvd_isspare ? "spare in" : 39599946SMark.Musante@Sun.COM replacing ? "replace" : "attach", newvdpath, 39609946SMark.Musante@Sun.COM replacing ? "for" : "to", oldvdpath); 39617313SEric.Kustarz@Sun.COM 39627313SEric.Kustarz@Sun.COM spa_strfree(oldvdpath); 39637313SEric.Kustarz@Sun.COM spa_strfree(newvdpath); 39647313SEric.Kustarz@Sun.COM 396512865Slori.alt@oracle.com if (spa->spa_bootfs) 396612865Slori.alt@oracle.com spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 396712865Slori.alt@oracle.com 3968789Sahrens return (0); 3969789Sahrens } 3970789Sahrens 3971789Sahrens /* 3972789Sahrens * Detach a device from a mirror or replacing vdev. 3973789Sahrens * If 'replace_done' is specified, only detach if the parent 3974789Sahrens * is a replacing vdev. 3975789Sahrens */ 3976789Sahrens int 39778241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3978789Sahrens { 3979789Sahrens uint64_t txg; 39808241SJeff.Bonwick@Sun.COM int error; 3981789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3982789Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 39832082Seschrock boolean_t unspare = B_FALSE; 39842082Seschrock uint64_t unspare_guid; 398511422SMark.Musante@Sun.COM char *vdpath; 3986789Sahrens 3987789Sahrens txg = spa_vdev_enter(spa); 3988789Sahrens 39896643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3990789Sahrens 3991789Sahrens if (vd == NULL) 3992789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3993789Sahrens 39941585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 39951585Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 39961585Sbonwick 3997789Sahrens pvd = vd->vdev_parent; 3998789Sahrens 3999789Sahrens /* 40008241SJeff.Bonwick@Sun.COM * If the parent/child relationship is not as expected, don't do it. 40018241SJeff.Bonwick@Sun.COM * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 40028241SJeff.Bonwick@Sun.COM * vdev that's replacing B with C. The user's intent in replacing 40038241SJeff.Bonwick@Sun.COM * is to go from M(A,B) to M(A,C). If the user decides to cancel 40048241SJeff.Bonwick@Sun.COM * the replace by detaching C, the expected behavior is to end up 40058241SJeff.Bonwick@Sun.COM * M(A,B). But suppose that right after deciding to detach C, 40068241SJeff.Bonwick@Sun.COM * the replacement of B completes. We would have M(A,C), and then 40078241SJeff.Bonwick@Sun.COM * ask to detach C, which would leave us with just A -- not what 40088241SJeff.Bonwick@Sun.COM * the user wanted. To prevent this, we make sure that the 40098241SJeff.Bonwick@Sun.COM * parent/child relationship hasn't changed -- in this example, 40108241SJeff.Bonwick@Sun.COM * that C's parent is still the replacing vdev R. 40118241SJeff.Bonwick@Sun.COM */ 40128241SJeff.Bonwick@Sun.COM if (pvd->vdev_guid != pguid && pguid != 0) 40138241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 40148241SJeff.Bonwick@Sun.COM 40158241SJeff.Bonwick@Sun.COM /* 4016*13037SMark.Musante@Sun.COM * Only 'replacing' or 'spare' vdevs can be replaced. 4017789Sahrens */ 4018*13037SMark.Musante@Sun.COM if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4019*13037SMark.Musante@Sun.COM pvd->vdev_ops != &vdev_spare_ops) 4020*13037SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 40212082Seschrock 40222082Seschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 40234577Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4024789Sahrens 4025789Sahrens /* 40262082Seschrock * Only mirror, replacing, and spare vdevs support detach. 4027789Sahrens */ 4028789Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 40292082Seschrock pvd->vdev_ops != &vdev_mirror_ops && 40302082Seschrock pvd->vdev_ops != &vdev_spare_ops) 4031789Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4032789Sahrens 4033789Sahrens /* 40348241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 40358241SJeff.Bonwick@Sun.COM * we cannot safely detach it. 4036789Sahrens */ 40378241SJeff.Bonwick@Sun.COM if (vdev_dtl_required(vd)) 4038789Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4039789Sahrens 40408241SJeff.Bonwick@Sun.COM ASSERT(pvd->vdev_children >= 2); 40418241SJeff.Bonwick@Sun.COM 4042789Sahrens /* 40436673Seschrock * If we are detaching the second disk from a replacing vdev, then 40446673Seschrock * check to see if we changed the original vdev's path to have "/old" 40456673Seschrock * at the end in spa_vdev_attach(). If so, undo that change now. 40466673Seschrock */ 4047*13037SMark.Musante@Sun.COM if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4048*13037SMark.Musante@Sun.COM vd->vdev_path != NULL) { 4049*13037SMark.Musante@Sun.COM size_t len = strlen(vd->vdev_path); 4050*13037SMark.Musante@Sun.COM 4051*13037SMark.Musante@Sun.COM for (int c = 0; c < pvd->vdev_children; c++) { 4052*13037SMark.Musante@Sun.COM cvd = pvd->vdev_child[c]; 4053*13037SMark.Musante@Sun.COM 4054*13037SMark.Musante@Sun.COM if (cvd == vd || cvd->vdev_path == NULL) 4055*13037SMark.Musante@Sun.COM continue; 4056*13037SMark.Musante@Sun.COM 4057*13037SMark.Musante@Sun.COM if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4058*13037SMark.Musante@Sun.COM strcmp(cvd->vdev_path + len, "/old") == 0) { 4059*13037SMark.Musante@Sun.COM spa_strfree(cvd->vdev_path); 4060*13037SMark.Musante@Sun.COM cvd->vdev_path = spa_strdup(vd->vdev_path); 4061*13037SMark.Musante@Sun.COM break; 4062*13037SMark.Musante@Sun.COM } 40636673Seschrock } 40646673Seschrock } 40656673Seschrock 40666673Seschrock /* 40672082Seschrock * If we are detaching the original disk from a spare, then it implies 40682082Seschrock * that the spare should become a real disk, and be removed from the 40692082Seschrock * active spare list for the pool. 40702082Seschrock */ 40712082Seschrock if (pvd->vdev_ops == &vdev_spare_ops && 4072*13037SMark.Musante@Sun.COM vd->vdev_id == 0 && 4073*13037SMark.Musante@Sun.COM pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 40742082Seschrock unspare = B_TRUE; 40752082Seschrock 40762082Seschrock /* 4077789Sahrens * Erase the disk labels so the disk can be used for other things. 4078789Sahrens * This must be done after all other error cases are handled, 4079789Sahrens * but before we disembowel vd (so we can still do I/O to it). 4080789Sahrens * But if we can't do it, don't treat the error as fatal -- 4081789Sahrens * it may be that the unwritability of the disk is the reason 4082789Sahrens * it's being detached! 4083789Sahrens */ 40843377Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4085789Sahrens 4086789Sahrens /* 4087789Sahrens * Remove vd from its parent and compact the parent's children. 4088789Sahrens */ 4089789Sahrens vdev_remove_child(pvd, vd); 4090789Sahrens vdev_compact_children(pvd); 4091789Sahrens 4092789Sahrens /* 4093789Sahrens * Remember one of the remaining children so we can get tvd below. 4094789Sahrens */ 4095*13037SMark.Musante@Sun.COM cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4096789Sahrens 4097789Sahrens /* 40982082Seschrock * If we need to remove the remaining child from the list of hot spares, 40998241SJeff.Bonwick@Sun.COM * do it now, marking the vdev as no longer a spare in the process. 41008241SJeff.Bonwick@Sun.COM * We must do this before vdev_remove_parent(), because that can 41018241SJeff.Bonwick@Sun.COM * change the GUID if it creates a new toplevel GUID. For a similar 41028241SJeff.Bonwick@Sun.COM * reason, we must remove the spare now, in the same txg as the detach; 41038241SJeff.Bonwick@Sun.COM * otherwise someone could attach a new sibling, change the GUID, and 41048241SJeff.Bonwick@Sun.COM * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 41052082Seschrock */ 41062082Seschrock if (unspare) { 41072082Seschrock ASSERT(cvd->vdev_isspare); 41083377Seschrock spa_spare_remove(cvd); 41092082Seschrock unspare_guid = cvd->vdev_guid; 41108241SJeff.Bonwick@Sun.COM (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4111*13037SMark.Musante@Sun.COM cvd->vdev_unspare = B_TRUE; 41122082Seschrock } 41132082Seschrock 41142082Seschrock /* 4115789Sahrens * If the parent mirror/replacing vdev only has one child, 4116789Sahrens * the parent is no longer needed. Remove it from the tree. 4117789Sahrens */ 4118*13037SMark.Musante@Sun.COM if (pvd->vdev_children == 1) { 4119*13037SMark.Musante@Sun.COM if (pvd->vdev_ops == &vdev_spare_ops) 4120*13037SMark.Musante@Sun.COM cvd->vdev_unspare = B_FALSE; 4121789Sahrens vdev_remove_parent(cvd); 4122*13037SMark.Musante@Sun.COM cvd->vdev_resilvering = B_FALSE; 4123*13037SMark.Musante@Sun.COM } 4124*13037SMark.Musante@Sun.COM 4125789Sahrens 4126789Sahrens /* 4127789Sahrens * We don't set tvd until now because the parent we just removed 4128789Sahrens * may have been the previous top-level vdev. 4129789Sahrens */ 4130789Sahrens tvd = cvd->vdev_top; 4131789Sahrens ASSERT(tvd->vdev_parent == rvd); 4132789Sahrens 4133789Sahrens /* 41343377Seschrock * Reevaluate the parent vdev state. 4135789Sahrens */ 41364451Seschrock vdev_propagate_state(cvd); 4137789Sahrens 4138789Sahrens /* 41399816SGeorge.Wilson@Sun.COM * If the 'autoexpand' property is set on the pool then automatically 41409816SGeorge.Wilson@Sun.COM * try to expand the size of the pool. For example if the device we 41419816SGeorge.Wilson@Sun.COM * just detached was smaller than the others, it may be possible to 41429816SGeorge.Wilson@Sun.COM * add metaslabs (i.e. grow the pool). We need to reopen the vdev 41439816SGeorge.Wilson@Sun.COM * first so that we can obtain the updated sizes of the leaf vdevs. 4144789Sahrens */ 41459816SGeorge.Wilson@Sun.COM if (spa->spa_autoexpand) { 41469816SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 41479816SGeorge.Wilson@Sun.COM vdev_expand(tvd, txg); 41489816SGeorge.Wilson@Sun.COM } 4149789Sahrens 4150789Sahrens vdev_config_dirty(tvd); 4151789Sahrens 4152789Sahrens /* 41533377Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 41543377Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 41553377Seschrock * But first make sure we're not on any *other* txg's DTL list, to 41563377Seschrock * prevent vd from being accessed after it's freed. 4157789Sahrens */ 415811422SMark.Musante@Sun.COM vdpath = spa_strdup(vd->vdev_path); 41598241SJeff.Bonwick@Sun.COM for (int t = 0; t < TXG_SIZE; t++) 4160789Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 41611732Sbonwick vd->vdev_detached = B_TRUE; 41621732Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4163789Sahrens 41644451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 41654451Seschrock 4166*13037SMark.Musante@Sun.COM /* hang on to the spa before we release the lock */ 4167*13037SMark.Musante@Sun.COM spa_open_ref(spa, FTAG); 4168*13037SMark.Musante@Sun.COM 41692082Seschrock error = spa_vdev_exit(spa, vd, txg, 0); 41702082Seschrock 417112296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL, 417211422SMark.Musante@Sun.COM "vdev=%s", vdpath); 417311422SMark.Musante@Sun.COM spa_strfree(vdpath); 417411422SMark.Musante@Sun.COM 41752082Seschrock /* 41763377Seschrock * If this was the removal of the original device in a hot spare vdev, 41773377Seschrock * then we want to go through and remove the device from the hot spare 41783377Seschrock * list of every other pool. 41792082Seschrock */ 41802082Seschrock if (unspare) { 4181*13037SMark.Musante@Sun.COM spa_t *altspa = NULL; 4182*13037SMark.Musante@Sun.COM 41832082Seschrock mutex_enter(&spa_namespace_lock); 4184*13037SMark.Musante@Sun.COM while ((altspa = spa_next(altspa)) != NULL) { 4185*13037SMark.Musante@Sun.COM if (altspa->spa_state != POOL_STATE_ACTIVE || 4186*13037SMark.Musante@Sun.COM altspa == spa) 41872082Seschrock continue; 4188*13037SMark.Musante@Sun.COM 4189*13037SMark.Musante@Sun.COM spa_open_ref(altspa, FTAG); 41907793SJeff.Bonwick@Sun.COM mutex_exit(&spa_namespace_lock); 4191*13037SMark.Musante@Sun.COM (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 41927793SJeff.Bonwick@Sun.COM mutex_enter(&spa_namespace_lock); 4193*13037SMark.Musante@Sun.COM spa_close(altspa, FTAG); 41942082Seschrock } 41952082Seschrock mutex_exit(&spa_namespace_lock); 4196*13037SMark.Musante@Sun.COM 4197*13037SMark.Musante@Sun.COM /* search the rest of the vdevs for spares to remove */ 4198*13037SMark.Musante@Sun.COM spa_vdev_resilver_done(spa); 41992082Seschrock } 42002082Seschrock 4201*13037SMark.Musante@Sun.COM /* all done with the spa; OK to release */ 4202*13037SMark.Musante@Sun.COM mutex_enter(&spa_namespace_lock); 4203*13037SMark.Musante@Sun.COM spa_close(spa, FTAG); 4204*13037SMark.Musante@Sun.COM mutex_exit(&spa_namespace_lock); 4205*13037SMark.Musante@Sun.COM 42062082Seschrock return (error); 42072082Seschrock } 42082082Seschrock 420911422SMark.Musante@Sun.COM /* 421011422SMark.Musante@Sun.COM * Split a set of devices from their mirrors, and create a new pool from them. 421111422SMark.Musante@Sun.COM */ 421211422SMark.Musante@Sun.COM int 421311422SMark.Musante@Sun.COM spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 421411422SMark.Musante@Sun.COM nvlist_t *props, boolean_t exp) 421511422SMark.Musante@Sun.COM { 421611422SMark.Musante@Sun.COM int error = 0; 421711422SMark.Musante@Sun.COM uint64_t txg, *glist; 421811422SMark.Musante@Sun.COM spa_t *newspa; 421911422SMark.Musante@Sun.COM uint_t c, children, lastlog; 422011422SMark.Musante@Sun.COM nvlist_t **child, *nvl, *tmp; 422111422SMark.Musante@Sun.COM dmu_tx_t *tx; 422211422SMark.Musante@Sun.COM char *altroot = NULL; 422311422SMark.Musante@Sun.COM vdev_t *rvd, **vml = NULL; /* vdev modify list */ 422411422SMark.Musante@Sun.COM boolean_t activate_slog; 422511422SMark.Musante@Sun.COM 422611422SMark.Musante@Sun.COM if (!spa_writeable(spa)) 422711422SMark.Musante@Sun.COM return (EROFS); 422811422SMark.Musante@Sun.COM 422911422SMark.Musante@Sun.COM txg = spa_vdev_enter(spa); 423011422SMark.Musante@Sun.COM 423111422SMark.Musante@Sun.COM /* clear the log and flush everything up to now */ 423211422SMark.Musante@Sun.COM activate_slog = spa_passivate_log(spa); 423311422SMark.Musante@Sun.COM (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 423411422SMark.Musante@Sun.COM error = spa_offline_log(spa); 423511422SMark.Musante@Sun.COM txg = spa_vdev_config_enter(spa); 423611422SMark.Musante@Sun.COM 423711422SMark.Musante@Sun.COM if (activate_slog) 423811422SMark.Musante@Sun.COM spa_activate_log(spa); 423911422SMark.Musante@Sun.COM 424011422SMark.Musante@Sun.COM if (error != 0) 424111422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 424211422SMark.Musante@Sun.COM 424311422SMark.Musante@Sun.COM /* check new spa name before going any further */ 424411422SMark.Musante@Sun.COM if (spa_lookup(newname) != NULL) 424511422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 424611422SMark.Musante@Sun.COM 424711422SMark.Musante@Sun.COM /* 424811422SMark.Musante@Sun.COM * scan through all the children to ensure they're all mirrors 424911422SMark.Musante@Sun.COM */ 425011422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 425111422SMark.Musante@Sun.COM nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 425211422SMark.Musante@Sun.COM &children) != 0) 425311422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 425411422SMark.Musante@Sun.COM 425511422SMark.Musante@Sun.COM /* first, check to ensure we've got the right child count */ 425611422SMark.Musante@Sun.COM rvd = spa->spa_root_vdev; 425711422SMark.Musante@Sun.COM lastlog = 0; 425811422SMark.Musante@Sun.COM for (c = 0; c < rvd->vdev_children; c++) { 425911422SMark.Musante@Sun.COM vdev_t *vd = rvd->vdev_child[c]; 426011422SMark.Musante@Sun.COM 426111422SMark.Musante@Sun.COM /* don't count the holes & logs as children */ 426211422SMark.Musante@Sun.COM if (vd->vdev_islog || vd->vdev_ishole) { 426311422SMark.Musante@Sun.COM if (lastlog == 0) 426411422SMark.Musante@Sun.COM lastlog = c; 426511422SMark.Musante@Sun.COM continue; 426611422SMark.Musante@Sun.COM } 426711422SMark.Musante@Sun.COM 426811422SMark.Musante@Sun.COM lastlog = 0; 426911422SMark.Musante@Sun.COM } 427011422SMark.Musante@Sun.COM if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 427111422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 427211422SMark.Musante@Sun.COM 427311422SMark.Musante@Sun.COM /* next, ensure no spare or cache devices are part of the split */ 427411422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 427511422SMark.Musante@Sun.COM nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 427611422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 427711422SMark.Musante@Sun.COM 427811422SMark.Musante@Sun.COM vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 427911422SMark.Musante@Sun.COM glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 428011422SMark.Musante@Sun.COM 428111422SMark.Musante@Sun.COM /* then, loop over each vdev and validate it */ 428211422SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 428311422SMark.Musante@Sun.COM uint64_t is_hole = 0; 428411422SMark.Musante@Sun.COM 428511422SMark.Musante@Sun.COM (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 428611422SMark.Musante@Sun.COM &is_hole); 428711422SMark.Musante@Sun.COM 428811422SMark.Musante@Sun.COM if (is_hole != 0) { 428911422SMark.Musante@Sun.COM if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 429011422SMark.Musante@Sun.COM spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 429111422SMark.Musante@Sun.COM continue; 429211422SMark.Musante@Sun.COM } else { 429311422SMark.Musante@Sun.COM error = EINVAL; 429411422SMark.Musante@Sun.COM break; 429511422SMark.Musante@Sun.COM } 429611422SMark.Musante@Sun.COM } 429711422SMark.Musante@Sun.COM 429811422SMark.Musante@Sun.COM /* which disk is going to be split? */ 429911422SMark.Musante@Sun.COM if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 430011422SMark.Musante@Sun.COM &glist[c]) != 0) { 430111422SMark.Musante@Sun.COM error = EINVAL; 430211422SMark.Musante@Sun.COM break; 430311422SMark.Musante@Sun.COM } 430411422SMark.Musante@Sun.COM 430511422SMark.Musante@Sun.COM /* look it up in the spa */ 430611422SMark.Musante@Sun.COM vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 430711422SMark.Musante@Sun.COM if (vml[c] == NULL) { 430811422SMark.Musante@Sun.COM error = ENODEV; 430911422SMark.Musante@Sun.COM break; 431011422SMark.Musante@Sun.COM } 431111422SMark.Musante@Sun.COM 431211422SMark.Musante@Sun.COM /* make sure there's nothing stopping the split */ 431311422SMark.Musante@Sun.COM if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 431411422SMark.Musante@Sun.COM vml[c]->vdev_islog || 431511422SMark.Musante@Sun.COM vml[c]->vdev_ishole || 431611422SMark.Musante@Sun.COM vml[c]->vdev_isspare || 431711422SMark.Musante@Sun.COM vml[c]->vdev_isl2cache || 431811422SMark.Musante@Sun.COM !vdev_writeable(vml[c]) || 431911497SMark.Musante@Sun.COM vml[c]->vdev_children != 0 || 432011422SMark.Musante@Sun.COM vml[c]->vdev_state != VDEV_STATE_HEALTHY || 432111422SMark.Musante@Sun.COM c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 432211422SMark.Musante@Sun.COM error = EINVAL; 432311422SMark.Musante@Sun.COM break; 432411422SMark.Musante@Sun.COM } 432511422SMark.Musante@Sun.COM 432611422SMark.Musante@Sun.COM if (vdev_dtl_required(vml[c])) { 432711422SMark.Musante@Sun.COM error = EBUSY; 432811422SMark.Musante@Sun.COM break; 432911422SMark.Musante@Sun.COM } 433011422SMark.Musante@Sun.COM 433111422SMark.Musante@Sun.COM /* we need certain info from the top level */ 433211422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 433311422SMark.Musante@Sun.COM vml[c]->vdev_top->vdev_ms_array) == 0); 433411422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 433511422SMark.Musante@Sun.COM vml[c]->vdev_top->vdev_ms_shift) == 0); 433611422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 433711422SMark.Musante@Sun.COM vml[c]->vdev_top->vdev_asize) == 0); 433811422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 433911422SMark.Musante@Sun.COM vml[c]->vdev_top->vdev_ashift) == 0); 434011422SMark.Musante@Sun.COM } 434111422SMark.Musante@Sun.COM 434211422SMark.Musante@Sun.COM if (error != 0) { 434311422SMark.Musante@Sun.COM kmem_free(vml, children * sizeof (vdev_t *)); 434411422SMark.Musante@Sun.COM kmem_free(glist, children * sizeof (uint64_t)); 434511422SMark.Musante@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 434611422SMark.Musante@Sun.COM } 434711422SMark.Musante@Sun.COM 434811422SMark.Musante@Sun.COM /* stop writers from using the disks */ 434911422SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 435011422SMark.Musante@Sun.COM if (vml[c] != NULL) 435111422SMark.Musante@Sun.COM vml[c]->vdev_offline = B_TRUE; 435211422SMark.Musante@Sun.COM } 435311422SMark.Musante@Sun.COM vdev_reopen(spa->spa_root_vdev); 435411422SMark.Musante@Sun.COM 435511422SMark.Musante@Sun.COM /* 435611422SMark.Musante@Sun.COM * Temporarily record the splitting vdevs in the spa config. This 435711422SMark.Musante@Sun.COM * will disappear once the config is regenerated. 435811422SMark.Musante@Sun.COM */ 435911422SMark.Musante@Sun.COM VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 436011422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 436111422SMark.Musante@Sun.COM glist, children) == 0); 436211422SMark.Musante@Sun.COM kmem_free(glist, children * sizeof (uint64_t)); 436311422SMark.Musante@Sun.COM 436411864SMark.Musante@Sun.COM mutex_enter(&spa->spa_props_lock); 436511422SMark.Musante@Sun.COM VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 436611422SMark.Musante@Sun.COM nvl) == 0); 436711864SMark.Musante@Sun.COM mutex_exit(&spa->spa_props_lock); 436811422SMark.Musante@Sun.COM spa->spa_config_splitting = nvl; 436911422SMark.Musante@Sun.COM vdev_config_dirty(spa->spa_root_vdev); 437011422SMark.Musante@Sun.COM 437111422SMark.Musante@Sun.COM /* configure and create the new pool */ 437211422SMark.Musante@Sun.COM VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 437311422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 437411422SMark.Musante@Sun.COM exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 437511422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 437611422SMark.Musante@Sun.COM spa_version(spa)) == 0); 437711422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 437811422SMark.Musante@Sun.COM spa->spa_config_txg) == 0); 437911422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 438011422SMark.Musante@Sun.COM spa_generate_guid(NULL)) == 0); 438111422SMark.Musante@Sun.COM (void) nvlist_lookup_string(props, 438211422SMark.Musante@Sun.COM zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 438311422SMark.Musante@Sun.COM 438411497SMark.Musante@Sun.COM /* add the new pool to the namespace */ 438511422SMark.Musante@Sun.COM newspa = spa_add(newname, config, altroot); 438611422SMark.Musante@Sun.COM newspa->spa_config_txg = spa->spa_config_txg; 438711422SMark.Musante@Sun.COM spa_set_log_state(newspa, SPA_LOG_CLEAR); 438811422SMark.Musante@Sun.COM 438911422SMark.Musante@Sun.COM /* release the spa config lock, retaining the namespace lock */ 439011422SMark.Musante@Sun.COM spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 439111422SMark.Musante@Sun.COM 439211422SMark.Musante@Sun.COM if (zio_injection_enabled) 439311422SMark.Musante@Sun.COM zio_handle_panic_injection(spa, FTAG, 1); 439411422SMark.Musante@Sun.COM 439511422SMark.Musante@Sun.COM spa_activate(newspa, spa_mode_global); 439611422SMark.Musante@Sun.COM spa_async_suspend(newspa); 439711422SMark.Musante@Sun.COM 439811422SMark.Musante@Sun.COM /* create the new pool from the disks of the original pool */ 439911422SMark.Musante@Sun.COM error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 440011422SMark.Musante@Sun.COM if (error) 440111422SMark.Musante@Sun.COM goto out; 440211422SMark.Musante@Sun.COM 440311422SMark.Musante@Sun.COM /* if that worked, generate a real config for the new pool */ 440411422SMark.Musante@Sun.COM if (newspa->spa_root_vdev != NULL) { 440511422SMark.Musante@Sun.COM VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 440611422SMark.Musante@Sun.COM NV_UNIQUE_NAME, KM_SLEEP) == 0); 440711422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 440811422SMark.Musante@Sun.COM ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 440911422SMark.Musante@Sun.COM spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 441011422SMark.Musante@Sun.COM B_TRUE)); 441111422SMark.Musante@Sun.COM } 441211422SMark.Musante@Sun.COM 441311422SMark.Musante@Sun.COM /* set the props */ 441411422SMark.Musante@Sun.COM if (props != NULL) { 441511422SMark.Musante@Sun.COM spa_configfile_set(newspa, props, B_FALSE); 441611422SMark.Musante@Sun.COM error = spa_prop_set(newspa, props); 441711422SMark.Musante@Sun.COM if (error) 441811422SMark.Musante@Sun.COM goto out; 441911422SMark.Musante@Sun.COM } 442011422SMark.Musante@Sun.COM 442111422SMark.Musante@Sun.COM /* flush everything */ 442211422SMark.Musante@Sun.COM txg = spa_vdev_config_enter(newspa); 442311422SMark.Musante@Sun.COM vdev_config_dirty(newspa->spa_root_vdev); 442411422SMark.Musante@Sun.COM (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 442511422SMark.Musante@Sun.COM 442611422SMark.Musante@Sun.COM if (zio_injection_enabled) 442711422SMark.Musante@Sun.COM zio_handle_panic_injection(spa, FTAG, 2); 442811422SMark.Musante@Sun.COM 442911422SMark.Musante@Sun.COM spa_async_resume(newspa); 443011422SMark.Musante@Sun.COM 443111422SMark.Musante@Sun.COM /* finally, update the original pool's config */ 443211422SMark.Musante@Sun.COM txg = spa_vdev_config_enter(spa); 443311422SMark.Musante@Sun.COM tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 443411422SMark.Musante@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 443511422SMark.Musante@Sun.COM if (error != 0) 443611422SMark.Musante@Sun.COM dmu_tx_abort(tx); 443711422SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 443811422SMark.Musante@Sun.COM if (vml[c] != NULL) { 443911422SMark.Musante@Sun.COM vdev_split(vml[c]); 444011422SMark.Musante@Sun.COM if (error == 0) 444112296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_VDEV_DETACH, 444212296SLin.Ling@Sun.COM spa, tx, "vdev=%s", 444311422SMark.Musante@Sun.COM vml[c]->vdev_path); 444411422SMark.Musante@Sun.COM vdev_free(vml[c]); 444511422SMark.Musante@Sun.COM } 444611422SMark.Musante@Sun.COM } 444711422SMark.Musante@Sun.COM vdev_config_dirty(spa->spa_root_vdev); 444811422SMark.Musante@Sun.COM spa->spa_config_splitting = NULL; 444911422SMark.Musante@Sun.COM nvlist_free(nvl); 445011422SMark.Musante@Sun.COM if (error == 0) 445111422SMark.Musante@Sun.COM dmu_tx_commit(tx); 445211422SMark.Musante@Sun.COM (void) spa_vdev_exit(spa, NULL, txg, 0); 445311422SMark.Musante@Sun.COM 445411422SMark.Musante@Sun.COM if (zio_injection_enabled) 445511422SMark.Musante@Sun.COM zio_handle_panic_injection(spa, FTAG, 3); 445611422SMark.Musante@Sun.COM 445711422SMark.Musante@Sun.COM /* split is complete; log a history record */ 445812296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL, 445911422SMark.Musante@Sun.COM "split new pool %s from pool %s", newname, spa_name(spa)); 446011422SMark.Musante@Sun.COM 446111422SMark.Musante@Sun.COM kmem_free(vml, children * sizeof (vdev_t *)); 446211422SMark.Musante@Sun.COM 446311422SMark.Musante@Sun.COM /* if we're not going to mount the filesystems in userland, export */ 446411422SMark.Musante@Sun.COM if (exp) 446511422SMark.Musante@Sun.COM error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 446611422SMark.Musante@Sun.COM B_FALSE, B_FALSE); 446711422SMark.Musante@Sun.COM 446811422SMark.Musante@Sun.COM return (error); 446911422SMark.Musante@Sun.COM 447011422SMark.Musante@Sun.COM out: 447111422SMark.Musante@Sun.COM spa_unload(newspa); 447211422SMark.Musante@Sun.COM spa_deactivate(newspa); 447311422SMark.Musante@Sun.COM spa_remove(newspa); 447411422SMark.Musante@Sun.COM 447511422SMark.Musante@Sun.COM txg = spa_vdev_config_enter(spa); 447611864SMark.Musante@Sun.COM 447711864SMark.Musante@Sun.COM /* re-online all offlined disks */ 447811864SMark.Musante@Sun.COM for (c = 0; c < children; c++) { 447911864SMark.Musante@Sun.COM if (vml[c] != NULL) 448011864SMark.Musante@Sun.COM vml[c]->vdev_offline = B_FALSE; 448111864SMark.Musante@Sun.COM } 448211864SMark.Musante@Sun.COM vdev_reopen(spa->spa_root_vdev); 448311864SMark.Musante@Sun.COM 448411422SMark.Musante@Sun.COM nvlist_free(spa->spa_config_splitting); 448511422SMark.Musante@Sun.COM spa->spa_config_splitting = NULL; 448611497SMark.Musante@Sun.COM (void) spa_vdev_exit(spa, NULL, txg, error); 448711422SMark.Musante@Sun.COM 448811422SMark.Musante@Sun.COM kmem_free(vml, children * sizeof (vdev_t *)); 448911422SMark.Musante@Sun.COM return (error); 449011422SMark.Musante@Sun.COM } 449111422SMark.Musante@Sun.COM 44927754SJeff.Bonwick@Sun.COM static nvlist_t * 44937754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 44942082Seschrock { 44957754SJeff.Bonwick@Sun.COM for (int i = 0; i < count; i++) { 44967754SJeff.Bonwick@Sun.COM uint64_t guid; 44977754SJeff.Bonwick@Sun.COM 44987754SJeff.Bonwick@Sun.COM VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 44997754SJeff.Bonwick@Sun.COM &guid) == 0); 45007754SJeff.Bonwick@Sun.COM 45017754SJeff.Bonwick@Sun.COM if (guid == target_guid) 45027754SJeff.Bonwick@Sun.COM return (nvpp[i]); 45032082Seschrock } 45042082Seschrock 45057754SJeff.Bonwick@Sun.COM return (NULL); 45065450Sbrendan } 45075450Sbrendan 45087754SJeff.Bonwick@Sun.COM static void 45097754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 45107754SJeff.Bonwick@Sun.COM nvlist_t *dev_to_remove) 45115450Sbrendan { 45127754SJeff.Bonwick@Sun.COM nvlist_t **newdev = NULL; 45137754SJeff.Bonwick@Sun.COM 45147754SJeff.Bonwick@Sun.COM if (count > 1) 45157754SJeff.Bonwick@Sun.COM newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 45167754SJeff.Bonwick@Sun.COM 45177754SJeff.Bonwick@Sun.COM for (int i = 0, j = 0; i < count; i++) { 45187754SJeff.Bonwick@Sun.COM if (dev[i] == dev_to_remove) 45197754SJeff.Bonwick@Sun.COM continue; 45207754SJeff.Bonwick@Sun.COM VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 45215450Sbrendan } 45225450Sbrendan 45237754SJeff.Bonwick@Sun.COM VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 45247754SJeff.Bonwick@Sun.COM VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 45257754SJeff.Bonwick@Sun.COM 45267754SJeff.Bonwick@Sun.COM for (int i = 0; i < count - 1; i++) 45277754SJeff.Bonwick@Sun.COM nvlist_free(newdev[i]); 45287754SJeff.Bonwick@Sun.COM 45297754SJeff.Bonwick@Sun.COM if (count > 1) 45307754SJeff.Bonwick@Sun.COM kmem_free(newdev, (count - 1) * sizeof (void *)); 45315450Sbrendan } 45325450Sbrendan 45335450Sbrendan /* 453410594SGeorge.Wilson@Sun.COM * Evacuate the device. 453510594SGeorge.Wilson@Sun.COM */ 453612296SLin.Ling@Sun.COM static int 453710594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 453810594SGeorge.Wilson@Sun.COM { 453912296SLin.Ling@Sun.COM uint64_t txg; 454010974SJeff.Bonwick@Sun.COM int error = 0; 454110594SGeorge.Wilson@Sun.COM 454210594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 454310594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 454410922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 454510594SGeorge.Wilson@Sun.COM 454610594SGeorge.Wilson@Sun.COM /* 454710594SGeorge.Wilson@Sun.COM * Evacuate the device. We don't hold the config lock as writer 454810594SGeorge.Wilson@Sun.COM * since we need to do I/O but we do keep the 454910594SGeorge.Wilson@Sun.COM * spa_namespace_lock held. Once this completes the device 455010594SGeorge.Wilson@Sun.COM * should no longer have any blocks allocated on it. 455110594SGeorge.Wilson@Sun.COM */ 455210594SGeorge.Wilson@Sun.COM if (vd->vdev_islog) { 455312296SLin.Ling@Sun.COM if (vd->vdev_stat.vs_alloc != 0) 455412296SLin.Ling@Sun.COM error = spa_offline_log(spa); 455510974SJeff.Bonwick@Sun.COM } else { 455612296SLin.Ling@Sun.COM error = ENOTSUP; 455710594SGeorge.Wilson@Sun.COM } 455810594SGeorge.Wilson@Sun.COM 455910974SJeff.Bonwick@Sun.COM if (error) 456010974SJeff.Bonwick@Sun.COM return (error); 456110974SJeff.Bonwick@Sun.COM 456210594SGeorge.Wilson@Sun.COM /* 456310974SJeff.Bonwick@Sun.COM * The evacuation succeeded. Remove any remaining MOS metadata 456410974SJeff.Bonwick@Sun.COM * associated with this vdev, and wait for these changes to sync. 456510594SGeorge.Wilson@Sun.COM */ 456612296SLin.Ling@Sun.COM ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 456710594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 456810594SGeorge.Wilson@Sun.COM vd->vdev_removing = B_TRUE; 456910594SGeorge.Wilson@Sun.COM vdev_dirty(vd, 0, NULL, txg); 457010594SGeorge.Wilson@Sun.COM vdev_config_dirty(vd); 457110594SGeorge.Wilson@Sun.COM spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 457210594SGeorge.Wilson@Sun.COM 457310594SGeorge.Wilson@Sun.COM return (0); 457410594SGeorge.Wilson@Sun.COM } 457510594SGeorge.Wilson@Sun.COM 457610594SGeorge.Wilson@Sun.COM /* 457710594SGeorge.Wilson@Sun.COM * Complete the removal by cleaning up the namespace. 457810594SGeorge.Wilson@Sun.COM */ 457912296SLin.Ling@Sun.COM static void 458010974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 458110594SGeorge.Wilson@Sun.COM { 458210594SGeorge.Wilson@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 458310594SGeorge.Wilson@Sun.COM uint64_t id = vd->vdev_id; 458410594SGeorge.Wilson@Sun.COM boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 458510594SGeorge.Wilson@Sun.COM 458610594SGeorge.Wilson@Sun.COM ASSERT(MUTEX_HELD(&spa_namespace_lock)); 458710594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 458810922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 458910594SGeorge.Wilson@Sun.COM 459012296SLin.Ling@Sun.COM /* 459112296SLin.Ling@Sun.COM * Only remove any devices which are empty. 459212296SLin.Ling@Sun.COM */ 459312296SLin.Ling@Sun.COM if (vd->vdev_stat.vs_alloc != 0) 459412296SLin.Ling@Sun.COM return; 459512296SLin.Ling@Sun.COM 459610594SGeorge.Wilson@Sun.COM (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 459710922SJeff.Bonwick@Sun.COM 459810922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_state_dirty_node)) 459910922SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 460010922SJeff.Bonwick@Sun.COM if (list_link_active(&vd->vdev_config_dirty_node)) 460110922SJeff.Bonwick@Sun.COM vdev_config_clean(vd); 460210922SJeff.Bonwick@Sun.COM 460310594SGeorge.Wilson@Sun.COM vdev_free(vd); 460410594SGeorge.Wilson@Sun.COM 460510594SGeorge.Wilson@Sun.COM if (last_vdev) { 460610594SGeorge.Wilson@Sun.COM vdev_compact_children(rvd); 460710594SGeorge.Wilson@Sun.COM } else { 460810594SGeorge.Wilson@Sun.COM vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 460910594SGeorge.Wilson@Sun.COM vdev_add_child(rvd, vd); 461010594SGeorge.Wilson@Sun.COM } 461112352SLin.Ling@Sun.COM vdev_config_dirty(rvd); 461212352SLin.Ling@Sun.COM 461312352SLin.Ling@Sun.COM /* 461412352SLin.Ling@Sun.COM * Reassess the health of our root vdev. 461512352SLin.Ling@Sun.COM */ 461612352SLin.Ling@Sun.COM vdev_reopen(rvd); 461710594SGeorge.Wilson@Sun.COM } 461810594SGeorge.Wilson@Sun.COM 461910594SGeorge.Wilson@Sun.COM /* 462012296SLin.Ling@Sun.COM * Remove a device from the pool - 462112296SLin.Ling@Sun.COM * 462212296SLin.Ling@Sun.COM * Removing a device from the vdev namespace requires several steps 462312296SLin.Ling@Sun.COM * and can take a significant amount of time. As a result we use 462412296SLin.Ling@Sun.COM * the spa_vdev_config_[enter/exit] functions which allow us to 462512296SLin.Ling@Sun.COM * grab and release the spa_config_lock while still holding the namespace 462612296SLin.Ling@Sun.COM * lock. During each step the configuration is synced out. 462712296SLin.Ling@Sun.COM */ 462812296SLin.Ling@Sun.COM 462912296SLin.Ling@Sun.COM /* 46305450Sbrendan * Remove a device from the pool. Currently, this supports removing only hot 463110594SGeorge.Wilson@Sun.COM * spares, slogs, and level 2 ARC devices. 46325450Sbrendan */ 46335450Sbrendan int 46345450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 46355450Sbrendan { 46365450Sbrendan vdev_t *vd; 463710974SJeff.Bonwick@Sun.COM metaslab_group_t *mg; 46387754SJeff.Bonwick@Sun.COM nvlist_t **spares, **l2cache, *nv; 463910594SGeorge.Wilson@Sun.COM uint64_t txg = 0; 46405450Sbrendan uint_t nspares, nl2cache; 46415450Sbrendan int error = 0; 46428241SJeff.Bonwick@Sun.COM boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 46438241SJeff.Bonwick@Sun.COM 46448241SJeff.Bonwick@Sun.COM if (!locked) 46458241SJeff.Bonwick@Sun.COM txg = spa_vdev_enter(spa); 46465450Sbrendan 46476643Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 46485450Sbrendan 46495450Sbrendan if (spa->spa_spares.sav_vdevs != NULL && 46505450Sbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 46517754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 46527754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 46537754SJeff.Bonwick@Sun.COM /* 46547754SJeff.Bonwick@Sun.COM * Only remove the hot spare if it's not currently in use 46557754SJeff.Bonwick@Sun.COM * in this pool. 46567754SJeff.Bonwick@Sun.COM */ 46577754SJeff.Bonwick@Sun.COM if (vd == NULL || unspare) { 46587754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_spares.sav_config, 46597754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_SPARES, spares, nspares, nv); 46607754SJeff.Bonwick@Sun.COM spa_load_spares(spa); 46617754SJeff.Bonwick@Sun.COM spa->spa_spares.sav_sync = B_TRUE; 46627754SJeff.Bonwick@Sun.COM } else { 46637754SJeff.Bonwick@Sun.COM error = EBUSY; 46647754SJeff.Bonwick@Sun.COM } 46657754SJeff.Bonwick@Sun.COM } else if (spa->spa_l2cache.sav_vdevs != NULL && 46665450Sbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 46677754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 46687754SJeff.Bonwick@Sun.COM (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 46697754SJeff.Bonwick@Sun.COM /* 46707754SJeff.Bonwick@Sun.COM * Cache devices can always be removed. 46717754SJeff.Bonwick@Sun.COM */ 46727754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 46737754SJeff.Bonwick@Sun.COM ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 46745450Sbrendan spa_load_l2cache(spa); 46755450Sbrendan spa->spa_l2cache.sav_sync = B_TRUE; 467610594SGeorge.Wilson@Sun.COM } else if (vd != NULL && vd->vdev_islog) { 467710594SGeorge.Wilson@Sun.COM ASSERT(!locked); 467810922SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 467910594SGeorge.Wilson@Sun.COM 468010594SGeorge.Wilson@Sun.COM /* 468110594SGeorge.Wilson@Sun.COM * XXX - Once we have bp-rewrite this should 468210594SGeorge.Wilson@Sun.COM * become the common case. 468310594SGeorge.Wilson@Sun.COM */ 468410594SGeorge.Wilson@Sun.COM 468510974SJeff.Bonwick@Sun.COM mg = vd->vdev_mg; 468610974SJeff.Bonwick@Sun.COM 468710594SGeorge.Wilson@Sun.COM /* 468810974SJeff.Bonwick@Sun.COM * Stop allocating from this vdev. 468910594SGeorge.Wilson@Sun.COM */ 469010974SJeff.Bonwick@Sun.COM metaslab_group_passivate(mg); 469110594SGeorge.Wilson@Sun.COM 469210922SJeff.Bonwick@Sun.COM /* 469310922SJeff.Bonwick@Sun.COM * Wait for the youngest allocations and frees to sync, 469410922SJeff.Bonwick@Sun.COM * and then wait for the deferral of those frees to finish. 469510922SJeff.Bonwick@Sun.COM */ 469610922SJeff.Bonwick@Sun.COM spa_vdev_config_exit(spa, NULL, 469710922SJeff.Bonwick@Sun.COM txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 469810922SJeff.Bonwick@Sun.COM 469910974SJeff.Bonwick@Sun.COM /* 470010974SJeff.Bonwick@Sun.COM * Attempt to evacuate the vdev. 470110974SJeff.Bonwick@Sun.COM */ 470210974SJeff.Bonwick@Sun.COM error = spa_vdev_remove_evacuate(spa, vd); 470310974SJeff.Bonwick@Sun.COM 470410594SGeorge.Wilson@Sun.COM txg = spa_vdev_config_enter(spa); 470510594SGeorge.Wilson@Sun.COM 470610974SJeff.Bonwick@Sun.COM /* 470710974SJeff.Bonwick@Sun.COM * If we couldn't evacuate the vdev, unwind. 470810974SJeff.Bonwick@Sun.COM */ 470910974SJeff.Bonwick@Sun.COM if (error) { 471010974SJeff.Bonwick@Sun.COM metaslab_group_activate(mg); 471110974SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 471210974SJeff.Bonwick@Sun.COM } 471310974SJeff.Bonwick@Sun.COM 471410974SJeff.Bonwick@Sun.COM /* 471510974SJeff.Bonwick@Sun.COM * Clean up the vdev namespace. 471610974SJeff.Bonwick@Sun.COM */ 471710974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa, vd); 471810594SGeorge.Wilson@Sun.COM 47197754SJeff.Bonwick@Sun.COM } else if (vd != NULL) { 47207754SJeff.Bonwick@Sun.COM /* 47217754SJeff.Bonwick@Sun.COM * Normal vdevs cannot be removed (yet). 47227754SJeff.Bonwick@Sun.COM */ 47237754SJeff.Bonwick@Sun.COM error = ENOTSUP; 47247754SJeff.Bonwick@Sun.COM } else { 47257754SJeff.Bonwick@Sun.COM /* 47267754SJeff.Bonwick@Sun.COM * There is no vdev of any kind with the specified guid. 47277754SJeff.Bonwick@Sun.COM */ 47287754SJeff.Bonwick@Sun.COM error = ENOENT; 47295450Sbrendan } 47302082Seschrock 47318241SJeff.Bonwick@Sun.COM if (!locked) 47328241SJeff.Bonwick@Sun.COM return (spa_vdev_exit(spa, NULL, txg, error)); 47338241SJeff.Bonwick@Sun.COM 47348241SJeff.Bonwick@Sun.COM return (error); 4735789Sahrens } 4736789Sahrens 4737789Sahrens /* 47384451Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 47394451Seschrock * current spared, so we can detach it. 4740789Sahrens */ 47411544Seschrock static vdev_t * 47424451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 4743789Sahrens { 47441544Seschrock vdev_t *newvd, *oldvd; 47459816SGeorge.Wilson@Sun.COM 47469816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 47474451Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 47481544Seschrock if (oldvd != NULL) 47491544Seschrock return (oldvd); 47501544Seschrock } 4751789Sahrens 47524451Seschrock /* 4753*13037SMark.Musante@Sun.COM * Check for a completed replacement. We always consider the first 4754*13037SMark.Musante@Sun.COM * vdev in the list to be the oldest vdev, and the last one to be 4755*13037SMark.Musante@Sun.COM * the newest (see spa_vdev_attach() for how that works). In 4756*13037SMark.Musante@Sun.COM * the case where the newest vdev is faulted, we will not automatically 4757*13037SMark.Musante@Sun.COM * remove it after a resilver completes. This is OK as it will require 4758*13037SMark.Musante@Sun.COM * user intervention to determine which disk the admin wishes to keep. 47594451Seschrock */ 4760*13037SMark.Musante@Sun.COM if (vd->vdev_ops == &vdev_replacing_ops) { 4761*13037SMark.Musante@Sun.COM ASSERT(vd->vdev_children > 1); 4762*13037SMark.Musante@Sun.COM 4763*13037SMark.Musante@Sun.COM newvd = vd->vdev_child[vd->vdev_children - 1]; 47641544Seschrock oldvd = vd->vdev_child[0]; 4765789Sahrens 47668241SJeff.Bonwick@Sun.COM if (vdev_dtl_empty(newvd, DTL_MISSING) && 476711820SVictor.Latushkin@Sun.COM vdev_dtl_empty(newvd, DTL_OUTAGE) && 47688241SJeff.Bonwick@Sun.COM !vdev_dtl_required(oldvd)) 47691544Seschrock return (oldvd); 47701544Seschrock } 4771789Sahrens 47724451Seschrock /* 47734451Seschrock * Check for a completed resilver with the 'unspare' flag set. 47744451Seschrock */ 4775*13037SMark.Musante@Sun.COM if (vd->vdev_ops == &vdev_spare_ops) { 4776*13037SMark.Musante@Sun.COM vdev_t *first = vd->vdev_child[0]; 4777*13037SMark.Musante@Sun.COM vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 4778*13037SMark.Musante@Sun.COM 4779*13037SMark.Musante@Sun.COM if (last->vdev_unspare) { 4780*13037SMark.Musante@Sun.COM oldvd = first; 4781*13037SMark.Musante@Sun.COM newvd = last; 4782*13037SMark.Musante@Sun.COM } else if (first->vdev_unspare) { 4783*13037SMark.Musante@Sun.COM oldvd = last; 4784*13037SMark.Musante@Sun.COM newvd = first; 4785*13037SMark.Musante@Sun.COM } else { 4786*13037SMark.Musante@Sun.COM oldvd = NULL; 4787*13037SMark.Musante@Sun.COM } 4788*13037SMark.Musante@Sun.COM 4789*13037SMark.Musante@Sun.COM if (oldvd != NULL && 47908241SJeff.Bonwick@Sun.COM vdev_dtl_empty(newvd, DTL_MISSING) && 479111820SVictor.Latushkin@Sun.COM vdev_dtl_empty(newvd, DTL_OUTAGE) && 4792*13037SMark.Musante@Sun.COM !vdev_dtl_required(oldvd)) 47934451Seschrock return (oldvd); 4794*13037SMark.Musante@Sun.COM 4795*13037SMark.Musante@Sun.COM /* 4796*13037SMark.Musante@Sun.COM * If there are more than two spares attached to a disk, 4797*13037SMark.Musante@Sun.COM * and those spares are not required, then we want to 4798*13037SMark.Musante@Sun.COM * attempt to free them up now so that they can be used 4799*13037SMark.Musante@Sun.COM * by other pools. Once we're back down to a single 4800*13037SMark.Musante@Sun.COM * disk+spare, we stop removing them. 4801*13037SMark.Musante@Sun.COM */ 4802*13037SMark.Musante@Sun.COM if (vd->vdev_children > 2) { 4803*13037SMark.Musante@Sun.COM newvd = vd->vdev_child[1]; 4804*13037SMark.Musante@Sun.COM 4805*13037SMark.Musante@Sun.COM if (newvd->vdev_isspare && last->vdev_isspare && 4806*13037SMark.Musante@Sun.COM vdev_dtl_empty(last, DTL_MISSING) && 4807*13037SMark.Musante@Sun.COM vdev_dtl_empty(last, DTL_OUTAGE) && 4808*13037SMark.Musante@Sun.COM !vdev_dtl_required(newvd)) 4809*13037SMark.Musante@Sun.COM return (newvd); 48104451Seschrock } 48114451Seschrock } 48124451Seschrock 48131544Seschrock return (NULL); 4814789Sahrens } 4815789Sahrens 48161544Seschrock static void 48174451Seschrock spa_vdev_resilver_done(spa_t *spa) 4818789Sahrens { 48198241SJeff.Bonwick@Sun.COM vdev_t *vd, *pvd, *ppvd; 48208241SJeff.Bonwick@Sun.COM uint64_t guid, sguid, pguid, ppguid; 48218241SJeff.Bonwick@Sun.COM 48228241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4823789Sahrens 48244451Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 48258241SJeff.Bonwick@Sun.COM pvd = vd->vdev_parent; 48268241SJeff.Bonwick@Sun.COM ppvd = pvd->vdev_parent; 48271544Seschrock guid = vd->vdev_guid; 48288241SJeff.Bonwick@Sun.COM pguid = pvd->vdev_guid; 48298241SJeff.Bonwick@Sun.COM ppguid = ppvd->vdev_guid; 48308241SJeff.Bonwick@Sun.COM sguid = 0; 48312082Seschrock /* 48322082Seschrock * If we have just finished replacing a hot spared device, then 48332082Seschrock * we need to detach the parent's first child (the original hot 48342082Seschrock * spare) as well. 48352082Seschrock */ 4836*13037SMark.Musante@Sun.COM if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 4837*13037SMark.Musante@Sun.COM ppvd->vdev_children == 2) { 48382082Seschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 48398241SJeff.Bonwick@Sun.COM sguid = ppvd->vdev_child[1]->vdev_guid; 48402082Seschrock } 48418241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 48428241SJeff.Bonwick@Sun.COM if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 48431544Seschrock return; 48448241SJeff.Bonwick@Sun.COM if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 48452082Seschrock return; 48468241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4847789Sahrens } 4848789Sahrens 48498241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 4850789Sahrens } 4851789Sahrens 4852789Sahrens /* 485311041SEric.Taylor@Sun.COM * Update the stored path or FRU for this vdev. 48541354Seschrock */ 48551354Seschrock int 48569425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 48579425SEric.Schrock@Sun.COM boolean_t ispath) 48581354Seschrock { 48596643Seschrock vdev_t *vd; 486011817SGeorge.Wilson@Sun.COM boolean_t sync = B_FALSE; 486111041SEric.Taylor@Sun.COM 486211041SEric.Taylor@Sun.COM spa_vdev_state_enter(spa, SCL_ALL); 48631354Seschrock 48649425SEric.Schrock@Sun.COM if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 486511041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOENT)); 48661354Seschrock 48671585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 486811041SEric.Taylor@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 48691585Sbonwick 48709425SEric.Schrock@Sun.COM if (ispath) { 487111817SGeorge.Wilson@Sun.COM if (strcmp(value, vd->vdev_path) != 0) { 487211817SGeorge.Wilson@Sun.COM spa_strfree(vd->vdev_path); 487311817SGeorge.Wilson@Sun.COM vd->vdev_path = spa_strdup(value); 487411817SGeorge.Wilson@Sun.COM sync = B_TRUE; 487511817SGeorge.Wilson@Sun.COM } 48769425SEric.Schrock@Sun.COM } else { 487711817SGeorge.Wilson@Sun.COM if (vd->vdev_fru == NULL) { 487811817SGeorge.Wilson@Sun.COM vd->vdev_fru = spa_strdup(value); 487911817SGeorge.Wilson@Sun.COM sync = B_TRUE; 488011817SGeorge.Wilson@Sun.COM } else if (strcmp(value, vd->vdev_fru) != 0) { 48819425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 488211817SGeorge.Wilson@Sun.COM vd->vdev_fru = spa_strdup(value); 488311817SGeorge.Wilson@Sun.COM sync = B_TRUE; 488411817SGeorge.Wilson@Sun.COM } 48859425SEric.Schrock@Sun.COM } 48861354Seschrock 488711817SGeorge.Wilson@Sun.COM return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 48881354Seschrock } 48891354Seschrock 48909425SEric.Schrock@Sun.COM int 48919425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 48929425SEric.Schrock@Sun.COM { 48939425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 48949425SEric.Schrock@Sun.COM } 48959425SEric.Schrock@Sun.COM 48969425SEric.Schrock@Sun.COM int 48979425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 48989425SEric.Schrock@Sun.COM { 48999425SEric.Schrock@Sun.COM return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 49009425SEric.Schrock@Sun.COM } 49019425SEric.Schrock@Sun.COM 49021354Seschrock /* 4903789Sahrens * ========================================================================== 490412296SLin.Ling@Sun.COM * SPA Scanning 4905789Sahrens * ========================================================================== 4906789Sahrens */ 4907789Sahrens 49087046Sahrens int 490912296SLin.Ling@Sun.COM spa_scan_stop(spa_t *spa) 4910789Sahrens { 49117754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 491212296SLin.Ling@Sun.COM if (dsl_scan_resilvering(spa->spa_dsl_pool)) 491312296SLin.Ling@Sun.COM return (EBUSY); 491412296SLin.Ling@Sun.COM return (dsl_scan_cancel(spa->spa_dsl_pool)); 491512296SLin.Ling@Sun.COM } 491612296SLin.Ling@Sun.COM 491712296SLin.Ling@Sun.COM int 491812296SLin.Ling@Sun.COM spa_scan(spa_t *spa, pool_scan_func_t func) 491912296SLin.Ling@Sun.COM { 492012296SLin.Ling@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 492112296SLin.Ling@Sun.COM 492212296SLin.Ling@Sun.COM if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 4923789Sahrens return (ENOTSUP); 4924789Sahrens 4925789Sahrens /* 49267046Sahrens * If a resilver was requested, but there is no DTL on a 49277046Sahrens * writeable leaf device, we have nothing to do. 4928789Sahrens */ 492912296SLin.Ling@Sun.COM if (func == POOL_SCAN_RESILVER && 49307046Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 49317046Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 49321544Seschrock return (0); 49331544Seschrock } 4934789Sahrens 493512296SLin.Ling@Sun.COM return (dsl_scan(spa->spa_dsl_pool, func)); 4936789Sahrens } 4937789Sahrens 49381544Seschrock /* 49391544Seschrock * ========================================================================== 49401544Seschrock * SPA async task processing 49411544Seschrock * ========================================================================== 49421544Seschrock */ 49431544Seschrock 49441544Seschrock static void 49454451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 4946789Sahrens { 49477361SBrendan.Gregg@Sun.COM if (vd->vdev_remove_wanted) { 494812247SGeorge.Wilson@Sun.COM vd->vdev_remove_wanted = B_FALSE; 494912247SGeorge.Wilson@Sun.COM vd->vdev_delayed_close = B_FALSE; 49507361SBrendan.Gregg@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 495110575SEric.Schrock@Sun.COM 495210575SEric.Schrock@Sun.COM /* 495310575SEric.Schrock@Sun.COM * We want to clear the stats, but we don't want to do a full 495410575SEric.Schrock@Sun.COM * vdev_clear() as that will cause us to throw away 495510575SEric.Schrock@Sun.COM * degraded/faulted state as well as attempt to reopen the 495610575SEric.Schrock@Sun.COM * device, all of which is a waste. 495710575SEric.Schrock@Sun.COM */ 495810575SEric.Schrock@Sun.COM vd->vdev_stat.vs_read_errors = 0; 495910575SEric.Schrock@Sun.COM vd->vdev_stat.vs_write_errors = 0; 496010575SEric.Schrock@Sun.COM vd->vdev_stat.vs_checksum_errors = 0; 496110575SEric.Schrock@Sun.COM 49627754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 49631544Seschrock } 49647361SBrendan.Gregg@Sun.COM 49657754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 49667361SBrendan.Gregg@Sun.COM spa_async_remove(spa, vd->vdev_child[c]); 49671544Seschrock } 49681544Seschrock 49691544Seschrock static void 49707754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd) 49717754SJeff.Bonwick@Sun.COM { 49727754SJeff.Bonwick@Sun.COM if (vd->vdev_probe_wanted) { 497312247SGeorge.Wilson@Sun.COM vd->vdev_probe_wanted = B_FALSE; 49747754SJeff.Bonwick@Sun.COM vdev_reopen(vd); /* vdev_open() does the actual probe */ 49757754SJeff.Bonwick@Sun.COM } 49767754SJeff.Bonwick@Sun.COM 49777754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 49787754SJeff.Bonwick@Sun.COM spa_async_probe(spa, vd->vdev_child[c]); 49797754SJeff.Bonwick@Sun.COM } 49807754SJeff.Bonwick@Sun.COM 49817754SJeff.Bonwick@Sun.COM static void 49829816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd) 49839816SGeorge.Wilson@Sun.COM { 49849816SGeorge.Wilson@Sun.COM sysevent_id_t eid; 49859816SGeorge.Wilson@Sun.COM nvlist_t *attr; 49869816SGeorge.Wilson@Sun.COM char *physpath; 49879816SGeorge.Wilson@Sun.COM 49889816SGeorge.Wilson@Sun.COM if (!spa->spa_autoexpand) 49899816SGeorge.Wilson@Sun.COM return; 49909816SGeorge.Wilson@Sun.COM 49919816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 49929816SGeorge.Wilson@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 49939816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, cvd); 49949816SGeorge.Wilson@Sun.COM } 49959816SGeorge.Wilson@Sun.COM 49969816SGeorge.Wilson@Sun.COM if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 49979816SGeorge.Wilson@Sun.COM return; 49989816SGeorge.Wilson@Sun.COM 49999816SGeorge.Wilson@Sun.COM physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 50009816SGeorge.Wilson@Sun.COM (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 50019816SGeorge.Wilson@Sun.COM 50029816SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 50039816SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 50049816SGeorge.Wilson@Sun.COM 50059816SGeorge.Wilson@Sun.COM (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 50069816SGeorge.Wilson@Sun.COM ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 50079816SGeorge.Wilson@Sun.COM 50089816SGeorge.Wilson@Sun.COM nvlist_free(attr); 50099816SGeorge.Wilson@Sun.COM kmem_free(physpath, MAXPATHLEN); 50109816SGeorge.Wilson@Sun.COM } 50119816SGeorge.Wilson@Sun.COM 50129816SGeorge.Wilson@Sun.COM static void 50131544Seschrock spa_async_thread(spa_t *spa) 50141544Seschrock { 50157754SJeff.Bonwick@Sun.COM int tasks; 50161544Seschrock 50171544Seschrock ASSERT(spa->spa_sync_on); 5018789Sahrens 50191544Seschrock mutex_enter(&spa->spa_async_lock); 50201544Seschrock tasks = spa->spa_async_tasks; 50211544Seschrock spa->spa_async_tasks = 0; 50221544Seschrock mutex_exit(&spa->spa_async_lock); 50231544Seschrock 50241544Seschrock /* 50251635Sbonwick * See if the config needs to be updated. 50261635Sbonwick */ 50271635Sbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 502810922SJeff.Bonwick@Sun.COM uint64_t old_space, new_space; 50299816SGeorge.Wilson@Sun.COM 50301635Sbonwick mutex_enter(&spa_namespace_lock); 503110922SJeff.Bonwick@Sun.COM old_space = metaslab_class_get_space(spa_normal_class(spa)); 50321635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 503310922SJeff.Bonwick@Sun.COM new_space = metaslab_class_get_space(spa_normal_class(spa)); 50341635Sbonwick mutex_exit(&spa_namespace_lock); 50359816SGeorge.Wilson@Sun.COM 50369816SGeorge.Wilson@Sun.COM /* 50379816SGeorge.Wilson@Sun.COM * If the pool grew as a result of the config update, 50389816SGeorge.Wilson@Sun.COM * then log an internal history event. 50399816SGeorge.Wilson@Sun.COM */ 504010922SJeff.Bonwick@Sun.COM if (new_space != old_space) { 504112296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_VDEV_ONLINE, 504212296SLin.Ling@Sun.COM spa, NULL, 50439946SMark.Musante@Sun.COM "pool '%s' size: %llu(+%llu)", 504410922SJeff.Bonwick@Sun.COM spa_name(spa), new_space, new_space - old_space); 50459816SGeorge.Wilson@Sun.COM } 50461635Sbonwick } 50471635Sbonwick 50481635Sbonwick /* 50494451Seschrock * See if any devices need to be marked REMOVED. 50501544Seschrock */ 50517754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_REMOVE) { 505210685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 50534451Seschrock spa_async_remove(spa, spa->spa_root_vdev); 50547754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 50557361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 50567754SJeff.Bonwick@Sun.COM for (int i = 0; i < spa->spa_spares.sav_count; i++) 50577361SBrendan.Gregg@Sun.COM spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 50587754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 50597754SJeff.Bonwick@Sun.COM } 50607754SJeff.Bonwick@Sun.COM 50619816SGeorge.Wilson@Sun.COM if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 50629816SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 50639816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa, spa->spa_root_vdev); 50649816SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 50659816SGeorge.Wilson@Sun.COM } 50669816SGeorge.Wilson@Sun.COM 50677754SJeff.Bonwick@Sun.COM /* 50687754SJeff.Bonwick@Sun.COM * See if any devices need to be probed. 50697754SJeff.Bonwick@Sun.COM */ 50707754SJeff.Bonwick@Sun.COM if (tasks & SPA_ASYNC_PROBE) { 507110685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 50727754SJeff.Bonwick@Sun.COM spa_async_probe(spa, spa->spa_root_vdev); 50737754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 50744451Seschrock } 50751544Seschrock 50761544Seschrock /* 50771544Seschrock * If any devices are done replacing, detach them. 50781544Seschrock */ 50794451Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 50804451Seschrock spa_vdev_resilver_done(spa); 5081789Sahrens 50821544Seschrock /* 50831544Seschrock * Kick off a resilver. 50841544Seschrock */ 50857046Sahrens if (tasks & SPA_ASYNC_RESILVER) 508612296SLin.Ling@Sun.COM dsl_resilver_restart(spa->spa_dsl_pool, 0); 50871544Seschrock 50881544Seschrock /* 50891544Seschrock * Let the world know that we're done. 50901544Seschrock */ 50911544Seschrock mutex_enter(&spa->spa_async_lock); 50921544Seschrock spa->spa_async_thread = NULL; 50931544Seschrock cv_broadcast(&spa->spa_async_cv); 50941544Seschrock mutex_exit(&spa->spa_async_lock); 50951544Seschrock thread_exit(); 50961544Seschrock } 50971544Seschrock 50981544Seschrock void 50991544Seschrock spa_async_suspend(spa_t *spa) 51001544Seschrock { 51011544Seschrock mutex_enter(&spa->spa_async_lock); 51021544Seschrock spa->spa_async_suspended++; 51031544Seschrock while (spa->spa_async_thread != NULL) 51041544Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 51051544Seschrock mutex_exit(&spa->spa_async_lock); 51061544Seschrock } 51071544Seschrock 51081544Seschrock void 51091544Seschrock spa_async_resume(spa_t *spa) 51101544Seschrock { 51111544Seschrock mutex_enter(&spa->spa_async_lock); 51121544Seschrock ASSERT(spa->spa_async_suspended != 0); 51131544Seschrock spa->spa_async_suspended--; 51141544Seschrock mutex_exit(&spa->spa_async_lock); 51151544Seschrock } 51161544Seschrock 51171544Seschrock static void 51181544Seschrock spa_async_dispatch(spa_t *spa) 51191544Seschrock { 51201544Seschrock mutex_enter(&spa->spa_async_lock); 51211544Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 51221635Sbonwick spa->spa_async_thread == NULL && 51231635Sbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 51241544Seschrock spa->spa_async_thread = thread_create(NULL, 0, 51251544Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 51261544Seschrock mutex_exit(&spa->spa_async_lock); 51271544Seschrock } 51281544Seschrock 51291544Seschrock void 51301544Seschrock spa_async_request(spa_t *spa, int task) 51311544Seschrock { 513212296SLin.Ling@Sun.COM zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 51331544Seschrock mutex_enter(&spa->spa_async_lock); 51341544Seschrock spa->spa_async_tasks |= task; 51351544Seschrock mutex_exit(&spa->spa_async_lock); 5136789Sahrens } 5137789Sahrens 5138789Sahrens /* 5139789Sahrens * ========================================================================== 5140789Sahrens * SPA syncing routines 5141789Sahrens * ========================================================================== 5142789Sahrens */ 514312470SMatthew.Ahrens@Sun.COM 514412470SMatthew.Ahrens@Sun.COM static int 514512470SMatthew.Ahrens@Sun.COM bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5146789Sahrens { 514712470SMatthew.Ahrens@Sun.COM bpobj_t *bpo = arg; 514812470SMatthew.Ahrens@Sun.COM bpobj_enqueue(bpo, bp, tx); 514912470SMatthew.Ahrens@Sun.COM return (0); 515010922SJeff.Bonwick@Sun.COM } 515110922SJeff.Bonwick@Sun.COM 515212470SMatthew.Ahrens@Sun.COM static int 515312470SMatthew.Ahrens@Sun.COM spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 515410922SJeff.Bonwick@Sun.COM { 515510922SJeff.Bonwick@Sun.COM zio_t *zio = arg; 515610922SJeff.Bonwick@Sun.COM 515710922SJeff.Bonwick@Sun.COM zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 515810922SJeff.Bonwick@Sun.COM zio->io_flags)); 515912470SMatthew.Ahrens@Sun.COM return (0); 5160789Sahrens } 5161789Sahrens 5162789Sahrens static void 51632082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 51642082Seschrock { 51652082Seschrock char *packed = NULL; 51667497STim.Haley@Sun.COM size_t bufsize; 51672082Seschrock size_t nvsize = 0; 51682082Seschrock dmu_buf_t *db; 51692082Seschrock 51702082Seschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 51712082Seschrock 51727497STim.Haley@Sun.COM /* 51737497STim.Haley@Sun.COM * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 51747497STim.Haley@Sun.COM * information. This avoids the dbuf_will_dirty() path and 51757497STim.Haley@Sun.COM * saves us a pre-read to get data we don't actually care about. 51767497STim.Haley@Sun.COM */ 51777497STim.Haley@Sun.COM bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 51787497STim.Haley@Sun.COM packed = kmem_alloc(bufsize, KM_SLEEP); 51792082Seschrock 51802082Seschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 51812082Seschrock KM_SLEEP) == 0); 51827497STim.Haley@Sun.COM bzero(packed + nvsize, bufsize - nvsize); 51837497STim.Haley@Sun.COM 51847497STim.Haley@Sun.COM dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 51857497STim.Haley@Sun.COM 51867497STim.Haley@Sun.COM kmem_free(packed, bufsize); 51872082Seschrock 51882082Seschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 51892082Seschrock dmu_buf_will_dirty(db, tx); 51902082Seschrock *(uint64_t *)db->db_data = nvsize; 51912082Seschrock dmu_buf_rele(db, FTAG); 51922082Seschrock } 51932082Seschrock 51942082Seschrock static void 51955450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 51965450Sbrendan const char *config, const char *entry) 51972082Seschrock { 51982082Seschrock nvlist_t *nvroot; 51995450Sbrendan nvlist_t **list; 52002082Seschrock int i; 52012082Seschrock 52025450Sbrendan if (!sav->sav_sync) 52032082Seschrock return; 52042082Seschrock 52052082Seschrock /* 52065450Sbrendan * Update the MOS nvlist describing the list of available devices. 52075450Sbrendan * spa_validate_aux() will have already made sure this nvlist is 52084451Seschrock * valid and the vdevs are labeled appropriately. 52092082Seschrock */ 52105450Sbrendan if (sav->sav_object == 0) { 52115450Sbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 52125450Sbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 52135450Sbrendan sizeof (uint64_t), tx); 52142082Seschrock VERIFY(zap_update(spa->spa_meta_objset, 52155450Sbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 52165450Sbrendan &sav->sav_object, tx) == 0); 52172082Seschrock } 52182082Seschrock 52192082Seschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 52205450Sbrendan if (sav->sav_count == 0) { 52215450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 52222082Seschrock } else { 52235450Sbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 52245450Sbrendan for (i = 0; i < sav->sav_count; i++) 52255450Sbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 522612296SLin.Ling@Sun.COM B_FALSE, VDEV_CONFIG_L2CACHE); 52275450Sbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 52285450Sbrendan sav->sav_count) == 0); 52295450Sbrendan for (i = 0; i < sav->sav_count; i++) 52305450Sbrendan nvlist_free(list[i]); 52315450Sbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 52322082Seschrock } 52332082Seschrock 52345450Sbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 52352926Sek110237 nvlist_free(nvroot); 52362082Seschrock 52375450Sbrendan sav->sav_sync = B_FALSE; 52382082Seschrock } 52392082Seschrock 52402082Seschrock static void 5241789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 5242789Sahrens { 5243789Sahrens nvlist_t *config; 5244789Sahrens 52457754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) 5246789Sahrens return; 5247789Sahrens 52487754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 52497754SJeff.Bonwick@Sun.COM 52507754SJeff.Bonwick@Sun.COM config = spa_config_generate(spa, spa->spa_root_vdev, 52517754SJeff.Bonwick@Sun.COM dmu_tx_get_txg(tx), B_FALSE); 52527754SJeff.Bonwick@Sun.COM 52537754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 5254789Sahrens 52551635Sbonwick if (spa->spa_config_syncing) 52561635Sbonwick nvlist_free(spa->spa_config_syncing); 52571635Sbonwick spa->spa_config_syncing = config; 5258789Sahrens 52592082Seschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 5260789Sahrens } 5261789Sahrens 52625094Slling /* 52635094Slling * Set zpool properties. 52645094Slling */ 52653912Slling static void 526612296SLin.Ling@Sun.COM spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 52673912Slling { 52683912Slling spa_t *spa = arg1; 52695094Slling objset_t *mos = spa->spa_meta_objset; 52703912Slling nvlist_t *nvp = arg2; 52715094Slling nvpair_t *elem; 52724451Seschrock uint64_t intval; 52736643Seschrock char *strval; 52745094Slling zpool_prop_t prop; 52755094Slling const char *propname; 52765094Slling zprop_type_t proptype; 52775094Slling 52787754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 52797754SJeff.Bonwick@Sun.COM 52805094Slling elem = NULL; 52815094Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 52825094Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 52835094Slling case ZPOOL_PROP_VERSION: 52845094Slling /* 52855094Slling * Only set version for non-zpool-creation cases 52865094Slling * (set/import). spa_create() needs special care 52875094Slling * for version setting. 52885094Slling */ 52895094Slling if (tx->tx_txg != TXG_INITIAL) { 52905094Slling VERIFY(nvpair_value_uint64(elem, 52915094Slling &intval) == 0); 52925094Slling ASSERT(intval <= SPA_VERSION); 52935094Slling ASSERT(intval >= spa_version(spa)); 52945094Slling spa->spa_uberblock.ub_version = intval; 52955094Slling vdev_config_dirty(spa->spa_root_vdev); 52965094Slling } 52975094Slling break; 52985094Slling 52995094Slling case ZPOOL_PROP_ALTROOT: 53005094Slling /* 53015094Slling * 'altroot' is a non-persistent property. It should 53025094Slling * have been set temporarily at creation or import time. 53035094Slling */ 53045094Slling ASSERT(spa->spa_root != NULL); 53055094Slling break; 53065094Slling 53075363Seschrock case ZPOOL_PROP_CACHEFILE: 53085094Slling /* 53098525SEric.Schrock@Sun.COM * 'cachefile' is also a non-persisitent property. 53105094Slling */ 53114543Smarks break; 53125094Slling default: 53135094Slling /* 53145094Slling * Set pool property values in the poolprops mos object. 53155094Slling */ 53165094Slling if (spa->spa_pool_props_object == 0) { 53175094Slling VERIFY((spa->spa_pool_props_object = 53185094Slling zap_create(mos, DMU_OT_POOL_PROPS, 53195094Slling DMU_OT_NONE, 0, tx)) > 0); 53205094Slling 53215094Slling VERIFY(zap_update(mos, 53225094Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 53235094Slling 8, 1, &spa->spa_pool_props_object, tx) 53245094Slling == 0); 53255094Slling } 53265094Slling 53275094Slling /* normalize the property name */ 53285094Slling propname = zpool_prop_to_name(prop); 53295094Slling proptype = zpool_prop_get_type(prop); 53305094Slling 53315094Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 53325094Slling ASSERT(proptype == PROP_TYPE_STRING); 53335094Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 53345094Slling VERIFY(zap_update(mos, 53355094Slling spa->spa_pool_props_object, propname, 53365094Slling 1, strlen(strval) + 1, strval, tx) == 0); 53375094Slling 53385094Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 53395094Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 53405094Slling 53415094Slling if (proptype == PROP_TYPE_INDEX) { 53425094Slling const char *unused; 53435094Slling VERIFY(zpool_prop_index_to_string( 53445094Slling prop, intval, &unused) == 0); 53455094Slling } 53465094Slling VERIFY(zap_update(mos, 53475094Slling spa->spa_pool_props_object, propname, 53485094Slling 8, 1, &intval, tx) == 0); 53495094Slling } else { 53505094Slling ASSERT(0); /* not allowed */ 53515094Slling } 53525094Slling 53535329Sgw25295 switch (prop) { 53545329Sgw25295 case ZPOOL_PROP_DELEGATION: 53555094Slling spa->spa_delegation = intval; 53565329Sgw25295 break; 53575329Sgw25295 case ZPOOL_PROP_BOOTFS: 53585094Slling spa->spa_bootfs = intval; 53595329Sgw25295 break; 53605329Sgw25295 case ZPOOL_PROP_FAILUREMODE: 53615329Sgw25295 spa->spa_failmode = intval; 53625329Sgw25295 break; 53639816SGeorge.Wilson@Sun.COM case ZPOOL_PROP_AUTOEXPAND: 53649816SGeorge.Wilson@Sun.COM spa->spa_autoexpand = intval; 536512318SEric.Taylor@Sun.COM if (tx->tx_txg != TXG_INITIAL) 536612318SEric.Taylor@Sun.COM spa_async_request(spa, 536712318SEric.Taylor@Sun.COM SPA_ASYNC_AUTOEXPAND); 53689816SGeorge.Wilson@Sun.COM break; 536910922SJeff.Bonwick@Sun.COM case ZPOOL_PROP_DEDUPDITTO: 537010922SJeff.Bonwick@Sun.COM spa->spa_dedup_ditto = intval; 537110922SJeff.Bonwick@Sun.COM break; 53725329Sgw25295 default: 53735329Sgw25295 break; 53745329Sgw25295 } 53753912Slling } 53765094Slling 53775094Slling /* log internal history if this is not a zpool create */ 53785094Slling if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 53795094Slling tx->tx_txg != TXG_INITIAL) { 538012296SLin.Ling@Sun.COM spa_history_log_internal(LOG_POOL_PROPSET, 538112296SLin.Ling@Sun.COM spa, tx, "%s %lld %s", 53827754SJeff.Bonwick@Sun.COM nvpair_name(elem), intval, spa_name(spa)); 53835094Slling } 53843912Slling } 53857754SJeff.Bonwick@Sun.COM 53867754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 53873912Slling } 53883912Slling 5389789Sahrens /* 539012470SMatthew.Ahrens@Sun.COM * Perform one-time upgrade on-disk changes. spa_version() does not 539112470SMatthew.Ahrens@Sun.COM * reflect the new version this txg, so there must be no changes this 539212470SMatthew.Ahrens@Sun.COM * txg to anything that the upgrade code depends on after it executes. 539312470SMatthew.Ahrens@Sun.COM * Therefore this must be called after dsl_pool_sync() does the sync 539412470SMatthew.Ahrens@Sun.COM * tasks. 539512470SMatthew.Ahrens@Sun.COM */ 539612470SMatthew.Ahrens@Sun.COM static void 539712470SMatthew.Ahrens@Sun.COM spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 539812470SMatthew.Ahrens@Sun.COM { 539912470SMatthew.Ahrens@Sun.COM dsl_pool_t *dp = spa->spa_dsl_pool; 540012470SMatthew.Ahrens@Sun.COM 540112470SMatthew.Ahrens@Sun.COM ASSERT(spa->spa_sync_pass == 1); 540212470SMatthew.Ahrens@Sun.COM 540312470SMatthew.Ahrens@Sun.COM if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 540412470SMatthew.Ahrens@Sun.COM spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 540512470SMatthew.Ahrens@Sun.COM dsl_pool_create_origin(dp, tx); 540612470SMatthew.Ahrens@Sun.COM 540712470SMatthew.Ahrens@Sun.COM /* Keeping the origin open increases spa_minref */ 540812470SMatthew.Ahrens@Sun.COM spa->spa_minref += 3; 540912470SMatthew.Ahrens@Sun.COM } 541012470SMatthew.Ahrens@Sun.COM 541112470SMatthew.Ahrens@Sun.COM if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 541212470SMatthew.Ahrens@Sun.COM spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 541312470SMatthew.Ahrens@Sun.COM dsl_pool_upgrade_clones(dp, tx); 541412470SMatthew.Ahrens@Sun.COM } 541512470SMatthew.Ahrens@Sun.COM 541612470SMatthew.Ahrens@Sun.COM if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 541712470SMatthew.Ahrens@Sun.COM spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 541812470SMatthew.Ahrens@Sun.COM dsl_pool_upgrade_dir_clones(dp, tx); 541912470SMatthew.Ahrens@Sun.COM 542012470SMatthew.Ahrens@Sun.COM /* Keeping the freedir open increases spa_minref */ 542112470SMatthew.Ahrens@Sun.COM spa->spa_minref += 3; 542212470SMatthew.Ahrens@Sun.COM } 542312470SMatthew.Ahrens@Sun.COM } 542412470SMatthew.Ahrens@Sun.COM 542512470SMatthew.Ahrens@Sun.COM /* 5426789Sahrens * Sync the specified transaction group. New blocks may be dirtied as 5427789Sahrens * part of the process, so we iterate until it converges. 5428789Sahrens */ 5429789Sahrens void 5430789Sahrens spa_sync(spa_t *spa, uint64_t txg) 5431789Sahrens { 5432789Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5433789Sahrens objset_t *mos = spa->spa_meta_objset; 543412470SMatthew.Ahrens@Sun.COM bpobj_t *defer_bpo = &spa->spa_deferred_bpobj; 543510922SJeff.Bonwick@Sun.COM bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 54361635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 5437789Sahrens vdev_t *vd; 5438789Sahrens dmu_tx_t *tx; 54397754SJeff.Bonwick@Sun.COM int error; 5440789Sahrens 5441789Sahrens /* 5442789Sahrens * Lock out configuration changes. 5443789Sahrens */ 54447754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5445789Sahrens 5446789Sahrens spa->spa_syncing_txg = txg; 5447789Sahrens spa->spa_sync_pass = 0; 5448789Sahrens 54497754SJeff.Bonwick@Sun.COM /* 54507754SJeff.Bonwick@Sun.COM * If there are any pending vdev state changes, convert them 54517754SJeff.Bonwick@Sun.COM * into config changes that go out with this transaction group. 54527754SJeff.Bonwick@Sun.COM */ 54537754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 54548241SJeff.Bonwick@Sun.COM while (list_head(&spa->spa_state_dirty_list) != NULL) { 54558241SJeff.Bonwick@Sun.COM /* 54568241SJeff.Bonwick@Sun.COM * We need the write lock here because, for aux vdevs, 54578241SJeff.Bonwick@Sun.COM * calling vdev_config_dirty() modifies sav_config. 54588241SJeff.Bonwick@Sun.COM * This is ugly and will become unnecessary when we 54598241SJeff.Bonwick@Sun.COM * eliminate the aux vdev wart by integrating all vdevs 54608241SJeff.Bonwick@Sun.COM * into the root vdev tree. 54618241SJeff.Bonwick@Sun.COM */ 54628241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 54638241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 54648241SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 54658241SJeff.Bonwick@Sun.COM vdev_state_clean(vd); 54668241SJeff.Bonwick@Sun.COM vdev_config_dirty(vd); 54678241SJeff.Bonwick@Sun.COM } 54688241SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 54698241SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 54707754SJeff.Bonwick@Sun.COM } 54717754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 54727754SJeff.Bonwick@Sun.COM 54732082Seschrock tx = dmu_tx_create_assigned(dp, txg); 54742082Seschrock 54752082Seschrock /* 54764577Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 54772082Seschrock * set spa_deflate if we have no raid-z vdevs. 54782082Seschrock */ 54794577Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 54804577Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 54812082Seschrock int i; 54822082Seschrock 54832082Seschrock for (i = 0; i < rvd->vdev_children; i++) { 54842082Seschrock vd = rvd->vdev_child[i]; 54852082Seschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 54862082Seschrock break; 54872082Seschrock } 54882082Seschrock if (i == rvd->vdev_children) { 54892082Seschrock spa->spa_deflate = TRUE; 54902082Seschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 54912082Seschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 54922082Seschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 54932082Seschrock } 54942082Seschrock } 54952082Seschrock 5496789Sahrens /* 549712296SLin.Ling@Sun.COM * If anything has changed in this txg, or if someone is waiting 549812296SLin.Ling@Sun.COM * for this txg to sync (eg, spa_vdev_remove()), push the 549912296SLin.Ling@Sun.COM * deferred frees from the previous txg. If not, leave them 550012296SLin.Ling@Sun.COM * alone so that we don't generate work on an otherwise idle 550112296SLin.Ling@Sun.COM * system. 5502789Sahrens */ 5503789Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 55042329Sek110237 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 550512296SLin.Ling@Sun.COM !txg_list_empty(&dp->dp_sync_tasks, txg) || 550612470SMatthew.Ahrens@Sun.COM ((dsl_scan_active(dp->dp_scan) || 550712470SMatthew.Ahrens@Sun.COM txg_sync_waiting(dp)) && !spa_shutting_down(spa))) { 550812470SMatthew.Ahrens@Sun.COM zio_t *zio = zio_root(spa, NULL, NULL, 0); 550912470SMatthew.Ahrens@Sun.COM VERIFY3U(bpobj_iterate(defer_bpo, 551012470SMatthew.Ahrens@Sun.COM spa_free_sync_cb, zio, tx), ==, 0); 551112470SMatthew.Ahrens@Sun.COM VERIFY3U(zio_wait(zio), ==, 0); 551212470SMatthew.Ahrens@Sun.COM } 5513789Sahrens 5514789Sahrens /* 5515789Sahrens * Iterate to convergence. 5516789Sahrens */ 5517789Sahrens do { 551810922SJeff.Bonwick@Sun.COM int pass = ++spa->spa_sync_pass; 5519789Sahrens 5520789Sahrens spa_sync_config_object(spa, tx); 55215450Sbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 55225450Sbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 55235450Sbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 55245450Sbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 55251544Seschrock spa_errlog_sync(spa, txg); 5526789Sahrens dsl_pool_sync(dp, txg); 5527789Sahrens 552810922SJeff.Bonwick@Sun.COM if (pass <= SYNC_PASS_DEFERRED_FREE) { 552910922SJeff.Bonwick@Sun.COM zio_t *zio = zio_root(spa, NULL, NULL, 0); 553012470SMatthew.Ahrens@Sun.COM bplist_iterate(free_bpl, spa_free_sync_cb, 553112470SMatthew.Ahrens@Sun.COM zio, tx); 553210922SJeff.Bonwick@Sun.COM VERIFY(zio_wait(zio) == 0); 553310922SJeff.Bonwick@Sun.COM } else { 553412470SMatthew.Ahrens@Sun.COM bplist_iterate(free_bpl, bpobj_enqueue_cb, 553512470SMatthew.Ahrens@Sun.COM defer_bpo, tx); 5536789Sahrens } 5537789Sahrens 553810922SJeff.Bonwick@Sun.COM ddt_sync(spa, txg); 553912296SLin.Ling@Sun.COM dsl_scan_sync(dp, tx); 554011619SGeorge.Wilson@Sun.COM 554110922SJeff.Bonwick@Sun.COM while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 554210922SJeff.Bonwick@Sun.COM vdev_sync(vd, txg); 554310922SJeff.Bonwick@Sun.COM 554412470SMatthew.Ahrens@Sun.COM if (pass == 1) 554512470SMatthew.Ahrens@Sun.COM spa_sync_upgrades(spa, tx); 554612470SMatthew.Ahrens@Sun.COM 554710922SJeff.Bonwick@Sun.COM } while (dmu_objset_is_dirty(mos, txg)); 554810922SJeff.Bonwick@Sun.COM 5549789Sahrens /* 5550789Sahrens * Rewrite the vdev configuration (which includes the uberblock) 5551789Sahrens * to commit the transaction group. 55521635Sbonwick * 55535688Sbonwick * If there are no dirty vdevs, we sync the uberblock to a few 55545688Sbonwick * random top-level vdevs that are known to be visible in the 55557754SJeff.Bonwick@Sun.COM * config cache (see spa_vdev_add() for a complete description). 55567754SJeff.Bonwick@Sun.COM * If there *are* dirty vdevs, sync the uberblock to all vdevs. 5557789Sahrens */ 55587754SJeff.Bonwick@Sun.COM for (;;) { 55597754SJeff.Bonwick@Sun.COM /* 55607754SJeff.Bonwick@Sun.COM * We hold SCL_STATE to prevent vdev open/close/etc. 55617754SJeff.Bonwick@Sun.COM * while we're attempting to write the vdev labels. 55627754SJeff.Bonwick@Sun.COM */ 55637754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 55647754SJeff.Bonwick@Sun.COM 55657754SJeff.Bonwick@Sun.COM if (list_is_empty(&spa->spa_config_dirty_list)) { 55667754SJeff.Bonwick@Sun.COM vdev_t *svd[SPA_DVAS_PER_BP]; 55677754SJeff.Bonwick@Sun.COM int svdcount = 0; 55687754SJeff.Bonwick@Sun.COM int children = rvd->vdev_children; 55697754SJeff.Bonwick@Sun.COM int c0 = spa_get_random(children); 55709816SGeorge.Wilson@Sun.COM 55719816SGeorge.Wilson@Sun.COM for (int c = 0; c < children; c++) { 55727754SJeff.Bonwick@Sun.COM vd = rvd->vdev_child[(c0 + c) % children]; 55737754SJeff.Bonwick@Sun.COM if (vd->vdev_ms_array == 0 || vd->vdev_islog) 55747754SJeff.Bonwick@Sun.COM continue; 55757754SJeff.Bonwick@Sun.COM svd[svdcount++] = vd; 55767754SJeff.Bonwick@Sun.COM if (svdcount == SPA_DVAS_PER_BP) 55777754SJeff.Bonwick@Sun.COM break; 55787754SJeff.Bonwick@Sun.COM } 55799725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 55809725SEric.Schrock@Sun.COM if (error != 0) 55819725SEric.Schrock@Sun.COM error = vdev_config_sync(svd, svdcount, txg, 55829725SEric.Schrock@Sun.COM B_TRUE); 55837754SJeff.Bonwick@Sun.COM } else { 55847754SJeff.Bonwick@Sun.COM error = vdev_config_sync(rvd->vdev_child, 55859725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_FALSE); 55869725SEric.Schrock@Sun.COM if (error != 0) 55879725SEric.Schrock@Sun.COM error = vdev_config_sync(rvd->vdev_child, 55889725SEric.Schrock@Sun.COM rvd->vdev_children, txg, B_TRUE); 55891635Sbonwick } 55907754SJeff.Bonwick@Sun.COM 55917754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 55927754SJeff.Bonwick@Sun.COM 55937754SJeff.Bonwick@Sun.COM if (error == 0) 55947754SJeff.Bonwick@Sun.COM break; 55957754SJeff.Bonwick@Sun.COM zio_suspend(spa, NULL); 55967754SJeff.Bonwick@Sun.COM zio_resume_wait(spa); 55971635Sbonwick } 55982082Seschrock dmu_tx_commit(tx); 55992082Seschrock 56001635Sbonwick /* 56011635Sbonwick * Clear the dirty config list. 56021635Sbonwick */ 56037754SJeff.Bonwick@Sun.COM while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 56041635Sbonwick vdev_config_clean(vd); 56051635Sbonwick 56061635Sbonwick /* 56071635Sbonwick * Now that the new config has synced transactionally, 56081635Sbonwick * let it become visible to the config cache. 56091635Sbonwick */ 56101635Sbonwick if (spa->spa_config_syncing != NULL) { 56111635Sbonwick spa_config_set(spa, spa->spa_config_syncing); 56121635Sbonwick spa->spa_config_txg = txg; 56131635Sbonwick spa->spa_config_syncing = NULL; 56141635Sbonwick } 5615789Sahrens 5616789Sahrens spa->spa_ubsync = spa->spa_uberblock; 5617789Sahrens 561810922SJeff.Bonwick@Sun.COM dsl_pool_sync_done(dp, txg); 5619789Sahrens 5620789Sahrens /* 5621789Sahrens * Update usable space statistics. 5622789Sahrens */ 5623789Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 5624789Sahrens vdev_sync_done(vd, txg); 5625789Sahrens 562610956SGeorge.Wilson@Sun.COM spa_update_dspace(spa); 562710956SGeorge.Wilson@Sun.COM 5628789Sahrens /* 5629789Sahrens * It had better be the case that we didn't dirty anything 56302082Seschrock * since vdev_config_sync(). 5631789Sahrens */ 5632789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 5633789Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 5634789Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 563510922SJeff.Bonwick@Sun.COM 563610922SJeff.Bonwick@Sun.COM spa->spa_sync_pass = 0; 5637789Sahrens 56387754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG, FTAG); 56391544Seschrock 564010921STim.Haley@Sun.COM spa_handle_ignored_writes(spa); 564110921STim.Haley@Sun.COM 56421544Seschrock /* 56431544Seschrock * If any async tasks have been requested, kick them off. 56441544Seschrock */ 56451544Seschrock spa_async_dispatch(spa); 5646789Sahrens } 5647789Sahrens 5648789Sahrens /* 5649789Sahrens * Sync all pools. We don't want to hold the namespace lock across these 5650789Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 5651789Sahrens * sync. 5652789Sahrens */ 5653789Sahrens void 5654789Sahrens spa_sync_allpools(void) 5655789Sahrens { 5656789Sahrens spa_t *spa = NULL; 5657789Sahrens mutex_enter(&spa_namespace_lock); 5658789Sahrens while ((spa = spa_next(spa)) != NULL) { 56597754SJeff.Bonwick@Sun.COM if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 5660789Sahrens continue; 5661789Sahrens spa_open_ref(spa, FTAG); 5662789Sahrens mutex_exit(&spa_namespace_lock); 5663789Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 5664789Sahrens mutex_enter(&spa_namespace_lock); 5665789Sahrens spa_close(spa, FTAG); 5666789Sahrens } 5667789Sahrens mutex_exit(&spa_namespace_lock); 5668789Sahrens } 5669789Sahrens 5670789Sahrens /* 5671789Sahrens * ========================================================================== 5672789Sahrens * Miscellaneous routines 5673789Sahrens * ========================================================================== 5674789Sahrens */ 5675789Sahrens 5676789Sahrens /* 5677789Sahrens * Remove all pools in the system. 5678789Sahrens */ 5679789Sahrens void 5680789Sahrens spa_evict_all(void) 5681789Sahrens { 5682789Sahrens spa_t *spa; 5683789Sahrens 5684789Sahrens /* 5685789Sahrens * Remove all cached state. All pools should be closed now, 5686789Sahrens * so every spa in the AVL tree should be unreferenced. 5687789Sahrens */ 5688789Sahrens mutex_enter(&spa_namespace_lock); 5689789Sahrens while ((spa = spa_next(NULL)) != NULL) { 5690789Sahrens /* 56911544Seschrock * Stop async tasks. The async thread may need to detach 56921544Seschrock * a device that's been replaced, which requires grabbing 56931544Seschrock * spa_namespace_lock, so we must drop it here. 5694789Sahrens */ 5695789Sahrens spa_open_ref(spa, FTAG); 5696789Sahrens mutex_exit(&spa_namespace_lock); 56971544Seschrock spa_async_suspend(spa); 56984808Sek110237 mutex_enter(&spa_namespace_lock); 5699789Sahrens spa_close(spa, FTAG); 5700789Sahrens 5701789Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 5702789Sahrens spa_unload(spa); 5703789Sahrens spa_deactivate(spa); 5704789Sahrens } 5705789Sahrens spa_remove(spa); 5706789Sahrens } 5707789Sahrens mutex_exit(&spa_namespace_lock); 5708789Sahrens } 57091544Seschrock 57101544Seschrock vdev_t * 57119425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 57121544Seschrock { 57136643Seschrock vdev_t *vd; 57146643Seschrock int i; 57156643Seschrock 57166643Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 57176643Seschrock return (vd); 57186643Seschrock 57199425SEric.Schrock@Sun.COM if (aux) { 57206643Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 57216643Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 57226643Seschrock if (vd->vdev_guid == guid) 57236643Seschrock return (vd); 57246643Seschrock } 57259425SEric.Schrock@Sun.COM 57269425SEric.Schrock@Sun.COM for (i = 0; i < spa->spa_spares.sav_count; i++) { 57279425SEric.Schrock@Sun.COM vd = spa->spa_spares.sav_vdevs[i]; 57289425SEric.Schrock@Sun.COM if (vd->vdev_guid == guid) 57299425SEric.Schrock@Sun.COM return (vd); 57309425SEric.Schrock@Sun.COM } 57316643Seschrock } 57326643Seschrock 57336643Seschrock return (NULL); 57341544Seschrock } 57351760Seschrock 57361760Seschrock void 57375094Slling spa_upgrade(spa_t *spa, uint64_t version) 57381760Seschrock { 57397754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 57401760Seschrock 57411760Seschrock /* 57421760Seschrock * This should only be called for a non-faulted pool, and since a 57431760Seschrock * future version would result in an unopenable pool, this shouldn't be 57441760Seschrock * possible. 57451760Seschrock */ 57464577Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 57475094Slling ASSERT(version >= spa->spa_uberblock.ub_version); 57485094Slling 57495094Slling spa->spa_uberblock.ub_version = version; 57501760Seschrock vdev_config_dirty(spa->spa_root_vdev); 57511760Seschrock 57527754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 57532082Seschrock 57542082Seschrock txg_wait_synced(spa_get_dsl(spa), 0); 57551760Seschrock } 57562082Seschrock 57572082Seschrock boolean_t 57582082Seschrock spa_has_spare(spa_t *spa, uint64_t guid) 57592082Seschrock { 57602082Seschrock int i; 57613377Seschrock uint64_t spareguid; 57625450Sbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 57635450Sbrendan 57645450Sbrendan for (i = 0; i < sav->sav_count; i++) 57655450Sbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 57662082Seschrock return (B_TRUE); 57672082Seschrock 57685450Sbrendan for (i = 0; i < sav->sav_npending; i++) { 57695450Sbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 57705450Sbrendan &spareguid) == 0 && spareguid == guid) 57713377Seschrock return (B_TRUE); 57723377Seschrock } 57733377Seschrock 57742082Seschrock return (B_FALSE); 57752082Seschrock } 57763912Slling 57774451Seschrock /* 57787214Slling * Check if a pool has an active shared spare device. 57797214Slling * Note: reference count of an active spare is 2, as a spare and as a replace 57807214Slling */ 57817214Slling static boolean_t 57827214Slling spa_has_active_shared_spare(spa_t *spa) 57837214Slling { 57847214Slling int i, refcnt; 57857214Slling uint64_t pool; 57867214Slling spa_aux_vdev_t *sav = &spa->spa_spares; 57877214Slling 57887214Slling for (i = 0; i < sav->sav_count; i++) { 57897214Slling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 57907214Slling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 57917214Slling refcnt > 2) 57927214Slling return (B_TRUE); 57937214Slling } 57947214Slling 57957214Slling return (B_FALSE); 57967214Slling } 57977214Slling 57987214Slling /* 57994451Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 58004451Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 58014451Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 58024451Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 58034451Seschrock * or zdb as real changes. 58044451Seschrock */ 58054451Seschrock void 58064451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 58074451Seschrock { 58084451Seschrock #ifdef _KERNEL 58094451Seschrock sysevent_t *ev; 58104451Seschrock sysevent_attr_list_t *attr = NULL; 58114451Seschrock sysevent_value_t value; 58124451Seschrock sysevent_id_t eid; 58134451Seschrock 58144451Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 58154451Seschrock SE_SLEEP); 58164451Seschrock 58174451Seschrock value.value_type = SE_DATA_TYPE_STRING; 58184451Seschrock value.value.sv_string = spa_name(spa); 58194451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 58204451Seschrock goto done; 58214451Seschrock 58224451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 58234451Seschrock value.value.sv_uint64 = spa_guid(spa); 58244451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 58254451Seschrock goto done; 58264451Seschrock 58274451Seschrock if (vd) { 58284451Seschrock value.value_type = SE_DATA_TYPE_UINT64; 58294451Seschrock value.value.sv_uint64 = vd->vdev_guid; 58304451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 58314451Seschrock SE_SLEEP) != 0) 58324451Seschrock goto done; 58334451Seschrock 58344451Seschrock if (vd->vdev_path) { 58354451Seschrock value.value_type = SE_DATA_TYPE_STRING; 58364451Seschrock value.value.sv_string = vd->vdev_path; 58374451Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 58384451Seschrock &value, SE_SLEEP) != 0) 58394451Seschrock goto done; 58404451Seschrock } 58414451Seschrock } 58424451Seschrock 58435756Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 58445756Seschrock goto done; 58455756Seschrock attr = NULL; 58465756Seschrock 58474451Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 58484451Seschrock 58494451Seschrock done: 58504451Seschrock if (attr) 58514451Seschrock sysevent_free_attr(attr); 58524451Seschrock sysevent_free(ev); 58534451Seschrock #endif 58544451Seschrock } 5855