]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
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> | |
34dc7c2f | 84 | #include <sys/spa_boot.h> |
9babb374 | 85 | #include <sys/zfs_ioctl.h> |
428870ff | 86 | #include <sys/dsl_scan.h> |
9ae529ec | 87 | #include <sys/zfeature.h> |
13fe0198 | 88 | #include <sys/dsl_destroy.h> |
526af785 | 89 | #include <sys/zvol.h> |
34dc7c2f | 90 | |
d164b209 | 91 | #ifdef _KERNEL |
12fa0466 DE |
92 | #include <sys/fm/protocol.h> |
93 | #include <sys/fm/util.h> | |
428870ff | 94 | #include <sys/callb.h> |
d164b209 | 95 | #include <sys/zone.h> |
c8242a96 | 96 | #include <sys/vmsystm.h> |
d164b209 BB |
97 | #endif /* _KERNEL */ |
98 | ||
34dc7c2f BB |
99 | #include "zfs_prop.h" |
100 | #include "zfs_comutil.h" | |
101 | ||
e6cfd633 WA |
102 | /* |
103 | * The interval, in seconds, at which failed configuration cache file writes | |
104 | * should be retried. | |
105 | */ | |
a1d477c2 | 106 | int zfs_ccw_retry_interval = 300; |
e6cfd633 | 107 | |
428870ff | 108 | typedef enum zti_modes { |
7ef5e54e | 109 | ZTI_MODE_FIXED, /* value is # of threads (min 1) */ |
7ef5e54e | 110 | ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ |
7457b024 | 111 | ZTI_MODE_SCALE, /* Taskqs scale with CPUs. */ |
7ef5e54e AL |
112 | ZTI_MODE_NULL, /* don't create a taskq */ |
113 | ZTI_NMODES | |
428870ff | 114 | } zti_modes_t; |
34dc7c2f | 115 | |
7ef5e54e AL |
116 | #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } |
117 | #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } | |
118 | #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } | |
7457b024 | 119 | #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 } |
7ef5e54e | 120 | #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } |
9babb374 | 121 | |
7ef5e54e AL |
122 | #define ZTI_N(n) ZTI_P(n, 1) |
123 | #define ZTI_ONE ZTI_N(1) | |
9babb374 BB |
124 | |
125 | typedef struct zio_taskq_info { | |
7ef5e54e | 126 | zti_modes_t zti_mode; |
428870ff | 127 | uint_t zti_value; |
7ef5e54e | 128 | uint_t zti_count; |
9babb374 BB |
129 | } zio_taskq_info_t; |
130 | ||
131 | static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { | |
451041db | 132 | "iss", "iss_h", "int", "int_h" |
9babb374 BB |
133 | }; |
134 | ||
428870ff | 135 | /* |
7ef5e54e AL |
136 | * This table defines the taskq settings for each ZFS I/O type. When |
137 | * initializing a pool, we use this table to create an appropriately sized | |
138 | * taskq. Some operations are low volume and therefore have a small, static | |
139 | * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE | |
140 | * macros. Other operations process a large amount of data; the ZTI_BATCH | |
141 | * macro causes us to create a taskq oriented for throughput. Some operations | |
1b939560 | 142 | * are so high frequency and short-lived that the taskq itself can become a |
7ef5e54e AL |
143 | * point of lock contention. The ZTI_P(#, #) macro indicates that we need an |
144 | * additional degree of parallelism specified by the number of threads per- | |
145 | * taskq and the number of taskqs; when dispatching an event in this case, the | |
7457b024 AM |
146 | * particular taskq is chosen at random. ZTI_SCALE is similar to ZTI_BATCH, |
147 | * but with number of taskqs also scaling with number of CPUs. | |
7ef5e54e AL |
148 | * |
149 | * The different taskq priorities are to handle the different contexts (issue | |
150 | * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that | |
151 | * need to be handled with minimum delay. | |
428870ff | 152 | */ |
18168da7 | 153 | static const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { |
428870ff | 154 | /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ |
7ef5e54e | 155 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ |
7457b024 AM |
156 | { ZTI_N(8), ZTI_NULL, ZTI_SCALE, ZTI_NULL }, /* READ */ |
157 | { ZTI_BATCH, ZTI_N(5), ZTI_SCALE, ZTI_N(5) }, /* WRITE */ | |
158 | { ZTI_SCALE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ | |
7ef5e54e AL |
159 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ |
160 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ | |
1b939560 | 161 | { ZTI_N(4), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* TRIM */ |
9babb374 BB |
162 | }; |
163 | ||
13fe0198 MA |
164 | static void spa_sync_version(void *arg, dmu_tx_t *tx); |
165 | static void spa_sync_props(void *arg, dmu_tx_t *tx); | |
b128c09f | 166 | static boolean_t spa_has_active_shared_spare(spa_t *spa); |
d2734cce | 167 | static int spa_load_impl(spa_t *spa, spa_import_type_t type, 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 | */ | |
221 | unsigned long zfs_max_missing_tvds = 0; | |
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 | |
280 | spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, | |
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) { |
a1d477c2 | 912 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 913 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); |
3bc7e0fb | 914 | } |
3541dc6d | 915 | |
3bc7e0fb | 916 | mutex_exit(&spa_namespace_lock); |
621dd7bb | 917 | mutex_exit(&spa->spa_vdev_top_lock); |
3541dc6d | 918 | |
3bc7e0fb | 919 | return (error); |
3541dc6d GA |
920 | } |
921 | ||
34dc7c2f BB |
922 | /* |
923 | * ========================================================================== | |
924 | * SPA state manipulation (open/create/destroy/import/export) | |
925 | * ========================================================================== | |
926 | */ | |
927 | ||
928 | static int | |
929 | spa_error_entry_compare(const void *a, const void *b) | |
930 | { | |
ee36c709 GN |
931 | const spa_error_entry_t *sa = (const spa_error_entry_t *)a; |
932 | const spa_error_entry_t *sb = (const spa_error_entry_t *)b; | |
34dc7c2f BB |
933 | int ret; |
934 | ||
ee36c709 | 935 | ret = memcmp(&sa->se_bookmark, &sb->se_bookmark, |
5dbd68a3 | 936 | sizeof (zbookmark_phys_t)); |
34dc7c2f | 937 | |
ca577779 | 938 | return (TREE_ISIGN(ret)); |
34dc7c2f BB |
939 | } |
940 | ||
941 | /* | |
942 | * Utility function which retrieves copies of the current logs and | |
943 | * re-initializes them in the process. | |
944 | */ | |
945 | void | |
946 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
947 | { | |
948 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
949 | ||
950 | bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); | |
951 | bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); | |
952 | ||
953 | avl_create(&spa->spa_errlist_scrub, | |
954 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
955 | offsetof(spa_error_entry_t, se_avl)); | |
956 | avl_create(&spa->spa_errlist_last, | |
957 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
958 | offsetof(spa_error_entry_t, se_avl)); | |
959 | } | |
960 | ||
7ef5e54e AL |
961 | static void |
962 | spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
34dc7c2f | 963 | { |
7ef5e54e AL |
964 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; |
965 | enum zti_modes mode = ztip->zti_mode; | |
966 | uint_t value = ztip->zti_value; | |
967 | uint_t count = ztip->zti_count; | |
968 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7457b024 | 969 | uint_t cpus, flags = TASKQ_DYNAMIC; |
428870ff | 970 | boolean_t batch = B_FALSE; |
34dc7c2f | 971 | |
e8b96c60 MA |
972 | switch (mode) { |
973 | case ZTI_MODE_FIXED: | |
7457b024 | 974 | ASSERT3U(value, >, 0); |
e8b96c60 | 975 | break; |
7ef5e54e | 976 | |
e8b96c60 MA |
977 | case ZTI_MODE_BATCH: |
978 | batch = B_TRUE; | |
979 | flags |= TASKQ_THREADS_CPU_PCT; | |
dcb6bed1 | 980 | value = MIN(zio_taskq_batch_pct, 100); |
e8b96c60 | 981 | break; |
7ef5e54e | 982 | |
7457b024 AM |
983 | case ZTI_MODE_SCALE: |
984 | flags |= TASKQ_THREADS_CPU_PCT; | |
985 | /* | |
986 | * We want more taskqs to reduce lock contention, but we want | |
987 | * less for better request ordering and CPU utilization. | |
988 | */ | |
989 | cpus = MAX(1, boot_ncpus * zio_taskq_batch_pct / 100); | |
990 | if (zio_taskq_batch_tpq > 0) { | |
991 | count = MAX(1, (cpus + zio_taskq_batch_tpq / 2) / | |
992 | zio_taskq_batch_tpq); | |
993 | } else { | |
994 | /* | |
995 | * Prefer 6 threads per taskq, but no more taskqs | |
996 | * than threads in them on large systems. For 80%: | |
997 | * | |
998 | * taskq taskq total | |
999 | * cpus taskqs percent threads threads | |
1000 | * ------- ------- ------- ------- ------- | |
1001 | * 1 1 80% 1 1 | |
1002 | * 2 1 80% 1 1 | |
1003 | * 4 1 80% 3 3 | |
1004 | * 8 2 40% 3 6 | |
1005 | * 16 3 27% 4 12 | |
1006 | * 32 5 16% 5 25 | |
1007 | * 64 7 11% 7 49 | |
1008 | * 128 10 8% 10 100 | |
1009 | * 256 14 6% 15 210 | |
1010 | */ | |
1011 | count = 1 + cpus / 6; | |
1012 | while (count * count > cpus) | |
1013 | count--; | |
1014 | } | |
1015 | /* Limit each taskq within 100% to not trigger assertion. */ | |
1016 | count = MAX(count, (zio_taskq_batch_pct + 99) / 100); | |
1017 | value = (zio_taskq_batch_pct + count / 2) / count; | |
1018 | break; | |
1019 | ||
1020 | case ZTI_MODE_NULL: | |
1021 | tqs->stqs_count = 0; | |
1022 | tqs->stqs_taskq = NULL; | |
1023 | return; | |
1024 | ||
e8b96c60 MA |
1025 | default: |
1026 | panic("unrecognized mode for %s_%s taskq (%u:%u) in " | |
1027 | "spa_activate()", | |
1028 | zio_type_name[t], zio_taskq_types[q], mode, value); | |
1029 | break; | |
1030 | } | |
7ef5e54e | 1031 | |
7457b024 AM |
1032 | ASSERT3U(count, >, 0); |
1033 | tqs->stqs_count = count; | |
1034 | tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); | |
1035 | ||
1c27024e | 1036 | for (uint_t i = 0; i < count; i++) { |
e8b96c60 | 1037 | taskq_t *tq; |
af430294 | 1038 | char name[32]; |
7ef5e54e | 1039 | |
7457b024 AM |
1040 | if (count > 1) |
1041 | (void) snprintf(name, sizeof (name), "%s_%s_%u", | |
1042 | zio_type_name[t], zio_taskq_types[q], i); | |
1043 | else | |
1044 | (void) snprintf(name, sizeof (name), "%s_%s", | |
1045 | zio_type_name[t], zio_taskq_types[q]); | |
7ef5e54e AL |
1046 | |
1047 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { | |
1048 | if (batch) | |
1049 | flags |= TASKQ_DC_BATCH; | |
1050 | ||
18168da7 | 1051 | (void) zio_taskq_basedc; |
7ef5e54e AL |
1052 | tq = taskq_create_sysdc(name, value, 50, INT_MAX, |
1053 | spa->spa_proc, zio_taskq_basedc, flags); | |
1054 | } else { | |
e8b96c60 MA |
1055 | pri_t pri = maxclsyspri; |
1056 | /* | |
1057 | * The write issue taskq can be extremely CPU | |
1229323d | 1058 | * intensive. Run it at slightly less important |
7432d297 MM |
1059 | * priority than the other taskqs. |
1060 | * | |
1061 | * Under Linux and FreeBSD this means incrementing | |
1062 | * the priority value as opposed to platforms like | |
1063 | * illumos where it should be decremented. | |
1064 | * | |
1065 | * On FreeBSD, if priorities divided by four (RQ_PPQ) | |
1066 | * are equal then a difference between them is | |
1067 | * insignificant. | |
e8b96c60 | 1068 | */ |
7432d297 MM |
1069 | if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) { |
1070 | #if defined(__linux__) | |
1229323d | 1071 | pri++; |
7432d297 MM |
1072 | #elif defined(__FreeBSD__) |
1073 | pri += 4; | |
1074 | #else | |
1075 | #error "unknown OS" | |
1076 | #endif | |
1077 | } | |
e8b96c60 | 1078 | tq = taskq_create_proc(name, value, pri, 50, |
7ef5e54e AL |
1079 | INT_MAX, spa->spa_proc, flags); |
1080 | } | |
1081 | ||
1082 | tqs->stqs_taskq[i] = tq; | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | static void | |
1087 | spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
1088 | { | |
1089 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7ef5e54e AL |
1090 | |
1091 | if (tqs->stqs_taskq == NULL) { | |
1092 | ASSERT3U(tqs->stqs_count, ==, 0); | |
1093 | return; | |
1094 | } | |
1095 | ||
1c27024e | 1096 | for (uint_t i = 0; i < tqs->stqs_count; i++) { |
7ef5e54e AL |
1097 | ASSERT3P(tqs->stqs_taskq[i], !=, NULL); |
1098 | taskq_destroy(tqs->stqs_taskq[i]); | |
428870ff | 1099 | } |
34dc7c2f | 1100 | |
7ef5e54e AL |
1101 | kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); |
1102 | tqs->stqs_taskq = NULL; | |
1103 | } | |
34dc7c2f | 1104 | |
7ef5e54e AL |
1105 | /* |
1106 | * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. | |
1107 | * Note that a type may have multiple discrete taskqs to avoid lock contention | |
1108 | * on the taskq itself. In that case we choose which taskq at random by using | |
1109 | * the low bits of gethrtime(). | |
1110 | */ | |
1111 | void | |
1112 | spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1113 | task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) | |
1114 | { | |
1115 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1116 | taskq_t *tq; | |
1117 | ||
1118 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1119 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1120 | ||
1121 | if (tqs->stqs_count == 1) { | |
1122 | tq = tqs->stqs_taskq[0]; | |
1123 | } else { | |
c12936b1 | 1124 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
428870ff | 1125 | } |
7ef5e54e AL |
1126 | |
1127 | taskq_dispatch_ent(tq, func, arg, flags, ent); | |
428870ff BB |
1128 | } |
1129 | ||
044baf00 BB |
1130 | /* |
1131 | * Same as spa_taskq_dispatch_ent() but block on the task until completion. | |
1132 | */ | |
1133 | void | |
1134 | spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1135 | task_func_t *func, void *arg, uint_t flags) | |
1136 | { | |
1137 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1138 | taskq_t *tq; | |
1139 | taskqid_t id; | |
1140 | ||
1141 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1142 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1143 | ||
1144 | if (tqs->stqs_count == 1) { | |
1145 | tq = tqs->stqs_taskq[0]; | |
1146 | } else { | |
c12936b1 | 1147 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
044baf00 BB |
1148 | } |
1149 | ||
1150 | id = taskq_dispatch(tq, func, arg, flags); | |
1151 | if (id) | |
1152 | taskq_wait_id(tq, id); | |
1153 | } | |
1154 | ||
428870ff BB |
1155 | static void |
1156 | spa_create_zio_taskqs(spa_t *spa) | |
1157 | { | |
1c27024e DB |
1158 | for (int t = 0; t < ZIO_TYPES; t++) { |
1159 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1160 | spa_taskqs_init(spa, t, q); |
428870ff BB |
1161 | } |
1162 | } | |
1163 | } | |
9babb374 | 1164 | |
c25b8f99 BB |
1165 | /* |
1166 | * Disabled until spa_thread() can be adapted for Linux. | |
1167 | */ | |
1168 | #undef HAVE_SPA_THREAD | |
1169 | ||
7b89a549 | 1170 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) |
428870ff BB |
1171 | static void |
1172 | spa_thread(void *arg) | |
1173 | { | |
93ce2b4c | 1174 | psetid_t zio_taskq_psrset_bind = PS_NONE; |
428870ff | 1175 | callb_cpr_t cprinfo; |
9babb374 | 1176 | |
428870ff BB |
1177 | spa_t *spa = arg; |
1178 | user_t *pu = PTOU(curproc); | |
9babb374 | 1179 | |
428870ff BB |
1180 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, |
1181 | spa->spa_name); | |
9babb374 | 1182 | |
428870ff BB |
1183 | ASSERT(curproc != &p0); |
1184 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
1185 | "zpool-%s", spa->spa_name); | |
1186 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
1187 | ||
1188 | /* bind this thread to the requested psrset */ | |
1189 | if (zio_taskq_psrset_bind != PS_NONE) { | |
1190 | pool_lock(); | |
1191 | mutex_enter(&cpu_lock); | |
1192 | mutex_enter(&pidlock); | |
1193 | mutex_enter(&curproc->p_lock); | |
1194 | ||
1195 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
1196 | 0, NULL, NULL) == 0) { | |
1197 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
1198 | } else { | |
1199 | cmn_err(CE_WARN, | |
1200 | "Couldn't bind process for zfs pool \"%s\" to " | |
1201 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
1202 | } | |
1203 | ||
1204 | mutex_exit(&curproc->p_lock); | |
1205 | mutex_exit(&pidlock); | |
1206 | mutex_exit(&cpu_lock); | |
1207 | pool_unlock(); | |
1208 | } | |
1209 | ||
1210 | if (zio_taskq_sysdc) { | |
1211 | sysdc_thread_enter(curthread, 100, 0); | |
1212 | } | |
1213 | ||
1214 | spa->spa_proc = curproc; | |
1215 | spa->spa_did = curthread->t_did; | |
1216 | ||
1217 | spa_create_zio_taskqs(spa); | |
1218 | ||
1219 | mutex_enter(&spa->spa_proc_lock); | |
1220 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
1221 | ||
1222 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
1223 | cv_broadcast(&spa->spa_proc_cv); | |
1224 | ||
1225 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
1226 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
1227 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1228 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
1229 | ||
1230 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
1231 | spa->spa_proc_state = SPA_PROC_GONE; | |
1232 | spa->spa_proc = &p0; | |
1233 | cv_broadcast(&spa->spa_proc_cv); | |
1234 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
1235 | ||
1236 | mutex_enter(&curproc->p_lock); | |
1237 | lwp_exit(); | |
1238 | } | |
1239 | #endif | |
1240 | ||
1241 | /* | |
1242 | * Activate an uninitialized pool. | |
1243 | */ | |
1244 | static void | |
da92d5cb | 1245 | spa_activate(spa_t *spa, spa_mode_t mode) |
428870ff BB |
1246 | { |
1247 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); | |
1248 | ||
1249 | spa->spa_state = POOL_STATE_ACTIVE; | |
1250 | spa->spa_mode = mode; | |
e39fe05b | 1251 | spa->spa_read_spacemaps = spa_mode_readable_spacemaps; |
428870ff | 1252 | |
18168da7 AZ |
1253 | spa->spa_normal_class = metaslab_class_create(spa, &zfs_metaslab_ops); |
1254 | spa->spa_log_class = metaslab_class_create(spa, &zfs_metaslab_ops); | |
aa755b35 | 1255 | spa->spa_embedded_log_class = |
18168da7 AZ |
1256 | metaslab_class_create(spa, &zfs_metaslab_ops); |
1257 | spa->spa_special_class = metaslab_class_create(spa, &zfs_metaslab_ops); | |
1258 | spa->spa_dedup_class = metaslab_class_create(spa, &zfs_metaslab_ops); | |
428870ff BB |
1259 | |
1260 | /* Try to create a covering process */ | |
1261 | mutex_enter(&spa->spa_proc_lock); | |
1262 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
1263 | ASSERT(spa->spa_proc == &p0); | |
1264 | spa->spa_did = 0; | |
1265 | ||
18168da7 | 1266 | (void) spa_create_process; |
7b89a549 | 1267 | #ifdef HAVE_SPA_THREAD |
428870ff BB |
1268 | /* Only create a process if we're going to be around a while. */ |
1269 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
1270 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
1271 | NULL, 0) == 0) { | |
1272 | spa->spa_proc_state = SPA_PROC_CREATED; | |
1273 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
1274 | cv_wait(&spa->spa_proc_cv, | |
1275 | &spa->spa_proc_lock); | |
9babb374 | 1276 | } |
428870ff BB |
1277 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); |
1278 | ASSERT(spa->spa_proc != &p0); | |
1279 | ASSERT(spa->spa_did != 0); | |
1280 | } else { | |
1281 | #ifdef _KERNEL | |
1282 | cmn_err(CE_WARN, | |
1283 | "Couldn't create process for zfs pool \"%s\"\n", | |
1284 | spa->spa_name); | |
1285 | #endif | |
b128c09f | 1286 | } |
34dc7c2f | 1287 | } |
7b89a549 | 1288 | #endif /* HAVE_SPA_THREAD */ |
428870ff BB |
1289 | mutex_exit(&spa->spa_proc_lock); |
1290 | ||
1291 | /* If we didn't create a process, we need to create our taskqs. */ | |
1292 | if (spa->spa_proc == &p0) { | |
1293 | spa_create_zio_taskqs(spa); | |
1294 | } | |
34dc7c2f | 1295 | |
619f0976 GW |
1296 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1297 | spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, | |
1298 | ZIO_FLAG_CANFAIL); | |
1299 | } | |
a1d477c2 | 1300 | |
b128c09f BB |
1301 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), |
1302 | offsetof(vdev_t, vdev_config_dirty_node)); | |
0c66c32d JG |
1303 | list_create(&spa->spa_evicting_os_list, sizeof (objset_t), |
1304 | offsetof(objset_t, os_evicting_node)); | |
b128c09f BB |
1305 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), |
1306 | offsetof(vdev_t, vdev_state_dirty_node)); | |
34dc7c2f | 1307 | |
4747a7d3 | 1308 | txg_list_create(&spa->spa_vdev_txg_list, spa, |
34dc7c2f BB |
1309 | offsetof(struct vdev, vdev_txg_node)); |
1310 | ||
1311 | avl_create(&spa->spa_errlist_scrub, | |
1312 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1313 | offsetof(spa_error_entry_t, se_avl)); | |
1314 | avl_create(&spa->spa_errlist_last, | |
1315 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1316 | offsetof(spa_error_entry_t, se_avl)); | |
a0bd735a | 1317 | |
b5256303 TC |
1318 | spa_keystore_init(&spa->spa_keystore); |
1319 | ||
a0bd735a BP |
1320 | /* |
1321 | * This taskq is used to perform zvol-minor-related tasks | |
1322 | * asynchronously. This has several advantages, including easy | |
d0249a4b | 1323 | * resolution of various deadlocks. |
a0bd735a BP |
1324 | * |
1325 | * The taskq must be single threaded to ensure tasks are always | |
1326 | * processed in the order in which they were dispatched. | |
1327 | * | |
1328 | * A taskq per pool allows one to keep the pools independent. | |
1329 | * This way if one pool is suspended, it will not impact another. | |
1330 | * | |
1331 | * The preferred location to dispatch a zvol minor task is a sync | |
1332 | * task. In this context, there is easy access to the spa_t and minimal | |
1333 | * error handling is required because the sync task must succeed. | |
1334 | */ | |
1335 | spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri, | |
1336 | 1, INT_MAX, 0); | |
1de321e6 | 1337 | |
77d8a0f1 | 1338 | /* |
1339 | * Taskq dedicated to prefetcher threads: this is used to prevent the | |
1340 | * pool traverse code from monopolizing the global (and limited) | |
1341 | * system_taskq by inappropriately scheduling long running tasks on it. | |
1342 | */ | |
60a4c7d2 PD |
1343 | spa->spa_prefetch_taskq = taskq_create("z_prefetch", 100, |
1344 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); | |
77d8a0f1 | 1345 | |
1de321e6 JX |
1346 | /* |
1347 | * The taskq to upgrade datasets in this pool. Currently used by | |
9c5167d1 | 1348 | * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA. |
1de321e6 | 1349 | */ |
60a4c7d2 PD |
1350 | spa->spa_upgrade_taskq = taskq_create("z_upgrade", 100, |
1351 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); | |
34dc7c2f BB |
1352 | } |
1353 | ||
1354 | /* | |
1355 | * Opposite of spa_activate(). | |
1356 | */ | |
1357 | static void | |
1358 | spa_deactivate(spa_t *spa) | |
1359 | { | |
34dc7c2f BB |
1360 | ASSERT(spa->spa_sync_on == B_FALSE); |
1361 | ASSERT(spa->spa_dsl_pool == NULL); | |
1362 | ASSERT(spa->spa_root_vdev == NULL); | |
9babb374 | 1363 | ASSERT(spa->spa_async_zio_root == NULL); |
34dc7c2f BB |
1364 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); |
1365 | ||
0c66c32d JG |
1366 | spa_evicting_os_wait(spa); |
1367 | ||
a0bd735a BP |
1368 | if (spa->spa_zvol_taskq) { |
1369 | taskq_destroy(spa->spa_zvol_taskq); | |
1370 | spa->spa_zvol_taskq = NULL; | |
1371 | } | |
1372 | ||
77d8a0f1 | 1373 | if (spa->spa_prefetch_taskq) { |
1374 | taskq_destroy(spa->spa_prefetch_taskq); | |
1375 | spa->spa_prefetch_taskq = NULL; | |
1376 | } | |
1377 | ||
1de321e6 JX |
1378 | if (spa->spa_upgrade_taskq) { |
1379 | taskq_destroy(spa->spa_upgrade_taskq); | |
1380 | spa->spa_upgrade_taskq = NULL; | |
1381 | } | |
1382 | ||
34dc7c2f BB |
1383 | txg_list_destroy(&spa->spa_vdev_txg_list); |
1384 | ||
b128c09f | 1385 | list_destroy(&spa->spa_config_dirty_list); |
0c66c32d | 1386 | list_destroy(&spa->spa_evicting_os_list); |
b128c09f | 1387 | list_destroy(&spa->spa_state_dirty_list); |
34dc7c2f | 1388 | |
57ddcda1 | 1389 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 | 1390 | |
1c27024e DB |
1391 | for (int t = 0; t < ZIO_TYPES; t++) { |
1392 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1393 | spa_taskqs_fini(spa, t, q); |
b128c09f | 1394 | } |
34dc7c2f BB |
1395 | } |
1396 | ||
a1d477c2 MA |
1397 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1398 | ASSERT3P(spa->spa_txg_zio[i], !=, NULL); | |
1399 | VERIFY0(zio_wait(spa->spa_txg_zio[i])); | |
1400 | spa->spa_txg_zio[i] = NULL; | |
1401 | } | |
1402 | ||
34dc7c2f BB |
1403 | metaslab_class_destroy(spa->spa_normal_class); |
1404 | spa->spa_normal_class = NULL; | |
1405 | ||
1406 | metaslab_class_destroy(spa->spa_log_class); | |
1407 | spa->spa_log_class = NULL; | |
1408 | ||
aa755b35 MA |
1409 | metaslab_class_destroy(spa->spa_embedded_log_class); |
1410 | spa->spa_embedded_log_class = NULL; | |
1411 | ||
cc99f275 DB |
1412 | metaslab_class_destroy(spa->spa_special_class); |
1413 | spa->spa_special_class = NULL; | |
1414 | ||
1415 | metaslab_class_destroy(spa->spa_dedup_class); | |
1416 | spa->spa_dedup_class = NULL; | |
1417 | ||
34dc7c2f BB |
1418 | /* |
1419 | * If this was part of an import or the open otherwise failed, we may | |
1420 | * still have errors left in the queues. Empty them just in case. | |
1421 | */ | |
1422 | spa_errlog_drain(spa); | |
34dc7c2f BB |
1423 | avl_destroy(&spa->spa_errlist_scrub); |
1424 | avl_destroy(&spa->spa_errlist_last); | |
1425 | ||
b5256303 TC |
1426 | spa_keystore_fini(&spa->spa_keystore); |
1427 | ||
34dc7c2f | 1428 | spa->spa_state = POOL_STATE_UNINITIALIZED; |
428870ff BB |
1429 | |
1430 | mutex_enter(&spa->spa_proc_lock); | |
1431 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1432 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1433 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1434 | cv_broadcast(&spa->spa_proc_cv); | |
1435 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1436 | ASSERT(spa->spa_proc != &p0); | |
1437 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1438 | } | |
1439 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1440 | spa->spa_proc_state = SPA_PROC_NONE; | |
1441 | } | |
1442 | ASSERT(spa->spa_proc == &p0); | |
1443 | mutex_exit(&spa->spa_proc_lock); | |
1444 | ||
1445 | /* | |
1446 | * We want to make sure spa_thread() has actually exited the ZFS | |
1447 | * module, so that the module can't be unloaded out from underneath | |
1448 | * it. | |
1449 | */ | |
1450 | if (spa->spa_did != 0) { | |
1451 | thread_join(spa->spa_did); | |
1452 | spa->spa_did = 0; | |
1453 | } | |
34dc7c2f BB |
1454 | } |
1455 | ||
1456 | /* | |
1457 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1458 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1459 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1460 | * All vdev validation is done by the vdev_alloc() routine. | |
1461 | */ | |
4a22ba5b | 1462 | int |
34dc7c2f BB |
1463 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, |
1464 | uint_t id, int atype) | |
1465 | { | |
1466 | nvlist_t **child; | |
9babb374 | 1467 | uint_t children; |
34dc7c2f BB |
1468 | int error; |
1469 | ||
1470 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1471 | return (error); | |
1472 | ||
1473 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1474 | return (0); | |
1475 | ||
b128c09f BB |
1476 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1477 | &child, &children); | |
1478 | ||
1479 | if (error == ENOENT) | |
1480 | return (0); | |
1481 | ||
1482 | if (error) { | |
34dc7c2f BB |
1483 | vdev_free(*vdp); |
1484 | *vdp = NULL; | |
2e528b49 | 1485 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1486 | } |
1487 | ||
1c27024e | 1488 | for (int c = 0; c < children; c++) { |
34dc7c2f BB |
1489 | vdev_t *vd; |
1490 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1491 | atype)) != 0) { | |
1492 | vdev_free(*vdp); | |
1493 | *vdp = NULL; | |
1494 | return (error); | |
1495 | } | |
1496 | } | |
1497 | ||
1498 | ASSERT(*vdp != NULL); | |
1499 | ||
1500 | return (0); | |
1501 | } | |
1502 | ||
93e28d66 SD |
1503 | static boolean_t |
1504 | spa_should_flush_logs_on_unload(spa_t *spa) | |
1505 | { | |
1506 | if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) | |
1507 | return (B_FALSE); | |
1508 | ||
1509 | if (!spa_writeable(spa)) | |
1510 | return (B_FALSE); | |
1511 | ||
1512 | if (!spa->spa_sync_on) | |
1513 | return (B_FALSE); | |
1514 | ||
1515 | if (spa_state(spa) != POOL_STATE_EXPORTED) | |
1516 | return (B_FALSE); | |
1517 | ||
1518 | if (zfs_keep_log_spacemaps_at_export) | |
1519 | return (B_FALSE); | |
1520 | ||
1521 | return (B_TRUE); | |
1522 | } | |
1523 | ||
1524 | /* | |
1525 | * Opens a transaction that will set the flag that will instruct | |
1526 | * spa_sync to attempt to flush all the metaslabs for that txg. | |
1527 | */ | |
1528 | static void | |
1529 | spa_unload_log_sm_flush_all(spa_t *spa) | |
1530 | { | |
1531 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
1532 | VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); | |
1533 | ||
1534 | ASSERT3U(spa->spa_log_flushall_txg, ==, 0); | |
1535 | spa->spa_log_flushall_txg = dmu_tx_get_txg(tx); | |
1536 | ||
1537 | dmu_tx_commit(tx); | |
1538 | txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg); | |
1539 | } | |
1540 | ||
1541 | static void | |
1542 | spa_unload_log_sm_metadata(spa_t *spa) | |
1543 | { | |
1544 | void *cookie = NULL; | |
1545 | spa_log_sm_t *sls; | |
1546 | while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg, | |
1547 | &cookie)) != NULL) { | |
1548 | VERIFY0(sls->sls_mscount); | |
1549 | kmem_free(sls, sizeof (spa_log_sm_t)); | |
1550 | } | |
1551 | ||
1552 | for (log_summary_entry_t *e = list_head(&spa->spa_log_summary); | |
1553 | e != NULL; e = list_head(&spa->spa_log_summary)) { | |
1554 | VERIFY0(e->lse_mscount); | |
1555 | list_remove(&spa->spa_log_summary, e); | |
1556 | kmem_free(e, sizeof (log_summary_entry_t)); | |
1557 | } | |
1558 | ||
1559 | spa->spa_unflushed_stats.sus_nblocks = 0; | |
1560 | spa->spa_unflushed_stats.sus_memused = 0; | |
1561 | spa->spa_unflushed_stats.sus_blocklimit = 0; | |
1562 | } | |
1563 | ||
37f03da8 SH |
1564 | static void |
1565 | spa_destroy_aux_threads(spa_t *spa) | |
1566 | { | |
1567 | if (spa->spa_condense_zthr != NULL) { | |
1568 | zthr_destroy(spa->spa_condense_zthr); | |
1569 | spa->spa_condense_zthr = NULL; | |
1570 | } | |
1571 | if (spa->spa_checkpoint_discard_zthr != NULL) { | |
1572 | zthr_destroy(spa->spa_checkpoint_discard_zthr); | |
1573 | spa->spa_checkpoint_discard_zthr = NULL; | |
1574 | } | |
1575 | if (spa->spa_livelist_delete_zthr != NULL) { | |
1576 | zthr_destroy(spa->spa_livelist_delete_zthr); | |
1577 | spa->spa_livelist_delete_zthr = NULL; | |
1578 | } | |
1579 | if (spa->spa_livelist_condense_zthr != NULL) { | |
1580 | zthr_destroy(spa->spa_livelist_condense_zthr); | |
1581 | spa->spa_livelist_condense_zthr = NULL; | |
1582 | } | |
1583 | } | |
1584 | ||
34dc7c2f BB |
1585 | /* |
1586 | * Opposite of spa_load(). | |
1587 | */ | |
1588 | static void | |
1589 | spa_unload(spa_t *spa) | |
1590 | { | |
b128c09f | 1591 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
93e28d66 | 1592 | ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED); |
b128c09f | 1593 | |
ca95f70d | 1594 | spa_import_progress_remove(spa_guid(spa)); |
4a0ee12a PZ |
1595 | spa_load_note(spa, "UNLOADING"); |
1596 | ||
e60e158e JG |
1597 | spa_wake_waiters(spa); |
1598 | ||
93e28d66 | 1599 | /* |
2fb52853 GA |
1600 | * If we have set the spa_final_txg, we have already performed the |
1601 | * tasks below in spa_export_common(). We should not redo it here since | |
1602 | * we delay the final TXGs beyond what spa_final_txg is set at. | |
93e28d66 | 1603 | */ |
2fb52853 GA |
1604 | if (spa->spa_final_txg == UINT64_MAX) { |
1605 | /* | |
1606 | * If the log space map feature is enabled and the pool is | |
1607 | * getting exported (but not destroyed), we want to spend some | |
1608 | * time flushing as many metaslabs as we can in an attempt to | |
1609 | * destroy log space maps and save import time. | |
1610 | */ | |
1611 | if (spa_should_flush_logs_on_unload(spa)) | |
1612 | spa_unload_log_sm_flush_all(spa); | |
93e28d66 | 1613 | |
2fb52853 GA |
1614 | /* |
1615 | * Stop async tasks. | |
1616 | */ | |
1617 | spa_async_suspend(spa); | |
34dc7c2f | 1618 | |
2fb52853 GA |
1619 | if (spa->spa_root_vdev) { |
1620 | vdev_t *root_vdev = spa->spa_root_vdev; | |
1621 | vdev_initialize_stop_all(root_vdev, | |
1622 | VDEV_INITIALIZE_ACTIVE); | |
1623 | vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE); | |
1624 | vdev_autotrim_stop_all(spa); | |
1625 | vdev_rebuild_stop_all(spa); | |
1626 | } | |
619f0976 GW |
1627 | } |
1628 | ||
34dc7c2f BB |
1629 | /* |
1630 | * Stop syncing. | |
1631 | */ | |
1632 | if (spa->spa_sync_on) { | |
1633 | txg_sync_stop(spa->spa_dsl_pool); | |
1634 | spa->spa_sync_on = B_FALSE; | |
1635 | } | |
1636 | ||
4e21fd06 | 1637 | /* |
93e28d66 SD |
1638 | * This ensures that there is no async metaslab prefetching |
1639 | * while we attempt to unload the spa. | |
4e21fd06 DB |
1640 | */ |
1641 | if (spa->spa_root_vdev != NULL) { | |
93e28d66 SD |
1642 | for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) { |
1643 | vdev_t *vc = spa->spa_root_vdev->vdev_child[c]; | |
1644 | if (vc->vdev_mg != NULL) | |
1645 | taskq_wait(vc->vdev_mg->mg_taskq); | |
1646 | } | |
4e21fd06 DB |
1647 | } |
1648 | ||
379ca9cf OF |
1649 | if (spa->spa_mmp.mmp_thread) |
1650 | mmp_thread_stop(spa); | |
1651 | ||
34dc7c2f | 1652 | /* |
b128c09f | 1653 | * Wait for any outstanding async I/O to complete. |
34dc7c2f | 1654 | */ |
9babb374 | 1655 | if (spa->spa_async_zio_root != NULL) { |
1c27024e | 1656 | for (int i = 0; i < max_ncpus; i++) |
e022864d MA |
1657 | (void) zio_wait(spa->spa_async_zio_root[i]); |
1658 | kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); | |
9babb374 BB |
1659 | spa->spa_async_zio_root = NULL; |
1660 | } | |
34dc7c2f | 1661 | |
a1d477c2 MA |
1662 | if (spa->spa_vdev_removal != NULL) { |
1663 | spa_vdev_removal_destroy(spa->spa_vdev_removal); | |
1664 | spa->spa_vdev_removal = NULL; | |
1665 | } | |
1666 | ||
37f03da8 | 1667 | spa_destroy_aux_threads(spa); |
d2734cce | 1668 | |
a1d477c2 MA |
1669 | spa_condense_fini(spa); |
1670 | ||
428870ff BB |
1671 | bpobj_close(&spa->spa_deferred_bpobj); |
1672 | ||
619f0976 | 1673 | spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); |
93cf2076 GW |
1674 | |
1675 | /* | |
1676 | * Close all vdevs. | |
1677 | */ | |
1678 | if (spa->spa_root_vdev) | |
1679 | vdev_free(spa->spa_root_vdev); | |
1680 | ASSERT(spa->spa_root_vdev == NULL); | |
1681 | ||
34dc7c2f BB |
1682 | /* |
1683 | * Close the dsl pool. | |
1684 | */ | |
1685 | if (spa->spa_dsl_pool) { | |
1686 | dsl_pool_close(spa->spa_dsl_pool); | |
1687 | spa->spa_dsl_pool = NULL; | |
428870ff | 1688 | spa->spa_meta_objset = NULL; |
34dc7c2f BB |
1689 | } |
1690 | ||
428870ff | 1691 | ddt_unload(spa); |
93e28d66 | 1692 | spa_unload_log_sm_metadata(spa); |
428870ff | 1693 | |
fb5f0bc8 BB |
1694 | /* |
1695 | * Drop and purge level 2 cache | |
1696 | */ | |
1697 | spa_l2cache_drop(spa); | |
1698 | ||
93e28d66 | 1699 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
34dc7c2f BB |
1700 | vdev_free(spa->spa_spares.sav_vdevs[i]); |
1701 | if (spa->spa_spares.sav_vdevs) { | |
1702 | kmem_free(spa->spa_spares.sav_vdevs, | |
1703 | spa->spa_spares.sav_count * sizeof (void *)); | |
1704 | spa->spa_spares.sav_vdevs = NULL; | |
1705 | } | |
1706 | if (spa->spa_spares.sav_config) { | |
1707 | nvlist_free(spa->spa_spares.sav_config); | |
1708 | spa->spa_spares.sav_config = NULL; | |
1709 | } | |
b128c09f | 1710 | spa->spa_spares.sav_count = 0; |
34dc7c2f | 1711 | |
93e28d66 | 1712 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { |
5ffb9d1d | 1713 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); |
34dc7c2f | 1714 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); |
5ffb9d1d | 1715 | } |
34dc7c2f BB |
1716 | if (spa->spa_l2cache.sav_vdevs) { |
1717 | kmem_free(spa->spa_l2cache.sav_vdevs, | |
1718 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1719 | spa->spa_l2cache.sav_vdevs = NULL; | |
1720 | } | |
1721 | if (spa->spa_l2cache.sav_config) { | |
1722 | nvlist_free(spa->spa_l2cache.sav_config); | |
1723 | spa->spa_l2cache.sav_config = NULL; | |
1724 | } | |
b128c09f | 1725 | spa->spa_l2cache.sav_count = 0; |
34dc7c2f BB |
1726 | |
1727 | spa->spa_async_suspended = 0; | |
fb5f0bc8 | 1728 | |
a1d477c2 MA |
1729 | spa->spa_indirect_vdevs_loaded = B_FALSE; |
1730 | ||
d96eb2b1 DM |
1731 | if (spa->spa_comment != NULL) { |
1732 | spa_strfree(spa->spa_comment); | |
1733 | spa->spa_comment = NULL; | |
1734 | } | |
658fb802 CB |
1735 | if (spa->spa_compatibility != NULL) { |
1736 | spa_strfree(spa->spa_compatibility); | |
1737 | spa->spa_compatibility = NULL; | |
1738 | } | |
d96eb2b1 | 1739 | |
619f0976 | 1740 | spa_config_exit(spa, SCL_ALL, spa); |
34dc7c2f BB |
1741 | } |
1742 | ||
1743 | /* | |
1744 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1745 | * this pool. When this is called, we have some form of basic information in | |
1746 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1747 | * then re-generate a more complete list including status information. | |
1748 | */ | |
a1d477c2 | 1749 | void |
34dc7c2f BB |
1750 | spa_load_spares(spa_t *spa) |
1751 | { | |
1752 | nvlist_t **spares; | |
1753 | uint_t nspares; | |
1754 | int i; | |
1755 | vdev_t *vd, *tvd; | |
1756 | ||
d2734cce SD |
1757 | #ifndef _KERNEL |
1758 | /* | |
1759 | * zdb opens both the current state of the pool and the | |
1760 | * checkpointed state (if present), with a different spa_t. | |
1761 | * | |
1762 | * As spare vdevs are shared among open pools, we skip loading | |
1763 | * them when we load the checkpointed state of the pool. | |
1764 | */ | |
1765 | if (!spa_writeable(spa)) | |
1766 | return; | |
1767 | #endif | |
1768 | ||
b128c09f BB |
1769 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1770 | ||
34dc7c2f BB |
1771 | /* |
1772 | * First, close and free any existing spare vdevs. | |
1773 | */ | |
1774 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1775 | vd = spa->spa_spares.sav_vdevs[i]; | |
1776 | ||
1777 | /* Undo the call to spa_activate() below */ | |
b128c09f BB |
1778 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1779 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
34dc7c2f BB |
1780 | spa_spare_remove(tvd); |
1781 | vdev_close(vd); | |
1782 | vdev_free(vd); | |
1783 | } | |
1784 | ||
1785 | if (spa->spa_spares.sav_vdevs) | |
1786 | kmem_free(spa->spa_spares.sav_vdevs, | |
1787 | spa->spa_spares.sav_count * sizeof (void *)); | |
1788 | ||
1789 | if (spa->spa_spares.sav_config == NULL) | |
1790 | nspares = 0; | |
1791 | else | |
65ad5d11 AJ |
1792 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, |
1793 | ZPOOL_CONFIG_SPARES, &spares, &nspares)); | |
34dc7c2f BB |
1794 | |
1795 | spa->spa_spares.sav_count = (int)nspares; | |
1796 | spa->spa_spares.sav_vdevs = NULL; | |
1797 | ||
1798 | if (nspares == 0) | |
1799 | return; | |
1800 | ||
1801 | /* | |
1802 | * Construct the array of vdevs, opening them to get status in the | |
1803 | * process. For each spare, there is potentially two different vdev_t | |
1804 | * structures associated with it: one in the list of spares (used only | |
1805 | * for basic validation purposes) and one in the active vdev | |
1806 | * configuration (if it's spared in). During this phase we open and | |
1807 | * validate each vdev on the spare list. If the vdev also exists in the | |
1808 | * active configuration, then we also mark this vdev as an active spare. | |
1809 | */ | |
904ea276 | 1810 | spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *), |
79c76d5b | 1811 | KM_SLEEP); |
34dc7c2f BB |
1812 | for (i = 0; i < spa->spa_spares.sav_count; i++) { |
1813 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1814 | VDEV_ALLOC_SPARE) == 0); | |
1815 | ASSERT(vd != NULL); | |
1816 | ||
1817 | spa->spa_spares.sav_vdevs[i] = vd; | |
1818 | ||
b128c09f BB |
1819 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1820 | B_FALSE)) != NULL) { | |
34dc7c2f BB |
1821 | if (!tvd->vdev_isspare) |
1822 | spa_spare_add(tvd); | |
1823 | ||
1824 | /* | |
1825 | * We only mark the spare active if we were successfully | |
1826 | * able to load the vdev. Otherwise, importing a pool | |
1827 | * with a bad active spare would result in strange | |
1828 | * behavior, because multiple pool would think the spare | |
1829 | * is actively in use. | |
1830 | * | |
1831 | * There is a vulnerability here to an equally bizarre | |
1832 | * circumstance, where a dead active spare is later | |
1833 | * brought back to life (onlined or otherwise). Given | |
1834 | * the rarity of this scenario, and the extra complexity | |
1835 | * it adds, we ignore the possibility. | |
1836 | */ | |
1837 | if (!vdev_is_dead(tvd)) | |
1838 | spa_spare_activate(tvd); | |
1839 | } | |
1840 | ||
b128c09f | 1841 | vd->vdev_top = vd; |
9babb374 | 1842 | vd->vdev_aux = &spa->spa_spares; |
b128c09f | 1843 | |
34dc7c2f BB |
1844 | if (vdev_open(vd) != 0) |
1845 | continue; | |
1846 | ||
34dc7c2f BB |
1847 | if (vdev_validate_aux(vd) == 0) |
1848 | spa_spare_add(vd); | |
1849 | } | |
1850 | ||
1851 | /* | |
1852 | * Recompute the stashed list of spares, with status information | |
1853 | * this time. | |
1854 | */ | |
65ad5d11 | 1855 | fnvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES); |
34dc7c2f BB |
1856 | |
1857 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
79c76d5b | 1858 | KM_SLEEP); |
34dc7c2f BB |
1859 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1860 | spares[i] = vdev_config_generate(spa, | |
428870ff | 1861 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); |
65ad5d11 | 1862 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, |
795075e6 PD |
1863 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
1864 | spa->spa_spares.sav_count); | |
34dc7c2f BB |
1865 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1866 | nvlist_free(spares[i]); | |
1867 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1868 | } | |
1869 | ||
1870 | /* | |
1871 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1872 | * this pool. When this is called, we have some form of basic information in | |
1873 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1874 | * then re-generate a more complete list including status information. | |
1875 | * Devices which are already active have their details maintained, and are | |
1876 | * not re-opened. | |
1877 | */ | |
a1d477c2 | 1878 | void |
34dc7c2f BB |
1879 | spa_load_l2cache(spa_t *spa) |
1880 | { | |
460f239e | 1881 | nvlist_t **l2cache = NULL; |
34dc7c2f BB |
1882 | uint_t nl2cache; |
1883 | int i, j, oldnvdevs; | |
9babb374 | 1884 | uint64_t guid; |
a117a6d6 | 1885 | vdev_t *vd, **oldvdevs, **newvdevs; |
34dc7c2f BB |
1886 | spa_aux_vdev_t *sav = &spa->spa_l2cache; |
1887 | ||
d2734cce SD |
1888 | #ifndef _KERNEL |
1889 | /* | |
1890 | * zdb opens both the current state of the pool and the | |
1891 | * checkpointed state (if present), with a different spa_t. | |
1892 | * | |
1893 | * As L2 caches are part of the ARC which is shared among open | |
1894 | * pools, we skip loading them when we load the checkpointed | |
1895 | * state of the pool. | |
1896 | */ | |
1897 | if (!spa_writeable(spa)) | |
1898 | return; | |
1899 | #endif | |
1900 | ||
b128c09f BB |
1901 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1902 | ||
34dc7c2f BB |
1903 | oldvdevs = sav->sav_vdevs; |
1904 | oldnvdevs = sav->sav_count; | |
1905 | sav->sav_vdevs = NULL; | |
1906 | sav->sav_count = 0; | |
1907 | ||
67d60824 NB |
1908 | if (sav->sav_config == NULL) { |
1909 | nl2cache = 0; | |
1910 | newvdevs = NULL; | |
1911 | goto out; | |
1912 | } | |
1913 | ||
65ad5d11 AJ |
1914 | VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config, |
1915 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache)); | |
67d60824 NB |
1916 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); |
1917 | ||
34dc7c2f BB |
1918 | /* |
1919 | * Process new nvlist of vdevs. | |
1920 | */ | |
1921 | for (i = 0; i < nl2cache; i++) { | |
65ad5d11 | 1922 | guid = fnvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID); |
34dc7c2f BB |
1923 | |
1924 | newvdevs[i] = NULL; | |
1925 | for (j = 0; j < oldnvdevs; j++) { | |
1926 | vd = oldvdevs[j]; | |
1927 | if (vd != NULL && guid == vd->vdev_guid) { | |
1928 | /* | |
1929 | * Retain previous vdev for add/remove ops. | |
1930 | */ | |
1931 | newvdevs[i] = vd; | |
1932 | oldvdevs[j] = NULL; | |
1933 | break; | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | if (newvdevs[i] == NULL) { | |
1938 | /* | |
1939 | * Create new vdev | |
1940 | */ | |
1941 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
1942 | VDEV_ALLOC_L2CACHE) == 0); | |
1943 | ASSERT(vd != NULL); | |
1944 | newvdevs[i] = vd; | |
1945 | ||
1946 | /* | |
1947 | * Commit this vdev as an l2cache device, | |
1948 | * even if it fails to open. | |
1949 | */ | |
1950 | spa_l2cache_add(vd); | |
1951 | ||
b128c09f BB |
1952 | vd->vdev_top = vd; |
1953 | vd->vdev_aux = sav; | |
1954 | ||
1955 | spa_l2cache_activate(vd); | |
1956 | ||
34dc7c2f BB |
1957 | if (vdev_open(vd) != 0) |
1958 | continue; | |
1959 | ||
34dc7c2f BB |
1960 | (void) vdev_validate_aux(vd); |
1961 | ||
9babb374 BB |
1962 | if (!vdev_is_dead(vd)) |
1963 | l2arc_add_vdev(spa, vd); | |
b7654bd7 GA |
1964 | |
1965 | /* | |
1966 | * Upon cache device addition to a pool or pool | |
1967 | * creation with a cache device or if the header | |
1968 | * of the device is invalid we issue an async | |
1969 | * TRIM command for the whole device which will | |
1970 | * execute if l2arc_trim_ahead > 0. | |
1971 | */ | |
1972 | spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM); | |
34dc7c2f BB |
1973 | } |
1974 | } | |
1975 | ||
67d60824 NB |
1976 | sav->sav_vdevs = newvdevs; |
1977 | sav->sav_count = (int)nl2cache; | |
1978 | ||
1979 | /* | |
1980 | * Recompute the stashed list of l2cache devices, with status | |
1981 | * information this time. | |
1982 | */ | |
65ad5d11 | 1983 | fnvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE); |
67d60824 | 1984 | |
460f239e D |
1985 | if (sav->sav_count > 0) |
1986 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), | |
1987 | KM_SLEEP); | |
67d60824 NB |
1988 | for (i = 0; i < sav->sav_count; i++) |
1989 | l2cache[i] = vdev_config_generate(spa, | |
1990 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); | |
795075e6 PD |
1991 | fnvlist_add_nvlist_array(sav->sav_config, ZPOOL_CONFIG_L2CACHE, |
1992 | (const nvlist_t * const *)l2cache, sav->sav_count); | |
67d60824 NB |
1993 | |
1994 | out: | |
34dc7c2f BB |
1995 | /* |
1996 | * Purge vdevs that were dropped | |
1997 | */ | |
1998 | for (i = 0; i < oldnvdevs; i++) { | |
1999 | uint64_t pool; | |
2000 | ||
2001 | vd = oldvdevs[i]; | |
2002 | if (vd != NULL) { | |
5ffb9d1d GW |
2003 | ASSERT(vd->vdev_isl2cache); |
2004 | ||
fb5f0bc8 BB |
2005 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
2006 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 2007 | l2arc_remove_vdev(vd); |
5ffb9d1d GW |
2008 | vdev_clear_stats(vd); |
2009 | vdev_free(vd); | |
34dc7c2f BB |
2010 | } |
2011 | } | |
2012 | ||
2013 | if (oldvdevs) | |
2014 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); | |
2015 | ||
34dc7c2f BB |
2016 | for (i = 0; i < sav->sav_count; i++) |
2017 | nvlist_free(l2cache[i]); | |
2018 | if (sav->sav_count) | |
2019 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
2020 | } | |
2021 | ||
2022 | static int | |
2023 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
2024 | { | |
2025 | dmu_buf_t *db; | |
2026 | char *packed = NULL; | |
2027 | size_t nvsize = 0; | |
2028 | int error; | |
2029 | *value = NULL; | |
2030 | ||
c3275b56 BB |
2031 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); |
2032 | if (error) | |
2033 | return (error); | |
2034 | ||
34dc7c2f BB |
2035 | nvsize = *(uint64_t *)db->db_data; |
2036 | dmu_buf_rele(db, FTAG); | |
2037 | ||
77aef6f6 | 2038 | packed = vmem_alloc(nvsize, KM_SLEEP); |
9babb374 BB |
2039 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, |
2040 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
2041 | if (error == 0) |
2042 | error = nvlist_unpack(packed, nvsize, value, 0); | |
77aef6f6 | 2043 | vmem_free(packed, nvsize); |
34dc7c2f BB |
2044 | |
2045 | return (error); | |
2046 | } | |
2047 | ||
6cb8e530 PZ |
2048 | /* |
2049 | * Concrete top-level vdevs that are not missing and are not logs. At every | |
2050 | * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. | |
2051 | */ | |
2052 | static uint64_t | |
2053 | spa_healthy_core_tvds(spa_t *spa) | |
2054 | { | |
2055 | vdev_t *rvd = spa->spa_root_vdev; | |
2056 | uint64_t tvds = 0; | |
2057 | ||
2058 | for (uint64_t i = 0; i < rvd->vdev_children; i++) { | |
2059 | vdev_t *vd = rvd->vdev_child[i]; | |
2060 | if (vd->vdev_islog) | |
2061 | continue; | |
2062 | if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) | |
2063 | tvds++; | |
2064 | } | |
2065 | ||
2066 | return (tvds); | |
2067 | } | |
2068 | ||
34dc7c2f BB |
2069 | /* |
2070 | * Checks to see if the given vdev could not be opened, in which case we post a | |
2071 | * sysevent to notify the autoreplace code that the device has been removed. | |
2072 | */ | |
2073 | static void | |
2074 | spa_check_removed(vdev_t *vd) | |
2075 | { | |
6cb8e530 | 2076 | for (uint64_t c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
2077 | spa_check_removed(vd->vdev_child[c]); |
2078 | ||
7011fb60 | 2079 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && |
a1d477c2 | 2080 | vdev_is_concrete(vd)) { |
fb390aaf | 2081 | zfs_post_autoreplace(vd->vdev_spa, vd); |
12fa0466 | 2082 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); |
34dc7c2f BB |
2083 | } |
2084 | } | |
2085 | ||
6cb8e530 PZ |
2086 | static int |
2087 | spa_check_for_missing_logs(spa_t *spa) | |
9babb374 | 2088 | { |
6cb8e530 | 2089 | vdev_t *rvd = spa->spa_root_vdev; |
9babb374 | 2090 | |
428870ff | 2091 | /* |
572e2857 | 2092 | * If we're doing a normal import, then build up any additional |
6cb8e530 | 2093 | * diagnostic information about missing log devices. |
572e2857 | 2094 | * We'll pass this up to the user for further processing. |
428870ff | 2095 | */ |
572e2857 BB |
2096 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { |
2097 | nvlist_t **child, *nv; | |
2098 | uint64_t idx = 0; | |
2099 | ||
160987b5 | 2100 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *), |
79c76d5b | 2101 | KM_SLEEP); |
65ad5d11 | 2102 | nv = fnvlist_alloc(); |
572e2857 | 2103 | |
6cb8e530 | 2104 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { |
572e2857 | 2105 | vdev_t *tvd = rvd->vdev_child[c]; |
572e2857 | 2106 | |
6cb8e530 PZ |
2107 | /* |
2108 | * We consider a device as missing only if it failed | |
2109 | * to open (i.e. offline or faulted is not considered | |
2110 | * as missing). | |
2111 | */ | |
2112 | if (tvd->vdev_islog && | |
2113 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
2114 | child[idx++] = vdev_config_generate(spa, tvd, | |
2115 | B_FALSE, VDEV_CONFIG_MISSING); | |
2116 | } | |
572e2857 | 2117 | } |
9babb374 | 2118 | |
6cb8e530 | 2119 | if (idx > 0) { |
795075e6 PD |
2120 | fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
2121 | (const nvlist_t * const *)child, idx); | |
6cb8e530 PZ |
2122 | fnvlist_add_nvlist(spa->spa_load_info, |
2123 | ZPOOL_CONFIG_MISSING_DEVICES, nv); | |
572e2857 | 2124 | |
6cb8e530 | 2125 | for (uint64_t i = 0; i < idx; i++) |
572e2857 BB |
2126 | nvlist_free(child[i]); |
2127 | } | |
2128 | nvlist_free(nv); | |
2129 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
572e2857 | 2130 | |
6cb8e530 PZ |
2131 | if (idx > 0) { |
2132 | spa_load_failed(spa, "some log devices are missing"); | |
db7d07e1 | 2133 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 PZ |
2134 | return (SET_ERROR(ENXIO)); |
2135 | } | |
2136 | } else { | |
2137 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
2138 | vdev_t *tvd = rvd->vdev_child[c]; | |
a1d477c2 | 2139 | |
6cb8e530 PZ |
2140 | if (tvd->vdev_islog && |
2141 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
572e2857 | 2142 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
6cb8e530 PZ |
2143 | spa_load_note(spa, "some log devices are " |
2144 | "missing, ZIL is dropped."); | |
db7d07e1 | 2145 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 | 2146 | break; |
e0ab3ab5 | 2147 | } |
572e2857 | 2148 | } |
9babb374 | 2149 | } |
e0ab3ab5 | 2150 | |
6cb8e530 | 2151 | return (0); |
9babb374 BB |
2152 | } |
2153 | ||
b128c09f BB |
2154 | /* |
2155 | * Check for missing log devices | |
2156 | */ | |
13fe0198 | 2157 | static boolean_t |
b128c09f BB |
2158 | spa_check_logs(spa_t *spa) |
2159 | { | |
13fe0198 | 2160 | boolean_t rv = B_FALSE; |
9c43027b | 2161 | dsl_pool_t *dp = spa_get_dsl(spa); |
13fe0198 | 2162 | |
b128c09f | 2163 | switch (spa->spa_log_state) { |
e75c13c3 BB |
2164 | default: |
2165 | break; | |
b128c09f BB |
2166 | case SPA_LOG_MISSING: |
2167 | /* need to recheck in case slog has been restored */ | |
2168 | case SPA_LOG_UNKNOWN: | |
9c43027b AJ |
2169 | rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, |
2170 | zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); | |
13fe0198 | 2171 | if (rv) |
428870ff | 2172 | spa_set_log_state(spa, SPA_LOG_MISSING); |
b128c09f | 2173 | break; |
b128c09f | 2174 | } |
13fe0198 | 2175 | return (rv); |
b128c09f BB |
2176 | } |
2177 | ||
aa755b35 MA |
2178 | /* |
2179 | * Passivate any log vdevs (note, does not apply to embedded log metaslabs). | |
2180 | */ | |
428870ff BB |
2181 | static boolean_t |
2182 | spa_passivate_log(spa_t *spa) | |
34dc7c2f | 2183 | { |
428870ff BB |
2184 | vdev_t *rvd = spa->spa_root_vdev; |
2185 | boolean_t slog_found = B_FALSE; | |
b128c09f | 2186 | |
428870ff | 2187 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
fb5f0bc8 | 2188 | |
1c27024e | 2189 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff | 2190 | vdev_t *tvd = rvd->vdev_child[c]; |
34dc7c2f | 2191 | |
428870ff | 2192 | if (tvd->vdev_islog) { |
aa755b35 MA |
2193 | ASSERT3P(tvd->vdev_log_mg, ==, NULL); |
2194 | metaslab_group_passivate(tvd->vdev_mg); | |
428870ff BB |
2195 | slog_found = B_TRUE; |
2196 | } | |
34dc7c2f BB |
2197 | } |
2198 | ||
428870ff BB |
2199 | return (slog_found); |
2200 | } | |
34dc7c2f | 2201 | |
aa755b35 MA |
2202 | /* |
2203 | * Activate any log vdevs (note, does not apply to embedded log metaslabs). | |
2204 | */ | |
428870ff BB |
2205 | static void |
2206 | spa_activate_log(spa_t *spa) | |
2207 | { | |
2208 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 2209 | |
428870ff BB |
2210 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
2211 | ||
1c27024e | 2212 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff | 2213 | vdev_t *tvd = rvd->vdev_child[c]; |
428870ff | 2214 | |
aa755b35 MA |
2215 | if (tvd->vdev_islog) { |
2216 | ASSERT3P(tvd->vdev_log_mg, ==, NULL); | |
2217 | metaslab_group_activate(tvd->vdev_mg); | |
2218 | } | |
34dc7c2f | 2219 | } |
428870ff | 2220 | } |
34dc7c2f | 2221 | |
428870ff | 2222 | int |
a1d477c2 | 2223 | spa_reset_logs(spa_t *spa) |
428870ff | 2224 | { |
13fe0198 | 2225 | int error; |
9babb374 | 2226 | |
a1d477c2 | 2227 | error = dmu_objset_find(spa_name(spa), zil_reset, |
13fe0198 MA |
2228 | NULL, DS_FIND_CHILDREN); |
2229 | if (error == 0) { | |
428870ff BB |
2230 | /* |
2231 | * We successfully offlined the log device, sync out the | |
2232 | * current txg so that the "stubby" block can be removed | |
2233 | * by zil_sync(). | |
2234 | */ | |
2235 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
2236 | } | |
2237 | return (error); | |
2238 | } | |
34dc7c2f | 2239 | |
428870ff BB |
2240 | static void |
2241 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
2242 | { | |
1c27024e | 2243 | for (int i = 0; i < sav->sav_count; i++) |
428870ff BB |
2244 | spa_check_removed(sav->sav_vdevs[i]); |
2245 | } | |
34dc7c2f | 2246 | |
428870ff BB |
2247 | void |
2248 | spa_claim_notify(zio_t *zio) | |
2249 | { | |
2250 | spa_t *spa = zio->io_spa; | |
34dc7c2f | 2251 | |
428870ff BB |
2252 | if (zio->io_error) |
2253 | return; | |
34dc7c2f | 2254 | |
428870ff BB |
2255 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ |
2256 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
2257 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
2258 | mutex_exit(&spa->spa_props_lock); | |
2259 | } | |
34dc7c2f | 2260 | |
428870ff | 2261 | typedef struct spa_load_error { |
f2c5bc15 | 2262 | boolean_t sle_verify_data; |
428870ff BB |
2263 | uint64_t sle_meta_count; |
2264 | uint64_t sle_data_count; | |
2265 | } spa_load_error_t; | |
34dc7c2f | 2266 | |
428870ff BB |
2267 | static void |
2268 | spa_load_verify_done(zio_t *zio) | |
2269 | { | |
2270 | blkptr_t *bp = zio->io_bp; | |
2271 | spa_load_error_t *sle = zio->io_private; | |
2272 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
2273 | int error = zio->io_error; | |
dea377c0 | 2274 | spa_t *spa = zio->io_spa; |
34dc7c2f | 2275 | |
a6255b7f | 2276 | abd_free(zio->io_abd); |
428870ff | 2277 | if (error) { |
9ae529ec | 2278 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && |
428870ff | 2279 | type != DMU_OT_INTENT_LOG) |
bc89ac84 | 2280 | atomic_inc_64(&sle->sle_meta_count); |
428870ff | 2281 | else |
bc89ac84 | 2282 | atomic_inc_64(&sle->sle_data_count); |
34dc7c2f | 2283 | } |
dea377c0 MA |
2284 | |
2285 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2286 | spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp); |
dea377c0 MA |
2287 | cv_broadcast(&spa->spa_scrub_io_cv); |
2288 | mutex_exit(&spa->spa_scrub_lock); | |
428870ff | 2289 | } |
34dc7c2f | 2290 | |
dea377c0 | 2291 | /* |
e1cfd73f | 2292 | * Maximum number of inflight bytes is the log2 fraction of the arc size. |
c8242a96 | 2293 | * By default, we set it to 1/16th of the arc. |
dea377c0 | 2294 | */ |
18168da7 AZ |
2295 | static int spa_load_verify_shift = 4; |
2296 | static int spa_load_verify_metadata = B_TRUE; | |
2297 | static int spa_load_verify_data = B_TRUE; | |
dea377c0 | 2298 | |
428870ff BB |
2299 | static int |
2300 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
5dbd68a3 | 2301 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) |
428870ff | 2302 | { |
f2c5bc15 AM |
2303 | zio_t *rio = arg; |
2304 | spa_load_error_t *sle = rio->io_private; | |
2305 | ||
14e4e3cb AZ |
2306 | (void) zilog, (void) dnp; |
2307 | ||
30af21b0 PD |
2308 | if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) || |
2309 | BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp)) | |
dea377c0 MA |
2310 | return (0); |
2311 | /* | |
2312 | * Note: normally this routine will not be called if | |
2313 | * spa_load_verify_metadata is not set. However, it may be useful | |
2314 | * to manually set the flag after the traversal has begun. | |
2315 | */ | |
2316 | if (!spa_load_verify_metadata) | |
2317 | return (0); | |
f2c5bc15 AM |
2318 | if (!BP_IS_METADATA(bp) && |
2319 | (!spa_load_verify_data || !sle->sle_verify_data)) | |
dea377c0 MA |
2320 | return (0); |
2321 | ||
1e527162 GW |
2322 | uint64_t maxinflight_bytes = |
2323 | arc_target_bytes() >> spa_load_verify_shift; | |
1c27024e | 2324 | size_t size = BP_GET_PSIZE(bp); |
dea377c0 MA |
2325 | |
2326 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2327 | while (spa->spa_load_verify_bytes >= maxinflight_bytes) |
dea377c0 | 2328 | cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); |
c8242a96 | 2329 | spa->spa_load_verify_bytes += size; |
dea377c0 MA |
2330 | mutex_exit(&spa->spa_scrub_lock); |
2331 | ||
a6255b7f | 2332 | zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, |
dea377c0 MA |
2333 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, |
2334 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
2335 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
428870ff BB |
2336 | return (0); |
2337 | } | |
34dc7c2f | 2338 | |
65c7cc49 | 2339 | static int |
d1d19c78 PD |
2340 | verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) |
2341 | { | |
14e4e3cb AZ |
2342 | (void) dp, (void) arg; |
2343 | ||
d1d19c78 PD |
2344 | if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) |
2345 | return (SET_ERROR(ENAMETOOLONG)); | |
2346 | ||
2347 | return (0); | |
2348 | } | |
2349 | ||
428870ff BB |
2350 | static int |
2351 | spa_load_verify(spa_t *spa) | |
2352 | { | |
2353 | zio_t *rio; | |
2354 | spa_load_error_t sle = { 0 }; | |
8a393be3 | 2355 | zpool_load_policy_t policy; |
428870ff | 2356 | boolean_t verify_ok = B_FALSE; |
dea377c0 | 2357 | int error = 0; |
34dc7c2f | 2358 | |
8a393be3 | 2359 | zpool_get_load_policy(spa->spa_config, &policy); |
34dc7c2f | 2360 | |
f2c5bc15 AM |
2361 | if (policy.zlp_rewind & ZPOOL_NEVER_REWIND || |
2362 | policy.zlp_maxmeta == UINT64_MAX) | |
428870ff | 2363 | return (0); |
34dc7c2f | 2364 | |
d1d19c78 PD |
2365 | dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); |
2366 | error = dmu_objset_find_dp(spa->spa_dsl_pool, | |
2367 | spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, | |
2368 | DS_FIND_CHILDREN); | |
2369 | dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); | |
2370 | if (error != 0) | |
2371 | return (error); | |
2372 | ||
f2c5bc15 AM |
2373 | /* |
2374 | * Verify data only if we are rewinding or error limit was set. | |
2375 | * Otherwise nothing except dbgmsg care about it to waste time. | |
2376 | */ | |
2377 | sle.sle_verify_data = (policy.zlp_rewind & ZPOOL_REWIND_MASK) || | |
2378 | (policy.zlp_maxdata < UINT64_MAX); | |
2379 | ||
428870ff BB |
2380 | rio = zio_root(spa, NULL, &sle, |
2381 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
34dc7c2f | 2382 | |
dea377c0 | 2383 | if (spa_load_verify_metadata) { |
4a0ee12a PZ |
2384 | if (spa->spa_extreme_rewind) { |
2385 | spa_load_note(spa, "performing a complete scan of the " | |
2386 | "pool since extreme rewind is on. This may take " | |
2387 | "a very long time.\n (spa_load_verify_data=%u, " | |
2388 | "spa_load_verify_metadata=%u)", | |
2389 | spa_load_verify_data, spa_load_verify_metadata); | |
2390 | } | |
c8242a96 | 2391 | |
dea377c0 | 2392 | error = traverse_pool(spa, spa->spa_verify_min_txg, |
b5256303 TC |
2393 | TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | |
2394 | TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio); | |
dea377c0 | 2395 | } |
428870ff BB |
2396 | |
2397 | (void) zio_wait(rio); | |
c8242a96 | 2398 | ASSERT0(spa->spa_load_verify_bytes); |
428870ff BB |
2399 | |
2400 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
2401 | spa->spa_load_data_errors = sle.sle_data_count; | |
2402 | ||
afd2f7b7 PZ |
2403 | if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { |
2404 | spa_load_note(spa, "spa_load_verify found %llu metadata errors " | |
2405 | "and %llu data errors", (u_longlong_t)sle.sle_meta_count, | |
2406 | (u_longlong_t)sle.sle_data_count); | |
2407 | } | |
2408 | ||
2409 | if (spa_load_verify_dryrun || | |
8a393be3 PZ |
2410 | (!error && sle.sle_meta_count <= policy.zlp_maxmeta && |
2411 | sle.sle_data_count <= policy.zlp_maxdata)) { | |
572e2857 BB |
2412 | int64_t loss = 0; |
2413 | ||
428870ff BB |
2414 | verify_ok = B_TRUE; |
2415 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
2416 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
572e2857 BB |
2417 | |
2418 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
65ad5d11 AJ |
2419 | fnvlist_add_uint64(spa->spa_load_info, ZPOOL_CONFIG_LOAD_TIME, |
2420 | spa->spa_load_txg_ts); | |
2421 | fnvlist_add_int64(spa->spa_load_info, ZPOOL_CONFIG_REWIND_TIME, | |
2422 | loss); | |
f2c5bc15 AM |
2423 | fnvlist_add_uint64(spa->spa_load_info, |
2424 | ZPOOL_CONFIG_LOAD_META_ERRORS, sle.sle_meta_count); | |
65ad5d11 AJ |
2425 | fnvlist_add_uint64(spa->spa_load_info, |
2426 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count); | |
428870ff BB |
2427 | } else { |
2428 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
2429 | } | |
2430 | ||
afd2f7b7 PZ |
2431 | if (spa_load_verify_dryrun) |
2432 | return (0); | |
2433 | ||
428870ff BB |
2434 | if (error) { |
2435 | if (error != ENXIO && error != EIO) | |
2e528b49 | 2436 | error = SET_ERROR(EIO); |
428870ff BB |
2437 | return (error); |
2438 | } | |
2439 | ||
2440 | return (verify_ok ? 0 : EIO); | |
2441 | } | |
2442 | ||
2443 | /* | |
2444 | * Find a value in the pool props object. | |
2445 | */ | |
2446 | static void | |
2447 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
2448 | { | |
2449 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
2450 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
2451 | } | |
2452 | ||
2453 | /* | |
2454 | * Find a value in the pool directory object. | |
2455 | */ | |
2456 | static int | |
4a0ee12a | 2457 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) |
428870ff | 2458 | { |
4a0ee12a PZ |
2459 | int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
2460 | name, sizeof (uint64_t), 1, val); | |
2461 | ||
2462 | if (error != 0 && (error != ENOENT || log_enoent)) { | |
2463 | spa_load_failed(spa, "couldn't get '%s' value in MOS directory " | |
2464 | "[error=%d]", name, error); | |
2465 | } | |
2466 | ||
2467 | return (error); | |
428870ff BB |
2468 | } |
2469 | ||
2470 | static int | |
2471 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
2472 | { | |
2473 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
a1d477c2 | 2474 | return (SET_ERROR(err)); |
428870ff BB |
2475 | } |
2476 | ||
37f03da8 SH |
2477 | boolean_t |
2478 | spa_livelist_delete_check(spa_t *spa) | |
2479 | { | |
2480 | return (spa->spa_livelists_to_delete != 0); | |
2481 | } | |
2482 | ||
37f03da8 SH |
2483 | static boolean_t |
2484 | spa_livelist_delete_cb_check(void *arg, zthr_t *z) | |
2485 | { | |
14e4e3cb | 2486 | (void) z; |
37f03da8 SH |
2487 | spa_t *spa = arg; |
2488 | return (spa_livelist_delete_check(spa)); | |
2489 | } | |
2490 | ||
2491 | static int | |
2492 | delete_blkptr_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
2493 | { | |
2494 | spa_t *spa = arg; | |
2495 | zio_free(spa, tx->tx_txg, bp); | |
2496 | dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD, | |
2497 | -bp_get_dsize_sync(spa, bp), | |
2498 | -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx); | |
2499 | return (0); | |
2500 | } | |
2501 | ||
2502 | static int | |
2503 | dsl_get_next_livelist_obj(objset_t *os, uint64_t zap_obj, uint64_t *llp) | |
2504 | { | |
2505 | int err; | |
2506 | zap_cursor_t zc; | |
2507 | zap_attribute_t za; | |
2508 | zap_cursor_init(&zc, os, zap_obj); | |
2509 | err = zap_cursor_retrieve(&zc, &za); | |
2510 | zap_cursor_fini(&zc); | |
2511 | if (err == 0) | |
2512 | *llp = za.za_first_integer; | |
2513 | return (err); | |
2514 | } | |
2515 | ||
2516 | /* | |
2517 | * Components of livelist deletion that must be performed in syncing | |
2518 | * context: freeing block pointers and updating the pool-wide data | |
2519 | * structures to indicate how much work is left to do | |
2520 | */ | |
2521 | typedef struct sublist_delete_arg { | |
2522 | spa_t *spa; | |
2523 | dsl_deadlist_t *ll; | |
2524 | uint64_t key; | |
2525 | bplist_t *to_free; | |
2526 | } sublist_delete_arg_t; | |
2527 | ||
2528 | static void | |
2529 | sublist_delete_sync(void *arg, dmu_tx_t *tx) | |
2530 | { | |
2531 | sublist_delete_arg_t *sda = arg; | |
2532 | spa_t *spa = sda->spa; | |
2533 | dsl_deadlist_t *ll = sda->ll; | |
2534 | uint64_t key = sda->key; | |
2535 | bplist_t *to_free = sda->to_free; | |
2536 | ||
2537 | bplist_iterate(to_free, delete_blkptr_cb, spa, tx); | |
2538 | dsl_deadlist_remove_entry(ll, key, tx); | |
2539 | } | |
2540 | ||
2541 | typedef struct livelist_delete_arg { | |
2542 | spa_t *spa; | |
2543 | uint64_t ll_obj; | |
2544 | uint64_t zap_obj; | |
2545 | } livelist_delete_arg_t; | |
2546 | ||
2547 | static void | |
2548 | livelist_delete_sync(void *arg, dmu_tx_t *tx) | |
2549 | { | |
2550 | livelist_delete_arg_t *lda = arg; | |
2551 | spa_t *spa = lda->spa; | |
2552 | uint64_t ll_obj = lda->ll_obj; | |
2553 | uint64_t zap_obj = lda->zap_obj; | |
2554 | objset_t *mos = spa->spa_meta_objset; | |
2555 | uint64_t count; | |
2556 | ||
2557 | /* free the livelist and decrement the feature count */ | |
2558 | VERIFY0(zap_remove_int(mos, zap_obj, ll_obj, tx)); | |
2559 | dsl_deadlist_free(mos, ll_obj, tx); | |
2560 | spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx); | |
2561 | VERIFY0(zap_count(mos, zap_obj, &count)); | |
2562 | if (count == 0) { | |
2563 | /* no more livelists to delete */ | |
2564 | VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT, | |
2565 | DMU_POOL_DELETED_CLONES, tx)); | |
2566 | VERIFY0(zap_destroy(mos, zap_obj, tx)); | |
2567 | spa->spa_livelists_to_delete = 0; | |
e60e158e | 2568 | spa_notify_waiters(spa); |
37f03da8 SH |
2569 | } |
2570 | } | |
2571 | ||
2572 | /* | |
2573 | * Load in the value for the livelist to be removed and open it. Then, | |
2574 | * load its first sublist and determine which block pointers should actually | |
2575 | * be freed. Then, call a synctask which performs the actual frees and updates | |
2576 | * the pool-wide livelist data. | |
2577 | */ | |
65c7cc49 | 2578 | static void |
37f03da8 SH |
2579 | spa_livelist_delete_cb(void *arg, zthr_t *z) |
2580 | { | |
2581 | spa_t *spa = arg; | |
2582 | uint64_t ll_obj = 0, count; | |
2583 | objset_t *mos = spa->spa_meta_objset; | |
2584 | uint64_t zap_obj = spa->spa_livelists_to_delete; | |
2585 | /* | |
2586 | * Determine the next livelist to delete. This function should only | |
2587 | * be called if there is at least one deleted clone. | |
2588 | */ | |
2589 | VERIFY0(dsl_get_next_livelist_obj(mos, zap_obj, &ll_obj)); | |
2590 | VERIFY0(zap_count(mos, ll_obj, &count)); | |
2591 | if (count > 0) { | |
c9562576 | 2592 | dsl_deadlist_t *ll; |
37f03da8 SH |
2593 | dsl_deadlist_entry_t *dle; |
2594 | bplist_t to_free; | |
c9562576 PS |
2595 | ll = kmem_zalloc(sizeof (dsl_deadlist_t), KM_SLEEP); |
2596 | dsl_deadlist_open(ll, mos, ll_obj); | |
2597 | dle = dsl_deadlist_first(ll); | |
37f03da8 SH |
2598 | ASSERT3P(dle, !=, NULL); |
2599 | bplist_create(&to_free); | |
2600 | int err = dsl_process_sub_livelist(&dle->dle_bpobj, &to_free, | |
2601 | z, NULL); | |
2602 | if (err == 0) { | |
2603 | sublist_delete_arg_t sync_arg = { | |
2604 | .spa = spa, | |
c9562576 | 2605 | .ll = ll, |
37f03da8 SH |
2606 | .key = dle->dle_mintxg, |
2607 | .to_free = &to_free | |
2608 | }; | |
2609 | zfs_dbgmsg("deleting sublist (id %llu) from" | |
8e739b2c RE |
2610 | " livelist %llu, %lld remaining", |
2611 | (u_longlong_t)dle->dle_bpobj.bpo_object, | |
2612 | (u_longlong_t)ll_obj, (longlong_t)count - 1); | |
37f03da8 SH |
2613 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, |
2614 | sublist_delete_sync, &sync_arg, 0, | |
2615 | ZFS_SPACE_CHECK_DESTROY)); | |
2616 | } else { | |
d87676a9 | 2617 | VERIFY3U(err, ==, EINTR); |
37f03da8 SH |
2618 | } |
2619 | bplist_clear(&to_free); | |
2620 | bplist_destroy(&to_free); | |
c9562576 PS |
2621 | dsl_deadlist_close(ll); |
2622 | kmem_free(ll, sizeof (dsl_deadlist_t)); | |
37f03da8 SH |
2623 | } else { |
2624 | livelist_delete_arg_t sync_arg = { | |
2625 | .spa = spa, | |
2626 | .ll_obj = ll_obj, | |
2627 | .zap_obj = zap_obj | |
2628 | }; | |
8e739b2c RE |
2629 | zfs_dbgmsg("deletion of livelist %llu completed", |
2630 | (u_longlong_t)ll_obj); | |
37f03da8 SH |
2631 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, livelist_delete_sync, |
2632 | &sync_arg, 0, ZFS_SPACE_CHECK_DESTROY)); | |
2633 | } | |
2634 | } | |
2635 | ||
65c7cc49 | 2636 | static void |
37f03da8 SH |
2637 | spa_start_livelist_destroy_thread(spa_t *spa) |
2638 | { | |
2639 | ASSERT3P(spa->spa_livelist_delete_zthr, ==, NULL); | |
843e9ca2 SD |
2640 | spa->spa_livelist_delete_zthr = |
2641 | zthr_create("z_livelist_destroy", | |
6bc61d22 TN |
2642 | spa_livelist_delete_cb_check, spa_livelist_delete_cb, spa, |
2643 | minclsyspri); | |
37f03da8 SH |
2644 | } |
2645 | ||
2646 | typedef struct livelist_new_arg { | |
2647 | bplist_t *allocs; | |
2648 | bplist_t *frees; | |
2649 | } livelist_new_arg_t; | |
2650 | ||
2651 | static int | |
2652 | livelist_track_new_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
2653 | dmu_tx_t *tx) | |
2654 | { | |
2655 | ASSERT(tx == NULL); | |
2656 | livelist_new_arg_t *lna = arg; | |
2657 | if (bp_freed) { | |
2658 | bplist_append(lna->frees, bp); | |
2659 | } else { | |
2660 | bplist_append(lna->allocs, bp); | |
2661 | zfs_livelist_condense_new_alloc++; | |
2662 | } | |
2663 | return (0); | |
2664 | } | |
2665 | ||
2666 | typedef struct livelist_condense_arg { | |
2667 | spa_t *spa; | |
2668 | bplist_t to_keep; | |
2669 | uint64_t first_size; | |
2670 | uint64_t next_size; | |
2671 | } livelist_condense_arg_t; | |
2672 | ||
2673 | static void | |
2674 | spa_livelist_condense_sync(void *arg, dmu_tx_t *tx) | |
2675 | { | |
2676 | livelist_condense_arg_t *lca = arg; | |
2677 | spa_t *spa = lca->spa; | |
2678 | bplist_t new_frees; | |
2679 | dsl_dataset_t *ds = spa->spa_to_condense.ds; | |
2680 | ||
2681 | /* Have we been cancelled? */ | |
2682 | if (spa->spa_to_condense.cancelled) { | |
2683 | zfs_livelist_condense_sync_cancel++; | |
2684 | goto out; | |
2685 | } | |
2686 | ||
2687 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2688 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2689 | dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist; | |
2690 | ||
2691 | /* | |
2692 | * It's possible that the livelist was changed while the zthr was | |
2693 | * running. Therefore, we need to check for new blkptrs in the two | |
2694 | * entries being condensed and continue to track them in the livelist. | |
2695 | * Because of the way we handle remapped blkptrs (see dbuf_remap_impl), | |
2696 | * it's possible that the newly added blkptrs are FREEs or ALLOCs so | |
2697 | * we need to sort them into two different bplists. | |
2698 | */ | |
2699 | uint64_t first_obj = first->dle_bpobj.bpo_object; | |
2700 | uint64_t next_obj = next->dle_bpobj.bpo_object; | |
2701 | uint64_t cur_first_size = first->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2702 | uint64_t cur_next_size = next->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2703 | ||
2704 | bplist_create(&new_frees); | |
2705 | livelist_new_arg_t new_bps = { | |
2706 | .allocs = &lca->to_keep, | |
2707 | .frees = &new_frees, | |
2708 | }; | |
2709 | ||
2710 | if (cur_first_size > lca->first_size) { | |
2711 | VERIFY0(livelist_bpobj_iterate_from_nofree(&first->dle_bpobj, | |
2712 | livelist_track_new_cb, &new_bps, lca->first_size)); | |
2713 | } | |
2714 | if (cur_next_size > lca->next_size) { | |
2715 | VERIFY0(livelist_bpobj_iterate_from_nofree(&next->dle_bpobj, | |
2716 | livelist_track_new_cb, &new_bps, lca->next_size)); | |
2717 | } | |
2718 | ||
2719 | dsl_deadlist_clear_entry(first, ll, tx); | |
2720 | ASSERT(bpobj_is_empty(&first->dle_bpobj)); | |
2721 | dsl_deadlist_remove_entry(ll, next->dle_mintxg, tx); | |
2722 | ||
2723 | bplist_iterate(&lca->to_keep, dsl_deadlist_insert_alloc_cb, ll, tx); | |
2724 | bplist_iterate(&new_frees, dsl_deadlist_insert_free_cb, ll, tx); | |
2725 | bplist_destroy(&new_frees); | |
2726 | ||
2727 | char dsname[ZFS_MAX_DATASET_NAME_LEN]; | |
2728 | dsl_dataset_name(ds, dsname); | |
2729 | zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu " | |
2730 | "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu " | |
8e739b2c RE |
2731 | "(%llu blkptrs)", (u_longlong_t)tx->tx_txg, dsname, |
2732 | (u_longlong_t)ds->ds_object, (u_longlong_t)first_obj, | |
2733 | (u_longlong_t)cur_first_size, (u_longlong_t)next_obj, | |
2734 | (u_longlong_t)cur_next_size, | |
2735 | (u_longlong_t)first->dle_bpobj.bpo_object, | |
2736 | (u_longlong_t)first->dle_bpobj.bpo_phys->bpo_num_blkptrs); | |
37f03da8 SH |
2737 | out: |
2738 | dmu_buf_rele(ds->ds_dbuf, spa); | |
2739 | spa->spa_to_condense.ds = NULL; | |
2740 | bplist_clear(&lca->to_keep); | |
2741 | bplist_destroy(&lca->to_keep); | |
2742 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2743 | spa->spa_to_condense.syncing = B_FALSE; | |
2744 | } | |
2745 | ||
65c7cc49 | 2746 | static void |
37f03da8 SH |
2747 | spa_livelist_condense_cb(void *arg, zthr_t *t) |
2748 | { | |
2749 | while (zfs_livelist_condense_zthr_pause && | |
2750 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2751 | delay(1); | |
2752 | ||
2753 | spa_t *spa = arg; | |
2754 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2755 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2756 | uint64_t first_size, next_size; | |
2757 | ||
2758 | livelist_condense_arg_t *lca = | |
2759 | kmem_alloc(sizeof (livelist_condense_arg_t), KM_SLEEP); | |
2760 | bplist_create(&lca->to_keep); | |
2761 | ||
2762 | /* | |
2763 | * Process the livelists (matching FREEs and ALLOCs) in open context | |
2764 | * so we have minimal work in syncing context to condense. | |
2765 | * | |
2766 | * We save bpobj sizes (first_size and next_size) to use later in | |
2767 | * syncing context to determine if entries were added to these sublists | |
2768 | * while in open context. This is possible because the clone is still | |
2769 | * active and open for normal writes and we want to make sure the new, | |
2770 | * unprocessed blockpointers are inserted into the livelist normally. | |
2771 | * | |
2772 | * Note that dsl_process_sub_livelist() both stores the size number of | |
2773 | * blockpointers and iterates over them while the bpobj's lock held, so | |
2774 | * the sizes returned to us are consistent which what was actually | |
2775 | * processed. | |
2776 | */ | |
2777 | int err = dsl_process_sub_livelist(&first->dle_bpobj, &lca->to_keep, t, | |
2778 | &first_size); | |
2779 | if (err == 0) | |
2780 | err = dsl_process_sub_livelist(&next->dle_bpobj, &lca->to_keep, | |
2781 | t, &next_size); | |
2782 | ||
2783 | if (err == 0) { | |
2784 | while (zfs_livelist_condense_sync_pause && | |
2785 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2786 | delay(1); | |
2787 | ||
2788 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
2789 | dmu_tx_mark_netfree(tx); | |
2790 | dmu_tx_hold_space(tx, 1); | |
2791 | err = dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE); | |
2792 | if (err == 0) { | |
2793 | /* | |
2794 | * Prevent the condense zthr restarting before | |
2795 | * the synctask completes. | |
2796 | */ | |
2797 | spa->spa_to_condense.syncing = B_TRUE; | |
2798 | lca->spa = spa; | |
2799 | lca->first_size = first_size; | |
2800 | lca->next_size = next_size; | |
2801 | dsl_sync_task_nowait(spa_get_dsl(spa), | |
38080324 | 2802 | spa_livelist_condense_sync, lca, tx); |
37f03da8 SH |
2803 | dmu_tx_commit(tx); |
2804 | return; | |
2805 | } | |
2806 | } | |
2807 | /* | |
2808 | * Condensing can not continue: either it was externally stopped or | |
2809 | * we were unable to assign to a tx because the pool has run out of | |
2810 | * space. In the second case, we'll just end up trying to condense | |
2811 | * again in a later txg. | |
2812 | */ | |
2813 | ASSERT(err != 0); | |
2814 | bplist_clear(&lca->to_keep); | |
2815 | bplist_destroy(&lca->to_keep); | |
2816 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2817 | dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, spa); | |
2818 | spa->spa_to_condense.ds = NULL; | |
2819 | if (err == EINTR) | |
2820 | zfs_livelist_condense_zthr_cancel++; | |
2821 | } | |
2822 | ||
37f03da8 SH |
2823 | /* |
2824 | * Check that there is something to condense but that a condense is not | |
2825 | * already in progress and that condensing has not been cancelled. | |
2826 | */ | |
2827 | static boolean_t | |
2828 | spa_livelist_condense_cb_check(void *arg, zthr_t *z) | |
2829 | { | |
14e4e3cb | 2830 | (void) z; |
37f03da8 SH |
2831 | spa_t *spa = arg; |
2832 | if ((spa->spa_to_condense.ds != NULL) && | |
2833 | (spa->spa_to_condense.syncing == B_FALSE) && | |
2834 | (spa->spa_to_condense.cancelled == B_FALSE)) { | |
2835 | return (B_TRUE); | |
2836 | } | |
2837 | return (B_FALSE); | |
2838 | } | |
2839 | ||
65c7cc49 | 2840 | static void |
37f03da8 SH |
2841 | spa_start_livelist_condensing_thread(spa_t *spa) |
2842 | { | |
2843 | spa->spa_to_condense.ds = NULL; | |
2844 | spa->spa_to_condense.first = NULL; | |
2845 | spa->spa_to_condense.next = NULL; | |
2846 | spa->spa_to_condense.syncing = B_FALSE; | |
2847 | spa->spa_to_condense.cancelled = B_FALSE; | |
2848 | ||
2849 | ASSERT3P(spa->spa_livelist_condense_zthr, ==, NULL); | |
843e9ca2 SD |
2850 | spa->spa_livelist_condense_zthr = |
2851 | zthr_create("z_livelist_condense", | |
2852 | spa_livelist_condense_cb_check, | |
6bc61d22 | 2853 | spa_livelist_condense_cb, spa, minclsyspri); |
37f03da8 SH |
2854 | } |
2855 | ||
9d5b5245 SD |
2856 | static void |
2857 | spa_spawn_aux_threads(spa_t *spa) | |
2858 | { | |
2859 | ASSERT(spa_writeable(spa)); | |
2860 | ||
2861 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2862 | ||
2863 | spa_start_indirect_condensing_thread(spa); | |
37f03da8 SH |
2864 | spa_start_livelist_destroy_thread(spa); |
2865 | spa_start_livelist_condensing_thread(spa); | |
d2734cce SD |
2866 | |
2867 | ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); | |
2868 | spa->spa_checkpoint_discard_zthr = | |
843e9ca2 SD |
2869 | zthr_create("z_checkpoint_discard", |
2870 | spa_checkpoint_discard_thread_check, | |
6bc61d22 | 2871 | spa_checkpoint_discard_thread, spa, minclsyspri); |
9d5b5245 SD |
2872 | } |
2873 | ||
428870ff BB |
2874 | /* |
2875 | * Fix up config after a partly-completed split. This is done with the | |
2876 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
2877 | * pool have that entry in their config, but only the splitting one contains | |
2878 | * a list of all the guids of the vdevs that are being split off. | |
2879 | * | |
2880 | * This function determines what to do with that list: either rejoin | |
2881 | * all the disks to the pool, or complete the splitting process. To attempt | |
2882 | * the rejoin, each disk that is offlined is marked online again, and | |
2883 | * we do a reopen() call. If the vdev label for every disk that was | |
2884 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
2885 | * then we call vdev_split() on each disk, and complete the split. | |
2886 | * | |
2887 | * Otherwise we leave the config alone, with all the vdevs in place in | |
2888 | * the original pool. | |
2889 | */ | |
2890 | static void | |
2891 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
2892 | { | |
2893 | uint_t extracted; | |
2894 | uint64_t *glist; | |
2895 | uint_t i, gcount; | |
2896 | nvlist_t *nvl; | |
2897 | vdev_t **vd; | |
2898 | boolean_t attempt_reopen; | |
2899 | ||
2900 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
2901 | return; | |
2902 | ||
2903 | /* check that the config is complete */ | |
2904 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
2905 | &glist, &gcount) != 0) | |
2906 | return; | |
2907 | ||
79c76d5b | 2908 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); |
428870ff BB |
2909 | |
2910 | /* attempt to online all the vdevs & validate */ | |
2911 | attempt_reopen = B_TRUE; | |
2912 | for (i = 0; i < gcount; i++) { | |
2913 | if (glist[i] == 0) /* vdev is hole */ | |
2914 | continue; | |
2915 | ||
2916 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
2917 | if (vd[i] == NULL) { | |
2918 | /* | |
2919 | * Don't bother attempting to reopen the disks; | |
2920 | * just do the split. | |
2921 | */ | |
2922 | attempt_reopen = B_FALSE; | |
2923 | } else { | |
2924 | /* attempt to re-online it */ | |
2925 | vd[i]->vdev_offline = B_FALSE; | |
2926 | } | |
2927 | } | |
2928 | ||
2929 | if (attempt_reopen) { | |
2930 | vdev_reopen(spa->spa_root_vdev); | |
2931 | ||
2932 | /* check each device to see what state it's in */ | |
2933 | for (extracted = 0, i = 0; i < gcount; i++) { | |
2934 | if (vd[i] != NULL && | |
2935 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
2936 | break; | |
2937 | ++extracted; | |
2938 | } | |
2939 | } | |
2940 | ||
2941 | /* | |
2942 | * If every disk has been moved to the new pool, or if we never | |
2943 | * even attempted to look at them, then we split them off for | |
2944 | * good. | |
2945 | */ | |
2946 | if (!attempt_reopen || gcount == extracted) { | |
2947 | for (i = 0; i < gcount; i++) | |
2948 | if (vd[i] != NULL) | |
2949 | vdev_split(vd[i]); | |
2950 | vdev_reopen(spa->spa_root_vdev); | |
2951 | } | |
2952 | ||
2953 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
2954 | } | |
2955 | ||
2956 | static int | |
6cb8e530 | 2957 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) |
428870ff | 2958 | { |
428870ff BB |
2959 | char *ereport = FM_EREPORT_ZFS_POOL; |
2960 | int error; | |
428870ff | 2961 | |
6cb8e530 | 2962 | spa->spa_load_state = state; |
ca95f70d OF |
2963 | (void) spa_import_progress_set_state(spa_guid(spa), |
2964 | spa_load_state(spa)); | |
9ae529ec | 2965 | |
6cb8e530 | 2966 | gethrestime(&spa->spa_loaded_ts); |
d2734cce | 2967 | error = spa_load_impl(spa, type, &ereport); |
428870ff | 2968 | |
0c66c32d JG |
2969 | /* |
2970 | * Don't count references from objsets that are already closed | |
2971 | * and are making their way through the eviction process. | |
2972 | */ | |
2973 | spa_evicting_os_wait(spa); | |
424fd7c3 | 2974 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
572e2857 BB |
2975 | if (error) { |
2976 | if (error != EEXIST) { | |
2977 | spa->spa_loaded_ts.tv_sec = 0; | |
2978 | spa->spa_loaded_ts.tv_nsec = 0; | |
2979 | } | |
2980 | if (error != EBADF) { | |
1144586b | 2981 | (void) zfs_ereport_post(ereport, spa, |
4f072827 | 2982 | NULL, NULL, NULL, 0); |
572e2857 BB |
2983 | } |
2984 | } | |
428870ff BB |
2985 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; |
2986 | spa->spa_ena = 0; | |
2987 | ||
ca95f70d OF |
2988 | (void) spa_import_progress_set_state(spa_guid(spa), |
2989 | spa_load_state(spa)); | |
2990 | ||
428870ff BB |
2991 | return (error); |
2992 | } | |
2993 | ||
33cf67cd | 2994 | #ifdef ZFS_DEBUG |
e0ab3ab5 JS |
2995 | /* |
2996 | * Count the number of per-vdev ZAPs associated with all of the vdevs in the | |
2997 | * vdev tree rooted in the given vd, and ensure that each ZAP is present in the | |
2998 | * spa's per-vdev ZAP list. | |
2999 | */ | |
3000 | static uint64_t | |
3001 | vdev_count_verify_zaps(vdev_t *vd) | |
3002 | { | |
3003 | spa_t *spa = vd->vdev_spa; | |
3004 | uint64_t total = 0; | |
e0ab3ab5 JS |
3005 | |
3006 | if (vd->vdev_top_zap != 0) { | |
3007 | total++; | |
3008 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
3009 | spa->spa_all_vdev_zaps, vd->vdev_top_zap)); | |
3010 | } | |
3011 | if (vd->vdev_leaf_zap != 0) { | |
3012 | total++; | |
3013 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
3014 | spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); | |
3015 | } | |
3016 | ||
1c27024e | 3017 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
3018 | total += vdev_count_verify_zaps(vd->vdev_child[i]); |
3019 | } | |
3020 | ||
3021 | return (total); | |
3022 | } | |
36542b06 AZ |
3023 | #else |
3024 | #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0) | |
33cf67cd | 3025 | #endif |
e0ab3ab5 | 3026 | |
379ca9cf OF |
3027 | /* |
3028 | * Determine whether the activity check is required. | |
3029 | */ | |
3030 | static boolean_t | |
bbffb59e BB |
3031 | spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label, |
3032 | nvlist_t *config) | |
379ca9cf OF |
3033 | { |
3034 | uint64_t state = 0; | |
3035 | uint64_t hostid = 0; | |
3036 | uint64_t tryconfig_txg = 0; | |
3037 | uint64_t tryconfig_timestamp = 0; | |
060f0226 | 3038 | uint16_t tryconfig_mmp_seq = 0; |
379ca9cf OF |
3039 | nvlist_t *nvinfo; |
3040 | ||
3041 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3042 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
3043 | (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, | |
3044 | &tryconfig_txg); | |
3045 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
3046 | &tryconfig_timestamp); | |
060f0226 OF |
3047 | (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ, |
3048 | &tryconfig_mmp_seq); | |
379ca9cf OF |
3049 | } |
3050 | ||
3051 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); | |
379ca9cf OF |
3052 | |
3053 | /* | |
3054 | * Disable the MMP activity check - This is used by zdb which | |
3055 | * is intended to be used on potentially active pools. | |
3056 | */ | |
3057 | if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) | |
3058 | return (B_FALSE); | |
3059 | ||
3060 | /* | |
3061 | * Skip the activity check when the MMP feature is disabled. | |
3062 | */ | |
3063 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) | |
3064 | return (B_FALSE); | |
ca95f70d | 3065 | |
379ca9cf | 3066 | /* |
060f0226 OF |
3067 | * If the tryconfig_ values are nonzero, they are the results of an |
3068 | * earlier tryimport. If they all match the uberblock we just found, | |
3069 | * then the pool has not changed and we return false so we do not test | |
3070 | * a second time. | |
379ca9cf OF |
3071 | */ |
3072 | if (tryconfig_txg && tryconfig_txg == ub->ub_txg && | |
060f0226 OF |
3073 | tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp && |
3074 | tryconfig_mmp_seq && tryconfig_mmp_seq == | |
3075 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) | |
379ca9cf OF |
3076 | return (B_FALSE); |
3077 | ||
3078 | /* | |
3079 | * Allow the activity check to be skipped when importing the pool | |
bbffb59e BB |
3080 | * on the same host which last imported it. Since the hostid from |
3081 | * configuration may be stale use the one read from the label. | |
379ca9cf | 3082 | */ |
bbffb59e BB |
3083 | if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) |
3084 | hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); | |
3085 | ||
25f06d67 | 3086 | if (hostid == spa_get_hostid(spa)) |
379ca9cf OF |
3087 | return (B_FALSE); |
3088 | ||
3089 | /* | |
3090 | * Skip the activity test when the pool was cleanly exported. | |
3091 | */ | |
3092 | if (state != POOL_STATE_ACTIVE) | |
3093 | return (B_FALSE); | |
3094 | ||
3095 | return (B_TRUE); | |
3096 | } | |
3097 | ||
060f0226 OF |
3098 | /* |
3099 | * Nanoseconds the activity check must watch for changes on-disk. | |
3100 | */ | |
3101 | static uint64_t | |
3102 | spa_activity_check_duration(spa_t *spa, uberblock_t *ub) | |
3103 | { | |
3104 | uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); | |
3105 | uint64_t multihost_interval = MSEC2NSEC( | |
3106 | MMP_INTERVAL_OK(zfs_multihost_interval)); | |
3107 | uint64_t import_delay = MAX(NANOSEC, import_intervals * | |
3108 | multihost_interval); | |
3109 | ||
3110 | /* | |
3111 | * Local tunables determine a minimum duration except for the case | |
3112 | * where we know when the remote host will suspend the pool if MMP | |
3113 | * writes do not land. | |
3114 | * | |
3115 | * See Big Theory comment at the top of mmp.c for the reasoning behind | |
3116 | * these cases and times. | |
3117 | */ | |
3118 | ||
3119 | ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100); | |
3120 | ||
3121 | if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3122 | MMP_FAIL_INT(ub) > 0) { | |
3123 | ||
3124 | /* MMP on remote host will suspend pool after failed writes */ | |
3125 | import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) * | |
3126 | MMP_IMPORT_SAFETY_FACTOR / 100; | |
3127 | ||
3128 | zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp " | |
3129 | "mmp_fails=%llu ub_mmp mmp_interval=%llu " | |
8e739b2c RE |
3130 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3131 | (u_longlong_t)MMP_FAIL_INT(ub), | |
3132 | (u_longlong_t)MMP_INTERVAL(ub), | |
3133 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3134 | |
3135 | } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3136 | MMP_FAIL_INT(ub) == 0) { | |
3137 | ||
3138 | /* MMP on remote host will never suspend pool */ | |
3139 | import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) + | |
3140 | ub->ub_mmp_delay) * import_intervals); | |
3141 | ||
3142 | zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp " | |
3143 | "mmp_interval=%llu ub_mmp_delay=%llu " | |
8e739b2c RE |
3144 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3145 | (u_longlong_t)MMP_INTERVAL(ub), | |
3146 | (u_longlong_t)ub->ub_mmp_delay, | |
3147 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3148 | |
3149 | } else if (MMP_VALID(ub)) { | |
3150 | /* | |
e1cfd73f | 3151 | * zfs-0.7 compatibility case |
060f0226 OF |
3152 | */ |
3153 | ||
3154 | import_delay = MAX(import_delay, (multihost_interval + | |
3155 | ub->ub_mmp_delay) * import_intervals); | |
3156 | ||
3157 | zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu " | |
8e739b2c RE |
3158 | "import_intervals=%llu leaves=%u", |
3159 | (u_longlong_t)import_delay, | |
3160 | (u_longlong_t)ub->ub_mmp_delay, | |
3161 | (u_longlong_t)import_intervals, | |
060f0226 OF |
3162 | vdev_count_leaves(spa)); |
3163 | } else { | |
3164 | /* Using local tunings is the only reasonable option */ | |
3165 | zfs_dbgmsg("pool last imported on non-MMP aware " | |
3166 | "host using import_delay=%llu multihost_interval=%llu " | |
8e739b2c RE |
3167 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3168 | (u_longlong_t)multihost_interval, | |
3169 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3170 | } |
3171 | ||
3172 | return (import_delay); | |
3173 | } | |
3174 | ||
379ca9cf OF |
3175 | /* |
3176 | * Perform the import activity check. If the user canceled the import or | |
3177 | * we detected activity then fail. | |
3178 | */ | |
3179 | static int | |
3180 | spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) | |
3181 | { | |
379ca9cf OF |
3182 | uint64_t txg = ub->ub_txg; |
3183 | uint64_t timestamp = ub->ub_timestamp; | |
060f0226 OF |
3184 | uint64_t mmp_config = ub->ub_mmp_config; |
3185 | uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0; | |
3186 | uint64_t import_delay; | |
379ca9cf OF |
3187 | hrtime_t import_expire; |
3188 | nvlist_t *mmp_label = NULL; | |
3189 | vdev_t *rvd = spa->spa_root_vdev; | |
3190 | kcondvar_t cv; | |
3191 | kmutex_t mtx; | |
3192 | int error = 0; | |
3193 | ||
3194 | cv_init(&cv, NULL, CV_DEFAULT, NULL); | |
3195 | mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); | |
3196 | mutex_enter(&mtx); | |
3197 | ||
3198 | /* | |
3199 | * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed | |
3200 | * during the earlier tryimport. If the txg recorded there is 0 then | |
3201 | * the pool is known to be active on another host. | |
3202 | * | |
060f0226 | 3203 | * Otherwise, the pool might be in use on another host. Check for |
379ca9cf OF |
3204 | * changes in the uberblocks on disk if necessary. |
3205 | */ | |
3206 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3207 | nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, | |
3208 | ZPOOL_CONFIG_LOAD_INFO); | |
3209 | ||
3210 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && | |
3211 | fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { | |
3212 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
3213 | error = SET_ERROR(EREMOTEIO); | |
3214 | goto out; | |
3215 | } | |
3216 | } | |
3217 | ||
060f0226 | 3218 | import_delay = spa_activity_check_duration(spa, ub); |
533ea041 | 3219 | |
379ca9cf | 3220 | /* Add a small random factor in case of simultaneous imports (0-25%) */ |
29274c9f | 3221 | import_delay += import_delay * random_in_range(250) / 1000; |
ca95f70d OF |
3222 | |
3223 | import_expire = gethrtime() + import_delay; | |
379ca9cf OF |
3224 | |
3225 | while (gethrtime() < import_expire) { | |
ca95f70d OF |
3226 | (void) spa_import_progress_set_mmp_check(spa_guid(spa), |
3227 | NSEC2SEC(import_expire - gethrtime())); | |
3228 | ||
379ca9cf OF |
3229 | vdev_uberblock_load(rvd, ub, &mmp_label); |
3230 | ||
060f0226 OF |
3231 | if (txg != ub->ub_txg || timestamp != ub->ub_timestamp || |
3232 | mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) { | |
3233 | zfs_dbgmsg("multihost activity detected " | |
3234 | "txg %llu ub_txg %llu " | |
3235 | "timestamp %llu ub_timestamp %llu " | |
3236 | "mmp_config %#llx ub_mmp_config %#llx", | |
8e739b2c RE |
3237 | (u_longlong_t)txg, (u_longlong_t)ub->ub_txg, |
3238 | (u_longlong_t)timestamp, | |
3239 | (u_longlong_t)ub->ub_timestamp, | |
3240 | (u_longlong_t)mmp_config, | |
3241 | (u_longlong_t)ub->ub_mmp_config); | |
060f0226 | 3242 | |
379ca9cf OF |
3243 | error = SET_ERROR(EREMOTEIO); |
3244 | break; | |
3245 | } | |
3246 | ||
3247 | if (mmp_label) { | |
3248 | nvlist_free(mmp_label); | |
3249 | mmp_label = NULL; | |
3250 | } | |
3251 | ||
3252 | error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); | |
3253 | if (error != -1) { | |
3254 | error = SET_ERROR(EINTR); | |
3255 | break; | |
3256 | } | |
3257 | error = 0; | |
3258 | } | |
3259 | ||
3260 | out: | |
3261 | mutex_exit(&mtx); | |
3262 | mutex_destroy(&mtx); | |
3263 | cv_destroy(&cv); | |
3264 | ||
3265 | /* | |
3266 | * If the pool is determined to be active store the status in the | |
3267 | * spa->spa_load_info nvlist. If the remote hostname or hostid are | |
3268 | * available from configuration read from disk store them as well. | |
3269 | * This allows 'zpool import' to generate a more useful message. | |
3270 | * | |
3271 | * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) | |
3272 | * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool | |
3273 | * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool | |
3274 | */ | |
3275 | if (error == EREMOTEIO) { | |
3276 | char *hostname = "<unknown>"; | |
3277 | uint64_t hostid = 0; | |
3278 | ||
3279 | if (mmp_label) { | |
3280 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { | |
3281 | hostname = fnvlist_lookup_string(mmp_label, | |
3282 | ZPOOL_CONFIG_HOSTNAME); | |
3283 | fnvlist_add_string(spa->spa_load_info, | |
3284 | ZPOOL_CONFIG_MMP_HOSTNAME, hostname); | |
3285 | } | |
3286 | ||
3287 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { | |
3288 | hostid = fnvlist_lookup_uint64(mmp_label, | |
3289 | ZPOOL_CONFIG_HOSTID); | |
3290 | fnvlist_add_uint64(spa->spa_load_info, | |
3291 | ZPOOL_CONFIG_MMP_HOSTID, hostid); | |
3292 | } | |
3293 | } | |
3294 | ||
3295 | fnvlist_add_uint64(spa->spa_load_info, | |
3296 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); | |
3297 | fnvlist_add_uint64(spa->spa_load_info, | |
3298 | ZPOOL_CONFIG_MMP_TXG, 0); | |
3299 | ||
3300 | error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); | |
3301 | } | |
3302 | ||
3303 | if (mmp_label) | |
3304 | nvlist_free(mmp_label); | |
3305 | ||
3306 | return (error); | |
3307 | } | |
3308 | ||
9eb7b46e | 3309 | static int |
6cb8e530 PZ |
3310 | spa_verify_host(spa_t *spa, nvlist_t *mos_config) |
3311 | { | |
3312 | uint64_t hostid; | |
3313 | char *hostname; | |
3314 | uint64_t myhostid = 0; | |
3315 | ||
3316 | if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, | |
3317 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
3318 | hostname = fnvlist_lookup_string(mos_config, | |
3319 | ZPOOL_CONFIG_HOSTNAME); | |
3320 | ||
3321 | myhostid = zone_get_hostid(NULL); | |
3322 | ||
3323 | if (hostid != 0 && myhostid != 0 && hostid != myhostid) { | |
3324 | cmn_err(CE_WARN, "pool '%s' could not be " | |
3325 | "loaded as it was last accessed by " | |
3326 | "another system (host: %s hostid: 0x%llx). " | |
a2f944a1 RM |
3327 | "See: https://openzfs.github.io/openzfs-docs/msg/" |
3328 | "ZFS-8000-EY", | |
6cb8e530 PZ |
3329 | spa_name(spa), hostname, (u_longlong_t)hostid); |
3330 | spa_load_failed(spa, "hostid verification failed: pool " | |
3331 | "last accessed by host: %s (hostid: 0x%llx)", | |
3332 | hostname, (u_longlong_t)hostid); | |
3333 | return (SET_ERROR(EBADF)); | |
3334 | } | |
3335 | } | |
3336 | ||
3337 | return (0); | |
3338 | } | |
3339 | ||
3340 | static int | |
3341 | spa_ld_parse_config(spa_t *spa, spa_import_type_t type) | |
428870ff BB |
3342 | { |
3343 | int error = 0; | |
6cb8e530 | 3344 | nvlist_t *nvtree, *nvl, *config = spa->spa_config; |
1c27024e | 3345 | int parse; |
9eb7b46e | 3346 | vdev_t *rvd; |
6cb8e530 PZ |
3347 | uint64_t pool_guid; |
3348 | char *comment; | |
658fb802 | 3349 | char *compatibility; |
6cb8e530 PZ |
3350 | |
3351 | /* | |
3352 | * Versioning wasn't explicitly added to the label until later, so if | |
3353 | * it's not present treat it as the initial version. | |
3354 | */ | |
3355 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
3356 | &spa->spa_ubsync.ub_version) != 0) | |
3357 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
3358 | ||
3359 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { | |
3360 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3361 | ZPOOL_CONFIG_POOL_GUID); | |
3362 | return (SET_ERROR(EINVAL)); | |
3363 | } | |
3364 | ||
d2734cce SD |
3365 | /* |
3366 | * If we are doing an import, ensure that the pool is not already | |
3367 | * imported by checking if its pool guid already exists in the | |
3368 | * spa namespace. | |
3369 | * | |
3370 | * The only case that we allow an already imported pool to be | |
3371 | * imported again, is when the pool is checkpointed and we want to | |
3372 | * look at its checkpointed state from userland tools like zdb. | |
3373 | */ | |
3374 | #ifdef _KERNEL | |
3375 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3376 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3377 | spa_guid_exists(pool_guid, 0)) { | |
3378 | #else | |
3379 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3380 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3381 | spa_guid_exists(pool_guid, 0) && | |
3382 | !spa_importing_readonly_checkpoint(spa)) { | |
3383 | #endif | |
6cb8e530 PZ |
3384 | spa_load_failed(spa, "a pool with guid %llu is already open", |
3385 | (u_longlong_t)pool_guid); | |
3386 | return (SET_ERROR(EEXIST)); | |
3387 | } | |
3388 | ||
3389 | spa->spa_config_guid = pool_guid; | |
3390 | ||
3391 | nvlist_free(spa->spa_load_info); | |
3392 | spa->spa_load_info = fnvlist_alloc(); | |
3393 | ||
3394 | ASSERT(spa->spa_comment == NULL); | |
3395 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
3396 | spa->spa_comment = spa_strdup(comment); | |
3397 | ||
658fb802 CB |
3398 | ASSERT(spa->spa_compatibility == NULL); |
3399 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMPATIBILITY, | |
3400 | &compatibility) == 0) | |
3401 | spa->spa_compatibility = spa_strdup(compatibility); | |
3402 | ||
6cb8e530 PZ |
3403 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, |
3404 | &spa->spa_config_txg); | |
3405 | ||
3406 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) | |
3407 | spa->spa_config_splitting = fnvlist_dup(nvl); | |
428870ff | 3408 | |
4a0ee12a PZ |
3409 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { |
3410 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3411 | ZPOOL_CONFIG_VDEV_TREE); | |
2e528b49 | 3412 | return (SET_ERROR(EINVAL)); |
4a0ee12a | 3413 | } |
428870ff | 3414 | |
428870ff BB |
3415 | /* |
3416 | * Create "The Godfather" zio to hold all async IOs | |
3417 | */ | |
e022864d MA |
3418 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
3419 | KM_SLEEP); | |
1c27024e | 3420 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
3421 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
3422 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
3423 | ZIO_FLAG_GODFATHER); | |
3424 | } | |
428870ff BB |
3425 | |
3426 | /* | |
3427 | * Parse the configuration into a vdev tree. We explicitly set the | |
3428 | * value that will be returned by spa_version() since parsing the | |
3429 | * configuration requires knowing the version number. | |
3430 | */ | |
3431 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 PZ |
3432 | parse = (type == SPA_IMPORT_EXISTING ? |
3433 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
9eb7b46e | 3434 | error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); |
428870ff BB |
3435 | spa_config_exit(spa, SCL_ALL, FTAG); |
3436 | ||
4a0ee12a PZ |
3437 | if (error != 0) { |
3438 | spa_load_failed(spa, "unable to parse config [error=%d]", | |
3439 | error); | |
428870ff | 3440 | return (error); |
4a0ee12a | 3441 | } |
428870ff BB |
3442 | |
3443 | ASSERT(spa->spa_root_vdev == rvd); | |
c3520e7f MA |
3444 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); |
3445 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
428870ff BB |
3446 | |
3447 | if (type != SPA_IMPORT_ASSEMBLE) { | |
3448 | ASSERT(spa_guid(spa) == pool_guid); | |
3449 | } | |
3450 | ||
9eb7b46e PZ |
3451 | return (0); |
3452 | } | |
3453 | ||
6cb8e530 PZ |
3454 | /* |
3455 | * Recursively open all vdevs in the vdev tree. This function is called twice: | |
3456 | * first with the untrusted config, then with the trusted config. | |
3457 | */ | |
9eb7b46e PZ |
3458 | static int |
3459 | spa_ld_open_vdevs(spa_t *spa) | |
3460 | { | |
3461 | int error = 0; | |
3462 | ||
6cb8e530 PZ |
3463 | /* |
3464 | * spa_missing_tvds_allowed defines how many top-level vdevs can be | |
3465 | * missing/unopenable for the root vdev to be still considered openable. | |
3466 | */ | |
3467 | if (spa->spa_trust_config) { | |
3468 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; | |
3469 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { | |
3470 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; | |
3471 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { | |
3472 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; | |
3473 | } else { | |
3474 | spa->spa_missing_tvds_allowed = 0; | |
3475 | } | |
3476 | ||
3477 | spa->spa_missing_tvds_allowed = | |
3478 | MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); | |
3479 | ||
428870ff | 3480 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9eb7b46e | 3481 | error = vdev_open(spa->spa_root_vdev); |
428870ff | 3482 | spa_config_exit(spa, SCL_ALL, FTAG); |
6cb8e530 PZ |
3483 | |
3484 | if (spa->spa_missing_tvds != 0) { | |
3485 | spa_load_note(spa, "vdev tree has %lld missing top-level " | |
3486 | "vdevs.", (u_longlong_t)spa->spa_missing_tvds); | |
da92d5cb | 3487 | if (spa->spa_trust_config && (spa->spa_mode & SPA_MODE_WRITE)) { |
6cb8e530 PZ |
3488 | /* |
3489 | * Although theoretically we could allow users to open | |
3490 | * incomplete pools in RW mode, we'd need to add a lot | |
3491 | * of extra logic (e.g. adjust pool space to account | |
3492 | * for missing vdevs). | |
3493 | * This limitation also prevents users from accidentally | |
3494 | * opening the pool in RW mode during data recovery and | |
3495 | * damaging it further. | |
3496 | */ | |
3497 | spa_load_note(spa, "pools with missing top-level " | |
3498 | "vdevs can only be opened in read-only mode."); | |
3499 | error = SET_ERROR(ENXIO); | |
3500 | } else { | |
3501 | spa_load_note(spa, "current settings allow for maximum " | |
3502 | "%lld missing top-level vdevs at this stage.", | |
3503 | (u_longlong_t)spa->spa_missing_tvds_allowed); | |
3504 | } | |
3505 | } | |
4a0ee12a PZ |
3506 | if (error != 0) { |
3507 | spa_load_failed(spa, "unable to open vdev tree [error=%d]", | |
3508 | error); | |
3509 | } | |
6cb8e530 PZ |
3510 | if (spa->spa_missing_tvds != 0 || error != 0) |
3511 | vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); | |
9eb7b46e PZ |
3512 | |
3513 | return (error); | |
3514 | } | |
3515 | ||
6cb8e530 PZ |
3516 | /* |
3517 | * We need to validate the vdev labels against the configuration that | |
3518 | * we have in hand. This function is called twice: first with an untrusted | |
3519 | * config, then with a trusted config. The validation is more strict when the | |
3520 | * config is trusted. | |
3521 | */ | |
9eb7b46e | 3522 | static int |
6cb8e530 | 3523 | spa_ld_validate_vdevs(spa_t *spa) |
9eb7b46e PZ |
3524 | { |
3525 | int error = 0; | |
3526 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 3527 | |
6cb8e530 PZ |
3528 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3529 | error = vdev_validate(rvd); | |
3530 | spa_config_exit(spa, SCL_ALL, FTAG); | |
428870ff | 3531 | |
6cb8e530 PZ |
3532 | if (error != 0) { |
3533 | spa_load_failed(spa, "vdev_validate failed [error=%d]", error); | |
3534 | return (error); | |
3535 | } | |
428870ff | 3536 | |
6cb8e530 PZ |
3537 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { |
3538 | spa_load_failed(spa, "cannot open vdev tree after invalidating " | |
3539 | "some vdevs"); | |
3540 | vdev_dbgmsg_print_tree(rvd, 2); | |
3541 | return (SET_ERROR(ENXIO)); | |
428870ff BB |
3542 | } |
3543 | ||
9eb7b46e PZ |
3544 | return (0); |
3545 | } | |
3546 | ||
d2734cce SD |
3547 | static void |
3548 | spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) | |
3549 | { | |
3550 | spa->spa_state = POOL_STATE_ACTIVE; | |
3551 | spa->spa_ubsync = spa->spa_uberblock; | |
3552 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
3553 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
3554 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
3555 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
3556 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
3557 | spa->spa_prev_software_version = ub->ub_software_version; | |
3558 | } | |
3559 | ||
9eb7b46e | 3560 | static int |
6cb8e530 | 3561 | spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) |
9eb7b46e PZ |
3562 | { |
3563 | vdev_t *rvd = spa->spa_root_vdev; | |
3564 | nvlist_t *label; | |
3565 | uberblock_t *ub = &spa->spa_uberblock; | |
9eb7b46e PZ |
3566 | boolean_t activity_check = B_FALSE; |
3567 | ||
d2734cce SD |
3568 | /* |
3569 | * If we are opening the checkpointed state of the pool by | |
3570 | * rewinding to it, at this point we will have written the | |
3571 | * checkpointed uberblock to the vdev labels, so searching | |
3572 | * the labels will find the right uberblock. However, if | |
3573 | * we are opening the checkpointed state read-only, we have | |
3574 | * not modified the labels. Therefore, we must ignore the | |
3575 | * labels and continue using the spa_uberblock that was set | |
3576 | * by spa_ld_checkpoint_rewind. | |
3577 | * | |
3578 | * Note that it would be fine to ignore the labels when | |
3579 | * rewinding (opening writeable) as well. However, if we | |
3580 | * crash just after writing the labels, we will end up | |
3581 | * searching the labels. Doing so in the common case means | |
3582 | * that this code path gets exercised normally, rather than | |
3583 | * just in the edge case. | |
3584 | */ | |
3585 | if (ub->ub_checkpoint_txg != 0 && | |
3586 | spa_importing_readonly_checkpoint(spa)) { | |
3587 | spa_ld_select_uberblock_done(spa, ub); | |
3588 | return (0); | |
3589 | } | |
3590 | ||
428870ff BB |
3591 | /* |
3592 | * Find the best uberblock. | |
3593 | */ | |
9ae529ec | 3594 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
3595 | |
3596 | /* | |
3597 | * If we weren't able to find a single valid uberblock, return failure. | |
3598 | */ | |
9ae529ec CS |
3599 | if (ub->ub_txg == 0) { |
3600 | nvlist_free(label); | |
4a0ee12a | 3601 | spa_load_failed(spa, "no valid uberblock found"); |
428870ff | 3602 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 3603 | } |
428870ff | 3604 | |
ca95f70d OF |
3605 | if (spa->spa_load_max_txg != UINT64_MAX) { |
3606 | (void) spa_import_progress_set_max_txg(spa_guid(spa), | |
3607 | (u_longlong_t)spa->spa_load_max_txg); | |
3608 | } | |
4a0ee12a PZ |
3609 | spa_load_note(spa, "using uberblock with txg=%llu", |
3610 | (u_longlong_t)ub->ub_txg); | |
3611 | ||
3612 | ||
379ca9cf OF |
3613 | /* |
3614 | * For pools which have the multihost property on determine if the | |
3615 | * pool is truly inactive and can be safely imported. Prevent | |
3616 | * hosts which don't have a hostid set from importing the pool. | |
3617 | */ | |
6cb8e530 PZ |
3618 | activity_check = spa_activity_check_required(spa, ub, label, |
3619 | spa->spa_config); | |
379ca9cf | 3620 | if (activity_check) { |
379ca9cf | 3621 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && |
25f06d67 | 3622 | spa_get_hostid(spa) == 0) { |
379ca9cf OF |
3623 | nvlist_free(label); |
3624 | fnvlist_add_uint64(spa->spa_load_info, | |
3625 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3626 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3627 | } | |
3628 | ||
6cb8e530 | 3629 | int error = spa_activity_check(spa, ub, spa->spa_config); |
e889f0f5 OF |
3630 | if (error) { |
3631 | nvlist_free(label); | |
3632 | return (error); | |
3633 | } | |
3634 | ||
379ca9cf OF |
3635 | fnvlist_add_uint64(spa->spa_load_info, |
3636 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); | |
3637 | fnvlist_add_uint64(spa->spa_load_info, | |
3638 | ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); | |
060f0226 OF |
3639 | fnvlist_add_uint16(spa->spa_load_info, |
3640 | ZPOOL_CONFIG_MMP_SEQ, | |
3641 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)); | |
379ca9cf OF |
3642 | } |
3643 | ||
428870ff | 3644 | /* |
9ae529ec | 3645 | * If the pool has an unsupported version we can't open it. |
428870ff | 3646 | */ |
9ae529ec CS |
3647 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
3648 | nvlist_free(label); | |
4a0ee12a PZ |
3649 | spa_load_failed(spa, "version %llu is not supported", |
3650 | (u_longlong_t)ub->ub_version); | |
428870ff | 3651 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
3652 | } |
3653 | ||
3654 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3655 | nvlist_t *features; | |
3656 | ||
3657 | /* | |
3658 | * If we weren't able to find what's necessary for reading the | |
3659 | * MOS in the label, return failure. | |
3660 | */ | |
4a0ee12a PZ |
3661 | if (label == NULL) { |
3662 | spa_load_failed(spa, "label config unavailable"); | |
3663 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
3664 | ENXIO)); | |
3665 | } | |
3666 | ||
3667 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, | |
3668 | &features) != 0) { | |
9ae529ec | 3669 | nvlist_free(label); |
4a0ee12a PZ |
3670 | spa_load_failed(spa, "invalid label: '%s' missing", |
3671 | ZPOOL_CONFIG_FEATURES_FOR_READ); | |
9ae529ec CS |
3672 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3673 | ENXIO)); | |
3674 | } | |
3675 | ||
3676 | /* | |
3677 | * Update our in-core representation with the definitive values | |
3678 | * from the label. | |
3679 | */ | |
3680 | nvlist_free(spa->spa_label_features); | |
65ad5d11 | 3681 | spa->spa_label_features = fnvlist_dup(features); |
9ae529ec CS |
3682 | } |
3683 | ||
3684 | nvlist_free(label); | |
3685 | ||
3686 | /* | |
3687 | * Look through entries in the label nvlist's features_for_read. If | |
3688 | * there is a feature listed there which we don't understand then we | |
3689 | * cannot open a pool. | |
3690 | */ | |
3691 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3692 | nvlist_t *unsup_feat; | |
9ae529ec | 3693 | |
65ad5d11 | 3694 | unsup_feat = fnvlist_alloc(); |
9ae529ec | 3695 | |
1c27024e DB |
3696 | for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, |
3697 | NULL); nvp != NULL; | |
9ae529ec CS |
3698 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { |
3699 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
65ad5d11 AJ |
3700 | fnvlist_add_string(unsup_feat, |
3701 | nvpair_name(nvp), ""); | |
9ae529ec CS |
3702 | } |
3703 | } | |
3704 | ||
3705 | if (!nvlist_empty(unsup_feat)) { | |
65ad5d11 AJ |
3706 | fnvlist_add_nvlist(spa->spa_load_info, |
3707 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec | 3708 | nvlist_free(unsup_feat); |
4a0ee12a | 3709 | spa_load_failed(spa, "some features are unsupported"); |
9ae529ec CS |
3710 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3711 | ENOTSUP)); | |
3712 | } | |
3713 | ||
3714 | nvlist_free(unsup_feat); | |
3715 | } | |
428870ff | 3716 | |
428870ff BB |
3717 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { |
3718 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 | 3719 | spa_try_repair(spa, spa->spa_config); |
428870ff BB |
3720 | spa_config_exit(spa, SCL_ALL, FTAG); |
3721 | nvlist_free(spa->spa_config_splitting); | |
3722 | spa->spa_config_splitting = NULL; | |
3723 | } | |
3724 | ||
3725 | /* | |
3726 | * Initialize internal SPA structures. | |
3727 | */ | |
d2734cce | 3728 | spa_ld_select_uberblock_done(spa, ub); |
428870ff | 3729 | |
9eb7b46e PZ |
3730 | return (0); |
3731 | } | |
3732 | ||
3733 | static int | |
3734 | spa_ld_open_rootbp(spa_t *spa) | |
3735 | { | |
3736 | int error = 0; | |
3737 | vdev_t *rvd = spa->spa_root_vdev; | |
a1d477c2 | 3738 | |
9ae529ec | 3739 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
4a0ee12a PZ |
3740 | if (error != 0) { |
3741 | spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " | |
3742 | "[error=%d]", error); | |
428870ff | 3743 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3744 | } |
428870ff BB |
3745 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; |
3746 | ||
9eb7b46e PZ |
3747 | return (0); |
3748 | } | |
3749 | ||
3750 | static int | |
d2734cce | 3751 | spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, |
6cb8e530 | 3752 | boolean_t reloading) |
9eb7b46e | 3753 | { |
6cb8e530 PZ |
3754 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
3755 | nvlist_t *nv, *mos_config, *policy; | |
3756 | int error = 0, copy_error; | |
3757 | uint64_t healthy_tvds, healthy_tvds_mos; | |
3758 | uint64_t mos_config_txg; | |
9eb7b46e | 3759 | |
4a0ee12a PZ |
3760 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) |
3761 | != 0) | |
428870ff BB |
3762 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3763 | ||
a1d477c2 | 3764 | /* |
6cb8e530 PZ |
3765 | * If we're assembling a pool from a split, the config provided is |
3766 | * already trusted so there is nothing to do. | |
a1d477c2 | 3767 | */ |
6cb8e530 PZ |
3768 | if (type == SPA_IMPORT_ASSEMBLE) |
3769 | return (0); | |
3770 | ||
3771 | healthy_tvds = spa_healthy_core_tvds(spa); | |
a1d477c2 | 3772 | |
6cb8e530 PZ |
3773 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) |
3774 | != 0) { | |
3775 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
3776 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3777 | } | |
3778 | ||
3779 | /* | |
3780 | * If we are doing an open, pool owner wasn't verified yet, thus do | |
3781 | * the verification here. | |
3782 | */ | |
3783 | if (spa->spa_load_state == SPA_LOAD_OPEN) { | |
3784 | error = spa_verify_host(spa, mos_config); | |
3785 | if (error != 0) { | |
a1d477c2 | 3786 | nvlist_free(mos_config); |
6cb8e530 | 3787 | return (error); |
a1d477c2 | 3788 | } |
6cb8e530 PZ |
3789 | } |
3790 | ||
3791 | nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); | |
a1d477c2 | 3792 | |
6cb8e530 PZ |
3793 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3794 | ||
3795 | /* | |
3796 | * Build a new vdev tree from the trusted config | |
3797 | */ | |
b2255edc BB |
3798 | error = spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD); |
3799 | if (error != 0) { | |
3800 | nvlist_free(mos_config); | |
3801 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3802 | spa_load_failed(spa, "spa_config_parse failed [error=%d]", | |
3803 | error); | |
3804 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
3805 | } | |
6cb8e530 PZ |
3806 | |
3807 | /* | |
3808 | * Vdev paths in the MOS may be obsolete. If the untrusted config was | |
3809 | * obtained by scanning /dev/dsk, then it will have the right vdev | |
3810 | * paths. We update the trusted MOS config with this information. | |
3811 | * We first try to copy the paths with vdev_copy_path_strict, which | |
3812 | * succeeds only when both configs have exactly the same vdev tree. | |
3813 | * If that fails, we fall back to a more flexible method that has a | |
3814 | * best effort policy. | |
3815 | */ | |
3816 | copy_error = vdev_copy_path_strict(rvd, mrvd); | |
3817 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3818 | spa_load_note(spa, "provided vdev tree:"); | |
3819 | vdev_dbgmsg_print_tree(rvd, 2); | |
3820 | spa_load_note(spa, "MOS vdev tree:"); | |
3821 | vdev_dbgmsg_print_tree(mrvd, 2); | |
3822 | } | |
3823 | if (copy_error != 0) { | |
3824 | spa_load_note(spa, "vdev_copy_path_strict failed, falling " | |
3825 | "back to vdev_copy_path_relaxed"); | |
3826 | vdev_copy_path_relaxed(rvd, mrvd); | |
3827 | } | |
3828 | ||
3829 | vdev_close(rvd); | |
3830 | vdev_free(rvd); | |
3831 | spa->spa_root_vdev = mrvd; | |
3832 | rvd = mrvd; | |
3833 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3834 | ||
3835 | /* | |
3836 | * We will use spa_config if we decide to reload the spa or if spa_load | |
3837 | * fails and we rewind. We must thus regenerate the config using the | |
8a393be3 PZ |
3838 | * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to |
3839 | * pass settings on how to load the pool and is not stored in the MOS. | |
3840 | * We copy it over to our new, trusted config. | |
6cb8e530 PZ |
3841 | */ |
3842 | mos_config_txg = fnvlist_lookup_uint64(mos_config, | |
3843 | ZPOOL_CONFIG_POOL_TXG); | |
3844 | nvlist_free(mos_config); | |
3845 | mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); | |
8a393be3 | 3846 | if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, |
6cb8e530 | 3847 | &policy) == 0) |
8a393be3 | 3848 | fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); |
6cb8e530 PZ |
3849 | spa_config_set(spa, mos_config); |
3850 | spa->spa_config_source = SPA_CONFIG_SRC_MOS; | |
3851 | ||
3852 | /* | |
3853 | * Now that we got the config from the MOS, we should be more strict | |
3854 | * in checking blkptrs and can make assumptions about the consistency | |
3855 | * of the vdev tree. spa_trust_config must be set to true before opening | |
3856 | * vdevs in order for them to be writeable. | |
3857 | */ | |
3858 | spa->spa_trust_config = B_TRUE; | |
3859 | ||
3860 | /* | |
3861 | * Open and validate the new vdev tree | |
3862 | */ | |
3863 | error = spa_ld_open_vdevs(spa); | |
3864 | if (error != 0) | |
3865 | return (error); | |
3866 | ||
3867 | error = spa_ld_validate_vdevs(spa); | |
3868 | if (error != 0) | |
3869 | return (error); | |
3870 | ||
3871 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3872 | spa_load_note(spa, "final vdev tree:"); | |
3873 | vdev_dbgmsg_print_tree(rvd, 2); | |
3874 | } | |
3875 | ||
3876 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && | |
3877 | !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { | |
a1d477c2 | 3878 | /* |
6cb8e530 PZ |
3879 | * Sanity check to make sure that we are indeed loading the |
3880 | * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds | |
3881 | * in the config provided and they happened to be the only ones | |
3882 | * to have the latest uberblock, we could involuntarily perform | |
3883 | * an extreme rewind. | |
a1d477c2 | 3884 | */ |
6cb8e530 PZ |
3885 | healthy_tvds_mos = spa_healthy_core_tvds(spa); |
3886 | if (healthy_tvds_mos - healthy_tvds >= | |
3887 | SPA_SYNC_MIN_VDEVS) { | |
3888 | spa_load_note(spa, "config provided misses too many " | |
3889 | "top-level vdevs compared to MOS (%lld vs %lld). ", | |
3890 | (u_longlong_t)healthy_tvds, | |
3891 | (u_longlong_t)healthy_tvds_mos); | |
3892 | spa_load_note(spa, "vdev tree:"); | |
3893 | vdev_dbgmsg_print_tree(rvd, 2); | |
3894 | if (reloading) { | |
3895 | spa_load_failed(spa, "config was already " | |
3896 | "provided from MOS. Aborting."); | |
3897 | return (spa_vdev_err(rvd, | |
3898 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3899 | } | |
3900 | spa_load_note(spa, "spa must be reloaded using MOS " | |
3901 | "config"); | |
3902 | return (SET_ERROR(EAGAIN)); | |
4a0ee12a | 3903 | } |
a1d477c2 MA |
3904 | } |
3905 | ||
6cb8e530 PZ |
3906 | error = spa_check_for_missing_logs(spa); |
3907 | if (error != 0) | |
3908 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
3909 | ||
3910 | if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { | |
3911 | spa_load_failed(spa, "uberblock guid sum doesn't match MOS " | |
3912 | "guid sum (%llu != %llu)", | |
3913 | (u_longlong_t)spa->spa_uberblock.ub_guid_sum, | |
3914 | (u_longlong_t)rvd->vdev_guid_sum); | |
3915 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
3916 | ENXIO)); | |
3917 | } | |
3918 | ||
9eb7b46e PZ |
3919 | return (0); |
3920 | } | |
3921 | ||
3922 | static int | |
3923 | spa_ld_open_indirect_vdev_metadata(spa_t *spa) | |
3924 | { | |
3925 | int error = 0; | |
3926 | vdev_t *rvd = spa->spa_root_vdev; | |
3927 | ||
a1d477c2 MA |
3928 | /* |
3929 | * Everything that we read before spa_remove_init() must be stored | |
3930 | * on concreted vdevs. Therefore we do this as early as possible. | |
3931 | */ | |
4a0ee12a PZ |
3932 | error = spa_remove_init(spa); |
3933 | if (error != 0) { | |
3934 | spa_load_failed(spa, "spa_remove_init failed [error=%d]", | |
3935 | error); | |
a1d477c2 | 3936 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3937 | } |
a1d477c2 | 3938 | |
9eb7b46e PZ |
3939 | /* |
3940 | * Retrieve information needed to condense indirect vdev mappings. | |
3941 | */ | |
3942 | error = spa_condense_init(spa); | |
3943 | if (error != 0) { | |
4a0ee12a PZ |
3944 | spa_load_failed(spa, "spa_condense_init failed [error=%d]", |
3945 | error); | |
9eb7b46e PZ |
3946 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3947 | } | |
3948 | ||
3949 | return (0); | |
3950 | } | |
3951 | ||
3952 | static int | |
4a0ee12a | 3953 | spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) |
9eb7b46e PZ |
3954 | { |
3955 | int error = 0; | |
3956 | vdev_t *rvd = spa->spa_root_vdev; | |
3957 | ||
9ae529ec CS |
3958 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
3959 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 3960 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
3961 | |
3962 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
4a0ee12a | 3963 | &spa->spa_feat_for_read_obj, B_TRUE) != 0) { |
9ae529ec CS |
3964 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3965 | } | |
3966 | ||
3967 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
4a0ee12a | 3968 | &spa->spa_feat_for_write_obj, B_TRUE) != 0) { |
9ae529ec CS |
3969 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3970 | } | |
3971 | ||
3972 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
4a0ee12a | 3973 | &spa->spa_feat_desc_obj, B_TRUE) != 0) { |
9ae529ec CS |
3974 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3975 | } | |
3976 | ||
b9b24bb4 CS |
3977 | enabled_feat = fnvlist_alloc(); |
3978 | unsup_feat = fnvlist_alloc(); | |
9ae529ec | 3979 | |
fa86b5db | 3980 | if (!spa_features_check(spa, B_FALSE, |
b9b24bb4 | 3981 | unsup_feat, enabled_feat)) |
9ae529ec CS |
3982 | missing_feat_read = B_TRUE; |
3983 | ||
4a0ee12a PZ |
3984 | if (spa_writeable(spa) || |
3985 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) { | |
fa86b5db | 3986 | if (!spa_features_check(spa, B_TRUE, |
b9b24bb4 | 3987 | unsup_feat, enabled_feat)) { |
9eb7b46e | 3988 | *missing_feat_writep = B_TRUE; |
b9b24bb4 | 3989 | } |
9ae529ec CS |
3990 | } |
3991 | ||
b9b24bb4 CS |
3992 | fnvlist_add_nvlist(spa->spa_load_info, |
3993 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
3994 | ||
9ae529ec | 3995 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
3996 | fnvlist_add_nvlist(spa->spa_load_info, |
3997 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
3998 | } |
3999 | ||
b9b24bb4 CS |
4000 | fnvlist_free(enabled_feat); |
4001 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
4002 | |
4003 | if (!missing_feat_read) { | |
4004 | fnvlist_add_boolean(spa->spa_load_info, | |
4005 | ZPOOL_CONFIG_CAN_RDONLY); | |
4006 | } | |
4007 | ||
4008 | /* | |
4009 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
4010 | * twofold: to determine whether the pool is available for | |
4011 | * import in read-write mode and (if it is not) whether the | |
4012 | * pool is available for import in read-only mode. If the pool | |
4013 | * is available for import in read-write mode, it is displayed | |
4014 | * as available in userland; if it is not available for import | |
4015 | * in read-only mode, it is displayed as unavailable in | |
4016 | * userland. If the pool is available for import in read-only | |
4017 | * mode but not read-write mode, it is displayed as unavailable | |
4018 | * in userland with a special note that the pool is actually | |
4019 | * available for open in read-only mode. | |
4020 | * | |
4021 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
4022 | * missing a feature for write, we must first determine whether | |
4023 | * the pool can be opened read-only before returning to | |
4024 | * userland in order to know whether to display the | |
4025 | * abovementioned note. | |
4026 | */ | |
9eb7b46e | 4027 | if (missing_feat_read || (*missing_feat_writep && |
9ae529ec | 4028 | spa_writeable(spa))) { |
4a0ee12a | 4029 | spa_load_failed(spa, "pool uses unsupported features"); |
9ae529ec CS |
4030 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
4031 | ENOTSUP)); | |
4032 | } | |
b0bc7a84 MG |
4033 | |
4034 | /* | |
4035 | * Load refcounts for ZFS features from disk into an in-memory | |
4036 | * cache during SPA initialization. | |
4037 | */ | |
1c27024e | 4038 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { |
b0bc7a84 MG |
4039 | uint64_t refcount; |
4040 | ||
4041 | error = feature_get_refcount_from_disk(spa, | |
4042 | &spa_feature_table[i], &refcount); | |
4043 | if (error == 0) { | |
4044 | spa->spa_feat_refcount_cache[i] = refcount; | |
4045 | } else if (error == ENOTSUP) { | |
4046 | spa->spa_feat_refcount_cache[i] = | |
4047 | SPA_FEATURE_DISABLED; | |
4048 | } else { | |
4a0ee12a PZ |
4049 | spa_load_failed(spa, "error getting refcount " |
4050 | "for feature %s [error=%d]", | |
4051 | spa_feature_table[i].fi_guid, error); | |
b0bc7a84 MG |
4052 | return (spa_vdev_err(rvd, |
4053 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
4054 | } | |
4055 | } | |
4056 | } | |
4057 | ||
4058 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
4059 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
4a0ee12a | 4060 | &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) |
b0bc7a84 | 4061 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
9ae529ec CS |
4062 | } |
4063 | ||
f00ab3f2 TC |
4064 | /* |
4065 | * Encryption was added before bookmark_v2, even though bookmark_v2 | |
4066 | * is now a dependency. If this pool has encryption enabled without | |
4067 | * bookmark_v2, trigger an errata message. | |
4068 | */ | |
4069 | if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && | |
4070 | !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) { | |
4071 | spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; | |
4072 | } | |
4073 | ||
9eb7b46e PZ |
4074 | return (0); |
4075 | } | |
4076 | ||
4077 | static int | |
4078 | spa_ld_load_special_directories(spa_t *spa) | |
4079 | { | |
4080 | int error = 0; | |
4081 | vdev_t *rvd = spa->spa_root_vdev; | |
4082 | ||
9ae529ec CS |
4083 | spa->spa_is_initializing = B_TRUE; |
4084 | error = dsl_pool_open(spa->spa_dsl_pool); | |
4085 | spa->spa_is_initializing = B_FALSE; | |
4a0ee12a PZ |
4086 | if (error != 0) { |
4087 | spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); | |
9ae529ec | 4088 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4089 | } |
9ae529ec | 4090 | |
9eb7b46e PZ |
4091 | return (0); |
4092 | } | |
428870ff | 4093 | |
9eb7b46e PZ |
4094 | static int |
4095 | spa_ld_get_props(spa_t *spa) | |
4096 | { | |
4097 | int error = 0; | |
4098 | uint64_t obj; | |
4099 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4100 | |
3c67d83a TH |
4101 | /* Grab the checksum salt from the MOS. */ |
4102 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4103 | DMU_POOL_CHECKSUM_SALT, 1, | |
4104 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
4105 | spa->spa_cksum_salt.zcs_bytes); | |
4106 | if (error == ENOENT) { | |
4107 | /* Generate a new salt for subsequent use */ | |
4108 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
4109 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
4110 | } else if (error != 0) { | |
4a0ee12a PZ |
4111 | spa_load_failed(spa, "unable to retrieve checksum salt from " |
4112 | "MOS [error=%d]", error); | |
3c67d83a TH |
4113 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4114 | } | |
4115 | ||
4a0ee12a | 4116 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) |
428870ff BB |
4117 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4118 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
4a0ee12a PZ |
4119 | if (error != 0) { |
4120 | spa_load_failed(spa, "error opening deferred-frees bpobj " | |
4121 | "[error=%d]", error); | |
428870ff | 4122 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4123 | } |
34dc7c2f BB |
4124 | |
4125 | /* | |
4126 | * Load the bit that tells us to use the new accounting function | |
4127 | * (raid-z deflation). If we have an older pool, this will not | |
4128 | * be present. | |
4129 | */ | |
4a0ee12a | 4130 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); |
428870ff BB |
4131 | if (error != 0 && error != ENOENT) |
4132 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4133 | ||
4134 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
4a0ee12a | 4135 | &spa->spa_creation_version, B_FALSE); |
428870ff BB |
4136 | if (error != 0 && error != ENOENT) |
4137 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
4138 | |
4139 | /* | |
4140 | * Load the persistent error log. If we have an older pool, this will | |
4141 | * not be present. | |
4142 | */ | |
4a0ee12a PZ |
4143 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, |
4144 | B_FALSE); | |
428870ff BB |
4145 | if (error != 0 && error != ENOENT) |
4146 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4147 | |
428870ff | 4148 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
4a0ee12a | 4149 | &spa->spa_errlog_scrub, B_FALSE); |
428870ff BB |
4150 | if (error != 0 && error != ENOENT) |
4151 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4152 | |
37f03da8 SH |
4153 | /* |
4154 | * Load the livelist deletion field. If a livelist is queued for | |
4155 | * deletion, indicate that in the spa | |
4156 | */ | |
4157 | error = spa_dir_prop(spa, DMU_POOL_DELETED_CLONES, | |
4158 | &spa->spa_livelists_to_delete, B_FALSE); | |
4159 | if (error != 0 && error != ENOENT) | |
4160 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4161 | ||
34dc7c2f BB |
4162 | /* |
4163 | * Load the history object. If we have an older pool, this | |
4164 | * will not be present. | |
4165 | */ | |
4a0ee12a | 4166 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); |
428870ff BB |
4167 | if (error != 0 && error != ENOENT) |
4168 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4169 | ||
e0ab3ab5 JS |
4170 | /* |
4171 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
4172 | * be present; in this case, defer its creation to a later time to | |
4173 | * avoid dirtying the MOS this early / out of sync context. See | |
4174 | * spa_sync_config_object. | |
4175 | */ | |
4176 | ||
4177 | /* The sentinel is only available in the MOS config. */ | |
1c27024e | 4178 | nvlist_t *mos_config; |
4a0ee12a PZ |
4179 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { |
4180 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
e0ab3ab5 | 4181 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4182 | } |
e0ab3ab5 JS |
4183 | |
4184 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
4a0ee12a | 4185 | &spa->spa_all_vdev_zaps, B_FALSE); |
e0ab3ab5 | 4186 | |
38640550 DB |
4187 | if (error == ENOENT) { |
4188 | VERIFY(!nvlist_exists(mos_config, | |
4189 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
4190 | spa->spa_avz_action = AVZ_ACTION_INITIALIZE; | |
4191 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4192 | } else if (error != 0) { | |
e0ab3ab5 | 4193 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
38640550 | 4194 | } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { |
e0ab3ab5 JS |
4195 | /* |
4196 | * An older version of ZFS overwrote the sentinel value, so | |
4197 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
4198 | * destruction to later; see spa_sync_config_object. | |
4199 | */ | |
4200 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
4201 | /* | |
4202 | * We're assuming that no vdevs have had their ZAPs created | |
4203 | * before this. Better be sure of it. | |
4204 | */ | |
4205 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4206 | } | |
4207 | nvlist_free(mos_config); | |
4208 | ||
9eb7b46e PZ |
4209 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
4210 | ||
4a0ee12a PZ |
4211 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, |
4212 | B_FALSE); | |
9eb7b46e PZ |
4213 | if (error && error != ENOENT) |
4214 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4215 | ||
4216 | if (error == 0) { | |
da27b8bc | 4217 | uint64_t autoreplace = 0; |
9eb7b46e PZ |
4218 | |
4219 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
4220 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
4221 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
4222 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
4223 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
c02c1bec | 4224 | spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); |
1b939560 | 4225 | spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim); |
9eb7b46e PZ |
4226 | spa->spa_autoreplace = (autoreplace != 0); |
4227 | } | |
4228 | ||
6cb8e530 PZ |
4229 | /* |
4230 | * If we are importing a pool with missing top-level vdevs, | |
4231 | * we enforce that the pool doesn't panic or get suspended on | |
4232 | * error since the likelihood of missing data is extremely high. | |
4233 | */ | |
4234 | if (spa->spa_missing_tvds > 0 && | |
4235 | spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && | |
4236 | spa->spa_load_state != SPA_LOAD_TRYIMPORT) { | |
4237 | spa_load_note(spa, "forcing failmode to 'continue' " | |
4238 | "as some top level vdevs are missing"); | |
4239 | spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; | |
4240 | } | |
4241 | ||
9eb7b46e PZ |
4242 | return (0); |
4243 | } | |
4244 | ||
4245 | static int | |
4246 | spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) | |
4247 | { | |
4248 | int error = 0; | |
4249 | vdev_t *rvd = spa->spa_root_vdev; | |
4250 | ||
428870ff BB |
4251 | /* |
4252 | * If we're assembling the pool from the split-off vdevs of | |
4253 | * an existing pool, we don't want to attach the spares & cache | |
4254 | * devices. | |
4255 | */ | |
34dc7c2f BB |
4256 | |
4257 | /* | |
4258 | * Load any hot spares for this pool. | |
4259 | */ | |
4a0ee12a PZ |
4260 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, |
4261 | B_FALSE); | |
428870ff BB |
4262 | if (error != 0 && error != ENOENT) |
4263 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4264 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4265 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
4266 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
4a0ee12a PZ |
4267 | &spa->spa_spares.sav_config) != 0) { |
4268 | spa_load_failed(spa, "error loading spares nvlist"); | |
428870ff | 4269 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4270 | } |
34dc7c2f | 4271 | |
b128c09f | 4272 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4273 | spa_load_spares(spa); |
b128c09f | 4274 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4275 | } else if (error == 0) { |
4276 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
4277 | } |
4278 | ||
4279 | /* | |
4280 | * Load any level 2 ARC devices for this pool. | |
4281 | */ | |
428870ff | 4282 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
4a0ee12a | 4283 | &spa->spa_l2cache.sav_object, B_FALSE); |
428870ff BB |
4284 | if (error != 0 && error != ENOENT) |
4285 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4286 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4287 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
4288 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
4a0ee12a PZ |
4289 | &spa->spa_l2cache.sav_config) != 0) { |
4290 | spa_load_failed(spa, "error loading l2cache nvlist"); | |
428870ff | 4291 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4292 | } |
34dc7c2f | 4293 | |
b128c09f | 4294 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4295 | spa_load_l2cache(spa); |
b128c09f | 4296 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4297 | } else if (error == 0) { |
4298 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
4299 | } |
4300 | ||
9eb7b46e PZ |
4301 | return (0); |
4302 | } | |
428870ff | 4303 | |
9eb7b46e | 4304 | static int |
4a0ee12a | 4305 | spa_ld_load_vdev_metadata(spa_t *spa) |
9eb7b46e PZ |
4306 | { |
4307 | int error = 0; | |
4308 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4309 | |
379ca9cf OF |
4310 | /* |
4311 | * If the 'multihost' property is set, then never allow a pool to | |
4312 | * be imported when the system hostid is zero. The exception to | |
4313 | * this rule is zdb which is always allowed to access pools. | |
4314 | */ | |
25f06d67 | 4315 | if (spa_multihost(spa) && spa_get_hostid(spa) == 0 && |
379ca9cf OF |
4316 | (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { |
4317 | fnvlist_add_uint64(spa->spa_load_info, | |
4318 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
4319 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
4320 | } | |
4321 | ||
34dc7c2f BB |
4322 | /* |
4323 | * If the 'autoreplace' property is set, then post a resource notifying | |
4324 | * the ZFS DE that it should not issue any faults for unopenable | |
4325 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
4326 | * unopenable vdevs so that the normal autoreplace handler can take | |
4327 | * over. | |
4328 | */ | |
4a0ee12a | 4329 | if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 4330 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
4331 | /* |
4332 | * For the import case, this is done in spa_import(), because | |
4333 | * at this point we're using the spare definitions from | |
4334 | * the MOS config, not necessarily from the userland config. | |
4335 | */ | |
4a0ee12a | 4336 | if (spa->spa_load_state != SPA_LOAD_IMPORT) { |
428870ff BB |
4337 | spa_aux_check_removed(&spa->spa_spares); |
4338 | spa_aux_check_removed(&spa->spa_l2cache); | |
4339 | } | |
4340 | } | |
34dc7c2f BB |
4341 | |
4342 | /* | |
9eb7b46e | 4343 | * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. |
34dc7c2f | 4344 | */ |
a1d477c2 MA |
4345 | error = vdev_load(rvd); |
4346 | if (error != 0) { | |
4a0ee12a | 4347 | spa_load_failed(spa, "vdev_load failed [error=%d]", error); |
a1d477c2 MA |
4348 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
4349 | } | |
4350 | ||
93e28d66 SD |
4351 | error = spa_ld_log_spacemaps(spa); |
4352 | if (error != 0) { | |
4353 | spa_load_failed(spa, "spa_ld_log_sm_data failed [error=%d]", | |
4354 | error); | |
4355 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
4356 | } | |
4357 | ||
34dc7c2f | 4358 | /* |
9eb7b46e | 4359 | * Propagate the leaf DTLs we just loaded all the way up the vdev tree. |
34dc7c2f | 4360 | */ |
b128c09f | 4361 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9a49d3f3 | 4362 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE, B_FALSE); |
b128c09f | 4363 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 4364 | |
9eb7b46e PZ |
4365 | return (0); |
4366 | } | |
4367 | ||
4368 | static int | |
4369 | spa_ld_load_dedup_tables(spa_t *spa) | |
4370 | { | |
4371 | int error = 0; | |
4372 | vdev_t *rvd = spa->spa_root_vdev; | |
4373 | ||
428870ff | 4374 | error = ddt_load(spa); |
4a0ee12a PZ |
4375 | if (error != 0) { |
4376 | spa_load_failed(spa, "ddt_load failed [error=%d]", error); | |
428870ff | 4377 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4378 | } |
428870ff | 4379 | |
9eb7b46e PZ |
4380 | return (0); |
4381 | } | |
4382 | ||
4383 | static int | |
4384 | spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) | |
4385 | { | |
4386 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 4387 | |
4a0ee12a PZ |
4388 | if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { |
4389 | boolean_t missing = spa_check_logs(spa); | |
4390 | if (missing) { | |
6cb8e530 PZ |
4391 | if (spa->spa_missing_tvds != 0) { |
4392 | spa_load_note(spa, "spa_check_logs failed " | |
4393 | "so dropping the logs"); | |
4394 | } else { | |
4395 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
4396 | spa_load_failed(spa, "spa_check_logs failed"); | |
4397 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, | |
4398 | ENXIO)); | |
4399 | } | |
4a0ee12a | 4400 | } |
428870ff BB |
4401 | } |
4402 | ||
9eb7b46e PZ |
4403 | return (0); |
4404 | } | |
4405 | ||
4406 | static int | |
4a0ee12a | 4407 | spa_ld_verify_pool_data(spa_t *spa) |
9eb7b46e PZ |
4408 | { |
4409 | int error = 0; | |
4410 | vdev_t *rvd = spa->spa_root_vdev; | |
4411 | ||
4412 | /* | |
4413 | * We've successfully opened the pool, verify that we're ready | |
4414 | * to start pushing transactions. | |
4415 | */ | |
4a0ee12a | 4416 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
9eb7b46e PZ |
4417 | error = spa_load_verify(spa); |
4418 | if (error != 0) { | |
4a0ee12a PZ |
4419 | spa_load_failed(spa, "spa_load_verify failed " |
4420 | "[error=%d]", error); | |
9eb7b46e PZ |
4421 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
4422 | error)); | |
4423 | } | |
4424 | } | |
4425 | ||
4426 | return (0); | |
4427 | } | |
4428 | ||
4429 | static void | |
4430 | spa_ld_claim_log_blocks(spa_t *spa) | |
4431 | { | |
4432 | dmu_tx_t *tx; | |
4433 | dsl_pool_t *dp = spa_get_dsl(spa); | |
4434 | ||
4435 | /* | |
4436 | * Claim log blocks that haven't been committed yet. | |
4437 | * This must all happen in a single txg. | |
4438 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
4439 | * invoked from zil_claim_log_block()'s i/o done callback. | |
4440 | * Price of rollback is that we abandon the log. | |
4441 | */ | |
4442 | spa->spa_claiming = B_TRUE; | |
4443 | ||
4444 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
4445 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
4446 | zil_claim, tx, DS_FIND_CHILDREN); | |
4447 | dmu_tx_commit(tx); | |
4448 | ||
4449 | spa->spa_claiming = B_FALSE; | |
4450 | ||
4451 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
4452 | } | |
4453 | ||
4454 | static void | |
6cb8e530 | 4455 | spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, |
d2734cce | 4456 | boolean_t update_config_cache) |
9eb7b46e PZ |
4457 | { |
4458 | vdev_t *rvd = spa->spa_root_vdev; | |
4459 | int need_update = B_FALSE; | |
4460 | ||
4461 | /* | |
4462 | * If the config cache is stale, or we have uninitialized | |
4463 | * metaslabs (see spa_vdev_add()), then update the config. | |
4464 | * | |
4465 | * If this is a verbatim import, trust the current | |
4466 | * in-core spa_config and update the disk labels. | |
4467 | */ | |
d2734cce | 4468 | if (update_config_cache || config_cache_txg != spa->spa_config_txg || |
4a0ee12a PZ |
4469 | spa->spa_load_state == SPA_LOAD_IMPORT || |
4470 | spa->spa_load_state == SPA_LOAD_RECOVER || | |
9eb7b46e PZ |
4471 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) |
4472 | need_update = B_TRUE; | |
4473 | ||
4474 | for (int c = 0; c < rvd->vdev_children; c++) | |
4475 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
4476 | need_update = B_TRUE; | |
4477 | ||
4478 | /* | |
e1cfd73f | 4479 | * Update the config cache asynchronously in case we're the |
9eb7b46e PZ |
4480 | * root pool, in which case the config cache isn't writable yet. |
4481 | */ | |
4482 | if (need_update) | |
4483 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
4484 | } | |
4485 | ||
6cb8e530 PZ |
4486 | static void |
4487 | spa_ld_prepare_for_reload(spa_t *spa) | |
4488 | { | |
da92d5cb | 4489 | spa_mode_t mode = spa->spa_mode; |
6cb8e530 PZ |
4490 | int async_suspended = spa->spa_async_suspended; |
4491 | ||
4492 | spa_unload(spa); | |
4493 | spa_deactivate(spa); | |
4494 | spa_activate(spa, mode); | |
4495 | ||
4496 | /* | |
4497 | * We save the value of spa_async_suspended as it gets reset to 0 by | |
4498 | * spa_unload(). We want to restore it back to the original value before | |
4499 | * returning as we might be calling spa_async_resume() later. | |
4500 | */ | |
4501 | spa->spa_async_suspended = async_suspended; | |
4502 | } | |
4503 | ||
9eb7b46e | 4504 | static int |
d2734cce SD |
4505 | spa_ld_read_checkpoint_txg(spa_t *spa) |
4506 | { | |
4507 | uberblock_t checkpoint; | |
4508 | int error = 0; | |
4509 | ||
4510 | ASSERT0(spa->spa_checkpoint_txg); | |
4511 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4512 | ||
4513 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4514 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4515 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4516 | ||
4517 | if (error == ENOENT) | |
4518 | return (0); | |
4519 | ||
4520 | if (error != 0) | |
4521 | return (error); | |
4522 | ||
4523 | ASSERT3U(checkpoint.ub_txg, !=, 0); | |
4524 | ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); | |
4525 | ASSERT3U(checkpoint.ub_timestamp, !=, 0); | |
4526 | spa->spa_checkpoint_txg = checkpoint.ub_txg; | |
4527 | spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; | |
4528 | ||
4529 | return (0); | |
4530 | } | |
4531 | ||
4532 | static int | |
4533 | spa_ld_mos_init(spa_t *spa, spa_import_type_t type) | |
9eb7b46e PZ |
4534 | { |
4535 | int error = 0; | |
9eb7b46e | 4536 | |
4a0ee12a | 4537 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6cb8e530 | 4538 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); |
4a0ee12a | 4539 | |
9eb7b46e | 4540 | /* |
6cb8e530 PZ |
4541 | * Never trust the config that is provided unless we are assembling |
4542 | * a pool following a split. | |
4543 | * This means don't trust blkptrs and the vdev tree in general. This | |
4544 | * also effectively puts the spa in read-only mode since | |
4545 | * spa_writeable() checks for spa_trust_config to be true. | |
4546 | * We will later load a trusted config from the MOS. | |
9eb7b46e | 4547 | */ |
6cb8e530 PZ |
4548 | if (type != SPA_IMPORT_ASSEMBLE) |
4549 | spa->spa_trust_config = B_FALSE; | |
4550 | ||
9eb7b46e PZ |
4551 | /* |
4552 | * Parse the config provided to create a vdev tree. | |
4553 | */ | |
6cb8e530 | 4554 | error = spa_ld_parse_config(spa, type); |
9eb7b46e PZ |
4555 | if (error != 0) |
4556 | return (error); | |
4557 | ||
ca95f70d OF |
4558 | spa_import_progress_add(spa); |
4559 | ||
9eb7b46e PZ |
4560 | /* |
4561 | * Now that we have the vdev tree, try to open each vdev. This involves | |
4562 | * opening the underlying physical device, retrieving its geometry and | |
4563 | * probing the vdev with a dummy I/O. The state of each vdev will be set | |
4564 | * based on the success of those operations. After this we'll be ready | |
4565 | * to read from the vdevs. | |
4566 | */ | |
4567 | error = spa_ld_open_vdevs(spa); | |
4568 | if (error != 0) | |
4569 | return (error); | |
4570 | ||
4571 | /* | |
4572 | * Read the label of each vdev and make sure that the GUIDs stored | |
4573 | * there match the GUIDs in the config provided. | |
6cb8e530 PZ |
4574 | * If we're assembling a new pool that's been split off from an |
4575 | * existing pool, the labels haven't yet been updated so we skip | |
4576 | * validation for now. | |
9eb7b46e | 4577 | */ |
6cb8e530 PZ |
4578 | if (type != SPA_IMPORT_ASSEMBLE) { |
4579 | error = spa_ld_validate_vdevs(spa); | |
4580 | if (error != 0) | |
4581 | return (error); | |
4582 | } | |
9eb7b46e PZ |
4583 | |
4584 | /* | |
d2734cce SD |
4585 | * Read all vdev labels to find the best uberblock (i.e. latest, |
4586 | * unless spa_load_max_txg is set) and store it in spa_uberblock. We | |
4587 | * get the list of features required to read blkptrs in the MOS from | |
4588 | * the vdev label with the best uberblock and verify that our version | |
4589 | * of zfs supports them all. | |
9eb7b46e | 4590 | */ |
6cb8e530 | 4591 | error = spa_ld_select_uberblock(spa, type); |
9eb7b46e PZ |
4592 | if (error != 0) |
4593 | return (error); | |
4594 | ||
4595 | /* | |
4596 | * Pass that uberblock to the dsl_pool layer which will open the root | |
4597 | * blkptr. This blkptr points to the latest version of the MOS and will | |
4598 | * allow us to read its contents. | |
4599 | */ | |
4600 | error = spa_ld_open_rootbp(spa); | |
4601 | if (error != 0) | |
4602 | return (error); | |
4603 | ||
d2734cce SD |
4604 | return (0); |
4605 | } | |
4606 | ||
4607 | static int | |
4608 | spa_ld_checkpoint_rewind(spa_t *spa) | |
4609 | { | |
4610 | uberblock_t checkpoint; | |
4611 | int error = 0; | |
4612 | ||
4613 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4614 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4615 | ||
4616 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4617 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4618 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4619 | ||
4620 | if (error != 0) { | |
4621 | spa_load_failed(spa, "unable to retrieve checkpointed " | |
4622 | "uberblock from the MOS config [error=%d]", error); | |
4623 | ||
4624 | if (error == ENOENT) | |
4625 | error = ZFS_ERR_NO_CHECKPOINT; | |
4626 | ||
4627 | return (error); | |
4628 | } | |
4629 | ||
4630 | ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); | |
4631 | ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); | |
4632 | ||
4633 | /* | |
4634 | * We need to update the txg and timestamp of the checkpointed | |
4635 | * uberblock to be higher than the latest one. This ensures that | |
4636 | * the checkpointed uberblock is selected if we were to close and | |
4637 | * reopen the pool right after we've written it in the vdev labels. | |
4638 | * (also see block comment in vdev_uberblock_compare) | |
4639 | */ | |
4640 | checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; | |
4641 | checkpoint.ub_timestamp = gethrestime_sec(); | |
4642 | ||
4643 | /* | |
4644 | * Set current uberblock to be the checkpointed uberblock. | |
4645 | */ | |
4646 | spa->spa_uberblock = checkpoint; | |
4647 | ||
4648 | /* | |
4649 | * If we are doing a normal rewind, then the pool is open for | |
4650 | * writing and we sync the "updated" checkpointed uberblock to | |
4651 | * disk. Once this is done, we've basically rewound the whole | |
4652 | * pool and there is no way back. | |
4653 | * | |
4654 | * There are cases when we don't want to attempt and sync the | |
4655 | * checkpointed uberblock to disk because we are opening a | |
4656 | * pool as read-only. Specifically, verifying the checkpointed | |
4657 | * state with zdb, and importing the checkpointed state to get | |
4658 | * a "preview" of its content. | |
4659 | */ | |
4660 | if (spa_writeable(spa)) { | |
4661 | vdev_t *rvd = spa->spa_root_vdev; | |
4662 | ||
4663 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4664 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; | |
4665 | int svdcount = 0; | |
4666 | int children = rvd->vdev_children; | |
29274c9f | 4667 | int c0 = random_in_range(children); |
d2734cce SD |
4668 | |
4669 | for (int c = 0; c < children; c++) { | |
4670 | vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; | |
4671 | ||
4672 | /* Stop when revisiting the first vdev */ | |
4673 | if (c > 0 && svd[0] == vd) | |
4674 | break; | |
4675 | ||
4676 | if (vd->vdev_ms_array == 0 || vd->vdev_islog || | |
4677 | !vdev_is_concrete(vd)) | |
4678 | continue; | |
4679 | ||
4680 | svd[svdcount++] = vd; | |
4681 | if (svdcount == SPA_SYNC_MIN_VDEVS) | |
4682 | break; | |
4683 | } | |
4684 | error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); | |
4685 | if (error == 0) | |
4686 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
4687 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4688 | ||
4689 | if (error != 0) { | |
4690 | spa_load_failed(spa, "failed to write checkpointed " | |
4691 | "uberblock to the vdev labels [error=%d]", error); | |
4692 | return (error); | |
4693 | } | |
4694 | } | |
4695 | ||
4696 | return (0); | |
4697 | } | |
4698 | ||
4699 | static int | |
4700 | spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, | |
4701 | boolean_t *update_config_cache) | |
4702 | { | |
4703 | int error; | |
4704 | ||
4705 | /* | |
4706 | * Parse the config for pool, open and validate vdevs, | |
4707 | * select an uberblock, and use that uberblock to open | |
4708 | * the MOS. | |
4709 | */ | |
4710 | error = spa_ld_mos_init(spa, type); | |
4711 | if (error != 0) | |
4712 | return (error); | |
4713 | ||
9eb7b46e | 4714 | /* |
6cb8e530 PZ |
4715 | * Retrieve the trusted config stored in the MOS and use it to create |
4716 | * a new, exact version of the vdev tree, then reopen all vdevs. | |
9eb7b46e | 4717 | */ |
d2734cce | 4718 | error = spa_ld_trusted_config(spa, type, B_FALSE); |
6cb8e530 | 4719 | if (error == EAGAIN) { |
d2734cce SD |
4720 | if (update_config_cache != NULL) |
4721 | *update_config_cache = B_TRUE; | |
4722 | ||
6cb8e530 PZ |
4723 | /* |
4724 | * Redo the loading process with the trusted config if it is | |
4725 | * too different from the untrusted config. | |
4726 | */ | |
4727 | spa_ld_prepare_for_reload(spa); | |
d2734cce SD |
4728 | spa_load_note(spa, "RELOADING"); |
4729 | error = spa_ld_mos_init(spa, type); | |
4730 | if (error != 0) | |
4731 | return (error); | |
4732 | ||
4733 | error = spa_ld_trusted_config(spa, type, B_TRUE); | |
4734 | if (error != 0) | |
4735 | return (error); | |
4736 | ||
6cb8e530 | 4737 | } else if (error != 0) { |
9eb7b46e | 4738 | return (error); |
6cb8e530 | 4739 | } |
9eb7b46e | 4740 | |
d2734cce SD |
4741 | return (0); |
4742 | } | |
4743 | ||
4744 | /* | |
4745 | * Load an existing storage pool, using the config provided. This config | |
4746 | * describes which vdevs are part of the pool and is later validated against | |
4747 | * partial configs present in each vdev's label and an entire copy of the | |
4748 | * config stored in the MOS. | |
4749 | */ | |
4750 | static int | |
4751 | spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport) | |
4752 | { | |
4753 | int error = 0; | |
4754 | boolean_t missing_feat_write = B_FALSE; | |
4755 | boolean_t checkpoint_rewind = | |
4756 | (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4757 | boolean_t update_config_cache = B_FALSE; | |
4758 | ||
4759 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4760 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); | |
4761 | ||
4762 | spa_load_note(spa, "LOADING"); | |
4763 | ||
4764 | error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); | |
4765 | if (error != 0) | |
4766 | return (error); | |
4767 | ||
4768 | /* | |
4769 | * If we are rewinding to the checkpoint then we need to repeat | |
4770 | * everything we've done so far in this function but this time | |
4771 | * selecting the checkpointed uberblock and using that to open | |
4772 | * the MOS. | |
4773 | */ | |
4774 | if (checkpoint_rewind) { | |
4775 | /* | |
4776 | * If we are rewinding to the checkpoint update config cache | |
4777 | * anyway. | |
4778 | */ | |
4779 | update_config_cache = B_TRUE; | |
4780 | ||
4781 | /* | |
4782 | * Extract the checkpointed uberblock from the current MOS | |
4783 | * and use this as the pool's uberblock from now on. If the | |
4784 | * pool is imported as writeable we also write the checkpoint | |
4785 | * uberblock to the labels, making the rewind permanent. | |
4786 | */ | |
4787 | error = spa_ld_checkpoint_rewind(spa); | |
4788 | if (error != 0) | |
4789 | return (error); | |
4790 | ||
4791 | /* | |
e1cfd73f | 4792 | * Redo the loading process again with the |
d2734cce SD |
4793 | * checkpointed uberblock. |
4794 | */ | |
4795 | spa_ld_prepare_for_reload(spa); | |
4796 | spa_load_note(spa, "LOADING checkpointed uberblock"); | |
4797 | error = spa_ld_mos_with_trusted_config(spa, type, NULL); | |
4798 | if (error != 0) | |
4799 | return (error); | |
4800 | } | |
4801 | ||
4802 | /* | |
4803 | * Retrieve the checkpoint txg if the pool has a checkpoint. | |
4804 | */ | |
4805 | error = spa_ld_read_checkpoint_txg(spa); | |
4806 | if (error != 0) | |
4807 | return (error); | |
4808 | ||
9eb7b46e PZ |
4809 | /* |
4810 | * Retrieve the mapping of indirect vdevs. Those vdevs were removed | |
4811 | * from the pool and their contents were re-mapped to other vdevs. Note | |
4812 | * that everything that we read before this step must have been | |
4813 | * rewritten on concrete vdevs after the last device removal was | |
4814 | * initiated. Otherwise we could be reading from indirect vdevs before | |
4815 | * we have loaded their mappings. | |
4816 | */ | |
4817 | error = spa_ld_open_indirect_vdev_metadata(spa); | |
4818 | if (error != 0) | |
4819 | return (error); | |
4820 | ||
4821 | /* | |
4822 | * Retrieve the full list of active features from the MOS and check if | |
4823 | * they are all supported. | |
4824 | */ | |
4a0ee12a | 4825 | error = spa_ld_check_features(spa, &missing_feat_write); |
9eb7b46e PZ |
4826 | if (error != 0) |
4827 | return (error); | |
4828 | ||
4829 | /* | |
4830 | * Load several special directories from the MOS needed by the dsl_pool | |
4831 | * layer. | |
4832 | */ | |
4833 | error = spa_ld_load_special_directories(spa); | |
4834 | if (error != 0) | |
4835 | return (error); | |
4836 | ||
9eb7b46e PZ |
4837 | /* |
4838 | * Retrieve pool properties from the MOS. | |
4839 | */ | |
4840 | error = spa_ld_get_props(spa); | |
4841 | if (error != 0) | |
4842 | return (error); | |
4843 | ||
4844 | /* | |
4845 | * Retrieve the list of auxiliary devices - cache devices and spares - | |
4846 | * and open them. | |
4847 | */ | |
4848 | error = spa_ld_open_aux_vdevs(spa, type); | |
4849 | if (error != 0) | |
4850 | return (error); | |
4851 | ||
4852 | /* | |
4853 | * Load the metadata for all vdevs. Also check if unopenable devices | |
4854 | * should be autoreplaced. | |
4855 | */ | |
4a0ee12a | 4856 | error = spa_ld_load_vdev_metadata(spa); |
9eb7b46e PZ |
4857 | if (error != 0) |
4858 | return (error); | |
4859 | ||
4860 | error = spa_ld_load_dedup_tables(spa); | |
4861 | if (error != 0) | |
4862 | return (error); | |
4863 | ||
4864 | /* | |
4865 | * Verify the logs now to make sure we don't have any unexpected errors | |
4866 | * when we claim log blocks later. | |
4867 | */ | |
4868 | error = spa_ld_verify_logs(spa, type, ereport); | |
4869 | if (error != 0) | |
4870 | return (error); | |
4871 | ||
9ae529ec | 4872 | if (missing_feat_write) { |
6cb8e530 | 4873 | ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); |
9ae529ec CS |
4874 | |
4875 | /* | |
4876 | * At this point, we know that we can open the pool in | |
4877 | * read-only mode but not read-write mode. We now have enough | |
4878 | * information and can return to userland. | |
4879 | */ | |
9eb7b46e PZ |
4880 | return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, |
4881 | ENOTSUP)); | |
9ae529ec CS |
4882 | } |
4883 | ||
572e2857 | 4884 | /* |
9eb7b46e PZ |
4885 | * Traverse the last txgs to make sure the pool was left off in a safe |
4886 | * state. When performing an extreme rewind, we verify the whole pool, | |
4887 | * which can take a very long time. | |
572e2857 | 4888 | */ |
4a0ee12a | 4889 | error = spa_ld_verify_pool_data(spa); |
9eb7b46e PZ |
4890 | if (error != 0) |
4891 | return (error); | |
572e2857 | 4892 | |
9eb7b46e PZ |
4893 | /* |
4894 | * Calculate the deflated space for the pool. This must be done before | |
4895 | * we write anything to the pool because we'd need to update the space | |
4896 | * accounting using the deflated sizes. | |
4897 | */ | |
4898 | spa_update_dspace(spa); | |
4899 | ||
4900 | /* | |
4901 | * We have now retrieved all the information we needed to open the | |
4902 | * pool. If we are importing the pool in read-write mode, a few | |
4903 | * additional steps must be performed to finish the import. | |
4904 | */ | |
6cb8e530 | 4905 | if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || |
428870ff | 4906 | spa->spa_load_max_txg == UINT64_MAX)) { |
6cb8e530 PZ |
4907 | uint64_t config_cache_txg = spa->spa_config_txg; |
4908 | ||
4909 | ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f | 4910 | |
d2734cce SD |
4911 | /* |
4912 | * In case of a checkpoint rewind, log the original txg | |
4913 | * of the checkpointed uberblock. | |
4914 | */ | |
4915 | if (checkpoint_rewind) { | |
4916 | spa_history_log_internal(spa, "checkpoint rewind", | |
4917 | NULL, "rewound state to txg=%llu", | |
4918 | (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); | |
4919 | } | |
4920 | ||
34dc7c2f | 4921 | /* |
9eb7b46e | 4922 | * Traverse the ZIL and claim all blocks. |
34dc7c2f | 4923 | */ |
9eb7b46e | 4924 | spa_ld_claim_log_blocks(spa); |
428870ff | 4925 | |
9eb7b46e PZ |
4926 | /* |
4927 | * Kick-off the syncing thread. | |
4928 | */ | |
34dc7c2f BB |
4929 | spa->spa_sync_on = B_TRUE; |
4930 | txg_sync_start(spa->spa_dsl_pool); | |
379ca9cf | 4931 | mmp_thread_start(spa); |
34dc7c2f BB |
4932 | |
4933 | /* | |
428870ff BB |
4934 | * Wait for all claims to sync. We sync up to the highest |
4935 | * claimed log block birth time so that claimed log blocks | |
4936 | * don't appear to be from the future. spa_claim_max_txg | |
9eb7b46e PZ |
4937 | * will have been set for us by ZIL traversal operations |
4938 | * performed above. | |
34dc7c2f | 4939 | */ |
428870ff | 4940 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
4941 | |
4942 | /* | |
9eb7b46e PZ |
4943 | * Check if we need to request an update of the config. On the |
4944 | * next sync, we would update the config stored in vdev labels | |
4945 | * and the cachefile (by default /etc/zfs/zpool.cache). | |
34dc7c2f | 4946 | */ |
6cb8e530 | 4947 | spa_ld_check_for_config_update(spa, config_cache_txg, |
d2734cce | 4948 | update_config_cache); |
fb5f0bc8 BB |
4949 | |
4950 | /* | |
9a49d3f3 BB |
4951 | * Check if a rebuild was in progress and if so resume it. |
4952 | * Then check all DTLs to see if anything needs resilvering. | |
4953 | * The resilver will be deferred if a rebuild was started. | |
fb5f0bc8 | 4954 | */ |
9a49d3f3 BB |
4955 | if (vdev_rebuild_active(spa->spa_root_vdev)) { |
4956 | vdev_rebuild_restart(spa); | |
4957 | } else if (!dsl_scan_resilvering(spa->spa_dsl_pool) && | |
4958 | vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { | |
fb5f0bc8 | 4959 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
9a49d3f3 | 4960 | } |
428870ff | 4961 | |
6f1ffb06 MA |
4962 | /* |
4963 | * Log the fact that we booted up (so that we can detect if | |
4964 | * we rebooted in the middle of an operation). | |
4965 | */ | |
d5e024cb | 4966 | spa_history_log_version(spa, "open", NULL); |
6f1ffb06 | 4967 | |
9b2266e3 SD |
4968 | spa_restart_removal(spa); |
4969 | spa_spawn_aux_threads(spa); | |
4970 | ||
428870ff BB |
4971 | /* |
4972 | * Delete any inconsistent datasets. | |
9b2266e3 SD |
4973 | * |
4974 | * Note: | |
4975 | * Since we may be issuing deletes for clones here, | |
4976 | * we make sure to do so after we've spawned all the | |
4977 | * auxiliary threads above (from which the livelist | |
4978 | * deletion zthr is part of). | |
428870ff BB |
4979 | */ |
4980 | (void) dmu_objset_find(spa_name(spa), | |
4981 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
4982 | ||
4983 | /* | |
4984 | * Clean up any stale temporary dataset userrefs. | |
4985 | */ | |
4986 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
a1d477c2 | 4987 | |
619f0976 GW |
4988 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
4989 | vdev_initialize_restart(spa->spa_root_vdev); | |
1b939560 BB |
4990 | vdev_trim_restart(spa->spa_root_vdev); |
4991 | vdev_autotrim_restart(spa); | |
619f0976 | 4992 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
4993 | } |
4994 | ||
ca95f70d | 4995 | spa_import_progress_remove(spa_guid(spa)); |
77f6826b GA |
4996 | spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD); |
4997 | ||
4a0ee12a PZ |
4998 | spa_load_note(spa, "LOADED"); |
4999 | ||
428870ff BB |
5000 | return (0); |
5001 | } | |
34dc7c2f | 5002 | |
428870ff | 5003 | static int |
6cb8e530 | 5004 | spa_load_retry(spa_t *spa, spa_load_state_t state) |
428870ff | 5005 | { |
da92d5cb | 5006 | spa_mode_t mode = spa->spa_mode; |
572e2857 | 5007 | |
428870ff BB |
5008 | spa_unload(spa); |
5009 | spa_deactivate(spa); | |
5010 | ||
dea377c0 | 5011 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; |
428870ff | 5012 | |
572e2857 | 5013 | spa_activate(spa, mode); |
428870ff BB |
5014 | spa_async_suspend(spa); |
5015 | ||
4a0ee12a PZ |
5016 | spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", |
5017 | (u_longlong_t)spa->spa_load_max_txg); | |
5018 | ||
6cb8e530 | 5019 | return (spa_load(spa, state, SPA_IMPORT_EXISTING)); |
428870ff BB |
5020 | } |
5021 | ||
9ae529ec CS |
5022 | /* |
5023 | * If spa_load() fails this function will try loading prior txg's. If | |
5024 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
5025 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
5026 | * function will not rewind the pool and will return the same error as | |
5027 | * spa_load(). | |
5028 | */ | |
428870ff | 5029 | static int |
6cb8e530 PZ |
5030 | spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, |
5031 | int rewind_flags) | |
428870ff | 5032 | { |
9ae529ec | 5033 | nvlist_t *loadinfo = NULL; |
428870ff BB |
5034 | nvlist_t *config = NULL; |
5035 | int load_error, rewind_error; | |
5036 | uint64_t safe_rewind_txg; | |
5037 | uint64_t min_txg; | |
5038 | ||
5039 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
5040 | spa->spa_load_max_txg = spa->spa_load_txg; | |
5041 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
5042 | } else { | |
5043 | spa->spa_load_max_txg = max_request; | |
dea377c0 MA |
5044 | if (max_request != UINT64_MAX) |
5045 | spa->spa_extreme_rewind = B_TRUE; | |
428870ff BB |
5046 | } |
5047 | ||
6cb8e530 | 5048 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); |
428870ff BB |
5049 | if (load_error == 0) |
5050 | return (0); | |
d2734cce SD |
5051 | if (load_error == ZFS_ERR_NO_CHECKPOINT) { |
5052 | /* | |
5053 | * When attempting checkpoint-rewind on a pool with no | |
5054 | * checkpoint, we should not attempt to load uberblocks | |
5055 | * from previous txgs when spa_load fails. | |
5056 | */ | |
5057 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
ca95f70d | 5058 | spa_import_progress_remove(spa_guid(spa)); |
d2734cce SD |
5059 | return (load_error); |
5060 | } | |
428870ff BB |
5061 | |
5062 | if (spa->spa_root_vdev != NULL) | |
5063 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
5064 | ||
5065 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
5066 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
5067 | ||
5068 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
5069 | nvlist_free(config); | |
ca95f70d | 5070 | spa_import_progress_remove(spa_guid(spa)); |
428870ff BB |
5071 | return (load_error); |
5072 | } | |
5073 | ||
9ae529ec CS |
5074 | if (state == SPA_LOAD_RECOVER) { |
5075 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 5076 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
5077 | } else { |
5078 | /* | |
5079 | * If we aren't rolling back save the load info from our first | |
5080 | * import attempt so that we can restore it after attempting | |
5081 | * to rewind. | |
5082 | */ | |
5083 | loadinfo = spa->spa_load_info; | |
5084 | spa->spa_load_info = fnvlist_alloc(); | |
5085 | } | |
428870ff BB |
5086 | |
5087 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
5088 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
5089 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
5090 | TXG_INITIAL : safe_rewind_txg; | |
5091 | ||
5092 | /* | |
5093 | * Continue as long as we're finding errors, we're still within | |
5094 | * the acceptable rewind range, and we're still finding uberblocks | |
5095 | */ | |
5096 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
5097 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
5098 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
5099 | spa->spa_extreme_rewind = B_TRUE; | |
6cb8e530 | 5100 | rewind_error = spa_load_retry(spa, state); |
428870ff BB |
5101 | } |
5102 | ||
428870ff BB |
5103 | spa->spa_extreme_rewind = B_FALSE; |
5104 | spa->spa_load_max_txg = UINT64_MAX; | |
5105 | ||
5106 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
5107 | spa_config_set(spa, config); | |
ee6370a7 | 5108 | else |
5109 | nvlist_free(config); | |
428870ff | 5110 | |
9ae529ec CS |
5111 | if (state == SPA_LOAD_RECOVER) { |
5112 | ASSERT3P(loadinfo, ==, NULL); | |
ca95f70d | 5113 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
5114 | return (rewind_error); |
5115 | } else { | |
5116 | /* Store the rewind info as part of the initial load info */ | |
5117 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
5118 | spa->spa_load_info); | |
5119 | ||
5120 | /* Restore the initial load info */ | |
5121 | fnvlist_free(spa->spa_load_info); | |
5122 | spa->spa_load_info = loadinfo; | |
5123 | ||
ca95f70d | 5124 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
5125 | return (load_error); |
5126 | } | |
34dc7c2f BB |
5127 | } |
5128 | ||
5129 | /* | |
5130 | * Pool Open/Import | |
5131 | * | |
5132 | * The import case is identical to an open except that the configuration is sent | |
5133 | * down from userland, instead of grabbed from the configuration cache. For the | |
5134 | * case of an open, the pool configuration will exist in the | |
5135 | * POOL_STATE_UNINITIALIZED state. | |
5136 | * | |
5137 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
5138 | * the same time open the pool, without having to keep around the spa_t in some | |
5139 | * ambiguous state. | |
5140 | */ | |
5141 | static int | |
428870ff BB |
5142 | spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, |
5143 | nvlist_t **config) | |
34dc7c2f BB |
5144 | { |
5145 | spa_t *spa; | |
572e2857 | 5146 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 5147 | int error; |
34dc7c2f | 5148 | int locked = B_FALSE; |
526af785 | 5149 | int firstopen = B_FALSE; |
34dc7c2f BB |
5150 | |
5151 | *spapp = NULL; | |
5152 | ||
5153 | /* | |
5154 | * As disgusting as this is, we need to support recursive calls to this | |
5155 | * function because dsl_dir_open() is called during spa_load(), and ends | |
5156 | * up calling spa_open() again. The real fix is to figure out how to | |
5157 | * avoid dsl_dir_open() calling this in the first place. | |
5158 | */ | |
c25b8f99 | 5159 | if (MUTEX_NOT_HELD(&spa_namespace_lock)) { |
34dc7c2f BB |
5160 | mutex_enter(&spa_namespace_lock); |
5161 | locked = B_TRUE; | |
5162 | } | |
5163 | ||
5164 | if ((spa = spa_lookup(pool)) == NULL) { | |
5165 | if (locked) | |
5166 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5167 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 5168 | } |
428870ff | 5169 | |
34dc7c2f | 5170 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
8a393be3 | 5171 | zpool_load_policy_t policy; |
428870ff | 5172 | |
526af785 PJD |
5173 | firstopen = B_TRUE; |
5174 | ||
8a393be3 | 5175 | zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, |
428870ff | 5176 | &policy); |
8a393be3 | 5177 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) |
428870ff | 5178 | state = SPA_LOAD_RECOVER; |
34dc7c2f | 5179 | |
fb5f0bc8 | 5180 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5181 | |
428870ff BB |
5182 | if (state != SPA_LOAD_RECOVER) |
5183 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6cb8e530 | 5184 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; |
428870ff | 5185 | |
4a0ee12a | 5186 | zfs_dbgmsg("spa_open_common: opening %s", pool); |
8a393be3 PZ |
5187 | error = spa_load_best(spa, state, policy.zlp_txg, |
5188 | policy.zlp_rewind); | |
34dc7c2f BB |
5189 | |
5190 | if (error == EBADF) { | |
5191 | /* | |
5192 | * If vdev_validate() returns failure (indicated by | |
5193 | * EBADF), it indicates that one of the vdevs indicates | |
5194 | * that the pool has been exported or destroyed. If | |
5195 | * this is the case, the config cache is out of sync and | |
5196 | * we should remove the pool from the namespace. | |
5197 | */ | |
34dc7c2f BB |
5198 | spa_unload(spa); |
5199 | spa_deactivate(spa); | |
a1d477c2 | 5200 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 5201 | spa_remove(spa); |
34dc7c2f BB |
5202 | if (locked) |
5203 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5204 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
5205 | } |
5206 | ||
5207 | if (error) { | |
5208 | /* | |
5209 | * We can't open the pool, but we still have useful | |
5210 | * information: the state of each vdev after the | |
5211 | * attempted vdev_open(). Return this to the user. | |
5212 | */ | |
572e2857 | 5213 | if (config != NULL && spa->spa_config) { |
65ad5d11 AJ |
5214 | *config = fnvlist_dup(spa->spa_config); |
5215 | fnvlist_add_nvlist(*config, | |
572e2857 | 5216 | ZPOOL_CONFIG_LOAD_INFO, |
65ad5d11 | 5217 | spa->spa_load_info); |
572e2857 | 5218 | } |
34dc7c2f BB |
5219 | spa_unload(spa); |
5220 | spa_deactivate(spa); | |
428870ff | 5221 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
5222 | if (locked) |
5223 | mutex_exit(&spa_namespace_lock); | |
5224 | *spapp = NULL; | |
5225 | return (error); | |
34dc7c2f | 5226 | } |
34dc7c2f BB |
5227 | } |
5228 | ||
5229 | spa_open_ref(spa, tag); | |
5230 | ||
b128c09f | 5231 | if (config != NULL) |
34dc7c2f | 5232 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 5233 | |
572e2857 BB |
5234 | /* |
5235 | * If we've recovered the pool, pass back any information we | |
5236 | * gathered while doing the load. | |
5237 | */ | |
5238 | if (state == SPA_LOAD_RECOVER) { | |
65ad5d11 AJ |
5239 | fnvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, |
5240 | spa->spa_load_info); | |
572e2857 BB |
5241 | } |
5242 | ||
428870ff BB |
5243 | if (locked) { |
5244 | spa->spa_last_open_failed = 0; | |
5245 | spa->spa_last_ubsync_txg = 0; | |
5246 | spa->spa_load_txg = 0; | |
5247 | mutex_exit(&spa_namespace_lock); | |
5248 | } | |
5249 | ||
526af785 | 5250 | if (firstopen) |
ec213971 | 5251 | zvol_create_minors_recursive(spa_name(spa)); |
526af785 | 5252 | |
428870ff BB |
5253 | *spapp = spa; |
5254 | ||
34dc7c2f BB |
5255 | return (0); |
5256 | } | |
5257 | ||
428870ff BB |
5258 | int |
5259 | spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, | |
5260 | nvlist_t **config) | |
5261 | { | |
5262 | return (spa_open_common(name, spapp, tag, policy, config)); | |
5263 | } | |
5264 | ||
34dc7c2f BB |
5265 | int |
5266 | spa_open(const char *name, spa_t **spapp, void *tag) | |
5267 | { | |
428870ff | 5268 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
5269 | } |
5270 | ||
5271 | /* | |
5272 | * Lookup the given spa_t, incrementing the inject count in the process, | |
5273 | * preventing it from being exported or destroyed. | |
5274 | */ | |
5275 | spa_t * | |
5276 | spa_inject_addref(char *name) | |
5277 | { | |
5278 | spa_t *spa; | |
5279 | ||
5280 | mutex_enter(&spa_namespace_lock); | |
5281 | if ((spa = spa_lookup(name)) == NULL) { | |
5282 | mutex_exit(&spa_namespace_lock); | |
5283 | return (NULL); | |
5284 | } | |
5285 | spa->spa_inject_ref++; | |
5286 | mutex_exit(&spa_namespace_lock); | |
5287 | ||
5288 | return (spa); | |
5289 | } | |
5290 | ||
5291 | void | |
5292 | spa_inject_delref(spa_t *spa) | |
5293 | { | |
5294 | mutex_enter(&spa_namespace_lock); | |
5295 | spa->spa_inject_ref--; | |
5296 | mutex_exit(&spa_namespace_lock); | |
5297 | } | |
5298 | ||
5299 | /* | |
5300 | * Add spares device information to the nvlist. | |
5301 | */ | |
5302 | static void | |
5303 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
5304 | { | |
5305 | nvlist_t **spares; | |
5306 | uint_t i, nspares; | |
5307 | nvlist_t *nvroot; | |
5308 | uint64_t guid; | |
5309 | vdev_stat_t *vs; | |
5310 | uint_t vsc; | |
5311 | uint64_t pool; | |
5312 | ||
9babb374 BB |
5313 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5314 | ||
34dc7c2f BB |
5315 | if (spa->spa_spares.sav_count == 0) |
5316 | return; | |
5317 | ||
65ad5d11 AJ |
5318 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
5319 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
5320 | ZPOOL_CONFIG_SPARES, &spares, &nspares)); | |
34dc7c2f | 5321 | if (nspares != 0) { |
795075e6 PD |
5322 | fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, |
5323 | (const nvlist_t * const *)spares, nspares); | |
65ad5d11 AJ |
5324 | VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, |
5325 | &spares, &nspares)); | |
34dc7c2f BB |
5326 | |
5327 | /* | |
5328 | * Go through and find any spares which have since been | |
5329 | * repurposed as an active spare. If this is the case, update | |
5330 | * their status appropriately. | |
5331 | */ | |
5332 | for (i = 0; i < nspares; i++) { | |
65ad5d11 AJ |
5333 | guid = fnvlist_lookup_uint64(spares[i], |
5334 | ZPOOL_CONFIG_GUID); | |
b128c09f BB |
5335 | if (spa_spare_exists(guid, &pool, NULL) && |
5336 | pool != 0ULL) { | |
65ad5d11 AJ |
5337 | VERIFY0(nvlist_lookup_uint64_array(spares[i], |
5338 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, | |
5339 | &vsc)); | |
34dc7c2f BB |
5340 | vs->vs_state = VDEV_STATE_CANT_OPEN; |
5341 | vs->vs_aux = VDEV_AUX_SPARED; | |
5342 | } | |
5343 | } | |
5344 | } | |
5345 | } | |
5346 | ||
5347 | /* | |
5348 | * Add l2cache device information to the nvlist, including vdev stats. | |
5349 | */ | |
5350 | static void | |
5351 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
5352 | { | |
5353 | nvlist_t **l2cache; | |
5354 | uint_t i, j, nl2cache; | |
5355 | nvlist_t *nvroot; | |
5356 | uint64_t guid; | |
5357 | vdev_t *vd; | |
5358 | vdev_stat_t *vs; | |
5359 | uint_t vsc; | |
5360 | ||
9babb374 BB |
5361 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5362 | ||
34dc7c2f BB |
5363 | if (spa->spa_l2cache.sav_count == 0) |
5364 | return; | |
5365 | ||
65ad5d11 AJ |
5366 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
5367 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
5368 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache)); | |
34dc7c2f | 5369 | if (nl2cache != 0) { |
795075e6 PD |
5370 | fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
5371 | (const nvlist_t * const *)l2cache, nl2cache); | |
65ad5d11 AJ |
5372 | VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
5373 | &l2cache, &nl2cache)); | |
34dc7c2f BB |
5374 | |
5375 | /* | |
5376 | * Update level 2 cache device stats. | |
5377 | */ | |
5378 | ||
5379 | for (i = 0; i < nl2cache; i++) { | |
65ad5d11 AJ |
5380 | guid = fnvlist_lookup_uint64(l2cache[i], |
5381 | ZPOOL_CONFIG_GUID); | |
34dc7c2f BB |
5382 | |
5383 | vd = NULL; | |
5384 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
5385 | if (guid == | |
5386 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
5387 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
5388 | break; | |
5389 | } | |
5390 | } | |
5391 | ASSERT(vd != NULL); | |
5392 | ||
65ad5d11 AJ |
5393 | VERIFY0(nvlist_lookup_uint64_array(l2cache[i], |
5394 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)); | |
34dc7c2f | 5395 | vdev_get_stats(vd, vs); |
193a37cb TH |
5396 | vdev_config_generate_stats(vd, l2cache[i]); |
5397 | ||
34dc7c2f BB |
5398 | } |
5399 | } | |
34dc7c2f BB |
5400 | } |
5401 | ||
9ae529ec | 5402 | static void |
417104bd | 5403 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) |
9ae529ec | 5404 | { |
9ae529ec CS |
5405 | zap_cursor_t zc; |
5406 | zap_attribute_t za; | |
5407 | ||
9ae529ec CS |
5408 | if (spa->spa_feat_for_read_obj != 0) { |
5409 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5410 | spa->spa_feat_for_read_obj); | |
5411 | zap_cursor_retrieve(&zc, &za) == 0; | |
5412 | zap_cursor_advance(&zc)) { | |
5413 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5414 | za.za_num_integers == 1); | |
417104bd | 5415 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5416 | za.za_first_integer)); |
5417 | } | |
5418 | zap_cursor_fini(&zc); | |
5419 | } | |
5420 | ||
5421 | if (spa->spa_feat_for_write_obj != 0) { | |
5422 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5423 | spa->spa_feat_for_write_obj); | |
5424 | zap_cursor_retrieve(&zc, &za) == 0; | |
5425 | zap_cursor_advance(&zc)) { | |
5426 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5427 | za.za_num_integers == 1); | |
417104bd | 5428 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5429 | za.za_first_integer)); |
5430 | } | |
5431 | zap_cursor_fini(&zc); | |
5432 | } | |
417104bd NB |
5433 | } |
5434 | ||
5435 | static void | |
5436 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
5437 | { | |
5438 | int i; | |
5439 | ||
5440 | for (i = 0; i < SPA_FEATURES; i++) { | |
5441 | zfeature_info_t feature = spa_feature_table[i]; | |
5442 | uint64_t refcount; | |
5443 | ||
5444 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
5445 | continue; | |
5446 | ||
5447 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
5448 | } | |
5449 | } | |
5450 | ||
5451 | /* | |
5452 | * Store a list of pool features and their reference counts in the | |
5453 | * config. | |
5454 | * | |
5455 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
5456 | * the pool features and reference counts from disk, then save the list | |
5457 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
5458 | * and refresh its values from the cached reference counts. This | |
5459 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
5460 | * clear' can resume the pool. | |
5461 | */ | |
5462 | static void | |
5463 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
5464 | { | |
4eb30c68 | 5465 | nvlist_t *features; |
417104bd NB |
5466 | |
5467 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
5468 | ||
4eb30c68 NB |
5469 | mutex_enter(&spa->spa_feat_stats_lock); |
5470 | features = spa->spa_feat_stats; | |
5471 | ||
417104bd NB |
5472 | if (features != NULL) { |
5473 | spa_feature_stats_from_cache(spa, features); | |
5474 | } else { | |
5475 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
5476 | spa->spa_feat_stats = features; | |
5477 | spa_feature_stats_from_disk(spa, features); | |
5478 | } | |
9ae529ec | 5479 | |
417104bd NB |
5480 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, |
5481 | features)); | |
4eb30c68 NB |
5482 | |
5483 | mutex_exit(&spa->spa_feat_stats_lock); | |
9ae529ec CS |
5484 | } |
5485 | ||
34dc7c2f | 5486 | int |
9ae529ec CS |
5487 | spa_get_stats(const char *name, nvlist_t **config, |
5488 | char *altroot, size_t buflen) | |
34dc7c2f BB |
5489 | { |
5490 | int error; | |
5491 | spa_t *spa; | |
5492 | ||
5493 | *config = NULL; | |
428870ff | 5494 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 5495 | |
9babb374 BB |
5496 | if (spa != NULL) { |
5497 | /* | |
5498 | * This still leaves a window of inconsistency where the spares | |
5499 | * or l2cache devices could change and the config would be | |
5500 | * self-inconsistent. | |
5501 | */ | |
5502 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 5503 | |
9babb374 | 5504 | if (*config != NULL) { |
572e2857 BB |
5505 | uint64_t loadtimes[2]; |
5506 | ||
5507 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
5508 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
65ad5d11 AJ |
5509 | fnvlist_add_uint64_array(*config, |
5510 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2); | |
572e2857 | 5511 | |
65ad5d11 | 5512 | fnvlist_add_uint64(*config, |
9babb374 | 5513 | ZPOOL_CONFIG_ERRCOUNT, |
65ad5d11 | 5514 | spa_get_errlog_size(spa)); |
9babb374 | 5515 | |
cec3a0a1 | 5516 | if (spa_suspended(spa)) { |
65ad5d11 | 5517 | fnvlist_add_uint64(*config, |
9babb374 | 5518 | ZPOOL_CONFIG_SUSPENDED, |
65ad5d11 AJ |
5519 | spa->spa_failmode); |
5520 | fnvlist_add_uint64(*config, | |
cec3a0a1 | 5521 | ZPOOL_CONFIG_SUSPENDED_REASON, |
65ad5d11 | 5522 | spa->spa_suspended); |
cec3a0a1 | 5523 | } |
b128c09f | 5524 | |
9babb374 BB |
5525 | spa_add_spares(spa, *config); |
5526 | spa_add_l2cache(spa, *config); | |
9ae529ec | 5527 | spa_add_feature_stats(spa, *config); |
9babb374 | 5528 | } |
34dc7c2f BB |
5529 | } |
5530 | ||
5531 | /* | |
5532 | * We want to get the alternate root even for faulted pools, so we cheat | |
5533 | * and call spa_lookup() directly. | |
5534 | */ | |
5535 | if (altroot) { | |
5536 | if (spa == NULL) { | |
5537 | mutex_enter(&spa_namespace_lock); | |
5538 | spa = spa_lookup(name); | |
5539 | if (spa) | |
5540 | spa_altroot(spa, altroot, buflen); | |
5541 | else | |
5542 | altroot[0] = '\0'; | |
5543 | spa = NULL; | |
5544 | mutex_exit(&spa_namespace_lock); | |
5545 | } else { | |
5546 | spa_altroot(spa, altroot, buflen); | |
5547 | } | |
5548 | } | |
5549 | ||
9babb374 BB |
5550 | if (spa != NULL) { |
5551 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 5552 | spa_close(spa, FTAG); |
9babb374 | 5553 | } |
34dc7c2f BB |
5554 | |
5555 | return (error); | |
5556 | } | |
5557 | ||
5558 | /* | |
5559 | * Validate that the auxiliary device array is well formed. We must have an | |
5560 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
5561 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
5562 | * specified, as long as they are well-formed. | |
5563 | */ | |
5564 | static int | |
5565 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
5566 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
5567 | vdev_labeltype_t label) | |
5568 | { | |
5569 | nvlist_t **dev; | |
5570 | uint_t i, ndev; | |
5571 | vdev_t *vd; | |
5572 | int error; | |
5573 | ||
b128c09f BB |
5574 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5575 | ||
34dc7c2f BB |
5576 | /* |
5577 | * It's acceptable to have no devs specified. | |
5578 | */ | |
5579 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
5580 | return (0); | |
5581 | ||
5582 | if (ndev == 0) | |
2e528b49 | 5583 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
5584 | |
5585 | /* | |
5586 | * Make sure the pool is formatted with a version that supports this | |
5587 | * device type. | |
5588 | */ | |
5589 | if (spa_version(spa) < version) | |
2e528b49 | 5590 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
5591 | |
5592 | /* | |
5593 | * Set the pending device list so we correctly handle device in-use | |
5594 | * checking. | |
5595 | */ | |
5596 | sav->sav_pending = dev; | |
5597 | sav->sav_npending = ndev; | |
5598 | ||
5599 | for (i = 0; i < ndev; i++) { | |
5600 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
5601 | mode)) != 0) | |
5602 | goto out; | |
5603 | ||
5604 | if (!vd->vdev_ops->vdev_op_leaf) { | |
5605 | vdev_free(vd); | |
2e528b49 | 5606 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5607 | goto out; |
5608 | } | |
5609 | ||
34dc7c2f BB |
5610 | vd->vdev_top = vd; |
5611 | ||
5612 | if ((error = vdev_open(vd)) == 0 && | |
5613 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
65ad5d11 AJ |
5614 | fnvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, |
5615 | vd->vdev_guid); | |
34dc7c2f BB |
5616 | } |
5617 | ||
5618 | vdev_free(vd); | |
5619 | ||
5620 | if (error && | |
5621 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
5622 | goto out; | |
5623 | else | |
5624 | error = 0; | |
5625 | } | |
5626 | ||
5627 | out: | |
5628 | sav->sav_pending = NULL; | |
5629 | sav->sav_npending = 0; | |
5630 | return (error); | |
5631 | } | |
5632 | ||
5633 | static int | |
5634 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
5635 | { | |
5636 | int error; | |
5637 | ||
b128c09f BB |
5638 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5639 | ||
34dc7c2f BB |
5640 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
5641 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
5642 | VDEV_LABEL_SPARE)) != 0) { | |
5643 | return (error); | |
5644 | } | |
5645 | ||
5646 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
5647 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
5648 | VDEV_LABEL_L2CACHE)); | |
5649 | } | |
5650 | ||
5651 | static void | |
5652 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
5653 | const char *config) | |
5654 | { | |
5655 | int i; | |
5656 | ||
5657 | if (sav->sav_config != NULL) { | |
5658 | nvlist_t **olddevs; | |
5659 | uint_t oldndevs; | |
5660 | nvlist_t **newdevs; | |
5661 | ||
5662 | /* | |
4e33ba4c | 5663 | * Generate new dev list by concatenating with the |
34dc7c2f BB |
5664 | * current dev list. |
5665 | */ | |
65ad5d11 AJ |
5666 | VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config, config, |
5667 | &olddevs, &oldndevs)); | |
34dc7c2f BB |
5668 | |
5669 | newdevs = kmem_alloc(sizeof (void *) * | |
79c76d5b | 5670 | (ndevs + oldndevs), KM_SLEEP); |
34dc7c2f | 5671 | for (i = 0; i < oldndevs; i++) |
65ad5d11 | 5672 | newdevs[i] = fnvlist_dup(olddevs[i]); |
34dc7c2f | 5673 | for (i = 0; i < ndevs; i++) |
65ad5d11 | 5674 | newdevs[i + oldndevs] = fnvlist_dup(devs[i]); |
34dc7c2f | 5675 | |
65ad5d11 | 5676 | fnvlist_remove(sav->sav_config, config); |
34dc7c2f | 5677 | |
795075e6 PD |
5678 | fnvlist_add_nvlist_array(sav->sav_config, config, |
5679 | (const nvlist_t * const *)newdevs, ndevs + oldndevs); | |
34dc7c2f BB |
5680 | for (i = 0; i < oldndevs + ndevs; i++) |
5681 | nvlist_free(newdevs[i]); | |
5682 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
5683 | } else { | |
5684 | /* | |
5685 | * Generate a new dev list. | |
5686 | */ | |
65ad5d11 | 5687 | sav->sav_config = fnvlist_alloc(); |
795075e6 PD |
5688 | fnvlist_add_nvlist_array(sav->sav_config, config, |
5689 | (const nvlist_t * const *)devs, ndevs); | |
34dc7c2f BB |
5690 | } |
5691 | } | |
5692 | ||
5693 | /* | |
5694 | * Stop and drop level 2 ARC devices | |
5695 | */ | |
5696 | void | |
5697 | spa_l2cache_drop(spa_t *spa) | |
5698 | { | |
5699 | vdev_t *vd; | |
5700 | int i; | |
5701 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
5702 | ||
5703 | for (i = 0; i < sav->sav_count; i++) { | |
5704 | uint64_t pool; | |
5705 | ||
5706 | vd = sav->sav_vdevs[i]; | |
5707 | ASSERT(vd != NULL); | |
5708 | ||
fb5f0bc8 BB |
5709 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
5710 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 5711 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
5712 | } |
5713 | } | |
5714 | ||
b5256303 TC |
5715 | /* |
5716 | * Verify encryption parameters for spa creation. If we are encrypting, we must | |
5717 | * have the encryption feature flag enabled. | |
5718 | */ | |
5719 | static int | |
5720 | spa_create_check_encryption_params(dsl_crypto_params_t *dcp, | |
5721 | boolean_t has_encryption) | |
5722 | { | |
5723 | if (dcp->cp_crypt != ZIO_CRYPT_OFF && | |
5724 | dcp->cp_crypt != ZIO_CRYPT_INHERIT && | |
5725 | !has_encryption) | |
5726 | return (SET_ERROR(ENOTSUP)); | |
5727 | ||
1fff937a | 5728 | return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); |
b5256303 TC |
5729 | } |
5730 | ||
34dc7c2f BB |
5731 | /* |
5732 | * Pool Creation | |
5733 | */ | |
5734 | int | |
5735 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b5256303 | 5736 | nvlist_t *zplprops, dsl_crypto_params_t *dcp) |
34dc7c2f BB |
5737 | { |
5738 | spa_t *spa; | |
5739 | char *altroot = NULL; | |
5740 | vdev_t *rvd; | |
5741 | dsl_pool_t *dp; | |
5742 | dmu_tx_t *tx; | |
9babb374 | 5743 | int error = 0; |
34dc7c2f BB |
5744 | uint64_t txg = TXG_INITIAL; |
5745 | nvlist_t **spares, **l2cache; | |
5746 | uint_t nspares, nl2cache; | |
b2255edc | 5747 | uint64_t version, obj, ndraid = 0; |
9ae529ec | 5748 | boolean_t has_features; |
b5256303 | 5749 | boolean_t has_encryption; |
715c996d | 5750 | boolean_t has_allocclass; |
b5256303 TC |
5751 | spa_feature_t feat; |
5752 | char *feat_name; | |
83e9986f RY |
5753 | char *poolname; |
5754 | nvlist_t *nvl; | |
5755 | ||
cc99f275 DB |
5756 | if (props == NULL || |
5757 | nvlist_lookup_string(props, "tname", &poolname) != 0) | |
83e9986f | 5758 | poolname = (char *)pool; |
34dc7c2f BB |
5759 | |
5760 | /* | |
5761 | * If this pool already exists, return failure. | |
5762 | */ | |
5763 | mutex_enter(&spa_namespace_lock); | |
83e9986f | 5764 | if (spa_lookup(poolname) != NULL) { |
34dc7c2f | 5765 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5766 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5767 | } |
5768 | ||
5769 | /* | |
5770 | * Allocate a new spa_t structure. | |
5771 | */ | |
83e9986f RY |
5772 | nvl = fnvlist_alloc(); |
5773 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
34dc7c2f BB |
5774 | (void) nvlist_lookup_string(props, |
5775 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
83e9986f RY |
5776 | spa = spa_add(poolname, nvl, altroot); |
5777 | fnvlist_free(nvl); | |
fb5f0bc8 | 5778 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5779 | |
34dc7c2f | 5780 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
5781 | spa_deactivate(spa); |
5782 | spa_remove(spa); | |
b128c09f | 5783 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
5784 | return (error); |
5785 | } | |
5786 | ||
83e9986f RY |
5787 | /* |
5788 | * Temporary pool names should never be written to disk. | |
5789 | */ | |
5790 | if (poolname != pool) | |
5791 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
5792 | ||
9ae529ec | 5793 | has_features = B_FALSE; |
b5256303 | 5794 | has_encryption = B_FALSE; |
715c996d | 5795 | has_allocclass = B_FALSE; |
1c27024e | 5796 | for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); |
9ae529ec | 5797 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { |
b5256303 | 5798 | if (zpool_prop_feature(nvpair_name(elem))) { |
9ae529ec | 5799 | has_features = B_TRUE; |
b5256303 TC |
5800 | |
5801 | feat_name = strchr(nvpair_name(elem), '@') + 1; | |
5802 | VERIFY0(zfeature_lookup_name(feat_name, &feat)); | |
5803 | if (feat == SPA_FEATURE_ENCRYPTION) | |
5804 | has_encryption = B_TRUE; | |
715c996d | 5805 | if (feat == SPA_FEATURE_ALLOCATION_CLASSES) |
5806 | has_allocclass = B_TRUE; | |
b5256303 TC |
5807 | } |
5808 | } | |
5809 | ||
5810 | /* verify encryption params, if they were provided */ | |
5811 | if (dcp != NULL) { | |
5812 | error = spa_create_check_encryption_params(dcp, has_encryption); | |
5813 | if (error != 0) { | |
5814 | spa_deactivate(spa); | |
5815 | spa_remove(spa); | |
5816 | mutex_exit(&spa_namespace_lock); | |
5817 | return (error); | |
5818 | } | |
9ae529ec | 5819 | } |
c24fa4b1 | 5820 | if (!has_allocclass && zfs_special_devs(nvroot, NULL)) { |
715c996d | 5821 | spa_deactivate(spa); |
5822 | spa_remove(spa); | |
5823 | mutex_exit(&spa_namespace_lock); | |
5824 | return (ENOTSUP); | |
5825 | } | |
9ae529ec CS |
5826 | |
5827 | if (has_features || nvlist_lookup_uint64(props, | |
5828 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 5829 | version = SPA_VERSION; |
9ae529ec CS |
5830 | } |
5831 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
5832 | |
5833 | spa->spa_first_txg = txg; | |
5834 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
5835 | spa->spa_uberblock.ub_version = version; |
5836 | spa->spa_ubsync = spa->spa_uberblock; | |
3dfb57a3 | 5837 | spa->spa_load_state = SPA_LOAD_CREATE; |
a1d477c2 MA |
5838 | spa->spa_removing_phys.sr_state = DSS_NONE; |
5839 | spa->spa_removing_phys.sr_removing_vdev = -1; | |
5840 | spa->spa_removing_phys.sr_prev_indirect_vdev = -1; | |
944a3724 | 5841 | spa->spa_indirect_vdevs_loaded = B_TRUE; |
34dc7c2f | 5842 | |
9babb374 BB |
5843 | /* |
5844 | * Create "The Godfather" zio to hold all async IOs | |
5845 | */ | |
e022864d MA |
5846 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
5847 | KM_SLEEP); | |
1c27024e | 5848 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
5849 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
5850 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
5851 | ZIO_FLAG_GODFATHER); | |
5852 | } | |
9babb374 | 5853 | |
34dc7c2f BB |
5854 | /* |
5855 | * Create the root vdev. | |
5856 | */ | |
b128c09f | 5857 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5858 | |
5859 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
5860 | ||
5861 | ASSERT(error != 0 || rvd != NULL); | |
5862 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
5863 | ||
5864 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
2e528b49 | 5865 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5866 | |
5867 | if (error == 0 && | |
5868 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
b2255edc BB |
5869 | (error = vdev_draid_spare_create(nvroot, rvd, &ndraid, 0)) == 0 && |
5870 | (error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) == 0) { | |
cc99f275 DB |
5871 | /* |
5872 | * instantiate the metaslab groups (this will dirty the vdevs) | |
5873 | * we can no longer error exit past this point | |
5874 | */ | |
5875 | for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { | |
5876 | vdev_t *vd = rvd->vdev_child[c]; | |
5877 | ||
5878 | vdev_metaslab_set_size(vd); | |
5879 | vdev_expand(vd, txg); | |
9babb374 | 5880 | } |
34dc7c2f BB |
5881 | } |
5882 | ||
b128c09f | 5883 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5884 | |
5885 | if (error != 0) { | |
5886 | spa_unload(spa); | |
5887 | spa_deactivate(spa); | |
5888 | spa_remove(spa); | |
5889 | mutex_exit(&spa_namespace_lock); | |
5890 | return (error); | |
5891 | } | |
5892 | ||
5893 | /* | |
5894 | * Get the list of spares, if specified. | |
5895 | */ | |
5896 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5897 | &spares, &nspares) == 0) { | |
65ad5d11 AJ |
5898 | spa->spa_spares.sav_config = fnvlist_alloc(); |
5899 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
795075e6 PD |
5900 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
5901 | nspares); | |
b128c09f | 5902 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5903 | spa_load_spares(spa); |
b128c09f | 5904 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5905 | spa->spa_spares.sav_sync = B_TRUE; |
5906 | } | |
5907 | ||
5908 | /* | |
5909 | * Get the list of level 2 cache devices, if specified. | |
5910 | */ | |
5911 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5912 | &l2cache, &nl2cache) == 0) { | |
795075e6 PD |
5913 | VERIFY0(nvlist_alloc(&spa->spa_l2cache.sav_config, |
5914 | NV_UNIQUE_NAME, KM_SLEEP)); | |
65ad5d11 | 5915 | fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
795075e6 PD |
5916 | ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache, |
5917 | nl2cache); | |
b128c09f | 5918 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5919 | spa_load_l2cache(spa); |
b128c09f | 5920 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5921 | spa->spa_l2cache.sav_sync = B_TRUE; |
5922 | } | |
5923 | ||
9ae529ec | 5924 | spa->spa_is_initializing = B_TRUE; |
b5256303 | 5925 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); |
9ae529ec | 5926 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 5927 | |
428870ff BB |
5928 | /* |
5929 | * Create DDTs (dedup tables). | |
5930 | */ | |
5931 | ddt_create(spa); | |
5932 | ||
5933 | spa_update_dspace(spa); | |
5934 | ||
34dc7c2f BB |
5935 | tx = dmu_tx_create_assigned(dp, txg); |
5936 | ||
d5e024cb BB |
5937 | /* |
5938 | * Create the pool's history object. | |
5939 | */ | |
5940 | if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history) | |
5941 | spa_history_create_obj(spa, tx); | |
5942 | ||
5943 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); | |
5944 | spa_history_log_version(spa, "create", tx); | |
5945 | ||
34dc7c2f BB |
5946 | /* |
5947 | * Create the pool config object. | |
5948 | */ | |
5949 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 5950 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
5951 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
5952 | ||
5953 | if (zap_add(spa->spa_meta_objset, | |
5954 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
5955 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
5956 | cmn_err(CE_PANIC, "failed to add pool config"); | |
5957 | } | |
5958 | ||
428870ff BB |
5959 | if (zap_add(spa->spa_meta_objset, |
5960 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
5961 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
5962 | cmn_err(CE_PANIC, "failed to add pool version"); | |
5963 | } | |
5964 | ||
34dc7c2f BB |
5965 | /* Newly created pools with the right version are always deflated. */ |
5966 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
5967 | spa->spa_deflate = TRUE; | |
5968 | if (zap_add(spa->spa_meta_objset, | |
5969 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
5970 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
5971 | cmn_err(CE_PANIC, "failed to add deflate"); | |
5972 | } | |
5973 | } | |
5974 | ||
5975 | /* | |
428870ff | 5976 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
5977 | * because sync-to-convergence takes longer if the blocksize |
5978 | * keeps changing. | |
5979 | */ | |
428870ff BB |
5980 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
5981 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 5982 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 5983 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
5984 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
5985 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
5986 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 5987 | } |
428870ff BB |
5988 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
5989 | spa->spa_meta_objset, obj)); | |
34dc7c2f | 5990 | |
3c67d83a TH |
5991 | /* |
5992 | * Generate some random noise for salted checksums to operate on. | |
5993 | */ | |
5994 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
5995 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
5996 | ||
34dc7c2f BB |
5997 | /* |
5998 | * Set pool properties. | |
5999 | */ | |
6000 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
6001 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
6002 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 6003 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
379ca9cf | 6004 | spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); |
1b939560 | 6005 | spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM); |
428870ff | 6006 | |
d164b209 BB |
6007 | if (props != NULL) { |
6008 | spa_configfile_set(spa, props, B_FALSE); | |
13fe0198 | 6009 | spa_sync_props(props, tx); |
d164b209 | 6010 | } |
34dc7c2f | 6011 | |
b2255edc BB |
6012 | for (int i = 0; i < ndraid; i++) |
6013 | spa_feature_incr(spa, SPA_FEATURE_DRAID, tx); | |
6014 | ||
34dc7c2f BB |
6015 | dmu_tx_commit(tx); |
6016 | ||
6017 | spa->spa_sync_on = B_TRUE; | |
b5256303 | 6018 | txg_sync_start(dp); |
379ca9cf | 6019 | mmp_thread_start(spa); |
b5256303 | 6020 | txg_wait_synced(dp, txg); |
34dc7c2f | 6021 | |
9d5b5245 SD |
6022 | spa_spawn_aux_threads(spa); |
6023 | ||
a1d477c2 | 6024 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
34dc7c2f | 6025 | |
0c66c32d JG |
6026 | /* |
6027 | * Don't count references from objsets that are already closed | |
6028 | * and are making their way through the eviction process. | |
6029 | */ | |
6030 | spa_evicting_os_wait(spa); | |
424fd7c3 | 6031 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
3dfb57a3 | 6032 | spa->spa_load_state = SPA_LOAD_NONE; |
b128c09f | 6033 | |
d164b209 BB |
6034 | mutex_exit(&spa_namespace_lock); |
6035 | ||
34dc7c2f BB |
6036 | return (0); |
6037 | } | |
6038 | ||
9babb374 BB |
6039 | /* |
6040 | * Import a non-root pool into the system. | |
6041 | */ | |
6042 | int | |
13fe0198 | 6043 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
6044 | { |
6045 | spa_t *spa; | |
6046 | char *altroot = NULL; | |
428870ff | 6047 | spa_load_state_t state = SPA_LOAD_IMPORT; |
8a393be3 | 6048 | zpool_load_policy_t policy; |
da92d5cb | 6049 | spa_mode_t mode = spa_mode_global; |
572e2857 | 6050 | uint64_t readonly = B_FALSE; |
9babb374 | 6051 | int error; |
34dc7c2f BB |
6052 | nvlist_t *nvroot; |
6053 | nvlist_t **spares, **l2cache; | |
6054 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
6055 | |
6056 | /* | |
6057 | * If a pool with this name exists, return failure. | |
6058 | */ | |
6059 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6060 | if (spa_lookup(pool) != NULL) { |
9babb374 | 6061 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 6062 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
6063 | } |
6064 | ||
6065 | /* | |
6066 | * Create and initialize the spa structure. | |
6067 | */ | |
6068 | (void) nvlist_lookup_string(props, | |
6069 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
6070 | (void) nvlist_lookup_uint64(props, |
6071 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
6072 | if (readonly) | |
da92d5cb | 6073 | mode = SPA_MODE_READ; |
428870ff | 6074 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
6075 | spa->spa_import_flags = flags; |
6076 | ||
6077 | /* | |
6078 | * Verbatim import - Take a pool and insert it into the namespace | |
6079 | * as if it had been loaded at boot. | |
6080 | */ | |
6081 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
6082 | if (props != NULL) | |
6083 | spa_configfile_set(spa, props, B_FALSE); | |
6084 | ||
a1d477c2 | 6085 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 6086 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
4a0ee12a | 6087 | zfs_dbgmsg("spa_import: verbatim import of %s", pool); |
572e2857 | 6088 | mutex_exit(&spa_namespace_lock); |
572e2857 BB |
6089 | return (0); |
6090 | } | |
6091 | ||
6092 | spa_activate(spa, mode); | |
34dc7c2f | 6093 | |
9babb374 BB |
6094 | /* |
6095 | * Don't start async tasks until we know everything is healthy. | |
6096 | */ | |
6097 | spa_async_suspend(spa); | |
b128c09f | 6098 | |
8a393be3 PZ |
6099 | zpool_get_load_policy(config, &policy); |
6100 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) | |
572e2857 BB |
6101 | state = SPA_LOAD_RECOVER; |
6102 | ||
6cb8e530 | 6103 | spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; |
572e2857 | 6104 | |
6cb8e530 PZ |
6105 | if (state != SPA_LOAD_RECOVER) { |
6106 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6107 | zfs_dbgmsg("spa_import: importing %s", pool); | |
6108 | } else { | |
6109 | zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " | |
8a393be3 | 6110 | "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); |
6cb8e530 | 6111 | } |
8a393be3 | 6112 | error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); |
428870ff BB |
6113 | |
6114 | /* | |
572e2857 BB |
6115 | * Propagate anything learned while loading the pool and pass it |
6116 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 6117 | */ |
65ad5d11 | 6118 | fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, spa->spa_load_info); |
34dc7c2f | 6119 | |
b128c09f | 6120 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6121 | /* |
9babb374 BB |
6122 | * Toss any existing sparelist, as it doesn't have any validity |
6123 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 6124 | */ |
9babb374 | 6125 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
6126 | nvlist_free(spa->spa_spares.sav_config); |
6127 | spa->spa_spares.sav_config = NULL; | |
6128 | spa_load_spares(spa); | |
6129 | } | |
9babb374 | 6130 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
6131 | nvlist_free(spa->spa_l2cache.sav_config); |
6132 | spa->spa_l2cache.sav_config = NULL; | |
6133 | spa_load_l2cache(spa); | |
6134 | } | |
6135 | ||
65ad5d11 | 6136 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
b128c09f | 6137 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 6138 | |
d164b209 BB |
6139 | if (props != NULL) |
6140 | spa_configfile_set(spa, props, B_FALSE); | |
6141 | ||
fb5f0bc8 BB |
6142 | if (error != 0 || (props && spa_writeable(spa) && |
6143 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
6144 | spa_unload(spa); |
6145 | spa_deactivate(spa); | |
6146 | spa_remove(spa); | |
34dc7c2f BB |
6147 | mutex_exit(&spa_namespace_lock); |
6148 | return (error); | |
6149 | } | |
6150 | ||
572e2857 BB |
6151 | spa_async_resume(spa); |
6152 | ||
34dc7c2f BB |
6153 | /* |
6154 | * Override any spares and level 2 cache devices as specified by | |
6155 | * the user, as these may have correct device names/devids, etc. | |
6156 | */ | |
6157 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
6158 | &spares, &nspares) == 0) { | |
6159 | if (spa->spa_spares.sav_config) | |
65ad5d11 AJ |
6160 | fnvlist_remove(spa->spa_spares.sav_config, |
6161 | ZPOOL_CONFIG_SPARES); | |
34dc7c2f | 6162 | else |
65ad5d11 AJ |
6163 | spa->spa_spares.sav_config = fnvlist_alloc(); |
6164 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
795075e6 PD |
6165 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
6166 | nspares); | |
b128c09f | 6167 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6168 | spa_load_spares(spa); |
b128c09f | 6169 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6170 | spa->spa_spares.sav_sync = B_TRUE; |
6171 | } | |
6172 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
6173 | &l2cache, &nl2cache) == 0) { | |
6174 | if (spa->spa_l2cache.sav_config) | |
65ad5d11 AJ |
6175 | fnvlist_remove(spa->spa_l2cache.sav_config, |
6176 | ZPOOL_CONFIG_L2CACHE); | |
34dc7c2f | 6177 | else |
65ad5d11 AJ |
6178 | spa->spa_l2cache.sav_config = fnvlist_alloc(); |
6179 | fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config, | |
795075e6 PD |
6180 | ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache, |
6181 | nl2cache); | |
b128c09f | 6182 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6183 | spa_load_l2cache(spa); |
b128c09f | 6184 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6185 | spa->spa_l2cache.sav_sync = B_TRUE; |
6186 | } | |
6187 | ||
428870ff BB |
6188 | /* |
6189 | * Check for any removed devices. | |
6190 | */ | |
6191 | if (spa->spa_autoreplace) { | |
6192 | spa_aux_check_removed(&spa->spa_spares); | |
6193 | spa_aux_check_removed(&spa->spa_l2cache); | |
6194 | } | |
6195 | ||
fb5f0bc8 | 6196 | if (spa_writeable(spa)) { |
b128c09f BB |
6197 | /* |
6198 | * Update the config cache to include the newly-imported pool. | |
6199 | */ | |
45d1cae3 | 6200 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 6201 | } |
34dc7c2f | 6202 | |
34dc7c2f | 6203 | /* |
9babb374 BB |
6204 | * It's possible that the pool was expanded while it was exported. |
6205 | * We kick off an async task to handle this for us. | |
34dc7c2f | 6206 | */ |
9babb374 | 6207 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 6208 | |
d5e024cb | 6209 | spa_history_log_version(spa, "import", NULL); |
fb390aaf | 6210 | |
12fa0466 | 6211 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
fb390aaf | 6212 | |
fb390aaf HR |
6213 | mutex_exit(&spa_namespace_lock); |
6214 | ||
ec213971 | 6215 | zvol_create_minors_recursive(pool); |
4a22ba5b | 6216 | |
b128c09f BB |
6217 | return (0); |
6218 | } | |
6219 | ||
34dc7c2f BB |
6220 | nvlist_t * |
6221 | spa_tryimport(nvlist_t *tryconfig) | |
6222 | { | |
6223 | nvlist_t *config = NULL; | |
6cb8e530 | 6224 | char *poolname, *cachefile; |
34dc7c2f BB |
6225 | spa_t *spa; |
6226 | uint64_t state; | |
d164b209 | 6227 | int error; |
8a393be3 | 6228 | zpool_load_policy_t policy; |
34dc7c2f BB |
6229 | |
6230 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
6231 | return (NULL); | |
6232 | ||
6233 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
6234 | return (NULL); | |
6235 | ||
6236 | /* | |
6237 | * Create and initialize the spa structure. | |
6238 | */ | |
6239 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6240 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
da92d5cb | 6241 | spa_activate(spa, SPA_MODE_READ); |
34dc7c2f BB |
6242 | |
6243 | /* | |
8a393be3 | 6244 | * Rewind pool if a max txg was provided. |
34dc7c2f | 6245 | */ |
8a393be3 PZ |
6246 | zpool_get_load_policy(spa->spa_config, &policy); |
6247 | if (policy.zlp_txg != UINT64_MAX) { | |
6248 | spa->spa_load_max_txg = policy.zlp_txg; | |
6cb8e530 PZ |
6249 | spa->spa_extreme_rewind = B_TRUE; |
6250 | zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", | |
8a393be3 | 6251 | poolname, (longlong_t)policy.zlp_txg); |
6cb8e530 PZ |
6252 | } else { |
6253 | zfs_dbgmsg("spa_tryimport: importing %s", poolname); | |
6254 | } | |
6255 | ||
6256 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) | |
6257 | == 0) { | |
6258 | zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); | |
6259 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; | |
6260 | } else { | |
6261 | spa->spa_config_source = SPA_CONFIG_SRC_SCAN; | |
6262 | } | |
6263 | ||
6264 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); | |
34dc7c2f BB |
6265 | |
6266 | /* | |
6267 | * If 'tryconfig' was at least parsable, return the current config. | |
6268 | */ | |
6269 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 6270 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
65ad5d11 AJ |
6271 | fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, poolname); |
6272 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, state); | |
6273 | fnvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
6274 | spa->spa_uberblock.ub_timestamp); | |
6275 | fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, | |
6276 | spa->spa_load_info); | |
6277 | fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, | |
6278 | spa->spa_errata); | |
34dc7c2f BB |
6279 | |
6280 | /* | |
6281 | * If the bootfs property exists on this pool then we | |
6282 | * copy it out so that external consumers can tell which | |
6283 | * pools are bootable. | |
6284 | */ | |
d164b209 | 6285 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
79c76d5b | 6286 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6287 | |
6288 | /* | |
6289 | * We have to play games with the name since the | |
6290 | * pool was opened as TRYIMPORT_NAME. | |
6291 | */ | |
b128c09f | 6292 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
6293 | spa->spa_bootfs, tmpname) == 0) { |
6294 | char *cp; | |
d1d7e268 MK |
6295 | char *dsname; |
6296 | ||
79c76d5b | 6297 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6298 | |
6299 | cp = strchr(tmpname, '/'); | |
6300 | if (cp == NULL) { | |
6301 | (void) strlcpy(dsname, tmpname, | |
6302 | MAXPATHLEN); | |
6303 | } else { | |
6304 | (void) snprintf(dsname, MAXPATHLEN, | |
6305 | "%s/%s", poolname, ++cp); | |
6306 | } | |
65ad5d11 AJ |
6307 | fnvlist_add_string(config, ZPOOL_CONFIG_BOOTFS, |
6308 | dsname); | |
34dc7c2f BB |
6309 | kmem_free(dsname, MAXPATHLEN); |
6310 | } | |
6311 | kmem_free(tmpname, MAXPATHLEN); | |
6312 | } | |
6313 | ||
6314 | /* | |
6315 | * Add the list of hot spares and level 2 cache devices. | |
6316 | */ | |
9babb374 | 6317 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
6318 | spa_add_spares(spa, config); |
6319 | spa_add_l2cache(spa, config); | |
9babb374 | 6320 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
6321 | } |
6322 | ||
6323 | spa_unload(spa); | |
6324 | spa_deactivate(spa); | |
6325 | spa_remove(spa); | |
6326 | mutex_exit(&spa_namespace_lock); | |
6327 | ||
6328 | return (config); | |
6329 | } | |
6330 | ||
6331 | /* | |
6332 | * Pool export/destroy | |
6333 | * | |
6334 | * The act of destroying or exporting a pool is very simple. We make sure there | |
6335 | * is no more pending I/O and any references to the pool are gone. Then, we | |
6336 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
6337 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
6338 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
6339 | */ |
6340 | static int | |
4d55ea81 | 6341 | spa_export_common(const char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 6342 | boolean_t force, boolean_t hardforce) |
34dc7c2f | 6343 | { |
f4f50a70 | 6344 | int error; |
34dc7c2f BB |
6345 | spa_t *spa; |
6346 | ||
6347 | if (oldconfig) | |
6348 | *oldconfig = NULL; | |
6349 | ||
da92d5cb | 6350 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
2e528b49 | 6351 | return (SET_ERROR(EROFS)); |
34dc7c2f BB |
6352 | |
6353 | mutex_enter(&spa_namespace_lock); | |
6354 | if ((spa = spa_lookup(pool)) == NULL) { | |
6355 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 6356 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
6357 | } |
6358 | ||
43a85362 SD |
6359 | if (spa->spa_is_exporting) { |
6360 | /* the pool is being exported by another thread */ | |
6361 | mutex_exit(&spa_namespace_lock); | |
6362 | return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS)); | |
6363 | } | |
6364 | spa->spa_is_exporting = B_TRUE; | |
6365 | ||
34dc7c2f BB |
6366 | /* |
6367 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
6368 | * reacquire the namespace lock, and see if we can export. | |
6369 | */ | |
6370 | spa_open_ref(spa, FTAG); | |
6371 | mutex_exit(&spa_namespace_lock); | |
6372 | spa_async_suspend(spa); | |
a0bd735a BP |
6373 | if (spa->spa_zvol_taskq) { |
6374 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
6375 | taskq_wait(spa->spa_zvol_taskq); | |
6376 | } | |
34dc7c2f BB |
6377 | mutex_enter(&spa_namespace_lock); |
6378 | spa_close(spa, FTAG); | |
6379 | ||
d14cfd83 IH |
6380 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) |
6381 | goto export_spa; | |
34dc7c2f | 6382 | /* |
d14cfd83 IH |
6383 | * The pool will be in core if it's openable, in which case we can |
6384 | * modify its state. Objsets may be open only because they're dirty, | |
6385 | * so we have to force it to sync before checking spa_refcnt. | |
34dc7c2f | 6386 | */ |
0c66c32d | 6387 | if (spa->spa_sync_on) { |
34dc7c2f | 6388 | txg_wait_synced(spa->spa_dsl_pool, 0); |
0c66c32d JG |
6389 | spa_evicting_os_wait(spa); |
6390 | } | |
34dc7c2f | 6391 | |
d14cfd83 IH |
6392 | /* |
6393 | * A pool cannot be exported or destroyed if there are active | |
6394 | * references. If we are resetting a pool, allow references by | |
6395 | * fault injection handlers. | |
6396 | */ | |
f4f50a70 WA |
6397 | if (!spa_refcount_zero(spa) || (spa->spa_inject_ref != 0)) { |
6398 | error = SET_ERROR(EBUSY); | |
6399 | goto fail; | |
d14cfd83 | 6400 | } |
34dc7c2f | 6401 | |
d14cfd83 | 6402 | if (spa->spa_sync_on) { |
b128c09f BB |
6403 | /* |
6404 | * A pool cannot be exported if it has an active shared spare. | |
6405 | * This is to prevent other pools stealing the active spare | |
6406 | * from an exported pool. At user's own will, such pool can | |
6407 | * be forcedly exported. | |
6408 | */ | |
6409 | if (!force && new_state == POOL_STATE_EXPORTED && | |
6410 | spa_has_active_shared_spare(spa)) { | |
f4f50a70 WA |
6411 | error = SET_ERROR(EXDEV); |
6412 | goto fail; | |
b128c09f | 6413 | } |
34dc7c2f | 6414 | |
619f0976 GW |
6415 | /* |
6416 | * We're about to export or destroy this pool. Make sure | |
1b939560 BB |
6417 | * we stop all initialization and trim activity here before |
6418 | * we set the spa_final_txg. This will ensure that all | |
619f0976 GW |
6419 | * dirty data resulting from the initialization is |
6420 | * committed to disk before we unload the pool. | |
6421 | */ | |
6422 | if (spa->spa_root_vdev != NULL) { | |
1b939560 BB |
6423 | vdev_t *rvd = spa->spa_root_vdev; |
6424 | vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE); | |
6425 | vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE); | |
6426 | vdev_autotrim_stop_all(spa); | |
9a49d3f3 | 6427 | vdev_rebuild_stop_all(spa); |
619f0976 GW |
6428 | } |
6429 | ||
34dc7c2f BB |
6430 | /* |
6431 | * We want this to be reflected on every label, | |
6432 | * so mark them all dirty. spa_unload() will do the | |
6433 | * final sync that pushes these changes out. | |
6434 | */ | |
fb5f0bc8 | 6435 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 6436 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6437 | spa->spa_state = new_state; |
2fb52853 GA |
6438 | vdev_config_dirty(spa->spa_root_vdev); |
6439 | spa_config_exit(spa, SCL_ALL, FTAG); | |
6440 | } | |
6441 | ||
6442 | /* | |
6443 | * If the log space map feature is enabled and the pool is | |
6444 | * getting exported (but not destroyed), we want to spend some | |
6445 | * time flushing as many metaslabs as we can in an attempt to | |
6446 | * destroy log space maps and save import time. This has to be | |
6447 | * done before we set the spa_final_txg, otherwise | |
6448 | * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs. | |
6449 | * spa_should_flush_logs_on_unload() should be called after | |
6450 | * spa_state has been set to the new_state. | |
6451 | */ | |
6452 | if (spa_should_flush_logs_on_unload(spa)) | |
6453 | spa_unload_log_sm_flush_all(spa); | |
6454 | ||
6455 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { | |
6456 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
428870ff BB |
6457 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
6458 | TXG_DEFER_SIZE + 1; | |
b128c09f | 6459 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6460 | } |
6461 | } | |
6462 | ||
d14cfd83 | 6463 | export_spa: |
d5e024cb BB |
6464 | if (new_state == POOL_STATE_DESTROYED) |
6465 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); | |
6466 | else if (new_state == POOL_STATE_EXPORTED) | |
6467 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT); | |
34dc7c2f BB |
6468 | |
6469 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
6470 | spa_unload(spa); | |
6471 | spa_deactivate(spa); | |
6472 | } | |
6473 | ||
6474 | if (oldconfig && spa->spa_config) | |
65ad5d11 | 6475 | *oldconfig = fnvlist_dup(spa->spa_config); |
34dc7c2f BB |
6476 | |
6477 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 | 6478 | if (!hardforce) |
a1d477c2 | 6479 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 6480 | spa_remove(spa); |
43a85362 SD |
6481 | } else { |
6482 | /* | |
6483 | * If spa_remove() is not called for this spa_t and | |
6484 | * there is any possibility that it can be reused, | |
6485 | * we make sure to reset the exporting flag. | |
6486 | */ | |
6487 | spa->spa_is_exporting = B_FALSE; | |
34dc7c2f | 6488 | } |
34dc7c2f | 6489 | |
43a85362 | 6490 | mutex_exit(&spa_namespace_lock); |
34dc7c2f | 6491 | return (0); |
f4f50a70 WA |
6492 | |
6493 | fail: | |
6494 | spa->spa_is_exporting = B_FALSE; | |
6495 | spa_async_resume(spa); | |
6496 | mutex_exit(&spa_namespace_lock); | |
6497 | return (error); | |
34dc7c2f BB |
6498 | } |
6499 | ||
6500 | /* | |
6501 | * Destroy a storage pool. | |
6502 | */ | |
6503 | int | |
4d55ea81 | 6504 | spa_destroy(const char *pool) |
34dc7c2f | 6505 | { |
fb5f0bc8 BB |
6506 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
6507 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
6508 | } |
6509 | ||
6510 | /* | |
6511 | * Export a storage pool. | |
6512 | */ | |
6513 | int | |
4d55ea81 | 6514 | spa_export(const char *pool, nvlist_t **oldconfig, boolean_t force, |
fb5f0bc8 | 6515 | boolean_t hardforce) |
34dc7c2f | 6516 | { |
fb5f0bc8 BB |
6517 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
6518 | force, hardforce)); | |
34dc7c2f BB |
6519 | } |
6520 | ||
6521 | /* | |
6522 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
6523 | * from the namespace in any way. | |
6524 | */ | |
6525 | int | |
4d55ea81 | 6526 | spa_reset(const char *pool) |
34dc7c2f | 6527 | { |
b128c09f | 6528 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 6529 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
6530 | } |
6531 | ||
34dc7c2f BB |
6532 | /* |
6533 | * ========================================================================== | |
6534 | * Device manipulation | |
6535 | * ========================================================================== | |
6536 | */ | |
6537 | ||
b2255edc BB |
6538 | /* |
6539 | * This is called as a synctask to increment the draid feature flag | |
6540 | */ | |
6541 | static void | |
6542 | spa_draid_feature_incr(void *arg, dmu_tx_t *tx) | |
6543 | { | |
6544 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
6545 | int draid = (int)(uintptr_t)arg; | |
6546 | ||
6547 | for (int c = 0; c < draid; c++) | |
6548 | spa_feature_incr(spa, SPA_FEATURE_DRAID, tx); | |
6549 | } | |
6550 | ||
34dc7c2f BB |
6551 | /* |
6552 | * Add a device to a storage pool. | |
6553 | */ | |
6554 | int | |
6555 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
6556 | { | |
b2255edc | 6557 | uint64_t txg, ndraid = 0; |
fb5f0bc8 | 6558 | int error; |
34dc7c2f BB |
6559 | vdev_t *rvd = spa->spa_root_vdev; |
6560 | vdev_t *vd, *tvd; | |
6561 | nvlist_t **spares, **l2cache; | |
6562 | uint_t nspares, nl2cache; | |
6563 | ||
572e2857 BB |
6564 | ASSERT(spa_writeable(spa)); |
6565 | ||
34dc7c2f BB |
6566 | txg = spa_vdev_enter(spa); |
6567 | ||
6568 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
6569 | VDEV_ALLOC_ADD)) != 0) | |
6570 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6571 | ||
b128c09f | 6572 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
6573 | |
6574 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
6575 | &nspares) != 0) | |
6576 | nspares = 0; | |
6577 | ||
6578 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
6579 | &nl2cache) != 0) | |
6580 | nl2cache = 0; | |
6581 | ||
b128c09f | 6582 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 6583 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 6584 | |
b128c09f | 6585 | if (vd->vdev_children != 0 && |
b2255edc | 6586 | (error = vdev_create(vd, txg, B_FALSE)) != 0) { |
b128c09f | 6587 | return (spa_vdev_exit(spa, vd, txg, error)); |
b2255edc BB |
6588 | } |
6589 | ||
6590 | /* | |
6591 | * The virtual dRAID spares must be added after vdev tree is created | |
bf169e9f | 6592 | * and the vdev guids are generated. The guid of their associated |
b2255edc BB |
6593 | * dRAID is stored in the config and used when opening the spare. |
6594 | */ | |
6595 | if ((error = vdev_draid_spare_create(nvroot, vd, &ndraid, | |
6596 | rvd->vdev_children)) == 0) { | |
6597 | if (ndraid > 0 && nvlist_lookup_nvlist_array(nvroot, | |
6598 | ZPOOL_CONFIG_SPARES, &spares, &nspares) != 0) | |
6599 | nspares = 0; | |
6600 | } else { | |
6601 | return (spa_vdev_exit(spa, vd, txg, error)); | |
6602 | } | |
34dc7c2f BB |
6603 | |
6604 | /* | |
6605 | * We must validate the spares and l2cache devices after checking the | |
6606 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
6607 | */ | |
b128c09f | 6608 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 6609 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
6610 | |
6611 | /* | |
a1d477c2 MA |
6612 | * If we are in the middle of a device removal, we can only add |
6613 | * devices which match the existing devices in the pool. | |
6614 | * If we are in the middle of a removal, or have some indirect | |
b2255edc | 6615 | * vdevs, we can not add raidz or dRAID top levels. |
34dc7c2f | 6616 | */ |
a1d477c2 MA |
6617 | if (spa->spa_vdev_removal != NULL || |
6618 | spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { | |
6619 | for (int c = 0; c < vd->vdev_children; c++) { | |
6620 | tvd = vd->vdev_child[c]; | |
6621 | if (spa->spa_vdev_removal != NULL && | |
9e052db4 | 6622 | tvd->vdev_ashift != spa->spa_max_ashift) { |
a1d477c2 MA |
6623 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
6624 | } | |
b2255edc BB |
6625 | /* Fail if top level vdev is raidz or a dRAID */ |
6626 | if (vdev_get_nparity(tvd) != 0) | |
a1d477c2 | 6627 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
b2255edc | 6628 | |
a1d477c2 MA |
6629 | /* |
6630 | * Need the top level mirror to be | |
6631 | * a mirror of leaf vdevs only | |
6632 | */ | |
6633 | if (tvd->vdev_ops == &vdev_mirror_ops) { | |
6634 | for (uint64_t cid = 0; | |
6635 | cid < tvd->vdev_children; cid++) { | |
6636 | vdev_t *cvd = tvd->vdev_child[cid]; | |
6637 | if (!cvd->vdev_ops->vdev_op_leaf) { | |
6638 | return (spa_vdev_exit(spa, vd, | |
6639 | txg, EINVAL)); | |
6640 | } | |
6641 | } | |
6642 | } | |
6643 | } | |
6644 | } | |
6645 | ||
1c27024e | 6646 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
6647 | tvd = vd->vdev_child[c]; |
6648 | vdev_remove_child(vd, tvd); | |
93e28d66 | 6649 | tvd->vdev_id = rvd->vdev_children; |
34dc7c2f BB |
6650 | vdev_add_child(rvd, tvd); |
6651 | vdev_config_dirty(tvd); | |
6652 | } | |
6653 | ||
6654 | if (nspares != 0) { | |
6655 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
6656 | ZPOOL_CONFIG_SPARES); | |
6657 | spa_load_spares(spa); | |
6658 | spa->spa_spares.sav_sync = B_TRUE; | |
6659 | } | |
6660 | ||
6661 | if (nl2cache != 0) { | |
6662 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
6663 | ZPOOL_CONFIG_L2CACHE); | |
6664 | spa_load_l2cache(spa); | |
6665 | spa->spa_l2cache.sav_sync = B_TRUE; | |
6666 | } | |
6667 | ||
b2255edc BB |
6668 | /* |
6669 | * We can't increment a feature while holding spa_vdev so we | |
6670 | * have to do it in a synctask. | |
6671 | */ | |
6672 | if (ndraid != 0) { | |
6673 | dmu_tx_t *tx; | |
6674 | ||
6675 | tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); | |
6676 | dsl_sync_task_nowait(spa->spa_dsl_pool, spa_draid_feature_incr, | |
6677 | (void *)(uintptr_t)ndraid, tx); | |
6678 | dmu_tx_commit(tx); | |
6679 | } | |
6680 | ||
34dc7c2f BB |
6681 | /* |
6682 | * We have to be careful when adding new vdevs to an existing pool. | |
6683 | * If other threads start allocating from these vdevs before we | |
6684 | * sync the config cache, and we lose power, then upon reboot we may | |
6685 | * fail to open the pool because there are DVAs that the config cache | |
6686 | * can't translate. Therefore, we first add the vdevs without | |
6687 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
6688 | * and then let spa_config_update() initialize the new metaslabs. | |
6689 | * | |
6690 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
6691 | * if we lose power at any point in this sequence, the remaining | |
6692 | * steps will be completed the next time we load the pool. | |
6693 | */ | |
6694 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
6695 | ||
6696 | mutex_enter(&spa_namespace_lock); | |
6697 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
12fa0466 | 6698 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); |
34dc7c2f BB |
6699 | mutex_exit(&spa_namespace_lock); |
6700 | ||
6701 | return (0); | |
6702 | } | |
6703 | ||
6704 | /* | |
6705 | * Attach a device to a mirror. The arguments are the path to any device | |
6706 | * in the mirror, and the nvroot for the new device. If the path specifies | |
6707 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
6708 | * | |
6709 | * If 'replacing' is specified, the new device is intended to replace the | |
6710 | * existing device; in this case the two devices are made into their own | |
6711 | * mirror using the 'replacing' vdev, which is functionally identical to | |
6712 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
6713 | * extra rules: you can't attach to it after it's been created, and upon | |
6714 | * completion of resilvering, the first disk (the one being replaced) | |
6715 | * is automatically detached. | |
9a49d3f3 BB |
6716 | * |
6717 | * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild) | |
6718 | * should be performed instead of traditional healing reconstruction. From | |
6719 | * an administrators perspective these are both resilver operations. | |
34dc7c2f BB |
6720 | */ |
6721 | int | |
9a49d3f3 BB |
6722 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing, |
6723 | int rebuild) | |
34dc7c2f | 6724 | { |
428870ff | 6725 | uint64_t txg, dtl_max_txg; |
9a49d3f3 | 6726 | vdev_t *rvd = spa->spa_root_vdev; |
34dc7c2f BB |
6727 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
6728 | vdev_ops_t *pvops; | |
b128c09f BB |
6729 | char *oldvdpath, *newvdpath; |
6730 | int newvd_isspare; | |
6731 | int error; | |
34dc7c2f | 6732 | |
572e2857 BB |
6733 | ASSERT(spa_writeable(spa)); |
6734 | ||
34dc7c2f BB |
6735 | txg = spa_vdev_enter(spa); |
6736 | ||
b128c09f | 6737 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6738 | |
d2734cce SD |
6739 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6740 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6741 | error = (spa_has_checkpoint(spa)) ? | |
6742 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6743 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6744 | } | |
6745 | ||
9a49d3f3 BB |
6746 | if (rebuild) { |
6747 | if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD)) | |
6748 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6749 | ||
6750 | if (dsl_scan_resilvering(spa_get_dsl(spa))) | |
6751 | return (spa_vdev_exit(spa, NULL, txg, | |
6752 | ZFS_ERR_RESILVER_IN_PROGRESS)); | |
6753 | } else { | |
6754 | if (vdev_rebuild_active(rvd)) | |
6755 | return (spa_vdev_exit(spa, NULL, txg, | |
6756 | ZFS_ERR_REBUILD_IN_PROGRESS)); | |
6757 | } | |
6758 | ||
9e052db4 | 6759 | if (spa->spa_vdev_removal != NULL) |
a1d477c2 | 6760 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
a1d477c2 | 6761 | |
34dc7c2f BB |
6762 | if (oldvd == NULL) |
6763 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6764 | ||
6765 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
6766 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6767 | ||
6768 | pvd = oldvd->vdev_parent; | |
6769 | ||
6770 | if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, | |
5ffb9d1d | 6771 | VDEV_ALLOC_ATTACH)) != 0) |
34dc7c2f BB |
6772 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
6773 | ||
6774 | if (newrootvd->vdev_children != 1) | |
6775 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6776 | ||
6777 | newvd = newrootvd->vdev_child[0]; | |
6778 | ||
6779 | if (!newvd->vdev_ops->vdev_op_leaf) | |
6780 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6781 | ||
6782 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
6783 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
6784 | ||
6785 | /* | |
6786 | * Spares can't replace logs | |
6787 | */ | |
b128c09f | 6788 | if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) |
34dc7c2f BB |
6789 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
6790 | ||
b2255edc BB |
6791 | /* |
6792 | * A dRAID spare can only replace a child of its parent dRAID vdev. | |
6793 | */ | |
6794 | if (newvd->vdev_ops == &vdev_draid_spare_ops && | |
6795 | oldvd->vdev_top != vdev_draid_spare_get_parent(newvd)) { | |
6796 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6797 | } | |
6798 | ||
9a49d3f3 BB |
6799 | if (rebuild) { |
6800 | /* | |
b2255edc | 6801 | * For rebuilds, the top vdev must support reconstruction |
9a49d3f3 | 6802 | * using only space maps. This means the only allowable |
b2255edc | 6803 | * vdevs types are the root vdev, a mirror, or dRAID. |
9a49d3f3 | 6804 | */ |
b2255edc BB |
6805 | tvd = pvd; |
6806 | if (pvd->vdev_top != NULL) | |
6807 | tvd = pvd->vdev_top; | |
6808 | ||
6809 | if (tvd->vdev_ops != &vdev_mirror_ops && | |
6810 | tvd->vdev_ops != &vdev_root_ops && | |
6811 | tvd->vdev_ops != &vdev_draid_ops) { | |
9a49d3f3 BB |
6812 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
6813 | } | |
6814 | } | |
6815 | ||
34dc7c2f BB |
6816 | if (!replacing) { |
6817 | /* | |
6818 | * For attach, the only allowable parent is a mirror or the root | |
6819 | * vdev. | |
6820 | */ | |
6821 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6822 | pvd->vdev_ops != &vdev_root_ops) | |
6823 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6824 | ||
6825 | pvops = &vdev_mirror_ops; | |
6826 | } else { | |
6827 | /* | |
6828 | * Active hot spares can only be replaced by inactive hot | |
6829 | * spares. | |
6830 | */ | |
6831 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 6832 | oldvd->vdev_isspare && |
34dc7c2f BB |
6833 | !spa_has_spare(spa, newvd->vdev_guid)) |
6834 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6835 | ||
6836 | /* | |
6837 | * If the source is a hot spare, and the parent isn't already a | |
6838 | * spare, then we want to create a new hot spare. Otherwise, we | |
6839 | * want to create a replacing vdev. The user is not allowed to | |
6840 | * attach to a spared vdev child unless the 'isspare' state is | |
6841 | * the same (spare replaces spare, non-spare replaces | |
6842 | * non-spare). | |
6843 | */ | |
572e2857 BB |
6844 | if (pvd->vdev_ops == &vdev_replacing_ops && |
6845 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 6846 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6847 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
6848 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 6849 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6850 | } |
6851 | ||
6852 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
6853 | pvops = &vdev_spare_ops; |
6854 | else | |
6855 | pvops = &vdev_replacing_ops; | |
6856 | } | |
6857 | ||
6858 | /* | |
9babb374 | 6859 | * Make sure the new device is big enough. |
34dc7c2f | 6860 | */ |
9babb374 | 6861 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
6862 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
6863 | ||
6864 | /* | |
6865 | * The new device cannot have a higher alignment requirement | |
6866 | * than the top-level vdev. | |
6867 | */ | |
6868 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
9a49d3f3 | 6869 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
34dc7c2f BB |
6870 | |
6871 | /* | |
6872 | * If this is an in-place replacement, update oldvd's path and devid | |
6873 | * to make it distinguishable from newvd, and unopenable from now on. | |
6874 | */ | |
6875 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
6876 | spa_strfree(oldvd->vdev_path); | |
6877 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
79c76d5b | 6878 | KM_SLEEP); |
c9e319fa JL |
6879 | (void) snprintf(oldvd->vdev_path, strlen(newvd->vdev_path) + 5, |
6880 | "%s/%s", newvd->vdev_path, "old"); | |
34dc7c2f BB |
6881 | if (oldvd->vdev_devid != NULL) { |
6882 | spa_strfree(oldvd->vdev_devid); | |
6883 | oldvd->vdev_devid = NULL; | |
6884 | } | |
6885 | } | |
6886 | ||
6887 | /* | |
6888 | * If the parent is not a mirror, or if we're replacing, insert the new | |
6889 | * mirror/replacing/spare vdev above oldvd. | |
6890 | */ | |
6891 | if (pvd->vdev_ops != pvops) | |
6892 | pvd = vdev_add_parent(oldvd, pvops); | |
6893 | ||
6894 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
6895 | ASSERT(pvd->vdev_ops == pvops); | |
6896 | ASSERT(oldvd->vdev_parent == pvd); | |
6897 | ||
6898 | /* | |
6899 | * Extract the new device from its root and add it to pvd. | |
6900 | */ | |
6901 | vdev_remove_child(newrootvd, newvd); | |
6902 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 6903 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
6904 | vdev_add_child(pvd, newvd); |
6905 | ||
6d82f98c IH |
6906 | /* |
6907 | * Reevaluate the parent vdev state. | |
6908 | */ | |
6909 | vdev_propagate_state(pvd); | |
6910 | ||
34dc7c2f BB |
6911 | tvd = newvd->vdev_top; |
6912 | ASSERT(pvd->vdev_top == tvd); | |
6913 | ASSERT(tvd->vdev_parent == rvd); | |
6914 | ||
6915 | vdev_config_dirty(tvd); | |
6916 | ||
6917 | /* | |
428870ff BB |
6918 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
6919 | * for any dmu_sync-ed blocks. It will propagate upward when | |
6920 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 6921 | */ |
428870ff | 6922 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 6923 | |
9a49d3f3 BB |
6924 | vdev_dtl_dirty(newvd, DTL_MISSING, |
6925 | TXG_INITIAL, dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 6926 | |
9babb374 | 6927 | if (newvd->vdev_isspare) { |
34dc7c2f | 6928 | spa_spare_activate(newvd); |
12fa0466 | 6929 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); |
9babb374 BB |
6930 | } |
6931 | ||
b128c09f BB |
6932 | oldvdpath = spa_strdup(oldvd->vdev_path); |
6933 | newvdpath = spa_strdup(newvd->vdev_path); | |
6934 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
6935 | |
6936 | /* | |
6937 | * Mark newvd's DTL dirty in this txg. | |
6938 | */ | |
6939 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
6940 | ||
428870ff | 6941 | /* |
9a49d3f3 BB |
6942 | * Schedule the resilver or rebuild to restart in the future. We do |
6943 | * this to ensure that dmu_sync-ed blocks have been stitched into the | |
6944 | * respective datasets. | |
428870ff | 6945 | */ |
9a49d3f3 BB |
6946 | if (rebuild) { |
6947 | newvd->vdev_rebuild_txg = txg; | |
6948 | ||
6949 | vdev_rebuild(tvd); | |
6950 | } else { | |
6951 | newvd->vdev_resilver_txg = txg; | |
6952 | ||
6953 | if (dsl_scan_resilvering(spa_get_dsl(spa)) && | |
6954 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) { | |
6955 | vdev_defer_resilver(newvd); | |
6956 | } else { | |
6957 | dsl_scan_restart_resilver(spa->spa_dsl_pool, | |
6958 | dtl_max_txg); | |
6959 | } | |
6960 | } | |
428870ff | 6961 | |
fb390aaf | 6962 | if (spa->spa_bootfs) |
12fa0466 | 6963 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); |
fb390aaf | 6964 | |
12fa0466 | 6965 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); |
fb390aaf | 6966 | |
428870ff BB |
6967 | /* |
6968 | * Commit the config | |
6969 | */ | |
6970 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 6971 | |
6f1ffb06 | 6972 | spa_history_log_internal(spa, "vdev attach", NULL, |
428870ff | 6973 | "%s vdev=%s %s vdev=%s", |
45d1cae3 BB |
6974 | replacing && newvd_isspare ? "spare in" : |
6975 | replacing ? "replace" : "attach", newvdpath, | |
6976 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
6977 | |
6978 | spa_strfree(oldvdpath); | |
6979 | spa_strfree(newvdpath); | |
6980 | ||
34dc7c2f BB |
6981 | return (0); |
6982 | } | |
6983 | ||
6984 | /* | |
6985 | * Detach a device from a mirror or replacing vdev. | |
d3cc8b15 | 6986 | * |
34dc7c2f BB |
6987 | * If 'replace_done' is specified, only detach if the parent |
6988 | * is a replacing vdev. | |
6989 | */ | |
6990 | int | |
fb5f0bc8 | 6991 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
6992 | { |
6993 | uint64_t txg; | |
fb5f0bc8 | 6994 | int error; |
2a8ba608 | 6995 | vdev_t *rvd __maybe_unused = spa->spa_root_vdev; |
34dc7c2f BB |
6996 | vdev_t *vd, *pvd, *cvd, *tvd; |
6997 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 6998 | uint64_t unspare_guid = 0; |
428870ff | 6999 | char *vdpath; |
1c27024e | 7000 | |
572e2857 BB |
7001 | ASSERT(spa_writeable(spa)); |
7002 | ||
9a49d3f3 | 7003 | txg = spa_vdev_detach_enter(spa, guid); |
34dc7c2f | 7004 | |
b128c09f | 7005 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 7006 | |
d2734cce SD |
7007 | /* |
7008 | * Besides being called directly from the userland through the | |
7009 | * ioctl interface, spa_vdev_detach() can be potentially called | |
7010 | * at the end of spa_vdev_resilver_done(). | |
7011 | * | |
7012 | * In the regular case, when we have a checkpoint this shouldn't | |
7013 | * happen as we never empty the DTLs of a vdev during the scrub | |
7014 | * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() | |
7015 | * should never get here when we have a checkpoint. | |
7016 | * | |
7017 | * That said, even in a case when we checkpoint the pool exactly | |
7018 | * as spa_vdev_resilver_done() calls this function everything | |
7019 | * should be fine as the resilver will return right away. | |
7020 | */ | |
7021 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7022 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7023 | error = (spa_has_checkpoint(spa)) ? | |
7024 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7025 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7026 | } | |
7027 | ||
34dc7c2f BB |
7028 | if (vd == NULL) |
7029 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
7030 | ||
7031 | if (!vd->vdev_ops->vdev_op_leaf) | |
7032 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
7033 | ||
7034 | pvd = vd->vdev_parent; | |
7035 | ||
fb5f0bc8 BB |
7036 | /* |
7037 | * If the parent/child relationship is not as expected, don't do it. | |
7038 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
7039 | * vdev that's replacing B with C. The user's intent in replacing | |
7040 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
7041 | * the replace by detaching C, the expected behavior is to end up | |
7042 | * M(A,B). But suppose that right after deciding to detach C, | |
7043 | * the replacement of B completes. We would have M(A,C), and then | |
7044 | * ask to detach C, which would leave us with just A -- not what | |
7045 | * the user wanted. To prevent this, we make sure that the | |
7046 | * parent/child relationship hasn't changed -- in this example, | |
7047 | * that C's parent is still the replacing vdev R. | |
7048 | */ | |
7049 | if (pvd->vdev_guid != pguid && pguid != 0) | |
7050 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
7051 | ||
34dc7c2f | 7052 | /* |
572e2857 | 7053 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 7054 | */ |
572e2857 BB |
7055 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
7056 | pvd->vdev_ops != &vdev_spare_ops) | |
7057 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
7058 | |
7059 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
7060 | spa_version(spa) >= SPA_VERSION_SPARES); | |
7061 | ||
7062 | /* | |
7063 | * Only mirror, replacing, and spare vdevs support detach. | |
7064 | */ | |
7065 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
7066 | pvd->vdev_ops != &vdev_mirror_ops && | |
7067 | pvd->vdev_ops != &vdev_spare_ops) | |
7068 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
7069 | ||
7070 | /* | |
fb5f0bc8 BB |
7071 | * If this device has the only valid copy of some data, |
7072 | * we cannot safely detach it. | |
34dc7c2f | 7073 | */ |
fb5f0bc8 | 7074 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
7075 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
7076 | ||
fb5f0bc8 | 7077 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 7078 | |
b128c09f BB |
7079 | /* |
7080 | * If we are detaching the second disk from a replacing vdev, then | |
7081 | * check to see if we changed the original vdev's path to have "/old" | |
7082 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
7083 | */ | |
572e2857 BB |
7084 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
7085 | vd->vdev_path != NULL) { | |
7086 | size_t len = strlen(vd->vdev_path); | |
7087 | ||
1c27024e | 7088 | for (int c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
7089 | cvd = pvd->vdev_child[c]; |
7090 | ||
7091 | if (cvd == vd || cvd->vdev_path == NULL) | |
7092 | continue; | |
7093 | ||
7094 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
7095 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
7096 | spa_strfree(cvd->vdev_path); | |
7097 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
7098 | break; | |
7099 | } | |
b128c09f BB |
7100 | } |
7101 | } | |
7102 | ||
34dc7c2f | 7103 | /* |
b2255edc BB |
7104 | * If we are detaching the original disk from a normal spare, then it |
7105 | * implies that the spare should become a real disk, and be removed | |
7106 | * from the active spare list for the pool. dRAID spares on the | |
7107 | * other hand are coupled to the pool and thus should never be removed | |
7108 | * from the spares list. | |
34dc7c2f | 7109 | */ |
b2255edc BB |
7110 | if (pvd->vdev_ops == &vdev_spare_ops && vd->vdev_id == 0) { |
7111 | vdev_t *last_cvd = pvd->vdev_child[pvd->vdev_children - 1]; | |
7112 | ||
7113 | if (last_cvd->vdev_isspare && | |
7114 | last_cvd->vdev_ops != &vdev_draid_spare_ops) { | |
7115 | unspare = B_TRUE; | |
7116 | } | |
7117 | } | |
34dc7c2f BB |
7118 | |
7119 | /* | |
7120 | * Erase the disk labels so the disk can be used for other things. | |
7121 | * This must be done after all other error cases are handled, | |
7122 | * but before we disembowel vd (so we can still do I/O to it). | |
7123 | * But if we can't do it, don't treat the error as fatal -- | |
7124 | * it may be that the unwritability of the disk is the reason | |
7125 | * it's being detached! | |
7126 | */ | |
7127 | error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
7128 | ||
7129 | /* | |
7130 | * Remove vd from its parent and compact the parent's children. | |
7131 | */ | |
7132 | vdev_remove_child(pvd, vd); | |
7133 | vdev_compact_children(pvd); | |
7134 | ||
7135 | /* | |
7136 | * Remember one of the remaining children so we can get tvd below. | |
7137 | */ | |
572e2857 | 7138 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
7139 | |
7140 | /* | |
7141 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
7142 | * do it now, marking the vdev as no longer a spare in the process. |
7143 | * We must do this before vdev_remove_parent(), because that can | |
7144 | * change the GUID if it creates a new toplevel GUID. For a similar | |
7145 | * reason, we must remove the spare now, in the same txg as the detach; | |
7146 | * otherwise someone could attach a new sibling, change the GUID, and | |
7147 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
7148 | */ |
7149 | if (unspare) { | |
7150 | ASSERT(cvd->vdev_isspare); | |
7151 | spa_spare_remove(cvd); | |
7152 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 7153 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 7154 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
7155 | } |
7156 | ||
428870ff BB |
7157 | /* |
7158 | * If the parent mirror/replacing vdev only has one child, | |
7159 | * the parent is no longer needed. Remove it from the tree. | |
7160 | */ | |
572e2857 BB |
7161 | if (pvd->vdev_children == 1) { |
7162 | if (pvd->vdev_ops == &vdev_spare_ops) | |
7163 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 7164 | vdev_remove_parent(cvd); |
572e2857 BB |
7165 | } |
7166 | ||
428870ff BB |
7167 | /* |
7168 | * We don't set tvd until now because the parent we just removed | |
7169 | * may have been the previous top-level vdev. | |
7170 | */ | |
7171 | tvd = cvd->vdev_top; | |
7172 | ASSERT(tvd->vdev_parent == rvd); | |
7173 | ||
7174 | /* | |
7175 | * Reevaluate the parent vdev state. | |
7176 | */ | |
7177 | vdev_propagate_state(cvd); | |
7178 | ||
7179 | /* | |
7180 | * If the 'autoexpand' property is set on the pool then automatically | |
7181 | * try to expand the size of the pool. For example if the device we | |
7182 | * just detached was smaller than the others, it may be possible to | |
7183 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
7184 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
7185 | */ | |
7186 | if (spa->spa_autoexpand) { | |
7187 | vdev_reopen(tvd); | |
7188 | vdev_expand(tvd, txg); | |
7189 | } | |
7190 | ||
7191 | vdev_config_dirty(tvd); | |
7192 | ||
7193 | /* | |
7194 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
7195 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
7196 | * But first make sure we're not on any *other* txg's DTL list, to | |
7197 | * prevent vd from being accessed after it's freed. | |
7198 | */ | |
b6ca6193 | 7199 | vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none"); |
1c27024e | 7200 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff BB |
7201 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
7202 | vd->vdev_detached = B_TRUE; | |
7203 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
7204 | ||
12fa0466 | 7205 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); |
e60e158e | 7206 | spa_notify_waiters(spa); |
428870ff | 7207 | |
572e2857 BB |
7208 | /* hang on to the spa before we release the lock */ |
7209 | spa_open_ref(spa, FTAG); | |
7210 | ||
428870ff BB |
7211 | error = spa_vdev_exit(spa, vd, txg, 0); |
7212 | ||
6f1ffb06 | 7213 | spa_history_log_internal(spa, "detach", NULL, |
428870ff BB |
7214 | "vdev=%s", vdpath); |
7215 | spa_strfree(vdpath); | |
7216 | ||
7217 | /* | |
7218 | * If this was the removal of the original device in a hot spare vdev, | |
7219 | * then we want to go through and remove the device from the hot spare | |
7220 | * list of every other pool. | |
7221 | */ | |
7222 | if (unspare) { | |
572e2857 BB |
7223 | spa_t *altspa = NULL; |
7224 | ||
428870ff | 7225 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
7226 | while ((altspa = spa_next(altspa)) != NULL) { |
7227 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
7228 | altspa == spa) | |
428870ff | 7229 | continue; |
572e2857 BB |
7230 | |
7231 | spa_open_ref(altspa, FTAG); | |
428870ff | 7232 | mutex_exit(&spa_namespace_lock); |
572e2857 | 7233 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 7234 | mutex_enter(&spa_namespace_lock); |
572e2857 | 7235 | spa_close(altspa, FTAG); |
428870ff BB |
7236 | } |
7237 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
7238 | |
7239 | /* search the rest of the vdevs for spares to remove */ | |
7240 | spa_vdev_resilver_done(spa); | |
428870ff BB |
7241 | } |
7242 | ||
572e2857 BB |
7243 | /* all done with the spa; OK to release */ |
7244 | mutex_enter(&spa_namespace_lock); | |
7245 | spa_close(spa, FTAG); | |
7246 | mutex_exit(&spa_namespace_lock); | |
7247 | ||
428870ff BB |
7248 | return (error); |
7249 | } | |
7250 | ||
c10d37dd GW |
7251 | static int |
7252 | spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7253 | list_t *vd_list) | |
619f0976 | 7254 | { |
c10d37dd GW |
7255 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7256 | ||
619f0976 GW |
7257 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); |
7258 | ||
7259 | /* Look up vdev and ensure it's a leaf. */ | |
7260 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7261 | if (vd == NULL || vd->vdev_detached) { | |
7262 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7263 | return (SET_ERROR(ENODEV)); |
7264 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7265 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7266 | return (SET_ERROR(EINVAL)); |
7267 | } else if (!vdev_writeable(vd)) { | |
7268 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7269 | return (SET_ERROR(EROFS)); |
7270 | } | |
7271 | mutex_enter(&vd->vdev_initialize_lock); | |
7272 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7273 | ||
7274 | /* | |
7275 | * When we activate an initialize action we check to see | |
7276 | * if the vdev_initialize_thread is NULL. We do this instead | |
7277 | * of using the vdev_initialize_state since there might be | |
7278 | * a previous initialization process which has completed but | |
7279 | * the thread is not exited. | |
7280 | */ | |
1b939560 | 7281 | if (cmd_type == POOL_INITIALIZE_START && |
619f0976 GW |
7282 | (vd->vdev_initialize_thread != NULL || |
7283 | vd->vdev_top->vdev_removing)) { | |
7284 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7285 | return (SET_ERROR(EBUSY)); |
7286 | } else if (cmd_type == POOL_INITIALIZE_CANCEL && | |
7287 | (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && | |
7288 | vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { | |
7289 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7290 | return (SET_ERROR(ESRCH)); |
7291 | } else if (cmd_type == POOL_INITIALIZE_SUSPEND && | |
7292 | vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { | |
7293 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7294 | return (SET_ERROR(ESRCH)); |
7295 | } | |
7296 | ||
7297 | switch (cmd_type) { | |
1b939560 | 7298 | case POOL_INITIALIZE_START: |
619f0976 GW |
7299 | vdev_initialize(vd); |
7300 | break; | |
7301 | case POOL_INITIALIZE_CANCEL: | |
c10d37dd | 7302 | vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); |
619f0976 GW |
7303 | break; |
7304 | case POOL_INITIALIZE_SUSPEND: | |
c10d37dd | 7305 | vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); |
619f0976 GW |
7306 | break; |
7307 | default: | |
7308 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7309 | } | |
7310 | mutex_exit(&vd->vdev_initialize_lock); | |
7311 | ||
c10d37dd GW |
7312 | return (0); |
7313 | } | |
7314 | ||
7315 | int | |
7316 | spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, | |
7317 | nvlist_t *vdev_errlist) | |
7318 | { | |
7319 | int total_errors = 0; | |
7320 | list_t vd_list; | |
7321 | ||
7322 | list_create(&vd_list, sizeof (vdev_t), | |
7323 | offsetof(vdev_t, vdev_initialize_node)); | |
7324 | ||
7325 | /* | |
7326 | * We hold the namespace lock through the whole function | |
7327 | * to prevent any changes to the pool while we're starting or | |
7328 | * stopping initialization. The config and state locks are held so that | |
7329 | * we can properly assess the vdev state before we commit to | |
7330 | * the initializing operation. | |
7331 | */ | |
7332 | mutex_enter(&spa_namespace_lock); | |
7333 | ||
7334 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7335 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7336 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7337 | ||
7338 | int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, | |
7339 | &vd_list); | |
7340 | if (error != 0) { | |
7341 | char guid_as_str[MAXNAMELEN]; | |
7342 | ||
7343 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7344 | "%llu", (unsigned long long)vdev_guid); | |
7345 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7346 | total_errors++; | |
7347 | } | |
7348 | } | |
7349 | ||
7350 | /* Wait for all initialize threads to stop. */ | |
7351 | vdev_initialize_stop_wait(spa, &vd_list); | |
7352 | ||
619f0976 GW |
7353 | /* Sync out the initializing state */ |
7354 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7355 | mutex_exit(&spa_namespace_lock); | |
7356 | ||
c10d37dd | 7357 | list_destroy(&vd_list); |
619f0976 | 7358 | |
c10d37dd GW |
7359 | return (total_errors); |
7360 | } | |
619f0976 | 7361 | |
1b939560 BB |
7362 | static int |
7363 | spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7364 | uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list) | |
7365 | { | |
7366 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7367 | ||
7368 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
7369 | ||
7370 | /* Look up vdev and ensure it's a leaf. */ | |
7371 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7372 | if (vd == NULL || vd->vdev_detached) { | |
7373 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7374 | return (SET_ERROR(ENODEV)); | |
7375 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7376 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7377 | return (SET_ERROR(EINVAL)); | |
7378 | } else if (!vdev_writeable(vd)) { | |
7379 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7380 | return (SET_ERROR(EROFS)); | |
7381 | } else if (!vd->vdev_has_trim) { | |
7382 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7383 | return (SET_ERROR(EOPNOTSUPP)); | |
7384 | } else if (secure && !vd->vdev_has_securetrim) { | |
7385 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7386 | return (SET_ERROR(EOPNOTSUPP)); | |
7387 | } | |
7388 | mutex_enter(&vd->vdev_trim_lock); | |
7389 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7390 | ||
7391 | /* | |
7392 | * When we activate a TRIM action we check to see if the | |
7393 | * vdev_trim_thread is NULL. We do this instead of using the | |
7394 | * vdev_trim_state since there might be a previous TRIM process | |
7395 | * which has completed but the thread is not exited. | |
7396 | */ | |
7397 | if (cmd_type == POOL_TRIM_START && | |
7398 | (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing)) { | |
7399 | mutex_exit(&vd->vdev_trim_lock); | |
7400 | return (SET_ERROR(EBUSY)); | |
7401 | } else if (cmd_type == POOL_TRIM_CANCEL && | |
7402 | (vd->vdev_trim_state != VDEV_TRIM_ACTIVE && | |
7403 | vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) { | |
7404 | mutex_exit(&vd->vdev_trim_lock); | |
7405 | return (SET_ERROR(ESRCH)); | |
7406 | } else if (cmd_type == POOL_TRIM_SUSPEND && | |
7407 | vd->vdev_trim_state != VDEV_TRIM_ACTIVE) { | |
7408 | mutex_exit(&vd->vdev_trim_lock); | |
7409 | return (SET_ERROR(ESRCH)); | |
7410 | } | |
7411 | ||
7412 | switch (cmd_type) { | |
7413 | case POOL_TRIM_START: | |
7414 | vdev_trim(vd, rate, partial, secure); | |
7415 | break; | |
7416 | case POOL_TRIM_CANCEL: | |
7417 | vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list); | |
7418 | break; | |
7419 | case POOL_TRIM_SUSPEND: | |
7420 | vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list); | |
7421 | break; | |
7422 | default: | |
7423 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7424 | } | |
7425 | mutex_exit(&vd->vdev_trim_lock); | |
7426 | ||
7427 | return (0); | |
7428 | } | |
7429 | ||
7430 | /* | |
7431 | * Initiates a manual TRIM for the requested vdevs. This kicks off individual | |
7432 | * TRIM threads for each child vdev. These threads pass over all of the free | |
7433 | * space in the vdev's metaslabs and issues TRIM commands for that space. | |
7434 | */ | |
7435 | int | |
7436 | spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate, | |
7437 | boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist) | |
7438 | { | |
7439 | int total_errors = 0; | |
7440 | list_t vd_list; | |
7441 | ||
7442 | list_create(&vd_list, sizeof (vdev_t), | |
7443 | offsetof(vdev_t, vdev_trim_node)); | |
7444 | ||
7445 | /* | |
7446 | * We hold the namespace lock through the whole function | |
7447 | * to prevent any changes to the pool while we're starting or | |
7448 | * stopping TRIM. The config and state locks are held so that | |
7449 | * we can properly assess the vdev state before we commit to | |
7450 | * the TRIM operation. | |
7451 | */ | |
7452 | mutex_enter(&spa_namespace_lock); | |
7453 | ||
7454 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7455 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7456 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7457 | ||
7458 | int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type, | |
7459 | rate, partial, secure, &vd_list); | |
7460 | if (error != 0) { | |
7461 | char guid_as_str[MAXNAMELEN]; | |
7462 | ||
7463 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7464 | "%llu", (unsigned long long)vdev_guid); | |
7465 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7466 | total_errors++; | |
7467 | } | |
7468 | } | |
7469 | ||
7470 | /* Wait for all TRIM threads to stop. */ | |
7471 | vdev_trim_stop_wait(spa, &vd_list); | |
7472 | ||
7473 | /* Sync out the TRIM state */ | |
7474 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7475 | mutex_exit(&spa_namespace_lock); | |
7476 | ||
7477 | list_destroy(&vd_list); | |
7478 | ||
7479 | return (total_errors); | |
7480 | } | |
7481 | ||
428870ff BB |
7482 | /* |
7483 | * Split a set of devices from their mirrors, and create a new pool from them. | |
7484 | */ | |
7485 | int | |
7486 | spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, | |
7487 | nvlist_t *props, boolean_t exp) | |
7488 | { | |
7489 | int error = 0; | |
7490 | uint64_t txg, *glist; | |
7491 | spa_t *newspa; | |
7492 | uint_t c, children, lastlog; | |
7493 | nvlist_t **child, *nvl, *tmp; | |
7494 | dmu_tx_t *tx; | |
7495 | char *altroot = NULL; | |
7496 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
7497 | boolean_t activate_slog; | |
7498 | ||
572e2857 | 7499 | ASSERT(spa_writeable(spa)); |
428870ff BB |
7500 | |
7501 | txg = spa_vdev_enter(spa); | |
7502 | ||
d2734cce SD |
7503 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7504 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7505 | error = (spa_has_checkpoint(spa)) ? | |
7506 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7507 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7508 | } | |
7509 | ||
428870ff BB |
7510 | /* clear the log and flush everything up to now */ |
7511 | activate_slog = spa_passivate_log(spa); | |
7512 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
a1d477c2 | 7513 | error = spa_reset_logs(spa); |
428870ff BB |
7514 | txg = spa_vdev_config_enter(spa); |
7515 | ||
7516 | if (activate_slog) | |
7517 | spa_activate_log(spa); | |
7518 | ||
7519 | if (error != 0) | |
7520 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7521 | ||
7522 | /* check new spa name before going any further */ | |
7523 | if (spa_lookup(newname) != NULL) | |
7524 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
7525 | ||
7526 | /* | |
7527 | * scan through all the children to ensure they're all mirrors | |
7528 | */ | |
7529 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
7530 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
7531 | &children) != 0) | |
7532 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7533 | ||
7534 | /* first, check to ensure we've got the right child count */ | |
7535 | rvd = spa->spa_root_vdev; | |
7536 | lastlog = 0; | |
7537 | for (c = 0; c < rvd->vdev_children; c++) { | |
7538 | vdev_t *vd = rvd->vdev_child[c]; | |
7539 | ||
7540 | /* don't count the holes & logs as children */ | |
1b664952 GA |
7541 | if (vd->vdev_islog || (vd->vdev_ops != &vdev_indirect_ops && |
7542 | !vdev_is_concrete(vd))) { | |
428870ff BB |
7543 | if (lastlog == 0) |
7544 | lastlog = c; | |
7545 | continue; | |
7546 | } | |
7547 | ||
7548 | lastlog = 0; | |
7549 | } | |
7550 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
7551 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7552 | ||
7553 | /* next, ensure no spare or cache devices are part of the split */ | |
7554 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
7555 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
7556 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7557 | ||
79c76d5b BB |
7558 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); |
7559 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
428870ff BB |
7560 | |
7561 | /* then, loop over each vdev and validate it */ | |
7562 | for (c = 0; c < children; c++) { | |
7563 | uint64_t is_hole = 0; | |
7564 | ||
7565 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
7566 | &is_hole); | |
7567 | ||
7568 | if (is_hole != 0) { | |
7569 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
7570 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
7571 | continue; | |
7572 | } else { | |
2e528b49 | 7573 | error = SET_ERROR(EINVAL); |
428870ff BB |
7574 | break; |
7575 | } | |
7576 | } | |
7577 | ||
1b664952 GA |
7578 | /* deal with indirect vdevs */ |
7579 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ops == | |
7580 | &vdev_indirect_ops) | |
7581 | continue; | |
7582 | ||
428870ff BB |
7583 | /* which disk is going to be split? */ |
7584 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
7585 | &glist[c]) != 0) { | |
2e528b49 | 7586 | error = SET_ERROR(EINVAL); |
428870ff BB |
7587 | break; |
7588 | } | |
7589 | ||
7590 | /* look it up in the spa */ | |
7591 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
7592 | if (vml[c] == NULL) { | |
2e528b49 | 7593 | error = SET_ERROR(ENODEV); |
428870ff BB |
7594 | break; |
7595 | } | |
7596 | ||
7597 | /* make sure there's nothing stopping the split */ | |
7598 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
7599 | vml[c]->vdev_islog || | |
a1d477c2 | 7600 | !vdev_is_concrete(vml[c]) || |
428870ff BB |
7601 | vml[c]->vdev_isspare || |
7602 | vml[c]->vdev_isl2cache || | |
7603 | !vdev_writeable(vml[c]) || | |
7604 | vml[c]->vdev_children != 0 || | |
7605 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
7606 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
2e528b49 | 7607 | error = SET_ERROR(EINVAL); |
428870ff BB |
7608 | break; |
7609 | } | |
7610 | ||
733b5722 RS |
7611 | if (vdev_dtl_required(vml[c]) || |
7612 | vdev_resilver_needed(vml[c], NULL, NULL)) { | |
2e528b49 | 7613 | error = SET_ERROR(EBUSY); |
428870ff BB |
7614 | break; |
7615 | } | |
7616 | ||
7617 | /* we need certain info from the top level */ | |
65ad5d11 AJ |
7618 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, |
7619 | vml[c]->vdev_top->vdev_ms_array); | |
7620 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
7621 | vml[c]->vdev_top->vdev_ms_shift); | |
7622 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
7623 | vml[c]->vdev_top->vdev_asize); | |
7624 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
7625 | vml[c]->vdev_top->vdev_ashift); | |
e0ab3ab5 JS |
7626 | |
7627 | /* transfer per-vdev ZAPs */ | |
7628 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
7629 | VERIFY0(nvlist_add_uint64(child[c], | |
7630 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
7631 | ||
7632 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
7633 | VERIFY0(nvlist_add_uint64(child[c], | |
7634 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
7635 | vml[c]->vdev_parent->vdev_top_zap)); | |
428870ff BB |
7636 | } |
7637 | ||
7638 | if (error != 0) { | |
7639 | kmem_free(vml, children * sizeof (vdev_t *)); | |
7640 | kmem_free(glist, children * sizeof (uint64_t)); | |
7641 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7642 | } | |
7643 | ||
7644 | /* stop writers from using the disks */ | |
7645 | for (c = 0; c < children; c++) { | |
7646 | if (vml[c] != NULL) | |
7647 | vml[c]->vdev_offline = B_TRUE; | |
7648 | } | |
7649 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
7650 | |
7651 | /* | |
428870ff BB |
7652 | * Temporarily record the splitting vdevs in the spa config. This |
7653 | * will disappear once the config is regenerated. | |
34dc7c2f | 7654 | */ |
65ad5d11 AJ |
7655 | nvl = fnvlist_alloc(); |
7656 | fnvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, glist, children); | |
428870ff | 7657 | kmem_free(glist, children * sizeof (uint64_t)); |
34dc7c2f | 7658 | |
428870ff | 7659 | mutex_enter(&spa->spa_props_lock); |
65ad5d11 | 7660 | fnvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, nvl); |
428870ff BB |
7661 | mutex_exit(&spa->spa_props_lock); |
7662 | spa->spa_config_splitting = nvl; | |
7663 | vdev_config_dirty(spa->spa_root_vdev); | |
7664 | ||
7665 | /* configure and create the new pool */ | |
65ad5d11 AJ |
7666 | fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname); |
7667 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
7668 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE); | |
7669 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa)); | |
7670 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, spa->spa_config_txg); | |
7671 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
7672 | spa_generate_guid(NULL)); | |
e0ab3ab5 | 7673 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); |
428870ff BB |
7674 | (void) nvlist_lookup_string(props, |
7675 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 7676 | |
428870ff BB |
7677 | /* add the new pool to the namespace */ |
7678 | newspa = spa_add(newname, config, altroot); | |
e0ab3ab5 | 7679 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7680 | newspa->spa_config_txg = spa->spa_config_txg; |
7681 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
7682 | ||
7683 | /* release the spa config lock, retaining the namespace lock */ | |
7684 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
7685 | ||
7686 | if (zio_injection_enabled) | |
7687 | zio_handle_panic_injection(spa, FTAG, 1); | |
7688 | ||
7689 | spa_activate(newspa, spa_mode_global); | |
7690 | spa_async_suspend(newspa); | |
7691 | ||
c10d37dd | 7692 | /* |
1b939560 BB |
7693 | * Temporarily stop the initializing and TRIM activity. We set the |
7694 | * state to ACTIVE so that we know to resume initializing or TRIM | |
7695 | * once the split has completed. | |
c10d37dd | 7696 | */ |
1b939560 BB |
7697 | list_t vd_initialize_list; |
7698 | list_create(&vd_initialize_list, sizeof (vdev_t), | |
c10d37dd GW |
7699 | offsetof(vdev_t, vdev_initialize_node)); |
7700 | ||
1b939560 BB |
7701 | list_t vd_trim_list; |
7702 | list_create(&vd_trim_list, sizeof (vdev_t), | |
7703 | offsetof(vdev_t, vdev_trim_node)); | |
7704 | ||
619f0976 | 7705 | for (c = 0; c < children; c++) { |
1b664952 | 7706 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
619f0976 | 7707 | mutex_enter(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7708 | vdev_initialize_stop(vml[c], |
7709 | VDEV_INITIALIZE_ACTIVE, &vd_initialize_list); | |
619f0976 | 7710 | mutex_exit(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7711 | |
7712 | mutex_enter(&vml[c]->vdev_trim_lock); | |
7713 | vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list); | |
7714 | mutex_exit(&vml[c]->vdev_trim_lock); | |
619f0976 GW |
7715 | } |
7716 | } | |
1b939560 BB |
7717 | |
7718 | vdev_initialize_stop_wait(spa, &vd_initialize_list); | |
7719 | vdev_trim_stop_wait(spa, &vd_trim_list); | |
7720 | ||
7721 | list_destroy(&vd_initialize_list); | |
7722 | list_destroy(&vd_trim_list); | |
619f0976 | 7723 | |
6cb8e530 | 7724 | newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; |
8b27e08e | 7725 | newspa->spa_is_splitting = B_TRUE; |
6cb8e530 | 7726 | |
428870ff | 7727 | /* create the new pool from the disks of the original pool */ |
6cb8e530 | 7728 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); |
428870ff BB |
7729 | if (error) |
7730 | goto out; | |
7731 | ||
7732 | /* if that worked, generate a real config for the new pool */ | |
7733 | if (newspa->spa_root_vdev != NULL) { | |
65ad5d11 AJ |
7734 | newspa->spa_config_splitting = fnvlist_alloc(); |
7735 | fnvlist_add_uint64(newspa->spa_config_splitting, | |
7736 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)); | |
428870ff BB |
7737 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, |
7738 | B_TRUE)); | |
9babb374 | 7739 | } |
34dc7c2f | 7740 | |
428870ff BB |
7741 | /* set the props */ |
7742 | if (props != NULL) { | |
7743 | spa_configfile_set(newspa, props, B_FALSE); | |
7744 | error = spa_prop_set(newspa, props); | |
7745 | if (error) | |
7746 | goto out; | |
7747 | } | |
34dc7c2f | 7748 | |
428870ff BB |
7749 | /* flush everything */ |
7750 | txg = spa_vdev_config_enter(newspa); | |
7751 | vdev_config_dirty(newspa->spa_root_vdev); | |
7752 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 7753 | |
428870ff BB |
7754 | if (zio_injection_enabled) |
7755 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 7756 | |
428870ff | 7757 | spa_async_resume(newspa); |
34dc7c2f | 7758 | |
428870ff BB |
7759 | /* finally, update the original pool's config */ |
7760 | txg = spa_vdev_config_enter(spa); | |
7761 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
7762 | error = dmu_tx_assign(tx, TXG_WAIT); | |
7763 | if (error != 0) | |
7764 | dmu_tx_abort(tx); | |
7765 | for (c = 0; c < children; c++) { | |
1b664952 | 7766 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
234234ca RS |
7767 | vdev_t *tvd = vml[c]->vdev_top; |
7768 | ||
7769 | /* | |
7770 | * Need to be sure the detachable VDEV is not | |
7771 | * on any *other* txg's DTL list to prevent it | |
7772 | * from being accessed after it's freed. | |
7773 | */ | |
7774 | for (int t = 0; t < TXG_SIZE; t++) { | |
7775 | (void) txg_list_remove_this( | |
7776 | &tvd->vdev_dtl_list, vml[c], t); | |
7777 | } | |
7778 | ||
428870ff BB |
7779 | vdev_split(vml[c]); |
7780 | if (error == 0) | |
6f1ffb06 MA |
7781 | spa_history_log_internal(spa, "detach", tx, |
7782 | "vdev=%s", vml[c]->vdev_path); | |
e0ab3ab5 | 7783 | |
428870ff | 7784 | vdev_free(vml[c]); |
34dc7c2f | 7785 | } |
34dc7c2f | 7786 | } |
e0ab3ab5 | 7787 | spa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7788 | vdev_config_dirty(spa->spa_root_vdev); |
7789 | spa->spa_config_splitting = NULL; | |
7790 | nvlist_free(nvl); | |
7791 | if (error == 0) | |
7792 | dmu_tx_commit(tx); | |
7793 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
7794 | ||
7795 | if (zio_injection_enabled) | |
7796 | zio_handle_panic_injection(spa, FTAG, 3); | |
7797 | ||
7798 | /* split is complete; log a history record */ | |
6f1ffb06 MA |
7799 | spa_history_log_internal(newspa, "split", NULL, |
7800 | "from pool %s", spa_name(spa)); | |
428870ff | 7801 | |
8b27e08e | 7802 | newspa->spa_is_splitting = B_FALSE; |
428870ff BB |
7803 | kmem_free(vml, children * sizeof (vdev_t *)); |
7804 | ||
7805 | /* if we're not going to mount the filesystems in userland, export */ | |
7806 | if (exp) | |
7807 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
7808 | B_FALSE, B_FALSE); | |
7809 | ||
7810 | return (error); | |
7811 | ||
7812 | out: | |
7813 | spa_unload(newspa); | |
7814 | spa_deactivate(newspa); | |
7815 | spa_remove(newspa); | |
7816 | ||
7817 | txg = spa_vdev_config_enter(spa); | |
7818 | ||
7819 | /* re-online all offlined disks */ | |
7820 | for (c = 0; c < children; c++) { | |
7821 | if (vml[c] != NULL) | |
7822 | vml[c]->vdev_offline = B_FALSE; | |
7823 | } | |
619f0976 | 7824 | |
1b939560 | 7825 | /* restart initializing or trimming disks as necessary */ |
619f0976 | 7826 | spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); |
1b939560 BB |
7827 | spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); |
7828 | spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); | |
619f0976 | 7829 | |
428870ff BB |
7830 | vdev_reopen(spa->spa_root_vdev); |
7831 | ||
7832 | nvlist_free(spa->spa_config_splitting); | |
7833 | spa->spa_config_splitting = NULL; | |
7834 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 7835 | |
428870ff | 7836 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
7837 | return (error); |
7838 | } | |
7839 | ||
34dc7c2f BB |
7840 | /* |
7841 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
d3cc8b15 | 7842 | * currently spared, so we can detach it. |
34dc7c2f BB |
7843 | */ |
7844 | static vdev_t * | |
7845 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
7846 | { | |
7847 | vdev_t *newvd, *oldvd; | |
34dc7c2f | 7848 | |
1c27024e | 7849 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
7850 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
7851 | if (oldvd != NULL) | |
7852 | return (oldvd); | |
7853 | } | |
7854 | ||
7855 | /* | |
572e2857 BB |
7856 | * Check for a completed replacement. We always consider the first |
7857 | * vdev in the list to be the oldest vdev, and the last one to be | |
7858 | * the newest (see spa_vdev_attach() for how that works). In | |
7859 | * the case where the newest vdev is faulted, we will not automatically | |
7860 | * remove it after a resilver completes. This is OK as it will require | |
7861 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 7862 | */ |
572e2857 BB |
7863 | if (vd->vdev_ops == &vdev_replacing_ops) { |
7864 | ASSERT(vd->vdev_children > 1); | |
7865 | ||
7866 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 7867 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 7868 | |
fb5f0bc8 | 7869 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7870 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 7871 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7872 | return (oldvd); |
34dc7c2f BB |
7873 | } |
7874 | ||
7875 | /* | |
7876 | * Check for a completed resilver with the 'unspare' flag set. | |
f65fbee1 | 7877 | * Also potentially update faulted state. |
34dc7c2f | 7878 | */ |
572e2857 BB |
7879 | if (vd->vdev_ops == &vdev_spare_ops) { |
7880 | vdev_t *first = vd->vdev_child[0]; | |
7881 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
7882 | ||
7883 | if (last->vdev_unspare) { | |
7884 | oldvd = first; | |
7885 | newvd = last; | |
7886 | } else if (first->vdev_unspare) { | |
7887 | oldvd = last; | |
7888 | newvd = first; | |
7889 | } else { | |
7890 | oldvd = NULL; | |
7891 | } | |
34dc7c2f | 7892 | |
572e2857 | 7893 | if (oldvd != NULL && |
fb5f0bc8 | 7894 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7895 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 7896 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7897 | return (oldvd); |
572e2857 | 7898 | |
f65fbee1 JJ |
7899 | vdev_propagate_state(vd); |
7900 | ||
572e2857 BB |
7901 | /* |
7902 | * If there are more than two spares attached to a disk, | |
7903 | * and those spares are not required, then we want to | |
7904 | * attempt to free them up now so that they can be used | |
7905 | * by other pools. Once we're back down to a single | |
7906 | * disk+spare, we stop removing them. | |
7907 | */ | |
7908 | if (vd->vdev_children > 2) { | |
7909 | newvd = vd->vdev_child[1]; | |
7910 | ||
7911 | if (newvd->vdev_isspare && last->vdev_isspare && | |
7912 | vdev_dtl_empty(last, DTL_MISSING) && | |
7913 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
7914 | !vdev_dtl_required(newvd)) | |
7915 | return (newvd); | |
34dc7c2f | 7916 | } |
34dc7c2f BB |
7917 | } |
7918 | ||
7919 | return (NULL); | |
7920 | } | |
7921 | ||
7922 | static void | |
7923 | spa_vdev_resilver_done(spa_t *spa) | |
7924 | { | |
fb5f0bc8 BB |
7925 | vdev_t *vd, *pvd, *ppvd; |
7926 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 7927 | |
fb5f0bc8 | 7928 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7929 | |
7930 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
7931 | pvd = vd->vdev_parent; |
7932 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 7933 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
7934 | pguid = pvd->vdev_guid; |
7935 | ppguid = ppvd->vdev_guid; | |
7936 | sguid = 0; | |
34dc7c2f BB |
7937 | /* |
7938 | * If we have just finished replacing a hot spared device, then | |
7939 | * we need to detach the parent's first child (the original hot | |
7940 | * spare) as well. | |
7941 | */ | |
572e2857 BB |
7942 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
7943 | ppvd->vdev_children == 2) { | |
34dc7c2f | 7944 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 7945 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 7946 | } |
5d1f7fb6 GW |
7947 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); |
7948 | ||
fb5f0bc8 BB |
7949 | spa_config_exit(spa, SCL_ALL, FTAG); |
7950 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 7951 | return; |
fb5f0bc8 | 7952 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 7953 | return; |
fb5f0bc8 | 7954 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7955 | } |
7956 | ||
fb5f0bc8 | 7957 | spa_config_exit(spa, SCL_ALL, FTAG); |
9a49d3f3 BB |
7958 | |
7959 | /* | |
7960 | * If a detach was not performed above replace waiters will not have | |
7961 | * been notified. In which case we must do so now. | |
7962 | */ | |
7963 | spa_notify_waiters(spa); | |
34dc7c2f BB |
7964 | } |
7965 | ||
7966 | /* | |
428870ff | 7967 | * Update the stored path or FRU for this vdev. |
34dc7c2f | 7968 | */ |
65c7cc49 | 7969 | static int |
9babb374 BB |
7970 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
7971 | boolean_t ispath) | |
34dc7c2f | 7972 | { |
b128c09f | 7973 | vdev_t *vd; |
428870ff | 7974 | boolean_t sync = B_FALSE; |
34dc7c2f | 7975 | |
572e2857 BB |
7976 | ASSERT(spa_writeable(spa)); |
7977 | ||
428870ff | 7978 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 7979 | |
9babb374 | 7980 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 7981 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
7982 | |
7983 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 7984 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 7985 | |
9babb374 | 7986 | if (ispath) { |
428870ff BB |
7987 | if (strcmp(value, vd->vdev_path) != 0) { |
7988 | spa_strfree(vd->vdev_path); | |
7989 | vd->vdev_path = spa_strdup(value); | |
7990 | sync = B_TRUE; | |
7991 | } | |
9babb374 | 7992 | } else { |
428870ff BB |
7993 | if (vd->vdev_fru == NULL) { |
7994 | vd->vdev_fru = spa_strdup(value); | |
7995 | sync = B_TRUE; | |
7996 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 7997 | spa_strfree(vd->vdev_fru); |
428870ff BB |
7998 | vd->vdev_fru = spa_strdup(value); |
7999 | sync = B_TRUE; | |
8000 | } | |
9babb374 | 8001 | } |
34dc7c2f | 8002 | |
428870ff | 8003 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
8004 | } |
8005 | ||
9babb374 BB |
8006 | int |
8007 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
8008 | { | |
8009 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
8010 | } | |
8011 | ||
8012 | int | |
8013 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
8014 | { | |
8015 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
8016 | } | |
8017 | ||
34dc7c2f BB |
8018 | /* |
8019 | * ========================================================================== | |
428870ff | 8020 | * SPA Scanning |
34dc7c2f BB |
8021 | * ========================================================================== |
8022 | */ | |
0ea05c64 AP |
8023 | int |
8024 | spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) | |
8025 | { | |
8026 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
8027 | ||
8028 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
8029 | return (SET_ERROR(EBUSY)); | |
8030 | ||
8031 | return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); | |
8032 | } | |
34dc7c2f | 8033 | |
34dc7c2f | 8034 | int |
428870ff BB |
8035 | spa_scan_stop(spa_t *spa) |
8036 | { | |
8037 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
8038 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
2e528b49 | 8039 | return (SET_ERROR(EBUSY)); |
428870ff BB |
8040 | return (dsl_scan_cancel(spa->spa_dsl_pool)); |
8041 | } | |
8042 | ||
8043 | int | |
8044 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 8045 | { |
b128c09f | 8046 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 8047 | |
428870ff | 8048 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
2e528b49 | 8049 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 8050 | |
fa241660 TC |
8051 | if (func == POOL_SCAN_RESILVER && |
8052 | !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
8053 | return (SET_ERROR(ENOTSUP)); | |
8054 | ||
34dc7c2f | 8055 | /* |
b128c09f BB |
8056 | * If a resilver was requested, but there is no DTL on a |
8057 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 8058 | */ |
428870ff | 8059 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
8060 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
8061 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
8062 | return (0); |
8063 | } | |
8064 | ||
428870ff | 8065 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
8066 | } |
8067 | ||
8068 | /* | |
8069 | * ========================================================================== | |
8070 | * SPA async task processing | |
8071 | * ========================================================================== | |
8072 | */ | |
8073 | ||
8074 | static void | |
8075 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
8076 | { | |
b128c09f | 8077 | if (vd->vdev_remove_wanted) { |
428870ff BB |
8078 | vd->vdev_remove_wanted = B_FALSE; |
8079 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 8080 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
8081 | |
8082 | /* | |
8083 | * We want to clear the stats, but we don't want to do a full | |
8084 | * vdev_clear() as that will cause us to throw away | |
8085 | * degraded/faulted state as well as attempt to reopen the | |
8086 | * device, all of which is a waste. | |
8087 | */ | |
8088 | vd->vdev_stat.vs_read_errors = 0; | |
8089 | vd->vdev_stat.vs_write_errors = 0; | |
8090 | vd->vdev_stat.vs_checksum_errors = 0; | |
8091 | ||
b128c09f | 8092 | vdev_state_dirty(vd->vdev_top); |
0aacde2e RM |
8093 | |
8094 | /* Tell userspace that the vdev is gone. */ | |
8095 | zfs_post_remove(spa, vd); | |
b128c09f | 8096 | } |
34dc7c2f | 8097 | |
1c27024e | 8098 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
8099 | spa_async_remove(spa, vd->vdev_child[c]); |
8100 | } | |
8101 | ||
8102 | static void | |
8103 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
8104 | { | |
8105 | if (vd->vdev_probe_wanted) { | |
428870ff | 8106 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 8107 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 8108 | } |
b128c09f | 8109 | |
1c27024e | 8110 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 8111 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
8112 | } |
8113 | ||
9babb374 BB |
8114 | static void |
8115 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
8116 | { | |
9babb374 BB |
8117 | if (!spa->spa_autoexpand) |
8118 | return; | |
8119 | ||
1c27024e | 8120 | for (int c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
8121 | vdev_t *cvd = vd->vdev_child[c]; |
8122 | spa_async_autoexpand(spa, cvd); | |
8123 | } | |
8124 | ||
8125 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
8126 | return; | |
8127 | ||
12fa0466 | 8128 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND); |
9babb374 BB |
8129 | } |
8130 | ||
34dc7c2f | 8131 | static void |
c25b8f99 | 8132 | spa_async_thread(void *arg) |
34dc7c2f | 8133 | { |
c25b8f99 | 8134 | spa_t *spa = (spa_t *)arg; |
80a91e74 | 8135 | dsl_pool_t *dp = spa->spa_dsl_pool; |
867959b5 | 8136 | int tasks; |
34dc7c2f BB |
8137 | |
8138 | ASSERT(spa->spa_sync_on); | |
8139 | ||
8140 | mutex_enter(&spa->spa_async_lock); | |
8141 | tasks = spa->spa_async_tasks; | |
8142 | spa->spa_async_tasks = 0; | |
8143 | mutex_exit(&spa->spa_async_lock); | |
8144 | ||
8145 | /* | |
8146 | * See if the config needs to be updated. | |
8147 | */ | |
8148 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 8149 | uint64_t old_space, new_space; |
9babb374 | 8150 | |
34dc7c2f | 8151 | mutex_enter(&spa_namespace_lock); |
428870ff | 8152 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
8153 | old_space += metaslab_class_get_space(spa_special_class(spa)); |
8154 | old_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
aa755b35 MA |
8155 | old_space += metaslab_class_get_space( |
8156 | spa_embedded_log_class(spa)); | |
cc99f275 | 8157 | |
34dc7c2f | 8158 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
cc99f275 | 8159 | |
428870ff | 8160 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
8161 | new_space += metaslab_class_get_space(spa_special_class(spa)); |
8162 | new_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
aa755b35 MA |
8163 | new_space += metaslab_class_get_space( |
8164 | spa_embedded_log_class(spa)); | |
34dc7c2f | 8165 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
8166 | |
8167 | /* | |
8168 | * If the pool grew as a result of the config update, | |
8169 | * then log an internal history event. | |
8170 | */ | |
428870ff | 8171 | if (new_space != old_space) { |
6f1ffb06 | 8172 | spa_history_log_internal(spa, "vdev online", NULL, |
45d1cae3 | 8173 | "pool '%s' size: %llu(+%llu)", |
74756182 MM |
8174 | spa_name(spa), (u_longlong_t)new_space, |
8175 | (u_longlong_t)(new_space - old_space)); | |
9babb374 | 8176 | } |
34dc7c2f BB |
8177 | } |
8178 | ||
8179 | /* | |
8180 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 8181 | */ |
b128c09f | 8182 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 8183 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 8184 | spa_async_remove(spa, spa->spa_root_vdev); |
867959b5 | 8185 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 8186 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
867959b5 | 8187 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
8188 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
8189 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
8190 | } |
8191 | ||
9babb374 BB |
8192 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
8193 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8194 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
8195 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8196 | } | |
8197 | ||
34dc7c2f | 8198 | /* |
b128c09f | 8199 | * See if any devices need to be probed. |
34dc7c2f | 8200 | */ |
b128c09f | 8201 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 8202 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
8203 | spa_async_probe(spa, spa->spa_root_vdev); |
8204 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
8205 | } | |
34dc7c2f BB |
8206 | |
8207 | /* | |
b128c09f | 8208 | * If any devices are done replacing, detach them. |
34dc7c2f | 8209 | */ |
b2255edc BB |
8210 | if (tasks & SPA_ASYNC_RESILVER_DONE || |
8211 | tasks & SPA_ASYNC_REBUILD_DONE) { | |
b128c09f | 8212 | spa_vdev_resilver_done(spa); |
9a49d3f3 BB |
8213 | } |
8214 | ||
34dc7c2f BB |
8215 | /* |
8216 | * Kick off a resilver. | |
8217 | */ | |
80a91e74 | 8218 | if (tasks & SPA_ASYNC_RESILVER && |
9a49d3f3 | 8219 | !vdev_rebuild_active(spa->spa_root_vdev) && |
80a91e74 TC |
8220 | (!dsl_scan_resilvering(dp) || |
8221 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) | |
3c819a2c | 8222 | dsl_scan_restart_resilver(dp, 0); |
34dc7c2f | 8223 | |
619f0976 GW |
8224 | if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { |
8225 | mutex_enter(&spa_namespace_lock); | |
8226 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8227 | vdev_initialize_restart(spa->spa_root_vdev); | |
8228 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8229 | mutex_exit(&spa_namespace_lock); | |
8230 | } | |
8231 | ||
1b939560 BB |
8232 | if (tasks & SPA_ASYNC_TRIM_RESTART) { |
8233 | mutex_enter(&spa_namespace_lock); | |
8234 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8235 | vdev_trim_restart(spa->spa_root_vdev); | |
8236 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8237 | mutex_exit(&spa_namespace_lock); | |
8238 | } | |
8239 | ||
8240 | if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) { | |
8241 | mutex_enter(&spa_namespace_lock); | |
8242 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8243 | vdev_autotrim_restart(spa); | |
8244 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8245 | mutex_exit(&spa_namespace_lock); | |
8246 | } | |
8247 | ||
b7654bd7 GA |
8248 | /* |
8249 | * Kick off L2 cache whole device TRIM. | |
8250 | */ | |
8251 | if (tasks & SPA_ASYNC_L2CACHE_TRIM) { | |
8252 | mutex_enter(&spa_namespace_lock); | |
8253 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8254 | vdev_trim_l2arc(spa); | |
8255 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8256 | mutex_exit(&spa_namespace_lock); | |
8257 | } | |
8258 | ||
77f6826b GA |
8259 | /* |
8260 | * Kick off L2 cache rebuilding. | |
8261 | */ | |
8262 | if (tasks & SPA_ASYNC_L2CACHE_REBUILD) { | |
8263 | mutex_enter(&spa_namespace_lock); | |
8264 | spa_config_enter(spa, SCL_L2ARC, FTAG, RW_READER); | |
8265 | l2arc_spa_rebuild_start(spa); | |
8266 | spa_config_exit(spa, SCL_L2ARC, FTAG); | |
8267 | mutex_exit(&spa_namespace_lock); | |
8268 | } | |
8269 | ||
34dc7c2f BB |
8270 | /* |
8271 | * Let the world know that we're done. | |
8272 | */ | |
8273 | mutex_enter(&spa->spa_async_lock); | |
8274 | spa->spa_async_thread = NULL; | |
8275 | cv_broadcast(&spa->spa_async_cv); | |
8276 | mutex_exit(&spa->spa_async_lock); | |
8277 | thread_exit(); | |
8278 | } | |
8279 | ||
8280 | void | |
8281 | spa_async_suspend(spa_t *spa) | |
8282 | { | |
8283 | mutex_enter(&spa->spa_async_lock); | |
8284 | spa->spa_async_suspended++; | |
9d5b5245 | 8285 | while (spa->spa_async_thread != NULL) |
34dc7c2f BB |
8286 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); |
8287 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 MA |
8288 | |
8289 | spa_vdev_remove_suspend(spa); | |
9d5b5245 SD |
8290 | |
8291 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a SD |
8292 | if (condense_thread != NULL) |
8293 | zthr_cancel(condense_thread); | |
d2734cce SD |
8294 | |
8295 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a SD |
8296 | if (discard_thread != NULL) |
8297 | zthr_cancel(discard_thread); | |
37f03da8 SH |
8298 | |
8299 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8300 | if (ll_delete_thread != NULL) | |
8301 | zthr_cancel(ll_delete_thread); | |
8302 | ||
8303 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8304 | if (ll_condense_thread != NULL) | |
8305 | zthr_cancel(ll_condense_thread); | |
34dc7c2f BB |
8306 | } |
8307 | ||
8308 | void | |
8309 | spa_async_resume(spa_t *spa) | |
8310 | { | |
8311 | mutex_enter(&spa->spa_async_lock); | |
8312 | ASSERT(spa->spa_async_suspended != 0); | |
8313 | spa->spa_async_suspended--; | |
8314 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 | 8315 | spa_restart_removal(spa); |
9d5b5245 SD |
8316 | |
8317 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a | 8318 | if (condense_thread != NULL) |
9d5b5245 | 8319 | zthr_resume(condense_thread); |
d2734cce SD |
8320 | |
8321 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a | 8322 | if (discard_thread != NULL) |
d2734cce | 8323 | zthr_resume(discard_thread); |
37f03da8 SH |
8324 | |
8325 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8326 | if (ll_delete_thread != NULL) | |
8327 | zthr_resume(ll_delete_thread); | |
8328 | ||
8329 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8330 | if (ll_condense_thread != NULL) | |
8331 | zthr_resume(ll_condense_thread); | |
34dc7c2f BB |
8332 | } |
8333 | ||
e6cfd633 WA |
8334 | static boolean_t |
8335 | spa_async_tasks_pending(spa_t *spa) | |
8336 | { | |
8337 | uint_t non_config_tasks; | |
8338 | uint_t config_task; | |
8339 | boolean_t config_task_suspended; | |
8340 | ||
8341 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
8342 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
8343 | if (spa->spa_ccw_fail_time == 0) { | |
8344 | config_task_suspended = B_FALSE; | |
8345 | } else { | |
8346 | config_task_suspended = | |
8347 | (gethrtime() - spa->spa_ccw_fail_time) < | |
05852b34 | 8348 | ((hrtime_t)zfs_ccw_retry_interval * NANOSEC); |
e6cfd633 WA |
8349 | } |
8350 | ||
8351 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
8352 | } | |
8353 | ||
34dc7c2f BB |
8354 | static void |
8355 | spa_async_dispatch(spa_t *spa) | |
8356 | { | |
8357 | mutex_enter(&spa->spa_async_lock); | |
e6cfd633 WA |
8358 | if (spa_async_tasks_pending(spa) && |
8359 | !spa->spa_async_suspended && | |
da92d5cb | 8360 | spa->spa_async_thread == NULL) |
34dc7c2f BB |
8361 | spa->spa_async_thread = thread_create(NULL, 0, |
8362 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
8363 | mutex_exit(&spa->spa_async_lock); | |
8364 | } | |
8365 | ||
8366 | void | |
8367 | spa_async_request(spa_t *spa, int task) | |
8368 | { | |
428870ff | 8369 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
8370 | mutex_enter(&spa->spa_async_lock); |
8371 | spa->spa_async_tasks |= task; | |
8372 | mutex_exit(&spa->spa_async_lock); | |
8373 | } | |
8374 | ||
3c819a2c JP |
8375 | int |
8376 | spa_async_tasks(spa_t *spa) | |
8377 | { | |
8378 | return (spa->spa_async_tasks); | |
8379 | } | |
8380 | ||
34dc7c2f BB |
8381 | /* |
8382 | * ========================================================================== | |
8383 | * SPA syncing routines | |
8384 | * ========================================================================== | |
8385 | */ | |
8386 | ||
37f03da8 | 8387 | |
428870ff | 8388 | static int |
37f03da8 SH |
8389 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, |
8390 | dmu_tx_t *tx) | |
34dc7c2f | 8391 | { |
428870ff | 8392 | bpobj_t *bpo = arg; |
37f03da8 | 8393 | bpobj_enqueue(bpo, bp, bp_freed, tx); |
428870ff BB |
8394 | return (0); |
8395 | } | |
34dc7c2f | 8396 | |
37f03da8 SH |
8397 | int |
8398 | bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8399 | { | |
8400 | return (bpobj_enqueue_cb(arg, bp, B_FALSE, tx)); | |
8401 | } | |
8402 | ||
8403 | int | |
8404 | bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8405 | { | |
8406 | return (bpobj_enqueue_cb(arg, bp, B_TRUE, tx)); | |
8407 | } | |
8408 | ||
428870ff BB |
8409 | static int |
8410 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8411 | { | |
9cdf7b1f | 8412 | zio_t *pio = arg; |
34dc7c2f | 8413 | |
9cdf7b1f MA |
8414 | zio_nowait(zio_free_sync(pio, pio->io_spa, dmu_tx_get_txg(tx), bp, |
8415 | pio->io_flags)); | |
428870ff | 8416 | return (0); |
34dc7c2f BB |
8417 | } |
8418 | ||
37f03da8 SH |
8419 | static int |
8420 | bpobj_spa_free_sync_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
8421 | dmu_tx_t *tx) | |
8422 | { | |
8423 | ASSERT(!bp_freed); | |
8424 | return (spa_free_sync_cb(arg, bp, tx)); | |
8425 | } | |
8426 | ||
e8b96c60 MA |
8427 | /* |
8428 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8429 | * amount of time spent syncing frees. | |
8430 | */ | |
8431 | static void | |
8432 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
8433 | { | |
8434 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
8435 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
8436 | VERIFY(zio_wait(zio) == 0); | |
8437 | } | |
8438 | ||
8439 | /* | |
8440 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8441 | * amount of time spent syncing deferred frees. | |
8442 | */ | |
8443 | static void | |
8444 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
8445 | { | |
8dc2197b SD |
8446 | if (spa_sync_pass(spa) != 1) |
8447 | return; | |
8448 | ||
93e28d66 SD |
8449 | /* |
8450 | * Note: | |
8451 | * If the log space map feature is active, we stop deferring | |
8452 | * frees to the next TXG and therefore running this function | |
8453 | * would be considered a no-op as spa_deferred_bpobj should | |
8454 | * not have any entries. | |
8455 | * | |
8456 | * That said we run this function anyway (instead of returning | |
8457 | * immediately) for the edge-case scenario where we just | |
8458 | * activated the log space map feature in this TXG but we have | |
8459 | * deferred frees from the previous TXG. | |
8460 | */ | |
e8b96c60 MA |
8461 | zio_t *zio = zio_root(spa, NULL, NULL, 0); |
8462 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
37f03da8 | 8463 | bpobj_spa_free_sync_cb, zio, tx), ==, 0); |
e8b96c60 MA |
8464 | VERIFY0(zio_wait(zio)); |
8465 | } | |
8466 | ||
34dc7c2f BB |
8467 | static void |
8468 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
8469 | { | |
8470 | char *packed = NULL; | |
b128c09f | 8471 | size_t bufsize; |
34dc7c2f BB |
8472 | size_t nvsize = 0; |
8473 | dmu_buf_t *db; | |
8474 | ||
8475 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
8476 | ||
b128c09f BB |
8477 | /* |
8478 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
b0bc7a84 | 8479 | * information. This avoids the dmu_buf_will_dirty() path and |
b128c09f BB |
8480 | * saves us a pre-read to get data we don't actually care about. |
8481 | */ | |
9ae529ec | 8482 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
79c76d5b | 8483 | packed = vmem_alloc(bufsize, KM_SLEEP); |
34dc7c2f BB |
8484 | |
8485 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
79c76d5b | 8486 | KM_SLEEP) == 0); |
b128c09f | 8487 | bzero(packed + nvsize, bufsize - nvsize); |
34dc7c2f | 8488 | |
b128c09f | 8489 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 8490 | |
00b46022 | 8491 | vmem_free(packed, bufsize); |
34dc7c2f BB |
8492 | |
8493 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
8494 | dmu_buf_will_dirty(db, tx); | |
8495 | *(uint64_t *)db->db_data = nvsize; | |
8496 | dmu_buf_rele(db, FTAG); | |
8497 | } | |
8498 | ||
8499 | static void | |
8500 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
8501 | const char *config, const char *entry) | |
8502 | { | |
8503 | nvlist_t *nvroot; | |
8504 | nvlist_t **list; | |
8505 | int i; | |
8506 | ||
8507 | if (!sav->sav_sync) | |
8508 | return; | |
8509 | ||
8510 | /* | |
8511 | * Update the MOS nvlist describing the list of available devices. | |
8512 | * spa_validate_aux() will have already made sure this nvlist is | |
8513 | * valid and the vdevs are labeled appropriately. | |
8514 | */ | |
8515 | if (sav->sav_object == 0) { | |
8516 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
8517 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
8518 | sizeof (uint64_t), tx); | |
8519 | VERIFY(zap_update(spa->spa_meta_objset, | |
8520 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
8521 | &sav->sav_object, tx) == 0); | |
8522 | } | |
8523 | ||
65ad5d11 | 8524 | nvroot = fnvlist_alloc(); |
34dc7c2f | 8525 | if (sav->sav_count == 0) { |
795075e6 PD |
8526 | fnvlist_add_nvlist_array(nvroot, config, |
8527 | (const nvlist_t * const *)NULL, 0); | |
34dc7c2f | 8528 | } else { |
79c76d5b | 8529 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); |
34dc7c2f BB |
8530 | for (i = 0; i < sav->sav_count; i++) |
8531 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 8532 | B_FALSE, VDEV_CONFIG_L2CACHE); |
795075e6 PD |
8533 | fnvlist_add_nvlist_array(nvroot, config, |
8534 | (const nvlist_t * const *)list, sav->sav_count); | |
34dc7c2f BB |
8535 | for (i = 0; i < sav->sav_count; i++) |
8536 | nvlist_free(list[i]); | |
8537 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
8538 | } | |
8539 | ||
8540 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
8541 | nvlist_free(nvroot); | |
8542 | ||
8543 | sav->sav_sync = B_FALSE; | |
8544 | } | |
8545 | ||
e0ab3ab5 JS |
8546 | /* |
8547 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
8548 | * The all-vdev ZAP must be empty. | |
8549 | */ | |
8550 | static void | |
8551 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
8552 | { | |
8553 | spa_t *spa = vd->vdev_spa; | |
e0ab3ab5 JS |
8554 | |
8555 | if (vd->vdev_top_zap != 0) { | |
8556 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8557 | vd->vdev_top_zap, tx)); | |
8558 | } | |
8559 | if (vd->vdev_leaf_zap != 0) { | |
8560 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8561 | vd->vdev_leaf_zap, tx)); | |
8562 | } | |
1c27024e | 8563 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
8564 | spa_avz_build(vd->vdev_child[i], avz, tx); |
8565 | } | |
8566 | } | |
8567 | ||
34dc7c2f BB |
8568 | static void |
8569 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
8570 | { | |
8571 | nvlist_t *config; | |
8572 | ||
e0ab3ab5 JS |
8573 | /* |
8574 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
8575 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
8576 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
8577 | * need to rebuild the AVZ although the config may not be dirty. | |
8578 | */ | |
8579 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
8580 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
34dc7c2f BB |
8581 | return; |
8582 | ||
b128c09f BB |
8583 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
8584 | ||
e0ab3ab5 | 8585 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || |
38640550 | 8586 | spa->spa_avz_action == AVZ_ACTION_INITIALIZE || |
e0ab3ab5 JS |
8587 | spa->spa_all_vdev_zaps != 0); |
8588 | ||
8589 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
e0ab3ab5 JS |
8590 | /* Make and build the new AVZ */ |
8591 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
8592 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
8593 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
8594 | ||
8595 | /* Diff old AVZ with new one */ | |
1c27024e DB |
8596 | zap_cursor_t zc; |
8597 | zap_attribute_t za; | |
8598 | ||
e0ab3ab5 JS |
8599 | for (zap_cursor_init(&zc, spa->spa_meta_objset, |
8600 | spa->spa_all_vdev_zaps); | |
8601 | zap_cursor_retrieve(&zc, &za) == 0; | |
8602 | zap_cursor_advance(&zc)) { | |
8603 | uint64_t vdzap = za.za_first_integer; | |
8604 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
8605 | vdzap) == ENOENT) { | |
8606 | /* | |
8607 | * ZAP is listed in old AVZ but not in new one; | |
8608 | * destroy it | |
8609 | */ | |
8610 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
8611 | tx)); | |
8612 | } | |
8613 | } | |
8614 | ||
8615 | zap_cursor_fini(&zc); | |
8616 | ||
8617 | /* Destroy the old AVZ */ | |
8618 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8619 | spa->spa_all_vdev_zaps, tx)); | |
8620 | ||
8621 | /* Replace the old AVZ in the dir obj with the new one */ | |
8622 | VERIFY0(zap_update(spa->spa_meta_objset, | |
8623 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
8624 | sizeof (new_avz), 1, &new_avz, tx)); | |
8625 | ||
8626 | spa->spa_all_vdev_zaps = new_avz; | |
8627 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
8628 | zap_cursor_t zc; | |
8629 | zap_attribute_t za; | |
8630 | ||
8631 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
8632 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
8633 | spa->spa_all_vdev_zaps); | |
8634 | zap_cursor_retrieve(&zc, &za) == 0; | |
8635 | zap_cursor_advance(&zc)) { | |
8636 | uint64_t zap = za.za_first_integer; | |
8637 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
8638 | } | |
8639 | ||
8640 | zap_cursor_fini(&zc); | |
8641 | ||
8642 | /* Destroy and unlink the AVZ itself */ | |
8643 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8644 | spa->spa_all_vdev_zaps, tx)); | |
8645 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
8646 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
8647 | spa->spa_all_vdev_zaps = 0; | |
8648 | } | |
8649 | ||
8650 | if (spa->spa_all_vdev_zaps == 0) { | |
8651 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
8652 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
8653 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
8654 | } | |
8655 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
8656 | ||
8657 | /* Create ZAPs for vdevs that don't have them. */ | |
8658 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
8659 | ||
b128c09f BB |
8660 | config = spa_config_generate(spa, spa->spa_root_vdev, |
8661 | dmu_tx_get_txg(tx), B_FALSE); | |
8662 | ||
ea0b2538 GW |
8663 | /* |
8664 | * If we're upgrading the spa version then make sure that | |
8665 | * the config object gets updated with the correct version. | |
8666 | */ | |
8667 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
8668 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
8669 | spa->spa_uberblock.ub_version); | |
8670 | ||
b128c09f | 8671 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f | 8672 | |
8a5fc748 | 8673 | nvlist_free(spa->spa_config_syncing); |
34dc7c2f BB |
8674 | spa->spa_config_syncing = config; |
8675 | ||
8676 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
8677 | } | |
8678 | ||
9ae529ec | 8679 | static void |
13fe0198 | 8680 | spa_sync_version(void *arg, dmu_tx_t *tx) |
9ae529ec | 8681 | { |
13fe0198 MA |
8682 | uint64_t *versionp = arg; |
8683 | uint64_t version = *versionp; | |
8684 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
9ae529ec CS |
8685 | |
8686 | /* | |
8687 | * Setting the version is special cased when first creating the pool. | |
8688 | */ | |
8689 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
8690 | ||
8dca0a9a | 8691 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
8692 | ASSERT(version >= spa_version(spa)); |
8693 | ||
8694 | spa->spa_uberblock.ub_version = version; | |
8695 | vdev_config_dirty(spa->spa_root_vdev); | |
74756182 MM |
8696 | spa_history_log_internal(spa, "set", tx, "version=%lld", |
8697 | (longlong_t)version); | |
9ae529ec CS |
8698 | } |
8699 | ||
34dc7c2f BB |
8700 | /* |
8701 | * Set zpool properties. | |
8702 | */ | |
8703 | static void | |
13fe0198 | 8704 | spa_sync_props(void *arg, dmu_tx_t *tx) |
34dc7c2f | 8705 | { |
13fe0198 MA |
8706 | nvlist_t *nvp = arg; |
8707 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f | 8708 | objset_t *mos = spa->spa_meta_objset; |
9ae529ec | 8709 | nvpair_t *elem = NULL; |
b128c09f BB |
8710 | |
8711 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 8712 | |
34dc7c2f | 8713 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec CS |
8714 | uint64_t intval; |
8715 | char *strval, *fname; | |
8716 | zpool_prop_t prop; | |
8717 | const char *propname; | |
8718 | zprop_type_t proptype; | |
fa86b5db | 8719 | spa_feature_t fid; |
9ae529ec | 8720 | |
31864e3d BB |
8721 | switch (prop = zpool_name_to_prop(nvpair_name(elem))) { |
8722 | case ZPOOL_PROP_INVAL: | |
9ae529ec CS |
8723 | /* |
8724 | * We checked this earlier in spa_prop_validate(). | |
8725 | */ | |
8726 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
8727 | ||
8728 | fname = strchr(nvpair_name(elem), '@') + 1; | |
fa86b5db | 8729 | VERIFY0(zfeature_lookup_name(fname, &fid)); |
9ae529ec | 8730 | |
fa86b5db | 8731 | spa_feature_enable(spa, fid, tx); |
6f1ffb06 MA |
8732 | spa_history_log_internal(spa, "set", tx, |
8733 | "%s=enabled", nvpair_name(elem)); | |
9ae529ec CS |
8734 | break; |
8735 | ||
34dc7c2f | 8736 | case ZPOOL_PROP_VERSION: |
93cf2076 | 8737 | intval = fnvpair_value_uint64(elem); |
34dc7c2f | 8738 | /* |
4e33ba4c | 8739 | * The version is synced separately before other |
9ae529ec | 8740 | * properties and should be correct by now. |
34dc7c2f | 8741 | */ |
9ae529ec | 8742 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
8743 | break; |
8744 | ||
8745 | case ZPOOL_PROP_ALTROOT: | |
8746 | /* | |
8747 | * 'altroot' is a non-persistent property. It should | |
8748 | * have been set temporarily at creation or import time. | |
8749 | */ | |
8750 | ASSERT(spa->spa_root != NULL); | |
8751 | break; | |
8752 | ||
572e2857 | 8753 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
8754 | case ZPOOL_PROP_CACHEFILE: |
8755 | /* | |
e1cfd73f | 8756 | * 'readonly' and 'cachefile' are also non-persistent |
572e2857 | 8757 | * properties. |
34dc7c2f | 8758 | */ |
34dc7c2f | 8759 | break; |
d96eb2b1 | 8760 | case ZPOOL_PROP_COMMENT: |
93cf2076 | 8761 | strval = fnvpair_value_string(elem); |
d96eb2b1 DM |
8762 | if (spa->spa_comment != NULL) |
8763 | spa_strfree(spa->spa_comment); | |
8764 | spa->spa_comment = spa_strdup(strval); | |
8765 | /* | |
8766 | * We need to dirty the configuration on all the vdevs | |
88a48330 BB |
8767 | * so that their labels get updated. We also need to |
8768 | * update the cache file to keep it in sync with the | |
8769 | * MOS version. It's unnecessary to do this for pool | |
8770 | * creation since the vdev's configuration has already | |
8771 | * been dirtied. | |
d96eb2b1 | 8772 | */ |
88a48330 | 8773 | if (tx->tx_txg != TXG_INITIAL) { |
d96eb2b1 | 8774 | vdev_config_dirty(spa->spa_root_vdev); |
88a48330 BB |
8775 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); |
8776 | } | |
6f1ffb06 MA |
8777 | spa_history_log_internal(spa, "set", tx, |
8778 | "%s=%s", nvpair_name(elem), strval); | |
d96eb2b1 | 8779 | break; |
658fb802 CB |
8780 | case ZPOOL_PROP_COMPATIBILITY: |
8781 | strval = fnvpair_value_string(elem); | |
8782 | if (spa->spa_compatibility != NULL) | |
8783 | spa_strfree(spa->spa_compatibility); | |
8784 | spa->spa_compatibility = spa_strdup(strval); | |
8785 | /* | |
8786 | * Dirty the configuration on vdevs as above. | |
8787 | */ | |
88a48330 | 8788 | if (tx->tx_txg != TXG_INITIAL) { |
658fb802 | 8789 | vdev_config_dirty(spa->spa_root_vdev); |
88a48330 BB |
8790 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); |
8791 | } | |
8792 | ||
658fb802 CB |
8793 | spa_history_log_internal(spa, "set", tx, |
8794 | "%s=%s", nvpair_name(elem), strval); | |
8795 | break; | |
8796 | ||
34dc7c2f BB |
8797 | default: |
8798 | /* | |
8799 | * Set pool property values in the poolprops mos object. | |
8800 | */ | |
34dc7c2f | 8801 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
8802 | spa->spa_pool_props_object = |
8803 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 8804 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 8805 | tx); |
34dc7c2f | 8806 | } |
34dc7c2f BB |
8807 | |
8808 | /* normalize the property name */ | |
8809 | propname = zpool_prop_to_name(prop); | |
8810 | proptype = zpool_prop_get_type(prop); | |
8811 | ||
8812 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
8813 | ASSERT(proptype == PROP_TYPE_STRING); | |
93cf2076 GW |
8814 | strval = fnvpair_value_string(elem); |
8815 | VERIFY0(zap_update(mos, | |
34dc7c2f | 8816 | spa->spa_pool_props_object, propname, |
93cf2076 | 8817 | 1, strlen(strval) + 1, strval, tx)); |
6f1ffb06 MA |
8818 | spa_history_log_internal(spa, "set", tx, |
8819 | "%s=%s", nvpair_name(elem), strval); | |
34dc7c2f | 8820 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { |
93cf2076 | 8821 | intval = fnvpair_value_uint64(elem); |
34dc7c2f BB |
8822 | |
8823 | if (proptype == PROP_TYPE_INDEX) { | |
8824 | const char *unused; | |
93cf2076 GW |
8825 | VERIFY0(zpool_prop_index_to_string( |
8826 | prop, intval, &unused)); | |
34dc7c2f | 8827 | } |
93cf2076 | 8828 | VERIFY0(zap_update(mos, |
34dc7c2f | 8829 | spa->spa_pool_props_object, propname, |
93cf2076 | 8830 | 8, 1, &intval, tx)); |
6f1ffb06 | 8831 | spa_history_log_internal(spa, "set", tx, |
74756182 MM |
8832 | "%s=%lld", nvpair_name(elem), |
8833 | (longlong_t)intval); | |
34dc7c2f BB |
8834 | } else { |
8835 | ASSERT(0); /* not allowed */ | |
8836 | } | |
8837 | ||
8838 | switch (prop) { | |
8839 | case ZPOOL_PROP_DELEGATION: | |
8840 | spa->spa_delegation = intval; | |
8841 | break; | |
8842 | case ZPOOL_PROP_BOOTFS: | |
8843 | spa->spa_bootfs = intval; | |
8844 | break; | |
8845 | case ZPOOL_PROP_FAILUREMODE: | |
8846 | spa->spa_failmode = intval; | |
8847 | break; | |
1b939560 BB |
8848 | case ZPOOL_PROP_AUTOTRIM: |
8849 | spa->spa_autotrim = intval; | |
8850 | spa_async_request(spa, | |
8851 | SPA_ASYNC_AUTOTRIM_RESTART); | |
8852 | break; | |
9babb374 BB |
8853 | case ZPOOL_PROP_AUTOEXPAND: |
8854 | spa->spa_autoexpand = intval; | |
428870ff BB |
8855 | if (tx->tx_txg != TXG_INITIAL) |
8856 | spa_async_request(spa, | |
8857 | SPA_ASYNC_AUTOEXPAND); | |
8858 | break; | |
379ca9cf OF |
8859 | case ZPOOL_PROP_MULTIHOST: |
8860 | spa->spa_multihost = intval; | |
8861 | break; | |
34dc7c2f BB |
8862 | default: |
8863 | break; | |
8864 | } | |
8865 | } | |
8866 | ||
34dc7c2f | 8867 | } |
b128c09f BB |
8868 | |
8869 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
8870 | } |
8871 | ||
428870ff BB |
8872 | /* |
8873 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
8874 | * reflect the new version this txg, so there must be no changes this | |
8875 | * txg to anything that the upgrade code depends on after it executes. | |
8876 | * Therefore this must be called after dsl_pool_sync() does the sync | |
8877 | * tasks. | |
8878 | */ | |
8879 | static void | |
8880 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
8881 | { | |
8dc2197b SD |
8882 | if (spa_sync_pass(spa) != 1) |
8883 | return; | |
428870ff | 8884 | |
8dc2197b | 8885 | dsl_pool_t *dp = spa->spa_dsl_pool; |
13fe0198 MA |
8886 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
8887 | ||
428870ff BB |
8888 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && |
8889 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
8890 | dsl_pool_create_origin(dp, tx); | |
8891 | ||
8892 | /* Keeping the origin open increases spa_minref */ | |
8893 | spa->spa_minref += 3; | |
8894 | } | |
8895 | ||
8896 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
8897 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
8898 | dsl_pool_upgrade_clones(dp, tx); | |
8899 | } | |
8900 | ||
8901 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
8902 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
8903 | dsl_pool_upgrade_dir_clones(dp, tx); | |
8904 | ||
8905 | /* Keeping the freedir open increases spa_minref */ | |
8906 | spa->spa_minref += 3; | |
8907 | } | |
9ae529ec CS |
8908 | |
8909 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
8910 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8911 | spa_feature_create_zap_objects(spa, tx); | |
8912 | } | |
62bdd5eb DL |
8913 | |
8914 | /* | |
8915 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
8916 | * when possibility to use lz4 compression for metadata was added | |
8917 | * Old pools that have this feature enabled must be upgraded to have | |
8918 | * this feature active | |
8919 | */ | |
8920 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8921 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
8922 | SPA_FEATURE_LZ4_COMPRESS); | |
8923 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
8924 | SPA_FEATURE_LZ4_COMPRESS); | |
8925 | ||
8926 | if (lz4_en && !lz4_ac) | |
8927 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
8928 | } | |
3c67d83a TH |
8929 | |
8930 | /* | |
8931 | * If we haven't written the salt, do so now. Note that the | |
8932 | * feature may not be activated yet, but that's fine since | |
8933 | * the presence of this ZAP entry is backwards compatible. | |
8934 | */ | |
8935 | if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
8936 | DMU_POOL_CHECKSUM_SALT) == ENOENT) { | |
8937 | VERIFY0(zap_add(spa->spa_meta_objset, | |
8938 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, | |
8939 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
8940 | spa->spa_cksum_salt.zcs_bytes, tx)); | |
8941 | } | |
8942 | ||
13fe0198 | 8943 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
428870ff BB |
8944 | } |
8945 | ||
a1d477c2 MA |
8946 | static void |
8947 | vdev_indirect_state_sync_verify(vdev_t *vd) | |
8948 | { | |
2a8ba608 MM |
8949 | vdev_indirect_mapping_t *vim __maybe_unused = vd->vdev_indirect_mapping; |
8950 | vdev_indirect_births_t *vib __maybe_unused = vd->vdev_indirect_births; | |
a1d477c2 MA |
8951 | |
8952 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
8953 | ASSERT(vim != NULL); | |
8954 | ASSERT(vib != NULL); | |
8955 | } | |
8956 | ||
27f80e85 BB |
8957 | uint64_t obsolete_sm_object = 0; |
8958 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
8959 | if (obsolete_sm_object != 0) { | |
a1d477c2 MA |
8960 | ASSERT(vd->vdev_obsolete_sm != NULL); |
8961 | ASSERT(vd->vdev_removing || | |
8962 | vd->vdev_ops == &vdev_indirect_ops); | |
8963 | ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); | |
8964 | ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); | |
27f80e85 | 8965 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
8966 | space_map_object(vd->vdev_obsolete_sm)); |
8967 | ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, | |
8968 | space_map_allocated(vd->vdev_obsolete_sm)); | |
8969 | } | |
8970 | ASSERT(vd->vdev_obsolete_segments != NULL); | |
8971 | ||
8972 | /* | |
8973 | * Since frees / remaps to an indirect vdev can only | |
8974 | * happen in syncing context, the obsolete segments | |
8975 | * tree must be empty when we start syncing. | |
8976 | */ | |
8977 | ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); | |
8978 | } | |
8979 | ||
34dc7c2f | 8980 | /* |
8dc2197b SD |
8981 | * Set the top-level vdev's max queue depth. Evaluate each top-level's |
8982 | * async write queue depth in case it changed. The max queue depth will | |
8983 | * not change in the middle of syncing out this txg. | |
34dc7c2f | 8984 | */ |
8dc2197b SD |
8985 | static void |
8986 | spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) | |
34dc7c2f | 8987 | { |
8dc2197b SD |
8988 | ASSERT(spa_writeable(spa)); |
8989 | ||
34dc7c2f | 8990 | vdev_t *rvd = spa->spa_root_vdev; |
3dfb57a3 DB |
8991 | uint32_t max_queue_depth = zfs_vdev_async_write_max_active * |
8992 | zfs_vdev_queue_depth_pct / 100; | |
8dc2197b SD |
8993 | metaslab_class_t *normal = spa_normal_class(spa); |
8994 | metaslab_class_t *special = spa_special_class(spa); | |
8995 | metaslab_class_t *dedup = spa_dedup_class(spa); | |
34dc7c2f | 8996 | |
492f64e9 | 8997 | uint64_t slots_per_allocator = 0; |
1c27024e | 8998 | for (int c = 0; c < rvd->vdev_children; c++) { |
3dfb57a3 | 8999 | vdev_t *tvd = rvd->vdev_child[c]; |
cc99f275 | 9000 | |
8dc2197b | 9001 | metaslab_group_t *mg = tvd->vdev_mg; |
cc99f275 DB |
9002 | if (mg == NULL || !metaslab_group_initialized(mg)) |
9003 | continue; | |
3dfb57a3 | 9004 | |
8dc2197b | 9005 | metaslab_class_t *mc = mg->mg_class; |
cc99f275 | 9006 | if (mc != normal && mc != special && mc != dedup) |
3dfb57a3 DB |
9007 | continue; |
9008 | ||
9009 | /* | |
9010 | * It is safe to do a lock-free check here because only async | |
9011 | * allocations look at mg_max_alloc_queue_depth, and async | |
9012 | * allocations all happen from spa_sync(). | |
9013 | */ | |
32d805c3 | 9014 | for (int i = 0; i < mg->mg_allocators; i++) { |
424fd7c3 | 9015 | ASSERT0(zfs_refcount_count( |
32d805c3 MA |
9016 | &(mg->mg_allocator[i].mga_alloc_queue_depth))); |
9017 | } | |
3dfb57a3 | 9018 | mg->mg_max_alloc_queue_depth = max_queue_depth; |
492f64e9 | 9019 | |
32d805c3 MA |
9020 | for (int i = 0; i < mg->mg_allocators; i++) { |
9021 | mg->mg_allocator[i].mga_cur_max_alloc_queue_depth = | |
492f64e9 PD |
9022 | zfs_vdev_def_queue_depth; |
9023 | } | |
9024 | slots_per_allocator += zfs_vdev_def_queue_depth; | |
3dfb57a3 | 9025 | } |
cc99f275 | 9026 | |
492f64e9 | 9027 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
f8020c93 AM |
9028 | ASSERT0(zfs_refcount_count(&normal->mc_allocator[i]. |
9029 | mca_alloc_slots)); | |
9030 | ASSERT0(zfs_refcount_count(&special->mc_allocator[i]. | |
9031 | mca_alloc_slots)); | |
9032 | ASSERT0(zfs_refcount_count(&dedup->mc_allocator[i]. | |
9033 | mca_alloc_slots)); | |
9034 | normal->mc_allocator[i].mca_alloc_max_slots = | |
9035 | slots_per_allocator; | |
9036 | special->mc_allocator[i].mca_alloc_max_slots = | |
9037 | slots_per_allocator; | |
9038 | dedup->mc_allocator[i].mca_alloc_max_slots = | |
9039 | slots_per_allocator; | |
cc99f275 DB |
9040 | } |
9041 | normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
9042 | special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
9043 | dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8dc2197b SD |
9044 | } |
9045 | ||
9046 | static void | |
9047 | spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) | |
9048 | { | |
9049 | ASSERT(spa_writeable(spa)); | |
3dfb57a3 | 9050 | |
8dc2197b | 9051 | vdev_t *rvd = spa->spa_root_vdev; |
a1d477c2 MA |
9052 | for (int c = 0; c < rvd->vdev_children; c++) { |
9053 | vdev_t *vd = rvd->vdev_child[c]; | |
9054 | vdev_indirect_state_sync_verify(vd); | |
9055 | ||
9056 | if (vdev_indirect_should_condense(vd)) { | |
9057 | spa_condense_indirect_start_sync(vd, tx); | |
9058 | break; | |
9059 | } | |
9060 | } | |
8dc2197b SD |
9061 | } |
9062 | ||
9063 | static void | |
9064 | spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) | |
9065 | { | |
9066 | objset_t *mos = spa->spa_meta_objset; | |
9067 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
9068 | uint64_t txg = tx->tx_txg; | |
9069 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
a1d477c2 | 9070 | |
34dc7c2f | 9071 | do { |
428870ff | 9072 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
9073 | |
9074 | spa_sync_config_object(spa, tx); | |
9075 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
9076 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
9077 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
9078 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
9079 | spa_errlog_sync(spa, txg); | |
9080 | dsl_pool_sync(dp, txg); | |
9081 | ||
93e28d66 SD |
9082 | if (pass < zfs_sync_pass_deferred_free || |
9083 | spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) { | |
9084 | /* | |
9085 | * If the log space map feature is active we don't | |
9086 | * care about deferred frees and the deferred bpobj | |
9087 | * as the log space map should effectively have the | |
9088 | * same results (i.e. appending only to one object). | |
9089 | */ | |
e8b96c60 | 9090 | spa_sync_frees(spa, free_bpl, tx); |
428870ff | 9091 | } else { |
905edb40 MA |
9092 | /* |
9093 | * We can not defer frees in pass 1, because | |
9094 | * we sync the deferred frees later in pass 1. | |
9095 | */ | |
9096 | ASSERT3U(pass, >, 1); | |
37f03da8 | 9097 | bplist_iterate(free_bpl, bpobj_enqueue_alloc_cb, |
e8b96c60 | 9098 | &spa->spa_deferred_bpobj, tx); |
34dc7c2f BB |
9099 | } |
9100 | ||
428870ff BB |
9101 | ddt_sync(spa, txg); |
9102 | dsl_scan_sync(dp, tx); | |
8dc2197b SD |
9103 | svr_sync(spa, tx); |
9104 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 9105 | |
93e28d66 SD |
9106 | spa_flush_metaslabs(spa, tx); |
9107 | ||
8dc2197b | 9108 | vdev_t *vd = NULL; |
a1d477c2 MA |
9109 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) |
9110 | != NULL) | |
428870ff BB |
9111 | vdev_sync(vd, txg); |
9112 | ||
8dc2197b SD |
9113 | /* |
9114 | * Note: We need to check if the MOS is dirty because we could | |
9115 | * have marked the MOS dirty without updating the uberblock | |
9116 | * (e.g. if we have sync tasks but no dirty user data). We need | |
9117 | * to check the uberblock's rootbp because it is updated if we | |
9118 | * have synced out dirty data (though in this case the MOS will | |
9119 | * most likely also be dirty due to second order effects, we | |
9120 | * don't want to rely on that here). | |
9121 | */ | |
9122 | if (pass == 1 && | |
9123 | spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
9124 | !dmu_objset_is_dirty(mos, txg)) { | |
905edb40 | 9125 | /* |
8dc2197b SD |
9126 | * Nothing changed on the first pass, therefore this |
9127 | * TXG is a no-op. Avoid syncing deferred frees, so | |
9128 | * that we can keep this TXG as a no-op. | |
905edb40 | 9129 | */ |
8dc2197b SD |
9130 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); |
9131 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9132 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
9133 | ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); | |
9134 | break; | |
905edb40 | 9135 | } |
34dc7c2f | 9136 | |
8dc2197b | 9137 | spa_sync_deferred_frees(spa, tx); |
428870ff | 9138 | } while (dmu_objset_is_dirty(mos, txg)); |
8dc2197b | 9139 | } |
34dc7c2f | 9140 | |
8dc2197b SD |
9141 | /* |
9142 | * Rewrite the vdev configuration (which includes the uberblock) to | |
9143 | * commit the transaction group. | |
9144 | * | |
9145 | * If there are no dirty vdevs, we sync the uberblock to a few random | |
9146 | * top-level vdevs that are known to be visible in the config cache | |
9147 | * (see spa_vdev_add() for a complete description). If there *are* dirty | |
9148 | * vdevs, sync the uberblock to all vdevs. | |
9149 | */ | |
9150 | static void | |
9151 | spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) | |
9152 | { | |
9153 | vdev_t *rvd = spa->spa_root_vdev; | |
9154 | uint64_t txg = tx->tx_txg; | |
a1d477c2 | 9155 | |
b128c09f | 9156 | for (;;) { |
8dc2197b SD |
9157 | int error = 0; |
9158 | ||
b128c09f BB |
9159 | /* |
9160 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
9161 | * while we're attempting to write the vdev labels. | |
9162 | */ | |
9163 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
9164 | ||
9165 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
d2734cce | 9166 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; |
b128c09f BB |
9167 | int svdcount = 0; |
9168 | int children = rvd->vdev_children; | |
29274c9f | 9169 | int c0 = random_in_range(children); |
b128c09f | 9170 | |
1c27024e | 9171 | for (int c = 0; c < children; c++) { |
8dc2197b SD |
9172 | vdev_t *vd = |
9173 | rvd->vdev_child[(c0 + c) % children]; | |
d2734cce SD |
9174 | |
9175 | /* Stop when revisiting the first vdev */ | |
9176 | if (c > 0 && svd[0] == vd) | |
9177 | break; | |
9178 | ||
8dc2197b SD |
9179 | if (vd->vdev_ms_array == 0 || |
9180 | vd->vdev_islog || | |
a1d477c2 | 9181 | !vdev_is_concrete(vd)) |
b128c09f | 9182 | continue; |
d2734cce | 9183 | |
b128c09f | 9184 | svd[svdcount++] = vd; |
6cb8e530 | 9185 | if (svdcount == SPA_SYNC_MIN_VDEVS) |
b128c09f BB |
9186 | break; |
9187 | } | |
b6fcb792 | 9188 | error = vdev_config_sync(svd, svdcount, txg); |
b128c09f BB |
9189 | } else { |
9190 | error = vdev_config_sync(rvd->vdev_child, | |
b6fcb792 | 9191 | rvd->vdev_children, txg); |
34dc7c2f | 9192 | } |
34dc7c2f | 9193 | |
3bc7e0fb GW |
9194 | if (error == 0) |
9195 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
9196 | ||
b128c09f BB |
9197 | spa_config_exit(spa, SCL_STATE, FTAG); |
9198 | ||
9199 | if (error == 0) | |
9200 | break; | |
cec3a0a1 | 9201 | zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); |
b128c09f BB |
9202 | zio_resume_wait(spa); |
9203 | } | |
8dc2197b SD |
9204 | } |
9205 | ||
9206 | /* | |
9207 | * Sync the specified transaction group. New blocks may be dirtied as | |
9208 | * part of the process, so we iterate until it converges. | |
9209 | */ | |
9210 | void | |
9211 | spa_sync(spa_t *spa, uint64_t txg) | |
9212 | { | |
9213 | vdev_t *vd = NULL; | |
9214 | ||
9215 | VERIFY(spa_writeable(spa)); | |
9216 | ||
9217 | /* | |
9218 | * Wait for i/os issued in open context that need to complete | |
9219 | * before this txg syncs. | |
9220 | */ | |
9221 | (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); | |
9222 | spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, | |
9223 | ZIO_FLAG_CANFAIL); | |
9224 | ||
9225 | /* | |
9226 | * Lock out configuration changes. | |
9227 | */ | |
9228 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
9229 | ||
9230 | spa->spa_syncing_txg = txg; | |
9231 | spa->spa_sync_pass = 0; | |
9232 | ||
9233 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
1b50749c AM |
9234 | mutex_enter(&spa->spa_allocs[i].spaa_lock); |
9235 | VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree)); | |
9236 | mutex_exit(&spa->spa_allocs[i].spaa_lock); | |
8dc2197b SD |
9237 | } |
9238 | ||
9239 | /* | |
9240 | * If there are any pending vdev state changes, convert them | |
9241 | * into config changes that go out with this transaction group. | |
9242 | */ | |
9243 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
9244 | while (list_head(&spa->spa_state_dirty_list) != NULL) { | |
9245 | /* | |
9246 | * We need the write lock here because, for aux vdevs, | |
9247 | * calling vdev_config_dirty() modifies sav_config. | |
9248 | * This is ugly and will become unnecessary when we | |
9249 | * eliminate the aux vdev wart by integrating all vdevs | |
9250 | * into the root vdev tree. | |
9251 | */ | |
9252 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9253 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
9254 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
9255 | vdev_state_clean(vd); | |
9256 | vdev_config_dirty(vd); | |
9257 | } | |
9258 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9259 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9260 | } | |
9261 | spa_config_exit(spa, SCL_STATE, FTAG); | |
9262 | ||
9263 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
9264 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
9265 | ||
9266 | spa->spa_sync_starttime = gethrtime(); | |
9267 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); | |
9268 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, | |
9269 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
9270 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
9271 | ||
9272 | /* | |
9273 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
9274 | * set spa_deflate if we have no raid-z vdevs. | |
9275 | */ | |
9276 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
9277 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
9278 | vdev_t *rvd = spa->spa_root_vdev; | |
9279 | ||
9280 | int i; | |
9281 | for (i = 0; i < rvd->vdev_children; i++) { | |
9282 | vd = rvd->vdev_child[i]; | |
9283 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
9284 | break; | |
9285 | } | |
9286 | if (i == rvd->vdev_children) { | |
9287 | spa->spa_deflate = TRUE; | |
9288 | VERIFY0(zap_add(spa->spa_meta_objset, | |
9289 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
9290 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
9291 | } | |
9292 | } | |
9293 | ||
9294 | spa_sync_adjust_vdev_max_queue_depth(spa); | |
9295 | ||
9296 | spa_sync_condense_indirect(spa, tx); | |
9297 | ||
9298 | spa_sync_iterate_to_convergence(spa, tx); | |
9299 | ||
9300 | #ifdef ZFS_DEBUG | |
9301 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
9302 | /* | |
9303 | * Make sure that the number of ZAPs for all the vdevs matches | |
9304 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
9305 | * called if the config is dirty; otherwise there may be | |
9306 | * outstanding AVZ operations that weren't completed in | |
9307 | * spa_sync_config_object. | |
9308 | */ | |
9309 | uint64_t all_vdev_zap_entry_count; | |
9310 | ASSERT0(zap_count(spa->spa_meta_objset, | |
9311 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
9312 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
9313 | all_vdev_zap_entry_count); | |
9314 | } | |
9315 | #endif | |
9316 | ||
9317 | if (spa->spa_vdev_removal != NULL) { | |
9318 | ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); | |
9319 | } | |
9320 | ||
9321 | spa_sync_rewrite_vdev_config(spa, tx); | |
34dc7c2f BB |
9322 | dmu_tx_commit(tx); |
9323 | ||
57ddcda1 | 9324 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 GW |
9325 | spa->spa_deadman_tqid = 0; |
9326 | ||
34dc7c2f BB |
9327 | /* |
9328 | * Clear the dirty config list. | |
9329 | */ | |
b128c09f | 9330 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
9331 | vdev_config_clean(vd); |
9332 | ||
9333 | /* | |
9334 | * Now that the new config has synced transactionally, | |
9335 | * let it become visible to the config cache. | |
9336 | */ | |
9337 | if (spa->spa_config_syncing != NULL) { | |
9338 | spa_config_set(spa, spa->spa_config_syncing); | |
9339 | spa->spa_config_txg = txg; | |
9340 | spa->spa_config_syncing = NULL; | |
9341 | } | |
9342 | ||
428870ff | 9343 | dsl_pool_sync_done(dp, txg); |
34dc7c2f | 9344 | |
492f64e9 | 9345 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
1b50749c AM |
9346 | mutex_enter(&spa->spa_allocs[i].spaa_lock); |
9347 | VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree)); | |
9348 | mutex_exit(&spa->spa_allocs[i].spaa_lock); | |
492f64e9 | 9349 | } |
3dfb57a3 | 9350 | |
34dc7c2f BB |
9351 | /* |
9352 | * Update usable space statistics. | |
9353 | */ | |
619f0976 GW |
9354 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) |
9355 | != NULL) | |
34dc7c2f | 9356 | vdev_sync_done(vd, txg); |
f09fda50 PD |
9357 | |
9358 | metaslab_class_evict_old(spa->spa_normal_class, txg); | |
9359 | metaslab_class_evict_old(spa->spa_log_class, txg); | |
9360 | ||
93e28d66 | 9361 | spa_sync_close_syncing_log_sm(spa); |
34dc7c2f | 9362 | |
428870ff BB |
9363 | spa_update_dspace(spa); |
9364 | ||
34dc7c2f BB |
9365 | /* |
9366 | * It had better be the case that we didn't dirty anything | |
9367 | * since vdev_config_sync(). | |
9368 | */ | |
9369 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
9370 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9371 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff | 9372 | |
d2734cce SD |
9373 | while (zfs_pause_spa_sync) |
9374 | delay(1); | |
9375 | ||
428870ff | 9376 | spa->spa_sync_pass = 0; |
34dc7c2f | 9377 | |
55922e73 GW |
9378 | /* |
9379 | * Update the last synced uberblock here. We want to do this at | |
9380 | * the end of spa_sync() so that consumers of spa_last_synced_txg() | |
9381 | * will be guaranteed that all the processing associated with | |
9382 | * that txg has been completed. | |
9383 | */ | |
9384 | spa->spa_ubsync = spa->spa_uberblock; | |
b128c09f | 9385 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 9386 | |
428870ff BB |
9387 | spa_handle_ignored_writes(spa); |
9388 | ||
34dc7c2f BB |
9389 | /* |
9390 | * If any async tasks have been requested, kick them off. | |
9391 | */ | |
9392 | spa_async_dispatch(spa); | |
9393 | } | |
9394 | ||
9395 | /* | |
9396 | * Sync all pools. We don't want to hold the namespace lock across these | |
9397 | * operations, so we take a reference on the spa_t and drop the lock during the | |
9398 | * sync. | |
9399 | */ | |
9400 | void | |
9401 | spa_sync_allpools(void) | |
9402 | { | |
9403 | spa_t *spa = NULL; | |
9404 | mutex_enter(&spa_namespace_lock); | |
9405 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
9406 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
9407 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
9408 | continue; |
9409 | spa_open_ref(spa, FTAG); | |
9410 | mutex_exit(&spa_namespace_lock); | |
9411 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9412 | mutex_enter(&spa_namespace_lock); | |
9413 | spa_close(spa, FTAG); | |
9414 | } | |
9415 | mutex_exit(&spa_namespace_lock); | |
9416 | } | |
9417 | ||
9418 | /* | |
9419 | * ========================================================================== | |
9420 | * Miscellaneous routines | |
9421 | * ========================================================================== | |
9422 | */ | |
9423 | ||
9424 | /* | |
9425 | * Remove all pools in the system. | |
9426 | */ | |
9427 | void | |
9428 | spa_evict_all(void) | |
9429 | { | |
9430 | spa_t *spa; | |
9431 | ||
9432 | /* | |
9433 | * Remove all cached state. All pools should be closed now, | |
9434 | * so every spa in the AVL tree should be unreferenced. | |
9435 | */ | |
9436 | mutex_enter(&spa_namespace_lock); | |
9437 | while ((spa = spa_next(NULL)) != NULL) { | |
9438 | /* | |
9439 | * Stop async tasks. The async thread may need to detach | |
9440 | * a device that's been replaced, which requires grabbing | |
9441 | * spa_namespace_lock, so we must drop it here. | |
9442 | */ | |
9443 | spa_open_ref(spa, FTAG); | |
9444 | mutex_exit(&spa_namespace_lock); | |
9445 | spa_async_suspend(spa); | |
9446 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
9447 | spa_close(spa, FTAG); |
9448 | ||
9449 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
9450 | spa_unload(spa); | |
9451 | spa_deactivate(spa); | |
9452 | } | |
9453 | spa_remove(spa); | |
9454 | } | |
9455 | mutex_exit(&spa_namespace_lock); | |
9456 | } | |
9457 | ||
9458 | vdev_t * | |
9babb374 | 9459 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 9460 | { |
b128c09f BB |
9461 | vdev_t *vd; |
9462 | int i; | |
9463 | ||
9464 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
9465 | return (vd); | |
9466 | ||
9babb374 | 9467 | if (aux) { |
b128c09f BB |
9468 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
9469 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
9470 | if (vd->vdev_guid == guid) |
9471 | return (vd); | |
9472 | } | |
9473 | ||
9474 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
9475 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
9476 | if (vd->vdev_guid == guid) |
9477 | return (vd); | |
9478 | } | |
9479 | } | |
9480 | ||
9481 | return (NULL); | |
34dc7c2f BB |
9482 | } |
9483 | ||
9484 | void | |
9485 | spa_upgrade(spa_t *spa, uint64_t version) | |
9486 | { | |
572e2857 BB |
9487 | ASSERT(spa_writeable(spa)); |
9488 | ||
b128c09f | 9489 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
9490 | |
9491 | /* | |
9492 | * This should only be called for a non-faulted pool, and since a | |
9493 | * future version would result in an unopenable pool, this shouldn't be | |
9494 | * possible. | |
9495 | */ | |
8dca0a9a | 9496 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
9b67f605 | 9497 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); |
34dc7c2f BB |
9498 | |
9499 | spa->spa_uberblock.ub_version = version; | |
9500 | vdev_config_dirty(spa->spa_root_vdev); | |
9501 | ||
b128c09f | 9502 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
9503 | |
9504 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9505 | } | |
9506 | ||
49d42425 FU |
9507 | static boolean_t |
9508 | spa_has_aux_vdev(spa_t *spa, uint64_t guid, spa_aux_vdev_t *sav) | |
34dc7c2f | 9509 | { |
14e4e3cb | 9510 | (void) spa; |
34dc7c2f | 9511 | int i; |
49d42425 | 9512 | uint64_t vdev_guid; |
34dc7c2f BB |
9513 | |
9514 | for (i = 0; i < sav->sav_count; i++) | |
9515 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
9516 | return (B_TRUE); | |
9517 | ||
9518 | for (i = 0; i < sav->sav_npending; i++) { | |
9519 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
49d42425 | 9520 | &vdev_guid) == 0 && vdev_guid == guid) |
34dc7c2f BB |
9521 | return (B_TRUE); |
9522 | } | |
9523 | ||
9524 | return (B_FALSE); | |
9525 | } | |
9526 | ||
49d42425 FU |
9527 | boolean_t |
9528 | spa_has_l2cache(spa_t *spa, uint64_t guid) | |
9529 | { | |
9530 | return (spa_has_aux_vdev(spa, guid, &spa->spa_l2cache)); | |
9531 | } | |
9532 | ||
9533 | boolean_t | |
9534 | spa_has_spare(spa_t *spa, uint64_t guid) | |
9535 | { | |
9536 | return (spa_has_aux_vdev(spa, guid, &spa->spa_spares)); | |
9537 | } | |
9538 | ||
b128c09f BB |
9539 | /* |
9540 | * Check if a pool has an active shared spare device. | |
9541 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
9542 | */ | |
9543 | static boolean_t | |
9544 | spa_has_active_shared_spare(spa_t *spa) | |
9545 | { | |
9546 | int i, refcnt; | |
9547 | uint64_t pool; | |
9548 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9549 | ||
9550 | for (i = 0; i < sav->sav_count; i++) { | |
9551 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
9552 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
9553 | refcnt > 2) | |
9554 | return (B_TRUE); | |
9555 | } | |
9556 | ||
9557 | return (B_FALSE); | |
9558 | } | |
9559 | ||
93e28d66 SD |
9560 | uint64_t |
9561 | spa_total_metaslabs(spa_t *spa) | |
9562 | { | |
9563 | vdev_t *rvd = spa->spa_root_vdev; | |
9564 | ||
9565 | uint64_t m = 0; | |
9566 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
9567 | vdev_t *vd = rvd->vdev_child[c]; | |
9568 | if (!vdev_is_concrete(vd)) | |
9569 | continue; | |
9570 | m += vd->vdev_ms_count; | |
9571 | } | |
9572 | return (m); | |
9573 | } | |
9574 | ||
e60e158e JG |
9575 | /* |
9576 | * Notify any waiting threads that some activity has switched from being in- | |
9577 | * progress to not-in-progress so that the thread can wake up and determine | |
9578 | * whether it is finished waiting. | |
9579 | */ | |
9580 | void | |
9581 | spa_notify_waiters(spa_t *spa) | |
9582 | { | |
9583 | /* | |
9584 | * Acquiring spa_activities_lock here prevents the cv_broadcast from | |
9585 | * happening between the waiting thread's check and cv_wait. | |
9586 | */ | |
9587 | mutex_enter(&spa->spa_activities_lock); | |
9588 | cv_broadcast(&spa->spa_activities_cv); | |
9589 | mutex_exit(&spa->spa_activities_lock); | |
9590 | } | |
9591 | ||
9592 | /* | |
9593 | * Notify any waiting threads that the pool is exporting, and then block until | |
9594 | * they are finished using the spa_t. | |
9595 | */ | |
9596 | void | |
9597 | spa_wake_waiters(spa_t *spa) | |
9598 | { | |
9599 | mutex_enter(&spa->spa_activities_lock); | |
9600 | spa->spa_waiters_cancel = B_TRUE; | |
9601 | cv_broadcast(&spa->spa_activities_cv); | |
9602 | while (spa->spa_waiters != 0) | |
9603 | cv_wait(&spa->spa_waiters_cv, &spa->spa_activities_lock); | |
9604 | spa->spa_waiters_cancel = B_FALSE; | |
9605 | mutex_exit(&spa->spa_activities_lock); | |
9606 | } | |
9607 | ||
2288d419 | 9608 | /* Whether the vdev or any of its descendants are being initialized/trimmed. */ |
e60e158e | 9609 | static boolean_t |
2288d419 | 9610 | spa_vdev_activity_in_progress_impl(vdev_t *vd, zpool_wait_activity_t activity) |
e60e158e JG |
9611 | { |
9612 | spa_t *spa = vd->vdev_spa; | |
e60e158e JG |
9613 | |
9614 | ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER)); | |
9615 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
2288d419 BB |
9616 | ASSERT(activity == ZPOOL_WAIT_INITIALIZE || |
9617 | activity == ZPOOL_WAIT_TRIM); | |
9618 | ||
9619 | kmutex_t *lock = activity == ZPOOL_WAIT_INITIALIZE ? | |
9620 | &vd->vdev_initialize_lock : &vd->vdev_trim_lock; | |
e60e158e JG |
9621 | |
9622 | mutex_exit(&spa->spa_activities_lock); | |
2288d419 | 9623 | mutex_enter(lock); |
e60e158e JG |
9624 | mutex_enter(&spa->spa_activities_lock); |
9625 | ||
2288d419 BB |
9626 | boolean_t in_progress = (activity == ZPOOL_WAIT_INITIALIZE) ? |
9627 | (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) : | |
9628 | (vd->vdev_trim_state == VDEV_TRIM_ACTIVE); | |
9629 | mutex_exit(lock); | |
e60e158e | 9630 | |
2288d419 | 9631 | if (in_progress) |
e60e158e JG |
9632 | return (B_TRUE); |
9633 | ||
9634 | for (int i = 0; i < vd->vdev_children; i++) { | |
2288d419 BB |
9635 | if (spa_vdev_activity_in_progress_impl(vd->vdev_child[i], |
9636 | activity)) | |
e60e158e JG |
9637 | return (B_TRUE); |
9638 | } | |
9639 | ||
9640 | return (B_FALSE); | |
9641 | } | |
9642 | ||
9643 | /* | |
9644 | * If use_guid is true, this checks whether the vdev specified by guid is | |
2288d419 BB |
9645 | * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool |
9646 | * is being initialized/trimmed. The caller must hold the config lock and | |
9647 | * spa_activities_lock. | |
e60e158e JG |
9648 | */ |
9649 | static int | |
2288d419 BB |
9650 | spa_vdev_activity_in_progress(spa_t *spa, boolean_t use_guid, uint64_t guid, |
9651 | zpool_wait_activity_t activity, boolean_t *in_progress) | |
e60e158e JG |
9652 | { |
9653 | mutex_exit(&spa->spa_activities_lock); | |
9654 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9655 | mutex_enter(&spa->spa_activities_lock); | |
9656 | ||
9657 | vdev_t *vd; | |
9658 | if (use_guid) { | |
9659 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
9660 | if (vd == NULL || !vd->vdev_ops->vdev_op_leaf) { | |
9661 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9662 | return (EINVAL); | |
9663 | } | |
9664 | } else { | |
9665 | vd = spa->spa_root_vdev; | |
9666 | } | |
9667 | ||
2288d419 | 9668 | *in_progress = spa_vdev_activity_in_progress_impl(vd, activity); |
e60e158e JG |
9669 | |
9670 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9671 | return (0); | |
9672 | } | |
9673 | ||
9674 | /* | |
9675 | * Locking for waiting threads | |
9676 | * --------------------------- | |
9677 | * | |
9678 | * Waiting threads need a way to check whether a given activity is in progress, | |
9679 | * and then, if it is, wait for it to complete. Each activity will have some | |
9680 | * in-memory representation of the relevant on-disk state which can be used to | |
9681 | * determine whether or not the activity is in progress. The in-memory state and | |
9682 | * the locking used to protect it will be different for each activity, and may | |
9683 | * not be suitable for use with a cvar (e.g., some state is protected by the | |
9684 | * config lock). To allow waiting threads to wait without any races, another | |
9685 | * lock, spa_activities_lock, is used. | |
9686 | * | |
9687 | * When the state is checked, both the activity-specific lock (if there is one) | |
9688 | * and spa_activities_lock are held. In some cases, the activity-specific lock | |
9689 | * is acquired explicitly (e.g. the config lock). In others, the locking is | |
9690 | * internal to some check (e.g. bpobj_is_empty). After checking, the waiting | |
9691 | * thread releases the activity-specific lock and, if the activity is in | |
9692 | * progress, then cv_waits using spa_activities_lock. | |
9693 | * | |
9694 | * The waiting thread is woken when another thread, one completing some | |
9695 | * activity, updates the state of the activity and then calls | |
9696 | * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only | |
9697 | * needs to hold its activity-specific lock when updating the state, and this | |
9698 | * lock can (but doesn't have to) be dropped before calling spa_notify_waiters. | |
9699 | * | |
9700 | * Because spa_notify_waiters acquires spa_activities_lock before broadcasting, | |
9701 | * and because it is held when the waiting thread checks the state of the | |
9702 | * activity, it can never be the case that the completing thread both updates | |
9703 | * the activity state and cv_broadcasts in between the waiting thread's check | |
9704 | * and cv_wait. Thus, a waiting thread can never miss a wakeup. | |
9705 | * | |
9706 | * In order to prevent deadlock, when the waiting thread does its check, in some | |
9707 | * cases it will temporarily drop spa_activities_lock in order to acquire the | |
9708 | * activity-specific lock. The order in which spa_activities_lock and the | |
9709 | * activity specific lock are acquired in the waiting thread is determined by | |
9710 | * the order in which they are acquired in the completing thread; if the | |
9711 | * completing thread calls spa_notify_waiters with the activity-specific lock | |
9712 | * held, then the waiting thread must also acquire the activity-specific lock | |
9713 | * first. | |
9714 | */ | |
9715 | ||
9716 | static int | |
9717 | spa_activity_in_progress(spa_t *spa, zpool_wait_activity_t activity, | |
9718 | boolean_t use_tag, uint64_t tag, boolean_t *in_progress) | |
9719 | { | |
9720 | int error = 0; | |
9721 | ||
9722 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
9723 | ||
9724 | switch (activity) { | |
9725 | case ZPOOL_WAIT_CKPT_DISCARD: | |
9726 | *in_progress = | |
9727 | (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT) && | |
9728 | zap_contains(spa_meta_objset(spa), | |
9729 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ZPOOL_CHECKPOINT) == | |
9730 | ENOENT); | |
9731 | break; | |
9732 | case ZPOOL_WAIT_FREE: | |
9733 | *in_progress = ((spa_version(spa) >= SPA_VERSION_DEADLISTS && | |
9734 | !bpobj_is_empty(&spa->spa_dsl_pool->dp_free_bpobj)) || | |
9735 | spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY) || | |
9736 | spa_livelist_delete_check(spa)); | |
9737 | break; | |
9738 | case ZPOOL_WAIT_INITIALIZE: | |
2288d419 BB |
9739 | case ZPOOL_WAIT_TRIM: |
9740 | error = spa_vdev_activity_in_progress(spa, use_tag, tag, | |
9741 | activity, in_progress); | |
e60e158e JG |
9742 | break; |
9743 | case ZPOOL_WAIT_REPLACE: | |
9744 | mutex_exit(&spa->spa_activities_lock); | |
9745 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9746 | mutex_enter(&spa->spa_activities_lock); | |
9747 | ||
9748 | *in_progress = vdev_replace_in_progress(spa->spa_root_vdev); | |
9749 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9750 | break; | |
9751 | case ZPOOL_WAIT_REMOVE: | |
9752 | *in_progress = (spa->spa_removing_phys.sr_state == | |
9753 | DSS_SCANNING); | |
9754 | break; | |
9755 | case ZPOOL_WAIT_RESILVER: | |
9a49d3f3 BB |
9756 | if ((*in_progress = vdev_rebuild_active(spa->spa_root_vdev))) |
9757 | break; | |
9a70e97f | 9758 | zfs_fallthrough; |
e60e158e JG |
9759 | case ZPOOL_WAIT_SCRUB: |
9760 | { | |
9761 | boolean_t scanning, paused, is_scrub; | |
9762 | dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; | |
9763 | ||
9764 | is_scrub = (scn->scn_phys.scn_func == POOL_SCAN_SCRUB); | |
9765 | scanning = (scn->scn_phys.scn_state == DSS_SCANNING); | |
9766 | paused = dsl_scan_is_paused_scrub(scn); | |
9767 | *in_progress = (scanning && !paused && | |
9768 | is_scrub == (activity == ZPOOL_WAIT_SCRUB)); | |
9769 | break; | |
9770 | } | |
9771 | default: | |
9772 | panic("unrecognized value for activity %d", activity); | |
9773 | } | |
9774 | ||
9775 | return (error); | |
9776 | } | |
9777 | ||
9778 | static int | |
9779 | spa_wait_common(const char *pool, zpool_wait_activity_t activity, | |
9780 | boolean_t use_tag, uint64_t tag, boolean_t *waited) | |
9781 | { | |
9782 | /* | |
9783 | * The tag is used to distinguish between instances of an activity. | |
2288d419 BB |
9784 | * 'initialize' and 'trim' are the only activities that we use this for. |
9785 | * The other activities can only have a single instance in progress in a | |
9786 | * pool at one time, making the tag unnecessary. | |
e60e158e JG |
9787 | * |
9788 | * There can be multiple devices being replaced at once, but since they | |
9789 | * all finish once resilvering finishes, we don't bother keeping track | |
9790 | * of them individually, we just wait for them all to finish. | |
9791 | */ | |
2288d419 BB |
9792 | if (use_tag && activity != ZPOOL_WAIT_INITIALIZE && |
9793 | activity != ZPOOL_WAIT_TRIM) | |
e60e158e JG |
9794 | return (EINVAL); |
9795 | ||
9796 | if (activity < 0 || activity >= ZPOOL_WAIT_NUM_ACTIVITIES) | |
9797 | return (EINVAL); | |
9798 | ||
9799 | spa_t *spa; | |
9800 | int error = spa_open(pool, &spa, FTAG); | |
9801 | if (error != 0) | |
9802 | return (error); | |
9803 | ||
9804 | /* | |
9805 | * Increment the spa's waiter count so that we can call spa_close and | |
9806 | * still ensure that the spa_t doesn't get freed before this thread is | |
9807 | * finished with it when the pool is exported. We want to call spa_close | |
9808 | * before we start waiting because otherwise the additional ref would | |
9809 | * prevent the pool from being exported or destroyed throughout the | |
9810 | * potentially long wait. | |
9811 | */ | |
9812 | mutex_enter(&spa->spa_activities_lock); | |
9813 | spa->spa_waiters++; | |
9814 | spa_close(spa, FTAG); | |
9815 | ||
9816 | *waited = B_FALSE; | |
9817 | for (;;) { | |
9818 | boolean_t in_progress; | |
9819 | error = spa_activity_in_progress(spa, activity, use_tag, tag, | |
9820 | &in_progress); | |
9821 | ||
b24771a8 | 9822 | if (error || !in_progress || spa->spa_waiters_cancel) |
e60e158e JG |
9823 | break; |
9824 | ||
9825 | *waited = B_TRUE; | |
9826 | ||
9827 | if (cv_wait_sig(&spa->spa_activities_cv, | |
9828 | &spa->spa_activities_lock) == 0) { | |
9829 | error = EINTR; | |
9830 | break; | |
9831 | } | |
9832 | } | |
9833 | ||
9834 | spa->spa_waiters--; | |
9835 | cv_signal(&spa->spa_waiters_cv); | |
9836 | mutex_exit(&spa->spa_activities_lock); | |
9837 | ||
9838 | return (error); | |
9839 | } | |
9840 | ||
9841 | /* | |
9842 | * Wait for a particular instance of the specified activity to complete, where | |
9843 | * the instance is identified by 'tag' | |
9844 | */ | |
9845 | int | |
9846 | spa_wait_tag(const char *pool, zpool_wait_activity_t activity, uint64_t tag, | |
9847 | boolean_t *waited) | |
9848 | { | |
9849 | return (spa_wait_common(pool, activity, B_TRUE, tag, waited)); | |
9850 | } | |
9851 | ||
9852 | /* | |
9853 | * Wait for all instances of the specified activity complete | |
9854 | */ | |
9855 | int | |
9856 | spa_wait(const char *pool, zpool_wait_activity_t activity, boolean_t *waited) | |
9857 | { | |
9858 | ||
9859 | return (spa_wait_common(pool, activity, B_FALSE, 0, waited)); | |
9860 | } | |
9861 | ||
a1d477c2 | 9862 | sysevent_t * |
12fa0466 DE |
9863 | spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
9864 | { | |
9865 | sysevent_t *ev = NULL; | |
9866 | #ifdef _KERNEL | |
9867 | nvlist_t *resource; | |
9868 | ||
9869 | resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl); | |
9870 | if (resource) { | |
9871 | ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP); | |
9872 | ev->resource = resource; | |
9873 | } | |
14e4e3cb AZ |
9874 | #else |
9875 | (void) spa, (void) vd, (void) hist_nvl, (void) name; | |
12fa0466 DE |
9876 | #endif |
9877 | return (ev); | |
9878 | } | |
9879 | ||
a1d477c2 | 9880 | void |
12fa0466 DE |
9881 | spa_event_post(sysevent_t *ev) |
9882 | { | |
9883 | #ifdef _KERNEL | |
9884 | if (ev) { | |
9885 | zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb); | |
9886 | kmem_free(ev, sizeof (*ev)); | |
9887 | } | |
14e4e3cb AZ |
9888 | #else |
9889 | (void) ev; | |
12fa0466 DE |
9890 | #endif |
9891 | } | |
9892 | ||
34dc7c2f | 9893 | /* |
fb390aaf HR |
9894 | * Post a zevent corresponding to the given sysevent. The 'name' must be one |
9895 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
34dc7c2f BB |
9896 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
9897 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
9898 | * or zdb as real changes. | |
9899 | */ | |
9900 | void | |
12fa0466 | 9901 | spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
34dc7c2f | 9902 | { |
12fa0466 | 9903 | spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); |
34dc7c2f | 9904 | } |
c28b2279 | 9905 | |
c28b2279 BB |
9906 | /* state manipulation functions */ |
9907 | EXPORT_SYMBOL(spa_open); | |
9908 | EXPORT_SYMBOL(spa_open_rewind); | |
9909 | EXPORT_SYMBOL(spa_get_stats); | |
9910 | EXPORT_SYMBOL(spa_create); | |
c28b2279 BB |
9911 | EXPORT_SYMBOL(spa_import); |
9912 | EXPORT_SYMBOL(spa_tryimport); | |
9913 | EXPORT_SYMBOL(spa_destroy); | |
9914 | EXPORT_SYMBOL(spa_export); | |
9915 | EXPORT_SYMBOL(spa_reset); | |
9916 | EXPORT_SYMBOL(spa_async_request); | |
9917 | EXPORT_SYMBOL(spa_async_suspend); | |
9918 | EXPORT_SYMBOL(spa_async_resume); | |
9919 | EXPORT_SYMBOL(spa_inject_addref); | |
9920 | EXPORT_SYMBOL(spa_inject_delref); | |
9921 | EXPORT_SYMBOL(spa_scan_stat_init); | |
9922 | EXPORT_SYMBOL(spa_scan_get_stats); | |
9923 | ||
e1cfd73f | 9924 | /* device manipulation */ |
c28b2279 BB |
9925 | EXPORT_SYMBOL(spa_vdev_add); |
9926 | EXPORT_SYMBOL(spa_vdev_attach); | |
9927 | EXPORT_SYMBOL(spa_vdev_detach); | |
c28b2279 BB |
9928 | EXPORT_SYMBOL(spa_vdev_setpath); |
9929 | EXPORT_SYMBOL(spa_vdev_setfru); | |
9930 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
9931 | ||
9932 | /* spare statech is global across all pools) */ | |
9933 | EXPORT_SYMBOL(spa_spare_add); | |
9934 | EXPORT_SYMBOL(spa_spare_remove); | |
9935 | EXPORT_SYMBOL(spa_spare_exists); | |
9936 | EXPORT_SYMBOL(spa_spare_activate); | |
9937 | ||
9938 | /* L2ARC statech is global across all pools) */ | |
9939 | EXPORT_SYMBOL(spa_l2cache_add); | |
9940 | EXPORT_SYMBOL(spa_l2cache_remove); | |
9941 | EXPORT_SYMBOL(spa_l2cache_exists); | |
9942 | EXPORT_SYMBOL(spa_l2cache_activate); | |
9943 | EXPORT_SYMBOL(spa_l2cache_drop); | |
9944 | ||
9945 | /* scanning */ | |
9946 | EXPORT_SYMBOL(spa_scan); | |
9947 | EXPORT_SYMBOL(spa_scan_stop); | |
9948 | ||
9949 | /* spa syncing */ | |
9950 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
9951 | EXPORT_SYMBOL(spa_sync_allpools); | |
9952 | ||
9953 | /* properties */ | |
9954 | EXPORT_SYMBOL(spa_prop_set); | |
9955 | EXPORT_SYMBOL(spa_prop_get); | |
9956 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
9957 | ||
9958 | /* asynchronous event notification */ | |
9959 | EXPORT_SYMBOL(spa_event_notify); | |
dea377c0 | 9960 | |
c8242a96 | 9961 | /* BEGIN CSTYLED */ |
03fdcb9a | 9962 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_shift, INT, ZMOD_RW, |
458f8231 | 9963 | "log2 fraction of arc that can be used by inflight I/Os when " |
03fdcb9a | 9964 | "verifying pool during import"); |
7ada752a | 9965 | /* END CSTYLED */ |
dea377c0 | 9966 | |
03fdcb9a | 9967 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_metadata, INT, ZMOD_RW, |
dea377c0 MA |
9968 | "Set to traverse metadata on pool import"); |
9969 | ||
03fdcb9a | 9970 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_data, INT, ZMOD_RW, |
dea377c0 | 9971 | "Set to traverse data on pool import"); |
dcb6bed1 | 9972 | |
03fdcb9a | 9973 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_print_vdev_tree, INT, ZMOD_RW, |
6cb8e530 PZ |
9974 | "Print vdev tree to zfs_dbgmsg during pool import"); |
9975 | ||
03fdcb9a | 9976 | ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_pct, UINT, ZMOD_RD, |
dcb6bed1 D |
9977 | "Percentage of CPUs to run an IO worker thread"); |
9978 | ||
7457b024 AM |
9979 | ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_tpq, UINT, ZMOD_RD, |
9980 | "Number of threads per IO worker taskqueue"); | |
9981 | ||
7ada752a | 9982 | /* BEGIN CSTYLED */ |
03fdcb9a MM |
9983 | ZFS_MODULE_PARAM(zfs, zfs_, max_missing_tvds, ULONG, ZMOD_RW, |
9984 | "Allow importing pool with up to this number of missing top-level " | |
9985 | "vdevs (in read-only mode)"); | |
7ada752a | 9986 | /* END CSTYLED */ |
6cb8e530 | 9987 | |
7ada752a AZ |
9988 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_pause, INT, |
9989 | ZMOD_RW, "Set the livelist condense zthr to pause"); | |
03fdcb9a | 9990 | |
7ada752a AZ |
9991 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_pause, INT, |
9992 | ZMOD_RW, "Set the livelist condense synctask to pause"); | |
37f03da8 | 9993 | |
7ada752a AZ |
9994 | /* BEGIN CSTYLED */ |
9995 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_cancel, | |
9996 | INT, ZMOD_RW, | |
37f03da8 | 9997 | "Whether livelist condensing was canceled in the synctask"); |
03fdcb9a | 9998 | |
7ada752a AZ |
9999 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_cancel, |
10000 | INT, ZMOD_RW, | |
37f03da8 SH |
10001 | "Whether livelist condensing was canceled in the zthr function"); |
10002 | ||
7ada752a AZ |
10003 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, new_alloc, INT, |
10004 | ZMOD_RW, | |
03fdcb9a MM |
10005 | "Whether extra ALLOC blkptrs were added to a livelist entry while it " |
10006 | "was being condensed"); | |
37f03da8 | 10007 | /* END CSTYLED */ |