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