]>
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; |
f1512ee6 | 624 | uint64_t propval; |
b128c09f | 625 | |
34dc7c2f BB |
626 | if (strval == NULL || strval[0] == '\0') { |
627 | objnum = zpool_prop_default_numeric( | |
628 | ZPOOL_PROP_BOOTFS); | |
629 | break; | |
630 | } | |
631 | ||
d1d7e268 | 632 | error = dmu_objset_hold(strval, FTAG, &os); |
619f0976 | 633 | if (error != 0) |
34dc7c2f | 634 | break; |
b128c09f | 635 | |
f1512ee6 MA |
636 | /* |
637 | * Must be ZPL, and its property settings | |
638 | * must be supported by GRUB (compression | |
fcf21f8f RL |
639 | * is not gzip, and large dnodes are not |
640 | * used). | |
f1512ee6 | 641 | */ |
428870ff BB |
642 | |
643 | if (dmu_objset_type(os) != DMU_OST_ZFS) { | |
2e528b49 | 644 | error = SET_ERROR(ENOTSUP); |
13fe0198 MA |
645 | } else if ((error = |
646 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
b128c09f | 647 | zfs_prop_to_name(ZFS_PROP_COMPRESSION), |
f1512ee6 MA |
648 | &propval)) == 0 && |
649 | !BOOTFS_COMPRESS_VALID(propval)) { | |
650 | error = SET_ERROR(ENOTSUP); | |
50c957f7 NB |
651 | } else if ((error = |
652 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
653 | zfs_prop_to_name(ZFS_PROP_DNODESIZE), | |
654 | &propval)) == 0 && | |
655 | propval != ZFS_DNSIZE_LEGACY) { | |
656 | error = SET_ERROR(ENOTSUP); | |
b128c09f BB |
657 | } else { |
658 | objnum = dmu_objset_id(os); | |
659 | } | |
428870ff | 660 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
661 | } |
662 | break; | |
b128c09f | 663 | |
34dc7c2f BB |
664 | case ZPOOL_PROP_FAILUREMODE: |
665 | error = nvpair_value_uint64(elem, &intval); | |
3bfd95d5 | 666 | if (!error && intval > ZIO_FAILURE_MODE_PANIC) |
2e528b49 | 667 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
668 | |
669 | /* | |
670 | * This is a special case which only occurs when | |
671 | * the pool has completely failed. This allows | |
672 | * the user to change the in-core failmode property | |
673 | * without syncing it out to disk (I/Os might | |
674 | * currently be blocked). We do this by returning | |
675 | * EIO to the caller (spa_prop_set) to trick it | |
676 | * into thinking we encountered a property validation | |
677 | * error. | |
678 | */ | |
b128c09f | 679 | if (!error && spa_suspended(spa)) { |
34dc7c2f | 680 | spa->spa_failmode = intval; |
2e528b49 | 681 | error = SET_ERROR(EIO); |
34dc7c2f BB |
682 | } |
683 | break; | |
684 | ||
685 | case ZPOOL_PROP_CACHEFILE: | |
686 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
687 | break; | |
688 | ||
689 | if (strval[0] == '\0') | |
690 | break; | |
691 | ||
692 | if (strcmp(strval, "none") == 0) | |
693 | break; | |
694 | ||
695 | if (strval[0] != '/') { | |
2e528b49 | 696 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
697 | break; |
698 | } | |
699 | ||
700 | slash = strrchr(strval, '/'); | |
701 | ASSERT(slash != NULL); | |
702 | ||
703 | if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || | |
704 | strcmp(slash, "/..") == 0) | |
2e528b49 | 705 | error = SET_ERROR(EINVAL); |
34dc7c2f | 706 | break; |
428870ff | 707 | |
d96eb2b1 DM |
708 | case ZPOOL_PROP_COMMENT: |
709 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
710 | break; | |
711 | for (check = strval; *check != '\0'; check++) { | |
712 | if (!isprint(*check)) { | |
2e528b49 | 713 | error = SET_ERROR(EINVAL); |
d96eb2b1 DM |
714 | break; |
715 | } | |
d96eb2b1 DM |
716 | } |
717 | if (strlen(strval) > ZPROP_MAX_COMMENT) | |
2e528b49 | 718 | error = SET_ERROR(E2BIG); |
d96eb2b1 DM |
719 | break; |
720 | ||
e75c13c3 BB |
721 | default: |
722 | break; | |
34dc7c2f BB |
723 | } |
724 | ||
725 | if (error) | |
726 | break; | |
727 | } | |
728 | ||
050d720c MA |
729 | (void) nvlist_remove_all(props, |
730 | zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO)); | |
731 | ||
34dc7c2f BB |
732 | if (!error && reset_bootfs) { |
733 | error = nvlist_remove(props, | |
734 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); | |
735 | ||
736 | if (!error) { | |
737 | error = nvlist_add_uint64(props, | |
738 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); | |
739 | } | |
740 | } | |
741 | ||
742 | return (error); | |
743 | } | |
744 | ||
d164b209 BB |
745 | void |
746 | spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) | |
747 | { | |
748 | char *cachefile; | |
749 | spa_config_dirent_t *dp; | |
750 | ||
751 | if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
752 | &cachefile) != 0) | |
753 | return; | |
754 | ||
755 | dp = kmem_alloc(sizeof (spa_config_dirent_t), | |
79c76d5b | 756 | KM_SLEEP); |
d164b209 BB |
757 | |
758 | if (cachefile[0] == '\0') | |
759 | dp->scd_path = spa_strdup(spa_config_path); | |
760 | else if (strcmp(cachefile, "none") == 0) | |
761 | dp->scd_path = NULL; | |
762 | else | |
763 | dp->scd_path = spa_strdup(cachefile); | |
764 | ||
765 | list_insert_head(&spa->spa_config_list, dp); | |
766 | if (need_sync) | |
767 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
768 | } | |
769 | ||
34dc7c2f BB |
770 | int |
771 | spa_prop_set(spa_t *spa, nvlist_t *nvp) | |
772 | { | |
773 | int error; | |
9ae529ec | 774 | nvpair_t *elem = NULL; |
d164b209 | 775 | boolean_t need_sync = B_FALSE; |
34dc7c2f BB |
776 | |
777 | if ((error = spa_prop_validate(spa, nvp)) != 0) | |
778 | return (error); | |
779 | ||
d164b209 | 780 | while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { |
9ae529ec | 781 | zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); |
d164b209 | 782 | |
572e2857 BB |
783 | if (prop == ZPOOL_PROP_CACHEFILE || |
784 | prop == ZPOOL_PROP_ALTROOT || | |
785 | prop == ZPOOL_PROP_READONLY) | |
d164b209 BB |
786 | continue; |
787 | ||
31864e3d | 788 | if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { |
9ae529ec CS |
789 | uint64_t ver; |
790 | ||
791 | if (prop == ZPOOL_PROP_VERSION) { | |
792 | VERIFY(nvpair_value_uint64(elem, &ver) == 0); | |
793 | } else { | |
794 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
795 | ver = SPA_VERSION_FEATURES; | |
796 | need_sync = B_TRUE; | |
797 | } | |
798 | ||
799 | /* Save time if the version is already set. */ | |
800 | if (ver == spa_version(spa)) | |
801 | continue; | |
802 | ||
803 | /* | |
804 | * In addition to the pool directory object, we might | |
805 | * create the pool properties object, the features for | |
806 | * read object, the features for write object, or the | |
807 | * feature descriptions object. | |
808 | */ | |
13fe0198 | 809 | error = dsl_sync_task(spa->spa_name, NULL, |
3d45fdd6 MA |
810 | spa_sync_version, &ver, |
811 | 6, ZFS_SPACE_CHECK_RESERVED); | |
9ae529ec CS |
812 | if (error) |
813 | return (error); | |
814 | continue; | |
815 | } | |
816 | ||
d164b209 BB |
817 | need_sync = B_TRUE; |
818 | break; | |
819 | } | |
820 | ||
9ae529ec | 821 | if (need_sync) { |
13fe0198 | 822 | return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, |
3d45fdd6 | 823 | nvp, 6, ZFS_SPACE_CHECK_RESERVED)); |
9ae529ec CS |
824 | } |
825 | ||
826 | return (0); | |
34dc7c2f BB |
827 | } |
828 | ||
829 | /* | |
830 | * If the bootfs property value is dsobj, clear it. | |
831 | */ | |
832 | void | |
833 | spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) | |
834 | { | |
835 | if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { | |
836 | VERIFY(zap_remove(spa->spa_meta_objset, | |
837 | spa->spa_pool_props_object, | |
838 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); | |
839 | spa->spa_bootfs = 0; | |
840 | } | |
841 | } | |
842 | ||
3bc7e0fb GW |
843 | /*ARGSUSED*/ |
844 | static int | |
13fe0198 | 845 | spa_change_guid_check(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 846 | { |
2a8ba608 | 847 | uint64_t *newguid __maybe_unused = arg; |
13fe0198 | 848 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; |
3bc7e0fb GW |
849 | vdev_t *rvd = spa->spa_root_vdev; |
850 | uint64_t vdev_state; | |
3bc7e0fb | 851 | |
d2734cce SD |
852 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { |
853 | int error = (spa_has_checkpoint(spa)) ? | |
854 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
855 | return (SET_ERROR(error)); | |
856 | } | |
857 | ||
3bc7e0fb GW |
858 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
859 | vdev_state = rvd->vdev_state; | |
860 | spa_config_exit(spa, SCL_STATE, FTAG); | |
861 | ||
862 | if (vdev_state != VDEV_STATE_HEALTHY) | |
2e528b49 | 863 | return (SET_ERROR(ENXIO)); |
3bc7e0fb GW |
864 | |
865 | ASSERT3U(spa_guid(spa), !=, *newguid); | |
866 | ||
867 | return (0); | |
868 | } | |
869 | ||
870 | static void | |
13fe0198 | 871 | spa_change_guid_sync(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 872 | { |
13fe0198 MA |
873 | uint64_t *newguid = arg; |
874 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
3bc7e0fb GW |
875 | uint64_t oldguid; |
876 | vdev_t *rvd = spa->spa_root_vdev; | |
877 | ||
878 | oldguid = spa_guid(spa); | |
879 | ||
880 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
881 | rvd->vdev_guid = *newguid; | |
882 | rvd->vdev_guid_sum += (*newguid - oldguid); | |
883 | vdev_config_dirty(rvd); | |
884 | spa_config_exit(spa, SCL_STATE, FTAG); | |
885 | ||
6f1ffb06 | 886 | spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", |
74756182 | 887 | (u_longlong_t)oldguid, (u_longlong_t)*newguid); |
3bc7e0fb GW |
888 | } |
889 | ||
3541dc6d GA |
890 | /* |
891 | * Change the GUID for the pool. This is done so that we can later | |
892 | * re-import a pool built from a clone of our own vdevs. We will modify | |
893 | * the root vdev's guid, our own pool guid, and then mark all of our | |
894 | * vdevs dirty. Note that we must make sure that all our vdevs are | |
895 | * online when we do this, or else any vdevs that weren't present | |
896 | * would be orphaned from our pool. We are also going to issue a | |
897 | * sysevent to update any watchers. | |
898 | */ | |
899 | int | |
900 | spa_change_guid(spa_t *spa) | |
901 | { | |
3bc7e0fb GW |
902 | int error; |
903 | uint64_t guid; | |
3541dc6d | 904 | |
621dd7bb | 905 | mutex_enter(&spa->spa_vdev_top_lock); |
3bc7e0fb GW |
906 | mutex_enter(&spa_namespace_lock); |
907 | guid = spa_generate_guid(NULL); | |
3541dc6d | 908 | |
13fe0198 | 909 | error = dsl_sync_task(spa->spa_name, spa_change_guid_check, |
3d45fdd6 | 910 | spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); |
3541dc6d | 911 | |
3bc7e0fb | 912 | if (error == 0) { |
a1d477c2 | 913 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 914 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); |
3bc7e0fb | 915 | } |
3541dc6d | 916 | |
3bc7e0fb | 917 | mutex_exit(&spa_namespace_lock); |
621dd7bb | 918 | mutex_exit(&spa->spa_vdev_top_lock); |
3541dc6d | 919 | |
3bc7e0fb | 920 | return (error); |
3541dc6d GA |
921 | } |
922 | ||
34dc7c2f BB |
923 | /* |
924 | * ========================================================================== | |
925 | * SPA state manipulation (open/create/destroy/import/export) | |
926 | * ========================================================================== | |
927 | */ | |
928 | ||
929 | static int | |
930 | spa_error_entry_compare(const void *a, const void *b) | |
931 | { | |
ee36c709 GN |
932 | const spa_error_entry_t *sa = (const spa_error_entry_t *)a; |
933 | const spa_error_entry_t *sb = (const spa_error_entry_t *)b; | |
34dc7c2f BB |
934 | int ret; |
935 | ||
ee36c709 | 936 | ret = memcmp(&sa->se_bookmark, &sb->se_bookmark, |
5dbd68a3 | 937 | sizeof (zbookmark_phys_t)); |
34dc7c2f | 938 | |
ca577779 | 939 | return (TREE_ISIGN(ret)); |
34dc7c2f BB |
940 | } |
941 | ||
942 | /* | |
943 | * Utility function which retrieves copies of the current logs and | |
944 | * re-initializes them in the process. | |
945 | */ | |
946 | void | |
947 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
948 | { | |
949 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
950 | ||
951 | bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); | |
952 | bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); | |
953 | ||
954 | avl_create(&spa->spa_errlist_scrub, | |
955 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
956 | offsetof(spa_error_entry_t, se_avl)); | |
957 | avl_create(&spa->spa_errlist_last, | |
958 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
959 | offsetof(spa_error_entry_t, se_avl)); | |
960 | } | |
961 | ||
7ef5e54e AL |
962 | static void |
963 | spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
34dc7c2f | 964 | { |
7ef5e54e AL |
965 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; |
966 | enum zti_modes mode = ztip->zti_mode; | |
967 | uint_t value = ztip->zti_value; | |
968 | uint_t count = ztip->zti_count; | |
969 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1c27024e | 970 | uint_t flags = 0; |
428870ff | 971 | boolean_t batch = B_FALSE; |
34dc7c2f | 972 | |
7ef5e54e AL |
973 | if (mode == ZTI_MODE_NULL) { |
974 | tqs->stqs_count = 0; | |
975 | tqs->stqs_taskq = NULL; | |
976 | return; | |
977 | } | |
428870ff | 978 | |
7ef5e54e | 979 | ASSERT3U(count, >, 0); |
428870ff | 980 | |
7ef5e54e AL |
981 | tqs->stqs_count = count; |
982 | tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); | |
428870ff | 983 | |
e8b96c60 MA |
984 | switch (mode) { |
985 | case ZTI_MODE_FIXED: | |
986 | ASSERT3U(value, >=, 1); | |
987 | value = MAX(value, 1); | |
d33931a8 | 988 | flags |= TASKQ_DYNAMIC; |
e8b96c60 | 989 | break; |
7ef5e54e | 990 | |
e8b96c60 MA |
991 | case ZTI_MODE_BATCH: |
992 | batch = B_TRUE; | |
993 | flags |= TASKQ_THREADS_CPU_PCT; | |
dcb6bed1 | 994 | value = MIN(zio_taskq_batch_pct, 100); |
e8b96c60 | 995 | break; |
7ef5e54e | 996 | |
e8b96c60 MA |
997 | default: |
998 | panic("unrecognized mode for %s_%s taskq (%u:%u) in " | |
999 | "spa_activate()", | |
1000 | zio_type_name[t], zio_taskq_types[q], mode, value); | |
1001 | break; | |
1002 | } | |
7ef5e54e | 1003 | |
1c27024e | 1004 | for (uint_t i = 0; i < count; i++) { |
e8b96c60 | 1005 | taskq_t *tq; |
af430294 | 1006 | char name[32]; |
7ef5e54e | 1007 | |
af430294 MA |
1008 | (void) snprintf(name, sizeof (name), "%s_%s", |
1009 | zio_type_name[t], zio_taskq_types[q]); | |
7ef5e54e AL |
1010 | |
1011 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { | |
1012 | if (batch) | |
1013 | flags |= TASKQ_DC_BATCH; | |
1014 | ||
1015 | tq = taskq_create_sysdc(name, value, 50, INT_MAX, | |
1016 | spa->spa_proc, zio_taskq_basedc, flags); | |
1017 | } else { | |
e8b96c60 MA |
1018 | pri_t pri = maxclsyspri; |
1019 | /* | |
1020 | * The write issue taskq can be extremely CPU | |
1229323d BB |
1021 | * intensive. Run it at slightly less important |
1022 | * priority than the other taskqs. Under Linux this | |
1023 | * means incrementing the priority value on platforms | |
1024 | * like illumos it should be decremented. | |
e8b96c60 MA |
1025 | */ |
1026 | if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) | |
1229323d | 1027 | pri++; |
e8b96c60 MA |
1028 | |
1029 | tq = taskq_create_proc(name, value, pri, 50, | |
7ef5e54e AL |
1030 | INT_MAX, spa->spa_proc, flags); |
1031 | } | |
1032 | ||
1033 | tqs->stqs_taskq[i] = tq; | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | static void | |
1038 | spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
1039 | { | |
1040 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7ef5e54e AL |
1041 | |
1042 | if (tqs->stqs_taskq == NULL) { | |
1043 | ASSERT3U(tqs->stqs_count, ==, 0); | |
1044 | return; | |
1045 | } | |
1046 | ||
1c27024e | 1047 | for (uint_t i = 0; i < tqs->stqs_count; i++) { |
7ef5e54e AL |
1048 | ASSERT3P(tqs->stqs_taskq[i], !=, NULL); |
1049 | taskq_destroy(tqs->stqs_taskq[i]); | |
428870ff | 1050 | } |
34dc7c2f | 1051 | |
7ef5e54e AL |
1052 | kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); |
1053 | tqs->stqs_taskq = NULL; | |
1054 | } | |
34dc7c2f | 1055 | |
7ef5e54e AL |
1056 | /* |
1057 | * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. | |
1058 | * Note that a type may have multiple discrete taskqs to avoid lock contention | |
1059 | * on the taskq itself. In that case we choose which taskq at random by using | |
1060 | * the low bits of gethrtime(). | |
1061 | */ | |
1062 | void | |
1063 | spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1064 | task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) | |
1065 | { | |
1066 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1067 | taskq_t *tq; | |
1068 | ||
1069 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1070 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1071 | ||
1072 | if (tqs->stqs_count == 1) { | |
1073 | tq = tqs->stqs_taskq[0]; | |
1074 | } else { | |
c12936b1 | 1075 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
428870ff | 1076 | } |
7ef5e54e AL |
1077 | |
1078 | taskq_dispatch_ent(tq, func, arg, flags, ent); | |
428870ff BB |
1079 | } |
1080 | ||
044baf00 BB |
1081 | /* |
1082 | * Same as spa_taskq_dispatch_ent() but block on the task until completion. | |
1083 | */ | |
1084 | void | |
1085 | spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1086 | task_func_t *func, void *arg, uint_t flags) | |
1087 | { | |
1088 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1089 | taskq_t *tq; | |
1090 | taskqid_t id; | |
1091 | ||
1092 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1093 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1094 | ||
1095 | if (tqs->stqs_count == 1) { | |
1096 | tq = tqs->stqs_taskq[0]; | |
1097 | } else { | |
c12936b1 | 1098 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
044baf00 BB |
1099 | } |
1100 | ||
1101 | id = taskq_dispatch(tq, func, arg, flags); | |
1102 | if (id) | |
1103 | taskq_wait_id(tq, id); | |
1104 | } | |
1105 | ||
428870ff BB |
1106 | static void |
1107 | spa_create_zio_taskqs(spa_t *spa) | |
1108 | { | |
1c27024e DB |
1109 | for (int t = 0; t < ZIO_TYPES; t++) { |
1110 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1111 | spa_taskqs_init(spa, t, q); |
428870ff BB |
1112 | } |
1113 | } | |
1114 | } | |
9babb374 | 1115 | |
c25b8f99 BB |
1116 | /* |
1117 | * Disabled until spa_thread() can be adapted for Linux. | |
1118 | */ | |
1119 | #undef HAVE_SPA_THREAD | |
1120 | ||
7b89a549 | 1121 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) |
428870ff BB |
1122 | static void |
1123 | spa_thread(void *arg) | |
1124 | { | |
93ce2b4c | 1125 | psetid_t zio_taskq_psrset_bind = PS_NONE; |
428870ff | 1126 | callb_cpr_t cprinfo; |
9babb374 | 1127 | |
428870ff BB |
1128 | spa_t *spa = arg; |
1129 | user_t *pu = PTOU(curproc); | |
9babb374 | 1130 | |
428870ff BB |
1131 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, |
1132 | spa->spa_name); | |
9babb374 | 1133 | |
428870ff BB |
1134 | ASSERT(curproc != &p0); |
1135 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
1136 | "zpool-%s", spa->spa_name); | |
1137 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
1138 | ||
1139 | /* bind this thread to the requested psrset */ | |
1140 | if (zio_taskq_psrset_bind != PS_NONE) { | |
1141 | pool_lock(); | |
1142 | mutex_enter(&cpu_lock); | |
1143 | mutex_enter(&pidlock); | |
1144 | mutex_enter(&curproc->p_lock); | |
1145 | ||
1146 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
1147 | 0, NULL, NULL) == 0) { | |
1148 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
1149 | } else { | |
1150 | cmn_err(CE_WARN, | |
1151 | "Couldn't bind process for zfs pool \"%s\" to " | |
1152 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
1153 | } | |
1154 | ||
1155 | mutex_exit(&curproc->p_lock); | |
1156 | mutex_exit(&pidlock); | |
1157 | mutex_exit(&cpu_lock); | |
1158 | pool_unlock(); | |
1159 | } | |
1160 | ||
1161 | if (zio_taskq_sysdc) { | |
1162 | sysdc_thread_enter(curthread, 100, 0); | |
1163 | } | |
1164 | ||
1165 | spa->spa_proc = curproc; | |
1166 | spa->spa_did = curthread->t_did; | |
1167 | ||
1168 | spa_create_zio_taskqs(spa); | |
1169 | ||
1170 | mutex_enter(&spa->spa_proc_lock); | |
1171 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
1172 | ||
1173 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
1174 | cv_broadcast(&spa->spa_proc_cv); | |
1175 | ||
1176 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
1177 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
1178 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1179 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
1180 | ||
1181 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
1182 | spa->spa_proc_state = SPA_PROC_GONE; | |
1183 | spa->spa_proc = &p0; | |
1184 | cv_broadcast(&spa->spa_proc_cv); | |
1185 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
1186 | ||
1187 | mutex_enter(&curproc->p_lock); | |
1188 | lwp_exit(); | |
1189 | } | |
1190 | #endif | |
1191 | ||
1192 | /* | |
1193 | * Activate an uninitialized pool. | |
1194 | */ | |
1195 | static void | |
da92d5cb | 1196 | spa_activate(spa_t *spa, spa_mode_t mode) |
428870ff BB |
1197 | { |
1198 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); | |
1199 | ||
1200 | spa->spa_state = POOL_STATE_ACTIVE; | |
1201 | spa->spa_mode = mode; | |
1202 | ||
1203 | spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
1204 | spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
cc99f275 DB |
1205 | spa->spa_special_class = metaslab_class_create(spa, zfs_metaslab_ops); |
1206 | spa->spa_dedup_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
428870ff BB |
1207 | |
1208 | /* Try to create a covering process */ | |
1209 | mutex_enter(&spa->spa_proc_lock); | |
1210 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
1211 | ASSERT(spa->spa_proc == &p0); | |
1212 | spa->spa_did = 0; | |
1213 | ||
7b89a549 | 1214 | #ifdef HAVE_SPA_THREAD |
428870ff BB |
1215 | /* Only create a process if we're going to be around a while. */ |
1216 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
1217 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
1218 | NULL, 0) == 0) { | |
1219 | spa->spa_proc_state = SPA_PROC_CREATED; | |
1220 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
1221 | cv_wait(&spa->spa_proc_cv, | |
1222 | &spa->spa_proc_lock); | |
9babb374 | 1223 | } |
428870ff BB |
1224 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); |
1225 | ASSERT(spa->spa_proc != &p0); | |
1226 | ASSERT(spa->spa_did != 0); | |
1227 | } else { | |
1228 | #ifdef _KERNEL | |
1229 | cmn_err(CE_WARN, | |
1230 | "Couldn't create process for zfs pool \"%s\"\n", | |
1231 | spa->spa_name); | |
1232 | #endif | |
b128c09f | 1233 | } |
34dc7c2f | 1234 | } |
7b89a549 | 1235 | #endif /* HAVE_SPA_THREAD */ |
428870ff BB |
1236 | mutex_exit(&spa->spa_proc_lock); |
1237 | ||
1238 | /* If we didn't create a process, we need to create our taskqs. */ | |
1239 | if (spa->spa_proc == &p0) { | |
1240 | spa_create_zio_taskqs(spa); | |
1241 | } | |
34dc7c2f | 1242 | |
619f0976 GW |
1243 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1244 | spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, | |
1245 | ZIO_FLAG_CANFAIL); | |
1246 | } | |
a1d477c2 | 1247 | |
b128c09f BB |
1248 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), |
1249 | offsetof(vdev_t, vdev_config_dirty_node)); | |
0c66c32d JG |
1250 | list_create(&spa->spa_evicting_os_list, sizeof (objset_t), |
1251 | offsetof(objset_t, os_evicting_node)); | |
b128c09f BB |
1252 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), |
1253 | offsetof(vdev_t, vdev_state_dirty_node)); | |
34dc7c2f | 1254 | |
4747a7d3 | 1255 | txg_list_create(&spa->spa_vdev_txg_list, spa, |
34dc7c2f BB |
1256 | offsetof(struct vdev, vdev_txg_node)); |
1257 | ||
1258 | avl_create(&spa->spa_errlist_scrub, | |
1259 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1260 | offsetof(spa_error_entry_t, se_avl)); | |
1261 | avl_create(&spa->spa_errlist_last, | |
1262 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1263 | offsetof(spa_error_entry_t, se_avl)); | |
a0bd735a | 1264 | |
b5256303 TC |
1265 | spa_keystore_init(&spa->spa_keystore); |
1266 | ||
a0bd735a BP |
1267 | /* |
1268 | * This taskq is used to perform zvol-minor-related tasks | |
1269 | * asynchronously. This has several advantages, including easy | |
1270 | * resolution of various deadlocks (zfsonlinux bug #3681). | |
1271 | * | |
1272 | * The taskq must be single threaded to ensure tasks are always | |
1273 | * processed in the order in which they were dispatched. | |
1274 | * | |
1275 | * A taskq per pool allows one to keep the pools independent. | |
1276 | * This way if one pool is suspended, it will not impact another. | |
1277 | * | |
1278 | * The preferred location to dispatch a zvol minor task is a sync | |
1279 | * task. In this context, there is easy access to the spa_t and minimal | |
1280 | * error handling is required because the sync task must succeed. | |
1281 | */ | |
1282 | spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri, | |
1283 | 1, INT_MAX, 0); | |
1de321e6 | 1284 | |
77d8a0f1 | 1285 | /* |
1286 | * Taskq dedicated to prefetcher threads: this is used to prevent the | |
1287 | * pool traverse code from monopolizing the global (and limited) | |
1288 | * system_taskq by inappropriately scheduling long running tasks on it. | |
1289 | */ | |
1290 | spa->spa_prefetch_taskq = taskq_create("z_prefetch", boot_ncpus, | |
1291 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); | |
1292 | ||
1de321e6 JX |
1293 | /* |
1294 | * The taskq to upgrade datasets in this pool. Currently used by | |
9c5167d1 | 1295 | * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA. |
1de321e6 JX |
1296 | */ |
1297 | spa->spa_upgrade_taskq = taskq_create("z_upgrade", boot_ncpus, | |
1298 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); | |
34dc7c2f BB |
1299 | } |
1300 | ||
1301 | /* | |
1302 | * Opposite of spa_activate(). | |
1303 | */ | |
1304 | static void | |
1305 | spa_deactivate(spa_t *spa) | |
1306 | { | |
34dc7c2f BB |
1307 | ASSERT(spa->spa_sync_on == B_FALSE); |
1308 | ASSERT(spa->spa_dsl_pool == NULL); | |
1309 | ASSERT(spa->spa_root_vdev == NULL); | |
9babb374 | 1310 | ASSERT(spa->spa_async_zio_root == NULL); |
34dc7c2f BB |
1311 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); |
1312 | ||
0c66c32d JG |
1313 | spa_evicting_os_wait(spa); |
1314 | ||
a0bd735a BP |
1315 | if (spa->spa_zvol_taskq) { |
1316 | taskq_destroy(spa->spa_zvol_taskq); | |
1317 | spa->spa_zvol_taskq = NULL; | |
1318 | } | |
1319 | ||
77d8a0f1 | 1320 | if (spa->spa_prefetch_taskq) { |
1321 | taskq_destroy(spa->spa_prefetch_taskq); | |
1322 | spa->spa_prefetch_taskq = NULL; | |
1323 | } | |
1324 | ||
1de321e6 JX |
1325 | if (spa->spa_upgrade_taskq) { |
1326 | taskq_destroy(spa->spa_upgrade_taskq); | |
1327 | spa->spa_upgrade_taskq = NULL; | |
1328 | } | |
1329 | ||
34dc7c2f BB |
1330 | txg_list_destroy(&spa->spa_vdev_txg_list); |
1331 | ||
b128c09f | 1332 | list_destroy(&spa->spa_config_dirty_list); |
0c66c32d | 1333 | list_destroy(&spa->spa_evicting_os_list); |
b128c09f | 1334 | list_destroy(&spa->spa_state_dirty_list); |
34dc7c2f | 1335 | |
57ddcda1 | 1336 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 | 1337 | |
1c27024e DB |
1338 | for (int t = 0; t < ZIO_TYPES; t++) { |
1339 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1340 | spa_taskqs_fini(spa, t, q); |
b128c09f | 1341 | } |
34dc7c2f BB |
1342 | } |
1343 | ||
a1d477c2 MA |
1344 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1345 | ASSERT3P(spa->spa_txg_zio[i], !=, NULL); | |
1346 | VERIFY0(zio_wait(spa->spa_txg_zio[i])); | |
1347 | spa->spa_txg_zio[i] = NULL; | |
1348 | } | |
1349 | ||
34dc7c2f BB |
1350 | metaslab_class_destroy(spa->spa_normal_class); |
1351 | spa->spa_normal_class = NULL; | |
1352 | ||
1353 | metaslab_class_destroy(spa->spa_log_class); | |
1354 | spa->spa_log_class = NULL; | |
1355 | ||
cc99f275 DB |
1356 | metaslab_class_destroy(spa->spa_special_class); |
1357 | spa->spa_special_class = NULL; | |
1358 | ||
1359 | metaslab_class_destroy(spa->spa_dedup_class); | |
1360 | spa->spa_dedup_class = NULL; | |
1361 | ||
34dc7c2f BB |
1362 | /* |
1363 | * If this was part of an import or the open otherwise failed, we may | |
1364 | * still have errors left in the queues. Empty them just in case. | |
1365 | */ | |
1366 | spa_errlog_drain(spa); | |
34dc7c2f BB |
1367 | avl_destroy(&spa->spa_errlist_scrub); |
1368 | avl_destroy(&spa->spa_errlist_last); | |
1369 | ||
b5256303 TC |
1370 | spa_keystore_fini(&spa->spa_keystore); |
1371 | ||
34dc7c2f | 1372 | spa->spa_state = POOL_STATE_UNINITIALIZED; |
428870ff BB |
1373 | |
1374 | mutex_enter(&spa->spa_proc_lock); | |
1375 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1376 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1377 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1378 | cv_broadcast(&spa->spa_proc_cv); | |
1379 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1380 | ASSERT(spa->spa_proc != &p0); | |
1381 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1382 | } | |
1383 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1384 | spa->spa_proc_state = SPA_PROC_NONE; | |
1385 | } | |
1386 | ASSERT(spa->spa_proc == &p0); | |
1387 | mutex_exit(&spa->spa_proc_lock); | |
1388 | ||
1389 | /* | |
1390 | * We want to make sure spa_thread() has actually exited the ZFS | |
1391 | * module, so that the module can't be unloaded out from underneath | |
1392 | * it. | |
1393 | */ | |
1394 | if (spa->spa_did != 0) { | |
1395 | thread_join(spa->spa_did); | |
1396 | spa->spa_did = 0; | |
1397 | } | |
34dc7c2f BB |
1398 | } |
1399 | ||
1400 | /* | |
1401 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1402 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1403 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1404 | * All vdev validation is done by the vdev_alloc() routine. | |
1405 | */ | |
4a22ba5b | 1406 | int |
34dc7c2f BB |
1407 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, |
1408 | uint_t id, int atype) | |
1409 | { | |
1410 | nvlist_t **child; | |
9babb374 | 1411 | uint_t children; |
34dc7c2f BB |
1412 | int error; |
1413 | ||
1414 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1415 | return (error); | |
1416 | ||
1417 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1418 | return (0); | |
1419 | ||
b128c09f BB |
1420 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1421 | &child, &children); | |
1422 | ||
1423 | if (error == ENOENT) | |
1424 | return (0); | |
1425 | ||
1426 | if (error) { | |
34dc7c2f BB |
1427 | vdev_free(*vdp); |
1428 | *vdp = NULL; | |
2e528b49 | 1429 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1430 | } |
1431 | ||
1c27024e | 1432 | for (int c = 0; c < children; c++) { |
34dc7c2f BB |
1433 | vdev_t *vd; |
1434 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1435 | atype)) != 0) { | |
1436 | vdev_free(*vdp); | |
1437 | *vdp = NULL; | |
1438 | return (error); | |
1439 | } | |
1440 | } | |
1441 | ||
1442 | ASSERT(*vdp != NULL); | |
1443 | ||
1444 | return (0); | |
1445 | } | |
1446 | ||
93e28d66 SD |
1447 | static boolean_t |
1448 | spa_should_flush_logs_on_unload(spa_t *spa) | |
1449 | { | |
1450 | if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) | |
1451 | return (B_FALSE); | |
1452 | ||
1453 | if (!spa_writeable(spa)) | |
1454 | return (B_FALSE); | |
1455 | ||
1456 | if (!spa->spa_sync_on) | |
1457 | return (B_FALSE); | |
1458 | ||
1459 | if (spa_state(spa) != POOL_STATE_EXPORTED) | |
1460 | return (B_FALSE); | |
1461 | ||
1462 | if (zfs_keep_log_spacemaps_at_export) | |
1463 | return (B_FALSE); | |
1464 | ||
1465 | return (B_TRUE); | |
1466 | } | |
1467 | ||
1468 | /* | |
1469 | * Opens a transaction that will set the flag that will instruct | |
1470 | * spa_sync to attempt to flush all the metaslabs for that txg. | |
1471 | */ | |
1472 | static void | |
1473 | spa_unload_log_sm_flush_all(spa_t *spa) | |
1474 | { | |
1475 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
1476 | VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); | |
1477 | ||
1478 | ASSERT3U(spa->spa_log_flushall_txg, ==, 0); | |
1479 | spa->spa_log_flushall_txg = dmu_tx_get_txg(tx); | |
1480 | ||
1481 | dmu_tx_commit(tx); | |
1482 | txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg); | |
1483 | } | |
1484 | ||
1485 | static void | |
1486 | spa_unload_log_sm_metadata(spa_t *spa) | |
1487 | { | |
1488 | void *cookie = NULL; | |
1489 | spa_log_sm_t *sls; | |
1490 | while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg, | |
1491 | &cookie)) != NULL) { | |
1492 | VERIFY0(sls->sls_mscount); | |
1493 | kmem_free(sls, sizeof (spa_log_sm_t)); | |
1494 | } | |
1495 | ||
1496 | for (log_summary_entry_t *e = list_head(&spa->spa_log_summary); | |
1497 | e != NULL; e = list_head(&spa->spa_log_summary)) { | |
1498 | VERIFY0(e->lse_mscount); | |
1499 | list_remove(&spa->spa_log_summary, e); | |
1500 | kmem_free(e, sizeof (log_summary_entry_t)); | |
1501 | } | |
1502 | ||
1503 | spa->spa_unflushed_stats.sus_nblocks = 0; | |
1504 | spa->spa_unflushed_stats.sus_memused = 0; | |
1505 | spa->spa_unflushed_stats.sus_blocklimit = 0; | |
1506 | } | |
1507 | ||
37f03da8 SH |
1508 | static void |
1509 | spa_destroy_aux_threads(spa_t *spa) | |
1510 | { | |
1511 | if (spa->spa_condense_zthr != NULL) { | |
1512 | zthr_destroy(spa->spa_condense_zthr); | |
1513 | spa->spa_condense_zthr = NULL; | |
1514 | } | |
1515 | if (spa->spa_checkpoint_discard_zthr != NULL) { | |
1516 | zthr_destroy(spa->spa_checkpoint_discard_zthr); | |
1517 | spa->spa_checkpoint_discard_zthr = NULL; | |
1518 | } | |
1519 | if (spa->spa_livelist_delete_zthr != NULL) { | |
1520 | zthr_destroy(spa->spa_livelist_delete_zthr); | |
1521 | spa->spa_livelist_delete_zthr = NULL; | |
1522 | } | |
1523 | if (spa->spa_livelist_condense_zthr != NULL) { | |
1524 | zthr_destroy(spa->spa_livelist_condense_zthr); | |
1525 | spa->spa_livelist_condense_zthr = NULL; | |
1526 | } | |
1527 | } | |
1528 | ||
34dc7c2f BB |
1529 | /* |
1530 | * Opposite of spa_load(). | |
1531 | */ | |
1532 | static void | |
1533 | spa_unload(spa_t *spa) | |
1534 | { | |
b128c09f | 1535 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
93e28d66 | 1536 | ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED); |
b128c09f | 1537 | |
ca95f70d | 1538 | spa_import_progress_remove(spa_guid(spa)); |
4a0ee12a PZ |
1539 | spa_load_note(spa, "UNLOADING"); |
1540 | ||
e60e158e JG |
1541 | spa_wake_waiters(spa); |
1542 | ||
93e28d66 SD |
1543 | /* |
1544 | * If the log space map feature is enabled and the pool is getting | |
1545 | * exported (but not destroyed), we want to spend some time flushing | |
1546 | * as many metaslabs as we can in an attempt to destroy log space | |
1547 | * maps and save import time. | |
1548 | */ | |
1549 | if (spa_should_flush_logs_on_unload(spa)) | |
1550 | spa_unload_log_sm_flush_all(spa); | |
1551 | ||
34dc7c2f BB |
1552 | /* |
1553 | * Stop async tasks. | |
1554 | */ | |
1555 | spa_async_suspend(spa); | |
1556 | ||
619f0976 | 1557 | if (spa->spa_root_vdev) { |
1b939560 BB |
1558 | vdev_t *root_vdev = spa->spa_root_vdev; |
1559 | vdev_initialize_stop_all(root_vdev, VDEV_INITIALIZE_ACTIVE); | |
1560 | vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE); | |
1561 | vdev_autotrim_stop_all(spa); | |
9a49d3f3 | 1562 | vdev_rebuild_stop_all(spa); |
619f0976 GW |
1563 | } |
1564 | ||
34dc7c2f BB |
1565 | /* |
1566 | * Stop syncing. | |
1567 | */ | |
1568 | if (spa->spa_sync_on) { | |
1569 | txg_sync_stop(spa->spa_dsl_pool); | |
1570 | spa->spa_sync_on = B_FALSE; | |
1571 | } | |
1572 | ||
4e21fd06 | 1573 | /* |
93e28d66 SD |
1574 | * This ensures that there is no async metaslab prefetching |
1575 | * while we attempt to unload the spa. | |
4e21fd06 DB |
1576 | */ |
1577 | if (spa->spa_root_vdev != NULL) { | |
93e28d66 SD |
1578 | for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) { |
1579 | vdev_t *vc = spa->spa_root_vdev->vdev_child[c]; | |
1580 | if (vc->vdev_mg != NULL) | |
1581 | taskq_wait(vc->vdev_mg->mg_taskq); | |
1582 | } | |
4e21fd06 DB |
1583 | } |
1584 | ||
379ca9cf OF |
1585 | if (spa->spa_mmp.mmp_thread) |
1586 | mmp_thread_stop(spa); | |
1587 | ||
34dc7c2f | 1588 | /* |
b128c09f | 1589 | * Wait for any outstanding async I/O to complete. |
34dc7c2f | 1590 | */ |
9babb374 | 1591 | if (spa->spa_async_zio_root != NULL) { |
1c27024e | 1592 | for (int i = 0; i < max_ncpus; i++) |
e022864d MA |
1593 | (void) zio_wait(spa->spa_async_zio_root[i]); |
1594 | kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); | |
9babb374 BB |
1595 | spa->spa_async_zio_root = NULL; |
1596 | } | |
34dc7c2f | 1597 | |
a1d477c2 MA |
1598 | if (spa->spa_vdev_removal != NULL) { |
1599 | spa_vdev_removal_destroy(spa->spa_vdev_removal); | |
1600 | spa->spa_vdev_removal = NULL; | |
1601 | } | |
1602 | ||
37f03da8 | 1603 | spa_destroy_aux_threads(spa); |
d2734cce | 1604 | |
a1d477c2 MA |
1605 | spa_condense_fini(spa); |
1606 | ||
428870ff BB |
1607 | bpobj_close(&spa->spa_deferred_bpobj); |
1608 | ||
619f0976 | 1609 | spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); |
93cf2076 GW |
1610 | |
1611 | /* | |
1612 | * Close all vdevs. | |
1613 | */ | |
1614 | if (spa->spa_root_vdev) | |
1615 | vdev_free(spa->spa_root_vdev); | |
1616 | ASSERT(spa->spa_root_vdev == NULL); | |
1617 | ||
34dc7c2f BB |
1618 | /* |
1619 | * Close the dsl pool. | |
1620 | */ | |
1621 | if (spa->spa_dsl_pool) { | |
1622 | dsl_pool_close(spa->spa_dsl_pool); | |
1623 | spa->spa_dsl_pool = NULL; | |
428870ff | 1624 | spa->spa_meta_objset = NULL; |
34dc7c2f BB |
1625 | } |
1626 | ||
428870ff | 1627 | ddt_unload(spa); |
93e28d66 | 1628 | spa_unload_log_sm_metadata(spa); |
428870ff | 1629 | |
fb5f0bc8 BB |
1630 | /* |
1631 | * Drop and purge level 2 cache | |
1632 | */ | |
1633 | spa_l2cache_drop(spa); | |
1634 | ||
93e28d66 | 1635 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
34dc7c2f BB |
1636 | vdev_free(spa->spa_spares.sav_vdevs[i]); |
1637 | if (spa->spa_spares.sav_vdevs) { | |
1638 | kmem_free(spa->spa_spares.sav_vdevs, | |
1639 | spa->spa_spares.sav_count * sizeof (void *)); | |
1640 | spa->spa_spares.sav_vdevs = NULL; | |
1641 | } | |
1642 | if (spa->spa_spares.sav_config) { | |
1643 | nvlist_free(spa->spa_spares.sav_config); | |
1644 | spa->spa_spares.sav_config = NULL; | |
1645 | } | |
b128c09f | 1646 | spa->spa_spares.sav_count = 0; |
34dc7c2f | 1647 | |
93e28d66 | 1648 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { |
5ffb9d1d | 1649 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); |
34dc7c2f | 1650 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); |
5ffb9d1d | 1651 | } |
34dc7c2f BB |
1652 | if (spa->spa_l2cache.sav_vdevs) { |
1653 | kmem_free(spa->spa_l2cache.sav_vdevs, | |
1654 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1655 | spa->spa_l2cache.sav_vdevs = NULL; | |
1656 | } | |
1657 | if (spa->spa_l2cache.sav_config) { | |
1658 | nvlist_free(spa->spa_l2cache.sav_config); | |
1659 | spa->spa_l2cache.sav_config = NULL; | |
1660 | } | |
b128c09f | 1661 | spa->spa_l2cache.sav_count = 0; |
34dc7c2f BB |
1662 | |
1663 | spa->spa_async_suspended = 0; | |
fb5f0bc8 | 1664 | |
a1d477c2 MA |
1665 | spa->spa_indirect_vdevs_loaded = B_FALSE; |
1666 | ||
d96eb2b1 DM |
1667 | if (spa->spa_comment != NULL) { |
1668 | spa_strfree(spa->spa_comment); | |
1669 | spa->spa_comment = NULL; | |
1670 | } | |
1671 | ||
619f0976 | 1672 | spa_config_exit(spa, SCL_ALL, spa); |
34dc7c2f BB |
1673 | } |
1674 | ||
1675 | /* | |
1676 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1677 | * this pool. When this is called, we have some form of basic information in | |
1678 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1679 | * then re-generate a more complete list including status information. | |
1680 | */ | |
a1d477c2 | 1681 | void |
34dc7c2f BB |
1682 | spa_load_spares(spa_t *spa) |
1683 | { | |
1684 | nvlist_t **spares; | |
1685 | uint_t nspares; | |
1686 | int i; | |
1687 | vdev_t *vd, *tvd; | |
1688 | ||
d2734cce SD |
1689 | #ifndef _KERNEL |
1690 | /* | |
1691 | * zdb opens both the current state of the pool and the | |
1692 | * checkpointed state (if present), with a different spa_t. | |
1693 | * | |
1694 | * As spare vdevs are shared among open pools, we skip loading | |
1695 | * them when we load the checkpointed state of the pool. | |
1696 | */ | |
1697 | if (!spa_writeable(spa)) | |
1698 | return; | |
1699 | #endif | |
1700 | ||
b128c09f BB |
1701 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1702 | ||
34dc7c2f BB |
1703 | /* |
1704 | * First, close and free any existing spare vdevs. | |
1705 | */ | |
1706 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1707 | vd = spa->spa_spares.sav_vdevs[i]; | |
1708 | ||
1709 | /* Undo the call to spa_activate() below */ | |
b128c09f BB |
1710 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1711 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
34dc7c2f BB |
1712 | spa_spare_remove(tvd); |
1713 | vdev_close(vd); | |
1714 | vdev_free(vd); | |
1715 | } | |
1716 | ||
1717 | if (spa->spa_spares.sav_vdevs) | |
1718 | kmem_free(spa->spa_spares.sav_vdevs, | |
1719 | spa->spa_spares.sav_count * sizeof (void *)); | |
1720 | ||
1721 | if (spa->spa_spares.sav_config == NULL) | |
1722 | nspares = 0; | |
1723 | else | |
1724 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
1725 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
1726 | ||
1727 | spa->spa_spares.sav_count = (int)nspares; | |
1728 | spa->spa_spares.sav_vdevs = NULL; | |
1729 | ||
1730 | if (nspares == 0) | |
1731 | return; | |
1732 | ||
1733 | /* | |
1734 | * Construct the array of vdevs, opening them to get status in the | |
1735 | * process. For each spare, there is potentially two different vdev_t | |
1736 | * structures associated with it: one in the list of spares (used only | |
1737 | * for basic validation purposes) and one in the active vdev | |
1738 | * configuration (if it's spared in). During this phase we open and | |
1739 | * validate each vdev on the spare list. If the vdev also exists in the | |
1740 | * active configuration, then we also mark this vdev as an active spare. | |
1741 | */ | |
904ea276 | 1742 | spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *), |
79c76d5b | 1743 | KM_SLEEP); |
34dc7c2f BB |
1744 | for (i = 0; i < spa->spa_spares.sav_count; i++) { |
1745 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1746 | VDEV_ALLOC_SPARE) == 0); | |
1747 | ASSERT(vd != NULL); | |
1748 | ||
1749 | spa->spa_spares.sav_vdevs[i] = vd; | |
1750 | ||
b128c09f BB |
1751 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1752 | B_FALSE)) != NULL) { | |
34dc7c2f BB |
1753 | if (!tvd->vdev_isspare) |
1754 | spa_spare_add(tvd); | |
1755 | ||
1756 | /* | |
1757 | * We only mark the spare active if we were successfully | |
1758 | * able to load the vdev. Otherwise, importing a pool | |
1759 | * with a bad active spare would result in strange | |
1760 | * behavior, because multiple pool would think the spare | |
1761 | * is actively in use. | |
1762 | * | |
1763 | * There is a vulnerability here to an equally bizarre | |
1764 | * circumstance, where a dead active spare is later | |
1765 | * brought back to life (onlined or otherwise). Given | |
1766 | * the rarity of this scenario, and the extra complexity | |
1767 | * it adds, we ignore the possibility. | |
1768 | */ | |
1769 | if (!vdev_is_dead(tvd)) | |
1770 | spa_spare_activate(tvd); | |
1771 | } | |
1772 | ||
b128c09f | 1773 | vd->vdev_top = vd; |
9babb374 | 1774 | vd->vdev_aux = &spa->spa_spares; |
b128c09f | 1775 | |
34dc7c2f BB |
1776 | if (vdev_open(vd) != 0) |
1777 | continue; | |
1778 | ||
34dc7c2f BB |
1779 | if (vdev_validate_aux(vd) == 0) |
1780 | spa_spare_add(vd); | |
1781 | } | |
1782 | ||
1783 | /* | |
1784 | * Recompute the stashed list of spares, with status information | |
1785 | * this time. | |
1786 | */ | |
1787 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, | |
1788 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1789 | ||
1790 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
79c76d5b | 1791 | KM_SLEEP); |
34dc7c2f BB |
1792 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1793 | spares[i] = vdev_config_generate(spa, | |
428870ff | 1794 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); |
34dc7c2f BB |
1795 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
1796 | ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); | |
1797 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1798 | nvlist_free(spares[i]); | |
1799 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1800 | } | |
1801 | ||
1802 | /* | |
1803 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1804 | * this pool. When this is called, we have some form of basic information in | |
1805 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1806 | * then re-generate a more complete list including status information. | |
1807 | * Devices which are already active have their details maintained, and are | |
1808 | * not re-opened. | |
1809 | */ | |
a1d477c2 | 1810 | void |
34dc7c2f BB |
1811 | spa_load_l2cache(spa_t *spa) |
1812 | { | |
460f239e | 1813 | nvlist_t **l2cache = NULL; |
34dc7c2f BB |
1814 | uint_t nl2cache; |
1815 | int i, j, oldnvdevs; | |
9babb374 | 1816 | uint64_t guid; |
a117a6d6 | 1817 | vdev_t *vd, **oldvdevs, **newvdevs; |
34dc7c2f BB |
1818 | spa_aux_vdev_t *sav = &spa->spa_l2cache; |
1819 | ||
d2734cce SD |
1820 | #ifndef _KERNEL |
1821 | /* | |
1822 | * zdb opens both the current state of the pool and the | |
1823 | * checkpointed state (if present), with a different spa_t. | |
1824 | * | |
1825 | * As L2 caches are part of the ARC which is shared among open | |
1826 | * pools, we skip loading them when we load the checkpointed | |
1827 | * state of the pool. | |
1828 | */ | |
1829 | if (!spa_writeable(spa)) | |
1830 | return; | |
1831 | #endif | |
1832 | ||
b128c09f BB |
1833 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1834 | ||
34dc7c2f BB |
1835 | oldvdevs = sav->sav_vdevs; |
1836 | oldnvdevs = sav->sav_count; | |
1837 | sav->sav_vdevs = NULL; | |
1838 | sav->sav_count = 0; | |
1839 | ||
67d60824 NB |
1840 | if (sav->sav_config == NULL) { |
1841 | nl2cache = 0; | |
1842 | newvdevs = NULL; | |
1843 | goto out; | |
1844 | } | |
1845 | ||
1846 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, | |
1847 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
1848 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); | |
1849 | ||
34dc7c2f BB |
1850 | /* |
1851 | * Process new nvlist of vdevs. | |
1852 | */ | |
1853 | for (i = 0; i < nl2cache; i++) { | |
1854 | VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, | |
1855 | &guid) == 0); | |
1856 | ||
1857 | newvdevs[i] = NULL; | |
1858 | for (j = 0; j < oldnvdevs; j++) { | |
1859 | vd = oldvdevs[j]; | |
1860 | if (vd != NULL && guid == vd->vdev_guid) { | |
1861 | /* | |
1862 | * Retain previous vdev for add/remove ops. | |
1863 | */ | |
1864 | newvdevs[i] = vd; | |
1865 | oldvdevs[j] = NULL; | |
1866 | break; | |
1867 | } | |
1868 | } | |
1869 | ||
1870 | if (newvdevs[i] == NULL) { | |
1871 | /* | |
1872 | * Create new vdev | |
1873 | */ | |
1874 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
1875 | VDEV_ALLOC_L2CACHE) == 0); | |
1876 | ASSERT(vd != NULL); | |
1877 | newvdevs[i] = vd; | |
1878 | ||
1879 | /* | |
1880 | * Commit this vdev as an l2cache device, | |
1881 | * even if it fails to open. | |
1882 | */ | |
1883 | spa_l2cache_add(vd); | |
1884 | ||
b128c09f BB |
1885 | vd->vdev_top = vd; |
1886 | vd->vdev_aux = sav; | |
1887 | ||
1888 | spa_l2cache_activate(vd); | |
1889 | ||
34dc7c2f BB |
1890 | if (vdev_open(vd) != 0) |
1891 | continue; | |
1892 | ||
34dc7c2f BB |
1893 | (void) vdev_validate_aux(vd); |
1894 | ||
9babb374 BB |
1895 | if (!vdev_is_dead(vd)) |
1896 | l2arc_add_vdev(spa, vd); | |
b7654bd7 GA |
1897 | |
1898 | /* | |
1899 | * Upon cache device addition to a pool or pool | |
1900 | * creation with a cache device or if the header | |
1901 | * of the device is invalid we issue an async | |
1902 | * TRIM command for the whole device which will | |
1903 | * execute if l2arc_trim_ahead > 0. | |
1904 | */ | |
1905 | spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM); | |
34dc7c2f BB |
1906 | } |
1907 | } | |
1908 | ||
67d60824 NB |
1909 | sav->sav_vdevs = newvdevs; |
1910 | sav->sav_count = (int)nl2cache; | |
1911 | ||
1912 | /* | |
1913 | * Recompute the stashed list of l2cache devices, with status | |
1914 | * information this time. | |
1915 | */ | |
1916 | VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, | |
1917 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1918 | ||
460f239e D |
1919 | if (sav->sav_count > 0) |
1920 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), | |
1921 | KM_SLEEP); | |
67d60824 NB |
1922 | for (i = 0; i < sav->sav_count; i++) |
1923 | l2cache[i] = vdev_config_generate(spa, | |
1924 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); | |
1925 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
1926 | ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); | |
1927 | ||
1928 | out: | |
34dc7c2f BB |
1929 | /* |
1930 | * Purge vdevs that were dropped | |
1931 | */ | |
1932 | for (i = 0; i < oldnvdevs; i++) { | |
1933 | uint64_t pool; | |
1934 | ||
1935 | vd = oldvdevs[i]; | |
1936 | if (vd != NULL) { | |
5ffb9d1d GW |
1937 | ASSERT(vd->vdev_isl2cache); |
1938 | ||
fb5f0bc8 BB |
1939 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
1940 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 1941 | l2arc_remove_vdev(vd); |
5ffb9d1d GW |
1942 | vdev_clear_stats(vd); |
1943 | vdev_free(vd); | |
34dc7c2f BB |
1944 | } |
1945 | } | |
1946 | ||
1947 | if (oldvdevs) | |
1948 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); | |
1949 | ||
34dc7c2f BB |
1950 | for (i = 0; i < sav->sav_count; i++) |
1951 | nvlist_free(l2cache[i]); | |
1952 | if (sav->sav_count) | |
1953 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
1954 | } | |
1955 | ||
1956 | static int | |
1957 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
1958 | { | |
1959 | dmu_buf_t *db; | |
1960 | char *packed = NULL; | |
1961 | size_t nvsize = 0; | |
1962 | int error; | |
1963 | *value = NULL; | |
1964 | ||
c3275b56 BB |
1965 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); |
1966 | if (error) | |
1967 | return (error); | |
1968 | ||
34dc7c2f BB |
1969 | nvsize = *(uint64_t *)db->db_data; |
1970 | dmu_buf_rele(db, FTAG); | |
1971 | ||
77aef6f6 | 1972 | packed = vmem_alloc(nvsize, KM_SLEEP); |
9babb374 BB |
1973 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, |
1974 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
1975 | if (error == 0) |
1976 | error = nvlist_unpack(packed, nvsize, value, 0); | |
77aef6f6 | 1977 | vmem_free(packed, nvsize); |
34dc7c2f BB |
1978 | |
1979 | return (error); | |
1980 | } | |
1981 | ||
6cb8e530 PZ |
1982 | /* |
1983 | * Concrete top-level vdevs that are not missing and are not logs. At every | |
1984 | * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. | |
1985 | */ | |
1986 | static uint64_t | |
1987 | spa_healthy_core_tvds(spa_t *spa) | |
1988 | { | |
1989 | vdev_t *rvd = spa->spa_root_vdev; | |
1990 | uint64_t tvds = 0; | |
1991 | ||
1992 | for (uint64_t i = 0; i < rvd->vdev_children; i++) { | |
1993 | vdev_t *vd = rvd->vdev_child[i]; | |
1994 | if (vd->vdev_islog) | |
1995 | continue; | |
1996 | if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) | |
1997 | tvds++; | |
1998 | } | |
1999 | ||
2000 | return (tvds); | |
2001 | } | |
2002 | ||
34dc7c2f BB |
2003 | /* |
2004 | * Checks to see if the given vdev could not be opened, in which case we post a | |
2005 | * sysevent to notify the autoreplace code that the device has been removed. | |
2006 | */ | |
2007 | static void | |
2008 | spa_check_removed(vdev_t *vd) | |
2009 | { | |
6cb8e530 | 2010 | for (uint64_t c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
2011 | spa_check_removed(vd->vdev_child[c]); |
2012 | ||
7011fb60 | 2013 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && |
a1d477c2 | 2014 | vdev_is_concrete(vd)) { |
fb390aaf | 2015 | zfs_post_autoreplace(vd->vdev_spa, vd); |
12fa0466 | 2016 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); |
34dc7c2f BB |
2017 | } |
2018 | } | |
2019 | ||
6cb8e530 PZ |
2020 | static int |
2021 | spa_check_for_missing_logs(spa_t *spa) | |
9babb374 | 2022 | { |
6cb8e530 | 2023 | vdev_t *rvd = spa->spa_root_vdev; |
9babb374 | 2024 | |
428870ff | 2025 | /* |
572e2857 | 2026 | * If we're doing a normal import, then build up any additional |
6cb8e530 | 2027 | * diagnostic information about missing log devices. |
572e2857 | 2028 | * We'll pass this up to the user for further processing. |
428870ff | 2029 | */ |
572e2857 BB |
2030 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { |
2031 | nvlist_t **child, *nv; | |
2032 | uint64_t idx = 0; | |
2033 | ||
160987b5 | 2034 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *), |
79c76d5b BB |
2035 | KM_SLEEP); |
2036 | VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
572e2857 | 2037 | |
6cb8e530 | 2038 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { |
572e2857 | 2039 | vdev_t *tvd = rvd->vdev_child[c]; |
572e2857 | 2040 | |
6cb8e530 PZ |
2041 | /* |
2042 | * We consider a device as missing only if it failed | |
2043 | * to open (i.e. offline or faulted is not considered | |
2044 | * as missing). | |
2045 | */ | |
2046 | if (tvd->vdev_islog && | |
2047 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
2048 | child[idx++] = vdev_config_generate(spa, tvd, | |
2049 | B_FALSE, VDEV_CONFIG_MISSING); | |
2050 | } | |
572e2857 | 2051 | } |
9babb374 | 2052 | |
6cb8e530 PZ |
2053 | if (idx > 0) { |
2054 | fnvlist_add_nvlist_array(nv, | |
2055 | ZPOOL_CONFIG_CHILDREN, child, idx); | |
2056 | fnvlist_add_nvlist(spa->spa_load_info, | |
2057 | ZPOOL_CONFIG_MISSING_DEVICES, nv); | |
572e2857 | 2058 | |
6cb8e530 | 2059 | for (uint64_t i = 0; i < idx; i++) |
572e2857 BB |
2060 | nvlist_free(child[i]); |
2061 | } | |
2062 | nvlist_free(nv); | |
2063 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
572e2857 | 2064 | |
6cb8e530 PZ |
2065 | if (idx > 0) { |
2066 | spa_load_failed(spa, "some log devices are missing"); | |
db7d07e1 | 2067 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 PZ |
2068 | return (SET_ERROR(ENXIO)); |
2069 | } | |
2070 | } else { | |
2071 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
2072 | vdev_t *tvd = rvd->vdev_child[c]; | |
a1d477c2 | 2073 | |
6cb8e530 PZ |
2074 | if (tvd->vdev_islog && |
2075 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
572e2857 | 2076 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
6cb8e530 PZ |
2077 | spa_load_note(spa, "some log devices are " |
2078 | "missing, ZIL is dropped."); | |
db7d07e1 | 2079 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 | 2080 | break; |
e0ab3ab5 | 2081 | } |
572e2857 | 2082 | } |
9babb374 | 2083 | } |
e0ab3ab5 | 2084 | |
6cb8e530 | 2085 | return (0); |
9babb374 BB |
2086 | } |
2087 | ||
b128c09f BB |
2088 | /* |
2089 | * Check for missing log devices | |
2090 | */ | |
13fe0198 | 2091 | static boolean_t |
b128c09f BB |
2092 | spa_check_logs(spa_t *spa) |
2093 | { | |
13fe0198 | 2094 | boolean_t rv = B_FALSE; |
9c43027b | 2095 | dsl_pool_t *dp = spa_get_dsl(spa); |
13fe0198 | 2096 | |
b128c09f | 2097 | switch (spa->spa_log_state) { |
e75c13c3 BB |
2098 | default: |
2099 | break; | |
b128c09f BB |
2100 | case SPA_LOG_MISSING: |
2101 | /* need to recheck in case slog has been restored */ | |
2102 | case SPA_LOG_UNKNOWN: | |
9c43027b AJ |
2103 | rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, |
2104 | zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); | |
13fe0198 | 2105 | if (rv) |
428870ff | 2106 | spa_set_log_state(spa, SPA_LOG_MISSING); |
b128c09f | 2107 | break; |
b128c09f | 2108 | } |
13fe0198 | 2109 | return (rv); |
b128c09f BB |
2110 | } |
2111 | ||
428870ff BB |
2112 | static boolean_t |
2113 | spa_passivate_log(spa_t *spa) | |
34dc7c2f | 2114 | { |
428870ff BB |
2115 | vdev_t *rvd = spa->spa_root_vdev; |
2116 | boolean_t slog_found = B_FALSE; | |
b128c09f | 2117 | |
428870ff | 2118 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
fb5f0bc8 | 2119 | |
428870ff BB |
2120 | if (!spa_has_slogs(spa)) |
2121 | return (B_FALSE); | |
34dc7c2f | 2122 | |
1c27024e | 2123 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
2124 | vdev_t *tvd = rvd->vdev_child[c]; |
2125 | metaslab_group_t *mg = tvd->vdev_mg; | |
34dc7c2f | 2126 | |
428870ff BB |
2127 | if (tvd->vdev_islog) { |
2128 | metaslab_group_passivate(mg); | |
2129 | slog_found = B_TRUE; | |
2130 | } | |
34dc7c2f BB |
2131 | } |
2132 | ||
428870ff BB |
2133 | return (slog_found); |
2134 | } | |
34dc7c2f | 2135 | |
428870ff BB |
2136 | static void |
2137 | spa_activate_log(spa_t *spa) | |
2138 | { | |
2139 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 2140 | |
428870ff BB |
2141 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
2142 | ||
1c27024e | 2143 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
2144 | vdev_t *tvd = rvd->vdev_child[c]; |
2145 | metaslab_group_t *mg = tvd->vdev_mg; | |
2146 | ||
2147 | if (tvd->vdev_islog) | |
2148 | metaslab_group_activate(mg); | |
34dc7c2f | 2149 | } |
428870ff | 2150 | } |
34dc7c2f | 2151 | |
428870ff | 2152 | int |
a1d477c2 | 2153 | spa_reset_logs(spa_t *spa) |
428870ff | 2154 | { |
13fe0198 | 2155 | int error; |
9babb374 | 2156 | |
a1d477c2 | 2157 | error = dmu_objset_find(spa_name(spa), zil_reset, |
13fe0198 MA |
2158 | NULL, DS_FIND_CHILDREN); |
2159 | if (error == 0) { | |
428870ff BB |
2160 | /* |
2161 | * We successfully offlined the log device, sync out the | |
2162 | * current txg so that the "stubby" block can be removed | |
2163 | * by zil_sync(). | |
2164 | */ | |
2165 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
2166 | } | |
2167 | return (error); | |
2168 | } | |
34dc7c2f | 2169 | |
428870ff BB |
2170 | static void |
2171 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
2172 | { | |
1c27024e | 2173 | for (int i = 0; i < sav->sav_count; i++) |
428870ff BB |
2174 | spa_check_removed(sav->sav_vdevs[i]); |
2175 | } | |
34dc7c2f | 2176 | |
428870ff BB |
2177 | void |
2178 | spa_claim_notify(zio_t *zio) | |
2179 | { | |
2180 | spa_t *spa = zio->io_spa; | |
34dc7c2f | 2181 | |
428870ff BB |
2182 | if (zio->io_error) |
2183 | return; | |
34dc7c2f | 2184 | |
428870ff BB |
2185 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ |
2186 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
2187 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
2188 | mutex_exit(&spa->spa_props_lock); | |
2189 | } | |
34dc7c2f | 2190 | |
428870ff BB |
2191 | typedef struct spa_load_error { |
2192 | uint64_t sle_meta_count; | |
2193 | uint64_t sle_data_count; | |
2194 | } spa_load_error_t; | |
34dc7c2f | 2195 | |
428870ff BB |
2196 | static void |
2197 | spa_load_verify_done(zio_t *zio) | |
2198 | { | |
2199 | blkptr_t *bp = zio->io_bp; | |
2200 | spa_load_error_t *sle = zio->io_private; | |
2201 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
2202 | int error = zio->io_error; | |
dea377c0 | 2203 | spa_t *spa = zio->io_spa; |
34dc7c2f | 2204 | |
a6255b7f | 2205 | abd_free(zio->io_abd); |
428870ff | 2206 | if (error) { |
9ae529ec | 2207 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && |
428870ff | 2208 | type != DMU_OT_INTENT_LOG) |
bc89ac84 | 2209 | atomic_inc_64(&sle->sle_meta_count); |
428870ff | 2210 | else |
bc89ac84 | 2211 | atomic_inc_64(&sle->sle_data_count); |
34dc7c2f | 2212 | } |
dea377c0 MA |
2213 | |
2214 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2215 | spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp); |
dea377c0 MA |
2216 | cv_broadcast(&spa->spa_scrub_io_cv); |
2217 | mutex_exit(&spa->spa_scrub_lock); | |
428870ff | 2218 | } |
34dc7c2f | 2219 | |
dea377c0 | 2220 | /* |
e1cfd73f | 2221 | * Maximum number of inflight bytes is the log2 fraction of the arc size. |
c8242a96 | 2222 | * By default, we set it to 1/16th of the arc. |
dea377c0 | 2223 | */ |
c8242a96 | 2224 | int spa_load_verify_shift = 4; |
dea377c0 MA |
2225 | int spa_load_verify_metadata = B_TRUE; |
2226 | int spa_load_verify_data = B_TRUE; | |
2227 | ||
428870ff BB |
2228 | /*ARGSUSED*/ |
2229 | static int | |
2230 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
5dbd68a3 | 2231 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) |
428870ff | 2232 | { |
30af21b0 PD |
2233 | if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) || |
2234 | BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp)) | |
dea377c0 MA |
2235 | return (0); |
2236 | /* | |
2237 | * Note: normally this routine will not be called if | |
2238 | * spa_load_verify_metadata is not set. However, it may be useful | |
2239 | * to manually set the flag after the traversal has begun. | |
2240 | */ | |
2241 | if (!spa_load_verify_metadata) | |
2242 | return (0); | |
a6255b7f | 2243 | if (!BP_IS_METADATA(bp) && !spa_load_verify_data) |
dea377c0 MA |
2244 | return (0); |
2245 | ||
1e527162 GW |
2246 | uint64_t maxinflight_bytes = |
2247 | arc_target_bytes() >> spa_load_verify_shift; | |
1c27024e DB |
2248 | zio_t *rio = arg; |
2249 | size_t size = BP_GET_PSIZE(bp); | |
dea377c0 MA |
2250 | |
2251 | mutex_enter(&spa->spa_scrub_lock); | |
c8242a96 | 2252 | while (spa->spa_load_verify_bytes >= maxinflight_bytes) |
dea377c0 | 2253 | cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); |
c8242a96 | 2254 | spa->spa_load_verify_bytes += size; |
dea377c0 MA |
2255 | mutex_exit(&spa->spa_scrub_lock); |
2256 | ||
a6255b7f | 2257 | zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, |
dea377c0 MA |
2258 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, |
2259 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
2260 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
428870ff BB |
2261 | return (0); |
2262 | } | |
34dc7c2f | 2263 | |
d1d19c78 | 2264 | /* ARGSUSED */ |
65c7cc49 | 2265 | static int |
d1d19c78 PD |
2266 | verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) |
2267 | { | |
2268 | if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) | |
2269 | return (SET_ERROR(ENAMETOOLONG)); | |
2270 | ||
2271 | return (0); | |
2272 | } | |
2273 | ||
428870ff BB |
2274 | static int |
2275 | spa_load_verify(spa_t *spa) | |
2276 | { | |
2277 | zio_t *rio; | |
2278 | spa_load_error_t sle = { 0 }; | |
8a393be3 | 2279 | zpool_load_policy_t policy; |
428870ff | 2280 | boolean_t verify_ok = B_FALSE; |
dea377c0 | 2281 | int error = 0; |
34dc7c2f | 2282 | |
8a393be3 | 2283 | zpool_get_load_policy(spa->spa_config, &policy); |
34dc7c2f | 2284 | |
8a393be3 | 2285 | if (policy.zlp_rewind & ZPOOL_NEVER_REWIND) |
428870ff | 2286 | return (0); |
34dc7c2f | 2287 | |
d1d19c78 PD |
2288 | dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); |
2289 | error = dmu_objset_find_dp(spa->spa_dsl_pool, | |
2290 | spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, | |
2291 | DS_FIND_CHILDREN); | |
2292 | dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); | |
2293 | if (error != 0) | |
2294 | return (error); | |
2295 | ||
428870ff BB |
2296 | rio = zio_root(spa, NULL, &sle, |
2297 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
34dc7c2f | 2298 | |
dea377c0 | 2299 | if (spa_load_verify_metadata) { |
4a0ee12a PZ |
2300 | if (spa->spa_extreme_rewind) { |
2301 | spa_load_note(spa, "performing a complete scan of the " | |
2302 | "pool since extreme rewind is on. This may take " | |
2303 | "a very long time.\n (spa_load_verify_data=%u, " | |
2304 | "spa_load_verify_metadata=%u)", | |
2305 | spa_load_verify_data, spa_load_verify_metadata); | |
2306 | } | |
c8242a96 | 2307 | |
dea377c0 | 2308 | error = traverse_pool(spa, spa->spa_verify_min_txg, |
b5256303 TC |
2309 | TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | |
2310 | TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio); | |
dea377c0 | 2311 | } |
428870ff BB |
2312 | |
2313 | (void) zio_wait(rio); | |
c8242a96 | 2314 | ASSERT0(spa->spa_load_verify_bytes); |
428870ff BB |
2315 | |
2316 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
2317 | spa->spa_load_data_errors = sle.sle_data_count; | |
2318 | ||
afd2f7b7 PZ |
2319 | if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { |
2320 | spa_load_note(spa, "spa_load_verify found %llu metadata errors " | |
2321 | "and %llu data errors", (u_longlong_t)sle.sle_meta_count, | |
2322 | (u_longlong_t)sle.sle_data_count); | |
2323 | } | |
2324 | ||
2325 | if (spa_load_verify_dryrun || | |
8a393be3 PZ |
2326 | (!error && sle.sle_meta_count <= policy.zlp_maxmeta && |
2327 | sle.sle_data_count <= policy.zlp_maxdata)) { | |
572e2857 BB |
2328 | int64_t loss = 0; |
2329 | ||
428870ff BB |
2330 | verify_ok = B_TRUE; |
2331 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
2332 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
572e2857 BB |
2333 | |
2334 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
2335 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2336 | ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); | |
2337 | VERIFY(nvlist_add_int64(spa->spa_load_info, | |
2338 | ZPOOL_CONFIG_REWIND_TIME, loss) == 0); | |
2339 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2340 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); | |
428870ff BB |
2341 | } else { |
2342 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
2343 | } | |
2344 | ||
afd2f7b7 PZ |
2345 | if (spa_load_verify_dryrun) |
2346 | return (0); | |
2347 | ||
428870ff BB |
2348 | if (error) { |
2349 | if (error != ENXIO && error != EIO) | |
2e528b49 | 2350 | error = SET_ERROR(EIO); |
428870ff BB |
2351 | return (error); |
2352 | } | |
2353 | ||
2354 | return (verify_ok ? 0 : EIO); | |
2355 | } | |
2356 | ||
2357 | /* | |
2358 | * Find a value in the pool props object. | |
2359 | */ | |
2360 | static void | |
2361 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
2362 | { | |
2363 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
2364 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
2365 | } | |
2366 | ||
2367 | /* | |
2368 | * Find a value in the pool directory object. | |
2369 | */ | |
2370 | static int | |
4a0ee12a | 2371 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) |
428870ff | 2372 | { |
4a0ee12a PZ |
2373 | int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
2374 | name, sizeof (uint64_t), 1, val); | |
2375 | ||
2376 | if (error != 0 && (error != ENOENT || log_enoent)) { | |
2377 | spa_load_failed(spa, "couldn't get '%s' value in MOS directory " | |
2378 | "[error=%d]", name, error); | |
2379 | } | |
2380 | ||
2381 | return (error); | |
428870ff BB |
2382 | } |
2383 | ||
2384 | static int | |
2385 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
2386 | { | |
2387 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
a1d477c2 | 2388 | return (SET_ERROR(err)); |
428870ff BB |
2389 | } |
2390 | ||
37f03da8 SH |
2391 | boolean_t |
2392 | spa_livelist_delete_check(spa_t *spa) | |
2393 | { | |
2394 | return (spa->spa_livelists_to_delete != 0); | |
2395 | } | |
2396 | ||
2397 | /* ARGSUSED */ | |
2398 | static boolean_t | |
2399 | spa_livelist_delete_cb_check(void *arg, zthr_t *z) | |
2400 | { | |
2401 | spa_t *spa = arg; | |
2402 | return (spa_livelist_delete_check(spa)); | |
2403 | } | |
2404 | ||
2405 | static int | |
2406 | delete_blkptr_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
2407 | { | |
2408 | spa_t *spa = arg; | |
2409 | zio_free(spa, tx->tx_txg, bp); | |
2410 | dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD, | |
2411 | -bp_get_dsize_sync(spa, bp), | |
2412 | -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx); | |
2413 | return (0); | |
2414 | } | |
2415 | ||
2416 | static int | |
2417 | dsl_get_next_livelist_obj(objset_t *os, uint64_t zap_obj, uint64_t *llp) | |
2418 | { | |
2419 | int err; | |
2420 | zap_cursor_t zc; | |
2421 | zap_attribute_t za; | |
2422 | zap_cursor_init(&zc, os, zap_obj); | |
2423 | err = zap_cursor_retrieve(&zc, &za); | |
2424 | zap_cursor_fini(&zc); | |
2425 | if (err == 0) | |
2426 | *llp = za.za_first_integer; | |
2427 | return (err); | |
2428 | } | |
2429 | ||
2430 | /* | |
2431 | * Components of livelist deletion that must be performed in syncing | |
2432 | * context: freeing block pointers and updating the pool-wide data | |
2433 | * structures to indicate how much work is left to do | |
2434 | */ | |
2435 | typedef struct sublist_delete_arg { | |
2436 | spa_t *spa; | |
2437 | dsl_deadlist_t *ll; | |
2438 | uint64_t key; | |
2439 | bplist_t *to_free; | |
2440 | } sublist_delete_arg_t; | |
2441 | ||
2442 | static void | |
2443 | sublist_delete_sync(void *arg, dmu_tx_t *tx) | |
2444 | { | |
2445 | sublist_delete_arg_t *sda = arg; | |
2446 | spa_t *spa = sda->spa; | |
2447 | dsl_deadlist_t *ll = sda->ll; | |
2448 | uint64_t key = sda->key; | |
2449 | bplist_t *to_free = sda->to_free; | |
2450 | ||
2451 | bplist_iterate(to_free, delete_blkptr_cb, spa, tx); | |
2452 | dsl_deadlist_remove_entry(ll, key, tx); | |
2453 | } | |
2454 | ||
2455 | typedef struct livelist_delete_arg { | |
2456 | spa_t *spa; | |
2457 | uint64_t ll_obj; | |
2458 | uint64_t zap_obj; | |
2459 | } livelist_delete_arg_t; | |
2460 | ||
2461 | static void | |
2462 | livelist_delete_sync(void *arg, dmu_tx_t *tx) | |
2463 | { | |
2464 | livelist_delete_arg_t *lda = arg; | |
2465 | spa_t *spa = lda->spa; | |
2466 | uint64_t ll_obj = lda->ll_obj; | |
2467 | uint64_t zap_obj = lda->zap_obj; | |
2468 | objset_t *mos = spa->spa_meta_objset; | |
2469 | uint64_t count; | |
2470 | ||
2471 | /* free the livelist and decrement the feature count */ | |
2472 | VERIFY0(zap_remove_int(mos, zap_obj, ll_obj, tx)); | |
2473 | dsl_deadlist_free(mos, ll_obj, tx); | |
2474 | spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx); | |
2475 | VERIFY0(zap_count(mos, zap_obj, &count)); | |
2476 | if (count == 0) { | |
2477 | /* no more livelists to delete */ | |
2478 | VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT, | |
2479 | DMU_POOL_DELETED_CLONES, tx)); | |
2480 | VERIFY0(zap_destroy(mos, zap_obj, tx)); | |
2481 | spa->spa_livelists_to_delete = 0; | |
e60e158e | 2482 | spa_notify_waiters(spa); |
37f03da8 SH |
2483 | } |
2484 | } | |
2485 | ||
2486 | /* | |
2487 | * Load in the value for the livelist to be removed and open it. Then, | |
2488 | * load its first sublist and determine which block pointers should actually | |
2489 | * be freed. Then, call a synctask which performs the actual frees and updates | |
2490 | * the pool-wide livelist data. | |
2491 | */ | |
2492 | /* ARGSUSED */ | |
65c7cc49 | 2493 | static void |
37f03da8 SH |
2494 | spa_livelist_delete_cb(void *arg, zthr_t *z) |
2495 | { | |
2496 | spa_t *spa = arg; | |
2497 | uint64_t ll_obj = 0, count; | |
2498 | objset_t *mos = spa->spa_meta_objset; | |
2499 | uint64_t zap_obj = spa->spa_livelists_to_delete; | |
2500 | /* | |
2501 | * Determine the next livelist to delete. This function should only | |
2502 | * be called if there is at least one deleted clone. | |
2503 | */ | |
2504 | VERIFY0(dsl_get_next_livelist_obj(mos, zap_obj, &ll_obj)); | |
2505 | VERIFY0(zap_count(mos, ll_obj, &count)); | |
2506 | if (count > 0) { | |
2507 | dsl_deadlist_t ll = { 0 }; | |
2508 | dsl_deadlist_entry_t *dle; | |
2509 | bplist_t to_free; | |
2510 | dsl_deadlist_open(&ll, mos, ll_obj); | |
2511 | dle = dsl_deadlist_first(&ll); | |
2512 | ASSERT3P(dle, !=, NULL); | |
2513 | bplist_create(&to_free); | |
2514 | int err = dsl_process_sub_livelist(&dle->dle_bpobj, &to_free, | |
2515 | z, NULL); | |
2516 | if (err == 0) { | |
2517 | sublist_delete_arg_t sync_arg = { | |
2518 | .spa = spa, | |
2519 | .ll = &ll, | |
2520 | .key = dle->dle_mintxg, | |
2521 | .to_free = &to_free | |
2522 | }; | |
2523 | zfs_dbgmsg("deleting sublist (id %llu) from" | |
2524 | " livelist %llu, %d remaining", | |
2525 | dle->dle_bpobj.bpo_object, ll_obj, count - 1); | |
2526 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, | |
2527 | sublist_delete_sync, &sync_arg, 0, | |
2528 | ZFS_SPACE_CHECK_DESTROY)); | |
2529 | } else { | |
2530 | ASSERT(err == EINTR); | |
2531 | } | |
2532 | bplist_clear(&to_free); | |
2533 | bplist_destroy(&to_free); | |
2534 | dsl_deadlist_close(&ll); | |
2535 | } else { | |
2536 | livelist_delete_arg_t sync_arg = { | |
2537 | .spa = spa, | |
2538 | .ll_obj = ll_obj, | |
2539 | .zap_obj = zap_obj | |
2540 | }; | |
2541 | zfs_dbgmsg("deletion of livelist %llu completed", ll_obj); | |
2542 | VERIFY0(dsl_sync_task(spa_name(spa), NULL, livelist_delete_sync, | |
2543 | &sync_arg, 0, ZFS_SPACE_CHECK_DESTROY)); | |
2544 | } | |
2545 | } | |
2546 | ||
65c7cc49 | 2547 | static void |
37f03da8 SH |
2548 | spa_start_livelist_destroy_thread(spa_t *spa) |
2549 | { | |
2550 | ASSERT3P(spa->spa_livelist_delete_zthr, ==, NULL); | |
843e9ca2 SD |
2551 | spa->spa_livelist_delete_zthr = |
2552 | zthr_create("z_livelist_destroy", | |
37f03da8 SH |
2553 | spa_livelist_delete_cb_check, spa_livelist_delete_cb, spa); |
2554 | } | |
2555 | ||
2556 | typedef struct livelist_new_arg { | |
2557 | bplist_t *allocs; | |
2558 | bplist_t *frees; | |
2559 | } livelist_new_arg_t; | |
2560 | ||
2561 | static int | |
2562 | livelist_track_new_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
2563 | dmu_tx_t *tx) | |
2564 | { | |
2565 | ASSERT(tx == NULL); | |
2566 | livelist_new_arg_t *lna = arg; | |
2567 | if (bp_freed) { | |
2568 | bplist_append(lna->frees, bp); | |
2569 | } else { | |
2570 | bplist_append(lna->allocs, bp); | |
2571 | zfs_livelist_condense_new_alloc++; | |
2572 | } | |
2573 | return (0); | |
2574 | } | |
2575 | ||
2576 | typedef struct livelist_condense_arg { | |
2577 | spa_t *spa; | |
2578 | bplist_t to_keep; | |
2579 | uint64_t first_size; | |
2580 | uint64_t next_size; | |
2581 | } livelist_condense_arg_t; | |
2582 | ||
2583 | static void | |
2584 | spa_livelist_condense_sync(void *arg, dmu_tx_t *tx) | |
2585 | { | |
2586 | livelist_condense_arg_t *lca = arg; | |
2587 | spa_t *spa = lca->spa; | |
2588 | bplist_t new_frees; | |
2589 | dsl_dataset_t *ds = spa->spa_to_condense.ds; | |
2590 | ||
2591 | /* Have we been cancelled? */ | |
2592 | if (spa->spa_to_condense.cancelled) { | |
2593 | zfs_livelist_condense_sync_cancel++; | |
2594 | goto out; | |
2595 | } | |
2596 | ||
2597 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2598 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2599 | dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist; | |
2600 | ||
2601 | /* | |
2602 | * It's possible that the livelist was changed while the zthr was | |
2603 | * running. Therefore, we need to check for new blkptrs in the two | |
2604 | * entries being condensed and continue to track them in the livelist. | |
2605 | * Because of the way we handle remapped blkptrs (see dbuf_remap_impl), | |
2606 | * it's possible that the newly added blkptrs are FREEs or ALLOCs so | |
2607 | * we need to sort them into two different bplists. | |
2608 | */ | |
2609 | uint64_t first_obj = first->dle_bpobj.bpo_object; | |
2610 | uint64_t next_obj = next->dle_bpobj.bpo_object; | |
2611 | uint64_t cur_first_size = first->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2612 | uint64_t cur_next_size = next->dle_bpobj.bpo_phys->bpo_num_blkptrs; | |
2613 | ||
2614 | bplist_create(&new_frees); | |
2615 | livelist_new_arg_t new_bps = { | |
2616 | .allocs = &lca->to_keep, | |
2617 | .frees = &new_frees, | |
2618 | }; | |
2619 | ||
2620 | if (cur_first_size > lca->first_size) { | |
2621 | VERIFY0(livelist_bpobj_iterate_from_nofree(&first->dle_bpobj, | |
2622 | livelist_track_new_cb, &new_bps, lca->first_size)); | |
2623 | } | |
2624 | if (cur_next_size > lca->next_size) { | |
2625 | VERIFY0(livelist_bpobj_iterate_from_nofree(&next->dle_bpobj, | |
2626 | livelist_track_new_cb, &new_bps, lca->next_size)); | |
2627 | } | |
2628 | ||
2629 | dsl_deadlist_clear_entry(first, ll, tx); | |
2630 | ASSERT(bpobj_is_empty(&first->dle_bpobj)); | |
2631 | dsl_deadlist_remove_entry(ll, next->dle_mintxg, tx); | |
2632 | ||
2633 | bplist_iterate(&lca->to_keep, dsl_deadlist_insert_alloc_cb, ll, tx); | |
2634 | bplist_iterate(&new_frees, dsl_deadlist_insert_free_cb, ll, tx); | |
2635 | bplist_destroy(&new_frees); | |
2636 | ||
2637 | char dsname[ZFS_MAX_DATASET_NAME_LEN]; | |
2638 | dsl_dataset_name(ds, dsname); | |
2639 | zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu " | |
2640 | "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu " | |
2641 | "(%llu blkptrs)", tx->tx_txg, dsname, ds->ds_object, first_obj, | |
2642 | cur_first_size, next_obj, cur_next_size, | |
2643 | first->dle_bpobj.bpo_object, | |
2644 | first->dle_bpobj.bpo_phys->bpo_num_blkptrs); | |
2645 | out: | |
2646 | dmu_buf_rele(ds->ds_dbuf, spa); | |
2647 | spa->spa_to_condense.ds = NULL; | |
2648 | bplist_clear(&lca->to_keep); | |
2649 | bplist_destroy(&lca->to_keep); | |
2650 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2651 | spa->spa_to_condense.syncing = B_FALSE; | |
2652 | } | |
2653 | ||
65c7cc49 | 2654 | static void |
37f03da8 SH |
2655 | spa_livelist_condense_cb(void *arg, zthr_t *t) |
2656 | { | |
2657 | while (zfs_livelist_condense_zthr_pause && | |
2658 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2659 | delay(1); | |
2660 | ||
2661 | spa_t *spa = arg; | |
2662 | dsl_deadlist_entry_t *first = spa->spa_to_condense.first; | |
2663 | dsl_deadlist_entry_t *next = spa->spa_to_condense.next; | |
2664 | uint64_t first_size, next_size; | |
2665 | ||
2666 | livelist_condense_arg_t *lca = | |
2667 | kmem_alloc(sizeof (livelist_condense_arg_t), KM_SLEEP); | |
2668 | bplist_create(&lca->to_keep); | |
2669 | ||
2670 | /* | |
2671 | * Process the livelists (matching FREEs and ALLOCs) in open context | |
2672 | * so we have minimal work in syncing context to condense. | |
2673 | * | |
2674 | * We save bpobj sizes (first_size and next_size) to use later in | |
2675 | * syncing context to determine if entries were added to these sublists | |
2676 | * while in open context. This is possible because the clone is still | |
2677 | * active and open for normal writes and we want to make sure the new, | |
2678 | * unprocessed blockpointers are inserted into the livelist normally. | |
2679 | * | |
2680 | * Note that dsl_process_sub_livelist() both stores the size number of | |
2681 | * blockpointers and iterates over them while the bpobj's lock held, so | |
2682 | * the sizes returned to us are consistent which what was actually | |
2683 | * processed. | |
2684 | */ | |
2685 | int err = dsl_process_sub_livelist(&first->dle_bpobj, &lca->to_keep, t, | |
2686 | &first_size); | |
2687 | if (err == 0) | |
2688 | err = dsl_process_sub_livelist(&next->dle_bpobj, &lca->to_keep, | |
2689 | t, &next_size); | |
2690 | ||
2691 | if (err == 0) { | |
2692 | while (zfs_livelist_condense_sync_pause && | |
2693 | !(zthr_has_waiters(t) || zthr_iscancelled(t))) | |
2694 | delay(1); | |
2695 | ||
2696 | dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
2697 | dmu_tx_mark_netfree(tx); | |
2698 | dmu_tx_hold_space(tx, 1); | |
2699 | err = dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE); | |
2700 | if (err == 0) { | |
2701 | /* | |
2702 | * Prevent the condense zthr restarting before | |
2703 | * the synctask completes. | |
2704 | */ | |
2705 | spa->spa_to_condense.syncing = B_TRUE; | |
2706 | lca->spa = spa; | |
2707 | lca->first_size = first_size; | |
2708 | lca->next_size = next_size; | |
2709 | dsl_sync_task_nowait(spa_get_dsl(spa), | |
2710 | spa_livelist_condense_sync, lca, 0, | |
2711 | ZFS_SPACE_CHECK_NONE, tx); | |
2712 | dmu_tx_commit(tx); | |
2713 | return; | |
2714 | } | |
2715 | } | |
2716 | /* | |
2717 | * Condensing can not continue: either it was externally stopped or | |
2718 | * we were unable to assign to a tx because the pool has run out of | |
2719 | * space. In the second case, we'll just end up trying to condense | |
2720 | * again in a later txg. | |
2721 | */ | |
2722 | ASSERT(err != 0); | |
2723 | bplist_clear(&lca->to_keep); | |
2724 | bplist_destroy(&lca->to_keep); | |
2725 | kmem_free(lca, sizeof (livelist_condense_arg_t)); | |
2726 | dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, spa); | |
2727 | spa->spa_to_condense.ds = NULL; | |
2728 | if (err == EINTR) | |
2729 | zfs_livelist_condense_zthr_cancel++; | |
2730 | } | |
2731 | ||
2732 | /* ARGSUSED */ | |
2733 | /* | |
2734 | * Check that there is something to condense but that a condense is not | |
2735 | * already in progress and that condensing has not been cancelled. | |
2736 | */ | |
2737 | static boolean_t | |
2738 | spa_livelist_condense_cb_check(void *arg, zthr_t *z) | |
2739 | { | |
2740 | spa_t *spa = arg; | |
2741 | if ((spa->spa_to_condense.ds != NULL) && | |
2742 | (spa->spa_to_condense.syncing == B_FALSE) && | |
2743 | (spa->spa_to_condense.cancelled == B_FALSE)) { | |
2744 | return (B_TRUE); | |
2745 | } | |
2746 | return (B_FALSE); | |
2747 | } | |
2748 | ||
65c7cc49 | 2749 | static void |
37f03da8 SH |
2750 | spa_start_livelist_condensing_thread(spa_t *spa) |
2751 | { | |
2752 | spa->spa_to_condense.ds = NULL; | |
2753 | spa->spa_to_condense.first = NULL; | |
2754 | spa->spa_to_condense.next = NULL; | |
2755 | spa->spa_to_condense.syncing = B_FALSE; | |
2756 | spa->spa_to_condense.cancelled = B_FALSE; | |
2757 | ||
2758 | ASSERT3P(spa->spa_livelist_condense_zthr, ==, NULL); | |
843e9ca2 SD |
2759 | spa->spa_livelist_condense_zthr = |
2760 | zthr_create("z_livelist_condense", | |
2761 | spa_livelist_condense_cb_check, | |
2762 | spa_livelist_condense_cb, spa); | |
37f03da8 SH |
2763 | } |
2764 | ||
9d5b5245 SD |
2765 | static void |
2766 | spa_spawn_aux_threads(spa_t *spa) | |
2767 | { | |
2768 | ASSERT(spa_writeable(spa)); | |
2769 | ||
2770 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2771 | ||
2772 | spa_start_indirect_condensing_thread(spa); | |
37f03da8 SH |
2773 | spa_start_livelist_destroy_thread(spa); |
2774 | spa_start_livelist_condensing_thread(spa); | |
d2734cce SD |
2775 | |
2776 | ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); | |
2777 | spa->spa_checkpoint_discard_zthr = | |
843e9ca2 SD |
2778 | zthr_create("z_checkpoint_discard", |
2779 | spa_checkpoint_discard_thread_check, | |
d2734cce | 2780 | spa_checkpoint_discard_thread, spa); |
9d5b5245 SD |
2781 | } |
2782 | ||
428870ff BB |
2783 | /* |
2784 | * Fix up config after a partly-completed split. This is done with the | |
2785 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
2786 | * pool have that entry in their config, but only the splitting one contains | |
2787 | * a list of all the guids of the vdevs that are being split off. | |
2788 | * | |
2789 | * This function determines what to do with that list: either rejoin | |
2790 | * all the disks to the pool, or complete the splitting process. To attempt | |
2791 | * the rejoin, each disk that is offlined is marked online again, and | |
2792 | * we do a reopen() call. If the vdev label for every disk that was | |
2793 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
2794 | * then we call vdev_split() on each disk, and complete the split. | |
2795 | * | |
2796 | * Otherwise we leave the config alone, with all the vdevs in place in | |
2797 | * the original pool. | |
2798 | */ | |
2799 | static void | |
2800 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
2801 | { | |
2802 | uint_t extracted; | |
2803 | uint64_t *glist; | |
2804 | uint_t i, gcount; | |
2805 | nvlist_t *nvl; | |
2806 | vdev_t **vd; | |
2807 | boolean_t attempt_reopen; | |
2808 | ||
2809 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
2810 | return; | |
2811 | ||
2812 | /* check that the config is complete */ | |
2813 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
2814 | &glist, &gcount) != 0) | |
2815 | return; | |
2816 | ||
79c76d5b | 2817 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); |
428870ff BB |
2818 | |
2819 | /* attempt to online all the vdevs & validate */ | |
2820 | attempt_reopen = B_TRUE; | |
2821 | for (i = 0; i < gcount; i++) { | |
2822 | if (glist[i] == 0) /* vdev is hole */ | |
2823 | continue; | |
2824 | ||
2825 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
2826 | if (vd[i] == NULL) { | |
2827 | /* | |
2828 | * Don't bother attempting to reopen the disks; | |
2829 | * just do the split. | |
2830 | */ | |
2831 | attempt_reopen = B_FALSE; | |
2832 | } else { | |
2833 | /* attempt to re-online it */ | |
2834 | vd[i]->vdev_offline = B_FALSE; | |
2835 | } | |
2836 | } | |
2837 | ||
2838 | if (attempt_reopen) { | |
2839 | vdev_reopen(spa->spa_root_vdev); | |
2840 | ||
2841 | /* check each device to see what state it's in */ | |
2842 | for (extracted = 0, i = 0; i < gcount; i++) { | |
2843 | if (vd[i] != NULL && | |
2844 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
2845 | break; | |
2846 | ++extracted; | |
2847 | } | |
2848 | } | |
2849 | ||
2850 | /* | |
2851 | * If every disk has been moved to the new pool, or if we never | |
2852 | * even attempted to look at them, then we split them off for | |
2853 | * good. | |
2854 | */ | |
2855 | if (!attempt_reopen || gcount == extracted) { | |
2856 | for (i = 0; i < gcount; i++) | |
2857 | if (vd[i] != NULL) | |
2858 | vdev_split(vd[i]); | |
2859 | vdev_reopen(spa->spa_root_vdev); | |
2860 | } | |
2861 | ||
2862 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
2863 | } | |
2864 | ||
2865 | static int | |
6cb8e530 | 2866 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) |
428870ff | 2867 | { |
428870ff BB |
2868 | char *ereport = FM_EREPORT_ZFS_POOL; |
2869 | int error; | |
428870ff | 2870 | |
6cb8e530 | 2871 | spa->spa_load_state = state; |
ca95f70d OF |
2872 | (void) spa_import_progress_set_state(spa_guid(spa), |
2873 | spa_load_state(spa)); | |
9ae529ec | 2874 | |
6cb8e530 | 2875 | gethrestime(&spa->spa_loaded_ts); |
d2734cce | 2876 | error = spa_load_impl(spa, type, &ereport); |
428870ff | 2877 | |
0c66c32d JG |
2878 | /* |
2879 | * Don't count references from objsets that are already closed | |
2880 | * and are making their way through the eviction process. | |
2881 | */ | |
2882 | spa_evicting_os_wait(spa); | |
424fd7c3 | 2883 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
572e2857 BB |
2884 | if (error) { |
2885 | if (error != EEXIST) { | |
2886 | spa->spa_loaded_ts.tv_sec = 0; | |
2887 | spa->spa_loaded_ts.tv_nsec = 0; | |
2888 | } | |
2889 | if (error != EBADF) { | |
b5256303 | 2890 | zfs_ereport_post(ereport, spa, NULL, NULL, NULL, 0, 0); |
572e2857 BB |
2891 | } |
2892 | } | |
428870ff BB |
2893 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; |
2894 | spa->spa_ena = 0; | |
2895 | ||
ca95f70d OF |
2896 | (void) spa_import_progress_set_state(spa_guid(spa), |
2897 | spa_load_state(spa)); | |
2898 | ||
428870ff BB |
2899 | return (error); |
2900 | } | |
2901 | ||
33cf67cd | 2902 | #ifdef ZFS_DEBUG |
e0ab3ab5 JS |
2903 | /* |
2904 | * Count the number of per-vdev ZAPs associated with all of the vdevs in the | |
2905 | * vdev tree rooted in the given vd, and ensure that each ZAP is present in the | |
2906 | * spa's per-vdev ZAP list. | |
2907 | */ | |
2908 | static uint64_t | |
2909 | vdev_count_verify_zaps(vdev_t *vd) | |
2910 | { | |
2911 | spa_t *spa = vd->vdev_spa; | |
2912 | uint64_t total = 0; | |
e0ab3ab5 JS |
2913 | |
2914 | if (vd->vdev_top_zap != 0) { | |
2915 | total++; | |
2916 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2917 | spa->spa_all_vdev_zaps, vd->vdev_top_zap)); | |
2918 | } | |
2919 | if (vd->vdev_leaf_zap != 0) { | |
2920 | total++; | |
2921 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2922 | spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); | |
2923 | } | |
2924 | ||
1c27024e | 2925 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
2926 | total += vdev_count_verify_zaps(vd->vdev_child[i]); |
2927 | } | |
2928 | ||
2929 | return (total); | |
2930 | } | |
33cf67cd | 2931 | #endif |
e0ab3ab5 | 2932 | |
379ca9cf OF |
2933 | /* |
2934 | * Determine whether the activity check is required. | |
2935 | */ | |
2936 | static boolean_t | |
bbffb59e BB |
2937 | spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label, |
2938 | nvlist_t *config) | |
379ca9cf OF |
2939 | { |
2940 | uint64_t state = 0; | |
2941 | uint64_t hostid = 0; | |
2942 | uint64_t tryconfig_txg = 0; | |
2943 | uint64_t tryconfig_timestamp = 0; | |
060f0226 | 2944 | uint16_t tryconfig_mmp_seq = 0; |
379ca9cf OF |
2945 | nvlist_t *nvinfo; |
2946 | ||
2947 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
2948 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
2949 | (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, | |
2950 | &tryconfig_txg); | |
2951 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
2952 | &tryconfig_timestamp); | |
060f0226 OF |
2953 | (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ, |
2954 | &tryconfig_mmp_seq); | |
379ca9cf OF |
2955 | } |
2956 | ||
2957 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); | |
379ca9cf OF |
2958 | |
2959 | /* | |
2960 | * Disable the MMP activity check - This is used by zdb which | |
2961 | * is intended to be used on potentially active pools. | |
2962 | */ | |
2963 | if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) | |
2964 | return (B_FALSE); | |
2965 | ||
2966 | /* | |
2967 | * Skip the activity check when the MMP feature is disabled. | |
2968 | */ | |
2969 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) | |
2970 | return (B_FALSE); | |
ca95f70d | 2971 | |
379ca9cf | 2972 | /* |
060f0226 OF |
2973 | * If the tryconfig_ values are nonzero, they are the results of an |
2974 | * earlier tryimport. If they all match the uberblock we just found, | |
2975 | * then the pool has not changed and we return false so we do not test | |
2976 | * a second time. | |
379ca9cf OF |
2977 | */ |
2978 | if (tryconfig_txg && tryconfig_txg == ub->ub_txg && | |
060f0226 OF |
2979 | tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp && |
2980 | tryconfig_mmp_seq && tryconfig_mmp_seq == | |
2981 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) | |
379ca9cf OF |
2982 | return (B_FALSE); |
2983 | ||
2984 | /* | |
2985 | * Allow the activity check to be skipped when importing the pool | |
bbffb59e BB |
2986 | * on the same host which last imported it. Since the hostid from |
2987 | * configuration may be stale use the one read from the label. | |
379ca9cf | 2988 | */ |
bbffb59e BB |
2989 | if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) |
2990 | hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); | |
2991 | ||
25f06d67 | 2992 | if (hostid == spa_get_hostid(spa)) |
379ca9cf OF |
2993 | return (B_FALSE); |
2994 | ||
2995 | /* | |
2996 | * Skip the activity test when the pool was cleanly exported. | |
2997 | */ | |
2998 | if (state != POOL_STATE_ACTIVE) | |
2999 | return (B_FALSE); | |
3000 | ||
3001 | return (B_TRUE); | |
3002 | } | |
3003 | ||
060f0226 OF |
3004 | /* |
3005 | * Nanoseconds the activity check must watch for changes on-disk. | |
3006 | */ | |
3007 | static uint64_t | |
3008 | spa_activity_check_duration(spa_t *spa, uberblock_t *ub) | |
3009 | { | |
3010 | uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); | |
3011 | uint64_t multihost_interval = MSEC2NSEC( | |
3012 | MMP_INTERVAL_OK(zfs_multihost_interval)); | |
3013 | uint64_t import_delay = MAX(NANOSEC, import_intervals * | |
3014 | multihost_interval); | |
3015 | ||
3016 | /* | |
3017 | * Local tunables determine a minimum duration except for the case | |
3018 | * where we know when the remote host will suspend the pool if MMP | |
3019 | * writes do not land. | |
3020 | * | |
3021 | * See Big Theory comment at the top of mmp.c for the reasoning behind | |
3022 | * these cases and times. | |
3023 | */ | |
3024 | ||
3025 | ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100); | |
3026 | ||
3027 | if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3028 | MMP_FAIL_INT(ub) > 0) { | |
3029 | ||
3030 | /* MMP on remote host will suspend pool after failed writes */ | |
3031 | import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) * | |
3032 | MMP_IMPORT_SAFETY_FACTOR / 100; | |
3033 | ||
3034 | zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp " | |
3035 | "mmp_fails=%llu ub_mmp mmp_interval=%llu " | |
3036 | "import_intervals=%u", import_delay, MMP_FAIL_INT(ub), | |
3037 | MMP_INTERVAL(ub), import_intervals); | |
3038 | ||
3039 | } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
3040 | MMP_FAIL_INT(ub) == 0) { | |
3041 | ||
3042 | /* MMP on remote host will never suspend pool */ | |
3043 | import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) + | |
3044 | ub->ub_mmp_delay) * import_intervals); | |
3045 | ||
3046 | zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp " | |
3047 | "mmp_interval=%llu ub_mmp_delay=%llu " | |
3048 | "import_intervals=%u", import_delay, MMP_INTERVAL(ub), | |
3049 | ub->ub_mmp_delay, import_intervals); | |
3050 | ||
3051 | } else if (MMP_VALID(ub)) { | |
3052 | /* | |
e1cfd73f | 3053 | * zfs-0.7 compatibility case |
060f0226 OF |
3054 | */ |
3055 | ||
3056 | import_delay = MAX(import_delay, (multihost_interval + | |
3057 | ub->ub_mmp_delay) * import_intervals); | |
3058 | ||
3059 | zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu " | |
3060 | "import_intervals=%u leaves=%u", import_delay, | |
3061 | ub->ub_mmp_delay, import_intervals, | |
3062 | vdev_count_leaves(spa)); | |
3063 | } else { | |
3064 | /* Using local tunings is the only reasonable option */ | |
3065 | zfs_dbgmsg("pool last imported on non-MMP aware " | |
3066 | "host using import_delay=%llu multihost_interval=%llu " | |
3067 | "import_intervals=%u", import_delay, multihost_interval, | |
3068 | import_intervals); | |
3069 | } | |
3070 | ||
3071 | return (import_delay); | |
3072 | } | |
3073 | ||
379ca9cf OF |
3074 | /* |
3075 | * Perform the import activity check. If the user canceled the import or | |
3076 | * we detected activity then fail. | |
3077 | */ | |
3078 | static int | |
3079 | spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) | |
3080 | { | |
379ca9cf OF |
3081 | uint64_t txg = ub->ub_txg; |
3082 | uint64_t timestamp = ub->ub_timestamp; | |
060f0226 OF |
3083 | uint64_t mmp_config = ub->ub_mmp_config; |
3084 | uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0; | |
3085 | uint64_t import_delay; | |
379ca9cf OF |
3086 | hrtime_t import_expire; |
3087 | nvlist_t *mmp_label = NULL; | |
3088 | vdev_t *rvd = spa->spa_root_vdev; | |
3089 | kcondvar_t cv; | |
3090 | kmutex_t mtx; | |
3091 | int error = 0; | |
3092 | ||
3093 | cv_init(&cv, NULL, CV_DEFAULT, NULL); | |
3094 | mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); | |
3095 | mutex_enter(&mtx); | |
3096 | ||
3097 | /* | |
3098 | * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed | |
3099 | * during the earlier tryimport. If the txg recorded there is 0 then | |
3100 | * the pool is known to be active on another host. | |
3101 | * | |
060f0226 | 3102 | * Otherwise, the pool might be in use on another host. Check for |
379ca9cf OF |
3103 | * changes in the uberblocks on disk if necessary. |
3104 | */ | |
3105 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
3106 | nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, | |
3107 | ZPOOL_CONFIG_LOAD_INFO); | |
3108 | ||
3109 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && | |
3110 | fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { | |
3111 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
3112 | error = SET_ERROR(EREMOTEIO); | |
3113 | goto out; | |
3114 | } | |
3115 | } | |
3116 | ||
060f0226 | 3117 | import_delay = spa_activity_check_duration(spa, ub); |
533ea041 | 3118 | |
379ca9cf | 3119 | /* Add a small random factor in case of simultaneous imports (0-25%) */ |
ca95f70d OF |
3120 | import_delay += import_delay * spa_get_random(250) / 1000; |
3121 | ||
3122 | import_expire = gethrtime() + import_delay; | |
379ca9cf OF |
3123 | |
3124 | while (gethrtime() < import_expire) { | |
ca95f70d OF |
3125 | (void) spa_import_progress_set_mmp_check(spa_guid(spa), |
3126 | NSEC2SEC(import_expire - gethrtime())); | |
3127 | ||
379ca9cf OF |
3128 | vdev_uberblock_load(rvd, ub, &mmp_label); |
3129 | ||
060f0226 OF |
3130 | if (txg != ub->ub_txg || timestamp != ub->ub_timestamp || |
3131 | mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) { | |
3132 | zfs_dbgmsg("multihost activity detected " | |
3133 | "txg %llu ub_txg %llu " | |
3134 | "timestamp %llu ub_timestamp %llu " | |
3135 | "mmp_config %#llx ub_mmp_config %#llx", | |
3136 | txg, ub->ub_txg, timestamp, ub->ub_timestamp, | |
3137 | mmp_config, ub->ub_mmp_config); | |
3138 | ||
379ca9cf OF |
3139 | error = SET_ERROR(EREMOTEIO); |
3140 | break; | |
3141 | } | |
3142 | ||
3143 | if (mmp_label) { | |
3144 | nvlist_free(mmp_label); | |
3145 | mmp_label = NULL; | |
3146 | } | |
3147 | ||
3148 | error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); | |
3149 | if (error != -1) { | |
3150 | error = SET_ERROR(EINTR); | |
3151 | break; | |
3152 | } | |
3153 | error = 0; | |
3154 | } | |
3155 | ||
3156 | out: | |
3157 | mutex_exit(&mtx); | |
3158 | mutex_destroy(&mtx); | |
3159 | cv_destroy(&cv); | |
3160 | ||
3161 | /* | |
3162 | * If the pool is determined to be active store the status in the | |
3163 | * spa->spa_load_info nvlist. If the remote hostname or hostid are | |
3164 | * available from configuration read from disk store them as well. | |
3165 | * This allows 'zpool import' to generate a more useful message. | |
3166 | * | |
3167 | * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) | |
3168 | * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool | |
3169 | * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool | |
3170 | */ | |
3171 | if (error == EREMOTEIO) { | |
3172 | char *hostname = "<unknown>"; | |
3173 | uint64_t hostid = 0; | |
3174 | ||
3175 | if (mmp_label) { | |
3176 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { | |
3177 | hostname = fnvlist_lookup_string(mmp_label, | |
3178 | ZPOOL_CONFIG_HOSTNAME); | |
3179 | fnvlist_add_string(spa->spa_load_info, | |
3180 | ZPOOL_CONFIG_MMP_HOSTNAME, hostname); | |
3181 | } | |
3182 | ||
3183 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { | |
3184 | hostid = fnvlist_lookup_uint64(mmp_label, | |
3185 | ZPOOL_CONFIG_HOSTID); | |
3186 | fnvlist_add_uint64(spa->spa_load_info, | |
3187 | ZPOOL_CONFIG_MMP_HOSTID, hostid); | |
3188 | } | |
3189 | } | |
3190 | ||
3191 | fnvlist_add_uint64(spa->spa_load_info, | |
3192 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); | |
3193 | fnvlist_add_uint64(spa->spa_load_info, | |
3194 | ZPOOL_CONFIG_MMP_TXG, 0); | |
3195 | ||
3196 | error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); | |
3197 | } | |
3198 | ||
3199 | if (mmp_label) | |
3200 | nvlist_free(mmp_label); | |
3201 | ||
3202 | return (error); | |
3203 | } | |
3204 | ||
9eb7b46e | 3205 | static int |
6cb8e530 PZ |
3206 | spa_verify_host(spa_t *spa, nvlist_t *mos_config) |
3207 | { | |
3208 | uint64_t hostid; | |
3209 | char *hostname; | |
3210 | uint64_t myhostid = 0; | |
3211 | ||
3212 | if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, | |
3213 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
3214 | hostname = fnvlist_lookup_string(mos_config, | |
3215 | ZPOOL_CONFIG_HOSTNAME); | |
3216 | ||
3217 | myhostid = zone_get_hostid(NULL); | |
3218 | ||
3219 | if (hostid != 0 && myhostid != 0 && hostid != myhostid) { | |
3220 | cmn_err(CE_WARN, "pool '%s' could not be " | |
3221 | "loaded as it was last accessed by " | |
3222 | "another system (host: %s hostid: 0x%llx). " | |
3223 | "See: http://illumos.org/msg/ZFS-8000-EY", | |
3224 | spa_name(spa), hostname, (u_longlong_t)hostid); | |
3225 | spa_load_failed(spa, "hostid verification failed: pool " | |
3226 | "last accessed by host: %s (hostid: 0x%llx)", | |
3227 | hostname, (u_longlong_t)hostid); | |
3228 | return (SET_ERROR(EBADF)); | |
3229 | } | |
3230 | } | |
3231 | ||
3232 | return (0); | |
3233 | } | |
3234 | ||
3235 | static int | |
3236 | spa_ld_parse_config(spa_t *spa, spa_import_type_t type) | |
428870ff BB |
3237 | { |
3238 | int error = 0; | |
6cb8e530 | 3239 | nvlist_t *nvtree, *nvl, *config = spa->spa_config; |
1c27024e | 3240 | int parse; |
9eb7b46e | 3241 | vdev_t *rvd; |
6cb8e530 PZ |
3242 | uint64_t pool_guid; |
3243 | char *comment; | |
3244 | ||
3245 | /* | |
3246 | * Versioning wasn't explicitly added to the label until later, so if | |
3247 | * it's not present treat it as the initial version. | |
3248 | */ | |
3249 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
3250 | &spa->spa_ubsync.ub_version) != 0) | |
3251 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
3252 | ||
3253 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { | |
3254 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3255 | ZPOOL_CONFIG_POOL_GUID); | |
3256 | return (SET_ERROR(EINVAL)); | |
3257 | } | |
3258 | ||
d2734cce SD |
3259 | /* |
3260 | * If we are doing an import, ensure that the pool is not already | |
3261 | * imported by checking if its pool guid already exists in the | |
3262 | * spa namespace. | |
3263 | * | |
3264 | * The only case that we allow an already imported pool to be | |
3265 | * imported again, is when the pool is checkpointed and we want to | |
3266 | * look at its checkpointed state from userland tools like zdb. | |
3267 | */ | |
3268 | #ifdef _KERNEL | |
3269 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3270 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3271 | spa_guid_exists(pool_guid, 0)) { | |
3272 | #else | |
3273 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
3274 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
3275 | spa_guid_exists(pool_guid, 0) && | |
3276 | !spa_importing_readonly_checkpoint(spa)) { | |
3277 | #endif | |
6cb8e530 PZ |
3278 | spa_load_failed(spa, "a pool with guid %llu is already open", |
3279 | (u_longlong_t)pool_guid); | |
3280 | return (SET_ERROR(EEXIST)); | |
3281 | } | |
3282 | ||
3283 | spa->spa_config_guid = pool_guid; | |
3284 | ||
3285 | nvlist_free(spa->spa_load_info); | |
3286 | spa->spa_load_info = fnvlist_alloc(); | |
3287 | ||
3288 | ASSERT(spa->spa_comment == NULL); | |
3289 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
3290 | spa->spa_comment = spa_strdup(comment); | |
3291 | ||
3292 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
3293 | &spa->spa_config_txg); | |
3294 | ||
3295 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) | |
3296 | spa->spa_config_splitting = fnvlist_dup(nvl); | |
428870ff | 3297 | |
4a0ee12a PZ |
3298 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { |
3299 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
3300 | ZPOOL_CONFIG_VDEV_TREE); | |
2e528b49 | 3301 | return (SET_ERROR(EINVAL)); |
4a0ee12a | 3302 | } |
428870ff | 3303 | |
428870ff BB |
3304 | /* |
3305 | * Create "The Godfather" zio to hold all async IOs | |
3306 | */ | |
e022864d MA |
3307 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
3308 | KM_SLEEP); | |
1c27024e | 3309 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
3310 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
3311 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
3312 | ZIO_FLAG_GODFATHER); | |
3313 | } | |
428870ff BB |
3314 | |
3315 | /* | |
3316 | * Parse the configuration into a vdev tree. We explicitly set the | |
3317 | * value that will be returned by spa_version() since parsing the | |
3318 | * configuration requires knowing the version number. | |
3319 | */ | |
3320 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 PZ |
3321 | parse = (type == SPA_IMPORT_EXISTING ? |
3322 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
9eb7b46e | 3323 | error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); |
428870ff BB |
3324 | spa_config_exit(spa, SCL_ALL, FTAG); |
3325 | ||
4a0ee12a PZ |
3326 | if (error != 0) { |
3327 | spa_load_failed(spa, "unable to parse config [error=%d]", | |
3328 | error); | |
428870ff | 3329 | return (error); |
4a0ee12a | 3330 | } |
428870ff BB |
3331 | |
3332 | ASSERT(spa->spa_root_vdev == rvd); | |
c3520e7f MA |
3333 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); |
3334 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
428870ff BB |
3335 | |
3336 | if (type != SPA_IMPORT_ASSEMBLE) { | |
3337 | ASSERT(spa_guid(spa) == pool_guid); | |
3338 | } | |
3339 | ||
9eb7b46e PZ |
3340 | return (0); |
3341 | } | |
3342 | ||
6cb8e530 PZ |
3343 | /* |
3344 | * Recursively open all vdevs in the vdev tree. This function is called twice: | |
3345 | * first with the untrusted config, then with the trusted config. | |
3346 | */ | |
9eb7b46e PZ |
3347 | static int |
3348 | spa_ld_open_vdevs(spa_t *spa) | |
3349 | { | |
3350 | int error = 0; | |
3351 | ||
6cb8e530 PZ |
3352 | /* |
3353 | * spa_missing_tvds_allowed defines how many top-level vdevs can be | |
3354 | * missing/unopenable for the root vdev to be still considered openable. | |
3355 | */ | |
3356 | if (spa->spa_trust_config) { | |
3357 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; | |
3358 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { | |
3359 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; | |
3360 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { | |
3361 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; | |
3362 | } else { | |
3363 | spa->spa_missing_tvds_allowed = 0; | |
3364 | } | |
3365 | ||
3366 | spa->spa_missing_tvds_allowed = | |
3367 | MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); | |
3368 | ||
428870ff | 3369 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9eb7b46e | 3370 | error = vdev_open(spa->spa_root_vdev); |
428870ff | 3371 | spa_config_exit(spa, SCL_ALL, FTAG); |
6cb8e530 PZ |
3372 | |
3373 | if (spa->spa_missing_tvds != 0) { | |
3374 | spa_load_note(spa, "vdev tree has %lld missing top-level " | |
3375 | "vdevs.", (u_longlong_t)spa->spa_missing_tvds); | |
da92d5cb | 3376 | if (spa->spa_trust_config && (spa->spa_mode & SPA_MODE_WRITE)) { |
6cb8e530 PZ |
3377 | /* |
3378 | * Although theoretically we could allow users to open | |
3379 | * incomplete pools in RW mode, we'd need to add a lot | |
3380 | * of extra logic (e.g. adjust pool space to account | |
3381 | * for missing vdevs). | |
3382 | * This limitation also prevents users from accidentally | |
3383 | * opening the pool in RW mode during data recovery and | |
3384 | * damaging it further. | |
3385 | */ | |
3386 | spa_load_note(spa, "pools with missing top-level " | |
3387 | "vdevs can only be opened in read-only mode."); | |
3388 | error = SET_ERROR(ENXIO); | |
3389 | } else { | |
3390 | spa_load_note(spa, "current settings allow for maximum " | |
3391 | "%lld missing top-level vdevs at this stage.", | |
3392 | (u_longlong_t)spa->spa_missing_tvds_allowed); | |
3393 | } | |
3394 | } | |
4a0ee12a PZ |
3395 | if (error != 0) { |
3396 | spa_load_failed(spa, "unable to open vdev tree [error=%d]", | |
3397 | error); | |
3398 | } | |
6cb8e530 PZ |
3399 | if (spa->spa_missing_tvds != 0 || error != 0) |
3400 | vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); | |
9eb7b46e PZ |
3401 | |
3402 | return (error); | |
3403 | } | |
3404 | ||
6cb8e530 PZ |
3405 | /* |
3406 | * We need to validate the vdev labels against the configuration that | |
3407 | * we have in hand. This function is called twice: first with an untrusted | |
3408 | * config, then with a trusted config. The validation is more strict when the | |
3409 | * config is trusted. | |
3410 | */ | |
9eb7b46e | 3411 | static int |
6cb8e530 | 3412 | spa_ld_validate_vdevs(spa_t *spa) |
9eb7b46e PZ |
3413 | { |
3414 | int error = 0; | |
3415 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 3416 | |
6cb8e530 PZ |
3417 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3418 | error = vdev_validate(rvd); | |
3419 | spa_config_exit(spa, SCL_ALL, FTAG); | |
428870ff | 3420 | |
6cb8e530 PZ |
3421 | if (error != 0) { |
3422 | spa_load_failed(spa, "vdev_validate failed [error=%d]", error); | |
3423 | return (error); | |
3424 | } | |
428870ff | 3425 | |
6cb8e530 PZ |
3426 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { |
3427 | spa_load_failed(spa, "cannot open vdev tree after invalidating " | |
3428 | "some vdevs"); | |
3429 | vdev_dbgmsg_print_tree(rvd, 2); | |
3430 | return (SET_ERROR(ENXIO)); | |
428870ff BB |
3431 | } |
3432 | ||
9eb7b46e PZ |
3433 | return (0); |
3434 | } | |
3435 | ||
d2734cce SD |
3436 | static void |
3437 | spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) | |
3438 | { | |
3439 | spa->spa_state = POOL_STATE_ACTIVE; | |
3440 | spa->spa_ubsync = spa->spa_uberblock; | |
3441 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
3442 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
3443 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
3444 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
3445 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
3446 | spa->spa_prev_software_version = ub->ub_software_version; | |
3447 | } | |
3448 | ||
9eb7b46e | 3449 | static int |
6cb8e530 | 3450 | spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) |
9eb7b46e PZ |
3451 | { |
3452 | vdev_t *rvd = spa->spa_root_vdev; | |
3453 | nvlist_t *label; | |
3454 | uberblock_t *ub = &spa->spa_uberblock; | |
9eb7b46e PZ |
3455 | boolean_t activity_check = B_FALSE; |
3456 | ||
d2734cce SD |
3457 | /* |
3458 | * If we are opening the checkpointed state of the pool by | |
3459 | * rewinding to it, at this point we will have written the | |
3460 | * checkpointed uberblock to the vdev labels, so searching | |
3461 | * the labels will find the right uberblock. However, if | |
3462 | * we are opening the checkpointed state read-only, we have | |
3463 | * not modified the labels. Therefore, we must ignore the | |
3464 | * labels and continue using the spa_uberblock that was set | |
3465 | * by spa_ld_checkpoint_rewind. | |
3466 | * | |
3467 | * Note that it would be fine to ignore the labels when | |
3468 | * rewinding (opening writeable) as well. However, if we | |
3469 | * crash just after writing the labels, we will end up | |
3470 | * searching the labels. Doing so in the common case means | |
3471 | * that this code path gets exercised normally, rather than | |
3472 | * just in the edge case. | |
3473 | */ | |
3474 | if (ub->ub_checkpoint_txg != 0 && | |
3475 | spa_importing_readonly_checkpoint(spa)) { | |
3476 | spa_ld_select_uberblock_done(spa, ub); | |
3477 | return (0); | |
3478 | } | |
3479 | ||
428870ff BB |
3480 | /* |
3481 | * Find the best uberblock. | |
3482 | */ | |
9ae529ec | 3483 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
3484 | |
3485 | /* | |
3486 | * If we weren't able to find a single valid uberblock, return failure. | |
3487 | */ | |
9ae529ec CS |
3488 | if (ub->ub_txg == 0) { |
3489 | nvlist_free(label); | |
4a0ee12a | 3490 | spa_load_failed(spa, "no valid uberblock found"); |
428870ff | 3491 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 3492 | } |
428870ff | 3493 | |
ca95f70d OF |
3494 | if (spa->spa_load_max_txg != UINT64_MAX) { |
3495 | (void) spa_import_progress_set_max_txg(spa_guid(spa), | |
3496 | (u_longlong_t)spa->spa_load_max_txg); | |
3497 | } | |
4a0ee12a PZ |
3498 | spa_load_note(spa, "using uberblock with txg=%llu", |
3499 | (u_longlong_t)ub->ub_txg); | |
3500 | ||
3501 | ||
379ca9cf OF |
3502 | /* |
3503 | * For pools which have the multihost property on determine if the | |
3504 | * pool is truly inactive and can be safely imported. Prevent | |
3505 | * hosts which don't have a hostid set from importing the pool. | |
3506 | */ | |
6cb8e530 PZ |
3507 | activity_check = spa_activity_check_required(spa, ub, label, |
3508 | spa->spa_config); | |
379ca9cf | 3509 | if (activity_check) { |
379ca9cf | 3510 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && |
25f06d67 | 3511 | spa_get_hostid(spa) == 0) { |
379ca9cf OF |
3512 | nvlist_free(label); |
3513 | fnvlist_add_uint64(spa->spa_load_info, | |
3514 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3515 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3516 | } | |
3517 | ||
6cb8e530 | 3518 | int error = spa_activity_check(spa, ub, spa->spa_config); |
e889f0f5 OF |
3519 | if (error) { |
3520 | nvlist_free(label); | |
3521 | return (error); | |
3522 | } | |
3523 | ||
379ca9cf OF |
3524 | fnvlist_add_uint64(spa->spa_load_info, |
3525 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); | |
3526 | fnvlist_add_uint64(spa->spa_load_info, | |
3527 | ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); | |
060f0226 OF |
3528 | fnvlist_add_uint16(spa->spa_load_info, |
3529 | ZPOOL_CONFIG_MMP_SEQ, | |
3530 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)); | |
379ca9cf OF |
3531 | } |
3532 | ||
428870ff | 3533 | /* |
9ae529ec | 3534 | * If the pool has an unsupported version we can't open it. |
428870ff | 3535 | */ |
9ae529ec CS |
3536 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
3537 | nvlist_free(label); | |
4a0ee12a PZ |
3538 | spa_load_failed(spa, "version %llu is not supported", |
3539 | (u_longlong_t)ub->ub_version); | |
428870ff | 3540 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
3541 | } |
3542 | ||
3543 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3544 | nvlist_t *features; | |
3545 | ||
3546 | /* | |
3547 | * If we weren't able to find what's necessary for reading the | |
3548 | * MOS in the label, return failure. | |
3549 | */ | |
4a0ee12a PZ |
3550 | if (label == NULL) { |
3551 | spa_load_failed(spa, "label config unavailable"); | |
3552 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
3553 | ENXIO)); | |
3554 | } | |
3555 | ||
3556 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, | |
3557 | &features) != 0) { | |
9ae529ec | 3558 | nvlist_free(label); |
4a0ee12a PZ |
3559 | spa_load_failed(spa, "invalid label: '%s' missing", |
3560 | ZPOOL_CONFIG_FEATURES_FOR_READ); | |
9ae529ec CS |
3561 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3562 | ENXIO)); | |
3563 | } | |
3564 | ||
3565 | /* | |
3566 | * Update our in-core representation with the definitive values | |
3567 | * from the label. | |
3568 | */ | |
3569 | nvlist_free(spa->spa_label_features); | |
3570 | VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); | |
3571 | } | |
3572 | ||
3573 | nvlist_free(label); | |
3574 | ||
3575 | /* | |
3576 | * Look through entries in the label nvlist's features_for_read. If | |
3577 | * there is a feature listed there which we don't understand then we | |
3578 | * cannot open a pool. | |
3579 | */ | |
3580 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3581 | nvlist_t *unsup_feat; | |
9ae529ec CS |
3582 | |
3583 | VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == | |
3584 | 0); | |
3585 | ||
1c27024e DB |
3586 | for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, |
3587 | NULL); nvp != NULL; | |
9ae529ec CS |
3588 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { |
3589 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
3590 | VERIFY(nvlist_add_string(unsup_feat, | |
3591 | nvpair_name(nvp), "") == 0); | |
3592 | } | |
3593 | } | |
3594 | ||
3595 | if (!nvlist_empty(unsup_feat)) { | |
3596 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
3597 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); | |
3598 | nvlist_free(unsup_feat); | |
4a0ee12a | 3599 | spa_load_failed(spa, "some features are unsupported"); |
9ae529ec CS |
3600 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3601 | ENOTSUP)); | |
3602 | } | |
3603 | ||
3604 | nvlist_free(unsup_feat); | |
3605 | } | |
428870ff | 3606 | |
428870ff BB |
3607 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { |
3608 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 | 3609 | spa_try_repair(spa, spa->spa_config); |
428870ff BB |
3610 | spa_config_exit(spa, SCL_ALL, FTAG); |
3611 | nvlist_free(spa->spa_config_splitting); | |
3612 | spa->spa_config_splitting = NULL; | |
3613 | } | |
3614 | ||
3615 | /* | |
3616 | * Initialize internal SPA structures. | |
3617 | */ | |
d2734cce | 3618 | spa_ld_select_uberblock_done(spa, ub); |
428870ff | 3619 | |
9eb7b46e PZ |
3620 | return (0); |
3621 | } | |
3622 | ||
3623 | static int | |
3624 | spa_ld_open_rootbp(spa_t *spa) | |
3625 | { | |
3626 | int error = 0; | |
3627 | vdev_t *rvd = spa->spa_root_vdev; | |
a1d477c2 | 3628 | |
9ae529ec | 3629 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
4a0ee12a PZ |
3630 | if (error != 0) { |
3631 | spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " | |
3632 | "[error=%d]", error); | |
428870ff | 3633 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3634 | } |
428870ff BB |
3635 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; |
3636 | ||
9eb7b46e PZ |
3637 | return (0); |
3638 | } | |
3639 | ||
3640 | static int | |
d2734cce | 3641 | spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, |
6cb8e530 | 3642 | boolean_t reloading) |
9eb7b46e | 3643 | { |
6cb8e530 PZ |
3644 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
3645 | nvlist_t *nv, *mos_config, *policy; | |
3646 | int error = 0, copy_error; | |
3647 | uint64_t healthy_tvds, healthy_tvds_mos; | |
3648 | uint64_t mos_config_txg; | |
9eb7b46e | 3649 | |
4a0ee12a PZ |
3650 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) |
3651 | != 0) | |
428870ff BB |
3652 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3653 | ||
a1d477c2 | 3654 | /* |
6cb8e530 PZ |
3655 | * If we're assembling a pool from a split, the config provided is |
3656 | * already trusted so there is nothing to do. | |
a1d477c2 | 3657 | */ |
6cb8e530 PZ |
3658 | if (type == SPA_IMPORT_ASSEMBLE) |
3659 | return (0); | |
3660 | ||
3661 | healthy_tvds = spa_healthy_core_tvds(spa); | |
a1d477c2 | 3662 | |
6cb8e530 PZ |
3663 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) |
3664 | != 0) { | |
3665 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
3666 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3667 | } | |
3668 | ||
3669 | /* | |
3670 | * If we are doing an open, pool owner wasn't verified yet, thus do | |
3671 | * the verification here. | |
3672 | */ | |
3673 | if (spa->spa_load_state == SPA_LOAD_OPEN) { | |
3674 | error = spa_verify_host(spa, mos_config); | |
3675 | if (error != 0) { | |
a1d477c2 | 3676 | nvlist_free(mos_config); |
6cb8e530 | 3677 | return (error); |
a1d477c2 | 3678 | } |
6cb8e530 PZ |
3679 | } |
3680 | ||
3681 | nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); | |
a1d477c2 | 3682 | |
6cb8e530 PZ |
3683 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3684 | ||
3685 | /* | |
3686 | * Build a new vdev tree from the trusted config | |
3687 | */ | |
3688 | VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); | |
3689 | ||
3690 | /* | |
3691 | * Vdev paths in the MOS may be obsolete. If the untrusted config was | |
3692 | * obtained by scanning /dev/dsk, then it will have the right vdev | |
3693 | * paths. We update the trusted MOS config with this information. | |
3694 | * We first try to copy the paths with vdev_copy_path_strict, which | |
3695 | * succeeds only when both configs have exactly the same vdev tree. | |
3696 | * If that fails, we fall back to a more flexible method that has a | |
3697 | * best effort policy. | |
3698 | */ | |
3699 | copy_error = vdev_copy_path_strict(rvd, mrvd); | |
3700 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3701 | spa_load_note(spa, "provided vdev tree:"); | |
3702 | vdev_dbgmsg_print_tree(rvd, 2); | |
3703 | spa_load_note(spa, "MOS vdev tree:"); | |
3704 | vdev_dbgmsg_print_tree(mrvd, 2); | |
3705 | } | |
3706 | if (copy_error != 0) { | |
3707 | spa_load_note(spa, "vdev_copy_path_strict failed, falling " | |
3708 | "back to vdev_copy_path_relaxed"); | |
3709 | vdev_copy_path_relaxed(rvd, mrvd); | |
3710 | } | |
3711 | ||
3712 | vdev_close(rvd); | |
3713 | vdev_free(rvd); | |
3714 | spa->spa_root_vdev = mrvd; | |
3715 | rvd = mrvd; | |
3716 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3717 | ||
3718 | /* | |
3719 | * We will use spa_config if we decide to reload the spa or if spa_load | |
3720 | * fails and we rewind. We must thus regenerate the config using the | |
8a393be3 PZ |
3721 | * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to |
3722 | * pass settings on how to load the pool and is not stored in the MOS. | |
3723 | * We copy it over to our new, trusted config. | |
6cb8e530 PZ |
3724 | */ |
3725 | mos_config_txg = fnvlist_lookup_uint64(mos_config, | |
3726 | ZPOOL_CONFIG_POOL_TXG); | |
3727 | nvlist_free(mos_config); | |
3728 | mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); | |
8a393be3 | 3729 | if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, |
6cb8e530 | 3730 | &policy) == 0) |
8a393be3 | 3731 | fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); |
6cb8e530 PZ |
3732 | spa_config_set(spa, mos_config); |
3733 | spa->spa_config_source = SPA_CONFIG_SRC_MOS; | |
3734 | ||
3735 | /* | |
3736 | * Now that we got the config from the MOS, we should be more strict | |
3737 | * in checking blkptrs and can make assumptions about the consistency | |
3738 | * of the vdev tree. spa_trust_config must be set to true before opening | |
3739 | * vdevs in order for them to be writeable. | |
3740 | */ | |
3741 | spa->spa_trust_config = B_TRUE; | |
3742 | ||
3743 | /* | |
3744 | * Open and validate the new vdev tree | |
3745 | */ | |
3746 | error = spa_ld_open_vdevs(spa); | |
3747 | if (error != 0) | |
3748 | return (error); | |
3749 | ||
3750 | error = spa_ld_validate_vdevs(spa); | |
3751 | if (error != 0) | |
3752 | return (error); | |
3753 | ||
3754 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3755 | spa_load_note(spa, "final vdev tree:"); | |
3756 | vdev_dbgmsg_print_tree(rvd, 2); | |
3757 | } | |
3758 | ||
3759 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && | |
3760 | !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { | |
a1d477c2 | 3761 | /* |
6cb8e530 PZ |
3762 | * Sanity check to make sure that we are indeed loading the |
3763 | * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds | |
3764 | * in the config provided and they happened to be the only ones | |
3765 | * to have the latest uberblock, we could involuntarily perform | |
3766 | * an extreme rewind. | |
a1d477c2 | 3767 | */ |
6cb8e530 PZ |
3768 | healthy_tvds_mos = spa_healthy_core_tvds(spa); |
3769 | if (healthy_tvds_mos - healthy_tvds >= | |
3770 | SPA_SYNC_MIN_VDEVS) { | |
3771 | spa_load_note(spa, "config provided misses too many " | |
3772 | "top-level vdevs compared to MOS (%lld vs %lld). ", | |
3773 | (u_longlong_t)healthy_tvds, | |
3774 | (u_longlong_t)healthy_tvds_mos); | |
3775 | spa_load_note(spa, "vdev tree:"); | |
3776 | vdev_dbgmsg_print_tree(rvd, 2); | |
3777 | if (reloading) { | |
3778 | spa_load_failed(spa, "config was already " | |
3779 | "provided from MOS. Aborting."); | |
3780 | return (spa_vdev_err(rvd, | |
3781 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3782 | } | |
3783 | spa_load_note(spa, "spa must be reloaded using MOS " | |
3784 | "config"); | |
3785 | return (SET_ERROR(EAGAIN)); | |
4a0ee12a | 3786 | } |
a1d477c2 MA |
3787 | } |
3788 | ||
6cb8e530 PZ |
3789 | error = spa_check_for_missing_logs(spa); |
3790 | if (error != 0) | |
3791 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
3792 | ||
3793 | if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { | |
3794 | spa_load_failed(spa, "uberblock guid sum doesn't match MOS " | |
3795 | "guid sum (%llu != %llu)", | |
3796 | (u_longlong_t)spa->spa_uberblock.ub_guid_sum, | |
3797 | (u_longlong_t)rvd->vdev_guid_sum); | |
3798 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
3799 | ENXIO)); | |
3800 | } | |
3801 | ||
9eb7b46e PZ |
3802 | return (0); |
3803 | } | |
3804 | ||
3805 | static int | |
3806 | spa_ld_open_indirect_vdev_metadata(spa_t *spa) | |
3807 | { | |
3808 | int error = 0; | |
3809 | vdev_t *rvd = spa->spa_root_vdev; | |
3810 | ||
a1d477c2 MA |
3811 | /* |
3812 | * Everything that we read before spa_remove_init() must be stored | |
3813 | * on concreted vdevs. Therefore we do this as early as possible. | |
3814 | */ | |
4a0ee12a PZ |
3815 | error = spa_remove_init(spa); |
3816 | if (error != 0) { | |
3817 | spa_load_failed(spa, "spa_remove_init failed [error=%d]", | |
3818 | error); | |
a1d477c2 | 3819 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3820 | } |
a1d477c2 | 3821 | |
9eb7b46e PZ |
3822 | /* |
3823 | * Retrieve information needed to condense indirect vdev mappings. | |
3824 | */ | |
3825 | error = spa_condense_init(spa); | |
3826 | if (error != 0) { | |
4a0ee12a PZ |
3827 | spa_load_failed(spa, "spa_condense_init failed [error=%d]", |
3828 | error); | |
9eb7b46e PZ |
3829 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3830 | } | |
3831 | ||
3832 | return (0); | |
3833 | } | |
3834 | ||
3835 | static int | |
4a0ee12a | 3836 | spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) |
9eb7b46e PZ |
3837 | { |
3838 | int error = 0; | |
3839 | vdev_t *rvd = spa->spa_root_vdev; | |
3840 | ||
9ae529ec CS |
3841 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
3842 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 3843 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
3844 | |
3845 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
4a0ee12a | 3846 | &spa->spa_feat_for_read_obj, B_TRUE) != 0) { |
9ae529ec CS |
3847 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3848 | } | |
3849 | ||
3850 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
4a0ee12a | 3851 | &spa->spa_feat_for_write_obj, B_TRUE) != 0) { |
9ae529ec CS |
3852 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3853 | } | |
3854 | ||
3855 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
4a0ee12a | 3856 | &spa->spa_feat_desc_obj, B_TRUE) != 0) { |
9ae529ec CS |
3857 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3858 | } | |
3859 | ||
b9b24bb4 CS |
3860 | enabled_feat = fnvlist_alloc(); |
3861 | unsup_feat = fnvlist_alloc(); | |
9ae529ec | 3862 | |
fa86b5db | 3863 | if (!spa_features_check(spa, B_FALSE, |
b9b24bb4 | 3864 | unsup_feat, enabled_feat)) |
9ae529ec CS |
3865 | missing_feat_read = B_TRUE; |
3866 | ||
4a0ee12a PZ |
3867 | if (spa_writeable(spa) || |
3868 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) { | |
fa86b5db | 3869 | if (!spa_features_check(spa, B_TRUE, |
b9b24bb4 | 3870 | unsup_feat, enabled_feat)) { |
9eb7b46e | 3871 | *missing_feat_writep = B_TRUE; |
b9b24bb4 | 3872 | } |
9ae529ec CS |
3873 | } |
3874 | ||
b9b24bb4 CS |
3875 | fnvlist_add_nvlist(spa->spa_load_info, |
3876 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
3877 | ||
9ae529ec | 3878 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
3879 | fnvlist_add_nvlist(spa->spa_load_info, |
3880 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
3881 | } |
3882 | ||
b9b24bb4 CS |
3883 | fnvlist_free(enabled_feat); |
3884 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
3885 | |
3886 | if (!missing_feat_read) { | |
3887 | fnvlist_add_boolean(spa->spa_load_info, | |
3888 | ZPOOL_CONFIG_CAN_RDONLY); | |
3889 | } | |
3890 | ||
3891 | /* | |
3892 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
3893 | * twofold: to determine whether the pool is available for | |
3894 | * import in read-write mode and (if it is not) whether the | |
3895 | * pool is available for import in read-only mode. If the pool | |
3896 | * is available for import in read-write mode, it is displayed | |
3897 | * as available in userland; if it is not available for import | |
3898 | * in read-only mode, it is displayed as unavailable in | |
3899 | * userland. If the pool is available for import in read-only | |
3900 | * mode but not read-write mode, it is displayed as unavailable | |
3901 | * in userland with a special note that the pool is actually | |
3902 | * available for open in read-only mode. | |
3903 | * | |
3904 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
3905 | * missing a feature for write, we must first determine whether | |
3906 | * the pool can be opened read-only before returning to | |
3907 | * userland in order to know whether to display the | |
3908 | * abovementioned note. | |
3909 | */ | |
9eb7b46e | 3910 | if (missing_feat_read || (*missing_feat_writep && |
9ae529ec | 3911 | spa_writeable(spa))) { |
4a0ee12a | 3912 | spa_load_failed(spa, "pool uses unsupported features"); |
9ae529ec CS |
3913 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3914 | ENOTSUP)); | |
3915 | } | |
b0bc7a84 MG |
3916 | |
3917 | /* | |
3918 | * Load refcounts for ZFS features from disk into an in-memory | |
3919 | * cache during SPA initialization. | |
3920 | */ | |
1c27024e | 3921 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { |
b0bc7a84 MG |
3922 | uint64_t refcount; |
3923 | ||
3924 | error = feature_get_refcount_from_disk(spa, | |
3925 | &spa_feature_table[i], &refcount); | |
3926 | if (error == 0) { | |
3927 | spa->spa_feat_refcount_cache[i] = refcount; | |
3928 | } else if (error == ENOTSUP) { | |
3929 | spa->spa_feat_refcount_cache[i] = | |
3930 | SPA_FEATURE_DISABLED; | |
3931 | } else { | |
4a0ee12a PZ |
3932 | spa_load_failed(spa, "error getting refcount " |
3933 | "for feature %s [error=%d]", | |
3934 | spa_feature_table[i].fi_guid, error); | |
b0bc7a84 MG |
3935 | return (spa_vdev_err(rvd, |
3936 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3937 | } | |
3938 | } | |
3939 | } | |
3940 | ||
3941 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
3942 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
4a0ee12a | 3943 | &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) |
b0bc7a84 | 3944 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
9ae529ec CS |
3945 | } |
3946 | ||
f00ab3f2 TC |
3947 | /* |
3948 | * Encryption was added before bookmark_v2, even though bookmark_v2 | |
3949 | * is now a dependency. If this pool has encryption enabled without | |
3950 | * bookmark_v2, trigger an errata message. | |
3951 | */ | |
3952 | if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && | |
3953 | !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) { | |
3954 | spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; | |
3955 | } | |
3956 | ||
9eb7b46e PZ |
3957 | return (0); |
3958 | } | |
3959 | ||
3960 | static int | |
3961 | spa_ld_load_special_directories(spa_t *spa) | |
3962 | { | |
3963 | int error = 0; | |
3964 | vdev_t *rvd = spa->spa_root_vdev; | |
3965 | ||
9ae529ec CS |
3966 | spa->spa_is_initializing = B_TRUE; |
3967 | error = dsl_pool_open(spa->spa_dsl_pool); | |
3968 | spa->spa_is_initializing = B_FALSE; | |
4a0ee12a PZ |
3969 | if (error != 0) { |
3970 | spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); | |
9ae529ec | 3971 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3972 | } |
9ae529ec | 3973 | |
9eb7b46e PZ |
3974 | return (0); |
3975 | } | |
428870ff | 3976 | |
9eb7b46e PZ |
3977 | static int |
3978 | spa_ld_get_props(spa_t *spa) | |
3979 | { | |
3980 | int error = 0; | |
3981 | uint64_t obj; | |
3982 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 3983 | |
3c67d83a TH |
3984 | /* Grab the checksum salt from the MOS. */ |
3985 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3986 | DMU_POOL_CHECKSUM_SALT, 1, | |
3987 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
3988 | spa->spa_cksum_salt.zcs_bytes); | |
3989 | if (error == ENOENT) { | |
3990 | /* Generate a new salt for subsequent use */ | |
3991 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
3992 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
3993 | } else if (error != 0) { | |
4a0ee12a PZ |
3994 | spa_load_failed(spa, "unable to retrieve checksum salt from " |
3995 | "MOS [error=%d]", error); | |
3c67d83a TH |
3996 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3997 | } | |
3998 | ||
4a0ee12a | 3999 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) |
428870ff BB |
4000 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4001 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
4a0ee12a PZ |
4002 | if (error != 0) { |
4003 | spa_load_failed(spa, "error opening deferred-frees bpobj " | |
4004 | "[error=%d]", error); | |
428870ff | 4005 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4006 | } |
34dc7c2f BB |
4007 | |
4008 | /* | |
4009 | * Load the bit that tells us to use the new accounting function | |
4010 | * (raid-z deflation). If we have an older pool, this will not | |
4011 | * be present. | |
4012 | */ | |
4a0ee12a | 4013 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); |
428870ff BB |
4014 | if (error != 0 && error != ENOENT) |
4015 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4016 | ||
4017 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
4a0ee12a | 4018 | &spa->spa_creation_version, B_FALSE); |
428870ff BB |
4019 | if (error != 0 && error != ENOENT) |
4020 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
4021 | |
4022 | /* | |
4023 | * Load the persistent error log. If we have an older pool, this will | |
4024 | * not be present. | |
4025 | */ | |
4a0ee12a PZ |
4026 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, |
4027 | B_FALSE); | |
428870ff BB |
4028 | if (error != 0 && error != ENOENT) |
4029 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4030 | |
428870ff | 4031 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
4a0ee12a | 4032 | &spa->spa_errlog_scrub, B_FALSE); |
428870ff BB |
4033 | if (error != 0 && error != ENOENT) |
4034 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 4035 | |
37f03da8 SH |
4036 | /* |
4037 | * Load the livelist deletion field. If a livelist is queued for | |
4038 | * deletion, indicate that in the spa | |
4039 | */ | |
4040 | error = spa_dir_prop(spa, DMU_POOL_DELETED_CLONES, | |
4041 | &spa->spa_livelists_to_delete, B_FALSE); | |
4042 | if (error != 0 && error != ENOENT) | |
4043 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4044 | ||
34dc7c2f BB |
4045 | /* |
4046 | * Load the history object. If we have an older pool, this | |
4047 | * will not be present. | |
4048 | */ | |
4a0ee12a | 4049 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); |
428870ff BB |
4050 | if (error != 0 && error != ENOENT) |
4051 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4052 | ||
e0ab3ab5 JS |
4053 | /* |
4054 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
4055 | * be present; in this case, defer its creation to a later time to | |
4056 | * avoid dirtying the MOS this early / out of sync context. See | |
4057 | * spa_sync_config_object. | |
4058 | */ | |
4059 | ||
4060 | /* The sentinel is only available in the MOS config. */ | |
1c27024e | 4061 | nvlist_t *mos_config; |
4a0ee12a PZ |
4062 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { |
4063 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
e0ab3ab5 | 4064 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4065 | } |
e0ab3ab5 JS |
4066 | |
4067 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
4a0ee12a | 4068 | &spa->spa_all_vdev_zaps, B_FALSE); |
e0ab3ab5 | 4069 | |
38640550 DB |
4070 | if (error == ENOENT) { |
4071 | VERIFY(!nvlist_exists(mos_config, | |
4072 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
4073 | spa->spa_avz_action = AVZ_ACTION_INITIALIZE; | |
4074 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4075 | } else if (error != 0) { | |
e0ab3ab5 | 4076 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
38640550 | 4077 | } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { |
e0ab3ab5 JS |
4078 | /* |
4079 | * An older version of ZFS overwrote the sentinel value, so | |
4080 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
4081 | * destruction to later; see spa_sync_config_object. | |
4082 | */ | |
4083 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
4084 | /* | |
4085 | * We're assuming that no vdevs have had their ZAPs created | |
4086 | * before this. Better be sure of it. | |
4087 | */ | |
4088 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
4089 | } | |
4090 | nvlist_free(mos_config); | |
4091 | ||
9eb7b46e PZ |
4092 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
4093 | ||
4a0ee12a PZ |
4094 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, |
4095 | B_FALSE); | |
9eb7b46e PZ |
4096 | if (error && error != ENOENT) |
4097 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4098 | ||
4099 | if (error == 0) { | |
4100 | uint64_t autoreplace; | |
4101 | ||
4102 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
4103 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
4104 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
4105 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
4106 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
c02c1bec | 4107 | spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); |
1b939560 | 4108 | spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim); |
9eb7b46e PZ |
4109 | spa->spa_autoreplace = (autoreplace != 0); |
4110 | } | |
4111 | ||
6cb8e530 PZ |
4112 | /* |
4113 | * If we are importing a pool with missing top-level vdevs, | |
4114 | * we enforce that the pool doesn't panic or get suspended on | |
4115 | * error since the likelihood of missing data is extremely high. | |
4116 | */ | |
4117 | if (spa->spa_missing_tvds > 0 && | |
4118 | spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && | |
4119 | spa->spa_load_state != SPA_LOAD_TRYIMPORT) { | |
4120 | spa_load_note(spa, "forcing failmode to 'continue' " | |
4121 | "as some top level vdevs are missing"); | |
4122 | spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; | |
4123 | } | |
4124 | ||
9eb7b46e PZ |
4125 | return (0); |
4126 | } | |
4127 | ||
4128 | static int | |
4129 | spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) | |
4130 | { | |
4131 | int error = 0; | |
4132 | vdev_t *rvd = spa->spa_root_vdev; | |
4133 | ||
428870ff BB |
4134 | /* |
4135 | * If we're assembling the pool from the split-off vdevs of | |
4136 | * an existing pool, we don't want to attach the spares & cache | |
4137 | * devices. | |
4138 | */ | |
34dc7c2f BB |
4139 | |
4140 | /* | |
4141 | * Load any hot spares for this pool. | |
4142 | */ | |
4a0ee12a PZ |
4143 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, |
4144 | B_FALSE); | |
428870ff BB |
4145 | if (error != 0 && error != ENOENT) |
4146 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4147 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4148 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
4149 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
4a0ee12a PZ |
4150 | &spa->spa_spares.sav_config) != 0) { |
4151 | spa_load_failed(spa, "error loading spares nvlist"); | |
428870ff | 4152 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4153 | } |
34dc7c2f | 4154 | |
b128c09f | 4155 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4156 | spa_load_spares(spa); |
b128c09f | 4157 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4158 | } else if (error == 0) { |
4159 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
4160 | } |
4161 | ||
4162 | /* | |
4163 | * Load any level 2 ARC devices for this pool. | |
4164 | */ | |
428870ff | 4165 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
4a0ee12a | 4166 | &spa->spa_l2cache.sav_object, B_FALSE); |
428870ff BB |
4167 | if (error != 0 && error != ENOENT) |
4168 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
4169 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
4170 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
4171 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
4a0ee12a PZ |
4172 | &spa->spa_l2cache.sav_config) != 0) { |
4173 | spa_load_failed(spa, "error loading l2cache nvlist"); | |
428870ff | 4174 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4175 | } |
34dc7c2f | 4176 | |
b128c09f | 4177 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4178 | spa_load_l2cache(spa); |
b128c09f | 4179 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
4180 | } else if (error == 0) { |
4181 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
4182 | } |
4183 | ||
9eb7b46e PZ |
4184 | return (0); |
4185 | } | |
428870ff | 4186 | |
9eb7b46e | 4187 | static int |
4a0ee12a | 4188 | spa_ld_load_vdev_metadata(spa_t *spa) |
9eb7b46e PZ |
4189 | { |
4190 | int error = 0; | |
4191 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 4192 | |
379ca9cf OF |
4193 | /* |
4194 | * If the 'multihost' property is set, then never allow a pool to | |
4195 | * be imported when the system hostid is zero. The exception to | |
4196 | * this rule is zdb which is always allowed to access pools. | |
4197 | */ | |
25f06d67 | 4198 | if (spa_multihost(spa) && spa_get_hostid(spa) == 0 && |
379ca9cf OF |
4199 | (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { |
4200 | fnvlist_add_uint64(spa->spa_load_info, | |
4201 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
4202 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
4203 | } | |
4204 | ||
34dc7c2f BB |
4205 | /* |
4206 | * If the 'autoreplace' property is set, then post a resource notifying | |
4207 | * the ZFS DE that it should not issue any faults for unopenable | |
4208 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
4209 | * unopenable vdevs so that the normal autoreplace handler can take | |
4210 | * over. | |
4211 | */ | |
4a0ee12a | 4212 | if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 4213 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
4214 | /* |
4215 | * For the import case, this is done in spa_import(), because | |
4216 | * at this point we're using the spare definitions from | |
4217 | * the MOS config, not necessarily from the userland config. | |
4218 | */ | |
4a0ee12a | 4219 | if (spa->spa_load_state != SPA_LOAD_IMPORT) { |
428870ff BB |
4220 | spa_aux_check_removed(&spa->spa_spares); |
4221 | spa_aux_check_removed(&spa->spa_l2cache); | |
4222 | } | |
4223 | } | |
34dc7c2f BB |
4224 | |
4225 | /* | |
9eb7b46e | 4226 | * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. |
34dc7c2f | 4227 | */ |
a1d477c2 MA |
4228 | error = vdev_load(rvd); |
4229 | if (error != 0) { | |
4a0ee12a | 4230 | spa_load_failed(spa, "vdev_load failed [error=%d]", error); |
a1d477c2 MA |
4231 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
4232 | } | |
4233 | ||
93e28d66 SD |
4234 | error = spa_ld_log_spacemaps(spa); |
4235 | if (error != 0) { | |
4236 | spa_load_failed(spa, "spa_ld_log_sm_data failed [error=%d]", | |
4237 | error); | |
4238 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); | |
4239 | } | |
4240 | ||
34dc7c2f | 4241 | /* |
9eb7b46e | 4242 | * Propagate the leaf DTLs we just loaded all the way up the vdev tree. |
34dc7c2f | 4243 | */ |
b128c09f | 4244 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9a49d3f3 | 4245 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE, B_FALSE); |
b128c09f | 4246 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 4247 | |
9eb7b46e PZ |
4248 | return (0); |
4249 | } | |
4250 | ||
4251 | static int | |
4252 | spa_ld_load_dedup_tables(spa_t *spa) | |
4253 | { | |
4254 | int error = 0; | |
4255 | vdev_t *rvd = spa->spa_root_vdev; | |
4256 | ||
428870ff | 4257 | error = ddt_load(spa); |
4a0ee12a PZ |
4258 | if (error != 0) { |
4259 | spa_load_failed(spa, "ddt_load failed [error=%d]", error); | |
428870ff | 4260 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 4261 | } |
428870ff | 4262 | |
9eb7b46e PZ |
4263 | return (0); |
4264 | } | |
4265 | ||
4266 | static int | |
4267 | spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) | |
4268 | { | |
4269 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 4270 | |
4a0ee12a PZ |
4271 | if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { |
4272 | boolean_t missing = spa_check_logs(spa); | |
4273 | if (missing) { | |
6cb8e530 PZ |
4274 | if (spa->spa_missing_tvds != 0) { |
4275 | spa_load_note(spa, "spa_check_logs failed " | |
4276 | "so dropping the logs"); | |
4277 | } else { | |
4278 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
4279 | spa_load_failed(spa, "spa_check_logs failed"); | |
4280 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, | |
4281 | ENXIO)); | |
4282 | } | |
4a0ee12a | 4283 | } |
428870ff BB |
4284 | } |
4285 | ||
9eb7b46e PZ |
4286 | return (0); |
4287 | } | |
4288 | ||
4289 | static int | |
4a0ee12a | 4290 | spa_ld_verify_pool_data(spa_t *spa) |
9eb7b46e PZ |
4291 | { |
4292 | int error = 0; | |
4293 | vdev_t *rvd = spa->spa_root_vdev; | |
4294 | ||
4295 | /* | |
4296 | * We've successfully opened the pool, verify that we're ready | |
4297 | * to start pushing transactions. | |
4298 | */ | |
4a0ee12a | 4299 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
9eb7b46e PZ |
4300 | error = spa_load_verify(spa); |
4301 | if (error != 0) { | |
4a0ee12a PZ |
4302 | spa_load_failed(spa, "spa_load_verify failed " |
4303 | "[error=%d]", error); | |
9eb7b46e PZ |
4304 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
4305 | error)); | |
4306 | } | |
4307 | } | |
4308 | ||
4309 | return (0); | |
4310 | } | |
4311 | ||
4312 | static void | |
4313 | spa_ld_claim_log_blocks(spa_t *spa) | |
4314 | { | |
4315 | dmu_tx_t *tx; | |
4316 | dsl_pool_t *dp = spa_get_dsl(spa); | |
4317 | ||
4318 | /* | |
4319 | * Claim log blocks that haven't been committed yet. | |
4320 | * This must all happen in a single txg. | |
4321 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
4322 | * invoked from zil_claim_log_block()'s i/o done callback. | |
4323 | * Price of rollback is that we abandon the log. | |
4324 | */ | |
4325 | spa->spa_claiming = B_TRUE; | |
4326 | ||
4327 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
4328 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
4329 | zil_claim, tx, DS_FIND_CHILDREN); | |
4330 | dmu_tx_commit(tx); | |
4331 | ||
4332 | spa->spa_claiming = B_FALSE; | |
4333 | ||
4334 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
4335 | } | |
4336 | ||
4337 | static void | |
6cb8e530 | 4338 | spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, |
d2734cce | 4339 | boolean_t update_config_cache) |
9eb7b46e PZ |
4340 | { |
4341 | vdev_t *rvd = spa->spa_root_vdev; | |
4342 | int need_update = B_FALSE; | |
4343 | ||
4344 | /* | |
4345 | * If the config cache is stale, or we have uninitialized | |
4346 | * metaslabs (see spa_vdev_add()), then update the config. | |
4347 | * | |
4348 | * If this is a verbatim import, trust the current | |
4349 | * in-core spa_config and update the disk labels. | |
4350 | */ | |
d2734cce | 4351 | if (update_config_cache || config_cache_txg != spa->spa_config_txg || |
4a0ee12a PZ |
4352 | spa->spa_load_state == SPA_LOAD_IMPORT || |
4353 | spa->spa_load_state == SPA_LOAD_RECOVER || | |
9eb7b46e PZ |
4354 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) |
4355 | need_update = B_TRUE; | |
4356 | ||
4357 | for (int c = 0; c < rvd->vdev_children; c++) | |
4358 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
4359 | need_update = B_TRUE; | |
4360 | ||
4361 | /* | |
e1cfd73f | 4362 | * Update the config cache asynchronously in case we're the |
9eb7b46e PZ |
4363 | * root pool, in which case the config cache isn't writable yet. |
4364 | */ | |
4365 | if (need_update) | |
4366 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
4367 | } | |
4368 | ||
6cb8e530 PZ |
4369 | static void |
4370 | spa_ld_prepare_for_reload(spa_t *spa) | |
4371 | { | |
da92d5cb | 4372 | spa_mode_t mode = spa->spa_mode; |
6cb8e530 PZ |
4373 | int async_suspended = spa->spa_async_suspended; |
4374 | ||
4375 | spa_unload(spa); | |
4376 | spa_deactivate(spa); | |
4377 | spa_activate(spa, mode); | |
4378 | ||
4379 | /* | |
4380 | * We save the value of spa_async_suspended as it gets reset to 0 by | |
4381 | * spa_unload(). We want to restore it back to the original value before | |
4382 | * returning as we might be calling spa_async_resume() later. | |
4383 | */ | |
4384 | spa->spa_async_suspended = async_suspended; | |
4385 | } | |
4386 | ||
9eb7b46e | 4387 | static int |
d2734cce SD |
4388 | spa_ld_read_checkpoint_txg(spa_t *spa) |
4389 | { | |
4390 | uberblock_t checkpoint; | |
4391 | int error = 0; | |
4392 | ||
4393 | ASSERT0(spa->spa_checkpoint_txg); | |
4394 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4395 | ||
4396 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4397 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4398 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4399 | ||
4400 | if (error == ENOENT) | |
4401 | return (0); | |
4402 | ||
4403 | if (error != 0) | |
4404 | return (error); | |
4405 | ||
4406 | ASSERT3U(checkpoint.ub_txg, !=, 0); | |
4407 | ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); | |
4408 | ASSERT3U(checkpoint.ub_timestamp, !=, 0); | |
4409 | spa->spa_checkpoint_txg = checkpoint.ub_txg; | |
4410 | spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; | |
4411 | ||
4412 | return (0); | |
4413 | } | |
4414 | ||
4415 | static int | |
4416 | spa_ld_mos_init(spa_t *spa, spa_import_type_t type) | |
9eb7b46e PZ |
4417 | { |
4418 | int error = 0; | |
9eb7b46e | 4419 | |
4a0ee12a | 4420 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6cb8e530 | 4421 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); |
4a0ee12a | 4422 | |
9eb7b46e | 4423 | /* |
6cb8e530 PZ |
4424 | * Never trust the config that is provided unless we are assembling |
4425 | * a pool following a split. | |
4426 | * This means don't trust blkptrs and the vdev tree in general. This | |
4427 | * also effectively puts the spa in read-only mode since | |
4428 | * spa_writeable() checks for spa_trust_config to be true. | |
4429 | * We will later load a trusted config from the MOS. | |
9eb7b46e | 4430 | */ |
6cb8e530 PZ |
4431 | if (type != SPA_IMPORT_ASSEMBLE) |
4432 | spa->spa_trust_config = B_FALSE; | |
4433 | ||
9eb7b46e PZ |
4434 | /* |
4435 | * Parse the config provided to create a vdev tree. | |
4436 | */ | |
6cb8e530 | 4437 | error = spa_ld_parse_config(spa, type); |
9eb7b46e PZ |
4438 | if (error != 0) |
4439 | return (error); | |
4440 | ||
ca95f70d OF |
4441 | spa_import_progress_add(spa); |
4442 | ||
9eb7b46e PZ |
4443 | /* |
4444 | * Now that we have the vdev tree, try to open each vdev. This involves | |
4445 | * opening the underlying physical device, retrieving its geometry and | |
4446 | * probing the vdev with a dummy I/O. The state of each vdev will be set | |
4447 | * based on the success of those operations. After this we'll be ready | |
4448 | * to read from the vdevs. | |
4449 | */ | |
4450 | error = spa_ld_open_vdevs(spa); | |
4451 | if (error != 0) | |
4452 | return (error); | |
4453 | ||
4454 | /* | |
4455 | * Read the label of each vdev and make sure that the GUIDs stored | |
4456 | * there match the GUIDs in the config provided. | |
6cb8e530 PZ |
4457 | * If we're assembling a new pool that's been split off from an |
4458 | * existing pool, the labels haven't yet been updated so we skip | |
4459 | * validation for now. | |
9eb7b46e | 4460 | */ |
6cb8e530 PZ |
4461 | if (type != SPA_IMPORT_ASSEMBLE) { |
4462 | error = spa_ld_validate_vdevs(spa); | |
4463 | if (error != 0) | |
4464 | return (error); | |
4465 | } | |
9eb7b46e PZ |
4466 | |
4467 | /* | |
d2734cce SD |
4468 | * Read all vdev labels to find the best uberblock (i.e. latest, |
4469 | * unless spa_load_max_txg is set) and store it in spa_uberblock. We | |
4470 | * get the list of features required to read blkptrs in the MOS from | |
4471 | * the vdev label with the best uberblock and verify that our version | |
4472 | * of zfs supports them all. | |
9eb7b46e | 4473 | */ |
6cb8e530 | 4474 | error = spa_ld_select_uberblock(spa, type); |
9eb7b46e PZ |
4475 | if (error != 0) |
4476 | return (error); | |
4477 | ||
4478 | /* | |
4479 | * Pass that uberblock to the dsl_pool layer which will open the root | |
4480 | * blkptr. This blkptr points to the latest version of the MOS and will | |
4481 | * allow us to read its contents. | |
4482 | */ | |
4483 | error = spa_ld_open_rootbp(spa); | |
4484 | if (error != 0) | |
4485 | return (error); | |
4486 | ||
d2734cce SD |
4487 | return (0); |
4488 | } | |
4489 | ||
4490 | static int | |
4491 | spa_ld_checkpoint_rewind(spa_t *spa) | |
4492 | { | |
4493 | uberblock_t checkpoint; | |
4494 | int error = 0; | |
4495 | ||
4496 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4497 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4498 | ||
4499 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
4500 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
4501 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
4502 | ||
4503 | if (error != 0) { | |
4504 | spa_load_failed(spa, "unable to retrieve checkpointed " | |
4505 | "uberblock from the MOS config [error=%d]", error); | |
4506 | ||
4507 | if (error == ENOENT) | |
4508 | error = ZFS_ERR_NO_CHECKPOINT; | |
4509 | ||
4510 | return (error); | |
4511 | } | |
4512 | ||
4513 | ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); | |
4514 | ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); | |
4515 | ||
4516 | /* | |
4517 | * We need to update the txg and timestamp of the checkpointed | |
4518 | * uberblock to be higher than the latest one. This ensures that | |
4519 | * the checkpointed uberblock is selected if we were to close and | |
4520 | * reopen the pool right after we've written it in the vdev labels. | |
4521 | * (also see block comment in vdev_uberblock_compare) | |
4522 | */ | |
4523 | checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; | |
4524 | checkpoint.ub_timestamp = gethrestime_sec(); | |
4525 | ||
4526 | /* | |
4527 | * Set current uberblock to be the checkpointed uberblock. | |
4528 | */ | |
4529 | spa->spa_uberblock = checkpoint; | |
4530 | ||
4531 | /* | |
4532 | * If we are doing a normal rewind, then the pool is open for | |
4533 | * writing and we sync the "updated" checkpointed uberblock to | |
4534 | * disk. Once this is done, we've basically rewound the whole | |
4535 | * pool and there is no way back. | |
4536 | * | |
4537 | * There are cases when we don't want to attempt and sync the | |
4538 | * checkpointed uberblock to disk because we are opening a | |
4539 | * pool as read-only. Specifically, verifying the checkpointed | |
4540 | * state with zdb, and importing the checkpointed state to get | |
4541 | * a "preview" of its content. | |
4542 | */ | |
4543 | if (spa_writeable(spa)) { | |
4544 | vdev_t *rvd = spa->spa_root_vdev; | |
4545 | ||
4546 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4547 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; | |
4548 | int svdcount = 0; | |
4549 | int children = rvd->vdev_children; | |
4550 | int c0 = spa_get_random(children); | |
4551 | ||
4552 | for (int c = 0; c < children; c++) { | |
4553 | vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; | |
4554 | ||
4555 | /* Stop when revisiting the first vdev */ | |
4556 | if (c > 0 && svd[0] == vd) | |
4557 | break; | |
4558 | ||
4559 | if (vd->vdev_ms_array == 0 || vd->vdev_islog || | |
4560 | !vdev_is_concrete(vd)) | |
4561 | continue; | |
4562 | ||
4563 | svd[svdcount++] = vd; | |
4564 | if (svdcount == SPA_SYNC_MIN_VDEVS) | |
4565 | break; | |
4566 | } | |
4567 | error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); | |
4568 | if (error == 0) | |
4569 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
4570 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4571 | ||
4572 | if (error != 0) { | |
4573 | spa_load_failed(spa, "failed to write checkpointed " | |
4574 | "uberblock to the vdev labels [error=%d]", error); | |
4575 | return (error); | |
4576 | } | |
4577 | } | |
4578 | ||
4579 | return (0); | |
4580 | } | |
4581 | ||
4582 | static int | |
4583 | spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, | |
4584 | boolean_t *update_config_cache) | |
4585 | { | |
4586 | int error; | |
4587 | ||
4588 | /* | |
4589 | * Parse the config for pool, open and validate vdevs, | |
4590 | * select an uberblock, and use that uberblock to open | |
4591 | * the MOS. | |
4592 | */ | |
4593 | error = spa_ld_mos_init(spa, type); | |
4594 | if (error != 0) | |
4595 | return (error); | |
4596 | ||
9eb7b46e | 4597 | /* |
6cb8e530 PZ |
4598 | * Retrieve the trusted config stored in the MOS and use it to create |
4599 | * a new, exact version of the vdev tree, then reopen all vdevs. | |
9eb7b46e | 4600 | */ |
d2734cce | 4601 | error = spa_ld_trusted_config(spa, type, B_FALSE); |
6cb8e530 | 4602 | if (error == EAGAIN) { |
d2734cce SD |
4603 | if (update_config_cache != NULL) |
4604 | *update_config_cache = B_TRUE; | |
4605 | ||
6cb8e530 PZ |
4606 | /* |
4607 | * Redo the loading process with the trusted config if it is | |
4608 | * too different from the untrusted config. | |
4609 | */ | |
4610 | spa_ld_prepare_for_reload(spa); | |
d2734cce SD |
4611 | spa_load_note(spa, "RELOADING"); |
4612 | error = spa_ld_mos_init(spa, type); | |
4613 | if (error != 0) | |
4614 | return (error); | |
4615 | ||
4616 | error = spa_ld_trusted_config(spa, type, B_TRUE); | |
4617 | if (error != 0) | |
4618 | return (error); | |
4619 | ||
6cb8e530 | 4620 | } else if (error != 0) { |
9eb7b46e | 4621 | return (error); |
6cb8e530 | 4622 | } |
9eb7b46e | 4623 | |
d2734cce SD |
4624 | return (0); |
4625 | } | |
4626 | ||
4627 | /* | |
4628 | * Load an existing storage pool, using the config provided. This config | |
4629 | * describes which vdevs are part of the pool and is later validated against | |
4630 | * partial configs present in each vdev's label and an entire copy of the | |
4631 | * config stored in the MOS. | |
4632 | */ | |
4633 | static int | |
4634 | spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport) | |
4635 | { | |
4636 | int error = 0; | |
4637 | boolean_t missing_feat_write = B_FALSE; | |
4638 | boolean_t checkpoint_rewind = | |
4639 | (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4640 | boolean_t update_config_cache = B_FALSE; | |
4641 | ||
4642 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4643 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); | |
4644 | ||
4645 | spa_load_note(spa, "LOADING"); | |
4646 | ||
4647 | error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); | |
4648 | if (error != 0) | |
4649 | return (error); | |
4650 | ||
4651 | /* | |
4652 | * If we are rewinding to the checkpoint then we need to repeat | |
4653 | * everything we've done so far in this function but this time | |
4654 | * selecting the checkpointed uberblock and using that to open | |
4655 | * the MOS. | |
4656 | */ | |
4657 | if (checkpoint_rewind) { | |
4658 | /* | |
4659 | * If we are rewinding to the checkpoint update config cache | |
4660 | * anyway. | |
4661 | */ | |
4662 | update_config_cache = B_TRUE; | |
4663 | ||
4664 | /* | |
4665 | * Extract the checkpointed uberblock from the current MOS | |
4666 | * and use this as the pool's uberblock from now on. If the | |
4667 | * pool is imported as writeable we also write the checkpoint | |
4668 | * uberblock to the labels, making the rewind permanent. | |
4669 | */ | |
4670 | error = spa_ld_checkpoint_rewind(spa); | |
4671 | if (error != 0) | |
4672 | return (error); | |
4673 | ||
4674 | /* | |
e1cfd73f | 4675 | * Redo the loading process again with the |
d2734cce SD |
4676 | * checkpointed uberblock. |
4677 | */ | |
4678 | spa_ld_prepare_for_reload(spa); | |
4679 | spa_load_note(spa, "LOADING checkpointed uberblock"); | |
4680 | error = spa_ld_mos_with_trusted_config(spa, type, NULL); | |
4681 | if (error != 0) | |
4682 | return (error); | |
4683 | } | |
4684 | ||
4685 | /* | |
4686 | * Retrieve the checkpoint txg if the pool has a checkpoint. | |
4687 | */ | |
4688 | error = spa_ld_read_checkpoint_txg(spa); | |
4689 | if (error != 0) | |
4690 | return (error); | |
4691 | ||
9eb7b46e PZ |
4692 | /* |
4693 | * Retrieve the mapping of indirect vdevs. Those vdevs were removed | |
4694 | * from the pool and their contents were re-mapped to other vdevs. Note | |
4695 | * that everything that we read before this step must have been | |
4696 | * rewritten on concrete vdevs after the last device removal was | |
4697 | * initiated. Otherwise we could be reading from indirect vdevs before | |
4698 | * we have loaded their mappings. | |
4699 | */ | |
4700 | error = spa_ld_open_indirect_vdev_metadata(spa); | |
4701 | if (error != 0) | |
4702 | return (error); | |
4703 | ||
4704 | /* | |
4705 | * Retrieve the full list of active features from the MOS and check if | |
4706 | * they are all supported. | |
4707 | */ | |
4a0ee12a | 4708 | error = spa_ld_check_features(spa, &missing_feat_write); |
9eb7b46e PZ |
4709 | if (error != 0) |
4710 | return (error); | |
4711 | ||
4712 | /* | |
4713 | * Load several special directories from the MOS needed by the dsl_pool | |
4714 | * layer. | |
4715 | */ | |
4716 | error = spa_ld_load_special_directories(spa); | |
4717 | if (error != 0) | |
4718 | return (error); | |
4719 | ||
9eb7b46e PZ |
4720 | /* |
4721 | * Retrieve pool properties from the MOS. | |
4722 | */ | |
4723 | error = spa_ld_get_props(spa); | |
4724 | if (error != 0) | |
4725 | return (error); | |
4726 | ||
4727 | /* | |
4728 | * Retrieve the list of auxiliary devices - cache devices and spares - | |
4729 | * and open them. | |
4730 | */ | |
4731 | error = spa_ld_open_aux_vdevs(spa, type); | |
4732 | if (error != 0) | |
4733 | return (error); | |
4734 | ||
4735 | /* | |
4736 | * Load the metadata for all vdevs. Also check if unopenable devices | |
4737 | * should be autoreplaced. | |
4738 | */ | |
4a0ee12a | 4739 | error = spa_ld_load_vdev_metadata(spa); |
9eb7b46e PZ |
4740 | if (error != 0) |
4741 | return (error); | |
4742 | ||
4743 | error = spa_ld_load_dedup_tables(spa); | |
4744 | if (error != 0) | |
4745 | return (error); | |
4746 | ||
4747 | /* | |
4748 | * Verify the logs now to make sure we don't have any unexpected errors | |
4749 | * when we claim log blocks later. | |
4750 | */ | |
4751 | error = spa_ld_verify_logs(spa, type, ereport); | |
4752 | if (error != 0) | |
4753 | return (error); | |
4754 | ||
9ae529ec | 4755 | if (missing_feat_write) { |
6cb8e530 | 4756 | ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); |
9ae529ec CS |
4757 | |
4758 | /* | |
4759 | * At this point, we know that we can open the pool in | |
4760 | * read-only mode but not read-write mode. We now have enough | |
4761 | * information and can return to userland. | |
4762 | */ | |
9eb7b46e PZ |
4763 | return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, |
4764 | ENOTSUP)); | |
9ae529ec CS |
4765 | } |
4766 | ||
572e2857 | 4767 | /* |
9eb7b46e PZ |
4768 | * Traverse the last txgs to make sure the pool was left off in a safe |
4769 | * state. When performing an extreme rewind, we verify the whole pool, | |
4770 | * which can take a very long time. | |
572e2857 | 4771 | */ |
4a0ee12a | 4772 | error = spa_ld_verify_pool_data(spa); |
9eb7b46e PZ |
4773 | if (error != 0) |
4774 | return (error); | |
572e2857 | 4775 | |
9eb7b46e PZ |
4776 | /* |
4777 | * Calculate the deflated space for the pool. This must be done before | |
4778 | * we write anything to the pool because we'd need to update the space | |
4779 | * accounting using the deflated sizes. | |
4780 | */ | |
4781 | spa_update_dspace(spa); | |
4782 | ||
4783 | /* | |
4784 | * We have now retrieved all the information we needed to open the | |
4785 | * pool. If we are importing the pool in read-write mode, a few | |
4786 | * additional steps must be performed to finish the import. | |
4787 | */ | |
6cb8e530 | 4788 | if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || |
428870ff | 4789 | spa->spa_load_max_txg == UINT64_MAX)) { |
6cb8e530 PZ |
4790 | uint64_t config_cache_txg = spa->spa_config_txg; |
4791 | ||
4792 | ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f | 4793 | |
d2734cce SD |
4794 | /* |
4795 | * In case of a checkpoint rewind, log the original txg | |
4796 | * of the checkpointed uberblock. | |
4797 | */ | |
4798 | if (checkpoint_rewind) { | |
4799 | spa_history_log_internal(spa, "checkpoint rewind", | |
4800 | NULL, "rewound state to txg=%llu", | |
4801 | (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); | |
4802 | } | |
4803 | ||
34dc7c2f | 4804 | /* |
9eb7b46e | 4805 | * Traverse the ZIL and claim all blocks. |
34dc7c2f | 4806 | */ |
9eb7b46e | 4807 | spa_ld_claim_log_blocks(spa); |
428870ff | 4808 | |
9eb7b46e PZ |
4809 | /* |
4810 | * Kick-off the syncing thread. | |
4811 | */ | |
34dc7c2f BB |
4812 | spa->spa_sync_on = B_TRUE; |
4813 | txg_sync_start(spa->spa_dsl_pool); | |
379ca9cf | 4814 | mmp_thread_start(spa); |
34dc7c2f BB |
4815 | |
4816 | /* | |
428870ff BB |
4817 | * Wait for all claims to sync. We sync up to the highest |
4818 | * claimed log block birth time so that claimed log blocks | |
4819 | * don't appear to be from the future. spa_claim_max_txg | |
9eb7b46e PZ |
4820 | * will have been set for us by ZIL traversal operations |
4821 | * performed above. | |
34dc7c2f | 4822 | */ |
428870ff | 4823 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
4824 | |
4825 | /* | |
9eb7b46e PZ |
4826 | * Check if we need to request an update of the config. On the |
4827 | * next sync, we would update the config stored in vdev labels | |
4828 | * and the cachefile (by default /etc/zfs/zpool.cache). | |
34dc7c2f | 4829 | */ |
6cb8e530 | 4830 | spa_ld_check_for_config_update(spa, config_cache_txg, |
d2734cce | 4831 | update_config_cache); |
fb5f0bc8 BB |
4832 | |
4833 | /* | |
9a49d3f3 BB |
4834 | * Check if a rebuild was in progress and if so resume it. |
4835 | * Then check all DTLs to see if anything needs resilvering. | |
4836 | * The resilver will be deferred if a rebuild was started. | |
fb5f0bc8 | 4837 | */ |
9a49d3f3 BB |
4838 | if (vdev_rebuild_active(spa->spa_root_vdev)) { |
4839 | vdev_rebuild_restart(spa); | |
4840 | } else if (!dsl_scan_resilvering(spa->spa_dsl_pool) && | |
4841 | vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { | |
fb5f0bc8 | 4842 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
9a49d3f3 | 4843 | } |
428870ff | 4844 | |
6f1ffb06 MA |
4845 | /* |
4846 | * Log the fact that we booted up (so that we can detect if | |
4847 | * we rebooted in the middle of an operation). | |
4848 | */ | |
d5e024cb | 4849 | spa_history_log_version(spa, "open", NULL); |
6f1ffb06 | 4850 | |
9b2266e3 SD |
4851 | spa_restart_removal(spa); |
4852 | spa_spawn_aux_threads(spa); | |
4853 | ||
428870ff BB |
4854 | /* |
4855 | * Delete any inconsistent datasets. | |
9b2266e3 SD |
4856 | * |
4857 | * Note: | |
4858 | * Since we may be issuing deletes for clones here, | |
4859 | * we make sure to do so after we've spawned all the | |
4860 | * auxiliary threads above (from which the livelist | |
4861 | * deletion zthr is part of). | |
428870ff BB |
4862 | */ |
4863 | (void) dmu_objset_find(spa_name(spa), | |
4864 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
4865 | ||
4866 | /* | |
4867 | * Clean up any stale temporary dataset userrefs. | |
4868 | */ | |
4869 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
a1d477c2 | 4870 | |
619f0976 GW |
4871 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
4872 | vdev_initialize_restart(spa->spa_root_vdev); | |
1b939560 BB |
4873 | vdev_trim_restart(spa->spa_root_vdev); |
4874 | vdev_autotrim_restart(spa); | |
619f0976 | 4875 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
4876 | } |
4877 | ||
ca95f70d | 4878 | spa_import_progress_remove(spa_guid(spa)); |
77f6826b GA |
4879 | spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD); |
4880 | ||
4a0ee12a PZ |
4881 | spa_load_note(spa, "LOADED"); |
4882 | ||
428870ff BB |
4883 | return (0); |
4884 | } | |
34dc7c2f | 4885 | |
428870ff | 4886 | static int |
6cb8e530 | 4887 | spa_load_retry(spa_t *spa, spa_load_state_t state) |
428870ff | 4888 | { |
da92d5cb | 4889 | spa_mode_t mode = spa->spa_mode; |
572e2857 | 4890 | |
428870ff BB |
4891 | spa_unload(spa); |
4892 | spa_deactivate(spa); | |
4893 | ||
dea377c0 | 4894 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; |
428870ff | 4895 | |
572e2857 | 4896 | spa_activate(spa, mode); |
428870ff BB |
4897 | spa_async_suspend(spa); |
4898 | ||
4a0ee12a PZ |
4899 | spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", |
4900 | (u_longlong_t)spa->spa_load_max_txg); | |
4901 | ||
6cb8e530 | 4902 | return (spa_load(spa, state, SPA_IMPORT_EXISTING)); |
428870ff BB |
4903 | } |
4904 | ||
9ae529ec CS |
4905 | /* |
4906 | * If spa_load() fails this function will try loading prior txg's. If | |
4907 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
4908 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
4909 | * function will not rewind the pool and will return the same error as | |
4910 | * spa_load(). | |
4911 | */ | |
428870ff | 4912 | static int |
6cb8e530 PZ |
4913 | spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, |
4914 | int rewind_flags) | |
428870ff | 4915 | { |
9ae529ec | 4916 | nvlist_t *loadinfo = NULL; |
428870ff BB |
4917 | nvlist_t *config = NULL; |
4918 | int load_error, rewind_error; | |
4919 | uint64_t safe_rewind_txg; | |
4920 | uint64_t min_txg; | |
4921 | ||
4922 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
4923 | spa->spa_load_max_txg = spa->spa_load_txg; | |
4924 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
4925 | } else { | |
4926 | spa->spa_load_max_txg = max_request; | |
dea377c0 MA |
4927 | if (max_request != UINT64_MAX) |
4928 | spa->spa_extreme_rewind = B_TRUE; | |
428870ff BB |
4929 | } |
4930 | ||
6cb8e530 | 4931 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); |
428870ff BB |
4932 | if (load_error == 0) |
4933 | return (0); | |
d2734cce SD |
4934 | if (load_error == ZFS_ERR_NO_CHECKPOINT) { |
4935 | /* | |
4936 | * When attempting checkpoint-rewind on a pool with no | |
4937 | * checkpoint, we should not attempt to load uberblocks | |
4938 | * from previous txgs when spa_load fails. | |
4939 | */ | |
4940 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
ca95f70d | 4941 | spa_import_progress_remove(spa_guid(spa)); |
d2734cce SD |
4942 | return (load_error); |
4943 | } | |
428870ff BB |
4944 | |
4945 | if (spa->spa_root_vdev != NULL) | |
4946 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
4947 | ||
4948 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
4949 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
4950 | ||
4951 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
4952 | nvlist_free(config); | |
ca95f70d | 4953 | spa_import_progress_remove(spa_guid(spa)); |
428870ff BB |
4954 | return (load_error); |
4955 | } | |
4956 | ||
9ae529ec CS |
4957 | if (state == SPA_LOAD_RECOVER) { |
4958 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 4959 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
4960 | } else { |
4961 | /* | |
4962 | * If we aren't rolling back save the load info from our first | |
4963 | * import attempt so that we can restore it after attempting | |
4964 | * to rewind. | |
4965 | */ | |
4966 | loadinfo = spa->spa_load_info; | |
4967 | spa->spa_load_info = fnvlist_alloc(); | |
4968 | } | |
428870ff BB |
4969 | |
4970 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
4971 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
4972 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
4973 | TXG_INITIAL : safe_rewind_txg; | |
4974 | ||
4975 | /* | |
4976 | * Continue as long as we're finding errors, we're still within | |
4977 | * the acceptable rewind range, and we're still finding uberblocks | |
4978 | */ | |
4979 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
4980 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
4981 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
4982 | spa->spa_extreme_rewind = B_TRUE; | |
6cb8e530 | 4983 | rewind_error = spa_load_retry(spa, state); |
428870ff BB |
4984 | } |
4985 | ||
428870ff BB |
4986 | spa->spa_extreme_rewind = B_FALSE; |
4987 | spa->spa_load_max_txg = UINT64_MAX; | |
4988 | ||
4989 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
4990 | spa_config_set(spa, config); | |
ee6370a7 | 4991 | else |
4992 | nvlist_free(config); | |
428870ff | 4993 | |
9ae529ec CS |
4994 | if (state == SPA_LOAD_RECOVER) { |
4995 | ASSERT3P(loadinfo, ==, NULL); | |
ca95f70d | 4996 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
4997 | return (rewind_error); |
4998 | } else { | |
4999 | /* Store the rewind info as part of the initial load info */ | |
5000 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
5001 | spa->spa_load_info); | |
5002 | ||
5003 | /* Restore the initial load info */ | |
5004 | fnvlist_free(spa->spa_load_info); | |
5005 | spa->spa_load_info = loadinfo; | |
5006 | ||
ca95f70d | 5007 | spa_import_progress_remove(spa_guid(spa)); |
9ae529ec CS |
5008 | return (load_error); |
5009 | } | |
34dc7c2f BB |
5010 | } |
5011 | ||
5012 | /* | |
5013 | * Pool Open/Import | |
5014 | * | |
5015 | * The import case is identical to an open except that the configuration is sent | |
5016 | * down from userland, instead of grabbed from the configuration cache. For the | |
5017 | * case of an open, the pool configuration will exist in the | |
5018 | * POOL_STATE_UNINITIALIZED state. | |
5019 | * | |
5020 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
5021 | * the same time open the pool, without having to keep around the spa_t in some | |
5022 | * ambiguous state. | |
5023 | */ | |
5024 | static int | |
428870ff BB |
5025 | spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, |
5026 | nvlist_t **config) | |
34dc7c2f BB |
5027 | { |
5028 | spa_t *spa; | |
572e2857 | 5029 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 5030 | int error; |
34dc7c2f | 5031 | int locked = B_FALSE; |
526af785 | 5032 | int firstopen = B_FALSE; |
34dc7c2f BB |
5033 | |
5034 | *spapp = NULL; | |
5035 | ||
5036 | /* | |
5037 | * As disgusting as this is, we need to support recursive calls to this | |
5038 | * function because dsl_dir_open() is called during spa_load(), and ends | |
5039 | * up calling spa_open() again. The real fix is to figure out how to | |
5040 | * avoid dsl_dir_open() calling this in the first place. | |
5041 | */ | |
c25b8f99 | 5042 | if (MUTEX_NOT_HELD(&spa_namespace_lock)) { |
34dc7c2f BB |
5043 | mutex_enter(&spa_namespace_lock); |
5044 | locked = B_TRUE; | |
5045 | } | |
5046 | ||
5047 | if ((spa = spa_lookup(pool)) == NULL) { | |
5048 | if (locked) | |
5049 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5050 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 5051 | } |
428870ff | 5052 | |
34dc7c2f | 5053 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
8a393be3 | 5054 | zpool_load_policy_t policy; |
428870ff | 5055 | |
526af785 PJD |
5056 | firstopen = B_TRUE; |
5057 | ||
8a393be3 | 5058 | zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, |
428870ff | 5059 | &policy); |
8a393be3 | 5060 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) |
428870ff | 5061 | state = SPA_LOAD_RECOVER; |
34dc7c2f | 5062 | |
fb5f0bc8 | 5063 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5064 | |
428870ff BB |
5065 | if (state != SPA_LOAD_RECOVER) |
5066 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6cb8e530 | 5067 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; |
428870ff | 5068 | |
4a0ee12a | 5069 | zfs_dbgmsg("spa_open_common: opening %s", pool); |
8a393be3 PZ |
5070 | error = spa_load_best(spa, state, policy.zlp_txg, |
5071 | policy.zlp_rewind); | |
34dc7c2f BB |
5072 | |
5073 | if (error == EBADF) { | |
5074 | /* | |
5075 | * If vdev_validate() returns failure (indicated by | |
5076 | * EBADF), it indicates that one of the vdevs indicates | |
5077 | * that the pool has been exported or destroyed. If | |
5078 | * this is the case, the config cache is out of sync and | |
5079 | * we should remove the pool from the namespace. | |
5080 | */ | |
34dc7c2f BB |
5081 | spa_unload(spa); |
5082 | spa_deactivate(spa); | |
a1d477c2 | 5083 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 5084 | spa_remove(spa); |
34dc7c2f BB |
5085 | if (locked) |
5086 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5087 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
5088 | } |
5089 | ||
5090 | if (error) { | |
5091 | /* | |
5092 | * We can't open the pool, but we still have useful | |
5093 | * information: the state of each vdev after the | |
5094 | * attempted vdev_open(). Return this to the user. | |
5095 | */ | |
572e2857 | 5096 | if (config != NULL && spa->spa_config) { |
428870ff | 5097 | VERIFY(nvlist_dup(spa->spa_config, config, |
79c76d5b | 5098 | KM_SLEEP) == 0); |
572e2857 BB |
5099 | VERIFY(nvlist_add_nvlist(*config, |
5100 | ZPOOL_CONFIG_LOAD_INFO, | |
5101 | spa->spa_load_info) == 0); | |
5102 | } | |
34dc7c2f BB |
5103 | spa_unload(spa); |
5104 | spa_deactivate(spa); | |
428870ff | 5105 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
5106 | if (locked) |
5107 | mutex_exit(&spa_namespace_lock); | |
5108 | *spapp = NULL; | |
5109 | return (error); | |
34dc7c2f | 5110 | } |
34dc7c2f BB |
5111 | } |
5112 | ||
5113 | spa_open_ref(spa, tag); | |
5114 | ||
b128c09f | 5115 | if (config != NULL) |
34dc7c2f | 5116 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 5117 | |
572e2857 BB |
5118 | /* |
5119 | * If we've recovered the pool, pass back any information we | |
5120 | * gathered while doing the load. | |
5121 | */ | |
5122 | if (state == SPA_LOAD_RECOVER) { | |
5123 | VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, | |
5124 | spa->spa_load_info) == 0); | |
5125 | } | |
5126 | ||
428870ff BB |
5127 | if (locked) { |
5128 | spa->spa_last_open_failed = 0; | |
5129 | spa->spa_last_ubsync_txg = 0; | |
5130 | spa->spa_load_txg = 0; | |
5131 | mutex_exit(&spa_namespace_lock); | |
5132 | } | |
5133 | ||
526af785 | 5134 | if (firstopen) |
ec213971 | 5135 | zvol_create_minors_recursive(spa_name(spa)); |
526af785 | 5136 | |
428870ff BB |
5137 | *spapp = spa; |
5138 | ||
34dc7c2f BB |
5139 | return (0); |
5140 | } | |
5141 | ||
428870ff BB |
5142 | int |
5143 | spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, | |
5144 | nvlist_t **config) | |
5145 | { | |
5146 | return (spa_open_common(name, spapp, tag, policy, config)); | |
5147 | } | |
5148 | ||
34dc7c2f BB |
5149 | int |
5150 | spa_open(const char *name, spa_t **spapp, void *tag) | |
5151 | { | |
428870ff | 5152 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
5153 | } |
5154 | ||
5155 | /* | |
5156 | * Lookup the given spa_t, incrementing the inject count in the process, | |
5157 | * preventing it from being exported or destroyed. | |
5158 | */ | |
5159 | spa_t * | |
5160 | spa_inject_addref(char *name) | |
5161 | { | |
5162 | spa_t *spa; | |
5163 | ||
5164 | mutex_enter(&spa_namespace_lock); | |
5165 | if ((spa = spa_lookup(name)) == NULL) { | |
5166 | mutex_exit(&spa_namespace_lock); | |
5167 | return (NULL); | |
5168 | } | |
5169 | spa->spa_inject_ref++; | |
5170 | mutex_exit(&spa_namespace_lock); | |
5171 | ||
5172 | return (spa); | |
5173 | } | |
5174 | ||
5175 | void | |
5176 | spa_inject_delref(spa_t *spa) | |
5177 | { | |
5178 | mutex_enter(&spa_namespace_lock); | |
5179 | spa->spa_inject_ref--; | |
5180 | mutex_exit(&spa_namespace_lock); | |
5181 | } | |
5182 | ||
5183 | /* | |
5184 | * Add spares device information to the nvlist. | |
5185 | */ | |
5186 | static void | |
5187 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
5188 | { | |
5189 | nvlist_t **spares; | |
5190 | uint_t i, nspares; | |
5191 | nvlist_t *nvroot; | |
5192 | uint64_t guid; | |
5193 | vdev_stat_t *vs; | |
5194 | uint_t vsc; | |
5195 | uint64_t pool; | |
5196 | ||
9babb374 BB |
5197 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5198 | ||
34dc7c2f BB |
5199 | if (spa->spa_spares.sav_count == 0) |
5200 | return; | |
5201 | ||
5202 | VERIFY(nvlist_lookup_nvlist(config, | |
5203 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
5204 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
5205 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
5206 | if (nspares != 0) { | |
5207 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
5208 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
5209 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
5210 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
5211 | ||
5212 | /* | |
5213 | * Go through and find any spares which have since been | |
5214 | * repurposed as an active spare. If this is the case, update | |
5215 | * their status appropriately. | |
5216 | */ | |
5217 | for (i = 0; i < nspares; i++) { | |
5218 | VERIFY(nvlist_lookup_uint64(spares[i], | |
5219 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
b128c09f BB |
5220 | if (spa_spare_exists(guid, &pool, NULL) && |
5221 | pool != 0ULL) { | |
34dc7c2f | 5222 | VERIFY(nvlist_lookup_uint64_array( |
428870ff | 5223 | spares[i], ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
5224 | (uint64_t **)&vs, &vsc) == 0); |
5225 | vs->vs_state = VDEV_STATE_CANT_OPEN; | |
5226 | vs->vs_aux = VDEV_AUX_SPARED; | |
5227 | } | |
5228 | } | |
5229 | } | |
5230 | } | |
5231 | ||
5232 | /* | |
5233 | * Add l2cache device information to the nvlist, including vdev stats. | |
5234 | */ | |
5235 | static void | |
5236 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
5237 | { | |
5238 | nvlist_t **l2cache; | |
5239 | uint_t i, j, nl2cache; | |
5240 | nvlist_t *nvroot; | |
5241 | uint64_t guid; | |
5242 | vdev_t *vd; | |
5243 | vdev_stat_t *vs; | |
5244 | uint_t vsc; | |
5245 | ||
9babb374 BB |
5246 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
5247 | ||
34dc7c2f BB |
5248 | if (spa->spa_l2cache.sav_count == 0) |
5249 | return; | |
5250 | ||
34dc7c2f BB |
5251 | VERIFY(nvlist_lookup_nvlist(config, |
5252 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
5253 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
5254 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
5255 | if (nl2cache != 0) { | |
5256 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
5257 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
5258 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
5259 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
5260 | ||
5261 | /* | |
5262 | * Update level 2 cache device stats. | |
5263 | */ | |
5264 | ||
5265 | for (i = 0; i < nl2cache; i++) { | |
5266 | VERIFY(nvlist_lookup_uint64(l2cache[i], | |
5267 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
5268 | ||
5269 | vd = NULL; | |
5270 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
5271 | if (guid == | |
5272 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
5273 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
5274 | break; | |
5275 | } | |
5276 | } | |
5277 | ASSERT(vd != NULL); | |
5278 | ||
5279 | VERIFY(nvlist_lookup_uint64_array(l2cache[i], | |
428870ff BB |
5280 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) |
5281 | == 0); | |
34dc7c2f | 5282 | vdev_get_stats(vd, vs); |
193a37cb TH |
5283 | vdev_config_generate_stats(vd, l2cache[i]); |
5284 | ||
34dc7c2f BB |
5285 | } |
5286 | } | |
34dc7c2f BB |
5287 | } |
5288 | ||
9ae529ec | 5289 | static void |
417104bd | 5290 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) |
9ae529ec | 5291 | { |
9ae529ec CS |
5292 | zap_cursor_t zc; |
5293 | zap_attribute_t za; | |
5294 | ||
9ae529ec CS |
5295 | if (spa->spa_feat_for_read_obj != 0) { |
5296 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5297 | spa->spa_feat_for_read_obj); | |
5298 | zap_cursor_retrieve(&zc, &za) == 0; | |
5299 | zap_cursor_advance(&zc)) { | |
5300 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5301 | za.za_num_integers == 1); | |
417104bd | 5302 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5303 | za.za_first_integer)); |
5304 | } | |
5305 | zap_cursor_fini(&zc); | |
5306 | } | |
5307 | ||
5308 | if (spa->spa_feat_for_write_obj != 0) { | |
5309 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
5310 | spa->spa_feat_for_write_obj); | |
5311 | zap_cursor_retrieve(&zc, &za) == 0; | |
5312 | zap_cursor_advance(&zc)) { | |
5313 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
5314 | za.za_num_integers == 1); | |
417104bd | 5315 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
5316 | za.za_first_integer)); |
5317 | } | |
5318 | zap_cursor_fini(&zc); | |
5319 | } | |
417104bd NB |
5320 | } |
5321 | ||
5322 | static void | |
5323 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
5324 | { | |
5325 | int i; | |
5326 | ||
5327 | for (i = 0; i < SPA_FEATURES; i++) { | |
5328 | zfeature_info_t feature = spa_feature_table[i]; | |
5329 | uint64_t refcount; | |
5330 | ||
5331 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
5332 | continue; | |
5333 | ||
5334 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
5335 | } | |
5336 | } | |
5337 | ||
5338 | /* | |
5339 | * Store a list of pool features and their reference counts in the | |
5340 | * config. | |
5341 | * | |
5342 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
5343 | * the pool features and reference counts from disk, then save the list | |
5344 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
5345 | * and refresh its values from the cached reference counts. This | |
5346 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
5347 | * clear' can resume the pool. | |
5348 | */ | |
5349 | static void | |
5350 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
5351 | { | |
4eb30c68 | 5352 | nvlist_t *features; |
417104bd NB |
5353 | |
5354 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
5355 | ||
4eb30c68 NB |
5356 | mutex_enter(&spa->spa_feat_stats_lock); |
5357 | features = spa->spa_feat_stats; | |
5358 | ||
417104bd NB |
5359 | if (features != NULL) { |
5360 | spa_feature_stats_from_cache(spa, features); | |
5361 | } else { | |
5362 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
5363 | spa->spa_feat_stats = features; | |
5364 | spa_feature_stats_from_disk(spa, features); | |
5365 | } | |
9ae529ec | 5366 | |
417104bd NB |
5367 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, |
5368 | features)); | |
4eb30c68 NB |
5369 | |
5370 | mutex_exit(&spa->spa_feat_stats_lock); | |
9ae529ec CS |
5371 | } |
5372 | ||
34dc7c2f | 5373 | int |
9ae529ec CS |
5374 | spa_get_stats(const char *name, nvlist_t **config, |
5375 | char *altroot, size_t buflen) | |
34dc7c2f BB |
5376 | { |
5377 | int error; | |
5378 | spa_t *spa; | |
5379 | ||
5380 | *config = NULL; | |
428870ff | 5381 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 5382 | |
9babb374 BB |
5383 | if (spa != NULL) { |
5384 | /* | |
5385 | * This still leaves a window of inconsistency where the spares | |
5386 | * or l2cache devices could change and the config would be | |
5387 | * self-inconsistent. | |
5388 | */ | |
5389 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 5390 | |
9babb374 | 5391 | if (*config != NULL) { |
572e2857 BB |
5392 | uint64_t loadtimes[2]; |
5393 | ||
5394 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
5395 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
5396 | VERIFY(nvlist_add_uint64_array(*config, | |
5397 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); | |
5398 | ||
b128c09f | 5399 | VERIFY(nvlist_add_uint64(*config, |
9babb374 BB |
5400 | ZPOOL_CONFIG_ERRCOUNT, |
5401 | spa_get_errlog_size(spa)) == 0); | |
5402 | ||
cec3a0a1 | 5403 | if (spa_suspended(spa)) { |
9babb374 BB |
5404 | VERIFY(nvlist_add_uint64(*config, |
5405 | ZPOOL_CONFIG_SUSPENDED, | |
5406 | spa->spa_failmode) == 0); | |
cec3a0a1 OF |
5407 | VERIFY(nvlist_add_uint64(*config, |
5408 | ZPOOL_CONFIG_SUSPENDED_REASON, | |
5409 | spa->spa_suspended) == 0); | |
5410 | } | |
b128c09f | 5411 | |
9babb374 BB |
5412 | spa_add_spares(spa, *config); |
5413 | spa_add_l2cache(spa, *config); | |
9ae529ec | 5414 | spa_add_feature_stats(spa, *config); |
9babb374 | 5415 | } |
34dc7c2f BB |
5416 | } |
5417 | ||
5418 | /* | |
5419 | * We want to get the alternate root even for faulted pools, so we cheat | |
5420 | * and call spa_lookup() directly. | |
5421 | */ | |
5422 | if (altroot) { | |
5423 | if (spa == NULL) { | |
5424 | mutex_enter(&spa_namespace_lock); | |
5425 | spa = spa_lookup(name); | |
5426 | if (spa) | |
5427 | spa_altroot(spa, altroot, buflen); | |
5428 | else | |
5429 | altroot[0] = '\0'; | |
5430 | spa = NULL; | |
5431 | mutex_exit(&spa_namespace_lock); | |
5432 | } else { | |
5433 | spa_altroot(spa, altroot, buflen); | |
5434 | } | |
5435 | } | |
5436 | ||
9babb374 BB |
5437 | if (spa != NULL) { |
5438 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 5439 | spa_close(spa, FTAG); |
9babb374 | 5440 | } |
34dc7c2f BB |
5441 | |
5442 | return (error); | |
5443 | } | |
5444 | ||
5445 | /* | |
5446 | * Validate that the auxiliary device array is well formed. We must have an | |
5447 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
5448 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
5449 | * specified, as long as they are well-formed. | |
5450 | */ | |
5451 | static int | |
5452 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
5453 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
5454 | vdev_labeltype_t label) | |
5455 | { | |
5456 | nvlist_t **dev; | |
5457 | uint_t i, ndev; | |
5458 | vdev_t *vd; | |
5459 | int error; | |
5460 | ||
b128c09f BB |
5461 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5462 | ||
34dc7c2f BB |
5463 | /* |
5464 | * It's acceptable to have no devs specified. | |
5465 | */ | |
5466 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
5467 | return (0); | |
5468 | ||
5469 | if (ndev == 0) | |
2e528b49 | 5470 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
5471 | |
5472 | /* | |
5473 | * Make sure the pool is formatted with a version that supports this | |
5474 | * device type. | |
5475 | */ | |
5476 | if (spa_version(spa) < version) | |
2e528b49 | 5477 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
5478 | |
5479 | /* | |
5480 | * Set the pending device list so we correctly handle device in-use | |
5481 | * checking. | |
5482 | */ | |
5483 | sav->sav_pending = dev; | |
5484 | sav->sav_npending = ndev; | |
5485 | ||
5486 | for (i = 0; i < ndev; i++) { | |
5487 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
5488 | mode)) != 0) | |
5489 | goto out; | |
5490 | ||
5491 | if (!vd->vdev_ops->vdev_op_leaf) { | |
5492 | vdev_free(vd); | |
2e528b49 | 5493 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5494 | goto out; |
5495 | } | |
5496 | ||
34dc7c2f BB |
5497 | vd->vdev_top = vd; |
5498 | ||
5499 | if ((error = vdev_open(vd)) == 0 && | |
5500 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
5501 | VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, | |
5502 | vd->vdev_guid) == 0); | |
5503 | } | |
5504 | ||
5505 | vdev_free(vd); | |
5506 | ||
5507 | if (error && | |
5508 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
5509 | goto out; | |
5510 | else | |
5511 | error = 0; | |
5512 | } | |
5513 | ||
5514 | out: | |
5515 | sav->sav_pending = NULL; | |
5516 | sav->sav_npending = 0; | |
5517 | return (error); | |
5518 | } | |
5519 | ||
5520 | static int | |
5521 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
5522 | { | |
5523 | int error; | |
5524 | ||
b128c09f BB |
5525 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
5526 | ||
34dc7c2f BB |
5527 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
5528 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
5529 | VDEV_LABEL_SPARE)) != 0) { | |
5530 | return (error); | |
5531 | } | |
5532 | ||
5533 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
5534 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
5535 | VDEV_LABEL_L2CACHE)); | |
5536 | } | |
5537 | ||
5538 | static void | |
5539 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
5540 | const char *config) | |
5541 | { | |
5542 | int i; | |
5543 | ||
5544 | if (sav->sav_config != NULL) { | |
5545 | nvlist_t **olddevs; | |
5546 | uint_t oldndevs; | |
5547 | nvlist_t **newdevs; | |
5548 | ||
5549 | /* | |
4e33ba4c | 5550 | * Generate new dev list by concatenating with the |
34dc7c2f BB |
5551 | * current dev list. |
5552 | */ | |
5553 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, | |
5554 | &olddevs, &oldndevs) == 0); | |
5555 | ||
5556 | newdevs = kmem_alloc(sizeof (void *) * | |
79c76d5b | 5557 | (ndevs + oldndevs), KM_SLEEP); |
34dc7c2f BB |
5558 | for (i = 0; i < oldndevs; i++) |
5559 | VERIFY(nvlist_dup(olddevs[i], &newdevs[i], | |
79c76d5b | 5560 | KM_SLEEP) == 0); |
34dc7c2f BB |
5561 | for (i = 0; i < ndevs; i++) |
5562 | VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], | |
79c76d5b | 5563 | KM_SLEEP) == 0); |
34dc7c2f BB |
5564 | |
5565 | VERIFY(nvlist_remove(sav->sav_config, config, | |
5566 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
5567 | ||
5568 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
5569 | config, newdevs, ndevs + oldndevs) == 0); | |
5570 | for (i = 0; i < oldndevs + ndevs; i++) | |
5571 | nvlist_free(newdevs[i]); | |
5572 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
5573 | } else { | |
5574 | /* | |
5575 | * Generate a new dev list. | |
5576 | */ | |
5577 | VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5578 | KM_SLEEP) == 0); |
34dc7c2f BB |
5579 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, |
5580 | devs, ndevs) == 0); | |
5581 | } | |
5582 | } | |
5583 | ||
5584 | /* | |
5585 | * Stop and drop level 2 ARC devices | |
5586 | */ | |
5587 | void | |
5588 | spa_l2cache_drop(spa_t *spa) | |
5589 | { | |
5590 | vdev_t *vd; | |
5591 | int i; | |
5592 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
5593 | ||
5594 | for (i = 0; i < sav->sav_count; i++) { | |
5595 | uint64_t pool; | |
5596 | ||
5597 | vd = sav->sav_vdevs[i]; | |
5598 | ASSERT(vd != NULL); | |
5599 | ||
fb5f0bc8 BB |
5600 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
5601 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 5602 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
5603 | } |
5604 | } | |
5605 | ||
b5256303 TC |
5606 | /* |
5607 | * Verify encryption parameters for spa creation. If we are encrypting, we must | |
5608 | * have the encryption feature flag enabled. | |
5609 | */ | |
5610 | static int | |
5611 | spa_create_check_encryption_params(dsl_crypto_params_t *dcp, | |
5612 | boolean_t has_encryption) | |
5613 | { | |
5614 | if (dcp->cp_crypt != ZIO_CRYPT_OFF && | |
5615 | dcp->cp_crypt != ZIO_CRYPT_INHERIT && | |
5616 | !has_encryption) | |
5617 | return (SET_ERROR(ENOTSUP)); | |
5618 | ||
1fff937a | 5619 | return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); |
b5256303 TC |
5620 | } |
5621 | ||
34dc7c2f BB |
5622 | /* |
5623 | * Pool Creation | |
5624 | */ | |
5625 | int | |
5626 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b5256303 | 5627 | nvlist_t *zplprops, dsl_crypto_params_t *dcp) |
34dc7c2f BB |
5628 | { |
5629 | spa_t *spa; | |
5630 | char *altroot = NULL; | |
5631 | vdev_t *rvd; | |
5632 | dsl_pool_t *dp; | |
5633 | dmu_tx_t *tx; | |
9babb374 | 5634 | int error = 0; |
34dc7c2f BB |
5635 | uint64_t txg = TXG_INITIAL; |
5636 | nvlist_t **spares, **l2cache; | |
5637 | uint_t nspares, nl2cache; | |
52ce99dd | 5638 | uint64_t version, obj; |
9ae529ec | 5639 | boolean_t has_features; |
b5256303 | 5640 | boolean_t has_encryption; |
715c996d | 5641 | boolean_t has_allocclass; |
b5256303 TC |
5642 | spa_feature_t feat; |
5643 | char *feat_name; | |
83e9986f RY |
5644 | char *poolname; |
5645 | nvlist_t *nvl; | |
5646 | ||
cc99f275 DB |
5647 | if (props == NULL || |
5648 | nvlist_lookup_string(props, "tname", &poolname) != 0) | |
83e9986f | 5649 | poolname = (char *)pool; |
34dc7c2f BB |
5650 | |
5651 | /* | |
5652 | * If this pool already exists, return failure. | |
5653 | */ | |
5654 | mutex_enter(&spa_namespace_lock); | |
83e9986f | 5655 | if (spa_lookup(poolname) != NULL) { |
34dc7c2f | 5656 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5657 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5658 | } |
5659 | ||
5660 | /* | |
5661 | * Allocate a new spa_t structure. | |
5662 | */ | |
83e9986f RY |
5663 | nvl = fnvlist_alloc(); |
5664 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
34dc7c2f BB |
5665 | (void) nvlist_lookup_string(props, |
5666 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
83e9986f RY |
5667 | spa = spa_add(poolname, nvl, altroot); |
5668 | fnvlist_free(nvl); | |
fb5f0bc8 | 5669 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5670 | |
34dc7c2f | 5671 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
5672 | spa_deactivate(spa); |
5673 | spa_remove(spa); | |
b128c09f | 5674 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
5675 | return (error); |
5676 | } | |
5677 | ||
83e9986f RY |
5678 | /* |
5679 | * Temporary pool names should never be written to disk. | |
5680 | */ | |
5681 | if (poolname != pool) | |
5682 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
5683 | ||
9ae529ec | 5684 | has_features = B_FALSE; |
b5256303 | 5685 | has_encryption = B_FALSE; |
715c996d | 5686 | has_allocclass = B_FALSE; |
1c27024e | 5687 | for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); |
9ae529ec | 5688 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { |
b5256303 | 5689 | if (zpool_prop_feature(nvpair_name(elem))) { |
9ae529ec | 5690 | has_features = B_TRUE; |
b5256303 TC |
5691 | |
5692 | feat_name = strchr(nvpair_name(elem), '@') + 1; | |
5693 | VERIFY0(zfeature_lookup_name(feat_name, &feat)); | |
5694 | if (feat == SPA_FEATURE_ENCRYPTION) | |
5695 | has_encryption = B_TRUE; | |
715c996d | 5696 | if (feat == SPA_FEATURE_ALLOCATION_CLASSES) |
5697 | has_allocclass = B_TRUE; | |
b5256303 TC |
5698 | } |
5699 | } | |
5700 | ||
5701 | /* verify encryption params, if they were provided */ | |
5702 | if (dcp != NULL) { | |
5703 | error = spa_create_check_encryption_params(dcp, has_encryption); | |
5704 | if (error != 0) { | |
5705 | spa_deactivate(spa); | |
5706 | spa_remove(spa); | |
5707 | mutex_exit(&spa_namespace_lock); | |
5708 | return (error); | |
5709 | } | |
9ae529ec | 5710 | } |
c24fa4b1 | 5711 | if (!has_allocclass && zfs_special_devs(nvroot, NULL)) { |
715c996d | 5712 | spa_deactivate(spa); |
5713 | spa_remove(spa); | |
5714 | mutex_exit(&spa_namespace_lock); | |
5715 | return (ENOTSUP); | |
5716 | } | |
9ae529ec CS |
5717 | |
5718 | if (has_features || nvlist_lookup_uint64(props, | |
5719 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 5720 | version = SPA_VERSION; |
9ae529ec CS |
5721 | } |
5722 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
5723 | |
5724 | spa->spa_first_txg = txg; | |
5725 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
5726 | spa->spa_uberblock.ub_version = version; |
5727 | spa->spa_ubsync = spa->spa_uberblock; | |
3dfb57a3 | 5728 | spa->spa_load_state = SPA_LOAD_CREATE; |
a1d477c2 MA |
5729 | spa->spa_removing_phys.sr_state = DSS_NONE; |
5730 | spa->spa_removing_phys.sr_removing_vdev = -1; | |
5731 | spa->spa_removing_phys.sr_prev_indirect_vdev = -1; | |
944a3724 | 5732 | spa->spa_indirect_vdevs_loaded = B_TRUE; |
34dc7c2f | 5733 | |
9babb374 BB |
5734 | /* |
5735 | * Create "The Godfather" zio to hold all async IOs | |
5736 | */ | |
e022864d MA |
5737 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
5738 | KM_SLEEP); | |
1c27024e | 5739 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
5740 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
5741 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
5742 | ZIO_FLAG_GODFATHER); | |
5743 | } | |
9babb374 | 5744 | |
34dc7c2f BB |
5745 | /* |
5746 | * Create the root vdev. | |
5747 | */ | |
b128c09f | 5748 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5749 | |
5750 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
5751 | ||
5752 | ASSERT(error != 0 || rvd != NULL); | |
5753 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
5754 | ||
5755 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
2e528b49 | 5756 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5757 | |
5758 | if (error == 0 && | |
5759 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
5760 | (error = spa_validate_aux(spa, nvroot, txg, | |
5761 | VDEV_ALLOC_ADD)) == 0) { | |
cc99f275 DB |
5762 | /* |
5763 | * instantiate the metaslab groups (this will dirty the vdevs) | |
5764 | * we can no longer error exit past this point | |
5765 | */ | |
5766 | for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { | |
5767 | vdev_t *vd = rvd->vdev_child[c]; | |
5768 | ||
5769 | vdev_metaslab_set_size(vd); | |
5770 | vdev_expand(vd, txg); | |
9babb374 | 5771 | } |
34dc7c2f BB |
5772 | } |
5773 | ||
b128c09f | 5774 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5775 | |
5776 | if (error != 0) { | |
5777 | spa_unload(spa); | |
5778 | spa_deactivate(spa); | |
5779 | spa_remove(spa); | |
5780 | mutex_exit(&spa_namespace_lock); | |
5781 | return (error); | |
5782 | } | |
5783 | ||
5784 | /* | |
5785 | * Get the list of spares, if specified. | |
5786 | */ | |
5787 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5788 | &spares, &nspares) == 0) { | |
5789 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5790 | KM_SLEEP) == 0); |
34dc7c2f BB |
5791 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
5792 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 5793 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5794 | spa_load_spares(spa); |
b128c09f | 5795 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5796 | spa->spa_spares.sav_sync = B_TRUE; |
5797 | } | |
5798 | ||
5799 | /* | |
5800 | * Get the list of level 2 cache devices, if specified. | |
5801 | */ | |
5802 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5803 | &l2cache, &nl2cache) == 0) { | |
5804 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 5805 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5806 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
5807 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 5808 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5809 | spa_load_l2cache(spa); |
b128c09f | 5810 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5811 | spa->spa_l2cache.sav_sync = B_TRUE; |
5812 | } | |
5813 | ||
9ae529ec | 5814 | spa->spa_is_initializing = B_TRUE; |
b5256303 | 5815 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); |
9ae529ec | 5816 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 5817 | |
428870ff BB |
5818 | /* |
5819 | * Create DDTs (dedup tables). | |
5820 | */ | |
5821 | ddt_create(spa); | |
5822 | ||
5823 | spa_update_dspace(spa); | |
5824 | ||
34dc7c2f BB |
5825 | tx = dmu_tx_create_assigned(dp, txg); |
5826 | ||
d5e024cb BB |
5827 | /* |
5828 | * Create the pool's history object. | |
5829 | */ | |
5830 | if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history) | |
5831 | spa_history_create_obj(spa, tx); | |
5832 | ||
5833 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); | |
5834 | spa_history_log_version(spa, "create", tx); | |
5835 | ||
34dc7c2f BB |
5836 | /* |
5837 | * Create the pool config object. | |
5838 | */ | |
5839 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 5840 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
5841 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
5842 | ||
5843 | if (zap_add(spa->spa_meta_objset, | |
5844 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
5845 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
5846 | cmn_err(CE_PANIC, "failed to add pool config"); | |
5847 | } | |
5848 | ||
428870ff BB |
5849 | if (zap_add(spa->spa_meta_objset, |
5850 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
5851 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
5852 | cmn_err(CE_PANIC, "failed to add pool version"); | |
5853 | } | |
5854 | ||
34dc7c2f BB |
5855 | /* Newly created pools with the right version are always deflated. */ |
5856 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
5857 | spa->spa_deflate = TRUE; | |
5858 | if (zap_add(spa->spa_meta_objset, | |
5859 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
5860 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
5861 | cmn_err(CE_PANIC, "failed to add deflate"); | |
5862 | } | |
5863 | } | |
5864 | ||
5865 | /* | |
428870ff | 5866 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
5867 | * because sync-to-convergence takes longer if the blocksize |
5868 | * keeps changing. | |
5869 | */ | |
428870ff BB |
5870 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
5871 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 5872 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 5873 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
5874 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
5875 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
5876 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 5877 | } |
428870ff BB |
5878 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
5879 | spa->spa_meta_objset, obj)); | |
34dc7c2f | 5880 | |
3c67d83a TH |
5881 | /* |
5882 | * Generate some random noise for salted checksums to operate on. | |
5883 | */ | |
5884 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
5885 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
5886 | ||
34dc7c2f BB |
5887 | /* |
5888 | * Set pool properties. | |
5889 | */ | |
5890 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
5891 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
5892 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 5893 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
379ca9cf | 5894 | spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); |
1b939560 | 5895 | spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM); |
428870ff | 5896 | |
d164b209 BB |
5897 | if (props != NULL) { |
5898 | spa_configfile_set(spa, props, B_FALSE); | |
13fe0198 | 5899 | spa_sync_props(props, tx); |
d164b209 | 5900 | } |
34dc7c2f BB |
5901 | |
5902 | dmu_tx_commit(tx); | |
5903 | ||
5904 | spa->spa_sync_on = B_TRUE; | |
b5256303 | 5905 | txg_sync_start(dp); |
379ca9cf | 5906 | mmp_thread_start(spa); |
b5256303 | 5907 | txg_wait_synced(dp, txg); |
34dc7c2f | 5908 | |
9d5b5245 SD |
5909 | spa_spawn_aux_threads(spa); |
5910 | ||
a1d477c2 | 5911 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
34dc7c2f | 5912 | |
0c66c32d JG |
5913 | /* |
5914 | * Don't count references from objsets that are already closed | |
5915 | * and are making their way through the eviction process. | |
5916 | */ | |
5917 | spa_evicting_os_wait(spa); | |
424fd7c3 | 5918 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
3dfb57a3 | 5919 | spa->spa_load_state = SPA_LOAD_NONE; |
b128c09f | 5920 | |
d164b209 BB |
5921 | mutex_exit(&spa_namespace_lock); |
5922 | ||
34dc7c2f BB |
5923 | return (0); |
5924 | } | |
5925 | ||
9babb374 BB |
5926 | /* |
5927 | * Import a non-root pool into the system. | |
5928 | */ | |
5929 | int | |
13fe0198 | 5930 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
5931 | { |
5932 | spa_t *spa; | |
5933 | char *altroot = NULL; | |
428870ff | 5934 | spa_load_state_t state = SPA_LOAD_IMPORT; |
8a393be3 | 5935 | zpool_load_policy_t policy; |
da92d5cb | 5936 | spa_mode_t mode = spa_mode_global; |
572e2857 | 5937 | uint64_t readonly = B_FALSE; |
9babb374 | 5938 | int error; |
34dc7c2f BB |
5939 | nvlist_t *nvroot; |
5940 | nvlist_t **spares, **l2cache; | |
5941 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
5942 | |
5943 | /* | |
5944 | * If a pool with this name exists, return failure. | |
5945 | */ | |
5946 | mutex_enter(&spa_namespace_lock); | |
428870ff | 5947 | if (spa_lookup(pool) != NULL) { |
9babb374 | 5948 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5949 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5950 | } |
5951 | ||
5952 | /* | |
5953 | * Create and initialize the spa structure. | |
5954 | */ | |
5955 | (void) nvlist_lookup_string(props, | |
5956 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
5957 | (void) nvlist_lookup_uint64(props, |
5958 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
5959 | if (readonly) | |
da92d5cb | 5960 | mode = SPA_MODE_READ; |
428870ff | 5961 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
5962 | spa->spa_import_flags = flags; |
5963 | ||
5964 | /* | |
5965 | * Verbatim import - Take a pool and insert it into the namespace | |
5966 | * as if it had been loaded at boot. | |
5967 | */ | |
5968 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
5969 | if (props != NULL) | |
5970 | spa_configfile_set(spa, props, B_FALSE); | |
5971 | ||
a1d477c2 | 5972 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 5973 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
4a0ee12a | 5974 | zfs_dbgmsg("spa_import: verbatim import of %s", pool); |
572e2857 | 5975 | mutex_exit(&spa_namespace_lock); |
572e2857 BB |
5976 | return (0); |
5977 | } | |
5978 | ||
5979 | spa_activate(spa, mode); | |
34dc7c2f | 5980 | |
9babb374 BB |
5981 | /* |
5982 | * Don't start async tasks until we know everything is healthy. | |
5983 | */ | |
5984 | spa_async_suspend(spa); | |
b128c09f | 5985 | |
8a393be3 PZ |
5986 | zpool_get_load_policy(config, &policy); |
5987 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) | |
572e2857 BB |
5988 | state = SPA_LOAD_RECOVER; |
5989 | ||
6cb8e530 | 5990 | spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; |
572e2857 | 5991 | |
6cb8e530 PZ |
5992 | if (state != SPA_LOAD_RECOVER) { |
5993 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
5994 | zfs_dbgmsg("spa_import: importing %s", pool); | |
5995 | } else { | |
5996 | zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " | |
8a393be3 | 5997 | "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); |
6cb8e530 | 5998 | } |
8a393be3 | 5999 | error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); |
428870ff BB |
6000 | |
6001 | /* | |
572e2857 BB |
6002 | * Propagate anything learned while loading the pool and pass it |
6003 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 6004 | */ |
572e2857 BB |
6005 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
6006 | spa->spa_load_info) == 0); | |
34dc7c2f | 6007 | |
b128c09f | 6008 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6009 | /* |
9babb374 BB |
6010 | * Toss any existing sparelist, as it doesn't have any validity |
6011 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 6012 | */ |
9babb374 | 6013 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
6014 | nvlist_free(spa->spa_spares.sav_config); |
6015 | spa->spa_spares.sav_config = NULL; | |
6016 | spa_load_spares(spa); | |
6017 | } | |
9babb374 | 6018 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
6019 | nvlist_free(spa->spa_l2cache.sav_config); |
6020 | spa->spa_l2cache.sav_config = NULL; | |
6021 | spa_load_l2cache(spa); | |
6022 | } | |
6023 | ||
6024 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
6025 | &nvroot) == 0); | |
b128c09f | 6026 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 6027 | |
d164b209 BB |
6028 | if (props != NULL) |
6029 | spa_configfile_set(spa, props, B_FALSE); | |
6030 | ||
fb5f0bc8 BB |
6031 | if (error != 0 || (props && spa_writeable(spa) && |
6032 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
6033 | spa_unload(spa); |
6034 | spa_deactivate(spa); | |
6035 | spa_remove(spa); | |
34dc7c2f BB |
6036 | mutex_exit(&spa_namespace_lock); |
6037 | return (error); | |
6038 | } | |
6039 | ||
572e2857 BB |
6040 | spa_async_resume(spa); |
6041 | ||
34dc7c2f BB |
6042 | /* |
6043 | * Override any spares and level 2 cache devices as specified by | |
6044 | * the user, as these may have correct device names/devids, etc. | |
6045 | */ | |
6046 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
6047 | &spares, &nspares) == 0) { | |
6048 | if (spa->spa_spares.sav_config) | |
6049 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, | |
6050 | ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); | |
6051 | else | |
6052 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, | |
79c76d5b | 6053 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
6054 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
6055 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 6056 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6057 | spa_load_spares(spa); |
b128c09f | 6058 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6059 | spa->spa_spares.sav_sync = B_TRUE; |
6060 | } | |
6061 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
6062 | &l2cache, &nl2cache) == 0) { | |
6063 | if (spa->spa_l2cache.sav_config) | |
6064 | VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, | |
6065 | ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); | |
6066 | else | |
6067 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 6068 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
6069 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
6070 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 6071 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6072 | spa_load_l2cache(spa); |
b128c09f | 6073 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6074 | spa->spa_l2cache.sav_sync = B_TRUE; |
6075 | } | |
6076 | ||
428870ff BB |
6077 | /* |
6078 | * Check for any removed devices. | |
6079 | */ | |
6080 | if (spa->spa_autoreplace) { | |
6081 | spa_aux_check_removed(&spa->spa_spares); | |
6082 | spa_aux_check_removed(&spa->spa_l2cache); | |
6083 | } | |
6084 | ||
fb5f0bc8 | 6085 | if (spa_writeable(spa)) { |
b128c09f BB |
6086 | /* |
6087 | * Update the config cache to include the newly-imported pool. | |
6088 | */ | |
45d1cae3 | 6089 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 6090 | } |
34dc7c2f | 6091 | |
34dc7c2f | 6092 | /* |
9babb374 BB |
6093 | * It's possible that the pool was expanded while it was exported. |
6094 | * We kick off an async task to handle this for us. | |
34dc7c2f | 6095 | */ |
9babb374 | 6096 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 6097 | |
d5e024cb | 6098 | spa_history_log_version(spa, "import", NULL); |
fb390aaf | 6099 | |
12fa0466 | 6100 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
fb390aaf | 6101 | |
fb390aaf HR |
6102 | mutex_exit(&spa_namespace_lock); |
6103 | ||
ec213971 | 6104 | zvol_create_minors_recursive(pool); |
4a22ba5b | 6105 | |
b128c09f BB |
6106 | return (0); |
6107 | } | |
6108 | ||
34dc7c2f BB |
6109 | nvlist_t * |
6110 | spa_tryimport(nvlist_t *tryconfig) | |
6111 | { | |
6112 | nvlist_t *config = NULL; | |
6cb8e530 | 6113 | char *poolname, *cachefile; |
34dc7c2f BB |
6114 | spa_t *spa; |
6115 | uint64_t state; | |
d164b209 | 6116 | int error; |
8a393be3 | 6117 | zpool_load_policy_t policy; |
34dc7c2f BB |
6118 | |
6119 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
6120 | return (NULL); | |
6121 | ||
6122 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
6123 | return (NULL); | |
6124 | ||
6125 | /* | |
6126 | * Create and initialize the spa structure. | |
6127 | */ | |
6128 | mutex_enter(&spa_namespace_lock); | |
428870ff | 6129 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
da92d5cb | 6130 | spa_activate(spa, SPA_MODE_READ); |
34dc7c2f BB |
6131 | |
6132 | /* | |
8a393be3 | 6133 | * Rewind pool if a max txg was provided. |
34dc7c2f | 6134 | */ |
8a393be3 PZ |
6135 | zpool_get_load_policy(spa->spa_config, &policy); |
6136 | if (policy.zlp_txg != UINT64_MAX) { | |
6137 | spa->spa_load_max_txg = policy.zlp_txg; | |
6cb8e530 PZ |
6138 | spa->spa_extreme_rewind = B_TRUE; |
6139 | zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", | |
8a393be3 | 6140 | poolname, (longlong_t)policy.zlp_txg); |
6cb8e530 PZ |
6141 | } else { |
6142 | zfs_dbgmsg("spa_tryimport: importing %s", poolname); | |
6143 | } | |
6144 | ||
6145 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) | |
6146 | == 0) { | |
6147 | zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); | |
6148 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; | |
6149 | } else { | |
6150 | spa->spa_config_source = SPA_CONFIG_SRC_SCAN; | |
6151 | } | |
6152 | ||
6153 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); | |
34dc7c2f BB |
6154 | |
6155 | /* | |
6156 | * If 'tryconfig' was at least parsable, return the current config. | |
6157 | */ | |
6158 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 6159 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f BB |
6160 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, |
6161 | poolname) == 0); | |
6162 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
6163 | state) == 0); | |
6164 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
6165 | spa->spa_uberblock.ub_timestamp) == 0); | |
9ae529ec CS |
6166 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
6167 | spa->spa_load_info) == 0); | |
ffe9d382 BB |
6168 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, |
6169 | spa->spa_errata) == 0); | |
34dc7c2f BB |
6170 | |
6171 | /* | |
6172 | * If the bootfs property exists on this pool then we | |
6173 | * copy it out so that external consumers can tell which | |
6174 | * pools are bootable. | |
6175 | */ | |
d164b209 | 6176 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
79c76d5b | 6177 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6178 | |
6179 | /* | |
6180 | * We have to play games with the name since the | |
6181 | * pool was opened as TRYIMPORT_NAME. | |
6182 | */ | |
b128c09f | 6183 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
6184 | spa->spa_bootfs, tmpname) == 0) { |
6185 | char *cp; | |
d1d7e268 MK |
6186 | char *dsname; |
6187 | ||
79c76d5b | 6188 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
6189 | |
6190 | cp = strchr(tmpname, '/'); | |
6191 | if (cp == NULL) { | |
6192 | (void) strlcpy(dsname, tmpname, | |
6193 | MAXPATHLEN); | |
6194 | } else { | |
6195 | (void) snprintf(dsname, MAXPATHLEN, | |
6196 | "%s/%s", poolname, ++cp); | |
6197 | } | |
6198 | VERIFY(nvlist_add_string(config, | |
6199 | ZPOOL_CONFIG_BOOTFS, dsname) == 0); | |
6200 | kmem_free(dsname, MAXPATHLEN); | |
6201 | } | |
6202 | kmem_free(tmpname, MAXPATHLEN); | |
6203 | } | |
6204 | ||
6205 | /* | |
6206 | * Add the list of hot spares and level 2 cache devices. | |
6207 | */ | |
9babb374 | 6208 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
6209 | spa_add_spares(spa, config); |
6210 | spa_add_l2cache(spa, config); | |
9babb374 | 6211 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
6212 | } |
6213 | ||
6214 | spa_unload(spa); | |
6215 | spa_deactivate(spa); | |
6216 | spa_remove(spa); | |
6217 | mutex_exit(&spa_namespace_lock); | |
6218 | ||
6219 | return (config); | |
6220 | } | |
6221 | ||
6222 | /* | |
6223 | * Pool export/destroy | |
6224 | * | |
6225 | * The act of destroying or exporting a pool is very simple. We make sure there | |
6226 | * is no more pending I/O and any references to the pool are gone. Then, we | |
6227 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
6228 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
6229 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
6230 | */ |
6231 | static int | |
b128c09f | 6232 | spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 6233 | boolean_t force, boolean_t hardforce) |
34dc7c2f BB |
6234 | { |
6235 | spa_t *spa; | |
6236 | ||
6237 | if (oldconfig) | |
6238 | *oldconfig = NULL; | |
6239 | ||
da92d5cb | 6240 | if (!(spa_mode_global & SPA_MODE_WRITE)) |
2e528b49 | 6241 | return (SET_ERROR(EROFS)); |
34dc7c2f BB |
6242 | |
6243 | mutex_enter(&spa_namespace_lock); | |
6244 | if ((spa = spa_lookup(pool)) == NULL) { | |
6245 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 6246 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
6247 | } |
6248 | ||
43a85362 SD |
6249 | if (spa->spa_is_exporting) { |
6250 | /* the pool is being exported by another thread */ | |
6251 | mutex_exit(&spa_namespace_lock); | |
6252 | return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS)); | |
6253 | } | |
6254 | spa->spa_is_exporting = B_TRUE; | |
6255 | ||
34dc7c2f BB |
6256 | /* |
6257 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
6258 | * reacquire the namespace lock, and see if we can export. | |
6259 | */ | |
6260 | spa_open_ref(spa, FTAG); | |
6261 | mutex_exit(&spa_namespace_lock); | |
6262 | spa_async_suspend(spa); | |
a0bd735a BP |
6263 | if (spa->spa_zvol_taskq) { |
6264 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
6265 | taskq_wait(spa->spa_zvol_taskq); | |
6266 | } | |
34dc7c2f BB |
6267 | mutex_enter(&spa_namespace_lock); |
6268 | spa_close(spa, FTAG); | |
6269 | ||
d14cfd83 IH |
6270 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) |
6271 | goto export_spa; | |
34dc7c2f | 6272 | /* |
d14cfd83 IH |
6273 | * The pool will be in core if it's openable, in which case we can |
6274 | * modify its state. Objsets may be open only because they're dirty, | |
6275 | * so we have to force it to sync before checking spa_refcnt. | |
34dc7c2f | 6276 | */ |
0c66c32d | 6277 | if (spa->spa_sync_on) { |
34dc7c2f | 6278 | txg_wait_synced(spa->spa_dsl_pool, 0); |
0c66c32d JG |
6279 | spa_evicting_os_wait(spa); |
6280 | } | |
34dc7c2f | 6281 | |
d14cfd83 IH |
6282 | /* |
6283 | * A pool cannot be exported or destroyed if there are active | |
6284 | * references. If we are resetting a pool, allow references by | |
6285 | * fault injection handlers. | |
6286 | */ | |
6287 | if (!spa_refcount_zero(spa) || | |
6288 | (spa->spa_inject_ref != 0 && | |
6289 | new_state != POOL_STATE_UNINITIALIZED)) { | |
6290 | spa_async_resume(spa); | |
43a85362 | 6291 | spa->spa_is_exporting = B_FALSE; |
d14cfd83 IH |
6292 | mutex_exit(&spa_namespace_lock); |
6293 | return (SET_ERROR(EBUSY)); | |
6294 | } | |
34dc7c2f | 6295 | |
d14cfd83 | 6296 | if (spa->spa_sync_on) { |
b128c09f BB |
6297 | /* |
6298 | * A pool cannot be exported if it has an active shared spare. | |
6299 | * This is to prevent other pools stealing the active spare | |
6300 | * from an exported pool. At user's own will, such pool can | |
6301 | * be forcedly exported. | |
6302 | */ | |
6303 | if (!force && new_state == POOL_STATE_EXPORTED && | |
6304 | spa_has_active_shared_spare(spa)) { | |
6305 | spa_async_resume(spa); | |
43a85362 | 6306 | spa->spa_is_exporting = B_FALSE; |
b128c09f | 6307 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 6308 | return (SET_ERROR(EXDEV)); |
b128c09f | 6309 | } |
34dc7c2f | 6310 | |
619f0976 GW |
6311 | /* |
6312 | * We're about to export or destroy this pool. Make sure | |
1b939560 BB |
6313 | * we stop all initialization and trim activity here before |
6314 | * we set the spa_final_txg. This will ensure that all | |
619f0976 GW |
6315 | * dirty data resulting from the initialization is |
6316 | * committed to disk before we unload the pool. | |
6317 | */ | |
6318 | if (spa->spa_root_vdev != NULL) { | |
1b939560 BB |
6319 | vdev_t *rvd = spa->spa_root_vdev; |
6320 | vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE); | |
6321 | vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE); | |
6322 | vdev_autotrim_stop_all(spa); | |
9a49d3f3 | 6323 | vdev_rebuild_stop_all(spa); |
619f0976 GW |
6324 | } |
6325 | ||
34dc7c2f BB |
6326 | /* |
6327 | * We want this to be reflected on every label, | |
6328 | * so mark them all dirty. spa_unload() will do the | |
6329 | * final sync that pushes these changes out. | |
6330 | */ | |
fb5f0bc8 | 6331 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 6332 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 6333 | spa->spa_state = new_state; |
428870ff BB |
6334 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
6335 | TXG_DEFER_SIZE + 1; | |
34dc7c2f | 6336 | vdev_config_dirty(spa->spa_root_vdev); |
b128c09f | 6337 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6338 | } |
6339 | } | |
6340 | ||
d14cfd83 | 6341 | export_spa: |
d5e024cb BB |
6342 | if (new_state == POOL_STATE_DESTROYED) |
6343 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); | |
6344 | else if (new_state == POOL_STATE_EXPORTED) | |
6345 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT); | |
34dc7c2f BB |
6346 | |
6347 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
6348 | spa_unload(spa); | |
6349 | spa_deactivate(spa); | |
6350 | } | |
6351 | ||
6352 | if (oldconfig && spa->spa_config) | |
6353 | VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); | |
6354 | ||
6355 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 | 6356 | if (!hardforce) |
a1d477c2 | 6357 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 6358 | spa_remove(spa); |
43a85362 SD |
6359 | } else { |
6360 | /* | |
6361 | * If spa_remove() is not called for this spa_t and | |
6362 | * there is any possibility that it can be reused, | |
6363 | * we make sure to reset the exporting flag. | |
6364 | */ | |
6365 | spa->spa_is_exporting = B_FALSE; | |
34dc7c2f | 6366 | } |
34dc7c2f | 6367 | |
43a85362 | 6368 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
6369 | return (0); |
6370 | } | |
6371 | ||
6372 | /* | |
6373 | * Destroy a storage pool. | |
6374 | */ | |
6375 | int | |
6376 | spa_destroy(char *pool) | |
6377 | { | |
fb5f0bc8 BB |
6378 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
6379 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
6380 | } |
6381 | ||
6382 | /* | |
6383 | * Export a storage pool. | |
6384 | */ | |
6385 | int | |
fb5f0bc8 BB |
6386 | spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, |
6387 | boolean_t hardforce) | |
34dc7c2f | 6388 | { |
fb5f0bc8 BB |
6389 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
6390 | force, hardforce)); | |
34dc7c2f BB |
6391 | } |
6392 | ||
6393 | /* | |
6394 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
6395 | * from the namespace in any way. | |
6396 | */ | |
6397 | int | |
6398 | spa_reset(char *pool) | |
6399 | { | |
b128c09f | 6400 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 6401 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
6402 | } |
6403 | ||
34dc7c2f BB |
6404 | /* |
6405 | * ========================================================================== | |
6406 | * Device manipulation | |
6407 | * ========================================================================== | |
6408 | */ | |
6409 | ||
6410 | /* | |
6411 | * Add a device to a storage pool. | |
6412 | */ | |
6413 | int | |
6414 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
6415 | { | |
93e28d66 | 6416 | uint64_t txg; |
fb5f0bc8 | 6417 | int error; |
34dc7c2f BB |
6418 | vdev_t *rvd = spa->spa_root_vdev; |
6419 | vdev_t *vd, *tvd; | |
6420 | nvlist_t **spares, **l2cache; | |
6421 | uint_t nspares, nl2cache; | |
6422 | ||
572e2857 BB |
6423 | ASSERT(spa_writeable(spa)); |
6424 | ||
34dc7c2f BB |
6425 | txg = spa_vdev_enter(spa); |
6426 | ||
6427 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
6428 | VDEV_ALLOC_ADD)) != 0) | |
6429 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6430 | ||
b128c09f | 6431 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
6432 | |
6433 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
6434 | &nspares) != 0) | |
6435 | nspares = 0; | |
6436 | ||
6437 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
6438 | &nl2cache) != 0) | |
6439 | nl2cache = 0; | |
6440 | ||
b128c09f | 6441 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 6442 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 6443 | |
b128c09f BB |
6444 | if (vd->vdev_children != 0 && |
6445 | (error = vdev_create(vd, txg, B_FALSE)) != 0) | |
6446 | return (spa_vdev_exit(spa, vd, txg, error)); | |
34dc7c2f BB |
6447 | |
6448 | /* | |
6449 | * We must validate the spares and l2cache devices after checking the | |
6450 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
6451 | */ | |
b128c09f | 6452 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 6453 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
6454 | |
6455 | /* | |
a1d477c2 MA |
6456 | * If we are in the middle of a device removal, we can only add |
6457 | * devices which match the existing devices in the pool. | |
6458 | * If we are in the middle of a removal, or have some indirect | |
6459 | * vdevs, we can not add raidz toplevels. | |
34dc7c2f | 6460 | */ |
a1d477c2 MA |
6461 | if (spa->spa_vdev_removal != NULL || |
6462 | spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { | |
6463 | for (int c = 0; c < vd->vdev_children; c++) { | |
6464 | tvd = vd->vdev_child[c]; | |
6465 | if (spa->spa_vdev_removal != NULL && | |
9e052db4 | 6466 | tvd->vdev_ashift != spa->spa_max_ashift) { |
a1d477c2 MA |
6467 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
6468 | } | |
6469 | /* Fail if top level vdev is raidz */ | |
6470 | if (tvd->vdev_ops == &vdev_raidz_ops) { | |
6471 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); | |
6472 | } | |
6473 | /* | |
6474 | * Need the top level mirror to be | |
6475 | * a mirror of leaf vdevs only | |
6476 | */ | |
6477 | if (tvd->vdev_ops == &vdev_mirror_ops) { | |
6478 | for (uint64_t cid = 0; | |
6479 | cid < tvd->vdev_children; cid++) { | |
6480 | vdev_t *cvd = tvd->vdev_child[cid]; | |
6481 | if (!cvd->vdev_ops->vdev_op_leaf) { | |
6482 | return (spa_vdev_exit(spa, vd, | |
6483 | txg, EINVAL)); | |
6484 | } | |
6485 | } | |
6486 | } | |
6487 | } | |
6488 | } | |
6489 | ||
1c27024e | 6490 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
6491 | tvd = vd->vdev_child[c]; |
6492 | vdev_remove_child(vd, tvd); | |
93e28d66 | 6493 | tvd->vdev_id = rvd->vdev_children; |
34dc7c2f BB |
6494 | vdev_add_child(rvd, tvd); |
6495 | vdev_config_dirty(tvd); | |
6496 | } | |
6497 | ||
6498 | if (nspares != 0) { | |
6499 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
6500 | ZPOOL_CONFIG_SPARES); | |
6501 | spa_load_spares(spa); | |
6502 | spa->spa_spares.sav_sync = B_TRUE; | |
6503 | } | |
6504 | ||
6505 | if (nl2cache != 0) { | |
6506 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
6507 | ZPOOL_CONFIG_L2CACHE); | |
6508 | spa_load_l2cache(spa); | |
6509 | spa->spa_l2cache.sav_sync = B_TRUE; | |
6510 | } | |
6511 | ||
6512 | /* | |
6513 | * We have to be careful when adding new vdevs to an existing pool. | |
6514 | * If other threads start allocating from these vdevs before we | |
6515 | * sync the config cache, and we lose power, then upon reboot we may | |
6516 | * fail to open the pool because there are DVAs that the config cache | |
6517 | * can't translate. Therefore, we first add the vdevs without | |
6518 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
6519 | * and then let spa_config_update() initialize the new metaslabs. | |
6520 | * | |
6521 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
6522 | * if we lose power at any point in this sequence, the remaining | |
6523 | * steps will be completed the next time we load the pool. | |
6524 | */ | |
6525 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
6526 | ||
6527 | mutex_enter(&spa_namespace_lock); | |
6528 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
12fa0466 | 6529 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); |
34dc7c2f BB |
6530 | mutex_exit(&spa_namespace_lock); |
6531 | ||
6532 | return (0); | |
6533 | } | |
6534 | ||
6535 | /* | |
6536 | * Attach a device to a mirror. The arguments are the path to any device | |
6537 | * in the mirror, and the nvroot for the new device. If the path specifies | |
6538 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
6539 | * | |
6540 | * If 'replacing' is specified, the new device is intended to replace the | |
6541 | * existing device; in this case the two devices are made into their own | |
6542 | * mirror using the 'replacing' vdev, which is functionally identical to | |
6543 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
6544 | * extra rules: you can't attach to it after it's been created, and upon | |
6545 | * completion of resilvering, the first disk (the one being replaced) | |
6546 | * is automatically detached. | |
9a49d3f3 BB |
6547 | * |
6548 | * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild) | |
6549 | * should be performed instead of traditional healing reconstruction. From | |
6550 | * an administrators perspective these are both resilver operations. | |
34dc7c2f BB |
6551 | */ |
6552 | int | |
9a49d3f3 BB |
6553 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing, |
6554 | int rebuild) | |
34dc7c2f | 6555 | { |
428870ff | 6556 | uint64_t txg, dtl_max_txg; |
9a49d3f3 | 6557 | vdev_t *rvd = spa->spa_root_vdev; |
34dc7c2f BB |
6558 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
6559 | vdev_ops_t *pvops; | |
b128c09f BB |
6560 | char *oldvdpath, *newvdpath; |
6561 | int newvd_isspare; | |
6562 | int error; | |
34dc7c2f | 6563 | |
572e2857 BB |
6564 | ASSERT(spa_writeable(spa)); |
6565 | ||
34dc7c2f BB |
6566 | txg = spa_vdev_enter(spa); |
6567 | ||
b128c09f | 6568 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6569 | |
d2734cce SD |
6570 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6571 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6572 | error = (spa_has_checkpoint(spa)) ? | |
6573 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6574 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6575 | } | |
6576 | ||
9a49d3f3 BB |
6577 | if (rebuild) { |
6578 | if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD)) | |
6579 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6580 | ||
6581 | if (dsl_scan_resilvering(spa_get_dsl(spa))) | |
6582 | return (spa_vdev_exit(spa, NULL, txg, | |
6583 | ZFS_ERR_RESILVER_IN_PROGRESS)); | |
6584 | } else { | |
6585 | if (vdev_rebuild_active(rvd)) | |
6586 | return (spa_vdev_exit(spa, NULL, txg, | |
6587 | ZFS_ERR_REBUILD_IN_PROGRESS)); | |
6588 | } | |
6589 | ||
9e052db4 | 6590 | if (spa->spa_vdev_removal != NULL) |
a1d477c2 | 6591 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
a1d477c2 | 6592 | |
34dc7c2f BB |
6593 | if (oldvd == NULL) |
6594 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6595 | ||
6596 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
6597 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6598 | ||
6599 | pvd = oldvd->vdev_parent; | |
6600 | ||
6601 | if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, | |
5ffb9d1d | 6602 | VDEV_ALLOC_ATTACH)) != 0) |
34dc7c2f BB |
6603 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
6604 | ||
6605 | if (newrootvd->vdev_children != 1) | |
6606 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6607 | ||
6608 | newvd = newrootvd->vdev_child[0]; | |
6609 | ||
6610 | if (!newvd->vdev_ops->vdev_op_leaf) | |
6611 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6612 | ||
6613 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
6614 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
6615 | ||
6616 | /* | |
6617 | * Spares can't replace logs | |
6618 | */ | |
b128c09f | 6619 | if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) |
34dc7c2f BB |
6620 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
6621 | ||
9a49d3f3 BB |
6622 | if (rebuild) { |
6623 | /* | |
6624 | * For rebuilds, the parent vdev must support reconstruction | |
6625 | * using only space maps. This means the only allowable | |
6626 | * parents are the root vdev or a mirror vdev. | |
6627 | */ | |
6628 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6629 | pvd->vdev_ops != &vdev_root_ops) { | |
6630 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6631 | } | |
6632 | } | |
6633 | ||
34dc7c2f BB |
6634 | if (!replacing) { |
6635 | /* | |
6636 | * For attach, the only allowable parent is a mirror or the root | |
6637 | * vdev. | |
6638 | */ | |
6639 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6640 | pvd->vdev_ops != &vdev_root_ops) | |
6641 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6642 | ||
6643 | pvops = &vdev_mirror_ops; | |
6644 | } else { | |
6645 | /* | |
6646 | * Active hot spares can only be replaced by inactive hot | |
6647 | * spares. | |
6648 | */ | |
6649 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 6650 | oldvd->vdev_isspare && |
34dc7c2f BB |
6651 | !spa_has_spare(spa, newvd->vdev_guid)) |
6652 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6653 | ||
6654 | /* | |
6655 | * If the source is a hot spare, and the parent isn't already a | |
6656 | * spare, then we want to create a new hot spare. Otherwise, we | |
6657 | * want to create a replacing vdev. The user is not allowed to | |
6658 | * attach to a spared vdev child unless the 'isspare' state is | |
6659 | * the same (spare replaces spare, non-spare replaces | |
6660 | * non-spare). | |
6661 | */ | |
572e2857 BB |
6662 | if (pvd->vdev_ops == &vdev_replacing_ops && |
6663 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 6664 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6665 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
6666 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 6667 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6668 | } |
6669 | ||
6670 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
6671 | pvops = &vdev_spare_ops; |
6672 | else | |
6673 | pvops = &vdev_replacing_ops; | |
6674 | } | |
6675 | ||
6676 | /* | |
9babb374 | 6677 | * Make sure the new device is big enough. |
34dc7c2f | 6678 | */ |
9babb374 | 6679 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
6680 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
6681 | ||
6682 | /* | |
6683 | * The new device cannot have a higher alignment requirement | |
6684 | * than the top-level vdev. | |
6685 | */ | |
6686 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
9a49d3f3 | 6687 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
34dc7c2f BB |
6688 | |
6689 | /* | |
6690 | * If this is an in-place replacement, update oldvd's path and devid | |
6691 | * to make it distinguishable from newvd, and unopenable from now on. | |
6692 | */ | |
6693 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
6694 | spa_strfree(oldvd->vdev_path); | |
6695 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
79c76d5b | 6696 | KM_SLEEP); |
c9e319fa JL |
6697 | (void) snprintf(oldvd->vdev_path, strlen(newvd->vdev_path) + 5, |
6698 | "%s/%s", newvd->vdev_path, "old"); | |
34dc7c2f BB |
6699 | if (oldvd->vdev_devid != NULL) { |
6700 | spa_strfree(oldvd->vdev_devid); | |
6701 | oldvd->vdev_devid = NULL; | |
6702 | } | |
6703 | } | |
6704 | ||
6705 | /* | |
6706 | * If the parent is not a mirror, or if we're replacing, insert the new | |
6707 | * mirror/replacing/spare vdev above oldvd. | |
6708 | */ | |
6709 | if (pvd->vdev_ops != pvops) | |
6710 | pvd = vdev_add_parent(oldvd, pvops); | |
6711 | ||
6712 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
6713 | ASSERT(pvd->vdev_ops == pvops); | |
6714 | ASSERT(oldvd->vdev_parent == pvd); | |
6715 | ||
6716 | /* | |
6717 | * Extract the new device from its root and add it to pvd. | |
6718 | */ | |
6719 | vdev_remove_child(newrootvd, newvd); | |
6720 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 6721 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
6722 | vdev_add_child(pvd, newvd); |
6723 | ||
6d82f98c IH |
6724 | /* |
6725 | * Reevaluate the parent vdev state. | |
6726 | */ | |
6727 | vdev_propagate_state(pvd); | |
6728 | ||
34dc7c2f BB |
6729 | tvd = newvd->vdev_top; |
6730 | ASSERT(pvd->vdev_top == tvd); | |
6731 | ASSERT(tvd->vdev_parent == rvd); | |
6732 | ||
6733 | vdev_config_dirty(tvd); | |
6734 | ||
6735 | /* | |
428870ff BB |
6736 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
6737 | * for any dmu_sync-ed blocks. It will propagate upward when | |
6738 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 6739 | */ |
428870ff | 6740 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 6741 | |
9a49d3f3 BB |
6742 | vdev_dtl_dirty(newvd, DTL_MISSING, |
6743 | TXG_INITIAL, dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 6744 | |
9babb374 | 6745 | if (newvd->vdev_isspare) { |
34dc7c2f | 6746 | spa_spare_activate(newvd); |
12fa0466 | 6747 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); |
9babb374 BB |
6748 | } |
6749 | ||
b128c09f BB |
6750 | oldvdpath = spa_strdup(oldvd->vdev_path); |
6751 | newvdpath = spa_strdup(newvd->vdev_path); | |
6752 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
6753 | |
6754 | /* | |
6755 | * Mark newvd's DTL dirty in this txg. | |
6756 | */ | |
6757 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
6758 | ||
428870ff | 6759 | /* |
9a49d3f3 BB |
6760 | * Schedule the resilver or rebuild to restart in the future. We do |
6761 | * this to ensure that dmu_sync-ed blocks have been stitched into the | |
6762 | * respective datasets. | |
428870ff | 6763 | */ |
9a49d3f3 BB |
6764 | if (rebuild) { |
6765 | newvd->vdev_rebuild_txg = txg; | |
6766 | ||
6767 | vdev_rebuild(tvd); | |
6768 | } else { | |
6769 | newvd->vdev_resilver_txg = txg; | |
6770 | ||
6771 | if (dsl_scan_resilvering(spa_get_dsl(spa)) && | |
6772 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) { | |
6773 | vdev_defer_resilver(newvd); | |
6774 | } else { | |
6775 | dsl_scan_restart_resilver(spa->spa_dsl_pool, | |
6776 | dtl_max_txg); | |
6777 | } | |
6778 | } | |
428870ff | 6779 | |
fb390aaf | 6780 | if (spa->spa_bootfs) |
12fa0466 | 6781 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); |
fb390aaf | 6782 | |
12fa0466 | 6783 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); |
fb390aaf | 6784 | |
428870ff BB |
6785 | /* |
6786 | * Commit the config | |
6787 | */ | |
6788 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 6789 | |
6f1ffb06 | 6790 | spa_history_log_internal(spa, "vdev attach", NULL, |
428870ff | 6791 | "%s vdev=%s %s vdev=%s", |
45d1cae3 BB |
6792 | replacing && newvd_isspare ? "spare in" : |
6793 | replacing ? "replace" : "attach", newvdpath, | |
6794 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
6795 | |
6796 | spa_strfree(oldvdpath); | |
6797 | spa_strfree(newvdpath); | |
6798 | ||
34dc7c2f BB |
6799 | return (0); |
6800 | } | |
6801 | ||
6802 | /* | |
6803 | * Detach a device from a mirror or replacing vdev. | |
d3cc8b15 | 6804 | * |
34dc7c2f BB |
6805 | * If 'replace_done' is specified, only detach if the parent |
6806 | * is a replacing vdev. | |
6807 | */ | |
6808 | int | |
fb5f0bc8 | 6809 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
6810 | { |
6811 | uint64_t txg; | |
fb5f0bc8 | 6812 | int error; |
2a8ba608 | 6813 | vdev_t *rvd __maybe_unused = spa->spa_root_vdev; |
34dc7c2f BB |
6814 | vdev_t *vd, *pvd, *cvd, *tvd; |
6815 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 6816 | uint64_t unspare_guid = 0; |
428870ff | 6817 | char *vdpath; |
1c27024e | 6818 | |
572e2857 BB |
6819 | ASSERT(spa_writeable(spa)); |
6820 | ||
9a49d3f3 | 6821 | txg = spa_vdev_detach_enter(spa, guid); |
34dc7c2f | 6822 | |
b128c09f | 6823 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6824 | |
d2734cce SD |
6825 | /* |
6826 | * Besides being called directly from the userland through the | |
6827 | * ioctl interface, spa_vdev_detach() can be potentially called | |
6828 | * at the end of spa_vdev_resilver_done(). | |
6829 | * | |
6830 | * In the regular case, when we have a checkpoint this shouldn't | |
6831 | * happen as we never empty the DTLs of a vdev during the scrub | |
6832 | * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() | |
6833 | * should never get here when we have a checkpoint. | |
6834 | * | |
6835 | * That said, even in a case when we checkpoint the pool exactly | |
6836 | * as spa_vdev_resilver_done() calls this function everything | |
6837 | * should be fine as the resilver will return right away. | |
6838 | */ | |
6839 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
6840 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6841 | error = (spa_has_checkpoint(spa)) ? | |
6842 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6843 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6844 | } | |
6845 | ||
34dc7c2f BB |
6846 | if (vd == NULL) |
6847 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6848 | ||
6849 | if (!vd->vdev_ops->vdev_op_leaf) | |
6850 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6851 | ||
6852 | pvd = vd->vdev_parent; | |
6853 | ||
fb5f0bc8 BB |
6854 | /* |
6855 | * If the parent/child relationship is not as expected, don't do it. | |
6856 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
6857 | * vdev that's replacing B with C. The user's intent in replacing | |
6858 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
6859 | * the replace by detaching C, the expected behavior is to end up | |
6860 | * M(A,B). But suppose that right after deciding to detach C, | |
6861 | * the replacement of B completes. We would have M(A,C), and then | |
6862 | * ask to detach C, which would leave us with just A -- not what | |
6863 | * the user wanted. To prevent this, we make sure that the | |
6864 | * parent/child relationship hasn't changed -- in this example, | |
6865 | * that C's parent is still the replacing vdev R. | |
6866 | */ | |
6867 | if (pvd->vdev_guid != pguid && pguid != 0) | |
6868 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
6869 | ||
34dc7c2f | 6870 | /* |
572e2857 | 6871 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 6872 | */ |
572e2857 BB |
6873 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
6874 | pvd->vdev_ops != &vdev_spare_ops) | |
6875 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
6876 | |
6877 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
6878 | spa_version(spa) >= SPA_VERSION_SPARES); | |
6879 | ||
6880 | /* | |
6881 | * Only mirror, replacing, and spare vdevs support detach. | |
6882 | */ | |
6883 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
6884 | pvd->vdev_ops != &vdev_mirror_ops && | |
6885 | pvd->vdev_ops != &vdev_spare_ops) | |
6886 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6887 | ||
6888 | /* | |
fb5f0bc8 BB |
6889 | * If this device has the only valid copy of some data, |
6890 | * we cannot safely detach it. | |
34dc7c2f | 6891 | */ |
fb5f0bc8 | 6892 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
6893 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
6894 | ||
fb5f0bc8 | 6895 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 6896 | |
b128c09f BB |
6897 | /* |
6898 | * If we are detaching the second disk from a replacing vdev, then | |
6899 | * check to see if we changed the original vdev's path to have "/old" | |
6900 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
6901 | */ | |
572e2857 BB |
6902 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
6903 | vd->vdev_path != NULL) { | |
6904 | size_t len = strlen(vd->vdev_path); | |
6905 | ||
1c27024e | 6906 | for (int c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
6907 | cvd = pvd->vdev_child[c]; |
6908 | ||
6909 | if (cvd == vd || cvd->vdev_path == NULL) | |
6910 | continue; | |
6911 | ||
6912 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
6913 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
6914 | spa_strfree(cvd->vdev_path); | |
6915 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
6916 | break; | |
6917 | } | |
b128c09f BB |
6918 | } |
6919 | } | |
6920 | ||
34dc7c2f BB |
6921 | /* |
6922 | * If we are detaching the original disk from a spare, then it implies | |
6923 | * that the spare should become a real disk, and be removed from the | |
6924 | * active spare list for the pool. | |
6925 | */ | |
6926 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 BB |
6927 | vd->vdev_id == 0 && |
6928 | pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) | |
34dc7c2f BB |
6929 | unspare = B_TRUE; |
6930 | ||
6931 | /* | |
6932 | * Erase the disk labels so the disk can be used for other things. | |
6933 | * This must be done after all other error cases are handled, | |
6934 | * but before we disembowel vd (so we can still do I/O to it). | |
6935 | * But if we can't do it, don't treat the error as fatal -- | |
6936 | * it may be that the unwritability of the disk is the reason | |
6937 | * it's being detached! | |
6938 | */ | |
6939 | error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
6940 | ||
6941 | /* | |
6942 | * Remove vd from its parent and compact the parent's children. | |
6943 | */ | |
6944 | vdev_remove_child(pvd, vd); | |
6945 | vdev_compact_children(pvd); | |
6946 | ||
6947 | /* | |
6948 | * Remember one of the remaining children so we can get tvd below. | |
6949 | */ | |
572e2857 | 6950 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
6951 | |
6952 | /* | |
6953 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
6954 | * do it now, marking the vdev as no longer a spare in the process. |
6955 | * We must do this before vdev_remove_parent(), because that can | |
6956 | * change the GUID if it creates a new toplevel GUID. For a similar | |
6957 | * reason, we must remove the spare now, in the same txg as the detach; | |
6958 | * otherwise someone could attach a new sibling, change the GUID, and | |
6959 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
6960 | */ |
6961 | if (unspare) { | |
6962 | ASSERT(cvd->vdev_isspare); | |
6963 | spa_spare_remove(cvd); | |
6964 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 6965 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 6966 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
6967 | } |
6968 | ||
428870ff BB |
6969 | /* |
6970 | * If the parent mirror/replacing vdev only has one child, | |
6971 | * the parent is no longer needed. Remove it from the tree. | |
6972 | */ | |
572e2857 BB |
6973 | if (pvd->vdev_children == 1) { |
6974 | if (pvd->vdev_ops == &vdev_spare_ops) | |
6975 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 6976 | vdev_remove_parent(cvd); |
572e2857 BB |
6977 | } |
6978 | ||
428870ff BB |
6979 | /* |
6980 | * We don't set tvd until now because the parent we just removed | |
6981 | * may have been the previous top-level vdev. | |
6982 | */ | |
6983 | tvd = cvd->vdev_top; | |
6984 | ASSERT(tvd->vdev_parent == rvd); | |
6985 | ||
6986 | /* | |
6987 | * Reevaluate the parent vdev state. | |
6988 | */ | |
6989 | vdev_propagate_state(cvd); | |
6990 | ||
6991 | /* | |
6992 | * If the 'autoexpand' property is set on the pool then automatically | |
6993 | * try to expand the size of the pool. For example if the device we | |
6994 | * just detached was smaller than the others, it may be possible to | |
6995 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
6996 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
6997 | */ | |
6998 | if (spa->spa_autoexpand) { | |
6999 | vdev_reopen(tvd); | |
7000 | vdev_expand(tvd, txg); | |
7001 | } | |
7002 | ||
7003 | vdev_config_dirty(tvd); | |
7004 | ||
7005 | /* | |
7006 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
7007 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
7008 | * But first make sure we're not on any *other* txg's DTL list, to | |
7009 | * prevent vd from being accessed after it's freed. | |
7010 | */ | |
b6ca6193 | 7011 | vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none"); |
1c27024e | 7012 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff BB |
7013 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
7014 | vd->vdev_detached = B_TRUE; | |
7015 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
7016 | ||
12fa0466 | 7017 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); |
e60e158e | 7018 | spa_notify_waiters(spa); |
428870ff | 7019 | |
572e2857 BB |
7020 | /* hang on to the spa before we release the lock */ |
7021 | spa_open_ref(spa, FTAG); | |
7022 | ||
428870ff BB |
7023 | error = spa_vdev_exit(spa, vd, txg, 0); |
7024 | ||
6f1ffb06 | 7025 | spa_history_log_internal(spa, "detach", NULL, |
428870ff BB |
7026 | "vdev=%s", vdpath); |
7027 | spa_strfree(vdpath); | |
7028 | ||
7029 | /* | |
7030 | * If this was the removal of the original device in a hot spare vdev, | |
7031 | * then we want to go through and remove the device from the hot spare | |
7032 | * list of every other pool. | |
7033 | */ | |
7034 | if (unspare) { | |
572e2857 BB |
7035 | spa_t *altspa = NULL; |
7036 | ||
428870ff | 7037 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
7038 | while ((altspa = spa_next(altspa)) != NULL) { |
7039 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
7040 | altspa == spa) | |
428870ff | 7041 | continue; |
572e2857 BB |
7042 | |
7043 | spa_open_ref(altspa, FTAG); | |
428870ff | 7044 | mutex_exit(&spa_namespace_lock); |
572e2857 | 7045 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 7046 | mutex_enter(&spa_namespace_lock); |
572e2857 | 7047 | spa_close(altspa, FTAG); |
428870ff BB |
7048 | } |
7049 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
7050 | |
7051 | /* search the rest of the vdevs for spares to remove */ | |
7052 | spa_vdev_resilver_done(spa); | |
428870ff BB |
7053 | } |
7054 | ||
572e2857 BB |
7055 | /* all done with the spa; OK to release */ |
7056 | mutex_enter(&spa_namespace_lock); | |
7057 | spa_close(spa, FTAG); | |
7058 | mutex_exit(&spa_namespace_lock); | |
7059 | ||
428870ff BB |
7060 | return (error); |
7061 | } | |
7062 | ||
c10d37dd GW |
7063 | static int |
7064 | spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7065 | list_t *vd_list) | |
619f0976 | 7066 | { |
c10d37dd GW |
7067 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7068 | ||
619f0976 GW |
7069 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); |
7070 | ||
7071 | /* Look up vdev and ensure it's a leaf. */ | |
7072 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7073 | if (vd == NULL || vd->vdev_detached) { | |
7074 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7075 | return (SET_ERROR(ENODEV)); |
7076 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7077 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7078 | return (SET_ERROR(EINVAL)); |
7079 | } else if (!vdev_writeable(vd)) { | |
7080 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
7081 | return (SET_ERROR(EROFS)); |
7082 | } | |
7083 | mutex_enter(&vd->vdev_initialize_lock); | |
7084 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7085 | ||
7086 | /* | |
7087 | * When we activate an initialize action we check to see | |
7088 | * if the vdev_initialize_thread is NULL. We do this instead | |
7089 | * of using the vdev_initialize_state since there might be | |
7090 | * a previous initialization process which has completed but | |
7091 | * the thread is not exited. | |
7092 | */ | |
1b939560 | 7093 | if (cmd_type == POOL_INITIALIZE_START && |
619f0976 GW |
7094 | (vd->vdev_initialize_thread != NULL || |
7095 | vd->vdev_top->vdev_removing)) { | |
7096 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7097 | return (SET_ERROR(EBUSY)); |
7098 | } else if (cmd_type == POOL_INITIALIZE_CANCEL && | |
7099 | (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && | |
7100 | vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { | |
7101 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7102 | return (SET_ERROR(ESRCH)); |
7103 | } else if (cmd_type == POOL_INITIALIZE_SUSPEND && | |
7104 | vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { | |
7105 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
7106 | return (SET_ERROR(ESRCH)); |
7107 | } | |
7108 | ||
7109 | switch (cmd_type) { | |
1b939560 | 7110 | case POOL_INITIALIZE_START: |
619f0976 GW |
7111 | vdev_initialize(vd); |
7112 | break; | |
7113 | case POOL_INITIALIZE_CANCEL: | |
c10d37dd | 7114 | vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); |
619f0976 GW |
7115 | break; |
7116 | case POOL_INITIALIZE_SUSPEND: | |
c10d37dd | 7117 | vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); |
619f0976 GW |
7118 | break; |
7119 | default: | |
7120 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7121 | } | |
7122 | mutex_exit(&vd->vdev_initialize_lock); | |
7123 | ||
c10d37dd GW |
7124 | return (0); |
7125 | } | |
7126 | ||
7127 | int | |
7128 | spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, | |
7129 | nvlist_t *vdev_errlist) | |
7130 | { | |
7131 | int total_errors = 0; | |
7132 | list_t vd_list; | |
7133 | ||
7134 | list_create(&vd_list, sizeof (vdev_t), | |
7135 | offsetof(vdev_t, vdev_initialize_node)); | |
7136 | ||
7137 | /* | |
7138 | * We hold the namespace lock through the whole function | |
7139 | * to prevent any changes to the pool while we're starting or | |
7140 | * stopping initialization. The config and state locks are held so that | |
7141 | * we can properly assess the vdev state before we commit to | |
7142 | * the initializing operation. | |
7143 | */ | |
7144 | mutex_enter(&spa_namespace_lock); | |
7145 | ||
7146 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7147 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7148 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7149 | ||
7150 | int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, | |
7151 | &vd_list); | |
7152 | if (error != 0) { | |
7153 | char guid_as_str[MAXNAMELEN]; | |
7154 | ||
7155 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7156 | "%llu", (unsigned long long)vdev_guid); | |
7157 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7158 | total_errors++; | |
7159 | } | |
7160 | } | |
7161 | ||
7162 | /* Wait for all initialize threads to stop. */ | |
7163 | vdev_initialize_stop_wait(spa, &vd_list); | |
7164 | ||
619f0976 GW |
7165 | /* Sync out the initializing state */ |
7166 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7167 | mutex_exit(&spa_namespace_lock); | |
7168 | ||
c10d37dd | 7169 | list_destroy(&vd_list); |
619f0976 | 7170 | |
c10d37dd GW |
7171 | return (total_errors); |
7172 | } | |
619f0976 | 7173 | |
1b939560 BB |
7174 | static int |
7175 | spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
7176 | uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list) | |
7177 | { | |
7178 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
7179 | ||
7180 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
7181 | ||
7182 | /* Look up vdev and ensure it's a leaf. */ | |
7183 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
7184 | if (vd == NULL || vd->vdev_detached) { | |
7185 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7186 | return (SET_ERROR(ENODEV)); | |
7187 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
7188 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7189 | return (SET_ERROR(EINVAL)); | |
7190 | } else if (!vdev_writeable(vd)) { | |
7191 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7192 | return (SET_ERROR(EROFS)); | |
7193 | } else if (!vd->vdev_has_trim) { | |
7194 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7195 | return (SET_ERROR(EOPNOTSUPP)); | |
7196 | } else if (secure && !vd->vdev_has_securetrim) { | |
7197 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7198 | return (SET_ERROR(EOPNOTSUPP)); | |
7199 | } | |
7200 | mutex_enter(&vd->vdev_trim_lock); | |
7201 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
7202 | ||
7203 | /* | |
7204 | * When we activate a TRIM action we check to see if the | |
7205 | * vdev_trim_thread is NULL. We do this instead of using the | |
7206 | * vdev_trim_state since there might be a previous TRIM process | |
7207 | * which has completed but the thread is not exited. | |
7208 | */ | |
7209 | if (cmd_type == POOL_TRIM_START && | |
7210 | (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing)) { | |
7211 | mutex_exit(&vd->vdev_trim_lock); | |
7212 | return (SET_ERROR(EBUSY)); | |
7213 | } else if (cmd_type == POOL_TRIM_CANCEL && | |
7214 | (vd->vdev_trim_state != VDEV_TRIM_ACTIVE && | |
7215 | vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) { | |
7216 | mutex_exit(&vd->vdev_trim_lock); | |
7217 | return (SET_ERROR(ESRCH)); | |
7218 | } else if (cmd_type == POOL_TRIM_SUSPEND && | |
7219 | vd->vdev_trim_state != VDEV_TRIM_ACTIVE) { | |
7220 | mutex_exit(&vd->vdev_trim_lock); | |
7221 | return (SET_ERROR(ESRCH)); | |
7222 | } | |
7223 | ||
7224 | switch (cmd_type) { | |
7225 | case POOL_TRIM_START: | |
7226 | vdev_trim(vd, rate, partial, secure); | |
7227 | break; | |
7228 | case POOL_TRIM_CANCEL: | |
7229 | vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list); | |
7230 | break; | |
7231 | case POOL_TRIM_SUSPEND: | |
7232 | vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list); | |
7233 | break; | |
7234 | default: | |
7235 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
7236 | } | |
7237 | mutex_exit(&vd->vdev_trim_lock); | |
7238 | ||
7239 | return (0); | |
7240 | } | |
7241 | ||
7242 | /* | |
7243 | * Initiates a manual TRIM for the requested vdevs. This kicks off individual | |
7244 | * TRIM threads for each child vdev. These threads pass over all of the free | |
7245 | * space in the vdev's metaslabs and issues TRIM commands for that space. | |
7246 | */ | |
7247 | int | |
7248 | spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate, | |
7249 | boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist) | |
7250 | { | |
7251 | int total_errors = 0; | |
7252 | list_t vd_list; | |
7253 | ||
7254 | list_create(&vd_list, sizeof (vdev_t), | |
7255 | offsetof(vdev_t, vdev_trim_node)); | |
7256 | ||
7257 | /* | |
7258 | * We hold the namespace lock through the whole function | |
7259 | * to prevent any changes to the pool while we're starting or | |
7260 | * stopping TRIM. The config and state locks are held so that | |
7261 | * we can properly assess the vdev state before we commit to | |
7262 | * the TRIM operation. | |
7263 | */ | |
7264 | mutex_enter(&spa_namespace_lock); | |
7265 | ||
7266 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
7267 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
7268 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
7269 | ||
7270 | int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type, | |
7271 | rate, partial, secure, &vd_list); | |
7272 | if (error != 0) { | |
7273 | char guid_as_str[MAXNAMELEN]; | |
7274 | ||
7275 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
7276 | "%llu", (unsigned long long)vdev_guid); | |
7277 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
7278 | total_errors++; | |
7279 | } | |
7280 | } | |
7281 | ||
7282 | /* Wait for all TRIM threads to stop. */ | |
7283 | vdev_trim_stop_wait(spa, &vd_list); | |
7284 | ||
7285 | /* Sync out the TRIM state */ | |
7286 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
7287 | mutex_exit(&spa_namespace_lock); | |
7288 | ||
7289 | list_destroy(&vd_list); | |
7290 | ||
7291 | return (total_errors); | |
7292 | } | |
7293 | ||
428870ff BB |
7294 | /* |
7295 | * Split a set of devices from their mirrors, and create a new pool from them. | |
7296 | */ | |
7297 | int | |
7298 | spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, | |
7299 | nvlist_t *props, boolean_t exp) | |
7300 | { | |
7301 | int error = 0; | |
7302 | uint64_t txg, *glist; | |
7303 | spa_t *newspa; | |
7304 | uint_t c, children, lastlog; | |
7305 | nvlist_t **child, *nvl, *tmp; | |
7306 | dmu_tx_t *tx; | |
7307 | char *altroot = NULL; | |
7308 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
7309 | boolean_t activate_slog; | |
7310 | ||
572e2857 | 7311 | ASSERT(spa_writeable(spa)); |
428870ff BB |
7312 | |
7313 | txg = spa_vdev_enter(spa); | |
7314 | ||
d2734cce SD |
7315 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
7316 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
7317 | error = (spa_has_checkpoint(spa)) ? | |
7318 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
7319 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7320 | } | |
7321 | ||
428870ff BB |
7322 | /* clear the log and flush everything up to now */ |
7323 | activate_slog = spa_passivate_log(spa); | |
7324 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
a1d477c2 | 7325 | error = spa_reset_logs(spa); |
428870ff BB |
7326 | txg = spa_vdev_config_enter(spa); |
7327 | ||
7328 | if (activate_slog) | |
7329 | spa_activate_log(spa); | |
7330 | ||
7331 | if (error != 0) | |
7332 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7333 | ||
7334 | /* check new spa name before going any further */ | |
7335 | if (spa_lookup(newname) != NULL) | |
7336 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
7337 | ||
7338 | /* | |
7339 | * scan through all the children to ensure they're all mirrors | |
7340 | */ | |
7341 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
7342 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
7343 | &children) != 0) | |
7344 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7345 | ||
7346 | /* first, check to ensure we've got the right child count */ | |
7347 | rvd = spa->spa_root_vdev; | |
7348 | lastlog = 0; | |
7349 | for (c = 0; c < rvd->vdev_children; c++) { | |
7350 | vdev_t *vd = rvd->vdev_child[c]; | |
7351 | ||
7352 | /* don't count the holes & logs as children */ | |
1b664952 GA |
7353 | if (vd->vdev_islog || (vd->vdev_ops != &vdev_indirect_ops && |
7354 | !vdev_is_concrete(vd))) { | |
428870ff BB |
7355 | if (lastlog == 0) |
7356 | lastlog = c; | |
7357 | continue; | |
7358 | } | |
7359 | ||
7360 | lastlog = 0; | |
7361 | } | |
7362 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
7363 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7364 | ||
7365 | /* next, ensure no spare or cache devices are part of the split */ | |
7366 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
7367 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
7368 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
7369 | ||
79c76d5b BB |
7370 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); |
7371 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
428870ff BB |
7372 | |
7373 | /* then, loop over each vdev and validate it */ | |
7374 | for (c = 0; c < children; c++) { | |
7375 | uint64_t is_hole = 0; | |
7376 | ||
7377 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
7378 | &is_hole); | |
7379 | ||
7380 | if (is_hole != 0) { | |
7381 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
7382 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
7383 | continue; | |
7384 | } else { | |
2e528b49 | 7385 | error = SET_ERROR(EINVAL); |
428870ff BB |
7386 | break; |
7387 | } | |
7388 | } | |
7389 | ||
1b664952 GA |
7390 | /* deal with indirect vdevs */ |
7391 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ops == | |
7392 | &vdev_indirect_ops) | |
7393 | continue; | |
7394 | ||
428870ff BB |
7395 | /* which disk is going to be split? */ |
7396 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
7397 | &glist[c]) != 0) { | |
2e528b49 | 7398 | error = SET_ERROR(EINVAL); |
428870ff BB |
7399 | break; |
7400 | } | |
7401 | ||
7402 | /* look it up in the spa */ | |
7403 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
7404 | if (vml[c] == NULL) { | |
2e528b49 | 7405 | error = SET_ERROR(ENODEV); |
428870ff BB |
7406 | break; |
7407 | } | |
7408 | ||
7409 | /* make sure there's nothing stopping the split */ | |
7410 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
7411 | vml[c]->vdev_islog || | |
a1d477c2 | 7412 | !vdev_is_concrete(vml[c]) || |
428870ff BB |
7413 | vml[c]->vdev_isspare || |
7414 | vml[c]->vdev_isl2cache || | |
7415 | !vdev_writeable(vml[c]) || | |
7416 | vml[c]->vdev_children != 0 || | |
7417 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
7418 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
2e528b49 | 7419 | error = SET_ERROR(EINVAL); |
428870ff BB |
7420 | break; |
7421 | } | |
7422 | ||
733b5722 RS |
7423 | if (vdev_dtl_required(vml[c]) || |
7424 | vdev_resilver_needed(vml[c], NULL, NULL)) { | |
2e528b49 | 7425 | error = SET_ERROR(EBUSY); |
428870ff BB |
7426 | break; |
7427 | } | |
7428 | ||
7429 | /* we need certain info from the top level */ | |
7430 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, | |
7431 | vml[c]->vdev_top->vdev_ms_array) == 0); | |
7432 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
7433 | vml[c]->vdev_top->vdev_ms_shift) == 0); | |
7434 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
7435 | vml[c]->vdev_top->vdev_asize) == 0); | |
7436 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
7437 | vml[c]->vdev_top->vdev_ashift) == 0); | |
e0ab3ab5 JS |
7438 | |
7439 | /* transfer per-vdev ZAPs */ | |
7440 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
7441 | VERIFY0(nvlist_add_uint64(child[c], | |
7442 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
7443 | ||
7444 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
7445 | VERIFY0(nvlist_add_uint64(child[c], | |
7446 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
7447 | vml[c]->vdev_parent->vdev_top_zap)); | |
428870ff BB |
7448 | } |
7449 | ||
7450 | if (error != 0) { | |
7451 | kmem_free(vml, children * sizeof (vdev_t *)); | |
7452 | kmem_free(glist, children * sizeof (uint64_t)); | |
7453 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
7454 | } | |
7455 | ||
7456 | /* stop writers from using the disks */ | |
7457 | for (c = 0; c < children; c++) { | |
7458 | if (vml[c] != NULL) | |
7459 | vml[c]->vdev_offline = B_TRUE; | |
7460 | } | |
7461 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
7462 | |
7463 | /* | |
428870ff BB |
7464 | * Temporarily record the splitting vdevs in the spa config. This |
7465 | * will disappear once the config is regenerated. | |
34dc7c2f | 7466 | */ |
79c76d5b | 7467 | VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
7468 | VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, |
7469 | glist, children) == 0); | |
7470 | kmem_free(glist, children * sizeof (uint64_t)); | |
34dc7c2f | 7471 | |
428870ff BB |
7472 | mutex_enter(&spa->spa_props_lock); |
7473 | VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, | |
7474 | nvl) == 0); | |
7475 | mutex_exit(&spa->spa_props_lock); | |
7476 | spa->spa_config_splitting = nvl; | |
7477 | vdev_config_dirty(spa->spa_root_vdev); | |
7478 | ||
7479 | /* configure and create the new pool */ | |
7480 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); | |
7481 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
7482 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); | |
7483 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
7484 | spa_version(spa)) == 0); | |
7485 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
7486 | spa->spa_config_txg) == 0); | |
7487 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
7488 | spa_generate_guid(NULL)) == 0); | |
e0ab3ab5 | 7489 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); |
428870ff BB |
7490 | (void) nvlist_lookup_string(props, |
7491 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 7492 | |
428870ff BB |
7493 | /* add the new pool to the namespace */ |
7494 | newspa = spa_add(newname, config, altroot); | |
e0ab3ab5 | 7495 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7496 | newspa->spa_config_txg = spa->spa_config_txg; |
7497 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
7498 | ||
7499 | /* release the spa config lock, retaining the namespace lock */ | |
7500 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
7501 | ||
7502 | if (zio_injection_enabled) | |
7503 | zio_handle_panic_injection(spa, FTAG, 1); | |
7504 | ||
7505 | spa_activate(newspa, spa_mode_global); | |
7506 | spa_async_suspend(newspa); | |
7507 | ||
c10d37dd | 7508 | /* |
1b939560 BB |
7509 | * Temporarily stop the initializing and TRIM activity. We set the |
7510 | * state to ACTIVE so that we know to resume initializing or TRIM | |
7511 | * once the split has completed. | |
c10d37dd | 7512 | */ |
1b939560 BB |
7513 | list_t vd_initialize_list; |
7514 | list_create(&vd_initialize_list, sizeof (vdev_t), | |
c10d37dd GW |
7515 | offsetof(vdev_t, vdev_initialize_node)); |
7516 | ||
1b939560 BB |
7517 | list_t vd_trim_list; |
7518 | list_create(&vd_trim_list, sizeof (vdev_t), | |
7519 | offsetof(vdev_t, vdev_trim_node)); | |
7520 | ||
619f0976 | 7521 | for (c = 0; c < children; c++) { |
1b664952 | 7522 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
619f0976 | 7523 | mutex_enter(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7524 | vdev_initialize_stop(vml[c], |
7525 | VDEV_INITIALIZE_ACTIVE, &vd_initialize_list); | |
619f0976 | 7526 | mutex_exit(&vml[c]->vdev_initialize_lock); |
1b939560 BB |
7527 | |
7528 | mutex_enter(&vml[c]->vdev_trim_lock); | |
7529 | vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list); | |
7530 | mutex_exit(&vml[c]->vdev_trim_lock); | |
619f0976 GW |
7531 | } |
7532 | } | |
1b939560 BB |
7533 | |
7534 | vdev_initialize_stop_wait(spa, &vd_initialize_list); | |
7535 | vdev_trim_stop_wait(spa, &vd_trim_list); | |
7536 | ||
7537 | list_destroy(&vd_initialize_list); | |
7538 | list_destroy(&vd_trim_list); | |
619f0976 | 7539 | |
6cb8e530 | 7540 | newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; |
8b27e08e | 7541 | newspa->spa_is_splitting = B_TRUE; |
6cb8e530 | 7542 | |
428870ff | 7543 | /* create the new pool from the disks of the original pool */ |
6cb8e530 | 7544 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); |
428870ff BB |
7545 | if (error) |
7546 | goto out; | |
7547 | ||
7548 | /* if that worked, generate a real config for the new pool */ | |
7549 | if (newspa->spa_root_vdev != NULL) { | |
7550 | VERIFY(nvlist_alloc(&newspa->spa_config_splitting, | |
79c76d5b | 7551 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
7552 | VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, |
7553 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); | |
7554 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, | |
7555 | B_TRUE)); | |
9babb374 | 7556 | } |
34dc7c2f | 7557 | |
428870ff BB |
7558 | /* set the props */ |
7559 | if (props != NULL) { | |
7560 | spa_configfile_set(newspa, props, B_FALSE); | |
7561 | error = spa_prop_set(newspa, props); | |
7562 | if (error) | |
7563 | goto out; | |
7564 | } | |
34dc7c2f | 7565 | |
428870ff BB |
7566 | /* flush everything */ |
7567 | txg = spa_vdev_config_enter(newspa); | |
7568 | vdev_config_dirty(newspa->spa_root_vdev); | |
7569 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 7570 | |
428870ff BB |
7571 | if (zio_injection_enabled) |
7572 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 7573 | |
428870ff | 7574 | spa_async_resume(newspa); |
34dc7c2f | 7575 | |
428870ff BB |
7576 | /* finally, update the original pool's config */ |
7577 | txg = spa_vdev_config_enter(spa); | |
7578 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
7579 | error = dmu_tx_assign(tx, TXG_WAIT); | |
7580 | if (error != 0) | |
7581 | dmu_tx_abort(tx); | |
7582 | for (c = 0; c < children; c++) { | |
1b664952 | 7583 | if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) { |
234234ca RS |
7584 | vdev_t *tvd = vml[c]->vdev_top; |
7585 | ||
7586 | /* | |
7587 | * Need to be sure the detachable VDEV is not | |
7588 | * on any *other* txg's DTL list to prevent it | |
7589 | * from being accessed after it's freed. | |
7590 | */ | |
7591 | for (int t = 0; t < TXG_SIZE; t++) { | |
7592 | (void) txg_list_remove_this( | |
7593 | &tvd->vdev_dtl_list, vml[c], t); | |
7594 | } | |
7595 | ||
428870ff BB |
7596 | vdev_split(vml[c]); |
7597 | if (error == 0) | |
6f1ffb06 MA |
7598 | spa_history_log_internal(spa, "detach", tx, |
7599 | "vdev=%s", vml[c]->vdev_path); | |
e0ab3ab5 | 7600 | |
428870ff | 7601 | vdev_free(vml[c]); |
34dc7c2f | 7602 | } |
34dc7c2f | 7603 | } |
e0ab3ab5 | 7604 | spa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
7605 | vdev_config_dirty(spa->spa_root_vdev); |
7606 | spa->spa_config_splitting = NULL; | |
7607 | nvlist_free(nvl); | |
7608 | if (error == 0) | |
7609 | dmu_tx_commit(tx); | |
7610 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
7611 | ||
7612 | if (zio_injection_enabled) | |
7613 | zio_handle_panic_injection(spa, FTAG, 3); | |
7614 | ||
7615 | /* split is complete; log a history record */ | |
6f1ffb06 MA |
7616 | spa_history_log_internal(newspa, "split", NULL, |
7617 | "from pool %s", spa_name(spa)); | |
428870ff | 7618 | |
8b27e08e | 7619 | newspa->spa_is_splitting = B_FALSE; |
428870ff BB |
7620 | kmem_free(vml, children * sizeof (vdev_t *)); |
7621 | ||
7622 | /* if we're not going to mount the filesystems in userland, export */ | |
7623 | if (exp) | |
7624 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
7625 | B_FALSE, B_FALSE); | |
7626 | ||
7627 | return (error); | |
7628 | ||
7629 | out: | |
7630 | spa_unload(newspa); | |
7631 | spa_deactivate(newspa); | |
7632 | spa_remove(newspa); | |
7633 | ||
7634 | txg = spa_vdev_config_enter(spa); | |
7635 | ||
7636 | /* re-online all offlined disks */ | |
7637 | for (c = 0; c < children; c++) { | |
7638 | if (vml[c] != NULL) | |
7639 | vml[c]->vdev_offline = B_FALSE; | |
7640 | } | |
619f0976 | 7641 | |
1b939560 | 7642 | /* restart initializing or trimming disks as necessary */ |
619f0976 | 7643 | spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); |
1b939560 BB |
7644 | spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); |
7645 | spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); | |
619f0976 | 7646 | |
428870ff BB |
7647 | vdev_reopen(spa->spa_root_vdev); |
7648 | ||
7649 | nvlist_free(spa->spa_config_splitting); | |
7650 | spa->spa_config_splitting = NULL; | |
7651 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 7652 | |
428870ff | 7653 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
7654 | return (error); |
7655 | } | |
7656 | ||
34dc7c2f BB |
7657 | /* |
7658 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
d3cc8b15 | 7659 | * currently spared, so we can detach it. |
34dc7c2f BB |
7660 | */ |
7661 | static vdev_t * | |
7662 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
7663 | { | |
7664 | vdev_t *newvd, *oldvd; | |
34dc7c2f | 7665 | |
1c27024e | 7666 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
7667 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
7668 | if (oldvd != NULL) | |
7669 | return (oldvd); | |
7670 | } | |
7671 | ||
7672 | /* | |
572e2857 BB |
7673 | * Check for a completed replacement. We always consider the first |
7674 | * vdev in the list to be the oldest vdev, and the last one to be | |
7675 | * the newest (see spa_vdev_attach() for how that works). In | |
7676 | * the case where the newest vdev is faulted, we will not automatically | |
7677 | * remove it after a resilver completes. This is OK as it will require | |
7678 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 7679 | */ |
572e2857 BB |
7680 | if (vd->vdev_ops == &vdev_replacing_ops) { |
7681 | ASSERT(vd->vdev_children > 1); | |
7682 | ||
7683 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 7684 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 7685 | |
fb5f0bc8 | 7686 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7687 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 7688 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7689 | return (oldvd); |
34dc7c2f BB |
7690 | } |
7691 | ||
7692 | /* | |
7693 | * Check for a completed resilver with the 'unspare' flag set. | |
f65fbee1 | 7694 | * Also potentially update faulted state. |
34dc7c2f | 7695 | */ |
572e2857 BB |
7696 | if (vd->vdev_ops == &vdev_spare_ops) { |
7697 | vdev_t *first = vd->vdev_child[0]; | |
7698 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
7699 | ||
7700 | if (last->vdev_unspare) { | |
7701 | oldvd = first; | |
7702 | newvd = last; | |
7703 | } else if (first->vdev_unspare) { | |
7704 | oldvd = last; | |
7705 | newvd = first; | |
7706 | } else { | |
7707 | oldvd = NULL; | |
7708 | } | |
34dc7c2f | 7709 | |
572e2857 | 7710 | if (oldvd != NULL && |
fb5f0bc8 | 7711 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 7712 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 7713 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 7714 | return (oldvd); |
572e2857 | 7715 | |
f65fbee1 JJ |
7716 | vdev_propagate_state(vd); |
7717 | ||
572e2857 BB |
7718 | /* |
7719 | * If there are more than two spares attached to a disk, | |
7720 | * and those spares are not required, then we want to | |
7721 | * attempt to free them up now so that they can be used | |
7722 | * by other pools. Once we're back down to a single | |
7723 | * disk+spare, we stop removing them. | |
7724 | */ | |
7725 | if (vd->vdev_children > 2) { | |
7726 | newvd = vd->vdev_child[1]; | |
7727 | ||
7728 | if (newvd->vdev_isspare && last->vdev_isspare && | |
7729 | vdev_dtl_empty(last, DTL_MISSING) && | |
7730 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
7731 | !vdev_dtl_required(newvd)) | |
7732 | return (newvd); | |
34dc7c2f | 7733 | } |
34dc7c2f BB |
7734 | } |
7735 | ||
7736 | return (NULL); | |
7737 | } | |
7738 | ||
7739 | static void | |
7740 | spa_vdev_resilver_done(spa_t *spa) | |
7741 | { | |
fb5f0bc8 BB |
7742 | vdev_t *vd, *pvd, *ppvd; |
7743 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 7744 | |
fb5f0bc8 | 7745 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7746 | |
7747 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
7748 | pvd = vd->vdev_parent; |
7749 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 7750 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
7751 | pguid = pvd->vdev_guid; |
7752 | ppguid = ppvd->vdev_guid; | |
7753 | sguid = 0; | |
34dc7c2f BB |
7754 | /* |
7755 | * If we have just finished replacing a hot spared device, then | |
7756 | * we need to detach the parent's first child (the original hot | |
7757 | * spare) as well. | |
7758 | */ | |
572e2857 BB |
7759 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
7760 | ppvd->vdev_children == 2) { | |
34dc7c2f | 7761 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 7762 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 7763 | } |
5d1f7fb6 GW |
7764 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); |
7765 | ||
fb5f0bc8 BB |
7766 | spa_config_exit(spa, SCL_ALL, FTAG); |
7767 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 7768 | return; |
fb5f0bc8 | 7769 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 7770 | return; |
fb5f0bc8 | 7771 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7772 | } |
7773 | ||
fb5f0bc8 | 7774 | spa_config_exit(spa, SCL_ALL, FTAG); |
9a49d3f3 BB |
7775 | |
7776 | /* | |
7777 | * If a detach was not performed above replace waiters will not have | |
7778 | * been notified. In which case we must do so now. | |
7779 | */ | |
7780 | spa_notify_waiters(spa); | |
34dc7c2f BB |
7781 | } |
7782 | ||
7783 | /* | |
428870ff | 7784 | * Update the stored path or FRU for this vdev. |
34dc7c2f | 7785 | */ |
65c7cc49 | 7786 | static int |
9babb374 BB |
7787 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
7788 | boolean_t ispath) | |
34dc7c2f | 7789 | { |
b128c09f | 7790 | vdev_t *vd; |
428870ff | 7791 | boolean_t sync = B_FALSE; |
34dc7c2f | 7792 | |
572e2857 BB |
7793 | ASSERT(spa_writeable(spa)); |
7794 | ||
428870ff | 7795 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 7796 | |
9babb374 | 7797 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 7798 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
7799 | |
7800 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 7801 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 7802 | |
9babb374 | 7803 | if (ispath) { |
428870ff BB |
7804 | if (strcmp(value, vd->vdev_path) != 0) { |
7805 | spa_strfree(vd->vdev_path); | |
7806 | vd->vdev_path = spa_strdup(value); | |
7807 | sync = B_TRUE; | |
7808 | } | |
9babb374 | 7809 | } else { |
428870ff BB |
7810 | if (vd->vdev_fru == NULL) { |
7811 | vd->vdev_fru = spa_strdup(value); | |
7812 | sync = B_TRUE; | |
7813 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 7814 | spa_strfree(vd->vdev_fru); |
428870ff BB |
7815 | vd->vdev_fru = spa_strdup(value); |
7816 | sync = B_TRUE; | |
7817 | } | |
9babb374 | 7818 | } |
34dc7c2f | 7819 | |
428870ff | 7820 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
7821 | } |
7822 | ||
9babb374 BB |
7823 | int |
7824 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
7825 | { | |
7826 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
7827 | } | |
7828 | ||
7829 | int | |
7830 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
7831 | { | |
7832 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
7833 | } | |
7834 | ||
34dc7c2f BB |
7835 | /* |
7836 | * ========================================================================== | |
428870ff | 7837 | * SPA Scanning |
34dc7c2f BB |
7838 | * ========================================================================== |
7839 | */ | |
0ea05c64 AP |
7840 | int |
7841 | spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) | |
7842 | { | |
7843 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7844 | ||
7845 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
7846 | return (SET_ERROR(EBUSY)); | |
7847 | ||
7848 | return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); | |
7849 | } | |
34dc7c2f | 7850 | |
34dc7c2f | 7851 | int |
428870ff BB |
7852 | spa_scan_stop(spa_t *spa) |
7853 | { | |
7854 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7855 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
2e528b49 | 7856 | return (SET_ERROR(EBUSY)); |
428870ff BB |
7857 | return (dsl_scan_cancel(spa->spa_dsl_pool)); |
7858 | } | |
7859 | ||
7860 | int | |
7861 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 7862 | { |
b128c09f | 7863 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 7864 | |
428870ff | 7865 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
2e528b49 | 7866 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 7867 | |
fa241660 TC |
7868 | if (func == POOL_SCAN_RESILVER && |
7869 | !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
7870 | return (SET_ERROR(ENOTSUP)); | |
7871 | ||
34dc7c2f | 7872 | /* |
b128c09f BB |
7873 | * If a resilver was requested, but there is no DTL on a |
7874 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 7875 | */ |
428870ff | 7876 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
7877 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
7878 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
7879 | return (0); |
7880 | } | |
7881 | ||
428870ff | 7882 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
7883 | } |
7884 | ||
7885 | /* | |
7886 | * ========================================================================== | |
7887 | * SPA async task processing | |
7888 | * ========================================================================== | |
7889 | */ | |
7890 | ||
7891 | static void | |
7892 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
7893 | { | |
b128c09f | 7894 | if (vd->vdev_remove_wanted) { |
428870ff BB |
7895 | vd->vdev_remove_wanted = B_FALSE; |
7896 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 7897 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
7898 | |
7899 | /* | |
7900 | * We want to clear the stats, but we don't want to do a full | |
7901 | * vdev_clear() as that will cause us to throw away | |
7902 | * degraded/faulted state as well as attempt to reopen the | |
7903 | * device, all of which is a waste. | |
7904 | */ | |
7905 | vd->vdev_stat.vs_read_errors = 0; | |
7906 | vd->vdev_stat.vs_write_errors = 0; | |
7907 | vd->vdev_stat.vs_checksum_errors = 0; | |
7908 | ||
b128c09f BB |
7909 | vdev_state_dirty(vd->vdev_top); |
7910 | } | |
34dc7c2f | 7911 | |
1c27024e | 7912 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
7913 | spa_async_remove(spa, vd->vdev_child[c]); |
7914 | } | |
7915 | ||
7916 | static void | |
7917 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
7918 | { | |
7919 | if (vd->vdev_probe_wanted) { | |
428870ff | 7920 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 7921 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 7922 | } |
b128c09f | 7923 | |
1c27024e | 7924 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 7925 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
7926 | } |
7927 | ||
9babb374 BB |
7928 | static void |
7929 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
7930 | { | |
9babb374 BB |
7931 | if (!spa->spa_autoexpand) |
7932 | return; | |
7933 | ||
1c27024e | 7934 | for (int c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
7935 | vdev_t *cvd = vd->vdev_child[c]; |
7936 | spa_async_autoexpand(spa, cvd); | |
7937 | } | |
7938 | ||
7939 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
7940 | return; | |
7941 | ||
12fa0466 | 7942 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND); |
9babb374 BB |
7943 | } |
7944 | ||
34dc7c2f | 7945 | static void |
c25b8f99 | 7946 | spa_async_thread(void *arg) |
34dc7c2f | 7947 | { |
c25b8f99 | 7948 | spa_t *spa = (spa_t *)arg; |
80a91e74 | 7949 | dsl_pool_t *dp = spa->spa_dsl_pool; |
867959b5 | 7950 | int tasks; |
34dc7c2f BB |
7951 | |
7952 | ASSERT(spa->spa_sync_on); | |
7953 | ||
7954 | mutex_enter(&spa->spa_async_lock); | |
7955 | tasks = spa->spa_async_tasks; | |
7956 | spa->spa_async_tasks = 0; | |
7957 | mutex_exit(&spa->spa_async_lock); | |
7958 | ||
7959 | /* | |
7960 | * See if the config needs to be updated. | |
7961 | */ | |
7962 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 7963 | uint64_t old_space, new_space; |
9babb374 | 7964 | |
34dc7c2f | 7965 | mutex_enter(&spa_namespace_lock); |
428870ff | 7966 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7967 | old_space += metaslab_class_get_space(spa_special_class(spa)); |
7968 | old_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
7969 | ||
34dc7c2f | 7970 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
cc99f275 | 7971 | |
428870ff | 7972 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7973 | new_space += metaslab_class_get_space(spa_special_class(spa)); |
7974 | new_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
34dc7c2f | 7975 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
7976 | |
7977 | /* | |
7978 | * If the pool grew as a result of the config update, | |
7979 | * then log an internal history event. | |
7980 | */ | |
428870ff | 7981 | if (new_space != old_space) { |
6f1ffb06 | 7982 | spa_history_log_internal(spa, "vdev online", NULL, |
45d1cae3 | 7983 | "pool '%s' size: %llu(+%llu)", |
74756182 MM |
7984 | spa_name(spa), (u_longlong_t)new_space, |
7985 | (u_longlong_t)(new_space - old_space)); | |
9babb374 | 7986 | } |
34dc7c2f BB |
7987 | } |
7988 | ||
7989 | /* | |
7990 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 7991 | */ |
b128c09f | 7992 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 7993 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 7994 | spa_async_remove(spa, spa->spa_root_vdev); |
867959b5 | 7995 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 7996 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
867959b5 | 7997 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
7998 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
7999 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
8000 | } |
8001 | ||
9babb374 BB |
8002 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
8003 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8004 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
8005 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8006 | } | |
8007 | ||
34dc7c2f | 8008 | /* |
b128c09f | 8009 | * See if any devices need to be probed. |
34dc7c2f | 8010 | */ |
b128c09f | 8011 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 8012 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
8013 | spa_async_probe(spa, spa->spa_root_vdev); |
8014 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
8015 | } | |
34dc7c2f BB |
8016 | |
8017 | /* | |
b128c09f | 8018 | * If any devices are done replacing, detach them. |
34dc7c2f | 8019 | */ |
b128c09f BB |
8020 | if (tasks & SPA_ASYNC_RESILVER_DONE) |
8021 | spa_vdev_resilver_done(spa); | |
34dc7c2f | 8022 | |
9a49d3f3 BB |
8023 | /* |
8024 | * If any devices are done replacing, detach them. Then if no | |
8025 | * top-level vdevs are rebuilding attempt to kick off a scrub. | |
8026 | */ | |
8027 | if (tasks & SPA_ASYNC_REBUILD_DONE) { | |
8028 | spa_vdev_resilver_done(spa); | |
8029 | ||
8030 | if (!vdev_rebuild_active(spa->spa_root_vdev)) | |
8031 | (void) dsl_scan(spa->spa_dsl_pool, POOL_SCAN_SCRUB); | |
8032 | } | |
8033 | ||
34dc7c2f BB |
8034 | /* |
8035 | * Kick off a resilver. | |
8036 | */ | |
80a91e74 | 8037 | if (tasks & SPA_ASYNC_RESILVER && |
9a49d3f3 | 8038 | !vdev_rebuild_active(spa->spa_root_vdev) && |
80a91e74 TC |
8039 | (!dsl_scan_resilvering(dp) || |
8040 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) | |
3c819a2c | 8041 | dsl_scan_restart_resilver(dp, 0); |
34dc7c2f | 8042 | |
619f0976 GW |
8043 | if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { |
8044 | mutex_enter(&spa_namespace_lock); | |
8045 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8046 | vdev_initialize_restart(spa->spa_root_vdev); | |
8047 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8048 | mutex_exit(&spa_namespace_lock); | |
8049 | } | |
8050 | ||
1b939560 BB |
8051 | if (tasks & SPA_ASYNC_TRIM_RESTART) { |
8052 | mutex_enter(&spa_namespace_lock); | |
8053 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8054 | vdev_trim_restart(spa->spa_root_vdev); | |
8055 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8056 | mutex_exit(&spa_namespace_lock); | |
8057 | } | |
8058 | ||
8059 | if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) { | |
8060 | mutex_enter(&spa_namespace_lock); | |
8061 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8062 | vdev_autotrim_restart(spa); | |
8063 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8064 | mutex_exit(&spa_namespace_lock); | |
8065 | } | |
8066 | ||
b7654bd7 GA |
8067 | /* |
8068 | * Kick off L2 cache whole device TRIM. | |
8069 | */ | |
8070 | if (tasks & SPA_ASYNC_L2CACHE_TRIM) { | |
8071 | mutex_enter(&spa_namespace_lock); | |
8072 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8073 | vdev_trim_l2arc(spa); | |
8074 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
8075 | mutex_exit(&spa_namespace_lock); | |
8076 | } | |
8077 | ||
77f6826b GA |
8078 | /* |
8079 | * Kick off L2 cache rebuilding. | |
8080 | */ | |
8081 | if (tasks & SPA_ASYNC_L2CACHE_REBUILD) { | |
8082 | mutex_enter(&spa_namespace_lock); | |
8083 | spa_config_enter(spa, SCL_L2ARC, FTAG, RW_READER); | |
8084 | l2arc_spa_rebuild_start(spa); | |
8085 | spa_config_exit(spa, SCL_L2ARC, FTAG); | |
8086 | mutex_exit(&spa_namespace_lock); | |
8087 | } | |
8088 | ||
34dc7c2f BB |
8089 | /* |
8090 | * Let the world know that we're done. | |
8091 | */ | |
8092 | mutex_enter(&spa->spa_async_lock); | |
8093 | spa->spa_async_thread = NULL; | |
8094 | cv_broadcast(&spa->spa_async_cv); | |
8095 | mutex_exit(&spa->spa_async_lock); | |
8096 | thread_exit(); | |
8097 | } | |
8098 | ||
8099 | void | |
8100 | spa_async_suspend(spa_t *spa) | |
8101 | { | |
8102 | mutex_enter(&spa->spa_async_lock); | |
8103 | spa->spa_async_suspended++; | |
9d5b5245 | 8104 | while (spa->spa_async_thread != NULL) |
34dc7c2f BB |
8105 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); |
8106 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 MA |
8107 | |
8108 | spa_vdev_remove_suspend(spa); | |
9d5b5245 SD |
8109 | |
8110 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a SD |
8111 | if (condense_thread != NULL) |
8112 | zthr_cancel(condense_thread); | |
d2734cce SD |
8113 | |
8114 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a SD |
8115 | if (discard_thread != NULL) |
8116 | zthr_cancel(discard_thread); | |
37f03da8 SH |
8117 | |
8118 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8119 | if (ll_delete_thread != NULL) | |
8120 | zthr_cancel(ll_delete_thread); | |
8121 | ||
8122 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8123 | if (ll_condense_thread != NULL) | |
8124 | zthr_cancel(ll_condense_thread); | |
34dc7c2f BB |
8125 | } |
8126 | ||
8127 | void | |
8128 | spa_async_resume(spa_t *spa) | |
8129 | { | |
8130 | mutex_enter(&spa->spa_async_lock); | |
8131 | ASSERT(spa->spa_async_suspended != 0); | |
8132 | spa->spa_async_suspended--; | |
8133 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 | 8134 | spa_restart_removal(spa); |
9d5b5245 SD |
8135 | |
8136 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a | 8137 | if (condense_thread != NULL) |
9d5b5245 | 8138 | zthr_resume(condense_thread); |
d2734cce SD |
8139 | |
8140 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a | 8141 | if (discard_thread != NULL) |
d2734cce | 8142 | zthr_resume(discard_thread); |
37f03da8 SH |
8143 | |
8144 | zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr; | |
8145 | if (ll_delete_thread != NULL) | |
8146 | zthr_resume(ll_delete_thread); | |
8147 | ||
8148 | zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; | |
8149 | if (ll_condense_thread != NULL) | |
8150 | zthr_resume(ll_condense_thread); | |
34dc7c2f BB |
8151 | } |
8152 | ||
e6cfd633 WA |
8153 | static boolean_t |
8154 | spa_async_tasks_pending(spa_t *spa) | |
8155 | { | |
8156 | uint_t non_config_tasks; | |
8157 | uint_t config_task; | |
8158 | boolean_t config_task_suspended; | |
8159 | ||
8160 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
8161 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
8162 | if (spa->spa_ccw_fail_time == 0) { | |
8163 | config_task_suspended = B_FALSE; | |
8164 | } else { | |
8165 | config_task_suspended = | |
8166 | (gethrtime() - spa->spa_ccw_fail_time) < | |
05852b34 | 8167 | ((hrtime_t)zfs_ccw_retry_interval * NANOSEC); |
e6cfd633 WA |
8168 | } |
8169 | ||
8170 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
8171 | } | |
8172 | ||
34dc7c2f BB |
8173 | static void |
8174 | spa_async_dispatch(spa_t *spa) | |
8175 | { | |
8176 | mutex_enter(&spa->spa_async_lock); | |
e6cfd633 WA |
8177 | if (spa_async_tasks_pending(spa) && |
8178 | !spa->spa_async_suspended && | |
da92d5cb | 8179 | spa->spa_async_thread == NULL) |
34dc7c2f BB |
8180 | spa->spa_async_thread = thread_create(NULL, 0, |
8181 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
8182 | mutex_exit(&spa->spa_async_lock); | |
8183 | } | |
8184 | ||
8185 | void | |
8186 | spa_async_request(spa_t *spa, int task) | |
8187 | { | |
428870ff | 8188 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
8189 | mutex_enter(&spa->spa_async_lock); |
8190 | spa->spa_async_tasks |= task; | |
8191 | mutex_exit(&spa->spa_async_lock); | |
8192 | } | |
8193 | ||
3c819a2c JP |
8194 | int |
8195 | spa_async_tasks(spa_t *spa) | |
8196 | { | |
8197 | return (spa->spa_async_tasks); | |
8198 | } | |
8199 | ||
34dc7c2f BB |
8200 | /* |
8201 | * ========================================================================== | |
8202 | * SPA syncing routines | |
8203 | * ========================================================================== | |
8204 | */ | |
8205 | ||
37f03da8 | 8206 | |
428870ff | 8207 | static int |
37f03da8 SH |
8208 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, |
8209 | dmu_tx_t *tx) | |
34dc7c2f | 8210 | { |
428870ff | 8211 | bpobj_t *bpo = arg; |
37f03da8 | 8212 | bpobj_enqueue(bpo, bp, bp_freed, tx); |
428870ff BB |
8213 | return (0); |
8214 | } | |
34dc7c2f | 8215 | |
37f03da8 SH |
8216 | int |
8217 | bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8218 | { | |
8219 | return (bpobj_enqueue_cb(arg, bp, B_FALSE, tx)); | |
8220 | } | |
8221 | ||
8222 | int | |
8223 | bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8224 | { | |
8225 | return (bpobj_enqueue_cb(arg, bp, B_TRUE, tx)); | |
8226 | } | |
8227 | ||
428870ff BB |
8228 | static int |
8229 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
8230 | { | |
9cdf7b1f | 8231 | zio_t *pio = arg; |
34dc7c2f | 8232 | |
9cdf7b1f MA |
8233 | zio_nowait(zio_free_sync(pio, pio->io_spa, dmu_tx_get_txg(tx), bp, |
8234 | pio->io_flags)); | |
428870ff | 8235 | return (0); |
34dc7c2f BB |
8236 | } |
8237 | ||
37f03da8 SH |
8238 | static int |
8239 | bpobj_spa_free_sync_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, | |
8240 | dmu_tx_t *tx) | |
8241 | { | |
8242 | ASSERT(!bp_freed); | |
8243 | return (spa_free_sync_cb(arg, bp, tx)); | |
8244 | } | |
8245 | ||
e8b96c60 MA |
8246 | /* |
8247 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8248 | * amount of time spent syncing frees. | |
8249 | */ | |
8250 | static void | |
8251 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
8252 | { | |
8253 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
8254 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
8255 | VERIFY(zio_wait(zio) == 0); | |
8256 | } | |
8257 | ||
8258 | /* | |
8259 | * Note: this simple function is not inlined to make it easier to dtrace the | |
8260 | * amount of time spent syncing deferred frees. | |
8261 | */ | |
8262 | static void | |
8263 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
8264 | { | |
8dc2197b SD |
8265 | if (spa_sync_pass(spa) != 1) |
8266 | return; | |
8267 | ||
93e28d66 SD |
8268 | /* |
8269 | * Note: | |
8270 | * If the log space map feature is active, we stop deferring | |
8271 | * frees to the next TXG and therefore running this function | |
8272 | * would be considered a no-op as spa_deferred_bpobj should | |
8273 | * not have any entries. | |
8274 | * | |
8275 | * That said we run this function anyway (instead of returning | |
8276 | * immediately) for the edge-case scenario where we just | |
8277 | * activated the log space map feature in this TXG but we have | |
8278 | * deferred frees from the previous TXG. | |
8279 | */ | |
e8b96c60 MA |
8280 | zio_t *zio = zio_root(spa, NULL, NULL, 0); |
8281 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
37f03da8 | 8282 | bpobj_spa_free_sync_cb, zio, tx), ==, 0); |
e8b96c60 MA |
8283 | VERIFY0(zio_wait(zio)); |
8284 | } | |
8285 | ||
34dc7c2f BB |
8286 | static void |
8287 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
8288 | { | |
8289 | char *packed = NULL; | |
b128c09f | 8290 | size_t bufsize; |
34dc7c2f BB |
8291 | size_t nvsize = 0; |
8292 | dmu_buf_t *db; | |
8293 | ||
8294 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
8295 | ||
b128c09f BB |
8296 | /* |
8297 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
b0bc7a84 | 8298 | * information. This avoids the dmu_buf_will_dirty() path and |
b128c09f BB |
8299 | * saves us a pre-read to get data we don't actually care about. |
8300 | */ | |
9ae529ec | 8301 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
79c76d5b | 8302 | packed = vmem_alloc(bufsize, KM_SLEEP); |
34dc7c2f BB |
8303 | |
8304 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
79c76d5b | 8305 | KM_SLEEP) == 0); |
b128c09f | 8306 | bzero(packed + nvsize, bufsize - nvsize); |
34dc7c2f | 8307 | |
b128c09f | 8308 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 8309 | |
00b46022 | 8310 | vmem_free(packed, bufsize); |
34dc7c2f BB |
8311 | |
8312 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
8313 | dmu_buf_will_dirty(db, tx); | |
8314 | *(uint64_t *)db->db_data = nvsize; | |
8315 | dmu_buf_rele(db, FTAG); | |
8316 | } | |
8317 | ||
8318 | static void | |
8319 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
8320 | const char *config, const char *entry) | |
8321 | { | |
8322 | nvlist_t *nvroot; | |
8323 | nvlist_t **list; | |
8324 | int i; | |
8325 | ||
8326 | if (!sav->sav_sync) | |
8327 | return; | |
8328 | ||
8329 | /* | |
8330 | * Update the MOS nvlist describing the list of available devices. | |
8331 | * spa_validate_aux() will have already made sure this nvlist is | |
8332 | * valid and the vdevs are labeled appropriately. | |
8333 | */ | |
8334 | if (sav->sav_object == 0) { | |
8335 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
8336 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
8337 | sizeof (uint64_t), tx); | |
8338 | VERIFY(zap_update(spa->spa_meta_objset, | |
8339 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
8340 | &sav->sav_object, tx) == 0); | |
8341 | } | |
8342 | ||
79c76d5b | 8343 | VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
8344 | if (sav->sav_count == 0) { |
8345 | VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); | |
8346 | } else { | |
79c76d5b | 8347 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); |
34dc7c2f BB |
8348 | for (i = 0; i < sav->sav_count; i++) |
8349 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 8350 | B_FALSE, VDEV_CONFIG_L2CACHE); |
34dc7c2f BB |
8351 | VERIFY(nvlist_add_nvlist_array(nvroot, config, list, |
8352 | sav->sav_count) == 0); | |
8353 | for (i = 0; i < sav->sav_count; i++) | |
8354 | nvlist_free(list[i]); | |
8355 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
8356 | } | |
8357 | ||
8358 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
8359 | nvlist_free(nvroot); | |
8360 | ||
8361 | sav->sav_sync = B_FALSE; | |
8362 | } | |
8363 | ||
e0ab3ab5 JS |
8364 | /* |
8365 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
8366 | * The all-vdev ZAP must be empty. | |
8367 | */ | |
8368 | static void | |
8369 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
8370 | { | |
8371 | spa_t *spa = vd->vdev_spa; | |
e0ab3ab5 JS |
8372 | |
8373 | if (vd->vdev_top_zap != 0) { | |
8374 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8375 | vd->vdev_top_zap, tx)); | |
8376 | } | |
8377 | if (vd->vdev_leaf_zap != 0) { | |
8378 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
8379 | vd->vdev_leaf_zap, tx)); | |
8380 | } | |
1c27024e | 8381 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
8382 | spa_avz_build(vd->vdev_child[i], avz, tx); |
8383 | } | |
8384 | } | |
8385 | ||
34dc7c2f BB |
8386 | static void |
8387 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
8388 | { | |
8389 | nvlist_t *config; | |
8390 | ||
e0ab3ab5 JS |
8391 | /* |
8392 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
8393 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
8394 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
8395 | * need to rebuild the AVZ although the config may not be dirty. | |
8396 | */ | |
8397 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
8398 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
34dc7c2f BB |
8399 | return; |
8400 | ||
b128c09f BB |
8401 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
8402 | ||
e0ab3ab5 | 8403 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || |
38640550 | 8404 | spa->spa_avz_action == AVZ_ACTION_INITIALIZE || |
e0ab3ab5 JS |
8405 | spa->spa_all_vdev_zaps != 0); |
8406 | ||
8407 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
e0ab3ab5 JS |
8408 | /* Make and build the new AVZ */ |
8409 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
8410 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
8411 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
8412 | ||
8413 | /* Diff old AVZ with new one */ | |
1c27024e DB |
8414 | zap_cursor_t zc; |
8415 | zap_attribute_t za; | |
8416 | ||
e0ab3ab5 JS |
8417 | for (zap_cursor_init(&zc, spa->spa_meta_objset, |
8418 | spa->spa_all_vdev_zaps); | |
8419 | zap_cursor_retrieve(&zc, &za) == 0; | |
8420 | zap_cursor_advance(&zc)) { | |
8421 | uint64_t vdzap = za.za_first_integer; | |
8422 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
8423 | vdzap) == ENOENT) { | |
8424 | /* | |
8425 | * ZAP is listed in old AVZ but not in new one; | |
8426 | * destroy it | |
8427 | */ | |
8428 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
8429 | tx)); | |
8430 | } | |
8431 | } | |
8432 | ||
8433 | zap_cursor_fini(&zc); | |
8434 | ||
8435 | /* Destroy the old AVZ */ | |
8436 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8437 | spa->spa_all_vdev_zaps, tx)); | |
8438 | ||
8439 | /* Replace the old AVZ in the dir obj with the new one */ | |
8440 | VERIFY0(zap_update(spa->spa_meta_objset, | |
8441 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
8442 | sizeof (new_avz), 1, &new_avz, tx)); | |
8443 | ||
8444 | spa->spa_all_vdev_zaps = new_avz; | |
8445 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
8446 | zap_cursor_t zc; | |
8447 | zap_attribute_t za; | |
8448 | ||
8449 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
8450 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
8451 | spa->spa_all_vdev_zaps); | |
8452 | zap_cursor_retrieve(&zc, &za) == 0; | |
8453 | zap_cursor_advance(&zc)) { | |
8454 | uint64_t zap = za.za_first_integer; | |
8455 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
8456 | } | |
8457 | ||
8458 | zap_cursor_fini(&zc); | |
8459 | ||
8460 | /* Destroy and unlink the AVZ itself */ | |
8461 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
8462 | spa->spa_all_vdev_zaps, tx)); | |
8463 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
8464 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
8465 | spa->spa_all_vdev_zaps = 0; | |
8466 | } | |
8467 | ||
8468 | if (spa->spa_all_vdev_zaps == 0) { | |
8469 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
8470 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
8471 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
8472 | } | |
8473 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
8474 | ||
8475 | /* Create ZAPs for vdevs that don't have them. */ | |
8476 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
8477 | ||
b128c09f BB |
8478 | config = spa_config_generate(spa, spa->spa_root_vdev, |
8479 | dmu_tx_get_txg(tx), B_FALSE); | |
8480 | ||
ea0b2538 GW |
8481 | /* |
8482 | * If we're upgrading the spa version then make sure that | |
8483 | * the config object gets updated with the correct version. | |
8484 | */ | |
8485 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
8486 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
8487 | spa->spa_uberblock.ub_version); | |
8488 | ||
b128c09f | 8489 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f | 8490 | |
8a5fc748 | 8491 | nvlist_free(spa->spa_config_syncing); |
34dc7c2f BB |
8492 | spa->spa_config_syncing = config; |
8493 | ||
8494 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
8495 | } | |
8496 | ||
9ae529ec | 8497 | static void |
13fe0198 | 8498 | spa_sync_version(void *arg, dmu_tx_t *tx) |
9ae529ec | 8499 | { |
13fe0198 MA |
8500 | uint64_t *versionp = arg; |
8501 | uint64_t version = *versionp; | |
8502 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
9ae529ec CS |
8503 | |
8504 | /* | |
8505 | * Setting the version is special cased when first creating the pool. | |
8506 | */ | |
8507 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
8508 | ||
8dca0a9a | 8509 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
8510 | ASSERT(version >= spa_version(spa)); |
8511 | ||
8512 | spa->spa_uberblock.ub_version = version; | |
8513 | vdev_config_dirty(spa->spa_root_vdev); | |
74756182 MM |
8514 | spa_history_log_internal(spa, "set", tx, "version=%lld", |
8515 | (longlong_t)version); | |
9ae529ec CS |
8516 | } |
8517 | ||
34dc7c2f BB |
8518 | /* |
8519 | * Set zpool properties. | |
8520 | */ | |
8521 | static void | |
13fe0198 | 8522 | spa_sync_props(void *arg, dmu_tx_t *tx) |
34dc7c2f | 8523 | { |
13fe0198 MA |
8524 | nvlist_t *nvp = arg; |
8525 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f | 8526 | objset_t *mos = spa->spa_meta_objset; |
9ae529ec | 8527 | nvpair_t *elem = NULL; |
b128c09f BB |
8528 | |
8529 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 8530 | |
34dc7c2f | 8531 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec CS |
8532 | uint64_t intval; |
8533 | char *strval, *fname; | |
8534 | zpool_prop_t prop; | |
8535 | const char *propname; | |
8536 | zprop_type_t proptype; | |
fa86b5db | 8537 | spa_feature_t fid; |
9ae529ec | 8538 | |
31864e3d BB |
8539 | switch (prop = zpool_name_to_prop(nvpair_name(elem))) { |
8540 | case ZPOOL_PROP_INVAL: | |
9ae529ec CS |
8541 | /* |
8542 | * We checked this earlier in spa_prop_validate(). | |
8543 | */ | |
8544 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
8545 | ||
8546 | fname = strchr(nvpair_name(elem), '@') + 1; | |
fa86b5db | 8547 | VERIFY0(zfeature_lookup_name(fname, &fid)); |
9ae529ec | 8548 | |
fa86b5db | 8549 | spa_feature_enable(spa, fid, tx); |
6f1ffb06 MA |
8550 | spa_history_log_internal(spa, "set", tx, |
8551 | "%s=enabled", nvpair_name(elem)); | |
9ae529ec CS |
8552 | break; |
8553 | ||
34dc7c2f | 8554 | case ZPOOL_PROP_VERSION: |
93cf2076 | 8555 | intval = fnvpair_value_uint64(elem); |
34dc7c2f | 8556 | /* |
4e33ba4c | 8557 | * The version is synced separately before other |
9ae529ec | 8558 | * properties and should be correct by now. |
34dc7c2f | 8559 | */ |
9ae529ec | 8560 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
8561 | break; |
8562 | ||
8563 | case ZPOOL_PROP_ALTROOT: | |
8564 | /* | |
8565 | * 'altroot' is a non-persistent property. It should | |
8566 | * have been set temporarily at creation or import time. | |
8567 | */ | |
8568 | ASSERT(spa->spa_root != NULL); | |
8569 | break; | |
8570 | ||
572e2857 | 8571 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
8572 | case ZPOOL_PROP_CACHEFILE: |
8573 | /* | |
e1cfd73f | 8574 | * 'readonly' and 'cachefile' are also non-persistent |
572e2857 | 8575 | * properties. |
34dc7c2f | 8576 | */ |
34dc7c2f | 8577 | break; |
d96eb2b1 | 8578 | case ZPOOL_PROP_COMMENT: |
93cf2076 | 8579 | strval = fnvpair_value_string(elem); |
d96eb2b1 DM |
8580 | if (spa->spa_comment != NULL) |
8581 | spa_strfree(spa->spa_comment); | |
8582 | spa->spa_comment = spa_strdup(strval); | |
8583 | /* | |
8584 | * We need to dirty the configuration on all the vdevs | |
8585 | * so that their labels get updated. It's unnecessary | |
8586 | * to do this for pool creation since the vdev's | |
4e33ba4c | 8587 | * configuration has already been dirtied. |
d96eb2b1 DM |
8588 | */ |
8589 | if (tx->tx_txg != TXG_INITIAL) | |
8590 | vdev_config_dirty(spa->spa_root_vdev); | |
6f1ffb06 MA |
8591 | spa_history_log_internal(spa, "set", tx, |
8592 | "%s=%s", nvpair_name(elem), strval); | |
d96eb2b1 | 8593 | break; |
34dc7c2f BB |
8594 | default: |
8595 | /* | |
8596 | * Set pool property values in the poolprops mos object. | |
8597 | */ | |
34dc7c2f | 8598 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
8599 | spa->spa_pool_props_object = |
8600 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 8601 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 8602 | tx); |
34dc7c2f | 8603 | } |
34dc7c2f BB |
8604 | |
8605 | /* normalize the property name */ | |
8606 | propname = zpool_prop_to_name(prop); | |
8607 | proptype = zpool_prop_get_type(prop); | |
8608 | ||
8609 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
8610 | ASSERT(proptype == PROP_TYPE_STRING); | |
93cf2076 GW |
8611 | strval = fnvpair_value_string(elem); |
8612 | VERIFY0(zap_update(mos, | |
34dc7c2f | 8613 | spa->spa_pool_props_object, propname, |
93cf2076 | 8614 | 1, strlen(strval) + 1, strval, tx)); |
6f1ffb06 MA |
8615 | spa_history_log_internal(spa, "set", tx, |
8616 | "%s=%s", nvpair_name(elem), strval); | |
34dc7c2f | 8617 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { |
93cf2076 | 8618 | intval = fnvpair_value_uint64(elem); |
34dc7c2f BB |
8619 | |
8620 | if (proptype == PROP_TYPE_INDEX) { | |
8621 | const char *unused; | |
93cf2076 GW |
8622 | VERIFY0(zpool_prop_index_to_string( |
8623 | prop, intval, &unused)); | |
34dc7c2f | 8624 | } |
93cf2076 | 8625 | VERIFY0(zap_update(mos, |
34dc7c2f | 8626 | spa->spa_pool_props_object, propname, |
93cf2076 | 8627 | 8, 1, &intval, tx)); |
6f1ffb06 | 8628 | spa_history_log_internal(spa, "set", tx, |
74756182 MM |
8629 | "%s=%lld", nvpair_name(elem), |
8630 | (longlong_t)intval); | |
34dc7c2f BB |
8631 | } else { |
8632 | ASSERT(0); /* not allowed */ | |
8633 | } | |
8634 | ||
8635 | switch (prop) { | |
8636 | case ZPOOL_PROP_DELEGATION: | |
8637 | spa->spa_delegation = intval; | |
8638 | break; | |
8639 | case ZPOOL_PROP_BOOTFS: | |
8640 | spa->spa_bootfs = intval; | |
8641 | break; | |
8642 | case ZPOOL_PROP_FAILUREMODE: | |
8643 | spa->spa_failmode = intval; | |
8644 | break; | |
1b939560 BB |
8645 | case ZPOOL_PROP_AUTOTRIM: |
8646 | spa->spa_autotrim = intval; | |
8647 | spa_async_request(spa, | |
8648 | SPA_ASYNC_AUTOTRIM_RESTART); | |
8649 | break; | |
9babb374 BB |
8650 | case ZPOOL_PROP_AUTOEXPAND: |
8651 | spa->spa_autoexpand = intval; | |
428870ff BB |
8652 | if (tx->tx_txg != TXG_INITIAL) |
8653 | spa_async_request(spa, | |
8654 | SPA_ASYNC_AUTOEXPAND); | |
8655 | break; | |
379ca9cf OF |
8656 | case ZPOOL_PROP_MULTIHOST: |
8657 | spa->spa_multihost = intval; | |
8658 | break; | |
34dc7c2f BB |
8659 | default: |
8660 | break; | |
8661 | } | |
8662 | } | |
8663 | ||
34dc7c2f | 8664 | } |
b128c09f BB |
8665 | |
8666 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
8667 | } |
8668 | ||
428870ff BB |
8669 | /* |
8670 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
8671 | * reflect the new version this txg, so there must be no changes this | |
8672 | * txg to anything that the upgrade code depends on after it executes. | |
8673 | * Therefore this must be called after dsl_pool_sync() does the sync | |
8674 | * tasks. | |
8675 | */ | |
8676 | static void | |
8677 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
8678 | { | |
8dc2197b SD |
8679 | if (spa_sync_pass(spa) != 1) |
8680 | return; | |
428870ff | 8681 | |
8dc2197b | 8682 | dsl_pool_t *dp = spa->spa_dsl_pool; |
13fe0198 MA |
8683 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
8684 | ||
428870ff BB |
8685 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && |
8686 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
8687 | dsl_pool_create_origin(dp, tx); | |
8688 | ||
8689 | /* Keeping the origin open increases spa_minref */ | |
8690 | spa->spa_minref += 3; | |
8691 | } | |
8692 | ||
8693 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
8694 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
8695 | dsl_pool_upgrade_clones(dp, tx); | |
8696 | } | |
8697 | ||
8698 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
8699 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
8700 | dsl_pool_upgrade_dir_clones(dp, tx); | |
8701 | ||
8702 | /* Keeping the freedir open increases spa_minref */ | |
8703 | spa->spa_minref += 3; | |
8704 | } | |
9ae529ec CS |
8705 | |
8706 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
8707 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8708 | spa_feature_create_zap_objects(spa, tx); | |
8709 | } | |
62bdd5eb DL |
8710 | |
8711 | /* | |
8712 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
8713 | * when possibility to use lz4 compression for metadata was added | |
8714 | * Old pools that have this feature enabled must be upgraded to have | |
8715 | * this feature active | |
8716 | */ | |
8717 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
8718 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
8719 | SPA_FEATURE_LZ4_COMPRESS); | |
8720 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
8721 | SPA_FEATURE_LZ4_COMPRESS); | |
8722 | ||
8723 | if (lz4_en && !lz4_ac) | |
8724 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
8725 | } | |
3c67d83a TH |
8726 | |
8727 | /* | |
8728 | * If we haven't written the salt, do so now. Note that the | |
8729 | * feature may not be activated yet, but that's fine since | |
8730 | * the presence of this ZAP entry is backwards compatible. | |
8731 | */ | |
8732 | if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
8733 | DMU_POOL_CHECKSUM_SALT) == ENOENT) { | |
8734 | VERIFY0(zap_add(spa->spa_meta_objset, | |
8735 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, | |
8736 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
8737 | spa->spa_cksum_salt.zcs_bytes, tx)); | |
8738 | } | |
8739 | ||
13fe0198 | 8740 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
428870ff BB |
8741 | } |
8742 | ||
a1d477c2 MA |
8743 | static void |
8744 | vdev_indirect_state_sync_verify(vdev_t *vd) | |
8745 | { | |
2a8ba608 MM |
8746 | vdev_indirect_mapping_t *vim __maybe_unused = vd->vdev_indirect_mapping; |
8747 | vdev_indirect_births_t *vib __maybe_unused = vd->vdev_indirect_births; | |
a1d477c2 MA |
8748 | |
8749 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
8750 | ASSERT(vim != NULL); | |
8751 | ASSERT(vib != NULL); | |
8752 | } | |
8753 | ||
27f80e85 BB |
8754 | uint64_t obsolete_sm_object = 0; |
8755 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
8756 | if (obsolete_sm_object != 0) { | |
a1d477c2 MA |
8757 | ASSERT(vd->vdev_obsolete_sm != NULL); |
8758 | ASSERT(vd->vdev_removing || | |
8759 | vd->vdev_ops == &vdev_indirect_ops); | |
8760 | ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); | |
8761 | ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); | |
27f80e85 | 8762 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
8763 | space_map_object(vd->vdev_obsolete_sm)); |
8764 | ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, | |
8765 | space_map_allocated(vd->vdev_obsolete_sm)); | |
8766 | } | |
8767 | ASSERT(vd->vdev_obsolete_segments != NULL); | |
8768 | ||
8769 | /* | |
8770 | * Since frees / remaps to an indirect vdev can only | |
8771 | * happen in syncing context, the obsolete segments | |
8772 | * tree must be empty when we start syncing. | |
8773 | */ | |
8774 | ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); | |
8775 | } | |
8776 | ||
34dc7c2f | 8777 | /* |
8dc2197b SD |
8778 | * Set the top-level vdev's max queue depth. Evaluate each top-level's |
8779 | * async write queue depth in case it changed. The max queue depth will | |
8780 | * not change in the middle of syncing out this txg. | |
34dc7c2f | 8781 | */ |
8dc2197b SD |
8782 | static void |
8783 | spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) | |
34dc7c2f | 8784 | { |
8dc2197b SD |
8785 | ASSERT(spa_writeable(spa)); |
8786 | ||
34dc7c2f | 8787 | vdev_t *rvd = spa->spa_root_vdev; |
3dfb57a3 DB |
8788 | uint32_t max_queue_depth = zfs_vdev_async_write_max_active * |
8789 | zfs_vdev_queue_depth_pct / 100; | |
8dc2197b SD |
8790 | metaslab_class_t *normal = spa_normal_class(spa); |
8791 | metaslab_class_t *special = spa_special_class(spa); | |
8792 | metaslab_class_t *dedup = spa_dedup_class(spa); | |
34dc7c2f | 8793 | |
492f64e9 | 8794 | uint64_t slots_per_allocator = 0; |
1c27024e | 8795 | for (int c = 0; c < rvd->vdev_children; c++) { |
3dfb57a3 | 8796 | vdev_t *tvd = rvd->vdev_child[c]; |
cc99f275 | 8797 | |
8dc2197b | 8798 | metaslab_group_t *mg = tvd->vdev_mg; |
cc99f275 DB |
8799 | if (mg == NULL || !metaslab_group_initialized(mg)) |
8800 | continue; | |
3dfb57a3 | 8801 | |
8dc2197b | 8802 | metaslab_class_t *mc = mg->mg_class; |
cc99f275 | 8803 | if (mc != normal && mc != special && mc != dedup) |
3dfb57a3 DB |
8804 | continue; |
8805 | ||
8806 | /* | |
8807 | * It is safe to do a lock-free check here because only async | |
8808 | * allocations look at mg_max_alloc_queue_depth, and async | |
8809 | * allocations all happen from spa_sync(). | |
8810 | */ | |
32d805c3 | 8811 | for (int i = 0; i < mg->mg_allocators; i++) { |
424fd7c3 | 8812 | ASSERT0(zfs_refcount_count( |
32d805c3 MA |
8813 | &(mg->mg_allocator[i].mga_alloc_queue_depth))); |
8814 | } | |
3dfb57a3 | 8815 | mg->mg_max_alloc_queue_depth = max_queue_depth; |
492f64e9 | 8816 | |
32d805c3 MA |
8817 | for (int i = 0; i < mg->mg_allocators; i++) { |
8818 | mg->mg_allocator[i].mga_cur_max_alloc_queue_depth = | |
492f64e9 PD |
8819 | zfs_vdev_def_queue_depth; |
8820 | } | |
8821 | slots_per_allocator += zfs_vdev_def_queue_depth; | |
3dfb57a3 | 8822 | } |
cc99f275 | 8823 | |
492f64e9 | 8824 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
424fd7c3 TS |
8825 | ASSERT0(zfs_refcount_count(&normal->mc_alloc_slots[i])); |
8826 | ASSERT0(zfs_refcount_count(&special->mc_alloc_slots[i])); | |
8827 | ASSERT0(zfs_refcount_count(&dedup->mc_alloc_slots[i])); | |
cc99f275 DB |
8828 | normal->mc_alloc_max_slots[i] = slots_per_allocator; |
8829 | special->mc_alloc_max_slots[i] = slots_per_allocator; | |
8830 | dedup->mc_alloc_max_slots[i] = slots_per_allocator; | |
8831 | } | |
8832 | normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8833 | special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8834 | dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8dc2197b SD |
8835 | } |
8836 | ||
8837 | static void | |
8838 | spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) | |
8839 | { | |
8840 | ASSERT(spa_writeable(spa)); | |
3dfb57a3 | 8841 | |
8dc2197b | 8842 | vdev_t *rvd = spa->spa_root_vdev; |
a1d477c2 MA |
8843 | for (int c = 0; c < rvd->vdev_children; c++) { |
8844 | vdev_t *vd = rvd->vdev_child[c]; | |
8845 | vdev_indirect_state_sync_verify(vd); | |
8846 | ||
8847 | if (vdev_indirect_should_condense(vd)) { | |
8848 | spa_condense_indirect_start_sync(vd, tx); | |
8849 | break; | |
8850 | } | |
8851 | } | |
8dc2197b SD |
8852 | } |
8853 | ||
8854 | static void | |
8855 | spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) | |
8856 | { | |
8857 | objset_t *mos = spa->spa_meta_objset; | |
8858 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
8859 | uint64_t txg = tx->tx_txg; | |
8860 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
a1d477c2 | 8861 | |
34dc7c2f | 8862 | do { |
428870ff | 8863 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
8864 | |
8865 | spa_sync_config_object(spa, tx); | |
8866 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
8867 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
8868 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
8869 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
8870 | spa_errlog_sync(spa, txg); | |
8871 | dsl_pool_sync(dp, txg); | |
8872 | ||
93e28d66 SD |
8873 | if (pass < zfs_sync_pass_deferred_free || |
8874 | spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) { | |
8875 | /* | |
8876 | * If the log space map feature is active we don't | |
8877 | * care about deferred frees and the deferred bpobj | |
8878 | * as the log space map should effectively have the | |
8879 | * same results (i.e. appending only to one object). | |
8880 | */ | |
e8b96c60 | 8881 | spa_sync_frees(spa, free_bpl, tx); |
428870ff | 8882 | } else { |
905edb40 MA |
8883 | /* |
8884 | * We can not defer frees in pass 1, because | |
8885 | * we sync the deferred frees later in pass 1. | |
8886 | */ | |
8887 | ASSERT3U(pass, >, 1); | |
37f03da8 | 8888 | bplist_iterate(free_bpl, bpobj_enqueue_alloc_cb, |
e8b96c60 | 8889 | &spa->spa_deferred_bpobj, tx); |
34dc7c2f BB |
8890 | } |
8891 | ||
428870ff BB |
8892 | ddt_sync(spa, txg); |
8893 | dsl_scan_sync(dp, tx); | |
8dc2197b SD |
8894 | svr_sync(spa, tx); |
8895 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 8896 | |
93e28d66 SD |
8897 | spa_flush_metaslabs(spa, tx); |
8898 | ||
8dc2197b | 8899 | vdev_t *vd = NULL; |
a1d477c2 MA |
8900 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) |
8901 | != NULL) | |
428870ff BB |
8902 | vdev_sync(vd, txg); |
8903 | ||
8dc2197b SD |
8904 | /* |
8905 | * Note: We need to check if the MOS is dirty because we could | |
8906 | * have marked the MOS dirty without updating the uberblock | |
8907 | * (e.g. if we have sync tasks but no dirty user data). We need | |
8908 | * to check the uberblock's rootbp because it is updated if we | |
8909 | * have synced out dirty data (though in this case the MOS will | |
8910 | * most likely also be dirty due to second order effects, we | |
8911 | * don't want to rely on that here). | |
8912 | */ | |
8913 | if (pass == 1 && | |
8914 | spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
8915 | !dmu_objset_is_dirty(mos, txg)) { | |
905edb40 | 8916 | /* |
8dc2197b SD |
8917 | * Nothing changed on the first pass, therefore this |
8918 | * TXG is a no-op. Avoid syncing deferred frees, so | |
8919 | * that we can keep this TXG as a no-op. | |
905edb40 | 8920 | */ |
8dc2197b SD |
8921 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); |
8922 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
8923 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
8924 | ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); | |
8925 | break; | |
905edb40 | 8926 | } |
34dc7c2f | 8927 | |
8dc2197b | 8928 | spa_sync_deferred_frees(spa, tx); |
428870ff | 8929 | } while (dmu_objset_is_dirty(mos, txg)); |
8dc2197b | 8930 | } |
34dc7c2f | 8931 | |
8dc2197b SD |
8932 | /* |
8933 | * Rewrite the vdev configuration (which includes the uberblock) to | |
8934 | * commit the transaction group. | |
8935 | * | |
8936 | * If there are no dirty vdevs, we sync the uberblock to a few random | |
8937 | * top-level vdevs that are known to be visible in the config cache | |
8938 | * (see spa_vdev_add() for a complete description). If there *are* dirty | |
8939 | * vdevs, sync the uberblock to all vdevs. | |
8940 | */ | |
8941 | static void | |
8942 | spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) | |
8943 | { | |
8944 | vdev_t *rvd = spa->spa_root_vdev; | |
8945 | uint64_t txg = tx->tx_txg; | |
a1d477c2 | 8946 | |
b128c09f | 8947 | for (;;) { |
8dc2197b SD |
8948 | int error = 0; |
8949 | ||
b128c09f BB |
8950 | /* |
8951 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
8952 | * while we're attempting to write the vdev labels. | |
8953 | */ | |
8954 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
8955 | ||
8956 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
d2734cce | 8957 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; |
b128c09f BB |
8958 | int svdcount = 0; |
8959 | int children = rvd->vdev_children; | |
8960 | int c0 = spa_get_random(children); | |
b128c09f | 8961 | |
1c27024e | 8962 | for (int c = 0; c < children; c++) { |
8dc2197b SD |
8963 | vdev_t *vd = |
8964 | rvd->vdev_child[(c0 + c) % children]; | |
d2734cce SD |
8965 | |
8966 | /* Stop when revisiting the first vdev */ | |
8967 | if (c > 0 && svd[0] == vd) | |
8968 | break; | |
8969 | ||
8dc2197b SD |
8970 | if (vd->vdev_ms_array == 0 || |
8971 | vd->vdev_islog || | |
a1d477c2 | 8972 | !vdev_is_concrete(vd)) |
b128c09f | 8973 | continue; |
d2734cce | 8974 | |
b128c09f | 8975 | svd[svdcount++] = vd; |
6cb8e530 | 8976 | if (svdcount == SPA_SYNC_MIN_VDEVS) |
b128c09f BB |
8977 | break; |
8978 | } | |
b6fcb792 | 8979 | error = vdev_config_sync(svd, svdcount, txg); |
b128c09f BB |
8980 | } else { |
8981 | error = vdev_config_sync(rvd->vdev_child, | |
b6fcb792 | 8982 | rvd->vdev_children, txg); |
34dc7c2f | 8983 | } |
34dc7c2f | 8984 | |
3bc7e0fb GW |
8985 | if (error == 0) |
8986 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
8987 | ||
b128c09f BB |
8988 | spa_config_exit(spa, SCL_STATE, FTAG); |
8989 | ||
8990 | if (error == 0) | |
8991 | break; | |
cec3a0a1 | 8992 | zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); |
b128c09f BB |
8993 | zio_resume_wait(spa); |
8994 | } | |
8dc2197b SD |
8995 | } |
8996 | ||
8997 | /* | |
8998 | * Sync the specified transaction group. New blocks may be dirtied as | |
8999 | * part of the process, so we iterate until it converges. | |
9000 | */ | |
9001 | void | |
9002 | spa_sync(spa_t *spa, uint64_t txg) | |
9003 | { | |
9004 | vdev_t *vd = NULL; | |
9005 | ||
9006 | VERIFY(spa_writeable(spa)); | |
9007 | ||
9008 | /* | |
9009 | * Wait for i/os issued in open context that need to complete | |
9010 | * before this txg syncs. | |
9011 | */ | |
9012 | (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); | |
9013 | spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, | |
9014 | ZIO_FLAG_CANFAIL); | |
9015 | ||
9016 | /* | |
9017 | * Lock out configuration changes. | |
9018 | */ | |
9019 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
9020 | ||
9021 | spa->spa_syncing_txg = txg; | |
9022 | spa->spa_sync_pass = 0; | |
9023 | ||
9024 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
9025 | mutex_enter(&spa->spa_alloc_locks[i]); | |
9026 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
9027 | mutex_exit(&spa->spa_alloc_locks[i]); | |
9028 | } | |
9029 | ||
9030 | /* | |
9031 | * If there are any pending vdev state changes, convert them | |
9032 | * into config changes that go out with this transaction group. | |
9033 | */ | |
9034 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
9035 | while (list_head(&spa->spa_state_dirty_list) != NULL) { | |
9036 | /* | |
9037 | * We need the write lock here because, for aux vdevs, | |
9038 | * calling vdev_config_dirty() modifies sav_config. | |
9039 | * This is ugly and will become unnecessary when we | |
9040 | * eliminate the aux vdev wart by integrating all vdevs | |
9041 | * into the root vdev tree. | |
9042 | */ | |
9043 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9044 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
9045 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
9046 | vdev_state_clean(vd); | |
9047 | vdev_config_dirty(vd); | |
9048 | } | |
9049 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9050 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9051 | } | |
9052 | spa_config_exit(spa, SCL_STATE, FTAG); | |
9053 | ||
9054 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
9055 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
9056 | ||
9057 | spa->spa_sync_starttime = gethrtime(); | |
9058 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); | |
9059 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, | |
9060 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
9061 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
9062 | ||
9063 | /* | |
9064 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
9065 | * set spa_deflate if we have no raid-z vdevs. | |
9066 | */ | |
9067 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
9068 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
9069 | vdev_t *rvd = spa->spa_root_vdev; | |
9070 | ||
9071 | int i; | |
9072 | for (i = 0; i < rvd->vdev_children; i++) { | |
9073 | vd = rvd->vdev_child[i]; | |
9074 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
9075 | break; | |
9076 | } | |
9077 | if (i == rvd->vdev_children) { | |
9078 | spa->spa_deflate = TRUE; | |
9079 | VERIFY0(zap_add(spa->spa_meta_objset, | |
9080 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
9081 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
9082 | } | |
9083 | } | |
9084 | ||
9085 | spa_sync_adjust_vdev_max_queue_depth(spa); | |
9086 | ||
9087 | spa_sync_condense_indirect(spa, tx); | |
9088 | ||
9089 | spa_sync_iterate_to_convergence(spa, tx); | |
9090 | ||
9091 | #ifdef ZFS_DEBUG | |
9092 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
9093 | /* | |
9094 | * Make sure that the number of ZAPs for all the vdevs matches | |
9095 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
9096 | * called if the config is dirty; otherwise there may be | |
9097 | * outstanding AVZ operations that weren't completed in | |
9098 | * spa_sync_config_object. | |
9099 | */ | |
9100 | uint64_t all_vdev_zap_entry_count; | |
9101 | ASSERT0(zap_count(spa->spa_meta_objset, | |
9102 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
9103 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
9104 | all_vdev_zap_entry_count); | |
9105 | } | |
9106 | #endif | |
9107 | ||
9108 | if (spa->spa_vdev_removal != NULL) { | |
9109 | ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); | |
9110 | } | |
9111 | ||
9112 | spa_sync_rewrite_vdev_config(spa, tx); | |
34dc7c2f BB |
9113 | dmu_tx_commit(tx); |
9114 | ||
57ddcda1 | 9115 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 GW |
9116 | spa->spa_deadman_tqid = 0; |
9117 | ||
34dc7c2f BB |
9118 | /* |
9119 | * Clear the dirty config list. | |
9120 | */ | |
b128c09f | 9121 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
9122 | vdev_config_clean(vd); |
9123 | ||
9124 | /* | |
9125 | * Now that the new config has synced transactionally, | |
9126 | * let it become visible to the config cache. | |
9127 | */ | |
9128 | if (spa->spa_config_syncing != NULL) { | |
9129 | spa_config_set(spa, spa->spa_config_syncing); | |
9130 | spa->spa_config_txg = txg; | |
9131 | spa->spa_config_syncing = NULL; | |
9132 | } | |
9133 | ||
428870ff | 9134 | dsl_pool_sync_done(dp, txg); |
34dc7c2f | 9135 | |
492f64e9 PD |
9136 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
9137 | mutex_enter(&spa->spa_alloc_locks[i]); | |
9138 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
9139 | mutex_exit(&spa->spa_alloc_locks[i]); | |
9140 | } | |
3dfb57a3 | 9141 | |
34dc7c2f BB |
9142 | /* |
9143 | * Update usable space statistics. | |
9144 | */ | |
619f0976 GW |
9145 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) |
9146 | != NULL) | |
34dc7c2f | 9147 | vdev_sync_done(vd, txg); |
f09fda50 PD |
9148 | |
9149 | metaslab_class_evict_old(spa->spa_normal_class, txg); | |
9150 | metaslab_class_evict_old(spa->spa_log_class, txg); | |
9151 | ||
93e28d66 | 9152 | spa_sync_close_syncing_log_sm(spa); |
34dc7c2f | 9153 | |
428870ff BB |
9154 | spa_update_dspace(spa); |
9155 | ||
34dc7c2f BB |
9156 | /* |
9157 | * It had better be the case that we didn't dirty anything | |
9158 | * since vdev_config_sync(). | |
9159 | */ | |
9160 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
9161 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
9162 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff | 9163 | |
d2734cce SD |
9164 | while (zfs_pause_spa_sync) |
9165 | delay(1); | |
9166 | ||
428870ff | 9167 | spa->spa_sync_pass = 0; |
34dc7c2f | 9168 | |
55922e73 GW |
9169 | /* |
9170 | * Update the last synced uberblock here. We want to do this at | |
9171 | * the end of spa_sync() so that consumers of spa_last_synced_txg() | |
9172 | * will be guaranteed that all the processing associated with | |
9173 | * that txg has been completed. | |
9174 | */ | |
9175 | spa->spa_ubsync = spa->spa_uberblock; | |
b128c09f | 9176 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 9177 | |
428870ff BB |
9178 | spa_handle_ignored_writes(spa); |
9179 | ||
34dc7c2f BB |
9180 | /* |
9181 | * If any async tasks have been requested, kick them off. | |
9182 | */ | |
9183 | spa_async_dispatch(spa); | |
9184 | } | |
9185 | ||
9186 | /* | |
9187 | * Sync all pools. We don't want to hold the namespace lock across these | |
9188 | * operations, so we take a reference on the spa_t and drop the lock during the | |
9189 | * sync. | |
9190 | */ | |
9191 | void | |
9192 | spa_sync_allpools(void) | |
9193 | { | |
9194 | spa_t *spa = NULL; | |
9195 | mutex_enter(&spa_namespace_lock); | |
9196 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
9197 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
9198 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
9199 | continue; |
9200 | spa_open_ref(spa, FTAG); | |
9201 | mutex_exit(&spa_namespace_lock); | |
9202 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9203 | mutex_enter(&spa_namespace_lock); | |
9204 | spa_close(spa, FTAG); | |
9205 | } | |
9206 | mutex_exit(&spa_namespace_lock); | |
9207 | } | |
9208 | ||
9209 | /* | |
9210 | * ========================================================================== | |
9211 | * Miscellaneous routines | |
9212 | * ========================================================================== | |
9213 | */ | |
9214 | ||
9215 | /* | |
9216 | * Remove all pools in the system. | |
9217 | */ | |
9218 | void | |
9219 | spa_evict_all(void) | |
9220 | { | |
9221 | spa_t *spa; | |
9222 | ||
9223 | /* | |
9224 | * Remove all cached state. All pools should be closed now, | |
9225 | * so every spa in the AVL tree should be unreferenced. | |
9226 | */ | |
9227 | mutex_enter(&spa_namespace_lock); | |
9228 | while ((spa = spa_next(NULL)) != NULL) { | |
9229 | /* | |
9230 | * Stop async tasks. The async thread may need to detach | |
9231 | * a device that's been replaced, which requires grabbing | |
9232 | * spa_namespace_lock, so we must drop it here. | |
9233 | */ | |
9234 | spa_open_ref(spa, FTAG); | |
9235 | mutex_exit(&spa_namespace_lock); | |
9236 | spa_async_suspend(spa); | |
9237 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
9238 | spa_close(spa, FTAG); |
9239 | ||
9240 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
9241 | spa_unload(spa); | |
9242 | spa_deactivate(spa); | |
9243 | } | |
9244 | spa_remove(spa); | |
9245 | } | |
9246 | mutex_exit(&spa_namespace_lock); | |
9247 | } | |
9248 | ||
9249 | vdev_t * | |
9babb374 | 9250 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 9251 | { |
b128c09f BB |
9252 | vdev_t *vd; |
9253 | int i; | |
9254 | ||
9255 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
9256 | return (vd); | |
9257 | ||
9babb374 | 9258 | if (aux) { |
b128c09f BB |
9259 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
9260 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
9261 | if (vd->vdev_guid == guid) |
9262 | return (vd); | |
9263 | } | |
9264 | ||
9265 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
9266 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
9267 | if (vd->vdev_guid == guid) |
9268 | return (vd); | |
9269 | } | |
9270 | } | |
9271 | ||
9272 | return (NULL); | |
34dc7c2f BB |
9273 | } |
9274 | ||
9275 | void | |
9276 | spa_upgrade(spa_t *spa, uint64_t version) | |
9277 | { | |
572e2857 BB |
9278 | ASSERT(spa_writeable(spa)); |
9279 | ||
b128c09f | 9280 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
9281 | |
9282 | /* | |
9283 | * This should only be called for a non-faulted pool, and since a | |
9284 | * future version would result in an unopenable pool, this shouldn't be | |
9285 | * possible. | |
9286 | */ | |
8dca0a9a | 9287 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
9b67f605 | 9288 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); |
34dc7c2f BB |
9289 | |
9290 | spa->spa_uberblock.ub_version = version; | |
9291 | vdev_config_dirty(spa->spa_root_vdev); | |
9292 | ||
b128c09f | 9293 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
9294 | |
9295 | txg_wait_synced(spa_get_dsl(spa), 0); | |
9296 | } | |
9297 | ||
9298 | boolean_t | |
9299 | spa_has_spare(spa_t *spa, uint64_t guid) | |
9300 | { | |
9301 | int i; | |
9302 | uint64_t spareguid; | |
9303 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9304 | ||
9305 | for (i = 0; i < sav->sav_count; i++) | |
9306 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
9307 | return (B_TRUE); | |
9308 | ||
9309 | for (i = 0; i < sav->sav_npending; i++) { | |
9310 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
9311 | &spareguid) == 0 && spareguid == guid) | |
9312 | return (B_TRUE); | |
9313 | } | |
9314 | ||
9315 | return (B_FALSE); | |
9316 | } | |
9317 | ||
b128c09f BB |
9318 | /* |
9319 | * Check if a pool has an active shared spare device. | |
9320 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
9321 | */ | |
9322 | static boolean_t | |
9323 | spa_has_active_shared_spare(spa_t *spa) | |
9324 | { | |
9325 | int i, refcnt; | |
9326 | uint64_t pool; | |
9327 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
9328 | ||
9329 | for (i = 0; i < sav->sav_count; i++) { | |
9330 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
9331 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
9332 | refcnt > 2) | |
9333 | return (B_TRUE); | |
9334 | } | |
9335 | ||
9336 | return (B_FALSE); | |
9337 | } | |
9338 | ||
93e28d66 SD |
9339 | uint64_t |
9340 | spa_total_metaslabs(spa_t *spa) | |
9341 | { | |
9342 | vdev_t *rvd = spa->spa_root_vdev; | |
9343 | ||
9344 | uint64_t m = 0; | |
9345 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
9346 | vdev_t *vd = rvd->vdev_child[c]; | |
9347 | if (!vdev_is_concrete(vd)) | |
9348 | continue; | |
9349 | m += vd->vdev_ms_count; | |
9350 | } | |
9351 | return (m); | |
9352 | } | |
9353 | ||
e60e158e JG |
9354 | /* |
9355 | * Notify any waiting threads that some activity has switched from being in- | |
9356 | * progress to not-in-progress so that the thread can wake up and determine | |
9357 | * whether it is finished waiting. | |
9358 | */ | |
9359 | void | |
9360 | spa_notify_waiters(spa_t *spa) | |
9361 | { | |
9362 | /* | |
9363 | * Acquiring spa_activities_lock here prevents the cv_broadcast from | |
9364 | * happening between the waiting thread's check and cv_wait. | |
9365 | */ | |
9366 | mutex_enter(&spa->spa_activities_lock); | |
9367 | cv_broadcast(&spa->spa_activities_cv); | |
9368 | mutex_exit(&spa->spa_activities_lock); | |
9369 | } | |
9370 | ||
9371 | /* | |
9372 | * Notify any waiting threads that the pool is exporting, and then block until | |
9373 | * they are finished using the spa_t. | |
9374 | */ | |
9375 | void | |
9376 | spa_wake_waiters(spa_t *spa) | |
9377 | { | |
9378 | mutex_enter(&spa->spa_activities_lock); | |
9379 | spa->spa_waiters_cancel = B_TRUE; | |
9380 | cv_broadcast(&spa->spa_activities_cv); | |
9381 | while (spa->spa_waiters != 0) | |
9382 | cv_wait(&spa->spa_waiters_cv, &spa->spa_activities_lock); | |
9383 | spa->spa_waiters_cancel = B_FALSE; | |
9384 | mutex_exit(&spa->spa_activities_lock); | |
9385 | } | |
9386 | ||
2288d419 | 9387 | /* Whether the vdev or any of its descendants are being initialized/trimmed. */ |
e60e158e | 9388 | static boolean_t |
2288d419 | 9389 | spa_vdev_activity_in_progress_impl(vdev_t *vd, zpool_wait_activity_t activity) |
e60e158e JG |
9390 | { |
9391 | spa_t *spa = vd->vdev_spa; | |
e60e158e JG |
9392 | |
9393 | ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER)); | |
9394 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
2288d419 BB |
9395 | ASSERT(activity == ZPOOL_WAIT_INITIALIZE || |
9396 | activity == ZPOOL_WAIT_TRIM); | |
9397 | ||
9398 | kmutex_t *lock = activity == ZPOOL_WAIT_INITIALIZE ? | |
9399 | &vd->vdev_initialize_lock : &vd->vdev_trim_lock; | |
e60e158e JG |
9400 | |
9401 | mutex_exit(&spa->spa_activities_lock); | |
2288d419 | 9402 | mutex_enter(lock); |
e60e158e JG |
9403 | mutex_enter(&spa->spa_activities_lock); |
9404 | ||
2288d419 BB |
9405 | boolean_t in_progress = (activity == ZPOOL_WAIT_INITIALIZE) ? |
9406 | (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) : | |
9407 | (vd->vdev_trim_state == VDEV_TRIM_ACTIVE); | |
9408 | mutex_exit(lock); | |
e60e158e | 9409 | |
2288d419 | 9410 | if (in_progress) |
e60e158e JG |
9411 | return (B_TRUE); |
9412 | ||
9413 | for (int i = 0; i < vd->vdev_children; i++) { | |
2288d419 BB |
9414 | if (spa_vdev_activity_in_progress_impl(vd->vdev_child[i], |
9415 | activity)) | |
e60e158e JG |
9416 | return (B_TRUE); |
9417 | } | |
9418 | ||
9419 | return (B_FALSE); | |
9420 | } | |
9421 | ||
9422 | /* | |
9423 | * If use_guid is true, this checks whether the vdev specified by guid is | |
2288d419 BB |
9424 | * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool |
9425 | * is being initialized/trimmed. The caller must hold the config lock and | |
9426 | * spa_activities_lock. | |
e60e158e JG |
9427 | */ |
9428 | static int | |
2288d419 BB |
9429 | spa_vdev_activity_in_progress(spa_t *spa, boolean_t use_guid, uint64_t guid, |
9430 | zpool_wait_activity_t activity, boolean_t *in_progress) | |
e60e158e JG |
9431 | { |
9432 | mutex_exit(&spa->spa_activities_lock); | |
9433 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9434 | mutex_enter(&spa->spa_activities_lock); | |
9435 | ||
9436 | vdev_t *vd; | |
9437 | if (use_guid) { | |
9438 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
9439 | if (vd == NULL || !vd->vdev_ops->vdev_op_leaf) { | |
9440 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9441 | return (EINVAL); | |
9442 | } | |
9443 | } else { | |
9444 | vd = spa->spa_root_vdev; | |
9445 | } | |
9446 | ||
2288d419 | 9447 | *in_progress = spa_vdev_activity_in_progress_impl(vd, activity); |
e60e158e JG |
9448 | |
9449 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9450 | return (0); | |
9451 | } | |
9452 | ||
9453 | /* | |
9454 | * Locking for waiting threads | |
9455 | * --------------------------- | |
9456 | * | |
9457 | * Waiting threads need a way to check whether a given activity is in progress, | |
9458 | * and then, if it is, wait for it to complete. Each activity will have some | |
9459 | * in-memory representation of the relevant on-disk state which can be used to | |
9460 | * determine whether or not the activity is in progress. The in-memory state and | |
9461 | * the locking used to protect it will be different for each activity, and may | |
9462 | * not be suitable for use with a cvar (e.g., some state is protected by the | |
9463 | * config lock). To allow waiting threads to wait without any races, another | |
9464 | * lock, spa_activities_lock, is used. | |
9465 | * | |
9466 | * When the state is checked, both the activity-specific lock (if there is one) | |
9467 | * and spa_activities_lock are held. In some cases, the activity-specific lock | |
9468 | * is acquired explicitly (e.g. the config lock). In others, the locking is | |
9469 | * internal to some check (e.g. bpobj_is_empty). After checking, the waiting | |
9470 | * thread releases the activity-specific lock and, if the activity is in | |
9471 | * progress, then cv_waits using spa_activities_lock. | |
9472 | * | |
9473 | * The waiting thread is woken when another thread, one completing some | |
9474 | * activity, updates the state of the activity and then calls | |
9475 | * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only | |
9476 | * needs to hold its activity-specific lock when updating the state, and this | |
9477 | * lock can (but doesn't have to) be dropped before calling spa_notify_waiters. | |
9478 | * | |
9479 | * Because spa_notify_waiters acquires spa_activities_lock before broadcasting, | |
9480 | * and because it is held when the waiting thread checks the state of the | |
9481 | * activity, it can never be the case that the completing thread both updates | |
9482 | * the activity state and cv_broadcasts in between the waiting thread's check | |
9483 | * and cv_wait. Thus, a waiting thread can never miss a wakeup. | |
9484 | * | |
9485 | * In order to prevent deadlock, when the waiting thread does its check, in some | |
9486 | * cases it will temporarily drop spa_activities_lock in order to acquire the | |
9487 | * activity-specific lock. The order in which spa_activities_lock and the | |
9488 | * activity specific lock are acquired in the waiting thread is determined by | |
9489 | * the order in which they are acquired in the completing thread; if the | |
9490 | * completing thread calls spa_notify_waiters with the activity-specific lock | |
9491 | * held, then the waiting thread must also acquire the activity-specific lock | |
9492 | * first. | |
9493 | */ | |
9494 | ||
9495 | static int | |
9496 | spa_activity_in_progress(spa_t *spa, zpool_wait_activity_t activity, | |
9497 | boolean_t use_tag, uint64_t tag, boolean_t *in_progress) | |
9498 | { | |
9499 | int error = 0; | |
9500 | ||
9501 | ASSERT(MUTEX_HELD(&spa->spa_activities_lock)); | |
9502 | ||
9503 | switch (activity) { | |
9504 | case ZPOOL_WAIT_CKPT_DISCARD: | |
9505 | *in_progress = | |
9506 | (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT) && | |
9507 | zap_contains(spa_meta_objset(spa), | |
9508 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ZPOOL_CHECKPOINT) == | |
9509 | ENOENT); | |
9510 | break; | |
9511 | case ZPOOL_WAIT_FREE: | |
9512 | *in_progress = ((spa_version(spa) >= SPA_VERSION_DEADLISTS && | |
9513 | !bpobj_is_empty(&spa->spa_dsl_pool->dp_free_bpobj)) || | |
9514 | spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY) || | |
9515 | spa_livelist_delete_check(spa)); | |
9516 | break; | |
9517 | case ZPOOL_WAIT_INITIALIZE: | |
2288d419 BB |
9518 | case ZPOOL_WAIT_TRIM: |
9519 | error = spa_vdev_activity_in_progress(spa, use_tag, tag, | |
9520 | activity, in_progress); | |
e60e158e JG |
9521 | break; |
9522 | case ZPOOL_WAIT_REPLACE: | |
9523 | mutex_exit(&spa->spa_activities_lock); | |
9524 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
9525 | mutex_enter(&spa->spa_activities_lock); | |
9526 | ||
9527 | *in_progress = vdev_replace_in_progress(spa->spa_root_vdev); | |
9528 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
9529 | break; | |
9530 | case ZPOOL_WAIT_REMOVE: | |
9531 | *in_progress = (spa->spa_removing_phys.sr_state == | |
9532 | DSS_SCANNING); | |
9533 | break; | |
9534 | case ZPOOL_WAIT_RESILVER: | |
9a49d3f3 BB |
9535 | if ((*in_progress = vdev_rebuild_active(spa->spa_root_vdev))) |
9536 | break; | |
9537 | /* fall through */ | |
e60e158e JG |
9538 | case ZPOOL_WAIT_SCRUB: |
9539 | { | |
9540 | boolean_t scanning, paused, is_scrub; | |
9541 | dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; | |
9542 | ||
9543 | is_scrub = (scn->scn_phys.scn_func == POOL_SCAN_SCRUB); | |
9544 | scanning = (scn->scn_phys.scn_state == DSS_SCANNING); | |
9545 | paused = dsl_scan_is_paused_scrub(scn); | |
9546 | *in_progress = (scanning && !paused && | |
9547 | is_scrub == (activity == ZPOOL_WAIT_SCRUB)); | |
9548 | break; | |
9549 | } | |
9550 | default: | |
9551 | panic("unrecognized value for activity %d", activity); | |
9552 | } | |
9553 | ||
9554 | return (error); | |
9555 | } | |
9556 | ||
9557 | static int | |
9558 | spa_wait_common(const char *pool, zpool_wait_activity_t activity, | |
9559 | boolean_t use_tag, uint64_t tag, boolean_t *waited) | |
9560 | { | |
9561 | /* | |
9562 | * The tag is used to distinguish between instances of an activity. | |
2288d419 BB |
9563 | * 'initialize' and 'trim' are the only activities that we use this for. |
9564 | * The other activities can only have a single instance in progress in a | |
9565 | * pool at one time, making the tag unnecessary. | |
e60e158e JG |
9566 | * |
9567 | * There can be multiple devices being replaced at once, but since they | |
9568 | * all finish once resilvering finishes, we don't bother keeping track | |
9569 | * of them individually, we just wait for them all to finish. | |
9570 | */ | |
2288d419 BB |
9571 | if (use_tag && activity != ZPOOL_WAIT_INITIALIZE && |
9572 | activity != ZPOOL_WAIT_TRIM) | |
e60e158e JG |
9573 | return (EINVAL); |
9574 | ||
9575 | if (activity < 0 || activity >= ZPOOL_WAIT_NUM_ACTIVITIES) | |
9576 | return (EINVAL); | |
9577 | ||
9578 | spa_t *spa; | |
9579 | int error = spa_open(pool, &spa, FTAG); | |
9580 | if (error != 0) | |
9581 | return (error); | |
9582 | ||
9583 | /* | |
9584 | * Increment the spa's waiter count so that we can call spa_close and | |
9585 | * still ensure that the spa_t doesn't get freed before this thread is | |
9586 | * finished with it when the pool is exported. We want to call spa_close | |
9587 | * before we start waiting because otherwise the additional ref would | |
9588 | * prevent the pool from being exported or destroyed throughout the | |
9589 | * potentially long wait. | |
9590 | */ | |
9591 | mutex_enter(&spa->spa_activities_lock); | |
9592 | spa->spa_waiters++; | |
9593 | spa_close(spa, FTAG); | |
9594 | ||
9595 | *waited = B_FALSE; | |
9596 | for (;;) { | |
9597 | boolean_t in_progress; | |
9598 | error = spa_activity_in_progress(spa, activity, use_tag, tag, | |
9599 | &in_progress); | |
9600 | ||
b24771a8 | 9601 | if (error || !in_progress || spa->spa_waiters_cancel) |
e60e158e JG |
9602 | break; |
9603 | ||
9604 | *waited = B_TRUE; | |
9605 | ||
9606 | if (cv_wait_sig(&spa->spa_activities_cv, | |
9607 | &spa->spa_activities_lock) == 0) { | |
9608 | error = EINTR; | |
9609 | break; | |
9610 | } | |
9611 | } | |
9612 | ||
9613 | spa->spa_waiters--; | |
9614 | cv_signal(&spa->spa_waiters_cv); | |
9615 | mutex_exit(&spa->spa_activities_lock); | |
9616 | ||
9617 | return (error); | |
9618 | } | |
9619 | ||
9620 | /* | |
9621 | * Wait for a particular instance of the specified activity to complete, where | |
9622 | * the instance is identified by 'tag' | |
9623 | */ | |
9624 | int | |
9625 | spa_wait_tag(const char *pool, zpool_wait_activity_t activity, uint64_t tag, | |
9626 | boolean_t *waited) | |
9627 | { | |
9628 | return (spa_wait_common(pool, activity, B_TRUE, tag, waited)); | |
9629 | } | |
9630 | ||
9631 | /* | |
9632 | * Wait for all instances of the specified activity complete | |
9633 | */ | |
9634 | int | |
9635 | spa_wait(const char *pool, zpool_wait_activity_t activity, boolean_t *waited) | |
9636 | { | |
9637 | ||
9638 | return (spa_wait_common(pool, activity, B_FALSE, 0, waited)); | |
9639 | } | |
9640 | ||
a1d477c2 | 9641 | sysevent_t * |
12fa0466 DE |
9642 | spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
9643 | { | |
9644 | sysevent_t *ev = NULL; | |
9645 | #ifdef _KERNEL | |
9646 | nvlist_t *resource; | |
9647 | ||
9648 | resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl); | |
9649 | if (resource) { | |
9650 | ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP); | |
9651 | ev->resource = resource; | |
9652 | } | |
9653 | #endif | |
9654 | return (ev); | |
9655 | } | |
9656 | ||
a1d477c2 | 9657 | void |
12fa0466 DE |
9658 | spa_event_post(sysevent_t *ev) |
9659 | { | |
9660 | #ifdef _KERNEL | |
9661 | if (ev) { | |
9662 | zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb); | |
9663 | kmem_free(ev, sizeof (*ev)); | |
9664 | } | |
9665 | #endif | |
9666 | } | |
9667 | ||
34dc7c2f | 9668 | /* |
fb390aaf HR |
9669 | * Post a zevent corresponding to the given sysevent. The 'name' must be one |
9670 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
34dc7c2f BB |
9671 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
9672 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
9673 | * or zdb as real changes. | |
9674 | */ | |
9675 | void | |
12fa0466 | 9676 | spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
34dc7c2f | 9677 | { |
12fa0466 | 9678 | spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); |
34dc7c2f | 9679 | } |
c28b2279 | 9680 | |
c28b2279 BB |
9681 | /* state manipulation functions */ |
9682 | EXPORT_SYMBOL(spa_open); | |
9683 | EXPORT_SYMBOL(spa_open_rewind); | |
9684 | EXPORT_SYMBOL(spa_get_stats); | |
9685 | EXPORT_SYMBOL(spa_create); | |
c28b2279 BB |
9686 | EXPORT_SYMBOL(spa_import); |
9687 | EXPORT_SYMBOL(spa_tryimport); | |
9688 | EXPORT_SYMBOL(spa_destroy); | |
9689 | EXPORT_SYMBOL(spa_export); | |
9690 | EXPORT_SYMBOL(spa_reset); | |
9691 | EXPORT_SYMBOL(spa_async_request); | |
9692 | EXPORT_SYMBOL(spa_async_suspend); | |
9693 | EXPORT_SYMBOL(spa_async_resume); | |
9694 | EXPORT_SYMBOL(spa_inject_addref); | |
9695 | EXPORT_SYMBOL(spa_inject_delref); | |
9696 | EXPORT_SYMBOL(spa_scan_stat_init); | |
9697 | EXPORT_SYMBOL(spa_scan_get_stats); | |
9698 | ||
e1cfd73f | 9699 | /* device manipulation */ |
c28b2279 BB |
9700 | EXPORT_SYMBOL(spa_vdev_add); |
9701 | EXPORT_SYMBOL(spa_vdev_attach); | |
9702 | EXPORT_SYMBOL(spa_vdev_detach); | |
c28b2279 BB |
9703 | EXPORT_SYMBOL(spa_vdev_setpath); |
9704 | EXPORT_SYMBOL(spa_vdev_setfru); | |
9705 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
9706 | ||
9707 | /* spare statech is global across all pools) */ | |
9708 | EXPORT_SYMBOL(spa_spare_add); | |
9709 | EXPORT_SYMBOL(spa_spare_remove); | |
9710 | EXPORT_SYMBOL(spa_spare_exists); | |
9711 | EXPORT_SYMBOL(spa_spare_activate); | |
9712 | ||
9713 | /* L2ARC statech is global across all pools) */ | |
9714 | EXPORT_SYMBOL(spa_l2cache_add); | |
9715 | EXPORT_SYMBOL(spa_l2cache_remove); | |
9716 | EXPORT_SYMBOL(spa_l2cache_exists); | |
9717 | EXPORT_SYMBOL(spa_l2cache_activate); | |
9718 | EXPORT_SYMBOL(spa_l2cache_drop); | |
9719 | ||
9720 | /* scanning */ | |
9721 | EXPORT_SYMBOL(spa_scan); | |
9722 | EXPORT_SYMBOL(spa_scan_stop); | |
9723 | ||
9724 | /* spa syncing */ | |
9725 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
9726 | EXPORT_SYMBOL(spa_sync_allpools); | |
9727 | ||
9728 | /* properties */ | |
9729 | EXPORT_SYMBOL(spa_prop_set); | |
9730 | EXPORT_SYMBOL(spa_prop_get); | |
9731 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
9732 | ||
9733 | /* asynchronous event notification */ | |
9734 | EXPORT_SYMBOL(spa_event_notify); | |
dea377c0 | 9735 | |
c8242a96 | 9736 | /* BEGIN CSTYLED */ |
03fdcb9a MM |
9737 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_shift, INT, ZMOD_RW, |
9738 | "log2(fraction of arc that can be used by inflight I/Os when " | |
9739 | "verifying pool during import"); | |
dea377c0 | 9740 | |
03fdcb9a | 9741 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_metadata, INT, ZMOD_RW, |
dea377c0 MA |
9742 | "Set to traverse metadata on pool import"); |
9743 | ||
03fdcb9a | 9744 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_data, INT, ZMOD_RW, |
dea377c0 | 9745 | "Set to traverse data on pool import"); |
dcb6bed1 | 9746 | |
03fdcb9a | 9747 | ZFS_MODULE_PARAM(zfs_spa, spa_, load_print_vdev_tree, INT, ZMOD_RW, |
6cb8e530 PZ |
9748 | "Print vdev tree to zfs_dbgmsg during pool import"); |
9749 | ||
03fdcb9a | 9750 | ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_pct, UINT, ZMOD_RD, |
dcb6bed1 D |
9751 | "Percentage of CPUs to run an IO worker thread"); |
9752 | ||
03fdcb9a MM |
9753 | ZFS_MODULE_PARAM(zfs, zfs_, max_missing_tvds, ULONG, ZMOD_RW, |
9754 | "Allow importing pool with up to this number of missing top-level " | |
9755 | "vdevs (in read-only mode)"); | |
6cb8e530 | 9756 | |
03fdcb9a | 9757 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_pause, INT, ZMOD_RW, |
37f03da8 | 9758 | "Set the livelist condense zthr to pause"); |
03fdcb9a MM |
9759 | |
9760 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_pause, INT, ZMOD_RW, | |
37f03da8 SH |
9761 | "Set the livelist condense synctask to pause"); |
9762 | ||
03fdcb9a | 9763 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_cancel, INT, ZMOD_RW, |
37f03da8 | 9764 | "Whether livelist condensing was canceled in the synctask"); |
03fdcb9a MM |
9765 | |
9766 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_cancel, INT, ZMOD_RW, | |
37f03da8 SH |
9767 | "Whether livelist condensing was canceled in the zthr function"); |
9768 | ||
03fdcb9a MM |
9769 | ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, new_alloc, INT, ZMOD_RW, |
9770 | "Whether extra ALLOC blkptrs were added to a livelist entry while it " | |
9771 | "was being condensed"); | |
37f03da8 | 9772 | /* END CSTYLED */ |