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