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