]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
1d3ba0bf | 9 | * or https://opensource.org/licenses/CDDL-1.0. |
34dc7c2f BB |
10 | * See the License for the specific language governing permissions |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
4f072827 | 24 | * Copyright (c) 2011, 2020 by Delphix. All rights reserved. |
733b5722 | 25 | * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved. |
0c66c32d | 26 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. |
3c67d83a | 27 | * Copyright 2013 Saso Kiselkov. All rights reserved. |
e550644f BB |
28 | * Copyright (c) 2014 Integros [integros.com] |
29 | * Copyright 2016 Toomas Soome <tsoome@me.com> | |
a0bd735a | 30 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
f65fbee1 | 31 | * Copyright 2018 Joyent, Inc. |
3c819a2c | 32 | * Copyright (c) 2017, 2019, Datto Inc. All rights reserved. |
12fa0466 | 33 | * Copyright 2017 Joyent, Inc. |
cc99f275 | 34 | * Copyright (c) 2017, Intel Corporation. |
658fb802 | 35 | * Copyright (c) 2021, Colm Buckley <colm@tuatha.org> |
9d618615 | 36 | * Copyright (c) 2023 Hewlett Packard Enterprise Development LP. |
a38718a6 | 37 | */ |
34dc7c2f | 38 | |
34dc7c2f | 39 | /* |
e49f1e20 WA |
40 | * SPA: Storage Pool Allocator |
41 | * | |
34dc7c2f BB |
42 | * This file contains all the routines used when modifying on-disk SPA state. |
43 | * This includes opening, importing, destroying, exporting a pool, and syncing a | |
44 | * pool. | |
45 | */ | |
46 | ||
47 | #include <sys/zfs_context.h> | |
48 | #include <sys/fm/fs/zfs.h> | |
49 | #include <sys/spa_impl.h> | |
50 | #include <sys/zio.h> | |
51 | #include <sys/zio_checksum.h> | |
34dc7c2f BB |
52 | #include <sys/dmu.h> |
53 | #include <sys/dmu_tx.h> | |
54 | #include <sys/zap.h> | |
55 | #include <sys/zil.h> | |
67a1b037 | 56 | #include <sys/brt.h> |
428870ff | 57 | #include <sys/ddt.h> |
34dc7c2f | 58 | #include <sys/vdev_impl.h> |
a1d477c2 MA |
59 | #include <sys/vdev_removal.h> |
60 | #include <sys/vdev_indirect_mapping.h> | |
61 | #include <sys/vdev_indirect_births.h> | |
619f0976 | 62 | #include <sys/vdev_initialize.h> |
9a49d3f3 | 63 | #include <sys/vdev_rebuild.h> |
1b939560 | 64 | #include <sys/vdev_trim.h> |
c28b2279 | 65 | #include <sys/vdev_disk.h> |
b2255edc | 66 | #include <sys/vdev_draid.h> |
34dc7c2f | 67 | #include <sys/metaslab.h> |
428870ff | 68 | #include <sys/metaslab_impl.h> |
379ca9cf | 69 | #include <sys/mmp.h> |
34dc7c2f BB |
70 | #include <sys/uberblock_impl.h> |
71 | #include <sys/txg.h> | |
72 | #include <sys/avl.h> | |
a1d477c2 | 73 | #include <sys/bpobj.h> |
34dc7c2f BB |
74 | #include <sys/dmu_traverse.h> |
75 | #include <sys/dmu_objset.h> | |
76 | #include <sys/unique.h> | |
77 | #include <sys/dsl_pool.h> | |
78 | #include <sys/dsl_dataset.h> | |
79 | #include <sys/dsl_dir.h> | |
80 | #include <sys/dsl_prop.h> | |
81 | #include <sys/dsl_synctask.h> | |
82 | #include <sys/fs/zfs.h> | |
83 | #include <sys/arc.h> | |
84 | #include <sys/callb.h> | |
85 | #include <sys/systeminfo.h> | |
9babb374 | 86 | #include <sys/zfs_ioctl.h> |
428870ff | 87 | #include <sys/dsl_scan.h> |
9ae529ec | 88 | #include <sys/zfeature.h> |
13fe0198 | 89 | #include <sys/dsl_destroy.h> |
526af785 | 90 | #include <sys/zvol.h> |
34dc7c2f | 91 | |
d164b209 | 92 | #ifdef _KERNEL |
12fa0466 DE |
93 | #include <sys/fm/protocol.h> |
94 | #include <sys/fm/util.h> | |
428870ff | 95 | #include <sys/callb.h> |
d164b209 | 96 | #include <sys/zone.h> |
c8242a96 | 97 | #include <sys/vmsystm.h> |
d164b209 BB |
98 | #endif /* _KERNEL */ |
99 | ||
34dc7c2f BB |
100 | #include "zfs_prop.h" |
101 | #include "zfs_comutil.h" | |
102 | ||
e6cfd633 WA |
103 | /* |
104 | * The interval, in seconds, at which failed configuration cache file writes | |
105 | * should be retried. | |
106 | */ | |
a1d477c2 | 107 | int zfs_ccw_retry_interval = 300; |
e6cfd633 | 108 | |
428870ff | 109 | typedef enum zti_modes { |
7ef5e54e | 110 | ZTI_MODE_FIXED, /* value is # of threads (min 1) */ |
7ef5e54e | 111 | ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ |
7457b024 | 112 | ZTI_MODE_SCALE, /* Taskqs scale with CPUs. */ |
7ef5e54e AL |
113 | ZTI_MODE_NULL, /* don't create a taskq */ |
114 | ZTI_NMODES | |
428870ff | 115 | } zti_modes_t; |
34dc7c2f | 116 | |
7ef5e54e AL |
117 | #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } |
118 | #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } | |
119 | #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } | |
7457b024 | 120 | #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 } |
7ef5e54e | 121 | #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } |
9babb374 | 122 | |
7ef5e54e AL |
123 | #define ZTI_N(n) ZTI_P(n, 1) |
124 | #define ZTI_ONE ZTI_N(1) | |
9babb374 BB |
125 | |
126 | typedef struct zio_taskq_info { | |
7ef5e54e | 127 | zti_modes_t zti_mode; |
428870ff | 128 | uint_t zti_value; |
7ef5e54e | 129 | uint_t zti_count; |
9babb374 BB |
130 | } zio_taskq_info_t; |
131 | ||
132 | static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { | |
451041db | 133 | "iss", "iss_h", "int", "int_h" |
9babb374 BB |
134 | }; |
135 | ||
428870ff | 136 | /* |
7ef5e54e AL |
137 | * This table defines the taskq settings for each ZFS I/O type. When |
138 | * initializing a pool, we use this table to create an appropriately sized | |
139 | * taskq. Some operations are low volume and therefore have a small, static | |
140 | * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE | |
141 | * macros. Other operations process a large amount of data; the ZTI_BATCH | |
142 | * macro causes us to create a taskq oriented for throughput. Some operations | |
1b939560 | 143 | * are so high frequency and short-lived that the taskq itself can become a |
7ef5e54e AL |
144 | * point of lock contention. The ZTI_P(#, #) macro indicates that we need an |
145 | * additional degree of parallelism specified by the number of threads per- | |
146 | * taskq and the number of taskqs; when dispatching an event in this case, the | |
7457b024 AM |
147 | * particular taskq is chosen at random. ZTI_SCALE is similar to ZTI_BATCH, |
148 | * but with number of taskqs also scaling with number of CPUs. | |
7ef5e54e AL |
149 | * |
150 | * The different taskq priorities are to handle the different contexts (issue | |
151 | * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that | |
152 | * need to be handled with minimum delay. | |
428870ff | 153 | */ |
18168da7 | 154 | static const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { |
428870ff | 155 | /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ |
7ef5e54e | 156 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ |
7457b024 AM |
157 | { ZTI_N(8), ZTI_NULL, ZTI_SCALE, ZTI_NULL }, /* READ */ |
158 | { ZTI_BATCH, ZTI_N(5), ZTI_SCALE, ZTI_N(5) }, /* WRITE */ | |
159 | { ZTI_SCALE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ | |
7ef5e54e AL |
160 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ |
161 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ | |
1b939560 | 162 | { ZTI_N(4), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* TRIM */ |
9babb374 BB |
163 | }; |
164 | ||
13fe0198 MA |
165 | static void spa_sync_version(void *arg, dmu_tx_t *tx); |
166 | static void spa_sync_props(void *arg, dmu_tx_t *tx); | |
b128c09f | 167 | static boolean_t spa_has_active_shared_spare(spa_t *spa); |
a926aab9 AZ |
168 | static int spa_load_impl(spa_t *spa, spa_import_type_t type, |
169 | const char **ereport); | |
572e2857 | 170 | static void spa_vdev_resilver_done(spa_t *spa); |
428870ff | 171 | |
342357cd AM |
172 | /* |
173 | * Percentage of all CPUs that can be used by the metaslab preload taskq. | |
174 | */ | |
175 | static uint_t metaslab_preload_pct = 50; | |
176 | ||
18168da7 AZ |
177 | static uint_t zio_taskq_batch_pct = 80; /* 1 thread per cpu in pset */ |
178 | static uint_t zio_taskq_batch_tpq; /* threads per taskq */ | |
179 | static const boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ | |
180 | static const uint_t zio_taskq_basedc = 80; /* base duty cycle */ | |
428870ff | 181 | |
18168da7 | 182 | static const boolean_t spa_create_process = B_TRUE; /* no process => no sysdc */ |
428870ff | 183 | |
afd2f7b7 PZ |
184 | /* |
185 | * Report any spa_load_verify errors found, but do not fail spa_load. | |
186 | * This is used by zdb to analyze non-idle pools. | |
187 | */ | |
188 | boolean_t spa_load_verify_dryrun = B_FALSE; | |
189 | ||
e39fe05b FU |
190 | /* |
191 | * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ). | |
192 | * This is used by zdb for spacemaps verification. | |
193 | */ | |
194 | boolean_t spa_mode_readable_spacemaps = B_FALSE; | |
195 | ||
428870ff BB |
196 | /* |
197 | * This (illegal) pool name is used when temporarily importing a spa_t in order | |
198 | * to get the vdev stats associated with the imported devices. | |
199 | */ | |
200 | #define TRYIMPORT_NAME "$import" | |
34dc7c2f | 201 | |
6cb8e530 PZ |
202 | /* |
203 | * For debugging purposes: print out vdev tree during pool import. | |
204 | */ | |
18168da7 | 205 | static int spa_load_print_vdev_tree = B_FALSE; |
6cb8e530 PZ |
206 | |
207 | /* | |
208 | * A non-zero value for zfs_max_missing_tvds means that we allow importing | |
209 | * pools with missing top-level vdevs. This is strictly intended for advanced | |
210 | * pool recovery cases since missing data is almost inevitable. Pools with | |
211 | * missing devices can only be imported read-only for safety reasons, and their | |
212 | * fail-mode will be automatically set to "continue". | |
213 | * | |
214 | * With 1 missing vdev we should be able to import the pool and mount all | |
215 | * datasets. User data that was not modified after the missing device has been | |
216 | * added should be recoverable. This means that snapshots created prior to the | |
217 | * addition of that device should be completely intact. | |
218 | * | |
219 | * With 2 missing vdevs, some datasets may fail to mount since there are | |
220 | * dataset statistics that are stored as regular metadata. Some data might be | |
221 | * recoverable if those vdevs were added recently. | |
222 | * | |
223 | * With 3 or more missing vdevs, the pool is severely damaged and MOS entries | |
224 | * may be missing entirely. Chances of data recovery are very low. Note that | |
225 | * there are also risks of performing an inadvertent rewind as we might be | |
226 | * missing all the vdevs with the latest uberblocks. | |
227 | */ | |
ab8d9c17 | 228 | uint64_t zfs_max_missing_tvds = 0; |
6cb8e530 PZ |
229 | |
230 | /* | |
231 | * The parameters below are similar to zfs_max_missing_tvds but are only | |
232 | * intended for a preliminary open of the pool with an untrusted config which | |
233 | * might be incomplete or out-dated. | |
234 | * | |
235 | * We are more tolerant for pools opened from a cachefile since we could have | |
236 | * an out-dated cachefile where a device removal was not registered. | |
237 | * We could have set the limit arbitrarily high but in the case where devices | |
238 | * are really missing we would want to return the proper error codes; we chose | |
239 | * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available | |
240 | * and we get a chance to retrieve the trusted config. | |
241 | */ | |
242 | uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1; | |
d2734cce | 243 | |
6cb8e530 PZ |
244 | /* |
245 | * In the case where config was assembled by scanning device paths (/dev/dsks | |
246 | * by default) we are less tolerant since all the existing devices should have | |
247 | * been detected and we want spa_load to return the right error codes. | |
248 | */ | |
249 | uint64_t zfs_max_missing_tvds_scan = 0; | |
250 | ||
d2734cce SD |
251 | /* |
252 | * Debugging aid that pauses spa_sync() towards the end. | |
253 | */ | |
18168da7 | 254 | static const boolean_t zfs_pause_spa_sync = B_FALSE; |
d2734cce | 255 | |
37f03da8 SH |
256 | /* |
257 | * Variables to indicate the livelist condense zthr func should wait at certain | |
258 | * points for the livelist to be removed - used to test condense/destroy races | |
259 | */ | |
18168da7 AZ |
260 | static int zfs_livelist_condense_zthr_pause = 0; |
261 | static int zfs_livelist_condense_sync_pause = 0; | |
37f03da8 SH |
262 | |
263 | /* | |
264 | * Variables to track whether or not condense cancellation has been | |
265 | * triggered in testing. | |
266 | */ | |
18168da7 AZ |
267 | static int zfs_livelist_condense_sync_cancel = 0; |
268 | static int zfs_livelist_condense_zthr_cancel = 0; | |
37f03da8 SH |
269 | |
270 | /* | |
271 | * Variable to track whether or not extra ALLOC blkptrs were added to a | |
272 | * livelist entry while it was being condensed (caused by the way we track | |
273 | * remapped blkptrs in dbuf_remap_impl) | |
274 | */ | |
18168da7 | 275 | static int zfs_livelist_condense_new_alloc = 0; |
37f03da8 | 276 | |
34dc7c2f BB |
277 | /* |
278 | * ========================================================================== | |
279 | * SPA properties routines | |
280 | * ========================================================================== | |
281 | */ | |
282 | ||
283 | /* | |
284 | * Add a (source=src, propname=propval) list to an nvlist. | |
285 | */ | |
286 | static void | |
a926aab9 | 287 | spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, const char *strval, |
34dc7c2f BB |
288 | uint64_t intval, zprop_source_t src) |
289 | { | |
290 | const char *propname = zpool_prop_to_name(prop); | |
291 | nvlist_t *propval; | |
292 | ||
65ad5d11 AJ |
293 | propval = fnvlist_alloc(); |
294 | fnvlist_add_uint64(propval, ZPROP_SOURCE, src); | |
34dc7c2f BB |
295 | |
296 | if (strval != NULL) | |
65ad5d11 | 297 | fnvlist_add_string(propval, ZPROP_VALUE, strval); |
34dc7c2f | 298 | else |
65ad5d11 | 299 | fnvlist_add_uint64(propval, ZPROP_VALUE, intval); |
34dc7c2f | 300 | |
65ad5d11 | 301 | fnvlist_add_nvlist(nvl, propname, propval); |
34dc7c2f BB |
302 | nvlist_free(propval); |
303 | } | |
304 | ||
8eae2d21 AJ |
305 | /* |
306 | * Add a user property (source=src, propname=propval) to an nvlist. | |
307 | */ | |
308 | static void | |
309 | spa_prop_add_user(nvlist_t *nvl, const char *propname, char *strval, | |
310 | zprop_source_t src) | |
311 | { | |
312 | nvlist_t *propval; | |
313 | ||
314 | VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
315 | VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); | |
316 | VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); | |
317 | VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); | |
318 | nvlist_free(propval); | |
319 | } | |
320 | ||
34dc7c2f BB |
321 | /* |
322 | * Get property values from the spa configuration. | |
323 | */ | |
324 | static void | |
325 | spa_prop_get_config(spa_t *spa, nvlist_t **nvp) | |
326 | { | |
1bd201e7 | 327 | vdev_t *rvd = spa->spa_root_vdev; |
9ae529ec | 328 | dsl_pool_t *pool = spa->spa_dsl_pool; |
f3a7f661 | 329 | uint64_t size, alloc, cap, version; |
82ab6848 | 330 | const zprop_source_t src = ZPROP_SRC_NONE; |
b128c09f | 331 | spa_config_dirent_t *dp; |
f3a7f661 | 332 | metaslab_class_t *mc = spa_normal_class(spa); |
b128c09f BB |
333 | |
334 | ASSERT(MUTEX_HELD(&spa->spa_props_lock)); | |
34dc7c2f | 335 | |
1bd201e7 | 336 | if (rvd != NULL) { |
cc99f275 DB |
337 | alloc = metaslab_class_get_alloc(mc); |
338 | alloc += metaslab_class_get_alloc(spa_special_class(spa)); | |
339 | alloc += metaslab_class_get_alloc(spa_dedup_class(spa)); | |
aa755b35 | 340 | alloc += metaslab_class_get_alloc(spa_embedded_log_class(spa)); |
cc99f275 DB |
341 | |
342 | size = metaslab_class_get_space(mc); | |
343 | size += metaslab_class_get_space(spa_special_class(spa)); | |
344 | size += metaslab_class_get_space(spa_dedup_class(spa)); | |
aa755b35 | 345 | size += metaslab_class_get_space(spa_embedded_log_class(spa)); |
cc99f275 | 346 | |
d164b209 BB |
347 | spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); |
348 | spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); | |
428870ff BB |
349 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); |
350 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, | |
351 | size - alloc, src); | |
d2734cce SD |
352 | spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL, |
353 | spa->spa_checkpoint_info.sci_dspace, src); | |
1bd201e7 | 354 | |
f3a7f661 GW |
355 | spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, |
356 | metaslab_class_fragmentation(mc), src); | |
357 | spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, | |
358 | metaslab_class_expandable_space(mc), src); | |
572e2857 | 359 | spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, |
da92d5cb | 360 | (spa_mode(spa) == SPA_MODE_READ), src); |
d164b209 | 361 | |
428870ff | 362 | cap = (size == 0) ? 0 : (alloc * 100 / size); |
d164b209 BB |
363 | spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); |
364 | ||
428870ff BB |
365 | spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, |
366 | ddt_get_pool_dedup_ratio(spa), src); | |
67a1b037 PJD |
367 | spa_prop_add_list(*nvp, ZPOOL_PROP_BCLONEUSED, NULL, |
368 | brt_get_used(spa), src); | |
369 | spa_prop_add_list(*nvp, ZPOOL_PROP_BCLONESAVED, NULL, | |
370 | brt_get_saved(spa), src); | |
371 | spa_prop_add_list(*nvp, ZPOOL_PROP_BCLONERATIO, NULL, | |
372 | brt_get_ratio(spa), src); | |
428870ff | 373 | |
d164b209 | 374 | spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, |
1bd201e7 | 375 | rvd->vdev_state, src); |
d164b209 BB |
376 | |
377 | version = spa_version(spa); | |
82ab6848 HM |
378 | if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) { |
379 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
380 | version, ZPROP_SRC_DEFAULT); | |
381 | } else { | |
382 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
383 | version, ZPROP_SRC_LOCAL); | |
384 | } | |
a448a255 SD |
385 | spa_prop_add_list(*nvp, ZPOOL_PROP_LOAD_GUID, |
386 | NULL, spa_load_guid(spa), src); | |
d164b209 | 387 | } |
34dc7c2f | 388 | |
9ae529ec | 389 | if (pool != NULL) { |
9ae529ec CS |
390 | /* |
391 | * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, | |
392 | * when opening pools before this version freedir will be NULL. | |
393 | */ | |
fbeddd60 | 394 | if (pool->dp_free_dir != NULL) { |
9ae529ec | 395 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, |
d683ddbb JG |
396 | dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, |
397 | src); | |
9ae529ec CS |
398 | } else { |
399 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, | |
400 | NULL, 0, src); | |
401 | } | |
fbeddd60 MA |
402 | |
403 | if (pool->dp_leak_dir != NULL) { | |
404 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, | |
d683ddbb JG |
405 | dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, |
406 | src); | |
fbeddd60 MA |
407 | } else { |
408 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, | |
409 | NULL, 0, src); | |
410 | } | |
9ae529ec CS |
411 | } |
412 | ||
34dc7c2f | 413 | spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); |
34dc7c2f | 414 | |
d96eb2b1 DM |
415 | if (spa->spa_comment != NULL) { |
416 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, | |
417 | 0, ZPROP_SRC_LOCAL); | |
418 | } | |
419 | ||
658fb802 CB |
420 | if (spa->spa_compatibility != NULL) { |
421 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMPATIBILITY, | |
422 | spa->spa_compatibility, 0, ZPROP_SRC_LOCAL); | |
423 | } | |
424 | ||
34dc7c2f BB |
425 | if (spa->spa_root != NULL) |
426 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, | |
427 | 0, ZPROP_SRC_LOCAL); | |
428 | ||
f1512ee6 MA |
429 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { |
430 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
431 | MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); | |
432 | } else { | |
433 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
434 | SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); | |
435 | } | |
436 | ||
50c957f7 NB |
437 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) { |
438 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
439 | DNODE_MAX_SIZE, ZPROP_SRC_NONE); | |
440 | } else { | |
441 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
442 | DNODE_MIN_SIZE, ZPROP_SRC_NONE); | |
443 | } | |
444 | ||
b128c09f BB |
445 | if ((dp = list_head(&spa->spa_config_list)) != NULL) { |
446 | if (dp->scd_path == NULL) { | |
34dc7c2f | 447 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f BB |
448 | "none", 0, ZPROP_SRC_LOCAL); |
449 | } else if (strcmp(dp->scd_path, spa_config_path) != 0) { | |
34dc7c2f | 450 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f | 451 | dp->scd_path, 0, ZPROP_SRC_LOCAL); |
34dc7c2f BB |
452 | } |
453 | } | |
454 | } | |
455 | ||
456 | /* | |
457 | * Get zpool property values. | |
458 | */ | |
459 | int | |
460 | spa_prop_get(spa_t *spa, nvlist_t **nvp) | |
461 | { | |
428870ff | 462 | objset_t *mos = spa->spa_meta_objset; |
34dc7c2f BB |
463 | zap_cursor_t zc; |
464 | zap_attribute_t za; | |
1743c737 | 465 | dsl_pool_t *dp; |
34dc7c2f BB |
466 | int err; |
467 | ||
79c76d5b | 468 | err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP); |
c28b2279 | 469 | if (err) |
d1d7e268 | 470 | return (err); |
34dc7c2f | 471 | |
1743c737 AM |
472 | dp = spa_get_dsl(spa); |
473 | dsl_pool_config_enter(dp, FTAG); | |
b128c09f BB |
474 | mutex_enter(&spa->spa_props_lock); |
475 | ||
34dc7c2f BB |
476 | /* |
477 | * Get properties from the spa config. | |
478 | */ | |
479 | spa_prop_get_config(spa, nvp); | |
480 | ||
34dc7c2f | 481 | /* If no pool property object, no more prop to get. */ |
1743c737 | 482 | if (mos == NULL || spa->spa_pool_props_object == 0) |
c28b2279 | 483 | goto out; |
34dc7c2f BB |
484 | |
485 | /* | |
486 | * Get properties from the MOS pool property object. | |
487 | */ | |
488 | for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); | |
489 | (err = zap_cursor_retrieve(&zc, &za)) == 0; | |
490 | zap_cursor_advance(&zc)) { | |
491 | uint64_t intval = 0; | |
492 | char *strval = NULL; | |
493 | zprop_source_t src = ZPROP_SRC_DEFAULT; | |
494 | zpool_prop_t prop; | |
495 | ||
8eae2d21 AJ |
496 | if ((prop = zpool_name_to_prop(za.za_name)) == |
497 | ZPOOL_PROP_INVAL && !zfs_prop_user(za.za_name)) | |
34dc7c2f BB |
498 | continue; |
499 | ||
500 | switch (za.za_integer_length) { | |
501 | case 8: | |
502 | /* integer property */ | |
503 | if (za.za_first_integer != | |
504 | zpool_prop_default_numeric(prop)) | |
505 | src = ZPROP_SRC_LOCAL; | |
506 | ||
507 | if (prop == ZPOOL_PROP_BOOTFS) { | |
34dc7c2f BB |
508 | dsl_dataset_t *ds = NULL; |
509 | ||
619f0976 GW |
510 | err = dsl_dataset_hold_obj(dp, |
511 | za.za_first_integer, FTAG, &ds); | |
1743c737 | 512 | if (err != 0) |
34dc7c2f | 513 | break; |
34dc7c2f | 514 | |
eca7b760 | 515 | strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, |
79c76d5b | 516 | KM_SLEEP); |
34dc7c2f | 517 | dsl_dataset_name(ds, strval); |
b128c09f | 518 | dsl_dataset_rele(ds, FTAG); |
34dc7c2f BB |
519 | } else { |
520 | strval = NULL; | |
521 | intval = za.za_first_integer; | |
522 | } | |
523 | ||
524 | spa_prop_add_list(*nvp, prop, strval, intval, src); | |
525 | ||
526 | if (strval != NULL) | |
eca7b760 | 527 | kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); |
34dc7c2f BB |
528 | |
529 | break; | |
530 | ||
531 | case 1: | |
532 | /* string property */ | |
79c76d5b | 533 | strval = kmem_alloc(za.za_num_integers, KM_SLEEP); |
34dc7c2f BB |
534 | err = zap_lookup(mos, spa->spa_pool_props_object, |
535 | za.za_name, 1, za.za_num_integers, strval); | |
536 | if (err) { | |
537 | kmem_free(strval, za.za_num_integers); | |
538 | break; | |
539 | } | |
8eae2d21 AJ |
540 | if (prop != ZPOOL_PROP_INVAL) { |
541 | spa_prop_add_list(*nvp, prop, strval, 0, src); | |
542 | } else { | |
543 | src = ZPROP_SRC_LOCAL; | |
544 | spa_prop_add_user(*nvp, za.za_name, strval, | |
545 | src); | |
546 | } | |
34dc7c2f BB |
547 | kmem_free(strval, za.za_num_integers); |
548 | break; | |
549 | ||
550 | default: | |
551 | break; | |
552 | } | |
553 | } | |
554 | zap_cursor_fini(&zc); | |
34dc7c2f | 555 | out: |
1743c737 AM |
556 | mutex_exit(&spa->spa_props_lock); |
557 | dsl_pool_config_exit(dp, FTAG); | |
34dc7c2f BB |
558 | if (err && err != ENOENT) { |
559 | nvlist_free(*nvp); | |
560 | *nvp = NULL; | |
561 | return (err); | |
562 | } | |
563 | ||
564 | return (0); | |
565 | } | |
566 | ||
567 | /* | |
568 | * Validate the given pool properties nvlist and modify the list | |
569 | * for the property values to be set. | |
570 | */ | |
571 | static int | |
572 | spa_prop_validate(spa_t *spa, nvlist_t *props) | |
573 | { | |
574 | nvpair_t *elem; | |
575 | int error = 0, reset_bootfs = 0; | |
d4ed6673 | 576 | uint64_t objnum = 0; |
9ae529ec | 577 | boolean_t has_feature = B_FALSE; |
34dc7c2f BB |
578 | |
579 | elem = NULL; | |
580 | while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { | |
34dc7c2f | 581 | uint64_t intval; |
d1807f16 | 582 | const char *strval, *slash, *check, *fname; |
9ae529ec CS |
583 | const char *propname = nvpair_name(elem); |
584 | zpool_prop_t prop = zpool_name_to_prop(propname); | |
585 | ||
31864e3d BB |
586 | switch (prop) { |
587 | case ZPOOL_PROP_INVAL: | |
9ae529ec CS |
588 | /* |
589 | * Sanitize the input. | |
590 | */ | |
8eae2d21 AJ |
591 | if (zfs_prop_user(propname)) { |
592 | if (strlen(propname) >= ZAP_MAXNAMELEN) { | |
593 | error = SET_ERROR(ENAMETOOLONG); | |
594 | break; | |
595 | } | |
9ae529ec | 596 | |
8eae2d21 AJ |
597 | if (strlen(fnvpair_value_string(elem)) >= |
598 | ZAP_MAXVALUELEN) { | |
599 | error = SET_ERROR(E2BIG); | |
600 | break; | |
601 | } | |
602 | } else if (zpool_prop_feature(propname)) { | |
603 | if (nvpair_type(elem) != DATA_TYPE_UINT64) { | |
604 | error = SET_ERROR(EINVAL); | |
605 | break; | |
606 | } | |
34dc7c2f | 607 | |
8eae2d21 AJ |
608 | if (nvpair_value_uint64(elem, &intval) != 0) { |
609 | error = SET_ERROR(EINVAL); | |
610 | break; | |
611 | } | |
612 | ||
613 | if (intval != 0) { | |
614 | error = SET_ERROR(EINVAL); | |
615 | break; | |
616 | } | |
617 | ||
618 | fname = strchr(propname, '@') + 1; | |
619 | if (zfeature_lookup_name(fname, NULL) != 0) { | |
620 | error = SET_ERROR(EINVAL); | |
621 | break; | |
622 | } | |
34dc7c2f | 623 | |
8eae2d21 AJ |
624 | has_feature = B_TRUE; |
625 | } else { | |
2e528b49 | 626 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
627 | break; |
628 | } | |
9ae529ec | 629 | break; |
34dc7c2f | 630 | |
34dc7c2f BB |
631 | case ZPOOL_PROP_VERSION: |
632 | error = nvpair_value_uint64(elem, &intval); | |
633 | if (!error && | |
9ae529ec CS |
634 | (intval < spa_version(spa) || |
635 | intval > SPA_VERSION_BEFORE_FEATURES || | |
636 | has_feature)) | |
2e528b49 | 637 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
638 | break; |
639 | ||
640 | case ZPOOL_PROP_DELEGATION: | |
641 | case ZPOOL_PROP_AUTOREPLACE: | |
b128c09f | 642 | case ZPOOL_PROP_LISTSNAPS: |
9babb374 | 643 | case ZPOOL_PROP_AUTOEXPAND: |
1b939560 | 644 | case ZPOOL_PROP_AUTOTRIM: |
34dc7c2f BB |
645 | error = nvpair_value_uint64(elem, &intval); |
646 | if (!error && intval > 1) | |
2e528b49 | 647 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
648 | break; |
649 | ||
379ca9cf OF |
650 | case ZPOOL_PROP_MULTIHOST: |
651 | error = nvpair_value_uint64(elem, &intval); | |
652 | if (!error && intval > 1) | |
653 | error = SET_ERROR(EINVAL); | |
654 | ||
25f06d67 BB |
655 | if (!error) { |
656 | uint32_t hostid = zone_get_hostid(NULL); | |
657 | if (hostid) | |
658 | spa->spa_hostid = hostid; | |
659 | else | |
660 | error = SET_ERROR(ENOTSUP); | |
661 | } | |
379ca9cf OF |
662 | |
663 | break; | |
664 | ||
34dc7c2f | 665 | case ZPOOL_PROP_BOOTFS: |
9babb374 BB |
666 | /* |
667 | * If the pool version is less than SPA_VERSION_BOOTFS, | |
668 | * or the pool is still being created (version == 0), | |
669 | * the bootfs property cannot be set. | |
670 | */ | |
34dc7c2f | 671 | if (spa_version(spa) < SPA_VERSION_BOOTFS) { |
2e528b49 | 672 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
673 | break; |
674 | } | |
675 | ||
676 | /* | |
b128c09f | 677 | * Make sure the vdev config is bootable |
34dc7c2f | 678 | */ |
b128c09f | 679 | if (!vdev_is_bootable(spa->spa_root_vdev)) { |
2e528b49 | 680 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
681 | break; |
682 | } | |
683 | ||
684 | reset_bootfs = 1; | |
685 | ||
686 | error = nvpair_value_string(elem, &strval); | |
687 | ||
688 | if (!error) { | |
9ae529ec | 689 | objset_t *os; |
b128c09f | 690 | |
34dc7c2f BB |
691 | if (strval == NULL || strval[0] == '\0') { |
692 | objnum = zpool_prop_default_numeric( | |
693 | ZPOOL_PROP_BOOTFS); | |
694 | break; | |
695 | } | |
696 | ||
d1d7e268 | 697 | error = dmu_objset_hold(strval, FTAG, &os); |
619f0976 | 698 | if (error != 0) |
34dc7c2f | 699 | break; |
b128c09f | 700 | |
eaa25f1a | 701 | /* Must be ZPL. */ |
428870ff | 702 | if (dmu_objset_type(os) != DMU_OST_ZFS) { |
2e528b49 | 703 | error = SET_ERROR(ENOTSUP); |
b128c09f BB |
704 | } else { |
705 | objnum = dmu_objset_id(os); | |
706 | } | |
428870ff | 707 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
708 | } |
709 | break; | |
b128c09f | 710 | |
34dc7c2f BB |
711 | case ZPOOL_PROP_FAILUREMODE: |
712 | error = nvpair_value_uint64(elem, &intval); | |
3bfd95d5 | 713 | if (!error && intval > ZIO_FAILURE_MODE_PANIC) |
2e528b49 | 714 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
715 | |
716 | /* | |
717 | * This is a special case which only occurs when | |
718 | * the pool has completely failed. This allows | |
719 | * the user to change the in-core failmode property | |
720 | * without syncing it out to disk (I/Os might | |
721 | * currently be blocked). We do this by returning | |
722 | * EIO to the caller (spa_prop_set) to trick it | |
723 | * into thinking we encountered a property validation | |
724 | * error. | |
725 | */ | |
b128c09f | 726 | if (!error && spa_suspended(spa)) { |
34dc7c2f | 727 | spa->spa_failmode = intval; |
2e528b49 | 728 | error = SET_ERROR(EIO); |
34dc7c2f BB |
729 | } |
730 | break; | |
731 | ||
732 | case ZPOOL_PROP_CACHEFILE: | |
733 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
734 | break; | |
735 | ||
736 | if (strval[0] == '\0') | |
737 | break; | |
738 | ||
739 | if (strcmp(strval, "none") == 0) | |
740 | break; | |
741 | ||
742 | if (strval[0] != '/') { | |
2e528b49 | 743 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
744 | break; |
745 | } | |
746 | ||
747 | slash = strrchr(strval, '/'); | |
748 | ASSERT(slash != NULL); | |
749 | ||
750 | if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || | |
751 | strcmp(slash, "/..") == 0) | |
2e528b49 | 752 | error = SET_ERROR(EINVAL); |
34dc7c2f | 753 | break; |
428870ff | 754 | |
d96eb2b1 DM |
755 | case ZPOOL_PROP_COMMENT: |
756 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
757 | break; | |
758 | for (check = strval; *check != '\0'; check++) { | |
759 | if (!isprint(*check)) { | |
2e528b49 | 760 | error = SET_ERROR(EINVAL); |
d96eb2b1 DM |
761 | break; |
762 | } | |
d96eb2b1 DM |
763 | } |
764 | if (strlen(strval) > ZPROP_MAX_COMMENT) | |
2e528b49 | 765 | error = SET_ERROR(E2BIG); |
d96eb2b1 DM |
766 | break; |
767 | ||
e75c13c3 BB |
768 | default: |
769 | break; | |
34dc7c2f BB |
770 | } |
771 | ||
772 | if (error) | |
773 | break; | |
774 | } | |
775 | ||
050d720c MA |
776 | (void) nvlist_remove_all(props, |
777 | zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO)); | |
778 | ||
34dc7c2f BB |
779 | if (!error && reset_bootfs) { |
780 | error = nvlist_remove(props, | |
781 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); | |
782 | ||
783 | if (!error) { | |
784 | error = nvlist_add_uint64(props, | |
785 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); | |
786 | } | |
787 | } | |
788 | ||
789 | return (error); | |
790 | } | |
791 | ||
d164b209 BB |
792 | void |
793 | spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) | |
794 | { | |
d1807f16 | 795 | const char *cachefile; |
d164b209 BB |
796 | spa_config_dirent_t *dp; |
797 | ||
798 | if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
799 | &cachefile) != 0) | |
800 | return; | |
801 | ||
802 | dp = kmem_alloc(sizeof (spa_config_dirent_t), | |
79c76d5b | 803 | KM_SLEEP); |
d164b209 BB |
804 | |
805 | if (cachefile[0] == '\0') | |
806 | dp->scd_path = spa_strdup(spa_config_path); | |
807 | else if (strcmp(cachefile, "none") == 0) | |
808 | dp->scd_path = NULL; | |
809 | else | |
810 | dp->scd_path = spa_strdup(cachefile); | |
811 | ||
812 | list_insert_head(&spa->spa_config_list, dp); | |
813 | if (need_sync) | |
814 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
815 | } | |
816 | ||
34dc7c2f BB |
817 | int |
818 | spa_prop_set(spa_t *spa, nvlist_t *nvp) | |
819 | { | |
820 | int error; | |
9ae529ec | 821 | nvpair_t *elem = NULL; |
d164b209 | 822 | boolean_t need_sync = B_FALSE; |
34dc7c2f BB |
823 | |
824 | if ((error = spa_prop_validate(spa, nvp)) != 0) | |
825 | return (error); | |
826 | ||
d164b209 | 827 | while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { |
9ae529ec | 828 | zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); |
d164b209 | 829 | |
572e2857 BB |
830 | if (prop == ZPOOL_PROP_CACHEFILE || |
831 | prop == ZPOOL_PROP_ALTROOT || | |
832 | prop == ZPOOL_PROP_READONLY) | |
d164b209 BB |
833 | continue; |
834 | ||
8eae2d21 AJ |
835 | if (prop == ZPOOL_PROP_INVAL && |
836 | zfs_prop_user(nvpair_name(elem))) { | |
837 | need_sync = B_TRUE; | |
838 | break; | |
839 | } | |
840 | ||
31864e3d | 841 | if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { |
2a673e76 | 842 | uint64_t ver = 0; |
9ae529ec CS |
843 | |
844 | if (prop == ZPOOL_PROP_VERSION) { | |
845 | VERIFY(nvpair_value_uint64(elem, &ver) == 0); | |
846 | } else { | |
847 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
848 | ver = SPA_VERSION_FEATURES; | |
849 | need_sync = B_TRUE; | |
850 | } | |
851 | ||
852 | /* Save time if the version is already set. */ | |
853 | if (ver == spa_version(spa)) | |
854 | continue; | |
855 | ||
856 | /* | |
857 | * In addition to the pool directory object, we might | |
858 | * create the pool properties object, the features for | |
859 | * read object, the features for write object, or the | |
860 | * feature descriptions object. | |
861 | */ | |
13fe0198 | 862 | error = dsl_sync_task(spa->spa_name, NULL, |
3d45fdd6 MA |
863 | spa_sync_version, &ver, |
864 | 6, ZFS_SPACE_CHECK_RESERVED); | |
9ae529ec CS |
865 | if (error) |
866 | return (error); | |
867 | continue; | |
868 | } | |
869 | ||
d164b209 BB |
870 | need_sync = B_TRUE; |
871 | break; | |
872 | } | |
873 | ||
9ae529ec | 874 | if (need_sync) { |
13fe0198 | 875 | return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, |
3d45fdd6 | 876 | nvp, 6, ZFS_SPACE_CHECK_RESERVED)); |
9ae529ec CS |
877 | } |
878 | ||
879 | return (0); | |
34dc7c2f BB |
880 | } |
881 | ||
882 | /* | |
883 | * If the bootfs property value is dsobj, clear it. | |
884 | */ | |
885 | void | |
886 | spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) | |
887 | { | |
888 | if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { | |
889 | VERIFY(zap_remove(spa->spa_meta_objset, | |
890 | spa->spa_pool_props_object, | |
891 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); | |
892 | spa->spa_bootfs = 0; | |
893 | } | |
894 | } | |
895 | ||
3bc7e0fb | 896 | static int |
13fe0198 | 897 | spa_change_guid_check(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 898 | { |
2a8ba608 | 899 | uint64_t *newguid __maybe_unused = arg; |
13fe0198 | 900 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; |
3bc7e0fb GW |
901 | vdev_t *rvd = spa->spa_root_vdev; |
902 | uint64_t vdev_state; | |
3bc7e0fb | 903 | |
d2734cce SD |
904 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { |
905 | int error = (spa_has_checkpoint(spa)) ? | |
906 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
907 | return (SET_ERROR(error)); | |
908 | } | |
909 | ||
3bc7e0fb GW |
910 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
911 | vdev_state = rvd->vdev_state; | |
912 | spa_config_exit(spa, SCL_STATE, FTAG); | |
913 | ||
914 | if (vdev_state != VDEV_STATE_HEALTHY) | |
2e528b49 | 915 | return (SET_ERROR(ENXIO)); |
3bc7e0fb GW |
916 | |
917 | ASSERT3U(spa_guid(spa), !=, *newguid); | |
918 | ||
919 | return (0); | |
920 | } | |
921 | ||
922 | static void | |
13fe0198 | 923 | spa_change_guid_sync(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 924 | { |
13fe0198 MA |
925 | uint64_t *newguid = arg; |
926 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
3bc7e0fb GW |
927 | uint64_t oldguid; |
928 | vdev_t *rvd = spa->spa_root_vdev; | |
929 | ||
930 | oldguid = spa_guid(spa); | |
931 | ||
932 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
933 | rvd->vdev_guid = *newguid; | |
934 | rvd->vdev_guid_sum += (*newguid - oldguid); | |
935 | vdev_config_dirty(rvd); | |
936 | spa_config_exit(spa, SCL_STATE, FTAG); | |
937 | ||
6f1ffb06 | 938 | spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", |
74756182 | 939 | (u_longlong_t)oldguid, (u_longlong_t)*newguid); |
3bc7e0fb GW |
940 | } |
941 | ||
3541dc6d GA |
942 | /* |
943 | * Change the GUID for the pool. This is done so that we can later | |
944 | * re-import a pool built from a clone of our own vdevs. We will modify | |
945 | * the root vdev's guid, our own pool guid, and then mark all of our | |
946 | * vdevs dirty. Note that we must make sure that all our vdevs are | |
947 | * online when we do this, or else any vdevs that weren't present | |
948 | * would be orphaned from our pool. We are also going to issue a | |
949 | * sysevent to update any watchers. | |
950 | */ | |
951 | int | |
952 | spa_change_guid(spa_t *spa) | |
953 | { | |
3bc7e0fb GW |
954 | int error; |
955 | uint64_t guid; | |
3541dc6d | 956 | |
621dd7bb | 957 | mutex_enter(&spa->spa_vdev_top_lock); |
3bc7e0fb GW |
958 | mutex_enter(&spa_namespace_lock); |
959 | guid = spa_generate_guid(NULL); | |
3541dc6d | 960 | |
13fe0198 | 961 | error = dsl_sync_task(spa->spa_name, spa_change_guid_check, |
3d45fdd6 | 962 | spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); |
3541dc6d | 963 | |
3bc7e0fb | 964 | if (error == 0) { |
55c12724 AH |
965 | /* |
966 | * Clear the kobj flag from all the vdevs to allow | |
967 | * vdev_cache_process_kobj_evt() to post events to all the | |
968 | * vdevs since GUID is updated. | |
969 | */ | |
970 | vdev_clear_kobj_evt(spa->spa_root_vdev); | |
971 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) | |
972 | vdev_clear_kobj_evt(spa->spa_l2cache.sav_vdevs[i]); | |
973 | ||
974 | spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE); | |
12fa0466 | 975 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); |
3bc7e0fb | 976 | } |
3541dc6d | 977 | |
3bc7e0fb | 978 | mutex_exit(&spa_namespace_lock); |
621dd7bb | 979 | mutex_exit(&spa->spa_vdev_top_lock); |
3541dc6d | 980 | |
3bc7e0fb | 981 | return (error); |
3541dc6d GA |
982 | } |
983 | ||
34dc7c2f BB |
984 | /* |
985 | * ========================================================================== | |
986 | * SPA state manipulation (open/create/destroy/import/export) | |
987 | * ========================================================================== | |
988 | */ | |
989 | ||
990 | static int | |
991 | spa_error_entry_compare(const void *a, const void *b) | |
992 | { | |
ee36c709 GN |
993 | const spa_error_entry_t *sa = (const spa_error_entry_t *)a; |
994 | const spa_error_entry_t *sb = (const spa_error_entry_t *)b; | |
34dc7c2f BB |
995 | int ret; |
996 | ||
ee36c709 | 997 | ret = memcmp(&sa->se_bookmark, &sb->se_bookmark, |
5dbd68a3 | 998 | sizeof (zbookmark_phys_t)); |
34dc7c2f | 999 | |
ca577779 | 1000 | return (TREE_ISIGN(ret)); |
34dc7c2f BB |
1001 | } |
1002 | ||
1003 | /* | |
1004 | * Utility function which retrieves copies of the current logs and | |
1005 | * re-initializes them in the process. | |
1006 | */ | |
1007 | void | |
1008 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
1009 | { | |
1010 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
1011 | ||
861166b0 AZ |
1012 | memcpy(last, &spa->spa_errlist_last, sizeof (avl_tree_t)); |
1013 | memcpy(scrub, &spa->spa_errlist_scrub, sizeof (avl_tree_t)); | |
34dc7c2f BB |
1014 | |
1015 | avl_create(&spa->spa_errlist_scrub, | |
1016 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1017 | offsetof(spa_error_entry_t, se_avl)); | |
1018 | avl_create(&spa->spa_errlist_last, | |
1019 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1020 | offsetof(spa_error_entry_t, se_avl)); | |
1021 | } | |
1022 | ||
7ef5e54e AL |
1023 | static void |
1024 | spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
34dc7c2f | 1025 | { |
7ef5e54e AL |
1026 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; |
1027 | enum zti_modes mode = ztip->zti_mode; | |
1028 | uint_t value = ztip->zti_value; | |
1029 | uint_t count = ztip->zti_count; | |
1030 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7457b024 | 1031 | uint_t cpus, flags = TASKQ_DYNAMIC; |
428870ff | 1032 | boolean_t batch = B_FALSE; |
34dc7c2f | 1033 | |
e8b96c60 MA |
1034 | switch (mode) { |
1035 | case ZTI_MODE_FIXED: | |
7457b024 | 1036 | ASSERT3U(value, >, 0); |
e8b96c60 | 1037 | break; |
7ef5e54e | 1038 | |
e8b96c60 MA |
1039 | case ZTI_MODE_BATCH: |
1040 | batch = B_TRUE; | |
1041 | flags |= TASKQ_THREADS_CPU_PCT; | |
dcb6bed1 | 1042 | value = MIN(zio_taskq_batch_pct, 100); |
e8b96c60 | 1043 | break; |
7ef5e54e | 1044 | |
7457b024 AM |
1045 | case ZTI_MODE_SCALE: |
1046 | flags |= TASKQ_THREADS_CPU_PCT; | |
1047 | /* | |
1048 | * We want more taskqs to reduce lock contention, but we want | |
1049 | * less for better request ordering and CPU utilization. | |
1050 | */ | |
1051 | cpus = MAX(1, boot_ncpus * zio_taskq_batch_pct / 100); | |
1052 | if (zio_taskq_batch_tpq > 0) { | |
1053 | count = MAX(1, (cpus + zio_taskq_batch_tpq / 2) / | |
1054 | zio_taskq_batch_tpq); | |
1055 | } else { | |
1056 | /* | |
1057 | * Prefer 6 threads per taskq, but no more taskqs | |
1058 | * than threads in them on large systems. For 80%: | |
1059 | * | |
1060 | * taskq taskq total | |
1061 | * cpus taskqs percent threads threads | |
1062 | * ------- ------- ------- ------- ------- | |
1063 | * 1 1 80% 1 1 | |
1064 | * 2 1 80% 1 1 | |
1065 | * 4 1 80% 3 3 | |
1066 | * 8 2 40% 3 6 | |
1067 | * 16 3 27% 4 12 | |
1068 | * 32 5 16% 5 25 | |
1069 | * 64 7 11% 7 49 | |
1070 | * 128 10 8% 10 100 | |
1071 | * 256 14 6% 15 210 | |
1072 | */ | |
1073 | count = 1 + cpus / 6; | |
1074 | while (count * count > cpus) | |
1075 | count--; | |
1076 | } | |
1077 | /* Limit each taskq within 100% to not trigger assertion. */ | |
1078 | count = MAX(count, (zio_taskq_batch_pct + 99) / 100); | |
1079 | value = (zio_taskq_batch_pct + count / 2) / count; | |
1080 | break; | |
1081 | ||
1082 | case ZTI_MODE_NULL: | |
1083 | tqs->stqs_count = 0; | |
1084 | tqs->stqs_taskq = NULL; | |
1085 | return; | |
1086 | ||
e8b96c60 MA |
1087 | default: |
1088 | panic("unrecognized mode for %s_%s taskq (%u:%u) in " | |
1089 | "spa_activate()", | |
1090 | zio_type_name[t], zio_taskq_types[q], mode, value); | |
1091 | break; | |
1092 | } | |
7ef5e54e | 1093 | |
7457b024 AM |
1094 | ASSERT3U(count, >, 0); |
1095 | tqs->stqs_count = count; | |
1096 | tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); | |
1097 | ||
1c27024e | 1098 | for (uint_t i = 0; i < count; i++) { |
e8b96c60 | 1099 | taskq_t *tq; |
af430294 | 1100 | char name[32]; |
7ef5e54e | 1101 | |
7457b024 AM |
1102 | if (count > 1) |
1103 | (void) snprintf(name, sizeof (name), "%s_%s_%u", | |
1104 | zio_type_name[t], zio_taskq_types[q], i); | |
1105 | else | |
1106 | (void) snprintf(name, sizeof (name), "%s_%s", | |
1107 | zio_type_name[t], zio_taskq_types[q]); | |
7ef5e54e AL |
1108 | |
1109 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { | |
1110 | if (batch) | |
1111 | flags |= TASKQ_DC_BATCH; | |
1112 | ||
18168da7 | 1113 | (void) zio_taskq_basedc; |
7ef5e54e AL |
1114 | tq = taskq_create_sysdc(name, value, 50, INT_MAX, |
1115 | spa->spa_proc, zio_taskq_basedc, flags); | |
1116 | } else { | |
e8b96c60 MA |
1117 | pri_t pri = maxclsyspri; |
1118 | /* | |
1119 | * The write issue taskq can be extremely CPU | |
1229323d | 1120 | * intensive. Run it at slightly less important |
7432d297 MM |
1121 | * priority than the other taskqs. |
1122 | * | |
1123 | * Under Linux and FreeBSD this means incrementing | |
1124 | * the priority value as opposed to platforms like | |
1125 | * illumos where it should be decremented. | |
1126 | * | |
1127 | * On FreeBSD, if priorities divided by four (RQ_PPQ) | |
1128 | * are equal then a difference between them is | |
1129 | * insignificant. | |
e8b96c60 | 1130 | */ |
7432d297 MM |
1131 | if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) { |
1132 | #if defined(__linux__) | |
1229323d | 1133 | pri++; |
7432d297 MM |
1134 | #elif defined(__FreeBSD__) |
1135 | pri += 4; | |
1136 | #else | |
1137 | #error "unknown OS" | |
1138 | #endif | |
1139 | } | |
e8b96c60 | 1140 | tq = taskq_create_proc(name, value, pri, 50, |
7ef5e54e AL |
1141 | INT_MAX, spa->spa_proc, flags); |
1142 | } | |
1143 | ||
1144 | tqs->stqs_taskq[i] = tq; | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | static void | |
1149 | spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
1150 | { | |
1151 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7ef5e54e AL |
1152 | |
1153 | if (tqs->stqs_taskq == NULL) { | |
1154 | ASSERT3U(tqs->stqs_count, ==, 0); | |
1155 | return; | |
1156 | } | |
1157 | ||
1c27024e | 1158 | for (uint_t i = 0; i < tqs->stqs_count; i++) { |
7ef5e54e AL |
1159 | ASSERT3P(tqs->stqs_taskq[i], !=, NULL); |
1160 | taskq_destroy(tqs->stqs_taskq[i]); | |
428870ff | 1161 | } |
34dc7c2f | 1162 | |
7ef5e54e AL |
1163 | kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); |
1164 | tqs->stqs_taskq = NULL; | |
1165 | } | |
34dc7c2f | 1166 | |
7ef5e54e AL |
1167 | /* |
1168 | * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. | |
1169 | * Note that a type may have multiple discrete taskqs to avoid lock contention | |
1170 | * on the taskq itself. In that case we choose which taskq at random by using | |
1171 | * the low bits of gethrtime(). | |
1172 | */ | |
1173 | void | |
1174 | spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1175 | task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) | |
1176 | { | |
1177 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1178 | taskq_t *tq; | |
1179 | ||
1180 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1181 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1182 | ||
1183 | if (tqs->stqs_count == 1) { | |
1184 | tq = tqs->stqs_taskq[0]; | |
1185 | } else { | |
c12936b1 | 1186 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
428870ff | 1187 | } |
7ef5e54e AL |
1188 | |
1189 | taskq_dispatch_ent(tq, func, arg, flags, ent); | |
428870ff BB |
1190 | } |
1191 | ||
044baf00 BB |
1192 | /* |
1193 | * Same as spa_taskq_dispatch_ent() but block on the task until completion. | |
1194 | */ | |
1195 | void | |
1196 | spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1197 | task_func_t *func, void *arg, uint_t flags) | |
1198 | { | |
1199 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1200 | taskq_t *tq; | |
1201 | taskqid_t id; | |
1202 | ||
1203 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1204 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1205 | ||
1206 | if (tqs->stqs_count == 1) { | |
1207 | tq = tqs->stqs_taskq[0]; | |
1208 | } else { | |
c12936b1 | 1209 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
044baf00 BB |
1210 | } |
1211 | ||
1212 | id = taskq_dispatch(tq, func, arg, flags); | |
1213 | if (id) | |
1214 | taskq_wait_id(tq, id); | |
1215 | } | |
1216 | ||
428870ff BB |
1217 | static void |
1218 | spa_create_zio_taskqs(spa_t *spa) | |
1219 | { | |
1c27024e DB |
1220 | for (int t = 0; t < ZIO_TYPES; t++) { |
1221 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1222 | spa_taskqs_init(spa, t, q); |
428870ff BB |
1223 | } |
1224 | } | |
1225 | } | |
9babb374 | 1226 | |
c25b8f99 BB |
1227 | /* |
1228 | * Disabled until spa_thread() can be adapted for Linux. | |
1229 | */ | |
1230 | #undef HAVE_SPA_THREAD | |
1231 | ||
7b89a549 | 1232 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) |
428870ff BB |
1233 | static void |
1234 | spa_thread(void *arg) | |
1235 | { | |
93ce2b4c | 1236 | psetid_t zio_taskq_psrset_bind = PS_NONE; |
428870ff | 1237 | callb_cpr_t cprinfo; |
9babb374 | 1238 | |
428870ff BB |
1239 | spa_t *spa = arg; |
1240 | user_t *pu = PTOU(curproc); | |
9babb374 | 1241 | |
428870ff BB |
1242 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, |
1243 | spa->spa_name); | |
9babb374 | 1244 | |
428870ff BB |
1245 | ASSERT(curproc != &p0); |
1246 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
1247 | "zpool-%s", spa->spa_name); | |
1248 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
1249 | ||
1250 | /* bind this thread to the requested psrset */ | |
1251 | if (zio_taskq_psrset_bind != PS_NONE) { | |
1252 | pool_lock(); | |
1253 | mutex_enter(&cpu_lock); | |
1254 | mutex_enter(&pidlock); | |
1255 | mutex_enter(&curproc->p_lock); | |
1256 | ||
1257 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
1258 | 0, NULL, NULL) == 0) { | |
1259 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
1260 | } else { | |
1261 | cmn_err(CE_WARN, | |
1262 | "Couldn't bind process for zfs pool \"%s\" to " | |
1263 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
1264 | } | |
1265 | ||
1266 | mutex_exit(&curproc->p_lock); | |
1267 | mutex_exit(&pidlock); | |
1268 | mutex_exit(&cpu_lock); | |
1269 | pool_unlock(); | |
1270 | } | |
1271 | ||
1272 | if (zio_taskq_sysdc) { | |
1273 | sysdc_thread_enter(curthread, 100, 0); | |
1274 | } | |
1275 | ||
1276 | spa->spa_proc = curproc; | |
1277 | spa->spa_did = curthread->t_did; | |
1278 | ||
1279 | spa_create_zio_taskqs(spa); | |
1280 | ||
1281 | mutex_enter(&spa->spa_proc_lock); | |
1282 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
1283 | ||
1284 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
1285 | cv_broadcast(&spa->spa_proc_cv); | |
1286 | ||
1287 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
1288 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
1289 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1290 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
1291 | ||
1292 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
1293 | spa->spa_proc_state = SPA_PROC_GONE; | |
1294 | spa->spa_proc = &p0; | |
1295 | cv_broadcast(&spa->spa_proc_cv); | |
1296 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
1297 | ||
1298 | mutex_enter(&curproc->p_lock); | |
1299 | lwp_exit(); | |
1300 | } | |
1301 | #endif | |
1302 | ||
95f71c01 EN |
1303 | extern metaslab_ops_t *metaslab_allocator(spa_t *spa); |
1304 | ||
428870ff BB |
1305 | /* |
1306 | * Activate an uninitialized pool. | |
1307 | */ | |
1308 | static void | |
da92d5cb | 1309 | spa_activate(spa_t *spa, spa_mode_t mode) |
428870ff | 1310 | { |
95f71c01 | 1311 | metaslab_ops_t *msp = metaslab_allocator(spa); |
428870ff BB |
1312 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); |
1313 | ||
1314 | spa->spa_state = POOL_STATE_ACTIVE; | |
1315 | spa->spa_mode = mode; | |
e39fe05b | 1316 | spa->spa_read_spacemaps = spa_mode_readable_spacemaps; |
428870ff | 1317 | |
95f71c01 EN |
1318 | spa->spa_normal_class = metaslab_class_create(spa, msp); |
1319 | spa->spa_log_class = metaslab_class_create(spa, msp); | |
1320 | spa->spa_embedded_log_class = metaslab_class_create(spa, msp); | |
1321 | spa->spa_special_class = metaslab_class_create(spa, msp); | |
1322 | spa->spa_dedup_class = metaslab_class_create(spa, msp); | |
428870ff BB |
1323 | |
1324 | /* Try to create a covering process */ | |
1325 | mutex_enter(&spa->spa_proc_lock); | |
1326 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
1327 | ASSERT(spa->spa_proc == &p0); | |
1328 | spa->spa_did = 0; | |
1329 | ||
18168da7 | 1330 | (void) spa_create_process; |
7b89a549 | 1331 | #ifdef HAVE_SPA_THREAD |
428870ff BB |
1332 | /* Only create a process if we're going to be around a while. */ |
1333 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
1334 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
1335 | NULL, 0) == 0) { | |
1336 | spa->spa_proc_state = SPA_PROC_CREATED; | |
1337 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
1338 | cv_wait(&spa->spa_proc_cv, | |
1339 | &spa->spa_proc_lock); | |
9babb374 | 1340 | } |
428870ff BB |
1341 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); |
1342 | ASSERT(spa->spa_proc != &p0); | |
1343 | ASSERT(spa->spa_did != 0); | |
1344 | } else { | |
1345 | #ifdef _KERNEL | |
1346 | cmn_err(CE_WARN, | |
1347 | "Couldn't create process for zfs pool \"%s\"\n", | |
1348 | spa->spa_name); | |
1349 | #endif | |
b128c09f | 1350 | } |
34dc7c2f | 1351 | } |
7b89a549 | 1352 | #endif /* HAVE_SPA_THREAD */ |
428870ff BB |
1353 | mutex_exit(&spa->spa_proc_lock); |
1354 | ||
1355 | /* If we didn't create a process, we need to create our taskqs. */ | |
1356 | if (spa->spa_proc == &p0) { | |
1357 | spa_create_zio_taskqs(spa); | |
1358 | } | |
34dc7c2f | 1359 | |
619f0976 GW |
1360 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1361 | spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, | |
1362 | ZIO_FLAG_CANFAIL); | |
1363 | } | |
a1d477c2 | 1364 | |
b128c09f BB |
1365 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), |
1366 | offsetof(vdev_t, vdev_config_dirty_node)); | |
0c66c32d JG |
1367 | list_create(&spa->spa_evicting_os_list, sizeof (objset_t), |
1368 | offsetof(objset_t, os_evicting_node)); | |
b128c09f BB |
1369 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), |
1370 | offsetof(vdev_t, vdev_state_dirty_node)); | |
34dc7c2f | 1371 | |
4747a7d3 | 1372 | txg_list_create(&spa->spa_vdev_txg_list, spa, |
34dc7c2f BB |
1373 | offsetof(struct vdev, vdev_txg_node)); |
1374 | ||
1375 | avl_create(&spa->spa_errlist_scrub, | |
1376 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1377 | offsetof(spa_error_entry_t, se_avl)); | |
1378 | avl_create(&spa->spa_errlist_last, | |
1379 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1380 | offsetof(spa_error_entry_t, se_avl)); | |
e8cf3a4f AP |
1381 | avl_create(&spa->spa_errlist_healed, |
1382 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1383 | offsetof(spa_error_entry_t, se_avl)); | |
a0bd735a | 1384 | |
4759342a JL |
1385 | spa_activate_os(spa); |
1386 | ||
b5256303 TC |
1387 | spa_keystore_init(&spa->spa_keystore); |
1388 | ||
a0bd735a BP |
1389 | /* |
1390 | * This taskq is used to perform zvol-minor-related tasks | |
1391 | * asynchronously. This has several advantages, including easy | |
d0249a4b | 1392 | * resolution of various deadlocks. |
a0bd735a BP |
1393 | * |
1394 | * The taskq must be single threaded to ensure tasks are always | |
1395 | * processed in the order in which they were dispatched. | |
1396 | * | |
1397 | * A taskq per pool allows one to keep the pools independent. | |
1398 | * This way if one pool is suspended, it will not impact another. | |
1399 | * | |
1400 | * The preferred location to dispatch a zvol minor task is a sync | |
1401 | * task. In this context, there is easy access to the spa_t and minimal | |
1402 | * error handling is required because the sync task must succeed. | |
1403 | */ | |
1404 | spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri, | |
1405 | 1, INT_MAX, 0); | |
1de321e6 | 1406 | |
342357cd AM |
1407 | /* |
1408 | * The taskq to preload metaslabs. | |
1409 | */ | |
1410 | spa->spa_metaslab_taskq = taskq_create("z_metaslab", | |
1411 | metaslab_preload_pct, maxclsyspri, 1, INT_MAX, | |
1412 | TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); | |
1413 | ||
77d8a0f1 | 1414 | /* |
1415 | * Taskq dedicated to prefetcher threads: this is used to prevent the | |
1416 | * pool traverse code from monopolizing the global (and limited) | |
1417 | * system_taskq by inappropriately scheduling long running tasks on it. | |
1418 | */ | |
60a4c7d2 PD |
1419 | spa->spa_prefetch_taskq = taskq_create("z_prefetch", 100, |
1420 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); | |
77d8a0f1 | 1421 | |
1de321e6 JX |
1422 | /* |
1423 | * The taskq to upgrade datasets in this pool. Currently used by | |
9c5167d1 | 1424 | * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA. |
1de321e6 | 1425 | */ |
60a4c7d2 PD |
1426 | spa->spa_upgrade_taskq = taskq_create("z_upgrade", 100, |
1427 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); | |
34dc7c2f BB |
1428 | } |
1429 | ||
1430 | /* | |
1431 | * Opposite of spa_activate(). | |
1432 | */ | |
1433 | static void | |
1434 | spa_deactivate(spa_t *spa) | |
1435 | { | |
34dc7c2f BB |
1436 | ASSERT(spa->spa_sync_on == B_FALSE); |
1437 | ASSERT(spa->spa_dsl_pool == NULL); | |
1438 | ASSERT(spa->spa_root_vdev == NULL); | |
9babb374 | 1439 | ASSERT(spa->spa_async_zio_root == NULL); |
34dc7c2f BB |
1440 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); |
1441 | ||
0c66c32d JG |
1442 | spa_evicting_os_wait(spa); |
1443 | ||
a0bd735a BP |
1444 | if (spa->spa_zvol_taskq) { |
1445 | taskq_destroy(spa->spa_zvol_taskq); | |
1446 | spa->spa_zvol_taskq = NULL; | |
1447 | } | |
1448 | ||
342357cd AM |
1449 | if (spa->spa_metaslab_taskq) { |
1450 | taskq_destroy(spa->spa_metaslab_taskq); | |
1451 | spa->spa_metaslab_taskq = NULL; | |
1452 | } | |
1453 | ||
77d8a0f1 | 1454 | if (spa->spa_prefetch_taskq) { |
1455 | taskq_destroy(spa->spa_prefetch_taskq); | |
1456 | spa->spa_prefetch_taskq = NULL; | |
1457 | } | |
1458 | ||
1de321e6 JX |
1459 | if (spa->spa_upgrade_taskq) { |
1460 | taskq_destroy(spa->spa_upgrade_taskq); | |
1461 | spa->spa_upgrade_taskq = NULL; | |
1462 | } | |
1463 | ||
34dc7c2f BB |
1464 | txg_list_destroy(&spa->spa_vdev_txg_list); |
1465 | ||
b128c09f | 1466 | list_destroy(&spa->spa_config_dirty_list); |
0c66c32d | 1467 | list_destroy(&spa->spa_evicting_os_list); |
b128c09f | 1468 | list_destroy(&spa->spa_state_dirty_list); |
34dc7c2f | 1469 | |
57ddcda1 | 1470 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 | 1471 | |
1c27024e DB |
1472 | for (int t = 0; t < ZIO_TYPES; t++) { |
1473 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1474 | spa_taskqs_fini(spa, t, q); |
b128c09f | 1475 | } |
34dc7c2f BB |
1476 | } |
1477 | ||
a1d477c2 MA |
1478 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1479 | ASSERT3P(spa->spa_txg_zio[i], !=, NULL); | |
1480 | VERIFY0(zio_wait(spa->spa_txg_zio[i])); | |
1481 | spa->spa_txg_zio[i] = NULL; | |
1482 | } | |
1483 | ||
34dc7c2f BB |
1484 | metaslab_class_destroy(spa->spa_normal_class); |
1485 | spa->spa_normal_class = NULL; | |
1486 | ||
1487 | metaslab_class_destroy(spa->spa_log_class); | |
1488 | spa->spa_log_class = NULL; | |
1489 | ||
aa755b35 MA |
1490 | metaslab_class_destroy(spa->spa_embedded_log_class); |
1491 | spa->spa_embedded_log_class = NULL; | |
1492 | ||
cc99f275 DB |
1493 | metaslab_class_destroy(spa->spa_special_class); |
1494 | spa->spa_special_class = NULL; | |
1495 | ||
1496 | metaslab_class_destroy(spa->spa_dedup_class); | |
1497 | spa->spa_dedup_class = NULL; | |
1498 | ||
34dc7c2f BB |
1499 | /* |
1500 | * If this was part of an import or the open otherwise failed, we may | |
1501 | * still have errors left in the queues. Empty them just in case. | |
1502 | */ | |
1503 | spa_errlog_drain(spa); | |
34dc7c2f BB |
1504 | avl_destroy(&spa->spa_errlist_scrub); |
1505 | avl_destroy(&spa->spa_errlist_last); | |
e8cf3a4f | 1506 | avl_destroy(&spa->spa_errlist_healed); |
34dc7c2f | 1507 | |
b5256303 TC |
1508 | spa_keystore_fini(&spa->spa_keystore); |
1509 | ||
34dc7c2f | 1510 | spa->spa_state = POOL_STATE_UNINITIALIZED; |
428870ff BB |
1511 | |
1512 | mutex_enter(&spa->spa_proc_lock); | |
1513 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1514 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1515 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1516 | cv_broadcast(&spa->spa_proc_cv); | |
1517 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1518 | ASSERT(spa->spa_proc != &p0); | |
1519 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1520 | } | |
1521 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1522 | spa->spa_proc_state = SPA_PROC_NONE; | |
1523 | } | |
1524 | ASSERT(spa->spa_proc == &p0); | |
1525 | mutex_exit(&spa->spa_proc_lock); | |
1526 | ||
1527 | /* | |
1528 | * We want to make sure spa_thread() has actually exited the ZFS | |
1529 | * module, so that the module can't be unloaded out from underneath | |
1530 | * it. | |
1531 | */ | |
1532 | if (spa->spa_did != 0) { | |
1533 | thread_join(spa->spa_did); | |
1534 | spa->spa_did = 0; | |
1535 | } | |
4759342a JL |
1536 | |
1537 | spa_deactivate_os(spa); | |
1538 | ||
34dc7c2f BB |
1539 | } |
1540 | ||
1541 | /* | |
1542 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1543 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1544 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1545 | * All vdev validation is done by the vdev_alloc() routine. | |
1546 | */ | |
4a22ba5b | 1547 | int |
34dc7c2f BB |
1548 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, |
1549 | uint_t id, int atype) | |
1550 | { | |
1551 | nvlist_t **child; | |
9babb374 | 1552 | uint_t children; |
34dc7c2f BB |
1553 | int error; |
1554 | ||
1555 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1556 | return (error); | |
1557 | ||
1558 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1559 | return (0); | |
1560 | ||
b128c09f BB |
1561 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1562 | &child, &children); | |
1563 | ||
1564 | if (error == ENOENT) | |
1565 | return (0); | |
1566 | ||
1567 | if (error) { | |
34dc7c2f BB |
1568 | vdev_free(*vdp); |
1569 | *vdp = NULL; | |
2e528b49 | 1570 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1571 | } |
1572 | ||
1c27024e | 1573 | for (int c = 0; c < children; c++) { |
34dc7c2f BB |
1574 | vdev_t *vd; |
1575 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1576 | atype)) != 0) { | |
1577 | vdev_free(*vdp); | |
1578 | *vdp = NULL; | |
1579 | return (error); | |
1580 | } | |
1581 | } | |
1582 | ||
1583 | ASSERT(*vdp != NULL); | |
1584 | ||
1585 | return (0); | |
1586 | } | |
1587 | ||
93e28d66 SD |
1588 | static boolean_t |
1589 | spa_should_flush_logs_on_unload(spa_t *spa) | |
1590 | { | |
1591 | if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) | |
1592 | return (B_FALSE); | |
1593 | ||
1594 | if (!spa_writeable(spa)) | |
1595 | return (B_FALSE); | |
1596 | ||
1597 | if (!spa->spa_sync_on) | |
1598 | return (B_FALSE); | |
1599 | ||
1600 | if (spa_state(spa) != POOL_STATE_EXPORTED) | |
1601 | return (B_FALSE); | |
1602 | ||
1603 | if (zfs_keep_log_spacemaps_at_export) | |
1604 | return (B_FALSE); | |
1605 | ||
1606 | return (B_TRUE); | |
1607 | } | |
1608 | ||
1609 | /* | |
1610 | * Opens a transaction that will set the flag that will instruct | |
1611 | * spa_sync to attempt to flush all the metaslabs for that txg. | |
1612 | */ | |
1613 | static void | |
1614 | spa_unload_log_sm_flush_all(spa_t *spa) | |
1615 | { | |
1616 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
1617 | VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); | |
1618 | ||
1619 | ASSERT3U(spa->spa_log_flushall_txg, ==, 0); | |
1620 | spa->spa_log_flushall_txg = dmu_tx_get_txg(tx); | |
1621 | ||
1622 | dmu_tx_commit(tx); | |
1623 | txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg); | |
1624 | } | |
1625 | ||
1626 | static void | |
1627 | spa_unload_log_sm_metadata(spa_t *spa) | |
1628 | { | |
1629 | void *cookie = NULL; | |
1630 | spa_log_sm_t *sls; | |
b3ad3f48 AM |
1631 | log_summary_entry_t *e; |
1632 | ||
93e28d66 SD |
1633 | while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg, |
1634 | &cookie)) != NULL) { | |
1635 | VERIFY0(sls->sls_mscount); | |
1636 | kmem_free(sls, sizeof (spa_log_sm_t)); | |
1637 | } | |
1638 | ||
b3ad3f48 | 1639 | while ((e = list_remove_head(&spa->spa_log_summary)) != NULL) { |
93e28d66 | 1640 | VERIFY0(e->lse_mscount); |
93e28d66 SD |
1641 | kmem_free(e, sizeof (log_summary_entry_t)); |
1642 | } | |
1643 | ||
1644 | spa->spa_unflushed_stats.sus_nblocks = 0; | |
1645 | spa->spa_unflushed_stats.sus_memused = 0; | |
1646 | spa->spa_unflushed_stats.sus_blocklimit = 0; | |
1647 | } | |
1648 | ||
37f03da8 SH |
1649 | static void |
1650 | spa_destroy_aux_threads(spa_t *spa) | |
1651 | { | |
1652 | if (spa->spa_condense_zthr != NULL) { | |
1653 | zthr_destroy(spa->spa_condense_zthr); | |
1654 | spa->spa_condense_zthr = NULL; | |
1655 | } | |
1656 | if (spa->spa_checkpoint_discard_zthr != NULL) { | |
1657 | zthr_destroy(spa->spa_checkpoint_discard_zthr); | |
1658 | spa->spa_checkpoint_discard_zthr = NULL; | |
1659 | } | |
1660 | if (spa->spa_livelist_delete_zthr != NULL) { | |
1661 | zthr_destroy(spa->spa_livelist_delete_zthr); | |
1662 | spa->spa_livelist_delete_zthr = NULL; | |
1663 | } | |
1664 | if (spa->spa_livelist_condense_zthr != NULL) { | |
1665 | zthr_destroy(spa->spa_livelist_condense_zthr); | |
1666 | spa->spa_livelist_condense_zthr = NULL; | |
1667 | } | |
1668 | } | |
1669 | ||
34dc7c2f BB |
1670 | /* |
1671 | * Opposite of spa_load(). | |
1672 | */ | |
1673 | static void | |
1674 | spa_unload(spa_t *spa) | |
1675 | { | |
b128c09f | 1676 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
93e28d66 | 1677 | ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED); |
b128c09f | 1678 | |
ca95f70d | 1679 | spa_import_progress_remove(spa_guid(spa)); |
4a0ee12a PZ |
1680 | spa_load_note(spa, "UNLOADING"); |
1681 | ||
e60e158e JG |
1682 | spa_wake_waiters(spa); |
1683 | ||
93e28d66 | 1684 | /* |
2fb52853 GA |
1685 | * If we have set the spa_final_txg, we have already performed the |
1686 | * tasks below in spa_export_common(). We should not redo it here since | |
1687 | * we delay the final TXGs beyond what spa_final_txg is set at. | |
93e28d66 | 1688 | */ |
2fb52853 GA |
1689 | if (spa->spa_final_txg == UINT64_MAX) { |
1690 | /* | |
1691 | * If the log space map feature is enabled and the pool is | |
1692 | * getting exported (but not destroyed), we want to spend some | |
1693 | * time flushing as many metaslabs as we can in an attempt to | |
1694 | * destroy log space maps and save import time. | |
1695 | */ | |
1696 | if (spa_should_flush_logs_on_unload(spa)) | |
1697 | spa_unload_log_sm_flush_all(spa); | |
93e28d66 | 1698 | |
2fb52853 GA |
1699 | /* |
1700 | * Stop async tasks. | |
1701 | */ | |
1702 | spa_async_suspend(spa); | |
34dc7c2f | 1703 | |
2fb52853 GA |
1704 | if (spa->spa_root_vdev) { |
1705 | vdev_t *root_vdev = spa->spa_root_vdev; | |
1706 | vdev_initialize_stop_all(root_vdev, | |
1707 | VDEV_INITIALIZE_ACTIVE); | |
1708 | vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE); | |
1709 | vdev_autotrim_stop_all(spa); | |
1710 | vdev_rebuild_stop_all(spa); | |
1711 | } | |
619f0976 GW |
1712 | } |
1713 | ||
34dc7c2f BB |
1714 | /* |
1715 | * Stop syncing. | |
1716 | */ | |
1717 | if (spa->spa_sync_on) { | |
1718 | txg_sync_stop(spa->spa_dsl_pool); | |
1719 | spa->spa_sync_on = B_FALSE; | |
1720 | } | |
1721 | ||
4e21fd06 | 1722 | /* |
93e28d66 SD |
1723 | * This ensures that there is no async metaslab prefetching |
1724 | * while we attempt to unload the spa. | |
4e21fd06 | 1725 | */ |
342357cd | 1726 | taskq_wait(spa->spa_metaslab_taskq); |
4e21fd06 | 1727 | |
379ca9cf OF |
1728 | if (spa->spa_mmp.mmp_thread) |
1729 | mmp_thread_stop(spa); | |
1730 | ||
34dc7c2f | 1731 | /* |
b128c09f | 1732 | * Wait for any outstanding async I/O to complete. |
34dc7c2f | 1733 | */ |
9babb374 | 1734 | if (spa->spa_async_zio_root != NULL) { |
1c27024e | 1735 | for (int i = 0; i < max_ncpus; i++) |
e022864d MA |
1736 | (void) zio_wait(spa->spa_async_zio_root[i]); |
1737 | kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); | |
9babb374 BB |
1738 | spa->spa_async_zio_root = NULL; |
1739 | } | |
34dc7c2f | 1740 | |
a1d477c2 MA |
1741 | if (spa->spa_vdev_removal != NULL) { |
1742 | spa_vdev_removal_destroy(spa->spa_vdev_removal); | |
1743 | spa->spa_vdev_removal = NULL; | |
1744 | } | |
1745 | ||
37f03da8 | 1746 | spa_destroy_aux_threads(spa); |
d2734cce | 1747 | |
a1d477c2 MA |
1748 | spa_condense_fini(spa); |
1749 | ||
428870ff BB |
1750 | bpobj_close(&spa->spa_deferred_bpobj); |
1751 | ||
619f0976 | 1752 | spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); |
93cf2076 GW |
1753 | |
1754 | /* | |
1755 | * Close all vdevs. | |
1756 | */ | |
1757 | if (spa->spa_root_vdev) | |
1758 | vdev_free(spa->spa_root_vdev); | |
1759 | ASSERT(spa->spa_root_vdev == NULL); | |
1760 | ||
34dc7c2f BB |
1761 | /* |
1762 | * Close the dsl pool. | |
1763 | */ | |
1764 | if (spa->spa_dsl_pool) { | |
1765 | dsl_pool_close(spa->spa_dsl_pool); | |
1766 | spa->spa_dsl_pool = NULL; | |
428870ff | 1767 | spa->spa_meta_objset = NULL; |
34dc7c2f BB |
1768 | } |
1769 | ||
428870ff | 1770 | ddt_unload(spa); |
67a1b037 | 1771 | brt_unload(spa); |
93e28d66 | 1772 | spa_unload_log_sm_metadata(spa); |
428870ff | 1773 | |
fb5f0bc8 BB |
1774 | /* |
1775 | * Drop and purge level 2 cache | |
1776 | */ | |
1777 | spa_l2cache_drop(spa); | |
1778 | ||
34dc7c2f | 1779 | if (spa->spa_spares.sav_vdevs) { |
cfb49616 RY |
1780 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
1781 | vdev_free(spa->spa_spares.sav_vdevs[i]); | |
34dc7c2f BB |
1782 | kmem_free(spa->spa_spares.sav_vdevs, |
1783 | spa->spa_spares.sav_count * sizeof (void *)); | |
1784 | spa->spa_spares.sav_vdevs = NULL; | |
1785 | } | |
1786 | if (spa->spa_spares.sav_config) { | |
1787 | nvlist_free(spa->spa_spares.sav_config); | |
1788 | spa->spa_spares.sav_config = NULL; | |
1789 | } | |
b128c09f | 1790 | spa->spa_spares.sav_count = 0; |
34dc7c2f | 1791 | |
34dc7c2f | 1792 | if (spa->spa_l2cache.sav_vdevs) { |
cfb49616 RY |
1793 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { |
1794 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); | |
1795 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); | |
1796 | } | |
34dc7c2f BB |
1797 | kmem_free(spa->spa_l2cache.sav_vdevs, |
1798 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1799 | spa->spa_l2cache.sav_vdevs = NULL; | |
1800 | } | |
1801 | if (spa->spa_l2cache.sav_config) { | |
1802 | nvlist_free(spa->spa_l2cache.sav_config); | |
1803 | spa->spa_l2cache.sav_config = NULL; | |
1804 | } | |
b128c09f | 1805 | spa->spa_l2cache.sav_count = 0; |
34dc7c2f BB |
1806 | |
1807 | spa->spa_async_suspended = 0; | |
fb5f0bc8 | 1808 | |
a1d477c2 MA |
1809 | spa->spa_indirect_vdevs_loaded = B_FALSE; |
1810 | ||
d96eb2b1 DM |
1811 | if (spa->spa_comment != NULL) { |
1812 | spa_strfree(spa->spa_comment); | |
1813 | spa->spa_comment = NULL; | |
1814 | } | |
658fb802 CB |
1815 | if (spa->spa_compatibility != NULL) { |
1816 | spa_strfree(spa->spa_compatibility); | |
1817 | spa->spa_compatibility = NULL; | |
1818 | } | |
d96eb2b1 | 1819 | |
619f0976 | 1820 | spa_config_exit(spa, SCL_ALL, spa); |
34dc7c2f BB |
1821 | } |
1822 | ||
1823 | /* | |
1824 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1825 | * this pool. When this is called, we have some form of basic information in | |
1826 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1827 | * then re-generate a more complete list including status information. | |
1828 | */ | |
a1d477c2 | 1829 | void |
34dc7c2f BB |
1830 | spa_load_spares(spa_t *spa) |
1831 | { | |
1832 | nvlist_t **spares; | |
1833 | uint_t nspares; | |
1834 | int i; | |
1835 | vdev_t *vd, *tvd; | |
1836 | ||
d2734cce SD |
1837 | #ifndef _KERNEL |
1838 | /* | |
1839 | * zdb opens both the current state of the pool and the | |
1840 | * checkpointed state (if present), with a different spa_t. | |
1841 | * | |
1842 | * As spare vdevs are shared among open pools, we skip loading | |
1843 | * them when we load the checkpointed state of the pool. | |
1844 | */ | |
1845 | if (!spa_writeable(spa)) | |
1846 | return; | |
1847 | #endif | |
1848 | ||
b128c09f BB |
1849 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1850 | ||
34dc7c2f BB |
1851 | /* |
1852 | * First, close and free any existing spare vdevs. | |
1853 | */ | |
cfb49616 RY |
1854 | if (spa->spa_spares.sav_vdevs) { |
1855 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1856 | vd = spa->spa_spares.sav_vdevs[i]; | |
1857 | ||
1858 | /* Undo the call to spa_activate() below */ | |
1859 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, | |
1860 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
1861 | spa_spare_remove(tvd); | |
1862 | vdev_close(vd); | |
1863 | vdev_free(vd); | |
1864 | } | |
34dc7c2f | 1865 | |
34dc7c2f BB |
1866 | kmem_free(spa->spa_spares.sav_vdevs, |
1867 | spa->spa_spares.sav_count * sizeof (void *)); | |
cfb49616 | 1868 | } |
34dc7c2f BB |
1869 | |
1870 | if (spa->spa_spares.sav_config == NULL) | |
1871 | nspares = 0; | |
1872 | else | |
65ad5d11 AJ |
1873 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, |
1874 | ZPOOL_CONFIG_SPARES, &spares, &nspares)); | |
34dc7c2f BB |
1875 | |
1876 | spa->spa_spares.sav_count = (int)nspares; | |
1877 | spa->spa_spares.sav_vdevs = NULL; | |
1878 | ||
1879 | if (nspares == 0) | |
1880 | return; | |
1881 | ||
1882 | /* | |
1883 | * Construct the array of vdevs, opening them to get status in the | |
1884 | * process. For each spare, there is potentially two different vdev_t | |
1885 | * structures associated with it: one in the list of spares (used only | |
1886 | * for basic validation purposes) and one in the active vdev | |
1887 | * configuration (if it's spared in). During this phase we open and | |
1888 | * validate each vdev on the spare list. If the vdev also exists in the | |
1889 | * active configuration, then we also mark this vdev as an active spare. | |
1890 | */ | |
904ea276 | 1891 | spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *), |
79c76d5b | 1892 | KM_SLEEP); |
34dc7c2f BB |
1893 | for (i = 0; i < spa->spa_spares.sav_count; i++) { |
1894 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1895 | VDEV_ALLOC_SPARE) == 0); | |
1896 | ASSERT(vd != NULL); | |
1897 | ||
1898 | spa->spa_spares.sav_vdevs[i] = vd; | |
1899 | ||
b128c09f BB |
1900 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1901 | B_FALSE)) != NULL) { | |
34dc7c2f BB |
1902 | if (!tvd->vdev_isspare) |
1903 | spa_spare_add(tvd); | |
1904 | ||
1905 | /* | |
1906 | * We only mark the spare active if we were successfully | |
1907 | * able to load the vdev. Otherwise, importing a pool | |
1908 | * with a bad active spare would result in strange | |
1909 | * behavior, because multiple pool would think the spare | |
1910 | * is actively in use. | |
1911 | * | |
1912 | * There is a vulnerability here to an equally bizarre | |
1913 | * circumstance, where a dead active spare is later | |
1914 | * brought back to life (onlined or otherwise). Given | |
1915 | * the rarity of this scenario, and the extra complexity | |
1916 | * it adds, we ignore the possibility. | |
1917 | */ | |
1918 | if (!vdev_is_dead(tvd)) | |
1919 | spa_spare_activate(tvd); | |
1920 | } | |
1921 | ||
b128c09f | 1922 | vd->vdev_top = vd; |
9babb374 | 1923 | vd->vdev_aux = &spa->spa_spares; |
b128c09f | 1924 | |
34dc7c2f BB |
1925 | if (vdev_open(vd) != 0) |
1926 | continue; | |
1927 | ||
34dc7c2f BB |
1928 | if (vdev_validate_aux(vd) == 0) |
1929 | spa_spare_add(vd); | |
1930 | } | |
1931 | ||
1932 | /* | |
1933 | * Recompute the stashed list of spares, with status information | |
1934 | * this time. | |
1935 | */ | |
65ad5d11 | 1936 | fnvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES); |
34dc7c2f BB |
1937 | |
1938 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
79c76d5b | 1939 | KM_SLEEP); |
34dc7c2f BB |
1940 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1941 | spares[i] = vdev_config_generate(spa, | |
428870ff | 1942 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); |
65ad5d11 | 1943 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, |
795075e6 PD |
1944 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
1945 | spa->spa_spares.sav_count); | |
34dc7c2f BB |
1946 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1947 | nvlist_free(spares[i]); | |
1948 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1949 | } | |
1950 | ||
1951 | /* | |
1952 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1953 | * this pool. When this is called, we have some form of basic information in | |
1954 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1955 | * then re-generate a more complete list including status information. | |
1956 | * Devices which are already active have their details maintained, and are | |
1957 | * not re-opened. | |
1958 | */ | |
a1d477c2 | 1959 | void |
34dc7c2f BB |
1960 | spa_load_l2cache(spa_t *spa) |
1961 | { | |
460f239e | 1962 | nvlist_t **l2cache = NULL; |
34dc7c2f BB |
1963 | uint_t nl2cache; |
1964 | int i, j, oldnvdevs; | |
9babb374 | 1965 | uint64_t guid; |
a117a6d6 | 1966 | vdev_t *vd, **oldvdevs, **newvdevs; |
34dc7c2f BB |
1967 | spa_aux_vdev_t *sav = &spa->spa_l2cache; |
1968 | ||
d2734cce SD |
1969 | #ifndef _KERNEL |
1970 | /* | |
1971 | * zdb opens both the current state of the pool and the | |
1972 | * checkpointed state (if present), with a different spa_t. | |
1973 | * | |
1974 | * As L2 caches are part of the ARC which is shared among open | |
1975 | * pools, we skip loading them when we load the checkpointed | |
1976 | * state of the pool. | |
1977 | */ | |
1978 | if (!spa_writeable(spa)) | |
1979 | return; | |
1980 | #endif | |
1981 | ||
b128c09f BB |
1982 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1983 | ||
34dc7c2f BB |
1984 | oldvdevs = sav->sav_vdevs; |
1985 | oldnvdevs = sav->sav_count; | |
1986 | sav->sav_vdevs = NULL; | |
1987 | sav->sav_count = 0; | |
1988 | ||
67d60824 NB |
1989 | if (sav->sav_config == NULL) { |
1990 | nl2cache = 0; | |
1991 | newvdevs = NULL; | |
1992 | goto out; | |
1993 | } | |
1994 | ||
65ad5d11 AJ |
1995 | VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config, |
1996 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache)); | |
67d60824 NB |
1997 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); |
1998 | ||
34dc7c2f BB |
1999 | /* |
2000 | * Process new nvlist of vdevs. | |
2001 | */ | |
2002 | for (i = 0; i < nl2cache; i++) { | |
65ad5d11 | 2003 | guid = fnvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID); |
34dc7c2f BB |
2004 | |
2005 | newvdevs[i] = NULL; | |
2006 | for (j = 0; j < oldnvdevs; j++) { | |
2007 | vd = oldvdevs[j]; | |
2008 | if (vd != NULL && guid == vd->vdev_guid) { | |
2009 | /* | |
2010 | * Retain previous vdev for add/remove ops. | |
2011 | */ | |
2012 | newvdevs[i] = vd; | |
2013 | oldvdevs[j] = NULL; | |
2014 | break; | |
2015 | } | |
2016 | } | |
2017 | ||
2018 | if (newvdevs[i] == NULL) { | |
2019 | /* | |
2020 | * Create new vdev | |
2021 | */ | |
2022 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
2023 | VDEV_ALLOC_L2CACHE) == 0); | |
2024 | ASSERT(vd != NULL); | |
2025 | newvdevs[i] = vd; | |
2026 | ||
2027 | /* | |
2028 | * Commit this vdev as an l2cache device, | |
2029 | * even if it fails to open. | |
2030 | */ | |
2031 | spa_l2cache_add(vd); | |
2032 | ||
b128c09f BB |
2033 | vd->vdev_top = vd; |
2034 | vd->vdev_aux = sav; | |
2035 | ||
2036 | spa_l2cache_activate(vd); | |
2037 | ||
34dc7c2f BB |
2038 | if (vdev_open(vd) != 0) |
2039 | continue; | |
2040 | ||
34dc7c2f BB |
2041 | (void) vdev_validate_aux(vd); |
2042 | ||
9babb374 BB |
2043 | if (!vdev_is_dead(vd)) |
2044 | l2arc_add_vdev(spa, vd); | |
b7654bd7 GA |
2045 | |
2046 | /* | |
2047 | * Upon cache device addition to a pool or pool | |
2048 | * creation with a cache device or if the header | |
2049 | * of the device is invalid we issue an async | |
2050 | * TRIM command for the whole device which will | |
2051 | * execute if l2arc_trim_ahead > 0. | |
2052 | */ | |
2053 | spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM); | |
34dc7c2f BB |
2054 | } |
2055 | } | |
2056 | ||
67d60824 NB |
2057 | sav->sav_vdevs = newvdevs; |
2058 | sav->sav_count = (int)nl2cache; | |
2059 | ||
2060 | /* | |
2061 | * Recompute the stashed list of l2cache devices, with status | |
2062 | * information this time. | |
2063 | */ | |
65ad5d11 | 2064 | fnvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE); |
67d60824 | 2065 | |
460f239e D |
2066 | if (sav->sav_count > 0) |
2067 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), | |
2068 | KM_SLEEP); | |
67d60824 NB |
2069 | for (i = 0; i < sav->sav_count; i++) |
2070 | l2cache[i] = vdev_config_generate(spa, | |
2071 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); | |
795075e6 PD |
2072 | fnvlist_add_nvlist_array(sav->sav_config, ZPOOL_CONFIG_L2CACHE, |
2073 | (const nvlist_t * const *)l2cache, sav->sav_count); | |
67d60824 NB |
2074 | |
2075 | out: | |
34dc7c2f BB |
2076 | /* |
2077 | * Purge vdevs that were dropped | |
2078 | */ | |
cfb49616 RY |
2079 | if (oldvdevs) { |
2080 | for (i = 0; i < oldnvdevs; i++) { | |
2081 | uint64_t pool; | |
2082 | ||
2083 | vd = oldvdevs[i]; | |
2084 | if (vd != NULL) { | |
2085 | ASSERT(vd->vdev_isl2cache); | |
2086 | ||
2087 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && | |
2088 | pool != 0ULL && l2arc_vdev_present(vd)) | |
2089 | l2arc_remove_vdev(vd); | |
2090 | vdev_clear_stats(vd); | |
2091 | vdev_free(vd); | |
2092 | } | |
34dc7c2f | 2093 | } |
34dc7c2f | 2094 | |
34dc7c2f | 2095 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); |
cfb49616 | 2096 | } |
34dc7c2f | 2097 | |
34dc7c2f BB |
2098 | for (i = 0; i < sav->sav_count; i++) |
2099 | nvlist_free(l2cache[i]); | |
2100 | if (sav->sav_count) | |
2101 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
2102 | } | |
2103 | ||
2104 | static int | |
2105 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
2106 | { | |
2107 | dmu_buf_t *db; | |
2108 | char *packed = NULL; | |
2109 | size_t nvsize = 0; | |
2110 | int error; | |
2111 | *value = NULL; | |
2112 | ||
c3275b56 BB |
2113 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); |
2114 | if (error) | |
2115 | return (error); | |
2116 | ||
34dc7c2f BB |
2117 | nvsize = *(uint64_t *)db->db_data; |
2118 | dmu_buf_rele(db, FTAG); | |
2119 | ||
77aef6f6 | 2120 | packed = vmem_alloc(nvsize, KM_SLEEP); |
9babb374 BB |
2121 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, |
2122 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
2123 | if (error == 0) |
2124 | error = nvlist_unpack(packed, nvsize, value, 0); | |
77aef6f6 | 2125 | vmem_free(packed, nvsize); |
34dc7c2f BB |
2126 | |
2127 | return (error); | |
2128 | } | |
2129 | ||
6cb8e530 PZ |
2130 | /* |
2131 | * Concrete top-level vdevs that are not missing and are not logs. At every | |
2132 | * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. | |
2133 | */ | |
2134 | static uint64_t | |
2135 | spa_healthy_core_tvds(spa_t *spa) | |
2136 | { | |
2137 | vdev_t *rvd = spa->spa_root_vdev; | |
2138 | uint64_t tvds = 0; | |
2139 | ||
2140 | for (uint64_t i = 0; i < rvd->vdev_children; i++) { | |
2141 | vdev_t *vd = rvd->vdev_child[i]; | |
2142 | if (vd->vdev_islog) | |
2143 | continue; | |
2144 | if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) | |
2145 | tvds++; | |
2146 | } | |
2147 | ||
2148 | return (tvds); | |
2149 | } | |
2150 | ||
34dc7c2f BB |
2151 | /* |
2152 | * Checks to see if the given vdev could not be opened, in which case we post a | |
2153 | * sysevent to notify the autoreplace code that the device has been removed. | |
2154 | */ | |
2155 | static void | |
2156 | spa_check_removed(vdev_t *vd) | |
2157 | { | |
6cb8e530 | 2158 | for (uint64_t c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
2159 | spa_check_removed(vd->vdev_child[c]); |
2160 | ||
7011fb60 | 2161 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && |
a1d477c2 | 2162 | vdev_is_concrete(vd)) { |
fb390aaf | 2163 | zfs_post_autoreplace(vd->vdev_spa, vd); |
12fa0466 | 2164 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); |
34dc7c2f BB |
2165 | } |
2166 | } | |
2167 | ||
6cb8e530 PZ |
2168 | static int |
2169 | spa_check_for_missing_logs(spa_t *spa) | |
9babb374 | 2170 | { |
6cb8e530 | 2171 | vdev_t *rvd = spa->spa_root_vdev; |
9babb374 | 2172 | |
428870ff | 2173 | /* |
572e2857 | 2174 | * If we're doing a normal import, then build up any additional |
6cb8e530 | 2175 | * diagnostic information about missing log devices. |
572e2857 | 2176 | * We'll pass this up to the user for further processing. |
428870ff | 2177 | */ |
572e2857 BB |
2178 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { |
2179 | nvlist_t **child, *nv; | |
2180 | uint64_t idx = 0; | |
2181 | ||
160987b5 | 2182 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *), |
79c76d5b | 2183 | KM_SLEEP); |
65ad5d11 | 2184 | nv = fnvlist_alloc(); |
572e2857 | 2185 | |
6cb8e530 | 2186 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { |
572e2857 | 2187 | vdev_t *tvd = rvd->vdev_child[c]; |
572e2857 | 2188 | |
6cb8e530 PZ |
2189 | /* |
2190 | * We consider a device as missing only if it failed | |
2191 | * to open (i.e. offline or faulted is not considered | |
2192 | * as missing). | |
2193 | */ | |
2194 | if (tvd->vdev_islog && | |
2195 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
2196 | child[idx++] = vdev_config_generate(spa, tvd, | |
2197 | B_FALSE, VDEV_CONFIG_MISSING); | |
2198 | } | |
572e2857 | 2199 | } |
9babb374 | 2200 | |
6cb8e530 | 2201 | if (idx > 0) { |
795075e6 PD |
2202 | fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
2203 | (const nvlist_t * const *)child, idx); | |
6cb8e530 PZ |
2204 | fnvlist_add_nvlist(spa->spa_load_info, |
2205 | ZPOOL_CONFIG_MISSING_DEVICES, nv); | |
572e2857 | 2206 | |
6cb8e530 | 2207 | for (uint64_t i = 0; i < idx; i++) |
572e2857 BB |
2208 | nvlist_free(child[i]); |
2209 | } | |
2210 | nvlist_free(nv); | |
2211 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
572e2857 | 2212 | |
6cb8e530 PZ |
2213 | if (idx > 0) { |
2214 | spa_load_failed(spa, "some log devices are missing"); | |
db7d07e1 | 2215 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 PZ |
2216 | return (SET_ERROR(ENXIO)); |
2217 | } | |
2218 | } else { | |
2219 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
2220 | vdev_t *tvd = rvd->vdev_child[c]; | |
a1d477c2 | 2221 | |
6cb8e530 PZ |
2222 | if (tvd->vdev_islog && |
2223 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
572e2857 | 2224 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
6cb8e530 PZ |
2225 | spa_load_note(spa, "some log devices are " |
2226 | "missing, ZIL is dropped."); | |
db7d07e1 | 2227 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 | 2228 | break; |
e0ab3ab5 | 2229 | } |
572e2857 | 2230 | } |
9babb374 | 2231 | } |
e0ab3ab5 | 2232 | |
6cb8e530 | 2233 | return (0); |
9babb374 BB |
2234 | } |
2235 | ||
b128c09f BB |
2236 | /* |
2237 | * Check for missing log devices | |
2238 | */ | |
13fe0198 | 2239 | static boolean_t |
b128c09f BB |
2240 | spa_check_logs(spa_t *spa) |
2241 | { | |
13fe0198 | 2242 | boolean_t rv = B_FALSE; |
9c43027b | 2243 | dsl_pool_t *dp = spa_get_dsl(spa); |
13fe0198 | 2244 | |
b128c09f | 2245 | switch (spa->spa_log_state) { |
e75c13c3 BB |
2246 | default: |
2247 | break; | |
b128c09f BB |
2248 | case SPA_LOG_MISSING: |
2249 | /* need to recheck in case slog has been restored */ | |
2250 | case SPA_LOG_UNKNOWN: | |
9c43027b AJ |
2251 | rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, |
2252 | zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); | |
13fe0198 | 2253 | if (rv) |
428870ff | 2254 | spa_set_log_state(spa, SPA_LOG_MISSING); |
b128c09f | 2255 | break; |
b128c09f | 2256 | } |
13fe0198 | 2257 | return (rv); |
b128c09f BB |
2258 | } |
2259 | ||
aa755b35 MA |
2260 | /* |
2261 | * Passivate any log vdevs (note, does not apply to embedded log metaslabs). | |
2262 | */ | |
428870ff BB |
2263 | static boolean_t |
2264 | spa_passivate_log(spa_t *spa) | |
34dc7c2f | 2265 | { |
428870ff BB |
2266 | vdev_t *rvd = spa->spa_root_vdev; |
2267 | boolean_t slog_found = B_FALSE; | |
b128c09f | 2268 | |
428870ff | 2269 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
fb5f0bc8 | 2270 | |
1c27024e | 2271 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff | 2272 | vdev_t *tvd = rvd->vdev_child[c]; |
34dc7c2f | 2273 | |
428870ff | 2274 | if (tvd->vdev_islog) { |
aa755b35 MA |
2275 | ASSERT3P(tvd->vdev_log_mg, ==, NULL); |
2276 | metaslab_group_passivate(tvd->vdev_mg); | |
428870ff BB |
2277 | slog_found = B_TRUE; |
2278 | } | |
34dc7c2f BB |
2279 | } |
2280 | ||
428870ff BB |
2281 | return (slog_found); |
2282 | } | |
34dc7c2f | 2283 | |
aa755b35 MA |
2284 | /* |
2285 | * Activate any log vdevs (note, does not apply to embedded log metaslabs). | |
2286 | */ | |
428870ff BB |
2287 | static void |
2288 | spa_activate_log(spa_t *spa) | |
2289 | { | |
2290 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 2291 | |
428870ff BB |
2292 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
2293 | ||
1c27024e | 2294 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff | 2295 | vdev_t *tvd = rvd->vdev_child[c]; |
428870ff | 2296 | |
aa755b35 MA |
2297 | if (tvd->vdev_islog) { |
2298 | ASSERT3P(tvd->vdev_log_mg, ==, NULL); | |
2299 | metaslab_group_activate(tvd->vdev_mg); | |
2300 | } | |
34dc7c2f | 2301 | } |
428870ff | 2302 | } |
34dc7c2f | 2303 | |
428870ff | 2304 | int |
a1d477c2 | 2305 | spa_reset_logs(spa_t *spa) |
428870ff | 2306 | { |
13fe0198 | 2307 | int error; |
9babb374 | 2308 | |
a1d477c2 | 2309 | error = dmu_objset_find(spa_name(spa), zil_reset, |
13fe0198 MA |
2310 | NULL, DS_FIND_CHILDREN); |
2311 | if (error == 0) { | |
428870ff BB |
2312 | /* |
2313 | * We successfully offlined the log device, sync out the | |
2314 | * current txg so that the "stubby" block can be removed | |
2315 | * by zil_sync(). | |
2316 | */ | |
2317 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
2318 | } | |
2319 | return (error); | |
2320 | } | |
34dc7c2f | 2321 | |
428870ff BB |
2322 | static void |
2323 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
2324 | { | |
1c27024e | 2325 | for (int i = 0; i < sav->sav_count; i++) |
428870ff BB |
2326 | spa_check_removed(sav->sav_vdevs[i]); |
2327 | } | |
34dc7c2f | 2328 | |
428870ff BB |
2329 | void |
2330 | spa_claim_notify(zio_t *zio) | |
2331 | { | |
2332 | spa_t *spa = zio->io_spa; | |
34dc7c2f | 2333 | |
428870ff BB |
2334 | if (zio->io_error) |
2335 | return; | |
34dc7c2f | 2336 | |
428870ff BB |
2337 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ |
2338 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
2339 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
2340 | mutex_exit(&spa->spa_props_lock); | |
2341 | } | |
34dc7c2f | 2342 | |
428870ff | 2343 | typedef struct spa_load_error { |
f2c5bc15 | 2344 | boolean_t sle_verify_data; |
428870ff BB |
2345 | uint64_t sle_meta_count; |
2346 | uint64_t sle_data_count; | |
2347 | } spa_load_error_t; | |
34dc7c2f | 2348 | |
428870ff BB |
2349 | static void |
2350 | spa_load_verify_done(zio_t *zio) | |
2351 | { | |
2352 | blkptr_t *bp = zio->io_bp; | |
2353 | spa_load_error_t *sle = zio->io_private; | |
2354 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
2355 | int error = zio->io_error; | |
dea377c0 | 2356 | spa_t *spa = zio->io_spa; |
34dc7c2f | 2357 | |
a6255b7f | 2358 | abd_free(zio->io_abd); |
428870ff | 2359 | if (error) { |
9ae529ec | 2360 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && |
428870ff | 2361 | type != DMU_OT_INTENT_LOG) |
bc89ac84 | 2362 | atomic_inc_64(&sle->sle_meta_count); |
428870ff | 2363 | else |
bc89ac84 | 2364 | atomic_inc_64(&sle->sle_data_count); |
34dc7c2f | 2365 | } |
dea377c0 MA |
2366 | |
2367 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2368 | spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp); |
dea377c0 MA |
2369 | cv_broadcast(&spa->spa_scrub_io_cv); |
2370 | mutex_exit(&spa->spa_scrub_lock); | |
428870ff | 2371 | } |
34dc7c2f | 2372 | |
dea377c0 | 2373 | /* |
e1cfd73f | 2374 | * Maximum number of inflight bytes is the log2 fraction of the arc size. |
c8242a96 | 2375 | * By default, we set it to 1/16th of the arc. |
dea377c0 | 2376 | */ |
fdc2d303 | 2377 | static uint_t spa_load_verify_shift = 4; |
18168da7 AZ |
2378 | static int spa_load_verify_metadata = B_TRUE; |
2379 | static int spa_load_verify_data = B_TRUE; | |
dea377c0 | 2380 | |
428870ff BB |
2381 | static int |
2382 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
5dbd68a3 | 2383 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) |
428870ff | 2384 | { |
f2c5bc15 AM |
2385 | zio_t *rio = arg; |
2386 | spa_load_error_t *sle = rio->io_private; | |
2387 | ||
14e4e3cb AZ |
2388 | (void) zilog, (void) dnp; |
2389 | ||
dea377c0 MA |
2390 | /* |
2391 | * Note: normally this routine will not be called if | |
2392 | * spa_load_verify_metadata is not set. However, it may be useful | |
2393 | * to manually set the flag after the traversal has begun. | |
2394 | */ | |
2395 | if (!spa_load_verify_metadata) | |
2396 | return (0); | |
2cd0f98f BB |
2397 | |
2398 | /* | |
2399 | * Sanity check the block pointer in order to detect obvious damage | |
2400 | * before using the contents in subsequent checks or in zio_read(). | |
2401 | * When damaged consider it to be a metadata error since we cannot | |
2402 | * trust the BP_GET_TYPE and BP_GET_LEVEL values. | |
2403 | */ | |
3095ca91 | 2404 | if (!zfs_blkptr_verify(spa, bp, BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) { |
2cd0f98f BB |
2405 | atomic_inc_64(&sle->sle_meta_count); |
2406 | return (0); | |
2407 | } | |
2408 | ||
2409 | if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) || | |
2410 | BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp)) | |
2411 | return (0); | |
2412 | ||
f2c5bc15 AM |
2413 | if (!BP_IS_METADATA(bp) && |
2414 | (!spa_load_verify_data || !sle->sle_verify_data)) | |
dea377c0 MA |
2415 | return (0); |
2416 | ||
1e527162 GW |
2417 | uint64_t maxinflight_bytes = |
2418 | arc_target_bytes() >> spa_load_verify_shift; | |
1c27024e | 2419 | size_t size = BP_GET_PSIZE(bp); |
dea377c0 MA |
2420 | |
2421 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2422 | while (spa->spa_load_verify_bytes >= maxinflight_bytes) |
dea377c0 | 2423 | cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); |
c8242a96 | 2424 | spa->spa_load_verify_bytes += size; |
dea377c0 MA |
2425 | mutex_exit(&spa->spa_scrub_lock); |
2426 | ||
a6255b7f | 2427 | zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, |
dea377c0 MA |
2428 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, |
2429 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
2430 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
428870ff BB |
2431 | return (0); |
2432 | } | |
34dc7c2f | 2433 | |
65c7cc49 | 2434 | static int |
d1d19c78 PD |
2435 | verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) |
2436 | { | |
14e4e3cb AZ |
2437 | (void) dp, (void) arg; |
2438 | ||
d1d19c78 PD |
2439 | if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) |
2440 | return (SET_ERROR(ENAMETOOLONG)); | |
2441 | ||
2442 | return (0); | |
2443 | } | |
2444 | ||
428870ff BB |
2445 | static int |
2446 | spa_load_verify(spa_t *spa) | |
2447 | { | |
2448 | zio_t *rio; | |
2449 | spa_load_error_t sle = { 0 }; | |
8a393be3 | 2450 | zpool_load_policy_t policy; |
428870ff | 2451 | boolean_t verify_ok = B_FALSE; |
dea377c0 | 2452 | int error = 0; |
34dc7c2f | 2453 | |
8a393be3 | 2454 | zpool_get_load_policy(spa->spa_config, &policy); |
34dc7c2f | 2455 | |
f2c5bc15 AM |
2456 | if (policy.zlp_rewind & ZPOOL_NEVER_REWIND || |
2457 | policy.zlp_maxmeta == UINT64_MAX) | |
428870ff | 2458 | return (0); |
34dc7c2f | 2459 | |
d1d19c78 PD |
2460 | dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); |
2461 | error = dmu_objset_find_dp(spa->spa_dsl_pool, | |
2462 | spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, | |
2463 | DS_FIND_CHILDREN); | |
2464 | dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); | |
2465 | if (error != 0) | |
2466 | return (error); | |
2467 | ||
f2c5bc15 AM |
2468 | /* |
2469 | * Verify data only if we are rewinding or error limit was set. | |
2470 | * Otherwise nothing except dbgmsg care about it to waste time. | |
2471 | */ | |
2472 | sle.sle_verify_data = (policy.zlp_rewind & ZPOOL_REWIND_MASK) || | |
2473 | (policy.zlp_maxdata < UINT64_MAX); | |
2474 | ||
428870ff BB |
2475 | rio = zio_root(spa, NULL, &sle, |
2476 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
34dc7c2f | 2477 | |
dea377c0 | 2478 | if (spa_load_verify_metadata) { |
4a0ee12a PZ |
2479 | if (spa->spa_extreme_rewind) { |
2480 | spa_load_note(spa, "performing a complete scan of the " | |
2481 | "pool since extreme rewind is on. This may take " | |
2482 | "a very long time.\n (spa_load_verify_data=%u, " | |
2483 | "spa_load_verify_metadata=%u)", | |
2484 | spa_load_verify_data, spa_load_verify_metadata); | |
2485 | } | |
c8242a96 | 2486 | |
dea377c0 | 2487 | error = traverse_pool(spa, spa->spa_verify_min_txg, |
b5256303 TC |
2488 | TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | |
2489 | TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio); | |
dea377c0 | 2490 | } |
428870ff BB |
2491 | |
2492 | (void) zio_wait(rio); | |
c8242a96 | 2493 | ASSERT0(spa->spa_load_verify_bytes); |
428870ff BB |
2494 | |
2495 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
2496 | spa->spa_load_data_errors = sle.sle_data_count; | |
2497 | ||
afd2f7b7 PZ |
2498 | if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { |
2499 | spa_load_note(spa, "spa_load_verify found %llu metadata errors " | |
2500 | "and %llu data errors", (u_longlong_t)sle.sle_meta_count, | |
2501 | (u_longlong_t)sle.sle_data_count); | |
2502 | } | |
2503 | ||
2504 | if (spa_load_verify_dryrun || | |
8a393be3 PZ |
2505 | (!error && sle.sle_meta_count <= policy.zlp_maxmeta && |
2506 | sle.sle_data_count <= policy.zlp_maxdata)) { | |
572e2857 BB |
2507 | int64_t loss = 0; |
2508 | ||
428870ff BB |
2509 | verify_ok = B_TRUE; |
2510 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
2511 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
572e2857 BB |
2512 | |
2513 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
65ad5d11 AJ |
2514 | fnvlist_add_uint64(spa->spa_load_info, ZPOOL_CONFIG_LOAD_TIME, |
2515 | spa->spa_load_txg_ts); | |
2516 | fnvlist_add_int64(spa->spa_load_info, ZPOOL_CONFIG_REWIND_TIME, | |
2517 | loss); | |
f2c5bc15 AM |
2518 | fnvlist_add_uint64(spa->spa_load_info, |
2519 | ZPOOL_CONFIG_LOAD_META_ERRORS, sle.sle_meta_count); | |
65ad5d11 AJ |
2520 | fnvlist_add_uint64(spa->spa_load_info, |
2521 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count); | |
428870ff BB |
2522 | } else { |
2523 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
2524 | } | |
2525 | ||
afd2f7b7 PZ |
2526 | if (spa_load_verify_dryrun) |
2527 | return (0); | |
2528 | ||
428870ff BB |
2529 | if (error) { |
2530 | if (error != ENXIO && error != EIO) | |
2e528b49 | 2531 | error = SET_ERROR(EIO); |
428870ff BB |
2532 | return (error); |
2533 | } | |
2534 | ||
2535 | return (verify_ok ? 0 : EIO); | |
2536 | } | |
2537 | ||
2538 | /* | |
2539 | * Find a value in the pool props object. | |
2540 | */ | |
2541 | static void | |
2542 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
2543 | { | |
2544 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
2545 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
2546 | } | |
2547 | ||
2548 | /* | |
2549 | * Find a value in the pool directory object. | |
2550 | */ | |
2551 | static int | |
4a0ee12a | 2552 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) |
428870ff | 2553 | { |
4a0ee12a PZ |
2554 | int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
2555 | name, sizeof (uint64_t), 1, val); | |
2556 | ||
2557 | if (error != 0 && (error != ENOENT || log_enoent)) { | |
2558 | spa_load_failed(spa, "couldn't get '%s' value in MOS directory " | |
2559 | "[error=%d]", name, error); | |
2560 | } | |
2561 | ||
2562 | return (error); | |
428870ff BB |
2563 | } |
2564 | ||
2565 | static int | |
2566 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
2567 | { | |
2568 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
a1d477c2 | 2569 | return (SET_ERROR(err)); |
428870ff BB |
2570 | } |
2571 | ||
37f03da8 SH |
2572 | boolean_t |
2573 | spa_livelist_delete_check(spa_t *spa) | |
2574 | { | |
2575 | return (spa->spa_livelists_to_delete != 0); | |
2576 | } | |
2577 | ||
37f03da8 SH |
2578 | static boolean_t |
2579 | spa_livelist_delete_cb_check(void *arg, zthr_t *z) | |
2580 | { | |
14e4e3cb | 2581 | (void) z; |
37f03da8 SH |
2582 | spa_t *spa = arg; |
2583 | return (spa_livelist_delete_check(spa)); | |
2584 | } | |
2585 | ||
2586 | static int | |
2587 | delete_blkptr_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
2588 | { | |
2589 | spa_t *spa = arg; | |
2590 | zio_free(spa, tx->tx_txg, bp); | |
2591 | dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD, | |
2592 | -bp_get_dsize_sync(spa, bp), | |
2593 | -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx); | |
2594 | return (0); | |
2595 | } | |
2596 | ||
2597 | static int | |
2598 | dsl_get_next_livelist_obj(objset_t *os, uint64_t zap_obj, uint64_t *llp) | |
2599 | { | |
2600 | int err; | |
2601 | zap_cursor_t zc; | |
2602 | zap_attribute_t za; | |
2603 | zap_cursor_init(&zc, os, zap_obj); | |
2604 | err = zap_cursor_retrieve(&zc, &za); | |
2605 | zap_cursor_fini(&zc); | |
2606 | if (err == 0) | |
2607 | *llp = za.za_first_integer; | |
2608 | return (err); | |
2609 | } | |
2610 | ||
2611 | /* | |
2612 | * Components of livelist deletion that must be performed in syncing | |
2613 | * context: freeing block pointers and updating the pool-wide data | |
2614 | * structures to indicate how much work is left to do | |
2615 | */ | |
2616 | typedef struct sublist_delete_arg { | |
2617 | spa_t *spa; | |
2618 | dsl_deadlist_t *ll; | |
2619 | uint64_t key; | |
2620 | bplist_t *to_free; | |
2621 | } sublist_delete_arg_t; | |
2622 | ||
2623 | static void | |
2624 | sublist_delete_sync(void *arg, dmu_tx_t *tx) | |
2625 | { | |
2626 | sublist_delete_arg_t *sda = arg; | |
2627 | spa_t *spa = sda->spa; | |
2628 | dsl_deadlist_t *ll = sda->ll; | |
2629 | uint64_t key = sda->key; | |
2630 | bplist_t *to_free = sda->to_free; | |
2631 | ||
2632 | bplist_iterate(to_free, delete_blkptr_cb, spa, tx); | |
2633 | dsl_deadlist_remove_entry(ll, key, tx); | |
2634 | } | |
2635 | ||
2636 | typedef struct livelist_delete_arg { | |
2637 | spa_t *spa; | |
2638 | uint64_t ll_obj; | |
2639 | uint64_t zap_obj; | |
2640 | } livelist_delete_arg_t; | |
2641 | ||
2642 | static void | |
2643 | livelist_delete_sync(void *arg, dmu_tx_t *tx) | |
2644 | { | |
2645 | livelist_delete_arg_t *lda = arg; | |
2646 | spa_t *spa = lda->spa; | |
2647 | uint64_t ll_obj = lda->ll_obj; | |
2648 | uint64_t zap_obj = lda->zap_obj; | |
2649 | objset_t *mos = spa->spa_meta_objset; | |
2650 | uint64_t count; | |
2651 | ||
2652 | /* free the livelist and decrement the feature count */ | |
2653 | VERIFY0(zap_remove_int(mos, zap_obj, ll_obj, tx)); | |
2654 | dsl_deadlist_free(mos, ll_obj, tx); | |
2655 | spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx); | |
2656 | VERIFY0(zap_count(mos, zap_obj, &count)); | |
2657 | if (count == 0) { | |
2658 | /* no more livelists to delete */ | |
2659 | VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT, | |
2660 | DMU_POOL_DELETED_CLONES, tx)); | |
2661 | VERIFY0(zap_destroy(mos, zap_obj, tx)); | |
2662 | spa->spa_livelists_to_delete = 0; | |
e60e158e | 2663 | spa_notify_waiters(spa); |
37f03da8 SH |
2664 | } |
2665 | } | |
2666 | ||
2667 | /* | |
2668 | * Load in the value for the livelist to be removed and open it. Then, | |
2669 | * load its first sublist and determine which block pointers should actually | |
2670 | * be freed. Then, call a synctask which performs the actual frees and updates | |
2671 | * the pool-wide livelist data. | |
2672 | */ | |
65c7cc49 | 2673 | static void |
37f03da8 SH |
2674 | spa_livelist_delete_cb(void *arg, zthr_t *z) |
2675 | { | |
2676 | spa_t *spa = arg; | |
2677 | uint64_t ll_obj = 0, count; | |
2678 | objset_t *mos = spa->spa_meta_objset; | |
2679 | uint64_t zap_obj = spa->spa_livelists_to_delete; | |
2680 | /* | |
2681 | * Determine the next livelist to delete. This function should only | |
2682 | * be called if there is at least one deleted clone. | |
2683 | */ | |
2684 | VERIFY0(dsl_get_next_livelist_obj(mos, zap_obj, &ll_obj)); | |
2685 | VERIFY0(zap_count(mos, ll_obj, &count)); | |
2686 | if (count > 0) { | |
c9562576 | 2687 | dsl_deadlist_t *ll; |
37f03da8 SH |
2688 | dsl_deadlist_entry_t *dle; |
2689 | bplist_t to_free; | |
c9562576 PS |
2690 | ll = kmem_zalloc(sizeof (dsl_deadlist_t), KM_SLEEP); |
2691 | dsl_deadlist_open(ll, mos, ll_obj); | |
2692 | dle = dsl_deadlist_first(ll); | |
37f03da8 SH |
2693 | ASSERT3P(dle, !=, NULL); |
2694 | bplist_create(&to_free); | |
2695 | int err = dsl_process_sub_livelist(&dle->dle_bpobj, &to_free, | |
2696 | z, NULL); | |
2697 | if (err == 0) { | |
2698 | sublist_delete_arg_t sync_arg = { | |
2699 | .spa = spa, | |
c9562576 | 2700 | .ll = ll, |
37f03da8 SH |
2701 | .key = dle->dle_mintxg, |
2702 | .to_free = &to_free | |
2703 | }; | |
2704 | zfs_dbgmsg("deleting sublist (id %llu) from" | |
8e739b2c RE |
2705 | " livelist %llu, %lld remaining", |
2706 | (u_longlong_t)dle->dle_bpobj.bpo_object, | |
2707 | (u_longlong_t)ll_obj, (longlong_t)count - 1); | |
37f03da8 SH |
2708 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, |
2709 | sublist_delete_sync, &sync_arg, 0, | |
2710 | ZFS_SPACE_CHECK_DESTROY)); | |
2711 | } else { | |
d87676a9 | 2712 | VERIFY3U(err, ==, EINTR); |
37f03da8 SH |
2713 | } |
2714 | bplist_clear(&to_free); | |
2715 | bplist_destroy(&to_free); | |
c9562576 PS |
2716 | dsl_deadlist_close(ll); |
2717 | kmem_free(ll, sizeof (dsl_deadlist_t)); | |
37f03da8 SH |
2718 | } else { |
2719 | livelist_delete_arg_t sync_arg = { | |
2720 | .spa = spa, | |
2721 | .ll_obj = ll_obj, | |
2722 | .zap_obj = zap_obj | |
2723 | }; | |
8e739b2c RE |
2724 | zfs_dbgmsg("deletion of livelist %llu completed", |
2725 | (u_longlong_t)ll_obj); | |
37f03da8 SH |
2726 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, livelist_delete_sync, |
2727 | &sync_arg, 0, ZFS_SPACE_CHECK_DESTROY)); | |
2728 | } | |
2729 | } | |
2730 | ||
65c7cc49 | 2731 | static void |
37f03da8 SH |
2732 | spa_start_livelist_destroy_thread(spa_t *spa) |
2733 | { | |
2734 | ASSERT3P(spa->spa_livelist_delete_zthr, ==, NULL); | |
843e9ca2 SD |
2735 | spa->spa_livelist_delete_zthr = |
2736 | zthr_create("z_livelist_destroy", | |
6bc61d22 TN |
2737 | spa_livelist_delete_cb_check, spa_livelist_delete_cb, spa, |
2738 | minclsyspri); | |
37f03da8 SH |
2739 | } |
2740 | ||
2741 | typedef struct livelist_new_arg { | |
2742 | bplist_t *allocs; | |
2743 | bplist_t *frees; | |
2744 | } livelist_new_arg_t; | |
2745 | ||
2746 | static int | |
2747 | livelist_track_new_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
2748 | dmu_tx_t *tx) | |
2749 | { | |
2750 | ASSERT(tx == NULL); | |
2751 | livelist_new_arg_t *lna = arg; | |
2752 | if (bp_freed) { | |
2753 | bplist_append(lna->frees, bp); | |
2754 | } else { | |
2755 | bplist_append(lna->allocs, bp); | |
2756 | zfs_livelist_condense_new_alloc++; | |
2757 | } | |
2758 | return (0); | |
2759 | } | |
2760 | ||
2761 | typedef struct livelist_condense_arg { | |
2762 | spa_t *spa; | |
2763 | bplist_t to_keep; | |
2764 | uint64_t first_size; | |
2765 | uint64_t next_size; | |
2766 | } livelist_condense_arg_t; | |
2767 | ||
2768 | static void | |
2769 | spa_livelist_condense_sync(void *arg, dmu_tx_t *tx) | |
2770 | { | |
2771 | livelist_condense_arg_t *lca = arg; | |
2772 | spa_t *spa = lca->spa; | |
2773 | bplist_t new_frees; | |
2774 | dsl_dataset_t *ds = spa->spa_to_condense.ds; | |
2775 | ||
2776 | /* Have we been cancelled? */ | |
2777 | if (spa->spa_to_condense.cancelled) { | |
2778 | zfs_livelist_condense_sync_cancel++; | |
2779 | goto out; | |
2780 | } | |
2781 | ||
2782 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2783 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2784 | dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist; | |
2785 | ||
2786 | /* | |
2787 | * It's possible that the livelist was changed while the zthr was | |
2788 | * running. Therefore, we need to check for new blkptrs in the two | |
2789 | * entries being condensed and continue to track them in the livelist. | |
2790 | * Because of the way we handle remapped blkptrs (see dbuf_remap_impl), | |
2791 | * it's possible that the newly added blkptrs are FREEs or ALLOCs so | |
2792 | * we need to sort them into two different bplists. | |
2793 | */ | |
2794 | uint64_t first_obj = first->dle_bpobj.bpo_object; | |
2795 | uint64_t next_obj = next->dle_bpobj.bpo_object; | |
2796 | uint64_t cur_first_size = first->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2797 | uint64_t cur_next_size = next->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2798 | ||
2799 | bplist_create(&new_frees); | |
2800 | livelist_new_arg_t new_bps = { | |
2801 | .allocs = &lca->to_keep, | |
2802 | .frees = &new_frees, | |
2803 | }; | |
2804 | ||
2805 | if (cur_first_size > lca->first_size) { | |
2806 | VERIFY0(livelist_bpobj_iterate_from_nofree(&first->dle_bpobj, | |
2807 | livelist_track_new_cb, &new_bps, lca->first_size)); | |
2808 | } | |
2809 | if (cur_next_size > lca->next_size) { | |
2810 | VERIFY0(livelist_bpobj_iterate_from_nofree(&next->dle_bpobj, | |
2811 | livelist_track_new_cb, &new_bps, lca->next_size)); | |
2812 | } | |
2813 | ||
2814 | dsl_deadlist_clear_entry(first, ll, tx); | |
2815 | ASSERT(bpobj_is_empty(&first->dle_bpobj)); | |
2816 | dsl_deadlist_remove_entry(ll, next->dle_mintxg, tx); | |
2817 | ||
2818 | bplist_iterate(&lca->to_keep, dsl_deadlist_insert_alloc_cb, ll, tx); | |
2819 | bplist_iterate(&new_frees, dsl_deadlist_insert_free_cb, ll, tx); | |
2820 | bplist_destroy(&new_frees); | |
2821 | ||
2822 | char dsname[ZFS_MAX_DATASET_NAME_LEN]; | |
2823 | dsl_dataset_name(ds, dsname); | |
2824 | zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu " | |
2825 | "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu " | |
8e739b2c RE |
2826 | "(%llu blkptrs)", (u_longlong_t)tx->tx_txg, dsname, |
2827 | (u_longlong_t)ds->ds_object, (u_longlong_t)first_obj, | |
2828 | (u_longlong_t)cur_first_size, (u_longlong_t)next_obj, | |
2829 | (u_longlong_t)cur_next_size, | |
2830 | (u_longlong_t)first->dle_bpobj.bpo_object, | |
2831 | (u_longlong_t)first->dle_bpobj.bpo_phys->bpo_num_blkptrs); | |
37f03da8 SH |
2832 | out: |
2833 | dmu_buf_rele(ds->ds_dbuf, spa); | |
2834 | spa->spa_to_condense.ds = NULL; | |
2835 | bplist_clear(&lca->to_keep); | |
2836 | bplist_destroy(&lca->to_keep); | |
2837 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2838 | spa->spa_to_condense.syncing = B_FALSE; | |
2839 | } | |
2840 | ||
65c7cc49 | 2841 | static void |
37f03da8 SH |
2842 | spa_livelist_condense_cb(void *arg, zthr_t *t) |
2843 | { | |
2844 | while (zfs_livelist_condense_zthr_pause && | |
2845 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2846 | delay(1); | |
2847 | ||
2848 | spa_t *spa = arg; | |
2849 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2850 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2851 | uint64_t first_size, next_size; | |
2852 | ||
2853 | livelist_condense_arg_t *lca = | |
2854 | kmem_alloc(sizeof (livelist_condense_arg_t), KM_SLEEP); | |
2855 | bplist_create(&lca->to_keep); | |
2856 | ||
2857 | /* | |
2858 | * Process the livelists (matching FREEs and ALLOCs) in open context | |
2859 | * so we have minimal work in syncing context to condense. | |
2860 | * | |
2861 | * We save bpobj sizes (first_size and next_size) to use later in | |
2862 | * syncing context to determine if entries were added to these sublists | |
2863 | * while in open context. This is possible because the clone is still | |
2864 | * active and open for normal writes and we want to make sure the new, | |
2865 | * unprocessed blockpointers are inserted into the livelist normally. | |
2866 | * | |
2867 | * Note that dsl_process_sub_livelist() both stores the size number of | |
2868 | * blockpointers and iterates over them while the bpobj's lock held, so | |
2869 | * the sizes returned to us are consistent which what was actually | |
2870 | * processed. | |
2871 | */ | |
2872 | int err = dsl_process_sub_livelist(&first->dle_bpobj, &lca->to_keep, t, | |
2873 | &first_size); | |
2874 | if (err == 0) | |
2875 | err = dsl_process_sub_livelist(&next->dle_bpobj, &lca->to_keep, | |
2876 | t, &next_size); | |
2877 | ||
2878 | if (err == 0) { | |
2879 | while (zfs_livelist_condense_sync_pause && | |
2880 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2881 | delay(1); | |
2882 | ||
2883 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
2884 | dmu_tx_mark_netfree(tx); | |
2885 | dmu_tx_hold_space(tx, 1); | |
2886 | err = dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE); | |
2887 | if (err == 0) { | |
2888 | /* | |
2889 | * Prevent the condense zthr restarting before | |
2890 | * the synctask completes. | |
2891 | */ | |
2892 | spa->spa_to_condense.syncing = B_TRUE; | |
2893 | lca->spa = spa; | |
2894 | lca->first_size = first_size; | |
2895 | lca->next_size = next_size; | |
2896 | dsl_sync_task_nowait(spa_get_dsl(spa), | |
38080324 | 2897 | spa_livelist_condense_sync, lca, tx); |
37f03da8 SH |
2898 | dmu_tx_commit(tx); |
2899 | return; | |
2900 | } | |
2901 | } | |
2902 | /* | |
2903 | * Condensing can not continue: either it was externally stopped or | |
2904 | * we were unable to assign to a tx because the pool has run out of | |
2905 | * space. In the second case, we'll just end up trying to condense | |
2906 | * again in a later txg. | |
2907 | */ | |
2908 | ASSERT(err != 0); | |
2909 | bplist_clear(&lca->to_keep); | |
2910 | bplist_destroy(&lca->to_keep); | |
2911 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2912 | dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, spa); | |
2913 | spa->spa_to_condense.ds = NULL; | |
2914 | if (err == EINTR) | |
2915 | zfs_livelist_condense_zthr_cancel++; | |
2916 | } | |
2917 | ||
37f03da8 SH |
2918 | /* |
2919 | * Check that there is something to condense but that a condense is not | |
2920 | * already in progress and that condensing has not been cancelled. | |
2921 | */ | |
2922 | static boolean_t | |
2923 | spa_livelist_condense_cb_check(void *arg, zthr_t *z) | |
2924 | { | |
14e4e3cb | 2925 | (void) z; |
37f03da8 SH |
2926 | spa_t *spa = arg; |
2927 | if ((spa->spa_to_condense.ds != NULL) && | |
2928 | (spa->spa_to_condense.syncing == B_FALSE) && | |
2929 | (spa->spa_to_condense.cancelled == B_FALSE)) { | |
2930 | return (B_TRUE); | |
2931 | } | |
2932 | return (B_FALSE); | |
2933 | } | |
2934 | ||
65c7cc49 | 2935 | static void |
37f03da8 SH |
2936 | spa_start_livelist_condensing_thread(spa_t *spa) |
2937 | { | |
2938 | spa->spa_to_condense.ds = NULL; | |
2939 | spa->spa_to_condense.first = NULL; | |
2940 | spa->spa_to_condense.next = NULL; | |
2941 | spa->spa_to_condense.syncing = B_FALSE; | |
2942 | spa->spa_to_condense.cancelled = B_FALSE; | |
2943 | ||
2944 | ASSERT3P(spa->spa_livelist_condense_zthr, ==, NULL); | |
843e9ca2 SD |
2945 | spa->spa_livelist_condense_zthr = |
2946 | zthr_create("z_livelist_condense", | |
2947 | spa_livelist_condense_cb_check, | |
6bc61d22 | 2948 | spa_livelist_condense_cb, spa, minclsyspri); |
37f03da8 SH |
2949 | } |
2950 | ||
9d5b5245 SD |
2951 | static void |
2952 | spa_spawn_aux_threads(spa_t *spa) | |
2953 | { | |
2954 | ASSERT(spa_writeable(spa)); | |
2955 | ||
2956 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2957 | ||
2958 | spa_start_indirect_condensing_thread(spa); | |
37f03da8 SH |
2959 | spa_start_livelist_destroy_thread(spa); |
2960 | spa_start_livelist_condensing_thread(spa); | |
d2734cce SD |
2961 | |
2962 | ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); | |
2963 | spa->spa_checkpoint_discard_zthr = | |
843e9ca2 SD |
2964 | zthr_create("z_checkpoint_discard", |
2965 | spa_checkpoint_discard_thread_check, | |
6bc61d22 | 2966 | spa_checkpoint_discard_thread, spa, minclsyspri); |
9d5b5245 SD |
2967 | } |
2968 | ||
428870ff BB |
2969 | /* |
2970 | * Fix up config after a partly-completed split. This is done with the | |
2971 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
2972 | * pool have that entry in their config, but only the splitting one contains | |
2973 | * a list of all the guids of the vdevs that are being split off. | |
2974 | * | |
2975 | * This function determines what to do with that list: either rejoin | |
2976 | * all the disks to the pool, or complete the splitting process. To attempt | |
2977 | * the rejoin, each disk that is offlined is marked online again, and | |
2978 | * we do a reopen() call. If the vdev label for every disk that was | |
2979 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
2980 | * then we call vdev_split() on each disk, and complete the split. | |
2981 | * | |
2982 | * Otherwise we leave the config alone, with all the vdevs in place in | |
2983 | * the original pool. | |
2984 | */ | |
2985 | static void | |
2986 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
2987 | { | |
2988 | uint_t extracted; | |
2989 | uint64_t *glist; | |
2990 | uint_t i, gcount; | |
2991 | nvlist_t *nvl; | |
2992 | vdev_t **vd; | |
2993 | boolean_t attempt_reopen; | |
2994 | ||
2995 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
2996 | return; | |
2997 | ||
2998 | /* check that the config is complete */ | |
2999 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
3000 | &glist, &gcount) != 0) | |
3001 | return; | |
3002 | ||
79c76d5b | 3003 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); |
428870ff BB |
3004 | |
3005 | /* attempt to online all the vdevs & validate */ | |
3006 | attempt_reopen = B_TRUE; | |
3007 | for (i = 0; i < gcount; i++) { | |
3008 | if (glist[i] == 0) /* vdev is hole */ | |
3009 | continue; | |
3010 | ||
3011 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
3012 | if (vd[i] == NULL) { | |
3013 | /* | |
3014 | * Don't bother attempting to reopen the disks; | |
3015 | * just do the split. | |
3016 | */ | |
3017 | attempt_reopen = B_FALSE; | |
3018 | } else { | |
3019 | /* attempt to re-online it */ | |
3020 | vd[i]->vdev_offline = B_FALSE; | |
3021 | } | |
3022 | } | |
3023 | ||
3024 | if (attempt_reopen) { | |
3025 | vdev_reopen(spa->spa_root_vdev); | |
3026 | ||
3027 | /* check each device to see what state it's in */ | |
3028 | for (extracted = 0, i = 0; i < gcount; i++) { | |
3029 | if (vd[i] != NULL && | |
3030 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
3031 | break; | |
3032 | ++extracted; | |
3033 | } | |
3034 | } | |
3035 | ||
3036 | /* | |
3037 | * If every disk has been moved to the new pool, or if we never | |
3038 | * even attempted to look at them, then we split them off for | |
3039 | * good. | |
3040 | */ | |
3041 | if (!attempt_reopen || gcount == extracted) { | |
3042 | for (i = 0; i < gcount; i++) | |
3043 | if (vd[i] != NULL) | |
3044 | vdev_split(vd[i]); | |
3045 | vdev_reopen(spa->spa_root_vdev); | |
3046 | } | |
3047 | ||
3048 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
3049 | } | |
3050 | ||
3051 | static int | |
6cb8e530 | 3052 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) |
428870ff | 3053 | { |
a926aab9 | 3054 | const char *ereport = FM_EREPORT_ZFS_POOL; |
428870ff | 3055 | int error; |
428870ff | 3056 | |
6cb8e530 | 3057 | spa->spa_load_state = state; |
ca95f70d OF |
3058 | (void) spa_import_progress_set_state(spa_guid(spa), |
3059 | spa_load_state(spa)); | |
9ae529ec | 3060 | |
6cb8e530 | 3061 | gethrestime(&spa->spa_loaded_ts); |
d2734cce | 3062 | error = spa_load_impl(spa, type, &ereport); |
428870ff | 3063 | |
0c66c32d JG |
3064 | /* |
3065 | * Don't count references from objsets that are already closed | |
3066 | * and are making their way through the eviction process. | |
3067 | */ | |
3068 | spa_evicting_os_wait(spa); | |
424fd7c3 | 3069 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
572e2857 BB |
3070 | if (error) { |
3071 | if (error != EEXIST) { | |
3072 | spa->spa_loaded_ts.tv_sec = 0; | |
3073 | spa->spa_loaded_ts.tv_nsec = 0; | |
3074 | } | |
3075 | if (error != EBADF) { | |
1144586b | 3076 | (void) zfs_ereport_post(ereport, spa, |
4f072827 | 3077 | NULL, NULL, NULL, 0); |
572e2857 BB |
3078 | } |
3079 | } | |
428870ff BB |
3080 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; |
3081 | spa->spa_ena = 0; | |
3082 | ||
ca95f70d OF |
3083 | (void) spa_import_progress_set_state(spa_guid(spa), |
3084 | spa_load_state(spa)); | |
3085 | ||
428870ff BB |
3086 | return (error); |
3087 | } | |
3088 | ||
33cf67cd | 3089 | #ifdef ZFS_DEBUG |
e0ab3ab5 JS |
3090 | /* |
3091 | * Count the number of per-vdev ZAPs associated with all of the vdevs in the | |
3092 | * vdev tree rooted in the given vd, and ensure that each ZAP is present in the | |
3093 | * spa's per-vdev ZAP list. | |
3094 | */ | |
3095 | static uint64_t | |
3096 | vdev_count_verify_zaps(vdev_t *vd) | |
3097 | { | |
3098 | spa_t *spa = vd->vdev_spa; | |
3099 | uint64_t total = 0; | |
e0ab3ab5 | 3100 | |
3e4ed421 RW |
3101 | if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2) && |
3102 | vd->vdev_root_zap != 0) { | |
3103 | total++; | |
3104 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
3105 | spa->spa_all_vdev_zaps, vd->vdev_root_zap)); | |
3106 | } | |
e0ab3ab5 JS |
3107 | if (vd->vdev_top_zap != 0) { |
3108 | total++; | |
3109 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
3110 | spa->spa_all_vdev_zaps, vd->vdev_top_zap)); | |
3111 | } | |
3112 | if (vd->vdev_leaf_zap != 0) { | |
3113 | total++; | |
3114 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
3115 | spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); | |
3116 | } | |
3117 | ||
1c27024e | 3118 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
3119 | total += vdev_count_verify_zaps(vd->vdev_child[i]); |
3120 | } | |
3121 | ||
3122 | return (total); | |
3123 | } | |
36542b06 AZ |
3124 | #else |
3125 | #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0) | |
33cf67cd | 3126 | #endif |
e0ab3ab5 | 3127 | |
379ca9cf OF |
3128 | /* |
3129 | * Determine whether the activity check is required. | |
3130 | */ | |
3131 | static boolean_t | |
bbffb59e BB |
3132 | spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label, |
3133 | nvlist_t *config) | |
379ca9cf OF |
3134 | { |
3135 | uint64_t state = 0; | |
3136 | uint64_t hostid = 0; | |
3137 | uint64_t tryconfig_txg = 0; | |
3138 | uint64_t tryconfig_timestamp = 0; | |
060f0226 | 3139 | uint16_t tryconfig_mmp_seq = 0; |
379ca9cf OF |
3140 | nvlist_t *nvinfo; |
3141 | ||
3142 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3143 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
3144 | (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, | |
3145 | &tryconfig_txg); | |
3146 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
3147 | &tryconfig_timestamp); | |
060f0226 OF |
3148 | (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ, |
3149 | &tryconfig_mmp_seq); | |
379ca9cf OF |
3150 | } |
3151 | ||
3152 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); | |
379ca9cf OF |
3153 | |
3154 | /* | |
3155 | * Disable the MMP activity check - This is used by zdb which | |
3156 | * is intended to be used on potentially active pools. | |
3157 | */ | |
3158 | if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) | |
3159 | return (B_FALSE); | |
3160 | ||
3161 | /* | |
3162 | * Skip the activity check when the MMP feature is disabled. | |
3163 | */ | |
3164 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) | |
3165 | return (B_FALSE); | |
ca95f70d | 3166 | |
379ca9cf | 3167 | /* |
060f0226 OF |
3168 | * If the tryconfig_ values are nonzero, they are the results of an |
3169 | * earlier tryimport. If they all match the uberblock we just found, | |
3170 | * then the pool has not changed and we return false so we do not test | |
3171 | * a second time. | |
379ca9cf OF |
3172 | */ |
3173 | if (tryconfig_txg && tryconfig_txg == ub->ub_txg && | |
060f0226 OF |
3174 | tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp && |
3175 | tryconfig_mmp_seq && tryconfig_mmp_seq == | |
3176 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) | |
379ca9cf OF |
3177 | return (B_FALSE); |
3178 | ||
3179 | /* | |
3180 | * Allow the activity check to be skipped when importing the pool | |
bbffb59e BB |
3181 | * on the same host which last imported it. Since the hostid from |
3182 | * configuration may be stale use the one read from the label. | |
379ca9cf | 3183 | */ |
bbffb59e BB |
3184 | if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) |
3185 | hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); | |
3186 | ||
25f06d67 | 3187 | if (hostid == spa_get_hostid(spa)) |
379ca9cf OF |
3188 | return (B_FALSE); |
3189 | ||
3190 | /* | |
3191 | * Skip the activity test when the pool was cleanly exported. | |
3192 | */ | |
3193 | if (state != POOL_STATE_ACTIVE) | |
3194 | return (B_FALSE); | |
3195 | ||
3196 | return (B_TRUE); | |
3197 | } | |
3198 | ||
060f0226 OF |
3199 | /* |
3200 | * Nanoseconds the activity check must watch for changes on-disk. | |
3201 | */ | |
3202 | static uint64_t | |
3203 | spa_activity_check_duration(spa_t *spa, uberblock_t *ub) | |
3204 | { | |
3205 | uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); | |
3206 | uint64_t multihost_interval = MSEC2NSEC( | |
3207 | MMP_INTERVAL_OK(zfs_multihost_interval)); | |
3208 | uint64_t import_delay = MAX(NANOSEC, import_intervals * | |
3209 | multihost_interval); | |
3210 | ||
3211 | /* | |
3212 | * Local tunables determine a minimum duration except for the case | |
3213 | * where we know when the remote host will suspend the pool if MMP | |
3214 | * writes do not land. | |
3215 | * | |
3216 | * See Big Theory comment at the top of mmp.c for the reasoning behind | |
3217 | * these cases and times. | |
3218 | */ | |
3219 | ||
3220 | ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100); | |
3221 | ||
3222 | if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3223 | MMP_FAIL_INT(ub) > 0) { | |
3224 | ||
3225 | /* MMP on remote host will suspend pool after failed writes */ | |
3226 | import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) * | |
3227 | MMP_IMPORT_SAFETY_FACTOR / 100; | |
3228 | ||
3229 | zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp " | |
3230 | "mmp_fails=%llu ub_mmp mmp_interval=%llu " | |
8e739b2c RE |
3231 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3232 | (u_longlong_t)MMP_FAIL_INT(ub), | |
3233 | (u_longlong_t)MMP_INTERVAL(ub), | |
3234 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3235 | |
3236 | } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3237 | MMP_FAIL_INT(ub) == 0) { | |
3238 | ||
3239 | /* MMP on remote host will never suspend pool */ | |
3240 | import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) + | |
3241 | ub->ub_mmp_delay) * import_intervals); | |
3242 | ||
3243 | zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp " | |
3244 | "mmp_interval=%llu ub_mmp_delay=%llu " | |
8e739b2c RE |
3245 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3246 | (u_longlong_t)MMP_INTERVAL(ub), | |
3247 | (u_longlong_t)ub->ub_mmp_delay, | |
3248 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3249 | |
3250 | } else if (MMP_VALID(ub)) { | |
3251 | /* | |
e1cfd73f | 3252 | * zfs-0.7 compatibility case |
060f0226 OF |
3253 | */ |
3254 | ||
3255 | import_delay = MAX(import_delay, (multihost_interval + | |
3256 | ub->ub_mmp_delay) * import_intervals); | |
3257 | ||
3258 | zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu " | |
8e739b2c RE |
3259 | "import_intervals=%llu leaves=%u", |
3260 | (u_longlong_t)import_delay, | |
3261 | (u_longlong_t)ub->ub_mmp_delay, | |
3262 | (u_longlong_t)import_intervals, | |
060f0226 OF |
3263 | vdev_count_leaves(spa)); |
3264 | } else { | |
3265 | /* Using local tunings is the only reasonable option */ | |
3266 | zfs_dbgmsg("pool last imported on non-MMP aware " | |
3267 | "host using import_delay=%llu multihost_interval=%llu " | |
8e739b2c RE |
3268 | "import_intervals=%llu", (u_longlong_t)import_delay, |
3269 | (u_longlong_t)multihost_interval, | |
3270 | (u_longlong_t)import_intervals); | |
060f0226 OF |
3271 | } |
3272 | ||
3273 | return (import_delay); | |
3274 | } | |
3275 | ||
379ca9cf OF |
3276 | /* |
3277 | * Perform the import activity check. If the user canceled the import or | |
3278 | * we detected activity then fail. | |
3279 | */ | |
3280 | static int | |
3281 | spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) | |
3282 | { | |
379ca9cf OF |
3283 | uint64_t txg = ub->ub_txg; |
3284 | uint64_t timestamp = ub->ub_timestamp; | |
060f0226 OF |
3285 | uint64_t mmp_config = ub->ub_mmp_config; |
3286 | uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0; | |
3287 | uint64_t import_delay; | |
379ca9cf OF |
3288 | hrtime_t import_expire; |
3289 | nvlist_t *mmp_label = NULL; | |
3290 | vdev_t *rvd = spa->spa_root_vdev; | |
3291 | kcondvar_t cv; | |
3292 | kmutex_t mtx; | |
3293 | int error = 0; | |
3294 | ||
3295 | cv_init(&cv, NULL, CV_DEFAULT, NULL); | |
3296 | mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); | |
3297 | mutex_enter(&mtx); | |
3298 | ||
3299 | /* | |
3300 | * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed | |
3301 | * during the earlier tryimport. If the txg recorded there is 0 then | |
3302 | * the pool is known to be active on another host. | |
3303 | * | |
060f0226 | 3304 | * Otherwise, the pool might be in use on another host. Check for |
379ca9cf OF |
3305 | * changes in the uberblocks on disk if necessary. |
3306 | */ | |
3307 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3308 | nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, | |
3309 | ZPOOL_CONFIG_LOAD_INFO); | |
3310 | ||
3311 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && | |
3312 | fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { | |
3313 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
3314 | error = SET_ERROR(EREMOTEIO); | |
3315 | goto out; | |
3316 | } | |
3317 | } | |
3318 | ||
060f0226 | 3319 | import_delay = spa_activity_check_duration(spa, ub); |
533ea041 | 3320 | |
379ca9cf | 3321 | /* Add a small random factor in case of simultaneous imports (0-25%) */ |
29274c9f | 3322 | import_delay += import_delay * random_in_range(250) / 1000; |
ca95f70d OF |
3323 | |
3324 | import_expire = gethrtime() + import_delay; | |
379ca9cf OF |
3325 | |
3326 | while (gethrtime() < import_expire) { | |
ca95f70d OF |
3327 | (void) spa_import_progress_set_mmp_check(spa_guid(spa), |
3328 | NSEC2SEC(import_expire - gethrtime())); | |
3329 | ||
379ca9cf OF |
3330 | vdev_uberblock_load(rvd, ub, &mmp_label); |
3331 | ||
060f0226 OF |
3332 | if (txg != ub->ub_txg || timestamp != ub->ub_timestamp || |
3333 | mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) { | |
3334 | zfs_dbgmsg("multihost activity detected " | |
3335 | "txg %llu ub_txg %llu " | |
3336 | "timestamp %llu ub_timestamp %llu " | |
3337 | "mmp_config %#llx ub_mmp_config %#llx", | |
8e739b2c RE |
3338 | (u_longlong_t)txg, (u_longlong_t)ub->ub_txg, |
3339 | (u_longlong_t)timestamp, | |
3340 | (u_longlong_t)ub->ub_timestamp, | |
3341 | (u_longlong_t)mmp_config, | |
3342 | (u_longlong_t)ub->ub_mmp_config); | |
060f0226 | 3343 | |
379ca9cf OF |
3344 | error = SET_ERROR(EREMOTEIO); |
3345 | break; | |
3346 | } | |
3347 | ||
3348 | if (mmp_label) { | |
3349 | nvlist_free(mmp_label); | |
3350 | mmp_label = NULL; | |
3351 | } | |
3352 | ||
3353 | error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); | |
3354 | if (error != -1) { | |
3355 | error = SET_ERROR(EINTR); | |
3356 | break; | |
3357 | } | |
3358 | error = 0; | |
3359 | } | |
3360 | ||
3361 | out: | |
3362 | mutex_exit(&mtx); | |
3363 | mutex_destroy(&mtx); | |
3364 | cv_destroy(&cv); | |
3365 | ||
3366 | /* | |
3367 | * If the pool is determined to be active store the status in the | |
3368 | * spa->spa_load_info nvlist. If the remote hostname or hostid are | |
3369 | * available from configuration read from disk store them as well. | |
3370 | * This allows 'zpool import' to generate a more useful message. | |
3371 | * | |
3372 | * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) | |
3373 | * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool | |
3374 | * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool | |
3375 | */ | |
3376 | if (error == EREMOTEIO) { | |
a926aab9 | 3377 | const char *hostname = "<unknown>"; |
379ca9cf OF |
3378 | uint64_t hostid = 0; |
3379 | ||
3380 | if (mmp_label) { | |
3381 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { | |
3382 | hostname = fnvlist_lookup_string(mmp_label, | |
3383 | ZPOOL_CONFIG_HOSTNAME); | |
3384 | fnvlist_add_string(spa->spa_load_info, | |
3385 | ZPOOL_CONFIG_MMP_HOSTNAME, hostname); | |
3386 | } | |
3387 | ||
3388 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { | |
3389 | hostid = fnvlist_lookup_uint64(mmp_label, | |
3390 | ZPOOL_CONFIG_HOSTID); | |
3391 | fnvlist_add_uint64(spa->spa_load_info, | |
3392 | ZPOOL_CONFIG_MMP_HOSTID, hostid); | |
3393 | } | |
3394 | } | |
3395 | ||
3396 | fnvlist_add_uint64(spa->spa_load_info, | |
3397 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); | |
3398 | fnvlist_add_uint64(spa->spa_load_info, | |
3399 | ZPOOL_CONFIG_MMP_TXG, 0); | |
3400 | ||
3401 | error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); | |
3402 | } | |
3403 | ||
3404 | if (mmp_label) | |
3405 | nvlist_free(mmp_label); | |
3406 | ||
3407 | return (error); | |
3408 | } | |
3409 | ||
9eb7b46e | 3410 | static int |
6cb8e530 PZ |
3411 | spa_verify_host(spa_t *spa, nvlist_t *mos_config) |
3412 | { | |
3413 | uint64_t hostid; | |
d1807f16 | 3414 | const char *hostname; |
6cb8e530 PZ |
3415 | uint64_t myhostid = 0; |
3416 | ||
3417 | if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, | |
3418 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
3419 | hostname = fnvlist_lookup_string(mos_config, | |
3420 | ZPOOL_CONFIG_HOSTNAME); | |
3421 | ||
3422 | myhostid = zone_get_hostid(NULL); | |
3423 | ||
3424 | if (hostid != 0 && myhostid != 0 && hostid != myhostid) { | |
3425 | cmn_err(CE_WARN, "pool '%s' could not be " | |
3426 | "loaded as it was last accessed by " | |
3427 | "another system (host: %s hostid: 0x%llx). " | |
a2f944a1 RM |
3428 | "See: https://openzfs.github.io/openzfs-docs/msg/" |
3429 | "ZFS-8000-EY", | |
6cb8e530 PZ |
3430 | spa_name(spa), hostname, (u_longlong_t)hostid); |
3431 | spa_load_failed(spa, "hostid verification failed: pool " | |
3432 | "last accessed by host: %s (hostid: 0x%llx)", | |
3433 | hostname, (u_longlong_t)hostid); | |
3434 | return (SET_ERROR(EBADF)); | |
3435 | } | |
3436 | } | |
3437 | ||
3438 | return (0); | |
3439 | } | |
3440 | ||
3441 | static int | |
3442 | spa_ld_parse_config(spa_t *spa, spa_import_type_t type) | |
428870ff BB |
3443 | { |
3444 | int error = 0; | |
6cb8e530 | 3445 | nvlist_t *nvtree, *nvl, *config = spa->spa_config; |
1c27024e | 3446 | int parse; |
9eb7b46e | 3447 | vdev_t *rvd; |
6cb8e530 | 3448 | uint64_t pool_guid; |
d1807f16 RY |
3449 | const char *comment; |
3450 | const char *compatibility; | |
6cb8e530 PZ |
3451 | |
3452 | /* | |
3453 | * Versioning wasn't explicitly added to the label until later, so if | |
3454 | * it's not present treat it as the initial version. | |
3455 | */ | |
3456 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
3457 | &spa->spa_ubsync.ub_version) != 0) | |
3458 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
3459 | ||
3460 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { | |
3461 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3462 | ZPOOL_CONFIG_POOL_GUID); | |
3463 | return (SET_ERROR(EINVAL)); | |
3464 | } | |
3465 | ||
d2734cce SD |
3466 | /* |
3467 | * If we are doing an import, ensure that the pool is not already | |
3468 | * imported by checking if its pool guid already exists in the | |
3469 | * spa namespace. | |
3470 | * | |
3471 | * The only case that we allow an already imported pool to be | |
3472 | * imported again, is when the pool is checkpointed and we want to | |
3473 | * look at its checkpointed state from userland tools like zdb. | |
3474 | */ | |
3475 | #ifdef _KERNEL | |
3476 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3477 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3478 | spa_guid_exists(pool_guid, 0)) { | |
3479 | #else | |
3480 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3481 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3482 | spa_guid_exists(pool_guid, 0) && | |
3483 | !spa_importing_readonly_checkpoint(spa)) { | |
3484 | #endif | |
6cb8e530 PZ |
3485 | spa_load_failed(spa, "a pool with guid %llu is already open", |
3486 | (u_longlong_t)pool_guid); | |
3487 | return (SET_ERROR(EEXIST)); | |
3488 | } | |
3489 | ||
3490 | spa->spa_config_guid = pool_guid; | |
3491 | ||
3492 | nvlist_free(spa->spa_load_info); | |
3493 | spa->spa_load_info = fnvlist_alloc(); | |
3494 | ||
3495 | ASSERT(spa->spa_comment == NULL); | |
3496 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
3497 | spa->spa_comment = spa_strdup(comment); | |
3498 | ||
658fb802 CB |
3499 | ASSERT(spa->spa_compatibility == NULL); |
3500 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMPATIBILITY, | |
3501 | &compatibility) == 0) | |
3502 | spa->spa_compatibility = spa_strdup(compatibility); | |
3503 | ||
6cb8e530 PZ |
3504 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, |
3505 | &spa->spa_config_txg); | |
3506 | ||
3507 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) | |
3508 | spa->spa_config_splitting = fnvlist_dup(nvl); | |
428870ff | 3509 | |
4a0ee12a PZ |
3510 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { |
3511 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3512 | ZPOOL_CONFIG_VDEV_TREE); | |
2e528b49 | 3513 | return (SET_ERROR(EINVAL)); |
4a0ee12a | 3514 | } |
428870ff | 3515 | |
428870ff BB |
3516 | /* |
3517 | * Create "The Godfather" zio to hold all async IOs | |
3518 | */ | |
e022864d MA |
3519 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
3520 | KM_SLEEP); | |
1c27024e | 3521 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
3522 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
3523 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
3524 | ZIO_FLAG_GODFATHER); | |
3525 | } | |
428870ff BB |
3526 | |
3527 | /* | |
3528 | * Parse the configuration into a vdev tree. We explicitly set the | |
3529 | * value that will be returned by spa_version() since parsing the | |
3530 | * configuration requires knowing the version number. | |
3531 | */ | |
3532 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 PZ |
3533 | parse = (type == SPA_IMPORT_EXISTING ? |
3534 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
9eb7b46e | 3535 | error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); |
428870ff BB |
3536 | spa_config_exit(spa, SCL_ALL, FTAG); |
3537 | ||
4a0ee12a PZ |
3538 | if (error != 0) { |
3539 | spa_load_failed(spa, "unable to parse config [error=%d]", | |
3540 | error); | |
428870ff | 3541 | return (error); |
4a0ee12a | 3542 | } |
428870ff BB |
3543 | |
3544 | ASSERT(spa->spa_root_vdev == rvd); | |
c3520e7f MA |
3545 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); |
3546 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
428870ff BB |
3547 | |
3548 | if (type != SPA_IMPORT_ASSEMBLE) { | |
3549 | ASSERT(spa_guid(spa) == pool_guid); | |
3550 | } | |
3551 | ||
9eb7b46e PZ |
3552 | return (0); |
3553 | } | |
3554 | ||
6cb8e530 PZ |
3555 | /* |
3556 | * Recursively open all vdevs in the vdev tree. This function is called twice: | |
3557 | * first with the untrusted config, then with the trusted config. | |
3558 | */ | |
9eb7b46e PZ |
3559 | static int |
3560 | spa_ld_open_vdevs(spa_t *spa) | |
3561 | { | |
3562 | int error = 0; | |
3563 | ||
6cb8e530 PZ |
3564 | /* |
3565 | * spa_missing_tvds_allowed defines how many top-level vdevs can be | |
3566 | * missing/unopenable for the root vdev to be still considered openable. | |
3567 | */ | |
3568 | if (spa->spa_trust_config) { | |
3569 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; | |
3570 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { | |
3571 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; | |
3572 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { | |
3573 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; | |
3574 | } else { | |
3575 | spa->spa_missing_tvds_allowed = 0; | |
3576 | } | |
3577 | ||
3578 | spa->spa_missing_tvds_allowed = | |
3579 | MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); | |
3580 | ||
428870ff | 3581 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9eb7b46e | 3582 | error = vdev_open(spa->spa_root_vdev); |
428870ff | 3583 | spa_config_exit(spa, SCL_ALL, FTAG); |
6cb8e530 PZ |
3584 | |
3585 | if (spa->spa_missing_tvds != 0) { | |
3586 | spa_load_note(spa, "vdev tree has %lld missing top-level " | |
3587 | "vdevs.", (u_longlong_t)spa->spa_missing_tvds); | |
da92d5cb | 3588 | if (spa->spa_trust_config && (spa->spa_mode & SPA_MODE_WRITE)) { |
6cb8e530 PZ |
3589 | /* |
3590 | * Although theoretically we could allow users to open | |
3591 | * incomplete pools in RW mode, we'd need to add a lot | |
3592 | * of extra logic (e.g. adjust pool space to account | |
3593 | * for missing vdevs). | |
3594 | * This limitation also prevents users from accidentally | |
3595 | * opening the pool in RW mode during data recovery and | |
3596 | * damaging it further. | |
3597 | */ | |
3598 | spa_load_note(spa, "pools with missing top-level " | |
3599 | "vdevs can only be opened in read-only mode."); | |
3600 | error = SET_ERROR(ENXIO); | |
3601 | } else { | |
3602 | spa_load_note(spa, "current settings allow for maximum " | |
3603 | "%lld missing top-level vdevs at this stage.", | |
3604 | (u_longlong_t)spa->spa_missing_tvds_allowed); | |
3605 | } | |
3606 | } | |
4a0ee12a PZ |
3607 | if (error != 0) { |
3608 | spa_load_failed(spa, "unable to open vdev tree [error=%d]", | |
3609 | error); | |
3610 | } | |
6cb8e530 PZ |
3611 | if (spa->spa_missing_tvds != 0 || error != 0) |
3612 | vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); | |
9eb7b46e PZ |
3613 | |
3614 | return (error); | |
3615 | } | |
3616 | ||
6cb8e530 PZ |
3617 | /* |
3618 | * We need to validate the vdev labels against the configuration that | |
3619 | * we have in hand. This function is called twice: first with an untrusted | |
3620 | * config, then with a trusted config. The validation is more strict when the | |
3621 | * config is trusted. | |
3622 | */ | |
9eb7b46e | 3623 | static int |
6cb8e530 | 3624 | spa_ld_validate_vdevs(spa_t *spa) |
9eb7b46e PZ |
3625 | { |
3626 | int error = 0; | |
3627 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 3628 | |
6cb8e530 PZ |
3629 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3630 | error = vdev_validate(rvd); | |
3631 | spa_config_exit(spa, SCL_ALL, FTAG); | |
428870ff | 3632 | |
6cb8e530 PZ |
3633 | if (error != 0) { |
3634 | spa_load_failed(spa, "vdev_validate failed [error=%d]", error); | |
3635 | return (error); | |
3636 | } | |
428870ff | 3637 | |
6cb8e530 PZ |
3638 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { |
3639 | spa_load_failed(spa, "cannot open vdev tree after invalidating " | |
3640 | "some vdevs"); | |
3641 | vdev_dbgmsg_print_tree(rvd, 2); | |
3642 | return (SET_ERROR(ENXIO)); | |
428870ff BB |
3643 | } |
3644 | ||
9eb7b46e PZ |
3645 | return (0); |
3646 | } | |
3647 | ||
d2734cce SD |
3648 | static void |
3649 | spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) | |
3650 | { | |
3651 | spa->spa_state = POOL_STATE_ACTIVE; | |
3652 | spa->spa_ubsync = spa->spa_uberblock; | |
3653 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
3654 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
3655 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
3656 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
3657 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
3658 | spa->spa_prev_software_version = ub->ub_software_version; | |
3659 | } | |
3660 | ||
9eb7b46e | 3661 | static int |
6cb8e530 | 3662 | spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) |
9eb7b46e PZ |
3663 | { |
3664 | vdev_t *rvd = spa->spa_root_vdev; | |
3665 | nvlist_t *label; | |
3666 | uberblock_t *ub = &spa->spa_uberblock; | |
9eb7b46e PZ |
3667 | boolean_t activity_check = B_FALSE; |
3668 | ||
d2734cce SD |
3669 | /* |
3670 | * If we are opening the checkpointed state of the pool by | |
3671 | * rewinding to it, at this point we will have written the | |
3672 | * checkpointed uberblock to the vdev labels, so searching | |
3673 | * the labels will find the right uberblock. However, if | |
3674 | * we are opening the checkpointed state read-only, we have | |
3675 | * not modified the labels. Therefore, we must ignore the | |
3676 | * labels and continue using the spa_uberblock that was set | |
3677 | * by spa_ld_checkpoint_rewind. | |
3678 | * | |
3679 | * Note that it would be fine to ignore the labels when | |
3680 | * rewinding (opening writeable) as well. However, if we | |
3681 | * crash just after writing the labels, we will end up | |
3682 | * searching the labels. Doing so in the common case means | |
3683 | * that this code path gets exercised normally, rather than | |
3684 | * just in the edge case. | |
3685 | */ | |
3686 | if (ub->ub_checkpoint_txg != 0 && | |
3687 | spa_importing_readonly_checkpoint(spa)) { | |
3688 | spa_ld_select_uberblock_done(spa, ub); | |
3689 | return (0); | |
3690 | } | |
3691 | ||
428870ff BB |
3692 | /* |
3693 | * Find the best uberblock. | |
3694 | */ | |
9ae529ec | 3695 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
3696 | |
3697 | /* | |
3698 | * If we weren't able to find a single valid uberblock, return failure. | |
3699 | */ | |
9ae529ec CS |
3700 | if (ub->ub_txg == 0) { |
3701 | nvlist_free(label); | |
4a0ee12a | 3702 | spa_load_failed(spa, "no valid uberblock found"); |
428870ff | 3703 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 3704 | } |
428870ff | 3705 | |
ca95f70d OF |
3706 | if (spa->spa_load_max_txg != UINT64_MAX) { |
3707 | (void) spa_import_progress_set_max_txg(spa_guid(spa), | |
3708 | (u_longlong_t)spa->spa_load_max_txg); | |
3709 | } | |
4a0ee12a PZ |
3710 | spa_load_note(spa, "using uberblock with txg=%llu", |
3711 | (u_longlong_t)ub->ub_txg); | |
3712 | ||
3713 | ||
379ca9cf OF |
3714 | /* |
3715 | * For pools which have the multihost property on determine if the | |
3716 | * pool is truly inactive and can be safely imported. Prevent | |
3717 | * hosts which don't have a hostid set from importing the pool. | |
3718 | */ | |
6cb8e530 PZ |
3719 | activity_check = spa_activity_check_required(spa, ub, label, |
3720 | spa->spa_config); | |
379ca9cf | 3721 | if (activity_check) { |
379ca9cf | 3722 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && |
25f06d67 | 3723 | spa_get_hostid(spa) == 0) { |
379ca9cf OF |
3724 | nvlist_free(label); |
3725 | fnvlist_add_uint64(spa->spa_load_info, | |
3726 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3727 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3728 | } | |
3729 | ||
6cb8e530 | 3730 | int error = spa_activity_check(spa, ub, spa->spa_config); |
e889f0f5 OF |
3731 | if (error) { |
3732 | nvlist_free(label); | |
3733 | return (error); | |
3734 | } | |
3735 | ||
379ca9cf OF |
3736 | fnvlist_add_uint64(spa->spa_load_info, |
3737 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); | |
3738 | fnvlist_add_uint64(spa->spa_load_info, | |
3739 | ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); | |
060f0226 OF |
3740 | fnvlist_add_uint16(spa->spa_load_info, |
3741 | ZPOOL_CONFIG_MMP_SEQ, | |
3742 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)); | |
379ca9cf OF |
3743 | } |
3744 | ||
428870ff | 3745 | /* |
9ae529ec | 3746 | * If the pool has an unsupported version we can't open it. |
428870ff | 3747 | */ |
9ae529ec CS |
3748 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
3749 | nvlist_free(label); | |
4a0ee12a PZ |
3750 | spa_load_failed(spa, "version %llu is not supported", |
3751 | (u_longlong_t)ub->ub_version); | |
428870ff | 3752 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
3753 | } |
3754 | ||
3755 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3756 | nvlist_t *features; | |
3757 | ||
3758 | /* | |
3759 | * If we weren't able to find what's necessary for reading the | |
3760 | * MOS in the label, return failure. | |
3761 | */ | |
4a0ee12a PZ |
3762 | if (label == NULL) { |
3763 | spa_load_failed(spa, "label config unavailable"); | |
3764 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
3765 | ENXIO)); | |
3766 | } | |
3767 | ||
3768 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, | |
3769 | &features) != 0) { | |
9ae529ec | 3770 | nvlist_free(label); |
4a0ee12a PZ |
3771 | spa_load_failed(spa, "invalid label: '%s' missing", |
3772 | ZPOOL_CONFIG_FEATURES_FOR_READ); | |
9ae529ec CS |
3773 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3774 | ENXIO)); | |
3775 | } | |
3776 | ||
3777 | /* | |
3778 | * Update our in-core representation with the definitive values | |
3779 | * from the label. | |
3780 | */ | |
3781 | nvlist_free(spa->spa_label_features); | |
65ad5d11 | 3782 | spa->spa_label_features = fnvlist_dup(features); |
9ae529ec CS |
3783 | } |
3784 | ||
3785 | nvlist_free(label); | |
3786 | ||
3787 | /* | |
3788 | * Look through entries in the label nvlist's features_for_read. If | |
3789 | * there is a feature listed there which we don't understand then we | |
3790 | * cannot open a pool. | |
3791 | */ | |
3792 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3793 | nvlist_t *unsup_feat; | |
9ae529ec | 3794 | |
65ad5d11 | 3795 | unsup_feat = fnvlist_alloc(); |
9ae529ec | 3796 | |
1c27024e DB |
3797 | for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, |
3798 | NULL); nvp != NULL; | |
9ae529ec CS |
3799 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { |
3800 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
65ad5d11 AJ |
3801 | fnvlist_add_string(unsup_feat, |
3802 | nvpair_name(nvp), ""); | |
9ae529ec CS |
3803 | } |
3804 | } | |
3805 | ||
3806 | if (!nvlist_empty(unsup_feat)) { | |
65ad5d11 AJ |
3807 | fnvlist_add_nvlist(spa->spa_load_info, |
3808 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec | 3809 | nvlist_free(unsup_feat); |
4a0ee12a | 3810 | spa_load_failed(spa, "some features are unsupported"); |
9ae529ec CS |
3811 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3812 | ENOTSUP)); | |
3813 | } | |
3814 | ||
3815 | nvlist_free(unsup_feat); | |
3816 | } | |
428870ff | 3817 | |
428870ff BB |
3818 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { |
3819 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 | 3820 | spa_try_repair(spa, spa->spa_config); |
428870ff BB |
3821 | spa_config_exit(spa, SCL_ALL, FTAG); |
3822 | nvlist_free(spa->spa_config_splitting); | |
3823 | spa->spa_config_splitting = NULL; | |
3824 | } | |
3825 | ||
3826 | /* | |
3827 | * Initialize internal SPA structures. | |
3828 | */ | |
d2734cce | 3829 | spa_ld_select_uberblock_done(spa, ub); |
428870ff | 3830 | |
9eb7b46e PZ |
3831 | return (0); |
3832 | } | |
3833 | ||
3834 | static int | |
3835 | spa_ld_open_rootbp(spa_t *spa) | |
3836 | { | |
3837 | int error = 0; | |
3838 | vdev_t *rvd = spa->spa_root_vdev; | |
a1d477c2 | 3839 | |
9ae529ec | 3840 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
4a0ee12a PZ |
3841 | if (error != 0) { |
3842 | spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " | |
3843 | "[error=%d]", error); | |
428870ff | 3844 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3845 | } |
428870ff BB |
3846 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; |
3847 | ||
9eb7b46e PZ |
3848 | return (0); |
3849 | } | |
3850 | ||
3851 | static int | |
d2734cce | 3852 | spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, |
6cb8e530 | 3853 | boolean_t reloading) |
9eb7b46e | 3854 | { |
6cb8e530 PZ |
3855 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
3856 | nvlist_t *nv, *mos_config, *policy; | |
3857 | int error = 0, copy_error; | |
3858 | uint64_t healthy_tvds, healthy_tvds_mos; | |
3859 | uint64_t mos_config_txg; | |
9eb7b46e | 3860 | |
4a0ee12a PZ |
3861 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) |
3862 | != 0) | |
428870ff BB |
3863 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3864 | ||
a1d477c2 | 3865 | /* |
6cb8e530 PZ |
3866 | * If we're assembling a pool from a split, the config provided is |
3867 | * already trusted so there is nothing to do. | |
a1d477c2 | 3868 | */ |
6cb8e530 PZ |
3869 | if (type == SPA_IMPORT_ASSEMBLE) |
3870 | return (0); | |
3871 | ||
3872 | healthy_tvds = spa_healthy_core_tvds(spa); | |
a1d477c2 | 3873 | |
6cb8e530 PZ |
3874 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) |
3875 | != 0) { | |
3876 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
3877 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3878 | } | |
3879 | ||
3880 | /* | |
3881 | * If we are doing an open, pool owner wasn't verified yet, thus do | |
3882 | * the verification here. | |
3883 | */ | |
3884 | if (spa->spa_load_state == SPA_LOAD_OPEN) { | |
3885 | error = spa_verify_host(spa, mos_config); | |
3886 | if (error != 0) { | |
a1d477c2 | 3887 | nvlist_free(mos_config); |
6cb8e530 | 3888 | return (error); |
a1d477c2 | 3889 | } |
6cb8e530 PZ |
3890 | } |
3891 | ||
3892 | nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); | |
a1d477c2 | 3893 | |
6cb8e530 PZ |
3894 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3895 | ||
3896 | /* | |
3897 | * Build a new vdev tree from the trusted config | |
3898 | */ | |
b2255edc BB |
3899 | error = spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD); |
3900 | if (error != 0) { | |
3901 | nvlist_free(mos_config); | |
3902 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3903 | spa_load_failed(spa, "spa_config_parse failed [error=%d]", | |
3904 | error); | |
3905 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
3906 | } | |
6cb8e530 PZ |
3907 | |
3908 | /* | |
3909 | * Vdev paths in the MOS may be obsolete. If the untrusted config was | |
3910 | * obtained by scanning /dev/dsk, then it will have the right vdev | |
3911 | * paths. We update the trusted MOS config with this information. | |
3912 | * We first try to copy the paths with vdev_copy_path_strict, which | |
3913 | * succeeds only when both configs have exactly the same vdev tree. | |
3914 | * If that fails, we fall back to a more flexible method that has a | |
3915 | * best effort policy. | |
3916 | */ | |
3917 | copy_error = vdev_copy_path_strict(rvd, mrvd); | |
3918 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3919 | spa_load_note(spa, "provided vdev tree:"); | |
3920 | vdev_dbgmsg_print_tree(rvd, 2); | |
3921 | spa_load_note(spa, "MOS vdev tree:"); | |
3922 | vdev_dbgmsg_print_tree(mrvd, 2); | |
3923 | } | |
3924 | if (copy_error != 0) { | |
3925 | spa_load_note(spa, "vdev_copy_path_strict failed, falling " | |
3926 | "back to vdev_copy_path_relaxed"); | |
3927 | vdev_copy_path_relaxed(rvd, mrvd); | |
3928 | } | |
3929 | ||
3930 | vdev_close(rvd); | |
3931 | vdev_free(rvd); | |
3932 | spa->spa_root_vdev = mrvd; | |
3933 | rvd = mrvd; | |
3934 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3935 | ||
3936 | /* | |
3937 | * We will use spa_config if we decide to reload the spa or if spa_load | |
3938 | * fails and we rewind. We must thus regenerate the config using the | |
8a393be3 PZ |
3939 | * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to |
3940 | * pass settings on how to load the pool and is not stored in the MOS. | |
3941 | * We copy it over to our new, trusted config. | |
6cb8e530 PZ |
3942 | */ |
3943 | mos_config_txg = fnvlist_lookup_uint64(mos_config, | |
3944 | ZPOOL_CONFIG_POOL_TXG); | |
3945 | nvlist_free(mos_config); | |
3946 | mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); | |
8a393be3 | 3947 | if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, |
6cb8e530 | 3948 | &policy) == 0) |
8a393be3 | 3949 | fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); |
6cb8e530 PZ |
3950 | spa_config_set(spa, mos_config); |
3951 | spa->spa_config_source = SPA_CONFIG_SRC_MOS; | |
3952 | ||
3953 | /* | |
3954 | * Now that we got the config from the MOS, we should be more strict | |
3955 | * in checking blkptrs and can make assumptions about the consistency | |
3956 | * of the vdev tree. spa_trust_config must be set to true before opening | |
3957 | * vdevs in order for them to be writeable. | |
3958 | */ | |
3959 | spa->spa_trust_config = B_TRUE; | |
3960 | ||
3961 | /* | |
3962 | * Open and validate the new vdev tree | |
3963 | */ | |
3964 | error = spa_ld_open_vdevs(spa); | |
3965 | if (error != 0) | |
3966 | return (error); | |
3967 | ||
3968 | error = spa_ld_validate_vdevs(spa); | |
3969 | if (error != 0) | |
3970 | return (error); | |
3971 | ||
3972 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3973 | spa_load_note(spa, "final vdev tree:"); | |
3974 | vdev_dbgmsg_print_tree(rvd, 2); | |
3975 | } | |
3976 | ||
3977 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && | |
3978 | !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { | |
a1d477c2 | 3979 | /* |
6cb8e530 PZ |
3980 | * Sanity check to make sure that we are indeed loading the |
3981 | * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds | |
3982 | * in the config provided and they happened to be the only ones | |
3983 | * to have the latest uberblock, we could involuntarily perform | |
3984 | * an extreme rewind. | |
a1d477c2 | 3985 | */ |
6cb8e530 PZ |
3986 | healthy_tvds_mos = spa_healthy_core_tvds(spa); |
3987 | if (healthy_tvds_mos - healthy_tvds >= | |
3988 | SPA_SYNC_MIN_VDEVS) { | |
3989 | spa_load_note(spa, "config provided misses too many " | |
3990 | "top-level vdevs compared to MOS (%lld vs %lld). ", | |
3991 | (u_longlong_t)healthy_tvds, | |
3992 | (u_longlong_t)healthy_tvds_mos); | |
3993 | spa_load_note(spa, "vdev tree:"); | |
3994 | vdev_dbgmsg_print_tree(rvd, 2); | |
3995 | if (reloading) { | |
3996 | spa_load_failed(spa, "config was already " | |
3997 | "provided from MOS. Aborting."); | |
3998 | return (spa_vdev_err(rvd, | |
3999 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
4000 | } | |
4001 | spa_load_note(spa, "spa must be reloaded using MOS " | |
4002 | "config"); | |
4003 | return (SET_ERROR(EAGAIN)); | |
4a0ee12a | 4004 | } |
a1d477c2 MA |
4005 | } |
4006 | ||
6cb8e530 PZ |
4007 | error = spa_check_for_missing_logs(spa); |
4008 | if (error != 0) | |
4009 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
4010 | ||
4011 | if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { | |
4012 | spa_load_failed(spa, "uberblock guid sum doesn't match MOS " | |
4013 | "guid sum (%llu != %llu)", | |
4014 | (u_longlong_t)spa->spa_uberblock.ub_guid_sum, | |
4015 | (u_longlong_t)rvd->vdev_guid_sum); | |
4016 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
4017 | ENXIO)); | |
4018 | } | |
4019 | ||
9eb7b46e PZ |
4020 | return (0); |
4021 | } | |
4022 | ||
4023 | static int | |
4024 | spa_ld_open_indirect_vdev_metadata(spa_t *spa) | |
4025 | { | |
4026 | int error = 0; | |
4027 | vdev_t *rvd = spa->spa_root_vdev; | |
4028 | ||
a1d477c2 MA |
4029 | /* |
4030 | * Everything that we read before spa_remove_init() must be stored | |
4031 | * on concreted vdevs. Therefore we do this as early as possible. | |
4032 | */ | |
4a0ee12a PZ |
4033 | error = spa_remove_init(spa); |
4034 | if (error != 0) { | |
4035 | spa_load_failed(spa, "spa_remove_init failed [error=%d]", | |
4036 | error); | |
a1d477c2 | 4037 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4038 | } |
a1d477c2 | 4039 | |
9eb7b46e PZ |
4040 | /* |
4041 | * Retrieve information needed to condense indirect vdev mappings. | |
4042 | */ | |
4043 | error = spa_condense_init(spa); | |
4044 | if (error != 0) { | |
4a0ee12a PZ |
4045 | spa_load_failed(spa, "spa_condense_init failed [error=%d]", |
4046 | error); | |
9eb7b46e PZ |
4047 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
4048 | } | |
4049 | ||
4050 | return (0); | |
4051 | } | |
4052 | ||
4053 | static int | |
4a0ee12a | 4054 | spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) |
9eb7b46e PZ |
4055 | { |
4056 | int error = 0; | |
4057 | vdev_t *rvd = spa->spa_root_vdev; | |
4058 | ||
9ae529ec CS |
4059 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
4060 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 4061 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
4062 | |
4063 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
4a0ee12a | 4064 | &spa->spa_feat_for_read_obj, B_TRUE) != 0) { |
9ae529ec CS |
4065 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4066 | } | |
4067 | ||
4068 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
4a0ee12a | 4069 | &spa->spa_feat_for_write_obj, B_TRUE) != 0) { |
9ae529ec CS |
4070 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4071 | } | |
4072 | ||
4073 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
4a0ee12a | 4074 | &spa->spa_feat_desc_obj, B_TRUE) != 0) { |
9ae529ec CS |
4075 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4076 | } | |
4077 | ||
b9b24bb4 CS |
4078 | enabled_feat = fnvlist_alloc(); |
4079 | unsup_feat = fnvlist_alloc(); | |
9ae529ec | 4080 | |
fa86b5db | 4081 | if (!spa_features_check(spa, B_FALSE, |
b9b24bb4 | 4082 | unsup_feat, enabled_feat)) |
9ae529ec CS |
4083 | missing_feat_read = B_TRUE; |
4084 | ||
4a0ee12a PZ |
4085 | if (spa_writeable(spa) || |
4086 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) { | |
fa86b5db | 4087 | if (!spa_features_check(spa, B_TRUE, |
b9b24bb4 | 4088 | unsup_feat, enabled_feat)) { |
9eb7b46e | 4089 | *missing_feat_writep = B_TRUE; |
b9b24bb4 | 4090 | } |
9ae529ec CS |
4091 | } |
4092 | ||
b9b24bb4 CS |
4093 | fnvlist_add_nvlist(spa->spa_load_info, |
4094 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
4095 | ||
9ae529ec | 4096 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
4097 | fnvlist_add_nvlist(spa->spa_load_info, |
4098 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
4099 | } |
4100 | ||
b9b24bb4 CS |
4101 | fnvlist_free(enabled_feat); |
4102 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
4103 | |
4104 | if (!missing_feat_read) { | |
4105 | fnvlist_add_boolean(spa->spa_load_info, | |
4106 | ZPOOL_CONFIG_CAN_RDONLY); | |
4107 | } | |
4108 | ||
4109 | /* | |
4110 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
4111 | * twofold: to determine whether the pool is available for | |
4112 | * import in read-write mode and (if it is not) whether the | |
4113 | * pool is available for import in read-only mode. If the pool | |
4114 | * is available for import in read-write mode, it is displayed | |
4115 | * as available in userland; if it is not available for import | |
4116 | * in read-only mode, it is displayed as unavailable in | |
4117 | * userland. If the pool is available for import in read-only | |
4118 | * mode but not read-write mode, it is displayed as unavailable | |
4119 | * in userland with a special note that the pool is actually | |
4120 | * available for open in read-only mode. | |
4121 | * | |
4122 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
4123 | * missing a feature for write, we must first determine whether | |
4124 | * the pool can be opened read-only before returning to | |
4125 | * userland in order to know whether to display the | |
4126 | * abovementioned note. | |
4127 | */ | |
9eb7b46e | 4128 | if (missing_feat_read || (*missing_feat_writep && |
9ae529ec | 4129 | spa_writeable(spa))) { |
4a0ee12a | 4130 | spa_load_failed(spa, "pool uses unsupported features"); |
9ae529ec CS |
4131 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
4132 | ENOTSUP)); | |
4133 | } | |
b0bc7a84 MG |
4134 | |
4135 | /* | |
4136 | * Load refcounts for ZFS features from disk into an in-memory | |
4137 | * cache during SPA initialization. | |
4138 | */ | |
1c27024e | 4139 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { |
b0bc7a84 MG |
4140 | uint64_t refcount; |
4141 | ||
4142 | error = feature_get_refcount_from_disk(spa, | |
4143 | &spa_feature_table[i], &refcount); | |
4144 | if (error == 0) { | |
4145 | spa->spa_feat_refcount_cache[i] = refcount; | |
4146 | } else if (error == ENOTSUP) { | |
4147 | spa->spa_feat_refcount_cache[i] = | |
4148 | SPA_FEATURE_DISABLED; | |
4149 | } else { | |
4a0ee12a PZ |
4150 | spa_load_failed(spa, "error getting refcount " |
4151 | "for feature %s [error=%d]", | |
4152 | spa_feature_table[i].fi_guid, error); | |
b0bc7a84 MG |
4153 | return (spa_vdev_err(rvd, |
4154 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
4155 | } | |
4156 | } | |
4157 | } | |
4158 | ||
4159 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
4160 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
4a0ee12a | 4161 | &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) |
b0bc7a84 | 4162 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
9ae529ec CS |
4163 | } |
4164 | ||
f00ab3f2 TC |
4165 | /* |
4166 | * Encryption was added before bookmark_v2, even though bookmark_v2 | |
4167 | * is now a dependency. If this pool has encryption enabled without | |
4168 | * bookmark_v2, trigger an errata message. | |
4169 | */ | |
4170 | if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && | |
4171 | !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) { | |
4172 | spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; | |
4173 | } | |
4174 | ||
9eb7b46e PZ |
4175 | return (0); |
4176 | } | |
4177 | ||
4178 | static int | |
4179 | spa_ld_load_special_directories(spa_t *spa) | |
4180 | { | |
4181 | int error = 0; | |
4182 | vdev_t *rvd = spa->spa_root_vdev; | |
4183 | ||
9ae529ec CS |
4184 | spa->spa_is_initializing = B_TRUE; |
4185 | error = dsl_pool_open(spa->spa_dsl_pool); | |
4186 | spa->spa_is_initializing = B_FALSE; | |
4a0ee12a PZ |
4187 | if (error != 0) { |
4188 | spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); | |
9ae529ec | 4189 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4190 | } |
9ae529ec | 4191 | |
9eb7b46e PZ |
4192 | return (0); |
4193 | } | |
428870ff | 4194 | |
9eb7b46e PZ |
4195 | static int |
4196 | spa_ld_get_props(spa_t *spa) | |
4197 | { | |
4198 | int error = 0; | |
4199 | uint64_t obj; | |
4200 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4201 | |
3c67d83a TH |
4202 | /* Grab the checksum salt from the MOS. */ |
4203 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4204 | DMU_POOL_CHECKSUM_SALT, 1, | |
4205 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
4206 | spa->spa_cksum_salt.zcs_bytes); | |
4207 | if (error == ENOENT) { | |
4208 | /* Generate a new salt for subsequent use */ | |
4209 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
4210 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
4211 | } else if (error != 0) { | |
4a0ee12a PZ |
4212 | spa_load_failed(spa, "unable to retrieve checksum salt from " |
4213 | "MOS [error=%d]", error); | |
3c67d83a TH |
4214 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4215 | } | |
4216 | ||
4a0ee12a | 4217 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) |
428870ff BB |
4218 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4219 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
4a0ee12a PZ |
4220 | if (error != 0) { |
4221 | spa_load_failed(spa, "error opening deferred-frees bpobj " | |
4222 | "[error=%d]", error); | |
428870ff | 4223 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4224 | } |
34dc7c2f BB |
4225 | |
4226 | /* | |
4227 | * Load the bit that tells us to use the new accounting function | |
4228 | * (raid-z deflation). If we have an older pool, this will not | |
4229 | * be present. | |
4230 | */ | |
4a0ee12a | 4231 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); |
428870ff BB |
4232 | if (error != 0 && error != ENOENT) |
4233 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4234 | ||
4235 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
4a0ee12a | 4236 | &spa->spa_creation_version, B_FALSE); |
428870ff BB |
4237 | if (error != 0 && error != ENOENT) |
4238 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
4239 | |
4240 | /* | |
4241 | * Load the persistent error log. If we have an older pool, this will | |
4242 | * not be present. | |
4243 | */ | |
4a0ee12a PZ |
4244 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, |
4245 | B_FALSE); | |
428870ff BB |
4246 | if (error != 0 && error != ENOENT) |
4247 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4248 | |
428870ff | 4249 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
4a0ee12a | 4250 | &spa->spa_errlog_scrub, B_FALSE); |
428870ff BB |
4251 | if (error != 0 && error != ENOENT) |
4252 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4253 | |
37f03da8 SH |
4254 | /* |
4255 | * Load the livelist deletion field. If a livelist is queued for | |
4256 | * deletion, indicate that in the spa | |
4257 | */ | |
4258 | error = spa_dir_prop(spa, DMU_POOL_DELETED_CLONES, | |
4259 | &spa->spa_livelists_to_delete, B_FALSE); | |
4260 | if (error != 0 && error != ENOENT) | |
4261 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4262 | ||
34dc7c2f BB |
4263 | /* |
4264 | * Load the history object. If we have an older pool, this | |
4265 | * will not be present. | |
4266 | */ | |
4a0ee12a | 4267 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); |
428870ff BB |
4268 | if (error != 0 && error != ENOENT) |
4269 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4270 | ||
e0ab3ab5 JS |
4271 | /* |
4272 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
4273 | * be present; in this case, defer its creation to a later time to | |
4274 | * avoid dirtying the MOS this early / out of sync context. See | |
4275 | * spa_sync_config_object. | |
4276 | */ | |
4277 | ||
4278 | /* The sentinel is only available in the MOS config. */ | |
1c27024e | 4279 | nvlist_t *mos_config; |
4a0ee12a PZ |
4280 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { |
4281 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
e0ab3ab5 | 4282 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4283 | } |
e0ab3ab5 JS |
4284 | |
4285 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
4a0ee12a | 4286 | &spa->spa_all_vdev_zaps, B_FALSE); |
e0ab3ab5 | 4287 | |
38640550 DB |
4288 | if (error == ENOENT) { |
4289 | VERIFY(!nvlist_exists(mos_config, | |
4290 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
4291 | spa->spa_avz_action = AVZ_ACTION_INITIALIZE; | |
4292 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4293 | } else if (error != 0) { | |
cb01da68 | 4294 | nvlist_free(mos_config); |
e0ab3ab5 | 4295 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
38640550 | 4296 | } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { |
e0ab3ab5 JS |
4297 | /* |
4298 | * An older version of ZFS overwrote the sentinel value, so | |
4299 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
4300 | * destruction to later; see spa_sync_config_object. | |
4301 | */ | |
4302 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
4303 | /* | |
4304 | * We're assuming that no vdevs have had their ZAPs created | |
4305 | * before this. Better be sure of it. | |
4306 | */ | |
4307 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4308 | } | |
4309 | nvlist_free(mos_config); | |
4310 | ||
9eb7b46e PZ |
4311 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
4312 | ||
4a0ee12a PZ |
4313 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, |
4314 | B_FALSE); | |
9eb7b46e PZ |
4315 | if (error && error != ENOENT) |
4316 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4317 | ||
4318 | if (error == 0) { | |
da27b8bc | 4319 | uint64_t autoreplace = 0; |
9eb7b46e PZ |
4320 | |
4321 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
4322 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
4323 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
4324 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
4325 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
c02c1bec | 4326 | spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); |
1b939560 | 4327 | spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim); |
9eb7b46e PZ |
4328 | spa->spa_autoreplace = (autoreplace != 0); |
4329 | } | |
4330 | ||
6cb8e530 PZ |
4331 | /* |
4332 | * If we are importing a pool with missing top-level vdevs, | |
4333 | * we enforce that the pool doesn't panic or get suspended on | |
4334 | * error since the likelihood of missing data is extremely high. | |
4335 | */ | |
4336 | if (spa->spa_missing_tvds > 0 && | |
4337 | spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && | |
4338 | spa->spa_load_state != SPA_LOAD_TRYIMPORT) { | |
4339 | spa_load_note(spa, "forcing failmode to 'continue' " | |
4340 | "as some top level vdevs are missing"); | |
4341 | spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; | |
4342 | } | |
4343 | ||
9eb7b46e PZ |
4344 | return (0); |
4345 | } | |
4346 | ||
4347 | static int | |
4348 | spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) | |
4349 | { | |
4350 | int error = 0; | |
4351 | vdev_t *rvd = spa->spa_root_vdev; | |
4352 | ||
428870ff BB |
4353 | /* |
4354 | * If we're assembling the pool from the split-off vdevs of | |
4355 | * an existing pool, we don't want to attach the spares & cache | |
4356 | * devices. | |
4357 | */ | |
34dc7c2f BB |
4358 | |
4359 | /* | |
4360 | * Load any hot spares for this pool. | |
4361 | */ | |
4a0ee12a PZ |
4362 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, |
4363 | B_FALSE); | |
428870ff BB |
4364 | if (error != 0 && error != ENOENT) |
4365 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4366 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4367 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
4368 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
4a0ee12a PZ |
4369 | &spa->spa_spares.sav_config) != 0) { |
4370 | spa_load_failed(spa, "error loading spares nvlist"); | |
428870ff | 4371 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4372 | } |
34dc7c2f | 4373 | |
b128c09f | 4374 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4375 | spa_load_spares(spa); |
b128c09f | 4376 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4377 | } else if (error == 0) { |
4378 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
4379 | } |
4380 | ||
4381 | /* | |
4382 | * Load any level 2 ARC devices for this pool. | |
4383 | */ | |
428870ff | 4384 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
4a0ee12a | 4385 | &spa->spa_l2cache.sav_object, B_FALSE); |
428870ff BB |
4386 | if (error != 0 && error != ENOENT) |
4387 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4388 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4389 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
4390 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
4a0ee12a PZ |
4391 | &spa->spa_l2cache.sav_config) != 0) { |
4392 | spa_load_failed(spa, "error loading l2cache nvlist"); | |
428870ff | 4393 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4394 | } |
34dc7c2f | 4395 | |
b128c09f | 4396 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4397 | spa_load_l2cache(spa); |
b128c09f | 4398 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4399 | } else if (error == 0) { |
4400 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
4401 | } |
4402 | ||
9eb7b46e PZ |
4403 | return (0); |
4404 | } | |
428870ff | 4405 | |
9eb7b46e | 4406 | static int |
4a0ee12a | 4407 | spa_ld_load_vdev_metadata(spa_t *spa) |
9eb7b46e PZ |
4408 | { |
4409 | int error = 0; | |
4410 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4411 | |
379ca9cf OF |
4412 | /* |
4413 | * If the 'multihost' property is set, then never allow a pool to | |
4414 | * be imported when the system hostid is zero. The exception to | |
4415 | * this rule is zdb which is always allowed to access pools. | |
4416 | */ | |
25f06d67 | 4417 | if (spa_multihost(spa) && spa_get_hostid(spa) == 0 && |
379ca9cf OF |
4418 | (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { |
4419 | fnvlist_add_uint64(spa->spa_load_info, | |
4420 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
4421 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
4422 | } | |
4423 | ||
34dc7c2f BB |
4424 | /* |
4425 | * If the 'autoreplace' property is set, then post a resource notifying | |
4426 | * the ZFS DE that it should not issue any faults for unopenable | |
4427 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
4428 | * unopenable vdevs so that the normal autoreplace handler can take | |
4429 | * over. | |
4430 | */ | |
4a0ee12a | 4431 | if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 4432 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
4433 | /* |
4434 | * For the import case, this is done in spa_import(), because | |
4435 | * at this point we're using the spare definitions from | |
4436 | * the MOS config, not necessarily from the userland config. | |
4437 | */ | |
4a0ee12a | 4438 | if (spa->spa_load_state != SPA_LOAD_IMPORT) { |
428870ff BB |
4439 | spa_aux_check_removed(&spa->spa_spares); |
4440 | spa_aux_check_removed(&spa->spa_l2cache); | |
4441 | } | |
4442 | } | |
34dc7c2f BB |
4443 | |
4444 | /* | |
9eb7b46e | 4445 | * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. |
34dc7c2f | 4446 | */ |
a1d477c2 MA |
4447 | error = vdev_load(rvd); |
4448 | if (error != 0) { | |
4a0ee12a | 4449 | spa_load_failed(spa, "vdev_load failed [error=%d]", error); |
a1d477c2 MA |
4450 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
4451 | } | |
4452 | ||
93e28d66 SD |
4453 | error = spa_ld_log_spacemaps(spa); |
4454 | if (error != 0) { | |
600a02b8 | 4455 | spa_load_failed(spa, "spa_ld_log_spacemaps failed [error=%d]", |
93e28d66 SD |
4456 | error); |
4457 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
4458 | } | |
4459 | ||
34dc7c2f | 4460 | /* |
9eb7b46e | 4461 | * Propagate the leaf DTLs we just loaded all the way up the vdev tree. |
34dc7c2f | 4462 | */ |
b128c09f | 4463 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9a49d3f3 | 4464 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE, B_FALSE); |
b128c09f | 4465 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 4466 | |
9eb7b46e PZ |
4467 | return (0); |
4468 | } | |
4469 | ||
4470 | static int | |
4471 | spa_ld_load_dedup_tables(spa_t *spa) | |
4472 | { | |
4473 | int error = 0; | |
4474 | vdev_t *rvd = spa->spa_root_vdev; | |
4475 | ||
428870ff | 4476 | error = ddt_load(spa); |
4a0ee12a PZ |
4477 | if (error != 0) { |
4478 | spa_load_failed(spa, "ddt_load failed [error=%d]", error); | |
428870ff | 4479 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4480 | } |
428870ff | 4481 | |
9eb7b46e PZ |
4482 | return (0); |
4483 | } | |
4484 | ||
67a1b037 PJD |
4485 | static int |
4486 | spa_ld_load_brt(spa_t *spa) | |
4487 | { | |
4488 | int error = 0; | |
4489 | vdev_t *rvd = spa->spa_root_vdev; | |
4490 | ||
4491 | error = brt_load(spa); | |
4492 | if (error != 0) { | |
4493 | spa_load_failed(spa, "brt_load failed [error=%d]", error); | |
4494 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4495 | } | |
4496 | ||
4497 | return (0); | |
4498 | } | |
4499 | ||
9eb7b46e | 4500 | static int |
a926aab9 | 4501 | spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, const char **ereport) |
9eb7b46e PZ |
4502 | { |
4503 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 4504 | |
4a0ee12a PZ |
4505 | if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { |
4506 | boolean_t missing = spa_check_logs(spa); | |
4507 | if (missing) { | |
6cb8e530 PZ |
4508 | if (spa->spa_missing_tvds != 0) { |
4509 | spa_load_note(spa, "spa_check_logs failed " | |
4510 | "so dropping the logs"); | |
4511 | } else { | |
4512 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
4513 | spa_load_failed(spa, "spa_check_logs failed"); | |
4514 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, | |
4515 | ENXIO)); | |
4516 | } | |
4a0ee12a | 4517 | } |
428870ff BB |
4518 | } |
4519 | ||
9eb7b46e PZ |
4520 | return (0); |
4521 | } | |
4522 | ||
4523 | static int | |
4a0ee12a | 4524 | spa_ld_verify_pool_data(spa_t *spa) |
9eb7b46e PZ |
4525 | { |
4526 | int error = 0; | |
4527 | vdev_t *rvd = spa->spa_root_vdev; | |
4528 | ||
4529 | /* | |
4530 | * We've successfully opened the pool, verify that we're ready | |
4531 | * to start pushing transactions. | |
4532 | */ | |
4a0ee12a | 4533 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
9eb7b46e PZ |
4534 | error = spa_load_verify(spa); |
4535 | if (error != 0) { | |
4a0ee12a PZ |
4536 | spa_load_failed(spa, "spa_load_verify failed " |
4537 | "[error=%d]", error); | |
9eb7b46e PZ |
4538 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
4539 | error)); | |
4540 | } | |
4541 | } | |
4542 | ||
4543 | return (0); | |
4544 | } | |
4545 | ||
4546 | static void | |
4547 | spa_ld_claim_log_blocks(spa_t *spa) | |
4548 | { | |
4549 | dmu_tx_t *tx; | |
4550 | dsl_pool_t *dp = spa_get_dsl(spa); | |
4551 | ||
4552 | /* | |
4553 | * Claim log blocks that haven't been committed yet. | |
4554 | * This must all happen in a single txg. | |
4555 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
4556 | * invoked from zil_claim_log_block()'s i/o done callback. | |
4557 | * Price of rollback is that we abandon the log. | |
4558 | */ | |
4559 | spa->spa_claiming = B_TRUE; | |
4560 | ||
4561 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
4562 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
4563 | zil_claim, tx, DS_FIND_CHILDREN); | |
4564 | dmu_tx_commit(tx); | |
4565 | ||
4566 | spa->spa_claiming = B_FALSE; | |
4567 | ||
4568 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
4569 | } | |
4570 | ||
4571 | static void | |
6cb8e530 | 4572 | spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, |
d2734cce | 4573 | boolean_t update_config_cache) |
9eb7b46e PZ |
4574 | { |
4575 | vdev_t *rvd = spa->spa_root_vdev; | |
4576 | int need_update = B_FALSE; | |
4577 | ||
4578 | /* | |
4579 | * If the config cache is stale, or we have uninitialized | |
4580 | * metaslabs (see spa_vdev_add()), then update the config. | |
4581 | * | |
4582 | * If this is a verbatim import, trust the current | |
4583 | * in-core spa_config and update the disk labels. | |
4584 | */ | |
d2734cce | 4585 | if (update_config_cache || config_cache_txg != spa->spa_config_txg || |
4a0ee12a PZ |
4586 | spa->spa_load_state == SPA_LOAD_IMPORT || |
4587 | spa->spa_load_state == SPA_LOAD_RECOVER || | |
9eb7b46e PZ |
4588 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) |
4589 | need_update = B_TRUE; | |
4590 | ||
4591 | for (int c = 0; c < rvd->vdev_children; c++) | |
4592 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
4593 | need_update = B_TRUE; | |
4594 | ||
4595 | /* | |
e1cfd73f | 4596 | * Update the config cache asynchronously in case we're the |
9eb7b46e PZ |
4597 | * root pool, in which case the config cache isn't writable yet. |
4598 | */ | |
4599 | if (need_update) | |
4600 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
4601 | } | |
4602 | ||
6cb8e530 PZ |
4603 | static void |
4604 | spa_ld_prepare_for_reload(spa_t *spa) | |
4605 | { | |
da92d5cb | 4606 | spa_mode_t mode = spa->spa_mode; |
6cb8e530 PZ |
4607 | int async_suspended = spa->spa_async_suspended; |
4608 | ||
4609 | spa_unload(spa); | |
4610 | spa_deactivate(spa); | |
4611 | spa_activate(spa, mode); | |
4612 | ||
4613 | /* | |
4614 | * We save the value of spa_async_suspended as it gets reset to 0 by | |
4615 | * spa_unload(). We want to restore it back to the original value before | |
4616 | * returning as we might be calling spa_async_resume() later. | |
4617 | */ | |
4618 | spa->spa_async_suspended = async_suspended; | |
4619 | } | |
4620 | ||
9eb7b46e | 4621 | static int |
d2734cce SD |
4622 | spa_ld_read_checkpoint_txg(spa_t *spa) |
4623 | { | |
4624 | uberblock_t checkpoint; | |
4625 | int error = 0; | |
4626 | ||
4627 | ASSERT0(spa->spa_checkpoint_txg); | |
4628 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4629 | ||
4630 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4631 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4632 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4633 | ||
4634 | if (error == ENOENT) | |
4635 | return (0); | |
4636 | ||
4637 | if (error != 0) | |
4638 | return (error); | |
4639 | ||
4640 | ASSERT3U(checkpoint.ub_txg, !=, 0); | |
4641 | ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); | |
4642 | ASSERT3U(checkpoint.ub_timestamp, !=, 0); | |
4643 | spa->spa_checkpoint_txg = checkpoint.ub_txg; | |
4644 | spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; | |
4645 | ||
4646 | return (0); | |
4647 | } | |
4648 | ||
4649 | static int | |
4650 | spa_ld_mos_init(spa_t *spa, spa_import_type_t type) | |
9eb7b46e PZ |
4651 | { |
4652 | int error = 0; | |
9eb7b46e | 4653 | |
4a0ee12a | 4654 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6cb8e530 | 4655 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); |
4a0ee12a | 4656 | |
9eb7b46e | 4657 | /* |
6cb8e530 PZ |
4658 | * Never trust the config that is provided unless we are assembling |
4659 | * a pool following a split. | |
4660 | * This means don't trust blkptrs and the vdev tree in general. This | |
4661 | * also effectively puts the spa in read-only mode since | |
4662 | * spa_writeable() checks for spa_trust_config to be true. | |
4663 | * We will later load a trusted config from the MOS. | |
9eb7b46e | 4664 | */ |
6cb8e530 PZ |
4665 | if (type != SPA_IMPORT_ASSEMBLE) |
4666 | spa->spa_trust_config = B_FALSE; | |
4667 | ||
9eb7b46e PZ |
4668 | /* |
4669 | * Parse the config provided to create a vdev tree. | |
4670 | */ | |
6cb8e530 | 4671 | error = spa_ld_parse_config(spa, type); |
9eb7b46e PZ |
4672 | if (error != 0) |
4673 | return (error); | |
4674 | ||
ca95f70d OF |
4675 | spa_import_progress_add(spa); |
4676 | ||
9eb7b46e PZ |
4677 | /* |
4678 | * Now that we have the vdev tree, try to open each vdev. This involves | |
4679 | * opening the underlying physical device, retrieving its geometry and | |
4680 | * probing the vdev with a dummy I/O. The state of each vdev will be set | |
4681 | * based on the success of those operations. After this we'll be ready | |
4682 | * to read from the vdevs. | |
4683 | */ | |
4684 | error = spa_ld_open_vdevs(spa); | |
4685 | if (error != 0) | |
4686 | return (error); | |
4687 | ||
4688 | /* | |
4689 | * Read the label of each vdev and make sure that the GUIDs stored | |
4690 | * there match the GUIDs in the config provided. | |
6cb8e530 PZ |
4691 | * If we're assembling a new pool that's been split off from an |
4692 | * existing pool, the labels haven't yet been updated so we skip | |
4693 | * validation for now. | |
9eb7b46e | 4694 | */ |
6cb8e530 PZ |
4695 | if (type != SPA_IMPORT_ASSEMBLE) { |
4696 | error = spa_ld_validate_vdevs(spa); | |
4697 | if (error != 0) | |
4698 | return (error); | |
4699 | } | |
9eb7b46e PZ |
4700 | |
4701 | /* | |
d2734cce SD |
4702 | * Read all vdev labels to find the best uberblock (i.e. latest, |
4703 | * unless spa_load_max_txg is set) and store it in spa_uberblock. We | |
4704 | * get the list of features required to read blkptrs in the MOS from | |
4705 | * the vdev label with the best uberblock and verify that our version | |
4706 | * of zfs supports them all. | |
9eb7b46e | 4707 | */ |
6cb8e530 | 4708 | error = spa_ld_select_uberblock(spa, type); |
9eb7b46e PZ |
4709 | if (error != 0) |
4710 | return (error); | |
4711 | ||
4712 | /* | |
4713 | * Pass that uberblock to the dsl_pool layer which will open the root | |
4714 | * blkptr. This blkptr points to the latest version of the MOS and will | |
4715 | * allow us to read its contents. | |
4716 | */ | |
4717 | error = spa_ld_open_rootbp(spa); | |
4718 | if (error != 0) | |
4719 | return (error); | |
4720 | ||
d2734cce SD |
4721 | return (0); |
4722 | } | |
4723 | ||
4724 | static int | |
4725 | spa_ld_checkpoint_rewind(spa_t *spa) | |
4726 | { | |
4727 | uberblock_t checkpoint; | |
4728 | int error = 0; | |
4729 | ||
4730 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4731 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4732 | ||
4733 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4734 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4735 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4736 | ||
4737 | if (error != 0) { | |
4738 | spa_load_failed(spa, "unable to retrieve checkpointed " | |
4739 | "uberblock from the MOS config [error=%d]", error); | |
4740 | ||
4741 | if (error == ENOENT) | |
4742 | error = ZFS_ERR_NO_CHECKPOINT; | |
4743 | ||
4744 | return (error); | |
4745 | } | |
4746 | ||
4747 | ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); | |
4748 | ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); | |
4749 | ||
4750 | /* | |
4751 | * We need to update the txg and timestamp of the checkpointed | |
4752 | * uberblock to be higher than the latest one. This ensures that | |
4753 | * the checkpointed uberblock is selected if we were to close and | |
4754 | * reopen the pool right after we've written it in the vdev labels. | |
4755 | * (also see block comment in vdev_uberblock_compare) | |
4756 | */ | |
4757 | checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; | |
4758 | checkpoint.ub_timestamp = gethrestime_sec(); | |
4759 | ||
4760 | /* | |
4761 | * Set current uberblock to be the checkpointed uberblock. | |
4762 | */ | |
4763 | spa->spa_uberblock = checkpoint; | |
4764 | ||
4765 | /* | |
4766 | * If we are doing a normal rewind, then the pool is open for | |
4767 | * writing and we sync the "updated" checkpointed uberblock to | |
4768 | * disk. Once this is done, we've basically rewound the whole | |
4769 | * pool and there is no way back. | |
4770 | * | |
4771 | * There are cases when we don't want to attempt and sync the | |
4772 | * checkpointed uberblock to disk because we are opening a | |
4773 | * pool as read-only. Specifically, verifying the checkpointed | |
4774 | * state with zdb, and importing the checkpointed state to get | |
4775 | * a "preview" of its content. | |
4776 | */ | |
4777 | if (spa_writeable(spa)) { | |
4778 | vdev_t *rvd = spa->spa_root_vdev; | |
4779 | ||
4780 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4781 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; | |
4782 | int svdcount = 0; | |
4783 | int children = rvd->vdev_children; | |
29274c9f | 4784 | int c0 = random_in_range(children); |
d2734cce SD |
4785 | |
4786 | for (int c = 0; c < children; c++) { | |
4787 | vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; | |
4788 | ||
4789 | /* Stop when revisiting the first vdev */ | |
4790 | if (c > 0 && svd[0] == vd) | |
4791 | break; | |
4792 | ||
4793 | if (vd->vdev_ms_array == 0 || vd->vdev_islog || | |
4794 | !vdev_is_concrete(vd)) | |
4795 | continue; | |
4796 | ||
4797 | svd[svdcount++] = vd; | |
4798 | if (svdcount == SPA_SYNC_MIN_VDEVS) | |
4799 | break; | |
4800 | } | |
4801 | error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); | |
4802 | if (error == 0) | |
4803 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
4804 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4805 | ||
4806 | if (error != 0) { | |
4807 | spa_load_failed(spa, "failed to write checkpointed " | |
4808 | "uberblock to the vdev labels [error=%d]", error); | |
4809 | return (error); | |
4810 | } | |
4811 | } | |
4812 | ||
4813 | return (0); | |
4814 | } | |
4815 | ||
4816 | static int | |
4817 | spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, | |
4818 | boolean_t *update_config_cache) | |
4819 | { | |
4820 | int error; | |
4821 | ||
4822 | /* | |
4823 | * Parse the config for pool, open and validate vdevs, | |
4824 | * select an uberblock, and use that uberblock to open | |
4825 | * the MOS. | |
4826 | */ | |
4827 | error = spa_ld_mos_init(spa, type); | |
4828 | if (error != 0) | |
4829 | return (error); | |
4830 | ||
9eb7b46e | 4831 | /* |
6cb8e530 PZ |
4832 | * Retrieve the trusted config stored in the MOS and use it to create |
4833 | * a new, exact version of the vdev tree, then reopen all vdevs. | |
9eb7b46e | 4834 | */ |
d2734cce | 4835 | error = spa_ld_trusted_config(spa, type, B_FALSE); |
6cb8e530 | 4836 | if (error == EAGAIN) { |
d2734cce SD |
4837 | if (update_config_cache != NULL) |
4838 | *update_config_cache = B_TRUE; | |
4839 | ||
6cb8e530 PZ |
4840 | /* |
4841 | * Redo the loading process with the trusted config if it is | |
4842 | * too different from the untrusted config. | |
4843 | */ | |
4844 | spa_ld_prepare_for_reload(spa); | |
d2734cce SD |
4845 | spa_load_note(spa, "RELOADING"); |
4846 | error = spa_ld_mos_init(spa, type); | |
4847 | if (error != 0) | |
4848 | return (error); | |
4849 | ||
4850 | error = spa_ld_trusted_config(spa, type, B_TRUE); | |
4851 | if (error != 0) | |
4852 | return (error); | |
4853 | ||
6cb8e530 | 4854 | } else if (error != 0) { |
9eb7b46e | 4855 | return (error); |
6cb8e530 | 4856 | } |
9eb7b46e | 4857 | |
d2734cce SD |
4858 | return (0); |
4859 | } | |
4860 | ||
4861 | /* | |
4862 | * Load an existing storage pool, using the config provided. This config | |
4863 | * describes which vdevs are part of the pool and is later validated against | |
4864 | * partial configs present in each vdev's label and an entire copy of the | |
4865 | * config stored in the MOS. | |
4866 | */ | |
4867 | static int | |
a926aab9 | 4868 | spa_load_impl(spa_t *spa, spa_import_type_t type, const char **ereport) |
d2734cce SD |
4869 | { |
4870 | int error = 0; | |
4871 | boolean_t missing_feat_write = B_FALSE; | |
4872 | boolean_t checkpoint_rewind = | |
4873 | (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4874 | boolean_t update_config_cache = B_FALSE; | |
4875 | ||
4876 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4877 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); | |
4878 | ||
4879 | spa_load_note(spa, "LOADING"); | |
4880 | ||
4881 | error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); | |
4882 | if (error != 0) | |
4883 | return (error); | |
4884 | ||
4885 | /* | |
4886 | * If we are rewinding to the checkpoint then we need to repeat | |
4887 | * everything we've done so far in this function but this time | |
4888 | * selecting the checkpointed uberblock and using that to open | |
4889 | * the MOS. | |
4890 | */ | |
4891 | if (checkpoint_rewind) { | |
4892 | /* | |
4893 | * If we are rewinding to the checkpoint update config cache | |
4894 | * anyway. | |
4895 | */ | |
4896 | update_config_cache = B_TRUE; | |
4897 | ||
4898 | /* | |
4899 | * Extract the checkpointed uberblock from the current MOS | |
4900 | * and use this as the pool's uberblock from now on. If the | |
4901 | * pool is imported as writeable we also write the checkpoint | |
4902 | * uberblock to the labels, making the rewind permanent. | |
4903 | */ | |
4904 | error = spa_ld_checkpoint_rewind(spa); | |
4905 | if (error != 0) | |
4906 | return (error); | |
4907 | ||
4908 | /* | |
e1cfd73f | 4909 | * Redo the loading process again with the |
d2734cce SD |
4910 | * checkpointed uberblock. |
4911 | */ | |
4912 | spa_ld_prepare_for_reload(spa); | |
4913 | spa_load_note(spa, "LOADING checkpointed uberblock"); | |
4914 | error = spa_ld_mos_with_trusted_config(spa, type, NULL); | |
4915 | if (error != 0) | |
4916 | return (error); | |
4917 | } | |
4918 | ||
4919 | /* | |
4920 | * Retrieve the checkpoint txg if the pool has a checkpoint. | |
4921 | */ | |
4922 | error = spa_ld_read_checkpoint_txg(spa); | |
4923 | if (error != 0) | |
4924 | return (error); | |
4925 | ||
9eb7b46e PZ |
4926 | /* |
4927 | * Retrieve the mapping of indirect vdevs. Those vdevs were removed | |
4928 | * from the pool and their contents were re-mapped to other vdevs. Note | |
4929 | * that everything that we read before this step must have been | |
4930 | * rewritten on concrete vdevs after the last device removal was | |
4931 | * initiated. Otherwise we could be reading from indirect vdevs before | |
4932 | * we have loaded their mappings. | |
4933 | */ | |
4934 | error = spa_ld_open_indirect_vdev_metadata(spa); | |
4935 | if (error != 0) | |
4936 | return (error); | |
4937 | ||
4938 | /* | |
4939 | * Retrieve the full list of active features from the MOS and check if | |
4940 | * they are all supported. | |
4941 | */ | |
4a0ee12a | 4942 | error = spa_ld_check_features(spa, &missing_feat_write); |
9eb7b46e PZ |
4943 | if (error != 0) |
4944 | return (error); | |
4945 | ||
4946 | /* | |
4947 | * Load several special directories from the MOS needed by the dsl_pool | |
4948 | * layer. | |
4949 | */ | |
4950 | error = spa_ld_load_special_directories(spa); | |
4951 | if (error != 0) | |
4952 | return (error); | |
4953 | ||
9eb7b46e PZ |
4954 | /* |
4955 | * Retrieve pool properties from the MOS. | |
4956 | */ | |
4957 | error = spa_ld_get_props(spa); | |
4958 | if (error != 0) | |
4959 | return (error); | |
4960 | ||
4961 | /* | |
4962 | * Retrieve the list of auxiliary devices - cache devices and spares - | |
4963 | * and open them. | |
4964 | */ | |
4965 | error = spa_ld_open_aux_vdevs(spa, type); | |
4966 | if (error != 0) | |
4967 | return (error); | |
4968 | ||
4969 | /* | |
4970 | * Load the metadata for all vdevs. Also check if unopenable devices | |
4971 | * should be autoreplaced. | |
4972 | */ | |
4a0ee12a | 4973 | error = spa_ld_load_vdev_metadata(spa); |
9eb7b46e PZ |
4974 | if (error != 0) |
4975 | return (error); | |
4976 | ||
4977 | error = spa_ld_load_dedup_tables(spa); | |
4978 | if (error != 0) | |
4979 | return (error); | |
4980 | ||
67a1b037 PJD |
4981 | error = spa_ld_load_brt(spa); |
4982 | if (error != 0) | |
4983 | return (error); | |
4984 | ||
9eb7b46e PZ |
4985 | /* |
4986 | * Verify the logs now to make sure we don't have any unexpected errors | |
4987 | * when we claim log blocks later. | |
4988 | */ | |
4989 | error = spa_ld_verify_logs(spa, type, ereport); | |
4990 | if (error != 0) | |
4991 | return (error); | |
4992 | ||
9ae529ec | 4993 | if (missing_feat_write) { |
6cb8e530 | 4994 | ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); |
9ae529ec CS |
4995 | |
4996 | /* | |
4997 | * At this point, we know that we can open the pool in | |
4998 | * read-only mode but not read-write mode. We now have enough | |
4999 | * information and can return to userland. | |
5000 | */ | |
9eb7b46e PZ |
5001 | return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, |
5002 | ENOTSUP)); | |
9ae529ec CS |
5003 | } |
5004 | ||
572e2857 | 5005 | /* |
9eb7b46e PZ |
5006 | * Traverse the last txgs to make sure the pool was left off in a safe |
5007 | * state. When performing an extreme rewind, we verify the whole pool, | |
5008 | * which can take a very long time. | |
572e2857 | 5009 | */ |
4a0ee12a | 5010 | error = spa_ld_verify_pool_data(spa); |
9eb7b46e PZ |
5011 | if (error != 0) |
5012 | return (error); | |
572e2857 | 5013 | |
9eb7b46e PZ |
5014 | /* |
5015 | * Calculate the deflated space for the pool. This must be done before | |
5016 | * we write anything to the pool because we'd need to update the space | |
5017 | * accounting using the deflated sizes. | |
5018 | */ | |
5019 | spa_update_dspace(spa); | |
5020 | ||
5021 | /* | |
5022 | * We have now retrieved all the information we needed to open the | |
5023 | * pool. If we are importing the pool in read-write mode, a few | |
5024 | * additional steps must be performed to finish the import. | |
5025 | */ | |
6cb8e530 | 5026 | if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || |
428870ff | 5027 | spa->spa_load_max_txg == UINT64_MAX)) { |
6cb8e530 PZ |
5028 | uint64_t config_cache_txg = spa->spa_config_txg; |
5029 | ||
5030 | ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f | 5031 | |
d2734cce SD |
5032 | /* |
5033 | * In case of a checkpoint rewind, log the original txg | |
5034 | * of the checkpointed uberblock. | |
5035 | */ | |
5036 | if (checkpoint_rewind) { | |
5037 | spa_history_log_internal(spa, "checkpoint rewind", | |
5038 | NULL, "rewound state to txg=%llu", | |
5039 | (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); | |
5040 | } | |
5041 | ||
34dc7c2f | 5042 | /* |
9eb7b46e | 5043 | * Traverse the ZIL and claim all blocks. |
34dc7c2f | 5044 | */ |
9eb7b46e | 5045 | spa_ld_claim_log_blocks(spa); |
428870ff | 5046 | |
9eb7b46e PZ |
5047 | /* |
5048 | * Kick-off the syncing thread. | |
5049 | */ | |
34dc7c2f BB |
5050 | spa->spa_sync_on = B_TRUE; |
5051 | txg_sync_start(spa->spa_dsl_pool); | |
379ca9cf | 5052 | mmp_thread_start(spa); |
34dc7c2f BB |
5053 | |
5054 | /* | |
428870ff BB |
5055 | * Wait for all claims to sync. We sync up to the highest |
5056 | * claimed log block birth time so that claimed log blocks | |
5057 | * don't appear to be from the future. spa_claim_max_txg | |
9eb7b46e PZ |
5058 | * will have been set for us by ZIL traversal operations |
5059 | * performed above. | |
34dc7c2f | 5060 | */ |
428870ff | 5061 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
5062 | |
5063 | /* | |
9eb7b46e PZ |
5064 | * Check if we need to request an update of the config. On the |
5065 | * next sync, we would update the config stored in vdev labels | |
5066 | * and the cachefile (by default /etc/zfs/zpool.cache). | |
34dc7c2f | 5067 | */ |
6cb8e530 | 5068 | spa_ld_check_for_config_update(spa, config_cache_txg, |
d2734cce | 5069 | update_config_cache); |
fb5f0bc8 BB |
5070 | |
5071 | /* | |
9a49d3f3 BB |
5072 | * Check if a rebuild was in progress and if so resume it. |
5073 | * Then check all DTLs to see if anything needs resilvering. | |
5074 | * The resilver will be deferred if a rebuild was started. | |
fb5f0bc8 | 5075 | */ |
9a49d3f3 BB |
5076 | if (vdev_rebuild_active(spa->spa_root_vdev)) { |
5077 | vdev_rebuild_restart(spa); | |
5078 | } else if (!dsl_scan_resilvering(spa->spa_dsl_pool) && | |
5079 | vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { | |
fb5f0bc8 | 5080 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
9a49d3f3 | 5081 | } |
428870ff | 5082 | |
6f1ffb06 MA |
5083 | /* |
5084 | * Log the fact that we booted up (so that we can detect if | |
5085 | * we rebooted in the middle of an operation). | |
5086 | */ | |
d5e024cb | 5087 | spa_history_log_version(spa, "open", NULL); |
6f1ffb06 | 5088 | |
9b2266e3 SD |
5089 | spa_restart_removal(spa); |
5090 | spa_spawn_aux_threads(spa); | |
5091 | ||
428870ff BB |
5092 | /* |
5093 | * Delete any inconsistent datasets. | |
9b2266e3 SD |
5094 | * |
5095 | * Note: | |
5096 | * Since we may be issuing deletes for clones here, | |
5097 | * we make sure to do so after we've spawned all the | |
5098 | * auxiliary threads above (from which the livelist | |
5099 | * deletion zthr is part of). | |
428870ff BB |
5100 | */ |
5101 | (void) dmu_objset_find(spa_name(spa), | |
5102 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
5103 | ||
5104 | /* | |
5105 | * Clean up any stale temporary dataset userrefs. | |
5106 | */ | |
5107 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
a1d477c2 | 5108 | |
619f0976 GW |
5109 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
5110 | vdev_initialize_restart(spa->spa_root_vdev); | |
1b939560 BB |
5111 | vdev_trim_restart(spa->spa_root_vdev); |
5112 | vdev_autotrim_restart(spa); | |
619f0976 | 5113 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
5114 | } |
5115 | ||
ca95f70d | 5116 | spa_import_progress_remove(spa_guid(spa)); |
77f6826b GA |
5117 | spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD); |
5118 | ||
4a0ee12a PZ |
5119 | spa_load_note(spa, "LOADED"); |
5120 | ||
428870ff BB |
5121 | return (0); |
5122 | } | |
34dc7c2f | 5123 | |
428870ff | 5124 | static int |
6cb8e530 | 5125 | spa_load_retry(spa_t *spa, spa_load_state_t state) |
428870ff | 5126 | { |
da92d5cb | 5127 | spa_mode_t mode = spa->spa_mode; |
572e2857 | 5128 | |
428870ff BB |
5129 | spa_unload(spa); |
5130 | spa_deactivate(spa); | |
5131 | ||
dea377c0 | 5132 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; |
428870ff | 5133 | |
572e2857 | 5134 | spa_activate(spa, mode); |
428870ff BB |
5135 | spa_async_suspend(spa); |
5136 | ||
4a0ee12a PZ |
5137 | spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", |
5138 | (u_longlong_t)spa->spa_load_max_txg); | |
5139 | ||
6cb8e530 | 5140 | return (spa_load(spa, state, SPA_IMPORT_EXISTING)); |
428870ff BB |
5141 | } |
5142 | ||
9ae529ec CS |
5143 | /* |
5144 | * If spa_load() fails this function will try loading prior txg's. If | |
5145 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
5146 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
5147 | * function will not rewind the pool and will return the same error as | |
5148 | * spa_load(). | |
5149 | */ | |
428870ff | 5150 | static int |
6cb8e530 PZ |
5151 | spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, |
5152 | int rewind_flags) | |
428870ff | 5153 | { |
9ae529ec | 5154 | nvlist_t *loadinfo = NULL; |
428870ff BB |
5155 | nvlist_t *config = NULL; |
5156 | int load_error, rewind_error; | |
5157 | uint64_t safe_rewind_txg; | |
5158 | uint64_t min_txg; | |
5159 | ||
5160 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
5161 | spa->spa_load_max_txg = spa->spa_load_txg; | |
5162 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
5163 | } else { | |
5164 | spa->spa_load_max_txg = max_request; | |
dea377c0 MA |
5165 | if (max_request != UINT64_MAX) |
5166 | spa->spa_extreme_rewind = B_TRUE; | |
428870ff BB |
5167 | } |
5168 | ||
6cb8e530 | 5169 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); |
428870ff BB |
5170 | if (load_error == 0) |
5171 | return (0); | |
d2734cce SD |
5172 | if (load_error == ZFS_ERR_NO_CHECKPOINT) { |
5173 | /* | |
5174 | * When attempting checkpoint-rewind on a pool with no | |
5175 | * checkpoint, we should not attempt to load uberblocks | |
5176 | * from previous txgs when spa_load fails. | |
5177 | */ | |
5178 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
ca95f70d | 5179 | spa_import_progress_remove(spa_guid(spa)); |
d2734cce SD |
5180 | return (load_error); |
5181 | } | |
428870ff BB |
5182 | |
5183 | if (spa->spa_root_vdev != NULL) | |
5184 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
5185 | ||
5186 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
5187 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
5188 | ||
5189 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
5190 | nvlist_free(config); | |
ca95f70d | 5191 | spa_import_progress_remove(spa_guid(spa)); |
428870ff BB |
5192 | return (load_error); |
5193 | } | |
5194 | ||
9ae529ec CS |
5195 | if (state == SPA_LOAD_RECOVER) { |
5196 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 5197 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
5198 | } else { |
5199 | /* | |
5200 | * If we aren't rolling back save the load info from our first | |
5201 | * import attempt so that we can restore it after attempting | |
5202 | * to rewind. | |
5203 | */ | |
5204 | loadinfo = spa->spa_load_info; | |
5205 | spa->spa_load_info = fnvlist_alloc(); | |
5206 | } | |
428870ff BB |
5207 | |
5208 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
5209 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
5210 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
5211 | TXG_INITIAL : safe_rewind_txg; | |
5212 | ||
5213 | /* | |
5214 | * Continue as long as we're finding errors, we're still within | |
5215 | * the acceptable rewind range, and we're still finding uberblocks | |
5216 | */ | |
5217 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
5218 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
5219 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
5220 | spa->spa_extreme_rewind = B_TRUE; | |
6cb8e530 | 5221 | rewind_error = spa_load_retry(spa, state); |
428870ff BB |
5222 | } |
5223 | ||
428870ff BB |
5224 | spa->spa_extreme_rewind = B_FALSE; |
5225 | spa->spa_load_max_txg = UINT64_MAX; | |
5226 | ||
5227 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
5228 | spa_config_set(spa, config); | |
ee6370a7 | 5229 | else |
5230 | nvlist_free(config); | |
428870ff | 5231 | |
9ae529ec CS |
5232 | if (state == SPA_LOAD_RECOVER) { |
5233 | ASSERT3P(loadinfo, ==, NULL); | |
ca95f70d | 5234 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
5235 | return (rewind_error); |
5236 | } else { | |
5237 | /* Store the rewind info as part of the initial load info */ | |
5238 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
5239 | spa->spa_load_info); | |
5240 | ||
5241 | /* Restore the initial load info */ | |
5242 | fnvlist_free(spa->spa_load_info); | |
5243 | spa->spa_load_info = loadinfo; | |
5244 | ||
ca95f70d | 5245 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
5246 | return (load_error); |
5247 | } | |
34dc7c2f BB |
5248 | } |
5249 | ||
5250 | /* | |
5251 | * Pool Open/Import | |
5252 | * | |
5253 | * The import case is identical to an open except that the configuration is sent | |
5254 | * down from userland, instead of grabbed from the configuration cache. For the | |
5255 | * case of an open, the pool configuration will exist in the | |
5256 | * POOL_STATE_UNINITIALIZED state. | |
5257 | * | |
5258 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
5259 | * the same time open the pool, without having to keep around the spa_t in some | |
5260 | * ambiguous state. | |
5261 | */ | |
5262 | static int | |
a926aab9 AZ |
5263 | spa_open_common(const char *pool, spa_t **spapp, const void *tag, |
5264 | nvlist_t *nvpolicy, nvlist_t **config) | |
34dc7c2f BB |
5265 | { |
5266 | spa_t *spa; | |
572e2857 | 5267 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 5268 | int error; |
34dc7c2f | 5269 | int locked = B_FALSE; |
526af785 | 5270 | int firstopen = B_FALSE; |
34dc7c2f BB |
5271 | |
5272 | *spapp = NULL; | |
5273 | ||
5274 | /* | |
5275 | * As disgusting as this is, we need to support recursive calls to this | |
5276 | * function because dsl_dir_open() is called during spa_load(), and ends | |
5277 | * up calling spa_open() again. The real fix is to figure out how to | |
5278 | * avoid dsl_dir_open() calling this in the first place. | |
5279 | */ | |
c25b8f99 | 5280 | if (MUTEX_NOT_HELD(&spa_namespace_lock)) { |
34dc7c2f BB |
5281 | mutex_enter(&spa_namespace_lock); |
5282 | locked = B_TRUE; | |
5283 | } | |
5284 | ||
5285 | if ((spa = spa_lookup(pool)) == NULL) { | |
5286 | if (locked) | |
5287 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5288 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 5289 | } |
428870ff | 5290 | |
34dc7c2f | 5291 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
8a393be3 | 5292 | zpool_load_policy_t policy; |
428870ff | 5293 | |
526af785 PJD |
5294 | firstopen = B_TRUE; |
5295 | ||
8a393be3 | 5296 | zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, |
428870ff | 5297 | &policy); |
8a393be3 | 5298 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) |
428870ff | 5299 | state = SPA_LOAD_RECOVER; |
34dc7c2f | 5300 | |
fb5f0bc8 | 5301 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5302 | |
428870ff BB |
5303 | if (state != SPA_LOAD_RECOVER) |
5304 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6cb8e530 | 5305 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; |
428870ff | 5306 | |
4a0ee12a | 5307 | zfs_dbgmsg("spa_open_common: opening %s", pool); |
8a393be3 PZ |
5308 | error = spa_load_best(spa, state, policy.zlp_txg, |
5309 | policy.zlp_rewind); | |
34dc7c2f BB |
5310 | |
5311 | if (error == EBADF) { | |
5312 | /* | |
5313 | * If vdev_validate() returns failure (indicated by | |
5314 | * EBADF), it indicates that one of the vdevs indicates | |
5315 | * that the pool has been exported or destroyed. If | |
5316 | * this is the case, the config cache is out of sync and | |
5317 | * we should remove the pool from the namespace. | |
5318 | */ | |
34dc7c2f BB |
5319 | spa_unload(spa); |
5320 | spa_deactivate(spa); | |
55c12724 | 5321 | spa_write_cachefile(spa, B_TRUE, B_TRUE, B_FALSE); |
34dc7c2f | 5322 | spa_remove(spa); |
34dc7c2f BB |
5323 | if (locked) |
5324 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5325 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
5326 | } |
5327 | ||
5328 | if (error) { | |
5329 | /* | |
5330 | * We can't open the pool, but we still have useful | |
5331 | * information: the state of each vdev after the | |
5332 | * attempted vdev_open(). Return this to the user. | |
5333 | */ | |
572e2857 | 5334 | if (config != NULL && spa->spa_config) { |
65ad5d11 AJ |
5335 | *config = fnvlist_dup(spa->spa_config); |
5336 | fnvlist_add_nvlist(*config, | |
572e2857 | 5337 | ZPOOL_CONFIG_LOAD_INFO, |
65ad5d11 | 5338 | spa->spa_load_info); |
572e2857 | 5339 | } |
34dc7c2f BB |
5340 | spa_unload(spa); |
5341 | spa_deactivate(spa); | |
428870ff | 5342 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
5343 | if (locked) |
5344 | mutex_exit(&spa_namespace_lock); | |
5345 | *spapp = NULL; | |
5346 | return (error); | |
34dc7c2f | 5347 | } |
34dc7c2f BB |
5348 | } |
5349 | ||
5350 | spa_open_ref(spa, tag); | |
5351 | ||
b128c09f | 5352 | if (config != NULL) |
34dc7c2f | 5353 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 5354 | |
572e2857 BB |
5355 | /* |
5356 | * If we've recovered the pool, pass back any information we | |
5357 | * gathered while doing the load. | |
5358 | */ | |
1bd02680 | 5359 | if (state == SPA_LOAD_RECOVER && config != NULL) { |
65ad5d11 AJ |
5360 | fnvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, |
5361 | spa->spa_load_info); | |
572e2857 BB |
5362 | } |
5363 | ||
428870ff BB |
5364 | if (locked) { |
5365 | spa->spa_last_open_failed = 0; | |
5366 | spa->spa_last_ubsync_txg = 0; | |
5367 | spa->spa_load_txg = 0; | |
5368 | mutex_exit(&spa_namespace_lock); | |
5369 | } | |
5370 | ||
526af785 | 5371 | if (firstopen) |
ec213971 | 5372 | zvol_create_minors_recursive(spa_name(spa)); |
526af785 | 5373 | |
428870ff BB |
5374 | *spapp = spa; |
5375 | ||
34dc7c2f BB |
5376 | return (0); |
5377 | } | |
5378 | ||
428870ff | 5379 | int |
a926aab9 AZ |
5380 | spa_open_rewind(const char *name, spa_t **spapp, const void *tag, |
5381 | nvlist_t *policy, nvlist_t **config) | |
428870ff BB |
5382 | { |
5383 | return (spa_open_common(name, spapp, tag, policy, config)); | |
5384 | } | |
5385 | ||
34dc7c2f | 5386 | int |
a926aab9 | 5387 | spa_open(const char *name, spa_t **spapp, const void *tag) |
34dc7c2f | 5388 | { |
428870ff | 5389 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
5390 | } |
5391 | ||
5392 | /* | |
5393 | * Lookup the given spa_t, incrementing the inject count in the process, | |
5394 | * preventing it from being exported or destroyed. | |
5395 | */ | |
5396 | spa_t * | |
5397 | spa_inject_addref(char *name) | |
5398 | { | |
5399 | spa_t *spa; | |
5400 | ||
5401 | mutex_enter(&spa_namespace_lock); | |
5402 | if ((spa = spa_lookup(name)) == NULL) { | |
5403 | mutex_exit(&spa_namespace_lock); | |
5404 | return (NULL); | |
5405 | } | |
5406 | spa->spa_inject_ref++; | |
5407 | mutex_exit(&spa_namespace_lock); | |
5408 | ||
5409 | return (spa); | |
5410 | } | |
5411 | ||
5412 | void | |
5413 | spa_inject_delref(spa_t *spa) | |
5414 | { | |
5415 | mutex_enter(&spa_namespace_lock); | |
5416 | spa->spa_inject_ref--; | |
5417 | mutex_exit(&spa_namespace_lock); | |
5418 | } | |
5419 | ||
5420 | /* | |
5421 | * Add spares device information to the nvlist. | |
5422 | */ | |
5423 | static void | |
5424 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
5425 | { | |
5426 | nvlist_t **spares; | |
5427 | uint_t i, nspares; | |
5428 | nvlist_t *nvroot; | |
5429 | uint64_t guid; | |
5430 | vdev_stat_t *vs; | |
5431 | uint_t vsc; | |
5432 | uint64_t pool; | |
5433 | ||
9babb374 BB |
5434 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5435 | ||
34dc7c2f BB |
5436 | if (spa->spa_spares.sav_count == 0) |
5437 | return; | |
5438 | ||
65ad5d11 AJ |
5439 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
5440 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
5441 | ZPOOL_CONFIG_SPARES, &spares, &nspares)); | |
34dc7c2f | 5442 | if (nspares != 0) { |
795075e6 PD |
5443 | fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, |
5444 | (const nvlist_t * const *)spares, nspares); | |
65ad5d11 AJ |
5445 | VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, |
5446 | &spares, &nspares)); | |
34dc7c2f BB |
5447 | |
5448 | /* | |
5449 | * Go through and find any spares which have since been | |
5450 | * repurposed as an active spare. If this is the case, update | |
5451 | * their status appropriately. | |
5452 | */ | |
5453 | for (i = 0; i < nspares; i++) { | |
65ad5d11 AJ |
5454 | guid = fnvlist_lookup_uint64(spares[i], |
5455 | ZPOOL_CONFIG_GUID); | |
a05263b7 AH |
5456 | VERIFY0(nvlist_lookup_uint64_array(spares[i], |
5457 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)); | |
b128c09f BB |
5458 | if (spa_spare_exists(guid, &pool, NULL) && |
5459 | pool != 0ULL) { | |
34dc7c2f BB |
5460 | vs->vs_state = VDEV_STATE_CANT_OPEN; |
5461 | vs->vs_aux = VDEV_AUX_SPARED; | |
a05263b7 AH |
5462 | } else { |
5463 | vs->vs_state = | |
5464 | spa->spa_spares.sav_vdevs[i]->vdev_state; | |
34dc7c2f BB |
5465 | } |
5466 | } | |
5467 | } | |
5468 | } | |
5469 | ||
5470 | /* | |
5471 | * Add l2cache device information to the nvlist, including vdev stats. | |
5472 | */ | |
5473 | static void | |
5474 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
5475 | { | |
5476 | nvlist_t **l2cache; | |
5477 | uint_t i, j, nl2cache; | |
5478 | nvlist_t *nvroot; | |
5479 | uint64_t guid; | |
5480 | vdev_t *vd; | |
5481 | vdev_stat_t *vs; | |
5482 | uint_t vsc; | |
5483 | ||
9babb374 BB |
5484 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5485 | ||
34dc7c2f BB |
5486 | if (spa->spa_l2cache.sav_count == 0) |
5487 | return; | |
5488 | ||
65ad5d11 AJ |
5489 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
5490 | VERIFY0(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
5491 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache)); | |
34dc7c2f | 5492 | if (nl2cache != 0) { |
795075e6 PD |
5493 | fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
5494 | (const nvlist_t * const *)l2cache, nl2cache); | |
65ad5d11 AJ |
5495 | VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, |
5496 | &l2cache, &nl2cache)); | |
34dc7c2f BB |
5497 | |
5498 | /* | |
5499 | * Update level 2 cache device stats. | |
5500 | */ | |
5501 | ||
5502 | for (i = 0; i < nl2cache; i++) { | |
65ad5d11 AJ |
5503 | guid = fnvlist_lookup_uint64(l2cache[i], |
5504 | ZPOOL_CONFIG_GUID); | |
34dc7c2f BB |
5505 | |
5506 | vd = NULL; | |
5507 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
5508 | if (guid == | |
5509 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
5510 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
5511 | break; | |
5512 | } | |
5513 | } | |
5514 | ASSERT(vd != NULL); | |
5515 | ||
65ad5d11 AJ |
5516 | VERIFY0(nvlist_lookup_uint64_array(l2cache[i], |
5517 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)); | |
34dc7c2f | 5518 | vdev_get_stats(vd, vs); |
193a37cb TH |
5519 | vdev_config_generate_stats(vd, l2cache[i]); |
5520 | ||
34dc7c2f BB |
5521 | } |
5522 | } | |
34dc7c2f BB |
5523 | } |
5524 | ||
9ae529ec | 5525 | static void |
417104bd | 5526 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) |
9ae529ec | 5527 | { |
9ae529ec CS |
5528 | zap_cursor_t zc; |
5529 | zap_attribute_t za; | |
5530 | ||
9ae529ec CS |
5531 | if (spa->spa_feat_for_read_obj != 0) { |
5532 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5533 | spa->spa_feat_for_read_obj); | |
5534 | zap_cursor_retrieve(&zc, &za) == 0; | |
5535 | zap_cursor_advance(&zc)) { | |
5536 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5537 | za.za_num_integers == 1); | |
417104bd | 5538 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5539 | za.za_first_integer)); |
5540 | } | |
5541 | zap_cursor_fini(&zc); | |
5542 | } | |
5543 | ||
5544 | if (spa->spa_feat_for_write_obj != 0) { | |
5545 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5546 | spa->spa_feat_for_write_obj); | |
5547 | zap_cursor_retrieve(&zc, &za) == 0; | |
5548 | zap_cursor_advance(&zc)) { | |
5549 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5550 | za.za_num_integers == 1); | |
417104bd | 5551 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5552 | za.za_first_integer)); |
5553 | } | |
5554 | zap_cursor_fini(&zc); | |
5555 | } | |
417104bd NB |
5556 | } |
5557 | ||
5558 | static void | |
5559 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
5560 | { | |
5561 | int i; | |
5562 | ||
5563 | for (i = 0; i < SPA_FEATURES; i++) { | |
5564 | zfeature_info_t feature = spa_feature_table[i]; | |
5565 | uint64_t refcount; | |
5566 | ||
5567 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
5568 | continue; | |
5569 | ||
5570 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
5571 | } | |
5572 | } | |
5573 | ||
5574 | /* | |
5575 | * Store a list of pool features and their reference counts in the | |
5576 | * config. | |
5577 | * | |
5578 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
5579 | * the pool features and reference counts from disk, then save the list | |
5580 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
5581 | * and refresh its values from the cached reference counts. This | |
5582 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
5583 | * clear' can resume the pool. | |
5584 | */ | |
5585 | static void | |
5586 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
5587 | { | |
4eb30c68 | 5588 | nvlist_t *features; |
417104bd NB |
5589 | |
5590 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
5591 | ||
4eb30c68 NB |
5592 | mutex_enter(&spa->spa_feat_stats_lock); |
5593 | features = spa->spa_feat_stats; | |
5594 | ||
417104bd NB |
5595 | if (features != NULL) { |
5596 | spa_feature_stats_from_cache(spa, features); | |
5597 | } else { | |
5598 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
5599 | spa->spa_feat_stats = features; | |
5600 | spa_feature_stats_from_disk(spa, features); | |
5601 | } | |
9ae529ec | 5602 | |
417104bd NB |
5603 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, |
5604 | features)); | |
4eb30c68 NB |
5605 | |
5606 | mutex_exit(&spa->spa_feat_stats_lock); | |
9ae529ec CS |
5607 | } |
5608 | ||
34dc7c2f | 5609 | int |
9ae529ec CS |
5610 | spa_get_stats(const char *name, nvlist_t **config, |
5611 | char *altroot, size_t buflen) | |
34dc7c2f BB |
5612 | { |
5613 | int error; | |
5614 | spa_t *spa; | |
5615 | ||
5616 | *config = NULL; | |
428870ff | 5617 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 5618 | |
9babb374 BB |
5619 | if (spa != NULL) { |
5620 | /* | |
5621 | * This still leaves a window of inconsistency where the spares | |
5622 | * or l2cache devices could change and the config would be | |
5623 | * self-inconsistent. | |
5624 | */ | |
5625 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 5626 | |
9babb374 | 5627 | if (*config != NULL) { |
572e2857 BB |
5628 | uint64_t loadtimes[2]; |
5629 | ||
5630 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
5631 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
65ad5d11 AJ |
5632 | fnvlist_add_uint64_array(*config, |
5633 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2); | |
572e2857 | 5634 | |
65ad5d11 | 5635 | fnvlist_add_uint64(*config, |
9babb374 | 5636 | ZPOOL_CONFIG_ERRCOUNT, |
018f2604 | 5637 | spa_approx_errlog_size(spa)); |
9babb374 | 5638 | |
cec3a0a1 | 5639 | if (spa_suspended(spa)) { |
65ad5d11 | 5640 | fnvlist_add_uint64(*config, |
9babb374 | 5641 | ZPOOL_CONFIG_SUSPENDED, |
65ad5d11 AJ |
5642 | spa->spa_failmode); |
5643 | fnvlist_add_uint64(*config, | |
cec3a0a1 | 5644 | ZPOOL_CONFIG_SUSPENDED_REASON, |
65ad5d11 | 5645 | spa->spa_suspended); |
cec3a0a1 | 5646 | } |
b128c09f | 5647 | |
9babb374 BB |
5648 | spa_add_spares(spa, *config); |
5649 | spa_add_l2cache(spa, *config); | |
9ae529ec | 5650 | spa_add_feature_stats(spa, *config); |
9babb374 | 5651 | } |
34dc7c2f BB |
5652 | } |
5653 | ||
5654 | /* | |
5655 | * We want to get the alternate root even for faulted pools, so we cheat | |
5656 | * and call spa_lookup() directly. | |
5657 | */ | |
5658 | if (altroot) { | |
5659 | if (spa == NULL) { | |
5660 | mutex_enter(&spa_namespace_lock); | |
5661 | spa = spa_lookup(name); | |
5662 | if (spa) | |
5663 | spa_altroot(spa, altroot, buflen); | |
5664 | else | |
5665 | altroot[0] = '\0'; | |
5666 | spa = NULL; | |
5667 | mutex_exit(&spa_namespace_lock); | |
5668 | } else { | |
5669 | spa_altroot(spa, altroot, buflen); | |
5670 | } | |
5671 | } | |
5672 | ||
9babb374 BB |
5673 | if (spa != NULL) { |
5674 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 5675 | spa_close(spa, FTAG); |
9babb374 | 5676 | } |
34dc7c2f BB |
5677 | |
5678 | return (error); | |
5679 | } | |
5680 | ||
5681 | /* | |
5682 | * Validate that the auxiliary device array is well formed. We must have an | |
5683 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
5684 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
5685 | * specified, as long as they are well-formed. | |
5686 | */ | |
5687 | static int | |
5688 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
5689 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
5690 | vdev_labeltype_t label) | |
5691 | { | |
5692 | nvlist_t **dev; | |
5693 | uint_t i, ndev; | |
5694 | vdev_t *vd; | |
5695 | int error; | |
5696 | ||
b128c09f BB |
5697 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5698 | ||
34dc7c2f BB |
5699 | /* |
5700 | * It's acceptable to have no devs specified. | |
5701 | */ | |
5702 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
5703 | return (0); | |
5704 | ||
5705 | if (ndev == 0) | |
2e528b49 | 5706 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
5707 | |
5708 | /* | |
5709 | * Make sure the pool is formatted with a version that supports this | |
5710 | * device type. | |
5711 | */ | |
5712 | if (spa_version(spa) < version) | |
2e528b49 | 5713 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
5714 | |
5715 | /* | |
5716 | * Set the pending device list so we correctly handle device in-use | |
5717 | * checking. | |
5718 | */ | |
5719 | sav->sav_pending = dev; | |
5720 | sav->sav_npending = ndev; | |
5721 | ||
5722 | for (i = 0; i < ndev; i++) { | |
5723 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
5724 | mode)) != 0) | |
5725 | goto out; | |
5726 | ||
5727 | if (!vd->vdev_ops->vdev_op_leaf) { | |
5728 | vdev_free(vd); | |
2e528b49 | 5729 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5730 | goto out; |
5731 | } | |
5732 | ||
34dc7c2f BB |
5733 | vd->vdev_top = vd; |
5734 | ||
5735 | if ((error = vdev_open(vd)) == 0 && | |
5736 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
65ad5d11 AJ |
5737 | fnvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, |
5738 | vd->vdev_guid); | |
34dc7c2f BB |
5739 | } |
5740 | ||
5741 | vdev_free(vd); | |
5742 | ||
5743 | if (error && | |
5744 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
5745 | goto out; | |
5746 | else | |
5747 | error = 0; | |
5748 | } | |
5749 | ||
5750 | out: | |
5751 | sav->sav_pending = NULL; | |
5752 | sav->sav_npending = 0; | |
5753 | return (error); | |
5754 | } | |
5755 | ||
5756 | static int | |
5757 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
5758 | { | |
5759 | int error; | |
5760 | ||
b128c09f BB |
5761 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5762 | ||
34dc7c2f BB |
5763 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
5764 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
5765 | VDEV_LABEL_SPARE)) != 0) { | |
5766 | return (error); | |
5767 | } | |
5768 | ||
5769 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
5770 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
5771 | VDEV_LABEL_L2CACHE)); | |
5772 | } | |
5773 | ||
5774 | static void | |
5775 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
5776 | const char *config) | |
5777 | { | |
5778 | int i; | |
5779 | ||
5780 | if (sav->sav_config != NULL) { | |
5781 | nvlist_t **olddevs; | |
5782 | uint_t oldndevs; | |
5783 | nvlist_t **newdevs; | |
5784 | ||
5785 | /* | |
4e33ba4c | 5786 | * Generate new dev list by concatenating with the |
34dc7c2f BB |
5787 | * current dev list. |
5788 | */ | |
65ad5d11 AJ |
5789 | VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config, config, |
5790 | &olddevs, &oldndevs)); | |
34dc7c2f BB |
5791 | |
5792 | newdevs = kmem_alloc(sizeof (void *) * | |
79c76d5b | 5793 | (ndevs + oldndevs), KM_SLEEP); |
34dc7c2f | 5794 | for (i = 0; i < oldndevs; i++) |
65ad5d11 | 5795 | newdevs[i] = fnvlist_dup(olddevs[i]); |
34dc7c2f | 5796 | for (i = 0; i < ndevs; i++) |
65ad5d11 | 5797 | newdevs[i + oldndevs] = fnvlist_dup(devs[i]); |
34dc7c2f | 5798 | |
65ad5d11 | 5799 | fnvlist_remove(sav->sav_config, config); |
34dc7c2f | 5800 | |
795075e6 PD |
5801 | fnvlist_add_nvlist_array(sav->sav_config, config, |
5802 | (const nvlist_t * const *)newdevs, ndevs + oldndevs); | |
34dc7c2f BB |
5803 | for (i = 0; i < oldndevs + ndevs; i++) |
5804 | nvlist_free(newdevs[i]); | |
5805 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
5806 | } else { | |
5807 | /* | |
5808 | * Generate a new dev list. | |
5809 | */ | |
65ad5d11 | 5810 | sav->sav_config = fnvlist_alloc(); |
795075e6 PD |
5811 | fnvlist_add_nvlist_array(sav->sav_config, config, |
5812 | (const nvlist_t * const *)devs, ndevs); | |
34dc7c2f BB |
5813 | } |
5814 | } | |
5815 | ||
5816 | /* | |
5817 | * Stop and drop level 2 ARC devices | |
5818 | */ | |
5819 | void | |
5820 | spa_l2cache_drop(spa_t *spa) | |
5821 | { | |
5822 | vdev_t *vd; | |
5823 | int i; | |
5824 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
5825 | ||
5826 | for (i = 0; i < sav->sav_count; i++) { | |
5827 | uint64_t pool; | |
5828 | ||
5829 | vd = sav->sav_vdevs[i]; | |
5830 | ASSERT(vd != NULL); | |
5831 | ||
fb5f0bc8 BB |
5832 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
5833 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 5834 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
5835 | } |
5836 | } | |
5837 | ||
b5256303 TC |
5838 | /* |
5839 | * Verify encryption parameters for spa creation. If we are encrypting, we must | |
5840 | * have the encryption feature flag enabled. | |
5841 | */ | |
5842 | static int | |
5843 | spa_create_check_encryption_params(dsl_crypto_params_t *dcp, | |
5844 | boolean_t has_encryption) | |
5845 | { | |
5846 | if (dcp->cp_crypt != ZIO_CRYPT_OFF && | |
5847 | dcp->cp_crypt != ZIO_CRYPT_INHERIT && | |
5848 | !has_encryption) | |
5849 | return (SET_ERROR(ENOTSUP)); | |
5850 | ||
1fff937a | 5851 | return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); |
b5256303 TC |
5852 | } |
5853 | ||
34dc7c2f BB |
5854 | /* |
5855 | * Pool Creation | |
5856 | */ | |
5857 | int | |
5858 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b5256303 | 5859 | nvlist_t *zplprops, dsl_crypto_params_t *dcp) |
34dc7c2f BB |
5860 | { |
5861 | spa_t *spa; | |
d1807f16 | 5862 | const char *altroot = NULL; |
34dc7c2f BB |
5863 | vdev_t *rvd; |
5864 | dsl_pool_t *dp; | |
5865 | dmu_tx_t *tx; | |
9babb374 | 5866 | int error = 0; |
34dc7c2f BB |
5867 | uint64_t txg = TXG_INITIAL; |
5868 | nvlist_t **spares, **l2cache; | |
5869 | uint_t nspares, nl2cache; | |
b2255edc | 5870 | uint64_t version, obj, ndraid = 0; |
9ae529ec | 5871 | boolean_t has_features; |
b5256303 | 5872 | boolean_t has_encryption; |
715c996d | 5873 | boolean_t has_allocclass; |
b5256303 | 5874 | spa_feature_t feat; |
d1807f16 RY |
5875 | const char *feat_name; |
5876 | const char *poolname; | |
83e9986f RY |
5877 | nvlist_t *nvl; |
5878 | ||
cc99f275 DB |
5879 | if (props == NULL || |
5880 | nvlist_lookup_string(props, "tname", &poolname) != 0) | |
83e9986f | 5881 | poolname = (char *)pool; |
34dc7c2f BB |
5882 | |
5883 | /* | |
5884 | * If this pool already exists, return failure. | |
5885 | */ | |
5886 | mutex_enter(&spa_namespace_lock); | |
83e9986f | 5887 | if (spa_lookup(poolname) != NULL) { |
34dc7c2f | 5888 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5889 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5890 | } |
5891 | ||
5892 | /* | |
5893 | * Allocate a new spa_t structure. | |
5894 | */ | |
83e9986f RY |
5895 | nvl = fnvlist_alloc(); |
5896 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
34dc7c2f BB |
5897 | (void) nvlist_lookup_string(props, |
5898 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
83e9986f RY |
5899 | spa = spa_add(poolname, nvl, altroot); |
5900 | fnvlist_free(nvl); | |
fb5f0bc8 | 5901 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5902 | |
34dc7c2f | 5903 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
5904 | spa_deactivate(spa); |
5905 | spa_remove(spa); | |
b128c09f | 5906 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
5907 | return (error); |
5908 | } | |
5909 | ||
83e9986f RY |
5910 | /* |
5911 | * Temporary pool names should never be written to disk. | |
5912 | */ | |
5913 | if (poolname != pool) | |
5914 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
5915 | ||
9ae529ec | 5916 | has_features = B_FALSE; |
b5256303 | 5917 | has_encryption = B_FALSE; |
715c996d | 5918 | has_allocclass = B_FALSE; |
1c27024e | 5919 | for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); |
9ae529ec | 5920 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { |
b5256303 | 5921 | if (zpool_prop_feature(nvpair_name(elem))) { |
9ae529ec | 5922 | has_features = B_TRUE; |
b5256303 TC |
5923 | |
5924 | feat_name = strchr(nvpair_name(elem), '@') + 1; | |
5925 | VERIFY0(zfeature_lookup_name(feat_name, &feat)); | |
5926 | if (feat == SPA_FEATURE_ENCRYPTION) | |
5927 | has_encryption = B_TRUE; | |
715c996d | 5928 | if (feat == SPA_FEATURE_ALLOCATION_CLASSES) |
5929 | has_allocclass = B_TRUE; | |
b5256303 TC |
5930 | } |
5931 | } | |
5932 | ||
5933 | /* verify encryption params, if they were provided */ | |
5934 | if (dcp != NULL) { | |
5935 | error = spa_create_check_encryption_params(dcp, has_encryption); | |
5936 | if (error != 0) { | |
5937 | spa_deactivate(spa); | |
5938 | spa_remove(spa); | |
5939 | mutex_exit(&spa_namespace_lock); | |
5940 | return (error); | |
5941 | } | |
9ae529ec | 5942 | } |
c24fa4b1 | 5943 | if (!has_allocclass && zfs_special_devs(nvroot, NULL)) { |
715c996d | 5944 | spa_deactivate(spa); |
5945 | spa_remove(spa); | |
5946 | mutex_exit(&spa_namespace_lock); | |
5947 | return (ENOTSUP); | |
5948 | } | |
9ae529ec CS |
5949 | |
5950 | if (has_features || nvlist_lookup_uint64(props, | |
5951 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 5952 | version = SPA_VERSION; |
9ae529ec CS |
5953 | } |
5954 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
5955 | |
5956 | spa->spa_first_txg = txg; | |
5957 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
5958 | spa->spa_uberblock.ub_version = version; |
5959 | spa->spa_ubsync = spa->spa_uberblock; | |
3dfb57a3 | 5960 | spa->spa_load_state = SPA_LOAD_CREATE; |
a1d477c2 MA |
5961 | spa->spa_removing_phys.sr_state = DSS_NONE; |
5962 | spa->spa_removing_phys.sr_removing_vdev = -1; | |
5963 | spa->spa_removing_phys.sr_prev_indirect_vdev = -1; | |
944a3724 | 5964 | spa->spa_indirect_vdevs_loaded = B_TRUE; |
34dc7c2f | 5965 | |
9babb374 BB |
5966 | /* |
5967 | * Create "The Godfather" zio to hold all async IOs | |
5968 | */ | |
e022864d MA |
5969 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
5970 | KM_SLEEP); | |
1c27024e | 5971 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
5972 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
5973 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
5974 | ZIO_FLAG_GODFATHER); | |
5975 | } | |
9babb374 | 5976 | |
34dc7c2f BB |
5977 | /* |
5978 | * Create the root vdev. | |
5979 | */ | |
b128c09f | 5980 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5981 | |
5982 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
5983 | ||
5984 | ASSERT(error != 0 || rvd != NULL); | |
5985 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
5986 | ||
5987 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
2e528b49 | 5988 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5989 | |
5990 | if (error == 0 && | |
5991 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
b2255edc BB |
5992 | (error = vdev_draid_spare_create(nvroot, rvd, &ndraid, 0)) == 0 && |
5993 | (error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) == 0) { | |
cc99f275 DB |
5994 | /* |
5995 | * instantiate the metaslab groups (this will dirty the vdevs) | |
5996 | * we can no longer error exit past this point | |
5997 | */ | |
5998 | for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { | |
5999 | vdev_t *vd = rvd->vdev_child[c]; | |
6000 | ||
6001 | vdev_metaslab_set_size(vd); | |
6002 | vdev_expand(vd, txg); | |
9babb374 | 6003 | } |
34dc7c2f BB |
6004 | } |
6005 | ||
b128c09f | 6006 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6007 | |
6008 | if (error != 0) { | |
6009 | spa_unload(spa); | |
6010 | spa_deactivate(spa); | |
6011 | spa_remove(spa); | |
6012 | mutex_exit(&spa_namespace_lock); | |
6013 | return (error); | |
6014 | } | |
6015 | ||
6016 | /* | |
6017 | * Get the list of spares, if specified. | |
6018 | */ | |
6019 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
6020 | &spares, &nspares) == 0) { | |
65ad5d11 AJ |
6021 | spa->spa_spares.sav_config = fnvlist_alloc(); |
6022 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
795075e6 PD |
6023 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
6024 | nspares); | |
b128c09f | 6025 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6026 | spa_load_spares(spa); |
b128c09f | 6027 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6028 | spa->spa_spares.sav_sync = B_TRUE; |
6029 | } | |
6030 | ||
6031 | /* | |
6032 | * Get the list of level 2 cache devices, if specified. | |
6033 | */ | |
6034 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
6035 | &l2cache, &nl2cache) == 0) { | |
795075e6 PD |
6036 | VERIFY0(nvlist_alloc(&spa->spa_l2cache.sav_config, |
6037 | NV_UNIQUE_NAME, KM_SLEEP)); | |
65ad5d11 | 6038 | fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
795075e6 PD |
6039 | ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache, |
6040 | nl2cache); | |
b128c09f | 6041 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6042 | spa_load_l2cache(spa); |
b128c09f | 6043 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6044 | spa->spa_l2cache.sav_sync = B_TRUE; |
6045 | } | |
6046 | ||
9ae529ec | 6047 | spa->spa_is_initializing = B_TRUE; |
b5256303 | 6048 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); |
9ae529ec | 6049 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 6050 | |
428870ff BB |
6051 | /* |
6052 | * Create DDTs (dedup tables). | |
6053 | */ | |
6054 | ddt_create(spa); | |
67a1b037 PJD |
6055 | /* |
6056 | * Create BRT table and BRT table object. | |
6057 | */ | |
6058 | brt_create(spa); | |
428870ff BB |
6059 | |
6060 | spa_update_dspace(spa); | |
6061 | ||
34dc7c2f BB |
6062 | tx = dmu_tx_create_assigned(dp, txg); |
6063 | ||
d5e024cb BB |
6064 | /* |
6065 | * Create the pool's history object. | |
6066 | */ | |
6067 | if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history) | |
6068 | spa_history_create_obj(spa, tx); | |
6069 | ||
6070 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); | |
6071 | spa_history_log_version(spa, "create", tx); | |
6072 | ||
34dc7c2f BB |
6073 | /* |
6074 | * Create the pool config object. | |
6075 | */ | |
6076 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 6077 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
6078 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
6079 | ||
6080 | if (zap_add(spa->spa_meta_objset, | |
6081 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
6082 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
6083 | cmn_err(CE_PANIC, "failed to add pool config"); | |
6084 | } | |
6085 | ||
428870ff BB |
6086 | if (zap_add(spa->spa_meta_objset, |
6087 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
6088 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
6089 | cmn_err(CE_PANIC, "failed to add pool version"); | |
6090 | } | |
6091 | ||
34dc7c2f BB |
6092 | /* Newly created pools with the right version are always deflated. */ |
6093 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
6094 | spa->spa_deflate = TRUE; | |
6095 | if (zap_add(spa->spa_meta_objset, | |
6096 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
6097 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
6098 | cmn_err(CE_PANIC, "failed to add deflate"); | |
6099 | } | |
6100 | } | |
6101 | ||
6102 | /* | |
428870ff | 6103 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
6104 | * because sync-to-convergence takes longer if the blocksize |
6105 | * keeps changing. | |
6106 | */ | |
428870ff BB |
6107 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
6108 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 6109 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 6110 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
6111 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
6112 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
6113 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 6114 | } |
428870ff BB |
6115 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
6116 | spa->spa_meta_objset, obj)); | |
34dc7c2f | 6117 | |
3c67d83a TH |
6118 | /* |
6119 | * Generate some random noise for salted checksums to operate on. | |
6120 | */ | |
6121 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
6122 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
6123 | ||
34dc7c2f BB |
6124 | /* |
6125 | * Set pool properties. | |
6126 | */ | |
6127 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
6128 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
6129 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 6130 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
379ca9cf | 6131 | spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); |
1b939560 | 6132 | spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM); |
428870ff | 6133 | |
d164b209 BB |
6134 | if (props != NULL) { |
6135 | spa_configfile_set(spa, props, B_FALSE); | |
13fe0198 | 6136 | spa_sync_props(props, tx); |
d164b209 | 6137 | } |
34dc7c2f | 6138 | |
b2255edc BB |
6139 | for (int i = 0; i < ndraid; i++) |
6140 | spa_feature_incr(spa, SPA_FEATURE_DRAID, tx); | |
6141 | ||
34dc7c2f BB |
6142 | dmu_tx_commit(tx); |
6143 | ||
6144 | spa->spa_sync_on = B_TRUE; | |
b5256303 | 6145 | txg_sync_start(dp); |
379ca9cf | 6146 | mmp_thread_start(spa); |
b5256303 | 6147 | txg_wait_synced(dp, txg); |
34dc7c2f | 6148 | |
9d5b5245 SD |
6149 | spa_spawn_aux_threads(spa); |
6150 | ||
55c12724 | 6151 | spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE); |
34dc7c2f | 6152 | |
0c66c32d JG |
6153 | /* |
6154 | * Don't count references from objsets that are already closed | |
6155 | * and are making their way through the eviction process. | |
6156 | */ | |
6157 | spa_evicting_os_wait(spa); | |
424fd7c3 | 6158 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
3dfb57a3 | 6159 | spa->spa_load_state = SPA_LOAD_NONE; |
b128c09f | 6160 | |
4759342a JL |
6161 | spa_import_os(spa); |
6162 | ||
d164b209 BB |
6163 | mutex_exit(&spa_namespace_lock); |
6164 | ||
34dc7c2f BB |
6165 | return (0); |
6166 | } | |
6167 | ||
9babb374 BB |
6168 | /* |
6169 | * Import a non-root pool into the system. | |
6170 | */ | |
6171 | int | |
13fe0198 | 6172 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
6173 | { |
6174 | spa_t *spa; | |
d1807f16 | 6175 | const char *altroot = NULL; |
428870ff | 6176 | spa_load_state_t state = SPA_LOAD_IMPORT; |
8a393be3 | 6177 | zpool_load_policy_t policy; |
da92d5cb | 6178 | spa_mode_t mode = spa_mode_global; |
572e2857 | 6179 | uint64_t readonly = B_FALSE; |
9babb374 | 6180 | int error; |
34dc7c2f BB |
6181 | nvlist_t *nvroot; |
6182 | nvlist_t **spares, **l2cache; | |
6183 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
6184 | |
6185 | /* | |
6186 | * If a pool with this name exists, return failure. | |
6187 | */ | |
6188 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6189 | if (spa_lookup(pool) != NULL) { |
9babb374 | 6190 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 6191 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
6192 | } |
6193 | ||
6194 | /* | |
6195 | * Create and initialize the spa structure. | |
6196 | */ | |
6197 | (void) nvlist_lookup_string(props, | |
6198 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
6199 | (void) nvlist_lookup_uint64(props, |
6200 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
6201 | if (readonly) | |
da92d5cb | 6202 | mode = SPA_MODE_READ; |
428870ff | 6203 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
6204 | spa->spa_import_flags = flags; |
6205 | ||
6206 | /* | |
6207 | * Verbatim import - Take a pool and insert it into the namespace | |
6208 | * as if it had been loaded at boot. | |
6209 | */ | |
6210 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
6211 | if (props != NULL) | |
6212 | spa_configfile_set(spa, props, B_FALSE); | |
6213 | ||
55c12724 | 6214 | spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE); |
12fa0466 | 6215 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
4a0ee12a | 6216 | zfs_dbgmsg("spa_import: verbatim import of %s", pool); |
572e2857 | 6217 | mutex_exit(&spa_namespace_lock); |
572e2857 BB |
6218 | return (0); |
6219 | } | |
6220 | ||
6221 | spa_activate(spa, mode); | |
34dc7c2f | 6222 | |
9babb374 BB |
6223 | /* |
6224 | * Don't start async tasks until we know everything is healthy. | |
6225 | */ | |
6226 | spa_async_suspend(spa); | |
b128c09f | 6227 | |
8a393be3 PZ |
6228 | zpool_get_load_policy(config, &policy); |
6229 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) | |
572e2857 BB |
6230 | state = SPA_LOAD_RECOVER; |
6231 | ||
6cb8e530 | 6232 | spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; |
572e2857 | 6233 | |
6cb8e530 PZ |
6234 | if (state != SPA_LOAD_RECOVER) { |
6235 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6236 | zfs_dbgmsg("spa_import: importing %s", pool); | |
6237 | } else { | |
6238 | zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " | |
8a393be3 | 6239 | "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); |
6cb8e530 | 6240 | } |
8a393be3 | 6241 | error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); |
428870ff BB |
6242 | |
6243 | /* | |
572e2857 BB |
6244 | * Propagate anything learned while loading the pool and pass it |
6245 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 6246 | */ |
65ad5d11 | 6247 | fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, spa->spa_load_info); |
34dc7c2f | 6248 | |
b128c09f | 6249 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6250 | /* |
9babb374 BB |
6251 | * Toss any existing sparelist, as it doesn't have any validity |
6252 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 6253 | */ |
9babb374 | 6254 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
6255 | nvlist_free(spa->spa_spares.sav_config); |
6256 | spa->spa_spares.sav_config = NULL; | |
6257 | spa_load_spares(spa); | |
6258 | } | |
9babb374 | 6259 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
6260 | nvlist_free(spa->spa_l2cache.sav_config); |
6261 | spa->spa_l2cache.sav_config = NULL; | |
6262 | spa_load_l2cache(spa); | |
6263 | } | |
6264 | ||
65ad5d11 | 6265 | nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); |
b128c09f | 6266 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 6267 | |
d164b209 BB |
6268 | if (props != NULL) |
6269 | spa_configfile_set(spa, props, B_FALSE); | |
6270 | ||
fb5f0bc8 BB |
6271 | if (error != 0 || (props && spa_writeable(spa) && |
6272 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
6273 | spa_unload(spa); |
6274 | spa_deactivate(spa); | |
6275 | spa_remove(spa); | |
34dc7c2f BB |
6276 | mutex_exit(&spa_namespace_lock); |
6277 | return (error); | |
6278 | } | |
6279 | ||
572e2857 BB |
6280 | spa_async_resume(spa); |
6281 | ||
34dc7c2f BB |
6282 | /* |
6283 | * Override any spares and level 2 cache devices as specified by | |
6284 | * the user, as these may have correct device names/devids, etc. | |
6285 | */ | |
6286 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
6287 | &spares, &nspares) == 0) { | |
6288 | if (spa->spa_spares.sav_config) | |
65ad5d11 AJ |
6289 | fnvlist_remove(spa->spa_spares.sav_config, |
6290 | ZPOOL_CONFIG_SPARES); | |
34dc7c2f | 6291 | else |
65ad5d11 AJ |
6292 | spa->spa_spares.sav_config = fnvlist_alloc(); |
6293 | fnvlist_add_nvlist_array(spa->spa_spares.sav_config, | |
795075e6 PD |
6294 | ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares, |
6295 | nspares); | |
b128c09f | 6296 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6297 | spa_load_spares(spa); |
b128c09f | 6298 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6299 | spa->spa_spares.sav_sync = B_TRUE; |
6300 | } | |
6301 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
6302 | &l2cache, &nl2cache) == 0) { | |
6303 | if (spa->spa_l2cache.sav_config) | |
65ad5d11 AJ |
6304 | fnvlist_remove(spa->spa_l2cache.sav_config, |
6305 | ZPOOL_CONFIG_L2CACHE); | |
34dc7c2f | 6306 | else |
65ad5d11 AJ |
6307 | spa->spa_l2cache.sav_config = fnvlist_alloc(); |
6308 | fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config, | |
795075e6 PD |
6309 | ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache, |
6310 | nl2cache); | |
b128c09f | 6311 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6312 | spa_load_l2cache(spa); |
b128c09f | 6313 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6314 | spa->spa_l2cache.sav_sync = B_TRUE; |
6315 | } | |
6316 | ||
428870ff BB |
6317 | /* |
6318 | * Check for any removed devices. | |
6319 | */ | |
6320 | if (spa->spa_autoreplace) { | |
6321 | spa_aux_check_removed(&spa->spa_spares); | |
6322 | spa_aux_check_removed(&spa->spa_l2cache); | |
6323 | } | |
6324 | ||
fb5f0bc8 | 6325 | if (spa_writeable(spa)) { |
b128c09f BB |
6326 | /* |
6327 | * Update the config cache to include the newly-imported pool. | |
6328 | */ | |
45d1cae3 | 6329 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 6330 | } |
34dc7c2f | 6331 | |
34dc7c2f | 6332 | /* |
9babb374 BB |
6333 | * It's possible that the pool was expanded while it was exported. |
6334 | * We kick off an async task to handle this for us. | |
34dc7c2f | 6335 | */ |
9babb374 | 6336 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 6337 | |
d5e024cb | 6338 | spa_history_log_version(spa, "import", NULL); |
fb390aaf | 6339 | |
12fa0466 | 6340 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
fb390aaf | 6341 | |
fb390aaf HR |
6342 | mutex_exit(&spa_namespace_lock); |
6343 | ||
ec213971 | 6344 | zvol_create_minors_recursive(pool); |
4a22ba5b | 6345 | |
4759342a JL |
6346 | spa_import_os(spa); |
6347 | ||
b128c09f BB |
6348 | return (0); |
6349 | } | |
6350 | ||
34dc7c2f BB |
6351 | nvlist_t * |
6352 | spa_tryimport(nvlist_t *tryconfig) | |
6353 | { | |
6354 | nvlist_t *config = NULL; | |
d1807f16 | 6355 | const char *poolname, *cachefile; |
34dc7c2f BB |
6356 | spa_t *spa; |
6357 | uint64_t state; | |
d164b209 | 6358 | int error; |
8a393be3 | 6359 | zpool_load_policy_t policy; |
34dc7c2f BB |
6360 | |
6361 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
6362 | return (NULL); | |
6363 | ||
6364 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
6365 | return (NULL); | |
6366 | ||
6367 | /* | |
6368 | * Create and initialize the spa structure. | |
6369 | */ | |
6370 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6371 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
da92d5cb | 6372 | spa_activate(spa, SPA_MODE_READ); |
34dc7c2f BB |
6373 | |
6374 | /* | |
8a393be3 | 6375 | * Rewind pool if a max txg was provided. |
34dc7c2f | 6376 | */ |
8a393be3 PZ |
6377 | zpool_get_load_policy(spa->spa_config, &policy); |
6378 | if (policy.zlp_txg != UINT64_MAX) { | |
6379 | spa->spa_load_max_txg = policy.zlp_txg; | |
6cb8e530 PZ |
6380 | spa->spa_extreme_rewind = B_TRUE; |
6381 | zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", | |
8a393be3 | 6382 | poolname, (longlong_t)policy.zlp_txg); |
6cb8e530 PZ |
6383 | } else { |
6384 | zfs_dbgmsg("spa_tryimport: importing %s", poolname); | |
6385 | } | |
6386 | ||
6387 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) | |
6388 | == 0) { | |
6389 | zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); | |
6390 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; | |
6391 | } else { | |
6392 | spa->spa_config_source = SPA_CONFIG_SRC_SCAN; | |
6393 | } | |
6394 | ||
82ac409a AH |
6395 | /* |
6396 | * spa_import() relies on a pool config fetched by spa_try_import() | |
6397 | * for spare/cache devices. Import flags are not passed to | |
6398 | * spa_tryimport(), which makes it return early due to a missing log | |
6399 | * device and missing retrieving the cache device and spare eventually. | |
6400 | * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch | |
6401 | * the correct configuration regardless of the missing log device. | |
6402 | */ | |
6403 | spa->spa_import_flags |= ZFS_IMPORT_MISSING_LOG; | |
6404 | ||
6cb8e530 | 6405 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); |
34dc7c2f BB |
6406 | |
6407 | /* | |
6408 | * If 'tryconfig' was at least parsable, return the current config. | |
6409 | */ | |
6410 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 6411 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
65ad5d11 AJ |
6412 | fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, poolname); |
6413 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, state); | |
6414 | fnvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
6415 | spa->spa_uberblock.ub_timestamp); | |
6416 | fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, | |
6417 | spa->spa_load_info); | |
6418 | fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, | |
6419 | spa->spa_errata); | |
34dc7c2f BB |
6420 | |
6421 | /* | |
6422 | * If the bootfs property exists on this pool then we | |
6423 | * copy it out so that external consumers can tell which | |
6424 | * pools are bootable. | |
6425 | */ | |
d164b209 | 6426 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
79c76d5b | 6427 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6428 | |
6429 | /* | |
6430 | * We have to play games with the name since the | |
6431 | * pool was opened as TRYIMPORT_NAME. | |
6432 | */ | |
b128c09f | 6433 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
6434 | spa->spa_bootfs, tmpname) == 0) { |
6435 | char *cp; | |
d1d7e268 MK |
6436 | char *dsname; |
6437 | ||
79c76d5b | 6438 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6439 | |
6440 | cp = strchr(tmpname, '/'); | |
6441 | if (cp == NULL) { | |
6442 | (void) strlcpy(dsname, tmpname, | |
6443 | MAXPATHLEN); | |
6444 | } else { | |
6445 | (void) snprintf(dsname, MAXPATHLEN, | |
6446 | "%s/%s", poolname, ++cp); | |
6447 | } | |
65ad5d11 AJ |
6448 | fnvlist_add_string(config, ZPOOL_CONFIG_BOOTFS, |
6449 | dsname); | |
34dc7c2f BB |
6450 | kmem_free(dsname, MAXPATHLEN); |
6451 | } | |
6452 | kmem_free(tmpname, MAXPATHLEN); | |
6453 | } | |
6454 | ||
6455 | /* | |
6456 | * Add the list of hot spares and level 2 cache devices. | |
6457 | */ | |
9babb374 | 6458 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
6459 | spa_add_spares(spa, config); |
6460 | spa_add_l2cache(spa, config); | |
9babb374 | 6461 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
6462 | } |
6463 | ||
6464 | spa_unload(spa); | |
6465 | spa_deactivate(spa); | |
6466 | spa_remove(spa); | |
6467 | mutex_exit(&spa_namespace_lock); | |
6468 | ||
6469 | return (config); | |
6470 | } | |
6471 | ||
6472 | /* | |
6473 | * Pool export/destroy | |
6474 | * | |
6475 | * The act of destroying or exporting a pool is very simple. We make sure there | |
6476 | * is no more pending I/O and any references to the pool are gone. Then, we | |
6477 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
6478 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
6479 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
6480 | */ |
6481 | static int | |
4d55ea81 | 6482 | spa_export_common(const char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 6483 | boolean_t force, boolean_t hardforce) |
34dc7c2f | 6484 | { |
f4f50a70 | 6485 | int error; |
34dc7c2f BB |
6486 | spa_t *spa; |
6487 | ||
6488 | if (oldconfig) | |
6489 | *oldconfig = NULL; | |
6490 | ||
da92d5cb | 6491 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
2e528b49 | 6492 | return (SET_ERROR(EROFS)); |
34dc7c2f BB |
6493 | |
6494 | mutex_enter(&spa_namespace_lock); | |
6495 | if ((spa = spa_lookup(pool)) == NULL) { | |
6496 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 6497 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
6498 | } |
6499 | ||
43a85362 SD |
6500 | if (spa->spa_is_exporting) { |
6501 | /* the pool is being exported by another thread */ | |
6502 | mutex_exit(&spa_namespace_lock); | |
6503 | return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS)); | |
6504 | } | |
6505 | spa->spa_is_exporting = B_TRUE; | |
6506 | ||
34dc7c2f BB |
6507 | /* |
6508 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
6509 | * reacquire the namespace lock, and see if we can export. | |
6510 | */ | |
6511 | spa_open_ref(spa, FTAG); | |
6512 | mutex_exit(&spa_namespace_lock); | |
6513 | spa_async_suspend(spa); | |
a0bd735a BP |
6514 | if (spa->spa_zvol_taskq) { |
6515 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
6516 | taskq_wait(spa->spa_zvol_taskq); | |
6517 | } | |
34dc7c2f BB |
6518 | mutex_enter(&spa_namespace_lock); |
6519 | spa_close(spa, FTAG); | |
6520 | ||
d14cfd83 IH |
6521 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) |
6522 | goto export_spa; | |
34dc7c2f | 6523 | /* |
d14cfd83 IH |
6524 | * The pool will be in core if it's openable, in which case we can |
6525 | * modify its state. Objsets may be open only because they're dirty, | |
6526 | * so we have to force it to sync before checking spa_refcnt. | |
34dc7c2f | 6527 | */ |
0c66c32d | 6528 | if (spa->spa_sync_on) { |
34dc7c2f | 6529 | txg_wait_synced(spa->spa_dsl_pool, 0); |
0c66c32d JG |
6530 | spa_evicting_os_wait(spa); |
6531 | } | |
34dc7c2f | 6532 | |
d14cfd83 IH |
6533 | /* |
6534 | * A pool cannot be exported or destroyed if there are active | |
6535 | * references. If we are resetting a pool, allow references by | |
6536 | * fault injection handlers. | |
6537 | */ | |
f4f50a70 WA |
6538 | if (!spa_refcount_zero(spa) || (spa->spa_inject_ref != 0)) { |
6539 | error = SET_ERROR(EBUSY); | |
6540 | goto fail; | |
d14cfd83 | 6541 | } |
34dc7c2f | 6542 | |
d14cfd83 | 6543 | if (spa->spa_sync_on) { |
88b199c2 | 6544 | vdev_t *rvd = spa->spa_root_vdev; |
b128c09f BB |
6545 | /* |
6546 | * A pool cannot be exported if it has an active shared spare. | |
6547 | * This is to prevent other pools stealing the active spare | |
6548 | * from an exported pool. At user's own will, such pool can | |
6549 | * be forcedly exported. | |
6550 | */ | |
6551 | if (!force && new_state == POOL_STATE_EXPORTED && | |
6552 | spa_has_active_shared_spare(spa)) { | |
f4f50a70 WA |
6553 | error = SET_ERROR(EXDEV); |
6554 | goto fail; | |
b128c09f | 6555 | } |
34dc7c2f | 6556 | |
619f0976 GW |
6557 | /* |
6558 | * We're about to export or destroy this pool. Make sure | |
1b939560 BB |
6559 | * we stop all initialization and trim activity here before |
6560 | * we set the spa_final_txg. This will ensure that all | |
619f0976 GW |
6561 | * dirty data resulting from the initialization is |
6562 | * committed to disk before we unload the pool. | |
6563 | */ | |
88b199c2 RY |
6564 | vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE); |
6565 | vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE); | |
6566 | vdev_autotrim_stop_all(spa); | |
6567 | vdev_rebuild_stop_all(spa); | |
619f0976 | 6568 | |
34dc7c2f BB |
6569 | /* |
6570 | * We want this to be reflected on every label, | |
6571 | * so mark them all dirty. spa_unload() will do the | |
6572 | * final sync that pushes these changes out. | |
6573 | */ | |
fb5f0bc8 | 6574 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 6575 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6576 | spa->spa_state = new_state; |
88b199c2 | 6577 | vdev_config_dirty(rvd); |
2fb52853 GA |
6578 | spa_config_exit(spa, SCL_ALL, FTAG); |
6579 | } | |
6580 | ||
6581 | /* | |
6582 | * If the log space map feature is enabled and the pool is | |
6583 | * getting exported (but not destroyed), we want to spend some | |
6584 | * time flushing as many metaslabs as we can in an attempt to | |
6585 | * destroy log space maps and save import time. This has to be | |
6586 | * done before we set the spa_final_txg, otherwise | |
6587 | * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs. | |
6588 | * spa_should_flush_logs_on_unload() should be called after | |
6589 | * spa_state has been set to the new_state. | |
6590 | */ | |
6591 | if (spa_should_flush_logs_on_unload(spa)) | |
6592 | spa_unload_log_sm_flush_all(spa); | |
6593 | ||
6594 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { | |
6595 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
428870ff BB |
6596 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
6597 | TXG_DEFER_SIZE + 1; | |
b128c09f | 6598 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6599 | } |
6600 | } | |
6601 | ||
d14cfd83 | 6602 | export_spa: |
4759342a JL |
6603 | spa_export_os(spa); |
6604 | ||
d5e024cb BB |
6605 | if (new_state == POOL_STATE_DESTROYED) |
6606 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); | |
6607 | else if (new_state == POOL_STATE_EXPORTED) | |
6608 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT); | |
34dc7c2f BB |
6609 | |
6610 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
6611 | spa_unload(spa); | |
6612 | spa_deactivate(spa); | |
6613 | } | |
6614 | ||
6615 | if (oldconfig && spa->spa_config) | |
65ad5d11 | 6616 | *oldconfig = fnvlist_dup(spa->spa_config); |
34dc7c2f BB |
6617 | |
6618 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 | 6619 | if (!hardforce) |
55c12724 | 6620 | spa_write_cachefile(spa, B_TRUE, B_TRUE, B_FALSE); |
34dc7c2f | 6621 | spa_remove(spa); |
43a85362 SD |
6622 | } else { |
6623 | /* | |
6624 | * If spa_remove() is not called for this spa_t and | |
6625 | * there is any possibility that it can be reused, | |
6626 | * we make sure to reset the exporting flag. | |
6627 | */ | |
6628 | spa->spa_is_exporting = B_FALSE; | |
34dc7c2f | 6629 | } |
34dc7c2f | 6630 | |
43a85362 | 6631 | mutex_exit(&spa_namespace_lock); |
34dc7c2f | 6632 | return (0); |
f4f50a70 WA |
6633 | |
6634 | fail: | |
6635 | spa->spa_is_exporting = B_FALSE; | |
6636 | spa_async_resume(spa); | |
6637 | mutex_exit(&spa_namespace_lock); | |
6638 | return (error); | |
34dc7c2f BB |
6639 | } |
6640 | ||
6641 | /* | |
6642 | * Destroy a storage pool. | |
6643 | */ | |
6644 | int | |
4d55ea81 | 6645 | spa_destroy(const char *pool) |
34dc7c2f | 6646 | { |
fb5f0bc8 BB |
6647 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
6648 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
6649 | } |
6650 | ||
6651 | /* | |
6652 | * Export a storage pool. | |
6653 | */ | |
6654 | int | |
4d55ea81 | 6655 | spa_export(const char *pool, nvlist_t **oldconfig, boolean_t force, |
fb5f0bc8 | 6656 | boolean_t hardforce) |
34dc7c2f | 6657 | { |
fb5f0bc8 BB |
6658 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
6659 | force, hardforce)); | |
34dc7c2f BB |
6660 | } |
6661 | ||
6662 | /* | |
6663 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
6664 | * from the namespace in any way. | |
6665 | */ | |
6666 | int | |
4d55ea81 | 6667 | spa_reset(const char *pool) |
34dc7c2f | 6668 | { |
b128c09f | 6669 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 6670 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
6671 | } |
6672 | ||
34dc7c2f BB |
6673 | /* |
6674 | * ========================================================================== | |
6675 | * Device manipulation | |
6676 | * ========================================================================== | |
6677 | */ | |
6678 | ||
b2255edc BB |
6679 | /* |
6680 | * This is called as a synctask to increment the draid feature flag | |
6681 | */ | |
6682 | static void | |
6683 | spa_draid_feature_incr(void *arg, dmu_tx_t *tx) | |
6684 | { | |
6685 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
6686 | int draid = (int)(uintptr_t)arg; | |
6687 | ||
6688 | for (int c = 0; c < draid; c++) | |
6689 | spa_feature_incr(spa, SPA_FEATURE_DRAID, tx); | |
6690 | } | |
6691 | ||
34dc7c2f BB |
6692 | /* |
6693 | * Add a device to a storage pool. | |
6694 | */ | |
6695 | int | |
6696 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
6697 | { | |
b2255edc | 6698 | uint64_t txg, ndraid = 0; |
fb5f0bc8 | 6699 | int error; |
34dc7c2f BB |
6700 | vdev_t *rvd = spa->spa_root_vdev; |
6701 | vdev_t *vd, *tvd; | |
6702 | nvlist_t **spares, **l2cache; | |
6703 | uint_t nspares, nl2cache; | |
6704 | ||
572e2857 BB |
6705 | ASSERT(spa_writeable(spa)); |
6706 | ||
34dc7c2f BB |
6707 | txg = spa_vdev_enter(spa); |
6708 | ||
6709 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
6710 | VDEV_ALLOC_ADD)) != 0) | |
6711 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6712 | ||
b128c09f | 6713 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
6714 | |
6715 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
6716 | &nspares) != 0) | |
6717 | nspares = 0; | |
6718 | ||
6719 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
6720 | &nl2cache) != 0) | |
6721 | nl2cache = 0; | |
6722 | ||
b128c09f | 6723 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 6724 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 6725 | |
b128c09f | 6726 | if (vd->vdev_children != 0 && |
b2255edc | 6727 | (error = vdev_create(vd, txg, B_FALSE)) != 0) { |
b128c09f | 6728 | return (spa_vdev_exit(spa, vd, txg, error)); |
b2255edc BB |
6729 | } |
6730 | ||
6731 | /* | |
6732 | * The virtual dRAID spares must be added after vdev tree is created | |
bf169e9f | 6733 | * and the vdev guids are generated. The guid of their associated |
b2255edc BB |
6734 | * dRAID is stored in the config and used when opening the spare. |
6735 | */ | |
6736 | if ((error = vdev_draid_spare_create(nvroot, vd, &ndraid, | |
6737 | rvd->vdev_children)) == 0) { | |
6738 | if (ndraid > 0 && nvlist_lookup_nvlist_array(nvroot, | |
6739 | ZPOOL_CONFIG_SPARES, &spares, &nspares) != 0) | |
6740 | nspares = 0; | |
6741 | } else { | |
6742 | return (spa_vdev_exit(spa, vd, txg, error)); | |
6743 | } | |
34dc7c2f BB |
6744 | |
6745 | /* | |
6746 | * We must validate the spares and l2cache devices after checking the | |
6747 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
6748 | */ | |
b128c09f | 6749 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 6750 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
6751 | |
6752 | /* | |
a1d477c2 MA |
6753 | * If we are in the middle of a device removal, we can only add |
6754 | * devices which match the existing devices in the pool. | |
6755 | * If we are in the middle of a removal, or have some indirect | |
b2255edc | 6756 | * vdevs, we can not add raidz or dRAID top levels. |
34dc7c2f | 6757 | */ |
a1d477c2 MA |
6758 | if (spa->spa_vdev_removal != NULL || |
6759 | spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { | |
6760 | for (int c = 0; c < vd->vdev_children; c++) { | |
6761 | tvd = vd->vdev_child[c]; | |
6762 | if (spa->spa_vdev_removal != NULL && | |
9e052db4 | 6763 | tvd->vdev_ashift != spa->spa_max_ashift) { |
a1d477c2 MA |
6764 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
6765 | } | |
b2255edc BB |
6766 | /* Fail if top level vdev is raidz or a dRAID */ |
6767 | if (vdev_get_nparity(tvd) != 0) | |
a1d477c2 | 6768 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
b2255edc | 6769 | |
a1d477c2 MA |
6770 | /* |
6771 | * Need the top level mirror to be | |
6772 | * a mirror of leaf vdevs only | |
6773 | */ | |
6774 | if (tvd->vdev_ops == &vdev_mirror_ops) { | |
6775 | for (uint64_t cid = 0; | |
6776 | cid < tvd->vdev_children; cid++) { | |
6777 | vdev_t *cvd = tvd->vdev_child[cid]; | |
6778 | if (!cvd->vdev_ops->vdev_op_leaf) { | |
6779 | return (spa_vdev_exit(spa, vd, | |
6780 | txg, EINVAL)); | |
6781 | } | |
6782 | } | |
6783 | } | |
6784 | } | |
6785 | } | |
6786 | ||
1c27024e | 6787 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
6788 | tvd = vd->vdev_child[c]; |
6789 | vdev_remove_child(vd, tvd); | |
93e28d66 | 6790 | tvd->vdev_id = rvd->vdev_children; |
34dc7c2f BB |
6791 | vdev_add_child(rvd, tvd); |
6792 | vdev_config_dirty(tvd); | |
6793 | } | |
6794 | ||
6795 | if (nspares != 0) { | |
6796 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
6797 | ZPOOL_CONFIG_SPARES); | |
6798 | spa_load_spares(spa); | |
6799 | spa->spa_spares.sav_sync = B_TRUE; | |
6800 | } | |
6801 | ||
6802 | if (nl2cache != 0) { | |
6803 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
6804 | ZPOOL_CONFIG_L2CACHE); | |
6805 | spa_load_l2cache(spa); | |
6806 | spa->spa_l2cache.sav_sync = B_TRUE; | |
6807 | } | |
6808 | ||
b2255edc BB |
6809 | /* |
6810 | * We can't increment a feature while holding spa_vdev so we | |
6811 | * have to do it in a synctask. | |
6812 | */ | |
6813 | if (ndraid != 0) { | |
6814 | dmu_tx_t *tx; | |
6815 | ||
6816 | tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); | |
6817 | dsl_sync_task_nowait(spa->spa_dsl_pool, spa_draid_feature_incr, | |
6818 | (void *)(uintptr_t)ndraid, tx); | |
6819 | dmu_tx_commit(tx); | |
6820 | } | |
6821 | ||
34dc7c2f BB |
6822 | /* |
6823 | * We have to be careful when adding new vdevs to an existing pool. | |
6824 | * If other threads start allocating from these vdevs before we | |
6825 | * sync the config cache, and we lose power, then upon reboot we may | |
6826 | * fail to open the pool because there are DVAs that the config cache | |
6827 | * can't translate. Therefore, we first add the vdevs without | |
6828 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
6829 | * and then let spa_config_update() initialize the new metaslabs. | |
6830 | * | |
6831 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
6832 | * if we lose power at any point in this sequence, the remaining | |
6833 | * steps will be completed the next time we load the pool. | |
6834 | */ | |
6835 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
6836 | ||
6837 | mutex_enter(&spa_namespace_lock); | |
6838 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
12fa0466 | 6839 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); |
34dc7c2f BB |
6840 | mutex_exit(&spa_namespace_lock); |
6841 | ||
6842 | return (0); | |
6843 | } | |
6844 | ||
6845 | /* | |
6846 | * Attach a device to a mirror. The arguments are the path to any device | |
6847 | * in the mirror, and the nvroot for the new device. If the path specifies | |
6848 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
6849 | * | |
6850 | * If 'replacing' is specified, the new device is intended to replace the | |
6851 | * existing device; in this case the two devices are made into their own | |
6852 | * mirror using the 'replacing' vdev, which is functionally identical to | |
6853 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
6854 | * extra rules: you can't attach to it after it's been created, and upon | |
6855 | * completion of resilvering, the first disk (the one being replaced) | |
6856 | * is automatically detached. | |
9a49d3f3 BB |
6857 | * |
6858 | * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild) | |
6859 | * should be performed instead of traditional healing reconstruction. From | |
6860 | * an administrators perspective these are both resilver operations. | |
34dc7c2f BB |
6861 | */ |
6862 | int | |
9a49d3f3 BB |
6863 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing, |
6864 | int rebuild) | |
34dc7c2f | 6865 | { |
428870ff | 6866 | uint64_t txg, dtl_max_txg; |
9a49d3f3 | 6867 | vdev_t *rvd = spa->spa_root_vdev; |
34dc7c2f BB |
6868 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
6869 | vdev_ops_t *pvops; | |
b128c09f BB |
6870 | char *oldvdpath, *newvdpath; |
6871 | int newvd_isspare; | |
6872 | int error; | |
34dc7c2f | 6873 | |
572e2857 BB |
6874 | ASSERT(spa_writeable(spa)); |
6875 | ||
34dc7c2f BB |
6876 | txg = spa_vdev_enter(spa); |
6877 | ||
b128c09f | 6878 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6879 | |
d2734cce SD |
6880 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6881 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6882 | error = (spa_has_checkpoint(spa)) ? | |
6883 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6884 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6885 | } | |
6886 | ||
9a49d3f3 BB |
6887 | if (rebuild) { |
6888 | if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD)) | |
6889 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6890 | ||
9d618615 A |
6891 | if (dsl_scan_resilvering(spa_get_dsl(spa)) || |
6892 | dsl_scan_resilver_scheduled(spa_get_dsl(spa))) { | |
9a49d3f3 BB |
6893 | return (spa_vdev_exit(spa, NULL, txg, |
6894 | ZFS_ERR_RESILVER_IN_PROGRESS)); | |
9d618615 | 6895 | } |
9a49d3f3 BB |
6896 | } else { |
6897 | if (vdev_rebuild_active(rvd)) | |
6898 | return (spa_vdev_exit(spa, NULL, txg, | |
6899 | ZFS_ERR_REBUILD_IN_PROGRESS)); | |
6900 | } | |
6901 | ||
9e052db4 | 6902 | if (spa->spa_vdev_removal != NULL) |
a1d477c2 | 6903 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
a1d477c2 | 6904 | |
34dc7c2f BB |
6905 | if (oldvd == NULL) |
6906 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6907 | ||
6908 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
6909 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6910 | ||
6911 | pvd = oldvd->vdev_parent; | |
6912 | ||
6a42939f RY |
6913 | if (spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, |
6914 | VDEV_ALLOC_ATTACH) != 0) | |
34dc7c2f BB |
6915 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
6916 | ||
6917 | if (newrootvd->vdev_children != 1) | |
6918 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6919 | ||
6920 | newvd = newrootvd->vdev_child[0]; | |
6921 | ||
6922 | if (!newvd->vdev_ops->vdev_op_leaf) | |
6923 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6924 | ||
6925 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
6926 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
6927 | ||
6928 | /* | |
c23738c7 | 6929 | * log, dedup and special vdevs should not be replaced by spares. |
34dc7c2f | 6930 | */ |
c23738c7 AH |
6931 | if ((oldvd->vdev_top->vdev_alloc_bias != VDEV_BIAS_NONE || |
6932 | oldvd->vdev_top->vdev_islog) && newvd->vdev_isspare) { | |
34dc7c2f | 6933 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
c23738c7 | 6934 | } |
34dc7c2f | 6935 | |
b2255edc BB |
6936 | /* |
6937 | * A dRAID spare can only replace a child of its parent dRAID vdev. | |
6938 | */ | |
6939 | if (newvd->vdev_ops == &vdev_draid_spare_ops && | |
6940 | oldvd->vdev_top != vdev_draid_spare_get_parent(newvd)) { | |
6941 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6942 | } | |
6943 | ||
9a49d3f3 BB |
6944 | if (rebuild) { |
6945 | /* | |
b2255edc | 6946 | * For rebuilds, the top vdev must support reconstruction |
9a49d3f3 | 6947 | * using only space maps. This means the only allowable |
b2255edc | 6948 | * vdevs types are the root vdev, a mirror, or dRAID. |
9a49d3f3 | 6949 | */ |
b2255edc BB |
6950 | tvd = pvd; |
6951 | if (pvd->vdev_top != NULL) | |
6952 | tvd = pvd->vdev_top; | |
6953 | ||
6954 | if (tvd->vdev_ops != &vdev_mirror_ops && | |
6955 | tvd->vdev_ops != &vdev_root_ops && | |
6956 | tvd->vdev_ops != &vdev_draid_ops) { | |
9a49d3f3 BB |
6957 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
6958 | } | |
6959 | } | |
6960 | ||
34dc7c2f BB |
6961 | if (!replacing) { |
6962 | /* | |
6963 | * For attach, the only allowable parent is a mirror or the root | |
6964 | * vdev. | |
6965 | */ | |
6966 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6967 | pvd->vdev_ops != &vdev_root_ops) | |
6968 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6969 | ||
6970 | pvops = &vdev_mirror_ops; | |
6971 | } else { | |
6972 | /* | |
6973 | * Active hot spares can only be replaced by inactive hot | |
6974 | * spares. | |
6975 | */ | |
6976 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 6977 | oldvd->vdev_isspare && |
34dc7c2f BB |
6978 | !spa_has_spare(spa, newvd->vdev_guid)) |
6979 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6980 | ||
6981 | /* | |
6982 | * If the source is a hot spare, and the parent isn't already a | |
6983 | * spare, then we want to create a new hot spare. Otherwise, we | |
6984 | * want to create a replacing vdev. The user is not allowed to | |
6985 | * attach to a spared vdev child unless the 'isspare' state is | |
6986 | * the same (spare replaces spare, non-spare replaces | |
6987 | * non-spare). | |
6988 | */ | |
572e2857 BB |
6989 | if (pvd->vdev_ops == &vdev_replacing_ops && |
6990 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 6991 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6992 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
6993 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 6994 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6995 | } |
6996 | ||
6997 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
6998 | pvops = &vdev_spare_ops; |
6999 | else | |
7000 | pvops = &vdev_replacing_ops; | |
7001 | } | |
7002 | ||
7003 | /* | |
9babb374 | 7004 | * Make sure the new device is big enough. |
34dc7c2f | 7005 | */ |
9babb374 | 7006 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
7007 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
7008 | ||
7009 | /* | |
7010 | * The new device cannot have a higher alignment requirement | |
7011 | * than the top-level vdev. | |
7012 | */ | |
7013 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
9a49d3f3 | 7014 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
34dc7c2f BB |
7015 | |
7016 | /* | |
7017 | * If this is an in-place replacement, update oldvd's path and devid | |
7018 | * to make it distinguishable from newvd, and unopenable from now on. | |
7019 | */ | |
7020 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
7021 | spa_strfree(oldvd->vdev_path); | |
7022 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
79c76d5b | 7023 | KM_SLEEP); |
c9e319fa JL |
7024 | (void) snprintf(oldvd->vdev_path, strlen(newvd->vdev_path) + 5, |
7025 | "%s/%s", newvd->vdev_path, "old"); | |
34dc7c2f BB |
7026 | if (oldvd->vdev_devid != NULL) { |
7027 | spa_strfree(oldvd->vdev_devid); | |
7028 | oldvd->vdev_devid = NULL; | |
7029 | } | |
7030 | } | |
7031 | ||
7032 | /* | |
7033 | * If the parent is not a mirror, or if we're replacing, insert the new | |
7034 | * mirror/replacing/spare vdev above oldvd. | |
7035 | */ | |
7036 | if (pvd->vdev_ops != pvops) | |
7037 | pvd = vdev_add_parent(oldvd, pvops); | |
7038 | ||
7039 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
7040 | ASSERT(pvd->vdev_ops == pvops); | |
7041 | ASSERT(oldvd->vdev_parent == pvd); | |
7042 | ||
7043 | /* | |
7044 | * Extract the new device from its root and add it to pvd. | |
7045 | */ | |
7046 | vdev_remove_child(newrootvd, newvd); | |
7047 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 7048 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
7049 | vdev_add_child(pvd, newvd); |
7050 | ||
6d82f98c IH |
7051 | /* |
7052 | * Reevaluate the parent vdev state. | |
7053 | */ | |
7054 | vdev_propagate_state(pvd); | |
7055 | ||
34dc7c2f BB |
7056 | tvd = newvd->vdev_top; |
7057 | ASSERT(pvd->vdev_top == tvd); | |
7058 | ASSERT(tvd->vdev_parent == rvd); | |
7059 | ||
7060 | vdev_config_dirty(tvd); | |
7061 | ||
7062 | /* | |
428870ff BB |
7063 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
7064 | * for any dmu_sync-ed blocks. It will propagate upward when | |
7065 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 7066 | */ |
428870ff | 7067 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 7068 | |
9a49d3f3 BB |
7069 | vdev_dtl_dirty(newvd, DTL_MISSING, |
7070 | TXG_INITIAL, dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 7071 | |
9babb374 | 7072 | if (newvd->vdev_isspare) { |
34dc7c2f | 7073 | spa_spare_activate(newvd); |
12fa0466 | 7074 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); |
9babb374 BB |
7075 | } |
7076 | ||
b128c09f BB |
7077 | oldvdpath = spa_strdup(oldvd->vdev_path); |
7078 | newvdpath = spa_strdup(newvd->vdev_path); | |
7079 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
7080 | |
7081 | /* | |
7082 | * Mark newvd's DTL dirty in this txg. | |
7083 | */ | |
7084 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
7085 | ||
428870ff | 7086 | /* |
9a49d3f3 BB |
7087 | * Schedule the resilver or rebuild to restart in the future. We do |
7088 | * this to ensure that dmu_sync-ed blocks have been stitched into the | |
7089 | * respective datasets. | |
428870ff | 7090 | */ |
9a49d3f3 BB |
7091 | if (rebuild) { |
7092 | newvd->vdev_rebuild_txg = txg; | |
7093 | ||
7094 | vdev_rebuild(tvd); | |
7095 | } else { | |
7096 | newvd->vdev_resilver_txg = txg; | |
7097 | ||
7098 | if (dsl_scan_resilvering(spa_get_dsl(spa)) && | |
7099 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) { | |
7100 | vdev_defer_resilver(newvd); | |
7101 | } else { | |
7102 | dsl_scan_restart_resilver(spa->spa_dsl_pool, | |
7103 | dtl_max_txg); | |
7104 | } | |
7105 | } | |
428870ff | 7106 | |
fb390aaf | 7107 | if (spa->spa_bootfs) |
12fa0466 | 7108 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); |
fb390aaf | 7109 | |
12fa0466 | 7110 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); |
fb390aaf | 7111 | |
428870ff BB |
7112 | /* |
7113 | * Commit the config | |
7114 | */ | |
7115 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 7116 | |
6f1ffb06 | 7117 | spa_history_log_internal(spa, "vdev attach", NULL, |
428870ff | 7118 | "%s vdev=%s %s vdev=%s", |
45d1cae3 BB |
7119 | replacing && newvd_isspare ? "spare in" : |
7120 | replacing ? "replace" : "attach", newvdpath, | |
7121 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
7122 | |
7123 | spa_strfree(oldvdpath); | |
7124 | spa_strfree(newvdpath); | |
7125 | ||
34dc7c2f BB |
7126 | return (0); |
7127 | } | |
7128 | ||
7129 | /* | |
7130 | * Detach a device from a mirror or replacing vdev. | |
d3cc8b15 | 7131 | * |
34dc7c2f | 7132 | * If 'replace_done' is specified, only detach if the parent |
719534ca | 7133 | * is a replacing or a spare vdev. |
34dc7c2f BB |
7134 | */ |
7135 | int | |
fb5f0bc8 | 7136 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
7137 | { |
7138 | uint64_t txg; | |
fb5f0bc8 | 7139 | int error; |
2a8ba608 | 7140 | vdev_t *rvd __maybe_unused = spa->spa_root_vdev; |
34dc7c2f BB |
7141 | vdev_t *vd, *pvd, *cvd, *tvd; |
7142 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 7143 | uint64_t unspare_guid = 0; |
428870ff | 7144 | char *vdpath; |
1c27024e | 7145 | |
572e2857 BB |
7146 | ASSERT(spa_writeable(spa)); |
7147 | ||
9a49d3f3 | 7148 | txg = spa_vdev_detach_enter(spa, guid); |
34dc7c2f | 7149 | |
b128c09f | 7150 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 7151 | |
d2734cce SD |
7152 | /* |
7153 | * Besides being called directly from the userland through the | |
7154 | * ioctl interface, spa_vdev_detach() can be potentially called | |
7155 | * at the end of spa_vdev_resilver_done(). | |
7156 | * | |
7157 | * In the regular case, when we have a checkpoint this shouldn't | |
7158 | * happen as we never empty the DTLs of a vdev during the scrub | |
7159 | * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() | |
7160 | * should never get here when we have a checkpoint. | |
7161 | * | |
7162 | * That said, even in a case when we checkpoint the pool exactly | |
7163 | * as spa_vdev_resilver_done() calls this function everything | |
7164 | * should be fine as the resilver will return right away. | |
7165 | */ | |
7166 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7167 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7168 | error = (spa_has_checkpoint(spa)) ? | |
7169 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7170 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7171 | } | |
7172 | ||
34dc7c2f BB |
7173 | if (vd == NULL) |
7174 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
7175 | ||
7176 | if (!vd->vdev_ops->vdev_op_leaf) | |
7177 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
7178 | ||
7179 | pvd = vd->vdev_parent; | |
7180 | ||
fb5f0bc8 BB |
7181 | /* |
7182 | * If the parent/child relationship is not as expected, don't do it. | |
7183 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
7184 | * vdev that's replacing B with C. The user's intent in replacing | |
7185 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
7186 | * the replace by detaching C, the expected behavior is to end up | |
7187 | * M(A,B). But suppose that right after deciding to detach C, | |
7188 | * the replacement of B completes. We would have M(A,C), and then | |
7189 | * ask to detach C, which would leave us with just A -- not what | |
7190 | * the user wanted. To prevent this, we make sure that the | |
7191 | * parent/child relationship hasn't changed -- in this example, | |
7192 | * that C's parent is still the replacing vdev R. | |
7193 | */ | |
7194 | if (pvd->vdev_guid != pguid && pguid != 0) | |
7195 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
7196 | ||
34dc7c2f | 7197 | /* |
572e2857 | 7198 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 7199 | */ |
572e2857 BB |
7200 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
7201 | pvd->vdev_ops != &vdev_spare_ops) | |
7202 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
7203 | |
7204 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
7205 | spa_version(spa) >= SPA_VERSION_SPARES); | |
7206 | ||
7207 | /* | |
7208 | * Only mirror, replacing, and spare vdevs support detach. | |
7209 | */ | |
7210 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
7211 | pvd->vdev_ops != &vdev_mirror_ops && | |
7212 | pvd->vdev_ops != &vdev_spare_ops) | |
7213 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
7214 | ||
7215 | /* | |
fb5f0bc8 BB |
7216 | * If this device has the only valid copy of some data, |
7217 | * we cannot safely detach it. | |
34dc7c2f | 7218 | */ |
fb5f0bc8 | 7219 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
7220 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
7221 | ||
fb5f0bc8 | 7222 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 7223 | |
b128c09f BB |
7224 | /* |
7225 | * If we are detaching the second disk from a replacing vdev, then | |
7226 | * check to see if we changed the original vdev's path to have "/old" | |
7227 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
7228 | */ | |
572e2857 BB |
7229 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
7230 | vd->vdev_path != NULL) { | |
7231 | size_t len = strlen(vd->vdev_path); | |
7232 | ||
1c27024e | 7233 | for (int c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
7234 | cvd = pvd->vdev_child[c]; |
7235 | ||
7236 | if (cvd == vd || cvd->vdev_path == NULL) | |
7237 | continue; | |
7238 | ||
7239 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
7240 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
7241 | spa_strfree(cvd->vdev_path); | |
7242 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
7243 | break; | |
7244 | } | |
b128c09f BB |
7245 | } |
7246 | } | |
7247 | ||
34dc7c2f | 7248 | /* |
b2255edc BB |
7249 | * If we are detaching the original disk from a normal spare, then it |
7250 | * implies that the spare should become a real disk, and be removed | |
7251 | * from the active spare list for the pool. dRAID spares on the | |
7252 | * other hand are coupled to the pool and thus should never be removed | |
7253 | * from the spares list. | |
34dc7c2f | 7254 | */ |
b2255edc BB |
7255 | if (pvd->vdev_ops == &vdev_spare_ops && vd->vdev_id == 0) { |
7256 | vdev_t *last_cvd = pvd->vdev_child[pvd->vdev_children - 1]; | |
7257 | ||
7258 | if (last_cvd->vdev_isspare && | |
7259 | last_cvd->vdev_ops != &vdev_draid_spare_ops) { | |
7260 | unspare = B_TRUE; | |
7261 | } | |
7262 | } | |
34dc7c2f BB |
7263 | |
7264 | /* | |
7265 | * Erase the disk labels so the disk can be used for other things. | |
7266 | * This must be done after all other error cases are handled, | |
7267 | * but before we disembowel vd (so we can still do I/O to it). | |
7268 | * But if we can't do it, don't treat the error as fatal -- | |
7269 | * it may be that the unwritability of the disk is the reason | |
7270 | * it's being detached! | |
7271 | */ | |
6a42939f | 7272 | (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); |
34dc7c2f BB |
7273 | |
7274 | /* | |
7275 | * Remove vd from its parent and compact the parent's children. | |
7276 | */ | |
7277 | vdev_remove_child(pvd, vd); | |
7278 | vdev_compact_children(pvd); | |
7279 | ||
7280 | /* | |
7281 | * Remember one of the remaining children so we can get tvd below. | |
7282 | */ | |
572e2857 | 7283 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
7284 | |
7285 | /* | |
7286 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
7287 | * do it now, marking the vdev as no longer a spare in the process. |
7288 | * We must do this before vdev_remove_parent(), because that can | |
7289 | * change the GUID if it creates a new toplevel GUID. For a similar | |
7290 | * reason, we must remove the spare now, in the same txg as the detach; | |
7291 | * otherwise someone could attach a new sibling, change the GUID, and | |
7292 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
7293 | */ |
7294 | if (unspare) { | |
7295 | ASSERT(cvd->vdev_isspare); | |
7296 | spa_spare_remove(cvd); | |
7297 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 7298 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 7299 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
7300 | } |
7301 | ||
428870ff BB |
7302 | /* |
7303 | * If the parent mirror/replacing vdev only has one child, | |
7304 | * the parent is no longer needed. Remove it from the tree. | |
7305 | */ | |
572e2857 BB |
7306 | if (pvd->vdev_children == 1) { |
7307 | if (pvd->vdev_ops == &vdev_spare_ops) | |
7308 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 7309 | vdev_remove_parent(cvd); |
572e2857 BB |
7310 | } |
7311 | ||
428870ff BB |
7312 | /* |
7313 | * We don't set tvd until now because the parent we just removed | |
7314 | * may have been the previous top-level vdev. | |
7315 | */ | |
7316 | tvd = cvd->vdev_top; | |
7317 | ASSERT(tvd->vdev_parent == rvd); | |
7318 | ||
7319 | /* | |
7320 | * Reevaluate the parent vdev state. | |
7321 | */ | |
7322 | vdev_propagate_state(cvd); | |
7323 | ||
7324 | /* | |
7325 | * If the 'autoexpand' property is set on the pool then automatically | |
7326 | * try to expand the size of the pool. For example if the device we | |
7327 | * just detached was smaller than the others, it may be possible to | |
7328 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
7329 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
7330 | */ | |
7331 | if (spa->spa_autoexpand) { | |
7332 | vdev_reopen(tvd); | |
7333 | vdev_expand(tvd, txg); | |
7334 | } | |
7335 | ||
7336 | vdev_config_dirty(tvd); | |
7337 | ||
7338 | /* | |
7339 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
7340 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
7341 | * But first make sure we're not on any *other* txg's DTL list, to | |
7342 | * prevent vd from being accessed after it's freed. | |
7343 | */ | |
b6ca6193 | 7344 | vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none"); |
1c27024e | 7345 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff BB |
7346 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
7347 | vd->vdev_detached = B_TRUE; | |
7348 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
7349 | ||
12fa0466 | 7350 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); |
e60e158e | 7351 | spa_notify_waiters(spa); |
428870ff | 7352 | |
572e2857 BB |
7353 | /* hang on to the spa before we release the lock */ |
7354 | spa_open_ref(spa, FTAG); | |
7355 | ||
428870ff BB |
7356 | error = spa_vdev_exit(spa, vd, txg, 0); |
7357 | ||
6f1ffb06 | 7358 | spa_history_log_internal(spa, "detach", NULL, |
428870ff BB |
7359 | "vdev=%s", vdpath); |
7360 | spa_strfree(vdpath); | |
7361 | ||
7362 | /* | |
7363 | * If this was the removal of the original device in a hot spare vdev, | |
7364 | * then we want to go through and remove the device from the hot spare | |
7365 | * list of every other pool. | |
7366 | */ | |
7367 | if (unspare) { | |
572e2857 BB |
7368 | spa_t *altspa = NULL; |
7369 | ||
428870ff | 7370 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
7371 | while ((altspa = spa_next(altspa)) != NULL) { |
7372 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
7373 | altspa == spa) | |
428870ff | 7374 | continue; |
572e2857 BB |
7375 | |
7376 | spa_open_ref(altspa, FTAG); | |
428870ff | 7377 | mutex_exit(&spa_namespace_lock); |
572e2857 | 7378 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 7379 | mutex_enter(&spa_namespace_lock); |
572e2857 | 7380 | spa_close(altspa, FTAG); |
428870ff BB |
7381 | } |
7382 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
7383 | |
7384 | /* search the rest of the vdevs for spares to remove */ | |
7385 | spa_vdev_resilver_done(spa); | |
428870ff BB |
7386 | } |
7387 | ||
572e2857 BB |
7388 | /* all done with the spa; OK to release */ |
7389 | mutex_enter(&spa_namespace_lock); | |
7390 | spa_close(spa, FTAG); | |
7391 | mutex_exit(&spa_namespace_lock); | |
7392 | ||
428870ff BB |
7393 | return (error); |
7394 | } | |
7395 | ||
c10d37dd GW |
7396 | static int |
7397 | spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7398 | list_t *vd_list) | |
619f0976 | 7399 | { |
c10d37dd GW |
7400 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7401 | ||
619f0976 GW |
7402 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); |
7403 | ||
7404 | /* Look up vdev and ensure it's a leaf. */ | |
7405 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7406 | if (vd == NULL || vd->vdev_detached) { | |
7407 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7408 | return (SET_ERROR(ENODEV)); |
7409 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7410 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7411 | return (SET_ERROR(EINVAL)); |
7412 | } else if (!vdev_writeable(vd)) { | |
7413 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7414 | return (SET_ERROR(EROFS)); |
7415 | } | |
7416 | mutex_enter(&vd->vdev_initialize_lock); | |
7417 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7418 | ||
7419 | /* | |
7420 | * When we activate an initialize action we check to see | |
7421 | * if the vdev_initialize_thread is NULL. We do this instead | |
7422 | * of using the vdev_initialize_state since there might be | |
7423 | * a previous initialization process which has completed but | |
7424 | * the thread is not exited. | |
7425 | */ | |
1b939560 | 7426 | if (cmd_type == POOL_INITIALIZE_START && |
619f0976 GW |
7427 | (vd->vdev_initialize_thread != NULL || |
7428 | vd->vdev_top->vdev_removing)) { | |
7429 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7430 | return (SET_ERROR(EBUSY)); |
7431 | } else if (cmd_type == POOL_INITIALIZE_CANCEL && | |
7432 | (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && | |
7433 | vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { | |
7434 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7435 | return (SET_ERROR(ESRCH)); |
7436 | } else if (cmd_type == POOL_INITIALIZE_SUSPEND && | |
7437 | vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { | |
7438 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 | 7439 | return (SET_ERROR(ESRCH)); |
e34e15ed BB |
7440 | } else if (cmd_type == POOL_INITIALIZE_UNINIT && |
7441 | vd->vdev_initialize_thread != NULL) { | |
7442 | mutex_exit(&vd->vdev_initialize_lock); | |
7443 | return (SET_ERROR(EBUSY)); | |
619f0976 GW |
7444 | } |
7445 | ||
7446 | switch (cmd_type) { | |
1b939560 | 7447 | case POOL_INITIALIZE_START: |
619f0976 GW |
7448 | vdev_initialize(vd); |
7449 | break; | |
7450 | case POOL_INITIALIZE_CANCEL: | |
c10d37dd | 7451 | vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); |
619f0976 GW |
7452 | break; |
7453 | case POOL_INITIALIZE_SUSPEND: | |
c10d37dd | 7454 | vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); |
619f0976 | 7455 | break; |
e34e15ed BB |
7456 | case POOL_INITIALIZE_UNINIT: |
7457 | vdev_uninitialize(vd); | |
7458 | break; | |
619f0976 GW |
7459 | default: |
7460 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7461 | } | |
7462 | mutex_exit(&vd->vdev_initialize_lock); | |
7463 | ||
c10d37dd GW |
7464 | return (0); |
7465 | } | |
7466 | ||
7467 | int | |
7468 | spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, | |
7469 | nvlist_t *vdev_errlist) | |
7470 | { | |
7471 | int total_errors = 0; | |
7472 | list_t vd_list; | |
7473 | ||
7474 | list_create(&vd_list, sizeof (vdev_t), | |
7475 | offsetof(vdev_t, vdev_initialize_node)); | |
7476 | ||
7477 | /* | |
7478 | * We hold the namespace lock through the whole function | |
7479 | * to prevent any changes to the pool while we're starting or | |
7480 | * stopping initialization. The config and state locks are held so that | |
7481 | * we can properly assess the vdev state before we commit to | |
7482 | * the initializing operation. | |
7483 | */ | |
7484 | mutex_enter(&spa_namespace_lock); | |
7485 | ||
7486 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7487 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7488 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7489 | ||
7490 | int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, | |
7491 | &vd_list); | |
7492 | if (error != 0) { | |
7493 | char guid_as_str[MAXNAMELEN]; | |
7494 | ||
7495 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7496 | "%llu", (unsigned long long)vdev_guid); | |
7497 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7498 | total_errors++; | |
7499 | } | |
7500 | } | |
7501 | ||
7502 | /* Wait for all initialize threads to stop. */ | |
7503 | vdev_initialize_stop_wait(spa, &vd_list); | |
7504 | ||
619f0976 GW |
7505 | /* Sync out the initializing state */ |
7506 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7507 | mutex_exit(&spa_namespace_lock); | |
7508 | ||
c10d37dd | 7509 | list_destroy(&vd_list); |
619f0976 | 7510 | |
c10d37dd GW |
7511 | return (total_errors); |
7512 | } | |
619f0976 | 7513 | |
1b939560 BB |
7514 | static int |
7515 | spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7516 | uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list) | |
7517 | { | |
7518 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7519 | ||
7520 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
7521 | ||
7522 | /* Look up vdev and ensure it's a leaf. */ | |
7523 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7524 | if (vd == NULL || vd->vdev_detached) { | |
7525 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7526 | return (SET_ERROR(ENODEV)); | |
7527 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7528 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7529 | return (SET_ERROR(EINVAL)); | |
7530 | } else if (!vdev_writeable(vd)) { | |
7531 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7532 | return (SET_ERROR(EROFS)); | |
7533 | } else if (!vd->vdev_has_trim) { | |
7534 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7535 | return (SET_ERROR(EOPNOTSUPP)); | |
7536 | } else if (secure && !vd->vdev_has_securetrim) { | |
7537 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7538 | return (SET_ERROR(EOPNOTSUPP)); | |
7539 | } | |
7540 | mutex_enter(&vd->vdev_trim_lock); | |
7541 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7542 | ||
7543 | /* | |
7544 | * When we activate a TRIM action we check to see if the | |
7545 | * vdev_trim_thread is NULL. We do this instead of using the | |
7546 | * vdev_trim_state since there might be a previous TRIM process | |
7547 | * which has completed but the thread is not exited. | |
7548 | */ | |
7549 | if (cmd_type == POOL_TRIM_START && | |
7550 | (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing)) { | |
7551 | mutex_exit(&vd->vdev_trim_lock); | |
7552 | return (SET_ERROR(EBUSY)); | |
7553 | } else if (cmd_type == POOL_TRIM_CANCEL && | |
7554 | (vd->vdev_trim_state != VDEV_TRIM_ACTIVE && | |
7555 | vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) { | |
7556 | mutex_exit(&vd->vdev_trim_lock); | |
7557 | return (SET_ERROR(ESRCH)); | |
7558 | } else if (cmd_type == POOL_TRIM_SUSPEND && | |
7559 | vd->vdev_trim_state != VDEV_TRIM_ACTIVE) { | |
7560 | mutex_exit(&vd->vdev_trim_lock); | |
7561 | return (SET_ERROR(ESRCH)); | |
7562 | } | |
7563 | ||
7564 | switch (cmd_type) { | |
7565 | case POOL_TRIM_START: | |
7566 | vdev_trim(vd, rate, partial, secure); | |
7567 | break; | |
7568 | case POOL_TRIM_CANCEL: | |
7569 | vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list); | |
7570 | break; | |
7571 | case POOL_TRIM_SUSPEND: | |
7572 | vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list); | |
7573 | break; | |
7574 | default: | |
7575 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7576 | } | |
7577 | mutex_exit(&vd->vdev_trim_lock); | |
7578 | ||
7579 | return (0); | |
7580 | } | |
7581 | ||
7582 | /* | |
7583 | * Initiates a manual TRIM for the requested vdevs. This kicks off individual | |
7584 | * TRIM threads for each child vdev. These threads pass over all of the free | |
7585 | * space in the vdev's metaslabs and issues TRIM commands for that space. | |
7586 | */ | |
7587 | int | |
7588 | spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate, | |
7589 | boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist) | |
7590 | { | |
7591 | int total_errors = 0; | |
7592 | list_t vd_list; | |
7593 | ||
7594 | list_create(&vd_list, sizeof (vdev_t), | |
7595 | offsetof(vdev_t, vdev_trim_node)); | |
7596 | ||
7597 | /* | |
7598 | * We hold the namespace lock through the whole function | |
7599 | * to prevent any changes to the pool while we're starting or | |
7600 | * stopping TRIM. The config and state locks are held so that | |
7601 | * we can properly assess the vdev state before we commit to | |
7602 | * the TRIM operation. | |
7603 | */ | |
7604 | mutex_enter(&spa_namespace_lock); | |
7605 | ||
7606 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7607 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7608 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7609 | ||
7610 | int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type, | |
7611 | rate, partial, secure, &vd_list); | |
7612 | if (error != 0) { | |
7613 | char guid_as_str[MAXNAMELEN]; | |
7614 | ||
7615 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7616 | "%llu", (unsigned long long)vdev_guid); | |
7617 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7618 | total_errors++; | |
7619 | } | |
7620 | } | |
7621 | ||
7622 | /* Wait for all TRIM threads to stop. */ | |
7623 | vdev_trim_stop_wait(spa, &vd_list); | |
7624 | ||
7625 | /* Sync out the TRIM state */ | |
7626 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7627 | mutex_exit(&spa_namespace_lock); | |
7628 | ||
7629 | list_destroy(&vd_list); | |
7630 | ||
7631 | return (total_errors); | |
7632 | } | |
7633 | ||
428870ff BB |
7634 | /* |
7635 | * Split a set of devices from their mirrors, and create a new pool from them. | |
7636 | */ | |
7637 | int | |
a926aab9 | 7638 | spa_vdev_split_mirror(spa_t *spa, const char *newname, nvlist_t *config, |
428870ff BB |
7639 | nvlist_t *props, boolean_t exp) |
7640 | { | |
7641 | int error = 0; | |
7642 | uint64_t txg, *glist; | |
7643 | spa_t *newspa; | |
7644 | uint_t c, children, lastlog; | |
7645 | nvlist_t **child, *nvl, *tmp; | |
7646 | dmu_tx_t *tx; | |
d1807f16 | 7647 | const char *altroot = NULL; |
428870ff BB |
7648 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ |
7649 | boolean_t activate_slog; | |
7650 | ||
572e2857 | 7651 | ASSERT(spa_writeable(spa)); |
428870ff BB |
7652 | |
7653 | txg = spa_vdev_enter(spa); | |
7654 | ||
d2734cce SD |
7655 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7656 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7657 | error = (spa_has_checkpoint(spa)) ? | |
7658 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7659 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7660 | } | |
7661 | ||
428870ff BB |
7662 | /* clear the log and flush everything up to now */ |
7663 | activate_slog = spa_passivate_log(spa); | |
7664 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
a1d477c2 | 7665 | error = spa_reset_logs(spa); |
428870ff BB |
7666 | txg = spa_vdev_config_enter(spa); |
7667 | ||
7668 | if (activate_slog) | |
7669 | spa_activate_log(spa); | |
7670 | ||
7671 | if (error != 0) | |
7672 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7673 | ||
7674 | /* check new spa name before going any further */ | |
7675 | if (spa_lookup(newname) != NULL) | |
7676 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
7677 | ||
7678 | /* | |
7679 | * scan through all the children to ensure they're all mirrors | |
7680 | */ | |
7681 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
7682 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
7683 | &children) != 0) | |
7684 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7685 | ||
7686 | /* first, check to ensure we've got the right child count */ | |
7687 | rvd = spa->spa_root_vdev; | |
7688 | lastlog = 0; | |
7689 | for (c = 0; c < rvd->vdev_children; c++) { | |
7690 | vdev_t *vd = rvd->vdev_child[c]; | |
7691 | ||
7692 | /* don't count the holes & logs as children */ | |
1b664952 GA |
7693 | if (vd->vdev_islog || (vd->vdev_ops != &vdev_indirect_ops && |
7694 | !vdev_is_concrete(vd))) { | |
428870ff BB |
7695 | if (lastlog == 0) |
7696 | lastlog = c; | |
7697 | continue; | |
7698 | } | |
7699 | ||
7700 | lastlog = 0; | |
7701 | } | |
7702 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
7703 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7704 | ||
7705 | /* next, ensure no spare or cache devices are part of the split */ | |
7706 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
7707 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
7708 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7709 | ||
79c76d5b BB |
7710 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); |
7711 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
428870ff BB |
7712 | |
7713 | /* then, loop over each vdev and validate it */ | |
7714 | for (c = 0; c < children; c++) { | |
7715 | uint64_t is_hole = 0; | |
7716 | ||
7717 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
7718 | &is_hole); | |
7719 | ||
7720 | if (is_hole != 0) { | |
7721 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
7722 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
7723 | continue; | |
7724 | } else { | |
2e528b49 | 7725 | error = SET_ERROR(EINVAL); |
428870ff BB |
7726 | break; |
7727 | } | |
7728 | } | |
7729 | ||
1b664952 GA |
7730 | /* deal with indirect vdevs */ |
7731 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ops == | |
7732 | &vdev_indirect_ops) | |
7733 | continue; | |
7734 | ||
428870ff BB |
7735 | /* which disk is going to be split? */ |
7736 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
7737 | &glist[c]) != 0) { | |
2e528b49 | 7738 | error = SET_ERROR(EINVAL); |
428870ff BB |
7739 | break; |
7740 | } | |
7741 | ||
7742 | /* look it up in the spa */ | |
7743 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
7744 | if (vml[c] == NULL) { | |
2e528b49 | 7745 | error = SET_ERROR(ENODEV); |
428870ff BB |
7746 | break; |
7747 | } | |
7748 | ||
7749 | /* make sure there's nothing stopping the split */ | |
7750 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
7751 | vml[c]->vdev_islog || | |
a1d477c2 | 7752 | !vdev_is_concrete(vml[c]) || |
428870ff BB |
7753 | vml[c]->vdev_isspare || |
7754 | vml[c]->vdev_isl2cache || | |
7755 | !vdev_writeable(vml[c]) || | |
7756 | vml[c]->vdev_children != 0 || | |
7757 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
7758 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
2e528b49 | 7759 | error = SET_ERROR(EINVAL); |
428870ff BB |
7760 | break; |
7761 | } | |
7762 | ||
733b5722 RS |
7763 | if (vdev_dtl_required(vml[c]) || |
7764 | vdev_resilver_needed(vml[c], NULL, NULL)) { | |
2e528b49 | 7765 | error = SET_ERROR(EBUSY); |
428870ff BB |
7766 | break; |
7767 | } | |
7768 | ||
7769 | /* we need certain info from the top level */ | |
65ad5d11 AJ |
7770 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, |
7771 | vml[c]->vdev_top->vdev_ms_array); | |
7772 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
7773 | vml[c]->vdev_top->vdev_ms_shift); | |
7774 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
7775 | vml[c]->vdev_top->vdev_asize); | |
7776 | fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
7777 | vml[c]->vdev_top->vdev_ashift); | |
e0ab3ab5 JS |
7778 | |
7779 | /* transfer per-vdev ZAPs */ | |
7780 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
7781 | VERIFY0(nvlist_add_uint64(child[c], | |
7782 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
7783 | ||
7784 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
7785 | VERIFY0(nvlist_add_uint64(child[c], | |
7786 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
7787 | vml[c]->vdev_parent->vdev_top_zap)); | |
428870ff BB |
7788 | } |
7789 | ||
7790 | if (error != 0) { | |
7791 | kmem_free(vml, children * sizeof (vdev_t *)); | |
7792 | kmem_free(glist, children * sizeof (uint64_t)); | |
7793 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7794 | } | |
7795 | ||
7796 | /* stop writers from using the disks */ | |
7797 | for (c = 0; c < children; c++) { | |
7798 | if (vml[c] != NULL) | |
7799 | vml[c]->vdev_offline = B_TRUE; | |
7800 | } | |
7801 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
7802 | |
7803 | /* | |
428870ff BB |
7804 | * Temporarily record the splitting vdevs in the spa config. This |
7805 | * will disappear once the config is regenerated. | |
34dc7c2f | 7806 | */ |
65ad5d11 AJ |
7807 | nvl = fnvlist_alloc(); |
7808 | fnvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, glist, children); | |
428870ff | 7809 | kmem_free(glist, children * sizeof (uint64_t)); |
34dc7c2f | 7810 | |
428870ff | 7811 | mutex_enter(&spa->spa_props_lock); |
65ad5d11 | 7812 | fnvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, nvl); |
428870ff BB |
7813 | mutex_exit(&spa->spa_props_lock); |
7814 | spa->spa_config_splitting = nvl; | |
7815 | vdev_config_dirty(spa->spa_root_vdev); | |
7816 | ||
7817 | /* configure and create the new pool */ | |
65ad5d11 AJ |
7818 | fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname); |
7819 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
7820 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE); | |
7821 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa)); | |
7822 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, spa->spa_config_txg); | |
7823 | fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
7824 | spa_generate_guid(NULL)); | |
e0ab3ab5 | 7825 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); |
428870ff BB |
7826 | (void) nvlist_lookup_string(props, |
7827 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 7828 | |
428870ff BB |
7829 | /* add the new pool to the namespace */ |
7830 | newspa = spa_add(newname, config, altroot); | |
e0ab3ab5 | 7831 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7832 | newspa->spa_config_txg = spa->spa_config_txg; |
7833 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
7834 | ||
7835 | /* release the spa config lock, retaining the namespace lock */ | |
7836 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
7837 | ||
7838 | if (zio_injection_enabled) | |
7839 | zio_handle_panic_injection(spa, FTAG, 1); | |
7840 | ||
7841 | spa_activate(newspa, spa_mode_global); | |
7842 | spa_async_suspend(newspa); | |
7843 | ||
c10d37dd | 7844 | /* |
1b939560 BB |
7845 | * Temporarily stop the initializing and TRIM activity. We set the |
7846 | * state to ACTIVE so that we know to resume initializing or TRIM | |
7847 | * once the split has completed. | |
c10d37dd | 7848 | */ |
1b939560 BB |
7849 | list_t vd_initialize_list; |
7850 | list_create(&vd_initialize_list, sizeof (vdev_t), | |
c10d37dd GW |
7851 | offsetof(vdev_t, vdev_initialize_node)); |
7852 | ||
1b939560 BB |
7853 | list_t vd_trim_list; |
7854 | list_create(&vd_trim_list, sizeof (vdev_t), | |
7855 | offsetof(vdev_t, vdev_trim_node)); | |
7856 | ||
619f0976 | 7857 | for (c = 0; c < children; c++) { |
1b664952 | 7858 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
619f0976 | 7859 | mutex_enter(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7860 | vdev_initialize_stop(vml[c], |
7861 | VDEV_INITIALIZE_ACTIVE, &vd_initialize_list); | |
619f0976 | 7862 | mutex_exit(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7863 | |
7864 | mutex_enter(&vml[c]->vdev_trim_lock); | |
7865 | vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list); | |
7866 | mutex_exit(&vml[c]->vdev_trim_lock); | |
619f0976 GW |
7867 | } |
7868 | } | |
1b939560 BB |
7869 | |
7870 | vdev_initialize_stop_wait(spa, &vd_initialize_list); | |
7871 | vdev_trim_stop_wait(spa, &vd_trim_list); | |
7872 | ||
7873 | list_destroy(&vd_initialize_list); | |
7874 | list_destroy(&vd_trim_list); | |
619f0976 | 7875 | |
6cb8e530 | 7876 | newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; |
8b27e08e | 7877 | newspa->spa_is_splitting = B_TRUE; |
6cb8e530 | 7878 | |
428870ff | 7879 | /* create the new pool from the disks of the original pool */ |
6cb8e530 | 7880 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); |
428870ff BB |
7881 | if (error) |
7882 | goto out; | |
7883 | ||
7884 | /* if that worked, generate a real config for the new pool */ | |
7885 | if (newspa->spa_root_vdev != NULL) { | |
65ad5d11 AJ |
7886 | newspa->spa_config_splitting = fnvlist_alloc(); |
7887 | fnvlist_add_uint64(newspa->spa_config_splitting, | |
7888 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)); | |
428870ff BB |
7889 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, |
7890 | B_TRUE)); | |
9babb374 | 7891 | } |
34dc7c2f | 7892 | |
428870ff BB |
7893 | /* set the props */ |
7894 | if (props != NULL) { | |
7895 | spa_configfile_set(newspa, props, B_FALSE); | |
7896 | error = spa_prop_set(newspa, props); | |
7897 | if (error) | |
7898 | goto out; | |
7899 | } | |
34dc7c2f | 7900 | |
428870ff BB |
7901 | /* flush everything */ |
7902 | txg = spa_vdev_config_enter(newspa); | |
7903 | vdev_config_dirty(newspa->spa_root_vdev); | |
7904 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 7905 | |
428870ff BB |
7906 | if (zio_injection_enabled) |
7907 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 7908 | |
428870ff | 7909 | spa_async_resume(newspa); |
34dc7c2f | 7910 | |
428870ff BB |
7911 | /* finally, update the original pool's config */ |
7912 | txg = spa_vdev_config_enter(spa); | |
7913 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
7914 | error = dmu_tx_assign(tx, TXG_WAIT); | |
7915 | if (error != 0) | |
7916 | dmu_tx_abort(tx); | |
7917 | for (c = 0; c < children; c++) { | |
1b664952 | 7918 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
234234ca RS |
7919 | vdev_t *tvd = vml[c]->vdev_top; |
7920 | ||
7921 | /* | |
7922 | * Need to be sure the detachable VDEV is not | |
7923 | * on any *other* txg's DTL list to prevent it | |
7924 | * from being accessed after it's freed. | |
7925 | */ | |
7926 | for (int t = 0; t < TXG_SIZE; t++) { | |
7927 | (void) txg_list_remove_this( | |
7928 | &tvd->vdev_dtl_list, vml[c], t); | |
7929 | } | |
7930 | ||
428870ff BB |
7931 | vdev_split(vml[c]); |
7932 | if (error == 0) | |
6f1ffb06 MA |
7933 | spa_history_log_internal(spa, "detach", tx, |
7934 | "vdev=%s", vml[c]->vdev_path); | |
e0ab3ab5 | 7935 | |
428870ff | 7936 | vdev_free(vml[c]); |
34dc7c2f | 7937 | } |
34dc7c2f | 7938 | } |
e0ab3ab5 | 7939 | spa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7940 | vdev_config_dirty(spa->spa_root_vdev); |
7941 | spa->spa_config_splitting = NULL; | |
7942 | nvlist_free(nvl); | |
7943 | if (error == 0) | |
7944 | dmu_tx_commit(tx); | |
7945 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
7946 | ||
7947 | if (zio_injection_enabled) | |
7948 | zio_handle_panic_injection(spa, FTAG, 3); | |
7949 | ||
7950 | /* split is complete; log a history record */ | |
6f1ffb06 MA |
7951 | spa_history_log_internal(newspa, "split", NULL, |
7952 | "from pool %s", spa_name(spa)); | |
428870ff | 7953 | |
8b27e08e | 7954 | newspa->spa_is_splitting = B_FALSE; |
428870ff BB |
7955 | kmem_free(vml, children * sizeof (vdev_t *)); |
7956 | ||
7957 | /* if we're not going to mount the filesystems in userland, export */ | |
7958 | if (exp) | |
7959 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
7960 | B_FALSE, B_FALSE); | |
7961 | ||
7962 | return (error); | |
7963 | ||
7964 | out: | |
7965 | spa_unload(newspa); | |
7966 | spa_deactivate(newspa); | |
7967 | spa_remove(newspa); | |
7968 | ||
7969 | txg = spa_vdev_config_enter(spa); | |
7970 | ||
7971 | /* re-online all offlined disks */ | |
7972 | for (c = 0; c < children; c++) { | |
7973 | if (vml[c] != NULL) | |
7974 | vml[c]->vdev_offline = B_FALSE; | |
7975 | } | |
619f0976 | 7976 | |
1b939560 | 7977 | /* restart initializing or trimming disks as necessary */ |
619f0976 | 7978 | spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); |
1b939560 BB |
7979 | spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); |
7980 | spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); | |
619f0976 | 7981 | |
428870ff BB |
7982 | vdev_reopen(spa->spa_root_vdev); |
7983 | ||
7984 | nvlist_free(spa->spa_config_splitting); | |
7985 | spa->spa_config_splitting = NULL; | |
7986 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 7987 | |
428870ff | 7988 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
7989 | return (error); |
7990 | } | |
7991 | ||
34dc7c2f BB |
7992 | /* |
7993 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
d3cc8b15 | 7994 | * currently spared, so we can detach it. |
34dc7c2f BB |
7995 | */ |
7996 | static vdev_t * | |
7997 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
7998 | { | |
7999 | vdev_t *newvd, *oldvd; | |
34dc7c2f | 8000 | |
1c27024e | 8001 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
8002 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
8003 | if (oldvd != NULL) | |
8004 | return (oldvd); | |
8005 | } | |
8006 | ||
8007 | /* | |
572e2857 BB |
8008 | * Check for a completed replacement. We always consider the first |
8009 | * vdev in the list to be the oldest vdev, and the last one to be | |
8010 | * the newest (see spa_vdev_attach() for how that works). In | |
8011 | * the case where the newest vdev is faulted, we will not automatically | |
8012 | * remove it after a resilver completes. This is OK as it will require | |
8013 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 8014 | */ |
572e2857 BB |
8015 | if (vd->vdev_ops == &vdev_replacing_ops) { |
8016 | ASSERT(vd->vdev_children > 1); | |
8017 | ||
8018 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 8019 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 8020 | |
fb5f0bc8 | 8021 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 8022 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 8023 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 8024 | return (oldvd); |
34dc7c2f BB |
8025 | } |
8026 | ||
8027 | /* | |
8028 | * Check for a completed resilver with the 'unspare' flag set. | |
f65fbee1 | 8029 | * Also potentially update faulted state. |
34dc7c2f | 8030 | */ |
572e2857 BB |
8031 | if (vd->vdev_ops == &vdev_spare_ops) { |
8032 | vdev_t *first = vd->vdev_child[0]; | |
8033 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
8034 | ||
8035 | if (last->vdev_unspare) { | |
8036 | oldvd = first; | |
8037 | newvd = last; | |
8038 | } else if (first->vdev_unspare) { | |
8039 | oldvd = last; | |
8040 | newvd = first; | |
8041 | } else { | |
8042 | oldvd = NULL; | |
8043 | } | |
34dc7c2f | 8044 | |
572e2857 | 8045 | if (oldvd != NULL && |
fb5f0bc8 | 8046 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 8047 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 8048 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 8049 | return (oldvd); |
572e2857 | 8050 | |
f65fbee1 JJ |
8051 | vdev_propagate_state(vd); |
8052 | ||
572e2857 BB |
8053 | /* |
8054 | * If there are more than two spares attached to a disk, | |
8055 | * and those spares are not required, then we want to | |
8056 | * attempt to free them up now so that they can be used | |
8057 | * by other pools. Once we're back down to a single | |
8058 | * disk+spare, we stop removing them. | |
8059 | */ | |
8060 | if (vd->vdev_children > 2) { | |
8061 | newvd = vd->vdev_child[1]; | |
8062 | ||
8063 | if (newvd->vdev_isspare && last->vdev_isspare && | |
8064 | vdev_dtl_empty(last, DTL_MISSING) && | |
8065 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
8066 | !vdev_dtl_required(newvd)) | |
8067 | return (newvd); | |
34dc7c2f | 8068 | } |
34dc7c2f BB |
8069 | } |
8070 | ||
8071 | return (NULL); | |
8072 | } | |
8073 | ||
8074 | static void | |
8075 | spa_vdev_resilver_done(spa_t *spa) | |
8076 | { | |
fb5f0bc8 BB |
8077 | vdev_t *vd, *pvd, *ppvd; |
8078 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 8079 | |
fb5f0bc8 | 8080 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
8081 | |
8082 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
8083 | pvd = vd->vdev_parent; |
8084 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 8085 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
8086 | pguid = pvd->vdev_guid; |
8087 | ppguid = ppvd->vdev_guid; | |
8088 | sguid = 0; | |
34dc7c2f BB |
8089 | /* |
8090 | * If we have just finished replacing a hot spared device, then | |
8091 | * we need to detach the parent's first child (the original hot | |
8092 | * spare) as well. | |
8093 | */ | |
572e2857 BB |
8094 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
8095 | ppvd->vdev_children == 2) { | |
34dc7c2f | 8096 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 8097 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 8098 | } |
5d1f7fb6 GW |
8099 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); |
8100 | ||
fb5f0bc8 BB |
8101 | spa_config_exit(spa, SCL_ALL, FTAG); |
8102 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 8103 | return; |
fb5f0bc8 | 8104 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 8105 | return; |
fb5f0bc8 | 8106 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
8107 | } |
8108 | ||
fb5f0bc8 | 8109 | spa_config_exit(spa, SCL_ALL, FTAG); |
9a49d3f3 BB |
8110 | |
8111 | /* | |
8112 | * If a detach was not performed above replace waiters will not have | |
8113 | * been notified. In which case we must do so now. | |
8114 | */ | |
8115 | spa_notify_waiters(spa); | |
34dc7c2f BB |
8116 | } |
8117 | ||
8118 | /* | |
428870ff | 8119 | * Update the stored path or FRU for this vdev. |
34dc7c2f | 8120 | */ |
65c7cc49 | 8121 | static int |
9babb374 BB |
8122 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
8123 | boolean_t ispath) | |
34dc7c2f | 8124 | { |
b128c09f | 8125 | vdev_t *vd; |
428870ff | 8126 | boolean_t sync = B_FALSE; |
34dc7c2f | 8127 | |
572e2857 BB |
8128 | ASSERT(spa_writeable(spa)); |
8129 | ||
428870ff | 8130 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 8131 | |
9babb374 | 8132 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 8133 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
8134 | |
8135 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 8136 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 8137 | |
9babb374 | 8138 | if (ispath) { |
428870ff BB |
8139 | if (strcmp(value, vd->vdev_path) != 0) { |
8140 | spa_strfree(vd->vdev_path); | |
8141 | vd->vdev_path = spa_strdup(value); | |
8142 | sync = B_TRUE; | |
8143 | } | |
9babb374 | 8144 | } else { |
428870ff BB |
8145 | if (vd->vdev_fru == NULL) { |
8146 | vd->vdev_fru = spa_strdup(value); | |
8147 | sync = B_TRUE; | |
8148 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 8149 | spa_strfree(vd->vdev_fru); |
428870ff BB |
8150 | vd->vdev_fru = spa_strdup(value); |
8151 | sync = B_TRUE; | |
8152 | } | |
9babb374 | 8153 | } |
34dc7c2f | 8154 | |
428870ff | 8155 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
8156 | } |
8157 | ||
9babb374 BB |
8158 | int |
8159 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
8160 | { | |
8161 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
8162 | } | |
8163 | ||
8164 | int | |
8165 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
8166 | { | |
8167 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
8168 | } | |
8169 | ||
34dc7c2f BB |
8170 | /* |
8171 | * ========================================================================== | |
428870ff | 8172 | * SPA Scanning |
34dc7c2f BB |
8173 | * ========================================================================== |
8174 | */ | |
0ea05c64 AP |
8175 | int |
8176 | spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) | |
8177 | { | |
8178 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
8179 | ||
8180 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
8181 | return (SET_ERROR(EBUSY)); | |
8182 | ||
8183 | return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); | |
8184 | } | |
34dc7c2f | 8185 | |
34dc7c2f | 8186 | int |
428870ff BB |
8187 | spa_scan_stop(spa_t *spa) |
8188 | { | |
8189 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
8190 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
2e528b49 | 8191 | return (SET_ERROR(EBUSY)); |
482eeef8 | 8192 | |
428870ff BB |
8193 | return (dsl_scan_cancel(spa->spa_dsl_pool)); |
8194 | } | |
8195 | ||
8196 | int | |
8197 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 8198 | { |
b128c09f | 8199 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 8200 | |
428870ff | 8201 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
2e528b49 | 8202 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 8203 | |
fa241660 TC |
8204 | if (func == POOL_SCAN_RESILVER && |
8205 | !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
8206 | return (SET_ERROR(ENOTSUP)); | |
8207 | ||
34dc7c2f | 8208 | /* |
b128c09f BB |
8209 | * If a resilver was requested, but there is no DTL on a |
8210 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 8211 | */ |
428870ff | 8212 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
8213 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
8214 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
8215 | return (0); |
8216 | } | |
8217 | ||
482eeef8 GA |
8218 | if (func == POOL_SCAN_ERRORSCRUB && |
8219 | !spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG)) | |
8220 | return (SET_ERROR(ENOTSUP)); | |
8221 | ||
428870ff | 8222 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
8223 | } |
8224 | ||
8225 | /* | |
8226 | * ========================================================================== | |
8227 | * SPA async task processing | |
8228 | * ========================================================================== | |
8229 | */ | |
8230 | ||
8231 | static void | |
8232 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
8233 | { | |
b128c09f | 8234 | if (vd->vdev_remove_wanted) { |
428870ff BB |
8235 | vd->vdev_remove_wanted = B_FALSE; |
8236 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 8237 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
8238 | |
8239 | /* | |
8240 | * We want to clear the stats, but we don't want to do a full | |
8241 | * vdev_clear() as that will cause us to throw away | |
8242 | * degraded/faulted state as well as attempt to reopen the | |
8243 | * device, all of which is a waste. | |
8244 | */ | |
8245 | vd->vdev_stat.vs_read_errors = 0; | |
8246 | vd->vdev_stat.vs_write_errors = 0; | |
8247 | vd->vdev_stat.vs_checksum_errors = 0; | |
8248 | ||
b128c09f | 8249 | vdev_state_dirty(vd->vdev_top); |
0aacde2e RM |
8250 | |
8251 | /* Tell userspace that the vdev is gone. */ | |
8252 | zfs_post_remove(spa, vd); | |
b128c09f | 8253 | } |
34dc7c2f | 8254 | |
1c27024e | 8255 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
8256 | spa_async_remove(spa, vd->vdev_child[c]); |
8257 | } | |
8258 | ||
8259 | static void | |
8260 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
8261 | { | |
8262 | if (vd->vdev_probe_wanted) { | |
428870ff | 8263 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 8264 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 8265 | } |
b128c09f | 8266 | |
1c27024e | 8267 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 8268 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
8269 | } |
8270 | ||
9babb374 BB |
8271 | static void |
8272 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
8273 | { | |
9babb374 BB |
8274 | if (!spa->spa_autoexpand) |
8275 | return; | |
8276 | ||
1c27024e | 8277 | for (int c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
8278 | vdev_t *cvd = vd->vdev_child[c]; |
8279 | spa_async_autoexpand(spa, cvd); | |
8280 | } | |
8281 | ||
8282 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
8283 | return; | |
8284 | ||
12fa0466 | 8285 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND); |
9babb374 BB |
8286 | } |
8287 | ||
460748d4 | 8288 | static __attribute__((noreturn)) void |
c25b8f99 | 8289 | spa_async_thread(void *arg) |
34dc7c2f | 8290 | { |
c25b8f99 | 8291 | spa_t *spa = (spa_t *)arg; |
80a91e74 | 8292 | dsl_pool_t *dp = spa->spa_dsl_pool; |
867959b5 | 8293 | int tasks; |
34dc7c2f BB |
8294 | |
8295 | ASSERT(spa->spa_sync_on); | |
8296 | ||
8297 | mutex_enter(&spa->spa_async_lock); | |
8298 | tasks = spa->spa_async_tasks; | |
8299 | spa->spa_async_tasks = 0; | |
8300 | mutex_exit(&spa->spa_async_lock); | |
8301 | ||
8302 | /* | |
8303 | * See if the config needs to be updated. | |
8304 | */ | |
8305 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 8306 | uint64_t old_space, new_space; |
9babb374 | 8307 | |
34dc7c2f | 8308 | mutex_enter(&spa_namespace_lock); |
428870ff | 8309 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
8310 | old_space += metaslab_class_get_space(spa_special_class(spa)); |
8311 | old_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
aa755b35 MA |
8312 | old_space += metaslab_class_get_space( |
8313 | spa_embedded_log_class(spa)); | |
cc99f275 | 8314 | |
34dc7c2f | 8315 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
cc99f275 | 8316 | |
428870ff | 8317 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
8318 | new_space += metaslab_class_get_space(spa_special_class(spa)); |
8319 | new_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
aa755b35 MA |
8320 | new_space += metaslab_class_get_space( |
8321 | spa_embedded_log_class(spa)); | |
34dc7c2f | 8322 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
8323 | |
8324 | /* | |
8325 | * If the pool grew as a result of the config update, | |
8326 | * then log an internal history event. | |
8327 | */ | |
428870ff | 8328 | if (new_space != old_space) { |
6f1ffb06 | 8329 | spa_history_log_internal(spa, "vdev online", NULL, |
45d1cae3 | 8330 | "pool '%s' size: %llu(+%llu)", |
74756182 MM |
8331 | spa_name(spa), (u_longlong_t)new_space, |
8332 | (u_longlong_t)(new_space - old_space)); | |
9babb374 | 8333 | } |
34dc7c2f BB |
8334 | } |
8335 | ||
8336 | /* | |
8337 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 8338 | */ |
b128c09f | 8339 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 8340 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 8341 | spa_async_remove(spa, spa->spa_root_vdev); |
867959b5 | 8342 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 8343 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
867959b5 | 8344 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
8345 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
8346 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
8347 | } |
8348 | ||
9babb374 BB |
8349 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
8350 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8351 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
8352 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8353 | } | |
8354 | ||
34dc7c2f | 8355 | /* |
b128c09f | 8356 | * See if any devices need to be probed. |
34dc7c2f | 8357 | */ |
b128c09f | 8358 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 8359 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
8360 | spa_async_probe(spa, spa->spa_root_vdev); |
8361 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
8362 | } | |
34dc7c2f BB |
8363 | |
8364 | /* | |
b128c09f | 8365 | * If any devices are done replacing, detach them. |
34dc7c2f | 8366 | */ |
b2255edc | 8367 | if (tasks & SPA_ASYNC_RESILVER_DONE || |
719534ca AH |
8368 | tasks & SPA_ASYNC_REBUILD_DONE || |
8369 | tasks & SPA_ASYNC_DETACH_SPARE) { | |
b128c09f | 8370 | spa_vdev_resilver_done(spa); |
9a49d3f3 BB |
8371 | } |
8372 | ||
34dc7c2f BB |
8373 | /* |
8374 | * Kick off a resilver. | |
8375 | */ | |
80a91e74 | 8376 | if (tasks & SPA_ASYNC_RESILVER && |
9a49d3f3 | 8377 | !vdev_rebuild_active(spa->spa_root_vdev) && |
80a91e74 TC |
8378 | (!dsl_scan_resilvering(dp) || |
8379 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) | |
3c819a2c | 8380 | dsl_scan_restart_resilver(dp, 0); |
34dc7c2f | 8381 | |
619f0976 GW |
8382 | if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { |
8383 | mutex_enter(&spa_namespace_lock); | |
8384 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8385 | vdev_initialize_restart(spa->spa_root_vdev); | |
8386 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8387 | mutex_exit(&spa_namespace_lock); | |
8388 | } | |
8389 | ||
1b939560 BB |
8390 | if (tasks & SPA_ASYNC_TRIM_RESTART) { |
8391 | mutex_enter(&spa_namespace_lock); | |
8392 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8393 | vdev_trim_restart(spa->spa_root_vdev); | |
8394 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8395 | mutex_exit(&spa_namespace_lock); | |
8396 | } | |
8397 | ||
8398 | if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) { | |
8399 | mutex_enter(&spa_namespace_lock); | |
8400 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8401 | vdev_autotrim_restart(spa); | |
8402 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8403 | mutex_exit(&spa_namespace_lock); | |
8404 | } | |
8405 | ||
b7654bd7 GA |
8406 | /* |
8407 | * Kick off L2 cache whole device TRIM. | |
8408 | */ | |
8409 | if (tasks & SPA_ASYNC_L2CACHE_TRIM) { | |
8410 | mutex_enter(&spa_namespace_lock); | |
8411 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8412 | vdev_trim_l2arc(spa); | |
8413 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8414 | mutex_exit(&spa_namespace_lock); | |
8415 | } | |
8416 | ||
77f6826b GA |
8417 | /* |
8418 | * Kick off L2 cache rebuilding. | |
8419 | */ | |
8420 | if (tasks & SPA_ASYNC_L2CACHE_REBUILD) { | |
8421 | mutex_enter(&spa_namespace_lock); | |
8422 | spa_config_enter(spa, SCL_L2ARC, FTAG, RW_READER); | |
8423 | l2arc_spa_rebuild_start(spa); | |
8424 | spa_config_exit(spa, SCL_L2ARC, FTAG); | |
8425 | mutex_exit(&spa_namespace_lock); | |
8426 | } | |
8427 | ||
34dc7c2f BB |
8428 | /* |
8429 | * Let the world know that we're done. | |
8430 | */ | |
8431 | mutex_enter(&spa->spa_async_lock); | |
8432 | spa->spa_async_thread = NULL; | |
8433 | cv_broadcast(&spa->spa_async_cv); | |
8434 | mutex_exit(&spa->spa_async_lock); | |
8435 | thread_exit(); | |
8436 | } | |
8437 | ||
8438 | void | |
8439 | spa_async_suspend(spa_t *spa) | |
8440 | { | |
8441 | mutex_enter(&spa->spa_async_lock); | |
8442 | spa->spa_async_suspended++; | |
9d5b5245 | 8443 | while (spa->spa_async_thread != NULL) |
34dc7c2f BB |
8444 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); |
8445 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 MA |
8446 | |
8447 | spa_vdev_remove_suspend(spa); | |
9d5b5245 SD |
8448 | |
8449 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a SD |
8450 | if (condense_thread != NULL) |
8451 | zthr_cancel(condense_thread); | |
d2734cce SD |
8452 | |
8453 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a SD |
8454 | if (discard_thread != NULL) |
8455 | zthr_cancel(discard_thread); | |
37f03da8 SH |
8456 | |
8457 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8458 | if (ll_delete_thread != NULL) | |
8459 | zthr_cancel(ll_delete_thread); | |
8460 | ||
8461 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8462 | if (ll_condense_thread != NULL) | |
8463 | zthr_cancel(ll_condense_thread); | |
34dc7c2f BB |
8464 | } |
8465 | ||
8466 | void | |
8467 | spa_async_resume(spa_t *spa) | |
8468 | { | |
8469 | mutex_enter(&spa->spa_async_lock); | |
8470 | ASSERT(spa->spa_async_suspended != 0); | |
8471 | spa->spa_async_suspended--; | |
8472 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 | 8473 | spa_restart_removal(spa); |
9d5b5245 SD |
8474 | |
8475 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a | 8476 | if (condense_thread != NULL) |
9d5b5245 | 8477 | zthr_resume(condense_thread); |
d2734cce SD |
8478 | |
8479 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a | 8480 | if (discard_thread != NULL) |
d2734cce | 8481 | zthr_resume(discard_thread); |
37f03da8 SH |
8482 | |
8483 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8484 | if (ll_delete_thread != NULL) | |
8485 | zthr_resume(ll_delete_thread); | |
8486 | ||
8487 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8488 | if (ll_condense_thread != NULL) | |
8489 | zthr_resume(ll_condense_thread); | |
34dc7c2f BB |
8490 | } |
8491 | ||
e6cfd633 WA |
8492 | static boolean_t |
8493 | spa_async_tasks_pending(spa_t *spa) | |
8494 | { | |
8495 | uint_t non_config_tasks; | |
8496 | uint_t config_task; | |
8497 | boolean_t config_task_suspended; | |
8498 | ||
8499 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
8500 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
8501 | if (spa->spa_ccw_fail_time == 0) { | |
8502 | config_task_suspended = B_FALSE; | |
8503 | } else { | |
8504 | config_task_suspended = | |
8505 | (gethrtime() - spa->spa_ccw_fail_time) < | |
05852b34 | 8506 | ((hrtime_t)zfs_ccw_retry_interval * NANOSEC); |
e6cfd633 WA |
8507 | } |
8508 | ||
8509 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
8510 | } | |
8511 | ||
34dc7c2f BB |
8512 | static void |
8513 | spa_async_dispatch(spa_t *spa) | |
8514 | { | |
8515 | mutex_enter(&spa->spa_async_lock); | |
e6cfd633 WA |
8516 | if (spa_async_tasks_pending(spa) && |
8517 | !spa->spa_async_suspended && | |
da92d5cb | 8518 | spa->spa_async_thread == NULL) |
34dc7c2f BB |
8519 | spa->spa_async_thread = thread_create(NULL, 0, |
8520 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
8521 | mutex_exit(&spa->spa_async_lock); | |
8522 | } | |
8523 | ||
8524 | void | |
8525 | spa_async_request(spa_t *spa, int task) | |
8526 | { | |
428870ff | 8527 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
8528 | mutex_enter(&spa->spa_async_lock); |
8529 | spa->spa_async_tasks |= task; | |
8530 | mutex_exit(&spa->spa_async_lock); | |
8531 | } | |
8532 | ||
3c819a2c JP |
8533 | int |
8534 | spa_async_tasks(spa_t *spa) | |
8535 | { | |
8536 | return (spa->spa_async_tasks); | |
8537 | } | |
8538 | ||
34dc7c2f BB |
8539 | /* |
8540 | * ========================================================================== | |
8541 | * SPA syncing routines | |
8542 | * ========================================================================== | |
8543 | */ | |
8544 | ||
37f03da8 | 8545 | |
428870ff | 8546 | static int |
37f03da8 SH |
8547 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, |
8548 | dmu_tx_t *tx) | |
34dc7c2f | 8549 | { |
428870ff | 8550 | bpobj_t *bpo = arg; |
37f03da8 | 8551 | bpobj_enqueue(bpo, bp, bp_freed, tx); |
428870ff BB |
8552 | return (0); |
8553 | } | |
34dc7c2f | 8554 | |
37f03da8 SH |
8555 | int |
8556 | bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8557 | { | |
8558 | return (bpobj_enqueue_cb(arg, bp, B_FALSE, tx)); | |
8559 | } | |
8560 | ||
8561 | int | |
8562 | bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8563 | { | |
8564 | return (bpobj_enqueue_cb(arg, bp, B_TRUE, tx)); | |
8565 | } | |
8566 | ||
428870ff BB |
8567 | static int |
8568 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8569 | { | |
9cdf7b1f | 8570 | zio_t *pio = arg; |
34dc7c2f | 8571 | |
9cdf7b1f MA |
8572 | zio_nowait(zio_free_sync(pio, pio->io_spa, dmu_tx_get_txg(tx), bp, |
8573 | pio->io_flags)); | |
428870ff | 8574 | return (0); |
34dc7c2f BB |
8575 | } |
8576 | ||
37f03da8 SH |
8577 | static int |
8578 | bpobj_spa_free_sync_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
8579 | dmu_tx_t *tx) | |
8580 | { | |
8581 | ASSERT(!bp_freed); | |
8582 | return (spa_free_sync_cb(arg, bp, tx)); | |
8583 | } | |
8584 | ||
e8b96c60 MA |
8585 | /* |
8586 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8587 | * amount of time spent syncing frees. | |
8588 | */ | |
8589 | static void | |
8590 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
8591 | { | |
8592 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
8593 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
8594 | VERIFY(zio_wait(zio) == 0); | |
8595 | } | |
8596 | ||
8597 | /* | |
8598 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8599 | * amount of time spent syncing deferred frees. | |
8600 | */ | |
8601 | static void | |
8602 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
8603 | { | |
8dc2197b SD |
8604 | if (spa_sync_pass(spa) != 1) |
8605 | return; | |
8606 | ||
93e28d66 SD |
8607 | /* |
8608 | * Note: | |
8609 | * If the log space map feature is active, we stop deferring | |
8610 | * frees to the next TXG and therefore running this function | |
8611 | * would be considered a no-op as spa_deferred_bpobj should | |
8612 | * not have any entries. | |
8613 | * | |
8614 | * That said we run this function anyway (instead of returning | |
8615 | * immediately) for the edge-case scenario where we just | |
8616 | * activated the log space map feature in this TXG but we have | |
8617 | * deferred frees from the previous TXG. | |
8618 | */ | |
e8b96c60 MA |
8619 | zio_t *zio = zio_root(spa, NULL, NULL, 0); |
8620 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
37f03da8 | 8621 | bpobj_spa_free_sync_cb, zio, tx), ==, 0); |
e8b96c60 MA |
8622 | VERIFY0(zio_wait(zio)); |
8623 | } | |
8624 | ||
34dc7c2f BB |
8625 | static void |
8626 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
8627 | { | |
8628 | char *packed = NULL; | |
b128c09f | 8629 | size_t bufsize; |
34dc7c2f BB |
8630 | size_t nvsize = 0; |
8631 | dmu_buf_t *db; | |
8632 | ||
8633 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
8634 | ||
b128c09f BB |
8635 | /* |
8636 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
b0bc7a84 | 8637 | * information. This avoids the dmu_buf_will_dirty() path and |
b128c09f BB |
8638 | * saves us a pre-read to get data we don't actually care about. |
8639 | */ | |
9ae529ec | 8640 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
79c76d5b | 8641 | packed = vmem_alloc(bufsize, KM_SLEEP); |
34dc7c2f BB |
8642 | |
8643 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
79c76d5b | 8644 | KM_SLEEP) == 0); |
861166b0 | 8645 | memset(packed + nvsize, 0, bufsize - nvsize); |
34dc7c2f | 8646 | |
b128c09f | 8647 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 8648 | |
00b46022 | 8649 | vmem_free(packed, bufsize); |
34dc7c2f BB |
8650 | |
8651 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
8652 | dmu_buf_will_dirty(db, tx); | |
8653 | *(uint64_t *)db->db_data = nvsize; | |
8654 | dmu_buf_rele(db, FTAG); | |
8655 | } | |
8656 | ||
8657 | static void | |
8658 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
8659 | const char *config, const char *entry) | |
8660 | { | |
8661 | nvlist_t *nvroot; | |
8662 | nvlist_t **list; | |
8663 | int i; | |
8664 | ||
8665 | if (!sav->sav_sync) | |
8666 | return; | |
8667 | ||
8668 | /* | |
8669 | * Update the MOS nvlist describing the list of available devices. | |
8670 | * spa_validate_aux() will have already made sure this nvlist is | |
8671 | * valid and the vdevs are labeled appropriately. | |
8672 | */ | |
8673 | if (sav->sav_object == 0) { | |
8674 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
8675 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
8676 | sizeof (uint64_t), tx); | |
8677 | VERIFY(zap_update(spa->spa_meta_objset, | |
8678 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
8679 | &sav->sav_object, tx) == 0); | |
8680 | } | |
8681 | ||
65ad5d11 | 8682 | nvroot = fnvlist_alloc(); |
34dc7c2f | 8683 | if (sav->sav_count == 0) { |
795075e6 PD |
8684 | fnvlist_add_nvlist_array(nvroot, config, |
8685 | (const nvlist_t * const *)NULL, 0); | |
34dc7c2f | 8686 | } else { |
79c76d5b | 8687 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); |
34dc7c2f BB |
8688 | for (i = 0; i < sav->sav_count; i++) |
8689 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 8690 | B_FALSE, VDEV_CONFIG_L2CACHE); |
795075e6 PD |
8691 | fnvlist_add_nvlist_array(nvroot, config, |
8692 | (const nvlist_t * const *)list, sav->sav_count); | |
34dc7c2f BB |
8693 | for (i = 0; i < sav->sav_count; i++) |
8694 | nvlist_free(list[i]); | |
8695 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
8696 | } | |
8697 | ||
8698 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
8699 | nvlist_free(nvroot); | |
8700 | ||
8701 | sav->sav_sync = B_FALSE; | |
8702 | } | |
8703 | ||
e0ab3ab5 JS |
8704 | /* |
8705 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
8706 | * The all-vdev ZAP must be empty. | |
8707 | */ | |
8708 | static void | |
8709 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
8710 | { | |
8711 | spa_t *spa = vd->vdev_spa; | |
e0ab3ab5 | 8712 | |
3e4ed421 RW |
8713 | if (vd->vdev_root_zap != 0 && |
8714 | spa_feature_is_active(spa, SPA_FEATURE_AVZ_V2)) { | |
8715 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8716 | vd->vdev_root_zap, tx)); | |
8717 | } | |
e0ab3ab5 JS |
8718 | if (vd->vdev_top_zap != 0) { |
8719 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8720 | vd->vdev_top_zap, tx)); | |
8721 | } | |
8722 | if (vd->vdev_leaf_zap != 0) { | |
8723 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8724 | vd->vdev_leaf_zap, tx)); | |
8725 | } | |
1c27024e | 8726 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
8727 | spa_avz_build(vd->vdev_child[i], avz, tx); |
8728 | } | |
8729 | } | |
8730 | ||
34dc7c2f BB |
8731 | static void |
8732 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
8733 | { | |
8734 | nvlist_t *config; | |
8735 | ||
e0ab3ab5 JS |
8736 | /* |
8737 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
8738 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
8739 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
8740 | * need to rebuild the AVZ although the config may not be dirty. | |
8741 | */ | |
8742 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
8743 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
34dc7c2f BB |
8744 | return; |
8745 | ||
b128c09f BB |
8746 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
8747 | ||
e0ab3ab5 | 8748 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || |
38640550 | 8749 | spa->spa_avz_action == AVZ_ACTION_INITIALIZE || |
e0ab3ab5 JS |
8750 | spa->spa_all_vdev_zaps != 0); |
8751 | ||
8752 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
e0ab3ab5 JS |
8753 | /* Make and build the new AVZ */ |
8754 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
8755 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
8756 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
8757 | ||
8758 | /* Diff old AVZ with new one */ | |
1c27024e DB |
8759 | zap_cursor_t zc; |
8760 | zap_attribute_t za; | |
8761 | ||
e0ab3ab5 JS |
8762 | for (zap_cursor_init(&zc, spa->spa_meta_objset, |
8763 | spa->spa_all_vdev_zaps); | |
8764 | zap_cursor_retrieve(&zc, &za) == 0; | |
8765 | zap_cursor_advance(&zc)) { | |
8766 | uint64_t vdzap = za.za_first_integer; | |
8767 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
8768 | vdzap) == ENOENT) { | |
8769 | /* | |
8770 | * ZAP is listed in old AVZ but not in new one; | |
8771 | * destroy it | |
8772 | */ | |
8773 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
8774 | tx)); | |
8775 | } | |
8776 | } | |
8777 | ||
8778 | zap_cursor_fini(&zc); | |
8779 | ||
8780 | /* Destroy the old AVZ */ | |
8781 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8782 | spa->spa_all_vdev_zaps, tx)); | |
8783 | ||
8784 | /* Replace the old AVZ in the dir obj with the new one */ | |
8785 | VERIFY0(zap_update(spa->spa_meta_objset, | |
8786 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
8787 | sizeof (new_avz), 1, &new_avz, tx)); | |
8788 | ||
8789 | spa->spa_all_vdev_zaps = new_avz; | |
8790 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
8791 | zap_cursor_t zc; | |
8792 | zap_attribute_t za; | |
8793 | ||
8794 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
8795 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
8796 | spa->spa_all_vdev_zaps); | |
8797 | zap_cursor_retrieve(&zc, &za) == 0; | |
8798 | zap_cursor_advance(&zc)) { | |
8799 | uint64_t zap = za.za_first_integer; | |
8800 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
8801 | } | |
8802 | ||
8803 | zap_cursor_fini(&zc); | |
8804 | ||
8805 | /* Destroy and unlink the AVZ itself */ | |
8806 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8807 | spa->spa_all_vdev_zaps, tx)); | |
8808 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
8809 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
8810 | spa->spa_all_vdev_zaps = 0; | |
8811 | } | |
8812 | ||
8813 | if (spa->spa_all_vdev_zaps == 0) { | |
8814 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
8815 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
8816 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
8817 | } | |
8818 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
8819 | ||
8820 | /* Create ZAPs for vdevs that don't have them. */ | |
8821 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
8822 | ||
b128c09f BB |
8823 | config = spa_config_generate(spa, spa->spa_root_vdev, |
8824 | dmu_tx_get_txg(tx), B_FALSE); | |
8825 | ||
ea0b2538 GW |
8826 | /* |
8827 | * If we're upgrading the spa version then make sure that | |
8828 | * the config object gets updated with the correct version. | |
8829 | */ | |
8830 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
8831 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
8832 | spa->spa_uberblock.ub_version); | |
8833 | ||
b128c09f | 8834 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f | 8835 | |
8a5fc748 | 8836 | nvlist_free(spa->spa_config_syncing); |
34dc7c2f BB |
8837 | spa->spa_config_syncing = config; |
8838 | ||
8839 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
8840 | } | |
8841 | ||
9ae529ec | 8842 | static void |
13fe0198 | 8843 | spa_sync_version(void *arg, dmu_tx_t *tx) |
9ae529ec | 8844 | { |
13fe0198 MA |
8845 | uint64_t *versionp = arg; |
8846 | uint64_t version = *versionp; | |
8847 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
9ae529ec CS |
8848 | |
8849 | /* | |
8850 | * Setting the version is special cased when first creating the pool. | |
8851 | */ | |
8852 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
8853 | ||
8dca0a9a | 8854 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
8855 | ASSERT(version >= spa_version(spa)); |
8856 | ||
8857 | spa->spa_uberblock.ub_version = version; | |
8858 | vdev_config_dirty(spa->spa_root_vdev); | |
74756182 MM |
8859 | spa_history_log_internal(spa, "set", tx, "version=%lld", |
8860 | (longlong_t)version); | |
9ae529ec CS |
8861 | } |
8862 | ||
34dc7c2f BB |
8863 | /* |
8864 | * Set zpool properties. | |
8865 | */ | |
8866 | static void | |
13fe0198 | 8867 | spa_sync_props(void *arg, dmu_tx_t *tx) |
34dc7c2f | 8868 | { |
13fe0198 MA |
8869 | nvlist_t *nvp = arg; |
8870 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f | 8871 | objset_t *mos = spa->spa_meta_objset; |
9ae529ec | 8872 | nvpair_t *elem = NULL; |
b128c09f BB |
8873 | |
8874 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 8875 | |
34dc7c2f | 8876 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec | 8877 | uint64_t intval; |
d1807f16 | 8878 | const char *strval, *fname; |
9ae529ec CS |
8879 | zpool_prop_t prop; |
8880 | const char *propname; | |
8eae2d21 | 8881 | const char *elemname = nvpair_name(elem); |
9ae529ec | 8882 | zprop_type_t proptype; |
fa86b5db | 8883 | spa_feature_t fid; |
9ae529ec | 8884 | |
8eae2d21 | 8885 | switch (prop = zpool_name_to_prop(elemname)) { |
34dc7c2f | 8886 | case ZPOOL_PROP_VERSION: |
93cf2076 | 8887 | intval = fnvpair_value_uint64(elem); |
34dc7c2f | 8888 | /* |
4e33ba4c | 8889 | * The version is synced separately before other |
9ae529ec | 8890 | * properties and should be correct by now. |
34dc7c2f | 8891 | */ |
9ae529ec | 8892 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
8893 | break; |
8894 | ||
8895 | case ZPOOL_PROP_ALTROOT: | |
8896 | /* | |
8897 | * 'altroot' is a non-persistent property. It should | |
8898 | * have been set temporarily at creation or import time. | |
8899 | */ | |
8900 | ASSERT(spa->spa_root != NULL); | |
8901 | break; | |
8902 | ||
572e2857 | 8903 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
8904 | case ZPOOL_PROP_CACHEFILE: |
8905 | /* | |
e1cfd73f | 8906 | * 'readonly' and 'cachefile' are also non-persistent |
572e2857 | 8907 | * properties. |
34dc7c2f | 8908 | */ |
34dc7c2f | 8909 | break; |
d96eb2b1 | 8910 | case ZPOOL_PROP_COMMENT: |
93cf2076 | 8911 | strval = fnvpair_value_string(elem); |
d96eb2b1 DM |
8912 | if (spa->spa_comment != NULL) |
8913 | spa_strfree(spa->spa_comment); | |
8914 | spa->spa_comment = spa_strdup(strval); | |
8915 | /* | |
8916 | * We need to dirty the configuration on all the vdevs | |
88a48330 BB |
8917 | * so that their labels get updated. We also need to |
8918 | * update the cache file to keep it in sync with the | |
8919 | * MOS version. It's unnecessary to do this for pool | |
8920 | * creation since the vdev's configuration has already | |
8921 | * been dirtied. | |
d96eb2b1 | 8922 | */ |
88a48330 | 8923 | if (tx->tx_txg != TXG_INITIAL) { |
d96eb2b1 | 8924 | vdev_config_dirty(spa->spa_root_vdev); |
88a48330 BB |
8925 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); |
8926 | } | |
6f1ffb06 | 8927 | spa_history_log_internal(spa, "set", tx, |
8eae2d21 | 8928 | "%s=%s", elemname, strval); |
d96eb2b1 | 8929 | break; |
658fb802 CB |
8930 | case ZPOOL_PROP_COMPATIBILITY: |
8931 | strval = fnvpair_value_string(elem); | |
8932 | if (spa->spa_compatibility != NULL) | |
8933 | spa_strfree(spa->spa_compatibility); | |
8934 | spa->spa_compatibility = spa_strdup(strval); | |
8935 | /* | |
8936 | * Dirty the configuration on vdevs as above. | |
8937 | */ | |
88a48330 | 8938 | if (tx->tx_txg != TXG_INITIAL) { |
658fb802 | 8939 | vdev_config_dirty(spa->spa_root_vdev); |
88a48330 BB |
8940 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); |
8941 | } | |
8942 | ||
658fb802 CB |
8943 | spa_history_log_internal(spa, "set", tx, |
8944 | "%s=%s", nvpair_name(elem), strval); | |
8945 | break; | |
8946 | ||
8eae2d21 AJ |
8947 | case ZPOOL_PROP_INVAL: |
8948 | if (zpool_prop_feature(elemname)) { | |
8949 | fname = strchr(elemname, '@') + 1; | |
8950 | VERIFY0(zfeature_lookup_name(fname, &fid)); | |
8951 | ||
8952 | spa_feature_enable(spa, fid, tx); | |
8953 | spa_history_log_internal(spa, "set", tx, | |
8954 | "%s=enabled", elemname); | |
8955 | break; | |
8956 | } else if (!zfs_prop_user(elemname)) { | |
8957 | ASSERT(zpool_prop_feature(elemname)); | |
8958 | break; | |
8959 | } | |
8960 | zfs_fallthrough; | |
34dc7c2f BB |
8961 | default: |
8962 | /* | |
8963 | * Set pool property values in the poolprops mos object. | |
8964 | */ | |
34dc7c2f | 8965 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
8966 | spa->spa_pool_props_object = |
8967 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 8968 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 8969 | tx); |
34dc7c2f | 8970 | } |
34dc7c2f BB |
8971 | |
8972 | /* normalize the property name */ | |
ee7b71db | 8973 | if (prop == ZPOOL_PROP_INVAL) { |
8eae2d21 AJ |
8974 | propname = elemname; |
8975 | proptype = PROP_TYPE_STRING; | |
ee7b71db RY |
8976 | } else { |
8977 | propname = zpool_prop_to_name(prop); | |
8978 | proptype = zpool_prop_get_type(prop); | |
8eae2d21 | 8979 | } |
34dc7c2f BB |
8980 | |
8981 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
8982 | ASSERT(proptype == PROP_TYPE_STRING); | |
93cf2076 GW |
8983 | strval = fnvpair_value_string(elem); |
8984 | VERIFY0(zap_update(mos, | |
34dc7c2f | 8985 | spa->spa_pool_props_object, propname, |
93cf2076 | 8986 | 1, strlen(strval) + 1, strval, tx)); |
6f1ffb06 | 8987 | spa_history_log_internal(spa, "set", tx, |
8eae2d21 | 8988 | "%s=%s", elemname, strval); |
34dc7c2f | 8989 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { |
93cf2076 | 8990 | intval = fnvpair_value_uint64(elem); |
34dc7c2f BB |
8991 | |
8992 | if (proptype == PROP_TYPE_INDEX) { | |
8993 | const char *unused; | |
93cf2076 GW |
8994 | VERIFY0(zpool_prop_index_to_string( |
8995 | prop, intval, &unused)); | |
34dc7c2f | 8996 | } |
93cf2076 | 8997 | VERIFY0(zap_update(mos, |
34dc7c2f | 8998 | spa->spa_pool_props_object, propname, |
93cf2076 | 8999 | 8, 1, &intval, tx)); |
6f1ffb06 | 9000 | spa_history_log_internal(spa, "set", tx, |
8eae2d21 | 9001 | "%s=%lld", elemname, |
74756182 | 9002 | (longlong_t)intval); |
34dc7c2f | 9003 | |
44f71818 RY |
9004 | switch (prop) { |
9005 | case ZPOOL_PROP_DELEGATION: | |
9006 | spa->spa_delegation = intval; | |
9007 | break; | |
9008 | case ZPOOL_PROP_BOOTFS: | |
9009 | spa->spa_bootfs = intval; | |
9010 | break; | |
9011 | case ZPOOL_PROP_FAILUREMODE: | |
9012 | spa->spa_failmode = intval; | |
9013 | break; | |
9014 | case ZPOOL_PROP_AUTOTRIM: | |
9015 | spa->spa_autotrim = intval; | |
428870ff | 9016 | spa_async_request(spa, |
44f71818 RY |
9017 | SPA_ASYNC_AUTOTRIM_RESTART); |
9018 | break; | |
9019 | case ZPOOL_PROP_AUTOEXPAND: | |
9020 | spa->spa_autoexpand = intval; | |
9021 | if (tx->tx_txg != TXG_INITIAL) | |
9022 | spa_async_request(spa, | |
9023 | SPA_ASYNC_AUTOEXPAND); | |
9024 | break; | |
9025 | case ZPOOL_PROP_MULTIHOST: | |
9026 | spa->spa_multihost = intval; | |
9027 | break; | |
9028 | default: | |
9029 | break; | |
9030 | } | |
9031 | } else { | |
9032 | ASSERT(0); /* not allowed */ | |
34dc7c2f BB |
9033 | } |
9034 | } | |
9035 | ||
34dc7c2f | 9036 | } |
b128c09f BB |
9037 | |
9038 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
9039 | } |
9040 | ||
428870ff BB |
9041 | /* |
9042 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
9043 | * reflect the new version this txg, so there must be no changes this | |
9044 | * txg to anything that the upgrade code depends on after it executes. | |
9045 | * Therefore this must be called after dsl_pool_sync() does the sync | |
9046 | * tasks. | |
9047 | */ | |
9048 | static void | |
9049 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
9050 | { | |
8dc2197b SD |
9051 | if (spa_sync_pass(spa) != 1) |
9052 | return; | |
428870ff | 9053 | |
8dc2197b | 9054 | dsl_pool_t *dp = spa->spa_dsl_pool; |
13fe0198 MA |
9055 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
9056 | ||
428870ff BB |
9057 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && |
9058 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
9059 | dsl_pool_create_origin(dp, tx); | |
9060 | ||
9061 | /* Keeping the origin open increases spa_minref */ | |
9062 | spa->spa_minref += 3; | |
9063 | } | |
9064 | ||
9065 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
9066 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
9067 | dsl_pool_upgrade_clones(dp, tx); | |
9068 | } | |
9069 | ||
9070 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
9071 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
9072 | dsl_pool_upgrade_dir_clones(dp, tx); | |
9073 | ||
9074 | /* Keeping the freedir open increases spa_minref */ | |
9075 | spa->spa_minref += 3; | |
9076 | } | |
9ae529ec CS |
9077 | |
9078 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
9079 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
9080 | spa_feature_create_zap_objects(spa, tx); | |
9081 | } | |
62bdd5eb DL |
9082 | |
9083 | /* | |
9084 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
9085 | * when possibility to use lz4 compression for metadata was added | |
9086 | * Old pools that have this feature enabled must be upgraded to have | |
9087 | * this feature active | |
9088 | */ | |
9089 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
9090 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
9091 | SPA_FEATURE_LZ4_COMPRESS); | |
9092 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
9093 | SPA_FEATURE_LZ4_COMPRESS); | |
9094 | ||
9095 | if (lz4_en && !lz4_ac) | |
9096 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
9097 | } | |
3c67d83a TH |
9098 | |
9099 | /* | |
9100 | * If we haven't written the salt, do so now. Note that the | |
9101 | * feature may not be activated yet, but that's fine since | |
9102 | * the presence of this ZAP entry is backwards compatible. | |
9103 | */ | |
9104 | if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
9105 | DMU_POOL_CHECKSUM_SALT) == ENOENT) { | |
9106 | VERIFY0(zap_add(spa->spa_meta_objset, | |
9107 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, | |
9108 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
9109 | spa->spa_cksum_salt.zcs_bytes, tx)); | |
9110 | } | |
9111 | ||
13fe0198 | 9112 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
428870ff BB |
9113 | } |
9114 | ||
a1d477c2 MA |
9115 | static void |
9116 | vdev_indirect_state_sync_verify(vdev_t *vd) | |
9117 | { | |
2a8ba608 MM |
9118 | vdev_indirect_mapping_t *vim __maybe_unused = vd->vdev_indirect_mapping; |
9119 | vdev_indirect_births_t *vib __maybe_unused = vd->vdev_indirect_births; | |
a1d477c2 MA |
9120 | |
9121 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
9122 | ASSERT(vim != NULL); | |
9123 | ASSERT(vib != NULL); | |
9124 | } | |
9125 | ||
27f80e85 BB |
9126 | uint64_t obsolete_sm_object = 0; |
9127 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
9128 | if (obsolete_sm_object != 0) { | |
a1d477c2 MA |
9129 | ASSERT(vd->vdev_obsolete_sm != NULL); |
9130 | ASSERT(vd->vdev_removing || | |
9131 | vd->vdev_ops == &vdev_indirect_ops); | |
9132 | ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); | |
9133 | ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); | |
27f80e85 | 9134 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
9135 | space_map_object(vd->vdev_obsolete_sm)); |
9136 | ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, | |
9137 | space_map_allocated(vd->vdev_obsolete_sm)); | |
9138 | } | |
9139 | ASSERT(vd->vdev_obsolete_segments != NULL); | |
9140 | ||
9141 | /* | |
9142 | * Since frees / remaps to an indirect vdev can only | |
9143 | * happen in syncing context, the obsolete segments | |
9144 | * tree must be empty when we start syncing. | |
9145 | */ | |
9146 | ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); | |
9147 | } | |
9148 | ||
34dc7c2f | 9149 | /* |
8dc2197b SD |
9150 | * Set the top-level vdev's max queue depth. Evaluate each top-level's |
9151 | * async write queue depth in case it changed. The max queue depth will | |
9152 | * not change in the middle of syncing out this txg. | |
34dc7c2f | 9153 | */ |
8dc2197b SD |
9154 | static void |
9155 | spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) | |
34dc7c2f | 9156 | { |
8dc2197b SD |
9157 | ASSERT(spa_writeable(spa)); |
9158 | ||
34dc7c2f | 9159 | vdev_t *rvd = spa->spa_root_vdev; |
3dfb57a3 DB |
9160 | uint32_t max_queue_depth = zfs_vdev_async_write_max_active * |
9161 | zfs_vdev_queue_depth_pct / 100; | |
8dc2197b SD |
9162 | metaslab_class_t *normal = spa_normal_class(spa); |
9163 | metaslab_class_t *special = spa_special_class(spa); | |
9164 | metaslab_class_t *dedup = spa_dedup_class(spa); | |
34dc7c2f | 9165 | |
492f64e9 | 9166 | uint64_t slots_per_allocator = 0; |
1c27024e | 9167 | for (int c = 0; c < rvd->vdev_children; c++) { |
3dfb57a3 | 9168 | vdev_t *tvd = rvd->vdev_child[c]; |
cc99f275 | 9169 | |
8dc2197b | 9170 | metaslab_group_t *mg = tvd->vdev_mg; |
cc99f275 DB |
9171 | if (mg == NULL || !metaslab_group_initialized(mg)) |
9172 | continue; | |
3dfb57a3 | 9173 | |
8dc2197b | 9174 | metaslab_class_t *mc = mg->mg_class; |
cc99f275 | 9175 | if (mc != normal && mc != special && mc != dedup) |
3dfb57a3 DB |
9176 | continue; |
9177 | ||
9178 | /* | |
9179 | * It is safe to do a lock-free check here because only async | |
9180 | * allocations look at mg_max_alloc_queue_depth, and async | |
9181 | * allocations all happen from spa_sync(). | |
9182 | */ | |
32d805c3 | 9183 | for (int i = 0; i < mg->mg_allocators; i++) { |
424fd7c3 | 9184 | ASSERT0(zfs_refcount_count( |
32d805c3 MA |
9185 | &(mg->mg_allocator[i].mga_alloc_queue_depth))); |
9186 | } | |
3dfb57a3 | 9187 | mg->mg_max_alloc_queue_depth = max_queue_depth; |
492f64e9 | 9188 | |
32d805c3 MA |
9189 | for (int i = 0; i < mg->mg_allocators; i++) { |
9190 | mg->mg_allocator[i].mga_cur_max_alloc_queue_depth = | |
492f64e9 PD |
9191 | zfs_vdev_def_queue_depth; |
9192 | } | |
9193 | slots_per_allocator += zfs_vdev_def_queue_depth; | |
3dfb57a3 | 9194 | } |
cc99f275 | 9195 | |
492f64e9 | 9196 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
f8020c93 AM |
9197 | ASSERT0(zfs_refcount_count(&normal->mc_allocator[i]. |
9198 | mca_alloc_slots)); | |
9199 | ASSERT0(zfs_refcount_count(&special->mc_allocator[i]. | |
9200 | mca_alloc_slots)); | |
9201 | ASSERT0(zfs_refcount_count(&dedup->mc_allocator[i]. | |
9202 | mca_alloc_slots)); | |
9203 | normal->mc_allocator[i].mca_alloc_max_slots = | |
9204 | slots_per_allocator; | |
9205 | special->mc_allocator[i].mca_alloc_max_slots = | |
9206 | slots_per_allocator; | |
9207 | dedup->mc_allocator[i].mca_alloc_max_slots = | |
9208 | slots_per_allocator; | |
cc99f275 DB |
9209 | } |
9210 | normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
9211 | special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
9212 | dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8dc2197b SD |
9213 | } |
9214 | ||
9215 | static void | |
9216 | spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) | |
9217 | { | |
9218 | ASSERT(spa_writeable(spa)); | |
3dfb57a3 | 9219 | |
8dc2197b | 9220 | vdev_t *rvd = spa->spa_root_vdev; |
a1d477c2 MA |
9221 | for (int c = 0; c < rvd->vdev_children; c++) { |
9222 | vdev_t *vd = rvd->vdev_child[c]; | |
9223 | vdev_indirect_state_sync_verify(vd); | |
9224 | ||
9225 | if (vdev_indirect_should_condense(vd)) { | |
9226 | spa_condense_indirect_start_sync(vd, tx); | |
9227 | break; | |
9228 | } | |
9229 | } | |
8dc2197b SD |
9230 | } |
9231 | ||
9232 | static void | |
9233 | spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) | |
9234 | { | |
9235 | objset_t *mos = spa->spa_meta_objset; | |
9236 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
9237 | uint64_t txg = tx->tx_txg; | |
9238 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
a1d477c2 | 9239 | |
34dc7c2f | 9240 | do { |
428870ff | 9241 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
9242 | |
9243 | spa_sync_config_object(spa, tx); | |
9244 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
9245 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
9246 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
9247 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
9248 | spa_errlog_sync(spa, txg); | |
9249 | dsl_pool_sync(dp, txg); | |
9250 | ||
93e28d66 SD |
9251 | if (pass < zfs_sync_pass_deferred_free || |
9252 | spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) { | |
9253 | /* | |
9254 | * If the log space map feature is active we don't | |
9255 | * care about deferred frees and the deferred bpobj | |
9256 | * as the log space map should effectively have the | |
9257 | * same results (i.e. appending only to one object). | |
9258 | */ | |
e8b96c60 | 9259 | spa_sync_frees(spa, free_bpl, tx); |
428870ff | 9260 | } else { |
905edb40 MA |
9261 | /* |
9262 | * We can not defer frees in pass 1, because | |
9263 | * we sync the deferred frees later in pass 1. | |
9264 | */ | |
9265 | ASSERT3U(pass, >, 1); | |
37f03da8 | 9266 | bplist_iterate(free_bpl, bpobj_enqueue_alloc_cb, |
e8b96c60 | 9267 | &spa->spa_deferred_bpobj, tx); |
34dc7c2f BB |
9268 | } |
9269 | ||
67a1b037 | 9270 | brt_sync(spa, txg); |
428870ff BB |
9271 | ddt_sync(spa, txg); |
9272 | dsl_scan_sync(dp, tx); | |
482eeef8 | 9273 | dsl_errorscrub_sync(dp, tx); |
8dc2197b SD |
9274 | svr_sync(spa, tx); |
9275 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 9276 | |
93e28d66 SD |
9277 | spa_flush_metaslabs(spa, tx); |
9278 | ||
8dc2197b | 9279 | vdev_t *vd = NULL; |
a1d477c2 MA |
9280 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) |
9281 | != NULL) | |
428870ff BB |
9282 | vdev_sync(vd, txg); |
9283 | ||
8dc2197b SD |
9284 | /* |
9285 | * Note: We need to check if the MOS is dirty because we could | |
9286 | * have marked the MOS dirty without updating the uberblock | |
9287 | * (e.g. if we have sync tasks but no dirty user data). We need | |
9288 | * to check the uberblock's rootbp because it is updated if we | |
9289 | * have synced out dirty data (though in this case the MOS will | |
9290 | * most likely also be dirty due to second order effects, we | |
9291 | * don't want to rely on that here). | |
9292 | */ | |
9293 | if (pass == 1 && | |
9294 | spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
9295 | !dmu_objset_is_dirty(mos, txg)) { | |
905edb40 | 9296 | /* |
8dc2197b SD |
9297 | * Nothing changed on the first pass, therefore this |
9298 | * TXG is a no-op. Avoid syncing deferred frees, so | |
9299 | * that we can keep this TXG as a no-op. | |
905edb40 | 9300 | */ |
8dc2197b SD |
9301 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); |
9302 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9303 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
9304 | ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); | |
9305 | break; | |
905edb40 | 9306 | } |
34dc7c2f | 9307 | |
8dc2197b | 9308 | spa_sync_deferred_frees(spa, tx); |
428870ff | 9309 | } while (dmu_objset_is_dirty(mos, txg)); |
8dc2197b | 9310 | } |
34dc7c2f | 9311 | |
8dc2197b SD |
9312 | /* |
9313 | * Rewrite the vdev configuration (which includes the uberblock) to | |
9314 | * commit the transaction group. | |
9315 | * | |
9316 | * If there are no dirty vdevs, we sync the uberblock to a few random | |
9317 | * top-level vdevs that are known to be visible in the config cache | |
9318 | * (see spa_vdev_add() for a complete description). If there *are* dirty | |
9319 | * vdevs, sync the uberblock to all vdevs. | |
9320 | */ | |
9321 | static void | |
9322 | spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) | |
9323 | { | |
9324 | vdev_t *rvd = spa->spa_root_vdev; | |
9325 | uint64_t txg = tx->tx_txg; | |
a1d477c2 | 9326 | |
b128c09f | 9327 | for (;;) { |
8dc2197b SD |
9328 | int error = 0; |
9329 | ||
b128c09f BB |
9330 | /* |
9331 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
9332 | * while we're attempting to write the vdev labels. | |
9333 | */ | |
9334 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
9335 | ||
9336 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
d2734cce | 9337 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; |
b128c09f BB |
9338 | int svdcount = 0; |
9339 | int children = rvd->vdev_children; | |
29274c9f | 9340 | int c0 = random_in_range(children); |
b128c09f | 9341 | |
1c27024e | 9342 | for (int c = 0; c < children; c++) { |
8dc2197b SD |
9343 | vdev_t *vd = |
9344 | rvd->vdev_child[(c0 + c) % children]; | |
d2734cce SD |
9345 | |
9346 | /* Stop when revisiting the first vdev */ | |
9347 | if (c > 0 && svd[0] == vd) | |
9348 | break; | |
9349 | ||
8dc2197b SD |
9350 | if (vd->vdev_ms_array == 0 || |
9351 | vd->vdev_islog || | |
a1d477c2 | 9352 | !vdev_is_concrete(vd)) |
b128c09f | 9353 | continue; |
d2734cce | 9354 | |
b128c09f | 9355 | svd[svdcount++] = vd; |
6cb8e530 | 9356 | if (svdcount == SPA_SYNC_MIN_VDEVS) |
b128c09f BB |
9357 | break; |
9358 | } | |
b6fcb792 | 9359 | error = vdev_config_sync(svd, svdcount, txg); |
b128c09f BB |
9360 | } else { |
9361 | error = vdev_config_sync(rvd->vdev_child, | |
b6fcb792 | 9362 | rvd->vdev_children, txg); |
34dc7c2f | 9363 | } |
34dc7c2f | 9364 | |
3bc7e0fb GW |
9365 | if (error == 0) |
9366 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
9367 | ||
b128c09f BB |
9368 | spa_config_exit(spa, SCL_STATE, FTAG); |
9369 | ||
9370 | if (error == 0) | |
9371 | break; | |
cec3a0a1 | 9372 | zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); |
b128c09f BB |
9373 | zio_resume_wait(spa); |
9374 | } | |
8dc2197b SD |
9375 | } |
9376 | ||
9377 | /* | |
9378 | * Sync the specified transaction group. New blocks may be dirtied as | |
9379 | * part of the process, so we iterate until it converges. | |
9380 | */ | |
9381 | void | |
9382 | spa_sync(spa_t *spa, uint64_t txg) | |
9383 | { | |
9384 | vdev_t *vd = NULL; | |
9385 | ||
9386 | VERIFY(spa_writeable(spa)); | |
9387 | ||
9388 | /* | |
9389 | * Wait for i/os issued in open context that need to complete | |
9390 | * before this txg syncs. | |
9391 | */ | |
9392 | (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); | |
9393 | spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, | |
9394 | ZIO_FLAG_CANFAIL); | |
9395 | ||
67a1b037 PJD |
9396 | /* |
9397 | * Now that there can be no more cloning in this transaction group, | |
9398 | * but we are still before issuing frees, we can process pending BRT | |
9399 | * updates. | |
9400 | */ | |
9401 | brt_pending_apply(spa, txg); | |
9402 | ||
8dc2197b SD |
9403 | /* |
9404 | * Lock out configuration changes. | |
9405 | */ | |
9406 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
9407 | ||
9408 | spa->spa_syncing_txg = txg; | |
9409 | spa->spa_sync_pass = 0; | |
9410 | ||
9411 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
1b50749c AM |
9412 | mutex_enter(&spa->spa_allocs[i].spaa_lock); |
9413 | VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree)); | |
9414 | mutex_exit(&spa->spa_allocs[i].spaa_lock); | |
8dc2197b SD |
9415 | } |
9416 | ||
9417 | /* | |
9418 | * If there are any pending vdev state changes, convert them | |
9419 | * into config changes that go out with this transaction group. | |
9420 | */ | |
9421 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
9f08b6e3 RY |
9422 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { |
9423 | /* Avoid holding the write lock unless actually necessary */ | |
9424 | if (vd->vdev_aux == NULL) { | |
9425 | vdev_state_clean(vd); | |
9426 | vdev_config_dirty(vd); | |
9427 | continue; | |
9428 | } | |
8dc2197b SD |
9429 | /* |
9430 | * We need the write lock here because, for aux vdevs, | |
9431 | * calling vdev_config_dirty() modifies sav_config. | |
9432 | * This is ugly and will become unnecessary when we | |
9433 | * eliminate the aux vdev wart by integrating all vdevs | |
9434 | * into the root vdev tree. | |
9435 | */ | |
9436 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9437 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
9438 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
9439 | vdev_state_clean(vd); | |
9440 | vdev_config_dirty(vd); | |
9441 | } | |
9442 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9443 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9444 | } | |
9445 | spa_config_exit(spa, SCL_STATE, FTAG); | |
9446 | ||
9447 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
9448 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
9449 | ||
9450 | spa->spa_sync_starttime = gethrtime(); | |
9451 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); | |
9452 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, | |
9453 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
9454 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
9455 | ||
9456 | /* | |
9457 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
9458 | * set spa_deflate if we have no raid-z vdevs. | |
9459 | */ | |
9460 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
9461 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
9462 | vdev_t *rvd = spa->spa_root_vdev; | |
9463 | ||
9464 | int i; | |
9465 | for (i = 0; i < rvd->vdev_children; i++) { | |
9466 | vd = rvd->vdev_child[i]; | |
9467 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
9468 | break; | |
9469 | } | |
9470 | if (i == rvd->vdev_children) { | |
9471 | spa->spa_deflate = TRUE; | |
9472 | VERIFY0(zap_add(spa->spa_meta_objset, | |
9473 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
9474 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
9475 | } | |
9476 | } | |
9477 | ||
9478 | spa_sync_adjust_vdev_max_queue_depth(spa); | |
9479 | ||
9480 | spa_sync_condense_indirect(spa, tx); | |
9481 | ||
9482 | spa_sync_iterate_to_convergence(spa, tx); | |
9483 | ||
9484 | #ifdef ZFS_DEBUG | |
9485 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
9486 | /* | |
9487 | * Make sure that the number of ZAPs for all the vdevs matches | |
9488 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
9489 | * called if the config is dirty; otherwise there may be | |
9490 | * outstanding AVZ operations that weren't completed in | |
9491 | * spa_sync_config_object. | |
9492 | */ | |
9493 | uint64_t all_vdev_zap_entry_count; | |
9494 | ASSERT0(zap_count(spa->spa_meta_objset, | |
9495 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
9496 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
9497 | all_vdev_zap_entry_count); | |
9498 | } | |
9499 | #endif | |
9500 | ||
9501 | if (spa->spa_vdev_removal != NULL) { | |
9502 | ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); | |
9503 | } | |
9504 | ||
9505 | spa_sync_rewrite_vdev_config(spa, tx); | |
34dc7c2f BB |
9506 | dmu_tx_commit(tx); |
9507 | ||
57ddcda1 | 9508 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 GW |
9509 | spa->spa_deadman_tqid = 0; |
9510 | ||
34dc7c2f BB |
9511 | /* |
9512 | * Clear the dirty config list. | |
9513 | */ | |
b128c09f | 9514 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
9515 | vdev_config_clean(vd); |
9516 | ||
9517 | /* | |
9518 | * Now that the new config has synced transactionally, | |
9519 | * let it become visible to the config cache. | |
9520 | */ | |
9521 | if (spa->spa_config_syncing != NULL) { | |
9522 | spa_config_set(spa, spa->spa_config_syncing); | |
9523 | spa->spa_config_txg = txg; | |
9524 | spa->spa_config_syncing = NULL; | |
9525 | } | |
9526 | ||
428870ff | 9527 | dsl_pool_sync_done(dp, txg); |
34dc7c2f | 9528 | |
492f64e9 | 9529 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
1b50749c AM |
9530 | mutex_enter(&spa->spa_allocs[i].spaa_lock); |
9531 | VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree)); | |
9532 | mutex_exit(&spa->spa_allocs[i].spaa_lock); | |
492f64e9 | 9533 | } |
3dfb57a3 | 9534 | |
34dc7c2f BB |
9535 | /* |
9536 | * Update usable space statistics. | |
9537 | */ | |
619f0976 GW |
9538 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) |
9539 | != NULL) | |
34dc7c2f | 9540 | vdev_sync_done(vd, txg); |
f09fda50 PD |
9541 | |
9542 | metaslab_class_evict_old(spa->spa_normal_class, txg); | |
9543 | metaslab_class_evict_old(spa->spa_log_class, txg); | |
9544 | ||
93e28d66 | 9545 | spa_sync_close_syncing_log_sm(spa); |
34dc7c2f | 9546 | |
428870ff BB |
9547 | spa_update_dspace(spa); |
9548 | ||
65d10bd8 KJ |
9549 | if (spa_get_autotrim(spa) == SPA_AUTOTRIM_ON) |
9550 | vdev_autotrim_kick(spa); | |
9551 | ||
34dc7c2f BB |
9552 | /* |
9553 | * It had better be the case that we didn't dirty anything | |
9554 | * since vdev_config_sync(). | |
9555 | */ | |
9556 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
9557 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9558 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff | 9559 | |
d2734cce SD |
9560 | while (zfs_pause_spa_sync) |
9561 | delay(1); | |
9562 | ||
428870ff | 9563 | spa->spa_sync_pass = 0; |
34dc7c2f | 9564 | |
55922e73 GW |
9565 | /* |
9566 | * Update the last synced uberblock here. We want to do this at | |
9567 | * the end of spa_sync() so that consumers of spa_last_synced_txg() | |
9568 | * will be guaranteed that all the processing associated with | |
9569 | * that txg has been completed. | |
9570 | */ | |
9571 | spa->spa_ubsync = spa->spa_uberblock; | |
b128c09f | 9572 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 9573 | |
428870ff BB |
9574 | spa_handle_ignored_writes(spa); |
9575 | ||
34dc7c2f BB |
9576 | /* |
9577 | * If any async tasks have been requested, kick them off. | |
9578 | */ | |
9579 | spa_async_dispatch(spa); | |
9580 | } | |
9581 | ||
9582 | /* | |
9583 | * Sync all pools. We don't want to hold the namespace lock across these | |
9584 | * operations, so we take a reference on the spa_t and drop the lock during the | |
9585 | * sync. | |
9586 | */ | |
9587 | void | |
9588 | spa_sync_allpools(void) | |
9589 | { | |
9590 | spa_t *spa = NULL; | |
9591 | mutex_enter(&spa_namespace_lock); | |
9592 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
9593 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
9594 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
9595 | continue; |
9596 | spa_open_ref(spa, FTAG); | |
9597 | mutex_exit(&spa_namespace_lock); | |
9598 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9599 | mutex_enter(&spa_namespace_lock); | |
9600 | spa_close(spa, FTAG); | |
9601 | } | |
9602 | mutex_exit(&spa_namespace_lock); | |
9603 | } | |
9604 | ||
9605 | /* | |
9606 | * ========================================================================== | |
9607 | * Miscellaneous routines | |
9608 | * ========================================================================== | |
9609 | */ | |
9610 | ||
9611 | /* | |
9612 | * Remove all pools in the system. | |
9613 | */ | |
9614 | void | |
9615 | spa_evict_all(void) | |
9616 | { | |
9617 | spa_t *spa; | |
9618 | ||
9619 | /* | |
9620 | * Remove all cached state. All pools should be closed now, | |
9621 | * so every spa in the AVL tree should be unreferenced. | |
9622 | */ | |
9623 | mutex_enter(&spa_namespace_lock); | |
9624 | while ((spa = spa_next(NULL)) != NULL) { | |
9625 | /* | |
9626 | * Stop async tasks. The async thread may need to detach | |
9627 | * a device that's been replaced, which requires grabbing | |
9628 | * spa_namespace_lock, so we must drop it here. | |
9629 | */ | |
9630 | spa_open_ref(spa, FTAG); | |
9631 | mutex_exit(&spa_namespace_lock); | |
9632 | spa_async_suspend(spa); | |
9633 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
9634 | spa_close(spa, FTAG); |
9635 | ||
9636 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
9637 | spa_unload(spa); | |
9638 | spa_deactivate(spa); | |
9639 | } | |
9640 | spa_remove(spa); | |
9641 | } | |
9642 | mutex_exit(&spa_namespace_lock); | |
9643 | } | |
9644 | ||
9645 | vdev_t * | |
9babb374 | 9646 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 9647 | { |
b128c09f BB |
9648 | vdev_t *vd; |
9649 | int i; | |
9650 | ||
9651 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
9652 | return (vd); | |
9653 | ||
9babb374 | 9654 | if (aux) { |
b128c09f BB |
9655 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
9656 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
9657 | if (vd->vdev_guid == guid) |
9658 | return (vd); | |
9659 | } | |
9660 | ||
9661 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
9662 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
9663 | if (vd->vdev_guid == guid) |
9664 | return (vd); | |
9665 | } | |
9666 | } | |
9667 | ||
9668 | return (NULL); | |
34dc7c2f BB |
9669 | } |
9670 | ||
9671 | void | |
9672 | spa_upgrade(spa_t *spa, uint64_t version) | |
9673 | { | |
572e2857 BB |
9674 | ASSERT(spa_writeable(spa)); |
9675 | ||
b128c09f | 9676 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
9677 | |
9678 | /* | |
9679 | * This should only be called for a non-faulted pool, and since a | |
9680 | * future version would result in an unopenable pool, this shouldn't be | |
9681 | * possible. | |
9682 | */ | |
8dca0a9a | 9683 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
9b67f605 | 9684 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); |
34dc7c2f BB |
9685 | |
9686 | spa->spa_uberblock.ub_version = version; | |
9687 | vdev_config_dirty(spa->spa_root_vdev); | |
9688 | ||
b128c09f | 9689 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
9690 | |
9691 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9692 | } | |
9693 | ||
49d42425 FU |
9694 | static boolean_t |
9695 | spa_has_aux_vdev(spa_t *spa, uint64_t guid, spa_aux_vdev_t *sav) | |
34dc7c2f | 9696 | { |
14e4e3cb | 9697 | (void) spa; |
34dc7c2f | 9698 | int i; |
49d42425 | 9699 | uint64_t vdev_guid; |
34dc7c2f BB |
9700 | |
9701 | for (i = 0; i < sav->sav_count; i++) | |
9702 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
9703 | return (B_TRUE); | |
9704 | ||
9705 | for (i = 0; i < sav->sav_npending; i++) { | |
9706 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
49d42425 | 9707 | &vdev_guid) == 0 && vdev_guid == guid) |
34dc7c2f BB |
9708 | return (B_TRUE); |
9709 | } | |
9710 | ||
9711 | return (B_FALSE); | |
9712 | } | |
9713 | ||
49d42425 FU |
9714 | boolean_t |
9715 | spa_has_l2cache(spa_t *spa, uint64_t guid) | |
9716 | { | |
9717 | return (spa_has_aux_vdev(spa, guid, &spa->spa_l2cache)); | |
9718 | } | |
9719 | ||
9720 | boolean_t | |
9721 | spa_has_spare(spa_t *spa, uint64_t guid) | |
9722 | { | |
9723 | return (spa_has_aux_vdev(spa, guid, &spa->spa_spares)); | |
9724 | } | |
9725 | ||
b128c09f BB |
9726 | /* |
9727 | * Check if a pool has an active shared spare device. | |
9728 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
9729 | */ | |
9730 | static boolean_t | |
9731 | spa_has_active_shared_spare(spa_t *spa) | |
9732 | { | |
9733 | int i, refcnt; | |
9734 | uint64_t pool; | |
9735 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9736 | ||
9737 | for (i = 0; i < sav->sav_count; i++) { | |
9738 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
9739 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
9740 | refcnt > 2) | |
9741 | return (B_TRUE); | |
9742 | } | |
9743 | ||
9744 | return (B_FALSE); | |
9745 | } | |
9746 | ||
93e28d66 SD |
9747 | uint64_t |
9748 | spa_total_metaslabs(spa_t *spa) | |
9749 | { | |
9750 | vdev_t *rvd = spa->spa_root_vdev; | |
9751 | ||
9752 | uint64_t m = 0; | |
9753 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
9754 | vdev_t *vd = rvd->vdev_child[c]; | |
9755 | if (!vdev_is_concrete(vd)) | |
9756 | continue; | |
9757 | m += vd->vdev_ms_count; | |
9758 | } | |
9759 | return (m); | |
9760 | } | |
9761 | ||
e60e158e JG |
9762 | /* |
9763 | * Notify any waiting threads that some activity has switched from being in- | |
9764 | * progress to not-in-progress so that the thread can wake up and determine | |
9765 | * whether it is finished waiting. | |
9766 | */ | |
9767 | void | |
9768 | spa_notify_waiters(spa_t *spa) | |
9769 | { | |
9770 | /* | |
9771 | * Acquiring spa_activities_lock here prevents the cv_broadcast from | |
9772 | * happening between the waiting thread's check and cv_wait. | |
9773 | */ | |
9774 | mutex_enter(&spa->spa_activities_lock); | |
9775 | cv_broadcast(&spa->spa_activities_cv); | |
9776 | mutex_exit(&spa->spa_activities_lock); | |
9777 | } | |
9778 | ||
9779 | /* | |
9780 | * Notify any waiting threads that the pool is exporting, and then block until | |
9781 | * they are finished using the spa_t. | |
9782 | */ | |
9783 | void | |
9784 | spa_wake_waiters(spa_t *spa) | |
9785 | { | |
9786 | mutex_enter(&spa->spa_activities_lock); | |
9787 | spa->spa_waiters_cancel = B_TRUE; | |
9788 | cv_broadcast(&spa->spa_activities_cv); | |
9789 | while (spa->spa_waiters != 0) | |
9790 | cv_wait(&spa->spa_waiters_cv, &spa->spa_activities_lock); | |
9791 | spa->spa_waiters_cancel = B_FALSE; | |
9792 | mutex_exit(&spa->spa_activities_lock); | |
9793 | } | |
9794 | ||
2288d419 | 9795 | /* Whether the vdev or any of its descendants are being initialized/trimmed. */ |
e60e158e | 9796 | static boolean_t |
2288d419 | 9797 | spa_vdev_activity_in_progress_impl(vdev_t *vd, zpool_wait_activity_t activity) |
e60e158e JG |
9798 | { |
9799 | spa_t *spa = vd->vdev_spa; | |
e60e158e JG |
9800 | |
9801 | ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER)); | |
9802 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
2288d419 BB |
9803 | ASSERT(activity == ZPOOL_WAIT_INITIALIZE || |
9804 | activity == ZPOOL_WAIT_TRIM); | |
9805 | ||
9806 | kmutex_t *lock = activity == ZPOOL_WAIT_INITIALIZE ? | |
9807 | &vd->vdev_initialize_lock : &vd->vdev_trim_lock; | |
e60e158e JG |
9808 | |
9809 | mutex_exit(&spa->spa_activities_lock); | |
2288d419 | 9810 | mutex_enter(lock); |
e60e158e JG |
9811 | mutex_enter(&spa->spa_activities_lock); |
9812 | ||
2288d419 BB |
9813 | boolean_t in_progress = (activity == ZPOOL_WAIT_INITIALIZE) ? |
9814 | (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) : | |
9815 | (vd->vdev_trim_state == VDEV_TRIM_ACTIVE); | |
9816 | mutex_exit(lock); | |
e60e158e | 9817 | |
2288d419 | 9818 | if (in_progress) |
e60e158e JG |
9819 | return (B_TRUE); |
9820 | ||
9821 | for (int i = 0; i < vd->vdev_children; i++) { | |
2288d419 BB |
9822 | if (spa_vdev_activity_in_progress_impl(vd->vdev_child[i], |
9823 | activity)) | |
e60e158e JG |
9824 | return (B_TRUE); |
9825 | } | |
9826 | ||
9827 | return (B_FALSE); | |
9828 | } | |
9829 | ||
9830 | /* | |
9831 | * If use_guid is true, this checks whether the vdev specified by guid is | |
2288d419 BB |
9832 | * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool |
9833 | * is being initialized/trimmed. The caller must hold the config lock and | |
9834 | * spa_activities_lock. | |
e60e158e JG |
9835 | */ |
9836 | static int | |
2288d419 BB |
9837 | spa_vdev_activity_in_progress(spa_t *spa, boolean_t use_guid, uint64_t guid, |
9838 | zpool_wait_activity_t activity, boolean_t *in_progress) | |
e60e158e JG |
9839 | { |
9840 | mutex_exit(&spa->spa_activities_lock); | |
9841 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9842 | mutex_enter(&spa->spa_activities_lock); | |
9843 | ||
9844 | vdev_t *vd; | |
9845 | if (use_guid) { | |
9846 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
9847 | if (vd == NULL || !vd->vdev_ops->vdev_op_leaf) { | |
9848 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9849 | return (EINVAL); | |
9850 | } | |
9851 | } else { | |
9852 | vd = spa->spa_root_vdev; | |
9853 | } | |
9854 | ||
2288d419 | 9855 | *in_progress = spa_vdev_activity_in_progress_impl(vd, activity); |
e60e158e JG |
9856 | |
9857 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9858 | return (0); | |
9859 | } | |
9860 | ||
9861 | /* | |
9862 | * Locking for waiting threads | |
9863 | * --------------------------- | |
9864 | * | |
9865 | * Waiting threads need a way to check whether a given activity is in progress, | |
9866 | * and then, if it is, wait for it to complete. Each activity will have some | |
9867 | * in-memory representation of the relevant on-disk state which can be used to | |
9868 | * determine whether or not the activity is in progress. The in-memory state and | |
9869 | * the locking used to protect it will be different for each activity, and may | |
9870 | * not be suitable for use with a cvar (e.g., some state is protected by the | |
9871 | * config lock). To allow waiting threads to wait without any races, another | |
9872 | * lock, spa_activities_lock, is used. | |
9873 | * | |
9874 | * When the state is checked, both the activity-specific lock (if there is one) | |
9875 | * and spa_activities_lock are held. In some cases, the activity-specific lock | |
9876 | * is acquired explicitly (e.g. the config lock). In others, the locking is | |
9877 | * internal to some check (e.g. bpobj_is_empty). After checking, the waiting | |
9878 | * thread releases the activity-specific lock and, if the activity is in | |
9879 | * progress, then cv_waits using spa_activities_lock. | |
9880 | * | |
9881 | * The waiting thread is woken when another thread, one completing some | |
9882 | * activity, updates the state of the activity and then calls | |
9883 | * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only | |
9884 | * needs to hold its activity-specific lock when updating the state, and this | |
9885 | * lock can (but doesn't have to) be dropped before calling spa_notify_waiters. | |
9886 | * | |
9887 | * Because spa_notify_waiters acquires spa_activities_lock before broadcasting, | |
9888 | * and because it is held when the waiting thread checks the state of the | |
9889 | * activity, it can never be the case that the completing thread both updates | |
9890 | * the activity state and cv_broadcasts in between the waiting thread's check | |
9891 | * and cv_wait. Thus, a waiting thread can never miss a wakeup. | |
9892 | * | |
9893 | * In order to prevent deadlock, when the waiting thread does its check, in some | |
9894 | * cases it will temporarily drop spa_activities_lock in order to acquire the | |
9895 | * activity-specific lock. The order in which spa_activities_lock and the | |
9896 | * activity specific lock are acquired in the waiting thread is determined by | |
9897 | * the order in which they are acquired in the completing thread; if the | |
9898 | * completing thread calls spa_notify_waiters with the activity-specific lock | |
9899 | * held, then the waiting thread must also acquire the activity-specific lock | |
9900 | * first. | |
9901 | */ | |
9902 | ||
9903 | static int | |
9904 | spa_activity_in_progress(spa_t *spa, zpool_wait_activity_t activity, | |
9905 | boolean_t use_tag, uint64_t tag, boolean_t *in_progress) | |
9906 | { | |
9907 | int error = 0; | |
9908 | ||
9909 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
9910 | ||
9911 | switch (activity) { | |
9912 | case ZPOOL_WAIT_CKPT_DISCARD: | |
9913 | *in_progress = | |
9914 | (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT) && | |
9915 | zap_contains(spa_meta_objset(spa), | |
9916 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ZPOOL_CHECKPOINT) == | |
9917 | ENOENT); | |
9918 | break; | |
9919 | case ZPOOL_WAIT_FREE: | |
9920 | *in_progress = ((spa_version(spa) >= SPA_VERSION_DEADLISTS && | |
9921 | !bpobj_is_empty(&spa->spa_dsl_pool->dp_free_bpobj)) || | |
9922 | spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY) || | |
9923 | spa_livelist_delete_check(spa)); | |
9924 | break; | |
9925 | case ZPOOL_WAIT_INITIALIZE: | |
2288d419 BB |
9926 | case ZPOOL_WAIT_TRIM: |
9927 | error = spa_vdev_activity_in_progress(spa, use_tag, tag, | |
9928 | activity, in_progress); | |
e60e158e JG |
9929 | break; |
9930 | case ZPOOL_WAIT_REPLACE: | |
9931 | mutex_exit(&spa->spa_activities_lock); | |
9932 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9933 | mutex_enter(&spa->spa_activities_lock); | |
9934 | ||
9935 | *in_progress = vdev_replace_in_progress(spa->spa_root_vdev); | |
9936 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9937 | break; | |
9938 | case ZPOOL_WAIT_REMOVE: | |
9939 | *in_progress = (spa->spa_removing_phys.sr_state == | |
9940 | DSS_SCANNING); | |
9941 | break; | |
9942 | case ZPOOL_WAIT_RESILVER: | |
9a49d3f3 BB |
9943 | if ((*in_progress = vdev_rebuild_active(spa->spa_root_vdev))) |
9944 | break; | |
9a70e97f | 9945 | zfs_fallthrough; |
e60e158e JG |
9946 | case ZPOOL_WAIT_SCRUB: |
9947 | { | |
9948 | boolean_t scanning, paused, is_scrub; | |
9949 | dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; | |
9950 | ||
9951 | is_scrub = (scn->scn_phys.scn_func == POOL_SCAN_SCRUB); | |
9952 | scanning = (scn->scn_phys.scn_state == DSS_SCANNING); | |
9953 | paused = dsl_scan_is_paused_scrub(scn); | |
9954 | *in_progress = (scanning && !paused && | |
9955 | is_scrub == (activity == ZPOOL_WAIT_SCRUB)); | |
9956 | break; | |
9957 | } | |
9958 | default: | |
9959 | panic("unrecognized value for activity %d", activity); | |
9960 | } | |
9961 | ||
9962 | return (error); | |
9963 | } | |
9964 | ||
9965 | static int | |
9966 | spa_wait_common(const char *pool, zpool_wait_activity_t activity, | |
9967 | boolean_t use_tag, uint64_t tag, boolean_t *waited) | |
9968 | { | |
9969 | /* | |
9970 | * The tag is used to distinguish between instances of an activity. | |
2288d419 BB |
9971 | * 'initialize' and 'trim' are the only activities that we use this for. |
9972 | * The other activities can only have a single instance in progress in a | |
9973 | * pool at one time, making the tag unnecessary. | |
e60e158e JG |
9974 | * |
9975 | * There can be multiple devices being replaced at once, but since they | |
9976 | * all finish once resilvering finishes, we don't bother keeping track | |
9977 | * of them individually, we just wait for them all to finish. | |
9978 | */ | |
2288d419 BB |
9979 | if (use_tag && activity != ZPOOL_WAIT_INITIALIZE && |
9980 | activity != ZPOOL_WAIT_TRIM) | |
e60e158e JG |
9981 | return (EINVAL); |
9982 | ||
9983 | if (activity < 0 || activity >= ZPOOL_WAIT_NUM_ACTIVITIES) | |
9984 | return (EINVAL); | |
9985 | ||
9986 | spa_t *spa; | |
9987 | int error = spa_open(pool, &spa, FTAG); | |
9988 | if (error != 0) | |
9989 | return (error); | |
9990 | ||
9991 | /* | |
9992 | * Increment the spa's waiter count so that we can call spa_close and | |
9993 | * still ensure that the spa_t doesn't get freed before this thread is | |
9994 | * finished with it when the pool is exported. We want to call spa_close | |
9995 | * before we start waiting because otherwise the additional ref would | |
9996 | * prevent the pool from being exported or destroyed throughout the | |
9997 | * potentially long wait. | |
9998 | */ | |
9999 | mutex_enter(&spa->spa_activities_lock); | |
10000 | spa->spa_waiters++; | |
10001 | spa_close(spa, FTAG); | |
10002 | ||
10003 | *waited = B_FALSE; | |
10004 | for (;;) { | |
10005 | boolean_t in_progress; | |
10006 | error = spa_activity_in_progress(spa, activity, use_tag, tag, | |
10007 | &in_progress); | |
10008 | ||
b24771a8 | 10009 | if (error || !in_progress || spa->spa_waiters_cancel) |
e60e158e JG |
10010 | break; |
10011 | ||
10012 | *waited = B_TRUE; | |
10013 | ||
10014 | if (cv_wait_sig(&spa->spa_activities_cv, | |
10015 | &spa->spa_activities_lock) == 0) { | |
10016 | error = EINTR; | |
10017 | break; | |
10018 | } | |
10019 | } | |
10020 | ||
10021 | spa->spa_waiters--; | |
10022 | cv_signal(&spa->spa_waiters_cv); | |
10023 | mutex_exit(&spa->spa_activities_lock); | |
10024 | ||
10025 | return (error); | |
10026 | } | |
10027 | ||
10028 | /* | |
10029 | * Wait for a particular instance of the specified activity to complete, where | |
10030 | * the instance is identified by 'tag' | |
10031 | */ | |
10032 | int | |
10033 | spa_wait_tag(const char *pool, zpool_wait_activity_t activity, uint64_t tag, | |
10034 | boolean_t *waited) | |
10035 | { | |
10036 | return (spa_wait_common(pool, activity, B_TRUE, tag, waited)); | |
10037 | } | |
10038 | ||
10039 | /* | |
10040 | * Wait for all instances of the specified activity complete | |
10041 | */ | |
10042 | int | |
10043 | spa_wait(const char *pool, zpool_wait_activity_t activity, boolean_t *waited) | |
10044 | { | |
10045 | ||
10046 | return (spa_wait_common(pool, activity, B_FALSE, 0, waited)); | |
10047 | } | |
10048 | ||
a1d477c2 | 10049 | sysevent_t * |
12fa0466 DE |
10050 | spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
10051 | { | |
10052 | sysevent_t *ev = NULL; | |
10053 | #ifdef _KERNEL | |
10054 | nvlist_t *resource; | |
10055 | ||
10056 | resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl); | |
10057 | if (resource) { | |
10058 | ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP); | |
10059 | ev->resource = resource; | |
10060 | } | |
14e4e3cb AZ |
10061 | #else |
10062 | (void) spa, (void) vd, (void) hist_nvl, (void) name; | |
12fa0466 DE |
10063 | #endif |
10064 | return (ev); | |
10065 | } | |
10066 | ||
a1d477c2 | 10067 | void |
12fa0466 DE |
10068 | spa_event_post(sysevent_t *ev) |
10069 | { | |
10070 | #ifdef _KERNEL | |
10071 | if (ev) { | |
10072 | zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb); | |
10073 | kmem_free(ev, sizeof (*ev)); | |
10074 | } | |
14e4e3cb AZ |
10075 | #else |
10076 | (void) ev; | |
12fa0466 DE |
10077 | #endif |
10078 | } | |
10079 | ||
34dc7c2f | 10080 | /* |
fb390aaf HR |
10081 | * Post a zevent corresponding to the given sysevent. The 'name' must be one |
10082 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
34dc7c2f BB |
10083 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
10084 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
10085 | * or zdb as real changes. | |
10086 | */ | |
10087 | void | |
12fa0466 | 10088 | spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
34dc7c2f | 10089 | { |
12fa0466 | 10090 | spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); |
34dc7c2f | 10091 | } |
c28b2279 | 10092 | |
c28b2279 BB |
10093 | /* state manipulation functions */ |
10094 | EXPORT_SYMBOL(spa_open); | |
10095 | EXPORT_SYMBOL(spa_open_rewind); | |
10096 | EXPORT_SYMBOL(spa_get_stats); | |
10097 | EXPORT_SYMBOL(spa_create); | |
c28b2279 BB |
10098 | EXPORT_SYMBOL(spa_import); |
10099 | EXPORT_SYMBOL(spa_tryimport); | |
10100 | EXPORT_SYMBOL(spa_destroy); | |
10101 | EXPORT_SYMBOL(spa_export); | |
10102 | EXPORT_SYMBOL(spa_reset); | |
10103 | EXPORT_SYMBOL(spa_async_request); | |
10104 | EXPORT_SYMBOL(spa_async_suspend); | |
10105 | EXPORT_SYMBOL(spa_async_resume); | |
10106 | EXPORT_SYMBOL(spa_inject_addref); | |
10107 | EXPORT_SYMBOL(spa_inject_delref); | |
10108 | EXPORT_SYMBOL(spa_scan_stat_init); | |
10109 | EXPORT_SYMBOL(spa_scan_get_stats); | |
10110 | ||
e1cfd73f | 10111 | /* device manipulation */ |
c28b2279 BB |
10112 | EXPORT_SYMBOL(spa_vdev_add); |
10113 | EXPORT_SYMBOL(spa_vdev_attach); | |
10114 | EXPORT_SYMBOL(spa_vdev_detach); | |
c28b2279 BB |
10115 | EXPORT_SYMBOL(spa_vdev_setpath); |
10116 | EXPORT_SYMBOL(spa_vdev_setfru); | |
10117 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
10118 | ||
10119 | /* spare statech is global across all pools) */ | |
10120 | EXPORT_SYMBOL(spa_spare_add); | |
10121 | EXPORT_SYMBOL(spa_spare_remove); | |
10122 | EXPORT_SYMBOL(spa_spare_exists); | |
10123 | EXPORT_SYMBOL(spa_spare_activate); | |
10124 | ||
10125 | /* L2ARC statech is global across all pools) */ | |
10126 | EXPORT_SYMBOL(spa_l2cache_add); | |
10127 | EXPORT_SYMBOL(spa_l2cache_remove); | |
10128 | EXPORT_SYMBOL(spa_l2cache_exists); | |
10129 | EXPORT_SYMBOL(spa_l2cache_activate); | |
10130 | EXPORT_SYMBOL(spa_l2cache_drop); | |
10131 | ||
10132 | /* scanning */ | |
10133 | EXPORT_SYMBOL(spa_scan); | |
10134 | EXPORT_SYMBOL(spa_scan_stop); | |
10135 | ||
10136 | /* spa syncing */ | |
10137 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
10138 | EXPORT_SYMBOL(spa_sync_allpools); | |
10139 | ||
10140 | /* properties */ | |
10141 | EXPORT_SYMBOL(spa_prop_set); | |
10142 | EXPORT_SYMBOL(spa_prop_get); | |
10143 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
10144 | ||
10145 | /* asynchronous event notification */ | |
10146 | EXPORT_SYMBOL(spa_event_notify); | |
dea377c0 | 10147 | |
342357cd AM |
10148 | ZFS_MODULE_PARAM(zfs_metaslab, metaslab_, preload_pct, UINT, ZMOD_RW, |
10149 | "Percentage of CPUs to run a metaslab preload taskq"); | |
10150 | ||
c8242a96 | 10151 | /* BEGIN CSTYLED */ |
fdc2d303 | 10152 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_shift, UINT, ZMOD_RW, |
458f8231 | 10153 | "log2 fraction of arc that can be used by inflight I/Os when " |
03fdcb9a | 10154 | "verifying pool during import"); |
7ada752a | 10155 | /* END CSTYLED */ |
dea377c0 | 10156 | |
03fdcb9a | 10157 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_metadata, INT, ZMOD_RW, |
dea377c0 MA |
10158 | "Set to traverse metadata on pool import"); |
10159 | ||
03fdcb9a | 10160 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_data, INT, ZMOD_RW, |
dea377c0 | 10161 | "Set to traverse data on pool import"); |
dcb6bed1 | 10162 | |
03fdcb9a | 10163 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_print_vdev_tree, INT, ZMOD_RW, |
6cb8e530 PZ |
10164 | "Print vdev tree to zfs_dbgmsg during pool import"); |
10165 | ||
03fdcb9a | 10166 | ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_pct, UINT, ZMOD_RD, |
dcb6bed1 D |
10167 | "Percentage of CPUs to run an IO worker thread"); |
10168 | ||
7457b024 AM |
10169 | ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_tpq, UINT, ZMOD_RD, |
10170 | "Number of threads per IO worker taskqueue"); | |
10171 | ||
7ada752a | 10172 | /* BEGIN CSTYLED */ |
ab8d9c17 | 10173 | ZFS_MODULE_PARAM(zfs, zfs_, max_missing_tvds, U64, ZMOD_RW, |
03fdcb9a MM |
10174 | "Allow importing pool with up to this number of missing top-level " |
10175 | "vdevs (in read-only mode)"); | |
7ada752a | 10176 | /* END CSTYLED */ |
6cb8e530 | 10177 | |
7ada752a AZ |
10178 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_pause, INT, |
10179 | ZMOD_RW, "Set the livelist condense zthr to pause"); | |
03fdcb9a | 10180 | |
7ada752a AZ |
10181 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_pause, INT, |
10182 | ZMOD_RW, "Set the livelist condense synctask to pause"); | |
37f03da8 | 10183 | |
7ada752a AZ |
10184 | /* BEGIN CSTYLED */ |
10185 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_cancel, | |
10186 | INT, ZMOD_RW, | |
37f03da8 | 10187 | "Whether livelist condensing was canceled in the synctask"); |
03fdcb9a | 10188 | |
7ada752a AZ |
10189 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_cancel, |
10190 | INT, ZMOD_RW, | |
37f03da8 SH |
10191 | "Whether livelist condensing was canceled in the zthr function"); |
10192 | ||
7ada752a AZ |
10193 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, new_alloc, INT, |
10194 | ZMOD_RW, | |
03fdcb9a MM |
10195 | "Whether extra ALLOC blkptrs were added to a livelist entry while it " |
10196 | "was being condensed"); | |
37f03da8 | 10197 | /* END CSTYLED */ |