]>
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 | |
1d3ba0bf | 9 | * or https://opensource.org/licenses/CDDL-1.0. |
34dc7c2f BB |
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. |
4f072827 | 24 | * Copyright (c) 2011, 2020 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. |
3c819a2c | 32 | * Copyright (c) 2017, 2019, Datto Inc. All rights reserved. |
12fa0466 | 33 | * Copyright 2017 Joyent, Inc. |
cc99f275 | 34 | * Copyright (c) 2017, Intel Corporation. |
658fb802 | 35 | * Copyright (c) 2021, Colm Buckley <colm@tuatha.org> |
a38718a6 | 36 | */ |
34dc7c2f | 37 | |
34dc7c2f | 38 | /* |
e49f1e20 WA |
39 | * SPA: Storage Pool Allocator |
40 | * | |
34dc7c2f BB |
41 | * This file contains all the routines used when modifying on-disk SPA state. |
42 | * This includes opening, importing, destroying, exporting a pool, and syncing a | |
43 | * pool. | |
44 | */ | |
45 | ||
46 | #include <sys/zfs_context.h> | |
47 | #include <sys/fm/fs/zfs.h> | |
48 | #include <sys/spa_impl.h> | |
49 | #include <sys/zio.h> | |
50 | #include <sys/zio_checksum.h> | |
34dc7c2f BB |
51 | #include <sys/dmu.h> |
52 | #include <sys/dmu_tx.h> | |
53 | #include <sys/zap.h> | |
54 | #include <sys/zil.h> | |
428870ff | 55 | #include <sys/ddt.h> |
34dc7c2f | 56 | #include <sys/vdev_impl.h> |
a1d477c2 MA |
57 | #include <sys/vdev_removal.h> |
58 | #include <sys/vdev_indirect_mapping.h> | |
59 | #include <sys/vdev_indirect_births.h> | |
619f0976 | 60 | #include <sys/vdev_initialize.h> |
9a49d3f3 | 61 | #include <sys/vdev_rebuild.h> |
1b939560 | 62 | #include <sys/vdev_trim.h> |
c28b2279 | 63 | #include <sys/vdev_disk.h> |
b2255edc | 64 | #include <sys/vdev_draid.h> |
34dc7c2f | 65 | #include <sys/metaslab.h> |
428870ff | 66 | #include <sys/metaslab_impl.h> |
379ca9cf | 67 | #include <sys/mmp.h> |
34dc7c2f BB |
68 | #include <sys/uberblock_impl.h> |
69 | #include <sys/txg.h> | |
70 | #include <sys/avl.h> | |
a1d477c2 | 71 | #include <sys/bpobj.h> |
34dc7c2f BB |
72 | #include <sys/dmu_traverse.h> |
73 | #include <sys/dmu_objset.h> | |
74 | #include <sys/unique.h> | |
75 | #include <sys/dsl_pool.h> | |
76 | #include <sys/dsl_dataset.h> | |
77 | #include <sys/dsl_dir.h> | |
78 | #include <sys/dsl_prop.h> | |
79 | #include <sys/dsl_synctask.h> | |
80 | #include <sys/fs/zfs.h> | |
81 | #include <sys/arc.h> | |
82 | #include <sys/callb.h> | |
83 | #include <sys/systeminfo.h> | |
9babb374 | 84 | #include <sys/zfs_ioctl.h> |
428870ff | 85 | #include <sys/dsl_scan.h> |
9ae529ec | 86 | #include <sys/zfeature.h> |
13fe0198 | 87 | #include <sys/dsl_destroy.h> |
526af785 | 88 | #include <sys/zvol.h> |
34dc7c2f | 89 | |
d164b209 | 90 | #ifdef _KERNEL |
12fa0466 DE |
91 | #include <sys/fm/protocol.h> |
92 | #include <sys/fm/util.h> | |
428870ff | 93 | #include <sys/callb.h> |
d164b209 | 94 | #include <sys/zone.h> |
c8242a96 | 95 | #include <sys/vmsystm.h> |
d164b209 BB |
96 | #endif /* _KERNEL */ |
97 | ||
34dc7c2f BB |
98 | #include "zfs_prop.h" |
99 | #include "zfs_comutil.h" | |
100 | ||
e6cfd633 WA |
101 | /* |
102 | * The interval, in seconds, at which failed configuration cache file writes | |
103 | * should be retried. | |
104 | */ | |
a1d477c2 | 105 | int zfs_ccw_retry_interval = 300; |
e6cfd633 | 106 | |
428870ff | 107 | typedef enum zti_modes { |
7ef5e54e | 108 | ZTI_MODE_FIXED, /* value is # of threads (min 1) */ |
7ef5e54e | 109 | ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ |
7457b024 | 110 | ZTI_MODE_SCALE, /* Taskqs scale with CPUs. */ |
7ef5e54e AL |
111 | ZTI_MODE_NULL, /* don't create a taskq */ |
112 | ZTI_NMODES | |
428870ff | 113 | } zti_modes_t; |
34dc7c2f | 114 | |
7ef5e54e AL |
115 | #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } |
116 | #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } | |
117 | #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } | |
7457b024 | 118 | #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 } |
7ef5e54e | 119 | #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } |
9babb374 | 120 | |
7ef5e54e AL |
121 | #define ZTI_N(n) ZTI_P(n, 1) |
122 | #define ZTI_ONE ZTI_N(1) | |
9babb374 BB |
123 | |
124 | typedef struct zio_taskq_info { | |
7ef5e54e | 125 | zti_modes_t zti_mode; |
428870ff | 126 | uint_t zti_value; |
7ef5e54e | 127 | uint_t zti_count; |
9babb374 BB |
128 | } zio_taskq_info_t; |
129 | ||
130 | static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { | |
451041db | 131 | "iss", "iss_h", "int", "int_h" |
9babb374 BB |
132 | }; |
133 | ||
428870ff | 134 | /* |
7ef5e54e AL |
135 | * This table defines the taskq settings for each ZFS I/O type. When |
136 | * initializing a pool, we use this table to create an appropriately sized | |
137 | * taskq. Some operations are low volume and therefore have a small, static | |
138 | * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE | |
139 | * macros. Other operations process a large amount of data; the ZTI_BATCH | |
140 | * macro causes us to create a taskq oriented for throughput. Some operations | |
1b939560 | 141 | * are so high frequency and short-lived that the taskq itself can become a |
7ef5e54e AL |
142 | * point of lock contention. The ZTI_P(#, #) macro indicates that we need an |
143 | * additional degree of parallelism specified by the number of threads per- | |
144 | * taskq and the number of taskqs; when dispatching an event in this case, the | |
7457b024 AM |
145 | * particular taskq is chosen at random. ZTI_SCALE is similar to ZTI_BATCH, |
146 | * but with number of taskqs also scaling with number of CPUs. | |
7ef5e54e AL |
147 | * |
148 | * The different taskq priorities are to handle the different contexts (issue | |
149 | * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that | |
150 | * need to be handled with minimum delay. | |
428870ff | 151 | */ |
18168da7 | 152 | static const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { |
428870ff | 153 | /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ |
7ef5e54e | 154 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ |
7457b024 AM |
155 | { ZTI_N(8), ZTI_NULL, ZTI_SCALE, ZTI_NULL }, /* READ */ |
156 | { ZTI_BATCH, ZTI_N(5), ZTI_SCALE, ZTI_N(5) }, /* WRITE */ | |
157 | { ZTI_SCALE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ | |
7ef5e54e AL |
158 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ |
159 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ | |
1b939560 | 160 | { ZTI_N(4), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* TRIM */ |
9babb374 BB |
161 | }; |
162 | ||
13fe0198 MA |
163 | static void spa_sync_version(void *arg, dmu_tx_t *tx); |
164 | static void spa_sync_props(void *arg, dmu_tx_t *tx); | |
b128c09f | 165 | static boolean_t spa_has_active_shared_spare(spa_t *spa); |
a926aab9 AZ |
166 | static int spa_load_impl(spa_t *spa, spa_import_type_t type, |
167 | const char **ereport); | |
572e2857 | 168 | static void spa_vdev_resilver_done(spa_t *spa); |
428870ff | 169 | |
18168da7 AZ |
170 | static uint_t zio_taskq_batch_pct = 80; /* 1 thread per cpu in pset */ |
171 | static uint_t zio_taskq_batch_tpq; /* threads per taskq */ | |
172 | static const boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ | |
173 | static const uint_t zio_taskq_basedc = 80; /* base duty cycle */ | |
428870ff | 174 | |
18168da7 | 175 | static const boolean_t spa_create_process = B_TRUE; /* no process => no sysdc */ |
428870ff | 176 | |
afd2f7b7 PZ |
177 | /* |
178 | * Report any spa_load_verify errors found, but do not fail spa_load. | |
179 | * This is used by zdb to analyze non-idle pools. | |
180 | */ | |
181 | boolean_t spa_load_verify_dryrun = B_FALSE; | |
182 | ||
e39fe05b FU |
183 | /* |
184 | * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ). | |
185 | * This is used by zdb for spacemaps verification. | |
186 | */ | |
187 | boolean_t spa_mode_readable_spacemaps = B_FALSE; | |
188 | ||
428870ff BB |
189 | /* |
190 | * This (illegal) pool name is used when temporarily importing a spa_t in order | |
191 | * to get the vdev stats associated with the imported devices. | |
192 | */ | |
193 | #define TRYIMPORT_NAME "$import" | |
34dc7c2f | 194 | |
6cb8e530 PZ |
195 | /* |
196 | * For debugging purposes: print out vdev tree during pool import. | |
197 | */ | |
18168da7 | 198 | static int spa_load_print_vdev_tree = B_FALSE; |
6cb8e530 PZ |
199 | |
200 | /* | |
201 | * A non-zero value for zfs_max_missing_tvds means that we allow importing | |
202 | * pools with missing top-level vdevs. This is strictly intended for advanced | |
203 | * pool recovery cases since missing data is almost inevitable. Pools with | |
204 | * missing devices can only be imported read-only for safety reasons, and their | |
205 | * fail-mode will be automatically set to "continue". | |
206 | * | |
207 | * With 1 missing vdev we should be able to import the pool and mount all | |
208 | * datasets. User data that was not modified after the missing device has been | |
209 | * added should be recoverable. This means that snapshots created prior to the | |
210 | * addition of that device should be completely intact. | |
211 | * | |
212 | * With 2 missing vdevs, some datasets may fail to mount since there are | |
213 | * dataset statistics that are stored as regular metadata. Some data might be | |
214 | * recoverable if those vdevs were added recently. | |
215 | * | |
216 | * With 3 or more missing vdevs, the pool is severely damaged and MOS entries | |
217 | * may be missing entirely. Chances of data recovery are very low. Note that | |
218 | * there are also risks of performing an inadvertent rewind as we might be | |
219 | * missing all the vdevs with the latest uberblocks. | |
220 | */ | |
ab8d9c17 | 221 | uint64_t zfs_max_missing_tvds = 0; |
6cb8e530 PZ |
222 | |
223 | /* | |
224 | * The parameters below are similar to zfs_max_missing_tvds but are only | |
225 | * intended for a preliminary open of the pool with an untrusted config which | |
226 | * might be incomplete or out-dated. | |
227 | * | |
228 | * We are more tolerant for pools opened from a cachefile since we could have | |
229 | * an out-dated cachefile where a device removal was not registered. | |
230 | * We could have set the limit arbitrarily high but in the case where devices | |
231 | * are really missing we would want to return the proper error codes; we chose | |
232 | * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available | |
233 | * and we get a chance to retrieve the trusted config. | |
234 | */ | |
235 | uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1; | |
d2734cce | 236 | |
6cb8e530 PZ |
237 | /* |
238 | * In the case where config was assembled by scanning device paths (/dev/dsks | |
239 | * by default) we are less tolerant since all the existing devices should have | |
240 | * been detected and we want spa_load to return the right error codes. | |
241 | */ | |
242 | uint64_t zfs_max_missing_tvds_scan = 0; | |
243 | ||
d2734cce SD |
244 | /* |
245 | * Debugging aid that pauses spa_sync() towards the end. | |
246 | */ | |
18168da7 | 247 | static const boolean_t zfs_pause_spa_sync = B_FALSE; |
d2734cce | 248 | |
37f03da8 SH |
249 | /* |
250 | * Variables to indicate the livelist condense zthr func should wait at certain | |
251 | * points for the livelist to be removed - used to test condense/destroy races | |
252 | */ | |
18168da7 AZ |
253 | static int zfs_livelist_condense_zthr_pause = 0; |
254 | static int zfs_livelist_condense_sync_pause = 0; | |
37f03da8 SH |
255 | |
256 | /* | |
257 | * Variables to track whether or not condense cancellation has been | |
258 | * triggered in testing. | |
259 | */ | |
18168da7 AZ |
260 | static int zfs_livelist_condense_sync_cancel = 0; |
261 | static int zfs_livelist_condense_zthr_cancel = 0; | |
37f03da8 SH |
262 | |
263 | /* | |
264 | * Variable to track whether or not extra ALLOC blkptrs were added to a | |
265 | * livelist entry while it was being condensed (caused by the way we track | |
266 | * remapped blkptrs in dbuf_remap_impl) | |
267 | */ | |
18168da7 | 268 | static int zfs_livelist_condense_new_alloc = 0; |
37f03da8 | 269 | |
34dc7c2f BB |
270 | /* |
271 | * ========================================================================== | |
272 | * SPA properties routines | |
273 | * ========================================================================== | |
274 | */ | |
275 | ||
276 | /* | |
277 | * Add a (source=src, propname=propval) list to an nvlist. | |
278 | */ | |
279 | static void | |
a926aab9 | 280 | spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, const char *strval, |
34dc7c2f BB |
281 | uint64_t intval, zprop_source_t src) |
282 | { | |
283 | const char *propname = zpool_prop_to_name(prop); | |
284 | nvlist_t *propval; | |
285 | ||
65ad5d11 AJ |
286 | propval = fnvlist_alloc(); |
287 | fnvlist_add_uint64(propval, ZPROP_SOURCE, src); | |
34dc7c2f BB |
288 | |
289 | if (strval != NULL) | |
65ad5d11 | 290 | fnvlist_add_string(propval, ZPROP_VALUE, strval); |
34dc7c2f | 291 | else |
65ad5d11 | 292 | fnvlist_add_uint64(propval, ZPROP_VALUE, intval); |
34dc7c2f | 293 | |
65ad5d11 | 294 | fnvlist_add_nvlist(nvl, propname, propval); |
34dc7c2f BB |
295 | nvlist_free(propval); |
296 | } | |
297 | ||
298 | /* | |
299 | * Get property values from the spa configuration. | |
300 | */ | |
301 | static void | |
302 | spa_prop_get_config(spa_t *spa, nvlist_t **nvp) | |
303 | { | |
1bd201e7 | 304 | vdev_t *rvd = spa->spa_root_vdev; |
9ae529ec | 305 | dsl_pool_t *pool = spa->spa_dsl_pool; |
f3a7f661 | 306 | uint64_t size, alloc, cap, version; |
82ab6848 | 307 | const zprop_source_t src = ZPROP_SRC_NONE; |
b128c09f | 308 | spa_config_dirent_t *dp; |
f3a7f661 | 309 | metaslab_class_t *mc = spa_normal_class(spa); |
b128c09f BB |
310 | |
311 | ASSERT(MUTEX_HELD(&spa->spa_props_lock)); | |
34dc7c2f | 312 | |
1bd201e7 | 313 | if (rvd != NULL) { |
cc99f275 DB |
314 | alloc = metaslab_class_get_alloc(mc); |
315 | alloc += metaslab_class_get_alloc(spa_special_class(spa)); | |
316 | alloc += metaslab_class_get_alloc(spa_dedup_class(spa)); | |
aa755b35 | 317 | alloc += metaslab_class_get_alloc(spa_embedded_log_class(spa)); |
cc99f275 DB |
318 | |
319 | size = metaslab_class_get_space(mc); | |
320 | size += metaslab_class_get_space(spa_special_class(spa)); | |
321 | size += metaslab_class_get_space(spa_dedup_class(spa)); | |
aa755b35 | 322 | size += metaslab_class_get_space(spa_embedded_log_class(spa)); |
cc99f275 | 323 | |
d164b209 BB |
324 | spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); |
325 | spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); | |
428870ff BB |
326 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); |
327 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, | |
328 | size - alloc, src); | |
d2734cce SD |
329 | spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL, |
330 | spa->spa_checkpoint_info.sci_dspace, src); | |
1bd201e7 | 331 | |
f3a7f661 GW |
332 | spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, |
333 | metaslab_class_fragmentation(mc), src); | |
334 | spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, | |
335 | metaslab_class_expandable_space(mc), src); | |
572e2857 | 336 | spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, |
da92d5cb | 337 | (spa_mode(spa) == SPA_MODE_READ), src); |
d164b209 | 338 | |
428870ff | 339 | cap = (size == 0) ? 0 : (alloc * 100 / size); |
d164b209 BB |
340 | spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); |
341 | ||
428870ff BB |
342 | spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, |
343 | ddt_get_pool_dedup_ratio(spa), src); | |
344 | ||
d164b209 | 345 | spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, |
1bd201e7 | 346 | rvd->vdev_state, src); |
d164b209 BB |
347 | |
348 | version = spa_version(spa); | |
82ab6848 HM |
349 | if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) { |
350 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
351 | version, ZPROP_SRC_DEFAULT); | |
352 | } else { | |
353 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
354 | version, ZPROP_SRC_LOCAL); | |
355 | } | |
a448a255 SD |
356 | spa_prop_add_list(*nvp, ZPOOL_PROP_LOAD_GUID, |
357 | NULL, spa_load_guid(spa), src); | |
d164b209 | 358 | } |
34dc7c2f | 359 | |
9ae529ec | 360 | if (pool != NULL) { |
9ae529ec CS |
361 | /* |
362 | * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, | |
363 | * when opening pools before this version freedir will be NULL. | |
364 | */ | |
fbeddd60 | 365 | if (pool->dp_free_dir != NULL) { |
9ae529ec | 366 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, |
d683ddbb JG |
367 | dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, |
368 | src); | |
9ae529ec CS |
369 | } else { |
370 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, | |
371 | NULL, 0, src); | |
372 | } | |
fbeddd60 MA |
373 | |
374 | if (pool->dp_leak_dir != NULL) { | |
375 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, | |
d683ddbb JG |
376 | dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, |
377 | src); | |
fbeddd60 MA |
378 | } else { |
379 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, | |
380 | NULL, 0, src); | |
381 | } | |
9ae529ec CS |
382 | } |
383 | ||
34dc7c2f | 384 | spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); |
34dc7c2f | 385 | |
d96eb2b1 DM |
386 | if (spa->spa_comment != NULL) { |
387 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, | |
388 | 0, ZPROP_SRC_LOCAL); | |
389 | } | |
390 | ||
658fb802 CB |
391 | if (spa->spa_compatibility != NULL) { |
392 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMPATIBILITY, | |
393 | spa->spa_compatibility, 0, ZPROP_SRC_LOCAL); | |
394 | } | |
395 | ||
34dc7c2f BB |
396 | if (spa->spa_root != NULL) |
397 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, | |
398 | 0, ZPROP_SRC_LOCAL); | |
399 | ||
f1512ee6 MA |
400 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { |
401 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
402 | MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); | |
403 | } else { | |
404 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
405 | SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); | |
406 | } | |
407 | ||
50c957f7 NB |
408 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) { |
409 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
410 | DNODE_MAX_SIZE, ZPROP_SRC_NONE); | |
411 | } else { | |
412 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
413 | DNODE_MIN_SIZE, ZPROP_SRC_NONE); | |
414 | } | |
415 | ||
b128c09f BB |
416 | if ((dp = list_head(&spa->spa_config_list)) != NULL) { |
417 | if (dp->scd_path == NULL) { | |
34dc7c2f | 418 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f BB |
419 | "none", 0, ZPROP_SRC_LOCAL); |
420 | } else if (strcmp(dp->scd_path, spa_config_path) != 0) { | |
34dc7c2f | 421 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f | 422 | dp->scd_path, 0, ZPROP_SRC_LOCAL); |
34dc7c2f BB |
423 | } |
424 | } | |
425 | } | |
426 | ||
427 | /* | |
428 | * Get zpool property values. | |
429 | */ | |
430 | int | |
431 | spa_prop_get(spa_t *spa, nvlist_t **nvp) | |
432 | { | |
428870ff | 433 | objset_t *mos = spa->spa_meta_objset; |
34dc7c2f BB |
434 | zap_cursor_t zc; |
435 | zap_attribute_t za; | |
1743c737 | 436 | dsl_pool_t *dp; |
34dc7c2f BB |
437 | int err; |
438 | ||
79c76d5b | 439 | err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP); |
c28b2279 | 440 | if (err) |
d1d7e268 | 441 | return (err); |
34dc7c2f | 442 | |
1743c737 AM |
443 | dp = spa_get_dsl(spa); |
444 | dsl_pool_config_enter(dp, FTAG); | |
b128c09f BB |
445 | mutex_enter(&spa->spa_props_lock); |
446 | ||
34dc7c2f BB |
447 | /* |
448 | * Get properties from the spa config. | |
449 | */ | |
450 | spa_prop_get_config(spa, nvp); | |
451 | ||
34dc7c2f | 452 | /* If no pool property object, no more prop to get. */ |
1743c737 | 453 | if (mos == NULL || spa->spa_pool_props_object == 0) |
c28b2279 | 454 | goto out; |
34dc7c2f BB |
455 | |
456 | /* | |
457 | * Get properties from the MOS pool property object. | |
458 | */ | |
459 | for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); | |
460 | (err = zap_cursor_retrieve(&zc, &za)) == 0; | |
461 | zap_cursor_advance(&zc)) { | |
462 | uint64_t intval = 0; | |
463 | char *strval = NULL; | |
464 | zprop_source_t src = ZPROP_SRC_DEFAULT; | |
465 | zpool_prop_t prop; | |
466 | ||
31864e3d | 467 | if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL) |
34dc7c2f BB |
468 | continue; |
469 | ||
470 | switch (za.za_integer_length) { | |
471 | case 8: | |
472 | /* integer property */ | |
473 | if (za.za_first_integer != | |
474 | zpool_prop_default_numeric(prop)) | |
475 | src = ZPROP_SRC_LOCAL; | |
476 | ||
477 | if (prop == ZPOOL_PROP_BOOTFS) { | |
34dc7c2f BB |
478 | dsl_dataset_t *ds = NULL; |
479 | ||
619f0976 GW |
480 | err = dsl_dataset_hold_obj(dp, |
481 | za.za_first_integer, FTAG, &ds); | |
1743c737 | 482 | if (err != 0) |
34dc7c2f | 483 | break; |
34dc7c2f | 484 | |
eca7b760 | 485 | strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, |
79c76d5b | 486 | KM_SLEEP); |
34dc7c2f | 487 | dsl_dataset_name(ds, strval); |
b128c09f | 488 | dsl_dataset_rele(ds, FTAG); |
34dc7c2f BB |
489 | } else { |
490 | strval = NULL; | |
491 | intval = za.za_first_integer; | |
492 | } | |
493 | ||
494 | spa_prop_add_list(*nvp, prop, strval, intval, src); | |
495 | ||
496 | if (strval != NULL) | |
eca7b760 | 497 | kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); |
34dc7c2f BB |
498 | |
499 | break; | |
500 | ||
501 | case 1: | |
502 | /* string property */ | |
79c76d5b | 503 | strval = kmem_alloc(za.za_num_integers, KM_SLEEP); |
34dc7c2f BB |
504 | err = zap_lookup(mos, spa->spa_pool_props_object, |
505 | za.za_name, 1, za.za_num_integers, strval); | |
506 | if (err) { | |
507 | kmem_free(strval, za.za_num_integers); | |
508 | break; | |
509 | } | |
510 | spa_prop_add_list(*nvp, prop, strval, 0, src); | |
511 | kmem_free(strval, za.za_num_integers); | |
512 | break; | |
513 | ||
514 | default: | |
515 | break; | |
516 | } | |
517 | } | |
518 | zap_cursor_fini(&zc); | |
34dc7c2f | 519 | out: |
1743c737 AM |
520 | mutex_exit(&spa->spa_props_lock); |
521 | dsl_pool_config_exit(dp, FTAG); | |
34dc7c2f BB |
522 | if (err && err != ENOENT) { |
523 | nvlist_free(*nvp); | |
524 | *nvp = NULL; | |
525 | return (err); | |
526 | } | |
527 | ||
528 | return (0); | |
529 | } | |
530 | ||
531 | /* | |
532 | * Validate the given pool properties nvlist and modify the list | |
533 | * for the property values to be set. | |
534 | */ | |
535 | static int | |
536 | spa_prop_validate(spa_t *spa, nvlist_t *props) | |
537 | { | |
538 | nvpair_t *elem; | |
539 | int error = 0, reset_bootfs = 0; | |
d4ed6673 | 540 | uint64_t objnum = 0; |
9ae529ec | 541 | boolean_t has_feature = B_FALSE; |
34dc7c2f BB |
542 | |
543 | elem = NULL; | |
544 | while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { | |
34dc7c2f | 545 | uint64_t intval; |
9ae529ec CS |
546 | char *strval, *slash, *check, *fname; |
547 | const char *propname = nvpair_name(elem); | |
548 | zpool_prop_t prop = zpool_name_to_prop(propname); | |
549 | ||
31864e3d BB |
550 | switch (prop) { |
551 | case ZPOOL_PROP_INVAL: | |
9ae529ec | 552 | if (!zpool_prop_feature(propname)) { |
2e528b49 | 553 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
554 | break; |
555 | } | |
556 | ||
557 | /* | |
558 | * Sanitize the input. | |
559 | */ | |
560 | if (nvpair_type(elem) != DATA_TYPE_UINT64) { | |
2e528b49 | 561 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
562 | break; |
563 | } | |
564 | ||
565 | if (nvpair_value_uint64(elem, &intval) != 0) { | |
2e528b49 | 566 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
567 | break; |
568 | } | |
34dc7c2f | 569 | |
9ae529ec | 570 | if (intval != 0) { |
2e528b49 | 571 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
572 | break; |
573 | } | |
34dc7c2f | 574 | |
9ae529ec CS |
575 | fname = strchr(propname, '@') + 1; |
576 | if (zfeature_lookup_name(fname, NULL) != 0) { | |
2e528b49 | 577 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
578 | break; |
579 | } | |
580 | ||
581 | has_feature = B_TRUE; | |
582 | break; | |
34dc7c2f | 583 | |
34dc7c2f BB |
584 | case ZPOOL_PROP_VERSION: |
585 | error = nvpair_value_uint64(elem, &intval); | |
586 | if (!error && | |
9ae529ec CS |
587 | (intval < spa_version(spa) || |
588 | intval > SPA_VERSION_BEFORE_FEATURES || | |
589 | has_feature)) | |
2e528b49 | 590 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
591 | break; |
592 | ||
593 | case ZPOOL_PROP_DELEGATION: | |
594 | case ZPOOL_PROP_AUTOREPLACE: | |
b128c09f | 595 | case ZPOOL_PROP_LISTSNAPS: |
9babb374 | 596 | case ZPOOL_PROP_AUTOEXPAND: |
1b939560 | 597 | case ZPOOL_PROP_AUTOTRIM: |
34dc7c2f BB |
598 | error = nvpair_value_uint64(elem, &intval); |
599 | if (!error && intval > 1) | |
2e528b49 | 600 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
601 | break; |
602 | ||
379ca9cf OF |
603 | case ZPOOL_PROP_MULTIHOST: |
604 | error = nvpair_value_uint64(elem, &intval); | |
605 | if (!error && intval > 1) | |
606 | error = SET_ERROR(EINVAL); | |
607 | ||
25f06d67 BB |
608 | if (!error) { |
609 | uint32_t hostid = zone_get_hostid(NULL); | |
610 | if (hostid) | |
611 | spa->spa_hostid = hostid; | |
612 | else | |
613 | error = SET_ERROR(ENOTSUP); | |
614 | } | |
379ca9cf OF |
615 | |
616 | break; | |
617 | ||
34dc7c2f | 618 | case ZPOOL_PROP_BOOTFS: |
9babb374 BB |
619 | /* |
620 | * If the pool version is less than SPA_VERSION_BOOTFS, | |
621 | * or the pool is still being created (version == 0), | |
622 | * the bootfs property cannot be set. | |
623 | */ | |
34dc7c2f | 624 | if (spa_version(spa) < SPA_VERSION_BOOTFS) { |
2e528b49 | 625 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
626 | break; |
627 | } | |
628 | ||
629 | /* | |
b128c09f | 630 | * Make sure the vdev config is bootable |
34dc7c2f | 631 | */ |
b128c09f | 632 | if (!vdev_is_bootable(spa->spa_root_vdev)) { |
2e528b49 | 633 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
634 | break; |
635 | } | |
636 | ||
637 | reset_bootfs = 1; | |
638 | ||
639 | error = nvpair_value_string(elem, &strval); | |
640 | ||
641 | if (!error) { | |
9ae529ec | 642 | objset_t *os; |
b128c09f | 643 | |
34dc7c2f BB |
644 | if (strval == NULL || strval[0] == '\0') { |
645 | objnum = zpool_prop_default_numeric( | |
646 | ZPOOL_PROP_BOOTFS); | |
647 | break; | |
648 | } | |
649 | ||
d1d7e268 | 650 | error = dmu_objset_hold(strval, FTAG, &os); |
619f0976 | 651 | if (error != 0) |
34dc7c2f | 652 | break; |
b128c09f | 653 | |
eaa25f1a | 654 | /* Must be ZPL. */ |
428870ff | 655 | if (dmu_objset_type(os) != DMU_OST_ZFS) { |
2e528b49 | 656 | error = SET_ERROR(ENOTSUP); |
b128c09f BB |
657 | } else { |
658 | objnum = dmu_objset_id(os); | |
659 | } | |
428870ff | 660 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
661 | } |
662 | break; | |
b128c09f | 663 | |
34dc7c2f BB |
664 | case ZPOOL_PROP_FAILUREMODE: |
665 | error = nvpair_value_uint64(elem, &intval); | |
3bfd95d5 | 666 | if (!error && intval > ZIO_FAILURE_MODE_PANIC) |
2e528b49 | 667 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
668 | |
669 | /* | |
670 | * This is a special case which only occurs when | |
671 | * the pool has completely failed. This allows | |
672 | * the user to change the in-core failmode property | |
673 | * without syncing it out to disk (I/Os might | |
674 | * currently be blocked). We do this by returning | |
675 | * EIO to the caller (spa_prop_set) to trick it | |
676 | * into thinking we encountered a property validation | |
677 | * error. | |
678 | */ | |
b128c09f | 679 | if (!error && spa_suspended(spa)) { |
34dc7c2f | 680 | spa->spa_failmode = intval; |
2e528b49 | 681 | error = SET_ERROR(EIO); |
34dc7c2f BB |
682 | } |
683 | break; | |
684 | ||
685 | case ZPOOL_PROP_CACHEFILE: | |
686 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
687 | break; | |
688 | ||
689 | if (strval[0] == '\0') | |
690 | break; | |
691 | ||
692 | if (strcmp(strval, "none") == 0) | |
693 | break; | |
694 | ||
695 | if (strval[0] != '/') { | |
2e528b49 | 696 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
697 | break; |
698 | } | |
699 | ||
700 | slash = strrchr(strval, '/'); | |
701 | ASSERT(slash != NULL); | |
702 | ||
703 | if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || | |
704 | strcmp(slash, "/..") == 0) | |
2e528b49 | 705 | error = SET_ERROR(EINVAL); |
34dc7c2f | 706 | break; |
428870ff | 707 | |
d96eb2b1 DM |
708 | case ZPOOL_PROP_COMMENT: |
709 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
710 | break; | |
711 | for (check = strval; *check != '\0'; check++) { | |
712 | if (!isprint(*check)) { | |
2e528b49 | 713 | error = SET_ERROR(EINVAL); |
d96eb2b1 DM |
714 | break; |
715 | } | |
d96eb2b1 DM |
716 | } |
717 | if (strlen(strval) > ZPROP_MAX_COMMENT) | |
2e528b49 | 718 | error = SET_ERROR(E2BIG); |
d96eb2b1 DM |
719 | break; |
720 | ||
e75c13c3 BB |
721 | default: |
722 | break; | |
34dc7c2f BB |
723 | } |
724 | ||
725 | if (error) | |
726 | break; | |
727 | } | |
728 | ||
050d720c MA |
729 | (void) nvlist_remove_all(props, |
730 | zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO)); | |
731 | ||
34dc7c2f BB |
732 | if (!error && reset_bootfs) { |
733 | error = nvlist_remove(props, | |
734 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); | |
735 | ||
736 | if (!error) { | |
737 | error = nvlist_add_uint64(props, | |
738 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); | |
739 | } | |
740 | } | |
741 | ||
742 | return (error); | |
743 | } | |
744 | ||
d164b209 BB |
745 | void |
746 | spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) | |
747 | { | |
748 | char *cachefile; | |
749 | spa_config_dirent_t *dp; | |
750 | ||
751 | if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
752 | &cachefile) != 0) | |
753 | return; | |
754 | ||
755 | dp = kmem_alloc(sizeof (spa_config_dirent_t), | |
79c76d5b | 756 | KM_SLEEP); |
d164b209 BB |
757 | |
758 | if (cachefile[0] == '\0') | |
759 | dp->scd_path = spa_strdup(spa_config_path); | |
760 | else if (strcmp(cachefile, "none") == 0) | |
761 | dp->scd_path = NULL; | |
762 | else | |
763 | dp->scd_path = spa_strdup(cachefile); | |
764 | ||
765 | list_insert_head(&spa->spa_config_list, dp); | |
766 | if (need_sync) | |
767 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
768 | } | |
769 | ||
34dc7c2f BB |
770 | int |
771 | spa_prop_set(spa_t *spa, nvlist_t *nvp) | |
772 | { | |
773 | int error; | |
9ae529ec | 774 | nvpair_t *elem = NULL; |
d164b209 | 775 | boolean_t need_sync = B_FALSE; |
34dc7c2f BB |
776 | |
777 | if ((error = spa_prop_validate(spa, nvp)) != 0) | |
778 | return (error); | |
779 | ||
d164b209 | 780 | while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { |
9ae529ec | 781 | zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); |
d164b209 | 782 | |
572e2857 BB |
783 | if (prop == ZPOOL_PROP_CACHEFILE || |
784 | prop == ZPOOL_PROP_ALTROOT || | |
785 | prop == ZPOOL_PROP_READONLY) | |
d164b209 BB |
786 | continue; |
787 | ||
31864e3d | 788 | if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { |
2a673e76 | 789 | uint64_t ver = 0; |
9ae529ec CS |
790 | |
791 | if (prop == ZPOOL_PROP_VERSION) { | |
792 | VERIFY(nvpair_value_uint64(elem, &ver) == 0); | |
793 | } else { | |
794 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
795 | ver = SPA_VERSION_FEATURES; | |
796 | need_sync = B_TRUE; | |
797 | } | |
798 | ||
799 | /* Save time if the version is already set. */ | |
800 | if (ver == spa_version(spa)) | |
801 | continue; | |
802 | ||
803 | /* | |
804 | * In addition to the pool directory object, we might | |
805 | * create the pool properties object, the features for | |
806 | * read object, the features for write object, or the | |
807 | * feature descriptions object. | |
808 | */ | |
13fe0198 | 809 | error = dsl_sync_task(spa->spa_name, NULL, |
3d45fdd6 MA |
810 | spa_sync_version, &ver, |
811 | 6, ZFS_SPACE_CHECK_RESERVED); | |
9ae529ec CS |
812 | if (error) |
813 | return (error); | |
814 | continue; | |
815 | } | |
816 | ||
d164b209 BB |
817 | need_sync = B_TRUE; |
818 | break; | |
819 | } | |
820 | ||
9ae529ec | 821 | if (need_sync) { |
13fe0198 | 822 | return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, |
3d45fdd6 | 823 | nvp, 6, ZFS_SPACE_CHECK_RESERVED)); |
9ae529ec CS |
824 | } |
825 | ||
826 | return (0); | |
34dc7c2f BB |
827 | } |
828 | ||
829 | /* | |
830 | * If the bootfs property value is dsobj, clear it. | |
831 | */ | |
832 | void | |
833 | spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) | |
834 | { | |
835 | if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { | |
836 | VERIFY(zap_remove(spa->spa_meta_objset, | |
837 | spa->spa_pool_props_object, | |
838 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); | |
839 | spa->spa_bootfs = 0; | |
840 | } | |
841 | } | |
842 | ||
3bc7e0fb | 843 | static int |
13fe0198 | 844 | spa_change_guid_check(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 845 | { |
2a8ba608 | 846 | uint64_t *newguid __maybe_unused = arg; |
13fe0198 | 847 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; |
3bc7e0fb GW |
848 | vdev_t *rvd = spa->spa_root_vdev; |
849 | uint64_t vdev_state; | |
3bc7e0fb | 850 | |
d2734cce SD |
851 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { |
852 | int error = (spa_has_checkpoint(spa)) ? | |
853 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
854 | return (SET_ERROR(error)); | |
855 | } | |
856 | ||
3bc7e0fb GW |
857 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
858 | vdev_state = rvd->vdev_state; | |
859 | spa_config_exit(spa, SCL_STATE, FTAG); | |
860 | ||
861 | if (vdev_state != VDEV_STATE_HEALTHY) | |
2e528b49 | 862 | return (SET_ERROR(ENXIO)); |
3bc7e0fb GW |
863 | |
864 | ASSERT3U(spa_guid(spa), !=, *newguid); | |
865 | ||
866 | return (0); | |
867 | } | |
868 | ||
869 | static void | |
13fe0198 | 870 | spa_change_guid_sync(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 871 | { |
13fe0198 MA |
872 | uint64_t *newguid = arg; |
873 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
3bc7e0fb GW |
874 | uint64_t oldguid; |
875 | vdev_t *rvd = spa->spa_root_vdev; | |
876 | ||
877 | oldguid = spa_guid(spa); | |
878 | ||
879 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
880 | rvd->vdev_guid = *newguid; | |
881 | rvd->vdev_guid_sum += (*newguid - oldguid); | |
882 | vdev_config_dirty(rvd); | |
883 | spa_config_exit(spa, SCL_STATE, FTAG); | |
884 | ||
6f1ffb06 | 885 | spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", |
74756182 | 886 | (u_longlong_t)oldguid, (u_longlong_t)*newguid); |
3bc7e0fb GW |
887 | } |
888 | ||
3541dc6d GA |
889 | /* |
890 | * Change the GUID for the pool. This is done so that we can later | |
891 | * re-import a pool built from a clone of our own vdevs. We will modify | |
892 | * the root vdev's guid, our own pool guid, and then mark all of our | |
893 | * vdevs dirty. Note that we must make sure that all our vdevs are | |
894 | * online when we do this, or else any vdevs that weren't present | |
895 | * would be orphaned from our pool. We are also going to issue a | |
896 | * sysevent to update any watchers. | |
897 | */ | |
898 | int | |
899 | spa_change_guid(spa_t *spa) | |
900 | { | |
3bc7e0fb GW |
901 | int error; |
902 | uint64_t guid; | |
3541dc6d | 903 | |
621dd7bb | 904 | mutex_enter(&spa->spa_vdev_top_lock); |
3bc7e0fb GW |
905 | mutex_enter(&spa_namespace_lock); |
906 | guid = spa_generate_guid(NULL); | |
3541dc6d | 907 | |
13fe0198 | 908 | error = dsl_sync_task(spa->spa_name, spa_change_guid_check, |
3d45fdd6 | 909 | spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); |
3541dc6d | 910 | |
3bc7e0fb | 911 | if (error == 0) { |
55c12724 AH |
912 | /* |
913 | * Clear the kobj flag from all the vdevs to allow | |
914 | * vdev_cache_process_kobj_evt() to post events to all the | |
915 | * vdevs since GUID is updated. | |
916 | */ | |
917 | vdev_clear_kobj_evt(spa->spa_root_vdev); | |
918 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) | |
919 | vdev_clear_kobj_evt(spa->spa_l2cache.sav_vdevs[i]); | |
920 | ||
921 | spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE); | |
12fa0466 | 922 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); |
3bc7e0fb | 923 | } |
3541dc6d | 924 | |
3bc7e0fb | 925 | mutex_exit(&spa_namespace_lock); |
621dd7bb | 926 | mutex_exit(&spa->spa_vdev_top_lock); |
3541dc6d | 927 | |
3bc7e0fb | 928 | return (error); |
3541dc6d GA |
929 | } |
930 | ||
34dc7c2f BB |
931 | /* |
932 | * ========================================================================== | |
933 | * SPA state manipulation (open/create/destroy/import/export) | |
934 | * ========================================================================== | |
935 | */ | |
936 | ||
937 | static int | |
938 | spa_error_entry_compare(const void *a, const void *b) | |
939 | { | |
ee36c709 GN |
940 | const spa_error_entry_t *sa = (const spa_error_entry_t *)a; |
941 | const spa_error_entry_t *sb = (const spa_error_entry_t *)b; | |
34dc7c2f BB |
942 | int ret; |
943 | ||
ee36c709 | 944 | ret = memcmp(&sa->se_bookmark, &sb->se_bookmark, |
5dbd68a3 | 945 | sizeof (zbookmark_phys_t)); |
34dc7c2f | 946 | |
ca577779 | 947 | return (TREE_ISIGN(ret)); |
34dc7c2f BB |
948 | } |
949 | ||
950 | /* | |
951 | * Utility function which retrieves copies of the current logs and | |
952 | * re-initializes them in the process. | |
953 | */ | |
954 | void | |
955 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
956 | { | |
957 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
958 | ||
861166b0 AZ |
959 | memcpy(last, &spa->spa_errlist_last, sizeof (avl_tree_t)); |
960 | memcpy(scrub, &spa->spa_errlist_scrub, sizeof (avl_tree_t)); | |
34dc7c2f BB |
961 | |
962 | avl_create(&spa->spa_errlist_scrub, | |
963 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
964 | offsetof(spa_error_entry_t, se_avl)); | |
965 | avl_create(&spa->spa_errlist_last, | |
966 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
967 | offsetof(spa_error_entry_t, se_avl)); | |
968 | } | |
969 | ||
7ef5e54e AL |
970 | static void |
971 | spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
34dc7c2f | 972 | { |
7ef5e54e AL |
973 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; |
974 | enum zti_modes mode = ztip->zti_mode; | |
975 | uint_t value = ztip->zti_value; | |
976 | uint_t count = ztip->zti_count; | |
977 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7457b024 | 978 | uint_t cpus, flags = TASKQ_DYNAMIC; |
428870ff | 979 | boolean_t batch = B_FALSE; |
34dc7c2f | 980 | |
e8b96c60 MA |
981 | switch (mode) { |
982 | case ZTI_MODE_FIXED: | |
7457b024 | 983 | ASSERT3U(value, >, 0); |
e8b96c60 | 984 | break; |
7ef5e54e | 985 | |
e8b96c60 MA |
986 | case ZTI_MODE_BATCH: |
987 | batch = B_TRUE; | |
988 | flags |= TASKQ_THREADS_CPU_PCT; | |
dcb6bed1 | 989 | value = MIN(zio_taskq_batch_pct, 100); |
e8b96c60 | 990 | break; |
7ef5e54e | 991 | |
7457b024 AM |
992 | case ZTI_MODE_SCALE: |
993 | flags |= TASKQ_THREADS_CPU_PCT; | |
994 | /* | |
995 | * We want more taskqs to reduce lock contention, but we want | |
996 | * less for better request ordering and CPU utilization. | |
997 | */ | |
998 | cpus = MAX(1, boot_ncpus * zio_taskq_batch_pct / 100); | |
999 | if (zio_taskq_batch_tpq > 0) { | |
1000 | count = MAX(1, (cpus + zio_taskq_batch_tpq / 2) / | |
1001 | zio_taskq_batch_tpq); | |
1002 | } else { | |
1003 | /* | |
1004 | * Prefer 6 threads per taskq, but no more taskqs | |
1005 | * than threads in them on large systems. For 80%: | |
1006 | * | |
1007 | * taskq taskq total | |
1008 | * cpus taskqs percent threads threads | |
1009 | * ------- ------- ------- ------- ------- | |
1010 | * 1 1 80% 1 1 | |
1011 | * 2 1 80% 1 1 | |
1012 | * 4 1 80% 3 3 | |
1013 | * 8 2 40% 3 6 | |
1014 | * 16 3 27% 4 12 | |
1015 | * 32 5 16% 5 25 | |
1016 | * 64 7 11% 7 49 | |
1017 | * 128 10 8% 10 100 | |
1018 | * 256 14 6% 15 210 | |
1019 | */ | |
1020 | count = 1 + cpus / 6; | |
1021 | while (count * count > cpus) | |
1022 | count--; | |
1023 | } | |
1024 | /* Limit each taskq within 100% to not trigger assertion. */ | |
1025 | count = MAX(count, (zio_taskq_batch_pct + 99) / 100); | |
1026 | value = (zio_taskq_batch_pct + count / 2) / count; | |
1027 | break; | |
1028 | ||
1029 | case ZTI_MODE_NULL: | |
1030 | tqs->stqs_count = 0; | |
1031 | tqs->stqs_taskq = NULL; | |
1032 | return; | |
1033 | ||
e8b96c60 MA |
1034 | default: |
1035 | panic("unrecognized mode for %s_%s taskq (%u:%u) in " | |
1036 | "spa_activate()", | |
1037 | zio_type_name[t], zio_taskq_types[q], mode, value); | |
1038 | break; | |
1039 | } | |
7ef5e54e | 1040 | |
7457b024 AM |
1041 | ASSERT3U(count, >, 0); |
1042 | tqs->stqs_count = count; | |
1043 | tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); | |
1044 | ||
1c27024e | 1045 | for (uint_t i = 0; i < count; i++) { |
e8b96c60 | 1046 | taskq_t *tq; |
af430294 | 1047 | char name[32]; |
7ef5e54e | 1048 | |
7457b024 AM |
1049 | if (count > 1) |
1050 | (void) snprintf(name, sizeof (name), "%s_%s_%u", | |
1051 | zio_type_name[t], zio_taskq_types[q], i); | |
1052 | else | |
1053 | (void) snprintf(name, sizeof (name), "%s_%s", | |
1054 | zio_type_name[t], zio_taskq_types[q]); | |
7ef5e54e AL |
1055 | |
1056 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { | |
1057 | if (batch) | |
1058 | flags |= TASKQ_DC_BATCH; | |
1059 | ||
18168da7 | 1060 | (void) zio_taskq_basedc; |
7ef5e54e AL |
1061 | tq = taskq_create_sysdc(name, value, 50, INT_MAX, |
1062 | spa->spa_proc, zio_taskq_basedc, flags); | |
1063 | } else { | |
e8b96c60 MA |
1064 | pri_t pri = maxclsyspri; |
1065 | /* | |
1066 | * The write issue taskq can be extremely CPU | |
1229323d | 1067 | * intensive. Run it at slightly less important |
7432d297 MM |
1068 | * priority than the other taskqs. |
1069 | * | |
1070 | * Under Linux and FreeBSD this means incrementing | |
1071 | * the priority value as opposed to platforms like | |
1072 | * illumos where it should be decremented. | |
1073 | * | |
1074 | * On FreeBSD, if priorities divided by four (RQ_PPQ) | |
1075 | * are equal then a difference between them is | |
1076 | * insignificant. | |
e8b96c60 | 1077 | */ |
7432d297 MM |
1078 | if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) { |
1079 | #if defined(__linux__) | |
1229323d | 1080 | pri++; |
7432d297 MM |
1081 | #elif defined(__FreeBSD__) |
1082 | pri += 4; | |
1083 | #else | |
1084 | #error "unknown OS" | |
1085 | #endif | |
1086 | } | |
e8b96c60 | 1087 | tq = taskq_create_proc(name, value, pri, 50, |
7ef5e54e AL |
1088 | INT_MAX, spa->spa_proc, flags); |
1089 | } | |
1090 | ||
1091 | tqs->stqs_taskq[i] = tq; | |
1092 | } | |
1093 | } | |
1094 | ||
1095 | static void | |
1096 | spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
1097 | { | |
1098 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7ef5e54e AL |
1099 | |
1100 | if (tqs->stqs_taskq == NULL) { | |
1101 | ASSERT3U(tqs->stqs_count, ==, 0); | |
1102 | return; | |
1103 | } | |
1104 | ||
1c27024e | 1105 | for (uint_t i = 0; i < tqs->stqs_count; i++) { |
7ef5e54e AL |
1106 | ASSERT3P(tqs->stqs_taskq[i], !=, NULL); |
1107 | taskq_destroy(tqs->stqs_taskq[i]); | |
428870ff | 1108 | } |
34dc7c2f | 1109 | |
7ef5e54e AL |
1110 | kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); |
1111 | tqs->stqs_taskq = NULL; | |
1112 | } | |
34dc7c2f | 1113 | |
7ef5e54e AL |
1114 | /* |
1115 | * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. | |
1116 | * Note that a type may have multiple discrete taskqs to avoid lock contention | |
1117 | * on the taskq itself. In that case we choose which taskq at random by using | |
1118 | * the low bits of gethrtime(). | |
1119 | */ | |
1120 | void | |
1121 | spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1122 | task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) | |
1123 | { | |
1124 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1125 | taskq_t *tq; | |
1126 | ||
1127 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1128 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1129 | ||
1130 | if (tqs->stqs_count == 1) { | |
1131 | tq = tqs->stqs_taskq[0]; | |
1132 | } else { | |
c12936b1 | 1133 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
428870ff | 1134 | } |
7ef5e54e AL |
1135 | |
1136 | taskq_dispatch_ent(tq, func, arg, flags, ent); | |
428870ff BB |
1137 | } |
1138 | ||
044baf00 BB |
1139 | /* |
1140 | * Same as spa_taskq_dispatch_ent() but block on the task until completion. | |
1141 | */ | |
1142 | void | |
1143 | spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1144 | task_func_t *func, void *arg, uint_t flags) | |
1145 | { | |
1146 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1147 | taskq_t *tq; | |
1148 | taskqid_t id; | |
1149 | ||
1150 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1151 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1152 | ||
1153 | if (tqs->stqs_count == 1) { | |
1154 | tq = tqs->stqs_taskq[0]; | |
1155 | } else { | |
c12936b1 | 1156 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
044baf00 BB |
1157 | } |
1158 | ||
1159 | id = taskq_dispatch(tq, func, arg, flags); | |
1160 | if (id) | |
1161 | taskq_wait_id(tq, id); | |
1162 | } | |
1163 | ||
428870ff BB |
1164 | static void |
1165 | spa_create_zio_taskqs(spa_t *spa) | |
1166 | { | |
1c27024e DB |
1167 | for (int t = 0; t < ZIO_TYPES; t++) { |
1168 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1169 | spa_taskqs_init(spa, t, q); |
428870ff BB |
1170 | } |
1171 | } | |
1172 | } | |
9babb374 | 1173 | |
c25b8f99 BB |
1174 | /* |
1175 | * Disabled until spa_thread() can be adapted for Linux. | |
1176 | */ | |
1177 | #undef HAVE_SPA_THREAD | |
1178 | ||
7b89a549 | 1179 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) |
428870ff BB |
1180 | static void |
1181 | spa_thread(void *arg) | |
1182 | { | |
93ce2b4c | 1183 | psetid_t zio_taskq_psrset_bind = PS_NONE; |
428870ff | 1184 | callb_cpr_t cprinfo; |
9babb374 | 1185 | |
428870ff BB |
1186 | spa_t *spa = arg; |
1187 | user_t *pu = PTOU(curproc); | |
9babb374 | 1188 | |
428870ff BB |
1189 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, |
1190 | spa->spa_name); | |
9babb374 | 1191 | |
428870ff BB |
1192 | ASSERT(curproc != &p0); |
1193 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
1194 | "zpool-%s", spa->spa_name); | |
1195 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
1196 | ||
1197 | /* bind this thread to the requested psrset */ | |
1198 | if (zio_taskq_psrset_bind != PS_NONE) { | |
1199 | pool_lock(); | |
1200 | mutex_enter(&cpu_lock); | |
1201 | mutex_enter(&pidlock); | |
1202 | mutex_enter(&curproc->p_lock); | |
1203 | ||
1204 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
1205 | 0, NULL, NULL) == 0) { | |
1206 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
1207 | } else { | |
1208 | cmn_err(CE_WARN, | |
1209 | "Couldn't bind process for zfs pool \"%s\" to " | |
1210 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
1211 | } | |
1212 | ||
1213 | mutex_exit(&curproc->p_lock); | |
1214 | mutex_exit(&pidlock); | |
1215 | mutex_exit(&cpu_lock); | |
1216 | pool_unlock(); | |
1217 | } | |
1218 | ||
1219 | if (zio_taskq_sysdc) { | |
1220 | sysdc_thread_enter(curthread, 100, 0); | |
1221 | } | |
1222 | ||
1223 | spa->spa_proc = curproc; | |
1224 | spa->spa_did = curthread->t_did; | |
1225 | ||
1226 | spa_create_zio_taskqs(spa); | |
1227 | ||
1228 | mutex_enter(&spa->spa_proc_lock); | |
1229 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
1230 | ||
1231 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
1232 | cv_broadcast(&spa->spa_proc_cv); | |
1233 | ||
1234 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
1235 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
1236 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1237 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
1238 | ||
1239 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
1240 | spa->spa_proc_state = SPA_PROC_GONE; | |
1241 | spa->spa_proc = &p0; | |
1242 | cv_broadcast(&spa->spa_proc_cv); | |
1243 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
1244 | ||
1245 | mutex_enter(&curproc->p_lock); | |
1246 | lwp_exit(); | |
1247 | } | |
1248 | #endif | |
1249 | ||
1250 | /* | |
1251 | * Activate an uninitialized pool. | |
1252 | */ | |
1253 | static void | |
da92d5cb | 1254 | spa_activate(spa_t *spa, spa_mode_t mode) |
428870ff BB |
1255 | { |
1256 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); | |
1257 | ||
1258 | spa->spa_state = POOL_STATE_ACTIVE; | |
1259 | spa->spa_mode = mode; | |
e39fe05b | 1260 | spa->spa_read_spacemaps = spa_mode_readable_spacemaps; |
428870ff | 1261 | |
18168da7 AZ |
1262 | spa->spa_normal_class = metaslab_class_create(spa, &zfs_metaslab_ops); |
1263 | spa->spa_log_class = metaslab_class_create(spa, &zfs_metaslab_ops); | |
aa755b35 | 1264 | spa->spa_embedded_log_class = |
18168da7 AZ |
1265 | metaslab_class_create(spa, &zfs_metaslab_ops); |
1266 | spa->spa_special_class = metaslab_class_create(spa, &zfs_metaslab_ops); | |
1267 | spa->spa_dedup_class = metaslab_class_create(spa, &zfs_metaslab_ops); | |
428870ff BB |
1268 | |
1269 | /* Try to create a covering process */ | |
1270 | mutex_enter(&spa->spa_proc_lock); | |
1271 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
1272 | ASSERT(spa->spa_proc == &p0); | |
1273 | spa->spa_did = 0; | |
1274 | ||
18168da7 | 1275 | (void) spa_create_process; |
7b89a549 | 1276 | #ifdef HAVE_SPA_THREAD |
428870ff BB |
1277 | /* Only create a process if we're going to be around a while. */ |
1278 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
1279 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
1280 | NULL, 0) == 0) { | |
1281 | spa->spa_proc_state = SPA_PROC_CREATED; | |
1282 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
1283 | cv_wait(&spa->spa_proc_cv, | |
1284 | &spa->spa_proc_lock); | |
9babb374 | 1285 | } |
428870ff BB |
1286 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); |
1287 | ASSERT(spa->spa_proc != &p0); | |
1288 | ASSERT(spa->spa_did != 0); | |
1289 | } else { | |
1290 | #ifdef _KERNEL | |
1291 | cmn_err(CE_WARN, | |
1292 | "Couldn't create process for zfs pool \"%s\"\n", | |
1293 | spa->spa_name); | |
1294 | #endif | |
b128c09f | 1295 | } |
34dc7c2f | 1296 | } |
7b89a549 | 1297 | #endif /* HAVE_SPA_THREAD */ |
428870ff BB |
1298 | mutex_exit(&spa->spa_proc_lock); |
1299 | ||
1300 | /* If we didn't create a process, we need to create our taskqs. */ | |
1301 | if (spa->spa_proc == &p0) { | |
1302 | spa_create_zio_taskqs(spa); | |
1303 | } | |
34dc7c2f | 1304 | |
619f0976 GW |
1305 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1306 | spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, | |
1307 | ZIO_FLAG_CANFAIL); | |
1308 | } | |
a1d477c2 | 1309 | |
b128c09f BB |
1310 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), |
1311 | offsetof(vdev_t, vdev_config_dirty_node)); | |
0c66c32d JG |
1312 | list_create(&spa->spa_evicting_os_list, sizeof (objset_t), |
1313 | offsetof(objset_t, os_evicting_node)); | |
b128c09f BB |
1314 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), |
1315 | offsetof(vdev_t, vdev_state_dirty_node)); | |
34dc7c2f | 1316 | |
4747a7d3 | 1317 | txg_list_create(&spa->spa_vdev_txg_list, spa, |
34dc7c2f BB |
1318 | offsetof(struct vdev, vdev_txg_node)); |
1319 | ||
1320 | avl_create(&spa->spa_errlist_scrub, | |
1321 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1322 | offsetof(spa_error_entry_t, se_avl)); | |
1323 | avl_create(&spa->spa_errlist_last, | |
1324 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1325 | offsetof(spa_error_entry_t, se_avl)); | |
e8cf3a4f AP |
1326 | avl_create(&spa->spa_errlist_healed, |
1327 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1328 | offsetof(spa_error_entry_t, se_avl)); | |
a0bd735a | 1329 | |
4759342a JL |
1330 | spa_activate_os(spa); |
1331 | ||
b5256303 TC |
1332 | spa_keystore_init(&spa->spa_keystore); |
1333 | ||
a0bd735a BP |
1334 | /* |
1335 | * This taskq is used to perform zvol-minor-related tasks | |
1336 | * asynchronously. This has several advantages, including easy | |
d0249a4b | 1337 | * resolution of various deadlocks. |
a0bd735a BP |
1338 | * |
1339 | * The taskq must be single threaded to ensure tasks are always | |
1340 | * processed in the order in which they were dispatched. | |
1341 | * | |
1342 | * A taskq per pool allows one to keep the pools independent. | |
1343 | * This way if one pool is suspended, it will not impact another. | |
1344 | * | |
1345 | * The preferred location to dispatch a zvol minor task is a sync | |
1346 | * task. In this context, there is easy access to the spa_t and minimal | |
1347 | * error handling is required because the sync task must succeed. | |
1348 | */ | |
1349 | spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri, | |
1350 | 1, INT_MAX, 0); | |
1de321e6 | 1351 | |
77d8a0f1 | 1352 | /* |
1353 | * Taskq dedicated to prefetcher threads: this is used to prevent the | |
1354 | * pool traverse code from monopolizing the global (and limited) | |
1355 | * system_taskq by inappropriately scheduling long running tasks on it. | |
1356 | */ | |
60a4c7d2 PD |
1357 | spa->spa_prefetch_taskq = taskq_create("z_prefetch", 100, |
1358 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); | |
77d8a0f1 | 1359 | |
1de321e6 JX |
1360 | /* |
1361 | * The taskq to upgrade datasets in this pool. Currently used by | |
9c5167d1 | 1362 | * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA. |
1de321e6 | 1363 | */ |
60a4c7d2 PD |
1364 | spa->spa_upgrade_taskq = taskq_create("z_upgrade", 100, |
1365 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); | |
34dc7c2f BB |
1366 | } |
1367 | ||
1368 | /* | |
1369 | * Opposite of spa_activate(). | |
1370 | */ | |
1371 | static void | |
1372 | spa_deactivate(spa_t *spa) | |
1373 | { | |
34dc7c2f BB |
1374 | ASSERT(spa->spa_sync_on == B_FALSE); |
1375 | ASSERT(spa->spa_dsl_pool == NULL); | |
1376 | ASSERT(spa->spa_root_vdev == NULL); | |
9babb374 | 1377 | ASSERT(spa->spa_async_zio_root == NULL); |
34dc7c2f BB |
1378 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); |
1379 | ||
0c66c32d JG |
1380 | spa_evicting_os_wait(spa); |
1381 | ||
a0bd735a BP |
1382 | if (spa->spa_zvol_taskq) { |
1383 | taskq_destroy(spa->spa_zvol_taskq); | |
1384 | spa->spa_zvol_taskq = NULL; | |
1385 | } | |
1386 | ||
77d8a0f1 | 1387 | if (spa->spa_prefetch_taskq) { |
1388 | taskq_destroy(spa->spa_prefetch_taskq); | |
1389 | spa->spa_prefetch_taskq = NULL; | |
1390 | } | |
1391 | ||
1de321e6 JX |
1392 | if (spa->spa_upgrade_taskq) { |
1393 | taskq_destroy(spa->spa_upgrade_taskq); | |
1394 | spa->spa_upgrade_taskq = NULL; | |
1395 | } | |
1396 | ||
34dc7c2f BB |
1397 | txg_list_destroy(&spa->spa_vdev_txg_list); |
1398 | ||
b128c09f | 1399 | list_destroy(&spa->spa_config_dirty_list); |
0c66c32d | 1400 | list_destroy(&spa->spa_evicting_os_list); |
b128c09f | 1401 | list_destroy(&spa->spa_state_dirty_list); |
34dc7c2f | 1402 | |
57ddcda1 | 1403 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 | 1404 | |
1c27024e DB |
1405 | for (int t = 0; t < ZIO_TYPES; t++) { |
1406 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1407 | spa_taskqs_fini(spa, t, q); |
b128c09f | 1408 | } |
34dc7c2f BB |
1409 | } |
1410 | ||
a1d477c2 MA |
1411 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1412 | ASSERT3P(spa->spa_txg_zio[i], !=, NULL); | |
1413 | VERIFY0(zio_wait(spa->spa_txg_zio[i])); | |
1414 | spa->spa_txg_zio[i] = NULL; | |
1415 | } | |
1416 | ||
34dc7c2f BB |
1417 | metaslab_class_destroy(spa->spa_normal_class); |
1418 | spa->spa_normal_class = NULL; | |
1419 | ||
1420 | metaslab_class_destroy(spa->spa_log_class); | |
1421 | spa->spa_log_class = NULL; | |
1422 | ||
aa755b35 MA |
1423 | metaslab_class_destroy(spa->spa_embedded_log_class); |
1424 | spa->spa_embedded_log_class = NULL; | |
1425 | ||
cc99f275 DB |
1426 | metaslab_class_destroy(spa->spa_special_class); |
1427 | spa->spa_special_class = NULL; | |
1428 | ||
1429 | metaslab_class_destroy(spa->spa_dedup_class); | |
1430 | spa->spa_dedup_class = NULL; | |
1431 | ||
34dc7c2f BB |
1432 | /* |
1433 | * If this was part of an import or the open otherwise failed, we may | |
1434 | * still have errors left in the queues. Empty them just in case. | |
1435 | */ | |
1436 | spa_errlog_drain(spa); | |
34dc7c2f BB |
1437 | avl_destroy(&spa->spa_errlist_scrub); |
1438 | avl_destroy(&spa->spa_errlist_last); | |
e8cf3a4f | 1439 | avl_destroy(&spa->spa_errlist_healed); |
34dc7c2f | 1440 | |
b5256303 TC |
1441 | spa_keystore_fini(&spa->spa_keystore); |
1442 | ||
34dc7c2f | 1443 | spa->spa_state = POOL_STATE_UNINITIALIZED; |
428870ff BB |
1444 | |
1445 | mutex_enter(&spa->spa_proc_lock); | |
1446 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1447 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1448 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1449 | cv_broadcast(&spa->spa_proc_cv); | |
1450 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1451 | ASSERT(spa->spa_proc != &p0); | |
1452 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1453 | } | |
1454 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1455 | spa->spa_proc_state = SPA_PROC_NONE; | |
1456 | } | |
1457 | ASSERT(spa->spa_proc == &p0); | |
1458 | mutex_exit(&spa->spa_proc_lock); | |
1459 | ||
1460 | /* | |
1461 | * We want to make sure spa_thread() has actually exited the ZFS | |
1462 | * module, so that the module can't be unloaded out from underneath | |
1463 | * it. | |
1464 | */ | |
1465 | if (spa->spa_did != 0) { | |
1466 | thread_join(spa->spa_did); | |
1467 | spa->spa_did = 0; | |
1468 | } | |
4759342a JL |
1469 | |
1470 | spa_deactivate_os(spa); | |
1471 | ||
34dc7c2f BB |
1472 | } |
1473 | ||
1474 | /* | |
1475 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1476 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1477 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1478 | * All vdev validation is done by the vdev_alloc() routine. | |
1479 | */ | |
4a22ba5b | 1480 | int |
34dc7c2f BB |
1481 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, |
1482 | uint_t id, int atype) | |
1483 | { | |
1484 | nvlist_t **child; | |
9babb374 | 1485 | uint_t children; |
34dc7c2f BB |
1486 | int error; |
1487 | ||
1488 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1489 | return (error); | |
1490 | ||
1491 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1492 | return (0); | |
1493 | ||
b128c09f BB |
1494 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1495 | &child, &children); | |
1496 | ||
1497 | if (error == ENOENT) | |
1498 | return (0); | |
1499 | ||
1500 | if (error) { | |
34dc7c2f BB |
1501 | vdev_free(*vdp); |
1502 | *vdp = NULL; | |
2e528b49 | 1503 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1504 | } |
1505 | ||
1c27024e | 1506 | for (int c = 0; c < children; c++) { |
34dc7c2f BB |
1507 | vdev_t *vd; |
1508 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1509 | atype)) != 0) { | |
1510 | vdev_free(*vdp); | |
1511 | *vdp = NULL; | |
1512 | return (error); | |
1513 | } | |
1514 | } | |
1515 | ||
1516 | ASSERT(*vdp != NULL); | |
1517 | ||
1518 | return (0); | |
1519 | } | |
1520 | ||
93e28d66 SD |
1521 | static boolean_t |
1522 | spa_should_flush_logs_on_unload(spa_t *spa) | |
1523 | { | |
1524 | if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) | |
1525 | return (B_FALSE); | |
1526 | ||
1527 | if (!spa_writeable(spa)) | |
1528 | return (B_FALSE); | |
1529 | ||
1530 | if (!spa->spa_sync_on) | |
1531 | return (B_FALSE); | |
1532 | ||
1533 | if (spa_state(spa) != POOL_STATE_EXPORTED) | |
1534 | return (B_FALSE); | |
1535 | ||
1536 | if (zfs_keep_log_spacemaps_at_export) | |
1537 | return (B_FALSE); | |
1538 | ||
1539 | return (B_TRUE); | |
1540 | } | |
1541 | ||
1542 | /* | |
1543 | * Opens a transaction that will set the flag that will instruct | |
1544 | * spa_sync to attempt to flush all the metaslabs for that txg. | |
1545 | */ | |
1546 | static void | |
1547 | spa_unload_log_sm_flush_all(spa_t *spa) | |
1548 | { | |
1549 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
1550 | VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); | |
1551 | ||
1552 | ASSERT3U(spa->spa_log_flushall_txg, ==, 0); | |
1553 | spa->spa_log_flushall_txg = dmu_tx_get_txg(tx); | |
1554 | ||
1555 | dmu_tx_commit(tx); | |
1556 | txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg); | |
1557 | } | |
1558 | ||
1559 | static void | |
1560 | spa_unload_log_sm_metadata(spa_t *spa) | |
1561 | { | |
1562 | void *cookie = NULL; | |
1563 | spa_log_sm_t *sls; | |
1564 | while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg, | |
1565 | &cookie)) != NULL) { | |
1566 | VERIFY0(sls->sls_mscount); | |
1567 | kmem_free(sls, sizeof (spa_log_sm_t)); | |
1568 | } | |
1569 | ||
1570 | for (log_summary_entry_t *e = list_head(&spa->spa_log_summary); | |
1571 | e != NULL; e = list_head(&spa->spa_log_summary)) { | |
1572 | VERIFY0(e->lse_mscount); | |
1573 | list_remove(&spa->spa_log_summary, e); | |
1574 | kmem_free(e, sizeof (log_summary_entry_t)); | |
1575 | } | |
1576 | ||
1577 | spa->spa_unflushed_stats.sus_nblocks = 0; | |
1578 | spa->spa_unflushed_stats.sus_memused = 0; | |
1579 | spa->spa_unflushed_stats.sus_blocklimit = 0; | |
1580 | } | |
1581 | ||
37f03da8 SH |
1582 | static void |
1583 | spa_destroy_aux_threads(spa_t *spa) | |
1584 | { | |
1585 | if (spa->spa_condense_zthr != NULL) { | |
1586 | zthr_destroy(spa->spa_condense_zthr); | |
1587 | spa->spa_condense_zthr = NULL; | |
1588 | } | |
1589 | if (spa->spa_checkpoint_discard_zthr != NULL) { | |
1590 | zthr_destroy(spa->spa_checkpoint_discard_zthr); | |
1591 | spa->spa_checkpoint_discard_zthr = NULL; | |
1592 | } | |
1593 | if (spa->spa_livelist_delete_zthr != NULL) { | |
1594 | zthr_destroy(spa->spa_livelist_delete_zthr); | |
1595 | spa->spa_livelist_delete_zthr = NULL; | |
1596 | } | |
1597 | if (spa->spa_livelist_condense_zthr != NULL) { | |
1598 | zthr_destroy(spa->spa_livelist_condense_zthr); | |
1599 | spa->spa_livelist_condense_zthr = NULL; | |
1600 | } | |
1601 | } | |
1602 | ||
34dc7c2f BB |
1603 | /* |
1604 | * Opposite of spa_load(). | |
1605 | */ | |
1606 | static void | |
1607 | spa_unload(spa_t *spa) | |
1608 | { | |
b128c09f | 1609 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
93e28d66 | 1610 | ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED); |
b128c09f | 1611 | |
ca95f70d | 1612 | spa_import_progress_remove(spa_guid(spa)); |
4a0ee12a PZ |
1613 | spa_load_note(spa, "UNLOADING"); |
1614 | ||
e60e158e JG |
1615 | spa_wake_waiters(spa); |
1616 | ||
93e28d66 | 1617 | /* |
2fb52853 GA |
1618 | * If we have set the spa_final_txg, we have already performed the |
1619 | * tasks below in spa_export_common(). We should not redo it here since | |
1620 | * we delay the final TXGs beyond what spa_final_txg is set at. | |
93e28d66 | 1621 | */ |
2fb52853 GA |
1622 | if (spa->spa_final_txg == UINT64_MAX) { |
1623 | /* | |
1624 | * If the log space map feature is enabled and the pool is | |
1625 | * getting exported (but not destroyed), we want to spend some | |
1626 | * time flushing as many metaslabs as we can in an attempt to | |
1627 | * destroy log space maps and save import time. | |
1628 | */ | |
1629 | if (spa_should_flush_logs_on_unload(spa)) | |
1630 | spa_unload_log_sm_flush_all(spa); | |
93e28d66 | 1631 | |
2fb52853 GA |
1632 | /* |
1633 | * Stop async tasks. | |
1634 | */ | |
1635 | spa_async_suspend(spa); | |
34dc7c2f | 1636 | |
2fb52853 GA |
1637 | if (spa->spa_root_vdev) { |
1638 | vdev_t *root_vdev = spa->spa_root_vdev; | |
1639 | vdev_initialize_stop_all(root_vdev, | |
1640 | VDEV_INITIALIZE_ACTIVE); | |
1641 | vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE); | |
1642 | vdev_autotrim_stop_all(spa); | |
1643 | vdev_rebuild_stop_all(spa); | |
1644 | } | |
619f0976 GW |
1645 | } |
1646 | ||
34dc7c2f BB |
1647 | /* |
1648 | * Stop syncing. | |
1649 | */ | |
1650 | if (spa->spa_sync_on) { | |
1651 | txg_sync_stop(spa->spa_dsl_pool); | |
1652 | spa->spa_sync_on = B_FALSE; | |
1653 | } | |
1654 | ||
4e21fd06 | 1655 | /* |
93e28d66 SD |
1656 | * This ensures that there is no async metaslab prefetching |
1657 | * while we attempt to unload the spa. | |
4e21fd06 DB |
1658 | */ |
1659 | if (spa->spa_root_vdev != NULL) { | |
93e28d66 SD |
1660 | for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) { |
1661 | vdev_t *vc = spa->spa_root_vdev->vdev_child[c]; | |
1662 | if (vc->vdev_mg != NULL) | |
1663 | taskq_wait(vc->vdev_mg->mg_taskq); | |
1664 | } | |
4e21fd06 DB |
1665 | } |
1666 | ||
379ca9cf OF |
1667 | if (spa->spa_mmp.mmp_thread) |
1668 | mmp_thread_stop(spa); | |
1669 | ||
34dc7c2f | 1670 | /* |
b128c09f | 1671 | * Wait for any outstanding async I/O to complete. |
34dc7c2f | 1672 | */ |
9babb374 | 1673 | if (spa->spa_async_zio_root != NULL) { |
1c27024e | 1674 | for (int i = 0; i < max_ncpus; i++) |
e022864d MA |
1675 | (void) zio_wait(spa->spa_async_zio_root[i]); |
1676 | kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); | |
9babb374 BB |
1677 | spa->spa_async_zio_root = NULL; |
1678 | } | |
34dc7c2f | 1679 | |
a1d477c2 MA |
1680 | if (spa->spa_vdev_removal != NULL) { |
1681 | spa_vdev_removal_destroy(spa->spa_vdev_removal); | |
1682 | spa->spa_vdev_removal = NULL; | |
1683 | } | |
1684 | ||
37f03da8 | 1685 | spa_destroy_aux_threads(spa); |
d2734cce | 1686 | |
a1d477c2 MA |
1687 | spa_condense_fini(spa); |
1688 | ||
428870ff BB |
1689 | bpobj_close(&spa->spa_deferred_bpobj); |
1690 | ||
619f0976 | 1691 | spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); |
93cf2076 GW |
1692 | |
1693 | /* | |
1694 | * Close all vdevs. | |
1695 | */ | |
1696 | if (spa->spa_root_vdev) | |
1697 | vdev_free(spa->spa_root_vdev); | |
1698 | ASSERT(spa->spa_root_vdev == NULL); | |
1699 | ||
34dc7c2f BB |
1700 | /* |
1701 | * Close the dsl pool. | |
1702 | */ | |
1703 | if (spa->spa_dsl_pool) { | |
1704 | dsl_pool_close(spa->spa_dsl_pool); | |
1705 | spa->spa_dsl_pool = NULL; | |
428870ff | 1706 | spa->spa_meta_objset = NULL; |
34dc7c2f BB |
1707 | } |
1708 | ||
428870ff | 1709 | ddt_unload(spa); |
93e28d66 | 1710 | spa_unload_log_sm_metadata(spa); |
428870ff | 1711 | |
fb5f0bc8 BB |
1712 | /* |
1713 | * Drop and purge level 2 cache | |
1714 | */ | |
1715 | spa_l2cache_drop(spa); | |
1716 | ||
34dc7c2f | 1717 | if (spa->spa_spares.sav_vdevs) { |
cfb49616 RY |
1718 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
1719 | vdev_free(spa->spa_spares.sav_vdevs[i]); | |
34dc7c2f BB |
1720 | kmem_free(spa->spa_spares.sav_vdevs, |
1721 | spa->spa_spares.sav_count * sizeof (void *)); | |
1722 | spa->spa_spares.sav_vdevs = NULL; | |
1723 | } | |
1724 | if (spa->spa_spares.sav_config) { | |
1725 | nvlist_free(spa->spa_spares.sav_config); | |
1726 | spa->spa_spares.sav_config = NULL; | |
1727 | } | |
b128c09f | 1728 | spa->spa_spares.sav_count = 0; |
34dc7c2f | 1729 | |
34dc7c2f | 1730 | if (spa->spa_l2cache.sav_vdevs) { |
cfb49616 RY |
1731 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { |
1732 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); | |
1733 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); | |
1734 | } | |
34dc7c2f BB |
1735 | kmem_free(spa->spa_l2cache.sav_vdevs, |
1736 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1737 | spa->spa_l2cache.sav_vdevs = NULL; | |
1738 | } | |
1739 | if (spa->spa_l2cache.sav_config) { | |
1740 | nvlist_free(spa->spa_l2cache.sav_config); | |
1741 | spa->spa_l2cache.sav_config = NULL; | |
1742 | } | |
b128c09f | 1743 | spa->spa_l2cache.sav_count = 0; |
34dc7c2f BB |
1744 | |
1745 | spa->spa_async_suspended = 0; | |
fb5f0bc8 | 1746 | |
a1d477c2 MA |
1747 | spa->spa_indirect_vdevs_loaded = B_FALSE; |
1748 | ||
d96eb2b1 DM |
1749 | if (spa->spa_comment != NULL) { |
1750 | spa_strfree(spa->spa_comment); | |
1751 | spa->spa_comment = NULL; | |
1752 | } | |
658fb802 CB |
1753 | if (spa->spa_compatibility != NULL) { |
1754 | spa_strfree(spa->spa_compatibility); | |
1755 | spa->spa_compatibility = NULL; | |
1756 | } | |
d96eb2b1 | 1757 | |
619f0976 | 1758 | spa_config_exit(spa, SCL_ALL, spa); |
34dc7c2f BB |
1759 | } |
1760 | ||
1761 | /* | |
1762 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1763 | * this pool. When this is called, we have some form of basic information in | |
1764 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1765 | * then re-generate a more complete list including status information. | |
1766 | */ | |
a1d477c2 | 1767 | void |
34dc7c2f BB |
1768 | spa_load_spares(spa_t *spa) |
1769 | { | |
1770 | nvlist_t **spares; | |
1771 | uint_t nspares; | |
1772 | int i; | |
1773 | vdev_t *vd, *tvd; | |
1774 | ||
d2734cce SD |
1775 | #ifndef _KERNEL |
1776 | /* | |
1777 | * zdb opens both the current state of the pool and the | |
1778 | * checkpointed state (if present), with a different spa_t. | |
1779 | * | |
1780 | * As spare vdevs are shared among open pools, we skip loading | |
1781 | * them when we load the checkpointed state of the pool. | |
1782 | */ | |
1783 | if (!spa_writeable(spa)) | |
1784 | return; | |
1785 | #endif | |
1786 | ||
b128c09f BB |
1787 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1788 | ||
34dc7c2f BB |
1789 | /* |
1790 | * First, close and free any existing spare vdevs. | |
1791 | */ | |
cfb49616 RY |
1792 | if (spa->spa_spares.sav_vdevs) { |
1793 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1794 | vd = spa->spa_spares.sav_vdevs[i]; | |
1795 | ||
1796 | /* Undo the call to spa_activate() below */ | |
1797 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, | |
1798 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
1799 | spa_spare_remove(tvd); | |
1800 | vdev_close(vd); | |
1801 | vdev_free(vd); | |
1802 | } | |
34dc7c2f | 1803 | |
34dc7c2f BB |
1804 | kmem_free(spa->spa_spares.sav_vdevs, |
1805 | spa->spa_spares.sav_count * sizeof (void *)); | |
cfb49616 | 1806 | } |
34dc7c2f BB |
1807 | |
1808 | if (spa->spa_spares.sav_config == NULL) | |
1809 | nspares = 0; | |
1810 | else | |
65ad5d11 AJ |
1811 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, |
1812 | ZPOOL_CONFIG_SPARES, &spares, &nspares)); | |
34dc7c2f BB |
1813 | |
1814 | spa->spa_spares.sav_count = (int)nspares; | |
1815 | spa->spa_spares.sav_vdevs = NULL; | |
1816 | ||
1817 | if (nspares == 0) | |
1818 | return; | |
1819 | ||
1820 | /* | |
1821 | * Construct the array of vdevs, opening them to get status in the | |
1822 | * process. For each spare, there is potentially two different vdev_t | |
1823 | * structures associated with it: one in the list of spares (used only | |
1824 | * for basic validation purposes) and one in the active vdev | |
1825 | * configuration (if it's spared in). During this phase we open and | |
1826 | * validate each vdev on the spare list. If the vdev also exists in the | |
1827 | * active configuration, then we also mark this vdev as an active spare. | |
1828 | */ | |
904ea276 | 1829 | spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *), |
79c76d5b | 1830 | KM_SLEEP); |
34dc7c2f BB |
1831 | for (i = 0; i < spa->spa_spares.sav_count; i++) { |
1832 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1833 | VDEV_ALLOC_SPARE) == 0); | |
1834 | ASSERT(vd != NULL); | |
1835 | ||
1836 | spa->spa_spares.sav_vdevs[i] = vd; | |
1837 | ||
b128c09f BB |
1838 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1839 | B_FALSE)) != NULL) { | |
34dc7c2f BB |
1840 | if (!tvd->vdev_isspare) |
1841 | spa_spare_add(tvd); | |
1842 | ||
1843 | /* | |
1844 | * We only mark the spare active if we were successfully | |
1845 | * able to load the vdev. Otherwise, importing a pool | |
1846 | * with a bad active spare would result in strange | |
1847 | * behavior, because multiple pool would think the spare | |
1848 | * is actively in use. | |
1849 | * | |
1850 | * There is a vulnerability here to an equally bizarre | |
1851 | * circumstance, where a dead active spare is later | |
1852 | * brought back to life (onlined or otherwise). Given | |
1853 | * the rarity of this scenario, and the extra complexity | |
1854 | * it adds, we ignore the possibility. | |
1855 | */ | |
1856 | if (!vdev_is_dead(tvd)) | |
1857 | spa_spare_activate(tvd); | |
1858 | } | |
1859 | ||
b128c09f | 1860 | vd->vdev_top = vd; |
9babb374 | 1861 | vd->vdev_aux = &spa->spa_spares; |
b128c09f | 1862 | |
34dc7c2f BB |
1863 | if (vdev_open(vd) != 0) |
1864 | continue; | |
1865 | ||
34dc7c2f BB |
1866 | if (vdev_validate_aux(vd) == 0) |
1867 | spa_spare_add(vd); | |
1868 | } | |
1869 | ||
1870 | /* | |
1871 | * Recompute the stashed list of spares, with status information | |
1872 | * this time. | |
1873 | */ | |
65ad5d11 | 1874 | fnvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES); |
34dc7c2f BB |
1875 | |
1876 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
79c76d5b | 1877 | KM_SLEEP); |
34dc7c2f BB |
1878 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1879 | spares[i] = vdev_config_generate(spa, | |
428870ff | 1880 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); |
65ad5d11 | 1881 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, |
795075e6 PD |
1882 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
1883 | spa->spa_spares.sav_count); | |
34dc7c2f BB |
1884 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1885 | nvlist_free(spares[i]); | |
1886 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1887 | } | |
1888 | ||
1889 | /* | |
1890 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1891 | * this pool. When this is called, we have some form of basic information in | |
1892 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1893 | * then re-generate a more complete list including status information. | |
1894 | * Devices which are already active have their details maintained, and are | |
1895 | * not re-opened. | |
1896 | */ | |
a1d477c2 | 1897 | void |
34dc7c2f BB |
1898 | spa_load_l2cache(spa_t *spa) |
1899 | { | |
460f239e | 1900 | nvlist_t **l2cache = NULL; |
34dc7c2f BB |
1901 | uint_t nl2cache; |
1902 | int i, j, oldnvdevs; | |
9babb374 | 1903 | uint64_t guid; |
a117a6d6 | 1904 | vdev_t *vd, **oldvdevs, **newvdevs; |
34dc7c2f BB |
1905 | spa_aux_vdev_t *sav = &spa->spa_l2cache; |
1906 | ||
d2734cce SD |
1907 | #ifndef _KERNEL |
1908 | /* | |
1909 | * zdb opens both the current state of the pool and the | |
1910 | * checkpointed state (if present), with a different spa_t. | |
1911 | * | |
1912 | * As L2 caches are part of the ARC which is shared among open | |
1913 | * pools, we skip loading them when we load the checkpointed | |
1914 | * state of the pool. | |
1915 | */ | |
1916 | if (!spa_writeable(spa)) | |
1917 | return; | |
1918 | #endif | |
1919 | ||
b128c09f BB |
1920 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1921 | ||
34dc7c2f BB |
1922 | oldvdevs = sav->sav_vdevs; |
1923 | oldnvdevs = sav->sav_count; | |
1924 | sav->sav_vdevs = NULL; | |
1925 | sav->sav_count = 0; | |
1926 | ||
67d60824 NB |
1927 | if (sav->sav_config == NULL) { |
1928 | nl2cache = 0; | |
1929 | newvdevs = NULL; | |
1930 | goto out; | |
1931 | } | |
1932 | ||
65ad5d11 AJ |
1933 | VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config, |
1934 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache)); | |
67d60824 NB |
1935 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); |
1936 | ||
34dc7c2f BB |
1937 | /* |
1938 | * Process new nvlist of vdevs. | |
1939 | */ | |
1940 | for (i = 0; i < nl2cache; i++) { | |
65ad5d11 | 1941 | guid = fnvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID); |
34dc7c2f BB |
1942 | |
1943 | newvdevs[i] = NULL; | |
1944 | for (j = 0; j < oldnvdevs; j++) { | |
1945 | vd = oldvdevs[j]; | |
1946 | if (vd != NULL && guid == vd->vdev_guid) { | |
1947 | /* | |
1948 | * Retain previous vdev for add/remove ops. | |
1949 | */ | |
1950 | newvdevs[i] = vd; | |
1951 | oldvdevs[j] = NULL; | |
1952 | break; | |
1953 | } | |
1954 | } | |
1955 | ||
1956 | if (newvdevs[i] == NULL) { | |
1957 | /* | |
1958 | * Create new vdev | |
1959 | */ | |
1960 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
1961 | VDEV_ALLOC_L2CACHE) == 0); | |
1962 | ASSERT(vd != NULL); | |
1963 | newvdevs[i] = vd; | |
1964 | ||
1965 | /* | |
1966 | * Commit this vdev as an l2cache device, | |
1967 | * even if it fails to open. | |
1968 | */ | |
1969 | spa_l2cache_add(vd); | |
1970 | ||
b128c09f BB |
1971 | vd->vdev_top = vd; |
1972 | vd->vdev_aux = sav; | |
1973 | ||
1974 | spa_l2cache_activate(vd); | |
1975 | ||
34dc7c2f BB |
1976 | if (vdev_open(vd) != 0) |
1977 | continue; | |
1978 | ||
34dc7c2f BB |
1979 | (void) vdev_validate_aux(vd); |
1980 | ||
9babb374 BB |
1981 | if (!vdev_is_dead(vd)) |
1982 | l2arc_add_vdev(spa, vd); | |
b7654bd7 GA |
1983 | |
1984 | /* | |
1985 | * Upon cache device addition to a pool or pool | |
1986 | * creation with a cache device or if the header | |
1987 | * of the device is invalid we issue an async | |
1988 | * TRIM command for the whole device which will | |
1989 | * execute if l2arc_trim_ahead > 0. | |
1990 | */ | |
1991 | spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM); | |
34dc7c2f BB |
1992 | } |
1993 | } | |
1994 | ||
67d60824 NB |
1995 | sav->sav_vdevs = newvdevs; |
1996 | sav->sav_count = (int)nl2cache; | |
1997 | ||
1998 | /* | |
1999 | * Recompute the stashed list of l2cache devices, with status | |
2000 | * information this time. | |
2001 | */ | |
65ad5d11 | 2002 | fnvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE); |
67d60824 | 2003 | |
460f239e D |
2004 | if (sav->sav_count > 0) |
2005 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), | |
2006 | KM_SLEEP); | |
67d60824 NB |
2007 | for (i = 0; i < sav->sav_count; i++) |
2008 | l2cache[i] = vdev_config_generate(spa, | |
2009 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); | |
795075e6 PD |
2010 | fnvlist_add_nvlist_array(sav->sav_config, ZPOOL_CONFIG_L2CACHE, |
2011 | (const nvlist_t * const *)l2cache, sav->sav_count); | |
67d60824 NB |
2012 | |
2013 | out: | |
34dc7c2f BB |
2014 | /* |
2015 | * Purge vdevs that were dropped | |
2016 | */ | |
cfb49616 RY |
2017 | if (oldvdevs) { |
2018 | for (i = 0; i < oldnvdevs; i++) { | |
2019 | uint64_t pool; | |
2020 | ||
2021 | vd = oldvdevs[i]; | |
2022 | if (vd != NULL) { | |
2023 | ASSERT(vd->vdev_isl2cache); | |
2024 | ||
2025 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && | |
2026 | pool != 0ULL && l2arc_vdev_present(vd)) | |
2027 | l2arc_remove_vdev(vd); | |
2028 | vdev_clear_stats(vd); | |
2029 | vdev_free(vd); | |
2030 | } | |
34dc7c2f | 2031 | } |
34dc7c2f | 2032 | |
34dc7c2f | 2033 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); |
cfb49616 | 2034 | } |
34dc7c2f | 2035 | |
34dc7c2f BB |
2036 | for (i = 0; i < sav->sav_count; i++) |
2037 | nvlist_free(l2cache[i]); | |
2038 | if (sav->sav_count) | |
2039 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
2040 | } | |
2041 | ||
2042 | static int | |
2043 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
2044 | { | |
2045 | dmu_buf_t *db; | |
2046 | char *packed = NULL; | |
2047 | size_t nvsize = 0; | |
2048 | int error; | |
2049 | *value = NULL; | |
2050 | ||
c3275b56 BB |
2051 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); |
2052 | if (error) | |
2053 | return (error); | |
2054 | ||
34dc7c2f BB |
2055 | nvsize = *(uint64_t *)db->db_data; |
2056 | dmu_buf_rele(db, FTAG); | |
2057 | ||
77aef6f6 | 2058 | packed = vmem_alloc(nvsize, KM_SLEEP); |
9babb374 BB |
2059 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, |
2060 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
2061 | if (error == 0) |
2062 | error = nvlist_unpack(packed, nvsize, value, 0); | |
77aef6f6 | 2063 | vmem_free(packed, nvsize); |
34dc7c2f BB |
2064 | |
2065 | return (error); | |
2066 | } | |
2067 | ||
6cb8e530 PZ |
2068 | /* |
2069 | * Concrete top-level vdevs that are not missing and are not logs. At every | |
2070 | * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. | |
2071 | */ | |
2072 | static uint64_t | |
2073 | spa_healthy_core_tvds(spa_t *spa) | |
2074 | { | |
2075 | vdev_t *rvd = spa->spa_root_vdev; | |
2076 | uint64_t tvds = 0; | |
2077 | ||
2078 | for (uint64_t i = 0; i < rvd->vdev_children; i++) { | |
2079 | vdev_t *vd = rvd->vdev_child[i]; | |
2080 | if (vd->vdev_islog) | |
2081 | continue; | |
2082 | if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) | |
2083 | tvds++; | |
2084 | } | |
2085 | ||
2086 | return (tvds); | |
2087 | } | |
2088 | ||
34dc7c2f BB |
2089 | /* |
2090 | * Checks to see if the given vdev could not be opened, in which case we post a | |
2091 | * sysevent to notify the autoreplace code that the device has been removed. | |
2092 | */ | |
2093 | static void | |
2094 | spa_check_removed(vdev_t *vd) | |
2095 | { | |
6cb8e530 | 2096 | for (uint64_t c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
2097 | spa_check_removed(vd->vdev_child[c]); |
2098 | ||
7011fb60 | 2099 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && |
a1d477c2 | 2100 | vdev_is_concrete(vd)) { |
fb390aaf | 2101 | zfs_post_autoreplace(vd->vdev_spa, vd); |
12fa0466 | 2102 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); |
34dc7c2f BB |
2103 | } |
2104 | } | |
2105 | ||
6cb8e530 PZ |
2106 | static int |
2107 | spa_check_for_missing_logs(spa_t *spa) | |
9babb374 | 2108 | { |
6cb8e530 | 2109 | vdev_t *rvd = spa->spa_root_vdev; |
9babb374 | 2110 | |
428870ff | 2111 | /* |
572e2857 | 2112 | * If we're doing a normal import, then build up any additional |
6cb8e530 | 2113 | * diagnostic information about missing log devices. |
572e2857 | 2114 | * We'll pass this up to the user for further processing. |
428870ff | 2115 | */ |
572e2857 BB |
2116 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { |
2117 | nvlist_t **child, *nv; | |
2118 | uint64_t idx = 0; | |
2119 | ||
160987b5 | 2120 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *), |
79c76d5b | 2121 | KM_SLEEP); |
65ad5d11 | 2122 | nv = fnvlist_alloc(); |
572e2857 | 2123 | |
6cb8e530 | 2124 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { |
572e2857 | 2125 | vdev_t *tvd = rvd->vdev_child[c]; |
572e2857 | 2126 | |
6cb8e530 PZ |
2127 | /* |
2128 | * We consider a device as missing only if it failed | |
2129 | * to open (i.e. offline or faulted is not considered | |
2130 | * as missing). | |
2131 | */ | |
2132 | if (tvd->vdev_islog && | |
2133 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
2134 | child[idx++] = vdev_config_generate(spa, tvd, | |
2135 | B_FALSE, VDEV_CONFIG_MISSING); | |
2136 | } | |
572e2857 | 2137 | } |
9babb374 | 2138 | |
6cb8e530 | 2139 | if (idx > 0) { |
795075e6 PD |
2140 | fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
2141 | (const nvlist_t * const *)child, idx); | |
6cb8e530 PZ |
2142 | fnvlist_add_nvlist(spa->spa_load_info, |
2143 | ZPOOL_CONFIG_MISSING_DEVICES, nv); | |
572e2857 | 2144 | |
6cb8e530 | 2145 | for (uint64_t i = 0; i < idx; i++) |
572e2857 BB |
2146 | nvlist_free(child[i]); |
2147 | } | |
2148 | nvlist_free(nv); | |
2149 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
572e2857 | 2150 | |
6cb8e530 PZ |
2151 | if (idx > 0) { |
2152 | spa_load_failed(spa, "some log devices are missing"); | |
db7d07e1 | 2153 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 PZ |
2154 | return (SET_ERROR(ENXIO)); |
2155 | } | |
2156 | } else { | |
2157 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
2158 | vdev_t *tvd = rvd->vdev_child[c]; | |
a1d477c2 | 2159 | |
6cb8e530 PZ |
2160 | if (tvd->vdev_islog && |
2161 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
572e2857 | 2162 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
6cb8e530 PZ |
2163 | spa_load_note(spa, "some log devices are " |
2164 | "missing, ZIL is dropped."); | |
db7d07e1 | 2165 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 | 2166 | break; |
e0ab3ab5 | 2167 | } |
572e2857 | 2168 | } |
9babb374 | 2169 | } |
e0ab3ab5 | 2170 | |
6cb8e530 | 2171 | return (0); |
9babb374 BB |
2172 | } |
2173 | ||
b128c09f BB |
2174 | /* |
2175 | * Check for missing log devices | |
2176 | */ | |
13fe0198 | 2177 | static boolean_t |
b128c09f BB |
2178 | spa_check_logs(spa_t *spa) |
2179 | { | |
13fe0198 | 2180 | boolean_t rv = B_FALSE; |
9c43027b | 2181 | dsl_pool_t *dp = spa_get_dsl(spa); |
13fe0198 | 2182 | |
b128c09f | 2183 | switch (spa->spa_log_state) { |
e75c13c3 BB |
2184 | default: |
2185 | break; | |
b128c09f BB |
2186 | case SPA_LOG_MISSING: |
2187 | /* need to recheck in case slog has been restored */ | |
2188 | case SPA_LOG_UNKNOWN: | |
9c43027b AJ |
2189 | rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, |
2190 | zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); | |
13fe0198 | 2191 | if (rv) |
428870ff | 2192 | spa_set_log_state(spa, SPA_LOG_MISSING); |
b128c09f | 2193 | break; |
b128c09f | 2194 | } |
13fe0198 | 2195 | return (rv); |
b128c09f BB |
2196 | } |
2197 | ||
aa755b35 MA |
2198 | /* |
2199 | * Passivate any log vdevs (note, does not apply to embedded log metaslabs). | |
2200 | */ | |
428870ff BB |
2201 | static boolean_t |
2202 | spa_passivate_log(spa_t *spa) | |
34dc7c2f | 2203 | { |
428870ff BB |
2204 | vdev_t *rvd = spa->spa_root_vdev; |
2205 | boolean_t slog_found = B_FALSE; | |
b128c09f | 2206 | |
428870ff | 2207 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
fb5f0bc8 | 2208 | |
1c27024e | 2209 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff | 2210 | vdev_t *tvd = rvd->vdev_child[c]; |
34dc7c2f | 2211 | |
428870ff | 2212 | if (tvd->vdev_islog) { |
aa755b35 MA |
2213 | ASSERT3P(tvd->vdev_log_mg, ==, NULL); |
2214 | metaslab_group_passivate(tvd->vdev_mg); | |
428870ff BB |
2215 | slog_found = B_TRUE; |
2216 | } | |
34dc7c2f BB |
2217 | } |
2218 | ||
428870ff BB |
2219 | return (slog_found); |
2220 | } | |
34dc7c2f | 2221 | |
aa755b35 MA |
2222 | /* |
2223 | * Activate any log vdevs (note, does not apply to embedded log metaslabs). | |
2224 | */ | |
428870ff BB |
2225 | static void |
2226 | spa_activate_log(spa_t *spa) | |
2227 | { | |
2228 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 2229 | |
428870ff BB |
2230 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
2231 | ||
1c27024e | 2232 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff | 2233 | vdev_t *tvd = rvd->vdev_child[c]; |
428870ff | 2234 | |
aa755b35 MA |
2235 | if (tvd->vdev_islog) { |
2236 | ASSERT3P(tvd->vdev_log_mg, ==, NULL); | |
2237 | metaslab_group_activate(tvd->vdev_mg); | |
2238 | } | |
34dc7c2f | 2239 | } |
428870ff | 2240 | } |
34dc7c2f | 2241 | |
428870ff | 2242 | int |
a1d477c2 | 2243 | spa_reset_logs(spa_t *spa) |
428870ff | 2244 | { |
13fe0198 | 2245 | int error; |
9babb374 | 2246 | |
a1d477c2 | 2247 | error = dmu_objset_find(spa_name(spa), zil_reset, |
13fe0198 MA |
2248 | NULL, DS_FIND_CHILDREN); |
2249 | if (error == 0) { | |
428870ff BB |
2250 | /* |
2251 | * We successfully offlined the log device, sync out the | |
2252 | * current txg so that the "stubby" block can be removed | |
2253 | * by zil_sync(). | |
2254 | */ | |
2255 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
2256 | } | |
2257 | return (error); | |
2258 | } | |
34dc7c2f | 2259 | |
428870ff BB |
2260 | static void |
2261 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
2262 | { | |
1c27024e | 2263 | for (int i = 0; i < sav->sav_count; i++) |
428870ff BB |
2264 | spa_check_removed(sav->sav_vdevs[i]); |
2265 | } | |
34dc7c2f | 2266 | |
428870ff BB |
2267 | void |
2268 | spa_claim_notify(zio_t *zio) | |
2269 | { | |
2270 | spa_t *spa = zio->io_spa; | |
34dc7c2f | 2271 | |
428870ff BB |
2272 | if (zio->io_error) |
2273 | return; | |
34dc7c2f | 2274 | |
428870ff BB |
2275 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ |
2276 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
2277 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
2278 | mutex_exit(&spa->spa_props_lock); | |
2279 | } | |
34dc7c2f | 2280 | |
428870ff | 2281 | typedef struct spa_load_error { |
f2c5bc15 | 2282 | boolean_t sle_verify_data; |
428870ff BB |
2283 | uint64_t sle_meta_count; |
2284 | uint64_t sle_data_count; | |
2285 | } spa_load_error_t; | |
34dc7c2f | 2286 | |
428870ff BB |
2287 | static void |
2288 | spa_load_verify_done(zio_t *zio) | |
2289 | { | |
2290 | blkptr_t *bp = zio->io_bp; | |
2291 | spa_load_error_t *sle = zio->io_private; | |
2292 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
2293 | int error = zio->io_error; | |
dea377c0 | 2294 | spa_t *spa = zio->io_spa; |
34dc7c2f | 2295 | |
a6255b7f | 2296 | abd_free(zio->io_abd); |
428870ff | 2297 | if (error) { |
9ae529ec | 2298 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && |
428870ff | 2299 | type != DMU_OT_INTENT_LOG) |
bc89ac84 | 2300 | atomic_inc_64(&sle->sle_meta_count); |
428870ff | 2301 | else |
bc89ac84 | 2302 | atomic_inc_64(&sle->sle_data_count); |
34dc7c2f | 2303 | } |
dea377c0 MA |
2304 | |
2305 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2306 | spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp); |
dea377c0 MA |
2307 | cv_broadcast(&spa->spa_scrub_io_cv); |
2308 | mutex_exit(&spa->spa_scrub_lock); | |
428870ff | 2309 | } |
34dc7c2f | 2310 | |
dea377c0 | 2311 | /* |
e1cfd73f | 2312 | * Maximum number of inflight bytes is the log2 fraction of the arc size. |
c8242a96 | 2313 | * By default, we set it to 1/16th of the arc. |
dea377c0 | 2314 | */ |
fdc2d303 | 2315 | static uint_t spa_load_verify_shift = 4; |
18168da7 AZ |
2316 | static int spa_load_verify_metadata = B_TRUE; |
2317 | static int spa_load_verify_data = B_TRUE; | |
dea377c0 | 2318 | |
428870ff BB |
2319 | static int |
2320 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
5dbd68a3 | 2321 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) |
428870ff | 2322 | { |
f2c5bc15 AM |
2323 | zio_t *rio = arg; |
2324 | spa_load_error_t *sle = rio->io_private; | |
2325 | ||
14e4e3cb AZ |
2326 | (void) zilog, (void) dnp; |
2327 | ||
dea377c0 MA |
2328 | /* |
2329 | * Note: normally this routine will not be called if | |
2330 | * spa_load_verify_metadata is not set. However, it may be useful | |
2331 | * to manually set the flag after the traversal has begun. | |
2332 | */ | |
2333 | if (!spa_load_verify_metadata) | |
2334 | return (0); | |
2cd0f98f BB |
2335 | |
2336 | /* | |
2337 | * Sanity check the block pointer in order to detect obvious damage | |
2338 | * before using the contents in subsequent checks or in zio_read(). | |
2339 | * When damaged consider it to be a metadata error since we cannot | |
2340 | * trust the BP_GET_TYPE and BP_GET_LEVEL values. | |
2341 | */ | |
2342 | if (!zfs_blkptr_verify(spa, bp, B_FALSE, BLK_VERIFY_LOG)) { | |
2343 | atomic_inc_64(&sle->sle_meta_count); | |
2344 | return (0); | |
2345 | } | |
2346 | ||
2347 | if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) || | |
2348 | BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp)) | |
2349 | return (0); | |
2350 | ||
f2c5bc15 AM |
2351 | if (!BP_IS_METADATA(bp) && |
2352 | (!spa_load_verify_data || !sle->sle_verify_data)) | |
dea377c0 MA |
2353 | return (0); |
2354 | ||
1e527162 GW |
2355 | uint64_t maxinflight_bytes = |
2356 | arc_target_bytes() >> spa_load_verify_shift; | |
1c27024e | 2357 | size_t size = BP_GET_PSIZE(bp); |
dea377c0 MA |
2358 | |
2359 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2360 | while (spa->spa_load_verify_bytes >= maxinflight_bytes) |
dea377c0 | 2361 | cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); |
c8242a96 | 2362 | spa->spa_load_verify_bytes += size; |
dea377c0 MA |
2363 | mutex_exit(&spa->spa_scrub_lock); |
2364 | ||
a6255b7f | 2365 | zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, |
dea377c0 MA |
2366 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, |
2367 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
2368 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
428870ff BB |
2369 | return (0); |
2370 | } | |
34dc7c2f | 2371 | |
65c7cc49 | 2372 | static int |
d1d19c78 PD |
2373 | verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) |
2374 | { | |
14e4e3cb AZ |
2375 | (void) dp, (void) arg; |
2376 | ||
d1d19c78 PD |
2377 | if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) |
2378 | return (SET_ERROR(ENAMETOOLONG)); | |
2379 | ||
2380 | return (0); | |
2381 | } | |
2382 | ||
428870ff BB |
2383 | static int |
2384 | spa_load_verify(spa_t *spa) | |
2385 | { | |
2386 | zio_t *rio; | |
2387 | spa_load_error_t sle = { 0 }; | |
8a393be3 | 2388 | zpool_load_policy_t policy; |
428870ff | 2389 | boolean_t verify_ok = B_FALSE; |
dea377c0 | 2390 | int error = 0; |
34dc7c2f | 2391 | |
8a393be3 | 2392 | zpool_get_load_policy(spa->spa_config, &policy); |
34dc7c2f | 2393 | |
f2c5bc15 AM |
2394 | if (policy.zlp_rewind & ZPOOL_NEVER_REWIND || |
2395 | policy.zlp_maxmeta == UINT64_MAX) | |
428870ff | 2396 | return (0); |
34dc7c2f | 2397 | |
d1d19c78 PD |
2398 | dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); |
2399 | error = dmu_objset_find_dp(spa->spa_dsl_pool, | |
2400 | spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, | |
2401 | DS_FIND_CHILDREN); | |
2402 | dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); | |
2403 | if (error != 0) | |
2404 | return (error); | |
2405 | ||
f2c5bc15 AM |
2406 | /* |
2407 | * Verify data only if we are rewinding or error limit was set. | |
2408 | * Otherwise nothing except dbgmsg care about it to waste time. | |
2409 | */ | |
2410 | sle.sle_verify_data = (policy.zlp_rewind & ZPOOL_REWIND_MASK) || | |
2411 | (policy.zlp_maxdata < UINT64_MAX); | |
2412 | ||
428870ff BB |
2413 | rio = zio_root(spa, NULL, &sle, |
2414 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
34dc7c2f | 2415 | |
dea377c0 | 2416 | if (spa_load_verify_metadata) { |
4a0ee12a PZ |
2417 | if (spa->spa_extreme_rewind) { |
2418 | spa_load_note(spa, "performing a complete scan of the " | |
2419 | "pool since extreme rewind is on. This may take " | |
2420 | "a very long time.\n (spa_load_verify_data=%u, " | |
2421 | "spa_load_verify_metadata=%u)", | |
2422 | spa_load_verify_data, spa_load_verify_metadata); | |
2423 | } | |
c8242a96 | 2424 | |
dea377c0 | 2425 | error = traverse_pool(spa, spa->spa_verify_min_txg, |
b5256303 TC |
2426 | TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | |
2427 | TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio); | |
dea377c0 | 2428 | } |
428870ff BB |
2429 | |
2430 | (void) zio_wait(rio); | |
c8242a96 | 2431 | ASSERT0(spa->spa_load_verify_bytes); |
428870ff BB |
2432 | |
2433 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
2434 | spa->spa_load_data_errors = sle.sle_data_count; | |
2435 | ||
afd2f7b7 PZ |
2436 | if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { |
2437 | spa_load_note(spa, "spa_load_verify found %llu metadata errors " | |
2438 | "and %llu data errors", (u_longlong_t)sle.sle_meta_count, | |
2439 | (u_longlong_t)sle.sle_data_count); | |
2440 | } | |
2441 | ||
2442 | if (spa_load_verify_dryrun || | |
8a393be3 PZ |
2443 | (!error && sle.sle_meta_count <= policy.zlp_maxmeta && |
2444 | sle.sle_data_count <= policy.zlp_maxdata)) { | |
572e2857 BB |
2445 | int64_t loss = 0; |
2446 | ||
428870ff BB |
2447 | verify_ok = B_TRUE; |
2448 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
2449 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
572e2857 BB |
2450 | |
2451 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
65ad5d11 AJ |
2452 | fnvlist_add_uint64(spa->spa_load_info, ZPOOL_CONFIG_LOAD_TIME, |
2453 | spa->spa_load_txg_ts); | |
2454 | fnvlist_add_int64(spa->spa_load_info, ZPOOL_CONFIG_REWIND_TIME, | |
2455 | loss); | |
f2c5bc15 AM |
2456 | fnvlist_add_uint64(spa->spa_load_info, |
2457 | ZPOOL_CONFIG_LOAD_META_ERRORS, sle.sle_meta_count); | |
65ad5d11 AJ |
2458 | fnvlist_add_uint64(spa->spa_load_info, |
2459 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count); | |
428870ff BB |
2460 | } else { |
2461 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
2462 | } | |
2463 | ||
afd2f7b7 PZ |
2464 | if (spa_load_verify_dryrun) |
2465 | return (0); | |
2466 | ||
428870ff BB |
2467 | if (error) { |
2468 | if (error != ENXIO && error != EIO) | |
2e528b49 | 2469 | error = SET_ERROR(EIO); |
428870ff BB |
2470 | return (error); |
2471 | } | |
2472 | ||
2473 | return (verify_ok ? 0 : EIO); | |
2474 | } | |
2475 | ||
2476 | /* | |
2477 | * Find a value in the pool props object. | |
2478 | */ | |
2479 | static void | |
2480 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
2481 | { | |
2482 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
2483 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
2484 | } | |
2485 | ||
2486 | /* | |
2487 | * Find a value in the pool directory object. | |
2488 | */ | |
2489 | static int | |
4a0ee12a | 2490 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) |
428870ff | 2491 | { |
4a0ee12a PZ |
2492 | int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
2493 | name, sizeof (uint64_t), 1, val); | |
2494 | ||
2495 | if (error != 0 && (error != ENOENT || log_enoent)) { | |
2496 | spa_load_failed(spa, "couldn't get '%s' value in MOS directory " | |
2497 | "[error=%d]", name, error); | |
2498 | } | |
2499 | ||
2500 | return (error); | |
428870ff BB |
2501 | } |
2502 | ||
2503 | static int | |
2504 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
2505 | { | |
2506 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
a1d477c2 | 2507 | return (SET_ERROR(err)); |
428870ff BB |
2508 | } |
2509 | ||
37f03da8 SH |
2510 | boolean_t |
2511 | spa_livelist_delete_check(spa_t *spa) | |
2512 | { | |
2513 | return (spa->spa_livelists_to_delete != 0); | |
2514 | } | |
2515 | ||
37f03da8 SH |
2516 | static boolean_t |
2517 | spa_livelist_delete_cb_check(void *arg, zthr_t *z) | |
2518 | { | |
14e4e3cb | 2519 | (void) z; |
37f03da8 SH |
2520 | spa_t *spa = arg; |
2521 | return (spa_livelist_delete_check(spa)); | |
2522 | } | |
2523 | ||
2524 | static int | |
2525 | delete_blkptr_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
2526 | { | |
2527 | spa_t *spa = arg; | |
2528 | zio_free(spa, tx->tx_txg, bp); | |
2529 | dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD, | |
2530 | -bp_get_dsize_sync(spa, bp), | |
2531 | -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx); | |
2532 | return (0); | |
2533 | } | |
2534 | ||
2535 | static int | |
2536 | dsl_get_next_livelist_obj(objset_t *os, uint64_t zap_obj, uint64_t *llp) | |
2537 | { | |
2538 | int err; | |
2539 | zap_cursor_t zc; | |
2540 | zap_attribute_t za; | |
2541 | zap_cursor_init(&zc, os, zap_obj); | |
2542 | err = zap_cursor_retrieve(&zc, &za); | |
2543 | zap_cursor_fini(&zc); | |
2544 | if (err == 0) | |
2545 | *llp = za.za_first_integer; | |
2546 | return (err); | |
2547 | } | |
2548 | ||
2549 | /* | |
2550 | * Components of livelist deletion that must be performed in syncing | |
2551 | * context: freeing block pointers and updating the pool-wide data | |
2552 | * structures to indicate how much work is left to do | |
2553 | */ | |
2554 | typedef struct sublist_delete_arg { | |
2555 | spa_t *spa; | |
2556 | dsl_deadlist_t *ll; | |
2557 | uint64_t key; | |
2558 | bplist_t *to_free; | |
2559 | } sublist_delete_arg_t; | |
2560 | ||
2561 | static void | |
2562 | sublist_delete_sync(void *arg, dmu_tx_t *tx) | |
2563 | { | |
2564 | sublist_delete_arg_t *sda = arg; | |
2565 | spa_t *spa = sda->spa; | |
2566 | dsl_deadlist_t *ll = sda->ll; | |
2567 | uint64_t key = sda->key; | |
2568 | bplist_t *to_free = sda->to_free; | |
2569 | ||
2570 | bplist_iterate(to_free, delete_blkptr_cb, spa, tx); | |
2571 | dsl_deadlist_remove_entry(ll, key, tx); | |
2572 | } | |
2573 | ||
2574 | typedef struct livelist_delete_arg { | |
2575 | spa_t *spa; | |
2576 | uint64_t ll_obj; | |
2577 | uint64_t zap_obj; | |
2578 | } livelist_delete_arg_t; | |
2579 | ||
2580 | static void | |
2581 | livelist_delete_sync(void *arg, dmu_tx_t *tx) | |
2582 | { | |
2583 | livelist_delete_arg_t *lda = arg; | |
2584 | spa_t *spa = lda->spa; | |
2585 | uint64_t ll_obj = lda->ll_obj; | |
2586 | uint64_t zap_obj = lda->zap_obj; | |
2587 | objset_t *mos = spa->spa_meta_objset; | |
2588 | uint64_t count; | |
2589 | ||
2590 | /* free the livelist and decrement the feature count */ | |
2591 | VERIFY0(zap_remove_int(mos, zap_obj, ll_obj, tx)); | |
2592 | dsl_deadlist_free(mos, ll_obj, tx); | |
2593 | spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx); | |
2594 | VERIFY0(zap_count(mos, zap_obj, &count)); | |
2595 | if (count == 0) { | |
2596 | /* no more livelists to delete */ | |
2597 | VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT, | |
2598 | DMU_POOL_DELETED_CLONES, tx)); | |
2599 | VERIFY0(zap_destroy(mos, zap_obj, tx)); | |
2600 | spa->spa_livelists_to_delete = 0; | |
e60e158e | 2601 | spa_notify_waiters(spa); |
37f03da8 SH |
2602 | } |
2603 | } | |
2604 | ||
2605 | /* | |
2606 | * Load in the value for the livelist to be removed and open it. Then, | |
2607 | * load its first sublist and determine which block pointers should actually | |
2608 | * be freed. Then, call a synctask which performs the actual frees and updates | |
2609 | * the pool-wide livelist data. | |
2610 | */ | |
65c7cc49 | 2611 | static void |
37f03da8 SH |
2612 | spa_livelist_delete_cb(void *arg, zthr_t *z) |
2613 | { | |
2614 | spa_t *spa = arg; | |
2615 | uint64_t ll_obj = 0, count; | |
2616 | objset_t *mos = spa->spa_meta_objset; | |
2617 | uint64_t zap_obj = spa->spa_livelists_to_delete; | |
2618 | /* | |
2619 | * Determine the next livelist to delete. This function should only | |
2620 | * be called if there is at least one deleted clone. | |
2621 | */ | |
2622 | VERIFY0(dsl_get_next_livelist_obj(mos, zap_obj, &ll_obj)); | |
2623 | VERIFY0(zap_count(mos, ll_obj, &count)); | |
2624 | if (count > 0) { | |
c9562576 | 2625 | dsl_deadlist_t *ll; |
37f03da8 SH |
2626 | dsl_deadlist_entry_t *dle; |
2627 | bplist_t to_free; | |
c9562576 PS |
2628 | ll = kmem_zalloc(sizeof (dsl_deadlist_t), KM_SLEEP); |
2629 | dsl_deadlist_open(ll, mos, ll_obj); | |
2630 | dle = dsl_deadlist_first(ll); | |
37f03da8 SH |
2631 | ASSERT3P(dle, !=, NULL); |
2632 | bplist_create(&to_free); | |
2633 | int err = dsl_process_sub_livelist(&dle->dle_bpobj, &to_free, | |
2634 | z, NULL); | |
2635 | if (err == 0) { | |
2636 | sublist_delete_arg_t sync_arg = { | |
2637 | .spa = spa, | |
c9562576 | 2638 | .ll = ll, |
37f03da8 SH |
2639 | .key = dle->dle_mintxg, |
2640 | .to_free = &to_free | |
2641 | }; | |
2642 | zfs_dbgmsg("deleting sublist (id %llu) from" | |
8e739b2c RE |
2643 | " livelist %llu, %lld remaining", |
2644 | (u_longlong_t)dle->dle_bpobj.bpo_object, | |
2645 | (u_longlong_t)ll_obj, (longlong_t)count - 1); | |
37f03da8 SH |
2646 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, |
2647 | sublist_delete_sync, &sync_arg, 0, | |
2648 | ZFS_SPACE_CHECK_DESTROY)); | |
2649 | } else { | |
d87676a9 | 2650 | VERIFY3U(err, ==, EINTR); |
37f03da8 SH |
2651 | } |
2652 | bplist_clear(&to_free); | |
2653 | bplist_destroy(&to_free); | |
c9562576 PS |
2654 | dsl_deadlist_close(ll); |
2655 | kmem_free(ll, sizeof (dsl_deadlist_t)); | |
37f03da8 SH |
2656 | } else { |
2657 | livelist_delete_arg_t sync_arg = { | |
2658 | .spa = spa, | |
2659 | .ll_obj = ll_obj, | |
2660 | .zap_obj = zap_obj | |
2661 | }; | |
8e739b2c RE |
2662 | zfs_dbgmsg("deletion of livelist %llu completed", |
2663 | (u_longlong_t)ll_obj); | |
37f03da8 SH |
2664 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, livelist_delete_sync, |
2665 | &sync_arg, 0, ZFS_SPACE_CHECK_DESTROY)); | |
2666 | } | |
2667 | } | |
2668 | ||
65c7cc49 | 2669 | static void |
37f03da8 SH |
2670 | spa_start_livelist_destroy_thread(spa_t *spa) |
2671 | { | |
2672 | ASSERT3P(spa->spa_livelist_delete_zthr, ==, NULL); | |
843e9ca2 SD |
2673 | spa->spa_livelist_delete_zthr = |
2674 | zthr_create("z_livelist_destroy", | |
6bc61d22 TN |
2675 | spa_livelist_delete_cb_check, spa_livelist_delete_cb, spa, |
2676 | minclsyspri); | |
37f03da8 SH |
2677 | } |
2678 | ||
2679 | typedef struct livelist_new_arg { | |
2680 | bplist_t *allocs; | |
2681 | bplist_t *frees; | |
2682 | } livelist_new_arg_t; | |
2683 | ||
2684 | static int | |
2685 | livelist_track_new_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
2686 | dmu_tx_t *tx) | |
2687 | { | |
2688 | ASSERT(tx == NULL); | |
2689 | livelist_new_arg_t *lna = arg; | |
2690 | if (bp_freed) { | |
2691 | bplist_append(lna->frees, bp); | |
2692 | } else { | |
2693 | bplist_append(lna->allocs, bp); | |
2694 | zfs_livelist_condense_new_alloc++; | |
2695 | } | |
2696 | return (0); | |
2697 | } | |
2698 | ||
2699 | typedef struct livelist_condense_arg { | |
2700 | spa_t *spa; | |
2701 | bplist_t to_keep; | |
2702 | uint64_t first_size; | |
2703 | uint64_t next_size; | |
2704 | } livelist_condense_arg_t; | |
2705 | ||
2706 | static void | |
2707 | spa_livelist_condense_sync(void *arg, dmu_tx_t *tx) | |
2708 | { | |
2709 | livelist_condense_arg_t *lca = arg; | |
2710 | spa_t *spa = lca->spa; | |
2711 | bplist_t new_frees; | |
2712 | dsl_dataset_t *ds = spa->spa_to_condense.ds; | |
2713 | ||
2714 | /* Have we been cancelled? */ | |
2715 | if (spa->spa_to_condense.cancelled) { | |
2716 | zfs_livelist_condense_sync_cancel++; | |
2717 | goto out; | |
2718 | } | |
2719 | ||
2720 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2721 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2722 | dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist; | |
2723 | ||
2724 | /* | |
2725 | * It's possible that the livelist was changed while the zthr was | |
2726 | * running. Therefore, we need to check for new blkptrs in the two | |
2727 | * entries being condensed and continue to track them in the livelist. | |
2728 | * Because of the way we handle remapped blkptrs (see dbuf_remap_impl), | |
2729 | * it's possible that the newly added blkptrs are FREEs or ALLOCs so | |
2730 | * we need to sort them into two different bplists. | |
2731 | */ | |
2732 | uint64_t first_obj = first->dle_bpobj.bpo_object; | |
2733 | uint64_t next_obj = next->dle_bpobj.bpo_object; | |
2734 | uint64_t cur_first_size = first->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2735 | uint64_t cur_next_size = next->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2736 | ||
2737 | bplist_create(&new_frees); | |
2738 | livelist_new_arg_t new_bps = { | |
2739 | .allocs = &lca->to_keep, | |
2740 | .frees = &new_frees, | |
2741 | }; | |
2742 | ||
2743 | if (cur_first_size > lca->first_size) { | |
2744 | VERIFY0(livelist_bpobj_iterate_from_nofree(&first->dle_bpobj, | |
2745 | livelist_track_new_cb, &new_bps, lca->first_size)); | |
2746 | } | |
2747 | if (cur_next_size > lca->next_size) { | |
2748 | VERIFY0(livelist_bpobj_iterate_from_nofree(&next->dle_bpobj, | |
2749 | livelist_track_new_cb, &new_bps, lca->next_size)); | |
2750 | } | |
2751 | ||
2752 | dsl_deadlist_clear_entry(first, ll, tx); | |
2753 | ASSERT(bpobj_is_empty(&first->dle_bpobj)); | |
2754 | dsl_deadlist_remove_entry(ll, next->dle_mintxg, tx); | |
2755 | ||
2756 | bplist_iterate(&lca->to_keep, dsl_deadlist_insert_alloc_cb, ll, tx); | |
2757 | bplist_iterate(&new_frees, dsl_deadlist_insert_free_cb, ll, tx); | |
2758 | bplist_destroy(&new_frees); | |
2759 | ||
2760 | char dsname[ZFS_MAX_DATASET_NAME_LEN]; | |
2761 | dsl_dataset_name(ds, dsname); | |
2762 | zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu " | |
2763 | "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu " | |
8e739b2c RE |
2764 | "(%llu blkptrs)", (u_longlong_t)tx->tx_txg, dsname, |
2765 | (u_longlong_t)ds->ds_object, (u_longlong_t)first_obj, | |
2766 | (u_longlong_t)cur_first_size, (u_longlong_t)next_obj, | |
2767 | (u_longlong_t)cur_next_size, | |
2768 | (u_longlong_t)first->dle_bpobj.bpo_object, | |
2769 | (u_longlong_t)first->dle_bpobj.bpo_phys->bpo_num_blkptrs); | |
37f03da8 SH |
2770 | out: |
2771 | dmu_buf_rele(ds->ds_dbuf, spa); | |
2772 | spa->spa_to_condense.ds = NULL; | |
2773 | bplist_clear(&lca->to_keep); | |
2774 | bplist_destroy(&lca->to_keep); | |
2775 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2776 | spa->spa_to_condense.syncing = B_FALSE; | |
2777 | } | |
2778 | ||
65c7cc49 | 2779 | static void |
37f03da8 SH |
2780 | spa_livelist_condense_cb(void *arg, zthr_t *t) |
2781 | { | |
2782 | while (zfs_livelist_condense_zthr_pause && | |
2783 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2784 | delay(1); | |
2785 | ||
2786 | spa_t *spa = arg; | |
2787 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2788 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2789 | uint64_t first_size, next_size; | |
2790 | ||
2791 | livelist_condense_arg_t *lca = | |
2792 | kmem_alloc(sizeof (livelist_condense_arg_t), KM_SLEEP); | |
2793 | bplist_create(&lca->to_keep); | |
2794 | ||
2795 | /* | |
2796 | * Process the livelists (matching FREEs and ALLOCs) in open context | |
2797 | * so we have minimal work in syncing context to condense. | |
2798 | * | |
2799 | * We save bpobj sizes (first_size and next_size) to use later in | |
2800 | * syncing context to determine if entries were added to these sublists | |
2801 | * while in open context. This is possible because the clone is still | |
2802 | * active and open for normal writes and we want to make sure the new, | |
2803 | * unprocessed blockpointers are inserted into the livelist normally. | |
2804 | * | |
2805 | * Note that dsl_process_sub_livelist() both stores the size number of | |
2806 | * blockpointers and iterates over them while the bpobj's lock held, so | |
2807 | * the sizes returned to us are consistent which what was actually | |
2808 | * processed. | |
2809 | */ | |
2810 | int err = dsl_process_sub_livelist(&first->dle_bpobj, &lca->to_keep, t, | |
2811 | &first_size); | |
2812 | if (err == 0) | |
2813 | err = dsl_process_sub_livelist(&next->dle_bpobj, &lca->to_keep, | |
2814 | t, &next_size); | |
2815 | ||
2816 | if (err == 0) { | |
2817 | while (zfs_livelist_condense_sync_pause && | |
2818 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2819 | delay(1); | |
2820 | ||
2821 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
2822 | dmu_tx_mark_netfree(tx); | |
2823 | dmu_tx_hold_space(tx, 1); | |
2824 | err = dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE); | |
2825 | if (err == 0) { | |
2826 | /* | |
2827 | * Prevent the condense zthr restarting before | |
2828 | * the synctask completes. | |
2829 | */ | |
2830 | spa->spa_to_condense.syncing = B_TRUE; | |
2831 | lca->spa = spa; | |
2832 | lca->first_size = first_size; | |
2833 | lca->next_size = next_size; | |
2834 | dsl_sync_task_nowait(spa_get_dsl(spa), | |
38080324 | 2835 | spa_livelist_condense_sync, lca, tx); |
37f03da8 SH |
2836 | dmu_tx_commit(tx); |
2837 | return; | |
2838 | } | |
2839 | } | |
2840 | /* | |
2841 | * Condensing can not continue: either it was externally stopped or | |
2842 | * we were unable to assign to a tx because the pool has run out of | |
2843 | * space. In the second case, we'll just end up trying to condense | |
2844 | * again in a later txg. | |
2845 | */ | |
2846 | ASSERT(err != 0); | |
2847 | bplist_clear(&lca->to_keep); | |
2848 | bplist_destroy(&lca->to_keep); | |
2849 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2850 | dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, spa); | |
2851 | spa->spa_to_condense.ds = NULL; | |
2852 | if (err == EINTR) | |
2853 | zfs_livelist_condense_zthr_cancel++; | |
2854 | } | |
2855 | ||
37f03da8 SH |
2856 | /* |
2857 | * Check that there is something to condense but that a condense is not | |
2858 | * already in progress and that condensing has not been cancelled. | |
2859 | */ | |
2860 | static boolean_t | |
2861 | spa_livelist_condense_cb_check(void *arg, zthr_t *z) | |
2862 | { | |
14e4e3cb | 2863 | (void) z; |
37f03da8 SH |
2864 | spa_t *spa = arg; |
2865 | if ((spa->spa_to_condense.ds != NULL) && | |
2866 | (spa->spa_to_condense.syncing == B_FALSE) && | |
2867 | (spa->spa_to_condense.cancelled == B_FALSE)) { | |
2868 | return (B_TRUE); | |
2869 | } | |
2870 | return (B_FALSE); | |
2871 | } | |
2872 | ||
65c7cc49 | 2873 | static void |
37f03da8 SH |
2874 | spa_start_livelist_condensing_thread(spa_t *spa) |
2875 | { | |
2876 | spa->spa_to_condense.ds = NULL; | |
2877 | spa->spa_to_condense.first = NULL; | |
2878 | spa->spa_to_condense.next = NULL; | |
2879 | spa->spa_to_condense.syncing = B_FALSE; | |
2880 | spa->spa_to_condense.cancelled = B_FALSE; | |
2881 | ||
2882 | ASSERT3P(spa->spa_livelist_condense_zthr, ==, NULL); | |
843e9ca2 SD |
2883 | spa->spa_livelist_condense_zthr = |
2884 | zthr_create("z_livelist_condense", | |
2885 | spa_livelist_condense_cb_check, | |
6bc61d22 | 2886 | spa_livelist_condense_cb, spa, minclsyspri); |
37f03da8 SH |
2887 | } |
2888 | ||
9d5b5245 SD |
2889 | static void |
2890 | spa_spawn_aux_threads(spa_t *spa) | |
2891 | { | |
2892 | ASSERT(spa_writeable(spa)); | |
2893 | ||
2894 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2895 | ||
2896 | spa_start_indirect_condensing_thread(spa); | |
37f03da8 SH |
2897 | spa_start_livelist_destroy_thread(spa); |
2898 | spa_start_livelist_condensing_thread(spa); | |
d2734cce SD |
2899 | |
2900 | ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); | |
2901 | spa->spa_checkpoint_discard_zthr = | |
843e9ca2 SD |
2902 | zthr_create("z_checkpoint_discard", |
2903 | spa_checkpoint_discard_thread_check, | |
6bc61d22 | 2904 | spa_checkpoint_discard_thread, spa, minclsyspri); |
9d5b5245 SD |
2905 | } |
2906 | ||
428870ff BB |
2907 | /* |
2908 | * Fix up config after a partly-completed split. This is done with the | |
2909 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
2910 | * pool have that entry in their config, but only the splitting one contains | |
2911 | * a list of all the guids of the vdevs that are being split off. | |
2912 | * | |
2913 | * This function determines what to do with that list: either rejoin | |
2914 | * all the disks to the pool, or complete the splitting process. To attempt | |
2915 | * the rejoin, each disk that is offlined is marked online again, and | |
2916 | * we do a reopen() call. If the vdev label for every disk that was | |
2917 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
2918 | * then we call vdev_split() on each disk, and complete the split. | |
2919 | * | |
2920 | * Otherwise we leave the config alone, with all the vdevs in place in | |
2921 | * the original pool. | |
2922 | */ | |
2923 | static void | |
2924 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
2925 | { | |
2926 | uint_t extracted; | |
2927 | uint64_t *glist; | |
2928 | uint_t i, gcount; | |
2929 | nvlist_t *nvl; | |
2930 | vdev_t **vd; | |
2931 | boolean_t attempt_reopen; | |
2932 | ||
2933 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
2934 | return; | |
2935 | ||
2936 | /* check that the config is complete */ | |
2937 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
2938 | &glist, &gcount) != 0) | |
2939 | return; | |
2940 | ||
79c76d5b | 2941 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); |
428870ff BB |
2942 | |
2943 | /* attempt to online all the vdevs & validate */ | |
2944 | attempt_reopen = B_TRUE; | |
2945 | for (i = 0; i < gcount; i++) { | |
2946 | if (glist[i] == 0) /* vdev is hole */ | |
2947 | continue; | |
2948 | ||
2949 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
2950 | if (vd[i] == NULL) { | |
2951 | /* | |
2952 | * Don't bother attempting to reopen the disks; | |
2953 | * just do the split. | |
2954 | */ | |
2955 | attempt_reopen = B_FALSE; | |
2956 | } else { | |
2957 | /* attempt to re-online it */ | |
2958 | vd[i]->vdev_offline = B_FALSE; | |
2959 | } | |
2960 | } | |
2961 | ||
2962 | if (attempt_reopen) { | |
2963 | vdev_reopen(spa->spa_root_vdev); | |
2964 | ||
2965 | /* check each device to see what state it's in */ | |
2966 | for (extracted = 0, i = 0; i < gcount; i++) { | |
2967 | if (vd[i] != NULL && | |
2968 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
2969 | break; | |
2970 | ++extracted; | |
2971 | } | |
2972 | } | |
2973 | ||
2974 | /* | |
2975 | * If every disk has been moved to the new pool, or if we never | |
2976 | * even attempted to look at them, then we split them off for | |
2977 | * good. | |
2978 | */ | |
2979 | if (!attempt_reopen || gcount == extracted) { | |
2980 | for (i = 0; i < gcount; i++) | |
2981 | if (vd[i] != NULL) | |
2982 | vdev_split(vd[i]); | |
2983 | vdev_reopen(spa->spa_root_vdev); | |
2984 | } | |
2985 | ||
2986 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
2987 | } | |
2988 | ||
2989 | static int | |
6cb8e530 | 2990 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) |
428870ff | 2991 | { |
a926aab9 | 2992 | const char *ereport = FM_EREPORT_ZFS_POOL; |
428870ff | 2993 | int error; |
428870ff | 2994 | |
6cb8e530 | 2995 | spa->spa_load_state = state; |
ca95f70d OF |
2996 | (void) spa_import_progress_set_state(spa_guid(spa), |
2997 | spa_load_state(spa)); | |
9ae529ec | 2998 | |
6cb8e530 | 2999 | gethrestime(&spa->spa_loaded_ts); |
d2734cce | 3000 | error = spa_load_impl(spa, type, &ereport); |
428870ff | 3001 | |
0c66c32d JG |
3002 | /* |
3003 | * Don't count references from objsets that are already closed | |
3004 | * and are making their way through the eviction process. | |
3005 | */ | |
3006 | spa_evicting_os_wait(spa); | |
424fd7c3 | 3007 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
572e2857 BB |
3008 | if (error) { |
3009 | if (error != EEXIST) { | |
3010 | spa->spa_loaded_ts.tv_sec = 0; | |
3011 | spa->spa_loaded_ts.tv_nsec = 0; | |
3012 | } | |
3013 | if (error != EBADF) { | |
1144586b | 3014 | (void) zfs_ereport_post(ereport, spa, |
4f072827 | 3015 | NULL, NULL, NULL, 0); |
572e2857 BB |
3016 | } |
3017 | } | |
428870ff BB |
3018 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; |
3019 | spa->spa_ena = 0; | |
3020 | ||
ca95f70d OF |
3021 | (void) spa_import_progress_set_state(spa_guid(spa), |
3022 | spa_load_state(spa)); | |
3023 | ||
428870ff BB |
3024 | return (error); |
3025 | } | |
3026 | ||
33cf67cd | 3027 | #ifdef ZFS_DEBUG |
e0ab3ab5 JS |
3028 | /* |
3029 | * Count the number of per-vdev ZAPs associated with all of the vdevs in the | |
3030 | * vdev tree rooted in the given vd, and ensure that each ZAP is present in the | |
3031 | * spa's per-vdev ZAP list. | |
3032 | */ | |
3033 | static uint64_t | |
3034 | vdev_count_verify_zaps(vdev_t *vd) | |
3035 | { | |
3036 | spa_t *spa = vd->vdev_spa; | |
3037 | uint64_t total = 0; | |
e0ab3ab5 JS |
3038 | |
3039 | if (vd->vdev_top_zap != 0) { | |
3040 | total++; | |
3041 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
3042 | spa->spa_all_vdev_zaps, vd->vdev_top_zap)); | |
3043 | } | |
3044 | if (vd->vdev_leaf_zap != 0) { | |
3045 | total++; | |
3046 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
3047 | spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); | |
3048 | } | |
3049 | ||
1c27024e | 3050 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
3051 | total += vdev_count_verify_zaps(vd->vdev_child[i]); |
3052 | } | |
3053 | ||
3054 | return (total); | |
3055 | } | |
36542b06 AZ |
3056 | #else |
3057 | #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0) | |
33cf67cd | 3058 | #endif |
e0ab3ab5 | 3059 | |
379ca9cf OF |
3060 | /* |
3061 | * Determine whether the activity check is required. | |
3062 | */ | |
3063 | static boolean_t | |
bbffb59e BB |
3064 | spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label, |
3065 | nvlist_t *config) | |
379ca9cf OF |
3066 | { |
3067 | uint64_t state = 0; | |
3068 | uint64_t hostid = 0; | |
3069 | uint64_t tryconfig_txg = 0; | |
3070 | uint64_t tryconfig_timestamp = 0; | |
060f0226 | 3071 | uint16_t tryconfig_mmp_seq = 0; |
379ca9cf OF |
3072 | nvlist_t *nvinfo; |
3073 | ||
3074 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3075 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
3076 | (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, | |
3077 | &tryconfig_txg); | |
3078 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
3079 | &tryconfig_timestamp); | |
060f0226 OF |
3080 | (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ, |
3081 | &tryconfig_mmp_seq); | |
379ca9cf OF |
3082 | } |
3083 | ||
3084 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); | |
379ca9cf OF |
3085 | |
3086 | /* | |
3087 | * Disable the MMP activity check - This is used by zdb which | |
3088 | * is intended to be used on potentially active pools. | |
3089 | */ | |
3090 | if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) | |
3091 | return (B_FALSE); | |
3092 | ||
3093 | /* | |
3094 | * Skip the activity check when the MMP feature is disabled. | |
3095 | */ | |
3096 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) | |
3097 | return (B_FALSE); | |
ca95f70d | 3098 | |
379ca9cf | 3099 | /* |
060f0226 OF |
3100 | * If the tryconfig_ values are nonzero, they are the results of an |
3101 | * earlier tryimport. If they all match the uberblock we just found, | |
3102 | * then the pool has not changed and we return false so we do not test | |
3103 | * a second time. | |
379ca9cf OF |
3104 | */ |
3105 | if (tryconfig_txg && tryconfig_txg == ub->ub_txg && | |
060f0226 OF |
3106 | tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp && |
3107 | tryconfig_mmp_seq && tryconfig_mmp_seq == | |
3108 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) | |
379ca9cf OF |
3109 | return (B_FALSE); |
3110 | ||
3111 | /* | |
3112 | * Allow the activity check to be skipped when importing the pool | |
bbffb59e BB |
3113 | * on the same host which last imported it. Since the hostid from |
3114 | * configuration may be stale use the one read from the label. | |
379ca9cf | 3115 | */ |
bbffb59e BB |
3116 | if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) |
3117 | hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); | |
3118 | ||
25f06d67 | 3119 | if (hostid == spa_get_hostid(spa)) |
379ca9cf OF |
3120 | return (B_FALSE); |
3121 | ||
3122 | /* | |
3123 | * Skip the activity test when the pool was cleanly exported. | |
3124 | */ | |
3125 | if (state != POOL_STATE_ACTIVE) | |
3126 | return (B_FALSE); | |
3127 | ||
3128 | return (B_TRUE); | |
3129 | } | |
3130 | ||
060f0226 OF |
3131 | /* |
3132 | * Nanoseconds the activity check must watch for changes on-disk. | |
3133 | */ | |
3134 | static uint64_t | |
3135 | spa_activity_check_duration(spa_t *spa, uberblock_t *ub) | |
3136 | { | |
3137 | uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); | |
3138 | uint64_t multihost_interval = MSEC2NSEC( | |
3139 | MMP_INTERVAL_OK(zfs_multihost_interval)); | |
3140 | uint64_t import_delay = MAX(NANOSEC, import_intervals * | |
3141 | multihost_interval); | |
3142 | ||
3143 | /* | |
3144 | * Local tunables determine a minimum duration except for the case | |
3145 | * where we know when the remote host will suspend the pool if MMP | |
3146 | * writes do not land. | |
3147 | * | |
3148 | * See Big Theory comment at the top of mmp.c for the reasoning behind | |
3149 | * these cases and times. | |
3150 | */ | |
3151 | ||
3152 | ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100); | |
3153 | ||
3154 | if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3155 | MMP_FAIL_INT(ub) > 0) { | |
3156 | ||
3157 | /* MMP on remote host will suspend pool after failed writes */ | |
3158 | import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) * | |
3159 | MMP_IMPORT_SAFETY_FACTOR / 100; | |
3160 | ||
3161 | zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp " | |
3162 | "mmp_fails=%llu ub_mmp mmp_interval=%llu " | |
8e739b2c RE |
3163 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3164 | (u_longlong_t)MMP_FAIL_INT(ub), | |
3165 | (u_longlong_t)MMP_INTERVAL(ub), | |
3166 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3167 | |
3168 | } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3169 | MMP_FAIL_INT(ub) == 0) { | |
3170 | ||
3171 | /* MMP on remote host will never suspend pool */ | |
3172 | import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) + | |
3173 | ub->ub_mmp_delay) * import_intervals); | |
3174 | ||
3175 | zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp " | |
3176 | "mmp_interval=%llu ub_mmp_delay=%llu " | |
8e739b2c RE |
3177 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3178 | (u_longlong_t)MMP_INTERVAL(ub), | |
3179 | (u_longlong_t)ub->ub_mmp_delay, | |
3180 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3181 | |
3182 | } else if (MMP_VALID(ub)) { | |
3183 | /* | |
e1cfd73f | 3184 | * zfs-0.7 compatibility case |
060f0226 OF |
3185 | */ |
3186 | ||
3187 | import_delay = MAX(import_delay, (multihost_interval + | |
3188 | ub->ub_mmp_delay) * import_intervals); | |
3189 | ||
3190 | zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu " | |
8e739b2c RE |
3191 | "import_intervals=%llu leaves=%u", |
3192 | (u_longlong_t)import_delay, | |
3193 | (u_longlong_t)ub->ub_mmp_delay, | |
3194 | (u_longlong_t)import_intervals, | |
060f0226 OF |
3195 | vdev_count_leaves(spa)); |
3196 | } else { | |
3197 | /* Using local tunings is the only reasonable option */ | |
3198 | zfs_dbgmsg("pool last imported on non-MMP aware " | |
3199 | "host using import_delay=%llu multihost_interval=%llu " | |
8e739b2c RE |
3200 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3201 | (u_longlong_t)multihost_interval, | |
3202 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3203 | } |
3204 | ||
3205 | return (import_delay); | |
3206 | } | |
3207 | ||
379ca9cf OF |
3208 | /* |
3209 | * Perform the import activity check. If the user canceled the import or | |
3210 | * we detected activity then fail. | |
3211 | */ | |
3212 | static int | |
3213 | spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) | |
3214 | { | |
379ca9cf OF |
3215 | uint64_t txg = ub->ub_txg; |
3216 | uint64_t timestamp = ub->ub_timestamp; | |
060f0226 OF |
3217 | uint64_t mmp_config = ub->ub_mmp_config; |
3218 | uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0; | |
3219 | uint64_t import_delay; | |
379ca9cf OF |
3220 | hrtime_t import_expire; |
3221 | nvlist_t *mmp_label = NULL; | |
3222 | vdev_t *rvd = spa->spa_root_vdev; | |
3223 | kcondvar_t cv; | |
3224 | kmutex_t mtx; | |
3225 | int error = 0; | |
3226 | ||
3227 | cv_init(&cv, NULL, CV_DEFAULT, NULL); | |
3228 | mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); | |
3229 | mutex_enter(&mtx); | |
3230 | ||
3231 | /* | |
3232 | * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed | |
3233 | * during the earlier tryimport. If the txg recorded there is 0 then | |
3234 | * the pool is known to be active on another host. | |
3235 | * | |
060f0226 | 3236 | * Otherwise, the pool might be in use on another host. Check for |
379ca9cf OF |
3237 | * changes in the uberblocks on disk if necessary. |
3238 | */ | |
3239 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3240 | nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, | |
3241 | ZPOOL_CONFIG_LOAD_INFO); | |
3242 | ||
3243 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && | |
3244 | fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { | |
3245 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
3246 | error = SET_ERROR(EREMOTEIO); | |
3247 | goto out; | |
3248 | } | |
3249 | } | |
3250 | ||
060f0226 | 3251 | import_delay = spa_activity_check_duration(spa, ub); |
533ea041 | 3252 | |
379ca9cf | 3253 | /* Add a small random factor in case of simultaneous imports (0-25%) */ |
29274c9f | 3254 | import_delay += import_delay * random_in_range(250) / 1000; |
ca95f70d OF |
3255 | |
3256 | import_expire = gethrtime() + import_delay; | |
379ca9cf OF |
3257 | |
3258 | while (gethrtime() < import_expire) { | |
ca95f70d OF |
3259 | (void) spa_import_progress_set_mmp_check(spa_guid(spa), |
3260 | NSEC2SEC(import_expire - gethrtime())); | |
3261 | ||
379ca9cf OF |
3262 | vdev_uberblock_load(rvd, ub, &mmp_label); |
3263 | ||
060f0226 OF |
3264 | if (txg != ub->ub_txg || timestamp != ub->ub_timestamp || |
3265 | mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) { | |
3266 | zfs_dbgmsg("multihost activity detected " | |
3267 | "txg %llu ub_txg %llu " | |
3268 | "timestamp %llu ub_timestamp %llu " | |
3269 | "mmp_config %#llx ub_mmp_config %#llx", | |
8e739b2c RE |
3270 | (u_longlong_t)txg, (u_longlong_t)ub->ub_txg, |
3271 | (u_longlong_t)timestamp, | |
3272 | (u_longlong_t)ub->ub_timestamp, | |
3273 | (u_longlong_t)mmp_config, | |
3274 | (u_longlong_t)ub->ub_mmp_config); | |
060f0226 | 3275 | |
379ca9cf OF |
3276 | error = SET_ERROR(EREMOTEIO); |
3277 | break; | |
3278 | } | |
3279 | ||
3280 | if (mmp_label) { | |
3281 | nvlist_free(mmp_label); | |
3282 | mmp_label = NULL; | |
3283 | } | |
3284 | ||
3285 | error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); | |
3286 | if (error != -1) { | |
3287 | error = SET_ERROR(EINTR); | |
3288 | break; | |
3289 | } | |
3290 | error = 0; | |
3291 | } | |
3292 | ||
3293 | out: | |
3294 | mutex_exit(&mtx); | |
3295 | mutex_destroy(&mtx); | |
3296 | cv_destroy(&cv); | |
3297 | ||
3298 | /* | |
3299 | * If the pool is determined to be active store the status in the | |
3300 | * spa->spa_load_info nvlist. If the remote hostname or hostid are | |
3301 | * available from configuration read from disk store them as well. | |
3302 | * This allows 'zpool import' to generate a more useful message. | |
3303 | * | |
3304 | * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) | |
3305 | * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool | |
3306 | * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool | |
3307 | */ | |
3308 | if (error == EREMOTEIO) { | |
a926aab9 | 3309 | const char *hostname = "<unknown>"; |
379ca9cf OF |
3310 | uint64_t hostid = 0; |
3311 | ||
3312 | if (mmp_label) { | |
3313 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { | |
3314 | hostname = fnvlist_lookup_string(mmp_label, | |
3315 | ZPOOL_CONFIG_HOSTNAME); | |
3316 | fnvlist_add_string(spa->spa_load_info, | |
3317 | ZPOOL_CONFIG_MMP_HOSTNAME, hostname); | |
3318 | } | |
3319 | ||
3320 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { | |
3321 | hostid = fnvlist_lookup_uint64(mmp_label, | |
3322 | ZPOOL_CONFIG_HOSTID); | |
3323 | fnvlist_add_uint64(spa->spa_load_info, | |
3324 | ZPOOL_CONFIG_MMP_HOSTID, hostid); | |
3325 | } | |
3326 | } | |
3327 | ||
3328 | fnvlist_add_uint64(spa->spa_load_info, | |
3329 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); | |
3330 | fnvlist_add_uint64(spa->spa_load_info, | |
3331 | ZPOOL_CONFIG_MMP_TXG, 0); | |
3332 | ||
3333 | error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); | |
3334 | } | |
3335 | ||
3336 | if (mmp_label) | |
3337 | nvlist_free(mmp_label); | |
3338 | ||
3339 | return (error); | |
3340 | } | |
3341 | ||
9eb7b46e | 3342 | static int |
6cb8e530 PZ |
3343 | spa_verify_host(spa_t *spa, nvlist_t *mos_config) |
3344 | { | |
3345 | uint64_t hostid; | |
3346 | char *hostname; | |
3347 | uint64_t myhostid = 0; | |
3348 | ||
3349 | if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, | |
3350 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
3351 | hostname = fnvlist_lookup_string(mos_config, | |
3352 | ZPOOL_CONFIG_HOSTNAME); | |
3353 | ||
3354 | myhostid = zone_get_hostid(NULL); | |
3355 | ||
3356 | if (hostid != 0 && myhostid != 0 && hostid != myhostid) { | |
3357 | cmn_err(CE_WARN, "pool '%s' could not be " | |
3358 | "loaded as it was last accessed by " | |
3359 | "another system (host: %s hostid: 0x%llx). " | |
a2f944a1 RM |
3360 | "See: https://openzfs.github.io/openzfs-docs/msg/" |
3361 | "ZFS-8000-EY", | |
6cb8e530 PZ |
3362 | spa_name(spa), hostname, (u_longlong_t)hostid); |
3363 | spa_load_failed(spa, "hostid verification failed: pool " | |
3364 | "last accessed by host: %s (hostid: 0x%llx)", | |
3365 | hostname, (u_longlong_t)hostid); | |
3366 | return (SET_ERROR(EBADF)); | |
3367 | } | |
3368 | } | |
3369 | ||
3370 | return (0); | |
3371 | } | |
3372 | ||
3373 | static int | |
3374 | spa_ld_parse_config(spa_t *spa, spa_import_type_t type) | |
428870ff BB |
3375 | { |
3376 | int error = 0; | |
6cb8e530 | 3377 | nvlist_t *nvtree, *nvl, *config = spa->spa_config; |
1c27024e | 3378 | int parse; |
9eb7b46e | 3379 | vdev_t *rvd; |
6cb8e530 PZ |
3380 | uint64_t pool_guid; |
3381 | char *comment; | |
658fb802 | 3382 | char *compatibility; |
6cb8e530 PZ |
3383 | |
3384 | /* | |
3385 | * Versioning wasn't explicitly added to the label until later, so if | |
3386 | * it's not present treat it as the initial version. | |
3387 | */ | |
3388 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
3389 | &spa->spa_ubsync.ub_version) != 0) | |
3390 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
3391 | ||
3392 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { | |
3393 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3394 | ZPOOL_CONFIG_POOL_GUID); | |
3395 | return (SET_ERROR(EINVAL)); | |
3396 | } | |
3397 | ||
d2734cce SD |
3398 | /* |
3399 | * If we are doing an import, ensure that the pool is not already | |
3400 | * imported by checking if its pool guid already exists in the | |
3401 | * spa namespace. | |
3402 | * | |
3403 | * The only case that we allow an already imported pool to be | |
3404 | * imported again, is when the pool is checkpointed and we want to | |
3405 | * look at its checkpointed state from userland tools like zdb. | |
3406 | */ | |
3407 | #ifdef _KERNEL | |
3408 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3409 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3410 | spa_guid_exists(pool_guid, 0)) { | |
3411 | #else | |
3412 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3413 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3414 | spa_guid_exists(pool_guid, 0) && | |
3415 | !spa_importing_readonly_checkpoint(spa)) { | |
3416 | #endif | |
6cb8e530 PZ |
3417 | spa_load_failed(spa, "a pool with guid %llu is already open", |
3418 | (u_longlong_t)pool_guid); | |
3419 | return (SET_ERROR(EEXIST)); | |
3420 | } | |
3421 | ||
3422 | spa->spa_config_guid = pool_guid; | |
3423 | ||
3424 | nvlist_free(spa->spa_load_info); | |
3425 | spa->spa_load_info = fnvlist_alloc(); | |
3426 | ||
3427 | ASSERT(spa->spa_comment == NULL); | |
3428 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
3429 | spa->spa_comment = spa_strdup(comment); | |
3430 | ||
658fb802 CB |
3431 | ASSERT(spa->spa_compatibility == NULL); |
3432 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMPATIBILITY, | |
3433 | &compatibility) == 0) | |
3434 | spa->spa_compatibility = spa_strdup(compatibility); | |
3435 | ||
6cb8e530 PZ |
3436 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, |
3437 | &spa->spa_config_txg); | |
3438 | ||
3439 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) | |
3440 | spa->spa_config_splitting = fnvlist_dup(nvl); | |
428870ff | 3441 | |
4a0ee12a PZ |
3442 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { |
3443 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3444 | ZPOOL_CONFIG_VDEV_TREE); | |
2e528b49 | 3445 | return (SET_ERROR(EINVAL)); |
4a0ee12a | 3446 | } |
428870ff | 3447 | |
428870ff BB |
3448 | /* |
3449 | * Create "The Godfather" zio to hold all async IOs | |
3450 | */ | |
e022864d MA |
3451 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
3452 | KM_SLEEP); | |
1c27024e | 3453 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
3454 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
3455 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
3456 | ZIO_FLAG_GODFATHER); | |
3457 | } | |
428870ff BB |
3458 | |
3459 | /* | |
3460 | * Parse the configuration into a vdev tree. We explicitly set the | |
3461 | * value that will be returned by spa_version() since parsing the | |
3462 | * configuration requires knowing the version number. | |
3463 | */ | |
3464 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 PZ |
3465 | parse = (type == SPA_IMPORT_EXISTING ? |
3466 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
9eb7b46e | 3467 | error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); |
428870ff BB |
3468 | spa_config_exit(spa, SCL_ALL, FTAG); |
3469 | ||
4a0ee12a PZ |
3470 | if (error != 0) { |
3471 | spa_load_failed(spa, "unable to parse config [error=%d]", | |
3472 | error); | |
428870ff | 3473 | return (error); |
4a0ee12a | 3474 | } |
428870ff BB |
3475 | |
3476 | ASSERT(spa->spa_root_vdev == rvd); | |
c3520e7f MA |
3477 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); |
3478 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
428870ff BB |
3479 | |
3480 | if (type != SPA_IMPORT_ASSEMBLE) { | |
3481 | ASSERT(spa_guid(spa) == pool_guid); | |
3482 | } | |
3483 | ||
9eb7b46e PZ |
3484 | return (0); |
3485 | } | |
3486 | ||
6cb8e530 PZ |
3487 | /* |
3488 | * Recursively open all vdevs in the vdev tree. This function is called twice: | |
3489 | * first with the untrusted config, then with the trusted config. | |
3490 | */ | |
9eb7b46e PZ |
3491 | static int |
3492 | spa_ld_open_vdevs(spa_t *spa) | |
3493 | { | |
3494 | int error = 0; | |
3495 | ||
6cb8e530 PZ |
3496 | /* |
3497 | * spa_missing_tvds_allowed defines how many top-level vdevs can be | |
3498 | * missing/unopenable for the root vdev to be still considered openable. | |
3499 | */ | |
3500 | if (spa->spa_trust_config) { | |
3501 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; | |
3502 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { | |
3503 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; | |
3504 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { | |
3505 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; | |
3506 | } else { | |
3507 | spa->spa_missing_tvds_allowed = 0; | |
3508 | } | |
3509 | ||
3510 | spa->spa_missing_tvds_allowed = | |
3511 | MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); | |
3512 | ||
428870ff | 3513 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9eb7b46e | 3514 | error = vdev_open(spa->spa_root_vdev); |
428870ff | 3515 | spa_config_exit(spa, SCL_ALL, FTAG); |
6cb8e530 PZ |
3516 | |
3517 | if (spa->spa_missing_tvds != 0) { | |
3518 | spa_load_note(spa, "vdev tree has %lld missing top-level " | |
3519 | "vdevs.", (u_longlong_t)spa->spa_missing_tvds); | |
da92d5cb | 3520 | if (spa->spa_trust_config && (spa->spa_mode & SPA_MODE_WRITE)) { |
6cb8e530 PZ |
3521 | /* |
3522 | * Although theoretically we could allow users to open | |
3523 | * incomplete pools in RW mode, we'd need to add a lot | |
3524 | * of extra logic (e.g. adjust pool space to account | |
3525 | * for missing vdevs). | |
3526 | * This limitation also prevents users from accidentally | |
3527 | * opening the pool in RW mode during data recovery and | |
3528 | * damaging it further. | |
3529 | */ | |
3530 | spa_load_note(spa, "pools with missing top-level " | |
3531 | "vdevs can only be opened in read-only mode."); | |
3532 | error = SET_ERROR(ENXIO); | |
3533 | } else { | |
3534 | spa_load_note(spa, "current settings allow for maximum " | |
3535 | "%lld missing top-level vdevs at this stage.", | |
3536 | (u_longlong_t)spa->spa_missing_tvds_allowed); | |
3537 | } | |
3538 | } | |
4a0ee12a PZ |
3539 | if (error != 0) { |
3540 | spa_load_failed(spa, "unable to open vdev tree [error=%d]", | |
3541 | error); | |
3542 | } | |
6cb8e530 PZ |
3543 | if (spa->spa_missing_tvds != 0 || error != 0) |
3544 | vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); | |
9eb7b46e PZ |
3545 | |
3546 | return (error); | |
3547 | } | |
3548 | ||
6cb8e530 PZ |
3549 | /* |
3550 | * We need to validate the vdev labels against the configuration that | |
3551 | * we have in hand. This function is called twice: first with an untrusted | |
3552 | * config, then with a trusted config. The validation is more strict when the | |
3553 | * config is trusted. | |
3554 | */ | |
9eb7b46e | 3555 | static int |
6cb8e530 | 3556 | spa_ld_validate_vdevs(spa_t *spa) |
9eb7b46e PZ |
3557 | { |
3558 | int error = 0; | |
3559 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 3560 | |
6cb8e530 PZ |
3561 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3562 | error = vdev_validate(rvd); | |
3563 | spa_config_exit(spa, SCL_ALL, FTAG); | |
428870ff | 3564 | |
6cb8e530 PZ |
3565 | if (error != 0) { |
3566 | spa_load_failed(spa, "vdev_validate failed [error=%d]", error); | |
3567 | return (error); | |
3568 | } | |
428870ff | 3569 | |
6cb8e530 PZ |
3570 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { |
3571 | spa_load_failed(spa, "cannot open vdev tree after invalidating " | |
3572 | "some vdevs"); | |
3573 | vdev_dbgmsg_print_tree(rvd, 2); | |
3574 | return (SET_ERROR(ENXIO)); | |
428870ff BB |
3575 | } |
3576 | ||
9eb7b46e PZ |
3577 | return (0); |
3578 | } | |
3579 | ||
d2734cce SD |
3580 | static void |
3581 | spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) | |
3582 | { | |
3583 | spa->spa_state = POOL_STATE_ACTIVE; | |
3584 | spa->spa_ubsync = spa->spa_uberblock; | |
3585 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
3586 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
3587 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
3588 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
3589 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
3590 | spa->spa_prev_software_version = ub->ub_software_version; | |
3591 | } | |
3592 | ||
9eb7b46e | 3593 | static int |
6cb8e530 | 3594 | spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) |
9eb7b46e PZ |
3595 | { |
3596 | vdev_t *rvd = spa->spa_root_vdev; | |
3597 | nvlist_t *label; | |
3598 | uberblock_t *ub = &spa->spa_uberblock; | |
9eb7b46e PZ |
3599 | boolean_t activity_check = B_FALSE; |
3600 | ||
d2734cce SD |
3601 | /* |
3602 | * If we are opening the checkpointed state of the pool by | |
3603 | * rewinding to it, at this point we will have written the | |
3604 | * checkpointed uberblock to the vdev labels, so searching | |
3605 | * the labels will find the right uberblock. However, if | |
3606 | * we are opening the checkpointed state read-only, we have | |
3607 | * not modified the labels. Therefore, we must ignore the | |
3608 | * labels and continue using the spa_uberblock that was set | |
3609 | * by spa_ld_checkpoint_rewind. | |
3610 | * | |
3611 | * Note that it would be fine to ignore the labels when | |
3612 | * rewinding (opening writeable) as well. However, if we | |
3613 | * crash just after writing the labels, we will end up | |
3614 | * searching the labels. Doing so in the common case means | |
3615 | * that this code path gets exercised normally, rather than | |
3616 | * just in the edge case. | |
3617 | */ | |
3618 | if (ub->ub_checkpoint_txg != 0 && | |
3619 | spa_importing_readonly_checkpoint(spa)) { | |
3620 | spa_ld_select_uberblock_done(spa, ub); | |
3621 | return (0); | |
3622 | } | |
3623 | ||
428870ff BB |
3624 | /* |
3625 | * Find the best uberblock. | |
3626 | */ | |
9ae529ec | 3627 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
3628 | |
3629 | /* | |
3630 | * If we weren't able to find a single valid uberblock, return failure. | |
3631 | */ | |
9ae529ec CS |
3632 | if (ub->ub_txg == 0) { |
3633 | nvlist_free(label); | |
4a0ee12a | 3634 | spa_load_failed(spa, "no valid uberblock found"); |
428870ff | 3635 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 3636 | } |
428870ff | 3637 | |
ca95f70d OF |
3638 | if (spa->spa_load_max_txg != UINT64_MAX) { |
3639 | (void) spa_import_progress_set_max_txg(spa_guid(spa), | |
3640 | (u_longlong_t)spa->spa_load_max_txg); | |
3641 | } | |
4a0ee12a PZ |
3642 | spa_load_note(spa, "using uberblock with txg=%llu", |
3643 | (u_longlong_t)ub->ub_txg); | |
3644 | ||
3645 | ||
379ca9cf OF |
3646 | /* |
3647 | * For pools which have the multihost property on determine if the | |
3648 | * pool is truly inactive and can be safely imported. Prevent | |
3649 | * hosts which don't have a hostid set from importing the pool. | |
3650 | */ | |
6cb8e530 PZ |
3651 | activity_check = spa_activity_check_required(spa, ub, label, |
3652 | spa->spa_config); | |
379ca9cf | 3653 | if (activity_check) { |
379ca9cf | 3654 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && |
25f06d67 | 3655 | spa_get_hostid(spa) == 0) { |
379ca9cf OF |
3656 | nvlist_free(label); |
3657 | fnvlist_add_uint64(spa->spa_load_info, | |
3658 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3659 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3660 | } | |
3661 | ||
6cb8e530 | 3662 | int error = spa_activity_check(spa, ub, spa->spa_config); |
e889f0f5 OF |
3663 | if (error) { |
3664 | nvlist_free(label); | |
3665 | return (error); | |
3666 | } | |
3667 | ||
379ca9cf OF |
3668 | fnvlist_add_uint64(spa->spa_load_info, |
3669 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); | |
3670 | fnvlist_add_uint64(spa->spa_load_info, | |
3671 | ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); | |
060f0226 OF |
3672 | fnvlist_add_uint16(spa->spa_load_info, |
3673 | ZPOOL_CONFIG_MMP_SEQ, | |
3674 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)); | |
379ca9cf OF |
3675 | } |
3676 | ||
428870ff | 3677 | /* |
9ae529ec | 3678 | * If the pool has an unsupported version we can't open it. |
428870ff | 3679 | */ |
9ae529ec CS |
3680 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
3681 | nvlist_free(label); | |
4a0ee12a PZ |
3682 | spa_load_failed(spa, "version %llu is not supported", |
3683 | (u_longlong_t)ub->ub_version); | |
428870ff | 3684 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
3685 | } |
3686 | ||
3687 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3688 | nvlist_t *features; | |
3689 | ||
3690 | /* | |
3691 | * If we weren't able to find what's necessary for reading the | |
3692 | * MOS in the label, return failure. | |
3693 | */ | |
4a0ee12a PZ |
3694 | if (label == NULL) { |
3695 | spa_load_failed(spa, "label config unavailable"); | |
3696 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
3697 | ENXIO)); | |
3698 | } | |
3699 | ||
3700 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, | |
3701 | &features) != 0) { | |
9ae529ec | 3702 | nvlist_free(label); |
4a0ee12a PZ |
3703 | spa_load_failed(spa, "invalid label: '%s' missing", |
3704 | ZPOOL_CONFIG_FEATURES_FOR_READ); | |
9ae529ec CS |
3705 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3706 | ENXIO)); | |
3707 | } | |
3708 | ||
3709 | /* | |
3710 | * Update our in-core representation with the definitive values | |
3711 | * from the label. | |
3712 | */ | |
3713 | nvlist_free(spa->spa_label_features); | |
65ad5d11 | 3714 | spa->spa_label_features = fnvlist_dup(features); |
9ae529ec CS |
3715 | } |
3716 | ||
3717 | nvlist_free(label); | |
3718 | ||
3719 | /* | |
3720 | * Look through entries in the label nvlist's features_for_read. If | |
3721 | * there is a feature listed there which we don't understand then we | |
3722 | * cannot open a pool. | |
3723 | */ | |
3724 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3725 | nvlist_t *unsup_feat; | |
9ae529ec | 3726 | |
65ad5d11 | 3727 | unsup_feat = fnvlist_alloc(); |
9ae529ec | 3728 | |
1c27024e DB |
3729 | for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, |
3730 | NULL); nvp != NULL; | |
9ae529ec CS |
3731 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { |
3732 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
65ad5d11 AJ |
3733 | fnvlist_add_string(unsup_feat, |
3734 | nvpair_name(nvp), ""); | |
9ae529ec CS |
3735 | } |
3736 | } | |
3737 | ||
3738 | if (!nvlist_empty(unsup_feat)) { | |
65ad5d11 AJ |
3739 | fnvlist_add_nvlist(spa->spa_load_info, |
3740 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec | 3741 | nvlist_free(unsup_feat); |
4a0ee12a | 3742 | spa_load_failed(spa, "some features are unsupported"); |
9ae529ec CS |
3743 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3744 | ENOTSUP)); | |
3745 | } | |
3746 | ||
3747 | nvlist_free(unsup_feat); | |
3748 | } | |
428870ff | 3749 | |
428870ff BB |
3750 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { |
3751 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 | 3752 | spa_try_repair(spa, spa->spa_config); |
428870ff BB |
3753 | spa_config_exit(spa, SCL_ALL, FTAG); |
3754 | nvlist_free(spa->spa_config_splitting); | |
3755 | spa->spa_config_splitting = NULL; | |
3756 | } | |
3757 | ||
3758 | /* | |
3759 | * Initialize internal SPA structures. | |
3760 | */ | |
d2734cce | 3761 | spa_ld_select_uberblock_done(spa, ub); |
428870ff | 3762 | |
9eb7b46e PZ |
3763 | return (0); |
3764 | } | |
3765 | ||
3766 | static int | |
3767 | spa_ld_open_rootbp(spa_t *spa) | |
3768 | { | |
3769 | int error = 0; | |
3770 | vdev_t *rvd = spa->spa_root_vdev; | |
a1d477c2 | 3771 | |
9ae529ec | 3772 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
4a0ee12a PZ |
3773 | if (error != 0) { |
3774 | spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " | |
3775 | "[error=%d]", error); | |
428870ff | 3776 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3777 | } |
428870ff BB |
3778 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; |
3779 | ||
9eb7b46e PZ |
3780 | return (0); |
3781 | } | |
3782 | ||
3783 | static int | |
d2734cce | 3784 | spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, |
6cb8e530 | 3785 | boolean_t reloading) |
9eb7b46e | 3786 | { |
6cb8e530 PZ |
3787 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
3788 | nvlist_t *nv, *mos_config, *policy; | |
3789 | int error = 0, copy_error; | |
3790 | uint64_t healthy_tvds, healthy_tvds_mos; | |
3791 | uint64_t mos_config_txg; | |
9eb7b46e | 3792 | |
4a0ee12a PZ |
3793 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) |
3794 | != 0) | |
428870ff BB |
3795 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3796 | ||
a1d477c2 | 3797 | /* |
6cb8e530 PZ |
3798 | * If we're assembling a pool from a split, the config provided is |
3799 | * already trusted so there is nothing to do. | |
a1d477c2 | 3800 | */ |
6cb8e530 PZ |
3801 | if (type == SPA_IMPORT_ASSEMBLE) |
3802 | return (0); | |
3803 | ||
3804 | healthy_tvds = spa_healthy_core_tvds(spa); | |
a1d477c2 | 3805 | |
6cb8e530 PZ |
3806 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) |
3807 | != 0) { | |
3808 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
3809 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3810 | } | |
3811 | ||
3812 | /* | |
3813 | * If we are doing an open, pool owner wasn't verified yet, thus do | |
3814 | * the verification here. | |
3815 | */ | |
3816 | if (spa->spa_load_state == SPA_LOAD_OPEN) { | |
3817 | error = spa_verify_host(spa, mos_config); | |
3818 | if (error != 0) { | |
a1d477c2 | 3819 | nvlist_free(mos_config); |
6cb8e530 | 3820 | return (error); |
a1d477c2 | 3821 | } |
6cb8e530 PZ |
3822 | } |
3823 | ||
3824 | nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); | |
a1d477c2 | 3825 | |
6cb8e530 PZ |
3826 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3827 | ||
3828 | /* | |
3829 | * Build a new vdev tree from the trusted config | |
3830 | */ | |
b2255edc BB |
3831 | error = spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD); |
3832 | if (error != 0) { | |
3833 | nvlist_free(mos_config); | |
3834 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3835 | spa_load_failed(spa, "spa_config_parse failed [error=%d]", | |
3836 | error); | |
3837 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
3838 | } | |
6cb8e530 PZ |
3839 | |
3840 | /* | |
3841 | * Vdev paths in the MOS may be obsolete. If the untrusted config was | |
3842 | * obtained by scanning /dev/dsk, then it will have the right vdev | |
3843 | * paths. We update the trusted MOS config with this information. | |
3844 | * We first try to copy the paths with vdev_copy_path_strict, which | |
3845 | * succeeds only when both configs have exactly the same vdev tree. | |
3846 | * If that fails, we fall back to a more flexible method that has a | |
3847 | * best effort policy. | |
3848 | */ | |
3849 | copy_error = vdev_copy_path_strict(rvd, mrvd); | |
3850 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3851 | spa_load_note(spa, "provided vdev tree:"); | |
3852 | vdev_dbgmsg_print_tree(rvd, 2); | |
3853 | spa_load_note(spa, "MOS vdev tree:"); | |
3854 | vdev_dbgmsg_print_tree(mrvd, 2); | |
3855 | } | |
3856 | if (copy_error != 0) { | |
3857 | spa_load_note(spa, "vdev_copy_path_strict failed, falling " | |
3858 | "back to vdev_copy_path_relaxed"); | |
3859 | vdev_copy_path_relaxed(rvd, mrvd); | |
3860 | } | |
3861 | ||
3862 | vdev_close(rvd); | |
3863 | vdev_free(rvd); | |
3864 | spa->spa_root_vdev = mrvd; | |
3865 | rvd = mrvd; | |
3866 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3867 | ||
3868 | /* | |
3869 | * We will use spa_config if we decide to reload the spa or if spa_load | |
3870 | * fails and we rewind. We must thus regenerate the config using the | |
8a393be3 PZ |
3871 | * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to |
3872 | * pass settings on how to load the pool and is not stored in the MOS. | |
3873 | * We copy it over to our new, trusted config. | |
6cb8e530 PZ |
3874 | */ |
3875 | mos_config_txg = fnvlist_lookup_uint64(mos_config, | |
3876 | ZPOOL_CONFIG_POOL_TXG); | |
3877 | nvlist_free(mos_config); | |
3878 | mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); | |
8a393be3 | 3879 | if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, |
6cb8e530 | 3880 | &policy) == 0) |
8a393be3 | 3881 | fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); |
6cb8e530 PZ |
3882 | spa_config_set(spa, mos_config); |
3883 | spa->spa_config_source = SPA_CONFIG_SRC_MOS; | |
3884 | ||
3885 | /* | |
3886 | * Now that we got the config from the MOS, we should be more strict | |
3887 | * in checking blkptrs and can make assumptions about the consistency | |
3888 | * of the vdev tree. spa_trust_config must be set to true before opening | |
3889 | * vdevs in order for them to be writeable. | |
3890 | */ | |
3891 | spa->spa_trust_config = B_TRUE; | |
3892 | ||
3893 | /* | |
3894 | * Open and validate the new vdev tree | |
3895 | */ | |
3896 | error = spa_ld_open_vdevs(spa); | |
3897 | if (error != 0) | |
3898 | return (error); | |
3899 | ||
3900 | error = spa_ld_validate_vdevs(spa); | |
3901 | if (error != 0) | |
3902 | return (error); | |
3903 | ||
3904 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3905 | spa_load_note(spa, "final vdev tree:"); | |
3906 | vdev_dbgmsg_print_tree(rvd, 2); | |
3907 | } | |
3908 | ||
3909 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && | |
3910 | !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { | |
a1d477c2 | 3911 | /* |
6cb8e530 PZ |
3912 | * Sanity check to make sure that we are indeed loading the |
3913 | * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds | |
3914 | * in the config provided and they happened to be the only ones | |
3915 | * to have the latest uberblock, we could involuntarily perform | |
3916 | * an extreme rewind. | |
a1d477c2 | 3917 | */ |
6cb8e530 PZ |
3918 | healthy_tvds_mos = spa_healthy_core_tvds(spa); |
3919 | if (healthy_tvds_mos - healthy_tvds >= | |
3920 | SPA_SYNC_MIN_VDEVS) { | |
3921 | spa_load_note(spa, "config provided misses too many " | |
3922 | "top-level vdevs compared to MOS (%lld vs %lld). ", | |
3923 | (u_longlong_t)healthy_tvds, | |
3924 | (u_longlong_t)healthy_tvds_mos); | |
3925 | spa_load_note(spa, "vdev tree:"); | |
3926 | vdev_dbgmsg_print_tree(rvd, 2); | |
3927 | if (reloading) { | |
3928 | spa_load_failed(spa, "config was already " | |
3929 | "provided from MOS. Aborting."); | |
3930 | return (spa_vdev_err(rvd, | |
3931 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3932 | } | |
3933 | spa_load_note(spa, "spa must be reloaded using MOS " | |
3934 | "config"); | |
3935 | return (SET_ERROR(EAGAIN)); | |
4a0ee12a | 3936 | } |
a1d477c2 MA |
3937 | } |
3938 | ||
6cb8e530 PZ |
3939 | error = spa_check_for_missing_logs(spa); |
3940 | if (error != 0) | |
3941 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
3942 | ||
3943 | if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { | |
3944 | spa_load_failed(spa, "uberblock guid sum doesn't match MOS " | |
3945 | "guid sum (%llu != %llu)", | |
3946 | (u_longlong_t)spa->spa_uberblock.ub_guid_sum, | |
3947 | (u_longlong_t)rvd->vdev_guid_sum); | |
3948 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
3949 | ENXIO)); | |
3950 | } | |
3951 | ||
9eb7b46e PZ |
3952 | return (0); |
3953 | } | |
3954 | ||
3955 | static int | |
3956 | spa_ld_open_indirect_vdev_metadata(spa_t *spa) | |
3957 | { | |
3958 | int error = 0; | |
3959 | vdev_t *rvd = spa->spa_root_vdev; | |
3960 | ||
a1d477c2 MA |
3961 | /* |
3962 | * Everything that we read before spa_remove_init() must be stored | |
3963 | * on concreted vdevs. Therefore we do this as early as possible. | |
3964 | */ | |
4a0ee12a PZ |
3965 | error = spa_remove_init(spa); |
3966 | if (error != 0) { | |
3967 | spa_load_failed(spa, "spa_remove_init failed [error=%d]", | |
3968 | error); | |
a1d477c2 | 3969 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3970 | } |
a1d477c2 | 3971 | |
9eb7b46e PZ |
3972 | /* |
3973 | * Retrieve information needed to condense indirect vdev mappings. | |
3974 | */ | |
3975 | error = spa_condense_init(spa); | |
3976 | if (error != 0) { | |
4a0ee12a PZ |
3977 | spa_load_failed(spa, "spa_condense_init failed [error=%d]", |
3978 | error); | |
9eb7b46e PZ |
3979 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3980 | } | |
3981 | ||
3982 | return (0); | |
3983 | } | |
3984 | ||
3985 | static int | |
4a0ee12a | 3986 | spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) |
9eb7b46e PZ |
3987 | { |
3988 | int error = 0; | |
3989 | vdev_t *rvd = spa->spa_root_vdev; | |
3990 | ||
9ae529ec CS |
3991 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
3992 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 3993 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
3994 | |
3995 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
4a0ee12a | 3996 | &spa->spa_feat_for_read_obj, B_TRUE) != 0) { |
9ae529ec CS |
3997 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3998 | } | |
3999 | ||
4000 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
4a0ee12a | 4001 | &spa->spa_feat_for_write_obj, B_TRUE) != 0) { |
9ae529ec CS |
4002 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4003 | } | |
4004 | ||
4005 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
4a0ee12a | 4006 | &spa->spa_feat_desc_obj, B_TRUE) != 0) { |
9ae529ec CS |
4007 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4008 | } | |
4009 | ||
b9b24bb4 CS |
4010 | enabled_feat = fnvlist_alloc(); |
4011 | unsup_feat = fnvlist_alloc(); | |
9ae529ec | 4012 | |
fa86b5db | 4013 | if (!spa_features_check(spa, B_FALSE, |
b9b24bb4 | 4014 | unsup_feat, enabled_feat)) |
9ae529ec CS |
4015 | missing_feat_read = B_TRUE; |
4016 | ||
4a0ee12a PZ |
4017 | if (spa_writeable(spa) || |
4018 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) { | |
fa86b5db | 4019 | if (!spa_features_check(spa, B_TRUE, |
b9b24bb4 | 4020 | unsup_feat, enabled_feat)) { |
9eb7b46e | 4021 | *missing_feat_writep = B_TRUE; |
b9b24bb4 | 4022 | } |
9ae529ec CS |
4023 | } |
4024 | ||
b9b24bb4 CS |
4025 | fnvlist_add_nvlist(spa->spa_load_info, |
4026 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
4027 | ||
9ae529ec | 4028 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
4029 | fnvlist_add_nvlist(spa->spa_load_info, |
4030 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
4031 | } |
4032 | ||
b9b24bb4 CS |
4033 | fnvlist_free(enabled_feat); |
4034 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
4035 | |
4036 | if (!missing_feat_read) { | |
4037 | fnvlist_add_boolean(spa->spa_load_info, | |
4038 | ZPOOL_CONFIG_CAN_RDONLY); | |
4039 | } | |
4040 | ||
4041 | /* | |
4042 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
4043 | * twofold: to determine whether the pool is available for | |
4044 | * import in read-write mode and (if it is not) whether the | |
4045 | * pool is available for import in read-only mode. If the pool | |
4046 | * is available for import in read-write mode, it is displayed | |
4047 | * as available in userland; if it is not available for import | |
4048 | * in read-only mode, it is displayed as unavailable in | |
4049 | * userland. If the pool is available for import in read-only | |
4050 | * mode but not read-write mode, it is displayed as unavailable | |
4051 | * in userland with a special note that the pool is actually | |
4052 | * available for open in read-only mode. | |
4053 | * | |
4054 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
4055 | * missing a feature for write, we must first determine whether | |
4056 | * the pool can be opened read-only before returning to | |
4057 | * userland in order to know whether to display the | |
4058 | * abovementioned note. | |
4059 | */ | |
9eb7b46e | 4060 | if (missing_feat_read || (*missing_feat_writep && |
9ae529ec | 4061 | spa_writeable(spa))) { |
4a0ee12a | 4062 | spa_load_failed(spa, "pool uses unsupported features"); |
9ae529ec CS |
4063 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
4064 | ENOTSUP)); | |
4065 | } | |
b0bc7a84 MG |
4066 | |
4067 | /* | |
4068 | * Load refcounts for ZFS features from disk into an in-memory | |
4069 | * cache during SPA initialization. | |
4070 | */ | |
1c27024e | 4071 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { |
b0bc7a84 MG |
4072 | uint64_t refcount; |
4073 | ||
4074 | error = feature_get_refcount_from_disk(spa, | |
4075 | &spa_feature_table[i], &refcount); | |
4076 | if (error == 0) { | |
4077 | spa->spa_feat_refcount_cache[i] = refcount; | |
4078 | } else if (error == ENOTSUP) { | |
4079 | spa->spa_feat_refcount_cache[i] = | |
4080 | SPA_FEATURE_DISABLED; | |
4081 | } else { | |
4a0ee12a PZ |
4082 | spa_load_failed(spa, "error getting refcount " |
4083 | "for feature %s [error=%d]", | |
4084 | spa_feature_table[i].fi_guid, error); | |
b0bc7a84 MG |
4085 | return (spa_vdev_err(rvd, |
4086 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
4087 | } | |
4088 | } | |
4089 | } | |
4090 | ||
4091 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
4092 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
4a0ee12a | 4093 | &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) |
b0bc7a84 | 4094 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
9ae529ec CS |
4095 | } |
4096 | ||
f00ab3f2 TC |
4097 | /* |
4098 | * Encryption was added before bookmark_v2, even though bookmark_v2 | |
4099 | * is now a dependency. If this pool has encryption enabled without | |
4100 | * bookmark_v2, trigger an errata message. | |
4101 | */ | |
4102 | if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && | |
4103 | !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) { | |
4104 | spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; | |
4105 | } | |
4106 | ||
9eb7b46e PZ |
4107 | return (0); |
4108 | } | |
4109 | ||
4110 | static int | |
4111 | spa_ld_load_special_directories(spa_t *spa) | |
4112 | { | |
4113 | int error = 0; | |
4114 | vdev_t *rvd = spa->spa_root_vdev; | |
4115 | ||
9ae529ec CS |
4116 | spa->spa_is_initializing = B_TRUE; |
4117 | error = dsl_pool_open(spa->spa_dsl_pool); | |
4118 | spa->spa_is_initializing = B_FALSE; | |
4a0ee12a PZ |
4119 | if (error != 0) { |
4120 | spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); | |
9ae529ec | 4121 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4122 | } |
9ae529ec | 4123 | |
9eb7b46e PZ |
4124 | return (0); |
4125 | } | |
428870ff | 4126 | |
9eb7b46e PZ |
4127 | static int |
4128 | spa_ld_get_props(spa_t *spa) | |
4129 | { | |
4130 | int error = 0; | |
4131 | uint64_t obj; | |
4132 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4133 | |
3c67d83a TH |
4134 | /* Grab the checksum salt from the MOS. */ |
4135 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4136 | DMU_POOL_CHECKSUM_SALT, 1, | |
4137 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
4138 | spa->spa_cksum_salt.zcs_bytes); | |
4139 | if (error == ENOENT) { | |
4140 | /* Generate a new salt for subsequent use */ | |
4141 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
4142 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
4143 | } else if (error != 0) { | |
4a0ee12a PZ |
4144 | spa_load_failed(spa, "unable to retrieve checksum salt from " |
4145 | "MOS [error=%d]", error); | |
3c67d83a TH |
4146 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4147 | } | |
4148 | ||
4a0ee12a | 4149 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) |
428870ff BB |
4150 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4151 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
4a0ee12a PZ |
4152 | if (error != 0) { |
4153 | spa_load_failed(spa, "error opening deferred-frees bpobj " | |
4154 | "[error=%d]", error); | |
428870ff | 4155 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4156 | } |
34dc7c2f BB |
4157 | |
4158 | /* | |
4159 | * Load the bit that tells us to use the new accounting function | |
4160 | * (raid-z deflation). If we have an older pool, this will not | |
4161 | * be present. | |
4162 | */ | |
4a0ee12a | 4163 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); |
428870ff BB |
4164 | if (error != 0 && error != ENOENT) |
4165 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4166 | ||
4167 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
4a0ee12a | 4168 | &spa->spa_creation_version, B_FALSE); |
428870ff BB |
4169 | if (error != 0 && error != ENOENT) |
4170 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
4171 | |
4172 | /* | |
4173 | * Load the persistent error log. If we have an older pool, this will | |
4174 | * not be present. | |
4175 | */ | |
4a0ee12a PZ |
4176 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, |
4177 | B_FALSE); | |
428870ff BB |
4178 | if (error != 0 && error != ENOENT) |
4179 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4180 | |
428870ff | 4181 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
4a0ee12a | 4182 | &spa->spa_errlog_scrub, B_FALSE); |
428870ff BB |
4183 | if (error != 0 && error != ENOENT) |
4184 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4185 | |
37f03da8 SH |
4186 | /* |
4187 | * Load the livelist deletion field. If a livelist is queued for | |
4188 | * deletion, indicate that in the spa | |
4189 | */ | |
4190 | error = spa_dir_prop(spa, DMU_POOL_DELETED_CLONES, | |
4191 | &spa->spa_livelists_to_delete, B_FALSE); | |
4192 | if (error != 0 && error != ENOENT) | |
4193 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4194 | ||
34dc7c2f BB |
4195 | /* |
4196 | * Load the history object. If we have an older pool, this | |
4197 | * will not be present. | |
4198 | */ | |
4a0ee12a | 4199 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); |
428870ff BB |
4200 | if (error != 0 && error != ENOENT) |
4201 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4202 | ||
e0ab3ab5 JS |
4203 | /* |
4204 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
4205 | * be present; in this case, defer its creation to a later time to | |
4206 | * avoid dirtying the MOS this early / out of sync context. See | |
4207 | * spa_sync_config_object. | |
4208 | */ | |
4209 | ||
4210 | /* The sentinel is only available in the MOS config. */ | |
1c27024e | 4211 | nvlist_t *mos_config; |
4a0ee12a PZ |
4212 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { |
4213 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
e0ab3ab5 | 4214 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4215 | } |
e0ab3ab5 JS |
4216 | |
4217 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
4a0ee12a | 4218 | &spa->spa_all_vdev_zaps, B_FALSE); |
e0ab3ab5 | 4219 | |
38640550 DB |
4220 | if (error == ENOENT) { |
4221 | VERIFY(!nvlist_exists(mos_config, | |
4222 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
4223 | spa->spa_avz_action = AVZ_ACTION_INITIALIZE; | |
4224 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4225 | } else if (error != 0) { | |
cb01da68 | 4226 | nvlist_free(mos_config); |
e0ab3ab5 | 4227 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
38640550 | 4228 | } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { |
e0ab3ab5 JS |
4229 | /* |
4230 | * An older version of ZFS overwrote the sentinel value, so | |
4231 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
4232 | * destruction to later; see spa_sync_config_object. | |
4233 | */ | |
4234 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
4235 | /* | |
4236 | * We're assuming that no vdevs have had their ZAPs created | |
4237 | * before this. Better be sure of it. | |
4238 | */ | |
4239 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4240 | } | |
4241 | nvlist_free(mos_config); | |
4242 | ||
9eb7b46e PZ |
4243 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
4244 | ||
4a0ee12a PZ |
4245 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, |
4246 | B_FALSE); | |
9eb7b46e PZ |
4247 | if (error && error != ENOENT) |
4248 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4249 | ||
4250 | if (error == 0) { | |
da27b8bc | 4251 | uint64_t autoreplace = 0; |
9eb7b46e PZ |
4252 | |
4253 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
4254 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
4255 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
4256 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
4257 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
c02c1bec | 4258 | spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); |
1b939560 | 4259 | spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim); |
9eb7b46e PZ |
4260 | spa->spa_autoreplace = (autoreplace != 0); |
4261 | } | |
4262 | ||
6cb8e530 PZ |
4263 | /* |
4264 | * If we are importing a pool with missing top-level vdevs, | |
4265 | * we enforce that the pool doesn't panic or get suspended on | |
4266 | * error since the likelihood of missing data is extremely high. | |
4267 | */ | |
4268 | if (spa->spa_missing_tvds > 0 && | |
4269 | spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && | |
4270 | spa->spa_load_state != SPA_LOAD_TRYIMPORT) { | |
4271 | spa_load_note(spa, "forcing failmode to 'continue' " | |
4272 | "as some top level vdevs are missing"); | |
4273 | spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; | |
4274 | } | |
4275 | ||
9eb7b46e PZ |
4276 | return (0); |
4277 | } | |
4278 | ||
4279 | static int | |
4280 | spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) | |
4281 | { | |
4282 | int error = 0; | |
4283 | vdev_t *rvd = spa->spa_root_vdev; | |
4284 | ||
428870ff BB |
4285 | /* |
4286 | * If we're assembling the pool from the split-off vdevs of | |
4287 | * an existing pool, we don't want to attach the spares & cache | |
4288 | * devices. | |
4289 | */ | |
34dc7c2f BB |
4290 | |
4291 | /* | |
4292 | * Load any hot spares for this pool. | |
4293 | */ | |
4a0ee12a PZ |
4294 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, |
4295 | B_FALSE); | |
428870ff BB |
4296 | if (error != 0 && error != ENOENT) |
4297 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4298 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4299 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
4300 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
4a0ee12a PZ |
4301 | &spa->spa_spares.sav_config) != 0) { |
4302 | spa_load_failed(spa, "error loading spares nvlist"); | |
428870ff | 4303 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4304 | } |
34dc7c2f | 4305 | |
b128c09f | 4306 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4307 | spa_load_spares(spa); |
b128c09f | 4308 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4309 | } else if (error == 0) { |
4310 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
4311 | } |
4312 | ||
4313 | /* | |
4314 | * Load any level 2 ARC devices for this pool. | |
4315 | */ | |
428870ff | 4316 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
4a0ee12a | 4317 | &spa->spa_l2cache.sav_object, B_FALSE); |
428870ff BB |
4318 | if (error != 0 && error != ENOENT) |
4319 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4320 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4321 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
4322 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
4a0ee12a PZ |
4323 | &spa->spa_l2cache.sav_config) != 0) { |
4324 | spa_load_failed(spa, "error loading l2cache nvlist"); | |
428870ff | 4325 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4326 | } |
34dc7c2f | 4327 | |
b128c09f | 4328 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4329 | spa_load_l2cache(spa); |
b128c09f | 4330 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4331 | } else if (error == 0) { |
4332 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
4333 | } |
4334 | ||
9eb7b46e PZ |
4335 | return (0); |
4336 | } | |
428870ff | 4337 | |
9eb7b46e | 4338 | static int |
4a0ee12a | 4339 | spa_ld_load_vdev_metadata(spa_t *spa) |
9eb7b46e PZ |
4340 | { |
4341 | int error = 0; | |
4342 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4343 | |
379ca9cf OF |
4344 | /* |
4345 | * If the 'multihost' property is set, then never allow a pool to | |
4346 | * be imported when the system hostid is zero. The exception to | |
4347 | * this rule is zdb which is always allowed to access pools. | |
4348 | */ | |
25f06d67 | 4349 | if (spa_multihost(spa) && spa_get_hostid(spa) == 0 && |
379ca9cf OF |
4350 | (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { |
4351 | fnvlist_add_uint64(spa->spa_load_info, | |
4352 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
4353 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
4354 | } | |
4355 | ||
34dc7c2f BB |
4356 | /* |
4357 | * If the 'autoreplace' property is set, then post a resource notifying | |
4358 | * the ZFS DE that it should not issue any faults for unopenable | |
4359 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
4360 | * unopenable vdevs so that the normal autoreplace handler can take | |
4361 | * over. | |
4362 | */ | |
4a0ee12a | 4363 | if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 4364 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
4365 | /* |
4366 | * For the import case, this is done in spa_import(), because | |
4367 | * at this point we're using the spare definitions from | |
4368 | * the MOS config, not necessarily from the userland config. | |
4369 | */ | |
4a0ee12a | 4370 | if (spa->spa_load_state != SPA_LOAD_IMPORT) { |
428870ff BB |
4371 | spa_aux_check_removed(&spa->spa_spares); |
4372 | spa_aux_check_removed(&spa->spa_l2cache); | |
4373 | } | |
4374 | } | |
34dc7c2f BB |
4375 | |
4376 | /* | |
9eb7b46e | 4377 | * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. |
34dc7c2f | 4378 | */ |
a1d477c2 MA |
4379 | error = vdev_load(rvd); |
4380 | if (error != 0) { | |
4a0ee12a | 4381 | spa_load_failed(spa, "vdev_load failed [error=%d]", error); |
a1d477c2 MA |
4382 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
4383 | } | |
4384 | ||
93e28d66 SD |
4385 | error = spa_ld_log_spacemaps(spa); |
4386 | if (error != 0) { | |
600a02b8 | 4387 | spa_load_failed(spa, "spa_ld_log_spacemaps failed [error=%d]", |
93e28d66 SD |
4388 | error); |
4389 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
4390 | } | |
4391 | ||
34dc7c2f | 4392 | /* |
9eb7b46e | 4393 | * Propagate the leaf DTLs we just loaded all the way up the vdev tree. |
34dc7c2f | 4394 | */ |
b128c09f | 4395 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9a49d3f3 | 4396 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE, B_FALSE); |
b128c09f | 4397 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 4398 | |
9eb7b46e PZ |
4399 | return (0); |
4400 | } | |
4401 | ||
4402 | static int | |
4403 | spa_ld_load_dedup_tables(spa_t *spa) | |
4404 | { | |
4405 | int error = 0; | |
4406 | vdev_t *rvd = spa->spa_root_vdev; | |
4407 | ||
428870ff | 4408 | error = ddt_load(spa); |
4a0ee12a PZ |
4409 | if (error != 0) { |
4410 | spa_load_failed(spa, "ddt_load failed [error=%d]", error); | |
428870ff | 4411 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4412 | } |
428870ff | 4413 | |
9eb7b46e PZ |
4414 | return (0); |
4415 | } | |
4416 | ||
4417 | static int | |
a926aab9 | 4418 | spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, const char **ereport) |
9eb7b46e PZ |
4419 | { |
4420 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 4421 | |
4a0ee12a PZ |
4422 | if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { |
4423 | boolean_t missing = spa_check_logs(spa); | |
4424 | if (missing) { | |
6cb8e530 PZ |
4425 | if (spa->spa_missing_tvds != 0) { |
4426 | spa_load_note(spa, "spa_check_logs failed " | |
4427 | "so dropping the logs"); | |
4428 | } else { | |
4429 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
4430 | spa_load_failed(spa, "spa_check_logs failed"); | |
4431 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, | |
4432 | ENXIO)); | |
4433 | } | |
4a0ee12a | 4434 | } |
428870ff BB |
4435 | } |
4436 | ||
9eb7b46e PZ |
4437 | return (0); |
4438 | } | |
4439 | ||
4440 | static int | |
4a0ee12a | 4441 | spa_ld_verify_pool_data(spa_t *spa) |
9eb7b46e PZ |
4442 | { |
4443 | int error = 0; | |
4444 | vdev_t *rvd = spa->spa_root_vdev; | |
4445 | ||
4446 | /* | |
4447 | * We've successfully opened the pool, verify that we're ready | |
4448 | * to start pushing transactions. | |
4449 | */ | |
4a0ee12a | 4450 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
9eb7b46e PZ |
4451 | error = spa_load_verify(spa); |
4452 | if (error != 0) { | |
4a0ee12a PZ |
4453 | spa_load_failed(spa, "spa_load_verify failed " |
4454 | "[error=%d]", error); | |
9eb7b46e PZ |
4455 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
4456 | error)); | |
4457 | } | |
4458 | } | |
4459 | ||
4460 | return (0); | |
4461 | } | |
4462 | ||
4463 | static void | |
4464 | spa_ld_claim_log_blocks(spa_t *spa) | |
4465 | { | |
4466 | dmu_tx_t *tx; | |
4467 | dsl_pool_t *dp = spa_get_dsl(spa); | |
4468 | ||
4469 | /* | |
4470 | * Claim log blocks that haven't been committed yet. | |
4471 | * This must all happen in a single txg. | |
4472 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
4473 | * invoked from zil_claim_log_block()'s i/o done callback. | |
4474 | * Price of rollback is that we abandon the log. | |
4475 | */ | |
4476 | spa->spa_claiming = B_TRUE; | |
4477 | ||
4478 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
4479 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
4480 | zil_claim, tx, DS_FIND_CHILDREN); | |
4481 | dmu_tx_commit(tx); | |
4482 | ||
4483 | spa->spa_claiming = B_FALSE; | |
4484 | ||
4485 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
4486 | } | |
4487 | ||
4488 | static void | |
6cb8e530 | 4489 | spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, |
d2734cce | 4490 | boolean_t update_config_cache) |
9eb7b46e PZ |
4491 | { |
4492 | vdev_t *rvd = spa->spa_root_vdev; | |
4493 | int need_update = B_FALSE; | |
4494 | ||
4495 | /* | |
4496 | * If the config cache is stale, or we have uninitialized | |
4497 | * metaslabs (see spa_vdev_add()), then update the config. | |
4498 | * | |
4499 | * If this is a verbatim import, trust the current | |
4500 | * in-core spa_config and update the disk labels. | |
4501 | */ | |
d2734cce | 4502 | if (update_config_cache || config_cache_txg != spa->spa_config_txg || |
4a0ee12a PZ |
4503 | spa->spa_load_state == SPA_LOAD_IMPORT || |
4504 | spa->spa_load_state == SPA_LOAD_RECOVER || | |
9eb7b46e PZ |
4505 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) |
4506 | need_update = B_TRUE; | |
4507 | ||
4508 | for (int c = 0; c < rvd->vdev_children; c++) | |
4509 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
4510 | need_update = B_TRUE; | |
4511 | ||
4512 | /* | |
e1cfd73f | 4513 | * Update the config cache asynchronously in case we're the |
9eb7b46e PZ |
4514 | * root pool, in which case the config cache isn't writable yet. |
4515 | */ | |
4516 | if (need_update) | |
4517 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
4518 | } | |
4519 | ||
6cb8e530 PZ |
4520 | static void |
4521 | spa_ld_prepare_for_reload(spa_t *spa) | |
4522 | { | |
da92d5cb | 4523 | spa_mode_t mode = spa->spa_mode; |
6cb8e530 PZ |
4524 | int async_suspended = spa->spa_async_suspended; |
4525 | ||
4526 | spa_unload(spa); | |
4527 | spa_deactivate(spa); | |
4528 | spa_activate(spa, mode); | |
4529 | ||
4530 | /* | |
4531 | * We save the value of spa_async_suspended as it gets reset to 0 by | |
4532 | * spa_unload(). We want to restore it back to the original value before | |
4533 | * returning as we might be calling spa_async_resume() later. | |
4534 | */ | |
4535 | spa->spa_async_suspended = async_suspended; | |
4536 | } | |
4537 | ||
9eb7b46e | 4538 | static int |
d2734cce SD |
4539 | spa_ld_read_checkpoint_txg(spa_t *spa) |
4540 | { | |
4541 | uberblock_t checkpoint; | |
4542 | int error = 0; | |
4543 | ||
4544 | ASSERT0(spa->spa_checkpoint_txg); | |
4545 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4546 | ||
4547 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4548 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4549 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4550 | ||
4551 | if (error == ENOENT) | |
4552 | return (0); | |
4553 | ||
4554 | if (error != 0) | |
4555 | return (error); | |
4556 | ||
4557 | ASSERT3U(checkpoint.ub_txg, !=, 0); | |
4558 | ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); | |
4559 | ASSERT3U(checkpoint.ub_timestamp, !=, 0); | |
4560 | spa->spa_checkpoint_txg = checkpoint.ub_txg; | |
4561 | spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; | |
4562 | ||
4563 | return (0); | |
4564 | } | |
4565 | ||
4566 | static int | |
4567 | spa_ld_mos_init(spa_t *spa, spa_import_type_t type) | |
9eb7b46e PZ |
4568 | { |
4569 | int error = 0; | |
9eb7b46e | 4570 | |
4a0ee12a | 4571 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6cb8e530 | 4572 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); |
4a0ee12a | 4573 | |
9eb7b46e | 4574 | /* |
6cb8e530 PZ |
4575 | * Never trust the config that is provided unless we are assembling |
4576 | * a pool following a split. | |
4577 | * This means don't trust blkptrs and the vdev tree in general. This | |
4578 | * also effectively puts the spa in read-only mode since | |
4579 | * spa_writeable() checks for spa_trust_config to be true. | |
4580 | * We will later load a trusted config from the MOS. | |
9eb7b46e | 4581 | */ |
6cb8e530 PZ |
4582 | if (type != SPA_IMPORT_ASSEMBLE) |
4583 | spa->spa_trust_config = B_FALSE; | |
4584 | ||
9eb7b46e PZ |
4585 | /* |
4586 | * Parse the config provided to create a vdev tree. | |
4587 | */ | |
6cb8e530 | 4588 | error = spa_ld_parse_config(spa, type); |
9eb7b46e PZ |
4589 | if (error != 0) |
4590 | return (error); | |
4591 | ||
ca95f70d OF |
4592 | spa_import_progress_add(spa); |
4593 | ||
9eb7b46e PZ |
4594 | /* |
4595 | * Now that we have the vdev tree, try to open each vdev. This involves | |
4596 | * opening the underlying physical device, retrieving its geometry and | |
4597 | * probing the vdev with a dummy I/O. The state of each vdev will be set | |
4598 | * based on the success of those operations. After this we'll be ready | |
4599 | * to read from the vdevs. | |
4600 | */ | |
4601 | error = spa_ld_open_vdevs(spa); | |
4602 | if (error != 0) | |
4603 | return (error); | |
4604 | ||
4605 | /* | |
4606 | * Read the label of each vdev and make sure that the GUIDs stored | |
4607 | * there match the GUIDs in the config provided. | |
6cb8e530 PZ |
4608 | * If we're assembling a new pool that's been split off from an |
4609 | * existing pool, the labels haven't yet been updated so we skip | |
4610 | * validation for now. | |
9eb7b46e | 4611 | */ |
6cb8e530 PZ |
4612 | if (type != SPA_IMPORT_ASSEMBLE) { |
4613 | error = spa_ld_validate_vdevs(spa); | |
4614 | if (error != 0) | |
4615 | return (error); | |
4616 | } | |
9eb7b46e PZ |
4617 | |
4618 | /* | |
d2734cce SD |
4619 | * Read all vdev labels to find the best uberblock (i.e. latest, |
4620 | * unless spa_load_max_txg is set) and store it in spa_uberblock. We | |
4621 | * get the list of features required to read blkptrs in the MOS from | |
4622 | * the vdev label with the best uberblock and verify that our version | |
4623 | * of zfs supports them all. | |
9eb7b46e | 4624 | */ |
6cb8e530 | 4625 | error = spa_ld_select_uberblock(spa, type); |
9eb7b46e PZ |
4626 | if (error != 0) |
4627 | return (error); | |
4628 | ||
4629 | /* | |
4630 | * Pass that uberblock to the dsl_pool layer which will open the root | |
4631 | * blkptr. This blkptr points to the latest version of the MOS and will | |
4632 | * allow us to read its contents. | |
4633 | */ | |
4634 | error = spa_ld_open_rootbp(spa); | |
4635 | if (error != 0) | |
4636 | return (error); | |
4637 | ||
d2734cce SD |
4638 | return (0); |
4639 | } | |
4640 | ||
4641 | static int | |
4642 | spa_ld_checkpoint_rewind(spa_t *spa) | |
4643 | { | |
4644 | uberblock_t checkpoint; | |
4645 | int error = 0; | |
4646 | ||
4647 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4648 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4649 | ||
4650 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4651 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4652 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4653 | ||
4654 | if (error != 0) { | |
4655 | spa_load_failed(spa, "unable to retrieve checkpointed " | |
4656 | "uberblock from the MOS config [error=%d]", error); | |
4657 | ||
4658 | if (error == ENOENT) | |
4659 | error = ZFS_ERR_NO_CHECKPOINT; | |
4660 | ||
4661 | return (error); | |
4662 | } | |
4663 | ||
4664 | ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); | |
4665 | ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); | |
4666 | ||
4667 | /* | |
4668 | * We need to update the txg and timestamp of the checkpointed | |
4669 | * uberblock to be higher than the latest one. This ensures that | |
4670 | * the checkpointed uberblock is selected if we were to close and | |
4671 | * reopen the pool right after we've written it in the vdev labels. | |
4672 | * (also see block comment in vdev_uberblock_compare) | |
4673 | */ | |
4674 | checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; | |
4675 | checkpoint.ub_timestamp = gethrestime_sec(); | |
4676 | ||
4677 | /* | |
4678 | * Set current uberblock to be the checkpointed uberblock. | |
4679 | */ | |
4680 | spa->spa_uberblock = checkpoint; | |
4681 | ||
4682 | /* | |
4683 | * If we are doing a normal rewind, then the pool is open for | |
4684 | * writing and we sync the "updated" checkpointed uberblock to | |
4685 | * disk. Once this is done, we've basically rewound the whole | |
4686 | * pool and there is no way back. | |
4687 | * | |
4688 | * There are cases when we don't want to attempt and sync the | |
4689 | * checkpointed uberblock to disk because we are opening a | |
4690 | * pool as read-only. Specifically, verifying the checkpointed | |
4691 | * state with zdb, and importing the checkpointed state to get | |
4692 | * a "preview" of its content. | |
4693 | */ | |
4694 | if (spa_writeable(spa)) { | |
4695 | vdev_t *rvd = spa->spa_root_vdev; | |
4696 | ||
4697 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4698 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; | |
4699 | int svdcount = 0; | |
4700 | int children = rvd->vdev_children; | |
29274c9f | 4701 | int c0 = random_in_range(children); |
d2734cce SD |
4702 | |
4703 | for (int c = 0; c < children; c++) { | |
4704 | vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; | |
4705 | ||
4706 | /* Stop when revisiting the first vdev */ | |
4707 | if (c > 0 && svd[0] == vd) | |
4708 | break; | |
4709 | ||
4710 | if (vd->vdev_ms_array == 0 || vd->vdev_islog || | |
4711 | !vdev_is_concrete(vd)) | |
4712 | continue; | |
4713 | ||
4714 | svd[svdcount++] = vd; | |
4715 | if (svdcount == SPA_SYNC_MIN_VDEVS) | |
4716 | break; | |
4717 | } | |
4718 | error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); | |
4719 | if (error == 0) | |
4720 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
4721 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4722 | ||
4723 | if (error != 0) { | |
4724 | spa_load_failed(spa, "failed to write checkpointed " | |
4725 | "uberblock to the vdev labels [error=%d]", error); | |
4726 | return (error); | |
4727 | } | |
4728 | } | |
4729 | ||
4730 | return (0); | |
4731 | } | |
4732 | ||
4733 | static int | |
4734 | spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, | |
4735 | boolean_t *update_config_cache) | |
4736 | { | |
4737 | int error; | |
4738 | ||
4739 | /* | |
4740 | * Parse the config for pool, open and validate vdevs, | |
4741 | * select an uberblock, and use that uberblock to open | |
4742 | * the MOS. | |
4743 | */ | |
4744 | error = spa_ld_mos_init(spa, type); | |
4745 | if (error != 0) | |
4746 | return (error); | |
4747 | ||
9eb7b46e | 4748 | /* |
6cb8e530 PZ |
4749 | * Retrieve the trusted config stored in the MOS and use it to create |
4750 | * a new, exact version of the vdev tree, then reopen all vdevs. | |
9eb7b46e | 4751 | */ |
d2734cce | 4752 | error = spa_ld_trusted_config(spa, type, B_FALSE); |
6cb8e530 | 4753 | if (error == EAGAIN) { |
d2734cce SD |
4754 | if (update_config_cache != NULL) |
4755 | *update_config_cache = B_TRUE; | |
4756 | ||
6cb8e530 PZ |
4757 | /* |
4758 | * Redo the loading process with the trusted config if it is | |
4759 | * too different from the untrusted config. | |
4760 | */ | |
4761 | spa_ld_prepare_for_reload(spa); | |
d2734cce SD |
4762 | spa_load_note(spa, "RELOADING"); |
4763 | error = spa_ld_mos_init(spa, type); | |
4764 | if (error != 0) | |
4765 | return (error); | |
4766 | ||
4767 | error = spa_ld_trusted_config(spa, type, B_TRUE); | |
4768 | if (error != 0) | |
4769 | return (error); | |
4770 | ||
6cb8e530 | 4771 | } else if (error != 0) { |
9eb7b46e | 4772 | return (error); |
6cb8e530 | 4773 | } |
9eb7b46e | 4774 | |
d2734cce SD |
4775 | return (0); |
4776 | } | |
4777 | ||
4778 | /* | |
4779 | * Load an existing storage pool, using the config provided. This config | |
4780 | * describes which vdevs are part of the pool and is later validated against | |
4781 | * partial configs present in each vdev's label and an entire copy of the | |
4782 | * config stored in the MOS. | |
4783 | */ | |
4784 | static int | |
a926aab9 | 4785 | spa_load_impl(spa_t *spa, spa_import_type_t type, const char **ereport) |
d2734cce SD |
4786 | { |
4787 | int error = 0; | |
4788 | boolean_t missing_feat_write = B_FALSE; | |
4789 | boolean_t checkpoint_rewind = | |
4790 | (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4791 | boolean_t update_config_cache = B_FALSE; | |
4792 | ||
4793 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4794 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); | |
4795 | ||
4796 | spa_load_note(spa, "LOADING"); | |
4797 | ||
4798 | error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); | |
4799 | if (error != 0) | |
4800 | return (error); | |
4801 | ||
4802 | /* | |
4803 | * If we are rewinding to the checkpoint then we need to repeat | |
4804 | * everything we've done so far in this function but this time | |
4805 | * selecting the checkpointed uberblock and using that to open | |
4806 | * the MOS. | |
4807 | */ | |
4808 | if (checkpoint_rewind) { | |
4809 | /* | |
4810 | * If we are rewinding to the checkpoint update config cache | |
4811 | * anyway. | |
4812 | */ | |
4813 | update_config_cache = B_TRUE; | |
4814 | ||
4815 | /* | |
4816 | * Extract the checkpointed uberblock from the current MOS | |
4817 | * and use this as the pool's uberblock from now on. If the | |
4818 | * pool is imported as writeable we also write the checkpoint | |
4819 | * uberblock to the labels, making the rewind permanent. | |
4820 | */ | |
4821 | error = spa_ld_checkpoint_rewind(spa); | |
4822 | if (error != 0) | |
4823 | return (error); | |
4824 | ||
4825 | /* | |
e1cfd73f | 4826 | * Redo the loading process again with the |
d2734cce SD |
4827 | * checkpointed uberblock. |
4828 | */ | |
4829 | spa_ld_prepare_for_reload(spa); | |
4830 | spa_load_note(spa, "LOADING checkpointed uberblock"); | |
4831 | error = spa_ld_mos_with_trusted_config(spa, type, NULL); | |
4832 | if (error != 0) | |
4833 | return (error); | |
4834 | } | |
4835 | ||
4836 | /* | |
4837 | * Retrieve the checkpoint txg if the pool has a checkpoint. | |
4838 | */ | |
4839 | error = spa_ld_read_checkpoint_txg(spa); | |
4840 | if (error != 0) | |
4841 | return (error); | |
4842 | ||
9eb7b46e PZ |
4843 | /* |
4844 | * Retrieve the mapping of indirect vdevs. Those vdevs were removed | |
4845 | * from the pool and their contents were re-mapped to other vdevs. Note | |
4846 | * that everything that we read before this step must have been | |
4847 | * rewritten on concrete vdevs after the last device removal was | |
4848 | * initiated. Otherwise we could be reading from indirect vdevs before | |
4849 | * we have loaded their mappings. | |
4850 | */ | |
4851 | error = spa_ld_open_indirect_vdev_metadata(spa); | |
4852 | if (error != 0) | |
4853 | return (error); | |
4854 | ||
4855 | /* | |
4856 | * Retrieve the full list of active features from the MOS and check if | |
4857 | * they are all supported. | |
4858 | */ | |
4a0ee12a | 4859 | error = spa_ld_check_features(spa, &missing_feat_write); |
9eb7b46e PZ |
4860 | if (error != 0) |
4861 | return (error); | |
4862 | ||
4863 | /* | |
4864 | * Load several special directories from the MOS needed by the dsl_pool | |
4865 | * layer. | |
4866 | */ | |
4867 | error = spa_ld_load_special_directories(spa); | |
4868 | if (error != 0) | |
4869 | return (error); | |
4870 | ||
9eb7b46e PZ |
4871 | /* |
4872 | * Retrieve pool properties from the MOS. | |
4873 | */ | |
4874 | error = spa_ld_get_props(spa); | |
4875 | if (error != 0) | |
4876 | return (error); | |
4877 | ||
4878 | /* | |
4879 | * Retrieve the list of auxiliary devices - cache devices and spares - | |
4880 | * and open them. | |
4881 | */ | |
4882 | error = spa_ld_open_aux_vdevs(spa, type); | |
4883 | if (error != 0) | |
4884 | return (error); | |
4885 | ||
4886 | /* | |
4887 | * Load the metadata for all vdevs. Also check if unopenable devices | |
4888 | * should be autoreplaced. | |
4889 | */ | |
4a0ee12a | 4890 | error = spa_ld_load_vdev_metadata(spa); |
9eb7b46e PZ |
4891 | if (error != 0) |
4892 | return (error); | |
4893 | ||
4894 | error = spa_ld_load_dedup_tables(spa); | |
4895 | if (error != 0) | |
4896 | return (error); | |
4897 | ||
4898 | /* | |
4899 | * Verify the logs now to make sure we don't have any unexpected errors | |
4900 | * when we claim log blocks later. | |
4901 | */ | |
4902 | error = spa_ld_verify_logs(spa, type, ereport); | |
4903 | if (error != 0) | |
4904 | return (error); | |
4905 | ||
9ae529ec | 4906 | if (missing_feat_write) { |
6cb8e530 | 4907 | ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); |
9ae529ec CS |
4908 | |
4909 | /* | |
4910 | * At this point, we know that we can open the pool in | |
4911 | * read-only mode but not read-write mode. We now have enough | |
4912 | * information and can return to userland. | |
4913 | */ | |
9eb7b46e PZ |
4914 | return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, |
4915 | ENOTSUP)); | |
9ae529ec CS |
4916 | } |
4917 | ||
572e2857 | 4918 | /* |
9eb7b46e PZ |
4919 | * Traverse the last txgs to make sure the pool was left off in a safe |
4920 | * state. When performing an extreme rewind, we verify the whole pool, | |
4921 | * which can take a very long time. | |
572e2857 | 4922 | */ |
4a0ee12a | 4923 | error = spa_ld_verify_pool_data(spa); |
9eb7b46e PZ |
4924 | if (error != 0) |
4925 | return (error); | |
572e2857 | 4926 | |
9eb7b46e PZ |
4927 | /* |
4928 | * Calculate the deflated space for the pool. This must be done before | |
4929 | * we write anything to the pool because we'd need to update the space | |
4930 | * accounting using the deflated sizes. | |
4931 | */ | |
4932 | spa_update_dspace(spa); | |
4933 | ||
4934 | /* | |
4935 | * We have now retrieved all the information we needed to open the | |
4936 | * pool. If we are importing the pool in read-write mode, a few | |
4937 | * additional steps must be performed to finish the import. | |
4938 | */ | |
6cb8e530 | 4939 | if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || |
428870ff | 4940 | spa->spa_load_max_txg == UINT64_MAX)) { |
6cb8e530 PZ |
4941 | uint64_t config_cache_txg = spa->spa_config_txg; |
4942 | ||
4943 | ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f | 4944 | |
d2734cce SD |
4945 | /* |
4946 | * In case of a checkpoint rewind, log the original txg | |
4947 | * of the checkpointed uberblock. | |
4948 | */ | |
4949 | if (checkpoint_rewind) { | |
4950 | spa_history_log_internal(spa, "checkpoint rewind", | |
4951 | NULL, "rewound state to txg=%llu", | |
4952 | (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); | |
4953 | } | |
4954 | ||
34dc7c2f | 4955 | /* |
9eb7b46e | 4956 | * Traverse the ZIL and claim all blocks. |
34dc7c2f | 4957 | */ |
9eb7b46e | 4958 | spa_ld_claim_log_blocks(spa); |
428870ff | 4959 | |
9eb7b46e PZ |
4960 | /* |
4961 | * Kick-off the syncing thread. | |
4962 | */ | |
34dc7c2f BB |
4963 | spa->spa_sync_on = B_TRUE; |
4964 | txg_sync_start(spa->spa_dsl_pool); | |
379ca9cf | 4965 | mmp_thread_start(spa); |
34dc7c2f BB |
4966 | |
4967 | /* | |
428870ff BB |
4968 | * Wait for all claims to sync. We sync up to the highest |
4969 | * claimed log block birth time so that claimed log blocks | |
4970 | * don't appear to be from the future. spa_claim_max_txg | |
9eb7b46e PZ |
4971 | * will have been set for us by ZIL traversal operations |
4972 | * performed above. | |
34dc7c2f | 4973 | */ |
428870ff | 4974 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
4975 | |
4976 | /* | |
9eb7b46e PZ |
4977 | * Check if we need to request an update of the config. On the |
4978 | * next sync, we would update the config stored in vdev labels | |
4979 | * and the cachefile (by default /etc/zfs/zpool.cache). | |
34dc7c2f | 4980 | */ |
6cb8e530 | 4981 | spa_ld_check_for_config_update(spa, config_cache_txg, |
d2734cce | 4982 | update_config_cache); |
fb5f0bc8 BB |
4983 | |
4984 | /* | |
9a49d3f3 BB |
4985 | * Check if a rebuild was in progress and if so resume it. |
4986 | * Then check all DTLs to see if anything needs resilvering. | |
4987 | * The resilver will be deferred if a rebuild was started. | |
fb5f0bc8 | 4988 | */ |
9a49d3f3 BB |
4989 | if (vdev_rebuild_active(spa->spa_root_vdev)) { |
4990 | vdev_rebuild_restart(spa); | |
4991 | } else if (!dsl_scan_resilvering(spa->spa_dsl_pool) && | |
4992 | vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { | |
fb5f0bc8 | 4993 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
9a49d3f3 | 4994 | } |
428870ff | 4995 | |
6f1ffb06 MA |
4996 | /* |
4997 | * Log the fact that we booted up (so that we can detect if | |
4998 | * we rebooted in the middle of an operation). | |
4999 | */ | |
d5e024cb | 5000 | spa_history_log_version(spa, "open", NULL); |
6f1ffb06 | 5001 | |
9b2266e3 SD |
5002 | spa_restart_removal(spa); |
5003 | spa_spawn_aux_threads(spa); | |
5004 | ||
428870ff BB |
5005 | /* |
5006 | * Delete any inconsistent datasets. | |
9b2266e3 SD |
5007 | * |
5008 | * Note: | |
5009 | * Since we may be issuing deletes for clones here, | |
5010 | * we make sure to do so after we've spawned all the | |
5011 | * auxiliary threads above (from which the livelist | |
5012 | * deletion zthr is part of). | |
428870ff BB |
5013 | */ |
5014 | (void) dmu_objset_find(spa_name(spa), | |
5015 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
5016 | ||
5017 | /* | |
5018 | * Clean up any stale temporary dataset userrefs. | |
5019 | */ | |
5020 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
a1d477c2 | 5021 | |
619f0976 GW |
5022 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
5023 | vdev_initialize_restart(spa->spa_root_vdev); | |
1b939560 BB |
5024 | vdev_trim_restart(spa->spa_root_vdev); |
5025 | vdev_autotrim_restart(spa); | |
619f0976 | 5026 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
5027 | } |
5028 | ||
ca95f70d | 5029 | spa_import_progress_remove(spa_guid(spa)); |
77f6826b GA |
5030 | spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD); |
5031 | ||
4a0ee12a PZ |
5032 | spa_load_note(spa, "LOADED"); |
5033 | ||
428870ff BB |
5034 | return (0); |
5035 | } | |
34dc7c2f | 5036 | |
428870ff | 5037 | static int |
6cb8e530 | 5038 | spa_load_retry(spa_t *spa, spa_load_state_t state) |
428870ff | 5039 | { |
da92d5cb | 5040 | spa_mode_t mode = spa->spa_mode; |
572e2857 | 5041 | |
428870ff BB |
5042 | spa_unload(spa); |
5043 | spa_deactivate(spa); | |
5044 | ||
dea377c0 | 5045 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; |
428870ff | 5046 | |
572e2857 | 5047 | spa_activate(spa, mode); |
428870ff BB |
5048 | spa_async_suspend(spa); |
5049 | ||
4a0ee12a PZ |
5050 | spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", |
5051 | (u_longlong_t)spa->spa_load_max_txg); | |
5052 | ||
6cb8e530 | 5053 | return (spa_load(spa, state, SPA_IMPORT_EXISTING)); |
428870ff BB |
5054 | } |
5055 | ||
9ae529ec CS |
5056 | /* |
5057 | * If spa_load() fails this function will try loading prior txg's. If | |
5058 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
5059 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
5060 | * function will not rewind the pool and will return the same error as | |
5061 | * spa_load(). | |
5062 | */ | |
428870ff | 5063 | static int |
6cb8e530 PZ |
5064 | spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, |
5065 | int rewind_flags) | |
428870ff | 5066 | { |
9ae529ec | 5067 | nvlist_t *loadinfo = NULL; |
428870ff BB |
5068 | nvlist_t *config = NULL; |
5069 | int load_error, rewind_error; | |
5070 | uint64_t safe_rewind_txg; | |
5071 | uint64_t min_txg; | |
5072 | ||
5073 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
5074 | spa->spa_load_max_txg = spa->spa_load_txg; | |
5075 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
5076 | } else { | |
5077 | spa->spa_load_max_txg = max_request; | |
dea377c0 MA |
5078 | if (max_request != UINT64_MAX) |
5079 | spa->spa_extreme_rewind = B_TRUE; | |
428870ff BB |
5080 | } |
5081 | ||
6cb8e530 | 5082 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); |
428870ff BB |
5083 | if (load_error == 0) |
5084 | return (0); | |
d2734cce SD |
5085 | if (load_error == ZFS_ERR_NO_CHECKPOINT) { |
5086 | /* | |
5087 | * When attempting checkpoint-rewind on a pool with no | |
5088 | * checkpoint, we should not attempt to load uberblocks | |
5089 | * from previous txgs when spa_load fails. | |
5090 | */ | |
5091 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
ca95f70d | 5092 | spa_import_progress_remove(spa_guid(spa)); |
d2734cce SD |
5093 | return (load_error); |
5094 | } | |
428870ff BB |
5095 | |
5096 | if (spa->spa_root_vdev != NULL) | |
5097 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
5098 | ||
5099 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
5100 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
5101 | ||
5102 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
5103 | nvlist_free(config); | |
ca95f70d | 5104 | spa_import_progress_remove(spa_guid(spa)); |
428870ff BB |
5105 | return (load_error); |
5106 | } | |
5107 | ||
9ae529ec CS |
5108 | if (state == SPA_LOAD_RECOVER) { |
5109 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 5110 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
5111 | } else { |
5112 | /* | |
5113 | * If we aren't rolling back save the load info from our first | |
5114 | * import attempt so that we can restore it after attempting | |
5115 | * to rewind. | |
5116 | */ | |
5117 | loadinfo = spa->spa_load_info; | |
5118 | spa->spa_load_info = fnvlist_alloc(); | |
5119 | } | |
428870ff BB |
5120 | |
5121 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
5122 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
5123 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
5124 | TXG_INITIAL : safe_rewind_txg; | |
5125 | ||
5126 | /* | |
5127 | * Continue as long as we're finding errors, we're still within | |
5128 | * the acceptable rewind range, and we're still finding uberblocks | |
5129 | */ | |
5130 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
5131 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
5132 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
5133 | spa->spa_extreme_rewind = B_TRUE; | |
6cb8e530 | 5134 | rewind_error = spa_load_retry(spa, state); |
428870ff BB |
5135 | } |
5136 | ||
428870ff BB |
5137 | spa->spa_extreme_rewind = B_FALSE; |
5138 | spa->spa_load_max_txg = UINT64_MAX; | |
5139 | ||
5140 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
5141 | spa_config_set(spa, config); | |
ee6370a7 | 5142 | else |
5143 | nvlist_free(config); | |
428870ff | 5144 | |
9ae529ec CS |
5145 | if (state == SPA_LOAD_RECOVER) { |
5146 | ASSERT3P(loadinfo, ==, NULL); | |
ca95f70d | 5147 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
5148 | return (rewind_error); |
5149 | } else { | |
5150 | /* Store the rewind info as part of the initial load info */ | |
5151 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
5152 | spa->spa_load_info); | |
5153 | ||
5154 | /* Restore the initial load info */ | |
5155 | fnvlist_free(spa->spa_load_info); | |
5156 | spa->spa_load_info = loadinfo; | |
5157 | ||
ca95f70d | 5158 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
5159 | return (load_error); |
5160 | } | |
34dc7c2f BB |
5161 | } |
5162 | ||
5163 | /* | |
5164 | * Pool Open/Import | |
5165 | * | |
5166 | * The import case is identical to an open except that the configuration is sent | |
5167 | * down from userland, instead of grabbed from the configuration cache. For the | |
5168 | * case of an open, the pool configuration will exist in the | |
5169 | * POOL_STATE_UNINITIALIZED state. | |
5170 | * | |
5171 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
5172 | * the same time open the pool, without having to keep around the spa_t in some | |
5173 | * ambiguous state. | |
5174 | */ | |
5175 | static int | |
a926aab9 AZ |
5176 | spa_open_common(const char *pool, spa_t **spapp, const void *tag, |
5177 | nvlist_t *nvpolicy, nvlist_t **config) | |
34dc7c2f BB |
5178 | { |
5179 | spa_t *spa; | |
572e2857 | 5180 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 5181 | int error; |
34dc7c2f | 5182 | int locked = B_FALSE; |
526af785 | 5183 | int firstopen = B_FALSE; |
34dc7c2f BB |
5184 | |
5185 | *spapp = NULL; | |
5186 | ||
5187 | /* | |
5188 | * As disgusting as this is, we need to support recursive calls to this | |
5189 | * function because dsl_dir_open() is called during spa_load(), and ends | |
5190 | * up calling spa_open() again. The real fix is to figure out how to | |
5191 | * avoid dsl_dir_open() calling this in the first place. | |
5192 | */ | |
c25b8f99 | 5193 | if (MUTEX_NOT_HELD(&spa_namespace_lock)) { |
34dc7c2f BB |
5194 | mutex_enter(&spa_namespace_lock); |
5195 | locked = B_TRUE; | |
5196 | } | |
5197 | ||
5198 | if ((spa = spa_lookup(pool)) == NULL) { | |
5199 | if (locked) | |
5200 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5201 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 5202 | } |
428870ff | 5203 | |
34dc7c2f | 5204 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
8a393be3 | 5205 | zpool_load_policy_t policy; |
428870ff | 5206 | |
526af785 PJD |
5207 | firstopen = B_TRUE; |
5208 | ||
8a393be3 | 5209 | zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, |
428870ff | 5210 | &policy); |
8a393be3 | 5211 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) |
428870ff | 5212 | state = SPA_LOAD_RECOVER; |
34dc7c2f | 5213 | |
fb5f0bc8 | 5214 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5215 | |
428870ff BB |
5216 | if (state != SPA_LOAD_RECOVER) |
5217 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6cb8e530 | 5218 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; |
428870ff | 5219 | |
4a0ee12a | 5220 | zfs_dbgmsg("spa_open_common: opening %s", pool); |
8a393be3 PZ |
5221 | error = spa_load_best(spa, state, policy.zlp_txg, |
5222 | policy.zlp_rewind); | |
34dc7c2f BB |
5223 | |
5224 | if (error == EBADF) { | |
5225 | /* | |
5226 | * If vdev_validate() returns failure (indicated by | |
5227 | * EBADF), it indicates that one of the vdevs indicates | |
5228 | * that the pool has been exported or destroyed. If | |
5229 | * this is the case, the config cache is out of sync and | |
5230 | * we should remove the pool from the namespace. | |
5231 | */ | |
34dc7c2f BB |
5232 | spa_unload(spa); |
5233 | spa_deactivate(spa); | |
55c12724 | 5234 | spa_write_cachefile(spa, B_TRUE, B_TRUE, B_FALSE); |
34dc7c2f | 5235 | spa_remove(spa); |
34dc7c2f BB |
5236 | if (locked) |
5237 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5238 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
5239 | } |
5240 | ||
5241 | if (error) { | |
5242 | /* | |
5243 | * We can't open the pool, but we still have useful | |
5244 | * information: the state of each vdev after the | |
5245 | * attempted vdev_open(). Return this to the user. | |
5246 | */ | |
572e2857 | 5247 | if (config != NULL && spa->spa_config) { |
65ad5d11 AJ |
5248 | *config = fnvlist_dup(spa->spa_config); |
5249 | fnvlist_add_nvlist(*config, | |
572e2857 | 5250 | ZPOOL_CONFIG_LOAD_INFO, |
65ad5d11 | 5251 | spa->spa_load_info); |
572e2857 | 5252 | } |
34dc7c2f BB |
5253 | spa_unload(spa); |
5254 | spa_deactivate(spa); | |
428870ff | 5255 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
5256 | if (locked) |
5257 | mutex_exit(&spa_namespace_lock); | |
5258 | *spapp = NULL; | |
5259 | return (error); | |
34dc7c2f | 5260 | } |
34dc7c2f BB |
5261 | } |
5262 | ||
5263 | spa_open_ref(spa, tag); | |
5264 | ||
b128c09f | 5265 | if (config != NULL) |
34dc7c2f | 5266 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 5267 | |
572e2857 BB |
5268 | /* |
5269 | * If we've recovered the pool, pass back any information we | |
5270 | * gathered while doing the load. | |
5271 | */ | |
1bd02680 | 5272 | if (state == SPA_LOAD_RECOVER && config != NULL) { |
65ad5d11 AJ |
5273 | fnvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, |
5274 | spa->spa_load_info); | |
572e2857 BB |
5275 | } |
5276 | ||
428870ff BB |
5277 | if (locked) { |
5278 | spa->spa_last_open_failed = 0; | |
5279 | spa->spa_last_ubsync_txg = 0; | |
5280 | spa->spa_load_txg = 0; | |
5281 | mutex_exit(&spa_namespace_lock); | |
5282 | } | |
5283 | ||
526af785 | 5284 | if (firstopen) |
ec213971 | 5285 | zvol_create_minors_recursive(spa_name(spa)); |
526af785 | 5286 | |
428870ff BB |
5287 | *spapp = spa; |
5288 | ||
34dc7c2f BB |
5289 | return (0); |
5290 | } | |
5291 | ||
428870ff | 5292 | int |
a926aab9 AZ |
5293 | spa_open_rewind(const char *name, spa_t **spapp, const void *tag, |
5294 | nvlist_t *policy, nvlist_t **config) | |
428870ff BB |
5295 | { |
5296 | return (spa_open_common(name, spapp, tag, policy, config)); | |
5297 | } | |
5298 | ||
34dc7c2f | 5299 | int |
a926aab9 | 5300 | spa_open(const char *name, spa_t **spapp, const void *tag) |
34dc7c2f | 5301 | { |
428870ff | 5302 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
5303 | } |
5304 | ||
5305 | /* | |
5306 | * Lookup the given spa_t, incrementing the inject count in the process, | |
5307 | * preventing it from being exported or destroyed. | |
5308 | */ | |
5309 | spa_t * | |
5310 | spa_inject_addref(char *name) | |
5311 | { | |
5312 | spa_t *spa; | |
5313 | ||
5314 | mutex_enter(&spa_namespace_lock); | |
5315 | if ((spa = spa_lookup(name)) == NULL) { | |
5316 | mutex_exit(&spa_namespace_lock); | |
5317 | return (NULL); | |
5318 | } | |
5319 | spa->spa_inject_ref++; | |
5320 | mutex_exit(&spa_namespace_lock); | |
5321 | ||
5322 | return (spa); | |
5323 | } | |
5324 | ||
5325 | void | |
5326 | spa_inject_delref(spa_t *spa) | |
5327 | { | |
5328 | mutex_enter(&spa_namespace_lock); | |
5329 | spa->spa_inject_ref--; | |
5330 | mutex_exit(&spa_namespace_lock); | |
5331 | } | |
5332 | ||
5333 | /* | |
5334 | * Add spares device information to the nvlist. | |
5335 | */ | |
5336 | static void | |
5337 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
5338 | { | |
5339 | nvlist_t **spares; | |
5340 | uint_t i, nspares; | |
5341 | nvlist_t *nvroot; | |
5342 | uint64_t guid; | |
5343 | vdev_stat_t *vs; | |
5344 | uint_t vsc; | |
5345 | uint64_t pool; | |
5346 | ||
9babb374 BB |
5347 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5348 | ||
34dc7c2f BB |
5349 | if (spa->spa_spares.sav_count == 0) |
5350 | return; | |
5351 | ||
65ad5d11 AJ |
5352 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
5353 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
5354 | ZPOOL_CONFIG_SPARES, &spares, &nspares)); | |
34dc7c2f | 5355 | if (nspares != 0) { |
795075e6 PD |
5356 | fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, |
5357 | (const nvlist_t * const *)spares, nspares); | |
65ad5d11 AJ |
5358 | VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, |
5359 | &spares, &nspares)); | |
34dc7c2f BB |
5360 | |
5361 | /* | |
5362 | * Go through and find any spares which have since been | |
5363 | * repurposed as an active spare. If this is the case, update | |
5364 | * their status appropriately. | |
5365 | */ | |
5366 | for (i = 0; i < nspares; i++) { | |
65ad5d11 AJ |
5367 | guid = fnvlist_lookup_uint64(spares[i], |
5368 | ZPOOL_CONFIG_GUID); | |
b128c09f BB |
5369 | if (spa_spare_exists(guid, &pool, NULL) && |
5370 | pool != 0ULL) { | |
65ad5d11 AJ |
5371 | VERIFY0(nvlist_lookup_uint64_array(spares[i], |
5372 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, | |
5373 | &vsc)); | |
34dc7c2f BB |
5374 | vs->vs_state = VDEV_STATE_CANT_OPEN; |
5375 | vs->vs_aux = VDEV_AUX_SPARED; | |
5376 | } | |
5377 | } | |
5378 | } | |
5379 | } | |
5380 | ||
5381 | /* | |
5382 | * Add l2cache device information to the nvlist, including vdev stats. | |
5383 | */ | |
5384 | static void | |
5385 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
5386 | { | |
5387 | nvlist_t **l2cache; | |
5388 | uint_t i, j, nl2cache; | |
5389 | nvlist_t *nvroot; | |
5390 | uint64_t guid; | |
5391 | vdev_t *vd; | |
5392 | vdev_stat_t *vs; | |
5393 | uint_t vsc; | |
5394 | ||
9babb374 BB |
5395 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5396 | ||
34dc7c2f BB |
5397 | if (spa->spa_l2cache.sav_count == 0) |
5398 | return; | |
5399 | ||
65ad5d11 AJ |
5400 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
5401 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
5402 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache)); | |
34dc7c2f | 5403 | if (nl2cache != 0) { |
795075e6 PD |
5404 | fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
5405 | (const nvlist_t * const *)l2cache, nl2cache); | |
65ad5d11 AJ |
5406 | VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
5407 | &l2cache, &nl2cache)); | |
34dc7c2f BB |
5408 | |
5409 | /* | |
5410 | * Update level 2 cache device stats. | |
5411 | */ | |
5412 | ||
5413 | for (i = 0; i < nl2cache; i++) { | |
65ad5d11 AJ |
5414 | guid = fnvlist_lookup_uint64(l2cache[i], |
5415 | ZPOOL_CONFIG_GUID); | |
34dc7c2f BB |
5416 | |
5417 | vd = NULL; | |
5418 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
5419 | if (guid == | |
5420 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
5421 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
5422 | break; | |
5423 | } | |
5424 | } | |
5425 | ASSERT(vd != NULL); | |
5426 | ||
65ad5d11 AJ |
5427 | VERIFY0(nvlist_lookup_uint64_array(l2cache[i], |
5428 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)); | |
34dc7c2f | 5429 | vdev_get_stats(vd, vs); |
193a37cb TH |
5430 | vdev_config_generate_stats(vd, l2cache[i]); |
5431 | ||
34dc7c2f BB |
5432 | } |
5433 | } | |
34dc7c2f BB |
5434 | } |
5435 | ||
9ae529ec | 5436 | static void |
417104bd | 5437 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) |
9ae529ec | 5438 | { |
9ae529ec CS |
5439 | zap_cursor_t zc; |
5440 | zap_attribute_t za; | |
5441 | ||
9ae529ec CS |
5442 | if (spa->spa_feat_for_read_obj != 0) { |
5443 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5444 | spa->spa_feat_for_read_obj); | |
5445 | zap_cursor_retrieve(&zc, &za) == 0; | |
5446 | zap_cursor_advance(&zc)) { | |
5447 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5448 | za.za_num_integers == 1); | |
417104bd | 5449 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5450 | za.za_first_integer)); |
5451 | } | |
5452 | zap_cursor_fini(&zc); | |
5453 | } | |
5454 | ||
5455 | if (spa->spa_feat_for_write_obj != 0) { | |
5456 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5457 | spa->spa_feat_for_write_obj); | |
5458 | zap_cursor_retrieve(&zc, &za) == 0; | |
5459 | zap_cursor_advance(&zc)) { | |
5460 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5461 | za.za_num_integers == 1); | |
417104bd | 5462 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5463 | za.za_first_integer)); |
5464 | } | |
5465 | zap_cursor_fini(&zc); | |
5466 | } | |
417104bd NB |
5467 | } |
5468 | ||
5469 | static void | |
5470 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
5471 | { | |
5472 | int i; | |
5473 | ||
5474 | for (i = 0; i < SPA_FEATURES; i++) { | |
5475 | zfeature_info_t feature = spa_feature_table[i]; | |
5476 | uint64_t refcount; | |
5477 | ||
5478 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
5479 | continue; | |
5480 | ||
5481 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
5482 | } | |
5483 | } | |
5484 | ||
5485 | /* | |
5486 | * Store a list of pool features and their reference counts in the | |
5487 | * config. | |
5488 | * | |
5489 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
5490 | * the pool features and reference counts from disk, then save the list | |
5491 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
5492 | * and refresh its values from the cached reference counts. This | |
5493 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
5494 | * clear' can resume the pool. | |
5495 | */ | |
5496 | static void | |
5497 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
5498 | { | |
4eb30c68 | 5499 | nvlist_t *features; |
417104bd NB |
5500 | |
5501 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
5502 | ||
4eb30c68 NB |
5503 | mutex_enter(&spa->spa_feat_stats_lock); |
5504 | features = spa->spa_feat_stats; | |
5505 | ||
417104bd NB |
5506 | if (features != NULL) { |
5507 | spa_feature_stats_from_cache(spa, features); | |
5508 | } else { | |
5509 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
5510 | spa->spa_feat_stats = features; | |
5511 | spa_feature_stats_from_disk(spa, features); | |
5512 | } | |
9ae529ec | 5513 | |
417104bd NB |
5514 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, |
5515 | features)); | |
4eb30c68 NB |
5516 | |
5517 | mutex_exit(&spa->spa_feat_stats_lock); | |
9ae529ec CS |
5518 | } |
5519 | ||
34dc7c2f | 5520 | int |
9ae529ec CS |
5521 | spa_get_stats(const char *name, nvlist_t **config, |
5522 | char *altroot, size_t buflen) | |
34dc7c2f BB |
5523 | { |
5524 | int error; | |
5525 | spa_t *spa; | |
5526 | ||
5527 | *config = NULL; | |
428870ff | 5528 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 5529 | |
9babb374 BB |
5530 | if (spa != NULL) { |
5531 | /* | |
5532 | * This still leaves a window of inconsistency where the spares | |
5533 | * or l2cache devices could change and the config would be | |
5534 | * self-inconsistent. | |
5535 | */ | |
5536 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 5537 | |
9babb374 | 5538 | if (*config != NULL) { |
572e2857 BB |
5539 | uint64_t loadtimes[2]; |
5540 | ||
5541 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
5542 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
65ad5d11 AJ |
5543 | fnvlist_add_uint64_array(*config, |
5544 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2); | |
572e2857 | 5545 | |
65ad5d11 | 5546 | fnvlist_add_uint64(*config, |
9babb374 | 5547 | ZPOOL_CONFIG_ERRCOUNT, |
018f2604 | 5548 | spa_approx_errlog_size(spa)); |
9babb374 | 5549 | |
cec3a0a1 | 5550 | if (spa_suspended(spa)) { |
65ad5d11 | 5551 | fnvlist_add_uint64(*config, |
9babb374 | 5552 | ZPOOL_CONFIG_SUSPENDED, |
65ad5d11 AJ |
5553 | spa->spa_failmode); |
5554 | fnvlist_add_uint64(*config, | |
cec3a0a1 | 5555 | ZPOOL_CONFIG_SUSPENDED_REASON, |
65ad5d11 | 5556 | spa->spa_suspended); |
cec3a0a1 | 5557 | } |
b128c09f | 5558 | |
9babb374 BB |
5559 | spa_add_spares(spa, *config); |
5560 | spa_add_l2cache(spa, *config); | |
9ae529ec | 5561 | spa_add_feature_stats(spa, *config); |
9babb374 | 5562 | } |
34dc7c2f BB |
5563 | } |
5564 | ||
5565 | /* | |
5566 | * We want to get the alternate root even for faulted pools, so we cheat | |
5567 | * and call spa_lookup() directly. | |
5568 | */ | |
5569 | if (altroot) { | |
5570 | if (spa == NULL) { | |
5571 | mutex_enter(&spa_namespace_lock); | |
5572 | spa = spa_lookup(name); | |
5573 | if (spa) | |
5574 | spa_altroot(spa, altroot, buflen); | |
5575 | else | |
5576 | altroot[0] = '\0'; | |
5577 | spa = NULL; | |
5578 | mutex_exit(&spa_namespace_lock); | |
5579 | } else { | |
5580 | spa_altroot(spa, altroot, buflen); | |
5581 | } | |
5582 | } | |
5583 | ||
9babb374 BB |
5584 | if (spa != NULL) { |
5585 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 5586 | spa_close(spa, FTAG); |
9babb374 | 5587 | } |
34dc7c2f BB |
5588 | |
5589 | return (error); | |
5590 | } | |
5591 | ||
5592 | /* | |
5593 | * Validate that the auxiliary device array is well formed. We must have an | |
5594 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
5595 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
5596 | * specified, as long as they are well-formed. | |
5597 | */ | |
5598 | static int | |
5599 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
5600 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
5601 | vdev_labeltype_t label) | |
5602 | { | |
5603 | nvlist_t **dev; | |
5604 | uint_t i, ndev; | |
5605 | vdev_t *vd; | |
5606 | int error; | |
5607 | ||
b128c09f BB |
5608 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5609 | ||
34dc7c2f BB |
5610 | /* |
5611 | * It's acceptable to have no devs specified. | |
5612 | */ | |
5613 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
5614 | return (0); | |
5615 | ||
5616 | if (ndev == 0) | |
2e528b49 | 5617 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
5618 | |
5619 | /* | |
5620 | * Make sure the pool is formatted with a version that supports this | |
5621 | * device type. | |
5622 | */ | |
5623 | if (spa_version(spa) < version) | |
2e528b49 | 5624 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
5625 | |
5626 | /* | |
5627 | * Set the pending device list so we correctly handle device in-use | |
5628 | * checking. | |
5629 | */ | |
5630 | sav->sav_pending = dev; | |
5631 | sav->sav_npending = ndev; | |
5632 | ||
5633 | for (i = 0; i < ndev; i++) { | |
5634 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
5635 | mode)) != 0) | |
5636 | goto out; | |
5637 | ||
5638 | if (!vd->vdev_ops->vdev_op_leaf) { | |
5639 | vdev_free(vd); | |
2e528b49 | 5640 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5641 | goto out; |
5642 | } | |
5643 | ||
34dc7c2f BB |
5644 | vd->vdev_top = vd; |
5645 | ||
5646 | if ((error = vdev_open(vd)) == 0 && | |
5647 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
65ad5d11 AJ |
5648 | fnvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, |
5649 | vd->vdev_guid); | |
34dc7c2f BB |
5650 | } |
5651 | ||
5652 | vdev_free(vd); | |
5653 | ||
5654 | if (error && | |
5655 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
5656 | goto out; | |
5657 | else | |
5658 | error = 0; | |
5659 | } | |
5660 | ||
5661 | out: | |
5662 | sav->sav_pending = NULL; | |
5663 | sav->sav_npending = 0; | |
5664 | return (error); | |
5665 | } | |
5666 | ||
5667 | static int | |
5668 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
5669 | { | |
5670 | int error; | |
5671 | ||
b128c09f BB |
5672 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5673 | ||
34dc7c2f BB |
5674 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
5675 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
5676 | VDEV_LABEL_SPARE)) != 0) { | |
5677 | return (error); | |
5678 | } | |
5679 | ||
5680 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
5681 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
5682 | VDEV_LABEL_L2CACHE)); | |
5683 | } | |
5684 | ||
5685 | static void | |
5686 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
5687 | const char *config) | |
5688 | { | |
5689 | int i; | |
5690 | ||
5691 | if (sav->sav_config != NULL) { | |
5692 | nvlist_t **olddevs; | |
5693 | uint_t oldndevs; | |
5694 | nvlist_t **newdevs; | |
5695 | ||
5696 | /* | |
4e33ba4c | 5697 | * Generate new dev list by concatenating with the |
34dc7c2f BB |
5698 | * current dev list. |
5699 | */ | |
65ad5d11 AJ |
5700 | VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config, config, |
5701 | &olddevs, &oldndevs)); | |
34dc7c2f BB |
5702 | |
5703 | newdevs = kmem_alloc(sizeof (void *) * | |
79c76d5b | 5704 | (ndevs + oldndevs), KM_SLEEP); |
34dc7c2f | 5705 | for (i = 0; i < oldndevs; i++) |
65ad5d11 | 5706 | newdevs[i] = fnvlist_dup(olddevs[i]); |
34dc7c2f | 5707 | for (i = 0; i < ndevs; i++) |
65ad5d11 | 5708 | newdevs[i + oldndevs] = fnvlist_dup(devs[i]); |
34dc7c2f | 5709 | |
65ad5d11 | 5710 | fnvlist_remove(sav->sav_config, config); |
34dc7c2f | 5711 | |
795075e6 PD |
5712 | fnvlist_add_nvlist_array(sav->sav_config, config, |
5713 | (const nvlist_t * const *)newdevs, ndevs + oldndevs); | |
34dc7c2f BB |
5714 | for (i = 0; i < oldndevs + ndevs; i++) |
5715 | nvlist_free(newdevs[i]); | |
5716 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
5717 | } else { | |
5718 | /* | |
5719 | * Generate a new dev list. | |
5720 | */ | |
65ad5d11 | 5721 | sav->sav_config = fnvlist_alloc(); |
795075e6 PD |
5722 | fnvlist_add_nvlist_array(sav->sav_config, config, |
5723 | (const nvlist_t * const *)devs, ndevs); | |
34dc7c2f BB |
5724 | } |
5725 | } | |
5726 | ||
5727 | /* | |
5728 | * Stop and drop level 2 ARC devices | |
5729 | */ | |
5730 | void | |
5731 | spa_l2cache_drop(spa_t *spa) | |
5732 | { | |
5733 | vdev_t *vd; | |
5734 | int i; | |
5735 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
5736 | ||
5737 | for (i = 0; i < sav->sav_count; i++) { | |
5738 | uint64_t pool; | |
5739 | ||
5740 | vd = sav->sav_vdevs[i]; | |
5741 | ASSERT(vd != NULL); | |
5742 | ||
fb5f0bc8 BB |
5743 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
5744 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 5745 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
5746 | } |
5747 | } | |
5748 | ||
b5256303 TC |
5749 | /* |
5750 | * Verify encryption parameters for spa creation. If we are encrypting, we must | |
5751 | * have the encryption feature flag enabled. | |
5752 | */ | |
5753 | static int | |
5754 | spa_create_check_encryption_params(dsl_crypto_params_t *dcp, | |
5755 | boolean_t has_encryption) | |
5756 | { | |
5757 | if (dcp->cp_crypt != ZIO_CRYPT_OFF && | |
5758 | dcp->cp_crypt != ZIO_CRYPT_INHERIT && | |
5759 | !has_encryption) | |
5760 | return (SET_ERROR(ENOTSUP)); | |
5761 | ||
1fff937a | 5762 | return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); |
b5256303 TC |
5763 | } |
5764 | ||
34dc7c2f BB |
5765 | /* |
5766 | * Pool Creation | |
5767 | */ | |
5768 | int | |
5769 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b5256303 | 5770 | nvlist_t *zplprops, dsl_crypto_params_t *dcp) |
34dc7c2f BB |
5771 | { |
5772 | spa_t *spa; | |
5773 | char *altroot = NULL; | |
5774 | vdev_t *rvd; | |
5775 | dsl_pool_t *dp; | |
5776 | dmu_tx_t *tx; | |
9babb374 | 5777 | int error = 0; |
34dc7c2f BB |
5778 | uint64_t txg = TXG_INITIAL; |
5779 | nvlist_t **spares, **l2cache; | |
5780 | uint_t nspares, nl2cache; | |
b2255edc | 5781 | uint64_t version, obj, ndraid = 0; |
9ae529ec | 5782 | boolean_t has_features; |
b5256303 | 5783 | boolean_t has_encryption; |
715c996d | 5784 | boolean_t has_allocclass; |
b5256303 TC |
5785 | spa_feature_t feat; |
5786 | char *feat_name; | |
83e9986f RY |
5787 | char *poolname; |
5788 | nvlist_t *nvl; | |
5789 | ||
cc99f275 DB |
5790 | if (props == NULL || |
5791 | nvlist_lookup_string(props, "tname", &poolname) != 0) | |
83e9986f | 5792 | poolname = (char *)pool; |
34dc7c2f BB |
5793 | |
5794 | /* | |
5795 | * If this pool already exists, return failure. | |
5796 | */ | |
5797 | mutex_enter(&spa_namespace_lock); | |
83e9986f | 5798 | if (spa_lookup(poolname) != NULL) { |
34dc7c2f | 5799 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5800 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5801 | } |
5802 | ||
5803 | /* | |
5804 | * Allocate a new spa_t structure. | |
5805 | */ | |
83e9986f RY |
5806 | nvl = fnvlist_alloc(); |
5807 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
34dc7c2f BB |
5808 | (void) nvlist_lookup_string(props, |
5809 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
83e9986f RY |
5810 | spa = spa_add(poolname, nvl, altroot); |
5811 | fnvlist_free(nvl); | |
fb5f0bc8 | 5812 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5813 | |
34dc7c2f | 5814 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
5815 | spa_deactivate(spa); |
5816 | spa_remove(spa); | |
b128c09f | 5817 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
5818 | return (error); |
5819 | } | |
5820 | ||
83e9986f RY |
5821 | /* |
5822 | * Temporary pool names should never be written to disk. | |
5823 | */ | |
5824 | if (poolname != pool) | |
5825 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
5826 | ||
9ae529ec | 5827 | has_features = B_FALSE; |
b5256303 | 5828 | has_encryption = B_FALSE; |
715c996d | 5829 | has_allocclass = B_FALSE; |
1c27024e | 5830 | for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); |
9ae529ec | 5831 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { |
b5256303 | 5832 | if (zpool_prop_feature(nvpair_name(elem))) { |
9ae529ec | 5833 | has_features = B_TRUE; |
b5256303 TC |
5834 | |
5835 | feat_name = strchr(nvpair_name(elem), '@') + 1; | |
5836 | VERIFY0(zfeature_lookup_name(feat_name, &feat)); | |
5837 | if (feat == SPA_FEATURE_ENCRYPTION) | |
5838 | has_encryption = B_TRUE; | |
715c996d | 5839 | if (feat == SPA_FEATURE_ALLOCATION_CLASSES) |
5840 | has_allocclass = B_TRUE; | |
b5256303 TC |
5841 | } |
5842 | } | |
5843 | ||
5844 | /* verify encryption params, if they were provided */ | |
5845 | if (dcp != NULL) { | |
5846 | error = spa_create_check_encryption_params(dcp, has_encryption); | |
5847 | if (error != 0) { | |
5848 | spa_deactivate(spa); | |
5849 | spa_remove(spa); | |
5850 | mutex_exit(&spa_namespace_lock); | |
5851 | return (error); | |
5852 | } | |
9ae529ec | 5853 | } |
c24fa4b1 | 5854 | if (!has_allocclass && zfs_special_devs(nvroot, NULL)) { |
715c996d | 5855 | spa_deactivate(spa); |
5856 | spa_remove(spa); | |
5857 | mutex_exit(&spa_namespace_lock); | |
5858 | return (ENOTSUP); | |
5859 | } | |
9ae529ec CS |
5860 | |
5861 | if (has_features || nvlist_lookup_uint64(props, | |
5862 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 5863 | version = SPA_VERSION; |
9ae529ec CS |
5864 | } |
5865 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
5866 | |
5867 | spa->spa_first_txg = txg; | |
5868 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
5869 | spa->spa_uberblock.ub_version = version; |
5870 | spa->spa_ubsync = spa->spa_uberblock; | |
3dfb57a3 | 5871 | spa->spa_load_state = SPA_LOAD_CREATE; |
a1d477c2 MA |
5872 | spa->spa_removing_phys.sr_state = DSS_NONE; |
5873 | spa->spa_removing_phys.sr_removing_vdev = -1; | |
5874 | spa->spa_removing_phys.sr_prev_indirect_vdev = -1; | |
944a3724 | 5875 | spa->spa_indirect_vdevs_loaded = B_TRUE; |
34dc7c2f | 5876 | |
9babb374 BB |
5877 | /* |
5878 | * Create "The Godfather" zio to hold all async IOs | |
5879 | */ | |
e022864d MA |
5880 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
5881 | KM_SLEEP); | |
1c27024e | 5882 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
5883 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
5884 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
5885 | ZIO_FLAG_GODFATHER); | |
5886 | } | |
9babb374 | 5887 | |
34dc7c2f BB |
5888 | /* |
5889 | * Create the root vdev. | |
5890 | */ | |
b128c09f | 5891 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5892 | |
5893 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
5894 | ||
5895 | ASSERT(error != 0 || rvd != NULL); | |
5896 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
5897 | ||
5898 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
2e528b49 | 5899 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5900 | |
5901 | if (error == 0 && | |
5902 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
b2255edc BB |
5903 | (error = vdev_draid_spare_create(nvroot, rvd, &ndraid, 0)) == 0 && |
5904 | (error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) == 0) { | |
cc99f275 DB |
5905 | /* |
5906 | * instantiate the metaslab groups (this will dirty the vdevs) | |
5907 | * we can no longer error exit past this point | |
5908 | */ | |
5909 | for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { | |
5910 | vdev_t *vd = rvd->vdev_child[c]; | |
5911 | ||
5912 | vdev_metaslab_set_size(vd); | |
5913 | vdev_expand(vd, txg); | |
9babb374 | 5914 | } |
34dc7c2f BB |
5915 | } |
5916 | ||
b128c09f | 5917 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5918 | |
5919 | if (error != 0) { | |
5920 | spa_unload(spa); | |
5921 | spa_deactivate(spa); | |
5922 | spa_remove(spa); | |
5923 | mutex_exit(&spa_namespace_lock); | |
5924 | return (error); | |
5925 | } | |
5926 | ||
5927 | /* | |
5928 | * Get the list of spares, if specified. | |
5929 | */ | |
5930 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5931 | &spares, &nspares) == 0) { | |
65ad5d11 AJ |
5932 | spa->spa_spares.sav_config = fnvlist_alloc(); |
5933 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
795075e6 PD |
5934 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
5935 | nspares); | |
b128c09f | 5936 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5937 | spa_load_spares(spa); |
b128c09f | 5938 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5939 | spa->spa_spares.sav_sync = B_TRUE; |
5940 | } | |
5941 | ||
5942 | /* | |
5943 | * Get the list of level 2 cache devices, if specified. | |
5944 | */ | |
5945 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5946 | &l2cache, &nl2cache) == 0) { | |
795075e6 PD |
5947 | VERIFY0(nvlist_alloc(&spa->spa_l2cache.sav_config, |
5948 | NV_UNIQUE_NAME, KM_SLEEP)); | |
65ad5d11 | 5949 | fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
795075e6 PD |
5950 | ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache, |
5951 | nl2cache); | |
b128c09f | 5952 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5953 | spa_load_l2cache(spa); |
b128c09f | 5954 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5955 | spa->spa_l2cache.sav_sync = B_TRUE; |
5956 | } | |
5957 | ||
9ae529ec | 5958 | spa->spa_is_initializing = B_TRUE; |
b5256303 | 5959 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); |
9ae529ec | 5960 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 5961 | |
428870ff BB |
5962 | /* |
5963 | * Create DDTs (dedup tables). | |
5964 | */ | |
5965 | ddt_create(spa); | |
5966 | ||
5967 | spa_update_dspace(spa); | |
5968 | ||
34dc7c2f BB |
5969 | tx = dmu_tx_create_assigned(dp, txg); |
5970 | ||
d5e024cb BB |
5971 | /* |
5972 | * Create the pool's history object. | |
5973 | */ | |
5974 | if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history) | |
5975 | spa_history_create_obj(spa, tx); | |
5976 | ||
5977 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); | |
5978 | spa_history_log_version(spa, "create", tx); | |
5979 | ||
34dc7c2f BB |
5980 | /* |
5981 | * Create the pool config object. | |
5982 | */ | |
5983 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 5984 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
5985 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
5986 | ||
5987 | if (zap_add(spa->spa_meta_objset, | |
5988 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
5989 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
5990 | cmn_err(CE_PANIC, "failed to add pool config"); | |
5991 | } | |
5992 | ||
428870ff BB |
5993 | if (zap_add(spa->spa_meta_objset, |
5994 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
5995 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
5996 | cmn_err(CE_PANIC, "failed to add pool version"); | |
5997 | } | |
5998 | ||
34dc7c2f BB |
5999 | /* Newly created pools with the right version are always deflated. */ |
6000 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
6001 | spa->spa_deflate = TRUE; | |
6002 | if (zap_add(spa->spa_meta_objset, | |
6003 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
6004 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
6005 | cmn_err(CE_PANIC, "failed to add deflate"); | |
6006 | } | |
6007 | } | |
6008 | ||
6009 | /* | |
428870ff | 6010 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
6011 | * because sync-to-convergence takes longer if the blocksize |
6012 | * keeps changing. | |
6013 | */ | |
428870ff BB |
6014 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
6015 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 6016 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 6017 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
6018 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
6019 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
6020 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 6021 | } |
428870ff BB |
6022 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
6023 | spa->spa_meta_objset, obj)); | |
34dc7c2f | 6024 | |
3c67d83a TH |
6025 | /* |
6026 | * Generate some random noise for salted checksums to operate on. | |
6027 | */ | |
6028 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
6029 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
6030 | ||
34dc7c2f BB |
6031 | /* |
6032 | * Set pool properties. | |
6033 | */ | |
6034 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
6035 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
6036 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 6037 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
379ca9cf | 6038 | spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); |
1b939560 | 6039 | spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM); |
428870ff | 6040 | |
d164b209 BB |
6041 | if (props != NULL) { |
6042 | spa_configfile_set(spa, props, B_FALSE); | |
13fe0198 | 6043 | spa_sync_props(props, tx); |
d164b209 | 6044 | } |
34dc7c2f | 6045 | |
b2255edc BB |
6046 | for (int i = 0; i < ndraid; i++) |
6047 | spa_feature_incr(spa, SPA_FEATURE_DRAID, tx); | |
6048 | ||
34dc7c2f BB |
6049 | dmu_tx_commit(tx); |
6050 | ||
6051 | spa->spa_sync_on = B_TRUE; | |
b5256303 | 6052 | txg_sync_start(dp); |
379ca9cf | 6053 | mmp_thread_start(spa); |
b5256303 | 6054 | txg_wait_synced(dp, txg); |
34dc7c2f | 6055 | |
9d5b5245 SD |
6056 | spa_spawn_aux_threads(spa); |
6057 | ||
55c12724 | 6058 | spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE); |
34dc7c2f | 6059 | |
0c66c32d JG |
6060 | /* |
6061 | * Don't count references from objsets that are already closed | |
6062 | * and are making their way through the eviction process. | |
6063 | */ | |
6064 | spa_evicting_os_wait(spa); | |
424fd7c3 | 6065 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
3dfb57a3 | 6066 | spa->spa_load_state = SPA_LOAD_NONE; |
b128c09f | 6067 | |
4759342a JL |
6068 | spa_import_os(spa); |
6069 | ||
d164b209 BB |
6070 | mutex_exit(&spa_namespace_lock); |
6071 | ||
34dc7c2f BB |
6072 | return (0); |
6073 | } | |
6074 | ||
9babb374 BB |
6075 | /* |
6076 | * Import a non-root pool into the system. | |
6077 | */ | |
6078 | int | |
13fe0198 | 6079 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
6080 | { |
6081 | spa_t *spa; | |
6082 | char *altroot = NULL; | |
428870ff | 6083 | spa_load_state_t state = SPA_LOAD_IMPORT; |
8a393be3 | 6084 | zpool_load_policy_t policy; |
da92d5cb | 6085 | spa_mode_t mode = spa_mode_global; |
572e2857 | 6086 | uint64_t readonly = B_FALSE; |
9babb374 | 6087 | int error; |
34dc7c2f BB |
6088 | nvlist_t *nvroot; |
6089 | nvlist_t **spares, **l2cache; | |
6090 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
6091 | |
6092 | /* | |
6093 | * If a pool with this name exists, return failure. | |
6094 | */ | |
6095 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6096 | if (spa_lookup(pool) != NULL) { |
9babb374 | 6097 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 6098 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
6099 | } |
6100 | ||
6101 | /* | |
6102 | * Create and initialize the spa structure. | |
6103 | */ | |
6104 | (void) nvlist_lookup_string(props, | |
6105 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
6106 | (void) nvlist_lookup_uint64(props, |
6107 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
6108 | if (readonly) | |
da92d5cb | 6109 | mode = SPA_MODE_READ; |
428870ff | 6110 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
6111 | spa->spa_import_flags = flags; |
6112 | ||
6113 | /* | |
6114 | * Verbatim import - Take a pool and insert it into the namespace | |
6115 | * as if it had been loaded at boot. | |
6116 | */ | |
6117 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
6118 | if (props != NULL) | |
6119 | spa_configfile_set(spa, props, B_FALSE); | |
6120 | ||
55c12724 | 6121 | spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE); |
12fa0466 | 6122 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
4a0ee12a | 6123 | zfs_dbgmsg("spa_import: verbatim import of %s", pool); |
572e2857 | 6124 | mutex_exit(&spa_namespace_lock); |
572e2857 BB |
6125 | return (0); |
6126 | } | |
6127 | ||
6128 | spa_activate(spa, mode); | |
34dc7c2f | 6129 | |
9babb374 BB |
6130 | /* |
6131 | * Don't start async tasks until we know everything is healthy. | |
6132 | */ | |
6133 | spa_async_suspend(spa); | |
b128c09f | 6134 | |
8a393be3 PZ |
6135 | zpool_get_load_policy(config, &policy); |
6136 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) | |
572e2857 BB |
6137 | state = SPA_LOAD_RECOVER; |
6138 | ||
6cb8e530 | 6139 | spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; |
572e2857 | 6140 | |
6cb8e530 PZ |
6141 | if (state != SPA_LOAD_RECOVER) { |
6142 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6143 | zfs_dbgmsg("spa_import: importing %s", pool); | |
6144 | } else { | |
6145 | zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " | |
8a393be3 | 6146 | "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); |
6cb8e530 | 6147 | } |
8a393be3 | 6148 | error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); |
428870ff BB |
6149 | |
6150 | /* | |
572e2857 BB |
6151 | * Propagate anything learned while loading the pool and pass it |
6152 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 6153 | */ |
65ad5d11 | 6154 | fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, spa->spa_load_info); |
34dc7c2f | 6155 | |
b128c09f | 6156 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6157 | /* |
9babb374 BB |
6158 | * Toss any existing sparelist, as it doesn't have any validity |
6159 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 6160 | */ |
9babb374 | 6161 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
6162 | nvlist_free(spa->spa_spares.sav_config); |
6163 | spa->spa_spares.sav_config = NULL; | |
6164 | spa_load_spares(spa); | |
6165 | } | |
9babb374 | 6166 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
6167 | nvlist_free(spa->spa_l2cache.sav_config); |
6168 | spa->spa_l2cache.sav_config = NULL; | |
6169 | spa_load_l2cache(spa); | |
6170 | } | |
6171 | ||
65ad5d11 | 6172 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
b128c09f | 6173 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 6174 | |
d164b209 BB |
6175 | if (props != NULL) |
6176 | spa_configfile_set(spa, props, B_FALSE); | |
6177 | ||
fb5f0bc8 BB |
6178 | if (error != 0 || (props && spa_writeable(spa) && |
6179 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
6180 | spa_unload(spa); |
6181 | spa_deactivate(spa); | |
6182 | spa_remove(spa); | |
34dc7c2f BB |
6183 | mutex_exit(&spa_namespace_lock); |
6184 | return (error); | |
6185 | } | |
6186 | ||
572e2857 BB |
6187 | spa_async_resume(spa); |
6188 | ||
34dc7c2f BB |
6189 | /* |
6190 | * Override any spares and level 2 cache devices as specified by | |
6191 | * the user, as these may have correct device names/devids, etc. | |
6192 | */ | |
6193 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
6194 | &spares, &nspares) == 0) { | |
6195 | if (spa->spa_spares.sav_config) | |
65ad5d11 AJ |
6196 | fnvlist_remove(spa->spa_spares.sav_config, |
6197 | ZPOOL_CONFIG_SPARES); | |
34dc7c2f | 6198 | else |
65ad5d11 AJ |
6199 | spa->spa_spares.sav_config = fnvlist_alloc(); |
6200 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
795075e6 PD |
6201 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
6202 | nspares); | |
b128c09f | 6203 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6204 | spa_load_spares(spa); |
b128c09f | 6205 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6206 | spa->spa_spares.sav_sync = B_TRUE; |
6207 | } | |
6208 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
6209 | &l2cache, &nl2cache) == 0) { | |
6210 | if (spa->spa_l2cache.sav_config) | |
65ad5d11 AJ |
6211 | fnvlist_remove(spa->spa_l2cache.sav_config, |
6212 | ZPOOL_CONFIG_L2CACHE); | |
34dc7c2f | 6213 | else |
65ad5d11 AJ |
6214 | spa->spa_l2cache.sav_config = fnvlist_alloc(); |
6215 | fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config, | |
795075e6 PD |
6216 | ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache, |
6217 | nl2cache); | |
b128c09f | 6218 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6219 | spa_load_l2cache(spa); |
b128c09f | 6220 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6221 | spa->spa_l2cache.sav_sync = B_TRUE; |
6222 | } | |
6223 | ||
428870ff BB |
6224 | /* |
6225 | * Check for any removed devices. | |
6226 | */ | |
6227 | if (spa->spa_autoreplace) { | |
6228 | spa_aux_check_removed(&spa->spa_spares); | |
6229 | spa_aux_check_removed(&spa->spa_l2cache); | |
6230 | } | |
6231 | ||
fb5f0bc8 | 6232 | if (spa_writeable(spa)) { |
b128c09f BB |
6233 | /* |
6234 | * Update the config cache to include the newly-imported pool. | |
6235 | */ | |
45d1cae3 | 6236 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 6237 | } |
34dc7c2f | 6238 | |
34dc7c2f | 6239 | /* |
9babb374 BB |
6240 | * It's possible that the pool was expanded while it was exported. |
6241 | * We kick off an async task to handle this for us. | |
34dc7c2f | 6242 | */ |
9babb374 | 6243 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 6244 | |
d5e024cb | 6245 | spa_history_log_version(spa, "import", NULL); |
fb390aaf | 6246 | |
12fa0466 | 6247 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
fb390aaf | 6248 | |
fb390aaf HR |
6249 | mutex_exit(&spa_namespace_lock); |
6250 | ||
ec213971 | 6251 | zvol_create_minors_recursive(pool); |
4a22ba5b | 6252 | |
4759342a JL |
6253 | spa_import_os(spa); |
6254 | ||
b128c09f BB |
6255 | return (0); |
6256 | } | |
6257 | ||
34dc7c2f BB |
6258 | nvlist_t * |
6259 | spa_tryimport(nvlist_t *tryconfig) | |
6260 | { | |
6261 | nvlist_t *config = NULL; | |
6cb8e530 | 6262 | char *poolname, *cachefile; |
34dc7c2f BB |
6263 | spa_t *spa; |
6264 | uint64_t state; | |
d164b209 | 6265 | int error; |
8a393be3 | 6266 | zpool_load_policy_t policy; |
34dc7c2f BB |
6267 | |
6268 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
6269 | return (NULL); | |
6270 | ||
6271 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
6272 | return (NULL); | |
6273 | ||
6274 | /* | |
6275 | * Create and initialize the spa structure. | |
6276 | */ | |
6277 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6278 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
da92d5cb | 6279 | spa_activate(spa, SPA_MODE_READ); |
34dc7c2f BB |
6280 | |
6281 | /* | |
8a393be3 | 6282 | * Rewind pool if a max txg was provided. |
34dc7c2f | 6283 | */ |
8a393be3 PZ |
6284 | zpool_get_load_policy(spa->spa_config, &policy); |
6285 | if (policy.zlp_txg != UINT64_MAX) { | |
6286 | spa->spa_load_max_txg = policy.zlp_txg; | |
6cb8e530 PZ |
6287 | spa->spa_extreme_rewind = B_TRUE; |
6288 | zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", | |
8a393be3 | 6289 | poolname, (longlong_t)policy.zlp_txg); |
6cb8e530 PZ |
6290 | } else { |
6291 | zfs_dbgmsg("spa_tryimport: importing %s", poolname); | |
6292 | } | |
6293 | ||
6294 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) | |
6295 | == 0) { | |
6296 | zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); | |
6297 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; | |
6298 | } else { | |
6299 | spa->spa_config_source = SPA_CONFIG_SRC_SCAN; | |
6300 | } | |
6301 | ||
6302 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); | |
34dc7c2f BB |
6303 | |
6304 | /* | |
6305 | * If 'tryconfig' was at least parsable, return the current config. | |
6306 | */ | |
6307 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 6308 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
65ad5d11 AJ |
6309 | fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, poolname); |
6310 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, state); | |
6311 | fnvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
6312 | spa->spa_uberblock.ub_timestamp); | |
6313 | fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, | |
6314 | spa->spa_load_info); | |
6315 | fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, | |
6316 | spa->spa_errata); | |
34dc7c2f BB |
6317 | |
6318 | /* | |
6319 | * If the bootfs property exists on this pool then we | |
6320 | * copy it out so that external consumers can tell which | |
6321 | * pools are bootable. | |
6322 | */ | |
d164b209 | 6323 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
79c76d5b | 6324 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6325 | |
6326 | /* | |
6327 | * We have to play games with the name since the | |
6328 | * pool was opened as TRYIMPORT_NAME. | |
6329 | */ | |
b128c09f | 6330 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
6331 | spa->spa_bootfs, tmpname) == 0) { |
6332 | char *cp; | |
d1d7e268 MK |
6333 | char *dsname; |
6334 | ||
79c76d5b | 6335 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6336 | |
6337 | cp = strchr(tmpname, '/'); | |
6338 | if (cp == NULL) { | |
6339 | (void) strlcpy(dsname, tmpname, | |
6340 | MAXPATHLEN); | |
6341 | } else { | |
6342 | (void) snprintf(dsname, MAXPATHLEN, | |
6343 | "%s/%s", poolname, ++cp); | |
6344 | } | |
65ad5d11 AJ |
6345 | fnvlist_add_string(config, ZPOOL_CONFIG_BOOTFS, |
6346 | dsname); | |
34dc7c2f BB |
6347 | kmem_free(dsname, MAXPATHLEN); |
6348 | } | |
6349 | kmem_free(tmpname, MAXPATHLEN); | |
6350 | } | |
6351 | ||
6352 | /* | |
6353 | * Add the list of hot spares and level 2 cache devices. | |
6354 | */ | |
9babb374 | 6355 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
6356 | spa_add_spares(spa, config); |
6357 | spa_add_l2cache(spa, config); | |
9babb374 | 6358 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
6359 | } |
6360 | ||
6361 | spa_unload(spa); | |
6362 | spa_deactivate(spa); | |
6363 | spa_remove(spa); | |
6364 | mutex_exit(&spa_namespace_lock); | |
6365 | ||
6366 | return (config); | |
6367 | } | |
6368 | ||
6369 | /* | |
6370 | * Pool export/destroy | |
6371 | * | |
6372 | * The act of destroying or exporting a pool is very simple. We make sure there | |
6373 | * is no more pending I/O and any references to the pool are gone. Then, we | |
6374 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
6375 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
6376 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
6377 | */ |
6378 | static int | |
4d55ea81 | 6379 | spa_export_common(const char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 6380 | boolean_t force, boolean_t hardforce) |
34dc7c2f | 6381 | { |
f4f50a70 | 6382 | int error; |
34dc7c2f BB |
6383 | spa_t *spa; |
6384 | ||
6385 | if (oldconfig) | |
6386 | *oldconfig = NULL; | |
6387 | ||
da92d5cb | 6388 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
2e528b49 | 6389 | return (SET_ERROR(EROFS)); |
34dc7c2f BB |
6390 | |
6391 | mutex_enter(&spa_namespace_lock); | |
6392 | if ((spa = spa_lookup(pool)) == NULL) { | |
6393 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 6394 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
6395 | } |
6396 | ||
43a85362 SD |
6397 | if (spa->spa_is_exporting) { |
6398 | /* the pool is being exported by another thread */ | |
6399 | mutex_exit(&spa_namespace_lock); | |
6400 | return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS)); | |
6401 | } | |
6402 | spa->spa_is_exporting = B_TRUE; | |
6403 | ||
34dc7c2f BB |
6404 | /* |
6405 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
6406 | * reacquire the namespace lock, and see if we can export. | |
6407 | */ | |
6408 | spa_open_ref(spa, FTAG); | |
6409 | mutex_exit(&spa_namespace_lock); | |
6410 | spa_async_suspend(spa); | |
a0bd735a BP |
6411 | if (spa->spa_zvol_taskq) { |
6412 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
6413 | taskq_wait(spa->spa_zvol_taskq); | |
6414 | } | |
34dc7c2f BB |
6415 | mutex_enter(&spa_namespace_lock); |
6416 | spa_close(spa, FTAG); | |
6417 | ||
d14cfd83 IH |
6418 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) |
6419 | goto export_spa; | |
34dc7c2f | 6420 | /* |
d14cfd83 IH |
6421 | * The pool will be in core if it's openable, in which case we can |
6422 | * modify its state. Objsets may be open only because they're dirty, | |
6423 | * so we have to force it to sync before checking spa_refcnt. | |
34dc7c2f | 6424 | */ |
0c66c32d | 6425 | if (spa->spa_sync_on) { |
34dc7c2f | 6426 | txg_wait_synced(spa->spa_dsl_pool, 0); |
0c66c32d JG |
6427 | spa_evicting_os_wait(spa); |
6428 | } | |
34dc7c2f | 6429 | |
d14cfd83 IH |
6430 | /* |
6431 | * A pool cannot be exported or destroyed if there are active | |
6432 | * references. If we are resetting a pool, allow references by | |
6433 | * fault injection handlers. | |
6434 | */ | |
f4f50a70 WA |
6435 | if (!spa_refcount_zero(spa) || (spa->spa_inject_ref != 0)) { |
6436 | error = SET_ERROR(EBUSY); | |
6437 | goto fail; | |
d14cfd83 | 6438 | } |
34dc7c2f | 6439 | |
d14cfd83 | 6440 | if (spa->spa_sync_on) { |
88b199c2 | 6441 | vdev_t *rvd = spa->spa_root_vdev; |
b128c09f BB |
6442 | /* |
6443 | * A pool cannot be exported if it has an active shared spare. | |
6444 | * This is to prevent other pools stealing the active spare | |
6445 | * from an exported pool. At user's own will, such pool can | |
6446 | * be forcedly exported. | |
6447 | */ | |
6448 | if (!force && new_state == POOL_STATE_EXPORTED && | |
6449 | spa_has_active_shared_spare(spa)) { | |
f4f50a70 WA |
6450 | error = SET_ERROR(EXDEV); |
6451 | goto fail; | |
b128c09f | 6452 | } |
34dc7c2f | 6453 | |
619f0976 GW |
6454 | /* |
6455 | * We're about to export or destroy this pool. Make sure | |
1b939560 BB |
6456 | * we stop all initialization and trim activity here before |
6457 | * we set the spa_final_txg. This will ensure that all | |
619f0976 GW |
6458 | * dirty data resulting from the initialization is |
6459 | * committed to disk before we unload the pool. | |
6460 | */ | |
88b199c2 RY |
6461 | vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE); |
6462 | vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE); | |
6463 | vdev_autotrim_stop_all(spa); | |
6464 | vdev_rebuild_stop_all(spa); | |
619f0976 | 6465 | |
34dc7c2f BB |
6466 | /* |
6467 | * We want this to be reflected on every label, | |
6468 | * so mark them all dirty. spa_unload() will do the | |
6469 | * final sync that pushes these changes out. | |
6470 | */ | |
fb5f0bc8 | 6471 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 6472 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6473 | spa->spa_state = new_state; |
88b199c2 | 6474 | vdev_config_dirty(rvd); |
2fb52853 GA |
6475 | spa_config_exit(spa, SCL_ALL, FTAG); |
6476 | } | |
6477 | ||
6478 | /* | |
6479 | * If the log space map feature is enabled and the pool is | |
6480 | * getting exported (but not destroyed), we want to spend some | |
6481 | * time flushing as many metaslabs as we can in an attempt to | |
6482 | * destroy log space maps and save import time. This has to be | |
6483 | * done before we set the spa_final_txg, otherwise | |
6484 | * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs. | |
6485 | * spa_should_flush_logs_on_unload() should be called after | |
6486 | * spa_state has been set to the new_state. | |
6487 | */ | |
6488 | if (spa_should_flush_logs_on_unload(spa)) | |
6489 | spa_unload_log_sm_flush_all(spa); | |
6490 | ||
6491 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { | |
6492 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
428870ff BB |
6493 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
6494 | TXG_DEFER_SIZE + 1; | |
b128c09f | 6495 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6496 | } |
6497 | } | |
6498 | ||
d14cfd83 | 6499 | export_spa: |
4759342a JL |
6500 | spa_export_os(spa); |
6501 | ||
d5e024cb BB |
6502 | if (new_state == POOL_STATE_DESTROYED) |
6503 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); | |
6504 | else if (new_state == POOL_STATE_EXPORTED) | |
6505 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT); | |
34dc7c2f BB |
6506 | |
6507 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
6508 | spa_unload(spa); | |
6509 | spa_deactivate(spa); | |
6510 | } | |
6511 | ||
6512 | if (oldconfig && spa->spa_config) | |
65ad5d11 | 6513 | *oldconfig = fnvlist_dup(spa->spa_config); |
34dc7c2f BB |
6514 | |
6515 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 | 6516 | if (!hardforce) |
55c12724 | 6517 | spa_write_cachefile(spa, B_TRUE, B_TRUE, B_FALSE); |
34dc7c2f | 6518 | spa_remove(spa); |
43a85362 SD |
6519 | } else { |
6520 | /* | |
6521 | * If spa_remove() is not called for this spa_t and | |
6522 | * there is any possibility that it can be reused, | |
6523 | * we make sure to reset the exporting flag. | |
6524 | */ | |
6525 | spa->spa_is_exporting = B_FALSE; | |
34dc7c2f | 6526 | } |
34dc7c2f | 6527 | |
43a85362 | 6528 | mutex_exit(&spa_namespace_lock); |
34dc7c2f | 6529 | return (0); |
f4f50a70 WA |
6530 | |
6531 | fail: | |
6532 | spa->spa_is_exporting = B_FALSE; | |
6533 | spa_async_resume(spa); | |
6534 | mutex_exit(&spa_namespace_lock); | |
6535 | return (error); | |
34dc7c2f BB |
6536 | } |
6537 | ||
6538 | /* | |
6539 | * Destroy a storage pool. | |
6540 | */ | |
6541 | int | |
4d55ea81 | 6542 | spa_destroy(const char *pool) |
34dc7c2f | 6543 | { |
fb5f0bc8 BB |
6544 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
6545 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
6546 | } |
6547 | ||
6548 | /* | |
6549 | * Export a storage pool. | |
6550 | */ | |
6551 | int | |
4d55ea81 | 6552 | spa_export(const char *pool, nvlist_t **oldconfig, boolean_t force, |
fb5f0bc8 | 6553 | boolean_t hardforce) |
34dc7c2f | 6554 | { |
fb5f0bc8 BB |
6555 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
6556 | force, hardforce)); | |
34dc7c2f BB |
6557 | } |
6558 | ||
6559 | /* | |
6560 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
6561 | * from the namespace in any way. | |
6562 | */ | |
6563 | int | |
4d55ea81 | 6564 | spa_reset(const char *pool) |
34dc7c2f | 6565 | { |
b128c09f | 6566 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 6567 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
6568 | } |
6569 | ||
34dc7c2f BB |
6570 | /* |
6571 | * ========================================================================== | |
6572 | * Device manipulation | |
6573 | * ========================================================================== | |
6574 | */ | |
6575 | ||
b2255edc BB |
6576 | /* |
6577 | * This is called as a synctask to increment the draid feature flag | |
6578 | */ | |
6579 | static void | |
6580 | spa_draid_feature_incr(void *arg, dmu_tx_t *tx) | |
6581 | { | |
6582 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
6583 | int draid = (int)(uintptr_t)arg; | |
6584 | ||
6585 | for (int c = 0; c < draid; c++) | |
6586 | spa_feature_incr(spa, SPA_FEATURE_DRAID, tx); | |
6587 | } | |
6588 | ||
34dc7c2f BB |
6589 | /* |
6590 | * Add a device to a storage pool. | |
6591 | */ | |
6592 | int | |
6593 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
6594 | { | |
b2255edc | 6595 | uint64_t txg, ndraid = 0; |
fb5f0bc8 | 6596 | int error; |
34dc7c2f BB |
6597 | vdev_t *rvd = spa->spa_root_vdev; |
6598 | vdev_t *vd, *tvd; | |
6599 | nvlist_t **spares, **l2cache; | |
6600 | uint_t nspares, nl2cache; | |
6601 | ||
572e2857 BB |
6602 | ASSERT(spa_writeable(spa)); |
6603 | ||
34dc7c2f BB |
6604 | txg = spa_vdev_enter(spa); |
6605 | ||
6606 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
6607 | VDEV_ALLOC_ADD)) != 0) | |
6608 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6609 | ||
b128c09f | 6610 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
6611 | |
6612 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
6613 | &nspares) != 0) | |
6614 | nspares = 0; | |
6615 | ||
6616 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
6617 | &nl2cache) != 0) | |
6618 | nl2cache = 0; | |
6619 | ||
b128c09f | 6620 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 6621 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 6622 | |
b128c09f | 6623 | if (vd->vdev_children != 0 && |
b2255edc | 6624 | (error = vdev_create(vd, txg, B_FALSE)) != 0) { |
b128c09f | 6625 | return (spa_vdev_exit(spa, vd, txg, error)); |
b2255edc BB |
6626 | } |
6627 | ||
6628 | /* | |
6629 | * The virtual dRAID spares must be added after vdev tree is created | |
bf169e9f | 6630 | * and the vdev guids are generated. The guid of their associated |
b2255edc BB |
6631 | * dRAID is stored in the config and used when opening the spare. |
6632 | */ | |
6633 | if ((error = vdev_draid_spare_create(nvroot, vd, &ndraid, | |
6634 | rvd->vdev_children)) == 0) { | |
6635 | if (ndraid > 0 && nvlist_lookup_nvlist_array(nvroot, | |
6636 | ZPOOL_CONFIG_SPARES, &spares, &nspares) != 0) | |
6637 | nspares = 0; | |
6638 | } else { | |
6639 | return (spa_vdev_exit(spa, vd, txg, error)); | |
6640 | } | |
34dc7c2f BB |
6641 | |
6642 | /* | |
6643 | * We must validate the spares and l2cache devices after checking the | |
6644 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
6645 | */ | |
b128c09f | 6646 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 6647 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
6648 | |
6649 | /* | |
a1d477c2 MA |
6650 | * If we are in the middle of a device removal, we can only add |
6651 | * devices which match the existing devices in the pool. | |
6652 | * If we are in the middle of a removal, or have some indirect | |
b2255edc | 6653 | * vdevs, we can not add raidz or dRAID top levels. |
34dc7c2f | 6654 | */ |
a1d477c2 MA |
6655 | if (spa->spa_vdev_removal != NULL || |
6656 | spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { | |
6657 | for (int c = 0; c < vd->vdev_children; c++) { | |
6658 | tvd = vd->vdev_child[c]; | |
6659 | if (spa->spa_vdev_removal != NULL && | |
9e052db4 | 6660 | tvd->vdev_ashift != spa->spa_max_ashift) { |
a1d477c2 MA |
6661 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
6662 | } | |
b2255edc BB |
6663 | /* Fail if top level vdev is raidz or a dRAID */ |
6664 | if (vdev_get_nparity(tvd) != 0) | |
a1d477c2 | 6665 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
b2255edc | 6666 | |
a1d477c2 MA |
6667 | /* |
6668 | * Need the top level mirror to be | |
6669 | * a mirror of leaf vdevs only | |
6670 | */ | |
6671 | if (tvd->vdev_ops == &vdev_mirror_ops) { | |
6672 | for (uint64_t cid = 0; | |
6673 | cid < tvd->vdev_children; cid++) { | |
6674 | vdev_t *cvd = tvd->vdev_child[cid]; | |
6675 | if (!cvd->vdev_ops->vdev_op_leaf) { | |
6676 | return (spa_vdev_exit(spa, vd, | |
6677 | txg, EINVAL)); | |
6678 | } | |
6679 | } | |
6680 | } | |
6681 | } | |
6682 | } | |
6683 | ||
1c27024e | 6684 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
6685 | tvd = vd->vdev_child[c]; |
6686 | vdev_remove_child(vd, tvd); | |
93e28d66 | 6687 | tvd->vdev_id = rvd->vdev_children; |
34dc7c2f BB |
6688 | vdev_add_child(rvd, tvd); |
6689 | vdev_config_dirty(tvd); | |
6690 | } | |
6691 | ||
6692 | if (nspares != 0) { | |
6693 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
6694 | ZPOOL_CONFIG_SPARES); | |
6695 | spa_load_spares(spa); | |
6696 | spa->spa_spares.sav_sync = B_TRUE; | |
6697 | } | |
6698 | ||
6699 | if (nl2cache != 0) { | |
6700 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
6701 | ZPOOL_CONFIG_L2CACHE); | |
6702 | spa_load_l2cache(spa); | |
6703 | spa->spa_l2cache.sav_sync = B_TRUE; | |
6704 | } | |
6705 | ||
b2255edc BB |
6706 | /* |
6707 | * We can't increment a feature while holding spa_vdev so we | |
6708 | * have to do it in a synctask. | |
6709 | */ | |
6710 | if (ndraid != 0) { | |
6711 | dmu_tx_t *tx; | |
6712 | ||
6713 | tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); | |
6714 | dsl_sync_task_nowait(spa->spa_dsl_pool, spa_draid_feature_incr, | |
6715 | (void *)(uintptr_t)ndraid, tx); | |
6716 | dmu_tx_commit(tx); | |
6717 | } | |
6718 | ||
34dc7c2f BB |
6719 | /* |
6720 | * We have to be careful when adding new vdevs to an existing pool. | |
6721 | * If other threads start allocating from these vdevs before we | |
6722 | * sync the config cache, and we lose power, then upon reboot we may | |
6723 | * fail to open the pool because there are DVAs that the config cache | |
6724 | * can't translate. Therefore, we first add the vdevs without | |
6725 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
6726 | * and then let spa_config_update() initialize the new metaslabs. | |
6727 | * | |
6728 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
6729 | * if we lose power at any point in this sequence, the remaining | |
6730 | * steps will be completed the next time we load the pool. | |
6731 | */ | |
6732 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
6733 | ||
6734 | mutex_enter(&spa_namespace_lock); | |
6735 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
12fa0466 | 6736 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); |
34dc7c2f BB |
6737 | mutex_exit(&spa_namespace_lock); |
6738 | ||
6739 | return (0); | |
6740 | } | |
6741 | ||
6742 | /* | |
6743 | * Attach a device to a mirror. The arguments are the path to any device | |
6744 | * in the mirror, and the nvroot for the new device. If the path specifies | |
6745 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
6746 | * | |
6747 | * If 'replacing' is specified, the new device is intended to replace the | |
6748 | * existing device; in this case the two devices are made into their own | |
6749 | * mirror using the 'replacing' vdev, which is functionally identical to | |
6750 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
6751 | * extra rules: you can't attach to it after it's been created, and upon | |
6752 | * completion of resilvering, the first disk (the one being replaced) | |
6753 | * is automatically detached. | |
9a49d3f3 BB |
6754 | * |
6755 | * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild) | |
6756 | * should be performed instead of traditional healing reconstruction. From | |
6757 | * an administrators perspective these are both resilver operations. | |
34dc7c2f BB |
6758 | */ |
6759 | int | |
9a49d3f3 BB |
6760 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing, |
6761 | int rebuild) | |
34dc7c2f | 6762 | { |
428870ff | 6763 | uint64_t txg, dtl_max_txg; |
9a49d3f3 | 6764 | vdev_t *rvd = spa->spa_root_vdev; |
34dc7c2f BB |
6765 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
6766 | vdev_ops_t *pvops; | |
b128c09f BB |
6767 | char *oldvdpath, *newvdpath; |
6768 | int newvd_isspare; | |
6769 | int error; | |
34dc7c2f | 6770 | |
572e2857 BB |
6771 | ASSERT(spa_writeable(spa)); |
6772 | ||
34dc7c2f BB |
6773 | txg = spa_vdev_enter(spa); |
6774 | ||
b128c09f | 6775 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6776 | |
d2734cce SD |
6777 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6778 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6779 | error = (spa_has_checkpoint(spa)) ? | |
6780 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6781 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6782 | } | |
6783 | ||
9a49d3f3 BB |
6784 | if (rebuild) { |
6785 | if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD)) | |
6786 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6787 | ||
6788 | if (dsl_scan_resilvering(spa_get_dsl(spa))) | |
6789 | return (spa_vdev_exit(spa, NULL, txg, | |
6790 | ZFS_ERR_RESILVER_IN_PROGRESS)); | |
6791 | } else { | |
6792 | if (vdev_rebuild_active(rvd)) | |
6793 | return (spa_vdev_exit(spa, NULL, txg, | |
6794 | ZFS_ERR_REBUILD_IN_PROGRESS)); | |
6795 | } | |
6796 | ||
9e052db4 | 6797 | if (spa->spa_vdev_removal != NULL) |
a1d477c2 | 6798 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
a1d477c2 | 6799 | |
34dc7c2f BB |
6800 | if (oldvd == NULL) |
6801 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6802 | ||
6803 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
6804 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6805 | ||
6806 | pvd = oldvd->vdev_parent; | |
6807 | ||
6a42939f RY |
6808 | if (spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, |
6809 | VDEV_ALLOC_ATTACH) != 0) | |
34dc7c2f BB |
6810 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
6811 | ||
6812 | if (newrootvd->vdev_children != 1) | |
6813 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6814 | ||
6815 | newvd = newrootvd->vdev_child[0]; | |
6816 | ||
6817 | if (!newvd->vdev_ops->vdev_op_leaf) | |
6818 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6819 | ||
6820 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
6821 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
6822 | ||
6823 | /* | |
c23738c7 | 6824 | * log, dedup and special vdevs should not be replaced by spares. |
34dc7c2f | 6825 | */ |
c23738c7 AH |
6826 | if ((oldvd->vdev_top->vdev_alloc_bias != VDEV_BIAS_NONE || |
6827 | oldvd->vdev_top->vdev_islog) && newvd->vdev_isspare) { | |
34dc7c2f | 6828 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
c23738c7 | 6829 | } |
34dc7c2f | 6830 | |
b2255edc BB |
6831 | /* |
6832 | * A dRAID spare can only replace a child of its parent dRAID vdev. | |
6833 | */ | |
6834 | if (newvd->vdev_ops == &vdev_draid_spare_ops && | |
6835 | oldvd->vdev_top != vdev_draid_spare_get_parent(newvd)) { | |
6836 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6837 | } | |
6838 | ||
9a49d3f3 BB |
6839 | if (rebuild) { |
6840 | /* | |
b2255edc | 6841 | * For rebuilds, the top vdev must support reconstruction |
9a49d3f3 | 6842 | * using only space maps. This means the only allowable |
b2255edc | 6843 | * vdevs types are the root vdev, a mirror, or dRAID. |
9a49d3f3 | 6844 | */ |
b2255edc BB |
6845 | tvd = pvd; |
6846 | if (pvd->vdev_top != NULL) | |
6847 | tvd = pvd->vdev_top; | |
6848 | ||
6849 | if (tvd->vdev_ops != &vdev_mirror_ops && | |
6850 | tvd->vdev_ops != &vdev_root_ops && | |
6851 | tvd->vdev_ops != &vdev_draid_ops) { | |
9a49d3f3 BB |
6852 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
6853 | } | |
6854 | } | |
6855 | ||
34dc7c2f BB |
6856 | if (!replacing) { |
6857 | /* | |
6858 | * For attach, the only allowable parent is a mirror or the root | |
6859 | * vdev. | |
6860 | */ | |
6861 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6862 | pvd->vdev_ops != &vdev_root_ops) | |
6863 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6864 | ||
6865 | pvops = &vdev_mirror_ops; | |
6866 | } else { | |
6867 | /* | |
6868 | * Active hot spares can only be replaced by inactive hot | |
6869 | * spares. | |
6870 | */ | |
6871 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 6872 | oldvd->vdev_isspare && |
34dc7c2f BB |
6873 | !spa_has_spare(spa, newvd->vdev_guid)) |
6874 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6875 | ||
6876 | /* | |
6877 | * If the source is a hot spare, and the parent isn't already a | |
6878 | * spare, then we want to create a new hot spare. Otherwise, we | |
6879 | * want to create a replacing vdev. The user is not allowed to | |
6880 | * attach to a spared vdev child unless the 'isspare' state is | |
6881 | * the same (spare replaces spare, non-spare replaces | |
6882 | * non-spare). | |
6883 | */ | |
572e2857 BB |
6884 | if (pvd->vdev_ops == &vdev_replacing_ops && |
6885 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 6886 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6887 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
6888 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 6889 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6890 | } |
6891 | ||
6892 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
6893 | pvops = &vdev_spare_ops; |
6894 | else | |
6895 | pvops = &vdev_replacing_ops; | |
6896 | } | |
6897 | ||
6898 | /* | |
9babb374 | 6899 | * Make sure the new device is big enough. |
34dc7c2f | 6900 | */ |
9babb374 | 6901 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
6902 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
6903 | ||
6904 | /* | |
6905 | * The new device cannot have a higher alignment requirement | |
6906 | * than the top-level vdev. | |
6907 | */ | |
6908 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
9a49d3f3 | 6909 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
34dc7c2f BB |
6910 | |
6911 | /* | |
6912 | * If this is an in-place replacement, update oldvd's path and devid | |
6913 | * to make it distinguishable from newvd, and unopenable from now on. | |
6914 | */ | |
6915 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
6916 | spa_strfree(oldvd->vdev_path); | |
6917 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
79c76d5b | 6918 | KM_SLEEP); |
c9e319fa JL |
6919 | (void) snprintf(oldvd->vdev_path, strlen(newvd->vdev_path) + 5, |
6920 | "%s/%s", newvd->vdev_path, "old"); | |
34dc7c2f BB |
6921 | if (oldvd->vdev_devid != NULL) { |
6922 | spa_strfree(oldvd->vdev_devid); | |
6923 | oldvd->vdev_devid = NULL; | |
6924 | } | |
6925 | } | |
6926 | ||
6927 | /* | |
6928 | * If the parent is not a mirror, or if we're replacing, insert the new | |
6929 | * mirror/replacing/spare vdev above oldvd. | |
6930 | */ | |
6931 | if (pvd->vdev_ops != pvops) | |
6932 | pvd = vdev_add_parent(oldvd, pvops); | |
6933 | ||
6934 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
6935 | ASSERT(pvd->vdev_ops == pvops); | |
6936 | ASSERT(oldvd->vdev_parent == pvd); | |
6937 | ||
6938 | /* | |
6939 | * Extract the new device from its root and add it to pvd. | |
6940 | */ | |
6941 | vdev_remove_child(newrootvd, newvd); | |
6942 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 6943 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
6944 | vdev_add_child(pvd, newvd); |
6945 | ||
6d82f98c IH |
6946 | /* |
6947 | * Reevaluate the parent vdev state. | |
6948 | */ | |
6949 | vdev_propagate_state(pvd); | |
6950 | ||
34dc7c2f BB |
6951 | tvd = newvd->vdev_top; |
6952 | ASSERT(pvd->vdev_top == tvd); | |
6953 | ASSERT(tvd->vdev_parent == rvd); | |
6954 | ||
6955 | vdev_config_dirty(tvd); | |
6956 | ||
6957 | /* | |
428870ff BB |
6958 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
6959 | * for any dmu_sync-ed blocks. It will propagate upward when | |
6960 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 6961 | */ |
428870ff | 6962 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 6963 | |
9a49d3f3 BB |
6964 | vdev_dtl_dirty(newvd, DTL_MISSING, |
6965 | TXG_INITIAL, dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 6966 | |
9babb374 | 6967 | if (newvd->vdev_isspare) { |
34dc7c2f | 6968 | spa_spare_activate(newvd); |
12fa0466 | 6969 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); |
9babb374 BB |
6970 | } |
6971 | ||
b128c09f BB |
6972 | oldvdpath = spa_strdup(oldvd->vdev_path); |
6973 | newvdpath = spa_strdup(newvd->vdev_path); | |
6974 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
6975 | |
6976 | /* | |
6977 | * Mark newvd's DTL dirty in this txg. | |
6978 | */ | |
6979 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
6980 | ||
428870ff | 6981 | /* |
9a49d3f3 BB |
6982 | * Schedule the resilver or rebuild to restart in the future. We do |
6983 | * this to ensure that dmu_sync-ed blocks have been stitched into the | |
6984 | * respective datasets. | |
428870ff | 6985 | */ |
9a49d3f3 BB |
6986 | if (rebuild) { |
6987 | newvd->vdev_rebuild_txg = txg; | |
6988 | ||
6989 | vdev_rebuild(tvd); | |
6990 | } else { | |
6991 | newvd->vdev_resilver_txg = txg; | |
6992 | ||
6993 | if (dsl_scan_resilvering(spa_get_dsl(spa)) && | |
6994 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) { | |
6995 | vdev_defer_resilver(newvd); | |
6996 | } else { | |
6997 | dsl_scan_restart_resilver(spa->spa_dsl_pool, | |
6998 | dtl_max_txg); | |
6999 | } | |
7000 | } | |
428870ff | 7001 | |
fb390aaf | 7002 | if (spa->spa_bootfs) |
12fa0466 | 7003 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); |
fb390aaf | 7004 | |
12fa0466 | 7005 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); |
fb390aaf | 7006 | |
428870ff BB |
7007 | /* |
7008 | * Commit the config | |
7009 | */ | |
7010 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 7011 | |
6f1ffb06 | 7012 | spa_history_log_internal(spa, "vdev attach", NULL, |
428870ff | 7013 | "%s vdev=%s %s vdev=%s", |
45d1cae3 BB |
7014 | replacing && newvd_isspare ? "spare in" : |
7015 | replacing ? "replace" : "attach", newvdpath, | |
7016 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
7017 | |
7018 | spa_strfree(oldvdpath); | |
7019 | spa_strfree(newvdpath); | |
7020 | ||
34dc7c2f BB |
7021 | return (0); |
7022 | } | |
7023 | ||
7024 | /* | |
7025 | * Detach a device from a mirror or replacing vdev. | |
d3cc8b15 | 7026 | * |
34dc7c2f BB |
7027 | * If 'replace_done' is specified, only detach if the parent |
7028 | * is a replacing vdev. | |
7029 | */ | |
7030 | int | |
fb5f0bc8 | 7031 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
7032 | { |
7033 | uint64_t txg; | |
fb5f0bc8 | 7034 | int error; |
2a8ba608 | 7035 | vdev_t *rvd __maybe_unused = spa->spa_root_vdev; |
34dc7c2f BB |
7036 | vdev_t *vd, *pvd, *cvd, *tvd; |
7037 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 7038 | uint64_t unspare_guid = 0; |
428870ff | 7039 | char *vdpath; |
1c27024e | 7040 | |
572e2857 BB |
7041 | ASSERT(spa_writeable(spa)); |
7042 | ||
9a49d3f3 | 7043 | txg = spa_vdev_detach_enter(spa, guid); |
34dc7c2f | 7044 | |
b128c09f | 7045 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 7046 | |
d2734cce SD |
7047 | /* |
7048 | * Besides being called directly from the userland through the | |
7049 | * ioctl interface, spa_vdev_detach() can be potentially called | |
7050 | * at the end of spa_vdev_resilver_done(). | |
7051 | * | |
7052 | * In the regular case, when we have a checkpoint this shouldn't | |
7053 | * happen as we never empty the DTLs of a vdev during the scrub | |
7054 | * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() | |
7055 | * should never get here when we have a checkpoint. | |
7056 | * | |
7057 | * That said, even in a case when we checkpoint the pool exactly | |
7058 | * as spa_vdev_resilver_done() calls this function everything | |
7059 | * should be fine as the resilver will return right away. | |
7060 | */ | |
7061 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7062 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7063 | error = (spa_has_checkpoint(spa)) ? | |
7064 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7065 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7066 | } | |
7067 | ||
34dc7c2f BB |
7068 | if (vd == NULL) |
7069 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
7070 | ||
7071 | if (!vd->vdev_ops->vdev_op_leaf) | |
7072 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
7073 | ||
7074 | pvd = vd->vdev_parent; | |
7075 | ||
fb5f0bc8 BB |
7076 | /* |
7077 | * If the parent/child relationship is not as expected, don't do it. | |
7078 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
7079 | * vdev that's replacing B with C. The user's intent in replacing | |
7080 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
7081 | * the replace by detaching C, the expected behavior is to end up | |
7082 | * M(A,B). But suppose that right after deciding to detach C, | |
7083 | * the replacement of B completes. We would have M(A,C), and then | |
7084 | * ask to detach C, which would leave us with just A -- not what | |
7085 | * the user wanted. To prevent this, we make sure that the | |
7086 | * parent/child relationship hasn't changed -- in this example, | |
7087 | * that C's parent is still the replacing vdev R. | |
7088 | */ | |
7089 | if (pvd->vdev_guid != pguid && pguid != 0) | |
7090 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
7091 | ||
34dc7c2f | 7092 | /* |
572e2857 | 7093 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 7094 | */ |
572e2857 BB |
7095 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
7096 | pvd->vdev_ops != &vdev_spare_ops) | |
7097 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
7098 | |
7099 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
7100 | spa_version(spa) >= SPA_VERSION_SPARES); | |
7101 | ||
7102 | /* | |
7103 | * Only mirror, replacing, and spare vdevs support detach. | |
7104 | */ | |
7105 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
7106 | pvd->vdev_ops != &vdev_mirror_ops && | |
7107 | pvd->vdev_ops != &vdev_spare_ops) | |
7108 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
7109 | ||
7110 | /* | |
fb5f0bc8 BB |
7111 | * If this device has the only valid copy of some data, |
7112 | * we cannot safely detach it. | |
34dc7c2f | 7113 | */ |
fb5f0bc8 | 7114 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
7115 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
7116 | ||
fb5f0bc8 | 7117 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 7118 | |
b128c09f BB |
7119 | /* |
7120 | * If we are detaching the second disk from a replacing vdev, then | |
7121 | * check to see if we changed the original vdev's path to have "/old" | |
7122 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
7123 | */ | |
572e2857 BB |
7124 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
7125 | vd->vdev_path != NULL) { | |
7126 | size_t len = strlen(vd->vdev_path); | |
7127 | ||
1c27024e | 7128 | for (int c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
7129 | cvd = pvd->vdev_child[c]; |
7130 | ||
7131 | if (cvd == vd || cvd->vdev_path == NULL) | |
7132 | continue; | |
7133 | ||
7134 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
7135 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
7136 | spa_strfree(cvd->vdev_path); | |
7137 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
7138 | break; | |
7139 | } | |
b128c09f BB |
7140 | } |
7141 | } | |
7142 | ||
34dc7c2f | 7143 | /* |
b2255edc BB |
7144 | * If we are detaching the original disk from a normal spare, then it |
7145 | * implies that the spare should become a real disk, and be removed | |
7146 | * from the active spare list for the pool. dRAID spares on the | |
7147 | * other hand are coupled to the pool and thus should never be removed | |
7148 | * from the spares list. | |
34dc7c2f | 7149 | */ |
b2255edc BB |
7150 | if (pvd->vdev_ops == &vdev_spare_ops && vd->vdev_id == 0) { |
7151 | vdev_t *last_cvd = pvd->vdev_child[pvd->vdev_children - 1]; | |
7152 | ||
7153 | if (last_cvd->vdev_isspare && | |
7154 | last_cvd->vdev_ops != &vdev_draid_spare_ops) { | |
7155 | unspare = B_TRUE; | |
7156 | } | |
7157 | } | |
34dc7c2f BB |
7158 | |
7159 | /* | |
7160 | * Erase the disk labels so the disk can be used for other things. | |
7161 | * This must be done after all other error cases are handled, | |
7162 | * but before we disembowel vd (so we can still do I/O to it). | |
7163 | * But if we can't do it, don't treat the error as fatal -- | |
7164 | * it may be that the unwritability of the disk is the reason | |
7165 | * it's being detached! | |
7166 | */ | |
6a42939f | 7167 | (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); |
34dc7c2f BB |
7168 | |
7169 | /* | |
7170 | * Remove vd from its parent and compact the parent's children. | |
7171 | */ | |
7172 | vdev_remove_child(pvd, vd); | |
7173 | vdev_compact_children(pvd); | |
7174 | ||
7175 | /* | |
7176 | * Remember one of the remaining children so we can get tvd below. | |
7177 | */ | |
572e2857 | 7178 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
7179 | |
7180 | /* | |
7181 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
7182 | * do it now, marking the vdev as no longer a spare in the process. |
7183 | * We must do this before vdev_remove_parent(), because that can | |
7184 | * change the GUID if it creates a new toplevel GUID. For a similar | |
7185 | * reason, we must remove the spare now, in the same txg as the detach; | |
7186 | * otherwise someone could attach a new sibling, change the GUID, and | |
7187 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
7188 | */ |
7189 | if (unspare) { | |
7190 | ASSERT(cvd->vdev_isspare); | |
7191 | spa_spare_remove(cvd); | |
7192 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 7193 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 7194 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
7195 | } |
7196 | ||
428870ff BB |
7197 | /* |
7198 | * If the parent mirror/replacing vdev only has one child, | |
7199 | * the parent is no longer needed. Remove it from the tree. | |
7200 | */ | |
572e2857 BB |
7201 | if (pvd->vdev_children == 1) { |
7202 | if (pvd->vdev_ops == &vdev_spare_ops) | |
7203 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 7204 | vdev_remove_parent(cvd); |
572e2857 BB |
7205 | } |
7206 | ||
428870ff BB |
7207 | /* |
7208 | * We don't set tvd until now because the parent we just removed | |
7209 | * may have been the previous top-level vdev. | |
7210 | */ | |
7211 | tvd = cvd->vdev_top; | |
7212 | ASSERT(tvd->vdev_parent == rvd); | |
7213 | ||
7214 | /* | |
7215 | * Reevaluate the parent vdev state. | |
7216 | */ | |
7217 | vdev_propagate_state(cvd); | |
7218 | ||
7219 | /* | |
7220 | * If the 'autoexpand' property is set on the pool then automatically | |
7221 | * try to expand the size of the pool. For example if the device we | |
7222 | * just detached was smaller than the others, it may be possible to | |
7223 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
7224 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
7225 | */ | |
7226 | if (spa->spa_autoexpand) { | |
7227 | vdev_reopen(tvd); | |
7228 | vdev_expand(tvd, txg); | |
7229 | } | |
7230 | ||
7231 | vdev_config_dirty(tvd); | |
7232 | ||
7233 | /* | |
7234 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
7235 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
7236 | * But first make sure we're not on any *other* txg's DTL list, to | |
7237 | * prevent vd from being accessed after it's freed. | |
7238 | */ | |
b6ca6193 | 7239 | vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none"); |
1c27024e | 7240 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff BB |
7241 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
7242 | vd->vdev_detached = B_TRUE; | |
7243 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
7244 | ||
12fa0466 | 7245 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); |
e60e158e | 7246 | spa_notify_waiters(spa); |
428870ff | 7247 | |
572e2857 BB |
7248 | /* hang on to the spa before we release the lock */ |
7249 | spa_open_ref(spa, FTAG); | |
7250 | ||
428870ff BB |
7251 | error = spa_vdev_exit(spa, vd, txg, 0); |
7252 | ||
6f1ffb06 | 7253 | spa_history_log_internal(spa, "detach", NULL, |
428870ff BB |
7254 | "vdev=%s", vdpath); |
7255 | spa_strfree(vdpath); | |
7256 | ||
7257 | /* | |
7258 | * If this was the removal of the original device in a hot spare vdev, | |
7259 | * then we want to go through and remove the device from the hot spare | |
7260 | * list of every other pool. | |
7261 | */ | |
7262 | if (unspare) { | |
572e2857 BB |
7263 | spa_t *altspa = NULL; |
7264 | ||
428870ff | 7265 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
7266 | while ((altspa = spa_next(altspa)) != NULL) { |
7267 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
7268 | altspa == spa) | |
428870ff | 7269 | continue; |
572e2857 BB |
7270 | |
7271 | spa_open_ref(altspa, FTAG); | |
428870ff | 7272 | mutex_exit(&spa_namespace_lock); |
572e2857 | 7273 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 7274 | mutex_enter(&spa_namespace_lock); |
572e2857 | 7275 | spa_close(altspa, FTAG); |
428870ff BB |
7276 | } |
7277 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
7278 | |
7279 | /* search the rest of the vdevs for spares to remove */ | |
7280 | spa_vdev_resilver_done(spa); | |
428870ff BB |
7281 | } |
7282 | ||
572e2857 BB |
7283 | /* all done with the spa; OK to release */ |
7284 | mutex_enter(&spa_namespace_lock); | |
7285 | spa_close(spa, FTAG); | |
7286 | mutex_exit(&spa_namespace_lock); | |
7287 | ||
428870ff BB |
7288 | return (error); |
7289 | } | |
7290 | ||
c10d37dd GW |
7291 | static int |
7292 | spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7293 | list_t *vd_list) | |
619f0976 | 7294 | { |
c10d37dd GW |
7295 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7296 | ||
619f0976 GW |
7297 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); |
7298 | ||
7299 | /* Look up vdev and ensure it's a leaf. */ | |
7300 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7301 | if (vd == NULL || vd->vdev_detached) { | |
7302 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7303 | return (SET_ERROR(ENODEV)); |
7304 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7305 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7306 | return (SET_ERROR(EINVAL)); |
7307 | } else if (!vdev_writeable(vd)) { | |
7308 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7309 | return (SET_ERROR(EROFS)); |
7310 | } | |
7311 | mutex_enter(&vd->vdev_initialize_lock); | |
7312 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7313 | ||
7314 | /* | |
7315 | * When we activate an initialize action we check to see | |
7316 | * if the vdev_initialize_thread is NULL. We do this instead | |
7317 | * of using the vdev_initialize_state since there might be | |
7318 | * a previous initialization process which has completed but | |
7319 | * the thread is not exited. | |
7320 | */ | |
1b939560 | 7321 | if (cmd_type == POOL_INITIALIZE_START && |
619f0976 GW |
7322 | (vd->vdev_initialize_thread != NULL || |
7323 | vd->vdev_top->vdev_removing)) { | |
7324 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7325 | return (SET_ERROR(EBUSY)); |
7326 | } else if (cmd_type == POOL_INITIALIZE_CANCEL && | |
7327 | (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && | |
7328 | vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { | |
7329 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7330 | return (SET_ERROR(ESRCH)); |
7331 | } else if (cmd_type == POOL_INITIALIZE_SUSPEND && | |
7332 | vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { | |
7333 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7334 | return (SET_ERROR(ESRCH)); |
7335 | } | |
7336 | ||
7337 | switch (cmd_type) { | |
1b939560 | 7338 | case POOL_INITIALIZE_START: |
619f0976 GW |
7339 | vdev_initialize(vd); |
7340 | break; | |
7341 | case POOL_INITIALIZE_CANCEL: | |
c10d37dd | 7342 | vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); |
619f0976 GW |
7343 | break; |
7344 | case POOL_INITIALIZE_SUSPEND: | |
c10d37dd | 7345 | vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); |
619f0976 GW |
7346 | break; |
7347 | default: | |
7348 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7349 | } | |
7350 | mutex_exit(&vd->vdev_initialize_lock); | |
7351 | ||
c10d37dd GW |
7352 | return (0); |
7353 | } | |
7354 | ||
7355 | int | |
7356 | spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, | |
7357 | nvlist_t *vdev_errlist) | |
7358 | { | |
7359 | int total_errors = 0; | |
7360 | list_t vd_list; | |
7361 | ||
7362 | list_create(&vd_list, sizeof (vdev_t), | |
7363 | offsetof(vdev_t, vdev_initialize_node)); | |
7364 | ||
7365 | /* | |
7366 | * We hold the namespace lock through the whole function | |
7367 | * to prevent any changes to the pool while we're starting or | |
7368 | * stopping initialization. The config and state locks are held so that | |
7369 | * we can properly assess the vdev state before we commit to | |
7370 | * the initializing operation. | |
7371 | */ | |
7372 | mutex_enter(&spa_namespace_lock); | |
7373 | ||
7374 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7375 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7376 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7377 | ||
7378 | int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, | |
7379 | &vd_list); | |
7380 | if (error != 0) { | |
7381 | char guid_as_str[MAXNAMELEN]; | |
7382 | ||
7383 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7384 | "%llu", (unsigned long long)vdev_guid); | |
7385 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7386 | total_errors++; | |
7387 | } | |
7388 | } | |
7389 | ||
7390 | /* Wait for all initialize threads to stop. */ | |
7391 | vdev_initialize_stop_wait(spa, &vd_list); | |
7392 | ||
619f0976 GW |
7393 | /* Sync out the initializing state */ |
7394 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7395 | mutex_exit(&spa_namespace_lock); | |
7396 | ||
c10d37dd | 7397 | list_destroy(&vd_list); |
619f0976 | 7398 | |
c10d37dd GW |
7399 | return (total_errors); |
7400 | } | |
619f0976 | 7401 | |
1b939560 BB |
7402 | static int |
7403 | spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7404 | uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list) | |
7405 | { | |
7406 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7407 | ||
7408 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
7409 | ||
7410 | /* Look up vdev and ensure it's a leaf. */ | |
7411 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7412 | if (vd == NULL || vd->vdev_detached) { | |
7413 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7414 | return (SET_ERROR(ENODEV)); | |
7415 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7416 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7417 | return (SET_ERROR(EINVAL)); | |
7418 | } else if (!vdev_writeable(vd)) { | |
7419 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7420 | return (SET_ERROR(EROFS)); | |
7421 | } else if (!vd->vdev_has_trim) { | |
7422 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7423 | return (SET_ERROR(EOPNOTSUPP)); | |
7424 | } else if (secure && !vd->vdev_has_securetrim) { | |
7425 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7426 | return (SET_ERROR(EOPNOTSUPP)); | |
7427 | } | |
7428 | mutex_enter(&vd->vdev_trim_lock); | |
7429 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7430 | ||
7431 | /* | |
7432 | * When we activate a TRIM action we check to see if the | |
7433 | * vdev_trim_thread is NULL. We do this instead of using the | |
7434 | * vdev_trim_state since there might be a previous TRIM process | |
7435 | * which has completed but the thread is not exited. | |
7436 | */ | |
7437 | if (cmd_type == POOL_TRIM_START && | |
7438 | (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing)) { | |
7439 | mutex_exit(&vd->vdev_trim_lock); | |
7440 | return (SET_ERROR(EBUSY)); | |
7441 | } else if (cmd_type == POOL_TRIM_CANCEL && | |
7442 | (vd->vdev_trim_state != VDEV_TRIM_ACTIVE && | |
7443 | vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) { | |
7444 | mutex_exit(&vd->vdev_trim_lock); | |
7445 | return (SET_ERROR(ESRCH)); | |
7446 | } else if (cmd_type == POOL_TRIM_SUSPEND && | |
7447 | vd->vdev_trim_state != VDEV_TRIM_ACTIVE) { | |
7448 | mutex_exit(&vd->vdev_trim_lock); | |
7449 | return (SET_ERROR(ESRCH)); | |
7450 | } | |
7451 | ||
7452 | switch (cmd_type) { | |
7453 | case POOL_TRIM_START: | |
7454 | vdev_trim(vd, rate, partial, secure); | |
7455 | break; | |
7456 | case POOL_TRIM_CANCEL: | |
7457 | vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list); | |
7458 | break; | |
7459 | case POOL_TRIM_SUSPEND: | |
7460 | vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list); | |
7461 | break; | |
7462 | default: | |
7463 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7464 | } | |
7465 | mutex_exit(&vd->vdev_trim_lock); | |
7466 | ||
7467 | return (0); | |
7468 | } | |
7469 | ||
7470 | /* | |
7471 | * Initiates a manual TRIM for the requested vdevs. This kicks off individual | |
7472 | * TRIM threads for each child vdev. These threads pass over all of the free | |
7473 | * space in the vdev's metaslabs and issues TRIM commands for that space. | |
7474 | */ | |
7475 | int | |
7476 | spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate, | |
7477 | boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist) | |
7478 | { | |
7479 | int total_errors = 0; | |
7480 | list_t vd_list; | |
7481 | ||
7482 | list_create(&vd_list, sizeof (vdev_t), | |
7483 | offsetof(vdev_t, vdev_trim_node)); | |
7484 | ||
7485 | /* | |
7486 | * We hold the namespace lock through the whole function | |
7487 | * to prevent any changes to the pool while we're starting or | |
7488 | * stopping TRIM. The config and state locks are held so that | |
7489 | * we can properly assess the vdev state before we commit to | |
7490 | * the TRIM operation. | |
7491 | */ | |
7492 | mutex_enter(&spa_namespace_lock); | |
7493 | ||
7494 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7495 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7496 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7497 | ||
7498 | int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type, | |
7499 | rate, partial, secure, &vd_list); | |
7500 | if (error != 0) { | |
7501 | char guid_as_str[MAXNAMELEN]; | |
7502 | ||
7503 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7504 | "%llu", (unsigned long long)vdev_guid); | |
7505 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7506 | total_errors++; | |
7507 | } | |
7508 | } | |
7509 | ||
7510 | /* Wait for all TRIM threads to stop. */ | |
7511 | vdev_trim_stop_wait(spa, &vd_list); | |
7512 | ||
7513 | /* Sync out the TRIM state */ | |
7514 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7515 | mutex_exit(&spa_namespace_lock); | |
7516 | ||
7517 | list_destroy(&vd_list); | |
7518 | ||
7519 | return (total_errors); | |
7520 | } | |
7521 | ||
428870ff BB |
7522 | /* |
7523 | * Split a set of devices from their mirrors, and create a new pool from them. | |
7524 | */ | |
7525 | int | |
a926aab9 | 7526 | spa_vdev_split_mirror(spa_t *spa, const char *newname, nvlist_t *config, |
428870ff BB |
7527 | nvlist_t *props, boolean_t exp) |
7528 | { | |
7529 | int error = 0; | |
7530 | uint64_t txg, *glist; | |
7531 | spa_t *newspa; | |
7532 | uint_t c, children, lastlog; | |
7533 | nvlist_t **child, *nvl, *tmp; | |
7534 | dmu_tx_t *tx; | |
7535 | char *altroot = NULL; | |
7536 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
7537 | boolean_t activate_slog; | |
7538 | ||
572e2857 | 7539 | ASSERT(spa_writeable(spa)); |
428870ff BB |
7540 | |
7541 | txg = spa_vdev_enter(spa); | |
7542 | ||
d2734cce SD |
7543 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7544 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7545 | error = (spa_has_checkpoint(spa)) ? | |
7546 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7547 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7548 | } | |
7549 | ||
428870ff BB |
7550 | /* clear the log and flush everything up to now */ |
7551 | activate_slog = spa_passivate_log(spa); | |
7552 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
a1d477c2 | 7553 | error = spa_reset_logs(spa); |
428870ff BB |
7554 | txg = spa_vdev_config_enter(spa); |
7555 | ||
7556 | if (activate_slog) | |
7557 | spa_activate_log(spa); | |
7558 | ||
7559 | if (error != 0) | |
7560 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7561 | ||
7562 | /* check new spa name before going any further */ | |
7563 | if (spa_lookup(newname) != NULL) | |
7564 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
7565 | ||
7566 | /* | |
7567 | * scan through all the children to ensure they're all mirrors | |
7568 | */ | |
7569 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
7570 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
7571 | &children) != 0) | |
7572 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7573 | ||
7574 | /* first, check to ensure we've got the right child count */ | |
7575 | rvd = spa->spa_root_vdev; | |
7576 | lastlog = 0; | |
7577 | for (c = 0; c < rvd->vdev_children; c++) { | |
7578 | vdev_t *vd = rvd->vdev_child[c]; | |
7579 | ||
7580 | /* don't count the holes & logs as children */ | |
1b664952 GA |
7581 | if (vd->vdev_islog || (vd->vdev_ops != &vdev_indirect_ops && |
7582 | !vdev_is_concrete(vd))) { | |
428870ff BB |
7583 | if (lastlog == 0) |
7584 | lastlog = c; | |
7585 | continue; | |
7586 | } | |
7587 | ||
7588 | lastlog = 0; | |
7589 | } | |
7590 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
7591 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7592 | ||
7593 | /* next, ensure no spare or cache devices are part of the split */ | |
7594 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
7595 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
7596 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7597 | ||
79c76d5b BB |
7598 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); |
7599 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
428870ff BB |
7600 | |
7601 | /* then, loop over each vdev and validate it */ | |
7602 | for (c = 0; c < children; c++) { | |
7603 | uint64_t is_hole = 0; | |
7604 | ||
7605 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
7606 | &is_hole); | |
7607 | ||
7608 | if (is_hole != 0) { | |
7609 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
7610 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
7611 | continue; | |
7612 | } else { | |
2e528b49 | 7613 | error = SET_ERROR(EINVAL); |
428870ff BB |
7614 | break; |
7615 | } | |
7616 | } | |
7617 | ||
1b664952 GA |
7618 | /* deal with indirect vdevs */ |
7619 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ops == | |
7620 | &vdev_indirect_ops) | |
7621 | continue; | |
7622 | ||
428870ff BB |
7623 | /* which disk is going to be split? */ |
7624 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
7625 | &glist[c]) != 0) { | |
2e528b49 | 7626 | error = SET_ERROR(EINVAL); |
428870ff BB |
7627 | break; |
7628 | } | |
7629 | ||
7630 | /* look it up in the spa */ | |
7631 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
7632 | if (vml[c] == NULL) { | |
2e528b49 | 7633 | error = SET_ERROR(ENODEV); |
428870ff BB |
7634 | break; |
7635 | } | |
7636 | ||
7637 | /* make sure there's nothing stopping the split */ | |
7638 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
7639 | vml[c]->vdev_islog || | |
a1d477c2 | 7640 | !vdev_is_concrete(vml[c]) || |
428870ff BB |
7641 | vml[c]->vdev_isspare || |
7642 | vml[c]->vdev_isl2cache || | |
7643 | !vdev_writeable(vml[c]) || | |
7644 | vml[c]->vdev_children != 0 || | |
7645 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
7646 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
2e528b49 | 7647 | error = SET_ERROR(EINVAL); |
428870ff BB |
7648 | break; |
7649 | } | |
7650 | ||
733b5722 RS |
7651 | if (vdev_dtl_required(vml[c]) || |
7652 | vdev_resilver_needed(vml[c], NULL, NULL)) { | |
2e528b49 | 7653 | error = SET_ERROR(EBUSY); |
428870ff BB |
7654 | break; |
7655 | } | |
7656 | ||
7657 | /* we need certain info from the top level */ | |
65ad5d11 AJ |
7658 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, |
7659 | vml[c]->vdev_top->vdev_ms_array); | |
7660 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
7661 | vml[c]->vdev_top->vdev_ms_shift); | |
7662 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
7663 | vml[c]->vdev_top->vdev_asize); | |
7664 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
7665 | vml[c]->vdev_top->vdev_ashift); | |
e0ab3ab5 JS |
7666 | |
7667 | /* transfer per-vdev ZAPs */ | |
7668 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
7669 | VERIFY0(nvlist_add_uint64(child[c], | |
7670 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
7671 | ||
7672 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
7673 | VERIFY0(nvlist_add_uint64(child[c], | |
7674 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
7675 | vml[c]->vdev_parent->vdev_top_zap)); | |
428870ff BB |
7676 | } |
7677 | ||
7678 | if (error != 0) { | |
7679 | kmem_free(vml, children * sizeof (vdev_t *)); | |
7680 | kmem_free(glist, children * sizeof (uint64_t)); | |
7681 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7682 | } | |
7683 | ||
7684 | /* stop writers from using the disks */ | |
7685 | for (c = 0; c < children; c++) { | |
7686 | if (vml[c] != NULL) | |
7687 | vml[c]->vdev_offline = B_TRUE; | |
7688 | } | |
7689 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
7690 | |
7691 | /* | |
428870ff BB |
7692 | * Temporarily record the splitting vdevs in the spa config. This |
7693 | * will disappear once the config is regenerated. | |
34dc7c2f | 7694 | */ |
65ad5d11 AJ |
7695 | nvl = fnvlist_alloc(); |
7696 | fnvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, glist, children); | |
428870ff | 7697 | kmem_free(glist, children * sizeof (uint64_t)); |
34dc7c2f | 7698 | |
428870ff | 7699 | mutex_enter(&spa->spa_props_lock); |
65ad5d11 | 7700 | fnvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, nvl); |
428870ff BB |
7701 | mutex_exit(&spa->spa_props_lock); |
7702 | spa->spa_config_splitting = nvl; | |
7703 | vdev_config_dirty(spa->spa_root_vdev); | |
7704 | ||
7705 | /* configure and create the new pool */ | |
65ad5d11 AJ |
7706 | fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname); |
7707 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
7708 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE); | |
7709 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa)); | |
7710 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, spa->spa_config_txg); | |
7711 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
7712 | spa_generate_guid(NULL)); | |
e0ab3ab5 | 7713 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); |
428870ff BB |
7714 | (void) nvlist_lookup_string(props, |
7715 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 7716 | |
428870ff BB |
7717 | /* add the new pool to the namespace */ |
7718 | newspa = spa_add(newname, config, altroot); | |
e0ab3ab5 | 7719 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7720 | newspa->spa_config_txg = spa->spa_config_txg; |
7721 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
7722 | ||
7723 | /* release the spa config lock, retaining the namespace lock */ | |
7724 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
7725 | ||
7726 | if (zio_injection_enabled) | |
7727 | zio_handle_panic_injection(spa, FTAG, 1); | |
7728 | ||
7729 | spa_activate(newspa, spa_mode_global); | |
7730 | spa_async_suspend(newspa); | |
7731 | ||
c10d37dd | 7732 | /* |
1b939560 BB |
7733 | * Temporarily stop the initializing and TRIM activity. We set the |
7734 | * state to ACTIVE so that we know to resume initializing or TRIM | |
7735 | * once the split has completed. | |
c10d37dd | 7736 | */ |
1b939560 BB |
7737 | list_t vd_initialize_list; |
7738 | list_create(&vd_initialize_list, sizeof (vdev_t), | |
c10d37dd GW |
7739 | offsetof(vdev_t, vdev_initialize_node)); |
7740 | ||
1b939560 BB |
7741 | list_t vd_trim_list; |
7742 | list_create(&vd_trim_list, sizeof (vdev_t), | |
7743 | offsetof(vdev_t, vdev_trim_node)); | |
7744 | ||
619f0976 | 7745 | for (c = 0; c < children; c++) { |
1b664952 | 7746 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
619f0976 | 7747 | mutex_enter(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7748 | vdev_initialize_stop(vml[c], |
7749 | VDEV_INITIALIZE_ACTIVE, &vd_initialize_list); | |
619f0976 | 7750 | mutex_exit(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7751 | |
7752 | mutex_enter(&vml[c]->vdev_trim_lock); | |
7753 | vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list); | |
7754 | mutex_exit(&vml[c]->vdev_trim_lock); | |
619f0976 GW |
7755 | } |
7756 | } | |
1b939560 BB |
7757 | |
7758 | vdev_initialize_stop_wait(spa, &vd_initialize_list); | |
7759 | vdev_trim_stop_wait(spa, &vd_trim_list); | |
7760 | ||
7761 | list_destroy(&vd_initialize_list); | |
7762 | list_destroy(&vd_trim_list); | |
619f0976 | 7763 | |
6cb8e530 | 7764 | newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; |
8b27e08e | 7765 | newspa->spa_is_splitting = B_TRUE; |
6cb8e530 | 7766 | |
428870ff | 7767 | /* create the new pool from the disks of the original pool */ |
6cb8e530 | 7768 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); |
428870ff BB |
7769 | if (error) |
7770 | goto out; | |
7771 | ||
7772 | /* if that worked, generate a real config for the new pool */ | |
7773 | if (newspa->spa_root_vdev != NULL) { | |
65ad5d11 AJ |
7774 | newspa->spa_config_splitting = fnvlist_alloc(); |
7775 | fnvlist_add_uint64(newspa->spa_config_splitting, | |
7776 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)); | |
428870ff BB |
7777 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, |
7778 | B_TRUE)); | |
9babb374 | 7779 | } |
34dc7c2f | 7780 | |
428870ff BB |
7781 | /* set the props */ |
7782 | if (props != NULL) { | |
7783 | spa_configfile_set(newspa, props, B_FALSE); | |
7784 | error = spa_prop_set(newspa, props); | |
7785 | if (error) | |
7786 | goto out; | |
7787 | } | |
34dc7c2f | 7788 | |
428870ff BB |
7789 | /* flush everything */ |
7790 | txg = spa_vdev_config_enter(newspa); | |
7791 | vdev_config_dirty(newspa->spa_root_vdev); | |
7792 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 7793 | |
428870ff BB |
7794 | if (zio_injection_enabled) |
7795 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 7796 | |
428870ff | 7797 | spa_async_resume(newspa); |
34dc7c2f | 7798 | |
428870ff BB |
7799 | /* finally, update the original pool's config */ |
7800 | txg = spa_vdev_config_enter(spa); | |
7801 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
7802 | error = dmu_tx_assign(tx, TXG_WAIT); | |
7803 | if (error != 0) | |
7804 | dmu_tx_abort(tx); | |
7805 | for (c = 0; c < children; c++) { | |
1b664952 | 7806 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
234234ca RS |
7807 | vdev_t *tvd = vml[c]->vdev_top; |
7808 | ||
7809 | /* | |
7810 | * Need to be sure the detachable VDEV is not | |
7811 | * on any *other* txg's DTL list to prevent it | |
7812 | * from being accessed after it's freed. | |
7813 | */ | |
7814 | for (int t = 0; t < TXG_SIZE; t++) { | |
7815 | (void) txg_list_remove_this( | |
7816 | &tvd->vdev_dtl_list, vml[c], t); | |
7817 | } | |
7818 | ||
428870ff BB |
7819 | vdev_split(vml[c]); |
7820 | if (error == 0) | |
6f1ffb06 MA |
7821 | spa_history_log_internal(spa, "detach", tx, |
7822 | "vdev=%s", vml[c]->vdev_path); | |
e0ab3ab5 | 7823 | |
428870ff | 7824 | vdev_free(vml[c]); |
34dc7c2f | 7825 | } |
34dc7c2f | 7826 | } |
e0ab3ab5 | 7827 | spa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7828 | vdev_config_dirty(spa->spa_root_vdev); |
7829 | spa->spa_config_splitting = NULL; | |
7830 | nvlist_free(nvl); | |
7831 | if (error == 0) | |
7832 | dmu_tx_commit(tx); | |
7833 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
7834 | ||
7835 | if (zio_injection_enabled) | |
7836 | zio_handle_panic_injection(spa, FTAG, 3); | |
7837 | ||
7838 | /* split is complete; log a history record */ | |
6f1ffb06 MA |
7839 | spa_history_log_internal(newspa, "split", NULL, |
7840 | "from pool %s", spa_name(spa)); | |
428870ff | 7841 | |
8b27e08e | 7842 | newspa->spa_is_splitting = B_FALSE; |
428870ff BB |
7843 | kmem_free(vml, children * sizeof (vdev_t *)); |
7844 | ||
7845 | /* if we're not going to mount the filesystems in userland, export */ | |
7846 | if (exp) | |
7847 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
7848 | B_FALSE, B_FALSE); | |
7849 | ||
7850 | return (error); | |
7851 | ||
7852 | out: | |
7853 | spa_unload(newspa); | |
7854 | spa_deactivate(newspa); | |
7855 | spa_remove(newspa); | |
7856 | ||
7857 | txg = spa_vdev_config_enter(spa); | |
7858 | ||
7859 | /* re-online all offlined disks */ | |
7860 | for (c = 0; c < children; c++) { | |
7861 | if (vml[c] != NULL) | |
7862 | vml[c]->vdev_offline = B_FALSE; | |
7863 | } | |
619f0976 | 7864 | |
1b939560 | 7865 | /* restart initializing or trimming disks as necessary */ |
619f0976 | 7866 | spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); |
1b939560 BB |
7867 | spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); |
7868 | spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); | |
619f0976 | 7869 | |
428870ff BB |
7870 | vdev_reopen(spa->spa_root_vdev); |
7871 | ||
7872 | nvlist_free(spa->spa_config_splitting); | |
7873 | spa->spa_config_splitting = NULL; | |
7874 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 7875 | |
428870ff | 7876 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
7877 | return (error); |
7878 | } | |
7879 | ||
34dc7c2f BB |
7880 | /* |
7881 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
d3cc8b15 | 7882 | * currently spared, so we can detach it. |
34dc7c2f BB |
7883 | */ |
7884 | static vdev_t * | |
7885 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
7886 | { | |
7887 | vdev_t *newvd, *oldvd; | |
34dc7c2f | 7888 | |
1c27024e | 7889 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
7890 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
7891 | if (oldvd != NULL) | |
7892 | return (oldvd); | |
7893 | } | |
7894 | ||
7895 | /* | |
572e2857 BB |
7896 | * Check for a completed replacement. We always consider the first |
7897 | * vdev in the list to be the oldest vdev, and the last one to be | |
7898 | * the newest (see spa_vdev_attach() for how that works). In | |
7899 | * the case where the newest vdev is faulted, we will not automatically | |
7900 | * remove it after a resilver completes. This is OK as it will require | |
7901 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 7902 | */ |
572e2857 BB |
7903 | if (vd->vdev_ops == &vdev_replacing_ops) { |
7904 | ASSERT(vd->vdev_children > 1); | |
7905 | ||
7906 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 7907 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 7908 | |
fb5f0bc8 | 7909 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7910 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 7911 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7912 | return (oldvd); |
34dc7c2f BB |
7913 | } |
7914 | ||
7915 | /* | |
7916 | * Check for a completed resilver with the 'unspare' flag set. | |
f65fbee1 | 7917 | * Also potentially update faulted state. |
34dc7c2f | 7918 | */ |
572e2857 BB |
7919 | if (vd->vdev_ops == &vdev_spare_ops) { |
7920 | vdev_t *first = vd->vdev_child[0]; | |
7921 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
7922 | ||
7923 | if (last->vdev_unspare) { | |
7924 | oldvd = first; | |
7925 | newvd = last; | |
7926 | } else if (first->vdev_unspare) { | |
7927 | oldvd = last; | |
7928 | newvd = first; | |
7929 | } else { | |
7930 | oldvd = NULL; | |
7931 | } | |
34dc7c2f | 7932 | |
572e2857 | 7933 | if (oldvd != NULL && |
fb5f0bc8 | 7934 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7935 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 7936 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7937 | return (oldvd); |
572e2857 | 7938 | |
f65fbee1 JJ |
7939 | vdev_propagate_state(vd); |
7940 | ||
572e2857 BB |
7941 | /* |
7942 | * If there are more than two spares attached to a disk, | |
7943 | * and those spares are not required, then we want to | |
7944 | * attempt to free them up now so that they can be used | |
7945 | * by other pools. Once we're back down to a single | |
7946 | * disk+spare, we stop removing them. | |
7947 | */ | |
7948 | if (vd->vdev_children > 2) { | |
7949 | newvd = vd->vdev_child[1]; | |
7950 | ||
7951 | if (newvd->vdev_isspare && last->vdev_isspare && | |
7952 | vdev_dtl_empty(last, DTL_MISSING) && | |
7953 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
7954 | !vdev_dtl_required(newvd)) | |
7955 | return (newvd); | |
34dc7c2f | 7956 | } |
34dc7c2f BB |
7957 | } |
7958 | ||
7959 | return (NULL); | |
7960 | } | |
7961 | ||
7962 | static void | |
7963 | spa_vdev_resilver_done(spa_t *spa) | |
7964 | { | |
fb5f0bc8 BB |
7965 | vdev_t *vd, *pvd, *ppvd; |
7966 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 7967 | |
fb5f0bc8 | 7968 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7969 | |
7970 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
7971 | pvd = vd->vdev_parent; |
7972 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 7973 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
7974 | pguid = pvd->vdev_guid; |
7975 | ppguid = ppvd->vdev_guid; | |
7976 | sguid = 0; | |
34dc7c2f BB |
7977 | /* |
7978 | * If we have just finished replacing a hot spared device, then | |
7979 | * we need to detach the parent's first child (the original hot | |
7980 | * spare) as well. | |
7981 | */ | |
572e2857 BB |
7982 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
7983 | ppvd->vdev_children == 2) { | |
34dc7c2f | 7984 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 7985 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 7986 | } |
5d1f7fb6 GW |
7987 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); |
7988 | ||
fb5f0bc8 BB |
7989 | spa_config_exit(spa, SCL_ALL, FTAG); |
7990 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 7991 | return; |
fb5f0bc8 | 7992 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 7993 | return; |
fb5f0bc8 | 7994 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7995 | } |
7996 | ||
fb5f0bc8 | 7997 | spa_config_exit(spa, SCL_ALL, FTAG); |
9a49d3f3 BB |
7998 | |
7999 | /* | |
8000 | * If a detach was not performed above replace waiters will not have | |
8001 | * been notified. In which case we must do so now. | |
8002 | */ | |
8003 | spa_notify_waiters(spa); | |
34dc7c2f BB |
8004 | } |
8005 | ||
8006 | /* | |
428870ff | 8007 | * Update the stored path or FRU for this vdev. |
34dc7c2f | 8008 | */ |
65c7cc49 | 8009 | static int |
9babb374 BB |
8010 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
8011 | boolean_t ispath) | |
34dc7c2f | 8012 | { |
b128c09f | 8013 | vdev_t *vd; |
428870ff | 8014 | boolean_t sync = B_FALSE; |
34dc7c2f | 8015 | |
572e2857 BB |
8016 | ASSERT(spa_writeable(spa)); |
8017 | ||
428870ff | 8018 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 8019 | |
9babb374 | 8020 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 8021 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
8022 | |
8023 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 8024 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 8025 | |
9babb374 | 8026 | if (ispath) { |
428870ff BB |
8027 | if (strcmp(value, vd->vdev_path) != 0) { |
8028 | spa_strfree(vd->vdev_path); | |
8029 | vd->vdev_path = spa_strdup(value); | |
8030 | sync = B_TRUE; | |
8031 | } | |
9babb374 | 8032 | } else { |
428870ff BB |
8033 | if (vd->vdev_fru == NULL) { |
8034 | vd->vdev_fru = spa_strdup(value); | |
8035 | sync = B_TRUE; | |
8036 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 8037 | spa_strfree(vd->vdev_fru); |
428870ff BB |
8038 | vd->vdev_fru = spa_strdup(value); |
8039 | sync = B_TRUE; | |
8040 | } | |
9babb374 | 8041 | } |
34dc7c2f | 8042 | |
428870ff | 8043 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
8044 | } |
8045 | ||
9babb374 BB |
8046 | int |
8047 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
8048 | { | |
8049 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
8050 | } | |
8051 | ||
8052 | int | |
8053 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
8054 | { | |
8055 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
8056 | } | |
8057 | ||
34dc7c2f BB |
8058 | /* |
8059 | * ========================================================================== | |
428870ff | 8060 | * SPA Scanning |
34dc7c2f BB |
8061 | * ========================================================================== |
8062 | */ | |
0ea05c64 AP |
8063 | int |
8064 | spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) | |
8065 | { | |
8066 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
8067 | ||
8068 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
8069 | return (SET_ERROR(EBUSY)); | |
8070 | ||
8071 | return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); | |
8072 | } | |
34dc7c2f | 8073 | |
34dc7c2f | 8074 | int |
428870ff BB |
8075 | spa_scan_stop(spa_t *spa) |
8076 | { | |
8077 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
8078 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
2e528b49 | 8079 | return (SET_ERROR(EBUSY)); |
428870ff BB |
8080 | return (dsl_scan_cancel(spa->spa_dsl_pool)); |
8081 | } | |
8082 | ||
8083 | int | |
8084 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 8085 | { |
b128c09f | 8086 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 8087 | |
428870ff | 8088 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
2e528b49 | 8089 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 8090 | |
fa241660 TC |
8091 | if (func == POOL_SCAN_RESILVER && |
8092 | !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
8093 | return (SET_ERROR(ENOTSUP)); | |
8094 | ||
34dc7c2f | 8095 | /* |
b128c09f BB |
8096 | * If a resilver was requested, but there is no DTL on a |
8097 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 8098 | */ |
428870ff | 8099 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
8100 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
8101 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
8102 | return (0); |
8103 | } | |
8104 | ||
428870ff | 8105 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
8106 | } |
8107 | ||
8108 | /* | |
8109 | * ========================================================================== | |
8110 | * SPA async task processing | |
8111 | * ========================================================================== | |
8112 | */ | |
8113 | ||
8114 | static void | |
8115 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
8116 | { | |
b128c09f | 8117 | if (vd->vdev_remove_wanted) { |
428870ff BB |
8118 | vd->vdev_remove_wanted = B_FALSE; |
8119 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 8120 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
8121 | |
8122 | /* | |
8123 | * We want to clear the stats, but we don't want to do a full | |
8124 | * vdev_clear() as that will cause us to throw away | |
8125 | * degraded/faulted state as well as attempt to reopen the | |
8126 | * device, all of which is a waste. | |
8127 | */ | |
8128 | vd->vdev_stat.vs_read_errors = 0; | |
8129 | vd->vdev_stat.vs_write_errors = 0; | |
8130 | vd->vdev_stat.vs_checksum_errors = 0; | |
8131 | ||
b128c09f | 8132 | vdev_state_dirty(vd->vdev_top); |
0aacde2e RM |
8133 | |
8134 | /* Tell userspace that the vdev is gone. */ | |
8135 | zfs_post_remove(spa, vd); | |
b128c09f | 8136 | } |
34dc7c2f | 8137 | |
1c27024e | 8138 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
8139 | spa_async_remove(spa, vd->vdev_child[c]); |
8140 | } | |
8141 | ||
8142 | static void | |
8143 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
8144 | { | |
8145 | if (vd->vdev_probe_wanted) { | |
428870ff | 8146 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 8147 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 8148 | } |
b128c09f | 8149 | |
1c27024e | 8150 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 8151 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
8152 | } |
8153 | ||
9babb374 BB |
8154 | static void |
8155 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
8156 | { | |
9babb374 BB |
8157 | if (!spa->spa_autoexpand) |
8158 | return; | |
8159 | ||
1c27024e | 8160 | for (int c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
8161 | vdev_t *cvd = vd->vdev_child[c]; |
8162 | spa_async_autoexpand(spa, cvd); | |
8163 | } | |
8164 | ||
8165 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
8166 | return; | |
8167 | ||
12fa0466 | 8168 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND); |
9babb374 BB |
8169 | } |
8170 | ||
460748d4 | 8171 | static __attribute__((noreturn)) void |
c25b8f99 | 8172 | spa_async_thread(void *arg) |
34dc7c2f | 8173 | { |
c25b8f99 | 8174 | spa_t *spa = (spa_t *)arg; |
80a91e74 | 8175 | dsl_pool_t *dp = spa->spa_dsl_pool; |
867959b5 | 8176 | int tasks; |
34dc7c2f BB |
8177 | |
8178 | ASSERT(spa->spa_sync_on); | |
8179 | ||
8180 | mutex_enter(&spa->spa_async_lock); | |
8181 | tasks = spa->spa_async_tasks; | |
8182 | spa->spa_async_tasks = 0; | |
8183 | mutex_exit(&spa->spa_async_lock); | |
8184 | ||
8185 | /* | |
8186 | * See if the config needs to be updated. | |
8187 | */ | |
8188 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 8189 | uint64_t old_space, new_space; |
9babb374 | 8190 | |
34dc7c2f | 8191 | mutex_enter(&spa_namespace_lock); |
428870ff | 8192 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
8193 | old_space += metaslab_class_get_space(spa_special_class(spa)); |
8194 | old_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
aa755b35 MA |
8195 | old_space += metaslab_class_get_space( |
8196 | spa_embedded_log_class(spa)); | |
cc99f275 | 8197 | |
34dc7c2f | 8198 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
cc99f275 | 8199 | |
428870ff | 8200 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
8201 | new_space += metaslab_class_get_space(spa_special_class(spa)); |
8202 | new_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
aa755b35 MA |
8203 | new_space += metaslab_class_get_space( |
8204 | spa_embedded_log_class(spa)); | |
34dc7c2f | 8205 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
8206 | |
8207 | /* | |
8208 | * If the pool grew as a result of the config update, | |
8209 | * then log an internal history event. | |
8210 | */ | |
428870ff | 8211 | if (new_space != old_space) { |
6f1ffb06 | 8212 | spa_history_log_internal(spa, "vdev online", NULL, |
45d1cae3 | 8213 | "pool '%s' size: %llu(+%llu)", |
74756182 MM |
8214 | spa_name(spa), (u_longlong_t)new_space, |
8215 | (u_longlong_t)(new_space - old_space)); | |
9babb374 | 8216 | } |
34dc7c2f BB |
8217 | } |
8218 | ||
8219 | /* | |
8220 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 8221 | */ |
b128c09f | 8222 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 8223 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 8224 | spa_async_remove(spa, spa->spa_root_vdev); |
867959b5 | 8225 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 8226 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
867959b5 | 8227 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
8228 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
8229 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
8230 | } |
8231 | ||
9babb374 BB |
8232 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
8233 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8234 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
8235 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8236 | } | |
8237 | ||
34dc7c2f | 8238 | /* |
b128c09f | 8239 | * See if any devices need to be probed. |
34dc7c2f | 8240 | */ |
b128c09f | 8241 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 8242 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
8243 | spa_async_probe(spa, spa->spa_root_vdev); |
8244 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
8245 | } | |
34dc7c2f BB |
8246 | |
8247 | /* | |
b128c09f | 8248 | * If any devices are done replacing, detach them. |
34dc7c2f | 8249 | */ |
b2255edc BB |
8250 | if (tasks & SPA_ASYNC_RESILVER_DONE || |
8251 | tasks & SPA_ASYNC_REBUILD_DONE) { | |
b128c09f | 8252 | spa_vdev_resilver_done(spa); |
9a49d3f3 BB |
8253 | } |
8254 | ||
34dc7c2f BB |
8255 | /* |
8256 | * Kick off a resilver. | |
8257 | */ | |
80a91e74 | 8258 | if (tasks & SPA_ASYNC_RESILVER && |
9a49d3f3 | 8259 | !vdev_rebuild_active(spa->spa_root_vdev) && |
80a91e74 TC |
8260 | (!dsl_scan_resilvering(dp) || |
8261 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) | |
3c819a2c | 8262 | dsl_scan_restart_resilver(dp, 0); |
34dc7c2f | 8263 | |
619f0976 GW |
8264 | if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { |
8265 | mutex_enter(&spa_namespace_lock); | |
8266 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8267 | vdev_initialize_restart(spa->spa_root_vdev); | |
8268 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8269 | mutex_exit(&spa_namespace_lock); | |
8270 | } | |
8271 | ||
1b939560 BB |
8272 | if (tasks & SPA_ASYNC_TRIM_RESTART) { |
8273 | mutex_enter(&spa_namespace_lock); | |
8274 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8275 | vdev_trim_restart(spa->spa_root_vdev); | |
8276 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8277 | mutex_exit(&spa_namespace_lock); | |
8278 | } | |
8279 | ||
8280 | if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) { | |
8281 | mutex_enter(&spa_namespace_lock); | |
8282 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8283 | vdev_autotrim_restart(spa); | |
8284 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8285 | mutex_exit(&spa_namespace_lock); | |
8286 | } | |
8287 | ||
b7654bd7 GA |
8288 | /* |
8289 | * Kick off L2 cache whole device TRIM. | |
8290 | */ | |
8291 | if (tasks & SPA_ASYNC_L2CACHE_TRIM) { | |
8292 | mutex_enter(&spa_namespace_lock); | |
8293 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8294 | vdev_trim_l2arc(spa); | |
8295 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8296 | mutex_exit(&spa_namespace_lock); | |
8297 | } | |
8298 | ||
77f6826b GA |
8299 | /* |
8300 | * Kick off L2 cache rebuilding. | |
8301 | */ | |
8302 | if (tasks & SPA_ASYNC_L2CACHE_REBUILD) { | |
8303 | mutex_enter(&spa_namespace_lock); | |
8304 | spa_config_enter(spa, SCL_L2ARC, FTAG, RW_READER); | |
8305 | l2arc_spa_rebuild_start(spa); | |
8306 | spa_config_exit(spa, SCL_L2ARC, FTAG); | |
8307 | mutex_exit(&spa_namespace_lock); | |
8308 | } | |
8309 | ||
34dc7c2f BB |
8310 | /* |
8311 | * Let the world know that we're done. | |
8312 | */ | |
8313 | mutex_enter(&spa->spa_async_lock); | |
8314 | spa->spa_async_thread = NULL; | |
8315 | cv_broadcast(&spa->spa_async_cv); | |
8316 | mutex_exit(&spa->spa_async_lock); | |
8317 | thread_exit(); | |
8318 | } | |
8319 | ||
8320 | void | |
8321 | spa_async_suspend(spa_t *spa) | |
8322 | { | |
8323 | mutex_enter(&spa->spa_async_lock); | |
8324 | spa->spa_async_suspended++; | |
9d5b5245 | 8325 | while (spa->spa_async_thread != NULL) |
34dc7c2f BB |
8326 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); |
8327 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 MA |
8328 | |
8329 | spa_vdev_remove_suspend(spa); | |
9d5b5245 SD |
8330 | |
8331 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a SD |
8332 | if (condense_thread != NULL) |
8333 | zthr_cancel(condense_thread); | |
d2734cce SD |
8334 | |
8335 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a SD |
8336 | if (discard_thread != NULL) |
8337 | zthr_cancel(discard_thread); | |
37f03da8 SH |
8338 | |
8339 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8340 | if (ll_delete_thread != NULL) | |
8341 | zthr_cancel(ll_delete_thread); | |
8342 | ||
8343 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8344 | if (ll_condense_thread != NULL) | |
8345 | zthr_cancel(ll_condense_thread); | |
34dc7c2f BB |
8346 | } |
8347 | ||
8348 | void | |
8349 | spa_async_resume(spa_t *spa) | |
8350 | { | |
8351 | mutex_enter(&spa->spa_async_lock); | |
8352 | ASSERT(spa->spa_async_suspended != 0); | |
8353 | spa->spa_async_suspended--; | |
8354 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 | 8355 | spa_restart_removal(spa); |
9d5b5245 SD |
8356 | |
8357 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a | 8358 | if (condense_thread != NULL) |
9d5b5245 | 8359 | zthr_resume(condense_thread); |
d2734cce SD |
8360 | |
8361 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a | 8362 | if (discard_thread != NULL) |
d2734cce | 8363 | zthr_resume(discard_thread); |
37f03da8 SH |
8364 | |
8365 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8366 | if (ll_delete_thread != NULL) | |
8367 | zthr_resume(ll_delete_thread); | |
8368 | ||
8369 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8370 | if (ll_condense_thread != NULL) | |
8371 | zthr_resume(ll_condense_thread); | |
34dc7c2f BB |
8372 | } |
8373 | ||
e6cfd633 WA |
8374 | static boolean_t |
8375 | spa_async_tasks_pending(spa_t *spa) | |
8376 | { | |
8377 | uint_t non_config_tasks; | |
8378 | uint_t config_task; | |
8379 | boolean_t config_task_suspended; | |
8380 | ||
8381 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
8382 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
8383 | if (spa->spa_ccw_fail_time == 0) { | |
8384 | config_task_suspended = B_FALSE; | |
8385 | } else { | |
8386 | config_task_suspended = | |
8387 | (gethrtime() - spa->spa_ccw_fail_time) < | |
05852b34 | 8388 | ((hrtime_t)zfs_ccw_retry_interval * NANOSEC); |
e6cfd633 WA |
8389 | } |
8390 | ||
8391 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
8392 | } | |
8393 | ||
34dc7c2f BB |
8394 | static void |
8395 | spa_async_dispatch(spa_t *spa) | |
8396 | { | |
8397 | mutex_enter(&spa->spa_async_lock); | |
e6cfd633 WA |
8398 | if (spa_async_tasks_pending(spa) && |
8399 | !spa->spa_async_suspended && | |
da92d5cb | 8400 | spa->spa_async_thread == NULL) |
34dc7c2f BB |
8401 | spa->spa_async_thread = thread_create(NULL, 0, |
8402 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
8403 | mutex_exit(&spa->spa_async_lock); | |
8404 | } | |
8405 | ||
8406 | void | |
8407 | spa_async_request(spa_t *spa, int task) | |
8408 | { | |
428870ff | 8409 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
8410 | mutex_enter(&spa->spa_async_lock); |
8411 | spa->spa_async_tasks |= task; | |
8412 | mutex_exit(&spa->spa_async_lock); | |
8413 | } | |
8414 | ||
3c819a2c JP |
8415 | int |
8416 | spa_async_tasks(spa_t *spa) | |
8417 | { | |
8418 | return (spa->spa_async_tasks); | |
8419 | } | |
8420 | ||
34dc7c2f BB |
8421 | /* |
8422 | * ========================================================================== | |
8423 | * SPA syncing routines | |
8424 | * ========================================================================== | |
8425 | */ | |
8426 | ||
37f03da8 | 8427 | |
428870ff | 8428 | static int |
37f03da8 SH |
8429 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, |
8430 | dmu_tx_t *tx) | |
34dc7c2f | 8431 | { |
428870ff | 8432 | bpobj_t *bpo = arg; |
37f03da8 | 8433 | bpobj_enqueue(bpo, bp, bp_freed, tx); |
428870ff BB |
8434 | return (0); |
8435 | } | |
34dc7c2f | 8436 | |
37f03da8 SH |
8437 | int |
8438 | bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8439 | { | |
8440 | return (bpobj_enqueue_cb(arg, bp, B_FALSE, tx)); | |
8441 | } | |
8442 | ||
8443 | int | |
8444 | bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8445 | { | |
8446 | return (bpobj_enqueue_cb(arg, bp, B_TRUE, tx)); | |
8447 | } | |
8448 | ||
428870ff BB |
8449 | static int |
8450 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8451 | { | |
9cdf7b1f | 8452 | zio_t *pio = arg; |
34dc7c2f | 8453 | |
9cdf7b1f MA |
8454 | zio_nowait(zio_free_sync(pio, pio->io_spa, dmu_tx_get_txg(tx), bp, |
8455 | pio->io_flags)); | |
428870ff | 8456 | return (0); |
34dc7c2f BB |
8457 | } |
8458 | ||
37f03da8 SH |
8459 | static int |
8460 | bpobj_spa_free_sync_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
8461 | dmu_tx_t *tx) | |
8462 | { | |
8463 | ASSERT(!bp_freed); | |
8464 | return (spa_free_sync_cb(arg, bp, tx)); | |
8465 | } | |
8466 | ||
e8b96c60 MA |
8467 | /* |
8468 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8469 | * amount of time spent syncing frees. | |
8470 | */ | |
8471 | static void | |
8472 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
8473 | { | |
8474 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
8475 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
8476 | VERIFY(zio_wait(zio) == 0); | |
8477 | } | |
8478 | ||
8479 | /* | |
8480 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8481 | * amount of time spent syncing deferred frees. | |
8482 | */ | |
8483 | static void | |
8484 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
8485 | { | |
8dc2197b SD |
8486 | if (spa_sync_pass(spa) != 1) |
8487 | return; | |
8488 | ||
93e28d66 SD |
8489 | /* |
8490 | * Note: | |
8491 | * If the log space map feature is active, we stop deferring | |
8492 | * frees to the next TXG and therefore running this function | |
8493 | * would be considered a no-op as spa_deferred_bpobj should | |
8494 | * not have any entries. | |
8495 | * | |
8496 | * That said we run this function anyway (instead of returning | |
8497 | * immediately) for the edge-case scenario where we just | |
8498 | * activated the log space map feature in this TXG but we have | |
8499 | * deferred frees from the previous TXG. | |
8500 | */ | |
e8b96c60 MA |
8501 | zio_t *zio = zio_root(spa, NULL, NULL, 0); |
8502 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
37f03da8 | 8503 | bpobj_spa_free_sync_cb, zio, tx), ==, 0); |
e8b96c60 MA |
8504 | VERIFY0(zio_wait(zio)); |
8505 | } | |
8506 | ||
34dc7c2f BB |
8507 | static void |
8508 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
8509 | { | |
8510 | char *packed = NULL; | |
b128c09f | 8511 | size_t bufsize; |
34dc7c2f BB |
8512 | size_t nvsize = 0; |
8513 | dmu_buf_t *db; | |
8514 | ||
8515 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
8516 | ||
b128c09f BB |
8517 | /* |
8518 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
b0bc7a84 | 8519 | * information. This avoids the dmu_buf_will_dirty() path and |
b128c09f BB |
8520 | * saves us a pre-read to get data we don't actually care about. |
8521 | */ | |
9ae529ec | 8522 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
79c76d5b | 8523 | packed = vmem_alloc(bufsize, KM_SLEEP); |
34dc7c2f BB |
8524 | |
8525 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
79c76d5b | 8526 | KM_SLEEP) == 0); |
861166b0 | 8527 | memset(packed + nvsize, 0, bufsize - nvsize); |
34dc7c2f | 8528 | |
b128c09f | 8529 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 8530 | |
00b46022 | 8531 | vmem_free(packed, bufsize); |
34dc7c2f BB |
8532 | |
8533 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
8534 | dmu_buf_will_dirty(db, tx); | |
8535 | *(uint64_t *)db->db_data = nvsize; | |
8536 | dmu_buf_rele(db, FTAG); | |
8537 | } | |
8538 | ||
8539 | static void | |
8540 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
8541 | const char *config, const char *entry) | |
8542 | { | |
8543 | nvlist_t *nvroot; | |
8544 | nvlist_t **list; | |
8545 | int i; | |
8546 | ||
8547 | if (!sav->sav_sync) | |
8548 | return; | |
8549 | ||
8550 | /* | |
8551 | * Update the MOS nvlist describing the list of available devices. | |
8552 | * spa_validate_aux() will have already made sure this nvlist is | |
8553 | * valid and the vdevs are labeled appropriately. | |
8554 | */ | |
8555 | if (sav->sav_object == 0) { | |
8556 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
8557 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
8558 | sizeof (uint64_t), tx); | |
8559 | VERIFY(zap_update(spa->spa_meta_objset, | |
8560 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
8561 | &sav->sav_object, tx) == 0); | |
8562 | } | |
8563 | ||
65ad5d11 | 8564 | nvroot = fnvlist_alloc(); |
34dc7c2f | 8565 | if (sav->sav_count == 0) { |
795075e6 PD |
8566 | fnvlist_add_nvlist_array(nvroot, config, |
8567 | (const nvlist_t * const *)NULL, 0); | |
34dc7c2f | 8568 | } else { |
79c76d5b | 8569 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); |
34dc7c2f BB |
8570 | for (i = 0; i < sav->sav_count; i++) |
8571 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 8572 | B_FALSE, VDEV_CONFIG_L2CACHE); |
795075e6 PD |
8573 | fnvlist_add_nvlist_array(nvroot, config, |
8574 | (const nvlist_t * const *)list, sav->sav_count); | |
34dc7c2f BB |
8575 | for (i = 0; i < sav->sav_count; i++) |
8576 | nvlist_free(list[i]); | |
8577 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
8578 | } | |
8579 | ||
8580 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
8581 | nvlist_free(nvroot); | |
8582 | ||
8583 | sav->sav_sync = B_FALSE; | |
8584 | } | |
8585 | ||
e0ab3ab5 JS |
8586 | /* |
8587 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
8588 | * The all-vdev ZAP must be empty. | |
8589 | */ | |
8590 | static void | |
8591 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
8592 | { | |
8593 | spa_t *spa = vd->vdev_spa; | |
e0ab3ab5 JS |
8594 | |
8595 | if (vd->vdev_top_zap != 0) { | |
8596 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8597 | vd->vdev_top_zap, tx)); | |
8598 | } | |
8599 | if (vd->vdev_leaf_zap != 0) { | |
8600 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8601 | vd->vdev_leaf_zap, tx)); | |
8602 | } | |
1c27024e | 8603 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
8604 | spa_avz_build(vd->vdev_child[i], avz, tx); |
8605 | } | |
8606 | } | |
8607 | ||
34dc7c2f BB |
8608 | static void |
8609 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
8610 | { | |
8611 | nvlist_t *config; | |
8612 | ||
e0ab3ab5 JS |
8613 | /* |
8614 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
8615 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
8616 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
8617 | * need to rebuild the AVZ although the config may not be dirty. | |
8618 | */ | |
8619 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
8620 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
34dc7c2f BB |
8621 | return; |
8622 | ||
b128c09f BB |
8623 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
8624 | ||
e0ab3ab5 | 8625 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || |
38640550 | 8626 | spa->spa_avz_action == AVZ_ACTION_INITIALIZE || |
e0ab3ab5 JS |
8627 | spa->spa_all_vdev_zaps != 0); |
8628 | ||
8629 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
e0ab3ab5 JS |
8630 | /* Make and build the new AVZ */ |
8631 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
8632 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
8633 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
8634 | ||
8635 | /* Diff old AVZ with new one */ | |
1c27024e DB |
8636 | zap_cursor_t zc; |
8637 | zap_attribute_t za; | |
8638 | ||
e0ab3ab5 JS |
8639 | for (zap_cursor_init(&zc, spa->spa_meta_objset, |
8640 | spa->spa_all_vdev_zaps); | |
8641 | zap_cursor_retrieve(&zc, &za) == 0; | |
8642 | zap_cursor_advance(&zc)) { | |
8643 | uint64_t vdzap = za.za_first_integer; | |
8644 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
8645 | vdzap) == ENOENT) { | |
8646 | /* | |
8647 | * ZAP is listed in old AVZ but not in new one; | |
8648 | * destroy it | |
8649 | */ | |
8650 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
8651 | tx)); | |
8652 | } | |
8653 | } | |
8654 | ||
8655 | zap_cursor_fini(&zc); | |
8656 | ||
8657 | /* Destroy the old AVZ */ | |
8658 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8659 | spa->spa_all_vdev_zaps, tx)); | |
8660 | ||
8661 | /* Replace the old AVZ in the dir obj with the new one */ | |
8662 | VERIFY0(zap_update(spa->spa_meta_objset, | |
8663 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
8664 | sizeof (new_avz), 1, &new_avz, tx)); | |
8665 | ||
8666 | spa->spa_all_vdev_zaps = new_avz; | |
8667 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
8668 | zap_cursor_t zc; | |
8669 | zap_attribute_t za; | |
8670 | ||
8671 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
8672 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
8673 | spa->spa_all_vdev_zaps); | |
8674 | zap_cursor_retrieve(&zc, &za) == 0; | |
8675 | zap_cursor_advance(&zc)) { | |
8676 | uint64_t zap = za.za_first_integer; | |
8677 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
8678 | } | |
8679 | ||
8680 | zap_cursor_fini(&zc); | |
8681 | ||
8682 | /* Destroy and unlink the AVZ itself */ | |
8683 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8684 | spa->spa_all_vdev_zaps, tx)); | |
8685 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
8686 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
8687 | spa->spa_all_vdev_zaps = 0; | |
8688 | } | |
8689 | ||
8690 | if (spa->spa_all_vdev_zaps == 0) { | |
8691 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
8692 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
8693 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
8694 | } | |
8695 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
8696 | ||
8697 | /* Create ZAPs for vdevs that don't have them. */ | |
8698 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
8699 | ||
b128c09f BB |
8700 | config = spa_config_generate(spa, spa->spa_root_vdev, |
8701 | dmu_tx_get_txg(tx), B_FALSE); | |
8702 | ||
ea0b2538 GW |
8703 | /* |
8704 | * If we're upgrading the spa version then make sure that | |
8705 | * the config object gets updated with the correct version. | |
8706 | */ | |
8707 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
8708 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
8709 | spa->spa_uberblock.ub_version); | |
8710 | ||
b128c09f | 8711 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f | 8712 | |
8a5fc748 | 8713 | nvlist_free(spa->spa_config_syncing); |
34dc7c2f BB |
8714 | spa->spa_config_syncing = config; |
8715 | ||
8716 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
8717 | } | |
8718 | ||
9ae529ec | 8719 | static void |
13fe0198 | 8720 | spa_sync_version(void *arg, dmu_tx_t *tx) |
9ae529ec | 8721 | { |
13fe0198 MA |
8722 | uint64_t *versionp = arg; |
8723 | uint64_t version = *versionp; | |
8724 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
9ae529ec CS |
8725 | |
8726 | /* | |
8727 | * Setting the version is special cased when first creating the pool. | |
8728 | */ | |
8729 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
8730 | ||
8dca0a9a | 8731 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
8732 | ASSERT(version >= spa_version(spa)); |
8733 | ||
8734 | spa->spa_uberblock.ub_version = version; | |
8735 | vdev_config_dirty(spa->spa_root_vdev); | |
74756182 MM |
8736 | spa_history_log_internal(spa, "set", tx, "version=%lld", |
8737 | (longlong_t)version); | |
9ae529ec CS |
8738 | } |
8739 | ||
34dc7c2f BB |
8740 | /* |
8741 | * Set zpool properties. | |
8742 | */ | |
8743 | static void | |
13fe0198 | 8744 | spa_sync_props(void *arg, dmu_tx_t *tx) |
34dc7c2f | 8745 | { |
13fe0198 MA |
8746 | nvlist_t *nvp = arg; |
8747 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f | 8748 | objset_t *mos = spa->spa_meta_objset; |
9ae529ec | 8749 | nvpair_t *elem = NULL; |
b128c09f BB |
8750 | |
8751 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 8752 | |
34dc7c2f | 8753 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec CS |
8754 | uint64_t intval; |
8755 | char *strval, *fname; | |
8756 | zpool_prop_t prop; | |
8757 | const char *propname; | |
8758 | zprop_type_t proptype; | |
fa86b5db | 8759 | spa_feature_t fid; |
9ae529ec | 8760 | |
31864e3d BB |
8761 | switch (prop = zpool_name_to_prop(nvpair_name(elem))) { |
8762 | case ZPOOL_PROP_INVAL: | |
9ae529ec CS |
8763 | /* |
8764 | * We checked this earlier in spa_prop_validate(). | |
8765 | */ | |
8766 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
8767 | ||
8768 | fname = strchr(nvpair_name(elem), '@') + 1; | |
fa86b5db | 8769 | VERIFY0(zfeature_lookup_name(fname, &fid)); |
9ae529ec | 8770 | |
fa86b5db | 8771 | spa_feature_enable(spa, fid, tx); |
6f1ffb06 MA |
8772 | spa_history_log_internal(spa, "set", tx, |
8773 | "%s=enabled", nvpair_name(elem)); | |
9ae529ec CS |
8774 | break; |
8775 | ||
34dc7c2f | 8776 | case ZPOOL_PROP_VERSION: |
93cf2076 | 8777 | intval = fnvpair_value_uint64(elem); |
34dc7c2f | 8778 | /* |
4e33ba4c | 8779 | * The version is synced separately before other |
9ae529ec | 8780 | * properties and should be correct by now. |
34dc7c2f | 8781 | */ |
9ae529ec | 8782 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
8783 | break; |
8784 | ||
8785 | case ZPOOL_PROP_ALTROOT: | |
8786 | /* | |
8787 | * 'altroot' is a non-persistent property. It should | |
8788 | * have been set temporarily at creation or import time. | |
8789 | */ | |
8790 | ASSERT(spa->spa_root != NULL); | |
8791 | break; | |
8792 | ||
572e2857 | 8793 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
8794 | case ZPOOL_PROP_CACHEFILE: |
8795 | /* | |
e1cfd73f | 8796 | * 'readonly' and 'cachefile' are also non-persistent |
572e2857 | 8797 | * properties. |
34dc7c2f | 8798 | */ |
34dc7c2f | 8799 | break; |
d96eb2b1 | 8800 | case ZPOOL_PROP_COMMENT: |
93cf2076 | 8801 | strval = fnvpair_value_string(elem); |
d96eb2b1 DM |
8802 | if (spa->spa_comment != NULL) |
8803 | spa_strfree(spa->spa_comment); | |
8804 | spa->spa_comment = spa_strdup(strval); | |
8805 | /* | |
8806 | * We need to dirty the configuration on all the vdevs | |
88a48330 BB |
8807 | * so that their labels get updated. We also need to |
8808 | * update the cache file to keep it in sync with the | |
8809 | * MOS version. It's unnecessary to do this for pool | |
8810 | * creation since the vdev's configuration has already | |
8811 | * been dirtied. | |
d96eb2b1 | 8812 | */ |
88a48330 | 8813 | if (tx->tx_txg != TXG_INITIAL) { |
d96eb2b1 | 8814 | vdev_config_dirty(spa->spa_root_vdev); |
88a48330 BB |
8815 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); |
8816 | } | |
6f1ffb06 MA |
8817 | spa_history_log_internal(spa, "set", tx, |
8818 | "%s=%s", nvpair_name(elem), strval); | |
d96eb2b1 | 8819 | break; |
658fb802 CB |
8820 | case ZPOOL_PROP_COMPATIBILITY: |
8821 | strval = fnvpair_value_string(elem); | |
8822 | if (spa->spa_compatibility != NULL) | |
8823 | spa_strfree(spa->spa_compatibility); | |
8824 | spa->spa_compatibility = spa_strdup(strval); | |
8825 | /* | |
8826 | * Dirty the configuration on vdevs as above. | |
8827 | */ | |
88a48330 | 8828 | if (tx->tx_txg != TXG_INITIAL) { |
658fb802 | 8829 | vdev_config_dirty(spa->spa_root_vdev); |
88a48330 BB |
8830 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); |
8831 | } | |
8832 | ||
658fb802 CB |
8833 | spa_history_log_internal(spa, "set", tx, |
8834 | "%s=%s", nvpair_name(elem), strval); | |
8835 | break; | |
8836 | ||
34dc7c2f BB |
8837 | default: |
8838 | /* | |
8839 | * Set pool property values in the poolprops mos object. | |
8840 | */ | |
34dc7c2f | 8841 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
8842 | spa->spa_pool_props_object = |
8843 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 8844 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 8845 | tx); |
34dc7c2f | 8846 | } |
34dc7c2f BB |
8847 | |
8848 | /* normalize the property name */ | |
8849 | propname = zpool_prop_to_name(prop); | |
8850 | proptype = zpool_prop_get_type(prop); | |
8851 | ||
8852 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
8853 | ASSERT(proptype == PROP_TYPE_STRING); | |
93cf2076 GW |
8854 | strval = fnvpair_value_string(elem); |
8855 | VERIFY0(zap_update(mos, | |
34dc7c2f | 8856 | spa->spa_pool_props_object, propname, |
93cf2076 | 8857 | 1, strlen(strval) + 1, strval, tx)); |
6f1ffb06 MA |
8858 | spa_history_log_internal(spa, "set", tx, |
8859 | "%s=%s", nvpair_name(elem), strval); | |
34dc7c2f | 8860 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { |
93cf2076 | 8861 | intval = fnvpair_value_uint64(elem); |
34dc7c2f BB |
8862 | |
8863 | if (proptype == PROP_TYPE_INDEX) { | |
8864 | const char *unused; | |
93cf2076 GW |
8865 | VERIFY0(zpool_prop_index_to_string( |
8866 | prop, intval, &unused)); | |
34dc7c2f | 8867 | } |
93cf2076 | 8868 | VERIFY0(zap_update(mos, |
34dc7c2f | 8869 | spa->spa_pool_props_object, propname, |
93cf2076 | 8870 | 8, 1, &intval, tx)); |
6f1ffb06 | 8871 | spa_history_log_internal(spa, "set", tx, |
74756182 MM |
8872 | "%s=%lld", nvpair_name(elem), |
8873 | (longlong_t)intval); | |
34dc7c2f | 8874 | |
44f71818 RY |
8875 | switch (prop) { |
8876 | case ZPOOL_PROP_DELEGATION: | |
8877 | spa->spa_delegation = intval; | |
8878 | break; | |
8879 | case ZPOOL_PROP_BOOTFS: | |
8880 | spa->spa_bootfs = intval; | |
8881 | break; | |
8882 | case ZPOOL_PROP_FAILUREMODE: | |
8883 | spa->spa_failmode = intval; | |
8884 | break; | |
8885 | case ZPOOL_PROP_AUTOTRIM: | |
8886 | spa->spa_autotrim = intval; | |
428870ff | 8887 | spa_async_request(spa, |
44f71818 RY |
8888 | SPA_ASYNC_AUTOTRIM_RESTART); |
8889 | break; | |
8890 | case ZPOOL_PROP_AUTOEXPAND: | |
8891 | spa->spa_autoexpand = intval; | |
8892 | if (tx->tx_txg != TXG_INITIAL) | |
8893 | spa_async_request(spa, | |
8894 | SPA_ASYNC_AUTOEXPAND); | |
8895 | break; | |
8896 | case ZPOOL_PROP_MULTIHOST: | |
8897 | spa->spa_multihost = intval; | |
8898 | break; | |
8899 | default: | |
8900 | break; | |
8901 | } | |
8902 | } else { | |
8903 | ASSERT(0); /* not allowed */ | |
34dc7c2f BB |
8904 | } |
8905 | } | |
8906 | ||
34dc7c2f | 8907 | } |
b128c09f BB |
8908 | |
8909 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
8910 | } |
8911 | ||
428870ff BB |
8912 | /* |
8913 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
8914 | * reflect the new version this txg, so there must be no changes this | |
8915 | * txg to anything that the upgrade code depends on after it executes. | |
8916 | * Therefore this must be called after dsl_pool_sync() does the sync | |
8917 | * tasks. | |
8918 | */ | |
8919 | static void | |
8920 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
8921 | { | |
8dc2197b SD |
8922 | if (spa_sync_pass(spa) != 1) |
8923 | return; | |
428870ff | 8924 | |
8dc2197b | 8925 | dsl_pool_t *dp = spa->spa_dsl_pool; |
13fe0198 MA |
8926 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
8927 | ||
428870ff BB |
8928 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && |
8929 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
8930 | dsl_pool_create_origin(dp, tx); | |
8931 | ||
8932 | /* Keeping the origin open increases spa_minref */ | |
8933 | spa->spa_minref += 3; | |
8934 | } | |
8935 | ||
8936 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
8937 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
8938 | dsl_pool_upgrade_clones(dp, tx); | |
8939 | } | |
8940 | ||
8941 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
8942 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
8943 | dsl_pool_upgrade_dir_clones(dp, tx); | |
8944 | ||
8945 | /* Keeping the freedir open increases spa_minref */ | |
8946 | spa->spa_minref += 3; | |
8947 | } | |
9ae529ec CS |
8948 | |
8949 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
8950 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8951 | spa_feature_create_zap_objects(spa, tx); | |
8952 | } | |
62bdd5eb DL |
8953 | |
8954 | /* | |
8955 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
8956 | * when possibility to use lz4 compression for metadata was added | |
8957 | * Old pools that have this feature enabled must be upgraded to have | |
8958 | * this feature active | |
8959 | */ | |
8960 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8961 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
8962 | SPA_FEATURE_LZ4_COMPRESS); | |
8963 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
8964 | SPA_FEATURE_LZ4_COMPRESS); | |
8965 | ||
8966 | if (lz4_en && !lz4_ac) | |
8967 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
8968 | } | |
3c67d83a TH |
8969 | |
8970 | /* | |
8971 | * If we haven't written the salt, do so now. Note that the | |
8972 | * feature may not be activated yet, but that's fine since | |
8973 | * the presence of this ZAP entry is backwards compatible. | |
8974 | */ | |
8975 | if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
8976 | DMU_POOL_CHECKSUM_SALT) == ENOENT) { | |
8977 | VERIFY0(zap_add(spa->spa_meta_objset, | |
8978 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, | |
8979 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
8980 | spa->spa_cksum_salt.zcs_bytes, tx)); | |
8981 | } | |
8982 | ||
13fe0198 | 8983 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
428870ff BB |
8984 | } |
8985 | ||
a1d477c2 MA |
8986 | static void |
8987 | vdev_indirect_state_sync_verify(vdev_t *vd) | |
8988 | { | |
2a8ba608 MM |
8989 | vdev_indirect_mapping_t *vim __maybe_unused = vd->vdev_indirect_mapping; |
8990 | vdev_indirect_births_t *vib __maybe_unused = vd->vdev_indirect_births; | |
a1d477c2 MA |
8991 | |
8992 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
8993 | ASSERT(vim != NULL); | |
8994 | ASSERT(vib != NULL); | |
8995 | } | |
8996 | ||
27f80e85 BB |
8997 | uint64_t obsolete_sm_object = 0; |
8998 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
8999 | if (obsolete_sm_object != 0) { | |
a1d477c2 MA |
9000 | ASSERT(vd->vdev_obsolete_sm != NULL); |
9001 | ASSERT(vd->vdev_removing || | |
9002 | vd->vdev_ops == &vdev_indirect_ops); | |
9003 | ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); | |
9004 | ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); | |
27f80e85 | 9005 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
9006 | space_map_object(vd->vdev_obsolete_sm)); |
9007 | ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, | |
9008 | space_map_allocated(vd->vdev_obsolete_sm)); | |
9009 | } | |
9010 | ASSERT(vd->vdev_obsolete_segments != NULL); | |
9011 | ||
9012 | /* | |
9013 | * Since frees / remaps to an indirect vdev can only | |
9014 | * happen in syncing context, the obsolete segments | |
9015 | * tree must be empty when we start syncing. | |
9016 | */ | |
9017 | ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); | |
9018 | } | |
9019 | ||
34dc7c2f | 9020 | /* |
8dc2197b SD |
9021 | * Set the top-level vdev's max queue depth. Evaluate each top-level's |
9022 | * async write queue depth in case it changed. The max queue depth will | |
9023 | * not change in the middle of syncing out this txg. | |
34dc7c2f | 9024 | */ |
8dc2197b SD |
9025 | static void |
9026 | spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) | |
34dc7c2f | 9027 | { |
8dc2197b SD |
9028 | ASSERT(spa_writeable(spa)); |
9029 | ||
34dc7c2f | 9030 | vdev_t *rvd = spa->spa_root_vdev; |
3dfb57a3 DB |
9031 | uint32_t max_queue_depth = zfs_vdev_async_write_max_active * |
9032 | zfs_vdev_queue_depth_pct / 100; | |
8dc2197b SD |
9033 | metaslab_class_t *normal = spa_normal_class(spa); |
9034 | metaslab_class_t *special = spa_special_class(spa); | |
9035 | metaslab_class_t *dedup = spa_dedup_class(spa); | |
34dc7c2f | 9036 | |
492f64e9 | 9037 | uint64_t slots_per_allocator = 0; |
1c27024e | 9038 | for (int c = 0; c < rvd->vdev_children; c++) { |
3dfb57a3 | 9039 | vdev_t *tvd = rvd->vdev_child[c]; |
cc99f275 | 9040 | |
8dc2197b | 9041 | metaslab_group_t *mg = tvd->vdev_mg; |
cc99f275 DB |
9042 | if (mg == NULL || !metaslab_group_initialized(mg)) |
9043 | continue; | |
3dfb57a3 | 9044 | |
8dc2197b | 9045 | metaslab_class_t *mc = mg->mg_class; |
cc99f275 | 9046 | if (mc != normal && mc != special && mc != dedup) |
3dfb57a3 DB |
9047 | continue; |
9048 | ||
9049 | /* | |
9050 | * It is safe to do a lock-free check here because only async | |
9051 | * allocations look at mg_max_alloc_queue_depth, and async | |
9052 | * allocations all happen from spa_sync(). | |
9053 | */ | |
32d805c3 | 9054 | for (int i = 0; i < mg->mg_allocators; i++) { |
424fd7c3 | 9055 | ASSERT0(zfs_refcount_count( |
32d805c3 MA |
9056 | &(mg->mg_allocator[i].mga_alloc_queue_depth))); |
9057 | } | |
3dfb57a3 | 9058 | mg->mg_max_alloc_queue_depth = max_queue_depth; |
492f64e9 | 9059 | |
32d805c3 MA |
9060 | for (int i = 0; i < mg->mg_allocators; i++) { |
9061 | mg->mg_allocator[i].mga_cur_max_alloc_queue_depth = | |
492f64e9 PD |
9062 | zfs_vdev_def_queue_depth; |
9063 | } | |
9064 | slots_per_allocator += zfs_vdev_def_queue_depth; | |
3dfb57a3 | 9065 | } |
cc99f275 | 9066 | |
492f64e9 | 9067 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
f8020c93 AM |
9068 | ASSERT0(zfs_refcount_count(&normal->mc_allocator[i]. |
9069 | mca_alloc_slots)); | |
9070 | ASSERT0(zfs_refcount_count(&special->mc_allocator[i]. | |
9071 | mca_alloc_slots)); | |
9072 | ASSERT0(zfs_refcount_count(&dedup->mc_allocator[i]. | |
9073 | mca_alloc_slots)); | |
9074 | normal->mc_allocator[i].mca_alloc_max_slots = | |
9075 | slots_per_allocator; | |
9076 | special->mc_allocator[i].mca_alloc_max_slots = | |
9077 | slots_per_allocator; | |
9078 | dedup->mc_allocator[i].mca_alloc_max_slots = | |
9079 | slots_per_allocator; | |
cc99f275 DB |
9080 | } |
9081 | normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
9082 | special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
9083 | dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8dc2197b SD |
9084 | } |
9085 | ||
9086 | static void | |
9087 | spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) | |
9088 | { | |
9089 | ASSERT(spa_writeable(spa)); | |
3dfb57a3 | 9090 | |
8dc2197b | 9091 | vdev_t *rvd = spa->spa_root_vdev; |
a1d477c2 MA |
9092 | for (int c = 0; c < rvd->vdev_children; c++) { |
9093 | vdev_t *vd = rvd->vdev_child[c]; | |
9094 | vdev_indirect_state_sync_verify(vd); | |
9095 | ||
9096 | if (vdev_indirect_should_condense(vd)) { | |
9097 | spa_condense_indirect_start_sync(vd, tx); | |
9098 | break; | |
9099 | } | |
9100 | } | |
8dc2197b SD |
9101 | } |
9102 | ||
9103 | static void | |
9104 | spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) | |
9105 | { | |
9106 | objset_t *mos = spa->spa_meta_objset; | |
9107 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
9108 | uint64_t txg = tx->tx_txg; | |
9109 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
a1d477c2 | 9110 | |
34dc7c2f | 9111 | do { |
428870ff | 9112 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
9113 | |
9114 | spa_sync_config_object(spa, tx); | |
9115 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
9116 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
9117 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
9118 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
9119 | spa_errlog_sync(spa, txg); | |
9120 | dsl_pool_sync(dp, txg); | |
9121 | ||
93e28d66 SD |
9122 | if (pass < zfs_sync_pass_deferred_free || |
9123 | spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) { | |
9124 | /* | |
9125 | * If the log space map feature is active we don't | |
9126 | * care about deferred frees and the deferred bpobj | |
9127 | * as the log space map should effectively have the | |
9128 | * same results (i.e. appending only to one object). | |
9129 | */ | |
e8b96c60 | 9130 | spa_sync_frees(spa, free_bpl, tx); |
428870ff | 9131 | } else { |
905edb40 MA |
9132 | /* |
9133 | * We can not defer frees in pass 1, because | |
9134 | * we sync the deferred frees later in pass 1. | |
9135 | */ | |
9136 | ASSERT3U(pass, >, 1); | |
37f03da8 | 9137 | bplist_iterate(free_bpl, bpobj_enqueue_alloc_cb, |
e8b96c60 | 9138 | &spa->spa_deferred_bpobj, tx); |
34dc7c2f BB |
9139 | } |
9140 | ||
428870ff BB |
9141 | ddt_sync(spa, txg); |
9142 | dsl_scan_sync(dp, tx); | |
8dc2197b SD |
9143 | svr_sync(spa, tx); |
9144 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 9145 | |
93e28d66 SD |
9146 | spa_flush_metaslabs(spa, tx); |
9147 | ||
8dc2197b | 9148 | vdev_t *vd = NULL; |
a1d477c2 MA |
9149 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) |
9150 | != NULL) | |
428870ff BB |
9151 | vdev_sync(vd, txg); |
9152 | ||
8dc2197b SD |
9153 | /* |
9154 | * Note: We need to check if the MOS is dirty because we could | |
9155 | * have marked the MOS dirty without updating the uberblock | |
9156 | * (e.g. if we have sync tasks but no dirty user data). We need | |
9157 | * to check the uberblock's rootbp because it is updated if we | |
9158 | * have synced out dirty data (though in this case the MOS will | |
9159 | * most likely also be dirty due to second order effects, we | |
9160 | * don't want to rely on that here). | |
9161 | */ | |
9162 | if (pass == 1 && | |
9163 | spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
9164 | !dmu_objset_is_dirty(mos, txg)) { | |
905edb40 | 9165 | /* |
8dc2197b SD |
9166 | * Nothing changed on the first pass, therefore this |
9167 | * TXG is a no-op. Avoid syncing deferred frees, so | |
9168 | * that we can keep this TXG as a no-op. | |
905edb40 | 9169 | */ |
8dc2197b SD |
9170 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); |
9171 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9172 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
9173 | ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); | |
9174 | break; | |
905edb40 | 9175 | } |
34dc7c2f | 9176 | |
8dc2197b | 9177 | spa_sync_deferred_frees(spa, tx); |
428870ff | 9178 | } while (dmu_objset_is_dirty(mos, txg)); |
8dc2197b | 9179 | } |
34dc7c2f | 9180 | |
8dc2197b SD |
9181 | /* |
9182 | * Rewrite the vdev configuration (which includes the uberblock) to | |
9183 | * commit the transaction group. | |
9184 | * | |
9185 | * If there are no dirty vdevs, we sync the uberblock to a few random | |
9186 | * top-level vdevs that are known to be visible in the config cache | |
9187 | * (see spa_vdev_add() for a complete description). If there *are* dirty | |
9188 | * vdevs, sync the uberblock to all vdevs. | |
9189 | */ | |
9190 | static void | |
9191 | spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) | |
9192 | { | |
9193 | vdev_t *rvd = spa->spa_root_vdev; | |
9194 | uint64_t txg = tx->tx_txg; | |
a1d477c2 | 9195 | |
b128c09f | 9196 | for (;;) { |
8dc2197b SD |
9197 | int error = 0; |
9198 | ||
b128c09f BB |
9199 | /* |
9200 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
9201 | * while we're attempting to write the vdev labels. | |
9202 | */ | |
9203 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
9204 | ||
9205 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
d2734cce | 9206 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; |
b128c09f BB |
9207 | int svdcount = 0; |
9208 | int children = rvd->vdev_children; | |
29274c9f | 9209 | int c0 = random_in_range(children); |
b128c09f | 9210 | |
1c27024e | 9211 | for (int c = 0; c < children; c++) { |
8dc2197b SD |
9212 | vdev_t *vd = |
9213 | rvd->vdev_child[(c0 + c) % children]; | |
d2734cce SD |
9214 | |
9215 | /* Stop when revisiting the first vdev */ | |
9216 | if (c > 0 && svd[0] == vd) | |
9217 | break; | |
9218 | ||
8dc2197b SD |
9219 | if (vd->vdev_ms_array == 0 || |
9220 | vd->vdev_islog || | |
a1d477c2 | 9221 | !vdev_is_concrete(vd)) |
b128c09f | 9222 | continue; |
d2734cce | 9223 | |
b128c09f | 9224 | svd[svdcount++] = vd; |
6cb8e530 | 9225 | if (svdcount == SPA_SYNC_MIN_VDEVS) |
b128c09f BB |
9226 | break; |
9227 | } | |
b6fcb792 | 9228 | error = vdev_config_sync(svd, svdcount, txg); |
b128c09f BB |
9229 | } else { |
9230 | error = vdev_config_sync(rvd->vdev_child, | |
b6fcb792 | 9231 | rvd->vdev_children, txg); |
34dc7c2f | 9232 | } |
34dc7c2f | 9233 | |
3bc7e0fb GW |
9234 | if (error == 0) |
9235 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
9236 | ||
b128c09f BB |
9237 | spa_config_exit(spa, SCL_STATE, FTAG); |
9238 | ||
9239 | if (error == 0) | |
9240 | break; | |
cec3a0a1 | 9241 | zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); |
b128c09f BB |
9242 | zio_resume_wait(spa); |
9243 | } | |
8dc2197b SD |
9244 | } |
9245 | ||
9246 | /* | |
9247 | * Sync the specified transaction group. New blocks may be dirtied as | |
9248 | * part of the process, so we iterate until it converges. | |
9249 | */ | |
9250 | void | |
9251 | spa_sync(spa_t *spa, uint64_t txg) | |
9252 | { | |
9253 | vdev_t *vd = NULL; | |
9254 | ||
9255 | VERIFY(spa_writeable(spa)); | |
9256 | ||
9257 | /* | |
9258 | * Wait for i/os issued in open context that need to complete | |
9259 | * before this txg syncs. | |
9260 | */ | |
9261 | (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); | |
9262 | spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, | |
9263 | ZIO_FLAG_CANFAIL); | |
9264 | ||
9265 | /* | |
9266 | * Lock out configuration changes. | |
9267 | */ | |
9268 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
9269 | ||
9270 | spa->spa_syncing_txg = txg; | |
9271 | spa->spa_sync_pass = 0; | |
9272 | ||
9273 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
1b50749c AM |
9274 | mutex_enter(&spa->spa_allocs[i].spaa_lock); |
9275 | VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree)); | |
9276 | mutex_exit(&spa->spa_allocs[i].spaa_lock); | |
8dc2197b SD |
9277 | } |
9278 | ||
9279 | /* | |
9280 | * If there are any pending vdev state changes, convert them | |
9281 | * into config changes that go out with this transaction group. | |
9282 | */ | |
9283 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
9f08b6e3 RY |
9284 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { |
9285 | /* Avoid holding the write lock unless actually necessary */ | |
9286 | if (vd->vdev_aux == NULL) { | |
9287 | vdev_state_clean(vd); | |
9288 | vdev_config_dirty(vd); | |
9289 | continue; | |
9290 | } | |
8dc2197b SD |
9291 | /* |
9292 | * We need the write lock here because, for aux vdevs, | |
9293 | * calling vdev_config_dirty() modifies sav_config. | |
9294 | * This is ugly and will become unnecessary when we | |
9295 | * eliminate the aux vdev wart by integrating all vdevs | |
9296 | * into the root vdev tree. | |
9297 | */ | |
9298 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9299 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
9300 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
9301 | vdev_state_clean(vd); | |
9302 | vdev_config_dirty(vd); | |
9303 | } | |
9304 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9305 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9306 | } | |
9307 | spa_config_exit(spa, SCL_STATE, FTAG); | |
9308 | ||
9309 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
9310 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
9311 | ||
9312 | spa->spa_sync_starttime = gethrtime(); | |
9313 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); | |
9314 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, | |
9315 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
9316 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
9317 | ||
9318 | /* | |
9319 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
9320 | * set spa_deflate if we have no raid-z vdevs. | |
9321 | */ | |
9322 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
9323 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
9324 | vdev_t *rvd = spa->spa_root_vdev; | |
9325 | ||
9326 | int i; | |
9327 | for (i = 0; i < rvd->vdev_children; i++) { | |
9328 | vd = rvd->vdev_child[i]; | |
9329 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
9330 | break; | |
9331 | } | |
9332 | if (i == rvd->vdev_children) { | |
9333 | spa->spa_deflate = TRUE; | |
9334 | VERIFY0(zap_add(spa->spa_meta_objset, | |
9335 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
9336 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
9337 | } | |
9338 | } | |
9339 | ||
9340 | spa_sync_adjust_vdev_max_queue_depth(spa); | |
9341 | ||
9342 | spa_sync_condense_indirect(spa, tx); | |
9343 | ||
9344 | spa_sync_iterate_to_convergence(spa, tx); | |
9345 | ||
9346 | #ifdef ZFS_DEBUG | |
9347 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
9348 | /* | |
9349 | * Make sure that the number of ZAPs for all the vdevs matches | |
9350 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
9351 | * called if the config is dirty; otherwise there may be | |
9352 | * outstanding AVZ operations that weren't completed in | |
9353 | * spa_sync_config_object. | |
9354 | */ | |
9355 | uint64_t all_vdev_zap_entry_count; | |
9356 | ASSERT0(zap_count(spa->spa_meta_objset, | |
9357 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
9358 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
9359 | all_vdev_zap_entry_count); | |
9360 | } | |
9361 | #endif | |
9362 | ||
9363 | if (spa->spa_vdev_removal != NULL) { | |
9364 | ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); | |
9365 | } | |
9366 | ||
9367 | spa_sync_rewrite_vdev_config(spa, tx); | |
34dc7c2f BB |
9368 | dmu_tx_commit(tx); |
9369 | ||
57ddcda1 | 9370 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 GW |
9371 | spa->spa_deadman_tqid = 0; |
9372 | ||
34dc7c2f BB |
9373 | /* |
9374 | * Clear the dirty config list. | |
9375 | */ | |
b128c09f | 9376 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
9377 | vdev_config_clean(vd); |
9378 | ||
9379 | /* | |
9380 | * Now that the new config has synced transactionally, | |
9381 | * let it become visible to the config cache. | |
9382 | */ | |
9383 | if (spa->spa_config_syncing != NULL) { | |
9384 | spa_config_set(spa, spa->spa_config_syncing); | |
9385 | spa->spa_config_txg = txg; | |
9386 | spa->spa_config_syncing = NULL; | |
9387 | } | |
9388 | ||
428870ff | 9389 | dsl_pool_sync_done(dp, txg); |
34dc7c2f | 9390 | |
492f64e9 | 9391 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
1b50749c AM |
9392 | mutex_enter(&spa->spa_allocs[i].spaa_lock); |
9393 | VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree)); | |
9394 | mutex_exit(&spa->spa_allocs[i].spaa_lock); | |
492f64e9 | 9395 | } |
3dfb57a3 | 9396 | |
34dc7c2f BB |
9397 | /* |
9398 | * Update usable space statistics. | |
9399 | */ | |
619f0976 GW |
9400 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) |
9401 | != NULL) | |
34dc7c2f | 9402 | vdev_sync_done(vd, txg); |
f09fda50 PD |
9403 | |
9404 | metaslab_class_evict_old(spa->spa_normal_class, txg); | |
9405 | metaslab_class_evict_old(spa->spa_log_class, txg); | |
9406 | ||
93e28d66 | 9407 | spa_sync_close_syncing_log_sm(spa); |
34dc7c2f | 9408 | |
428870ff BB |
9409 | spa_update_dspace(spa); |
9410 | ||
34dc7c2f BB |
9411 | /* |
9412 | * It had better be the case that we didn't dirty anything | |
9413 | * since vdev_config_sync(). | |
9414 | */ | |
9415 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
9416 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9417 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff | 9418 | |
d2734cce SD |
9419 | while (zfs_pause_spa_sync) |
9420 | delay(1); | |
9421 | ||
428870ff | 9422 | spa->spa_sync_pass = 0; |
34dc7c2f | 9423 | |
55922e73 GW |
9424 | /* |
9425 | * Update the last synced uberblock here. We want to do this at | |
9426 | * the end of spa_sync() so that consumers of spa_last_synced_txg() | |
9427 | * will be guaranteed that all the processing associated with | |
9428 | * that txg has been completed. | |
9429 | */ | |
9430 | spa->spa_ubsync = spa->spa_uberblock; | |
b128c09f | 9431 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 9432 | |
428870ff BB |
9433 | spa_handle_ignored_writes(spa); |
9434 | ||
34dc7c2f BB |
9435 | /* |
9436 | * If any async tasks have been requested, kick them off. | |
9437 | */ | |
9438 | spa_async_dispatch(spa); | |
9439 | } | |
9440 | ||
9441 | /* | |
9442 | * Sync all pools. We don't want to hold the namespace lock across these | |
9443 | * operations, so we take a reference on the spa_t and drop the lock during the | |
9444 | * sync. | |
9445 | */ | |
9446 | void | |
9447 | spa_sync_allpools(void) | |
9448 | { | |
9449 | spa_t *spa = NULL; | |
9450 | mutex_enter(&spa_namespace_lock); | |
9451 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
9452 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
9453 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
9454 | continue; |
9455 | spa_open_ref(spa, FTAG); | |
9456 | mutex_exit(&spa_namespace_lock); | |
9457 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9458 | mutex_enter(&spa_namespace_lock); | |
9459 | spa_close(spa, FTAG); | |
9460 | } | |
9461 | mutex_exit(&spa_namespace_lock); | |
9462 | } | |
9463 | ||
9464 | /* | |
9465 | * ========================================================================== | |
9466 | * Miscellaneous routines | |
9467 | * ========================================================================== | |
9468 | */ | |
9469 | ||
9470 | /* | |
9471 | * Remove all pools in the system. | |
9472 | */ | |
9473 | void | |
9474 | spa_evict_all(void) | |
9475 | { | |
9476 | spa_t *spa; | |
9477 | ||
9478 | /* | |
9479 | * Remove all cached state. All pools should be closed now, | |
9480 | * so every spa in the AVL tree should be unreferenced. | |
9481 | */ | |
9482 | mutex_enter(&spa_namespace_lock); | |
9483 | while ((spa = spa_next(NULL)) != NULL) { | |
9484 | /* | |
9485 | * Stop async tasks. The async thread may need to detach | |
9486 | * a device that's been replaced, which requires grabbing | |
9487 | * spa_namespace_lock, so we must drop it here. | |
9488 | */ | |
9489 | spa_open_ref(spa, FTAG); | |
9490 | mutex_exit(&spa_namespace_lock); | |
9491 | spa_async_suspend(spa); | |
9492 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
9493 | spa_close(spa, FTAG); |
9494 | ||
9495 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
9496 | spa_unload(spa); | |
9497 | spa_deactivate(spa); | |
9498 | } | |
9499 | spa_remove(spa); | |
9500 | } | |
9501 | mutex_exit(&spa_namespace_lock); | |
9502 | } | |
9503 | ||
9504 | vdev_t * | |
9babb374 | 9505 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 9506 | { |
b128c09f BB |
9507 | vdev_t *vd; |
9508 | int i; | |
9509 | ||
9510 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
9511 | return (vd); | |
9512 | ||
9babb374 | 9513 | if (aux) { |
b128c09f BB |
9514 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
9515 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
9516 | if (vd->vdev_guid == guid) |
9517 | return (vd); | |
9518 | } | |
9519 | ||
9520 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
9521 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
9522 | if (vd->vdev_guid == guid) |
9523 | return (vd); | |
9524 | } | |
9525 | } | |
9526 | ||
9527 | return (NULL); | |
34dc7c2f BB |
9528 | } |
9529 | ||
9530 | void | |
9531 | spa_upgrade(spa_t *spa, uint64_t version) | |
9532 | { | |
572e2857 BB |
9533 | ASSERT(spa_writeable(spa)); |
9534 | ||
b128c09f | 9535 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
9536 | |
9537 | /* | |
9538 | * This should only be called for a non-faulted pool, and since a | |
9539 | * future version would result in an unopenable pool, this shouldn't be | |
9540 | * possible. | |
9541 | */ | |
8dca0a9a | 9542 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
9b67f605 | 9543 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); |
34dc7c2f BB |
9544 | |
9545 | spa->spa_uberblock.ub_version = version; | |
9546 | vdev_config_dirty(spa->spa_root_vdev); | |
9547 | ||
b128c09f | 9548 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
9549 | |
9550 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9551 | } | |
9552 | ||
49d42425 FU |
9553 | static boolean_t |
9554 | spa_has_aux_vdev(spa_t *spa, uint64_t guid, spa_aux_vdev_t *sav) | |
34dc7c2f | 9555 | { |
14e4e3cb | 9556 | (void) spa; |
34dc7c2f | 9557 | int i; |
49d42425 | 9558 | uint64_t vdev_guid; |
34dc7c2f BB |
9559 | |
9560 | for (i = 0; i < sav->sav_count; i++) | |
9561 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
9562 | return (B_TRUE); | |
9563 | ||
9564 | for (i = 0; i < sav->sav_npending; i++) { | |
9565 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
49d42425 | 9566 | &vdev_guid) == 0 && vdev_guid == guid) |
34dc7c2f BB |
9567 | return (B_TRUE); |
9568 | } | |
9569 | ||
9570 | return (B_FALSE); | |
9571 | } | |
9572 | ||
49d42425 FU |
9573 | boolean_t |
9574 | spa_has_l2cache(spa_t *spa, uint64_t guid) | |
9575 | { | |
9576 | return (spa_has_aux_vdev(spa, guid, &spa->spa_l2cache)); | |
9577 | } | |
9578 | ||
9579 | boolean_t | |
9580 | spa_has_spare(spa_t *spa, uint64_t guid) | |
9581 | { | |
9582 | return (spa_has_aux_vdev(spa, guid, &spa->spa_spares)); | |
9583 | } | |
9584 | ||
b128c09f BB |
9585 | /* |
9586 | * Check if a pool has an active shared spare device. | |
9587 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
9588 | */ | |
9589 | static boolean_t | |
9590 | spa_has_active_shared_spare(spa_t *spa) | |
9591 | { | |
9592 | int i, refcnt; | |
9593 | uint64_t pool; | |
9594 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9595 | ||
9596 | for (i = 0; i < sav->sav_count; i++) { | |
9597 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
9598 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
9599 | refcnt > 2) | |
9600 | return (B_TRUE); | |
9601 | } | |
9602 | ||
9603 | return (B_FALSE); | |
9604 | } | |
9605 | ||
93e28d66 SD |
9606 | uint64_t |
9607 | spa_total_metaslabs(spa_t *spa) | |
9608 | { | |
9609 | vdev_t *rvd = spa->spa_root_vdev; | |
9610 | ||
9611 | uint64_t m = 0; | |
9612 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
9613 | vdev_t *vd = rvd->vdev_child[c]; | |
9614 | if (!vdev_is_concrete(vd)) | |
9615 | continue; | |
9616 | m += vd->vdev_ms_count; | |
9617 | } | |
9618 | return (m); | |
9619 | } | |
9620 | ||
e60e158e JG |
9621 | /* |
9622 | * Notify any waiting threads that some activity has switched from being in- | |
9623 | * progress to not-in-progress so that the thread can wake up and determine | |
9624 | * whether it is finished waiting. | |
9625 | */ | |
9626 | void | |
9627 | spa_notify_waiters(spa_t *spa) | |
9628 | { | |
9629 | /* | |
9630 | * Acquiring spa_activities_lock here prevents the cv_broadcast from | |
9631 | * happening between the waiting thread's check and cv_wait. | |
9632 | */ | |
9633 | mutex_enter(&spa->spa_activities_lock); | |
9634 | cv_broadcast(&spa->spa_activities_cv); | |
9635 | mutex_exit(&spa->spa_activities_lock); | |
9636 | } | |
9637 | ||
9638 | /* | |
9639 | * Notify any waiting threads that the pool is exporting, and then block until | |
9640 | * they are finished using the spa_t. | |
9641 | */ | |
9642 | void | |
9643 | spa_wake_waiters(spa_t *spa) | |
9644 | { | |
9645 | mutex_enter(&spa->spa_activities_lock); | |
9646 | spa->spa_waiters_cancel = B_TRUE; | |
9647 | cv_broadcast(&spa->spa_activities_cv); | |
9648 | while (spa->spa_waiters != 0) | |
9649 | cv_wait(&spa->spa_waiters_cv, &spa->spa_activities_lock); | |
9650 | spa->spa_waiters_cancel = B_FALSE; | |
9651 | mutex_exit(&spa->spa_activities_lock); | |
9652 | } | |
9653 | ||
2288d419 | 9654 | /* Whether the vdev or any of its descendants are being initialized/trimmed. */ |
e60e158e | 9655 | static boolean_t |
2288d419 | 9656 | spa_vdev_activity_in_progress_impl(vdev_t *vd, zpool_wait_activity_t activity) |
e60e158e JG |
9657 | { |
9658 | spa_t *spa = vd->vdev_spa; | |
e60e158e JG |
9659 | |
9660 | ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER)); | |
9661 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
2288d419 BB |
9662 | ASSERT(activity == ZPOOL_WAIT_INITIALIZE || |
9663 | activity == ZPOOL_WAIT_TRIM); | |
9664 | ||
9665 | kmutex_t *lock = activity == ZPOOL_WAIT_INITIALIZE ? | |
9666 | &vd->vdev_initialize_lock : &vd->vdev_trim_lock; | |
e60e158e JG |
9667 | |
9668 | mutex_exit(&spa->spa_activities_lock); | |
2288d419 | 9669 | mutex_enter(lock); |
e60e158e JG |
9670 | mutex_enter(&spa->spa_activities_lock); |
9671 | ||
2288d419 BB |
9672 | boolean_t in_progress = (activity == ZPOOL_WAIT_INITIALIZE) ? |
9673 | (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) : | |
9674 | (vd->vdev_trim_state == VDEV_TRIM_ACTIVE); | |
9675 | mutex_exit(lock); | |
e60e158e | 9676 | |
2288d419 | 9677 | if (in_progress) |
e60e158e JG |
9678 | return (B_TRUE); |
9679 | ||
9680 | for (int i = 0; i < vd->vdev_children; i++) { | |
2288d419 BB |
9681 | if (spa_vdev_activity_in_progress_impl(vd->vdev_child[i], |
9682 | activity)) | |
e60e158e JG |
9683 | return (B_TRUE); |
9684 | } | |
9685 | ||
9686 | return (B_FALSE); | |
9687 | } | |
9688 | ||
9689 | /* | |
9690 | * If use_guid is true, this checks whether the vdev specified by guid is | |
2288d419 BB |
9691 | * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool |
9692 | * is being initialized/trimmed. The caller must hold the config lock and | |
9693 | * spa_activities_lock. | |
e60e158e JG |
9694 | */ |
9695 | static int | |
2288d419 BB |
9696 | spa_vdev_activity_in_progress(spa_t *spa, boolean_t use_guid, uint64_t guid, |
9697 | zpool_wait_activity_t activity, boolean_t *in_progress) | |
e60e158e JG |
9698 | { |
9699 | mutex_exit(&spa->spa_activities_lock); | |
9700 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9701 | mutex_enter(&spa->spa_activities_lock); | |
9702 | ||
9703 | vdev_t *vd; | |
9704 | if (use_guid) { | |
9705 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
9706 | if (vd == NULL || !vd->vdev_ops->vdev_op_leaf) { | |
9707 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9708 | return (EINVAL); | |
9709 | } | |
9710 | } else { | |
9711 | vd = spa->spa_root_vdev; | |
9712 | } | |
9713 | ||
2288d419 | 9714 | *in_progress = spa_vdev_activity_in_progress_impl(vd, activity); |
e60e158e JG |
9715 | |
9716 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9717 | return (0); | |
9718 | } | |
9719 | ||
9720 | /* | |
9721 | * Locking for waiting threads | |
9722 | * --------------------------- | |
9723 | * | |
9724 | * Waiting threads need a way to check whether a given activity is in progress, | |
9725 | * and then, if it is, wait for it to complete. Each activity will have some | |
9726 | * in-memory representation of the relevant on-disk state which can be used to | |
9727 | * determine whether or not the activity is in progress. The in-memory state and | |
9728 | * the locking used to protect it will be different for each activity, and may | |
9729 | * not be suitable for use with a cvar (e.g., some state is protected by the | |
9730 | * config lock). To allow waiting threads to wait without any races, another | |
9731 | * lock, spa_activities_lock, is used. | |
9732 | * | |
9733 | * When the state is checked, both the activity-specific lock (if there is one) | |
9734 | * and spa_activities_lock are held. In some cases, the activity-specific lock | |
9735 | * is acquired explicitly (e.g. the config lock). In others, the locking is | |
9736 | * internal to some check (e.g. bpobj_is_empty). After checking, the waiting | |
9737 | * thread releases the activity-specific lock and, if the activity is in | |
9738 | * progress, then cv_waits using spa_activities_lock. | |
9739 | * | |
9740 | * The waiting thread is woken when another thread, one completing some | |
9741 | * activity, updates the state of the activity and then calls | |
9742 | * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only | |
9743 | * needs to hold its activity-specific lock when updating the state, and this | |
9744 | * lock can (but doesn't have to) be dropped before calling spa_notify_waiters. | |
9745 | * | |
9746 | * Because spa_notify_waiters acquires spa_activities_lock before broadcasting, | |
9747 | * and because it is held when the waiting thread checks the state of the | |
9748 | * activity, it can never be the case that the completing thread both updates | |
9749 | * the activity state and cv_broadcasts in between the waiting thread's check | |
9750 | * and cv_wait. Thus, a waiting thread can never miss a wakeup. | |
9751 | * | |
9752 | * In order to prevent deadlock, when the waiting thread does its check, in some | |
9753 | * cases it will temporarily drop spa_activities_lock in order to acquire the | |
9754 | * activity-specific lock. The order in which spa_activities_lock and the | |
9755 | * activity specific lock are acquired in the waiting thread is determined by | |
9756 | * the order in which they are acquired in the completing thread; if the | |
9757 | * completing thread calls spa_notify_waiters with the activity-specific lock | |
9758 | * held, then the waiting thread must also acquire the activity-specific lock | |
9759 | * first. | |
9760 | */ | |
9761 | ||
9762 | static int | |
9763 | spa_activity_in_progress(spa_t *spa, zpool_wait_activity_t activity, | |
9764 | boolean_t use_tag, uint64_t tag, boolean_t *in_progress) | |
9765 | { | |
9766 | int error = 0; | |
9767 | ||
9768 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
9769 | ||
9770 | switch (activity) { | |
9771 | case ZPOOL_WAIT_CKPT_DISCARD: | |
9772 | *in_progress = | |
9773 | (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT) && | |
9774 | zap_contains(spa_meta_objset(spa), | |
9775 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ZPOOL_CHECKPOINT) == | |
9776 | ENOENT); | |
9777 | break; | |
9778 | case ZPOOL_WAIT_FREE: | |
9779 | *in_progress = ((spa_version(spa) >= SPA_VERSION_DEADLISTS && | |
9780 | !bpobj_is_empty(&spa->spa_dsl_pool->dp_free_bpobj)) || | |
9781 | spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY) || | |
9782 | spa_livelist_delete_check(spa)); | |
9783 | break; | |
9784 | case ZPOOL_WAIT_INITIALIZE: | |
2288d419 BB |
9785 | case ZPOOL_WAIT_TRIM: |
9786 | error = spa_vdev_activity_in_progress(spa, use_tag, tag, | |
9787 | activity, in_progress); | |
e60e158e JG |
9788 | break; |
9789 | case ZPOOL_WAIT_REPLACE: | |
9790 | mutex_exit(&spa->spa_activities_lock); | |
9791 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9792 | mutex_enter(&spa->spa_activities_lock); | |
9793 | ||
9794 | *in_progress = vdev_replace_in_progress(spa->spa_root_vdev); | |
9795 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9796 | break; | |
9797 | case ZPOOL_WAIT_REMOVE: | |
9798 | *in_progress = (spa->spa_removing_phys.sr_state == | |
9799 | DSS_SCANNING); | |
9800 | break; | |
9801 | case ZPOOL_WAIT_RESILVER: | |
9a49d3f3 BB |
9802 | if ((*in_progress = vdev_rebuild_active(spa->spa_root_vdev))) |
9803 | break; | |
9a70e97f | 9804 | zfs_fallthrough; |
e60e158e JG |
9805 | case ZPOOL_WAIT_SCRUB: |
9806 | { | |
9807 | boolean_t scanning, paused, is_scrub; | |
9808 | dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; | |
9809 | ||
9810 | is_scrub = (scn->scn_phys.scn_func == POOL_SCAN_SCRUB); | |
9811 | scanning = (scn->scn_phys.scn_state == DSS_SCANNING); | |
9812 | paused = dsl_scan_is_paused_scrub(scn); | |
9813 | *in_progress = (scanning && !paused && | |
9814 | is_scrub == (activity == ZPOOL_WAIT_SCRUB)); | |
9815 | break; | |
9816 | } | |
9817 | default: | |
9818 | panic("unrecognized value for activity %d", activity); | |
9819 | } | |
9820 | ||
9821 | return (error); | |
9822 | } | |
9823 | ||
9824 | static int | |
9825 | spa_wait_common(const char *pool, zpool_wait_activity_t activity, | |
9826 | boolean_t use_tag, uint64_t tag, boolean_t *waited) | |
9827 | { | |
9828 | /* | |
9829 | * The tag is used to distinguish between instances of an activity. | |
2288d419 BB |
9830 | * 'initialize' and 'trim' are the only activities that we use this for. |
9831 | * The other activities can only have a single instance in progress in a | |
9832 | * pool at one time, making the tag unnecessary. | |
e60e158e JG |
9833 | * |
9834 | * There can be multiple devices being replaced at once, but since they | |
9835 | * all finish once resilvering finishes, we don't bother keeping track | |
9836 | * of them individually, we just wait for them all to finish. | |
9837 | */ | |
2288d419 BB |
9838 | if (use_tag && activity != ZPOOL_WAIT_INITIALIZE && |
9839 | activity != ZPOOL_WAIT_TRIM) | |
e60e158e JG |
9840 | return (EINVAL); |
9841 | ||
9842 | if (activity < 0 || activity >= ZPOOL_WAIT_NUM_ACTIVITIES) | |
9843 | return (EINVAL); | |
9844 | ||
9845 | spa_t *spa; | |
9846 | int error = spa_open(pool, &spa, FTAG); | |
9847 | if (error != 0) | |
9848 | return (error); | |
9849 | ||
9850 | /* | |
9851 | * Increment the spa's waiter count so that we can call spa_close and | |
9852 | * still ensure that the spa_t doesn't get freed before this thread is | |
9853 | * finished with it when the pool is exported. We want to call spa_close | |
9854 | * before we start waiting because otherwise the additional ref would | |
9855 | * prevent the pool from being exported or destroyed throughout the | |
9856 | * potentially long wait. | |
9857 | */ | |
9858 | mutex_enter(&spa->spa_activities_lock); | |
9859 | spa->spa_waiters++; | |
9860 | spa_close(spa, FTAG); | |
9861 | ||
9862 | *waited = B_FALSE; | |
9863 | for (;;) { | |
9864 | boolean_t in_progress; | |
9865 | error = spa_activity_in_progress(spa, activity, use_tag, tag, | |
9866 | &in_progress); | |
9867 | ||
b24771a8 | 9868 | if (error || !in_progress || spa->spa_waiters_cancel) |
e60e158e JG |
9869 | break; |
9870 | ||
9871 | *waited = B_TRUE; | |
9872 | ||
9873 | if (cv_wait_sig(&spa->spa_activities_cv, | |
9874 | &spa->spa_activities_lock) == 0) { | |
9875 | error = EINTR; | |
9876 | break; | |
9877 | } | |
9878 | } | |
9879 | ||
9880 | spa->spa_waiters--; | |
9881 | cv_signal(&spa->spa_waiters_cv); | |
9882 | mutex_exit(&spa->spa_activities_lock); | |
9883 | ||
9884 | return (error); | |
9885 | } | |
9886 | ||
9887 | /* | |
9888 | * Wait for a particular instance of the specified activity to complete, where | |
9889 | * the instance is identified by 'tag' | |
9890 | */ | |
9891 | int | |
9892 | spa_wait_tag(const char *pool, zpool_wait_activity_t activity, uint64_t tag, | |
9893 | boolean_t *waited) | |
9894 | { | |
9895 | return (spa_wait_common(pool, activity, B_TRUE, tag, waited)); | |
9896 | } | |
9897 | ||
9898 | /* | |
9899 | * Wait for all instances of the specified activity complete | |
9900 | */ | |
9901 | int | |
9902 | spa_wait(const char *pool, zpool_wait_activity_t activity, boolean_t *waited) | |
9903 | { | |
9904 | ||
9905 | return (spa_wait_common(pool, activity, B_FALSE, 0, waited)); | |
9906 | } | |
9907 | ||
a1d477c2 | 9908 | sysevent_t * |
12fa0466 DE |
9909 | spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
9910 | { | |
9911 | sysevent_t *ev = NULL; | |
9912 | #ifdef _KERNEL | |
9913 | nvlist_t *resource; | |
9914 | ||
9915 | resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl); | |
9916 | if (resource) { | |
9917 | ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP); | |
9918 | ev->resource = resource; | |
9919 | } | |
14e4e3cb AZ |
9920 | #else |
9921 | (void) spa, (void) vd, (void) hist_nvl, (void) name; | |
12fa0466 DE |
9922 | #endif |
9923 | return (ev); | |
9924 | } | |
9925 | ||
a1d477c2 | 9926 | void |
12fa0466 DE |
9927 | spa_event_post(sysevent_t *ev) |
9928 | { | |
9929 | #ifdef _KERNEL | |
9930 | if (ev) { | |
9931 | zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb); | |
9932 | kmem_free(ev, sizeof (*ev)); | |
9933 | } | |
14e4e3cb AZ |
9934 | #else |
9935 | (void) ev; | |
12fa0466 DE |
9936 | #endif |
9937 | } | |
9938 | ||
34dc7c2f | 9939 | /* |
fb390aaf HR |
9940 | * Post a zevent corresponding to the given sysevent. The 'name' must be one |
9941 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
34dc7c2f BB |
9942 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
9943 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
9944 | * or zdb as real changes. | |
9945 | */ | |
9946 | void | |
12fa0466 | 9947 | spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
34dc7c2f | 9948 | { |
12fa0466 | 9949 | spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); |
34dc7c2f | 9950 | } |
c28b2279 | 9951 | |
c28b2279 BB |
9952 | /* state manipulation functions */ |
9953 | EXPORT_SYMBOL(spa_open); | |
9954 | EXPORT_SYMBOL(spa_open_rewind); | |
9955 | EXPORT_SYMBOL(spa_get_stats); | |
9956 | EXPORT_SYMBOL(spa_create); | |
c28b2279 BB |
9957 | EXPORT_SYMBOL(spa_import); |
9958 | EXPORT_SYMBOL(spa_tryimport); | |
9959 | EXPORT_SYMBOL(spa_destroy); | |
9960 | EXPORT_SYMBOL(spa_export); | |
9961 | EXPORT_SYMBOL(spa_reset); | |
9962 | EXPORT_SYMBOL(spa_async_request); | |
9963 | EXPORT_SYMBOL(spa_async_suspend); | |
9964 | EXPORT_SYMBOL(spa_async_resume); | |
9965 | EXPORT_SYMBOL(spa_inject_addref); | |
9966 | EXPORT_SYMBOL(spa_inject_delref); | |
9967 | EXPORT_SYMBOL(spa_scan_stat_init); | |
9968 | EXPORT_SYMBOL(spa_scan_get_stats); | |
9969 | ||
e1cfd73f | 9970 | /* device manipulation */ |
c28b2279 BB |
9971 | EXPORT_SYMBOL(spa_vdev_add); |
9972 | EXPORT_SYMBOL(spa_vdev_attach); | |
9973 | EXPORT_SYMBOL(spa_vdev_detach); | |
c28b2279 BB |
9974 | EXPORT_SYMBOL(spa_vdev_setpath); |
9975 | EXPORT_SYMBOL(spa_vdev_setfru); | |
9976 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
9977 | ||
9978 | /* spare statech is global across all pools) */ | |
9979 | EXPORT_SYMBOL(spa_spare_add); | |
9980 | EXPORT_SYMBOL(spa_spare_remove); | |
9981 | EXPORT_SYMBOL(spa_spare_exists); | |
9982 | EXPORT_SYMBOL(spa_spare_activate); | |
9983 | ||
9984 | /* L2ARC statech is global across all pools) */ | |
9985 | EXPORT_SYMBOL(spa_l2cache_add); | |
9986 | EXPORT_SYMBOL(spa_l2cache_remove); | |
9987 | EXPORT_SYMBOL(spa_l2cache_exists); | |
9988 | EXPORT_SYMBOL(spa_l2cache_activate); | |
9989 | EXPORT_SYMBOL(spa_l2cache_drop); | |
9990 | ||
9991 | /* scanning */ | |
9992 | EXPORT_SYMBOL(spa_scan); | |
9993 | EXPORT_SYMBOL(spa_scan_stop); | |
9994 | ||
9995 | /* spa syncing */ | |
9996 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
9997 | EXPORT_SYMBOL(spa_sync_allpools); | |
9998 | ||
9999 | /* properties */ | |
10000 | EXPORT_SYMBOL(spa_prop_set); | |
10001 | EXPORT_SYMBOL(spa_prop_get); | |
10002 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
10003 | ||
10004 | /* asynchronous event notification */ | |
10005 | EXPORT_SYMBOL(spa_event_notify); | |
dea377c0 | 10006 | |
c8242a96 | 10007 | /* BEGIN CSTYLED */ |
fdc2d303 | 10008 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_shift, UINT, ZMOD_RW, |
458f8231 | 10009 | "log2 fraction of arc that can be used by inflight I/Os when " |
03fdcb9a | 10010 | "verifying pool during import"); |
7ada752a | 10011 | /* END CSTYLED */ |
dea377c0 | 10012 | |
03fdcb9a | 10013 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_metadata, INT, ZMOD_RW, |
dea377c0 MA |
10014 | "Set to traverse metadata on pool import"); |
10015 | ||
03fdcb9a | 10016 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_data, INT, ZMOD_RW, |
dea377c0 | 10017 | "Set to traverse data on pool import"); |
dcb6bed1 | 10018 | |
03fdcb9a | 10019 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_print_vdev_tree, INT, ZMOD_RW, |
6cb8e530 PZ |
10020 | "Print vdev tree to zfs_dbgmsg during pool import"); |
10021 | ||
03fdcb9a | 10022 | ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_pct, UINT, ZMOD_RD, |
dcb6bed1 D |
10023 | "Percentage of CPUs to run an IO worker thread"); |
10024 | ||
7457b024 AM |
10025 | ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_tpq, UINT, ZMOD_RD, |
10026 | "Number of threads per IO worker taskqueue"); | |
10027 | ||
7ada752a | 10028 | /* BEGIN CSTYLED */ |
ab8d9c17 | 10029 | ZFS_MODULE_PARAM(zfs, zfs_, max_missing_tvds, U64, ZMOD_RW, |
03fdcb9a MM |
10030 | "Allow importing pool with up to this number of missing top-level " |
10031 | "vdevs (in read-only mode)"); | |
7ada752a | 10032 | /* END CSTYLED */ |
6cb8e530 | 10033 | |
7ada752a AZ |
10034 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_pause, INT, |
10035 | ZMOD_RW, "Set the livelist condense zthr to pause"); | |
03fdcb9a | 10036 | |
7ada752a AZ |
10037 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_pause, INT, |
10038 | ZMOD_RW, "Set the livelist condense synctask to pause"); | |
37f03da8 | 10039 | |
7ada752a AZ |
10040 | /* BEGIN CSTYLED */ |
10041 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_cancel, | |
10042 | INT, ZMOD_RW, | |
37f03da8 | 10043 | "Whether livelist condensing was canceled in the synctask"); |
03fdcb9a | 10044 | |
7ada752a AZ |
10045 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_cancel, |
10046 | INT, ZMOD_RW, | |
37f03da8 SH |
10047 | "Whether livelist condensing was canceled in the zthr function"); |
10048 | ||
7ada752a AZ |
10049 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, new_alloc, INT, |
10050 | ZMOD_RW, | |
03fdcb9a MM |
10051 | "Whether extra ALLOC blkptrs were added to a livelist entry while it " |
10052 | "was being condensed"); | |
37f03da8 | 10053 | /* END CSTYLED */ |