Deleted Added
full compact
spa.c (219089) spa.c (228103)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011 by Delphix. All rights reserved.
24 */
25
26/*
27 * This file contains all the routines used when modifying on-disk SPA state.
28 * This includes opening, importing, destroying, exporting a pool, and syncing a
29 * pool.
30 */
31
32#include <sys/zfs_context.h>
33#include <sys/fm/fs/zfs.h>
34#include <sys/spa_impl.h>
35#include <sys/zio.h>
36#include <sys/zio_checksum.h>
37#include <sys/dmu.h>
38#include <sys/dmu_tx.h>
39#include <sys/zap.h>
40#include <sys/zil.h>
41#include <sys/ddt.h>
42#include <sys/vdev_impl.h>
43#include <sys/metaslab.h>
44#include <sys/metaslab_impl.h>
45#include <sys/uberblock_impl.h>
46#include <sys/txg.h>
47#include <sys/avl.h>
48#include <sys/dmu_traverse.h>
49#include <sys/dmu_objset.h>
50#include <sys/unique.h>
51#include <sys/dsl_pool.h>
52#include <sys/dsl_dataset.h>
53#include <sys/dsl_dir.h>
54#include <sys/dsl_prop.h>
55#include <sys/dsl_synctask.h>
56#include <sys/fs/zfs.h>
57#include <sys/arc.h>
58#include <sys/callb.h>
59#include <sys/spa_boot.h>
60#include <sys/zfs_ioctl.h>
61#include <sys/dsl_scan.h>
62#include <sys/zvol.h>
63
64#ifdef _KERNEL
65#include <sys/callb.h>
66#include <sys/cpupart.h>
67#include <sys/zone.h>
68#endif /* _KERNEL */
69
70#include "zfs_prop.h"
71#include "zfs_comutil.h"
72
73/* Check hostid on import? */
74static int check_hostid = 1;
75
76SYSCTL_DECL(_vfs_zfs);
77TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
78SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
79 "Check hostid on import?");
80
81typedef enum zti_modes {
82 zti_mode_fixed, /* value is # of threads (min 1) */
83 zti_mode_online_percent, /* value is % of online CPUs */
84 zti_mode_batch, /* cpu-intensive; value is ignored */
85 zti_mode_null, /* don't create a taskq */
86 zti_nmodes
87} zti_modes_t;
88
89#define ZTI_FIX(n) { zti_mode_fixed, (n) }
90#define ZTI_PCT(n) { zti_mode_online_percent, (n) }
91#define ZTI_BATCH { zti_mode_batch, 0 }
92#define ZTI_NULL { zti_mode_null, 0 }
93
94#define ZTI_ONE ZTI_FIX(1)
95
96typedef struct zio_taskq_info {
97 enum zti_modes zti_mode;
98 uint_t zti_value;
99} zio_taskq_info_t;
100
101static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
102 "issue", "issue_high", "intr", "intr_high"
103};
104
105/*
106 * Define the taskq threads for the following I/O types:
107 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
108 */
109const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
110 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
111 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
112 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
113 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
114 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
115 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
116 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
117};
118
119static dsl_syncfunc_t spa_sync_props;
120static boolean_t spa_has_active_shared_spare(spa_t *spa);
121static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
122 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
123 char **ereport);
124static void spa_vdev_resilver_done(spa_t *spa);
125
126uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
127#ifdef PSRSET_BIND
128id_t zio_taskq_psrset_bind = PS_NONE;
129#endif
130#ifdef SYSDC
131boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
132#endif
133uint_t zio_taskq_basedc = 80; /* base duty cycle */
134
135boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
136
137/*
138 * This (illegal) pool name is used when temporarily importing a spa_t in order
139 * to get the vdev stats associated with the imported devices.
140 */
141#define TRYIMPORT_NAME "$import"
142
143/*
144 * ==========================================================================
145 * SPA properties routines
146 * ==========================================================================
147 */
148
149/*
150 * Add a (source=src, propname=propval) list to an nvlist.
151 */
152static void
153spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
154 uint64_t intval, zprop_source_t src)
155{
156 const char *propname = zpool_prop_to_name(prop);
157 nvlist_t *propval;
158
159 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
160 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
161
162 if (strval != NULL)
163 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
164 else
165 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
166
167 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
168 nvlist_free(propval);
169}
170
171/*
172 * Get property values from the spa configuration.
173 */
174static void
175spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
176{
177 uint64_t size;
178 uint64_t alloc;
179 uint64_t cap, version;
180 zprop_source_t src = ZPROP_SRC_NONE;
181 spa_config_dirent_t *dp;
182
183 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
184
185 if (spa->spa_root_vdev != NULL) {
186 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
187 size = metaslab_class_get_space(spa_normal_class(spa));
188 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
189 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
190 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
191 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
192 size - alloc, src);
193 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
194 (spa_mode(spa) == FREAD), src);
195
196 cap = (size == 0) ? 0 : (alloc * 100 / size);
197 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
198
199 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
200 ddt_get_pool_dedup_ratio(spa), src);
201
202 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
203 spa->spa_root_vdev->vdev_state, src);
204
205 version = spa_version(spa);
206 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
207 src = ZPROP_SRC_DEFAULT;
208 else
209 src = ZPROP_SRC_LOCAL;
210 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
211 }
212
213 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
214
25 */
26
27/*
28 * This file contains all the routines used when modifying on-disk SPA state.
29 * This includes opening, importing, destroying, exporting a pool, and syncing a
30 * pool.
31 */
32
33#include <sys/zfs_context.h>
34#include <sys/fm/fs/zfs.h>
35#include <sys/spa_impl.h>
36#include <sys/zio.h>
37#include <sys/zio_checksum.h>
38#include <sys/dmu.h>
39#include <sys/dmu_tx.h>
40#include <sys/zap.h>
41#include <sys/zil.h>
42#include <sys/ddt.h>
43#include <sys/vdev_impl.h>
44#include <sys/metaslab.h>
45#include <sys/metaslab_impl.h>
46#include <sys/uberblock_impl.h>
47#include <sys/txg.h>
48#include <sys/avl.h>
49#include <sys/dmu_traverse.h>
50#include <sys/dmu_objset.h>
51#include <sys/unique.h>
52#include <sys/dsl_pool.h>
53#include <sys/dsl_dataset.h>
54#include <sys/dsl_dir.h>
55#include <sys/dsl_prop.h>
56#include <sys/dsl_synctask.h>
57#include <sys/fs/zfs.h>
58#include <sys/arc.h>
59#include <sys/callb.h>
60#include <sys/spa_boot.h>
61#include <sys/zfs_ioctl.h>
62#include <sys/dsl_scan.h>
63#include <sys/zvol.h>
64
65#ifdef _KERNEL
66#include <sys/callb.h>
67#include <sys/cpupart.h>
68#include <sys/zone.h>
69#endif /* _KERNEL */
70
71#include "zfs_prop.h"
72#include "zfs_comutil.h"
73
74/* Check hostid on import? */
75static int check_hostid = 1;
76
77SYSCTL_DECL(_vfs_zfs);
78TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
79SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
80 "Check hostid on import?");
81
82typedef enum zti_modes {
83 zti_mode_fixed, /* value is # of threads (min 1) */
84 zti_mode_online_percent, /* value is % of online CPUs */
85 zti_mode_batch, /* cpu-intensive; value is ignored */
86 zti_mode_null, /* don't create a taskq */
87 zti_nmodes
88} zti_modes_t;
89
90#define ZTI_FIX(n) { zti_mode_fixed, (n) }
91#define ZTI_PCT(n) { zti_mode_online_percent, (n) }
92#define ZTI_BATCH { zti_mode_batch, 0 }
93#define ZTI_NULL { zti_mode_null, 0 }
94
95#define ZTI_ONE ZTI_FIX(1)
96
97typedef struct zio_taskq_info {
98 enum zti_modes zti_mode;
99 uint_t zti_value;
100} zio_taskq_info_t;
101
102static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
103 "issue", "issue_high", "intr", "intr_high"
104};
105
106/*
107 * Define the taskq threads for the following I/O types:
108 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
109 */
110const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
111 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
112 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
113 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
114 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
115 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
116 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
117 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
118};
119
120static dsl_syncfunc_t spa_sync_props;
121static boolean_t spa_has_active_shared_spare(spa_t *spa);
122static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
123 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
124 char **ereport);
125static void spa_vdev_resilver_done(spa_t *spa);
126
127uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
128#ifdef PSRSET_BIND
129id_t zio_taskq_psrset_bind = PS_NONE;
130#endif
131#ifdef SYSDC
132boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
133#endif
134uint_t zio_taskq_basedc = 80; /* base duty cycle */
135
136boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
137
138/*
139 * This (illegal) pool name is used when temporarily importing a spa_t in order
140 * to get the vdev stats associated with the imported devices.
141 */
142#define TRYIMPORT_NAME "$import"
143
144/*
145 * ==========================================================================
146 * SPA properties routines
147 * ==========================================================================
148 */
149
150/*
151 * Add a (source=src, propname=propval) list to an nvlist.
152 */
153static void
154spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
155 uint64_t intval, zprop_source_t src)
156{
157 const char *propname = zpool_prop_to_name(prop);
158 nvlist_t *propval;
159
160 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
161 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
162
163 if (strval != NULL)
164 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
165 else
166 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
167
168 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
169 nvlist_free(propval);
170}
171
172/*
173 * Get property values from the spa configuration.
174 */
175static void
176spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
177{
178 uint64_t size;
179 uint64_t alloc;
180 uint64_t cap, version;
181 zprop_source_t src = ZPROP_SRC_NONE;
182 spa_config_dirent_t *dp;
183
184 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
185
186 if (spa->spa_root_vdev != NULL) {
187 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
188 size = metaslab_class_get_space(spa_normal_class(spa));
189 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
190 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
191 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
192 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
193 size - alloc, src);
194 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
195 (spa_mode(spa) == FREAD), src);
196
197 cap = (size == 0) ? 0 : (alloc * 100 / size);
198 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
199
200 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
201 ddt_get_pool_dedup_ratio(spa), src);
202
203 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
204 spa->spa_root_vdev->vdev_state, src);
205
206 version = spa_version(spa);
207 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
208 src = ZPROP_SRC_DEFAULT;
209 else
210 src = ZPROP_SRC_LOCAL;
211 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
212 }
213
214 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
215
216 if (spa->spa_comment != NULL) {
217 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
218 0, ZPROP_SRC_LOCAL);
219 }
220
215 if (spa->spa_root != NULL)
216 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
217 0, ZPROP_SRC_LOCAL);
218
219 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
220 if (dp->scd_path == NULL) {
221 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
222 "none", 0, ZPROP_SRC_LOCAL);
223 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
224 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
225 dp->scd_path, 0, ZPROP_SRC_LOCAL);
226 }
227 }
228}
229
230/*
231 * Get zpool property values.
232 */
233int
234spa_prop_get(spa_t *spa, nvlist_t **nvp)
235{
236 objset_t *mos = spa->spa_meta_objset;
237 zap_cursor_t zc;
238 zap_attribute_t za;
239 int err;
240
241 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
242
243 mutex_enter(&spa->spa_props_lock);
244
245 /*
246 * Get properties from the spa config.
247 */
248 spa_prop_get_config(spa, nvp);
249
250 /* If no pool property object, no more prop to get. */
251 if (mos == NULL || spa->spa_pool_props_object == 0) {
252 mutex_exit(&spa->spa_props_lock);
253 return (0);
254 }
255
256 /*
257 * Get properties from the MOS pool property object.
258 */
259 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
260 (err = zap_cursor_retrieve(&zc, &za)) == 0;
261 zap_cursor_advance(&zc)) {
262 uint64_t intval = 0;
263 char *strval = NULL;
264 zprop_source_t src = ZPROP_SRC_DEFAULT;
265 zpool_prop_t prop;
266
267 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
268 continue;
269
270 switch (za.za_integer_length) {
271 case 8:
272 /* integer property */
273 if (za.za_first_integer !=
274 zpool_prop_default_numeric(prop))
275 src = ZPROP_SRC_LOCAL;
276
277 if (prop == ZPOOL_PROP_BOOTFS) {
278 dsl_pool_t *dp;
279 dsl_dataset_t *ds = NULL;
280
281 dp = spa_get_dsl(spa);
282 rw_enter(&dp->dp_config_rwlock, RW_READER);
283 if (err = dsl_dataset_hold_obj(dp,
284 za.za_first_integer, FTAG, &ds)) {
285 rw_exit(&dp->dp_config_rwlock);
286 break;
287 }
288
289 strval = kmem_alloc(
290 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
291 KM_SLEEP);
292 dsl_dataset_name(ds, strval);
293 dsl_dataset_rele(ds, FTAG);
294 rw_exit(&dp->dp_config_rwlock);
295 } else {
296 strval = NULL;
297 intval = za.za_first_integer;
298 }
299
300 spa_prop_add_list(*nvp, prop, strval, intval, src);
301
302 if (strval != NULL)
303 kmem_free(strval,
304 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
305
306 break;
307
308 case 1:
309 /* string property */
310 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
311 err = zap_lookup(mos, spa->spa_pool_props_object,
312 za.za_name, 1, za.za_num_integers, strval);
313 if (err) {
314 kmem_free(strval, za.za_num_integers);
315 break;
316 }
317 spa_prop_add_list(*nvp, prop, strval, 0, src);
318 kmem_free(strval, za.za_num_integers);
319 break;
320
321 default:
322 break;
323 }
324 }
325 zap_cursor_fini(&zc);
326 mutex_exit(&spa->spa_props_lock);
327out:
328 if (err && err != ENOENT) {
329 nvlist_free(*nvp);
330 *nvp = NULL;
331 return (err);
332 }
333
334 return (0);
335}
336
337/*
338 * Validate the given pool properties nvlist and modify the list
339 * for the property values to be set.
340 */
341static int
342spa_prop_validate(spa_t *spa, nvlist_t *props)
343{
344 nvpair_t *elem;
345 int error = 0, reset_bootfs = 0;
346 uint64_t objnum;
347
348 elem = NULL;
349 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
350 zpool_prop_t prop;
351 char *propname, *strval;
352 uint64_t intval;
353 objset_t *os;
221 if (spa->spa_root != NULL)
222 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
223 0, ZPROP_SRC_LOCAL);
224
225 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
226 if (dp->scd_path == NULL) {
227 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
228 "none", 0, ZPROP_SRC_LOCAL);
229 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
230 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
231 dp->scd_path, 0, ZPROP_SRC_LOCAL);
232 }
233 }
234}
235
236/*
237 * Get zpool property values.
238 */
239int
240spa_prop_get(spa_t *spa, nvlist_t **nvp)
241{
242 objset_t *mos = spa->spa_meta_objset;
243 zap_cursor_t zc;
244 zap_attribute_t za;
245 int err;
246
247 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
248
249 mutex_enter(&spa->spa_props_lock);
250
251 /*
252 * Get properties from the spa config.
253 */
254 spa_prop_get_config(spa, nvp);
255
256 /* If no pool property object, no more prop to get. */
257 if (mos == NULL || spa->spa_pool_props_object == 0) {
258 mutex_exit(&spa->spa_props_lock);
259 return (0);
260 }
261
262 /*
263 * Get properties from the MOS pool property object.
264 */
265 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
266 (err = zap_cursor_retrieve(&zc, &za)) == 0;
267 zap_cursor_advance(&zc)) {
268 uint64_t intval = 0;
269 char *strval = NULL;
270 zprop_source_t src = ZPROP_SRC_DEFAULT;
271 zpool_prop_t prop;
272
273 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
274 continue;
275
276 switch (za.za_integer_length) {
277 case 8:
278 /* integer property */
279 if (za.za_first_integer !=
280 zpool_prop_default_numeric(prop))
281 src = ZPROP_SRC_LOCAL;
282
283 if (prop == ZPOOL_PROP_BOOTFS) {
284 dsl_pool_t *dp;
285 dsl_dataset_t *ds = NULL;
286
287 dp = spa_get_dsl(spa);
288 rw_enter(&dp->dp_config_rwlock, RW_READER);
289 if (err = dsl_dataset_hold_obj(dp,
290 za.za_first_integer, FTAG, &ds)) {
291 rw_exit(&dp->dp_config_rwlock);
292 break;
293 }
294
295 strval = kmem_alloc(
296 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
297 KM_SLEEP);
298 dsl_dataset_name(ds, strval);
299 dsl_dataset_rele(ds, FTAG);
300 rw_exit(&dp->dp_config_rwlock);
301 } else {
302 strval = NULL;
303 intval = za.za_first_integer;
304 }
305
306 spa_prop_add_list(*nvp, prop, strval, intval, src);
307
308 if (strval != NULL)
309 kmem_free(strval,
310 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
311
312 break;
313
314 case 1:
315 /* string property */
316 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
317 err = zap_lookup(mos, spa->spa_pool_props_object,
318 za.za_name, 1, za.za_num_integers, strval);
319 if (err) {
320 kmem_free(strval, za.za_num_integers);
321 break;
322 }
323 spa_prop_add_list(*nvp, prop, strval, 0, src);
324 kmem_free(strval, za.za_num_integers);
325 break;
326
327 default:
328 break;
329 }
330 }
331 zap_cursor_fini(&zc);
332 mutex_exit(&spa->spa_props_lock);
333out:
334 if (err && err != ENOENT) {
335 nvlist_free(*nvp);
336 *nvp = NULL;
337 return (err);
338 }
339
340 return (0);
341}
342
343/*
344 * Validate the given pool properties nvlist and modify the list
345 * for the property values to be set.
346 */
347static int
348spa_prop_validate(spa_t *spa, nvlist_t *props)
349{
350 nvpair_t *elem;
351 int error = 0, reset_bootfs = 0;
352 uint64_t objnum;
353
354 elem = NULL;
355 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
356 zpool_prop_t prop;
357 char *propname, *strval;
358 uint64_t intval;
359 objset_t *os;
354 char *slash;
360 char *slash, *check;
355
356 propname = nvpair_name(elem);
357
358 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
359 return (EINVAL);
360
361 switch (prop) {
362 case ZPOOL_PROP_VERSION:
363 error = nvpair_value_uint64(elem, &intval);
364 if (!error &&
365 (intval < spa_version(spa) || intval > SPA_VERSION))
366 error = EINVAL;
367 break;
368
369 case ZPOOL_PROP_DELEGATION:
370 case ZPOOL_PROP_AUTOREPLACE:
371 case ZPOOL_PROP_LISTSNAPS:
372 case ZPOOL_PROP_AUTOEXPAND:
373 error = nvpair_value_uint64(elem, &intval);
374 if (!error && intval > 1)
375 error = EINVAL;
376 break;
377
378 case ZPOOL_PROP_BOOTFS:
379 /*
380 * If the pool version is less than SPA_VERSION_BOOTFS,
381 * or the pool is still being created (version == 0),
382 * the bootfs property cannot be set.
383 */
384 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
385 error = ENOTSUP;
386 break;
387 }
388
389 /*
390 * Make sure the vdev config is bootable
391 */
392 if (!vdev_is_bootable(spa->spa_root_vdev)) {
393 error = ENOTSUP;
394 break;
395 }
396
397 reset_bootfs = 1;
398
399 error = nvpair_value_string(elem, &strval);
400
401 if (!error) {
402 uint64_t compress;
403
404 if (strval == NULL || strval[0] == '\0') {
405 objnum = zpool_prop_default_numeric(
406 ZPOOL_PROP_BOOTFS);
407 break;
408 }
409
410 if (error = dmu_objset_hold(strval, FTAG, &os))
411 break;
412
413 /* Must be ZPL and not gzip compressed. */
414
415 if (dmu_objset_type(os) != DMU_OST_ZFS) {
416 error = ENOTSUP;
417 } else if ((error = dsl_prop_get_integer(strval,
418 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
419 &compress, NULL)) == 0 &&
420 !BOOTFS_COMPRESS_VALID(compress)) {
421 error = ENOTSUP;
422 } else {
423 objnum = dmu_objset_id(os);
424 }
425 dmu_objset_rele(os, FTAG);
426 }
427 break;
428
429 case ZPOOL_PROP_FAILUREMODE:
430 error = nvpair_value_uint64(elem, &intval);
431 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
432 intval > ZIO_FAILURE_MODE_PANIC))
433 error = EINVAL;
434
435 /*
436 * This is a special case which only occurs when
437 * the pool has completely failed. This allows
438 * the user to change the in-core failmode property
439 * without syncing it out to disk (I/Os might
440 * currently be blocked). We do this by returning
441 * EIO to the caller (spa_prop_set) to trick it
442 * into thinking we encountered a property validation
443 * error.
444 */
445 if (!error && spa_suspended(spa)) {
446 spa->spa_failmode = intval;
447 error = EIO;
448 }
449 break;
450
451 case ZPOOL_PROP_CACHEFILE:
452 if ((error = nvpair_value_string(elem, &strval)) != 0)
453 break;
454
455 if (strval[0] == '\0')
456 break;
457
458 if (strcmp(strval, "none") == 0)
459 break;
460
461 if (strval[0] != '/') {
462 error = EINVAL;
463 break;
464 }
465
466 slash = strrchr(strval, '/');
467 ASSERT(slash != NULL);
468
469 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
470 strcmp(slash, "/..") == 0)
471 error = EINVAL;
472 break;
473
361
362 propname = nvpair_name(elem);
363
364 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
365 return (EINVAL);
366
367 switch (prop) {
368 case ZPOOL_PROP_VERSION:
369 error = nvpair_value_uint64(elem, &intval);
370 if (!error &&
371 (intval < spa_version(spa) || intval > SPA_VERSION))
372 error = EINVAL;
373 break;
374
375 case ZPOOL_PROP_DELEGATION:
376 case ZPOOL_PROP_AUTOREPLACE:
377 case ZPOOL_PROP_LISTSNAPS:
378 case ZPOOL_PROP_AUTOEXPAND:
379 error = nvpair_value_uint64(elem, &intval);
380 if (!error && intval > 1)
381 error = EINVAL;
382 break;
383
384 case ZPOOL_PROP_BOOTFS:
385 /*
386 * If the pool version is less than SPA_VERSION_BOOTFS,
387 * or the pool is still being created (version == 0),
388 * the bootfs property cannot be set.
389 */
390 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
391 error = ENOTSUP;
392 break;
393 }
394
395 /*
396 * Make sure the vdev config is bootable
397 */
398 if (!vdev_is_bootable(spa->spa_root_vdev)) {
399 error = ENOTSUP;
400 break;
401 }
402
403 reset_bootfs = 1;
404
405 error = nvpair_value_string(elem, &strval);
406
407 if (!error) {
408 uint64_t compress;
409
410 if (strval == NULL || strval[0] == '\0') {
411 objnum = zpool_prop_default_numeric(
412 ZPOOL_PROP_BOOTFS);
413 break;
414 }
415
416 if (error = dmu_objset_hold(strval, FTAG, &os))
417 break;
418
419 /* Must be ZPL and not gzip compressed. */
420
421 if (dmu_objset_type(os) != DMU_OST_ZFS) {
422 error = ENOTSUP;
423 } else if ((error = dsl_prop_get_integer(strval,
424 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
425 &compress, NULL)) == 0 &&
426 !BOOTFS_COMPRESS_VALID(compress)) {
427 error = ENOTSUP;
428 } else {
429 objnum = dmu_objset_id(os);
430 }
431 dmu_objset_rele(os, FTAG);
432 }
433 break;
434
435 case ZPOOL_PROP_FAILUREMODE:
436 error = nvpair_value_uint64(elem, &intval);
437 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
438 intval > ZIO_FAILURE_MODE_PANIC))
439 error = EINVAL;
440
441 /*
442 * This is a special case which only occurs when
443 * the pool has completely failed. This allows
444 * the user to change the in-core failmode property
445 * without syncing it out to disk (I/Os might
446 * currently be blocked). We do this by returning
447 * EIO to the caller (spa_prop_set) to trick it
448 * into thinking we encountered a property validation
449 * error.
450 */
451 if (!error && spa_suspended(spa)) {
452 spa->spa_failmode = intval;
453 error = EIO;
454 }
455 break;
456
457 case ZPOOL_PROP_CACHEFILE:
458 if ((error = nvpair_value_string(elem, &strval)) != 0)
459 break;
460
461 if (strval[0] == '\0')
462 break;
463
464 if (strcmp(strval, "none") == 0)
465 break;
466
467 if (strval[0] != '/') {
468 error = EINVAL;
469 break;
470 }
471
472 slash = strrchr(strval, '/');
473 ASSERT(slash != NULL);
474
475 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
476 strcmp(slash, "/..") == 0)
477 error = EINVAL;
478 break;
479
480 case ZPOOL_PROP_COMMENT:
481 if ((error = nvpair_value_string(elem, &strval)) != 0)
482 break;
483 for (check = strval; *check != '\0'; check++) {
484 /*
485 * The kernel doesn't have an easy isprint()
486 * check. For this kernel check, we merely
487 * check ASCII apart from DEL. Fix this if
488 * there is an easy-to-use kernel isprint().
489 */
490 if (*check >= 0x7f) {
491 error = EINVAL;
492 break;
493 }
494 check++;
495 }
496 if (strlen(strval) > ZPROP_MAX_COMMENT)
497 error = E2BIG;
498 break;
499
474 case ZPOOL_PROP_DEDUPDITTO:
475 if (spa_version(spa) < SPA_VERSION_DEDUP)
476 error = ENOTSUP;
477 else
478 error = nvpair_value_uint64(elem, &intval);
479 if (error == 0 &&
480 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
481 error = EINVAL;
482 break;
483 }
484
485 if (error)
486 break;
487 }
488
489 if (!error && reset_bootfs) {
490 error = nvlist_remove(props,
491 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
492
493 if (!error) {
494 error = nvlist_add_uint64(props,
495 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
496 }
497 }
498
499 return (error);
500}
501
502void
503spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
504{
505 char *cachefile;
506 spa_config_dirent_t *dp;
507
508 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
509 &cachefile) != 0)
510 return;
511
512 dp = kmem_alloc(sizeof (spa_config_dirent_t),
513 KM_SLEEP);
514
515 if (cachefile[0] == '\0')
516 dp->scd_path = spa_strdup(spa_config_path);
517 else if (strcmp(cachefile, "none") == 0)
518 dp->scd_path = NULL;
519 else
520 dp->scd_path = spa_strdup(cachefile);
521
522 list_insert_head(&spa->spa_config_list, dp);
523 if (need_sync)
524 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
525}
526
527int
528spa_prop_set(spa_t *spa, nvlist_t *nvp)
529{
530 int error;
531 nvpair_t *elem;
532 boolean_t need_sync = B_FALSE;
533 zpool_prop_t prop;
534
535 if ((error = spa_prop_validate(spa, nvp)) != 0)
536 return (error);
537
538 elem = NULL;
539 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
540 if ((prop = zpool_name_to_prop(
541 nvpair_name(elem))) == ZPROP_INVAL)
542 return (EINVAL);
543
544 if (prop == ZPOOL_PROP_CACHEFILE ||
545 prop == ZPOOL_PROP_ALTROOT ||
546 prop == ZPOOL_PROP_READONLY)
547 continue;
548
549 need_sync = B_TRUE;
550 break;
551 }
552
553 if (need_sync)
554 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
555 spa, nvp, 3));
556 else
557 return (0);
558}
559
560/*
561 * If the bootfs property value is dsobj, clear it.
562 */
563void
564spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
565{
566 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
567 VERIFY(zap_remove(spa->spa_meta_objset,
568 spa->spa_pool_props_object,
569 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
570 spa->spa_bootfs = 0;
571 }
572}
573
574/*
500 case ZPOOL_PROP_DEDUPDITTO:
501 if (spa_version(spa) < SPA_VERSION_DEDUP)
502 error = ENOTSUP;
503 else
504 error = nvpair_value_uint64(elem, &intval);
505 if (error == 0 &&
506 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
507 error = EINVAL;
508 break;
509 }
510
511 if (error)
512 break;
513 }
514
515 if (!error && reset_bootfs) {
516 error = nvlist_remove(props,
517 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
518
519 if (!error) {
520 error = nvlist_add_uint64(props,
521 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
522 }
523 }
524
525 return (error);
526}
527
528void
529spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
530{
531 char *cachefile;
532 spa_config_dirent_t *dp;
533
534 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
535 &cachefile) != 0)
536 return;
537
538 dp = kmem_alloc(sizeof (spa_config_dirent_t),
539 KM_SLEEP);
540
541 if (cachefile[0] == '\0')
542 dp->scd_path = spa_strdup(spa_config_path);
543 else if (strcmp(cachefile, "none") == 0)
544 dp->scd_path = NULL;
545 else
546 dp->scd_path = spa_strdup(cachefile);
547
548 list_insert_head(&spa->spa_config_list, dp);
549 if (need_sync)
550 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
551}
552
553int
554spa_prop_set(spa_t *spa, nvlist_t *nvp)
555{
556 int error;
557 nvpair_t *elem;
558 boolean_t need_sync = B_FALSE;
559 zpool_prop_t prop;
560
561 if ((error = spa_prop_validate(spa, nvp)) != 0)
562 return (error);
563
564 elem = NULL;
565 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
566 if ((prop = zpool_name_to_prop(
567 nvpair_name(elem))) == ZPROP_INVAL)
568 return (EINVAL);
569
570 if (prop == ZPOOL_PROP_CACHEFILE ||
571 prop == ZPOOL_PROP_ALTROOT ||
572 prop == ZPOOL_PROP_READONLY)
573 continue;
574
575 need_sync = B_TRUE;
576 break;
577 }
578
579 if (need_sync)
580 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
581 spa, nvp, 3));
582 else
583 return (0);
584}
585
586/*
587 * If the bootfs property value is dsobj, clear it.
588 */
589void
590spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
591{
592 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
593 VERIFY(zap_remove(spa->spa_meta_objset,
594 spa->spa_pool_props_object,
595 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
596 spa->spa_bootfs = 0;
597 }
598}
599
600/*
601 * Change the GUID for the pool. This is done so that we can later
602 * re-import a pool built from a clone of our own vdevs. We will modify
603 * the root vdev's guid, our own pool guid, and then mark all of our
604 * vdevs dirty. Note that we must make sure that all our vdevs are
605 * online when we do this, or else any vdevs that weren't present
606 * would be orphaned from our pool. We are also going to issue a
607 * sysevent to update any watchers.
608 */
609int
610spa_change_guid(spa_t *spa)
611{
612 uint64_t oldguid, newguid;
613 uint64_t txg;
614
615 if (!(spa_mode_global & FWRITE))
616 return (EROFS);
617
618 txg = spa_vdev_enter(spa);
619
620 if (spa->spa_root_vdev->vdev_state != VDEV_STATE_HEALTHY)
621 return (spa_vdev_exit(spa, NULL, txg, ENXIO));
622
623 oldguid = spa_guid(spa);
624 newguid = spa_generate_guid(NULL);
625 ASSERT3U(oldguid, !=, newguid);
626
627 spa->spa_root_vdev->vdev_guid = newguid;
628 spa->spa_root_vdev->vdev_guid_sum += (newguid - oldguid);
629
630 vdev_config_dirty(spa->spa_root_vdev);
631
632 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
633
634 return (spa_vdev_exit(spa, NULL, txg, 0));
635}
636
637/*
575 * ==========================================================================
576 * SPA state manipulation (open/create/destroy/import/export)
577 * ==========================================================================
578 */
579
580static int
581spa_error_entry_compare(const void *a, const void *b)
582{
583 spa_error_entry_t *sa = (spa_error_entry_t *)a;
584 spa_error_entry_t *sb = (spa_error_entry_t *)b;
585 int ret;
586
587 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
588 sizeof (zbookmark_t));
589
590 if (ret < 0)
591 return (-1);
592 else if (ret > 0)
593 return (1);
594 else
595 return (0);
596}
597
598/*
599 * Utility function which retrieves copies of the current logs and
600 * re-initializes them in the process.
601 */
602void
603spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
604{
605 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
606
607 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
608 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
609
610 avl_create(&spa->spa_errlist_scrub,
611 spa_error_entry_compare, sizeof (spa_error_entry_t),
612 offsetof(spa_error_entry_t, se_avl));
613 avl_create(&spa->spa_errlist_last,
614 spa_error_entry_compare, sizeof (spa_error_entry_t),
615 offsetof(spa_error_entry_t, se_avl));
616}
617
618static taskq_t *
619spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
620 uint_t value)
621{
622 uint_t flags = TASKQ_PREPOPULATE;
623 boolean_t batch = B_FALSE;
624
625 switch (mode) {
626 case zti_mode_null:
627 return (NULL); /* no taskq needed */
628
629 case zti_mode_fixed:
630 ASSERT3U(value, >=, 1);
631 value = MAX(value, 1);
632 break;
633
634 case zti_mode_batch:
635 batch = B_TRUE;
636 flags |= TASKQ_THREADS_CPU_PCT;
637 value = zio_taskq_batch_pct;
638 break;
639
640 case zti_mode_online_percent:
641 flags |= TASKQ_THREADS_CPU_PCT;
642 break;
643
644 default:
645 panic("unrecognized mode for %s taskq (%u:%u) in "
646 "spa_activate()",
647 name, mode, value);
648 break;
649 }
650
651#ifdef SYSDC
652 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
653 if (batch)
654 flags |= TASKQ_DC_BATCH;
655
656 return (taskq_create_sysdc(name, value, 50, INT_MAX,
657 spa->spa_proc, zio_taskq_basedc, flags));
658 }
659#endif
660 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
661 spa->spa_proc, flags));
662}
663
664static void
665spa_create_zio_taskqs(spa_t *spa)
666{
667 for (int t = 0; t < ZIO_TYPES; t++) {
668 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
669 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
670 enum zti_modes mode = ztip->zti_mode;
671 uint_t value = ztip->zti_value;
672 char name[32];
673
674 (void) snprintf(name, sizeof (name),
675 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
676
677 spa->spa_zio_taskq[t][q] =
678 spa_taskq_create(spa, name, mode, value);
679 }
680 }
681}
682
683#ifdef _KERNEL
684#ifdef SPA_PROCESS
685static void
686spa_thread(void *arg)
687{
688 callb_cpr_t cprinfo;
689
690 spa_t *spa = arg;
691 user_t *pu = PTOU(curproc);
692
693 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
694 spa->spa_name);
695
696 ASSERT(curproc != &p0);
697 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
698 "zpool-%s", spa->spa_name);
699 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
700
701#ifdef PSRSET_BIND
702 /* bind this thread to the requested psrset */
703 if (zio_taskq_psrset_bind != PS_NONE) {
704 pool_lock();
705 mutex_enter(&cpu_lock);
706 mutex_enter(&pidlock);
707 mutex_enter(&curproc->p_lock);
708
709 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
710 0, NULL, NULL) == 0) {
711 curthread->t_bind_pset = zio_taskq_psrset_bind;
712 } else {
713 cmn_err(CE_WARN,
714 "Couldn't bind process for zfs pool \"%s\" to "
715 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
716 }
717
718 mutex_exit(&curproc->p_lock);
719 mutex_exit(&pidlock);
720 mutex_exit(&cpu_lock);
721 pool_unlock();
722 }
723#endif
724
725#ifdef SYSDC
726 if (zio_taskq_sysdc) {
727 sysdc_thread_enter(curthread, 100, 0);
728 }
729#endif
730
731 spa->spa_proc = curproc;
732 spa->spa_did = curthread->t_did;
733
734 spa_create_zio_taskqs(spa);
735
736 mutex_enter(&spa->spa_proc_lock);
737 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
738
739 spa->spa_proc_state = SPA_PROC_ACTIVE;
740 cv_broadcast(&spa->spa_proc_cv);
741
742 CALLB_CPR_SAFE_BEGIN(&cprinfo);
743 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
744 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
745 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
746
747 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
748 spa->spa_proc_state = SPA_PROC_GONE;
749 spa->spa_proc = &p0;
750 cv_broadcast(&spa->spa_proc_cv);
751 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
752
753 mutex_enter(&curproc->p_lock);
754 lwp_exit();
755}
756#endif /* SPA_PROCESS */
757#endif
758
759/*
760 * Activate an uninitialized pool.
761 */
762static void
763spa_activate(spa_t *spa, int mode)
764{
765 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
766
767 spa->spa_state = POOL_STATE_ACTIVE;
768 spa->spa_mode = mode;
769
770 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
771 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
772
773 /* Try to create a covering process */
774 mutex_enter(&spa->spa_proc_lock);
775 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
776 ASSERT(spa->spa_proc == &p0);
777 spa->spa_did = 0;
778
779#ifdef SPA_PROCESS
780 /* Only create a process if we're going to be around a while. */
781 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
782 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
783 NULL, 0) == 0) {
784 spa->spa_proc_state = SPA_PROC_CREATED;
785 while (spa->spa_proc_state == SPA_PROC_CREATED) {
786 cv_wait(&spa->spa_proc_cv,
787 &spa->spa_proc_lock);
788 }
789 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
790 ASSERT(spa->spa_proc != &p0);
791 ASSERT(spa->spa_did != 0);
792 } else {
793#ifdef _KERNEL
794 cmn_err(CE_WARN,
795 "Couldn't create process for zfs pool \"%s\"\n",
796 spa->spa_name);
797#endif
798 }
799 }
800#endif /* SPA_PROCESS */
801 mutex_exit(&spa->spa_proc_lock);
802
803 /* If we didn't create a process, we need to create our taskqs. */
804 ASSERT(spa->spa_proc == &p0);
805 if (spa->spa_proc == &p0) {
806 spa_create_zio_taskqs(spa);
807 }
808
809 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
810 offsetof(vdev_t, vdev_config_dirty_node));
811 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
812 offsetof(vdev_t, vdev_state_dirty_node));
813
814 txg_list_create(&spa->spa_vdev_txg_list,
815 offsetof(struct vdev, vdev_txg_node));
816
817 avl_create(&spa->spa_errlist_scrub,
818 spa_error_entry_compare, sizeof (spa_error_entry_t),
819 offsetof(spa_error_entry_t, se_avl));
820 avl_create(&spa->spa_errlist_last,
821 spa_error_entry_compare, sizeof (spa_error_entry_t),
822 offsetof(spa_error_entry_t, se_avl));
823}
824
825/*
826 * Opposite of spa_activate().
827 */
828static void
829spa_deactivate(spa_t *spa)
830{
831 ASSERT(spa->spa_sync_on == B_FALSE);
832 ASSERT(spa->spa_dsl_pool == NULL);
833 ASSERT(spa->spa_root_vdev == NULL);
834 ASSERT(spa->spa_async_zio_root == NULL);
835 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
836
837 txg_list_destroy(&spa->spa_vdev_txg_list);
838
839 list_destroy(&spa->spa_config_dirty_list);
840 list_destroy(&spa->spa_state_dirty_list);
841
842 for (int t = 0; t < ZIO_TYPES; t++) {
843 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
844 if (spa->spa_zio_taskq[t][q] != NULL)
845 taskq_destroy(spa->spa_zio_taskq[t][q]);
846 spa->spa_zio_taskq[t][q] = NULL;
847 }
848 }
849
850 metaslab_class_destroy(spa->spa_normal_class);
851 spa->spa_normal_class = NULL;
852
853 metaslab_class_destroy(spa->spa_log_class);
854 spa->spa_log_class = NULL;
855
856 /*
857 * If this was part of an import or the open otherwise failed, we may
858 * still have errors left in the queues. Empty them just in case.
859 */
860 spa_errlog_drain(spa);
861
862 avl_destroy(&spa->spa_errlist_scrub);
863 avl_destroy(&spa->spa_errlist_last);
864
865 spa->spa_state = POOL_STATE_UNINITIALIZED;
866
867 mutex_enter(&spa->spa_proc_lock);
868 if (spa->spa_proc_state != SPA_PROC_NONE) {
869 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
870 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
871 cv_broadcast(&spa->spa_proc_cv);
872 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
873 ASSERT(spa->spa_proc != &p0);
874 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
875 }
876 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
877 spa->spa_proc_state = SPA_PROC_NONE;
878 }
879 ASSERT(spa->spa_proc == &p0);
880 mutex_exit(&spa->spa_proc_lock);
881
882#ifdef SPA_PROCESS
883 /*
884 * We want to make sure spa_thread() has actually exited the ZFS
885 * module, so that the module can't be unloaded out from underneath
886 * it.
887 */
888 if (spa->spa_did != 0) {
889 thread_join(spa->spa_did);
890 spa->spa_did = 0;
891 }
892#endif /* SPA_PROCESS */
893}
894
895/*
896 * Verify a pool configuration, and construct the vdev tree appropriately. This
897 * will create all the necessary vdevs in the appropriate layout, with each vdev
898 * in the CLOSED state. This will prep the pool before open/creation/import.
899 * All vdev validation is done by the vdev_alloc() routine.
900 */
901static int
902spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
903 uint_t id, int atype)
904{
905 nvlist_t **child;
906 uint_t children;
907 int error;
908
909 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
910 return (error);
911
912 if ((*vdp)->vdev_ops->vdev_op_leaf)
913 return (0);
914
915 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
916 &child, &children);
917
918 if (error == ENOENT)
919 return (0);
920
921 if (error) {
922 vdev_free(*vdp);
923 *vdp = NULL;
924 return (EINVAL);
925 }
926
927 for (int c = 0; c < children; c++) {
928 vdev_t *vd;
929 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
930 atype)) != 0) {
931 vdev_free(*vdp);
932 *vdp = NULL;
933 return (error);
934 }
935 }
936
937 ASSERT(*vdp != NULL);
938
939 return (0);
940}
941
942/*
943 * Opposite of spa_load().
944 */
945static void
946spa_unload(spa_t *spa)
947{
948 int i;
949
950 ASSERT(MUTEX_HELD(&spa_namespace_lock));
951
952 /*
953 * Stop async tasks.
954 */
955 spa_async_suspend(spa);
956
957 /*
958 * Stop syncing.
959 */
960 if (spa->spa_sync_on) {
961 txg_sync_stop(spa->spa_dsl_pool);
962 spa->spa_sync_on = B_FALSE;
963 }
964
965 /*
966 * Wait for any outstanding async I/O to complete.
967 */
968 if (spa->spa_async_zio_root != NULL) {
969 (void) zio_wait(spa->spa_async_zio_root);
970 spa->spa_async_zio_root = NULL;
971 }
972
973 bpobj_close(&spa->spa_deferred_bpobj);
974
975 /*
976 * Close the dsl pool.
977 */
978 if (spa->spa_dsl_pool) {
979 dsl_pool_close(spa->spa_dsl_pool);
980 spa->spa_dsl_pool = NULL;
981 spa->spa_meta_objset = NULL;
982 }
983
984 ddt_unload(spa);
985
986 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
987
988 /*
989 * Drop and purge level 2 cache
990 */
991 spa_l2cache_drop(spa);
992
993 /*
994 * Close all vdevs.
995 */
996 if (spa->spa_root_vdev)
997 vdev_free(spa->spa_root_vdev);
998 ASSERT(spa->spa_root_vdev == NULL);
999
1000 for (i = 0; i < spa->spa_spares.sav_count; i++)
1001 vdev_free(spa->spa_spares.sav_vdevs[i]);
1002 if (spa->spa_spares.sav_vdevs) {
1003 kmem_free(spa->spa_spares.sav_vdevs,
1004 spa->spa_spares.sav_count * sizeof (void *));
1005 spa->spa_spares.sav_vdevs = NULL;
1006 }
1007 if (spa->spa_spares.sav_config) {
1008 nvlist_free(spa->spa_spares.sav_config);
1009 spa->spa_spares.sav_config = NULL;
1010 }
1011 spa->spa_spares.sav_count = 0;
1012
1013 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
1014 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1015 if (spa->spa_l2cache.sav_vdevs) {
1016 kmem_free(spa->spa_l2cache.sav_vdevs,
1017 spa->spa_l2cache.sav_count * sizeof (void *));
1018 spa->spa_l2cache.sav_vdevs = NULL;
1019 }
1020 if (spa->spa_l2cache.sav_config) {
1021 nvlist_free(spa->spa_l2cache.sav_config);
1022 spa->spa_l2cache.sav_config = NULL;
1023 }
1024 spa->spa_l2cache.sav_count = 0;
1025
1026 spa->spa_async_suspended = 0;
1027
638 * ==========================================================================
639 * SPA state manipulation (open/create/destroy/import/export)
640 * ==========================================================================
641 */
642
643static int
644spa_error_entry_compare(const void *a, const void *b)
645{
646 spa_error_entry_t *sa = (spa_error_entry_t *)a;
647 spa_error_entry_t *sb = (spa_error_entry_t *)b;
648 int ret;
649
650 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
651 sizeof (zbookmark_t));
652
653 if (ret < 0)
654 return (-1);
655 else if (ret > 0)
656 return (1);
657 else
658 return (0);
659}
660
661/*
662 * Utility function which retrieves copies of the current logs and
663 * re-initializes them in the process.
664 */
665void
666spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
667{
668 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
669
670 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
671 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
672
673 avl_create(&spa->spa_errlist_scrub,
674 spa_error_entry_compare, sizeof (spa_error_entry_t),
675 offsetof(spa_error_entry_t, se_avl));
676 avl_create(&spa->spa_errlist_last,
677 spa_error_entry_compare, sizeof (spa_error_entry_t),
678 offsetof(spa_error_entry_t, se_avl));
679}
680
681static taskq_t *
682spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
683 uint_t value)
684{
685 uint_t flags = TASKQ_PREPOPULATE;
686 boolean_t batch = B_FALSE;
687
688 switch (mode) {
689 case zti_mode_null:
690 return (NULL); /* no taskq needed */
691
692 case zti_mode_fixed:
693 ASSERT3U(value, >=, 1);
694 value = MAX(value, 1);
695 break;
696
697 case zti_mode_batch:
698 batch = B_TRUE;
699 flags |= TASKQ_THREADS_CPU_PCT;
700 value = zio_taskq_batch_pct;
701 break;
702
703 case zti_mode_online_percent:
704 flags |= TASKQ_THREADS_CPU_PCT;
705 break;
706
707 default:
708 panic("unrecognized mode for %s taskq (%u:%u) in "
709 "spa_activate()",
710 name, mode, value);
711 break;
712 }
713
714#ifdef SYSDC
715 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
716 if (batch)
717 flags |= TASKQ_DC_BATCH;
718
719 return (taskq_create_sysdc(name, value, 50, INT_MAX,
720 spa->spa_proc, zio_taskq_basedc, flags));
721 }
722#endif
723 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
724 spa->spa_proc, flags));
725}
726
727static void
728spa_create_zio_taskqs(spa_t *spa)
729{
730 for (int t = 0; t < ZIO_TYPES; t++) {
731 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
732 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
733 enum zti_modes mode = ztip->zti_mode;
734 uint_t value = ztip->zti_value;
735 char name[32];
736
737 (void) snprintf(name, sizeof (name),
738 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
739
740 spa->spa_zio_taskq[t][q] =
741 spa_taskq_create(spa, name, mode, value);
742 }
743 }
744}
745
746#ifdef _KERNEL
747#ifdef SPA_PROCESS
748static void
749spa_thread(void *arg)
750{
751 callb_cpr_t cprinfo;
752
753 spa_t *spa = arg;
754 user_t *pu = PTOU(curproc);
755
756 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
757 spa->spa_name);
758
759 ASSERT(curproc != &p0);
760 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
761 "zpool-%s", spa->spa_name);
762 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
763
764#ifdef PSRSET_BIND
765 /* bind this thread to the requested psrset */
766 if (zio_taskq_psrset_bind != PS_NONE) {
767 pool_lock();
768 mutex_enter(&cpu_lock);
769 mutex_enter(&pidlock);
770 mutex_enter(&curproc->p_lock);
771
772 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
773 0, NULL, NULL) == 0) {
774 curthread->t_bind_pset = zio_taskq_psrset_bind;
775 } else {
776 cmn_err(CE_WARN,
777 "Couldn't bind process for zfs pool \"%s\" to "
778 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
779 }
780
781 mutex_exit(&curproc->p_lock);
782 mutex_exit(&pidlock);
783 mutex_exit(&cpu_lock);
784 pool_unlock();
785 }
786#endif
787
788#ifdef SYSDC
789 if (zio_taskq_sysdc) {
790 sysdc_thread_enter(curthread, 100, 0);
791 }
792#endif
793
794 spa->spa_proc = curproc;
795 spa->spa_did = curthread->t_did;
796
797 spa_create_zio_taskqs(spa);
798
799 mutex_enter(&spa->spa_proc_lock);
800 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
801
802 spa->spa_proc_state = SPA_PROC_ACTIVE;
803 cv_broadcast(&spa->spa_proc_cv);
804
805 CALLB_CPR_SAFE_BEGIN(&cprinfo);
806 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
807 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
808 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
809
810 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
811 spa->spa_proc_state = SPA_PROC_GONE;
812 spa->spa_proc = &p0;
813 cv_broadcast(&spa->spa_proc_cv);
814 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
815
816 mutex_enter(&curproc->p_lock);
817 lwp_exit();
818}
819#endif /* SPA_PROCESS */
820#endif
821
822/*
823 * Activate an uninitialized pool.
824 */
825static void
826spa_activate(spa_t *spa, int mode)
827{
828 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
829
830 spa->spa_state = POOL_STATE_ACTIVE;
831 spa->spa_mode = mode;
832
833 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
834 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
835
836 /* Try to create a covering process */
837 mutex_enter(&spa->spa_proc_lock);
838 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
839 ASSERT(spa->spa_proc == &p0);
840 spa->spa_did = 0;
841
842#ifdef SPA_PROCESS
843 /* Only create a process if we're going to be around a while. */
844 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
845 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
846 NULL, 0) == 0) {
847 spa->spa_proc_state = SPA_PROC_CREATED;
848 while (spa->spa_proc_state == SPA_PROC_CREATED) {
849 cv_wait(&spa->spa_proc_cv,
850 &spa->spa_proc_lock);
851 }
852 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
853 ASSERT(spa->spa_proc != &p0);
854 ASSERT(spa->spa_did != 0);
855 } else {
856#ifdef _KERNEL
857 cmn_err(CE_WARN,
858 "Couldn't create process for zfs pool \"%s\"\n",
859 spa->spa_name);
860#endif
861 }
862 }
863#endif /* SPA_PROCESS */
864 mutex_exit(&spa->spa_proc_lock);
865
866 /* If we didn't create a process, we need to create our taskqs. */
867 ASSERT(spa->spa_proc == &p0);
868 if (spa->spa_proc == &p0) {
869 spa_create_zio_taskqs(spa);
870 }
871
872 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
873 offsetof(vdev_t, vdev_config_dirty_node));
874 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
875 offsetof(vdev_t, vdev_state_dirty_node));
876
877 txg_list_create(&spa->spa_vdev_txg_list,
878 offsetof(struct vdev, vdev_txg_node));
879
880 avl_create(&spa->spa_errlist_scrub,
881 spa_error_entry_compare, sizeof (spa_error_entry_t),
882 offsetof(spa_error_entry_t, se_avl));
883 avl_create(&spa->spa_errlist_last,
884 spa_error_entry_compare, sizeof (spa_error_entry_t),
885 offsetof(spa_error_entry_t, se_avl));
886}
887
888/*
889 * Opposite of spa_activate().
890 */
891static void
892spa_deactivate(spa_t *spa)
893{
894 ASSERT(spa->spa_sync_on == B_FALSE);
895 ASSERT(spa->spa_dsl_pool == NULL);
896 ASSERT(spa->spa_root_vdev == NULL);
897 ASSERT(spa->spa_async_zio_root == NULL);
898 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
899
900 txg_list_destroy(&spa->spa_vdev_txg_list);
901
902 list_destroy(&spa->spa_config_dirty_list);
903 list_destroy(&spa->spa_state_dirty_list);
904
905 for (int t = 0; t < ZIO_TYPES; t++) {
906 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
907 if (spa->spa_zio_taskq[t][q] != NULL)
908 taskq_destroy(spa->spa_zio_taskq[t][q]);
909 spa->spa_zio_taskq[t][q] = NULL;
910 }
911 }
912
913 metaslab_class_destroy(spa->spa_normal_class);
914 spa->spa_normal_class = NULL;
915
916 metaslab_class_destroy(spa->spa_log_class);
917 spa->spa_log_class = NULL;
918
919 /*
920 * If this was part of an import or the open otherwise failed, we may
921 * still have errors left in the queues. Empty them just in case.
922 */
923 spa_errlog_drain(spa);
924
925 avl_destroy(&spa->spa_errlist_scrub);
926 avl_destroy(&spa->spa_errlist_last);
927
928 spa->spa_state = POOL_STATE_UNINITIALIZED;
929
930 mutex_enter(&spa->spa_proc_lock);
931 if (spa->spa_proc_state != SPA_PROC_NONE) {
932 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
933 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
934 cv_broadcast(&spa->spa_proc_cv);
935 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
936 ASSERT(spa->spa_proc != &p0);
937 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
938 }
939 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
940 spa->spa_proc_state = SPA_PROC_NONE;
941 }
942 ASSERT(spa->spa_proc == &p0);
943 mutex_exit(&spa->spa_proc_lock);
944
945#ifdef SPA_PROCESS
946 /*
947 * We want to make sure spa_thread() has actually exited the ZFS
948 * module, so that the module can't be unloaded out from underneath
949 * it.
950 */
951 if (spa->spa_did != 0) {
952 thread_join(spa->spa_did);
953 spa->spa_did = 0;
954 }
955#endif /* SPA_PROCESS */
956}
957
958/*
959 * Verify a pool configuration, and construct the vdev tree appropriately. This
960 * will create all the necessary vdevs in the appropriate layout, with each vdev
961 * in the CLOSED state. This will prep the pool before open/creation/import.
962 * All vdev validation is done by the vdev_alloc() routine.
963 */
964static int
965spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
966 uint_t id, int atype)
967{
968 nvlist_t **child;
969 uint_t children;
970 int error;
971
972 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
973 return (error);
974
975 if ((*vdp)->vdev_ops->vdev_op_leaf)
976 return (0);
977
978 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
979 &child, &children);
980
981 if (error == ENOENT)
982 return (0);
983
984 if (error) {
985 vdev_free(*vdp);
986 *vdp = NULL;
987 return (EINVAL);
988 }
989
990 for (int c = 0; c < children; c++) {
991 vdev_t *vd;
992 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
993 atype)) != 0) {
994 vdev_free(*vdp);
995 *vdp = NULL;
996 return (error);
997 }
998 }
999
1000 ASSERT(*vdp != NULL);
1001
1002 return (0);
1003}
1004
1005/*
1006 * Opposite of spa_load().
1007 */
1008static void
1009spa_unload(spa_t *spa)
1010{
1011 int i;
1012
1013 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1014
1015 /*
1016 * Stop async tasks.
1017 */
1018 spa_async_suspend(spa);
1019
1020 /*
1021 * Stop syncing.
1022 */
1023 if (spa->spa_sync_on) {
1024 txg_sync_stop(spa->spa_dsl_pool);
1025 spa->spa_sync_on = B_FALSE;
1026 }
1027
1028 /*
1029 * Wait for any outstanding async I/O to complete.
1030 */
1031 if (spa->spa_async_zio_root != NULL) {
1032 (void) zio_wait(spa->spa_async_zio_root);
1033 spa->spa_async_zio_root = NULL;
1034 }
1035
1036 bpobj_close(&spa->spa_deferred_bpobj);
1037
1038 /*
1039 * Close the dsl pool.
1040 */
1041 if (spa->spa_dsl_pool) {
1042 dsl_pool_close(spa->spa_dsl_pool);
1043 spa->spa_dsl_pool = NULL;
1044 spa->spa_meta_objset = NULL;
1045 }
1046
1047 ddt_unload(spa);
1048
1049 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1050
1051 /*
1052 * Drop and purge level 2 cache
1053 */
1054 spa_l2cache_drop(spa);
1055
1056 /*
1057 * Close all vdevs.
1058 */
1059 if (spa->spa_root_vdev)
1060 vdev_free(spa->spa_root_vdev);
1061 ASSERT(spa->spa_root_vdev == NULL);
1062
1063 for (i = 0; i < spa->spa_spares.sav_count; i++)
1064 vdev_free(spa->spa_spares.sav_vdevs[i]);
1065 if (spa->spa_spares.sav_vdevs) {
1066 kmem_free(spa->spa_spares.sav_vdevs,
1067 spa->spa_spares.sav_count * sizeof (void *));
1068 spa->spa_spares.sav_vdevs = NULL;
1069 }
1070 if (spa->spa_spares.sav_config) {
1071 nvlist_free(spa->spa_spares.sav_config);
1072 spa->spa_spares.sav_config = NULL;
1073 }
1074 spa->spa_spares.sav_count = 0;
1075
1076 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
1077 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1078 if (spa->spa_l2cache.sav_vdevs) {
1079 kmem_free(spa->spa_l2cache.sav_vdevs,
1080 spa->spa_l2cache.sav_count * sizeof (void *));
1081 spa->spa_l2cache.sav_vdevs = NULL;
1082 }
1083 if (spa->spa_l2cache.sav_config) {
1084 nvlist_free(spa->spa_l2cache.sav_config);
1085 spa->spa_l2cache.sav_config = NULL;
1086 }
1087 spa->spa_l2cache.sav_count = 0;
1088
1089 spa->spa_async_suspended = 0;
1090
1091 if (spa->spa_comment != NULL) {
1092 spa_strfree(spa->spa_comment);
1093 spa->spa_comment = NULL;
1094 }
1095
1028 spa_config_exit(spa, SCL_ALL, FTAG);
1029}
1030
1031/*
1032 * Load (or re-load) the current list of vdevs describing the active spares for
1033 * this pool. When this is called, we have some form of basic information in
1034 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1035 * then re-generate a more complete list including status information.
1036 */
1037static void
1038spa_load_spares(spa_t *spa)
1039{
1040 nvlist_t **spares;
1041 uint_t nspares;
1042 int i;
1043 vdev_t *vd, *tvd;
1044
1045 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1046
1047 /*
1048 * First, close and free any existing spare vdevs.
1049 */
1050 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1051 vd = spa->spa_spares.sav_vdevs[i];
1052
1053 /* Undo the call to spa_activate() below */
1054 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1055 B_FALSE)) != NULL && tvd->vdev_isspare)
1056 spa_spare_remove(tvd);
1057 vdev_close(vd);
1058 vdev_free(vd);
1059 }
1060
1061 if (spa->spa_spares.sav_vdevs)
1062 kmem_free(spa->spa_spares.sav_vdevs,
1063 spa->spa_spares.sav_count * sizeof (void *));
1064
1065 if (spa->spa_spares.sav_config == NULL)
1066 nspares = 0;
1067 else
1068 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1069 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1070
1071 spa->spa_spares.sav_count = (int)nspares;
1072 spa->spa_spares.sav_vdevs = NULL;
1073
1074 if (nspares == 0)
1075 return;
1076
1077 /*
1078 * Construct the array of vdevs, opening them to get status in the
1079 * process. For each spare, there is potentially two different vdev_t
1080 * structures associated with it: one in the list of spares (used only
1081 * for basic validation purposes) and one in the active vdev
1082 * configuration (if it's spared in). During this phase we open and
1083 * validate each vdev on the spare list. If the vdev also exists in the
1084 * active configuration, then we also mark this vdev as an active spare.
1085 */
1086 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1087 KM_SLEEP);
1088 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1089 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1090 VDEV_ALLOC_SPARE) == 0);
1091 ASSERT(vd != NULL);
1092
1093 spa->spa_spares.sav_vdevs[i] = vd;
1094
1095 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1096 B_FALSE)) != NULL) {
1097 if (!tvd->vdev_isspare)
1098 spa_spare_add(tvd);
1099
1100 /*
1101 * We only mark the spare active if we were successfully
1102 * able to load the vdev. Otherwise, importing a pool
1103 * with a bad active spare would result in strange
1104 * behavior, because multiple pool would think the spare
1105 * is actively in use.
1106 *
1107 * There is a vulnerability here to an equally bizarre
1108 * circumstance, where a dead active spare is later
1109 * brought back to life (onlined or otherwise). Given
1110 * the rarity of this scenario, and the extra complexity
1111 * it adds, we ignore the possibility.
1112 */
1113 if (!vdev_is_dead(tvd))
1114 spa_spare_activate(tvd);
1115 }
1116
1117 vd->vdev_top = vd;
1118 vd->vdev_aux = &spa->spa_spares;
1119
1120 if (vdev_open(vd) != 0)
1121 continue;
1122
1123 if (vdev_validate_aux(vd) == 0)
1124 spa_spare_add(vd);
1125 }
1126
1127 /*
1128 * Recompute the stashed list of spares, with status information
1129 * this time.
1130 */
1131 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1132 DATA_TYPE_NVLIST_ARRAY) == 0);
1133
1134 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1135 KM_SLEEP);
1136 for (i = 0; i < spa->spa_spares.sav_count; i++)
1137 spares[i] = vdev_config_generate(spa,
1138 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1139 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1140 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1141 for (i = 0; i < spa->spa_spares.sav_count; i++)
1142 nvlist_free(spares[i]);
1143 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1144}
1145
1146/*
1147 * Load (or re-load) the current list of vdevs describing the active l2cache for
1148 * this pool. When this is called, we have some form of basic information in
1149 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1150 * then re-generate a more complete list including status information.
1151 * Devices which are already active have their details maintained, and are
1152 * not re-opened.
1153 */
1154static void
1155spa_load_l2cache(spa_t *spa)
1156{
1157 nvlist_t **l2cache;
1158 uint_t nl2cache;
1159 int i, j, oldnvdevs;
1160 uint64_t guid;
1161 vdev_t *vd, **oldvdevs, **newvdevs;
1162 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1163
1164 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1165
1166 if (sav->sav_config != NULL) {
1167 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1168 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1169 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1170 } else {
1171 nl2cache = 0;
1172 }
1173
1174 oldvdevs = sav->sav_vdevs;
1175 oldnvdevs = sav->sav_count;
1176 sav->sav_vdevs = NULL;
1177 sav->sav_count = 0;
1178
1179 /*
1180 * Process new nvlist of vdevs.
1181 */
1182 for (i = 0; i < nl2cache; i++) {
1183 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1184 &guid) == 0);
1185
1186 newvdevs[i] = NULL;
1187 for (j = 0; j < oldnvdevs; j++) {
1188 vd = oldvdevs[j];
1189 if (vd != NULL && guid == vd->vdev_guid) {
1190 /*
1191 * Retain previous vdev for add/remove ops.
1192 */
1193 newvdevs[i] = vd;
1194 oldvdevs[j] = NULL;
1195 break;
1196 }
1197 }
1198
1199 if (newvdevs[i] == NULL) {
1200 /*
1201 * Create new vdev
1202 */
1203 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1204 VDEV_ALLOC_L2CACHE) == 0);
1205 ASSERT(vd != NULL);
1206 newvdevs[i] = vd;
1207
1208 /*
1209 * Commit this vdev as an l2cache device,
1210 * even if it fails to open.
1211 */
1212 spa_l2cache_add(vd);
1213
1214 vd->vdev_top = vd;
1215 vd->vdev_aux = sav;
1216
1217 spa_l2cache_activate(vd);
1218
1219 if (vdev_open(vd) != 0)
1220 continue;
1221
1222 (void) vdev_validate_aux(vd);
1223
1224 if (!vdev_is_dead(vd))
1225 l2arc_add_vdev(spa, vd);
1226 }
1227 }
1228
1229 /*
1230 * Purge vdevs that were dropped
1231 */
1232 for (i = 0; i < oldnvdevs; i++) {
1233 uint64_t pool;
1234
1235 vd = oldvdevs[i];
1236 if (vd != NULL) {
1237 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1238 pool != 0ULL && l2arc_vdev_present(vd))
1239 l2arc_remove_vdev(vd);
1240 (void) vdev_close(vd);
1241 spa_l2cache_remove(vd);
1242 }
1243 }
1244
1245 if (oldvdevs)
1246 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1247
1248 if (sav->sav_config == NULL)
1249 goto out;
1250
1251 sav->sav_vdevs = newvdevs;
1252 sav->sav_count = (int)nl2cache;
1253
1254 /*
1255 * Recompute the stashed list of l2cache devices, with status
1256 * information this time.
1257 */
1258 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1259 DATA_TYPE_NVLIST_ARRAY) == 0);
1260
1261 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1262 for (i = 0; i < sav->sav_count; i++)
1263 l2cache[i] = vdev_config_generate(spa,
1264 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1265 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1266 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1267out:
1268 for (i = 0; i < sav->sav_count; i++)
1269 nvlist_free(l2cache[i]);
1270 if (sav->sav_count)
1271 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1272}
1273
1274static int
1275load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1276{
1277 dmu_buf_t *db;
1278 char *packed = NULL;
1279 size_t nvsize = 0;
1280 int error;
1281 *value = NULL;
1282
1283 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1284 nvsize = *(uint64_t *)db->db_data;
1285 dmu_buf_rele(db, FTAG);
1286
1287 packed = kmem_alloc(nvsize, KM_SLEEP);
1288 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1289 DMU_READ_PREFETCH);
1290 if (error == 0)
1291 error = nvlist_unpack(packed, nvsize, value, 0);
1292 kmem_free(packed, nvsize);
1293
1294 return (error);
1295}
1296
1297/*
1298 * Checks to see if the given vdev could not be opened, in which case we post a
1299 * sysevent to notify the autoreplace code that the device has been removed.
1300 */
1301static void
1302spa_check_removed(vdev_t *vd)
1303{
1304 for (int c = 0; c < vd->vdev_children; c++)
1305 spa_check_removed(vd->vdev_child[c]);
1306
1307 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1308 zfs_post_autoreplace(vd->vdev_spa, vd);
1309 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1310 }
1311}
1312
1313/*
1314 * Validate the current config against the MOS config
1315 */
1316static boolean_t
1317spa_config_valid(spa_t *spa, nvlist_t *config)
1318{
1319 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1320 nvlist_t *nv;
1321
1322 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1323
1324 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1325 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1326
1327 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1328
1329 /*
1330 * If we're doing a normal import, then build up any additional
1331 * diagnostic information about missing devices in this config.
1332 * We'll pass this up to the user for further processing.
1333 */
1334 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1335 nvlist_t **child, *nv;
1336 uint64_t idx = 0;
1337
1338 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1339 KM_SLEEP);
1340 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1341
1342 for (int c = 0; c < rvd->vdev_children; c++) {
1343 vdev_t *tvd = rvd->vdev_child[c];
1344 vdev_t *mtvd = mrvd->vdev_child[c];
1345
1346 if (tvd->vdev_ops == &vdev_missing_ops &&
1347 mtvd->vdev_ops != &vdev_missing_ops &&
1348 mtvd->vdev_islog)
1349 child[idx++] = vdev_config_generate(spa, mtvd,
1350 B_FALSE, 0);
1351 }
1352
1353 if (idx) {
1354 VERIFY(nvlist_add_nvlist_array(nv,
1355 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1356 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1357 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1358
1359 for (int i = 0; i < idx; i++)
1360 nvlist_free(child[i]);
1361 }
1362 nvlist_free(nv);
1363 kmem_free(child, rvd->vdev_children * sizeof (char **));
1364 }
1365
1366 /*
1367 * Compare the root vdev tree with the information we have
1368 * from the MOS config (mrvd). Check each top-level vdev
1369 * with the corresponding MOS config top-level (mtvd).
1370 */
1371 for (int c = 0; c < rvd->vdev_children; c++) {
1372 vdev_t *tvd = rvd->vdev_child[c];
1373 vdev_t *mtvd = mrvd->vdev_child[c];
1374
1375 /*
1376 * Resolve any "missing" vdevs in the current configuration.
1377 * If we find that the MOS config has more accurate information
1378 * about the top-level vdev then use that vdev instead.
1379 */
1380 if (tvd->vdev_ops == &vdev_missing_ops &&
1381 mtvd->vdev_ops != &vdev_missing_ops) {
1382
1383 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1384 continue;
1385
1386 /*
1387 * Device specific actions.
1388 */
1389 if (mtvd->vdev_islog) {
1390 spa_set_log_state(spa, SPA_LOG_CLEAR);
1391 } else {
1392 /*
1393 * XXX - once we have 'readonly' pool
1394 * support we should be able to handle
1395 * missing data devices by transitioning
1396 * the pool to readonly.
1397 */
1398 continue;
1399 }
1400
1401 /*
1402 * Swap the missing vdev with the data we were
1403 * able to obtain from the MOS config.
1404 */
1405 vdev_remove_child(rvd, tvd);
1406 vdev_remove_child(mrvd, mtvd);
1407
1408 vdev_add_child(rvd, mtvd);
1409 vdev_add_child(mrvd, tvd);
1410
1411 spa_config_exit(spa, SCL_ALL, FTAG);
1412 vdev_load(mtvd);
1413 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1414
1415 vdev_reopen(rvd);
1416 } else if (mtvd->vdev_islog) {
1417 /*
1418 * Load the slog device's state from the MOS config
1419 * since it's possible that the label does not
1420 * contain the most up-to-date information.
1421 */
1422 vdev_load_log_state(tvd, mtvd);
1423 vdev_reopen(tvd);
1424 }
1425 }
1426 vdev_free(mrvd);
1427 spa_config_exit(spa, SCL_ALL, FTAG);
1428
1429 /*
1430 * Ensure we were able to validate the config.
1431 */
1432 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1433}
1434
1435/*
1436 * Check for missing log devices
1437 */
1438static int
1439spa_check_logs(spa_t *spa)
1440{
1441 switch (spa->spa_log_state) {
1442 case SPA_LOG_MISSING:
1443 /* need to recheck in case slog has been restored */
1444 case SPA_LOG_UNKNOWN:
1445 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1446 DS_FIND_CHILDREN)) {
1447 spa_set_log_state(spa, SPA_LOG_MISSING);
1448 return (1);
1449 }
1450 break;
1451 }
1452 return (0);
1453}
1454
1455static boolean_t
1456spa_passivate_log(spa_t *spa)
1457{
1458 vdev_t *rvd = spa->spa_root_vdev;
1459 boolean_t slog_found = B_FALSE;
1460
1461 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1462
1463 if (!spa_has_slogs(spa))
1464 return (B_FALSE);
1465
1466 for (int c = 0; c < rvd->vdev_children; c++) {
1467 vdev_t *tvd = rvd->vdev_child[c];
1468 metaslab_group_t *mg = tvd->vdev_mg;
1469
1470 if (tvd->vdev_islog) {
1471 metaslab_group_passivate(mg);
1472 slog_found = B_TRUE;
1473 }
1474 }
1475
1476 return (slog_found);
1477}
1478
1479static void
1480spa_activate_log(spa_t *spa)
1481{
1482 vdev_t *rvd = spa->spa_root_vdev;
1483
1484 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1485
1486 for (int c = 0; c < rvd->vdev_children; c++) {
1487 vdev_t *tvd = rvd->vdev_child[c];
1488 metaslab_group_t *mg = tvd->vdev_mg;
1489
1490 if (tvd->vdev_islog)
1491 metaslab_group_activate(mg);
1492 }
1493}
1494
1495int
1496spa_offline_log(spa_t *spa)
1497{
1498 int error = 0;
1499
1500 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1501 NULL, DS_FIND_CHILDREN)) == 0) {
1502
1503 /*
1504 * We successfully offlined the log device, sync out the
1505 * current txg so that the "stubby" block can be removed
1506 * by zil_sync().
1507 */
1508 txg_wait_synced(spa->spa_dsl_pool, 0);
1509 }
1510 return (error);
1511}
1512
1513static void
1514spa_aux_check_removed(spa_aux_vdev_t *sav)
1515{
1516 int i;
1517
1518 for (i = 0; i < sav->sav_count; i++)
1519 spa_check_removed(sav->sav_vdevs[i]);
1520}
1521
1522void
1523spa_claim_notify(zio_t *zio)
1524{
1525 spa_t *spa = zio->io_spa;
1526
1527 if (zio->io_error)
1528 return;
1529
1530 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1531 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1532 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1533 mutex_exit(&spa->spa_props_lock);
1534}
1535
1536typedef struct spa_load_error {
1537 uint64_t sle_meta_count;
1538 uint64_t sle_data_count;
1539} spa_load_error_t;
1540
1541static void
1542spa_load_verify_done(zio_t *zio)
1543{
1544 blkptr_t *bp = zio->io_bp;
1545 spa_load_error_t *sle = zio->io_private;
1546 dmu_object_type_t type = BP_GET_TYPE(bp);
1547 int error = zio->io_error;
1548
1549 if (error) {
1550 if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1551 type != DMU_OT_INTENT_LOG)
1552 atomic_add_64(&sle->sle_meta_count, 1);
1553 else
1554 atomic_add_64(&sle->sle_data_count, 1);
1555 }
1556 zio_data_buf_free(zio->io_data, zio->io_size);
1557}
1558
1559/*ARGSUSED*/
1560static int
1561spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1562 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1563{
1564 if (bp != NULL) {
1565 zio_t *rio = arg;
1566 size_t size = BP_GET_PSIZE(bp);
1567 void *data = zio_data_buf_alloc(size);
1568
1569 zio_nowait(zio_read(rio, spa, bp, data, size,
1570 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1571 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1572 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1573 }
1574 return (0);
1575}
1576
1577static int
1578spa_load_verify(spa_t *spa)
1579{
1580 zio_t *rio;
1581 spa_load_error_t sle = { 0 };
1582 zpool_rewind_policy_t policy;
1583 boolean_t verify_ok = B_FALSE;
1584 int error;
1585
1586 zpool_get_rewind_policy(spa->spa_config, &policy);
1587
1588 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1589 return (0);
1590
1591 rio = zio_root(spa, NULL, &sle,
1592 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1593
1594 error = traverse_pool(spa, spa->spa_verify_min_txg,
1595 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1596
1597 (void) zio_wait(rio);
1598
1599 spa->spa_load_meta_errors = sle.sle_meta_count;
1600 spa->spa_load_data_errors = sle.sle_data_count;
1601
1602 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1603 sle.sle_data_count <= policy.zrp_maxdata) {
1604 int64_t loss = 0;
1605
1606 verify_ok = B_TRUE;
1607 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1608 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1609
1610 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1611 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1612 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1613 VERIFY(nvlist_add_int64(spa->spa_load_info,
1614 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1615 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1616 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1617 } else {
1618 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1619 }
1620
1621 if (error) {
1622 if (error != ENXIO && error != EIO)
1623 error = EIO;
1624 return (error);
1625 }
1626
1627 return (verify_ok ? 0 : EIO);
1628}
1629
1630/*
1631 * Find a value in the pool props object.
1632 */
1633static void
1634spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1635{
1636 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1637 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1638}
1639
1640/*
1641 * Find a value in the pool directory object.
1642 */
1643static int
1644spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1645{
1646 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1647 name, sizeof (uint64_t), 1, val));
1648}
1649
1650static int
1651spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1652{
1653 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1654 return (err);
1655}
1656
1657/*
1658 * Fix up config after a partly-completed split. This is done with the
1659 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1660 * pool have that entry in their config, but only the splitting one contains
1661 * a list of all the guids of the vdevs that are being split off.
1662 *
1663 * This function determines what to do with that list: either rejoin
1664 * all the disks to the pool, or complete the splitting process. To attempt
1665 * the rejoin, each disk that is offlined is marked online again, and
1666 * we do a reopen() call. If the vdev label for every disk that was
1667 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1668 * then we call vdev_split() on each disk, and complete the split.
1669 *
1670 * Otherwise we leave the config alone, with all the vdevs in place in
1671 * the original pool.
1672 */
1673static void
1674spa_try_repair(spa_t *spa, nvlist_t *config)
1675{
1676 uint_t extracted;
1677 uint64_t *glist;
1678 uint_t i, gcount;
1679 nvlist_t *nvl;
1680 vdev_t **vd;
1681 boolean_t attempt_reopen;
1682
1683 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1684 return;
1685
1686 /* check that the config is complete */
1687 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1688 &glist, &gcount) != 0)
1689 return;
1690
1691 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1692
1693 /* attempt to online all the vdevs & validate */
1694 attempt_reopen = B_TRUE;
1695 for (i = 0; i < gcount; i++) {
1696 if (glist[i] == 0) /* vdev is hole */
1697 continue;
1698
1699 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1700 if (vd[i] == NULL) {
1701 /*
1702 * Don't bother attempting to reopen the disks;
1703 * just do the split.
1704 */
1705 attempt_reopen = B_FALSE;
1706 } else {
1707 /* attempt to re-online it */
1708 vd[i]->vdev_offline = B_FALSE;
1709 }
1710 }
1711
1712 if (attempt_reopen) {
1713 vdev_reopen(spa->spa_root_vdev);
1714
1715 /* check each device to see what state it's in */
1716 for (extracted = 0, i = 0; i < gcount; i++) {
1717 if (vd[i] != NULL &&
1718 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1719 break;
1720 ++extracted;
1721 }
1722 }
1723
1724 /*
1725 * If every disk has been moved to the new pool, or if we never
1726 * even attempted to look at them, then we split them off for
1727 * good.
1728 */
1729 if (!attempt_reopen || gcount == extracted) {
1730 for (i = 0; i < gcount; i++)
1731 if (vd[i] != NULL)
1732 vdev_split(vd[i]);
1733 vdev_reopen(spa->spa_root_vdev);
1734 }
1735
1736 kmem_free(vd, gcount * sizeof (vdev_t *));
1737}
1738
1739static int
1740spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1741 boolean_t mosconfig)
1742{
1743 nvlist_t *config = spa->spa_config;
1744 char *ereport = FM_EREPORT_ZFS_POOL;
1096 spa_config_exit(spa, SCL_ALL, FTAG);
1097}
1098
1099/*
1100 * Load (or re-load) the current list of vdevs describing the active spares for
1101 * this pool. When this is called, we have some form of basic information in
1102 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1103 * then re-generate a more complete list including status information.
1104 */
1105static void
1106spa_load_spares(spa_t *spa)
1107{
1108 nvlist_t **spares;
1109 uint_t nspares;
1110 int i;
1111 vdev_t *vd, *tvd;
1112
1113 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1114
1115 /*
1116 * First, close and free any existing spare vdevs.
1117 */
1118 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1119 vd = spa->spa_spares.sav_vdevs[i];
1120
1121 /* Undo the call to spa_activate() below */
1122 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1123 B_FALSE)) != NULL && tvd->vdev_isspare)
1124 spa_spare_remove(tvd);
1125 vdev_close(vd);
1126 vdev_free(vd);
1127 }
1128
1129 if (spa->spa_spares.sav_vdevs)
1130 kmem_free(spa->spa_spares.sav_vdevs,
1131 spa->spa_spares.sav_count * sizeof (void *));
1132
1133 if (spa->spa_spares.sav_config == NULL)
1134 nspares = 0;
1135 else
1136 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1137 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1138
1139 spa->spa_spares.sav_count = (int)nspares;
1140 spa->spa_spares.sav_vdevs = NULL;
1141
1142 if (nspares == 0)
1143 return;
1144
1145 /*
1146 * Construct the array of vdevs, opening them to get status in the
1147 * process. For each spare, there is potentially two different vdev_t
1148 * structures associated with it: one in the list of spares (used only
1149 * for basic validation purposes) and one in the active vdev
1150 * configuration (if it's spared in). During this phase we open and
1151 * validate each vdev on the spare list. If the vdev also exists in the
1152 * active configuration, then we also mark this vdev as an active spare.
1153 */
1154 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1155 KM_SLEEP);
1156 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1157 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1158 VDEV_ALLOC_SPARE) == 0);
1159 ASSERT(vd != NULL);
1160
1161 spa->spa_spares.sav_vdevs[i] = vd;
1162
1163 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1164 B_FALSE)) != NULL) {
1165 if (!tvd->vdev_isspare)
1166 spa_spare_add(tvd);
1167
1168 /*
1169 * We only mark the spare active if we were successfully
1170 * able to load the vdev. Otherwise, importing a pool
1171 * with a bad active spare would result in strange
1172 * behavior, because multiple pool would think the spare
1173 * is actively in use.
1174 *
1175 * There is a vulnerability here to an equally bizarre
1176 * circumstance, where a dead active spare is later
1177 * brought back to life (onlined or otherwise). Given
1178 * the rarity of this scenario, and the extra complexity
1179 * it adds, we ignore the possibility.
1180 */
1181 if (!vdev_is_dead(tvd))
1182 spa_spare_activate(tvd);
1183 }
1184
1185 vd->vdev_top = vd;
1186 vd->vdev_aux = &spa->spa_spares;
1187
1188 if (vdev_open(vd) != 0)
1189 continue;
1190
1191 if (vdev_validate_aux(vd) == 0)
1192 spa_spare_add(vd);
1193 }
1194
1195 /*
1196 * Recompute the stashed list of spares, with status information
1197 * this time.
1198 */
1199 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1200 DATA_TYPE_NVLIST_ARRAY) == 0);
1201
1202 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1203 KM_SLEEP);
1204 for (i = 0; i < spa->spa_spares.sav_count; i++)
1205 spares[i] = vdev_config_generate(spa,
1206 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1207 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1208 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1209 for (i = 0; i < spa->spa_spares.sav_count; i++)
1210 nvlist_free(spares[i]);
1211 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1212}
1213
1214/*
1215 * Load (or re-load) the current list of vdevs describing the active l2cache for
1216 * this pool. When this is called, we have some form of basic information in
1217 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1218 * then re-generate a more complete list including status information.
1219 * Devices which are already active have their details maintained, and are
1220 * not re-opened.
1221 */
1222static void
1223spa_load_l2cache(spa_t *spa)
1224{
1225 nvlist_t **l2cache;
1226 uint_t nl2cache;
1227 int i, j, oldnvdevs;
1228 uint64_t guid;
1229 vdev_t *vd, **oldvdevs, **newvdevs;
1230 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1231
1232 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1233
1234 if (sav->sav_config != NULL) {
1235 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1236 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1237 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1238 } else {
1239 nl2cache = 0;
1240 }
1241
1242 oldvdevs = sav->sav_vdevs;
1243 oldnvdevs = sav->sav_count;
1244 sav->sav_vdevs = NULL;
1245 sav->sav_count = 0;
1246
1247 /*
1248 * Process new nvlist of vdevs.
1249 */
1250 for (i = 0; i < nl2cache; i++) {
1251 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1252 &guid) == 0);
1253
1254 newvdevs[i] = NULL;
1255 for (j = 0; j < oldnvdevs; j++) {
1256 vd = oldvdevs[j];
1257 if (vd != NULL && guid == vd->vdev_guid) {
1258 /*
1259 * Retain previous vdev for add/remove ops.
1260 */
1261 newvdevs[i] = vd;
1262 oldvdevs[j] = NULL;
1263 break;
1264 }
1265 }
1266
1267 if (newvdevs[i] == NULL) {
1268 /*
1269 * Create new vdev
1270 */
1271 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1272 VDEV_ALLOC_L2CACHE) == 0);
1273 ASSERT(vd != NULL);
1274 newvdevs[i] = vd;
1275
1276 /*
1277 * Commit this vdev as an l2cache device,
1278 * even if it fails to open.
1279 */
1280 spa_l2cache_add(vd);
1281
1282 vd->vdev_top = vd;
1283 vd->vdev_aux = sav;
1284
1285 spa_l2cache_activate(vd);
1286
1287 if (vdev_open(vd) != 0)
1288 continue;
1289
1290 (void) vdev_validate_aux(vd);
1291
1292 if (!vdev_is_dead(vd))
1293 l2arc_add_vdev(spa, vd);
1294 }
1295 }
1296
1297 /*
1298 * Purge vdevs that were dropped
1299 */
1300 for (i = 0; i < oldnvdevs; i++) {
1301 uint64_t pool;
1302
1303 vd = oldvdevs[i];
1304 if (vd != NULL) {
1305 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1306 pool != 0ULL && l2arc_vdev_present(vd))
1307 l2arc_remove_vdev(vd);
1308 (void) vdev_close(vd);
1309 spa_l2cache_remove(vd);
1310 }
1311 }
1312
1313 if (oldvdevs)
1314 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1315
1316 if (sav->sav_config == NULL)
1317 goto out;
1318
1319 sav->sav_vdevs = newvdevs;
1320 sav->sav_count = (int)nl2cache;
1321
1322 /*
1323 * Recompute the stashed list of l2cache devices, with status
1324 * information this time.
1325 */
1326 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1327 DATA_TYPE_NVLIST_ARRAY) == 0);
1328
1329 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1330 for (i = 0; i < sav->sav_count; i++)
1331 l2cache[i] = vdev_config_generate(spa,
1332 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1333 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1334 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1335out:
1336 for (i = 0; i < sav->sav_count; i++)
1337 nvlist_free(l2cache[i]);
1338 if (sav->sav_count)
1339 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1340}
1341
1342static int
1343load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1344{
1345 dmu_buf_t *db;
1346 char *packed = NULL;
1347 size_t nvsize = 0;
1348 int error;
1349 *value = NULL;
1350
1351 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1352 nvsize = *(uint64_t *)db->db_data;
1353 dmu_buf_rele(db, FTAG);
1354
1355 packed = kmem_alloc(nvsize, KM_SLEEP);
1356 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1357 DMU_READ_PREFETCH);
1358 if (error == 0)
1359 error = nvlist_unpack(packed, nvsize, value, 0);
1360 kmem_free(packed, nvsize);
1361
1362 return (error);
1363}
1364
1365/*
1366 * Checks to see if the given vdev could not be opened, in which case we post a
1367 * sysevent to notify the autoreplace code that the device has been removed.
1368 */
1369static void
1370spa_check_removed(vdev_t *vd)
1371{
1372 for (int c = 0; c < vd->vdev_children; c++)
1373 spa_check_removed(vd->vdev_child[c]);
1374
1375 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1376 zfs_post_autoreplace(vd->vdev_spa, vd);
1377 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1378 }
1379}
1380
1381/*
1382 * Validate the current config against the MOS config
1383 */
1384static boolean_t
1385spa_config_valid(spa_t *spa, nvlist_t *config)
1386{
1387 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1388 nvlist_t *nv;
1389
1390 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1391
1392 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1393 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1394
1395 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1396
1397 /*
1398 * If we're doing a normal import, then build up any additional
1399 * diagnostic information about missing devices in this config.
1400 * We'll pass this up to the user for further processing.
1401 */
1402 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1403 nvlist_t **child, *nv;
1404 uint64_t idx = 0;
1405
1406 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1407 KM_SLEEP);
1408 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1409
1410 for (int c = 0; c < rvd->vdev_children; c++) {
1411 vdev_t *tvd = rvd->vdev_child[c];
1412 vdev_t *mtvd = mrvd->vdev_child[c];
1413
1414 if (tvd->vdev_ops == &vdev_missing_ops &&
1415 mtvd->vdev_ops != &vdev_missing_ops &&
1416 mtvd->vdev_islog)
1417 child[idx++] = vdev_config_generate(spa, mtvd,
1418 B_FALSE, 0);
1419 }
1420
1421 if (idx) {
1422 VERIFY(nvlist_add_nvlist_array(nv,
1423 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1424 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1425 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1426
1427 for (int i = 0; i < idx; i++)
1428 nvlist_free(child[i]);
1429 }
1430 nvlist_free(nv);
1431 kmem_free(child, rvd->vdev_children * sizeof (char **));
1432 }
1433
1434 /*
1435 * Compare the root vdev tree with the information we have
1436 * from the MOS config (mrvd). Check each top-level vdev
1437 * with the corresponding MOS config top-level (mtvd).
1438 */
1439 for (int c = 0; c < rvd->vdev_children; c++) {
1440 vdev_t *tvd = rvd->vdev_child[c];
1441 vdev_t *mtvd = mrvd->vdev_child[c];
1442
1443 /*
1444 * Resolve any "missing" vdevs in the current configuration.
1445 * If we find that the MOS config has more accurate information
1446 * about the top-level vdev then use that vdev instead.
1447 */
1448 if (tvd->vdev_ops == &vdev_missing_ops &&
1449 mtvd->vdev_ops != &vdev_missing_ops) {
1450
1451 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1452 continue;
1453
1454 /*
1455 * Device specific actions.
1456 */
1457 if (mtvd->vdev_islog) {
1458 spa_set_log_state(spa, SPA_LOG_CLEAR);
1459 } else {
1460 /*
1461 * XXX - once we have 'readonly' pool
1462 * support we should be able to handle
1463 * missing data devices by transitioning
1464 * the pool to readonly.
1465 */
1466 continue;
1467 }
1468
1469 /*
1470 * Swap the missing vdev with the data we were
1471 * able to obtain from the MOS config.
1472 */
1473 vdev_remove_child(rvd, tvd);
1474 vdev_remove_child(mrvd, mtvd);
1475
1476 vdev_add_child(rvd, mtvd);
1477 vdev_add_child(mrvd, tvd);
1478
1479 spa_config_exit(spa, SCL_ALL, FTAG);
1480 vdev_load(mtvd);
1481 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1482
1483 vdev_reopen(rvd);
1484 } else if (mtvd->vdev_islog) {
1485 /*
1486 * Load the slog device's state from the MOS config
1487 * since it's possible that the label does not
1488 * contain the most up-to-date information.
1489 */
1490 vdev_load_log_state(tvd, mtvd);
1491 vdev_reopen(tvd);
1492 }
1493 }
1494 vdev_free(mrvd);
1495 spa_config_exit(spa, SCL_ALL, FTAG);
1496
1497 /*
1498 * Ensure we were able to validate the config.
1499 */
1500 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1501}
1502
1503/*
1504 * Check for missing log devices
1505 */
1506static int
1507spa_check_logs(spa_t *spa)
1508{
1509 switch (spa->spa_log_state) {
1510 case SPA_LOG_MISSING:
1511 /* need to recheck in case slog has been restored */
1512 case SPA_LOG_UNKNOWN:
1513 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1514 DS_FIND_CHILDREN)) {
1515 spa_set_log_state(spa, SPA_LOG_MISSING);
1516 return (1);
1517 }
1518 break;
1519 }
1520 return (0);
1521}
1522
1523static boolean_t
1524spa_passivate_log(spa_t *spa)
1525{
1526 vdev_t *rvd = spa->spa_root_vdev;
1527 boolean_t slog_found = B_FALSE;
1528
1529 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1530
1531 if (!spa_has_slogs(spa))
1532 return (B_FALSE);
1533
1534 for (int c = 0; c < rvd->vdev_children; c++) {
1535 vdev_t *tvd = rvd->vdev_child[c];
1536 metaslab_group_t *mg = tvd->vdev_mg;
1537
1538 if (tvd->vdev_islog) {
1539 metaslab_group_passivate(mg);
1540 slog_found = B_TRUE;
1541 }
1542 }
1543
1544 return (slog_found);
1545}
1546
1547static void
1548spa_activate_log(spa_t *spa)
1549{
1550 vdev_t *rvd = spa->spa_root_vdev;
1551
1552 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1553
1554 for (int c = 0; c < rvd->vdev_children; c++) {
1555 vdev_t *tvd = rvd->vdev_child[c];
1556 metaslab_group_t *mg = tvd->vdev_mg;
1557
1558 if (tvd->vdev_islog)
1559 metaslab_group_activate(mg);
1560 }
1561}
1562
1563int
1564spa_offline_log(spa_t *spa)
1565{
1566 int error = 0;
1567
1568 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1569 NULL, DS_FIND_CHILDREN)) == 0) {
1570
1571 /*
1572 * We successfully offlined the log device, sync out the
1573 * current txg so that the "stubby" block can be removed
1574 * by zil_sync().
1575 */
1576 txg_wait_synced(spa->spa_dsl_pool, 0);
1577 }
1578 return (error);
1579}
1580
1581static void
1582spa_aux_check_removed(spa_aux_vdev_t *sav)
1583{
1584 int i;
1585
1586 for (i = 0; i < sav->sav_count; i++)
1587 spa_check_removed(sav->sav_vdevs[i]);
1588}
1589
1590void
1591spa_claim_notify(zio_t *zio)
1592{
1593 spa_t *spa = zio->io_spa;
1594
1595 if (zio->io_error)
1596 return;
1597
1598 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1599 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1600 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1601 mutex_exit(&spa->spa_props_lock);
1602}
1603
1604typedef struct spa_load_error {
1605 uint64_t sle_meta_count;
1606 uint64_t sle_data_count;
1607} spa_load_error_t;
1608
1609static void
1610spa_load_verify_done(zio_t *zio)
1611{
1612 blkptr_t *bp = zio->io_bp;
1613 spa_load_error_t *sle = zio->io_private;
1614 dmu_object_type_t type = BP_GET_TYPE(bp);
1615 int error = zio->io_error;
1616
1617 if (error) {
1618 if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1619 type != DMU_OT_INTENT_LOG)
1620 atomic_add_64(&sle->sle_meta_count, 1);
1621 else
1622 atomic_add_64(&sle->sle_data_count, 1);
1623 }
1624 zio_data_buf_free(zio->io_data, zio->io_size);
1625}
1626
1627/*ARGSUSED*/
1628static int
1629spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1630 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1631{
1632 if (bp != NULL) {
1633 zio_t *rio = arg;
1634 size_t size = BP_GET_PSIZE(bp);
1635 void *data = zio_data_buf_alloc(size);
1636
1637 zio_nowait(zio_read(rio, spa, bp, data, size,
1638 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1639 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1640 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1641 }
1642 return (0);
1643}
1644
1645static int
1646spa_load_verify(spa_t *spa)
1647{
1648 zio_t *rio;
1649 spa_load_error_t sle = { 0 };
1650 zpool_rewind_policy_t policy;
1651 boolean_t verify_ok = B_FALSE;
1652 int error;
1653
1654 zpool_get_rewind_policy(spa->spa_config, &policy);
1655
1656 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1657 return (0);
1658
1659 rio = zio_root(spa, NULL, &sle,
1660 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1661
1662 error = traverse_pool(spa, spa->spa_verify_min_txg,
1663 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1664
1665 (void) zio_wait(rio);
1666
1667 spa->spa_load_meta_errors = sle.sle_meta_count;
1668 spa->spa_load_data_errors = sle.sle_data_count;
1669
1670 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1671 sle.sle_data_count <= policy.zrp_maxdata) {
1672 int64_t loss = 0;
1673
1674 verify_ok = B_TRUE;
1675 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1676 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1677
1678 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1679 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1680 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1681 VERIFY(nvlist_add_int64(spa->spa_load_info,
1682 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1683 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1684 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1685 } else {
1686 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1687 }
1688
1689 if (error) {
1690 if (error != ENXIO && error != EIO)
1691 error = EIO;
1692 return (error);
1693 }
1694
1695 return (verify_ok ? 0 : EIO);
1696}
1697
1698/*
1699 * Find a value in the pool props object.
1700 */
1701static void
1702spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1703{
1704 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1705 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1706}
1707
1708/*
1709 * Find a value in the pool directory object.
1710 */
1711static int
1712spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1713{
1714 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1715 name, sizeof (uint64_t), 1, val));
1716}
1717
1718static int
1719spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1720{
1721 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1722 return (err);
1723}
1724
1725/*
1726 * Fix up config after a partly-completed split. This is done with the
1727 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1728 * pool have that entry in their config, but only the splitting one contains
1729 * a list of all the guids of the vdevs that are being split off.
1730 *
1731 * This function determines what to do with that list: either rejoin
1732 * all the disks to the pool, or complete the splitting process. To attempt
1733 * the rejoin, each disk that is offlined is marked online again, and
1734 * we do a reopen() call. If the vdev label for every disk that was
1735 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1736 * then we call vdev_split() on each disk, and complete the split.
1737 *
1738 * Otherwise we leave the config alone, with all the vdevs in place in
1739 * the original pool.
1740 */
1741static void
1742spa_try_repair(spa_t *spa, nvlist_t *config)
1743{
1744 uint_t extracted;
1745 uint64_t *glist;
1746 uint_t i, gcount;
1747 nvlist_t *nvl;
1748 vdev_t **vd;
1749 boolean_t attempt_reopen;
1750
1751 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1752 return;
1753
1754 /* check that the config is complete */
1755 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1756 &glist, &gcount) != 0)
1757 return;
1758
1759 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1760
1761 /* attempt to online all the vdevs & validate */
1762 attempt_reopen = B_TRUE;
1763 for (i = 0; i < gcount; i++) {
1764 if (glist[i] == 0) /* vdev is hole */
1765 continue;
1766
1767 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1768 if (vd[i] == NULL) {
1769 /*
1770 * Don't bother attempting to reopen the disks;
1771 * just do the split.
1772 */
1773 attempt_reopen = B_FALSE;
1774 } else {
1775 /* attempt to re-online it */
1776 vd[i]->vdev_offline = B_FALSE;
1777 }
1778 }
1779
1780 if (attempt_reopen) {
1781 vdev_reopen(spa->spa_root_vdev);
1782
1783 /* check each device to see what state it's in */
1784 for (extracted = 0, i = 0; i < gcount; i++) {
1785 if (vd[i] != NULL &&
1786 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1787 break;
1788 ++extracted;
1789 }
1790 }
1791
1792 /*
1793 * If every disk has been moved to the new pool, or if we never
1794 * even attempted to look at them, then we split them off for
1795 * good.
1796 */
1797 if (!attempt_reopen || gcount == extracted) {
1798 for (i = 0; i < gcount; i++)
1799 if (vd[i] != NULL)
1800 vdev_split(vd[i]);
1801 vdev_reopen(spa->spa_root_vdev);
1802 }
1803
1804 kmem_free(vd, gcount * sizeof (vdev_t *));
1805}
1806
1807static int
1808spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1809 boolean_t mosconfig)
1810{
1811 nvlist_t *config = spa->spa_config;
1812 char *ereport = FM_EREPORT_ZFS_POOL;
1813 char *comment;
1745 int error;
1746 uint64_t pool_guid;
1747 nvlist_t *nvl;
1748
1749 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1750 return (EINVAL);
1751
1814 int error;
1815 uint64_t pool_guid;
1816 nvlist_t *nvl;
1817
1818 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1819 return (EINVAL);
1820
1821 ASSERT(spa->spa_comment == NULL);
1822 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1823 spa->spa_comment = spa_strdup(comment);
1824
1752 /*
1753 * Versioning wasn't explicitly added to the label until later, so if
1754 * it's not present treat it as the initial version.
1755 */
1756 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1757 &spa->spa_ubsync.ub_version) != 0)
1758 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1759
1760 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1761 &spa->spa_config_txg);
1762
1763 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1764 spa_guid_exists(pool_guid, 0)) {
1765 error = EEXIST;
1766 } else {
1825 /*
1826 * Versioning wasn't explicitly added to the label until later, so if
1827 * it's not present treat it as the initial version.
1828 */
1829 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1830 &spa->spa_ubsync.ub_version) != 0)
1831 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1832
1833 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1834 &spa->spa_config_txg);
1835
1836 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1837 spa_guid_exists(pool_guid, 0)) {
1838 error = EEXIST;
1839 } else {
1767 spa->spa_load_guid = pool_guid;
1840 spa->spa_config_guid = pool_guid;
1768
1769 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1770 &nvl) == 0) {
1771 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1772 KM_SLEEP) == 0);
1773 }
1774
1775 gethrestime(&spa->spa_loaded_ts);
1776 error = spa_load_impl(spa, pool_guid, config, state, type,
1777 mosconfig, &ereport);
1778 }
1779
1780 spa->spa_minref = refcount_count(&spa->spa_refcount);
1781 if (error) {
1782 if (error != EEXIST) {
1783 spa->spa_loaded_ts.tv_sec = 0;
1784 spa->spa_loaded_ts.tv_nsec = 0;
1785 }
1786 if (error != EBADF) {
1787 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1788 }
1789 }
1790 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1791 spa->spa_ena = 0;
1792
1793 return (error);
1794}
1795
1796/*
1797 * Load an existing storage pool, using the pool's builtin spa_config as a
1798 * source of configuration information.
1799 */
1800static int
1801spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
1802 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1803 char **ereport)
1804{
1805 int error = 0;
1806 nvlist_t *nvroot = NULL;
1807 vdev_t *rvd;
1808 uberblock_t *ub = &spa->spa_uberblock;
1809 uint64_t children, config_cache_txg = spa->spa_config_txg;
1810 int orig_mode = spa->spa_mode;
1811 int parse;
1812 uint64_t obj;
1813
1814 /*
1815 * If this is an untrusted config, access the pool in read-only mode.
1816 * This prevents things like resilvering recently removed devices.
1817 */
1818 if (!mosconfig)
1819 spa->spa_mode = FREAD;
1820
1821 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1822
1823 spa->spa_load_state = state;
1824
1825 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
1826 return (EINVAL);
1827
1828 parse = (type == SPA_IMPORT_EXISTING ?
1829 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
1830
1831 /*
1832 * Create "The Godfather" zio to hold all async IOs
1833 */
1834 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1835 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
1836
1837 /*
1838 * Parse the configuration into a vdev tree. We explicitly set the
1839 * value that will be returned by spa_version() since parsing the
1840 * configuration requires knowing the version number.
1841 */
1842 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1843 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
1844 spa_config_exit(spa, SCL_ALL, FTAG);
1845
1846 if (error != 0)
1847 return (error);
1848
1849 ASSERT(spa->spa_root_vdev == rvd);
1850
1851 if (type != SPA_IMPORT_ASSEMBLE) {
1852 ASSERT(spa_guid(spa) == pool_guid);
1853 }
1854
1855 /*
1856 * Try to open all vdevs, loading each label in the process.
1857 */
1858 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1859 error = vdev_open(rvd);
1860 spa_config_exit(spa, SCL_ALL, FTAG);
1861 if (error != 0)
1862 return (error);
1863
1864 /*
1865 * We need to validate the vdev labels against the configuration that
1866 * we have in hand, which is dependent on the setting of mosconfig. If
1867 * mosconfig is true then we're validating the vdev labels based on
1868 * that config. Otherwise, we're validating against the cached config
1869 * (zpool.cache) that was read when we loaded the zfs module, and then
1870 * later we will recursively call spa_load() and validate against
1871 * the vdev config.
1872 *
1873 * If we're assembling a new pool that's been split off from an
1874 * existing pool, the labels haven't yet been updated so we skip
1875 * validation for now.
1876 */
1877 if (type != SPA_IMPORT_ASSEMBLE) {
1878 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1879 error = vdev_validate(rvd);
1880 spa_config_exit(spa, SCL_ALL, FTAG);
1881
1882 if (error != 0)
1883 return (error);
1884
1885 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
1886 return (ENXIO);
1887 }
1888
1889 /*
1890 * Find the best uberblock.
1891 */
1892 vdev_uberblock_load(NULL, rvd, ub);
1893
1894 /*
1895 * If we weren't able to find a single valid uberblock, return failure.
1896 */
1897 if (ub->ub_txg == 0)
1898 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
1899
1900 /*
1901 * If the pool is newer than the code, we can't open it.
1902 */
1903 if (ub->ub_version > SPA_VERSION)
1904 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1905
1906 /*
1907 * If the vdev guid sum doesn't match the uberblock, we have an
1908 * incomplete configuration. We first check to see if the pool
1909 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1910 * If it is, defer the vdev_guid_sum check till later so we
1911 * can handle missing vdevs.
1912 */
1913 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
1914 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
1915 rvd->vdev_guid_sum != ub->ub_guid_sum)
1916 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
1917
1918 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
1919 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1920 spa_try_repair(spa, config);
1921 spa_config_exit(spa, SCL_ALL, FTAG);
1922 nvlist_free(spa->spa_config_splitting);
1923 spa->spa_config_splitting = NULL;
1924 }
1925
1926 /*
1927 * Initialize internal SPA structures.
1928 */
1929 spa->spa_state = POOL_STATE_ACTIVE;
1930 spa->spa_ubsync = spa->spa_uberblock;
1931 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
1932 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
1933 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
1934 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
1935 spa->spa_claim_max_txg = spa->spa_first_txg;
1936 spa->spa_prev_software_version = ub->ub_software_version;
1937
1938 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
1939 if (error)
1940 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1941 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1942
1943 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
1944 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1945
1946 if (!mosconfig) {
1947 uint64_t hostid;
1948 nvlist_t *policy = NULL, *nvconfig;
1949
1950 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
1951 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1952
1953 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
1954 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
1955 char *hostname;
1956 unsigned long myhostid = 0;
1957
1958 VERIFY(nvlist_lookup_string(nvconfig,
1959 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1960
1961#ifdef _KERNEL
1962 myhostid = zone_get_hostid(NULL);
1963#else /* _KERNEL */
1964 /*
1965 * We're emulating the system's hostid in userland, so
1966 * we can't use zone_get_hostid().
1967 */
1968 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
1969#endif /* _KERNEL */
1970 if (check_hostid && hostid != 0 && myhostid != 0 &&
1971 hostid != myhostid) {
1972 nvlist_free(nvconfig);
1973 cmn_err(CE_WARN, "pool '%s' could not be "
1974 "loaded as it was last accessed by "
1975 "another system (host: %s hostid: 0x%lx). "
1976 "See: http://www.sun.com/msg/ZFS-8000-EY",
1977 spa_name(spa), hostname,
1978 (unsigned long)hostid);
1979 return (EBADF);
1980 }
1981 }
1982 if (nvlist_lookup_nvlist(spa->spa_config,
1983 ZPOOL_REWIND_POLICY, &policy) == 0)
1984 VERIFY(nvlist_add_nvlist(nvconfig,
1985 ZPOOL_REWIND_POLICY, policy) == 0);
1986
1987 spa_config_set(spa, nvconfig);
1988 spa_unload(spa);
1989 spa_deactivate(spa);
1990 spa_activate(spa, orig_mode);
1991
1992 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
1993 }
1994
1995 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
1996 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1997 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
1998 if (error != 0)
1999 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2000
2001 /*
2002 * Load the bit that tells us to use the new accounting function
2003 * (raid-z deflation). If we have an older pool, this will not
2004 * be present.
2005 */
2006 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2007 if (error != 0 && error != ENOENT)
2008 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2009
2010 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2011 &spa->spa_creation_version);
2012 if (error != 0 && error != ENOENT)
2013 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2014
2015 /*
2016 * Load the persistent error log. If we have an older pool, this will
2017 * not be present.
2018 */
2019 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2020 if (error != 0 && error != ENOENT)
2021 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2022
2023 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2024 &spa->spa_errlog_scrub);
2025 if (error != 0 && error != ENOENT)
2026 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2027
2028 /*
2029 * Load the history object. If we have an older pool, this
2030 * will not be present.
2031 */
2032 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2033 if (error != 0 && error != ENOENT)
2034 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2035
2036 /*
2037 * If we're assembling the pool from the split-off vdevs of
2038 * an existing pool, we don't want to attach the spares & cache
2039 * devices.
2040 */
2041
2042 /*
2043 * Load any hot spares for this pool.
2044 */
2045 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2046 if (error != 0 && error != ENOENT)
2047 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2048 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2049 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2050 if (load_nvlist(spa, spa->spa_spares.sav_object,
2051 &spa->spa_spares.sav_config) != 0)
2052 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2053
2054 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2055 spa_load_spares(spa);
2056 spa_config_exit(spa, SCL_ALL, FTAG);
2057 } else if (error == 0) {
2058 spa->spa_spares.sav_sync = B_TRUE;
2059 }
2060
2061 /*
2062 * Load any level 2 ARC devices for this pool.
2063 */
2064 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2065 &spa->spa_l2cache.sav_object);
2066 if (error != 0 && error != ENOENT)
2067 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2068 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2069 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2070 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2071 &spa->spa_l2cache.sav_config) != 0)
2072 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2073
2074 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2075 spa_load_l2cache(spa);
2076 spa_config_exit(spa, SCL_ALL, FTAG);
2077 } else if (error == 0) {
2078 spa->spa_l2cache.sav_sync = B_TRUE;
2079 }
2080
2081 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2082
2083 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2084 if (error && error != ENOENT)
2085 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2086
2087 if (error == 0) {
2088 uint64_t autoreplace;
2089
2090 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2091 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2092 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2093 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2094 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2095 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2096 &spa->spa_dedup_ditto);
2097
2098 spa->spa_autoreplace = (autoreplace != 0);
2099 }
2100
2101 /*
2102 * If the 'autoreplace' property is set, then post a resource notifying
2103 * the ZFS DE that it should not issue any faults for unopenable
2104 * devices. We also iterate over the vdevs, and post a sysevent for any
2105 * unopenable vdevs so that the normal autoreplace handler can take
2106 * over.
2107 */
2108 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2109 spa_check_removed(spa->spa_root_vdev);
2110 /*
2111 * For the import case, this is done in spa_import(), because
2112 * at this point we're using the spare definitions from
2113 * the MOS config, not necessarily from the userland config.
2114 */
2115 if (state != SPA_LOAD_IMPORT) {
2116 spa_aux_check_removed(&spa->spa_spares);
2117 spa_aux_check_removed(&spa->spa_l2cache);
2118 }
2119 }
2120
2121 /*
2122 * Load the vdev state for all toplevel vdevs.
2123 */
2124 vdev_load(rvd);
2125
2126 /*
2127 * Propagate the leaf DTLs we just loaded all the way up the tree.
2128 */
2129 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2130 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2131 spa_config_exit(spa, SCL_ALL, FTAG);
2132
2133 /*
2134 * Load the DDTs (dedup tables).
2135 */
2136 error = ddt_load(spa);
2137 if (error != 0)
2138 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2139
2140 spa_update_dspace(spa);
2141
2142 /*
2143 * Validate the config, using the MOS config to fill in any
2144 * information which might be missing. If we fail to validate
2145 * the config then declare the pool unfit for use. If we're
2146 * assembling a pool from a split, the log is not transferred
2147 * over.
2148 */
2149 if (type != SPA_IMPORT_ASSEMBLE) {
2150 nvlist_t *nvconfig;
2151
2152 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2153 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2154
2155 if (!spa_config_valid(spa, nvconfig)) {
2156 nvlist_free(nvconfig);
2157 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2158 ENXIO));
2159 }
2160 nvlist_free(nvconfig);
2161
2162 /*
2163 * Now that we've validate the config, check the state of the
2164 * root vdev. If it can't be opened, it indicates one or
2165 * more toplevel vdevs are faulted.
2166 */
2167 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2168 return (ENXIO);
2169
2170 if (spa_check_logs(spa)) {
2171 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2172 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2173 }
2174 }
2175
2176 /*
2177 * We've successfully opened the pool, verify that we're ready
2178 * to start pushing transactions.
2179 */
2180 if (state != SPA_LOAD_TRYIMPORT) {
2181 if (error = spa_load_verify(spa))
2182 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2183 error));
2184 }
2185
2186 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2187 spa->spa_load_max_txg == UINT64_MAX)) {
2188 dmu_tx_t *tx;
2189 int need_update = B_FALSE;
2190
2191 ASSERT(state != SPA_LOAD_TRYIMPORT);
2192
2193 /*
2194 * Claim log blocks that haven't been committed yet.
2195 * This must all happen in a single txg.
2196 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2197 * invoked from zil_claim_log_block()'s i/o done callback.
2198 * Price of rollback is that we abandon the log.
2199 */
2200 spa->spa_claiming = B_TRUE;
2201
2202 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2203 spa_first_txg(spa));
2204 (void) dmu_objset_find(spa_name(spa),
2205 zil_claim, tx, DS_FIND_CHILDREN);
2206 dmu_tx_commit(tx);
2207
2208 spa->spa_claiming = B_FALSE;
2209
2210 spa_set_log_state(spa, SPA_LOG_GOOD);
2211 spa->spa_sync_on = B_TRUE;
2212 txg_sync_start(spa->spa_dsl_pool);
2213
2214 /*
2215 * Wait for all claims to sync. We sync up to the highest
2216 * claimed log block birth time so that claimed log blocks
2217 * don't appear to be from the future. spa_claim_max_txg
2218 * will have been set for us by either zil_check_log_chain()
2219 * (invoked from spa_check_logs()) or zil_claim() above.
2220 */
2221 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2222
2223 /*
2224 * If the config cache is stale, or we have uninitialized
2225 * metaslabs (see spa_vdev_add()), then update the config.
2226 *
2227 * If this is a verbatim import, trust the current
2228 * in-core spa_config and update the disk labels.
2229 */
2230 if (config_cache_txg != spa->spa_config_txg ||
2231 state == SPA_LOAD_IMPORT ||
2232 state == SPA_LOAD_RECOVER ||
2233 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2234 need_update = B_TRUE;
2235
2236 for (int c = 0; c < rvd->vdev_children; c++)
2237 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2238 need_update = B_TRUE;
2239
2240 /*
2241 * Update the config cache asychronously in case we're the
2242 * root pool, in which case the config cache isn't writable yet.
2243 */
2244 if (need_update)
2245 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2246
2247 /*
2248 * Check all DTLs to see if anything needs resilvering.
2249 */
2250 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2251 vdev_resilver_needed(rvd, NULL, NULL))
2252 spa_async_request(spa, SPA_ASYNC_RESILVER);
2253
2254 /*
2255 * Delete any inconsistent datasets.
2256 */
2257 (void) dmu_objset_find(spa_name(spa),
2258 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2259
2260 /*
2261 * Clean up any stale temporary dataset userrefs.
2262 */
2263 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2264 }
2265
2266 return (0);
2267}
2268
2269static int
2270spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2271{
2272 int mode = spa->spa_mode;
2273
2274 spa_unload(spa);
2275 spa_deactivate(spa);
2276
2277 spa->spa_load_max_txg--;
2278
2279 spa_activate(spa, mode);
2280 spa_async_suspend(spa);
2281
2282 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2283}
2284
2285static int
2286spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2287 uint64_t max_request, int rewind_flags)
2288{
2289 nvlist_t *config = NULL;
2290 int load_error, rewind_error;
2291 uint64_t safe_rewind_txg;
2292 uint64_t min_txg;
2293
2294 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2295 spa->spa_load_max_txg = spa->spa_load_txg;
2296 spa_set_log_state(spa, SPA_LOG_CLEAR);
2297 } else {
2298 spa->spa_load_max_txg = max_request;
2299 }
2300
2301 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2302 mosconfig);
2303 if (load_error == 0)
2304 return (0);
2305
2306 if (spa->spa_root_vdev != NULL)
2307 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2308
2309 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2310 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2311
2312 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2313 nvlist_free(config);
2314 return (load_error);
2315 }
2316
2317 /* Price of rolling back is discarding txgs, including log */
2318 if (state == SPA_LOAD_RECOVER)
2319 spa_set_log_state(spa, SPA_LOG_CLEAR);
2320
2321 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2322 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2323 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2324 TXG_INITIAL : safe_rewind_txg;
2325
2326 /*
2327 * Continue as long as we're finding errors, we're still within
2328 * the acceptable rewind range, and we're still finding uberblocks
2329 */
2330 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2331 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2332 if (spa->spa_load_max_txg < safe_rewind_txg)
2333 spa->spa_extreme_rewind = B_TRUE;
2334 rewind_error = spa_load_retry(spa, state, mosconfig);
2335 }
2336
2337 spa->spa_extreme_rewind = B_FALSE;
2338 spa->spa_load_max_txg = UINT64_MAX;
2339
2340 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2341 spa_config_set(spa, config);
2342
2343 return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
2344}
2345
2346/*
2347 * Pool Open/Import
2348 *
2349 * The import case is identical to an open except that the configuration is sent
2350 * down from userland, instead of grabbed from the configuration cache. For the
2351 * case of an open, the pool configuration will exist in the
2352 * POOL_STATE_UNINITIALIZED state.
2353 *
2354 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2355 * the same time open the pool, without having to keep around the spa_t in some
2356 * ambiguous state.
2357 */
2358static int
2359spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2360 nvlist_t **config)
2361{
2362 spa_t *spa;
2363 spa_load_state_t state = SPA_LOAD_OPEN;
2364 int error;
2365 int locked = B_FALSE;
2366 int firstopen = B_FALSE;
2367
2368 *spapp = NULL;
2369
2370 /*
2371 * As disgusting as this is, we need to support recursive calls to this
2372 * function because dsl_dir_open() is called during spa_load(), and ends
2373 * up calling spa_open() again. The real fix is to figure out how to
2374 * avoid dsl_dir_open() calling this in the first place.
2375 */
2376 if (mutex_owner(&spa_namespace_lock) != curthread) {
2377 mutex_enter(&spa_namespace_lock);
2378 locked = B_TRUE;
2379 }
2380
2381 if ((spa = spa_lookup(pool)) == NULL) {
2382 if (locked)
2383 mutex_exit(&spa_namespace_lock);
2384 return (ENOENT);
2385 }
2386
2387 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2388 zpool_rewind_policy_t policy;
2389
2390 firstopen = B_TRUE;
2391
2392 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2393 &policy);
2394 if (policy.zrp_request & ZPOOL_DO_REWIND)
2395 state = SPA_LOAD_RECOVER;
2396
2397 spa_activate(spa, spa_mode_global);
2398
2399 if (state != SPA_LOAD_RECOVER)
2400 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2401
2402 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2403 policy.zrp_request);
2404
2405 if (error == EBADF) {
2406 /*
2407 * If vdev_validate() returns failure (indicated by
2408 * EBADF), it indicates that one of the vdevs indicates
2409 * that the pool has been exported or destroyed. If
2410 * this is the case, the config cache is out of sync and
2411 * we should remove the pool from the namespace.
2412 */
2413 spa_unload(spa);
2414 spa_deactivate(spa);
2415 spa_config_sync(spa, B_TRUE, B_TRUE);
2416 spa_remove(spa);
2417 if (locked)
2418 mutex_exit(&spa_namespace_lock);
2419 return (ENOENT);
2420 }
2421
2422 if (error) {
2423 /*
2424 * We can't open the pool, but we still have useful
2425 * information: the state of each vdev after the
2426 * attempted vdev_open(). Return this to the user.
2427 */
2428 if (config != NULL && spa->spa_config) {
2429 VERIFY(nvlist_dup(spa->spa_config, config,
2430 KM_SLEEP) == 0);
2431 VERIFY(nvlist_add_nvlist(*config,
2432 ZPOOL_CONFIG_LOAD_INFO,
2433 spa->spa_load_info) == 0);
2434 }
2435 spa_unload(spa);
2436 spa_deactivate(spa);
2437 spa->spa_last_open_failed = error;
2438 if (locked)
2439 mutex_exit(&spa_namespace_lock);
2440 *spapp = NULL;
2441 return (error);
2442 }
2443 }
2444
2445 spa_open_ref(spa, tag);
2446
2447 if (config != NULL)
2448 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2449
2450 /*
2451 * If we've recovered the pool, pass back any information we
2452 * gathered while doing the load.
2453 */
2454 if (state == SPA_LOAD_RECOVER) {
2455 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2456 spa->spa_load_info) == 0);
2457 }
2458
2459 if (locked) {
2460 spa->spa_last_open_failed = 0;
2461 spa->spa_last_ubsync_txg = 0;
2462 spa->spa_load_txg = 0;
2463 mutex_exit(&spa_namespace_lock);
2464#ifdef __FreeBSD__
2465#ifdef _KERNEL
2466 if (firstopen)
2467 zvol_create_minors(pool);
2468#endif
2469#endif
2470 }
2471
2472 *spapp = spa;
2473
2474 return (0);
2475}
2476
2477int
2478spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2479 nvlist_t **config)
2480{
2481 return (spa_open_common(name, spapp, tag, policy, config));
2482}
2483
2484int
2485spa_open(const char *name, spa_t **spapp, void *tag)
2486{
2487 return (spa_open_common(name, spapp, tag, NULL, NULL));
2488}
2489
2490/*
2491 * Lookup the given spa_t, incrementing the inject count in the process,
2492 * preventing it from being exported or destroyed.
2493 */
2494spa_t *
2495spa_inject_addref(char *name)
2496{
2497 spa_t *spa;
2498
2499 mutex_enter(&spa_namespace_lock);
2500 if ((spa = spa_lookup(name)) == NULL) {
2501 mutex_exit(&spa_namespace_lock);
2502 return (NULL);
2503 }
2504 spa->spa_inject_ref++;
2505 mutex_exit(&spa_namespace_lock);
2506
2507 return (spa);
2508}
2509
2510void
2511spa_inject_delref(spa_t *spa)
2512{
2513 mutex_enter(&spa_namespace_lock);
2514 spa->spa_inject_ref--;
2515 mutex_exit(&spa_namespace_lock);
2516}
2517
2518/*
2519 * Add spares device information to the nvlist.
2520 */
2521static void
2522spa_add_spares(spa_t *spa, nvlist_t *config)
2523{
2524 nvlist_t **spares;
2525 uint_t i, nspares;
2526 nvlist_t *nvroot;
2527 uint64_t guid;
2528 vdev_stat_t *vs;
2529 uint_t vsc;
2530 uint64_t pool;
2531
2532 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2533
2534 if (spa->spa_spares.sav_count == 0)
2535 return;
2536
2537 VERIFY(nvlist_lookup_nvlist(config,
2538 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2539 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2540 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2541 if (nspares != 0) {
2542 VERIFY(nvlist_add_nvlist_array(nvroot,
2543 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2544 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2545 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2546
2547 /*
2548 * Go through and find any spares which have since been
2549 * repurposed as an active spare. If this is the case, update
2550 * their status appropriately.
2551 */
2552 for (i = 0; i < nspares; i++) {
2553 VERIFY(nvlist_lookup_uint64(spares[i],
2554 ZPOOL_CONFIG_GUID, &guid) == 0);
2555 if (spa_spare_exists(guid, &pool, NULL) &&
2556 pool != 0ULL) {
2557 VERIFY(nvlist_lookup_uint64_array(
2558 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2559 (uint64_t **)&vs, &vsc) == 0);
2560 vs->vs_state = VDEV_STATE_CANT_OPEN;
2561 vs->vs_aux = VDEV_AUX_SPARED;
2562 }
2563 }
2564 }
2565}
2566
2567/*
2568 * Add l2cache device information to the nvlist, including vdev stats.
2569 */
2570static void
2571spa_add_l2cache(spa_t *spa, nvlist_t *config)
2572{
2573 nvlist_t **l2cache;
2574 uint_t i, j, nl2cache;
2575 nvlist_t *nvroot;
2576 uint64_t guid;
2577 vdev_t *vd;
2578 vdev_stat_t *vs;
2579 uint_t vsc;
2580
2581 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2582
2583 if (spa->spa_l2cache.sav_count == 0)
2584 return;
2585
2586 VERIFY(nvlist_lookup_nvlist(config,
2587 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2588 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2589 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2590 if (nl2cache != 0) {
2591 VERIFY(nvlist_add_nvlist_array(nvroot,
2592 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2593 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2594 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2595
2596 /*
2597 * Update level 2 cache device stats.
2598 */
2599
2600 for (i = 0; i < nl2cache; i++) {
2601 VERIFY(nvlist_lookup_uint64(l2cache[i],
2602 ZPOOL_CONFIG_GUID, &guid) == 0);
2603
2604 vd = NULL;
2605 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2606 if (guid ==
2607 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2608 vd = spa->spa_l2cache.sav_vdevs[j];
2609 break;
2610 }
2611 }
2612 ASSERT(vd != NULL);
2613
2614 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2615 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2616 == 0);
2617 vdev_get_stats(vd, vs);
2618 }
2619 }
2620}
2621
2622int
2623spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2624{
2625 int error;
2626 spa_t *spa;
2627
2628 *config = NULL;
2629 error = spa_open_common(name, &spa, FTAG, NULL, config);
2630
2631 if (spa != NULL) {
2632 /*
2633 * This still leaves a window of inconsistency where the spares
2634 * or l2cache devices could change and the config would be
2635 * self-inconsistent.
2636 */
2637 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2638
2639 if (*config != NULL) {
2640 uint64_t loadtimes[2];
2641
2642 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
2643 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
2644 VERIFY(nvlist_add_uint64_array(*config,
2645 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
2646
2647 VERIFY(nvlist_add_uint64(*config,
2648 ZPOOL_CONFIG_ERRCOUNT,
2649 spa_get_errlog_size(spa)) == 0);
2650
2651 if (spa_suspended(spa))
2652 VERIFY(nvlist_add_uint64(*config,
2653 ZPOOL_CONFIG_SUSPENDED,
2654 spa->spa_failmode) == 0);
2655
2656 spa_add_spares(spa, *config);
2657 spa_add_l2cache(spa, *config);
2658 }
2659 }
2660
2661 /*
2662 * We want to get the alternate root even for faulted pools, so we cheat
2663 * and call spa_lookup() directly.
2664 */
2665 if (altroot) {
2666 if (spa == NULL) {
2667 mutex_enter(&spa_namespace_lock);
2668 spa = spa_lookup(name);
2669 if (spa)
2670 spa_altroot(spa, altroot, buflen);
2671 else
2672 altroot[0] = '\0';
2673 spa = NULL;
2674 mutex_exit(&spa_namespace_lock);
2675 } else {
2676 spa_altroot(spa, altroot, buflen);
2677 }
2678 }
2679
2680 if (spa != NULL) {
2681 spa_config_exit(spa, SCL_CONFIG, FTAG);
2682 spa_close(spa, FTAG);
2683 }
2684
2685 return (error);
2686}
2687
2688/*
2689 * Validate that the auxiliary device array is well formed. We must have an
2690 * array of nvlists, each which describes a valid leaf vdev. If this is an
2691 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2692 * specified, as long as they are well-formed.
2693 */
2694static int
2695spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
2696 spa_aux_vdev_t *sav, const char *config, uint64_t version,
2697 vdev_labeltype_t label)
2698{
2699 nvlist_t **dev;
2700 uint_t i, ndev;
2701 vdev_t *vd;
2702 int error;
2703
2704 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2705
2706 /*
2707 * It's acceptable to have no devs specified.
2708 */
2709 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
2710 return (0);
2711
2712 if (ndev == 0)
2713 return (EINVAL);
2714
2715 /*
2716 * Make sure the pool is formatted with a version that supports this
2717 * device type.
2718 */
2719 if (spa_version(spa) < version)
2720 return (ENOTSUP);
2721
2722 /*
2723 * Set the pending device list so we correctly handle device in-use
2724 * checking.
2725 */
2726 sav->sav_pending = dev;
2727 sav->sav_npending = ndev;
2728
2729 for (i = 0; i < ndev; i++) {
2730 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
2731 mode)) != 0)
2732 goto out;
2733
2734 if (!vd->vdev_ops->vdev_op_leaf) {
2735 vdev_free(vd);
2736 error = EINVAL;
2737 goto out;
2738 }
2739
2740 /*
2741 * The L2ARC currently only supports disk devices in
2742 * kernel context. For user-level testing, we allow it.
2743 */
2744#ifdef _KERNEL
2745 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
2746 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2747 error = ENOTBLK;
2748 goto out;
2749 }
2750#endif
2751 vd->vdev_top = vd;
2752
2753 if ((error = vdev_open(vd)) == 0 &&
2754 (error = vdev_label_init(vd, crtxg, label)) == 0) {
2755 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
2756 vd->vdev_guid) == 0);
2757 }
2758
2759 vdev_free(vd);
2760
2761 if (error &&
2762 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
2763 goto out;
2764 else
2765 error = 0;
2766 }
2767
2768out:
2769 sav->sav_pending = NULL;
2770 sav->sav_npending = 0;
2771 return (error);
2772}
2773
2774static int
2775spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
2776{
2777 int error;
2778
2779 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2780
2781 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2782 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
2783 VDEV_LABEL_SPARE)) != 0) {
2784 return (error);
2785 }
2786
2787 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2788 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
2789 VDEV_LABEL_L2CACHE));
2790}
2791
2792static void
2793spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
2794 const char *config)
2795{
2796 int i;
2797
2798 if (sav->sav_config != NULL) {
2799 nvlist_t **olddevs;
2800 uint_t oldndevs;
2801 nvlist_t **newdevs;
2802
2803 /*
2804 * Generate new dev list by concatentating with the
2805 * current dev list.
2806 */
2807 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
2808 &olddevs, &oldndevs) == 0);
2809
2810 newdevs = kmem_alloc(sizeof (void *) *
2811 (ndevs + oldndevs), KM_SLEEP);
2812 for (i = 0; i < oldndevs; i++)
2813 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
2814 KM_SLEEP) == 0);
2815 for (i = 0; i < ndevs; i++)
2816 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
2817 KM_SLEEP) == 0);
2818
2819 VERIFY(nvlist_remove(sav->sav_config, config,
2820 DATA_TYPE_NVLIST_ARRAY) == 0);
2821
2822 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2823 config, newdevs, ndevs + oldndevs) == 0);
2824 for (i = 0; i < oldndevs + ndevs; i++)
2825 nvlist_free(newdevs[i]);
2826 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
2827 } else {
2828 /*
2829 * Generate a new dev list.
2830 */
2831 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
2832 KM_SLEEP) == 0);
2833 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
2834 devs, ndevs) == 0);
2835 }
2836}
2837
2838/*
2839 * Stop and drop level 2 ARC devices
2840 */
2841void
2842spa_l2cache_drop(spa_t *spa)
2843{
2844 vdev_t *vd;
2845 int i;
2846 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2847
2848 for (i = 0; i < sav->sav_count; i++) {
2849 uint64_t pool;
2850
2851 vd = sav->sav_vdevs[i];
2852 ASSERT(vd != NULL);
2853
2854 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2855 pool != 0ULL && l2arc_vdev_present(vd))
2856 l2arc_remove_vdev(vd);
2857 if (vd->vdev_isl2cache)
2858 spa_l2cache_remove(vd);
2859 vdev_clear_stats(vd);
2860 (void) vdev_close(vd);
2861 }
2862}
2863
2864/*
2865 * Pool Creation
2866 */
2867int
2868spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
2869 const char *history_str, nvlist_t *zplprops)
2870{
2871 spa_t *spa;
2872 char *altroot = NULL;
2873 vdev_t *rvd;
2874 dsl_pool_t *dp;
2875 dmu_tx_t *tx;
2876 int error = 0;
2877 uint64_t txg = TXG_INITIAL;
2878 nvlist_t **spares, **l2cache;
2879 uint_t nspares, nl2cache;
2880 uint64_t version, obj;
2881
2882 /*
2883 * If this pool already exists, return failure.
2884 */
2885 mutex_enter(&spa_namespace_lock);
2886 if (spa_lookup(pool) != NULL) {
2887 mutex_exit(&spa_namespace_lock);
2888 return (EEXIST);
2889 }
2890
2891 /*
2892 * Allocate a new spa_t structure.
2893 */
2894 (void) nvlist_lookup_string(props,
2895 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2896 spa = spa_add(pool, NULL, altroot);
2897 spa_activate(spa, spa_mode_global);
2898
2899 if (props && (error = spa_prop_validate(spa, props))) {
2900 spa_deactivate(spa);
2901 spa_remove(spa);
2902 mutex_exit(&spa_namespace_lock);
2903 return (error);
2904 }
2905
2906 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2907 &version) != 0)
2908 version = SPA_VERSION;
2909 ASSERT(version <= SPA_VERSION);
2910
2911 spa->spa_first_txg = txg;
2912 spa->spa_uberblock.ub_txg = txg - 1;
2913 spa->spa_uberblock.ub_version = version;
2914 spa->spa_ubsync = spa->spa_uberblock;
2915
2916 /*
2917 * Create "The Godfather" zio to hold all async IOs
2918 */
2919 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2920 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2921
2922 /*
2923 * Create the root vdev.
2924 */
2925 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2926
2927 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
2928
2929 ASSERT(error != 0 || rvd != NULL);
2930 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
2931
2932 if (error == 0 && !zfs_allocatable_devs(nvroot))
2933 error = EINVAL;
2934
2935 if (error == 0 &&
2936 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
2937 (error = spa_validate_aux(spa, nvroot, txg,
2938 VDEV_ALLOC_ADD)) == 0) {
2939 for (int c = 0; c < rvd->vdev_children; c++) {
2940 vdev_metaslab_set_size(rvd->vdev_child[c]);
2941 vdev_expand(rvd->vdev_child[c], txg);
2942 }
2943 }
2944
2945 spa_config_exit(spa, SCL_ALL, FTAG);
2946
2947 if (error != 0) {
2948 spa_unload(spa);
2949 spa_deactivate(spa);
2950 spa_remove(spa);
2951 mutex_exit(&spa_namespace_lock);
2952 return (error);
2953 }
2954
2955 /*
2956 * Get the list of spares, if specified.
2957 */
2958 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2959 &spares, &nspares) == 0) {
2960 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
2961 KM_SLEEP) == 0);
2962 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
2963 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2964 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2965 spa_load_spares(spa);
2966 spa_config_exit(spa, SCL_ALL, FTAG);
2967 spa->spa_spares.sav_sync = B_TRUE;
2968 }
2969
2970 /*
2971 * Get the list of level 2 cache devices, if specified.
2972 */
2973 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2974 &l2cache, &nl2cache) == 0) {
2975 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2976 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2977 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2978 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2979 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2980 spa_load_l2cache(spa);
2981 spa_config_exit(spa, SCL_ALL, FTAG);
2982 spa->spa_l2cache.sav_sync = B_TRUE;
2983 }
2984
2985 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2986 spa->spa_meta_objset = dp->dp_meta_objset;
2987
2988 /*
2989 * Create DDTs (dedup tables).
2990 */
2991 ddt_create(spa);
2992
2993 spa_update_dspace(spa);
2994
2995 tx = dmu_tx_create_assigned(dp, txg);
2996
2997 /*
2998 * Create the pool config object.
2999 */
3000 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3001 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3002 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3003
3004 if (zap_add(spa->spa_meta_objset,
3005 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3006 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3007 cmn_err(CE_PANIC, "failed to add pool config");
3008 }
3009
3010 if (zap_add(spa->spa_meta_objset,
3011 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3012 sizeof (uint64_t), 1, &version, tx) != 0) {
3013 cmn_err(CE_PANIC, "failed to add pool version");
3014 }
3015
3016 /* Newly created pools with the right version are always deflated. */
3017 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3018 spa->spa_deflate = TRUE;
3019 if (zap_add(spa->spa_meta_objset,
3020 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3021 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3022 cmn_err(CE_PANIC, "failed to add deflate");
3023 }
3024 }
3025
3026 /*
3027 * Create the deferred-free bpobj. Turn off compression
3028 * because sync-to-convergence takes longer if the blocksize
3029 * keeps changing.
3030 */
3031 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3032 dmu_object_set_compress(spa->spa_meta_objset, obj,
3033 ZIO_COMPRESS_OFF, tx);
3034 if (zap_add(spa->spa_meta_objset,
3035 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3036 sizeof (uint64_t), 1, &obj, tx) != 0) {
3037 cmn_err(CE_PANIC, "failed to add bpobj");
3038 }
3039 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3040 spa->spa_meta_objset, obj));
3041
3042 /*
3043 * Create the pool's history object.
3044 */
3045 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3046 spa_history_create_obj(spa, tx);
3047
3048 /*
3049 * Set pool properties.
3050 */
3051 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3052 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3053 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3054 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3055
3056 if (props != NULL) {
3057 spa_configfile_set(spa, props, B_FALSE);
3058 spa_sync_props(spa, props, tx);
3059 }
3060
3061 dmu_tx_commit(tx);
3062
3063 spa->spa_sync_on = B_TRUE;
3064 txg_sync_start(spa->spa_dsl_pool);
3065
3066 /*
3067 * We explicitly wait for the first transaction to complete so that our
3068 * bean counters are appropriately updated.
3069 */
3070 txg_wait_synced(spa->spa_dsl_pool, txg);
3071
3072 spa_config_sync(spa, B_FALSE, B_TRUE);
3073
3074 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3075 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3076 spa_history_log_version(spa, LOG_POOL_CREATE);
3077
3078 spa->spa_minref = refcount_count(&spa->spa_refcount);
3079
3080 mutex_exit(&spa_namespace_lock);
3081
3082 return (0);
3083}
3084
3085#if defined(sun)
3086#ifdef _KERNEL
3087/*
3088 * Get the root pool information from the root disk, then import the root pool
3089 * during the system boot up time.
3090 */
3091extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3092
3093static nvlist_t *
3094spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3095{
3096 nvlist_t *config;
3097 nvlist_t *nvtop, *nvroot;
3098 uint64_t pgid;
3099
3100 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3101 return (NULL);
3102
3103 /*
3104 * Add this top-level vdev to the child array.
3105 */
3106 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3107 &nvtop) == 0);
3108 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3109 &pgid) == 0);
3110 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3111
3112 /*
3113 * Put this pool's top-level vdevs into a root vdev.
3114 */
3115 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3116 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3117 VDEV_TYPE_ROOT) == 0);
3118 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3119 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3120 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3121 &nvtop, 1) == 0);
3122
3123 /*
3124 * Replace the existing vdev_tree with the new root vdev in
3125 * this pool's configuration (remove the old, add the new).
3126 */
3127 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3128 nvlist_free(nvroot);
3129 return (config);
3130}
3131
3132/*
3133 * Walk the vdev tree and see if we can find a device with "better"
3134 * configuration. A configuration is "better" if the label on that
3135 * device has a more recent txg.
3136 */
3137static void
3138spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3139{
3140 for (int c = 0; c < vd->vdev_children; c++)
3141 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3142
3143 if (vd->vdev_ops->vdev_op_leaf) {
3144 nvlist_t *label;
3145 uint64_t label_txg;
3146
3147 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3148 &label) != 0)
3149 return;
3150
3151 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3152 &label_txg) == 0);
3153
3154 /*
3155 * Do we have a better boot device?
3156 */
3157 if (label_txg > *txg) {
3158 *txg = label_txg;
3159 *avd = vd;
3160 }
3161 nvlist_free(label);
3162 }
3163}
3164
3165/*
3166 * Import a root pool.
3167 *
3168 * For x86. devpath_list will consist of devid and/or physpath name of
3169 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3170 * The GRUB "findroot" command will return the vdev we should boot.
3171 *
3172 * For Sparc, devpath_list consists the physpath name of the booting device
3173 * no matter the rootpool is a single device pool or a mirrored pool.
3174 * e.g.
3175 * "/pci@1f,0/ide@d/disk@0,0:a"
3176 */
3177int
3178spa_import_rootpool(char *devpath, char *devid)
3179{
3180 spa_t *spa;
3181 vdev_t *rvd, *bvd, *avd = NULL;
3182 nvlist_t *config, *nvtop;
3183 uint64_t guid, txg;
3184 char *pname;
3185 int error;
3186
3187 /*
3188 * Read the label from the boot device and generate a configuration.
3189 */
3190 config = spa_generate_rootconf(devpath, devid, &guid);
3191#if defined(_OBP) && defined(_KERNEL)
3192 if (config == NULL) {
3193 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3194 /* iscsi boot */
3195 get_iscsi_bootpath_phy(devpath);
3196 config = spa_generate_rootconf(devpath, devid, &guid);
3197 }
3198 }
3199#endif
3200 if (config == NULL) {
3201 cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
3202 devpath);
3203 return (EIO);
3204 }
3205
3206 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3207 &pname) == 0);
3208 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3209
3210 mutex_enter(&spa_namespace_lock);
3211 if ((spa = spa_lookup(pname)) != NULL) {
3212 /*
3213 * Remove the existing root pool from the namespace so that we
3214 * can replace it with the correct config we just read in.
3215 */
3216 spa_remove(spa);
3217 }
3218
3219 spa = spa_add(pname, config, NULL);
3220 spa->spa_is_root = B_TRUE;
3221 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3222
3223 /*
3224 * Build up a vdev tree based on the boot device's label config.
3225 */
3226 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3227 &nvtop) == 0);
3228 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3229 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3230 VDEV_ALLOC_ROOTPOOL);
3231 spa_config_exit(spa, SCL_ALL, FTAG);
3232 if (error) {
3233 mutex_exit(&spa_namespace_lock);
3234 nvlist_free(config);
3235 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3236 pname);
3237 return (error);
3238 }
3239
3240 /*
3241 * Get the boot vdev.
3242 */
3243 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3244 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3245 (u_longlong_t)guid);
3246 error = ENOENT;
3247 goto out;
3248 }
3249
3250 /*
3251 * Determine if there is a better boot device.
3252 */
3253 avd = bvd;
3254 spa_alt_rootvdev(rvd, &avd, &txg);
3255 if (avd != bvd) {
3256 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3257 "try booting from '%s'", avd->vdev_path);
3258 error = EINVAL;
3259 goto out;
3260 }
3261
3262 /*
3263 * If the boot device is part of a spare vdev then ensure that
3264 * we're booting off the active spare.
3265 */
3266 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3267 !bvd->vdev_isspare) {
3268 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3269 "try booting from '%s'",
3270 bvd->vdev_parent->
3271 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3272 error = EINVAL;
3273 goto out;
3274 }
3275
3276 error = 0;
3277 spa_history_log_version(spa, LOG_POOL_IMPORT);
3278out:
3279 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3280 vdev_free(rvd);
3281 spa_config_exit(spa, SCL_ALL, FTAG);
3282 mutex_exit(&spa_namespace_lock);
3283
3284 nvlist_free(config);
3285 return (error);
3286}
3287
3288#endif
3289#endif /* sun */
3290
3291/*
3292 * Import a non-root pool into the system.
3293 */
3294int
3295spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3296{
3297 spa_t *spa;
3298 char *altroot = NULL;
3299 spa_load_state_t state = SPA_LOAD_IMPORT;
3300 zpool_rewind_policy_t policy;
3301 uint64_t mode = spa_mode_global;
3302 uint64_t readonly = B_FALSE;
3303 int error;
3304 nvlist_t *nvroot;
3305 nvlist_t **spares, **l2cache;
3306 uint_t nspares, nl2cache;
3307
3308 /*
3309 * If a pool with this name exists, return failure.
3310 */
3311 mutex_enter(&spa_namespace_lock);
3312 if (spa_lookup(pool) != NULL) {
3313 mutex_exit(&spa_namespace_lock);
3314 return (EEXIST);
3315 }
3316
3317 /*
3318 * Create and initialize the spa structure.
3319 */
3320 (void) nvlist_lookup_string(props,
3321 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3322 (void) nvlist_lookup_uint64(props,
3323 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3324 if (readonly)
3325 mode = FREAD;
3326 spa = spa_add(pool, config, altroot);
3327 spa->spa_import_flags = flags;
3328
3329 /*
3330 * Verbatim import - Take a pool and insert it into the namespace
3331 * as if it had been loaded at boot.
3332 */
3333 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3334 if (props != NULL)
3335 spa_configfile_set(spa, props, B_FALSE);
3336
3337 spa_config_sync(spa, B_FALSE, B_TRUE);
3338
3339 mutex_exit(&spa_namespace_lock);
3340 spa_history_log_version(spa, LOG_POOL_IMPORT);
3341
3342 return (0);
3343 }
3344
3345 spa_activate(spa, mode);
3346
3347 /*
3348 * Don't start async tasks until we know everything is healthy.
3349 */
3350 spa_async_suspend(spa);
3351
3352 zpool_get_rewind_policy(config, &policy);
3353 if (policy.zrp_request & ZPOOL_DO_REWIND)
3354 state = SPA_LOAD_RECOVER;
3355
3356 /*
3357 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3358 * because the user-supplied config is actually the one to trust when
3359 * doing an import.
3360 */
3361 if (state != SPA_LOAD_RECOVER)
3362 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3363
3364 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3365 policy.zrp_request);
3366
3367 /*
3368 * Propagate anything learned while loading the pool and pass it
3369 * back to caller (i.e. rewind info, missing devices, etc).
3370 */
3371 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3372 spa->spa_load_info) == 0);
3373
3374 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3375 /*
3376 * Toss any existing sparelist, as it doesn't have any validity
3377 * anymore, and conflicts with spa_has_spare().
3378 */
3379 if (spa->spa_spares.sav_config) {
3380 nvlist_free(spa->spa_spares.sav_config);
3381 spa->spa_spares.sav_config = NULL;
3382 spa_load_spares(spa);
3383 }
3384 if (spa->spa_l2cache.sav_config) {
3385 nvlist_free(spa->spa_l2cache.sav_config);
3386 spa->spa_l2cache.sav_config = NULL;
3387 spa_load_l2cache(spa);
3388 }
3389
3390 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3391 &nvroot) == 0);
3392 if (error == 0)
3393 error = spa_validate_aux(spa, nvroot, -1ULL,
3394 VDEV_ALLOC_SPARE);
3395 if (error == 0)
3396 error = spa_validate_aux(spa, nvroot, -1ULL,
3397 VDEV_ALLOC_L2CACHE);
3398 spa_config_exit(spa, SCL_ALL, FTAG);
3399
3400 if (props != NULL)
3401 spa_configfile_set(spa, props, B_FALSE);
3402
3403 if (error != 0 || (props && spa_writeable(spa) &&
3404 (error = spa_prop_set(spa, props)))) {
3405 spa_unload(spa);
3406 spa_deactivate(spa);
3407 spa_remove(spa);
3408 mutex_exit(&spa_namespace_lock);
3409 return (error);
3410 }
3411
3412 spa_async_resume(spa);
3413
3414 /*
3415 * Override any spares and level 2 cache devices as specified by
3416 * the user, as these may have correct device names/devids, etc.
3417 */
3418 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3419 &spares, &nspares) == 0) {
3420 if (spa->spa_spares.sav_config)
3421 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3422 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3423 else
3424 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3425 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3426 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3427 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3428 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3429 spa_load_spares(spa);
3430 spa_config_exit(spa, SCL_ALL, FTAG);
3431 spa->spa_spares.sav_sync = B_TRUE;
3432 }
3433 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3434 &l2cache, &nl2cache) == 0) {
3435 if (spa->spa_l2cache.sav_config)
3436 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3437 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3438 else
3439 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3440 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3441 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3442 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3443 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3444 spa_load_l2cache(spa);
3445 spa_config_exit(spa, SCL_ALL, FTAG);
3446 spa->spa_l2cache.sav_sync = B_TRUE;
3447 }
3448
3449 /*
3450 * Check for any removed devices.
3451 */
3452 if (spa->spa_autoreplace) {
3453 spa_aux_check_removed(&spa->spa_spares);
3454 spa_aux_check_removed(&spa->spa_l2cache);
3455 }
3456
3457 if (spa_writeable(spa)) {
3458 /*
3459 * Update the config cache to include the newly-imported pool.
3460 */
3461 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3462 }
3463
3464 /*
3465 * It's possible that the pool was expanded while it was exported.
3466 * We kick off an async task to handle this for us.
3467 */
3468 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3469
3470 mutex_exit(&spa_namespace_lock);
3471 spa_history_log_version(spa, LOG_POOL_IMPORT);
3472
3473#ifdef __FreeBSD__
3474#ifdef _KERNEL
3475 zvol_create_minors(pool);
3476#endif
3477#endif
3478 return (0);
3479}
3480
3481nvlist_t *
3482spa_tryimport(nvlist_t *tryconfig)
3483{
3484 nvlist_t *config = NULL;
3485 char *poolname;
3486 spa_t *spa;
3487 uint64_t state;
3488 int error;
3489
3490 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3491 return (NULL);
3492
3493 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3494 return (NULL);
3495
3496 /*
3497 * Create and initialize the spa structure.
3498 */
3499 mutex_enter(&spa_namespace_lock);
3500 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3501 spa_activate(spa, FREAD);
3502
3503 /*
3504 * Pass off the heavy lifting to spa_load().
3505 * Pass TRUE for mosconfig because the user-supplied config
3506 * is actually the one to trust when doing an import.
3507 */
3508 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3509
3510 /*
3511 * If 'tryconfig' was at least parsable, return the current config.
3512 */
3513 if (spa->spa_root_vdev != NULL) {
3514 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3515 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3516 poolname) == 0);
3517 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3518 state) == 0);
3519 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3520 spa->spa_uberblock.ub_timestamp) == 0);
3521
3522 /*
3523 * If the bootfs property exists on this pool then we
3524 * copy it out so that external consumers can tell which
3525 * pools are bootable.
3526 */
3527 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3528 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3529
3530 /*
3531 * We have to play games with the name since the
3532 * pool was opened as TRYIMPORT_NAME.
3533 */
3534 if (dsl_dsobj_to_dsname(spa_name(spa),
3535 spa->spa_bootfs, tmpname) == 0) {
3536 char *cp;
3537 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3538
3539 cp = strchr(tmpname, '/');
3540 if (cp == NULL) {
3541 (void) strlcpy(dsname, tmpname,
3542 MAXPATHLEN);
3543 } else {
3544 (void) snprintf(dsname, MAXPATHLEN,
3545 "%s/%s", poolname, ++cp);
3546 }
3547 VERIFY(nvlist_add_string(config,
3548 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3549 kmem_free(dsname, MAXPATHLEN);
3550 }
3551 kmem_free(tmpname, MAXPATHLEN);
3552 }
3553
3554 /*
3555 * Add the list of hot spares and level 2 cache devices.
3556 */
3557 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3558 spa_add_spares(spa, config);
3559 spa_add_l2cache(spa, config);
3560 spa_config_exit(spa, SCL_CONFIG, FTAG);
3561 }
3562
3563 spa_unload(spa);
3564 spa_deactivate(spa);
3565 spa_remove(spa);
3566 mutex_exit(&spa_namespace_lock);
3567
3568 return (config);
3569}
3570
3571/*
3572 * Pool export/destroy
3573 *
3574 * The act of destroying or exporting a pool is very simple. We make sure there
3575 * is no more pending I/O and any references to the pool are gone. Then, we
3576 * update the pool state and sync all the labels to disk, removing the
3577 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3578 * we don't sync the labels or remove the configuration cache.
3579 */
3580static int
3581spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3582 boolean_t force, boolean_t hardforce)
3583{
3584 spa_t *spa;
3585
3586 if (oldconfig)
3587 *oldconfig = NULL;
3588
3589 if (!(spa_mode_global & FWRITE))
3590 return (EROFS);
3591
3592 mutex_enter(&spa_namespace_lock);
3593 if ((spa = spa_lookup(pool)) == NULL) {
3594 mutex_exit(&spa_namespace_lock);
3595 return (ENOENT);
3596 }
3597
3598 /*
3599 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3600 * reacquire the namespace lock, and see if we can export.
3601 */
3602 spa_open_ref(spa, FTAG);
3603 mutex_exit(&spa_namespace_lock);
3604 spa_async_suspend(spa);
3605 mutex_enter(&spa_namespace_lock);
3606 spa_close(spa, FTAG);
3607
3608 /*
3609 * The pool will be in core if it's openable,
3610 * in which case we can modify its state.
3611 */
3612 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3613 /*
3614 * Objsets may be open only because they're dirty, so we
3615 * have to force it to sync before checking spa_refcnt.
3616 */
3617 txg_wait_synced(spa->spa_dsl_pool, 0);
3618
3619 /*
3620 * A pool cannot be exported or destroyed if there are active
3621 * references. If we are resetting a pool, allow references by
3622 * fault injection handlers.
3623 */
3624 if (!spa_refcount_zero(spa) ||
3625 (spa->spa_inject_ref != 0 &&
3626 new_state != POOL_STATE_UNINITIALIZED)) {
3627 spa_async_resume(spa);
3628 mutex_exit(&spa_namespace_lock);
3629 return (EBUSY);
3630 }
3631
3632 /*
3633 * A pool cannot be exported if it has an active shared spare.
3634 * This is to prevent other pools stealing the active spare
3635 * from an exported pool. At user's own will, such pool can
3636 * be forcedly exported.
3637 */
3638 if (!force && new_state == POOL_STATE_EXPORTED &&
3639 spa_has_active_shared_spare(spa)) {
3640 spa_async_resume(spa);
3641 mutex_exit(&spa_namespace_lock);
3642 return (EXDEV);
3643 }
3644
3645 /*
3646 * We want this to be reflected on every label,
3647 * so mark them all dirty. spa_unload() will do the
3648 * final sync that pushes these changes out.
3649 */
3650 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
3651 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3652 spa->spa_state = new_state;
3653 spa->spa_final_txg = spa_last_synced_txg(spa) +
3654 TXG_DEFER_SIZE + 1;
3655 vdev_config_dirty(spa->spa_root_vdev);
3656 spa_config_exit(spa, SCL_ALL, FTAG);
3657 }
3658 }
3659
3660 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
3661
3662 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3663 spa_unload(spa);
3664 spa_deactivate(spa);
3665 }
3666
3667 if (oldconfig && spa->spa_config)
3668 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
3669
3670 if (new_state != POOL_STATE_UNINITIALIZED) {
3671 if (!hardforce)
3672 spa_config_sync(spa, B_TRUE, B_TRUE);
3673 spa_remove(spa);
3674 }
3675 mutex_exit(&spa_namespace_lock);
3676
3677 return (0);
3678}
3679
3680/*
3681 * Destroy a storage pool.
3682 */
3683int
3684spa_destroy(char *pool)
3685{
3686 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
3687 B_FALSE, B_FALSE));
3688}
3689
3690/*
3691 * Export a storage pool.
3692 */
3693int
3694spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
3695 boolean_t hardforce)
3696{
3697 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
3698 force, hardforce));
3699}
3700
3701/*
3702 * Similar to spa_export(), this unloads the spa_t without actually removing it
3703 * from the namespace in any way.
3704 */
3705int
3706spa_reset(char *pool)
3707{
3708 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
3709 B_FALSE, B_FALSE));
3710}
3711
3712/*
3713 * ==========================================================================
3714 * Device manipulation
3715 * ==========================================================================
3716 */
3717
3718/*
3719 * Add a device to a storage pool.
3720 */
3721int
3722spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3723{
3724 uint64_t txg, id;
3725 int error;
3726 vdev_t *rvd = spa->spa_root_vdev;
3727 vdev_t *vd, *tvd;
3728 nvlist_t **spares, **l2cache;
3729 uint_t nspares, nl2cache;
3730
3731 ASSERT(spa_writeable(spa));
3732
3733 txg = spa_vdev_enter(spa);
3734
3735 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
3736 VDEV_ALLOC_ADD)) != 0)
3737 return (spa_vdev_exit(spa, NULL, txg, error));
3738
3739 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
3740
3741 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
3742 &nspares) != 0)
3743 nspares = 0;
3744
3745 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
3746 &nl2cache) != 0)
3747 nl2cache = 0;
3748
3749 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
3750 return (spa_vdev_exit(spa, vd, txg, EINVAL));
3751
3752 if (vd->vdev_children != 0 &&
3753 (error = vdev_create(vd, txg, B_FALSE)) != 0)
3754 return (spa_vdev_exit(spa, vd, txg, error));
3755
3756 /*
3757 * We must validate the spares and l2cache devices after checking the
3758 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
3759 */
3760 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
3761 return (spa_vdev_exit(spa, vd, txg, error));
3762
3763 /*
3764 * Transfer each new top-level vdev from vd to rvd.
3765 */
3766 for (int c = 0; c < vd->vdev_children; c++) {
3767
3768 /*
3769 * Set the vdev id to the first hole, if one exists.
3770 */
3771 for (id = 0; id < rvd->vdev_children; id++) {
3772 if (rvd->vdev_child[id]->vdev_ishole) {
3773 vdev_free(rvd->vdev_child[id]);
3774 break;
3775 }
3776 }
3777 tvd = vd->vdev_child[c];
3778 vdev_remove_child(vd, tvd);
3779 tvd->vdev_id = id;
3780 vdev_add_child(rvd, tvd);
3781 vdev_config_dirty(tvd);
3782 }
3783
3784 if (nspares != 0) {
3785 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
3786 ZPOOL_CONFIG_SPARES);
3787 spa_load_spares(spa);
3788 spa->spa_spares.sav_sync = B_TRUE;
3789 }
3790
3791 if (nl2cache != 0) {
3792 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
3793 ZPOOL_CONFIG_L2CACHE);
3794 spa_load_l2cache(spa);
3795 spa->spa_l2cache.sav_sync = B_TRUE;
3796 }
3797
3798 /*
3799 * We have to be careful when adding new vdevs to an existing pool.
3800 * If other threads start allocating from these vdevs before we
3801 * sync the config cache, and we lose power, then upon reboot we may
3802 * fail to open the pool because there are DVAs that the config cache
3803 * can't translate. Therefore, we first add the vdevs without
3804 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
3805 * and then let spa_config_update() initialize the new metaslabs.
3806 *
3807 * spa_load() checks for added-but-not-initialized vdevs, so that
3808 * if we lose power at any point in this sequence, the remaining
3809 * steps will be completed the next time we load the pool.
3810 */
3811 (void) spa_vdev_exit(spa, vd, txg, 0);
3812
3813 mutex_enter(&spa_namespace_lock);
3814 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3815 mutex_exit(&spa_namespace_lock);
3816
3817 return (0);
3818}
3819
3820/*
3821 * Attach a device to a mirror. The arguments are the path to any device
3822 * in the mirror, and the nvroot for the new device. If the path specifies
3823 * a device that is not mirrored, we automatically insert the mirror vdev.
3824 *
3825 * If 'replacing' is specified, the new device is intended to replace the
3826 * existing device; in this case the two devices are made into their own
3827 * mirror using the 'replacing' vdev, which is functionally identical to
3828 * the mirror vdev (it actually reuses all the same ops) but has a few
3829 * extra rules: you can't attach to it after it's been created, and upon
3830 * completion of resilvering, the first disk (the one being replaced)
3831 * is automatically detached.
3832 */
3833int
3834spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3835{
3836 uint64_t txg, dtl_max_txg;
3837 vdev_t *rvd = spa->spa_root_vdev;
3838 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
3839 vdev_ops_t *pvops;
3840 char *oldvdpath, *newvdpath;
3841 int newvd_isspare;
3842 int error;
3843
3844 ASSERT(spa_writeable(spa));
3845
3846 txg = spa_vdev_enter(spa);
3847
3848 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3849
3850 if (oldvd == NULL)
3851 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3852
3853 if (!oldvd->vdev_ops->vdev_op_leaf)
3854 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3855
3856 pvd = oldvd->vdev_parent;
3857
3858 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
3859 VDEV_ALLOC_ADD)) != 0)
3860 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
3861
3862 if (newrootvd->vdev_children != 1)
3863 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3864
3865 newvd = newrootvd->vdev_child[0];
3866
3867 if (!newvd->vdev_ops->vdev_op_leaf)
3868 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3869
3870 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3871 return (spa_vdev_exit(spa, newrootvd, txg, error));
3872
3873 /*
3874 * Spares can't replace logs
3875 */
3876 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
3877 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3878
3879 if (!replacing) {
3880 /*
3881 * For attach, the only allowable parent is a mirror or the root
3882 * vdev.
3883 */
3884 if (pvd->vdev_ops != &vdev_mirror_ops &&
3885 pvd->vdev_ops != &vdev_root_ops)
3886 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3887
3888 pvops = &vdev_mirror_ops;
3889 } else {
3890 /*
3891 * Active hot spares can only be replaced by inactive hot
3892 * spares.
3893 */
3894 if (pvd->vdev_ops == &vdev_spare_ops &&
3895 oldvd->vdev_isspare &&
3896 !spa_has_spare(spa, newvd->vdev_guid))
3897 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3898
3899 /*
3900 * If the source is a hot spare, and the parent isn't already a
3901 * spare, then we want to create a new hot spare. Otherwise, we
3902 * want to create a replacing vdev. The user is not allowed to
3903 * attach to a spared vdev child unless the 'isspare' state is
3904 * the same (spare replaces spare, non-spare replaces
3905 * non-spare).
3906 */
3907 if (pvd->vdev_ops == &vdev_replacing_ops &&
3908 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
3909 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3910 } else if (pvd->vdev_ops == &vdev_spare_ops &&
3911 newvd->vdev_isspare != oldvd->vdev_isspare) {
3912 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3913 }
3914
3915 if (newvd->vdev_isspare)
3916 pvops = &vdev_spare_ops;
3917 else
3918 pvops = &vdev_replacing_ops;
3919 }
3920
3921 /*
3922 * Make sure the new device is big enough.
3923 */
3924 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3925 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3926
3927 /*
3928 * The new device cannot have a higher alignment requirement
3929 * than the top-level vdev.
3930 */
3931 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3932 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3933
3934 /*
3935 * If this is an in-place replacement, update oldvd's path and devid
3936 * to make it distinguishable from newvd, and unopenable from now on.
3937 */
3938 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3939 spa_strfree(oldvd->vdev_path);
3940 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3941 KM_SLEEP);
3942 (void) sprintf(oldvd->vdev_path, "%s/%s",
3943 newvd->vdev_path, "old");
3944 if (oldvd->vdev_devid != NULL) {
3945 spa_strfree(oldvd->vdev_devid);
3946 oldvd->vdev_devid = NULL;
3947 }
3948 }
3949
3950 /* mark the device being resilvered */
3951 newvd->vdev_resilvering = B_TRUE;
3952
3953 /*
3954 * If the parent is not a mirror, or if we're replacing, insert the new
3955 * mirror/replacing/spare vdev above oldvd.
3956 */
3957 if (pvd->vdev_ops != pvops)
3958 pvd = vdev_add_parent(oldvd, pvops);
3959
3960 ASSERT(pvd->vdev_top->vdev_parent == rvd);
3961 ASSERT(pvd->vdev_ops == pvops);
3962 ASSERT(oldvd->vdev_parent == pvd);
3963
3964 /*
3965 * Extract the new device from its root and add it to pvd.
3966 */
3967 vdev_remove_child(newrootvd, newvd);
3968 newvd->vdev_id = pvd->vdev_children;
3969 newvd->vdev_crtxg = oldvd->vdev_crtxg;
3970 vdev_add_child(pvd, newvd);
3971
3972 tvd = newvd->vdev_top;
3973 ASSERT(pvd->vdev_top == tvd);
3974 ASSERT(tvd->vdev_parent == rvd);
3975
3976 vdev_config_dirty(tvd);
3977
3978 /*
3979 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
3980 * for any dmu_sync-ed blocks. It will propagate upward when
3981 * spa_vdev_exit() calls vdev_dtl_reassess().
3982 */
3983 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
3984
3985 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
3986 dtl_max_txg - TXG_INITIAL);
3987
3988 if (newvd->vdev_isspare) {
3989 spa_spare_activate(newvd);
3990 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
3991 }
3992
3993 oldvdpath = spa_strdup(oldvd->vdev_path);
3994 newvdpath = spa_strdup(newvd->vdev_path);
3995 newvd_isspare = newvd->vdev_isspare;
3996
3997 /*
3998 * Mark newvd's DTL dirty in this txg.
3999 */
4000 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4001
4002 /*
4003 * Restart the resilver
4004 */
4005 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4006
4007 /*
4008 * Commit the config
4009 */
4010 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4011
4012 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4013 "%s vdev=%s %s vdev=%s",
4014 replacing && newvd_isspare ? "spare in" :
4015 replacing ? "replace" : "attach", newvdpath,
4016 replacing ? "for" : "to", oldvdpath);
4017
4018 spa_strfree(oldvdpath);
4019 spa_strfree(newvdpath);
4020
4021 if (spa->spa_bootfs)
4022 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4023
4024 return (0);
4025}
4026
4027/*
4028 * Detach a device from a mirror or replacing vdev.
4029 * If 'replace_done' is specified, only detach if the parent
4030 * is a replacing vdev.
4031 */
4032int
4033spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4034{
4035 uint64_t txg;
4036 int error;
4037 vdev_t *rvd = spa->spa_root_vdev;
4038 vdev_t *vd, *pvd, *cvd, *tvd;
4039 boolean_t unspare = B_FALSE;
4040 uint64_t unspare_guid;
4041 char *vdpath;
4042
4043 ASSERT(spa_writeable(spa));
4044
4045 txg = spa_vdev_enter(spa);
4046
4047 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4048
4049 if (vd == NULL)
4050 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4051
4052 if (!vd->vdev_ops->vdev_op_leaf)
4053 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4054
4055 pvd = vd->vdev_parent;
4056
4057 /*
4058 * If the parent/child relationship is not as expected, don't do it.
4059 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4060 * vdev that's replacing B with C. The user's intent in replacing
4061 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4062 * the replace by detaching C, the expected behavior is to end up
4063 * M(A,B). But suppose that right after deciding to detach C,
4064 * the replacement of B completes. We would have M(A,C), and then
4065 * ask to detach C, which would leave us with just A -- not what
4066 * the user wanted. To prevent this, we make sure that the
4067 * parent/child relationship hasn't changed -- in this example,
4068 * that C's parent is still the replacing vdev R.
4069 */
4070 if (pvd->vdev_guid != pguid && pguid != 0)
4071 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4072
4073 /*
4074 * Only 'replacing' or 'spare' vdevs can be replaced.
4075 */
4076 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4077 pvd->vdev_ops != &vdev_spare_ops)
4078 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4079
4080 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4081 spa_version(spa) >= SPA_VERSION_SPARES);
4082
4083 /*
4084 * Only mirror, replacing, and spare vdevs support detach.
4085 */
4086 if (pvd->vdev_ops != &vdev_replacing_ops &&
4087 pvd->vdev_ops != &vdev_mirror_ops &&
4088 pvd->vdev_ops != &vdev_spare_ops)
4089 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4090
4091 /*
4092 * If this device has the only valid copy of some data,
4093 * we cannot safely detach it.
4094 */
4095 if (vdev_dtl_required(vd))
4096 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4097
4098 ASSERT(pvd->vdev_children >= 2);
4099
4100 /*
4101 * If we are detaching the second disk from a replacing vdev, then
4102 * check to see if we changed the original vdev's path to have "/old"
4103 * at the end in spa_vdev_attach(). If so, undo that change now.
4104 */
4105 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4106 vd->vdev_path != NULL) {
4107 size_t len = strlen(vd->vdev_path);
4108
4109 for (int c = 0; c < pvd->vdev_children; c++) {
4110 cvd = pvd->vdev_child[c];
4111
4112 if (cvd == vd || cvd->vdev_path == NULL)
4113 continue;
4114
4115 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4116 strcmp(cvd->vdev_path + len, "/old") == 0) {
4117 spa_strfree(cvd->vdev_path);
4118 cvd->vdev_path = spa_strdup(vd->vdev_path);
4119 break;
4120 }
4121 }
4122 }
4123
4124 /*
4125 * If we are detaching the original disk from a spare, then it implies
4126 * that the spare should become a real disk, and be removed from the
4127 * active spare list for the pool.
4128 */
4129 if (pvd->vdev_ops == &vdev_spare_ops &&
4130 vd->vdev_id == 0 &&
4131 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4132 unspare = B_TRUE;
4133
4134 /*
4135 * Erase the disk labels so the disk can be used for other things.
4136 * This must be done after all other error cases are handled,
4137 * but before we disembowel vd (so we can still do I/O to it).
4138 * But if we can't do it, don't treat the error as fatal --
4139 * it may be that the unwritability of the disk is the reason
4140 * it's being detached!
4141 */
4142 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4143
4144 /*
4145 * Remove vd from its parent and compact the parent's children.
4146 */
4147 vdev_remove_child(pvd, vd);
4148 vdev_compact_children(pvd);
4149
4150 /*
4151 * Remember one of the remaining children so we can get tvd below.
4152 */
4153 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4154
4155 /*
4156 * If we need to remove the remaining child from the list of hot spares,
4157 * do it now, marking the vdev as no longer a spare in the process.
4158 * We must do this before vdev_remove_parent(), because that can
4159 * change the GUID if it creates a new toplevel GUID. For a similar
4160 * reason, we must remove the spare now, in the same txg as the detach;
4161 * otherwise someone could attach a new sibling, change the GUID, and
4162 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4163 */
4164 if (unspare) {
4165 ASSERT(cvd->vdev_isspare);
4166 spa_spare_remove(cvd);
4167 unspare_guid = cvd->vdev_guid;
4168 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4169 cvd->vdev_unspare = B_TRUE;
4170 }
4171
4172 /*
4173 * If the parent mirror/replacing vdev only has one child,
4174 * the parent is no longer needed. Remove it from the tree.
4175 */
4176 if (pvd->vdev_children == 1) {
4177 if (pvd->vdev_ops == &vdev_spare_ops)
4178 cvd->vdev_unspare = B_FALSE;
4179 vdev_remove_parent(cvd);
4180 cvd->vdev_resilvering = B_FALSE;
4181 }
4182
4183
4184 /*
4185 * We don't set tvd until now because the parent we just removed
4186 * may have been the previous top-level vdev.
4187 */
4188 tvd = cvd->vdev_top;
4189 ASSERT(tvd->vdev_parent == rvd);
4190
4191 /*
4192 * Reevaluate the parent vdev state.
4193 */
4194 vdev_propagate_state(cvd);
4195
4196 /*
4197 * If the 'autoexpand' property is set on the pool then automatically
4198 * try to expand the size of the pool. For example if the device we
4199 * just detached was smaller than the others, it may be possible to
4200 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4201 * first so that we can obtain the updated sizes of the leaf vdevs.
4202 */
4203 if (spa->spa_autoexpand) {
4204 vdev_reopen(tvd);
4205 vdev_expand(tvd, txg);
4206 }
4207
4208 vdev_config_dirty(tvd);
4209
4210 /*
4211 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4212 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4213 * But first make sure we're not on any *other* txg's DTL list, to
4214 * prevent vd from being accessed after it's freed.
4215 */
4216 vdpath = spa_strdup(vd->vdev_path);
4217 for (int t = 0; t < TXG_SIZE; t++)
4218 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4219 vd->vdev_detached = B_TRUE;
4220 vdev_dirty(tvd, VDD_DTL, vd, txg);
4221
4222 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4223
4224 /* hang on to the spa before we release the lock */
4225 spa_open_ref(spa, FTAG);
4226
4227 error = spa_vdev_exit(spa, vd, txg, 0);
4228
4229 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4230 "vdev=%s", vdpath);
4231 spa_strfree(vdpath);
4232
4233 /*
4234 * If this was the removal of the original device in a hot spare vdev,
4235 * then we want to go through and remove the device from the hot spare
4236 * list of every other pool.
4237 */
4238 if (unspare) {
4239 spa_t *altspa = NULL;
4240
4241 mutex_enter(&spa_namespace_lock);
4242 while ((altspa = spa_next(altspa)) != NULL) {
4243 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4244 altspa == spa)
4245 continue;
4246
4247 spa_open_ref(altspa, FTAG);
4248 mutex_exit(&spa_namespace_lock);
4249 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4250 mutex_enter(&spa_namespace_lock);
4251 spa_close(altspa, FTAG);
4252 }
4253 mutex_exit(&spa_namespace_lock);
4254
4255 /* search the rest of the vdevs for spares to remove */
4256 spa_vdev_resilver_done(spa);
4257 }
4258
4259 /* all done with the spa; OK to release */
4260 mutex_enter(&spa_namespace_lock);
4261 spa_close(spa, FTAG);
4262 mutex_exit(&spa_namespace_lock);
4263
4264 return (error);
4265}
4266
4267/*
4268 * Split a set of devices from their mirrors, and create a new pool from them.
4269 */
4270int
4271spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4272 nvlist_t *props, boolean_t exp)
4273{
4274 int error = 0;
4275 uint64_t txg, *glist;
4276 spa_t *newspa;
4277 uint_t c, children, lastlog;
4278 nvlist_t **child, *nvl, *tmp;
4279 dmu_tx_t *tx;
4280 char *altroot = NULL;
4281 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4282 boolean_t activate_slog;
4283
4284 ASSERT(spa_writeable(spa));
4285
4286 txg = spa_vdev_enter(spa);
4287
4288 /* clear the log and flush everything up to now */
4289 activate_slog = spa_passivate_log(spa);
4290 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4291 error = spa_offline_log(spa);
4292 txg = spa_vdev_config_enter(spa);
4293
4294 if (activate_slog)
4295 spa_activate_log(spa);
4296
4297 if (error != 0)
4298 return (spa_vdev_exit(spa, NULL, txg, error));
4299
4300 /* check new spa name before going any further */
4301 if (spa_lookup(newname) != NULL)
4302 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4303
4304 /*
4305 * scan through all the children to ensure they're all mirrors
4306 */
4307 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4308 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4309 &children) != 0)
4310 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4311
4312 /* first, check to ensure we've got the right child count */
4313 rvd = spa->spa_root_vdev;
4314 lastlog = 0;
4315 for (c = 0; c < rvd->vdev_children; c++) {
4316 vdev_t *vd = rvd->vdev_child[c];
4317
4318 /* don't count the holes & logs as children */
4319 if (vd->vdev_islog || vd->vdev_ishole) {
4320 if (lastlog == 0)
4321 lastlog = c;
4322 continue;
4323 }
4324
4325 lastlog = 0;
4326 }
4327 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4328 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4329
4330 /* next, ensure no spare or cache devices are part of the split */
4331 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4332 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4333 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4334
4335 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
4336 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
4337
4338 /* then, loop over each vdev and validate it */
4339 for (c = 0; c < children; c++) {
4340 uint64_t is_hole = 0;
4341
4342 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4343 &is_hole);
4344
4345 if (is_hole != 0) {
4346 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4347 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4348 continue;
4349 } else {
4350 error = EINVAL;
4351 break;
4352 }
4353 }
4354
4355 /* which disk is going to be split? */
4356 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4357 &glist[c]) != 0) {
4358 error = EINVAL;
4359 break;
4360 }
4361
4362 /* look it up in the spa */
4363 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4364 if (vml[c] == NULL) {
4365 error = ENODEV;
4366 break;
4367 }
4368
4369 /* make sure there's nothing stopping the split */
4370 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4371 vml[c]->vdev_islog ||
4372 vml[c]->vdev_ishole ||
4373 vml[c]->vdev_isspare ||
4374 vml[c]->vdev_isl2cache ||
4375 !vdev_writeable(vml[c]) ||
4376 vml[c]->vdev_children != 0 ||
4377 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4378 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4379 error = EINVAL;
4380 break;
4381 }
4382
4383 if (vdev_dtl_required(vml[c])) {
4384 error = EBUSY;
4385 break;
4386 }
4387
4388 /* we need certain info from the top level */
4389 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4390 vml[c]->vdev_top->vdev_ms_array) == 0);
4391 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4392 vml[c]->vdev_top->vdev_ms_shift) == 0);
4393 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4394 vml[c]->vdev_top->vdev_asize) == 0);
4395 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4396 vml[c]->vdev_top->vdev_ashift) == 0);
4397 }
4398
4399 if (error != 0) {
4400 kmem_free(vml, children * sizeof (vdev_t *));
4401 kmem_free(glist, children * sizeof (uint64_t));
4402 return (spa_vdev_exit(spa, NULL, txg, error));
4403 }
4404
4405 /* stop writers from using the disks */
4406 for (c = 0; c < children; c++) {
4407 if (vml[c] != NULL)
4408 vml[c]->vdev_offline = B_TRUE;
4409 }
4410 vdev_reopen(spa->spa_root_vdev);
4411
4412 /*
4413 * Temporarily record the splitting vdevs in the spa config. This
4414 * will disappear once the config is regenerated.
4415 */
4416 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4417 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4418 glist, children) == 0);
4419 kmem_free(glist, children * sizeof (uint64_t));
4420
4421 mutex_enter(&spa->spa_props_lock);
4422 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4423 nvl) == 0);
4424 mutex_exit(&spa->spa_props_lock);
4425 spa->spa_config_splitting = nvl;
4426 vdev_config_dirty(spa->spa_root_vdev);
4427
4428 /* configure and create the new pool */
4429 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4430 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4431 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4432 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4433 spa_version(spa)) == 0);
4434 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4435 spa->spa_config_txg) == 0);
4436 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4437 spa_generate_guid(NULL)) == 0);
4438 (void) nvlist_lookup_string(props,
4439 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4440
4441 /* add the new pool to the namespace */
4442 newspa = spa_add(newname, config, altroot);
4443 newspa->spa_config_txg = spa->spa_config_txg;
4444 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4445
4446 /* release the spa config lock, retaining the namespace lock */
4447 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4448
4449 if (zio_injection_enabled)
4450 zio_handle_panic_injection(spa, FTAG, 1);
4451
4452 spa_activate(newspa, spa_mode_global);
4453 spa_async_suspend(newspa);
4454
4455#ifndef sun
4456 /* mark that we are creating new spa by splitting */
4457 newspa->spa_splitting_newspa = B_TRUE;
4458#endif
4459 /* create the new pool from the disks of the original pool */
4460 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4461#ifndef sun
4462 newspa->spa_splitting_newspa = B_FALSE;
4463#endif
4464 if (error)
4465 goto out;
4466
4467 /* if that worked, generate a real config for the new pool */
4468 if (newspa->spa_root_vdev != NULL) {
4469 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4470 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4471 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4472 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4473 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4474 B_TRUE));
4475 }
4476
4477 /* set the props */
4478 if (props != NULL) {
4479 spa_configfile_set(newspa, props, B_FALSE);
4480 error = spa_prop_set(newspa, props);
4481 if (error)
4482 goto out;
4483 }
4484
4485 /* flush everything */
4486 txg = spa_vdev_config_enter(newspa);
4487 vdev_config_dirty(newspa->spa_root_vdev);
4488 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4489
4490 if (zio_injection_enabled)
4491 zio_handle_panic_injection(spa, FTAG, 2);
4492
4493 spa_async_resume(newspa);
4494
4495 /* finally, update the original pool's config */
4496 txg = spa_vdev_config_enter(spa);
4497 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4498 error = dmu_tx_assign(tx, TXG_WAIT);
4499 if (error != 0)
4500 dmu_tx_abort(tx);
4501 for (c = 0; c < children; c++) {
4502 if (vml[c] != NULL) {
4503 vdev_split(vml[c]);
4504 if (error == 0)
4505 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4506 spa, tx, "vdev=%s",
4507 vml[c]->vdev_path);
4508 vdev_free(vml[c]);
4509 }
4510 }
4511 vdev_config_dirty(spa->spa_root_vdev);
4512 spa->spa_config_splitting = NULL;
4513 nvlist_free(nvl);
4514 if (error == 0)
4515 dmu_tx_commit(tx);
4516 (void) spa_vdev_exit(spa, NULL, txg, 0);
4517
4518 if (zio_injection_enabled)
4519 zio_handle_panic_injection(spa, FTAG, 3);
4520
4521 /* split is complete; log a history record */
4522 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4523 "split new pool %s from pool %s", newname, spa_name(spa));
4524
4525 kmem_free(vml, children * sizeof (vdev_t *));
4526
4527 /* if we're not going to mount the filesystems in userland, export */
4528 if (exp)
4529 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4530 B_FALSE, B_FALSE);
4531
4532 return (error);
4533
4534out:
4535 spa_unload(newspa);
4536 spa_deactivate(newspa);
4537 spa_remove(newspa);
4538
4539 txg = spa_vdev_config_enter(spa);
4540
4541 /* re-online all offlined disks */
4542 for (c = 0; c < children; c++) {
4543 if (vml[c] != NULL)
4544 vml[c]->vdev_offline = B_FALSE;
4545 }
4546 vdev_reopen(spa->spa_root_vdev);
4547
4548 nvlist_free(spa->spa_config_splitting);
4549 spa->spa_config_splitting = NULL;
4550 (void) spa_vdev_exit(spa, NULL, txg, error);
4551
4552 kmem_free(vml, children * sizeof (vdev_t *));
4553 return (error);
4554}
4555
4556static nvlist_t *
4557spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4558{
4559 for (int i = 0; i < count; i++) {
4560 uint64_t guid;
4561
4562 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4563 &guid) == 0);
4564
4565 if (guid == target_guid)
4566 return (nvpp[i]);
4567 }
4568
4569 return (NULL);
4570}
4571
4572static void
4573spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4574 nvlist_t *dev_to_remove)
4575{
4576 nvlist_t **newdev = NULL;
4577
4578 if (count > 1)
4579 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
4580
4581 for (int i = 0, j = 0; i < count; i++) {
4582 if (dev[i] == dev_to_remove)
4583 continue;
4584 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
4585 }
4586
4587 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
4588 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
4589
4590 for (int i = 0; i < count - 1; i++)
4591 nvlist_free(newdev[i]);
4592
4593 if (count > 1)
4594 kmem_free(newdev, (count - 1) * sizeof (void *));
4595}
4596
4597/*
4598 * Evacuate the device.
4599 */
4600static int
4601spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
4602{
4603 uint64_t txg;
4604 int error = 0;
4605
4606 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4607 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4608 ASSERT(vd == vd->vdev_top);
4609
4610 /*
4611 * Evacuate the device. We don't hold the config lock as writer
4612 * since we need to do I/O but we do keep the
4613 * spa_namespace_lock held. Once this completes the device
4614 * should no longer have any blocks allocated on it.
4615 */
4616 if (vd->vdev_islog) {
4617 if (vd->vdev_stat.vs_alloc != 0)
4618 error = spa_offline_log(spa);
4619 } else {
4620 error = ENOTSUP;
4621 }
4622
4623 if (error)
4624 return (error);
4625
4626 /*
4627 * The evacuation succeeded. Remove any remaining MOS metadata
4628 * associated with this vdev, and wait for these changes to sync.
4629 */
4630 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
4631 txg = spa_vdev_config_enter(spa);
4632 vd->vdev_removing = B_TRUE;
4633 vdev_dirty(vd, 0, NULL, txg);
4634 vdev_config_dirty(vd);
4635 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4636
4637 return (0);
4638}
4639
4640/*
4641 * Complete the removal by cleaning up the namespace.
4642 */
4643static void
4644spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
4645{
4646 vdev_t *rvd = spa->spa_root_vdev;
4647 uint64_t id = vd->vdev_id;
4648 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
4649
4650 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4651 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4652 ASSERT(vd == vd->vdev_top);
4653
4654 /*
4655 * Only remove any devices which are empty.
4656 */
4657 if (vd->vdev_stat.vs_alloc != 0)
4658 return;
4659
4660 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4661
4662 if (list_link_active(&vd->vdev_state_dirty_node))
4663 vdev_state_clean(vd);
4664 if (list_link_active(&vd->vdev_config_dirty_node))
4665 vdev_config_clean(vd);
4666
4667 vdev_free(vd);
4668
4669 if (last_vdev) {
4670 vdev_compact_children(rvd);
4671 } else {
4672 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
4673 vdev_add_child(rvd, vd);
4674 }
4675 vdev_config_dirty(rvd);
4676
4677 /*
4678 * Reassess the health of our root vdev.
4679 */
4680 vdev_reopen(rvd);
4681}
4682
4683/*
4684 * Remove a device from the pool -
4685 *
4686 * Removing a device from the vdev namespace requires several steps
4687 * and can take a significant amount of time. As a result we use
4688 * the spa_vdev_config_[enter/exit] functions which allow us to
4689 * grab and release the spa_config_lock while still holding the namespace
4690 * lock. During each step the configuration is synced out.
4691 */
4692
4693/*
4694 * Remove a device from the pool. Currently, this supports removing only hot
4695 * spares, slogs, and level 2 ARC devices.
4696 */
4697int
4698spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
4699{
4700 vdev_t *vd;
4701 metaslab_group_t *mg;
4702 nvlist_t **spares, **l2cache, *nv;
4703 uint64_t txg = 0;
4704 uint_t nspares, nl2cache;
4705 int error = 0;
4706 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
4707
4708 ASSERT(spa_writeable(spa));
4709
4710 if (!locked)
4711 txg = spa_vdev_enter(spa);
4712
4713 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4714
4715 if (spa->spa_spares.sav_vdevs != NULL &&
4716 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
4717 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
4718 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
4719 /*
4720 * Only remove the hot spare if it's not currently in use
4721 * in this pool.
4722 */
4723 if (vd == NULL || unspare) {
4724 spa_vdev_remove_aux(spa->spa_spares.sav_config,
4725 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
4726 spa_load_spares(spa);
4727 spa->spa_spares.sav_sync = B_TRUE;
4728 } else {
4729 error = EBUSY;
4730 }
4731 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
4732 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4733 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
4734 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
4735 /*
4736 * Cache devices can always be removed.
4737 */
4738 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
4739 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
4740 spa_load_l2cache(spa);
4741 spa->spa_l2cache.sav_sync = B_TRUE;
4742 } else if (vd != NULL && vd->vdev_islog) {
4743 ASSERT(!locked);
4744 ASSERT(vd == vd->vdev_top);
4745
4746 /*
4747 * XXX - Once we have bp-rewrite this should
4748 * become the common case.
4749 */
4750
4751 mg = vd->vdev_mg;
4752
4753 /*
4754 * Stop allocating from this vdev.
4755 */
4756 metaslab_group_passivate(mg);
4757
4758 /*
4759 * Wait for the youngest allocations and frees to sync,
4760 * and then wait for the deferral of those frees to finish.
4761 */
4762 spa_vdev_config_exit(spa, NULL,
4763 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
4764
4765 /*
4766 * Attempt to evacuate the vdev.
4767 */
4768 error = spa_vdev_remove_evacuate(spa, vd);
4769
4770 txg = spa_vdev_config_enter(spa);
4771
4772 /*
4773 * If we couldn't evacuate the vdev, unwind.
4774 */
4775 if (error) {
4776 metaslab_group_activate(mg);
4777 return (spa_vdev_exit(spa, NULL, txg, error));
4778 }
4779
4780 /*
4781 * Clean up the vdev namespace.
4782 */
4783 spa_vdev_remove_from_namespace(spa, vd);
4784
4785 } else if (vd != NULL) {
4786 /*
4787 * Normal vdevs cannot be removed (yet).
4788 */
4789 error = ENOTSUP;
4790 } else {
4791 /*
4792 * There is no vdev of any kind with the specified guid.
4793 */
4794 error = ENOENT;
4795 }
4796
4797 if (!locked)
4798 return (spa_vdev_exit(spa, NULL, txg, error));
4799
4800 return (error);
4801}
4802
4803/*
4804 * Find any device that's done replacing, or a vdev marked 'unspare' that's
4805 * current spared, so we can detach it.
4806 */
4807static vdev_t *
4808spa_vdev_resilver_done_hunt(vdev_t *vd)
4809{
4810 vdev_t *newvd, *oldvd;
4811
4812 for (int c = 0; c < vd->vdev_children; c++) {
4813 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
4814 if (oldvd != NULL)
4815 return (oldvd);
4816 }
4817
4818 /*
4819 * Check for a completed replacement. We always consider the first
4820 * vdev in the list to be the oldest vdev, and the last one to be
4821 * the newest (see spa_vdev_attach() for how that works). In
4822 * the case where the newest vdev is faulted, we will not automatically
4823 * remove it after a resilver completes. This is OK as it will require
4824 * user intervention to determine which disk the admin wishes to keep.
4825 */
4826 if (vd->vdev_ops == &vdev_replacing_ops) {
4827 ASSERT(vd->vdev_children > 1);
4828
4829 newvd = vd->vdev_child[vd->vdev_children - 1];
4830 oldvd = vd->vdev_child[0];
4831
4832 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
4833 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4834 !vdev_dtl_required(oldvd))
4835 return (oldvd);
4836 }
4837
4838 /*
4839 * Check for a completed resilver with the 'unspare' flag set.
4840 */
4841 if (vd->vdev_ops == &vdev_spare_ops) {
4842 vdev_t *first = vd->vdev_child[0];
4843 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
4844
4845 if (last->vdev_unspare) {
4846 oldvd = first;
4847 newvd = last;
4848 } else if (first->vdev_unspare) {
4849 oldvd = last;
4850 newvd = first;
4851 } else {
4852 oldvd = NULL;
4853 }
4854
4855 if (oldvd != NULL &&
4856 vdev_dtl_empty(newvd, DTL_MISSING) &&
4857 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4858 !vdev_dtl_required(oldvd))
4859 return (oldvd);
4860
4861 /*
4862 * If there are more than two spares attached to a disk,
4863 * and those spares are not required, then we want to
4864 * attempt to free them up now so that they can be used
4865 * by other pools. Once we're back down to a single
4866 * disk+spare, we stop removing them.
4867 */
4868 if (vd->vdev_children > 2) {
4869 newvd = vd->vdev_child[1];
4870
4871 if (newvd->vdev_isspare && last->vdev_isspare &&
4872 vdev_dtl_empty(last, DTL_MISSING) &&
4873 vdev_dtl_empty(last, DTL_OUTAGE) &&
4874 !vdev_dtl_required(newvd))
4875 return (newvd);
4876 }
4877 }
4878
4879 return (NULL);
4880}
4881
4882static void
4883spa_vdev_resilver_done(spa_t *spa)
4884{
4885 vdev_t *vd, *pvd, *ppvd;
4886 uint64_t guid, sguid, pguid, ppguid;
4887
4888 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4889
4890 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
4891 pvd = vd->vdev_parent;
4892 ppvd = pvd->vdev_parent;
4893 guid = vd->vdev_guid;
4894 pguid = pvd->vdev_guid;
4895 ppguid = ppvd->vdev_guid;
4896 sguid = 0;
4897 /*
4898 * If we have just finished replacing a hot spared device, then
4899 * we need to detach the parent's first child (the original hot
4900 * spare) as well.
4901 */
4902 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
4903 ppvd->vdev_children == 2) {
4904 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
4905 sguid = ppvd->vdev_child[1]->vdev_guid;
4906 }
4907 spa_config_exit(spa, SCL_ALL, FTAG);
4908 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
4909 return;
4910 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
4911 return;
4912 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4913 }
4914
4915 spa_config_exit(spa, SCL_ALL, FTAG);
4916}
4917
4918/*
4919 * Update the stored path or FRU for this vdev.
4920 */
4921int
4922spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
4923 boolean_t ispath)
4924{
4925 vdev_t *vd;
4926 boolean_t sync = B_FALSE;
4927
4928 ASSERT(spa_writeable(spa));
4929
4930 spa_vdev_state_enter(spa, SCL_ALL);
4931
4932 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4933 return (spa_vdev_state_exit(spa, NULL, ENOENT));
4934
4935 if (!vd->vdev_ops->vdev_op_leaf)
4936 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4937
4938 if (ispath) {
4939 if (strcmp(value, vd->vdev_path) != 0) {
4940 spa_strfree(vd->vdev_path);
4941 vd->vdev_path = spa_strdup(value);
4942 sync = B_TRUE;
4943 }
4944 } else {
4945 if (vd->vdev_fru == NULL) {
4946 vd->vdev_fru = spa_strdup(value);
4947 sync = B_TRUE;
4948 } else if (strcmp(value, vd->vdev_fru) != 0) {
4949 spa_strfree(vd->vdev_fru);
4950 vd->vdev_fru = spa_strdup(value);
4951 sync = B_TRUE;
4952 }
4953 }
4954
4955 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
4956}
4957
4958int
4959spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
4960{
4961 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
4962}
4963
4964int
4965spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
4966{
4967 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
4968}
4969
4970/*
4971 * ==========================================================================
4972 * SPA Scanning
4973 * ==========================================================================
4974 */
4975
4976int
4977spa_scan_stop(spa_t *spa)
4978{
4979 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4980 if (dsl_scan_resilvering(spa->spa_dsl_pool))
4981 return (EBUSY);
4982 return (dsl_scan_cancel(spa->spa_dsl_pool));
4983}
4984
4985int
4986spa_scan(spa_t *spa, pool_scan_func_t func)
4987{
4988 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4989
4990 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
4991 return (ENOTSUP);
4992
4993 /*
4994 * If a resilver was requested, but there is no DTL on a
4995 * writeable leaf device, we have nothing to do.
4996 */
4997 if (func == POOL_SCAN_RESILVER &&
4998 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
4999 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5000 return (0);
5001 }
5002
5003 return (dsl_scan(spa->spa_dsl_pool, func));
5004}
5005
5006/*
5007 * ==========================================================================
5008 * SPA async task processing
5009 * ==========================================================================
5010 */
5011
5012static void
5013spa_async_remove(spa_t *spa, vdev_t *vd)
5014{
5015 if (vd->vdev_remove_wanted) {
5016 vd->vdev_remove_wanted = B_FALSE;
5017 vd->vdev_delayed_close = B_FALSE;
5018 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5019
5020 /*
5021 * We want to clear the stats, but we don't want to do a full
5022 * vdev_clear() as that will cause us to throw away
5023 * degraded/faulted state as well as attempt to reopen the
5024 * device, all of which is a waste.
5025 */
5026 vd->vdev_stat.vs_read_errors = 0;
5027 vd->vdev_stat.vs_write_errors = 0;
5028 vd->vdev_stat.vs_checksum_errors = 0;
5029
5030 vdev_state_dirty(vd->vdev_top);
5031 }
5032
5033 for (int c = 0; c < vd->vdev_children; c++)
5034 spa_async_remove(spa, vd->vdev_child[c]);
5035}
5036
5037static void
5038spa_async_probe(spa_t *spa, vdev_t *vd)
5039{
5040 if (vd->vdev_probe_wanted) {
5041 vd->vdev_probe_wanted = B_FALSE;
5042 vdev_reopen(vd); /* vdev_open() does the actual probe */
5043 }
5044
5045 for (int c = 0; c < vd->vdev_children; c++)
5046 spa_async_probe(spa, vd->vdev_child[c]);
5047}
5048
5049static void
5050spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5051{
5052 sysevent_id_t eid;
5053 nvlist_t *attr;
5054 char *physpath;
5055
5056 if (!spa->spa_autoexpand)
5057 return;
5058
5059 for (int c = 0; c < vd->vdev_children; c++) {
5060 vdev_t *cvd = vd->vdev_child[c];
5061 spa_async_autoexpand(spa, cvd);
5062 }
5063
5064 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5065 return;
5066
5067 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5068 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5069
5070 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5071 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5072
5073 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5074 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5075
5076 nvlist_free(attr);
5077 kmem_free(physpath, MAXPATHLEN);
5078}
5079
5080static void
5081spa_async_thread(void *arg)
5082{
5083 spa_t *spa = arg;
5084 int tasks;
5085
5086 ASSERT(spa->spa_sync_on);
5087
5088 mutex_enter(&spa->spa_async_lock);
5089 tasks = spa->spa_async_tasks;
5090 spa->spa_async_tasks = 0;
5091 mutex_exit(&spa->spa_async_lock);
5092
5093 /*
5094 * See if the config needs to be updated.
5095 */
5096 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5097 uint64_t old_space, new_space;
5098
5099 mutex_enter(&spa_namespace_lock);
5100 old_space = metaslab_class_get_space(spa_normal_class(spa));
5101 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5102 new_space = metaslab_class_get_space(spa_normal_class(spa));
5103 mutex_exit(&spa_namespace_lock);
5104
5105 /*
5106 * If the pool grew as a result of the config update,
5107 * then log an internal history event.
5108 */
5109 if (new_space != old_space) {
5110 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5111 spa, NULL,
5112 "pool '%s' size: %llu(+%llu)",
5113 spa_name(spa), new_space, new_space - old_space);
5114 }
5115 }
5116
5117 /*
5118 * See if any devices need to be marked REMOVED.
5119 */
5120 if (tasks & SPA_ASYNC_REMOVE) {
5121 spa_vdev_state_enter(spa, SCL_NONE);
5122 spa_async_remove(spa, spa->spa_root_vdev);
5123 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5124 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5125 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5126 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5127 (void) spa_vdev_state_exit(spa, NULL, 0);
5128 }
5129
5130 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5131 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5132 spa_async_autoexpand(spa, spa->spa_root_vdev);
5133 spa_config_exit(spa, SCL_CONFIG, FTAG);
5134 }
5135
5136 /*
5137 * See if any devices need to be probed.
5138 */
5139 if (tasks & SPA_ASYNC_PROBE) {
5140 spa_vdev_state_enter(spa, SCL_NONE);
5141 spa_async_probe(spa, spa->spa_root_vdev);
5142 (void) spa_vdev_state_exit(spa, NULL, 0);
5143 }
5144
5145 /*
5146 * If any devices are done replacing, detach them.
5147 */
5148 if (tasks & SPA_ASYNC_RESILVER_DONE)
5149 spa_vdev_resilver_done(spa);
5150
5151 /*
5152 * Kick off a resilver.
5153 */
5154 if (tasks & SPA_ASYNC_RESILVER)
5155 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5156
5157 /*
5158 * Let the world know that we're done.
5159 */
5160 mutex_enter(&spa->spa_async_lock);
5161 spa->spa_async_thread = NULL;
5162 cv_broadcast(&spa->spa_async_cv);
5163 mutex_exit(&spa->spa_async_lock);
5164 thread_exit();
5165}
5166
5167void
5168spa_async_suspend(spa_t *spa)
5169{
5170 mutex_enter(&spa->spa_async_lock);
5171 spa->spa_async_suspended++;
5172 while (spa->spa_async_thread != NULL)
5173 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5174 mutex_exit(&spa->spa_async_lock);
5175}
5176
5177void
5178spa_async_resume(spa_t *spa)
5179{
5180 mutex_enter(&spa->spa_async_lock);
5181 ASSERT(spa->spa_async_suspended != 0);
5182 spa->spa_async_suspended--;
5183 mutex_exit(&spa->spa_async_lock);
5184}
5185
5186static void
5187spa_async_dispatch(spa_t *spa)
5188{
5189 mutex_enter(&spa->spa_async_lock);
5190 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5191 spa->spa_async_thread == NULL &&
5192 rootdir != NULL && !vn_is_readonly(rootdir))
5193 spa->spa_async_thread = thread_create(NULL, 0,
5194 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5195 mutex_exit(&spa->spa_async_lock);
5196}
5197
5198void
5199spa_async_request(spa_t *spa, int task)
5200{
5201 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5202 mutex_enter(&spa->spa_async_lock);
5203 spa->spa_async_tasks |= task;
5204 mutex_exit(&spa->spa_async_lock);
5205}
5206
5207/*
5208 * ==========================================================================
5209 * SPA syncing routines
5210 * ==========================================================================
5211 */
5212
5213static int
5214bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5215{
5216 bpobj_t *bpo = arg;
5217 bpobj_enqueue(bpo, bp, tx);
5218 return (0);
5219}
5220
5221static int
5222spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5223{
5224 zio_t *zio = arg;
5225
5226 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5227 zio->io_flags));
5228 return (0);
5229}
5230
5231static void
5232spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5233{
5234 char *packed = NULL;
5235 size_t bufsize;
5236 size_t nvsize = 0;
5237 dmu_buf_t *db;
5238
5239 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5240
5241 /*
5242 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5243 * information. This avoids the dbuf_will_dirty() path and
5244 * saves us a pre-read to get data we don't actually care about.
5245 */
5246 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
5247 packed = kmem_alloc(bufsize, KM_SLEEP);
5248
5249 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5250 KM_SLEEP) == 0);
5251 bzero(packed + nvsize, bufsize - nvsize);
5252
5253 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5254
5255 kmem_free(packed, bufsize);
5256
5257 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5258 dmu_buf_will_dirty(db, tx);
5259 *(uint64_t *)db->db_data = nvsize;
5260 dmu_buf_rele(db, FTAG);
5261}
5262
5263static void
5264spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5265 const char *config, const char *entry)
5266{
5267 nvlist_t *nvroot;
5268 nvlist_t **list;
5269 int i;
5270
5271 if (!sav->sav_sync)
5272 return;
5273
5274 /*
5275 * Update the MOS nvlist describing the list of available devices.
5276 * spa_validate_aux() will have already made sure this nvlist is
5277 * valid and the vdevs are labeled appropriately.
5278 */
5279 if (sav->sav_object == 0) {
5280 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5281 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5282 sizeof (uint64_t), tx);
5283 VERIFY(zap_update(spa->spa_meta_objset,
5284 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5285 &sav->sav_object, tx) == 0);
5286 }
5287
5288 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5289 if (sav->sav_count == 0) {
5290 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5291 } else {
5292 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5293 for (i = 0; i < sav->sav_count; i++)
5294 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5295 B_FALSE, VDEV_CONFIG_L2CACHE);
5296 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5297 sav->sav_count) == 0);
5298 for (i = 0; i < sav->sav_count; i++)
5299 nvlist_free(list[i]);
5300 kmem_free(list, sav->sav_count * sizeof (void *));
5301 }
5302
5303 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5304 nvlist_free(nvroot);
5305
5306 sav->sav_sync = B_FALSE;
5307}
5308
5309static void
5310spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5311{
5312 nvlist_t *config;
5313
5314 if (list_is_empty(&spa->spa_config_dirty_list))
5315 return;
5316
5317 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5318
5319 config = spa_config_generate(spa, spa->spa_root_vdev,
5320 dmu_tx_get_txg(tx), B_FALSE);
5321
5322 spa_config_exit(spa, SCL_STATE, FTAG);
5323
5324 if (spa->spa_config_syncing)
5325 nvlist_free(spa->spa_config_syncing);
5326 spa->spa_config_syncing = config;
5327
5328 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5329}
5330
5331/*
5332 * Set zpool properties.
5333 */
5334static void
5335spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5336{
5337 spa_t *spa = arg1;
5338 objset_t *mos = spa->spa_meta_objset;
5339 nvlist_t *nvp = arg2;
5340 nvpair_t *elem;
5341 uint64_t intval;
5342 char *strval;
5343 zpool_prop_t prop;
5344 const char *propname;
5345 zprop_type_t proptype;
5346
5347 mutex_enter(&spa->spa_props_lock);
5348
5349 elem = NULL;
5350 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5351 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
5352 case ZPOOL_PROP_VERSION:
5353 /*
5354 * Only set version for non-zpool-creation cases
5355 * (set/import). spa_create() needs special care
5356 * for version setting.
5357 */
5358 if (tx->tx_txg != TXG_INITIAL) {
5359 VERIFY(nvpair_value_uint64(elem,
5360 &intval) == 0);
5361 ASSERT(intval <= SPA_VERSION);
5362 ASSERT(intval >= spa_version(spa));
5363 spa->spa_uberblock.ub_version = intval;
5364 vdev_config_dirty(spa->spa_root_vdev);
5365 }
5366 break;
5367
5368 case ZPOOL_PROP_ALTROOT:
5369 /*
5370 * 'altroot' is a non-persistent property. It should
5371 * have been set temporarily at creation or import time.
5372 */
5373 ASSERT(spa->spa_root != NULL);
5374 break;
5375
5376 case ZPOOL_PROP_READONLY:
5377 case ZPOOL_PROP_CACHEFILE:
5378 /*
5379 * 'readonly' and 'cachefile' are also non-persisitent
5380 * properties.
5381 */
5382 break;
1841
1842 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1843 &nvl) == 0) {
1844 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1845 KM_SLEEP) == 0);
1846 }
1847
1848 gethrestime(&spa->spa_loaded_ts);
1849 error = spa_load_impl(spa, pool_guid, config, state, type,
1850 mosconfig, &ereport);
1851 }
1852
1853 spa->spa_minref = refcount_count(&spa->spa_refcount);
1854 if (error) {
1855 if (error != EEXIST) {
1856 spa->spa_loaded_ts.tv_sec = 0;
1857 spa->spa_loaded_ts.tv_nsec = 0;
1858 }
1859 if (error != EBADF) {
1860 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1861 }
1862 }
1863 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1864 spa->spa_ena = 0;
1865
1866 return (error);
1867}
1868
1869/*
1870 * Load an existing storage pool, using the pool's builtin spa_config as a
1871 * source of configuration information.
1872 */
1873static int
1874spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
1875 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1876 char **ereport)
1877{
1878 int error = 0;
1879 nvlist_t *nvroot = NULL;
1880 vdev_t *rvd;
1881 uberblock_t *ub = &spa->spa_uberblock;
1882 uint64_t children, config_cache_txg = spa->spa_config_txg;
1883 int orig_mode = spa->spa_mode;
1884 int parse;
1885 uint64_t obj;
1886
1887 /*
1888 * If this is an untrusted config, access the pool in read-only mode.
1889 * This prevents things like resilvering recently removed devices.
1890 */
1891 if (!mosconfig)
1892 spa->spa_mode = FREAD;
1893
1894 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1895
1896 spa->spa_load_state = state;
1897
1898 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
1899 return (EINVAL);
1900
1901 parse = (type == SPA_IMPORT_EXISTING ?
1902 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
1903
1904 /*
1905 * Create "The Godfather" zio to hold all async IOs
1906 */
1907 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1908 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
1909
1910 /*
1911 * Parse the configuration into a vdev tree. We explicitly set the
1912 * value that will be returned by spa_version() since parsing the
1913 * configuration requires knowing the version number.
1914 */
1915 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1916 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
1917 spa_config_exit(spa, SCL_ALL, FTAG);
1918
1919 if (error != 0)
1920 return (error);
1921
1922 ASSERT(spa->spa_root_vdev == rvd);
1923
1924 if (type != SPA_IMPORT_ASSEMBLE) {
1925 ASSERT(spa_guid(spa) == pool_guid);
1926 }
1927
1928 /*
1929 * Try to open all vdevs, loading each label in the process.
1930 */
1931 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1932 error = vdev_open(rvd);
1933 spa_config_exit(spa, SCL_ALL, FTAG);
1934 if (error != 0)
1935 return (error);
1936
1937 /*
1938 * We need to validate the vdev labels against the configuration that
1939 * we have in hand, which is dependent on the setting of mosconfig. If
1940 * mosconfig is true then we're validating the vdev labels based on
1941 * that config. Otherwise, we're validating against the cached config
1942 * (zpool.cache) that was read when we loaded the zfs module, and then
1943 * later we will recursively call spa_load() and validate against
1944 * the vdev config.
1945 *
1946 * If we're assembling a new pool that's been split off from an
1947 * existing pool, the labels haven't yet been updated so we skip
1948 * validation for now.
1949 */
1950 if (type != SPA_IMPORT_ASSEMBLE) {
1951 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1952 error = vdev_validate(rvd);
1953 spa_config_exit(spa, SCL_ALL, FTAG);
1954
1955 if (error != 0)
1956 return (error);
1957
1958 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
1959 return (ENXIO);
1960 }
1961
1962 /*
1963 * Find the best uberblock.
1964 */
1965 vdev_uberblock_load(NULL, rvd, ub);
1966
1967 /*
1968 * If we weren't able to find a single valid uberblock, return failure.
1969 */
1970 if (ub->ub_txg == 0)
1971 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
1972
1973 /*
1974 * If the pool is newer than the code, we can't open it.
1975 */
1976 if (ub->ub_version > SPA_VERSION)
1977 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1978
1979 /*
1980 * If the vdev guid sum doesn't match the uberblock, we have an
1981 * incomplete configuration. We first check to see if the pool
1982 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1983 * If it is, defer the vdev_guid_sum check till later so we
1984 * can handle missing vdevs.
1985 */
1986 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
1987 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
1988 rvd->vdev_guid_sum != ub->ub_guid_sum)
1989 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
1990
1991 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
1992 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1993 spa_try_repair(spa, config);
1994 spa_config_exit(spa, SCL_ALL, FTAG);
1995 nvlist_free(spa->spa_config_splitting);
1996 spa->spa_config_splitting = NULL;
1997 }
1998
1999 /*
2000 * Initialize internal SPA structures.
2001 */
2002 spa->spa_state = POOL_STATE_ACTIVE;
2003 spa->spa_ubsync = spa->spa_uberblock;
2004 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2005 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2006 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2007 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2008 spa->spa_claim_max_txg = spa->spa_first_txg;
2009 spa->spa_prev_software_version = ub->ub_software_version;
2010
2011 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2012 if (error)
2013 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2014 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2015
2016 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2017 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2018
2019 if (!mosconfig) {
2020 uint64_t hostid;
2021 nvlist_t *policy = NULL, *nvconfig;
2022
2023 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2024 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2025
2026 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2027 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2028 char *hostname;
2029 unsigned long myhostid = 0;
2030
2031 VERIFY(nvlist_lookup_string(nvconfig,
2032 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2033
2034#ifdef _KERNEL
2035 myhostid = zone_get_hostid(NULL);
2036#else /* _KERNEL */
2037 /*
2038 * We're emulating the system's hostid in userland, so
2039 * we can't use zone_get_hostid().
2040 */
2041 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2042#endif /* _KERNEL */
2043 if (check_hostid && hostid != 0 && myhostid != 0 &&
2044 hostid != myhostid) {
2045 nvlist_free(nvconfig);
2046 cmn_err(CE_WARN, "pool '%s' could not be "
2047 "loaded as it was last accessed by "
2048 "another system (host: %s hostid: 0x%lx). "
2049 "See: http://www.sun.com/msg/ZFS-8000-EY",
2050 spa_name(spa), hostname,
2051 (unsigned long)hostid);
2052 return (EBADF);
2053 }
2054 }
2055 if (nvlist_lookup_nvlist(spa->spa_config,
2056 ZPOOL_REWIND_POLICY, &policy) == 0)
2057 VERIFY(nvlist_add_nvlist(nvconfig,
2058 ZPOOL_REWIND_POLICY, policy) == 0);
2059
2060 spa_config_set(spa, nvconfig);
2061 spa_unload(spa);
2062 spa_deactivate(spa);
2063 spa_activate(spa, orig_mode);
2064
2065 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2066 }
2067
2068 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2069 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2070 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2071 if (error != 0)
2072 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2073
2074 /*
2075 * Load the bit that tells us to use the new accounting function
2076 * (raid-z deflation). If we have an older pool, this will not
2077 * be present.
2078 */
2079 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2080 if (error != 0 && error != ENOENT)
2081 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2082
2083 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2084 &spa->spa_creation_version);
2085 if (error != 0 && error != ENOENT)
2086 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2087
2088 /*
2089 * Load the persistent error log. If we have an older pool, this will
2090 * not be present.
2091 */
2092 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2093 if (error != 0 && error != ENOENT)
2094 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2095
2096 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2097 &spa->spa_errlog_scrub);
2098 if (error != 0 && error != ENOENT)
2099 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2100
2101 /*
2102 * Load the history object. If we have an older pool, this
2103 * will not be present.
2104 */
2105 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2106 if (error != 0 && error != ENOENT)
2107 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2108
2109 /*
2110 * If we're assembling the pool from the split-off vdevs of
2111 * an existing pool, we don't want to attach the spares & cache
2112 * devices.
2113 */
2114
2115 /*
2116 * Load any hot spares for this pool.
2117 */
2118 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2119 if (error != 0 && error != ENOENT)
2120 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2121 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2122 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2123 if (load_nvlist(spa, spa->spa_spares.sav_object,
2124 &spa->spa_spares.sav_config) != 0)
2125 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2126
2127 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2128 spa_load_spares(spa);
2129 spa_config_exit(spa, SCL_ALL, FTAG);
2130 } else if (error == 0) {
2131 spa->spa_spares.sav_sync = B_TRUE;
2132 }
2133
2134 /*
2135 * Load any level 2 ARC devices for this pool.
2136 */
2137 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2138 &spa->spa_l2cache.sav_object);
2139 if (error != 0 && error != ENOENT)
2140 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2141 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2142 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2143 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2144 &spa->spa_l2cache.sav_config) != 0)
2145 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2146
2147 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2148 spa_load_l2cache(spa);
2149 spa_config_exit(spa, SCL_ALL, FTAG);
2150 } else if (error == 0) {
2151 spa->spa_l2cache.sav_sync = B_TRUE;
2152 }
2153
2154 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2155
2156 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2157 if (error && error != ENOENT)
2158 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2159
2160 if (error == 0) {
2161 uint64_t autoreplace;
2162
2163 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2164 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2165 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2166 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2167 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2168 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2169 &spa->spa_dedup_ditto);
2170
2171 spa->spa_autoreplace = (autoreplace != 0);
2172 }
2173
2174 /*
2175 * If the 'autoreplace' property is set, then post a resource notifying
2176 * the ZFS DE that it should not issue any faults for unopenable
2177 * devices. We also iterate over the vdevs, and post a sysevent for any
2178 * unopenable vdevs so that the normal autoreplace handler can take
2179 * over.
2180 */
2181 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2182 spa_check_removed(spa->spa_root_vdev);
2183 /*
2184 * For the import case, this is done in spa_import(), because
2185 * at this point we're using the spare definitions from
2186 * the MOS config, not necessarily from the userland config.
2187 */
2188 if (state != SPA_LOAD_IMPORT) {
2189 spa_aux_check_removed(&spa->spa_spares);
2190 spa_aux_check_removed(&spa->spa_l2cache);
2191 }
2192 }
2193
2194 /*
2195 * Load the vdev state for all toplevel vdevs.
2196 */
2197 vdev_load(rvd);
2198
2199 /*
2200 * Propagate the leaf DTLs we just loaded all the way up the tree.
2201 */
2202 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2203 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2204 spa_config_exit(spa, SCL_ALL, FTAG);
2205
2206 /*
2207 * Load the DDTs (dedup tables).
2208 */
2209 error = ddt_load(spa);
2210 if (error != 0)
2211 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2212
2213 spa_update_dspace(spa);
2214
2215 /*
2216 * Validate the config, using the MOS config to fill in any
2217 * information which might be missing. If we fail to validate
2218 * the config then declare the pool unfit for use. If we're
2219 * assembling a pool from a split, the log is not transferred
2220 * over.
2221 */
2222 if (type != SPA_IMPORT_ASSEMBLE) {
2223 nvlist_t *nvconfig;
2224
2225 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2226 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2227
2228 if (!spa_config_valid(spa, nvconfig)) {
2229 nvlist_free(nvconfig);
2230 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2231 ENXIO));
2232 }
2233 nvlist_free(nvconfig);
2234
2235 /*
2236 * Now that we've validate the config, check the state of the
2237 * root vdev. If it can't be opened, it indicates one or
2238 * more toplevel vdevs are faulted.
2239 */
2240 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2241 return (ENXIO);
2242
2243 if (spa_check_logs(spa)) {
2244 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2245 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2246 }
2247 }
2248
2249 /*
2250 * We've successfully opened the pool, verify that we're ready
2251 * to start pushing transactions.
2252 */
2253 if (state != SPA_LOAD_TRYIMPORT) {
2254 if (error = spa_load_verify(spa))
2255 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2256 error));
2257 }
2258
2259 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2260 spa->spa_load_max_txg == UINT64_MAX)) {
2261 dmu_tx_t *tx;
2262 int need_update = B_FALSE;
2263
2264 ASSERT(state != SPA_LOAD_TRYIMPORT);
2265
2266 /*
2267 * Claim log blocks that haven't been committed yet.
2268 * This must all happen in a single txg.
2269 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2270 * invoked from zil_claim_log_block()'s i/o done callback.
2271 * Price of rollback is that we abandon the log.
2272 */
2273 spa->spa_claiming = B_TRUE;
2274
2275 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2276 spa_first_txg(spa));
2277 (void) dmu_objset_find(spa_name(spa),
2278 zil_claim, tx, DS_FIND_CHILDREN);
2279 dmu_tx_commit(tx);
2280
2281 spa->spa_claiming = B_FALSE;
2282
2283 spa_set_log_state(spa, SPA_LOG_GOOD);
2284 spa->spa_sync_on = B_TRUE;
2285 txg_sync_start(spa->spa_dsl_pool);
2286
2287 /*
2288 * Wait for all claims to sync. We sync up to the highest
2289 * claimed log block birth time so that claimed log blocks
2290 * don't appear to be from the future. spa_claim_max_txg
2291 * will have been set for us by either zil_check_log_chain()
2292 * (invoked from spa_check_logs()) or zil_claim() above.
2293 */
2294 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2295
2296 /*
2297 * If the config cache is stale, or we have uninitialized
2298 * metaslabs (see spa_vdev_add()), then update the config.
2299 *
2300 * If this is a verbatim import, trust the current
2301 * in-core spa_config and update the disk labels.
2302 */
2303 if (config_cache_txg != spa->spa_config_txg ||
2304 state == SPA_LOAD_IMPORT ||
2305 state == SPA_LOAD_RECOVER ||
2306 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2307 need_update = B_TRUE;
2308
2309 for (int c = 0; c < rvd->vdev_children; c++)
2310 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2311 need_update = B_TRUE;
2312
2313 /*
2314 * Update the config cache asychronously in case we're the
2315 * root pool, in which case the config cache isn't writable yet.
2316 */
2317 if (need_update)
2318 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2319
2320 /*
2321 * Check all DTLs to see if anything needs resilvering.
2322 */
2323 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2324 vdev_resilver_needed(rvd, NULL, NULL))
2325 spa_async_request(spa, SPA_ASYNC_RESILVER);
2326
2327 /*
2328 * Delete any inconsistent datasets.
2329 */
2330 (void) dmu_objset_find(spa_name(spa),
2331 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2332
2333 /*
2334 * Clean up any stale temporary dataset userrefs.
2335 */
2336 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2337 }
2338
2339 return (0);
2340}
2341
2342static int
2343spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2344{
2345 int mode = spa->spa_mode;
2346
2347 spa_unload(spa);
2348 spa_deactivate(spa);
2349
2350 spa->spa_load_max_txg--;
2351
2352 spa_activate(spa, mode);
2353 spa_async_suspend(spa);
2354
2355 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2356}
2357
2358static int
2359spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2360 uint64_t max_request, int rewind_flags)
2361{
2362 nvlist_t *config = NULL;
2363 int load_error, rewind_error;
2364 uint64_t safe_rewind_txg;
2365 uint64_t min_txg;
2366
2367 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2368 spa->spa_load_max_txg = spa->spa_load_txg;
2369 spa_set_log_state(spa, SPA_LOG_CLEAR);
2370 } else {
2371 spa->spa_load_max_txg = max_request;
2372 }
2373
2374 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2375 mosconfig);
2376 if (load_error == 0)
2377 return (0);
2378
2379 if (spa->spa_root_vdev != NULL)
2380 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2381
2382 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2383 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2384
2385 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2386 nvlist_free(config);
2387 return (load_error);
2388 }
2389
2390 /* Price of rolling back is discarding txgs, including log */
2391 if (state == SPA_LOAD_RECOVER)
2392 spa_set_log_state(spa, SPA_LOG_CLEAR);
2393
2394 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2395 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2396 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2397 TXG_INITIAL : safe_rewind_txg;
2398
2399 /*
2400 * Continue as long as we're finding errors, we're still within
2401 * the acceptable rewind range, and we're still finding uberblocks
2402 */
2403 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2404 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2405 if (spa->spa_load_max_txg < safe_rewind_txg)
2406 spa->spa_extreme_rewind = B_TRUE;
2407 rewind_error = spa_load_retry(spa, state, mosconfig);
2408 }
2409
2410 spa->spa_extreme_rewind = B_FALSE;
2411 spa->spa_load_max_txg = UINT64_MAX;
2412
2413 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2414 spa_config_set(spa, config);
2415
2416 return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
2417}
2418
2419/*
2420 * Pool Open/Import
2421 *
2422 * The import case is identical to an open except that the configuration is sent
2423 * down from userland, instead of grabbed from the configuration cache. For the
2424 * case of an open, the pool configuration will exist in the
2425 * POOL_STATE_UNINITIALIZED state.
2426 *
2427 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2428 * the same time open the pool, without having to keep around the spa_t in some
2429 * ambiguous state.
2430 */
2431static int
2432spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2433 nvlist_t **config)
2434{
2435 spa_t *spa;
2436 spa_load_state_t state = SPA_LOAD_OPEN;
2437 int error;
2438 int locked = B_FALSE;
2439 int firstopen = B_FALSE;
2440
2441 *spapp = NULL;
2442
2443 /*
2444 * As disgusting as this is, we need to support recursive calls to this
2445 * function because dsl_dir_open() is called during spa_load(), and ends
2446 * up calling spa_open() again. The real fix is to figure out how to
2447 * avoid dsl_dir_open() calling this in the first place.
2448 */
2449 if (mutex_owner(&spa_namespace_lock) != curthread) {
2450 mutex_enter(&spa_namespace_lock);
2451 locked = B_TRUE;
2452 }
2453
2454 if ((spa = spa_lookup(pool)) == NULL) {
2455 if (locked)
2456 mutex_exit(&spa_namespace_lock);
2457 return (ENOENT);
2458 }
2459
2460 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2461 zpool_rewind_policy_t policy;
2462
2463 firstopen = B_TRUE;
2464
2465 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2466 &policy);
2467 if (policy.zrp_request & ZPOOL_DO_REWIND)
2468 state = SPA_LOAD_RECOVER;
2469
2470 spa_activate(spa, spa_mode_global);
2471
2472 if (state != SPA_LOAD_RECOVER)
2473 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2474
2475 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2476 policy.zrp_request);
2477
2478 if (error == EBADF) {
2479 /*
2480 * If vdev_validate() returns failure (indicated by
2481 * EBADF), it indicates that one of the vdevs indicates
2482 * that the pool has been exported or destroyed. If
2483 * this is the case, the config cache is out of sync and
2484 * we should remove the pool from the namespace.
2485 */
2486 spa_unload(spa);
2487 spa_deactivate(spa);
2488 spa_config_sync(spa, B_TRUE, B_TRUE);
2489 spa_remove(spa);
2490 if (locked)
2491 mutex_exit(&spa_namespace_lock);
2492 return (ENOENT);
2493 }
2494
2495 if (error) {
2496 /*
2497 * We can't open the pool, but we still have useful
2498 * information: the state of each vdev after the
2499 * attempted vdev_open(). Return this to the user.
2500 */
2501 if (config != NULL && spa->spa_config) {
2502 VERIFY(nvlist_dup(spa->spa_config, config,
2503 KM_SLEEP) == 0);
2504 VERIFY(nvlist_add_nvlist(*config,
2505 ZPOOL_CONFIG_LOAD_INFO,
2506 spa->spa_load_info) == 0);
2507 }
2508 spa_unload(spa);
2509 spa_deactivate(spa);
2510 spa->spa_last_open_failed = error;
2511 if (locked)
2512 mutex_exit(&spa_namespace_lock);
2513 *spapp = NULL;
2514 return (error);
2515 }
2516 }
2517
2518 spa_open_ref(spa, tag);
2519
2520 if (config != NULL)
2521 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2522
2523 /*
2524 * If we've recovered the pool, pass back any information we
2525 * gathered while doing the load.
2526 */
2527 if (state == SPA_LOAD_RECOVER) {
2528 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2529 spa->spa_load_info) == 0);
2530 }
2531
2532 if (locked) {
2533 spa->spa_last_open_failed = 0;
2534 spa->spa_last_ubsync_txg = 0;
2535 spa->spa_load_txg = 0;
2536 mutex_exit(&spa_namespace_lock);
2537#ifdef __FreeBSD__
2538#ifdef _KERNEL
2539 if (firstopen)
2540 zvol_create_minors(pool);
2541#endif
2542#endif
2543 }
2544
2545 *spapp = spa;
2546
2547 return (0);
2548}
2549
2550int
2551spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2552 nvlist_t **config)
2553{
2554 return (spa_open_common(name, spapp, tag, policy, config));
2555}
2556
2557int
2558spa_open(const char *name, spa_t **spapp, void *tag)
2559{
2560 return (spa_open_common(name, spapp, tag, NULL, NULL));
2561}
2562
2563/*
2564 * Lookup the given spa_t, incrementing the inject count in the process,
2565 * preventing it from being exported or destroyed.
2566 */
2567spa_t *
2568spa_inject_addref(char *name)
2569{
2570 spa_t *spa;
2571
2572 mutex_enter(&spa_namespace_lock);
2573 if ((spa = spa_lookup(name)) == NULL) {
2574 mutex_exit(&spa_namespace_lock);
2575 return (NULL);
2576 }
2577 spa->spa_inject_ref++;
2578 mutex_exit(&spa_namespace_lock);
2579
2580 return (spa);
2581}
2582
2583void
2584spa_inject_delref(spa_t *spa)
2585{
2586 mutex_enter(&spa_namespace_lock);
2587 spa->spa_inject_ref--;
2588 mutex_exit(&spa_namespace_lock);
2589}
2590
2591/*
2592 * Add spares device information to the nvlist.
2593 */
2594static void
2595spa_add_spares(spa_t *spa, nvlist_t *config)
2596{
2597 nvlist_t **spares;
2598 uint_t i, nspares;
2599 nvlist_t *nvroot;
2600 uint64_t guid;
2601 vdev_stat_t *vs;
2602 uint_t vsc;
2603 uint64_t pool;
2604
2605 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2606
2607 if (spa->spa_spares.sav_count == 0)
2608 return;
2609
2610 VERIFY(nvlist_lookup_nvlist(config,
2611 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2612 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2613 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2614 if (nspares != 0) {
2615 VERIFY(nvlist_add_nvlist_array(nvroot,
2616 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2617 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2618 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2619
2620 /*
2621 * Go through and find any spares which have since been
2622 * repurposed as an active spare. If this is the case, update
2623 * their status appropriately.
2624 */
2625 for (i = 0; i < nspares; i++) {
2626 VERIFY(nvlist_lookup_uint64(spares[i],
2627 ZPOOL_CONFIG_GUID, &guid) == 0);
2628 if (spa_spare_exists(guid, &pool, NULL) &&
2629 pool != 0ULL) {
2630 VERIFY(nvlist_lookup_uint64_array(
2631 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2632 (uint64_t **)&vs, &vsc) == 0);
2633 vs->vs_state = VDEV_STATE_CANT_OPEN;
2634 vs->vs_aux = VDEV_AUX_SPARED;
2635 }
2636 }
2637 }
2638}
2639
2640/*
2641 * Add l2cache device information to the nvlist, including vdev stats.
2642 */
2643static void
2644spa_add_l2cache(spa_t *spa, nvlist_t *config)
2645{
2646 nvlist_t **l2cache;
2647 uint_t i, j, nl2cache;
2648 nvlist_t *nvroot;
2649 uint64_t guid;
2650 vdev_t *vd;
2651 vdev_stat_t *vs;
2652 uint_t vsc;
2653
2654 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2655
2656 if (spa->spa_l2cache.sav_count == 0)
2657 return;
2658
2659 VERIFY(nvlist_lookup_nvlist(config,
2660 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2661 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2662 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2663 if (nl2cache != 0) {
2664 VERIFY(nvlist_add_nvlist_array(nvroot,
2665 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2666 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2667 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2668
2669 /*
2670 * Update level 2 cache device stats.
2671 */
2672
2673 for (i = 0; i < nl2cache; i++) {
2674 VERIFY(nvlist_lookup_uint64(l2cache[i],
2675 ZPOOL_CONFIG_GUID, &guid) == 0);
2676
2677 vd = NULL;
2678 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2679 if (guid ==
2680 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2681 vd = spa->spa_l2cache.sav_vdevs[j];
2682 break;
2683 }
2684 }
2685 ASSERT(vd != NULL);
2686
2687 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2688 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2689 == 0);
2690 vdev_get_stats(vd, vs);
2691 }
2692 }
2693}
2694
2695int
2696spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2697{
2698 int error;
2699 spa_t *spa;
2700
2701 *config = NULL;
2702 error = spa_open_common(name, &spa, FTAG, NULL, config);
2703
2704 if (spa != NULL) {
2705 /*
2706 * This still leaves a window of inconsistency where the spares
2707 * or l2cache devices could change and the config would be
2708 * self-inconsistent.
2709 */
2710 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2711
2712 if (*config != NULL) {
2713 uint64_t loadtimes[2];
2714
2715 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
2716 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
2717 VERIFY(nvlist_add_uint64_array(*config,
2718 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
2719
2720 VERIFY(nvlist_add_uint64(*config,
2721 ZPOOL_CONFIG_ERRCOUNT,
2722 spa_get_errlog_size(spa)) == 0);
2723
2724 if (spa_suspended(spa))
2725 VERIFY(nvlist_add_uint64(*config,
2726 ZPOOL_CONFIG_SUSPENDED,
2727 spa->spa_failmode) == 0);
2728
2729 spa_add_spares(spa, *config);
2730 spa_add_l2cache(spa, *config);
2731 }
2732 }
2733
2734 /*
2735 * We want to get the alternate root even for faulted pools, so we cheat
2736 * and call spa_lookup() directly.
2737 */
2738 if (altroot) {
2739 if (spa == NULL) {
2740 mutex_enter(&spa_namespace_lock);
2741 spa = spa_lookup(name);
2742 if (spa)
2743 spa_altroot(spa, altroot, buflen);
2744 else
2745 altroot[0] = '\0';
2746 spa = NULL;
2747 mutex_exit(&spa_namespace_lock);
2748 } else {
2749 spa_altroot(spa, altroot, buflen);
2750 }
2751 }
2752
2753 if (spa != NULL) {
2754 spa_config_exit(spa, SCL_CONFIG, FTAG);
2755 spa_close(spa, FTAG);
2756 }
2757
2758 return (error);
2759}
2760
2761/*
2762 * Validate that the auxiliary device array is well formed. We must have an
2763 * array of nvlists, each which describes a valid leaf vdev. If this is an
2764 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2765 * specified, as long as they are well-formed.
2766 */
2767static int
2768spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
2769 spa_aux_vdev_t *sav, const char *config, uint64_t version,
2770 vdev_labeltype_t label)
2771{
2772 nvlist_t **dev;
2773 uint_t i, ndev;
2774 vdev_t *vd;
2775 int error;
2776
2777 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2778
2779 /*
2780 * It's acceptable to have no devs specified.
2781 */
2782 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
2783 return (0);
2784
2785 if (ndev == 0)
2786 return (EINVAL);
2787
2788 /*
2789 * Make sure the pool is formatted with a version that supports this
2790 * device type.
2791 */
2792 if (spa_version(spa) < version)
2793 return (ENOTSUP);
2794
2795 /*
2796 * Set the pending device list so we correctly handle device in-use
2797 * checking.
2798 */
2799 sav->sav_pending = dev;
2800 sav->sav_npending = ndev;
2801
2802 for (i = 0; i < ndev; i++) {
2803 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
2804 mode)) != 0)
2805 goto out;
2806
2807 if (!vd->vdev_ops->vdev_op_leaf) {
2808 vdev_free(vd);
2809 error = EINVAL;
2810 goto out;
2811 }
2812
2813 /*
2814 * The L2ARC currently only supports disk devices in
2815 * kernel context. For user-level testing, we allow it.
2816 */
2817#ifdef _KERNEL
2818 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
2819 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2820 error = ENOTBLK;
2821 goto out;
2822 }
2823#endif
2824 vd->vdev_top = vd;
2825
2826 if ((error = vdev_open(vd)) == 0 &&
2827 (error = vdev_label_init(vd, crtxg, label)) == 0) {
2828 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
2829 vd->vdev_guid) == 0);
2830 }
2831
2832 vdev_free(vd);
2833
2834 if (error &&
2835 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
2836 goto out;
2837 else
2838 error = 0;
2839 }
2840
2841out:
2842 sav->sav_pending = NULL;
2843 sav->sav_npending = 0;
2844 return (error);
2845}
2846
2847static int
2848spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
2849{
2850 int error;
2851
2852 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2853
2854 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2855 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
2856 VDEV_LABEL_SPARE)) != 0) {
2857 return (error);
2858 }
2859
2860 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2861 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
2862 VDEV_LABEL_L2CACHE));
2863}
2864
2865static void
2866spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
2867 const char *config)
2868{
2869 int i;
2870
2871 if (sav->sav_config != NULL) {
2872 nvlist_t **olddevs;
2873 uint_t oldndevs;
2874 nvlist_t **newdevs;
2875
2876 /*
2877 * Generate new dev list by concatentating with the
2878 * current dev list.
2879 */
2880 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
2881 &olddevs, &oldndevs) == 0);
2882
2883 newdevs = kmem_alloc(sizeof (void *) *
2884 (ndevs + oldndevs), KM_SLEEP);
2885 for (i = 0; i < oldndevs; i++)
2886 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
2887 KM_SLEEP) == 0);
2888 for (i = 0; i < ndevs; i++)
2889 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
2890 KM_SLEEP) == 0);
2891
2892 VERIFY(nvlist_remove(sav->sav_config, config,
2893 DATA_TYPE_NVLIST_ARRAY) == 0);
2894
2895 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2896 config, newdevs, ndevs + oldndevs) == 0);
2897 for (i = 0; i < oldndevs + ndevs; i++)
2898 nvlist_free(newdevs[i]);
2899 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
2900 } else {
2901 /*
2902 * Generate a new dev list.
2903 */
2904 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
2905 KM_SLEEP) == 0);
2906 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
2907 devs, ndevs) == 0);
2908 }
2909}
2910
2911/*
2912 * Stop and drop level 2 ARC devices
2913 */
2914void
2915spa_l2cache_drop(spa_t *spa)
2916{
2917 vdev_t *vd;
2918 int i;
2919 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2920
2921 for (i = 0; i < sav->sav_count; i++) {
2922 uint64_t pool;
2923
2924 vd = sav->sav_vdevs[i];
2925 ASSERT(vd != NULL);
2926
2927 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2928 pool != 0ULL && l2arc_vdev_present(vd))
2929 l2arc_remove_vdev(vd);
2930 if (vd->vdev_isl2cache)
2931 spa_l2cache_remove(vd);
2932 vdev_clear_stats(vd);
2933 (void) vdev_close(vd);
2934 }
2935}
2936
2937/*
2938 * Pool Creation
2939 */
2940int
2941spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
2942 const char *history_str, nvlist_t *zplprops)
2943{
2944 spa_t *spa;
2945 char *altroot = NULL;
2946 vdev_t *rvd;
2947 dsl_pool_t *dp;
2948 dmu_tx_t *tx;
2949 int error = 0;
2950 uint64_t txg = TXG_INITIAL;
2951 nvlist_t **spares, **l2cache;
2952 uint_t nspares, nl2cache;
2953 uint64_t version, obj;
2954
2955 /*
2956 * If this pool already exists, return failure.
2957 */
2958 mutex_enter(&spa_namespace_lock);
2959 if (spa_lookup(pool) != NULL) {
2960 mutex_exit(&spa_namespace_lock);
2961 return (EEXIST);
2962 }
2963
2964 /*
2965 * Allocate a new spa_t structure.
2966 */
2967 (void) nvlist_lookup_string(props,
2968 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2969 spa = spa_add(pool, NULL, altroot);
2970 spa_activate(spa, spa_mode_global);
2971
2972 if (props && (error = spa_prop_validate(spa, props))) {
2973 spa_deactivate(spa);
2974 spa_remove(spa);
2975 mutex_exit(&spa_namespace_lock);
2976 return (error);
2977 }
2978
2979 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2980 &version) != 0)
2981 version = SPA_VERSION;
2982 ASSERT(version <= SPA_VERSION);
2983
2984 spa->spa_first_txg = txg;
2985 spa->spa_uberblock.ub_txg = txg - 1;
2986 spa->spa_uberblock.ub_version = version;
2987 spa->spa_ubsync = spa->spa_uberblock;
2988
2989 /*
2990 * Create "The Godfather" zio to hold all async IOs
2991 */
2992 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2993 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2994
2995 /*
2996 * Create the root vdev.
2997 */
2998 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2999
3000 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3001
3002 ASSERT(error != 0 || rvd != NULL);
3003 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3004
3005 if (error == 0 && !zfs_allocatable_devs(nvroot))
3006 error = EINVAL;
3007
3008 if (error == 0 &&
3009 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3010 (error = spa_validate_aux(spa, nvroot, txg,
3011 VDEV_ALLOC_ADD)) == 0) {
3012 for (int c = 0; c < rvd->vdev_children; c++) {
3013 vdev_metaslab_set_size(rvd->vdev_child[c]);
3014 vdev_expand(rvd->vdev_child[c], txg);
3015 }
3016 }
3017
3018 spa_config_exit(spa, SCL_ALL, FTAG);
3019
3020 if (error != 0) {
3021 spa_unload(spa);
3022 spa_deactivate(spa);
3023 spa_remove(spa);
3024 mutex_exit(&spa_namespace_lock);
3025 return (error);
3026 }
3027
3028 /*
3029 * Get the list of spares, if specified.
3030 */
3031 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3032 &spares, &nspares) == 0) {
3033 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3034 KM_SLEEP) == 0);
3035 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3036 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3037 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3038 spa_load_spares(spa);
3039 spa_config_exit(spa, SCL_ALL, FTAG);
3040 spa->spa_spares.sav_sync = B_TRUE;
3041 }
3042
3043 /*
3044 * Get the list of level 2 cache devices, if specified.
3045 */
3046 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3047 &l2cache, &nl2cache) == 0) {
3048 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3049 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3050 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3051 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3052 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3053 spa_load_l2cache(spa);
3054 spa_config_exit(spa, SCL_ALL, FTAG);
3055 spa->spa_l2cache.sav_sync = B_TRUE;
3056 }
3057
3058 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3059 spa->spa_meta_objset = dp->dp_meta_objset;
3060
3061 /*
3062 * Create DDTs (dedup tables).
3063 */
3064 ddt_create(spa);
3065
3066 spa_update_dspace(spa);
3067
3068 tx = dmu_tx_create_assigned(dp, txg);
3069
3070 /*
3071 * Create the pool config object.
3072 */
3073 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3074 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3075 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3076
3077 if (zap_add(spa->spa_meta_objset,
3078 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3079 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3080 cmn_err(CE_PANIC, "failed to add pool config");
3081 }
3082
3083 if (zap_add(spa->spa_meta_objset,
3084 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3085 sizeof (uint64_t), 1, &version, tx) != 0) {
3086 cmn_err(CE_PANIC, "failed to add pool version");
3087 }
3088
3089 /* Newly created pools with the right version are always deflated. */
3090 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3091 spa->spa_deflate = TRUE;
3092 if (zap_add(spa->spa_meta_objset,
3093 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3094 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3095 cmn_err(CE_PANIC, "failed to add deflate");
3096 }
3097 }
3098
3099 /*
3100 * Create the deferred-free bpobj. Turn off compression
3101 * because sync-to-convergence takes longer if the blocksize
3102 * keeps changing.
3103 */
3104 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3105 dmu_object_set_compress(spa->spa_meta_objset, obj,
3106 ZIO_COMPRESS_OFF, tx);
3107 if (zap_add(spa->spa_meta_objset,
3108 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3109 sizeof (uint64_t), 1, &obj, tx) != 0) {
3110 cmn_err(CE_PANIC, "failed to add bpobj");
3111 }
3112 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3113 spa->spa_meta_objset, obj));
3114
3115 /*
3116 * Create the pool's history object.
3117 */
3118 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3119 spa_history_create_obj(spa, tx);
3120
3121 /*
3122 * Set pool properties.
3123 */
3124 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3125 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3126 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3127 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3128
3129 if (props != NULL) {
3130 spa_configfile_set(spa, props, B_FALSE);
3131 spa_sync_props(spa, props, tx);
3132 }
3133
3134 dmu_tx_commit(tx);
3135
3136 spa->spa_sync_on = B_TRUE;
3137 txg_sync_start(spa->spa_dsl_pool);
3138
3139 /*
3140 * We explicitly wait for the first transaction to complete so that our
3141 * bean counters are appropriately updated.
3142 */
3143 txg_wait_synced(spa->spa_dsl_pool, txg);
3144
3145 spa_config_sync(spa, B_FALSE, B_TRUE);
3146
3147 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3148 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3149 spa_history_log_version(spa, LOG_POOL_CREATE);
3150
3151 spa->spa_minref = refcount_count(&spa->spa_refcount);
3152
3153 mutex_exit(&spa_namespace_lock);
3154
3155 return (0);
3156}
3157
3158#if defined(sun)
3159#ifdef _KERNEL
3160/*
3161 * Get the root pool information from the root disk, then import the root pool
3162 * during the system boot up time.
3163 */
3164extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3165
3166static nvlist_t *
3167spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3168{
3169 nvlist_t *config;
3170 nvlist_t *nvtop, *nvroot;
3171 uint64_t pgid;
3172
3173 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3174 return (NULL);
3175
3176 /*
3177 * Add this top-level vdev to the child array.
3178 */
3179 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3180 &nvtop) == 0);
3181 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3182 &pgid) == 0);
3183 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3184
3185 /*
3186 * Put this pool's top-level vdevs into a root vdev.
3187 */
3188 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3189 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3190 VDEV_TYPE_ROOT) == 0);
3191 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3192 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3193 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3194 &nvtop, 1) == 0);
3195
3196 /*
3197 * Replace the existing vdev_tree with the new root vdev in
3198 * this pool's configuration (remove the old, add the new).
3199 */
3200 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3201 nvlist_free(nvroot);
3202 return (config);
3203}
3204
3205/*
3206 * Walk the vdev tree and see if we can find a device with "better"
3207 * configuration. A configuration is "better" if the label on that
3208 * device has a more recent txg.
3209 */
3210static void
3211spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3212{
3213 for (int c = 0; c < vd->vdev_children; c++)
3214 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3215
3216 if (vd->vdev_ops->vdev_op_leaf) {
3217 nvlist_t *label;
3218 uint64_t label_txg;
3219
3220 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3221 &label) != 0)
3222 return;
3223
3224 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3225 &label_txg) == 0);
3226
3227 /*
3228 * Do we have a better boot device?
3229 */
3230 if (label_txg > *txg) {
3231 *txg = label_txg;
3232 *avd = vd;
3233 }
3234 nvlist_free(label);
3235 }
3236}
3237
3238/*
3239 * Import a root pool.
3240 *
3241 * For x86. devpath_list will consist of devid and/or physpath name of
3242 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3243 * The GRUB "findroot" command will return the vdev we should boot.
3244 *
3245 * For Sparc, devpath_list consists the physpath name of the booting device
3246 * no matter the rootpool is a single device pool or a mirrored pool.
3247 * e.g.
3248 * "/pci@1f,0/ide@d/disk@0,0:a"
3249 */
3250int
3251spa_import_rootpool(char *devpath, char *devid)
3252{
3253 spa_t *spa;
3254 vdev_t *rvd, *bvd, *avd = NULL;
3255 nvlist_t *config, *nvtop;
3256 uint64_t guid, txg;
3257 char *pname;
3258 int error;
3259
3260 /*
3261 * Read the label from the boot device and generate a configuration.
3262 */
3263 config = spa_generate_rootconf(devpath, devid, &guid);
3264#if defined(_OBP) && defined(_KERNEL)
3265 if (config == NULL) {
3266 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3267 /* iscsi boot */
3268 get_iscsi_bootpath_phy(devpath);
3269 config = spa_generate_rootconf(devpath, devid, &guid);
3270 }
3271 }
3272#endif
3273 if (config == NULL) {
3274 cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
3275 devpath);
3276 return (EIO);
3277 }
3278
3279 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3280 &pname) == 0);
3281 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3282
3283 mutex_enter(&spa_namespace_lock);
3284 if ((spa = spa_lookup(pname)) != NULL) {
3285 /*
3286 * Remove the existing root pool from the namespace so that we
3287 * can replace it with the correct config we just read in.
3288 */
3289 spa_remove(spa);
3290 }
3291
3292 spa = spa_add(pname, config, NULL);
3293 spa->spa_is_root = B_TRUE;
3294 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3295
3296 /*
3297 * Build up a vdev tree based on the boot device's label config.
3298 */
3299 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3300 &nvtop) == 0);
3301 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3302 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3303 VDEV_ALLOC_ROOTPOOL);
3304 spa_config_exit(spa, SCL_ALL, FTAG);
3305 if (error) {
3306 mutex_exit(&spa_namespace_lock);
3307 nvlist_free(config);
3308 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3309 pname);
3310 return (error);
3311 }
3312
3313 /*
3314 * Get the boot vdev.
3315 */
3316 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3317 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3318 (u_longlong_t)guid);
3319 error = ENOENT;
3320 goto out;
3321 }
3322
3323 /*
3324 * Determine if there is a better boot device.
3325 */
3326 avd = bvd;
3327 spa_alt_rootvdev(rvd, &avd, &txg);
3328 if (avd != bvd) {
3329 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3330 "try booting from '%s'", avd->vdev_path);
3331 error = EINVAL;
3332 goto out;
3333 }
3334
3335 /*
3336 * If the boot device is part of a spare vdev then ensure that
3337 * we're booting off the active spare.
3338 */
3339 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3340 !bvd->vdev_isspare) {
3341 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3342 "try booting from '%s'",
3343 bvd->vdev_parent->
3344 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3345 error = EINVAL;
3346 goto out;
3347 }
3348
3349 error = 0;
3350 spa_history_log_version(spa, LOG_POOL_IMPORT);
3351out:
3352 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3353 vdev_free(rvd);
3354 spa_config_exit(spa, SCL_ALL, FTAG);
3355 mutex_exit(&spa_namespace_lock);
3356
3357 nvlist_free(config);
3358 return (error);
3359}
3360
3361#endif
3362#endif /* sun */
3363
3364/*
3365 * Import a non-root pool into the system.
3366 */
3367int
3368spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3369{
3370 spa_t *spa;
3371 char *altroot = NULL;
3372 spa_load_state_t state = SPA_LOAD_IMPORT;
3373 zpool_rewind_policy_t policy;
3374 uint64_t mode = spa_mode_global;
3375 uint64_t readonly = B_FALSE;
3376 int error;
3377 nvlist_t *nvroot;
3378 nvlist_t **spares, **l2cache;
3379 uint_t nspares, nl2cache;
3380
3381 /*
3382 * If a pool with this name exists, return failure.
3383 */
3384 mutex_enter(&spa_namespace_lock);
3385 if (spa_lookup(pool) != NULL) {
3386 mutex_exit(&spa_namespace_lock);
3387 return (EEXIST);
3388 }
3389
3390 /*
3391 * Create and initialize the spa structure.
3392 */
3393 (void) nvlist_lookup_string(props,
3394 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3395 (void) nvlist_lookup_uint64(props,
3396 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3397 if (readonly)
3398 mode = FREAD;
3399 spa = spa_add(pool, config, altroot);
3400 spa->spa_import_flags = flags;
3401
3402 /*
3403 * Verbatim import - Take a pool and insert it into the namespace
3404 * as if it had been loaded at boot.
3405 */
3406 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3407 if (props != NULL)
3408 spa_configfile_set(spa, props, B_FALSE);
3409
3410 spa_config_sync(spa, B_FALSE, B_TRUE);
3411
3412 mutex_exit(&spa_namespace_lock);
3413 spa_history_log_version(spa, LOG_POOL_IMPORT);
3414
3415 return (0);
3416 }
3417
3418 spa_activate(spa, mode);
3419
3420 /*
3421 * Don't start async tasks until we know everything is healthy.
3422 */
3423 spa_async_suspend(spa);
3424
3425 zpool_get_rewind_policy(config, &policy);
3426 if (policy.zrp_request & ZPOOL_DO_REWIND)
3427 state = SPA_LOAD_RECOVER;
3428
3429 /*
3430 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3431 * because the user-supplied config is actually the one to trust when
3432 * doing an import.
3433 */
3434 if (state != SPA_LOAD_RECOVER)
3435 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3436
3437 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3438 policy.zrp_request);
3439
3440 /*
3441 * Propagate anything learned while loading the pool and pass it
3442 * back to caller (i.e. rewind info, missing devices, etc).
3443 */
3444 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3445 spa->spa_load_info) == 0);
3446
3447 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3448 /*
3449 * Toss any existing sparelist, as it doesn't have any validity
3450 * anymore, and conflicts with spa_has_spare().
3451 */
3452 if (spa->spa_spares.sav_config) {
3453 nvlist_free(spa->spa_spares.sav_config);
3454 spa->spa_spares.sav_config = NULL;
3455 spa_load_spares(spa);
3456 }
3457 if (spa->spa_l2cache.sav_config) {
3458 nvlist_free(spa->spa_l2cache.sav_config);
3459 spa->spa_l2cache.sav_config = NULL;
3460 spa_load_l2cache(spa);
3461 }
3462
3463 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3464 &nvroot) == 0);
3465 if (error == 0)
3466 error = spa_validate_aux(spa, nvroot, -1ULL,
3467 VDEV_ALLOC_SPARE);
3468 if (error == 0)
3469 error = spa_validate_aux(spa, nvroot, -1ULL,
3470 VDEV_ALLOC_L2CACHE);
3471 spa_config_exit(spa, SCL_ALL, FTAG);
3472
3473 if (props != NULL)
3474 spa_configfile_set(spa, props, B_FALSE);
3475
3476 if (error != 0 || (props && spa_writeable(spa) &&
3477 (error = spa_prop_set(spa, props)))) {
3478 spa_unload(spa);
3479 spa_deactivate(spa);
3480 spa_remove(spa);
3481 mutex_exit(&spa_namespace_lock);
3482 return (error);
3483 }
3484
3485 spa_async_resume(spa);
3486
3487 /*
3488 * Override any spares and level 2 cache devices as specified by
3489 * the user, as these may have correct device names/devids, etc.
3490 */
3491 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3492 &spares, &nspares) == 0) {
3493 if (spa->spa_spares.sav_config)
3494 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3495 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3496 else
3497 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3498 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3499 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3500 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3501 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3502 spa_load_spares(spa);
3503 spa_config_exit(spa, SCL_ALL, FTAG);
3504 spa->spa_spares.sav_sync = B_TRUE;
3505 }
3506 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3507 &l2cache, &nl2cache) == 0) {
3508 if (spa->spa_l2cache.sav_config)
3509 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3510 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3511 else
3512 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3513 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3514 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3515 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3516 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3517 spa_load_l2cache(spa);
3518 spa_config_exit(spa, SCL_ALL, FTAG);
3519 spa->spa_l2cache.sav_sync = B_TRUE;
3520 }
3521
3522 /*
3523 * Check for any removed devices.
3524 */
3525 if (spa->spa_autoreplace) {
3526 spa_aux_check_removed(&spa->spa_spares);
3527 spa_aux_check_removed(&spa->spa_l2cache);
3528 }
3529
3530 if (spa_writeable(spa)) {
3531 /*
3532 * Update the config cache to include the newly-imported pool.
3533 */
3534 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3535 }
3536
3537 /*
3538 * It's possible that the pool was expanded while it was exported.
3539 * We kick off an async task to handle this for us.
3540 */
3541 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3542
3543 mutex_exit(&spa_namespace_lock);
3544 spa_history_log_version(spa, LOG_POOL_IMPORT);
3545
3546#ifdef __FreeBSD__
3547#ifdef _KERNEL
3548 zvol_create_minors(pool);
3549#endif
3550#endif
3551 return (0);
3552}
3553
3554nvlist_t *
3555spa_tryimport(nvlist_t *tryconfig)
3556{
3557 nvlist_t *config = NULL;
3558 char *poolname;
3559 spa_t *spa;
3560 uint64_t state;
3561 int error;
3562
3563 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3564 return (NULL);
3565
3566 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3567 return (NULL);
3568
3569 /*
3570 * Create and initialize the spa structure.
3571 */
3572 mutex_enter(&spa_namespace_lock);
3573 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3574 spa_activate(spa, FREAD);
3575
3576 /*
3577 * Pass off the heavy lifting to spa_load().
3578 * Pass TRUE for mosconfig because the user-supplied config
3579 * is actually the one to trust when doing an import.
3580 */
3581 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3582
3583 /*
3584 * If 'tryconfig' was at least parsable, return the current config.
3585 */
3586 if (spa->spa_root_vdev != NULL) {
3587 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3588 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3589 poolname) == 0);
3590 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3591 state) == 0);
3592 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3593 spa->spa_uberblock.ub_timestamp) == 0);
3594
3595 /*
3596 * If the bootfs property exists on this pool then we
3597 * copy it out so that external consumers can tell which
3598 * pools are bootable.
3599 */
3600 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3601 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3602
3603 /*
3604 * We have to play games with the name since the
3605 * pool was opened as TRYIMPORT_NAME.
3606 */
3607 if (dsl_dsobj_to_dsname(spa_name(spa),
3608 spa->spa_bootfs, tmpname) == 0) {
3609 char *cp;
3610 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3611
3612 cp = strchr(tmpname, '/');
3613 if (cp == NULL) {
3614 (void) strlcpy(dsname, tmpname,
3615 MAXPATHLEN);
3616 } else {
3617 (void) snprintf(dsname, MAXPATHLEN,
3618 "%s/%s", poolname, ++cp);
3619 }
3620 VERIFY(nvlist_add_string(config,
3621 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3622 kmem_free(dsname, MAXPATHLEN);
3623 }
3624 kmem_free(tmpname, MAXPATHLEN);
3625 }
3626
3627 /*
3628 * Add the list of hot spares and level 2 cache devices.
3629 */
3630 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3631 spa_add_spares(spa, config);
3632 spa_add_l2cache(spa, config);
3633 spa_config_exit(spa, SCL_CONFIG, FTAG);
3634 }
3635
3636 spa_unload(spa);
3637 spa_deactivate(spa);
3638 spa_remove(spa);
3639 mutex_exit(&spa_namespace_lock);
3640
3641 return (config);
3642}
3643
3644/*
3645 * Pool export/destroy
3646 *
3647 * The act of destroying or exporting a pool is very simple. We make sure there
3648 * is no more pending I/O and any references to the pool are gone. Then, we
3649 * update the pool state and sync all the labels to disk, removing the
3650 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3651 * we don't sync the labels or remove the configuration cache.
3652 */
3653static int
3654spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3655 boolean_t force, boolean_t hardforce)
3656{
3657 spa_t *spa;
3658
3659 if (oldconfig)
3660 *oldconfig = NULL;
3661
3662 if (!(spa_mode_global & FWRITE))
3663 return (EROFS);
3664
3665 mutex_enter(&spa_namespace_lock);
3666 if ((spa = spa_lookup(pool)) == NULL) {
3667 mutex_exit(&spa_namespace_lock);
3668 return (ENOENT);
3669 }
3670
3671 /*
3672 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3673 * reacquire the namespace lock, and see if we can export.
3674 */
3675 spa_open_ref(spa, FTAG);
3676 mutex_exit(&spa_namespace_lock);
3677 spa_async_suspend(spa);
3678 mutex_enter(&spa_namespace_lock);
3679 spa_close(spa, FTAG);
3680
3681 /*
3682 * The pool will be in core if it's openable,
3683 * in which case we can modify its state.
3684 */
3685 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3686 /*
3687 * Objsets may be open only because they're dirty, so we
3688 * have to force it to sync before checking spa_refcnt.
3689 */
3690 txg_wait_synced(spa->spa_dsl_pool, 0);
3691
3692 /*
3693 * A pool cannot be exported or destroyed if there are active
3694 * references. If we are resetting a pool, allow references by
3695 * fault injection handlers.
3696 */
3697 if (!spa_refcount_zero(spa) ||
3698 (spa->spa_inject_ref != 0 &&
3699 new_state != POOL_STATE_UNINITIALIZED)) {
3700 spa_async_resume(spa);
3701 mutex_exit(&spa_namespace_lock);
3702 return (EBUSY);
3703 }
3704
3705 /*
3706 * A pool cannot be exported if it has an active shared spare.
3707 * This is to prevent other pools stealing the active spare
3708 * from an exported pool. At user's own will, such pool can
3709 * be forcedly exported.
3710 */
3711 if (!force && new_state == POOL_STATE_EXPORTED &&
3712 spa_has_active_shared_spare(spa)) {
3713 spa_async_resume(spa);
3714 mutex_exit(&spa_namespace_lock);
3715 return (EXDEV);
3716 }
3717
3718 /*
3719 * We want this to be reflected on every label,
3720 * so mark them all dirty. spa_unload() will do the
3721 * final sync that pushes these changes out.
3722 */
3723 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
3724 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3725 spa->spa_state = new_state;
3726 spa->spa_final_txg = spa_last_synced_txg(spa) +
3727 TXG_DEFER_SIZE + 1;
3728 vdev_config_dirty(spa->spa_root_vdev);
3729 spa_config_exit(spa, SCL_ALL, FTAG);
3730 }
3731 }
3732
3733 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
3734
3735 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3736 spa_unload(spa);
3737 spa_deactivate(spa);
3738 }
3739
3740 if (oldconfig && spa->spa_config)
3741 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
3742
3743 if (new_state != POOL_STATE_UNINITIALIZED) {
3744 if (!hardforce)
3745 spa_config_sync(spa, B_TRUE, B_TRUE);
3746 spa_remove(spa);
3747 }
3748 mutex_exit(&spa_namespace_lock);
3749
3750 return (0);
3751}
3752
3753/*
3754 * Destroy a storage pool.
3755 */
3756int
3757spa_destroy(char *pool)
3758{
3759 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
3760 B_FALSE, B_FALSE));
3761}
3762
3763/*
3764 * Export a storage pool.
3765 */
3766int
3767spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
3768 boolean_t hardforce)
3769{
3770 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
3771 force, hardforce));
3772}
3773
3774/*
3775 * Similar to spa_export(), this unloads the spa_t without actually removing it
3776 * from the namespace in any way.
3777 */
3778int
3779spa_reset(char *pool)
3780{
3781 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
3782 B_FALSE, B_FALSE));
3783}
3784
3785/*
3786 * ==========================================================================
3787 * Device manipulation
3788 * ==========================================================================
3789 */
3790
3791/*
3792 * Add a device to a storage pool.
3793 */
3794int
3795spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3796{
3797 uint64_t txg, id;
3798 int error;
3799 vdev_t *rvd = spa->spa_root_vdev;
3800 vdev_t *vd, *tvd;
3801 nvlist_t **spares, **l2cache;
3802 uint_t nspares, nl2cache;
3803
3804 ASSERT(spa_writeable(spa));
3805
3806 txg = spa_vdev_enter(spa);
3807
3808 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
3809 VDEV_ALLOC_ADD)) != 0)
3810 return (spa_vdev_exit(spa, NULL, txg, error));
3811
3812 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
3813
3814 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
3815 &nspares) != 0)
3816 nspares = 0;
3817
3818 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
3819 &nl2cache) != 0)
3820 nl2cache = 0;
3821
3822 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
3823 return (spa_vdev_exit(spa, vd, txg, EINVAL));
3824
3825 if (vd->vdev_children != 0 &&
3826 (error = vdev_create(vd, txg, B_FALSE)) != 0)
3827 return (spa_vdev_exit(spa, vd, txg, error));
3828
3829 /*
3830 * We must validate the spares and l2cache devices after checking the
3831 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
3832 */
3833 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
3834 return (spa_vdev_exit(spa, vd, txg, error));
3835
3836 /*
3837 * Transfer each new top-level vdev from vd to rvd.
3838 */
3839 for (int c = 0; c < vd->vdev_children; c++) {
3840
3841 /*
3842 * Set the vdev id to the first hole, if one exists.
3843 */
3844 for (id = 0; id < rvd->vdev_children; id++) {
3845 if (rvd->vdev_child[id]->vdev_ishole) {
3846 vdev_free(rvd->vdev_child[id]);
3847 break;
3848 }
3849 }
3850 tvd = vd->vdev_child[c];
3851 vdev_remove_child(vd, tvd);
3852 tvd->vdev_id = id;
3853 vdev_add_child(rvd, tvd);
3854 vdev_config_dirty(tvd);
3855 }
3856
3857 if (nspares != 0) {
3858 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
3859 ZPOOL_CONFIG_SPARES);
3860 spa_load_spares(spa);
3861 spa->spa_spares.sav_sync = B_TRUE;
3862 }
3863
3864 if (nl2cache != 0) {
3865 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
3866 ZPOOL_CONFIG_L2CACHE);
3867 spa_load_l2cache(spa);
3868 spa->spa_l2cache.sav_sync = B_TRUE;
3869 }
3870
3871 /*
3872 * We have to be careful when adding new vdevs to an existing pool.
3873 * If other threads start allocating from these vdevs before we
3874 * sync the config cache, and we lose power, then upon reboot we may
3875 * fail to open the pool because there are DVAs that the config cache
3876 * can't translate. Therefore, we first add the vdevs without
3877 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
3878 * and then let spa_config_update() initialize the new metaslabs.
3879 *
3880 * spa_load() checks for added-but-not-initialized vdevs, so that
3881 * if we lose power at any point in this sequence, the remaining
3882 * steps will be completed the next time we load the pool.
3883 */
3884 (void) spa_vdev_exit(spa, vd, txg, 0);
3885
3886 mutex_enter(&spa_namespace_lock);
3887 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3888 mutex_exit(&spa_namespace_lock);
3889
3890 return (0);
3891}
3892
3893/*
3894 * Attach a device to a mirror. The arguments are the path to any device
3895 * in the mirror, and the nvroot for the new device. If the path specifies
3896 * a device that is not mirrored, we automatically insert the mirror vdev.
3897 *
3898 * If 'replacing' is specified, the new device is intended to replace the
3899 * existing device; in this case the two devices are made into their own
3900 * mirror using the 'replacing' vdev, which is functionally identical to
3901 * the mirror vdev (it actually reuses all the same ops) but has a few
3902 * extra rules: you can't attach to it after it's been created, and upon
3903 * completion of resilvering, the first disk (the one being replaced)
3904 * is automatically detached.
3905 */
3906int
3907spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3908{
3909 uint64_t txg, dtl_max_txg;
3910 vdev_t *rvd = spa->spa_root_vdev;
3911 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
3912 vdev_ops_t *pvops;
3913 char *oldvdpath, *newvdpath;
3914 int newvd_isspare;
3915 int error;
3916
3917 ASSERT(spa_writeable(spa));
3918
3919 txg = spa_vdev_enter(spa);
3920
3921 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3922
3923 if (oldvd == NULL)
3924 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3925
3926 if (!oldvd->vdev_ops->vdev_op_leaf)
3927 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3928
3929 pvd = oldvd->vdev_parent;
3930
3931 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
3932 VDEV_ALLOC_ADD)) != 0)
3933 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
3934
3935 if (newrootvd->vdev_children != 1)
3936 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3937
3938 newvd = newrootvd->vdev_child[0];
3939
3940 if (!newvd->vdev_ops->vdev_op_leaf)
3941 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3942
3943 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3944 return (spa_vdev_exit(spa, newrootvd, txg, error));
3945
3946 /*
3947 * Spares can't replace logs
3948 */
3949 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
3950 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3951
3952 if (!replacing) {
3953 /*
3954 * For attach, the only allowable parent is a mirror or the root
3955 * vdev.
3956 */
3957 if (pvd->vdev_ops != &vdev_mirror_ops &&
3958 pvd->vdev_ops != &vdev_root_ops)
3959 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3960
3961 pvops = &vdev_mirror_ops;
3962 } else {
3963 /*
3964 * Active hot spares can only be replaced by inactive hot
3965 * spares.
3966 */
3967 if (pvd->vdev_ops == &vdev_spare_ops &&
3968 oldvd->vdev_isspare &&
3969 !spa_has_spare(spa, newvd->vdev_guid))
3970 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3971
3972 /*
3973 * If the source is a hot spare, and the parent isn't already a
3974 * spare, then we want to create a new hot spare. Otherwise, we
3975 * want to create a replacing vdev. The user is not allowed to
3976 * attach to a spared vdev child unless the 'isspare' state is
3977 * the same (spare replaces spare, non-spare replaces
3978 * non-spare).
3979 */
3980 if (pvd->vdev_ops == &vdev_replacing_ops &&
3981 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
3982 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3983 } else if (pvd->vdev_ops == &vdev_spare_ops &&
3984 newvd->vdev_isspare != oldvd->vdev_isspare) {
3985 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3986 }
3987
3988 if (newvd->vdev_isspare)
3989 pvops = &vdev_spare_ops;
3990 else
3991 pvops = &vdev_replacing_ops;
3992 }
3993
3994 /*
3995 * Make sure the new device is big enough.
3996 */
3997 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3998 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3999
4000 /*
4001 * The new device cannot have a higher alignment requirement
4002 * than the top-level vdev.
4003 */
4004 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4005 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4006
4007 /*
4008 * If this is an in-place replacement, update oldvd's path and devid
4009 * to make it distinguishable from newvd, and unopenable from now on.
4010 */
4011 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4012 spa_strfree(oldvd->vdev_path);
4013 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4014 KM_SLEEP);
4015 (void) sprintf(oldvd->vdev_path, "%s/%s",
4016 newvd->vdev_path, "old");
4017 if (oldvd->vdev_devid != NULL) {
4018 spa_strfree(oldvd->vdev_devid);
4019 oldvd->vdev_devid = NULL;
4020 }
4021 }
4022
4023 /* mark the device being resilvered */
4024 newvd->vdev_resilvering = B_TRUE;
4025
4026 /*
4027 * If the parent is not a mirror, or if we're replacing, insert the new
4028 * mirror/replacing/spare vdev above oldvd.
4029 */
4030 if (pvd->vdev_ops != pvops)
4031 pvd = vdev_add_parent(oldvd, pvops);
4032
4033 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4034 ASSERT(pvd->vdev_ops == pvops);
4035 ASSERT(oldvd->vdev_parent == pvd);
4036
4037 /*
4038 * Extract the new device from its root and add it to pvd.
4039 */
4040 vdev_remove_child(newrootvd, newvd);
4041 newvd->vdev_id = pvd->vdev_children;
4042 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4043 vdev_add_child(pvd, newvd);
4044
4045 tvd = newvd->vdev_top;
4046 ASSERT(pvd->vdev_top == tvd);
4047 ASSERT(tvd->vdev_parent == rvd);
4048
4049 vdev_config_dirty(tvd);
4050
4051 /*
4052 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4053 * for any dmu_sync-ed blocks. It will propagate upward when
4054 * spa_vdev_exit() calls vdev_dtl_reassess().
4055 */
4056 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4057
4058 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4059 dtl_max_txg - TXG_INITIAL);
4060
4061 if (newvd->vdev_isspare) {
4062 spa_spare_activate(newvd);
4063 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4064 }
4065
4066 oldvdpath = spa_strdup(oldvd->vdev_path);
4067 newvdpath = spa_strdup(newvd->vdev_path);
4068 newvd_isspare = newvd->vdev_isspare;
4069
4070 /*
4071 * Mark newvd's DTL dirty in this txg.
4072 */
4073 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4074
4075 /*
4076 * Restart the resilver
4077 */
4078 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4079
4080 /*
4081 * Commit the config
4082 */
4083 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4084
4085 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4086 "%s vdev=%s %s vdev=%s",
4087 replacing && newvd_isspare ? "spare in" :
4088 replacing ? "replace" : "attach", newvdpath,
4089 replacing ? "for" : "to", oldvdpath);
4090
4091 spa_strfree(oldvdpath);
4092 spa_strfree(newvdpath);
4093
4094 if (spa->spa_bootfs)
4095 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4096
4097 return (0);
4098}
4099
4100/*
4101 * Detach a device from a mirror or replacing vdev.
4102 * If 'replace_done' is specified, only detach if the parent
4103 * is a replacing vdev.
4104 */
4105int
4106spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4107{
4108 uint64_t txg;
4109 int error;
4110 vdev_t *rvd = spa->spa_root_vdev;
4111 vdev_t *vd, *pvd, *cvd, *tvd;
4112 boolean_t unspare = B_FALSE;
4113 uint64_t unspare_guid;
4114 char *vdpath;
4115
4116 ASSERT(spa_writeable(spa));
4117
4118 txg = spa_vdev_enter(spa);
4119
4120 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4121
4122 if (vd == NULL)
4123 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4124
4125 if (!vd->vdev_ops->vdev_op_leaf)
4126 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4127
4128 pvd = vd->vdev_parent;
4129
4130 /*
4131 * If the parent/child relationship is not as expected, don't do it.
4132 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4133 * vdev that's replacing B with C. The user's intent in replacing
4134 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4135 * the replace by detaching C, the expected behavior is to end up
4136 * M(A,B). But suppose that right after deciding to detach C,
4137 * the replacement of B completes. We would have M(A,C), and then
4138 * ask to detach C, which would leave us with just A -- not what
4139 * the user wanted. To prevent this, we make sure that the
4140 * parent/child relationship hasn't changed -- in this example,
4141 * that C's parent is still the replacing vdev R.
4142 */
4143 if (pvd->vdev_guid != pguid && pguid != 0)
4144 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4145
4146 /*
4147 * Only 'replacing' or 'spare' vdevs can be replaced.
4148 */
4149 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4150 pvd->vdev_ops != &vdev_spare_ops)
4151 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4152
4153 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4154 spa_version(spa) >= SPA_VERSION_SPARES);
4155
4156 /*
4157 * Only mirror, replacing, and spare vdevs support detach.
4158 */
4159 if (pvd->vdev_ops != &vdev_replacing_ops &&
4160 pvd->vdev_ops != &vdev_mirror_ops &&
4161 pvd->vdev_ops != &vdev_spare_ops)
4162 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4163
4164 /*
4165 * If this device has the only valid copy of some data,
4166 * we cannot safely detach it.
4167 */
4168 if (vdev_dtl_required(vd))
4169 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4170
4171 ASSERT(pvd->vdev_children >= 2);
4172
4173 /*
4174 * If we are detaching the second disk from a replacing vdev, then
4175 * check to see if we changed the original vdev's path to have "/old"
4176 * at the end in spa_vdev_attach(). If so, undo that change now.
4177 */
4178 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4179 vd->vdev_path != NULL) {
4180 size_t len = strlen(vd->vdev_path);
4181
4182 for (int c = 0; c < pvd->vdev_children; c++) {
4183 cvd = pvd->vdev_child[c];
4184
4185 if (cvd == vd || cvd->vdev_path == NULL)
4186 continue;
4187
4188 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4189 strcmp(cvd->vdev_path + len, "/old") == 0) {
4190 spa_strfree(cvd->vdev_path);
4191 cvd->vdev_path = spa_strdup(vd->vdev_path);
4192 break;
4193 }
4194 }
4195 }
4196
4197 /*
4198 * If we are detaching the original disk from a spare, then it implies
4199 * that the spare should become a real disk, and be removed from the
4200 * active spare list for the pool.
4201 */
4202 if (pvd->vdev_ops == &vdev_spare_ops &&
4203 vd->vdev_id == 0 &&
4204 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4205 unspare = B_TRUE;
4206
4207 /*
4208 * Erase the disk labels so the disk can be used for other things.
4209 * This must be done after all other error cases are handled,
4210 * but before we disembowel vd (so we can still do I/O to it).
4211 * But if we can't do it, don't treat the error as fatal --
4212 * it may be that the unwritability of the disk is the reason
4213 * it's being detached!
4214 */
4215 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4216
4217 /*
4218 * Remove vd from its parent and compact the parent's children.
4219 */
4220 vdev_remove_child(pvd, vd);
4221 vdev_compact_children(pvd);
4222
4223 /*
4224 * Remember one of the remaining children so we can get tvd below.
4225 */
4226 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4227
4228 /*
4229 * If we need to remove the remaining child from the list of hot spares,
4230 * do it now, marking the vdev as no longer a spare in the process.
4231 * We must do this before vdev_remove_parent(), because that can
4232 * change the GUID if it creates a new toplevel GUID. For a similar
4233 * reason, we must remove the spare now, in the same txg as the detach;
4234 * otherwise someone could attach a new sibling, change the GUID, and
4235 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4236 */
4237 if (unspare) {
4238 ASSERT(cvd->vdev_isspare);
4239 spa_spare_remove(cvd);
4240 unspare_guid = cvd->vdev_guid;
4241 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4242 cvd->vdev_unspare = B_TRUE;
4243 }
4244
4245 /*
4246 * If the parent mirror/replacing vdev only has one child,
4247 * the parent is no longer needed. Remove it from the tree.
4248 */
4249 if (pvd->vdev_children == 1) {
4250 if (pvd->vdev_ops == &vdev_spare_ops)
4251 cvd->vdev_unspare = B_FALSE;
4252 vdev_remove_parent(cvd);
4253 cvd->vdev_resilvering = B_FALSE;
4254 }
4255
4256
4257 /*
4258 * We don't set tvd until now because the parent we just removed
4259 * may have been the previous top-level vdev.
4260 */
4261 tvd = cvd->vdev_top;
4262 ASSERT(tvd->vdev_parent == rvd);
4263
4264 /*
4265 * Reevaluate the parent vdev state.
4266 */
4267 vdev_propagate_state(cvd);
4268
4269 /*
4270 * If the 'autoexpand' property is set on the pool then automatically
4271 * try to expand the size of the pool. For example if the device we
4272 * just detached was smaller than the others, it may be possible to
4273 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4274 * first so that we can obtain the updated sizes of the leaf vdevs.
4275 */
4276 if (spa->spa_autoexpand) {
4277 vdev_reopen(tvd);
4278 vdev_expand(tvd, txg);
4279 }
4280
4281 vdev_config_dirty(tvd);
4282
4283 /*
4284 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4285 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4286 * But first make sure we're not on any *other* txg's DTL list, to
4287 * prevent vd from being accessed after it's freed.
4288 */
4289 vdpath = spa_strdup(vd->vdev_path);
4290 for (int t = 0; t < TXG_SIZE; t++)
4291 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4292 vd->vdev_detached = B_TRUE;
4293 vdev_dirty(tvd, VDD_DTL, vd, txg);
4294
4295 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4296
4297 /* hang on to the spa before we release the lock */
4298 spa_open_ref(spa, FTAG);
4299
4300 error = spa_vdev_exit(spa, vd, txg, 0);
4301
4302 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4303 "vdev=%s", vdpath);
4304 spa_strfree(vdpath);
4305
4306 /*
4307 * If this was the removal of the original device in a hot spare vdev,
4308 * then we want to go through and remove the device from the hot spare
4309 * list of every other pool.
4310 */
4311 if (unspare) {
4312 spa_t *altspa = NULL;
4313
4314 mutex_enter(&spa_namespace_lock);
4315 while ((altspa = spa_next(altspa)) != NULL) {
4316 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4317 altspa == spa)
4318 continue;
4319
4320 spa_open_ref(altspa, FTAG);
4321 mutex_exit(&spa_namespace_lock);
4322 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4323 mutex_enter(&spa_namespace_lock);
4324 spa_close(altspa, FTAG);
4325 }
4326 mutex_exit(&spa_namespace_lock);
4327
4328 /* search the rest of the vdevs for spares to remove */
4329 spa_vdev_resilver_done(spa);
4330 }
4331
4332 /* all done with the spa; OK to release */
4333 mutex_enter(&spa_namespace_lock);
4334 spa_close(spa, FTAG);
4335 mutex_exit(&spa_namespace_lock);
4336
4337 return (error);
4338}
4339
4340/*
4341 * Split a set of devices from their mirrors, and create a new pool from them.
4342 */
4343int
4344spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4345 nvlist_t *props, boolean_t exp)
4346{
4347 int error = 0;
4348 uint64_t txg, *glist;
4349 spa_t *newspa;
4350 uint_t c, children, lastlog;
4351 nvlist_t **child, *nvl, *tmp;
4352 dmu_tx_t *tx;
4353 char *altroot = NULL;
4354 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4355 boolean_t activate_slog;
4356
4357 ASSERT(spa_writeable(spa));
4358
4359 txg = spa_vdev_enter(spa);
4360
4361 /* clear the log and flush everything up to now */
4362 activate_slog = spa_passivate_log(spa);
4363 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4364 error = spa_offline_log(spa);
4365 txg = spa_vdev_config_enter(spa);
4366
4367 if (activate_slog)
4368 spa_activate_log(spa);
4369
4370 if (error != 0)
4371 return (spa_vdev_exit(spa, NULL, txg, error));
4372
4373 /* check new spa name before going any further */
4374 if (spa_lookup(newname) != NULL)
4375 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4376
4377 /*
4378 * scan through all the children to ensure they're all mirrors
4379 */
4380 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4381 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4382 &children) != 0)
4383 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4384
4385 /* first, check to ensure we've got the right child count */
4386 rvd = spa->spa_root_vdev;
4387 lastlog = 0;
4388 for (c = 0; c < rvd->vdev_children; c++) {
4389 vdev_t *vd = rvd->vdev_child[c];
4390
4391 /* don't count the holes & logs as children */
4392 if (vd->vdev_islog || vd->vdev_ishole) {
4393 if (lastlog == 0)
4394 lastlog = c;
4395 continue;
4396 }
4397
4398 lastlog = 0;
4399 }
4400 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4401 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4402
4403 /* next, ensure no spare or cache devices are part of the split */
4404 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4405 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4406 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4407
4408 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
4409 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
4410
4411 /* then, loop over each vdev and validate it */
4412 for (c = 0; c < children; c++) {
4413 uint64_t is_hole = 0;
4414
4415 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4416 &is_hole);
4417
4418 if (is_hole != 0) {
4419 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4420 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4421 continue;
4422 } else {
4423 error = EINVAL;
4424 break;
4425 }
4426 }
4427
4428 /* which disk is going to be split? */
4429 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4430 &glist[c]) != 0) {
4431 error = EINVAL;
4432 break;
4433 }
4434
4435 /* look it up in the spa */
4436 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4437 if (vml[c] == NULL) {
4438 error = ENODEV;
4439 break;
4440 }
4441
4442 /* make sure there's nothing stopping the split */
4443 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4444 vml[c]->vdev_islog ||
4445 vml[c]->vdev_ishole ||
4446 vml[c]->vdev_isspare ||
4447 vml[c]->vdev_isl2cache ||
4448 !vdev_writeable(vml[c]) ||
4449 vml[c]->vdev_children != 0 ||
4450 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4451 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4452 error = EINVAL;
4453 break;
4454 }
4455
4456 if (vdev_dtl_required(vml[c])) {
4457 error = EBUSY;
4458 break;
4459 }
4460
4461 /* we need certain info from the top level */
4462 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4463 vml[c]->vdev_top->vdev_ms_array) == 0);
4464 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4465 vml[c]->vdev_top->vdev_ms_shift) == 0);
4466 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4467 vml[c]->vdev_top->vdev_asize) == 0);
4468 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4469 vml[c]->vdev_top->vdev_ashift) == 0);
4470 }
4471
4472 if (error != 0) {
4473 kmem_free(vml, children * sizeof (vdev_t *));
4474 kmem_free(glist, children * sizeof (uint64_t));
4475 return (spa_vdev_exit(spa, NULL, txg, error));
4476 }
4477
4478 /* stop writers from using the disks */
4479 for (c = 0; c < children; c++) {
4480 if (vml[c] != NULL)
4481 vml[c]->vdev_offline = B_TRUE;
4482 }
4483 vdev_reopen(spa->spa_root_vdev);
4484
4485 /*
4486 * Temporarily record the splitting vdevs in the spa config. This
4487 * will disappear once the config is regenerated.
4488 */
4489 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4490 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4491 glist, children) == 0);
4492 kmem_free(glist, children * sizeof (uint64_t));
4493
4494 mutex_enter(&spa->spa_props_lock);
4495 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4496 nvl) == 0);
4497 mutex_exit(&spa->spa_props_lock);
4498 spa->spa_config_splitting = nvl;
4499 vdev_config_dirty(spa->spa_root_vdev);
4500
4501 /* configure and create the new pool */
4502 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4503 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4504 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4505 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4506 spa_version(spa)) == 0);
4507 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4508 spa->spa_config_txg) == 0);
4509 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4510 spa_generate_guid(NULL)) == 0);
4511 (void) nvlist_lookup_string(props,
4512 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4513
4514 /* add the new pool to the namespace */
4515 newspa = spa_add(newname, config, altroot);
4516 newspa->spa_config_txg = spa->spa_config_txg;
4517 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4518
4519 /* release the spa config lock, retaining the namespace lock */
4520 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4521
4522 if (zio_injection_enabled)
4523 zio_handle_panic_injection(spa, FTAG, 1);
4524
4525 spa_activate(newspa, spa_mode_global);
4526 spa_async_suspend(newspa);
4527
4528#ifndef sun
4529 /* mark that we are creating new spa by splitting */
4530 newspa->spa_splitting_newspa = B_TRUE;
4531#endif
4532 /* create the new pool from the disks of the original pool */
4533 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4534#ifndef sun
4535 newspa->spa_splitting_newspa = B_FALSE;
4536#endif
4537 if (error)
4538 goto out;
4539
4540 /* if that worked, generate a real config for the new pool */
4541 if (newspa->spa_root_vdev != NULL) {
4542 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4543 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4544 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4545 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4546 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4547 B_TRUE));
4548 }
4549
4550 /* set the props */
4551 if (props != NULL) {
4552 spa_configfile_set(newspa, props, B_FALSE);
4553 error = spa_prop_set(newspa, props);
4554 if (error)
4555 goto out;
4556 }
4557
4558 /* flush everything */
4559 txg = spa_vdev_config_enter(newspa);
4560 vdev_config_dirty(newspa->spa_root_vdev);
4561 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4562
4563 if (zio_injection_enabled)
4564 zio_handle_panic_injection(spa, FTAG, 2);
4565
4566 spa_async_resume(newspa);
4567
4568 /* finally, update the original pool's config */
4569 txg = spa_vdev_config_enter(spa);
4570 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4571 error = dmu_tx_assign(tx, TXG_WAIT);
4572 if (error != 0)
4573 dmu_tx_abort(tx);
4574 for (c = 0; c < children; c++) {
4575 if (vml[c] != NULL) {
4576 vdev_split(vml[c]);
4577 if (error == 0)
4578 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4579 spa, tx, "vdev=%s",
4580 vml[c]->vdev_path);
4581 vdev_free(vml[c]);
4582 }
4583 }
4584 vdev_config_dirty(spa->spa_root_vdev);
4585 spa->spa_config_splitting = NULL;
4586 nvlist_free(nvl);
4587 if (error == 0)
4588 dmu_tx_commit(tx);
4589 (void) spa_vdev_exit(spa, NULL, txg, 0);
4590
4591 if (zio_injection_enabled)
4592 zio_handle_panic_injection(spa, FTAG, 3);
4593
4594 /* split is complete; log a history record */
4595 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4596 "split new pool %s from pool %s", newname, spa_name(spa));
4597
4598 kmem_free(vml, children * sizeof (vdev_t *));
4599
4600 /* if we're not going to mount the filesystems in userland, export */
4601 if (exp)
4602 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4603 B_FALSE, B_FALSE);
4604
4605 return (error);
4606
4607out:
4608 spa_unload(newspa);
4609 spa_deactivate(newspa);
4610 spa_remove(newspa);
4611
4612 txg = spa_vdev_config_enter(spa);
4613
4614 /* re-online all offlined disks */
4615 for (c = 0; c < children; c++) {
4616 if (vml[c] != NULL)
4617 vml[c]->vdev_offline = B_FALSE;
4618 }
4619 vdev_reopen(spa->spa_root_vdev);
4620
4621 nvlist_free(spa->spa_config_splitting);
4622 spa->spa_config_splitting = NULL;
4623 (void) spa_vdev_exit(spa, NULL, txg, error);
4624
4625 kmem_free(vml, children * sizeof (vdev_t *));
4626 return (error);
4627}
4628
4629static nvlist_t *
4630spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4631{
4632 for (int i = 0; i < count; i++) {
4633 uint64_t guid;
4634
4635 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4636 &guid) == 0);
4637
4638 if (guid == target_guid)
4639 return (nvpp[i]);
4640 }
4641
4642 return (NULL);
4643}
4644
4645static void
4646spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4647 nvlist_t *dev_to_remove)
4648{
4649 nvlist_t **newdev = NULL;
4650
4651 if (count > 1)
4652 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
4653
4654 for (int i = 0, j = 0; i < count; i++) {
4655 if (dev[i] == dev_to_remove)
4656 continue;
4657 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
4658 }
4659
4660 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
4661 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
4662
4663 for (int i = 0; i < count - 1; i++)
4664 nvlist_free(newdev[i]);
4665
4666 if (count > 1)
4667 kmem_free(newdev, (count - 1) * sizeof (void *));
4668}
4669
4670/*
4671 * Evacuate the device.
4672 */
4673static int
4674spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
4675{
4676 uint64_t txg;
4677 int error = 0;
4678
4679 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4680 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4681 ASSERT(vd == vd->vdev_top);
4682
4683 /*
4684 * Evacuate the device. We don't hold the config lock as writer
4685 * since we need to do I/O but we do keep the
4686 * spa_namespace_lock held. Once this completes the device
4687 * should no longer have any blocks allocated on it.
4688 */
4689 if (vd->vdev_islog) {
4690 if (vd->vdev_stat.vs_alloc != 0)
4691 error = spa_offline_log(spa);
4692 } else {
4693 error = ENOTSUP;
4694 }
4695
4696 if (error)
4697 return (error);
4698
4699 /*
4700 * The evacuation succeeded. Remove any remaining MOS metadata
4701 * associated with this vdev, and wait for these changes to sync.
4702 */
4703 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
4704 txg = spa_vdev_config_enter(spa);
4705 vd->vdev_removing = B_TRUE;
4706 vdev_dirty(vd, 0, NULL, txg);
4707 vdev_config_dirty(vd);
4708 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4709
4710 return (0);
4711}
4712
4713/*
4714 * Complete the removal by cleaning up the namespace.
4715 */
4716static void
4717spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
4718{
4719 vdev_t *rvd = spa->spa_root_vdev;
4720 uint64_t id = vd->vdev_id;
4721 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
4722
4723 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4724 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4725 ASSERT(vd == vd->vdev_top);
4726
4727 /*
4728 * Only remove any devices which are empty.
4729 */
4730 if (vd->vdev_stat.vs_alloc != 0)
4731 return;
4732
4733 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4734
4735 if (list_link_active(&vd->vdev_state_dirty_node))
4736 vdev_state_clean(vd);
4737 if (list_link_active(&vd->vdev_config_dirty_node))
4738 vdev_config_clean(vd);
4739
4740 vdev_free(vd);
4741
4742 if (last_vdev) {
4743 vdev_compact_children(rvd);
4744 } else {
4745 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
4746 vdev_add_child(rvd, vd);
4747 }
4748 vdev_config_dirty(rvd);
4749
4750 /*
4751 * Reassess the health of our root vdev.
4752 */
4753 vdev_reopen(rvd);
4754}
4755
4756/*
4757 * Remove a device from the pool -
4758 *
4759 * Removing a device from the vdev namespace requires several steps
4760 * and can take a significant amount of time. As a result we use
4761 * the spa_vdev_config_[enter/exit] functions which allow us to
4762 * grab and release the spa_config_lock while still holding the namespace
4763 * lock. During each step the configuration is synced out.
4764 */
4765
4766/*
4767 * Remove a device from the pool. Currently, this supports removing only hot
4768 * spares, slogs, and level 2 ARC devices.
4769 */
4770int
4771spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
4772{
4773 vdev_t *vd;
4774 metaslab_group_t *mg;
4775 nvlist_t **spares, **l2cache, *nv;
4776 uint64_t txg = 0;
4777 uint_t nspares, nl2cache;
4778 int error = 0;
4779 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
4780
4781 ASSERT(spa_writeable(spa));
4782
4783 if (!locked)
4784 txg = spa_vdev_enter(spa);
4785
4786 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4787
4788 if (spa->spa_spares.sav_vdevs != NULL &&
4789 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
4790 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
4791 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
4792 /*
4793 * Only remove the hot spare if it's not currently in use
4794 * in this pool.
4795 */
4796 if (vd == NULL || unspare) {
4797 spa_vdev_remove_aux(spa->spa_spares.sav_config,
4798 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
4799 spa_load_spares(spa);
4800 spa->spa_spares.sav_sync = B_TRUE;
4801 } else {
4802 error = EBUSY;
4803 }
4804 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
4805 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4806 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
4807 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
4808 /*
4809 * Cache devices can always be removed.
4810 */
4811 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
4812 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
4813 spa_load_l2cache(spa);
4814 spa->spa_l2cache.sav_sync = B_TRUE;
4815 } else if (vd != NULL && vd->vdev_islog) {
4816 ASSERT(!locked);
4817 ASSERT(vd == vd->vdev_top);
4818
4819 /*
4820 * XXX - Once we have bp-rewrite this should
4821 * become the common case.
4822 */
4823
4824 mg = vd->vdev_mg;
4825
4826 /*
4827 * Stop allocating from this vdev.
4828 */
4829 metaslab_group_passivate(mg);
4830
4831 /*
4832 * Wait for the youngest allocations and frees to sync,
4833 * and then wait for the deferral of those frees to finish.
4834 */
4835 spa_vdev_config_exit(spa, NULL,
4836 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
4837
4838 /*
4839 * Attempt to evacuate the vdev.
4840 */
4841 error = spa_vdev_remove_evacuate(spa, vd);
4842
4843 txg = spa_vdev_config_enter(spa);
4844
4845 /*
4846 * If we couldn't evacuate the vdev, unwind.
4847 */
4848 if (error) {
4849 metaslab_group_activate(mg);
4850 return (spa_vdev_exit(spa, NULL, txg, error));
4851 }
4852
4853 /*
4854 * Clean up the vdev namespace.
4855 */
4856 spa_vdev_remove_from_namespace(spa, vd);
4857
4858 } else if (vd != NULL) {
4859 /*
4860 * Normal vdevs cannot be removed (yet).
4861 */
4862 error = ENOTSUP;
4863 } else {
4864 /*
4865 * There is no vdev of any kind with the specified guid.
4866 */
4867 error = ENOENT;
4868 }
4869
4870 if (!locked)
4871 return (spa_vdev_exit(spa, NULL, txg, error));
4872
4873 return (error);
4874}
4875
4876/*
4877 * Find any device that's done replacing, or a vdev marked 'unspare' that's
4878 * current spared, so we can detach it.
4879 */
4880static vdev_t *
4881spa_vdev_resilver_done_hunt(vdev_t *vd)
4882{
4883 vdev_t *newvd, *oldvd;
4884
4885 for (int c = 0; c < vd->vdev_children; c++) {
4886 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
4887 if (oldvd != NULL)
4888 return (oldvd);
4889 }
4890
4891 /*
4892 * Check for a completed replacement. We always consider the first
4893 * vdev in the list to be the oldest vdev, and the last one to be
4894 * the newest (see spa_vdev_attach() for how that works). In
4895 * the case where the newest vdev is faulted, we will not automatically
4896 * remove it after a resilver completes. This is OK as it will require
4897 * user intervention to determine which disk the admin wishes to keep.
4898 */
4899 if (vd->vdev_ops == &vdev_replacing_ops) {
4900 ASSERT(vd->vdev_children > 1);
4901
4902 newvd = vd->vdev_child[vd->vdev_children - 1];
4903 oldvd = vd->vdev_child[0];
4904
4905 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
4906 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4907 !vdev_dtl_required(oldvd))
4908 return (oldvd);
4909 }
4910
4911 /*
4912 * Check for a completed resilver with the 'unspare' flag set.
4913 */
4914 if (vd->vdev_ops == &vdev_spare_ops) {
4915 vdev_t *first = vd->vdev_child[0];
4916 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
4917
4918 if (last->vdev_unspare) {
4919 oldvd = first;
4920 newvd = last;
4921 } else if (first->vdev_unspare) {
4922 oldvd = last;
4923 newvd = first;
4924 } else {
4925 oldvd = NULL;
4926 }
4927
4928 if (oldvd != NULL &&
4929 vdev_dtl_empty(newvd, DTL_MISSING) &&
4930 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4931 !vdev_dtl_required(oldvd))
4932 return (oldvd);
4933
4934 /*
4935 * If there are more than two spares attached to a disk,
4936 * and those spares are not required, then we want to
4937 * attempt to free them up now so that they can be used
4938 * by other pools. Once we're back down to a single
4939 * disk+spare, we stop removing them.
4940 */
4941 if (vd->vdev_children > 2) {
4942 newvd = vd->vdev_child[1];
4943
4944 if (newvd->vdev_isspare && last->vdev_isspare &&
4945 vdev_dtl_empty(last, DTL_MISSING) &&
4946 vdev_dtl_empty(last, DTL_OUTAGE) &&
4947 !vdev_dtl_required(newvd))
4948 return (newvd);
4949 }
4950 }
4951
4952 return (NULL);
4953}
4954
4955static void
4956spa_vdev_resilver_done(spa_t *spa)
4957{
4958 vdev_t *vd, *pvd, *ppvd;
4959 uint64_t guid, sguid, pguid, ppguid;
4960
4961 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4962
4963 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
4964 pvd = vd->vdev_parent;
4965 ppvd = pvd->vdev_parent;
4966 guid = vd->vdev_guid;
4967 pguid = pvd->vdev_guid;
4968 ppguid = ppvd->vdev_guid;
4969 sguid = 0;
4970 /*
4971 * If we have just finished replacing a hot spared device, then
4972 * we need to detach the parent's first child (the original hot
4973 * spare) as well.
4974 */
4975 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
4976 ppvd->vdev_children == 2) {
4977 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
4978 sguid = ppvd->vdev_child[1]->vdev_guid;
4979 }
4980 spa_config_exit(spa, SCL_ALL, FTAG);
4981 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
4982 return;
4983 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
4984 return;
4985 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4986 }
4987
4988 spa_config_exit(spa, SCL_ALL, FTAG);
4989}
4990
4991/*
4992 * Update the stored path or FRU for this vdev.
4993 */
4994int
4995spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
4996 boolean_t ispath)
4997{
4998 vdev_t *vd;
4999 boolean_t sync = B_FALSE;
5000
5001 ASSERT(spa_writeable(spa));
5002
5003 spa_vdev_state_enter(spa, SCL_ALL);
5004
5005 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5006 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5007
5008 if (!vd->vdev_ops->vdev_op_leaf)
5009 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5010
5011 if (ispath) {
5012 if (strcmp(value, vd->vdev_path) != 0) {
5013 spa_strfree(vd->vdev_path);
5014 vd->vdev_path = spa_strdup(value);
5015 sync = B_TRUE;
5016 }
5017 } else {
5018 if (vd->vdev_fru == NULL) {
5019 vd->vdev_fru = spa_strdup(value);
5020 sync = B_TRUE;
5021 } else if (strcmp(value, vd->vdev_fru) != 0) {
5022 spa_strfree(vd->vdev_fru);
5023 vd->vdev_fru = spa_strdup(value);
5024 sync = B_TRUE;
5025 }
5026 }
5027
5028 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5029}
5030
5031int
5032spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5033{
5034 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5035}
5036
5037int
5038spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5039{
5040 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5041}
5042
5043/*
5044 * ==========================================================================
5045 * SPA Scanning
5046 * ==========================================================================
5047 */
5048
5049int
5050spa_scan_stop(spa_t *spa)
5051{
5052 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5053 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5054 return (EBUSY);
5055 return (dsl_scan_cancel(spa->spa_dsl_pool));
5056}
5057
5058int
5059spa_scan(spa_t *spa, pool_scan_func_t func)
5060{
5061 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5062
5063 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5064 return (ENOTSUP);
5065
5066 /*
5067 * If a resilver was requested, but there is no DTL on a
5068 * writeable leaf device, we have nothing to do.
5069 */
5070 if (func == POOL_SCAN_RESILVER &&
5071 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5072 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5073 return (0);
5074 }
5075
5076 return (dsl_scan(spa->spa_dsl_pool, func));
5077}
5078
5079/*
5080 * ==========================================================================
5081 * SPA async task processing
5082 * ==========================================================================
5083 */
5084
5085static void
5086spa_async_remove(spa_t *spa, vdev_t *vd)
5087{
5088 if (vd->vdev_remove_wanted) {
5089 vd->vdev_remove_wanted = B_FALSE;
5090 vd->vdev_delayed_close = B_FALSE;
5091 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5092
5093 /*
5094 * We want to clear the stats, but we don't want to do a full
5095 * vdev_clear() as that will cause us to throw away
5096 * degraded/faulted state as well as attempt to reopen the
5097 * device, all of which is a waste.
5098 */
5099 vd->vdev_stat.vs_read_errors = 0;
5100 vd->vdev_stat.vs_write_errors = 0;
5101 vd->vdev_stat.vs_checksum_errors = 0;
5102
5103 vdev_state_dirty(vd->vdev_top);
5104 }
5105
5106 for (int c = 0; c < vd->vdev_children; c++)
5107 spa_async_remove(spa, vd->vdev_child[c]);
5108}
5109
5110static void
5111spa_async_probe(spa_t *spa, vdev_t *vd)
5112{
5113 if (vd->vdev_probe_wanted) {
5114 vd->vdev_probe_wanted = B_FALSE;
5115 vdev_reopen(vd); /* vdev_open() does the actual probe */
5116 }
5117
5118 for (int c = 0; c < vd->vdev_children; c++)
5119 spa_async_probe(spa, vd->vdev_child[c]);
5120}
5121
5122static void
5123spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5124{
5125 sysevent_id_t eid;
5126 nvlist_t *attr;
5127 char *physpath;
5128
5129 if (!spa->spa_autoexpand)
5130 return;
5131
5132 for (int c = 0; c < vd->vdev_children; c++) {
5133 vdev_t *cvd = vd->vdev_child[c];
5134 spa_async_autoexpand(spa, cvd);
5135 }
5136
5137 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5138 return;
5139
5140 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5141 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5142
5143 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5144 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5145
5146 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5147 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5148
5149 nvlist_free(attr);
5150 kmem_free(physpath, MAXPATHLEN);
5151}
5152
5153static void
5154spa_async_thread(void *arg)
5155{
5156 spa_t *spa = arg;
5157 int tasks;
5158
5159 ASSERT(spa->spa_sync_on);
5160
5161 mutex_enter(&spa->spa_async_lock);
5162 tasks = spa->spa_async_tasks;
5163 spa->spa_async_tasks = 0;
5164 mutex_exit(&spa->spa_async_lock);
5165
5166 /*
5167 * See if the config needs to be updated.
5168 */
5169 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5170 uint64_t old_space, new_space;
5171
5172 mutex_enter(&spa_namespace_lock);
5173 old_space = metaslab_class_get_space(spa_normal_class(spa));
5174 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5175 new_space = metaslab_class_get_space(spa_normal_class(spa));
5176 mutex_exit(&spa_namespace_lock);
5177
5178 /*
5179 * If the pool grew as a result of the config update,
5180 * then log an internal history event.
5181 */
5182 if (new_space != old_space) {
5183 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5184 spa, NULL,
5185 "pool '%s' size: %llu(+%llu)",
5186 spa_name(spa), new_space, new_space - old_space);
5187 }
5188 }
5189
5190 /*
5191 * See if any devices need to be marked REMOVED.
5192 */
5193 if (tasks & SPA_ASYNC_REMOVE) {
5194 spa_vdev_state_enter(spa, SCL_NONE);
5195 spa_async_remove(spa, spa->spa_root_vdev);
5196 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5197 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5198 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5199 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5200 (void) spa_vdev_state_exit(spa, NULL, 0);
5201 }
5202
5203 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5204 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5205 spa_async_autoexpand(spa, spa->spa_root_vdev);
5206 spa_config_exit(spa, SCL_CONFIG, FTAG);
5207 }
5208
5209 /*
5210 * See if any devices need to be probed.
5211 */
5212 if (tasks & SPA_ASYNC_PROBE) {
5213 spa_vdev_state_enter(spa, SCL_NONE);
5214 spa_async_probe(spa, spa->spa_root_vdev);
5215 (void) spa_vdev_state_exit(spa, NULL, 0);
5216 }
5217
5218 /*
5219 * If any devices are done replacing, detach them.
5220 */
5221 if (tasks & SPA_ASYNC_RESILVER_DONE)
5222 spa_vdev_resilver_done(spa);
5223
5224 /*
5225 * Kick off a resilver.
5226 */
5227 if (tasks & SPA_ASYNC_RESILVER)
5228 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5229
5230 /*
5231 * Let the world know that we're done.
5232 */
5233 mutex_enter(&spa->spa_async_lock);
5234 spa->spa_async_thread = NULL;
5235 cv_broadcast(&spa->spa_async_cv);
5236 mutex_exit(&spa->spa_async_lock);
5237 thread_exit();
5238}
5239
5240void
5241spa_async_suspend(spa_t *spa)
5242{
5243 mutex_enter(&spa->spa_async_lock);
5244 spa->spa_async_suspended++;
5245 while (spa->spa_async_thread != NULL)
5246 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5247 mutex_exit(&spa->spa_async_lock);
5248}
5249
5250void
5251spa_async_resume(spa_t *spa)
5252{
5253 mutex_enter(&spa->spa_async_lock);
5254 ASSERT(spa->spa_async_suspended != 0);
5255 spa->spa_async_suspended--;
5256 mutex_exit(&spa->spa_async_lock);
5257}
5258
5259static void
5260spa_async_dispatch(spa_t *spa)
5261{
5262 mutex_enter(&spa->spa_async_lock);
5263 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5264 spa->spa_async_thread == NULL &&
5265 rootdir != NULL && !vn_is_readonly(rootdir))
5266 spa->spa_async_thread = thread_create(NULL, 0,
5267 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5268 mutex_exit(&spa->spa_async_lock);
5269}
5270
5271void
5272spa_async_request(spa_t *spa, int task)
5273{
5274 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5275 mutex_enter(&spa->spa_async_lock);
5276 spa->spa_async_tasks |= task;
5277 mutex_exit(&spa->spa_async_lock);
5278}
5279
5280/*
5281 * ==========================================================================
5282 * SPA syncing routines
5283 * ==========================================================================
5284 */
5285
5286static int
5287bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5288{
5289 bpobj_t *bpo = arg;
5290 bpobj_enqueue(bpo, bp, tx);
5291 return (0);
5292}
5293
5294static int
5295spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5296{
5297 zio_t *zio = arg;
5298
5299 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5300 zio->io_flags));
5301 return (0);
5302}
5303
5304static void
5305spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5306{
5307 char *packed = NULL;
5308 size_t bufsize;
5309 size_t nvsize = 0;
5310 dmu_buf_t *db;
5311
5312 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5313
5314 /*
5315 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5316 * information. This avoids the dbuf_will_dirty() path and
5317 * saves us a pre-read to get data we don't actually care about.
5318 */
5319 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
5320 packed = kmem_alloc(bufsize, KM_SLEEP);
5321
5322 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5323 KM_SLEEP) == 0);
5324 bzero(packed + nvsize, bufsize - nvsize);
5325
5326 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5327
5328 kmem_free(packed, bufsize);
5329
5330 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5331 dmu_buf_will_dirty(db, tx);
5332 *(uint64_t *)db->db_data = nvsize;
5333 dmu_buf_rele(db, FTAG);
5334}
5335
5336static void
5337spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5338 const char *config, const char *entry)
5339{
5340 nvlist_t *nvroot;
5341 nvlist_t **list;
5342 int i;
5343
5344 if (!sav->sav_sync)
5345 return;
5346
5347 /*
5348 * Update the MOS nvlist describing the list of available devices.
5349 * spa_validate_aux() will have already made sure this nvlist is
5350 * valid and the vdevs are labeled appropriately.
5351 */
5352 if (sav->sav_object == 0) {
5353 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5354 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5355 sizeof (uint64_t), tx);
5356 VERIFY(zap_update(spa->spa_meta_objset,
5357 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5358 &sav->sav_object, tx) == 0);
5359 }
5360
5361 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5362 if (sav->sav_count == 0) {
5363 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5364 } else {
5365 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5366 for (i = 0; i < sav->sav_count; i++)
5367 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5368 B_FALSE, VDEV_CONFIG_L2CACHE);
5369 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5370 sav->sav_count) == 0);
5371 for (i = 0; i < sav->sav_count; i++)
5372 nvlist_free(list[i]);
5373 kmem_free(list, sav->sav_count * sizeof (void *));
5374 }
5375
5376 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5377 nvlist_free(nvroot);
5378
5379 sav->sav_sync = B_FALSE;
5380}
5381
5382static void
5383spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5384{
5385 nvlist_t *config;
5386
5387 if (list_is_empty(&spa->spa_config_dirty_list))
5388 return;
5389
5390 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5391
5392 config = spa_config_generate(spa, spa->spa_root_vdev,
5393 dmu_tx_get_txg(tx), B_FALSE);
5394
5395 spa_config_exit(spa, SCL_STATE, FTAG);
5396
5397 if (spa->spa_config_syncing)
5398 nvlist_free(spa->spa_config_syncing);
5399 spa->spa_config_syncing = config;
5400
5401 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5402}
5403
5404/*
5405 * Set zpool properties.
5406 */
5407static void
5408spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5409{
5410 spa_t *spa = arg1;
5411 objset_t *mos = spa->spa_meta_objset;
5412 nvlist_t *nvp = arg2;
5413 nvpair_t *elem;
5414 uint64_t intval;
5415 char *strval;
5416 zpool_prop_t prop;
5417 const char *propname;
5418 zprop_type_t proptype;
5419
5420 mutex_enter(&spa->spa_props_lock);
5421
5422 elem = NULL;
5423 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5424 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
5425 case ZPOOL_PROP_VERSION:
5426 /*
5427 * Only set version for non-zpool-creation cases
5428 * (set/import). spa_create() needs special care
5429 * for version setting.
5430 */
5431 if (tx->tx_txg != TXG_INITIAL) {
5432 VERIFY(nvpair_value_uint64(elem,
5433 &intval) == 0);
5434 ASSERT(intval <= SPA_VERSION);
5435 ASSERT(intval >= spa_version(spa));
5436 spa->spa_uberblock.ub_version = intval;
5437 vdev_config_dirty(spa->spa_root_vdev);
5438 }
5439 break;
5440
5441 case ZPOOL_PROP_ALTROOT:
5442 /*
5443 * 'altroot' is a non-persistent property. It should
5444 * have been set temporarily at creation or import time.
5445 */
5446 ASSERT(spa->spa_root != NULL);
5447 break;
5448
5449 case ZPOOL_PROP_READONLY:
5450 case ZPOOL_PROP_CACHEFILE:
5451 /*
5452 * 'readonly' and 'cachefile' are also non-persisitent
5453 * properties.
5454 */
5455 break;
5456 case ZPOOL_PROP_COMMENT:
5457 VERIFY(nvpair_value_string(elem, &strval) == 0);
5458 if (spa->spa_comment != NULL)
5459 spa_strfree(spa->spa_comment);
5460 spa->spa_comment = spa_strdup(strval);
5461 /*
5462 * We need to dirty the configuration on all the vdevs
5463 * so that their labels get updated. It's unnecessary
5464 * to do this for pool creation since the vdev's
5465 * configuratoin has already been dirtied.
5466 */
5467 if (tx->tx_txg != TXG_INITIAL)
5468 vdev_config_dirty(spa->spa_root_vdev);
5469 break;
5383 default:
5384 /*
5385 * Set pool property values in the poolprops mos object.
5386 */
5387 if (spa->spa_pool_props_object == 0) {
5388 VERIFY((spa->spa_pool_props_object =
5389 zap_create(mos, DMU_OT_POOL_PROPS,
5390 DMU_OT_NONE, 0, tx)) > 0);
5391
5392 VERIFY(zap_update(mos,
5393 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5394 8, 1, &spa->spa_pool_props_object, tx)
5395 == 0);
5396 }
5397
5398 /* normalize the property name */
5399 propname = zpool_prop_to_name(prop);
5400 proptype = zpool_prop_get_type(prop);
5401
5402 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5403 ASSERT(proptype == PROP_TYPE_STRING);
5404 VERIFY(nvpair_value_string(elem, &strval) == 0);
5405 VERIFY(zap_update(mos,
5406 spa->spa_pool_props_object, propname,
5407 1, strlen(strval) + 1, strval, tx) == 0);
5408
5409 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5410 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5411
5412 if (proptype == PROP_TYPE_INDEX) {
5413 const char *unused;
5414 VERIFY(zpool_prop_index_to_string(
5415 prop, intval, &unused) == 0);
5416 }
5417 VERIFY(zap_update(mos,
5418 spa->spa_pool_props_object, propname,
5419 8, 1, &intval, tx) == 0);
5420 } else {
5421 ASSERT(0); /* not allowed */
5422 }
5423
5424 switch (prop) {
5425 case ZPOOL_PROP_DELEGATION:
5426 spa->spa_delegation = intval;
5427 break;
5428 case ZPOOL_PROP_BOOTFS:
5429 spa->spa_bootfs = intval;
5430 break;
5431 case ZPOOL_PROP_FAILUREMODE:
5432 spa->spa_failmode = intval;
5433 break;
5434 case ZPOOL_PROP_AUTOEXPAND:
5435 spa->spa_autoexpand = intval;
5436 if (tx->tx_txg != TXG_INITIAL)
5437 spa_async_request(spa,
5438 SPA_ASYNC_AUTOEXPAND);
5439 break;
5440 case ZPOOL_PROP_DEDUPDITTO:
5441 spa->spa_dedup_ditto = intval;
5442 break;
5443 default:
5444 break;
5445 }
5446 }
5447
5448 /* log internal history if this is not a zpool create */
5449 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5450 tx->tx_txg != TXG_INITIAL) {
5451 spa_history_log_internal(LOG_POOL_PROPSET,
5452 spa, tx, "%s %lld %s",
5453 nvpair_name(elem), intval, spa_name(spa));
5454 }
5455 }
5456
5457 mutex_exit(&spa->spa_props_lock);
5458}
5459
5460/*
5461 * Perform one-time upgrade on-disk changes. spa_version() does not
5462 * reflect the new version this txg, so there must be no changes this
5463 * txg to anything that the upgrade code depends on after it executes.
5464 * Therefore this must be called after dsl_pool_sync() does the sync
5465 * tasks.
5466 */
5467static void
5468spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5469{
5470 dsl_pool_t *dp = spa->spa_dsl_pool;
5471
5472 ASSERT(spa->spa_sync_pass == 1);
5473
5474 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5475 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5476 dsl_pool_create_origin(dp, tx);
5477
5478 /* Keeping the origin open increases spa_minref */
5479 spa->spa_minref += 3;
5480 }
5481
5482 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5483 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5484 dsl_pool_upgrade_clones(dp, tx);
5485 }
5486
5487 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5488 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5489 dsl_pool_upgrade_dir_clones(dp, tx);
5490
5491 /* Keeping the freedir open increases spa_minref */
5492 spa->spa_minref += 3;
5493 }
5494}
5495
5496/*
5497 * Sync the specified transaction group. New blocks may be dirtied as
5498 * part of the process, so we iterate until it converges.
5499 */
5500void
5501spa_sync(spa_t *spa, uint64_t txg)
5502{
5503 dsl_pool_t *dp = spa->spa_dsl_pool;
5504 objset_t *mos = spa->spa_meta_objset;
5505 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5506 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5507 vdev_t *rvd = spa->spa_root_vdev;
5508 vdev_t *vd;
5509 dmu_tx_t *tx;
5510 int error;
5511
5512 VERIFY(spa_writeable(spa));
5513
5514 /*
5515 * Lock out configuration changes.
5516 */
5517 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5518
5519 spa->spa_syncing_txg = txg;
5520 spa->spa_sync_pass = 0;
5521
5522 /*
5523 * If there are any pending vdev state changes, convert them
5524 * into config changes that go out with this transaction group.
5525 */
5526 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5527 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5528 /*
5529 * We need the write lock here because, for aux vdevs,
5530 * calling vdev_config_dirty() modifies sav_config.
5531 * This is ugly and will become unnecessary when we
5532 * eliminate the aux vdev wart by integrating all vdevs
5533 * into the root vdev tree.
5534 */
5535 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5536 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5537 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5538 vdev_state_clean(vd);
5539 vdev_config_dirty(vd);
5540 }
5541 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5542 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
5543 }
5544 spa_config_exit(spa, SCL_STATE, FTAG);
5545
5546 tx = dmu_tx_create_assigned(dp, txg);
5547
5548 /*
5549 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5550 * set spa_deflate if we have no raid-z vdevs.
5551 */
5552 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
5553 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
5554 int i;
5555
5556 for (i = 0; i < rvd->vdev_children; i++) {
5557 vd = rvd->vdev_child[i];
5558 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
5559 break;
5560 }
5561 if (i == rvd->vdev_children) {
5562 spa->spa_deflate = TRUE;
5563 VERIFY(0 == zap_add(spa->spa_meta_objset,
5564 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5565 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
5566 }
5567 }
5568
5569 /*
5570 * If anything has changed in this txg, or if someone is waiting
5571 * for this txg to sync (eg, spa_vdev_remove()), push the
5572 * deferred frees from the previous txg. If not, leave them
5573 * alone so that we don't generate work on an otherwise idle
5574 * system.
5575 */
5576 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
5577 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
5578 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5579 ((dsl_scan_active(dp->dp_scan) ||
5580 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
5581 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5582 VERIFY3U(bpobj_iterate(defer_bpo,
5583 spa_free_sync_cb, zio, tx), ==, 0);
5584 VERIFY3U(zio_wait(zio), ==, 0);
5585 }
5586
5587 /*
5588 * Iterate to convergence.
5589 */
5590 do {
5591 int pass = ++spa->spa_sync_pass;
5592
5593 spa_sync_config_object(spa, tx);
5594 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
5595 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
5596 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
5597 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
5598 spa_errlog_sync(spa, txg);
5599 dsl_pool_sync(dp, txg);
5600
5601 if (pass <= SYNC_PASS_DEFERRED_FREE) {
5602 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5603 bplist_iterate(free_bpl, spa_free_sync_cb,
5604 zio, tx);
5605 VERIFY(zio_wait(zio) == 0);
5606 } else {
5607 bplist_iterate(free_bpl, bpobj_enqueue_cb,
5608 defer_bpo, tx);
5609 }
5610
5611 ddt_sync(spa, txg);
5612 dsl_scan_sync(dp, tx);
5613
5614 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
5615 vdev_sync(vd, txg);
5616
5617 if (pass == 1)
5618 spa_sync_upgrades(spa, tx);
5619
5620 } while (dmu_objset_is_dirty(mos, txg));
5621
5622 /*
5623 * Rewrite the vdev configuration (which includes the uberblock)
5624 * to commit the transaction group.
5625 *
5626 * If there are no dirty vdevs, we sync the uberblock to a few
5627 * random top-level vdevs that are known to be visible in the
5628 * config cache (see spa_vdev_add() for a complete description).
5629 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5630 */
5631 for (;;) {
5632 /*
5633 * We hold SCL_STATE to prevent vdev open/close/etc.
5634 * while we're attempting to write the vdev labels.
5635 */
5636 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5637
5638 if (list_is_empty(&spa->spa_config_dirty_list)) {
5639 vdev_t *svd[SPA_DVAS_PER_BP];
5640 int svdcount = 0;
5641 int children = rvd->vdev_children;
5642 int c0 = spa_get_random(children);
5643
5644 for (int c = 0; c < children; c++) {
5645 vd = rvd->vdev_child[(c0 + c) % children];
5646 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
5647 continue;
5648 svd[svdcount++] = vd;
5649 if (svdcount == SPA_DVAS_PER_BP)
5650 break;
5651 }
5652 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
5653 if (error != 0)
5654 error = vdev_config_sync(svd, svdcount, txg,
5655 B_TRUE);
5656 } else {
5657 error = vdev_config_sync(rvd->vdev_child,
5658 rvd->vdev_children, txg, B_FALSE);
5659 if (error != 0)
5660 error = vdev_config_sync(rvd->vdev_child,
5661 rvd->vdev_children, txg, B_TRUE);
5662 }
5663
5664 spa_config_exit(spa, SCL_STATE, FTAG);
5665
5666 if (error == 0)
5667 break;
5668 zio_suspend(spa, NULL);
5669 zio_resume_wait(spa);
5670 }
5671 dmu_tx_commit(tx);
5672
5673 /*
5674 * Clear the dirty config list.
5675 */
5676 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
5677 vdev_config_clean(vd);
5678
5679 /*
5680 * Now that the new config has synced transactionally,
5681 * let it become visible to the config cache.
5682 */
5683 if (spa->spa_config_syncing != NULL) {
5684 spa_config_set(spa, spa->spa_config_syncing);
5685 spa->spa_config_txg = txg;
5686 spa->spa_config_syncing = NULL;
5687 }
5688
5689 spa->spa_ubsync = spa->spa_uberblock;
5690
5691 dsl_pool_sync_done(dp, txg);
5692
5693 /*
5694 * Update usable space statistics.
5695 */
5696 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5697 vdev_sync_done(vd, txg);
5698
5699 spa_update_dspace(spa);
5700
5701 /*
5702 * It had better be the case that we didn't dirty anything
5703 * since vdev_config_sync().
5704 */
5705 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5706 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5707 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
5708
5709 spa->spa_sync_pass = 0;
5710
5711 spa_config_exit(spa, SCL_CONFIG, FTAG);
5712
5713 spa_handle_ignored_writes(spa);
5714
5715 /*
5716 * If any async tasks have been requested, kick them off.
5717 */
5718 spa_async_dispatch(spa);
5719}
5720
5721/*
5722 * Sync all pools. We don't want to hold the namespace lock across these
5723 * operations, so we take a reference on the spa_t and drop the lock during the
5724 * sync.
5725 */
5726void
5727spa_sync_allpools(void)
5728{
5729 spa_t *spa = NULL;
5730 mutex_enter(&spa_namespace_lock);
5731 while ((spa = spa_next(spa)) != NULL) {
5732 if (spa_state(spa) != POOL_STATE_ACTIVE ||
5733 !spa_writeable(spa) || spa_suspended(spa))
5734 continue;
5735 spa_open_ref(spa, FTAG);
5736 mutex_exit(&spa_namespace_lock);
5737 txg_wait_synced(spa_get_dsl(spa), 0);
5738 mutex_enter(&spa_namespace_lock);
5739 spa_close(spa, FTAG);
5740 }
5741 mutex_exit(&spa_namespace_lock);
5742}
5743
5744/*
5745 * ==========================================================================
5746 * Miscellaneous routines
5747 * ==========================================================================
5748 */
5749
5750/*
5751 * Remove all pools in the system.
5752 */
5753void
5754spa_evict_all(void)
5755{
5756 spa_t *spa;
5757
5758 /*
5759 * Remove all cached state. All pools should be closed now,
5760 * so every spa in the AVL tree should be unreferenced.
5761 */
5762 mutex_enter(&spa_namespace_lock);
5763 while ((spa = spa_next(NULL)) != NULL) {
5764 /*
5765 * Stop async tasks. The async thread may need to detach
5766 * a device that's been replaced, which requires grabbing
5767 * spa_namespace_lock, so we must drop it here.
5768 */
5769 spa_open_ref(spa, FTAG);
5770 mutex_exit(&spa_namespace_lock);
5771 spa_async_suspend(spa);
5772 mutex_enter(&spa_namespace_lock);
5773 spa_close(spa, FTAG);
5774
5775 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5776 spa_unload(spa);
5777 spa_deactivate(spa);
5778 }
5779 spa_remove(spa);
5780 }
5781 mutex_exit(&spa_namespace_lock);
5782}
5783
5784vdev_t *
5785spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
5786{
5787 vdev_t *vd;
5788 int i;
5789
5790 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
5791 return (vd);
5792
5793 if (aux) {
5794 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
5795 vd = spa->spa_l2cache.sav_vdevs[i];
5796 if (vd->vdev_guid == guid)
5797 return (vd);
5798 }
5799
5800 for (i = 0; i < spa->spa_spares.sav_count; i++) {
5801 vd = spa->spa_spares.sav_vdevs[i];
5802 if (vd->vdev_guid == guid)
5803 return (vd);
5804 }
5805 }
5806
5807 return (NULL);
5808}
5809
5810void
5811spa_upgrade(spa_t *spa, uint64_t version)
5812{
5813 ASSERT(spa_writeable(spa));
5814
5815 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5816
5817 /*
5818 * This should only be called for a non-faulted pool, and since a
5819 * future version would result in an unopenable pool, this shouldn't be
5820 * possible.
5821 */
5822 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
5823 ASSERT(version >= spa->spa_uberblock.ub_version);
5824
5825 spa->spa_uberblock.ub_version = version;
5826 vdev_config_dirty(spa->spa_root_vdev);
5827
5828 spa_config_exit(spa, SCL_ALL, FTAG);
5829
5830 txg_wait_synced(spa_get_dsl(spa), 0);
5831}
5832
5833boolean_t
5834spa_has_spare(spa_t *spa, uint64_t guid)
5835{
5836 int i;
5837 uint64_t spareguid;
5838 spa_aux_vdev_t *sav = &spa->spa_spares;
5839
5840 for (i = 0; i < sav->sav_count; i++)
5841 if (sav->sav_vdevs[i]->vdev_guid == guid)
5842 return (B_TRUE);
5843
5844 for (i = 0; i < sav->sav_npending; i++) {
5845 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
5846 &spareguid) == 0 && spareguid == guid)
5847 return (B_TRUE);
5848 }
5849
5850 return (B_FALSE);
5851}
5852
5853/*
5854 * Check if a pool has an active shared spare device.
5855 * Note: reference count of an active spare is 2, as a spare and as a replace
5856 */
5857static boolean_t
5858spa_has_active_shared_spare(spa_t *spa)
5859{
5860 int i, refcnt;
5861 uint64_t pool;
5862 spa_aux_vdev_t *sav = &spa->spa_spares;
5863
5864 for (i = 0; i < sav->sav_count; i++) {
5865 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
5866 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
5867 refcnt > 2)
5868 return (B_TRUE);
5869 }
5870
5871 return (B_FALSE);
5872}
5873
5874/*
5875 * Post a sysevent corresponding to the given event. The 'name' must be one of
5876 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
5877 * filled in from the spa and (optionally) the vdev. This doesn't do anything
5878 * in the userland libzpool, as we don't want consumers to misinterpret ztest
5879 * or zdb as real changes.
5880 */
5881void
5882spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
5883{
5884#ifdef _KERNEL
5885 sysevent_t *ev;
5886 sysevent_attr_list_t *attr = NULL;
5887 sysevent_value_t value;
5888 sysevent_id_t eid;
5889
5890 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
5891 SE_SLEEP);
5892
5893 value.value_type = SE_DATA_TYPE_STRING;
5894 value.value.sv_string = spa_name(spa);
5895 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
5896 goto done;
5897
5898 value.value_type = SE_DATA_TYPE_UINT64;
5899 value.value.sv_uint64 = spa_guid(spa);
5900 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
5901 goto done;
5902
5903 if (vd) {
5904 value.value_type = SE_DATA_TYPE_UINT64;
5905 value.value.sv_uint64 = vd->vdev_guid;
5906 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
5907 SE_SLEEP) != 0)
5908 goto done;
5909
5910 if (vd->vdev_path) {
5911 value.value_type = SE_DATA_TYPE_STRING;
5912 value.value.sv_string = vd->vdev_path;
5913 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
5914 &value, SE_SLEEP) != 0)
5915 goto done;
5916 }
5917 }
5918
5919 if (sysevent_attach_attributes(ev, attr) != 0)
5920 goto done;
5921 attr = NULL;
5922
5923 (void) log_sysevent(ev, SE_SLEEP, &eid);
5924
5925done:
5926 if (attr)
5927 sysevent_free_attr(attr);
5928 sysevent_free(ev);
5929#endif
5930}
5470 default:
5471 /*
5472 * Set pool property values in the poolprops mos object.
5473 */
5474 if (spa->spa_pool_props_object == 0) {
5475 VERIFY((spa->spa_pool_props_object =
5476 zap_create(mos, DMU_OT_POOL_PROPS,
5477 DMU_OT_NONE, 0, tx)) > 0);
5478
5479 VERIFY(zap_update(mos,
5480 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5481 8, 1, &spa->spa_pool_props_object, tx)
5482 == 0);
5483 }
5484
5485 /* normalize the property name */
5486 propname = zpool_prop_to_name(prop);
5487 proptype = zpool_prop_get_type(prop);
5488
5489 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5490 ASSERT(proptype == PROP_TYPE_STRING);
5491 VERIFY(nvpair_value_string(elem, &strval) == 0);
5492 VERIFY(zap_update(mos,
5493 spa->spa_pool_props_object, propname,
5494 1, strlen(strval) + 1, strval, tx) == 0);
5495
5496 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5497 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5498
5499 if (proptype == PROP_TYPE_INDEX) {
5500 const char *unused;
5501 VERIFY(zpool_prop_index_to_string(
5502 prop, intval, &unused) == 0);
5503 }
5504 VERIFY(zap_update(mos,
5505 spa->spa_pool_props_object, propname,
5506 8, 1, &intval, tx) == 0);
5507 } else {
5508 ASSERT(0); /* not allowed */
5509 }
5510
5511 switch (prop) {
5512 case ZPOOL_PROP_DELEGATION:
5513 spa->spa_delegation = intval;
5514 break;
5515 case ZPOOL_PROP_BOOTFS:
5516 spa->spa_bootfs = intval;
5517 break;
5518 case ZPOOL_PROP_FAILUREMODE:
5519 spa->spa_failmode = intval;
5520 break;
5521 case ZPOOL_PROP_AUTOEXPAND:
5522 spa->spa_autoexpand = intval;
5523 if (tx->tx_txg != TXG_INITIAL)
5524 spa_async_request(spa,
5525 SPA_ASYNC_AUTOEXPAND);
5526 break;
5527 case ZPOOL_PROP_DEDUPDITTO:
5528 spa->spa_dedup_ditto = intval;
5529 break;
5530 default:
5531 break;
5532 }
5533 }
5534
5535 /* log internal history if this is not a zpool create */
5536 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5537 tx->tx_txg != TXG_INITIAL) {
5538 spa_history_log_internal(LOG_POOL_PROPSET,
5539 spa, tx, "%s %lld %s",
5540 nvpair_name(elem), intval, spa_name(spa));
5541 }
5542 }
5543
5544 mutex_exit(&spa->spa_props_lock);
5545}
5546
5547/*
5548 * Perform one-time upgrade on-disk changes. spa_version() does not
5549 * reflect the new version this txg, so there must be no changes this
5550 * txg to anything that the upgrade code depends on after it executes.
5551 * Therefore this must be called after dsl_pool_sync() does the sync
5552 * tasks.
5553 */
5554static void
5555spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5556{
5557 dsl_pool_t *dp = spa->spa_dsl_pool;
5558
5559 ASSERT(spa->spa_sync_pass == 1);
5560
5561 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5562 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5563 dsl_pool_create_origin(dp, tx);
5564
5565 /* Keeping the origin open increases spa_minref */
5566 spa->spa_minref += 3;
5567 }
5568
5569 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5570 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5571 dsl_pool_upgrade_clones(dp, tx);
5572 }
5573
5574 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5575 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5576 dsl_pool_upgrade_dir_clones(dp, tx);
5577
5578 /* Keeping the freedir open increases spa_minref */
5579 spa->spa_minref += 3;
5580 }
5581}
5582
5583/*
5584 * Sync the specified transaction group. New blocks may be dirtied as
5585 * part of the process, so we iterate until it converges.
5586 */
5587void
5588spa_sync(spa_t *spa, uint64_t txg)
5589{
5590 dsl_pool_t *dp = spa->spa_dsl_pool;
5591 objset_t *mos = spa->spa_meta_objset;
5592 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5593 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5594 vdev_t *rvd = spa->spa_root_vdev;
5595 vdev_t *vd;
5596 dmu_tx_t *tx;
5597 int error;
5598
5599 VERIFY(spa_writeable(spa));
5600
5601 /*
5602 * Lock out configuration changes.
5603 */
5604 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5605
5606 spa->spa_syncing_txg = txg;
5607 spa->spa_sync_pass = 0;
5608
5609 /*
5610 * If there are any pending vdev state changes, convert them
5611 * into config changes that go out with this transaction group.
5612 */
5613 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5614 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5615 /*
5616 * We need the write lock here because, for aux vdevs,
5617 * calling vdev_config_dirty() modifies sav_config.
5618 * This is ugly and will become unnecessary when we
5619 * eliminate the aux vdev wart by integrating all vdevs
5620 * into the root vdev tree.
5621 */
5622 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5623 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5624 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5625 vdev_state_clean(vd);
5626 vdev_config_dirty(vd);
5627 }
5628 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5629 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
5630 }
5631 spa_config_exit(spa, SCL_STATE, FTAG);
5632
5633 tx = dmu_tx_create_assigned(dp, txg);
5634
5635 /*
5636 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5637 * set spa_deflate if we have no raid-z vdevs.
5638 */
5639 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
5640 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
5641 int i;
5642
5643 for (i = 0; i < rvd->vdev_children; i++) {
5644 vd = rvd->vdev_child[i];
5645 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
5646 break;
5647 }
5648 if (i == rvd->vdev_children) {
5649 spa->spa_deflate = TRUE;
5650 VERIFY(0 == zap_add(spa->spa_meta_objset,
5651 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5652 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
5653 }
5654 }
5655
5656 /*
5657 * If anything has changed in this txg, or if someone is waiting
5658 * for this txg to sync (eg, spa_vdev_remove()), push the
5659 * deferred frees from the previous txg. If not, leave them
5660 * alone so that we don't generate work on an otherwise idle
5661 * system.
5662 */
5663 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
5664 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
5665 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5666 ((dsl_scan_active(dp->dp_scan) ||
5667 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
5668 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5669 VERIFY3U(bpobj_iterate(defer_bpo,
5670 spa_free_sync_cb, zio, tx), ==, 0);
5671 VERIFY3U(zio_wait(zio), ==, 0);
5672 }
5673
5674 /*
5675 * Iterate to convergence.
5676 */
5677 do {
5678 int pass = ++spa->spa_sync_pass;
5679
5680 spa_sync_config_object(spa, tx);
5681 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
5682 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
5683 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
5684 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
5685 spa_errlog_sync(spa, txg);
5686 dsl_pool_sync(dp, txg);
5687
5688 if (pass <= SYNC_PASS_DEFERRED_FREE) {
5689 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5690 bplist_iterate(free_bpl, spa_free_sync_cb,
5691 zio, tx);
5692 VERIFY(zio_wait(zio) == 0);
5693 } else {
5694 bplist_iterate(free_bpl, bpobj_enqueue_cb,
5695 defer_bpo, tx);
5696 }
5697
5698 ddt_sync(spa, txg);
5699 dsl_scan_sync(dp, tx);
5700
5701 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
5702 vdev_sync(vd, txg);
5703
5704 if (pass == 1)
5705 spa_sync_upgrades(spa, tx);
5706
5707 } while (dmu_objset_is_dirty(mos, txg));
5708
5709 /*
5710 * Rewrite the vdev configuration (which includes the uberblock)
5711 * to commit the transaction group.
5712 *
5713 * If there are no dirty vdevs, we sync the uberblock to a few
5714 * random top-level vdevs that are known to be visible in the
5715 * config cache (see spa_vdev_add() for a complete description).
5716 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5717 */
5718 for (;;) {
5719 /*
5720 * We hold SCL_STATE to prevent vdev open/close/etc.
5721 * while we're attempting to write the vdev labels.
5722 */
5723 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5724
5725 if (list_is_empty(&spa->spa_config_dirty_list)) {
5726 vdev_t *svd[SPA_DVAS_PER_BP];
5727 int svdcount = 0;
5728 int children = rvd->vdev_children;
5729 int c0 = spa_get_random(children);
5730
5731 for (int c = 0; c < children; c++) {
5732 vd = rvd->vdev_child[(c0 + c) % children];
5733 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
5734 continue;
5735 svd[svdcount++] = vd;
5736 if (svdcount == SPA_DVAS_PER_BP)
5737 break;
5738 }
5739 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
5740 if (error != 0)
5741 error = vdev_config_sync(svd, svdcount, txg,
5742 B_TRUE);
5743 } else {
5744 error = vdev_config_sync(rvd->vdev_child,
5745 rvd->vdev_children, txg, B_FALSE);
5746 if (error != 0)
5747 error = vdev_config_sync(rvd->vdev_child,
5748 rvd->vdev_children, txg, B_TRUE);
5749 }
5750
5751 spa_config_exit(spa, SCL_STATE, FTAG);
5752
5753 if (error == 0)
5754 break;
5755 zio_suspend(spa, NULL);
5756 zio_resume_wait(spa);
5757 }
5758 dmu_tx_commit(tx);
5759
5760 /*
5761 * Clear the dirty config list.
5762 */
5763 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
5764 vdev_config_clean(vd);
5765
5766 /*
5767 * Now that the new config has synced transactionally,
5768 * let it become visible to the config cache.
5769 */
5770 if (spa->spa_config_syncing != NULL) {
5771 spa_config_set(spa, spa->spa_config_syncing);
5772 spa->spa_config_txg = txg;
5773 spa->spa_config_syncing = NULL;
5774 }
5775
5776 spa->spa_ubsync = spa->spa_uberblock;
5777
5778 dsl_pool_sync_done(dp, txg);
5779
5780 /*
5781 * Update usable space statistics.
5782 */
5783 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5784 vdev_sync_done(vd, txg);
5785
5786 spa_update_dspace(spa);
5787
5788 /*
5789 * It had better be the case that we didn't dirty anything
5790 * since vdev_config_sync().
5791 */
5792 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5793 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5794 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
5795
5796 spa->spa_sync_pass = 0;
5797
5798 spa_config_exit(spa, SCL_CONFIG, FTAG);
5799
5800 spa_handle_ignored_writes(spa);
5801
5802 /*
5803 * If any async tasks have been requested, kick them off.
5804 */
5805 spa_async_dispatch(spa);
5806}
5807
5808/*
5809 * Sync all pools. We don't want to hold the namespace lock across these
5810 * operations, so we take a reference on the spa_t and drop the lock during the
5811 * sync.
5812 */
5813void
5814spa_sync_allpools(void)
5815{
5816 spa_t *spa = NULL;
5817 mutex_enter(&spa_namespace_lock);
5818 while ((spa = spa_next(spa)) != NULL) {
5819 if (spa_state(spa) != POOL_STATE_ACTIVE ||
5820 !spa_writeable(spa) || spa_suspended(spa))
5821 continue;
5822 spa_open_ref(spa, FTAG);
5823 mutex_exit(&spa_namespace_lock);
5824 txg_wait_synced(spa_get_dsl(spa), 0);
5825 mutex_enter(&spa_namespace_lock);
5826 spa_close(spa, FTAG);
5827 }
5828 mutex_exit(&spa_namespace_lock);
5829}
5830
5831/*
5832 * ==========================================================================
5833 * Miscellaneous routines
5834 * ==========================================================================
5835 */
5836
5837/*
5838 * Remove all pools in the system.
5839 */
5840void
5841spa_evict_all(void)
5842{
5843 spa_t *spa;
5844
5845 /*
5846 * Remove all cached state. All pools should be closed now,
5847 * so every spa in the AVL tree should be unreferenced.
5848 */
5849 mutex_enter(&spa_namespace_lock);
5850 while ((spa = spa_next(NULL)) != NULL) {
5851 /*
5852 * Stop async tasks. The async thread may need to detach
5853 * a device that's been replaced, which requires grabbing
5854 * spa_namespace_lock, so we must drop it here.
5855 */
5856 spa_open_ref(spa, FTAG);
5857 mutex_exit(&spa_namespace_lock);
5858 spa_async_suspend(spa);
5859 mutex_enter(&spa_namespace_lock);
5860 spa_close(spa, FTAG);
5861
5862 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5863 spa_unload(spa);
5864 spa_deactivate(spa);
5865 }
5866 spa_remove(spa);
5867 }
5868 mutex_exit(&spa_namespace_lock);
5869}
5870
5871vdev_t *
5872spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
5873{
5874 vdev_t *vd;
5875 int i;
5876
5877 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
5878 return (vd);
5879
5880 if (aux) {
5881 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
5882 vd = spa->spa_l2cache.sav_vdevs[i];
5883 if (vd->vdev_guid == guid)
5884 return (vd);
5885 }
5886
5887 for (i = 0; i < spa->spa_spares.sav_count; i++) {
5888 vd = spa->spa_spares.sav_vdevs[i];
5889 if (vd->vdev_guid == guid)
5890 return (vd);
5891 }
5892 }
5893
5894 return (NULL);
5895}
5896
5897void
5898spa_upgrade(spa_t *spa, uint64_t version)
5899{
5900 ASSERT(spa_writeable(spa));
5901
5902 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5903
5904 /*
5905 * This should only be called for a non-faulted pool, and since a
5906 * future version would result in an unopenable pool, this shouldn't be
5907 * possible.
5908 */
5909 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
5910 ASSERT(version >= spa->spa_uberblock.ub_version);
5911
5912 spa->spa_uberblock.ub_version = version;
5913 vdev_config_dirty(spa->spa_root_vdev);
5914
5915 spa_config_exit(spa, SCL_ALL, FTAG);
5916
5917 txg_wait_synced(spa_get_dsl(spa), 0);
5918}
5919
5920boolean_t
5921spa_has_spare(spa_t *spa, uint64_t guid)
5922{
5923 int i;
5924 uint64_t spareguid;
5925 spa_aux_vdev_t *sav = &spa->spa_spares;
5926
5927 for (i = 0; i < sav->sav_count; i++)
5928 if (sav->sav_vdevs[i]->vdev_guid == guid)
5929 return (B_TRUE);
5930
5931 for (i = 0; i < sav->sav_npending; i++) {
5932 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
5933 &spareguid) == 0 && spareguid == guid)
5934 return (B_TRUE);
5935 }
5936
5937 return (B_FALSE);
5938}
5939
5940/*
5941 * Check if a pool has an active shared spare device.
5942 * Note: reference count of an active spare is 2, as a spare and as a replace
5943 */
5944static boolean_t
5945spa_has_active_shared_spare(spa_t *spa)
5946{
5947 int i, refcnt;
5948 uint64_t pool;
5949 spa_aux_vdev_t *sav = &spa->spa_spares;
5950
5951 for (i = 0; i < sav->sav_count; i++) {
5952 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
5953 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
5954 refcnt > 2)
5955 return (B_TRUE);
5956 }
5957
5958 return (B_FALSE);
5959}
5960
5961/*
5962 * Post a sysevent corresponding to the given event. The 'name' must be one of
5963 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
5964 * filled in from the spa and (optionally) the vdev. This doesn't do anything
5965 * in the userland libzpool, as we don't want consumers to misinterpret ztest
5966 * or zdb as real changes.
5967 */
5968void
5969spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
5970{
5971#ifdef _KERNEL
5972 sysevent_t *ev;
5973 sysevent_attr_list_t *attr = NULL;
5974 sysevent_value_t value;
5975 sysevent_id_t eid;
5976
5977 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
5978 SE_SLEEP);
5979
5980 value.value_type = SE_DATA_TYPE_STRING;
5981 value.value.sv_string = spa_name(spa);
5982 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
5983 goto done;
5984
5985 value.value_type = SE_DATA_TYPE_UINT64;
5986 value.value.sv_uint64 = spa_guid(spa);
5987 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
5988 goto done;
5989
5990 if (vd) {
5991 value.value_type = SE_DATA_TYPE_UINT64;
5992 value.value.sv_uint64 = vd->vdev_guid;
5993 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
5994 SE_SLEEP) != 0)
5995 goto done;
5996
5997 if (vd->vdev_path) {
5998 value.value_type = SE_DATA_TYPE_STRING;
5999 value.value.sv_string = vd->vdev_path;
6000 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6001 &value, SE_SLEEP) != 0)
6002 goto done;
6003 }
6004 }
6005
6006 if (sysevent_attach_attributes(ev, attr) != 0)
6007 goto done;
6008 attr = NULL;
6009
6010 (void) log_sysevent(ev, SE_SLEEP, &eid);
6011
6012done:
6013 if (attr)
6014 sysevent_free_attr(attr);
6015 sysevent_free(ev);
6016#endif
6017}