]>
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. |
944a3724 | 24 | * Copyright (c) 2011, 2019 by Delphix. All rights reserved. |
733b5722 | 25 | * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved. |
0c66c32d | 26 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. |
3c67d83a | 27 | * Copyright 2013 Saso Kiselkov. All rights reserved. |
e550644f BB |
28 | * Copyright (c) 2014 Integros [integros.com] |
29 | * Copyright 2016 Toomas Soome <tsoome@me.com> | |
a0bd735a | 30 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
f65fbee1 | 31 | * Copyright 2018 Joyent, Inc. |
0ea05c64 | 32 | * Copyright (c) 2017 Datto Inc. |
12fa0466 | 33 | * Copyright 2017 Joyent, Inc. |
cc99f275 | 34 | * Copyright (c) 2017, Intel Corporation. |
a38718a6 | 35 | */ |
34dc7c2f | 36 | |
34dc7c2f | 37 | /* |
e49f1e20 WA |
38 | * SPA: Storage Pool Allocator |
39 | * | |
34dc7c2f BB |
40 | * This file contains all the routines used when modifying on-disk SPA state. |
41 | * This includes opening, importing, destroying, exporting a pool, and syncing a | |
42 | * pool. | |
43 | */ | |
44 | ||
45 | #include <sys/zfs_context.h> | |
46 | #include <sys/fm/fs/zfs.h> | |
47 | #include <sys/spa_impl.h> | |
48 | #include <sys/zio.h> | |
49 | #include <sys/zio_checksum.h> | |
34dc7c2f BB |
50 | #include <sys/dmu.h> |
51 | #include <sys/dmu_tx.h> | |
52 | #include <sys/zap.h> | |
53 | #include <sys/zil.h> | |
428870ff | 54 | #include <sys/ddt.h> |
34dc7c2f | 55 | #include <sys/vdev_impl.h> |
a1d477c2 MA |
56 | #include <sys/vdev_removal.h> |
57 | #include <sys/vdev_indirect_mapping.h> | |
58 | #include <sys/vdev_indirect_births.h> | |
619f0976 | 59 | #include <sys/vdev_initialize.h> |
1b939560 | 60 | #include <sys/vdev_trim.h> |
c28b2279 | 61 | #include <sys/vdev_disk.h> |
34dc7c2f | 62 | #include <sys/metaslab.h> |
428870ff | 63 | #include <sys/metaslab_impl.h> |
379ca9cf | 64 | #include <sys/mmp.h> |
34dc7c2f BB |
65 | #include <sys/uberblock_impl.h> |
66 | #include <sys/txg.h> | |
67 | #include <sys/avl.h> | |
a1d477c2 | 68 | #include <sys/bpobj.h> |
34dc7c2f BB |
69 | #include <sys/dmu_traverse.h> |
70 | #include <sys/dmu_objset.h> | |
71 | #include <sys/unique.h> | |
72 | #include <sys/dsl_pool.h> | |
73 | #include <sys/dsl_dataset.h> | |
74 | #include <sys/dsl_dir.h> | |
75 | #include <sys/dsl_prop.h> | |
76 | #include <sys/dsl_synctask.h> | |
77 | #include <sys/fs/zfs.h> | |
78 | #include <sys/arc.h> | |
79 | #include <sys/callb.h> | |
80 | #include <sys/systeminfo.h> | |
34dc7c2f | 81 | #include <sys/spa_boot.h> |
9babb374 | 82 | #include <sys/zfs_ioctl.h> |
428870ff | 83 | #include <sys/dsl_scan.h> |
9ae529ec | 84 | #include <sys/zfeature.h> |
13fe0198 | 85 | #include <sys/dsl_destroy.h> |
526af785 | 86 | #include <sys/zvol.h> |
34dc7c2f | 87 | |
d164b209 | 88 | #ifdef _KERNEL |
12fa0466 DE |
89 | #include <sys/fm/protocol.h> |
90 | #include <sys/fm/util.h> | |
428870ff | 91 | #include <sys/callb.h> |
d164b209 BB |
92 | #include <sys/zone.h> |
93 | #endif /* _KERNEL */ | |
94 | ||
34dc7c2f BB |
95 | #include "zfs_prop.h" |
96 | #include "zfs_comutil.h" | |
97 | ||
e6cfd633 WA |
98 | /* |
99 | * The interval, in seconds, at which failed configuration cache file writes | |
100 | * should be retried. | |
101 | */ | |
a1d477c2 | 102 | int zfs_ccw_retry_interval = 300; |
e6cfd633 | 103 | |
428870ff | 104 | typedef enum zti_modes { |
7ef5e54e | 105 | ZTI_MODE_FIXED, /* value is # of threads (min 1) */ |
7ef5e54e AL |
106 | ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ |
107 | ZTI_MODE_NULL, /* don't create a taskq */ | |
108 | ZTI_NMODES | |
428870ff | 109 | } zti_modes_t; |
34dc7c2f | 110 | |
7ef5e54e AL |
111 | #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } |
112 | #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } | |
113 | #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } | |
114 | #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } | |
9babb374 | 115 | |
7ef5e54e AL |
116 | #define ZTI_N(n) ZTI_P(n, 1) |
117 | #define ZTI_ONE ZTI_N(1) | |
9babb374 BB |
118 | |
119 | typedef struct zio_taskq_info { | |
7ef5e54e | 120 | zti_modes_t zti_mode; |
428870ff | 121 | uint_t zti_value; |
7ef5e54e | 122 | uint_t zti_count; |
9babb374 BB |
123 | } zio_taskq_info_t; |
124 | ||
125 | static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { | |
451041db | 126 | "iss", "iss_h", "int", "int_h" |
9babb374 BB |
127 | }; |
128 | ||
428870ff | 129 | /* |
7ef5e54e AL |
130 | * This table defines the taskq settings for each ZFS I/O type. When |
131 | * initializing a pool, we use this table to create an appropriately sized | |
132 | * taskq. Some operations are low volume and therefore have a small, static | |
133 | * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE | |
134 | * macros. Other operations process a large amount of data; the ZTI_BATCH | |
135 | * macro causes us to create a taskq oriented for throughput. Some operations | |
1b939560 | 136 | * are so high frequency and short-lived that the taskq itself can become a |
7ef5e54e AL |
137 | * point of lock contention. The ZTI_P(#, #) macro indicates that we need an |
138 | * additional degree of parallelism specified by the number of threads per- | |
139 | * taskq and the number of taskqs; when dispatching an event in this case, the | |
140 | * particular taskq is chosen at random. | |
141 | * | |
142 | * The different taskq priorities are to handle the different contexts (issue | |
143 | * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that | |
144 | * need to be handled with minimum delay. | |
428870ff BB |
145 | */ |
146 | const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { | |
147 | /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ | |
7ef5e54e | 148 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ |
aa9af22c BB |
149 | { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ |
150 | { ZTI_BATCH, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */ | |
151 | { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ | |
7ef5e54e AL |
152 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ |
153 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ | |
1b939560 | 154 | { ZTI_N(4), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* TRIM */ |
9babb374 BB |
155 | }; |
156 | ||
13fe0198 MA |
157 | static void spa_sync_version(void *arg, dmu_tx_t *tx); |
158 | static void spa_sync_props(void *arg, dmu_tx_t *tx); | |
b128c09f | 159 | static boolean_t spa_has_active_shared_spare(spa_t *spa); |
d2734cce | 160 | static int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport); |
572e2857 | 161 | static void spa_vdev_resilver_done(spa_t *spa); |
428870ff | 162 | |
e8b96c60 | 163 | uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ |
428870ff BB |
164 | boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ |
165 | uint_t zio_taskq_basedc = 80; /* base duty cycle */ | |
166 | ||
167 | boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ | |
168 | ||
afd2f7b7 PZ |
169 | /* |
170 | * Report any spa_load_verify errors found, but do not fail spa_load. | |
171 | * This is used by zdb to analyze non-idle pools. | |
172 | */ | |
173 | boolean_t spa_load_verify_dryrun = B_FALSE; | |
174 | ||
428870ff BB |
175 | /* |
176 | * This (illegal) pool name is used when temporarily importing a spa_t in order | |
177 | * to get the vdev stats associated with the imported devices. | |
178 | */ | |
179 | #define TRYIMPORT_NAME "$import" | |
34dc7c2f | 180 | |
6cb8e530 PZ |
181 | /* |
182 | * For debugging purposes: print out vdev tree during pool import. | |
183 | */ | |
184 | int spa_load_print_vdev_tree = B_FALSE; | |
185 | ||
186 | /* | |
187 | * A non-zero value for zfs_max_missing_tvds means that we allow importing | |
188 | * pools with missing top-level vdevs. This is strictly intended for advanced | |
189 | * pool recovery cases since missing data is almost inevitable. Pools with | |
190 | * missing devices can only be imported read-only for safety reasons, and their | |
191 | * fail-mode will be automatically set to "continue". | |
192 | * | |
193 | * With 1 missing vdev we should be able to import the pool and mount all | |
194 | * datasets. User data that was not modified after the missing device has been | |
195 | * added should be recoverable. This means that snapshots created prior to the | |
196 | * addition of that device should be completely intact. | |
197 | * | |
198 | * With 2 missing vdevs, some datasets may fail to mount since there are | |
199 | * dataset statistics that are stored as regular metadata. Some data might be | |
200 | * recoverable if those vdevs were added recently. | |
201 | * | |
202 | * With 3 or more missing vdevs, the pool is severely damaged and MOS entries | |
203 | * may be missing entirely. Chances of data recovery are very low. Note that | |
204 | * there are also risks of performing an inadvertent rewind as we might be | |
205 | * missing all the vdevs with the latest uberblocks. | |
206 | */ | |
207 | unsigned long zfs_max_missing_tvds = 0; | |
208 | ||
209 | /* | |
210 | * The parameters below are similar to zfs_max_missing_tvds but are only | |
211 | * intended for a preliminary open of the pool with an untrusted config which | |
212 | * might be incomplete or out-dated. | |
213 | * | |
214 | * We are more tolerant for pools opened from a cachefile since we could have | |
215 | * an out-dated cachefile where a device removal was not registered. | |
216 | * We could have set the limit arbitrarily high but in the case where devices | |
217 | * are really missing we would want to return the proper error codes; we chose | |
218 | * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available | |
219 | * and we get a chance to retrieve the trusted config. | |
220 | */ | |
221 | uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1; | |
d2734cce | 222 | |
6cb8e530 PZ |
223 | /* |
224 | * In the case where config was assembled by scanning device paths (/dev/dsks | |
225 | * by default) we are less tolerant since all the existing devices should have | |
226 | * been detected and we want spa_load to return the right error codes. | |
227 | */ | |
228 | uint64_t zfs_max_missing_tvds_scan = 0; | |
229 | ||
d2734cce SD |
230 | /* |
231 | * Debugging aid that pauses spa_sync() towards the end. | |
232 | */ | |
233 | boolean_t zfs_pause_spa_sync = B_FALSE; | |
234 | ||
37f03da8 SH |
235 | /* |
236 | * Variables to indicate the livelist condense zthr func should wait at certain | |
237 | * points for the livelist to be removed - used to test condense/destroy races | |
238 | */ | |
239 | int zfs_livelist_condense_zthr_pause = 0; | |
240 | int zfs_livelist_condense_sync_pause = 0; | |
241 | ||
242 | /* | |
243 | * Variables to track whether or not condense cancellation has been | |
244 | * triggered in testing. | |
245 | */ | |
246 | int zfs_livelist_condense_sync_cancel = 0; | |
247 | int zfs_livelist_condense_zthr_cancel = 0; | |
248 | ||
249 | /* | |
250 | * Variable to track whether or not extra ALLOC blkptrs were added to a | |
251 | * livelist entry while it was being condensed (caused by the way we track | |
252 | * remapped blkptrs in dbuf_remap_impl) | |
253 | */ | |
254 | int zfs_livelist_condense_new_alloc = 0; | |
255 | ||
34dc7c2f BB |
256 | /* |
257 | * ========================================================================== | |
258 | * SPA properties routines | |
259 | * ========================================================================== | |
260 | */ | |
261 | ||
262 | /* | |
263 | * Add a (source=src, propname=propval) list to an nvlist. | |
264 | */ | |
265 | static void | |
266 | spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, | |
267 | uint64_t intval, zprop_source_t src) | |
268 | { | |
269 | const char *propname = zpool_prop_to_name(prop); | |
270 | nvlist_t *propval; | |
271 | ||
79c76d5b | 272 | VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
273 | VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); |
274 | ||
275 | if (strval != NULL) | |
276 | VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); | |
277 | else | |
278 | VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); | |
279 | ||
280 | VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); | |
281 | nvlist_free(propval); | |
282 | } | |
283 | ||
284 | /* | |
285 | * Get property values from the spa configuration. | |
286 | */ | |
287 | static void | |
288 | spa_prop_get_config(spa_t *spa, nvlist_t **nvp) | |
289 | { | |
1bd201e7 | 290 | vdev_t *rvd = spa->spa_root_vdev; |
9ae529ec | 291 | dsl_pool_t *pool = spa->spa_dsl_pool; |
f3a7f661 | 292 | uint64_t size, alloc, cap, version; |
82ab6848 | 293 | const zprop_source_t src = ZPROP_SRC_NONE; |
b128c09f | 294 | spa_config_dirent_t *dp; |
f3a7f661 | 295 | metaslab_class_t *mc = spa_normal_class(spa); |
b128c09f BB |
296 | |
297 | ASSERT(MUTEX_HELD(&spa->spa_props_lock)); | |
34dc7c2f | 298 | |
1bd201e7 | 299 | if (rvd != NULL) { |
cc99f275 DB |
300 | alloc = metaslab_class_get_alloc(mc); |
301 | alloc += metaslab_class_get_alloc(spa_special_class(spa)); | |
302 | alloc += metaslab_class_get_alloc(spa_dedup_class(spa)); | |
303 | ||
304 | size = metaslab_class_get_space(mc); | |
305 | size += metaslab_class_get_space(spa_special_class(spa)); | |
306 | size += metaslab_class_get_space(spa_dedup_class(spa)); | |
307 | ||
d164b209 BB |
308 | spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); |
309 | spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); | |
428870ff BB |
310 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); |
311 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, | |
312 | size - alloc, src); | |
d2734cce SD |
313 | spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL, |
314 | spa->spa_checkpoint_info.sci_dspace, src); | |
1bd201e7 | 315 | |
f3a7f661 GW |
316 | spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, |
317 | metaslab_class_fragmentation(mc), src); | |
318 | spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, | |
319 | metaslab_class_expandable_space(mc), src); | |
572e2857 BB |
320 | spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, |
321 | (spa_mode(spa) == FREAD), src); | |
d164b209 | 322 | |
428870ff | 323 | cap = (size == 0) ? 0 : (alloc * 100 / size); |
d164b209 BB |
324 | spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); |
325 | ||
428870ff BB |
326 | spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, |
327 | ddt_get_pool_dedup_ratio(spa), src); | |
328 | ||
d164b209 | 329 | spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, |
1bd201e7 | 330 | rvd->vdev_state, src); |
d164b209 BB |
331 | |
332 | version = spa_version(spa); | |
82ab6848 HM |
333 | if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) { |
334 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
335 | version, ZPROP_SRC_DEFAULT); | |
336 | } else { | |
337 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
338 | version, ZPROP_SRC_LOCAL); | |
339 | } | |
a448a255 SD |
340 | spa_prop_add_list(*nvp, ZPOOL_PROP_LOAD_GUID, |
341 | NULL, spa_load_guid(spa), src); | |
d164b209 | 342 | } |
34dc7c2f | 343 | |
9ae529ec | 344 | if (pool != NULL) { |
9ae529ec CS |
345 | /* |
346 | * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, | |
347 | * when opening pools before this version freedir will be NULL. | |
348 | */ | |
fbeddd60 | 349 | if (pool->dp_free_dir != NULL) { |
9ae529ec | 350 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, |
d683ddbb JG |
351 | dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, |
352 | src); | |
9ae529ec CS |
353 | } else { |
354 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, | |
355 | NULL, 0, src); | |
356 | } | |
fbeddd60 MA |
357 | |
358 | if (pool->dp_leak_dir != NULL) { | |
359 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, | |
d683ddbb JG |
360 | dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, |
361 | src); | |
fbeddd60 MA |
362 | } else { |
363 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, | |
364 | NULL, 0, src); | |
365 | } | |
9ae529ec CS |
366 | } |
367 | ||
34dc7c2f | 368 | spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); |
34dc7c2f | 369 | |
d96eb2b1 DM |
370 | if (spa->spa_comment != NULL) { |
371 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, | |
372 | 0, ZPROP_SRC_LOCAL); | |
373 | } | |
374 | ||
34dc7c2f BB |
375 | if (spa->spa_root != NULL) |
376 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, | |
377 | 0, ZPROP_SRC_LOCAL); | |
378 | ||
f1512ee6 MA |
379 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { |
380 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
381 | MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); | |
382 | } else { | |
383 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
384 | SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); | |
385 | } | |
386 | ||
50c957f7 NB |
387 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) { |
388 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
389 | DNODE_MAX_SIZE, ZPROP_SRC_NONE); | |
390 | } else { | |
391 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
392 | DNODE_MIN_SIZE, ZPROP_SRC_NONE); | |
393 | } | |
394 | ||
b128c09f BB |
395 | if ((dp = list_head(&spa->spa_config_list)) != NULL) { |
396 | if (dp->scd_path == NULL) { | |
34dc7c2f | 397 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f BB |
398 | "none", 0, ZPROP_SRC_LOCAL); |
399 | } else if (strcmp(dp->scd_path, spa_config_path) != 0) { | |
34dc7c2f | 400 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f | 401 | dp->scd_path, 0, ZPROP_SRC_LOCAL); |
34dc7c2f BB |
402 | } |
403 | } | |
404 | } | |
405 | ||
406 | /* | |
407 | * Get zpool property values. | |
408 | */ | |
409 | int | |
410 | spa_prop_get(spa_t *spa, nvlist_t **nvp) | |
411 | { | |
428870ff | 412 | objset_t *mos = spa->spa_meta_objset; |
34dc7c2f BB |
413 | zap_cursor_t zc; |
414 | zap_attribute_t za; | |
34dc7c2f BB |
415 | int err; |
416 | ||
79c76d5b | 417 | err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP); |
c28b2279 | 418 | if (err) |
d1d7e268 | 419 | return (err); |
34dc7c2f | 420 | |
b128c09f BB |
421 | mutex_enter(&spa->spa_props_lock); |
422 | ||
34dc7c2f BB |
423 | /* |
424 | * Get properties from the spa config. | |
425 | */ | |
426 | spa_prop_get_config(spa, nvp); | |
427 | ||
34dc7c2f | 428 | /* If no pool property object, no more prop to get. */ |
428870ff | 429 | if (mos == NULL || spa->spa_pool_props_object == 0) { |
34dc7c2f | 430 | mutex_exit(&spa->spa_props_lock); |
c28b2279 | 431 | goto out; |
34dc7c2f BB |
432 | } |
433 | ||
434 | /* | |
435 | * Get properties from the MOS pool property object. | |
436 | */ | |
437 | for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); | |
438 | (err = zap_cursor_retrieve(&zc, &za)) == 0; | |
439 | zap_cursor_advance(&zc)) { | |
440 | uint64_t intval = 0; | |
441 | char *strval = NULL; | |
442 | zprop_source_t src = ZPROP_SRC_DEFAULT; | |
443 | zpool_prop_t prop; | |
444 | ||
31864e3d | 445 | if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL) |
34dc7c2f BB |
446 | continue; |
447 | ||
448 | switch (za.za_integer_length) { | |
449 | case 8: | |
450 | /* integer property */ | |
451 | if (za.za_first_integer != | |
452 | zpool_prop_default_numeric(prop)) | |
453 | src = ZPROP_SRC_LOCAL; | |
454 | ||
455 | if (prop == ZPOOL_PROP_BOOTFS) { | |
456 | dsl_pool_t *dp; | |
457 | dsl_dataset_t *ds = NULL; | |
458 | ||
459 | dp = spa_get_dsl(spa); | |
13fe0198 | 460 | dsl_pool_config_enter(dp, FTAG); |
619f0976 GW |
461 | err = dsl_dataset_hold_obj(dp, |
462 | za.za_first_integer, FTAG, &ds); | |
463 | if (err != 0) { | |
13fe0198 | 464 | dsl_pool_config_exit(dp, FTAG); |
34dc7c2f BB |
465 | break; |
466 | } | |
467 | ||
eca7b760 | 468 | strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, |
79c76d5b | 469 | KM_SLEEP); |
34dc7c2f | 470 | dsl_dataset_name(ds, strval); |
b128c09f | 471 | dsl_dataset_rele(ds, FTAG); |
13fe0198 | 472 | dsl_pool_config_exit(dp, FTAG); |
34dc7c2f BB |
473 | } else { |
474 | strval = NULL; | |
475 | intval = za.za_first_integer; | |
476 | } | |
477 | ||
478 | spa_prop_add_list(*nvp, prop, strval, intval, src); | |
479 | ||
480 | if (strval != NULL) | |
eca7b760 | 481 | kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); |
34dc7c2f BB |
482 | |
483 | break; | |
484 | ||
485 | case 1: | |
486 | /* string property */ | |
79c76d5b | 487 | strval = kmem_alloc(za.za_num_integers, KM_SLEEP); |
34dc7c2f BB |
488 | err = zap_lookup(mos, spa->spa_pool_props_object, |
489 | za.za_name, 1, za.za_num_integers, strval); | |
490 | if (err) { | |
491 | kmem_free(strval, za.za_num_integers); | |
492 | break; | |
493 | } | |
494 | spa_prop_add_list(*nvp, prop, strval, 0, src); | |
495 | kmem_free(strval, za.za_num_integers); | |
496 | break; | |
497 | ||
498 | default: | |
499 | break; | |
500 | } | |
501 | } | |
502 | zap_cursor_fini(&zc); | |
503 | mutex_exit(&spa->spa_props_lock); | |
504 | out: | |
505 | if (err && err != ENOENT) { | |
506 | nvlist_free(*nvp); | |
507 | *nvp = NULL; | |
508 | return (err); | |
509 | } | |
510 | ||
511 | return (0); | |
512 | } | |
513 | ||
514 | /* | |
515 | * Validate the given pool properties nvlist and modify the list | |
516 | * for the property values to be set. | |
517 | */ | |
518 | static int | |
519 | spa_prop_validate(spa_t *spa, nvlist_t *props) | |
520 | { | |
521 | nvpair_t *elem; | |
522 | int error = 0, reset_bootfs = 0; | |
d4ed6673 | 523 | uint64_t objnum = 0; |
9ae529ec | 524 | boolean_t has_feature = B_FALSE; |
34dc7c2f BB |
525 | |
526 | elem = NULL; | |
527 | while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { | |
34dc7c2f | 528 | uint64_t intval; |
9ae529ec CS |
529 | char *strval, *slash, *check, *fname; |
530 | const char *propname = nvpair_name(elem); | |
531 | zpool_prop_t prop = zpool_name_to_prop(propname); | |
532 | ||
31864e3d BB |
533 | switch (prop) { |
534 | case ZPOOL_PROP_INVAL: | |
9ae529ec | 535 | if (!zpool_prop_feature(propname)) { |
2e528b49 | 536 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
537 | break; |
538 | } | |
539 | ||
540 | /* | |
541 | * Sanitize the input. | |
542 | */ | |
543 | if (nvpair_type(elem) != DATA_TYPE_UINT64) { | |
2e528b49 | 544 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
545 | break; |
546 | } | |
547 | ||
548 | if (nvpair_value_uint64(elem, &intval) != 0) { | |
2e528b49 | 549 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
550 | break; |
551 | } | |
34dc7c2f | 552 | |
9ae529ec | 553 | if (intval != 0) { |
2e528b49 | 554 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
555 | break; |
556 | } | |
34dc7c2f | 557 | |
9ae529ec CS |
558 | fname = strchr(propname, '@') + 1; |
559 | if (zfeature_lookup_name(fname, NULL) != 0) { | |
2e528b49 | 560 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
561 | break; |
562 | } | |
563 | ||
564 | has_feature = B_TRUE; | |
565 | break; | |
34dc7c2f | 566 | |
34dc7c2f BB |
567 | case ZPOOL_PROP_VERSION: |
568 | error = nvpair_value_uint64(elem, &intval); | |
569 | if (!error && | |
9ae529ec CS |
570 | (intval < spa_version(spa) || |
571 | intval > SPA_VERSION_BEFORE_FEATURES || | |
572 | has_feature)) | |
2e528b49 | 573 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
574 | break; |
575 | ||
576 | case ZPOOL_PROP_DELEGATION: | |
577 | case ZPOOL_PROP_AUTOREPLACE: | |
b128c09f | 578 | case ZPOOL_PROP_LISTSNAPS: |
9babb374 | 579 | case ZPOOL_PROP_AUTOEXPAND: |
1b939560 | 580 | case ZPOOL_PROP_AUTOTRIM: |
34dc7c2f BB |
581 | error = nvpair_value_uint64(elem, &intval); |
582 | if (!error && intval > 1) | |
2e528b49 | 583 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
584 | break; |
585 | ||
379ca9cf OF |
586 | case ZPOOL_PROP_MULTIHOST: |
587 | error = nvpair_value_uint64(elem, &intval); | |
588 | if (!error && intval > 1) | |
589 | error = SET_ERROR(EINVAL); | |
590 | ||
591 | if (!error && !spa_get_hostid()) | |
592 | error = SET_ERROR(ENOTSUP); | |
593 | ||
594 | break; | |
595 | ||
34dc7c2f | 596 | case ZPOOL_PROP_BOOTFS: |
9babb374 BB |
597 | /* |
598 | * If the pool version is less than SPA_VERSION_BOOTFS, | |
599 | * or the pool is still being created (version == 0), | |
600 | * the bootfs property cannot be set. | |
601 | */ | |
34dc7c2f | 602 | if (spa_version(spa) < SPA_VERSION_BOOTFS) { |
2e528b49 | 603 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
604 | break; |
605 | } | |
606 | ||
607 | /* | |
b128c09f | 608 | * Make sure the vdev config is bootable |
34dc7c2f | 609 | */ |
b128c09f | 610 | if (!vdev_is_bootable(spa->spa_root_vdev)) { |
2e528b49 | 611 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
612 | break; |
613 | } | |
614 | ||
615 | reset_bootfs = 1; | |
616 | ||
617 | error = nvpair_value_string(elem, &strval); | |
618 | ||
619 | if (!error) { | |
9ae529ec | 620 | objset_t *os; |
f1512ee6 | 621 | uint64_t propval; |
b128c09f | 622 | |
34dc7c2f BB |
623 | if (strval == NULL || strval[0] == '\0') { |
624 | objnum = zpool_prop_default_numeric( | |
625 | ZPOOL_PROP_BOOTFS); | |
626 | break; | |
627 | } | |
628 | ||
d1d7e268 | 629 | error = dmu_objset_hold(strval, FTAG, &os); |
619f0976 | 630 | if (error != 0) |
34dc7c2f | 631 | break; |
b128c09f | 632 | |
f1512ee6 MA |
633 | /* |
634 | * Must be ZPL, and its property settings | |
635 | * must be supported by GRUB (compression | |
fcf21f8f RL |
636 | * is not gzip, and large dnodes are not |
637 | * used). | |
f1512ee6 | 638 | */ |
428870ff BB |
639 | |
640 | if (dmu_objset_type(os) != DMU_OST_ZFS) { | |
2e528b49 | 641 | error = SET_ERROR(ENOTSUP); |
13fe0198 MA |
642 | } else if ((error = |
643 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
b128c09f | 644 | zfs_prop_to_name(ZFS_PROP_COMPRESSION), |
f1512ee6 MA |
645 | &propval)) == 0 && |
646 | !BOOTFS_COMPRESS_VALID(propval)) { | |
647 | error = SET_ERROR(ENOTSUP); | |
50c957f7 NB |
648 | } else if ((error = |
649 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
650 | zfs_prop_to_name(ZFS_PROP_DNODESIZE), | |
651 | &propval)) == 0 && | |
652 | propval != ZFS_DNSIZE_LEGACY) { | |
653 | error = SET_ERROR(ENOTSUP); | |
b128c09f BB |
654 | } else { |
655 | objnum = dmu_objset_id(os); | |
656 | } | |
428870ff | 657 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
658 | } |
659 | break; | |
b128c09f | 660 | |
34dc7c2f BB |
661 | case ZPOOL_PROP_FAILUREMODE: |
662 | error = nvpair_value_uint64(elem, &intval); | |
3bfd95d5 | 663 | if (!error && intval > ZIO_FAILURE_MODE_PANIC) |
2e528b49 | 664 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
665 | |
666 | /* | |
667 | * This is a special case which only occurs when | |
668 | * the pool has completely failed. This allows | |
669 | * the user to change the in-core failmode property | |
670 | * without syncing it out to disk (I/Os might | |
671 | * currently be blocked). We do this by returning | |
672 | * EIO to the caller (spa_prop_set) to trick it | |
673 | * into thinking we encountered a property validation | |
674 | * error. | |
675 | */ | |
b128c09f | 676 | if (!error && spa_suspended(spa)) { |
34dc7c2f | 677 | spa->spa_failmode = intval; |
2e528b49 | 678 | error = SET_ERROR(EIO); |
34dc7c2f BB |
679 | } |
680 | break; | |
681 | ||
682 | case ZPOOL_PROP_CACHEFILE: | |
683 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
684 | break; | |
685 | ||
686 | if (strval[0] == '\0') | |
687 | break; | |
688 | ||
689 | if (strcmp(strval, "none") == 0) | |
690 | break; | |
691 | ||
692 | if (strval[0] != '/') { | |
2e528b49 | 693 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
694 | break; |
695 | } | |
696 | ||
697 | slash = strrchr(strval, '/'); | |
698 | ASSERT(slash != NULL); | |
699 | ||
700 | if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || | |
701 | strcmp(slash, "/..") == 0) | |
2e528b49 | 702 | error = SET_ERROR(EINVAL); |
34dc7c2f | 703 | break; |
428870ff | 704 | |
d96eb2b1 DM |
705 | case ZPOOL_PROP_COMMENT: |
706 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
707 | break; | |
708 | for (check = strval; *check != '\0'; check++) { | |
709 | if (!isprint(*check)) { | |
2e528b49 | 710 | error = SET_ERROR(EINVAL); |
d96eb2b1 DM |
711 | break; |
712 | } | |
d96eb2b1 DM |
713 | } |
714 | if (strlen(strval) > ZPROP_MAX_COMMENT) | |
2e528b49 | 715 | error = SET_ERROR(E2BIG); |
d96eb2b1 DM |
716 | break; |
717 | ||
e75c13c3 BB |
718 | default: |
719 | break; | |
34dc7c2f BB |
720 | } |
721 | ||
722 | if (error) | |
723 | break; | |
724 | } | |
725 | ||
050d720c MA |
726 | (void) nvlist_remove_all(props, |
727 | zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO)); | |
728 | ||
34dc7c2f BB |
729 | if (!error && reset_bootfs) { |
730 | error = nvlist_remove(props, | |
731 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); | |
732 | ||
733 | if (!error) { | |
734 | error = nvlist_add_uint64(props, | |
735 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); | |
736 | } | |
737 | } | |
738 | ||
739 | return (error); | |
740 | } | |
741 | ||
d164b209 BB |
742 | void |
743 | spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) | |
744 | { | |
745 | char *cachefile; | |
746 | spa_config_dirent_t *dp; | |
747 | ||
748 | if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
749 | &cachefile) != 0) | |
750 | return; | |
751 | ||
752 | dp = kmem_alloc(sizeof (spa_config_dirent_t), | |
79c76d5b | 753 | KM_SLEEP); |
d164b209 BB |
754 | |
755 | if (cachefile[0] == '\0') | |
756 | dp->scd_path = spa_strdup(spa_config_path); | |
757 | else if (strcmp(cachefile, "none") == 0) | |
758 | dp->scd_path = NULL; | |
759 | else | |
760 | dp->scd_path = spa_strdup(cachefile); | |
761 | ||
762 | list_insert_head(&spa->spa_config_list, dp); | |
763 | if (need_sync) | |
764 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
765 | } | |
766 | ||
34dc7c2f BB |
767 | int |
768 | spa_prop_set(spa_t *spa, nvlist_t *nvp) | |
769 | { | |
770 | int error; | |
9ae529ec | 771 | nvpair_t *elem = NULL; |
d164b209 | 772 | boolean_t need_sync = B_FALSE; |
34dc7c2f BB |
773 | |
774 | if ((error = spa_prop_validate(spa, nvp)) != 0) | |
775 | return (error); | |
776 | ||
d164b209 | 777 | while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { |
9ae529ec | 778 | zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); |
d164b209 | 779 | |
572e2857 BB |
780 | if (prop == ZPOOL_PROP_CACHEFILE || |
781 | prop == ZPOOL_PROP_ALTROOT || | |
782 | prop == ZPOOL_PROP_READONLY) | |
d164b209 BB |
783 | continue; |
784 | ||
31864e3d | 785 | if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { |
9ae529ec CS |
786 | uint64_t ver; |
787 | ||
788 | if (prop == ZPOOL_PROP_VERSION) { | |
789 | VERIFY(nvpair_value_uint64(elem, &ver) == 0); | |
790 | } else { | |
791 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
792 | ver = SPA_VERSION_FEATURES; | |
793 | need_sync = B_TRUE; | |
794 | } | |
795 | ||
796 | /* Save time if the version is already set. */ | |
797 | if (ver == spa_version(spa)) | |
798 | continue; | |
799 | ||
800 | /* | |
801 | * In addition to the pool directory object, we might | |
802 | * create the pool properties object, the features for | |
803 | * read object, the features for write object, or the | |
804 | * feature descriptions object. | |
805 | */ | |
13fe0198 | 806 | error = dsl_sync_task(spa->spa_name, NULL, |
3d45fdd6 MA |
807 | spa_sync_version, &ver, |
808 | 6, ZFS_SPACE_CHECK_RESERVED); | |
9ae529ec CS |
809 | if (error) |
810 | return (error); | |
811 | continue; | |
812 | } | |
813 | ||
d164b209 BB |
814 | need_sync = B_TRUE; |
815 | break; | |
816 | } | |
817 | ||
9ae529ec | 818 | if (need_sync) { |
13fe0198 | 819 | return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, |
3d45fdd6 | 820 | nvp, 6, ZFS_SPACE_CHECK_RESERVED)); |
9ae529ec CS |
821 | } |
822 | ||
823 | return (0); | |
34dc7c2f BB |
824 | } |
825 | ||
826 | /* | |
827 | * If the bootfs property value is dsobj, clear it. | |
828 | */ | |
829 | void | |
830 | spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) | |
831 | { | |
832 | if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { | |
833 | VERIFY(zap_remove(spa->spa_meta_objset, | |
834 | spa->spa_pool_props_object, | |
835 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); | |
836 | spa->spa_bootfs = 0; | |
837 | } | |
838 | } | |
839 | ||
3bc7e0fb GW |
840 | /*ARGSUSED*/ |
841 | static int | |
13fe0198 | 842 | spa_change_guid_check(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 843 | { |
1c27024e | 844 | ASSERTV(uint64_t *newguid = arg); |
13fe0198 | 845 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; |
3bc7e0fb GW |
846 | vdev_t *rvd = spa->spa_root_vdev; |
847 | uint64_t vdev_state; | |
3bc7e0fb | 848 | |
d2734cce SD |
849 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { |
850 | int error = (spa_has_checkpoint(spa)) ? | |
851 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
852 | return (SET_ERROR(error)); | |
853 | } | |
854 | ||
3bc7e0fb GW |
855 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
856 | vdev_state = rvd->vdev_state; | |
857 | spa_config_exit(spa, SCL_STATE, FTAG); | |
858 | ||
859 | if (vdev_state != VDEV_STATE_HEALTHY) | |
2e528b49 | 860 | return (SET_ERROR(ENXIO)); |
3bc7e0fb GW |
861 | |
862 | ASSERT3U(spa_guid(spa), !=, *newguid); | |
863 | ||
864 | return (0); | |
865 | } | |
866 | ||
867 | static void | |
13fe0198 | 868 | spa_change_guid_sync(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 869 | { |
13fe0198 MA |
870 | uint64_t *newguid = arg; |
871 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
3bc7e0fb GW |
872 | uint64_t oldguid; |
873 | vdev_t *rvd = spa->spa_root_vdev; | |
874 | ||
875 | oldguid = spa_guid(spa); | |
876 | ||
877 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
878 | rvd->vdev_guid = *newguid; | |
879 | rvd->vdev_guid_sum += (*newguid - oldguid); | |
880 | vdev_config_dirty(rvd); | |
881 | spa_config_exit(spa, SCL_STATE, FTAG); | |
882 | ||
6f1ffb06 MA |
883 | spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", |
884 | oldguid, *newguid); | |
3bc7e0fb GW |
885 | } |
886 | ||
3541dc6d GA |
887 | /* |
888 | * Change the GUID for the pool. This is done so that we can later | |
889 | * re-import a pool built from a clone of our own vdevs. We will modify | |
890 | * the root vdev's guid, our own pool guid, and then mark all of our | |
891 | * vdevs dirty. Note that we must make sure that all our vdevs are | |
892 | * online when we do this, or else any vdevs that weren't present | |
893 | * would be orphaned from our pool. We are also going to issue a | |
894 | * sysevent to update any watchers. | |
895 | */ | |
896 | int | |
897 | spa_change_guid(spa_t *spa) | |
898 | { | |
3bc7e0fb GW |
899 | int error; |
900 | uint64_t guid; | |
3541dc6d | 901 | |
621dd7bb | 902 | mutex_enter(&spa->spa_vdev_top_lock); |
3bc7e0fb GW |
903 | mutex_enter(&spa_namespace_lock); |
904 | guid = spa_generate_guid(NULL); | |
3541dc6d | 905 | |
13fe0198 | 906 | error = dsl_sync_task(spa->spa_name, spa_change_guid_check, |
3d45fdd6 | 907 | spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); |
3541dc6d | 908 | |
3bc7e0fb | 909 | if (error == 0) { |
a1d477c2 | 910 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 911 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); |
3bc7e0fb | 912 | } |
3541dc6d | 913 | |
3bc7e0fb | 914 | mutex_exit(&spa_namespace_lock); |
621dd7bb | 915 | mutex_exit(&spa->spa_vdev_top_lock); |
3541dc6d | 916 | |
3bc7e0fb | 917 | return (error); |
3541dc6d GA |
918 | } |
919 | ||
34dc7c2f BB |
920 | /* |
921 | * ========================================================================== | |
922 | * SPA state manipulation (open/create/destroy/import/export) | |
923 | * ========================================================================== | |
924 | */ | |
925 | ||
926 | static int | |
927 | spa_error_entry_compare(const void *a, const void *b) | |
928 | { | |
ee36c709 GN |
929 | const spa_error_entry_t *sa = (const spa_error_entry_t *)a; |
930 | const spa_error_entry_t *sb = (const spa_error_entry_t *)b; | |
34dc7c2f BB |
931 | int ret; |
932 | ||
ee36c709 | 933 | ret = memcmp(&sa->se_bookmark, &sb->se_bookmark, |
5dbd68a3 | 934 | sizeof (zbookmark_phys_t)); |
34dc7c2f | 935 | |
ee36c709 | 936 | return (AVL_ISIGN(ret)); |
34dc7c2f BB |
937 | } |
938 | ||
939 | /* | |
940 | * Utility function which retrieves copies of the current logs and | |
941 | * re-initializes them in the process. | |
942 | */ | |
943 | void | |
944 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
945 | { | |
946 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
947 | ||
948 | bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); | |
949 | bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); | |
950 | ||
951 | avl_create(&spa->spa_errlist_scrub, | |
952 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
953 | offsetof(spa_error_entry_t, se_avl)); | |
954 | avl_create(&spa->spa_errlist_last, | |
955 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
956 | offsetof(spa_error_entry_t, se_avl)); | |
957 | } | |
958 | ||
7ef5e54e AL |
959 | static void |
960 | spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
34dc7c2f | 961 | { |
7ef5e54e AL |
962 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; |
963 | enum zti_modes mode = ztip->zti_mode; | |
964 | uint_t value = ztip->zti_value; | |
965 | uint_t count = ztip->zti_count; | |
966 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1c27024e | 967 | uint_t flags = 0; |
428870ff | 968 | boolean_t batch = B_FALSE; |
34dc7c2f | 969 | |
7ef5e54e AL |
970 | if (mode == ZTI_MODE_NULL) { |
971 | tqs->stqs_count = 0; | |
972 | tqs->stqs_taskq = NULL; | |
973 | return; | |
974 | } | |
428870ff | 975 | |
7ef5e54e | 976 | ASSERT3U(count, >, 0); |
428870ff | 977 | |
7ef5e54e AL |
978 | tqs->stqs_count = count; |
979 | tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); | |
428870ff | 980 | |
e8b96c60 MA |
981 | switch (mode) { |
982 | case ZTI_MODE_FIXED: | |
983 | ASSERT3U(value, >=, 1); | |
984 | value = MAX(value, 1); | |
d33931a8 | 985 | flags |= TASKQ_DYNAMIC; |
e8b96c60 | 986 | break; |
7ef5e54e | 987 | |
e8b96c60 MA |
988 | case ZTI_MODE_BATCH: |
989 | batch = B_TRUE; | |
990 | flags |= TASKQ_THREADS_CPU_PCT; | |
dcb6bed1 | 991 | value = MIN(zio_taskq_batch_pct, 100); |
e8b96c60 | 992 | break; |
7ef5e54e | 993 | |
e8b96c60 MA |
994 | default: |
995 | panic("unrecognized mode for %s_%s taskq (%u:%u) in " | |
996 | "spa_activate()", | |
997 | zio_type_name[t], zio_taskq_types[q], mode, value); | |
998 | break; | |
999 | } | |
7ef5e54e | 1000 | |
1c27024e | 1001 | for (uint_t i = 0; i < count; i++) { |
e8b96c60 | 1002 | taskq_t *tq; |
af430294 | 1003 | char name[32]; |
7ef5e54e | 1004 | |
af430294 MA |
1005 | (void) snprintf(name, sizeof (name), "%s_%s", |
1006 | zio_type_name[t], zio_taskq_types[q]); | |
7ef5e54e AL |
1007 | |
1008 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { | |
1009 | if (batch) | |
1010 | flags |= TASKQ_DC_BATCH; | |
1011 | ||
1012 | tq = taskq_create_sysdc(name, value, 50, INT_MAX, | |
1013 | spa->spa_proc, zio_taskq_basedc, flags); | |
1014 | } else { | |
e8b96c60 MA |
1015 | pri_t pri = maxclsyspri; |
1016 | /* | |
1017 | * The write issue taskq can be extremely CPU | |
1229323d BB |
1018 | * intensive. Run it at slightly less important |
1019 | * priority than the other taskqs. Under Linux this | |
1020 | * means incrementing the priority value on platforms | |
1021 | * like illumos it should be decremented. | |
e8b96c60 MA |
1022 | */ |
1023 | if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) | |
1229323d | 1024 | pri++; |
e8b96c60 MA |
1025 | |
1026 | tq = taskq_create_proc(name, value, pri, 50, | |
7ef5e54e AL |
1027 | INT_MAX, spa->spa_proc, flags); |
1028 | } | |
1029 | ||
1030 | tqs->stqs_taskq[i] = tq; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | static void | |
1035 | spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
1036 | { | |
1037 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7ef5e54e AL |
1038 | |
1039 | if (tqs->stqs_taskq == NULL) { | |
1040 | ASSERT3U(tqs->stqs_count, ==, 0); | |
1041 | return; | |
1042 | } | |
1043 | ||
1c27024e | 1044 | for (uint_t i = 0; i < tqs->stqs_count; i++) { |
7ef5e54e AL |
1045 | ASSERT3P(tqs->stqs_taskq[i], !=, NULL); |
1046 | taskq_destroy(tqs->stqs_taskq[i]); | |
428870ff | 1047 | } |
34dc7c2f | 1048 | |
7ef5e54e AL |
1049 | kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); |
1050 | tqs->stqs_taskq = NULL; | |
1051 | } | |
34dc7c2f | 1052 | |
7ef5e54e AL |
1053 | /* |
1054 | * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. | |
1055 | * Note that a type may have multiple discrete taskqs to avoid lock contention | |
1056 | * on the taskq itself. In that case we choose which taskq at random by using | |
1057 | * the low bits of gethrtime(). | |
1058 | */ | |
1059 | void | |
1060 | spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1061 | task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) | |
1062 | { | |
1063 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1064 | taskq_t *tq; | |
1065 | ||
1066 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1067 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1068 | ||
1069 | if (tqs->stqs_count == 1) { | |
1070 | tq = tqs->stqs_taskq[0]; | |
1071 | } else { | |
c12936b1 | 1072 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
428870ff | 1073 | } |
7ef5e54e AL |
1074 | |
1075 | taskq_dispatch_ent(tq, func, arg, flags, ent); | |
428870ff BB |
1076 | } |
1077 | ||
044baf00 BB |
1078 | /* |
1079 | * Same as spa_taskq_dispatch_ent() but block on the task until completion. | |
1080 | */ | |
1081 | void | |
1082 | spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1083 | task_func_t *func, void *arg, uint_t flags) | |
1084 | { | |
1085 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1086 | taskq_t *tq; | |
1087 | taskqid_t id; | |
1088 | ||
1089 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1090 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1091 | ||
1092 | if (tqs->stqs_count == 1) { | |
1093 | tq = tqs->stqs_taskq[0]; | |
1094 | } else { | |
c12936b1 | 1095 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
044baf00 BB |
1096 | } |
1097 | ||
1098 | id = taskq_dispatch(tq, func, arg, flags); | |
1099 | if (id) | |
1100 | taskq_wait_id(tq, id); | |
1101 | } | |
1102 | ||
428870ff BB |
1103 | static void |
1104 | spa_create_zio_taskqs(spa_t *spa) | |
1105 | { | |
1c27024e DB |
1106 | for (int t = 0; t < ZIO_TYPES; t++) { |
1107 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1108 | spa_taskqs_init(spa, t, q); |
428870ff BB |
1109 | } |
1110 | } | |
1111 | } | |
9babb374 | 1112 | |
c25b8f99 BB |
1113 | /* |
1114 | * Disabled until spa_thread() can be adapted for Linux. | |
1115 | */ | |
1116 | #undef HAVE_SPA_THREAD | |
1117 | ||
7b89a549 | 1118 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) |
428870ff BB |
1119 | static void |
1120 | spa_thread(void *arg) | |
1121 | { | |
93ce2b4c | 1122 | psetid_t zio_taskq_psrset_bind = PS_NONE; |
428870ff | 1123 | callb_cpr_t cprinfo; |
9babb374 | 1124 | |
428870ff BB |
1125 | spa_t *spa = arg; |
1126 | user_t *pu = PTOU(curproc); | |
9babb374 | 1127 | |
428870ff BB |
1128 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, |
1129 | spa->spa_name); | |
9babb374 | 1130 | |
428870ff BB |
1131 | ASSERT(curproc != &p0); |
1132 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
1133 | "zpool-%s", spa->spa_name); | |
1134 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
1135 | ||
1136 | /* bind this thread to the requested psrset */ | |
1137 | if (zio_taskq_psrset_bind != PS_NONE) { | |
1138 | pool_lock(); | |
1139 | mutex_enter(&cpu_lock); | |
1140 | mutex_enter(&pidlock); | |
1141 | mutex_enter(&curproc->p_lock); | |
1142 | ||
1143 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
1144 | 0, NULL, NULL) == 0) { | |
1145 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
1146 | } else { | |
1147 | cmn_err(CE_WARN, | |
1148 | "Couldn't bind process for zfs pool \"%s\" to " | |
1149 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
1150 | } | |
1151 | ||
1152 | mutex_exit(&curproc->p_lock); | |
1153 | mutex_exit(&pidlock); | |
1154 | mutex_exit(&cpu_lock); | |
1155 | pool_unlock(); | |
1156 | } | |
1157 | ||
1158 | if (zio_taskq_sysdc) { | |
1159 | sysdc_thread_enter(curthread, 100, 0); | |
1160 | } | |
1161 | ||
1162 | spa->spa_proc = curproc; | |
1163 | spa->spa_did = curthread->t_did; | |
1164 | ||
1165 | spa_create_zio_taskqs(spa); | |
1166 | ||
1167 | mutex_enter(&spa->spa_proc_lock); | |
1168 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
1169 | ||
1170 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
1171 | cv_broadcast(&spa->spa_proc_cv); | |
1172 | ||
1173 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
1174 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
1175 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1176 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
1177 | ||
1178 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
1179 | spa->spa_proc_state = SPA_PROC_GONE; | |
1180 | spa->spa_proc = &p0; | |
1181 | cv_broadcast(&spa->spa_proc_cv); | |
1182 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
1183 | ||
1184 | mutex_enter(&curproc->p_lock); | |
1185 | lwp_exit(); | |
1186 | } | |
1187 | #endif | |
1188 | ||
1189 | /* | |
1190 | * Activate an uninitialized pool. | |
1191 | */ | |
1192 | static void | |
1193 | spa_activate(spa_t *spa, int mode) | |
1194 | { | |
1195 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); | |
1196 | ||
1197 | spa->spa_state = POOL_STATE_ACTIVE; | |
1198 | spa->spa_mode = mode; | |
1199 | ||
1200 | spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
1201 | spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
cc99f275 DB |
1202 | spa->spa_special_class = metaslab_class_create(spa, zfs_metaslab_ops); |
1203 | spa->spa_dedup_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
428870ff BB |
1204 | |
1205 | /* Try to create a covering process */ | |
1206 | mutex_enter(&spa->spa_proc_lock); | |
1207 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
1208 | ASSERT(spa->spa_proc == &p0); | |
1209 | spa->spa_did = 0; | |
1210 | ||
7b89a549 | 1211 | #ifdef HAVE_SPA_THREAD |
428870ff BB |
1212 | /* Only create a process if we're going to be around a while. */ |
1213 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
1214 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
1215 | NULL, 0) == 0) { | |
1216 | spa->spa_proc_state = SPA_PROC_CREATED; | |
1217 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
1218 | cv_wait(&spa->spa_proc_cv, | |
1219 | &spa->spa_proc_lock); | |
9babb374 | 1220 | } |
428870ff BB |
1221 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); |
1222 | ASSERT(spa->spa_proc != &p0); | |
1223 | ASSERT(spa->spa_did != 0); | |
1224 | } else { | |
1225 | #ifdef _KERNEL | |
1226 | cmn_err(CE_WARN, | |
1227 | "Couldn't create process for zfs pool \"%s\"\n", | |
1228 | spa->spa_name); | |
1229 | #endif | |
b128c09f | 1230 | } |
34dc7c2f | 1231 | } |
7b89a549 | 1232 | #endif /* HAVE_SPA_THREAD */ |
428870ff BB |
1233 | mutex_exit(&spa->spa_proc_lock); |
1234 | ||
1235 | /* If we didn't create a process, we need to create our taskqs. */ | |
1236 | if (spa->spa_proc == &p0) { | |
1237 | spa_create_zio_taskqs(spa); | |
1238 | } | |
34dc7c2f | 1239 | |
619f0976 GW |
1240 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1241 | spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, | |
1242 | ZIO_FLAG_CANFAIL); | |
1243 | } | |
a1d477c2 | 1244 | |
b128c09f BB |
1245 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), |
1246 | offsetof(vdev_t, vdev_config_dirty_node)); | |
0c66c32d JG |
1247 | list_create(&spa->spa_evicting_os_list, sizeof (objset_t), |
1248 | offsetof(objset_t, os_evicting_node)); | |
b128c09f BB |
1249 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), |
1250 | offsetof(vdev_t, vdev_state_dirty_node)); | |
34dc7c2f | 1251 | |
4747a7d3 | 1252 | txg_list_create(&spa->spa_vdev_txg_list, spa, |
34dc7c2f BB |
1253 | offsetof(struct vdev, vdev_txg_node)); |
1254 | ||
1255 | avl_create(&spa->spa_errlist_scrub, | |
1256 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1257 | offsetof(spa_error_entry_t, se_avl)); | |
1258 | avl_create(&spa->spa_errlist_last, | |
1259 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1260 | offsetof(spa_error_entry_t, se_avl)); | |
a0bd735a | 1261 | |
b5256303 TC |
1262 | spa_keystore_init(&spa->spa_keystore); |
1263 | ||
a0bd735a BP |
1264 | /* |
1265 | * This taskq is used to perform zvol-minor-related tasks | |
1266 | * asynchronously. This has several advantages, including easy | |
1267 | * resolution of various deadlocks (zfsonlinux bug #3681). | |
1268 | * | |
1269 | * The taskq must be single threaded to ensure tasks are always | |
1270 | * processed in the order in which they were dispatched. | |
1271 | * | |
1272 | * A taskq per pool allows one to keep the pools independent. | |
1273 | * This way if one pool is suspended, it will not impact another. | |
1274 | * | |
1275 | * The preferred location to dispatch a zvol minor task is a sync | |
1276 | * task. In this context, there is easy access to the spa_t and minimal | |
1277 | * error handling is required because the sync task must succeed. | |
1278 | */ | |
1279 | spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri, | |
1280 | 1, INT_MAX, 0); | |
1de321e6 | 1281 | |
77d8a0f1 | 1282 | /* |
1283 | * Taskq dedicated to prefetcher threads: this is used to prevent the | |
1284 | * pool traverse code from monopolizing the global (and limited) | |
1285 | * system_taskq by inappropriately scheduling long running tasks on it. | |
1286 | */ | |
1287 | spa->spa_prefetch_taskq = taskq_create("z_prefetch", boot_ncpus, | |
1288 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); | |
1289 | ||
1de321e6 JX |
1290 | /* |
1291 | * The taskq to upgrade datasets in this pool. Currently used by | |
9c5167d1 | 1292 | * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA. |
1de321e6 JX |
1293 | */ |
1294 | spa->spa_upgrade_taskq = taskq_create("z_upgrade", boot_ncpus, | |
1295 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); | |
34dc7c2f BB |
1296 | } |
1297 | ||
1298 | /* | |
1299 | * Opposite of spa_activate(). | |
1300 | */ | |
1301 | static void | |
1302 | spa_deactivate(spa_t *spa) | |
1303 | { | |
34dc7c2f BB |
1304 | ASSERT(spa->spa_sync_on == B_FALSE); |
1305 | ASSERT(spa->spa_dsl_pool == NULL); | |
1306 | ASSERT(spa->spa_root_vdev == NULL); | |
9babb374 | 1307 | ASSERT(spa->spa_async_zio_root == NULL); |
34dc7c2f BB |
1308 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); |
1309 | ||
0c66c32d JG |
1310 | spa_evicting_os_wait(spa); |
1311 | ||
a0bd735a BP |
1312 | if (spa->spa_zvol_taskq) { |
1313 | taskq_destroy(spa->spa_zvol_taskq); | |
1314 | spa->spa_zvol_taskq = NULL; | |
1315 | } | |
1316 | ||
77d8a0f1 | 1317 | if (spa->spa_prefetch_taskq) { |
1318 | taskq_destroy(spa->spa_prefetch_taskq); | |
1319 | spa->spa_prefetch_taskq = NULL; | |
1320 | } | |
1321 | ||
1de321e6 JX |
1322 | if (spa->spa_upgrade_taskq) { |
1323 | taskq_destroy(spa->spa_upgrade_taskq); | |
1324 | spa->spa_upgrade_taskq = NULL; | |
1325 | } | |
1326 | ||
34dc7c2f BB |
1327 | txg_list_destroy(&spa->spa_vdev_txg_list); |
1328 | ||
b128c09f | 1329 | list_destroy(&spa->spa_config_dirty_list); |
0c66c32d | 1330 | list_destroy(&spa->spa_evicting_os_list); |
b128c09f | 1331 | list_destroy(&spa->spa_state_dirty_list); |
34dc7c2f | 1332 | |
57ddcda1 | 1333 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 | 1334 | |
1c27024e DB |
1335 | for (int t = 0; t < ZIO_TYPES; t++) { |
1336 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1337 | spa_taskqs_fini(spa, t, q); |
b128c09f | 1338 | } |
34dc7c2f BB |
1339 | } |
1340 | ||
a1d477c2 MA |
1341 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1342 | ASSERT3P(spa->spa_txg_zio[i], !=, NULL); | |
1343 | VERIFY0(zio_wait(spa->spa_txg_zio[i])); | |
1344 | spa->spa_txg_zio[i] = NULL; | |
1345 | } | |
1346 | ||
34dc7c2f BB |
1347 | metaslab_class_destroy(spa->spa_normal_class); |
1348 | spa->spa_normal_class = NULL; | |
1349 | ||
1350 | metaslab_class_destroy(spa->spa_log_class); | |
1351 | spa->spa_log_class = NULL; | |
1352 | ||
cc99f275 DB |
1353 | metaslab_class_destroy(spa->spa_special_class); |
1354 | spa->spa_special_class = NULL; | |
1355 | ||
1356 | metaslab_class_destroy(spa->spa_dedup_class); | |
1357 | spa->spa_dedup_class = NULL; | |
1358 | ||
34dc7c2f BB |
1359 | /* |
1360 | * If this was part of an import or the open otherwise failed, we may | |
1361 | * still have errors left in the queues. Empty them just in case. | |
1362 | */ | |
1363 | spa_errlog_drain(spa); | |
34dc7c2f BB |
1364 | avl_destroy(&spa->spa_errlist_scrub); |
1365 | avl_destroy(&spa->spa_errlist_last); | |
1366 | ||
b5256303 TC |
1367 | spa_keystore_fini(&spa->spa_keystore); |
1368 | ||
34dc7c2f | 1369 | spa->spa_state = POOL_STATE_UNINITIALIZED; |
428870ff BB |
1370 | |
1371 | mutex_enter(&spa->spa_proc_lock); | |
1372 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1373 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1374 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1375 | cv_broadcast(&spa->spa_proc_cv); | |
1376 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1377 | ASSERT(spa->spa_proc != &p0); | |
1378 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1379 | } | |
1380 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1381 | spa->spa_proc_state = SPA_PROC_NONE; | |
1382 | } | |
1383 | ASSERT(spa->spa_proc == &p0); | |
1384 | mutex_exit(&spa->spa_proc_lock); | |
1385 | ||
1386 | /* | |
1387 | * We want to make sure spa_thread() has actually exited the ZFS | |
1388 | * module, so that the module can't be unloaded out from underneath | |
1389 | * it. | |
1390 | */ | |
1391 | if (spa->spa_did != 0) { | |
1392 | thread_join(spa->spa_did); | |
1393 | spa->spa_did = 0; | |
1394 | } | |
34dc7c2f BB |
1395 | } |
1396 | ||
1397 | /* | |
1398 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1399 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1400 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1401 | * All vdev validation is done by the vdev_alloc() routine. | |
1402 | */ | |
1403 | static int | |
1404 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, | |
1405 | uint_t id, int atype) | |
1406 | { | |
1407 | nvlist_t **child; | |
9babb374 | 1408 | uint_t children; |
34dc7c2f BB |
1409 | int error; |
1410 | ||
1411 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1412 | return (error); | |
1413 | ||
1414 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1415 | return (0); | |
1416 | ||
b128c09f BB |
1417 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1418 | &child, &children); | |
1419 | ||
1420 | if (error == ENOENT) | |
1421 | return (0); | |
1422 | ||
1423 | if (error) { | |
34dc7c2f BB |
1424 | vdev_free(*vdp); |
1425 | *vdp = NULL; | |
2e528b49 | 1426 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1427 | } |
1428 | ||
1c27024e | 1429 | for (int c = 0; c < children; c++) { |
34dc7c2f BB |
1430 | vdev_t *vd; |
1431 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1432 | atype)) != 0) { | |
1433 | vdev_free(*vdp); | |
1434 | *vdp = NULL; | |
1435 | return (error); | |
1436 | } | |
1437 | } | |
1438 | ||
1439 | ASSERT(*vdp != NULL); | |
1440 | ||
1441 | return (0); | |
1442 | } | |
1443 | ||
93e28d66 SD |
1444 | static boolean_t |
1445 | spa_should_flush_logs_on_unload(spa_t *spa) | |
1446 | { | |
1447 | if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) | |
1448 | return (B_FALSE); | |
1449 | ||
1450 | if (!spa_writeable(spa)) | |
1451 | return (B_FALSE); | |
1452 | ||
1453 | if (!spa->spa_sync_on) | |
1454 | return (B_FALSE); | |
1455 | ||
1456 | if (spa_state(spa) != POOL_STATE_EXPORTED) | |
1457 | return (B_FALSE); | |
1458 | ||
1459 | if (zfs_keep_log_spacemaps_at_export) | |
1460 | return (B_FALSE); | |
1461 | ||
1462 | return (B_TRUE); | |
1463 | } | |
1464 | ||
1465 | /* | |
1466 | * Opens a transaction that will set the flag that will instruct | |
1467 | * spa_sync to attempt to flush all the metaslabs for that txg. | |
1468 | */ | |
1469 | static void | |
1470 | spa_unload_log_sm_flush_all(spa_t *spa) | |
1471 | { | |
1472 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
1473 | VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); | |
1474 | ||
1475 | ASSERT3U(spa->spa_log_flushall_txg, ==, 0); | |
1476 | spa->spa_log_flushall_txg = dmu_tx_get_txg(tx); | |
1477 | ||
1478 | dmu_tx_commit(tx); | |
1479 | txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg); | |
1480 | } | |
1481 | ||
1482 | static void | |
1483 | spa_unload_log_sm_metadata(spa_t *spa) | |
1484 | { | |
1485 | void *cookie = NULL; | |
1486 | spa_log_sm_t *sls; | |
1487 | while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg, | |
1488 | &cookie)) != NULL) { | |
1489 | VERIFY0(sls->sls_mscount); | |
1490 | kmem_free(sls, sizeof (spa_log_sm_t)); | |
1491 | } | |
1492 | ||
1493 | for (log_summary_entry_t *e = list_head(&spa->spa_log_summary); | |
1494 | e != NULL; e = list_head(&spa->spa_log_summary)) { | |
1495 | VERIFY0(e->lse_mscount); | |
1496 | list_remove(&spa->spa_log_summary, e); | |
1497 | kmem_free(e, sizeof (log_summary_entry_t)); | |
1498 | } | |
1499 | ||
1500 | spa->spa_unflushed_stats.sus_nblocks = 0; | |
1501 | spa->spa_unflushed_stats.sus_memused = 0; | |
1502 | spa->spa_unflushed_stats.sus_blocklimit = 0; | |
1503 | } | |
1504 | ||
37f03da8 SH |
1505 | static void |
1506 | spa_destroy_aux_threads(spa_t *spa) | |
1507 | { | |
1508 | if (spa->spa_condense_zthr != NULL) { | |
1509 | zthr_destroy(spa->spa_condense_zthr); | |
1510 | spa->spa_condense_zthr = NULL; | |
1511 | } | |
1512 | if (spa->spa_checkpoint_discard_zthr != NULL) { | |
1513 | zthr_destroy(spa->spa_checkpoint_discard_zthr); | |
1514 | spa->spa_checkpoint_discard_zthr = NULL; | |
1515 | } | |
1516 | if (spa->spa_livelist_delete_zthr != NULL) { | |
1517 | zthr_destroy(spa->spa_livelist_delete_zthr); | |
1518 | spa->spa_livelist_delete_zthr = NULL; | |
1519 | } | |
1520 | if (spa->spa_livelist_condense_zthr != NULL) { | |
1521 | zthr_destroy(spa->spa_livelist_condense_zthr); | |
1522 | spa->spa_livelist_condense_zthr = NULL; | |
1523 | } | |
1524 | } | |
1525 | ||
34dc7c2f BB |
1526 | /* |
1527 | * Opposite of spa_load(). | |
1528 | */ | |
1529 | static void | |
1530 | spa_unload(spa_t *spa) | |
1531 | { | |
b128c09f | 1532 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
93e28d66 | 1533 | ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED); |
b128c09f | 1534 | |
ca95f70d | 1535 | spa_import_progress_remove(spa_guid(spa)); |
4a0ee12a PZ |
1536 | spa_load_note(spa, "UNLOADING"); |
1537 | ||
93e28d66 SD |
1538 | /* |
1539 | * If the log space map feature is enabled and the pool is getting | |
1540 | * exported (but not destroyed), we want to spend some time flushing | |
1541 | * as many metaslabs as we can in an attempt to destroy log space | |
1542 | * maps and save import time. | |
1543 | */ | |
1544 | if (spa_should_flush_logs_on_unload(spa)) | |
1545 | spa_unload_log_sm_flush_all(spa); | |
1546 | ||
34dc7c2f BB |
1547 | /* |
1548 | * Stop async tasks. | |
1549 | */ | |
1550 | spa_async_suspend(spa); | |
1551 | ||
619f0976 | 1552 | if (spa->spa_root_vdev) { |
1b939560 BB |
1553 | vdev_t *root_vdev = spa->spa_root_vdev; |
1554 | vdev_initialize_stop_all(root_vdev, VDEV_INITIALIZE_ACTIVE); | |
1555 | vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE); | |
1556 | vdev_autotrim_stop_all(spa); | |
619f0976 GW |
1557 | } |
1558 | ||
34dc7c2f BB |
1559 | /* |
1560 | * Stop syncing. | |
1561 | */ | |
1562 | if (spa->spa_sync_on) { | |
1563 | txg_sync_stop(spa->spa_dsl_pool); | |
1564 | spa->spa_sync_on = B_FALSE; | |
1565 | } | |
1566 | ||
4e21fd06 | 1567 | /* |
93e28d66 SD |
1568 | * This ensures that there is no async metaslab prefetching |
1569 | * while we attempt to unload the spa. | |
4e21fd06 DB |
1570 | */ |
1571 | if (spa->spa_root_vdev != NULL) { | |
93e28d66 SD |
1572 | for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) { |
1573 | vdev_t *vc = spa->spa_root_vdev->vdev_child[c]; | |
1574 | if (vc->vdev_mg != NULL) | |
1575 | taskq_wait(vc->vdev_mg->mg_taskq); | |
1576 | } | |
4e21fd06 DB |
1577 | } |
1578 | ||
379ca9cf OF |
1579 | if (spa->spa_mmp.mmp_thread) |
1580 | mmp_thread_stop(spa); | |
1581 | ||
34dc7c2f | 1582 | /* |
b128c09f | 1583 | * Wait for any outstanding async I/O to complete. |
34dc7c2f | 1584 | */ |
9babb374 | 1585 | if (spa->spa_async_zio_root != NULL) { |
1c27024e | 1586 | for (int i = 0; i < max_ncpus; i++) |
e022864d MA |
1587 | (void) zio_wait(spa->spa_async_zio_root[i]); |
1588 | kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); | |
9babb374 BB |
1589 | spa->spa_async_zio_root = NULL; |
1590 | } | |
34dc7c2f | 1591 | |
a1d477c2 MA |
1592 | if (spa->spa_vdev_removal != NULL) { |
1593 | spa_vdev_removal_destroy(spa->spa_vdev_removal); | |
1594 | spa->spa_vdev_removal = NULL; | |
1595 | } | |
1596 | ||
37f03da8 | 1597 | spa_destroy_aux_threads(spa); |
d2734cce | 1598 | |
a1d477c2 MA |
1599 | spa_condense_fini(spa); |
1600 | ||
428870ff BB |
1601 | bpobj_close(&spa->spa_deferred_bpobj); |
1602 | ||
619f0976 | 1603 | spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); |
93cf2076 GW |
1604 | |
1605 | /* | |
1606 | * Close all vdevs. | |
1607 | */ | |
1608 | if (spa->spa_root_vdev) | |
1609 | vdev_free(spa->spa_root_vdev); | |
1610 | ASSERT(spa->spa_root_vdev == NULL); | |
1611 | ||
34dc7c2f BB |
1612 | /* |
1613 | * Close the dsl pool. | |
1614 | */ | |
1615 | if (spa->spa_dsl_pool) { | |
1616 | dsl_pool_close(spa->spa_dsl_pool); | |
1617 | spa->spa_dsl_pool = NULL; | |
428870ff | 1618 | spa->spa_meta_objset = NULL; |
34dc7c2f BB |
1619 | } |
1620 | ||
428870ff | 1621 | ddt_unload(spa); |
93e28d66 | 1622 | spa_unload_log_sm_metadata(spa); |
428870ff | 1623 | |
fb5f0bc8 BB |
1624 | /* |
1625 | * Drop and purge level 2 cache | |
1626 | */ | |
1627 | spa_l2cache_drop(spa); | |
1628 | ||
93e28d66 | 1629 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
34dc7c2f BB |
1630 | vdev_free(spa->spa_spares.sav_vdevs[i]); |
1631 | if (spa->spa_spares.sav_vdevs) { | |
1632 | kmem_free(spa->spa_spares.sav_vdevs, | |
1633 | spa->spa_spares.sav_count * sizeof (void *)); | |
1634 | spa->spa_spares.sav_vdevs = NULL; | |
1635 | } | |
1636 | if (spa->spa_spares.sav_config) { | |
1637 | nvlist_free(spa->spa_spares.sav_config); | |
1638 | spa->spa_spares.sav_config = NULL; | |
1639 | } | |
b128c09f | 1640 | spa->spa_spares.sav_count = 0; |
34dc7c2f | 1641 | |
93e28d66 | 1642 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { |
5ffb9d1d | 1643 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); |
34dc7c2f | 1644 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); |
5ffb9d1d | 1645 | } |
34dc7c2f BB |
1646 | if (spa->spa_l2cache.sav_vdevs) { |
1647 | kmem_free(spa->spa_l2cache.sav_vdevs, | |
1648 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1649 | spa->spa_l2cache.sav_vdevs = NULL; | |
1650 | } | |
1651 | if (spa->spa_l2cache.sav_config) { | |
1652 | nvlist_free(spa->spa_l2cache.sav_config); | |
1653 | spa->spa_l2cache.sav_config = NULL; | |
1654 | } | |
b128c09f | 1655 | spa->spa_l2cache.sav_count = 0; |
34dc7c2f BB |
1656 | |
1657 | spa->spa_async_suspended = 0; | |
fb5f0bc8 | 1658 | |
a1d477c2 MA |
1659 | spa->spa_indirect_vdevs_loaded = B_FALSE; |
1660 | ||
d96eb2b1 DM |
1661 | if (spa->spa_comment != NULL) { |
1662 | spa_strfree(spa->spa_comment); | |
1663 | spa->spa_comment = NULL; | |
1664 | } | |
1665 | ||
619f0976 | 1666 | spa_config_exit(spa, SCL_ALL, spa); |
34dc7c2f BB |
1667 | } |
1668 | ||
1669 | /* | |
1670 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1671 | * this pool. When this is called, we have some form of basic information in | |
1672 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1673 | * then re-generate a more complete list including status information. | |
1674 | */ | |
a1d477c2 | 1675 | void |
34dc7c2f BB |
1676 | spa_load_spares(spa_t *spa) |
1677 | { | |
1678 | nvlist_t **spares; | |
1679 | uint_t nspares; | |
1680 | int i; | |
1681 | vdev_t *vd, *tvd; | |
1682 | ||
d2734cce SD |
1683 | #ifndef _KERNEL |
1684 | /* | |
1685 | * zdb opens both the current state of the pool and the | |
1686 | * checkpointed state (if present), with a different spa_t. | |
1687 | * | |
1688 | * As spare vdevs are shared among open pools, we skip loading | |
1689 | * them when we load the checkpointed state of the pool. | |
1690 | */ | |
1691 | if (!spa_writeable(spa)) | |
1692 | return; | |
1693 | #endif | |
1694 | ||
b128c09f BB |
1695 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1696 | ||
34dc7c2f BB |
1697 | /* |
1698 | * First, close and free any existing spare vdevs. | |
1699 | */ | |
1700 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1701 | vd = spa->spa_spares.sav_vdevs[i]; | |
1702 | ||
1703 | /* Undo the call to spa_activate() below */ | |
b128c09f BB |
1704 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1705 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
34dc7c2f BB |
1706 | spa_spare_remove(tvd); |
1707 | vdev_close(vd); | |
1708 | vdev_free(vd); | |
1709 | } | |
1710 | ||
1711 | if (spa->spa_spares.sav_vdevs) | |
1712 | kmem_free(spa->spa_spares.sav_vdevs, | |
1713 | spa->spa_spares.sav_count * sizeof (void *)); | |
1714 | ||
1715 | if (spa->spa_spares.sav_config == NULL) | |
1716 | nspares = 0; | |
1717 | else | |
1718 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
1719 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
1720 | ||
1721 | spa->spa_spares.sav_count = (int)nspares; | |
1722 | spa->spa_spares.sav_vdevs = NULL; | |
1723 | ||
1724 | if (nspares == 0) | |
1725 | return; | |
1726 | ||
1727 | /* | |
1728 | * Construct the array of vdevs, opening them to get status in the | |
1729 | * process. For each spare, there is potentially two different vdev_t | |
1730 | * structures associated with it: one in the list of spares (used only | |
1731 | * for basic validation purposes) and one in the active vdev | |
1732 | * configuration (if it's spared in). During this phase we open and | |
1733 | * validate each vdev on the spare list. If the vdev also exists in the | |
1734 | * active configuration, then we also mark this vdev as an active spare. | |
1735 | */ | |
904ea276 | 1736 | spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *), |
79c76d5b | 1737 | KM_SLEEP); |
34dc7c2f BB |
1738 | for (i = 0; i < spa->spa_spares.sav_count; i++) { |
1739 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1740 | VDEV_ALLOC_SPARE) == 0); | |
1741 | ASSERT(vd != NULL); | |
1742 | ||
1743 | spa->spa_spares.sav_vdevs[i] = vd; | |
1744 | ||
b128c09f BB |
1745 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1746 | B_FALSE)) != NULL) { | |
34dc7c2f BB |
1747 | if (!tvd->vdev_isspare) |
1748 | spa_spare_add(tvd); | |
1749 | ||
1750 | /* | |
1751 | * We only mark the spare active if we were successfully | |
1752 | * able to load the vdev. Otherwise, importing a pool | |
1753 | * with a bad active spare would result in strange | |
1754 | * behavior, because multiple pool would think the spare | |
1755 | * is actively in use. | |
1756 | * | |
1757 | * There is a vulnerability here to an equally bizarre | |
1758 | * circumstance, where a dead active spare is later | |
1759 | * brought back to life (onlined or otherwise). Given | |
1760 | * the rarity of this scenario, and the extra complexity | |
1761 | * it adds, we ignore the possibility. | |
1762 | */ | |
1763 | if (!vdev_is_dead(tvd)) | |
1764 | spa_spare_activate(tvd); | |
1765 | } | |
1766 | ||
b128c09f | 1767 | vd->vdev_top = vd; |
9babb374 | 1768 | vd->vdev_aux = &spa->spa_spares; |
b128c09f | 1769 | |
34dc7c2f BB |
1770 | if (vdev_open(vd) != 0) |
1771 | continue; | |
1772 | ||
34dc7c2f BB |
1773 | if (vdev_validate_aux(vd) == 0) |
1774 | spa_spare_add(vd); | |
1775 | } | |
1776 | ||
1777 | /* | |
1778 | * Recompute the stashed list of spares, with status information | |
1779 | * this time. | |
1780 | */ | |
1781 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, | |
1782 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1783 | ||
1784 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
79c76d5b | 1785 | KM_SLEEP); |
34dc7c2f BB |
1786 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1787 | spares[i] = vdev_config_generate(spa, | |
428870ff | 1788 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); |
34dc7c2f BB |
1789 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
1790 | ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); | |
1791 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1792 | nvlist_free(spares[i]); | |
1793 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1794 | } | |
1795 | ||
1796 | /* | |
1797 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1798 | * this pool. When this is called, we have some form of basic information in | |
1799 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1800 | * then re-generate a more complete list including status information. | |
1801 | * Devices which are already active have their details maintained, and are | |
1802 | * not re-opened. | |
1803 | */ | |
a1d477c2 | 1804 | void |
34dc7c2f BB |
1805 | spa_load_l2cache(spa_t *spa) |
1806 | { | |
460f239e | 1807 | nvlist_t **l2cache = NULL; |
34dc7c2f BB |
1808 | uint_t nl2cache; |
1809 | int i, j, oldnvdevs; | |
9babb374 | 1810 | uint64_t guid; |
a117a6d6 | 1811 | vdev_t *vd, **oldvdevs, **newvdevs; |
34dc7c2f BB |
1812 | spa_aux_vdev_t *sav = &spa->spa_l2cache; |
1813 | ||
d2734cce SD |
1814 | #ifndef _KERNEL |
1815 | /* | |
1816 | * zdb opens both the current state of the pool and the | |
1817 | * checkpointed state (if present), with a different spa_t. | |
1818 | * | |
1819 | * As L2 caches are part of the ARC which is shared among open | |
1820 | * pools, we skip loading them when we load the checkpointed | |
1821 | * state of the pool. | |
1822 | */ | |
1823 | if (!spa_writeable(spa)) | |
1824 | return; | |
1825 | #endif | |
1826 | ||
b128c09f BB |
1827 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1828 | ||
34dc7c2f BB |
1829 | oldvdevs = sav->sav_vdevs; |
1830 | oldnvdevs = sav->sav_count; | |
1831 | sav->sav_vdevs = NULL; | |
1832 | sav->sav_count = 0; | |
1833 | ||
67d60824 NB |
1834 | if (sav->sav_config == NULL) { |
1835 | nl2cache = 0; | |
1836 | newvdevs = NULL; | |
1837 | goto out; | |
1838 | } | |
1839 | ||
1840 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, | |
1841 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
1842 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); | |
1843 | ||
34dc7c2f BB |
1844 | /* |
1845 | * Process new nvlist of vdevs. | |
1846 | */ | |
1847 | for (i = 0; i < nl2cache; i++) { | |
1848 | VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, | |
1849 | &guid) == 0); | |
1850 | ||
1851 | newvdevs[i] = NULL; | |
1852 | for (j = 0; j < oldnvdevs; j++) { | |
1853 | vd = oldvdevs[j]; | |
1854 | if (vd != NULL && guid == vd->vdev_guid) { | |
1855 | /* | |
1856 | * Retain previous vdev for add/remove ops. | |
1857 | */ | |
1858 | newvdevs[i] = vd; | |
1859 | oldvdevs[j] = NULL; | |
1860 | break; | |
1861 | } | |
1862 | } | |
1863 | ||
1864 | if (newvdevs[i] == NULL) { | |
1865 | /* | |
1866 | * Create new vdev | |
1867 | */ | |
1868 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
1869 | VDEV_ALLOC_L2CACHE) == 0); | |
1870 | ASSERT(vd != NULL); | |
1871 | newvdevs[i] = vd; | |
1872 | ||
1873 | /* | |
1874 | * Commit this vdev as an l2cache device, | |
1875 | * even if it fails to open. | |
1876 | */ | |
1877 | spa_l2cache_add(vd); | |
1878 | ||
b128c09f BB |
1879 | vd->vdev_top = vd; |
1880 | vd->vdev_aux = sav; | |
1881 | ||
1882 | spa_l2cache_activate(vd); | |
1883 | ||
34dc7c2f BB |
1884 | if (vdev_open(vd) != 0) |
1885 | continue; | |
1886 | ||
34dc7c2f BB |
1887 | (void) vdev_validate_aux(vd); |
1888 | ||
9babb374 BB |
1889 | if (!vdev_is_dead(vd)) |
1890 | l2arc_add_vdev(spa, vd); | |
34dc7c2f BB |
1891 | } |
1892 | } | |
1893 | ||
67d60824 NB |
1894 | sav->sav_vdevs = newvdevs; |
1895 | sav->sav_count = (int)nl2cache; | |
1896 | ||
1897 | /* | |
1898 | * Recompute the stashed list of l2cache devices, with status | |
1899 | * information this time. | |
1900 | */ | |
1901 | VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, | |
1902 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1903 | ||
460f239e D |
1904 | if (sav->sav_count > 0) |
1905 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), | |
1906 | KM_SLEEP); | |
67d60824 NB |
1907 | for (i = 0; i < sav->sav_count; i++) |
1908 | l2cache[i] = vdev_config_generate(spa, | |
1909 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); | |
1910 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
1911 | ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); | |
1912 | ||
1913 | out: | |
34dc7c2f BB |
1914 | /* |
1915 | * Purge vdevs that were dropped | |
1916 | */ | |
1917 | for (i = 0; i < oldnvdevs; i++) { | |
1918 | uint64_t pool; | |
1919 | ||
1920 | vd = oldvdevs[i]; | |
1921 | if (vd != NULL) { | |
5ffb9d1d GW |
1922 | ASSERT(vd->vdev_isl2cache); |
1923 | ||
fb5f0bc8 BB |
1924 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
1925 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 1926 | l2arc_remove_vdev(vd); |
5ffb9d1d GW |
1927 | vdev_clear_stats(vd); |
1928 | vdev_free(vd); | |
34dc7c2f BB |
1929 | } |
1930 | } | |
1931 | ||
1932 | if (oldvdevs) | |
1933 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); | |
1934 | ||
34dc7c2f BB |
1935 | for (i = 0; i < sav->sav_count; i++) |
1936 | nvlist_free(l2cache[i]); | |
1937 | if (sav->sav_count) | |
1938 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
1939 | } | |
1940 | ||
1941 | static int | |
1942 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
1943 | { | |
1944 | dmu_buf_t *db; | |
1945 | char *packed = NULL; | |
1946 | size_t nvsize = 0; | |
1947 | int error; | |
1948 | *value = NULL; | |
1949 | ||
c3275b56 BB |
1950 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); |
1951 | if (error) | |
1952 | return (error); | |
1953 | ||
34dc7c2f BB |
1954 | nvsize = *(uint64_t *)db->db_data; |
1955 | dmu_buf_rele(db, FTAG); | |
1956 | ||
77aef6f6 | 1957 | packed = vmem_alloc(nvsize, KM_SLEEP); |
9babb374 BB |
1958 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, |
1959 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
1960 | if (error == 0) |
1961 | error = nvlist_unpack(packed, nvsize, value, 0); | |
77aef6f6 | 1962 | vmem_free(packed, nvsize); |
34dc7c2f BB |
1963 | |
1964 | return (error); | |
1965 | } | |
1966 | ||
6cb8e530 PZ |
1967 | /* |
1968 | * Concrete top-level vdevs that are not missing and are not logs. At every | |
1969 | * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. | |
1970 | */ | |
1971 | static uint64_t | |
1972 | spa_healthy_core_tvds(spa_t *spa) | |
1973 | { | |
1974 | vdev_t *rvd = spa->spa_root_vdev; | |
1975 | uint64_t tvds = 0; | |
1976 | ||
1977 | for (uint64_t i = 0; i < rvd->vdev_children; i++) { | |
1978 | vdev_t *vd = rvd->vdev_child[i]; | |
1979 | if (vd->vdev_islog) | |
1980 | continue; | |
1981 | if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) | |
1982 | tvds++; | |
1983 | } | |
1984 | ||
1985 | return (tvds); | |
1986 | } | |
1987 | ||
34dc7c2f BB |
1988 | /* |
1989 | * Checks to see if the given vdev could not be opened, in which case we post a | |
1990 | * sysevent to notify the autoreplace code that the device has been removed. | |
1991 | */ | |
1992 | static void | |
1993 | spa_check_removed(vdev_t *vd) | |
1994 | { | |
6cb8e530 | 1995 | for (uint64_t c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
1996 | spa_check_removed(vd->vdev_child[c]); |
1997 | ||
7011fb60 | 1998 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && |
a1d477c2 | 1999 | vdev_is_concrete(vd)) { |
fb390aaf | 2000 | zfs_post_autoreplace(vd->vdev_spa, vd); |
12fa0466 | 2001 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); |
34dc7c2f BB |
2002 | } |
2003 | } | |
2004 | ||
6cb8e530 PZ |
2005 | static int |
2006 | spa_check_for_missing_logs(spa_t *spa) | |
9babb374 | 2007 | { |
6cb8e530 | 2008 | vdev_t *rvd = spa->spa_root_vdev; |
9babb374 | 2009 | |
428870ff | 2010 | /* |
572e2857 | 2011 | * If we're doing a normal import, then build up any additional |
6cb8e530 | 2012 | * diagnostic information about missing log devices. |
572e2857 | 2013 | * We'll pass this up to the user for further processing. |
428870ff | 2014 | */ |
572e2857 BB |
2015 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { |
2016 | nvlist_t **child, *nv; | |
2017 | uint64_t idx = 0; | |
2018 | ||
160987b5 | 2019 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *), |
79c76d5b BB |
2020 | KM_SLEEP); |
2021 | VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
572e2857 | 2022 | |
6cb8e530 | 2023 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { |
572e2857 | 2024 | vdev_t *tvd = rvd->vdev_child[c]; |
572e2857 | 2025 | |
6cb8e530 PZ |
2026 | /* |
2027 | * We consider a device as missing only if it failed | |
2028 | * to open (i.e. offline or faulted is not considered | |
2029 | * as missing). | |
2030 | */ | |
2031 | if (tvd->vdev_islog && | |
2032 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
2033 | child[idx++] = vdev_config_generate(spa, tvd, | |
2034 | B_FALSE, VDEV_CONFIG_MISSING); | |
2035 | } | |
572e2857 | 2036 | } |
9babb374 | 2037 | |
6cb8e530 PZ |
2038 | if (idx > 0) { |
2039 | fnvlist_add_nvlist_array(nv, | |
2040 | ZPOOL_CONFIG_CHILDREN, child, idx); | |
2041 | fnvlist_add_nvlist(spa->spa_load_info, | |
2042 | ZPOOL_CONFIG_MISSING_DEVICES, nv); | |
572e2857 | 2043 | |
6cb8e530 | 2044 | for (uint64_t i = 0; i < idx; i++) |
572e2857 BB |
2045 | nvlist_free(child[i]); |
2046 | } | |
2047 | nvlist_free(nv); | |
2048 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
572e2857 | 2049 | |
6cb8e530 PZ |
2050 | if (idx > 0) { |
2051 | spa_load_failed(spa, "some log devices are missing"); | |
db7d07e1 | 2052 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 PZ |
2053 | return (SET_ERROR(ENXIO)); |
2054 | } | |
2055 | } else { | |
2056 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
2057 | vdev_t *tvd = rvd->vdev_child[c]; | |
a1d477c2 | 2058 | |
6cb8e530 PZ |
2059 | if (tvd->vdev_islog && |
2060 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
572e2857 | 2061 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
6cb8e530 PZ |
2062 | spa_load_note(spa, "some log devices are " |
2063 | "missing, ZIL is dropped."); | |
db7d07e1 | 2064 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 | 2065 | break; |
e0ab3ab5 | 2066 | } |
572e2857 | 2067 | } |
9babb374 | 2068 | } |
e0ab3ab5 | 2069 | |
6cb8e530 | 2070 | return (0); |
9babb374 BB |
2071 | } |
2072 | ||
b128c09f BB |
2073 | /* |
2074 | * Check for missing log devices | |
2075 | */ | |
13fe0198 | 2076 | static boolean_t |
b128c09f BB |
2077 | spa_check_logs(spa_t *spa) |
2078 | { | |
13fe0198 | 2079 | boolean_t rv = B_FALSE; |
9c43027b | 2080 | dsl_pool_t *dp = spa_get_dsl(spa); |
13fe0198 | 2081 | |
b128c09f | 2082 | switch (spa->spa_log_state) { |
e75c13c3 BB |
2083 | default: |
2084 | break; | |
b128c09f BB |
2085 | case SPA_LOG_MISSING: |
2086 | /* need to recheck in case slog has been restored */ | |
2087 | case SPA_LOG_UNKNOWN: | |
9c43027b AJ |
2088 | rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, |
2089 | zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); | |
13fe0198 | 2090 | if (rv) |
428870ff | 2091 | spa_set_log_state(spa, SPA_LOG_MISSING); |
b128c09f | 2092 | break; |
b128c09f | 2093 | } |
13fe0198 | 2094 | return (rv); |
b128c09f BB |
2095 | } |
2096 | ||
428870ff BB |
2097 | static boolean_t |
2098 | spa_passivate_log(spa_t *spa) | |
34dc7c2f | 2099 | { |
428870ff BB |
2100 | vdev_t *rvd = spa->spa_root_vdev; |
2101 | boolean_t slog_found = B_FALSE; | |
b128c09f | 2102 | |
428870ff | 2103 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
fb5f0bc8 | 2104 | |
428870ff BB |
2105 | if (!spa_has_slogs(spa)) |
2106 | return (B_FALSE); | |
34dc7c2f | 2107 | |
1c27024e | 2108 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
2109 | vdev_t *tvd = rvd->vdev_child[c]; |
2110 | metaslab_group_t *mg = tvd->vdev_mg; | |
34dc7c2f | 2111 | |
428870ff BB |
2112 | if (tvd->vdev_islog) { |
2113 | metaslab_group_passivate(mg); | |
2114 | slog_found = B_TRUE; | |
2115 | } | |
34dc7c2f BB |
2116 | } |
2117 | ||
428870ff BB |
2118 | return (slog_found); |
2119 | } | |
34dc7c2f | 2120 | |
428870ff BB |
2121 | static void |
2122 | spa_activate_log(spa_t *spa) | |
2123 | { | |
2124 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 2125 | |
428870ff BB |
2126 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
2127 | ||
1c27024e | 2128 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
2129 | vdev_t *tvd = rvd->vdev_child[c]; |
2130 | metaslab_group_t *mg = tvd->vdev_mg; | |
2131 | ||
2132 | if (tvd->vdev_islog) | |
2133 | metaslab_group_activate(mg); | |
34dc7c2f | 2134 | } |
428870ff | 2135 | } |
34dc7c2f | 2136 | |
428870ff | 2137 | int |
a1d477c2 | 2138 | spa_reset_logs(spa_t *spa) |
428870ff | 2139 | { |
13fe0198 | 2140 | int error; |
9babb374 | 2141 | |
a1d477c2 | 2142 | error = dmu_objset_find(spa_name(spa), zil_reset, |
13fe0198 MA |
2143 | NULL, DS_FIND_CHILDREN); |
2144 | if (error == 0) { | |
428870ff BB |
2145 | /* |
2146 | * We successfully offlined the log device, sync out the | |
2147 | * current txg so that the "stubby" block can be removed | |
2148 | * by zil_sync(). | |
2149 | */ | |
2150 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
2151 | } | |
2152 | return (error); | |
2153 | } | |
34dc7c2f | 2154 | |
428870ff BB |
2155 | static void |
2156 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
2157 | { | |
1c27024e | 2158 | for (int i = 0; i < sav->sav_count; i++) |
428870ff BB |
2159 | spa_check_removed(sav->sav_vdevs[i]); |
2160 | } | |
34dc7c2f | 2161 | |
428870ff BB |
2162 | void |
2163 | spa_claim_notify(zio_t *zio) | |
2164 | { | |
2165 | spa_t *spa = zio->io_spa; | |
34dc7c2f | 2166 | |
428870ff BB |
2167 | if (zio->io_error) |
2168 | return; | |
34dc7c2f | 2169 | |
428870ff BB |
2170 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ |
2171 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
2172 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
2173 | mutex_exit(&spa->spa_props_lock); | |
2174 | } | |
34dc7c2f | 2175 | |
428870ff BB |
2176 | typedef struct spa_load_error { |
2177 | uint64_t sle_meta_count; | |
2178 | uint64_t sle_data_count; | |
2179 | } spa_load_error_t; | |
34dc7c2f | 2180 | |
428870ff BB |
2181 | static void |
2182 | spa_load_verify_done(zio_t *zio) | |
2183 | { | |
2184 | blkptr_t *bp = zio->io_bp; | |
2185 | spa_load_error_t *sle = zio->io_private; | |
2186 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
2187 | int error = zio->io_error; | |
dea377c0 | 2188 | spa_t *spa = zio->io_spa; |
34dc7c2f | 2189 | |
a6255b7f | 2190 | abd_free(zio->io_abd); |
428870ff | 2191 | if (error) { |
9ae529ec | 2192 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && |
428870ff | 2193 | type != DMU_OT_INTENT_LOG) |
bc89ac84 | 2194 | atomic_inc_64(&sle->sle_meta_count); |
428870ff | 2195 | else |
bc89ac84 | 2196 | atomic_inc_64(&sle->sle_data_count); |
34dc7c2f | 2197 | } |
dea377c0 MA |
2198 | |
2199 | mutex_enter(&spa->spa_scrub_lock); | |
d4a72f23 | 2200 | spa->spa_load_verify_ios--; |
dea377c0 MA |
2201 | cv_broadcast(&spa->spa_scrub_io_cv); |
2202 | mutex_exit(&spa->spa_scrub_lock); | |
428870ff | 2203 | } |
34dc7c2f | 2204 | |
dea377c0 MA |
2205 | /* |
2206 | * Maximum number of concurrent scrub i/os to create while verifying | |
2207 | * a pool while importing it. | |
2208 | */ | |
2209 | int spa_load_verify_maxinflight = 10000; | |
2210 | int spa_load_verify_metadata = B_TRUE; | |
2211 | int spa_load_verify_data = B_TRUE; | |
2212 | ||
428870ff BB |
2213 | /*ARGSUSED*/ |
2214 | static int | |
2215 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
5dbd68a3 | 2216 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) |
428870ff | 2217 | { |
30af21b0 PD |
2218 | if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) || |
2219 | BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp)) | |
dea377c0 MA |
2220 | return (0); |
2221 | /* | |
2222 | * Note: normally this routine will not be called if | |
2223 | * spa_load_verify_metadata is not set. However, it may be useful | |
2224 | * to manually set the flag after the traversal has begun. | |
2225 | */ | |
2226 | if (!spa_load_verify_metadata) | |
2227 | return (0); | |
a6255b7f | 2228 | if (!BP_IS_METADATA(bp) && !spa_load_verify_data) |
dea377c0 MA |
2229 | return (0); |
2230 | ||
1c27024e DB |
2231 | zio_t *rio = arg; |
2232 | size_t size = BP_GET_PSIZE(bp); | |
dea377c0 MA |
2233 | |
2234 | mutex_enter(&spa->spa_scrub_lock); | |
d4a72f23 | 2235 | while (spa->spa_load_verify_ios >= spa_load_verify_maxinflight) |
dea377c0 | 2236 | cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); |
d4a72f23 | 2237 | spa->spa_load_verify_ios++; |
dea377c0 MA |
2238 | mutex_exit(&spa->spa_scrub_lock); |
2239 | ||
a6255b7f | 2240 | zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, |
dea377c0 MA |
2241 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, |
2242 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
2243 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
428870ff BB |
2244 | return (0); |
2245 | } | |
34dc7c2f | 2246 | |
d1d19c78 PD |
2247 | /* ARGSUSED */ |
2248 | int | |
2249 | verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) | |
2250 | { | |
2251 | if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) | |
2252 | return (SET_ERROR(ENAMETOOLONG)); | |
2253 | ||
2254 | return (0); | |
2255 | } | |
2256 | ||
428870ff BB |
2257 | static int |
2258 | spa_load_verify(spa_t *spa) | |
2259 | { | |
2260 | zio_t *rio; | |
2261 | spa_load_error_t sle = { 0 }; | |
8a393be3 | 2262 | zpool_load_policy_t policy; |
428870ff | 2263 | boolean_t verify_ok = B_FALSE; |
dea377c0 | 2264 | int error = 0; |
34dc7c2f | 2265 | |
8a393be3 | 2266 | zpool_get_load_policy(spa->spa_config, &policy); |
34dc7c2f | 2267 | |
8a393be3 | 2268 | if (policy.zlp_rewind & ZPOOL_NEVER_REWIND) |
428870ff | 2269 | return (0); |
34dc7c2f | 2270 | |
d1d19c78 PD |
2271 | dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); |
2272 | error = dmu_objset_find_dp(spa->spa_dsl_pool, | |
2273 | spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, | |
2274 | DS_FIND_CHILDREN); | |
2275 | dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); | |
2276 | if (error != 0) | |
2277 | return (error); | |
2278 | ||
428870ff BB |
2279 | rio = zio_root(spa, NULL, &sle, |
2280 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
34dc7c2f | 2281 | |
dea377c0 | 2282 | if (spa_load_verify_metadata) { |
4a0ee12a PZ |
2283 | if (spa->spa_extreme_rewind) { |
2284 | spa_load_note(spa, "performing a complete scan of the " | |
2285 | "pool since extreme rewind is on. This may take " | |
2286 | "a very long time.\n (spa_load_verify_data=%u, " | |
2287 | "spa_load_verify_metadata=%u)", | |
2288 | spa_load_verify_data, spa_load_verify_metadata); | |
2289 | } | |
dea377c0 | 2290 | error = traverse_pool(spa, spa->spa_verify_min_txg, |
b5256303 TC |
2291 | TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | |
2292 | TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio); | |
dea377c0 | 2293 | } |
428870ff BB |
2294 | |
2295 | (void) zio_wait(rio); | |
2296 | ||
2297 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
2298 | spa->spa_load_data_errors = sle.sle_data_count; | |
2299 | ||
afd2f7b7 PZ |
2300 | if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { |
2301 | spa_load_note(spa, "spa_load_verify found %llu metadata errors " | |
2302 | "and %llu data errors", (u_longlong_t)sle.sle_meta_count, | |
2303 | (u_longlong_t)sle.sle_data_count); | |
2304 | } | |
2305 | ||
2306 | if (spa_load_verify_dryrun || | |
8a393be3 PZ |
2307 | (!error && sle.sle_meta_count <= policy.zlp_maxmeta && |
2308 | sle.sle_data_count <= policy.zlp_maxdata)) { | |
572e2857 BB |
2309 | int64_t loss = 0; |
2310 | ||
428870ff BB |
2311 | verify_ok = B_TRUE; |
2312 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
2313 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
572e2857 BB |
2314 | |
2315 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
2316 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2317 | ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); | |
2318 | VERIFY(nvlist_add_int64(spa->spa_load_info, | |
2319 | ZPOOL_CONFIG_REWIND_TIME, loss) == 0); | |
2320 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2321 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); | |
428870ff BB |
2322 | } else { |
2323 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
2324 | } | |
2325 | ||
afd2f7b7 PZ |
2326 | if (spa_load_verify_dryrun) |
2327 | return (0); | |
2328 | ||
428870ff BB |
2329 | if (error) { |
2330 | if (error != ENXIO && error != EIO) | |
2e528b49 | 2331 | error = SET_ERROR(EIO); |
428870ff BB |
2332 | return (error); |
2333 | } | |
2334 | ||
2335 | return (verify_ok ? 0 : EIO); | |
2336 | } | |
2337 | ||
2338 | /* | |
2339 | * Find a value in the pool props object. | |
2340 | */ | |
2341 | static void | |
2342 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
2343 | { | |
2344 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
2345 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
2346 | } | |
2347 | ||
2348 | /* | |
2349 | * Find a value in the pool directory object. | |
2350 | */ | |
2351 | static int | |
4a0ee12a | 2352 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) |
428870ff | 2353 | { |
4a0ee12a PZ |
2354 | int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
2355 | name, sizeof (uint64_t), 1, val); | |
2356 | ||
2357 | if (error != 0 && (error != ENOENT || log_enoent)) { | |
2358 | spa_load_failed(spa, "couldn't get '%s' value in MOS directory " | |
2359 | "[error=%d]", name, error); | |
2360 | } | |
2361 | ||
2362 | return (error); | |
428870ff BB |
2363 | } |
2364 | ||
2365 | static int | |
2366 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
2367 | { | |
2368 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
a1d477c2 | 2369 | return (SET_ERROR(err)); |
428870ff BB |
2370 | } |
2371 | ||
37f03da8 SH |
2372 | boolean_t |
2373 | spa_livelist_delete_check(spa_t *spa) | |
2374 | { | |
2375 | return (spa->spa_livelists_to_delete != 0); | |
2376 | } | |
2377 | ||
2378 | /* ARGSUSED */ | |
2379 | static boolean_t | |
2380 | spa_livelist_delete_cb_check(void *arg, zthr_t *z) | |
2381 | { | |
2382 | spa_t *spa = arg; | |
2383 | return (spa_livelist_delete_check(spa)); | |
2384 | } | |
2385 | ||
2386 | static int | |
2387 | delete_blkptr_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
2388 | { | |
2389 | spa_t *spa = arg; | |
2390 | zio_free(spa, tx->tx_txg, bp); | |
2391 | dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD, | |
2392 | -bp_get_dsize_sync(spa, bp), | |
2393 | -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx); | |
2394 | return (0); | |
2395 | } | |
2396 | ||
2397 | static int | |
2398 | dsl_get_next_livelist_obj(objset_t *os, uint64_t zap_obj, uint64_t *llp) | |
2399 | { | |
2400 | int err; | |
2401 | zap_cursor_t zc; | |
2402 | zap_attribute_t za; | |
2403 | zap_cursor_init(&zc, os, zap_obj); | |
2404 | err = zap_cursor_retrieve(&zc, &za); | |
2405 | zap_cursor_fini(&zc); | |
2406 | if (err == 0) | |
2407 | *llp = za.za_first_integer; | |
2408 | return (err); | |
2409 | } | |
2410 | ||
2411 | /* | |
2412 | * Components of livelist deletion that must be performed in syncing | |
2413 | * context: freeing block pointers and updating the pool-wide data | |
2414 | * structures to indicate how much work is left to do | |
2415 | */ | |
2416 | typedef struct sublist_delete_arg { | |
2417 | spa_t *spa; | |
2418 | dsl_deadlist_t *ll; | |
2419 | uint64_t key; | |
2420 | bplist_t *to_free; | |
2421 | } sublist_delete_arg_t; | |
2422 | ||
2423 | static void | |
2424 | sublist_delete_sync(void *arg, dmu_tx_t *tx) | |
2425 | { | |
2426 | sublist_delete_arg_t *sda = arg; | |
2427 | spa_t *spa = sda->spa; | |
2428 | dsl_deadlist_t *ll = sda->ll; | |
2429 | uint64_t key = sda->key; | |
2430 | bplist_t *to_free = sda->to_free; | |
2431 | ||
2432 | bplist_iterate(to_free, delete_blkptr_cb, spa, tx); | |
2433 | dsl_deadlist_remove_entry(ll, key, tx); | |
2434 | } | |
2435 | ||
2436 | typedef struct livelist_delete_arg { | |
2437 | spa_t *spa; | |
2438 | uint64_t ll_obj; | |
2439 | uint64_t zap_obj; | |
2440 | } livelist_delete_arg_t; | |
2441 | ||
2442 | static void | |
2443 | livelist_delete_sync(void *arg, dmu_tx_t *tx) | |
2444 | { | |
2445 | livelist_delete_arg_t *lda = arg; | |
2446 | spa_t *spa = lda->spa; | |
2447 | uint64_t ll_obj = lda->ll_obj; | |
2448 | uint64_t zap_obj = lda->zap_obj; | |
2449 | objset_t *mos = spa->spa_meta_objset; | |
2450 | uint64_t count; | |
2451 | ||
2452 | /* free the livelist and decrement the feature count */ | |
2453 | VERIFY0(zap_remove_int(mos, zap_obj, ll_obj, tx)); | |
2454 | dsl_deadlist_free(mos, ll_obj, tx); | |
2455 | spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx); | |
2456 | VERIFY0(zap_count(mos, zap_obj, &count)); | |
2457 | if (count == 0) { | |
2458 | /* no more livelists to delete */ | |
2459 | VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT, | |
2460 | DMU_POOL_DELETED_CLONES, tx)); | |
2461 | VERIFY0(zap_destroy(mos, zap_obj, tx)); | |
2462 | spa->spa_livelists_to_delete = 0; | |
2463 | } | |
2464 | } | |
2465 | ||
2466 | /* | |
2467 | * Load in the value for the livelist to be removed and open it. Then, | |
2468 | * load its first sublist and determine which block pointers should actually | |
2469 | * be freed. Then, call a synctask which performs the actual frees and updates | |
2470 | * the pool-wide livelist data. | |
2471 | */ | |
2472 | /* ARGSUSED */ | |
2473 | void | |
2474 | spa_livelist_delete_cb(void *arg, zthr_t *z) | |
2475 | { | |
2476 | spa_t *spa = arg; | |
2477 | uint64_t ll_obj = 0, count; | |
2478 | objset_t *mos = spa->spa_meta_objset; | |
2479 | uint64_t zap_obj = spa->spa_livelists_to_delete; | |
2480 | /* | |
2481 | * Determine the next livelist to delete. This function should only | |
2482 | * be called if there is at least one deleted clone. | |
2483 | */ | |
2484 | VERIFY0(dsl_get_next_livelist_obj(mos, zap_obj, &ll_obj)); | |
2485 | VERIFY0(zap_count(mos, ll_obj, &count)); | |
2486 | if (count > 0) { | |
2487 | dsl_deadlist_t ll = { 0 }; | |
2488 | dsl_deadlist_entry_t *dle; | |
2489 | bplist_t to_free; | |
2490 | dsl_deadlist_open(&ll, mos, ll_obj); | |
2491 | dle = dsl_deadlist_first(&ll); | |
2492 | ASSERT3P(dle, !=, NULL); | |
2493 | bplist_create(&to_free); | |
2494 | int err = dsl_process_sub_livelist(&dle->dle_bpobj, &to_free, | |
2495 | z, NULL); | |
2496 | if (err == 0) { | |
2497 | sublist_delete_arg_t sync_arg = { | |
2498 | .spa = spa, | |
2499 | .ll = &ll, | |
2500 | .key = dle->dle_mintxg, | |
2501 | .to_free = &to_free | |
2502 | }; | |
2503 | zfs_dbgmsg("deleting sublist (id %llu) from" | |
2504 | " livelist %llu, %d remaining", | |
2505 | dle->dle_bpobj.bpo_object, ll_obj, count - 1); | |
2506 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, | |
2507 | sublist_delete_sync, &sync_arg, 0, | |
2508 | ZFS_SPACE_CHECK_DESTROY)); | |
2509 | } else { | |
2510 | ASSERT(err == EINTR); | |
2511 | } | |
2512 | bplist_clear(&to_free); | |
2513 | bplist_destroy(&to_free); | |
2514 | dsl_deadlist_close(&ll); | |
2515 | } else { | |
2516 | livelist_delete_arg_t sync_arg = { | |
2517 | .spa = spa, | |
2518 | .ll_obj = ll_obj, | |
2519 | .zap_obj = zap_obj | |
2520 | }; | |
2521 | zfs_dbgmsg("deletion of livelist %llu completed", ll_obj); | |
2522 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, livelist_delete_sync, | |
2523 | &sync_arg, 0, ZFS_SPACE_CHECK_DESTROY)); | |
2524 | } | |
2525 | } | |
2526 | ||
2527 | void | |
2528 | spa_start_livelist_destroy_thread(spa_t *spa) | |
2529 | { | |
2530 | ASSERT3P(spa->spa_livelist_delete_zthr, ==, NULL); | |
2531 | spa->spa_livelist_delete_zthr = zthr_create( | |
2532 | spa_livelist_delete_cb_check, spa_livelist_delete_cb, spa); | |
2533 | } | |
2534 | ||
2535 | typedef struct livelist_new_arg { | |
2536 | bplist_t *allocs; | |
2537 | bplist_t *frees; | |
2538 | } livelist_new_arg_t; | |
2539 | ||
2540 | static int | |
2541 | livelist_track_new_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
2542 | dmu_tx_t *tx) | |
2543 | { | |
2544 | ASSERT(tx == NULL); | |
2545 | livelist_new_arg_t *lna = arg; | |
2546 | if (bp_freed) { | |
2547 | bplist_append(lna->frees, bp); | |
2548 | } else { | |
2549 | bplist_append(lna->allocs, bp); | |
2550 | zfs_livelist_condense_new_alloc++; | |
2551 | } | |
2552 | return (0); | |
2553 | } | |
2554 | ||
2555 | typedef struct livelist_condense_arg { | |
2556 | spa_t *spa; | |
2557 | bplist_t to_keep; | |
2558 | uint64_t first_size; | |
2559 | uint64_t next_size; | |
2560 | } livelist_condense_arg_t; | |
2561 | ||
2562 | static void | |
2563 | spa_livelist_condense_sync(void *arg, dmu_tx_t *tx) | |
2564 | { | |
2565 | livelist_condense_arg_t *lca = arg; | |
2566 | spa_t *spa = lca->spa; | |
2567 | bplist_t new_frees; | |
2568 | dsl_dataset_t *ds = spa->spa_to_condense.ds; | |
2569 | ||
2570 | /* Have we been cancelled? */ | |
2571 | if (spa->spa_to_condense.cancelled) { | |
2572 | zfs_livelist_condense_sync_cancel++; | |
2573 | goto out; | |
2574 | } | |
2575 | ||
2576 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2577 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2578 | dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist; | |
2579 | ||
2580 | /* | |
2581 | * It's possible that the livelist was changed while the zthr was | |
2582 | * running. Therefore, we need to check for new blkptrs in the two | |
2583 | * entries being condensed and continue to track them in the livelist. | |
2584 | * Because of the way we handle remapped blkptrs (see dbuf_remap_impl), | |
2585 | * it's possible that the newly added blkptrs are FREEs or ALLOCs so | |
2586 | * we need to sort them into two different bplists. | |
2587 | */ | |
2588 | uint64_t first_obj = first->dle_bpobj.bpo_object; | |
2589 | uint64_t next_obj = next->dle_bpobj.bpo_object; | |
2590 | uint64_t cur_first_size = first->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2591 | uint64_t cur_next_size = next->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2592 | ||
2593 | bplist_create(&new_frees); | |
2594 | livelist_new_arg_t new_bps = { | |
2595 | .allocs = &lca->to_keep, | |
2596 | .frees = &new_frees, | |
2597 | }; | |
2598 | ||
2599 | if (cur_first_size > lca->first_size) { | |
2600 | VERIFY0(livelist_bpobj_iterate_from_nofree(&first->dle_bpobj, | |
2601 | livelist_track_new_cb, &new_bps, lca->first_size)); | |
2602 | } | |
2603 | if (cur_next_size > lca->next_size) { | |
2604 | VERIFY0(livelist_bpobj_iterate_from_nofree(&next->dle_bpobj, | |
2605 | livelist_track_new_cb, &new_bps, lca->next_size)); | |
2606 | } | |
2607 | ||
2608 | dsl_deadlist_clear_entry(first, ll, tx); | |
2609 | ASSERT(bpobj_is_empty(&first->dle_bpobj)); | |
2610 | dsl_deadlist_remove_entry(ll, next->dle_mintxg, tx); | |
2611 | ||
2612 | bplist_iterate(&lca->to_keep, dsl_deadlist_insert_alloc_cb, ll, tx); | |
2613 | bplist_iterate(&new_frees, dsl_deadlist_insert_free_cb, ll, tx); | |
2614 | bplist_destroy(&new_frees); | |
2615 | ||
2616 | char dsname[ZFS_MAX_DATASET_NAME_LEN]; | |
2617 | dsl_dataset_name(ds, dsname); | |
2618 | zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu " | |
2619 | "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu " | |
2620 | "(%llu blkptrs)", tx->tx_txg, dsname, ds->ds_object, first_obj, | |
2621 | cur_first_size, next_obj, cur_next_size, | |
2622 | first->dle_bpobj.bpo_object, | |
2623 | first->dle_bpobj.bpo_phys->bpo_num_blkptrs); | |
2624 | out: | |
2625 | dmu_buf_rele(ds->ds_dbuf, spa); | |
2626 | spa->spa_to_condense.ds = NULL; | |
2627 | bplist_clear(&lca->to_keep); | |
2628 | bplist_destroy(&lca->to_keep); | |
2629 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2630 | spa->spa_to_condense.syncing = B_FALSE; | |
2631 | } | |
2632 | ||
2633 | void | |
2634 | spa_livelist_condense_cb(void *arg, zthr_t *t) | |
2635 | { | |
2636 | while (zfs_livelist_condense_zthr_pause && | |
2637 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2638 | delay(1); | |
2639 | ||
2640 | spa_t *spa = arg; | |
2641 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2642 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2643 | uint64_t first_size, next_size; | |
2644 | ||
2645 | livelist_condense_arg_t *lca = | |
2646 | kmem_alloc(sizeof (livelist_condense_arg_t), KM_SLEEP); | |
2647 | bplist_create(&lca->to_keep); | |
2648 | ||
2649 | /* | |
2650 | * Process the livelists (matching FREEs and ALLOCs) in open context | |
2651 | * so we have minimal work in syncing context to condense. | |
2652 | * | |
2653 | * We save bpobj sizes (first_size and next_size) to use later in | |
2654 | * syncing context to determine if entries were added to these sublists | |
2655 | * while in open context. This is possible because the clone is still | |
2656 | * active and open for normal writes and we want to make sure the new, | |
2657 | * unprocessed blockpointers are inserted into the livelist normally. | |
2658 | * | |
2659 | * Note that dsl_process_sub_livelist() both stores the size number of | |
2660 | * blockpointers and iterates over them while the bpobj's lock held, so | |
2661 | * the sizes returned to us are consistent which what was actually | |
2662 | * processed. | |
2663 | */ | |
2664 | int err = dsl_process_sub_livelist(&first->dle_bpobj, &lca->to_keep, t, | |
2665 | &first_size); | |
2666 | if (err == 0) | |
2667 | err = dsl_process_sub_livelist(&next->dle_bpobj, &lca->to_keep, | |
2668 | t, &next_size); | |
2669 | ||
2670 | if (err == 0) { | |
2671 | while (zfs_livelist_condense_sync_pause && | |
2672 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2673 | delay(1); | |
2674 | ||
2675 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
2676 | dmu_tx_mark_netfree(tx); | |
2677 | dmu_tx_hold_space(tx, 1); | |
2678 | err = dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE); | |
2679 | if (err == 0) { | |
2680 | /* | |
2681 | * Prevent the condense zthr restarting before | |
2682 | * the synctask completes. | |
2683 | */ | |
2684 | spa->spa_to_condense.syncing = B_TRUE; | |
2685 | lca->spa = spa; | |
2686 | lca->first_size = first_size; | |
2687 | lca->next_size = next_size; | |
2688 | dsl_sync_task_nowait(spa_get_dsl(spa), | |
2689 | spa_livelist_condense_sync, lca, 0, | |
2690 | ZFS_SPACE_CHECK_NONE, tx); | |
2691 | dmu_tx_commit(tx); | |
2692 | return; | |
2693 | } | |
2694 | } | |
2695 | /* | |
2696 | * Condensing can not continue: either it was externally stopped or | |
2697 | * we were unable to assign to a tx because the pool has run out of | |
2698 | * space. In the second case, we'll just end up trying to condense | |
2699 | * again in a later txg. | |
2700 | */ | |
2701 | ASSERT(err != 0); | |
2702 | bplist_clear(&lca->to_keep); | |
2703 | bplist_destroy(&lca->to_keep); | |
2704 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2705 | dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, spa); | |
2706 | spa->spa_to_condense.ds = NULL; | |
2707 | if (err == EINTR) | |
2708 | zfs_livelist_condense_zthr_cancel++; | |
2709 | } | |
2710 | ||
2711 | /* ARGSUSED */ | |
2712 | /* | |
2713 | * Check that there is something to condense but that a condense is not | |
2714 | * already in progress and that condensing has not been cancelled. | |
2715 | */ | |
2716 | static boolean_t | |
2717 | spa_livelist_condense_cb_check(void *arg, zthr_t *z) | |
2718 | { | |
2719 | spa_t *spa = arg; | |
2720 | if ((spa->spa_to_condense.ds != NULL) && | |
2721 | (spa->spa_to_condense.syncing == B_FALSE) && | |
2722 | (spa->spa_to_condense.cancelled == B_FALSE)) { | |
2723 | return (B_TRUE); | |
2724 | } | |
2725 | return (B_FALSE); | |
2726 | } | |
2727 | ||
2728 | void | |
2729 | spa_start_livelist_condensing_thread(spa_t *spa) | |
2730 | { | |
2731 | spa->spa_to_condense.ds = NULL; | |
2732 | spa->spa_to_condense.first = NULL; | |
2733 | spa->spa_to_condense.next = NULL; | |
2734 | spa->spa_to_condense.syncing = B_FALSE; | |
2735 | spa->spa_to_condense.cancelled = B_FALSE; | |
2736 | ||
2737 | ASSERT3P(spa->spa_livelist_condense_zthr, ==, NULL); | |
2738 | spa->spa_livelist_condense_zthr = zthr_create( | |
2739 | spa_livelist_condense_cb_check, spa_livelist_condense_cb, spa); | |
2740 | } | |
2741 | ||
9d5b5245 SD |
2742 | static void |
2743 | spa_spawn_aux_threads(spa_t *spa) | |
2744 | { | |
2745 | ASSERT(spa_writeable(spa)); | |
2746 | ||
2747 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2748 | ||
2749 | spa_start_indirect_condensing_thread(spa); | |
37f03da8 SH |
2750 | spa_start_livelist_destroy_thread(spa); |
2751 | spa_start_livelist_condensing_thread(spa); | |
d2734cce SD |
2752 | |
2753 | ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); | |
2754 | spa->spa_checkpoint_discard_zthr = | |
2755 | zthr_create(spa_checkpoint_discard_thread_check, | |
2756 | spa_checkpoint_discard_thread, spa); | |
9d5b5245 SD |
2757 | } |
2758 | ||
428870ff BB |
2759 | /* |
2760 | * Fix up config after a partly-completed split. This is done with the | |
2761 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
2762 | * pool have that entry in their config, but only the splitting one contains | |
2763 | * a list of all the guids of the vdevs that are being split off. | |
2764 | * | |
2765 | * This function determines what to do with that list: either rejoin | |
2766 | * all the disks to the pool, or complete the splitting process. To attempt | |
2767 | * the rejoin, each disk that is offlined is marked online again, and | |
2768 | * we do a reopen() call. If the vdev label for every disk that was | |
2769 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
2770 | * then we call vdev_split() on each disk, and complete the split. | |
2771 | * | |
2772 | * Otherwise we leave the config alone, with all the vdevs in place in | |
2773 | * the original pool. | |
2774 | */ | |
2775 | static void | |
2776 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
2777 | { | |
2778 | uint_t extracted; | |
2779 | uint64_t *glist; | |
2780 | uint_t i, gcount; | |
2781 | nvlist_t *nvl; | |
2782 | vdev_t **vd; | |
2783 | boolean_t attempt_reopen; | |
2784 | ||
2785 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
2786 | return; | |
2787 | ||
2788 | /* check that the config is complete */ | |
2789 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
2790 | &glist, &gcount) != 0) | |
2791 | return; | |
2792 | ||
79c76d5b | 2793 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); |
428870ff BB |
2794 | |
2795 | /* attempt to online all the vdevs & validate */ | |
2796 | attempt_reopen = B_TRUE; | |
2797 | for (i = 0; i < gcount; i++) { | |
2798 | if (glist[i] == 0) /* vdev is hole */ | |
2799 | continue; | |
2800 | ||
2801 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
2802 | if (vd[i] == NULL) { | |
2803 | /* | |
2804 | * Don't bother attempting to reopen the disks; | |
2805 | * just do the split. | |
2806 | */ | |
2807 | attempt_reopen = B_FALSE; | |
2808 | } else { | |
2809 | /* attempt to re-online it */ | |
2810 | vd[i]->vdev_offline = B_FALSE; | |
2811 | } | |
2812 | } | |
2813 | ||
2814 | if (attempt_reopen) { | |
2815 | vdev_reopen(spa->spa_root_vdev); | |
2816 | ||
2817 | /* check each device to see what state it's in */ | |
2818 | for (extracted = 0, i = 0; i < gcount; i++) { | |
2819 | if (vd[i] != NULL && | |
2820 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
2821 | break; | |
2822 | ++extracted; | |
2823 | } | |
2824 | } | |
2825 | ||
2826 | /* | |
2827 | * If every disk has been moved to the new pool, or if we never | |
2828 | * even attempted to look at them, then we split them off for | |
2829 | * good. | |
2830 | */ | |
2831 | if (!attempt_reopen || gcount == extracted) { | |
2832 | for (i = 0; i < gcount; i++) | |
2833 | if (vd[i] != NULL) | |
2834 | vdev_split(vd[i]); | |
2835 | vdev_reopen(spa->spa_root_vdev); | |
2836 | } | |
2837 | ||
2838 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
2839 | } | |
2840 | ||
2841 | static int | |
6cb8e530 | 2842 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) |
428870ff | 2843 | { |
428870ff BB |
2844 | char *ereport = FM_EREPORT_ZFS_POOL; |
2845 | int error; | |
428870ff | 2846 | |
6cb8e530 | 2847 | spa->spa_load_state = state; |
ca95f70d OF |
2848 | (void) spa_import_progress_set_state(spa_guid(spa), |
2849 | spa_load_state(spa)); | |
9ae529ec | 2850 | |
6cb8e530 | 2851 | gethrestime(&spa->spa_loaded_ts); |
d2734cce | 2852 | error = spa_load_impl(spa, type, &ereport); |
428870ff | 2853 | |
0c66c32d JG |
2854 | /* |
2855 | * Don't count references from objsets that are already closed | |
2856 | * and are making their way through the eviction process. | |
2857 | */ | |
2858 | spa_evicting_os_wait(spa); | |
424fd7c3 | 2859 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
572e2857 BB |
2860 | if (error) { |
2861 | if (error != EEXIST) { | |
2862 | spa->spa_loaded_ts.tv_sec = 0; | |
2863 | spa->spa_loaded_ts.tv_nsec = 0; | |
2864 | } | |
2865 | if (error != EBADF) { | |
b5256303 | 2866 | zfs_ereport_post(ereport, spa, NULL, NULL, NULL, 0, 0); |
572e2857 BB |
2867 | } |
2868 | } | |
428870ff BB |
2869 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; |
2870 | spa->spa_ena = 0; | |
2871 | ||
ca95f70d OF |
2872 | (void) spa_import_progress_set_state(spa_guid(spa), |
2873 | spa_load_state(spa)); | |
2874 | ||
428870ff BB |
2875 | return (error); |
2876 | } | |
2877 | ||
33cf67cd | 2878 | #ifdef ZFS_DEBUG |
e0ab3ab5 JS |
2879 | /* |
2880 | * Count the number of per-vdev ZAPs associated with all of the vdevs in the | |
2881 | * vdev tree rooted in the given vd, and ensure that each ZAP is present in the | |
2882 | * spa's per-vdev ZAP list. | |
2883 | */ | |
2884 | static uint64_t | |
2885 | vdev_count_verify_zaps(vdev_t *vd) | |
2886 | { | |
2887 | spa_t *spa = vd->vdev_spa; | |
2888 | uint64_t total = 0; | |
e0ab3ab5 JS |
2889 | |
2890 | if (vd->vdev_top_zap != 0) { | |
2891 | total++; | |
2892 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2893 | spa->spa_all_vdev_zaps, vd->vdev_top_zap)); | |
2894 | } | |
2895 | if (vd->vdev_leaf_zap != 0) { | |
2896 | total++; | |
2897 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2898 | spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); | |
2899 | } | |
2900 | ||
1c27024e | 2901 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
2902 | total += vdev_count_verify_zaps(vd->vdev_child[i]); |
2903 | } | |
2904 | ||
2905 | return (total); | |
2906 | } | |
33cf67cd | 2907 | #endif |
e0ab3ab5 | 2908 | |
379ca9cf OF |
2909 | /* |
2910 | * Determine whether the activity check is required. | |
2911 | */ | |
2912 | static boolean_t | |
bbffb59e BB |
2913 | spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label, |
2914 | nvlist_t *config) | |
379ca9cf OF |
2915 | { |
2916 | uint64_t state = 0; | |
2917 | uint64_t hostid = 0; | |
2918 | uint64_t tryconfig_txg = 0; | |
2919 | uint64_t tryconfig_timestamp = 0; | |
060f0226 | 2920 | uint16_t tryconfig_mmp_seq = 0; |
379ca9cf OF |
2921 | nvlist_t *nvinfo; |
2922 | ||
2923 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
2924 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
2925 | (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, | |
2926 | &tryconfig_txg); | |
2927 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
2928 | &tryconfig_timestamp); | |
060f0226 OF |
2929 | (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ, |
2930 | &tryconfig_mmp_seq); | |
379ca9cf OF |
2931 | } |
2932 | ||
2933 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); | |
379ca9cf OF |
2934 | |
2935 | /* | |
2936 | * Disable the MMP activity check - This is used by zdb which | |
2937 | * is intended to be used on potentially active pools. | |
2938 | */ | |
2939 | if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) | |
2940 | return (B_FALSE); | |
2941 | ||
2942 | /* | |
2943 | * Skip the activity check when the MMP feature is disabled. | |
2944 | */ | |
2945 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) | |
2946 | return (B_FALSE); | |
ca95f70d | 2947 | |
379ca9cf | 2948 | /* |
060f0226 OF |
2949 | * If the tryconfig_ values are nonzero, they are the results of an |
2950 | * earlier tryimport. If they all match the uberblock we just found, | |
2951 | * then the pool has not changed and we return false so we do not test | |
2952 | * a second time. | |
379ca9cf OF |
2953 | */ |
2954 | if (tryconfig_txg && tryconfig_txg == ub->ub_txg && | |
060f0226 OF |
2955 | tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp && |
2956 | tryconfig_mmp_seq && tryconfig_mmp_seq == | |
2957 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) | |
379ca9cf OF |
2958 | return (B_FALSE); |
2959 | ||
2960 | /* | |
2961 | * Allow the activity check to be skipped when importing the pool | |
bbffb59e BB |
2962 | * on the same host which last imported it. Since the hostid from |
2963 | * configuration may be stale use the one read from the label. | |
379ca9cf | 2964 | */ |
bbffb59e BB |
2965 | if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) |
2966 | hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); | |
2967 | ||
379ca9cf OF |
2968 | if (hostid == spa_get_hostid()) |
2969 | return (B_FALSE); | |
2970 | ||
2971 | /* | |
2972 | * Skip the activity test when the pool was cleanly exported. | |
2973 | */ | |
2974 | if (state != POOL_STATE_ACTIVE) | |
2975 | return (B_FALSE); | |
2976 | ||
2977 | return (B_TRUE); | |
2978 | } | |
2979 | ||
060f0226 OF |
2980 | /* |
2981 | * Nanoseconds the activity check must watch for changes on-disk. | |
2982 | */ | |
2983 | static uint64_t | |
2984 | spa_activity_check_duration(spa_t *spa, uberblock_t *ub) | |
2985 | { | |
2986 | uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); | |
2987 | uint64_t multihost_interval = MSEC2NSEC( | |
2988 | MMP_INTERVAL_OK(zfs_multihost_interval)); | |
2989 | uint64_t import_delay = MAX(NANOSEC, import_intervals * | |
2990 | multihost_interval); | |
2991 | ||
2992 | /* | |
2993 | * Local tunables determine a minimum duration except for the case | |
2994 | * where we know when the remote host will suspend the pool if MMP | |
2995 | * writes do not land. | |
2996 | * | |
2997 | * See Big Theory comment at the top of mmp.c for the reasoning behind | |
2998 | * these cases and times. | |
2999 | */ | |
3000 | ||
3001 | ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100); | |
3002 | ||
3003 | if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3004 | MMP_FAIL_INT(ub) > 0) { | |
3005 | ||
3006 | /* MMP on remote host will suspend pool after failed writes */ | |
3007 | import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) * | |
3008 | MMP_IMPORT_SAFETY_FACTOR / 100; | |
3009 | ||
3010 | zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp " | |
3011 | "mmp_fails=%llu ub_mmp mmp_interval=%llu " | |
3012 | "import_intervals=%u", import_delay, MMP_FAIL_INT(ub), | |
3013 | MMP_INTERVAL(ub), import_intervals); | |
3014 | ||
3015 | } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3016 | MMP_FAIL_INT(ub) == 0) { | |
3017 | ||
3018 | /* MMP on remote host will never suspend pool */ | |
3019 | import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) + | |
3020 | ub->ub_mmp_delay) * import_intervals); | |
3021 | ||
3022 | zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp " | |
3023 | "mmp_interval=%llu ub_mmp_delay=%llu " | |
3024 | "import_intervals=%u", import_delay, MMP_INTERVAL(ub), | |
3025 | ub->ub_mmp_delay, import_intervals); | |
3026 | ||
3027 | } else if (MMP_VALID(ub)) { | |
3028 | /* | |
3029 | * zfs-0.7 compatability case | |
3030 | */ | |
3031 | ||
3032 | import_delay = MAX(import_delay, (multihost_interval + | |
3033 | ub->ub_mmp_delay) * import_intervals); | |
3034 | ||
3035 | zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu " | |
3036 | "import_intervals=%u leaves=%u", import_delay, | |
3037 | ub->ub_mmp_delay, import_intervals, | |
3038 | vdev_count_leaves(spa)); | |
3039 | } else { | |
3040 | /* Using local tunings is the only reasonable option */ | |
3041 | zfs_dbgmsg("pool last imported on non-MMP aware " | |
3042 | "host using import_delay=%llu multihost_interval=%llu " | |
3043 | "import_intervals=%u", import_delay, multihost_interval, | |
3044 | import_intervals); | |
3045 | } | |
3046 | ||
3047 | return (import_delay); | |
3048 | } | |
3049 | ||
379ca9cf OF |
3050 | /* |
3051 | * Perform the import activity check. If the user canceled the import or | |
3052 | * we detected activity then fail. | |
3053 | */ | |
3054 | static int | |
3055 | spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) | |
3056 | { | |
379ca9cf OF |
3057 | uint64_t txg = ub->ub_txg; |
3058 | uint64_t timestamp = ub->ub_timestamp; | |
060f0226 OF |
3059 | uint64_t mmp_config = ub->ub_mmp_config; |
3060 | uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0; | |
3061 | uint64_t import_delay; | |
379ca9cf OF |
3062 | hrtime_t import_expire; |
3063 | nvlist_t *mmp_label = NULL; | |
3064 | vdev_t *rvd = spa->spa_root_vdev; | |
3065 | kcondvar_t cv; | |
3066 | kmutex_t mtx; | |
3067 | int error = 0; | |
3068 | ||
3069 | cv_init(&cv, NULL, CV_DEFAULT, NULL); | |
3070 | mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); | |
3071 | mutex_enter(&mtx); | |
3072 | ||
3073 | /* | |
3074 | * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed | |
3075 | * during the earlier tryimport. If the txg recorded there is 0 then | |
3076 | * the pool is known to be active on another host. | |
3077 | * | |
060f0226 | 3078 | * Otherwise, the pool might be in use on another host. Check for |
379ca9cf OF |
3079 | * changes in the uberblocks on disk if necessary. |
3080 | */ | |
3081 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3082 | nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, | |
3083 | ZPOOL_CONFIG_LOAD_INFO); | |
3084 | ||
3085 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && | |
3086 | fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { | |
3087 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
3088 | error = SET_ERROR(EREMOTEIO); | |
3089 | goto out; | |
3090 | } | |
3091 | } | |
3092 | ||
060f0226 | 3093 | import_delay = spa_activity_check_duration(spa, ub); |
533ea041 | 3094 | |
379ca9cf | 3095 | /* Add a small random factor in case of simultaneous imports (0-25%) */ |
ca95f70d OF |
3096 | import_delay += import_delay * spa_get_random(250) / 1000; |
3097 | ||
3098 | import_expire = gethrtime() + import_delay; | |
379ca9cf OF |
3099 | |
3100 | while (gethrtime() < import_expire) { | |
ca95f70d OF |
3101 | (void) spa_import_progress_set_mmp_check(spa_guid(spa), |
3102 | NSEC2SEC(import_expire - gethrtime())); | |
3103 | ||
379ca9cf OF |
3104 | vdev_uberblock_load(rvd, ub, &mmp_label); |
3105 | ||
060f0226 OF |
3106 | if (txg != ub->ub_txg || timestamp != ub->ub_timestamp || |
3107 | mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) { | |
3108 | zfs_dbgmsg("multihost activity detected " | |
3109 | "txg %llu ub_txg %llu " | |
3110 | "timestamp %llu ub_timestamp %llu " | |
3111 | "mmp_config %#llx ub_mmp_config %#llx", | |
3112 | txg, ub->ub_txg, timestamp, ub->ub_timestamp, | |
3113 | mmp_config, ub->ub_mmp_config); | |
3114 | ||
379ca9cf OF |
3115 | error = SET_ERROR(EREMOTEIO); |
3116 | break; | |
3117 | } | |
3118 | ||
3119 | if (mmp_label) { | |
3120 | nvlist_free(mmp_label); | |
3121 | mmp_label = NULL; | |
3122 | } | |
3123 | ||
3124 | error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); | |
3125 | if (error != -1) { | |
3126 | error = SET_ERROR(EINTR); | |
3127 | break; | |
3128 | } | |
3129 | error = 0; | |
3130 | } | |
3131 | ||
3132 | out: | |
3133 | mutex_exit(&mtx); | |
3134 | mutex_destroy(&mtx); | |
3135 | cv_destroy(&cv); | |
3136 | ||
3137 | /* | |
3138 | * If the pool is determined to be active store the status in the | |
3139 | * spa->spa_load_info nvlist. If the remote hostname or hostid are | |
3140 | * available from configuration read from disk store them as well. | |
3141 | * This allows 'zpool import' to generate a more useful message. | |
3142 | * | |
3143 | * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) | |
3144 | * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool | |
3145 | * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool | |
3146 | */ | |
3147 | if (error == EREMOTEIO) { | |
3148 | char *hostname = "<unknown>"; | |
3149 | uint64_t hostid = 0; | |
3150 | ||
3151 | if (mmp_label) { | |
3152 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { | |
3153 | hostname = fnvlist_lookup_string(mmp_label, | |
3154 | ZPOOL_CONFIG_HOSTNAME); | |
3155 | fnvlist_add_string(spa->spa_load_info, | |
3156 | ZPOOL_CONFIG_MMP_HOSTNAME, hostname); | |
3157 | } | |
3158 | ||
3159 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { | |
3160 | hostid = fnvlist_lookup_uint64(mmp_label, | |
3161 | ZPOOL_CONFIG_HOSTID); | |
3162 | fnvlist_add_uint64(spa->spa_load_info, | |
3163 | ZPOOL_CONFIG_MMP_HOSTID, hostid); | |
3164 | } | |
3165 | } | |
3166 | ||
3167 | fnvlist_add_uint64(spa->spa_load_info, | |
3168 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); | |
3169 | fnvlist_add_uint64(spa->spa_load_info, | |
3170 | ZPOOL_CONFIG_MMP_TXG, 0); | |
3171 | ||
3172 | error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); | |
3173 | } | |
3174 | ||
3175 | if (mmp_label) | |
3176 | nvlist_free(mmp_label); | |
3177 | ||
3178 | return (error); | |
3179 | } | |
3180 | ||
9eb7b46e | 3181 | static int |
6cb8e530 PZ |
3182 | spa_verify_host(spa_t *spa, nvlist_t *mos_config) |
3183 | { | |
3184 | uint64_t hostid; | |
3185 | char *hostname; | |
3186 | uint64_t myhostid = 0; | |
3187 | ||
3188 | if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, | |
3189 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
3190 | hostname = fnvlist_lookup_string(mos_config, | |
3191 | ZPOOL_CONFIG_HOSTNAME); | |
3192 | ||
3193 | myhostid = zone_get_hostid(NULL); | |
3194 | ||
3195 | if (hostid != 0 && myhostid != 0 && hostid != myhostid) { | |
3196 | cmn_err(CE_WARN, "pool '%s' could not be " | |
3197 | "loaded as it was last accessed by " | |
3198 | "another system (host: %s hostid: 0x%llx). " | |
3199 | "See: http://illumos.org/msg/ZFS-8000-EY", | |
3200 | spa_name(spa), hostname, (u_longlong_t)hostid); | |
3201 | spa_load_failed(spa, "hostid verification failed: pool " | |
3202 | "last accessed by host: %s (hostid: 0x%llx)", | |
3203 | hostname, (u_longlong_t)hostid); | |
3204 | return (SET_ERROR(EBADF)); | |
3205 | } | |
3206 | } | |
3207 | ||
3208 | return (0); | |
3209 | } | |
3210 | ||
3211 | static int | |
3212 | spa_ld_parse_config(spa_t *spa, spa_import_type_t type) | |
428870ff BB |
3213 | { |
3214 | int error = 0; | |
6cb8e530 | 3215 | nvlist_t *nvtree, *nvl, *config = spa->spa_config; |
1c27024e | 3216 | int parse; |
9eb7b46e | 3217 | vdev_t *rvd; |
6cb8e530 PZ |
3218 | uint64_t pool_guid; |
3219 | char *comment; | |
3220 | ||
3221 | /* | |
3222 | * Versioning wasn't explicitly added to the label until later, so if | |
3223 | * it's not present treat it as the initial version. | |
3224 | */ | |
3225 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
3226 | &spa->spa_ubsync.ub_version) != 0) | |
3227 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
3228 | ||
3229 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { | |
3230 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3231 | ZPOOL_CONFIG_POOL_GUID); | |
3232 | return (SET_ERROR(EINVAL)); | |
3233 | } | |
3234 | ||
d2734cce SD |
3235 | /* |
3236 | * If we are doing an import, ensure that the pool is not already | |
3237 | * imported by checking if its pool guid already exists in the | |
3238 | * spa namespace. | |
3239 | * | |
3240 | * The only case that we allow an already imported pool to be | |
3241 | * imported again, is when the pool is checkpointed and we want to | |
3242 | * look at its checkpointed state from userland tools like zdb. | |
3243 | */ | |
3244 | #ifdef _KERNEL | |
3245 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3246 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3247 | spa_guid_exists(pool_guid, 0)) { | |
3248 | #else | |
3249 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3250 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3251 | spa_guid_exists(pool_guid, 0) && | |
3252 | !spa_importing_readonly_checkpoint(spa)) { | |
3253 | #endif | |
6cb8e530 PZ |
3254 | spa_load_failed(spa, "a pool with guid %llu is already open", |
3255 | (u_longlong_t)pool_guid); | |
3256 | return (SET_ERROR(EEXIST)); | |
3257 | } | |
3258 | ||
3259 | spa->spa_config_guid = pool_guid; | |
3260 | ||
3261 | nvlist_free(spa->spa_load_info); | |
3262 | spa->spa_load_info = fnvlist_alloc(); | |
3263 | ||
3264 | ASSERT(spa->spa_comment == NULL); | |
3265 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
3266 | spa->spa_comment = spa_strdup(comment); | |
3267 | ||
3268 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
3269 | &spa->spa_config_txg); | |
3270 | ||
3271 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) | |
3272 | spa->spa_config_splitting = fnvlist_dup(nvl); | |
428870ff | 3273 | |
4a0ee12a PZ |
3274 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { |
3275 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3276 | ZPOOL_CONFIG_VDEV_TREE); | |
2e528b49 | 3277 | return (SET_ERROR(EINVAL)); |
4a0ee12a | 3278 | } |
428870ff | 3279 | |
428870ff BB |
3280 | /* |
3281 | * Create "The Godfather" zio to hold all async IOs | |
3282 | */ | |
e022864d MA |
3283 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
3284 | KM_SLEEP); | |
1c27024e | 3285 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
3286 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
3287 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
3288 | ZIO_FLAG_GODFATHER); | |
3289 | } | |
428870ff BB |
3290 | |
3291 | /* | |
3292 | * Parse the configuration into a vdev tree. We explicitly set the | |
3293 | * value that will be returned by spa_version() since parsing the | |
3294 | * configuration requires knowing the version number. | |
3295 | */ | |
3296 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 PZ |
3297 | parse = (type == SPA_IMPORT_EXISTING ? |
3298 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
9eb7b46e | 3299 | error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); |
428870ff BB |
3300 | spa_config_exit(spa, SCL_ALL, FTAG); |
3301 | ||
4a0ee12a PZ |
3302 | if (error != 0) { |
3303 | spa_load_failed(spa, "unable to parse config [error=%d]", | |
3304 | error); | |
428870ff | 3305 | return (error); |
4a0ee12a | 3306 | } |
428870ff BB |
3307 | |
3308 | ASSERT(spa->spa_root_vdev == rvd); | |
c3520e7f MA |
3309 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); |
3310 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
428870ff BB |
3311 | |
3312 | if (type != SPA_IMPORT_ASSEMBLE) { | |
3313 | ASSERT(spa_guid(spa) == pool_guid); | |
3314 | } | |
3315 | ||
9eb7b46e PZ |
3316 | return (0); |
3317 | } | |
3318 | ||
6cb8e530 PZ |
3319 | /* |
3320 | * Recursively open all vdevs in the vdev tree. This function is called twice: | |
3321 | * first with the untrusted config, then with the trusted config. | |
3322 | */ | |
9eb7b46e PZ |
3323 | static int |
3324 | spa_ld_open_vdevs(spa_t *spa) | |
3325 | { | |
3326 | int error = 0; | |
3327 | ||
6cb8e530 PZ |
3328 | /* |
3329 | * spa_missing_tvds_allowed defines how many top-level vdevs can be | |
3330 | * missing/unopenable for the root vdev to be still considered openable. | |
3331 | */ | |
3332 | if (spa->spa_trust_config) { | |
3333 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; | |
3334 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { | |
3335 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; | |
3336 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { | |
3337 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; | |
3338 | } else { | |
3339 | spa->spa_missing_tvds_allowed = 0; | |
3340 | } | |
3341 | ||
3342 | spa->spa_missing_tvds_allowed = | |
3343 | MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); | |
3344 | ||
428870ff | 3345 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9eb7b46e | 3346 | error = vdev_open(spa->spa_root_vdev); |
428870ff | 3347 | spa_config_exit(spa, SCL_ALL, FTAG); |
6cb8e530 PZ |
3348 | |
3349 | if (spa->spa_missing_tvds != 0) { | |
3350 | spa_load_note(spa, "vdev tree has %lld missing top-level " | |
3351 | "vdevs.", (u_longlong_t)spa->spa_missing_tvds); | |
3352 | if (spa->spa_trust_config && (spa->spa_mode & FWRITE)) { | |
3353 | /* | |
3354 | * Although theoretically we could allow users to open | |
3355 | * incomplete pools in RW mode, we'd need to add a lot | |
3356 | * of extra logic (e.g. adjust pool space to account | |
3357 | * for missing vdevs). | |
3358 | * This limitation also prevents users from accidentally | |
3359 | * opening the pool in RW mode during data recovery and | |
3360 | * damaging it further. | |
3361 | */ | |
3362 | spa_load_note(spa, "pools with missing top-level " | |
3363 | "vdevs can only be opened in read-only mode."); | |
3364 | error = SET_ERROR(ENXIO); | |
3365 | } else { | |
3366 | spa_load_note(spa, "current settings allow for maximum " | |
3367 | "%lld missing top-level vdevs at this stage.", | |
3368 | (u_longlong_t)spa->spa_missing_tvds_allowed); | |
3369 | } | |
3370 | } | |
4a0ee12a PZ |
3371 | if (error != 0) { |
3372 | spa_load_failed(spa, "unable to open vdev tree [error=%d]", | |
3373 | error); | |
3374 | } | |
6cb8e530 PZ |
3375 | if (spa->spa_missing_tvds != 0 || error != 0) |
3376 | vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); | |
9eb7b46e PZ |
3377 | |
3378 | return (error); | |
3379 | } | |
3380 | ||
6cb8e530 PZ |
3381 | /* |
3382 | * We need to validate the vdev labels against the configuration that | |
3383 | * we have in hand. This function is called twice: first with an untrusted | |
3384 | * config, then with a trusted config. The validation is more strict when the | |
3385 | * config is trusted. | |
3386 | */ | |
9eb7b46e | 3387 | static int |
6cb8e530 | 3388 | spa_ld_validate_vdevs(spa_t *spa) |
9eb7b46e PZ |
3389 | { |
3390 | int error = 0; | |
3391 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 3392 | |
6cb8e530 PZ |
3393 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3394 | error = vdev_validate(rvd); | |
3395 | spa_config_exit(spa, SCL_ALL, FTAG); | |
428870ff | 3396 | |
6cb8e530 PZ |
3397 | if (error != 0) { |
3398 | spa_load_failed(spa, "vdev_validate failed [error=%d]", error); | |
3399 | return (error); | |
3400 | } | |
428870ff | 3401 | |
6cb8e530 PZ |
3402 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { |
3403 | spa_load_failed(spa, "cannot open vdev tree after invalidating " | |
3404 | "some vdevs"); | |
3405 | vdev_dbgmsg_print_tree(rvd, 2); | |
3406 | return (SET_ERROR(ENXIO)); | |
428870ff BB |
3407 | } |
3408 | ||
9eb7b46e PZ |
3409 | return (0); |
3410 | } | |
3411 | ||
d2734cce SD |
3412 | static void |
3413 | spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) | |
3414 | { | |
3415 | spa->spa_state = POOL_STATE_ACTIVE; | |
3416 | spa->spa_ubsync = spa->spa_uberblock; | |
3417 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
3418 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
3419 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
3420 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
3421 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
3422 | spa->spa_prev_software_version = ub->ub_software_version; | |
3423 | } | |
3424 | ||
9eb7b46e | 3425 | static int |
6cb8e530 | 3426 | spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) |
9eb7b46e PZ |
3427 | { |
3428 | vdev_t *rvd = spa->spa_root_vdev; | |
3429 | nvlist_t *label; | |
3430 | uberblock_t *ub = &spa->spa_uberblock; | |
9eb7b46e PZ |
3431 | boolean_t activity_check = B_FALSE; |
3432 | ||
d2734cce SD |
3433 | /* |
3434 | * If we are opening the checkpointed state of the pool by | |
3435 | * rewinding to it, at this point we will have written the | |
3436 | * checkpointed uberblock to the vdev labels, so searching | |
3437 | * the labels will find the right uberblock. However, if | |
3438 | * we are opening the checkpointed state read-only, we have | |
3439 | * not modified the labels. Therefore, we must ignore the | |
3440 | * labels and continue using the spa_uberblock that was set | |
3441 | * by spa_ld_checkpoint_rewind. | |
3442 | * | |
3443 | * Note that it would be fine to ignore the labels when | |
3444 | * rewinding (opening writeable) as well. However, if we | |
3445 | * crash just after writing the labels, we will end up | |
3446 | * searching the labels. Doing so in the common case means | |
3447 | * that this code path gets exercised normally, rather than | |
3448 | * just in the edge case. | |
3449 | */ | |
3450 | if (ub->ub_checkpoint_txg != 0 && | |
3451 | spa_importing_readonly_checkpoint(spa)) { | |
3452 | spa_ld_select_uberblock_done(spa, ub); | |
3453 | return (0); | |
3454 | } | |
3455 | ||
428870ff BB |
3456 | /* |
3457 | * Find the best uberblock. | |
3458 | */ | |
9ae529ec | 3459 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
3460 | |
3461 | /* | |
3462 | * If we weren't able to find a single valid uberblock, return failure. | |
3463 | */ | |
9ae529ec CS |
3464 | if (ub->ub_txg == 0) { |
3465 | nvlist_free(label); | |
4a0ee12a | 3466 | spa_load_failed(spa, "no valid uberblock found"); |
428870ff | 3467 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 3468 | } |
428870ff | 3469 | |
ca95f70d OF |
3470 | if (spa->spa_load_max_txg != UINT64_MAX) { |
3471 | (void) spa_import_progress_set_max_txg(spa_guid(spa), | |
3472 | (u_longlong_t)spa->spa_load_max_txg); | |
3473 | } | |
4a0ee12a PZ |
3474 | spa_load_note(spa, "using uberblock with txg=%llu", |
3475 | (u_longlong_t)ub->ub_txg); | |
3476 | ||
3477 | ||
379ca9cf OF |
3478 | /* |
3479 | * For pools which have the multihost property on determine if the | |
3480 | * pool is truly inactive and can be safely imported. Prevent | |
3481 | * hosts which don't have a hostid set from importing the pool. | |
3482 | */ | |
6cb8e530 PZ |
3483 | activity_check = spa_activity_check_required(spa, ub, label, |
3484 | spa->spa_config); | |
379ca9cf | 3485 | if (activity_check) { |
379ca9cf OF |
3486 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && |
3487 | spa_get_hostid() == 0) { | |
3488 | nvlist_free(label); | |
3489 | fnvlist_add_uint64(spa->spa_load_info, | |
3490 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3491 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3492 | } | |
3493 | ||
6cb8e530 | 3494 | int error = spa_activity_check(spa, ub, spa->spa_config); |
e889f0f5 OF |
3495 | if (error) { |
3496 | nvlist_free(label); | |
3497 | return (error); | |
3498 | } | |
3499 | ||
379ca9cf OF |
3500 | fnvlist_add_uint64(spa->spa_load_info, |
3501 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); | |
3502 | fnvlist_add_uint64(spa->spa_load_info, | |
3503 | ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); | |
060f0226 OF |
3504 | fnvlist_add_uint16(spa->spa_load_info, |
3505 | ZPOOL_CONFIG_MMP_SEQ, | |
3506 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)); | |
379ca9cf OF |
3507 | } |
3508 | ||
428870ff | 3509 | /* |
9ae529ec | 3510 | * If the pool has an unsupported version we can't open it. |
428870ff | 3511 | */ |
9ae529ec CS |
3512 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
3513 | nvlist_free(label); | |
4a0ee12a PZ |
3514 | spa_load_failed(spa, "version %llu is not supported", |
3515 | (u_longlong_t)ub->ub_version); | |
428870ff | 3516 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
3517 | } |
3518 | ||
3519 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3520 | nvlist_t *features; | |
3521 | ||
3522 | /* | |
3523 | * If we weren't able to find what's necessary for reading the | |
3524 | * MOS in the label, return failure. | |
3525 | */ | |
4a0ee12a PZ |
3526 | if (label == NULL) { |
3527 | spa_load_failed(spa, "label config unavailable"); | |
3528 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
3529 | ENXIO)); | |
3530 | } | |
3531 | ||
3532 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, | |
3533 | &features) != 0) { | |
9ae529ec | 3534 | nvlist_free(label); |
4a0ee12a PZ |
3535 | spa_load_failed(spa, "invalid label: '%s' missing", |
3536 | ZPOOL_CONFIG_FEATURES_FOR_READ); | |
9ae529ec CS |
3537 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3538 | ENXIO)); | |
3539 | } | |
3540 | ||
3541 | /* | |
3542 | * Update our in-core representation with the definitive values | |
3543 | * from the label. | |
3544 | */ | |
3545 | nvlist_free(spa->spa_label_features); | |
3546 | VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); | |
3547 | } | |
3548 | ||
3549 | nvlist_free(label); | |
3550 | ||
3551 | /* | |
3552 | * Look through entries in the label nvlist's features_for_read. If | |
3553 | * there is a feature listed there which we don't understand then we | |
3554 | * cannot open a pool. | |
3555 | */ | |
3556 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3557 | nvlist_t *unsup_feat; | |
9ae529ec CS |
3558 | |
3559 | VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == | |
3560 | 0); | |
3561 | ||
1c27024e DB |
3562 | for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, |
3563 | NULL); nvp != NULL; | |
9ae529ec CS |
3564 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { |
3565 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
3566 | VERIFY(nvlist_add_string(unsup_feat, | |
3567 | nvpair_name(nvp), "") == 0); | |
3568 | } | |
3569 | } | |
3570 | ||
3571 | if (!nvlist_empty(unsup_feat)) { | |
3572 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
3573 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); | |
3574 | nvlist_free(unsup_feat); | |
4a0ee12a | 3575 | spa_load_failed(spa, "some features are unsupported"); |
9ae529ec CS |
3576 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3577 | ENOTSUP)); | |
3578 | } | |
3579 | ||
3580 | nvlist_free(unsup_feat); | |
3581 | } | |
428870ff | 3582 | |
428870ff BB |
3583 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { |
3584 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 | 3585 | spa_try_repair(spa, spa->spa_config); |
428870ff BB |
3586 | spa_config_exit(spa, SCL_ALL, FTAG); |
3587 | nvlist_free(spa->spa_config_splitting); | |
3588 | spa->spa_config_splitting = NULL; | |
3589 | } | |
3590 | ||
3591 | /* | |
3592 | * Initialize internal SPA structures. | |
3593 | */ | |
d2734cce | 3594 | spa_ld_select_uberblock_done(spa, ub); |
428870ff | 3595 | |
9eb7b46e PZ |
3596 | return (0); |
3597 | } | |
3598 | ||
3599 | static int | |
3600 | spa_ld_open_rootbp(spa_t *spa) | |
3601 | { | |
3602 | int error = 0; | |
3603 | vdev_t *rvd = spa->spa_root_vdev; | |
a1d477c2 | 3604 | |
9ae529ec | 3605 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
4a0ee12a PZ |
3606 | if (error != 0) { |
3607 | spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " | |
3608 | "[error=%d]", error); | |
428870ff | 3609 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3610 | } |
428870ff BB |
3611 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; |
3612 | ||
9eb7b46e PZ |
3613 | return (0); |
3614 | } | |
3615 | ||
3616 | static int | |
d2734cce | 3617 | spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, |
6cb8e530 | 3618 | boolean_t reloading) |
9eb7b46e | 3619 | { |
6cb8e530 PZ |
3620 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
3621 | nvlist_t *nv, *mos_config, *policy; | |
3622 | int error = 0, copy_error; | |
3623 | uint64_t healthy_tvds, healthy_tvds_mos; | |
3624 | uint64_t mos_config_txg; | |
9eb7b46e | 3625 | |
4a0ee12a PZ |
3626 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) |
3627 | != 0) | |
428870ff BB |
3628 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3629 | ||
a1d477c2 | 3630 | /* |
6cb8e530 PZ |
3631 | * If we're assembling a pool from a split, the config provided is |
3632 | * already trusted so there is nothing to do. | |
a1d477c2 | 3633 | */ |
6cb8e530 PZ |
3634 | if (type == SPA_IMPORT_ASSEMBLE) |
3635 | return (0); | |
3636 | ||
3637 | healthy_tvds = spa_healthy_core_tvds(spa); | |
a1d477c2 | 3638 | |
6cb8e530 PZ |
3639 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) |
3640 | != 0) { | |
3641 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
3642 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3643 | } | |
3644 | ||
3645 | /* | |
3646 | * If we are doing an open, pool owner wasn't verified yet, thus do | |
3647 | * the verification here. | |
3648 | */ | |
3649 | if (spa->spa_load_state == SPA_LOAD_OPEN) { | |
3650 | error = spa_verify_host(spa, mos_config); | |
3651 | if (error != 0) { | |
a1d477c2 | 3652 | nvlist_free(mos_config); |
6cb8e530 | 3653 | return (error); |
a1d477c2 | 3654 | } |
6cb8e530 PZ |
3655 | } |
3656 | ||
3657 | nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); | |
a1d477c2 | 3658 | |
6cb8e530 PZ |
3659 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3660 | ||
3661 | /* | |
3662 | * Build a new vdev tree from the trusted config | |
3663 | */ | |
3664 | VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); | |
3665 | ||
3666 | /* | |
3667 | * Vdev paths in the MOS may be obsolete. If the untrusted config was | |
3668 | * obtained by scanning /dev/dsk, then it will have the right vdev | |
3669 | * paths. We update the trusted MOS config with this information. | |
3670 | * We first try to copy the paths with vdev_copy_path_strict, which | |
3671 | * succeeds only when both configs have exactly the same vdev tree. | |
3672 | * If that fails, we fall back to a more flexible method that has a | |
3673 | * best effort policy. | |
3674 | */ | |
3675 | copy_error = vdev_copy_path_strict(rvd, mrvd); | |
3676 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3677 | spa_load_note(spa, "provided vdev tree:"); | |
3678 | vdev_dbgmsg_print_tree(rvd, 2); | |
3679 | spa_load_note(spa, "MOS vdev tree:"); | |
3680 | vdev_dbgmsg_print_tree(mrvd, 2); | |
3681 | } | |
3682 | if (copy_error != 0) { | |
3683 | spa_load_note(spa, "vdev_copy_path_strict failed, falling " | |
3684 | "back to vdev_copy_path_relaxed"); | |
3685 | vdev_copy_path_relaxed(rvd, mrvd); | |
3686 | } | |
3687 | ||
3688 | vdev_close(rvd); | |
3689 | vdev_free(rvd); | |
3690 | spa->spa_root_vdev = mrvd; | |
3691 | rvd = mrvd; | |
3692 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3693 | ||
3694 | /* | |
3695 | * We will use spa_config if we decide to reload the spa or if spa_load | |
3696 | * fails and we rewind. We must thus regenerate the config using the | |
8a393be3 PZ |
3697 | * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to |
3698 | * pass settings on how to load the pool and is not stored in the MOS. | |
3699 | * We copy it over to our new, trusted config. | |
6cb8e530 PZ |
3700 | */ |
3701 | mos_config_txg = fnvlist_lookup_uint64(mos_config, | |
3702 | ZPOOL_CONFIG_POOL_TXG); | |
3703 | nvlist_free(mos_config); | |
3704 | mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); | |
8a393be3 | 3705 | if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, |
6cb8e530 | 3706 | &policy) == 0) |
8a393be3 | 3707 | fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); |
6cb8e530 PZ |
3708 | spa_config_set(spa, mos_config); |
3709 | spa->spa_config_source = SPA_CONFIG_SRC_MOS; | |
3710 | ||
3711 | /* | |
3712 | * Now that we got the config from the MOS, we should be more strict | |
3713 | * in checking blkptrs and can make assumptions about the consistency | |
3714 | * of the vdev tree. spa_trust_config must be set to true before opening | |
3715 | * vdevs in order for them to be writeable. | |
3716 | */ | |
3717 | spa->spa_trust_config = B_TRUE; | |
3718 | ||
3719 | /* | |
3720 | * Open and validate the new vdev tree | |
3721 | */ | |
3722 | error = spa_ld_open_vdevs(spa); | |
3723 | if (error != 0) | |
3724 | return (error); | |
3725 | ||
3726 | error = spa_ld_validate_vdevs(spa); | |
3727 | if (error != 0) | |
3728 | return (error); | |
3729 | ||
3730 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3731 | spa_load_note(spa, "final vdev tree:"); | |
3732 | vdev_dbgmsg_print_tree(rvd, 2); | |
3733 | } | |
3734 | ||
3735 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && | |
3736 | !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { | |
a1d477c2 | 3737 | /* |
6cb8e530 PZ |
3738 | * Sanity check to make sure that we are indeed loading the |
3739 | * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds | |
3740 | * in the config provided and they happened to be the only ones | |
3741 | * to have the latest uberblock, we could involuntarily perform | |
3742 | * an extreme rewind. | |
a1d477c2 | 3743 | */ |
6cb8e530 PZ |
3744 | healthy_tvds_mos = spa_healthy_core_tvds(spa); |
3745 | if (healthy_tvds_mos - healthy_tvds >= | |
3746 | SPA_SYNC_MIN_VDEVS) { | |
3747 | spa_load_note(spa, "config provided misses too many " | |
3748 | "top-level vdevs compared to MOS (%lld vs %lld). ", | |
3749 | (u_longlong_t)healthy_tvds, | |
3750 | (u_longlong_t)healthy_tvds_mos); | |
3751 | spa_load_note(spa, "vdev tree:"); | |
3752 | vdev_dbgmsg_print_tree(rvd, 2); | |
3753 | if (reloading) { | |
3754 | spa_load_failed(spa, "config was already " | |
3755 | "provided from MOS. Aborting."); | |
3756 | return (spa_vdev_err(rvd, | |
3757 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3758 | } | |
3759 | spa_load_note(spa, "spa must be reloaded using MOS " | |
3760 | "config"); | |
3761 | return (SET_ERROR(EAGAIN)); | |
4a0ee12a | 3762 | } |
a1d477c2 MA |
3763 | } |
3764 | ||
6cb8e530 PZ |
3765 | error = spa_check_for_missing_logs(spa); |
3766 | if (error != 0) | |
3767 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
3768 | ||
3769 | if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { | |
3770 | spa_load_failed(spa, "uberblock guid sum doesn't match MOS " | |
3771 | "guid sum (%llu != %llu)", | |
3772 | (u_longlong_t)spa->spa_uberblock.ub_guid_sum, | |
3773 | (u_longlong_t)rvd->vdev_guid_sum); | |
3774 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
3775 | ENXIO)); | |
3776 | } | |
3777 | ||
9eb7b46e PZ |
3778 | return (0); |
3779 | } | |
3780 | ||
3781 | static int | |
3782 | spa_ld_open_indirect_vdev_metadata(spa_t *spa) | |
3783 | { | |
3784 | int error = 0; | |
3785 | vdev_t *rvd = spa->spa_root_vdev; | |
3786 | ||
a1d477c2 MA |
3787 | /* |
3788 | * Everything that we read before spa_remove_init() must be stored | |
3789 | * on concreted vdevs. Therefore we do this as early as possible. | |
3790 | */ | |
4a0ee12a PZ |
3791 | error = spa_remove_init(spa); |
3792 | if (error != 0) { | |
3793 | spa_load_failed(spa, "spa_remove_init failed [error=%d]", | |
3794 | error); | |
a1d477c2 | 3795 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3796 | } |
a1d477c2 | 3797 | |
9eb7b46e PZ |
3798 | /* |
3799 | * Retrieve information needed to condense indirect vdev mappings. | |
3800 | */ | |
3801 | error = spa_condense_init(spa); | |
3802 | if (error != 0) { | |
4a0ee12a PZ |
3803 | spa_load_failed(spa, "spa_condense_init failed [error=%d]", |
3804 | error); | |
9eb7b46e PZ |
3805 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3806 | } | |
3807 | ||
3808 | return (0); | |
3809 | } | |
3810 | ||
3811 | static int | |
4a0ee12a | 3812 | spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) |
9eb7b46e PZ |
3813 | { |
3814 | int error = 0; | |
3815 | vdev_t *rvd = spa->spa_root_vdev; | |
3816 | ||
9ae529ec CS |
3817 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
3818 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 3819 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
3820 | |
3821 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
4a0ee12a | 3822 | &spa->spa_feat_for_read_obj, B_TRUE) != 0) { |
9ae529ec CS |
3823 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3824 | } | |
3825 | ||
3826 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
4a0ee12a | 3827 | &spa->spa_feat_for_write_obj, B_TRUE) != 0) { |
9ae529ec CS |
3828 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3829 | } | |
3830 | ||
3831 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
4a0ee12a | 3832 | &spa->spa_feat_desc_obj, B_TRUE) != 0) { |
9ae529ec CS |
3833 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3834 | } | |
3835 | ||
b9b24bb4 CS |
3836 | enabled_feat = fnvlist_alloc(); |
3837 | unsup_feat = fnvlist_alloc(); | |
9ae529ec | 3838 | |
fa86b5db | 3839 | if (!spa_features_check(spa, B_FALSE, |
b9b24bb4 | 3840 | unsup_feat, enabled_feat)) |
9ae529ec CS |
3841 | missing_feat_read = B_TRUE; |
3842 | ||
4a0ee12a PZ |
3843 | if (spa_writeable(spa) || |
3844 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) { | |
fa86b5db | 3845 | if (!spa_features_check(spa, B_TRUE, |
b9b24bb4 | 3846 | unsup_feat, enabled_feat)) { |
9eb7b46e | 3847 | *missing_feat_writep = B_TRUE; |
b9b24bb4 | 3848 | } |
9ae529ec CS |
3849 | } |
3850 | ||
b9b24bb4 CS |
3851 | fnvlist_add_nvlist(spa->spa_load_info, |
3852 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
3853 | ||
9ae529ec | 3854 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
3855 | fnvlist_add_nvlist(spa->spa_load_info, |
3856 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
3857 | } |
3858 | ||
b9b24bb4 CS |
3859 | fnvlist_free(enabled_feat); |
3860 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
3861 | |
3862 | if (!missing_feat_read) { | |
3863 | fnvlist_add_boolean(spa->spa_load_info, | |
3864 | ZPOOL_CONFIG_CAN_RDONLY); | |
3865 | } | |
3866 | ||
3867 | /* | |
3868 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
3869 | * twofold: to determine whether the pool is available for | |
3870 | * import in read-write mode and (if it is not) whether the | |
3871 | * pool is available for import in read-only mode. If the pool | |
3872 | * is available for import in read-write mode, it is displayed | |
3873 | * as available in userland; if it is not available for import | |
3874 | * in read-only mode, it is displayed as unavailable in | |
3875 | * userland. If the pool is available for import in read-only | |
3876 | * mode but not read-write mode, it is displayed as unavailable | |
3877 | * in userland with a special note that the pool is actually | |
3878 | * available for open in read-only mode. | |
3879 | * | |
3880 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
3881 | * missing a feature for write, we must first determine whether | |
3882 | * the pool can be opened read-only before returning to | |
3883 | * userland in order to know whether to display the | |
3884 | * abovementioned note. | |
3885 | */ | |
9eb7b46e | 3886 | if (missing_feat_read || (*missing_feat_writep && |
9ae529ec | 3887 | spa_writeable(spa))) { |
4a0ee12a | 3888 | spa_load_failed(spa, "pool uses unsupported features"); |
9ae529ec CS |
3889 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3890 | ENOTSUP)); | |
3891 | } | |
b0bc7a84 MG |
3892 | |
3893 | /* | |
3894 | * Load refcounts for ZFS features from disk into an in-memory | |
3895 | * cache during SPA initialization. | |
3896 | */ | |
1c27024e | 3897 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { |
b0bc7a84 MG |
3898 | uint64_t refcount; |
3899 | ||
3900 | error = feature_get_refcount_from_disk(spa, | |
3901 | &spa_feature_table[i], &refcount); | |
3902 | if (error == 0) { | |
3903 | spa->spa_feat_refcount_cache[i] = refcount; | |
3904 | } else if (error == ENOTSUP) { | |
3905 | spa->spa_feat_refcount_cache[i] = | |
3906 | SPA_FEATURE_DISABLED; | |
3907 | } else { | |
4a0ee12a PZ |
3908 | spa_load_failed(spa, "error getting refcount " |
3909 | "for feature %s [error=%d]", | |
3910 | spa_feature_table[i].fi_guid, error); | |
b0bc7a84 MG |
3911 | return (spa_vdev_err(rvd, |
3912 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3913 | } | |
3914 | } | |
3915 | } | |
3916 | ||
3917 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
3918 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
4a0ee12a | 3919 | &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) |
b0bc7a84 | 3920 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
9ae529ec CS |
3921 | } |
3922 | ||
f00ab3f2 TC |
3923 | /* |
3924 | * Encryption was added before bookmark_v2, even though bookmark_v2 | |
3925 | * is now a dependency. If this pool has encryption enabled without | |
3926 | * bookmark_v2, trigger an errata message. | |
3927 | */ | |
3928 | if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && | |
3929 | !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) { | |
3930 | spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; | |
3931 | } | |
3932 | ||
9eb7b46e PZ |
3933 | return (0); |
3934 | } | |
3935 | ||
3936 | static int | |
3937 | spa_ld_load_special_directories(spa_t *spa) | |
3938 | { | |
3939 | int error = 0; | |
3940 | vdev_t *rvd = spa->spa_root_vdev; | |
3941 | ||
9ae529ec CS |
3942 | spa->spa_is_initializing = B_TRUE; |
3943 | error = dsl_pool_open(spa->spa_dsl_pool); | |
3944 | spa->spa_is_initializing = B_FALSE; | |
4a0ee12a PZ |
3945 | if (error != 0) { |
3946 | spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); | |
9ae529ec | 3947 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3948 | } |
9ae529ec | 3949 | |
9eb7b46e PZ |
3950 | return (0); |
3951 | } | |
428870ff | 3952 | |
9eb7b46e PZ |
3953 | static int |
3954 | spa_ld_get_props(spa_t *spa) | |
3955 | { | |
3956 | int error = 0; | |
3957 | uint64_t obj; | |
3958 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 3959 | |
3c67d83a TH |
3960 | /* Grab the checksum salt from the MOS. */ |
3961 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3962 | DMU_POOL_CHECKSUM_SALT, 1, | |
3963 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
3964 | spa->spa_cksum_salt.zcs_bytes); | |
3965 | if (error == ENOENT) { | |
3966 | /* Generate a new salt for subsequent use */ | |
3967 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
3968 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
3969 | } else if (error != 0) { | |
4a0ee12a PZ |
3970 | spa_load_failed(spa, "unable to retrieve checksum salt from " |
3971 | "MOS [error=%d]", error); | |
3c67d83a TH |
3972 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3973 | } | |
3974 | ||
4a0ee12a | 3975 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) |
428870ff BB |
3976 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3977 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
4a0ee12a PZ |
3978 | if (error != 0) { |
3979 | spa_load_failed(spa, "error opening deferred-frees bpobj " | |
3980 | "[error=%d]", error); | |
428870ff | 3981 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3982 | } |
34dc7c2f BB |
3983 | |
3984 | /* | |
3985 | * Load the bit that tells us to use the new accounting function | |
3986 | * (raid-z deflation). If we have an older pool, this will not | |
3987 | * be present. | |
3988 | */ | |
4a0ee12a | 3989 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); |
428870ff BB |
3990 | if (error != 0 && error != ENOENT) |
3991 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3992 | ||
3993 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
4a0ee12a | 3994 | &spa->spa_creation_version, B_FALSE); |
428870ff BB |
3995 | if (error != 0 && error != ENOENT) |
3996 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
3997 | |
3998 | /* | |
3999 | * Load the persistent error log. If we have an older pool, this will | |
4000 | * not be present. | |
4001 | */ | |
4a0ee12a PZ |
4002 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, |
4003 | B_FALSE); | |
428870ff BB |
4004 | if (error != 0 && error != ENOENT) |
4005 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4006 | |
428870ff | 4007 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
4a0ee12a | 4008 | &spa->spa_errlog_scrub, B_FALSE); |
428870ff BB |
4009 | if (error != 0 && error != ENOENT) |
4010 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4011 | |
37f03da8 SH |
4012 | /* |
4013 | * Load the livelist deletion field. If a livelist is queued for | |
4014 | * deletion, indicate that in the spa | |
4015 | */ | |
4016 | error = spa_dir_prop(spa, DMU_POOL_DELETED_CLONES, | |
4017 | &spa->spa_livelists_to_delete, B_FALSE); | |
4018 | if (error != 0 && error != ENOENT) | |
4019 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4020 | ||
34dc7c2f BB |
4021 | /* |
4022 | * Load the history object. If we have an older pool, this | |
4023 | * will not be present. | |
4024 | */ | |
4a0ee12a | 4025 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); |
428870ff BB |
4026 | if (error != 0 && error != ENOENT) |
4027 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4028 | ||
e0ab3ab5 JS |
4029 | /* |
4030 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
4031 | * be present; in this case, defer its creation to a later time to | |
4032 | * avoid dirtying the MOS this early / out of sync context. See | |
4033 | * spa_sync_config_object. | |
4034 | */ | |
4035 | ||
4036 | /* The sentinel is only available in the MOS config. */ | |
1c27024e | 4037 | nvlist_t *mos_config; |
4a0ee12a PZ |
4038 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { |
4039 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
e0ab3ab5 | 4040 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4041 | } |
e0ab3ab5 JS |
4042 | |
4043 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
4a0ee12a | 4044 | &spa->spa_all_vdev_zaps, B_FALSE); |
e0ab3ab5 | 4045 | |
38640550 DB |
4046 | if (error == ENOENT) { |
4047 | VERIFY(!nvlist_exists(mos_config, | |
4048 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
4049 | spa->spa_avz_action = AVZ_ACTION_INITIALIZE; | |
4050 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4051 | } else if (error != 0) { | |
e0ab3ab5 | 4052 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
38640550 | 4053 | } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { |
e0ab3ab5 JS |
4054 | /* |
4055 | * An older version of ZFS overwrote the sentinel value, so | |
4056 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
4057 | * destruction to later; see spa_sync_config_object. | |
4058 | */ | |
4059 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
4060 | /* | |
4061 | * We're assuming that no vdevs have had their ZAPs created | |
4062 | * before this. Better be sure of it. | |
4063 | */ | |
4064 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4065 | } | |
4066 | nvlist_free(mos_config); | |
4067 | ||
9eb7b46e PZ |
4068 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
4069 | ||
4a0ee12a PZ |
4070 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, |
4071 | B_FALSE); | |
9eb7b46e PZ |
4072 | if (error && error != ENOENT) |
4073 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4074 | ||
4075 | if (error == 0) { | |
4076 | uint64_t autoreplace; | |
4077 | ||
4078 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
4079 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
4080 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
4081 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
4082 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
c02c1bec | 4083 | spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); |
1b939560 | 4084 | spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim); |
9eb7b46e PZ |
4085 | spa->spa_autoreplace = (autoreplace != 0); |
4086 | } | |
4087 | ||
6cb8e530 PZ |
4088 | /* |
4089 | * If we are importing a pool with missing top-level vdevs, | |
4090 | * we enforce that the pool doesn't panic or get suspended on | |
4091 | * error since the likelihood of missing data is extremely high. | |
4092 | */ | |
4093 | if (spa->spa_missing_tvds > 0 && | |
4094 | spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && | |
4095 | spa->spa_load_state != SPA_LOAD_TRYIMPORT) { | |
4096 | spa_load_note(spa, "forcing failmode to 'continue' " | |
4097 | "as some top level vdevs are missing"); | |
4098 | spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; | |
4099 | } | |
4100 | ||
9eb7b46e PZ |
4101 | return (0); |
4102 | } | |
4103 | ||
4104 | static int | |
4105 | spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) | |
4106 | { | |
4107 | int error = 0; | |
4108 | vdev_t *rvd = spa->spa_root_vdev; | |
4109 | ||
428870ff BB |
4110 | /* |
4111 | * If we're assembling the pool from the split-off vdevs of | |
4112 | * an existing pool, we don't want to attach the spares & cache | |
4113 | * devices. | |
4114 | */ | |
34dc7c2f BB |
4115 | |
4116 | /* | |
4117 | * Load any hot spares for this pool. | |
4118 | */ | |
4a0ee12a PZ |
4119 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, |
4120 | B_FALSE); | |
428870ff BB |
4121 | if (error != 0 && error != ENOENT) |
4122 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4123 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4124 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
4125 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
4a0ee12a PZ |
4126 | &spa->spa_spares.sav_config) != 0) { |
4127 | spa_load_failed(spa, "error loading spares nvlist"); | |
428870ff | 4128 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4129 | } |
34dc7c2f | 4130 | |
b128c09f | 4131 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4132 | spa_load_spares(spa); |
b128c09f | 4133 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4134 | } else if (error == 0) { |
4135 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
4136 | } |
4137 | ||
4138 | /* | |
4139 | * Load any level 2 ARC devices for this pool. | |
4140 | */ | |
428870ff | 4141 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
4a0ee12a | 4142 | &spa->spa_l2cache.sav_object, B_FALSE); |
428870ff BB |
4143 | if (error != 0 && error != ENOENT) |
4144 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4145 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4146 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
4147 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
4a0ee12a PZ |
4148 | &spa->spa_l2cache.sav_config) != 0) { |
4149 | spa_load_failed(spa, "error loading l2cache nvlist"); | |
428870ff | 4150 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4151 | } |
34dc7c2f | 4152 | |
b128c09f | 4153 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4154 | spa_load_l2cache(spa); |
b128c09f | 4155 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4156 | } else if (error == 0) { |
4157 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
4158 | } |
4159 | ||
9eb7b46e PZ |
4160 | return (0); |
4161 | } | |
428870ff | 4162 | |
9eb7b46e | 4163 | static int |
4a0ee12a | 4164 | spa_ld_load_vdev_metadata(spa_t *spa) |
9eb7b46e PZ |
4165 | { |
4166 | int error = 0; | |
4167 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4168 | |
379ca9cf OF |
4169 | /* |
4170 | * If the 'multihost' property is set, then never allow a pool to | |
4171 | * be imported when the system hostid is zero. The exception to | |
4172 | * this rule is zdb which is always allowed to access pools. | |
4173 | */ | |
4174 | if (spa_multihost(spa) && spa_get_hostid() == 0 && | |
4175 | (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { | |
4176 | fnvlist_add_uint64(spa->spa_load_info, | |
4177 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
4178 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
4179 | } | |
4180 | ||
34dc7c2f BB |
4181 | /* |
4182 | * If the 'autoreplace' property is set, then post a resource notifying | |
4183 | * the ZFS DE that it should not issue any faults for unopenable | |
4184 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
4185 | * unopenable vdevs so that the normal autoreplace handler can take | |
4186 | * over. | |
4187 | */ | |
4a0ee12a | 4188 | if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 4189 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
4190 | /* |
4191 | * For the import case, this is done in spa_import(), because | |
4192 | * at this point we're using the spare definitions from | |
4193 | * the MOS config, not necessarily from the userland config. | |
4194 | */ | |
4a0ee12a | 4195 | if (spa->spa_load_state != SPA_LOAD_IMPORT) { |
428870ff BB |
4196 | spa_aux_check_removed(&spa->spa_spares); |
4197 | spa_aux_check_removed(&spa->spa_l2cache); | |
4198 | } | |
4199 | } | |
34dc7c2f BB |
4200 | |
4201 | /* | |
9eb7b46e | 4202 | * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. |
34dc7c2f | 4203 | */ |
a1d477c2 MA |
4204 | error = vdev_load(rvd); |
4205 | if (error != 0) { | |
4a0ee12a | 4206 | spa_load_failed(spa, "vdev_load failed [error=%d]", error); |
a1d477c2 MA |
4207 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
4208 | } | |
4209 | ||
93e28d66 SD |
4210 | error = spa_ld_log_spacemaps(spa); |
4211 | if (error != 0) { | |
4212 | spa_load_failed(spa, "spa_ld_log_sm_data failed [error=%d]", | |
4213 | error); | |
4214 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
4215 | } | |
4216 | ||
34dc7c2f | 4217 | /* |
9eb7b46e | 4218 | * Propagate the leaf DTLs we just loaded all the way up the vdev tree. |
34dc7c2f | 4219 | */ |
b128c09f | 4220 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4221 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE); |
b128c09f | 4222 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 4223 | |
9eb7b46e PZ |
4224 | return (0); |
4225 | } | |
4226 | ||
4227 | static int | |
4228 | spa_ld_load_dedup_tables(spa_t *spa) | |
4229 | { | |
4230 | int error = 0; | |
4231 | vdev_t *rvd = spa->spa_root_vdev; | |
4232 | ||
428870ff | 4233 | error = ddt_load(spa); |
4a0ee12a PZ |
4234 | if (error != 0) { |
4235 | spa_load_failed(spa, "ddt_load failed [error=%d]", error); | |
428870ff | 4236 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4237 | } |
428870ff | 4238 | |
9eb7b46e PZ |
4239 | return (0); |
4240 | } | |
4241 | ||
4242 | static int | |
4243 | spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) | |
4244 | { | |
4245 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 4246 | |
4a0ee12a PZ |
4247 | if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { |
4248 | boolean_t missing = spa_check_logs(spa); | |
4249 | if (missing) { | |
6cb8e530 PZ |
4250 | if (spa->spa_missing_tvds != 0) { |
4251 | spa_load_note(spa, "spa_check_logs failed " | |
4252 | "so dropping the logs"); | |
4253 | } else { | |
4254 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
4255 | spa_load_failed(spa, "spa_check_logs failed"); | |
4256 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, | |
4257 | ENXIO)); | |
4258 | } | |
4a0ee12a | 4259 | } |
428870ff BB |
4260 | } |
4261 | ||
9eb7b46e PZ |
4262 | return (0); |
4263 | } | |
4264 | ||
4265 | static int | |
4a0ee12a | 4266 | spa_ld_verify_pool_data(spa_t *spa) |
9eb7b46e PZ |
4267 | { |
4268 | int error = 0; | |
4269 | vdev_t *rvd = spa->spa_root_vdev; | |
4270 | ||
4271 | /* | |
4272 | * We've successfully opened the pool, verify that we're ready | |
4273 | * to start pushing transactions. | |
4274 | */ | |
4a0ee12a | 4275 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
9eb7b46e PZ |
4276 | error = spa_load_verify(spa); |
4277 | if (error != 0) { | |
4a0ee12a PZ |
4278 | spa_load_failed(spa, "spa_load_verify failed " |
4279 | "[error=%d]", error); | |
9eb7b46e PZ |
4280 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
4281 | error)); | |
4282 | } | |
4283 | } | |
4284 | ||
4285 | return (0); | |
4286 | } | |
4287 | ||
4288 | static void | |
4289 | spa_ld_claim_log_blocks(spa_t *spa) | |
4290 | { | |
4291 | dmu_tx_t *tx; | |
4292 | dsl_pool_t *dp = spa_get_dsl(spa); | |
4293 | ||
4294 | /* | |
4295 | * Claim log blocks that haven't been committed yet. | |
4296 | * This must all happen in a single txg. | |
4297 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
4298 | * invoked from zil_claim_log_block()'s i/o done callback. | |
4299 | * Price of rollback is that we abandon the log. | |
4300 | */ | |
4301 | spa->spa_claiming = B_TRUE; | |
4302 | ||
4303 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
4304 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
4305 | zil_claim, tx, DS_FIND_CHILDREN); | |
4306 | dmu_tx_commit(tx); | |
4307 | ||
4308 | spa->spa_claiming = B_FALSE; | |
4309 | ||
4310 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
4311 | } | |
4312 | ||
4313 | static void | |
6cb8e530 | 4314 | spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, |
d2734cce | 4315 | boolean_t update_config_cache) |
9eb7b46e PZ |
4316 | { |
4317 | vdev_t *rvd = spa->spa_root_vdev; | |
4318 | int need_update = B_FALSE; | |
4319 | ||
4320 | /* | |
4321 | * If the config cache is stale, or we have uninitialized | |
4322 | * metaslabs (see spa_vdev_add()), then update the config. | |
4323 | * | |
4324 | * If this is a verbatim import, trust the current | |
4325 | * in-core spa_config and update the disk labels. | |
4326 | */ | |
d2734cce | 4327 | if (update_config_cache || config_cache_txg != spa->spa_config_txg || |
4a0ee12a PZ |
4328 | spa->spa_load_state == SPA_LOAD_IMPORT || |
4329 | spa->spa_load_state == SPA_LOAD_RECOVER || | |
9eb7b46e PZ |
4330 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) |
4331 | need_update = B_TRUE; | |
4332 | ||
4333 | for (int c = 0; c < rvd->vdev_children; c++) | |
4334 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
4335 | need_update = B_TRUE; | |
4336 | ||
4337 | /* | |
4338 | * Update the config cache asychronously in case we're the | |
4339 | * root pool, in which case the config cache isn't writable yet. | |
4340 | */ | |
4341 | if (need_update) | |
4342 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
4343 | } | |
4344 | ||
6cb8e530 PZ |
4345 | static void |
4346 | spa_ld_prepare_for_reload(spa_t *spa) | |
4347 | { | |
4348 | int mode = spa->spa_mode; | |
4349 | int async_suspended = spa->spa_async_suspended; | |
4350 | ||
4351 | spa_unload(spa); | |
4352 | spa_deactivate(spa); | |
4353 | spa_activate(spa, mode); | |
4354 | ||
4355 | /* | |
4356 | * We save the value of spa_async_suspended as it gets reset to 0 by | |
4357 | * spa_unload(). We want to restore it back to the original value before | |
4358 | * returning as we might be calling spa_async_resume() later. | |
4359 | */ | |
4360 | spa->spa_async_suspended = async_suspended; | |
4361 | } | |
4362 | ||
9eb7b46e | 4363 | static int |
d2734cce SD |
4364 | spa_ld_read_checkpoint_txg(spa_t *spa) |
4365 | { | |
4366 | uberblock_t checkpoint; | |
4367 | int error = 0; | |
4368 | ||
4369 | ASSERT0(spa->spa_checkpoint_txg); | |
4370 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4371 | ||
4372 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4373 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4374 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4375 | ||
4376 | if (error == ENOENT) | |
4377 | return (0); | |
4378 | ||
4379 | if (error != 0) | |
4380 | return (error); | |
4381 | ||
4382 | ASSERT3U(checkpoint.ub_txg, !=, 0); | |
4383 | ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); | |
4384 | ASSERT3U(checkpoint.ub_timestamp, !=, 0); | |
4385 | spa->spa_checkpoint_txg = checkpoint.ub_txg; | |
4386 | spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; | |
4387 | ||
4388 | return (0); | |
4389 | } | |
4390 | ||
4391 | static int | |
4392 | spa_ld_mos_init(spa_t *spa, spa_import_type_t type) | |
9eb7b46e PZ |
4393 | { |
4394 | int error = 0; | |
9eb7b46e | 4395 | |
4a0ee12a | 4396 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6cb8e530 | 4397 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); |
4a0ee12a | 4398 | |
9eb7b46e | 4399 | /* |
6cb8e530 PZ |
4400 | * Never trust the config that is provided unless we are assembling |
4401 | * a pool following a split. | |
4402 | * This means don't trust blkptrs and the vdev tree in general. This | |
4403 | * also effectively puts the spa in read-only mode since | |
4404 | * spa_writeable() checks for spa_trust_config to be true. | |
4405 | * We will later load a trusted config from the MOS. | |
9eb7b46e | 4406 | */ |
6cb8e530 PZ |
4407 | if (type != SPA_IMPORT_ASSEMBLE) |
4408 | spa->spa_trust_config = B_FALSE; | |
4409 | ||
9eb7b46e PZ |
4410 | /* |
4411 | * Parse the config provided to create a vdev tree. | |
4412 | */ | |
6cb8e530 | 4413 | error = spa_ld_parse_config(spa, type); |
9eb7b46e PZ |
4414 | if (error != 0) |
4415 | return (error); | |
4416 | ||
ca95f70d OF |
4417 | spa_import_progress_add(spa); |
4418 | ||
9eb7b46e PZ |
4419 | /* |
4420 | * Now that we have the vdev tree, try to open each vdev. This involves | |
4421 | * opening the underlying physical device, retrieving its geometry and | |
4422 | * probing the vdev with a dummy I/O. The state of each vdev will be set | |
4423 | * based on the success of those operations. After this we'll be ready | |
4424 | * to read from the vdevs. | |
4425 | */ | |
4426 | error = spa_ld_open_vdevs(spa); | |
4427 | if (error != 0) | |
4428 | return (error); | |
4429 | ||
4430 | /* | |
4431 | * Read the label of each vdev and make sure that the GUIDs stored | |
4432 | * there match the GUIDs in the config provided. | |
6cb8e530 PZ |
4433 | * If we're assembling a new pool that's been split off from an |
4434 | * existing pool, the labels haven't yet been updated so we skip | |
4435 | * validation for now. | |
9eb7b46e | 4436 | */ |
6cb8e530 PZ |
4437 | if (type != SPA_IMPORT_ASSEMBLE) { |
4438 | error = spa_ld_validate_vdevs(spa); | |
4439 | if (error != 0) | |
4440 | return (error); | |
4441 | } | |
9eb7b46e PZ |
4442 | |
4443 | /* | |
d2734cce SD |
4444 | * Read all vdev labels to find the best uberblock (i.e. latest, |
4445 | * unless spa_load_max_txg is set) and store it in spa_uberblock. We | |
4446 | * get the list of features required to read blkptrs in the MOS from | |
4447 | * the vdev label with the best uberblock and verify that our version | |
4448 | * of zfs supports them all. | |
9eb7b46e | 4449 | */ |
6cb8e530 | 4450 | error = spa_ld_select_uberblock(spa, type); |
9eb7b46e PZ |
4451 | if (error != 0) |
4452 | return (error); | |
4453 | ||
4454 | /* | |
4455 | * Pass that uberblock to the dsl_pool layer which will open the root | |
4456 | * blkptr. This blkptr points to the latest version of the MOS and will | |
4457 | * allow us to read its contents. | |
4458 | */ | |
4459 | error = spa_ld_open_rootbp(spa); | |
4460 | if (error != 0) | |
4461 | return (error); | |
4462 | ||
d2734cce SD |
4463 | return (0); |
4464 | } | |
4465 | ||
4466 | static int | |
4467 | spa_ld_checkpoint_rewind(spa_t *spa) | |
4468 | { | |
4469 | uberblock_t checkpoint; | |
4470 | int error = 0; | |
4471 | ||
4472 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4473 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4474 | ||
4475 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4476 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4477 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4478 | ||
4479 | if (error != 0) { | |
4480 | spa_load_failed(spa, "unable to retrieve checkpointed " | |
4481 | "uberblock from the MOS config [error=%d]", error); | |
4482 | ||
4483 | if (error == ENOENT) | |
4484 | error = ZFS_ERR_NO_CHECKPOINT; | |
4485 | ||
4486 | return (error); | |
4487 | } | |
4488 | ||
4489 | ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); | |
4490 | ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); | |
4491 | ||
4492 | /* | |
4493 | * We need to update the txg and timestamp of the checkpointed | |
4494 | * uberblock to be higher than the latest one. This ensures that | |
4495 | * the checkpointed uberblock is selected if we were to close and | |
4496 | * reopen the pool right after we've written it in the vdev labels. | |
4497 | * (also see block comment in vdev_uberblock_compare) | |
4498 | */ | |
4499 | checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; | |
4500 | checkpoint.ub_timestamp = gethrestime_sec(); | |
4501 | ||
4502 | /* | |
4503 | * Set current uberblock to be the checkpointed uberblock. | |
4504 | */ | |
4505 | spa->spa_uberblock = checkpoint; | |
4506 | ||
4507 | /* | |
4508 | * If we are doing a normal rewind, then the pool is open for | |
4509 | * writing and we sync the "updated" checkpointed uberblock to | |
4510 | * disk. Once this is done, we've basically rewound the whole | |
4511 | * pool and there is no way back. | |
4512 | * | |
4513 | * There are cases when we don't want to attempt and sync the | |
4514 | * checkpointed uberblock to disk because we are opening a | |
4515 | * pool as read-only. Specifically, verifying the checkpointed | |
4516 | * state with zdb, and importing the checkpointed state to get | |
4517 | * a "preview" of its content. | |
4518 | */ | |
4519 | if (spa_writeable(spa)) { | |
4520 | vdev_t *rvd = spa->spa_root_vdev; | |
4521 | ||
4522 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4523 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; | |
4524 | int svdcount = 0; | |
4525 | int children = rvd->vdev_children; | |
4526 | int c0 = spa_get_random(children); | |
4527 | ||
4528 | for (int c = 0; c < children; c++) { | |
4529 | vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; | |
4530 | ||
4531 | /* Stop when revisiting the first vdev */ | |
4532 | if (c > 0 && svd[0] == vd) | |
4533 | break; | |
4534 | ||
4535 | if (vd->vdev_ms_array == 0 || vd->vdev_islog || | |
4536 | !vdev_is_concrete(vd)) | |
4537 | continue; | |
4538 | ||
4539 | svd[svdcount++] = vd; | |
4540 | if (svdcount == SPA_SYNC_MIN_VDEVS) | |
4541 | break; | |
4542 | } | |
4543 | error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); | |
4544 | if (error == 0) | |
4545 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
4546 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4547 | ||
4548 | if (error != 0) { | |
4549 | spa_load_failed(spa, "failed to write checkpointed " | |
4550 | "uberblock to the vdev labels [error=%d]", error); | |
4551 | return (error); | |
4552 | } | |
4553 | } | |
4554 | ||
4555 | return (0); | |
4556 | } | |
4557 | ||
4558 | static int | |
4559 | spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, | |
4560 | boolean_t *update_config_cache) | |
4561 | { | |
4562 | int error; | |
4563 | ||
4564 | /* | |
4565 | * Parse the config for pool, open and validate vdevs, | |
4566 | * select an uberblock, and use that uberblock to open | |
4567 | * the MOS. | |
4568 | */ | |
4569 | error = spa_ld_mos_init(spa, type); | |
4570 | if (error != 0) | |
4571 | return (error); | |
4572 | ||
9eb7b46e | 4573 | /* |
6cb8e530 PZ |
4574 | * Retrieve the trusted config stored in the MOS and use it to create |
4575 | * a new, exact version of the vdev tree, then reopen all vdevs. | |
9eb7b46e | 4576 | */ |
d2734cce | 4577 | error = spa_ld_trusted_config(spa, type, B_FALSE); |
6cb8e530 | 4578 | if (error == EAGAIN) { |
d2734cce SD |
4579 | if (update_config_cache != NULL) |
4580 | *update_config_cache = B_TRUE; | |
4581 | ||
6cb8e530 PZ |
4582 | /* |
4583 | * Redo the loading process with the trusted config if it is | |
4584 | * too different from the untrusted config. | |
4585 | */ | |
4586 | spa_ld_prepare_for_reload(spa); | |
d2734cce SD |
4587 | spa_load_note(spa, "RELOADING"); |
4588 | error = spa_ld_mos_init(spa, type); | |
4589 | if (error != 0) | |
4590 | return (error); | |
4591 | ||
4592 | error = spa_ld_trusted_config(spa, type, B_TRUE); | |
4593 | if (error != 0) | |
4594 | return (error); | |
4595 | ||
6cb8e530 | 4596 | } else if (error != 0) { |
9eb7b46e | 4597 | return (error); |
6cb8e530 | 4598 | } |
9eb7b46e | 4599 | |
d2734cce SD |
4600 | return (0); |
4601 | } | |
4602 | ||
4603 | /* | |
4604 | * Load an existing storage pool, using the config provided. This config | |
4605 | * describes which vdevs are part of the pool and is later validated against | |
4606 | * partial configs present in each vdev's label and an entire copy of the | |
4607 | * config stored in the MOS. | |
4608 | */ | |
4609 | static int | |
4610 | spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport) | |
4611 | { | |
4612 | int error = 0; | |
4613 | boolean_t missing_feat_write = B_FALSE; | |
4614 | boolean_t checkpoint_rewind = | |
4615 | (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4616 | boolean_t update_config_cache = B_FALSE; | |
4617 | ||
4618 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4619 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); | |
4620 | ||
4621 | spa_load_note(spa, "LOADING"); | |
4622 | ||
4623 | error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); | |
4624 | if (error != 0) | |
4625 | return (error); | |
4626 | ||
4627 | /* | |
4628 | * If we are rewinding to the checkpoint then we need to repeat | |
4629 | * everything we've done so far in this function but this time | |
4630 | * selecting the checkpointed uberblock and using that to open | |
4631 | * the MOS. | |
4632 | */ | |
4633 | if (checkpoint_rewind) { | |
4634 | /* | |
4635 | * If we are rewinding to the checkpoint update config cache | |
4636 | * anyway. | |
4637 | */ | |
4638 | update_config_cache = B_TRUE; | |
4639 | ||
4640 | /* | |
4641 | * Extract the checkpointed uberblock from the current MOS | |
4642 | * and use this as the pool's uberblock from now on. If the | |
4643 | * pool is imported as writeable we also write the checkpoint | |
4644 | * uberblock to the labels, making the rewind permanent. | |
4645 | */ | |
4646 | error = spa_ld_checkpoint_rewind(spa); | |
4647 | if (error != 0) | |
4648 | return (error); | |
4649 | ||
4650 | /* | |
4651 | * Redo the loading process process again with the | |
4652 | * checkpointed uberblock. | |
4653 | */ | |
4654 | spa_ld_prepare_for_reload(spa); | |
4655 | spa_load_note(spa, "LOADING checkpointed uberblock"); | |
4656 | error = spa_ld_mos_with_trusted_config(spa, type, NULL); | |
4657 | if (error != 0) | |
4658 | return (error); | |
4659 | } | |
4660 | ||
4661 | /* | |
4662 | * Retrieve the checkpoint txg if the pool has a checkpoint. | |
4663 | */ | |
4664 | error = spa_ld_read_checkpoint_txg(spa); | |
4665 | if (error != 0) | |
4666 | return (error); | |
4667 | ||
9eb7b46e PZ |
4668 | /* |
4669 | * Retrieve the mapping of indirect vdevs. Those vdevs were removed | |
4670 | * from the pool and their contents were re-mapped to other vdevs. Note | |
4671 | * that everything that we read before this step must have been | |
4672 | * rewritten on concrete vdevs after the last device removal was | |
4673 | * initiated. Otherwise we could be reading from indirect vdevs before | |
4674 | * we have loaded their mappings. | |
4675 | */ | |
4676 | error = spa_ld_open_indirect_vdev_metadata(spa); | |
4677 | if (error != 0) | |
4678 | return (error); | |
4679 | ||
4680 | /* | |
4681 | * Retrieve the full list of active features from the MOS and check if | |
4682 | * they are all supported. | |
4683 | */ | |
4a0ee12a | 4684 | error = spa_ld_check_features(spa, &missing_feat_write); |
9eb7b46e PZ |
4685 | if (error != 0) |
4686 | return (error); | |
4687 | ||
4688 | /* | |
4689 | * Load several special directories from the MOS needed by the dsl_pool | |
4690 | * layer. | |
4691 | */ | |
4692 | error = spa_ld_load_special_directories(spa); | |
4693 | if (error != 0) | |
4694 | return (error); | |
4695 | ||
9eb7b46e PZ |
4696 | /* |
4697 | * Retrieve pool properties from the MOS. | |
4698 | */ | |
4699 | error = spa_ld_get_props(spa); | |
4700 | if (error != 0) | |
4701 | return (error); | |
4702 | ||
4703 | /* | |
4704 | * Retrieve the list of auxiliary devices - cache devices and spares - | |
4705 | * and open them. | |
4706 | */ | |
4707 | error = spa_ld_open_aux_vdevs(spa, type); | |
4708 | if (error != 0) | |
4709 | return (error); | |
4710 | ||
4711 | /* | |
4712 | * Load the metadata for all vdevs. Also check if unopenable devices | |
4713 | * should be autoreplaced. | |
4714 | */ | |
4a0ee12a | 4715 | error = spa_ld_load_vdev_metadata(spa); |
9eb7b46e PZ |
4716 | if (error != 0) |
4717 | return (error); | |
4718 | ||
4719 | error = spa_ld_load_dedup_tables(spa); | |
4720 | if (error != 0) | |
4721 | return (error); | |
4722 | ||
4723 | /* | |
4724 | * Verify the logs now to make sure we don't have any unexpected errors | |
4725 | * when we claim log blocks later. | |
4726 | */ | |
4727 | error = spa_ld_verify_logs(spa, type, ereport); | |
4728 | if (error != 0) | |
4729 | return (error); | |
4730 | ||
9ae529ec | 4731 | if (missing_feat_write) { |
6cb8e530 | 4732 | ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); |
9ae529ec CS |
4733 | |
4734 | /* | |
4735 | * At this point, we know that we can open the pool in | |
4736 | * read-only mode but not read-write mode. We now have enough | |
4737 | * information and can return to userland. | |
4738 | */ | |
9eb7b46e PZ |
4739 | return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, |
4740 | ENOTSUP)); | |
9ae529ec CS |
4741 | } |
4742 | ||
572e2857 | 4743 | /* |
9eb7b46e PZ |
4744 | * Traverse the last txgs to make sure the pool was left off in a safe |
4745 | * state. When performing an extreme rewind, we verify the whole pool, | |
4746 | * which can take a very long time. | |
572e2857 | 4747 | */ |
4a0ee12a | 4748 | error = spa_ld_verify_pool_data(spa); |
9eb7b46e PZ |
4749 | if (error != 0) |
4750 | return (error); | |
572e2857 | 4751 | |
9eb7b46e PZ |
4752 | /* |
4753 | * Calculate the deflated space for the pool. This must be done before | |
4754 | * we write anything to the pool because we'd need to update the space | |
4755 | * accounting using the deflated sizes. | |
4756 | */ | |
4757 | spa_update_dspace(spa); | |
4758 | ||
4759 | /* | |
4760 | * We have now retrieved all the information we needed to open the | |
4761 | * pool. If we are importing the pool in read-write mode, a few | |
4762 | * additional steps must be performed to finish the import. | |
4763 | */ | |
6cb8e530 | 4764 | if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || |
428870ff | 4765 | spa->spa_load_max_txg == UINT64_MAX)) { |
6cb8e530 PZ |
4766 | uint64_t config_cache_txg = spa->spa_config_txg; |
4767 | ||
4768 | ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f | 4769 | |
d2734cce SD |
4770 | /* |
4771 | * In case of a checkpoint rewind, log the original txg | |
4772 | * of the checkpointed uberblock. | |
4773 | */ | |
4774 | if (checkpoint_rewind) { | |
4775 | spa_history_log_internal(spa, "checkpoint rewind", | |
4776 | NULL, "rewound state to txg=%llu", | |
4777 | (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); | |
4778 | } | |
4779 | ||
34dc7c2f | 4780 | /* |
9eb7b46e | 4781 | * Traverse the ZIL and claim all blocks. |
34dc7c2f | 4782 | */ |
9eb7b46e | 4783 | spa_ld_claim_log_blocks(spa); |
428870ff | 4784 | |
9eb7b46e PZ |
4785 | /* |
4786 | * Kick-off the syncing thread. | |
4787 | */ | |
34dc7c2f BB |
4788 | spa->spa_sync_on = B_TRUE; |
4789 | txg_sync_start(spa->spa_dsl_pool); | |
379ca9cf | 4790 | mmp_thread_start(spa); |
34dc7c2f BB |
4791 | |
4792 | /* | |
428870ff BB |
4793 | * Wait for all claims to sync. We sync up to the highest |
4794 | * claimed log block birth time so that claimed log blocks | |
4795 | * don't appear to be from the future. spa_claim_max_txg | |
9eb7b46e PZ |
4796 | * will have been set for us by ZIL traversal operations |
4797 | * performed above. | |
34dc7c2f | 4798 | */ |
428870ff | 4799 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
4800 | |
4801 | /* | |
9eb7b46e PZ |
4802 | * Check if we need to request an update of the config. On the |
4803 | * next sync, we would update the config stored in vdev labels | |
4804 | * and the cachefile (by default /etc/zfs/zpool.cache). | |
34dc7c2f | 4805 | */ |
6cb8e530 | 4806 | spa_ld_check_for_config_update(spa, config_cache_txg, |
d2734cce | 4807 | update_config_cache); |
fb5f0bc8 BB |
4808 | |
4809 | /* | |
4810 | * Check all DTLs to see if anything needs resilvering. | |
4811 | */ | |
428870ff | 4812 | if (!dsl_scan_resilvering(spa->spa_dsl_pool) && |
9eb7b46e | 4813 | vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) |
fb5f0bc8 | 4814 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
428870ff | 4815 | |
6f1ffb06 MA |
4816 | /* |
4817 | * Log the fact that we booted up (so that we can detect if | |
4818 | * we rebooted in the middle of an operation). | |
4819 | */ | |
d5e024cb | 4820 | spa_history_log_version(spa, "open", NULL); |
6f1ffb06 | 4821 | |
9b2266e3 SD |
4822 | spa_restart_removal(spa); |
4823 | spa_spawn_aux_threads(spa); | |
4824 | ||
428870ff BB |
4825 | /* |
4826 | * Delete any inconsistent datasets. | |
9b2266e3 SD |
4827 | * |
4828 | * Note: | |
4829 | * Since we may be issuing deletes for clones here, | |
4830 | * we make sure to do so after we've spawned all the | |
4831 | * auxiliary threads above (from which the livelist | |
4832 | * deletion zthr is part of). | |
428870ff BB |
4833 | */ |
4834 | (void) dmu_objset_find(spa_name(spa), | |
4835 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
4836 | ||
4837 | /* | |
4838 | * Clean up any stale temporary dataset userrefs. | |
4839 | */ | |
4840 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
a1d477c2 | 4841 | |
619f0976 GW |
4842 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
4843 | vdev_initialize_restart(spa->spa_root_vdev); | |
1b939560 BB |
4844 | vdev_trim_restart(spa->spa_root_vdev); |
4845 | vdev_autotrim_restart(spa); | |
619f0976 | 4846 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
4847 | } |
4848 | ||
ca95f70d | 4849 | spa_import_progress_remove(spa_guid(spa)); |
4a0ee12a PZ |
4850 | spa_load_note(spa, "LOADED"); |
4851 | ||
428870ff BB |
4852 | return (0); |
4853 | } | |
34dc7c2f | 4854 | |
428870ff | 4855 | static int |
6cb8e530 | 4856 | spa_load_retry(spa_t *spa, spa_load_state_t state) |
428870ff | 4857 | { |
572e2857 BB |
4858 | int mode = spa->spa_mode; |
4859 | ||
428870ff BB |
4860 | spa_unload(spa); |
4861 | spa_deactivate(spa); | |
4862 | ||
dea377c0 | 4863 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; |
428870ff | 4864 | |
572e2857 | 4865 | spa_activate(spa, mode); |
428870ff BB |
4866 | spa_async_suspend(spa); |
4867 | ||
4a0ee12a PZ |
4868 | spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", |
4869 | (u_longlong_t)spa->spa_load_max_txg); | |
4870 | ||
6cb8e530 | 4871 | return (spa_load(spa, state, SPA_IMPORT_EXISTING)); |
428870ff BB |
4872 | } |
4873 | ||
9ae529ec CS |
4874 | /* |
4875 | * If spa_load() fails this function will try loading prior txg's. If | |
4876 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
4877 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
4878 | * function will not rewind the pool and will return the same error as | |
4879 | * spa_load(). | |
4880 | */ | |
428870ff | 4881 | static int |
6cb8e530 PZ |
4882 | spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, |
4883 | int rewind_flags) | |
428870ff | 4884 | { |
9ae529ec | 4885 | nvlist_t *loadinfo = NULL; |
428870ff BB |
4886 | nvlist_t *config = NULL; |
4887 | int load_error, rewind_error; | |
4888 | uint64_t safe_rewind_txg; | |
4889 | uint64_t min_txg; | |
4890 | ||
4891 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
4892 | spa->spa_load_max_txg = spa->spa_load_txg; | |
4893 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
4894 | } else { | |
4895 | spa->spa_load_max_txg = max_request; | |
dea377c0 MA |
4896 | if (max_request != UINT64_MAX) |
4897 | spa->spa_extreme_rewind = B_TRUE; | |
428870ff BB |
4898 | } |
4899 | ||
6cb8e530 | 4900 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); |
428870ff BB |
4901 | if (load_error == 0) |
4902 | return (0); | |
d2734cce SD |
4903 | if (load_error == ZFS_ERR_NO_CHECKPOINT) { |
4904 | /* | |
4905 | * When attempting checkpoint-rewind on a pool with no | |
4906 | * checkpoint, we should not attempt to load uberblocks | |
4907 | * from previous txgs when spa_load fails. | |
4908 | */ | |
4909 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
ca95f70d | 4910 | spa_import_progress_remove(spa_guid(spa)); |
d2734cce SD |
4911 | return (load_error); |
4912 | } | |
428870ff BB |
4913 | |
4914 | if (spa->spa_root_vdev != NULL) | |
4915 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
4916 | ||
4917 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
4918 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
4919 | ||
4920 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
4921 | nvlist_free(config); | |
ca95f70d | 4922 | spa_import_progress_remove(spa_guid(spa)); |
428870ff BB |
4923 | return (load_error); |
4924 | } | |
4925 | ||
9ae529ec CS |
4926 | if (state == SPA_LOAD_RECOVER) { |
4927 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 4928 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
4929 | } else { |
4930 | /* | |
4931 | * If we aren't rolling back save the load info from our first | |
4932 | * import attempt so that we can restore it after attempting | |
4933 | * to rewind. | |
4934 | */ | |
4935 | loadinfo = spa->spa_load_info; | |
4936 | spa->spa_load_info = fnvlist_alloc(); | |
4937 | } | |
428870ff BB |
4938 | |
4939 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
4940 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
4941 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
4942 | TXG_INITIAL : safe_rewind_txg; | |
4943 | ||
4944 | /* | |
4945 | * Continue as long as we're finding errors, we're still within | |
4946 | * the acceptable rewind range, and we're still finding uberblocks | |
4947 | */ | |
4948 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
4949 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
4950 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
4951 | spa->spa_extreme_rewind = B_TRUE; | |
6cb8e530 | 4952 | rewind_error = spa_load_retry(spa, state); |
428870ff BB |
4953 | } |
4954 | ||
428870ff BB |
4955 | spa->spa_extreme_rewind = B_FALSE; |
4956 | spa->spa_load_max_txg = UINT64_MAX; | |
4957 | ||
4958 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
4959 | spa_config_set(spa, config); | |
ee6370a7 | 4960 | else |
4961 | nvlist_free(config); | |
428870ff | 4962 | |
9ae529ec CS |
4963 | if (state == SPA_LOAD_RECOVER) { |
4964 | ASSERT3P(loadinfo, ==, NULL); | |
ca95f70d | 4965 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
4966 | return (rewind_error); |
4967 | } else { | |
4968 | /* Store the rewind info as part of the initial load info */ | |
4969 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
4970 | spa->spa_load_info); | |
4971 | ||
4972 | /* Restore the initial load info */ | |
4973 | fnvlist_free(spa->spa_load_info); | |
4974 | spa->spa_load_info = loadinfo; | |
4975 | ||
ca95f70d | 4976 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
4977 | return (load_error); |
4978 | } | |
34dc7c2f BB |
4979 | } |
4980 | ||
4981 | /* | |
4982 | * Pool Open/Import | |
4983 | * | |
4984 | * The import case is identical to an open except that the configuration is sent | |
4985 | * down from userland, instead of grabbed from the configuration cache. For the | |
4986 | * case of an open, the pool configuration will exist in the | |
4987 | * POOL_STATE_UNINITIALIZED state. | |
4988 | * | |
4989 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
4990 | * the same time open the pool, without having to keep around the spa_t in some | |
4991 | * ambiguous state. | |
4992 | */ | |
4993 | static int | |
428870ff BB |
4994 | spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, |
4995 | nvlist_t **config) | |
34dc7c2f BB |
4996 | { |
4997 | spa_t *spa; | |
572e2857 | 4998 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 4999 | int error; |
34dc7c2f | 5000 | int locked = B_FALSE; |
526af785 | 5001 | int firstopen = B_FALSE; |
34dc7c2f BB |
5002 | |
5003 | *spapp = NULL; | |
5004 | ||
5005 | /* | |
5006 | * As disgusting as this is, we need to support recursive calls to this | |
5007 | * function because dsl_dir_open() is called during spa_load(), and ends | |
5008 | * up calling spa_open() again. The real fix is to figure out how to | |
5009 | * avoid dsl_dir_open() calling this in the first place. | |
5010 | */ | |
c25b8f99 | 5011 | if (MUTEX_NOT_HELD(&spa_namespace_lock)) { |
34dc7c2f BB |
5012 | mutex_enter(&spa_namespace_lock); |
5013 | locked = B_TRUE; | |
5014 | } | |
5015 | ||
5016 | if ((spa = spa_lookup(pool)) == NULL) { | |
5017 | if (locked) | |
5018 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5019 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 5020 | } |
428870ff | 5021 | |
34dc7c2f | 5022 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
8a393be3 | 5023 | zpool_load_policy_t policy; |
428870ff | 5024 | |
526af785 PJD |
5025 | firstopen = B_TRUE; |
5026 | ||
8a393be3 | 5027 | zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, |
428870ff | 5028 | &policy); |
8a393be3 | 5029 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) |
428870ff | 5030 | state = SPA_LOAD_RECOVER; |
34dc7c2f | 5031 | |
fb5f0bc8 | 5032 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5033 | |
428870ff BB |
5034 | if (state != SPA_LOAD_RECOVER) |
5035 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6cb8e530 | 5036 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; |
428870ff | 5037 | |
4a0ee12a | 5038 | zfs_dbgmsg("spa_open_common: opening %s", pool); |
8a393be3 PZ |
5039 | error = spa_load_best(spa, state, policy.zlp_txg, |
5040 | policy.zlp_rewind); | |
34dc7c2f BB |
5041 | |
5042 | if (error == EBADF) { | |
5043 | /* | |
5044 | * If vdev_validate() returns failure (indicated by | |
5045 | * EBADF), it indicates that one of the vdevs indicates | |
5046 | * that the pool has been exported or destroyed. If | |
5047 | * this is the case, the config cache is out of sync and | |
5048 | * we should remove the pool from the namespace. | |
5049 | */ | |
34dc7c2f BB |
5050 | spa_unload(spa); |
5051 | spa_deactivate(spa); | |
a1d477c2 | 5052 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 5053 | spa_remove(spa); |
34dc7c2f BB |
5054 | if (locked) |
5055 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5056 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
5057 | } |
5058 | ||
5059 | if (error) { | |
5060 | /* | |
5061 | * We can't open the pool, but we still have useful | |
5062 | * information: the state of each vdev after the | |
5063 | * attempted vdev_open(). Return this to the user. | |
5064 | */ | |
572e2857 | 5065 | if (config != NULL && spa->spa_config) { |
428870ff | 5066 | VERIFY(nvlist_dup(spa->spa_config, config, |
79c76d5b | 5067 | KM_SLEEP) == 0); |
572e2857 BB |
5068 | VERIFY(nvlist_add_nvlist(*config, |
5069 | ZPOOL_CONFIG_LOAD_INFO, | |
5070 | spa->spa_load_info) == 0); | |
5071 | } | |
34dc7c2f BB |
5072 | spa_unload(spa); |
5073 | spa_deactivate(spa); | |
428870ff | 5074 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
5075 | if (locked) |
5076 | mutex_exit(&spa_namespace_lock); | |
5077 | *spapp = NULL; | |
5078 | return (error); | |
34dc7c2f | 5079 | } |
34dc7c2f BB |
5080 | } |
5081 | ||
5082 | spa_open_ref(spa, tag); | |
5083 | ||
b128c09f | 5084 | if (config != NULL) |
34dc7c2f | 5085 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 5086 | |
572e2857 BB |
5087 | /* |
5088 | * If we've recovered the pool, pass back any information we | |
5089 | * gathered while doing the load. | |
5090 | */ | |
5091 | if (state == SPA_LOAD_RECOVER) { | |
5092 | VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, | |
5093 | spa->spa_load_info) == 0); | |
5094 | } | |
5095 | ||
428870ff BB |
5096 | if (locked) { |
5097 | spa->spa_last_open_failed = 0; | |
5098 | spa->spa_last_ubsync_txg = 0; | |
5099 | spa->spa_load_txg = 0; | |
5100 | mutex_exit(&spa_namespace_lock); | |
5101 | } | |
5102 | ||
526af785 | 5103 | if (firstopen) |
a0bd735a | 5104 | zvol_create_minors(spa, spa_name(spa), B_TRUE); |
526af785 | 5105 | |
428870ff BB |
5106 | *spapp = spa; |
5107 | ||
34dc7c2f BB |
5108 | return (0); |
5109 | } | |
5110 | ||
428870ff BB |
5111 | int |
5112 | spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, | |
5113 | nvlist_t **config) | |
5114 | { | |
5115 | return (spa_open_common(name, spapp, tag, policy, config)); | |
5116 | } | |
5117 | ||
34dc7c2f BB |
5118 | int |
5119 | spa_open(const char *name, spa_t **spapp, void *tag) | |
5120 | { | |
428870ff | 5121 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
5122 | } |
5123 | ||
5124 | /* | |
5125 | * Lookup the given spa_t, incrementing the inject count in the process, | |
5126 | * preventing it from being exported or destroyed. | |
5127 | */ | |
5128 | spa_t * | |
5129 | spa_inject_addref(char *name) | |
5130 | { | |
5131 | spa_t *spa; | |
5132 | ||
5133 | mutex_enter(&spa_namespace_lock); | |
5134 | if ((spa = spa_lookup(name)) == NULL) { | |
5135 | mutex_exit(&spa_namespace_lock); | |
5136 | return (NULL); | |
5137 | } | |
5138 | spa->spa_inject_ref++; | |
5139 | mutex_exit(&spa_namespace_lock); | |
5140 | ||
5141 | return (spa); | |
5142 | } | |
5143 | ||
5144 | void | |
5145 | spa_inject_delref(spa_t *spa) | |
5146 | { | |
5147 | mutex_enter(&spa_namespace_lock); | |
5148 | spa->spa_inject_ref--; | |
5149 | mutex_exit(&spa_namespace_lock); | |
5150 | } | |
5151 | ||
5152 | /* | |
5153 | * Add spares device information to the nvlist. | |
5154 | */ | |
5155 | static void | |
5156 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
5157 | { | |
5158 | nvlist_t **spares; | |
5159 | uint_t i, nspares; | |
5160 | nvlist_t *nvroot; | |
5161 | uint64_t guid; | |
5162 | vdev_stat_t *vs; | |
5163 | uint_t vsc; | |
5164 | uint64_t pool; | |
5165 | ||
9babb374 BB |
5166 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5167 | ||
34dc7c2f BB |
5168 | if (spa->spa_spares.sav_count == 0) |
5169 | return; | |
5170 | ||
5171 | VERIFY(nvlist_lookup_nvlist(config, | |
5172 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
5173 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
5174 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
5175 | if (nspares != 0) { | |
5176 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
5177 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
5178 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
5179 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
5180 | ||
5181 | /* | |
5182 | * Go through and find any spares which have since been | |
5183 | * repurposed as an active spare. If this is the case, update | |
5184 | * their status appropriately. | |
5185 | */ | |
5186 | for (i = 0; i < nspares; i++) { | |
5187 | VERIFY(nvlist_lookup_uint64(spares[i], | |
5188 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
b128c09f BB |
5189 | if (spa_spare_exists(guid, &pool, NULL) && |
5190 | pool != 0ULL) { | |
34dc7c2f | 5191 | VERIFY(nvlist_lookup_uint64_array( |
428870ff | 5192 | spares[i], ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
5193 | (uint64_t **)&vs, &vsc) == 0); |
5194 | vs->vs_state = VDEV_STATE_CANT_OPEN; | |
5195 | vs->vs_aux = VDEV_AUX_SPARED; | |
5196 | } | |
5197 | } | |
5198 | } | |
5199 | } | |
5200 | ||
5201 | /* | |
5202 | * Add l2cache device information to the nvlist, including vdev stats. | |
5203 | */ | |
5204 | static void | |
5205 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
5206 | { | |
5207 | nvlist_t **l2cache; | |
5208 | uint_t i, j, nl2cache; | |
5209 | nvlist_t *nvroot; | |
5210 | uint64_t guid; | |
5211 | vdev_t *vd; | |
5212 | vdev_stat_t *vs; | |
5213 | uint_t vsc; | |
5214 | ||
9babb374 BB |
5215 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5216 | ||
34dc7c2f BB |
5217 | if (spa->spa_l2cache.sav_count == 0) |
5218 | return; | |
5219 | ||
34dc7c2f BB |
5220 | VERIFY(nvlist_lookup_nvlist(config, |
5221 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
5222 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
5223 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
5224 | if (nl2cache != 0) { | |
5225 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
5226 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
5227 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
5228 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
5229 | ||
5230 | /* | |
5231 | * Update level 2 cache device stats. | |
5232 | */ | |
5233 | ||
5234 | for (i = 0; i < nl2cache; i++) { | |
5235 | VERIFY(nvlist_lookup_uint64(l2cache[i], | |
5236 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
5237 | ||
5238 | vd = NULL; | |
5239 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
5240 | if (guid == | |
5241 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
5242 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
5243 | break; | |
5244 | } | |
5245 | } | |
5246 | ASSERT(vd != NULL); | |
5247 | ||
5248 | VERIFY(nvlist_lookup_uint64_array(l2cache[i], | |
428870ff BB |
5249 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) |
5250 | == 0); | |
34dc7c2f | 5251 | vdev_get_stats(vd, vs); |
193a37cb TH |
5252 | vdev_config_generate_stats(vd, l2cache[i]); |
5253 | ||
34dc7c2f BB |
5254 | } |
5255 | } | |
34dc7c2f BB |
5256 | } |
5257 | ||
9ae529ec | 5258 | static void |
417104bd | 5259 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) |
9ae529ec | 5260 | { |
9ae529ec CS |
5261 | zap_cursor_t zc; |
5262 | zap_attribute_t za; | |
5263 | ||
9ae529ec CS |
5264 | if (spa->spa_feat_for_read_obj != 0) { |
5265 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5266 | spa->spa_feat_for_read_obj); | |
5267 | zap_cursor_retrieve(&zc, &za) == 0; | |
5268 | zap_cursor_advance(&zc)) { | |
5269 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5270 | za.za_num_integers == 1); | |
417104bd | 5271 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5272 | za.za_first_integer)); |
5273 | } | |
5274 | zap_cursor_fini(&zc); | |
5275 | } | |
5276 | ||
5277 | if (spa->spa_feat_for_write_obj != 0) { | |
5278 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5279 | spa->spa_feat_for_write_obj); | |
5280 | zap_cursor_retrieve(&zc, &za) == 0; | |
5281 | zap_cursor_advance(&zc)) { | |
5282 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5283 | za.za_num_integers == 1); | |
417104bd | 5284 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5285 | za.za_first_integer)); |
5286 | } | |
5287 | zap_cursor_fini(&zc); | |
5288 | } | |
417104bd NB |
5289 | } |
5290 | ||
5291 | static void | |
5292 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
5293 | { | |
5294 | int i; | |
5295 | ||
5296 | for (i = 0; i < SPA_FEATURES; i++) { | |
5297 | zfeature_info_t feature = spa_feature_table[i]; | |
5298 | uint64_t refcount; | |
5299 | ||
5300 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
5301 | continue; | |
5302 | ||
5303 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
5304 | } | |
5305 | } | |
5306 | ||
5307 | /* | |
5308 | * Store a list of pool features and their reference counts in the | |
5309 | * config. | |
5310 | * | |
5311 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
5312 | * the pool features and reference counts from disk, then save the list | |
5313 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
5314 | * and refresh its values from the cached reference counts. This | |
5315 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
5316 | * clear' can resume the pool. | |
5317 | */ | |
5318 | static void | |
5319 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
5320 | { | |
4eb30c68 | 5321 | nvlist_t *features; |
417104bd NB |
5322 | |
5323 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
5324 | ||
4eb30c68 NB |
5325 | mutex_enter(&spa->spa_feat_stats_lock); |
5326 | features = spa->spa_feat_stats; | |
5327 | ||
417104bd NB |
5328 | if (features != NULL) { |
5329 | spa_feature_stats_from_cache(spa, features); | |
5330 | } else { | |
5331 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
5332 | spa->spa_feat_stats = features; | |
5333 | spa_feature_stats_from_disk(spa, features); | |
5334 | } | |
9ae529ec | 5335 | |
417104bd NB |
5336 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, |
5337 | features)); | |
4eb30c68 NB |
5338 | |
5339 | mutex_exit(&spa->spa_feat_stats_lock); | |
9ae529ec CS |
5340 | } |
5341 | ||
34dc7c2f | 5342 | int |
9ae529ec CS |
5343 | spa_get_stats(const char *name, nvlist_t **config, |
5344 | char *altroot, size_t buflen) | |
34dc7c2f BB |
5345 | { |
5346 | int error; | |
5347 | spa_t *spa; | |
5348 | ||
5349 | *config = NULL; | |
428870ff | 5350 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 5351 | |
9babb374 BB |
5352 | if (spa != NULL) { |
5353 | /* | |
5354 | * This still leaves a window of inconsistency where the spares | |
5355 | * or l2cache devices could change and the config would be | |
5356 | * self-inconsistent. | |
5357 | */ | |
5358 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 5359 | |
9babb374 | 5360 | if (*config != NULL) { |
572e2857 BB |
5361 | uint64_t loadtimes[2]; |
5362 | ||
5363 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
5364 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
5365 | VERIFY(nvlist_add_uint64_array(*config, | |
5366 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); | |
5367 | ||
b128c09f | 5368 | VERIFY(nvlist_add_uint64(*config, |
9babb374 BB |
5369 | ZPOOL_CONFIG_ERRCOUNT, |
5370 | spa_get_errlog_size(spa)) == 0); | |
5371 | ||
cec3a0a1 | 5372 | if (spa_suspended(spa)) { |
9babb374 BB |
5373 | VERIFY(nvlist_add_uint64(*config, |
5374 | ZPOOL_CONFIG_SUSPENDED, | |
5375 | spa->spa_failmode) == 0); | |
cec3a0a1 OF |
5376 | VERIFY(nvlist_add_uint64(*config, |
5377 | ZPOOL_CONFIG_SUSPENDED_REASON, | |
5378 | spa->spa_suspended) == 0); | |
5379 | } | |
b128c09f | 5380 | |
9babb374 BB |
5381 | spa_add_spares(spa, *config); |
5382 | spa_add_l2cache(spa, *config); | |
9ae529ec | 5383 | spa_add_feature_stats(spa, *config); |
9babb374 | 5384 | } |
34dc7c2f BB |
5385 | } |
5386 | ||
5387 | /* | |
5388 | * We want to get the alternate root even for faulted pools, so we cheat | |
5389 | * and call spa_lookup() directly. | |
5390 | */ | |
5391 | if (altroot) { | |
5392 | if (spa == NULL) { | |
5393 | mutex_enter(&spa_namespace_lock); | |
5394 | spa = spa_lookup(name); | |
5395 | if (spa) | |
5396 | spa_altroot(spa, altroot, buflen); | |
5397 | else | |
5398 | altroot[0] = '\0'; | |
5399 | spa = NULL; | |
5400 | mutex_exit(&spa_namespace_lock); | |
5401 | } else { | |
5402 | spa_altroot(spa, altroot, buflen); | |
5403 | } | |
5404 | } | |
5405 | ||
9babb374 BB |
5406 | if (spa != NULL) { |
5407 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 5408 | spa_close(spa, FTAG); |
9babb374 | 5409 | } |
34dc7c2f BB |
5410 | |
5411 | return (error); | |
5412 | } | |
5413 | ||
5414 | /* | |
5415 | * Validate that the auxiliary device array is well formed. We must have an | |
5416 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
5417 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
5418 | * specified, as long as they are well-formed. | |
5419 | */ | |
5420 | static int | |
5421 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
5422 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
5423 | vdev_labeltype_t label) | |
5424 | { | |
5425 | nvlist_t **dev; | |
5426 | uint_t i, ndev; | |
5427 | vdev_t *vd; | |
5428 | int error; | |
5429 | ||
b128c09f BB |
5430 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5431 | ||
34dc7c2f BB |
5432 | /* |
5433 | * It's acceptable to have no devs specified. | |
5434 | */ | |
5435 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
5436 | return (0); | |
5437 | ||
5438 | if (ndev == 0) | |
2e528b49 | 5439 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
5440 | |
5441 | /* | |
5442 | * Make sure the pool is formatted with a version that supports this | |
5443 | * device type. | |
5444 | */ | |
5445 | if (spa_version(spa) < version) | |
2e528b49 | 5446 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
5447 | |
5448 | /* | |
5449 | * Set the pending device list so we correctly handle device in-use | |
5450 | * checking. | |
5451 | */ | |
5452 | sav->sav_pending = dev; | |
5453 | sav->sav_npending = ndev; | |
5454 | ||
5455 | for (i = 0; i < ndev; i++) { | |
5456 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
5457 | mode)) != 0) | |
5458 | goto out; | |
5459 | ||
5460 | if (!vd->vdev_ops->vdev_op_leaf) { | |
5461 | vdev_free(vd); | |
2e528b49 | 5462 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5463 | goto out; |
5464 | } | |
5465 | ||
34dc7c2f BB |
5466 | vd->vdev_top = vd; |
5467 | ||
5468 | if ((error = vdev_open(vd)) == 0 && | |
5469 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
5470 | VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, | |
5471 | vd->vdev_guid) == 0); | |
5472 | } | |
5473 | ||
5474 | vdev_free(vd); | |
5475 | ||
5476 | if (error && | |
5477 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
5478 | goto out; | |
5479 | else | |
5480 | error = 0; | |
5481 | } | |
5482 | ||
5483 | out: | |
5484 | sav->sav_pending = NULL; | |
5485 | sav->sav_npending = 0; | |
5486 | return (error); | |
5487 | } | |
5488 | ||
5489 | static int | |
5490 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
5491 | { | |
5492 | int error; | |
5493 | ||
b128c09f BB |
5494 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5495 | ||
34dc7c2f BB |
5496 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
5497 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
5498 | VDEV_LABEL_SPARE)) != 0) { | |
5499 | return (error); | |
5500 | } | |
5501 | ||
5502 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
5503 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
5504 | VDEV_LABEL_L2CACHE)); | |
5505 | } | |
5506 | ||
5507 | static void | |
5508 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
5509 | const char *config) | |
5510 | { | |
5511 | int i; | |
5512 | ||
5513 | if (sav->sav_config != NULL) { | |
5514 | nvlist_t **olddevs; | |
5515 | uint_t oldndevs; | |
5516 | nvlist_t **newdevs; | |
5517 | ||
5518 | /* | |
4e33ba4c | 5519 | * Generate new dev list by concatenating with the |
34dc7c2f BB |
5520 | * current dev list. |
5521 | */ | |
5522 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, | |
5523 | &olddevs, &oldndevs) == 0); | |
5524 | ||
5525 | newdevs = kmem_alloc(sizeof (void *) * | |
79c76d5b | 5526 | (ndevs + oldndevs), KM_SLEEP); |
34dc7c2f BB |
5527 | for (i = 0; i < oldndevs; i++) |
5528 | VERIFY(nvlist_dup(olddevs[i], &newdevs[i], | |
79c76d5b | 5529 | KM_SLEEP) == 0); |
34dc7c2f BB |
5530 | for (i = 0; i < ndevs; i++) |
5531 | VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], | |
79c76d5b | 5532 | KM_SLEEP) == 0); |
34dc7c2f BB |
5533 | |
5534 | VERIFY(nvlist_remove(sav->sav_config, config, | |
5535 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
5536 | ||
5537 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
5538 | config, newdevs, ndevs + oldndevs) == 0); | |
5539 | for (i = 0; i < oldndevs + ndevs; i++) | |
5540 | nvlist_free(newdevs[i]); | |
5541 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
5542 | } else { | |
5543 | /* | |
5544 | * Generate a new dev list. | |
5545 | */ | |
5546 | VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5547 | KM_SLEEP) == 0); |
34dc7c2f BB |
5548 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, |
5549 | devs, ndevs) == 0); | |
5550 | } | |
5551 | } | |
5552 | ||
5553 | /* | |
5554 | * Stop and drop level 2 ARC devices | |
5555 | */ | |
5556 | void | |
5557 | spa_l2cache_drop(spa_t *spa) | |
5558 | { | |
5559 | vdev_t *vd; | |
5560 | int i; | |
5561 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
5562 | ||
5563 | for (i = 0; i < sav->sav_count; i++) { | |
5564 | uint64_t pool; | |
5565 | ||
5566 | vd = sav->sav_vdevs[i]; | |
5567 | ASSERT(vd != NULL); | |
5568 | ||
fb5f0bc8 BB |
5569 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
5570 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 5571 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
5572 | } |
5573 | } | |
5574 | ||
b5256303 TC |
5575 | /* |
5576 | * Verify encryption parameters for spa creation. If we are encrypting, we must | |
5577 | * have the encryption feature flag enabled. | |
5578 | */ | |
5579 | static int | |
5580 | spa_create_check_encryption_params(dsl_crypto_params_t *dcp, | |
5581 | boolean_t has_encryption) | |
5582 | { | |
5583 | if (dcp->cp_crypt != ZIO_CRYPT_OFF && | |
5584 | dcp->cp_crypt != ZIO_CRYPT_INHERIT && | |
5585 | !has_encryption) | |
5586 | return (SET_ERROR(ENOTSUP)); | |
5587 | ||
1fff937a | 5588 | return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); |
b5256303 TC |
5589 | } |
5590 | ||
34dc7c2f BB |
5591 | /* |
5592 | * Pool Creation | |
5593 | */ | |
5594 | int | |
5595 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b5256303 | 5596 | nvlist_t *zplprops, dsl_crypto_params_t *dcp) |
34dc7c2f BB |
5597 | { |
5598 | spa_t *spa; | |
5599 | char *altroot = NULL; | |
5600 | vdev_t *rvd; | |
5601 | dsl_pool_t *dp; | |
5602 | dmu_tx_t *tx; | |
9babb374 | 5603 | int error = 0; |
34dc7c2f BB |
5604 | uint64_t txg = TXG_INITIAL; |
5605 | nvlist_t **spares, **l2cache; | |
5606 | uint_t nspares, nl2cache; | |
52ce99dd | 5607 | uint64_t version, obj; |
9ae529ec | 5608 | boolean_t has_features; |
b5256303 TC |
5609 | boolean_t has_encryption; |
5610 | spa_feature_t feat; | |
5611 | char *feat_name; | |
83e9986f RY |
5612 | char *poolname; |
5613 | nvlist_t *nvl; | |
5614 | ||
cc99f275 DB |
5615 | if (props == NULL || |
5616 | nvlist_lookup_string(props, "tname", &poolname) != 0) | |
83e9986f | 5617 | poolname = (char *)pool; |
34dc7c2f BB |
5618 | |
5619 | /* | |
5620 | * If this pool already exists, return failure. | |
5621 | */ | |
5622 | mutex_enter(&spa_namespace_lock); | |
83e9986f | 5623 | if (spa_lookup(poolname) != NULL) { |
34dc7c2f | 5624 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5625 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5626 | } |
5627 | ||
5628 | /* | |
5629 | * Allocate a new spa_t structure. | |
5630 | */ | |
83e9986f RY |
5631 | nvl = fnvlist_alloc(); |
5632 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
34dc7c2f BB |
5633 | (void) nvlist_lookup_string(props, |
5634 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
83e9986f RY |
5635 | spa = spa_add(poolname, nvl, altroot); |
5636 | fnvlist_free(nvl); | |
fb5f0bc8 | 5637 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5638 | |
34dc7c2f | 5639 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
5640 | spa_deactivate(spa); |
5641 | spa_remove(spa); | |
b128c09f | 5642 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
5643 | return (error); |
5644 | } | |
5645 | ||
83e9986f RY |
5646 | /* |
5647 | * Temporary pool names should never be written to disk. | |
5648 | */ | |
5649 | if (poolname != pool) | |
5650 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
5651 | ||
9ae529ec | 5652 | has_features = B_FALSE; |
b5256303 | 5653 | has_encryption = B_FALSE; |
1c27024e | 5654 | for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); |
9ae529ec | 5655 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { |
b5256303 | 5656 | if (zpool_prop_feature(nvpair_name(elem))) { |
9ae529ec | 5657 | has_features = B_TRUE; |
b5256303 TC |
5658 | |
5659 | feat_name = strchr(nvpair_name(elem), '@') + 1; | |
5660 | VERIFY0(zfeature_lookup_name(feat_name, &feat)); | |
5661 | if (feat == SPA_FEATURE_ENCRYPTION) | |
5662 | has_encryption = B_TRUE; | |
5663 | } | |
5664 | } | |
5665 | ||
5666 | /* verify encryption params, if they were provided */ | |
5667 | if (dcp != NULL) { | |
5668 | error = spa_create_check_encryption_params(dcp, has_encryption); | |
5669 | if (error != 0) { | |
5670 | spa_deactivate(spa); | |
5671 | spa_remove(spa); | |
5672 | mutex_exit(&spa_namespace_lock); | |
5673 | return (error); | |
5674 | } | |
9ae529ec CS |
5675 | } |
5676 | ||
5677 | if (has_features || nvlist_lookup_uint64(props, | |
5678 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 5679 | version = SPA_VERSION; |
9ae529ec CS |
5680 | } |
5681 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
5682 | |
5683 | spa->spa_first_txg = txg; | |
5684 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
5685 | spa->spa_uberblock.ub_version = version; |
5686 | spa->spa_ubsync = spa->spa_uberblock; | |
3dfb57a3 | 5687 | spa->spa_load_state = SPA_LOAD_CREATE; |
a1d477c2 MA |
5688 | spa->spa_removing_phys.sr_state = DSS_NONE; |
5689 | spa->spa_removing_phys.sr_removing_vdev = -1; | |
5690 | spa->spa_removing_phys.sr_prev_indirect_vdev = -1; | |
944a3724 | 5691 | spa->spa_indirect_vdevs_loaded = B_TRUE; |
34dc7c2f | 5692 | |
9babb374 BB |
5693 | /* |
5694 | * Create "The Godfather" zio to hold all async IOs | |
5695 | */ | |
e022864d MA |
5696 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
5697 | KM_SLEEP); | |
1c27024e | 5698 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
5699 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
5700 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
5701 | ZIO_FLAG_GODFATHER); | |
5702 | } | |
9babb374 | 5703 | |
34dc7c2f BB |
5704 | /* |
5705 | * Create the root vdev. | |
5706 | */ | |
b128c09f | 5707 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5708 | |
5709 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
5710 | ||
5711 | ASSERT(error != 0 || rvd != NULL); | |
5712 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
5713 | ||
5714 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
2e528b49 | 5715 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5716 | |
5717 | if (error == 0 && | |
5718 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
5719 | (error = spa_validate_aux(spa, nvroot, txg, | |
5720 | VDEV_ALLOC_ADD)) == 0) { | |
cc99f275 DB |
5721 | /* |
5722 | * instantiate the metaslab groups (this will dirty the vdevs) | |
5723 | * we can no longer error exit past this point | |
5724 | */ | |
5725 | for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { | |
5726 | vdev_t *vd = rvd->vdev_child[c]; | |
5727 | ||
5728 | vdev_metaslab_set_size(vd); | |
5729 | vdev_expand(vd, txg); | |
9babb374 | 5730 | } |
34dc7c2f BB |
5731 | } |
5732 | ||
b128c09f | 5733 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5734 | |
5735 | if (error != 0) { | |
5736 | spa_unload(spa); | |
5737 | spa_deactivate(spa); | |
5738 | spa_remove(spa); | |
5739 | mutex_exit(&spa_namespace_lock); | |
5740 | return (error); | |
5741 | } | |
5742 | ||
5743 | /* | |
5744 | * Get the list of spares, if specified. | |
5745 | */ | |
5746 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5747 | &spares, &nspares) == 0) { | |
5748 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5749 | KM_SLEEP) == 0); |
34dc7c2f BB |
5750 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
5751 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 5752 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5753 | spa_load_spares(spa); |
b128c09f | 5754 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5755 | spa->spa_spares.sav_sync = B_TRUE; |
5756 | } | |
5757 | ||
5758 | /* | |
5759 | * Get the list of level 2 cache devices, if specified. | |
5760 | */ | |
5761 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5762 | &l2cache, &nl2cache) == 0) { | |
5763 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 5764 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5765 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
5766 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 5767 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5768 | spa_load_l2cache(spa); |
b128c09f | 5769 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5770 | spa->spa_l2cache.sav_sync = B_TRUE; |
5771 | } | |
5772 | ||
9ae529ec | 5773 | spa->spa_is_initializing = B_TRUE; |
b5256303 | 5774 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); |
9ae529ec | 5775 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 5776 | |
428870ff BB |
5777 | /* |
5778 | * Create DDTs (dedup tables). | |
5779 | */ | |
5780 | ddt_create(spa); | |
5781 | ||
5782 | spa_update_dspace(spa); | |
5783 | ||
34dc7c2f BB |
5784 | tx = dmu_tx_create_assigned(dp, txg); |
5785 | ||
d5e024cb BB |
5786 | /* |
5787 | * Create the pool's history object. | |
5788 | */ | |
5789 | if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history) | |
5790 | spa_history_create_obj(spa, tx); | |
5791 | ||
5792 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); | |
5793 | spa_history_log_version(spa, "create", tx); | |
5794 | ||
34dc7c2f BB |
5795 | /* |
5796 | * Create the pool config object. | |
5797 | */ | |
5798 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 5799 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
5800 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
5801 | ||
5802 | if (zap_add(spa->spa_meta_objset, | |
5803 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
5804 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
5805 | cmn_err(CE_PANIC, "failed to add pool config"); | |
5806 | } | |
5807 | ||
428870ff BB |
5808 | if (zap_add(spa->spa_meta_objset, |
5809 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
5810 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
5811 | cmn_err(CE_PANIC, "failed to add pool version"); | |
5812 | } | |
5813 | ||
34dc7c2f BB |
5814 | /* Newly created pools with the right version are always deflated. */ |
5815 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
5816 | spa->spa_deflate = TRUE; | |
5817 | if (zap_add(spa->spa_meta_objset, | |
5818 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
5819 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
5820 | cmn_err(CE_PANIC, "failed to add deflate"); | |
5821 | } | |
5822 | } | |
5823 | ||
5824 | /* | |
428870ff | 5825 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
5826 | * because sync-to-convergence takes longer if the blocksize |
5827 | * keeps changing. | |
5828 | */ | |
428870ff BB |
5829 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
5830 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 5831 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 5832 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
5833 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
5834 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
5835 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 5836 | } |
428870ff BB |
5837 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
5838 | spa->spa_meta_objset, obj)); | |
34dc7c2f | 5839 | |
3c67d83a TH |
5840 | /* |
5841 | * Generate some random noise for salted checksums to operate on. | |
5842 | */ | |
5843 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
5844 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
5845 | ||
34dc7c2f BB |
5846 | /* |
5847 | * Set pool properties. | |
5848 | */ | |
5849 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
5850 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
5851 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 5852 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
379ca9cf | 5853 | spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); |
1b939560 | 5854 | spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM); |
428870ff | 5855 | |
d164b209 BB |
5856 | if (props != NULL) { |
5857 | spa_configfile_set(spa, props, B_FALSE); | |
13fe0198 | 5858 | spa_sync_props(props, tx); |
d164b209 | 5859 | } |
34dc7c2f BB |
5860 | |
5861 | dmu_tx_commit(tx); | |
5862 | ||
5863 | spa->spa_sync_on = B_TRUE; | |
b5256303 | 5864 | txg_sync_start(dp); |
379ca9cf | 5865 | mmp_thread_start(spa); |
b5256303 | 5866 | txg_wait_synced(dp, txg); |
34dc7c2f | 5867 | |
9d5b5245 SD |
5868 | spa_spawn_aux_threads(spa); |
5869 | ||
a1d477c2 | 5870 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
34dc7c2f | 5871 | |
0c66c32d JG |
5872 | /* |
5873 | * Don't count references from objsets that are already closed | |
5874 | * and are making their way through the eviction process. | |
5875 | */ | |
5876 | spa_evicting_os_wait(spa); | |
424fd7c3 | 5877 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
3dfb57a3 | 5878 | spa->spa_load_state = SPA_LOAD_NONE; |
b128c09f | 5879 | |
d164b209 BB |
5880 | mutex_exit(&spa_namespace_lock); |
5881 | ||
34dc7c2f BB |
5882 | return (0); |
5883 | } | |
5884 | ||
9babb374 BB |
5885 | /* |
5886 | * Import a non-root pool into the system. | |
5887 | */ | |
5888 | int | |
13fe0198 | 5889 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
5890 | { |
5891 | spa_t *spa; | |
5892 | char *altroot = NULL; | |
428870ff | 5893 | spa_load_state_t state = SPA_LOAD_IMPORT; |
8a393be3 | 5894 | zpool_load_policy_t policy; |
572e2857 BB |
5895 | uint64_t mode = spa_mode_global; |
5896 | uint64_t readonly = B_FALSE; | |
9babb374 | 5897 | int error; |
34dc7c2f BB |
5898 | nvlist_t *nvroot; |
5899 | nvlist_t **spares, **l2cache; | |
5900 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
5901 | |
5902 | /* | |
5903 | * If a pool with this name exists, return failure. | |
5904 | */ | |
5905 | mutex_enter(&spa_namespace_lock); | |
428870ff | 5906 | if (spa_lookup(pool) != NULL) { |
9babb374 | 5907 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5908 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5909 | } |
5910 | ||
5911 | /* | |
5912 | * Create and initialize the spa structure. | |
5913 | */ | |
5914 | (void) nvlist_lookup_string(props, | |
5915 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
5916 | (void) nvlist_lookup_uint64(props, |
5917 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
5918 | if (readonly) | |
5919 | mode = FREAD; | |
428870ff | 5920 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
5921 | spa->spa_import_flags = flags; |
5922 | ||
5923 | /* | |
5924 | * Verbatim import - Take a pool and insert it into the namespace | |
5925 | * as if it had been loaded at boot. | |
5926 | */ | |
5927 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
5928 | if (props != NULL) | |
5929 | spa_configfile_set(spa, props, B_FALSE); | |
5930 | ||
a1d477c2 | 5931 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 5932 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
4a0ee12a | 5933 | zfs_dbgmsg("spa_import: verbatim import of %s", pool); |
572e2857 | 5934 | mutex_exit(&spa_namespace_lock); |
572e2857 BB |
5935 | return (0); |
5936 | } | |
5937 | ||
5938 | spa_activate(spa, mode); | |
34dc7c2f | 5939 | |
9babb374 BB |
5940 | /* |
5941 | * Don't start async tasks until we know everything is healthy. | |
5942 | */ | |
5943 | spa_async_suspend(spa); | |
b128c09f | 5944 | |
8a393be3 PZ |
5945 | zpool_get_load_policy(config, &policy); |
5946 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) | |
572e2857 BB |
5947 | state = SPA_LOAD_RECOVER; |
5948 | ||
6cb8e530 | 5949 | spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; |
572e2857 | 5950 | |
6cb8e530 PZ |
5951 | if (state != SPA_LOAD_RECOVER) { |
5952 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
5953 | zfs_dbgmsg("spa_import: importing %s", pool); | |
5954 | } else { | |
5955 | zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " | |
8a393be3 | 5956 | "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); |
6cb8e530 | 5957 | } |
8a393be3 | 5958 | error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); |
428870ff BB |
5959 | |
5960 | /* | |
572e2857 BB |
5961 | * Propagate anything learned while loading the pool and pass it |
5962 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 5963 | */ |
572e2857 BB |
5964 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
5965 | spa->spa_load_info) == 0); | |
34dc7c2f | 5966 | |
b128c09f | 5967 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5968 | /* |
9babb374 BB |
5969 | * Toss any existing sparelist, as it doesn't have any validity |
5970 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 5971 | */ |
9babb374 | 5972 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
5973 | nvlist_free(spa->spa_spares.sav_config); |
5974 | spa->spa_spares.sav_config = NULL; | |
5975 | spa_load_spares(spa); | |
5976 | } | |
9babb374 | 5977 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
5978 | nvlist_free(spa->spa_l2cache.sav_config); |
5979 | spa->spa_l2cache.sav_config = NULL; | |
5980 | spa_load_l2cache(spa); | |
5981 | } | |
5982 | ||
5983 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
5984 | &nvroot) == 0); | |
b128c09f | 5985 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 5986 | |
d164b209 BB |
5987 | if (props != NULL) |
5988 | spa_configfile_set(spa, props, B_FALSE); | |
5989 | ||
fb5f0bc8 BB |
5990 | if (error != 0 || (props && spa_writeable(spa) && |
5991 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
5992 | spa_unload(spa); |
5993 | spa_deactivate(spa); | |
5994 | spa_remove(spa); | |
34dc7c2f BB |
5995 | mutex_exit(&spa_namespace_lock); |
5996 | return (error); | |
5997 | } | |
5998 | ||
572e2857 BB |
5999 | spa_async_resume(spa); |
6000 | ||
34dc7c2f BB |
6001 | /* |
6002 | * Override any spares and level 2 cache devices as specified by | |
6003 | * the user, as these may have correct device names/devids, etc. | |
6004 | */ | |
6005 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
6006 | &spares, &nspares) == 0) { | |
6007 | if (spa->spa_spares.sav_config) | |
6008 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, | |
6009 | ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); | |
6010 | else | |
6011 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, | |
79c76d5b | 6012 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
6013 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
6014 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 6015 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6016 | spa_load_spares(spa); |
b128c09f | 6017 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6018 | spa->spa_spares.sav_sync = B_TRUE; |
6019 | } | |
6020 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
6021 | &l2cache, &nl2cache) == 0) { | |
6022 | if (spa->spa_l2cache.sav_config) | |
6023 | VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, | |
6024 | ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); | |
6025 | else | |
6026 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 6027 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
6028 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
6029 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 6030 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6031 | spa_load_l2cache(spa); |
b128c09f | 6032 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6033 | spa->spa_l2cache.sav_sync = B_TRUE; |
6034 | } | |
6035 | ||
428870ff BB |
6036 | /* |
6037 | * Check for any removed devices. | |
6038 | */ | |
6039 | if (spa->spa_autoreplace) { | |
6040 | spa_aux_check_removed(&spa->spa_spares); | |
6041 | spa_aux_check_removed(&spa->spa_l2cache); | |
6042 | } | |
6043 | ||
fb5f0bc8 | 6044 | if (spa_writeable(spa)) { |
b128c09f BB |
6045 | /* |
6046 | * Update the config cache to include the newly-imported pool. | |
6047 | */ | |
45d1cae3 | 6048 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 6049 | } |
34dc7c2f | 6050 | |
34dc7c2f | 6051 | /* |
9babb374 BB |
6052 | * It's possible that the pool was expanded while it was exported. |
6053 | * We kick off an async task to handle this for us. | |
34dc7c2f | 6054 | */ |
9babb374 | 6055 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 6056 | |
d5e024cb | 6057 | spa_history_log_version(spa, "import", NULL); |
fb390aaf | 6058 | |
12fa0466 | 6059 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
fb390aaf | 6060 | |
a0bd735a | 6061 | zvol_create_minors(spa, pool, B_TRUE); |
526af785 | 6062 | |
fb390aaf HR |
6063 | mutex_exit(&spa_namespace_lock); |
6064 | ||
b128c09f BB |
6065 | return (0); |
6066 | } | |
6067 | ||
34dc7c2f BB |
6068 | nvlist_t * |
6069 | spa_tryimport(nvlist_t *tryconfig) | |
6070 | { | |
6071 | nvlist_t *config = NULL; | |
6cb8e530 | 6072 | char *poolname, *cachefile; |
34dc7c2f BB |
6073 | spa_t *spa; |
6074 | uint64_t state; | |
d164b209 | 6075 | int error; |
8a393be3 | 6076 | zpool_load_policy_t policy; |
34dc7c2f BB |
6077 | |
6078 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
6079 | return (NULL); | |
6080 | ||
6081 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
6082 | return (NULL); | |
6083 | ||
6084 | /* | |
6085 | * Create and initialize the spa structure. | |
6086 | */ | |
6087 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6088 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
fb5f0bc8 | 6089 | spa_activate(spa, FREAD); |
34dc7c2f BB |
6090 | |
6091 | /* | |
8a393be3 | 6092 | * Rewind pool if a max txg was provided. |
34dc7c2f | 6093 | */ |
8a393be3 PZ |
6094 | zpool_get_load_policy(spa->spa_config, &policy); |
6095 | if (policy.zlp_txg != UINT64_MAX) { | |
6096 | spa->spa_load_max_txg = policy.zlp_txg; | |
6cb8e530 PZ |
6097 | spa->spa_extreme_rewind = B_TRUE; |
6098 | zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", | |
8a393be3 | 6099 | poolname, (longlong_t)policy.zlp_txg); |
6cb8e530 PZ |
6100 | } else { |
6101 | zfs_dbgmsg("spa_tryimport: importing %s", poolname); | |
6102 | } | |
6103 | ||
6104 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) | |
6105 | == 0) { | |
6106 | zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); | |
6107 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; | |
6108 | } else { | |
6109 | spa->spa_config_source = SPA_CONFIG_SRC_SCAN; | |
6110 | } | |
6111 | ||
6112 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); | |
34dc7c2f BB |
6113 | |
6114 | /* | |
6115 | * If 'tryconfig' was at least parsable, return the current config. | |
6116 | */ | |
6117 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 6118 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f BB |
6119 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, |
6120 | poolname) == 0); | |
6121 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
6122 | state) == 0); | |
6123 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
6124 | spa->spa_uberblock.ub_timestamp) == 0); | |
9ae529ec CS |
6125 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
6126 | spa->spa_load_info) == 0); | |
ffe9d382 BB |
6127 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, |
6128 | spa->spa_errata) == 0); | |
34dc7c2f BB |
6129 | |
6130 | /* | |
6131 | * If the bootfs property exists on this pool then we | |
6132 | * copy it out so that external consumers can tell which | |
6133 | * pools are bootable. | |
6134 | */ | |
d164b209 | 6135 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
79c76d5b | 6136 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6137 | |
6138 | /* | |
6139 | * We have to play games with the name since the | |
6140 | * pool was opened as TRYIMPORT_NAME. | |
6141 | */ | |
b128c09f | 6142 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
6143 | spa->spa_bootfs, tmpname) == 0) { |
6144 | char *cp; | |
d1d7e268 MK |
6145 | char *dsname; |
6146 | ||
79c76d5b | 6147 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6148 | |
6149 | cp = strchr(tmpname, '/'); | |
6150 | if (cp == NULL) { | |
6151 | (void) strlcpy(dsname, tmpname, | |
6152 | MAXPATHLEN); | |
6153 | } else { | |
6154 | (void) snprintf(dsname, MAXPATHLEN, | |
6155 | "%s/%s", poolname, ++cp); | |
6156 | } | |
6157 | VERIFY(nvlist_add_string(config, | |
6158 | ZPOOL_CONFIG_BOOTFS, dsname) == 0); | |
6159 | kmem_free(dsname, MAXPATHLEN); | |
6160 | } | |
6161 | kmem_free(tmpname, MAXPATHLEN); | |
6162 | } | |
6163 | ||
6164 | /* | |
6165 | * Add the list of hot spares and level 2 cache devices. | |
6166 | */ | |
9babb374 | 6167 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
6168 | spa_add_spares(spa, config); |
6169 | spa_add_l2cache(spa, config); | |
9babb374 | 6170 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
6171 | } |
6172 | ||
6173 | spa_unload(spa); | |
6174 | spa_deactivate(spa); | |
6175 | spa_remove(spa); | |
6176 | mutex_exit(&spa_namespace_lock); | |
6177 | ||
6178 | return (config); | |
6179 | } | |
6180 | ||
6181 | /* | |
6182 | * Pool export/destroy | |
6183 | * | |
6184 | * The act of destroying or exporting a pool is very simple. We make sure there | |
6185 | * is no more pending I/O and any references to the pool are gone. Then, we | |
6186 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
6187 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
6188 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
6189 | */ |
6190 | static int | |
b128c09f | 6191 | spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 6192 | boolean_t force, boolean_t hardforce) |
34dc7c2f BB |
6193 | { |
6194 | spa_t *spa; | |
6195 | ||
6196 | if (oldconfig) | |
6197 | *oldconfig = NULL; | |
6198 | ||
fb5f0bc8 | 6199 | if (!(spa_mode_global & FWRITE)) |
2e528b49 | 6200 | return (SET_ERROR(EROFS)); |
34dc7c2f BB |
6201 | |
6202 | mutex_enter(&spa_namespace_lock); | |
6203 | if ((spa = spa_lookup(pool)) == NULL) { | |
6204 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 6205 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
6206 | } |
6207 | ||
43a85362 SD |
6208 | if (spa->spa_is_exporting) { |
6209 | /* the pool is being exported by another thread */ | |
6210 | mutex_exit(&spa_namespace_lock); | |
6211 | return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS)); | |
6212 | } | |
6213 | spa->spa_is_exporting = B_TRUE; | |
6214 | ||
34dc7c2f BB |
6215 | /* |
6216 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
6217 | * reacquire the namespace lock, and see if we can export. | |
6218 | */ | |
6219 | spa_open_ref(spa, FTAG); | |
6220 | mutex_exit(&spa_namespace_lock); | |
6221 | spa_async_suspend(spa); | |
a0bd735a BP |
6222 | if (spa->spa_zvol_taskq) { |
6223 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
6224 | taskq_wait(spa->spa_zvol_taskq); | |
6225 | } | |
34dc7c2f BB |
6226 | mutex_enter(&spa_namespace_lock); |
6227 | spa_close(spa, FTAG); | |
6228 | ||
d14cfd83 IH |
6229 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) |
6230 | goto export_spa; | |
34dc7c2f | 6231 | /* |
d14cfd83 IH |
6232 | * The pool will be in core if it's openable, in which case we can |
6233 | * modify its state. Objsets may be open only because they're dirty, | |
6234 | * so we have to force it to sync before checking spa_refcnt. | |
34dc7c2f | 6235 | */ |
0c66c32d | 6236 | if (spa->spa_sync_on) { |
34dc7c2f | 6237 | txg_wait_synced(spa->spa_dsl_pool, 0); |
0c66c32d JG |
6238 | spa_evicting_os_wait(spa); |
6239 | } | |
34dc7c2f | 6240 | |
d14cfd83 IH |
6241 | /* |
6242 | * A pool cannot be exported or destroyed if there are active | |
6243 | * references. If we are resetting a pool, allow references by | |
6244 | * fault injection handlers. | |
6245 | */ | |
6246 | if (!spa_refcount_zero(spa) || | |
6247 | (spa->spa_inject_ref != 0 && | |
6248 | new_state != POOL_STATE_UNINITIALIZED)) { | |
6249 | spa_async_resume(spa); | |
43a85362 | 6250 | spa->spa_is_exporting = B_FALSE; |
d14cfd83 IH |
6251 | mutex_exit(&spa_namespace_lock); |
6252 | return (SET_ERROR(EBUSY)); | |
6253 | } | |
34dc7c2f | 6254 | |
d14cfd83 | 6255 | if (spa->spa_sync_on) { |
b128c09f BB |
6256 | /* |
6257 | * A pool cannot be exported if it has an active shared spare. | |
6258 | * This is to prevent other pools stealing the active spare | |
6259 | * from an exported pool. At user's own will, such pool can | |
6260 | * be forcedly exported. | |
6261 | */ | |
6262 | if (!force && new_state == POOL_STATE_EXPORTED && | |
6263 | spa_has_active_shared_spare(spa)) { | |
6264 | spa_async_resume(spa); | |
43a85362 | 6265 | spa->spa_is_exporting = B_FALSE; |
b128c09f | 6266 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 6267 | return (SET_ERROR(EXDEV)); |
b128c09f | 6268 | } |
34dc7c2f | 6269 | |
619f0976 GW |
6270 | /* |
6271 | * We're about to export or destroy this pool. Make sure | |
1b939560 BB |
6272 | * we stop all initialization and trim activity here before |
6273 | * we set the spa_final_txg. This will ensure that all | |
619f0976 GW |
6274 | * dirty data resulting from the initialization is |
6275 | * committed to disk before we unload the pool. | |
6276 | */ | |
6277 | if (spa->spa_root_vdev != NULL) { | |
1b939560 BB |
6278 | vdev_t *rvd = spa->spa_root_vdev; |
6279 | vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE); | |
6280 | vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE); | |
6281 | vdev_autotrim_stop_all(spa); | |
619f0976 GW |
6282 | } |
6283 | ||
34dc7c2f BB |
6284 | /* |
6285 | * We want this to be reflected on every label, | |
6286 | * so mark them all dirty. spa_unload() will do the | |
6287 | * final sync that pushes these changes out. | |
6288 | */ | |
fb5f0bc8 | 6289 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 6290 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6291 | spa->spa_state = new_state; |
428870ff BB |
6292 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
6293 | TXG_DEFER_SIZE + 1; | |
34dc7c2f | 6294 | vdev_config_dirty(spa->spa_root_vdev); |
b128c09f | 6295 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6296 | } |
6297 | } | |
6298 | ||
d14cfd83 | 6299 | export_spa: |
d5e024cb BB |
6300 | if (new_state == POOL_STATE_DESTROYED) |
6301 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); | |
6302 | else if (new_state == POOL_STATE_EXPORTED) | |
6303 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT); | |
34dc7c2f BB |
6304 | |
6305 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
6306 | spa_unload(spa); | |
6307 | spa_deactivate(spa); | |
6308 | } | |
6309 | ||
6310 | if (oldconfig && spa->spa_config) | |
6311 | VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); | |
6312 | ||
6313 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 | 6314 | if (!hardforce) |
a1d477c2 | 6315 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 6316 | spa_remove(spa); |
43a85362 SD |
6317 | } else { |
6318 | /* | |
6319 | * If spa_remove() is not called for this spa_t and | |
6320 | * there is any possibility that it can be reused, | |
6321 | * we make sure to reset the exporting flag. | |
6322 | */ | |
6323 | spa->spa_is_exporting = B_FALSE; | |
34dc7c2f | 6324 | } |
34dc7c2f | 6325 | |
43a85362 | 6326 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
6327 | return (0); |
6328 | } | |
6329 | ||
6330 | /* | |
6331 | * Destroy a storage pool. | |
6332 | */ | |
6333 | int | |
6334 | spa_destroy(char *pool) | |
6335 | { | |
fb5f0bc8 BB |
6336 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
6337 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
6338 | } |
6339 | ||
6340 | /* | |
6341 | * Export a storage pool. | |
6342 | */ | |
6343 | int | |
fb5f0bc8 BB |
6344 | spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, |
6345 | boolean_t hardforce) | |
34dc7c2f | 6346 | { |
fb5f0bc8 BB |
6347 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
6348 | force, hardforce)); | |
34dc7c2f BB |
6349 | } |
6350 | ||
6351 | /* | |
6352 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
6353 | * from the namespace in any way. | |
6354 | */ | |
6355 | int | |
6356 | spa_reset(char *pool) | |
6357 | { | |
b128c09f | 6358 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 6359 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
6360 | } |
6361 | ||
34dc7c2f BB |
6362 | /* |
6363 | * ========================================================================== | |
6364 | * Device manipulation | |
6365 | * ========================================================================== | |
6366 | */ | |
6367 | ||
6368 | /* | |
6369 | * Add a device to a storage pool. | |
6370 | */ | |
6371 | int | |
6372 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
6373 | { | |
93e28d66 | 6374 | uint64_t txg; |
fb5f0bc8 | 6375 | int error; |
34dc7c2f BB |
6376 | vdev_t *rvd = spa->spa_root_vdev; |
6377 | vdev_t *vd, *tvd; | |
6378 | nvlist_t **spares, **l2cache; | |
6379 | uint_t nspares, nl2cache; | |
6380 | ||
572e2857 BB |
6381 | ASSERT(spa_writeable(spa)); |
6382 | ||
34dc7c2f BB |
6383 | txg = spa_vdev_enter(spa); |
6384 | ||
6385 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
6386 | VDEV_ALLOC_ADD)) != 0) | |
6387 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6388 | ||
b128c09f | 6389 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
6390 | |
6391 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
6392 | &nspares) != 0) | |
6393 | nspares = 0; | |
6394 | ||
6395 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
6396 | &nl2cache) != 0) | |
6397 | nl2cache = 0; | |
6398 | ||
b128c09f | 6399 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 6400 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 6401 | |
b128c09f BB |
6402 | if (vd->vdev_children != 0 && |
6403 | (error = vdev_create(vd, txg, B_FALSE)) != 0) | |
6404 | return (spa_vdev_exit(spa, vd, txg, error)); | |
34dc7c2f BB |
6405 | |
6406 | /* | |
6407 | * We must validate the spares and l2cache devices after checking the | |
6408 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
6409 | */ | |
b128c09f | 6410 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 6411 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
6412 | |
6413 | /* | |
a1d477c2 MA |
6414 | * If we are in the middle of a device removal, we can only add |
6415 | * devices which match the existing devices in the pool. | |
6416 | * If we are in the middle of a removal, or have some indirect | |
6417 | * vdevs, we can not add raidz toplevels. | |
34dc7c2f | 6418 | */ |
a1d477c2 MA |
6419 | if (spa->spa_vdev_removal != NULL || |
6420 | spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { | |
6421 | for (int c = 0; c < vd->vdev_children; c++) { | |
6422 | tvd = vd->vdev_child[c]; | |
6423 | if (spa->spa_vdev_removal != NULL && | |
9e052db4 | 6424 | tvd->vdev_ashift != spa->spa_max_ashift) { |
a1d477c2 MA |
6425 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
6426 | } | |
6427 | /* Fail if top level vdev is raidz */ | |
6428 | if (tvd->vdev_ops == &vdev_raidz_ops) { | |
6429 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); | |
6430 | } | |
6431 | /* | |
6432 | * Need the top level mirror to be | |
6433 | * a mirror of leaf vdevs only | |
6434 | */ | |
6435 | if (tvd->vdev_ops == &vdev_mirror_ops) { | |
6436 | for (uint64_t cid = 0; | |
6437 | cid < tvd->vdev_children; cid++) { | |
6438 | vdev_t *cvd = tvd->vdev_child[cid]; | |
6439 | if (!cvd->vdev_ops->vdev_op_leaf) { | |
6440 | return (spa_vdev_exit(spa, vd, | |
6441 | txg, EINVAL)); | |
6442 | } | |
6443 | } | |
6444 | } | |
6445 | } | |
6446 | } | |
6447 | ||
1c27024e | 6448 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
6449 | tvd = vd->vdev_child[c]; |
6450 | vdev_remove_child(vd, tvd); | |
93e28d66 | 6451 | tvd->vdev_id = rvd->vdev_children; |
34dc7c2f BB |
6452 | vdev_add_child(rvd, tvd); |
6453 | vdev_config_dirty(tvd); | |
6454 | } | |
6455 | ||
6456 | if (nspares != 0) { | |
6457 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
6458 | ZPOOL_CONFIG_SPARES); | |
6459 | spa_load_spares(spa); | |
6460 | spa->spa_spares.sav_sync = B_TRUE; | |
6461 | } | |
6462 | ||
6463 | if (nl2cache != 0) { | |
6464 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
6465 | ZPOOL_CONFIG_L2CACHE); | |
6466 | spa_load_l2cache(spa); | |
6467 | spa->spa_l2cache.sav_sync = B_TRUE; | |
6468 | } | |
6469 | ||
6470 | /* | |
6471 | * We have to be careful when adding new vdevs to an existing pool. | |
6472 | * If other threads start allocating from these vdevs before we | |
6473 | * sync the config cache, and we lose power, then upon reboot we may | |
6474 | * fail to open the pool because there are DVAs that the config cache | |
6475 | * can't translate. Therefore, we first add the vdevs without | |
6476 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
6477 | * and then let spa_config_update() initialize the new metaslabs. | |
6478 | * | |
6479 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
6480 | * if we lose power at any point in this sequence, the remaining | |
6481 | * steps will be completed the next time we load the pool. | |
6482 | */ | |
6483 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
6484 | ||
6485 | mutex_enter(&spa_namespace_lock); | |
6486 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
12fa0466 | 6487 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); |
34dc7c2f BB |
6488 | mutex_exit(&spa_namespace_lock); |
6489 | ||
6490 | return (0); | |
6491 | } | |
6492 | ||
6493 | /* | |
6494 | * Attach a device to a mirror. The arguments are the path to any device | |
6495 | * in the mirror, and the nvroot for the new device. If the path specifies | |
6496 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
6497 | * | |
6498 | * If 'replacing' is specified, the new device is intended to replace the | |
6499 | * existing device; in this case the two devices are made into their own | |
6500 | * mirror using the 'replacing' vdev, which is functionally identical to | |
6501 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
6502 | * extra rules: you can't attach to it after it's been created, and upon | |
6503 | * completion of resilvering, the first disk (the one being replaced) | |
6504 | * is automatically detached. | |
6505 | */ | |
6506 | int | |
6507 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) | |
6508 | { | |
428870ff | 6509 | uint64_t txg, dtl_max_txg; |
1c27024e | 6510 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev); |
34dc7c2f BB |
6511 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
6512 | vdev_ops_t *pvops; | |
b128c09f BB |
6513 | char *oldvdpath, *newvdpath; |
6514 | int newvd_isspare; | |
6515 | int error; | |
34dc7c2f | 6516 | |
572e2857 BB |
6517 | ASSERT(spa_writeable(spa)); |
6518 | ||
34dc7c2f BB |
6519 | txg = spa_vdev_enter(spa); |
6520 | ||
b128c09f | 6521 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6522 | |
d2734cce SD |
6523 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6524 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6525 | error = (spa_has_checkpoint(spa)) ? | |
6526 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6527 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6528 | } | |
6529 | ||
9e052db4 | 6530 | if (spa->spa_vdev_removal != NULL) |
a1d477c2 | 6531 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
a1d477c2 | 6532 | |
34dc7c2f BB |
6533 | if (oldvd == NULL) |
6534 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6535 | ||
6536 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
6537 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6538 | ||
6539 | pvd = oldvd->vdev_parent; | |
6540 | ||
6541 | if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, | |
5ffb9d1d | 6542 | VDEV_ALLOC_ATTACH)) != 0) |
34dc7c2f BB |
6543 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
6544 | ||
6545 | if (newrootvd->vdev_children != 1) | |
6546 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6547 | ||
6548 | newvd = newrootvd->vdev_child[0]; | |
6549 | ||
6550 | if (!newvd->vdev_ops->vdev_op_leaf) | |
6551 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6552 | ||
6553 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
6554 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
6555 | ||
6556 | /* | |
6557 | * Spares can't replace logs | |
6558 | */ | |
b128c09f | 6559 | if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) |
34dc7c2f BB |
6560 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
6561 | ||
6562 | if (!replacing) { | |
6563 | /* | |
6564 | * For attach, the only allowable parent is a mirror or the root | |
6565 | * vdev. | |
6566 | */ | |
6567 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6568 | pvd->vdev_ops != &vdev_root_ops) | |
6569 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6570 | ||
6571 | pvops = &vdev_mirror_ops; | |
6572 | } else { | |
6573 | /* | |
6574 | * Active hot spares can only be replaced by inactive hot | |
6575 | * spares. | |
6576 | */ | |
6577 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 6578 | oldvd->vdev_isspare && |
34dc7c2f BB |
6579 | !spa_has_spare(spa, newvd->vdev_guid)) |
6580 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6581 | ||
6582 | /* | |
6583 | * If the source is a hot spare, and the parent isn't already a | |
6584 | * spare, then we want to create a new hot spare. Otherwise, we | |
6585 | * want to create a replacing vdev. The user is not allowed to | |
6586 | * attach to a spared vdev child unless the 'isspare' state is | |
6587 | * the same (spare replaces spare, non-spare replaces | |
6588 | * non-spare). | |
6589 | */ | |
572e2857 BB |
6590 | if (pvd->vdev_ops == &vdev_replacing_ops && |
6591 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 6592 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6593 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
6594 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 6595 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6596 | } |
6597 | ||
6598 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
6599 | pvops = &vdev_spare_ops; |
6600 | else | |
6601 | pvops = &vdev_replacing_ops; | |
6602 | } | |
6603 | ||
6604 | /* | |
9babb374 | 6605 | * Make sure the new device is big enough. |
34dc7c2f | 6606 | */ |
9babb374 | 6607 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
6608 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
6609 | ||
6610 | /* | |
6611 | * The new device cannot have a higher alignment requirement | |
6612 | * than the top-level vdev. | |
6613 | */ | |
6614 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
6615 | return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); | |
6616 | ||
6617 | /* | |
6618 | * If this is an in-place replacement, update oldvd's path and devid | |
6619 | * to make it distinguishable from newvd, and unopenable from now on. | |
6620 | */ | |
6621 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
6622 | spa_strfree(oldvd->vdev_path); | |
6623 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
79c76d5b | 6624 | KM_SLEEP); |
34dc7c2f BB |
6625 | (void) sprintf(oldvd->vdev_path, "%s/%s", |
6626 | newvd->vdev_path, "old"); | |
6627 | if (oldvd->vdev_devid != NULL) { | |
6628 | spa_strfree(oldvd->vdev_devid); | |
6629 | oldvd->vdev_devid = NULL; | |
6630 | } | |
6631 | } | |
6632 | ||
572e2857 | 6633 | /* mark the device being resilvered */ |
5d1f7fb6 | 6634 | newvd->vdev_resilver_txg = txg; |
572e2857 | 6635 | |
34dc7c2f BB |
6636 | /* |
6637 | * If the parent is not a mirror, or if we're replacing, insert the new | |
6638 | * mirror/replacing/spare vdev above oldvd. | |
6639 | */ | |
6640 | if (pvd->vdev_ops != pvops) | |
6641 | pvd = vdev_add_parent(oldvd, pvops); | |
6642 | ||
6643 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
6644 | ASSERT(pvd->vdev_ops == pvops); | |
6645 | ASSERT(oldvd->vdev_parent == pvd); | |
6646 | ||
6647 | /* | |
6648 | * Extract the new device from its root and add it to pvd. | |
6649 | */ | |
6650 | vdev_remove_child(newrootvd, newvd); | |
6651 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 6652 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
6653 | vdev_add_child(pvd, newvd); |
6654 | ||
6d82f98c IH |
6655 | /* |
6656 | * Reevaluate the parent vdev state. | |
6657 | */ | |
6658 | vdev_propagate_state(pvd); | |
6659 | ||
34dc7c2f BB |
6660 | tvd = newvd->vdev_top; |
6661 | ASSERT(pvd->vdev_top == tvd); | |
6662 | ASSERT(tvd->vdev_parent == rvd); | |
6663 | ||
6664 | vdev_config_dirty(tvd); | |
6665 | ||
6666 | /* | |
428870ff BB |
6667 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
6668 | * for any dmu_sync-ed blocks. It will propagate upward when | |
6669 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 6670 | */ |
428870ff | 6671 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 6672 | |
428870ff BB |
6673 | vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, |
6674 | dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 6675 | |
9babb374 | 6676 | if (newvd->vdev_isspare) { |
34dc7c2f | 6677 | spa_spare_activate(newvd); |
12fa0466 | 6678 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); |
9babb374 BB |
6679 | } |
6680 | ||
b128c09f BB |
6681 | oldvdpath = spa_strdup(oldvd->vdev_path); |
6682 | newvdpath = spa_strdup(newvd->vdev_path); | |
6683 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
6684 | |
6685 | /* | |
6686 | * Mark newvd's DTL dirty in this txg. | |
6687 | */ | |
6688 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
6689 | ||
428870ff | 6690 | /* |
93cf2076 GW |
6691 | * Schedule the resilver to restart in the future. We do this to |
6692 | * ensure that dmu_sync-ed blocks have been stitched into the | |
80a91e74 TC |
6693 | * respective datasets. We do not do this if resilvers have been |
6694 | * deferred. | |
428870ff | 6695 | */ |
80a91e74 TC |
6696 | if (dsl_scan_resilvering(spa_get_dsl(spa)) && |
6697 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
6698 | vdev_set_deferred_resilver(spa, newvd); | |
6699 | else | |
6700 | dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); | |
428870ff | 6701 | |
fb390aaf | 6702 | if (spa->spa_bootfs) |
12fa0466 | 6703 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); |
fb390aaf | 6704 | |
12fa0466 | 6705 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); |
fb390aaf | 6706 | |
428870ff BB |
6707 | /* |
6708 | * Commit the config | |
6709 | */ | |
6710 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 6711 | |
6f1ffb06 | 6712 | spa_history_log_internal(spa, "vdev attach", NULL, |
428870ff | 6713 | "%s vdev=%s %s vdev=%s", |
45d1cae3 BB |
6714 | replacing && newvd_isspare ? "spare in" : |
6715 | replacing ? "replace" : "attach", newvdpath, | |
6716 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
6717 | |
6718 | spa_strfree(oldvdpath); | |
6719 | spa_strfree(newvdpath); | |
6720 | ||
34dc7c2f BB |
6721 | return (0); |
6722 | } | |
6723 | ||
6724 | /* | |
6725 | * Detach a device from a mirror or replacing vdev. | |
d3cc8b15 | 6726 | * |
34dc7c2f BB |
6727 | * If 'replace_done' is specified, only detach if the parent |
6728 | * is a replacing vdev. | |
6729 | */ | |
6730 | int | |
fb5f0bc8 | 6731 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
6732 | { |
6733 | uint64_t txg; | |
fb5f0bc8 | 6734 | int error; |
1c27024e | 6735 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev); |
34dc7c2f BB |
6736 | vdev_t *vd, *pvd, *cvd, *tvd; |
6737 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 6738 | uint64_t unspare_guid = 0; |
428870ff | 6739 | char *vdpath; |
1c27024e | 6740 | |
572e2857 BB |
6741 | ASSERT(spa_writeable(spa)); |
6742 | ||
34dc7c2f BB |
6743 | txg = spa_vdev_enter(spa); |
6744 | ||
b128c09f | 6745 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6746 | |
d2734cce SD |
6747 | /* |
6748 | * Besides being called directly from the userland through the | |
6749 | * ioctl interface, spa_vdev_detach() can be potentially called | |
6750 | * at the end of spa_vdev_resilver_done(). | |
6751 | * | |
6752 | * In the regular case, when we have a checkpoint this shouldn't | |
6753 | * happen as we never empty the DTLs of a vdev during the scrub | |
6754 | * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() | |
6755 | * should never get here when we have a checkpoint. | |
6756 | * | |
6757 | * That said, even in a case when we checkpoint the pool exactly | |
6758 | * as spa_vdev_resilver_done() calls this function everything | |
6759 | * should be fine as the resilver will return right away. | |
6760 | */ | |
6761 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
6762 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6763 | error = (spa_has_checkpoint(spa)) ? | |
6764 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6765 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6766 | } | |
6767 | ||
34dc7c2f BB |
6768 | if (vd == NULL) |
6769 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6770 | ||
6771 | if (!vd->vdev_ops->vdev_op_leaf) | |
6772 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6773 | ||
6774 | pvd = vd->vdev_parent; | |
6775 | ||
fb5f0bc8 BB |
6776 | /* |
6777 | * If the parent/child relationship is not as expected, don't do it. | |
6778 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
6779 | * vdev that's replacing B with C. The user's intent in replacing | |
6780 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
6781 | * the replace by detaching C, the expected behavior is to end up | |
6782 | * M(A,B). But suppose that right after deciding to detach C, | |
6783 | * the replacement of B completes. We would have M(A,C), and then | |
6784 | * ask to detach C, which would leave us with just A -- not what | |
6785 | * the user wanted. To prevent this, we make sure that the | |
6786 | * parent/child relationship hasn't changed -- in this example, | |
6787 | * that C's parent is still the replacing vdev R. | |
6788 | */ | |
6789 | if (pvd->vdev_guid != pguid && pguid != 0) | |
6790 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
6791 | ||
34dc7c2f | 6792 | /* |
572e2857 | 6793 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 6794 | */ |
572e2857 BB |
6795 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
6796 | pvd->vdev_ops != &vdev_spare_ops) | |
6797 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
6798 | |
6799 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
6800 | spa_version(spa) >= SPA_VERSION_SPARES); | |
6801 | ||
6802 | /* | |
6803 | * Only mirror, replacing, and spare vdevs support detach. | |
6804 | */ | |
6805 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
6806 | pvd->vdev_ops != &vdev_mirror_ops && | |
6807 | pvd->vdev_ops != &vdev_spare_ops) | |
6808 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6809 | ||
6810 | /* | |
fb5f0bc8 BB |
6811 | * If this device has the only valid copy of some data, |
6812 | * we cannot safely detach it. | |
34dc7c2f | 6813 | */ |
fb5f0bc8 | 6814 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
6815 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
6816 | ||
fb5f0bc8 | 6817 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 6818 | |
b128c09f BB |
6819 | /* |
6820 | * If we are detaching the second disk from a replacing vdev, then | |
6821 | * check to see if we changed the original vdev's path to have "/old" | |
6822 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
6823 | */ | |
572e2857 BB |
6824 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
6825 | vd->vdev_path != NULL) { | |
6826 | size_t len = strlen(vd->vdev_path); | |
6827 | ||
1c27024e | 6828 | for (int c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
6829 | cvd = pvd->vdev_child[c]; |
6830 | ||
6831 | if (cvd == vd || cvd->vdev_path == NULL) | |
6832 | continue; | |
6833 | ||
6834 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
6835 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
6836 | spa_strfree(cvd->vdev_path); | |
6837 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
6838 | break; | |
6839 | } | |
b128c09f BB |
6840 | } |
6841 | } | |
6842 | ||
34dc7c2f BB |
6843 | /* |
6844 | * If we are detaching the original disk from a spare, then it implies | |
6845 | * that the spare should become a real disk, and be removed from the | |
6846 | * active spare list for the pool. | |
6847 | */ | |
6848 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 BB |
6849 | vd->vdev_id == 0 && |
6850 | pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) | |
34dc7c2f BB |
6851 | unspare = B_TRUE; |
6852 | ||
6853 | /* | |
6854 | * Erase the disk labels so the disk can be used for other things. | |
6855 | * This must be done after all other error cases are handled, | |
6856 | * but before we disembowel vd (so we can still do I/O to it). | |
6857 | * But if we can't do it, don't treat the error as fatal -- | |
6858 | * it may be that the unwritability of the disk is the reason | |
6859 | * it's being detached! | |
6860 | */ | |
6861 | error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
6862 | ||
6863 | /* | |
6864 | * Remove vd from its parent and compact the parent's children. | |
6865 | */ | |
6866 | vdev_remove_child(pvd, vd); | |
6867 | vdev_compact_children(pvd); | |
6868 | ||
6869 | /* | |
6870 | * Remember one of the remaining children so we can get tvd below. | |
6871 | */ | |
572e2857 | 6872 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
6873 | |
6874 | /* | |
6875 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
6876 | * do it now, marking the vdev as no longer a spare in the process. |
6877 | * We must do this before vdev_remove_parent(), because that can | |
6878 | * change the GUID if it creates a new toplevel GUID. For a similar | |
6879 | * reason, we must remove the spare now, in the same txg as the detach; | |
6880 | * otherwise someone could attach a new sibling, change the GUID, and | |
6881 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
6882 | */ |
6883 | if (unspare) { | |
6884 | ASSERT(cvd->vdev_isspare); | |
6885 | spa_spare_remove(cvd); | |
6886 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 6887 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 6888 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
6889 | } |
6890 | ||
428870ff BB |
6891 | /* |
6892 | * If the parent mirror/replacing vdev only has one child, | |
6893 | * the parent is no longer needed. Remove it from the tree. | |
6894 | */ | |
572e2857 BB |
6895 | if (pvd->vdev_children == 1) { |
6896 | if (pvd->vdev_ops == &vdev_spare_ops) | |
6897 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 6898 | vdev_remove_parent(cvd); |
572e2857 BB |
6899 | } |
6900 | ||
428870ff BB |
6901 | /* |
6902 | * We don't set tvd until now because the parent we just removed | |
6903 | * may have been the previous top-level vdev. | |
6904 | */ | |
6905 | tvd = cvd->vdev_top; | |
6906 | ASSERT(tvd->vdev_parent == rvd); | |
6907 | ||
6908 | /* | |
6909 | * Reevaluate the parent vdev state. | |
6910 | */ | |
6911 | vdev_propagate_state(cvd); | |
6912 | ||
6913 | /* | |
6914 | * If the 'autoexpand' property is set on the pool then automatically | |
6915 | * try to expand the size of the pool. For example if the device we | |
6916 | * just detached was smaller than the others, it may be possible to | |
6917 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
6918 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
6919 | */ | |
6920 | if (spa->spa_autoexpand) { | |
6921 | vdev_reopen(tvd); | |
6922 | vdev_expand(tvd, txg); | |
6923 | } | |
6924 | ||
6925 | vdev_config_dirty(tvd); | |
6926 | ||
6927 | /* | |
6928 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
6929 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
6930 | * But first make sure we're not on any *other* txg's DTL list, to | |
6931 | * prevent vd from being accessed after it's freed. | |
6932 | */ | |
b6ca6193 | 6933 | vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none"); |
1c27024e | 6934 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff BB |
6935 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
6936 | vd->vdev_detached = B_TRUE; | |
6937 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
6938 | ||
12fa0466 | 6939 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); |
428870ff | 6940 | |
572e2857 BB |
6941 | /* hang on to the spa before we release the lock */ |
6942 | spa_open_ref(spa, FTAG); | |
6943 | ||
428870ff BB |
6944 | error = spa_vdev_exit(spa, vd, txg, 0); |
6945 | ||
6f1ffb06 | 6946 | spa_history_log_internal(spa, "detach", NULL, |
428870ff BB |
6947 | "vdev=%s", vdpath); |
6948 | spa_strfree(vdpath); | |
6949 | ||
6950 | /* | |
6951 | * If this was the removal of the original device in a hot spare vdev, | |
6952 | * then we want to go through and remove the device from the hot spare | |
6953 | * list of every other pool. | |
6954 | */ | |
6955 | if (unspare) { | |
572e2857 BB |
6956 | spa_t *altspa = NULL; |
6957 | ||
428870ff | 6958 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
6959 | while ((altspa = spa_next(altspa)) != NULL) { |
6960 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
6961 | altspa == spa) | |
428870ff | 6962 | continue; |
572e2857 BB |
6963 | |
6964 | spa_open_ref(altspa, FTAG); | |
428870ff | 6965 | mutex_exit(&spa_namespace_lock); |
572e2857 | 6966 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 6967 | mutex_enter(&spa_namespace_lock); |
572e2857 | 6968 | spa_close(altspa, FTAG); |
428870ff BB |
6969 | } |
6970 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
6971 | |
6972 | /* search the rest of the vdevs for spares to remove */ | |
6973 | spa_vdev_resilver_done(spa); | |
428870ff BB |
6974 | } |
6975 | ||
572e2857 BB |
6976 | /* all done with the spa; OK to release */ |
6977 | mutex_enter(&spa_namespace_lock); | |
6978 | spa_close(spa, FTAG); | |
6979 | mutex_exit(&spa_namespace_lock); | |
6980 | ||
428870ff BB |
6981 | return (error); |
6982 | } | |
6983 | ||
c10d37dd GW |
6984 | static int |
6985 | spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
6986 | list_t *vd_list) | |
619f0976 | 6987 | { |
c10d37dd GW |
6988 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6989 | ||
619f0976 GW |
6990 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); |
6991 | ||
6992 | /* Look up vdev and ensure it's a leaf. */ | |
6993 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
6994 | if (vd == NULL || vd->vdev_detached) { | |
6995 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
6996 | return (SET_ERROR(ENODEV)); |
6997 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
6998 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
6999 | return (SET_ERROR(EINVAL)); |
7000 | } else if (!vdev_writeable(vd)) { | |
7001 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7002 | return (SET_ERROR(EROFS)); |
7003 | } | |
7004 | mutex_enter(&vd->vdev_initialize_lock); | |
7005 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7006 | ||
7007 | /* | |
7008 | * When we activate an initialize action we check to see | |
7009 | * if the vdev_initialize_thread is NULL. We do this instead | |
7010 | * of using the vdev_initialize_state since there might be | |
7011 | * a previous initialization process which has completed but | |
7012 | * the thread is not exited. | |
7013 | */ | |
1b939560 | 7014 | if (cmd_type == POOL_INITIALIZE_START && |
619f0976 GW |
7015 | (vd->vdev_initialize_thread != NULL || |
7016 | vd->vdev_top->vdev_removing)) { | |
7017 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7018 | return (SET_ERROR(EBUSY)); |
7019 | } else if (cmd_type == POOL_INITIALIZE_CANCEL && | |
7020 | (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && | |
7021 | vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { | |
7022 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7023 | return (SET_ERROR(ESRCH)); |
7024 | } else if (cmd_type == POOL_INITIALIZE_SUSPEND && | |
7025 | vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { | |
7026 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7027 | return (SET_ERROR(ESRCH)); |
7028 | } | |
7029 | ||
7030 | switch (cmd_type) { | |
1b939560 | 7031 | case POOL_INITIALIZE_START: |
619f0976 GW |
7032 | vdev_initialize(vd); |
7033 | break; | |
7034 | case POOL_INITIALIZE_CANCEL: | |
c10d37dd | 7035 | vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); |
619f0976 GW |
7036 | break; |
7037 | case POOL_INITIALIZE_SUSPEND: | |
c10d37dd | 7038 | vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); |
619f0976 GW |
7039 | break; |
7040 | default: | |
7041 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7042 | } | |
7043 | mutex_exit(&vd->vdev_initialize_lock); | |
7044 | ||
c10d37dd GW |
7045 | return (0); |
7046 | } | |
7047 | ||
7048 | int | |
7049 | spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, | |
7050 | nvlist_t *vdev_errlist) | |
7051 | { | |
7052 | int total_errors = 0; | |
7053 | list_t vd_list; | |
7054 | ||
7055 | list_create(&vd_list, sizeof (vdev_t), | |
7056 | offsetof(vdev_t, vdev_initialize_node)); | |
7057 | ||
7058 | /* | |
7059 | * We hold the namespace lock through the whole function | |
7060 | * to prevent any changes to the pool while we're starting or | |
7061 | * stopping initialization. The config and state locks are held so that | |
7062 | * we can properly assess the vdev state before we commit to | |
7063 | * the initializing operation. | |
7064 | */ | |
7065 | mutex_enter(&spa_namespace_lock); | |
7066 | ||
7067 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7068 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7069 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7070 | ||
7071 | int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, | |
7072 | &vd_list); | |
7073 | if (error != 0) { | |
7074 | char guid_as_str[MAXNAMELEN]; | |
7075 | ||
7076 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7077 | "%llu", (unsigned long long)vdev_guid); | |
7078 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7079 | total_errors++; | |
7080 | } | |
7081 | } | |
7082 | ||
7083 | /* Wait for all initialize threads to stop. */ | |
7084 | vdev_initialize_stop_wait(spa, &vd_list); | |
7085 | ||
619f0976 GW |
7086 | /* Sync out the initializing state */ |
7087 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7088 | mutex_exit(&spa_namespace_lock); | |
7089 | ||
c10d37dd | 7090 | list_destroy(&vd_list); |
619f0976 | 7091 | |
c10d37dd GW |
7092 | return (total_errors); |
7093 | } | |
619f0976 | 7094 | |
1b939560 BB |
7095 | static int |
7096 | spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7097 | uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list) | |
7098 | { | |
7099 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7100 | ||
7101 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
7102 | ||
7103 | /* Look up vdev and ensure it's a leaf. */ | |
7104 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7105 | if (vd == NULL || vd->vdev_detached) { | |
7106 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7107 | return (SET_ERROR(ENODEV)); | |
7108 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7109 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7110 | return (SET_ERROR(EINVAL)); | |
7111 | } else if (!vdev_writeable(vd)) { | |
7112 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7113 | return (SET_ERROR(EROFS)); | |
7114 | } else if (!vd->vdev_has_trim) { | |
7115 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7116 | return (SET_ERROR(EOPNOTSUPP)); | |
7117 | } else if (secure && !vd->vdev_has_securetrim) { | |
7118 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7119 | return (SET_ERROR(EOPNOTSUPP)); | |
7120 | } | |
7121 | mutex_enter(&vd->vdev_trim_lock); | |
7122 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7123 | ||
7124 | /* | |
7125 | * When we activate a TRIM action we check to see if the | |
7126 | * vdev_trim_thread is NULL. We do this instead of using the | |
7127 | * vdev_trim_state since there might be a previous TRIM process | |
7128 | * which has completed but the thread is not exited. | |
7129 | */ | |
7130 | if (cmd_type == POOL_TRIM_START && | |
7131 | (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing)) { | |
7132 | mutex_exit(&vd->vdev_trim_lock); | |
7133 | return (SET_ERROR(EBUSY)); | |
7134 | } else if (cmd_type == POOL_TRIM_CANCEL && | |
7135 | (vd->vdev_trim_state != VDEV_TRIM_ACTIVE && | |
7136 | vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) { | |
7137 | mutex_exit(&vd->vdev_trim_lock); | |
7138 | return (SET_ERROR(ESRCH)); | |
7139 | } else if (cmd_type == POOL_TRIM_SUSPEND && | |
7140 | vd->vdev_trim_state != VDEV_TRIM_ACTIVE) { | |
7141 | mutex_exit(&vd->vdev_trim_lock); | |
7142 | return (SET_ERROR(ESRCH)); | |
7143 | } | |
7144 | ||
7145 | switch (cmd_type) { | |
7146 | case POOL_TRIM_START: | |
7147 | vdev_trim(vd, rate, partial, secure); | |
7148 | break; | |
7149 | case POOL_TRIM_CANCEL: | |
7150 | vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list); | |
7151 | break; | |
7152 | case POOL_TRIM_SUSPEND: | |
7153 | vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list); | |
7154 | break; | |
7155 | default: | |
7156 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7157 | } | |
7158 | mutex_exit(&vd->vdev_trim_lock); | |
7159 | ||
7160 | return (0); | |
7161 | } | |
7162 | ||
7163 | /* | |
7164 | * Initiates a manual TRIM for the requested vdevs. This kicks off individual | |
7165 | * TRIM threads for each child vdev. These threads pass over all of the free | |
7166 | * space in the vdev's metaslabs and issues TRIM commands for that space. | |
7167 | */ | |
7168 | int | |
7169 | spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate, | |
7170 | boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist) | |
7171 | { | |
7172 | int total_errors = 0; | |
7173 | list_t vd_list; | |
7174 | ||
7175 | list_create(&vd_list, sizeof (vdev_t), | |
7176 | offsetof(vdev_t, vdev_trim_node)); | |
7177 | ||
7178 | /* | |
7179 | * We hold the namespace lock through the whole function | |
7180 | * to prevent any changes to the pool while we're starting or | |
7181 | * stopping TRIM. The config and state locks are held so that | |
7182 | * we can properly assess the vdev state before we commit to | |
7183 | * the TRIM operation. | |
7184 | */ | |
7185 | mutex_enter(&spa_namespace_lock); | |
7186 | ||
7187 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7188 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7189 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7190 | ||
7191 | int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type, | |
7192 | rate, partial, secure, &vd_list); | |
7193 | if (error != 0) { | |
7194 | char guid_as_str[MAXNAMELEN]; | |
7195 | ||
7196 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7197 | "%llu", (unsigned long long)vdev_guid); | |
7198 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7199 | total_errors++; | |
7200 | } | |
7201 | } | |
7202 | ||
7203 | /* Wait for all TRIM threads to stop. */ | |
7204 | vdev_trim_stop_wait(spa, &vd_list); | |
7205 | ||
7206 | /* Sync out the TRIM state */ | |
7207 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7208 | mutex_exit(&spa_namespace_lock); | |
7209 | ||
7210 | list_destroy(&vd_list); | |
7211 | ||
7212 | return (total_errors); | |
7213 | } | |
7214 | ||
428870ff BB |
7215 | /* |
7216 | * Split a set of devices from their mirrors, and create a new pool from them. | |
7217 | */ | |
7218 | int | |
7219 | spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, | |
7220 | nvlist_t *props, boolean_t exp) | |
7221 | { | |
7222 | int error = 0; | |
7223 | uint64_t txg, *glist; | |
7224 | spa_t *newspa; | |
7225 | uint_t c, children, lastlog; | |
7226 | nvlist_t **child, *nvl, *tmp; | |
7227 | dmu_tx_t *tx; | |
7228 | char *altroot = NULL; | |
7229 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
7230 | boolean_t activate_slog; | |
7231 | ||
572e2857 | 7232 | ASSERT(spa_writeable(spa)); |
428870ff BB |
7233 | |
7234 | txg = spa_vdev_enter(spa); | |
7235 | ||
d2734cce SD |
7236 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7237 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7238 | error = (spa_has_checkpoint(spa)) ? | |
7239 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7240 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7241 | } | |
7242 | ||
428870ff BB |
7243 | /* clear the log and flush everything up to now */ |
7244 | activate_slog = spa_passivate_log(spa); | |
7245 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
a1d477c2 | 7246 | error = spa_reset_logs(spa); |
428870ff BB |
7247 | txg = spa_vdev_config_enter(spa); |
7248 | ||
7249 | if (activate_slog) | |
7250 | spa_activate_log(spa); | |
7251 | ||
7252 | if (error != 0) | |
7253 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7254 | ||
7255 | /* check new spa name before going any further */ | |
7256 | if (spa_lookup(newname) != NULL) | |
7257 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
7258 | ||
7259 | /* | |
7260 | * scan through all the children to ensure they're all mirrors | |
7261 | */ | |
7262 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
7263 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
7264 | &children) != 0) | |
7265 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7266 | ||
7267 | /* first, check to ensure we've got the right child count */ | |
7268 | rvd = spa->spa_root_vdev; | |
7269 | lastlog = 0; | |
7270 | for (c = 0; c < rvd->vdev_children; c++) { | |
7271 | vdev_t *vd = rvd->vdev_child[c]; | |
7272 | ||
7273 | /* don't count the holes & logs as children */ | |
a1d477c2 | 7274 | if (vd->vdev_islog || !vdev_is_concrete(vd)) { |
428870ff BB |
7275 | if (lastlog == 0) |
7276 | lastlog = c; | |
7277 | continue; | |
7278 | } | |
7279 | ||
7280 | lastlog = 0; | |
7281 | } | |
7282 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
7283 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7284 | ||
7285 | /* next, ensure no spare or cache devices are part of the split */ | |
7286 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
7287 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
7288 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7289 | ||
79c76d5b BB |
7290 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); |
7291 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
428870ff BB |
7292 | |
7293 | /* then, loop over each vdev and validate it */ | |
7294 | for (c = 0; c < children; c++) { | |
7295 | uint64_t is_hole = 0; | |
7296 | ||
7297 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
7298 | &is_hole); | |
7299 | ||
7300 | if (is_hole != 0) { | |
7301 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
7302 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
7303 | continue; | |
7304 | } else { | |
2e528b49 | 7305 | error = SET_ERROR(EINVAL); |
428870ff BB |
7306 | break; |
7307 | } | |
7308 | } | |
7309 | ||
7310 | /* which disk is going to be split? */ | |
7311 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
7312 | &glist[c]) != 0) { | |
2e528b49 | 7313 | error = SET_ERROR(EINVAL); |
428870ff BB |
7314 | break; |
7315 | } | |
7316 | ||
7317 | /* look it up in the spa */ | |
7318 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
7319 | if (vml[c] == NULL) { | |
2e528b49 | 7320 | error = SET_ERROR(ENODEV); |
428870ff BB |
7321 | break; |
7322 | } | |
7323 | ||
7324 | /* make sure there's nothing stopping the split */ | |
7325 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
7326 | vml[c]->vdev_islog || | |
a1d477c2 | 7327 | !vdev_is_concrete(vml[c]) || |
428870ff BB |
7328 | vml[c]->vdev_isspare || |
7329 | vml[c]->vdev_isl2cache || | |
7330 | !vdev_writeable(vml[c]) || | |
7331 | vml[c]->vdev_children != 0 || | |
7332 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
7333 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
2e528b49 | 7334 | error = SET_ERROR(EINVAL); |
428870ff BB |
7335 | break; |
7336 | } | |
7337 | ||
733b5722 RS |
7338 | if (vdev_dtl_required(vml[c]) || |
7339 | vdev_resilver_needed(vml[c], NULL, NULL)) { | |
2e528b49 | 7340 | error = SET_ERROR(EBUSY); |
428870ff BB |
7341 | break; |
7342 | } | |
7343 | ||
7344 | /* we need certain info from the top level */ | |
7345 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, | |
7346 | vml[c]->vdev_top->vdev_ms_array) == 0); | |
7347 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
7348 | vml[c]->vdev_top->vdev_ms_shift) == 0); | |
7349 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
7350 | vml[c]->vdev_top->vdev_asize) == 0); | |
7351 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
7352 | vml[c]->vdev_top->vdev_ashift) == 0); | |
e0ab3ab5 JS |
7353 | |
7354 | /* transfer per-vdev ZAPs */ | |
7355 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
7356 | VERIFY0(nvlist_add_uint64(child[c], | |
7357 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
7358 | ||
7359 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
7360 | VERIFY0(nvlist_add_uint64(child[c], | |
7361 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
7362 | vml[c]->vdev_parent->vdev_top_zap)); | |
428870ff BB |
7363 | } |
7364 | ||
7365 | if (error != 0) { | |
7366 | kmem_free(vml, children * sizeof (vdev_t *)); | |
7367 | kmem_free(glist, children * sizeof (uint64_t)); | |
7368 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7369 | } | |
7370 | ||
7371 | /* stop writers from using the disks */ | |
7372 | for (c = 0; c < children; c++) { | |
7373 | if (vml[c] != NULL) | |
7374 | vml[c]->vdev_offline = B_TRUE; | |
7375 | } | |
7376 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
7377 | |
7378 | /* | |
428870ff BB |
7379 | * Temporarily record the splitting vdevs in the spa config. This |
7380 | * will disappear once the config is regenerated. | |
34dc7c2f | 7381 | */ |
79c76d5b | 7382 | VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
7383 | VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, |
7384 | glist, children) == 0); | |
7385 | kmem_free(glist, children * sizeof (uint64_t)); | |
34dc7c2f | 7386 | |
428870ff BB |
7387 | mutex_enter(&spa->spa_props_lock); |
7388 | VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, | |
7389 | nvl) == 0); | |
7390 | mutex_exit(&spa->spa_props_lock); | |
7391 | spa->spa_config_splitting = nvl; | |
7392 | vdev_config_dirty(spa->spa_root_vdev); | |
7393 | ||
7394 | /* configure and create the new pool */ | |
7395 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); | |
7396 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
7397 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); | |
7398 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
7399 | spa_version(spa)) == 0); | |
7400 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
7401 | spa->spa_config_txg) == 0); | |
7402 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
7403 | spa_generate_guid(NULL)) == 0); | |
e0ab3ab5 | 7404 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); |
428870ff BB |
7405 | (void) nvlist_lookup_string(props, |
7406 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 7407 | |
428870ff BB |
7408 | /* add the new pool to the namespace */ |
7409 | newspa = spa_add(newname, config, altroot); | |
e0ab3ab5 | 7410 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7411 | newspa->spa_config_txg = spa->spa_config_txg; |
7412 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
7413 | ||
7414 | /* release the spa config lock, retaining the namespace lock */ | |
7415 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
7416 | ||
7417 | if (zio_injection_enabled) | |
7418 | zio_handle_panic_injection(spa, FTAG, 1); | |
7419 | ||
7420 | spa_activate(newspa, spa_mode_global); | |
7421 | spa_async_suspend(newspa); | |
7422 | ||
c10d37dd | 7423 | /* |
1b939560 BB |
7424 | * Temporarily stop the initializing and TRIM activity. We set the |
7425 | * state to ACTIVE so that we know to resume initializing or TRIM | |
7426 | * once the split has completed. | |
c10d37dd | 7427 | */ |
1b939560 BB |
7428 | list_t vd_initialize_list; |
7429 | list_create(&vd_initialize_list, sizeof (vdev_t), | |
c10d37dd GW |
7430 | offsetof(vdev_t, vdev_initialize_node)); |
7431 | ||
1b939560 BB |
7432 | list_t vd_trim_list; |
7433 | list_create(&vd_trim_list, sizeof (vdev_t), | |
7434 | offsetof(vdev_t, vdev_trim_node)); | |
7435 | ||
619f0976 GW |
7436 | for (c = 0; c < children; c++) { |
7437 | if (vml[c] != NULL) { | |
619f0976 | 7438 | mutex_enter(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7439 | vdev_initialize_stop(vml[c], |
7440 | VDEV_INITIALIZE_ACTIVE, &vd_initialize_list); | |
619f0976 | 7441 | mutex_exit(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7442 | |
7443 | mutex_enter(&vml[c]->vdev_trim_lock); | |
7444 | vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list); | |
7445 | mutex_exit(&vml[c]->vdev_trim_lock); | |
619f0976 GW |
7446 | } |
7447 | } | |
1b939560 BB |
7448 | |
7449 | vdev_initialize_stop_wait(spa, &vd_initialize_list); | |
7450 | vdev_trim_stop_wait(spa, &vd_trim_list); | |
7451 | ||
7452 | list_destroy(&vd_initialize_list); | |
7453 | list_destroy(&vd_trim_list); | |
619f0976 | 7454 | |
6cb8e530 PZ |
7455 | newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; |
7456 | ||
428870ff | 7457 | /* create the new pool from the disks of the original pool */ |
6cb8e530 | 7458 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); |
428870ff BB |
7459 | if (error) |
7460 | goto out; | |
7461 | ||
7462 | /* if that worked, generate a real config for the new pool */ | |
7463 | if (newspa->spa_root_vdev != NULL) { | |
7464 | VERIFY(nvlist_alloc(&newspa->spa_config_splitting, | |
79c76d5b | 7465 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
7466 | VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, |
7467 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); | |
7468 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, | |
7469 | B_TRUE)); | |
9babb374 | 7470 | } |
34dc7c2f | 7471 | |
428870ff BB |
7472 | /* set the props */ |
7473 | if (props != NULL) { | |
7474 | spa_configfile_set(newspa, props, B_FALSE); | |
7475 | error = spa_prop_set(newspa, props); | |
7476 | if (error) | |
7477 | goto out; | |
7478 | } | |
34dc7c2f | 7479 | |
428870ff BB |
7480 | /* flush everything */ |
7481 | txg = spa_vdev_config_enter(newspa); | |
7482 | vdev_config_dirty(newspa->spa_root_vdev); | |
7483 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 7484 | |
428870ff BB |
7485 | if (zio_injection_enabled) |
7486 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 7487 | |
428870ff | 7488 | spa_async_resume(newspa); |
34dc7c2f | 7489 | |
428870ff BB |
7490 | /* finally, update the original pool's config */ |
7491 | txg = spa_vdev_config_enter(spa); | |
7492 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
7493 | error = dmu_tx_assign(tx, TXG_WAIT); | |
7494 | if (error != 0) | |
7495 | dmu_tx_abort(tx); | |
7496 | for (c = 0; c < children; c++) { | |
7497 | if (vml[c] != NULL) { | |
234234ca RS |
7498 | vdev_t *tvd = vml[c]->vdev_top; |
7499 | ||
7500 | /* | |
7501 | * Need to be sure the detachable VDEV is not | |
7502 | * on any *other* txg's DTL list to prevent it | |
7503 | * from being accessed after it's freed. | |
7504 | */ | |
7505 | for (int t = 0; t < TXG_SIZE; t++) { | |
7506 | (void) txg_list_remove_this( | |
7507 | &tvd->vdev_dtl_list, vml[c], t); | |
7508 | } | |
7509 | ||
428870ff BB |
7510 | vdev_split(vml[c]); |
7511 | if (error == 0) | |
6f1ffb06 MA |
7512 | spa_history_log_internal(spa, "detach", tx, |
7513 | "vdev=%s", vml[c]->vdev_path); | |
e0ab3ab5 | 7514 | |
428870ff | 7515 | vdev_free(vml[c]); |
34dc7c2f | 7516 | } |
34dc7c2f | 7517 | } |
e0ab3ab5 | 7518 | spa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7519 | vdev_config_dirty(spa->spa_root_vdev); |
7520 | spa->spa_config_splitting = NULL; | |
7521 | nvlist_free(nvl); | |
7522 | if (error == 0) | |
7523 | dmu_tx_commit(tx); | |
7524 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
7525 | ||
7526 | if (zio_injection_enabled) | |
7527 | zio_handle_panic_injection(spa, FTAG, 3); | |
7528 | ||
7529 | /* split is complete; log a history record */ | |
6f1ffb06 MA |
7530 | spa_history_log_internal(newspa, "split", NULL, |
7531 | "from pool %s", spa_name(spa)); | |
428870ff BB |
7532 | |
7533 | kmem_free(vml, children * sizeof (vdev_t *)); | |
7534 | ||
7535 | /* if we're not going to mount the filesystems in userland, export */ | |
7536 | if (exp) | |
7537 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
7538 | B_FALSE, B_FALSE); | |
7539 | ||
7540 | return (error); | |
7541 | ||
7542 | out: | |
7543 | spa_unload(newspa); | |
7544 | spa_deactivate(newspa); | |
7545 | spa_remove(newspa); | |
7546 | ||
7547 | txg = spa_vdev_config_enter(spa); | |
7548 | ||
7549 | /* re-online all offlined disks */ | |
7550 | for (c = 0; c < children; c++) { | |
7551 | if (vml[c] != NULL) | |
7552 | vml[c]->vdev_offline = B_FALSE; | |
7553 | } | |
619f0976 | 7554 | |
1b939560 | 7555 | /* restart initializing or trimming disks as necessary */ |
619f0976 | 7556 | spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); |
1b939560 BB |
7557 | spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); |
7558 | spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); | |
619f0976 | 7559 | |
428870ff BB |
7560 | vdev_reopen(spa->spa_root_vdev); |
7561 | ||
7562 | nvlist_free(spa->spa_config_splitting); | |
7563 | spa->spa_config_splitting = NULL; | |
7564 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 7565 | |
428870ff | 7566 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
7567 | return (error); |
7568 | } | |
7569 | ||
34dc7c2f BB |
7570 | /* |
7571 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
d3cc8b15 | 7572 | * currently spared, so we can detach it. |
34dc7c2f BB |
7573 | */ |
7574 | static vdev_t * | |
7575 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
7576 | { | |
7577 | vdev_t *newvd, *oldvd; | |
34dc7c2f | 7578 | |
1c27024e | 7579 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
7580 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
7581 | if (oldvd != NULL) | |
7582 | return (oldvd); | |
7583 | } | |
7584 | ||
7585 | /* | |
572e2857 BB |
7586 | * Check for a completed replacement. We always consider the first |
7587 | * vdev in the list to be the oldest vdev, and the last one to be | |
7588 | * the newest (see spa_vdev_attach() for how that works). In | |
7589 | * the case where the newest vdev is faulted, we will not automatically | |
7590 | * remove it after a resilver completes. This is OK as it will require | |
7591 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 7592 | */ |
572e2857 BB |
7593 | if (vd->vdev_ops == &vdev_replacing_ops) { |
7594 | ASSERT(vd->vdev_children > 1); | |
7595 | ||
7596 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 7597 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 7598 | |
fb5f0bc8 | 7599 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7600 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 7601 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7602 | return (oldvd); |
34dc7c2f BB |
7603 | } |
7604 | ||
7605 | /* | |
7606 | * Check for a completed resilver with the 'unspare' flag set. | |
f65fbee1 | 7607 | * Also potentially update faulted state. |
34dc7c2f | 7608 | */ |
572e2857 BB |
7609 | if (vd->vdev_ops == &vdev_spare_ops) { |
7610 | vdev_t *first = vd->vdev_child[0]; | |
7611 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
7612 | ||
7613 | if (last->vdev_unspare) { | |
7614 | oldvd = first; | |
7615 | newvd = last; | |
7616 | } else if (first->vdev_unspare) { | |
7617 | oldvd = last; | |
7618 | newvd = first; | |
7619 | } else { | |
7620 | oldvd = NULL; | |
7621 | } | |
34dc7c2f | 7622 | |
572e2857 | 7623 | if (oldvd != NULL && |
fb5f0bc8 | 7624 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7625 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 7626 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7627 | return (oldvd); |
572e2857 | 7628 | |
f65fbee1 JJ |
7629 | vdev_propagate_state(vd); |
7630 | ||
572e2857 BB |
7631 | /* |
7632 | * If there are more than two spares attached to a disk, | |
7633 | * and those spares are not required, then we want to | |
7634 | * attempt to free them up now so that they can be used | |
7635 | * by other pools. Once we're back down to a single | |
7636 | * disk+spare, we stop removing them. | |
7637 | */ | |
7638 | if (vd->vdev_children > 2) { | |
7639 | newvd = vd->vdev_child[1]; | |
7640 | ||
7641 | if (newvd->vdev_isspare && last->vdev_isspare && | |
7642 | vdev_dtl_empty(last, DTL_MISSING) && | |
7643 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
7644 | !vdev_dtl_required(newvd)) | |
7645 | return (newvd); | |
34dc7c2f | 7646 | } |
34dc7c2f BB |
7647 | } |
7648 | ||
7649 | return (NULL); | |
7650 | } | |
7651 | ||
7652 | static void | |
7653 | spa_vdev_resilver_done(spa_t *spa) | |
7654 | { | |
fb5f0bc8 BB |
7655 | vdev_t *vd, *pvd, *ppvd; |
7656 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 7657 | |
fb5f0bc8 | 7658 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7659 | |
7660 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
7661 | pvd = vd->vdev_parent; |
7662 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 7663 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
7664 | pguid = pvd->vdev_guid; |
7665 | ppguid = ppvd->vdev_guid; | |
7666 | sguid = 0; | |
34dc7c2f BB |
7667 | /* |
7668 | * If we have just finished replacing a hot spared device, then | |
7669 | * we need to detach the parent's first child (the original hot | |
7670 | * spare) as well. | |
7671 | */ | |
572e2857 BB |
7672 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
7673 | ppvd->vdev_children == 2) { | |
34dc7c2f | 7674 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 7675 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 7676 | } |
5d1f7fb6 GW |
7677 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); |
7678 | ||
fb5f0bc8 BB |
7679 | spa_config_exit(spa, SCL_ALL, FTAG); |
7680 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 7681 | return; |
fb5f0bc8 | 7682 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 7683 | return; |
fb5f0bc8 | 7684 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7685 | } |
7686 | ||
fb5f0bc8 | 7687 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
7688 | } |
7689 | ||
7690 | /* | |
428870ff | 7691 | * Update the stored path or FRU for this vdev. |
34dc7c2f BB |
7692 | */ |
7693 | int | |
9babb374 BB |
7694 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
7695 | boolean_t ispath) | |
34dc7c2f | 7696 | { |
b128c09f | 7697 | vdev_t *vd; |
428870ff | 7698 | boolean_t sync = B_FALSE; |
34dc7c2f | 7699 | |
572e2857 BB |
7700 | ASSERT(spa_writeable(spa)); |
7701 | ||
428870ff | 7702 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 7703 | |
9babb374 | 7704 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 7705 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
7706 | |
7707 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 7708 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 7709 | |
9babb374 | 7710 | if (ispath) { |
428870ff BB |
7711 | if (strcmp(value, vd->vdev_path) != 0) { |
7712 | spa_strfree(vd->vdev_path); | |
7713 | vd->vdev_path = spa_strdup(value); | |
7714 | sync = B_TRUE; | |
7715 | } | |
9babb374 | 7716 | } else { |
428870ff BB |
7717 | if (vd->vdev_fru == NULL) { |
7718 | vd->vdev_fru = spa_strdup(value); | |
7719 | sync = B_TRUE; | |
7720 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 7721 | spa_strfree(vd->vdev_fru); |
428870ff BB |
7722 | vd->vdev_fru = spa_strdup(value); |
7723 | sync = B_TRUE; | |
7724 | } | |
9babb374 | 7725 | } |
34dc7c2f | 7726 | |
428870ff | 7727 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
7728 | } |
7729 | ||
9babb374 BB |
7730 | int |
7731 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
7732 | { | |
7733 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
7734 | } | |
7735 | ||
7736 | int | |
7737 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
7738 | { | |
7739 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
7740 | } | |
7741 | ||
34dc7c2f BB |
7742 | /* |
7743 | * ========================================================================== | |
428870ff | 7744 | * SPA Scanning |
34dc7c2f BB |
7745 | * ========================================================================== |
7746 | */ | |
0ea05c64 AP |
7747 | int |
7748 | spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) | |
7749 | { | |
7750 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7751 | ||
7752 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
7753 | return (SET_ERROR(EBUSY)); | |
7754 | ||
7755 | return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); | |
7756 | } | |
34dc7c2f | 7757 | |
34dc7c2f | 7758 | int |
428870ff BB |
7759 | spa_scan_stop(spa_t *spa) |
7760 | { | |
7761 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7762 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
2e528b49 | 7763 | return (SET_ERROR(EBUSY)); |
428870ff BB |
7764 | return (dsl_scan_cancel(spa->spa_dsl_pool)); |
7765 | } | |
7766 | ||
7767 | int | |
7768 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 7769 | { |
b128c09f | 7770 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 7771 | |
428870ff | 7772 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
2e528b49 | 7773 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 7774 | |
fa241660 TC |
7775 | if (func == POOL_SCAN_RESILVER && |
7776 | !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
7777 | return (SET_ERROR(ENOTSUP)); | |
7778 | ||
34dc7c2f | 7779 | /* |
b128c09f BB |
7780 | * If a resilver was requested, but there is no DTL on a |
7781 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 7782 | */ |
428870ff | 7783 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
7784 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
7785 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
7786 | return (0); |
7787 | } | |
7788 | ||
428870ff | 7789 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
7790 | } |
7791 | ||
7792 | /* | |
7793 | * ========================================================================== | |
7794 | * SPA async task processing | |
7795 | * ========================================================================== | |
7796 | */ | |
7797 | ||
7798 | static void | |
7799 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
7800 | { | |
b128c09f | 7801 | if (vd->vdev_remove_wanted) { |
428870ff BB |
7802 | vd->vdev_remove_wanted = B_FALSE; |
7803 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 7804 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
7805 | |
7806 | /* | |
7807 | * We want to clear the stats, but we don't want to do a full | |
7808 | * vdev_clear() as that will cause us to throw away | |
7809 | * degraded/faulted state as well as attempt to reopen the | |
7810 | * device, all of which is a waste. | |
7811 | */ | |
7812 | vd->vdev_stat.vs_read_errors = 0; | |
7813 | vd->vdev_stat.vs_write_errors = 0; | |
7814 | vd->vdev_stat.vs_checksum_errors = 0; | |
7815 | ||
b128c09f BB |
7816 | vdev_state_dirty(vd->vdev_top); |
7817 | } | |
34dc7c2f | 7818 | |
1c27024e | 7819 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
7820 | spa_async_remove(spa, vd->vdev_child[c]); |
7821 | } | |
7822 | ||
7823 | static void | |
7824 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
7825 | { | |
7826 | if (vd->vdev_probe_wanted) { | |
428870ff | 7827 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 7828 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 7829 | } |
b128c09f | 7830 | |
1c27024e | 7831 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 7832 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
7833 | } |
7834 | ||
9babb374 BB |
7835 | static void |
7836 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
7837 | { | |
9babb374 BB |
7838 | if (!spa->spa_autoexpand) |
7839 | return; | |
7840 | ||
1c27024e | 7841 | for (int c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
7842 | vdev_t *cvd = vd->vdev_child[c]; |
7843 | spa_async_autoexpand(spa, cvd); | |
7844 | } | |
7845 | ||
7846 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
7847 | return; | |
7848 | ||
12fa0466 | 7849 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND); |
9babb374 BB |
7850 | } |
7851 | ||
34dc7c2f | 7852 | static void |
c25b8f99 | 7853 | spa_async_thread(void *arg) |
34dc7c2f | 7854 | { |
c25b8f99 | 7855 | spa_t *spa = (spa_t *)arg; |
80a91e74 | 7856 | dsl_pool_t *dp = spa->spa_dsl_pool; |
867959b5 | 7857 | int tasks; |
34dc7c2f BB |
7858 | |
7859 | ASSERT(spa->spa_sync_on); | |
7860 | ||
7861 | mutex_enter(&spa->spa_async_lock); | |
7862 | tasks = spa->spa_async_tasks; | |
7863 | spa->spa_async_tasks = 0; | |
7864 | mutex_exit(&spa->spa_async_lock); | |
7865 | ||
7866 | /* | |
7867 | * See if the config needs to be updated. | |
7868 | */ | |
7869 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 7870 | uint64_t old_space, new_space; |
9babb374 | 7871 | |
34dc7c2f | 7872 | mutex_enter(&spa_namespace_lock); |
428870ff | 7873 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7874 | old_space += metaslab_class_get_space(spa_special_class(spa)); |
7875 | old_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
7876 | ||
34dc7c2f | 7877 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
cc99f275 | 7878 | |
428870ff | 7879 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7880 | new_space += metaslab_class_get_space(spa_special_class(spa)); |
7881 | new_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
34dc7c2f | 7882 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
7883 | |
7884 | /* | |
7885 | * If the pool grew as a result of the config update, | |
7886 | * then log an internal history event. | |
7887 | */ | |
428870ff | 7888 | if (new_space != old_space) { |
6f1ffb06 | 7889 | spa_history_log_internal(spa, "vdev online", NULL, |
45d1cae3 | 7890 | "pool '%s' size: %llu(+%llu)", |
428870ff | 7891 | spa_name(spa), new_space, new_space - old_space); |
9babb374 | 7892 | } |
34dc7c2f BB |
7893 | } |
7894 | ||
7895 | /* | |
7896 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 7897 | */ |
b128c09f | 7898 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 7899 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 7900 | spa_async_remove(spa, spa->spa_root_vdev); |
867959b5 | 7901 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 7902 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
867959b5 | 7903 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
7904 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
7905 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
7906 | } |
7907 | ||
9babb374 BB |
7908 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
7909 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7910 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
7911 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7912 | } | |
7913 | ||
34dc7c2f | 7914 | /* |
b128c09f | 7915 | * See if any devices need to be probed. |
34dc7c2f | 7916 | */ |
b128c09f | 7917 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 7918 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
7919 | spa_async_probe(spa, spa->spa_root_vdev); |
7920 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
7921 | } | |
34dc7c2f BB |
7922 | |
7923 | /* | |
b128c09f | 7924 | * If any devices are done replacing, detach them. |
34dc7c2f | 7925 | */ |
b128c09f BB |
7926 | if (tasks & SPA_ASYNC_RESILVER_DONE) |
7927 | spa_vdev_resilver_done(spa); | |
34dc7c2f BB |
7928 | |
7929 | /* | |
7930 | * Kick off a resilver. | |
7931 | */ | |
80a91e74 TC |
7932 | if (tasks & SPA_ASYNC_RESILVER && |
7933 | (!dsl_scan_resilvering(dp) || | |
7934 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) | |
7935 | dsl_resilver_restart(dp, 0); | |
34dc7c2f | 7936 | |
619f0976 GW |
7937 | if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { |
7938 | mutex_enter(&spa_namespace_lock); | |
7939 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7940 | vdev_initialize_restart(spa->spa_root_vdev); | |
7941 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7942 | mutex_exit(&spa_namespace_lock); | |
7943 | } | |
7944 | ||
1b939560 BB |
7945 | if (tasks & SPA_ASYNC_TRIM_RESTART) { |
7946 | mutex_enter(&spa_namespace_lock); | |
7947 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7948 | vdev_trim_restart(spa->spa_root_vdev); | |
7949 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7950 | mutex_exit(&spa_namespace_lock); | |
7951 | } | |
7952 | ||
7953 | if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) { | |
7954 | mutex_enter(&spa_namespace_lock); | |
7955 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7956 | vdev_autotrim_restart(spa); | |
7957 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7958 | mutex_exit(&spa_namespace_lock); | |
7959 | } | |
7960 | ||
34dc7c2f BB |
7961 | /* |
7962 | * Let the world know that we're done. | |
7963 | */ | |
7964 | mutex_enter(&spa->spa_async_lock); | |
7965 | spa->spa_async_thread = NULL; | |
7966 | cv_broadcast(&spa->spa_async_cv); | |
7967 | mutex_exit(&spa->spa_async_lock); | |
7968 | thread_exit(); | |
7969 | } | |
7970 | ||
7971 | void | |
7972 | spa_async_suspend(spa_t *spa) | |
7973 | { | |
7974 | mutex_enter(&spa->spa_async_lock); | |
7975 | spa->spa_async_suspended++; | |
9d5b5245 | 7976 | while (spa->spa_async_thread != NULL) |
34dc7c2f BB |
7977 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); |
7978 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 MA |
7979 | |
7980 | spa_vdev_remove_suspend(spa); | |
9d5b5245 SD |
7981 | |
7982 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a SD |
7983 | if (condense_thread != NULL) |
7984 | zthr_cancel(condense_thread); | |
d2734cce SD |
7985 | |
7986 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a SD |
7987 | if (discard_thread != NULL) |
7988 | zthr_cancel(discard_thread); | |
37f03da8 SH |
7989 | |
7990 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
7991 | if (ll_delete_thread != NULL) | |
7992 | zthr_cancel(ll_delete_thread); | |
7993 | ||
7994 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
7995 | if (ll_condense_thread != NULL) | |
7996 | zthr_cancel(ll_condense_thread); | |
34dc7c2f BB |
7997 | } |
7998 | ||
7999 | void | |
8000 | spa_async_resume(spa_t *spa) | |
8001 | { | |
8002 | mutex_enter(&spa->spa_async_lock); | |
8003 | ASSERT(spa->spa_async_suspended != 0); | |
8004 | spa->spa_async_suspended--; | |
8005 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 | 8006 | spa_restart_removal(spa); |
9d5b5245 SD |
8007 | |
8008 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a | 8009 | if (condense_thread != NULL) |
9d5b5245 | 8010 | zthr_resume(condense_thread); |
d2734cce SD |
8011 | |
8012 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a | 8013 | if (discard_thread != NULL) |
d2734cce | 8014 | zthr_resume(discard_thread); |
37f03da8 SH |
8015 | |
8016 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8017 | if (ll_delete_thread != NULL) | |
8018 | zthr_resume(ll_delete_thread); | |
8019 | ||
8020 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8021 | if (ll_condense_thread != NULL) | |
8022 | zthr_resume(ll_condense_thread); | |
34dc7c2f BB |
8023 | } |
8024 | ||
e6cfd633 WA |
8025 | static boolean_t |
8026 | spa_async_tasks_pending(spa_t *spa) | |
8027 | { | |
8028 | uint_t non_config_tasks; | |
8029 | uint_t config_task; | |
8030 | boolean_t config_task_suspended; | |
8031 | ||
8032 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
8033 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
8034 | if (spa->spa_ccw_fail_time == 0) { | |
8035 | config_task_suspended = B_FALSE; | |
8036 | } else { | |
8037 | config_task_suspended = | |
8038 | (gethrtime() - spa->spa_ccw_fail_time) < | |
05852b34 | 8039 | ((hrtime_t)zfs_ccw_retry_interval * NANOSEC); |
e6cfd633 WA |
8040 | } |
8041 | ||
8042 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
8043 | } | |
8044 | ||
34dc7c2f BB |
8045 | static void |
8046 | spa_async_dispatch(spa_t *spa) | |
8047 | { | |
8048 | mutex_enter(&spa->spa_async_lock); | |
e6cfd633 WA |
8049 | if (spa_async_tasks_pending(spa) && |
8050 | !spa->spa_async_suspended && | |
34dc7c2f | 8051 | spa->spa_async_thread == NULL && |
e6cfd633 | 8052 | rootdir != NULL) |
34dc7c2f BB |
8053 | spa->spa_async_thread = thread_create(NULL, 0, |
8054 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
8055 | mutex_exit(&spa->spa_async_lock); | |
8056 | } | |
8057 | ||
8058 | void | |
8059 | spa_async_request(spa_t *spa, int task) | |
8060 | { | |
428870ff | 8061 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
8062 | mutex_enter(&spa->spa_async_lock); |
8063 | spa->spa_async_tasks |= task; | |
8064 | mutex_exit(&spa->spa_async_lock); | |
8065 | } | |
8066 | ||
8067 | /* | |
8068 | * ========================================================================== | |
8069 | * SPA syncing routines | |
8070 | * ========================================================================== | |
8071 | */ | |
8072 | ||
37f03da8 | 8073 | |
428870ff | 8074 | static int |
37f03da8 SH |
8075 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, |
8076 | dmu_tx_t *tx) | |
34dc7c2f | 8077 | { |
428870ff | 8078 | bpobj_t *bpo = arg; |
37f03da8 | 8079 | bpobj_enqueue(bpo, bp, bp_freed, tx); |
428870ff BB |
8080 | return (0); |
8081 | } | |
34dc7c2f | 8082 | |
37f03da8 SH |
8083 | int |
8084 | bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8085 | { | |
8086 | return (bpobj_enqueue_cb(arg, bp, B_FALSE, tx)); | |
8087 | } | |
8088 | ||
8089 | int | |
8090 | bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8091 | { | |
8092 | return (bpobj_enqueue_cb(arg, bp, B_TRUE, tx)); | |
8093 | } | |
8094 | ||
428870ff BB |
8095 | static int |
8096 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8097 | { | |
8098 | zio_t *zio = arg; | |
34dc7c2f | 8099 | |
428870ff BB |
8100 | zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, |
8101 | zio->io_flags)); | |
8102 | return (0); | |
34dc7c2f BB |
8103 | } |
8104 | ||
37f03da8 SH |
8105 | static int |
8106 | bpobj_spa_free_sync_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
8107 | dmu_tx_t *tx) | |
8108 | { | |
8109 | ASSERT(!bp_freed); | |
8110 | return (spa_free_sync_cb(arg, bp, tx)); | |
8111 | } | |
8112 | ||
e8b96c60 MA |
8113 | /* |
8114 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8115 | * amount of time spent syncing frees. | |
8116 | */ | |
8117 | static void | |
8118 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
8119 | { | |
8120 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
8121 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
8122 | VERIFY(zio_wait(zio) == 0); | |
8123 | } | |
8124 | ||
8125 | /* | |
8126 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8127 | * amount of time spent syncing deferred frees. | |
8128 | */ | |
8129 | static void | |
8130 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
8131 | { | |
8dc2197b SD |
8132 | if (spa_sync_pass(spa) != 1) |
8133 | return; | |
8134 | ||
93e28d66 SD |
8135 | /* |
8136 | * Note: | |
8137 | * If the log space map feature is active, we stop deferring | |
8138 | * frees to the next TXG and therefore running this function | |
8139 | * would be considered a no-op as spa_deferred_bpobj should | |
8140 | * not have any entries. | |
8141 | * | |
8142 | * That said we run this function anyway (instead of returning | |
8143 | * immediately) for the edge-case scenario where we just | |
8144 | * activated the log space map feature in this TXG but we have | |
8145 | * deferred frees from the previous TXG. | |
8146 | */ | |
e8b96c60 MA |
8147 | zio_t *zio = zio_root(spa, NULL, NULL, 0); |
8148 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
37f03da8 | 8149 | bpobj_spa_free_sync_cb, zio, tx), ==, 0); |
e8b96c60 MA |
8150 | VERIFY0(zio_wait(zio)); |
8151 | } | |
8152 | ||
34dc7c2f BB |
8153 | static void |
8154 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
8155 | { | |
8156 | char *packed = NULL; | |
b128c09f | 8157 | size_t bufsize; |
34dc7c2f BB |
8158 | size_t nvsize = 0; |
8159 | dmu_buf_t *db; | |
8160 | ||
8161 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
8162 | ||
b128c09f BB |
8163 | /* |
8164 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
b0bc7a84 | 8165 | * information. This avoids the dmu_buf_will_dirty() path and |
b128c09f BB |
8166 | * saves us a pre-read to get data we don't actually care about. |
8167 | */ | |
9ae529ec | 8168 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
79c76d5b | 8169 | packed = vmem_alloc(bufsize, KM_SLEEP); |
34dc7c2f BB |
8170 | |
8171 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
79c76d5b | 8172 | KM_SLEEP) == 0); |
b128c09f | 8173 | bzero(packed + nvsize, bufsize - nvsize); |
34dc7c2f | 8174 | |
b128c09f | 8175 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 8176 | |
00b46022 | 8177 | vmem_free(packed, bufsize); |
34dc7c2f BB |
8178 | |
8179 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
8180 | dmu_buf_will_dirty(db, tx); | |
8181 | *(uint64_t *)db->db_data = nvsize; | |
8182 | dmu_buf_rele(db, FTAG); | |
8183 | } | |
8184 | ||
8185 | static void | |
8186 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
8187 | const char *config, const char *entry) | |
8188 | { | |
8189 | nvlist_t *nvroot; | |
8190 | nvlist_t **list; | |
8191 | int i; | |
8192 | ||
8193 | if (!sav->sav_sync) | |
8194 | return; | |
8195 | ||
8196 | /* | |
8197 | * Update the MOS nvlist describing the list of available devices. | |
8198 | * spa_validate_aux() will have already made sure this nvlist is | |
8199 | * valid and the vdevs are labeled appropriately. | |
8200 | */ | |
8201 | if (sav->sav_object == 0) { | |
8202 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
8203 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
8204 | sizeof (uint64_t), tx); | |
8205 | VERIFY(zap_update(spa->spa_meta_objset, | |
8206 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
8207 | &sav->sav_object, tx) == 0); | |
8208 | } | |
8209 | ||
79c76d5b | 8210 | VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
8211 | if (sav->sav_count == 0) { |
8212 | VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); | |
8213 | } else { | |
79c76d5b | 8214 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); |
34dc7c2f BB |
8215 | for (i = 0; i < sav->sav_count; i++) |
8216 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 8217 | B_FALSE, VDEV_CONFIG_L2CACHE); |
34dc7c2f BB |
8218 | VERIFY(nvlist_add_nvlist_array(nvroot, config, list, |
8219 | sav->sav_count) == 0); | |
8220 | for (i = 0; i < sav->sav_count; i++) | |
8221 | nvlist_free(list[i]); | |
8222 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
8223 | } | |
8224 | ||
8225 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
8226 | nvlist_free(nvroot); | |
8227 | ||
8228 | sav->sav_sync = B_FALSE; | |
8229 | } | |
8230 | ||
e0ab3ab5 JS |
8231 | /* |
8232 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
8233 | * The all-vdev ZAP must be empty. | |
8234 | */ | |
8235 | static void | |
8236 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
8237 | { | |
8238 | spa_t *spa = vd->vdev_spa; | |
e0ab3ab5 JS |
8239 | |
8240 | if (vd->vdev_top_zap != 0) { | |
8241 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8242 | vd->vdev_top_zap, tx)); | |
8243 | } | |
8244 | if (vd->vdev_leaf_zap != 0) { | |
8245 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8246 | vd->vdev_leaf_zap, tx)); | |
8247 | } | |
1c27024e | 8248 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
8249 | spa_avz_build(vd->vdev_child[i], avz, tx); |
8250 | } | |
8251 | } | |
8252 | ||
34dc7c2f BB |
8253 | static void |
8254 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
8255 | { | |
8256 | nvlist_t *config; | |
8257 | ||
e0ab3ab5 JS |
8258 | /* |
8259 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
8260 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
8261 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
8262 | * need to rebuild the AVZ although the config may not be dirty. | |
8263 | */ | |
8264 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
8265 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
34dc7c2f BB |
8266 | return; |
8267 | ||
b128c09f BB |
8268 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
8269 | ||
e0ab3ab5 | 8270 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || |
38640550 | 8271 | spa->spa_avz_action == AVZ_ACTION_INITIALIZE || |
e0ab3ab5 JS |
8272 | spa->spa_all_vdev_zaps != 0); |
8273 | ||
8274 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
e0ab3ab5 JS |
8275 | /* Make and build the new AVZ */ |
8276 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
8277 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
8278 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
8279 | ||
8280 | /* Diff old AVZ with new one */ | |
1c27024e DB |
8281 | zap_cursor_t zc; |
8282 | zap_attribute_t za; | |
8283 | ||
e0ab3ab5 JS |
8284 | for (zap_cursor_init(&zc, spa->spa_meta_objset, |
8285 | spa->spa_all_vdev_zaps); | |
8286 | zap_cursor_retrieve(&zc, &za) == 0; | |
8287 | zap_cursor_advance(&zc)) { | |
8288 | uint64_t vdzap = za.za_first_integer; | |
8289 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
8290 | vdzap) == ENOENT) { | |
8291 | /* | |
8292 | * ZAP is listed in old AVZ but not in new one; | |
8293 | * destroy it | |
8294 | */ | |
8295 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
8296 | tx)); | |
8297 | } | |
8298 | } | |
8299 | ||
8300 | zap_cursor_fini(&zc); | |
8301 | ||
8302 | /* Destroy the old AVZ */ | |
8303 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8304 | spa->spa_all_vdev_zaps, tx)); | |
8305 | ||
8306 | /* Replace the old AVZ in the dir obj with the new one */ | |
8307 | VERIFY0(zap_update(spa->spa_meta_objset, | |
8308 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
8309 | sizeof (new_avz), 1, &new_avz, tx)); | |
8310 | ||
8311 | spa->spa_all_vdev_zaps = new_avz; | |
8312 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
8313 | zap_cursor_t zc; | |
8314 | zap_attribute_t za; | |
8315 | ||
8316 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
8317 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
8318 | spa->spa_all_vdev_zaps); | |
8319 | zap_cursor_retrieve(&zc, &za) == 0; | |
8320 | zap_cursor_advance(&zc)) { | |
8321 | uint64_t zap = za.za_first_integer; | |
8322 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
8323 | } | |
8324 | ||
8325 | zap_cursor_fini(&zc); | |
8326 | ||
8327 | /* Destroy and unlink the AVZ itself */ | |
8328 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8329 | spa->spa_all_vdev_zaps, tx)); | |
8330 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
8331 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
8332 | spa->spa_all_vdev_zaps = 0; | |
8333 | } | |
8334 | ||
8335 | if (spa->spa_all_vdev_zaps == 0) { | |
8336 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
8337 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
8338 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
8339 | } | |
8340 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
8341 | ||
8342 | /* Create ZAPs for vdevs that don't have them. */ | |
8343 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
8344 | ||
b128c09f BB |
8345 | config = spa_config_generate(spa, spa->spa_root_vdev, |
8346 | dmu_tx_get_txg(tx), B_FALSE); | |
8347 | ||
ea0b2538 GW |
8348 | /* |
8349 | * If we're upgrading the spa version then make sure that | |
8350 | * the config object gets updated with the correct version. | |
8351 | */ | |
8352 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
8353 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
8354 | spa->spa_uberblock.ub_version); | |
8355 | ||
b128c09f | 8356 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f | 8357 | |
8a5fc748 | 8358 | nvlist_free(spa->spa_config_syncing); |
34dc7c2f BB |
8359 | spa->spa_config_syncing = config; |
8360 | ||
8361 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
8362 | } | |
8363 | ||
9ae529ec | 8364 | static void |
13fe0198 | 8365 | spa_sync_version(void *arg, dmu_tx_t *tx) |
9ae529ec | 8366 | { |
13fe0198 MA |
8367 | uint64_t *versionp = arg; |
8368 | uint64_t version = *versionp; | |
8369 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
9ae529ec CS |
8370 | |
8371 | /* | |
8372 | * Setting the version is special cased when first creating the pool. | |
8373 | */ | |
8374 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
8375 | ||
8dca0a9a | 8376 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
8377 | ASSERT(version >= spa_version(spa)); |
8378 | ||
8379 | spa->spa_uberblock.ub_version = version; | |
8380 | vdev_config_dirty(spa->spa_root_vdev); | |
6f1ffb06 | 8381 | spa_history_log_internal(spa, "set", tx, "version=%lld", version); |
9ae529ec CS |
8382 | } |
8383 | ||
34dc7c2f BB |
8384 | /* |
8385 | * Set zpool properties. | |
8386 | */ | |
8387 | static void | |
13fe0198 | 8388 | spa_sync_props(void *arg, dmu_tx_t *tx) |
34dc7c2f | 8389 | { |
13fe0198 MA |
8390 | nvlist_t *nvp = arg; |
8391 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f | 8392 | objset_t *mos = spa->spa_meta_objset; |
9ae529ec | 8393 | nvpair_t *elem = NULL; |
b128c09f BB |
8394 | |
8395 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 8396 | |
34dc7c2f | 8397 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec CS |
8398 | uint64_t intval; |
8399 | char *strval, *fname; | |
8400 | zpool_prop_t prop; | |
8401 | const char *propname; | |
8402 | zprop_type_t proptype; | |
fa86b5db | 8403 | spa_feature_t fid; |
9ae529ec | 8404 | |
31864e3d BB |
8405 | switch (prop = zpool_name_to_prop(nvpair_name(elem))) { |
8406 | case ZPOOL_PROP_INVAL: | |
9ae529ec CS |
8407 | /* |
8408 | * We checked this earlier in spa_prop_validate(). | |
8409 | */ | |
8410 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
8411 | ||
8412 | fname = strchr(nvpair_name(elem), '@') + 1; | |
fa86b5db | 8413 | VERIFY0(zfeature_lookup_name(fname, &fid)); |
9ae529ec | 8414 | |
fa86b5db | 8415 | spa_feature_enable(spa, fid, tx); |
6f1ffb06 MA |
8416 | spa_history_log_internal(spa, "set", tx, |
8417 | "%s=enabled", nvpair_name(elem)); | |
9ae529ec CS |
8418 | break; |
8419 | ||
34dc7c2f | 8420 | case ZPOOL_PROP_VERSION: |
93cf2076 | 8421 | intval = fnvpair_value_uint64(elem); |
34dc7c2f | 8422 | /* |
4e33ba4c | 8423 | * The version is synced separately before other |
9ae529ec | 8424 | * properties and should be correct by now. |
34dc7c2f | 8425 | */ |
9ae529ec | 8426 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
8427 | break; |
8428 | ||
8429 | case ZPOOL_PROP_ALTROOT: | |
8430 | /* | |
8431 | * 'altroot' is a non-persistent property. It should | |
8432 | * have been set temporarily at creation or import time. | |
8433 | */ | |
8434 | ASSERT(spa->spa_root != NULL); | |
8435 | break; | |
8436 | ||
572e2857 | 8437 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
8438 | case ZPOOL_PROP_CACHEFILE: |
8439 | /* | |
572e2857 BB |
8440 | * 'readonly' and 'cachefile' are also non-persisitent |
8441 | * properties. | |
34dc7c2f | 8442 | */ |
34dc7c2f | 8443 | break; |
d96eb2b1 | 8444 | case ZPOOL_PROP_COMMENT: |
93cf2076 | 8445 | strval = fnvpair_value_string(elem); |
d96eb2b1 DM |
8446 | if (spa->spa_comment != NULL) |
8447 | spa_strfree(spa->spa_comment); | |
8448 | spa->spa_comment = spa_strdup(strval); | |
8449 | /* | |
8450 | * We need to dirty the configuration on all the vdevs | |
8451 | * so that their labels get updated. It's unnecessary | |
8452 | * to do this for pool creation since the vdev's | |
4e33ba4c | 8453 | * configuration has already been dirtied. |
d96eb2b1 DM |
8454 | */ |
8455 | if (tx->tx_txg != TXG_INITIAL) | |
8456 | vdev_config_dirty(spa->spa_root_vdev); | |
6f1ffb06 MA |
8457 | spa_history_log_internal(spa, "set", tx, |
8458 | "%s=%s", nvpair_name(elem), strval); | |
d96eb2b1 | 8459 | break; |
34dc7c2f BB |
8460 | default: |
8461 | /* | |
8462 | * Set pool property values in the poolprops mos object. | |
8463 | */ | |
34dc7c2f | 8464 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
8465 | spa->spa_pool_props_object = |
8466 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 8467 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 8468 | tx); |
34dc7c2f | 8469 | } |
34dc7c2f BB |
8470 | |
8471 | /* normalize the property name */ | |
8472 | propname = zpool_prop_to_name(prop); | |
8473 | proptype = zpool_prop_get_type(prop); | |
8474 | ||
8475 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
8476 | ASSERT(proptype == PROP_TYPE_STRING); | |
93cf2076 GW |
8477 | strval = fnvpair_value_string(elem); |
8478 | VERIFY0(zap_update(mos, | |
34dc7c2f | 8479 | spa->spa_pool_props_object, propname, |
93cf2076 | 8480 | 1, strlen(strval) + 1, strval, tx)); |
6f1ffb06 MA |
8481 | spa_history_log_internal(spa, "set", tx, |
8482 | "%s=%s", nvpair_name(elem), strval); | |
34dc7c2f | 8483 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { |
93cf2076 | 8484 | intval = fnvpair_value_uint64(elem); |
34dc7c2f BB |
8485 | |
8486 | if (proptype == PROP_TYPE_INDEX) { | |
8487 | const char *unused; | |
93cf2076 GW |
8488 | VERIFY0(zpool_prop_index_to_string( |
8489 | prop, intval, &unused)); | |
34dc7c2f | 8490 | } |
93cf2076 | 8491 | VERIFY0(zap_update(mos, |
34dc7c2f | 8492 | spa->spa_pool_props_object, propname, |
93cf2076 | 8493 | 8, 1, &intval, tx)); |
6f1ffb06 MA |
8494 | spa_history_log_internal(spa, "set", tx, |
8495 | "%s=%lld", nvpair_name(elem), intval); | |
34dc7c2f BB |
8496 | } else { |
8497 | ASSERT(0); /* not allowed */ | |
8498 | } | |
8499 | ||
8500 | switch (prop) { | |
8501 | case ZPOOL_PROP_DELEGATION: | |
8502 | spa->spa_delegation = intval; | |
8503 | break; | |
8504 | case ZPOOL_PROP_BOOTFS: | |
8505 | spa->spa_bootfs = intval; | |
8506 | break; | |
8507 | case ZPOOL_PROP_FAILUREMODE: | |
8508 | spa->spa_failmode = intval; | |
8509 | break; | |
1b939560 BB |
8510 | case ZPOOL_PROP_AUTOTRIM: |
8511 | spa->spa_autotrim = intval; | |
8512 | spa_async_request(spa, | |
8513 | SPA_ASYNC_AUTOTRIM_RESTART); | |
8514 | break; | |
9babb374 BB |
8515 | case ZPOOL_PROP_AUTOEXPAND: |
8516 | spa->spa_autoexpand = intval; | |
428870ff BB |
8517 | if (tx->tx_txg != TXG_INITIAL) |
8518 | spa_async_request(spa, | |
8519 | SPA_ASYNC_AUTOEXPAND); | |
8520 | break; | |
379ca9cf OF |
8521 | case ZPOOL_PROP_MULTIHOST: |
8522 | spa->spa_multihost = intval; | |
8523 | break; | |
34dc7c2f BB |
8524 | default: |
8525 | break; | |
8526 | } | |
8527 | } | |
8528 | ||
34dc7c2f | 8529 | } |
b128c09f BB |
8530 | |
8531 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
8532 | } |
8533 | ||
428870ff BB |
8534 | /* |
8535 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
8536 | * reflect the new version this txg, so there must be no changes this | |
8537 | * txg to anything that the upgrade code depends on after it executes. | |
8538 | * Therefore this must be called after dsl_pool_sync() does the sync | |
8539 | * tasks. | |
8540 | */ | |
8541 | static void | |
8542 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
8543 | { | |
8dc2197b SD |
8544 | if (spa_sync_pass(spa) != 1) |
8545 | return; | |
428870ff | 8546 | |
8dc2197b | 8547 | dsl_pool_t *dp = spa->spa_dsl_pool; |
13fe0198 MA |
8548 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
8549 | ||
428870ff BB |
8550 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && |
8551 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
8552 | dsl_pool_create_origin(dp, tx); | |
8553 | ||
8554 | /* Keeping the origin open increases spa_minref */ | |
8555 | spa->spa_minref += 3; | |
8556 | } | |
8557 | ||
8558 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
8559 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
8560 | dsl_pool_upgrade_clones(dp, tx); | |
8561 | } | |
8562 | ||
8563 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
8564 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
8565 | dsl_pool_upgrade_dir_clones(dp, tx); | |
8566 | ||
8567 | /* Keeping the freedir open increases spa_minref */ | |
8568 | spa->spa_minref += 3; | |
8569 | } | |
9ae529ec CS |
8570 | |
8571 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
8572 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8573 | spa_feature_create_zap_objects(spa, tx); | |
8574 | } | |
62bdd5eb DL |
8575 | |
8576 | /* | |
8577 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
8578 | * when possibility to use lz4 compression for metadata was added | |
8579 | * Old pools that have this feature enabled must be upgraded to have | |
8580 | * this feature active | |
8581 | */ | |
8582 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8583 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
8584 | SPA_FEATURE_LZ4_COMPRESS); | |
8585 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
8586 | SPA_FEATURE_LZ4_COMPRESS); | |
8587 | ||
8588 | if (lz4_en && !lz4_ac) | |
8589 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
8590 | } | |
3c67d83a TH |
8591 | |
8592 | /* | |
8593 | * If we haven't written the salt, do so now. Note that the | |
8594 | * feature may not be activated yet, but that's fine since | |
8595 | * the presence of this ZAP entry is backwards compatible. | |
8596 | */ | |
8597 | if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
8598 | DMU_POOL_CHECKSUM_SALT) == ENOENT) { | |
8599 | VERIFY0(zap_add(spa->spa_meta_objset, | |
8600 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, | |
8601 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
8602 | spa->spa_cksum_salt.zcs_bytes, tx)); | |
8603 | } | |
8604 | ||
13fe0198 | 8605 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
428870ff BB |
8606 | } |
8607 | ||
a1d477c2 MA |
8608 | static void |
8609 | vdev_indirect_state_sync_verify(vdev_t *vd) | |
8610 | { | |
8611 | ASSERTV(vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping); | |
8612 | ASSERTV(vdev_indirect_births_t *vib = vd->vdev_indirect_births); | |
8613 | ||
8614 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
8615 | ASSERT(vim != NULL); | |
8616 | ASSERT(vib != NULL); | |
8617 | } | |
8618 | ||
27f80e85 BB |
8619 | uint64_t obsolete_sm_object = 0; |
8620 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
8621 | if (obsolete_sm_object != 0) { | |
a1d477c2 MA |
8622 | ASSERT(vd->vdev_obsolete_sm != NULL); |
8623 | ASSERT(vd->vdev_removing || | |
8624 | vd->vdev_ops == &vdev_indirect_ops); | |
8625 | ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); | |
8626 | ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); | |
27f80e85 | 8627 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
8628 | space_map_object(vd->vdev_obsolete_sm)); |
8629 | ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, | |
8630 | space_map_allocated(vd->vdev_obsolete_sm)); | |
8631 | } | |
8632 | ASSERT(vd->vdev_obsolete_segments != NULL); | |
8633 | ||
8634 | /* | |
8635 | * Since frees / remaps to an indirect vdev can only | |
8636 | * happen in syncing context, the obsolete segments | |
8637 | * tree must be empty when we start syncing. | |
8638 | */ | |
8639 | ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); | |
8640 | } | |
8641 | ||
34dc7c2f | 8642 | /* |
8dc2197b SD |
8643 | * Set the top-level vdev's max queue depth. Evaluate each top-level's |
8644 | * async write queue depth in case it changed. The max queue depth will | |
8645 | * not change in the middle of syncing out this txg. | |
34dc7c2f | 8646 | */ |
8dc2197b SD |
8647 | static void |
8648 | spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) | |
34dc7c2f | 8649 | { |
8dc2197b SD |
8650 | ASSERT(spa_writeable(spa)); |
8651 | ||
34dc7c2f | 8652 | vdev_t *rvd = spa->spa_root_vdev; |
3dfb57a3 DB |
8653 | uint32_t max_queue_depth = zfs_vdev_async_write_max_active * |
8654 | zfs_vdev_queue_depth_pct / 100; | |
8dc2197b SD |
8655 | metaslab_class_t *normal = spa_normal_class(spa); |
8656 | metaslab_class_t *special = spa_special_class(spa); | |
8657 | metaslab_class_t *dedup = spa_dedup_class(spa); | |
34dc7c2f | 8658 | |
492f64e9 | 8659 | uint64_t slots_per_allocator = 0; |
1c27024e | 8660 | for (int c = 0; c < rvd->vdev_children; c++) { |
3dfb57a3 | 8661 | vdev_t *tvd = rvd->vdev_child[c]; |
cc99f275 | 8662 | |
8dc2197b | 8663 | metaslab_group_t *mg = tvd->vdev_mg; |
cc99f275 DB |
8664 | if (mg == NULL || !metaslab_group_initialized(mg)) |
8665 | continue; | |
3dfb57a3 | 8666 | |
8dc2197b | 8667 | metaslab_class_t *mc = mg->mg_class; |
cc99f275 | 8668 | if (mc != normal && mc != special && mc != dedup) |
3dfb57a3 DB |
8669 | continue; |
8670 | ||
8671 | /* | |
8672 | * It is safe to do a lock-free check here because only async | |
8673 | * allocations look at mg_max_alloc_queue_depth, and async | |
8674 | * allocations all happen from spa_sync(). | |
8675 | */ | |
492f64e9 | 8676 | for (int i = 0; i < spa->spa_alloc_count; i++) |
424fd7c3 TS |
8677 | ASSERT0(zfs_refcount_count( |
8678 | &(mg->mg_alloc_queue_depth[i]))); | |
3dfb57a3 | 8679 | mg->mg_max_alloc_queue_depth = max_queue_depth; |
492f64e9 PD |
8680 | |
8681 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
8682 | mg->mg_cur_max_alloc_queue_depth[i] = | |
8683 | zfs_vdev_def_queue_depth; | |
8684 | } | |
8685 | slots_per_allocator += zfs_vdev_def_queue_depth; | |
3dfb57a3 | 8686 | } |
cc99f275 | 8687 | |
492f64e9 | 8688 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
424fd7c3 TS |
8689 | ASSERT0(zfs_refcount_count(&normal->mc_alloc_slots[i])); |
8690 | ASSERT0(zfs_refcount_count(&special->mc_alloc_slots[i])); | |
8691 | ASSERT0(zfs_refcount_count(&dedup->mc_alloc_slots[i])); | |
cc99f275 DB |
8692 | normal->mc_alloc_max_slots[i] = slots_per_allocator; |
8693 | special->mc_alloc_max_slots[i] = slots_per_allocator; | |
8694 | dedup->mc_alloc_max_slots[i] = slots_per_allocator; | |
8695 | } | |
8696 | normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8697 | special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8698 | dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8dc2197b SD |
8699 | } |
8700 | ||
8701 | static void | |
8702 | spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) | |
8703 | { | |
8704 | ASSERT(spa_writeable(spa)); | |
3dfb57a3 | 8705 | |
8dc2197b | 8706 | vdev_t *rvd = spa->spa_root_vdev; |
a1d477c2 MA |
8707 | for (int c = 0; c < rvd->vdev_children; c++) { |
8708 | vdev_t *vd = rvd->vdev_child[c]; | |
8709 | vdev_indirect_state_sync_verify(vd); | |
8710 | ||
8711 | if (vdev_indirect_should_condense(vd)) { | |
8712 | spa_condense_indirect_start_sync(vd, tx); | |
8713 | break; | |
8714 | } | |
8715 | } | |
8dc2197b SD |
8716 | } |
8717 | ||
8718 | static void | |
8719 | spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) | |
8720 | { | |
8721 | objset_t *mos = spa->spa_meta_objset; | |
8722 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
8723 | uint64_t txg = tx->tx_txg; | |
8724 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
a1d477c2 | 8725 | |
34dc7c2f | 8726 | do { |
428870ff | 8727 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
8728 | |
8729 | spa_sync_config_object(spa, tx); | |
8730 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
8731 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
8732 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
8733 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
8734 | spa_errlog_sync(spa, txg); | |
8735 | dsl_pool_sync(dp, txg); | |
8736 | ||
93e28d66 SD |
8737 | if (pass < zfs_sync_pass_deferred_free || |
8738 | spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) { | |
8739 | /* | |
8740 | * If the log space map feature is active we don't | |
8741 | * care about deferred frees and the deferred bpobj | |
8742 | * as the log space map should effectively have the | |
8743 | * same results (i.e. appending only to one object). | |
8744 | */ | |
e8b96c60 | 8745 | spa_sync_frees(spa, free_bpl, tx); |
428870ff | 8746 | } else { |
905edb40 MA |
8747 | /* |
8748 | * We can not defer frees in pass 1, because | |
8749 | * we sync the deferred frees later in pass 1. | |
8750 | */ | |
8751 | ASSERT3U(pass, >, 1); | |
37f03da8 | 8752 | bplist_iterate(free_bpl, bpobj_enqueue_alloc_cb, |
e8b96c60 | 8753 | &spa->spa_deferred_bpobj, tx); |
34dc7c2f BB |
8754 | } |
8755 | ||
428870ff BB |
8756 | ddt_sync(spa, txg); |
8757 | dsl_scan_sync(dp, tx); | |
8dc2197b SD |
8758 | svr_sync(spa, tx); |
8759 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 8760 | |
93e28d66 SD |
8761 | spa_flush_metaslabs(spa, tx); |
8762 | ||
8dc2197b | 8763 | vdev_t *vd = NULL; |
a1d477c2 MA |
8764 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) |
8765 | != NULL) | |
428870ff BB |
8766 | vdev_sync(vd, txg); |
8767 | ||
8dc2197b SD |
8768 | /* |
8769 | * Note: We need to check if the MOS is dirty because we could | |
8770 | * have marked the MOS dirty without updating the uberblock | |
8771 | * (e.g. if we have sync tasks but no dirty user data). We need | |
8772 | * to check the uberblock's rootbp because it is updated if we | |
8773 | * have synced out dirty data (though in this case the MOS will | |
8774 | * most likely also be dirty due to second order effects, we | |
8775 | * don't want to rely on that here). | |
8776 | */ | |
8777 | if (pass == 1 && | |
8778 | spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
8779 | !dmu_objset_is_dirty(mos, txg)) { | |
905edb40 | 8780 | /* |
8dc2197b SD |
8781 | * Nothing changed on the first pass, therefore this |
8782 | * TXG is a no-op. Avoid syncing deferred frees, so | |
8783 | * that we can keep this TXG as a no-op. | |
905edb40 | 8784 | */ |
8dc2197b SD |
8785 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); |
8786 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
8787 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
8788 | ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); | |
8789 | break; | |
905edb40 | 8790 | } |
34dc7c2f | 8791 | |
8dc2197b | 8792 | spa_sync_deferred_frees(spa, tx); |
428870ff | 8793 | } while (dmu_objset_is_dirty(mos, txg)); |
8dc2197b | 8794 | } |
34dc7c2f | 8795 | |
8dc2197b SD |
8796 | /* |
8797 | * Rewrite the vdev configuration (which includes the uberblock) to | |
8798 | * commit the transaction group. | |
8799 | * | |
8800 | * If there are no dirty vdevs, we sync the uberblock to a few random | |
8801 | * top-level vdevs that are known to be visible in the config cache | |
8802 | * (see spa_vdev_add() for a complete description). If there *are* dirty | |
8803 | * vdevs, sync the uberblock to all vdevs. | |
8804 | */ | |
8805 | static void | |
8806 | spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) | |
8807 | { | |
8808 | vdev_t *rvd = spa->spa_root_vdev; | |
8809 | uint64_t txg = tx->tx_txg; | |
a1d477c2 | 8810 | |
b128c09f | 8811 | for (;;) { |
8dc2197b SD |
8812 | int error = 0; |
8813 | ||
b128c09f BB |
8814 | /* |
8815 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
8816 | * while we're attempting to write the vdev labels. | |
8817 | */ | |
8818 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
8819 | ||
8820 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
d2734cce | 8821 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; |
b128c09f BB |
8822 | int svdcount = 0; |
8823 | int children = rvd->vdev_children; | |
8824 | int c0 = spa_get_random(children); | |
b128c09f | 8825 | |
1c27024e | 8826 | for (int c = 0; c < children; c++) { |
8dc2197b SD |
8827 | vdev_t *vd = |
8828 | rvd->vdev_child[(c0 + c) % children]; | |
d2734cce SD |
8829 | |
8830 | /* Stop when revisiting the first vdev */ | |
8831 | if (c > 0 && svd[0] == vd) | |
8832 | break; | |
8833 | ||
8dc2197b SD |
8834 | if (vd->vdev_ms_array == 0 || |
8835 | vd->vdev_islog || | |
a1d477c2 | 8836 | !vdev_is_concrete(vd)) |
b128c09f | 8837 | continue; |
d2734cce | 8838 | |
b128c09f | 8839 | svd[svdcount++] = vd; |
6cb8e530 | 8840 | if (svdcount == SPA_SYNC_MIN_VDEVS) |
b128c09f BB |
8841 | break; |
8842 | } | |
b6fcb792 | 8843 | error = vdev_config_sync(svd, svdcount, txg); |
b128c09f BB |
8844 | } else { |
8845 | error = vdev_config_sync(rvd->vdev_child, | |
b6fcb792 | 8846 | rvd->vdev_children, txg); |
34dc7c2f | 8847 | } |
34dc7c2f | 8848 | |
3bc7e0fb GW |
8849 | if (error == 0) |
8850 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
8851 | ||
b128c09f BB |
8852 | spa_config_exit(spa, SCL_STATE, FTAG); |
8853 | ||
8854 | if (error == 0) | |
8855 | break; | |
cec3a0a1 | 8856 | zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); |
b128c09f BB |
8857 | zio_resume_wait(spa); |
8858 | } | |
8dc2197b SD |
8859 | } |
8860 | ||
8861 | /* | |
8862 | * Sync the specified transaction group. New blocks may be dirtied as | |
8863 | * part of the process, so we iterate until it converges. | |
8864 | */ | |
8865 | void | |
8866 | spa_sync(spa_t *spa, uint64_t txg) | |
8867 | { | |
8868 | vdev_t *vd = NULL; | |
8869 | ||
8870 | VERIFY(spa_writeable(spa)); | |
8871 | ||
8872 | /* | |
8873 | * Wait for i/os issued in open context that need to complete | |
8874 | * before this txg syncs. | |
8875 | */ | |
8876 | (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); | |
8877 | spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, | |
8878 | ZIO_FLAG_CANFAIL); | |
8879 | ||
8880 | /* | |
8881 | * Lock out configuration changes. | |
8882 | */ | |
8883 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8884 | ||
8885 | spa->spa_syncing_txg = txg; | |
8886 | spa->spa_sync_pass = 0; | |
8887 | ||
8888 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
8889 | mutex_enter(&spa->spa_alloc_locks[i]); | |
8890 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
8891 | mutex_exit(&spa->spa_alloc_locks[i]); | |
8892 | } | |
8893 | ||
8894 | /* | |
8895 | * If there are any pending vdev state changes, convert them | |
8896 | * into config changes that go out with this transaction group. | |
8897 | */ | |
8898 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
8899 | while (list_head(&spa->spa_state_dirty_list) != NULL) { | |
8900 | /* | |
8901 | * We need the write lock here because, for aux vdevs, | |
8902 | * calling vdev_config_dirty() modifies sav_config. | |
8903 | * This is ugly and will become unnecessary when we | |
8904 | * eliminate the aux vdev wart by integrating all vdevs | |
8905 | * into the root vdev tree. | |
8906 | */ | |
8907 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
8908 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
8909 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
8910 | vdev_state_clean(vd); | |
8911 | vdev_config_dirty(vd); | |
8912 | } | |
8913 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
8914 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
8915 | } | |
8916 | spa_config_exit(spa, SCL_STATE, FTAG); | |
8917 | ||
8918 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
8919 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
8920 | ||
8921 | spa->spa_sync_starttime = gethrtime(); | |
8922 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); | |
8923 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, | |
8924 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
8925 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
8926 | ||
8927 | /* | |
8928 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
8929 | * set spa_deflate if we have no raid-z vdevs. | |
8930 | */ | |
8931 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
8932 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
8933 | vdev_t *rvd = spa->spa_root_vdev; | |
8934 | ||
8935 | int i; | |
8936 | for (i = 0; i < rvd->vdev_children; i++) { | |
8937 | vd = rvd->vdev_child[i]; | |
8938 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
8939 | break; | |
8940 | } | |
8941 | if (i == rvd->vdev_children) { | |
8942 | spa->spa_deflate = TRUE; | |
8943 | VERIFY0(zap_add(spa->spa_meta_objset, | |
8944 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
8945 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
8946 | } | |
8947 | } | |
8948 | ||
8949 | spa_sync_adjust_vdev_max_queue_depth(spa); | |
8950 | ||
8951 | spa_sync_condense_indirect(spa, tx); | |
8952 | ||
8953 | spa_sync_iterate_to_convergence(spa, tx); | |
8954 | ||
8955 | #ifdef ZFS_DEBUG | |
8956 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
8957 | /* | |
8958 | * Make sure that the number of ZAPs for all the vdevs matches | |
8959 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
8960 | * called if the config is dirty; otherwise there may be | |
8961 | * outstanding AVZ operations that weren't completed in | |
8962 | * spa_sync_config_object. | |
8963 | */ | |
8964 | uint64_t all_vdev_zap_entry_count; | |
8965 | ASSERT0(zap_count(spa->spa_meta_objset, | |
8966 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
8967 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
8968 | all_vdev_zap_entry_count); | |
8969 | } | |
8970 | #endif | |
8971 | ||
8972 | if (spa->spa_vdev_removal != NULL) { | |
8973 | ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); | |
8974 | } | |
8975 | ||
8976 | spa_sync_rewrite_vdev_config(spa, tx); | |
34dc7c2f BB |
8977 | dmu_tx_commit(tx); |
8978 | ||
57ddcda1 | 8979 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 GW |
8980 | spa->spa_deadman_tqid = 0; |
8981 | ||
34dc7c2f BB |
8982 | /* |
8983 | * Clear the dirty config list. | |
8984 | */ | |
b128c09f | 8985 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
8986 | vdev_config_clean(vd); |
8987 | ||
8988 | /* | |
8989 | * Now that the new config has synced transactionally, | |
8990 | * let it become visible to the config cache. | |
8991 | */ | |
8992 | if (spa->spa_config_syncing != NULL) { | |
8993 | spa_config_set(spa, spa->spa_config_syncing); | |
8994 | spa->spa_config_txg = txg; | |
8995 | spa->spa_config_syncing = NULL; | |
8996 | } | |
8997 | ||
428870ff | 8998 | dsl_pool_sync_done(dp, txg); |
34dc7c2f | 8999 | |
492f64e9 PD |
9000 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
9001 | mutex_enter(&spa->spa_alloc_locks[i]); | |
9002 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
9003 | mutex_exit(&spa->spa_alloc_locks[i]); | |
9004 | } | |
3dfb57a3 | 9005 | |
34dc7c2f BB |
9006 | /* |
9007 | * Update usable space statistics. | |
9008 | */ | |
619f0976 GW |
9009 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) |
9010 | != NULL) | |
34dc7c2f | 9011 | vdev_sync_done(vd, txg); |
93e28d66 | 9012 | spa_sync_close_syncing_log_sm(spa); |
34dc7c2f | 9013 | |
428870ff BB |
9014 | spa_update_dspace(spa); |
9015 | ||
34dc7c2f BB |
9016 | /* |
9017 | * It had better be the case that we didn't dirty anything | |
9018 | * since vdev_config_sync(). | |
9019 | */ | |
9020 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
9021 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9022 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff | 9023 | |
d2734cce SD |
9024 | while (zfs_pause_spa_sync) |
9025 | delay(1); | |
9026 | ||
428870ff | 9027 | spa->spa_sync_pass = 0; |
34dc7c2f | 9028 | |
55922e73 GW |
9029 | /* |
9030 | * Update the last synced uberblock here. We want to do this at | |
9031 | * the end of spa_sync() so that consumers of spa_last_synced_txg() | |
9032 | * will be guaranteed that all the processing associated with | |
9033 | * that txg has been completed. | |
9034 | */ | |
9035 | spa->spa_ubsync = spa->spa_uberblock; | |
b128c09f | 9036 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 9037 | |
428870ff BB |
9038 | spa_handle_ignored_writes(spa); |
9039 | ||
34dc7c2f BB |
9040 | /* |
9041 | * If any async tasks have been requested, kick them off. | |
9042 | */ | |
9043 | spa_async_dispatch(spa); | |
9044 | } | |
9045 | ||
9046 | /* | |
9047 | * Sync all pools. We don't want to hold the namespace lock across these | |
9048 | * operations, so we take a reference on the spa_t and drop the lock during the | |
9049 | * sync. | |
9050 | */ | |
9051 | void | |
9052 | spa_sync_allpools(void) | |
9053 | { | |
9054 | spa_t *spa = NULL; | |
9055 | mutex_enter(&spa_namespace_lock); | |
9056 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
9057 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
9058 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
9059 | continue; |
9060 | spa_open_ref(spa, FTAG); | |
9061 | mutex_exit(&spa_namespace_lock); | |
9062 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9063 | mutex_enter(&spa_namespace_lock); | |
9064 | spa_close(spa, FTAG); | |
9065 | } | |
9066 | mutex_exit(&spa_namespace_lock); | |
9067 | } | |
9068 | ||
9069 | /* | |
9070 | * ========================================================================== | |
9071 | * Miscellaneous routines | |
9072 | * ========================================================================== | |
9073 | */ | |
9074 | ||
9075 | /* | |
9076 | * Remove all pools in the system. | |
9077 | */ | |
9078 | void | |
9079 | spa_evict_all(void) | |
9080 | { | |
9081 | spa_t *spa; | |
9082 | ||
9083 | /* | |
9084 | * Remove all cached state. All pools should be closed now, | |
9085 | * so every spa in the AVL tree should be unreferenced. | |
9086 | */ | |
9087 | mutex_enter(&spa_namespace_lock); | |
9088 | while ((spa = spa_next(NULL)) != NULL) { | |
9089 | /* | |
9090 | * Stop async tasks. The async thread may need to detach | |
9091 | * a device that's been replaced, which requires grabbing | |
9092 | * spa_namespace_lock, so we must drop it here. | |
9093 | */ | |
9094 | spa_open_ref(spa, FTAG); | |
9095 | mutex_exit(&spa_namespace_lock); | |
9096 | spa_async_suspend(spa); | |
9097 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
9098 | spa_close(spa, FTAG); |
9099 | ||
9100 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
9101 | spa_unload(spa); | |
9102 | spa_deactivate(spa); | |
9103 | } | |
9104 | spa_remove(spa); | |
9105 | } | |
9106 | mutex_exit(&spa_namespace_lock); | |
9107 | } | |
9108 | ||
9109 | vdev_t * | |
9babb374 | 9110 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 9111 | { |
b128c09f BB |
9112 | vdev_t *vd; |
9113 | int i; | |
9114 | ||
9115 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
9116 | return (vd); | |
9117 | ||
9babb374 | 9118 | if (aux) { |
b128c09f BB |
9119 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
9120 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
9121 | if (vd->vdev_guid == guid) |
9122 | return (vd); | |
9123 | } | |
9124 | ||
9125 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
9126 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
9127 | if (vd->vdev_guid == guid) |
9128 | return (vd); | |
9129 | } | |
9130 | } | |
9131 | ||
9132 | return (NULL); | |
34dc7c2f BB |
9133 | } |
9134 | ||
9135 | void | |
9136 | spa_upgrade(spa_t *spa, uint64_t version) | |
9137 | { | |
572e2857 BB |
9138 | ASSERT(spa_writeable(spa)); |
9139 | ||
b128c09f | 9140 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
9141 | |
9142 | /* | |
9143 | * This should only be called for a non-faulted pool, and since a | |
9144 | * future version would result in an unopenable pool, this shouldn't be | |
9145 | * possible. | |
9146 | */ | |
8dca0a9a | 9147 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
9b67f605 | 9148 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); |
34dc7c2f BB |
9149 | |
9150 | spa->spa_uberblock.ub_version = version; | |
9151 | vdev_config_dirty(spa->spa_root_vdev); | |
9152 | ||
b128c09f | 9153 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
9154 | |
9155 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9156 | } | |
9157 | ||
9158 | boolean_t | |
9159 | spa_has_spare(spa_t *spa, uint64_t guid) | |
9160 | { | |
9161 | int i; | |
9162 | uint64_t spareguid; | |
9163 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9164 | ||
9165 | for (i = 0; i < sav->sav_count; i++) | |
9166 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
9167 | return (B_TRUE); | |
9168 | ||
9169 | for (i = 0; i < sav->sav_npending; i++) { | |
9170 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
9171 | &spareguid) == 0 && spareguid == guid) | |
9172 | return (B_TRUE); | |
9173 | } | |
9174 | ||
9175 | return (B_FALSE); | |
9176 | } | |
9177 | ||
b128c09f BB |
9178 | /* |
9179 | * Check if a pool has an active shared spare device. | |
9180 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
9181 | */ | |
9182 | static boolean_t | |
9183 | spa_has_active_shared_spare(spa_t *spa) | |
9184 | { | |
9185 | int i, refcnt; | |
9186 | uint64_t pool; | |
9187 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9188 | ||
9189 | for (i = 0; i < sav->sav_count; i++) { | |
9190 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
9191 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
9192 | refcnt > 2) | |
9193 | return (B_TRUE); | |
9194 | } | |
9195 | ||
9196 | return (B_FALSE); | |
9197 | } | |
9198 | ||
93e28d66 SD |
9199 | uint64_t |
9200 | spa_total_metaslabs(spa_t *spa) | |
9201 | { | |
9202 | vdev_t *rvd = spa->spa_root_vdev; | |
9203 | ||
9204 | uint64_t m = 0; | |
9205 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
9206 | vdev_t *vd = rvd->vdev_child[c]; | |
9207 | if (!vdev_is_concrete(vd)) | |
9208 | continue; | |
9209 | m += vd->vdev_ms_count; | |
9210 | } | |
9211 | return (m); | |
9212 | } | |
9213 | ||
a1d477c2 | 9214 | sysevent_t * |
12fa0466 DE |
9215 | spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
9216 | { | |
9217 | sysevent_t *ev = NULL; | |
9218 | #ifdef _KERNEL | |
9219 | nvlist_t *resource; | |
9220 | ||
9221 | resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl); | |
9222 | if (resource) { | |
9223 | ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP); | |
9224 | ev->resource = resource; | |
9225 | } | |
9226 | #endif | |
9227 | return (ev); | |
9228 | } | |
9229 | ||
a1d477c2 | 9230 | void |
12fa0466 DE |
9231 | spa_event_post(sysevent_t *ev) |
9232 | { | |
9233 | #ifdef _KERNEL | |
9234 | if (ev) { | |
9235 | zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb); | |
9236 | kmem_free(ev, sizeof (*ev)); | |
9237 | } | |
9238 | #endif | |
9239 | } | |
9240 | ||
34dc7c2f | 9241 | /* |
fb390aaf HR |
9242 | * Post a zevent corresponding to the given sysevent. The 'name' must be one |
9243 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
34dc7c2f BB |
9244 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
9245 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
9246 | * or zdb as real changes. | |
9247 | */ | |
9248 | void | |
12fa0466 | 9249 | spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
34dc7c2f | 9250 | { |
12fa0466 | 9251 | spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); |
34dc7c2f | 9252 | } |
c28b2279 | 9253 | |
93ce2b4c | 9254 | #if defined(_KERNEL) |
c28b2279 BB |
9255 | /* state manipulation functions */ |
9256 | EXPORT_SYMBOL(spa_open); | |
9257 | EXPORT_SYMBOL(spa_open_rewind); | |
9258 | EXPORT_SYMBOL(spa_get_stats); | |
9259 | EXPORT_SYMBOL(spa_create); | |
c28b2279 BB |
9260 | EXPORT_SYMBOL(spa_import); |
9261 | EXPORT_SYMBOL(spa_tryimport); | |
9262 | EXPORT_SYMBOL(spa_destroy); | |
9263 | EXPORT_SYMBOL(spa_export); | |
9264 | EXPORT_SYMBOL(spa_reset); | |
9265 | EXPORT_SYMBOL(spa_async_request); | |
9266 | EXPORT_SYMBOL(spa_async_suspend); | |
9267 | EXPORT_SYMBOL(spa_async_resume); | |
9268 | EXPORT_SYMBOL(spa_inject_addref); | |
9269 | EXPORT_SYMBOL(spa_inject_delref); | |
9270 | EXPORT_SYMBOL(spa_scan_stat_init); | |
9271 | EXPORT_SYMBOL(spa_scan_get_stats); | |
9272 | ||
9273 | /* device maniion */ | |
9274 | EXPORT_SYMBOL(spa_vdev_add); | |
9275 | EXPORT_SYMBOL(spa_vdev_attach); | |
9276 | EXPORT_SYMBOL(spa_vdev_detach); | |
c28b2279 BB |
9277 | EXPORT_SYMBOL(spa_vdev_setpath); |
9278 | EXPORT_SYMBOL(spa_vdev_setfru); | |
9279 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
9280 | ||
9281 | /* spare statech is global across all pools) */ | |
9282 | EXPORT_SYMBOL(spa_spare_add); | |
9283 | EXPORT_SYMBOL(spa_spare_remove); | |
9284 | EXPORT_SYMBOL(spa_spare_exists); | |
9285 | EXPORT_SYMBOL(spa_spare_activate); | |
9286 | ||
9287 | /* L2ARC statech is global across all pools) */ | |
9288 | EXPORT_SYMBOL(spa_l2cache_add); | |
9289 | EXPORT_SYMBOL(spa_l2cache_remove); | |
9290 | EXPORT_SYMBOL(spa_l2cache_exists); | |
9291 | EXPORT_SYMBOL(spa_l2cache_activate); | |
9292 | EXPORT_SYMBOL(spa_l2cache_drop); | |
9293 | ||
9294 | /* scanning */ | |
9295 | EXPORT_SYMBOL(spa_scan); | |
9296 | EXPORT_SYMBOL(spa_scan_stop); | |
9297 | ||
9298 | /* spa syncing */ | |
9299 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
9300 | EXPORT_SYMBOL(spa_sync_allpools); | |
9301 | ||
9302 | /* properties */ | |
9303 | EXPORT_SYMBOL(spa_prop_set); | |
9304 | EXPORT_SYMBOL(spa_prop_get); | |
9305 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
9306 | ||
9307 | /* asynchronous event notification */ | |
9308 | EXPORT_SYMBOL(spa_event_notify); | |
9309 | #endif | |
dea377c0 | 9310 | |
93ce2b4c | 9311 | #if defined(_KERNEL) |
dea377c0 MA |
9312 | module_param(spa_load_verify_maxinflight, int, 0644); |
9313 | MODULE_PARM_DESC(spa_load_verify_maxinflight, | |
9314 | "Max concurrent traversal I/Os while verifying pool during import -X"); | |
9315 | ||
9316 | module_param(spa_load_verify_metadata, int, 0644); | |
9317 | MODULE_PARM_DESC(spa_load_verify_metadata, | |
9318 | "Set to traverse metadata on pool import"); | |
9319 | ||
9320 | module_param(spa_load_verify_data, int, 0644); | |
9321 | MODULE_PARM_DESC(spa_load_verify_data, | |
9322 | "Set to traverse data on pool import"); | |
dcb6bed1 | 9323 | |
6cb8e530 PZ |
9324 | module_param(spa_load_print_vdev_tree, int, 0644); |
9325 | MODULE_PARM_DESC(spa_load_print_vdev_tree, | |
9326 | "Print vdev tree to zfs_dbgmsg during pool import"); | |
9327 | ||
02730c33 | 9328 | /* CSTYLED */ |
dcb6bed1 D |
9329 | module_param(zio_taskq_batch_pct, uint, 0444); |
9330 | MODULE_PARM_DESC(zio_taskq_batch_pct, | |
9331 | "Percentage of CPUs to run an IO worker thread"); | |
9332 | ||
6cb8e530 PZ |
9333 | /* BEGIN CSTYLED */ |
9334 | module_param(zfs_max_missing_tvds, ulong, 0644); | |
9335 | MODULE_PARM_DESC(zfs_max_missing_tvds, | |
9336 | "Allow importing pool with up to this number of missing top-level vdevs" | |
9337 | " (in read-only mode)"); | |
9338 | /* END CSTYLED */ | |
9339 | ||
37f03da8 SH |
9340 | module_param(zfs_livelist_condense_zthr_pause, int, 0644); |
9341 | MODULE_PARM_DESC(zfs_livelist_condense_zthr_pause, | |
9342 | "Set the livelist condense zthr to pause"); | |
9343 | module_param(zfs_livelist_condense_sync_pause, int, 0644); | |
9344 | MODULE_PARM_DESC(zfs_livelist_condense_sync_pause, | |
9345 | "Set the livelist condense synctask to pause"); | |
9346 | ||
9347 | module_param(zfs_livelist_condense_sync_cancel, int, 0644); | |
9348 | MODULE_PARM_DESC(zfs_livelist_condense_sync_cancel, | |
9349 | "Whether livelist condensing was canceled in the synctask"); | |
9350 | module_param(zfs_livelist_condense_zthr_cancel, int, 0644); | |
9351 | MODULE_PARM_DESC(zfs_livelist_condense_zthr_cancel, | |
9352 | "Whether livelist condensing was canceled in the zthr function"); | |
9353 | ||
9354 | /* BEGIN CSTYLED */ | |
9355 | module_param(zfs_livelist_condense_new_alloc, int, 0644); | |
9356 | MODULE_PARM_DESC(zfs_livelist_condense_new_alloc, | |
9357 | "Whether extra ALLOC blkptrs were added to a livelist entry while it" | |
9358 | " was being condensed"); | |
9359 | /* END CSTYLED */ | |
dea377c0 | 9360 | #endif |