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