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