]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
944a3724 | 24 | * Copyright (c) 2011, 2019 by Delphix. All rights reserved. |
733b5722 | 25 | * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved. |
0c66c32d | 26 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. |
3c67d83a | 27 | * Copyright 2013 Saso Kiselkov. All rights reserved. |
e550644f BB |
28 | * Copyright (c) 2014 Integros [integros.com] |
29 | * Copyright 2016 Toomas Soome <tsoome@me.com> | |
a0bd735a | 30 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
f65fbee1 | 31 | * Copyright 2018 Joyent, Inc. |
3c819a2c | 32 | * Copyright (c) 2017, 2019, Datto Inc. All rights reserved. |
12fa0466 | 33 | * Copyright 2017 Joyent, Inc. |
cc99f275 | 34 | * Copyright (c) 2017, Intel Corporation. |
a38718a6 | 35 | */ |
34dc7c2f | 36 | |
34dc7c2f | 37 | /* |
e49f1e20 WA |
38 | * SPA: Storage Pool Allocator |
39 | * | |
34dc7c2f BB |
40 | * This file contains all the routines used when modifying on-disk SPA state. |
41 | * This includes opening, importing, destroying, exporting a pool, and syncing a | |
42 | * pool. | |
43 | */ | |
44 | ||
45 | #include <sys/zfs_context.h> | |
46 | #include <sys/fm/fs/zfs.h> | |
47 | #include <sys/spa_impl.h> | |
48 | #include <sys/zio.h> | |
49 | #include <sys/zio_checksum.h> | |
34dc7c2f BB |
50 | #include <sys/dmu.h> |
51 | #include <sys/dmu_tx.h> | |
52 | #include <sys/zap.h> | |
53 | #include <sys/zil.h> | |
428870ff | 54 | #include <sys/ddt.h> |
34dc7c2f | 55 | #include <sys/vdev_impl.h> |
a1d477c2 MA |
56 | #include <sys/vdev_removal.h> |
57 | #include <sys/vdev_indirect_mapping.h> | |
58 | #include <sys/vdev_indirect_births.h> | |
619f0976 | 59 | #include <sys/vdev_initialize.h> |
9a49d3f3 | 60 | #include <sys/vdev_rebuild.h> |
1b939560 | 61 | #include <sys/vdev_trim.h> |
c28b2279 | 62 | #include <sys/vdev_disk.h> |
34dc7c2f | 63 | #include <sys/metaslab.h> |
428870ff | 64 | #include <sys/metaslab_impl.h> |
379ca9cf | 65 | #include <sys/mmp.h> |
34dc7c2f BB |
66 | #include <sys/uberblock_impl.h> |
67 | #include <sys/txg.h> | |
68 | #include <sys/avl.h> | |
a1d477c2 | 69 | #include <sys/bpobj.h> |
34dc7c2f BB |
70 | #include <sys/dmu_traverse.h> |
71 | #include <sys/dmu_objset.h> | |
72 | #include <sys/unique.h> | |
73 | #include <sys/dsl_pool.h> | |
74 | #include <sys/dsl_dataset.h> | |
75 | #include <sys/dsl_dir.h> | |
76 | #include <sys/dsl_prop.h> | |
77 | #include <sys/dsl_synctask.h> | |
78 | #include <sys/fs/zfs.h> | |
79 | #include <sys/arc.h> | |
80 | #include <sys/callb.h> | |
81 | #include <sys/systeminfo.h> | |
34dc7c2f | 82 | #include <sys/spa_boot.h> |
9babb374 | 83 | #include <sys/zfs_ioctl.h> |
428870ff | 84 | #include <sys/dsl_scan.h> |
9ae529ec | 85 | #include <sys/zfeature.h> |
13fe0198 | 86 | #include <sys/dsl_destroy.h> |
526af785 | 87 | #include <sys/zvol.h> |
34dc7c2f | 88 | |
d164b209 | 89 | #ifdef _KERNEL |
12fa0466 DE |
90 | #include <sys/fm/protocol.h> |
91 | #include <sys/fm/util.h> | |
428870ff | 92 | #include <sys/callb.h> |
d164b209 | 93 | #include <sys/zone.h> |
c8242a96 | 94 | #include <sys/vmsystm.h> |
d164b209 BB |
95 | #endif /* _KERNEL */ |
96 | ||
34dc7c2f BB |
97 | #include "zfs_prop.h" |
98 | #include "zfs_comutil.h" | |
99 | ||
e6cfd633 WA |
100 | /* |
101 | * The interval, in seconds, at which failed configuration cache file writes | |
102 | * should be retried. | |
103 | */ | |
a1d477c2 | 104 | int zfs_ccw_retry_interval = 300; |
e6cfd633 | 105 | |
428870ff | 106 | typedef enum zti_modes { |
7ef5e54e | 107 | ZTI_MODE_FIXED, /* value is # of threads (min 1) */ |
7ef5e54e AL |
108 | ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ |
109 | ZTI_MODE_NULL, /* don't create a taskq */ | |
110 | ZTI_NMODES | |
428870ff | 111 | } zti_modes_t; |
34dc7c2f | 112 | |
7ef5e54e AL |
113 | #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } |
114 | #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } | |
115 | #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } | |
116 | #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } | |
9babb374 | 117 | |
7ef5e54e AL |
118 | #define ZTI_N(n) ZTI_P(n, 1) |
119 | #define ZTI_ONE ZTI_N(1) | |
9babb374 BB |
120 | |
121 | typedef struct zio_taskq_info { | |
7ef5e54e | 122 | zti_modes_t zti_mode; |
428870ff | 123 | uint_t zti_value; |
7ef5e54e | 124 | uint_t zti_count; |
9babb374 BB |
125 | } zio_taskq_info_t; |
126 | ||
127 | static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { | |
451041db | 128 | "iss", "iss_h", "int", "int_h" |
9babb374 BB |
129 | }; |
130 | ||
428870ff | 131 | /* |
7ef5e54e AL |
132 | * This table defines the taskq settings for each ZFS I/O type. When |
133 | * initializing a pool, we use this table to create an appropriately sized | |
134 | * taskq. Some operations are low volume and therefore have a small, static | |
135 | * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE | |
136 | * macros. Other operations process a large amount of data; the ZTI_BATCH | |
137 | * macro causes us to create a taskq oriented for throughput. Some operations | |
1b939560 | 138 | * are so high frequency and short-lived that the taskq itself can become a |
7ef5e54e AL |
139 | * point of lock contention. The ZTI_P(#, #) macro indicates that we need an |
140 | * additional degree of parallelism specified by the number of threads per- | |
141 | * taskq and the number of taskqs; when dispatching an event in this case, the | |
142 | * particular taskq is chosen at random. | |
143 | * | |
144 | * The different taskq priorities are to handle the different contexts (issue | |
145 | * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that | |
146 | * need to be handled with minimum delay. | |
428870ff BB |
147 | */ |
148 | const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { | |
149 | /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ | |
7ef5e54e | 150 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ |
aa9af22c BB |
151 | { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ |
152 | { ZTI_BATCH, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */ | |
153 | { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ | |
7ef5e54e AL |
154 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ |
155 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ | |
1b939560 | 156 | { ZTI_N(4), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* TRIM */ |
9babb374 BB |
157 | }; |
158 | ||
13fe0198 MA |
159 | static void spa_sync_version(void *arg, dmu_tx_t *tx); |
160 | static void spa_sync_props(void *arg, dmu_tx_t *tx); | |
b128c09f | 161 | static boolean_t spa_has_active_shared_spare(spa_t *spa); |
d2734cce | 162 | static int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport); |
572e2857 | 163 | static void spa_vdev_resilver_done(spa_t *spa); |
428870ff | 164 | |
e8b96c60 | 165 | uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ |
428870ff BB |
166 | boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ |
167 | uint_t zio_taskq_basedc = 80; /* base duty cycle */ | |
168 | ||
169 | boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ | |
170 | ||
afd2f7b7 PZ |
171 | /* |
172 | * Report any spa_load_verify errors found, but do not fail spa_load. | |
173 | * This is used by zdb to analyze non-idle pools. | |
174 | */ | |
175 | boolean_t spa_load_verify_dryrun = B_FALSE; | |
176 | ||
428870ff BB |
177 | /* |
178 | * This (illegal) pool name is used when temporarily importing a spa_t in order | |
179 | * to get the vdev stats associated with the imported devices. | |
180 | */ | |
181 | #define TRYIMPORT_NAME "$import" | |
34dc7c2f | 182 | |
6cb8e530 PZ |
183 | /* |
184 | * For debugging purposes: print out vdev tree during pool import. | |
185 | */ | |
186 | int spa_load_print_vdev_tree = B_FALSE; | |
187 | ||
188 | /* | |
189 | * A non-zero value for zfs_max_missing_tvds means that we allow importing | |
190 | * pools with missing top-level vdevs. This is strictly intended for advanced | |
191 | * pool recovery cases since missing data is almost inevitable. Pools with | |
192 | * missing devices can only be imported read-only for safety reasons, and their | |
193 | * fail-mode will be automatically set to "continue". | |
194 | * | |
195 | * With 1 missing vdev we should be able to import the pool and mount all | |
196 | * datasets. User data that was not modified after the missing device has been | |
197 | * added should be recoverable. This means that snapshots created prior to the | |
198 | * addition of that device should be completely intact. | |
199 | * | |
200 | * With 2 missing vdevs, some datasets may fail to mount since there are | |
201 | * dataset statistics that are stored as regular metadata. Some data might be | |
202 | * recoverable if those vdevs were added recently. | |
203 | * | |
204 | * With 3 or more missing vdevs, the pool is severely damaged and MOS entries | |
205 | * may be missing entirely. Chances of data recovery are very low. Note that | |
206 | * there are also risks of performing an inadvertent rewind as we might be | |
207 | * missing all the vdevs with the latest uberblocks. | |
208 | */ | |
209 | unsigned long zfs_max_missing_tvds = 0; | |
210 | ||
211 | /* | |
212 | * The parameters below are similar to zfs_max_missing_tvds but are only | |
213 | * intended for a preliminary open of the pool with an untrusted config which | |
214 | * might be incomplete or out-dated. | |
215 | * | |
216 | * We are more tolerant for pools opened from a cachefile since we could have | |
217 | * an out-dated cachefile where a device removal was not registered. | |
218 | * We could have set the limit arbitrarily high but in the case where devices | |
219 | * are really missing we would want to return the proper error codes; we chose | |
220 | * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available | |
221 | * and we get a chance to retrieve the trusted config. | |
222 | */ | |
223 | uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1; | |
d2734cce | 224 | |
6cb8e530 PZ |
225 | /* |
226 | * In the case where config was assembled by scanning device paths (/dev/dsks | |
227 | * by default) we are less tolerant since all the existing devices should have | |
228 | * been detected and we want spa_load to return the right error codes. | |
229 | */ | |
230 | uint64_t zfs_max_missing_tvds_scan = 0; | |
231 | ||
d2734cce SD |
232 | /* |
233 | * Debugging aid that pauses spa_sync() towards the end. | |
234 | */ | |
235 | boolean_t zfs_pause_spa_sync = B_FALSE; | |
236 | ||
37f03da8 SH |
237 | /* |
238 | * Variables to indicate the livelist condense zthr func should wait at certain | |
239 | * points for the livelist to be removed - used to test condense/destroy races | |
240 | */ | |
241 | int zfs_livelist_condense_zthr_pause = 0; | |
242 | int zfs_livelist_condense_sync_pause = 0; | |
243 | ||
244 | /* | |
245 | * Variables to track whether or not condense cancellation has been | |
246 | * triggered in testing. | |
247 | */ | |
248 | int zfs_livelist_condense_sync_cancel = 0; | |
249 | int zfs_livelist_condense_zthr_cancel = 0; | |
250 | ||
251 | /* | |
252 | * Variable to track whether or not extra ALLOC blkptrs were added to a | |
253 | * livelist entry while it was being condensed (caused by the way we track | |
254 | * remapped blkptrs in dbuf_remap_impl) | |
255 | */ | |
256 | int zfs_livelist_condense_new_alloc = 0; | |
257 | ||
34dc7c2f BB |
258 | /* |
259 | * ========================================================================== | |
260 | * SPA properties routines | |
261 | * ========================================================================== | |
262 | */ | |
263 | ||
264 | /* | |
265 | * Add a (source=src, propname=propval) list to an nvlist. | |
266 | */ | |
267 | static void | |
268 | spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, | |
269 | uint64_t intval, zprop_source_t src) | |
270 | { | |
271 | const char *propname = zpool_prop_to_name(prop); | |
272 | nvlist_t *propval; | |
273 | ||
79c76d5b | 274 | VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
275 | VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); |
276 | ||
277 | if (strval != NULL) | |
278 | VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); | |
279 | else | |
280 | VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); | |
281 | ||
282 | VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); | |
283 | nvlist_free(propval); | |
284 | } | |
285 | ||
286 | /* | |
287 | * Get property values from the spa configuration. | |
288 | */ | |
289 | static void | |
290 | spa_prop_get_config(spa_t *spa, nvlist_t **nvp) | |
291 | { | |
1bd201e7 | 292 | vdev_t *rvd = spa->spa_root_vdev; |
9ae529ec | 293 | dsl_pool_t *pool = spa->spa_dsl_pool; |
f3a7f661 | 294 | uint64_t size, alloc, cap, version; |
82ab6848 | 295 | const zprop_source_t src = ZPROP_SRC_NONE; |
b128c09f | 296 | spa_config_dirent_t *dp; |
f3a7f661 | 297 | metaslab_class_t *mc = spa_normal_class(spa); |
b128c09f BB |
298 | |
299 | ASSERT(MUTEX_HELD(&spa->spa_props_lock)); | |
34dc7c2f | 300 | |
1bd201e7 | 301 | if (rvd != NULL) { |
cc99f275 DB |
302 | alloc = metaslab_class_get_alloc(mc); |
303 | alloc += metaslab_class_get_alloc(spa_special_class(spa)); | |
304 | alloc += metaslab_class_get_alloc(spa_dedup_class(spa)); | |
305 | ||
306 | size = metaslab_class_get_space(mc); | |
307 | size += metaslab_class_get_space(spa_special_class(spa)); | |
308 | size += metaslab_class_get_space(spa_dedup_class(spa)); | |
309 | ||
d164b209 BB |
310 | spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); |
311 | spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); | |
428870ff BB |
312 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); |
313 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, | |
314 | size - alloc, src); | |
d2734cce SD |
315 | spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL, |
316 | spa->spa_checkpoint_info.sci_dspace, src); | |
1bd201e7 | 317 | |
f3a7f661 GW |
318 | spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, |
319 | metaslab_class_fragmentation(mc), src); | |
320 | spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, | |
321 | metaslab_class_expandable_space(mc), src); | |
572e2857 | 322 | spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, |
da92d5cb | 323 | (spa_mode(spa) == SPA_MODE_READ), src); |
d164b209 | 324 | |
428870ff | 325 | cap = (size == 0) ? 0 : (alloc * 100 / size); |
d164b209 BB |
326 | spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); |
327 | ||
428870ff BB |
328 | spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, |
329 | ddt_get_pool_dedup_ratio(spa), src); | |
330 | ||
d164b209 | 331 | spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, |
1bd201e7 | 332 | rvd->vdev_state, src); |
d164b209 BB |
333 | |
334 | version = spa_version(spa); | |
82ab6848 HM |
335 | if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) { |
336 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
337 | version, ZPROP_SRC_DEFAULT); | |
338 | } else { | |
339 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
340 | version, ZPROP_SRC_LOCAL); | |
341 | } | |
a448a255 SD |
342 | spa_prop_add_list(*nvp, ZPOOL_PROP_LOAD_GUID, |
343 | NULL, spa_load_guid(spa), src); | |
d164b209 | 344 | } |
34dc7c2f | 345 | |
9ae529ec | 346 | if (pool != NULL) { |
9ae529ec CS |
347 | /* |
348 | * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, | |
349 | * when opening pools before this version freedir will be NULL. | |
350 | */ | |
fbeddd60 | 351 | if (pool->dp_free_dir != NULL) { |
9ae529ec | 352 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, |
d683ddbb JG |
353 | dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, |
354 | src); | |
9ae529ec CS |
355 | } else { |
356 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, | |
357 | NULL, 0, src); | |
358 | } | |
fbeddd60 MA |
359 | |
360 | if (pool->dp_leak_dir != NULL) { | |
361 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, | |
d683ddbb JG |
362 | dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, |
363 | src); | |
fbeddd60 MA |
364 | } else { |
365 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, | |
366 | NULL, 0, src); | |
367 | } | |
9ae529ec CS |
368 | } |
369 | ||
34dc7c2f | 370 | spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); |
34dc7c2f | 371 | |
d96eb2b1 DM |
372 | if (spa->spa_comment != NULL) { |
373 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, | |
374 | 0, ZPROP_SRC_LOCAL); | |
375 | } | |
376 | ||
34dc7c2f BB |
377 | if (spa->spa_root != NULL) |
378 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, | |
379 | 0, ZPROP_SRC_LOCAL); | |
380 | ||
f1512ee6 MA |
381 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { |
382 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
383 | MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); | |
384 | } else { | |
385 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
386 | SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); | |
387 | } | |
388 | ||
50c957f7 NB |
389 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) { |
390 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
391 | DNODE_MAX_SIZE, ZPROP_SRC_NONE); | |
392 | } else { | |
393 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
394 | DNODE_MIN_SIZE, ZPROP_SRC_NONE); | |
395 | } | |
396 | ||
b128c09f BB |
397 | if ((dp = list_head(&spa->spa_config_list)) != NULL) { |
398 | if (dp->scd_path == NULL) { | |
34dc7c2f | 399 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f BB |
400 | "none", 0, ZPROP_SRC_LOCAL); |
401 | } else if (strcmp(dp->scd_path, spa_config_path) != 0) { | |
34dc7c2f | 402 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f | 403 | dp->scd_path, 0, ZPROP_SRC_LOCAL); |
34dc7c2f BB |
404 | } |
405 | } | |
406 | } | |
407 | ||
408 | /* | |
409 | * Get zpool property values. | |
410 | */ | |
411 | int | |
412 | spa_prop_get(spa_t *spa, nvlist_t **nvp) | |
413 | { | |
428870ff | 414 | objset_t *mos = spa->spa_meta_objset; |
34dc7c2f BB |
415 | zap_cursor_t zc; |
416 | zap_attribute_t za; | |
1743c737 | 417 | dsl_pool_t *dp; |
34dc7c2f BB |
418 | int err; |
419 | ||
79c76d5b | 420 | err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP); |
c28b2279 | 421 | if (err) |
d1d7e268 | 422 | return (err); |
34dc7c2f | 423 | |
1743c737 AM |
424 | dp = spa_get_dsl(spa); |
425 | dsl_pool_config_enter(dp, FTAG); | |
b128c09f BB |
426 | mutex_enter(&spa->spa_props_lock); |
427 | ||
34dc7c2f BB |
428 | /* |
429 | * Get properties from the spa config. | |
430 | */ | |
431 | spa_prop_get_config(spa, nvp); | |
432 | ||
34dc7c2f | 433 | /* If no pool property object, no more prop to get. */ |
1743c737 | 434 | if (mos == NULL || spa->spa_pool_props_object == 0) |
c28b2279 | 435 | goto out; |
34dc7c2f BB |
436 | |
437 | /* | |
438 | * Get properties from the MOS pool property object. | |
439 | */ | |
440 | for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); | |
441 | (err = zap_cursor_retrieve(&zc, &za)) == 0; | |
442 | zap_cursor_advance(&zc)) { | |
443 | uint64_t intval = 0; | |
444 | char *strval = NULL; | |
445 | zprop_source_t src = ZPROP_SRC_DEFAULT; | |
446 | zpool_prop_t prop; | |
447 | ||
31864e3d | 448 | if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL) |
34dc7c2f BB |
449 | continue; |
450 | ||
451 | switch (za.za_integer_length) { | |
452 | case 8: | |
453 | /* integer property */ | |
454 | if (za.za_first_integer != | |
455 | zpool_prop_default_numeric(prop)) | |
456 | src = ZPROP_SRC_LOCAL; | |
457 | ||
458 | if (prop == ZPOOL_PROP_BOOTFS) { | |
34dc7c2f BB |
459 | dsl_dataset_t *ds = NULL; |
460 | ||
619f0976 GW |
461 | err = dsl_dataset_hold_obj(dp, |
462 | za.za_first_integer, FTAG, &ds); | |
1743c737 | 463 | if (err != 0) |
34dc7c2f | 464 | break; |
34dc7c2f | 465 | |
eca7b760 | 466 | strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, |
79c76d5b | 467 | KM_SLEEP); |
34dc7c2f | 468 | dsl_dataset_name(ds, strval); |
b128c09f | 469 | dsl_dataset_rele(ds, FTAG); |
34dc7c2f BB |
470 | } else { |
471 | strval = NULL; | |
472 | intval = za.za_first_integer; | |
473 | } | |
474 | ||
475 | spa_prop_add_list(*nvp, prop, strval, intval, src); | |
476 | ||
477 | if (strval != NULL) | |
eca7b760 | 478 | kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); |
34dc7c2f BB |
479 | |
480 | break; | |
481 | ||
482 | case 1: | |
483 | /* string property */ | |
79c76d5b | 484 | strval = kmem_alloc(za.za_num_integers, KM_SLEEP); |
34dc7c2f BB |
485 | err = zap_lookup(mos, spa->spa_pool_props_object, |
486 | za.za_name, 1, za.za_num_integers, strval); | |
487 | if (err) { | |
488 | kmem_free(strval, za.za_num_integers); | |
489 | break; | |
490 | } | |
491 | spa_prop_add_list(*nvp, prop, strval, 0, src); | |
492 | kmem_free(strval, za.za_num_integers); | |
493 | break; | |
494 | ||
495 | default: | |
496 | break; | |
497 | } | |
498 | } | |
499 | zap_cursor_fini(&zc); | |
34dc7c2f | 500 | out: |
1743c737 AM |
501 | mutex_exit(&spa->spa_props_lock); |
502 | dsl_pool_config_exit(dp, FTAG); | |
34dc7c2f BB |
503 | if (err && err != ENOENT) { |
504 | nvlist_free(*nvp); | |
505 | *nvp = NULL; | |
506 | return (err); | |
507 | } | |
508 | ||
509 | return (0); | |
510 | } | |
511 | ||
512 | /* | |
513 | * Validate the given pool properties nvlist and modify the list | |
514 | * for the property values to be set. | |
515 | */ | |
516 | static int | |
517 | spa_prop_validate(spa_t *spa, nvlist_t *props) | |
518 | { | |
519 | nvpair_t *elem; | |
520 | int error = 0, reset_bootfs = 0; | |
d4ed6673 | 521 | uint64_t objnum = 0; |
9ae529ec | 522 | boolean_t has_feature = B_FALSE; |
34dc7c2f BB |
523 | |
524 | elem = NULL; | |
525 | while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { | |
34dc7c2f | 526 | uint64_t intval; |
9ae529ec CS |
527 | char *strval, *slash, *check, *fname; |
528 | const char *propname = nvpair_name(elem); | |
529 | zpool_prop_t prop = zpool_name_to_prop(propname); | |
530 | ||
31864e3d BB |
531 | switch (prop) { |
532 | case ZPOOL_PROP_INVAL: | |
9ae529ec | 533 | if (!zpool_prop_feature(propname)) { |
2e528b49 | 534 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
535 | break; |
536 | } | |
537 | ||
538 | /* | |
539 | * Sanitize the input. | |
540 | */ | |
541 | if (nvpair_type(elem) != DATA_TYPE_UINT64) { | |
2e528b49 | 542 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
543 | break; |
544 | } | |
545 | ||
546 | if (nvpair_value_uint64(elem, &intval) != 0) { | |
2e528b49 | 547 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
548 | break; |
549 | } | |
34dc7c2f | 550 | |
9ae529ec | 551 | if (intval != 0) { |
2e528b49 | 552 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
553 | break; |
554 | } | |
34dc7c2f | 555 | |
9ae529ec CS |
556 | fname = strchr(propname, '@') + 1; |
557 | if (zfeature_lookup_name(fname, NULL) != 0) { | |
2e528b49 | 558 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
559 | break; |
560 | } | |
561 | ||
562 | has_feature = B_TRUE; | |
563 | break; | |
34dc7c2f | 564 | |
34dc7c2f BB |
565 | case ZPOOL_PROP_VERSION: |
566 | error = nvpair_value_uint64(elem, &intval); | |
567 | if (!error && | |
9ae529ec CS |
568 | (intval < spa_version(spa) || |
569 | intval > SPA_VERSION_BEFORE_FEATURES || | |
570 | has_feature)) | |
2e528b49 | 571 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
572 | break; |
573 | ||
574 | case ZPOOL_PROP_DELEGATION: | |
575 | case ZPOOL_PROP_AUTOREPLACE: | |
b128c09f | 576 | case ZPOOL_PROP_LISTSNAPS: |
9babb374 | 577 | case ZPOOL_PROP_AUTOEXPAND: |
1b939560 | 578 | case ZPOOL_PROP_AUTOTRIM: |
34dc7c2f BB |
579 | error = nvpair_value_uint64(elem, &intval); |
580 | if (!error && intval > 1) | |
2e528b49 | 581 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
582 | break; |
583 | ||
379ca9cf OF |
584 | case ZPOOL_PROP_MULTIHOST: |
585 | error = nvpair_value_uint64(elem, &intval); | |
586 | if (!error && intval > 1) | |
587 | error = SET_ERROR(EINVAL); | |
588 | ||
25f06d67 BB |
589 | if (!error) { |
590 | uint32_t hostid = zone_get_hostid(NULL); | |
591 | if (hostid) | |
592 | spa->spa_hostid = hostid; | |
593 | else | |
594 | error = SET_ERROR(ENOTSUP); | |
595 | } | |
379ca9cf OF |
596 | |
597 | break; | |
598 | ||
34dc7c2f | 599 | case ZPOOL_PROP_BOOTFS: |
9babb374 BB |
600 | /* |
601 | * If the pool version is less than SPA_VERSION_BOOTFS, | |
602 | * or the pool is still being created (version == 0), | |
603 | * the bootfs property cannot be set. | |
604 | */ | |
34dc7c2f | 605 | if (spa_version(spa) < SPA_VERSION_BOOTFS) { |
2e528b49 | 606 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
607 | break; |
608 | } | |
609 | ||
610 | /* | |
b128c09f | 611 | * Make sure the vdev config is bootable |
34dc7c2f | 612 | */ |
b128c09f | 613 | if (!vdev_is_bootable(spa->spa_root_vdev)) { |
2e528b49 | 614 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
615 | break; |
616 | } | |
617 | ||
618 | reset_bootfs = 1; | |
619 | ||
620 | error = nvpair_value_string(elem, &strval); | |
621 | ||
622 | if (!error) { | |
9ae529ec | 623 | objset_t *os; |
b128c09f | 624 | |
34dc7c2f BB |
625 | if (strval == NULL || strval[0] == '\0') { |
626 | objnum = zpool_prop_default_numeric( | |
627 | ZPOOL_PROP_BOOTFS); | |
628 | break; | |
629 | } | |
630 | ||
d1d7e268 | 631 | error = dmu_objset_hold(strval, FTAG, &os); |
619f0976 | 632 | if (error != 0) |
34dc7c2f | 633 | break; |
b128c09f | 634 | |
eaa25f1a | 635 | /* Must be ZPL. */ |
428870ff | 636 | if (dmu_objset_type(os) != DMU_OST_ZFS) { |
2e528b49 | 637 | error = SET_ERROR(ENOTSUP); |
b128c09f BB |
638 | } else { |
639 | objnum = dmu_objset_id(os); | |
640 | } | |
428870ff | 641 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
642 | } |
643 | break; | |
b128c09f | 644 | |
34dc7c2f BB |
645 | case ZPOOL_PROP_FAILUREMODE: |
646 | error = nvpair_value_uint64(elem, &intval); | |
3bfd95d5 | 647 | if (!error && intval > ZIO_FAILURE_MODE_PANIC) |
2e528b49 | 648 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
649 | |
650 | /* | |
651 | * This is a special case which only occurs when | |
652 | * the pool has completely failed. This allows | |
653 | * the user to change the in-core failmode property | |
654 | * without syncing it out to disk (I/Os might | |
655 | * currently be blocked). We do this by returning | |
656 | * EIO to the caller (spa_prop_set) to trick it | |
657 | * into thinking we encountered a property validation | |
658 | * error. | |
659 | */ | |
b128c09f | 660 | if (!error && spa_suspended(spa)) { |
34dc7c2f | 661 | spa->spa_failmode = intval; |
2e528b49 | 662 | error = SET_ERROR(EIO); |
34dc7c2f BB |
663 | } |
664 | break; | |
665 | ||
666 | case ZPOOL_PROP_CACHEFILE: | |
667 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
668 | break; | |
669 | ||
670 | if (strval[0] == '\0') | |
671 | break; | |
672 | ||
673 | if (strcmp(strval, "none") == 0) | |
674 | break; | |
675 | ||
676 | if (strval[0] != '/') { | |
2e528b49 | 677 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
678 | break; |
679 | } | |
680 | ||
681 | slash = strrchr(strval, '/'); | |
682 | ASSERT(slash != NULL); | |
683 | ||
684 | if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || | |
685 | strcmp(slash, "/..") == 0) | |
2e528b49 | 686 | error = SET_ERROR(EINVAL); |
34dc7c2f | 687 | break; |
428870ff | 688 | |
d96eb2b1 DM |
689 | case ZPOOL_PROP_COMMENT: |
690 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
691 | break; | |
692 | for (check = strval; *check != '\0'; check++) { | |
693 | if (!isprint(*check)) { | |
2e528b49 | 694 | error = SET_ERROR(EINVAL); |
d96eb2b1 DM |
695 | break; |
696 | } | |
d96eb2b1 DM |
697 | } |
698 | if (strlen(strval) > ZPROP_MAX_COMMENT) | |
2e528b49 | 699 | error = SET_ERROR(E2BIG); |
d96eb2b1 DM |
700 | break; |
701 | ||
e75c13c3 BB |
702 | default: |
703 | break; | |
34dc7c2f BB |
704 | } |
705 | ||
706 | if (error) | |
707 | break; | |
708 | } | |
709 | ||
050d720c MA |
710 | (void) nvlist_remove_all(props, |
711 | zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO)); | |
712 | ||
34dc7c2f BB |
713 | if (!error && reset_bootfs) { |
714 | error = nvlist_remove(props, | |
715 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); | |
716 | ||
717 | if (!error) { | |
718 | error = nvlist_add_uint64(props, | |
719 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); | |
720 | } | |
721 | } | |
722 | ||
723 | return (error); | |
724 | } | |
725 | ||
d164b209 BB |
726 | void |
727 | spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) | |
728 | { | |
729 | char *cachefile; | |
730 | spa_config_dirent_t *dp; | |
731 | ||
732 | if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
733 | &cachefile) != 0) | |
734 | return; | |
735 | ||
736 | dp = kmem_alloc(sizeof (spa_config_dirent_t), | |
79c76d5b | 737 | KM_SLEEP); |
d164b209 BB |
738 | |
739 | if (cachefile[0] == '\0') | |
740 | dp->scd_path = spa_strdup(spa_config_path); | |
741 | else if (strcmp(cachefile, "none") == 0) | |
742 | dp->scd_path = NULL; | |
743 | else | |
744 | dp->scd_path = spa_strdup(cachefile); | |
745 | ||
746 | list_insert_head(&spa->spa_config_list, dp); | |
747 | if (need_sync) | |
748 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
749 | } | |
750 | ||
34dc7c2f BB |
751 | int |
752 | spa_prop_set(spa_t *spa, nvlist_t *nvp) | |
753 | { | |
754 | int error; | |
9ae529ec | 755 | nvpair_t *elem = NULL; |
d164b209 | 756 | boolean_t need_sync = B_FALSE; |
34dc7c2f BB |
757 | |
758 | if ((error = spa_prop_validate(spa, nvp)) != 0) | |
759 | return (error); | |
760 | ||
d164b209 | 761 | while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { |
9ae529ec | 762 | zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); |
d164b209 | 763 | |
572e2857 BB |
764 | if (prop == ZPOOL_PROP_CACHEFILE || |
765 | prop == ZPOOL_PROP_ALTROOT || | |
766 | prop == ZPOOL_PROP_READONLY) | |
d164b209 BB |
767 | continue; |
768 | ||
31864e3d | 769 | if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { |
9ae529ec CS |
770 | uint64_t ver; |
771 | ||
772 | if (prop == ZPOOL_PROP_VERSION) { | |
773 | VERIFY(nvpair_value_uint64(elem, &ver) == 0); | |
774 | } else { | |
775 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
776 | ver = SPA_VERSION_FEATURES; | |
777 | need_sync = B_TRUE; | |
778 | } | |
779 | ||
780 | /* Save time if the version is already set. */ | |
781 | if (ver == spa_version(spa)) | |
782 | continue; | |
783 | ||
784 | /* | |
785 | * In addition to the pool directory object, we might | |
786 | * create the pool properties object, the features for | |
787 | * read object, the features for write object, or the | |
788 | * feature descriptions object. | |
789 | */ | |
13fe0198 | 790 | error = dsl_sync_task(spa->spa_name, NULL, |
3d45fdd6 MA |
791 | spa_sync_version, &ver, |
792 | 6, ZFS_SPACE_CHECK_RESERVED); | |
9ae529ec CS |
793 | if (error) |
794 | return (error); | |
795 | continue; | |
796 | } | |
797 | ||
d164b209 BB |
798 | need_sync = B_TRUE; |
799 | break; | |
800 | } | |
801 | ||
9ae529ec | 802 | if (need_sync) { |
13fe0198 | 803 | return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, |
3d45fdd6 | 804 | nvp, 6, ZFS_SPACE_CHECK_RESERVED)); |
9ae529ec CS |
805 | } |
806 | ||
807 | return (0); | |
34dc7c2f BB |
808 | } |
809 | ||
810 | /* | |
811 | * If the bootfs property value is dsobj, clear it. | |
812 | */ | |
813 | void | |
814 | spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) | |
815 | { | |
816 | if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { | |
817 | VERIFY(zap_remove(spa->spa_meta_objset, | |
818 | spa->spa_pool_props_object, | |
819 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); | |
820 | spa->spa_bootfs = 0; | |
821 | } | |
822 | } | |
823 | ||
3bc7e0fb GW |
824 | /*ARGSUSED*/ |
825 | static int | |
13fe0198 | 826 | spa_change_guid_check(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 827 | { |
2a8ba608 | 828 | uint64_t *newguid __maybe_unused = arg; |
13fe0198 | 829 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; |
3bc7e0fb GW |
830 | vdev_t *rvd = spa->spa_root_vdev; |
831 | uint64_t vdev_state; | |
3bc7e0fb | 832 | |
d2734cce SD |
833 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { |
834 | int error = (spa_has_checkpoint(spa)) ? | |
835 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
836 | return (SET_ERROR(error)); | |
837 | } | |
838 | ||
3bc7e0fb GW |
839 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
840 | vdev_state = rvd->vdev_state; | |
841 | spa_config_exit(spa, SCL_STATE, FTAG); | |
842 | ||
843 | if (vdev_state != VDEV_STATE_HEALTHY) | |
2e528b49 | 844 | return (SET_ERROR(ENXIO)); |
3bc7e0fb GW |
845 | |
846 | ASSERT3U(spa_guid(spa), !=, *newguid); | |
847 | ||
848 | return (0); | |
849 | } | |
850 | ||
851 | static void | |
13fe0198 | 852 | spa_change_guid_sync(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 853 | { |
13fe0198 MA |
854 | uint64_t *newguid = arg; |
855 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
3bc7e0fb GW |
856 | uint64_t oldguid; |
857 | vdev_t *rvd = spa->spa_root_vdev; | |
858 | ||
859 | oldguid = spa_guid(spa); | |
860 | ||
861 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
862 | rvd->vdev_guid = *newguid; | |
863 | rvd->vdev_guid_sum += (*newguid - oldguid); | |
864 | vdev_config_dirty(rvd); | |
865 | spa_config_exit(spa, SCL_STATE, FTAG); | |
866 | ||
6f1ffb06 | 867 | spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", |
74756182 | 868 | (u_longlong_t)oldguid, (u_longlong_t)*newguid); |
3bc7e0fb GW |
869 | } |
870 | ||
3541dc6d GA |
871 | /* |
872 | * Change the GUID for the pool. This is done so that we can later | |
873 | * re-import a pool built from a clone of our own vdevs. We will modify | |
874 | * the root vdev's guid, our own pool guid, and then mark all of our | |
875 | * vdevs dirty. Note that we must make sure that all our vdevs are | |
876 | * online when we do this, or else any vdevs that weren't present | |
877 | * would be orphaned from our pool. We are also going to issue a | |
878 | * sysevent to update any watchers. | |
879 | */ | |
880 | int | |
881 | spa_change_guid(spa_t *spa) | |
882 | { | |
3bc7e0fb GW |
883 | int error; |
884 | uint64_t guid; | |
3541dc6d | 885 | |
621dd7bb | 886 | mutex_enter(&spa->spa_vdev_top_lock); |
3bc7e0fb GW |
887 | mutex_enter(&spa_namespace_lock); |
888 | guid = spa_generate_guid(NULL); | |
3541dc6d | 889 | |
13fe0198 | 890 | error = dsl_sync_task(spa->spa_name, spa_change_guid_check, |
3d45fdd6 | 891 | spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); |
3541dc6d | 892 | |
3bc7e0fb | 893 | if (error == 0) { |
a1d477c2 | 894 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 895 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); |
3bc7e0fb | 896 | } |
3541dc6d | 897 | |
3bc7e0fb | 898 | mutex_exit(&spa_namespace_lock); |
621dd7bb | 899 | mutex_exit(&spa->spa_vdev_top_lock); |
3541dc6d | 900 | |
3bc7e0fb | 901 | return (error); |
3541dc6d GA |
902 | } |
903 | ||
34dc7c2f BB |
904 | /* |
905 | * ========================================================================== | |
906 | * SPA state manipulation (open/create/destroy/import/export) | |
907 | * ========================================================================== | |
908 | */ | |
909 | ||
910 | static int | |
911 | spa_error_entry_compare(const void *a, const void *b) | |
912 | { | |
ee36c709 GN |
913 | const spa_error_entry_t *sa = (const spa_error_entry_t *)a; |
914 | const spa_error_entry_t *sb = (const spa_error_entry_t *)b; | |
34dc7c2f BB |
915 | int ret; |
916 | ||
ee36c709 | 917 | ret = memcmp(&sa->se_bookmark, &sb->se_bookmark, |
5dbd68a3 | 918 | sizeof (zbookmark_phys_t)); |
34dc7c2f | 919 | |
ca577779 | 920 | return (TREE_ISIGN(ret)); |
34dc7c2f BB |
921 | } |
922 | ||
923 | /* | |
924 | * Utility function which retrieves copies of the current logs and | |
925 | * re-initializes them in the process. | |
926 | */ | |
927 | void | |
928 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
929 | { | |
930 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
931 | ||
932 | bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); | |
933 | bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); | |
934 | ||
935 | avl_create(&spa->spa_errlist_scrub, | |
936 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
937 | offsetof(spa_error_entry_t, se_avl)); | |
938 | avl_create(&spa->spa_errlist_last, | |
939 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
940 | offsetof(spa_error_entry_t, se_avl)); | |
941 | } | |
942 | ||
7ef5e54e AL |
943 | static void |
944 | spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
34dc7c2f | 945 | { |
7ef5e54e AL |
946 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; |
947 | enum zti_modes mode = ztip->zti_mode; | |
948 | uint_t value = ztip->zti_value; | |
949 | uint_t count = ztip->zti_count; | |
950 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1c27024e | 951 | uint_t flags = 0; |
428870ff | 952 | boolean_t batch = B_FALSE; |
34dc7c2f | 953 | |
7ef5e54e AL |
954 | if (mode == ZTI_MODE_NULL) { |
955 | tqs->stqs_count = 0; | |
956 | tqs->stqs_taskq = NULL; | |
957 | return; | |
958 | } | |
428870ff | 959 | |
7ef5e54e | 960 | ASSERT3U(count, >, 0); |
428870ff | 961 | |
7ef5e54e AL |
962 | tqs->stqs_count = count; |
963 | tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); | |
428870ff | 964 | |
e8b96c60 MA |
965 | switch (mode) { |
966 | case ZTI_MODE_FIXED: | |
967 | ASSERT3U(value, >=, 1); | |
968 | value = MAX(value, 1); | |
d33931a8 | 969 | flags |= TASKQ_DYNAMIC; |
e8b96c60 | 970 | break; |
7ef5e54e | 971 | |
e8b96c60 MA |
972 | case ZTI_MODE_BATCH: |
973 | batch = B_TRUE; | |
974 | flags |= TASKQ_THREADS_CPU_PCT; | |
dcb6bed1 | 975 | value = MIN(zio_taskq_batch_pct, 100); |
e8b96c60 | 976 | break; |
7ef5e54e | 977 | |
e8b96c60 MA |
978 | default: |
979 | panic("unrecognized mode for %s_%s taskq (%u:%u) in " | |
980 | "spa_activate()", | |
981 | zio_type_name[t], zio_taskq_types[q], mode, value); | |
982 | break; | |
983 | } | |
7ef5e54e | 984 | |
1c27024e | 985 | for (uint_t i = 0; i < count; i++) { |
e8b96c60 | 986 | taskq_t *tq; |
af430294 | 987 | char name[32]; |
7ef5e54e | 988 | |
af430294 MA |
989 | (void) snprintf(name, sizeof (name), "%s_%s", |
990 | zio_type_name[t], zio_taskq_types[q]); | |
7ef5e54e AL |
991 | |
992 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { | |
993 | if (batch) | |
994 | flags |= TASKQ_DC_BATCH; | |
995 | ||
996 | tq = taskq_create_sysdc(name, value, 50, INT_MAX, | |
997 | spa->spa_proc, zio_taskq_basedc, flags); | |
998 | } else { | |
e8b96c60 MA |
999 | pri_t pri = maxclsyspri; |
1000 | /* | |
1001 | * The write issue taskq can be extremely CPU | |
1229323d BB |
1002 | * intensive. Run it at slightly less important |
1003 | * priority than the other taskqs. Under Linux this | |
1004 | * means incrementing the priority value on platforms | |
1005 | * like illumos it should be decremented. | |
e8b96c60 MA |
1006 | */ |
1007 | if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) | |
1229323d | 1008 | pri++; |
e8b96c60 MA |
1009 | |
1010 | tq = taskq_create_proc(name, value, pri, 50, | |
7ef5e54e AL |
1011 | INT_MAX, spa->spa_proc, flags); |
1012 | } | |
1013 | ||
1014 | tqs->stqs_taskq[i] = tq; | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | static void | |
1019 | spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
1020 | { | |
1021 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7ef5e54e AL |
1022 | |
1023 | if (tqs->stqs_taskq == NULL) { | |
1024 | ASSERT3U(tqs->stqs_count, ==, 0); | |
1025 | return; | |
1026 | } | |
1027 | ||
1c27024e | 1028 | for (uint_t i = 0; i < tqs->stqs_count; i++) { |
7ef5e54e AL |
1029 | ASSERT3P(tqs->stqs_taskq[i], !=, NULL); |
1030 | taskq_destroy(tqs->stqs_taskq[i]); | |
428870ff | 1031 | } |
34dc7c2f | 1032 | |
7ef5e54e AL |
1033 | kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); |
1034 | tqs->stqs_taskq = NULL; | |
1035 | } | |
34dc7c2f | 1036 | |
7ef5e54e AL |
1037 | /* |
1038 | * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. | |
1039 | * Note that a type may have multiple discrete taskqs to avoid lock contention | |
1040 | * on the taskq itself. In that case we choose which taskq at random by using | |
1041 | * the low bits of gethrtime(). | |
1042 | */ | |
1043 | void | |
1044 | spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1045 | task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) | |
1046 | { | |
1047 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1048 | taskq_t *tq; | |
1049 | ||
1050 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1051 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1052 | ||
1053 | if (tqs->stqs_count == 1) { | |
1054 | tq = tqs->stqs_taskq[0]; | |
1055 | } else { | |
c12936b1 | 1056 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
428870ff | 1057 | } |
7ef5e54e AL |
1058 | |
1059 | taskq_dispatch_ent(tq, func, arg, flags, ent); | |
428870ff BB |
1060 | } |
1061 | ||
044baf00 BB |
1062 | /* |
1063 | * Same as spa_taskq_dispatch_ent() but block on the task until completion. | |
1064 | */ | |
1065 | void | |
1066 | spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1067 | task_func_t *func, void *arg, uint_t flags) | |
1068 | { | |
1069 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1070 | taskq_t *tq; | |
1071 | taskqid_t id; | |
1072 | ||
1073 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1074 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1075 | ||
1076 | if (tqs->stqs_count == 1) { | |
1077 | tq = tqs->stqs_taskq[0]; | |
1078 | } else { | |
c12936b1 | 1079 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
044baf00 BB |
1080 | } |
1081 | ||
1082 | id = taskq_dispatch(tq, func, arg, flags); | |
1083 | if (id) | |
1084 | taskq_wait_id(tq, id); | |
1085 | } | |
1086 | ||
428870ff BB |
1087 | static void |
1088 | spa_create_zio_taskqs(spa_t *spa) | |
1089 | { | |
1c27024e DB |
1090 | for (int t = 0; t < ZIO_TYPES; t++) { |
1091 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1092 | spa_taskqs_init(spa, t, q); |
428870ff BB |
1093 | } |
1094 | } | |
1095 | } | |
9babb374 | 1096 | |
c25b8f99 BB |
1097 | /* |
1098 | * Disabled until spa_thread() can be adapted for Linux. | |
1099 | */ | |
1100 | #undef HAVE_SPA_THREAD | |
1101 | ||
7b89a549 | 1102 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) |
428870ff BB |
1103 | static void |
1104 | spa_thread(void *arg) | |
1105 | { | |
93ce2b4c | 1106 | psetid_t zio_taskq_psrset_bind = PS_NONE; |
428870ff | 1107 | callb_cpr_t cprinfo; |
9babb374 | 1108 | |
428870ff BB |
1109 | spa_t *spa = arg; |
1110 | user_t *pu = PTOU(curproc); | |
9babb374 | 1111 | |
428870ff BB |
1112 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, |
1113 | spa->spa_name); | |
9babb374 | 1114 | |
428870ff BB |
1115 | ASSERT(curproc != &p0); |
1116 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
1117 | "zpool-%s", spa->spa_name); | |
1118 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
1119 | ||
1120 | /* bind this thread to the requested psrset */ | |
1121 | if (zio_taskq_psrset_bind != PS_NONE) { | |
1122 | pool_lock(); | |
1123 | mutex_enter(&cpu_lock); | |
1124 | mutex_enter(&pidlock); | |
1125 | mutex_enter(&curproc->p_lock); | |
1126 | ||
1127 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
1128 | 0, NULL, NULL) == 0) { | |
1129 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
1130 | } else { | |
1131 | cmn_err(CE_WARN, | |
1132 | "Couldn't bind process for zfs pool \"%s\" to " | |
1133 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
1134 | } | |
1135 | ||
1136 | mutex_exit(&curproc->p_lock); | |
1137 | mutex_exit(&pidlock); | |
1138 | mutex_exit(&cpu_lock); | |
1139 | pool_unlock(); | |
1140 | } | |
1141 | ||
1142 | if (zio_taskq_sysdc) { | |
1143 | sysdc_thread_enter(curthread, 100, 0); | |
1144 | } | |
1145 | ||
1146 | spa->spa_proc = curproc; | |
1147 | spa->spa_did = curthread->t_did; | |
1148 | ||
1149 | spa_create_zio_taskqs(spa); | |
1150 | ||
1151 | mutex_enter(&spa->spa_proc_lock); | |
1152 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
1153 | ||
1154 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
1155 | cv_broadcast(&spa->spa_proc_cv); | |
1156 | ||
1157 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
1158 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
1159 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1160 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
1161 | ||
1162 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
1163 | spa->spa_proc_state = SPA_PROC_GONE; | |
1164 | spa->spa_proc = &p0; | |
1165 | cv_broadcast(&spa->spa_proc_cv); | |
1166 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
1167 | ||
1168 | mutex_enter(&curproc->p_lock); | |
1169 | lwp_exit(); | |
1170 | } | |
1171 | #endif | |
1172 | ||
1173 | /* | |
1174 | * Activate an uninitialized pool. | |
1175 | */ | |
1176 | static void | |
da92d5cb | 1177 | spa_activate(spa_t *spa, spa_mode_t mode) |
428870ff BB |
1178 | { |
1179 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); | |
1180 | ||
1181 | spa->spa_state = POOL_STATE_ACTIVE; | |
1182 | spa->spa_mode = mode; | |
1183 | ||
1184 | spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
1185 | spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
cc99f275 DB |
1186 | spa->spa_special_class = metaslab_class_create(spa, zfs_metaslab_ops); |
1187 | spa->spa_dedup_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
428870ff BB |
1188 | |
1189 | /* Try to create a covering process */ | |
1190 | mutex_enter(&spa->spa_proc_lock); | |
1191 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
1192 | ASSERT(spa->spa_proc == &p0); | |
1193 | spa->spa_did = 0; | |
1194 | ||
7b89a549 | 1195 | #ifdef HAVE_SPA_THREAD |
428870ff BB |
1196 | /* Only create a process if we're going to be around a while. */ |
1197 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
1198 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
1199 | NULL, 0) == 0) { | |
1200 | spa->spa_proc_state = SPA_PROC_CREATED; | |
1201 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
1202 | cv_wait(&spa->spa_proc_cv, | |
1203 | &spa->spa_proc_lock); | |
9babb374 | 1204 | } |
428870ff BB |
1205 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); |
1206 | ASSERT(spa->spa_proc != &p0); | |
1207 | ASSERT(spa->spa_did != 0); | |
1208 | } else { | |
1209 | #ifdef _KERNEL | |
1210 | cmn_err(CE_WARN, | |
1211 | "Couldn't create process for zfs pool \"%s\"\n", | |
1212 | spa->spa_name); | |
1213 | #endif | |
b128c09f | 1214 | } |
34dc7c2f | 1215 | } |
7b89a549 | 1216 | #endif /* HAVE_SPA_THREAD */ |
428870ff BB |
1217 | mutex_exit(&spa->spa_proc_lock); |
1218 | ||
1219 | /* If we didn't create a process, we need to create our taskqs. */ | |
1220 | if (spa->spa_proc == &p0) { | |
1221 | spa_create_zio_taskqs(spa); | |
1222 | } | |
34dc7c2f | 1223 | |
619f0976 GW |
1224 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1225 | spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, | |
1226 | ZIO_FLAG_CANFAIL); | |
1227 | } | |
a1d477c2 | 1228 | |
b128c09f BB |
1229 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), |
1230 | offsetof(vdev_t, vdev_config_dirty_node)); | |
0c66c32d JG |
1231 | list_create(&spa->spa_evicting_os_list, sizeof (objset_t), |
1232 | offsetof(objset_t, os_evicting_node)); | |
b128c09f BB |
1233 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), |
1234 | offsetof(vdev_t, vdev_state_dirty_node)); | |
34dc7c2f | 1235 | |
4747a7d3 | 1236 | txg_list_create(&spa->spa_vdev_txg_list, spa, |
34dc7c2f BB |
1237 | offsetof(struct vdev, vdev_txg_node)); |
1238 | ||
1239 | avl_create(&spa->spa_errlist_scrub, | |
1240 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1241 | offsetof(spa_error_entry_t, se_avl)); | |
1242 | avl_create(&spa->spa_errlist_last, | |
1243 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1244 | offsetof(spa_error_entry_t, se_avl)); | |
a0bd735a | 1245 | |
b5256303 TC |
1246 | spa_keystore_init(&spa->spa_keystore); |
1247 | ||
a0bd735a BP |
1248 | /* |
1249 | * This taskq is used to perform zvol-minor-related tasks | |
1250 | * asynchronously. This has several advantages, including easy | |
1251 | * resolution of various deadlocks (zfsonlinux bug #3681). | |
1252 | * | |
1253 | * The taskq must be single threaded to ensure tasks are always | |
1254 | * processed in the order in which they were dispatched. | |
1255 | * | |
1256 | * A taskq per pool allows one to keep the pools independent. | |
1257 | * This way if one pool is suspended, it will not impact another. | |
1258 | * | |
1259 | * The preferred location to dispatch a zvol minor task is a sync | |
1260 | * task. In this context, there is easy access to the spa_t and minimal | |
1261 | * error handling is required because the sync task must succeed. | |
1262 | */ | |
1263 | spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri, | |
1264 | 1, INT_MAX, 0); | |
1de321e6 | 1265 | |
77d8a0f1 | 1266 | /* |
1267 | * Taskq dedicated to prefetcher threads: this is used to prevent the | |
1268 | * pool traverse code from monopolizing the global (and limited) | |
1269 | * system_taskq by inappropriately scheduling long running tasks on it. | |
1270 | */ | |
1271 | spa->spa_prefetch_taskq = taskq_create("z_prefetch", boot_ncpus, | |
1272 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); | |
1273 | ||
1de321e6 JX |
1274 | /* |
1275 | * The taskq to upgrade datasets in this pool. Currently used by | |
9c5167d1 | 1276 | * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA. |
1de321e6 JX |
1277 | */ |
1278 | spa->spa_upgrade_taskq = taskq_create("z_upgrade", boot_ncpus, | |
1279 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); | |
34dc7c2f BB |
1280 | } |
1281 | ||
1282 | /* | |
1283 | * Opposite of spa_activate(). | |
1284 | */ | |
1285 | static void | |
1286 | spa_deactivate(spa_t *spa) | |
1287 | { | |
34dc7c2f BB |
1288 | ASSERT(spa->spa_sync_on == B_FALSE); |
1289 | ASSERT(spa->spa_dsl_pool == NULL); | |
1290 | ASSERT(spa->spa_root_vdev == NULL); | |
9babb374 | 1291 | ASSERT(spa->spa_async_zio_root == NULL); |
34dc7c2f BB |
1292 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); |
1293 | ||
0c66c32d JG |
1294 | spa_evicting_os_wait(spa); |
1295 | ||
a0bd735a BP |
1296 | if (spa->spa_zvol_taskq) { |
1297 | taskq_destroy(spa->spa_zvol_taskq); | |
1298 | spa->spa_zvol_taskq = NULL; | |
1299 | } | |
1300 | ||
77d8a0f1 | 1301 | if (spa->spa_prefetch_taskq) { |
1302 | taskq_destroy(spa->spa_prefetch_taskq); | |
1303 | spa->spa_prefetch_taskq = NULL; | |
1304 | } | |
1305 | ||
1de321e6 JX |
1306 | if (spa->spa_upgrade_taskq) { |
1307 | taskq_destroy(spa->spa_upgrade_taskq); | |
1308 | spa->spa_upgrade_taskq = NULL; | |
1309 | } | |
1310 | ||
34dc7c2f BB |
1311 | txg_list_destroy(&spa->spa_vdev_txg_list); |
1312 | ||
b128c09f | 1313 | list_destroy(&spa->spa_config_dirty_list); |
0c66c32d | 1314 | list_destroy(&spa->spa_evicting_os_list); |
b128c09f | 1315 | list_destroy(&spa->spa_state_dirty_list); |
34dc7c2f | 1316 | |
57ddcda1 | 1317 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 | 1318 | |
1c27024e DB |
1319 | for (int t = 0; t < ZIO_TYPES; t++) { |
1320 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1321 | spa_taskqs_fini(spa, t, q); |
b128c09f | 1322 | } |
34dc7c2f BB |
1323 | } |
1324 | ||
a1d477c2 MA |
1325 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1326 | ASSERT3P(spa->spa_txg_zio[i], !=, NULL); | |
1327 | VERIFY0(zio_wait(spa->spa_txg_zio[i])); | |
1328 | spa->spa_txg_zio[i] = NULL; | |
1329 | } | |
1330 | ||
34dc7c2f BB |
1331 | metaslab_class_destroy(spa->spa_normal_class); |
1332 | spa->spa_normal_class = NULL; | |
1333 | ||
1334 | metaslab_class_destroy(spa->spa_log_class); | |
1335 | spa->spa_log_class = NULL; | |
1336 | ||
cc99f275 DB |
1337 | metaslab_class_destroy(spa->spa_special_class); |
1338 | spa->spa_special_class = NULL; | |
1339 | ||
1340 | metaslab_class_destroy(spa->spa_dedup_class); | |
1341 | spa->spa_dedup_class = NULL; | |
1342 | ||
34dc7c2f BB |
1343 | /* |
1344 | * If this was part of an import or the open otherwise failed, we may | |
1345 | * still have errors left in the queues. Empty them just in case. | |
1346 | */ | |
1347 | spa_errlog_drain(spa); | |
34dc7c2f BB |
1348 | avl_destroy(&spa->spa_errlist_scrub); |
1349 | avl_destroy(&spa->spa_errlist_last); | |
1350 | ||
b5256303 TC |
1351 | spa_keystore_fini(&spa->spa_keystore); |
1352 | ||
34dc7c2f | 1353 | spa->spa_state = POOL_STATE_UNINITIALIZED; |
428870ff BB |
1354 | |
1355 | mutex_enter(&spa->spa_proc_lock); | |
1356 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1357 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1358 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1359 | cv_broadcast(&spa->spa_proc_cv); | |
1360 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1361 | ASSERT(spa->spa_proc != &p0); | |
1362 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1363 | } | |
1364 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1365 | spa->spa_proc_state = SPA_PROC_NONE; | |
1366 | } | |
1367 | ASSERT(spa->spa_proc == &p0); | |
1368 | mutex_exit(&spa->spa_proc_lock); | |
1369 | ||
1370 | /* | |
1371 | * We want to make sure spa_thread() has actually exited the ZFS | |
1372 | * module, so that the module can't be unloaded out from underneath | |
1373 | * it. | |
1374 | */ | |
1375 | if (spa->spa_did != 0) { | |
1376 | thread_join(spa->spa_did); | |
1377 | spa->spa_did = 0; | |
1378 | } | |
34dc7c2f BB |
1379 | } |
1380 | ||
1381 | /* | |
1382 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1383 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1384 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1385 | * All vdev validation is done by the vdev_alloc() routine. | |
1386 | */ | |
4a22ba5b | 1387 | int |
34dc7c2f BB |
1388 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, |
1389 | uint_t id, int atype) | |
1390 | { | |
1391 | nvlist_t **child; | |
9babb374 | 1392 | uint_t children; |
34dc7c2f BB |
1393 | int error; |
1394 | ||
1395 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1396 | return (error); | |
1397 | ||
1398 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1399 | return (0); | |
1400 | ||
b128c09f BB |
1401 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1402 | &child, &children); | |
1403 | ||
1404 | if (error == ENOENT) | |
1405 | return (0); | |
1406 | ||
1407 | if (error) { | |
34dc7c2f BB |
1408 | vdev_free(*vdp); |
1409 | *vdp = NULL; | |
2e528b49 | 1410 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1411 | } |
1412 | ||
1c27024e | 1413 | for (int c = 0; c < children; c++) { |
34dc7c2f BB |
1414 | vdev_t *vd; |
1415 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1416 | atype)) != 0) { | |
1417 | vdev_free(*vdp); | |
1418 | *vdp = NULL; | |
1419 | return (error); | |
1420 | } | |
1421 | } | |
1422 | ||
1423 | ASSERT(*vdp != NULL); | |
1424 | ||
1425 | return (0); | |
1426 | } | |
1427 | ||
93e28d66 SD |
1428 | static boolean_t |
1429 | spa_should_flush_logs_on_unload(spa_t *spa) | |
1430 | { | |
1431 | if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) | |
1432 | return (B_FALSE); | |
1433 | ||
1434 | if (!spa_writeable(spa)) | |
1435 | return (B_FALSE); | |
1436 | ||
1437 | if (!spa->spa_sync_on) | |
1438 | return (B_FALSE); | |
1439 | ||
1440 | if (spa_state(spa) != POOL_STATE_EXPORTED) | |
1441 | return (B_FALSE); | |
1442 | ||
1443 | if (zfs_keep_log_spacemaps_at_export) | |
1444 | return (B_FALSE); | |
1445 | ||
1446 | return (B_TRUE); | |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Opens a transaction that will set the flag that will instruct | |
1451 | * spa_sync to attempt to flush all the metaslabs for that txg. | |
1452 | */ | |
1453 | static void | |
1454 | spa_unload_log_sm_flush_all(spa_t *spa) | |
1455 | { | |
1456 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
1457 | VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); | |
1458 | ||
1459 | ASSERT3U(spa->spa_log_flushall_txg, ==, 0); | |
1460 | spa->spa_log_flushall_txg = dmu_tx_get_txg(tx); | |
1461 | ||
1462 | dmu_tx_commit(tx); | |
1463 | txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg); | |
1464 | } | |
1465 | ||
1466 | static void | |
1467 | spa_unload_log_sm_metadata(spa_t *spa) | |
1468 | { | |
1469 | void *cookie = NULL; | |
1470 | spa_log_sm_t *sls; | |
1471 | while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg, | |
1472 | &cookie)) != NULL) { | |
1473 | VERIFY0(sls->sls_mscount); | |
1474 | kmem_free(sls, sizeof (spa_log_sm_t)); | |
1475 | } | |
1476 | ||
1477 | for (log_summary_entry_t *e = list_head(&spa->spa_log_summary); | |
1478 | e != NULL; e = list_head(&spa->spa_log_summary)) { | |
1479 | VERIFY0(e->lse_mscount); | |
1480 | list_remove(&spa->spa_log_summary, e); | |
1481 | kmem_free(e, sizeof (log_summary_entry_t)); | |
1482 | } | |
1483 | ||
1484 | spa->spa_unflushed_stats.sus_nblocks = 0; | |
1485 | spa->spa_unflushed_stats.sus_memused = 0; | |
1486 | spa->spa_unflushed_stats.sus_blocklimit = 0; | |
1487 | } | |
1488 | ||
37f03da8 SH |
1489 | static void |
1490 | spa_destroy_aux_threads(spa_t *spa) | |
1491 | { | |
1492 | if (spa->spa_condense_zthr != NULL) { | |
1493 | zthr_destroy(spa->spa_condense_zthr); | |
1494 | spa->spa_condense_zthr = NULL; | |
1495 | } | |
1496 | if (spa->spa_checkpoint_discard_zthr != NULL) { | |
1497 | zthr_destroy(spa->spa_checkpoint_discard_zthr); | |
1498 | spa->spa_checkpoint_discard_zthr = NULL; | |
1499 | } | |
1500 | if (spa->spa_livelist_delete_zthr != NULL) { | |
1501 | zthr_destroy(spa->spa_livelist_delete_zthr); | |
1502 | spa->spa_livelist_delete_zthr = NULL; | |
1503 | } | |
1504 | if (spa->spa_livelist_condense_zthr != NULL) { | |
1505 | zthr_destroy(spa->spa_livelist_condense_zthr); | |
1506 | spa->spa_livelist_condense_zthr = NULL; | |
1507 | } | |
1508 | } | |
1509 | ||
34dc7c2f BB |
1510 | /* |
1511 | * Opposite of spa_load(). | |
1512 | */ | |
1513 | static void | |
1514 | spa_unload(spa_t *spa) | |
1515 | { | |
b128c09f | 1516 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
93e28d66 | 1517 | ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED); |
b128c09f | 1518 | |
ca95f70d | 1519 | spa_import_progress_remove(spa_guid(spa)); |
4a0ee12a PZ |
1520 | spa_load_note(spa, "UNLOADING"); |
1521 | ||
e60e158e JG |
1522 | spa_wake_waiters(spa); |
1523 | ||
93e28d66 SD |
1524 | /* |
1525 | * If the log space map feature is enabled and the pool is getting | |
1526 | * exported (but not destroyed), we want to spend some time flushing | |
1527 | * as many metaslabs as we can in an attempt to destroy log space | |
1528 | * maps and save import time. | |
1529 | */ | |
1530 | if (spa_should_flush_logs_on_unload(spa)) | |
1531 | spa_unload_log_sm_flush_all(spa); | |
1532 | ||
34dc7c2f BB |
1533 | /* |
1534 | * Stop async tasks. | |
1535 | */ | |
1536 | spa_async_suspend(spa); | |
1537 | ||
619f0976 | 1538 | if (spa->spa_root_vdev) { |
1b939560 BB |
1539 | vdev_t *root_vdev = spa->spa_root_vdev; |
1540 | vdev_initialize_stop_all(root_vdev, VDEV_INITIALIZE_ACTIVE); | |
1541 | vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE); | |
1542 | vdev_autotrim_stop_all(spa); | |
9a49d3f3 | 1543 | vdev_rebuild_stop_all(spa); |
619f0976 GW |
1544 | } |
1545 | ||
34dc7c2f BB |
1546 | /* |
1547 | * Stop syncing. | |
1548 | */ | |
1549 | if (spa->spa_sync_on) { | |
1550 | txg_sync_stop(spa->spa_dsl_pool); | |
1551 | spa->spa_sync_on = B_FALSE; | |
1552 | } | |
1553 | ||
4e21fd06 | 1554 | /* |
93e28d66 SD |
1555 | * This ensures that there is no async metaslab prefetching |
1556 | * while we attempt to unload the spa. | |
4e21fd06 DB |
1557 | */ |
1558 | if (spa->spa_root_vdev != NULL) { | |
93e28d66 SD |
1559 | for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) { |
1560 | vdev_t *vc = spa->spa_root_vdev->vdev_child[c]; | |
1561 | if (vc->vdev_mg != NULL) | |
1562 | taskq_wait(vc->vdev_mg->mg_taskq); | |
1563 | } | |
4e21fd06 DB |
1564 | } |
1565 | ||
379ca9cf OF |
1566 | if (spa->spa_mmp.mmp_thread) |
1567 | mmp_thread_stop(spa); | |
1568 | ||
34dc7c2f | 1569 | /* |
b128c09f | 1570 | * Wait for any outstanding async I/O to complete. |
34dc7c2f | 1571 | */ |
9babb374 | 1572 | if (spa->spa_async_zio_root != NULL) { |
1c27024e | 1573 | for (int i = 0; i < max_ncpus; i++) |
e022864d MA |
1574 | (void) zio_wait(spa->spa_async_zio_root[i]); |
1575 | kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); | |
9babb374 BB |
1576 | spa->spa_async_zio_root = NULL; |
1577 | } | |
34dc7c2f | 1578 | |
a1d477c2 MA |
1579 | if (spa->spa_vdev_removal != NULL) { |
1580 | spa_vdev_removal_destroy(spa->spa_vdev_removal); | |
1581 | spa->spa_vdev_removal = NULL; | |
1582 | } | |
1583 | ||
37f03da8 | 1584 | spa_destroy_aux_threads(spa); |
d2734cce | 1585 | |
a1d477c2 MA |
1586 | spa_condense_fini(spa); |
1587 | ||
428870ff BB |
1588 | bpobj_close(&spa->spa_deferred_bpobj); |
1589 | ||
619f0976 | 1590 | spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); |
93cf2076 GW |
1591 | |
1592 | /* | |
1593 | * Close all vdevs. | |
1594 | */ | |
1595 | if (spa->spa_root_vdev) | |
1596 | vdev_free(spa->spa_root_vdev); | |
1597 | ASSERT(spa->spa_root_vdev == NULL); | |
1598 | ||
34dc7c2f BB |
1599 | /* |
1600 | * Close the dsl pool. | |
1601 | */ | |
1602 | if (spa->spa_dsl_pool) { | |
1603 | dsl_pool_close(spa->spa_dsl_pool); | |
1604 | spa->spa_dsl_pool = NULL; | |
428870ff | 1605 | spa->spa_meta_objset = NULL; |
34dc7c2f BB |
1606 | } |
1607 | ||
428870ff | 1608 | ddt_unload(spa); |
93e28d66 | 1609 | spa_unload_log_sm_metadata(spa); |
428870ff | 1610 | |
fb5f0bc8 BB |
1611 | /* |
1612 | * Drop and purge level 2 cache | |
1613 | */ | |
1614 | spa_l2cache_drop(spa); | |
1615 | ||
93e28d66 | 1616 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
34dc7c2f BB |
1617 | vdev_free(spa->spa_spares.sav_vdevs[i]); |
1618 | if (spa->spa_spares.sav_vdevs) { | |
1619 | kmem_free(spa->spa_spares.sav_vdevs, | |
1620 | spa->spa_spares.sav_count * sizeof (void *)); | |
1621 | spa->spa_spares.sav_vdevs = NULL; | |
1622 | } | |
1623 | if (spa->spa_spares.sav_config) { | |
1624 | nvlist_free(spa->spa_spares.sav_config); | |
1625 | spa->spa_spares.sav_config = NULL; | |
1626 | } | |
b128c09f | 1627 | spa->spa_spares.sav_count = 0; |
34dc7c2f | 1628 | |
93e28d66 | 1629 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { |
5ffb9d1d | 1630 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); |
34dc7c2f | 1631 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); |
5ffb9d1d | 1632 | } |
34dc7c2f BB |
1633 | if (spa->spa_l2cache.sav_vdevs) { |
1634 | kmem_free(spa->spa_l2cache.sav_vdevs, | |
1635 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1636 | spa->spa_l2cache.sav_vdevs = NULL; | |
1637 | } | |
1638 | if (spa->spa_l2cache.sav_config) { | |
1639 | nvlist_free(spa->spa_l2cache.sav_config); | |
1640 | spa->spa_l2cache.sav_config = NULL; | |
1641 | } | |
b128c09f | 1642 | spa->spa_l2cache.sav_count = 0; |
34dc7c2f BB |
1643 | |
1644 | spa->spa_async_suspended = 0; | |
fb5f0bc8 | 1645 | |
a1d477c2 MA |
1646 | spa->spa_indirect_vdevs_loaded = B_FALSE; |
1647 | ||
d96eb2b1 DM |
1648 | if (spa->spa_comment != NULL) { |
1649 | spa_strfree(spa->spa_comment); | |
1650 | spa->spa_comment = NULL; | |
1651 | } | |
1652 | ||
619f0976 | 1653 | spa_config_exit(spa, SCL_ALL, spa); |
34dc7c2f BB |
1654 | } |
1655 | ||
1656 | /* | |
1657 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1658 | * this pool. When this is called, we have some form of basic information in | |
1659 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1660 | * then re-generate a more complete list including status information. | |
1661 | */ | |
a1d477c2 | 1662 | void |
34dc7c2f BB |
1663 | spa_load_spares(spa_t *spa) |
1664 | { | |
1665 | nvlist_t **spares; | |
1666 | uint_t nspares; | |
1667 | int i; | |
1668 | vdev_t *vd, *tvd; | |
1669 | ||
d2734cce SD |
1670 | #ifndef _KERNEL |
1671 | /* | |
1672 | * zdb opens both the current state of the pool and the | |
1673 | * checkpointed state (if present), with a different spa_t. | |
1674 | * | |
1675 | * As spare vdevs are shared among open pools, we skip loading | |
1676 | * them when we load the checkpointed state of the pool. | |
1677 | */ | |
1678 | if (!spa_writeable(spa)) | |
1679 | return; | |
1680 | #endif | |
1681 | ||
b128c09f BB |
1682 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1683 | ||
34dc7c2f BB |
1684 | /* |
1685 | * First, close and free any existing spare vdevs. | |
1686 | */ | |
1687 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1688 | vd = spa->spa_spares.sav_vdevs[i]; | |
1689 | ||
1690 | /* Undo the call to spa_activate() below */ | |
b128c09f BB |
1691 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1692 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
34dc7c2f BB |
1693 | spa_spare_remove(tvd); |
1694 | vdev_close(vd); | |
1695 | vdev_free(vd); | |
1696 | } | |
1697 | ||
1698 | if (spa->spa_spares.sav_vdevs) | |
1699 | kmem_free(spa->spa_spares.sav_vdevs, | |
1700 | spa->spa_spares.sav_count * sizeof (void *)); | |
1701 | ||
1702 | if (spa->spa_spares.sav_config == NULL) | |
1703 | nspares = 0; | |
1704 | else | |
1705 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
1706 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
1707 | ||
1708 | spa->spa_spares.sav_count = (int)nspares; | |
1709 | spa->spa_spares.sav_vdevs = NULL; | |
1710 | ||
1711 | if (nspares == 0) | |
1712 | return; | |
1713 | ||
1714 | /* | |
1715 | * Construct the array of vdevs, opening them to get status in the | |
1716 | * process. For each spare, there is potentially two different vdev_t | |
1717 | * structures associated with it: one in the list of spares (used only | |
1718 | * for basic validation purposes) and one in the active vdev | |
1719 | * configuration (if it's spared in). During this phase we open and | |
1720 | * validate each vdev on the spare list. If the vdev also exists in the | |
1721 | * active configuration, then we also mark this vdev as an active spare. | |
1722 | */ | |
904ea276 | 1723 | spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *), |
79c76d5b | 1724 | KM_SLEEP); |
34dc7c2f BB |
1725 | for (i = 0; i < spa->spa_spares.sav_count; i++) { |
1726 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1727 | VDEV_ALLOC_SPARE) == 0); | |
1728 | ASSERT(vd != NULL); | |
1729 | ||
1730 | spa->spa_spares.sav_vdevs[i] = vd; | |
1731 | ||
b128c09f BB |
1732 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1733 | B_FALSE)) != NULL) { | |
34dc7c2f BB |
1734 | if (!tvd->vdev_isspare) |
1735 | spa_spare_add(tvd); | |
1736 | ||
1737 | /* | |
1738 | * We only mark the spare active if we were successfully | |
1739 | * able to load the vdev. Otherwise, importing a pool | |
1740 | * with a bad active spare would result in strange | |
1741 | * behavior, because multiple pool would think the spare | |
1742 | * is actively in use. | |
1743 | * | |
1744 | * There is a vulnerability here to an equally bizarre | |
1745 | * circumstance, where a dead active spare is later | |
1746 | * brought back to life (onlined or otherwise). Given | |
1747 | * the rarity of this scenario, and the extra complexity | |
1748 | * it adds, we ignore the possibility. | |
1749 | */ | |
1750 | if (!vdev_is_dead(tvd)) | |
1751 | spa_spare_activate(tvd); | |
1752 | } | |
1753 | ||
b128c09f | 1754 | vd->vdev_top = vd; |
9babb374 | 1755 | vd->vdev_aux = &spa->spa_spares; |
b128c09f | 1756 | |
34dc7c2f BB |
1757 | if (vdev_open(vd) != 0) |
1758 | continue; | |
1759 | ||
34dc7c2f BB |
1760 | if (vdev_validate_aux(vd) == 0) |
1761 | spa_spare_add(vd); | |
1762 | } | |
1763 | ||
1764 | /* | |
1765 | * Recompute the stashed list of spares, with status information | |
1766 | * this time. | |
1767 | */ | |
1768 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, | |
1769 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1770 | ||
1771 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
79c76d5b | 1772 | KM_SLEEP); |
34dc7c2f BB |
1773 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1774 | spares[i] = vdev_config_generate(spa, | |
428870ff | 1775 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); |
34dc7c2f BB |
1776 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
1777 | ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); | |
1778 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1779 | nvlist_free(spares[i]); | |
1780 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1781 | } | |
1782 | ||
1783 | /* | |
1784 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1785 | * this pool. When this is called, we have some form of basic information in | |
1786 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1787 | * then re-generate a more complete list including status information. | |
1788 | * Devices which are already active have their details maintained, and are | |
1789 | * not re-opened. | |
1790 | */ | |
a1d477c2 | 1791 | void |
34dc7c2f BB |
1792 | spa_load_l2cache(spa_t *spa) |
1793 | { | |
460f239e | 1794 | nvlist_t **l2cache = NULL; |
34dc7c2f BB |
1795 | uint_t nl2cache; |
1796 | int i, j, oldnvdevs; | |
9babb374 | 1797 | uint64_t guid; |
a117a6d6 | 1798 | vdev_t *vd, **oldvdevs, **newvdevs; |
34dc7c2f BB |
1799 | spa_aux_vdev_t *sav = &spa->spa_l2cache; |
1800 | ||
d2734cce SD |
1801 | #ifndef _KERNEL |
1802 | /* | |
1803 | * zdb opens both the current state of the pool and the | |
1804 | * checkpointed state (if present), with a different spa_t. | |
1805 | * | |
1806 | * As L2 caches are part of the ARC which is shared among open | |
1807 | * pools, we skip loading them when we load the checkpointed | |
1808 | * state of the pool. | |
1809 | */ | |
1810 | if (!spa_writeable(spa)) | |
1811 | return; | |
1812 | #endif | |
1813 | ||
b128c09f BB |
1814 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1815 | ||
34dc7c2f BB |
1816 | oldvdevs = sav->sav_vdevs; |
1817 | oldnvdevs = sav->sav_count; | |
1818 | sav->sav_vdevs = NULL; | |
1819 | sav->sav_count = 0; | |
1820 | ||
67d60824 NB |
1821 | if (sav->sav_config == NULL) { |
1822 | nl2cache = 0; | |
1823 | newvdevs = NULL; | |
1824 | goto out; | |
1825 | } | |
1826 | ||
1827 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, | |
1828 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
1829 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); | |
1830 | ||
34dc7c2f BB |
1831 | /* |
1832 | * Process new nvlist of vdevs. | |
1833 | */ | |
1834 | for (i = 0; i < nl2cache; i++) { | |
1835 | VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, | |
1836 | &guid) == 0); | |
1837 | ||
1838 | newvdevs[i] = NULL; | |
1839 | for (j = 0; j < oldnvdevs; j++) { | |
1840 | vd = oldvdevs[j]; | |
1841 | if (vd != NULL && guid == vd->vdev_guid) { | |
1842 | /* | |
1843 | * Retain previous vdev for add/remove ops. | |
1844 | */ | |
1845 | newvdevs[i] = vd; | |
1846 | oldvdevs[j] = NULL; | |
1847 | break; | |
1848 | } | |
1849 | } | |
1850 | ||
1851 | if (newvdevs[i] == NULL) { | |
1852 | /* | |
1853 | * Create new vdev | |
1854 | */ | |
1855 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
1856 | VDEV_ALLOC_L2CACHE) == 0); | |
1857 | ASSERT(vd != NULL); | |
1858 | newvdevs[i] = vd; | |
1859 | ||
1860 | /* | |
1861 | * Commit this vdev as an l2cache device, | |
1862 | * even if it fails to open. | |
1863 | */ | |
1864 | spa_l2cache_add(vd); | |
1865 | ||
b128c09f BB |
1866 | vd->vdev_top = vd; |
1867 | vd->vdev_aux = sav; | |
1868 | ||
1869 | spa_l2cache_activate(vd); | |
1870 | ||
34dc7c2f BB |
1871 | if (vdev_open(vd) != 0) |
1872 | continue; | |
1873 | ||
34dc7c2f BB |
1874 | (void) vdev_validate_aux(vd); |
1875 | ||
9babb374 BB |
1876 | if (!vdev_is_dead(vd)) |
1877 | l2arc_add_vdev(spa, vd); | |
b7654bd7 GA |
1878 | |
1879 | /* | |
1880 | * Upon cache device addition to a pool or pool | |
1881 | * creation with a cache device or if the header | |
1882 | * of the device is invalid we issue an async | |
1883 | * TRIM command for the whole device which will | |
1884 | * execute if l2arc_trim_ahead > 0. | |
1885 | */ | |
1886 | spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM); | |
34dc7c2f BB |
1887 | } |
1888 | } | |
1889 | ||
67d60824 NB |
1890 | sav->sav_vdevs = newvdevs; |
1891 | sav->sav_count = (int)nl2cache; | |
1892 | ||
1893 | /* | |
1894 | * Recompute the stashed list of l2cache devices, with status | |
1895 | * information this time. | |
1896 | */ | |
1897 | VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, | |
1898 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1899 | ||
460f239e D |
1900 | if (sav->sav_count > 0) |
1901 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), | |
1902 | KM_SLEEP); | |
67d60824 NB |
1903 | for (i = 0; i < sav->sav_count; i++) |
1904 | l2cache[i] = vdev_config_generate(spa, | |
1905 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); | |
1906 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
1907 | ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); | |
1908 | ||
1909 | out: | |
34dc7c2f BB |
1910 | /* |
1911 | * Purge vdevs that were dropped | |
1912 | */ | |
1913 | for (i = 0; i < oldnvdevs; i++) { | |
1914 | uint64_t pool; | |
1915 | ||
1916 | vd = oldvdevs[i]; | |
1917 | if (vd != NULL) { | |
5ffb9d1d GW |
1918 | ASSERT(vd->vdev_isl2cache); |
1919 | ||
fb5f0bc8 BB |
1920 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
1921 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 1922 | l2arc_remove_vdev(vd); |
5ffb9d1d GW |
1923 | vdev_clear_stats(vd); |
1924 | vdev_free(vd); | |
34dc7c2f BB |
1925 | } |
1926 | } | |
1927 | ||
1928 | if (oldvdevs) | |
1929 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); | |
1930 | ||
34dc7c2f BB |
1931 | for (i = 0; i < sav->sav_count; i++) |
1932 | nvlist_free(l2cache[i]); | |
1933 | if (sav->sav_count) | |
1934 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
1935 | } | |
1936 | ||
1937 | static int | |
1938 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
1939 | { | |
1940 | dmu_buf_t *db; | |
1941 | char *packed = NULL; | |
1942 | size_t nvsize = 0; | |
1943 | int error; | |
1944 | *value = NULL; | |
1945 | ||
c3275b56 BB |
1946 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); |
1947 | if (error) | |
1948 | return (error); | |
1949 | ||
34dc7c2f BB |
1950 | nvsize = *(uint64_t *)db->db_data; |
1951 | dmu_buf_rele(db, FTAG); | |
1952 | ||
77aef6f6 | 1953 | packed = vmem_alloc(nvsize, KM_SLEEP); |
9babb374 BB |
1954 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, |
1955 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
1956 | if (error == 0) |
1957 | error = nvlist_unpack(packed, nvsize, value, 0); | |
77aef6f6 | 1958 | vmem_free(packed, nvsize); |
34dc7c2f BB |
1959 | |
1960 | return (error); | |
1961 | } | |
1962 | ||
6cb8e530 PZ |
1963 | /* |
1964 | * Concrete top-level vdevs that are not missing and are not logs. At every | |
1965 | * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. | |
1966 | */ | |
1967 | static uint64_t | |
1968 | spa_healthy_core_tvds(spa_t *spa) | |
1969 | { | |
1970 | vdev_t *rvd = spa->spa_root_vdev; | |
1971 | uint64_t tvds = 0; | |
1972 | ||
1973 | for (uint64_t i = 0; i < rvd->vdev_children; i++) { | |
1974 | vdev_t *vd = rvd->vdev_child[i]; | |
1975 | if (vd->vdev_islog) | |
1976 | continue; | |
1977 | if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) | |
1978 | tvds++; | |
1979 | } | |
1980 | ||
1981 | return (tvds); | |
1982 | } | |
1983 | ||
34dc7c2f BB |
1984 | /* |
1985 | * Checks to see if the given vdev could not be opened, in which case we post a | |
1986 | * sysevent to notify the autoreplace code that the device has been removed. | |
1987 | */ | |
1988 | static void | |
1989 | spa_check_removed(vdev_t *vd) | |
1990 | { | |
6cb8e530 | 1991 | for (uint64_t c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
1992 | spa_check_removed(vd->vdev_child[c]); |
1993 | ||
7011fb60 | 1994 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && |
a1d477c2 | 1995 | vdev_is_concrete(vd)) { |
fb390aaf | 1996 | zfs_post_autoreplace(vd->vdev_spa, vd); |
12fa0466 | 1997 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); |
34dc7c2f BB |
1998 | } |
1999 | } | |
2000 | ||
6cb8e530 PZ |
2001 | static int |
2002 | spa_check_for_missing_logs(spa_t *spa) | |
9babb374 | 2003 | { |
6cb8e530 | 2004 | vdev_t *rvd = spa->spa_root_vdev; |
9babb374 | 2005 | |
428870ff | 2006 | /* |
572e2857 | 2007 | * If we're doing a normal import, then build up any additional |
6cb8e530 | 2008 | * diagnostic information about missing log devices. |
572e2857 | 2009 | * We'll pass this up to the user for further processing. |
428870ff | 2010 | */ |
572e2857 BB |
2011 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { |
2012 | nvlist_t **child, *nv; | |
2013 | uint64_t idx = 0; | |
2014 | ||
160987b5 | 2015 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *), |
79c76d5b BB |
2016 | KM_SLEEP); |
2017 | VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
572e2857 | 2018 | |
6cb8e530 | 2019 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { |
572e2857 | 2020 | vdev_t *tvd = rvd->vdev_child[c]; |
572e2857 | 2021 | |
6cb8e530 PZ |
2022 | /* |
2023 | * We consider a device as missing only if it failed | |
2024 | * to open (i.e. offline or faulted is not considered | |
2025 | * as missing). | |
2026 | */ | |
2027 | if (tvd->vdev_islog && | |
2028 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
2029 | child[idx++] = vdev_config_generate(spa, tvd, | |
2030 | B_FALSE, VDEV_CONFIG_MISSING); | |
2031 | } | |
572e2857 | 2032 | } |
9babb374 | 2033 | |
6cb8e530 PZ |
2034 | if (idx > 0) { |
2035 | fnvlist_add_nvlist_array(nv, | |
2036 | ZPOOL_CONFIG_CHILDREN, child, idx); | |
2037 | fnvlist_add_nvlist(spa->spa_load_info, | |
2038 | ZPOOL_CONFIG_MISSING_DEVICES, nv); | |
572e2857 | 2039 | |
6cb8e530 | 2040 | for (uint64_t i = 0; i < idx; i++) |
572e2857 BB |
2041 | nvlist_free(child[i]); |
2042 | } | |
2043 | nvlist_free(nv); | |
2044 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
572e2857 | 2045 | |
6cb8e530 PZ |
2046 | if (idx > 0) { |
2047 | spa_load_failed(spa, "some log devices are missing"); | |
db7d07e1 | 2048 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 PZ |
2049 | return (SET_ERROR(ENXIO)); |
2050 | } | |
2051 | } else { | |
2052 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
2053 | vdev_t *tvd = rvd->vdev_child[c]; | |
a1d477c2 | 2054 | |
6cb8e530 PZ |
2055 | if (tvd->vdev_islog && |
2056 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
572e2857 | 2057 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
6cb8e530 PZ |
2058 | spa_load_note(spa, "some log devices are " |
2059 | "missing, ZIL is dropped."); | |
db7d07e1 | 2060 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 | 2061 | break; |
e0ab3ab5 | 2062 | } |
572e2857 | 2063 | } |
9babb374 | 2064 | } |
e0ab3ab5 | 2065 | |
6cb8e530 | 2066 | return (0); |
9babb374 BB |
2067 | } |
2068 | ||
b128c09f BB |
2069 | /* |
2070 | * Check for missing log devices | |
2071 | */ | |
13fe0198 | 2072 | static boolean_t |
b128c09f BB |
2073 | spa_check_logs(spa_t *spa) |
2074 | { | |
13fe0198 | 2075 | boolean_t rv = B_FALSE; |
9c43027b | 2076 | dsl_pool_t *dp = spa_get_dsl(spa); |
13fe0198 | 2077 | |
b128c09f | 2078 | switch (spa->spa_log_state) { |
e75c13c3 BB |
2079 | default: |
2080 | break; | |
b128c09f BB |
2081 | case SPA_LOG_MISSING: |
2082 | /* need to recheck in case slog has been restored */ | |
2083 | case SPA_LOG_UNKNOWN: | |
9c43027b AJ |
2084 | rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, |
2085 | zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); | |
13fe0198 | 2086 | if (rv) |
428870ff | 2087 | spa_set_log_state(spa, SPA_LOG_MISSING); |
b128c09f | 2088 | break; |
b128c09f | 2089 | } |
13fe0198 | 2090 | return (rv); |
b128c09f BB |
2091 | } |
2092 | ||
428870ff BB |
2093 | static boolean_t |
2094 | spa_passivate_log(spa_t *spa) | |
34dc7c2f | 2095 | { |
428870ff BB |
2096 | vdev_t *rvd = spa->spa_root_vdev; |
2097 | boolean_t slog_found = B_FALSE; | |
b128c09f | 2098 | |
428870ff | 2099 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
fb5f0bc8 | 2100 | |
428870ff BB |
2101 | if (!spa_has_slogs(spa)) |
2102 | return (B_FALSE); | |
34dc7c2f | 2103 | |
1c27024e | 2104 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
2105 | vdev_t *tvd = rvd->vdev_child[c]; |
2106 | metaslab_group_t *mg = tvd->vdev_mg; | |
34dc7c2f | 2107 | |
428870ff BB |
2108 | if (tvd->vdev_islog) { |
2109 | metaslab_group_passivate(mg); | |
2110 | slog_found = B_TRUE; | |
2111 | } | |
34dc7c2f BB |
2112 | } |
2113 | ||
428870ff BB |
2114 | return (slog_found); |
2115 | } | |
34dc7c2f | 2116 | |
428870ff BB |
2117 | static void |
2118 | spa_activate_log(spa_t *spa) | |
2119 | { | |
2120 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 2121 | |
428870ff BB |
2122 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
2123 | ||
1c27024e | 2124 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
2125 | vdev_t *tvd = rvd->vdev_child[c]; |
2126 | metaslab_group_t *mg = tvd->vdev_mg; | |
2127 | ||
2128 | if (tvd->vdev_islog) | |
2129 | metaslab_group_activate(mg); | |
34dc7c2f | 2130 | } |
428870ff | 2131 | } |
34dc7c2f | 2132 | |
428870ff | 2133 | int |
a1d477c2 | 2134 | spa_reset_logs(spa_t *spa) |
428870ff | 2135 | { |
13fe0198 | 2136 | int error; |
9babb374 | 2137 | |
a1d477c2 | 2138 | error = dmu_objset_find(spa_name(spa), zil_reset, |
13fe0198 MA |
2139 | NULL, DS_FIND_CHILDREN); |
2140 | if (error == 0) { | |
428870ff BB |
2141 | /* |
2142 | * We successfully offlined the log device, sync out the | |
2143 | * current txg so that the "stubby" block can be removed | |
2144 | * by zil_sync(). | |
2145 | */ | |
2146 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
2147 | } | |
2148 | return (error); | |
2149 | } | |
34dc7c2f | 2150 | |
428870ff BB |
2151 | static void |
2152 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
2153 | { | |
1c27024e | 2154 | for (int i = 0; i < sav->sav_count; i++) |
428870ff BB |
2155 | spa_check_removed(sav->sav_vdevs[i]); |
2156 | } | |
34dc7c2f | 2157 | |
428870ff BB |
2158 | void |
2159 | spa_claim_notify(zio_t *zio) | |
2160 | { | |
2161 | spa_t *spa = zio->io_spa; | |
34dc7c2f | 2162 | |
428870ff BB |
2163 | if (zio->io_error) |
2164 | return; | |
34dc7c2f | 2165 | |
428870ff BB |
2166 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ |
2167 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
2168 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
2169 | mutex_exit(&spa->spa_props_lock); | |
2170 | } | |
34dc7c2f | 2171 | |
428870ff BB |
2172 | typedef struct spa_load_error { |
2173 | uint64_t sle_meta_count; | |
2174 | uint64_t sle_data_count; | |
2175 | } spa_load_error_t; | |
34dc7c2f | 2176 | |
428870ff BB |
2177 | static void |
2178 | spa_load_verify_done(zio_t *zio) | |
2179 | { | |
2180 | blkptr_t *bp = zio->io_bp; | |
2181 | spa_load_error_t *sle = zio->io_private; | |
2182 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
2183 | int error = zio->io_error; | |
dea377c0 | 2184 | spa_t *spa = zio->io_spa; |
34dc7c2f | 2185 | |
a6255b7f | 2186 | abd_free(zio->io_abd); |
428870ff | 2187 | if (error) { |
9ae529ec | 2188 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && |
428870ff | 2189 | type != DMU_OT_INTENT_LOG) |
bc89ac84 | 2190 | atomic_inc_64(&sle->sle_meta_count); |
428870ff | 2191 | else |
bc89ac84 | 2192 | atomic_inc_64(&sle->sle_data_count); |
34dc7c2f | 2193 | } |
dea377c0 MA |
2194 | |
2195 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2196 | spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp); |
dea377c0 MA |
2197 | cv_broadcast(&spa->spa_scrub_io_cv); |
2198 | mutex_exit(&spa->spa_scrub_lock); | |
428870ff | 2199 | } |
34dc7c2f | 2200 | |
dea377c0 | 2201 | /* |
e1cfd73f | 2202 | * Maximum number of inflight bytes is the log2 fraction of the arc size. |
c8242a96 | 2203 | * By default, we set it to 1/16th of the arc. |
dea377c0 | 2204 | */ |
c8242a96 | 2205 | int spa_load_verify_shift = 4; |
dea377c0 MA |
2206 | int spa_load_verify_metadata = B_TRUE; |
2207 | int spa_load_verify_data = B_TRUE; | |
2208 | ||
428870ff BB |
2209 | /*ARGSUSED*/ |
2210 | static int | |
2211 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
5dbd68a3 | 2212 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) |
428870ff | 2213 | { |
30af21b0 PD |
2214 | if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) || |
2215 | BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp)) | |
dea377c0 MA |
2216 | return (0); |
2217 | /* | |
2218 | * Note: normally this routine will not be called if | |
2219 | * spa_load_verify_metadata is not set. However, it may be useful | |
2220 | * to manually set the flag after the traversal has begun. | |
2221 | */ | |
2222 | if (!spa_load_verify_metadata) | |
2223 | return (0); | |
a6255b7f | 2224 | if (!BP_IS_METADATA(bp) && !spa_load_verify_data) |
dea377c0 MA |
2225 | return (0); |
2226 | ||
1e527162 GW |
2227 | uint64_t maxinflight_bytes = |
2228 | arc_target_bytes() >> spa_load_verify_shift; | |
1c27024e DB |
2229 | zio_t *rio = arg; |
2230 | size_t size = BP_GET_PSIZE(bp); | |
dea377c0 MA |
2231 | |
2232 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2233 | while (spa->spa_load_verify_bytes >= maxinflight_bytes) |
dea377c0 | 2234 | cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); |
c8242a96 | 2235 | spa->spa_load_verify_bytes += size; |
dea377c0 MA |
2236 | mutex_exit(&spa->spa_scrub_lock); |
2237 | ||
a6255b7f | 2238 | zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, |
dea377c0 MA |
2239 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, |
2240 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
2241 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
428870ff BB |
2242 | return (0); |
2243 | } | |
34dc7c2f | 2244 | |
d1d19c78 | 2245 | /* ARGSUSED */ |
65c7cc49 | 2246 | static int |
d1d19c78 PD |
2247 | verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) |
2248 | { | |
2249 | if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) | |
2250 | return (SET_ERROR(ENAMETOOLONG)); | |
2251 | ||
2252 | return (0); | |
2253 | } | |
2254 | ||
428870ff BB |
2255 | static int |
2256 | spa_load_verify(spa_t *spa) | |
2257 | { | |
2258 | zio_t *rio; | |
2259 | spa_load_error_t sle = { 0 }; | |
8a393be3 | 2260 | zpool_load_policy_t policy; |
428870ff | 2261 | boolean_t verify_ok = B_FALSE; |
dea377c0 | 2262 | int error = 0; |
34dc7c2f | 2263 | |
8a393be3 | 2264 | zpool_get_load_policy(spa->spa_config, &policy); |
34dc7c2f | 2265 | |
8a393be3 | 2266 | if (policy.zlp_rewind & ZPOOL_NEVER_REWIND) |
428870ff | 2267 | return (0); |
34dc7c2f | 2268 | |
d1d19c78 PD |
2269 | dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); |
2270 | error = dmu_objset_find_dp(spa->spa_dsl_pool, | |
2271 | spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, | |
2272 | DS_FIND_CHILDREN); | |
2273 | dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); | |
2274 | if (error != 0) | |
2275 | return (error); | |
2276 | ||
428870ff BB |
2277 | rio = zio_root(spa, NULL, &sle, |
2278 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
34dc7c2f | 2279 | |
dea377c0 | 2280 | if (spa_load_verify_metadata) { |
4a0ee12a PZ |
2281 | if (spa->spa_extreme_rewind) { |
2282 | spa_load_note(spa, "performing a complete scan of the " | |
2283 | "pool since extreme rewind is on. This may take " | |
2284 | "a very long time.\n (spa_load_verify_data=%u, " | |
2285 | "spa_load_verify_metadata=%u)", | |
2286 | spa_load_verify_data, spa_load_verify_metadata); | |
2287 | } | |
c8242a96 | 2288 | |
dea377c0 | 2289 | error = traverse_pool(spa, spa->spa_verify_min_txg, |
b5256303 TC |
2290 | TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | |
2291 | TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio); | |
dea377c0 | 2292 | } |
428870ff BB |
2293 | |
2294 | (void) zio_wait(rio); | |
c8242a96 | 2295 | ASSERT0(spa->spa_load_verify_bytes); |
428870ff BB |
2296 | |
2297 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
2298 | spa->spa_load_data_errors = sle.sle_data_count; | |
2299 | ||
afd2f7b7 PZ |
2300 | if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { |
2301 | spa_load_note(spa, "spa_load_verify found %llu metadata errors " | |
2302 | "and %llu data errors", (u_longlong_t)sle.sle_meta_count, | |
2303 | (u_longlong_t)sle.sle_data_count); | |
2304 | } | |
2305 | ||
2306 | if (spa_load_verify_dryrun || | |
8a393be3 PZ |
2307 | (!error && sle.sle_meta_count <= policy.zlp_maxmeta && |
2308 | sle.sle_data_count <= policy.zlp_maxdata)) { | |
572e2857 BB |
2309 | int64_t loss = 0; |
2310 | ||
428870ff BB |
2311 | verify_ok = B_TRUE; |
2312 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
2313 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
572e2857 BB |
2314 | |
2315 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
2316 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2317 | ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); | |
2318 | VERIFY(nvlist_add_int64(spa->spa_load_info, | |
2319 | ZPOOL_CONFIG_REWIND_TIME, loss) == 0); | |
2320 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2321 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); | |
428870ff BB |
2322 | } else { |
2323 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
2324 | } | |
2325 | ||
afd2f7b7 PZ |
2326 | if (spa_load_verify_dryrun) |
2327 | return (0); | |
2328 | ||
428870ff BB |
2329 | if (error) { |
2330 | if (error != ENXIO && error != EIO) | |
2e528b49 | 2331 | error = SET_ERROR(EIO); |
428870ff BB |
2332 | return (error); |
2333 | } | |
2334 | ||
2335 | return (verify_ok ? 0 : EIO); | |
2336 | } | |
2337 | ||
2338 | /* | |
2339 | * Find a value in the pool props object. | |
2340 | */ | |
2341 | static void | |
2342 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
2343 | { | |
2344 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
2345 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
2346 | } | |
2347 | ||
2348 | /* | |
2349 | * Find a value in the pool directory object. | |
2350 | */ | |
2351 | static int | |
4a0ee12a | 2352 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) |
428870ff | 2353 | { |
4a0ee12a PZ |
2354 | int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
2355 | name, sizeof (uint64_t), 1, val); | |
2356 | ||
2357 | if (error != 0 && (error != ENOENT || log_enoent)) { | |
2358 | spa_load_failed(spa, "couldn't get '%s' value in MOS directory " | |
2359 | "[error=%d]", name, error); | |
2360 | } | |
2361 | ||
2362 | return (error); | |
428870ff BB |
2363 | } |
2364 | ||
2365 | static int | |
2366 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
2367 | { | |
2368 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
a1d477c2 | 2369 | return (SET_ERROR(err)); |
428870ff BB |
2370 | } |
2371 | ||
37f03da8 SH |
2372 | boolean_t |
2373 | spa_livelist_delete_check(spa_t *spa) | |
2374 | { | |
2375 | return (spa->spa_livelists_to_delete != 0); | |
2376 | } | |
2377 | ||
2378 | /* ARGSUSED */ | |
2379 | static boolean_t | |
2380 | spa_livelist_delete_cb_check(void *arg, zthr_t *z) | |
2381 | { | |
2382 | spa_t *spa = arg; | |
2383 | return (spa_livelist_delete_check(spa)); | |
2384 | } | |
2385 | ||
2386 | static int | |
2387 | delete_blkptr_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
2388 | { | |
2389 | spa_t *spa = arg; | |
2390 | zio_free(spa, tx->tx_txg, bp); | |
2391 | dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD, | |
2392 | -bp_get_dsize_sync(spa, bp), | |
2393 | -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx); | |
2394 | return (0); | |
2395 | } | |
2396 | ||
2397 | static int | |
2398 | dsl_get_next_livelist_obj(objset_t *os, uint64_t zap_obj, uint64_t *llp) | |
2399 | { | |
2400 | int err; | |
2401 | zap_cursor_t zc; | |
2402 | zap_attribute_t za; | |
2403 | zap_cursor_init(&zc, os, zap_obj); | |
2404 | err = zap_cursor_retrieve(&zc, &za); | |
2405 | zap_cursor_fini(&zc); | |
2406 | if (err == 0) | |
2407 | *llp = za.za_first_integer; | |
2408 | return (err); | |
2409 | } | |
2410 | ||
2411 | /* | |
2412 | * Components of livelist deletion that must be performed in syncing | |
2413 | * context: freeing block pointers and updating the pool-wide data | |
2414 | * structures to indicate how much work is left to do | |
2415 | */ | |
2416 | typedef struct sublist_delete_arg { | |
2417 | spa_t *spa; | |
2418 | dsl_deadlist_t *ll; | |
2419 | uint64_t key; | |
2420 | bplist_t *to_free; | |
2421 | } sublist_delete_arg_t; | |
2422 | ||
2423 | static void | |
2424 | sublist_delete_sync(void *arg, dmu_tx_t *tx) | |
2425 | { | |
2426 | sublist_delete_arg_t *sda = arg; | |
2427 | spa_t *spa = sda->spa; | |
2428 | dsl_deadlist_t *ll = sda->ll; | |
2429 | uint64_t key = sda->key; | |
2430 | bplist_t *to_free = sda->to_free; | |
2431 | ||
2432 | bplist_iterate(to_free, delete_blkptr_cb, spa, tx); | |
2433 | dsl_deadlist_remove_entry(ll, key, tx); | |
2434 | } | |
2435 | ||
2436 | typedef struct livelist_delete_arg { | |
2437 | spa_t *spa; | |
2438 | uint64_t ll_obj; | |
2439 | uint64_t zap_obj; | |
2440 | } livelist_delete_arg_t; | |
2441 | ||
2442 | static void | |
2443 | livelist_delete_sync(void *arg, dmu_tx_t *tx) | |
2444 | { | |
2445 | livelist_delete_arg_t *lda = arg; | |
2446 | spa_t *spa = lda->spa; | |
2447 | uint64_t ll_obj = lda->ll_obj; | |
2448 | uint64_t zap_obj = lda->zap_obj; | |
2449 | objset_t *mos = spa->spa_meta_objset; | |
2450 | uint64_t count; | |
2451 | ||
2452 | /* free the livelist and decrement the feature count */ | |
2453 | VERIFY0(zap_remove_int(mos, zap_obj, ll_obj, tx)); | |
2454 | dsl_deadlist_free(mos, ll_obj, tx); | |
2455 | spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx); | |
2456 | VERIFY0(zap_count(mos, zap_obj, &count)); | |
2457 | if (count == 0) { | |
2458 | /* no more livelists to delete */ | |
2459 | VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT, | |
2460 | DMU_POOL_DELETED_CLONES, tx)); | |
2461 | VERIFY0(zap_destroy(mos, zap_obj, tx)); | |
2462 | spa->spa_livelists_to_delete = 0; | |
e60e158e | 2463 | spa_notify_waiters(spa); |
37f03da8 SH |
2464 | } |
2465 | } | |
2466 | ||
2467 | /* | |
2468 | * Load in the value for the livelist to be removed and open it. Then, | |
2469 | * load its first sublist and determine which block pointers should actually | |
2470 | * be freed. Then, call a synctask which performs the actual frees and updates | |
2471 | * the pool-wide livelist data. | |
2472 | */ | |
2473 | /* ARGSUSED */ | |
65c7cc49 | 2474 | static void |
37f03da8 SH |
2475 | spa_livelist_delete_cb(void *arg, zthr_t *z) |
2476 | { | |
2477 | spa_t *spa = arg; | |
2478 | uint64_t ll_obj = 0, count; | |
2479 | objset_t *mos = spa->spa_meta_objset; | |
2480 | uint64_t zap_obj = spa->spa_livelists_to_delete; | |
2481 | /* | |
2482 | * Determine the next livelist to delete. This function should only | |
2483 | * be called if there is at least one deleted clone. | |
2484 | */ | |
2485 | VERIFY0(dsl_get_next_livelist_obj(mos, zap_obj, &ll_obj)); | |
2486 | VERIFY0(zap_count(mos, ll_obj, &count)); | |
2487 | if (count > 0) { | |
2488 | dsl_deadlist_t ll = { 0 }; | |
2489 | dsl_deadlist_entry_t *dle; | |
2490 | bplist_t to_free; | |
2491 | dsl_deadlist_open(&ll, mos, ll_obj); | |
2492 | dle = dsl_deadlist_first(&ll); | |
2493 | ASSERT3P(dle, !=, NULL); | |
2494 | bplist_create(&to_free); | |
2495 | int err = dsl_process_sub_livelist(&dle->dle_bpobj, &to_free, | |
2496 | z, NULL); | |
2497 | if (err == 0) { | |
2498 | sublist_delete_arg_t sync_arg = { | |
2499 | .spa = spa, | |
2500 | .ll = &ll, | |
2501 | .key = dle->dle_mintxg, | |
2502 | .to_free = &to_free | |
2503 | }; | |
2504 | zfs_dbgmsg("deleting sublist (id %llu) from" | |
2505 | " livelist %llu, %d remaining", | |
2506 | dle->dle_bpobj.bpo_object, ll_obj, count - 1); | |
2507 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, | |
2508 | sublist_delete_sync, &sync_arg, 0, | |
2509 | ZFS_SPACE_CHECK_DESTROY)); | |
2510 | } else { | |
d87676a9 | 2511 | VERIFY3U(err, ==, EINTR); |
37f03da8 SH |
2512 | } |
2513 | bplist_clear(&to_free); | |
2514 | bplist_destroy(&to_free); | |
2515 | dsl_deadlist_close(&ll); | |
2516 | } else { | |
2517 | livelist_delete_arg_t sync_arg = { | |
2518 | .spa = spa, | |
2519 | .ll_obj = ll_obj, | |
2520 | .zap_obj = zap_obj | |
2521 | }; | |
2522 | zfs_dbgmsg("deletion of livelist %llu completed", ll_obj); | |
2523 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, livelist_delete_sync, | |
2524 | &sync_arg, 0, ZFS_SPACE_CHECK_DESTROY)); | |
2525 | } | |
2526 | } | |
2527 | ||
65c7cc49 | 2528 | static void |
37f03da8 SH |
2529 | spa_start_livelist_destroy_thread(spa_t *spa) |
2530 | { | |
2531 | ASSERT3P(spa->spa_livelist_delete_zthr, ==, NULL); | |
843e9ca2 SD |
2532 | spa->spa_livelist_delete_zthr = |
2533 | zthr_create("z_livelist_destroy", | |
37f03da8 SH |
2534 | spa_livelist_delete_cb_check, spa_livelist_delete_cb, spa); |
2535 | } | |
2536 | ||
2537 | typedef struct livelist_new_arg { | |
2538 | bplist_t *allocs; | |
2539 | bplist_t *frees; | |
2540 | } livelist_new_arg_t; | |
2541 | ||
2542 | static int | |
2543 | livelist_track_new_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
2544 | dmu_tx_t *tx) | |
2545 | { | |
2546 | ASSERT(tx == NULL); | |
2547 | livelist_new_arg_t *lna = arg; | |
2548 | if (bp_freed) { | |
2549 | bplist_append(lna->frees, bp); | |
2550 | } else { | |
2551 | bplist_append(lna->allocs, bp); | |
2552 | zfs_livelist_condense_new_alloc++; | |
2553 | } | |
2554 | return (0); | |
2555 | } | |
2556 | ||
2557 | typedef struct livelist_condense_arg { | |
2558 | spa_t *spa; | |
2559 | bplist_t to_keep; | |
2560 | uint64_t first_size; | |
2561 | uint64_t next_size; | |
2562 | } livelist_condense_arg_t; | |
2563 | ||
2564 | static void | |
2565 | spa_livelist_condense_sync(void *arg, dmu_tx_t *tx) | |
2566 | { | |
2567 | livelist_condense_arg_t *lca = arg; | |
2568 | spa_t *spa = lca->spa; | |
2569 | bplist_t new_frees; | |
2570 | dsl_dataset_t *ds = spa->spa_to_condense.ds; | |
2571 | ||
2572 | /* Have we been cancelled? */ | |
2573 | if (spa->spa_to_condense.cancelled) { | |
2574 | zfs_livelist_condense_sync_cancel++; | |
2575 | goto out; | |
2576 | } | |
2577 | ||
2578 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2579 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2580 | dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist; | |
2581 | ||
2582 | /* | |
2583 | * It's possible that the livelist was changed while the zthr was | |
2584 | * running. Therefore, we need to check for new blkptrs in the two | |
2585 | * entries being condensed and continue to track them in the livelist. | |
2586 | * Because of the way we handle remapped blkptrs (see dbuf_remap_impl), | |
2587 | * it's possible that the newly added blkptrs are FREEs or ALLOCs so | |
2588 | * we need to sort them into two different bplists. | |
2589 | */ | |
2590 | uint64_t first_obj = first->dle_bpobj.bpo_object; | |
2591 | uint64_t next_obj = next->dle_bpobj.bpo_object; | |
2592 | uint64_t cur_first_size = first->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2593 | uint64_t cur_next_size = next->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2594 | ||
2595 | bplist_create(&new_frees); | |
2596 | livelist_new_arg_t new_bps = { | |
2597 | .allocs = &lca->to_keep, | |
2598 | .frees = &new_frees, | |
2599 | }; | |
2600 | ||
2601 | if (cur_first_size > lca->first_size) { | |
2602 | VERIFY0(livelist_bpobj_iterate_from_nofree(&first->dle_bpobj, | |
2603 | livelist_track_new_cb, &new_bps, lca->first_size)); | |
2604 | } | |
2605 | if (cur_next_size > lca->next_size) { | |
2606 | VERIFY0(livelist_bpobj_iterate_from_nofree(&next->dle_bpobj, | |
2607 | livelist_track_new_cb, &new_bps, lca->next_size)); | |
2608 | } | |
2609 | ||
2610 | dsl_deadlist_clear_entry(first, ll, tx); | |
2611 | ASSERT(bpobj_is_empty(&first->dle_bpobj)); | |
2612 | dsl_deadlist_remove_entry(ll, next->dle_mintxg, tx); | |
2613 | ||
2614 | bplist_iterate(&lca->to_keep, dsl_deadlist_insert_alloc_cb, ll, tx); | |
2615 | bplist_iterate(&new_frees, dsl_deadlist_insert_free_cb, ll, tx); | |
2616 | bplist_destroy(&new_frees); | |
2617 | ||
2618 | char dsname[ZFS_MAX_DATASET_NAME_LEN]; | |
2619 | dsl_dataset_name(ds, dsname); | |
2620 | zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu " | |
2621 | "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu " | |
2622 | "(%llu blkptrs)", tx->tx_txg, dsname, ds->ds_object, first_obj, | |
2623 | cur_first_size, next_obj, cur_next_size, | |
2624 | first->dle_bpobj.bpo_object, | |
2625 | first->dle_bpobj.bpo_phys->bpo_num_blkptrs); | |
2626 | out: | |
2627 | dmu_buf_rele(ds->ds_dbuf, spa); | |
2628 | spa->spa_to_condense.ds = NULL; | |
2629 | bplist_clear(&lca->to_keep); | |
2630 | bplist_destroy(&lca->to_keep); | |
2631 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2632 | spa->spa_to_condense.syncing = B_FALSE; | |
2633 | } | |
2634 | ||
65c7cc49 | 2635 | static void |
37f03da8 SH |
2636 | spa_livelist_condense_cb(void *arg, zthr_t *t) |
2637 | { | |
2638 | while (zfs_livelist_condense_zthr_pause && | |
2639 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2640 | delay(1); | |
2641 | ||
2642 | spa_t *spa = arg; | |
2643 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2644 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2645 | uint64_t first_size, next_size; | |
2646 | ||
2647 | livelist_condense_arg_t *lca = | |
2648 | kmem_alloc(sizeof (livelist_condense_arg_t), KM_SLEEP); | |
2649 | bplist_create(&lca->to_keep); | |
2650 | ||
2651 | /* | |
2652 | * Process the livelists (matching FREEs and ALLOCs) in open context | |
2653 | * so we have minimal work in syncing context to condense. | |
2654 | * | |
2655 | * We save bpobj sizes (first_size and next_size) to use later in | |
2656 | * syncing context to determine if entries were added to these sublists | |
2657 | * while in open context. This is possible because the clone is still | |
2658 | * active and open for normal writes and we want to make sure the new, | |
2659 | * unprocessed blockpointers are inserted into the livelist normally. | |
2660 | * | |
2661 | * Note that dsl_process_sub_livelist() both stores the size number of | |
2662 | * blockpointers and iterates over them while the bpobj's lock held, so | |
2663 | * the sizes returned to us are consistent which what was actually | |
2664 | * processed. | |
2665 | */ | |
2666 | int err = dsl_process_sub_livelist(&first->dle_bpobj, &lca->to_keep, t, | |
2667 | &first_size); | |
2668 | if (err == 0) | |
2669 | err = dsl_process_sub_livelist(&next->dle_bpobj, &lca->to_keep, | |
2670 | t, &next_size); | |
2671 | ||
2672 | if (err == 0) { | |
2673 | while (zfs_livelist_condense_sync_pause && | |
2674 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2675 | delay(1); | |
2676 | ||
2677 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
2678 | dmu_tx_mark_netfree(tx); | |
2679 | dmu_tx_hold_space(tx, 1); | |
2680 | err = dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE); | |
2681 | if (err == 0) { | |
2682 | /* | |
2683 | * Prevent the condense zthr restarting before | |
2684 | * the synctask completes. | |
2685 | */ | |
2686 | spa->spa_to_condense.syncing = B_TRUE; | |
2687 | lca->spa = spa; | |
2688 | lca->first_size = first_size; | |
2689 | lca->next_size = next_size; | |
2690 | dsl_sync_task_nowait(spa_get_dsl(spa), | |
38080324 | 2691 | spa_livelist_condense_sync, lca, tx); |
37f03da8 SH |
2692 | dmu_tx_commit(tx); |
2693 | return; | |
2694 | } | |
2695 | } | |
2696 | /* | |
2697 | * Condensing can not continue: either it was externally stopped or | |
2698 | * we were unable to assign to a tx because the pool has run out of | |
2699 | * space. In the second case, we'll just end up trying to condense | |
2700 | * again in a later txg. | |
2701 | */ | |
2702 | ASSERT(err != 0); | |
2703 | bplist_clear(&lca->to_keep); | |
2704 | bplist_destroy(&lca->to_keep); | |
2705 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2706 | dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, spa); | |
2707 | spa->spa_to_condense.ds = NULL; | |
2708 | if (err == EINTR) | |
2709 | zfs_livelist_condense_zthr_cancel++; | |
2710 | } | |
2711 | ||
2712 | /* ARGSUSED */ | |
2713 | /* | |
2714 | * Check that there is something to condense but that a condense is not | |
2715 | * already in progress and that condensing has not been cancelled. | |
2716 | */ | |
2717 | static boolean_t | |
2718 | spa_livelist_condense_cb_check(void *arg, zthr_t *z) | |
2719 | { | |
2720 | spa_t *spa = arg; | |
2721 | if ((spa->spa_to_condense.ds != NULL) && | |
2722 | (spa->spa_to_condense.syncing == B_FALSE) && | |
2723 | (spa->spa_to_condense.cancelled == B_FALSE)) { | |
2724 | return (B_TRUE); | |
2725 | } | |
2726 | return (B_FALSE); | |
2727 | } | |
2728 | ||
65c7cc49 | 2729 | static void |
37f03da8 SH |
2730 | spa_start_livelist_condensing_thread(spa_t *spa) |
2731 | { | |
2732 | spa->spa_to_condense.ds = NULL; | |
2733 | spa->spa_to_condense.first = NULL; | |
2734 | spa->spa_to_condense.next = NULL; | |
2735 | spa->spa_to_condense.syncing = B_FALSE; | |
2736 | spa->spa_to_condense.cancelled = B_FALSE; | |
2737 | ||
2738 | ASSERT3P(spa->spa_livelist_condense_zthr, ==, NULL); | |
843e9ca2 SD |
2739 | spa->spa_livelist_condense_zthr = |
2740 | zthr_create("z_livelist_condense", | |
2741 | spa_livelist_condense_cb_check, | |
2742 | spa_livelist_condense_cb, spa); | |
37f03da8 SH |
2743 | } |
2744 | ||
9d5b5245 SD |
2745 | static void |
2746 | spa_spawn_aux_threads(spa_t *spa) | |
2747 | { | |
2748 | ASSERT(spa_writeable(spa)); | |
2749 | ||
2750 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2751 | ||
2752 | spa_start_indirect_condensing_thread(spa); | |
37f03da8 SH |
2753 | spa_start_livelist_destroy_thread(spa); |
2754 | spa_start_livelist_condensing_thread(spa); | |
d2734cce SD |
2755 | |
2756 | ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); | |
2757 | spa->spa_checkpoint_discard_zthr = | |
843e9ca2 SD |
2758 | zthr_create("z_checkpoint_discard", |
2759 | spa_checkpoint_discard_thread_check, | |
d2734cce | 2760 | spa_checkpoint_discard_thread, spa); |
9d5b5245 SD |
2761 | } |
2762 | ||
428870ff BB |
2763 | /* |
2764 | * Fix up config after a partly-completed split. This is done with the | |
2765 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
2766 | * pool have that entry in their config, but only the splitting one contains | |
2767 | * a list of all the guids of the vdevs that are being split off. | |
2768 | * | |
2769 | * This function determines what to do with that list: either rejoin | |
2770 | * all the disks to the pool, or complete the splitting process. To attempt | |
2771 | * the rejoin, each disk that is offlined is marked online again, and | |
2772 | * we do a reopen() call. If the vdev label for every disk that was | |
2773 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
2774 | * then we call vdev_split() on each disk, and complete the split. | |
2775 | * | |
2776 | * Otherwise we leave the config alone, with all the vdevs in place in | |
2777 | * the original pool. | |
2778 | */ | |
2779 | static void | |
2780 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
2781 | { | |
2782 | uint_t extracted; | |
2783 | uint64_t *glist; | |
2784 | uint_t i, gcount; | |
2785 | nvlist_t *nvl; | |
2786 | vdev_t **vd; | |
2787 | boolean_t attempt_reopen; | |
2788 | ||
2789 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
2790 | return; | |
2791 | ||
2792 | /* check that the config is complete */ | |
2793 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
2794 | &glist, &gcount) != 0) | |
2795 | return; | |
2796 | ||
79c76d5b | 2797 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); |
428870ff BB |
2798 | |
2799 | /* attempt to online all the vdevs & validate */ | |
2800 | attempt_reopen = B_TRUE; | |
2801 | for (i = 0; i < gcount; i++) { | |
2802 | if (glist[i] == 0) /* vdev is hole */ | |
2803 | continue; | |
2804 | ||
2805 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
2806 | if (vd[i] == NULL) { | |
2807 | /* | |
2808 | * Don't bother attempting to reopen the disks; | |
2809 | * just do the split. | |
2810 | */ | |
2811 | attempt_reopen = B_FALSE; | |
2812 | } else { | |
2813 | /* attempt to re-online it */ | |
2814 | vd[i]->vdev_offline = B_FALSE; | |
2815 | } | |
2816 | } | |
2817 | ||
2818 | if (attempt_reopen) { | |
2819 | vdev_reopen(spa->spa_root_vdev); | |
2820 | ||
2821 | /* check each device to see what state it's in */ | |
2822 | for (extracted = 0, i = 0; i < gcount; i++) { | |
2823 | if (vd[i] != NULL && | |
2824 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
2825 | break; | |
2826 | ++extracted; | |
2827 | } | |
2828 | } | |
2829 | ||
2830 | /* | |
2831 | * If every disk has been moved to the new pool, or if we never | |
2832 | * even attempted to look at them, then we split them off for | |
2833 | * good. | |
2834 | */ | |
2835 | if (!attempt_reopen || gcount == extracted) { | |
2836 | for (i = 0; i < gcount; i++) | |
2837 | if (vd[i] != NULL) | |
2838 | vdev_split(vd[i]); | |
2839 | vdev_reopen(spa->spa_root_vdev); | |
2840 | } | |
2841 | ||
2842 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
2843 | } | |
2844 | ||
2845 | static int | |
6cb8e530 | 2846 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) |
428870ff | 2847 | { |
428870ff BB |
2848 | char *ereport = FM_EREPORT_ZFS_POOL; |
2849 | int error; | |
428870ff | 2850 | |
6cb8e530 | 2851 | spa->spa_load_state = state; |
ca95f70d OF |
2852 | (void) spa_import_progress_set_state(spa_guid(spa), |
2853 | spa_load_state(spa)); | |
9ae529ec | 2854 | |
6cb8e530 | 2855 | gethrestime(&spa->spa_loaded_ts); |
d2734cce | 2856 | error = spa_load_impl(spa, type, &ereport); |
428870ff | 2857 | |
0c66c32d JG |
2858 | /* |
2859 | * Don't count references from objsets that are already closed | |
2860 | * and are making their way through the eviction process. | |
2861 | */ | |
2862 | spa_evicting_os_wait(spa); | |
424fd7c3 | 2863 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
572e2857 BB |
2864 | if (error) { |
2865 | if (error != EEXIST) { | |
2866 | spa->spa_loaded_ts.tv_sec = 0; | |
2867 | spa->spa_loaded_ts.tv_nsec = 0; | |
2868 | } | |
2869 | if (error != EBADF) { | |
1144586b TS |
2870 | (void) zfs_ereport_post(ereport, spa, |
2871 | NULL, NULL, NULL, 0, 0); | |
572e2857 BB |
2872 | } |
2873 | } | |
428870ff BB |
2874 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; |
2875 | spa->spa_ena = 0; | |
2876 | ||
ca95f70d OF |
2877 | (void) spa_import_progress_set_state(spa_guid(spa), |
2878 | spa_load_state(spa)); | |
2879 | ||
428870ff BB |
2880 | return (error); |
2881 | } | |
2882 | ||
33cf67cd | 2883 | #ifdef ZFS_DEBUG |
e0ab3ab5 JS |
2884 | /* |
2885 | * Count the number of per-vdev ZAPs associated with all of the vdevs in the | |
2886 | * vdev tree rooted in the given vd, and ensure that each ZAP is present in the | |
2887 | * spa's per-vdev ZAP list. | |
2888 | */ | |
2889 | static uint64_t | |
2890 | vdev_count_verify_zaps(vdev_t *vd) | |
2891 | { | |
2892 | spa_t *spa = vd->vdev_spa; | |
2893 | uint64_t total = 0; | |
e0ab3ab5 JS |
2894 | |
2895 | if (vd->vdev_top_zap != 0) { | |
2896 | total++; | |
2897 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2898 | spa->spa_all_vdev_zaps, vd->vdev_top_zap)); | |
2899 | } | |
2900 | if (vd->vdev_leaf_zap != 0) { | |
2901 | total++; | |
2902 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2903 | spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); | |
2904 | } | |
2905 | ||
1c27024e | 2906 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
2907 | total += vdev_count_verify_zaps(vd->vdev_child[i]); |
2908 | } | |
2909 | ||
2910 | return (total); | |
2911 | } | |
33cf67cd | 2912 | #endif |
e0ab3ab5 | 2913 | |
379ca9cf OF |
2914 | /* |
2915 | * Determine whether the activity check is required. | |
2916 | */ | |
2917 | static boolean_t | |
bbffb59e BB |
2918 | spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label, |
2919 | nvlist_t *config) | |
379ca9cf OF |
2920 | { |
2921 | uint64_t state = 0; | |
2922 | uint64_t hostid = 0; | |
2923 | uint64_t tryconfig_txg = 0; | |
2924 | uint64_t tryconfig_timestamp = 0; | |
060f0226 | 2925 | uint16_t tryconfig_mmp_seq = 0; |
379ca9cf OF |
2926 | nvlist_t *nvinfo; |
2927 | ||
2928 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
2929 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
2930 | (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, | |
2931 | &tryconfig_txg); | |
2932 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
2933 | &tryconfig_timestamp); | |
060f0226 OF |
2934 | (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ, |
2935 | &tryconfig_mmp_seq); | |
379ca9cf OF |
2936 | } |
2937 | ||
2938 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); | |
379ca9cf OF |
2939 | |
2940 | /* | |
2941 | * Disable the MMP activity check - This is used by zdb which | |
2942 | * is intended to be used on potentially active pools. | |
2943 | */ | |
2944 | if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) | |
2945 | return (B_FALSE); | |
2946 | ||
2947 | /* | |
2948 | * Skip the activity check when the MMP feature is disabled. | |
2949 | */ | |
2950 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) | |
2951 | return (B_FALSE); | |
ca95f70d | 2952 | |
379ca9cf | 2953 | /* |
060f0226 OF |
2954 | * If the tryconfig_ values are nonzero, they are the results of an |
2955 | * earlier tryimport. If they all match the uberblock we just found, | |
2956 | * then the pool has not changed and we return false so we do not test | |
2957 | * a second time. | |
379ca9cf OF |
2958 | */ |
2959 | if (tryconfig_txg && tryconfig_txg == ub->ub_txg && | |
060f0226 OF |
2960 | tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp && |
2961 | tryconfig_mmp_seq && tryconfig_mmp_seq == | |
2962 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) | |
379ca9cf OF |
2963 | return (B_FALSE); |
2964 | ||
2965 | /* | |
2966 | * Allow the activity check to be skipped when importing the pool | |
bbffb59e BB |
2967 | * on the same host which last imported it. Since the hostid from |
2968 | * configuration may be stale use the one read from the label. | |
379ca9cf | 2969 | */ |
bbffb59e BB |
2970 | if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) |
2971 | hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); | |
2972 | ||
25f06d67 | 2973 | if (hostid == spa_get_hostid(spa)) |
379ca9cf OF |
2974 | return (B_FALSE); |
2975 | ||
2976 | /* | |
2977 | * Skip the activity test when the pool was cleanly exported. | |
2978 | */ | |
2979 | if (state != POOL_STATE_ACTIVE) | |
2980 | return (B_FALSE); | |
2981 | ||
2982 | return (B_TRUE); | |
2983 | } | |
2984 | ||
060f0226 OF |
2985 | /* |
2986 | * Nanoseconds the activity check must watch for changes on-disk. | |
2987 | */ | |
2988 | static uint64_t | |
2989 | spa_activity_check_duration(spa_t *spa, uberblock_t *ub) | |
2990 | { | |
2991 | uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); | |
2992 | uint64_t multihost_interval = MSEC2NSEC( | |
2993 | MMP_INTERVAL_OK(zfs_multihost_interval)); | |
2994 | uint64_t import_delay = MAX(NANOSEC, import_intervals * | |
2995 | multihost_interval); | |
2996 | ||
2997 | /* | |
2998 | * Local tunables determine a minimum duration except for the case | |
2999 | * where we know when the remote host will suspend the pool if MMP | |
3000 | * writes do not land. | |
3001 | * | |
3002 | * See Big Theory comment at the top of mmp.c for the reasoning behind | |
3003 | * these cases and times. | |
3004 | */ | |
3005 | ||
3006 | ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100); | |
3007 | ||
3008 | if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3009 | MMP_FAIL_INT(ub) > 0) { | |
3010 | ||
3011 | /* MMP on remote host will suspend pool after failed writes */ | |
3012 | import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) * | |
3013 | MMP_IMPORT_SAFETY_FACTOR / 100; | |
3014 | ||
3015 | zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp " | |
3016 | "mmp_fails=%llu ub_mmp mmp_interval=%llu " | |
3017 | "import_intervals=%u", import_delay, MMP_FAIL_INT(ub), | |
3018 | MMP_INTERVAL(ub), import_intervals); | |
3019 | ||
3020 | } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3021 | MMP_FAIL_INT(ub) == 0) { | |
3022 | ||
3023 | /* MMP on remote host will never suspend pool */ | |
3024 | import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) + | |
3025 | ub->ub_mmp_delay) * import_intervals); | |
3026 | ||
3027 | zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp " | |
3028 | "mmp_interval=%llu ub_mmp_delay=%llu " | |
3029 | "import_intervals=%u", import_delay, MMP_INTERVAL(ub), | |
3030 | ub->ub_mmp_delay, import_intervals); | |
3031 | ||
3032 | } else if (MMP_VALID(ub)) { | |
3033 | /* | |
e1cfd73f | 3034 | * zfs-0.7 compatibility case |
060f0226 OF |
3035 | */ |
3036 | ||
3037 | import_delay = MAX(import_delay, (multihost_interval + | |
3038 | ub->ub_mmp_delay) * import_intervals); | |
3039 | ||
3040 | zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu " | |
3041 | "import_intervals=%u leaves=%u", import_delay, | |
3042 | ub->ub_mmp_delay, import_intervals, | |
3043 | vdev_count_leaves(spa)); | |
3044 | } else { | |
3045 | /* Using local tunings is the only reasonable option */ | |
3046 | zfs_dbgmsg("pool last imported on non-MMP aware " | |
3047 | "host using import_delay=%llu multihost_interval=%llu " | |
3048 | "import_intervals=%u", import_delay, multihost_interval, | |
3049 | import_intervals); | |
3050 | } | |
3051 | ||
3052 | return (import_delay); | |
3053 | } | |
3054 | ||
379ca9cf OF |
3055 | /* |
3056 | * Perform the import activity check. If the user canceled the import or | |
3057 | * we detected activity then fail. | |
3058 | */ | |
3059 | static int | |
3060 | spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) | |
3061 | { | |
379ca9cf OF |
3062 | uint64_t txg = ub->ub_txg; |
3063 | uint64_t timestamp = ub->ub_timestamp; | |
060f0226 OF |
3064 | uint64_t mmp_config = ub->ub_mmp_config; |
3065 | uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0; | |
3066 | uint64_t import_delay; | |
379ca9cf OF |
3067 | hrtime_t import_expire; |
3068 | nvlist_t *mmp_label = NULL; | |
3069 | vdev_t *rvd = spa->spa_root_vdev; | |
3070 | kcondvar_t cv; | |
3071 | kmutex_t mtx; | |
3072 | int error = 0; | |
3073 | ||
3074 | cv_init(&cv, NULL, CV_DEFAULT, NULL); | |
3075 | mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); | |
3076 | mutex_enter(&mtx); | |
3077 | ||
3078 | /* | |
3079 | * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed | |
3080 | * during the earlier tryimport. If the txg recorded there is 0 then | |
3081 | * the pool is known to be active on another host. | |
3082 | * | |
060f0226 | 3083 | * Otherwise, the pool might be in use on another host. Check for |
379ca9cf OF |
3084 | * changes in the uberblocks on disk if necessary. |
3085 | */ | |
3086 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3087 | nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, | |
3088 | ZPOOL_CONFIG_LOAD_INFO); | |
3089 | ||
3090 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && | |
3091 | fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { | |
3092 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
3093 | error = SET_ERROR(EREMOTEIO); | |
3094 | goto out; | |
3095 | } | |
3096 | } | |
3097 | ||
060f0226 | 3098 | import_delay = spa_activity_check_duration(spa, ub); |
533ea041 | 3099 | |
379ca9cf | 3100 | /* Add a small random factor in case of simultaneous imports (0-25%) */ |
ca95f70d OF |
3101 | import_delay += import_delay * spa_get_random(250) / 1000; |
3102 | ||
3103 | import_expire = gethrtime() + import_delay; | |
379ca9cf OF |
3104 | |
3105 | while (gethrtime() < import_expire) { | |
ca95f70d OF |
3106 | (void) spa_import_progress_set_mmp_check(spa_guid(spa), |
3107 | NSEC2SEC(import_expire - gethrtime())); | |
3108 | ||
379ca9cf OF |
3109 | vdev_uberblock_load(rvd, ub, &mmp_label); |
3110 | ||
060f0226 OF |
3111 | if (txg != ub->ub_txg || timestamp != ub->ub_timestamp || |
3112 | mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) { | |
3113 | zfs_dbgmsg("multihost activity detected " | |
3114 | "txg %llu ub_txg %llu " | |
3115 | "timestamp %llu ub_timestamp %llu " | |
3116 | "mmp_config %#llx ub_mmp_config %#llx", | |
3117 | txg, ub->ub_txg, timestamp, ub->ub_timestamp, | |
3118 | mmp_config, ub->ub_mmp_config); | |
3119 | ||
379ca9cf OF |
3120 | error = SET_ERROR(EREMOTEIO); |
3121 | break; | |
3122 | } | |
3123 | ||
3124 | if (mmp_label) { | |
3125 | nvlist_free(mmp_label); | |
3126 | mmp_label = NULL; | |
3127 | } | |
3128 | ||
3129 | error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); | |
3130 | if (error != -1) { | |
3131 | error = SET_ERROR(EINTR); | |
3132 | break; | |
3133 | } | |
3134 | error = 0; | |
3135 | } | |
3136 | ||
3137 | out: | |
3138 | mutex_exit(&mtx); | |
3139 | mutex_destroy(&mtx); | |
3140 | cv_destroy(&cv); | |
3141 | ||
3142 | /* | |
3143 | * If the pool is determined to be active store the status in the | |
3144 | * spa->spa_load_info nvlist. If the remote hostname or hostid are | |
3145 | * available from configuration read from disk store them as well. | |
3146 | * This allows 'zpool import' to generate a more useful message. | |
3147 | * | |
3148 | * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) | |
3149 | * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool | |
3150 | * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool | |
3151 | */ | |
3152 | if (error == EREMOTEIO) { | |
3153 | char *hostname = "<unknown>"; | |
3154 | uint64_t hostid = 0; | |
3155 | ||
3156 | if (mmp_label) { | |
3157 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { | |
3158 | hostname = fnvlist_lookup_string(mmp_label, | |
3159 | ZPOOL_CONFIG_HOSTNAME); | |
3160 | fnvlist_add_string(spa->spa_load_info, | |
3161 | ZPOOL_CONFIG_MMP_HOSTNAME, hostname); | |
3162 | } | |
3163 | ||
3164 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { | |
3165 | hostid = fnvlist_lookup_uint64(mmp_label, | |
3166 | ZPOOL_CONFIG_HOSTID); | |
3167 | fnvlist_add_uint64(spa->spa_load_info, | |
3168 | ZPOOL_CONFIG_MMP_HOSTID, hostid); | |
3169 | } | |
3170 | } | |
3171 | ||
3172 | fnvlist_add_uint64(spa->spa_load_info, | |
3173 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); | |
3174 | fnvlist_add_uint64(spa->spa_load_info, | |
3175 | ZPOOL_CONFIG_MMP_TXG, 0); | |
3176 | ||
3177 | error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); | |
3178 | } | |
3179 | ||
3180 | if (mmp_label) | |
3181 | nvlist_free(mmp_label); | |
3182 | ||
3183 | return (error); | |
3184 | } | |
3185 | ||
9eb7b46e | 3186 | static int |
6cb8e530 PZ |
3187 | spa_verify_host(spa_t *spa, nvlist_t *mos_config) |
3188 | { | |
3189 | uint64_t hostid; | |
3190 | char *hostname; | |
3191 | uint64_t myhostid = 0; | |
3192 | ||
3193 | if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, | |
3194 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
3195 | hostname = fnvlist_lookup_string(mos_config, | |
3196 | ZPOOL_CONFIG_HOSTNAME); | |
3197 | ||
3198 | myhostid = zone_get_hostid(NULL); | |
3199 | ||
3200 | if (hostid != 0 && myhostid != 0 && hostid != myhostid) { | |
3201 | cmn_err(CE_WARN, "pool '%s' could not be " | |
3202 | "loaded as it was last accessed by " | |
3203 | "another system (host: %s hostid: 0x%llx). " | |
a2f944a1 RM |
3204 | "See: https://openzfs.github.io/openzfs-docs/msg/" |
3205 | "ZFS-8000-EY", | |
6cb8e530 PZ |
3206 | spa_name(spa), hostname, (u_longlong_t)hostid); |
3207 | spa_load_failed(spa, "hostid verification failed: pool " | |
3208 | "last accessed by host: %s (hostid: 0x%llx)", | |
3209 | hostname, (u_longlong_t)hostid); | |
3210 | return (SET_ERROR(EBADF)); | |
3211 | } | |
3212 | } | |
3213 | ||
3214 | return (0); | |
3215 | } | |
3216 | ||
3217 | static int | |
3218 | spa_ld_parse_config(spa_t *spa, spa_import_type_t type) | |
428870ff BB |
3219 | { |
3220 | int error = 0; | |
6cb8e530 | 3221 | nvlist_t *nvtree, *nvl, *config = spa->spa_config; |
1c27024e | 3222 | int parse; |
9eb7b46e | 3223 | vdev_t *rvd; |
6cb8e530 PZ |
3224 | uint64_t pool_guid; |
3225 | char *comment; | |
3226 | ||
3227 | /* | |
3228 | * Versioning wasn't explicitly added to the label until later, so if | |
3229 | * it's not present treat it as the initial version. | |
3230 | */ | |
3231 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
3232 | &spa->spa_ubsync.ub_version) != 0) | |
3233 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
3234 | ||
3235 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { | |
3236 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3237 | ZPOOL_CONFIG_POOL_GUID); | |
3238 | return (SET_ERROR(EINVAL)); | |
3239 | } | |
3240 | ||
d2734cce SD |
3241 | /* |
3242 | * If we are doing an import, ensure that the pool is not already | |
3243 | * imported by checking if its pool guid already exists in the | |
3244 | * spa namespace. | |
3245 | * | |
3246 | * The only case that we allow an already imported pool to be | |
3247 | * imported again, is when the pool is checkpointed and we want to | |
3248 | * look at its checkpointed state from userland tools like zdb. | |
3249 | */ | |
3250 | #ifdef _KERNEL | |
3251 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3252 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3253 | spa_guid_exists(pool_guid, 0)) { | |
3254 | #else | |
3255 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3256 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3257 | spa_guid_exists(pool_guid, 0) && | |
3258 | !spa_importing_readonly_checkpoint(spa)) { | |
3259 | #endif | |
6cb8e530 PZ |
3260 | spa_load_failed(spa, "a pool with guid %llu is already open", |
3261 | (u_longlong_t)pool_guid); | |
3262 | return (SET_ERROR(EEXIST)); | |
3263 | } | |
3264 | ||
3265 | spa->spa_config_guid = pool_guid; | |
3266 | ||
3267 | nvlist_free(spa->spa_load_info); | |
3268 | spa->spa_load_info = fnvlist_alloc(); | |
3269 | ||
3270 | ASSERT(spa->spa_comment == NULL); | |
3271 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
3272 | spa->spa_comment = spa_strdup(comment); | |
3273 | ||
3274 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
3275 | &spa->spa_config_txg); | |
3276 | ||
3277 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) | |
3278 | spa->spa_config_splitting = fnvlist_dup(nvl); | |
428870ff | 3279 | |
4a0ee12a PZ |
3280 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { |
3281 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3282 | ZPOOL_CONFIG_VDEV_TREE); | |
2e528b49 | 3283 | return (SET_ERROR(EINVAL)); |
4a0ee12a | 3284 | } |
428870ff | 3285 | |
428870ff BB |
3286 | /* |
3287 | * Create "The Godfather" zio to hold all async IOs | |
3288 | */ | |
e022864d MA |
3289 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
3290 | KM_SLEEP); | |
1c27024e | 3291 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
3292 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
3293 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
3294 | ZIO_FLAG_GODFATHER); | |
3295 | } | |
428870ff BB |
3296 | |
3297 | /* | |
3298 | * Parse the configuration into a vdev tree. We explicitly set the | |
3299 | * value that will be returned by spa_version() since parsing the | |
3300 | * configuration requires knowing the version number. | |
3301 | */ | |
3302 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 PZ |
3303 | parse = (type == SPA_IMPORT_EXISTING ? |
3304 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
9eb7b46e | 3305 | error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); |
428870ff BB |
3306 | spa_config_exit(spa, SCL_ALL, FTAG); |
3307 | ||
4a0ee12a PZ |
3308 | if (error != 0) { |
3309 | spa_load_failed(spa, "unable to parse config [error=%d]", | |
3310 | error); | |
428870ff | 3311 | return (error); |
4a0ee12a | 3312 | } |
428870ff BB |
3313 | |
3314 | ASSERT(spa->spa_root_vdev == rvd); | |
c3520e7f MA |
3315 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); |
3316 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
428870ff BB |
3317 | |
3318 | if (type != SPA_IMPORT_ASSEMBLE) { | |
3319 | ASSERT(spa_guid(spa) == pool_guid); | |
3320 | } | |
3321 | ||
9eb7b46e PZ |
3322 | return (0); |
3323 | } | |
3324 | ||
6cb8e530 PZ |
3325 | /* |
3326 | * Recursively open all vdevs in the vdev tree. This function is called twice: | |
3327 | * first with the untrusted config, then with the trusted config. | |
3328 | */ | |
9eb7b46e PZ |
3329 | static int |
3330 | spa_ld_open_vdevs(spa_t *spa) | |
3331 | { | |
3332 | int error = 0; | |
3333 | ||
6cb8e530 PZ |
3334 | /* |
3335 | * spa_missing_tvds_allowed defines how many top-level vdevs can be | |
3336 | * missing/unopenable for the root vdev to be still considered openable. | |
3337 | */ | |
3338 | if (spa->spa_trust_config) { | |
3339 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; | |
3340 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { | |
3341 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; | |
3342 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { | |
3343 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; | |
3344 | } else { | |
3345 | spa->spa_missing_tvds_allowed = 0; | |
3346 | } | |
3347 | ||
3348 | spa->spa_missing_tvds_allowed = | |
3349 | MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); | |
3350 | ||
428870ff | 3351 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9eb7b46e | 3352 | error = vdev_open(spa->spa_root_vdev); |
428870ff | 3353 | spa_config_exit(spa, SCL_ALL, FTAG); |
6cb8e530 PZ |
3354 | |
3355 | if (spa->spa_missing_tvds != 0) { | |
3356 | spa_load_note(spa, "vdev tree has %lld missing top-level " | |
3357 | "vdevs.", (u_longlong_t)spa->spa_missing_tvds); | |
da92d5cb | 3358 | if (spa->spa_trust_config && (spa->spa_mode & SPA_MODE_WRITE)) { |
6cb8e530 PZ |
3359 | /* |
3360 | * Although theoretically we could allow users to open | |
3361 | * incomplete pools in RW mode, we'd need to add a lot | |
3362 | * of extra logic (e.g. adjust pool space to account | |
3363 | * for missing vdevs). | |
3364 | * This limitation also prevents users from accidentally | |
3365 | * opening the pool in RW mode during data recovery and | |
3366 | * damaging it further. | |
3367 | */ | |
3368 | spa_load_note(spa, "pools with missing top-level " | |
3369 | "vdevs can only be opened in read-only mode."); | |
3370 | error = SET_ERROR(ENXIO); | |
3371 | } else { | |
3372 | spa_load_note(spa, "current settings allow for maximum " | |
3373 | "%lld missing top-level vdevs at this stage.", | |
3374 | (u_longlong_t)spa->spa_missing_tvds_allowed); | |
3375 | } | |
3376 | } | |
4a0ee12a PZ |
3377 | if (error != 0) { |
3378 | spa_load_failed(spa, "unable to open vdev tree [error=%d]", | |
3379 | error); | |
3380 | } | |
6cb8e530 PZ |
3381 | if (spa->spa_missing_tvds != 0 || error != 0) |
3382 | vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); | |
9eb7b46e PZ |
3383 | |
3384 | return (error); | |
3385 | } | |
3386 | ||
6cb8e530 PZ |
3387 | /* |
3388 | * We need to validate the vdev labels against the configuration that | |
3389 | * we have in hand. This function is called twice: first with an untrusted | |
3390 | * config, then with a trusted config. The validation is more strict when the | |
3391 | * config is trusted. | |
3392 | */ | |
9eb7b46e | 3393 | static int |
6cb8e530 | 3394 | spa_ld_validate_vdevs(spa_t *spa) |
9eb7b46e PZ |
3395 | { |
3396 | int error = 0; | |
3397 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 3398 | |
6cb8e530 PZ |
3399 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3400 | error = vdev_validate(rvd); | |
3401 | spa_config_exit(spa, SCL_ALL, FTAG); | |
428870ff | 3402 | |
6cb8e530 PZ |
3403 | if (error != 0) { |
3404 | spa_load_failed(spa, "vdev_validate failed [error=%d]", error); | |
3405 | return (error); | |
3406 | } | |
428870ff | 3407 | |
6cb8e530 PZ |
3408 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { |
3409 | spa_load_failed(spa, "cannot open vdev tree after invalidating " | |
3410 | "some vdevs"); | |
3411 | vdev_dbgmsg_print_tree(rvd, 2); | |
3412 | return (SET_ERROR(ENXIO)); | |
428870ff BB |
3413 | } |
3414 | ||
9eb7b46e PZ |
3415 | return (0); |
3416 | } | |
3417 | ||
d2734cce SD |
3418 | static void |
3419 | spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) | |
3420 | { | |
3421 | spa->spa_state = POOL_STATE_ACTIVE; | |
3422 | spa->spa_ubsync = spa->spa_uberblock; | |
3423 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
3424 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
3425 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
3426 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
3427 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
3428 | spa->spa_prev_software_version = ub->ub_software_version; | |
3429 | } | |
3430 | ||
9eb7b46e | 3431 | static int |
6cb8e530 | 3432 | spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) |
9eb7b46e PZ |
3433 | { |
3434 | vdev_t *rvd = spa->spa_root_vdev; | |
3435 | nvlist_t *label; | |
3436 | uberblock_t *ub = &spa->spa_uberblock; | |
9eb7b46e PZ |
3437 | boolean_t activity_check = B_FALSE; |
3438 | ||
d2734cce SD |
3439 | /* |
3440 | * If we are opening the checkpointed state of the pool by | |
3441 | * rewinding to it, at this point we will have written the | |
3442 | * checkpointed uberblock to the vdev labels, so searching | |
3443 | * the labels will find the right uberblock. However, if | |
3444 | * we are opening the checkpointed state read-only, we have | |
3445 | * not modified the labels. Therefore, we must ignore the | |
3446 | * labels and continue using the spa_uberblock that was set | |
3447 | * by spa_ld_checkpoint_rewind. | |
3448 | * | |
3449 | * Note that it would be fine to ignore the labels when | |
3450 | * rewinding (opening writeable) as well. However, if we | |
3451 | * crash just after writing the labels, we will end up | |
3452 | * searching the labels. Doing so in the common case means | |
3453 | * that this code path gets exercised normally, rather than | |
3454 | * just in the edge case. | |
3455 | */ | |
3456 | if (ub->ub_checkpoint_txg != 0 && | |
3457 | spa_importing_readonly_checkpoint(spa)) { | |
3458 | spa_ld_select_uberblock_done(spa, ub); | |
3459 | return (0); | |
3460 | } | |
3461 | ||
428870ff BB |
3462 | /* |
3463 | * Find the best uberblock. | |
3464 | */ | |
9ae529ec | 3465 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
3466 | |
3467 | /* | |
3468 | * If we weren't able to find a single valid uberblock, return failure. | |
3469 | */ | |
9ae529ec CS |
3470 | if (ub->ub_txg == 0) { |
3471 | nvlist_free(label); | |
4a0ee12a | 3472 | spa_load_failed(spa, "no valid uberblock found"); |
428870ff | 3473 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 3474 | } |
428870ff | 3475 | |
ca95f70d OF |
3476 | if (spa->spa_load_max_txg != UINT64_MAX) { |
3477 | (void) spa_import_progress_set_max_txg(spa_guid(spa), | |
3478 | (u_longlong_t)spa->spa_load_max_txg); | |
3479 | } | |
4a0ee12a PZ |
3480 | spa_load_note(spa, "using uberblock with txg=%llu", |
3481 | (u_longlong_t)ub->ub_txg); | |
3482 | ||
3483 | ||
379ca9cf OF |
3484 | /* |
3485 | * For pools which have the multihost property on determine if the | |
3486 | * pool is truly inactive and can be safely imported. Prevent | |
3487 | * hosts which don't have a hostid set from importing the pool. | |
3488 | */ | |
6cb8e530 PZ |
3489 | activity_check = spa_activity_check_required(spa, ub, label, |
3490 | spa->spa_config); | |
379ca9cf | 3491 | if (activity_check) { |
379ca9cf | 3492 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && |
25f06d67 | 3493 | spa_get_hostid(spa) == 0) { |
379ca9cf OF |
3494 | nvlist_free(label); |
3495 | fnvlist_add_uint64(spa->spa_load_info, | |
3496 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3497 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3498 | } | |
3499 | ||
6cb8e530 | 3500 | int error = spa_activity_check(spa, ub, spa->spa_config); |
e889f0f5 OF |
3501 | if (error) { |
3502 | nvlist_free(label); | |
3503 | return (error); | |
3504 | } | |
3505 | ||
379ca9cf OF |
3506 | fnvlist_add_uint64(spa->spa_load_info, |
3507 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); | |
3508 | fnvlist_add_uint64(spa->spa_load_info, | |
3509 | ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); | |
060f0226 OF |
3510 | fnvlist_add_uint16(spa->spa_load_info, |
3511 | ZPOOL_CONFIG_MMP_SEQ, | |
3512 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)); | |
379ca9cf OF |
3513 | } |
3514 | ||
428870ff | 3515 | /* |
9ae529ec | 3516 | * If the pool has an unsupported version we can't open it. |
428870ff | 3517 | */ |
9ae529ec CS |
3518 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
3519 | nvlist_free(label); | |
4a0ee12a PZ |
3520 | spa_load_failed(spa, "version %llu is not supported", |
3521 | (u_longlong_t)ub->ub_version); | |
428870ff | 3522 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
3523 | } |
3524 | ||
3525 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3526 | nvlist_t *features; | |
3527 | ||
3528 | /* | |
3529 | * If we weren't able to find what's necessary for reading the | |
3530 | * MOS in the label, return failure. | |
3531 | */ | |
4a0ee12a PZ |
3532 | if (label == NULL) { |
3533 | spa_load_failed(spa, "label config unavailable"); | |
3534 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
3535 | ENXIO)); | |
3536 | } | |
3537 | ||
3538 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, | |
3539 | &features) != 0) { | |
9ae529ec | 3540 | nvlist_free(label); |
4a0ee12a PZ |
3541 | spa_load_failed(spa, "invalid label: '%s' missing", |
3542 | ZPOOL_CONFIG_FEATURES_FOR_READ); | |
9ae529ec CS |
3543 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3544 | ENXIO)); | |
3545 | } | |
3546 | ||
3547 | /* | |
3548 | * Update our in-core representation with the definitive values | |
3549 | * from the label. | |
3550 | */ | |
3551 | nvlist_free(spa->spa_label_features); | |
3552 | VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); | |
3553 | } | |
3554 | ||
3555 | nvlist_free(label); | |
3556 | ||
3557 | /* | |
3558 | * Look through entries in the label nvlist's features_for_read. If | |
3559 | * there is a feature listed there which we don't understand then we | |
3560 | * cannot open a pool. | |
3561 | */ | |
3562 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3563 | nvlist_t *unsup_feat; | |
9ae529ec CS |
3564 | |
3565 | VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == | |
3566 | 0); | |
3567 | ||
1c27024e DB |
3568 | for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, |
3569 | NULL); nvp != NULL; | |
9ae529ec CS |
3570 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { |
3571 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
3572 | VERIFY(nvlist_add_string(unsup_feat, | |
3573 | nvpair_name(nvp), "") == 0); | |
3574 | } | |
3575 | } | |
3576 | ||
3577 | if (!nvlist_empty(unsup_feat)) { | |
3578 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
3579 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); | |
3580 | nvlist_free(unsup_feat); | |
4a0ee12a | 3581 | spa_load_failed(spa, "some features are unsupported"); |
9ae529ec CS |
3582 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3583 | ENOTSUP)); | |
3584 | } | |
3585 | ||
3586 | nvlist_free(unsup_feat); | |
3587 | } | |
428870ff | 3588 | |
428870ff BB |
3589 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { |
3590 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 | 3591 | spa_try_repair(spa, spa->spa_config); |
428870ff BB |
3592 | spa_config_exit(spa, SCL_ALL, FTAG); |
3593 | nvlist_free(spa->spa_config_splitting); | |
3594 | spa->spa_config_splitting = NULL; | |
3595 | } | |
3596 | ||
3597 | /* | |
3598 | * Initialize internal SPA structures. | |
3599 | */ | |
d2734cce | 3600 | spa_ld_select_uberblock_done(spa, ub); |
428870ff | 3601 | |
9eb7b46e PZ |
3602 | return (0); |
3603 | } | |
3604 | ||
3605 | static int | |
3606 | spa_ld_open_rootbp(spa_t *spa) | |
3607 | { | |
3608 | int error = 0; | |
3609 | vdev_t *rvd = spa->spa_root_vdev; | |
a1d477c2 | 3610 | |
9ae529ec | 3611 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
4a0ee12a PZ |
3612 | if (error != 0) { |
3613 | spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " | |
3614 | "[error=%d]", error); | |
428870ff | 3615 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3616 | } |
428870ff BB |
3617 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; |
3618 | ||
9eb7b46e PZ |
3619 | return (0); |
3620 | } | |
3621 | ||
3622 | static int | |
d2734cce | 3623 | spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, |
6cb8e530 | 3624 | boolean_t reloading) |
9eb7b46e | 3625 | { |
6cb8e530 PZ |
3626 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
3627 | nvlist_t *nv, *mos_config, *policy; | |
3628 | int error = 0, copy_error; | |
3629 | uint64_t healthy_tvds, healthy_tvds_mos; | |
3630 | uint64_t mos_config_txg; | |
9eb7b46e | 3631 | |
4a0ee12a PZ |
3632 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) |
3633 | != 0) | |
428870ff BB |
3634 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3635 | ||
a1d477c2 | 3636 | /* |
6cb8e530 PZ |
3637 | * If we're assembling a pool from a split, the config provided is |
3638 | * already trusted so there is nothing to do. | |
a1d477c2 | 3639 | */ |
6cb8e530 PZ |
3640 | if (type == SPA_IMPORT_ASSEMBLE) |
3641 | return (0); | |
3642 | ||
3643 | healthy_tvds = spa_healthy_core_tvds(spa); | |
a1d477c2 | 3644 | |
6cb8e530 PZ |
3645 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) |
3646 | != 0) { | |
3647 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
3648 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3649 | } | |
3650 | ||
3651 | /* | |
3652 | * If we are doing an open, pool owner wasn't verified yet, thus do | |
3653 | * the verification here. | |
3654 | */ | |
3655 | if (spa->spa_load_state == SPA_LOAD_OPEN) { | |
3656 | error = spa_verify_host(spa, mos_config); | |
3657 | if (error != 0) { | |
a1d477c2 | 3658 | nvlist_free(mos_config); |
6cb8e530 | 3659 | return (error); |
a1d477c2 | 3660 | } |
6cb8e530 PZ |
3661 | } |
3662 | ||
3663 | nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); | |
a1d477c2 | 3664 | |
6cb8e530 PZ |
3665 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3666 | ||
3667 | /* | |
3668 | * Build a new vdev tree from the trusted config | |
3669 | */ | |
3670 | VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); | |
3671 | ||
3672 | /* | |
3673 | * Vdev paths in the MOS may be obsolete. If the untrusted config was | |
3674 | * obtained by scanning /dev/dsk, then it will have the right vdev | |
3675 | * paths. We update the trusted MOS config with this information. | |
3676 | * We first try to copy the paths with vdev_copy_path_strict, which | |
3677 | * succeeds only when both configs have exactly the same vdev tree. | |
3678 | * If that fails, we fall back to a more flexible method that has a | |
3679 | * best effort policy. | |
3680 | */ | |
3681 | copy_error = vdev_copy_path_strict(rvd, mrvd); | |
3682 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3683 | spa_load_note(spa, "provided vdev tree:"); | |
3684 | vdev_dbgmsg_print_tree(rvd, 2); | |
3685 | spa_load_note(spa, "MOS vdev tree:"); | |
3686 | vdev_dbgmsg_print_tree(mrvd, 2); | |
3687 | } | |
3688 | if (copy_error != 0) { | |
3689 | spa_load_note(spa, "vdev_copy_path_strict failed, falling " | |
3690 | "back to vdev_copy_path_relaxed"); | |
3691 | vdev_copy_path_relaxed(rvd, mrvd); | |
3692 | } | |
3693 | ||
3694 | vdev_close(rvd); | |
3695 | vdev_free(rvd); | |
3696 | spa->spa_root_vdev = mrvd; | |
3697 | rvd = mrvd; | |
3698 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3699 | ||
3700 | /* | |
3701 | * We will use spa_config if we decide to reload the spa or if spa_load | |
3702 | * fails and we rewind. We must thus regenerate the config using the | |
8a393be3 PZ |
3703 | * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to |
3704 | * pass settings on how to load the pool and is not stored in the MOS. | |
3705 | * We copy it over to our new, trusted config. | |
6cb8e530 PZ |
3706 | */ |
3707 | mos_config_txg = fnvlist_lookup_uint64(mos_config, | |
3708 | ZPOOL_CONFIG_POOL_TXG); | |
3709 | nvlist_free(mos_config); | |
3710 | mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); | |
8a393be3 | 3711 | if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, |
6cb8e530 | 3712 | &policy) == 0) |
8a393be3 | 3713 | fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); |
6cb8e530 PZ |
3714 | spa_config_set(spa, mos_config); |
3715 | spa->spa_config_source = SPA_CONFIG_SRC_MOS; | |
3716 | ||
3717 | /* | |
3718 | * Now that we got the config from the MOS, we should be more strict | |
3719 | * in checking blkptrs and can make assumptions about the consistency | |
3720 | * of the vdev tree. spa_trust_config must be set to true before opening | |
3721 | * vdevs in order for them to be writeable. | |
3722 | */ | |
3723 | spa->spa_trust_config = B_TRUE; | |
3724 | ||
3725 | /* | |
3726 | * Open and validate the new vdev tree | |
3727 | */ | |
3728 | error = spa_ld_open_vdevs(spa); | |
3729 | if (error != 0) | |
3730 | return (error); | |
3731 | ||
3732 | error = spa_ld_validate_vdevs(spa); | |
3733 | if (error != 0) | |
3734 | return (error); | |
3735 | ||
3736 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3737 | spa_load_note(spa, "final vdev tree:"); | |
3738 | vdev_dbgmsg_print_tree(rvd, 2); | |
3739 | } | |
3740 | ||
3741 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && | |
3742 | !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { | |
a1d477c2 | 3743 | /* |
6cb8e530 PZ |
3744 | * Sanity check to make sure that we are indeed loading the |
3745 | * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds | |
3746 | * in the config provided and they happened to be the only ones | |
3747 | * to have the latest uberblock, we could involuntarily perform | |
3748 | * an extreme rewind. | |
a1d477c2 | 3749 | */ |
6cb8e530 PZ |
3750 | healthy_tvds_mos = spa_healthy_core_tvds(spa); |
3751 | if (healthy_tvds_mos - healthy_tvds >= | |
3752 | SPA_SYNC_MIN_VDEVS) { | |
3753 | spa_load_note(spa, "config provided misses too many " | |
3754 | "top-level vdevs compared to MOS (%lld vs %lld). ", | |
3755 | (u_longlong_t)healthy_tvds, | |
3756 | (u_longlong_t)healthy_tvds_mos); | |
3757 | spa_load_note(spa, "vdev tree:"); | |
3758 | vdev_dbgmsg_print_tree(rvd, 2); | |
3759 | if (reloading) { | |
3760 | spa_load_failed(spa, "config was already " | |
3761 | "provided from MOS. Aborting."); | |
3762 | return (spa_vdev_err(rvd, | |
3763 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3764 | } | |
3765 | spa_load_note(spa, "spa must be reloaded using MOS " | |
3766 | "config"); | |
3767 | return (SET_ERROR(EAGAIN)); | |
4a0ee12a | 3768 | } |
a1d477c2 MA |
3769 | } |
3770 | ||
6cb8e530 PZ |
3771 | error = spa_check_for_missing_logs(spa); |
3772 | if (error != 0) | |
3773 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
3774 | ||
3775 | if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { | |
3776 | spa_load_failed(spa, "uberblock guid sum doesn't match MOS " | |
3777 | "guid sum (%llu != %llu)", | |
3778 | (u_longlong_t)spa->spa_uberblock.ub_guid_sum, | |
3779 | (u_longlong_t)rvd->vdev_guid_sum); | |
3780 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
3781 | ENXIO)); | |
3782 | } | |
3783 | ||
9eb7b46e PZ |
3784 | return (0); |
3785 | } | |
3786 | ||
3787 | static int | |
3788 | spa_ld_open_indirect_vdev_metadata(spa_t *spa) | |
3789 | { | |
3790 | int error = 0; | |
3791 | vdev_t *rvd = spa->spa_root_vdev; | |
3792 | ||
a1d477c2 MA |
3793 | /* |
3794 | * Everything that we read before spa_remove_init() must be stored | |
3795 | * on concreted vdevs. Therefore we do this as early as possible. | |
3796 | */ | |
4a0ee12a PZ |
3797 | error = spa_remove_init(spa); |
3798 | if (error != 0) { | |
3799 | spa_load_failed(spa, "spa_remove_init failed [error=%d]", | |
3800 | error); | |
a1d477c2 | 3801 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3802 | } |
a1d477c2 | 3803 | |
9eb7b46e PZ |
3804 | /* |
3805 | * Retrieve information needed to condense indirect vdev mappings. | |
3806 | */ | |
3807 | error = spa_condense_init(spa); | |
3808 | if (error != 0) { | |
4a0ee12a PZ |
3809 | spa_load_failed(spa, "spa_condense_init failed [error=%d]", |
3810 | error); | |
9eb7b46e PZ |
3811 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3812 | } | |
3813 | ||
3814 | return (0); | |
3815 | } | |
3816 | ||
3817 | static int | |
4a0ee12a | 3818 | spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) |
9eb7b46e PZ |
3819 | { |
3820 | int error = 0; | |
3821 | vdev_t *rvd = spa->spa_root_vdev; | |
3822 | ||
9ae529ec CS |
3823 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
3824 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 3825 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
3826 | |
3827 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
4a0ee12a | 3828 | &spa->spa_feat_for_read_obj, B_TRUE) != 0) { |
9ae529ec CS |
3829 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3830 | } | |
3831 | ||
3832 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
4a0ee12a | 3833 | &spa->spa_feat_for_write_obj, B_TRUE) != 0) { |
9ae529ec CS |
3834 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3835 | } | |
3836 | ||
3837 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
4a0ee12a | 3838 | &spa->spa_feat_desc_obj, B_TRUE) != 0) { |
9ae529ec CS |
3839 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3840 | } | |
3841 | ||
b9b24bb4 CS |
3842 | enabled_feat = fnvlist_alloc(); |
3843 | unsup_feat = fnvlist_alloc(); | |
9ae529ec | 3844 | |
fa86b5db | 3845 | if (!spa_features_check(spa, B_FALSE, |
b9b24bb4 | 3846 | unsup_feat, enabled_feat)) |
9ae529ec CS |
3847 | missing_feat_read = B_TRUE; |
3848 | ||
4a0ee12a PZ |
3849 | if (spa_writeable(spa) || |
3850 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) { | |
fa86b5db | 3851 | if (!spa_features_check(spa, B_TRUE, |
b9b24bb4 | 3852 | unsup_feat, enabled_feat)) { |
9eb7b46e | 3853 | *missing_feat_writep = B_TRUE; |
b9b24bb4 | 3854 | } |
9ae529ec CS |
3855 | } |
3856 | ||
b9b24bb4 CS |
3857 | fnvlist_add_nvlist(spa->spa_load_info, |
3858 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
3859 | ||
9ae529ec | 3860 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
3861 | fnvlist_add_nvlist(spa->spa_load_info, |
3862 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
3863 | } |
3864 | ||
b9b24bb4 CS |
3865 | fnvlist_free(enabled_feat); |
3866 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
3867 | |
3868 | if (!missing_feat_read) { | |
3869 | fnvlist_add_boolean(spa->spa_load_info, | |
3870 | ZPOOL_CONFIG_CAN_RDONLY); | |
3871 | } | |
3872 | ||
3873 | /* | |
3874 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
3875 | * twofold: to determine whether the pool is available for | |
3876 | * import in read-write mode and (if it is not) whether the | |
3877 | * pool is available for import in read-only mode. If the pool | |
3878 | * is available for import in read-write mode, it is displayed | |
3879 | * as available in userland; if it is not available for import | |
3880 | * in read-only mode, it is displayed as unavailable in | |
3881 | * userland. If the pool is available for import in read-only | |
3882 | * mode but not read-write mode, it is displayed as unavailable | |
3883 | * in userland with a special note that the pool is actually | |
3884 | * available for open in read-only mode. | |
3885 | * | |
3886 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
3887 | * missing a feature for write, we must first determine whether | |
3888 | * the pool can be opened read-only before returning to | |
3889 | * userland in order to know whether to display the | |
3890 | * abovementioned note. | |
3891 | */ | |
9eb7b46e | 3892 | if (missing_feat_read || (*missing_feat_writep && |
9ae529ec | 3893 | spa_writeable(spa))) { |
4a0ee12a | 3894 | spa_load_failed(spa, "pool uses unsupported features"); |
9ae529ec CS |
3895 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3896 | ENOTSUP)); | |
3897 | } | |
b0bc7a84 MG |
3898 | |
3899 | /* | |
3900 | * Load refcounts for ZFS features from disk into an in-memory | |
3901 | * cache during SPA initialization. | |
3902 | */ | |
1c27024e | 3903 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { |
b0bc7a84 MG |
3904 | uint64_t refcount; |
3905 | ||
3906 | error = feature_get_refcount_from_disk(spa, | |
3907 | &spa_feature_table[i], &refcount); | |
3908 | if (error == 0) { | |
3909 | spa->spa_feat_refcount_cache[i] = refcount; | |
3910 | } else if (error == ENOTSUP) { | |
3911 | spa->spa_feat_refcount_cache[i] = | |
3912 | SPA_FEATURE_DISABLED; | |
3913 | } else { | |
4a0ee12a PZ |
3914 | spa_load_failed(spa, "error getting refcount " |
3915 | "for feature %s [error=%d]", | |
3916 | spa_feature_table[i].fi_guid, error); | |
b0bc7a84 MG |
3917 | return (spa_vdev_err(rvd, |
3918 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3919 | } | |
3920 | } | |
3921 | } | |
3922 | ||
3923 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
3924 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
4a0ee12a | 3925 | &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) |
b0bc7a84 | 3926 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
9ae529ec CS |
3927 | } |
3928 | ||
f00ab3f2 TC |
3929 | /* |
3930 | * Encryption was added before bookmark_v2, even though bookmark_v2 | |
3931 | * is now a dependency. If this pool has encryption enabled without | |
3932 | * bookmark_v2, trigger an errata message. | |
3933 | */ | |
3934 | if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && | |
3935 | !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) { | |
3936 | spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; | |
3937 | } | |
3938 | ||
9eb7b46e PZ |
3939 | return (0); |
3940 | } | |
3941 | ||
3942 | static int | |
3943 | spa_ld_load_special_directories(spa_t *spa) | |
3944 | { | |
3945 | int error = 0; | |
3946 | vdev_t *rvd = spa->spa_root_vdev; | |
3947 | ||
9ae529ec CS |
3948 | spa->spa_is_initializing = B_TRUE; |
3949 | error = dsl_pool_open(spa->spa_dsl_pool); | |
3950 | spa->spa_is_initializing = B_FALSE; | |
4a0ee12a PZ |
3951 | if (error != 0) { |
3952 | spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); | |
9ae529ec | 3953 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3954 | } |
9ae529ec | 3955 | |
9eb7b46e PZ |
3956 | return (0); |
3957 | } | |
428870ff | 3958 | |
9eb7b46e PZ |
3959 | static int |
3960 | spa_ld_get_props(spa_t *spa) | |
3961 | { | |
3962 | int error = 0; | |
3963 | uint64_t obj; | |
3964 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 3965 | |
3c67d83a TH |
3966 | /* Grab the checksum salt from the MOS. */ |
3967 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3968 | DMU_POOL_CHECKSUM_SALT, 1, | |
3969 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
3970 | spa->spa_cksum_salt.zcs_bytes); | |
3971 | if (error == ENOENT) { | |
3972 | /* Generate a new salt for subsequent use */ | |
3973 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
3974 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
3975 | } else if (error != 0) { | |
4a0ee12a PZ |
3976 | spa_load_failed(spa, "unable to retrieve checksum salt from " |
3977 | "MOS [error=%d]", error); | |
3c67d83a TH |
3978 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3979 | } | |
3980 | ||
4a0ee12a | 3981 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) |
428870ff BB |
3982 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3983 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
4a0ee12a PZ |
3984 | if (error != 0) { |
3985 | spa_load_failed(spa, "error opening deferred-frees bpobj " | |
3986 | "[error=%d]", error); | |
428870ff | 3987 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3988 | } |
34dc7c2f BB |
3989 | |
3990 | /* | |
3991 | * Load the bit that tells us to use the new accounting function | |
3992 | * (raid-z deflation). If we have an older pool, this will not | |
3993 | * be present. | |
3994 | */ | |
4a0ee12a | 3995 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); |
428870ff BB |
3996 | if (error != 0 && error != ENOENT) |
3997 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3998 | ||
3999 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
4a0ee12a | 4000 | &spa->spa_creation_version, B_FALSE); |
428870ff BB |
4001 | if (error != 0 && error != ENOENT) |
4002 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
4003 | |
4004 | /* | |
4005 | * Load the persistent error log. If we have an older pool, this will | |
4006 | * not be present. | |
4007 | */ | |
4a0ee12a PZ |
4008 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, |
4009 | B_FALSE); | |
428870ff BB |
4010 | if (error != 0 && error != ENOENT) |
4011 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4012 | |
428870ff | 4013 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
4a0ee12a | 4014 | &spa->spa_errlog_scrub, B_FALSE); |
428870ff BB |
4015 | if (error != 0 && error != ENOENT) |
4016 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4017 | |
37f03da8 SH |
4018 | /* |
4019 | * Load the livelist deletion field. If a livelist is queued for | |
4020 | * deletion, indicate that in the spa | |
4021 | */ | |
4022 | error = spa_dir_prop(spa, DMU_POOL_DELETED_CLONES, | |
4023 | &spa->spa_livelists_to_delete, B_FALSE); | |
4024 | if (error != 0 && error != ENOENT) | |
4025 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4026 | ||
34dc7c2f BB |
4027 | /* |
4028 | * Load the history object. If we have an older pool, this | |
4029 | * will not be present. | |
4030 | */ | |
4a0ee12a | 4031 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); |
428870ff BB |
4032 | if (error != 0 && error != ENOENT) |
4033 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4034 | ||
e0ab3ab5 JS |
4035 | /* |
4036 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
4037 | * be present; in this case, defer its creation to a later time to | |
4038 | * avoid dirtying the MOS this early / out of sync context. See | |
4039 | * spa_sync_config_object. | |
4040 | */ | |
4041 | ||
4042 | /* The sentinel is only available in the MOS config. */ | |
1c27024e | 4043 | nvlist_t *mos_config; |
4a0ee12a PZ |
4044 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { |
4045 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
e0ab3ab5 | 4046 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4047 | } |
e0ab3ab5 JS |
4048 | |
4049 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
4a0ee12a | 4050 | &spa->spa_all_vdev_zaps, B_FALSE); |
e0ab3ab5 | 4051 | |
38640550 DB |
4052 | if (error == ENOENT) { |
4053 | VERIFY(!nvlist_exists(mos_config, | |
4054 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
4055 | spa->spa_avz_action = AVZ_ACTION_INITIALIZE; | |
4056 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4057 | } else if (error != 0) { | |
e0ab3ab5 | 4058 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
38640550 | 4059 | } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { |
e0ab3ab5 JS |
4060 | /* |
4061 | * An older version of ZFS overwrote the sentinel value, so | |
4062 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
4063 | * destruction to later; see spa_sync_config_object. | |
4064 | */ | |
4065 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
4066 | /* | |
4067 | * We're assuming that no vdevs have had their ZAPs created | |
4068 | * before this. Better be sure of it. | |
4069 | */ | |
4070 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4071 | } | |
4072 | nvlist_free(mos_config); | |
4073 | ||
9eb7b46e PZ |
4074 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
4075 | ||
4a0ee12a PZ |
4076 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, |
4077 | B_FALSE); | |
9eb7b46e PZ |
4078 | if (error && error != ENOENT) |
4079 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4080 | ||
4081 | if (error == 0) { | |
4082 | uint64_t autoreplace; | |
4083 | ||
4084 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
4085 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
4086 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
4087 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
4088 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
c02c1bec | 4089 | spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); |
1b939560 | 4090 | spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim); |
9eb7b46e PZ |
4091 | spa->spa_autoreplace = (autoreplace != 0); |
4092 | } | |
4093 | ||
6cb8e530 PZ |
4094 | /* |
4095 | * If we are importing a pool with missing top-level vdevs, | |
4096 | * we enforce that the pool doesn't panic or get suspended on | |
4097 | * error since the likelihood of missing data is extremely high. | |
4098 | */ | |
4099 | if (spa->spa_missing_tvds > 0 && | |
4100 | spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && | |
4101 | spa->spa_load_state != SPA_LOAD_TRYIMPORT) { | |
4102 | spa_load_note(spa, "forcing failmode to 'continue' " | |
4103 | "as some top level vdevs are missing"); | |
4104 | spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; | |
4105 | } | |
4106 | ||
9eb7b46e PZ |
4107 | return (0); |
4108 | } | |
4109 | ||
4110 | static int | |
4111 | spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) | |
4112 | { | |
4113 | int error = 0; | |
4114 | vdev_t *rvd = spa->spa_root_vdev; | |
4115 | ||
428870ff BB |
4116 | /* |
4117 | * If we're assembling the pool from the split-off vdevs of | |
4118 | * an existing pool, we don't want to attach the spares & cache | |
4119 | * devices. | |
4120 | */ | |
34dc7c2f BB |
4121 | |
4122 | /* | |
4123 | * Load any hot spares for this pool. | |
4124 | */ | |
4a0ee12a PZ |
4125 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, |
4126 | B_FALSE); | |
428870ff BB |
4127 | if (error != 0 && error != ENOENT) |
4128 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4129 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4130 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
4131 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
4a0ee12a PZ |
4132 | &spa->spa_spares.sav_config) != 0) { |
4133 | spa_load_failed(spa, "error loading spares nvlist"); | |
428870ff | 4134 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4135 | } |
34dc7c2f | 4136 | |
b128c09f | 4137 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4138 | spa_load_spares(spa); |
b128c09f | 4139 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4140 | } else if (error == 0) { |
4141 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
4142 | } |
4143 | ||
4144 | /* | |
4145 | * Load any level 2 ARC devices for this pool. | |
4146 | */ | |
428870ff | 4147 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
4a0ee12a | 4148 | &spa->spa_l2cache.sav_object, B_FALSE); |
428870ff BB |
4149 | if (error != 0 && error != ENOENT) |
4150 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4151 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4152 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
4153 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
4a0ee12a PZ |
4154 | &spa->spa_l2cache.sav_config) != 0) { |
4155 | spa_load_failed(spa, "error loading l2cache nvlist"); | |
428870ff | 4156 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4157 | } |
34dc7c2f | 4158 | |
b128c09f | 4159 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4160 | spa_load_l2cache(spa); |
b128c09f | 4161 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4162 | } else if (error == 0) { |
4163 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
4164 | } |
4165 | ||
9eb7b46e PZ |
4166 | return (0); |
4167 | } | |
428870ff | 4168 | |
9eb7b46e | 4169 | static int |
4a0ee12a | 4170 | spa_ld_load_vdev_metadata(spa_t *spa) |
9eb7b46e PZ |
4171 | { |
4172 | int error = 0; | |
4173 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4174 | |
379ca9cf OF |
4175 | /* |
4176 | * If the 'multihost' property is set, then never allow a pool to | |
4177 | * be imported when the system hostid is zero. The exception to | |
4178 | * this rule is zdb which is always allowed to access pools. | |
4179 | */ | |
25f06d67 | 4180 | if (spa_multihost(spa) && spa_get_hostid(spa) == 0 && |
379ca9cf OF |
4181 | (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { |
4182 | fnvlist_add_uint64(spa->spa_load_info, | |
4183 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
4184 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
4185 | } | |
4186 | ||
34dc7c2f BB |
4187 | /* |
4188 | * If the 'autoreplace' property is set, then post a resource notifying | |
4189 | * the ZFS DE that it should not issue any faults for unopenable | |
4190 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
4191 | * unopenable vdevs so that the normal autoreplace handler can take | |
4192 | * over. | |
4193 | */ | |
4a0ee12a | 4194 | if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 4195 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
4196 | /* |
4197 | * For the import case, this is done in spa_import(), because | |
4198 | * at this point we're using the spare definitions from | |
4199 | * the MOS config, not necessarily from the userland config. | |
4200 | */ | |
4a0ee12a | 4201 | if (spa->spa_load_state != SPA_LOAD_IMPORT) { |
428870ff BB |
4202 | spa_aux_check_removed(&spa->spa_spares); |
4203 | spa_aux_check_removed(&spa->spa_l2cache); | |
4204 | } | |
4205 | } | |
34dc7c2f BB |
4206 | |
4207 | /* | |
9eb7b46e | 4208 | * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. |
34dc7c2f | 4209 | */ |
a1d477c2 MA |
4210 | error = vdev_load(rvd); |
4211 | if (error != 0) { | |
4a0ee12a | 4212 | spa_load_failed(spa, "vdev_load failed [error=%d]", error); |
a1d477c2 MA |
4213 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
4214 | } | |
4215 | ||
93e28d66 SD |
4216 | error = spa_ld_log_spacemaps(spa); |
4217 | if (error != 0) { | |
4218 | spa_load_failed(spa, "spa_ld_log_sm_data failed [error=%d]", | |
4219 | error); | |
4220 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
4221 | } | |
4222 | ||
34dc7c2f | 4223 | /* |
9eb7b46e | 4224 | * Propagate the leaf DTLs we just loaded all the way up the vdev tree. |
34dc7c2f | 4225 | */ |
b128c09f | 4226 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9a49d3f3 | 4227 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE, B_FALSE); |
b128c09f | 4228 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 4229 | |
9eb7b46e PZ |
4230 | return (0); |
4231 | } | |
4232 | ||
4233 | static int | |
4234 | spa_ld_load_dedup_tables(spa_t *spa) | |
4235 | { | |
4236 | int error = 0; | |
4237 | vdev_t *rvd = spa->spa_root_vdev; | |
4238 | ||
428870ff | 4239 | error = ddt_load(spa); |
4a0ee12a PZ |
4240 | if (error != 0) { |
4241 | spa_load_failed(spa, "ddt_load failed [error=%d]", error); | |
428870ff | 4242 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4243 | } |
428870ff | 4244 | |
9eb7b46e PZ |
4245 | return (0); |
4246 | } | |
4247 | ||
4248 | static int | |
4249 | spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) | |
4250 | { | |
4251 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 4252 | |
4a0ee12a PZ |
4253 | if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { |
4254 | boolean_t missing = spa_check_logs(spa); | |
4255 | if (missing) { | |
6cb8e530 PZ |
4256 | if (spa->spa_missing_tvds != 0) { |
4257 | spa_load_note(spa, "spa_check_logs failed " | |
4258 | "so dropping the logs"); | |
4259 | } else { | |
4260 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
4261 | spa_load_failed(spa, "spa_check_logs failed"); | |
4262 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, | |
4263 | ENXIO)); | |
4264 | } | |
4a0ee12a | 4265 | } |
428870ff BB |
4266 | } |
4267 | ||
9eb7b46e PZ |
4268 | return (0); |
4269 | } | |
4270 | ||
4271 | static int | |
4a0ee12a | 4272 | spa_ld_verify_pool_data(spa_t *spa) |
9eb7b46e PZ |
4273 | { |
4274 | int error = 0; | |
4275 | vdev_t *rvd = spa->spa_root_vdev; | |
4276 | ||
4277 | /* | |
4278 | * We've successfully opened the pool, verify that we're ready | |
4279 | * to start pushing transactions. | |
4280 | */ | |
4a0ee12a | 4281 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
9eb7b46e PZ |
4282 | error = spa_load_verify(spa); |
4283 | if (error != 0) { | |
4a0ee12a PZ |
4284 | spa_load_failed(spa, "spa_load_verify failed " |
4285 | "[error=%d]", error); | |
9eb7b46e PZ |
4286 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
4287 | error)); | |
4288 | } | |
4289 | } | |
4290 | ||
4291 | return (0); | |
4292 | } | |
4293 | ||
4294 | static void | |
4295 | spa_ld_claim_log_blocks(spa_t *spa) | |
4296 | { | |
4297 | dmu_tx_t *tx; | |
4298 | dsl_pool_t *dp = spa_get_dsl(spa); | |
4299 | ||
4300 | /* | |
4301 | * Claim log blocks that haven't been committed yet. | |
4302 | * This must all happen in a single txg. | |
4303 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
4304 | * invoked from zil_claim_log_block()'s i/o done callback. | |
4305 | * Price of rollback is that we abandon the log. | |
4306 | */ | |
4307 | spa->spa_claiming = B_TRUE; | |
4308 | ||
4309 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
4310 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
4311 | zil_claim, tx, DS_FIND_CHILDREN); | |
4312 | dmu_tx_commit(tx); | |
4313 | ||
4314 | spa->spa_claiming = B_FALSE; | |
4315 | ||
4316 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
4317 | } | |
4318 | ||
4319 | static void | |
6cb8e530 | 4320 | spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, |
d2734cce | 4321 | boolean_t update_config_cache) |
9eb7b46e PZ |
4322 | { |
4323 | vdev_t *rvd = spa->spa_root_vdev; | |
4324 | int need_update = B_FALSE; | |
4325 | ||
4326 | /* | |
4327 | * If the config cache is stale, or we have uninitialized | |
4328 | * metaslabs (see spa_vdev_add()), then update the config. | |
4329 | * | |
4330 | * If this is a verbatim import, trust the current | |
4331 | * in-core spa_config and update the disk labels. | |
4332 | */ | |
d2734cce | 4333 | if (update_config_cache || config_cache_txg != spa->spa_config_txg || |
4a0ee12a PZ |
4334 | spa->spa_load_state == SPA_LOAD_IMPORT || |
4335 | spa->spa_load_state == SPA_LOAD_RECOVER || | |
9eb7b46e PZ |
4336 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) |
4337 | need_update = B_TRUE; | |
4338 | ||
4339 | for (int c = 0; c < rvd->vdev_children; c++) | |
4340 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
4341 | need_update = B_TRUE; | |
4342 | ||
4343 | /* | |
e1cfd73f | 4344 | * Update the config cache asynchronously in case we're the |
9eb7b46e PZ |
4345 | * root pool, in which case the config cache isn't writable yet. |
4346 | */ | |
4347 | if (need_update) | |
4348 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
4349 | } | |
4350 | ||
6cb8e530 PZ |
4351 | static void |
4352 | spa_ld_prepare_for_reload(spa_t *spa) | |
4353 | { | |
da92d5cb | 4354 | spa_mode_t mode = spa->spa_mode; |
6cb8e530 PZ |
4355 | int async_suspended = spa->spa_async_suspended; |
4356 | ||
4357 | spa_unload(spa); | |
4358 | spa_deactivate(spa); | |
4359 | spa_activate(spa, mode); | |
4360 | ||
4361 | /* | |
4362 | * We save the value of spa_async_suspended as it gets reset to 0 by | |
4363 | * spa_unload(). We want to restore it back to the original value before | |
4364 | * returning as we might be calling spa_async_resume() later. | |
4365 | */ | |
4366 | spa->spa_async_suspended = async_suspended; | |
4367 | } | |
4368 | ||
9eb7b46e | 4369 | static int |
d2734cce SD |
4370 | spa_ld_read_checkpoint_txg(spa_t *spa) |
4371 | { | |
4372 | uberblock_t checkpoint; | |
4373 | int error = 0; | |
4374 | ||
4375 | ASSERT0(spa->spa_checkpoint_txg); | |
4376 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4377 | ||
4378 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4379 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4380 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4381 | ||
4382 | if (error == ENOENT) | |
4383 | return (0); | |
4384 | ||
4385 | if (error != 0) | |
4386 | return (error); | |
4387 | ||
4388 | ASSERT3U(checkpoint.ub_txg, !=, 0); | |
4389 | ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); | |
4390 | ASSERT3U(checkpoint.ub_timestamp, !=, 0); | |
4391 | spa->spa_checkpoint_txg = checkpoint.ub_txg; | |
4392 | spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; | |
4393 | ||
4394 | return (0); | |
4395 | } | |
4396 | ||
4397 | static int | |
4398 | spa_ld_mos_init(spa_t *spa, spa_import_type_t type) | |
9eb7b46e PZ |
4399 | { |
4400 | int error = 0; | |
9eb7b46e | 4401 | |
4a0ee12a | 4402 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6cb8e530 | 4403 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); |
4a0ee12a | 4404 | |
9eb7b46e | 4405 | /* |
6cb8e530 PZ |
4406 | * Never trust the config that is provided unless we are assembling |
4407 | * a pool following a split. | |
4408 | * This means don't trust blkptrs and the vdev tree in general. This | |
4409 | * also effectively puts the spa in read-only mode since | |
4410 | * spa_writeable() checks for spa_trust_config to be true. | |
4411 | * We will later load a trusted config from the MOS. | |
9eb7b46e | 4412 | */ |
6cb8e530 PZ |
4413 | if (type != SPA_IMPORT_ASSEMBLE) |
4414 | spa->spa_trust_config = B_FALSE; | |
4415 | ||
9eb7b46e PZ |
4416 | /* |
4417 | * Parse the config provided to create a vdev tree. | |
4418 | */ | |
6cb8e530 | 4419 | error = spa_ld_parse_config(spa, type); |
9eb7b46e PZ |
4420 | if (error != 0) |
4421 | return (error); | |
4422 | ||
ca95f70d OF |
4423 | spa_import_progress_add(spa); |
4424 | ||
9eb7b46e PZ |
4425 | /* |
4426 | * Now that we have the vdev tree, try to open each vdev. This involves | |
4427 | * opening the underlying physical device, retrieving its geometry and | |
4428 | * probing the vdev with a dummy I/O. The state of each vdev will be set | |
4429 | * based on the success of those operations. After this we'll be ready | |
4430 | * to read from the vdevs. | |
4431 | */ | |
4432 | error = spa_ld_open_vdevs(spa); | |
4433 | if (error != 0) | |
4434 | return (error); | |
4435 | ||
4436 | /* | |
4437 | * Read the label of each vdev and make sure that the GUIDs stored | |
4438 | * there match the GUIDs in the config provided. | |
6cb8e530 PZ |
4439 | * If we're assembling a new pool that's been split off from an |
4440 | * existing pool, the labels haven't yet been updated so we skip | |
4441 | * validation for now. | |
9eb7b46e | 4442 | */ |
6cb8e530 PZ |
4443 | if (type != SPA_IMPORT_ASSEMBLE) { |
4444 | error = spa_ld_validate_vdevs(spa); | |
4445 | if (error != 0) | |
4446 | return (error); | |
4447 | } | |
9eb7b46e PZ |
4448 | |
4449 | /* | |
d2734cce SD |
4450 | * Read all vdev labels to find the best uberblock (i.e. latest, |
4451 | * unless spa_load_max_txg is set) and store it in spa_uberblock. We | |
4452 | * get the list of features required to read blkptrs in the MOS from | |
4453 | * the vdev label with the best uberblock and verify that our version | |
4454 | * of zfs supports them all. | |
9eb7b46e | 4455 | */ |
6cb8e530 | 4456 | error = spa_ld_select_uberblock(spa, type); |
9eb7b46e PZ |
4457 | if (error != 0) |
4458 | return (error); | |
4459 | ||
4460 | /* | |
4461 | * Pass that uberblock to the dsl_pool layer which will open the root | |
4462 | * blkptr. This blkptr points to the latest version of the MOS and will | |
4463 | * allow us to read its contents. | |
4464 | */ | |
4465 | error = spa_ld_open_rootbp(spa); | |
4466 | if (error != 0) | |
4467 | return (error); | |
4468 | ||
d2734cce SD |
4469 | return (0); |
4470 | } | |
4471 | ||
4472 | static int | |
4473 | spa_ld_checkpoint_rewind(spa_t *spa) | |
4474 | { | |
4475 | uberblock_t checkpoint; | |
4476 | int error = 0; | |
4477 | ||
4478 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4479 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4480 | ||
4481 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4482 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4483 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4484 | ||
4485 | if (error != 0) { | |
4486 | spa_load_failed(spa, "unable to retrieve checkpointed " | |
4487 | "uberblock from the MOS config [error=%d]", error); | |
4488 | ||
4489 | if (error == ENOENT) | |
4490 | error = ZFS_ERR_NO_CHECKPOINT; | |
4491 | ||
4492 | return (error); | |
4493 | } | |
4494 | ||
4495 | ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); | |
4496 | ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); | |
4497 | ||
4498 | /* | |
4499 | * We need to update the txg and timestamp of the checkpointed | |
4500 | * uberblock to be higher than the latest one. This ensures that | |
4501 | * the checkpointed uberblock is selected if we were to close and | |
4502 | * reopen the pool right after we've written it in the vdev labels. | |
4503 | * (also see block comment in vdev_uberblock_compare) | |
4504 | */ | |
4505 | checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; | |
4506 | checkpoint.ub_timestamp = gethrestime_sec(); | |
4507 | ||
4508 | /* | |
4509 | * Set current uberblock to be the checkpointed uberblock. | |
4510 | */ | |
4511 | spa->spa_uberblock = checkpoint; | |
4512 | ||
4513 | /* | |
4514 | * If we are doing a normal rewind, then the pool is open for | |
4515 | * writing and we sync the "updated" checkpointed uberblock to | |
4516 | * disk. Once this is done, we've basically rewound the whole | |
4517 | * pool and there is no way back. | |
4518 | * | |
4519 | * There are cases when we don't want to attempt and sync the | |
4520 | * checkpointed uberblock to disk because we are opening a | |
4521 | * pool as read-only. Specifically, verifying the checkpointed | |
4522 | * state with zdb, and importing the checkpointed state to get | |
4523 | * a "preview" of its content. | |
4524 | */ | |
4525 | if (spa_writeable(spa)) { | |
4526 | vdev_t *rvd = spa->spa_root_vdev; | |
4527 | ||
4528 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4529 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; | |
4530 | int svdcount = 0; | |
4531 | int children = rvd->vdev_children; | |
4532 | int c0 = spa_get_random(children); | |
4533 | ||
4534 | for (int c = 0; c < children; c++) { | |
4535 | vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; | |
4536 | ||
4537 | /* Stop when revisiting the first vdev */ | |
4538 | if (c > 0 && svd[0] == vd) | |
4539 | break; | |
4540 | ||
4541 | if (vd->vdev_ms_array == 0 || vd->vdev_islog || | |
4542 | !vdev_is_concrete(vd)) | |
4543 | continue; | |
4544 | ||
4545 | svd[svdcount++] = vd; | |
4546 | if (svdcount == SPA_SYNC_MIN_VDEVS) | |
4547 | break; | |
4548 | } | |
4549 | error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); | |
4550 | if (error == 0) | |
4551 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
4552 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4553 | ||
4554 | if (error != 0) { | |
4555 | spa_load_failed(spa, "failed to write checkpointed " | |
4556 | "uberblock to the vdev labels [error=%d]", error); | |
4557 | return (error); | |
4558 | } | |
4559 | } | |
4560 | ||
4561 | return (0); | |
4562 | } | |
4563 | ||
4564 | static int | |
4565 | spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, | |
4566 | boolean_t *update_config_cache) | |
4567 | { | |
4568 | int error; | |
4569 | ||
4570 | /* | |
4571 | * Parse the config for pool, open and validate vdevs, | |
4572 | * select an uberblock, and use that uberblock to open | |
4573 | * the MOS. | |
4574 | */ | |
4575 | error = spa_ld_mos_init(spa, type); | |
4576 | if (error != 0) | |
4577 | return (error); | |
4578 | ||
9eb7b46e | 4579 | /* |
6cb8e530 PZ |
4580 | * Retrieve the trusted config stored in the MOS and use it to create |
4581 | * a new, exact version of the vdev tree, then reopen all vdevs. | |
9eb7b46e | 4582 | */ |
d2734cce | 4583 | error = spa_ld_trusted_config(spa, type, B_FALSE); |
6cb8e530 | 4584 | if (error == EAGAIN) { |
d2734cce SD |
4585 | if (update_config_cache != NULL) |
4586 | *update_config_cache = B_TRUE; | |
4587 | ||
6cb8e530 PZ |
4588 | /* |
4589 | * Redo the loading process with the trusted config if it is | |
4590 | * too different from the untrusted config. | |
4591 | */ | |
4592 | spa_ld_prepare_for_reload(spa); | |
d2734cce SD |
4593 | spa_load_note(spa, "RELOADING"); |
4594 | error = spa_ld_mos_init(spa, type); | |
4595 | if (error != 0) | |
4596 | return (error); | |
4597 | ||
4598 | error = spa_ld_trusted_config(spa, type, B_TRUE); | |
4599 | if (error != 0) | |
4600 | return (error); | |
4601 | ||
6cb8e530 | 4602 | } else if (error != 0) { |
9eb7b46e | 4603 | return (error); |
6cb8e530 | 4604 | } |
9eb7b46e | 4605 | |
d2734cce SD |
4606 | return (0); |
4607 | } | |
4608 | ||
4609 | /* | |
4610 | * Load an existing storage pool, using the config provided. This config | |
4611 | * describes which vdevs are part of the pool and is later validated against | |
4612 | * partial configs present in each vdev's label and an entire copy of the | |
4613 | * config stored in the MOS. | |
4614 | */ | |
4615 | static int | |
4616 | spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport) | |
4617 | { | |
4618 | int error = 0; | |
4619 | boolean_t missing_feat_write = B_FALSE; | |
4620 | boolean_t checkpoint_rewind = | |
4621 | (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4622 | boolean_t update_config_cache = B_FALSE; | |
4623 | ||
4624 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4625 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); | |
4626 | ||
4627 | spa_load_note(spa, "LOADING"); | |
4628 | ||
4629 | error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); | |
4630 | if (error != 0) | |
4631 | return (error); | |
4632 | ||
4633 | /* | |
4634 | * If we are rewinding to the checkpoint then we need to repeat | |
4635 | * everything we've done so far in this function but this time | |
4636 | * selecting the checkpointed uberblock and using that to open | |
4637 | * the MOS. | |
4638 | */ | |
4639 | if (checkpoint_rewind) { | |
4640 | /* | |
4641 | * If we are rewinding to the checkpoint update config cache | |
4642 | * anyway. | |
4643 | */ | |
4644 | update_config_cache = B_TRUE; | |
4645 | ||
4646 | /* | |
4647 | * Extract the checkpointed uberblock from the current MOS | |
4648 | * and use this as the pool's uberblock from now on. If the | |
4649 | * pool is imported as writeable we also write the checkpoint | |
4650 | * uberblock to the labels, making the rewind permanent. | |
4651 | */ | |
4652 | error = spa_ld_checkpoint_rewind(spa); | |
4653 | if (error != 0) | |
4654 | return (error); | |
4655 | ||
4656 | /* | |
e1cfd73f | 4657 | * Redo the loading process again with the |
d2734cce SD |
4658 | * checkpointed uberblock. |
4659 | */ | |
4660 | spa_ld_prepare_for_reload(spa); | |
4661 | spa_load_note(spa, "LOADING checkpointed uberblock"); | |
4662 | error = spa_ld_mos_with_trusted_config(spa, type, NULL); | |
4663 | if (error != 0) | |
4664 | return (error); | |
4665 | } | |
4666 | ||
4667 | /* | |
4668 | * Retrieve the checkpoint txg if the pool has a checkpoint. | |
4669 | */ | |
4670 | error = spa_ld_read_checkpoint_txg(spa); | |
4671 | if (error != 0) | |
4672 | return (error); | |
4673 | ||
9eb7b46e PZ |
4674 | /* |
4675 | * Retrieve the mapping of indirect vdevs. Those vdevs were removed | |
4676 | * from the pool and their contents were re-mapped to other vdevs. Note | |
4677 | * that everything that we read before this step must have been | |
4678 | * rewritten on concrete vdevs after the last device removal was | |
4679 | * initiated. Otherwise we could be reading from indirect vdevs before | |
4680 | * we have loaded their mappings. | |
4681 | */ | |
4682 | error = spa_ld_open_indirect_vdev_metadata(spa); | |
4683 | if (error != 0) | |
4684 | return (error); | |
4685 | ||
4686 | /* | |
4687 | * Retrieve the full list of active features from the MOS and check if | |
4688 | * they are all supported. | |
4689 | */ | |
4a0ee12a | 4690 | error = spa_ld_check_features(spa, &missing_feat_write); |
9eb7b46e PZ |
4691 | if (error != 0) |
4692 | return (error); | |
4693 | ||
4694 | /* | |
4695 | * Load several special directories from the MOS needed by the dsl_pool | |
4696 | * layer. | |
4697 | */ | |
4698 | error = spa_ld_load_special_directories(spa); | |
4699 | if (error != 0) | |
4700 | return (error); | |
4701 | ||
9eb7b46e PZ |
4702 | /* |
4703 | * Retrieve pool properties from the MOS. | |
4704 | */ | |
4705 | error = spa_ld_get_props(spa); | |
4706 | if (error != 0) | |
4707 | return (error); | |
4708 | ||
4709 | /* | |
4710 | * Retrieve the list of auxiliary devices - cache devices and spares - | |
4711 | * and open them. | |
4712 | */ | |
4713 | error = spa_ld_open_aux_vdevs(spa, type); | |
4714 | if (error != 0) | |
4715 | return (error); | |
4716 | ||
4717 | /* | |
4718 | * Load the metadata for all vdevs. Also check if unopenable devices | |
4719 | * should be autoreplaced. | |
4720 | */ | |
4a0ee12a | 4721 | error = spa_ld_load_vdev_metadata(spa); |
9eb7b46e PZ |
4722 | if (error != 0) |
4723 | return (error); | |
4724 | ||
4725 | error = spa_ld_load_dedup_tables(spa); | |
4726 | if (error != 0) | |
4727 | return (error); | |
4728 | ||
4729 | /* | |
4730 | * Verify the logs now to make sure we don't have any unexpected errors | |
4731 | * when we claim log blocks later. | |
4732 | */ | |
4733 | error = spa_ld_verify_logs(spa, type, ereport); | |
4734 | if (error != 0) | |
4735 | return (error); | |
4736 | ||
9ae529ec | 4737 | if (missing_feat_write) { |
6cb8e530 | 4738 | ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); |
9ae529ec CS |
4739 | |
4740 | /* | |
4741 | * At this point, we know that we can open the pool in | |
4742 | * read-only mode but not read-write mode. We now have enough | |
4743 | * information and can return to userland. | |
4744 | */ | |
9eb7b46e PZ |
4745 | return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, |
4746 | ENOTSUP)); | |
9ae529ec CS |
4747 | } |
4748 | ||
572e2857 | 4749 | /* |
9eb7b46e PZ |
4750 | * Traverse the last txgs to make sure the pool was left off in a safe |
4751 | * state. When performing an extreme rewind, we verify the whole pool, | |
4752 | * which can take a very long time. | |
572e2857 | 4753 | */ |
4a0ee12a | 4754 | error = spa_ld_verify_pool_data(spa); |
9eb7b46e PZ |
4755 | if (error != 0) |
4756 | return (error); | |
572e2857 | 4757 | |
9eb7b46e PZ |
4758 | /* |
4759 | * Calculate the deflated space for the pool. This must be done before | |
4760 | * we write anything to the pool because we'd need to update the space | |
4761 | * accounting using the deflated sizes. | |
4762 | */ | |
4763 | spa_update_dspace(spa); | |
4764 | ||
4765 | /* | |
4766 | * We have now retrieved all the information we needed to open the | |
4767 | * pool. If we are importing the pool in read-write mode, a few | |
4768 | * additional steps must be performed to finish the import. | |
4769 | */ | |
6cb8e530 | 4770 | if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || |
428870ff | 4771 | spa->spa_load_max_txg == UINT64_MAX)) { |
6cb8e530 PZ |
4772 | uint64_t config_cache_txg = spa->spa_config_txg; |
4773 | ||
4774 | ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f | 4775 | |
d2734cce SD |
4776 | /* |
4777 | * In case of a checkpoint rewind, log the original txg | |
4778 | * of the checkpointed uberblock. | |
4779 | */ | |
4780 | if (checkpoint_rewind) { | |
4781 | spa_history_log_internal(spa, "checkpoint rewind", | |
4782 | NULL, "rewound state to txg=%llu", | |
4783 | (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); | |
4784 | } | |
4785 | ||
34dc7c2f | 4786 | /* |
9eb7b46e | 4787 | * Traverse the ZIL and claim all blocks. |
34dc7c2f | 4788 | */ |
9eb7b46e | 4789 | spa_ld_claim_log_blocks(spa); |
428870ff | 4790 | |
9eb7b46e PZ |
4791 | /* |
4792 | * Kick-off the syncing thread. | |
4793 | */ | |
34dc7c2f BB |
4794 | spa->spa_sync_on = B_TRUE; |
4795 | txg_sync_start(spa->spa_dsl_pool); | |
379ca9cf | 4796 | mmp_thread_start(spa); |
34dc7c2f BB |
4797 | |
4798 | /* | |
428870ff BB |
4799 | * Wait for all claims to sync. We sync up to the highest |
4800 | * claimed log block birth time so that claimed log blocks | |
4801 | * don't appear to be from the future. spa_claim_max_txg | |
9eb7b46e PZ |
4802 | * will have been set for us by ZIL traversal operations |
4803 | * performed above. | |
34dc7c2f | 4804 | */ |
428870ff | 4805 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
4806 | |
4807 | /* | |
9eb7b46e PZ |
4808 | * Check if we need to request an update of the config. On the |
4809 | * next sync, we would update the config stored in vdev labels | |
4810 | * and the cachefile (by default /etc/zfs/zpool.cache). | |
34dc7c2f | 4811 | */ |
6cb8e530 | 4812 | spa_ld_check_for_config_update(spa, config_cache_txg, |
d2734cce | 4813 | update_config_cache); |
fb5f0bc8 BB |
4814 | |
4815 | /* | |
9a49d3f3 BB |
4816 | * Check if a rebuild was in progress and if so resume it. |
4817 | * Then check all DTLs to see if anything needs resilvering. | |
4818 | * The resilver will be deferred if a rebuild was started. | |
fb5f0bc8 | 4819 | */ |
9a49d3f3 BB |
4820 | if (vdev_rebuild_active(spa->spa_root_vdev)) { |
4821 | vdev_rebuild_restart(spa); | |
4822 | } else if (!dsl_scan_resilvering(spa->spa_dsl_pool) && | |
4823 | vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { | |
fb5f0bc8 | 4824 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
9a49d3f3 | 4825 | } |
428870ff | 4826 | |
6f1ffb06 MA |
4827 | /* |
4828 | * Log the fact that we booted up (so that we can detect if | |
4829 | * we rebooted in the middle of an operation). | |
4830 | */ | |
d5e024cb | 4831 | spa_history_log_version(spa, "open", NULL); |
6f1ffb06 | 4832 | |
9b2266e3 SD |
4833 | spa_restart_removal(spa); |
4834 | spa_spawn_aux_threads(spa); | |
4835 | ||
428870ff BB |
4836 | /* |
4837 | * Delete any inconsistent datasets. | |
9b2266e3 SD |
4838 | * |
4839 | * Note: | |
4840 | * Since we may be issuing deletes for clones here, | |
4841 | * we make sure to do so after we've spawned all the | |
4842 | * auxiliary threads above (from which the livelist | |
4843 | * deletion zthr is part of). | |
428870ff BB |
4844 | */ |
4845 | (void) dmu_objset_find(spa_name(spa), | |
4846 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
4847 | ||
4848 | /* | |
4849 | * Clean up any stale temporary dataset userrefs. | |
4850 | */ | |
4851 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
a1d477c2 | 4852 | |
619f0976 GW |
4853 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
4854 | vdev_initialize_restart(spa->spa_root_vdev); | |
1b939560 BB |
4855 | vdev_trim_restart(spa->spa_root_vdev); |
4856 | vdev_autotrim_restart(spa); | |
619f0976 | 4857 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
4858 | } |
4859 | ||
ca95f70d | 4860 | spa_import_progress_remove(spa_guid(spa)); |
77f6826b GA |
4861 | spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD); |
4862 | ||
4a0ee12a PZ |
4863 | spa_load_note(spa, "LOADED"); |
4864 | ||
428870ff BB |
4865 | return (0); |
4866 | } | |
34dc7c2f | 4867 | |
428870ff | 4868 | static int |
6cb8e530 | 4869 | spa_load_retry(spa_t *spa, spa_load_state_t state) |
428870ff | 4870 | { |
da92d5cb | 4871 | spa_mode_t mode = spa->spa_mode; |
572e2857 | 4872 | |
428870ff BB |
4873 | spa_unload(spa); |
4874 | spa_deactivate(spa); | |
4875 | ||
dea377c0 | 4876 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; |
428870ff | 4877 | |
572e2857 | 4878 | spa_activate(spa, mode); |
428870ff BB |
4879 | spa_async_suspend(spa); |
4880 | ||
4a0ee12a PZ |
4881 | spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", |
4882 | (u_longlong_t)spa->spa_load_max_txg); | |
4883 | ||
6cb8e530 | 4884 | return (spa_load(spa, state, SPA_IMPORT_EXISTING)); |
428870ff BB |
4885 | } |
4886 | ||
9ae529ec CS |
4887 | /* |
4888 | * If spa_load() fails this function will try loading prior txg's. If | |
4889 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
4890 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
4891 | * function will not rewind the pool and will return the same error as | |
4892 | * spa_load(). | |
4893 | */ | |
428870ff | 4894 | static int |
6cb8e530 PZ |
4895 | spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, |
4896 | int rewind_flags) | |
428870ff | 4897 | { |
9ae529ec | 4898 | nvlist_t *loadinfo = NULL; |
428870ff BB |
4899 | nvlist_t *config = NULL; |
4900 | int load_error, rewind_error; | |
4901 | uint64_t safe_rewind_txg; | |
4902 | uint64_t min_txg; | |
4903 | ||
4904 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
4905 | spa->spa_load_max_txg = spa->spa_load_txg; | |
4906 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
4907 | } else { | |
4908 | spa->spa_load_max_txg = max_request; | |
dea377c0 MA |
4909 | if (max_request != UINT64_MAX) |
4910 | spa->spa_extreme_rewind = B_TRUE; | |
428870ff BB |
4911 | } |
4912 | ||
6cb8e530 | 4913 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); |
428870ff BB |
4914 | if (load_error == 0) |
4915 | return (0); | |
d2734cce SD |
4916 | if (load_error == ZFS_ERR_NO_CHECKPOINT) { |
4917 | /* | |
4918 | * When attempting checkpoint-rewind on a pool with no | |
4919 | * checkpoint, we should not attempt to load uberblocks | |
4920 | * from previous txgs when spa_load fails. | |
4921 | */ | |
4922 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
ca95f70d | 4923 | spa_import_progress_remove(spa_guid(spa)); |
d2734cce SD |
4924 | return (load_error); |
4925 | } | |
428870ff BB |
4926 | |
4927 | if (spa->spa_root_vdev != NULL) | |
4928 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
4929 | ||
4930 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
4931 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
4932 | ||
4933 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
4934 | nvlist_free(config); | |
ca95f70d | 4935 | spa_import_progress_remove(spa_guid(spa)); |
428870ff BB |
4936 | return (load_error); |
4937 | } | |
4938 | ||
9ae529ec CS |
4939 | if (state == SPA_LOAD_RECOVER) { |
4940 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 4941 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
4942 | } else { |
4943 | /* | |
4944 | * If we aren't rolling back save the load info from our first | |
4945 | * import attempt so that we can restore it after attempting | |
4946 | * to rewind. | |
4947 | */ | |
4948 | loadinfo = spa->spa_load_info; | |
4949 | spa->spa_load_info = fnvlist_alloc(); | |
4950 | } | |
428870ff BB |
4951 | |
4952 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
4953 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
4954 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
4955 | TXG_INITIAL : safe_rewind_txg; | |
4956 | ||
4957 | /* | |
4958 | * Continue as long as we're finding errors, we're still within | |
4959 | * the acceptable rewind range, and we're still finding uberblocks | |
4960 | */ | |
4961 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
4962 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
4963 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
4964 | spa->spa_extreme_rewind = B_TRUE; | |
6cb8e530 | 4965 | rewind_error = spa_load_retry(spa, state); |
428870ff BB |
4966 | } |
4967 | ||
428870ff BB |
4968 | spa->spa_extreme_rewind = B_FALSE; |
4969 | spa->spa_load_max_txg = UINT64_MAX; | |
4970 | ||
4971 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
4972 | spa_config_set(spa, config); | |
ee6370a7 | 4973 | else |
4974 | nvlist_free(config); | |
428870ff | 4975 | |
9ae529ec CS |
4976 | if (state == SPA_LOAD_RECOVER) { |
4977 | ASSERT3P(loadinfo, ==, NULL); | |
ca95f70d | 4978 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
4979 | return (rewind_error); |
4980 | } else { | |
4981 | /* Store the rewind info as part of the initial load info */ | |
4982 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
4983 | spa->spa_load_info); | |
4984 | ||
4985 | /* Restore the initial load info */ | |
4986 | fnvlist_free(spa->spa_load_info); | |
4987 | spa->spa_load_info = loadinfo; | |
4988 | ||
ca95f70d | 4989 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
4990 | return (load_error); |
4991 | } | |
34dc7c2f BB |
4992 | } |
4993 | ||
4994 | /* | |
4995 | * Pool Open/Import | |
4996 | * | |
4997 | * The import case is identical to an open except that the configuration is sent | |
4998 | * down from userland, instead of grabbed from the configuration cache. For the | |
4999 | * case of an open, the pool configuration will exist in the | |
5000 | * POOL_STATE_UNINITIALIZED state. | |
5001 | * | |
5002 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
5003 | * the same time open the pool, without having to keep around the spa_t in some | |
5004 | * ambiguous state. | |
5005 | */ | |
5006 | static int | |
428870ff BB |
5007 | spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, |
5008 | nvlist_t **config) | |
34dc7c2f BB |
5009 | { |
5010 | spa_t *spa; | |
572e2857 | 5011 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 5012 | int error; |
34dc7c2f | 5013 | int locked = B_FALSE; |
526af785 | 5014 | int firstopen = B_FALSE; |
34dc7c2f BB |
5015 | |
5016 | *spapp = NULL; | |
5017 | ||
5018 | /* | |
5019 | * As disgusting as this is, we need to support recursive calls to this | |
5020 | * function because dsl_dir_open() is called during spa_load(), and ends | |
5021 | * up calling spa_open() again. The real fix is to figure out how to | |
5022 | * avoid dsl_dir_open() calling this in the first place. | |
5023 | */ | |
c25b8f99 | 5024 | if (MUTEX_NOT_HELD(&spa_namespace_lock)) { |
34dc7c2f BB |
5025 | mutex_enter(&spa_namespace_lock); |
5026 | locked = B_TRUE; | |
5027 | } | |
5028 | ||
5029 | if ((spa = spa_lookup(pool)) == NULL) { | |
5030 | if (locked) | |
5031 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5032 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 5033 | } |
428870ff | 5034 | |
34dc7c2f | 5035 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
8a393be3 | 5036 | zpool_load_policy_t policy; |
428870ff | 5037 | |
526af785 PJD |
5038 | firstopen = B_TRUE; |
5039 | ||
8a393be3 | 5040 | zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, |
428870ff | 5041 | &policy); |
8a393be3 | 5042 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) |
428870ff | 5043 | state = SPA_LOAD_RECOVER; |
34dc7c2f | 5044 | |
fb5f0bc8 | 5045 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5046 | |
428870ff BB |
5047 | if (state != SPA_LOAD_RECOVER) |
5048 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6cb8e530 | 5049 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; |
428870ff | 5050 | |
4a0ee12a | 5051 | zfs_dbgmsg("spa_open_common: opening %s", pool); |
8a393be3 PZ |
5052 | error = spa_load_best(spa, state, policy.zlp_txg, |
5053 | policy.zlp_rewind); | |
34dc7c2f BB |
5054 | |
5055 | if (error == EBADF) { | |
5056 | /* | |
5057 | * If vdev_validate() returns failure (indicated by | |
5058 | * EBADF), it indicates that one of the vdevs indicates | |
5059 | * that the pool has been exported or destroyed. If | |
5060 | * this is the case, the config cache is out of sync and | |
5061 | * we should remove the pool from the namespace. | |
5062 | */ | |
34dc7c2f BB |
5063 | spa_unload(spa); |
5064 | spa_deactivate(spa); | |
a1d477c2 | 5065 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 5066 | spa_remove(spa); |
34dc7c2f BB |
5067 | if (locked) |
5068 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5069 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
5070 | } |
5071 | ||
5072 | if (error) { | |
5073 | /* | |
5074 | * We can't open the pool, but we still have useful | |
5075 | * information: the state of each vdev after the | |
5076 | * attempted vdev_open(). Return this to the user. | |
5077 | */ | |
572e2857 | 5078 | if (config != NULL && spa->spa_config) { |
428870ff | 5079 | VERIFY(nvlist_dup(spa->spa_config, config, |
79c76d5b | 5080 | KM_SLEEP) == 0); |
572e2857 BB |
5081 | VERIFY(nvlist_add_nvlist(*config, |
5082 | ZPOOL_CONFIG_LOAD_INFO, | |
5083 | spa->spa_load_info) == 0); | |
5084 | } | |
34dc7c2f BB |
5085 | spa_unload(spa); |
5086 | spa_deactivate(spa); | |
428870ff | 5087 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
5088 | if (locked) |
5089 | mutex_exit(&spa_namespace_lock); | |
5090 | *spapp = NULL; | |
5091 | return (error); | |
34dc7c2f | 5092 | } |
34dc7c2f BB |
5093 | } |
5094 | ||
5095 | spa_open_ref(spa, tag); | |
5096 | ||
b128c09f | 5097 | if (config != NULL) |
34dc7c2f | 5098 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 5099 | |
572e2857 BB |
5100 | /* |
5101 | * If we've recovered the pool, pass back any information we | |
5102 | * gathered while doing the load. | |
5103 | */ | |
5104 | if (state == SPA_LOAD_RECOVER) { | |
5105 | VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, | |
5106 | spa->spa_load_info) == 0); | |
5107 | } | |
5108 | ||
428870ff BB |
5109 | if (locked) { |
5110 | spa->spa_last_open_failed = 0; | |
5111 | spa->spa_last_ubsync_txg = 0; | |
5112 | spa->spa_load_txg = 0; | |
5113 | mutex_exit(&spa_namespace_lock); | |
5114 | } | |
5115 | ||
526af785 | 5116 | if (firstopen) |
ec213971 | 5117 | zvol_create_minors_recursive(spa_name(spa)); |
526af785 | 5118 | |
428870ff BB |
5119 | *spapp = spa; |
5120 | ||
34dc7c2f BB |
5121 | return (0); |
5122 | } | |
5123 | ||
428870ff BB |
5124 | int |
5125 | spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, | |
5126 | nvlist_t **config) | |
5127 | { | |
5128 | return (spa_open_common(name, spapp, tag, policy, config)); | |
5129 | } | |
5130 | ||
34dc7c2f BB |
5131 | int |
5132 | spa_open(const char *name, spa_t **spapp, void *tag) | |
5133 | { | |
428870ff | 5134 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
5135 | } |
5136 | ||
5137 | /* | |
5138 | * Lookup the given spa_t, incrementing the inject count in the process, | |
5139 | * preventing it from being exported or destroyed. | |
5140 | */ | |
5141 | spa_t * | |
5142 | spa_inject_addref(char *name) | |
5143 | { | |
5144 | spa_t *spa; | |
5145 | ||
5146 | mutex_enter(&spa_namespace_lock); | |
5147 | if ((spa = spa_lookup(name)) == NULL) { | |
5148 | mutex_exit(&spa_namespace_lock); | |
5149 | return (NULL); | |
5150 | } | |
5151 | spa->spa_inject_ref++; | |
5152 | mutex_exit(&spa_namespace_lock); | |
5153 | ||
5154 | return (spa); | |
5155 | } | |
5156 | ||
5157 | void | |
5158 | spa_inject_delref(spa_t *spa) | |
5159 | { | |
5160 | mutex_enter(&spa_namespace_lock); | |
5161 | spa->spa_inject_ref--; | |
5162 | mutex_exit(&spa_namespace_lock); | |
5163 | } | |
5164 | ||
5165 | /* | |
5166 | * Add spares device information to the nvlist. | |
5167 | */ | |
5168 | static void | |
5169 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
5170 | { | |
5171 | nvlist_t **spares; | |
5172 | uint_t i, nspares; | |
5173 | nvlist_t *nvroot; | |
5174 | uint64_t guid; | |
5175 | vdev_stat_t *vs; | |
5176 | uint_t vsc; | |
5177 | uint64_t pool; | |
5178 | ||
9babb374 BB |
5179 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5180 | ||
34dc7c2f BB |
5181 | if (spa->spa_spares.sav_count == 0) |
5182 | return; | |
5183 | ||
5184 | VERIFY(nvlist_lookup_nvlist(config, | |
5185 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
5186 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
5187 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
5188 | if (nspares != 0) { | |
5189 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
5190 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
5191 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
5192 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
5193 | ||
5194 | /* | |
5195 | * Go through and find any spares which have since been | |
5196 | * repurposed as an active spare. If this is the case, update | |
5197 | * their status appropriately. | |
5198 | */ | |
5199 | for (i = 0; i < nspares; i++) { | |
5200 | VERIFY(nvlist_lookup_uint64(spares[i], | |
5201 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
b128c09f BB |
5202 | if (spa_spare_exists(guid, &pool, NULL) && |
5203 | pool != 0ULL) { | |
34dc7c2f | 5204 | VERIFY(nvlist_lookup_uint64_array( |
428870ff | 5205 | spares[i], ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
5206 | (uint64_t **)&vs, &vsc) == 0); |
5207 | vs->vs_state = VDEV_STATE_CANT_OPEN; | |
5208 | vs->vs_aux = VDEV_AUX_SPARED; | |
5209 | } | |
5210 | } | |
5211 | } | |
5212 | } | |
5213 | ||
5214 | /* | |
5215 | * Add l2cache device information to the nvlist, including vdev stats. | |
5216 | */ | |
5217 | static void | |
5218 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
5219 | { | |
5220 | nvlist_t **l2cache; | |
5221 | uint_t i, j, nl2cache; | |
5222 | nvlist_t *nvroot; | |
5223 | uint64_t guid; | |
5224 | vdev_t *vd; | |
5225 | vdev_stat_t *vs; | |
5226 | uint_t vsc; | |
5227 | ||
9babb374 BB |
5228 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5229 | ||
34dc7c2f BB |
5230 | if (spa->spa_l2cache.sav_count == 0) |
5231 | return; | |
5232 | ||
34dc7c2f BB |
5233 | VERIFY(nvlist_lookup_nvlist(config, |
5234 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
5235 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
5236 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
5237 | if (nl2cache != 0) { | |
5238 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
5239 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
5240 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
5241 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
5242 | ||
5243 | /* | |
5244 | * Update level 2 cache device stats. | |
5245 | */ | |
5246 | ||
5247 | for (i = 0; i < nl2cache; i++) { | |
5248 | VERIFY(nvlist_lookup_uint64(l2cache[i], | |
5249 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
5250 | ||
5251 | vd = NULL; | |
5252 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
5253 | if (guid == | |
5254 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
5255 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
5256 | break; | |
5257 | } | |
5258 | } | |
5259 | ASSERT(vd != NULL); | |
5260 | ||
5261 | VERIFY(nvlist_lookup_uint64_array(l2cache[i], | |
428870ff BB |
5262 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) |
5263 | == 0); | |
34dc7c2f | 5264 | vdev_get_stats(vd, vs); |
193a37cb TH |
5265 | vdev_config_generate_stats(vd, l2cache[i]); |
5266 | ||
34dc7c2f BB |
5267 | } |
5268 | } | |
34dc7c2f BB |
5269 | } |
5270 | ||
9ae529ec | 5271 | static void |
417104bd | 5272 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) |
9ae529ec | 5273 | { |
9ae529ec CS |
5274 | zap_cursor_t zc; |
5275 | zap_attribute_t za; | |
5276 | ||
9ae529ec CS |
5277 | if (spa->spa_feat_for_read_obj != 0) { |
5278 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5279 | spa->spa_feat_for_read_obj); | |
5280 | zap_cursor_retrieve(&zc, &za) == 0; | |
5281 | zap_cursor_advance(&zc)) { | |
5282 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5283 | za.za_num_integers == 1); | |
417104bd | 5284 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5285 | za.za_first_integer)); |
5286 | } | |
5287 | zap_cursor_fini(&zc); | |
5288 | } | |
5289 | ||
5290 | if (spa->spa_feat_for_write_obj != 0) { | |
5291 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5292 | spa->spa_feat_for_write_obj); | |
5293 | zap_cursor_retrieve(&zc, &za) == 0; | |
5294 | zap_cursor_advance(&zc)) { | |
5295 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5296 | za.za_num_integers == 1); | |
417104bd | 5297 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5298 | za.za_first_integer)); |
5299 | } | |
5300 | zap_cursor_fini(&zc); | |
5301 | } | |
417104bd NB |
5302 | } |
5303 | ||
5304 | static void | |
5305 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
5306 | { | |
5307 | int i; | |
5308 | ||
5309 | for (i = 0; i < SPA_FEATURES; i++) { | |
5310 | zfeature_info_t feature = spa_feature_table[i]; | |
5311 | uint64_t refcount; | |
5312 | ||
5313 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
5314 | continue; | |
5315 | ||
5316 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
5317 | } | |
5318 | } | |
5319 | ||
5320 | /* | |
5321 | * Store a list of pool features and their reference counts in the | |
5322 | * config. | |
5323 | * | |
5324 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
5325 | * the pool features and reference counts from disk, then save the list | |
5326 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
5327 | * and refresh its values from the cached reference counts. This | |
5328 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
5329 | * clear' can resume the pool. | |
5330 | */ | |
5331 | static void | |
5332 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
5333 | { | |
4eb30c68 | 5334 | nvlist_t *features; |
417104bd NB |
5335 | |
5336 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
5337 | ||
4eb30c68 NB |
5338 | mutex_enter(&spa->spa_feat_stats_lock); |
5339 | features = spa->spa_feat_stats; | |
5340 | ||
417104bd NB |
5341 | if (features != NULL) { |
5342 | spa_feature_stats_from_cache(spa, features); | |
5343 | } else { | |
5344 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
5345 | spa->spa_feat_stats = features; | |
5346 | spa_feature_stats_from_disk(spa, features); | |
5347 | } | |
9ae529ec | 5348 | |
417104bd NB |
5349 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, |
5350 | features)); | |
4eb30c68 NB |
5351 | |
5352 | mutex_exit(&spa->spa_feat_stats_lock); | |
9ae529ec CS |
5353 | } |
5354 | ||
34dc7c2f | 5355 | int |
9ae529ec CS |
5356 | spa_get_stats(const char *name, nvlist_t **config, |
5357 | char *altroot, size_t buflen) | |
34dc7c2f BB |
5358 | { |
5359 | int error; | |
5360 | spa_t *spa; | |
5361 | ||
5362 | *config = NULL; | |
428870ff | 5363 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 5364 | |
9babb374 BB |
5365 | if (spa != NULL) { |
5366 | /* | |
5367 | * This still leaves a window of inconsistency where the spares | |
5368 | * or l2cache devices could change and the config would be | |
5369 | * self-inconsistent. | |
5370 | */ | |
5371 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 5372 | |
9babb374 | 5373 | if (*config != NULL) { |
572e2857 BB |
5374 | uint64_t loadtimes[2]; |
5375 | ||
5376 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
5377 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
5378 | VERIFY(nvlist_add_uint64_array(*config, | |
5379 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); | |
5380 | ||
b128c09f | 5381 | VERIFY(nvlist_add_uint64(*config, |
9babb374 BB |
5382 | ZPOOL_CONFIG_ERRCOUNT, |
5383 | spa_get_errlog_size(spa)) == 0); | |
5384 | ||
cec3a0a1 | 5385 | if (spa_suspended(spa)) { |
9babb374 BB |
5386 | VERIFY(nvlist_add_uint64(*config, |
5387 | ZPOOL_CONFIG_SUSPENDED, | |
5388 | spa->spa_failmode) == 0); | |
cec3a0a1 OF |
5389 | VERIFY(nvlist_add_uint64(*config, |
5390 | ZPOOL_CONFIG_SUSPENDED_REASON, | |
5391 | spa->spa_suspended) == 0); | |
5392 | } | |
b128c09f | 5393 | |
9babb374 BB |
5394 | spa_add_spares(spa, *config); |
5395 | spa_add_l2cache(spa, *config); | |
9ae529ec | 5396 | spa_add_feature_stats(spa, *config); |
9babb374 | 5397 | } |
34dc7c2f BB |
5398 | } |
5399 | ||
5400 | /* | |
5401 | * We want to get the alternate root even for faulted pools, so we cheat | |
5402 | * and call spa_lookup() directly. | |
5403 | */ | |
5404 | if (altroot) { | |
5405 | if (spa == NULL) { | |
5406 | mutex_enter(&spa_namespace_lock); | |
5407 | spa = spa_lookup(name); | |
5408 | if (spa) | |
5409 | spa_altroot(spa, altroot, buflen); | |
5410 | else | |
5411 | altroot[0] = '\0'; | |
5412 | spa = NULL; | |
5413 | mutex_exit(&spa_namespace_lock); | |
5414 | } else { | |
5415 | spa_altroot(spa, altroot, buflen); | |
5416 | } | |
5417 | } | |
5418 | ||
9babb374 BB |
5419 | if (spa != NULL) { |
5420 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 5421 | spa_close(spa, FTAG); |
9babb374 | 5422 | } |
34dc7c2f BB |
5423 | |
5424 | return (error); | |
5425 | } | |
5426 | ||
5427 | /* | |
5428 | * Validate that the auxiliary device array is well formed. We must have an | |
5429 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
5430 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
5431 | * specified, as long as they are well-formed. | |
5432 | */ | |
5433 | static int | |
5434 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
5435 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
5436 | vdev_labeltype_t label) | |
5437 | { | |
5438 | nvlist_t **dev; | |
5439 | uint_t i, ndev; | |
5440 | vdev_t *vd; | |
5441 | int error; | |
5442 | ||
b128c09f BB |
5443 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5444 | ||
34dc7c2f BB |
5445 | /* |
5446 | * It's acceptable to have no devs specified. | |
5447 | */ | |
5448 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
5449 | return (0); | |
5450 | ||
5451 | if (ndev == 0) | |
2e528b49 | 5452 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
5453 | |
5454 | /* | |
5455 | * Make sure the pool is formatted with a version that supports this | |
5456 | * device type. | |
5457 | */ | |
5458 | if (spa_version(spa) < version) | |
2e528b49 | 5459 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
5460 | |
5461 | /* | |
5462 | * Set the pending device list so we correctly handle device in-use | |
5463 | * checking. | |
5464 | */ | |
5465 | sav->sav_pending = dev; | |
5466 | sav->sav_npending = ndev; | |
5467 | ||
5468 | for (i = 0; i < ndev; i++) { | |
5469 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
5470 | mode)) != 0) | |
5471 | goto out; | |
5472 | ||
5473 | if (!vd->vdev_ops->vdev_op_leaf) { | |
5474 | vdev_free(vd); | |
2e528b49 | 5475 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5476 | goto out; |
5477 | } | |
5478 | ||
34dc7c2f BB |
5479 | vd->vdev_top = vd; |
5480 | ||
5481 | if ((error = vdev_open(vd)) == 0 && | |
5482 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
5483 | VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, | |
5484 | vd->vdev_guid) == 0); | |
5485 | } | |
5486 | ||
5487 | vdev_free(vd); | |
5488 | ||
5489 | if (error && | |
5490 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
5491 | goto out; | |
5492 | else | |
5493 | error = 0; | |
5494 | } | |
5495 | ||
5496 | out: | |
5497 | sav->sav_pending = NULL; | |
5498 | sav->sav_npending = 0; | |
5499 | return (error); | |
5500 | } | |
5501 | ||
5502 | static int | |
5503 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
5504 | { | |
5505 | int error; | |
5506 | ||
b128c09f BB |
5507 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5508 | ||
34dc7c2f BB |
5509 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
5510 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
5511 | VDEV_LABEL_SPARE)) != 0) { | |
5512 | return (error); | |
5513 | } | |
5514 | ||
5515 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
5516 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
5517 | VDEV_LABEL_L2CACHE)); | |
5518 | } | |
5519 | ||
5520 | static void | |
5521 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
5522 | const char *config) | |
5523 | { | |
5524 | int i; | |
5525 | ||
5526 | if (sav->sav_config != NULL) { | |
5527 | nvlist_t **olddevs; | |
5528 | uint_t oldndevs; | |
5529 | nvlist_t **newdevs; | |
5530 | ||
5531 | /* | |
4e33ba4c | 5532 | * Generate new dev list by concatenating with the |
34dc7c2f BB |
5533 | * current dev list. |
5534 | */ | |
5535 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, | |
5536 | &olddevs, &oldndevs) == 0); | |
5537 | ||
5538 | newdevs = kmem_alloc(sizeof (void *) * | |
79c76d5b | 5539 | (ndevs + oldndevs), KM_SLEEP); |
34dc7c2f BB |
5540 | for (i = 0; i < oldndevs; i++) |
5541 | VERIFY(nvlist_dup(olddevs[i], &newdevs[i], | |
79c76d5b | 5542 | KM_SLEEP) == 0); |
34dc7c2f BB |
5543 | for (i = 0; i < ndevs; i++) |
5544 | VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], | |
79c76d5b | 5545 | KM_SLEEP) == 0); |
34dc7c2f BB |
5546 | |
5547 | VERIFY(nvlist_remove(sav->sav_config, config, | |
5548 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
5549 | ||
5550 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
5551 | config, newdevs, ndevs + oldndevs) == 0); | |
5552 | for (i = 0; i < oldndevs + ndevs; i++) | |
5553 | nvlist_free(newdevs[i]); | |
5554 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
5555 | } else { | |
5556 | /* | |
5557 | * Generate a new dev list. | |
5558 | */ | |
5559 | VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5560 | KM_SLEEP) == 0); |
34dc7c2f BB |
5561 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, |
5562 | devs, ndevs) == 0); | |
5563 | } | |
5564 | } | |
5565 | ||
5566 | /* | |
5567 | * Stop and drop level 2 ARC devices | |
5568 | */ | |
5569 | void | |
5570 | spa_l2cache_drop(spa_t *spa) | |
5571 | { | |
5572 | vdev_t *vd; | |
5573 | int i; | |
5574 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
5575 | ||
5576 | for (i = 0; i < sav->sav_count; i++) { | |
5577 | uint64_t pool; | |
5578 | ||
5579 | vd = sav->sav_vdevs[i]; | |
5580 | ASSERT(vd != NULL); | |
5581 | ||
fb5f0bc8 BB |
5582 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
5583 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 5584 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
5585 | } |
5586 | } | |
5587 | ||
b5256303 TC |
5588 | /* |
5589 | * Verify encryption parameters for spa creation. If we are encrypting, we must | |
5590 | * have the encryption feature flag enabled. | |
5591 | */ | |
5592 | static int | |
5593 | spa_create_check_encryption_params(dsl_crypto_params_t *dcp, | |
5594 | boolean_t has_encryption) | |
5595 | { | |
5596 | if (dcp->cp_crypt != ZIO_CRYPT_OFF && | |
5597 | dcp->cp_crypt != ZIO_CRYPT_INHERIT && | |
5598 | !has_encryption) | |
5599 | return (SET_ERROR(ENOTSUP)); | |
5600 | ||
1fff937a | 5601 | return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); |
b5256303 TC |
5602 | } |
5603 | ||
34dc7c2f BB |
5604 | /* |
5605 | * Pool Creation | |
5606 | */ | |
5607 | int | |
5608 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b5256303 | 5609 | nvlist_t *zplprops, dsl_crypto_params_t *dcp) |
34dc7c2f BB |
5610 | { |
5611 | spa_t *spa; | |
5612 | char *altroot = NULL; | |
5613 | vdev_t *rvd; | |
5614 | dsl_pool_t *dp; | |
5615 | dmu_tx_t *tx; | |
9babb374 | 5616 | int error = 0; |
34dc7c2f BB |
5617 | uint64_t txg = TXG_INITIAL; |
5618 | nvlist_t **spares, **l2cache; | |
5619 | uint_t nspares, nl2cache; | |
52ce99dd | 5620 | uint64_t version, obj; |
9ae529ec | 5621 | boolean_t has_features; |
b5256303 | 5622 | boolean_t has_encryption; |
715c996d | 5623 | boolean_t has_allocclass; |
b5256303 TC |
5624 | spa_feature_t feat; |
5625 | char *feat_name; | |
83e9986f RY |
5626 | char *poolname; |
5627 | nvlist_t *nvl; | |
5628 | ||
cc99f275 DB |
5629 | if (props == NULL || |
5630 | nvlist_lookup_string(props, "tname", &poolname) != 0) | |
83e9986f | 5631 | poolname = (char *)pool; |
34dc7c2f BB |
5632 | |
5633 | /* | |
5634 | * If this pool already exists, return failure. | |
5635 | */ | |
5636 | mutex_enter(&spa_namespace_lock); | |
83e9986f | 5637 | if (spa_lookup(poolname) != NULL) { |
34dc7c2f | 5638 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5639 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5640 | } |
5641 | ||
5642 | /* | |
5643 | * Allocate a new spa_t structure. | |
5644 | */ | |
83e9986f RY |
5645 | nvl = fnvlist_alloc(); |
5646 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
34dc7c2f BB |
5647 | (void) nvlist_lookup_string(props, |
5648 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
83e9986f RY |
5649 | spa = spa_add(poolname, nvl, altroot); |
5650 | fnvlist_free(nvl); | |
fb5f0bc8 | 5651 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5652 | |
34dc7c2f | 5653 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
5654 | spa_deactivate(spa); |
5655 | spa_remove(spa); | |
b128c09f | 5656 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
5657 | return (error); |
5658 | } | |
5659 | ||
83e9986f RY |
5660 | /* |
5661 | * Temporary pool names should never be written to disk. | |
5662 | */ | |
5663 | if (poolname != pool) | |
5664 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
5665 | ||
9ae529ec | 5666 | has_features = B_FALSE; |
b5256303 | 5667 | has_encryption = B_FALSE; |
715c996d | 5668 | has_allocclass = B_FALSE; |
1c27024e | 5669 | for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); |
9ae529ec | 5670 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { |
b5256303 | 5671 | if (zpool_prop_feature(nvpair_name(elem))) { |
9ae529ec | 5672 | has_features = B_TRUE; |
b5256303 TC |
5673 | |
5674 | feat_name = strchr(nvpair_name(elem), '@') + 1; | |
5675 | VERIFY0(zfeature_lookup_name(feat_name, &feat)); | |
5676 | if (feat == SPA_FEATURE_ENCRYPTION) | |
5677 | has_encryption = B_TRUE; | |
715c996d | 5678 | if (feat == SPA_FEATURE_ALLOCATION_CLASSES) |
5679 | has_allocclass = B_TRUE; | |
b5256303 TC |
5680 | } |
5681 | } | |
5682 | ||
5683 | /* verify encryption params, if they were provided */ | |
5684 | if (dcp != NULL) { | |
5685 | error = spa_create_check_encryption_params(dcp, has_encryption); | |
5686 | if (error != 0) { | |
5687 | spa_deactivate(spa); | |
5688 | spa_remove(spa); | |
5689 | mutex_exit(&spa_namespace_lock); | |
5690 | return (error); | |
5691 | } | |
9ae529ec | 5692 | } |
c24fa4b1 | 5693 | if (!has_allocclass && zfs_special_devs(nvroot, NULL)) { |
715c996d | 5694 | spa_deactivate(spa); |
5695 | spa_remove(spa); | |
5696 | mutex_exit(&spa_namespace_lock); | |
5697 | return (ENOTSUP); | |
5698 | } | |
9ae529ec CS |
5699 | |
5700 | if (has_features || nvlist_lookup_uint64(props, | |
5701 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 5702 | version = SPA_VERSION; |
9ae529ec CS |
5703 | } |
5704 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
5705 | |
5706 | spa->spa_first_txg = txg; | |
5707 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
5708 | spa->spa_uberblock.ub_version = version; |
5709 | spa->spa_ubsync = spa->spa_uberblock; | |
3dfb57a3 | 5710 | spa->spa_load_state = SPA_LOAD_CREATE; |
a1d477c2 MA |
5711 | spa->spa_removing_phys.sr_state = DSS_NONE; |
5712 | spa->spa_removing_phys.sr_removing_vdev = -1; | |
5713 | spa->spa_removing_phys.sr_prev_indirect_vdev = -1; | |
944a3724 | 5714 | spa->spa_indirect_vdevs_loaded = B_TRUE; |
34dc7c2f | 5715 | |
9babb374 BB |
5716 | /* |
5717 | * Create "The Godfather" zio to hold all async IOs | |
5718 | */ | |
e022864d MA |
5719 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
5720 | KM_SLEEP); | |
1c27024e | 5721 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
5722 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
5723 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
5724 | ZIO_FLAG_GODFATHER); | |
5725 | } | |
9babb374 | 5726 | |
34dc7c2f BB |
5727 | /* |
5728 | * Create the root vdev. | |
5729 | */ | |
b128c09f | 5730 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5731 | |
5732 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
5733 | ||
5734 | ASSERT(error != 0 || rvd != NULL); | |
5735 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
5736 | ||
5737 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
2e528b49 | 5738 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5739 | |
5740 | if (error == 0 && | |
5741 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
5742 | (error = spa_validate_aux(spa, nvroot, txg, | |
5743 | VDEV_ALLOC_ADD)) == 0) { | |
cc99f275 DB |
5744 | /* |
5745 | * instantiate the metaslab groups (this will dirty the vdevs) | |
5746 | * we can no longer error exit past this point | |
5747 | */ | |
5748 | for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { | |
5749 | vdev_t *vd = rvd->vdev_child[c]; | |
5750 | ||
6fe3498c | 5751 | vdev_ashift_optimize(vd); |
cc99f275 DB |
5752 | vdev_metaslab_set_size(vd); |
5753 | vdev_expand(vd, txg); | |
9babb374 | 5754 | } |
34dc7c2f BB |
5755 | } |
5756 | ||
b128c09f | 5757 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5758 | |
5759 | if (error != 0) { | |
5760 | spa_unload(spa); | |
5761 | spa_deactivate(spa); | |
5762 | spa_remove(spa); | |
5763 | mutex_exit(&spa_namespace_lock); | |
5764 | return (error); | |
5765 | } | |
5766 | ||
5767 | /* | |
5768 | * Get the list of spares, if specified. | |
5769 | */ | |
5770 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5771 | &spares, &nspares) == 0) { | |
5772 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5773 | KM_SLEEP) == 0); |
34dc7c2f BB |
5774 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
5775 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 5776 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5777 | spa_load_spares(spa); |
b128c09f | 5778 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5779 | spa->spa_spares.sav_sync = B_TRUE; |
5780 | } | |
5781 | ||
5782 | /* | |
5783 | * Get the list of level 2 cache devices, if specified. | |
5784 | */ | |
5785 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5786 | &l2cache, &nl2cache) == 0) { | |
5787 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 5788 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5789 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
5790 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 5791 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5792 | spa_load_l2cache(spa); |
b128c09f | 5793 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5794 | spa->spa_l2cache.sav_sync = B_TRUE; |
5795 | } | |
5796 | ||
9ae529ec | 5797 | spa->spa_is_initializing = B_TRUE; |
b5256303 | 5798 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); |
9ae529ec | 5799 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 5800 | |
428870ff BB |
5801 | /* |
5802 | * Create DDTs (dedup tables). | |
5803 | */ | |
5804 | ddt_create(spa); | |
5805 | ||
5806 | spa_update_dspace(spa); | |
5807 | ||
34dc7c2f BB |
5808 | tx = dmu_tx_create_assigned(dp, txg); |
5809 | ||
d5e024cb BB |
5810 | /* |
5811 | * Create the pool's history object. | |
5812 | */ | |
5813 | if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history) | |
5814 | spa_history_create_obj(spa, tx); | |
5815 | ||
5816 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); | |
5817 | spa_history_log_version(spa, "create", tx); | |
5818 | ||
34dc7c2f BB |
5819 | /* |
5820 | * Create the pool config object. | |
5821 | */ | |
5822 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 5823 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
5824 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
5825 | ||
5826 | if (zap_add(spa->spa_meta_objset, | |
5827 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
5828 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
5829 | cmn_err(CE_PANIC, "failed to add pool config"); | |
5830 | } | |
5831 | ||
428870ff BB |
5832 | if (zap_add(spa->spa_meta_objset, |
5833 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
5834 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
5835 | cmn_err(CE_PANIC, "failed to add pool version"); | |
5836 | } | |
5837 | ||
34dc7c2f BB |
5838 | /* Newly created pools with the right version are always deflated. */ |
5839 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
5840 | spa->spa_deflate = TRUE; | |
5841 | if (zap_add(spa->spa_meta_objset, | |
5842 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
5843 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
5844 | cmn_err(CE_PANIC, "failed to add deflate"); | |
5845 | } | |
5846 | } | |
5847 | ||
5848 | /* | |
428870ff | 5849 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
5850 | * because sync-to-convergence takes longer if the blocksize |
5851 | * keeps changing. | |
5852 | */ | |
428870ff BB |
5853 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
5854 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 5855 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 5856 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
5857 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
5858 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
5859 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 5860 | } |
428870ff BB |
5861 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
5862 | spa->spa_meta_objset, obj)); | |
34dc7c2f | 5863 | |
3c67d83a TH |
5864 | /* |
5865 | * Generate some random noise for salted checksums to operate on. | |
5866 | */ | |
5867 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
5868 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
5869 | ||
34dc7c2f BB |
5870 | /* |
5871 | * Set pool properties. | |
5872 | */ | |
5873 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
5874 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
5875 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 5876 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
379ca9cf | 5877 | spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); |
1b939560 | 5878 | spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM); |
428870ff | 5879 | |
d164b209 BB |
5880 | if (props != NULL) { |
5881 | spa_configfile_set(spa, props, B_FALSE); | |
13fe0198 | 5882 | spa_sync_props(props, tx); |
d164b209 | 5883 | } |
34dc7c2f BB |
5884 | |
5885 | dmu_tx_commit(tx); | |
5886 | ||
5887 | spa->spa_sync_on = B_TRUE; | |
b5256303 | 5888 | txg_sync_start(dp); |
379ca9cf | 5889 | mmp_thread_start(spa); |
b5256303 | 5890 | txg_wait_synced(dp, txg); |
34dc7c2f | 5891 | |
9d5b5245 SD |
5892 | spa_spawn_aux_threads(spa); |
5893 | ||
a1d477c2 | 5894 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
34dc7c2f | 5895 | |
0c66c32d JG |
5896 | /* |
5897 | * Don't count references from objsets that are already closed | |
5898 | * and are making their way through the eviction process. | |
5899 | */ | |
5900 | spa_evicting_os_wait(spa); | |
424fd7c3 | 5901 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
3dfb57a3 | 5902 | spa->spa_load_state = SPA_LOAD_NONE; |
b128c09f | 5903 | |
d164b209 BB |
5904 | mutex_exit(&spa_namespace_lock); |
5905 | ||
34dc7c2f BB |
5906 | return (0); |
5907 | } | |
5908 | ||
9babb374 BB |
5909 | /* |
5910 | * Import a non-root pool into the system. | |
5911 | */ | |
5912 | int | |
13fe0198 | 5913 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
5914 | { |
5915 | spa_t *spa; | |
5916 | char *altroot = NULL; | |
428870ff | 5917 | spa_load_state_t state = SPA_LOAD_IMPORT; |
8a393be3 | 5918 | zpool_load_policy_t policy; |
da92d5cb | 5919 | spa_mode_t mode = spa_mode_global; |
572e2857 | 5920 | uint64_t readonly = B_FALSE; |
9babb374 | 5921 | int error; |
34dc7c2f BB |
5922 | nvlist_t *nvroot; |
5923 | nvlist_t **spares, **l2cache; | |
5924 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
5925 | |
5926 | /* | |
5927 | * If a pool with this name exists, return failure. | |
5928 | */ | |
5929 | mutex_enter(&spa_namespace_lock); | |
428870ff | 5930 | if (spa_lookup(pool) != NULL) { |
9babb374 | 5931 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5932 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5933 | } |
5934 | ||
5935 | /* | |
5936 | * Create and initialize the spa structure. | |
5937 | */ | |
5938 | (void) nvlist_lookup_string(props, | |
5939 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
5940 | (void) nvlist_lookup_uint64(props, |
5941 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
5942 | if (readonly) | |
da92d5cb | 5943 | mode = SPA_MODE_READ; |
428870ff | 5944 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
5945 | spa->spa_import_flags = flags; |
5946 | ||
5947 | /* | |
5948 | * Verbatim import - Take a pool and insert it into the namespace | |
5949 | * as if it had been loaded at boot. | |
5950 | */ | |
5951 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
5952 | if (props != NULL) | |
5953 | spa_configfile_set(spa, props, B_FALSE); | |
5954 | ||
a1d477c2 | 5955 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 5956 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
4a0ee12a | 5957 | zfs_dbgmsg("spa_import: verbatim import of %s", pool); |
572e2857 | 5958 | mutex_exit(&spa_namespace_lock); |
572e2857 BB |
5959 | return (0); |
5960 | } | |
5961 | ||
5962 | spa_activate(spa, mode); | |
34dc7c2f | 5963 | |
9babb374 BB |
5964 | /* |
5965 | * Don't start async tasks until we know everything is healthy. | |
5966 | */ | |
5967 | spa_async_suspend(spa); | |
b128c09f | 5968 | |
8a393be3 PZ |
5969 | zpool_get_load_policy(config, &policy); |
5970 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) | |
572e2857 BB |
5971 | state = SPA_LOAD_RECOVER; |
5972 | ||
6cb8e530 | 5973 | spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; |
572e2857 | 5974 | |
6cb8e530 PZ |
5975 | if (state != SPA_LOAD_RECOVER) { |
5976 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
5977 | zfs_dbgmsg("spa_import: importing %s", pool); | |
5978 | } else { | |
5979 | zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " | |
8a393be3 | 5980 | "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); |
6cb8e530 | 5981 | } |
8a393be3 | 5982 | error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); |
428870ff BB |
5983 | |
5984 | /* | |
572e2857 BB |
5985 | * Propagate anything learned while loading the pool and pass it |
5986 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 5987 | */ |
572e2857 BB |
5988 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
5989 | spa->spa_load_info) == 0); | |
34dc7c2f | 5990 | |
b128c09f | 5991 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5992 | /* |
9babb374 BB |
5993 | * Toss any existing sparelist, as it doesn't have any validity |
5994 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 5995 | */ |
9babb374 | 5996 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
5997 | nvlist_free(spa->spa_spares.sav_config); |
5998 | spa->spa_spares.sav_config = NULL; | |
5999 | spa_load_spares(spa); | |
6000 | } | |
9babb374 | 6001 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
6002 | nvlist_free(spa->spa_l2cache.sav_config); |
6003 | spa->spa_l2cache.sav_config = NULL; | |
6004 | spa_load_l2cache(spa); | |
6005 | } | |
6006 | ||
6007 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
6008 | &nvroot) == 0); | |
b128c09f | 6009 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 6010 | |
d164b209 BB |
6011 | if (props != NULL) |
6012 | spa_configfile_set(spa, props, B_FALSE); | |
6013 | ||
fb5f0bc8 BB |
6014 | if (error != 0 || (props && spa_writeable(spa) && |
6015 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
6016 | spa_unload(spa); |
6017 | spa_deactivate(spa); | |
6018 | spa_remove(spa); | |
34dc7c2f BB |
6019 | mutex_exit(&spa_namespace_lock); |
6020 | return (error); | |
6021 | } | |
6022 | ||
572e2857 BB |
6023 | spa_async_resume(spa); |
6024 | ||
34dc7c2f BB |
6025 | /* |
6026 | * Override any spares and level 2 cache devices as specified by | |
6027 | * the user, as these may have correct device names/devids, etc. | |
6028 | */ | |
6029 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
6030 | &spares, &nspares) == 0) { | |
6031 | if (spa->spa_spares.sav_config) | |
6032 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, | |
6033 | ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); | |
6034 | else | |
6035 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, | |
79c76d5b | 6036 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
6037 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
6038 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 6039 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6040 | spa_load_spares(spa); |
b128c09f | 6041 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6042 | spa->spa_spares.sav_sync = B_TRUE; |
6043 | } | |
6044 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
6045 | &l2cache, &nl2cache) == 0) { | |
6046 | if (spa->spa_l2cache.sav_config) | |
6047 | VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, | |
6048 | ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); | |
6049 | else | |
6050 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 6051 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
6052 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
6053 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 6054 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6055 | spa_load_l2cache(spa); |
b128c09f | 6056 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6057 | spa->spa_l2cache.sav_sync = B_TRUE; |
6058 | } | |
6059 | ||
428870ff BB |
6060 | /* |
6061 | * Check for any removed devices. | |
6062 | */ | |
6063 | if (spa->spa_autoreplace) { | |
6064 | spa_aux_check_removed(&spa->spa_spares); | |
6065 | spa_aux_check_removed(&spa->spa_l2cache); | |
6066 | } | |
6067 | ||
fb5f0bc8 | 6068 | if (spa_writeable(spa)) { |
b128c09f BB |
6069 | /* |
6070 | * Update the config cache to include the newly-imported pool. | |
6071 | */ | |
45d1cae3 | 6072 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 6073 | } |
34dc7c2f | 6074 | |
34dc7c2f | 6075 | /* |
9babb374 BB |
6076 | * It's possible that the pool was expanded while it was exported. |
6077 | * We kick off an async task to handle this for us. | |
34dc7c2f | 6078 | */ |
9babb374 | 6079 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 6080 | |
d5e024cb | 6081 | spa_history_log_version(spa, "import", NULL); |
fb390aaf | 6082 | |
12fa0466 | 6083 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
fb390aaf | 6084 | |
fb390aaf HR |
6085 | mutex_exit(&spa_namespace_lock); |
6086 | ||
ec213971 | 6087 | zvol_create_minors_recursive(pool); |
4a22ba5b | 6088 | |
b128c09f BB |
6089 | return (0); |
6090 | } | |
6091 | ||
34dc7c2f BB |
6092 | nvlist_t * |
6093 | spa_tryimport(nvlist_t *tryconfig) | |
6094 | { | |
6095 | nvlist_t *config = NULL; | |
6cb8e530 | 6096 | char *poolname, *cachefile; |
34dc7c2f BB |
6097 | spa_t *spa; |
6098 | uint64_t state; | |
d164b209 | 6099 | int error; |
8a393be3 | 6100 | zpool_load_policy_t policy; |
34dc7c2f BB |
6101 | |
6102 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
6103 | return (NULL); | |
6104 | ||
6105 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
6106 | return (NULL); | |
6107 | ||
6108 | /* | |
6109 | * Create and initialize the spa structure. | |
6110 | */ | |
6111 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6112 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
da92d5cb | 6113 | spa_activate(spa, SPA_MODE_READ); |
34dc7c2f BB |
6114 | |
6115 | /* | |
8a393be3 | 6116 | * Rewind pool if a max txg was provided. |
34dc7c2f | 6117 | */ |
8a393be3 PZ |
6118 | zpool_get_load_policy(spa->spa_config, &policy); |
6119 | if (policy.zlp_txg != UINT64_MAX) { | |
6120 | spa->spa_load_max_txg = policy.zlp_txg; | |
6cb8e530 PZ |
6121 | spa->spa_extreme_rewind = B_TRUE; |
6122 | zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", | |
8a393be3 | 6123 | poolname, (longlong_t)policy.zlp_txg); |
6cb8e530 PZ |
6124 | } else { |
6125 | zfs_dbgmsg("spa_tryimport: importing %s", poolname); | |
6126 | } | |
6127 | ||
6128 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) | |
6129 | == 0) { | |
6130 | zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); | |
6131 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; | |
6132 | } else { | |
6133 | spa->spa_config_source = SPA_CONFIG_SRC_SCAN; | |
6134 | } | |
6135 | ||
6136 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); | |
34dc7c2f BB |
6137 | |
6138 | /* | |
6139 | * If 'tryconfig' was at least parsable, return the current config. | |
6140 | */ | |
6141 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 6142 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f BB |
6143 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, |
6144 | poolname) == 0); | |
6145 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
6146 | state) == 0); | |
6147 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
6148 | spa->spa_uberblock.ub_timestamp) == 0); | |
9ae529ec CS |
6149 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
6150 | spa->spa_load_info) == 0); | |
ffe9d382 BB |
6151 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, |
6152 | spa->spa_errata) == 0); | |
34dc7c2f BB |
6153 | |
6154 | /* | |
6155 | * If the bootfs property exists on this pool then we | |
6156 | * copy it out so that external consumers can tell which | |
6157 | * pools are bootable. | |
6158 | */ | |
d164b209 | 6159 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
79c76d5b | 6160 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6161 | |
6162 | /* | |
6163 | * We have to play games with the name since the | |
6164 | * pool was opened as TRYIMPORT_NAME. | |
6165 | */ | |
b128c09f | 6166 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
6167 | spa->spa_bootfs, tmpname) == 0) { |
6168 | char *cp; | |
d1d7e268 MK |
6169 | char *dsname; |
6170 | ||
79c76d5b | 6171 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6172 | |
6173 | cp = strchr(tmpname, '/'); | |
6174 | if (cp == NULL) { | |
6175 | (void) strlcpy(dsname, tmpname, | |
6176 | MAXPATHLEN); | |
6177 | } else { | |
6178 | (void) snprintf(dsname, MAXPATHLEN, | |
6179 | "%s/%s", poolname, ++cp); | |
6180 | } | |
6181 | VERIFY(nvlist_add_string(config, | |
6182 | ZPOOL_CONFIG_BOOTFS, dsname) == 0); | |
6183 | kmem_free(dsname, MAXPATHLEN); | |
6184 | } | |
6185 | kmem_free(tmpname, MAXPATHLEN); | |
6186 | } | |
6187 | ||
6188 | /* | |
6189 | * Add the list of hot spares and level 2 cache devices. | |
6190 | */ | |
9babb374 | 6191 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
6192 | spa_add_spares(spa, config); |
6193 | spa_add_l2cache(spa, config); | |
9babb374 | 6194 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
6195 | } |
6196 | ||
6197 | spa_unload(spa); | |
6198 | spa_deactivate(spa); | |
6199 | spa_remove(spa); | |
6200 | mutex_exit(&spa_namespace_lock); | |
6201 | ||
6202 | return (config); | |
6203 | } | |
6204 | ||
6205 | /* | |
6206 | * Pool export/destroy | |
6207 | * | |
6208 | * The act of destroying or exporting a pool is very simple. We make sure there | |
6209 | * is no more pending I/O and any references to the pool are gone. Then, we | |
6210 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
6211 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
6212 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
6213 | */ |
6214 | static int | |
b128c09f | 6215 | spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 6216 | boolean_t force, boolean_t hardforce) |
34dc7c2f BB |
6217 | { |
6218 | spa_t *spa; | |
6219 | ||
6220 | if (oldconfig) | |
6221 | *oldconfig = NULL; | |
6222 | ||
da92d5cb | 6223 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
2e528b49 | 6224 | return (SET_ERROR(EROFS)); |
34dc7c2f BB |
6225 | |
6226 | mutex_enter(&spa_namespace_lock); | |
6227 | if ((spa = spa_lookup(pool)) == NULL) { | |
6228 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 6229 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
6230 | } |
6231 | ||
43a85362 SD |
6232 | if (spa->spa_is_exporting) { |
6233 | /* the pool is being exported by another thread */ | |
6234 | mutex_exit(&spa_namespace_lock); | |
6235 | return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS)); | |
6236 | } | |
6237 | spa->spa_is_exporting = B_TRUE; | |
6238 | ||
34dc7c2f BB |
6239 | /* |
6240 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
6241 | * reacquire the namespace lock, and see if we can export. | |
6242 | */ | |
6243 | spa_open_ref(spa, FTAG); | |
6244 | mutex_exit(&spa_namespace_lock); | |
6245 | spa_async_suspend(spa); | |
a0bd735a BP |
6246 | if (spa->spa_zvol_taskq) { |
6247 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
6248 | taskq_wait(spa->spa_zvol_taskq); | |
6249 | } | |
34dc7c2f BB |
6250 | mutex_enter(&spa_namespace_lock); |
6251 | spa_close(spa, FTAG); | |
6252 | ||
d14cfd83 IH |
6253 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) |
6254 | goto export_spa; | |
34dc7c2f | 6255 | /* |
d14cfd83 IH |
6256 | * The pool will be in core if it's openable, in which case we can |
6257 | * modify its state. Objsets may be open only because they're dirty, | |
6258 | * so we have to force it to sync before checking spa_refcnt. | |
34dc7c2f | 6259 | */ |
0c66c32d | 6260 | if (spa->spa_sync_on) { |
34dc7c2f | 6261 | txg_wait_synced(spa->spa_dsl_pool, 0); |
0c66c32d JG |
6262 | spa_evicting_os_wait(spa); |
6263 | } | |
34dc7c2f | 6264 | |
d14cfd83 IH |
6265 | /* |
6266 | * A pool cannot be exported or destroyed if there are active | |
6267 | * references. If we are resetting a pool, allow references by | |
6268 | * fault injection handlers. | |
6269 | */ | |
6270 | if (!spa_refcount_zero(spa) || | |
6271 | (spa->spa_inject_ref != 0 && | |
6272 | new_state != POOL_STATE_UNINITIALIZED)) { | |
6273 | spa_async_resume(spa); | |
43a85362 | 6274 | spa->spa_is_exporting = B_FALSE; |
d14cfd83 IH |
6275 | mutex_exit(&spa_namespace_lock); |
6276 | return (SET_ERROR(EBUSY)); | |
6277 | } | |
34dc7c2f | 6278 | |
d14cfd83 | 6279 | if (spa->spa_sync_on) { |
b128c09f BB |
6280 | /* |
6281 | * A pool cannot be exported if it has an active shared spare. | |
6282 | * This is to prevent other pools stealing the active spare | |
6283 | * from an exported pool. At user's own will, such pool can | |
6284 | * be forcedly exported. | |
6285 | */ | |
6286 | if (!force && new_state == POOL_STATE_EXPORTED && | |
6287 | spa_has_active_shared_spare(spa)) { | |
6288 | spa_async_resume(spa); | |
43a85362 | 6289 | spa->spa_is_exporting = B_FALSE; |
b128c09f | 6290 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 6291 | return (SET_ERROR(EXDEV)); |
b128c09f | 6292 | } |
34dc7c2f | 6293 | |
619f0976 GW |
6294 | /* |
6295 | * We're about to export or destroy this pool. Make sure | |
1b939560 BB |
6296 | * we stop all initialization and trim activity here before |
6297 | * we set the spa_final_txg. This will ensure that all | |
619f0976 GW |
6298 | * dirty data resulting from the initialization is |
6299 | * committed to disk before we unload the pool. | |
6300 | */ | |
6301 | if (spa->spa_root_vdev != NULL) { | |
1b939560 BB |
6302 | vdev_t *rvd = spa->spa_root_vdev; |
6303 | vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE); | |
6304 | vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE); | |
6305 | vdev_autotrim_stop_all(spa); | |
9a49d3f3 | 6306 | vdev_rebuild_stop_all(spa); |
619f0976 GW |
6307 | } |
6308 | ||
34dc7c2f BB |
6309 | /* |
6310 | * We want this to be reflected on every label, | |
6311 | * so mark them all dirty. spa_unload() will do the | |
6312 | * final sync that pushes these changes out. | |
6313 | */ | |
fb5f0bc8 | 6314 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 6315 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6316 | spa->spa_state = new_state; |
428870ff BB |
6317 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
6318 | TXG_DEFER_SIZE + 1; | |
34dc7c2f | 6319 | vdev_config_dirty(spa->spa_root_vdev); |
b128c09f | 6320 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6321 | } |
6322 | } | |
6323 | ||
d14cfd83 | 6324 | export_spa: |
d5e024cb BB |
6325 | if (new_state == POOL_STATE_DESTROYED) |
6326 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); | |
6327 | else if (new_state == POOL_STATE_EXPORTED) | |
6328 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT); | |
34dc7c2f BB |
6329 | |
6330 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
6331 | spa_unload(spa); | |
6332 | spa_deactivate(spa); | |
6333 | } | |
6334 | ||
6335 | if (oldconfig && spa->spa_config) | |
6336 | VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); | |
6337 | ||
6338 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 | 6339 | if (!hardforce) |
a1d477c2 | 6340 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 6341 | spa_remove(spa); |
43a85362 SD |
6342 | } else { |
6343 | /* | |
6344 | * If spa_remove() is not called for this spa_t and | |
6345 | * there is any possibility that it can be reused, | |
6346 | * we make sure to reset the exporting flag. | |
6347 | */ | |
6348 | spa->spa_is_exporting = B_FALSE; | |
34dc7c2f | 6349 | } |
34dc7c2f | 6350 | |
43a85362 | 6351 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
6352 | return (0); |
6353 | } | |
6354 | ||
6355 | /* | |
6356 | * Destroy a storage pool. | |
6357 | */ | |
6358 | int | |
6359 | spa_destroy(char *pool) | |
6360 | { | |
fb5f0bc8 BB |
6361 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
6362 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
6363 | } |
6364 | ||
6365 | /* | |
6366 | * Export a storage pool. | |
6367 | */ | |
6368 | int | |
fb5f0bc8 BB |
6369 | spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, |
6370 | boolean_t hardforce) | |
34dc7c2f | 6371 | { |
fb5f0bc8 BB |
6372 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
6373 | force, hardforce)); | |
34dc7c2f BB |
6374 | } |
6375 | ||
6376 | /* | |
6377 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
6378 | * from the namespace in any way. | |
6379 | */ | |
6380 | int | |
6381 | spa_reset(char *pool) | |
6382 | { | |
b128c09f | 6383 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 6384 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
6385 | } |
6386 | ||
34dc7c2f BB |
6387 | /* |
6388 | * ========================================================================== | |
6389 | * Device manipulation | |
6390 | * ========================================================================== | |
6391 | */ | |
6392 | ||
6393 | /* | |
6394 | * Add a device to a storage pool. | |
6395 | */ | |
6396 | int | |
6397 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
6398 | { | |
93e28d66 | 6399 | uint64_t txg; |
fb5f0bc8 | 6400 | int error; |
34dc7c2f BB |
6401 | vdev_t *rvd = spa->spa_root_vdev; |
6402 | vdev_t *vd, *tvd; | |
6403 | nvlist_t **spares, **l2cache; | |
6404 | uint_t nspares, nl2cache; | |
6405 | ||
572e2857 BB |
6406 | ASSERT(spa_writeable(spa)); |
6407 | ||
34dc7c2f BB |
6408 | txg = spa_vdev_enter(spa); |
6409 | ||
6410 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
6411 | VDEV_ALLOC_ADD)) != 0) | |
6412 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6413 | ||
b128c09f | 6414 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
6415 | |
6416 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
6417 | &nspares) != 0) | |
6418 | nspares = 0; | |
6419 | ||
6420 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
6421 | &nl2cache) != 0) | |
6422 | nl2cache = 0; | |
6423 | ||
b128c09f | 6424 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 6425 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 6426 | |
b128c09f BB |
6427 | if (vd->vdev_children != 0 && |
6428 | (error = vdev_create(vd, txg, B_FALSE)) != 0) | |
6429 | return (spa_vdev_exit(spa, vd, txg, error)); | |
34dc7c2f BB |
6430 | |
6431 | /* | |
6432 | * We must validate the spares and l2cache devices after checking the | |
6433 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
6434 | */ | |
b128c09f | 6435 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 6436 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
6437 | |
6438 | /* | |
a1d477c2 MA |
6439 | * If we are in the middle of a device removal, we can only add |
6440 | * devices which match the existing devices in the pool. | |
6441 | * If we are in the middle of a removal, or have some indirect | |
6442 | * vdevs, we can not add raidz toplevels. | |
34dc7c2f | 6443 | */ |
a1d477c2 MA |
6444 | if (spa->spa_vdev_removal != NULL || |
6445 | spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { | |
6446 | for (int c = 0; c < vd->vdev_children; c++) { | |
6447 | tvd = vd->vdev_child[c]; | |
6448 | if (spa->spa_vdev_removal != NULL && | |
9e052db4 | 6449 | tvd->vdev_ashift != spa->spa_max_ashift) { |
a1d477c2 MA |
6450 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
6451 | } | |
6452 | /* Fail if top level vdev is raidz */ | |
6453 | if (tvd->vdev_ops == &vdev_raidz_ops) { | |
6454 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); | |
6455 | } | |
6456 | /* | |
6457 | * Need the top level mirror to be | |
6458 | * a mirror of leaf vdevs only | |
6459 | */ | |
6460 | if (tvd->vdev_ops == &vdev_mirror_ops) { | |
6461 | for (uint64_t cid = 0; | |
6462 | cid < tvd->vdev_children; cid++) { | |
6463 | vdev_t *cvd = tvd->vdev_child[cid]; | |
6464 | if (!cvd->vdev_ops->vdev_op_leaf) { | |
6465 | return (spa_vdev_exit(spa, vd, | |
6466 | txg, EINVAL)); | |
6467 | } | |
6468 | } | |
6469 | } | |
6470 | } | |
6471 | } | |
6472 | ||
1c27024e | 6473 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
6474 | tvd = vd->vdev_child[c]; |
6475 | vdev_remove_child(vd, tvd); | |
93e28d66 | 6476 | tvd->vdev_id = rvd->vdev_children; |
34dc7c2f BB |
6477 | vdev_add_child(rvd, tvd); |
6478 | vdev_config_dirty(tvd); | |
6479 | } | |
6480 | ||
6481 | if (nspares != 0) { | |
6482 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
6483 | ZPOOL_CONFIG_SPARES); | |
6484 | spa_load_spares(spa); | |
6485 | spa->spa_spares.sav_sync = B_TRUE; | |
6486 | } | |
6487 | ||
6488 | if (nl2cache != 0) { | |
6489 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
6490 | ZPOOL_CONFIG_L2CACHE); | |
6491 | spa_load_l2cache(spa); | |
6492 | spa->spa_l2cache.sav_sync = B_TRUE; | |
6493 | } | |
6494 | ||
6495 | /* | |
6496 | * We have to be careful when adding new vdevs to an existing pool. | |
6497 | * If other threads start allocating from these vdevs before we | |
6498 | * sync the config cache, and we lose power, then upon reboot we may | |
6499 | * fail to open the pool because there are DVAs that the config cache | |
6500 | * can't translate. Therefore, we first add the vdevs without | |
6501 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
6502 | * and then let spa_config_update() initialize the new metaslabs. | |
6503 | * | |
6504 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
6505 | * if we lose power at any point in this sequence, the remaining | |
6506 | * steps will be completed the next time we load the pool. | |
6507 | */ | |
6508 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
6509 | ||
6510 | mutex_enter(&spa_namespace_lock); | |
6511 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
12fa0466 | 6512 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); |
34dc7c2f BB |
6513 | mutex_exit(&spa_namespace_lock); |
6514 | ||
6515 | return (0); | |
6516 | } | |
6517 | ||
6518 | /* | |
6519 | * Attach a device to a mirror. The arguments are the path to any device | |
6520 | * in the mirror, and the nvroot for the new device. If the path specifies | |
6521 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
6522 | * | |
6523 | * If 'replacing' is specified, the new device is intended to replace the | |
6524 | * existing device; in this case the two devices are made into their own | |
6525 | * mirror using the 'replacing' vdev, which is functionally identical to | |
6526 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
6527 | * extra rules: you can't attach to it after it's been created, and upon | |
6528 | * completion of resilvering, the first disk (the one being replaced) | |
6529 | * is automatically detached. | |
9a49d3f3 BB |
6530 | * |
6531 | * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild) | |
6532 | * should be performed instead of traditional healing reconstruction. From | |
6533 | * an administrators perspective these are both resilver operations. | |
34dc7c2f BB |
6534 | */ |
6535 | int | |
9a49d3f3 BB |
6536 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing, |
6537 | int rebuild) | |
34dc7c2f | 6538 | { |
428870ff | 6539 | uint64_t txg, dtl_max_txg; |
9a49d3f3 | 6540 | vdev_t *rvd = spa->spa_root_vdev; |
34dc7c2f BB |
6541 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
6542 | vdev_ops_t *pvops; | |
b128c09f BB |
6543 | char *oldvdpath, *newvdpath; |
6544 | int newvd_isspare; | |
6545 | int error; | |
34dc7c2f | 6546 | |
572e2857 BB |
6547 | ASSERT(spa_writeable(spa)); |
6548 | ||
34dc7c2f BB |
6549 | txg = spa_vdev_enter(spa); |
6550 | ||
b128c09f | 6551 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6552 | |
d2734cce SD |
6553 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6554 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6555 | error = (spa_has_checkpoint(spa)) ? | |
6556 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6557 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6558 | } | |
6559 | ||
9a49d3f3 BB |
6560 | if (rebuild) { |
6561 | if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD)) | |
6562 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6563 | ||
6564 | if (dsl_scan_resilvering(spa_get_dsl(spa))) | |
6565 | return (spa_vdev_exit(spa, NULL, txg, | |
6566 | ZFS_ERR_RESILVER_IN_PROGRESS)); | |
6567 | } else { | |
6568 | if (vdev_rebuild_active(rvd)) | |
6569 | return (spa_vdev_exit(spa, NULL, txg, | |
6570 | ZFS_ERR_REBUILD_IN_PROGRESS)); | |
6571 | } | |
6572 | ||
9e052db4 | 6573 | if (spa->spa_vdev_removal != NULL) |
a1d477c2 | 6574 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
a1d477c2 | 6575 | |
34dc7c2f BB |
6576 | if (oldvd == NULL) |
6577 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6578 | ||
6579 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
6580 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6581 | ||
6582 | pvd = oldvd->vdev_parent; | |
6583 | ||
6584 | if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, | |
5ffb9d1d | 6585 | VDEV_ALLOC_ATTACH)) != 0) |
34dc7c2f BB |
6586 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
6587 | ||
6588 | if (newrootvd->vdev_children != 1) | |
6589 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6590 | ||
6591 | newvd = newrootvd->vdev_child[0]; | |
6592 | ||
6593 | if (!newvd->vdev_ops->vdev_op_leaf) | |
6594 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6595 | ||
6596 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
6597 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
6598 | ||
6599 | /* | |
6600 | * Spares can't replace logs | |
6601 | */ | |
b128c09f | 6602 | if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) |
34dc7c2f BB |
6603 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
6604 | ||
9a49d3f3 BB |
6605 | if (rebuild) { |
6606 | /* | |
6607 | * For rebuilds, the parent vdev must support reconstruction | |
6608 | * using only space maps. This means the only allowable | |
6609 | * parents are the root vdev or a mirror vdev. | |
6610 | */ | |
6611 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6612 | pvd->vdev_ops != &vdev_root_ops) { | |
6613 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6614 | } | |
6615 | } | |
6616 | ||
34dc7c2f BB |
6617 | if (!replacing) { |
6618 | /* | |
6619 | * For attach, the only allowable parent is a mirror or the root | |
6620 | * vdev. | |
6621 | */ | |
6622 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6623 | pvd->vdev_ops != &vdev_root_ops) | |
6624 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6625 | ||
6626 | pvops = &vdev_mirror_ops; | |
6627 | } else { | |
6628 | /* | |
6629 | * Active hot spares can only be replaced by inactive hot | |
6630 | * spares. | |
6631 | */ | |
6632 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 6633 | oldvd->vdev_isspare && |
34dc7c2f BB |
6634 | !spa_has_spare(spa, newvd->vdev_guid)) |
6635 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6636 | ||
6637 | /* | |
6638 | * If the source is a hot spare, and the parent isn't already a | |
6639 | * spare, then we want to create a new hot spare. Otherwise, we | |
6640 | * want to create a replacing vdev. The user is not allowed to | |
6641 | * attach to a spared vdev child unless the 'isspare' state is | |
6642 | * the same (spare replaces spare, non-spare replaces | |
6643 | * non-spare). | |
6644 | */ | |
572e2857 BB |
6645 | if (pvd->vdev_ops == &vdev_replacing_ops && |
6646 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 6647 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6648 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
6649 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 6650 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6651 | } |
6652 | ||
6653 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
6654 | pvops = &vdev_spare_ops; |
6655 | else | |
6656 | pvops = &vdev_replacing_ops; | |
6657 | } | |
6658 | ||
6659 | /* | |
9babb374 | 6660 | * Make sure the new device is big enough. |
34dc7c2f | 6661 | */ |
9babb374 | 6662 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
6663 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
6664 | ||
6665 | /* | |
6666 | * The new device cannot have a higher alignment requirement | |
6667 | * than the top-level vdev. | |
6668 | */ | |
6669 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
9a49d3f3 | 6670 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
34dc7c2f BB |
6671 | |
6672 | /* | |
6673 | * If this is an in-place replacement, update oldvd's path and devid | |
6674 | * to make it distinguishable from newvd, and unopenable from now on. | |
6675 | */ | |
6676 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
6677 | spa_strfree(oldvd->vdev_path); | |
6678 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
79c76d5b | 6679 | KM_SLEEP); |
c9e319fa JL |
6680 | (void) snprintf(oldvd->vdev_path, strlen(newvd->vdev_path) + 5, |
6681 | "%s/%s", newvd->vdev_path, "old"); | |
34dc7c2f BB |
6682 | if (oldvd->vdev_devid != NULL) { |
6683 | spa_strfree(oldvd->vdev_devid); | |
6684 | oldvd->vdev_devid = NULL; | |
6685 | } | |
6686 | } | |
6687 | ||
6688 | /* | |
6689 | * If the parent is not a mirror, or if we're replacing, insert the new | |
6690 | * mirror/replacing/spare vdev above oldvd. | |
6691 | */ | |
6692 | if (pvd->vdev_ops != pvops) | |
6693 | pvd = vdev_add_parent(oldvd, pvops); | |
6694 | ||
6695 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
6696 | ASSERT(pvd->vdev_ops == pvops); | |
6697 | ASSERT(oldvd->vdev_parent == pvd); | |
6698 | ||
6699 | /* | |
6700 | * Extract the new device from its root and add it to pvd. | |
6701 | */ | |
6702 | vdev_remove_child(newrootvd, newvd); | |
6703 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 6704 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
6705 | vdev_add_child(pvd, newvd); |
6706 | ||
6d82f98c IH |
6707 | /* |
6708 | * Reevaluate the parent vdev state. | |
6709 | */ | |
6710 | vdev_propagate_state(pvd); | |
6711 | ||
34dc7c2f BB |
6712 | tvd = newvd->vdev_top; |
6713 | ASSERT(pvd->vdev_top == tvd); | |
6714 | ASSERT(tvd->vdev_parent == rvd); | |
6715 | ||
6716 | vdev_config_dirty(tvd); | |
6717 | ||
6718 | /* | |
428870ff BB |
6719 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
6720 | * for any dmu_sync-ed blocks. It will propagate upward when | |
6721 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 6722 | */ |
428870ff | 6723 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 6724 | |
9a49d3f3 BB |
6725 | vdev_dtl_dirty(newvd, DTL_MISSING, |
6726 | TXG_INITIAL, dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 6727 | |
9babb374 | 6728 | if (newvd->vdev_isspare) { |
34dc7c2f | 6729 | spa_spare_activate(newvd); |
12fa0466 | 6730 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); |
9babb374 BB |
6731 | } |
6732 | ||
b128c09f BB |
6733 | oldvdpath = spa_strdup(oldvd->vdev_path); |
6734 | newvdpath = spa_strdup(newvd->vdev_path); | |
6735 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
6736 | |
6737 | /* | |
6738 | * Mark newvd's DTL dirty in this txg. | |
6739 | */ | |
6740 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
6741 | ||
428870ff | 6742 | /* |
9a49d3f3 BB |
6743 | * Schedule the resilver or rebuild to restart in the future. We do |
6744 | * this to ensure that dmu_sync-ed blocks have been stitched into the | |
6745 | * respective datasets. | |
428870ff | 6746 | */ |
9a49d3f3 BB |
6747 | if (rebuild) { |
6748 | newvd->vdev_rebuild_txg = txg; | |
6749 | ||
6750 | vdev_rebuild(tvd); | |
6751 | } else { | |
6752 | newvd->vdev_resilver_txg = txg; | |
6753 | ||
6754 | if (dsl_scan_resilvering(spa_get_dsl(spa)) && | |
6755 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) { | |
6756 | vdev_defer_resilver(newvd); | |
6757 | } else { | |
6758 | dsl_scan_restart_resilver(spa->spa_dsl_pool, | |
6759 | dtl_max_txg); | |
6760 | } | |
6761 | } | |
428870ff | 6762 | |
fb390aaf | 6763 | if (spa->spa_bootfs) |
12fa0466 | 6764 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); |
fb390aaf | 6765 | |
12fa0466 | 6766 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); |
fb390aaf | 6767 | |
428870ff BB |
6768 | /* |
6769 | * Commit the config | |
6770 | */ | |
6771 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 6772 | |
6f1ffb06 | 6773 | spa_history_log_internal(spa, "vdev attach", NULL, |
428870ff | 6774 | "%s vdev=%s %s vdev=%s", |
45d1cae3 BB |
6775 | replacing && newvd_isspare ? "spare in" : |
6776 | replacing ? "replace" : "attach", newvdpath, | |
6777 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
6778 | |
6779 | spa_strfree(oldvdpath); | |
6780 | spa_strfree(newvdpath); | |
6781 | ||
34dc7c2f BB |
6782 | return (0); |
6783 | } | |
6784 | ||
6785 | /* | |
6786 | * Detach a device from a mirror or replacing vdev. | |
d3cc8b15 | 6787 | * |
34dc7c2f BB |
6788 | * If 'replace_done' is specified, only detach if the parent |
6789 | * is a replacing vdev. | |
6790 | */ | |
6791 | int | |
fb5f0bc8 | 6792 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
6793 | { |
6794 | uint64_t txg; | |
fb5f0bc8 | 6795 | int error; |
2a8ba608 | 6796 | vdev_t *rvd __maybe_unused = spa->spa_root_vdev; |
34dc7c2f BB |
6797 | vdev_t *vd, *pvd, *cvd, *tvd; |
6798 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 6799 | uint64_t unspare_guid = 0; |
428870ff | 6800 | char *vdpath; |
1c27024e | 6801 | |
572e2857 BB |
6802 | ASSERT(spa_writeable(spa)); |
6803 | ||
9a49d3f3 | 6804 | txg = spa_vdev_detach_enter(spa, guid); |
34dc7c2f | 6805 | |
b128c09f | 6806 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6807 | |
d2734cce SD |
6808 | /* |
6809 | * Besides being called directly from the userland through the | |
6810 | * ioctl interface, spa_vdev_detach() can be potentially called | |
6811 | * at the end of spa_vdev_resilver_done(). | |
6812 | * | |
6813 | * In the regular case, when we have a checkpoint this shouldn't | |
6814 | * happen as we never empty the DTLs of a vdev during the scrub | |
6815 | * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() | |
6816 | * should never get here when we have a checkpoint. | |
6817 | * | |
6818 | * That said, even in a case when we checkpoint the pool exactly | |
6819 | * as spa_vdev_resilver_done() calls this function everything | |
6820 | * should be fine as the resilver will return right away. | |
6821 | */ | |
6822 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
6823 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6824 | error = (spa_has_checkpoint(spa)) ? | |
6825 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6826 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6827 | } | |
6828 | ||
34dc7c2f BB |
6829 | if (vd == NULL) |
6830 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6831 | ||
6832 | if (!vd->vdev_ops->vdev_op_leaf) | |
6833 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6834 | ||
6835 | pvd = vd->vdev_parent; | |
6836 | ||
fb5f0bc8 BB |
6837 | /* |
6838 | * If the parent/child relationship is not as expected, don't do it. | |
6839 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
6840 | * vdev that's replacing B with C. The user's intent in replacing | |
6841 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
6842 | * the replace by detaching C, the expected behavior is to end up | |
6843 | * M(A,B). But suppose that right after deciding to detach C, | |
6844 | * the replacement of B completes. We would have M(A,C), and then | |
6845 | * ask to detach C, which would leave us with just A -- not what | |
6846 | * the user wanted. To prevent this, we make sure that the | |
6847 | * parent/child relationship hasn't changed -- in this example, | |
6848 | * that C's parent is still the replacing vdev R. | |
6849 | */ | |
6850 | if (pvd->vdev_guid != pguid && pguid != 0) | |
6851 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
6852 | ||
34dc7c2f | 6853 | /* |
572e2857 | 6854 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 6855 | */ |
572e2857 BB |
6856 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
6857 | pvd->vdev_ops != &vdev_spare_ops) | |
6858 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
6859 | |
6860 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
6861 | spa_version(spa) >= SPA_VERSION_SPARES); | |
6862 | ||
6863 | /* | |
6864 | * Only mirror, replacing, and spare vdevs support detach. | |
6865 | */ | |
6866 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
6867 | pvd->vdev_ops != &vdev_mirror_ops && | |
6868 | pvd->vdev_ops != &vdev_spare_ops) | |
6869 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6870 | ||
6871 | /* | |
fb5f0bc8 BB |
6872 | * If this device has the only valid copy of some data, |
6873 | * we cannot safely detach it. | |
34dc7c2f | 6874 | */ |
fb5f0bc8 | 6875 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
6876 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
6877 | ||
fb5f0bc8 | 6878 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 6879 | |
b128c09f BB |
6880 | /* |
6881 | * If we are detaching the second disk from a replacing vdev, then | |
6882 | * check to see if we changed the original vdev's path to have "/old" | |
6883 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
6884 | */ | |
572e2857 BB |
6885 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
6886 | vd->vdev_path != NULL) { | |
6887 | size_t len = strlen(vd->vdev_path); | |
6888 | ||
1c27024e | 6889 | for (int c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
6890 | cvd = pvd->vdev_child[c]; |
6891 | ||
6892 | if (cvd == vd || cvd->vdev_path == NULL) | |
6893 | continue; | |
6894 | ||
6895 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
6896 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
6897 | spa_strfree(cvd->vdev_path); | |
6898 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
6899 | break; | |
6900 | } | |
b128c09f BB |
6901 | } |
6902 | } | |
6903 | ||
34dc7c2f BB |
6904 | /* |
6905 | * If we are detaching the original disk from a spare, then it implies | |
6906 | * that the spare should become a real disk, and be removed from the | |
6907 | * active spare list for the pool. | |
6908 | */ | |
6909 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 BB |
6910 | vd->vdev_id == 0 && |
6911 | pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) | |
34dc7c2f BB |
6912 | unspare = B_TRUE; |
6913 | ||
6914 | /* | |
6915 | * Erase the disk labels so the disk can be used for other things. | |
6916 | * This must be done after all other error cases are handled, | |
6917 | * but before we disembowel vd (so we can still do I/O to it). | |
6918 | * But if we can't do it, don't treat the error as fatal -- | |
6919 | * it may be that the unwritability of the disk is the reason | |
6920 | * it's being detached! | |
6921 | */ | |
6922 | error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
6923 | ||
6924 | /* | |
6925 | * Remove vd from its parent and compact the parent's children. | |
6926 | */ | |
6927 | vdev_remove_child(pvd, vd); | |
6928 | vdev_compact_children(pvd); | |
6929 | ||
6930 | /* | |
6931 | * Remember one of the remaining children so we can get tvd below. | |
6932 | */ | |
572e2857 | 6933 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
6934 | |
6935 | /* | |
6936 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
6937 | * do it now, marking the vdev as no longer a spare in the process. |
6938 | * We must do this before vdev_remove_parent(), because that can | |
6939 | * change the GUID if it creates a new toplevel GUID. For a similar | |
6940 | * reason, we must remove the spare now, in the same txg as the detach; | |
6941 | * otherwise someone could attach a new sibling, change the GUID, and | |
6942 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
6943 | */ |
6944 | if (unspare) { | |
6945 | ASSERT(cvd->vdev_isspare); | |
6946 | spa_spare_remove(cvd); | |
6947 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 6948 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 6949 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
6950 | } |
6951 | ||
428870ff BB |
6952 | /* |
6953 | * If the parent mirror/replacing vdev only has one child, | |
6954 | * the parent is no longer needed. Remove it from the tree. | |
6955 | */ | |
572e2857 BB |
6956 | if (pvd->vdev_children == 1) { |
6957 | if (pvd->vdev_ops == &vdev_spare_ops) | |
6958 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 6959 | vdev_remove_parent(cvd); |
572e2857 BB |
6960 | } |
6961 | ||
428870ff BB |
6962 | /* |
6963 | * We don't set tvd until now because the parent we just removed | |
6964 | * may have been the previous top-level vdev. | |
6965 | */ | |
6966 | tvd = cvd->vdev_top; | |
6967 | ASSERT(tvd->vdev_parent == rvd); | |
6968 | ||
6969 | /* | |
6970 | * Reevaluate the parent vdev state. | |
6971 | */ | |
6972 | vdev_propagate_state(cvd); | |
6973 | ||
6974 | /* | |
6975 | * If the 'autoexpand' property is set on the pool then automatically | |
6976 | * try to expand the size of the pool. For example if the device we | |
6977 | * just detached was smaller than the others, it may be possible to | |
6978 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
6979 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
6980 | */ | |
6981 | if (spa->spa_autoexpand) { | |
6982 | vdev_reopen(tvd); | |
6983 | vdev_expand(tvd, txg); | |
6984 | } | |
6985 | ||
6986 | vdev_config_dirty(tvd); | |
6987 | ||
6988 | /* | |
6989 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
6990 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
6991 | * But first make sure we're not on any *other* txg's DTL list, to | |
6992 | * prevent vd from being accessed after it's freed. | |
6993 | */ | |
b6ca6193 | 6994 | vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none"); |
1c27024e | 6995 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff BB |
6996 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
6997 | vd->vdev_detached = B_TRUE; | |
6998 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
6999 | ||
12fa0466 | 7000 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); |
e60e158e | 7001 | spa_notify_waiters(spa); |
428870ff | 7002 | |
572e2857 BB |
7003 | /* hang on to the spa before we release the lock */ |
7004 | spa_open_ref(spa, FTAG); | |
7005 | ||
428870ff BB |
7006 | error = spa_vdev_exit(spa, vd, txg, 0); |
7007 | ||
6f1ffb06 | 7008 | spa_history_log_internal(spa, "detach", NULL, |
428870ff BB |
7009 | "vdev=%s", vdpath); |
7010 | spa_strfree(vdpath); | |
7011 | ||
7012 | /* | |
7013 | * If this was the removal of the original device in a hot spare vdev, | |
7014 | * then we want to go through and remove the device from the hot spare | |
7015 | * list of every other pool. | |
7016 | */ | |
7017 | if (unspare) { | |
572e2857 BB |
7018 | spa_t *altspa = NULL; |
7019 | ||
428870ff | 7020 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
7021 | while ((altspa = spa_next(altspa)) != NULL) { |
7022 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
7023 | altspa == spa) | |
428870ff | 7024 | continue; |
572e2857 BB |
7025 | |
7026 | spa_open_ref(altspa, FTAG); | |
428870ff | 7027 | mutex_exit(&spa_namespace_lock); |
572e2857 | 7028 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 7029 | mutex_enter(&spa_namespace_lock); |
572e2857 | 7030 | spa_close(altspa, FTAG); |
428870ff BB |
7031 | } |
7032 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
7033 | |
7034 | /* search the rest of the vdevs for spares to remove */ | |
7035 | spa_vdev_resilver_done(spa); | |
428870ff BB |
7036 | } |
7037 | ||
572e2857 BB |
7038 | /* all done with the spa; OK to release */ |
7039 | mutex_enter(&spa_namespace_lock); | |
7040 | spa_close(spa, FTAG); | |
7041 | mutex_exit(&spa_namespace_lock); | |
7042 | ||
428870ff BB |
7043 | return (error); |
7044 | } | |
7045 | ||
c10d37dd GW |
7046 | static int |
7047 | spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7048 | list_t *vd_list) | |
619f0976 | 7049 | { |
c10d37dd GW |
7050 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7051 | ||
619f0976 GW |
7052 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); |
7053 | ||
7054 | /* Look up vdev and ensure it's a leaf. */ | |
7055 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7056 | if (vd == NULL || vd->vdev_detached) { | |
7057 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7058 | return (SET_ERROR(ENODEV)); |
7059 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7060 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7061 | return (SET_ERROR(EINVAL)); |
7062 | } else if (!vdev_writeable(vd)) { | |
7063 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7064 | return (SET_ERROR(EROFS)); |
7065 | } | |
7066 | mutex_enter(&vd->vdev_initialize_lock); | |
7067 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7068 | ||
7069 | /* | |
7070 | * When we activate an initialize action we check to see | |
7071 | * if the vdev_initialize_thread is NULL. We do this instead | |
7072 | * of using the vdev_initialize_state since there might be | |
7073 | * a previous initialization process which has completed but | |
7074 | * the thread is not exited. | |
7075 | */ | |
1b939560 | 7076 | if (cmd_type == POOL_INITIALIZE_START && |
619f0976 GW |
7077 | (vd->vdev_initialize_thread != NULL || |
7078 | vd->vdev_top->vdev_removing)) { | |
7079 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7080 | return (SET_ERROR(EBUSY)); |
7081 | } else if (cmd_type == POOL_INITIALIZE_CANCEL && | |
7082 | (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && | |
7083 | vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { | |
7084 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7085 | return (SET_ERROR(ESRCH)); |
7086 | } else if (cmd_type == POOL_INITIALIZE_SUSPEND && | |
7087 | vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { | |
7088 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7089 | return (SET_ERROR(ESRCH)); |
7090 | } | |
7091 | ||
7092 | switch (cmd_type) { | |
1b939560 | 7093 | case POOL_INITIALIZE_START: |
619f0976 GW |
7094 | vdev_initialize(vd); |
7095 | break; | |
7096 | case POOL_INITIALIZE_CANCEL: | |
c10d37dd | 7097 | vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); |
619f0976 GW |
7098 | break; |
7099 | case POOL_INITIALIZE_SUSPEND: | |
c10d37dd | 7100 | vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); |
619f0976 GW |
7101 | break; |
7102 | default: | |
7103 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7104 | } | |
7105 | mutex_exit(&vd->vdev_initialize_lock); | |
7106 | ||
c10d37dd GW |
7107 | return (0); |
7108 | } | |
7109 | ||
7110 | int | |
7111 | spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, | |
7112 | nvlist_t *vdev_errlist) | |
7113 | { | |
7114 | int total_errors = 0; | |
7115 | list_t vd_list; | |
7116 | ||
7117 | list_create(&vd_list, sizeof (vdev_t), | |
7118 | offsetof(vdev_t, vdev_initialize_node)); | |
7119 | ||
7120 | /* | |
7121 | * We hold the namespace lock through the whole function | |
7122 | * to prevent any changes to the pool while we're starting or | |
7123 | * stopping initialization. The config and state locks are held so that | |
7124 | * we can properly assess the vdev state before we commit to | |
7125 | * the initializing operation. | |
7126 | */ | |
7127 | mutex_enter(&spa_namespace_lock); | |
7128 | ||
7129 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7130 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7131 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7132 | ||
7133 | int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, | |
7134 | &vd_list); | |
7135 | if (error != 0) { | |
7136 | char guid_as_str[MAXNAMELEN]; | |
7137 | ||
7138 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7139 | "%llu", (unsigned long long)vdev_guid); | |
7140 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7141 | total_errors++; | |
7142 | } | |
7143 | } | |
7144 | ||
7145 | /* Wait for all initialize threads to stop. */ | |
7146 | vdev_initialize_stop_wait(spa, &vd_list); | |
7147 | ||
619f0976 GW |
7148 | /* Sync out the initializing state */ |
7149 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7150 | mutex_exit(&spa_namespace_lock); | |
7151 | ||
c10d37dd | 7152 | list_destroy(&vd_list); |
619f0976 | 7153 | |
c10d37dd GW |
7154 | return (total_errors); |
7155 | } | |
619f0976 | 7156 | |
1b939560 BB |
7157 | static int |
7158 | spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7159 | uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list) | |
7160 | { | |
7161 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7162 | ||
7163 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
7164 | ||
7165 | /* Look up vdev and ensure it's a leaf. */ | |
7166 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7167 | if (vd == NULL || vd->vdev_detached) { | |
7168 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7169 | return (SET_ERROR(ENODEV)); | |
7170 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7171 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7172 | return (SET_ERROR(EINVAL)); | |
7173 | } else if (!vdev_writeable(vd)) { | |
7174 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7175 | return (SET_ERROR(EROFS)); | |
7176 | } else if (!vd->vdev_has_trim) { | |
7177 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7178 | return (SET_ERROR(EOPNOTSUPP)); | |
7179 | } else if (secure && !vd->vdev_has_securetrim) { | |
7180 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7181 | return (SET_ERROR(EOPNOTSUPP)); | |
7182 | } | |
7183 | mutex_enter(&vd->vdev_trim_lock); | |
7184 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7185 | ||
7186 | /* | |
7187 | * When we activate a TRIM action we check to see if the | |
7188 | * vdev_trim_thread is NULL. We do this instead of using the | |
7189 | * vdev_trim_state since there might be a previous TRIM process | |
7190 | * which has completed but the thread is not exited. | |
7191 | */ | |
7192 | if (cmd_type == POOL_TRIM_START && | |
7193 | (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing)) { | |
7194 | mutex_exit(&vd->vdev_trim_lock); | |
7195 | return (SET_ERROR(EBUSY)); | |
7196 | } else if (cmd_type == POOL_TRIM_CANCEL && | |
7197 | (vd->vdev_trim_state != VDEV_TRIM_ACTIVE && | |
7198 | vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) { | |
7199 | mutex_exit(&vd->vdev_trim_lock); | |
7200 | return (SET_ERROR(ESRCH)); | |
7201 | } else if (cmd_type == POOL_TRIM_SUSPEND && | |
7202 | vd->vdev_trim_state != VDEV_TRIM_ACTIVE) { | |
7203 | mutex_exit(&vd->vdev_trim_lock); | |
7204 | return (SET_ERROR(ESRCH)); | |
7205 | } | |
7206 | ||
7207 | switch (cmd_type) { | |
7208 | case POOL_TRIM_START: | |
7209 | vdev_trim(vd, rate, partial, secure); | |
7210 | break; | |
7211 | case POOL_TRIM_CANCEL: | |
7212 | vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list); | |
7213 | break; | |
7214 | case POOL_TRIM_SUSPEND: | |
7215 | vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list); | |
7216 | break; | |
7217 | default: | |
7218 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7219 | } | |
7220 | mutex_exit(&vd->vdev_trim_lock); | |
7221 | ||
7222 | return (0); | |
7223 | } | |
7224 | ||
7225 | /* | |
7226 | * Initiates a manual TRIM for the requested vdevs. This kicks off individual | |
7227 | * TRIM threads for each child vdev. These threads pass over all of the free | |
7228 | * space in the vdev's metaslabs and issues TRIM commands for that space. | |
7229 | */ | |
7230 | int | |
7231 | spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate, | |
7232 | boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist) | |
7233 | { | |
7234 | int total_errors = 0; | |
7235 | list_t vd_list; | |
7236 | ||
7237 | list_create(&vd_list, sizeof (vdev_t), | |
7238 | offsetof(vdev_t, vdev_trim_node)); | |
7239 | ||
7240 | /* | |
7241 | * We hold the namespace lock through the whole function | |
7242 | * to prevent any changes to the pool while we're starting or | |
7243 | * stopping TRIM. The config and state locks are held so that | |
7244 | * we can properly assess the vdev state before we commit to | |
7245 | * the TRIM operation. | |
7246 | */ | |
7247 | mutex_enter(&spa_namespace_lock); | |
7248 | ||
7249 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7250 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7251 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7252 | ||
7253 | int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type, | |
7254 | rate, partial, secure, &vd_list); | |
7255 | if (error != 0) { | |
7256 | char guid_as_str[MAXNAMELEN]; | |
7257 | ||
7258 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7259 | "%llu", (unsigned long long)vdev_guid); | |
7260 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7261 | total_errors++; | |
7262 | } | |
7263 | } | |
7264 | ||
7265 | /* Wait for all TRIM threads to stop. */ | |
7266 | vdev_trim_stop_wait(spa, &vd_list); | |
7267 | ||
7268 | /* Sync out the TRIM state */ | |
7269 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7270 | mutex_exit(&spa_namespace_lock); | |
7271 | ||
7272 | list_destroy(&vd_list); | |
7273 | ||
7274 | return (total_errors); | |
7275 | } | |
7276 | ||
428870ff BB |
7277 | /* |
7278 | * Split a set of devices from their mirrors, and create a new pool from them. | |
7279 | */ | |
7280 | int | |
7281 | spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, | |
7282 | nvlist_t *props, boolean_t exp) | |
7283 | { | |
7284 | int error = 0; | |
7285 | uint64_t txg, *glist; | |
7286 | spa_t *newspa; | |
7287 | uint_t c, children, lastlog; | |
7288 | nvlist_t **child, *nvl, *tmp; | |
7289 | dmu_tx_t *tx; | |
7290 | char *altroot = NULL; | |
7291 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
7292 | boolean_t activate_slog; | |
7293 | ||
572e2857 | 7294 | ASSERT(spa_writeable(spa)); |
428870ff BB |
7295 | |
7296 | txg = spa_vdev_enter(spa); | |
7297 | ||
d2734cce SD |
7298 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7299 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7300 | error = (spa_has_checkpoint(spa)) ? | |
7301 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7302 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7303 | } | |
7304 | ||
428870ff BB |
7305 | /* clear the log and flush everything up to now */ |
7306 | activate_slog = spa_passivate_log(spa); | |
7307 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
a1d477c2 | 7308 | error = spa_reset_logs(spa); |
428870ff BB |
7309 | txg = spa_vdev_config_enter(spa); |
7310 | ||
7311 | if (activate_slog) | |
7312 | spa_activate_log(spa); | |
7313 | ||
7314 | if (error != 0) | |
7315 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7316 | ||
7317 | /* check new spa name before going any further */ | |
7318 | if (spa_lookup(newname) != NULL) | |
7319 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
7320 | ||
7321 | /* | |
7322 | * scan through all the children to ensure they're all mirrors | |
7323 | */ | |
7324 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
7325 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
7326 | &children) != 0) | |
7327 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7328 | ||
7329 | /* first, check to ensure we've got the right child count */ | |
7330 | rvd = spa->spa_root_vdev; | |
7331 | lastlog = 0; | |
7332 | for (c = 0; c < rvd->vdev_children; c++) { | |
7333 | vdev_t *vd = rvd->vdev_child[c]; | |
7334 | ||
7335 | /* don't count the holes & logs as children */ | |
1b664952 GA |
7336 | if (vd->vdev_islog || (vd->vdev_ops != &vdev_indirect_ops && |
7337 | !vdev_is_concrete(vd))) { | |
428870ff BB |
7338 | if (lastlog == 0) |
7339 | lastlog = c; | |
7340 | continue; | |
7341 | } | |
7342 | ||
7343 | lastlog = 0; | |
7344 | } | |
7345 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
7346 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7347 | ||
7348 | /* next, ensure no spare or cache devices are part of the split */ | |
7349 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
7350 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
7351 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7352 | ||
79c76d5b BB |
7353 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); |
7354 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
428870ff BB |
7355 | |
7356 | /* then, loop over each vdev and validate it */ | |
7357 | for (c = 0; c < children; c++) { | |
7358 | uint64_t is_hole = 0; | |
7359 | ||
7360 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
7361 | &is_hole); | |
7362 | ||
7363 | if (is_hole != 0) { | |
7364 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
7365 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
7366 | continue; | |
7367 | } else { | |
2e528b49 | 7368 | error = SET_ERROR(EINVAL); |
428870ff BB |
7369 | break; |
7370 | } | |
7371 | } | |
7372 | ||
1b664952 GA |
7373 | /* deal with indirect vdevs */ |
7374 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ops == | |
7375 | &vdev_indirect_ops) | |
7376 | continue; | |
7377 | ||
428870ff BB |
7378 | /* which disk is going to be split? */ |
7379 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
7380 | &glist[c]) != 0) { | |
2e528b49 | 7381 | error = SET_ERROR(EINVAL); |
428870ff BB |
7382 | break; |
7383 | } | |
7384 | ||
7385 | /* look it up in the spa */ | |
7386 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
7387 | if (vml[c] == NULL) { | |
2e528b49 | 7388 | error = SET_ERROR(ENODEV); |
428870ff BB |
7389 | break; |
7390 | } | |
7391 | ||
7392 | /* make sure there's nothing stopping the split */ | |
7393 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
7394 | vml[c]->vdev_islog || | |
a1d477c2 | 7395 | !vdev_is_concrete(vml[c]) || |
428870ff BB |
7396 | vml[c]->vdev_isspare || |
7397 | vml[c]->vdev_isl2cache || | |
7398 | !vdev_writeable(vml[c]) || | |
7399 | vml[c]->vdev_children != 0 || | |
7400 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
7401 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
2e528b49 | 7402 | error = SET_ERROR(EINVAL); |
428870ff BB |
7403 | break; |
7404 | } | |
7405 | ||
733b5722 RS |
7406 | if (vdev_dtl_required(vml[c]) || |
7407 | vdev_resilver_needed(vml[c], NULL, NULL)) { | |
2e528b49 | 7408 | error = SET_ERROR(EBUSY); |
428870ff BB |
7409 | break; |
7410 | } | |
7411 | ||
7412 | /* we need certain info from the top level */ | |
7413 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, | |
7414 | vml[c]->vdev_top->vdev_ms_array) == 0); | |
7415 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
7416 | vml[c]->vdev_top->vdev_ms_shift) == 0); | |
7417 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
7418 | vml[c]->vdev_top->vdev_asize) == 0); | |
7419 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
7420 | vml[c]->vdev_top->vdev_ashift) == 0); | |
e0ab3ab5 JS |
7421 | |
7422 | /* transfer per-vdev ZAPs */ | |
7423 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
7424 | VERIFY0(nvlist_add_uint64(child[c], | |
7425 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
7426 | ||
7427 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
7428 | VERIFY0(nvlist_add_uint64(child[c], | |
7429 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
7430 | vml[c]->vdev_parent->vdev_top_zap)); | |
428870ff BB |
7431 | } |
7432 | ||
7433 | if (error != 0) { | |
7434 | kmem_free(vml, children * sizeof (vdev_t *)); | |
7435 | kmem_free(glist, children * sizeof (uint64_t)); | |
7436 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7437 | } | |
7438 | ||
7439 | /* stop writers from using the disks */ | |
7440 | for (c = 0; c < children; c++) { | |
7441 | if (vml[c] != NULL) | |
7442 | vml[c]->vdev_offline = B_TRUE; | |
7443 | } | |
7444 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
7445 | |
7446 | /* | |
428870ff BB |
7447 | * Temporarily record the splitting vdevs in the spa config. This |
7448 | * will disappear once the config is regenerated. | |
34dc7c2f | 7449 | */ |
79c76d5b | 7450 | VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
7451 | VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, |
7452 | glist, children) == 0); | |
7453 | kmem_free(glist, children * sizeof (uint64_t)); | |
34dc7c2f | 7454 | |
428870ff BB |
7455 | mutex_enter(&spa->spa_props_lock); |
7456 | VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, | |
7457 | nvl) == 0); | |
7458 | mutex_exit(&spa->spa_props_lock); | |
7459 | spa->spa_config_splitting = nvl; | |
7460 | vdev_config_dirty(spa->spa_root_vdev); | |
7461 | ||
7462 | /* configure and create the new pool */ | |
7463 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); | |
7464 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
7465 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); | |
7466 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
7467 | spa_version(spa)) == 0); | |
7468 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
7469 | spa->spa_config_txg) == 0); | |
7470 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
7471 | spa_generate_guid(NULL)) == 0); | |
e0ab3ab5 | 7472 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); |
428870ff BB |
7473 | (void) nvlist_lookup_string(props, |
7474 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 7475 | |
428870ff BB |
7476 | /* add the new pool to the namespace */ |
7477 | newspa = spa_add(newname, config, altroot); | |
e0ab3ab5 | 7478 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7479 | newspa->spa_config_txg = spa->spa_config_txg; |
7480 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
7481 | ||
7482 | /* release the spa config lock, retaining the namespace lock */ | |
7483 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
7484 | ||
7485 | if (zio_injection_enabled) | |
7486 | zio_handle_panic_injection(spa, FTAG, 1); | |
7487 | ||
7488 | spa_activate(newspa, spa_mode_global); | |
7489 | spa_async_suspend(newspa); | |
7490 | ||
c10d37dd | 7491 | /* |
1b939560 BB |
7492 | * Temporarily stop the initializing and TRIM activity. We set the |
7493 | * state to ACTIVE so that we know to resume initializing or TRIM | |
7494 | * once the split has completed. | |
c10d37dd | 7495 | */ |
1b939560 BB |
7496 | list_t vd_initialize_list; |
7497 | list_create(&vd_initialize_list, sizeof (vdev_t), | |
c10d37dd GW |
7498 | offsetof(vdev_t, vdev_initialize_node)); |
7499 | ||
1b939560 BB |
7500 | list_t vd_trim_list; |
7501 | list_create(&vd_trim_list, sizeof (vdev_t), | |
7502 | offsetof(vdev_t, vdev_trim_node)); | |
7503 | ||
619f0976 | 7504 | for (c = 0; c < children; c++) { |
1b664952 | 7505 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
619f0976 | 7506 | mutex_enter(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7507 | vdev_initialize_stop(vml[c], |
7508 | VDEV_INITIALIZE_ACTIVE, &vd_initialize_list); | |
619f0976 | 7509 | mutex_exit(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7510 | |
7511 | mutex_enter(&vml[c]->vdev_trim_lock); | |
7512 | vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list); | |
7513 | mutex_exit(&vml[c]->vdev_trim_lock); | |
619f0976 GW |
7514 | } |
7515 | } | |
1b939560 BB |
7516 | |
7517 | vdev_initialize_stop_wait(spa, &vd_initialize_list); | |
7518 | vdev_trim_stop_wait(spa, &vd_trim_list); | |
7519 | ||
7520 | list_destroy(&vd_initialize_list); | |
7521 | list_destroy(&vd_trim_list); | |
619f0976 | 7522 | |
6cb8e530 | 7523 | newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; |
8b27e08e | 7524 | newspa->spa_is_splitting = B_TRUE; |
6cb8e530 | 7525 | |
428870ff | 7526 | /* create the new pool from the disks of the original pool */ |
6cb8e530 | 7527 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); |
428870ff BB |
7528 | if (error) |
7529 | goto out; | |
7530 | ||
7531 | /* if that worked, generate a real config for the new pool */ | |
7532 | if (newspa->spa_root_vdev != NULL) { | |
7533 | VERIFY(nvlist_alloc(&newspa->spa_config_splitting, | |
79c76d5b | 7534 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
7535 | VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, |
7536 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); | |
7537 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, | |
7538 | B_TRUE)); | |
9babb374 | 7539 | } |
34dc7c2f | 7540 | |
428870ff BB |
7541 | /* set the props */ |
7542 | if (props != NULL) { | |
7543 | spa_configfile_set(newspa, props, B_FALSE); | |
7544 | error = spa_prop_set(newspa, props); | |
7545 | if (error) | |
7546 | goto out; | |
7547 | } | |
34dc7c2f | 7548 | |
428870ff BB |
7549 | /* flush everything */ |
7550 | txg = spa_vdev_config_enter(newspa); | |
7551 | vdev_config_dirty(newspa->spa_root_vdev); | |
7552 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 7553 | |
428870ff BB |
7554 | if (zio_injection_enabled) |
7555 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 7556 | |
428870ff | 7557 | spa_async_resume(newspa); |
34dc7c2f | 7558 | |
428870ff BB |
7559 | /* finally, update the original pool's config */ |
7560 | txg = spa_vdev_config_enter(spa); | |
7561 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
7562 | error = dmu_tx_assign(tx, TXG_WAIT); | |
7563 | if (error != 0) | |
7564 | dmu_tx_abort(tx); | |
7565 | for (c = 0; c < children; c++) { | |
1b664952 | 7566 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
234234ca RS |
7567 | vdev_t *tvd = vml[c]->vdev_top; |
7568 | ||
7569 | /* | |
7570 | * Need to be sure the detachable VDEV is not | |
7571 | * on any *other* txg's DTL list to prevent it | |
7572 | * from being accessed after it's freed. | |
7573 | */ | |
7574 | for (int t = 0; t < TXG_SIZE; t++) { | |
7575 | (void) txg_list_remove_this( | |
7576 | &tvd->vdev_dtl_list, vml[c], t); | |
7577 | } | |
7578 | ||
428870ff BB |
7579 | vdev_split(vml[c]); |
7580 | if (error == 0) | |
6f1ffb06 MA |
7581 | spa_history_log_internal(spa, "detach", tx, |
7582 | "vdev=%s", vml[c]->vdev_path); | |
e0ab3ab5 | 7583 | |
428870ff | 7584 | vdev_free(vml[c]); |
34dc7c2f | 7585 | } |
34dc7c2f | 7586 | } |
e0ab3ab5 | 7587 | spa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7588 | vdev_config_dirty(spa->spa_root_vdev); |
7589 | spa->spa_config_splitting = NULL; | |
7590 | nvlist_free(nvl); | |
7591 | if (error == 0) | |
7592 | dmu_tx_commit(tx); | |
7593 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
7594 | ||
7595 | if (zio_injection_enabled) | |
7596 | zio_handle_panic_injection(spa, FTAG, 3); | |
7597 | ||
7598 | /* split is complete; log a history record */ | |
6f1ffb06 MA |
7599 | spa_history_log_internal(newspa, "split", NULL, |
7600 | "from pool %s", spa_name(spa)); | |
428870ff | 7601 | |
8b27e08e | 7602 | newspa->spa_is_splitting = B_FALSE; |
428870ff BB |
7603 | kmem_free(vml, children * sizeof (vdev_t *)); |
7604 | ||
7605 | /* if we're not going to mount the filesystems in userland, export */ | |
7606 | if (exp) | |
7607 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
7608 | B_FALSE, B_FALSE); | |
7609 | ||
7610 | return (error); | |
7611 | ||
7612 | out: | |
7613 | spa_unload(newspa); | |
7614 | spa_deactivate(newspa); | |
7615 | spa_remove(newspa); | |
7616 | ||
7617 | txg = spa_vdev_config_enter(spa); | |
7618 | ||
7619 | /* re-online all offlined disks */ | |
7620 | for (c = 0; c < children; c++) { | |
7621 | if (vml[c] != NULL) | |
7622 | vml[c]->vdev_offline = B_FALSE; | |
7623 | } | |
619f0976 | 7624 | |
1b939560 | 7625 | /* restart initializing or trimming disks as necessary */ |
619f0976 | 7626 | spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); |
1b939560 BB |
7627 | spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); |
7628 | spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); | |
619f0976 | 7629 | |
428870ff BB |
7630 | vdev_reopen(spa->spa_root_vdev); |
7631 | ||
7632 | nvlist_free(spa->spa_config_splitting); | |
7633 | spa->spa_config_splitting = NULL; | |
7634 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 7635 | |
428870ff | 7636 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
7637 | return (error); |
7638 | } | |
7639 | ||
34dc7c2f BB |
7640 | /* |
7641 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
d3cc8b15 | 7642 | * currently spared, so we can detach it. |
34dc7c2f BB |
7643 | */ |
7644 | static vdev_t * | |
7645 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
7646 | { | |
7647 | vdev_t *newvd, *oldvd; | |
34dc7c2f | 7648 | |
1c27024e | 7649 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
7650 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
7651 | if (oldvd != NULL) | |
7652 | return (oldvd); | |
7653 | } | |
7654 | ||
7655 | /* | |
572e2857 BB |
7656 | * Check for a completed replacement. We always consider the first |
7657 | * vdev in the list to be the oldest vdev, and the last one to be | |
7658 | * the newest (see spa_vdev_attach() for how that works). In | |
7659 | * the case where the newest vdev is faulted, we will not automatically | |
7660 | * remove it after a resilver completes. This is OK as it will require | |
7661 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 7662 | */ |
572e2857 BB |
7663 | if (vd->vdev_ops == &vdev_replacing_ops) { |
7664 | ASSERT(vd->vdev_children > 1); | |
7665 | ||
7666 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 7667 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 7668 | |
fb5f0bc8 | 7669 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7670 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 7671 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7672 | return (oldvd); |
34dc7c2f BB |
7673 | } |
7674 | ||
7675 | /* | |
7676 | * Check for a completed resilver with the 'unspare' flag set. | |
f65fbee1 | 7677 | * Also potentially update faulted state. |
34dc7c2f | 7678 | */ |
572e2857 BB |
7679 | if (vd->vdev_ops == &vdev_spare_ops) { |
7680 | vdev_t *first = vd->vdev_child[0]; | |
7681 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
7682 | ||
7683 | if (last->vdev_unspare) { | |
7684 | oldvd = first; | |
7685 | newvd = last; | |
7686 | } else if (first->vdev_unspare) { | |
7687 | oldvd = last; | |
7688 | newvd = first; | |
7689 | } else { | |
7690 | oldvd = NULL; | |
7691 | } | |
34dc7c2f | 7692 | |
572e2857 | 7693 | if (oldvd != NULL && |
fb5f0bc8 | 7694 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7695 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 7696 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7697 | return (oldvd); |
572e2857 | 7698 | |
f65fbee1 JJ |
7699 | vdev_propagate_state(vd); |
7700 | ||
572e2857 BB |
7701 | /* |
7702 | * If there are more than two spares attached to a disk, | |
7703 | * and those spares are not required, then we want to | |
7704 | * attempt to free them up now so that they can be used | |
7705 | * by other pools. Once we're back down to a single | |
7706 | * disk+spare, we stop removing them. | |
7707 | */ | |
7708 | if (vd->vdev_children > 2) { | |
7709 | newvd = vd->vdev_child[1]; | |
7710 | ||
7711 | if (newvd->vdev_isspare && last->vdev_isspare && | |
7712 | vdev_dtl_empty(last, DTL_MISSING) && | |
7713 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
7714 | !vdev_dtl_required(newvd)) | |
7715 | return (newvd); | |
34dc7c2f | 7716 | } |
34dc7c2f BB |
7717 | } |
7718 | ||
7719 | return (NULL); | |
7720 | } | |
7721 | ||
7722 | static void | |
7723 | spa_vdev_resilver_done(spa_t *spa) | |
7724 | { | |
fb5f0bc8 BB |
7725 | vdev_t *vd, *pvd, *ppvd; |
7726 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 7727 | |
fb5f0bc8 | 7728 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7729 | |
7730 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
7731 | pvd = vd->vdev_parent; |
7732 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 7733 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
7734 | pguid = pvd->vdev_guid; |
7735 | ppguid = ppvd->vdev_guid; | |
7736 | sguid = 0; | |
34dc7c2f BB |
7737 | /* |
7738 | * If we have just finished replacing a hot spared device, then | |
7739 | * we need to detach the parent's first child (the original hot | |
7740 | * spare) as well. | |
7741 | */ | |
572e2857 BB |
7742 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
7743 | ppvd->vdev_children == 2) { | |
34dc7c2f | 7744 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 7745 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 7746 | } |
5d1f7fb6 GW |
7747 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); |
7748 | ||
fb5f0bc8 BB |
7749 | spa_config_exit(spa, SCL_ALL, FTAG); |
7750 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 7751 | return; |
fb5f0bc8 | 7752 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 7753 | return; |
fb5f0bc8 | 7754 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7755 | } |
7756 | ||
fb5f0bc8 | 7757 | spa_config_exit(spa, SCL_ALL, FTAG); |
9a49d3f3 BB |
7758 | |
7759 | /* | |
7760 | * If a detach was not performed above replace waiters will not have | |
7761 | * been notified. In which case we must do so now. | |
7762 | */ | |
7763 | spa_notify_waiters(spa); | |
34dc7c2f BB |
7764 | } |
7765 | ||
7766 | /* | |
428870ff | 7767 | * Update the stored path or FRU for this vdev. |
34dc7c2f | 7768 | */ |
65c7cc49 | 7769 | static int |
9babb374 BB |
7770 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
7771 | boolean_t ispath) | |
34dc7c2f | 7772 | { |
b128c09f | 7773 | vdev_t *vd; |
428870ff | 7774 | boolean_t sync = B_FALSE; |
34dc7c2f | 7775 | |
572e2857 BB |
7776 | ASSERT(spa_writeable(spa)); |
7777 | ||
428870ff | 7778 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 7779 | |
9babb374 | 7780 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 7781 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
7782 | |
7783 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 7784 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 7785 | |
9babb374 | 7786 | if (ispath) { |
428870ff BB |
7787 | if (strcmp(value, vd->vdev_path) != 0) { |
7788 | spa_strfree(vd->vdev_path); | |
7789 | vd->vdev_path = spa_strdup(value); | |
7790 | sync = B_TRUE; | |
7791 | } | |
9babb374 | 7792 | } else { |
428870ff BB |
7793 | if (vd->vdev_fru == NULL) { |
7794 | vd->vdev_fru = spa_strdup(value); | |
7795 | sync = B_TRUE; | |
7796 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 7797 | spa_strfree(vd->vdev_fru); |
428870ff BB |
7798 | vd->vdev_fru = spa_strdup(value); |
7799 | sync = B_TRUE; | |
7800 | } | |
9babb374 | 7801 | } |
34dc7c2f | 7802 | |
428870ff | 7803 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
7804 | } |
7805 | ||
9babb374 BB |
7806 | int |
7807 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
7808 | { | |
7809 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
7810 | } | |
7811 | ||
7812 | int | |
7813 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
7814 | { | |
7815 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
7816 | } | |
7817 | ||
34dc7c2f BB |
7818 | /* |
7819 | * ========================================================================== | |
428870ff | 7820 | * SPA Scanning |
34dc7c2f BB |
7821 | * ========================================================================== |
7822 | */ | |
0ea05c64 AP |
7823 | int |
7824 | spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) | |
7825 | { | |
7826 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7827 | ||
7828 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
7829 | return (SET_ERROR(EBUSY)); | |
7830 | ||
7831 | return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); | |
7832 | } | |
34dc7c2f | 7833 | |
34dc7c2f | 7834 | int |
428870ff BB |
7835 | spa_scan_stop(spa_t *spa) |
7836 | { | |
7837 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7838 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
2e528b49 | 7839 | return (SET_ERROR(EBUSY)); |
428870ff BB |
7840 | return (dsl_scan_cancel(spa->spa_dsl_pool)); |
7841 | } | |
7842 | ||
7843 | int | |
7844 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 7845 | { |
b128c09f | 7846 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 7847 | |
428870ff | 7848 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
2e528b49 | 7849 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 7850 | |
fa241660 TC |
7851 | if (func == POOL_SCAN_RESILVER && |
7852 | !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
7853 | return (SET_ERROR(ENOTSUP)); | |
7854 | ||
34dc7c2f | 7855 | /* |
b128c09f BB |
7856 | * If a resilver was requested, but there is no DTL on a |
7857 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 7858 | */ |
428870ff | 7859 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
7860 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
7861 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
7862 | return (0); |
7863 | } | |
7864 | ||
428870ff | 7865 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
7866 | } |
7867 | ||
7868 | /* | |
7869 | * ========================================================================== | |
7870 | * SPA async task processing | |
7871 | * ========================================================================== | |
7872 | */ | |
7873 | ||
7874 | static void | |
7875 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
7876 | { | |
b128c09f | 7877 | if (vd->vdev_remove_wanted) { |
428870ff BB |
7878 | vd->vdev_remove_wanted = B_FALSE; |
7879 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 7880 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
7881 | |
7882 | /* | |
7883 | * We want to clear the stats, but we don't want to do a full | |
7884 | * vdev_clear() as that will cause us to throw away | |
7885 | * degraded/faulted state as well as attempt to reopen the | |
7886 | * device, all of which is a waste. | |
7887 | */ | |
7888 | vd->vdev_stat.vs_read_errors = 0; | |
7889 | vd->vdev_stat.vs_write_errors = 0; | |
7890 | vd->vdev_stat.vs_checksum_errors = 0; | |
7891 | ||
b128c09f BB |
7892 | vdev_state_dirty(vd->vdev_top); |
7893 | } | |
34dc7c2f | 7894 | |
1c27024e | 7895 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
7896 | spa_async_remove(spa, vd->vdev_child[c]); |
7897 | } | |
7898 | ||
7899 | static void | |
7900 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
7901 | { | |
7902 | if (vd->vdev_probe_wanted) { | |
428870ff | 7903 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 7904 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 7905 | } |
b128c09f | 7906 | |
1c27024e | 7907 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 7908 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
7909 | } |
7910 | ||
9babb374 BB |
7911 | static void |
7912 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
7913 | { | |
9babb374 BB |
7914 | if (!spa->spa_autoexpand) |
7915 | return; | |
7916 | ||
1c27024e | 7917 | for (int c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
7918 | vdev_t *cvd = vd->vdev_child[c]; |
7919 | spa_async_autoexpand(spa, cvd); | |
7920 | } | |
7921 | ||
7922 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
7923 | return; | |
7924 | ||
12fa0466 | 7925 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND); |
9babb374 BB |
7926 | } |
7927 | ||
34dc7c2f | 7928 | static void |
c25b8f99 | 7929 | spa_async_thread(void *arg) |
34dc7c2f | 7930 | { |
c25b8f99 | 7931 | spa_t *spa = (spa_t *)arg; |
80a91e74 | 7932 | dsl_pool_t *dp = spa->spa_dsl_pool; |
867959b5 | 7933 | int tasks; |
34dc7c2f BB |
7934 | |
7935 | ASSERT(spa->spa_sync_on); | |
7936 | ||
7937 | mutex_enter(&spa->spa_async_lock); | |
7938 | tasks = spa->spa_async_tasks; | |
7939 | spa->spa_async_tasks = 0; | |
7940 | mutex_exit(&spa->spa_async_lock); | |
7941 | ||
7942 | /* | |
7943 | * See if the config needs to be updated. | |
7944 | */ | |
7945 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 7946 | uint64_t old_space, new_space; |
9babb374 | 7947 | |
34dc7c2f | 7948 | mutex_enter(&spa_namespace_lock); |
428870ff | 7949 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7950 | old_space += metaslab_class_get_space(spa_special_class(spa)); |
7951 | old_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
7952 | ||
34dc7c2f | 7953 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
cc99f275 | 7954 | |
428870ff | 7955 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7956 | new_space += metaslab_class_get_space(spa_special_class(spa)); |
7957 | new_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
34dc7c2f | 7958 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
7959 | |
7960 | /* | |
7961 | * If the pool grew as a result of the config update, | |
7962 | * then log an internal history event. | |
7963 | */ | |
428870ff | 7964 | if (new_space != old_space) { |
6f1ffb06 | 7965 | spa_history_log_internal(spa, "vdev online", NULL, |
45d1cae3 | 7966 | "pool '%s' size: %llu(+%llu)", |
74756182 MM |
7967 | spa_name(spa), (u_longlong_t)new_space, |
7968 | (u_longlong_t)(new_space - old_space)); | |
9babb374 | 7969 | } |
34dc7c2f BB |
7970 | } |
7971 | ||
7972 | /* | |
7973 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 7974 | */ |
b128c09f | 7975 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 7976 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 7977 | spa_async_remove(spa, spa->spa_root_vdev); |
867959b5 | 7978 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 7979 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
867959b5 | 7980 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
7981 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
7982 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
7983 | } |
7984 | ||
9babb374 BB |
7985 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
7986 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7987 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
7988 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7989 | } | |
7990 | ||
34dc7c2f | 7991 | /* |
b128c09f | 7992 | * See if any devices need to be probed. |
34dc7c2f | 7993 | */ |
b128c09f | 7994 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 7995 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
7996 | spa_async_probe(spa, spa->spa_root_vdev); |
7997 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
7998 | } | |
34dc7c2f BB |
7999 | |
8000 | /* | |
b128c09f | 8001 | * If any devices are done replacing, detach them. |
34dc7c2f | 8002 | */ |
b128c09f BB |
8003 | if (tasks & SPA_ASYNC_RESILVER_DONE) |
8004 | spa_vdev_resilver_done(spa); | |
34dc7c2f | 8005 | |
9a49d3f3 BB |
8006 | /* |
8007 | * If any devices are done replacing, detach them. Then if no | |
8008 | * top-level vdevs are rebuilding attempt to kick off a scrub. | |
8009 | */ | |
8010 | if (tasks & SPA_ASYNC_REBUILD_DONE) { | |
8011 | spa_vdev_resilver_done(spa); | |
8012 | ||
8013 | if (!vdev_rebuild_active(spa->spa_root_vdev)) | |
8014 | (void) dsl_scan(spa->spa_dsl_pool, POOL_SCAN_SCRUB); | |
8015 | } | |
8016 | ||
34dc7c2f BB |
8017 | /* |
8018 | * Kick off a resilver. | |
8019 | */ | |
80a91e74 | 8020 | if (tasks & SPA_ASYNC_RESILVER && |
9a49d3f3 | 8021 | !vdev_rebuild_active(spa->spa_root_vdev) && |
80a91e74 TC |
8022 | (!dsl_scan_resilvering(dp) || |
8023 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) | |
3c819a2c | 8024 | dsl_scan_restart_resilver(dp, 0); |
34dc7c2f | 8025 | |
619f0976 GW |
8026 | if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { |
8027 | mutex_enter(&spa_namespace_lock); | |
8028 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8029 | vdev_initialize_restart(spa->spa_root_vdev); | |
8030 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8031 | mutex_exit(&spa_namespace_lock); | |
8032 | } | |
8033 | ||
1b939560 BB |
8034 | if (tasks & SPA_ASYNC_TRIM_RESTART) { |
8035 | mutex_enter(&spa_namespace_lock); | |
8036 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8037 | vdev_trim_restart(spa->spa_root_vdev); | |
8038 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8039 | mutex_exit(&spa_namespace_lock); | |
8040 | } | |
8041 | ||
8042 | if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) { | |
8043 | mutex_enter(&spa_namespace_lock); | |
8044 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8045 | vdev_autotrim_restart(spa); | |
8046 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8047 | mutex_exit(&spa_namespace_lock); | |
8048 | } | |
8049 | ||
b7654bd7 GA |
8050 | /* |
8051 | * Kick off L2 cache whole device TRIM. | |
8052 | */ | |
8053 | if (tasks & SPA_ASYNC_L2CACHE_TRIM) { | |
8054 | mutex_enter(&spa_namespace_lock); | |
8055 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8056 | vdev_trim_l2arc(spa); | |
8057 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8058 | mutex_exit(&spa_namespace_lock); | |
8059 | } | |
8060 | ||
77f6826b GA |
8061 | /* |
8062 | * Kick off L2 cache rebuilding. | |
8063 | */ | |
8064 | if (tasks & SPA_ASYNC_L2CACHE_REBUILD) { | |
8065 | mutex_enter(&spa_namespace_lock); | |
8066 | spa_config_enter(spa, SCL_L2ARC, FTAG, RW_READER); | |
8067 | l2arc_spa_rebuild_start(spa); | |
8068 | spa_config_exit(spa, SCL_L2ARC, FTAG); | |
8069 | mutex_exit(&spa_namespace_lock); | |
8070 | } | |
8071 | ||
34dc7c2f BB |
8072 | /* |
8073 | * Let the world know that we're done. | |
8074 | */ | |
8075 | mutex_enter(&spa->spa_async_lock); | |
8076 | spa->spa_async_thread = NULL; | |
8077 | cv_broadcast(&spa->spa_async_cv); | |
8078 | mutex_exit(&spa->spa_async_lock); | |
8079 | thread_exit(); | |
8080 | } | |
8081 | ||
8082 | void | |
8083 | spa_async_suspend(spa_t *spa) | |
8084 | { | |
8085 | mutex_enter(&spa->spa_async_lock); | |
8086 | spa->spa_async_suspended++; | |
9d5b5245 | 8087 | while (spa->spa_async_thread != NULL) |
34dc7c2f BB |
8088 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); |
8089 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 MA |
8090 | |
8091 | spa_vdev_remove_suspend(spa); | |
9d5b5245 SD |
8092 | |
8093 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a SD |
8094 | if (condense_thread != NULL) |
8095 | zthr_cancel(condense_thread); | |
d2734cce SD |
8096 | |
8097 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a SD |
8098 | if (discard_thread != NULL) |
8099 | zthr_cancel(discard_thread); | |
37f03da8 SH |
8100 | |
8101 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8102 | if (ll_delete_thread != NULL) | |
8103 | zthr_cancel(ll_delete_thread); | |
8104 | ||
8105 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8106 | if (ll_condense_thread != NULL) | |
8107 | zthr_cancel(ll_condense_thread); | |
34dc7c2f BB |
8108 | } |
8109 | ||
8110 | void | |
8111 | spa_async_resume(spa_t *spa) | |
8112 | { | |
8113 | mutex_enter(&spa->spa_async_lock); | |
8114 | ASSERT(spa->spa_async_suspended != 0); | |
8115 | spa->spa_async_suspended--; | |
8116 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 | 8117 | spa_restart_removal(spa); |
9d5b5245 SD |
8118 | |
8119 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a | 8120 | if (condense_thread != NULL) |
9d5b5245 | 8121 | zthr_resume(condense_thread); |
d2734cce SD |
8122 | |
8123 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a | 8124 | if (discard_thread != NULL) |
d2734cce | 8125 | zthr_resume(discard_thread); |
37f03da8 SH |
8126 | |
8127 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8128 | if (ll_delete_thread != NULL) | |
8129 | zthr_resume(ll_delete_thread); | |
8130 | ||
8131 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8132 | if (ll_condense_thread != NULL) | |
8133 | zthr_resume(ll_condense_thread); | |
34dc7c2f BB |
8134 | } |
8135 | ||
e6cfd633 WA |
8136 | static boolean_t |
8137 | spa_async_tasks_pending(spa_t *spa) | |
8138 | { | |
8139 | uint_t non_config_tasks; | |
8140 | uint_t config_task; | |
8141 | boolean_t config_task_suspended; | |
8142 | ||
8143 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
8144 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
8145 | if (spa->spa_ccw_fail_time == 0) { | |
8146 | config_task_suspended = B_FALSE; | |
8147 | } else { | |
8148 | config_task_suspended = | |
8149 | (gethrtime() - spa->spa_ccw_fail_time) < | |
05852b34 | 8150 | ((hrtime_t)zfs_ccw_retry_interval * NANOSEC); |
e6cfd633 WA |
8151 | } |
8152 | ||
8153 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
8154 | } | |
8155 | ||
34dc7c2f BB |
8156 | static void |
8157 | spa_async_dispatch(spa_t *spa) | |
8158 | { | |
8159 | mutex_enter(&spa->spa_async_lock); | |
e6cfd633 WA |
8160 | if (spa_async_tasks_pending(spa) && |
8161 | !spa->spa_async_suspended && | |
da92d5cb | 8162 | spa->spa_async_thread == NULL) |
34dc7c2f BB |
8163 | spa->spa_async_thread = thread_create(NULL, 0, |
8164 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
8165 | mutex_exit(&spa->spa_async_lock); | |
8166 | } | |
8167 | ||
8168 | void | |
8169 | spa_async_request(spa_t *spa, int task) | |
8170 | { | |
428870ff | 8171 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
8172 | mutex_enter(&spa->spa_async_lock); |
8173 | spa->spa_async_tasks |= task; | |
8174 | mutex_exit(&spa->spa_async_lock); | |
8175 | } | |
8176 | ||
3c819a2c JP |
8177 | int |
8178 | spa_async_tasks(spa_t *spa) | |
8179 | { | |
8180 | return (spa->spa_async_tasks); | |
8181 | } | |
8182 | ||
34dc7c2f BB |
8183 | /* |
8184 | * ========================================================================== | |
8185 | * SPA syncing routines | |
8186 | * ========================================================================== | |
8187 | */ | |
8188 | ||
37f03da8 | 8189 | |
428870ff | 8190 | static int |
37f03da8 SH |
8191 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, |
8192 | dmu_tx_t *tx) | |
34dc7c2f | 8193 | { |
428870ff | 8194 | bpobj_t *bpo = arg; |
37f03da8 | 8195 | bpobj_enqueue(bpo, bp, bp_freed, tx); |
428870ff BB |
8196 | return (0); |
8197 | } | |
34dc7c2f | 8198 | |
37f03da8 SH |
8199 | int |
8200 | bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8201 | { | |
8202 | return (bpobj_enqueue_cb(arg, bp, B_FALSE, tx)); | |
8203 | } | |
8204 | ||
8205 | int | |
8206 | bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8207 | { | |
8208 | return (bpobj_enqueue_cb(arg, bp, B_TRUE, tx)); | |
8209 | } | |
8210 | ||
428870ff BB |
8211 | static int |
8212 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8213 | { | |
9cdf7b1f | 8214 | zio_t *pio = arg; |
34dc7c2f | 8215 | |
9cdf7b1f MA |
8216 | zio_nowait(zio_free_sync(pio, pio->io_spa, dmu_tx_get_txg(tx), bp, |
8217 | pio->io_flags)); | |
428870ff | 8218 | return (0); |
34dc7c2f BB |
8219 | } |
8220 | ||
37f03da8 SH |
8221 | static int |
8222 | bpobj_spa_free_sync_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
8223 | dmu_tx_t *tx) | |
8224 | { | |
8225 | ASSERT(!bp_freed); | |
8226 | return (spa_free_sync_cb(arg, bp, tx)); | |
8227 | } | |
8228 | ||
e8b96c60 MA |
8229 | /* |
8230 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8231 | * amount of time spent syncing frees. | |
8232 | */ | |
8233 | static void | |
8234 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
8235 | { | |
8236 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
8237 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
8238 | VERIFY(zio_wait(zio) == 0); | |
8239 | } | |
8240 | ||
8241 | /* | |
8242 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8243 | * amount of time spent syncing deferred frees. | |
8244 | */ | |
8245 | static void | |
8246 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
8247 | { | |
8dc2197b SD |
8248 | if (spa_sync_pass(spa) != 1) |
8249 | return; | |
8250 | ||
93e28d66 SD |
8251 | /* |
8252 | * Note: | |
8253 | * If the log space map feature is active, we stop deferring | |
8254 | * frees to the next TXG and therefore running this function | |
8255 | * would be considered a no-op as spa_deferred_bpobj should | |
8256 | * not have any entries. | |
8257 | * | |
8258 | * That said we run this function anyway (instead of returning | |
8259 | * immediately) for the edge-case scenario where we just | |
8260 | * activated the log space map feature in this TXG but we have | |
8261 | * deferred frees from the previous TXG. | |
8262 | */ | |
e8b96c60 MA |
8263 | zio_t *zio = zio_root(spa, NULL, NULL, 0); |
8264 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
37f03da8 | 8265 | bpobj_spa_free_sync_cb, zio, tx), ==, 0); |
e8b96c60 MA |
8266 | VERIFY0(zio_wait(zio)); |
8267 | } | |
8268 | ||
34dc7c2f BB |
8269 | static void |
8270 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
8271 | { | |
8272 | char *packed = NULL; | |
b128c09f | 8273 | size_t bufsize; |
34dc7c2f BB |
8274 | size_t nvsize = 0; |
8275 | dmu_buf_t *db; | |
8276 | ||
8277 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
8278 | ||
b128c09f BB |
8279 | /* |
8280 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
b0bc7a84 | 8281 | * information. This avoids the dmu_buf_will_dirty() path and |
b128c09f BB |
8282 | * saves us a pre-read to get data we don't actually care about. |
8283 | */ | |
9ae529ec | 8284 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
79c76d5b | 8285 | packed = vmem_alloc(bufsize, KM_SLEEP); |
34dc7c2f BB |
8286 | |
8287 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
79c76d5b | 8288 | KM_SLEEP) == 0); |
b128c09f | 8289 | bzero(packed + nvsize, bufsize - nvsize); |
34dc7c2f | 8290 | |
b128c09f | 8291 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 8292 | |
00b46022 | 8293 | vmem_free(packed, bufsize); |
34dc7c2f BB |
8294 | |
8295 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
8296 | dmu_buf_will_dirty(db, tx); | |
8297 | *(uint64_t *)db->db_data = nvsize; | |
8298 | dmu_buf_rele(db, FTAG); | |
8299 | } | |
8300 | ||
8301 | static void | |
8302 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
8303 | const char *config, const char *entry) | |
8304 | { | |
8305 | nvlist_t *nvroot; | |
8306 | nvlist_t **list; | |
8307 | int i; | |
8308 | ||
8309 | if (!sav->sav_sync) | |
8310 | return; | |
8311 | ||
8312 | /* | |
8313 | * Update the MOS nvlist describing the list of available devices. | |
8314 | * spa_validate_aux() will have already made sure this nvlist is | |
8315 | * valid and the vdevs are labeled appropriately. | |
8316 | */ | |
8317 | if (sav->sav_object == 0) { | |
8318 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
8319 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
8320 | sizeof (uint64_t), tx); | |
8321 | VERIFY(zap_update(spa->spa_meta_objset, | |
8322 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
8323 | &sav->sav_object, tx) == 0); | |
8324 | } | |
8325 | ||
79c76d5b | 8326 | VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
8327 | if (sav->sav_count == 0) { |
8328 | VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); | |
8329 | } else { | |
79c76d5b | 8330 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); |
34dc7c2f BB |
8331 | for (i = 0; i < sav->sav_count; i++) |
8332 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 8333 | B_FALSE, VDEV_CONFIG_L2CACHE); |
34dc7c2f BB |
8334 | VERIFY(nvlist_add_nvlist_array(nvroot, config, list, |
8335 | sav->sav_count) == 0); | |
8336 | for (i = 0; i < sav->sav_count; i++) | |
8337 | nvlist_free(list[i]); | |
8338 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
8339 | } | |
8340 | ||
8341 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
8342 | nvlist_free(nvroot); | |
8343 | ||
8344 | sav->sav_sync = B_FALSE; | |
8345 | } | |
8346 | ||
e0ab3ab5 JS |
8347 | /* |
8348 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
8349 | * The all-vdev ZAP must be empty. | |
8350 | */ | |
8351 | static void | |
8352 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
8353 | { | |
8354 | spa_t *spa = vd->vdev_spa; | |
e0ab3ab5 JS |
8355 | |
8356 | if (vd->vdev_top_zap != 0) { | |
8357 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8358 | vd->vdev_top_zap, tx)); | |
8359 | } | |
8360 | if (vd->vdev_leaf_zap != 0) { | |
8361 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8362 | vd->vdev_leaf_zap, tx)); | |
8363 | } | |
1c27024e | 8364 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
8365 | spa_avz_build(vd->vdev_child[i], avz, tx); |
8366 | } | |
8367 | } | |
8368 | ||
34dc7c2f BB |
8369 | static void |
8370 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
8371 | { | |
8372 | nvlist_t *config; | |
8373 | ||
e0ab3ab5 JS |
8374 | /* |
8375 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
8376 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
8377 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
8378 | * need to rebuild the AVZ although the config may not be dirty. | |
8379 | */ | |
8380 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
8381 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
34dc7c2f BB |
8382 | return; |
8383 | ||
b128c09f BB |
8384 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
8385 | ||
e0ab3ab5 | 8386 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || |
38640550 | 8387 | spa->spa_avz_action == AVZ_ACTION_INITIALIZE || |
e0ab3ab5 JS |
8388 | spa->spa_all_vdev_zaps != 0); |
8389 | ||
8390 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
e0ab3ab5 JS |
8391 | /* Make and build the new AVZ */ |
8392 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
8393 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
8394 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
8395 | ||
8396 | /* Diff old AVZ with new one */ | |
1c27024e DB |
8397 | zap_cursor_t zc; |
8398 | zap_attribute_t za; | |
8399 | ||
e0ab3ab5 JS |
8400 | for (zap_cursor_init(&zc, spa->spa_meta_objset, |
8401 | spa->spa_all_vdev_zaps); | |
8402 | zap_cursor_retrieve(&zc, &za) == 0; | |
8403 | zap_cursor_advance(&zc)) { | |
8404 | uint64_t vdzap = za.za_first_integer; | |
8405 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
8406 | vdzap) == ENOENT) { | |
8407 | /* | |
8408 | * ZAP is listed in old AVZ but not in new one; | |
8409 | * destroy it | |
8410 | */ | |
8411 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
8412 | tx)); | |
8413 | } | |
8414 | } | |
8415 | ||
8416 | zap_cursor_fini(&zc); | |
8417 | ||
8418 | /* Destroy the old AVZ */ | |
8419 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8420 | spa->spa_all_vdev_zaps, tx)); | |
8421 | ||
8422 | /* Replace the old AVZ in the dir obj with the new one */ | |
8423 | VERIFY0(zap_update(spa->spa_meta_objset, | |
8424 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
8425 | sizeof (new_avz), 1, &new_avz, tx)); | |
8426 | ||
8427 | spa->spa_all_vdev_zaps = new_avz; | |
8428 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
8429 | zap_cursor_t zc; | |
8430 | zap_attribute_t za; | |
8431 | ||
8432 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
8433 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
8434 | spa->spa_all_vdev_zaps); | |
8435 | zap_cursor_retrieve(&zc, &za) == 0; | |
8436 | zap_cursor_advance(&zc)) { | |
8437 | uint64_t zap = za.za_first_integer; | |
8438 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
8439 | } | |
8440 | ||
8441 | zap_cursor_fini(&zc); | |
8442 | ||
8443 | /* Destroy and unlink the AVZ itself */ | |
8444 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8445 | spa->spa_all_vdev_zaps, tx)); | |
8446 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
8447 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
8448 | spa->spa_all_vdev_zaps = 0; | |
8449 | } | |
8450 | ||
8451 | if (spa->spa_all_vdev_zaps == 0) { | |
8452 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
8453 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
8454 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
8455 | } | |
8456 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
8457 | ||
8458 | /* Create ZAPs for vdevs that don't have them. */ | |
8459 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
8460 | ||
b128c09f BB |
8461 | config = spa_config_generate(spa, spa->spa_root_vdev, |
8462 | dmu_tx_get_txg(tx), B_FALSE); | |
8463 | ||
ea0b2538 GW |
8464 | /* |
8465 | * If we're upgrading the spa version then make sure that | |
8466 | * the config object gets updated with the correct version. | |
8467 | */ | |
8468 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
8469 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
8470 | spa->spa_uberblock.ub_version); | |
8471 | ||
b128c09f | 8472 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f | 8473 | |
8a5fc748 | 8474 | nvlist_free(spa->spa_config_syncing); |
34dc7c2f BB |
8475 | spa->spa_config_syncing = config; |
8476 | ||
8477 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
8478 | } | |
8479 | ||
9ae529ec | 8480 | static void |
13fe0198 | 8481 | spa_sync_version(void *arg, dmu_tx_t *tx) |
9ae529ec | 8482 | { |
13fe0198 MA |
8483 | uint64_t *versionp = arg; |
8484 | uint64_t version = *versionp; | |
8485 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
9ae529ec CS |
8486 | |
8487 | /* | |
8488 | * Setting the version is special cased when first creating the pool. | |
8489 | */ | |
8490 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
8491 | ||
8dca0a9a | 8492 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
8493 | ASSERT(version >= spa_version(spa)); |
8494 | ||
8495 | spa->spa_uberblock.ub_version = version; | |
8496 | vdev_config_dirty(spa->spa_root_vdev); | |
74756182 MM |
8497 | spa_history_log_internal(spa, "set", tx, "version=%lld", |
8498 | (longlong_t)version); | |
9ae529ec CS |
8499 | } |
8500 | ||
34dc7c2f BB |
8501 | /* |
8502 | * Set zpool properties. | |
8503 | */ | |
8504 | static void | |
13fe0198 | 8505 | spa_sync_props(void *arg, dmu_tx_t *tx) |
34dc7c2f | 8506 | { |
13fe0198 MA |
8507 | nvlist_t *nvp = arg; |
8508 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f | 8509 | objset_t *mos = spa->spa_meta_objset; |
9ae529ec | 8510 | nvpair_t *elem = NULL; |
b128c09f BB |
8511 | |
8512 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 8513 | |
34dc7c2f | 8514 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec CS |
8515 | uint64_t intval; |
8516 | char *strval, *fname; | |
8517 | zpool_prop_t prop; | |
8518 | const char *propname; | |
8519 | zprop_type_t proptype; | |
fa86b5db | 8520 | spa_feature_t fid; |
9ae529ec | 8521 | |
31864e3d BB |
8522 | switch (prop = zpool_name_to_prop(nvpair_name(elem))) { |
8523 | case ZPOOL_PROP_INVAL: | |
9ae529ec CS |
8524 | /* |
8525 | * We checked this earlier in spa_prop_validate(). | |
8526 | */ | |
8527 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
8528 | ||
8529 | fname = strchr(nvpair_name(elem), '@') + 1; | |
fa86b5db | 8530 | VERIFY0(zfeature_lookup_name(fname, &fid)); |
9ae529ec | 8531 | |
fa86b5db | 8532 | spa_feature_enable(spa, fid, tx); |
6f1ffb06 MA |
8533 | spa_history_log_internal(spa, "set", tx, |
8534 | "%s=enabled", nvpair_name(elem)); | |
9ae529ec CS |
8535 | break; |
8536 | ||
34dc7c2f | 8537 | case ZPOOL_PROP_VERSION: |
93cf2076 | 8538 | intval = fnvpair_value_uint64(elem); |
34dc7c2f | 8539 | /* |
4e33ba4c | 8540 | * The version is synced separately before other |
9ae529ec | 8541 | * properties and should be correct by now. |
34dc7c2f | 8542 | */ |
9ae529ec | 8543 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
8544 | break; |
8545 | ||
8546 | case ZPOOL_PROP_ALTROOT: | |
8547 | /* | |
8548 | * 'altroot' is a non-persistent property. It should | |
8549 | * have been set temporarily at creation or import time. | |
8550 | */ | |
8551 | ASSERT(spa->spa_root != NULL); | |
8552 | break; | |
8553 | ||
572e2857 | 8554 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
8555 | case ZPOOL_PROP_CACHEFILE: |
8556 | /* | |
e1cfd73f | 8557 | * 'readonly' and 'cachefile' are also non-persistent |
572e2857 | 8558 | * properties. |
34dc7c2f | 8559 | */ |
34dc7c2f | 8560 | break; |
d96eb2b1 | 8561 | case ZPOOL_PROP_COMMENT: |
93cf2076 | 8562 | strval = fnvpair_value_string(elem); |
d96eb2b1 DM |
8563 | if (spa->spa_comment != NULL) |
8564 | spa_strfree(spa->spa_comment); | |
8565 | spa->spa_comment = spa_strdup(strval); | |
8566 | /* | |
8567 | * We need to dirty the configuration on all the vdevs | |
8568 | * so that their labels get updated. It's unnecessary | |
8569 | * to do this for pool creation since the vdev's | |
4e33ba4c | 8570 | * configuration has already been dirtied. |
d96eb2b1 DM |
8571 | */ |
8572 | if (tx->tx_txg != TXG_INITIAL) | |
8573 | vdev_config_dirty(spa->spa_root_vdev); | |
6f1ffb06 MA |
8574 | spa_history_log_internal(spa, "set", tx, |
8575 | "%s=%s", nvpair_name(elem), strval); | |
d96eb2b1 | 8576 | break; |
34dc7c2f BB |
8577 | default: |
8578 | /* | |
8579 | * Set pool property values in the poolprops mos object. | |
8580 | */ | |
34dc7c2f | 8581 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
8582 | spa->spa_pool_props_object = |
8583 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 8584 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 8585 | tx); |
34dc7c2f | 8586 | } |
34dc7c2f BB |
8587 | |
8588 | /* normalize the property name */ | |
8589 | propname = zpool_prop_to_name(prop); | |
8590 | proptype = zpool_prop_get_type(prop); | |
8591 | ||
8592 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
8593 | ASSERT(proptype == PROP_TYPE_STRING); | |
93cf2076 GW |
8594 | strval = fnvpair_value_string(elem); |
8595 | VERIFY0(zap_update(mos, | |
34dc7c2f | 8596 | spa->spa_pool_props_object, propname, |
93cf2076 | 8597 | 1, strlen(strval) + 1, strval, tx)); |
6f1ffb06 MA |
8598 | spa_history_log_internal(spa, "set", tx, |
8599 | "%s=%s", nvpair_name(elem), strval); | |
34dc7c2f | 8600 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { |
93cf2076 | 8601 | intval = fnvpair_value_uint64(elem); |
34dc7c2f BB |
8602 | |
8603 | if (proptype == PROP_TYPE_INDEX) { | |
8604 | const char *unused; | |
93cf2076 GW |
8605 | VERIFY0(zpool_prop_index_to_string( |
8606 | prop, intval, &unused)); | |
34dc7c2f | 8607 | } |
93cf2076 | 8608 | VERIFY0(zap_update(mos, |
34dc7c2f | 8609 | spa->spa_pool_props_object, propname, |
93cf2076 | 8610 | 8, 1, &intval, tx)); |
6f1ffb06 | 8611 | spa_history_log_internal(spa, "set", tx, |
74756182 MM |
8612 | "%s=%lld", nvpair_name(elem), |
8613 | (longlong_t)intval); | |
34dc7c2f BB |
8614 | } else { |
8615 | ASSERT(0); /* not allowed */ | |
8616 | } | |
8617 | ||
8618 | switch (prop) { | |
8619 | case ZPOOL_PROP_DELEGATION: | |
8620 | spa->spa_delegation = intval; | |
8621 | break; | |
8622 | case ZPOOL_PROP_BOOTFS: | |
8623 | spa->spa_bootfs = intval; | |
8624 | break; | |
8625 | case ZPOOL_PROP_FAILUREMODE: | |
8626 | spa->spa_failmode = intval; | |
8627 | break; | |
1b939560 BB |
8628 | case ZPOOL_PROP_AUTOTRIM: |
8629 | spa->spa_autotrim = intval; | |
8630 | spa_async_request(spa, | |
8631 | SPA_ASYNC_AUTOTRIM_RESTART); | |
8632 | break; | |
9babb374 BB |
8633 | case ZPOOL_PROP_AUTOEXPAND: |
8634 | spa->spa_autoexpand = intval; | |
428870ff BB |
8635 | if (tx->tx_txg != TXG_INITIAL) |
8636 | spa_async_request(spa, | |
8637 | SPA_ASYNC_AUTOEXPAND); | |
8638 | break; | |
379ca9cf OF |
8639 | case ZPOOL_PROP_MULTIHOST: |
8640 | spa->spa_multihost = intval; | |
8641 | break; | |
34dc7c2f BB |
8642 | default: |
8643 | break; | |
8644 | } | |
8645 | } | |
8646 | ||
34dc7c2f | 8647 | } |
b128c09f BB |
8648 | |
8649 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
8650 | } |
8651 | ||
428870ff BB |
8652 | /* |
8653 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
8654 | * reflect the new version this txg, so there must be no changes this | |
8655 | * txg to anything that the upgrade code depends on after it executes. | |
8656 | * Therefore this must be called after dsl_pool_sync() does the sync | |
8657 | * tasks. | |
8658 | */ | |
8659 | static void | |
8660 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
8661 | { | |
8dc2197b SD |
8662 | if (spa_sync_pass(spa) != 1) |
8663 | return; | |
428870ff | 8664 | |
8dc2197b | 8665 | dsl_pool_t *dp = spa->spa_dsl_pool; |
13fe0198 MA |
8666 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
8667 | ||
428870ff BB |
8668 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && |
8669 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
8670 | dsl_pool_create_origin(dp, tx); | |
8671 | ||
8672 | /* Keeping the origin open increases spa_minref */ | |
8673 | spa->spa_minref += 3; | |
8674 | } | |
8675 | ||
8676 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
8677 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
8678 | dsl_pool_upgrade_clones(dp, tx); | |
8679 | } | |
8680 | ||
8681 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
8682 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
8683 | dsl_pool_upgrade_dir_clones(dp, tx); | |
8684 | ||
8685 | /* Keeping the freedir open increases spa_minref */ | |
8686 | spa->spa_minref += 3; | |
8687 | } | |
9ae529ec CS |
8688 | |
8689 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
8690 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8691 | spa_feature_create_zap_objects(spa, tx); | |
8692 | } | |
62bdd5eb DL |
8693 | |
8694 | /* | |
8695 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
8696 | * when possibility to use lz4 compression for metadata was added | |
8697 | * Old pools that have this feature enabled must be upgraded to have | |
8698 | * this feature active | |
8699 | */ | |
8700 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8701 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
8702 | SPA_FEATURE_LZ4_COMPRESS); | |
8703 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
8704 | SPA_FEATURE_LZ4_COMPRESS); | |
8705 | ||
8706 | if (lz4_en && !lz4_ac) | |
8707 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
8708 | } | |
3c67d83a TH |
8709 | |
8710 | /* | |
8711 | * If we haven't written the salt, do so now. Note that the | |
8712 | * feature may not be activated yet, but that's fine since | |
8713 | * the presence of this ZAP entry is backwards compatible. | |
8714 | */ | |
8715 | if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
8716 | DMU_POOL_CHECKSUM_SALT) == ENOENT) { | |
8717 | VERIFY0(zap_add(spa->spa_meta_objset, | |
8718 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, | |
8719 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
8720 | spa->spa_cksum_salt.zcs_bytes, tx)); | |
8721 | } | |
8722 | ||
13fe0198 | 8723 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
428870ff BB |
8724 | } |
8725 | ||
a1d477c2 MA |
8726 | static void |
8727 | vdev_indirect_state_sync_verify(vdev_t *vd) | |
8728 | { | |
2a8ba608 MM |
8729 | vdev_indirect_mapping_t *vim __maybe_unused = vd->vdev_indirect_mapping; |
8730 | vdev_indirect_births_t *vib __maybe_unused = vd->vdev_indirect_births; | |
a1d477c2 MA |
8731 | |
8732 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
8733 | ASSERT(vim != NULL); | |
8734 | ASSERT(vib != NULL); | |
8735 | } | |
8736 | ||
27f80e85 BB |
8737 | uint64_t obsolete_sm_object = 0; |
8738 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
8739 | if (obsolete_sm_object != 0) { | |
a1d477c2 MA |
8740 | ASSERT(vd->vdev_obsolete_sm != NULL); |
8741 | ASSERT(vd->vdev_removing || | |
8742 | vd->vdev_ops == &vdev_indirect_ops); | |
8743 | ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); | |
8744 | ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); | |
27f80e85 | 8745 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
8746 | space_map_object(vd->vdev_obsolete_sm)); |
8747 | ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, | |
8748 | space_map_allocated(vd->vdev_obsolete_sm)); | |
8749 | } | |
8750 | ASSERT(vd->vdev_obsolete_segments != NULL); | |
8751 | ||
8752 | /* | |
8753 | * Since frees / remaps to an indirect vdev can only | |
8754 | * happen in syncing context, the obsolete segments | |
8755 | * tree must be empty when we start syncing. | |
8756 | */ | |
8757 | ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); | |
8758 | } | |
8759 | ||
34dc7c2f | 8760 | /* |
8dc2197b SD |
8761 | * Set the top-level vdev's max queue depth. Evaluate each top-level's |
8762 | * async write queue depth in case it changed. The max queue depth will | |
8763 | * not change in the middle of syncing out this txg. | |
34dc7c2f | 8764 | */ |
8dc2197b SD |
8765 | static void |
8766 | spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) | |
34dc7c2f | 8767 | { |
8dc2197b SD |
8768 | ASSERT(spa_writeable(spa)); |
8769 | ||
34dc7c2f | 8770 | vdev_t *rvd = spa->spa_root_vdev; |
3dfb57a3 DB |
8771 | uint32_t max_queue_depth = zfs_vdev_async_write_max_active * |
8772 | zfs_vdev_queue_depth_pct / 100; | |
8dc2197b SD |
8773 | metaslab_class_t *normal = spa_normal_class(spa); |
8774 | metaslab_class_t *special = spa_special_class(spa); | |
8775 | metaslab_class_t *dedup = spa_dedup_class(spa); | |
34dc7c2f | 8776 | |
492f64e9 | 8777 | uint64_t slots_per_allocator = 0; |
1c27024e | 8778 | for (int c = 0; c < rvd->vdev_children; c++) { |
3dfb57a3 | 8779 | vdev_t *tvd = rvd->vdev_child[c]; |
cc99f275 | 8780 | |
8dc2197b | 8781 | metaslab_group_t *mg = tvd->vdev_mg; |
cc99f275 DB |
8782 | if (mg == NULL || !metaslab_group_initialized(mg)) |
8783 | continue; | |
3dfb57a3 | 8784 | |
8dc2197b | 8785 | metaslab_class_t *mc = mg->mg_class; |
cc99f275 | 8786 | if (mc != normal && mc != special && mc != dedup) |
3dfb57a3 DB |
8787 | continue; |
8788 | ||
8789 | /* | |
8790 | * It is safe to do a lock-free check here because only async | |
8791 | * allocations look at mg_max_alloc_queue_depth, and async | |
8792 | * allocations all happen from spa_sync(). | |
8793 | */ | |
32d805c3 | 8794 | for (int i = 0; i < mg->mg_allocators; i++) { |
424fd7c3 | 8795 | ASSERT0(zfs_refcount_count( |
32d805c3 MA |
8796 | &(mg->mg_allocator[i].mga_alloc_queue_depth))); |
8797 | } | |
3dfb57a3 | 8798 | mg->mg_max_alloc_queue_depth = max_queue_depth; |
492f64e9 | 8799 | |
32d805c3 MA |
8800 | for (int i = 0; i < mg->mg_allocators; i++) { |
8801 | mg->mg_allocator[i].mga_cur_max_alloc_queue_depth = | |
492f64e9 PD |
8802 | zfs_vdev_def_queue_depth; |
8803 | } | |
8804 | slots_per_allocator += zfs_vdev_def_queue_depth; | |
3dfb57a3 | 8805 | } |
cc99f275 | 8806 | |
492f64e9 | 8807 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
424fd7c3 TS |
8808 | ASSERT0(zfs_refcount_count(&normal->mc_alloc_slots[i])); |
8809 | ASSERT0(zfs_refcount_count(&special->mc_alloc_slots[i])); | |
8810 | ASSERT0(zfs_refcount_count(&dedup->mc_alloc_slots[i])); | |
cc99f275 DB |
8811 | normal->mc_alloc_max_slots[i] = slots_per_allocator; |
8812 | special->mc_alloc_max_slots[i] = slots_per_allocator; | |
8813 | dedup->mc_alloc_max_slots[i] = slots_per_allocator; | |
8814 | } | |
8815 | normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8816 | special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8817 | dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8dc2197b SD |
8818 | } |
8819 | ||
8820 | static void | |
8821 | spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) | |
8822 | { | |
8823 | ASSERT(spa_writeable(spa)); | |
3dfb57a3 | 8824 | |
8dc2197b | 8825 | vdev_t *rvd = spa->spa_root_vdev; |
a1d477c2 MA |
8826 | for (int c = 0; c < rvd->vdev_children; c++) { |
8827 | vdev_t *vd = rvd->vdev_child[c]; | |
8828 | vdev_indirect_state_sync_verify(vd); | |
8829 | ||
8830 | if (vdev_indirect_should_condense(vd)) { | |
8831 | spa_condense_indirect_start_sync(vd, tx); | |
8832 | break; | |
8833 | } | |
8834 | } | |
8dc2197b SD |
8835 | } |
8836 | ||
8837 | static void | |
8838 | spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) | |
8839 | { | |
8840 | objset_t *mos = spa->spa_meta_objset; | |
8841 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
8842 | uint64_t txg = tx->tx_txg; | |
8843 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
a1d477c2 | 8844 | |
34dc7c2f | 8845 | do { |
428870ff | 8846 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
8847 | |
8848 | spa_sync_config_object(spa, tx); | |
8849 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
8850 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
8851 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
8852 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
8853 | spa_errlog_sync(spa, txg); | |
8854 | dsl_pool_sync(dp, txg); | |
8855 | ||
93e28d66 SD |
8856 | if (pass < zfs_sync_pass_deferred_free || |
8857 | spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) { | |
8858 | /* | |
8859 | * If the log space map feature is active we don't | |
8860 | * care about deferred frees and the deferred bpobj | |
8861 | * as the log space map should effectively have the | |
8862 | * same results (i.e. appending only to one object). | |
8863 | */ | |
e8b96c60 | 8864 | spa_sync_frees(spa, free_bpl, tx); |
428870ff | 8865 | } else { |
905edb40 MA |
8866 | /* |
8867 | * We can not defer frees in pass 1, because | |
8868 | * we sync the deferred frees later in pass 1. | |
8869 | */ | |
8870 | ASSERT3U(pass, >, 1); | |
37f03da8 | 8871 | bplist_iterate(free_bpl, bpobj_enqueue_alloc_cb, |
e8b96c60 | 8872 | &spa->spa_deferred_bpobj, tx); |
34dc7c2f BB |
8873 | } |
8874 | ||
428870ff BB |
8875 | ddt_sync(spa, txg); |
8876 | dsl_scan_sync(dp, tx); | |
8dc2197b SD |
8877 | svr_sync(spa, tx); |
8878 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 8879 | |
93e28d66 SD |
8880 | spa_flush_metaslabs(spa, tx); |
8881 | ||
8dc2197b | 8882 | vdev_t *vd = NULL; |
a1d477c2 MA |
8883 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) |
8884 | != NULL) | |
428870ff BB |
8885 | vdev_sync(vd, txg); |
8886 | ||
8dc2197b SD |
8887 | /* |
8888 | * Note: We need to check if the MOS is dirty because we could | |
8889 | * have marked the MOS dirty without updating the uberblock | |
8890 | * (e.g. if we have sync tasks but no dirty user data). We need | |
8891 | * to check the uberblock's rootbp because it is updated if we | |
8892 | * have synced out dirty data (though in this case the MOS will | |
8893 | * most likely also be dirty due to second order effects, we | |
8894 | * don't want to rely on that here). | |
8895 | */ | |
8896 | if (pass == 1 && | |
8897 | spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
8898 | !dmu_objset_is_dirty(mos, txg)) { | |
905edb40 | 8899 | /* |
8dc2197b SD |
8900 | * Nothing changed on the first pass, therefore this |
8901 | * TXG is a no-op. Avoid syncing deferred frees, so | |
8902 | * that we can keep this TXG as a no-op. | |
905edb40 | 8903 | */ |
8dc2197b SD |
8904 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); |
8905 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
8906 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
8907 | ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); | |
8908 | break; | |
905edb40 | 8909 | } |
34dc7c2f | 8910 | |
8dc2197b | 8911 | spa_sync_deferred_frees(spa, tx); |
428870ff | 8912 | } while (dmu_objset_is_dirty(mos, txg)); |
8dc2197b | 8913 | } |
34dc7c2f | 8914 | |
8dc2197b SD |
8915 | /* |
8916 | * Rewrite the vdev configuration (which includes the uberblock) to | |
8917 | * commit the transaction group. | |
8918 | * | |
8919 | * If there are no dirty vdevs, we sync the uberblock to a few random | |
8920 | * top-level vdevs that are known to be visible in the config cache | |
8921 | * (see spa_vdev_add() for a complete description). If there *are* dirty | |
8922 | * vdevs, sync the uberblock to all vdevs. | |
8923 | */ | |
8924 | static void | |
8925 | spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) | |
8926 | { | |
8927 | vdev_t *rvd = spa->spa_root_vdev; | |
8928 | uint64_t txg = tx->tx_txg; | |
a1d477c2 | 8929 | |
b128c09f | 8930 | for (;;) { |
8dc2197b SD |
8931 | int error = 0; |
8932 | ||
b128c09f BB |
8933 | /* |
8934 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
8935 | * while we're attempting to write the vdev labels. | |
8936 | */ | |
8937 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
8938 | ||
8939 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
d2734cce | 8940 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; |
b128c09f BB |
8941 | int svdcount = 0; |
8942 | int children = rvd->vdev_children; | |
8943 | int c0 = spa_get_random(children); | |
b128c09f | 8944 | |
1c27024e | 8945 | for (int c = 0; c < children; c++) { |
8dc2197b SD |
8946 | vdev_t *vd = |
8947 | rvd->vdev_child[(c0 + c) % children]; | |
d2734cce SD |
8948 | |
8949 | /* Stop when revisiting the first vdev */ | |
8950 | if (c > 0 && svd[0] == vd) | |
8951 | break; | |
8952 | ||
8dc2197b SD |
8953 | if (vd->vdev_ms_array == 0 || |
8954 | vd->vdev_islog || | |
a1d477c2 | 8955 | !vdev_is_concrete(vd)) |
b128c09f | 8956 | continue; |
d2734cce | 8957 | |
b128c09f | 8958 | svd[svdcount++] = vd; |
6cb8e530 | 8959 | if (svdcount == SPA_SYNC_MIN_VDEVS) |
b128c09f BB |
8960 | break; |
8961 | } | |
b6fcb792 | 8962 | error = vdev_config_sync(svd, svdcount, txg); |
b128c09f BB |
8963 | } else { |
8964 | error = vdev_config_sync(rvd->vdev_child, | |
b6fcb792 | 8965 | rvd->vdev_children, txg); |
34dc7c2f | 8966 | } |
34dc7c2f | 8967 | |
3bc7e0fb GW |
8968 | if (error == 0) |
8969 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
8970 | ||
b128c09f BB |
8971 | spa_config_exit(spa, SCL_STATE, FTAG); |
8972 | ||
8973 | if (error == 0) | |
8974 | break; | |
cec3a0a1 | 8975 | zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); |
b128c09f BB |
8976 | zio_resume_wait(spa); |
8977 | } | |
8dc2197b SD |
8978 | } |
8979 | ||
8980 | /* | |
8981 | * Sync the specified transaction group. New blocks may be dirtied as | |
8982 | * part of the process, so we iterate until it converges. | |
8983 | */ | |
8984 | void | |
8985 | spa_sync(spa_t *spa, uint64_t txg) | |
8986 | { | |
8987 | vdev_t *vd = NULL; | |
8988 | ||
8989 | VERIFY(spa_writeable(spa)); | |
8990 | ||
8991 | /* | |
8992 | * Wait for i/os issued in open context that need to complete | |
8993 | * before this txg syncs. | |
8994 | */ | |
8995 | (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); | |
8996 | spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, | |
8997 | ZIO_FLAG_CANFAIL); | |
8998 | ||
8999 | /* | |
9000 | * Lock out configuration changes. | |
9001 | */ | |
9002 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
9003 | ||
9004 | spa->spa_syncing_txg = txg; | |
9005 | spa->spa_sync_pass = 0; | |
9006 | ||
9007 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
9008 | mutex_enter(&spa->spa_alloc_locks[i]); | |
9009 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
9010 | mutex_exit(&spa->spa_alloc_locks[i]); | |
9011 | } | |
9012 | ||
9013 | /* | |
9014 | * If there are any pending vdev state changes, convert them | |
9015 | * into config changes that go out with this transaction group. | |
9016 | */ | |
9017 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
9018 | while (list_head(&spa->spa_state_dirty_list) != NULL) { | |
9019 | /* | |
9020 | * We need the write lock here because, for aux vdevs, | |
9021 | * calling vdev_config_dirty() modifies sav_config. | |
9022 | * This is ugly and will become unnecessary when we | |
9023 | * eliminate the aux vdev wart by integrating all vdevs | |
9024 | * into the root vdev tree. | |
9025 | */ | |
9026 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9027 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
9028 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
9029 | vdev_state_clean(vd); | |
9030 | vdev_config_dirty(vd); | |
9031 | } | |
9032 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9033 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9034 | } | |
9035 | spa_config_exit(spa, SCL_STATE, FTAG); | |
9036 | ||
9037 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
9038 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
9039 | ||
9040 | spa->spa_sync_starttime = gethrtime(); | |
9041 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); | |
9042 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, | |
9043 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
9044 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
9045 | ||
9046 | /* | |
9047 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
9048 | * set spa_deflate if we have no raid-z vdevs. | |
9049 | */ | |
9050 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
9051 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
9052 | vdev_t *rvd = spa->spa_root_vdev; | |
9053 | ||
9054 | int i; | |
9055 | for (i = 0; i < rvd->vdev_children; i++) { | |
9056 | vd = rvd->vdev_child[i]; | |
9057 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
9058 | break; | |
9059 | } | |
9060 | if (i == rvd->vdev_children) { | |
9061 | spa->spa_deflate = TRUE; | |
9062 | VERIFY0(zap_add(spa->spa_meta_objset, | |
9063 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
9064 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
9065 | } | |
9066 | } | |
9067 | ||
9068 | spa_sync_adjust_vdev_max_queue_depth(spa); | |
9069 | ||
9070 | spa_sync_condense_indirect(spa, tx); | |
9071 | ||
9072 | spa_sync_iterate_to_convergence(spa, tx); | |
9073 | ||
9074 | #ifdef ZFS_DEBUG | |
9075 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
9076 | /* | |
9077 | * Make sure that the number of ZAPs for all the vdevs matches | |
9078 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
9079 | * called if the config is dirty; otherwise there may be | |
9080 | * outstanding AVZ operations that weren't completed in | |
9081 | * spa_sync_config_object. | |
9082 | */ | |
9083 | uint64_t all_vdev_zap_entry_count; | |
9084 | ASSERT0(zap_count(spa->spa_meta_objset, | |
9085 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
9086 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
9087 | all_vdev_zap_entry_count); | |
9088 | } | |
9089 | #endif | |
9090 | ||
9091 | if (spa->spa_vdev_removal != NULL) { | |
9092 | ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); | |
9093 | } | |
9094 | ||
9095 | spa_sync_rewrite_vdev_config(spa, tx); | |
34dc7c2f BB |
9096 | dmu_tx_commit(tx); |
9097 | ||
57ddcda1 | 9098 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 GW |
9099 | spa->spa_deadman_tqid = 0; |
9100 | ||
34dc7c2f BB |
9101 | /* |
9102 | * Clear the dirty config list. | |
9103 | */ | |
b128c09f | 9104 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
9105 | vdev_config_clean(vd); |
9106 | ||
9107 | /* | |
9108 | * Now that the new config has synced transactionally, | |
9109 | * let it become visible to the config cache. | |
9110 | */ | |
9111 | if (spa->spa_config_syncing != NULL) { | |
9112 | spa_config_set(spa, spa->spa_config_syncing); | |
9113 | spa->spa_config_txg = txg; | |
9114 | spa->spa_config_syncing = NULL; | |
9115 | } | |
9116 | ||
428870ff | 9117 | dsl_pool_sync_done(dp, txg); |
34dc7c2f | 9118 | |
492f64e9 PD |
9119 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
9120 | mutex_enter(&spa->spa_alloc_locks[i]); | |
9121 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
9122 | mutex_exit(&spa->spa_alloc_locks[i]); | |
9123 | } | |
3dfb57a3 | 9124 | |
34dc7c2f BB |
9125 | /* |
9126 | * Update usable space statistics. | |
9127 | */ | |
619f0976 GW |
9128 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) |
9129 | != NULL) | |
34dc7c2f | 9130 | vdev_sync_done(vd, txg); |
f09fda50 PD |
9131 | |
9132 | metaslab_class_evict_old(spa->spa_normal_class, txg); | |
9133 | metaslab_class_evict_old(spa->spa_log_class, txg); | |
9134 | ||
93e28d66 | 9135 | spa_sync_close_syncing_log_sm(spa); |
34dc7c2f | 9136 | |
428870ff BB |
9137 | spa_update_dspace(spa); |
9138 | ||
34dc7c2f BB |
9139 | /* |
9140 | * It had better be the case that we didn't dirty anything | |
9141 | * since vdev_config_sync(). | |
9142 | */ | |
9143 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
9144 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9145 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff | 9146 | |
d2734cce SD |
9147 | while (zfs_pause_spa_sync) |
9148 | delay(1); | |
9149 | ||
428870ff | 9150 | spa->spa_sync_pass = 0; |
34dc7c2f | 9151 | |
55922e73 GW |
9152 | /* |
9153 | * Update the last synced uberblock here. We want to do this at | |
9154 | * the end of spa_sync() so that consumers of spa_last_synced_txg() | |
9155 | * will be guaranteed that all the processing associated with | |
9156 | * that txg has been completed. | |
9157 | */ | |
9158 | spa->spa_ubsync = spa->spa_uberblock; | |
b128c09f | 9159 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 9160 | |
428870ff BB |
9161 | spa_handle_ignored_writes(spa); |
9162 | ||
34dc7c2f BB |
9163 | /* |
9164 | * If any async tasks have been requested, kick them off. | |
9165 | */ | |
9166 | spa_async_dispatch(spa); | |
9167 | } | |
9168 | ||
9169 | /* | |
9170 | * Sync all pools. We don't want to hold the namespace lock across these | |
9171 | * operations, so we take a reference on the spa_t and drop the lock during the | |
9172 | * sync. | |
9173 | */ | |
9174 | void | |
9175 | spa_sync_allpools(void) | |
9176 | { | |
9177 | spa_t *spa = NULL; | |
9178 | mutex_enter(&spa_namespace_lock); | |
9179 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
9180 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
9181 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
9182 | continue; |
9183 | spa_open_ref(spa, FTAG); | |
9184 | mutex_exit(&spa_namespace_lock); | |
9185 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9186 | mutex_enter(&spa_namespace_lock); | |
9187 | spa_close(spa, FTAG); | |
9188 | } | |
9189 | mutex_exit(&spa_namespace_lock); | |
9190 | } | |
9191 | ||
9192 | /* | |
9193 | * ========================================================================== | |
9194 | * Miscellaneous routines | |
9195 | * ========================================================================== | |
9196 | */ | |
9197 | ||
9198 | /* | |
9199 | * Remove all pools in the system. | |
9200 | */ | |
9201 | void | |
9202 | spa_evict_all(void) | |
9203 | { | |
9204 | spa_t *spa; | |
9205 | ||
9206 | /* | |
9207 | * Remove all cached state. All pools should be closed now, | |
9208 | * so every spa in the AVL tree should be unreferenced. | |
9209 | */ | |
9210 | mutex_enter(&spa_namespace_lock); | |
9211 | while ((spa = spa_next(NULL)) != NULL) { | |
9212 | /* | |
9213 | * Stop async tasks. The async thread may need to detach | |
9214 | * a device that's been replaced, which requires grabbing | |
9215 | * spa_namespace_lock, so we must drop it here. | |
9216 | */ | |
9217 | spa_open_ref(spa, FTAG); | |
9218 | mutex_exit(&spa_namespace_lock); | |
9219 | spa_async_suspend(spa); | |
9220 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
9221 | spa_close(spa, FTAG); |
9222 | ||
9223 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
9224 | spa_unload(spa); | |
9225 | spa_deactivate(spa); | |
9226 | } | |
9227 | spa_remove(spa); | |
9228 | } | |
9229 | mutex_exit(&spa_namespace_lock); | |
9230 | } | |
9231 | ||
9232 | vdev_t * | |
9babb374 | 9233 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 9234 | { |
b128c09f BB |
9235 | vdev_t *vd; |
9236 | int i; | |
9237 | ||
9238 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
9239 | return (vd); | |
9240 | ||
9babb374 | 9241 | if (aux) { |
b128c09f BB |
9242 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
9243 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
9244 | if (vd->vdev_guid == guid) |
9245 | return (vd); | |
9246 | } | |
9247 | ||
9248 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
9249 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
9250 | if (vd->vdev_guid == guid) |
9251 | return (vd); | |
9252 | } | |
9253 | } | |
9254 | ||
9255 | return (NULL); | |
34dc7c2f BB |
9256 | } |
9257 | ||
9258 | void | |
9259 | spa_upgrade(spa_t *spa, uint64_t version) | |
9260 | { | |
572e2857 BB |
9261 | ASSERT(spa_writeable(spa)); |
9262 | ||
b128c09f | 9263 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
9264 | |
9265 | /* | |
9266 | * This should only be called for a non-faulted pool, and since a | |
9267 | * future version would result in an unopenable pool, this shouldn't be | |
9268 | * possible. | |
9269 | */ | |
8dca0a9a | 9270 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
9b67f605 | 9271 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); |
34dc7c2f BB |
9272 | |
9273 | spa->spa_uberblock.ub_version = version; | |
9274 | vdev_config_dirty(spa->spa_root_vdev); | |
9275 | ||
b128c09f | 9276 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
9277 | |
9278 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9279 | } | |
9280 | ||
9281 | boolean_t | |
9282 | spa_has_spare(spa_t *spa, uint64_t guid) | |
9283 | { | |
9284 | int i; | |
9285 | uint64_t spareguid; | |
9286 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9287 | ||
9288 | for (i = 0; i < sav->sav_count; i++) | |
9289 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
9290 | return (B_TRUE); | |
9291 | ||
9292 | for (i = 0; i < sav->sav_npending; i++) { | |
9293 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
9294 | &spareguid) == 0 && spareguid == guid) | |
9295 | return (B_TRUE); | |
9296 | } | |
9297 | ||
9298 | return (B_FALSE); | |
9299 | } | |
9300 | ||
b128c09f BB |
9301 | /* |
9302 | * Check if a pool has an active shared spare device. | |
9303 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
9304 | */ | |
9305 | static boolean_t | |
9306 | spa_has_active_shared_spare(spa_t *spa) | |
9307 | { | |
9308 | int i, refcnt; | |
9309 | uint64_t pool; | |
9310 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9311 | ||
9312 | for (i = 0; i < sav->sav_count; i++) { | |
9313 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
9314 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
9315 | refcnt > 2) | |
9316 | return (B_TRUE); | |
9317 | } | |
9318 | ||
9319 | return (B_FALSE); | |
9320 | } | |
9321 | ||
93e28d66 SD |
9322 | uint64_t |
9323 | spa_total_metaslabs(spa_t *spa) | |
9324 | { | |
9325 | vdev_t *rvd = spa->spa_root_vdev; | |
9326 | ||
9327 | uint64_t m = 0; | |
9328 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
9329 | vdev_t *vd = rvd->vdev_child[c]; | |
9330 | if (!vdev_is_concrete(vd)) | |
9331 | continue; | |
9332 | m += vd->vdev_ms_count; | |
9333 | } | |
9334 | return (m); | |
9335 | } | |
9336 | ||
e60e158e JG |
9337 | /* |
9338 | * Notify any waiting threads that some activity has switched from being in- | |
9339 | * progress to not-in-progress so that the thread can wake up and determine | |
9340 | * whether it is finished waiting. | |
9341 | */ | |
9342 | void | |
9343 | spa_notify_waiters(spa_t *spa) | |
9344 | { | |
9345 | /* | |
9346 | * Acquiring spa_activities_lock here prevents the cv_broadcast from | |
9347 | * happening between the waiting thread's check and cv_wait. | |
9348 | */ | |
9349 | mutex_enter(&spa->spa_activities_lock); | |
9350 | cv_broadcast(&spa->spa_activities_cv); | |
9351 | mutex_exit(&spa->spa_activities_lock); | |
9352 | } | |
9353 | ||
9354 | /* | |
9355 | * Notify any waiting threads that the pool is exporting, and then block until | |
9356 | * they are finished using the spa_t. | |
9357 | */ | |
9358 | void | |
9359 | spa_wake_waiters(spa_t *spa) | |
9360 | { | |
9361 | mutex_enter(&spa->spa_activities_lock); | |
9362 | spa->spa_waiters_cancel = B_TRUE; | |
9363 | cv_broadcast(&spa->spa_activities_cv); | |
9364 | while (spa->spa_waiters != 0) | |
9365 | cv_wait(&spa->spa_waiters_cv, &spa->spa_activities_lock); | |
9366 | spa->spa_waiters_cancel = B_FALSE; | |
9367 | mutex_exit(&spa->spa_activities_lock); | |
9368 | } | |
9369 | ||
2288d419 | 9370 | /* Whether the vdev or any of its descendants are being initialized/trimmed. */ |
e60e158e | 9371 | static boolean_t |
2288d419 | 9372 | spa_vdev_activity_in_progress_impl(vdev_t *vd, zpool_wait_activity_t activity) |
e60e158e JG |
9373 | { |
9374 | spa_t *spa = vd->vdev_spa; | |
e60e158e JG |
9375 | |
9376 | ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER)); | |
9377 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
2288d419 BB |
9378 | ASSERT(activity == ZPOOL_WAIT_INITIALIZE || |
9379 | activity == ZPOOL_WAIT_TRIM); | |
9380 | ||
9381 | kmutex_t *lock = activity == ZPOOL_WAIT_INITIALIZE ? | |
9382 | &vd->vdev_initialize_lock : &vd->vdev_trim_lock; | |
e60e158e JG |
9383 | |
9384 | mutex_exit(&spa->spa_activities_lock); | |
2288d419 | 9385 | mutex_enter(lock); |
e60e158e JG |
9386 | mutex_enter(&spa->spa_activities_lock); |
9387 | ||
2288d419 BB |
9388 | boolean_t in_progress = (activity == ZPOOL_WAIT_INITIALIZE) ? |
9389 | (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) : | |
9390 | (vd->vdev_trim_state == VDEV_TRIM_ACTIVE); | |
9391 | mutex_exit(lock); | |
e60e158e | 9392 | |
2288d419 | 9393 | if (in_progress) |
e60e158e JG |
9394 | return (B_TRUE); |
9395 | ||
9396 | for (int i = 0; i < vd->vdev_children; i++) { | |
2288d419 BB |
9397 | if (spa_vdev_activity_in_progress_impl(vd->vdev_child[i], |
9398 | activity)) | |
e60e158e JG |
9399 | return (B_TRUE); |
9400 | } | |
9401 | ||
9402 | return (B_FALSE); | |
9403 | } | |
9404 | ||
9405 | /* | |
9406 | * If use_guid is true, this checks whether the vdev specified by guid is | |
2288d419 BB |
9407 | * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool |
9408 | * is being initialized/trimmed. The caller must hold the config lock and | |
9409 | * spa_activities_lock. | |
e60e158e JG |
9410 | */ |
9411 | static int | |
2288d419 BB |
9412 | spa_vdev_activity_in_progress(spa_t *spa, boolean_t use_guid, uint64_t guid, |
9413 | zpool_wait_activity_t activity, boolean_t *in_progress) | |
e60e158e JG |
9414 | { |
9415 | mutex_exit(&spa->spa_activities_lock); | |
9416 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9417 | mutex_enter(&spa->spa_activities_lock); | |
9418 | ||
9419 | vdev_t *vd; | |
9420 | if (use_guid) { | |
9421 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
9422 | if (vd == NULL || !vd->vdev_ops->vdev_op_leaf) { | |
9423 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9424 | return (EINVAL); | |
9425 | } | |
9426 | } else { | |
9427 | vd = spa->spa_root_vdev; | |
9428 | } | |
9429 | ||
2288d419 | 9430 | *in_progress = spa_vdev_activity_in_progress_impl(vd, activity); |
e60e158e JG |
9431 | |
9432 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9433 | return (0); | |
9434 | } | |
9435 | ||
9436 | /* | |
9437 | * Locking for waiting threads | |
9438 | * --------------------------- | |
9439 | * | |
9440 | * Waiting threads need a way to check whether a given activity is in progress, | |
9441 | * and then, if it is, wait for it to complete. Each activity will have some | |
9442 | * in-memory representation of the relevant on-disk state which can be used to | |
9443 | * determine whether or not the activity is in progress. The in-memory state and | |
9444 | * the locking used to protect it will be different for each activity, and may | |
9445 | * not be suitable for use with a cvar (e.g., some state is protected by the | |
9446 | * config lock). To allow waiting threads to wait without any races, another | |
9447 | * lock, spa_activities_lock, is used. | |
9448 | * | |
9449 | * When the state is checked, both the activity-specific lock (if there is one) | |
9450 | * and spa_activities_lock are held. In some cases, the activity-specific lock | |
9451 | * is acquired explicitly (e.g. the config lock). In others, the locking is | |
9452 | * internal to some check (e.g. bpobj_is_empty). After checking, the waiting | |
9453 | * thread releases the activity-specific lock and, if the activity is in | |
9454 | * progress, then cv_waits using spa_activities_lock. | |
9455 | * | |
9456 | * The waiting thread is woken when another thread, one completing some | |
9457 | * activity, updates the state of the activity and then calls | |
9458 | * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only | |
9459 | * needs to hold its activity-specific lock when updating the state, and this | |
9460 | * lock can (but doesn't have to) be dropped before calling spa_notify_waiters. | |
9461 | * | |
9462 | * Because spa_notify_waiters acquires spa_activities_lock before broadcasting, | |
9463 | * and because it is held when the waiting thread checks the state of the | |
9464 | * activity, it can never be the case that the completing thread both updates | |
9465 | * the activity state and cv_broadcasts in between the waiting thread's check | |
9466 | * and cv_wait. Thus, a waiting thread can never miss a wakeup. | |
9467 | * | |
9468 | * In order to prevent deadlock, when the waiting thread does its check, in some | |
9469 | * cases it will temporarily drop spa_activities_lock in order to acquire the | |
9470 | * activity-specific lock. The order in which spa_activities_lock and the | |
9471 | * activity specific lock are acquired in the waiting thread is determined by | |
9472 | * the order in which they are acquired in the completing thread; if the | |
9473 | * completing thread calls spa_notify_waiters with the activity-specific lock | |
9474 | * held, then the waiting thread must also acquire the activity-specific lock | |
9475 | * first. | |
9476 | */ | |
9477 | ||
9478 | static int | |
9479 | spa_activity_in_progress(spa_t *spa, zpool_wait_activity_t activity, | |
9480 | boolean_t use_tag, uint64_t tag, boolean_t *in_progress) | |
9481 | { | |
9482 | int error = 0; | |
9483 | ||
9484 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
9485 | ||
9486 | switch (activity) { | |
9487 | case ZPOOL_WAIT_CKPT_DISCARD: | |
9488 | *in_progress = | |
9489 | (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT) && | |
9490 | zap_contains(spa_meta_objset(spa), | |
9491 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ZPOOL_CHECKPOINT) == | |
9492 | ENOENT); | |
9493 | break; | |
9494 | case ZPOOL_WAIT_FREE: | |
9495 | *in_progress = ((spa_version(spa) >= SPA_VERSION_DEADLISTS && | |
9496 | !bpobj_is_empty(&spa->spa_dsl_pool->dp_free_bpobj)) || | |
9497 | spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY) || | |
9498 | spa_livelist_delete_check(spa)); | |
9499 | break; | |
9500 | case ZPOOL_WAIT_INITIALIZE: | |
2288d419 BB |
9501 | case ZPOOL_WAIT_TRIM: |
9502 | error = spa_vdev_activity_in_progress(spa, use_tag, tag, | |
9503 | activity, in_progress); | |
e60e158e JG |
9504 | break; |
9505 | case ZPOOL_WAIT_REPLACE: | |
9506 | mutex_exit(&spa->spa_activities_lock); | |
9507 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9508 | mutex_enter(&spa->spa_activities_lock); | |
9509 | ||
9510 | *in_progress = vdev_replace_in_progress(spa->spa_root_vdev); | |
9511 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9512 | break; | |
9513 | case ZPOOL_WAIT_REMOVE: | |
9514 | *in_progress = (spa->spa_removing_phys.sr_state == | |
9515 | DSS_SCANNING); | |
9516 | break; | |
9517 | case ZPOOL_WAIT_RESILVER: | |
9a49d3f3 BB |
9518 | if ((*in_progress = vdev_rebuild_active(spa->spa_root_vdev))) |
9519 | break; | |
9520 | /* fall through */ | |
e60e158e JG |
9521 | case ZPOOL_WAIT_SCRUB: |
9522 | { | |
9523 | boolean_t scanning, paused, is_scrub; | |
9524 | dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; | |
9525 | ||
9526 | is_scrub = (scn->scn_phys.scn_func == POOL_SCAN_SCRUB); | |
9527 | scanning = (scn->scn_phys.scn_state == DSS_SCANNING); | |
9528 | paused = dsl_scan_is_paused_scrub(scn); | |
9529 | *in_progress = (scanning && !paused && | |
9530 | is_scrub == (activity == ZPOOL_WAIT_SCRUB)); | |
9531 | break; | |
9532 | } | |
9533 | default: | |
9534 | panic("unrecognized value for activity %d", activity); | |
9535 | } | |
9536 | ||
9537 | return (error); | |
9538 | } | |
9539 | ||
9540 | static int | |
9541 | spa_wait_common(const char *pool, zpool_wait_activity_t activity, | |
9542 | boolean_t use_tag, uint64_t tag, boolean_t *waited) | |
9543 | { | |
9544 | /* | |
9545 | * The tag is used to distinguish between instances of an activity. | |
2288d419 BB |
9546 | * 'initialize' and 'trim' are the only activities that we use this for. |
9547 | * The other activities can only have a single instance in progress in a | |
9548 | * pool at one time, making the tag unnecessary. | |
e60e158e JG |
9549 | * |
9550 | * There can be multiple devices being replaced at once, but since they | |
9551 | * all finish once resilvering finishes, we don't bother keeping track | |
9552 | * of them individually, we just wait for them all to finish. | |
9553 | */ | |
2288d419 BB |
9554 | if (use_tag && activity != ZPOOL_WAIT_INITIALIZE && |
9555 | activity != ZPOOL_WAIT_TRIM) | |
e60e158e JG |
9556 | return (EINVAL); |
9557 | ||
9558 | if (activity < 0 || activity >= ZPOOL_WAIT_NUM_ACTIVITIES) | |
9559 | return (EINVAL); | |
9560 | ||
9561 | spa_t *spa; | |
9562 | int error = spa_open(pool, &spa, FTAG); | |
9563 | if (error != 0) | |
9564 | return (error); | |
9565 | ||
9566 | /* | |
9567 | * Increment the spa's waiter count so that we can call spa_close and | |
9568 | * still ensure that the spa_t doesn't get freed before this thread is | |
9569 | * finished with it when the pool is exported. We want to call spa_close | |
9570 | * before we start waiting because otherwise the additional ref would | |
9571 | * prevent the pool from being exported or destroyed throughout the | |
9572 | * potentially long wait. | |
9573 | */ | |
9574 | mutex_enter(&spa->spa_activities_lock); | |
9575 | spa->spa_waiters++; | |
9576 | spa_close(spa, FTAG); | |
9577 | ||
9578 | *waited = B_FALSE; | |
9579 | for (;;) { | |
9580 | boolean_t in_progress; | |
9581 | error = spa_activity_in_progress(spa, activity, use_tag, tag, | |
9582 | &in_progress); | |
9583 | ||
b24771a8 | 9584 | if (error || !in_progress || spa->spa_waiters_cancel) |
e60e158e JG |
9585 | break; |
9586 | ||
9587 | *waited = B_TRUE; | |
9588 | ||
9589 | if (cv_wait_sig(&spa->spa_activities_cv, | |
9590 | &spa->spa_activities_lock) == 0) { | |
9591 | error = EINTR; | |
9592 | break; | |
9593 | } | |
9594 | } | |
9595 | ||
9596 | spa->spa_waiters--; | |
9597 | cv_signal(&spa->spa_waiters_cv); | |
9598 | mutex_exit(&spa->spa_activities_lock); | |
9599 | ||
9600 | return (error); | |
9601 | } | |
9602 | ||
9603 | /* | |
9604 | * Wait for a particular instance of the specified activity to complete, where | |
9605 | * the instance is identified by 'tag' | |
9606 | */ | |
9607 | int | |
9608 | spa_wait_tag(const char *pool, zpool_wait_activity_t activity, uint64_t tag, | |
9609 | boolean_t *waited) | |
9610 | { | |
9611 | return (spa_wait_common(pool, activity, B_TRUE, tag, waited)); | |
9612 | } | |
9613 | ||
9614 | /* | |
9615 | * Wait for all instances of the specified activity complete | |
9616 | */ | |
9617 | int | |
9618 | spa_wait(const char *pool, zpool_wait_activity_t activity, boolean_t *waited) | |
9619 | { | |
9620 | ||
9621 | return (spa_wait_common(pool, activity, B_FALSE, 0, waited)); | |
9622 | } | |
9623 | ||
a1d477c2 | 9624 | sysevent_t * |
12fa0466 DE |
9625 | spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
9626 | { | |
9627 | sysevent_t *ev = NULL; | |
9628 | #ifdef _KERNEL | |
9629 | nvlist_t *resource; | |
9630 | ||
9631 | resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl); | |
9632 | if (resource) { | |
9633 | ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP); | |
9634 | ev->resource = resource; | |
9635 | } | |
9636 | #endif | |
9637 | return (ev); | |
9638 | } | |
9639 | ||
a1d477c2 | 9640 | void |
12fa0466 DE |
9641 | spa_event_post(sysevent_t *ev) |
9642 | { | |
9643 | #ifdef _KERNEL | |
9644 | if (ev) { | |
9645 | zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb); | |
9646 | kmem_free(ev, sizeof (*ev)); | |
9647 | } | |
9648 | #endif | |
9649 | } | |
9650 | ||
34dc7c2f | 9651 | /* |
fb390aaf HR |
9652 | * Post a zevent corresponding to the given sysevent. The 'name' must be one |
9653 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
34dc7c2f BB |
9654 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
9655 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
9656 | * or zdb as real changes. | |
9657 | */ | |
9658 | void | |
12fa0466 | 9659 | spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
34dc7c2f | 9660 | { |
12fa0466 | 9661 | spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); |
34dc7c2f | 9662 | } |
c28b2279 | 9663 | |
c28b2279 BB |
9664 | /* state manipulation functions */ |
9665 | EXPORT_SYMBOL(spa_open); | |
9666 | EXPORT_SYMBOL(spa_open_rewind); | |
9667 | EXPORT_SYMBOL(spa_get_stats); | |
9668 | EXPORT_SYMBOL(spa_create); | |
c28b2279 BB |
9669 | EXPORT_SYMBOL(spa_import); |
9670 | EXPORT_SYMBOL(spa_tryimport); | |
9671 | EXPORT_SYMBOL(spa_destroy); | |
9672 | EXPORT_SYMBOL(spa_export); | |
9673 | EXPORT_SYMBOL(spa_reset); | |
9674 | EXPORT_SYMBOL(spa_async_request); | |
9675 | EXPORT_SYMBOL(spa_async_suspend); | |
9676 | EXPORT_SYMBOL(spa_async_resume); | |
9677 | EXPORT_SYMBOL(spa_inject_addref); | |
9678 | EXPORT_SYMBOL(spa_inject_delref); | |
9679 | EXPORT_SYMBOL(spa_scan_stat_init); | |
9680 | EXPORT_SYMBOL(spa_scan_get_stats); | |
9681 | ||
e1cfd73f | 9682 | /* device manipulation */ |
c28b2279 BB |
9683 | EXPORT_SYMBOL(spa_vdev_add); |
9684 | EXPORT_SYMBOL(spa_vdev_attach); | |
9685 | EXPORT_SYMBOL(spa_vdev_detach); | |
c28b2279 BB |
9686 | EXPORT_SYMBOL(spa_vdev_setpath); |
9687 | EXPORT_SYMBOL(spa_vdev_setfru); | |
9688 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
9689 | ||
9690 | /* spare statech is global across all pools) */ | |
9691 | EXPORT_SYMBOL(spa_spare_add); | |
9692 | EXPORT_SYMBOL(spa_spare_remove); | |
9693 | EXPORT_SYMBOL(spa_spare_exists); | |
9694 | EXPORT_SYMBOL(spa_spare_activate); | |
9695 | ||
9696 | /* L2ARC statech is global across all pools) */ | |
9697 | EXPORT_SYMBOL(spa_l2cache_add); | |
9698 | EXPORT_SYMBOL(spa_l2cache_remove); | |
9699 | EXPORT_SYMBOL(spa_l2cache_exists); | |
9700 | EXPORT_SYMBOL(spa_l2cache_activate); | |
9701 | EXPORT_SYMBOL(spa_l2cache_drop); | |
9702 | ||
9703 | /* scanning */ | |
9704 | EXPORT_SYMBOL(spa_scan); | |
9705 | EXPORT_SYMBOL(spa_scan_stop); | |
9706 | ||
9707 | /* spa syncing */ | |
9708 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
9709 | EXPORT_SYMBOL(spa_sync_allpools); | |
9710 | ||
9711 | /* properties */ | |
9712 | EXPORT_SYMBOL(spa_prop_set); | |
9713 | EXPORT_SYMBOL(spa_prop_get); | |
9714 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
9715 | ||
9716 | /* asynchronous event notification */ | |
9717 | EXPORT_SYMBOL(spa_event_notify); | |
dea377c0 | 9718 | |
c8242a96 | 9719 | /* BEGIN CSTYLED */ |
03fdcb9a MM |
9720 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_shift, INT, ZMOD_RW, |
9721 | "log2(fraction of arc that can be used by inflight I/Os when " | |
9722 | "verifying pool during import"); | |
dea377c0 | 9723 | |
03fdcb9a | 9724 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_metadata, INT, ZMOD_RW, |
dea377c0 MA |
9725 | "Set to traverse metadata on pool import"); |
9726 | ||
03fdcb9a | 9727 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_data, INT, ZMOD_RW, |
dea377c0 | 9728 | "Set to traverse data on pool import"); |
dcb6bed1 | 9729 | |
03fdcb9a | 9730 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_print_vdev_tree, INT, ZMOD_RW, |
6cb8e530 PZ |
9731 | "Print vdev tree to zfs_dbgmsg during pool import"); |
9732 | ||
03fdcb9a | 9733 | ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_pct, UINT, ZMOD_RD, |
dcb6bed1 D |
9734 | "Percentage of CPUs to run an IO worker thread"); |
9735 | ||
03fdcb9a MM |
9736 | ZFS_MODULE_PARAM(zfs, zfs_, max_missing_tvds, ULONG, ZMOD_RW, |
9737 | "Allow importing pool with up to this number of missing top-level " | |
9738 | "vdevs (in read-only mode)"); | |
6cb8e530 | 9739 | |
03fdcb9a | 9740 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_pause, INT, ZMOD_RW, |
37f03da8 | 9741 | "Set the livelist condense zthr to pause"); |
03fdcb9a MM |
9742 | |
9743 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_pause, INT, ZMOD_RW, | |
37f03da8 SH |
9744 | "Set the livelist condense synctask to pause"); |
9745 | ||
03fdcb9a | 9746 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_cancel, INT, ZMOD_RW, |
37f03da8 | 9747 | "Whether livelist condensing was canceled in the synctask"); |
03fdcb9a MM |
9748 | |
9749 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_cancel, INT, ZMOD_RW, | |
37f03da8 SH |
9750 | "Whether livelist condensing was canceled in the zthr function"); |
9751 | ||
03fdcb9a MM |
9752 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, new_alloc, INT, ZMOD_RW, |
9753 | "Whether extra ALLOC blkptrs were added to a livelist entry while it " | |
9754 | "was being condensed"); | |
37f03da8 | 9755 | /* END CSTYLED */ |